1 From b1c54da602ae9215cfbde1c3ed3b6296b76f07fc Mon Sep 17 00:00:00 2001
2 Message-Id: <b1c54da602ae9215cfbde1c3ed3b6296b76f07fc.1486337989.git.chunkeey@googlemail.com>
3 In-Reply-To: <246bd6614529d28dc48b11981ab5dae7a7364fc2.1486337989.git.chunkeey@googlemail.com>
4 References: <246bd6614529d28dc48b11981ab5dae7a7364fc2.1486337989.git.chunkeey@googlemail.com>
5 From: Christian Lamparter <chunkeey@gmail.com>
6 Date: Mon, 13 Jun 2016 15:42:21 +0200
7 Subject: [RFC 2/2] net: emac: add support for device-tree based PHY discovery
9 To: netdev@vger.kernel.org,
10 devicetree@vger.kernel.org
11 Cc: David S. Miller <davem@davemloft.net>,
12 Ivan Mikhaylov <ivan@de.ibm.com>,
13 Mark Rutland <mark.rutland@arm.com>,
14 Rob Herring <robh+dt@kernel.org>
16 This patch adds glue-code that allows the EMAC driver to interface
17 with the existing dt-supported PHYs in drivers/net/phy.
19 Because currently, the emac driver maintains a small library of
20 supported phys for in a private phy.c file located in the drivers
23 The support is limited to mostly single ethernet transceiver like the:
24 CIS8201, BCM5248, ET1011C, Marvell 88E1111 and 88E1112, AR8035.
25 However, routers like the Netgear WNDR4700 and Cisco Meraki MX60(W)
26 have a 5-port switch (QCA8327N) attached to the MDIO of the EMAC.
27 The switch chip has already a proper phy-driver (ar8216) that uses
28 the generic phy library.
30 Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
32 --- a/drivers/net/ethernet/ibm/emac/core.c
33 +++ b/drivers/net/ethernet/ibm/emac/core.c
35 #include <linux/of_address.h>
36 #include <linux/of_irq.h>
37 #include <linux/of_net.h>
38 +#include <linux/of_mdio.h>
39 #include <linux/slab.h>
41 #include <asm/processor.h>
42 @@ -2422,6 +2423,229 @@ static int emac_read_uint_prop(struct de
46 +static void emac_adjust_link(struct net_device *ndev)
48 + struct emac_instance *dev = netdev_priv(ndev);
49 + struct phy_device *phy = dev->phy_dev;
51 + dev->phy.autoneg = phy->autoneg;
52 + dev->phy.speed = phy->speed;
53 + dev->phy.duplex = phy->duplex;
54 + dev->phy.pause = phy->pause;
55 + dev->phy.asym_pause = phy->asym_pause;
56 + dev->phy.advertising = phy->advertising;
59 +static int emac_mii_bus_read(struct mii_bus *bus, int addr, int regnum)
61 + return emac_mdio_read(bus->priv, addr, regnum);
64 +static int emac_mii_bus_write(struct mii_bus *bus, int addr, int regnum, u16 val)
66 + emac_mdio_write(bus->priv, addr, regnum, val);
70 +static int emac_mii_bus_reset(struct mii_bus *bus)
72 + struct emac_instance *dev = netdev_priv(bus->priv);
75 + err = emac_reset(dev);
78 + /* Meraki MX60(W)'s uboot will disable the switch and
79 + * a bus reset won't do anything. */
80 + emac_mii_reset_phy(&dev->phy);
84 +static int emac_mdio_setup_aneg(struct mii_phy *phy, u32 advertise)
86 + struct net_device *ndev = phy->dev;
87 + struct emac_instance *dev = netdev_priv(ndev);
89 + dev->phy.autoneg = AUTONEG_ENABLE;
90 + dev->phy.speed = SPEED_1000;
91 + dev->phy.duplex = DUPLEX_FULL;
92 + dev->phy.advertising = advertise;
93 + phy->autoneg = AUTONEG_ENABLE;
94 + phy->speed = dev->phy.speed;
95 + phy->duplex = dev->phy.duplex;
96 + phy->advertising = advertise;
97 + return phy_start_aneg(dev->phy_dev);
100 +static int emac_mdio_setup_forced(struct mii_phy *phy, int speed, int fd)
102 + struct net_device *ndev = phy->dev;
103 + struct emac_instance *dev = netdev_priv(ndev);
105 + dev->phy.autoneg = AUTONEG_DISABLE;
106 + dev->phy.speed = speed;
107 + dev->phy.duplex = fd;
108 + phy->autoneg = AUTONEG_DISABLE;
109 + phy->speed = speed;
111 + return phy_start_aneg(dev->phy_dev);
114 +static int emac_mdio_poll_link(struct mii_phy *phy)
116 + struct net_device *ndev = phy->dev;
117 + struct emac_instance *dev = netdev_priv(ndev);
120 + res = phy_read_status(dev->phy_dev);
122 + dev_err(&dev->ofdev->dev, "link update failed (%d).", res);
123 + return ethtool_op_get_link(ndev);
126 + return dev->phy_dev->link;
129 +static int emac_mdio_read_link(struct mii_phy *phy)
131 + struct net_device *ndev = phy->dev;
132 + struct emac_instance *dev = netdev_priv(ndev);
135 + res = phy_read_status(dev->phy_dev);
139 + dev->phy.speed = phy->speed;
140 + dev->phy.duplex = phy->duplex;
141 + dev->phy.pause = phy->pause;
142 + dev->phy.asym_pause = phy->asym_pause;
146 +static int emac_mdio_init_phy(struct mii_phy *phy)
148 + struct net_device *ndev = phy->dev;
149 + struct emac_instance *dev = netdev_priv(ndev);
151 + phy_start(dev->phy_dev);
152 + dev->phy.autoneg = phy->autoneg;
153 + dev->phy.speed = phy->speed;
154 + dev->phy.duplex = phy->duplex;
155 + dev->phy.advertising = phy->advertising;
156 + dev->phy.pause = phy->pause;
157 + dev->phy.asym_pause = phy->asym_pause;
159 + return phy_init_hw(dev->phy_dev);
162 +static const struct mii_phy_ops emac_dt_mdio_phy_ops = {
163 + .init = emac_mdio_init_phy,
164 + .setup_aneg = emac_mdio_setup_aneg,
165 + .setup_forced = emac_mdio_setup_forced,
166 + .poll_link = emac_mdio_poll_link,
167 + .read_link = emac_mdio_read_link,
170 +static int emac_dt_mdio_probe(struct emac_instance *dev)
172 + struct device_node *mii_np;
175 + mii_np = of_get_child_by_name(dev->ofdev->dev.of_node, "mdio");
177 + dev_err(&dev->ofdev->dev, "no mdio definition found.");
181 + if (!of_device_is_available(mii_np)) {
186 + dev->mii_bus = devm_mdiobus_alloc(&dev->ofdev->dev);
187 + if (!dev->mii_bus) {
192 + dev->mii_bus->priv = dev->ndev;
193 + dev->mii_bus->parent = dev->ndev->dev.parent;
194 + dev->mii_bus->name = "emac_mdio";
195 + dev->mii_bus->read = &emac_mii_bus_read;
196 + dev->mii_bus->write = &emac_mii_bus_write;
197 + dev->mii_bus->reset = &emac_mii_bus_reset;
198 + snprintf(dev->mii_bus->id, MII_BUS_ID_SIZE, "%s", dev->ofdev->name);
199 + res = of_mdiobus_register(dev->mii_bus, mii_np);
201 + dev_err(&dev->ofdev->dev, "cannot register MDIO bus %s (%d)",
202 + dev->mii_bus->name, res);
206 + of_node_put(mii_np);
210 +static int emac_dt_phy_probe(struct emac_instance *dev,
211 + struct device_node *phy_handle)
216 + res = of_property_read_u32(phy_handle, "phy-flags", &phy_flags);
217 + if (res < 0 && res != -EINVAL)
220 + dev->phy.def = devm_kzalloc(&dev->ofdev->dev, sizeof(*dev->phy.def),
225 + dev->phy_dev = of_phy_connect(dev->ndev, phy_handle,
226 + &emac_adjust_link, phy_flags,
227 + PHY_INTERFACE_MODE_RGMII);
228 + if (!dev->phy_dev) {
229 + dev_err(&dev->ofdev->dev, "failed to connect to PHY.\n");
233 + dev->phy.def->phy_id = dev->phy_dev->drv->phy_id;
234 + dev->phy.def->phy_id_mask = dev->phy_dev->drv->phy_id_mask;
235 + dev->phy.def->name = dev->phy_dev->drv->name;
236 + dev->phy.def->ops = &emac_dt_mdio_phy_ops;
237 + dev->phy.features = dev->phy_dev->supported;
238 + dev->phy.address = dev->phy_dev->mdio.addr;
239 + dev->phy.mode = dev->phy_dev->interface;
243 +static int emac_probe_dt_phy(struct emac_instance *dev)
245 + struct device_node *np = dev->ofdev->dev.of_node;
246 + struct device_node *phy_handle;
249 + phy_handle = of_parse_phandle(np, "phy-handle", 0);
252 + res = emac_dt_mdio_probe(dev);
254 + res = emac_dt_phy_probe(dev, phy_handle);
258 + mdiobus_unregister(dev->mii_bus);
262 + of_node_put(phy_handle);
263 + /* if no phy device was specified in the device tree, then we fallback
264 + * to the old emac_phy.c probe code for compatibility reasons.
269 static int emac_init_phy(struct emac_instance *dev)
271 struct device_node *np = dev->ofdev->dev.of_node;
272 @@ -2492,6 +2716,22 @@ static int emac_init_phy(struct emac_ins
276 + if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
277 + int res = emac_probe_dt_phy(dev);
280 + mutex_unlock(&emac_phy_map_lock);
282 + } else if (res < 0) {
283 + mutex_unlock(&emac_phy_map_lock);
284 + dev_err(&dev->ofdev->dev, "failed to attach dt phy (%d).\n",
289 + /* continue with old code */
292 if (dev->phy_address != 0xffffffff)
293 phy_map = ~(1 << dev->phy_address);
295 @@ -2519,6 +2759,7 @@ static int emac_init_phy(struct emac_ins
301 if (dev->phy.def->ops->init)
302 dev->phy.def->ops->init(&dev->phy);
303 @@ -2987,6 +3228,12 @@ static int emac_remove(struct platform_d
304 if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
305 zmii_detach(dev->zmii_dev, dev->zmii_port);
308 + phy_disconnect(dev->phy_dev);
311 + mdiobus_unregister(dev->mii_bus);
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 */