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