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-2016 John Crispin <blogic@openwrt.org>
6 * Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org>
7 * Copyright (C) 2013-2016 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 mtk_mdio_reset(struct mii_bus *bus)
25 static void mtk_phy_link_adjust(struct net_device *dev)
27 struct mtk_eth *eth = netdev_priv(dev);
31 spin_lock_irqsave(ð->phy->lock, flags);
32 for (i = 0; i < 8; i++) {
33 if (eth->phy->phy_node[i]) {
34 struct phy_device *phydev = eth->phy->phy[i];
35 int status_change = 0;
38 if (eth->phy->duplex[i] != phydev->duplex ||
39 eth->phy->speed[i] != phydev->speed)
42 if (phydev->link != eth->link[i])
45 switch (phydev->speed) {
49 eth->link[i] = phydev->link;
50 eth->phy->duplex[i] = phydev->duplex;
51 eth->phy->speed[i] = phydev->speed;
54 eth->soc->mdio_adjust_link)
55 eth->soc->mdio_adjust_link(eth, i);
62 int mtk_connect_phy_node(struct mtk_eth *eth, struct mtk_mac *mac,
63 struct device_node *phy_node)
65 const __be32 *_port = NULL;
66 struct phy_device *phydev;
69 _port = of_get_property(phy_node, "reg", NULL);
71 if (!_port || (be32_to_cpu(*_port) >= 0x20)) {
72 pr_err("%s: invalid port id\n", phy_node->name);
75 port = be32_to_cpu(*_port);
76 phy_mode = of_get_phy_mode(phy_node);
78 dev_err(eth->dev, "incorrect phy-mode %d\n", phy_mode);
79 eth->phy->phy_node[port] = NULL;
83 phydev = of_phy_connect(eth->netdev[mac->id], phy_node,
84 mtk_phy_link_adjust, 0, phy_mode);
86 dev_err(eth->dev, "could not connect to PHY\n");
87 eth->phy->phy_node[port] = NULL;
91 phydev->supported &= PHY_GBIT_FEATURES;
92 phydev->advertising = phydev->supported;
95 "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
96 port, phydev_name(phydev), phydev->phy_id,
99 eth->phy->phy[port] = phydev;
105 static void phy_init(struct mtk_eth *eth, struct mtk_mac *mac,
106 struct phy_device *phy)
108 phy_attach(eth->netdev[mac->id], phydev_name(phy),
109 PHY_INTERFACE_MODE_MII);
111 phy->autoneg = AUTONEG_ENABLE;
114 phy->supported &= PHY_BASIC_FEATURES;
115 phy->advertising = phy->supported | ADVERTISED_Autoneg;
120 static int mtk_phy_connect(struct mtk_mac *mac)
122 struct mtk_eth *eth = mac->hw;
125 for (i = 0; i < 8; i++) {
126 if (eth->phy->phy_node[i]) {
128 mac->phy_dev = eth->phy->phy[i];
129 mac->phy_flags = MTK_PHY_FLAG_PORT;
131 } else if (eth->mii_bus) {
132 struct phy_device *phy;
133 phy = mdiobus_get_phy(eth->mii_bus, i);
135 phy_init(eth, mac, phy);
138 mac->phy_flags = MTK_PHY_FLAG_ATTACH;
147 static void mtk_phy_disconnect(struct mtk_mac *mac)
149 struct mtk_eth *eth = mac->hw;
153 for (i = 0; i < 8; i++)
154 if (eth->phy->phy_fixed[i]) {
155 spin_lock_irqsave(ð->phy->lock, flags);
157 if (eth->soc->mdio_adjust_link)
158 eth->soc->mdio_adjust_link(eth, i);
159 spin_unlock_irqrestore(ð->phy->lock, flags);
160 } else if (eth->phy->phy[i]) {
161 phy_disconnect(eth->phy->phy[i]);
162 } else if (eth->mii_bus) {
163 struct phy_device *phy = mdiobus_get_phy(eth->mii_bus, i);
169 static void mtk_phy_start(struct mtk_mac *mac)
171 struct mtk_eth *eth = mac->hw;
175 for (i = 0; i < 8; i++) {
176 if (eth->phy->phy_fixed[i]) {
177 spin_lock_irqsave(ð->phy->lock, flags);
179 if (eth->soc->mdio_adjust_link)
180 eth->soc->mdio_adjust_link(eth, i);
181 spin_unlock_irqrestore(ð->phy->lock, flags);
182 } else if (eth->phy->phy[i]) {
183 phy_start(eth->phy->phy[i]);
188 static void mtk_phy_stop(struct mtk_mac *mac)
190 struct mtk_eth *eth = mac->hw;
194 for (i = 0; i < 8; i++)
195 if (eth->phy->phy_fixed[i]) {
196 spin_lock_irqsave(ð->phy->lock, flags);
198 if (eth->soc->mdio_adjust_link)
199 eth->soc->mdio_adjust_link(eth, i);
200 spin_unlock_irqrestore(ð->phy->lock, flags);
201 } else if (eth->phy->phy[i]) {
202 phy_stop(eth->phy->phy[i]);
206 static struct mtk_phy phy_ralink = {
207 .connect = mtk_phy_connect,
208 .disconnect = mtk_phy_disconnect,
209 .start = mtk_phy_start,
210 .stop = mtk_phy_stop,
213 int mtk_mdio_init(struct mtk_eth *eth)
215 struct device_node *mii_np;
218 if (!eth->soc->mdio_read || !eth->soc->mdio_write)
221 spin_lock_init(&phy_ralink.lock);
222 eth->phy = &phy_ralink;
224 mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus");
226 dev_err(eth->dev, "no %s child node found", "mdio-bus");
230 if (!of_device_is_available(mii_np)) {
235 eth->mii_bus = mdiobus_alloc();
241 eth->mii_bus->name = "mdio";
242 eth->mii_bus->read = eth->soc->mdio_read;
243 eth->mii_bus->write = eth->soc->mdio_write;
244 eth->mii_bus->reset = mtk_mdio_reset;
245 eth->mii_bus->priv = eth;
246 eth->mii_bus->parent = eth->dev;
248 snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
249 err = of_mdiobus_register(eth->mii_bus, mii_np);
263 void mtk_mdio_cleanup(struct mtk_eth *eth)
268 mdiobus_unregister(eth->mii_bus);
269 of_node_put(eth->mii_bus->dev.of_node);