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 | */ | |
17 | #include <linux/config.h> | |
18 | #include <linux/kernel.h> | |
19 | #include <linux/sched.h> | |
20 | #include <linux/string.h> | |
21 | #include <linux/errno.h> | |
22 | #include <linux/unistd.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/interrupt.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/delay.h> | |
27 | #include <linux/netdevice.h> | |
28 | #include <linux/etherdevice.h> | |
29 | #include <linux/skbuff.h> | |
30 | #include <linux/spinlock.h> | |
31 | #include <linux/mm.h> | |
32 | #include <linux/module.h> | |
33 | #include <linux/version.h> | |
34 | #include <linux/mii.h> | |
35 | #include <linux/ethtool.h> | |
36 | #include <linux/phy.h> | |
37 | ||
38 | #include <asm/io.h> | |
39 | #include <asm/irq.h> | |
40 | #include <asm/uaccess.h> | |
41 | ||
67c4f3fa JG |
42 | static int genphy_config_init(struct phy_device *phydev); |
43 | ||
44 | static struct phy_driver genphy_driver = { | |
45 | .phy_id = 0xffffffff, | |
46 | .phy_id_mask = 0xffffffff, | |
47 | .name = "Generic PHY", | |
48 | .config_init = genphy_config_init, | |
49 | .features = 0, | |
50 | .config_aneg = genphy_config_aneg, | |
51 | .read_status = genphy_read_status, | |
52 | .driver = {.owner = THIS_MODULE, }, | |
53 | }; | |
54 | ||
00db8189 AF |
55 | /* get_phy_device |
56 | * | |
57 | * description: Reads the ID registers of the PHY at addr on the | |
58 | * bus, then allocates and returns the phy_device to | |
59 | * represent it. | |
60 | */ | |
61 | struct phy_device * get_phy_device(struct mii_bus *bus, int addr) | |
62 | { | |
63 | int phy_reg; | |
64 | u32 phy_id; | |
65 | struct phy_device *dev = NULL; | |
66 | ||
67 | /* Grab the bits from PHYIR1, and put them | |
68 | * in the upper half */ | |
69 | phy_reg = bus->read(bus, addr, MII_PHYSID1); | |
70 | ||
71 | if (phy_reg < 0) | |
72 | return ERR_PTR(phy_reg); | |
73 | ||
74 | phy_id = (phy_reg & 0xffff) << 16; | |
75 | ||
76 | /* Grab the bits from PHYIR2, and put them in the lower half */ | |
77 | phy_reg = bus->read(bus, addr, MII_PHYSID2); | |
78 | ||
79 | if (phy_reg < 0) | |
80 | return ERR_PTR(phy_reg); | |
81 | ||
82 | phy_id |= (phy_reg & 0xffff); | |
83 | ||
84 | /* If the phy_id is all Fs, there is no device there */ | |
85 | if (0xffffffff == phy_id) | |
86 | return NULL; | |
87 | ||
88 | /* Otherwise, we allocate the device, and initialize the | |
89 | * default values */ | |
90 | dev = kcalloc(1, sizeof(*dev), GFP_KERNEL); | |
91 | ||
92 | if (NULL == dev) | |
93 | return ERR_PTR(-ENOMEM); | |
94 | ||
95 | dev->speed = 0; | |
96 | dev->duplex = -1; | |
97 | dev->pause = dev->asym_pause = 0; | |
98 | dev->link = 1; | |
99 | ||
100 | dev->autoneg = AUTONEG_ENABLE; | |
101 | ||
102 | dev->addr = addr; | |
103 | dev->phy_id = phy_id; | |
104 | dev->bus = bus; | |
105 | ||
106 | dev->state = PHY_DOWN; | |
107 | ||
108 | spin_lock_init(&dev->lock); | |
109 | ||
110 | return dev; | |
111 | } | |
112 | ||
113 | /* phy_prepare_link: | |
114 | * | |
115 | * description: Tells the PHY infrastructure to handle the | |
116 | * gory details on monitoring link status (whether through | |
117 | * polling or an interrupt), and to call back to the | |
118 | * connected device driver when the link status changes. | |
119 | * If you want to monitor your own link state, don't call | |
120 | * this function */ | |
121 | void phy_prepare_link(struct phy_device *phydev, | |
122 | void (*handler)(struct net_device *)) | |
123 | { | |
124 | phydev->adjust_link = handler; | |
125 | } | |
126 | ||
00db8189 AF |
127 | /* Generic PHY support and helper functions */ |
128 | ||
129 | /* genphy_config_advert | |
130 | * | |
131 | * description: Writes MII_ADVERTISE with the appropriate values, | |
132 | * after sanitizing the values to make sure we only advertise | |
133 | * what is supported | |
134 | */ | |
2bf69b5f | 135 | static int genphy_config_advert(struct phy_device *phydev) |
00db8189 AF |
136 | { |
137 | u32 advertise; | |
138 | int adv; | |
139 | int err; | |
140 | ||
141 | /* Only allow advertising what | |
142 | * this PHY supports */ | |
143 | phydev->advertising &= phydev->supported; | |
144 | advertise = phydev->advertising; | |
145 | ||
146 | /* Setup standard advertisement */ | |
147 | adv = phy_read(phydev, MII_ADVERTISE); | |
148 | ||
149 | if (adv < 0) | |
150 | return adv; | |
151 | ||
152 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | | |
153 | ADVERTISE_PAUSE_ASYM); | |
154 | if (advertise & ADVERTISED_10baseT_Half) | |
155 | adv |= ADVERTISE_10HALF; | |
156 | if (advertise & ADVERTISED_10baseT_Full) | |
157 | adv |= ADVERTISE_10FULL; | |
158 | if (advertise & ADVERTISED_100baseT_Half) | |
159 | adv |= ADVERTISE_100HALF; | |
160 | if (advertise & ADVERTISED_100baseT_Full) | |
161 | adv |= ADVERTISE_100FULL; | |
162 | if (advertise & ADVERTISED_Pause) | |
163 | adv |= ADVERTISE_PAUSE_CAP; | |
164 | if (advertise & ADVERTISED_Asym_Pause) | |
165 | adv |= ADVERTISE_PAUSE_ASYM; | |
166 | ||
167 | err = phy_write(phydev, MII_ADVERTISE, adv); | |
168 | ||
169 | if (err < 0) | |
170 | return err; | |
171 | ||
172 | /* Configure gigabit if it's supported */ | |
173 | if (phydev->supported & (SUPPORTED_1000baseT_Half | | |
174 | SUPPORTED_1000baseT_Full)) { | |
175 | adv = phy_read(phydev, MII_CTRL1000); | |
176 | ||
177 | if (adv < 0) | |
178 | return adv; | |
179 | ||
180 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); | |
181 | if (advertise & SUPPORTED_1000baseT_Half) | |
182 | adv |= ADVERTISE_1000HALF; | |
183 | if (advertise & SUPPORTED_1000baseT_Full) | |
184 | adv |= ADVERTISE_1000FULL; | |
185 | err = phy_write(phydev, MII_CTRL1000, adv); | |
186 | ||
187 | if (err < 0) | |
188 | return err; | |
189 | } | |
190 | ||
191 | return adv; | |
192 | } | |
00db8189 AF |
193 | |
194 | /* genphy_setup_forced | |
195 | * | |
196 | * description: Configures MII_BMCR to force speed/duplex | |
197 | * to the values in phydev. Assumes that the values are valid. | |
198 | * Please see phy_sanitize_settings() */ | |
199 | int genphy_setup_forced(struct phy_device *phydev) | |
200 | { | |
201 | int ctl = BMCR_RESET; | |
202 | ||
203 | phydev->pause = phydev->asym_pause = 0; | |
204 | ||
205 | if (SPEED_1000 == phydev->speed) | |
206 | ctl |= BMCR_SPEED1000; | |
207 | else if (SPEED_100 == phydev->speed) | |
208 | ctl |= BMCR_SPEED100; | |
209 | ||
210 | if (DUPLEX_FULL == phydev->duplex) | |
211 | ctl |= BMCR_FULLDPLX; | |
212 | ||
213 | ctl = phy_write(phydev, MII_BMCR, ctl); | |
214 | ||
215 | if (ctl < 0) | |
216 | return ctl; | |
217 | ||
218 | /* We just reset the device, so we'd better configure any | |
219 | * settings the PHY requires to operate */ | |
220 | if (phydev->drv->config_init) | |
221 | ctl = phydev->drv->config_init(phydev); | |
222 | ||
223 | return ctl; | |
224 | } | |
225 | ||
226 | ||
227 | /* Enable and Restart Autonegotiation */ | |
228 | int genphy_restart_aneg(struct phy_device *phydev) | |
229 | { | |
230 | int ctl; | |
231 | ||
232 | ctl = phy_read(phydev, MII_BMCR); | |
233 | ||
234 | if (ctl < 0) | |
235 | return ctl; | |
236 | ||
237 | ctl |= (BMCR_ANENABLE | BMCR_ANRESTART); | |
238 | ||
239 | /* Don't isolate the PHY if we're negotiating */ | |
240 | ctl &= ~(BMCR_ISOLATE); | |
241 | ||
242 | ctl = phy_write(phydev, MII_BMCR, ctl); | |
243 | ||
244 | return ctl; | |
245 | } | |
246 | ||
247 | ||
248 | /* genphy_config_aneg | |
249 | * | |
250 | * description: If auto-negotiation is enabled, we configure the | |
251 | * advertising, and then restart auto-negotiation. If it is not | |
252 | * enabled, then we write the BMCR | |
253 | */ | |
254 | int genphy_config_aneg(struct phy_device *phydev) | |
255 | { | |
256 | int err = 0; | |
257 | ||
258 | if (AUTONEG_ENABLE == phydev->autoneg) { | |
259 | err = genphy_config_advert(phydev); | |
260 | ||
261 | if (err < 0) | |
262 | return err; | |
263 | ||
264 | err = genphy_restart_aneg(phydev); | |
265 | } else | |
266 | err = genphy_setup_forced(phydev); | |
267 | ||
268 | return err; | |
269 | } | |
270 | EXPORT_SYMBOL(genphy_config_aneg); | |
271 | ||
272 | /* genphy_update_link | |
273 | * | |
274 | * description: Update the value in phydev->link to reflect the | |
275 | * current link value. In order to do this, we need to read | |
276 | * the status register twice, keeping the second value | |
277 | */ | |
278 | int genphy_update_link(struct phy_device *phydev) | |
279 | { | |
280 | int status; | |
281 | ||
282 | /* Do a fake read */ | |
283 | status = phy_read(phydev, MII_BMSR); | |
284 | ||
285 | if (status < 0) | |
286 | return status; | |
287 | ||
288 | /* Read link and autonegotiation status */ | |
289 | status = phy_read(phydev, MII_BMSR); | |
290 | ||
291 | if (status < 0) | |
292 | return status; | |
293 | ||
294 | if ((status & BMSR_LSTATUS) == 0) | |
295 | phydev->link = 0; | |
296 | else | |
297 | phydev->link = 1; | |
298 | ||
299 | return 0; | |
300 | } | |
301 | ||
302 | /* genphy_read_status | |
303 | * | |
304 | * description: Check the link, then figure out the current state | |
305 | * by comparing what we advertise with what the link partner | |
306 | * advertises. Start by checking the gigabit possibilities, | |
307 | * then move on to 10/100. | |
308 | */ | |
309 | int genphy_read_status(struct phy_device *phydev) | |
310 | { | |
311 | int adv; | |
312 | int err; | |
313 | int lpa; | |
314 | int lpagb = 0; | |
315 | ||
316 | /* Update the link, but return if there | |
317 | * was an error */ | |
318 | err = genphy_update_link(phydev); | |
319 | if (err) | |
320 | return err; | |
321 | ||
322 | if (AUTONEG_ENABLE == phydev->autoneg) { | |
323 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
324 | | SUPPORTED_1000baseT_Full)) { | |
325 | lpagb = phy_read(phydev, MII_STAT1000); | |
326 | ||
327 | if (lpagb < 0) | |
328 | return lpagb; | |
329 | ||
330 | adv = phy_read(phydev, MII_CTRL1000); | |
331 | ||
332 | if (adv < 0) | |
333 | return adv; | |
334 | ||
335 | lpagb &= adv << 2; | |
336 | } | |
337 | ||
338 | lpa = phy_read(phydev, MII_LPA); | |
339 | ||
340 | if (lpa < 0) | |
341 | return lpa; | |
342 | ||
343 | adv = phy_read(phydev, MII_ADVERTISE); | |
344 | ||
345 | if (adv < 0) | |
346 | return adv; | |
347 | ||
348 | lpa &= adv; | |
349 | ||
350 | phydev->speed = SPEED_10; | |
351 | phydev->duplex = DUPLEX_HALF; | |
352 | phydev->pause = phydev->asym_pause = 0; | |
353 | ||
354 | if (lpagb & (LPA_1000FULL | LPA_1000HALF)) { | |
355 | phydev->speed = SPEED_1000; | |
356 | ||
357 | if (lpagb & LPA_1000FULL) | |
358 | phydev->duplex = DUPLEX_FULL; | |
359 | } else if (lpa & (LPA_100FULL | LPA_100HALF)) { | |
360 | phydev->speed = SPEED_100; | |
361 | ||
362 | if (lpa & LPA_100FULL) | |
363 | phydev->duplex = DUPLEX_FULL; | |
364 | } else | |
365 | if (lpa & LPA_10FULL) | |
366 | phydev->duplex = DUPLEX_FULL; | |
367 | ||
368 | if (phydev->duplex == DUPLEX_FULL){ | |
369 | phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0; | |
370 | phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0; | |
371 | } | |
372 | } else { | |
373 | int bmcr = phy_read(phydev, MII_BMCR); | |
374 | if (bmcr < 0) | |
375 | return bmcr; | |
376 | ||
377 | if (bmcr & BMCR_FULLDPLX) | |
378 | phydev->duplex = DUPLEX_FULL; | |
379 | else | |
380 | phydev->duplex = DUPLEX_HALF; | |
381 | ||
382 | if (bmcr & BMCR_SPEED1000) | |
383 | phydev->speed = SPEED_1000; | |
384 | else if (bmcr & BMCR_SPEED100) | |
385 | phydev->speed = SPEED_100; | |
386 | else | |
387 | phydev->speed = SPEED_10; | |
388 | ||
389 | phydev->pause = phydev->asym_pause = 0; | |
390 | } | |
391 | ||
392 | return 0; | |
393 | } | |
394 | EXPORT_SYMBOL(genphy_read_status); | |
395 | ||
396 | static int genphy_config_init(struct phy_device *phydev) | |
397 | { | |
398 | u32 val; | |
399 | u32 features; | |
400 | ||
401 | /* For now, I'll claim that the generic driver supports | |
402 | * all possible port types */ | |
403 | features = (SUPPORTED_TP | SUPPORTED_MII | |
404 | | SUPPORTED_AUI | SUPPORTED_FIBRE | | |
405 | SUPPORTED_BNC); | |
406 | ||
407 | /* Do we support autonegotiation? */ | |
408 | val = phy_read(phydev, MII_BMSR); | |
409 | ||
410 | if (val < 0) | |
411 | return val; | |
412 | ||
413 | if (val & BMSR_ANEGCAPABLE) | |
414 | features |= SUPPORTED_Autoneg; | |
415 | ||
416 | if (val & BMSR_100FULL) | |
417 | features |= SUPPORTED_100baseT_Full; | |
418 | if (val & BMSR_100HALF) | |
419 | features |= SUPPORTED_100baseT_Half; | |
420 | if (val & BMSR_10FULL) | |
421 | features |= SUPPORTED_10baseT_Full; | |
422 | if (val & BMSR_10HALF) | |
423 | features |= SUPPORTED_10baseT_Half; | |
424 | ||
425 | if (val & BMSR_ESTATEN) { | |
426 | val = phy_read(phydev, MII_ESTATUS); | |
427 | ||
428 | if (val < 0) | |
429 | return val; | |
430 | ||
431 | if (val & ESTATUS_1000_TFULL) | |
432 | features |= SUPPORTED_1000baseT_Full; | |
433 | if (val & ESTATUS_1000_THALF) | |
434 | features |= SUPPORTED_1000baseT_Half; | |
435 | } | |
436 | ||
437 | phydev->supported = features; | |
438 | phydev->advertising = features; | |
439 | ||
440 | return 0; | |
441 | } | |
442 | ||
443 | ||
444 | /* phy_probe | |
445 | * | |
446 | * description: Take care of setting up the phy_device structure, | |
447 | * set the state to READY (the driver's init function should | |
448 | * set it to STARTING if needed). | |
449 | */ | |
450 | static int phy_probe(struct device *dev) | |
451 | { | |
452 | struct phy_device *phydev; | |
453 | struct phy_driver *phydrv; | |
454 | struct device_driver *drv; | |
455 | int err = 0; | |
456 | ||
457 | phydev = to_phy_device(dev); | |
458 | ||
459 | /* Make sure the driver is held. | |
460 | * XXX -- Is this correct? */ | |
461 | drv = get_driver(phydev->dev.driver); | |
462 | phydrv = to_phy_driver(drv); | |
463 | phydev->drv = phydrv; | |
464 | ||
465 | /* Disable the interrupt if the PHY doesn't support it */ | |
466 | if (!(phydrv->flags & PHY_HAS_INTERRUPT)) | |
467 | phydev->irq = PHY_POLL; | |
468 | ||
469 | spin_lock(&phydev->lock); | |
470 | ||
471 | /* Start out supporting everything. Eventually, | |
472 | * a controller will attach, and may modify one | |
473 | * or both of these values */ | |
474 | phydev->supported = phydrv->features; | |
475 | phydev->advertising = phydrv->features; | |
476 | ||
477 | /* Set the state to READY by default */ | |
478 | phydev->state = PHY_READY; | |
479 | ||
480 | if (phydev->drv->probe) | |
481 | err = phydev->drv->probe(phydev); | |
482 | ||
483 | spin_unlock(&phydev->lock); | |
484 | ||
485 | if (err < 0) | |
486 | return err; | |
487 | ||
488 | if (phydev->drv->config_init) | |
489 | err = phydev->drv->config_init(phydev); | |
490 | ||
491 | return err; | |
492 | } | |
493 | ||
494 | static int phy_remove(struct device *dev) | |
495 | { | |
496 | struct phy_device *phydev; | |
497 | ||
498 | phydev = to_phy_device(dev); | |
499 | ||
500 | spin_lock(&phydev->lock); | |
501 | phydev->state = PHY_DOWN; | |
502 | spin_unlock(&phydev->lock); | |
503 | ||
504 | if (phydev->drv->remove) | |
505 | phydev->drv->remove(phydev); | |
506 | ||
507 | put_driver(dev->driver); | |
508 | phydev->drv = NULL; | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
513 | int phy_driver_register(struct phy_driver *new_driver) | |
514 | { | |
515 | int retval; | |
516 | ||
517 | memset(&new_driver->driver, 0, sizeof(new_driver->driver)); | |
518 | new_driver->driver.name = new_driver->name; | |
519 | new_driver->driver.bus = &mdio_bus_type; | |
520 | new_driver->driver.probe = phy_probe; | |
521 | new_driver->driver.remove = phy_remove; | |
522 | ||
523 | retval = driver_register(&new_driver->driver); | |
524 | ||
525 | if (retval) { | |
526 | printk(KERN_ERR "%s: Error %d in registering driver\n", | |
527 | new_driver->name, retval); | |
528 | ||
529 | return retval; | |
530 | } | |
531 | ||
532 | pr_info("%s: Registered new driver\n", new_driver->name); | |
533 | ||
534 | return 0; | |
535 | } | |
536 | EXPORT_SYMBOL(phy_driver_register); | |
537 | ||
538 | void phy_driver_unregister(struct phy_driver *drv) | |
539 | { | |
540 | driver_unregister(&drv->driver); | |
541 | } | |
542 | EXPORT_SYMBOL(phy_driver_unregister); | |
543 | ||
00db8189 | 544 | |
67c4f3fa | 545 | static int __init phy_init(void) |
00db8189 | 546 | { |
67c4f3fa JG |
547 | int rc; |
548 | extern int mdio_bus_init(void); | |
549 | ||
550 | rc = phy_driver_register(&genphy_driver); | |
551 | if (rc) | |
552 | goto out; | |
553 | ||
554 | rc = mdio_bus_init(); | |
555 | if (rc) | |
556 | goto out_unreg; | |
00db8189 | 557 | |
67c4f3fa JG |
558 | return 0; |
559 | ||
560 | out_unreg: | |
561 | phy_driver_unregister(&genphy_driver); | |
562 | out: | |
563 | return rc; | |
00db8189 AF |
564 | } |
565 | ||
67c4f3fa | 566 | static void __exit phy_exit(void) |
00db8189 AF |
567 | { |
568 | phy_driver_unregister(&genphy_driver); | |
569 | } | |
570 | ||
67c4f3fa JG |
571 | module_init(phy_init); |
572 | module_exit(phy_exit); |