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.
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/kernel.h>
18 #include <linux/sched.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>
38 #include <asm/uaccess.h>
40 /* Convenience function to print out the current phy status
42 void phy_print_status(struct phy_device *phydev)
44 pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,
45 phydev->link ? "Up" : "Down");
47 printk(" - %d/%s", phydev->speed,
48 DUPLEX_FULL == phydev->duplex ?
53 EXPORT_SYMBOL(phy_print_status);
56 /* Convenience functions for reading/writing a given PHY
57 * register. They MUST NOT be called from interrupt context,
58 * because the bus read/write functions may wait for an interrupt
59 * to conclude the operation. */
60 int phy_read(struct phy_device *phydev, u16 regnum)
63 struct mii_bus *bus = phydev->bus;
65 spin_lock_bh(&bus->mdio_lock);
66 retval = bus->read(bus, phydev->addr, regnum);
67 spin_unlock_bh(&bus->mdio_lock);
71 EXPORT_SYMBOL(phy_read);
73 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
76 struct mii_bus *bus = phydev->bus;
78 spin_lock_bh(&bus->mdio_lock);
79 err = bus->write(bus, phydev->addr, regnum, val);
80 spin_unlock_bh(&bus->mdio_lock);
84 EXPORT_SYMBOL(phy_write);
87 int phy_clear_interrupt(struct phy_device *phydev)
91 if (phydev->drv->ack_interrupt)
92 err = phydev->drv->ack_interrupt(phydev);
98 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
102 phydev->interrupts = interrupts;
103 if (phydev->drv->config_intr)
104 err = phydev->drv->config_intr(phydev);
112 * description: Reads the status register and returns 0 either if
113 * auto-negotiation is incomplete, or if there was an error.
114 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
116 static inline int phy_aneg_done(struct phy_device *phydev)
120 retval = phy_read(phydev, MII_BMSR);
122 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
125 /* A structure for mapping a particular speed and duplex
126 * combination to a particular SUPPORTED and ADVERTISED value */
133 /* A mapping of all SUPPORTED settings to speed/duplex */
134 static const struct phy_setting settings[] = {
137 .duplex = DUPLEX_FULL,
138 .setting = SUPPORTED_10000baseT_Full,
142 .duplex = DUPLEX_FULL,
143 .setting = SUPPORTED_1000baseT_Full,
147 .duplex = DUPLEX_HALF,
148 .setting = SUPPORTED_1000baseT_Half,
152 .duplex = DUPLEX_FULL,
153 .setting = SUPPORTED_100baseT_Full,
157 .duplex = DUPLEX_HALF,
158 .setting = SUPPORTED_100baseT_Half,
162 .duplex = DUPLEX_FULL,
163 .setting = SUPPORTED_10baseT_Full,
167 .duplex = DUPLEX_HALF,
168 .setting = SUPPORTED_10baseT_Half,
172 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
176 * description: Searches the settings array for the setting which
177 * matches the desired speed and duplex, and returns the index
178 * of that setting. Returns the index of the last setting if
179 * none of the others match.
181 static inline int phy_find_setting(int speed, int duplex)
185 while (idx < ARRAY_SIZE(settings) &&
186 (settings[idx].speed != speed ||
187 settings[idx].duplex != duplex))
190 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
194 * idx: The first index in settings[] to search
195 * features: A mask of the valid settings
197 * description: Returns the index of the first valid setting less
198 * than or equal to the one pointed to by idx, as determined by
199 * the mask in features. Returns the index of the last setting
200 * if nothing else matches.
202 static inline int phy_find_valid(int idx, u32 features)
204 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
207 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
210 /* phy_sanitize_settings
212 * description: Make sure the PHY is set to supported speeds and
213 * duplexes. Drop down by one in this order: 1000/FULL,
214 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
216 void phy_sanitize_settings(struct phy_device *phydev)
218 u32 features = phydev->supported;
221 /* Sanitize settings based on PHY capabilities */
222 if ((features & SUPPORTED_Autoneg) == 0)
225 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
228 phydev->speed = settings[idx].speed;
229 phydev->duplex = settings[idx].duplex;
231 EXPORT_SYMBOL(phy_sanitize_settings);
234 * A generic ethtool sset function. Handles all the details
236 * A few notes about parameter checking:
237 * - We don't set port or transceiver, so we don't care what they
239 * - phy_start_aneg() will make sure forced settings are sane, and
240 * choose the next best ones from the ones selected, so we don't
241 * care if ethtool tries to give us bad values
244 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
246 if (cmd->phy_address != phydev->addr)
249 /* We make sure that we don't pass unsupported
250 * values in to the PHY */
251 cmd->advertising &= phydev->supported;
253 /* Verify the settings we care about. */
254 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
257 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
260 if (cmd->autoneg == AUTONEG_DISABLE
261 && ((cmd->speed != SPEED_1000
262 && cmd->speed != SPEED_100
263 && cmd->speed != SPEED_10)
264 || (cmd->duplex != DUPLEX_HALF
265 && cmd->duplex != DUPLEX_FULL)))
268 phydev->autoneg = cmd->autoneg;
270 phydev->speed = cmd->speed;
272 phydev->advertising = cmd->advertising;
274 if (AUTONEG_ENABLE == cmd->autoneg)
275 phydev->advertising |= ADVERTISED_Autoneg;
277 phydev->advertising &= ~ADVERTISED_Autoneg;
279 phydev->duplex = cmd->duplex;
281 /* Restart the PHY */
282 phy_start_aneg(phydev);
287 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
289 cmd->supported = phydev->supported;
291 cmd->advertising = phydev->advertising;
293 cmd->speed = phydev->speed;
294 cmd->duplex = phydev->duplex;
295 cmd->port = PORT_MII;
296 cmd->phy_address = phydev->addr;
297 cmd->transceiver = XCVR_EXTERNAL;
298 cmd->autoneg = phydev->autoneg;
304 /* Note that this function is currently incompatible with the
305 * PHYCONTROL layer. It changes registers without regard to
306 * current state. Use at own risk
308 int phy_mii_ioctl(struct phy_device *phydev,
309 struct mii_ioctl_data *mii_data, int cmd)
311 u16 val = mii_data->val_in;
315 mii_data->phy_id = phydev->addr;
318 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
322 if (!capable(CAP_NET_ADMIN))
325 if (mii_data->phy_id == phydev->addr) {
326 switch(mii_data->reg_num) {
328 if (val & (BMCR_RESET|BMCR_ANENABLE))
329 phydev->autoneg = AUTONEG_DISABLE;
331 phydev->autoneg = AUTONEG_ENABLE;
332 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
333 phydev->duplex = DUPLEX_FULL;
335 phydev->duplex = DUPLEX_HALF;
338 phydev->advertising = val;
346 phy_write(phydev, mii_data->reg_num, val);
348 if (mii_data->reg_num == MII_BMCR
350 && phydev->drv->config_init)
351 phydev->drv->config_init(phydev);
360 * description: Sanitizes the settings (if we're not
361 * autonegotiating them), and then calls the driver's
362 * config_aneg function. If the PHYCONTROL Layer is operating,
363 * we change the state to reflect the beginning of
364 * Auto-negotiation or forcing.
366 int phy_start_aneg(struct phy_device *phydev)
370 spin_lock(&phydev->lock);
372 if (AUTONEG_DISABLE == phydev->autoneg)
373 phy_sanitize_settings(phydev);
375 err = phydev->drv->config_aneg(phydev);
380 if (phydev->state != PHY_HALTED) {
381 if (AUTONEG_ENABLE == phydev->autoneg) {
382 phydev->state = PHY_AN;
383 phydev->link_timeout = PHY_AN_TIMEOUT;
385 phydev->state = PHY_FORCING;
386 phydev->link_timeout = PHY_FORCE_TIMEOUT;
391 spin_unlock(&phydev->lock);
394 EXPORT_SYMBOL(phy_start_aneg);
397 static void phy_change(void *data);
398 static void phy_timer(unsigned long data);
400 /* phy_start_machine:
402 * description: The PHY infrastructure can run a state machine
403 * which tracks whether the PHY is starting up, negotiating,
404 * etc. This function starts the timer which tracks the state
405 * of the PHY. If you want to be notified when the state
406 * changes, pass in the callback, otherwise, pass NULL. If you
407 * want to maintain your own state machine, do not call this
409 void phy_start_machine(struct phy_device *phydev,
410 void (*handler)(struct net_device *))
412 phydev->adjust_state = handler;
414 init_timer(&phydev->phy_timer);
415 phydev->phy_timer.function = &phy_timer;
416 phydev->phy_timer.data = (unsigned long) phydev;
417 mod_timer(&phydev->phy_timer, jiffies + HZ);
422 * description: Stops the state machine timer, sets the state to UP
423 * (unless it wasn't up yet). This function must be called BEFORE
426 void phy_stop_machine(struct phy_device *phydev)
428 del_timer_sync(&phydev->phy_timer);
430 spin_lock(&phydev->lock);
431 if (phydev->state > PHY_UP)
432 phydev->state = PHY_UP;
433 spin_unlock(&phydev->lock);
435 phydev->adjust_state = NULL;
438 /* phy_force_reduction
440 * description: Reduces the speed/duplex settings by
441 * one notch. The order is so:
442 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
443 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
445 static void phy_force_reduction(struct phy_device *phydev)
449 idx = phy_find_setting(phydev->speed, phydev->duplex);
453 idx = phy_find_valid(idx, phydev->supported);
455 phydev->speed = settings[idx].speed;
456 phydev->duplex = settings[idx].duplex;
458 pr_info("Trying %d/%s\n", phydev->speed,
459 DUPLEX_FULL == phydev->duplex ?
466 * Moves the PHY to the HALTED state in response to a read
467 * or write error, and tells the controller the link is down.
468 * Must not be called from interrupt context, or while the
469 * phydev->lock is held.
471 void phy_error(struct phy_device *phydev)
473 spin_lock(&phydev->lock);
474 phydev->state = PHY_HALTED;
475 spin_unlock(&phydev->lock);
480 * description: When a PHY interrupt occurs, the handler disables
481 * interrupts, and schedules a work task to clear the interrupt.
483 static irqreturn_t phy_interrupt(int irq, void *phy_dat, struct pt_regs *regs)
485 struct phy_device *phydev = phy_dat;
487 /* The MDIO bus is not allowed to be written in interrupt
488 * context, so we need to disable the irq here. A work
489 * queue will write the PHY to disable and clear the
490 * interrupt, and then reenable the irq line. */
491 disable_irq_nosync(irq);
493 schedule_work(&phydev->phy_queue);
498 /* Enable the interrupts from the PHY side */
499 int phy_enable_interrupts(struct phy_device *phydev)
503 err = phy_clear_interrupt(phydev);
508 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
512 EXPORT_SYMBOL(phy_enable_interrupts);
514 /* Disable the PHY interrupts from the PHY side */
515 int phy_disable_interrupts(struct phy_device *phydev)
519 /* Disable PHY interrupts */
520 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
525 /* Clear the interrupt */
526 err = phy_clear_interrupt(phydev);
538 EXPORT_SYMBOL(phy_disable_interrupts);
540 /* phy_start_interrupts
542 * description: Request the interrupt for the given PHY. If
543 * this fails, then we set irq to PHY_POLL.
544 * Otherwise, we enable the interrupts in the PHY.
545 * Returns 0 on success.
546 * This should only be called with a valid IRQ number.
548 int phy_start_interrupts(struct phy_device *phydev)
552 INIT_WORK(&phydev->phy_queue, phy_change, phydev);
554 if (request_irq(phydev->irq, phy_interrupt,
558 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
561 phydev->irq = PHY_POLL;
565 err = phy_enable_interrupts(phydev);
569 EXPORT_SYMBOL(phy_start_interrupts);
571 int phy_stop_interrupts(struct phy_device *phydev)
575 err = phy_disable_interrupts(phydev);
580 free_irq(phydev->irq, phydev);
584 EXPORT_SYMBOL(phy_stop_interrupts);
587 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
588 static void phy_change(void *data)
591 struct phy_device *phydev = data;
593 err = phy_disable_interrupts(phydev);
598 spin_lock(&phydev->lock);
599 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
600 phydev->state = PHY_CHANGELINK;
601 spin_unlock(&phydev->lock);
603 enable_irq(phydev->irq);
605 /* Reenable interrupts */
606 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
614 disable_irq(phydev->irq);
619 /* Bring down the PHY link, and stop checking the status. */
620 void phy_stop(struct phy_device *phydev)
622 spin_lock(&phydev->lock);
624 if (PHY_HALTED == phydev->state)
627 if (phydev->irq != PHY_POLL) {
628 /* Clear any pending interrupts */
629 phy_clear_interrupt(phydev);
631 /* Disable PHY Interrupts */
632 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
635 phydev->state = PHY_HALTED;
638 spin_unlock(&phydev->lock);
644 * description: Indicates the attached device's readiness to
645 * handle PHY-related work. Used during startup to start the
646 * PHY, and after a call to phy_stop() to resume operation.
647 * Also used to indicate the MDIO bus has cleared an error
650 void phy_start(struct phy_device *phydev)
652 spin_lock(&phydev->lock);
654 switch (phydev->state) {
656 phydev->state = PHY_PENDING;
659 phydev->state = PHY_UP;
662 phydev->state = PHY_RESUMING;
666 spin_unlock(&phydev->lock);
668 EXPORT_SYMBOL(phy_stop);
669 EXPORT_SYMBOL(phy_start);
671 /* PHY timer which handles the state machine */
672 static void phy_timer(unsigned long data)
674 struct phy_device *phydev = (struct phy_device *)data;
678 spin_lock(&phydev->lock);
680 if (phydev->adjust_state)
681 phydev->adjust_state(phydev->attached_dev);
683 switch(phydev->state) {
692 phydev->link_timeout = PHY_AN_TIMEOUT;
696 /* Check if negotiation is done. Break
697 * if there's an error */
698 err = phy_aneg_done(phydev);
702 /* If auto-negotiation is done, we change to
703 * either RUNNING, or NOLINK */
705 err = phy_read_status(phydev);
711 phydev->state = PHY_RUNNING;
712 netif_carrier_on(phydev->attached_dev);
714 phydev->state = PHY_NOLINK;
715 netif_carrier_off(phydev->attached_dev);
718 phydev->adjust_link(phydev->attached_dev);
720 } else if (0 == phydev->link_timeout--) {
721 /* The counter expired, so either we
722 * switch to forced mode, or the
723 * magic_aneg bit exists, and we try aneg
725 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
728 /* We'll start from the
729 * fastest speed, and work
731 idx = phy_find_valid(0,
734 phydev->speed = settings[idx].speed;
735 phydev->duplex = settings[idx].duplex;
737 phydev->autoneg = AUTONEG_DISABLE;
738 phydev->state = PHY_FORCING;
739 phydev->link_timeout =
742 pr_info("Trying %d/%s\n",
753 err = phy_read_status(phydev);
759 phydev->state = PHY_RUNNING;
760 netif_carrier_on(phydev->attached_dev);
761 phydev->adjust_link(phydev->attached_dev);
765 err = phy_read_status(phydev);
771 phydev->state = PHY_RUNNING;
772 netif_carrier_on(phydev->attached_dev);
774 if (0 == phydev->link_timeout--) {
775 phy_force_reduction(phydev);
780 phydev->adjust_link(phydev->attached_dev);
783 /* Only register a CHANGE if we are
785 if (PHY_POLL == phydev->irq)
786 phydev->state = PHY_CHANGELINK;
789 err = phy_read_status(phydev);
795 phydev->state = PHY_RUNNING;
796 netif_carrier_on(phydev->attached_dev);
798 phydev->state = PHY_NOLINK;
799 netif_carrier_off(phydev->attached_dev);
802 phydev->adjust_link(phydev->attached_dev);
804 if (PHY_POLL != phydev->irq)
805 err = phy_config_interrupt(phydev,
806 PHY_INTERRUPT_ENABLED);
811 netif_carrier_off(phydev->attached_dev);
812 phydev->adjust_link(phydev->attached_dev);
817 err = phy_clear_interrupt(phydev);
822 err = phy_config_interrupt(phydev,
823 PHY_INTERRUPT_ENABLED);
828 if (AUTONEG_ENABLE == phydev->autoneg) {
829 err = phy_aneg_done(phydev);
833 /* err > 0 if AN is done.
834 * Otherwise, it's 0, and we're
835 * still waiting for AN */
837 phydev->state = PHY_RUNNING;
839 phydev->state = PHY_AN;
840 phydev->link_timeout = PHY_AN_TIMEOUT;
843 phydev->state = PHY_RUNNING;
847 spin_unlock(&phydev->lock);
850 err = phy_start_aneg(phydev);
855 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);