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/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>
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>
40 #include <asm/uaccess.h>
42 static void phy_change(void *data);
43 static void phy_timer(unsigned long data);
45 /* Convenience function to print out the current phy status
47 void phy_print_status(struct phy_device *phydev)
49 pr_info("%s: Link is %s", phydev->dev.bus_id,
50 phydev->link ? "Up" : "Down");
52 printk(" - %d/%s", phydev->speed,
53 DUPLEX_FULL == phydev->duplex ?
58 EXPORT_SYMBOL(phy_print_status);
61 /* Convenience functions for reading/writing a given PHY
62 * register. They MUST NOT be called from interrupt context,
63 * because the bus read/write functions may wait for an interrupt
64 * to conclude the operation. */
65 int phy_read(struct phy_device *phydev, u16 regnum)
68 struct mii_bus *bus = phydev->bus;
70 spin_lock_bh(&bus->mdio_lock);
71 retval = bus->read(bus, phydev->addr, regnum);
72 spin_unlock_bh(&bus->mdio_lock);
76 EXPORT_SYMBOL(phy_read);
78 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
81 struct mii_bus *bus = phydev->bus;
83 spin_lock_bh(&bus->mdio_lock);
84 err = bus->write(bus, phydev->addr, regnum, val);
85 spin_unlock_bh(&bus->mdio_lock);
89 EXPORT_SYMBOL(phy_write);
92 int phy_clear_interrupt(struct phy_device *phydev)
96 if (phydev->drv->ack_interrupt)
97 err = phydev->drv->ack_interrupt(phydev);
103 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
107 phydev->interrupts = interrupts;
108 if (phydev->drv->config_intr)
109 err = phydev->drv->config_intr(phydev);
117 * description: Reads the status register and returns 0 either if
118 * auto-negotiation is incomplete, or if there was an error.
119 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
121 static inline int phy_aneg_done(struct phy_device *phydev)
125 retval = phy_read(phydev, MII_BMSR);
127 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
132 * description: Calls the PHY driver's config_aneg, and then
133 * sets the PHY state to PHY_AN if auto-negotiation is enabled,
134 * and to PHY_FORCING if auto-negotiation is disabled. Unless
135 * the PHY is currently HALTED.
137 int phy_start_aneg(struct phy_device *phydev)
141 spin_lock(&phydev->lock);
143 if (AUTONEG_DISABLE == phydev->autoneg)
144 phy_sanitize_settings(phydev);
146 err = phydev->drv->config_aneg(phydev);
151 if (phydev->state != PHY_HALTED) {
152 if (AUTONEG_ENABLE == phydev->autoneg) {
153 phydev->state = PHY_AN;
154 phydev->link_timeout = PHY_AN_TIMEOUT;
156 phydev->state = PHY_FORCING;
157 phydev->link_timeout = PHY_FORCE_TIMEOUT;
162 spin_unlock(&phydev->lock);
165 EXPORT_SYMBOL(phy_start_aneg);
168 /* A structure for mapping a particular speed and duplex
169 * combination to a particular SUPPORTED and ADVERTISED value */
176 /* A mapping of all SUPPORTED settings to speed/duplex */
177 static struct phy_setting settings[] = {
180 .duplex = DUPLEX_FULL,
181 .setting = SUPPORTED_10000baseT_Full,
185 .duplex = DUPLEX_FULL,
186 .setting = SUPPORTED_1000baseT_Full,
190 .duplex = DUPLEX_HALF,
191 .setting = SUPPORTED_1000baseT_Half,
195 .duplex = DUPLEX_FULL,
196 .setting = SUPPORTED_100baseT_Full,
200 .duplex = DUPLEX_HALF,
201 .setting = SUPPORTED_100baseT_Half,
205 .duplex = DUPLEX_FULL,
206 .setting = SUPPORTED_10baseT_Full,
210 .duplex = DUPLEX_HALF,
211 .setting = SUPPORTED_10baseT_Half,
215 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
219 * description: Searches the settings array for the setting which
220 * matches the desired speed and duplex, and returns the index
221 * of that setting. Returns the index of the last setting if
222 * none of the others match.
224 static inline int phy_find_setting(int speed, int duplex)
228 while (idx < ARRAY_SIZE(settings) &&
229 (settings[idx].speed != speed ||
230 settings[idx].duplex != duplex))
233 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
237 * idx: The first index in settings[] to search
238 * features: A mask of the valid settings
240 * description: Returns the index of the first valid setting less
241 * than or equal to the one pointed to by idx, as determined by
242 * the mask in features. Returns the index of the last setting
243 * if nothing else matches.
245 static inline int phy_find_valid(int idx, u32 features)
247 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
250 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
253 /* phy_sanitize_settings
255 * description: Make sure the PHY is set to supported speeds and
256 * duplexes. Drop down by one in this order: 1000/FULL,
257 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
259 void phy_sanitize_settings(struct phy_device *phydev)
261 u32 features = phydev->supported;
264 /* Sanitize settings based on PHY capabilities */
265 if ((features & SUPPORTED_Autoneg) == 0)
268 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
271 phydev->speed = settings[idx].speed;
272 phydev->duplex = settings[idx].duplex;
274 EXPORT_SYMBOL(phy_sanitize_settings);
276 /* phy_force_reduction
278 * description: Reduces the speed/duplex settings by
279 * one notch. The order is so:
280 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
281 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
283 static void phy_force_reduction(struct phy_device *phydev)
287 idx = phy_find_setting(phydev->speed, phydev->duplex);
291 idx = phy_find_valid(idx, phydev->supported);
293 phydev->speed = settings[idx].speed;
294 phydev->duplex = settings[idx].duplex;
296 pr_info("Trying %d/%s\n", phydev->speed,
297 DUPLEX_FULL == phydev->duplex ?
302 * A generic ethtool sset function. Handles all the details
304 * A few notes about parameter checking:
305 * - We don't set port or transceiver, so we don't care what they
307 * - phy_start_aneg() will make sure forced settings are sane, and
308 * choose the next best ones from the ones selected, so we don't
309 * care if ethtool tries to give us bad values
311 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
313 if (cmd->phy_address != phydev->addr)
316 /* We make sure that we don't pass unsupported
317 * values in to the PHY */
318 cmd->advertising &= phydev->supported;
320 /* Verify the settings we care about. */
321 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
324 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
327 if (cmd->autoneg == AUTONEG_DISABLE
328 && ((cmd->speed != SPEED_1000
329 && cmd->speed != SPEED_100
330 && cmd->speed != SPEED_10)
331 || (cmd->duplex != DUPLEX_HALF
332 && cmd->duplex != DUPLEX_FULL)))
335 phydev->autoneg = cmd->autoneg;
337 phydev->speed = cmd->speed;
339 phydev->advertising = cmd->advertising;
341 if (AUTONEG_ENABLE == cmd->autoneg)
342 phydev->advertising |= ADVERTISED_Autoneg;
344 phydev->advertising &= ~ADVERTISED_Autoneg;
346 phydev->duplex = cmd->duplex;
348 /* Restart the PHY */
349 phy_start_aneg(phydev);
354 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
356 cmd->supported = phydev->supported;
358 cmd->advertising = phydev->advertising;
360 cmd->speed = phydev->speed;
361 cmd->duplex = phydev->duplex;
362 cmd->port = PORT_MII;
363 cmd->phy_address = phydev->addr;
364 cmd->transceiver = XCVR_EXTERNAL;
365 cmd->autoneg = phydev->autoneg;
371 /* Note that this function is currently incompatible with the
372 * PHYCONTROL layer. It changes registers without regard to
373 * current state. Use at own risk
375 int phy_mii_ioctl(struct phy_device *phydev,
376 struct mii_ioctl_data *mii_data, int cmd)
378 u16 val = mii_data->val_in;
382 mii_data->phy_id = phydev->addr;
385 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
389 if (!capable(CAP_NET_ADMIN))
392 if (mii_data->phy_id == phydev->addr) {
393 switch(mii_data->reg_num) {
395 if (val & (BMCR_RESET|BMCR_ANENABLE))
396 phydev->autoneg = AUTONEG_DISABLE;
398 phydev->autoneg = AUTONEG_ENABLE;
399 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
400 phydev->duplex = DUPLEX_FULL;
402 phydev->duplex = DUPLEX_HALF;
405 phydev->advertising = val;
413 phy_write(phydev, mii_data->reg_num, val);
415 if (mii_data->reg_num == MII_BMCR
417 && phydev->drv->config_init)
418 phydev->drv->config_init(phydev);
425 /* phy_start_machine:
427 * description: The PHY infrastructure can run a state machine
428 * which tracks whether the PHY is starting up, negotiating,
429 * etc. This function starts the timer which tracks the state
430 * of the PHY. If you want to be notified when the state
431 * changes, pass in the callback, otherwise, pass NULL. If you
432 * want to maintain your own state machine, do not call this
434 void phy_start_machine(struct phy_device *phydev,
435 void (*handler)(struct net_device *))
437 phydev->adjust_state = handler;
439 init_timer(&phydev->phy_timer);
440 phydev->phy_timer.function = &phy_timer;
441 phydev->phy_timer.data = (unsigned long) phydev;
442 mod_timer(&phydev->phy_timer, jiffies + HZ);
447 * description: Stops the state machine timer, sets the state to
448 * UP (unless it wasn't up yet), and then frees the interrupt,
449 * if it is in use. This function must be called BEFORE
452 void phy_stop_machine(struct phy_device *phydev)
454 del_timer_sync(&phydev->phy_timer);
456 spin_lock(&phydev->lock);
457 if (phydev->state > PHY_UP)
458 phydev->state = PHY_UP;
459 spin_unlock(&phydev->lock);
461 if (phydev->irq != PHY_POLL)
462 phy_stop_interrupts(phydev);
464 phydev->adjust_state = NULL;
467 #ifdef CONFIG_PHYCONTROL
470 * Moves the PHY to the HALTED state in response to a read
471 * or write error, and tells the controller the link is down.
472 * Must not be called from interrupt context, or while the
473 * phydev->lock is held.
475 void phy_error(struct phy_device *phydev)
477 spin_lock(&phydev->lock);
478 phydev->state = PHY_HALTED;
479 spin_unlock(&phydev->lock);
484 * description: When a PHY interrupt occurs, the handler disables
485 * interrupts, and schedules a work task to clear the interrupt.
487 static irqreturn_t phy_interrupt(int irq, void *phy_dat, struct pt_regs *regs)
489 struct phy_device *phydev = phy_dat;
491 /* The MDIO bus is not allowed to be written in interrupt
492 * context, so we need to disable the irq here. A work
493 * queue will write the PHY to disable and clear the
494 * interrupt, and then reenable the irq line. */
495 disable_irq_nosync(irq);
497 schedule_work(&phydev->phy_queue);
502 /* Enable the interrupts from the PHY side */
503 int phy_enable_interrupts(struct phy_device *phydev)
507 err = phy_clear_interrupt(phydev);
512 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
516 EXPORT_SYMBOL(phy_enable_interrupts);
518 /* Disable the PHY interrupts from the PHY side */
519 int phy_disable_interrupts(struct phy_device *phydev)
523 /* Disable PHY interrupts */
524 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
529 /* Clear the interrupt */
530 err = phy_clear_interrupt(phydev);
542 EXPORT_SYMBOL(phy_disable_interrupts);
544 /* phy_start_interrupts
546 * description: Request the interrupt for the given PHY. If
547 * this fails, then we set irq to PHY_POLL.
548 * Otherwise, we enable the interrupts in the PHY.
549 * Returns 0 on success.
550 * This should only be called with a valid IRQ number.
552 int phy_start_interrupts(struct phy_device *phydev)
556 INIT_WORK(&phydev->phy_queue, phy_change, phydev);
558 if (request_irq(phydev->irq, phy_interrupt,
562 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
565 phydev->irq = PHY_POLL;
569 err = phy_enable_interrupts(phydev);
573 EXPORT_SYMBOL(phy_start_interrupts);
575 int phy_stop_interrupts(struct phy_device *phydev)
579 err = phy_disable_interrupts(phydev);
584 free_irq(phydev->irq, phydev);
588 EXPORT_SYMBOL(phy_stop_interrupts);
591 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
592 static void phy_change(void *data)
595 struct phy_device *phydev = data;
597 err = phy_disable_interrupts(phydev);
602 spin_lock(&phydev->lock);
603 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
604 phydev->state = PHY_CHANGELINK;
605 spin_unlock(&phydev->lock);
607 enable_irq(phydev->irq);
609 /* Reenable interrupts */
610 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
618 disable_irq(phydev->irq);
623 /* Bring down the PHY link, and stop checking the status. */
624 void phy_stop(struct phy_device *phydev)
626 spin_lock(&phydev->lock);
628 if (PHY_HALTED == phydev->state)
631 if (phydev->irq != PHY_POLL) {
632 /* Clear any pending interrupts */
633 phy_clear_interrupt(phydev);
635 /* Disable PHY Interrupts */
636 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
639 phydev->state = PHY_HALTED;
642 spin_unlock(&phydev->lock);
648 * description: Indicates the attached device's readiness to
649 * handle PHY-related work. Used during startup to start the
650 * PHY, and after a call to phy_stop() to resume operation.
651 * Also used to indicate the MDIO bus has cleared an error
654 void phy_start(struct phy_device *phydev)
656 spin_lock(&phydev->lock);
658 switch (phydev->state) {
660 phydev->state = PHY_PENDING;
663 phydev->state = PHY_UP;
666 phydev->state = PHY_RESUMING;
670 spin_unlock(&phydev->lock);
672 EXPORT_SYMBOL(phy_stop);
673 EXPORT_SYMBOL(phy_start);
675 /* PHY timer which handles the state machine */
676 static void phy_timer(unsigned long data)
678 struct phy_device *phydev = (struct phy_device *)data;
682 spin_lock(&phydev->lock);
684 if (phydev->adjust_state)
685 phydev->adjust_state(phydev->attached_dev);
687 switch(phydev->state) {
696 phydev->link_timeout = PHY_AN_TIMEOUT;
700 /* Check if negotiation is done. Break
701 * if there's an error */
702 err = phy_aneg_done(phydev);
706 /* If auto-negotiation is done, we change to
707 * either RUNNING, or NOLINK */
709 err = phy_read_status(phydev);
715 phydev->state = PHY_RUNNING;
716 netif_carrier_on(phydev->attached_dev);
718 phydev->state = PHY_NOLINK;
719 netif_carrier_off(phydev->attached_dev);
722 phydev->adjust_link(phydev->attached_dev);
724 } else if (0 == phydev->link_timeout--) {
725 /* The counter expired, so either we
726 * switch to forced mode, or the
727 * magic_aneg bit exists, and we try aneg
729 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
732 /* We'll start from the
733 * fastest speed, and work
735 idx = phy_find_valid(0,
738 phydev->speed = settings[idx].speed;
739 phydev->duplex = settings[idx].duplex;
741 phydev->autoneg = AUTONEG_DISABLE;
742 phydev->state = PHY_FORCING;
743 phydev->link_timeout =
746 pr_info("Trying %d/%s\n",
757 err = phy_read_status(phydev);
763 phydev->state = PHY_RUNNING;
764 netif_carrier_on(phydev->attached_dev);
765 phydev->adjust_link(phydev->attached_dev);
769 err = phy_read_status(phydev);
775 phydev->state = PHY_RUNNING;
776 netif_carrier_on(phydev->attached_dev);
778 if (0 == phydev->link_timeout--) {
779 phy_force_reduction(phydev);
784 phydev->adjust_link(phydev->attached_dev);
787 /* Only register a CHANGE if we are
789 if (PHY_POLL == phydev->irq)
790 phydev->state = PHY_CHANGELINK;
793 err = phy_read_status(phydev);
799 phydev->state = PHY_RUNNING;
800 netif_carrier_on(phydev->attached_dev);
802 phydev->state = PHY_NOLINK;
803 netif_carrier_off(phydev->attached_dev);
806 phydev->adjust_link(phydev->attached_dev);
808 if (PHY_POLL != phydev->irq)
809 err = phy_config_interrupt(phydev,
810 PHY_INTERRUPT_ENABLED);
815 netif_carrier_off(phydev->attached_dev);
816 phydev->adjust_link(phydev->attached_dev);
821 err = phy_clear_interrupt(phydev);
826 err = phy_config_interrupt(phydev,
827 PHY_INTERRUPT_ENABLED);
832 if (AUTONEG_ENABLE == phydev->autoneg) {
833 err = phy_aneg_done(phydev);
837 /* err > 0 if AN is done.
838 * Otherwise, it's 0, and we're
839 * still waiting for AN */
841 phydev->state = PHY_RUNNING;
843 phydev->state = PHY_AN;
844 phydev->link_timeout = PHY_AN_TIMEOUT;
847 phydev->state = PHY_RUNNING;
851 spin_unlock(&phydev->lock);
854 err = phy_start_aneg(phydev);
859 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
862 #endif /* CONFIG_PHYCONTROL */