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>
42 /* Convenience function to print out the current phy status
44 void phy_print_status(struct phy_device *phydev)
46 pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,
47 phydev->link ? "Up" : "Down");
49 printk(" - %d/%s", phydev->speed,
50 DUPLEX_FULL == phydev->duplex ?
55 EXPORT_SYMBOL(phy_print_status);
58 /* Convenience functions for reading/writing a given PHY
59 * register. They MUST NOT be called from interrupt context,
60 * because the bus read/write functions may wait for an interrupt
61 * to conclude the operation. */
62 int phy_read(struct phy_device *phydev, u16 regnum)
65 struct mii_bus *bus = phydev->bus;
67 spin_lock_bh(&bus->mdio_lock);
68 retval = bus->read(bus, phydev->addr, regnum);
69 spin_unlock_bh(&bus->mdio_lock);
73 EXPORT_SYMBOL(phy_read);
75 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
78 struct mii_bus *bus = phydev->bus;
80 spin_lock_bh(&bus->mdio_lock);
81 err = bus->write(bus, phydev->addr, regnum, val);
82 spin_unlock_bh(&bus->mdio_lock);
86 EXPORT_SYMBOL(phy_write);
89 int phy_clear_interrupt(struct phy_device *phydev)
93 if (phydev->drv->ack_interrupt)
94 err = phydev->drv->ack_interrupt(phydev);
100 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
104 phydev->interrupts = interrupts;
105 if (phydev->drv->config_intr)
106 err = phydev->drv->config_intr(phydev);
114 * description: Reads the status register and returns 0 either if
115 * auto-negotiation is incomplete, or if there was an error.
116 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
118 static inline int phy_aneg_done(struct phy_device *phydev)
122 retval = phy_read(phydev, MII_BMSR);
124 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
127 /* A structure for mapping a particular speed and duplex
128 * combination to a particular SUPPORTED and ADVERTISED value */
135 /* A mapping of all SUPPORTED settings to speed/duplex */
136 static const struct phy_setting settings[] = {
139 .duplex = DUPLEX_FULL,
140 .setting = SUPPORTED_10000baseT_Full,
144 .duplex = DUPLEX_FULL,
145 .setting = SUPPORTED_1000baseT_Full,
149 .duplex = DUPLEX_HALF,
150 .setting = SUPPORTED_1000baseT_Half,
154 .duplex = DUPLEX_FULL,
155 .setting = SUPPORTED_100baseT_Full,
159 .duplex = DUPLEX_HALF,
160 .setting = SUPPORTED_100baseT_Half,
164 .duplex = DUPLEX_FULL,
165 .setting = SUPPORTED_10baseT_Full,
169 .duplex = DUPLEX_HALF,
170 .setting = SUPPORTED_10baseT_Half,
174 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
178 * description: Searches the settings array for the setting which
179 * matches the desired speed and duplex, and returns the index
180 * of that setting. Returns the index of the last setting if
181 * none of the others match.
183 static inline int phy_find_setting(int speed, int duplex)
187 while (idx < ARRAY_SIZE(settings) &&
188 (settings[idx].speed != speed ||
189 settings[idx].duplex != duplex))
192 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
196 * idx: The first index in settings[] to search
197 * features: A mask of the valid settings
199 * description: Returns the index of the first valid setting less
200 * than or equal to the one pointed to by idx, as determined by
201 * the mask in features. Returns the index of the last setting
202 * if nothing else matches.
204 static inline int phy_find_valid(int idx, u32 features)
206 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
209 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
212 /* phy_sanitize_settings
214 * description: Make sure the PHY is set to supported speeds and
215 * duplexes. Drop down by one in this order: 1000/FULL,
216 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
218 void phy_sanitize_settings(struct phy_device *phydev)
220 u32 features = phydev->supported;
223 /* Sanitize settings based on PHY capabilities */
224 if ((features & SUPPORTED_Autoneg) == 0)
227 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
230 phydev->speed = settings[idx].speed;
231 phydev->duplex = settings[idx].duplex;
233 EXPORT_SYMBOL(phy_sanitize_settings);
236 * A generic ethtool sset function. Handles all the details
238 * A few notes about parameter checking:
239 * - We don't set port or transceiver, so we don't care what they
241 * - phy_start_aneg() will make sure forced settings are sane, and
242 * choose the next best ones from the ones selected, so we don't
243 * care if ethtool tries to give us bad values
246 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
248 if (cmd->phy_address != phydev->addr)
251 /* We make sure that we don't pass unsupported
252 * values in to the PHY */
253 cmd->advertising &= phydev->supported;
255 /* Verify the settings we care about. */
256 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
259 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
262 if (cmd->autoneg == AUTONEG_DISABLE
263 && ((cmd->speed != SPEED_1000
264 && cmd->speed != SPEED_100
265 && cmd->speed != SPEED_10)
266 || (cmd->duplex != DUPLEX_HALF
267 && cmd->duplex != DUPLEX_FULL)))
270 phydev->autoneg = cmd->autoneg;
272 phydev->speed = cmd->speed;
274 phydev->advertising = cmd->advertising;
276 if (AUTONEG_ENABLE == cmd->autoneg)
277 phydev->advertising |= ADVERTISED_Autoneg;
279 phydev->advertising &= ~ADVERTISED_Autoneg;
281 phydev->duplex = cmd->duplex;
283 /* Restart the PHY */
284 phy_start_aneg(phydev);
288 EXPORT_SYMBOL(phy_ethtool_sset);
290 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
292 cmd->supported = phydev->supported;
294 cmd->advertising = phydev->advertising;
296 cmd->speed = phydev->speed;
297 cmd->duplex = phydev->duplex;
298 cmd->port = PORT_MII;
299 cmd->phy_address = phydev->addr;
300 cmd->transceiver = XCVR_EXTERNAL;
301 cmd->autoneg = phydev->autoneg;
305 EXPORT_SYMBOL(phy_ethtool_gset);
307 /* Note that this function is currently incompatible with the
308 * PHYCONTROL layer. It changes registers without regard to
309 * current state. Use at own risk
311 int phy_mii_ioctl(struct phy_device *phydev,
312 struct mii_ioctl_data *mii_data, int cmd)
314 u16 val = mii_data->val_in;
318 mii_data->phy_id = phydev->addr;
321 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
325 if (!capable(CAP_NET_ADMIN))
328 if (mii_data->phy_id == phydev->addr) {
329 switch(mii_data->reg_num) {
331 if (val & (BMCR_RESET|BMCR_ANENABLE))
332 phydev->autoneg = AUTONEG_DISABLE;
334 phydev->autoneg = AUTONEG_ENABLE;
335 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
336 phydev->duplex = DUPLEX_FULL;
338 phydev->duplex = DUPLEX_HALF;
341 phydev->advertising = val;
349 phy_write(phydev, mii_data->reg_num, val);
351 if (mii_data->reg_num == MII_BMCR
353 && phydev->drv->config_init)
354 phydev->drv->config_init(phydev);
363 * description: Sanitizes the settings (if we're not
364 * autonegotiating them), and then calls the driver's
365 * config_aneg function. If the PHYCONTROL Layer is operating,
366 * we change the state to reflect the beginning of
367 * Auto-negotiation or forcing.
369 int phy_start_aneg(struct phy_device *phydev)
373 spin_lock(&phydev->lock);
375 if (AUTONEG_DISABLE == phydev->autoneg)
376 phy_sanitize_settings(phydev);
378 err = phydev->drv->config_aneg(phydev);
383 if (phydev->state != PHY_HALTED) {
384 if (AUTONEG_ENABLE == phydev->autoneg) {
385 phydev->state = PHY_AN;
386 phydev->link_timeout = PHY_AN_TIMEOUT;
388 phydev->state = PHY_FORCING;
389 phydev->link_timeout = PHY_FORCE_TIMEOUT;
394 spin_unlock(&phydev->lock);
397 EXPORT_SYMBOL(phy_start_aneg);
400 static void phy_change(struct work_struct *work);
401 static void phy_timer(unsigned long data);
403 /* phy_start_machine:
405 * description: The PHY infrastructure can run a state machine
406 * which tracks whether the PHY is starting up, negotiating,
407 * etc. This function starts the timer which tracks the state
408 * of the PHY. If you want to be notified when the state
409 * changes, pass in the callback, otherwise, pass NULL. If you
410 * want to maintain your own state machine, do not call this
412 void phy_start_machine(struct phy_device *phydev,
413 void (*handler)(struct net_device *))
415 phydev->adjust_state = handler;
417 init_timer(&phydev->phy_timer);
418 phydev->phy_timer.function = &phy_timer;
419 phydev->phy_timer.data = (unsigned long) phydev;
420 mod_timer(&phydev->phy_timer, jiffies + HZ);
425 * description: Stops the state machine timer, sets the state to UP
426 * (unless it wasn't up yet). This function must be called BEFORE
429 void phy_stop_machine(struct phy_device *phydev)
431 del_timer_sync(&phydev->phy_timer);
433 spin_lock(&phydev->lock);
434 if (phydev->state > PHY_UP)
435 phydev->state = PHY_UP;
436 spin_unlock(&phydev->lock);
438 phydev->adjust_state = NULL;
441 /* phy_force_reduction
443 * description: Reduces the speed/duplex settings by
444 * one notch. The order is so:
445 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
446 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
448 static void phy_force_reduction(struct phy_device *phydev)
452 idx = phy_find_setting(phydev->speed, phydev->duplex);
456 idx = phy_find_valid(idx, phydev->supported);
458 phydev->speed = settings[idx].speed;
459 phydev->duplex = settings[idx].duplex;
461 pr_info("Trying %d/%s\n", phydev->speed,
462 DUPLEX_FULL == phydev->duplex ?
469 * Moves the PHY to the HALTED state in response to a read
470 * or write error, and tells the controller the link is down.
471 * Must not be called from interrupt context, or while the
472 * phydev->lock is held.
474 void phy_error(struct phy_device *phydev)
476 spin_lock(&phydev->lock);
477 phydev->state = PHY_HALTED;
478 spin_unlock(&phydev->lock);
483 * description: When a PHY interrupt occurs, the handler disables
484 * interrupts, and schedules a work task to clear the interrupt.
486 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
488 struct phy_device *phydev = phy_dat;
490 if (PHY_HALTED == phydev->state)
491 return IRQ_NONE; /* It can't be ours. */
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);
504 /* Enable the interrupts from the PHY side */
505 int phy_enable_interrupts(struct phy_device *phydev)
509 err = phy_clear_interrupt(phydev);
514 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
518 EXPORT_SYMBOL(phy_enable_interrupts);
520 /* Disable the PHY interrupts from the PHY side */
521 int phy_disable_interrupts(struct phy_device *phydev)
525 /* Disable PHY interrupts */
526 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
531 /* Clear the interrupt */
532 err = phy_clear_interrupt(phydev);
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)
558 INIT_WORK(&phydev->phy_queue, phy_change);
560 if (request_irq(phydev->irq, phy_interrupt,
564 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
567 phydev->irq = PHY_POLL;
571 err = phy_enable_interrupts(phydev);
575 EXPORT_SYMBOL(phy_start_interrupts);
577 int phy_stop_interrupts(struct phy_device *phydev)
581 err = phy_disable_interrupts(phydev);
587 * Finish any pending work; we might have been scheduled
588 * to be called from keventd ourselves, though.
590 run_scheduled_work(&phydev->phy_queue);
592 free_irq(phydev->irq, phydev);
596 EXPORT_SYMBOL(phy_stop_interrupts);
599 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
600 static void phy_change(struct work_struct *work)
603 struct phy_device *phydev =
604 container_of(work, struct phy_device, phy_queue);
606 err = phy_disable_interrupts(phydev);
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);
616 enable_irq(phydev->irq);
618 /* Reenable interrupts */
619 if (PHY_HALTED != phydev->state)
620 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
628 disable_irq(phydev->irq);
633 /* Bring down the PHY link, and stop checking the status. */
634 void phy_stop(struct phy_device *phydev)
636 spin_lock(&phydev->lock);
638 if (PHY_HALTED == phydev->state)
641 phydev->state = PHY_HALTED;
643 if (phydev->irq != PHY_POLL) {
644 /* Disable PHY Interrupts */
645 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
647 /* Clear any pending interrupts */
648 phy_clear_interrupt(phydev);
652 spin_unlock(&phydev->lock);
655 * Cannot call flush_scheduled_work() here as desired because
656 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
657 * will not reenable interrupts.
664 * description: Indicates the attached device's readiness to
665 * handle PHY-related work. Used during startup to start the
666 * PHY, and after a call to phy_stop() to resume operation.
667 * Also used to indicate the MDIO bus has cleared an error
670 void phy_start(struct phy_device *phydev)
672 spin_lock(&phydev->lock);
674 switch (phydev->state) {
676 phydev->state = PHY_PENDING;
679 phydev->state = PHY_UP;
682 phydev->state = PHY_RESUMING;
686 spin_unlock(&phydev->lock);
688 EXPORT_SYMBOL(phy_stop);
689 EXPORT_SYMBOL(phy_start);
691 /* PHY timer which handles the state machine */
692 static void phy_timer(unsigned long data)
694 struct phy_device *phydev = (struct phy_device *)data;
698 spin_lock(&phydev->lock);
700 if (phydev->adjust_state)
701 phydev->adjust_state(phydev->attached_dev);
703 switch(phydev->state) {
712 phydev->link_timeout = PHY_AN_TIMEOUT;
716 err = phy_read_status(phydev);
721 /* If the link is down, give up on
722 * negotiation for now */
724 phydev->state = PHY_NOLINK;
725 netif_carrier_off(phydev->attached_dev);
726 phydev->adjust_link(phydev->attached_dev);
730 /* Check if negotiation is done. Break
731 * if there's an error */
732 err = phy_aneg_done(phydev);
736 /* If AN is done, we're running */
738 phydev->state = PHY_RUNNING;
739 netif_carrier_on(phydev->attached_dev);
740 phydev->adjust_link(phydev->attached_dev);
742 } else if (0 == phydev->link_timeout--) {
746 /* If we have the magic_aneg bit,
748 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
751 /* The timer expired, and we still
752 * don't have a setting, so we try
753 * forcing it until we find one that
754 * works, starting from the fastest speed,
755 * and working our way down */
756 idx = phy_find_valid(0, phydev->supported);
758 phydev->speed = settings[idx].speed;
759 phydev->duplex = settings[idx].duplex;
761 phydev->autoneg = AUTONEG_DISABLE;
763 pr_info("Trying %d/%s\n", phydev->speed,
770 err = phy_read_status(phydev);
776 phydev->state = PHY_RUNNING;
777 netif_carrier_on(phydev->attached_dev);
778 phydev->adjust_link(phydev->attached_dev);
782 err = genphy_update_link(phydev);
788 phydev->state = PHY_RUNNING;
789 netif_carrier_on(phydev->attached_dev);
791 if (0 == phydev->link_timeout--) {
792 phy_force_reduction(phydev);
797 phydev->adjust_link(phydev->attached_dev);
800 /* Only register a CHANGE if we are
802 if (PHY_POLL == phydev->irq)
803 phydev->state = PHY_CHANGELINK;
806 err = phy_read_status(phydev);
812 phydev->state = PHY_RUNNING;
813 netif_carrier_on(phydev->attached_dev);
815 phydev->state = PHY_NOLINK;
816 netif_carrier_off(phydev->attached_dev);
819 phydev->adjust_link(phydev->attached_dev);
821 if (PHY_POLL != phydev->irq)
822 err = phy_config_interrupt(phydev,
823 PHY_INTERRUPT_ENABLED);
828 netif_carrier_off(phydev->attached_dev);
829 phydev->adjust_link(phydev->attached_dev);
834 err = phy_clear_interrupt(phydev);
839 err = phy_config_interrupt(phydev,
840 PHY_INTERRUPT_ENABLED);
845 if (AUTONEG_ENABLE == phydev->autoneg) {
846 err = phy_aneg_done(phydev);
850 /* err > 0 if AN is done.
851 * Otherwise, it's 0, and we're
852 * still waiting for AN */
854 phydev->state = PHY_RUNNING;
856 phydev->state = PHY_AN;
857 phydev->link_timeout = PHY_AN_TIMEOUT;
860 phydev->state = PHY_RUNNING;
864 spin_unlock(&phydev->lock);
867 err = phy_start_aneg(phydev);
872 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);