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 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
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>
22 #include <asm/processor.h>
23 @@ -2383,6 +2384,246 @@ static int emac_read_uint_prop(struct device_node *np, const char *name,
27 +static void emac_adjust_link(struct net_device *ndev)
29 + struct emac_instance *dev = netdev_priv(ndev);
30 + struct phy_device *phy = dev->phy_dev;
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;
40 +static int emac_mii_bus_read(struct mii_bus *bus, int addr, int regnum)
42 + return emac_mdio_read(bus->priv, addr, regnum);
45 +static int emac_mii_bus_write(struct mii_bus *bus, int addr, int regnum, u16 val)
47 + emac_mdio_write(bus->priv, addr, regnum, val);
51 +static int emac_mii_bus_reset(struct mii_bus *bus)
53 + struct emac_instance *dev = netdev_priv(bus->priv);
55 + emac_mii_reset_phy(&dev->phy);
59 +static int emac_mdio_setup_aneg(struct mii_phy *phy, u32 advertise)
61 + struct net_device *ndev = phy->dev;
62 + struct emac_instance *dev = netdev_priv(ndev);
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);
75 +static int emac_mdio_setup_forced(struct mii_phy *phy, int speed, int fd)
77 + struct net_device *ndev = phy->dev;
78 + struct emac_instance *dev = netdev_priv(ndev);
80 + dev->phy.autoneg = AUTONEG_DISABLE;
81 + dev->phy.speed = speed;
82 + dev->phy.duplex = fd;
83 + phy->autoneg = AUTONEG_DISABLE;
86 + return phy_start_aneg(dev->phy_dev);
89 +static int emac_mdio_poll_link(struct mii_phy *phy)
91 + struct net_device *ndev = phy->dev;
92 + struct emac_instance *dev = netdev_priv(ndev);
95 + res = phy_read_status(dev->phy_dev);
97 + dev_err(&dev->ndev->dev, "link update failed (%d).", res);
98 + return ethtool_op_get_link(ndev);
101 + return dev->phy_dev->link;
104 +static int emac_mdio_read_link(struct mii_phy *phy)
106 + struct net_device *ndev = phy->dev;
107 + struct emac_instance *dev = netdev_priv(ndev);
110 + res = phy_read_status(dev->phy_dev);
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;
121 +static int emac_mdio_init_phy(struct mii_phy *phy)
123 + struct net_device *ndev = phy->dev;
124 + struct emac_instance *dev = netdev_priv(ndev);
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;
134 + return phy_init_hw(dev->phy_dev);
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,
145 +static void emac_dt_phy_mdio_cleanup(struct emac_instance *dev)
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;
153 + kfree(dev->phy.def);
154 + dev->phy.def = NULL;
157 +static int emac_dt_mdio_probe(struct emac_instance *dev)
159 + struct device_node *mii_np;
162 + mii_np = of_get_child_by_name(dev->ofdev->dev.of_node, "mdio");
164 + dev_err(&dev->ndev->dev, "no mdio definition found.");
168 + if (!of_device_is_available(mii_np)) {
173 + dev->mii_bus = mdiobus_alloc();
174 + if (!dev->mii_bus) {
176 + goto err_cleanup_mdio;
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);
187 + res = of_mdiobus_register(dev->mii_bus, mii_np);
189 + dev_err(&dev->ndev->dev, "cannot register MDIO bus %s (%d)",
190 + dev->mii_bus->name, res);
191 + goto err_cleanup_mdio;
193 + of_node_put(mii_np);
197 + emac_dt_phy_mdio_cleanup(dev);
199 + of_node_put(mii_np);
203 +static int emac_dt_phy_probe(struct emac_instance *dev,
204 + struct device_node *phy_handle)
209 + res = of_property_read_u32(phy_handle, "phy-flags", &phy_flags);
210 + if (res < 0 && res != -EINVAL)
213 + dev->phy.def = kzalloc(sizeof(*dev->phy.def), GFP_KERNEL);
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;
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;
237 +static int emac_probe_dt_phy(struct emac_instance *dev)
239 + struct device_node *np = dev->ofdev->dev.of_node;
240 + struct device_node *phy_handle;
243 + phy_handle = of_parse_phandle(np, "phy-handle", 0);
246 + res = emac_dt_mdio_probe(dev);
250 + res = emac_dt_phy_probe(dev, phy_handle);
252 + emac_dt_phy_mdio_cleanup(dev);
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.
267 static int emac_init_phy(struct emac_instance *dev)
269 struct device_node *np = dev->ofdev->dev.of_node;
270 @@ -2453,6 +2694,18 @@ static int emac_init_phy(struct emac_instance *dev)
274 + if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
275 + int res = emac_probe_dt_phy(dev);
280 + dev_err(&dev->ndev->dev, "failed to attach dt phy (%d).",
283 + /* continue with old code */
286 if (dev->phy_address != 0xffffffff)
287 phy_map = ~(1 << dev->phy_address);
289 @@ -2480,6 +2733,7 @@ static int emac_init_phy(struct emac_instance *dev)
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 ... */
301 + emac_dt_phy_mdio_cleanup(dev);
303 if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
304 tah_detach(dev->tah_dev, dev->tah_port);
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);
311 + phy_disconnect(dev->phy_dev);
313 + emac_dt_phy_mdio_cleanup(dev);
315 busy_phy_map &= ~(1 << dev->phy.address);
316 DBG(dev, "busy_phy_map now %#x" NL, busy_phy_map);
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;
326 + /* Device-tree based phy configuration */
327 + struct mii_bus *mii_bus;
328 + struct phy_device *phy_dev;
330 /* ZMII infos if any */