blob: 5b3c24f3cde57e8f0c26f578c1ef12fd0d58d650 [file] [log] [blame]
Grant Likely8bc487d2009-04-25 12:52:56 +00001/*
2 * OF helpers for the MDIO (Ethernet PHY) API
3 *
4 * Copyright (c) 2009 Secret Lab Technologies, Ltd.
5 *
6 * This file is released under the GPLv2
7 *
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.
10 */
11
Anton Vorontsov24c30db2009-07-16 21:31:31 +000012#include <linux/kernel.h>
13#include <linux/device.h>
14#include <linux/netdevice.h>
15#include <linux/err.h>
Grant Likely8bc487d2009-04-25 12:52:56 +000016#include <linux/phy.h>
17#include <linux/of.h>
Grant Likelye3873442010-06-18 11:09:59 -060018#include <linux/of_irq.h>
Grant Likely8bc487d2009-04-25 12:52:56 +000019#include <linux/of_mdio.h>
20#include <linux/module.h>
21
22MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
23MODULE_LICENSE("GPL");
24
Florian Fainelli8fdade42013-12-05 14:52:14 -080025static void of_set_phy_supported(struct phy_device *phydev, u32 max_speed)
26{
Florian Fainellice11c432014-02-13 13:14:48 -080027 /* The default values for phydev->supported are provided by the PHY
28 * driver "features" member, we want to reset to sane defaults fist
29 * before supporting higher speeds.
30 */
31 phydev->supported &= PHY_DEFAULT_FEATURES;
Florian Fainelli8fdade42013-12-05 14:52:14 -080032
33 switch (max_speed) {
34 default:
35 return;
36
37 case SPEED_1000:
38 phydev->supported |= PHY_1000BT_FEATURES;
39 case SPEED_100:
40 phydev->supported |= PHY_100BT_FEATURES;
41 case SPEED_10:
42 phydev->supported |= PHY_10BT_FEATURES;
43 }
44}
45
Florian Fainelli2e79cb32013-12-05 14:52:10 -080046static int of_mdiobus_register_phy(struct mii_bus *mdio, struct device_node *child,
47 u32 addr)
48{
49 struct phy_device *phy;
50 bool is_c45;
Ben Dooksf15c5862014-02-18 12:16:58 +000051 int rc;
Florian Fainelli8fdade42013-12-05 14:52:14 -080052 u32 max_speed = 0;
Florian Fainelli2e79cb32013-12-05 14:52:10 -080053
54 is_c45 = of_device_is_compatible(child,
55 "ethernet-phy-ieee802.3-c45");
56
57 phy = get_phy_device(mdio, addr, is_c45);
58 if (!phy || IS_ERR(phy))
59 return 1;
60
Ben Dooksf15c5862014-02-18 12:16:58 +000061 rc = irq_of_parse_and_map(child, 0);
62 if (rc > 0) {
63 phy->irq = rc;
64 if (mdio->irq)
65 mdio->irq[addr] = rc;
66 } else {
67 if (mdio->irq)
68 phy->irq = mdio->irq[addr];
Florian Fainelli2e79cb32013-12-05 14:52:10 -080069 }
70
71 /* Associate the OF node with the device structure so it
72 * can be looked up later */
73 of_node_get(child);
74 phy->dev.of_node = child;
75
76 /* All data is now stored in the phy struct;
77 * register it */
78 rc = phy_device_register(phy);
79 if (rc) {
80 phy_device_free(phy);
81 of_node_put(child);
82 return 1;
83 }
84
Florian Fainelli8fdade42013-12-05 14:52:14 -080085 /* Set phydev->supported based on the "max-speed" property
86 * if present */
87 if (!of_property_read_u32(child, "max-speed", &max_speed))
88 of_set_phy_supported(phy, max_speed);
89
Florian Fainelli2e79cb32013-12-05 14:52:10 -080090 dev_dbg(&mdio->dev, "registered phy %s at address %i\n",
Florian Fainelli8fdade42013-12-05 14:52:14 -080091 child->name, addr);
Florian Fainelli2e79cb32013-12-05 14:52:10 -080092
93 return 0;
94}
95
Grant Likely8bc487d2009-04-25 12:52:56 +000096/**
97 * of_mdiobus_register - Register mii_bus and create PHYs from the device tree
98 * @mdio: pointer to mii_bus structure
99 * @np: pointer to device_node of MDIO bus.
100 *
101 * This function registers the mii_bus structure and registers a phy_device
102 * for each child node of @np.
103 */
104int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np)
105{
Grant Likely8bc487d2009-04-25 12:52:56 +0000106 struct device_node *child;
Sebastian Hesselbarth779d8352013-04-07 01:09:48 +0000107 const __be32 *paddr;
108 u32 addr;
Florian Fainelli2e79cb32013-12-05 14:52:10 -0800109 bool scanphys = false;
Sebastian Hesselbarth779d8352013-04-07 01:09:48 +0000110 int rc, i, len;
Grant Likely8bc487d2009-04-25 12:52:56 +0000111
112 /* Mask out all PHYs from auto probing. Instead the PHYs listed in
113 * the device tree are populated after the bus has been registered */
114 mdio->phy_mask = ~0;
115
116 /* Clear all the IRQ properties */
117 if (mdio->irq)
118 for (i=0; i<PHY_MAX_ADDR; i++)
119 mdio->irq[i] = PHY_POLL;
120
David Daney25106022012-05-02 15:16:37 +0000121 mdio->dev.of_node = np;
122
Grant Likely8bc487d2009-04-25 12:52:56 +0000123 /* Register the MDIO bus */
124 rc = mdiobus_register(mdio);
125 if (rc)
126 return rc;
127
128 /* Loop over the child nodes and register a phy_device for each one */
Alexander Sverdline207e762012-11-29 08:45:20 +0100129 for_each_available_child_of_node(np, child) {
Grant Likely8bc487d2009-04-25 12:52:56 +0000130 /* A PHY must have a reg property in the range [0-31] */
David Daney19458862010-10-27 18:03:47 -0700131 paddr = of_get_property(child, "reg", &len);
132 if (!paddr || len < sizeof(*paddr)) {
Sebastian Hesselbarth779d8352013-04-07 01:09:48 +0000133 scanphys = true;
Grant Likely8bc487d2009-04-25 12:52:56 +0000134 dev_err(&mdio->dev, "%s has invalid PHY address\n",
135 child->full_name);
136 continue;
137 }
138
David Daney19458862010-10-27 18:03:47 -0700139 addr = be32_to_cpup(paddr);
Florian Fainellibed2f9e2013-12-05 14:52:11 -0800140 if (addr >= PHY_MAX_ADDR) {
David Daney19458862010-10-27 18:03:47 -0700141 dev_err(&mdio->dev, "%s PHY address %i is too large\n",
142 child->full_name, addr);
143 continue;
Grant Likely8bc487d2009-04-25 12:52:56 +0000144 }
145
Florian Fainelli2e79cb32013-12-05 14:52:10 -0800146 rc = of_mdiobus_register_phy(mdio, child, addr);
147 if (rc)
Sebastian Hesselbarth058112c2013-05-06 23:49:31 +0000148 continue;
Grant Likely8bc487d2009-04-25 12:52:56 +0000149 }
150
Sebastian Hesselbarth779d8352013-04-07 01:09:48 +0000151 if (!scanphys)
152 return 0;
153
154 /* auto scan for PHYs with empty reg property */
155 for_each_available_child_of_node(np, child) {
156 /* Skip PHYs with reg property set */
157 paddr = of_get_property(child, "reg", &len);
158 if (paddr)
159 continue;
160
Sebastian Hesselbarth779d8352013-04-07 01:09:48 +0000161 for (addr = 0; addr < PHY_MAX_ADDR; addr++) {
162 /* skip already registered PHYs */
163 if (mdio->phy_map[addr])
164 continue;
165
166 /* be noisy to encourage people to set reg property */
167 dev_info(&mdio->dev, "scan phy %s at address %i\n",
168 child->name, addr);
169
Florian Fainelli2e79cb32013-12-05 14:52:10 -0800170 rc = of_mdiobus_register_phy(mdio, child, addr);
171 if (rc)
Sebastian Hesselbarth779d8352013-04-07 01:09:48 +0000172 continue;
Sebastian Hesselbarth779d8352013-04-07 01:09:48 +0000173 }
174 }
175
Grant Likely8bc487d2009-04-25 12:52:56 +0000176 return 0;
177}
178EXPORT_SYMBOL(of_mdiobus_register);
179
Jérôme Pouiller08a79632009-10-15 09:58:27 -0600180/* Helper function for of_phy_find_device */
181static int of_phy_match(struct device *dev, void *phy_np)
182{
Grant Likely61c7a082010-04-13 16:12:29 -0700183 return dev->of_node == phy_np;
Jérôme Pouiller08a79632009-10-15 09:58:27 -0600184}
185
Grant Likely8bc487d2009-04-25 12:52:56 +0000186/**
187 * of_phy_find_device - Give a PHY node, find the phy_device
188 * @phy_np: Pointer to the phy's device tree node
189 *
190 * Returns a pointer to the phy_device.
191 */
192struct phy_device *of_phy_find_device(struct device_node *phy_np)
193{
194 struct device *d;
Grant Likely8bc487d2009-04-25 12:52:56 +0000195 if (!phy_np)
196 return NULL;
197
Jérôme Pouiller08a79632009-10-15 09:58:27 -0600198 d = bus_find_device(&mdio_bus_type, NULL, phy_np, of_phy_match);
Grant Likely8bc487d2009-04-25 12:52:56 +0000199 return d ? to_phy_device(d) : NULL;
200}
201EXPORT_SYMBOL(of_phy_find_device);
202
203/**
204 * of_phy_connect - Connect to the phy described in the device tree
205 * @dev: pointer to net_device claiming the phy
206 * @phy_np: Pointer to device tree node for the PHY
207 * @hndlr: Link state callback for the network device
208 * @iface: PHY data interface type
209 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300210 * Returns a pointer to the phy_device if successful. NULL otherwise
Grant Likely8bc487d2009-04-25 12:52:56 +0000211 */
212struct phy_device *of_phy_connect(struct net_device *dev,
213 struct device_node *phy_np,
214 void (*hndlr)(struct net_device *), u32 flags,
215 phy_interface_t iface)
216{
217 struct phy_device *phy = of_phy_find_device(phy_np);
218
219 if (!phy)
220 return NULL;
221
Florian Fainellif9a8f832013-01-14 00:52:52 +0000222 return phy_connect_direct(dev, phy, hndlr, iface) ? NULL : phy;
Grant Likely8bc487d2009-04-25 12:52:56 +0000223}
224EXPORT_SYMBOL(of_phy_connect);
Anton Vorontsov24c30db2009-07-16 21:31:31 +0000225
226/**
227 * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy
228 * @dev: pointer to net_device claiming the phy
229 * @hndlr: Link state callback for the network device
230 * @iface: PHY data interface type
231 *
232 * This function is a temporary stop-gap and will be removed soon. It is
233 * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do
234 * not call this function from new drivers.
235 */
236struct phy_device *of_phy_connect_fixed_link(struct net_device *dev,
237 void (*hndlr)(struct net_device *),
238 phy_interface_t iface)
239{
240 struct device_node *net_np;
241 char bus_id[MII_BUS_ID_SIZE + 3];
242 struct phy_device *phy;
Jeremy Kerr33714882010-01-30 01:45:26 -0700243 const __be32 *phy_id;
Anton Vorontsov24c30db2009-07-16 21:31:31 +0000244 int sz;
245
246 if (!dev->dev.parent)
247 return NULL;
248
Grant Likely61c7a082010-04-13 16:12:29 -0700249 net_np = dev->dev.parent->of_node;
Anton Vorontsov24c30db2009-07-16 21:31:31 +0000250 if (!net_np)
251 return NULL;
252
253 phy_id = of_get_property(net_np, "fixed-link", &sz);
254 if (!phy_id || sz < sizeof(*phy_id))
255 return NULL;
256
Baruch Siache5c7d1f2012-02-27 14:48:46 +0200257 sprintf(bus_id, PHY_ID_FMT, "fixed-0", be32_to_cpu(phy_id[0]));
Anton Vorontsov24c30db2009-07-16 21:31:31 +0000258
Florian Fainellif9a8f832013-01-14 00:52:52 +0000259 phy = phy_connect(dev, bus_id, hndlr, iface);
Anton Vorontsov24c30db2009-07-16 21:31:31 +0000260 return IS_ERR(phy) ? NULL : phy;
261}
262EXPORT_SYMBOL(of_phy_connect_fixed_link);
Andy Fleming7614aba2014-01-10 14:28:11 +0800263
264/**
265 * of_phy_attach - Attach to a PHY without starting the state machine
266 * @dev: pointer to net_device claiming the phy
267 * @phy_np: Node pointer for the PHY
268 * @flags: flags to pass to the PHY
269 * @iface: PHY data interface type
270 */
271struct phy_device *of_phy_attach(struct net_device *dev,
272 struct device_node *phy_np, u32 flags,
273 phy_interface_t iface)
274{
275 struct phy_device *phy = of_phy_find_device(phy_np);
276
277 if (!phy)
278 return NULL;
279
280 return phy_attach_direct(dev, phy, flags, iface) ? NULL : phy;
281}
282EXPORT_SYMBOL(of_phy_attach);