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