apm821xx: add support for the Netgear Centria N900 WNDR4700/WNDR4720
[oweals/openwrt.git] / target / linux / apm821xx / patches-4.4 / 702-powerpc_ibm_phy_add_dt_parser.patch
1 From 59b394d0d2b2e11687e757820c52d6470d15a9c5 Mon Sep 17 00:00:00 2001
2 From: Christian Lamparter <chunkeey@gmail.com>
3 Date: Mon, 13 Jun 2016 15:42:21 +0200
4 Subject: [PATCH] phy device tree support for emac
5
6 ---
7  drivers/net/ethernet/ibm/emac/core.c | 261 +++++++++++++++++++++++++++++++++++
8  drivers/net/ethernet/ibm/emac/core.h |   4 +
9  2 files changed, 265 insertions(+)
10
11 diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
12 index 4c9771d..5a8a26c 100644
13 --- a/drivers/net/ethernet/ibm/emac/core.c
14 +++ b/drivers/net/ethernet/ibm/emac/core.c
15 @@ -42,6 +42,7 @@
16  #include <linux/of_address.h>
17  #include <linux/of_irq.h>
18  #include <linux/of_net.h>
19 +#include <linux/of_mdio.h>
20  #include <linux/slab.h>
21  
22  #include <asm/processor.h>
23 @@ -2383,6 +2384,246 @@ static int emac_read_uint_prop(struct device_node *np, const char *name,
24         return 0;
25  }
26  
27 +static void emac_adjust_link(struct net_device *ndev)
28 +{
29 +       struct emac_instance *dev = netdev_priv(ndev);
30 +       struct phy_device *phy = dev->phy_dev;
31 +
32 +       dev->phy.autoneg = phy->autoneg;
33 +       dev->phy.speed = phy->speed;
34 +       dev->phy.duplex = phy->duplex;
35 +       dev->phy.pause = phy->pause;
36 +       dev->phy.asym_pause = phy->asym_pause;
37 +       dev->phy.advertising = phy->advertising;
38 +}
39 +
40 +static int emac_mii_bus_read(struct mii_bus *bus, int addr, int regnum)
41 +{
42 +       return emac_mdio_read(bus->priv, addr, regnum);
43 +}
44 +
45 +static int emac_mii_bus_write(struct mii_bus *bus, int addr, int regnum, u16 val)
46 +{
47 +       emac_mdio_write(bus->priv, addr, regnum, val);
48 +       return 0;
49 +}
50 +
51 +static int emac_mii_bus_reset(struct mii_bus *bus)
52 +{
53 +       struct emac_instance *dev = netdev_priv(bus->priv);
54 +
55 +       emac_mii_reset_phy(&dev->phy);
56 +       return 0;
57 +}
58 +
59 +static int emac_mdio_setup_aneg(struct mii_phy *phy, u32 advertise)
60 +{
61 +       struct net_device *ndev = phy->dev;
62 +       struct emac_instance *dev = netdev_priv(ndev);
63 +
64 +       dev->phy.autoneg = AUTONEG_ENABLE;
65 +       dev->phy.speed = SPEED_1000;
66 +       dev->phy.duplex = DUPLEX_FULL;
67 +       dev->phy.advertising = advertise;
68 +       phy->autoneg = AUTONEG_ENABLE;
69 +       phy->speed = dev->phy.speed;
70 +       phy->duplex = dev->phy.duplex;
71 +       phy->advertising = advertise;
72 +       return phy_start_aneg(dev->phy_dev);
73 +}
74 +
75 +static int emac_mdio_setup_forced(struct mii_phy *phy, int speed, int fd)
76 +{
77 +       struct net_device *ndev = phy->dev;
78 +       struct emac_instance *dev = netdev_priv(ndev);
79 +
80 +       dev->phy.autoneg =  AUTONEG_DISABLE;
81 +       dev->phy.speed = speed;
82 +       dev->phy.duplex = fd;
83 +       phy->autoneg = AUTONEG_DISABLE;
84 +       phy->speed = speed;
85 +       phy->duplex = fd;
86 +       return phy_start_aneg(dev->phy_dev);
87 +}
88 +
89 +static int emac_mdio_poll_link(struct mii_phy *phy)
90 +{
91 +       struct net_device *ndev = phy->dev;
92 +       struct emac_instance *dev = netdev_priv(ndev);
93 +       int res;
94 +
95 +       res = phy_read_status(dev->phy_dev);
96 +       if (res) {
97 +               dev_err(&dev->ndev->dev, "link update failed (%d).", res);
98 +               return ethtool_op_get_link(ndev);
99 +       }
100 +
101 +       return dev->phy_dev->link;
102 +}
103 +
104 +static int emac_mdio_read_link(struct mii_phy *phy)
105 +{
106 +       struct net_device *ndev = phy->dev;
107 +       struct emac_instance *dev = netdev_priv(ndev);
108 +       int res;
109 +
110 +       res = phy_read_status(dev->phy_dev);
111 +       if (res)
112 +               return res;
113 +
114 +       dev->phy.speed = phy->speed;
115 +       dev->phy.duplex = phy->duplex;
116 +       dev->phy.pause = phy->pause;
117 +       dev->phy.asym_pause = phy->asym_pause;
118 +       return 0;
119 +}
120 +
121 +static int emac_mdio_init_phy(struct mii_phy *phy)
122 +{
123 +       struct net_device *ndev = phy->dev;
124 +       struct emac_instance *dev = netdev_priv(ndev);
125 +
126 +       phy_start(dev->phy_dev);
127 +       dev->phy.autoneg = phy->autoneg;
128 +       dev->phy.speed = phy->speed;
129 +       dev->phy.duplex = phy->duplex;
130 +       dev->phy.advertising = phy->advertising;
131 +       dev->phy.pause = phy->pause;
132 +       dev->phy.asym_pause = phy->asym_pause;
133 +
134 +       return phy_init_hw(dev->phy_dev);
135 +}
136 +
137 +static const struct mii_phy_ops emac_dt_mdio_phy_ops = {
138 +       .init           = emac_mdio_init_phy,
139 +       .setup_aneg     = emac_mdio_setup_aneg,
140 +       .setup_forced   = emac_mdio_setup_forced,
141 +       .poll_link      = emac_mdio_poll_link,
142 +       .read_link      = emac_mdio_read_link,
143 +};
144 +
145 +static void emac_dt_phy_mdio_cleanup(struct emac_instance *dev)
146 +{
147 +       if (dev->mii_bus) {
148 +               if (dev->mii_bus->state == MDIOBUS_REGISTERED)
149 +                       mdiobus_unregister(dev->mii_bus);
150 +               mdiobus_free(dev->mii_bus);
151 +               dev->mii_bus = NULL;
152 +       }
153 +       kfree(dev->phy.def);
154 +       dev->phy.def = NULL;
155 +}
156 +
157 +static int emac_dt_mdio_probe(struct emac_instance *dev)
158 +{
159 +       struct device_node *mii_np;
160 +       int res;
161 +
162 +       mii_np = of_get_child_by_name(dev->ofdev->dev.of_node, "mdio");
163 +       if (!mii_np) {
164 +               dev_err(&dev->ndev->dev, "no mdio definition found.");
165 +               return -ENODEV;
166 +       }
167 +
168 +       if (!of_device_is_available(mii_np)) {
169 +               res = 1;
170 +               goto err_put_node;
171 +       }
172 +
173 +       dev->mii_bus = mdiobus_alloc();
174 +       if (!dev->mii_bus) {
175 +               res = -ENOMEM;
176 +               goto err_cleanup_mdio;
177 +       }
178 +
179 +       dev->mii_bus->priv = dev->ndev;
180 +       dev->mii_bus->parent = dev->ndev->dev.parent;
181 +       dev->mii_bus->name = "emac_mdio";
182 +       dev->mii_bus->read = &emac_mii_bus_read;
183 +       dev->mii_bus->write = &emac_mii_bus_write;
184 +       dev->mii_bus->reset = &emac_mii_bus_reset;
185 +       snprintf(dev->mii_bus->id, MII_BUS_ID_SIZE, "%s", dev->mii_bus->name);
186 +
187 +       res = of_mdiobus_register(dev->mii_bus, mii_np);
188 +       if (res) {
189 +               dev_err(&dev->ndev->dev, "cannot register MDIO bus %s (%d)",
190 +                       dev->mii_bus->name, res);
191 +               goto err_cleanup_mdio;
192 +       }
193 +       of_node_put(mii_np);
194 +       return 0;
195 +
196 + err_cleanup_mdio:
197 +       emac_dt_phy_mdio_cleanup(dev);
198 + err_put_node:
199 +       of_node_put(mii_np);
200 +       return res;
201 +}
202 +
203 +static int emac_dt_phy_probe(struct emac_instance *dev,
204 +                            struct device_node *phy_handle)
205 +{
206 +       u32 phy_flags = 0;
207 +       int res;
208 +
209 +       res = of_property_read_u32(phy_handle, "phy-flags", &phy_flags);
210 +       if (res < 0 && res != -EINVAL)
211 +               return res;
212 +
213 +       dev->phy.def = kzalloc(sizeof(*dev->phy.def), GFP_KERNEL);
214 +       if (!dev->phy.def)
215 +               return -ENOMEM;
216 +
217 +       dev->phy_dev = of_phy_connect(dev->ndev, phy_handle,
218 +                                     &emac_adjust_link, phy_flags,
219 +                                     PHY_INTERFACE_MODE_RGMII);
220 +       if (!dev->phy_dev) {
221 +               dev_err(&dev->ndev->dev, "failed to connect to PHY.");
222 +               kfree(dev->phy.dev);
223 +               dev->phy.dev = NULL;
224 +               return -ENODEV;
225 +       }
226 +
227 +       dev->phy.def->phy_id = dev->phy_dev->drv->phy_id;
228 +       dev->phy.def->phy_id_mask = dev->phy_dev->drv->phy_id_mask;
229 +       dev->phy.def->name = dev->phy_dev->drv->name;
230 +       dev->phy.def->ops = &emac_dt_mdio_phy_ops;
231 +       dev->phy.features = dev->phy_dev->supported;
232 +       dev->phy.address = dev->phy_dev->addr;
233 +       dev->phy.mode = dev->phy_dev->interface;
234 +       return 0;
235 +}
236 +
237 +static int emac_probe_dt_phy(struct emac_instance *dev)
238 +{
239 +       struct device_node *np = dev->ofdev->dev.of_node;
240 +       struct device_node *phy_handle;
241 +       int res = 0;
242 +
243 +       phy_handle = of_parse_phandle(np, "phy-handle", 0);
244 +
245 +       if (phy_handle) {
246 +               res = emac_dt_mdio_probe(dev);
247 +               if (res)
248 +                       goto out;
249 +
250 +               res = emac_dt_phy_probe(dev, phy_handle);
251 +               if (res) {
252 +                       emac_dt_phy_mdio_cleanup(dev);
253 +                       goto out;
254 +               }
255 +
256 +               return 1;
257 +       }
258 +
259 + out:
260 +       of_node_put(phy_handle);
261 +       /* if no phy device was specifie in the device tree, then we fallback
262 +        * to the old emac_phy.c probe code for compatibility reasons.
263 +        */
264 +       return res;
265 +}
266 +
267  static int emac_init_phy(struct emac_instance *dev)
268  {
269         struct device_node *np = dev->ofdev->dev.of_node;
270 @@ -2453,6 +2694,18 @@ static int emac_init_phy(struct emac_instance *dev)
271  
272         emac_configure(dev);
273  
274 +       if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
275 +               int res = emac_probe_dt_phy(dev);
276 +
277 +               if (res == 1)
278 +                       goto init_phy;
279 +               if (res < 0)
280 +                       dev_err(&dev->ndev->dev, "failed to attach dt phy (%d).",
281 +                               res);
282 +
283 +               /* continue with old code */
284 +       }
285 +
286         if (dev->phy_address != 0xffffffff)
287                 phy_map = ~(1 << dev->phy_address);
288  
289 @@ -2480,6 +2733,7 @@ static int emac_init_phy(struct emac_instance *dev)
290                 return -ENXIO;
291         }
292  
293 + init_phy:
294         /* Init PHY */
295         if (dev->phy.def->ops->init)
296                 dev->phy.def->ops->init(&dev->phy);
297 @@ -2898,6 +3152,8 @@ static int emac_probe(struct platform_device *ofdev)
298         /* I have a bad feeling about this ... */
299  
300   err_detach_tah:
301 +       emac_dt_phy_mdio_cleanup(dev);
302 +
303         if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
304                 tah_detach(dev->tah_dev, dev->tah_port);
305   err_detach_rgmii:
306 @@ -2948,6 +3204,11 @@ static int emac_remove(struct platform_device *ofdev)
307         if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
308                 zmii_detach(dev->zmii_dev, dev->zmii_port);
309  
310 +       if (dev->phy_dev)
311 +               phy_disconnect(dev->phy_dev);
312 +
313 +       emac_dt_phy_mdio_cleanup(dev);
314 +
315         busy_phy_map &= ~(1 << dev->phy.address);
316         DBG(dev, "busy_phy_map now %#x" NL, busy_phy_map);
317  
318 diff --git a/drivers/net/ethernet/ibm/emac/core.h b/drivers/net/ethernet/ibm/emac/core.h
319 index 93ae114..0710a66 100644
320 --- a/drivers/net/ethernet/ibm/emac/core.h
321 +++ b/drivers/net/ethernet/ibm/emac/core.h
322 @@ -199,6 +199,10 @@ struct emac_instance {
323         struct emac_instance            *mdio_instance;
324         struct mutex                    mdio_lock;
325  
326 +       /* Device-tree based phy configuration */
327 +       struct mii_bus                  *mii_bus;
328 +       struct phy_device               *phy_dev;
329 +
330         /* ZMII infos if any */
331         u32                             zmii_ph;
332         u32                             zmii_port;
333 -- 
334 2.1.4
335