Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[platform/kernel/linux-rpi.git] / drivers / staging / mt7621-eth / mdio.c
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
4  *
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>
8  */
9
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>
15
16 #include "mtk_eth_soc.h"
17 #include "mdio.h"
18
19 static int mtk_mdio_reset(struct mii_bus *bus)
20 {
21         /* TODO */
22         return 0;
23 }
24
25 static void mtk_phy_link_adjust(struct net_device *dev)
26 {
27         struct mtk_eth *eth = netdev_priv(dev);
28         unsigned long flags;
29         int i;
30
31         spin_lock_irqsave(&eth->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;
36
37                         if (phydev->link)
38                                 if (eth->phy->duplex[i] != phydev->duplex ||
39                                     eth->phy->speed[i] != phydev->speed)
40                                         status_change = 1;
41
42                         if (phydev->link != eth->link[i])
43                                 status_change = 1;
44
45                         switch (phydev->speed) {
46                         case SPEED_1000:
47                         case SPEED_100:
48                         case SPEED_10:
49                                 eth->link[i] = phydev->link;
50                                 eth->phy->duplex[i] = phydev->duplex;
51                                 eth->phy->speed[i] = phydev->speed;
52
53                                 if (status_change &&
54                                     eth->soc->mdio_adjust_link)
55                                         eth->soc->mdio_adjust_link(eth, i);
56                                 break;
57                         }
58                 }
59         }
60 }
61
62 int mtk_connect_phy_node(struct mtk_eth *eth, struct mtk_mac *mac,
63                          struct device_node *phy_node)
64 {
65         const __be32 *_port = NULL;
66         struct phy_device *phydev;
67         int phy_mode, port;
68
69         _port = of_get_property(phy_node, "reg", NULL);
70
71         if (!_port || (be32_to_cpu(*_port) >= 0x20)) {
72                 pr_err("%s: invalid port id\n", phy_node->name);
73                 return -EINVAL;
74         }
75         port = be32_to_cpu(*_port);
76         phy_mode = of_get_phy_mode(phy_node);
77         if (phy_mode < 0) {
78                 dev_err(eth->dev, "incorrect phy-mode %d\n", phy_mode);
79                 eth->phy->phy_node[port] = NULL;
80                 return -EINVAL;
81         }
82
83         phydev = of_phy_connect(eth->netdev[mac->id], phy_node,
84                                 mtk_phy_link_adjust, 0, phy_mode);
85         if (!phydev) {
86                 dev_err(eth->dev, "could not connect to PHY\n");
87                 eth->phy->phy_node[port] = NULL;
88                 return -ENODEV;
89         }
90
91         phydev->supported &= PHY_GBIT_FEATURES;
92         phydev->advertising = phydev->supported;
93
94         dev_info(eth->dev,
95                  "connected port %d to PHY at %s [uid=%08x, driver=%s]\n",
96                  port, phydev_name(phydev), phydev->phy_id,
97                  phydev->drv->name);
98
99         eth->phy->phy[port] = phydev;
100         eth->link[port] = 0;
101
102         return 0;
103 }
104
105 static void phy_init(struct mtk_eth *eth, struct mtk_mac *mac,
106                      struct phy_device *phy)
107 {
108         phy_attach(eth->netdev[mac->id], phydev_name(phy),
109                    PHY_INTERFACE_MODE_MII);
110
111         phy->autoneg = AUTONEG_ENABLE;
112         phy->speed = 0;
113         phy->duplex = 0;
114         phy->supported &= PHY_BASIC_FEATURES;
115         phy->advertising = phy->supported | ADVERTISED_Autoneg;
116
117         phy_start_aneg(phy);
118 }
119
120 static int mtk_phy_connect(struct mtk_mac *mac)
121 {
122         struct mtk_eth *eth = mac->hw;
123         int i;
124
125         for (i = 0; i < 8; i++) {
126                 if (eth->phy->phy_node[i]) {
127                         if (!mac->phy_dev) {
128                                 mac->phy_dev = eth->phy->phy[i];
129                                 mac->phy_flags = MTK_PHY_FLAG_PORT;
130                         }
131                 } else if (eth->mii_bus) {
132                         struct phy_device *phy;
133                         phy = mdiobus_get_phy(eth->mii_bus, i);
134                         if (phy) {
135                                 phy_init(eth, mac, phy);
136                                 if (!mac->phy_dev) {
137                                         mac->phy_dev = phy;
138                                         mac->phy_flags = MTK_PHY_FLAG_ATTACH;
139                                 }
140                         }
141                 }
142         }
143
144         return 0;
145 }
146
147 static void mtk_phy_disconnect(struct mtk_mac *mac)
148 {
149         struct mtk_eth *eth = mac->hw;
150         unsigned long flags;
151         int i;
152
153         for (i = 0; i < 8; i++)
154                 if (eth->phy->phy_fixed[i]) {
155                         spin_lock_irqsave(&eth->phy->lock, flags);
156                         eth->link[i] = 0;
157                         if (eth->soc->mdio_adjust_link)
158                                 eth->soc->mdio_adjust_link(eth, i);
159                         spin_unlock_irqrestore(&eth->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);
164                         if (phy)
165                                 phy_detach(phy);
166                 }
167 }
168
169 static void mtk_phy_start(struct mtk_mac *mac)
170 {
171         struct mtk_eth *eth = mac->hw;
172         unsigned long flags;
173         int i;
174
175         for (i = 0; i < 8; i++) {
176                 if (eth->phy->phy_fixed[i]) {
177                         spin_lock_irqsave(&eth->phy->lock, flags);
178                         eth->link[i] = 1;
179                         if (eth->soc->mdio_adjust_link)
180                                 eth->soc->mdio_adjust_link(eth, i);
181                         spin_unlock_irqrestore(&eth->phy->lock, flags);
182                 } else if (eth->phy->phy[i]) {
183                         phy_start(eth->phy->phy[i]);
184                 }
185         }
186 }
187
188 static void mtk_phy_stop(struct mtk_mac *mac)
189 {
190         struct mtk_eth *eth = mac->hw;
191         unsigned long flags;
192         int i;
193
194         for (i = 0; i < 8; i++)
195                 if (eth->phy->phy_fixed[i]) {
196                         spin_lock_irqsave(&eth->phy->lock, flags);
197                         eth->link[i] = 0;
198                         if (eth->soc->mdio_adjust_link)
199                                 eth->soc->mdio_adjust_link(eth, i);
200                         spin_unlock_irqrestore(&eth->phy->lock, flags);
201                 } else if (eth->phy->phy[i]) {
202                         phy_stop(eth->phy->phy[i]);
203                 }
204 }
205
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,
211 };
212
213 int mtk_mdio_init(struct mtk_eth *eth)
214 {
215         struct device_node *mii_np;
216         int err;
217
218         if (!eth->soc->mdio_read || !eth->soc->mdio_write)
219                 return 0;
220
221         spin_lock_init(&phy_ralink.lock);
222         eth->phy = &phy_ralink;
223
224         mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus");
225         if (!mii_np) {
226                 dev_err(eth->dev, "no %s child node found", "mdio-bus");
227                 return -ENODEV;
228         }
229
230         if (!of_device_is_available(mii_np)) {
231                 err = 0;
232                 goto err_put_node;
233         }
234
235         eth->mii_bus = mdiobus_alloc();
236         if (!eth->mii_bus) {
237                 err = -ENOMEM;
238                 goto err_put_node;
239         }
240
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;
247
248         snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
249         err = of_mdiobus_register(eth->mii_bus, mii_np);
250         if (err)
251                 goto err_free_bus;
252
253         return 0;
254
255 err_free_bus:
256         kfree(eth->mii_bus);
257 err_put_node:
258         of_node_put(mii_np);
259         eth->mii_bus = NULL;
260         return err;
261 }
262
263 void mtk_mdio_cleanup(struct mtk_eth *eth)
264 {
265         if (!eth->mii_bus)
266                 return;
267
268         mdiobus_unregister(eth->mii_bus);
269         of_node_put(eth->mii_bus->dev.of_node);
270         kfree(eth->mii_bus);
271 }