f1edb9cc7a4b07e321c3a5144ba7b08fa652f08e
[openwrt/openwrt.git] / target / linux / apm821xx / patches-4.9 / 702-powerpc_ibm_phy_add_dt_parser.patch
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
8 and setup
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>
15
16 This patch adds glue-code that allows the EMAC driver to interface
17 with the existing dt-supported PHYs in drivers/net/phy.
18
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
21 directory.
22
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.
29
30 Signed-off-by: Christian Lamparter <chunkeey@googlemail.com>
31 ---
32 --- a/drivers/net/ethernet/ibm/emac/core.c
33 +++ b/drivers/net/ethernet/ibm/emac/core.c
34 @@ -42,6 +42,7 @@
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>
40
41 #include <asm/processor.h>
42 @@ -2422,6 +2423,229 @@ static int emac_read_uint_prop(struct de
43 return 0;
44 }
45
46 +static void emac_adjust_link(struct net_device *ndev)
47 +{
48 + struct emac_instance *dev = netdev_priv(ndev);
49 + struct phy_device *phy = dev->phy_dev;
50 +
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;
57 +}
58 +
59 +static int emac_mii_bus_read(struct mii_bus *bus, int addr, int regnum)
60 +{
61 + return emac_mdio_read(bus->priv, addr, regnum);
62 +}
63 +
64 +static int emac_mii_bus_write(struct mii_bus *bus, int addr, int regnum, u16 val)
65 +{
66 + emac_mdio_write(bus->priv, addr, regnum, val);
67 + return 0;
68 +}
69 +
70 +static int emac_mii_bus_reset(struct mii_bus *bus)
71 +{
72 + struct emac_instance *dev = netdev_priv(bus->priv);
73 + int err;
74 +
75 + err = emac_reset(dev);
76 + if (err)
77 + return err;
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);
81 + return 0;
82 +}
83 +
84 +static int emac_mdio_setup_aneg(struct mii_phy *phy, u32 advertise)
85 +{
86 + struct net_device *ndev = phy->dev;
87 + struct emac_instance *dev = netdev_priv(ndev);
88 +
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);
98 +}
99 +
100 +static int emac_mdio_setup_forced(struct mii_phy *phy, int speed, int fd)
101 +{
102 + struct net_device *ndev = phy->dev;
103 + struct emac_instance *dev = netdev_priv(ndev);
104 +
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;
110 + phy->duplex = fd;
111 + return phy_start_aneg(dev->phy_dev);
112 +}
113 +
114 +static int emac_mdio_poll_link(struct mii_phy *phy)
115 +{
116 + struct net_device *ndev = phy->dev;
117 + struct emac_instance *dev = netdev_priv(ndev);
118 + int res;
119 +
120 + res = phy_read_status(dev->phy_dev);
121 + if (res) {
122 + dev_err(&dev->ofdev->dev, "link update failed (%d).", res);
123 + return ethtool_op_get_link(ndev);
124 + }
125 +
126 + return dev->phy_dev->link;
127 +}
128 +
129 +static int emac_mdio_read_link(struct mii_phy *phy)
130 +{
131 + struct net_device *ndev = phy->dev;
132 + struct emac_instance *dev = netdev_priv(ndev);
133 + int res;
134 +
135 + res = phy_read_status(dev->phy_dev);
136 + if (res)
137 + return res;
138 +
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;
143 + return 0;
144 +}
145 +
146 +static int emac_mdio_init_phy(struct mii_phy *phy)
147 +{
148 + struct net_device *ndev = phy->dev;
149 + struct emac_instance *dev = netdev_priv(ndev);
150 +
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;
158 +
159 + return phy_init_hw(dev->phy_dev);
160 +}
161 +
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,
168 +};
169 +
170 +static int emac_dt_mdio_probe(struct emac_instance *dev)
171 +{
172 + struct device_node *mii_np;
173 + int res;
174 +
175 + mii_np = of_get_child_by_name(dev->ofdev->dev.of_node, "mdio");
176 + if (!mii_np) {
177 + dev_err(&dev->ofdev->dev, "no mdio definition found.");
178 + return -ENODEV;
179 + }
180 +
181 + if (!of_device_is_available(mii_np)) {
182 + res = 1;
183 + goto put_node;
184 + }
185 +
186 + dev->mii_bus = devm_mdiobus_alloc(&dev->ofdev->dev);
187 + if (!dev->mii_bus) {
188 + res = -ENOMEM;
189 + goto put_node;
190 + }
191 +
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);
200 + if (res) {
201 + dev_err(&dev->ofdev->dev, "cannot register MDIO bus %s (%d)",
202 + dev->mii_bus->name, res);
203 + }
204 +
205 + put_node:
206 + of_node_put(mii_np);
207 + return res;
208 +}
209 +
210 +static int emac_dt_phy_probe(struct emac_instance *dev,
211 + struct device_node *phy_handle)
212 +{
213 + u32 phy_flags = 0;
214 + int res;
215 +
216 + res = of_property_read_u32(phy_handle, "phy-flags", &phy_flags);
217 + if (res < 0 && res != -EINVAL)
218 + return res;
219 +
220 + dev->phy.def = devm_kzalloc(&dev->ofdev->dev, sizeof(*dev->phy.def),
221 + GFP_KERNEL);
222 + if (!dev->phy.def)
223 + return -ENOMEM;
224 +
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");
230 + return -ENODEV;
231 + }
232 +
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;
240 + return 0;
241 +}
242 +
243 +static int emac_probe_dt_phy(struct emac_instance *dev)
244 +{
245 + struct device_node *np = dev->ofdev->dev.of_node;
246 + struct device_node *phy_handle;
247 + int res = 0;
248 +
249 + phy_handle = of_parse_phandle(np, "phy-handle", 0);
250 +
251 + if (phy_handle) {
252 + res = emac_dt_mdio_probe(dev);
253 + if (!res) {
254 + res = emac_dt_phy_probe(dev, phy_handle);
255 + if (!res)
256 + res = 1;
257 + else
258 + mdiobus_unregister(dev->mii_bus);
259 + }
260 + }
261 +
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.
265 + */
266 + return res;
267 +}
268 +
269 static int emac_init_phy(struct emac_instance *dev)
270 {
271 struct device_node *np = dev->ofdev->dev.of_node;
272 @@ -2492,6 +2716,22 @@ static int emac_init_phy(struct emac_ins
273
274 emac_configure(dev);
275
276 + if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
277 + int res = emac_probe_dt_phy(dev);
278 +
279 + if (res == 1) {
280 + mutex_unlock(&emac_phy_map_lock);
281 + goto init_phy;
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",
285 + res);
286 + return res;
287 + }
288 +
289 + /* continue with old code */
290 + }
291 +
292 if (dev->phy_address != 0xffffffff)
293 phy_map = ~(1 << dev->phy_address);
294
295 @@ -2519,6 +2759,7 @@ static int emac_init_phy(struct emac_ins
296 return -ENXIO;
297 }
298
299 + init_phy:
300 /* Init PHY */
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);
306
307 + if (dev->phy_dev)
308 + phy_disconnect(dev->phy_dev);
309 +
310 + if (dev->mii_bus)
311 + mdiobus_unregister(dev->mii_bus);
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;