aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/phy/phy.c
diff options
context:
space:
mode:
authorAndy Fleming <afleming@freescale.com>2005-08-24 19:46:21 -0400
committerJeff Garzik <jgarzik@pobox.com>2005-08-28 20:28:25 -0400
commite13934563db047043ccead26412f552375cea90c (patch)
tree4013ca99f718559447315370c9d5e220e71d99d5 /drivers/net/phy/phy.c
parent86f0cd505781e42000763821ec6f70127a6abaae (diff)
[PATCH] PHY Layer fixup
This patch adds back the code that was taken out, thus re-enabling: * The PHY Layer to initialize without crashing * Drivers to actually connect to PHYs * The entire PHY Control Layer This patch is used by the gianfar driver, and other drivers which are in development. Signed-off-by: Andy Fleming <afleming@freescale.com> Signed-off-by: Jeff Garzik <jgarzik@pobox.com>
Diffstat (limited to 'drivers/net/phy/phy.c')
-rw-r--r--drivers/net/phy/phy.c325
1 files changed, 253 insertions, 72 deletions
diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c
index d3e43631b89b..d9e11f93bf3a 100644
--- a/drivers/net/phy/phy.c
+++ b/drivers/net/phy/phy.c
@@ -39,10 +39,20 @@
39#include <asm/irq.h> 39#include <asm/irq.h>
40#include <asm/uaccess.h> 40#include <asm/uaccess.h>
41 41
42static void phy_timer(unsigned long data); 42/* Convenience function to print out the current phy status
43static int phy_disable_interrupts(struct phy_device *phydev); 43 */
44static void phy_sanitize_settings(struct phy_device *phydev); 44void phy_print_status(struct phy_device *phydev)
45static int phy_stop_interrupts(struct phy_device *phydev); 45{
46 pr_info("%s: Link is %s", phydev->dev.bus_id,
47 phydev->link ? "Up" : "Down");
48 if (phydev->link)
49 printk(" - %d/%s", phydev->speed,
50 DUPLEX_FULL == phydev->duplex ?
51 "Full" : "Half");
52
53 printk("\n");
54}
55EXPORT_SYMBOL(phy_print_status);
46 56
47 57
48/* Convenience functions for reading/writing a given PHY 58/* Convenience functions for reading/writing a given PHY
@@ -114,42 +124,6 @@ static inline int phy_aneg_done(struct phy_device *phydev)
114 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); 124 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
115} 125}
116 126
117/* phy_start_aneg
118 *
119 * description: Calls the PHY driver's config_aneg, and then
120 * sets the PHY state to PHY_AN if auto-negotiation is enabled,
121 * and to PHY_FORCING if auto-negotiation is disabled. Unless
122 * the PHY is currently HALTED.
123 */
124static int phy_start_aneg(struct phy_device *phydev)
125{
126 int err;
127
128 spin_lock(&phydev->lock);
129
130 if (AUTONEG_DISABLE == phydev->autoneg)
131 phy_sanitize_settings(phydev);
132
133 err = phydev->drv->config_aneg(phydev);
134
135 if (err < 0)
136 goto out_unlock;
137
138 if (phydev->state != PHY_HALTED) {
139 if (AUTONEG_ENABLE == phydev->autoneg) {
140 phydev->state = PHY_AN;
141 phydev->link_timeout = PHY_AN_TIMEOUT;
142 } else {
143 phydev->state = PHY_FORCING;
144 phydev->link_timeout = PHY_FORCE_TIMEOUT;
145 }
146 }
147
148out_unlock:
149 spin_unlock(&phydev->lock);
150 return err;
151}
152
153/* A structure for mapping a particular speed and duplex 127/* A structure for mapping a particular speed and duplex
154 * combination to a particular SUPPORTED and ADVERTISED value */ 128 * combination to a particular SUPPORTED and ADVERTISED value */
155struct phy_setting { 129struct phy_setting {
@@ -241,7 +215,7 @@ static inline int phy_find_valid(int idx, u32 features)
241 * duplexes. Drop down by one in this order: 1000/FULL, 215 * duplexes. Drop down by one in this order: 1000/FULL,
242 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF 216 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
243 */ 217 */
244static void phy_sanitize_settings(struct phy_device *phydev) 218void phy_sanitize_settings(struct phy_device *phydev)
245{ 219{
246 u32 features = phydev->supported; 220 u32 features = phydev->supported;
247 int idx; 221 int idx;
@@ -256,31 +230,7 @@ static void phy_sanitize_settings(struct phy_device *phydev)
256 phydev->speed = settings[idx].speed; 230 phydev->speed = settings[idx].speed;
257 phydev->duplex = settings[idx].duplex; 231 phydev->duplex = settings[idx].duplex;
258} 232}
259 233EXPORT_SYMBOL(phy_sanitize_settings);
260/* phy_force_reduction
261 *
262 * description: Reduces the speed/duplex settings by
263 * one notch. The order is so:
264 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
265 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
266 */
267static void phy_force_reduction(struct phy_device *phydev)
268{
269 int idx;
270
271 idx = phy_find_setting(phydev->speed, phydev->duplex);
272
273 idx++;
274
275 idx = phy_find_valid(idx, phydev->supported);
276
277 phydev->speed = settings[idx].speed;
278 phydev->duplex = settings[idx].duplex;
279
280 pr_info("Trying %d/%s\n", phydev->speed,
281 DUPLEX_FULL == phydev->duplex ?
282 "FULL" : "HALF");
283}
284 234
285/* phy_ethtool_sset: 235/* phy_ethtool_sset:
286 * A generic ethtool sset function. Handles all the details 236 * A generic ethtool sset function. Handles all the details
@@ -291,6 +241,11 @@ static void phy_force_reduction(struct phy_device *phydev)
291 * - phy_start_aneg() will make sure forced settings are sane, and 241 * - phy_start_aneg() will make sure forced settings are sane, and
292 * choose the next best ones from the ones selected, so we don't 242 * choose the next best ones from the ones selected, so we don't
293 * care if ethtool tries to give us bad values 243 * care if ethtool tries to give us bad values
244 *
245 * A note about the PHYCONTROL Layer. If you turn off
246 * CONFIG_PHYCONTROL, you will need to read the PHY status
247 * registers after this function completes, and update your
248 * controller manually.
294 */ 249 */
295int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd) 250int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
296{ 251{
@@ -406,6 +361,51 @@ int phy_mii_ioctl(struct phy_device *phydev,
406 return 0; 361 return 0;
407} 362}
408 363
364/* phy_start_aneg
365 *
366 * description: Sanitizes the settings (if we're not
367 * autonegotiating them), and then calls the driver's
368 * config_aneg function. If the PHYCONTROL Layer is operating,
369 * we change the state to reflect the beginning of
370 * Auto-negotiation or forcing.
371 */
372int phy_start_aneg(struct phy_device *phydev)
373{
374 int err;
375
376 spin_lock(&phydev->lock);
377
378 if (AUTONEG_DISABLE == phydev->autoneg)
379 phy_sanitize_settings(phydev);
380
381 err = phydev->drv->config_aneg(phydev);
382
383#ifdef CONFIG_PHYCONTROL
384 if (err < 0)
385 goto out_unlock;
386
387 if (phydev->state != PHY_HALTED) {
388 if (AUTONEG_ENABLE == phydev->autoneg) {
389 phydev->state = PHY_AN;
390 phydev->link_timeout = PHY_AN_TIMEOUT;
391 } else {
392 phydev->state = PHY_FORCING;
393 phydev->link_timeout = PHY_FORCE_TIMEOUT;
394 }
395 }
396
397out_unlock:
398#endif
399 spin_unlock(&phydev->lock);
400 return err;
401}
402EXPORT_SYMBOL(phy_start_aneg);
403
404
405#ifdef CONFIG_PHYCONTROL
406static void phy_change(void *data);
407static void phy_timer(unsigned long data);
408
409/* phy_start_machine: 409/* phy_start_machine:
410 * 410 *
411 * description: The PHY infrastructure can run a state machine 411 * description: The PHY infrastructure can run a state machine
@@ -448,6 +448,32 @@ void phy_stop_machine(struct phy_device *phydev)
448 phydev->adjust_state = NULL; 448 phydev->adjust_state = NULL;
449} 449}
450 450
451/* phy_force_reduction
452 *
453 * description: Reduces the speed/duplex settings by
454 * one notch. The order is so:
455 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
456 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
457 */
458static void phy_force_reduction(struct phy_device *phydev)
459{
460 int idx;
461
462 idx = phy_find_setting(phydev->speed, phydev->duplex);
463
464 idx++;
465
466 idx = phy_find_valid(idx, phydev->supported);
467
468 phydev->speed = settings[idx].speed;
469 phydev->duplex = settings[idx].duplex;
470
471 pr_info("Trying %d/%s\n", phydev->speed,
472 DUPLEX_FULL == phydev->duplex ?
473 "FULL" : "HALF");
474}
475
476
451/* phy_error: 477/* phy_error:
452 * 478 *
453 * Moves the PHY to the HALTED state in response to a read 479 * Moves the PHY to the HALTED state in response to a read
@@ -462,22 +488,44 @@ void phy_error(struct phy_device *phydev)
462 spin_unlock(&phydev->lock); 488 spin_unlock(&phydev->lock);
463} 489}
464 490
465static int phy_stop_interrupts(struct phy_device *phydev) 491/* phy_interrupt
492 *
493 * description: When a PHY interrupt occurs, the handler disables
494 * interrupts, and schedules a work task to clear the interrupt.
495 */
496static irqreturn_t phy_interrupt(int irq, void *phy_dat, struct pt_regs *regs)
497{
498 struct phy_device *phydev = phy_dat;
499
500 /* The MDIO bus is not allowed to be written in interrupt
501 * context, so we need to disable the irq here. A work
502 * queue will write the PHY to disable and clear the
503 * interrupt, and then reenable the irq line. */
504 disable_irq_nosync(irq);
505
506 schedule_work(&phydev->phy_queue);
507
508 return IRQ_HANDLED;
509}
510
511/* Enable the interrupts from the PHY side */
512int phy_enable_interrupts(struct phy_device *phydev)
466{ 513{
467 int err; 514 int err;
468 515
469 err = phy_disable_interrupts(phydev); 516 err = phy_clear_interrupt(phydev);
470 517
471 if (err) 518 if (err < 0)
472 phy_error(phydev); 519 return err;
473 520
474 free_irq(phydev->irq, phydev); 521 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
475 522
476 return err; 523 return err;
477} 524}
525EXPORT_SYMBOL(phy_enable_interrupts);
478 526
479/* Disable the PHY interrupts from the PHY side */ 527/* Disable the PHY interrupts from the PHY side */
480static int phy_disable_interrupts(struct phy_device *phydev) 528int phy_disable_interrupts(struct phy_device *phydev)
481{ 529{
482 int err; 530 int err;
483 531
@@ -500,6 +548,138 @@ phy_err:
500 548
501 return err; 549 return err;
502} 550}
551EXPORT_SYMBOL(phy_disable_interrupts);
552
553/* phy_start_interrupts
554 *
555 * description: Request the interrupt for the given PHY. If
556 * this fails, then we set irq to PHY_POLL.
557 * Otherwise, we enable the interrupts in the PHY.
558 * Returns 0 on success.
559 * This should only be called with a valid IRQ number.
560 */
561int phy_start_interrupts(struct phy_device *phydev)
562{
563 int err = 0;
564
565 INIT_WORK(&phydev->phy_queue, phy_change, phydev);
566
567 if (request_irq(phydev->irq, phy_interrupt,
568 SA_SHIRQ,
569 "phy_interrupt",
570 phydev) < 0) {
571 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
572 phydev->bus->name,
573 phydev->irq);
574 phydev->irq = PHY_POLL;
575 return 0;
576 }
577
578 err = phy_enable_interrupts(phydev);
579
580 return err;
581}
582EXPORT_SYMBOL(phy_start_interrupts);
583
584int phy_stop_interrupts(struct phy_device *phydev)
585{
586 int err;
587
588 err = phy_disable_interrupts(phydev);
589
590 if (err)
591 phy_error(phydev);
592
593 free_irq(phydev->irq, phydev);
594
595 return err;
596}
597EXPORT_SYMBOL(phy_stop_interrupts);
598
599
600/* Scheduled by the phy_interrupt/timer to handle PHY changes */
601static void phy_change(void *data)
602{
603 int err;
604 struct phy_device *phydev = data;
605
606 err = phy_disable_interrupts(phydev);
607
608 if (err)
609 goto phy_err;
610
611 spin_lock(&phydev->lock);
612 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
613 phydev->state = PHY_CHANGELINK;
614 spin_unlock(&phydev->lock);
615
616 enable_irq(phydev->irq);
617
618 /* Reenable interrupts */
619 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
620
621 if (err)
622 goto irq_enable_err;
623
624 return;
625
626irq_enable_err:
627 disable_irq(phydev->irq);
628phy_err:
629 phy_error(phydev);
630}
631
632/* Bring down the PHY link, and stop checking the status. */
633void phy_stop(struct phy_device *phydev)
634{
635 spin_lock(&phydev->lock);
636
637 if (PHY_HALTED == phydev->state)
638 goto out_unlock;
639
640 if (phydev->irq != PHY_POLL) {
641 /* Clear any pending interrupts */
642 phy_clear_interrupt(phydev);
643
644 /* Disable PHY Interrupts */
645 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
646 }
647
648 phydev->state = PHY_HALTED;
649
650out_unlock:
651 spin_unlock(&phydev->lock);
652}
653
654
655/* phy_start
656 *
657 * description: Indicates the attached device's readiness to
658 * handle PHY-related work. Used during startup to start the
659 * PHY, and after a call to phy_stop() to resume operation.
660 * Also used to indicate the MDIO bus has cleared an error
661 * condition.
662 */
663void phy_start(struct phy_device *phydev)
664{
665 spin_lock(&phydev->lock);
666
667 switch (phydev->state) {
668 case PHY_STARTING:
669 phydev->state = PHY_PENDING;
670 break;
671 case PHY_READY:
672 phydev->state = PHY_UP;
673 break;
674 case PHY_HALTED:
675 phydev->state = PHY_RESUMING;
676 default:
677 break;
678 }
679 spin_unlock(&phydev->lock);
680}
681EXPORT_SYMBOL(phy_stop);
682EXPORT_SYMBOL(phy_start);
503 683
504/* PHY timer which handles the state machine */ 684/* PHY timer which handles the state machine */
505static void phy_timer(unsigned long data) 685static void phy_timer(unsigned long data)
@@ -688,3 +868,4 @@ static void phy_timer(unsigned long data)
688 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ); 868 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
689} 869}
690 870
871#endif /* CONFIG_PHYCONTROL */