2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; version 2 of the License
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
15 * Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/types.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/init.h>
23 #include <linux/skbuff.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/platform_device.h>
27 #include <linux/phy.h>
28 #include <linux/of_device.h>
29 #include <linux/clk.h>
30 #include <linux/of_net.h>
31 #include <linux/of_mdio.h>
33 #include "ralink_soc_eth.h"
36 static int fe_mdio_reset(struct mii_bus *bus)
42 static void fe_phy_link_adjust(struct net_device *dev)
44 struct fe_priv *priv = netdev_priv(dev);
48 spin_lock_irqsave(&priv->phy->lock, flags);
49 for (i = 0; i < 8; i++) {
50 if (priv->phy->phy_node[i]) {
51 struct phy_device *phydev = priv->phy->phy[i];
52 int status_change = 0;
55 if (priv->phy->duplex[i] != phydev->duplex ||
56 priv->phy->speed[i] != phydev->speed)
59 if (phydev->link != priv->link[i])
62 switch (phydev->speed) {
66 priv->link[i] = phydev->link;
67 priv->phy->duplex[i] = phydev->duplex;
68 priv->phy->speed[i] = phydev->speed;
70 if (status_change && priv->soc->mdio_adjust_link)
71 priv->soc->mdio_adjust_link(priv, i);
78 int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node)
80 const __be32 *_port = NULL;
81 struct phy_device *phydev;
84 _port = of_get_property(phy_node, "reg", NULL);
86 if (!_port || (be32_to_cpu(*_port) >= 0x20)) {
87 pr_err("%s: invalid port id\n", phy_node->name);
90 port = be32_to_cpu(*_port);
91 phy_mode = of_get_phy_mode(phy_node);
93 dev_err(priv->device, "incorrect phy-mode %d\n", phy_mode);
94 priv->phy->phy_node[port] = NULL;
98 phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
100 if (IS_ERR(phydev)) {
101 dev_err(priv->device, "could not connect to PHY\n");
102 priv->phy->phy_node[port] = NULL;
103 return PTR_ERR(phydev);
106 phydev->supported &= PHY_GBIT_FEATURES;
107 phydev->advertising = phydev->supported;
108 phydev->no_auto_carrier_off = 1;
110 dev_info(priv->device,
111 "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
112 port, dev_name(&phydev->dev), phydev->phy_id,
115 priv->phy->phy[port] = phydev;
116 priv->link[port] = 0;
121 static void phy_init(struct fe_priv *priv, struct phy_device *phy)
123 phy_attach(priv->netdev, dev_name(&phy->dev), PHY_INTERFACE_MODE_MII);
125 phy->autoneg = AUTONEG_ENABLE;
128 phy->supported &= PHY_BASIC_FEATURES;
129 phy->advertising = phy->supported | ADVERTISED_Autoneg;
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 && priv->mii_bus->phy_map[i]) {
145 phy_init(priv, priv->mii_bus->phy_map[i]);
146 if (!priv->phy_dev) {
147 priv->phy_dev = priv->mii_bus->phy_map[i];
148 priv->phy_flags = FE_PHY_FLAG_ATTACH;
156 static void fe_phy_disconnect(struct fe_priv *priv)
161 for (i = 0; i < 8; i++)
162 if (priv->phy->phy_fixed[i]) {
163 spin_lock_irqsave(&priv->phy->lock, flags);
165 if (priv->soc->mdio_adjust_link)
166 priv->soc->mdio_adjust_link(priv, i);
167 spin_unlock_irqrestore(&priv->phy->lock, flags);
168 } else if (priv->phy->phy[i]) {
169 phy_disconnect(priv->phy->phy[i]);
170 } else if (priv->mii_bus && priv->mii_bus->phy_map[i]) {
171 phy_detach(priv->mii_bus->phy_map[i]);
175 static void fe_phy_start(struct fe_priv *priv)
180 for (i = 0; i < 8; i++) {
181 if (priv->phy->phy_fixed[i]) {
182 spin_lock_irqsave(&priv->phy->lock, flags);
184 if (priv->soc->mdio_adjust_link)
185 priv->soc->mdio_adjust_link(priv, i);
186 spin_unlock_irqrestore(&priv->phy->lock, flags);
187 } else if (priv->phy->phy[i]) {
188 phy_start(priv->phy->phy[i]);
193 static void fe_phy_stop(struct fe_priv *priv)
198 for (i = 0; i < 8; i++)
199 if (priv->phy->phy_fixed[i]) {
200 spin_lock_irqsave(&priv->phy->lock, flags);
202 if (priv->soc->mdio_adjust_link)
203 priv->soc->mdio_adjust_link(priv, i);
204 spin_unlock_irqrestore(&priv->phy->lock, flags);
205 } else if (priv->phy->phy[i]) {
206 phy_stop(priv->phy->phy[i]);
210 static struct fe_phy phy_ralink = {
211 .connect = fe_phy_connect,
212 .disconnect = fe_phy_disconnect,
213 .start = fe_phy_start,
217 int fe_mdio_init(struct fe_priv *priv)
219 struct device_node *mii_np;
222 if (!priv->soc->mdio_read || !priv->soc->mdio_write)
225 spin_lock_init(&phy_ralink.lock);
226 priv->phy = &phy_ralink;
228 mii_np = of_get_child_by_name(priv->device->of_node, "mdio-bus");
230 dev_err(priv->device, "no %s child node found", "mdio-bus");
234 if (!of_device_is_available(mii_np)) {
239 priv->mii_bus = mdiobus_alloc();
240 if (priv->mii_bus == NULL) {
245 priv->mii_bus->name = "mdio";
246 priv->mii_bus->read = priv->soc->mdio_read;
247 priv->mii_bus->write = priv->soc->mdio_write;
248 priv->mii_bus->reset = fe_mdio_reset;
249 priv->mii_bus->priv = priv;
250 priv->mii_bus->parent = priv->device;
252 snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
253 err = of_mdiobus_register(priv->mii_bus, mii_np);
260 kfree(priv->mii_bus);
263 priv->mii_bus = NULL;
267 void fe_mdio_cleanup(struct fe_priv *priv)
272 mdiobus_unregister(priv->mii_bus);
273 of_node_put(priv->mii_bus->dev.of_node);
274 kfree(priv->mii_bus);