Fix numerous minor problems with new phy subsystem.
[linux-2.6/kvm.git] / drivers / net / phy / phy.c
blob934065dd6371e61e1d0ce4b734261c28feac0a8f
1 /*
2 * drivers/net/phy/phy.c
4 * Framework for configuring and reading PHY devices
5 * Based on code in sungem_phy.c and gianfar_phy.c
7 * Author: Andy Fleming
9 * Copyright (c) 2004 Freescale Semiconductor, Inc.
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.
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>
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #include <asm/uaccess.h>
42 static void phy_timer(unsigned long data);
44 /* Convenience function to print out the current phy status
46 void phy_print_status(struct phy_device *phydev)
48 pr_info("%s: Link is %s", phydev->dev.bus_id,
49 phydev->link ? "Up" : "Down");
50 if (phydev->link)
51 printk(" - %d/%s", phydev->speed,
52 DUPLEX_FULL == phydev->duplex ?
53 "Full" : "Half");
55 printk("\n");
57 EXPORT_SYMBOL(phy_print_status);
60 /* Convenience functions for reading/writing a given PHY
61 * register. They MUST NOT be called from interrupt context,
62 * because the bus read/write functions may wait for an interrupt
63 * to conclude the operation. */
64 int phy_read(struct phy_device *phydev, u16 regnum)
66 int retval;
67 struct mii_bus *bus = phydev->bus;
69 spin_lock_bh(&bus->mdio_lock);
70 retval = bus->read(bus, phydev->addr, regnum);
71 spin_unlock_bh(&bus->mdio_lock);
73 return retval;
75 EXPORT_SYMBOL(phy_read);
77 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
79 int err;
80 struct mii_bus *bus = phydev->bus;
82 spin_lock_bh(&bus->mdio_lock);
83 err = bus->write(bus, phydev->addr, regnum, val);
84 spin_unlock_bh(&bus->mdio_lock);
86 return err;
88 EXPORT_SYMBOL(phy_write);
91 int phy_clear_interrupt(struct phy_device *phydev)
93 int err = 0;
95 if (phydev->drv->ack_interrupt)
96 err = phydev->drv->ack_interrupt(phydev);
98 return err;
102 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
104 int err = 0;
106 phydev->interrupts = interrupts;
107 if (phydev->drv->config_intr)
108 err = phydev->drv->config_intr(phydev);
110 return err;
114 /* phy_aneg_done
116 * description: Reads the status register and returns 0 either if
117 * auto-negotiation is incomplete, or if there was an error.
118 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
120 static inline int phy_aneg_done(struct phy_device *phydev)
122 int retval;
124 retval = phy_read(phydev, MII_BMSR);
126 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
129 /* phy_start_aneg
131 * description: Calls the PHY driver's config_aneg, and then
132 * sets the PHY state to PHY_AN if auto-negotiation is enabled,
133 * and to PHY_FORCING if auto-negotiation is disabled. Unless
134 * the PHY is currently HALTED.
136 int phy_start_aneg(struct phy_device *phydev)
138 int err;
140 spin_lock(&phydev->lock);
142 if (AUTONEG_DISABLE == phydev->autoneg)
143 phy_sanitize_settings(phydev);
145 err = phydev->drv->config_aneg(phydev);
147 if (err < 0)
148 goto out_unlock;
150 if (phydev->state != PHY_HALTED) {
151 if (AUTONEG_ENABLE == phydev->autoneg) {
152 phydev->state = PHY_AN;
153 phydev->link_timeout = PHY_AN_TIMEOUT;
154 } else {
155 phydev->state = PHY_FORCING;
156 phydev->link_timeout = PHY_FORCE_TIMEOUT;
160 out_unlock:
161 spin_unlock(&phydev->lock);
162 return err;
164 EXPORT_SYMBOL(phy_start_aneg);
167 /* A structure for mapping a particular speed and duplex
168 * combination to a particular SUPPORTED and ADVERTISED value */
169 struct phy_setting {
170 int speed;
171 int duplex;
172 u32 setting;
175 /* A mapping of all SUPPORTED settings to speed/duplex */
176 static struct phy_setting settings[] = {
178 .speed = 10000,
179 .duplex = DUPLEX_FULL,
180 .setting = SUPPORTED_10000baseT_Full,
183 .speed = SPEED_1000,
184 .duplex = DUPLEX_FULL,
185 .setting = SUPPORTED_1000baseT_Full,
188 .speed = SPEED_1000,
189 .duplex = DUPLEX_HALF,
190 .setting = SUPPORTED_1000baseT_Half,
193 .speed = SPEED_100,
194 .duplex = DUPLEX_FULL,
195 .setting = SUPPORTED_100baseT_Full,
198 .speed = SPEED_100,
199 .duplex = DUPLEX_HALF,
200 .setting = SUPPORTED_100baseT_Half,
203 .speed = SPEED_10,
204 .duplex = DUPLEX_FULL,
205 .setting = SUPPORTED_10baseT_Full,
208 .speed = SPEED_10,
209 .duplex = DUPLEX_HALF,
210 .setting = SUPPORTED_10baseT_Half,
214 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
216 /* phy_find_setting
218 * description: Searches the settings array for the setting which
219 * matches the desired speed and duplex, and returns the index
220 * of that setting. Returns the index of the last setting if
221 * none of the others match.
223 static inline int phy_find_setting(int speed, int duplex)
225 int idx = 0;
227 while (idx < ARRAY_SIZE(settings) &&
228 (settings[idx].speed != speed ||
229 settings[idx].duplex != duplex))
230 idx++;
232 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
235 /* phy_find_valid
236 * idx: The first index in settings[] to search
237 * features: A mask of the valid settings
239 * description: Returns the index of the first valid setting less
240 * than or equal to the one pointed to by idx, as determined by
241 * the mask in features. Returns the index of the last setting
242 * if nothing else matches.
244 static inline int phy_find_valid(int idx, u32 features)
246 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
247 idx++;
249 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
252 /* phy_sanitize_settings
254 * description: Make sure the PHY is set to supported speeds and
255 * duplexes. Drop down by one in this order: 1000/FULL,
256 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
258 void phy_sanitize_settings(struct phy_device *phydev)
260 u32 features = phydev->supported;
261 int idx;
263 /* Sanitize settings based on PHY capabilities */
264 if ((features & SUPPORTED_Autoneg) == 0)
265 phydev->autoneg = 0;
267 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
268 features);
270 phydev->speed = settings[idx].speed;
271 phydev->duplex = settings[idx].duplex;
273 EXPORT_SYMBOL(phy_sanitize_settings);
275 /* phy_force_reduction
277 * description: Reduces the speed/duplex settings by
278 * one notch. The order is so:
279 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
280 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
282 static void phy_force_reduction(struct phy_device *phydev)
284 int idx;
286 idx = phy_find_setting(phydev->speed, phydev->duplex);
288 idx++;
290 idx = phy_find_valid(idx, phydev->supported);
292 phydev->speed = settings[idx].speed;
293 phydev->duplex = settings[idx].duplex;
295 pr_info("Trying %d/%s\n", phydev->speed,
296 DUPLEX_FULL == phydev->duplex ?
297 "FULL" : "HALF");
300 /* phy_ethtool_sset:
301 * A generic ethtool sset function. Handles all the details
303 * A few notes about parameter checking:
304 * - We don't set port or transceiver, so we don't care what they
305 * were set to.
306 * - phy_start_aneg() will make sure forced settings are sane, and
307 * choose the next best ones from the ones selected, so we don't
308 * care if ethtool tries to give us bad values
310 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
312 if (cmd->phy_address != phydev->addr)
313 return -EINVAL;
315 /* We make sure that we don't pass unsupported
316 * values in to the PHY */
317 cmd->advertising &= phydev->supported;
319 /* Verify the settings we care about. */
320 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
321 return -EINVAL;
323 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
324 return -EINVAL;
326 if (cmd->autoneg == AUTONEG_DISABLE
327 && ((cmd->speed != SPEED_1000
328 && cmd->speed != SPEED_100
329 && cmd->speed != SPEED_10)
330 || (cmd->duplex != DUPLEX_HALF
331 && cmd->duplex != DUPLEX_FULL)))
332 return -EINVAL;
334 phydev->autoneg = cmd->autoneg;
336 phydev->speed = cmd->speed;
338 phydev->advertising = cmd->advertising;
340 if (AUTONEG_ENABLE == cmd->autoneg)
341 phydev->advertising |= ADVERTISED_Autoneg;
342 else
343 phydev->advertising &= ~ADVERTISED_Autoneg;
345 phydev->duplex = cmd->duplex;
347 /* Restart the PHY */
348 phy_start_aneg(phydev);
350 return 0;
353 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
355 cmd->supported = phydev->supported;
357 cmd->advertising = phydev->advertising;
359 cmd->speed = phydev->speed;
360 cmd->duplex = phydev->duplex;
361 cmd->port = PORT_MII;
362 cmd->phy_address = phydev->addr;
363 cmd->transceiver = XCVR_EXTERNAL;
364 cmd->autoneg = phydev->autoneg;
366 return 0;
370 /* Note that this function is currently incompatible with the
371 * PHYCONTROL layer. It changes registers without regard to
372 * current state. Use at own risk
374 int phy_mii_ioctl(struct phy_device *phydev,
375 struct mii_ioctl_data *mii_data, int cmd)
377 u16 val = mii_data->val_in;
379 switch (cmd) {
380 case SIOCGMIIPHY:
381 mii_data->phy_id = phydev->addr;
382 break;
383 case SIOCGMIIREG:
384 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
385 break;
387 case SIOCSMIIREG:
388 if (!capable(CAP_NET_ADMIN))
389 return -EPERM;
391 if (mii_data->phy_id == phydev->addr) {
392 switch(mii_data->reg_num) {
393 case MII_BMCR:
394 if (val & (BMCR_RESET|BMCR_ANENABLE))
395 phydev->autoneg = AUTONEG_DISABLE;
396 else
397 phydev->autoneg = AUTONEG_ENABLE;
398 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
399 phydev->duplex = DUPLEX_FULL;
400 else
401 phydev->duplex = DUPLEX_HALF;
402 break;
403 case MII_ADVERTISE:
404 phydev->advertising = val;
405 break;
406 default:
407 /* do nothing */
408 break;
412 phy_write(phydev, mii_data->reg_num, val);
414 if (mii_data->reg_num == MII_BMCR
415 && val & BMCR_RESET
416 && phydev->drv->config_init)
417 phydev->drv->config_init(phydev);
418 break;
421 return 0;
424 /* phy_start_machine:
426 * description: The PHY infrastructure can run a state machine
427 * which tracks whether the PHY is starting up, negotiating,
428 * etc. This function starts the timer which tracks the state
429 * of the PHY. If you want to be notified when the state
430 * changes, pass in the callback, otherwise, pass NULL. If you
431 * want to maintain your own state machine, do not call this
432 * function. */
433 void phy_start_machine(struct phy_device *phydev,
434 void (*handler)(struct net_device *))
436 phydev->adjust_state = handler;
438 init_timer(&phydev->phy_timer);
439 phydev->phy_timer.function = &phy_timer;
440 phydev->phy_timer.data = (unsigned long) phydev;
441 mod_timer(&phydev->phy_timer, jiffies + HZ);
444 /* phy_stop_machine
446 * description: Stops the state machine timer, sets the state to
447 * UP (unless it wasn't up yet), and then frees the interrupt,
448 * if it is in use. This function must be called BEFORE
449 * phy_detach.
451 void phy_stop_machine(struct phy_device *phydev)
453 del_timer_sync(&phydev->phy_timer);
455 spin_lock(&phydev->lock);
456 if (phydev->state > PHY_UP)
457 phydev->state = PHY_UP;
458 spin_unlock(&phydev->lock);
460 if (phydev->irq != PHY_POLL)
461 phy_stop_interrupts(phydev);
463 phydev->adjust_state = NULL;
466 /* phy_error:
468 * Moves the PHY to the HALTED state in response to a read
469 * or write error, and tells the controller the link is down.
470 * Must not be called from interrupt context, or while the
471 * phydev->lock is held.
473 void phy_error(struct phy_device *phydev)
475 spin_lock(&phydev->lock);
476 phydev->state = PHY_HALTED;
477 spin_unlock(&phydev->lock);
480 #ifdef CONFIG_PHYCONTROL
482 static void phy_change(void *data);
484 /* phy_interrupt
486 * description: When a PHY interrupt occurs, the handler disables
487 * interrupts, and schedules a work task to clear the interrupt.
489 static irqreturn_t phy_interrupt(int irq, void *phy_dat, struct pt_regs *regs)
491 struct phy_device *phydev = phy_dat;
493 /* The MDIO bus is not allowed to be written in interrupt
494 * context, so we need to disable the irq here. A work
495 * queue will write the PHY to disable and clear the
496 * interrupt, and then reenable the irq line. */
497 disable_irq_nosync(irq);
499 schedule_work(&phydev->phy_queue);
501 return IRQ_HANDLED;
504 /* Enable the interrupts from the PHY side */
505 int phy_enable_interrupts(struct phy_device *phydev)
507 int err;
509 err = phy_clear_interrupt(phydev);
511 if (err < 0)
512 return err;
514 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
516 return err;
518 EXPORT_SYMBOL(phy_enable_interrupts);
520 /* Disable the PHY interrupts from the PHY side */
521 int phy_disable_interrupts(struct phy_device *phydev)
523 int err;
525 /* Disable PHY interrupts */
526 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
528 if (err)
529 goto phy_err;
531 /* Clear the interrupt */
532 err = phy_clear_interrupt(phydev);
534 if (err)
535 goto phy_err;
537 return 0;
539 phy_err:
540 phy_error(phydev);
542 return err;
544 EXPORT_SYMBOL(phy_disable_interrupts);
546 /* phy_start_interrupts
548 * description: Request the interrupt for the given PHY. If
549 * this fails, then we set irq to PHY_POLL.
550 * Otherwise, we enable the interrupts in the PHY.
551 * Returns 0 on success.
552 * This should only be called with a valid IRQ number.
554 int phy_start_interrupts(struct phy_device *phydev)
556 int err = 0;
558 INIT_WORK(&phydev->phy_queue, phy_change, phydev);
560 if (request_irq(phydev->irq, phy_interrupt,
561 SA_SHIRQ,
562 "phy_interrupt",
563 phydev) < 0) {
564 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
565 phydev->bus->name,
566 phydev->irq);
567 phydev->irq = PHY_POLL;
568 return 0;
571 err = phy_enable_interrupts(phydev);
573 return err;
575 EXPORT_SYMBOL(phy_start_interrupts);
577 int phy_stop_interrupts(struct phy_device *phydev)
579 int err;
581 err = phy_disable_interrupts(phydev);
583 if (err)
584 phy_error(phydev);
586 free_irq(phydev->irq, phydev);
588 return err;
590 EXPORT_SYMBOL(phy_stop_interrupts);
593 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
594 static void phy_change(void *data)
596 int err;
597 struct phy_device *phydev = data;
599 err = phy_disable_interrupts(phydev);
601 if (err)
602 goto phy_err;
604 spin_lock(&phydev->lock);
605 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
606 phydev->state = PHY_CHANGELINK;
607 spin_unlock(&phydev->lock);
609 enable_irq(phydev->irq);
611 /* Reenable interrupts */
612 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
614 if (err)
615 goto irq_enable_err;
617 return;
619 irq_enable_err:
620 disable_irq(phydev->irq);
621 phy_err:
622 phy_error(phydev);
625 /* Bring down the PHY link, and stop checking the status. */
626 void phy_stop(struct phy_device *phydev)
628 spin_lock(&phydev->lock);
630 if (PHY_HALTED == phydev->state)
631 goto out_unlock;
633 if (phydev->irq != PHY_POLL) {
634 /* Clear any pending interrupts */
635 phy_clear_interrupt(phydev);
637 /* Disable PHY Interrupts */
638 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
641 phydev->state = PHY_HALTED;
643 out_unlock:
644 spin_unlock(&phydev->lock);
648 /* phy_start
650 * description: Indicates the attached device's readiness to
651 * handle PHY-related work. Used during startup to start the
652 * PHY, and after a call to phy_stop() to resume operation.
653 * Also used to indicate the MDIO bus has cleared an error
654 * condition.
656 void phy_start(struct phy_device *phydev)
658 spin_lock(&phydev->lock);
660 switch (phydev->state) {
661 case PHY_STARTING:
662 phydev->state = PHY_PENDING;
663 break;
664 case PHY_READY:
665 phydev->state = PHY_UP;
666 break;
667 case PHY_HALTED:
668 phydev->state = PHY_RESUMING;
669 default:
670 break;
672 spin_unlock(&phydev->lock);
674 EXPORT_SYMBOL(phy_stop);
675 EXPORT_SYMBOL(phy_start);
677 #endif /* CONFIG_PHYCONTROL */
679 /* PHY timer which handles the state machine */
680 static void phy_timer(unsigned long data)
682 struct phy_device *phydev = (struct phy_device *)data;
683 int needs_aneg = 0;
684 int err = 0;
686 spin_lock(&phydev->lock);
688 if (phydev->adjust_state)
689 phydev->adjust_state(phydev->attached_dev);
691 switch(phydev->state) {
692 case PHY_DOWN:
693 case PHY_STARTING:
694 case PHY_READY:
695 case PHY_PENDING:
696 break;
697 case PHY_UP:
698 needs_aneg = 1;
700 phydev->link_timeout = PHY_AN_TIMEOUT;
702 break;
703 case PHY_AN:
704 /* Check if negotiation is done. Break
705 * if there's an error */
706 err = phy_aneg_done(phydev);
707 if (err < 0)
708 break;
710 /* If auto-negotiation is done, we change to
711 * either RUNNING, or NOLINK */
712 if (err > 0) {
713 err = phy_read_status(phydev);
715 if (err)
716 break;
718 if (phydev->link) {
719 phydev->state = PHY_RUNNING;
720 netif_carrier_on(phydev->attached_dev);
721 } else {
722 phydev->state = PHY_NOLINK;
723 netif_carrier_off(phydev->attached_dev);
726 phydev->adjust_link(phydev->attached_dev);
728 } else if (0 == phydev->link_timeout--) {
729 /* The counter expired, so either we
730 * switch to forced mode, or the
731 * magic_aneg bit exists, and we try aneg
732 * again */
733 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
734 int idx;
736 /* We'll start from the
737 * fastest speed, and work
738 * our way down */
739 idx = phy_find_valid(0,
740 phydev->supported);
742 phydev->speed = settings[idx].speed;
743 phydev->duplex = settings[idx].duplex;
745 phydev->autoneg = AUTONEG_DISABLE;
746 phydev->state = PHY_FORCING;
747 phydev->link_timeout =
748 PHY_FORCE_TIMEOUT;
750 pr_info("Trying %d/%s\n",
751 phydev->speed,
752 DUPLEX_FULL ==
753 phydev->duplex ?
754 "FULL" : "HALF");
757 needs_aneg = 1;
759 break;
760 case PHY_NOLINK:
761 err = phy_read_status(phydev);
763 if (err)
764 break;
766 if (phydev->link) {
767 phydev->state = PHY_RUNNING;
768 netif_carrier_on(phydev->attached_dev);
769 phydev->adjust_link(phydev->attached_dev);
771 break;
772 case PHY_FORCING:
773 err = phy_read_status(phydev);
775 if (err)
776 break;
778 if (phydev->link) {
779 phydev->state = PHY_RUNNING;
780 netif_carrier_on(phydev->attached_dev);
781 } else {
782 if (0 == phydev->link_timeout--) {
783 phy_force_reduction(phydev);
784 needs_aneg = 1;
788 phydev->adjust_link(phydev->attached_dev);
789 break;
790 case PHY_RUNNING:
791 /* Only register a CHANGE if we are
792 * polling */
793 if (PHY_POLL == phydev->irq)
794 phydev->state = PHY_CHANGELINK;
795 break;
796 case PHY_CHANGELINK:
797 err = phy_read_status(phydev);
799 if (err)
800 break;
802 if (phydev->link) {
803 phydev->state = PHY_RUNNING;
804 netif_carrier_on(phydev->attached_dev);
805 } else {
806 phydev->state = PHY_NOLINK;
807 netif_carrier_off(phydev->attached_dev);
810 phydev->adjust_link(phydev->attached_dev);
812 if (PHY_POLL != phydev->irq)
813 err = phy_config_interrupt(phydev,
814 PHY_INTERRUPT_ENABLED);
815 break;
816 case PHY_HALTED:
817 if (phydev->link) {
818 phydev->link = 0;
819 netif_carrier_off(phydev->attached_dev);
820 phydev->adjust_link(phydev->attached_dev);
822 break;
823 case PHY_RESUMING:
825 err = phy_clear_interrupt(phydev);
827 if (err)
828 break;
830 err = phy_config_interrupt(phydev,
831 PHY_INTERRUPT_ENABLED);
833 if (err)
834 break;
836 if (AUTONEG_ENABLE == phydev->autoneg) {
837 err = phy_aneg_done(phydev);
838 if (err < 0)
839 break;
841 /* err > 0 if AN is done.
842 * Otherwise, it's 0, and we're
843 * still waiting for AN */
844 if (err > 0) {
845 phydev->state = PHY_RUNNING;
846 } else {
847 phydev->state = PHY_AN;
848 phydev->link_timeout = PHY_AN_TIMEOUT;
850 } else
851 phydev->state = PHY_RUNNING;
852 break;
855 spin_unlock(&phydev->lock);
857 if (needs_aneg)
858 err = phy_start_aneg(phydev);
860 if (err < 0)
861 phy_error(phydev);
863 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);