Linux 3.12.49
[linux/fpc-iii.git] / drivers / net / phy / phy.c
blob0bc73f2c24ba015d5dc9bc5b1d2557d5c4462c06
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.
10 * Copyright (c) 2006, 2007 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.
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/errno.h>
24 #include <linux/unistd.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/mii.h>
34 #include <linux/ethtool.h>
35 #include <linux/phy.h>
36 #include <linux/timer.h>
37 #include <linux/workqueue.h>
38 #include <linux/mdio.h>
40 #include <linux/atomic.h>
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/uaccess.h>
45 /**
46 * phy_print_status - Convenience function to print out the current phy status
47 * @phydev: the phy_device struct
49 void phy_print_status(struct phy_device *phydev)
51 if (phydev->link)
52 pr_info("%s - Link is Up - %d/%s\n",
53 dev_name(&phydev->dev),
54 phydev->speed,
55 DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
56 else
57 pr_info("%s - Link is Down\n", dev_name(&phydev->dev));
59 EXPORT_SYMBOL(phy_print_status);
61 /**
62 * phy_clear_interrupt - Ack the phy device's interrupt
63 * @phydev: the phy_device struct
65 * If the @phydev driver has an ack_interrupt function, call it to
66 * ack and clear the phy device's interrupt.
68 * Returns 0 on success on < 0 on error.
70 static int phy_clear_interrupt(struct phy_device *phydev)
72 int err = 0;
74 if (phydev->drv->ack_interrupt)
75 err = phydev->drv->ack_interrupt(phydev);
77 return err;
80 /**
81 * phy_config_interrupt - configure the PHY device for the requested interrupts
82 * @phydev: the phy_device struct
83 * @interrupts: interrupt flags to configure for this @phydev
85 * Returns 0 on success on < 0 on error.
87 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
89 int err = 0;
91 phydev->interrupts = interrupts;
92 if (phydev->drv->config_intr)
93 err = phydev->drv->config_intr(phydev);
95 return err;
99 /**
100 * phy_aneg_done - return auto-negotiation status
101 * @phydev: target phy_device struct
103 * Description: Reads the status register and returns 0 either if
104 * auto-negotiation is incomplete, or if there was an error.
105 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
107 static inline int phy_aneg_done(struct phy_device *phydev)
109 int retval;
111 retval = phy_read(phydev, MII_BMSR);
113 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
116 /* A structure for mapping a particular speed and duplex
117 * combination to a particular SUPPORTED and ADVERTISED value */
118 struct phy_setting {
119 int speed;
120 int duplex;
121 u32 setting;
124 /* A mapping of all SUPPORTED settings to speed/duplex */
125 static const struct phy_setting settings[] = {
127 .speed = 10000,
128 .duplex = DUPLEX_FULL,
129 .setting = SUPPORTED_10000baseT_Full,
132 .speed = SPEED_1000,
133 .duplex = DUPLEX_FULL,
134 .setting = SUPPORTED_1000baseT_Full,
137 .speed = SPEED_1000,
138 .duplex = DUPLEX_HALF,
139 .setting = SUPPORTED_1000baseT_Half,
142 .speed = SPEED_100,
143 .duplex = DUPLEX_FULL,
144 .setting = SUPPORTED_100baseT_Full,
147 .speed = SPEED_100,
148 .duplex = DUPLEX_HALF,
149 .setting = SUPPORTED_100baseT_Half,
152 .speed = SPEED_10,
153 .duplex = DUPLEX_FULL,
154 .setting = SUPPORTED_10baseT_Full,
157 .speed = SPEED_10,
158 .duplex = DUPLEX_HALF,
159 .setting = SUPPORTED_10baseT_Half,
163 #define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
166 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
167 * @speed: speed to match
168 * @duplex: duplex to match
170 * Description: Searches the settings array for the setting which
171 * matches the desired speed and duplex, and returns the index
172 * of that setting. Returns the index of the last setting if
173 * none of the others match.
175 static inline int phy_find_setting(int speed, int duplex)
177 int idx = 0;
179 while (idx < ARRAY_SIZE(settings) &&
180 (settings[idx].speed != speed ||
181 settings[idx].duplex != duplex))
182 idx++;
184 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
188 * phy_find_valid - find a PHY setting that matches the requested features mask
189 * @idx: The first index in settings[] to search
190 * @features: A mask of the valid settings
192 * Description: Returns the index of the first valid setting less
193 * than or equal to the one pointed to by idx, as determined by
194 * the mask in features. Returns the index of the last setting
195 * if nothing else matches.
197 static inline int phy_find_valid(int idx, u32 features)
199 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
200 idx++;
202 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
206 * phy_check_valid - check if there is a valid PHY setting which matches
207 * speed, duplex, and feature mask
208 * @speed: speed to match
209 * @duplex: duplex to match
210 * @features: A mask of the valid settings
212 * Description: Returns true if there is a valid setting, false otherwise.
214 static inline bool phy_check_valid(int speed, int duplex, u32 features)
216 unsigned int idx;
218 idx = phy_find_valid(phy_find_setting(speed, duplex), features);
220 return settings[idx].speed == speed && settings[idx].duplex == duplex &&
221 (settings[idx].setting & features);
225 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
226 * @phydev: the target phy_device struct
228 * Description: Make sure the PHY is set to supported speeds and
229 * duplexes. Drop down by one in this order: 1000/FULL,
230 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
232 static void phy_sanitize_settings(struct phy_device *phydev)
234 u32 features = phydev->supported;
235 int idx;
237 /* Sanitize settings based on PHY capabilities */
238 if ((features & SUPPORTED_Autoneg) == 0)
239 phydev->autoneg = AUTONEG_DISABLE;
241 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
242 features);
244 phydev->speed = settings[idx].speed;
245 phydev->duplex = settings[idx].duplex;
249 * phy_ethtool_sset - generic ethtool sset function, handles all the details
250 * @phydev: target phy_device struct
251 * @cmd: ethtool_cmd
253 * A few notes about parameter checking:
254 * - We don't set port or transceiver, so we don't care what they
255 * were set to.
256 * - phy_start_aneg() will make sure forced settings are sane, and
257 * choose the next best ones from the ones selected, so we don't
258 * care if ethtool tries to give us bad values.
260 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
262 u32 speed = ethtool_cmd_speed(cmd);
264 if (cmd->phy_address != phydev->addr)
265 return -EINVAL;
267 /* We make sure that we don't pass unsupported
268 * values in to the PHY */
269 cmd->advertising &= phydev->supported;
271 /* Verify the settings we care about. */
272 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
273 return -EINVAL;
275 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
276 return -EINVAL;
278 if (cmd->autoneg == AUTONEG_DISABLE &&
279 ((speed != SPEED_1000 &&
280 speed != SPEED_100 &&
281 speed != SPEED_10) ||
282 (cmd->duplex != DUPLEX_HALF &&
283 cmd->duplex != DUPLEX_FULL)))
284 return -EINVAL;
286 phydev->autoneg = cmd->autoneg;
288 phydev->speed = speed;
290 phydev->advertising = cmd->advertising;
292 if (AUTONEG_ENABLE == cmd->autoneg)
293 phydev->advertising |= ADVERTISED_Autoneg;
294 else
295 phydev->advertising &= ~ADVERTISED_Autoneg;
297 phydev->duplex = cmd->duplex;
299 /* Restart the PHY */
300 phy_start_aneg(phydev);
302 return 0;
304 EXPORT_SYMBOL(phy_ethtool_sset);
306 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
308 cmd->supported = phydev->supported;
310 cmd->advertising = phydev->advertising;
312 ethtool_cmd_speed_set(cmd, phydev->speed);
313 cmd->duplex = phydev->duplex;
314 cmd->port = PORT_MII;
315 cmd->phy_address = phydev->addr;
316 cmd->transceiver = phy_is_internal(phydev) ?
317 XCVR_INTERNAL : XCVR_EXTERNAL;
318 cmd->autoneg = phydev->autoneg;
320 return 0;
322 EXPORT_SYMBOL(phy_ethtool_gset);
325 * phy_mii_ioctl - generic PHY MII ioctl interface
326 * @phydev: the phy_device struct
327 * @ifr: &struct ifreq for socket ioctl's
328 * @cmd: ioctl cmd to execute
330 * Note that this function is currently incompatible with the
331 * PHYCONTROL layer. It changes registers without regard to
332 * current state. Use at own risk.
334 int phy_mii_ioctl(struct phy_device *phydev,
335 struct ifreq *ifr, int cmd)
337 struct mii_ioctl_data *mii_data = if_mii(ifr);
338 u16 val = mii_data->val_in;
340 switch (cmd) {
341 case SIOCGMIIPHY:
342 mii_data->phy_id = phydev->addr;
343 /* fall through */
345 case SIOCGMIIREG:
346 mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
347 mii_data->reg_num);
348 break;
350 case SIOCSMIIREG:
351 if (mii_data->phy_id == phydev->addr) {
352 switch(mii_data->reg_num) {
353 case MII_BMCR:
354 if ((val & (BMCR_RESET|BMCR_ANENABLE)) == 0)
355 phydev->autoneg = AUTONEG_DISABLE;
356 else
357 phydev->autoneg = AUTONEG_ENABLE;
358 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
359 phydev->duplex = DUPLEX_FULL;
360 else
361 phydev->duplex = DUPLEX_HALF;
362 if ((!phydev->autoneg) &&
363 (val & BMCR_SPEED1000))
364 phydev->speed = SPEED_1000;
365 else if ((!phydev->autoneg) &&
366 (val & BMCR_SPEED100))
367 phydev->speed = SPEED_100;
368 break;
369 case MII_ADVERTISE:
370 phydev->advertising = val;
371 break;
372 default:
373 /* do nothing */
374 break;
378 mdiobus_write(phydev->bus, mii_data->phy_id,
379 mii_data->reg_num, val);
381 if (mii_data->reg_num == MII_BMCR &&
382 val & BMCR_RESET &&
383 phydev->drv->config_init) {
384 phy_scan_fixups(phydev);
385 phydev->drv->config_init(phydev);
387 break;
389 case SIOCSHWTSTAMP:
390 if (phydev->drv->hwtstamp)
391 return phydev->drv->hwtstamp(phydev, ifr);
392 /* fall through */
394 default:
395 return -EOPNOTSUPP;
398 return 0;
400 EXPORT_SYMBOL(phy_mii_ioctl);
403 * phy_start_aneg - start auto-negotiation for this PHY device
404 * @phydev: the phy_device struct
406 * Description: Sanitizes the settings (if we're not autonegotiating
407 * them), and then calls the driver's config_aneg function.
408 * If the PHYCONTROL Layer is operating, we change the state to
409 * reflect the beginning of Auto-negotiation or forcing.
411 int phy_start_aneg(struct phy_device *phydev)
413 int err;
415 mutex_lock(&phydev->lock);
417 if (AUTONEG_DISABLE == phydev->autoneg)
418 phy_sanitize_settings(phydev);
420 err = phydev->drv->config_aneg(phydev);
422 if (err < 0)
423 goto out_unlock;
425 if (phydev->state != PHY_HALTED) {
426 if (AUTONEG_ENABLE == phydev->autoneg) {
427 phydev->state = PHY_AN;
428 phydev->link_timeout = PHY_AN_TIMEOUT;
429 } else {
430 phydev->state = PHY_FORCING;
431 phydev->link_timeout = PHY_FORCE_TIMEOUT;
435 out_unlock:
436 mutex_unlock(&phydev->lock);
437 return err;
439 EXPORT_SYMBOL(phy_start_aneg);
443 * phy_start_machine - start PHY state machine tracking
444 * @phydev: the phy_device struct
445 * @handler: callback function for state change notifications
447 * Description: The PHY infrastructure can run a state machine
448 * which tracks whether the PHY is starting up, negotiating,
449 * etc. This function starts the timer which tracks the state
450 * of the PHY. If you want to be notified when the state changes,
451 * pass in the callback @handler, otherwise, pass NULL. If you
452 * want to maintain your own state machine, do not call this
453 * function.
455 void phy_start_machine(struct phy_device *phydev,
456 void (*handler)(struct net_device *))
458 phydev->adjust_state = handler;
460 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
464 * phy_stop_machine - stop the PHY state machine tracking
465 * @phydev: target phy_device struct
467 * Description: Stops the state machine timer, sets the state to UP
468 * (unless it wasn't up yet). This function must be called BEFORE
469 * phy_detach.
471 void phy_stop_machine(struct phy_device *phydev)
473 cancel_delayed_work_sync(&phydev->state_queue);
475 mutex_lock(&phydev->lock);
476 if (phydev->state > PHY_UP)
477 phydev->state = PHY_UP;
478 mutex_unlock(&phydev->lock);
480 phydev->adjust_state = NULL;
484 * phy_error - enter HALTED state for this PHY device
485 * @phydev: target phy_device struct
487 * Moves the PHY to the HALTED state in response to a read
488 * or write error, and tells the controller the link is down.
489 * Must not be called from interrupt context, or while the
490 * phydev->lock is held.
492 static void phy_error(struct phy_device *phydev)
494 mutex_lock(&phydev->lock);
495 phydev->state = PHY_HALTED;
496 mutex_unlock(&phydev->lock);
500 * phy_interrupt - PHY interrupt handler
501 * @irq: interrupt line
502 * @phy_dat: phy_device pointer
504 * Description: When a PHY interrupt occurs, the handler disables
505 * interrupts, and schedules a work task to clear the interrupt.
507 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
509 struct phy_device *phydev = phy_dat;
511 if (PHY_HALTED == phydev->state)
512 return IRQ_NONE; /* It can't be ours. */
514 /* The MDIO bus is not allowed to be written in interrupt
515 * context, so we need to disable the irq here. A work
516 * queue will write the PHY to disable and clear the
517 * interrupt, and then reenable the irq line. */
518 disable_irq_nosync(irq);
519 atomic_inc(&phydev->irq_disable);
521 queue_work(system_power_efficient_wq, &phydev->phy_queue);
523 return IRQ_HANDLED;
527 * phy_enable_interrupts - Enable the interrupts from the PHY side
528 * @phydev: target phy_device struct
530 static int phy_enable_interrupts(struct phy_device *phydev)
532 int err;
534 err = phy_clear_interrupt(phydev);
536 if (err < 0)
537 return err;
539 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
541 return err;
545 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
546 * @phydev: target phy_device struct
548 static int phy_disable_interrupts(struct phy_device *phydev)
550 int err;
552 /* Disable PHY interrupts */
553 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
555 if (err)
556 goto phy_err;
558 /* Clear the interrupt */
559 err = phy_clear_interrupt(phydev);
561 if (err)
562 goto phy_err;
564 return 0;
566 phy_err:
567 phy_error(phydev);
569 return err;
573 * phy_start_interrupts - request and enable interrupts for a PHY device
574 * @phydev: target phy_device struct
576 * Description: Request the interrupt for the given PHY.
577 * If this fails, then we set irq to PHY_POLL.
578 * Otherwise, we enable the interrupts in the PHY.
579 * This should only be called with a valid IRQ number.
580 * Returns 0 on success or < 0 on error.
582 int phy_start_interrupts(struct phy_device *phydev)
584 int err = 0;
586 atomic_set(&phydev->irq_disable, 0);
587 if (request_irq(phydev->irq, phy_interrupt,
588 IRQF_SHARED,
589 "phy_interrupt",
590 phydev) < 0) {
591 pr_warn("%s: Can't get IRQ %d (PHY)\n",
592 phydev->bus->name, phydev->irq);
593 phydev->irq = PHY_POLL;
594 return 0;
597 err = phy_enable_interrupts(phydev);
599 return err;
601 EXPORT_SYMBOL(phy_start_interrupts);
604 * phy_stop_interrupts - disable interrupts from a PHY device
605 * @phydev: target phy_device struct
607 int phy_stop_interrupts(struct phy_device *phydev)
609 int err;
611 err = phy_disable_interrupts(phydev);
613 if (err)
614 phy_error(phydev);
616 free_irq(phydev->irq, phydev);
619 * Cannot call flush_scheduled_work() here as desired because
620 * of rtnl_lock(), but we do not really care about what would
621 * be done, except from enable_irq(), so cancel any work
622 * possibly pending and take care of the matter below.
624 cancel_work_sync(&phydev->phy_queue);
626 * If work indeed has been cancelled, disable_irq() will have
627 * been left unbalanced from phy_interrupt() and enable_irq()
628 * has to be called so that other devices on the line work.
630 while (atomic_dec_return(&phydev->irq_disable) >= 0)
631 enable_irq(phydev->irq);
633 return err;
635 EXPORT_SYMBOL(phy_stop_interrupts);
639 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
640 * @work: work_struct that describes the work to be done
642 void phy_change(struct work_struct *work)
644 int err;
645 struct phy_device *phydev =
646 container_of(work, struct phy_device, phy_queue);
648 if (phydev->drv->did_interrupt &&
649 !phydev->drv->did_interrupt(phydev))
650 goto ignore;
652 err = phy_disable_interrupts(phydev);
654 if (err)
655 goto phy_err;
657 mutex_lock(&phydev->lock);
658 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
659 phydev->state = PHY_CHANGELINK;
660 mutex_unlock(&phydev->lock);
662 atomic_dec(&phydev->irq_disable);
663 enable_irq(phydev->irq);
665 /* Reenable interrupts */
666 if (PHY_HALTED != phydev->state)
667 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
669 if (err)
670 goto irq_enable_err;
672 /* reschedule state queue work to run as soon as possible */
673 cancel_delayed_work_sync(&phydev->state_queue);
674 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
676 return;
678 ignore:
679 atomic_dec(&phydev->irq_disable);
680 enable_irq(phydev->irq);
681 return;
683 irq_enable_err:
684 disable_irq(phydev->irq);
685 atomic_inc(&phydev->irq_disable);
686 phy_err:
687 phy_error(phydev);
691 * phy_stop - Bring down the PHY link, and stop checking the status
692 * @phydev: target phy_device struct
694 void phy_stop(struct phy_device *phydev)
696 mutex_lock(&phydev->lock);
698 if (PHY_HALTED == phydev->state)
699 goto out_unlock;
701 if (phy_interrupt_is_valid(phydev)) {
702 /* Disable PHY Interrupts */
703 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
705 /* Clear any pending interrupts */
706 phy_clear_interrupt(phydev);
709 phydev->state = PHY_HALTED;
711 out_unlock:
712 mutex_unlock(&phydev->lock);
715 * Cannot call flush_scheduled_work() here as desired because
716 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
717 * will not reenable interrupts.
723 * phy_start - start or restart a PHY device
724 * @phydev: target phy_device struct
726 * Description: Indicates the attached device's readiness to
727 * handle PHY-related work. Used during startup to start the
728 * PHY, and after a call to phy_stop() to resume operation.
729 * Also used to indicate the MDIO bus has cleared an error
730 * condition.
732 void phy_start(struct phy_device *phydev)
734 mutex_lock(&phydev->lock);
736 switch (phydev->state) {
737 case PHY_STARTING:
738 phydev->state = PHY_PENDING;
739 break;
740 case PHY_READY:
741 phydev->state = PHY_UP;
742 break;
743 case PHY_HALTED:
744 phydev->state = PHY_RESUMING;
745 default:
746 break;
748 mutex_unlock(&phydev->lock);
750 EXPORT_SYMBOL(phy_stop);
751 EXPORT_SYMBOL(phy_start);
754 * phy_state_machine - Handle the state machine
755 * @work: work_struct that describes the work to be done
757 void phy_state_machine(struct work_struct *work)
759 struct delayed_work *dwork = to_delayed_work(work);
760 struct phy_device *phydev =
761 container_of(dwork, struct phy_device, state_queue);
762 int needs_aneg = 0;
763 int err = 0;
765 mutex_lock(&phydev->lock);
767 if (phydev->adjust_state)
768 phydev->adjust_state(phydev->attached_dev);
770 switch(phydev->state) {
771 case PHY_DOWN:
772 case PHY_STARTING:
773 case PHY_READY:
774 case PHY_PENDING:
775 break;
776 case PHY_UP:
777 needs_aneg = 1;
779 phydev->link_timeout = PHY_AN_TIMEOUT;
781 break;
782 case PHY_AN:
783 err = phy_read_status(phydev);
785 if (err < 0)
786 break;
788 /* If the link is down, give up on
789 * negotiation for now */
790 if (!phydev->link) {
791 phydev->state = PHY_NOLINK;
792 netif_carrier_off(phydev->attached_dev);
793 phydev->adjust_link(phydev->attached_dev);
794 break;
797 /* Check if negotiation is done. Break
798 * if there's an error */
799 err = phy_aneg_done(phydev);
800 if (err < 0)
801 break;
803 /* If AN is done, we're running */
804 if (err > 0) {
805 phydev->state = PHY_RUNNING;
806 netif_carrier_on(phydev->attached_dev);
807 phydev->adjust_link(phydev->attached_dev);
809 } else if (0 == phydev->link_timeout--) {
810 needs_aneg = 1;
811 /* If we have the magic_aneg bit,
812 * we try again */
813 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
814 break;
816 break;
817 case PHY_NOLINK:
818 err = phy_read_status(phydev);
820 if (err)
821 break;
823 if (phydev->link) {
824 phydev->state = PHY_RUNNING;
825 netif_carrier_on(phydev->attached_dev);
826 phydev->adjust_link(phydev->attached_dev);
828 break;
829 case PHY_FORCING:
830 err = genphy_update_link(phydev);
832 if (err)
833 break;
835 if (phydev->link) {
836 phydev->state = PHY_RUNNING;
837 netif_carrier_on(phydev->attached_dev);
838 } else {
839 if (0 == phydev->link_timeout--)
840 needs_aneg = 1;
843 phydev->adjust_link(phydev->attached_dev);
844 break;
845 case PHY_RUNNING:
846 /* Only register a CHANGE if we are
847 * polling or ignoring interrupts
849 if (!phy_interrupt_is_valid(phydev))
850 phydev->state = PHY_CHANGELINK;
851 break;
852 case PHY_CHANGELINK:
853 err = phy_read_status(phydev);
855 if (err)
856 break;
858 if (phydev->link) {
859 phydev->state = PHY_RUNNING;
860 netif_carrier_on(phydev->attached_dev);
861 } else {
862 phydev->state = PHY_NOLINK;
863 netif_carrier_off(phydev->attached_dev);
866 phydev->adjust_link(phydev->attached_dev);
868 if (phy_interrupt_is_valid(phydev))
869 err = phy_config_interrupt(phydev,
870 PHY_INTERRUPT_ENABLED);
871 break;
872 case PHY_HALTED:
873 if (phydev->link) {
874 phydev->link = 0;
875 netif_carrier_off(phydev->attached_dev);
876 phydev->adjust_link(phydev->attached_dev);
878 break;
879 case PHY_RESUMING:
881 err = phy_clear_interrupt(phydev);
883 if (err)
884 break;
886 err = phy_config_interrupt(phydev,
887 PHY_INTERRUPT_ENABLED);
889 if (err)
890 break;
892 if (AUTONEG_ENABLE == phydev->autoneg) {
893 err = phy_aneg_done(phydev);
894 if (err < 0)
895 break;
897 /* err > 0 if AN is done.
898 * Otherwise, it's 0, and we're
899 * still waiting for AN */
900 if (err > 0) {
901 err = phy_read_status(phydev);
902 if (err)
903 break;
905 if (phydev->link) {
906 phydev->state = PHY_RUNNING;
907 netif_carrier_on(phydev->attached_dev);
908 } else
909 phydev->state = PHY_NOLINK;
910 phydev->adjust_link(phydev->attached_dev);
911 } else {
912 phydev->state = PHY_AN;
913 phydev->link_timeout = PHY_AN_TIMEOUT;
915 } else {
916 err = phy_read_status(phydev);
917 if (err)
918 break;
920 if (phydev->link) {
921 phydev->state = PHY_RUNNING;
922 netif_carrier_on(phydev->attached_dev);
923 } else
924 phydev->state = PHY_NOLINK;
925 phydev->adjust_link(phydev->attached_dev);
927 break;
930 mutex_unlock(&phydev->lock);
932 if (needs_aneg)
933 err = phy_start_aneg(phydev);
935 if (err < 0)
936 phy_error(phydev);
938 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
939 PHY_STATE_TIME * HZ);
942 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
944 cancel_work_sync(&phydev->phy_queue);
945 phydev->link = new_link;
946 schedule_work(&phydev->phy_queue);
948 EXPORT_SYMBOL(phy_mac_interrupt);
950 static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
951 int addr)
953 /* Write the desired MMD Devad */
954 bus->write(bus, addr, MII_MMD_CTRL, devad);
956 /* Write the desired MMD register address */
957 bus->write(bus, addr, MII_MMD_DATA, prtad);
959 /* Select the Function : DATA with no post increment */
960 bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
964 * phy_read_mmd_indirect - reads data from the MMD registers
965 * @bus: the target MII bus
966 * @prtad: MMD Address
967 * @devad: MMD DEVAD
968 * @addr: PHY address on the MII bus
970 * Description: it reads data from the MMD registers (clause 22 to access to
971 * clause 45) of the specified phy address.
972 * To read these register we have:
973 * 1) Write reg 13 // DEVAD
974 * 2) Write reg 14 // MMD Address
975 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
976 * 3) Read reg 14 // Read MMD data
978 static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
979 int addr)
981 u32 ret;
983 mmd_phy_indirect(bus, prtad, devad, addr);
985 /* Read the content of the MMD's selected register */
986 ret = bus->read(bus, addr, MII_MMD_DATA);
988 return ret;
992 * phy_write_mmd_indirect - writes data to the MMD registers
993 * @bus: the target MII bus
994 * @prtad: MMD Address
995 * @devad: MMD DEVAD
996 * @addr: PHY address on the MII bus
997 * @data: data to write in the MMD register
999 * Description: Write data from the MMD registers of the specified
1000 * phy address.
1001 * To write these register we have:
1002 * 1) Write reg 13 // DEVAD
1003 * 2) Write reg 14 // MMD Address
1004 * 3) Write reg 13 // MMD Data Command for MMD DEVAD
1005 * 3) Write reg 14 // Write MMD data
1007 static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
1008 int addr, u32 data)
1010 mmd_phy_indirect(bus, prtad, devad, addr);
1012 /* Write the data into MMD's selected register */
1013 bus->write(bus, addr, MII_MMD_DATA, data);
1017 * phy_init_eee - init and check the EEE feature
1018 * @phydev: target phy_device struct
1019 * @clk_stop_enable: PHY may stop the clock during LPI
1021 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1022 * is supported by looking at the MMD registers 3.20 and 7.60/61
1023 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1024 * bit if required.
1026 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1028 int ret = -EPROTONOSUPPORT;
1030 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1031 * Also EEE feature is active when core is operating with MII, GMII
1032 * or RGMII.
1034 if ((phydev->duplex == DUPLEX_FULL) &&
1035 ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
1036 (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
1037 (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
1038 int eee_lp, eee_cap, eee_adv;
1039 u32 lp, cap, adv;
1040 int status;
1042 /* Read phy status to properly get the right settings */
1043 status = phy_read_status(phydev);
1044 if (status)
1045 return status;
1047 /* First check if the EEE ability is supported */
1048 eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
1049 MDIO_MMD_PCS, phydev->addr);
1050 if (eee_cap < 0)
1051 return eee_cap;
1053 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1054 if (!cap)
1055 goto eee_exit;
1057 /* Check which link settings negotiated and verify it in
1058 * the EEE advertising registers.
1060 eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
1061 MDIO_MMD_AN, phydev->addr);
1062 if (eee_lp < 0)
1063 return eee_lp;
1065 eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
1066 MDIO_MMD_AN, phydev->addr);
1067 if (eee_adv < 0)
1068 return eee_adv;
1070 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1071 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1072 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1073 goto eee_exit;
1075 if (clk_stop_enable) {
1076 /* Configure the PHY to stop receiving xMII
1077 * clock while it is signaling LPI.
1079 int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
1080 MDIO_MMD_PCS,
1081 phydev->addr);
1082 if (val < 0)
1083 return val;
1085 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1086 phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
1087 MDIO_MMD_PCS, phydev->addr, val);
1090 ret = 0; /* EEE supported */
1093 eee_exit:
1094 return ret;
1096 EXPORT_SYMBOL(phy_init_eee);
1099 * phy_get_eee_err - report the EEE wake error count
1100 * @phydev: target phy_device struct
1102 * Description: it is to report the number of time where the PHY
1103 * failed to complete its normal wake sequence.
1105 int phy_get_eee_err(struct phy_device *phydev)
1107 return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
1108 MDIO_MMD_PCS, phydev->addr);
1111 EXPORT_SYMBOL(phy_get_eee_err);
1114 * phy_ethtool_get_eee - get EEE supported and status
1115 * @phydev: target phy_device struct
1116 * @data: ethtool_eee data
1118 * Description: it reportes the Supported/Advertisement/LP Advertisement
1119 * capabilities.
1121 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1123 int val;
1125 /* Get Supported EEE */
1126 val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
1127 MDIO_MMD_PCS, phydev->addr);
1128 if (val < 0)
1129 return val;
1130 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1132 /* Get advertisement EEE */
1133 val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
1134 MDIO_MMD_AN, phydev->addr);
1135 if (val < 0)
1136 return val;
1137 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1139 /* Get LP advertisement EEE */
1140 val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
1141 MDIO_MMD_AN, phydev->addr);
1142 if (val < 0)
1143 return val;
1144 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1146 return 0;
1148 EXPORT_SYMBOL(phy_ethtool_get_eee);
1151 * phy_ethtool_set_eee - set EEE supported and status
1152 * @phydev: target phy_device struct
1153 * @data: ethtool_eee data
1155 * Description: it is to program the Advertisement EEE register.
1157 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1159 int val;
1161 val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
1162 phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
1163 phydev->addr, val);
1165 return 0;
1167 EXPORT_SYMBOL(phy_ethtool_set_eee);
1169 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1171 if (phydev->drv->set_wol)
1172 return phydev->drv->set_wol(phydev, wol);
1174 return -EOPNOTSUPP;
1176 EXPORT_SYMBOL(phy_ethtool_set_wol);
1178 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1180 if (phydev->drv->get_wol)
1181 phydev->drv->get_wol(phydev, wol);
1183 EXPORT_SYMBOL(phy_ethtool_get_wol);