Merge remote-tracking branch 'stable/linux-5.15.y' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / net / ethernet / broadcom / genet / bcmmii.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Broadcom GENET MDIO routines
4  *
5  * Copyright (c) 2014-2017 Broadcom
6  */
7
8 #include <linux/acpi.h>
9 #include <linux/types.h>
10 #include <linux/delay.h>
11 #include <linux/wait.h>
12 #include <linux/mii.h>
13 #include <linux/ethtool.h>
14 #include <linux/bitops.h>
15 #include <linux/netdevice.h>
16 #include <linux/platform_device.h>
17 #include <linux/phy.h>
18 #include <linux/phy_fixed.h>
19 #include <linux/brcmphy.h>
20 #include <linux/of.h>
21 #include <linux/of_net.h>
22 #include <linux/of_mdio.h>
23 #include <linux/platform_data/bcmgenet.h>
24 #include <linux/platform_data/mdio-bcm-unimac.h>
25
26 #include "bcmgenet.h"
27
28 /* setup netdev link state when PHY link status change and
29  * update UMAC and RGMII block when link up
30  */
31 void bcmgenet_mii_setup(struct net_device *dev)
32 {
33         struct bcmgenet_priv *priv = netdev_priv(dev);
34         struct phy_device *phydev = dev->phydev;
35         u32 reg, cmd_bits = 0;
36         bool status_changed = false;
37
38         if (priv->old_link != phydev->link) {
39                 status_changed = true;
40                 priv->old_link = phydev->link;
41         }
42
43         if (phydev->link) {
44                 /* check speed/duplex/pause changes */
45                 if (priv->old_speed != phydev->speed) {
46                         status_changed = true;
47                         priv->old_speed = phydev->speed;
48                 }
49
50                 if (priv->old_duplex != phydev->duplex) {
51                         status_changed = true;
52                         priv->old_duplex = phydev->duplex;
53                 }
54
55                 if (priv->old_pause != phydev->pause) {
56                         status_changed = true;
57                         priv->old_pause = phydev->pause;
58                 }
59
60                 /* done if nothing has changed */
61                 if (!status_changed)
62                         return;
63
64                 /* speed */
65                 if (phydev->speed == SPEED_1000)
66                         cmd_bits = CMD_SPEED_1000;
67                 else if (phydev->speed == SPEED_100)
68                         cmd_bits = CMD_SPEED_100;
69                 else
70                         cmd_bits = CMD_SPEED_10;
71                 cmd_bits <<= CMD_SPEED_SHIFT;
72
73                 /* duplex */
74                 if (phydev->duplex != DUPLEX_FULL)
75                         cmd_bits |= CMD_HD_EN;
76
77                 /* pause capability */
78                 if (!phydev->pause)
79                         cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
80
81                 /*
82                  * Program UMAC and RGMII block based on established
83                  * link speed, duplex, and pause. The speed set in
84                  * umac->cmd tell RGMII block which clock to use for
85                  * transmit -- 25MHz(100Mbps) or 125MHz(1Gbps).
86                  * Receive clock is provided by the PHY.
87                  */
88                 reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
89                 reg &= ~OOB_DISABLE;
90                 reg |= RGMII_LINK;
91                 bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
92
93                 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
94                 reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
95                                CMD_HD_EN |
96                                CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE);
97                 reg |= cmd_bits;
98                 if (reg & CMD_SW_RESET) {
99                         reg &= ~CMD_SW_RESET;
100                         bcmgenet_umac_writel(priv, reg, UMAC_CMD);
101                         udelay(2);
102                         reg |= CMD_TX_EN | CMD_RX_EN;
103                 }
104                 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
105         } else {
106                 /* done if nothing has changed */
107                 if (!status_changed)
108                         return;
109
110                 /* needed for MoCA fixed PHY to reflect correct link status */
111                 netif_carrier_off(dev);
112         }
113
114         phy_print_status(phydev);
115 }
116
117
118 static int bcmgenet_fixed_phy_link_update(struct net_device *dev,
119                                           struct fixed_phy_status *status)
120 {
121         struct bcmgenet_priv *priv;
122         u32 reg;
123
124         if (dev && dev->phydev && status) {
125                 priv = netdev_priv(dev);
126                 reg = bcmgenet_umac_readl(priv, UMAC_MODE);
127                 status->link = !!(reg & MODE_LINK_STATUS);
128         }
129
130         return 0;
131 }
132
133 void bcmgenet_phy_power_set(struct net_device *dev, bool enable)
134 {
135         struct bcmgenet_priv *priv = netdev_priv(dev);
136         u32 reg = 0;
137
138         /* EXT_GPHY_CTRL is only valid for GENETv4 and onward */
139         if (GENET_IS_V4(priv)) {
140                 reg = bcmgenet_ext_readl(priv, EXT_GPHY_CTRL);
141                 if (enable) {
142                         reg &= ~EXT_CK25_DIS;
143                         bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
144                         mdelay(1);
145
146                         reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN);
147                         reg |= EXT_GPHY_RESET;
148                         bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
149                         mdelay(1);
150
151                         reg &= ~EXT_GPHY_RESET;
152                 } else {
153                         reg |= EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN |
154                                EXT_GPHY_RESET;
155                         bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
156                         mdelay(1);
157                         reg |= EXT_CK25_DIS;
158                 }
159                 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
160                 udelay(60);
161         } else {
162                 mdelay(1);
163         }
164 }
165
166 static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
167 {
168         u32 reg;
169
170         if (!GENET_IS_V5(priv)) {
171                 /* Speed settings are set in bcmgenet_mii_setup() */
172                 reg = bcmgenet_sys_readl(priv, SYS_PORT_CTRL);
173                 reg |= LED_ACT_SOURCE_MAC;
174                 bcmgenet_sys_writel(priv, reg, SYS_PORT_CTRL);
175         }
176
177         if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
178                 fixed_phy_set_link_update(priv->dev->phydev,
179                                           bcmgenet_fixed_phy_link_update);
180 }
181
182 int bcmgenet_mii_config(struct net_device *dev, bool init)
183 {
184         struct bcmgenet_priv *priv = netdev_priv(dev);
185         struct phy_device *phydev = dev->phydev;
186         struct device *kdev = &priv->pdev->dev;
187         const char *phy_name = NULL;
188         u32 id_mode_dis = 0;
189         u32 port_ctrl;
190         u32 reg;
191
192         switch (priv->phy_interface) {
193         case PHY_INTERFACE_MODE_INTERNAL:
194                 phy_name = "internal PHY";
195                 fallthrough;
196         case PHY_INTERFACE_MODE_MOCA:
197                 /* Irrespective of the actually configured PHY speed (100 or
198                  * 1000) GENETv4 only has an internal GPHY so we will just end
199                  * up masking the Gigabit features from what we support, not
200                  * switching to the EPHY
201                  */
202                 if (GENET_IS_V4(priv))
203                         port_ctrl = PORT_MODE_INT_GPHY;
204                 else
205                         port_ctrl = PORT_MODE_INT_EPHY;
206
207                 if (!phy_name) {
208                         phy_name = "MoCA";
209                         bcmgenet_moca_phy_setup(priv);
210                 }
211                 break;
212
213         case PHY_INTERFACE_MODE_MII:
214                 phy_name = "external MII";
215                 phy_set_max_speed(phydev, SPEED_100);
216                 port_ctrl = PORT_MODE_EXT_EPHY;
217                 break;
218
219         case PHY_INTERFACE_MODE_REVMII:
220                 phy_name = "external RvMII";
221                 /* of_mdiobus_register took care of reading the 'max-speed'
222                  * PHY property for us, effectively limiting the PHY supported
223                  * capabilities, use that knowledge to also configure the
224                  * Reverse MII interface correctly.
225                  */
226                 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
227                                       dev->phydev->supported))
228                         port_ctrl = PORT_MODE_EXT_RVMII_50;
229                 else
230                         port_ctrl = PORT_MODE_EXT_RVMII_25;
231                 break;
232
233         case PHY_INTERFACE_MODE_RGMII:
234                 /* RGMII_NO_ID: TXC transitions at the same time as TXD
235                  *              (requires PCB or receiver-side delay)
236                  *
237                  * ID is implicitly disabled for 100Mbps (RG)MII operation.
238                  */
239                 phy_name = "external RGMII (no delay)";
240                 id_mode_dis = BIT(16);
241                 port_ctrl = PORT_MODE_EXT_GPHY;
242                 break;
243
244         case PHY_INTERFACE_MODE_RGMII_TXID:
245                 /* RGMII_TXID:  Add 2ns delay on TXC (90 degree shift) */
246                 phy_name = "external RGMII (TX delay)";
247                 port_ctrl = PORT_MODE_EXT_GPHY;
248                 break;
249
250         case PHY_INTERFACE_MODE_RGMII_RXID:
251                 phy_name = "external RGMII (RX delay)";
252                 port_ctrl = PORT_MODE_EXT_GPHY;
253                 break;
254         default:
255                 dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
256                 return -EINVAL;
257         }
258
259         bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
260
261         priv->ext_phy = !priv->internal_phy &&
262                         (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
263
264         /* This is an external PHY (xMII), so we need to enable the RGMII
265          * block for the interface to work
266          */
267         if (priv->ext_phy) {
268                 reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
269                 reg &= ~ID_MODE_DIS;
270                 reg |= id_mode_dis;
271                 if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
272                         reg |= RGMII_MODE_EN_V123;
273                 else
274                         reg |= RGMII_MODE_EN;
275                 bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
276         }
277
278         if (init)
279                 dev_info(kdev, "configuring instance for %s\n", phy_name);
280
281         return 0;
282 }
283
284 int bcmgenet_mii_probe(struct net_device *dev)
285 {
286         struct bcmgenet_priv *priv = netdev_priv(dev);
287         struct device *kdev = &priv->pdev->dev;
288         struct device_node *dn = kdev->of_node;
289         struct phy_device *phydev;
290         u32 phy_flags = 0;
291         int ret;
292
293         /* Communicate the integrated PHY revision */
294         if (priv->internal_phy)
295                 phy_flags = priv->gphy_rev;
296         else
297                 phy_flags = PHY_BRCM_AUTO_PWRDWN_ENABLE;
298
299         /* Initialize link state variables that bcmgenet_mii_setup() uses */
300         priv->old_link = -1;
301         priv->old_speed = -1;
302         priv->old_duplex = -1;
303         priv->old_pause = -1;
304
305         if (dn) {
306                 phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
307                                         phy_flags, priv->phy_interface);
308                 if (!phydev) {
309                         pr_err("could not attach to PHY\n");
310                         return -ENODEV;
311                 }
312         } else {
313                 if (has_acpi_companion(kdev)) {
314                         char mdio_bus_id[MII_BUS_ID_SIZE];
315                         struct mii_bus *unimacbus;
316
317                         snprintf(mdio_bus_id, MII_BUS_ID_SIZE, "%s-%d",
318                                  UNIMAC_MDIO_DRV_NAME, priv->pdev->id);
319
320                         unimacbus = mdio_find_bus(mdio_bus_id);
321                         if (!unimacbus) {
322                                 pr_err("Unable to find mii\n");
323                                 return -ENODEV;
324                         }
325                         phydev = phy_find_first(unimacbus);
326                         put_device(&unimacbus->dev);
327                         if (!phydev) {
328                                 pr_err("Unable to find PHY\n");
329                                 return -ENODEV;
330                         }
331                 } else {
332                         phydev = dev->phydev;
333                 }
334                 phydev->dev_flags = phy_flags;
335
336                 ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
337                                          priv->phy_interface);
338                 if (ret) {
339                         pr_err("could not attach to PHY\n");
340                         return -ENODEV;
341                 }
342         }
343
344         /* Configure port multiplexer based on what the probed PHY device since
345          * reading the 'max-speed' property determines the maximum supported
346          * PHY speed which is needed for bcmgenet_mii_config() to configure
347          * things appropriately.
348          */
349         ret = bcmgenet_mii_config(dev, true);
350         if (ret) {
351                 phy_disconnect(dev->phydev);
352                 return ret;
353         }
354
355         linkmode_copy(phydev->advertising, phydev->supported);
356
357         /* The internal PHY has its link interrupts routed to the
358          * Ethernet MAC ISRs. On GENETv5 there is a hardware issue
359          * that prevents the signaling of link UP interrupts when
360          * the link operates at 10Mbps, so fallback to polling for
361          * those versions of GENET.
362          */
363         if (priv->internal_phy && !GENET_IS_V5(priv))
364                 dev->phydev->irq = PHY_MAC_INTERRUPT;
365
366         return 0;
367 }
368
369 static struct device_node *bcmgenet_mii_of_find_mdio(struct bcmgenet_priv *priv)
370 {
371         struct device_node *dn = priv->pdev->dev.of_node;
372         struct device *kdev = &priv->pdev->dev;
373         char *compat;
374
375         compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
376         if (!compat)
377                 return NULL;
378
379         priv->mdio_dn = of_get_compatible_child(dn, compat);
380         kfree(compat);
381         if (!priv->mdio_dn) {
382                 dev_err(kdev, "unable to find MDIO bus node\n");
383                 return NULL;
384         }
385
386         return priv->mdio_dn;
387 }
388
389 static void bcmgenet_mii_pdata_init(struct bcmgenet_priv *priv,
390                                     struct unimac_mdio_pdata *ppd)
391 {
392         struct device *kdev = &priv->pdev->dev;
393         struct bcmgenet_platform_data *pd = kdev->platform_data;
394
395         if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
396                 /*
397                  * Internal or external PHY with MDIO access
398                  */
399                 if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
400                         ppd->phy_mask = 1 << pd->phy_address;
401                 else
402                         ppd->phy_mask = 0;
403         }
404 }
405
406 static int bcmgenet_mii_wait(void *wait_func_data)
407 {
408         struct bcmgenet_priv *priv = wait_func_data;
409
410         wait_event_timeout(priv->wq,
411                            !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
412                            & MDIO_START_BUSY),
413                            HZ / 100);
414         return 0;
415 }
416
417 static int bcmgenet_mii_register(struct bcmgenet_priv *priv)
418 {
419         struct platform_device *pdev = priv->pdev;
420         struct bcmgenet_platform_data *pdata = pdev->dev.platform_data;
421         struct device_node *dn = pdev->dev.of_node;
422         struct unimac_mdio_pdata ppd;
423         struct platform_device *ppdev;
424         struct resource *pres, res;
425         int id, ret;
426
427         pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
428         if (!pres) {
429                 dev_err(&pdev->dev, "Invalid resource\n");
430                 return -EINVAL;
431         }
432         memset(&res, 0, sizeof(res));
433         memset(&ppd, 0, sizeof(ppd));
434
435         ppd.wait_func = bcmgenet_mii_wait;
436         ppd.wait_func_data = priv;
437         ppd.bus_name = "bcmgenet MII bus";
438
439         /* Unimac MDIO bus controller starts at UniMAC offset + MDIO_CMD
440          * and is 2 * 32-bits word long, 8 bytes total.
441          */
442         res.start = pres->start + GENET_UMAC_OFF + UMAC_MDIO_CMD;
443         res.end = res.start + 8;
444         res.flags = IORESOURCE_MEM;
445
446         if (dn)
447                 id = of_alias_get_id(dn, "eth");
448         else
449                 id = pdev->id;
450
451         ppdev = platform_device_alloc(UNIMAC_MDIO_DRV_NAME, id);
452         if (!ppdev)
453                 return -ENOMEM;
454
455         /* Retain this platform_device pointer for later cleanup */
456         priv->mii_pdev = ppdev;
457         ppdev->dev.parent = &pdev->dev;
458         if (dn)
459                 ppdev->dev.of_node = bcmgenet_mii_of_find_mdio(priv);
460         else if (pdata)
461                 bcmgenet_mii_pdata_init(priv, &ppd);
462         else
463                 ppd.phy_mask = ~0;
464
465         ret = platform_device_add_resources(ppdev, &res, 1);
466         if (ret)
467                 goto out;
468
469         ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
470         if (ret)
471                 goto out;
472
473         ret = platform_device_add(ppdev);
474         if (ret)
475                 goto out;
476
477         return 0;
478 out:
479         platform_device_put(ppdev);
480         return ret;
481 }
482
483 static int bcmgenet_phy_interface_init(struct bcmgenet_priv *priv)
484 {
485         struct device *kdev = &priv->pdev->dev;
486         int phy_mode = device_get_phy_mode(kdev);
487
488         if (phy_mode < 0) {
489                 dev_err(kdev, "invalid PHY mode property\n");
490                 return phy_mode;
491         }
492
493         priv->phy_interface = phy_mode;
494
495         /* We need to specifically look up whether this PHY interface is
496          * internal or not *before* we even try to probe the PHY driver
497          * over MDIO as we may have shut down the internal PHY for power
498          * saving purposes.
499          */
500         if (priv->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
501                 priv->internal_phy = true;
502
503         return 0;
504 }
505
506 static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
507 {
508         struct device_node *dn = priv->pdev->dev.of_node;
509         struct phy_device *phydev;
510         int ret;
511
512         /* Fetch the PHY phandle */
513         priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
514
515         /* In the case of a fixed PHY, the DT node associated
516          * to the PHY is the Ethernet MAC DT node.
517          */
518         if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
519                 ret = of_phy_register_fixed_link(dn);
520                 if (ret)
521                         return ret;
522
523                 priv->phy_dn = of_node_get(dn);
524         }
525
526         /* Get the link mode */
527         ret = bcmgenet_phy_interface_init(priv);
528         if (ret)
529                 return ret;
530
531         /* Make sure we initialize MoCA PHYs with a link down */
532         if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
533                 phydev = of_phy_find_device(dn);
534                 if (phydev) {
535                         phydev->link = 0;
536                         put_device(&phydev->mdio.dev);
537                 }
538         }
539
540         return 0;
541 }
542
543 static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
544 {
545         struct device *kdev = &priv->pdev->dev;
546         struct bcmgenet_platform_data *pd = kdev->platform_data;
547         char phy_name[MII_BUS_ID_SIZE + 3];
548         char mdio_bus_id[MII_BUS_ID_SIZE];
549         struct phy_device *phydev;
550
551         snprintf(mdio_bus_id, MII_BUS_ID_SIZE, "%s-%d",
552                  UNIMAC_MDIO_DRV_NAME, priv->pdev->id);
553
554         if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
555                 snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
556                          mdio_bus_id, pd->phy_address);
557
558                 /*
559                  * Internal or external PHY with MDIO access
560                  */
561                 phydev = phy_attach(priv->dev, phy_name, pd->phy_interface);
562                 if (!phydev) {
563                         dev_err(kdev, "failed to register PHY device\n");
564                         return -ENODEV;
565                 }
566         } else {
567                 /*
568                  * MoCA port or no MDIO access.
569                  * Use fixed PHY to represent the link layer.
570                  */
571                 struct fixed_phy_status fphy_status = {
572                         .link = 1,
573                         .speed = pd->phy_speed,
574                         .duplex = pd->phy_duplex,
575                         .pause = 0,
576                         .asym_pause = 0,
577                 };
578
579                 phydev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
580                 if (!phydev || IS_ERR(phydev)) {
581                         dev_err(kdev, "failed to register fixed PHY device\n");
582                         return -ENODEV;
583                 }
584
585                 /* Make sure we initialize MoCA PHYs with a link down */
586                 phydev->link = 0;
587
588         }
589
590         priv->phy_interface = pd->phy_interface;
591
592         return 0;
593 }
594
595 static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
596 {
597         struct device *kdev = &priv->pdev->dev;
598         struct device_node *dn = kdev->of_node;
599
600         if (dn)
601                 return bcmgenet_mii_of_init(priv);
602         else if (has_acpi_companion(kdev))
603                 return bcmgenet_phy_interface_init(priv);
604         else
605                 return bcmgenet_mii_pd_init(priv);
606 }
607
608 int bcmgenet_mii_init(struct net_device *dev)
609 {
610         struct bcmgenet_priv *priv = netdev_priv(dev);
611         int ret;
612
613         ret = bcmgenet_mii_register(priv);
614         if (ret)
615                 return ret;
616
617         ret = bcmgenet_mii_bus_init(priv);
618         if (ret)
619                 goto out;
620
621         return 0;
622
623 out:
624         bcmgenet_mii_exit(dev);
625         return ret;
626 }
627
628 void bcmgenet_mii_exit(struct net_device *dev)
629 {
630         struct bcmgenet_priv *priv = netdev_priv(dev);
631         struct device_node *dn = priv->pdev->dev.of_node;
632
633         if (of_phy_is_fixed_link(dn))
634                 of_phy_deregister_fixed_link(dn);
635         of_node_put(priv->phy_dn);
636         platform_device_unregister(priv->mii_pdev);
637 }