GNU Linux-libre 4.9.326-gnu1
[releases.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 <net/switchdev.h>
32 #include <linux/platform_data/b53.h>
33
34 #include "bcm_sf2.h"
35 #include "bcm_sf2_regs.h"
36 #include "b53/b53_priv.h"
37 #include "b53/b53_regs.h"
38
39 static enum dsa_tag_protocol bcm_sf2_sw_get_tag_protocol(struct dsa_switch *ds)
40 {
41         return DSA_TAG_PROTO_BRCM;
42 }
43
44 static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
45 {
46         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
47         unsigned int i;
48         u32 reg;
49
50         /* Enable the IMP Port to be in the same VLAN as the other ports
51          * on a per-port basis such that we only have Port i and IMP in
52          * the same VLAN.
53          */
54         for (i = 0; i < priv->hw_params.num_ports; i++) {
55                 if (!((1 << i) & ds->enabled_port_mask))
56                         continue;
57
58                 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
59                 reg |= (1 << cpu_port);
60                 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
61         }
62 }
63
64 static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
65 {
66         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
67         u32 reg, val;
68
69         /* Enable the port memories */
70         reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
71         reg &= ~P_TXQ_PSM_VDD(port);
72         core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
73
74         /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
75         reg = core_readl(priv, CORE_IMP_CTL);
76         reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
77         reg &= ~(RX_DIS | TX_DIS);
78         core_writel(priv, reg, CORE_IMP_CTL);
79
80         /* Enable forwarding */
81         core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
82
83         /* Enable IMP port in dumb mode */
84         reg = core_readl(priv, CORE_SWITCH_CTRL);
85         reg |= MII_DUMB_FWDG_EN;
86         core_writel(priv, reg, CORE_SWITCH_CTRL);
87
88         /* Resolve which bit controls the Broadcom tag */
89         switch (port) {
90         case 8:
91                 val = BRCM_HDR_EN_P8;
92                 break;
93         case 7:
94                 val = BRCM_HDR_EN_P7;
95                 break;
96         case 5:
97                 val = BRCM_HDR_EN_P5;
98                 break;
99         default:
100                 val = 0;
101                 break;
102         }
103
104         /* Enable Broadcom tags for IMP port */
105         reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
106         reg |= val;
107         core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
108
109         /* Enable reception Broadcom tag for CPU TX (switch RX) to
110          * allow us to tag outgoing frames
111          */
112         reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
113         reg &= ~(1 << port);
114         core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
115
116         /* Enable transmission of Broadcom tags from the switch (CPU RX) to
117          * allow delivering frames to the per-port net_devices
118          */
119         reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
120         reg &= ~(1 << port);
121         core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
122
123         /* Force link status for IMP port */
124         reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
125         reg |= (MII_SW_OR | LINK_STS);
126         core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
127 }
128
129 static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
130 {
131         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
132         u32 reg;
133
134         reg = core_readl(priv, CORE_EEE_EN_CTRL);
135         if (enable)
136                 reg |= 1 << port;
137         else
138                 reg &= ~(1 << port);
139         core_writel(priv, reg, CORE_EEE_EN_CTRL);
140 }
141
142 static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
143 {
144         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
145         u32 reg;
146
147         reg = reg_readl(priv, REG_SPHY_CNTRL);
148         if (enable) {
149                 reg |= PHY_RESET;
150                 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
151                 reg_writel(priv, reg, REG_SPHY_CNTRL);
152                 udelay(21);
153                 reg = reg_readl(priv, REG_SPHY_CNTRL);
154                 reg &= ~PHY_RESET;
155         } else {
156                 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
157                 reg_writel(priv, reg, REG_SPHY_CNTRL);
158                 mdelay(1);
159                 reg |= CK25_DIS;
160         }
161         reg_writel(priv, reg, REG_SPHY_CNTRL);
162
163         /* Use PHY-driven LED signaling */
164         if (!enable) {
165                 reg = reg_readl(priv, REG_LED_CNTRL(0));
166                 reg |= SPDLNK_SRC_SEL;
167                 reg_writel(priv, reg, REG_LED_CNTRL(0));
168         }
169 }
170
171 static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
172                                             int port)
173 {
174         unsigned int off;
175
176         switch (port) {
177         case 7:
178                 off = P7_IRQ_OFF;
179                 break;
180         case 0:
181                 /* Port 0 interrupts are located on the first bank */
182                 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
183                 return;
184         default:
185                 off = P_IRQ_OFF(port);
186                 break;
187         }
188
189         intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
190 }
191
192 static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
193                                              int port)
194 {
195         unsigned int off;
196
197         switch (port) {
198         case 7:
199                 off = P7_IRQ_OFF;
200                 break;
201         case 0:
202                 /* Port 0 interrupts are located on the first bank */
203                 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
204                 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
205                 return;
206         default:
207                 off = P_IRQ_OFF(port);
208                 break;
209         }
210
211         intrl2_1_mask_set(priv, P_IRQ_MASK(off));
212         intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
213 }
214
215 static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
216                               struct phy_device *phy)
217 {
218         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
219         s8 cpu_port = ds->dst[ds->index].cpu_port;
220         u32 reg;
221
222         /* Clear the memory power down */
223         reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
224         reg &= ~P_TXQ_PSM_VDD(port);
225         core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
226
227         /* Disable learning */
228         reg = core_readl(priv, CORE_DIS_LEARN);
229         reg |= BIT(port);
230         core_writel(priv, reg, CORE_DIS_LEARN);
231
232         /* Clear the Rx and Tx disable bits and set to no spanning tree */
233         core_writel(priv, 0, CORE_G_PCTL_PORT(port));
234
235         /* Re-enable the GPHY and re-apply workarounds */
236         if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
237                 bcm_sf2_gphy_enable_set(ds, true);
238                 if (phy) {
239                         /* if phy_stop() has been called before, phy
240                          * will be in halted state, and phy_start()
241                          * will call resume.
242                          *
243                          * the resume path does not configure back
244                          * autoneg settings, and since we hard reset
245                          * the phy manually here, we need to reset the
246                          * state machine also.
247                          */
248                         phy->state = PHY_READY;
249                         phy_init_hw(phy);
250                 }
251         }
252
253         /* Enable MoCA port interrupts to get notified */
254         if (port == priv->moca_port)
255                 bcm_sf2_port_intr_enable(priv, port);
256
257         /* Set this port, and only this one to be in the default VLAN,
258          * if member of a bridge, restore its membership prior to
259          * bringing down this port.
260          */
261         reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
262         reg &= ~PORT_VLAN_CTRL_MASK;
263         reg |= (1 << port);
264         reg |= priv->dev->ports[port].vlan_ctl_mask;
265         core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
266
267         bcm_sf2_imp_vlan_setup(ds, cpu_port);
268
269         /* If EEE was enabled, restore it */
270         if (priv->port_sts[port].eee.eee_enabled)
271                 bcm_sf2_eee_enable_set(ds, port, true);
272
273         return 0;
274 }
275
276 static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
277                                  struct phy_device *phy)
278 {
279         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
280         u32 off, reg;
281
282         if (priv->wol_ports_mask & (1 << port))
283                 return;
284
285         if (port == priv->moca_port)
286                 bcm_sf2_port_intr_disable(priv, port);
287
288         if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
289                 bcm_sf2_gphy_enable_set(ds, false);
290
291         if (dsa_is_cpu_port(ds, port))
292                 off = CORE_IMP_CTL;
293         else
294                 off = CORE_G_PCTL_PORT(port);
295
296         reg = core_readl(priv, off);
297         reg |= RX_DIS | TX_DIS;
298         core_writel(priv, reg, off);
299
300         /* Power down the port memory */
301         reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
302         reg |= P_TXQ_PSM_VDD(port);
303         core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
304 }
305
306 /* Returns 0 if EEE was not enabled, or 1 otherwise
307  */
308 static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
309                             struct phy_device *phy)
310 {
311         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
312         struct ethtool_eee *p = &priv->port_sts[port].eee;
313         int ret;
314
315         p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
316
317         ret = phy_init_eee(phy, 0);
318         if (ret)
319                 return 0;
320
321         bcm_sf2_eee_enable_set(ds, port, true);
322
323         return 1;
324 }
325
326 static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
327                               struct ethtool_eee *e)
328 {
329         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
330         struct ethtool_eee *p = &priv->port_sts[port].eee;
331         u32 reg;
332
333         reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
334         e->eee_enabled = p->eee_enabled;
335         e->eee_active = !!(reg & (1 << port));
336
337         return 0;
338 }
339
340 static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
341                               struct phy_device *phydev,
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
347         p->eee_enabled = e->eee_enabled;
348
349         if (!p->eee_enabled) {
350                 bcm_sf2_eee_enable_set(ds, port, false);
351         } else {
352                 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
353                 if (!p->eee_enabled)
354                         return -EOPNOTSUPP;
355         }
356
357         return 0;
358 }
359
360 static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
361                                int regnum, u16 val)
362 {
363         int ret = 0;
364         u32 reg;
365
366         reg = reg_readl(priv, REG_SWITCH_CNTRL);
367         reg |= MDIO_MASTER_SEL;
368         reg_writel(priv, reg, REG_SWITCH_CNTRL);
369
370         /* Page << 8 | offset */
371         reg = 0x70;
372         reg <<= 2;
373         core_writel(priv, addr, reg);
374
375         /* Page << 8 | offset */
376         reg = 0x80 << 8 | regnum << 1;
377         reg <<= 2;
378
379         if (op)
380                 ret = core_readl(priv, reg);
381         else
382                 core_writel(priv, val, reg);
383
384         reg = reg_readl(priv, REG_SWITCH_CNTRL);
385         reg &= ~MDIO_MASTER_SEL;
386         reg_writel(priv, reg, REG_SWITCH_CNTRL);
387
388         return ret & 0xffff;
389 }
390
391 static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
392 {
393         struct bcm_sf2_priv *priv = bus->priv;
394
395         /* Intercept reads from Broadcom pseudo-PHY address, else, send
396          * them to our master MDIO bus controller
397          */
398         if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
399                 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
400         else
401                 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
402 }
403
404 static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
405                                  u16 val)
406 {
407         struct bcm_sf2_priv *priv = bus->priv;
408
409         /* Intercept writes to the Broadcom pseudo-PHY address, else,
410          * send them to our master MDIO bus controller
411          */
412         if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
413                 return bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
414         else
415                 return mdiobus_write_nested(priv->master_mii_bus, addr,
416                                 regnum, val);
417 }
418
419 static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
420 {
421         struct bcm_sf2_priv *priv = dev_id;
422
423         priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
424                                 ~priv->irq0_mask;
425         intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
426
427         return IRQ_HANDLED;
428 }
429
430 static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
431 {
432         struct bcm_sf2_priv *priv = dev_id;
433
434         priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
435                                 ~priv->irq1_mask;
436         intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
437
438         if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
439                 priv->port_sts[7].link = 1;
440         if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
441                 priv->port_sts[7].link = 0;
442
443         return IRQ_HANDLED;
444 }
445
446 static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
447 {
448         unsigned int timeout = 1000;
449         u32 reg;
450
451         reg = core_readl(priv, CORE_WATCHDOG_CTRL);
452         reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
453         core_writel(priv, reg, CORE_WATCHDOG_CTRL);
454
455         do {
456                 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
457                 if (!(reg & SOFTWARE_RESET))
458                         break;
459
460                 usleep_range(1000, 2000);
461         } while (timeout-- > 0);
462
463         if (timeout == 0)
464                 return -ETIMEDOUT;
465
466         return 0;
467 }
468
469 static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
470 {
471         intrl2_0_mask_set(priv, 0xffffffff);
472         intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
473         intrl2_1_mask_set(priv, 0xffffffff);
474         intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
475 }
476
477 static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
478                                    struct device_node *dn)
479 {
480         struct device_node *port;
481         const char *phy_mode_str;
482         int mode;
483         unsigned int port_num;
484         int ret;
485
486         priv->moca_port = -1;
487
488         for_each_available_child_of_node(dn, port) {
489                 if (of_property_read_u32(port, "reg", &port_num))
490                         continue;
491
492                 /* Internal PHYs get assigned a specific 'phy-mode' property
493                  * value: "internal" to help flag them before MDIO probing
494                  * has completed, since they might be turned off at that
495                  * time
496                  */
497                 mode = of_get_phy_mode(port);
498                 if (mode < 0) {
499                         ret = of_property_read_string(port, "phy-mode",
500                                                       &phy_mode_str);
501                         if (ret < 0)
502                                 continue;
503
504                         if (!strcasecmp(phy_mode_str, "internal"))
505                                 priv->int_phy_mask |= 1 << port_num;
506                 }
507
508                 if (mode == PHY_INTERFACE_MODE_MOCA)
509                         priv->moca_port = port_num;
510         }
511 }
512
513 static int bcm_sf2_mdio_register(struct dsa_switch *ds)
514 {
515         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
516         struct device_node *dn;
517         static int index;
518         int err;
519
520         /* Find our integrated MDIO bus node */
521         dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
522         priv->master_mii_bus = of_mdio_find_bus(dn);
523         if (!priv->master_mii_bus) {
524                 of_node_put(dn);
525                 return -EPROBE_DEFER;
526         }
527
528         get_device(&priv->master_mii_bus->dev);
529         priv->master_mii_dn = dn;
530
531         priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
532         if (!priv->slave_mii_bus) {
533                 of_node_put(dn);
534                 return -ENOMEM;
535         }
536
537         priv->slave_mii_bus->priv = priv;
538         priv->slave_mii_bus->name = "sf2 slave mii";
539         priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
540         priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
541         snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
542                  index++);
543         priv->slave_mii_bus->dev.of_node = dn;
544
545         /* Include the pseudo-PHY address to divert reads towards our
546          * workaround. This is only required for 7445D0, since 7445E0
547          * disconnects the internal switch pseudo-PHY such that we can use the
548          * regular SWITCH_MDIO master controller instead.
549          *
550          * Here we flag the pseudo PHY as needing special treatment and would
551          * otherwise make all other PHY read/writes go to the master MDIO bus
552          * controller that comes with this switch backed by the "mdio-unimac"
553          * driver.
554          */
555         if (of_machine_is_compatible("brcm,bcm7445d0"))
556                 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
557         else
558                 priv->indir_phy_mask = 0;
559
560         ds->phys_mii_mask = priv->indir_phy_mask;
561         ds->slave_mii_bus = priv->slave_mii_bus;
562         priv->slave_mii_bus->parent = ds->dev->parent;
563         priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
564
565         if (dn)
566                 err = of_mdiobus_register(priv->slave_mii_bus, dn);
567         else
568                 err = mdiobus_register(priv->slave_mii_bus);
569
570         if (err)
571                 of_node_put(dn);
572
573         return err;
574 }
575
576 static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
577 {
578         mdiobus_unregister(priv->slave_mii_bus);
579         if (priv->master_mii_dn)
580                 of_node_put(priv->master_mii_dn);
581 }
582
583 static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
584 {
585         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
586
587         /* The BCM7xxx PHY driver expects to find the integrated PHY revision
588          * in bits 15:8 and the patch level in bits 7:0 which is exactly what
589          * the REG_PHY_REVISION register layout is.
590          */
591         if (priv->int_phy_mask & BIT(port))
592                 return priv->hw_params.gphy_rev;
593         else
594                 return 0;
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         u16 lcl_adv = 0, rmt_adv = 0;
604         const char *str = NULL;
605         u8 flowctrl = 0;
606         u32 reg;
607
608         switch (phydev->interface) {
609         case PHY_INTERFACE_MODE_RGMII:
610                 str = "RGMII (no delay)";
611                 id_mode_dis = 1;
612         case PHY_INTERFACE_MODE_RGMII_TXID:
613                 if (!str)
614                         str = "RGMII (TX delay)";
615                 port_mode = EXT_GPHY;
616                 break;
617         case PHY_INTERFACE_MODE_MII:
618                 str = "MII";
619                 port_mode = EXT_EPHY;
620                 break;
621         case PHY_INTERFACE_MODE_REVMII:
622                 str = "Reverse MII";
623                 port_mode = EXT_REVMII;
624                 break;
625         default:
626                 /* All other PHYs: internal and MoCA */
627                 goto force_link;
628         }
629
630         /* If the link is down, just disable the interface to conserve power */
631         if (!phydev->link) {
632                 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
633                 reg &= ~RGMII_MODE_EN;
634                 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
635                 goto force_link;
636         }
637
638         /* Clear id_mode_dis bit, and the existing port mode, but
639          * make sure we enable the RGMII block for data to pass
640          */
641         reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
642         reg &= ~ID_MODE_DIS;
643         reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
644         reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
645
646         reg |= port_mode | RGMII_MODE_EN;
647         if (id_mode_dis)
648                 reg |= ID_MODE_DIS;
649
650         if (phydev->pause) {
651                 if (phydev->asym_pause)
652                         reg |= TX_PAUSE_EN;
653                 reg |= RX_PAUSE_EN;
654         }
655
656         reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
657
658         pr_info("Port %d configured for %s\n", port, str);
659
660 force_link:
661         /* Force link settings detected from the PHY */
662         reg = SW_OVERRIDE;
663         switch (phydev->speed) {
664         case SPEED_1000:
665                 reg |= SPDSTS_1000 << SPEED_SHIFT;
666                 break;
667         case SPEED_100:
668                 reg |= SPDSTS_100 << SPEED_SHIFT;
669                 break;
670         }
671
672         if (phydev->duplex == DUPLEX_FULL &&
673             phydev->autoneg == AUTONEG_ENABLE) {
674                 if (phydev->pause)
675                         rmt_adv = LPA_PAUSE_CAP;
676                 if (phydev->asym_pause)
677                         rmt_adv |= LPA_PAUSE_ASYM;
678                 if (phydev->advertising & ADVERTISED_Pause)
679                         lcl_adv = ADVERTISE_PAUSE_CAP;
680                 if (phydev->advertising & ADVERTISED_Asym_Pause)
681                         lcl_adv |= ADVERTISE_PAUSE_ASYM;
682                 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
683         }
684
685         if (phydev->link)
686                 reg |= LINK_STS;
687         if (phydev->duplex == DUPLEX_FULL)
688                 reg |= DUPLX_MODE;
689         if (flowctrl & FLOW_CTRL_TX)
690                 reg |= TXFLOW_CNTL;
691         if (flowctrl & FLOW_CTRL_RX)
692                 reg |= RXFLOW_CNTL;
693
694         core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
695
696         if (!phydev->is_pseudo_fixed_link)
697                 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
698 }
699
700 static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
701                                          struct fixed_phy_status *status)
702 {
703         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
704         u32 duplex, pause;
705         u32 reg;
706
707         duplex = core_readl(priv, CORE_DUPSTS);
708         pause = core_readl(priv, CORE_PAUSESTS);
709
710         status->link = 0;
711
712         /* MoCA port is special as we do not get link status from CORE_LNKSTS,
713          * which means that we need to force the link at the port override
714          * level to get the data to flow. We do use what the interrupt handler
715          * did determine before.
716          *
717          * For the other ports, we just force the link status, since this is
718          * a fixed PHY device.
719          */
720         if (port == priv->moca_port) {
721                 status->link = priv->port_sts[port].link;
722                 /* For MoCA interfaces, also force a link down notification
723                  * since some version of the user-space daemon (mocad) use
724                  * cmd->autoneg to force the link, which messes up the PHY
725                  * state machine and make it go in PHY_FORCING state instead.
726                  */
727                 if (!status->link)
728                         netif_carrier_off(ds->ports[port].netdev);
729                 status->duplex = 1;
730         } else {
731                 status->link = 1;
732                 status->duplex = !!(duplex & (1 << port));
733         }
734
735         reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
736         reg |= SW_OVERRIDE;
737         if (status->link)
738                 reg |= LINK_STS;
739         else
740                 reg &= ~LINK_STS;
741         core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
742
743         if ((pause & (1 << port)) &&
744             (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
745                 status->asym_pause = 1;
746                 status->pause = 1;
747         }
748
749         if (pause & (1 << port))
750                 status->pause = 1;
751 }
752
753 static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
754 {
755         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
756         unsigned int port;
757
758         bcm_sf2_intr_disable(priv);
759
760         /* Disable all ports physically present including the IMP
761          * port, the other ones have already been disabled during
762          * bcm_sf2_sw_setup
763          */
764         for (port = 0; port < DSA_MAX_PORTS; port++) {
765                 if ((1 << port) & ds->enabled_port_mask ||
766                     dsa_is_cpu_port(ds, port))
767                         bcm_sf2_port_disable(ds, port, NULL);
768         }
769
770         return 0;
771 }
772
773 static int bcm_sf2_sw_resume(struct dsa_switch *ds)
774 {
775         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
776         int ret;
777
778         ret = bcm_sf2_sw_rst(priv);
779         if (ret) {
780                 pr_err("%s: failed to software reset switch\n", __func__);
781                 return ret;
782         }
783
784         if (priv->hw_params.num_gphy == 1)
785                 bcm_sf2_gphy_enable_set(ds, true);
786
787         ds->ops->setup(ds);
788
789         return 0;
790 }
791
792 static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
793                                struct ethtool_wolinfo *wol)
794 {
795         struct net_device *p = ds->dst[ds->index].master_netdev;
796         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
797         struct ethtool_wolinfo pwol;
798
799         /* Get the parent device WoL settings */
800         p->ethtool_ops->get_wol(p, &pwol);
801
802         /* Advertise the parent device supported settings */
803         wol->supported = pwol.supported;
804         memset(&wol->sopass, 0, sizeof(wol->sopass));
805
806         if (pwol.wolopts & WAKE_MAGICSECURE)
807                 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
808
809         if (priv->wol_ports_mask & (1 << port))
810                 wol->wolopts = pwol.wolopts;
811         else
812                 wol->wolopts = 0;
813 }
814
815 static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
816                               struct ethtool_wolinfo *wol)
817 {
818         struct net_device *p = ds->dst[ds->index].master_netdev;
819         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
820         s8 cpu_port = ds->dst[ds->index].cpu_port;
821         struct ethtool_wolinfo pwol;
822
823         p->ethtool_ops->get_wol(p, &pwol);
824         if (wol->wolopts & ~pwol.supported)
825                 return -EINVAL;
826
827         if (wol->wolopts)
828                 priv->wol_ports_mask |= (1 << port);
829         else
830                 priv->wol_ports_mask &= ~(1 << port);
831
832         /* If we have at least one port enabled, make sure the CPU port
833          * is also enabled. If the CPU port is the last one enabled, we disable
834          * it since this configuration does not make sense.
835          */
836         if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
837                 priv->wol_ports_mask |= (1 << cpu_port);
838         else
839                 priv->wol_ports_mask &= ~(1 << cpu_port);
840
841         return p->ethtool_ops->set_wol(p, wol);
842 }
843
844 static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
845 {
846         unsigned int timeout = 10;
847         u32 reg;
848
849         do {
850                 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
851                 if (!(reg & ARLA_VTBL_STDN))
852                         return 0;
853
854                 usleep_range(1000, 2000);
855         } while (timeout--);
856
857         return -ETIMEDOUT;
858 }
859
860 static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
861 {
862         core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
863
864         return bcm_sf2_vlan_op_wait(priv);
865 }
866
867 static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
868 {
869         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
870         unsigned int port;
871
872         /* Clear all VLANs */
873         bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
874
875         for (port = 0; port < priv->hw_params.num_ports; port++) {
876                 if (!((1 << port) & ds->enabled_port_mask))
877                         continue;
878
879                 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
880         }
881 }
882
883 static int bcm_sf2_sw_setup(struct dsa_switch *ds)
884 {
885         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
886         unsigned int port;
887
888         /* Enable all valid ports and disable those unused */
889         for (port = 0; port < priv->hw_params.num_ports; port++) {
890                 /* IMP port receives special treatment */
891                 if ((1 << port) & ds->enabled_port_mask)
892                         bcm_sf2_port_setup(ds, port, NULL);
893                 else if (dsa_is_cpu_port(ds, port))
894                         bcm_sf2_imp_setup(ds, port);
895                 else
896                         bcm_sf2_port_disable(ds, port, NULL);
897         }
898
899         bcm_sf2_sw_configure_vlan(ds);
900
901         return 0;
902 }
903
904 /* The SWITCH_CORE register space is managed by b53 but operates on a page +
905  * register basis so we need to translate that into an address that the
906  * bus-glue understands.
907  */
908 #define SF2_PAGE_REG_MKADDR(page, reg)  ((page) << 10 | (reg) << 2)
909
910 static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
911                               u8 *val)
912 {
913         struct bcm_sf2_priv *priv = dev->priv;
914
915         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
916
917         return 0;
918 }
919
920 static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
921                                u16 *val)
922 {
923         struct bcm_sf2_priv *priv = dev->priv;
924
925         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
926
927         return 0;
928 }
929
930 static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
931                                u32 *val)
932 {
933         struct bcm_sf2_priv *priv = dev->priv;
934
935         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
936
937         return 0;
938 }
939
940 static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
941                                u64 *val)
942 {
943         struct bcm_sf2_priv *priv = dev->priv;
944
945         *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
946
947         return 0;
948 }
949
950 static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
951                                u8 value)
952 {
953         struct bcm_sf2_priv *priv = dev->priv;
954
955         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
956
957         return 0;
958 }
959
960 static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
961                                 u16 value)
962 {
963         struct bcm_sf2_priv *priv = dev->priv;
964
965         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
966
967         return 0;
968 }
969
970 static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
971                                 u32 value)
972 {
973         struct bcm_sf2_priv *priv = dev->priv;
974
975         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
976
977         return 0;
978 }
979
980 static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
981                                 u64 value)
982 {
983         struct bcm_sf2_priv *priv = dev->priv;
984
985         core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
986
987         return 0;
988 }
989
990 static struct b53_io_ops bcm_sf2_io_ops = {
991         .read8  = bcm_sf2_core_read8,
992         .read16 = bcm_sf2_core_read16,
993         .read32 = bcm_sf2_core_read32,
994         .read48 = bcm_sf2_core_read64,
995         .read64 = bcm_sf2_core_read64,
996         .write8 = bcm_sf2_core_write8,
997         .write16 = bcm_sf2_core_write16,
998         .write32 = bcm_sf2_core_write32,
999         .write48 = bcm_sf2_core_write64,
1000         .write64 = bcm_sf2_core_write64,
1001 };
1002
1003 static int bcm_sf2_sw_probe(struct platform_device *pdev)
1004 {
1005         const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1006         struct device_node *dn = pdev->dev.of_node;
1007         struct b53_platform_data *pdata;
1008         struct dsa_switch_ops *ops;
1009         struct device_node *ports;
1010         struct bcm_sf2_priv *priv;
1011         struct b53_device *dev;
1012         struct dsa_switch *ds;
1013         void __iomem **base;
1014         struct resource *r;
1015         unsigned int i;
1016         u32 reg, rev;
1017         int ret;
1018
1019         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1020         if (!priv)
1021                 return -ENOMEM;
1022
1023         ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1024         if (!ops)
1025                 return -ENOMEM;
1026
1027         dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1028         if (!dev)
1029                 return -ENOMEM;
1030
1031         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1032         if (!pdata)
1033                 return -ENOMEM;
1034
1035         /* Auto-detection using standard registers will not work, so
1036          * provide an indication of what kind of device we are for
1037          * b53_common to work with
1038          */
1039         pdata->chip_id = BCM7445_DEVICE_ID;
1040         dev->pdata = pdata;
1041
1042         priv->dev = dev;
1043         ds = dev->ds;
1044
1045         /* Override the parts that are non-standard wrt. normal b53 devices */
1046         memcpy(ops, ds->ops, sizeof(*ops));
1047         ds->ops = ops;
1048         ds->ops->get_tag_protocol = bcm_sf2_sw_get_tag_protocol;
1049         ds->ops->setup = bcm_sf2_sw_setup;
1050         ds->ops->get_phy_flags = bcm_sf2_sw_get_phy_flags;
1051         ds->ops->adjust_link = bcm_sf2_sw_adjust_link;
1052         ds->ops->fixed_link_update = bcm_sf2_sw_fixed_link_update;
1053         ds->ops->suspend = bcm_sf2_sw_suspend;
1054         ds->ops->resume = bcm_sf2_sw_resume;
1055         ds->ops->get_wol = bcm_sf2_sw_get_wol;
1056         ds->ops->set_wol = bcm_sf2_sw_set_wol;
1057         ds->ops->port_enable = bcm_sf2_port_setup;
1058         ds->ops->port_disable = bcm_sf2_port_disable;
1059         ds->ops->get_eee = bcm_sf2_sw_get_eee;
1060         ds->ops->set_eee = bcm_sf2_sw_set_eee;
1061
1062         /* Avoid having DSA free our slave MDIO bus (checking for
1063          * ds->slave_mii_bus and ds->ops->phy_read being non-NULL)
1064          */
1065         ds->ops->phy_read = NULL;
1066
1067         dev_set_drvdata(&pdev->dev, priv);
1068
1069         spin_lock_init(&priv->indir_lock);
1070         mutex_init(&priv->stats_mutex);
1071
1072         /* Balance of_node_put() done by of_find_node_by_name() */
1073         of_node_get(dn);
1074         ports = of_find_node_by_name(dn, "ports");
1075         if (ports) {
1076                 bcm_sf2_identify_ports(priv, ports);
1077                 of_node_put(ports);
1078         }
1079
1080         priv->irq0 = irq_of_parse_and_map(dn, 0);
1081         priv->irq1 = irq_of_parse_and_map(dn, 1);
1082
1083         base = &priv->core;
1084         for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
1085                 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1086                 *base = devm_ioremap_resource(&pdev->dev, r);
1087                 if (IS_ERR(*base)) {
1088                         pr_err("unable to find register: %s\n", reg_names[i]);
1089                         return PTR_ERR(*base);
1090                 }
1091                 base++;
1092         }
1093
1094         ret = bcm_sf2_sw_rst(priv);
1095         if (ret) {
1096                 pr_err("unable to software reset switch: %d\n", ret);
1097                 return ret;
1098         }
1099
1100         ret = bcm_sf2_mdio_register(ds);
1101         if (ret) {
1102                 pr_err("failed to register MDIO bus\n");
1103                 return ret;
1104         }
1105
1106         /* Disable all interrupts and request them */
1107         bcm_sf2_intr_disable(priv);
1108
1109         ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1110                                "switch_0", priv);
1111         if (ret < 0) {
1112                 pr_err("failed to request switch_0 IRQ\n");
1113                 goto out_mdio;
1114         }
1115
1116         ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1117                                "switch_1", priv);
1118         if (ret < 0) {
1119                 pr_err("failed to request switch_1 IRQ\n");
1120                 goto out_mdio;
1121         }
1122
1123         /* Reset the MIB counters */
1124         reg = core_readl(priv, CORE_GMNCFGCFG);
1125         reg |= RST_MIB_CNT;
1126         core_writel(priv, reg, CORE_GMNCFGCFG);
1127         reg &= ~RST_MIB_CNT;
1128         core_writel(priv, reg, CORE_GMNCFGCFG);
1129
1130         /* Get the maximum number of ports for this switch */
1131         priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1132         if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1133                 priv->hw_params.num_ports = DSA_MAX_PORTS;
1134
1135         /* Assume a single GPHY setup if we can't read that property */
1136         if (of_property_read_u32(dn, "brcm,num-gphy",
1137                                  &priv->hw_params.num_gphy))
1138                 priv->hw_params.num_gphy = 1;
1139
1140         rev = reg_readl(priv, REG_SWITCH_REVISION);
1141         priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1142                                         SWITCH_TOP_REV_MASK;
1143         priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1144
1145         rev = reg_readl(priv, REG_PHY_REVISION);
1146         priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1147
1148         ret = b53_switch_register(dev);
1149         if (ret)
1150                 goto out_mdio;
1151
1152         pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1153                 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1154                 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1155                 priv->core, priv->irq0, priv->irq1);
1156
1157         return 0;
1158
1159 out_mdio:
1160         bcm_sf2_mdio_unregister(priv);
1161         return ret;
1162 }
1163
1164 static int bcm_sf2_sw_remove(struct platform_device *pdev)
1165 {
1166         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1167
1168         priv->wol_ports_mask = 0;
1169         dsa_unregister_switch(priv->dev->ds);
1170         /* Disable all ports and interrupts */
1171         bcm_sf2_sw_suspend(priv->dev->ds);
1172         bcm_sf2_mdio_unregister(priv);
1173
1174         return 0;
1175 }
1176
1177 static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1178 {
1179         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1180
1181         /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1182          * successful MDIO bus scan to occur. If we did turn off the GPHY
1183          * before (e.g: port_disable), this will also power it back on.
1184          *
1185          * Do not rely on kexec_in_progress, just power the PHY on.
1186          */
1187         if (priv->hw_params.num_gphy == 1)
1188                 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1189 }
1190
1191 #ifdef CONFIG_PM_SLEEP
1192 static int bcm_sf2_suspend(struct device *dev)
1193 {
1194         struct platform_device *pdev = to_platform_device(dev);
1195         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1196
1197         return dsa_switch_suspend(priv->dev->ds);
1198 }
1199
1200 static int bcm_sf2_resume(struct device *dev)
1201 {
1202         struct platform_device *pdev = to_platform_device(dev);
1203         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1204
1205         return dsa_switch_resume(priv->dev->ds);
1206 }
1207 #endif /* CONFIG_PM_SLEEP */
1208
1209 static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1210                          bcm_sf2_suspend, bcm_sf2_resume);
1211
1212 static const struct of_device_id bcm_sf2_of_match[] = {
1213         { .compatible = "brcm,bcm7445-switch-v4.0" },
1214         { /* sentinel */ },
1215 };
1216 MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1217
1218 static struct platform_driver bcm_sf2_driver = {
1219         .probe  = bcm_sf2_sw_probe,
1220         .remove = bcm_sf2_sw_remove,
1221         .shutdown = bcm_sf2_sw_shutdown,
1222         .driver = {
1223                 .name = "brcm-sf2",
1224                 .of_match_table = bcm_sf2_of_match,
1225                 .pm = &bcm_sf2_pm_ops,
1226         },
1227 };
1228 module_platform_driver(bcm_sf2_driver);
1229
1230 MODULE_AUTHOR("Broadcom Corporation");
1231 MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1232 MODULE_LICENSE("GPL");
1233 MODULE_ALIAS("platform:brcm-sf2");