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