1 /* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation; version 2 of the License
5 * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
6 * Copyright (C) 2009-2015 Felix Fietkau <nbd@nbd.name>
7 * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/phy.h>
13 #include <linux/of_net.h>
14 #include <linux/of_mdio.h>
16 #include "mtk_eth_soc.h"
19 static int fe_mdio_reset(struct mii_bus *bus)
25 static void fe_phy_link_adjust(struct net_device *dev)
27 struct fe_priv *priv = netdev_priv(dev);
31 spin_lock_irqsave(&priv->phy->lock, flags);
32 for (i = 0; i < 8; i++) {
33 if (priv->phy->phy_node[i]) {
34 struct phy_device *phydev = priv->phy->phy[i];
35 int status_change = 0;
38 if (priv->phy->duplex[i] != phydev->duplex ||
39 priv->phy->speed[i] != phydev->speed)
42 if (phydev->link != priv->link[i])
45 switch (phydev->speed) {
49 priv->link[i] = phydev->link;
50 priv->phy->duplex[i] = phydev->duplex;
51 priv->phy->speed[i] = phydev->speed;
54 priv->soc->mdio_adjust_link)
55 priv->soc->mdio_adjust_link(priv, i);
60 spin_unlock_irqrestore(&priv->phy->lock, flags);
63 int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node, int port)
65 const __be32 *_phy_addr = NULL;
66 struct phy_device *phydev;
69 _phy_addr = of_get_property(phy_node, "reg", NULL);
71 if (!_phy_addr || (be32_to_cpu(*_phy_addr) >= 0x20)) {
72 pr_err("%s: invalid phy id\n", phy_node->name);
76 phy_mode = of_get_phy_mode(phy_node);
78 dev_err(priv->dev, "incorrect phy-mode %d\n", phy_mode);
79 priv->phy->phy_node[port] = NULL;
83 phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
86 dev_err(priv->dev, "could not connect to PHY\n");
87 priv->phy->phy_node[port] = NULL;
91 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0)
92 phydev->supported &= PHY_GBIT_FEATURES;
93 phydev->advertising = phydev->supported;
95 phy_set_max_speed(phydev, SPEED_1000);
96 linkmode_copy(phydev->advertising, phydev->supported);
98 phydev->no_auto_carrier_off = 1;
101 "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
102 port, dev_name(&phydev->mdio.dev), phydev->phy_id,
105 priv->phy->phy[port] = phydev;
106 priv->link[port] = 0;
111 static void phy_init(struct fe_priv *priv, struct phy_device *phy)
113 phy_attach(priv->netdev, dev_name(&phy->mdio.dev), PHY_INTERFACE_MODE_MII);
115 phy->autoneg = AUTONEG_ENABLE;
119 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 4, 0)
120 phy->supported &= IS_ENABLED(CONFIG_NET_RALINK_MDIO_MT7620) ?
121 PHY_GBIT_FEATURES : PHY_BASIC_FEATURES;
122 phy->advertising = phy->supported | ADVERTISED_Autoneg;
124 phy_set_max_speed(phy, IS_ENABLED(CONFIG_NET_RALINK_MDIO_MT7620) ?
127 linkmode_copy(phy->advertising, phy->supported);
128 linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, phy->advertising);
134 static int fe_phy_connect(struct fe_priv *priv)
138 for (i = 0; i < 8; i++) {
139 if (priv->phy->phy_node[i]) {
140 if (!priv->phy_dev) {
141 priv->phy_dev = priv->phy->phy[i];
142 priv->phy_flags = FE_PHY_FLAG_PORT;
144 } else if (priv->mii_bus) {
145 struct phy_device *phydev;
147 phydev = mdiobus_get_phy(priv->mii_bus, i);
148 if (!phydev || phydev->attached_dev)
151 phy_init(priv, phydev);
152 if (!priv->phy_dev) {
153 priv->phy_dev = mdiobus_get_phy(priv->mii_bus, i);
154 priv->phy_flags = FE_PHY_FLAG_ATTACH;
162 static void fe_phy_disconnect(struct fe_priv *priv)
167 for (i = 0; i < 8; i++)
168 if (priv->phy->phy_fixed[i]) {
169 spin_lock_irqsave(&priv->phy->lock, flags);
171 if (priv->soc->mdio_adjust_link)
172 priv->soc->mdio_adjust_link(priv, i);
173 spin_unlock_irqrestore(&priv->phy->lock, flags);
174 } else if (priv->phy->phy[i]) {
175 phy_disconnect(priv->phy->phy[i]);
176 } else if (priv->mii_bus && mdiobus_get_phy(priv->mii_bus, i)) {
177 phy_detach(mdiobus_get_phy(priv->mii_bus, i));
181 static void fe_phy_start(struct fe_priv *priv)
186 for (i = 0; i < 8; i++) {
187 if (priv->phy->phy_fixed[i]) {
188 spin_lock_irqsave(&priv->phy->lock, flags);
190 if (priv->soc->mdio_adjust_link)
191 priv->soc->mdio_adjust_link(priv, i);
192 spin_unlock_irqrestore(&priv->phy->lock, flags);
193 } else if (priv->phy->phy[i]) {
194 phy_start(priv->phy->phy[i]);
199 static void fe_phy_stop(struct fe_priv *priv)
204 for (i = 0; i < 8; i++)
205 if (priv->phy->phy_fixed[i]) {
206 spin_lock_irqsave(&priv->phy->lock, flags);
208 if (priv->soc->mdio_adjust_link)
209 priv->soc->mdio_adjust_link(priv, i);
210 spin_unlock_irqrestore(&priv->phy->lock, flags);
211 } else if (priv->phy->phy[i]) {
212 phy_stop(priv->phy->phy[i]);
216 static struct fe_phy phy_ralink = {
217 .connect = fe_phy_connect,
218 .disconnect = fe_phy_disconnect,
219 .start = fe_phy_start,
223 int fe_mdio_init(struct fe_priv *priv)
225 struct device_node *mii_np;
228 if (!priv->soc->mdio_read || !priv->soc->mdio_write)
231 spin_lock_init(&phy_ralink.lock);
232 priv->phy = &phy_ralink;
234 mii_np = of_get_child_by_name(priv->dev->of_node, "mdio-bus");
236 dev_err(priv->dev, "no %s child node found", "mdio-bus");
240 if (!of_device_is_available(mii_np)) {
245 priv->mii_bus = mdiobus_alloc();
246 if (!priv->mii_bus) {
251 priv->mii_bus->name = "mdio";
252 priv->mii_bus->read = priv->soc->mdio_read;
253 priv->mii_bus->write = priv->soc->mdio_write;
254 priv->mii_bus->reset = fe_mdio_reset;
255 priv->mii_bus->priv = priv;
256 priv->mii_bus->parent = priv->dev;
258 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
259 err = of_mdiobus_register(priv->mii_bus, mii_np);
266 kfree(priv->mii_bus);
269 priv->mii_bus = NULL;
273 void fe_mdio_cleanup(struct fe_priv *priv)
278 mdiobus_unregister(priv->mii_bus);
279 of_node_put(priv->mii_bus->dev.of_node);
280 kfree(priv->mii_bus);