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