]>
Commit | Line | Data |
---|---|---|
2f53e904 | 1 | /* Framework for finding and configuring PHYs. |
00db8189 AF |
2 | * Also contains generic PHY driver |
3 | * | |
4 | * Author: Andy Fleming | |
5 | * | |
6 | * Copyright (c) 2004 Freescale Semiconductor, Inc. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2 of the License, or (at your | |
11 | * option) any later version. | |
12 | * | |
13 | */ | |
8d242488 JP |
14 | |
15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
16 | ||
00db8189 | 17 | #include <linux/kernel.h> |
00db8189 AF |
18 | #include <linux/string.h> |
19 | #include <linux/errno.h> | |
20 | #include <linux/unistd.h> | |
21 | #include <linux/slab.h> | |
22 | #include <linux/interrupt.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/delay.h> | |
25 | #include <linux/netdevice.h> | |
26 | #include <linux/etherdevice.h> | |
27 | #include <linux/skbuff.h> | |
00db8189 AF |
28 | #include <linux/mm.h> |
29 | #include <linux/module.h> | |
00db8189 AF |
30 | #include <linux/mii.h> |
31 | #include <linux/ethtool.h> | |
32 | #include <linux/phy.h> | |
2f53e904 SS |
33 | #include <linux/io.h> |
34 | #include <linux/uaccess.h> | |
00db8189 | 35 | |
00db8189 | 36 | #include <asm/irq.h> |
00db8189 | 37 | |
afcceaa3 OH |
38 | MODULE_DESCRIPTION("PHY library"); |
39 | MODULE_AUTHOR("Andy Fleming"); | |
40 | MODULE_LICENSE("GPL"); | |
41 | ||
6f4a7f41 AV |
42 | void phy_device_free(struct phy_device *phydev) |
43 | { | |
b2a43191 | 44 | put_device(&phydev->dev); |
6f4a7f41 | 45 | } |
4dea547f | 46 | EXPORT_SYMBOL(phy_device_free); |
6f4a7f41 AV |
47 | |
48 | static void phy_device_release(struct device *dev) | |
49 | { | |
b2a43191 | 50 | kfree(to_phy_device(dev)); |
6f4a7f41 AV |
51 | } |
52 | ||
4dea547f | 53 | static struct phy_driver genphy_driver; |
4dea547f | 54 | |
f62220d3 AF |
55 | static LIST_HEAD(phy_fixup_list); |
56 | static DEFINE_MUTEX(phy_fixup_lock); | |
57 | ||
89ff05ec | 58 | static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
59 | u32 flags, phy_interface_t interface); | |
60 | ||
2f53e904 SS |
61 | /** |
62 | * phy_register_fixup - creates a new phy_fixup and adds it to the list | |
f62220d3 AF |
63 | * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID) |
64 | * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) | |
2f53e904 | 65 | * It can also be PHY_ANY_UID |
f62220d3 | 66 | * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before |
2f53e904 | 67 | * comparison |
f62220d3 AF |
68 | * @run: The actual code to be run when a matching PHY is found |
69 | */ | |
70 | int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask, | |
2f53e904 | 71 | int (*run)(struct phy_device *)) |
f62220d3 AF |
72 | { |
73 | struct phy_fixup *fixup; | |
74 | ||
2f53e904 | 75 | fixup = kzalloc(sizeof(*fixup), GFP_KERNEL); |
f62220d3 AF |
76 | if (!fixup) |
77 | return -ENOMEM; | |
78 | ||
fb28ad35 | 79 | strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id)); |
f62220d3 AF |
80 | fixup->phy_uid = phy_uid; |
81 | fixup->phy_uid_mask = phy_uid_mask; | |
82 | fixup->run = run; | |
83 | ||
84 | mutex_lock(&phy_fixup_lock); | |
85 | list_add_tail(&fixup->list, &phy_fixup_list); | |
86 | mutex_unlock(&phy_fixup_lock); | |
87 | ||
88 | return 0; | |
89 | } | |
90 | EXPORT_SYMBOL(phy_register_fixup); | |
91 | ||
92 | /* Registers a fixup to be run on any PHY with the UID in phy_uid */ | |
93 | int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, | |
2f53e904 | 94 | int (*run)(struct phy_device *)) |
f62220d3 AF |
95 | { |
96 | return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run); | |
97 | } | |
98 | EXPORT_SYMBOL(phy_register_fixup_for_uid); | |
99 | ||
100 | /* Registers a fixup to be run on the PHY with id string bus_id */ | |
101 | int phy_register_fixup_for_id(const char *bus_id, | |
2f53e904 | 102 | int (*run)(struct phy_device *)) |
f62220d3 AF |
103 | { |
104 | return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run); | |
105 | } | |
106 | EXPORT_SYMBOL(phy_register_fixup_for_id); | |
107 | ||
2f53e904 | 108 | /* Returns 1 if fixup matches phydev in bus_id and phy_uid. |
f62220d3 AF |
109 | * Fixups can be set to match any in one or more fields. |
110 | */ | |
111 | static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup) | |
112 | { | |
fb28ad35 | 113 | if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0) |
f62220d3 AF |
114 | if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) |
115 | return 0; | |
116 | ||
117 | if ((fixup->phy_uid & fixup->phy_uid_mask) != | |
2f53e904 | 118 | (phydev->phy_id & fixup->phy_uid_mask)) |
f62220d3 AF |
119 | if (fixup->phy_uid != PHY_ANY_UID) |
120 | return 0; | |
121 | ||
122 | return 1; | |
123 | } | |
124 | ||
125 | /* Runs any matching fixups for this phydev */ | |
126 | int phy_scan_fixups(struct phy_device *phydev) | |
127 | { | |
128 | struct phy_fixup *fixup; | |
129 | ||
130 | mutex_lock(&phy_fixup_lock); | |
131 | list_for_each_entry(fixup, &phy_fixup_list, list) { | |
132 | if (phy_needs_fixup(phydev, fixup)) { | |
133 | int err; | |
134 | ||
135 | err = fixup->run(phydev); | |
136 | ||
bc23283c JS |
137 | if (err < 0) { |
138 | mutex_unlock(&phy_fixup_lock); | |
f62220d3 | 139 | return err; |
bc23283c | 140 | } |
f62220d3 AF |
141 | } |
142 | } | |
143 | mutex_unlock(&phy_fixup_lock); | |
144 | ||
145 | return 0; | |
146 | } | |
147 | EXPORT_SYMBOL(phy_scan_fixups); | |
148 | ||
ac28b9f8 | 149 | struct phy_device *phy_device_create(struct mii_bus *bus, int addr, int phy_id, |
2f53e904 SS |
150 | bool is_c45, |
151 | struct phy_c45_device_ids *c45_ids) | |
11b0bacd VB |
152 | { |
153 | struct phy_device *dev; | |
8626d3b4 | 154 | |
2f53e904 | 155 | /* We allocate the device, and initialize the default values */ |
cd861280 | 156 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
11b0bacd VB |
157 | |
158 | if (NULL == dev) | |
e109374f | 159 | return (struct phy_device *)PTR_ERR((void *)-ENOMEM); |
11b0bacd | 160 | |
6f4a7f41 AV |
161 | dev->dev.release = phy_device_release; |
162 | ||
11b0bacd VB |
163 | dev->speed = 0; |
164 | dev->duplex = -1; | |
2f53e904 SS |
165 | dev->pause = 0; |
166 | dev->asym_pause = 0; | |
11b0bacd | 167 | dev->link = 1; |
e8a2b6a4 | 168 | dev->interface = PHY_INTERFACE_MODE_GMII; |
11b0bacd VB |
169 | |
170 | dev->autoneg = AUTONEG_ENABLE; | |
171 | ||
ac28b9f8 | 172 | dev->is_c45 = is_c45; |
11b0bacd VB |
173 | dev->addr = addr; |
174 | dev->phy_id = phy_id; | |
ac28b9f8 DD |
175 | if (c45_ids) |
176 | dev->c45_ids = *c45_ids; | |
11b0bacd | 177 | dev->bus = bus; |
4dea547f GL |
178 | dev->dev.parent = bus->parent; |
179 | dev->dev.bus = &mdio_bus_type; | |
180 | dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL; | |
181 | dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr); | |
11b0bacd VB |
182 | |
183 | dev->state = PHY_DOWN; | |
184 | ||
35b5f6b1 | 185 | mutex_init(&dev->lock); |
4f9c85a1 | 186 | INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); |
5ea94e76 | 187 | INIT_WORK(&dev->phy_queue, phy_change); |
11b0bacd | 188 | |
8626d3b4 | 189 | /* Request the appropriate module unconditionally; don't |
2f53e904 SS |
190 | * bother trying to do so only if it isn't already loaded, |
191 | * because that gets complicated. A hotplug event would have | |
192 | * done an unconditional modprobe anyway. | |
193 | * We don't do normal hotplug because it won't work for MDIO | |
194 | * -- because it relies on the device staying around for long | |
195 | * enough for the driver to get loaded. With MDIO, the NIC | |
196 | * driver will get bored and give up as soon as it finds that | |
197 | * there's no driver _already_ loaded. | |
198 | */ | |
8626d3b4 DW |
199 | request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id)); |
200 | ||
b2a43191 PM |
201 | device_initialize(&dev->dev); |
202 | ||
11b0bacd VB |
203 | return dev; |
204 | } | |
ac28b9f8 DD |
205 | EXPORT_SYMBOL(phy_device_create); |
206 | ||
207 | /** | |
208 | * get_phy_c45_ids - reads the specified addr for its 802.3-c45 IDs. | |
209 | * @bus: the target MII bus | |
210 | * @addr: PHY address on the MII bus | |
211 | * @phy_id: where to store the ID retrieved. | |
212 | * @c45_ids: where to store the c45 ID information. | |
213 | * | |
214 | * If the PHY devices-in-package appears to be valid, it and the | |
215 | * corresponding identifiers are stored in @c45_ids, zero is stored | |
216 | * in @phy_id. Otherwise 0xffffffff is stored in @phy_id. Returns | |
217 | * zero on success. | |
218 | * | |
219 | */ | |
220 | static int get_phy_c45_ids(struct mii_bus *bus, int addr, u32 *phy_id, | |
221 | struct phy_c45_device_ids *c45_ids) { | |
222 | int phy_reg; | |
223 | int i, reg_addr; | |
224 | const int num_ids = ARRAY_SIZE(c45_ids->device_ids); | |
225 | ||
226 | /* Find first non-zero Devices In package. Device | |
227 | * zero is reserved, so don't probe it. | |
228 | */ | |
229 | for (i = 1; | |
230 | i < num_ids && c45_ids->devices_in_package == 0; | |
231 | i++) { | |
232 | reg_addr = MII_ADDR_C45 | i << 16 | 6; | |
233 | phy_reg = mdiobus_read(bus, addr, reg_addr); | |
234 | if (phy_reg < 0) | |
235 | return -EIO; | |
236 | c45_ids->devices_in_package = (phy_reg & 0xffff) << 16; | |
237 | ||
238 | reg_addr = MII_ADDR_C45 | i << 16 | 5; | |
239 | phy_reg = mdiobus_read(bus, addr, reg_addr); | |
240 | if (phy_reg < 0) | |
241 | return -EIO; | |
242 | c45_ids->devices_in_package |= (phy_reg & 0xffff); | |
243 | ||
244 | /* If mostly Fs, there is no device there, | |
245 | * let's get out of here. | |
246 | */ | |
247 | if ((c45_ids->devices_in_package & 0x1fffffff) == 0x1fffffff) { | |
248 | *phy_id = 0xffffffff; | |
249 | return 0; | |
250 | } | |
251 | } | |
252 | ||
253 | /* Now probe Device Identifiers for each device present. */ | |
254 | for (i = 1; i < num_ids; i++) { | |
255 | if (!(c45_ids->devices_in_package & (1 << i))) | |
256 | continue; | |
257 | ||
258 | reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID1; | |
259 | phy_reg = mdiobus_read(bus, addr, reg_addr); | |
260 | if (phy_reg < 0) | |
261 | return -EIO; | |
262 | c45_ids->device_ids[i] = (phy_reg & 0xffff) << 16; | |
263 | ||
264 | reg_addr = MII_ADDR_C45 | i << 16 | MII_PHYSID2; | |
265 | phy_reg = mdiobus_read(bus, addr, reg_addr); | |
266 | if (phy_reg < 0) | |
267 | return -EIO; | |
268 | c45_ids->device_ids[i] |= (phy_reg & 0xffff); | |
269 | } | |
270 | *phy_id = 0; | |
271 | return 0; | |
272 | } | |
11b0bacd | 273 | |
b3df0da8 | 274 | /** |
cac1f3c8 | 275 | * get_phy_id - reads the specified addr for its ID. |
b3df0da8 RD |
276 | * @bus: the target MII bus |
277 | * @addr: PHY address on the MII bus | |
cac1f3c8 | 278 | * @phy_id: where to store the ID retrieved. |
ac28b9f8 DD |
279 | * @is_c45: If true the PHY uses the 802.3 clause 45 protocol |
280 | * @c45_ids: where to store the c45 ID information. | |
281 | * | |
282 | * Description: In the case of a 802.3-c22 PHY, reads the ID registers | |
283 | * of the PHY at @addr on the @bus, stores it in @phy_id and returns | |
284 | * zero on success. | |
285 | * | |
286 | * In the case of a 802.3-c45 PHY, get_phy_c45_ids() is invoked, and | |
287 | * its return value is in turn returned. | |
00db8189 | 288 | * |
00db8189 | 289 | */ |
ac28b9f8 DD |
290 | static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id, |
291 | bool is_c45, struct phy_c45_device_ids *c45_ids) | |
00db8189 AF |
292 | { |
293 | int phy_reg; | |
00db8189 | 294 | |
ac28b9f8 DD |
295 | if (is_c45) |
296 | return get_phy_c45_ids(bus, addr, phy_id, c45_ids); | |
297 | ||
2f53e904 | 298 | /* Grab the bits from PHYIR1, and put them in the upper half */ |
6fe32649 | 299 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); |
00db8189 AF |
300 | |
301 | if (phy_reg < 0) | |
cac1f3c8 | 302 | return -EIO; |
00db8189 | 303 | |
cac1f3c8 | 304 | *phy_id = (phy_reg & 0xffff) << 16; |
00db8189 AF |
305 | |
306 | /* Grab the bits from PHYIR2, and put them in the lower half */ | |
6fe32649 | 307 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); |
00db8189 AF |
308 | |
309 | if (phy_reg < 0) | |
cac1f3c8 PG |
310 | return -EIO; |
311 | ||
312 | *phy_id |= (phy_reg & 0xffff); | |
313 | ||
314 | return 0; | |
315 | } | |
316 | ||
317 | /** | |
2f53e904 SS |
318 | * get_phy_device - reads the specified PHY device and returns its @phy_device |
319 | * struct | |
cac1f3c8 PG |
320 | * @bus: the target MII bus |
321 | * @addr: PHY address on the MII bus | |
ac28b9f8 | 322 | * @is_c45: If true the PHY uses the 802.3 clause 45 protocol |
cac1f3c8 PG |
323 | * |
324 | * Description: Reads the ID registers of the PHY at @addr on the | |
325 | * @bus, then allocates and returns the phy_device to represent it. | |
326 | */ | |
ac28b9f8 | 327 | struct phy_device *get_phy_device(struct mii_bus *bus, int addr, bool is_c45) |
cac1f3c8 | 328 | { |
ac28b9f8 | 329 | struct phy_c45_device_ids c45_ids = {0}; |
160c85f0 | 330 | u32 phy_id = 0; |
cac1f3c8 | 331 | int r; |
00db8189 | 332 | |
ac28b9f8 | 333 | r = get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids); |
cac1f3c8 PG |
334 | if (r) |
335 | return ERR_PTR(r); | |
00db8189 | 336 | |
6436cbcd GC |
337 | /* If the phy_id is mostly Fs, there is no device there */ |
338 | if ((phy_id & 0x1fffffff) == 0x1fffffff) | |
339 | return NULL; | |
340 | ||
e62a768f | 341 | return phy_device_create(bus, addr, phy_id, is_c45, &c45_ids); |
00db8189 | 342 | } |
4dea547f GL |
343 | EXPORT_SYMBOL(get_phy_device); |
344 | ||
345 | /** | |
346 | * phy_device_register - Register the phy device on the MDIO bus | |
1d4ac5d5 | 347 | * @phydev: phy_device structure to be added to the MDIO bus |
4dea547f GL |
348 | */ |
349 | int phy_device_register(struct phy_device *phydev) | |
350 | { | |
351 | int err; | |
352 | ||
2f53e904 | 353 | /* Don't register a phy if one is already registered at this address */ |
4dea547f GL |
354 | if (phydev->bus->phy_map[phydev->addr]) |
355 | return -EINVAL; | |
356 | phydev->bus->phy_map[phydev->addr] = phydev; | |
357 | ||
358 | /* Run all of the fixups for this PHY */ | |
87aa9f9c FF |
359 | err = phy_init_hw(phydev); |
360 | if (err) { | |
361 | pr_err("PHY %d failed to initialize\n", phydev->addr); | |
362 | goto out; | |
363 | } | |
4dea547f | 364 | |
b2a43191 | 365 | err = device_add(&phydev->dev); |
4dea547f | 366 | if (err) { |
b2a43191 | 367 | pr_err("PHY %d failed to add\n", phydev->addr); |
4dea547f GL |
368 | goto out; |
369 | } | |
370 | ||
371 | return 0; | |
372 | ||
373 | out: | |
374 | phydev->bus->phy_map[phydev->addr] = NULL; | |
375 | return err; | |
376 | } | |
377 | EXPORT_SYMBOL(phy_device_register); | |
00db8189 | 378 | |
f8f76db1 JP |
379 | /** |
380 | * phy_find_first - finds the first PHY device on the bus | |
381 | * @bus: the target MII bus | |
382 | */ | |
383 | struct phy_device *phy_find_first(struct mii_bus *bus) | |
384 | { | |
385 | int addr; | |
386 | ||
387 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { | |
388 | if (bus->phy_map[addr]) | |
389 | return bus->phy_map[addr]; | |
390 | } | |
391 | return NULL; | |
392 | } | |
393 | EXPORT_SYMBOL(phy_find_first); | |
394 | ||
b3df0da8 RD |
395 | /** |
396 | * phy_prepare_link - prepares the PHY layer to monitor link status | |
397 | * @phydev: target phy_device struct | |
398 | * @handler: callback function for link status change notifications | |
00db8189 | 399 | * |
b3df0da8 | 400 | * Description: Tells the PHY infrastructure to handle the |
00db8189 AF |
401 | * gory details on monitoring link status (whether through |
402 | * polling or an interrupt), and to call back to the | |
403 | * connected device driver when the link status changes. | |
404 | * If you want to monitor your own link state, don't call | |
b3df0da8 RD |
405 | * this function. |
406 | */ | |
89ff05ec | 407 | static void phy_prepare_link(struct phy_device *phydev, |
2f53e904 | 408 | void (*handler)(struct net_device *)) |
00db8189 AF |
409 | { |
410 | phydev->adjust_link = handler; | |
411 | } | |
412 | ||
fa94f6d9 GL |
413 | /** |
414 | * phy_connect_direct - connect an ethernet device to a specific phy_device | |
415 | * @dev: the network device to connect | |
416 | * @phydev: the pointer to the phy device | |
417 | * @handler: callback function for state change notifications | |
fa94f6d9 GL |
418 | * @interface: PHY device's interface |
419 | */ | |
420 | int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, | |
f9a8f83b | 421 | void (*handler)(struct net_device *), |
fa94f6d9 GL |
422 | phy_interface_t interface) |
423 | { | |
424 | int rc; | |
425 | ||
f9a8f83b | 426 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
fa94f6d9 GL |
427 | if (rc) |
428 | return rc; | |
429 | ||
430 | phy_prepare_link(phydev, handler); | |
431 | phy_start_machine(phydev, NULL); | |
432 | if (phydev->irq > 0) | |
433 | phy_start_interrupts(phydev); | |
434 | ||
435 | return 0; | |
436 | } | |
437 | EXPORT_SYMBOL(phy_connect_direct); | |
438 | ||
b3df0da8 RD |
439 | /** |
440 | * phy_connect - connect an ethernet device to a PHY device | |
441 | * @dev: the network device to connect | |
5d12b132 | 442 | * @bus_id: the id string of the PHY device to connect |
b3df0da8 | 443 | * @handler: callback function for state change notifications |
b3df0da8 | 444 | * @interface: PHY device's interface |
e1393456 | 445 | * |
b3df0da8 | 446 | * Description: Convenience function for connecting ethernet |
e1393456 AF |
447 | * devices to PHY devices. The default behavior is for |
448 | * the PHY infrastructure to handle everything, and only notify | |
449 | * the connected driver when the link status changes. If you | |
450 | * don't want, or can't use the provided functionality, you may | |
451 | * choose to call only the subset of functions which provide | |
452 | * the desired functionality. | |
453 | */ | |
e109374f | 454 | struct phy_device *phy_connect(struct net_device *dev, const char *bus_id, |
2f53e904 SS |
455 | void (*handler)(struct net_device *), |
456 | phy_interface_t interface) | |
e1393456 AF |
457 | { |
458 | struct phy_device *phydev; | |
fa94f6d9 GL |
459 | struct device *d; |
460 | int rc; | |
e1393456 | 461 | |
fa94f6d9 | 462 | /* Search the list of PHY devices on the mdio bus for the |
2f53e904 SS |
463 | * PHY with the requested name |
464 | */ | |
fa94f6d9 GL |
465 | d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); |
466 | if (!d) { | |
467 | pr_err("PHY %s not found\n", bus_id); | |
468 | return ERR_PTR(-ENODEV); | |
469 | } | |
470 | phydev = to_phy_device(d); | |
e1393456 | 471 | |
f9a8f83b | 472 | rc = phy_connect_direct(dev, phydev, handler, interface); |
fa94f6d9 GL |
473 | if (rc) |
474 | return ERR_PTR(rc); | |
e1393456 AF |
475 | |
476 | return phydev; | |
477 | } | |
478 | EXPORT_SYMBOL(phy_connect); | |
479 | ||
b3df0da8 | 480 | /** |
2f53e904 SS |
481 | * phy_disconnect - disable interrupts, stop state machine, and detach a PHY |
482 | * device | |
b3df0da8 RD |
483 | * @phydev: target phy_device struct |
484 | */ | |
e1393456 AF |
485 | void phy_disconnect(struct phy_device *phydev) |
486 | { | |
487 | if (phydev->irq > 0) | |
488 | phy_stop_interrupts(phydev); | |
489 | ||
490 | phy_stop_machine(phydev); | |
e109374f | 491 | |
e1393456 AF |
492 | phydev->adjust_link = NULL; |
493 | ||
494 | phy_detach(phydev); | |
495 | } | |
496 | EXPORT_SYMBOL(phy_disconnect); | |
497 | ||
87aa9f9c FF |
498 | /** |
499 | * phy_poll_reset - Safely wait until a PHY reset has properly completed | |
500 | * @phydev: The PHY device to poll | |
501 | * | |
502 | * Description: According to IEEE 802.3, Section 2, Subsection 22.2.4.1.1, as | |
503 | * published in 2008, a PHY reset may take up to 0.5 seconds. The MII BMCR | |
504 | * register must be polled until the BMCR_RESET bit clears. | |
505 | * | |
506 | * Furthermore, any attempts to write to PHY registers may have no effect | |
507 | * or even generate MDIO bus errors until this is complete. | |
508 | * | |
509 | * Some PHYs (such as the Marvell 88E1111) don't entirely conform to the | |
510 | * standard and do not fully reset after the BMCR_RESET bit is set, and may | |
511 | * even *REQUIRE* a soft-reset to properly restart autonegotiation. In an | |
512 | * effort to support such broken PHYs, this function is separate from the | |
513 | * standard phy_init_hw() which will zero all the other bits in the BMCR | |
514 | * and reapply all driver-specific and board-specific fixups. | |
515 | */ | |
516 | static int phy_poll_reset(struct phy_device *phydev) | |
517 | { | |
518 | /* Poll until the reset bit clears (50ms per retry == 0.6 sec) */ | |
519 | unsigned int retries = 12; | |
520 | int ret; | |
521 | ||
522 | do { | |
523 | msleep(50); | |
524 | ret = phy_read(phydev, MII_BMCR); | |
525 | if (ret < 0) | |
526 | return ret; | |
527 | } while (ret & BMCR_RESET && --retries); | |
528 | if (ret & BMCR_RESET) | |
529 | return -ETIMEDOUT; | |
530 | ||
2f53e904 | 531 | /* Some chips (smsc911x) may still need up to another 1ms after the |
87aa9f9c FF |
532 | * BMCR_RESET bit is cleared before they are usable. |
533 | */ | |
534 | msleep(1); | |
535 | return 0; | |
536 | } | |
537 | ||
2f5cb434 AV |
538 | int phy_init_hw(struct phy_device *phydev) |
539 | { | |
540 | int ret; | |
541 | ||
542 | if (!phydev->drv || !phydev->drv->config_init) | |
543 | return 0; | |
544 | ||
87aa9f9c FF |
545 | ret = phy_write(phydev, MII_BMCR, BMCR_RESET); |
546 | if (ret < 0) | |
547 | return ret; | |
548 | ||
549 | ret = phy_poll_reset(phydev); | |
550 | if (ret < 0) | |
551 | return ret; | |
552 | ||
2f5cb434 AV |
553 | ret = phy_scan_fixups(phydev); |
554 | if (ret < 0) | |
555 | return ret; | |
556 | ||
557 | return phydev->drv->config_init(phydev); | |
558 | } | |
87aa9f9c | 559 | EXPORT_SYMBOL(phy_init_hw); |
2f5cb434 | 560 | |
b3df0da8 | 561 | /** |
fa94f6d9 | 562 | * phy_attach_direct - attach a network device to a given PHY device pointer |
b3df0da8 | 563 | * @dev: network device to attach |
fa94f6d9 | 564 | * @phydev: Pointer to phy_device to attach |
b3df0da8 RD |
565 | * @flags: PHY device's dev_flags |
566 | * @interface: PHY device's interface | |
e1393456 | 567 | * |
b3df0da8 | 568 | * Description: Called by drivers to attach to a particular PHY |
e1393456 AF |
569 | * device. The phy_device is found, and properly hooked up |
570 | * to the phy_driver. If no driver is attached, then the | |
571 | * genphy_driver is used. The phy_device is given a ptr to | |
572 | * the attaching device, and given a callback for link status | |
b3df0da8 | 573 | * change. The phy_device is returned to the attaching driver. |
e1393456 | 574 | */ |
89ff05ec | 575 | static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
576 | u32 flags, phy_interface_t interface) | |
e1393456 | 577 | { |
fa94f6d9 | 578 | struct device *d = &phydev->dev; |
d005a09e | 579 | int err; |
e1393456 AF |
580 | |
581 | /* Assume that if there is no driver, that it doesn't | |
2f53e904 SS |
582 | * exist, and we should use the genphy driver. |
583 | */ | |
e1393456 | 584 | if (NULL == d->driver) { |
ac28b9f8 DD |
585 | if (phydev->is_c45) { |
586 | pr_err("No driver for phy %x\n", phydev->phy_id); | |
587 | return -ENODEV; | |
588 | } | |
589 | ||
e1393456 AF |
590 | d->driver = &genphy_driver.driver; |
591 | ||
592 | err = d->driver->probe(d); | |
b7a00ecd JG |
593 | if (err >= 0) |
594 | err = device_bind_driver(d); | |
e1393456 | 595 | |
b7a00ecd | 596 | if (err) |
fa94f6d9 | 597 | return err; |
e1393456 AF |
598 | } |
599 | ||
600 | if (phydev->attached_dev) { | |
fa94f6d9 GL |
601 | dev_err(&dev->dev, "PHY already attached\n"); |
602 | return -EBUSY; | |
e1393456 AF |
603 | } |
604 | ||
605 | phydev->attached_dev = dev; | |
c1f19b51 | 606 | dev->phydev = phydev; |
e1393456 AF |
607 | |
608 | phydev->dev_flags = flags; | |
609 | ||
e8a2b6a4 AF |
610 | phydev->interface = interface; |
611 | ||
ef24b16b AV |
612 | phydev->state = PHY_READY; |
613 | ||
e8a2b6a4 AF |
614 | /* Do initial configuration here, now that |
615 | * we have certain key parameters | |
2f53e904 SS |
616 | * (dev_flags and interface) |
617 | */ | |
d005a09e MKB |
618 | err = phy_init_hw(phydev); |
619 | if (err) | |
620 | phy_detach(phydev); | |
621 | ||
1211ce53 SH |
622 | phy_resume(phydev); |
623 | ||
d005a09e | 624 | return err; |
fa94f6d9 | 625 | } |
fa94f6d9 GL |
626 | |
627 | /** | |
628 | * phy_attach - attach a network device to a particular PHY device | |
629 | * @dev: network device to attach | |
630 | * @bus_id: Bus ID of PHY device to attach | |
fa94f6d9 GL |
631 | * @interface: PHY device's interface |
632 | * | |
633 | * Description: Same as phy_attach_direct() except that a PHY bus_id | |
634 | * string is passed instead of a pointer to a struct phy_device. | |
635 | */ | |
2f53e904 SS |
636 | struct phy_device *phy_attach(struct net_device *dev, const char *bus_id, |
637 | phy_interface_t interface) | |
fa94f6d9 GL |
638 | { |
639 | struct bus_type *bus = &mdio_bus_type; | |
640 | struct phy_device *phydev; | |
641 | struct device *d; | |
642 | int rc; | |
643 | ||
644 | /* Search the list of PHY devices on the mdio bus for the | |
2f53e904 SS |
645 | * PHY with the requested name |
646 | */ | |
fa94f6d9 GL |
647 | d = bus_find_device_by_name(bus, NULL, bus_id); |
648 | if (!d) { | |
649 | pr_err("PHY %s not found\n", bus_id); | |
650 | return ERR_PTR(-ENODEV); | |
e8a2b6a4 | 651 | } |
fa94f6d9 GL |
652 | phydev = to_phy_device(d); |
653 | ||
f9a8f83b | 654 | rc = phy_attach_direct(dev, phydev, phydev->dev_flags, interface); |
fa94f6d9 GL |
655 | if (rc) |
656 | return ERR_PTR(rc); | |
e8a2b6a4 | 657 | |
e1393456 AF |
658 | return phydev; |
659 | } | |
660 | EXPORT_SYMBOL(phy_attach); | |
661 | ||
b3df0da8 RD |
662 | /** |
663 | * phy_detach - detach a PHY device from its network device | |
664 | * @phydev: target phy_device struct | |
665 | */ | |
e1393456 AF |
666 | void phy_detach(struct phy_device *phydev) |
667 | { | |
c1f19b51 | 668 | phydev->attached_dev->phydev = NULL; |
e1393456 | 669 | phydev->attached_dev = NULL; |
1211ce53 | 670 | phy_suspend(phydev); |
e1393456 AF |
671 | |
672 | /* If the device had no specific driver before (i.e. - it | |
673 | * was using the generic driver), we unbind the device | |
674 | * from the generic driver so that there's a chance a | |
2f53e904 SS |
675 | * real driver could be loaded |
676 | */ | |
87aebe07 | 677 | if (phydev->dev.driver == &genphy_driver.driver) |
e1393456 | 678 | device_release_driver(&phydev->dev); |
e1393456 AF |
679 | } |
680 | EXPORT_SYMBOL(phy_detach); | |
681 | ||
481b5d93 SH |
682 | int phy_suspend(struct phy_device *phydev) |
683 | { | |
684 | struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver); | |
685 | struct ethtool_wolinfo wol; | |
686 | ||
687 | /* If the device has WOL enabled, we cannot suspend the PHY */ | |
688 | wol.cmd = ETHTOOL_GWOL; | |
689 | phy_ethtool_get_wol(phydev, &wol); | |
690 | if (wol.wolopts) | |
691 | return -EBUSY; | |
692 | ||
693 | if (phydrv->suspend) | |
694 | return phydrv->suspend(phydev); | |
695 | return 0; | |
696 | } | |
697 | ||
698 | int phy_resume(struct phy_device *phydev) | |
699 | { | |
700 | struct phy_driver *phydrv = to_phy_driver(phydev->dev.driver); | |
701 | ||
702 | if (phydrv->resume) | |
703 | return phydrv->resume(phydev); | |
704 | return 0; | |
705 | } | |
e1393456 | 706 | |
00db8189 AF |
707 | /* Generic PHY support and helper functions */ |
708 | ||
b3df0da8 | 709 | /** |
25985edc | 710 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
b3df0da8 | 711 | * @phydev: target phy_device struct |
00db8189 | 712 | * |
b3df0da8 | 713 | * Description: Writes MII_ADVERTISE with the appropriate values, |
00db8189 | 714 | * after sanitizing the values to make sure we only advertise |
51e2a384 TP |
715 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
716 | * hasn't changed, and > 0 if it has changed. | |
00db8189 | 717 | */ |
89ff05ec | 718 | static int genphy_config_advert(struct phy_device *phydev) |
00db8189 AF |
719 | { |
720 | u32 advertise; | |
51e2a384 TP |
721 | int oldadv, adv; |
722 | int err, changed = 0; | |
00db8189 | 723 | |
2f53e904 | 724 | /* Only allow advertising what this PHY supports */ |
00db8189 AF |
725 | phydev->advertising &= phydev->supported; |
726 | advertise = phydev->advertising; | |
727 | ||
728 | /* Setup standard advertisement */ | |
2f53e904 | 729 | adv = phy_read(phydev, MII_ADVERTISE); |
00db8189 AF |
730 | |
731 | if (adv < 0) | |
732 | return adv; | |
733 | ||
2f53e904 | 734 | oldadv = adv; |
28011cf1 | 735 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | |
00db8189 | 736 | ADVERTISE_PAUSE_ASYM); |
37f07023 | 737 | adv |= ethtool_adv_to_mii_adv_t(advertise); |
00db8189 | 738 | |
51e2a384 TP |
739 | if (adv != oldadv) { |
740 | err = phy_write(phydev, MII_ADVERTISE, adv); | |
00db8189 | 741 | |
51e2a384 TP |
742 | if (err < 0) |
743 | return err; | |
744 | changed = 1; | |
745 | } | |
00db8189 AF |
746 | |
747 | /* Configure gigabit if it's supported */ | |
748 | if (phydev->supported & (SUPPORTED_1000baseT_Half | | |
2f53e904 SS |
749 | SUPPORTED_1000baseT_Full)) { |
750 | adv = phy_read(phydev, MII_CTRL1000); | |
00db8189 AF |
751 | |
752 | if (adv < 0) | |
753 | return adv; | |
754 | ||
2f53e904 | 755 | oldadv = adv; |
00db8189 | 756 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
37f07023 | 757 | adv |= ethtool_adv_to_mii_ctrl1000_t(advertise); |
00db8189 | 758 | |
51e2a384 TP |
759 | if (adv != oldadv) { |
760 | err = phy_write(phydev, MII_CTRL1000, adv); | |
761 | ||
762 | if (err < 0) | |
763 | return err; | |
764 | changed = 1; | |
765 | } | |
00db8189 AF |
766 | } |
767 | ||
51e2a384 | 768 | return changed; |
00db8189 | 769 | } |
00db8189 | 770 | |
b3df0da8 RD |
771 | /** |
772 | * genphy_setup_forced - configures/forces speed/duplex from @phydev | |
773 | * @phydev: target phy_device struct | |
00db8189 | 774 | * |
b3df0da8 | 775 | * Description: Configures MII_BMCR to force speed/duplex |
00db8189 | 776 | * to the values in phydev. Assumes that the values are valid. |
b3df0da8 RD |
777 | * Please see phy_sanitize_settings(). |
778 | */ | |
3fb69bca | 779 | int genphy_setup_forced(struct phy_device *phydev) |
00db8189 | 780 | { |
bc1e0a09 | 781 | int ctl = 0; |
00db8189 | 782 | |
2f53e904 SS |
783 | phydev->pause = 0; |
784 | phydev->asym_pause = 0; | |
00db8189 AF |
785 | |
786 | if (SPEED_1000 == phydev->speed) | |
787 | ctl |= BMCR_SPEED1000; | |
788 | else if (SPEED_100 == phydev->speed) | |
789 | ctl |= BMCR_SPEED100; | |
790 | ||
791 | if (DUPLEX_FULL == phydev->duplex) | |
792 | ctl |= BMCR_FULLDPLX; | |
e109374f | 793 | |
e62a768f | 794 | return phy_write(phydev, MII_BMCR, ctl); |
00db8189 | 795 | } |
3fb69bca | 796 | EXPORT_SYMBOL(genphy_setup_forced); |
00db8189 | 797 | |
b3df0da8 RD |
798 | /** |
799 | * genphy_restart_aneg - Enable and Restart Autonegotiation | |
800 | * @phydev: target phy_device struct | |
801 | */ | |
00db8189 AF |
802 | int genphy_restart_aneg(struct phy_device *phydev) |
803 | { | |
804 | int ctl; | |
805 | ||
806 | ctl = phy_read(phydev, MII_BMCR); | |
807 | ||
808 | if (ctl < 0) | |
809 | return ctl; | |
810 | ||
2f53e904 | 811 | ctl |= BMCR_ANENABLE | BMCR_ANRESTART; |
00db8189 AF |
812 | |
813 | /* Don't isolate the PHY if we're negotiating */ | |
2f53e904 | 814 | ctl &= ~BMCR_ISOLATE; |
00db8189 AF |
815 | |
816 | ctl = phy_write(phydev, MII_BMCR, ctl); | |
817 | ||
818 | return ctl; | |
819 | } | |
892871dc | 820 | EXPORT_SYMBOL(genphy_restart_aneg); |
00db8189 AF |
821 | |
822 | ||
b3df0da8 RD |
823 | /** |
824 | * genphy_config_aneg - restart auto-negotiation or write BMCR | |
825 | * @phydev: target phy_device struct | |
00db8189 | 826 | * |
b3df0da8 | 827 | * Description: If auto-negotiation is enabled, we configure the |
00db8189 | 828 | * advertising, and then restart auto-negotiation. If it is not |
b3df0da8 | 829 | * enabled, then we write the BMCR. |
00db8189 AF |
830 | */ |
831 | int genphy_config_aneg(struct phy_device *phydev) | |
832 | { | |
de339c2a | 833 | int result; |
00db8189 | 834 | |
de339c2a TP |
835 | if (AUTONEG_ENABLE != phydev->autoneg) |
836 | return genphy_setup_forced(phydev); | |
00db8189 | 837 | |
de339c2a | 838 | result = genphy_config_advert(phydev); |
00db8189 | 839 | |
de339c2a TP |
840 | if (result < 0) /* error */ |
841 | return result; | |
842 | ||
843 | if (result == 0) { | |
25985edc | 844 | /* Advertisement hasn't changed, but maybe aneg was never on to |
2f53e904 SS |
845 | * begin with? Or maybe phy was isolated? |
846 | */ | |
de339c2a TP |
847 | int ctl = phy_read(phydev, MII_BMCR); |
848 | ||
849 | if (ctl < 0) | |
850 | return ctl; | |
851 | ||
852 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) | |
853 | result = 1; /* do restart aneg */ | |
854 | } | |
855 | ||
856 | /* Only restart aneg if we are advertising something different | |
2f53e904 SS |
857 | * than we were before. |
858 | */ | |
de339c2a TP |
859 | if (result > 0) |
860 | result = genphy_restart_aneg(phydev); | |
00db8189 | 861 | |
51e2a384 | 862 | return result; |
00db8189 AF |
863 | } |
864 | EXPORT_SYMBOL(genphy_config_aneg); | |
865 | ||
b3df0da8 RD |
866 | /** |
867 | * genphy_update_link - update link status in @phydev | |
868 | * @phydev: target phy_device struct | |
00db8189 | 869 | * |
b3df0da8 | 870 | * Description: Update the value in phydev->link to reflect the |
00db8189 | 871 | * current link value. In order to do this, we need to read |
b3df0da8 | 872 | * the status register twice, keeping the second value. |
00db8189 AF |
873 | */ |
874 | int genphy_update_link(struct phy_device *phydev) | |
875 | { | |
876 | int status; | |
877 | ||
878 | /* Do a fake read */ | |
879 | status = phy_read(phydev, MII_BMSR); | |
880 | ||
881 | if (status < 0) | |
882 | return status; | |
883 | ||
884 | /* Read link and autonegotiation status */ | |
885 | status = phy_read(phydev, MII_BMSR); | |
886 | ||
887 | if (status < 0) | |
888 | return status; | |
889 | ||
890 | if ((status & BMSR_LSTATUS) == 0) | |
891 | phydev->link = 0; | |
892 | else | |
893 | phydev->link = 1; | |
894 | ||
895 | return 0; | |
896 | } | |
6b655529 | 897 | EXPORT_SYMBOL(genphy_update_link); |
00db8189 | 898 | |
b3df0da8 RD |
899 | /** |
900 | * genphy_read_status - check the link status and update current link state | |
901 | * @phydev: target phy_device struct | |
00db8189 | 902 | * |
b3df0da8 | 903 | * Description: Check the link, then figure out the current state |
00db8189 AF |
904 | * by comparing what we advertise with what the link partner |
905 | * advertises. Start by checking the gigabit possibilities, | |
906 | * then move on to 10/100. | |
907 | */ | |
908 | int genphy_read_status(struct phy_device *phydev) | |
909 | { | |
910 | int adv; | |
911 | int err; | |
912 | int lpa; | |
913 | int lpagb = 0; | |
914 | ||
2f53e904 | 915 | /* Update the link, but return if there was an error */ |
00db8189 AF |
916 | err = genphy_update_link(phydev); |
917 | if (err) | |
918 | return err; | |
919 | ||
114002bc FF |
920 | phydev->lp_advertising = 0; |
921 | ||
00db8189 AF |
922 | if (AUTONEG_ENABLE == phydev->autoneg) { |
923 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
924 | | SUPPORTED_1000baseT_Full)) { | |
925 | lpagb = phy_read(phydev, MII_STAT1000); | |
926 | ||
927 | if (lpagb < 0) | |
928 | return lpagb; | |
929 | ||
930 | adv = phy_read(phydev, MII_CTRL1000); | |
931 | ||
932 | if (adv < 0) | |
933 | return adv; | |
934 | ||
114002bc FF |
935 | phydev->lp_advertising = |
936 | mii_stat1000_to_ethtool_lpa_t(lpagb); | |
00db8189 AF |
937 | lpagb &= adv << 2; |
938 | } | |
939 | ||
940 | lpa = phy_read(phydev, MII_LPA); | |
941 | ||
942 | if (lpa < 0) | |
943 | return lpa; | |
944 | ||
114002bc FF |
945 | phydev->lp_advertising |= mii_lpa_to_ethtool_lpa_t(lpa); |
946 | ||
00db8189 AF |
947 | adv = phy_read(phydev, MII_ADVERTISE); |
948 | ||
949 | if (adv < 0) | |
950 | return adv; | |
951 | ||
952 | lpa &= adv; | |
953 | ||
954 | phydev->speed = SPEED_10; | |
955 | phydev->duplex = DUPLEX_HALF; | |
2f53e904 SS |
956 | phydev->pause = 0; |
957 | phydev->asym_pause = 0; | |
00db8189 AF |
958 | |
959 | if (lpagb & (LPA_1000FULL | LPA_1000HALF)) { | |
960 | phydev->speed = SPEED_1000; | |
961 | ||
962 | if (lpagb & LPA_1000FULL) | |
963 | phydev->duplex = DUPLEX_FULL; | |
964 | } else if (lpa & (LPA_100FULL | LPA_100HALF)) { | |
965 | phydev->speed = SPEED_100; | |
e109374f | 966 | |
00db8189 AF |
967 | if (lpa & LPA_100FULL) |
968 | phydev->duplex = DUPLEX_FULL; | |
969 | } else | |
970 | if (lpa & LPA_10FULL) | |
971 | phydev->duplex = DUPLEX_FULL; | |
972 | ||
e109374f | 973 | if (phydev->duplex == DUPLEX_FULL) { |
00db8189 AF |
974 | phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0; |
975 | phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0; | |
976 | } | |
977 | } else { | |
978 | int bmcr = phy_read(phydev, MII_BMCR); | |
2f53e904 | 979 | |
00db8189 AF |
980 | if (bmcr < 0) |
981 | return bmcr; | |
982 | ||
983 | if (bmcr & BMCR_FULLDPLX) | |
984 | phydev->duplex = DUPLEX_FULL; | |
985 | else | |
986 | phydev->duplex = DUPLEX_HALF; | |
987 | ||
988 | if (bmcr & BMCR_SPEED1000) | |
989 | phydev->speed = SPEED_1000; | |
990 | else if (bmcr & BMCR_SPEED100) | |
991 | phydev->speed = SPEED_100; | |
992 | else | |
993 | phydev->speed = SPEED_10; | |
994 | ||
2f53e904 SS |
995 | phydev->pause = 0; |
996 | phydev->asym_pause = 0; | |
00db8189 AF |
997 | } |
998 | ||
999 | return 0; | |
1000 | } | |
1001 | EXPORT_SYMBOL(genphy_read_status); | |
1002 | ||
1003 | static int genphy_config_init(struct phy_device *phydev) | |
1004 | { | |
84c22d79 | 1005 | int val; |
00db8189 AF |
1006 | u32 features; |
1007 | ||
1008 | /* For now, I'll claim that the generic driver supports | |
2f53e904 SS |
1009 | * all possible port types |
1010 | */ | |
00db8189 AF |
1011 | features = (SUPPORTED_TP | SUPPORTED_MII |
1012 | | SUPPORTED_AUI | SUPPORTED_FIBRE | | |
1013 | SUPPORTED_BNC); | |
1014 | ||
1015 | /* Do we support autonegotiation? */ | |
1016 | val = phy_read(phydev, MII_BMSR); | |
1017 | ||
1018 | if (val < 0) | |
1019 | return val; | |
1020 | ||
1021 | if (val & BMSR_ANEGCAPABLE) | |
1022 | features |= SUPPORTED_Autoneg; | |
1023 | ||
1024 | if (val & BMSR_100FULL) | |
1025 | features |= SUPPORTED_100baseT_Full; | |
1026 | if (val & BMSR_100HALF) | |
1027 | features |= SUPPORTED_100baseT_Half; | |
1028 | if (val & BMSR_10FULL) | |
1029 | features |= SUPPORTED_10baseT_Full; | |
1030 | if (val & BMSR_10HALF) | |
1031 | features |= SUPPORTED_10baseT_Half; | |
1032 | ||
1033 | if (val & BMSR_ESTATEN) { | |
1034 | val = phy_read(phydev, MII_ESTATUS); | |
1035 | ||
1036 | if (val < 0) | |
1037 | return val; | |
1038 | ||
1039 | if (val & ESTATUS_1000_TFULL) | |
1040 | features |= SUPPORTED_1000baseT_Full; | |
1041 | if (val & ESTATUS_1000_THALF) | |
1042 | features |= SUPPORTED_1000baseT_Half; | |
1043 | } | |
1044 | ||
1045 | phydev->supported = features; | |
1046 | phydev->advertising = features; | |
1047 | ||
1048 | return 0; | |
1049 | } | |
0f0ca340 GC |
1050 | int genphy_suspend(struct phy_device *phydev) |
1051 | { | |
1052 | int value; | |
1053 | ||
1054 | mutex_lock(&phydev->lock); | |
1055 | ||
1056 | value = phy_read(phydev, MII_BMCR); | |
2f53e904 | 1057 | phy_write(phydev, MII_BMCR, value | BMCR_PDOWN); |
0f0ca340 GC |
1058 | |
1059 | mutex_unlock(&phydev->lock); | |
1060 | ||
1061 | return 0; | |
1062 | } | |
1063 | EXPORT_SYMBOL(genphy_suspend); | |
00db8189 | 1064 | |
0f0ca340 GC |
1065 | int genphy_resume(struct phy_device *phydev) |
1066 | { | |
1067 | int value; | |
1068 | ||
1069 | mutex_lock(&phydev->lock); | |
1070 | ||
1071 | value = phy_read(phydev, MII_BMCR); | |
2f53e904 | 1072 | phy_write(phydev, MII_BMCR, value & ~BMCR_PDOWN); |
0f0ca340 GC |
1073 | |
1074 | mutex_unlock(&phydev->lock); | |
1075 | ||
1076 | return 0; | |
1077 | } | |
1078 | EXPORT_SYMBOL(genphy_resume); | |
00db8189 | 1079 | |
b3df0da8 RD |
1080 | /** |
1081 | * phy_probe - probe and init a PHY device | |
1082 | * @dev: device to probe and init | |
00db8189 | 1083 | * |
b3df0da8 | 1084 | * Description: Take care of setting up the phy_device structure, |
00db8189 AF |
1085 | * set the state to READY (the driver's init function should |
1086 | * set it to STARTING if needed). | |
1087 | */ | |
1088 | static int phy_probe(struct device *dev) | |
1089 | { | |
1090 | struct phy_device *phydev; | |
1091 | struct phy_driver *phydrv; | |
1092 | struct device_driver *drv; | |
1093 | int err = 0; | |
1094 | ||
1095 | phydev = to_phy_device(dev); | |
1096 | ||
f3ff9247 | 1097 | drv = phydev->dev.driver; |
00db8189 AF |
1098 | phydrv = to_phy_driver(drv); |
1099 | phydev->drv = phydrv; | |
1100 | ||
2c7b4921 FF |
1101 | /* Disable the interrupt if the PHY doesn't support it |
1102 | * but the interrupt is still a valid one | |
1103 | */ | |
1104 | if (!(phydrv->flags & PHY_HAS_INTERRUPT) && | |
2f53e904 | 1105 | phy_interrupt_is_valid(phydev)) |
00db8189 AF |
1106 | phydev->irq = PHY_POLL; |
1107 | ||
4284b6a5 FF |
1108 | if (phydrv->flags & PHY_IS_INTERNAL) |
1109 | phydev->is_internal = true; | |
1110 | ||
35b5f6b1 | 1111 | mutex_lock(&phydev->lock); |
00db8189 AF |
1112 | |
1113 | /* Start out supporting everything. Eventually, | |
1114 | * a controller will attach, and may modify one | |
2f53e904 SS |
1115 | * or both of these values |
1116 | */ | |
00db8189 AF |
1117 | phydev->supported = phydrv->features; |
1118 | phydev->advertising = phydrv->features; | |
1119 | ||
1120 | /* Set the state to READY by default */ | |
1121 | phydev->state = PHY_READY; | |
1122 | ||
1123 | if (phydev->drv->probe) | |
1124 | err = phydev->drv->probe(phydev); | |
1125 | ||
35b5f6b1 | 1126 | mutex_unlock(&phydev->lock); |
00db8189 | 1127 | |
00db8189 AF |
1128 | return err; |
1129 | } | |
1130 | ||
1131 | static int phy_remove(struct device *dev) | |
1132 | { | |
1133 | struct phy_device *phydev; | |
1134 | ||
1135 | phydev = to_phy_device(dev); | |
1136 | ||
35b5f6b1 | 1137 | mutex_lock(&phydev->lock); |
00db8189 | 1138 | phydev->state = PHY_DOWN; |
35b5f6b1 | 1139 | mutex_unlock(&phydev->lock); |
00db8189 AF |
1140 | |
1141 | if (phydev->drv->remove) | |
1142 | phydev->drv->remove(phydev); | |
00db8189 AF |
1143 | phydev->drv = NULL; |
1144 | ||
1145 | return 0; | |
1146 | } | |
1147 | ||
b3df0da8 RD |
1148 | /** |
1149 | * phy_driver_register - register a phy_driver with the PHY layer | |
1150 | * @new_driver: new phy_driver to register | |
1151 | */ | |
00db8189 AF |
1152 | int phy_driver_register(struct phy_driver *new_driver) |
1153 | { | |
1154 | int retval; | |
1155 | ||
00db8189 AF |
1156 | new_driver->driver.name = new_driver->name; |
1157 | new_driver->driver.bus = &mdio_bus_type; | |
1158 | new_driver->driver.probe = phy_probe; | |
1159 | new_driver->driver.remove = phy_remove; | |
1160 | ||
1161 | retval = driver_register(&new_driver->driver); | |
1162 | ||
1163 | if (retval) { | |
8d242488 JP |
1164 | pr_err("%s: Error %d in registering driver\n", |
1165 | new_driver->name, retval); | |
00db8189 AF |
1166 | |
1167 | return retval; | |
1168 | } | |
1169 | ||
f2511f13 | 1170 | pr_debug("%s: Registered new driver\n", new_driver->name); |
00db8189 AF |
1171 | |
1172 | return 0; | |
1173 | } | |
1174 | EXPORT_SYMBOL(phy_driver_register); | |
1175 | ||
d5bf9071 CH |
1176 | int phy_drivers_register(struct phy_driver *new_driver, int n) |
1177 | { | |
1178 | int i, ret = 0; | |
1179 | ||
1180 | for (i = 0; i < n; i++) { | |
1181 | ret = phy_driver_register(new_driver + i); | |
1182 | if (ret) { | |
1183 | while (i-- > 0) | |
1184 | phy_driver_unregister(new_driver + i); | |
1185 | break; | |
1186 | } | |
1187 | } | |
1188 | return ret; | |
1189 | } | |
1190 | EXPORT_SYMBOL(phy_drivers_register); | |
1191 | ||
00db8189 AF |
1192 | void phy_driver_unregister(struct phy_driver *drv) |
1193 | { | |
1194 | driver_unregister(&drv->driver); | |
1195 | } | |
1196 | EXPORT_SYMBOL(phy_driver_unregister); | |
1197 | ||
d5bf9071 CH |
1198 | void phy_drivers_unregister(struct phy_driver *drv, int n) |
1199 | { | |
1200 | int i; | |
2f53e904 SS |
1201 | |
1202 | for (i = 0; i < n; i++) | |
d5bf9071 | 1203 | phy_driver_unregister(drv + i); |
d5bf9071 CH |
1204 | } |
1205 | EXPORT_SYMBOL(phy_drivers_unregister); | |
1206 | ||
e1393456 AF |
1207 | static struct phy_driver genphy_driver = { |
1208 | .phy_id = 0xffffffff, | |
1209 | .phy_id_mask = 0xffffffff, | |
1210 | .name = "Generic PHY", | |
1211 | .config_init = genphy_config_init, | |
1212 | .features = 0, | |
1213 | .config_aneg = genphy_config_aneg, | |
1214 | .read_status = genphy_read_status, | |
0f0ca340 GC |
1215 | .suspend = genphy_suspend, |
1216 | .resume = genphy_resume, | |
e109374f | 1217 | .driver = { .owner = THIS_MODULE, }, |
e1393456 | 1218 | }; |
00db8189 | 1219 | |
67c4f3fa | 1220 | static int __init phy_init(void) |
00db8189 | 1221 | { |
67c4f3fa | 1222 | int rc; |
67c4f3fa JG |
1223 | |
1224 | rc = mdio_bus_init(); | |
1225 | if (rc) | |
e1393456 | 1226 | return rc; |
00db8189 | 1227 | |
e1393456 AF |
1228 | rc = phy_driver_register(&genphy_driver); |
1229 | if (rc) | |
1230 | mdio_bus_exit(); | |
67c4f3fa | 1231 | |
67c4f3fa | 1232 | return rc; |
00db8189 AF |
1233 | } |
1234 | ||
67c4f3fa | 1235 | static void __exit phy_exit(void) |
00db8189 AF |
1236 | { |
1237 | phy_driver_unregister(&genphy_driver); | |
e1393456 | 1238 | mdio_bus_exit(); |
00db8189 AF |
1239 | } |
1240 | ||
e1393456 | 1241 | subsys_initcall(phy_init); |
67c4f3fa | 1242 | module_exit(phy_exit); |