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
9 * Copyright (c) 2004 Freescale Semiconductor, Inc.
10 * Copyright (c) 2006 Maciej W. Rozycki
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/etherdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
31 #include <linux/module.h>
32 #include <linux/mii.h>
33 #include <linux/ethtool.h>
34 #include <linux/phy.h>
35 #include <linux/timer.h>
36 #include <linux/workqueue.h>
40 #include <asm/uaccess.h>
43 * phy_print_status - Convenience function to print out the current phy status
44 * @phydev: the phy_device struct
46 void phy_print_status(struct phy_device *phydev)
48 pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,
49 phydev->link ? "Up" : "Down");
51 printk(" - %d/%s", phydev->speed,
52 DUPLEX_FULL == phydev->duplex ?
57 EXPORT_SYMBOL(phy_print_status);
61 * phy_read - Convenience function for reading a given PHY register
62 * @phydev: the phy_device struct
63 * @regnum: register number to read
65 * NOTE: MUST NOT be called from interrupt context,
66 * because the bus read/write functions may wait for an interrupt
67 * to conclude the operation.
69 int phy_read(struct phy_device *phydev, u16 regnum)
72 struct mii_bus *bus = phydev->bus;
74 spin_lock_bh(&bus->mdio_lock);
75 retval = bus->read(bus, phydev->addr, regnum);
76 spin_unlock_bh(&bus->mdio_lock);
80 EXPORT_SYMBOL(phy_read);
83 * phy_write - Convenience function for writing a given PHY register
84 * @phydev: the phy_device struct
85 * @regnum: register number to write
86 * @val: value to write to @regnum
88 * NOTE: MUST NOT be called from interrupt context,
89 * because the bus read/write functions may wait for an interrupt
90 * to conclude the operation.
92 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
95 struct mii_bus *bus = phydev->bus;
97 spin_lock_bh(&bus->mdio_lock);
98 err = bus->write(bus, phydev->addr, regnum, val);
99 spin_unlock_bh(&bus->mdio_lock);
103 EXPORT_SYMBOL(phy_write);
106 * phy_clear_interrupt - Ack the phy device's interrupt
107 * @phydev: the phy_device struct
109 * If the @phydev driver has an ack_interrupt function, call it to
110 * ack and clear the phy device's interrupt.
112 * Returns 0 on success on < 0 on error.
114 int phy_clear_interrupt(struct phy_device *phydev)
118 if (phydev->drv->ack_interrupt)
119 err = phydev->drv->ack_interrupt(phydev);
125 * phy_config_interrupt - configure the PHY device for the requested interrupts
126 * @phydev: the phy_device struct
127 * @interrupts: interrupt flags to configure for this @phydev
129 * Returns 0 on success on < 0 on error.
131 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
135 phydev->interrupts = interrupts;
136 if (phydev->drv->config_intr)
137 err = phydev->drv->config_intr(phydev);
144 * phy_aneg_done - return auto-negotiation status
145 * @phydev: target phy_device struct
147 * Description: Reads the status register and returns 0 either if
148 * auto-negotiation is incomplete, or if there was an error.
149 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
151 static inline int phy_aneg_done(struct phy_device *phydev)
155 retval = phy_read(phydev, MII_BMSR);
157 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
160 /* A structure for mapping a particular speed and duplex
161 * combination to a particular SUPPORTED and ADVERTISED value */
168 /* A mapping of all SUPPORTED settings to speed/duplex */
169 static const struct phy_setting settings[] = {
172 .duplex = DUPLEX_FULL,
173 .setting = SUPPORTED_10000baseT_Full,
177 .duplex = DUPLEX_FULL,
178 .setting = SUPPORTED_1000baseT_Full,
182 .duplex = DUPLEX_HALF,
183 .setting = SUPPORTED_1000baseT_Half,
187 .duplex = DUPLEX_FULL,
188 .setting = SUPPORTED_100baseT_Full,
192 .duplex = DUPLEX_HALF,
193 .setting = SUPPORTED_100baseT_Half,
197 .duplex = DUPLEX_FULL,
198 .setting = SUPPORTED_10baseT_Full,
202 .duplex = DUPLEX_HALF,
203 .setting = SUPPORTED_10baseT_Half,
207 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
210 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
211 * @speed: speed to match
212 * @duplex: duplex to match
214 * Description: Searches the settings array for the setting which
215 * matches the desired speed and duplex, and returns the index
216 * of that setting. Returns the index of the last setting if
217 * none of the others match.
219 static inline int phy_find_setting(int speed, int duplex)
223 while (idx < ARRAY_SIZE(settings) &&
224 (settings[idx].speed != speed ||
225 settings[idx].duplex != duplex))
228 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
232 * phy_find_valid - find a PHY setting that matches the requested features mask
233 * @idx: The first index in settings[] to search
234 * @features: A mask of the valid settings
236 * Description: Returns the index of the first valid setting less
237 * than or equal to the one pointed to by idx, as determined by
238 * the mask in features. Returns the index of the last setting
239 * if nothing else matches.
241 static inline int phy_find_valid(int idx, u32 features)
243 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
246 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
250 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
251 * @phydev: the target phy_device struct
253 * Description: Make sure the PHY is set to supported speeds and
254 * duplexes. Drop down by one in this order: 1000/FULL,
255 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
257 void phy_sanitize_settings(struct phy_device *phydev)
259 u32 features = phydev->supported;
262 /* Sanitize settings based on PHY capabilities */
263 if ((features & SUPPORTED_Autoneg) == 0)
264 phydev->autoneg = AUTONEG_DISABLE;
266 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
269 phydev->speed = settings[idx].speed;
270 phydev->duplex = settings[idx].duplex;
272 EXPORT_SYMBOL(phy_sanitize_settings);
275 * phy_ethtool_sset - generic ethtool sset function, handles all the details
276 * @phydev: target phy_device struct
279 * A few notes about parameter checking:
280 * - We don't set port or transceiver, so we don't care what they
282 * - phy_start_aneg() will make sure forced settings are sane, and
283 * choose the next best ones from the ones selected, so we don't
284 * care if ethtool tries to give us bad values.
286 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
288 if (cmd->phy_address != phydev->addr)
291 /* We make sure that we don't pass unsupported
292 * values in to the PHY */
293 cmd->advertising &= phydev->supported;
295 /* Verify the settings we care about. */
296 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
299 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
302 if (cmd->autoneg == AUTONEG_DISABLE
303 && ((cmd->speed != SPEED_1000
304 && cmd->speed != SPEED_100
305 && cmd->speed != SPEED_10)
306 || (cmd->duplex != DUPLEX_HALF
307 && cmd->duplex != DUPLEX_FULL)))
310 phydev->autoneg = cmd->autoneg;
312 phydev->speed = cmd->speed;
314 phydev->advertising = cmd->advertising;
316 if (AUTONEG_ENABLE == cmd->autoneg)
317 phydev->advertising |= ADVERTISED_Autoneg;
319 phydev->advertising &= ~ADVERTISED_Autoneg;
321 phydev->duplex = cmd->duplex;
323 /* Restart the PHY */
324 phy_start_aneg(phydev);
328 EXPORT_SYMBOL(phy_ethtool_sset);
330 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
332 cmd->supported = phydev->supported;
334 cmd->advertising = phydev->advertising;
336 cmd->speed = phydev->speed;
337 cmd->duplex = phydev->duplex;
338 cmd->port = PORT_MII;
339 cmd->phy_address = phydev->addr;
340 cmd->transceiver = XCVR_EXTERNAL;
341 cmd->autoneg = phydev->autoneg;
345 EXPORT_SYMBOL(phy_ethtool_gset);
348 * phy_mii_ioctl - generic PHY MII ioctl interface
349 * @phydev: the phy_device struct
350 * @mii_data: MII ioctl data
351 * @cmd: ioctl cmd to execute
353 * Note that this function is currently incompatible with the
354 * PHYCONTROL layer. It changes registers without regard to
355 * current state. Use at own risk.
357 int phy_mii_ioctl(struct phy_device *phydev,
358 struct mii_ioctl_data *mii_data, int cmd)
360 u16 val = mii_data->val_in;
364 mii_data->phy_id = phydev->addr;
367 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
371 if (!capable(CAP_NET_ADMIN))
374 if (mii_data->phy_id == phydev->addr) {
375 switch(mii_data->reg_num) {
377 if ((val & (BMCR_RESET|BMCR_ANENABLE)) == 0)
378 phydev->autoneg = AUTONEG_DISABLE;
380 phydev->autoneg = AUTONEG_ENABLE;
381 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
382 phydev->duplex = DUPLEX_FULL;
384 phydev->duplex = DUPLEX_HALF;
385 if ((!phydev->autoneg) &&
386 (val & BMCR_SPEED1000))
387 phydev->speed = SPEED_1000;
388 else if ((!phydev->autoneg) &&
389 (val & BMCR_SPEED100))
390 phydev->speed = SPEED_100;
393 phydev->advertising = val;
401 phy_write(phydev, mii_data->reg_num, val);
403 if (mii_data->reg_num == MII_BMCR
405 && phydev->drv->config_init)
406 phydev->drv->config_init(phydev);
412 EXPORT_SYMBOL(phy_mii_ioctl);
415 * phy_start_aneg - start auto-negotiation for this PHY device
416 * @phydev: the phy_device struct
418 * Description: Sanitizes the settings (if we're not autonegotiating
419 * them), and then calls the driver's config_aneg function.
420 * If the PHYCONTROL Layer is operating, we change the state to
421 * reflect the beginning of Auto-negotiation or forcing.
423 int phy_start_aneg(struct phy_device *phydev)
427 spin_lock(&phydev->lock);
429 if (AUTONEG_DISABLE == phydev->autoneg)
430 phy_sanitize_settings(phydev);
432 err = phydev->drv->config_aneg(phydev);
437 if (phydev->state != PHY_HALTED) {
438 if (AUTONEG_ENABLE == phydev->autoneg) {
439 phydev->state = PHY_AN;
440 phydev->link_timeout = PHY_AN_TIMEOUT;
442 phydev->state = PHY_FORCING;
443 phydev->link_timeout = PHY_FORCE_TIMEOUT;
448 spin_unlock(&phydev->lock);
451 EXPORT_SYMBOL(phy_start_aneg);
454 static void phy_change(struct work_struct *work);
455 static void phy_timer(unsigned long data);
458 * phy_start_machine - start PHY state machine tracking
459 * @phydev: the phy_device struct
460 * @handler: callback function for state change notifications
462 * Description: The PHY infrastructure can run a state machine
463 * which tracks whether the PHY is starting up, negotiating,
464 * etc. This function starts the timer which tracks the state
465 * of the PHY. If you want to be notified when the state changes,
466 * pass in the callback @handler, otherwise, pass NULL. If you
467 * want to maintain your own state machine, do not call this
470 void phy_start_machine(struct phy_device *phydev,
471 void (*handler)(struct net_device *))
473 phydev->adjust_state = handler;
475 init_timer(&phydev->phy_timer);
476 phydev->phy_timer.function = &phy_timer;
477 phydev->phy_timer.data = (unsigned long) phydev;
478 mod_timer(&phydev->phy_timer, jiffies + HZ);
482 * phy_stop_machine - stop the PHY state machine tracking
483 * @phydev: target phy_device struct
485 * Description: Stops the state machine timer, sets the state to UP
486 * (unless it wasn't up yet). This function must be called BEFORE
489 void phy_stop_machine(struct phy_device *phydev)
491 del_timer_sync(&phydev->phy_timer);
493 spin_lock(&phydev->lock);
494 if (phydev->state > PHY_UP)
495 phydev->state = PHY_UP;
496 spin_unlock(&phydev->lock);
498 phydev->adjust_state = NULL;
502 * phy_force_reduction - reduce PHY speed/duplex settings by one step
503 * @phydev: target phy_device struct
505 * Description: Reduces the speed/duplex settings by one notch,
507 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
508 * The function bottoms out at 10/HALF.
510 static void phy_force_reduction(struct phy_device *phydev)
514 idx = phy_find_setting(phydev->speed, phydev->duplex);
518 idx = phy_find_valid(idx, phydev->supported);
520 phydev->speed = settings[idx].speed;
521 phydev->duplex = settings[idx].duplex;
523 pr_info("Trying %d/%s\n", phydev->speed,
524 DUPLEX_FULL == phydev->duplex ?
530 * phy_error - enter HALTED state for this PHY device
531 * @phydev: target phy_device struct
533 * Moves the PHY to the HALTED state in response to a read
534 * or write error, and tells the controller the link is down.
535 * Must not be called from interrupt context, or while the
536 * phydev->lock is held.
538 void phy_error(struct phy_device *phydev)
540 spin_lock(&phydev->lock);
541 phydev->state = PHY_HALTED;
542 spin_unlock(&phydev->lock);
546 * phy_interrupt - PHY interrupt handler
547 * @irq: interrupt line
548 * @phy_dat: phy_device pointer
550 * Description: When a PHY interrupt occurs, the handler disables
551 * interrupts, and schedules a work task to clear the interrupt.
553 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
555 struct phy_device *phydev = phy_dat;
557 if (PHY_HALTED == phydev->state)
558 return IRQ_NONE; /* It can't be ours. */
560 /* The MDIO bus is not allowed to be written in interrupt
561 * context, so we need to disable the irq here. A work
562 * queue will write the PHY to disable and clear the
563 * interrupt, and then reenable the irq line. */
564 disable_irq_nosync(irq);
566 schedule_work(&phydev->phy_queue);
572 * phy_enable_interrupts - Enable the interrupts from the PHY side
573 * @phydev: target phy_device struct
575 int phy_enable_interrupts(struct phy_device *phydev)
579 err = phy_clear_interrupt(phydev);
584 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
588 EXPORT_SYMBOL(phy_enable_interrupts);
591 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
592 * @phydev: target phy_device struct
594 int phy_disable_interrupts(struct phy_device *phydev)
598 /* Disable PHY interrupts */
599 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
604 /* Clear the interrupt */
605 err = phy_clear_interrupt(phydev);
617 EXPORT_SYMBOL(phy_disable_interrupts);
620 * phy_start_interrupts - request and enable interrupts for a PHY device
621 * @phydev: target phy_device struct
623 * Description: Request the interrupt for the given PHY.
624 * If this fails, then we set irq to PHY_POLL.
625 * Otherwise, we enable the interrupts in the PHY.
626 * This should only be called with a valid IRQ number.
627 * Returns 0 on success or < 0 on error.
629 int phy_start_interrupts(struct phy_device *phydev)
633 INIT_WORK(&phydev->phy_queue, phy_change);
635 if (request_irq(phydev->irq, phy_interrupt,
639 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
642 phydev->irq = PHY_POLL;
646 err = phy_enable_interrupts(phydev);
650 EXPORT_SYMBOL(phy_start_interrupts);
653 * phy_stop_interrupts - disable interrupts from a PHY device
654 * @phydev: target phy_device struct
656 int phy_stop_interrupts(struct phy_device *phydev)
660 err = phy_disable_interrupts(phydev);
666 * Finish any pending work; we might have been scheduled to be called
667 * from keventd ourselves, but cancel_work_sync() handles that.
669 cancel_work_sync(&phydev->phy_queue);
671 free_irq(phydev->irq, phydev);
675 EXPORT_SYMBOL(phy_stop_interrupts);
679 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
680 * @work: work_struct that describes the work to be done
682 static void phy_change(struct work_struct *work)
685 struct phy_device *phydev =
686 container_of(work, struct phy_device, phy_queue);
688 err = phy_disable_interrupts(phydev);
693 spin_lock(&phydev->lock);
694 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
695 phydev->state = PHY_CHANGELINK;
696 spin_unlock(&phydev->lock);
698 enable_irq(phydev->irq);
700 /* Reenable interrupts */
701 if (PHY_HALTED != phydev->state)
702 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
710 disable_irq(phydev->irq);
716 * phy_stop - Bring down the PHY link, and stop checking the status
717 * @phydev: target phy_device struct
719 void phy_stop(struct phy_device *phydev)
721 spin_lock(&phydev->lock);
723 if (PHY_HALTED == phydev->state)
726 phydev->state = PHY_HALTED;
728 if (phydev->irq != PHY_POLL) {
729 /* Disable PHY Interrupts */
730 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
732 /* Clear any pending interrupts */
733 phy_clear_interrupt(phydev);
737 spin_unlock(&phydev->lock);
740 * Cannot call flush_scheduled_work() here as desired because
741 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
742 * will not reenable interrupts.
748 * phy_start - start or restart a PHY device
749 * @phydev: target phy_device struct
751 * Description: Indicates the attached device's readiness to
752 * handle PHY-related work. Used during startup to start the
753 * PHY, and after a call to phy_stop() to resume operation.
754 * Also used to indicate the MDIO bus has cleared an error
757 void phy_start(struct phy_device *phydev)
759 spin_lock_bh(&phydev->lock);
761 switch (phydev->state) {
763 phydev->state = PHY_PENDING;
766 phydev->state = PHY_UP;
769 phydev->state = PHY_RESUMING;
773 spin_unlock_bh(&phydev->lock);
775 EXPORT_SYMBOL(phy_stop);
776 EXPORT_SYMBOL(phy_start);
778 /* PHY timer which handles the state machine */
779 static void phy_timer(unsigned long data)
781 struct phy_device *phydev = (struct phy_device *)data;
785 spin_lock(&phydev->lock);
787 if (phydev->adjust_state)
788 phydev->adjust_state(phydev->attached_dev);
790 switch(phydev->state) {
799 phydev->link_timeout = PHY_AN_TIMEOUT;
803 err = phy_read_status(phydev);
808 /* If the link is down, give up on
809 * negotiation for now */
811 phydev->state = PHY_NOLINK;
812 netif_carrier_off(phydev->attached_dev);
813 phydev->adjust_link(phydev->attached_dev);
817 /* Check if negotiation is done. Break
818 * if there's an error */
819 err = phy_aneg_done(phydev);
823 /* If AN is done, we're running */
825 phydev->state = PHY_RUNNING;
826 netif_carrier_on(phydev->attached_dev);
827 phydev->adjust_link(phydev->attached_dev);
829 } else if (0 == phydev->link_timeout--) {
833 /* If we have the magic_aneg bit,
835 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
838 /* The timer expired, and we still
839 * don't have a setting, so we try
840 * forcing it until we find one that
841 * works, starting from the fastest speed,
842 * and working our way down */
843 idx = phy_find_valid(0, phydev->supported);
845 phydev->speed = settings[idx].speed;
846 phydev->duplex = settings[idx].duplex;
848 phydev->autoneg = AUTONEG_DISABLE;
850 pr_info("Trying %d/%s\n", phydev->speed,
857 err = phy_read_status(phydev);
863 phydev->state = PHY_RUNNING;
864 netif_carrier_on(phydev->attached_dev);
865 phydev->adjust_link(phydev->attached_dev);
869 err = genphy_update_link(phydev);
875 phydev->state = PHY_RUNNING;
876 netif_carrier_on(phydev->attached_dev);
878 if (0 == phydev->link_timeout--) {
879 phy_force_reduction(phydev);
884 phydev->adjust_link(phydev->attached_dev);
887 /* Only register a CHANGE if we are
889 if (PHY_POLL == phydev->irq)
890 phydev->state = PHY_CHANGELINK;
893 err = phy_read_status(phydev);
899 phydev->state = PHY_RUNNING;
900 netif_carrier_on(phydev->attached_dev);
902 phydev->state = PHY_NOLINK;
903 netif_carrier_off(phydev->attached_dev);
906 phydev->adjust_link(phydev->attached_dev);
908 if (PHY_POLL != phydev->irq)
909 err = phy_config_interrupt(phydev,
910 PHY_INTERRUPT_ENABLED);
915 netif_carrier_off(phydev->attached_dev);
916 phydev->adjust_link(phydev->attached_dev);
921 err = phy_clear_interrupt(phydev);
926 err = phy_config_interrupt(phydev,
927 PHY_INTERRUPT_ENABLED);
932 if (AUTONEG_ENABLE == phydev->autoneg) {
933 err = phy_aneg_done(phydev);
937 /* err > 0 if AN is done.
938 * Otherwise, it's 0, and we're
939 * still waiting for AN */
941 phydev->state = PHY_RUNNING;
943 phydev->state = PHY_AN;
944 phydev->link_timeout = PHY_AN_TIMEOUT;
947 phydev->state = PHY_RUNNING;
951 spin_unlock(&phydev->lock);
954 err = phy_start_aneg(phydev);
959 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);