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/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.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("%s: Link is %s", phydev->dev.bus_id,
46 phydev->link ? "Up" : "Down");
48 printk(" - %d/%s", phydev->speed,
49 DUPLEX_FULL == phydev->duplex ?
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)
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);
72 EXPORT_SYMBOL(phy_read);
74 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
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);
85 EXPORT_SYMBOL(phy_write);
88 int phy_clear_interrupt(struct phy_device *phydev)
92 if (phydev->drv->ack_interrupt)
93 err = phydev->drv->ack_interrupt(phydev);
99 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
103 phydev->interrupts = interrupts;
104 if (phydev->drv->config_intr)
105 err = phydev->drv->config_intr(phydev);
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)
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 */
134 /* A mapping of all SUPPORTED settings to speed/duplex */
135 static struct phy_setting settings[] = {
138 .duplex = DUPLEX_FULL,
139 .setting = SUPPORTED_10000baseT_Full,
143 .duplex = DUPLEX_FULL,
144 .setting = SUPPORTED_1000baseT_Full,
148 .duplex = DUPLEX_HALF,
149 .setting = SUPPORTED_1000baseT_Half,
153 .duplex = DUPLEX_FULL,
154 .setting = SUPPORTED_100baseT_Full,
158 .duplex = DUPLEX_HALF,
159 .setting = SUPPORTED_100baseT_Half,
163 .duplex = DUPLEX_FULL,
164 .setting = SUPPORTED_10baseT_Full,
168 .duplex = DUPLEX_HALF,
169 .setting = SUPPORTED_10baseT_Half,
173 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_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)
186 while (idx < ARRAY_SIZE(settings) &&
187 (settings[idx].speed != speed ||
188 settings[idx].duplex != duplex))
191 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
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))
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;
222 /* Sanitize settings based on PHY capabilities */
223 if ((features & SUPPORTED_Autoneg) == 0)
226 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
229 phydev->speed = settings[idx].speed;
230 phydev->duplex = settings[idx].duplex;
232 EXPORT_SYMBOL(phy_sanitize_settings);
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
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)
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)
258 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
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)))
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;
278 phydev->advertising &= ~ADVERTISED_Autoneg;
280 phydev->duplex = cmd->duplex;
282 /* Restart the PHY */
283 phy_start_aneg(phydev);
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;
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;
316 mii_data->phy_id = phydev->addr;
319 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
323 if (!capable(CAP_NET_ADMIN))
326 if (mii_data->phy_id == phydev->addr) {
327 switch(mii_data->reg_num) {
329 if (val & (BMCR_RESET|BMCR_ANENABLE))
330 phydev->autoneg = AUTONEG_DISABLE;
332 phydev->autoneg = AUTONEG_ENABLE;
333 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
334 phydev->duplex = DUPLEX_FULL;
336 phydev->duplex = DUPLEX_HALF;
339 phydev->advertising = val;
347 phy_write(phydev, mii_data->reg_num, val);
349 if (mii_data->reg_num == MII_BMCR
351 && phydev->drv->config_init)
352 phydev->drv->config_init(phydev);
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)
371 spin_lock(&phydev->lock);
373 if (AUTONEG_DISABLE == phydev->autoneg)
374 phy_sanitize_settings(phydev);
376 err = phydev->drv->config_aneg(phydev);
381 if (phydev->state != PHY_HALTED) {
382 if (AUTONEG_ENABLE == phydev->autoneg) {
383 phydev->state = PHY_AN;
384 phydev->link_timeout = PHY_AN_TIMEOUT;
386 phydev->state = PHY_FORCING;
387 phydev->link_timeout = PHY_FORCE_TIMEOUT;
392 spin_unlock(&phydev->lock);
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
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);
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
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)
454 idx = phy_find_setting(phydev->speed, phydev->duplex);
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 ?
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);
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);
503 /* Enable the interrupts from the PHY side */
504 int phy_enable_interrupts(struct phy_device *phydev)
508 err = phy_clear_interrupt(phydev);
513 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
517 EXPORT_SYMBOL(phy_enable_interrupts);
519 /* Disable the PHY interrupts from the PHY side */
520 int phy_disable_interrupts(struct phy_device *phydev)
524 /* Disable PHY interrupts */
525 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
530 /* Clear the interrupt */
531 err = phy_clear_interrupt(phydev);
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)
557 INIT_WORK(&phydev->phy_queue, phy_change, phydev);
559 if (request_irq(phydev->irq, phy_interrupt,
563 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
566 phydev->irq = PHY_POLL;
570 err = phy_enable_interrupts(phydev);
574 EXPORT_SYMBOL(phy_start_interrupts);
576 int phy_stop_interrupts(struct phy_device *phydev)
580 err = phy_disable_interrupts(phydev);
585 free_irq(phydev->irq, phydev);
589 EXPORT_SYMBOL(phy_stop_interrupts);
592 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
593 static void phy_change(void *data)
596 struct phy_device *phydev = data;
598 err = phy_disable_interrupts(phydev);
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);
619 disable_irq(phydev->irq);
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)
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;
643 spin_unlock(&phydev->lock);
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
655 void phy_start(struct phy_device *phydev)
657 spin_lock(&phydev->lock);
659 switch (phydev->state) {
661 phydev->state = PHY_PENDING;
664 phydev->state = PHY_UP;
667 phydev->state = PHY_RESUMING;
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;
683 spin_lock(&phydev->lock);
685 if (phydev->adjust_state)
686 phydev->adjust_state(phydev->attached_dev);
688 switch(phydev->state) {
697 phydev->link_timeout = PHY_AN_TIMEOUT;
701 /* Check if negotiation is done. Break
702 * if there's an error */
703 err = phy_aneg_done(phydev);
707 /* If auto-negotiation is done, we change to
708 * either RUNNING, or NOLINK */
710 err = phy_read_status(phydev);
716 phydev->state = PHY_RUNNING;
717 netif_carrier_on(phydev->attached_dev);
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
730 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
733 /* We'll start from the
734 * fastest speed, and work
736 idx = phy_find_valid(0,
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 =
747 pr_info("Trying %d/%s\n",
758 err = phy_read_status(phydev);
764 phydev->state = PHY_RUNNING;
765 netif_carrier_on(phydev->attached_dev);
766 phydev->adjust_link(phydev->attached_dev);
770 err = phy_read_status(phydev);
776 phydev->state = PHY_RUNNING;
777 netif_carrier_on(phydev->attached_dev);
779 if (0 == phydev->link_timeout--) {
780 phy_force_reduction(phydev);
785 phydev->adjust_link(phydev->attached_dev);
788 /* Only register a CHANGE if we are
790 if (PHY_POLL == phydev->irq)
791 phydev->state = PHY_CHANGELINK;
794 err = phy_read_status(phydev);
800 phydev->state = PHY_RUNNING;
801 netif_carrier_on(phydev->attached_dev);
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);
816 netif_carrier_off(phydev->attached_dev);
817 phydev->adjust_link(phydev->attached_dev);
822 err = phy_clear_interrupt(phydev);
827 err = phy_config_interrupt(phydev,
828 PHY_INTERRUPT_ENABLED);
833 if (AUTONEG_ENABLE == phydev->autoneg) {
834 err = phy_aneg_done(phydev);
838 /* err > 0 if AN is done.
839 * Otherwise, it's 0, and we're
840 * still waiting for AN */
842 phydev->state = PHY_RUNNING;
844 phydev->state = PHY_AN;
845 phydev->link_timeout = PHY_AN_TIMEOUT;
848 phydev->state = PHY_RUNNING;
852 spin_unlock(&phydev->lock);
855 err = phy_start_aneg(phydev);
860 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);