2 * Broadcom GENET MDIO routines
4 * Copyright (c) 2014-2017 Broadcom
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/types.h>
13 #include <linux/delay.h>
14 #include <linux/wait.h>
15 #include <linux/mii.h>
16 #include <linux/ethtool.h>
17 #include <linux/bitops.h>
18 #include <linux/netdevice.h>
19 #include <linux/platform_device.h>
20 #include <linux/phy.h>
21 #include <linux/phy_fixed.h>
22 #include <linux/brcmphy.h>
24 #include <linux/of_net.h>
25 #include <linux/of_mdio.h>
26 #include <linux/platform_data/bcmgenet.h>
27 #include <linux/platform_data/mdio-bcm-unimac.h>
31 /* setup netdev link state when PHY link status change and
32 * update UMAC and RGMII block when link up
34 void bcmgenet_mii_setup(struct net_device *dev)
36 struct bcmgenet_priv *priv = netdev_priv(dev);
37 struct phy_device *phydev = priv->phydev;
38 u32 reg, cmd_bits = 0;
39 bool status_changed = false;
41 if (priv->old_link != phydev->link) {
42 status_changed = true;
43 priv->old_link = phydev->link;
47 /* check speed/duplex/pause changes */
48 if (priv->old_speed != phydev->speed) {
49 status_changed = true;
50 priv->old_speed = phydev->speed;
53 if (priv->old_duplex != phydev->duplex) {
54 status_changed = true;
55 priv->old_duplex = phydev->duplex;
58 if (priv->old_pause != phydev->pause) {
59 status_changed = true;
60 priv->old_pause = phydev->pause;
63 /* done if nothing has changed */
68 if (phydev->speed == SPEED_1000)
69 cmd_bits = UMAC_SPEED_1000;
70 else if (phydev->speed == SPEED_100)
71 cmd_bits = UMAC_SPEED_100;
73 cmd_bits = UMAC_SPEED_10;
74 cmd_bits <<= CMD_SPEED_SHIFT;
77 if (phydev->duplex != DUPLEX_FULL)
78 cmd_bits |= CMD_HD_EN;
80 /* pause capability */
82 cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
85 * Program UMAC and RGMII block based on established
86 * link speed, duplex, and pause. The speed set in
87 * umac->cmd tell RGMII block which clock to use for
88 * transmit -- 25MHz(100Mbps) or 125MHz(1Gbps).
89 * Receive clock is provided by the PHY.
91 reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
94 bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
96 reg = bcmgenet_umac_readl(priv, UMAC_CMD);
97 reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
99 CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE);
101 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
103 /* done if nothing has changed */
107 /* needed for MoCA fixed PHY to reflect correct link status */
108 netif_carrier_off(dev);
111 phy_print_status(phydev);
115 static int bcmgenet_fixed_phy_link_update(struct net_device *dev,
116 struct fixed_phy_status *status)
118 struct bcmgenet_priv *priv;
121 if (dev && dev->phydev && status) {
122 priv = netdev_priv(dev);
123 reg = bcmgenet_umac_readl(priv, UMAC_MODE);
124 status->link = !!(reg & MODE_LINK_STATUS);
130 /* Perform a voluntary PHY software reset, since the EPHY is very finicky about
131 * not doing it and will start corrupting packets
133 void bcmgenet_mii_reset(struct net_device *dev)
135 struct bcmgenet_priv *priv = netdev_priv(dev);
137 if (GENET_IS_V4(priv))
141 phy_init_hw(priv->phydev);
142 phy_start_aneg(priv->phydev);
146 void bcmgenet_phy_power_set(struct net_device *dev, bool enable)
148 struct bcmgenet_priv *priv = netdev_priv(dev);
151 /* EXT_GPHY_CTRL is only valid for GENETv4 and onward */
152 if (GENET_IS_V4(priv)) {
153 reg = bcmgenet_ext_readl(priv, EXT_GPHY_CTRL);
155 reg &= ~EXT_CK25_DIS;
156 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
159 reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN);
160 reg |= EXT_GPHY_RESET;
161 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
164 reg &= ~EXT_GPHY_RESET;
166 reg |= EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN |
168 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
172 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
179 static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
183 if (!GENET_IS_V5(priv)) {
184 /* Speed settings are set in bcmgenet_mii_setup() */
185 reg = bcmgenet_sys_readl(priv, SYS_PORT_CTRL);
186 reg |= LED_ACT_SOURCE_MAC;
187 bcmgenet_sys_writel(priv, reg, SYS_PORT_CTRL);
190 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
191 fixed_phy_set_link_update(priv->phydev,
192 bcmgenet_fixed_phy_link_update);
195 int bcmgenet_mii_config(struct net_device *dev, bool init)
197 struct bcmgenet_priv *priv = netdev_priv(dev);
198 struct phy_device *phydev = priv->phydev;
199 struct device *kdev = &priv->pdev->dev;
200 const char *phy_name = NULL;
205 priv->ext_phy = !priv->internal_phy &&
206 (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
208 switch (priv->phy_interface) {
209 case PHY_INTERFACE_MODE_INTERNAL:
210 case PHY_INTERFACE_MODE_MOCA:
211 /* Irrespective of the actually configured PHY speed (100 or
212 * 1000) GENETv4 only has an internal GPHY so we will just end
213 * up masking the Gigabit features from what we support, not
214 * switching to the EPHY
216 if (GENET_IS_V4(priv))
217 port_ctrl = PORT_MODE_INT_GPHY;
219 port_ctrl = PORT_MODE_INT_EPHY;
221 bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
223 if (priv->internal_phy) {
224 phy_name = "internal PHY";
225 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
227 bcmgenet_moca_phy_setup(priv);
231 case PHY_INTERFACE_MODE_MII:
232 phy_name = "external MII";
233 phydev->supported &= PHY_BASIC_FEATURES;
234 bcmgenet_sys_writel(priv,
235 PORT_MODE_EXT_EPHY, SYS_PORT_CTRL);
238 case PHY_INTERFACE_MODE_REVMII:
239 phy_name = "external RvMII";
240 /* of_mdiobus_register took care of reading the 'max-speed'
241 * PHY property for us, effectively limiting the PHY supported
242 * capabilities, use that knowledge to also configure the
243 * Reverse MII interface correctly.
245 if ((priv->phydev->supported & PHY_BASIC_FEATURES) ==
247 port_ctrl = PORT_MODE_EXT_RVMII_25;
249 port_ctrl = PORT_MODE_EXT_RVMII_50;
250 bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
253 case PHY_INTERFACE_MODE_RGMII:
254 /* RGMII_NO_ID: TXC transitions at the same time as TXD
255 * (requires PCB or receiver-side delay)
256 * RGMII: Add 2ns delay on TXC (90 degree shift)
258 * ID is implicitly disabled for 100Mbps (RG)MII operation.
260 id_mode_dis = BIT(16);
262 case PHY_INTERFACE_MODE_RGMII_TXID:
264 phy_name = "external RGMII (no delay)";
266 phy_name = "external RGMII (TX delay)";
267 bcmgenet_sys_writel(priv,
268 PORT_MODE_EXT_GPHY, SYS_PORT_CTRL);
271 dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
275 /* This is an external PHY (xMII), so we need to enable the RGMII
276 * block for the interface to work
279 reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
281 if (GENET_IS_V1(priv) || GENET_IS_V2(priv) || GENET_IS_V3(priv))
282 reg |= RGMII_MODE_EN_V123;
284 reg |= RGMII_MODE_EN;
285 bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
289 dev_info(kdev, "configuring instance for %s\n", phy_name);
294 int bcmgenet_mii_probe(struct net_device *dev)
296 struct bcmgenet_priv *priv = netdev_priv(dev);
297 struct device_node *dn = priv->pdev->dev.of_node;
298 struct phy_device *phydev;
302 /* Communicate the integrated PHY revision */
303 if (priv->internal_phy)
304 phy_flags = priv->gphy_rev;
306 /* Initialize link state variables that bcmgenet_mii_setup() uses */
308 priv->old_speed = -1;
309 priv->old_duplex = -1;
310 priv->old_pause = -1;
313 phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
314 phy_flags, priv->phy_interface);
316 pr_err("could not attach to PHY\n");
320 phydev = priv->phydev;
321 phydev->dev_flags = phy_flags;
323 ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
324 priv->phy_interface);
326 pr_err("could not attach to PHY\n");
331 priv->phydev = phydev;
333 /* Configure port multiplexer based on what the probed PHY device since
334 * reading the 'max-speed' property determines the maximum supported
335 * PHY speed which is needed for bcmgenet_mii_config() to configure
336 * things appropriately.
338 ret = bcmgenet_mii_config(dev, true);
340 phy_disconnect(priv->phydev);
344 phydev->advertising = phydev->supported;
346 /* The internal PHY has its link interrupts routed to the
347 * Ethernet MAC ISRs. On GENETv5 there is a hardware issue
348 * that prevents the signaling of link UP interrupts when
349 * the link operates at 10Mbps, so fallback to polling for
350 * those versions of GENET.
352 if (priv->internal_phy && !GENET_IS_V5(priv))
353 priv->phydev->irq = PHY_IGNORE_INTERRUPT;
358 static struct device_node *bcmgenet_mii_of_find_mdio(struct bcmgenet_priv *priv)
360 struct device_node *dn = priv->pdev->dev.of_node;
361 struct device *kdev = &priv->pdev->dev;
364 compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
368 priv->mdio_dn = of_get_compatible_child(dn, compat);
370 if (!priv->mdio_dn) {
371 dev_err(kdev, "unable to find MDIO bus node\n");
375 return priv->mdio_dn;
378 static void bcmgenet_mii_pdata_init(struct bcmgenet_priv *priv,
379 struct unimac_mdio_pdata *ppd)
381 struct device *kdev = &priv->pdev->dev;
382 struct bcmgenet_platform_data *pd = kdev->platform_data;
384 if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
386 * Internal or external PHY with MDIO access
388 if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
389 ppd->phy_mask = 1 << pd->phy_address;
395 static int bcmgenet_mii_wait(void *wait_func_data)
397 struct bcmgenet_priv *priv = wait_func_data;
399 wait_event_timeout(priv->wq,
400 !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
406 static int bcmgenet_mii_register(struct bcmgenet_priv *priv)
408 struct platform_device *pdev = priv->pdev;
409 struct bcmgenet_platform_data *pdata = pdev->dev.platform_data;
410 struct device_node *dn = pdev->dev.of_node;
411 struct unimac_mdio_pdata ppd;
412 struct platform_device *ppdev;
413 struct resource *pres, res;
416 pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
418 dev_err(&pdev->dev, "Invalid resource\n");
421 memset(&res, 0, sizeof(res));
422 memset(&ppd, 0, sizeof(ppd));
424 ppd.wait_func = bcmgenet_mii_wait;
425 ppd.wait_func_data = priv;
426 ppd.bus_name = "bcmgenet MII bus";
428 /* Unimac MDIO bus controller starts at UniMAC offset + MDIO_CMD
429 * and is 2 * 32-bits word long, 8 bytes total.
431 res.start = pres->start + GENET_UMAC_OFF + UMAC_MDIO_CMD;
432 res.end = res.start + 8;
433 res.flags = IORESOURCE_MEM;
436 id = of_alias_get_id(dn, "eth");
440 ppdev = platform_device_alloc(UNIMAC_MDIO_DRV_NAME, id);
444 /* Retain this platform_device pointer for later cleanup */
445 priv->mii_pdev = ppdev;
446 ppdev->dev.parent = &pdev->dev;
447 ppdev->dev.of_node = bcmgenet_mii_of_find_mdio(priv);
449 bcmgenet_mii_pdata_init(priv, &ppd);
451 ret = platform_device_add_resources(ppdev, &res, 1);
455 ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
459 ret = platform_device_add(ppdev);
465 platform_device_put(ppdev);
469 static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
471 struct device_node *dn = priv->pdev->dev.of_node;
472 struct device *kdev = &priv->pdev->dev;
473 struct phy_device *phydev;
477 /* Fetch the PHY phandle */
478 priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
480 /* In the case of a fixed PHY, the DT node associated
481 * to the PHY is the Ethernet MAC DT node.
483 if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
484 ret = of_phy_register_fixed_link(dn);
488 priv->phy_dn = of_node_get(dn);
491 /* Get the link mode */
492 phy_mode = of_get_phy_mode(dn);
494 dev_err(kdev, "invalid PHY mode property\n");
498 priv->phy_interface = phy_mode;
500 /* We need to specifically look up whether this PHY interface is internal
501 * or not *before* we even try to probe the PHY driver over MDIO as we
502 * may have shut down the internal PHY for power saving purposes.
504 if (priv->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
505 priv->internal_phy = true;
507 /* Make sure we initialize MoCA PHYs with a link down */
508 if (phy_mode == PHY_INTERFACE_MODE_MOCA) {
509 phydev = of_phy_find_device(dn);
512 put_device(&phydev->mdio.dev);
519 static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
521 struct device *kdev = &priv->pdev->dev;
522 struct bcmgenet_platform_data *pd = kdev->platform_data;
523 char phy_name[MII_BUS_ID_SIZE + 3];
524 char mdio_bus_id[MII_BUS_ID_SIZE];
525 struct phy_device *phydev;
527 snprintf(mdio_bus_id, MII_BUS_ID_SIZE, "%s-%d",
528 UNIMAC_MDIO_DRV_NAME, priv->pdev->id);
530 if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
531 snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
532 mdio_bus_id, pd->phy_address);
535 * Internal or external PHY with MDIO access
537 phydev = phy_attach(priv->dev, phy_name, pd->phy_interface);
539 dev_err(kdev, "failed to register PHY device\n");
544 * MoCA port or no MDIO access.
545 * Use fixed PHY to represent the link layer.
547 struct fixed_phy_status fphy_status = {
549 .speed = pd->phy_speed,
550 .duplex = pd->phy_duplex,
555 phydev = fixed_phy_register(PHY_POLL, &fphy_status, -1, NULL);
556 if (!phydev || IS_ERR(phydev)) {
557 dev_err(kdev, "failed to register fixed PHY device\n");
561 /* Make sure we initialize MoCA PHYs with a link down */
566 priv->phydev = phydev;
567 priv->phy_interface = pd->phy_interface;
572 static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
574 struct device_node *dn = priv->pdev->dev.of_node;
577 return bcmgenet_mii_of_init(priv);
579 return bcmgenet_mii_pd_init(priv);
582 int bcmgenet_mii_init(struct net_device *dev)
584 struct bcmgenet_priv *priv = netdev_priv(dev);
587 ret = bcmgenet_mii_register(priv);
591 ret = bcmgenet_mii_bus_init(priv);
598 bcmgenet_mii_exit(dev);
602 void bcmgenet_mii_exit(struct net_device *dev)
604 struct bcmgenet_priv *priv = netdev_priv(dev);
605 struct device_node *dn = priv->pdev->dev.of_node;
607 if (of_phy_is_fixed_link(dn))
608 of_phy_deregister_fixed_link(dn);
609 of_node_put(priv->phy_dn);
610 platform_device_unregister(priv->mii_pdev);
611 platform_device_put(priv->mii_pdev);