[PATCH] phy: Added a macro to represent the string format used to match a phy device
[linux-2.6/libata-dev.git] / drivers / net / phy / phy.c
blob1474b7c5ac0bd5c882c29a6d77fa0708aac664c5
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/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39 #include <asm/uaccess.h>
41 /* Convenience function to print out the current phy status
43 void phy_print_status(struct phy_device *phydev)
45 pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,
46 phydev->link ? "Up" : "Down");
47 if (phydev->link)
48 printk(" - %d/%s", phydev->speed,
49 DUPLEX_FULL == phydev->duplex ?
50 "Full" : "Half");
52 printk("\n");
54 EXPORT_SYMBOL(phy_print_status);
57 /* Convenience functions for reading/writing a given PHY
58 * register. They MUST NOT be called from interrupt context,
59 * because the bus read/write functions may wait for an interrupt
60 * to conclude the operation. */
61 int phy_read(struct phy_device *phydev, u16 regnum)
63 int retval;
64 struct mii_bus *bus = phydev->bus;
66 spin_lock_bh(&bus->mdio_lock);
67 retval = bus->read(bus, phydev->addr, regnum);
68 spin_unlock_bh(&bus->mdio_lock);
70 return retval;
72 EXPORT_SYMBOL(phy_read);
74 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
76 int err;
77 struct mii_bus *bus = phydev->bus;
79 spin_lock_bh(&bus->mdio_lock);
80 err = bus->write(bus, phydev->addr, regnum, val);
81 spin_unlock_bh(&bus->mdio_lock);
83 return err;
85 EXPORT_SYMBOL(phy_write);
88 int phy_clear_interrupt(struct phy_device *phydev)
90 int err = 0;
92 if (phydev->drv->ack_interrupt)
93 err = phydev->drv->ack_interrupt(phydev);
95 return err;
99 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
101 int err = 0;
103 phydev->interrupts = interrupts;
104 if (phydev->drv->config_intr)
105 err = phydev->drv->config_intr(phydev);
107 return err;
111 /* phy_aneg_done
113 * description: Reads the status register and returns 0 either if
114 * auto-negotiation is incomplete, or if there was an error.
115 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
117 static inline int phy_aneg_done(struct phy_device *phydev)
119 int retval;
121 retval = phy_read(phydev, MII_BMSR);
123 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
126 /* A structure for mapping a particular speed and duplex
127 * combination to a particular SUPPORTED and ADVERTISED value */
128 struct phy_setting {
129 int speed;
130 int duplex;
131 u32 setting;
134 /* A mapping of all SUPPORTED settings to speed/duplex */
135 static struct phy_setting settings[] = {
137 .speed = 10000,
138 .duplex = DUPLEX_FULL,
139 .setting = SUPPORTED_10000baseT_Full,
142 .speed = SPEED_1000,
143 .duplex = DUPLEX_FULL,
144 .setting = SUPPORTED_1000baseT_Full,
147 .speed = SPEED_1000,
148 .duplex = DUPLEX_HALF,
149 .setting = SUPPORTED_1000baseT_Half,
152 .speed = SPEED_100,
153 .duplex = DUPLEX_FULL,
154 .setting = SUPPORTED_100baseT_Full,
157 .speed = SPEED_100,
158 .duplex = DUPLEX_HALF,
159 .setting = SUPPORTED_100baseT_Half,
162 .speed = SPEED_10,
163 .duplex = DUPLEX_FULL,
164 .setting = SUPPORTED_10baseT_Full,
167 .speed = SPEED_10,
168 .duplex = DUPLEX_HALF,
169 .setting = SUPPORTED_10baseT_Half,
173 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
175 /* phy_find_setting
177 * description: Searches the settings array for the setting which
178 * matches the desired speed and duplex, and returns the index
179 * of that setting. Returns the index of the last setting if
180 * none of the others match.
182 static inline int phy_find_setting(int speed, int duplex)
184 int idx = 0;
186 while (idx < ARRAY_SIZE(settings) &&
187 (settings[idx].speed != speed ||
188 settings[idx].duplex != duplex))
189 idx++;
191 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
194 /* phy_find_valid
195 * idx: The first index in settings[] to search
196 * features: A mask of the valid settings
198 * description: Returns the index of the first valid setting less
199 * than or equal to the one pointed to by idx, as determined by
200 * the mask in features. Returns the index of the last setting
201 * if nothing else matches.
203 static inline int phy_find_valid(int idx, u32 features)
205 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
206 idx++;
208 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
211 /* phy_sanitize_settings
213 * description: Make sure the PHY is set to supported speeds and
214 * duplexes. Drop down by one in this order: 1000/FULL,
215 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
217 void phy_sanitize_settings(struct phy_device *phydev)
219 u32 features = phydev->supported;
220 int idx;
222 /* Sanitize settings based on PHY capabilities */
223 if ((features & SUPPORTED_Autoneg) == 0)
224 phydev->autoneg = 0;
226 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
227 features);
229 phydev->speed = settings[idx].speed;
230 phydev->duplex = settings[idx].duplex;
232 EXPORT_SYMBOL(phy_sanitize_settings);
234 /* phy_ethtool_sset:
235 * A generic ethtool sset function. Handles all the details
237 * A few notes about parameter checking:
238 * - We don't set port or transceiver, so we don't care what they
239 * were set to.
240 * - phy_start_aneg() will make sure forced settings are sane, and
241 * choose the next best ones from the ones selected, so we don't
242 * care if ethtool tries to give us bad values
245 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
247 if (cmd->phy_address != phydev->addr)
248 return -EINVAL;
250 /* We make sure that we don't pass unsupported
251 * values in to the PHY */
252 cmd->advertising &= phydev->supported;
254 /* Verify the settings we care about. */
255 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
256 return -EINVAL;
258 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
259 return -EINVAL;
261 if (cmd->autoneg == AUTONEG_DISABLE
262 && ((cmd->speed != SPEED_1000
263 && cmd->speed != SPEED_100
264 && cmd->speed != SPEED_10)
265 || (cmd->duplex != DUPLEX_HALF
266 && cmd->duplex != DUPLEX_FULL)))
267 return -EINVAL;
269 phydev->autoneg = cmd->autoneg;
271 phydev->speed = cmd->speed;
273 phydev->advertising = cmd->advertising;
275 if (AUTONEG_ENABLE == cmd->autoneg)
276 phydev->advertising |= ADVERTISED_Autoneg;
277 else
278 phydev->advertising &= ~ADVERTISED_Autoneg;
280 phydev->duplex = cmd->duplex;
282 /* Restart the PHY */
283 phy_start_aneg(phydev);
285 return 0;
288 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
290 cmd->supported = phydev->supported;
292 cmd->advertising = phydev->advertising;
294 cmd->speed = phydev->speed;
295 cmd->duplex = phydev->duplex;
296 cmd->port = PORT_MII;
297 cmd->phy_address = phydev->addr;
298 cmd->transceiver = XCVR_EXTERNAL;
299 cmd->autoneg = phydev->autoneg;
301 return 0;
305 /* Note that this function is currently incompatible with the
306 * PHYCONTROL layer. It changes registers without regard to
307 * current state. Use at own risk
309 int phy_mii_ioctl(struct phy_device *phydev,
310 struct mii_ioctl_data *mii_data, int cmd)
312 u16 val = mii_data->val_in;
314 switch (cmd) {
315 case SIOCGMIIPHY:
316 mii_data->phy_id = phydev->addr;
317 break;
318 case SIOCGMIIREG:
319 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
320 break;
322 case SIOCSMIIREG:
323 if (!capable(CAP_NET_ADMIN))
324 return -EPERM;
326 if (mii_data->phy_id == phydev->addr) {
327 switch(mii_data->reg_num) {
328 case MII_BMCR:
329 if (val & (BMCR_RESET|BMCR_ANENABLE))
330 phydev->autoneg = AUTONEG_DISABLE;
331 else
332 phydev->autoneg = AUTONEG_ENABLE;
333 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
334 phydev->duplex = DUPLEX_FULL;
335 else
336 phydev->duplex = DUPLEX_HALF;
337 break;
338 case MII_ADVERTISE:
339 phydev->advertising = val;
340 break;
341 default:
342 /* do nothing */
343 break;
347 phy_write(phydev, mii_data->reg_num, val);
349 if (mii_data->reg_num == MII_BMCR
350 && val & BMCR_RESET
351 && phydev->drv->config_init)
352 phydev->drv->config_init(phydev);
353 break;
356 return 0;
359 /* phy_start_aneg
361 * description: Sanitizes the settings (if we're not
362 * autonegotiating them), and then calls the driver's
363 * config_aneg function. If the PHYCONTROL Layer is operating,
364 * we change the state to reflect the beginning of
365 * Auto-negotiation or forcing.
367 int phy_start_aneg(struct phy_device *phydev)
369 int err;
371 spin_lock(&phydev->lock);
373 if (AUTONEG_DISABLE == phydev->autoneg)
374 phy_sanitize_settings(phydev);
376 err = phydev->drv->config_aneg(phydev);
378 if (err < 0)
379 goto out_unlock;
381 if (phydev->state != PHY_HALTED) {
382 if (AUTONEG_ENABLE == phydev->autoneg) {
383 phydev->state = PHY_AN;
384 phydev->link_timeout = PHY_AN_TIMEOUT;
385 } else {
386 phydev->state = PHY_FORCING;
387 phydev->link_timeout = PHY_FORCE_TIMEOUT;
391 out_unlock:
392 spin_unlock(&phydev->lock);
393 return err;
395 EXPORT_SYMBOL(phy_start_aneg);
398 static void phy_change(void *data);
399 static void phy_timer(unsigned long data);
401 /* phy_start_machine:
403 * description: The PHY infrastructure can run a state machine
404 * which tracks whether the PHY is starting up, negotiating,
405 * etc. This function starts the timer which tracks the state
406 * of the PHY. If you want to be notified when the state
407 * changes, pass in the callback, otherwise, pass NULL. If you
408 * want to maintain your own state machine, do not call this
409 * function. */
410 void phy_start_machine(struct phy_device *phydev,
411 void (*handler)(struct net_device *))
413 phydev->adjust_state = handler;
415 init_timer(&phydev->phy_timer);
416 phydev->phy_timer.function = &phy_timer;
417 phydev->phy_timer.data = (unsigned long) phydev;
418 mod_timer(&phydev->phy_timer, jiffies + HZ);
421 /* phy_stop_machine
423 * description: Stops the state machine timer, sets the state to
424 * UP (unless it wasn't up yet), and then frees the interrupt,
425 * if it is in use. This function must be called BEFORE
426 * phy_detach.
428 void phy_stop_machine(struct phy_device *phydev)
430 del_timer_sync(&phydev->phy_timer);
432 spin_lock(&phydev->lock);
433 if (phydev->state > PHY_UP)
434 phydev->state = PHY_UP;
435 spin_unlock(&phydev->lock);
437 if (phydev->irq != PHY_POLL)
438 phy_stop_interrupts(phydev);
440 phydev->adjust_state = NULL;
443 /* phy_force_reduction
445 * description: Reduces the speed/duplex settings by
446 * one notch. The order is so:
447 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
448 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
450 static void phy_force_reduction(struct phy_device *phydev)
452 int idx;
454 idx = phy_find_setting(phydev->speed, phydev->duplex);
456 idx++;
458 idx = phy_find_valid(idx, phydev->supported);
460 phydev->speed = settings[idx].speed;
461 phydev->duplex = settings[idx].duplex;
463 pr_info("Trying %d/%s\n", phydev->speed,
464 DUPLEX_FULL == phydev->duplex ?
465 "FULL" : "HALF");
469 /* phy_error:
471 * Moves the PHY to the HALTED state in response to a read
472 * or write error, and tells the controller the link is down.
473 * Must not be called from interrupt context, or while the
474 * phydev->lock is held.
476 void phy_error(struct phy_device *phydev)
478 spin_lock(&phydev->lock);
479 phydev->state = PHY_HALTED;
480 spin_unlock(&phydev->lock);
483 /* phy_interrupt
485 * description: When a PHY interrupt occurs, the handler disables
486 * interrupts, and schedules a work task to clear the interrupt.
488 static irqreturn_t phy_interrupt(int irq, void *phy_dat, struct pt_regs *regs)
490 struct phy_device *phydev = phy_dat;
492 /* The MDIO bus is not allowed to be written in interrupt
493 * context, so we need to disable the irq here. A work
494 * queue will write the PHY to disable and clear the
495 * interrupt, and then reenable the irq line. */
496 disable_irq_nosync(irq);
498 schedule_work(&phydev->phy_queue);
500 return IRQ_HANDLED;
503 /* Enable the interrupts from the PHY side */
504 int phy_enable_interrupts(struct phy_device *phydev)
506 int err;
508 err = phy_clear_interrupt(phydev);
510 if (err < 0)
511 return err;
513 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
515 return err;
517 EXPORT_SYMBOL(phy_enable_interrupts);
519 /* Disable the PHY interrupts from the PHY side */
520 int phy_disable_interrupts(struct phy_device *phydev)
522 int err;
524 /* Disable PHY interrupts */
525 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
527 if (err)
528 goto phy_err;
530 /* Clear the interrupt */
531 err = phy_clear_interrupt(phydev);
533 if (err)
534 goto phy_err;
536 return 0;
538 phy_err:
539 phy_error(phydev);
541 return err;
543 EXPORT_SYMBOL(phy_disable_interrupts);
545 /* phy_start_interrupts
547 * description: Request the interrupt for the given PHY. If
548 * this fails, then we set irq to PHY_POLL.
549 * Otherwise, we enable the interrupts in the PHY.
550 * Returns 0 on success.
551 * This should only be called with a valid IRQ number.
553 int phy_start_interrupts(struct phy_device *phydev)
555 int err = 0;
557 INIT_WORK(&phydev->phy_queue, phy_change, phydev);
559 if (request_irq(phydev->irq, phy_interrupt,
560 SA_SHIRQ,
561 "phy_interrupt",
562 phydev) < 0) {
563 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
564 phydev->bus->name,
565 phydev->irq);
566 phydev->irq = PHY_POLL;
567 return 0;
570 err = phy_enable_interrupts(phydev);
572 return err;
574 EXPORT_SYMBOL(phy_start_interrupts);
576 int phy_stop_interrupts(struct phy_device *phydev)
578 int err;
580 err = phy_disable_interrupts(phydev);
582 if (err)
583 phy_error(phydev);
585 free_irq(phydev->irq, phydev);
587 return err;
589 EXPORT_SYMBOL(phy_stop_interrupts);
592 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
593 static void phy_change(void *data)
595 int err;
596 struct phy_device *phydev = data;
598 err = phy_disable_interrupts(phydev);
600 if (err)
601 goto phy_err;
603 spin_lock(&phydev->lock);
604 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
605 phydev->state = PHY_CHANGELINK;
606 spin_unlock(&phydev->lock);
608 enable_irq(phydev->irq);
610 /* Reenable interrupts */
611 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
613 if (err)
614 goto irq_enable_err;
616 return;
618 irq_enable_err:
619 disable_irq(phydev->irq);
620 phy_err:
621 phy_error(phydev);
624 /* Bring down the PHY link, and stop checking the status. */
625 void phy_stop(struct phy_device *phydev)
627 spin_lock(&phydev->lock);
629 if (PHY_HALTED == phydev->state)
630 goto out_unlock;
632 if (phydev->irq != PHY_POLL) {
633 /* Clear any pending interrupts */
634 phy_clear_interrupt(phydev);
636 /* Disable PHY Interrupts */
637 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
640 phydev->state = PHY_HALTED;
642 out_unlock:
643 spin_unlock(&phydev->lock);
647 /* phy_start
649 * description: Indicates the attached device's readiness to
650 * handle PHY-related work. Used during startup to start the
651 * PHY, and after a call to phy_stop() to resume operation.
652 * Also used to indicate the MDIO bus has cleared an error
653 * condition.
655 void phy_start(struct phy_device *phydev)
657 spin_lock(&phydev->lock);
659 switch (phydev->state) {
660 case PHY_STARTING:
661 phydev->state = PHY_PENDING;
662 break;
663 case PHY_READY:
664 phydev->state = PHY_UP;
665 break;
666 case PHY_HALTED:
667 phydev->state = PHY_RESUMING;
668 default:
669 break;
671 spin_unlock(&phydev->lock);
673 EXPORT_SYMBOL(phy_stop);
674 EXPORT_SYMBOL(phy_start);
676 /* PHY timer which handles the state machine */
677 static void phy_timer(unsigned long data)
679 struct phy_device *phydev = (struct phy_device *)data;
680 int needs_aneg = 0;
681 int err = 0;
683 spin_lock(&phydev->lock);
685 if (phydev->adjust_state)
686 phydev->adjust_state(phydev->attached_dev);
688 switch(phydev->state) {
689 case PHY_DOWN:
690 case PHY_STARTING:
691 case PHY_READY:
692 case PHY_PENDING:
693 break;
694 case PHY_UP:
695 needs_aneg = 1;
697 phydev->link_timeout = PHY_AN_TIMEOUT;
699 break;
700 case PHY_AN:
701 /* Check if negotiation is done. Break
702 * if there's an error */
703 err = phy_aneg_done(phydev);
704 if (err < 0)
705 break;
707 /* If auto-negotiation is done, we change to
708 * either RUNNING, or NOLINK */
709 if (err > 0) {
710 err = phy_read_status(phydev);
712 if (err)
713 break;
715 if (phydev->link) {
716 phydev->state = PHY_RUNNING;
717 netif_carrier_on(phydev->attached_dev);
718 } else {
719 phydev->state = PHY_NOLINK;
720 netif_carrier_off(phydev->attached_dev);
723 phydev->adjust_link(phydev->attached_dev);
725 } else if (0 == phydev->link_timeout--) {
726 /* The counter expired, so either we
727 * switch to forced mode, or the
728 * magic_aneg bit exists, and we try aneg
729 * again */
730 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
731 int idx;
733 /* We'll start from the
734 * fastest speed, and work
735 * our way down */
736 idx = phy_find_valid(0,
737 phydev->supported);
739 phydev->speed = settings[idx].speed;
740 phydev->duplex = settings[idx].duplex;
742 phydev->autoneg = AUTONEG_DISABLE;
743 phydev->state = PHY_FORCING;
744 phydev->link_timeout =
745 PHY_FORCE_TIMEOUT;
747 pr_info("Trying %d/%s\n",
748 phydev->speed,
749 DUPLEX_FULL ==
750 phydev->duplex ?
751 "FULL" : "HALF");
754 needs_aneg = 1;
756 break;
757 case PHY_NOLINK:
758 err = phy_read_status(phydev);
760 if (err)
761 break;
763 if (phydev->link) {
764 phydev->state = PHY_RUNNING;
765 netif_carrier_on(phydev->attached_dev);
766 phydev->adjust_link(phydev->attached_dev);
768 break;
769 case PHY_FORCING:
770 err = phy_read_status(phydev);
772 if (err)
773 break;
775 if (phydev->link) {
776 phydev->state = PHY_RUNNING;
777 netif_carrier_on(phydev->attached_dev);
778 } else {
779 if (0 == phydev->link_timeout--) {
780 phy_force_reduction(phydev);
781 needs_aneg = 1;
785 phydev->adjust_link(phydev->attached_dev);
786 break;
787 case PHY_RUNNING:
788 /* Only register a CHANGE if we are
789 * polling */
790 if (PHY_POLL == phydev->irq)
791 phydev->state = PHY_CHANGELINK;
792 break;
793 case PHY_CHANGELINK:
794 err = phy_read_status(phydev);
796 if (err)
797 break;
799 if (phydev->link) {
800 phydev->state = PHY_RUNNING;
801 netif_carrier_on(phydev->attached_dev);
802 } else {
803 phydev->state = PHY_NOLINK;
804 netif_carrier_off(phydev->attached_dev);
807 phydev->adjust_link(phydev->attached_dev);
809 if (PHY_POLL != phydev->irq)
810 err = phy_config_interrupt(phydev,
811 PHY_INTERRUPT_ENABLED);
812 break;
813 case PHY_HALTED:
814 if (phydev->link) {
815 phydev->link = 0;
816 netif_carrier_off(phydev->attached_dev);
817 phydev->adjust_link(phydev->attached_dev);
819 break;
820 case PHY_RESUMING:
822 err = phy_clear_interrupt(phydev);
824 if (err)
825 break;
827 err = phy_config_interrupt(phydev,
828 PHY_INTERRUPT_ENABLED);
830 if (err)
831 break;
833 if (AUTONEG_ENABLE == phydev->autoneg) {
834 err = phy_aneg_done(phydev);
835 if (err < 0)
836 break;
838 /* err > 0 if AN is done.
839 * Otherwise, it's 0, and we're
840 * still waiting for AN */
841 if (err > 0) {
842 phydev->state = PHY_RUNNING;
843 } else {
844 phydev->state = PHY_AN;
845 phydev->link_timeout = PHY_AN_TIMEOUT;
847 } else
848 phydev->state = PHY_RUNNING;
849 break;
852 spin_unlock(&phydev->lock);
854 if (needs_aneg)
855 err = phy_start_aneg(phydev);
857 if (err < 0)
858 phy_error(phydev);
860 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);