2 * OF helpers for the MDIO (Ethernet PHY) API
4 * Copyright (c) 2009 Secret Lab Technologies, Ltd.
6 * This file is released under the GPLv2
8 * This file provides helper functions for extracting PHY device information
9 * out of the OpenFirmware device tree and using it to populate an mii_bus.
12 #include <linux/kernel.h>
13 #include <linux/device.h>
14 #include <linux/netdevice.h>
15 #include <linux/err.h>
16 #include <linux/phy.h>
17 #include <linux/phy_fixed.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_mdio.h>
21 #include <linux/module.h>
23 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
24 MODULE_LICENSE("GPL");
26 static void of_set_phy_supported(struct phy_device *phydev, u32 max_speed)
28 /* The default values for phydev->supported are provided by the PHY
29 * driver "features" member, we want to reset to sane defaults fist
30 * before supporting higher speeds.
32 phydev->supported &= PHY_DEFAULT_FEATURES;
39 phydev->supported |= PHY_1000BT_FEATURES;
41 phydev->supported |= PHY_100BT_FEATURES;
43 phydev->supported |= PHY_10BT_FEATURES;
47 /* Extract the clause 22 phy ID from the compatible string of the form
48 * ethernet-phy-idAAAA.BBBB */
49 static int of_get_phy_id(struct device_node *device, u32 *phy_id)
51 struct property *prop;
53 unsigned int upper, lower;
55 of_property_for_each_string(device, "compatible", prop, cp) {
56 if (sscanf(cp, "ethernet-phy-id%4x.%4x", &upper, &lower) == 2) {
57 *phy_id = ((upper & 0xFFFF) << 16) | (lower & 0xFFFF);
64 static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child,
67 struct phy_device *phy;
73 is_c45 = of_device_is_compatible(child,
74 "ethernet-phy-ieee802.3-c45");
76 if (!is_c45 && !of_get_phy_id(child, &phy_id))
77 phy = phy_device_create(mdio, addr, phy_id, 0, NULL);
79 phy = get_phy_device(mdio, addr, is_c45);
80 if (!phy || IS_ERR(phy))
83 rc = irq_of_parse_and_map(child, 0);
90 phy->irq = mdio->irq[addr];
93 /* Associate the OF node with the device structure so it
94 * can be looked up later */
96 phy->dev.of_node = child;
98 /* All data is now stored in the phy struct;
100 rc = phy_device_register(phy);
102 phy_device_free(phy);
107 /* Set phydev->supported based on the "max-speed" property
109 if (!of_property_read_u32(child, "max-speed", &max_speed))
110 of_set_phy_supported(phy, max_speed);
112 dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
119 * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
120 * @mdio: pointer to mii_bus structure
121 * @np: pointer to device_node of MDIO bus.
123 * This function registers the mii_bus structure and registers a phy_device
124 * for each child node of @np.
126 int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
128 struct device_node *child;
131 bool scanphys = false;
134 /* Mask out all PHYs from auto probing. Instead the PHYs listed in
135 * the device tree are populated after the bus has been registered */
138 /* Clear all the IRQ properties */
140 for (i=0; i<PHY_MAX_ADDR; i++)
141 mdio->irq[i] = PHY_POLL;
143 mdio->dev.of_node = np;
145 /* Register the MDIO bus */
146 rc = mdiobus_register(mdio);
150 /* Loop over the child nodes and register a phy_device for each one */
151 for_each_available_child_of_node(np, child) {
152 /* A PHY must have a reg property in the range [0-31] */
153 paddr = of_get_property(child, "reg", &len);
154 if (!paddr || len < sizeof(*paddr)) {
156 dev_err(&mdio->dev, "%s has invalid PHY address\n",
161 addr = be32_to_cpup(paddr);
162 if (addr >= PHY_MAX_ADDR) {
163 dev_err(&mdio->dev, "%s PHY address %i is too large\n",
164 child->full_name, addr);
168 rc = of_mdiobus_register_phy(mdio, child, addr);
176 /* auto scan for PHYs with empty reg property */
177 for_each_available_child_of_node(np, child) {
178 /* Skip PHYs with reg property set */
179 paddr = of_get_property(child, "reg", &len);
183 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
184 /* skip already registered PHYs */
185 if (mdio->phy_map[addr])
188 /* be noisy to encourage people to set reg property */
189 dev_info(&mdio->dev, "scan phy %s at address %i\n",
192 rc = of_mdiobus_register_phy(mdio, child, addr);
200 EXPORT_SYMBOL(of_mdiobus_register);
202 /* Helper function for of_phy_find_device */
203 static int of_phy_match(struct device *dev, void *phy_np)
205 return dev->of_node == phy_np;
209 * of_phy_find_device - Give a PHY node, find the phy_device
210 * @phy_np: Pointer to the phy's device tree node
212 * Returns a pointer to the phy_device.
214 struct phy_device *of_phy_find_device(struct device_node *phy_np)
220 d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
221 return d ? to_phy_device(d) : NULL;
223 EXPORT_SYMBOL(of_phy_find_device);
226 * of_phy_connect - Connect to the phy described in the device tree
227 * @dev: pointer to net_device claiming the phy
228 * @phy_np: Pointer to device tree node for the PHY
229 * @hndlr: Link state callback for the network device
230 * @iface: PHY data interface type
232 * Returns a pointer to the phy_device if successful. NULL otherwise
234 struct phy_device *of_phy_connect(struct net_device *dev,
235 struct device_node *phy_np,
236 void (*hndlr)(struct net_device *), u32 flags,
237 phy_interface_t iface)
239 struct phy_device *phy = of_phy_find_device(phy_np);
244 return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
246 EXPORT_SYMBOL(of_phy_connect);
249 * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
250 * @dev: pointer to net_device claiming the phy
251 * @hndlr: Link state callback for the network device
252 * @iface: PHY data interface type
254 * This function is a temporary stop-gap and will be removed soon. It is
255 * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
256 * not call this function from new drivers.
258 struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
259 void (*hndlr)(struct net_device *),
260 phy_interface_t iface)
262 struct device_node *net_np;
263 char bus_id[MII_BUS_ID_SIZE + 3];
264 struct phy_device *phy;
265 const __be32 *phy_id;
268 if (!dev->dev.parent)
271 net_np = dev->dev.parent->of_node;
275 phy_id = of_get_property(net_np, "fixed-link", &sz);
276 if (!phy_id || sz < sizeof(*phy_id))
279 sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0]));
281 phy = phy_connect(dev, bus_id, hndlr, iface);
282 return IS_ERR(phy) ? NULL : phy;
284 EXPORT_SYMBOL(of_phy_connect_fixed_link);
287 * of_phy_attach - Attach to a PHY without starting the state machine
288 * @dev: pointer to net_device claiming the phy
289 * @phy_np: Node pointer for the PHY
290 * @flags: flags to pass to the PHY
291 * @iface: PHY data interface type
293 struct phy_device *of_phy_attach(struct net_device *dev,
294 struct device_node *phy_np, u32 flags,
295 phy_interface_t iface)
297 struct phy_device *phy = of_phy_find_device(phy_np);
302 return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
304 EXPORT_SYMBOL(of_phy_attach);
306 #if defined(CONFIG_FIXED_PHY)
308 * of_phy_is_fixed_link() and of_phy_register_fixed_link() must
309 * support two DT bindings:
310 * - the old DT binding, where 'fixed-link' was a property with 5
311 * cells encoding various informations about the fixed PHY
312 * - the new DT binding, where 'fixed-link' is a sub-node of the
315 bool of_phy_is_fixed_link(struct device_node *np)
317 struct device_node *dn;
321 dn = of_get_child_by_name(np, "fixed-link");
328 if (of_get_property(np, "fixed-link", &len) &&
329 len == (5 * sizeof(__be32)))
334 EXPORT_SYMBOL(of_phy_is_fixed_link);
336 int of_phy_register_fixed_link(struct device_node *np)
338 struct fixed_phy_status status = {};
339 struct device_node *fixed_link_node;
340 const __be32 *fixed_link_prop;
344 fixed_link_node = of_get_child_by_name(np, "fixed-link");
345 if (fixed_link_node) {
347 status.duplex = of_property_read_bool(np, "full-duplex");
348 if (of_property_read_u32(fixed_link_node, "speed", &status.speed))
350 status.pause = of_property_read_bool(np, "pause");
351 status.asym_pause = of_property_read_bool(np, "asym-pause");
352 of_node_put(fixed_link_node);
353 return fixed_phy_register(PHY_POLL, &status, np);
357 fixed_link_prop = of_get_property(np, "fixed-link", &len);
358 if (fixed_link_prop && len == (5 * sizeof(__be32))) {
360 status.duplex = be32_to_cpu(fixed_link_prop[1]);
361 status.speed = be32_to_cpu(fixed_link_prop[2]);
362 status.pause = be32_to_cpu(fixed_link_prop[3]);
363 status.asym_pause = be32_to_cpu(fixed_link_prop[4]);
364 return fixed_phy_register(PHY_POLL, &status, np);
369 EXPORT_SYMBOL(of_phy_register_fixed_link);