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