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