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
7 drivers/net/ethernet/ibm/emac/core.c | 261 +++++++++++++++++++++++++++++++++++
8 drivers/net/ethernet/ibm/emac/core.h | 4 +
9 2 files changed, 265 insertions(+)
11 --- a/drivers/net/ethernet/ibm/emac/core.c
12 +++ b/drivers/net/ethernet/ibm/emac/core.c
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>
20 #include <asm/processor.h>
21 @@ -2392,6 +2393,246 @@ static int emac_read_uint_prop(struct de
25 +static void emac_adjust_link(struct net_device *ndev)
27 + struct emac_instance *dev = netdev_priv(ndev);
28 + struct phy_device *phy = dev->phy_dev;
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;
38 +static int emac_mii_bus_read(struct mii_bus *bus, int addr, int regnum)
40 + return emac_mdio_read(bus->priv, addr, regnum);
43 +static int emac_mii_bus_write(struct mii_bus *bus, int addr, int regnum, u16 val)
45 + emac_mdio_write(bus->priv, addr, regnum, val);
49 +static int emac_mii_bus_reset(struct mii_bus *bus)
51 + struct emac_instance *dev = netdev_priv(bus->priv);
53 + emac_mii_reset_phy(&dev->phy);
57 +static int emac_mdio_setup_aneg(struct mii_phy *phy, u32 advertise)
59 + struct net_device *ndev = phy->dev;
60 + struct emac_instance *dev = netdev_priv(ndev);
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);
73 +static int emac_mdio_setup_forced(struct mii_phy *phy, int speed, int fd)
75 + struct net_device *ndev = phy->dev;
76 + struct emac_instance *dev = netdev_priv(ndev);
78 + dev->phy.autoneg = AUTONEG_DISABLE;
79 + dev->phy.speed = speed;
80 + dev->phy.duplex = fd;
81 + phy->autoneg = AUTONEG_DISABLE;
84 + return phy_start_aneg(dev->phy_dev);
87 +static int emac_mdio_poll_link(struct mii_phy *phy)
89 + struct net_device *ndev = phy->dev;
90 + struct emac_instance *dev = netdev_priv(ndev);
93 + res = phy_read_status(dev->phy_dev);
95 + dev_err(&dev->ndev->dev, "link update failed (%d).", res);
96 + return ethtool_op_get_link(ndev);
99 + return dev->phy_dev->link;
102 +static int emac_mdio_read_link(struct mii_phy *phy)
104 + struct net_device *ndev = phy->dev;
105 + struct emac_instance *dev = netdev_priv(ndev);
108 + res = phy_read_status(dev->phy_dev);
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;
119 +static int emac_mdio_init_phy(struct mii_phy *phy)
121 + struct net_device *ndev = phy->dev;
122 + struct emac_instance *dev = netdev_priv(ndev);
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;
132 + return phy_init_hw(dev->phy_dev);
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,
143 +static void emac_dt_phy_mdio_cleanup(struct emac_instance *dev)
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;
151 + kfree(dev->phy.def);
152 + dev->phy.def = NULL;
155 +static int emac_dt_mdio_probe(struct emac_instance *dev)
157 + struct device_node *mii_np;
160 + mii_np = of_get_child_by_name(dev->ofdev->dev.of_node, "mdio");
162 + dev_err(&dev->ndev->dev, "no mdio definition found.");
166 + if (!of_device_is_available(mii_np)) {
171 + dev->mii_bus = mdiobus_alloc();
172 + if (!dev->mii_bus) {
174 + goto err_cleanup_mdio;
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);
185 + res = of_mdiobus_register(dev->mii_bus, mii_np);
187 + dev_err(&dev->ndev->dev, "cannot register MDIO bus %s (%d)",
188 + dev->mii_bus->name, res);
189 + goto err_cleanup_mdio;
191 + of_node_put(mii_np);
195 + emac_dt_phy_mdio_cleanup(dev);
197 + of_node_put(mii_np);
201 +static int emac_dt_phy_probe(struct emac_instance *dev,
202 + struct device_node *phy_handle)
207 + res = of_property_read_u32(phy_handle, "phy-flags", &phy_flags);
208 + if (res < 0 && res != -EINVAL)
211 + dev->phy.def = kzalloc(sizeof(*dev->phy.def), GFP_KERNEL);
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;
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;
235 +static int emac_probe_dt_phy(struct emac_instance *dev)
237 + struct device_node *np = dev->ofdev->dev.of_node;
238 + struct device_node *phy_handle;
241 + phy_handle = of_parse_phandle(np, "phy-handle", 0);
244 + res = emac_dt_mdio_probe(dev);
248 + res = emac_dt_phy_probe(dev, phy_handle);
250 + emac_dt_phy_mdio_cleanup(dev);
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.
265 static int emac_init_phy(struct emac_instance *dev)
267 struct device_node *np = dev->ofdev->dev.of_node;
268 @@ -2462,6 +2703,18 @@ static int emac_init_phy(struct emac_ins
272 + if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
273 + int res = emac_probe_dt_phy(dev);
278 + dev_err(&dev->ndev->dev, "failed to attach dt phy (%d).",
281 + /* continue with old code */
284 if (dev->phy_address != 0xffffffff)
285 phy_map = ~(1 << dev->phy_address);
287 @@ -2489,6 +2742,7 @@ static int emac_init_phy(struct emac_ins
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 ... */
299 + emac_dt_phy_mdio_cleanup(dev);
301 if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
302 tah_detach(dev->tah_dev, dev->tah_port);
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);
309 + phy_disconnect(dev->phy_dev);
311 + emac_dt_phy_mdio_cleanup(dev);
313 busy_phy_map &= ~(1 << dev->phy.address);
314 DBG(dev, "busy_phy_map now %#x" NL, busy_phy_map);
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;
322 + /* Device-tree based phy configuration */
323 + struct mii_bus *mii_bus;
324 + struct phy_device *phy_dev;
326 /* ZMII infos if any */