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