blob: 51880d83131ac8c2ad34ff4d453a0fd5a757d4df [file] [log] [blame]
Florian Fainelliaa096772014-02-13 16:08:48 -08001/*
2 * Broadcom GENET MDIO routines
3 *
Doug Berger42138082017-03-13 17:41:42 -07004 * Copyright (c) 2014-2017 Broadcom
Florian Fainelliaa096772014-02-13 16:08:48 -08005 *
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 Fainelliaa096772014-02-13 16:08:48 -08009 */
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 Gyntherb0ba5122014-12-01 16:18:08 -080026#include <linux/platform_data/bcmgenet.h>
Florian Fainelli9a4e7962017-07-31 12:04:26 -070027#include <linux/platform_data/mdio-bcm-unimac.h>
Florian Fainelliaa096772014-02-13 16:08:48 -080028
29#include "bcmgenet.h"
30
Florian Fainelliaa096772014-02-13 16:08:48 -080031/* setup netdev link state when PHY link status change and
32 * update UMAC and RGMII block when link up
33 */
Florian Fainellic96e7312014-11-10 18:06:20 -080034void bcmgenet_mii_setup(struct net_device *dev)
Florian Fainelliaa096772014-02-13 16:08:48 -080035{
36 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger6c97f012017-10-25 15:04:19 -070037 struct phy_device *phydev = dev->phydev;
Florian Fainelliaa096772014-02-13 16:08:48 -080038 u32 reg, cmd_bits = 0;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070039 bool status_changed = false;
Florian Fainelliaa096772014-02-13 16:08:48 -080040
41 if (priv->old_link != phydev->link) {
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070042 status_changed = true;
Florian Fainelliaa096772014-02-13 16:08:48 -080043 priv->old_link = phydev->link;
44 }
45
46 if (phydev->link) {
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070047 /* 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 Fainelliaa096772014-02-13 16:08:48 -080066
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 Fainelliaa096772014-02-13 16:08:48 -080076 /* duplex */
77 if (phydev->duplex != DUPLEX_FULL)
78 cmd_bits |= CMD_HD_EN;
79
Florian Fainelliaa096772014-02-13 16:08:48 -080080 /* pause capability */
81 if (!phydev->pause)
82 cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
83
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070084 /*
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 Fainellic677ba82014-08-11 14:50:44 -070095
Florian Fainelliaa096772014-02-13 16:08:48 -080096 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 Gynther5ad6e6c2014-10-03 12:25:01 -0700102 } else {
103 /* done if nothing has changed */
104 if (!status_changed)
105 return;
Florian Fainelliaa096772014-02-13 16:08:48 -0800106
Petri Gynther5ad6e6c2014-10-03 12:25:01 -0700107 /* needed for MoCA fixed PHY to reflect correct link status */
108 netif_carrier_off(dev);
Florian Fainelli24052402014-07-21 17:42:39 -0700109 }
Florian Fainellic677ba82014-08-11 14:50:44 -0700110
111 phy_print_status(phydev);
Florian Fainelliaa096772014-02-13 16:08:48 -0800112}
113
Florian Fainelli5dbebbb2015-10-29 18:11:35 -0700114
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700115static int bcmgenet_fixed_phy_link_update(struct net_device *dev,
116 struct fixed_phy_status *status)
117{
Doug Bergerc3c397c2018-08-28 12:33:15 -0700118 struct bcmgenet_priv *priv;
119 u32 reg;
120
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);
125 }
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700126
127 return 0;
128}
129
Florian Fainellia642c4f2015-03-23 15:09:56 -0700130void bcmgenet_phy_power_set(struct net_device *dev, bool enable)
Florian Fainelliaa096772014-02-13 16:08:48 -0800131{
132 struct bcmgenet_priv *priv = netdev_priv(dev);
133 u32 reg = 0;
134
135 /* EXT_GPHY_CTRL is only valid for GENETv4 and onward */
Doug Berger42138082017-03-13 17:41:42 -0700136 if (GENET_IS_V4(priv)) {
137 reg = bcmgenet_ext_readl(priv, EXT_GPHY_CTRL);
138 if (enable) {
139 reg &= ~EXT_CK25_DIS;
140 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
141 mdelay(1);
Florian Fainelliaa096772014-02-13 16:08:48 -0800142
Doug Berger42138082017-03-13 17:41:42 -0700143 reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN);
144 reg |= EXT_GPHY_RESET;
145 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
146 mdelay(1);
147
148 reg &= ~EXT_GPHY_RESET;
149 } else {
150 reg |= EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN |
151 EXT_GPHY_RESET;
152 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
153 mdelay(1);
154 reg |= EXT_CK25_DIS;
155 }
Florian Fainelli0c81a8e2015-03-23 15:09:54 -0700156 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
Doug Berger42138082017-03-13 17:41:42 -0700157 udelay(60);
Florian Fainellia9d608c2015-03-23 15:09:55 -0700158 } else {
Florian Fainellia9d608c2015-03-23 15:09:55 -0700159 mdelay(1);
Florian Fainelli8212c982015-03-23 15:09:53 -0700160 }
Florian Fainelliaa096772014-02-13 16:08:48 -0800161}
162
Florian Fainelliaa096772014-02-13 16:08:48 -0800163static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
164{
165 u32 reg;
166
Doug Berger42138082017-03-13 17:41:42 -0700167 if (!GENET_IS_V5(priv)) {
168 /* Speed settings are set in bcmgenet_mii_setup() */
169 reg = bcmgenet_sys_readl(priv, SYS_PORT_CTRL);
170 reg |= LED_ACT_SOURCE_MAC;
171 bcmgenet_sys_writel(priv, reg, SYS_PORT_CTRL);
172 }
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700173
174 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
Doug Berger6c97f012017-10-25 15:04:19 -0700175 fixed_phy_set_link_update(priv->dev->phydev,
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700176 bcmgenet_fixed_phy_link_update);
Florian Fainelliaa096772014-02-13 16:08:48 -0800177}
178
Florian Fainelli00d51092017-07-31 11:05:32 -0700179int bcmgenet_mii_config(struct net_device *dev, bool init)
Florian Fainelliaa096772014-02-13 16:08:48 -0800180{
181 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger6c97f012017-10-25 15:04:19 -0700182 struct phy_device *phydev = dev->phydev;
Florian Fainelliaa096772014-02-13 16:08:48 -0800183 struct device *kdev = &priv->pdev->dev;
184 const char *phy_name = NULL;
185 u32 id_mode_dis = 0;
186 u32 port_ctrl;
187 u32 reg;
188
Florian Fainellic624f892015-07-16 15:51:17 -0700189 priv->ext_phy = !priv->internal_phy &&
Florian Fainelliaa096772014-02-13 16:08:48 -0800190 (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
191
Florian Fainelliaa096772014-02-13 16:08:48 -0800192 switch (priv->phy_interface) {
Florian Fainelli40bc8b02017-06-23 10:33:15 -0700193 case PHY_INTERFACE_MODE_INTERNAL:
Florian Fainelliaa096772014-02-13 16:08:48 -0800194 case PHY_INTERFACE_MODE_MOCA:
195 /* Irrespective of the actually configured PHY speed (100 or
196 * 1000) GENETv4 only has an internal GPHY so we will just end
197 * up masking the Gigabit features from what we support, not
198 * switching to the EPHY
199 */
200 if (GENET_IS_V4(priv))
201 port_ctrl = PORT_MODE_INT_GPHY;
202 else
203 port_ctrl = PORT_MODE_INT_EPHY;
204
205 bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
206
Florian Fainellic624f892015-07-16 15:51:17 -0700207 if (priv->internal_phy) {
Florian Fainelliaa096772014-02-13 16:08:48 -0800208 phy_name = "internal PHY";
Florian Fainelliaa096772014-02-13 16:08:48 -0800209 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
210 phy_name = "MoCA";
211 bcmgenet_moca_phy_setup(priv);
212 }
213 break;
214
215 case PHY_INTERFACE_MODE_MII:
216 phy_name = "external MII";
Andrew Lunn58056c12018-09-12 01:53:11 +0200217 phy_set_max_speed(phydev, SPEED_100);
Florian Fainelliaa096772014-02-13 16:08:48 -0800218 bcmgenet_sys_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700219 PORT_MODE_EXT_EPHY, SYS_PORT_CTRL);
Florian Fainelliaa096772014-02-13 16:08:48 -0800220 break;
221
222 case PHY_INTERFACE_MODE_REVMII:
223 phy_name = "external RvMII";
224 /* of_mdiobus_register took care of reading the 'max-speed'
225 * PHY property for us, effectively limiting the PHY supported
226 * capabilities, use that knowledge to also configure the
227 * Reverse MII interface correctly.
228 */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100229 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
230 dev->phydev->supported))
Florian Fainelliaa096772014-02-13 16:08:48 -0800231 port_ctrl = PORT_MODE_EXT_RVMII_50;
Andrew Lunn00eb2242018-09-12 01:53:12 +0200232 else
233 port_ctrl = PORT_MODE_EXT_RVMII_25;
Florian Fainelliaa096772014-02-13 16:08:48 -0800234 bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
235 break;
236
237 case PHY_INTERFACE_MODE_RGMII:
238 /* RGMII_NO_ID: TXC transitions at the same time as TXD
239 * (requires PCB or receiver-side delay)
240 * RGMII: Add 2ns delay on TXC (90 degree shift)
241 *
242 * ID is implicitly disabled for 100Mbps (RG)MII operation.
243 */
244 id_mode_dis = BIT(16);
245 /* fall through */
246 case PHY_INTERFACE_MODE_RGMII_TXID:
247 if (id_mode_dis)
248 phy_name = "external RGMII (no delay)";
249 else
250 phy_name = "external RGMII (TX delay)";
Florian Fainelliaa096772014-02-13 16:08:48 -0800251 bcmgenet_sys_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700252 PORT_MODE_EXT_GPHY, SYS_PORT_CTRL);
Florian Fainelliaa096772014-02-13 16:08:48 -0800253 break;
254 default:
255 dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
256 return -EINVAL;
257 }
258
Florian Fainelliafe3f902015-06-08 10:47:57 -0700259 /* This is an external PHY (xMII), so we need to enable the RGMII
260 * block for the interface to work
261 */
262 if (priv->ext_phy) {
263 reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
264 reg |= RGMII_MODE_EN | id_mode_dis;
265 bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
266 }
267
Florian Fainelli00d51092017-07-31 11:05:32 -0700268 if (init)
269 dev_info(kdev, "configuring instance for %s\n", phy_name);
Florian Fainelliaa096772014-02-13 16:08:48 -0800270
271 return 0;
272}
273
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -0700274int bcmgenet_mii_probe(struct net_device *dev)
Florian Fainelliaa096772014-02-13 16:08:48 -0800275{
276 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli9abf0c22014-05-22 09:47:45 -0700277 struct device_node *dn = priv->pdev->dev.of_node;
Florian Fainelliaa096772014-02-13 16:08:48 -0800278 struct phy_device *phydev;
Florian Fainelli487320c2014-09-19 13:07:53 -0700279 u32 phy_flags;
Florian Fainelliaa096772014-02-13 16:08:48 -0800280 int ret;
281
Florian Fainelli487320c2014-09-19 13:07:53 -0700282 /* Communicate the integrated PHY revision */
283 phy_flags = priv->gphy_rev;
284
Petri Gynther5ad6e6c2014-10-03 12:25:01 -0700285 /* Initialize link state variables that bcmgenet_mii_setup() uses */
286 priv->old_link = -1;
287 priv->old_speed = -1;
288 priv->old_duplex = -1;
289 priv->old_pause = -1;
290
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800291 if (dn) {
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800292 phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
293 phy_flags, priv->phy_interface);
294 if (!phydev) {
295 pr_err("could not attach to PHY\n");
296 return -ENODEV;
297 }
298 } else {
Doug Berger6c97f012017-10-25 15:04:19 -0700299 phydev = dev->phydev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800300 phydev->dev_flags = phy_flags;
301
302 ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
303 priv->phy_interface);
304 if (ret) {
305 pr_err("could not attach to PHY\n");
306 return -ENODEV;
307 }
Florian Fainelliaa096772014-02-13 16:08:48 -0800308 }
309
Florian Fainelliaa096772014-02-13 16:08:48 -0800310 /* Configure port multiplexer based on what the probed PHY device since
311 * reading the 'max-speed' property determines the maximum supported
312 * PHY speed which is needed for bcmgenet_mii_config() to configure
313 * things appropriately.
314 */
Florian Fainelli00d51092017-07-31 11:05:32 -0700315 ret = bcmgenet_mii_config(dev, true);
Florian Fainelliaa096772014-02-13 16:08:48 -0800316 if (ret) {
Doug Berger6c97f012017-10-25 15:04:19 -0700317 phy_disconnect(dev->phydev);
Florian Fainelliaa096772014-02-13 16:08:48 -0800318 return ret;
319 }
320
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100321 linkmode_copy(phydev->advertising, phydev->supported);
Florian Fainelliaa096772014-02-13 16:08:48 -0800322
323 /* The internal PHY has its link interrupts routed to the
Florian Fainelli64bd9c82018-10-11 15:06:33 -0700324 * Ethernet MAC ISRs. On GENETv5 there is a hardware issue
325 * that prevents the signaling of link UP interrupts when
326 * the link operates at 10Mbps, so fallback to polling for
327 * those versions of GENET.
Florian Fainelliaa096772014-02-13 16:08:48 -0800328 */
Florian Fainelli64bd9c82018-10-11 15:06:33 -0700329 if (priv->internal_phy && !GENET_IS_V5(priv))
Doug Berger6c97f012017-10-25 15:04:19 -0700330 dev->phydev->irq = PHY_IGNORE_INTERRUPT;
Florian Fainelliaa096772014-02-13 16:08:48 -0800331
Florian Fainelliaa096772014-02-13 16:08:48 -0800332 return 0;
333}
334
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700335static struct device_node *bcmgenet_mii_of_find_mdio(struct bcmgenet_priv *priv)
Florian Fainelliaa096772014-02-13 16:08:48 -0800336{
337 struct device_node *dn = priv->pdev->dev.of_node;
338 struct device *kdev = &priv->pdev->dev;
Florian Fainelliaa096772014-02-13 16:08:48 -0800339 char *compat;
Florian Fainelliaa096772014-02-13 16:08:48 -0800340
341 compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
342 if (!compat)
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700343 return NULL;
Florian Fainelliaa096772014-02-13 16:08:48 -0800344
Johan Hovoldd397dbe2018-08-27 10:21:50 +0200345 priv->mdio_dn = of_get_compatible_child(dn, compat);
Florian Fainelliaa096772014-02-13 16:08:48 -0800346 kfree(compat);
Florian Fainelli7b635da2015-06-26 10:39:05 -0700347 if (!priv->mdio_dn) {
Florian Fainelliaa096772014-02-13 16:08:48 -0800348 dev_err(kdev, "unable to find MDIO bus node\n");
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700349 return NULL;
Florian Fainelliaa096772014-02-13 16:08:48 -0800350 }
351
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700352 return priv->mdio_dn;
353}
354
355static void bcmgenet_mii_pdata_init(struct bcmgenet_priv *priv,
356 struct unimac_mdio_pdata *ppd)
357{
358 struct device *kdev = &priv->pdev->dev;
359 struct bcmgenet_platform_data *pd = kdev->platform_data;
360
361 if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
362 /*
363 * Internal or external PHY with MDIO access
364 */
365 if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
366 ppd->phy_mask = 1 << pd->phy_address;
367 else
368 ppd->phy_mask = 0;
Florian Fainelliaa096772014-02-13 16:08:48 -0800369 }
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700370}
371
372static int bcmgenet_mii_wait(void *wait_func_data)
373{
374 struct bcmgenet_priv *priv = wait_func_data;
375
376 wait_event_timeout(priv->wq,
377 !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
378 & MDIO_START_BUSY),
379 HZ / 100);
380 return 0;
381}
382
383static int bcmgenet_mii_register(struct bcmgenet_priv *priv)
384{
385 struct platform_device *pdev = priv->pdev;
386 struct bcmgenet_platform_data *pdata = pdev->dev.platform_data;
387 struct device_node *dn = pdev->dev.of_node;
388 struct unimac_mdio_pdata ppd;
389 struct platform_device *ppdev;
390 struct resource *pres, res;
391 int id, ret;
392
393 pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
394 memset(&res, 0, sizeof(res));
395 memset(&ppd, 0, sizeof(ppd));
396
397 ppd.wait_func = bcmgenet_mii_wait;
398 ppd.wait_func_data = priv;
399 ppd.bus_name = "bcmgenet MII bus";
400
401 /* Unimac MDIO bus controller starts at UniMAC offset + MDIO_CMD
402 * and is 2 * 32-bits word long, 8 bytes total.
403 */
404 res.start = pres->start + GENET_UMAC_OFF + UMAC_MDIO_CMD;
405 res.end = res.start + 8;
406 res.flags = IORESOURCE_MEM;
407
408 if (dn)
409 id = of_alias_get_id(dn, "eth");
410 else
411 id = pdev->id;
412
413 ppdev = platform_device_alloc(UNIMAC_MDIO_DRV_NAME, id);
414 if (!ppdev)
415 return -ENOMEM;
416
417 /* Retain this platform_device pointer for later cleanup */
418 priv->mii_pdev = ppdev;
419 ppdev->dev.parent = &pdev->dev;
420 ppdev->dev.of_node = bcmgenet_mii_of_find_mdio(priv);
421 if (pdata)
422 bcmgenet_mii_pdata_init(priv, &ppd);
423
424 ret = platform_device_add_resources(ppdev, &res, 1);
425 if (ret)
426 goto out;
427
428 ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
429 if (ret)
430 goto out;
431
432 ret = platform_device_add(ppdev);
433 if (ret)
434 goto out;
435
436 return 0;
437out:
438 platform_device_put(ppdev);
439 return ret;
440}
441
442static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
443{
444 struct device_node *dn = priv->pdev->dev.of_node;
445 struct device *kdev = &priv->pdev->dev;
446 struct phy_device *phydev;
447 int phy_mode;
448 int ret;
Florian Fainelliaa096772014-02-13 16:08:48 -0800449
450 /* Fetch the PHY phandle */
451 priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
452
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -0700453 /* In the case of a fixed PHY, the DT node associated
454 * to the PHY is the Ethernet MAC DT node.
455 */
456 if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
457 ret = of_phy_register_fixed_link(dn);
458 if (ret)
459 return ret;
460
461 priv->phy_dn = of_node_get(dn);
462 }
463
Florian Fainelliaa096772014-02-13 16:08:48 -0800464 /* Get the link mode */
Florian Fainellic624f892015-07-16 15:51:17 -0700465 phy_mode = of_get_phy_mode(dn);
Florian Fainelli40bc8b02017-06-23 10:33:15 -0700466 if (phy_mode < 0) {
467 dev_err(kdev, "invalid PHY mode property\n");
468 return phy_mode;
469 }
470
Florian Fainellic624f892015-07-16 15:51:17 -0700471 priv->phy_interface = phy_mode;
472
473 /* We need to specifically look up whether this PHY interface is internal
474 * or not *before* we even try to probe the PHY driver over MDIO as we
475 * may have shut down the internal PHY for power saving purposes.
476 */
Florian Fainelli40bc8b02017-06-23 10:33:15 -0700477 if (priv->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
478 priv->internal_phy = true;
Florian Fainelliaa096772014-02-13 16:08:48 -0800479
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700480 /* Make sure we initialize MoCA PHYs with a link down */
481 if (phy_mode == PHY_INTERFACE_MODE_MOCA) {
482 phydev = of_phy_find_device(dn);
Johan Hovold0da60542016-11-24 19:21:28 +0100483 if (phydev) {
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700484 phydev->link = 0;
Johan Hovold0da60542016-11-24 19:21:28 +0100485 put_device(&phydev->mdio.dev);
486 }
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700487 }
Petri Gynther8d88c6e2015-04-01 00:40:00 -0700488
489 return 0;
490}
491
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800492static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
493{
494 struct device *kdev = &priv->pdev->dev;
495 struct bcmgenet_platform_data *pd = kdev->platform_data;
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700496 char phy_name[MII_BUS_ID_SIZE + 3];
497 char mdio_bus_id[MII_BUS_ID_SIZE];
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800498 struct phy_device *phydev;
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700499
500 snprintf(mdio_bus_id, MII_BUS_ID_SIZE, "%s-%d",
501 UNIMAC_MDIO_DRV_NAME, priv->pdev->id);
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800502
503 if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700504 snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
505 mdio_bus_id, pd->phy_address);
506
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800507 /*
508 * Internal or external PHY with MDIO access
509 */
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700510 phydev = phy_attach(priv->dev, phy_name, pd->phy_interface);
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800511 if (!phydev) {
512 dev_err(kdev, "failed to register PHY device\n");
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800513 return -ENODEV;
514 }
515 } else {
516 /*
517 * MoCA port or no MDIO access.
518 * Use fixed PHY to represent the link layer.
519 */
520 struct fixed_phy_status fphy_status = {
521 .link = 1,
522 .speed = pd->phy_speed,
523 .duplex = pd->phy_duplex,
524 .pause = 0,
525 .asym_pause = 0,
526 };
527
Linus Walleij5468e822019-02-04 11:26:18 +0100528 phydev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800529 if (!phydev || IS_ERR(phydev)) {
530 dev_err(kdev, "failed to register fixed PHY device\n");
531 return -ENODEV;
532 }
Petri Gynther8d88c6e2015-04-01 00:40:00 -0700533
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700534 /* Make sure we initialize MoCA PHYs with a link down */
535 phydev->link = 0;
536
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800537 }
538
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800539 priv->phy_interface = pd->phy_interface;
540
541 return 0;
542}
543
544static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
545{
546 struct device_node *dn = priv->pdev->dev.of_node;
547
548 if (dn)
549 return bcmgenet_mii_of_init(priv);
550 else
551 return bcmgenet_mii_pd_init(priv);
552}
553
Florian Fainelliaa096772014-02-13 16:08:48 -0800554int bcmgenet_mii_init(struct net_device *dev)
555{
556 struct bcmgenet_priv *priv = netdev_priv(dev);
557 int ret;
558
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700559 ret = bcmgenet_mii_register(priv);
Florian Fainelliaa096772014-02-13 16:08:48 -0800560 if (ret)
561 return ret;
562
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800563 ret = bcmgenet_mii_bus_init(priv);
Florian Fainelliaa096772014-02-13 16:08:48 -0800564 if (ret)
Florian Fainelliaa096772014-02-13 16:08:48 -0800565 goto out;
566
567 return 0;
568
569out:
Florian Fainelli6f24b852017-07-31 12:04:28 -0700570 bcmgenet_mii_exit(dev);
Florian Fainelliaa096772014-02-13 16:08:48 -0800571 return ret;
572}
573
574void bcmgenet_mii_exit(struct net_device *dev)
575{
576 struct bcmgenet_priv *priv = netdev_priv(dev);
Johan Hovold140ca9d2016-11-28 19:24:59 +0100577 struct device_node *dn = priv->pdev->dev.of_node;
Florian Fainelliaa096772014-02-13 16:08:48 -0800578
Johan Hovold140ca9d2016-11-28 19:24:59 +0100579 if (of_phy_is_fixed_link(dn))
580 of_phy_deregister_fixed_link(dn);
Uwe Kleine-König95182592014-08-07 22:53:40 +0200581 of_node_put(priv->phy_dn);
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700582 platform_device_unregister(priv->mii_pdev);
Florian Fainelliaa096772014-02-13 16:08:48 -0800583}