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