blob: 970e478a9017f8ef6437870c5601f9942bd44f29 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Florian Fainelliaa096772014-02-13 16:08:48 -08002/*
3 * Broadcom GENET MDIO routines
4 *
Doug Berger42138082017-03-13 17:41:42 -07005 * Copyright (c) 2014-2017 Broadcom
Florian Fainelliaa096772014-02-13 16:08:48 -08006 */
7
8
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>
Petri Gyntherb0ba5122014-12-01 16:18:08 -080023#include <linux/platform_data/bcmgenet.h>
Florian Fainelli9a4e7962017-07-31 12:04:26 -070024#include <linux/platform_data/mdio-bcm-unimac.h>
Florian Fainelliaa096772014-02-13 16:08:48 -080025
26#include "bcmgenet.h"
27
Florian Fainelliaa096772014-02-13 16:08:48 -080028/* setup netdev link state when PHY link status change and
29 * update UMAC and RGMII block when link up
30 */
Florian Fainellic96e7312014-11-10 18:06:20 -080031void bcmgenet_mii_setup(struct net_device *dev)
Florian Fainelliaa096772014-02-13 16:08:48 -080032{
33 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger6c97f012017-10-25 15:04:19 -070034 struct phy_device *phydev = dev->phydev;
Florian Fainelliaa096772014-02-13 16:08:48 -080035 u32 reg, cmd_bits = 0;
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070036 bool status_changed = false;
Florian Fainelliaa096772014-02-13 16:08:48 -080037
38 if (priv->old_link != phydev->link) {
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070039 status_changed = true;
Florian Fainelliaa096772014-02-13 16:08:48 -080040 priv->old_link = phydev->link;
41 }
42
43 if (phydev->link) {
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070044 /* 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;
Florian Fainelliaa096772014-02-13 16:08:48 -080063
64 /* speed */
65 if (phydev->speed == SPEED_1000)
66 cmd_bits = UMAC_SPEED_1000;
67 else if (phydev->speed == SPEED_100)
68 cmd_bits = UMAC_SPEED_100;
69 else
70 cmd_bits = UMAC_SPEED_10;
71 cmd_bits <<= CMD_SPEED_SHIFT;
72
Florian Fainelliaa096772014-02-13 16:08:48 -080073 /* duplex */
74 if (phydev->duplex != DUPLEX_FULL)
75 cmd_bits |= CMD_HD_EN;
76
Florian Fainelliaa096772014-02-13 16:08:48 -080077 /* pause capability */
78 if (!phydev->pause)
79 cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
80
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070081 /*
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);
Florian Fainellic677ba82014-08-11 14:50:44 -070092
Florian Fainelliaa096772014-02-13 16:08:48 -080093 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 bcmgenet_umac_writel(priv, reg, UMAC_CMD);
Petri Gynther5ad6e6c2014-10-03 12:25:01 -070099 } else {
100 /* done if nothing has changed */
101 if (!status_changed)
102 return;
Florian Fainelliaa096772014-02-13 16:08:48 -0800103
Petri Gynther5ad6e6c2014-10-03 12:25:01 -0700104 /* needed for MoCA fixed PHY to reflect correct link status */
105 netif_carrier_off(dev);
Florian Fainelli24052402014-07-21 17:42:39 -0700106 }
Florian Fainellic677ba82014-08-11 14:50:44 -0700107
108 phy_print_status(phydev);
Florian Fainelliaa096772014-02-13 16:08:48 -0800109}
110
Florian Fainelli5dbebbb2015-10-29 18:11:35 -0700111
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700112static int bcmgenet_fixed_phy_link_update(struct net_device *dev,
113 struct fixed_phy_status *status)
114{
Doug Bergerc3c397c2018-08-28 12:33:15 -0700115 struct bcmgenet_priv *priv;
116 u32 reg;
117
118 if (dev && dev->phydev && status) {
119 priv = netdev_priv(dev);
120 reg = bcmgenet_umac_readl(priv, UMAC_MODE);
121 status->link = !!(reg & MODE_LINK_STATUS);
122 }
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700123
124 return 0;
125}
126
Florian Fainellia642c4f2015-03-23 15:09:56 -0700127void bcmgenet_phy_power_set(struct net_device *dev, bool enable)
Florian Fainelliaa096772014-02-13 16:08:48 -0800128{
129 struct bcmgenet_priv *priv = netdev_priv(dev);
130 u32 reg = 0;
131
132 /* EXT_GPHY_CTRL is only valid for GENETv4 and onward */
Doug Berger42138082017-03-13 17:41:42 -0700133 if (GENET_IS_V4(priv)) {
134 reg = bcmgenet_ext_readl(priv, EXT_GPHY_CTRL);
135 if (enable) {
136 reg &= ~EXT_CK25_DIS;
137 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
138 mdelay(1);
Florian Fainelliaa096772014-02-13 16:08:48 -0800139
Doug Berger42138082017-03-13 17:41:42 -0700140 reg &= ~(EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN);
141 reg |= EXT_GPHY_RESET;
142 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
143 mdelay(1);
144
145 reg &= ~EXT_GPHY_RESET;
146 } else {
147 reg |= EXT_CFG_IDDQ_BIAS | EXT_CFG_PWR_DOWN |
148 EXT_GPHY_RESET;
149 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
150 mdelay(1);
151 reg |= EXT_CK25_DIS;
152 }
Florian Fainelli0c81a8e2015-03-23 15:09:54 -0700153 bcmgenet_ext_writel(priv, reg, EXT_GPHY_CTRL);
Doug Berger42138082017-03-13 17:41:42 -0700154 udelay(60);
Florian Fainellia9d608c2015-03-23 15:09:55 -0700155 } else {
Florian Fainellia9d608c2015-03-23 15:09:55 -0700156 mdelay(1);
Florian Fainelli8212c982015-03-23 15:09:53 -0700157 }
Florian Fainelliaa096772014-02-13 16:08:48 -0800158}
159
Florian Fainelliaa096772014-02-13 16:08:48 -0800160static void bcmgenet_moca_phy_setup(struct bcmgenet_priv *priv)
161{
162 u32 reg;
163
Doug Berger42138082017-03-13 17:41:42 -0700164 if (!GENET_IS_V5(priv)) {
165 /* Speed settings are set in bcmgenet_mii_setup() */
166 reg = bcmgenet_sys_readl(priv, SYS_PORT_CTRL);
167 reg |= LED_ACT_SOURCE_MAC;
168 bcmgenet_sys_writel(priv, reg, SYS_PORT_CTRL);
169 }
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700170
171 if (priv->hw_params->flags & GENET_HAS_MOCA_LINK_DET)
Doug Berger6c97f012017-10-25 15:04:19 -0700172 fixed_phy_set_link_update(priv->dev->phydev,
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700173 bcmgenet_fixed_phy_link_update);
Florian Fainelliaa096772014-02-13 16:08:48 -0800174}
175
Florian Fainelli00d51092017-07-31 11:05:32 -0700176int bcmgenet_mii_config(struct net_device *dev, bool init)
Florian Fainelliaa096772014-02-13 16:08:48 -0800177{
178 struct bcmgenet_priv *priv = netdev_priv(dev);
Doug Berger6c97f012017-10-25 15:04:19 -0700179 struct phy_device *phydev = dev->phydev;
Florian Fainelliaa096772014-02-13 16:08:48 -0800180 struct device *kdev = &priv->pdev->dev;
181 const char *phy_name = NULL;
182 u32 id_mode_dis = 0;
183 u32 port_ctrl;
184 u32 reg;
185
Florian Fainellic624f892015-07-16 15:51:17 -0700186 priv->ext_phy = !priv->internal_phy &&
Florian Fainelliaa096772014-02-13 16:08:48 -0800187 (priv->phy_interface != PHY_INTERFACE_MODE_MOCA);
188
Florian Fainelliaa096772014-02-13 16:08:48 -0800189 switch (priv->phy_interface) {
Florian Fainelli40bc8b02017-06-23 10:33:15 -0700190 case PHY_INTERFACE_MODE_INTERNAL:
Florian Fainelliaa096772014-02-13 16:08:48 -0800191 case PHY_INTERFACE_MODE_MOCA:
192 /* Irrespective of the actually configured PHY speed (100 or
193 * 1000) GENETv4 only has an internal GPHY so we will just end
194 * up masking the Gigabit features from what we support, not
195 * switching to the EPHY
196 */
197 if (GENET_IS_V4(priv))
198 port_ctrl = PORT_MODE_INT_GPHY;
199 else
200 port_ctrl = PORT_MODE_INT_EPHY;
201
202 bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
203
Florian Fainellic624f892015-07-16 15:51:17 -0700204 if (priv->internal_phy) {
Florian Fainelliaa096772014-02-13 16:08:48 -0800205 phy_name = "internal PHY";
Florian Fainelliaa096772014-02-13 16:08:48 -0800206 } else if (priv->phy_interface == PHY_INTERFACE_MODE_MOCA) {
207 phy_name = "MoCA";
208 bcmgenet_moca_phy_setup(priv);
209 }
210 break;
211
212 case PHY_INTERFACE_MODE_MII:
213 phy_name = "external MII";
Andrew Lunn58056c12018-09-12 01:53:11 +0200214 phy_set_max_speed(phydev, SPEED_100);
Florian Fainelliaa096772014-02-13 16:08:48 -0800215 bcmgenet_sys_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700216 PORT_MODE_EXT_EPHY, SYS_PORT_CTRL);
Florian Fainelliaa096772014-02-13 16:08:48 -0800217 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 */
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100226 if (linkmode_test_bit(ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
227 dev->phydev->supported))
Florian Fainelliaa096772014-02-13 16:08:48 -0800228 port_ctrl = PORT_MODE_EXT_RVMII_50;
Andrew Lunn00eb2242018-09-12 01:53:12 +0200229 else
230 port_ctrl = PORT_MODE_EXT_RVMII_25;
Florian Fainelliaa096772014-02-13 16:08:48 -0800231 bcmgenet_sys_writel(priv, port_ctrl, SYS_PORT_CTRL);
232 break;
233
234 case PHY_INTERFACE_MODE_RGMII:
235 /* RGMII_NO_ID: TXC transitions at the same time as TXD
236 * (requires PCB or receiver-side delay)
237 * RGMII: Add 2ns delay on TXC (90 degree shift)
238 *
239 * ID is implicitly disabled for 100Mbps (RG)MII operation.
240 */
241 id_mode_dis = BIT(16);
242 /* fall through */
243 case PHY_INTERFACE_MODE_RGMII_TXID:
244 if (id_mode_dis)
245 phy_name = "external RGMII (no delay)";
246 else
247 phy_name = "external RGMII (TX delay)";
Florian Fainelliaa096772014-02-13 16:08:48 -0800248 bcmgenet_sys_writel(priv,
Florian Fainellic91b7f62014-07-23 10:42:12 -0700249 PORT_MODE_EXT_GPHY, SYS_PORT_CTRL);
Florian Fainelliaa096772014-02-13 16:08:48 -0800250 break;
251 default:
252 dev_err(kdev, "unknown phy mode: %d\n", priv->phy_interface);
253 return -EINVAL;
254 }
255
Florian Fainelliafe3f902015-06-08 10:47:57 -0700256 /* This is an external PHY (xMII), so we need to enable the RGMII
257 * block for the interface to work
258 */
259 if (priv->ext_phy) {
260 reg = bcmgenet_ext_readl(priv, EXT_RGMII_OOB_CTRL);
261 reg |= RGMII_MODE_EN | id_mode_dis;
262 bcmgenet_ext_writel(priv, reg, EXT_RGMII_OOB_CTRL);
263 }
264
Florian Fainelli00d51092017-07-31 11:05:32 -0700265 if (init)
266 dev_info(kdev, "configuring instance for %s\n", phy_name);
Florian Fainelliaa096772014-02-13 16:08:48 -0800267
268 return 0;
269}
270
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -0700271int bcmgenet_mii_probe(struct net_device *dev)
Florian Fainelliaa096772014-02-13 16:08:48 -0800272{
273 struct bcmgenet_priv *priv = netdev_priv(dev);
Florian Fainelli9abf0c22014-05-22 09:47:45 -0700274 struct device_node *dn = priv->pdev->dev.of_node;
Florian Fainelliaa096772014-02-13 16:08:48 -0800275 struct phy_device *phydev;
Florian Fainelli487320c2014-09-19 13:07:53 -0700276 u32 phy_flags;
Florian Fainelliaa096772014-02-13 16:08:48 -0800277 int ret;
278
Florian Fainelli487320c2014-09-19 13:07:53 -0700279 /* Communicate the integrated PHY revision */
280 phy_flags = priv->gphy_rev;
281
Petri Gynther5ad6e6c2014-10-03 12:25:01 -0700282 /* Initialize link state variables that bcmgenet_mii_setup() uses */
283 priv->old_link = -1;
284 priv->old_speed = -1;
285 priv->old_duplex = -1;
286 priv->old_pause = -1;
287
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800288 if (dn) {
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800289 phydev = of_phy_connect(dev, priv->phy_dn, bcmgenet_mii_setup,
290 phy_flags, priv->phy_interface);
291 if (!phydev) {
292 pr_err("could not attach to PHY\n");
293 return -ENODEV;
294 }
295 } else {
Doug Berger6c97f012017-10-25 15:04:19 -0700296 phydev = dev->phydev;
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800297 phydev->dev_flags = phy_flags;
298
299 ret = phy_connect_direct(dev, phydev, bcmgenet_mii_setup,
300 priv->phy_interface);
301 if (ret) {
302 pr_err("could not attach to PHY\n");
303 return -ENODEV;
304 }
Florian Fainelliaa096772014-02-13 16:08:48 -0800305 }
306
Florian Fainelliaa096772014-02-13 16:08:48 -0800307 /* Configure port multiplexer based on what the probed PHY device since
308 * reading the 'max-speed' property determines the maximum supported
309 * PHY speed which is needed for bcmgenet_mii_config() to configure
310 * things appropriately.
311 */
Florian Fainelli00d51092017-07-31 11:05:32 -0700312 ret = bcmgenet_mii_config(dev, true);
Florian Fainelliaa096772014-02-13 16:08:48 -0800313 if (ret) {
Doug Berger6c97f012017-10-25 15:04:19 -0700314 phy_disconnect(dev->phydev);
Florian Fainelliaa096772014-02-13 16:08:48 -0800315 return ret;
316 }
317
Andrew Lunn3c1bcc82018-11-10 23:43:33 +0100318 linkmode_copy(phydev->advertising, phydev->supported);
Florian Fainelliaa096772014-02-13 16:08:48 -0800319
320 /* The internal PHY has its link interrupts routed to the
Florian Fainelli64bd9c82018-10-11 15:06:33 -0700321 * Ethernet MAC ISRs. On GENETv5 there is a hardware issue
322 * that prevents the signaling of link UP interrupts when
323 * the link operates at 10Mbps, so fallback to polling for
324 * those versions of GENET.
Florian Fainelliaa096772014-02-13 16:08:48 -0800325 */
Florian Fainelli64bd9c82018-10-11 15:06:33 -0700326 if (priv->internal_phy && !GENET_IS_V5(priv))
Doug Berger6c97f012017-10-25 15:04:19 -0700327 dev->phydev->irq = PHY_IGNORE_INTERRUPT;
Florian Fainelliaa096772014-02-13 16:08:48 -0800328
Florian Fainelliaa096772014-02-13 16:08:48 -0800329 return 0;
330}
331
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700332static struct device_node *bcmgenet_mii_of_find_mdio(struct bcmgenet_priv *priv)
Florian Fainelliaa096772014-02-13 16:08:48 -0800333{
334 struct device_node *dn = priv->pdev->dev.of_node;
335 struct device *kdev = &priv->pdev->dev;
Florian Fainelliaa096772014-02-13 16:08:48 -0800336 char *compat;
Florian Fainelliaa096772014-02-13 16:08:48 -0800337
338 compat = kasprintf(GFP_KERNEL, "brcm,genet-mdio-v%d", priv->version);
339 if (!compat)
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700340 return NULL;
Florian Fainelliaa096772014-02-13 16:08:48 -0800341
Johan Hovoldd397dbe2018-08-27 10:21:50 +0200342 priv->mdio_dn = of_get_compatible_child(dn, compat);
Florian Fainelliaa096772014-02-13 16:08:48 -0800343 kfree(compat);
Florian Fainelli7b635da2015-06-26 10:39:05 -0700344 if (!priv->mdio_dn) {
Florian Fainelliaa096772014-02-13 16:08:48 -0800345 dev_err(kdev, "unable to find MDIO bus node\n");
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700346 return NULL;
Florian Fainelliaa096772014-02-13 16:08:48 -0800347 }
348
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700349 return priv->mdio_dn;
350}
351
352static void bcmgenet_mii_pdata_init(struct bcmgenet_priv *priv,
353 struct unimac_mdio_pdata *ppd)
354{
355 struct device *kdev = &priv->pdev->dev;
356 struct bcmgenet_platform_data *pd = kdev->platform_data;
357
358 if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
359 /*
360 * Internal or external PHY with MDIO access
361 */
362 if (pd->phy_address >= 0 && pd->phy_address < PHY_MAX_ADDR)
363 ppd->phy_mask = 1 << pd->phy_address;
364 else
365 ppd->phy_mask = 0;
Florian Fainelliaa096772014-02-13 16:08:48 -0800366 }
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700367}
368
369static int bcmgenet_mii_wait(void *wait_func_data)
370{
371 struct bcmgenet_priv *priv = wait_func_data;
372
373 wait_event_timeout(priv->wq,
374 !(bcmgenet_umac_readl(priv, UMAC_MDIO_CMD)
375 & MDIO_START_BUSY),
376 HZ / 100);
377 return 0;
378}
379
380static int bcmgenet_mii_register(struct bcmgenet_priv *priv)
381{
382 struct platform_device *pdev = priv->pdev;
383 struct bcmgenet_platform_data *pdata = pdev->dev.platform_data;
384 struct device_node *dn = pdev->dev.of_node;
385 struct unimac_mdio_pdata ppd;
386 struct platform_device *ppdev;
387 struct resource *pres, res;
388 int id, ret;
389
390 pres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
391 memset(&res, 0, sizeof(res));
392 memset(&ppd, 0, sizeof(ppd));
393
394 ppd.wait_func = bcmgenet_mii_wait;
395 ppd.wait_func_data = priv;
396 ppd.bus_name = "bcmgenet MII bus";
397
398 /* Unimac MDIO bus controller starts at UniMAC offset + MDIO_CMD
399 * and is 2 * 32-bits word long, 8 bytes total.
400 */
401 res.start = pres->start + GENET_UMAC_OFF + UMAC_MDIO_CMD;
402 res.end = res.start + 8;
403 res.flags = IORESOURCE_MEM;
404
405 if (dn)
406 id = of_alias_get_id(dn, "eth");
407 else
408 id = pdev->id;
409
410 ppdev = platform_device_alloc(UNIMAC_MDIO_DRV_NAME, id);
411 if (!ppdev)
412 return -ENOMEM;
413
414 /* Retain this platform_device pointer for later cleanup */
415 priv->mii_pdev = ppdev;
416 ppdev->dev.parent = &pdev->dev;
417 ppdev->dev.of_node = bcmgenet_mii_of_find_mdio(priv);
418 if (pdata)
419 bcmgenet_mii_pdata_init(priv, &ppd);
420
421 ret = platform_device_add_resources(ppdev, &res, 1);
422 if (ret)
423 goto out;
424
425 ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
426 if (ret)
427 goto out;
428
429 ret = platform_device_add(ppdev);
430 if (ret)
431 goto out;
432
433 return 0;
434out:
435 platform_device_put(ppdev);
436 return ret;
437}
438
439static int bcmgenet_mii_of_init(struct bcmgenet_priv *priv)
440{
441 struct device_node *dn = priv->pdev->dev.of_node;
442 struct device *kdev = &priv->pdev->dev;
443 struct phy_device *phydev;
444 int phy_mode;
445 int ret;
Florian Fainelliaa096772014-02-13 16:08:48 -0800446
447 /* Fetch the PHY phandle */
448 priv->phy_dn = of_parse_phandle(dn, "phy-handle", 0);
449
Florian Fainelli6cc8e6d2015-07-16 15:51:18 -0700450 /* In the case of a fixed PHY, the DT node associated
451 * to the PHY is the Ethernet MAC DT node.
452 */
453 if (!priv->phy_dn && of_phy_is_fixed_link(dn)) {
454 ret = of_phy_register_fixed_link(dn);
455 if (ret)
456 return ret;
457
458 priv->phy_dn = of_node_get(dn);
459 }
460
Florian Fainelliaa096772014-02-13 16:08:48 -0800461 /* Get the link mode */
Florian Fainellic624f892015-07-16 15:51:17 -0700462 phy_mode = of_get_phy_mode(dn);
Florian Fainelli40bc8b02017-06-23 10:33:15 -0700463 if (phy_mode < 0) {
464 dev_err(kdev, "invalid PHY mode property\n");
465 return phy_mode;
466 }
467
Florian Fainellic624f892015-07-16 15:51:17 -0700468 priv->phy_interface = phy_mode;
469
470 /* We need to specifically look up whether this PHY interface is internal
471 * or not *before* we even try to probe the PHY driver over MDIO as we
472 * may have shut down the internal PHY for power saving purposes.
473 */
Florian Fainelli40bc8b02017-06-23 10:33:15 -0700474 if (priv->phy_interface == PHY_INTERFACE_MODE_INTERNAL)
475 priv->internal_phy = true;
Florian Fainelliaa096772014-02-13 16:08:48 -0800476
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700477 /* Make sure we initialize MoCA PHYs with a link down */
478 if (phy_mode == PHY_INTERFACE_MODE_MOCA) {
479 phydev = of_phy_find_device(dn);
Johan Hovold0da60542016-11-24 19:21:28 +0100480 if (phydev) {
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700481 phydev->link = 0;
Johan Hovold0da60542016-11-24 19:21:28 +0100482 put_device(&phydev->mdio.dev);
483 }
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700484 }
Petri Gynther8d88c6e2015-04-01 00:40:00 -0700485
486 return 0;
487}
488
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800489static int bcmgenet_mii_pd_init(struct bcmgenet_priv *priv)
490{
491 struct device *kdev = &priv->pdev->dev;
492 struct bcmgenet_platform_data *pd = kdev->platform_data;
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700493 char phy_name[MII_BUS_ID_SIZE + 3];
494 char mdio_bus_id[MII_BUS_ID_SIZE];
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800495 struct phy_device *phydev;
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700496
497 snprintf(mdio_bus_id, MII_BUS_ID_SIZE, "%s-%d",
498 UNIMAC_MDIO_DRV_NAME, priv->pdev->id);
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800499
500 if (pd->phy_interface != PHY_INTERFACE_MODE_MOCA && pd->mdio_enabled) {
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700501 snprintf(phy_name, MII_BUS_ID_SIZE, PHY_ID_FMT,
502 mdio_bus_id, pd->phy_address);
503
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800504 /*
505 * Internal or external PHY with MDIO access
506 */
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700507 phydev = phy_attach(priv->dev, phy_name, pd->phy_interface);
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800508 if (!phydev) {
509 dev_err(kdev, "failed to register PHY device\n");
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800510 return -ENODEV;
511 }
512 } else {
513 /*
514 * MoCA port or no MDIO access.
515 * Use fixed PHY to represent the link layer.
516 */
517 struct fixed_phy_status fphy_status = {
518 .link = 1,
519 .speed = pd->phy_speed,
520 .duplex = pd->phy_duplex,
521 .pause = 0,
522 .asym_pause = 0,
523 };
524
Linus Walleij5468e822019-02-04 11:26:18 +0100525 phydev = fixed_phy_register(PHY_POLL, &fphy_status, NULL);
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800526 if (!phydev || IS_ERR(phydev)) {
527 dev_err(kdev, "failed to register fixed PHY device\n");
528 return -ENODEV;
529 }
Petri Gynther8d88c6e2015-04-01 00:40:00 -0700530
Florian Fainelli6ac9de52015-07-22 17:29:53 -0700531 /* Make sure we initialize MoCA PHYs with a link down */
532 phydev->link = 0;
533
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800534 }
535
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800536 priv->phy_interface = pd->phy_interface;
537
538 return 0;
539}
540
541static int bcmgenet_mii_bus_init(struct bcmgenet_priv *priv)
542{
543 struct device_node *dn = priv->pdev->dev.of_node;
544
545 if (dn)
546 return bcmgenet_mii_of_init(priv);
547 else
548 return bcmgenet_mii_pd_init(priv);
549}
550
Florian Fainelliaa096772014-02-13 16:08:48 -0800551int bcmgenet_mii_init(struct net_device *dev)
552{
553 struct bcmgenet_priv *priv = netdev_priv(dev);
554 int ret;
555
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700556 ret = bcmgenet_mii_register(priv);
Florian Fainelliaa096772014-02-13 16:08:48 -0800557 if (ret)
558 return ret;
559
Petri Gyntherb0ba5122014-12-01 16:18:08 -0800560 ret = bcmgenet_mii_bus_init(priv);
Florian Fainelliaa096772014-02-13 16:08:48 -0800561 if (ret)
Florian Fainelliaa096772014-02-13 16:08:48 -0800562 goto out;
563
564 return 0;
565
566out:
Florian Fainelli6f24b852017-07-31 12:04:28 -0700567 bcmgenet_mii_exit(dev);
Florian Fainelliaa096772014-02-13 16:08:48 -0800568 return ret;
569}
570
571void bcmgenet_mii_exit(struct net_device *dev)
572{
573 struct bcmgenet_priv *priv = netdev_priv(dev);
Johan Hovold140ca9d2016-11-28 19:24:59 +0100574 struct device_node *dn = priv->pdev->dev.of_node;
Florian Fainelliaa096772014-02-13 16:08:48 -0800575
Johan Hovold140ca9d2016-11-28 19:24:59 +0100576 if (of_phy_is_fixed_link(dn))
577 of_phy_deregister_fixed_link(dn);
Uwe Kleine-König95182592014-08-07 22:53:40 +0200578 of_node_put(priv->phy_dn);
Florian Fainelli9a4e7962017-07-31 12:04:26 -0700579 platform_device_unregister(priv->mii_pdev);
Florian Fainelliaa096772014-02-13 16:08:48 -0800580}