1 /* Framework for configuring and reading PHY devices
2 * Based on code in sungem_phy.c and gianfar_phy.c
6 * Copyright (c) 2004 Freescale Semiconductor, Inc.
7 * Copyright (c) 2006, 2007 Maciej W. Rozycki
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/errno.h>
21 #include <linux/unistd.h>
22 #include <linux/interrupt.h>
23 #include <linux/delay.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/skbuff.h>
28 #include <linux/module.h>
29 #include <linux/mii.h>
30 #include <linux/ethtool.h>
31 #include <linux/phy.h>
32 #include <linux/phy_led_triggers.h>
33 #include <linux/workqueue.h>
34 #include <linux/mdio.h>
36 #include <linux/uaccess.h>
37 #include <linux/atomic.h>
41 #define PHY_STATE_STR(_state) \
43 return __stringify(_state); \
45 static const char *phy_state_to_str(enum phy_state st)
49 PHY_STATE_STR(STARTING)
51 PHY_STATE_STR(PENDING)
54 PHY_STATE_STR(RUNNING)
56 PHY_STATE_STR(FORCING)
57 PHY_STATE_STR(CHANGELINK)
59 PHY_STATE_STR(RESUMING)
67 * phy_print_status - Convenience function to print out the current phy status
68 * @phydev: the phy_device struct
70 void phy_print_status(struct phy_device *phydev)
73 netdev_info(phydev->attached_dev,
74 "Link is Up - %s/%s - flow control %s\n",
75 phy_speed_to_str(phydev->speed),
76 phy_duplex_to_str(phydev->duplex),
77 phydev->pause ? "rx/tx" : "off");
79 netdev_info(phydev->attached_dev, "Link is Down\n");
82 EXPORT_SYMBOL(phy_print_status);
85 * phy_clear_interrupt - Ack the phy device's interrupt
86 * @phydev: the phy_device struct
88 * If the @phydev driver has an ack_interrupt function, call it to
89 * ack and clear the phy device's interrupt.
91 * Returns 0 on success or < 0 on error.
93 static int phy_clear_interrupt(struct phy_device *phydev)
95 if (phydev->drv->ack_interrupt)
96 return phydev->drv->ack_interrupt(phydev);
102 * phy_config_interrupt - configure the PHY device for the requested interrupts
103 * @phydev: the phy_device struct
104 * @interrupts: interrupt flags to configure for this @phydev
106 * Returns 0 on success or < 0 on error.
108 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
110 phydev->interrupts = interrupts;
111 if (phydev->drv->config_intr)
112 return phydev->drv->config_intr(phydev);
118 * phy_restart_aneg - restart auto-negotiation
119 * @phydev: target phy_device struct
121 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
122 * negative errno on error.
124 int phy_restart_aneg(struct phy_device *phydev)
128 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
129 ret = genphy_c45_restart_aneg(phydev);
131 ret = genphy_restart_aneg(phydev);
135 EXPORT_SYMBOL_GPL(phy_restart_aneg);
138 * phy_aneg_done - return auto-negotiation status
139 * @phydev: target phy_device struct
141 * Description: Return the auto-negotiation status from this @phydev
142 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
145 int phy_aneg_done(struct phy_device *phydev)
147 if (phydev->drv && phydev->drv->aneg_done)
148 return phydev->drv->aneg_done(phydev);
150 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
151 * implement Clause 22 registers
153 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
156 return genphy_aneg_done(phydev);
158 EXPORT_SYMBOL(phy_aneg_done);
160 /* A structure for mapping a particular speed and duplex
161 * combination to a particular SUPPORTED and ADVERTISED value
169 /* A mapping of all SUPPORTED settings to speed/duplex. This table
170 * must be grouped by speed and sorted in descending match priority
171 * - iow, descending speed. */
172 static const struct phy_setting settings[] = {
174 .speed = SPEED_10000,
175 .duplex = DUPLEX_FULL,
176 .bit = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
179 .speed = SPEED_10000,
180 .duplex = DUPLEX_FULL,
181 .bit = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
184 .speed = SPEED_10000,
185 .duplex = DUPLEX_FULL,
186 .bit = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
190 .duplex = DUPLEX_FULL,
191 .bit = ETHTOOL_LINK_MODE_2500baseX_Full_BIT,
195 .duplex = DUPLEX_FULL,
196 .bit = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
200 .duplex = DUPLEX_FULL,
201 .bit = ETHTOOL_LINK_MODE_1000baseT_Full_BIT,
205 .duplex = DUPLEX_HALF,
206 .bit = ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
210 .duplex = DUPLEX_FULL,
211 .bit = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
215 .duplex = DUPLEX_HALF,
216 .bit = ETHTOOL_LINK_MODE_100baseT_Half_BIT,
220 .duplex = DUPLEX_FULL,
221 .bit = ETHTOOL_LINK_MODE_10baseT_Full_BIT,
225 .duplex = DUPLEX_HALF,
226 .bit = ETHTOOL_LINK_MODE_10baseT_Half_BIT,
231 * phy_lookup_setting - lookup a PHY setting
232 * @speed: speed to match
233 * @duplex: duplex to match
234 * @mask: allowed link modes
235 * @maxbit: bit size of link modes
236 * @exact: an exact match is required
238 * Search the settings array for a setting that matches the speed and
239 * duplex, and which is supported.
241 * If @exact is unset, either an exact match or %NULL for no match will
244 * If @exact is set, an exact match, the fastest supported setting at
245 * or below the specified speed, the slowest supported setting, or if
246 * they all fail, %NULL will be returned.
248 static const struct phy_setting *
249 phy_lookup_setting(int speed, int duplex, const unsigned long *mask,
250 size_t maxbit, bool exact)
252 const struct phy_setting *p, *match = NULL, *last = NULL;
255 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
256 if (p->bit < maxbit && test_bit(p->bit, mask)) {
258 if (p->speed == speed && p->duplex == duplex) {
259 /* Exact match for speed and duplex */
263 if (!match && p->speed <= speed)
267 if (p->speed < speed)
273 if (!match && !exact)
280 * phy_find_valid - find a PHY setting that matches the requested parameters
281 * @speed: desired speed
282 * @duplex: desired duplex
283 * @supported: mask of supported link modes
285 * Locate a supported phy setting that is, in priority order:
286 * - an exact match for the specified speed and duplex mode
287 * - a match for the specified speed, or slower speed
288 * - the slowest supported speed
289 * Returns the matched phy_setting entry, or %NULL if no supported phy
290 * settings were found.
292 static const struct phy_setting *
293 phy_find_valid(int speed, int duplex, u32 supported)
295 unsigned long mask = supported;
297 return phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, false);
301 * phy_supported_speeds - return all speeds currently supported by a phy device
302 * @phy: The phy device to return supported speeds of.
303 * @speeds: buffer to store supported speeds in.
304 * @size: size of speeds buffer.
306 * Description: Returns the number of supported speeds, and fills the speeds
307 * buffer with the supported speeds. If speeds buffer is too small to contain
308 * all currently supported speeds, will return as many speeds as can fit.
310 unsigned int phy_supported_speeds(struct phy_device *phy,
311 unsigned int *speeds,
314 unsigned long supported = phy->supported;
315 unsigned int count = 0;
316 unsigned int idx = 0;
318 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++)
319 /* Assumes settings are grouped by speed */
320 if (settings[idx].bit < BITS_PER_LONG &&
321 !test_bit(settings[idx].bit, &supported) &&
322 (count == 0 || speeds[count - 1] != settings[idx].speed))
323 speeds[count++] = settings[idx].speed;
329 * phy_check_valid - check if there is a valid PHY setting which matches
330 * speed, duplex, and feature mask
331 * @speed: speed to match
332 * @duplex: duplex to match
333 * @features: A mask of the valid settings
335 * Description: Returns true if there is a valid setting, false otherwise.
337 static inline bool phy_check_valid(int speed, int duplex, u32 features)
339 unsigned long mask = features;
341 return !!phy_lookup_setting(speed, duplex, &mask, BITS_PER_LONG, true);
345 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
346 * @phydev: the target phy_device struct
348 * Description: Make sure the PHY is set to supported speeds and
349 * duplexes. Drop down by one in this order: 1000/FULL,
350 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
352 static void phy_sanitize_settings(struct phy_device *phydev)
354 const struct phy_setting *setting;
355 u32 features = phydev->supported;
357 /* Sanitize settings based on PHY capabilities */
358 if ((features & SUPPORTED_Autoneg) == 0)
359 phydev->autoneg = AUTONEG_DISABLE;
361 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
363 phydev->speed = setting->speed;
364 phydev->duplex = setting->duplex;
366 /* We failed to find anything (no supported speeds?) */
367 phydev->speed = SPEED_UNKNOWN;
368 phydev->duplex = DUPLEX_UNKNOWN;
373 * phy_ethtool_sset - generic ethtool sset function, handles all the details
374 * @phydev: target phy_device struct
377 * A few notes about parameter checking:
379 * - We don't set port or transceiver, so we don't care what they
381 * - phy_start_aneg() will make sure forced settings are sane, and
382 * choose the next best ones from the ones selected, so we don't
383 * care if ethtool tries to give us bad values.
385 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
387 u32 speed = ethtool_cmd_speed(cmd);
389 if (cmd->phy_address != phydev->mdio.addr)
392 /* We make sure that we don't pass unsupported values in to the PHY */
393 cmd->advertising &= phydev->supported;
395 /* Verify the settings we care about. */
396 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
399 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
402 if (cmd->autoneg == AUTONEG_DISABLE &&
403 ((speed != SPEED_1000 &&
404 speed != SPEED_100 &&
405 speed != SPEED_10) ||
406 (cmd->duplex != DUPLEX_HALF &&
407 cmd->duplex != DUPLEX_FULL)))
410 phydev->autoneg = cmd->autoneg;
412 phydev->speed = speed;
414 phydev->advertising = cmd->advertising;
416 if (AUTONEG_ENABLE == cmd->autoneg)
417 phydev->advertising |= ADVERTISED_Autoneg;
419 phydev->advertising &= ~ADVERTISED_Autoneg;
421 phydev->duplex = cmd->duplex;
423 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
425 /* Restart the PHY */
426 phy_start_aneg(phydev);
430 EXPORT_SYMBOL(phy_ethtool_sset);
432 int phy_ethtool_ksettings_set(struct phy_device *phydev,
433 const struct ethtool_link_ksettings *cmd)
435 u8 autoneg = cmd->base.autoneg;
436 u8 duplex = cmd->base.duplex;
437 u32 speed = cmd->base.speed;
440 if (cmd->base.phy_address != phydev->mdio.addr)
443 ethtool_convert_link_mode_to_legacy_u32(&advertising,
444 cmd->link_modes.advertising);
446 /* We make sure that we don't pass unsupported values in to the PHY */
447 advertising &= phydev->supported;
449 /* Verify the settings we care about. */
450 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
453 if (autoneg == AUTONEG_ENABLE && advertising == 0)
456 if (autoneg == AUTONEG_DISABLE &&
457 ((speed != SPEED_1000 &&
458 speed != SPEED_100 &&
459 speed != SPEED_10) ||
460 (duplex != DUPLEX_HALF &&
461 duplex != DUPLEX_FULL)))
464 phydev->autoneg = autoneg;
466 phydev->speed = speed;
468 phydev->advertising = advertising;
470 if (autoneg == AUTONEG_ENABLE)
471 phydev->advertising |= ADVERTISED_Autoneg;
473 phydev->advertising &= ~ADVERTISED_Autoneg;
475 phydev->duplex = duplex;
477 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
479 /* Restart the PHY */
480 phy_start_aneg(phydev);
484 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
486 void phy_ethtool_ksettings_get(struct phy_device *phydev,
487 struct ethtool_link_ksettings *cmd)
489 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
492 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
493 phydev->advertising);
495 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
496 phydev->lp_advertising);
498 cmd->base.speed = phydev->speed;
499 cmd->base.duplex = phydev->duplex;
500 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
501 cmd->base.port = PORT_BNC;
503 cmd->base.port = PORT_MII;
505 cmd->base.phy_address = phydev->mdio.addr;
506 cmd->base.autoneg = phydev->autoneg;
507 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
508 cmd->base.eth_tp_mdix = phydev->mdix;
510 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
513 * phy_mii_ioctl - generic PHY MII ioctl interface
514 * @phydev: the phy_device struct
515 * @ifr: &struct ifreq for socket ioctl's
516 * @cmd: ioctl cmd to execute
518 * Note that this function is currently incompatible with the
519 * PHYCONTROL layer. It changes registers without regard to
520 * current state. Use at own risk.
522 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
524 struct mii_ioctl_data *mii_data = if_mii(ifr);
525 u16 val = mii_data->val_in;
526 bool change_autoneg = false;
530 mii_data->phy_id = phydev->mdio.addr;
534 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
540 if (mii_data->phy_id == phydev->mdio.addr) {
541 switch (mii_data->reg_num) {
543 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
544 if (phydev->autoneg == AUTONEG_ENABLE)
545 change_autoneg = true;
546 phydev->autoneg = AUTONEG_DISABLE;
547 if (val & BMCR_FULLDPLX)
548 phydev->duplex = DUPLEX_FULL;
550 phydev->duplex = DUPLEX_HALF;
551 if (val & BMCR_SPEED1000)
552 phydev->speed = SPEED_1000;
553 else if (val & BMCR_SPEED100)
554 phydev->speed = SPEED_100;
555 else phydev->speed = SPEED_10;
558 if (phydev->autoneg == AUTONEG_DISABLE)
559 change_autoneg = true;
560 phydev->autoneg = AUTONEG_ENABLE;
564 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
565 change_autoneg = true;
573 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
574 mii_data->reg_num, val);
576 if (mii_data->phy_id == phydev->mdio.addr &&
577 mii_data->reg_num == MII_BMCR &&
579 return phy_init_hw(phydev);
582 return phy_start_aneg(phydev);
587 if (phydev->drv && phydev->drv->hwtstamp)
588 return phydev->drv->hwtstamp(phydev, ifr);
595 EXPORT_SYMBOL(phy_mii_ioctl);
598 * phy_start_aneg_priv - start auto-negotiation for this PHY device
599 * @phydev: the phy_device struct
600 * @sync: indicate whether we should wait for the workqueue cancelation
602 * Description: Sanitizes the settings (if we're not autonegotiating
603 * them), and then calls the driver's config_aneg function.
604 * If the PHYCONTROL Layer is operating, we change the state to
605 * reflect the beginning of Auto-negotiation or forcing.
607 static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
615 mutex_lock(&phydev->lock);
617 if (AUTONEG_DISABLE == phydev->autoneg)
618 phy_sanitize_settings(phydev);
620 /* Invalidate LP advertising flags */
621 phydev->lp_advertising = 0;
623 err = phydev->drv->config_aneg(phydev);
627 if (phydev->state != PHY_HALTED) {
628 if (AUTONEG_ENABLE == phydev->autoneg) {
629 phydev->state = PHY_AN;
630 phydev->link_timeout = PHY_AN_TIMEOUT;
632 phydev->state = PHY_FORCING;
633 phydev->link_timeout = PHY_FORCE_TIMEOUT;
637 /* Re-schedule a PHY state machine to check PHY status because
638 * negotiation may already be done and aneg interrupt may not be
641 if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
642 err = phy_aneg_done(phydev);
650 mutex_unlock(&phydev->lock);
653 phy_trigger_machine(phydev, sync);
659 * phy_start_aneg - start auto-negotiation for this PHY device
660 * @phydev: the phy_device struct
662 * Description: Sanitizes the settings (if we're not autonegotiating
663 * them), and then calls the driver's config_aneg function.
664 * If the PHYCONTROL Layer is operating, we change the state to
665 * reflect the beginning of Auto-negotiation or forcing.
667 int phy_start_aneg(struct phy_device *phydev)
669 return phy_start_aneg_priv(phydev, true);
671 EXPORT_SYMBOL(phy_start_aneg);
674 * phy_start_machine - start PHY state machine tracking
675 * @phydev: the phy_device struct
677 * Description: The PHY infrastructure can run a state machine
678 * which tracks whether the PHY is starting up, negotiating,
679 * etc. This function starts the delayed workqueue which tracks
680 * the state of the PHY. If you want to maintain your own state machine,
681 * do not call this function.
683 void phy_start_machine(struct phy_device *phydev)
685 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
689 * phy_trigger_machine - trigger the state machine to run
691 * @phydev: the phy_device struct
692 * @sync: indicate whether we should wait for the workqueue cancelation
694 * Description: There has been a change in state which requires that the
695 * state machine runs.
698 void phy_trigger_machine(struct phy_device *phydev, bool sync)
701 cancel_delayed_work_sync(&phydev->state_queue);
703 cancel_delayed_work(&phydev->state_queue);
704 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
708 * phy_stop_machine - stop the PHY state machine tracking
709 * @phydev: target phy_device struct
711 * Description: Stops the state machine delayed workqueue, sets the
712 * state to UP (unless it wasn't up yet). This function must be
713 * called BEFORE phy_detach.
715 void phy_stop_machine(struct phy_device *phydev)
717 cancel_delayed_work_sync(&phydev->state_queue);
719 mutex_lock(&phydev->lock);
720 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
721 phydev->state = PHY_UP;
722 mutex_unlock(&phydev->lock);
724 /* Now we can run the state machine synchronously */
725 phy_state_machine(&phydev->state_queue.work);
729 * phy_error - enter HALTED state for this PHY device
730 * @phydev: target phy_device struct
732 * Moves the PHY to the HALTED state in response to a read
733 * or write error, and tells the controller the link is down.
734 * Must not be called from interrupt context, or while the
735 * phydev->lock is held.
737 static void phy_error(struct phy_device *phydev)
739 mutex_lock(&phydev->lock);
740 phydev->state = PHY_HALTED;
741 mutex_unlock(&phydev->lock);
743 phy_trigger_machine(phydev, false);
747 * phy_interrupt - PHY interrupt handler
748 * @irq: interrupt line
749 * @phy_dat: phy_device pointer
751 * Description: When a PHY interrupt occurs, the handler disables
752 * interrupts, and uses phy_change to handle the interrupt.
754 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
756 struct phy_device *phydev = phy_dat;
758 if (PHY_HALTED == phydev->state)
759 return IRQ_NONE; /* It can't be ours. */
761 disable_irq_nosync(irq);
762 atomic_inc(&phydev->irq_disable);
770 * phy_enable_interrupts - Enable the interrupts from the PHY side
771 * @phydev: target phy_device struct
773 static int phy_enable_interrupts(struct phy_device *phydev)
775 int err = phy_clear_interrupt(phydev);
780 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
784 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
785 * @phydev: target phy_device struct
787 static int phy_disable_interrupts(struct phy_device *phydev)
791 /* Disable PHY interrupts */
792 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
796 /* Clear the interrupt */
797 err = phy_clear_interrupt(phydev);
810 * phy_start_interrupts - request and enable interrupts for a PHY device
811 * @phydev: target phy_device struct
813 * Description: Request the interrupt for the given PHY.
814 * If this fails, then we set irq to PHY_POLL.
815 * Otherwise, we enable the interrupts in the PHY.
816 * This should only be called with a valid IRQ number.
817 * Returns 0 on success or < 0 on error.
819 int phy_start_interrupts(struct phy_device *phydev)
821 atomic_set(&phydev->irq_disable, 0);
822 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
823 IRQF_ONESHOT | IRQF_SHARED,
824 phydev_name(phydev), phydev) < 0) {
825 pr_warn("%s: Can't get IRQ %d (PHY)\n",
826 phydev->mdio.bus->name, phydev->irq);
827 phydev->irq = PHY_POLL;
831 return phy_enable_interrupts(phydev);
833 EXPORT_SYMBOL(phy_start_interrupts);
836 * phy_stop_interrupts - disable interrupts from a PHY device
837 * @phydev: target phy_device struct
839 int phy_stop_interrupts(struct phy_device *phydev)
841 int err = phy_disable_interrupts(phydev);
846 free_irq(phydev->irq, phydev);
848 /* If work indeed has been cancelled, disable_irq() will have
849 * been left unbalanced from phy_interrupt() and enable_irq()
850 * has to be called so that other devices on the line work.
852 while (atomic_dec_return(&phydev->irq_disable) >= 0)
853 enable_irq(phydev->irq);
857 EXPORT_SYMBOL(phy_stop_interrupts);
860 * phy_change - Called by the phy_interrupt to handle PHY changes
861 * @phydev: phy_device struct that interrupted
863 void phy_change(struct phy_device *phydev)
865 if (phy_interrupt_is_valid(phydev)) {
866 if (phydev->drv->did_interrupt &&
867 !phydev->drv->did_interrupt(phydev))
870 if (phy_disable_interrupts(phydev))
874 mutex_lock(&phydev->lock);
875 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
876 phydev->state = PHY_CHANGELINK;
877 mutex_unlock(&phydev->lock);
879 if (phy_interrupt_is_valid(phydev)) {
880 atomic_dec(&phydev->irq_disable);
881 enable_irq(phydev->irq);
883 /* Reenable interrupts */
884 if (PHY_HALTED != phydev->state &&
885 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
889 /* reschedule state queue work to run as soon as possible */
890 phy_trigger_machine(phydev, true);
894 atomic_dec(&phydev->irq_disable);
895 enable_irq(phydev->irq);
899 disable_irq(phydev->irq);
900 atomic_inc(&phydev->irq_disable);
906 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
907 * @work: work_struct that describes the work to be done
909 void phy_change_work(struct work_struct *work)
911 struct phy_device *phydev =
912 container_of(work, struct phy_device, phy_queue);
918 * phy_stop - Bring down the PHY link, and stop checking the status
919 * @phydev: target phy_device struct
921 void phy_stop(struct phy_device *phydev)
923 mutex_lock(&phydev->lock);
925 if (PHY_HALTED == phydev->state)
928 if (phy_interrupt_is_valid(phydev)) {
929 /* Disable PHY Interrupts */
930 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
932 /* Clear any pending interrupts */
933 phy_clear_interrupt(phydev);
936 phydev->state = PHY_HALTED;
939 mutex_unlock(&phydev->lock);
941 /* Cannot call flush_scheduled_work() here as desired because
942 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
943 * will not reenable interrupts.
946 EXPORT_SYMBOL(phy_stop);
949 * phy_start - start or restart a PHY device
950 * @phydev: target phy_device struct
952 * Description: Indicates the attached device's readiness to
953 * handle PHY-related work. Used during startup to start the
954 * PHY, and after a call to phy_stop() to resume operation.
955 * Also used to indicate the MDIO bus has cleared an error
958 void phy_start(struct phy_device *phydev)
960 bool do_resume = false;
963 mutex_lock(&phydev->lock);
965 switch (phydev->state) {
967 phydev->state = PHY_PENDING;
970 phydev->state = PHY_UP;
973 /* make sure interrupts are re-enabled for the PHY */
974 if (phydev->irq != PHY_POLL) {
975 err = phy_enable_interrupts(phydev);
980 phydev->state = PHY_RESUMING;
986 mutex_unlock(&phydev->lock);
988 /* if phy was suspended, bring the physical link up again */
992 phy_trigger_machine(phydev, true);
994 EXPORT_SYMBOL(phy_start);
996 static void phy_adjust_link(struct phy_device *phydev)
998 phydev->adjust_link(phydev->attached_dev);
999 phy_led_trigger_change_speed(phydev);
1003 * phy_state_machine - Handle the state machine
1004 * @work: work_struct that describes the work to be done
1006 void phy_state_machine(struct work_struct *work)
1008 struct delayed_work *dwork = to_delayed_work(work);
1009 struct phy_device *phydev =
1010 container_of(dwork, struct phy_device, state_queue);
1011 bool needs_aneg = false, do_suspend = false;
1012 enum phy_state old_state;
1016 mutex_lock(&phydev->lock);
1018 old_state = phydev->state;
1020 if (phydev->drv && phydev->drv->link_change_notify)
1021 phydev->drv->link_change_notify(phydev);
1023 switch (phydev->state) {
1032 phydev->link_timeout = PHY_AN_TIMEOUT;
1036 err = phy_read_status(phydev);
1040 /* If the link is down, give up on negotiation for now */
1041 if (!phydev->link) {
1042 phydev->state = PHY_NOLINK;
1043 netif_carrier_off(phydev->attached_dev);
1044 phy_adjust_link(phydev);
1048 /* Check if negotiation is done. Break if there's an error */
1049 err = phy_aneg_done(phydev);
1053 /* If AN is done, we're running */
1055 phydev->state = PHY_RUNNING;
1056 netif_carrier_on(phydev->attached_dev);
1057 phy_adjust_link(phydev);
1059 } else if (0 == phydev->link_timeout--)
1063 if (phy_interrupt_is_valid(phydev))
1066 err = phy_read_status(phydev);
1071 if (AUTONEG_ENABLE == phydev->autoneg) {
1072 err = phy_aneg_done(phydev);
1077 phydev->state = PHY_AN;
1078 phydev->link_timeout = PHY_AN_TIMEOUT;
1082 phydev->state = PHY_RUNNING;
1083 netif_carrier_on(phydev->attached_dev);
1084 phy_adjust_link(phydev);
1088 err = genphy_update_link(phydev);
1093 phydev->state = PHY_RUNNING;
1094 netif_carrier_on(phydev->attached_dev);
1096 if (0 == phydev->link_timeout--)
1100 phy_adjust_link(phydev);
1103 /* Only register a CHANGE if we are polling and link changed
1104 * since latest checking.
1106 if (phydev->irq == PHY_POLL) {
1107 old_link = phydev->link;
1108 err = phy_read_status(phydev);
1112 if (old_link != phydev->link)
1113 phydev->state = PHY_CHANGELINK;
1116 * Failsafe: check that nobody set phydev->link=0 between two
1117 * poll cycles, otherwise we won't leave RUNNING state as long
1118 * as link remains down.
1120 if (!phydev->link && phydev->state == PHY_RUNNING) {
1121 phydev->state = PHY_CHANGELINK;
1122 phydev_err(phydev, "no link in PHY_RUNNING\n");
1125 case PHY_CHANGELINK:
1126 err = phy_read_status(phydev);
1131 phydev->state = PHY_RUNNING;
1132 netif_carrier_on(phydev->attached_dev);
1134 phydev->state = PHY_NOLINK;
1135 netif_carrier_off(phydev->attached_dev);
1138 phy_adjust_link(phydev);
1140 if (phy_interrupt_is_valid(phydev))
1141 err = phy_config_interrupt(phydev,
1142 PHY_INTERRUPT_ENABLED);
1147 netif_carrier_off(phydev->attached_dev);
1148 phy_adjust_link(phydev);
1153 if (AUTONEG_ENABLE == phydev->autoneg) {
1154 err = phy_aneg_done(phydev);
1158 /* err > 0 if AN is done.
1159 * Otherwise, it's 0, and we're still waiting for AN
1162 err = phy_read_status(phydev);
1167 phydev->state = PHY_RUNNING;
1168 netif_carrier_on(phydev->attached_dev);
1170 phydev->state = PHY_NOLINK;
1172 phy_adjust_link(phydev);
1174 phydev->state = PHY_AN;
1175 phydev->link_timeout = PHY_AN_TIMEOUT;
1178 err = phy_read_status(phydev);
1183 phydev->state = PHY_RUNNING;
1184 netif_carrier_on(phydev->attached_dev);
1186 phydev->state = PHY_NOLINK;
1188 phy_adjust_link(phydev);
1193 mutex_unlock(&phydev->lock);
1196 err = phy_start_aneg_priv(phydev, false);
1197 else if (do_suspend)
1198 phy_suspend(phydev);
1203 if (old_state != phydev->state)
1204 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1205 phy_state_to_str(old_state),
1206 phy_state_to_str(phydev->state));
1208 /* Only re-schedule a PHY state machine change if we are polling the
1209 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1210 * between states from phy_mac_interrupt()
1212 if (phydev->irq == PHY_POLL)
1213 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1214 PHY_STATE_TIME * HZ);
1218 * phy_mac_interrupt - MAC says the link has changed
1219 * @phydev: phy_device struct with changed link
1220 * @new_link: Link is Up/Down.
1222 * Description: The MAC layer is able indicate there has been a change
1223 * in the PHY link status. Set the new link status, and trigger the
1224 * state machine, work a work queue.
1226 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1228 phydev->link = new_link;
1230 /* Trigger a state machine change */
1231 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1233 EXPORT_SYMBOL(phy_mac_interrupt);
1236 * phy_init_eee - init and check the EEE feature
1237 * @phydev: target phy_device struct
1238 * @clk_stop_enable: PHY may stop the clock during LPI
1240 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1241 * is supported by looking at the MMD registers 3.20 and 7.60/61
1242 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1245 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1250 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1252 if (phydev->duplex == DUPLEX_FULL) {
1253 int eee_lp, eee_cap, eee_adv;
1257 /* Read phy status to properly get the right settings */
1258 status = phy_read_status(phydev);
1262 /* First check if the EEE ability is supported */
1263 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1267 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1271 /* Check which link settings negotiated and verify it in
1272 * the EEE advertising registers.
1274 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1278 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1282 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1283 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1284 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1287 if (clk_stop_enable) {
1288 /* Configure the PHY to stop receiving xMII
1289 * clock while it is signaling LPI.
1291 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1295 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1296 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1299 return 0; /* EEE supported */
1302 return -EPROTONOSUPPORT;
1304 EXPORT_SYMBOL(phy_init_eee);
1307 * phy_get_eee_err - report the EEE wake error count
1308 * @phydev: target phy_device struct
1310 * Description: it is to report the number of time where the PHY
1311 * failed to complete its normal wake sequence.
1313 int phy_get_eee_err(struct phy_device *phydev)
1318 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1320 EXPORT_SYMBOL(phy_get_eee_err);
1323 * phy_ethtool_get_eee - get EEE supported and status
1324 * @phydev: target phy_device struct
1325 * @data: ethtool_eee data
1327 * Description: it reportes the Supported/Advertisement/LP Advertisement
1330 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1337 /* Get Supported EEE */
1338 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1341 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1343 /* Get advertisement EEE */
1344 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1347 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1349 /* Get LP advertisement EEE */
1350 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1353 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1357 EXPORT_SYMBOL(phy_ethtool_get_eee);
1360 * phy_ethtool_set_eee - set EEE supported and status
1361 * @phydev: target phy_device struct
1362 * @data: ethtool_eee data
1364 * Description: it is to program the Advertisement EEE register.
1366 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1368 int cap, old_adv, adv, ret;
1373 /* Get Supported EEE */
1374 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1378 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1382 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1384 /* Mask prohibited EEE modes */
1385 adv &= ~phydev->eee_broken_modes;
1387 if (old_adv != adv) {
1388 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1392 /* Restart autonegotiation so the new modes get sent to the
1395 ret = phy_restart_aneg(phydev);
1402 EXPORT_SYMBOL(phy_ethtool_set_eee);
1404 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1406 if (phydev->drv && phydev->drv->set_wol)
1407 return phydev->drv->set_wol(phydev, wol);
1411 EXPORT_SYMBOL(phy_ethtool_set_wol);
1413 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1415 if (phydev->drv && phydev->drv->get_wol)
1416 phydev->drv->get_wol(phydev, wol);
1418 EXPORT_SYMBOL(phy_ethtool_get_wol);
1420 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1421 struct ethtool_link_ksettings *cmd)
1423 struct phy_device *phydev = ndev->phydev;
1428 phy_ethtool_ksettings_get(phydev, cmd);
1432 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1434 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1435 const struct ethtool_link_ksettings *cmd)
1437 struct phy_device *phydev = ndev->phydev;
1442 return phy_ethtool_ksettings_set(phydev, cmd);
1444 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1446 int phy_ethtool_nway_reset(struct net_device *ndev)
1448 struct phy_device *phydev = ndev->phydev;
1456 return phy_restart_aneg(phydev);
1458 EXPORT_SYMBOL(phy_ethtool_nway_reset);