ralink: add support for mt7621 switch counters
[15.05/openwrt.git] / target / linux / ramips / files / drivers / net / ethernet / ralink / mdio.c
1 /*
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
5  *
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.
10  *
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.
14  *
15  *   Copyright (C) 2009-2013 John Crispin <blogic@openwrt.org>
16  */
17
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>
32
33 #include "ralink_soc_eth.h"
34 #include "mdio.h"
35
36 static int fe_mdio_reset(struct mii_bus *bus)
37 {
38         /* TODO */
39         return 0;
40 }
41
42 static void fe_phy_link_adjust(struct net_device *dev)
43 {
44         struct fe_priv *priv = netdev_priv(dev);
45         unsigned long flags;
46         int i;
47
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;
53
54                         if (phydev->link)
55                                 if (priv->phy->duplex[i] != phydev->duplex ||
56                                                 priv->phy->speed[i] != phydev->speed)
57                                         status_change = 1;
58
59                         if (phydev->link != priv->link[i])
60                                 status_change = 1;
61
62                         switch (phydev->speed) {
63                         case SPEED_1000:
64                         case SPEED_100:
65                         case SPEED_10:
66                                 priv->link[i] = phydev->link;
67                                 priv->phy->duplex[i] = phydev->duplex;
68                                 priv->phy->speed[i] = phydev->speed;
69
70                                 if (status_change && priv->soc->mdio_adjust_link)
71                                         priv->soc->mdio_adjust_link(priv, i);
72                                 break;
73                         }
74                 }
75         }
76 }
77
78 int fe_connect_phy_node(struct fe_priv *priv, struct device_node *phy_node)
79 {
80         const __be32 *_port = NULL;
81         struct phy_device *phydev;
82         int phy_mode, port;
83
84         _port = of_get_property(phy_node, "reg", NULL);
85
86         if (!_port || (be32_to_cpu(*_port) >= 0x20)) {
87                 pr_err("%s: invalid port id\n", phy_node->name);
88                 return -EINVAL;
89         }
90         port = be32_to_cpu(*_port);
91         phy_mode = of_get_phy_mode(phy_node);
92         if (phy_mode < 0) {
93                 dev_err(priv->device, "incorrect phy-mode %d\n", phy_mode);
94                 priv->phy->phy_node[port] = NULL;
95                 return -EINVAL;
96         }
97
98         phydev = of_phy_connect(priv->netdev, phy_node, fe_phy_link_adjust,
99                                 0, phy_mode);
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);
104         }
105
106         phydev->supported &= PHY_GBIT_FEATURES;
107         phydev->advertising = phydev->supported;
108         phydev->no_auto_carrier_off = 1;
109
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,
113                  phydev->drv->name);
114
115         priv->phy->phy[port] = phydev;
116         priv->link[port] = 0;
117
118         return 0;
119 }
120
121 static void phy_init(struct fe_priv *priv, struct phy_device *phy)
122 {
123         phy_attach(priv->netdev, dev_name(&phy->dev), PHY_INTERFACE_MODE_MII);
124
125         phy->autoneg = AUTONEG_ENABLE;
126         phy->speed = 0;
127         phy->duplex = 0;
128         phy->supported &= PHY_BASIC_FEATURES;
129         phy->advertising = phy->supported | ADVERTISED_Autoneg;
130
131         phy_start_aneg(phy);
132 }
133
134 static int fe_phy_connect(struct fe_priv *priv)
135 {
136         int i;
137
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;
143                         }
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;
149                         }
150                 }
151         }
152
153         return 0;
154 }
155
156 static void fe_phy_disconnect(struct fe_priv *priv)
157 {
158         unsigned long flags;
159         int i;
160
161         for (i = 0; i < 8; i++)
162                 if (priv->phy->phy_fixed[i]) {
163                         spin_lock_irqsave(&priv->phy->lock, flags);
164                         priv->link[i] = 0;
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]);
172                 }
173 }
174
175 static void fe_phy_start(struct fe_priv *priv)
176 {
177         unsigned long flags;
178         int i;
179
180         for (i = 0; i < 8; i++) {
181                 if (priv->phy->phy_fixed[i]) {
182                         spin_lock_irqsave(&priv->phy->lock, flags);
183                         priv->link[i] = 1;
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]);
189                 }
190         }
191 }
192
193 static void fe_phy_stop(struct fe_priv *priv)
194 {
195         unsigned long flags;
196         int i;
197
198         for (i = 0; i < 8; i++)
199                 if (priv->phy->phy_fixed[i]) {
200                         spin_lock_irqsave(&priv->phy->lock, flags);
201                         priv->link[i] = 0;
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]);
207                 }
208 }
209
210 static struct fe_phy phy_ralink = {
211         .connect = fe_phy_connect,
212         .disconnect = fe_phy_disconnect,
213         .start = fe_phy_start,
214         .stop = fe_phy_stop,
215 };
216
217 int fe_mdio_init(struct fe_priv *priv)
218 {
219         struct device_node *mii_np;
220         int err;
221
222         if (!priv->soc->mdio_read || !priv->soc->mdio_write)
223                 return 0;
224
225         spin_lock_init(&phy_ralink.lock);
226         priv->phy = &phy_ralink;
227
228         mii_np = of_get_child_by_name(priv->device->of_node, "mdio-bus");
229         if (!mii_np) {
230                 dev_err(priv->device, "no %s child node found", "mdio-bus");
231                 return -ENODEV;
232         }
233
234         if (!of_device_is_available(mii_np)) {
235                 err = 0;
236                 goto err_put_node;
237         }
238
239         priv->mii_bus = mdiobus_alloc();
240         if (priv->mii_bus == NULL) {
241                 err = -ENOMEM;
242                 goto err_put_node;
243         }
244
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;
251
252         snprintf(priv->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
253         err = of_mdiobus_register(priv->mii_bus, mii_np);
254         if (err)
255                 goto err_free_bus;
256
257         return 0;
258
259 err_free_bus:
260         kfree(priv->mii_bus);
261 err_put_node:
262         of_node_put(mii_np);
263         priv->mii_bus = NULL;
264         return err;
265 }
266
267 void fe_mdio_cleanup(struct fe_priv *priv)
268 {
269         if (!priv->mii_bus)
270                 return;
271
272         mdiobus_unregister(priv->mii_bus);
273         of_node_put(priv->mii_bus->dev.of_node);
274         kfree(priv->mii_bus);
275 }