Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / of / of_mdio.c
blob875b7b6f0d2a48bfdac42f40d6927cf59e6cf552
1 /*
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/of.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_mdio.h>
20 #include <linux/module.h>
22 MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
23 MODULE_LICENSE("GPL");
25 static void of_set_phy_supported(struct phy_device *phydev, u32 max_speed)
27 phydev->supported |= PHY_DEFAULT_FEATURES;
29 switch (max_speed) {
30 default:
31 return;
33 case SPEED_1000:
34 phydev->supported |= PHY_1000BT_FEATURES;
35 case SPEED_100:
36 phydev->supported |= PHY_100BT_FEATURES;
37 case SPEED_10:
38 phydev->supported |= PHY_10BT_FEATURES;
42 static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child,
43 u32 addr)
45 struct phy_device *phy;
46 bool is_c45;
47 int rc, prev_irq;
48 u32 max_speed = 0;
50 is_c45 = of_device_is_compatible(child,
51 "ethernet-phy-ieee802.3-c45");
53 phy = get_phy_device(mdio, addr, is_c45);
54 if (!phy || IS_ERR(phy))
55 return 1;
57 if (mdio->irq) {
58 prev_irq = mdio->irq[addr];
59 mdio->irq[addr] =
60 irq_of_parse_and_map(child, 0);
61 if (!mdio->irq[addr])
62 mdio->irq[addr] = prev_irq;
65 /* Associate the OF node with the device structure so it
66 * can be looked up later */
67 of_node_get(child);
68 phy->dev.of_node = child;
70 /* All data is now stored in the phy struct;
71 * register it */
72 rc = phy_device_register(phy);
73 if (rc) {
74 phy_device_free(phy);
75 of_node_put(child);
76 return 1;
79 /* Set phydev->supported based on the "max-speed" property
80 * if present */
81 if (!of_property_read_u32(child, "max-speed", &max_speed))
82 of_set_phy_supported(phy, max_speed);
84 dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
85 child->name, addr);
87 return 0;
90 /**
91 * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
92 * @mdio: pointer to mii_bus structure
93 * @np: pointer to device_node of MDIO bus.
95 * This function registers the mii_bus structure and registers a phy_device
96 * for each child node of @np.
98 int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
100 struct device_node *child;
101 const __be32 *paddr;
102 u32 addr;
103 bool scanphys = false;
104 int rc, i, len;
106 /* Mask out all PHYs from auto probing. Instead the PHYs listed in
107 * the device tree are populated after the bus has been registered */
108 mdio->phy_mask = ~0;
110 /* Clear all the IRQ properties */
111 if (mdio->irq)
112 for (i=0; i<PHY_MAX_ADDR; i++)
113 mdio->irq[i] = PHY_POLL;
115 mdio->dev.of_node = np;
117 /* Register the MDIO bus */
118 rc = mdiobus_register(mdio);
119 if (rc)
120 return rc;
122 /* Loop over the child nodes and register a phy_device for each one */
123 for_each_available_child_of_node(np, child) {
124 /* A PHY must have a reg property in the range [0-31] */
125 paddr = of_get_property(child, "reg", &len);
126 if (!paddr || len < sizeof(*paddr)) {
127 scanphys = true;
128 dev_err(&mdio->dev, "%s has invalid PHY address\n",
129 child->full_name);
130 continue;
133 addr = be32_to_cpup(paddr);
134 if (addr >= PHY_MAX_ADDR) {
135 dev_err(&mdio->dev, "%s PHY address %i is too large\n",
136 child->full_name, addr);
137 continue;
140 rc = of_mdiobus_register_phy(mdio, child, addr);
141 if (rc)
142 continue;
145 if (!scanphys)
146 return 0;
148 /* auto scan for PHYs with empty reg property */
149 for_each_available_child_of_node(np, child) {
150 /* Skip PHYs with reg property set */
151 paddr = of_get_property(child, "reg", &len);
152 if (paddr)
153 continue;
155 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
156 /* skip already registered PHYs */
157 if (mdio->phy_map[addr])
158 continue;
160 /* be noisy to encourage people to set reg property */
161 dev_info(&mdio->dev, "scan phy %s at address %i\n",
162 child->name, addr);
164 rc = of_mdiobus_register_phy(mdio, child, addr);
165 if (rc)
166 continue;
170 return 0;
172 EXPORT_SYMBOL(of_mdiobus_register);
174 /* Helper function for of_phy_find_device */
175 static int of_phy_match(struct device *dev, void *phy_np)
177 return dev->of_node == phy_np;
181 * of_phy_find_device - Give a PHY node, find the phy_device
182 * @phy_np: Pointer to the phy's device tree node
184 * Returns a pointer to the phy_device.
186 struct phy_device *of_phy_find_device(struct device_node *phy_np)
188 struct device *d;
189 if (!phy_np)
190 return NULL;
192 d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
193 return d ? to_phy_device(d) : NULL;
195 EXPORT_SYMBOL(of_phy_find_device);
198 * of_phy_connect - Connect to the phy described in the device tree
199 * @dev: pointer to net_device claiming the phy
200 * @phy_np: Pointer to device tree node for the PHY
201 * @hndlr: Link state callback for the network device
202 * @iface: PHY data interface type
204 * Returns a pointer to the phy_device if successful. NULL otherwise
206 struct phy_device *of_phy_connect(struct net_device *dev,
207 struct device_node *phy_np,
208 void (*hndlr)(struct net_device *), u32 flags,
209 phy_interface_t iface)
211 struct phy_device *phy = of_phy_find_device(phy_np);
213 if (!phy)
214 return NULL;
216 return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
218 EXPORT_SYMBOL(of_phy_connect);
221 * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
222 * @dev: pointer to net_device claiming the phy
223 * @hndlr: Link state callback for the network device
224 * @iface: PHY data interface type
226 * This function is a temporary stop-gap and will be removed soon. It is
227 * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
228 * not call this function from new drivers.
230 struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
231 void (*hndlr)(struct net_device *),
232 phy_interface_t iface)
234 struct device_node *net_np;
235 char bus_id[MII_BUS_ID_SIZE + 3];
236 struct phy_device *phy;
237 const __be32 *phy_id;
238 int sz;
240 if (!dev->dev.parent)
241 return NULL;
243 net_np = dev->dev.parent->of_node;
244 if (!net_np)
245 return NULL;
247 phy_id = of_get_property(net_np, "fixed-link", &sz);
248 if (!phy_id || sz < sizeof(*phy_id))
249 return NULL;
251 sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0]));
253 phy = phy_connect(dev, bus_id, hndlr, iface);
254 return IS_ERR(phy) ? NULL : phy;
256 EXPORT_SYMBOL(of_phy_connect_fixed_link);
259 * of_phy_attach - Attach to a PHY without starting the state machine
260 * @dev: pointer to net_device claiming the phy
261 * @phy_np: Node pointer for the PHY
262 * @flags: flags to pass to the PHY
263 * @iface: PHY data interface type
265 struct phy_device *of_phy_attach(struct net_device *dev,
266 struct device_node *phy_np, u32 flags,
267 phy_interface_t iface)
269 struct phy_device *phy = of_phy_find_device(phy_np);
271 if (!phy)
272 return NULL;
274 return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
276 EXPORT_SYMBOL(of_phy_attach);