[PATCH] USB: Rename hcd->hub_suspend to hcd->bus_suspend
[linux/fpc-iii.git] / drivers / net / phy / phy.c
blob9209da9dde0da9738d662e7a8167ae2136f2d831
1 /*
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
7 * Author: Andy Fleming
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>
31 #include <linux/mm.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>
38 #include <asm/io.h>
39 #include <asm/irq.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("%s: Link is %s", phydev->dev.bus_id,
47 phydev->link ? "Up" : "Down");
48 if (phydev->link)
49 printk(" - %d/%s", phydev->speed,
50 DUPLEX_FULL == phydev->duplex ?
51 "Full" : "Half");
53 printk("\n");
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)
64 int retval;
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);
71 return retval;
73 EXPORT_SYMBOL(phy_read);
75 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
77 int err;
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);
84 return err;
86 EXPORT_SYMBOL(phy_write);
89 int phy_clear_interrupt(struct phy_device *phydev)
91 int err = 0;
93 if (phydev->drv->ack_interrupt)
94 err = phydev->drv->ack_interrupt(phydev);
96 return err;
100 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
102 int err = 0;
104 phydev->interrupts = interrupts;
105 if (phydev->drv->config_intr)
106 err = phydev->drv->config_intr(phydev);
108 return err;
112 /* phy_aneg_done
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)
120 int retval;
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 */
129 struct phy_setting {
130 int speed;
131 int duplex;
132 u32 setting;
135 /* A mapping of all SUPPORTED settings to speed/duplex */
136 static struct phy_setting settings[] = {
138 .speed = 10000,
139 .duplex = DUPLEX_FULL,
140 .setting = SUPPORTED_10000baseT_Full,
143 .speed = SPEED_1000,
144 .duplex = DUPLEX_FULL,
145 .setting = SUPPORTED_1000baseT_Full,
148 .speed = SPEED_1000,
149 .duplex = DUPLEX_HALF,
150 .setting = SUPPORTED_1000baseT_Half,
153 .speed = SPEED_100,
154 .duplex = DUPLEX_FULL,
155 .setting = SUPPORTED_100baseT_Full,
158 .speed = SPEED_100,
159 .duplex = DUPLEX_HALF,
160 .setting = SUPPORTED_100baseT_Half,
163 .speed = SPEED_10,
164 .duplex = DUPLEX_FULL,
165 .setting = SUPPORTED_10baseT_Full,
168 .speed = SPEED_10,
169 .duplex = DUPLEX_HALF,
170 .setting = SUPPORTED_10baseT_Half,
174 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
176 /* phy_find_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)
185 int idx = 0;
187 while (idx < ARRAY_SIZE(settings) &&
188 (settings[idx].speed != speed ||
189 settings[idx].duplex != duplex))
190 idx++;
192 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
195 /* phy_find_valid
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))
207 idx++;
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;
221 int idx;
223 /* Sanitize settings based on PHY capabilities */
224 if ((features & SUPPORTED_Autoneg) == 0)
225 phydev->autoneg = 0;
227 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
228 features);
230 phydev->speed = settings[idx].speed;
231 phydev->duplex = settings[idx].duplex;
233 EXPORT_SYMBOL(phy_sanitize_settings);
235 /* phy_ethtool_sset:
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
240 * were set to.
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)
249 return -EINVAL;
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)
257 return -EINVAL;
259 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
260 return -EINVAL;
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)))
268 return -EINVAL;
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;
278 else
279 phydev->advertising &= ~ADVERTISED_Autoneg;
281 phydev->duplex = cmd->duplex;
283 /* Restart the PHY */
284 phy_start_aneg(phydev);
286 return 0;
289 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
291 cmd->supported = phydev->supported;
293 cmd->advertising = phydev->advertising;
295 cmd->speed = phydev->speed;
296 cmd->duplex = phydev->duplex;
297 cmd->port = PORT_MII;
298 cmd->phy_address = phydev->addr;
299 cmd->transceiver = XCVR_EXTERNAL;
300 cmd->autoneg = phydev->autoneg;
302 return 0;
306 /* Note that this function is currently incompatible with the
307 * PHYCONTROL layer. It changes registers without regard to
308 * current state. Use at own risk
310 int phy_mii_ioctl(struct phy_device *phydev,
311 struct mii_ioctl_data *mii_data, int cmd)
313 u16 val = mii_data->val_in;
315 switch (cmd) {
316 case SIOCGMIIPHY:
317 mii_data->phy_id = phydev->addr;
318 break;
319 case SIOCGMIIREG:
320 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
321 break;
323 case SIOCSMIIREG:
324 if (!capable(CAP_NET_ADMIN))
325 return -EPERM;
327 if (mii_data->phy_id == phydev->addr) {
328 switch(mii_data->reg_num) {
329 case MII_BMCR:
330 if (val & (BMCR_RESET|BMCR_ANENABLE))
331 phydev->autoneg = AUTONEG_DISABLE;
332 else
333 phydev->autoneg = AUTONEG_ENABLE;
334 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
335 phydev->duplex = DUPLEX_FULL;
336 else
337 phydev->duplex = DUPLEX_HALF;
338 break;
339 case MII_ADVERTISE:
340 phydev->advertising = val;
341 break;
342 default:
343 /* do nothing */
344 break;
348 phy_write(phydev, mii_data->reg_num, val);
350 if (mii_data->reg_num == MII_BMCR
351 && val & BMCR_RESET
352 && phydev->drv->config_init)
353 phydev->drv->config_init(phydev);
354 break;
357 return 0;
360 /* phy_start_aneg
362 * description: Sanitizes the settings (if we're not
363 * autonegotiating them), and then calls the driver's
364 * config_aneg function. If the PHYCONTROL Layer is operating,
365 * we change the state to reflect the beginning of
366 * Auto-negotiation or forcing.
368 int phy_start_aneg(struct phy_device *phydev)
370 int err;
372 spin_lock(&phydev->lock);
374 if (AUTONEG_DISABLE == phydev->autoneg)
375 phy_sanitize_settings(phydev);
377 err = phydev->drv->config_aneg(phydev);
379 if (err < 0)
380 goto out_unlock;
382 if (phydev->state != PHY_HALTED) {
383 if (AUTONEG_ENABLE == phydev->autoneg) {
384 phydev->state = PHY_AN;
385 phydev->link_timeout = PHY_AN_TIMEOUT;
386 } else {
387 phydev->state = PHY_FORCING;
388 phydev->link_timeout = PHY_FORCE_TIMEOUT;
392 out_unlock:
393 spin_unlock(&phydev->lock);
394 return err;
396 EXPORT_SYMBOL(phy_start_aneg);
399 static void phy_change(void *data);
400 static void phy_timer(unsigned long data);
402 /* phy_start_machine:
404 * description: The PHY infrastructure can run a state machine
405 * which tracks whether the PHY is starting up, negotiating,
406 * etc. This function starts the timer which tracks the state
407 * of the PHY. If you want to be notified when the state
408 * changes, pass in the callback, otherwise, pass NULL. If you
409 * want to maintain your own state machine, do not call this
410 * function. */
411 void phy_start_machine(struct phy_device *phydev,
412 void (*handler)(struct net_device *))
414 phydev->adjust_state = handler;
416 init_timer(&phydev->phy_timer);
417 phydev->phy_timer.function = &phy_timer;
418 phydev->phy_timer.data = (unsigned long) phydev;
419 mod_timer(&phydev->phy_timer, jiffies + HZ);
422 /* phy_stop_machine
424 * description: Stops the state machine timer, sets the state to
425 * UP (unless it wasn't up yet), and then frees the interrupt,
426 * if it is in use. This function must be called BEFORE
427 * phy_detach.
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 if (phydev->irq != PHY_POLL)
439 phy_stop_interrupts(phydev);
441 phydev->adjust_state = NULL;
444 /* phy_force_reduction
446 * description: Reduces the speed/duplex settings by
447 * one notch. The order is so:
448 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
449 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
451 static void phy_force_reduction(struct phy_device *phydev)
453 int idx;
455 idx = phy_find_setting(phydev->speed, phydev->duplex);
457 idx++;
459 idx = phy_find_valid(idx, phydev->supported);
461 phydev->speed = settings[idx].speed;
462 phydev->duplex = settings[idx].duplex;
464 pr_info("Trying %d/%s\n", phydev->speed,
465 DUPLEX_FULL == phydev->duplex ?
466 "FULL" : "HALF");
470 /* phy_error:
472 * Moves the PHY to the HALTED state in response to a read
473 * or write error, and tells the controller the link is down.
474 * Must not be called from interrupt context, or while the
475 * phydev->lock is held.
477 void phy_error(struct phy_device *phydev)
479 spin_lock(&phydev->lock);
480 phydev->state = PHY_HALTED;
481 spin_unlock(&phydev->lock);
484 /* phy_interrupt
486 * description: When a PHY interrupt occurs, the handler disables
487 * interrupts, and schedules a work task to clear the interrupt.
489 static irqreturn_t phy_interrupt(int irq, void *phy_dat, struct pt_regs *regs)
491 struct phy_device *phydev = phy_dat;
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);
501 return IRQ_HANDLED;
504 /* Enable the interrupts from the PHY side */
505 int phy_enable_interrupts(struct phy_device *phydev)
507 int err;
509 err = phy_clear_interrupt(phydev);
511 if (err < 0)
512 return err;
514 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
516 return err;
518 EXPORT_SYMBOL(phy_enable_interrupts);
520 /* Disable the PHY interrupts from the PHY side */
521 int phy_disable_interrupts(struct phy_device *phydev)
523 int err;
525 /* Disable PHY interrupts */
526 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
528 if (err)
529 goto phy_err;
531 /* Clear the interrupt */
532 err = phy_clear_interrupt(phydev);
534 if (err)
535 goto phy_err;
537 return 0;
539 phy_err:
540 phy_error(phydev);
542 return err;
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)
556 int err = 0;
558 INIT_WORK(&phydev->phy_queue, phy_change, phydev);
560 if (request_irq(phydev->irq, phy_interrupt,
561 SA_SHIRQ,
562 "phy_interrupt",
563 phydev) < 0) {
564 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
565 phydev->bus->name,
566 phydev->irq);
567 phydev->irq = PHY_POLL;
568 return 0;
571 err = phy_enable_interrupts(phydev);
573 return err;
575 EXPORT_SYMBOL(phy_start_interrupts);
577 int phy_stop_interrupts(struct phy_device *phydev)
579 int err;
581 err = phy_disable_interrupts(phydev);
583 if (err)
584 phy_error(phydev);
586 free_irq(phydev->irq, phydev);
588 return err;
590 EXPORT_SYMBOL(phy_stop_interrupts);
593 /* Scheduled by the phy_interrupt/timer to handle PHY changes */
594 static void phy_change(void *data)
596 int err;
597 struct phy_device *phydev = data;
599 err = phy_disable_interrupts(phydev);
601 if (err)
602 goto phy_err;
604 spin_lock(&phydev->lock);
605 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
606 phydev->state = PHY_CHANGELINK;
607 spin_unlock(&phydev->lock);
609 enable_irq(phydev->irq);
611 /* Reenable interrupts */
612 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
614 if (err)
615 goto irq_enable_err;
617 return;
619 irq_enable_err:
620 disable_irq(phydev->irq);
621 phy_err:
622 phy_error(phydev);
625 /* Bring down the PHY link, and stop checking the status. */
626 void phy_stop(struct phy_device *phydev)
628 spin_lock(&phydev->lock);
630 if (PHY_HALTED == phydev->state)
631 goto out_unlock;
633 if (phydev->irq != PHY_POLL) {
634 /* Clear any pending interrupts */
635 phy_clear_interrupt(phydev);
637 /* Disable PHY Interrupts */
638 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
641 phydev->state = PHY_HALTED;
643 out_unlock:
644 spin_unlock(&phydev->lock);
648 /* phy_start
650 * description: Indicates the attached device's readiness to
651 * handle PHY-related work. Used during startup to start the
652 * PHY, and after a call to phy_stop() to resume operation.
653 * Also used to indicate the MDIO bus has cleared an error
654 * condition.
656 void phy_start(struct phy_device *phydev)
658 spin_lock(&phydev->lock);
660 switch (phydev->state) {
661 case PHY_STARTING:
662 phydev->state = PHY_PENDING;
663 break;
664 case PHY_READY:
665 phydev->state = PHY_UP;
666 break;
667 case PHY_HALTED:
668 phydev->state = PHY_RESUMING;
669 default:
670 break;
672 spin_unlock(&phydev->lock);
674 EXPORT_SYMBOL(phy_stop);
675 EXPORT_SYMBOL(phy_start);
677 /* PHY timer which handles the state machine */
678 static void phy_timer(unsigned long data)
680 struct phy_device *phydev = (struct phy_device *)data;
681 int needs_aneg = 0;
682 int err = 0;
684 spin_lock(&phydev->lock);
686 if (phydev->adjust_state)
687 phydev->adjust_state(phydev->attached_dev);
689 switch(phydev->state) {
690 case PHY_DOWN:
691 case PHY_STARTING:
692 case PHY_READY:
693 case PHY_PENDING:
694 break;
695 case PHY_UP:
696 needs_aneg = 1;
698 phydev->link_timeout = PHY_AN_TIMEOUT;
700 break;
701 case PHY_AN:
702 /* Check if negotiation is done. Break
703 * if there's an error */
704 err = phy_aneg_done(phydev);
705 if (err < 0)
706 break;
708 /* If auto-negotiation is done, we change to
709 * either RUNNING, or NOLINK */
710 if (err > 0) {
711 err = phy_read_status(phydev);
713 if (err)
714 break;
716 if (phydev->link) {
717 phydev->state = PHY_RUNNING;
718 netif_carrier_on(phydev->attached_dev);
719 } else {
720 phydev->state = PHY_NOLINK;
721 netif_carrier_off(phydev->attached_dev);
724 phydev->adjust_link(phydev->attached_dev);
726 } else if (0 == phydev->link_timeout--) {
727 /* The counter expired, so either we
728 * switch to forced mode, or the
729 * magic_aneg bit exists, and we try aneg
730 * again */
731 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
732 int idx;
734 /* We'll start from the
735 * fastest speed, and work
736 * our way down */
737 idx = phy_find_valid(0,
738 phydev->supported);
740 phydev->speed = settings[idx].speed;
741 phydev->duplex = settings[idx].duplex;
743 phydev->autoneg = AUTONEG_DISABLE;
744 phydev->state = PHY_FORCING;
745 phydev->link_timeout =
746 PHY_FORCE_TIMEOUT;
748 pr_info("Trying %d/%s\n",
749 phydev->speed,
750 DUPLEX_FULL ==
751 phydev->duplex ?
752 "FULL" : "HALF");
755 needs_aneg = 1;
757 break;
758 case PHY_NOLINK:
759 err = phy_read_status(phydev);
761 if (err)
762 break;
764 if (phydev->link) {
765 phydev->state = PHY_RUNNING;
766 netif_carrier_on(phydev->attached_dev);
767 phydev->adjust_link(phydev->attached_dev);
769 break;
770 case PHY_FORCING:
771 err = phy_read_status(phydev);
773 if (err)
774 break;
776 if (phydev->link) {
777 phydev->state = PHY_RUNNING;
778 netif_carrier_on(phydev->attached_dev);
779 } else {
780 if (0 == phydev->link_timeout--) {
781 phy_force_reduction(phydev);
782 needs_aneg = 1;
786 phydev->adjust_link(phydev->attached_dev);
787 break;
788 case PHY_RUNNING:
789 /* Only register a CHANGE if we are
790 * polling */
791 if (PHY_POLL == phydev->irq)
792 phydev->state = PHY_CHANGELINK;
793 break;
794 case PHY_CHANGELINK:
795 err = phy_read_status(phydev);
797 if (err)
798 break;
800 if (phydev->link) {
801 phydev->state = PHY_RUNNING;
802 netif_carrier_on(phydev->attached_dev);
803 } else {
804 phydev->state = PHY_NOLINK;
805 netif_carrier_off(phydev->attached_dev);
808 phydev->adjust_link(phydev->attached_dev);
810 if (PHY_POLL != phydev->irq)
811 err = phy_config_interrupt(phydev,
812 PHY_INTERRUPT_ENABLED);
813 break;
814 case PHY_HALTED:
815 if (phydev->link) {
816 phydev->link = 0;
817 netif_carrier_off(phydev->attached_dev);
818 phydev->adjust_link(phydev->attached_dev);
820 break;
821 case PHY_RESUMING:
823 err = phy_clear_interrupt(phydev);
825 if (err)
826 break;
828 err = phy_config_interrupt(phydev,
829 PHY_INTERRUPT_ENABLED);
831 if (err)
832 break;
834 if (AUTONEG_ENABLE == phydev->autoneg) {
835 err = phy_aneg_done(phydev);
836 if (err < 0)
837 break;
839 /* err > 0 if AN is done.
840 * Otherwise, it's 0, and we're
841 * still waiting for AN */
842 if (err > 0) {
843 phydev->state = PHY_RUNNING;
844 } else {
845 phydev->state = PHY_AN;
846 phydev->link_timeout = PHY_AN_TIMEOUT;
848 } else
849 phydev->state = PHY_RUNNING;
850 break;
853 spin_unlock(&phydev->lock);
855 if (needs_aneg)
856 err = phy_start_aneg(phydev);
858 if (err < 0)
859 phy_error(phydev);
861 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);