Commit | Line | Data |
---|---|---|
8bc487d1 GL |
1 | /* |
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 | ||
24c30dbb AV |
12 | #include <linux/kernel.h> |
13 | #include <linux/device.h> | |
14 | #include <linux/netdevice.h> | |
15 | #include <linux/err.h> | |
8bc487d1 GL |
16 | #include <linux/phy.h> |
17 | #include <linux/of.h> | |
18 | #include <linux/of_mdio.h> | |
19 | #include <linux/module.h> | |
20 | ||
21 | MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>"); | |
22 | MODULE_LICENSE("GPL"); | |
23 | ||
24 | /** | |
25 | * of_mdiobus_register - Register mii_bus and create PHYs from the device tree | |
26 | * @mdio: pointer to mii_bus structure | |
27 | * @np: pointer to device_node of MDIO bus. | |
28 | * | |
29 | * This function registers the mii_bus structure and registers a phy_device | |
30 | * for each child node of @np. | |
31 | */ | |
32 | int of_mdiobus_register(struct mii_bus *mdio, struct device_node *np) | |
33 | { | |
34 | struct phy_device *phy; | |
35 | struct device_node *child; | |
36 | int rc, i; | |
37 | ||
38 | /* Mask out all PHYs from auto probing. Instead the PHYs listed in | |
39 | * the device tree are populated after the bus has been registered */ | |
40 | mdio->phy_mask = ~0; | |
41 | ||
42 | /* Clear all the IRQ properties */ | |
43 | if (mdio->irq) | |
44 | for (i=0; i<PHY_MAX_ADDR; i++) | |
45 | mdio->irq[i] = PHY_POLL; | |
46 | ||
47 | /* Register the MDIO bus */ | |
48 | rc = mdiobus_register(mdio); | |
49 | if (rc) | |
50 | return rc; | |
51 | ||
52 | /* Loop over the child nodes and register a phy_device for each one */ | |
53 | for_each_child_of_node(np, child) { | |
54 | const u32 *addr; | |
55 | int len; | |
56 | ||
57 | /* A PHY must have a reg property in the range [0-31] */ | |
58 | addr = of_get_property(child, "reg", &len); | |
59 | if (!addr || len < sizeof(*addr) || *addr >= 32 || *addr < 0) { | |
60 | dev_err(&mdio->dev, "%s has invalid PHY address\n", | |
61 | child->full_name); | |
62 | continue; | |
63 | } | |
64 | ||
65 | if (mdio->irq) { | |
66 | mdio->irq[*addr] = irq_of_parse_and_map(child, 0); | |
67 | if (!mdio->irq[*addr]) | |
68 | mdio->irq[*addr] = PHY_POLL; | |
69 | } | |
70 | ||
71 | phy = get_phy_device(mdio, *addr); | |
72 | if (!phy) { | |
73 | dev_err(&mdio->dev, "error probing PHY at address %i\n", | |
74 | *addr); | |
75 | continue; | |
76 | } | |
77 | phy_scan_fixups(phy); | |
78 | ||
79 | /* Associate the OF node with the device structure so it | |
80 | * can be looked up later */ | |
81 | of_node_get(child); | |
82 | dev_archdata_set_node(&phy->dev.archdata, child); | |
83 | ||
84 | /* All data is now stored in the phy struct; register it */ | |
85 | rc = phy_device_register(phy); | |
86 | if (rc) { | |
87 | phy_device_free(phy); | |
88 | of_node_put(child); | |
89 | continue; | |
90 | } | |
91 | ||
92 | dev_dbg(&mdio->dev, "registered phy %s at address %i\n", | |
93 | child->name, *addr); | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } | |
98 | EXPORT_SYMBOL(of_mdiobus_register); | |
99 | ||
100 | /** | |
101 | * of_phy_find_device - Give a PHY node, find the phy_device | |
102 | * @phy_np: Pointer to the phy's device tree node | |
103 | * | |
104 | * Returns a pointer to the phy_device. | |
105 | */ | |
106 | struct phy_device *of_phy_find_device(struct device_node *phy_np) | |
107 | { | |
108 | struct device *d; | |
109 | int match(struct device *dev, void *phy_np) | |
110 | { | |
111 | return dev_archdata_get_node(&dev->archdata) == phy_np; | |
112 | } | |
113 | ||
114 | if (!phy_np) | |
115 | return NULL; | |
116 | ||
117 | d = bus_find_device(&mdio_bus_type, NULL, phy_np, match); | |
118 | return d ? to_phy_device(d) : NULL; | |
119 | } | |
120 | EXPORT_SYMBOL(of_phy_find_device); | |
121 | ||
122 | /** | |
123 | * of_phy_connect - Connect to the phy described in the device tree | |
124 | * @dev: pointer to net_device claiming the phy | |
125 | * @phy_np: Pointer to device tree node for the PHY | |
126 | * @hndlr: Link state callback for the network device | |
127 | * @iface: PHY data interface type | |
128 | * | |
129 | * Returns a pointer to the phy_device if successfull. NULL otherwise | |
130 | */ | |
131 | struct phy_device *of_phy_connect(struct net_device *dev, | |
132 | struct device_node *phy_np, | |
133 | void (*hndlr)(struct net_device *), u32 flags, | |
134 | phy_interface_t iface) | |
135 | { | |
136 | struct phy_device *phy = of_phy_find_device(phy_np); | |
137 | ||
138 | if (!phy) | |
139 | return NULL; | |
140 | ||
141 | return phy_connect_direct(dev, phy, hndlr, flags, iface) ? NULL : phy; | |
142 | } | |
143 | EXPORT_SYMBOL(of_phy_connect); | |
24c30dbb AV |
144 | |
145 | /** | |
146 | * of_phy_connect_fixed_link - Parse fixed-link property and return a dummy phy | |
147 | * @dev: pointer to net_device claiming the phy | |
148 | * @hndlr: Link state callback for the network device | |
149 | * @iface: PHY data interface type | |
150 | * | |
151 | * This function is a temporary stop-gap and will be removed soon. It is | |
152 | * only to support the fs_enet, ucc_geth and gianfar Ethernet drivers. Do | |
153 | * not call this function from new drivers. | |
154 | */ | |
155 | struct phy_device *of_phy_connect_fixed_link(struct net_device *dev, | |
156 | void (*hndlr)(struct net_device *), | |
157 | phy_interface_t iface) | |
158 | { | |
159 | struct device_node *net_np; | |
160 | char bus_id[MII_BUS_ID_SIZE + 3]; | |
161 | struct phy_device *phy; | |
162 | const u32 *phy_id; | |
163 | int sz; | |
164 | ||
165 | if (!dev->dev.parent) | |
166 | return NULL; | |
167 | ||
168 | net_np = dev_archdata_get_node(&dev->dev.parent->archdata); | |
169 | if (!net_np) | |
170 | return NULL; | |
171 | ||
172 | phy_id = of_get_property(net_np, "fixed-link", &sz); | |
173 | if (!phy_id || sz < sizeof(*phy_id)) | |
174 | return NULL; | |
175 | ||
176 | sprintf(bus_id, PHY_ID_FMT, "0", phy_id[0]); | |
177 | ||
178 | phy = phy_connect(dev, bus_id, hndlr, 0, iface); | |
179 | return IS_ERR(phy) ? NULL : phy; | |
180 | } | |
181 | EXPORT_SYMBOL(of_phy_connect_fixed_link); |