Merge tag 'chrome-platform-for-linus-4.13' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / net / phy / phy.c
blobd0626bf5c540911b0d15bdbab1b960145b6d124c
1 /* Framework for configuring and reading PHY devices
2 * Based on code in sungem_phy.c and gianfar_phy.c
4 * Author: Andy Fleming
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>
27 #include <linux/mm.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/timer.h>
34 #include <linux/workqueue.h>
35 #include <linux/mdio.h>
36 #include <linux/io.h>
37 #include <linux/uaccess.h>
38 #include <linux/atomic.h>
40 #include <asm/irq.h>
42 static const char *phy_speed_to_str(int speed)
44 switch (speed) {
45 case SPEED_10:
46 return "10Mbps";
47 case SPEED_100:
48 return "100Mbps";
49 case SPEED_1000:
50 return "1Gbps";
51 case SPEED_2500:
52 return "2.5Gbps";
53 case SPEED_5000:
54 return "5Gbps";
55 case SPEED_10000:
56 return "10Gbps";
57 case SPEED_14000:
58 return "14Gbps";
59 case SPEED_20000:
60 return "20Gbps";
61 case SPEED_25000:
62 return "25Gbps";
63 case SPEED_40000:
64 return "40Gbps";
65 case SPEED_50000:
66 return "50Gbps";
67 case SPEED_56000:
68 return "56Gbps";
69 case SPEED_100000:
70 return "100Gbps";
71 case SPEED_UNKNOWN:
72 return "Unknown";
73 default:
74 return "Unsupported (update phy.c)";
78 #define PHY_STATE_STR(_state) \
79 case PHY_##_state: \
80 return __stringify(_state); \
82 static const char *phy_state_to_str(enum phy_state st)
84 switch (st) {
85 PHY_STATE_STR(DOWN)
86 PHY_STATE_STR(STARTING)
87 PHY_STATE_STR(READY)
88 PHY_STATE_STR(PENDING)
89 PHY_STATE_STR(UP)
90 PHY_STATE_STR(AN)
91 PHY_STATE_STR(RUNNING)
92 PHY_STATE_STR(NOLINK)
93 PHY_STATE_STR(FORCING)
94 PHY_STATE_STR(CHANGELINK)
95 PHY_STATE_STR(HALTED)
96 PHY_STATE_STR(RESUMING)
99 return NULL;
104 * phy_print_status - Convenience function to print out the current phy status
105 * @phydev: the phy_device struct
107 void phy_print_status(struct phy_device *phydev)
109 if (phydev->link) {
110 netdev_info(phydev->attached_dev,
111 "Link is Up - %s/%s - flow control %s\n",
112 phy_speed_to_str(phydev->speed),
113 DUPLEX_FULL == phydev->duplex ? "Full" : "Half",
114 phydev->pause ? "rx/tx" : "off");
115 } else {
116 netdev_info(phydev->attached_dev, "Link is Down\n");
119 EXPORT_SYMBOL(phy_print_status);
122 * phy_clear_interrupt - Ack the phy device's interrupt
123 * @phydev: the phy_device struct
125 * If the @phydev driver has an ack_interrupt function, call it to
126 * ack and clear the phy device's interrupt.
128 * Returns 0 on success or < 0 on error.
130 static int phy_clear_interrupt(struct phy_device *phydev)
132 if (phydev->drv->ack_interrupt)
133 return phydev->drv->ack_interrupt(phydev);
135 return 0;
139 * phy_config_interrupt - configure the PHY device for the requested interrupts
140 * @phydev: the phy_device struct
141 * @interrupts: interrupt flags to configure for this @phydev
143 * Returns 0 on success or < 0 on error.
145 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
147 phydev->interrupts = interrupts;
148 if (phydev->drv->config_intr)
149 return phydev->drv->config_intr(phydev);
151 return 0;
155 * phy_restart_aneg - restart auto-negotiation
156 * @phydev: target phy_device struct
158 * Restart the autonegotiation on @phydev. Returns >= 0 on success or
159 * negative errno on error.
161 int phy_restart_aneg(struct phy_device *phydev)
163 int ret;
165 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
166 ret = genphy_c45_restart_aneg(phydev);
167 else
168 ret = genphy_restart_aneg(phydev);
170 return ret;
172 EXPORT_SYMBOL_GPL(phy_restart_aneg);
175 * phy_aneg_done - return auto-negotiation status
176 * @phydev: target phy_device struct
178 * Description: Return the auto-negotiation status from this @phydev
179 * Returns > 0 on success or < 0 on error. 0 means that auto-negotiation
180 * is still pending.
182 int phy_aneg_done(struct phy_device *phydev)
184 if (phydev->drv && phydev->drv->aneg_done)
185 return phydev->drv->aneg_done(phydev);
187 /* Avoid genphy_aneg_done() if the Clause 45 PHY does not
188 * implement Clause 22 registers
190 if (phydev->is_c45 && !(phydev->c45_ids.devices_in_package & BIT(0)))
191 return -EINVAL;
193 return genphy_aneg_done(phydev);
195 EXPORT_SYMBOL(phy_aneg_done);
197 /* A structure for mapping a particular speed and duplex
198 * combination to a particular SUPPORTED and ADVERTISED value
200 struct phy_setting {
201 int speed;
202 int duplex;
203 u32 setting;
206 /* A mapping of all SUPPORTED settings to speed/duplex. This table
207 * must be grouped by speed and sorted in descending match priority
208 * - iow, descending speed. */
209 static const struct phy_setting settings[] = {
211 .speed = SPEED_10000,
212 .duplex = DUPLEX_FULL,
213 .setting = SUPPORTED_10000baseKR_Full,
216 .speed = SPEED_10000,
217 .duplex = DUPLEX_FULL,
218 .setting = SUPPORTED_10000baseKX4_Full,
221 .speed = SPEED_10000,
222 .duplex = DUPLEX_FULL,
223 .setting = SUPPORTED_10000baseT_Full,
226 .speed = SPEED_2500,
227 .duplex = DUPLEX_FULL,
228 .setting = SUPPORTED_2500baseX_Full,
231 .speed = SPEED_1000,
232 .duplex = DUPLEX_FULL,
233 .setting = SUPPORTED_1000baseKX_Full,
236 .speed = SPEED_1000,
237 .duplex = DUPLEX_FULL,
238 .setting = SUPPORTED_1000baseT_Full,
241 .speed = SPEED_1000,
242 .duplex = DUPLEX_HALF,
243 .setting = SUPPORTED_1000baseT_Half,
246 .speed = SPEED_100,
247 .duplex = DUPLEX_FULL,
248 .setting = SUPPORTED_100baseT_Full,
251 .speed = SPEED_100,
252 .duplex = DUPLEX_HALF,
253 .setting = SUPPORTED_100baseT_Half,
256 .speed = SPEED_10,
257 .duplex = DUPLEX_FULL,
258 .setting = SUPPORTED_10baseT_Full,
261 .speed = SPEED_10,
262 .duplex = DUPLEX_HALF,
263 .setting = SUPPORTED_10baseT_Half,
268 * phy_lookup_setting - lookup a PHY setting
269 * @speed: speed to match
270 * @duplex: duplex to match
271 * @features: allowed link modes
272 * @exact: an exact match is required
274 * Search the settings array for a setting that matches the speed and
275 * duplex, and which is supported.
277 * If @exact is unset, either an exact match or %NULL for no match will
278 * be returned.
280 * If @exact is set, an exact match, the fastest supported setting at
281 * or below the specified speed, the slowest supported setting, or if
282 * they all fail, %NULL will be returned.
284 static const struct phy_setting *
285 phy_lookup_setting(int speed, int duplex, u32 features, bool exact)
287 const struct phy_setting *p, *match = NULL, *last = NULL;
288 int i;
290 for (i = 0, p = settings; i < ARRAY_SIZE(settings); i++, p++) {
291 if (p->setting & features) {
292 last = p;
293 if (p->speed == speed && p->duplex == duplex) {
294 /* Exact match for speed and duplex */
295 match = p;
296 break;
297 } else if (!exact) {
298 if (!match && p->speed <= speed)
299 /* Candidate */
300 match = p;
302 if (p->speed < speed)
303 break;
308 if (!match && !exact)
309 match = last;
311 return match;
315 * phy_find_valid - find a PHY setting that matches the requested parameters
316 * @speed: desired speed
317 * @duplex: desired duplex
318 * @supported: mask of supported link modes
320 * Locate a supported phy setting that is, in priority order:
321 * - an exact match for the specified speed and duplex mode
322 * - a match for the specified speed, or slower speed
323 * - the slowest supported speed
324 * Returns the matched phy_setting entry, or %NULL if no supported phy
325 * settings were found.
327 static const struct phy_setting *
328 phy_find_valid(int speed, int duplex, u32 supported)
330 return phy_lookup_setting(speed, duplex, supported, false);
334 * phy_supported_speeds - return all speeds currently supported by a phy device
335 * @phy: The phy device to return supported speeds of.
336 * @speeds: buffer to store supported speeds in.
337 * @size: size of speeds buffer.
339 * Description: Returns the number of supported speeds, and fills the speeds
340 * buffer with the supported speeds. If speeds buffer is too small to contain
341 * all currently supported speeds, will return as many speeds as can fit.
343 unsigned int phy_supported_speeds(struct phy_device *phy,
344 unsigned int *speeds,
345 unsigned int size)
347 unsigned int count = 0;
348 unsigned int idx = 0;
350 for (idx = 0; idx < ARRAY_SIZE(settings) && count < size; idx++)
351 /* Assumes settings are grouped by speed */
352 if ((settings[idx].setting & phy->supported) &&
353 (count == 0 || speeds[count - 1] != settings[idx].speed))
354 speeds[count++] = settings[idx].speed;
356 return count;
360 * phy_check_valid - check if there is a valid PHY setting which matches
361 * speed, duplex, and feature mask
362 * @speed: speed to match
363 * @duplex: duplex to match
364 * @features: A mask of the valid settings
366 * Description: Returns true if there is a valid setting, false otherwise.
368 static inline bool phy_check_valid(int speed, int duplex, u32 features)
370 return !!phy_lookup_setting(speed, duplex, features, true);
374 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
375 * @phydev: the target phy_device struct
377 * Description: Make sure the PHY is set to supported speeds and
378 * duplexes. Drop down by one in this order: 1000/FULL,
379 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
381 static void phy_sanitize_settings(struct phy_device *phydev)
383 const struct phy_setting *setting;
384 u32 features = phydev->supported;
386 /* Sanitize settings based on PHY capabilities */
387 if ((features & SUPPORTED_Autoneg) == 0)
388 phydev->autoneg = AUTONEG_DISABLE;
390 setting = phy_find_valid(phydev->speed, phydev->duplex, features);
391 if (setting) {
392 phydev->speed = setting->speed;
393 phydev->duplex = setting->duplex;
394 } else {
395 /* We failed to find anything (no supported speeds?) */
396 phydev->speed = SPEED_UNKNOWN;
397 phydev->duplex = DUPLEX_UNKNOWN;
402 * phy_ethtool_sset - generic ethtool sset function, handles all the details
403 * @phydev: target phy_device struct
404 * @cmd: ethtool_cmd
406 * A few notes about parameter checking:
408 * - We don't set port or transceiver, so we don't care what they
409 * were set to.
410 * - phy_start_aneg() will make sure forced settings are sane, and
411 * choose the next best ones from the ones selected, so we don't
412 * care if ethtool tries to give us bad values.
414 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
416 u32 speed = ethtool_cmd_speed(cmd);
418 if (cmd->phy_address != phydev->mdio.addr)
419 return -EINVAL;
421 /* We make sure that we don't pass unsupported values in to the PHY */
422 cmd->advertising &= phydev->supported;
424 /* Verify the settings we care about. */
425 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
426 return -EINVAL;
428 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
429 return -EINVAL;
431 if (cmd->autoneg == AUTONEG_DISABLE &&
432 ((speed != SPEED_1000 &&
433 speed != SPEED_100 &&
434 speed != SPEED_10) ||
435 (cmd->duplex != DUPLEX_HALF &&
436 cmd->duplex != DUPLEX_FULL)))
437 return -EINVAL;
439 phydev->autoneg = cmd->autoneg;
441 phydev->speed = speed;
443 phydev->advertising = cmd->advertising;
445 if (AUTONEG_ENABLE == cmd->autoneg)
446 phydev->advertising |= ADVERTISED_Autoneg;
447 else
448 phydev->advertising &= ~ADVERTISED_Autoneg;
450 phydev->duplex = cmd->duplex;
452 phydev->mdix_ctrl = cmd->eth_tp_mdix_ctrl;
454 /* Restart the PHY */
455 phy_start_aneg(phydev);
457 return 0;
459 EXPORT_SYMBOL(phy_ethtool_sset);
461 int phy_ethtool_ksettings_set(struct phy_device *phydev,
462 const struct ethtool_link_ksettings *cmd)
464 u8 autoneg = cmd->base.autoneg;
465 u8 duplex = cmd->base.duplex;
466 u32 speed = cmd->base.speed;
467 u32 advertising;
469 if (cmd->base.phy_address != phydev->mdio.addr)
470 return -EINVAL;
472 ethtool_convert_link_mode_to_legacy_u32(&advertising,
473 cmd->link_modes.advertising);
475 /* We make sure that we don't pass unsupported values in to the PHY */
476 advertising &= phydev->supported;
478 /* Verify the settings we care about. */
479 if (autoneg != AUTONEG_ENABLE && autoneg != AUTONEG_DISABLE)
480 return -EINVAL;
482 if (autoneg == AUTONEG_ENABLE && advertising == 0)
483 return -EINVAL;
485 if (autoneg == AUTONEG_DISABLE &&
486 ((speed != SPEED_1000 &&
487 speed != SPEED_100 &&
488 speed != SPEED_10) ||
489 (duplex != DUPLEX_HALF &&
490 duplex != DUPLEX_FULL)))
491 return -EINVAL;
493 phydev->autoneg = autoneg;
495 phydev->speed = speed;
497 phydev->advertising = advertising;
499 if (autoneg == AUTONEG_ENABLE)
500 phydev->advertising |= ADVERTISED_Autoneg;
501 else
502 phydev->advertising &= ~ADVERTISED_Autoneg;
504 phydev->duplex = duplex;
506 phydev->mdix_ctrl = cmd->base.eth_tp_mdix_ctrl;
508 /* Restart the PHY */
509 phy_start_aneg(phydev);
511 return 0;
513 EXPORT_SYMBOL(phy_ethtool_ksettings_set);
515 void phy_ethtool_ksettings_get(struct phy_device *phydev,
516 struct ethtool_link_ksettings *cmd)
518 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
519 phydev->supported);
521 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
522 phydev->advertising);
524 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
525 phydev->lp_advertising);
527 cmd->base.speed = phydev->speed;
528 cmd->base.duplex = phydev->duplex;
529 if (phydev->interface == PHY_INTERFACE_MODE_MOCA)
530 cmd->base.port = PORT_BNC;
531 else
532 cmd->base.port = PORT_MII;
534 cmd->base.phy_address = phydev->mdio.addr;
535 cmd->base.autoneg = phydev->autoneg;
536 cmd->base.eth_tp_mdix_ctrl = phydev->mdix_ctrl;
537 cmd->base.eth_tp_mdix = phydev->mdix;
539 EXPORT_SYMBOL(phy_ethtool_ksettings_get);
542 * phy_mii_ioctl - generic PHY MII ioctl interface
543 * @phydev: the phy_device struct
544 * @ifr: &struct ifreq for socket ioctl's
545 * @cmd: ioctl cmd to execute
547 * Note that this function is currently incompatible with the
548 * PHYCONTROL layer. It changes registers without regard to
549 * current state. Use at own risk.
551 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
553 struct mii_ioctl_data *mii_data = if_mii(ifr);
554 u16 val = mii_data->val_in;
555 bool change_autoneg = false;
557 switch (cmd) {
558 case SIOCGMIIPHY:
559 mii_data->phy_id = phydev->mdio.addr;
560 /* fall through */
562 case SIOCGMIIREG:
563 mii_data->val_out = mdiobus_read(phydev->mdio.bus,
564 mii_data->phy_id,
565 mii_data->reg_num);
566 return 0;
568 case SIOCSMIIREG:
569 if (mii_data->phy_id == phydev->mdio.addr) {
570 switch (mii_data->reg_num) {
571 case MII_BMCR:
572 if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0) {
573 if (phydev->autoneg == AUTONEG_ENABLE)
574 change_autoneg = true;
575 phydev->autoneg = AUTONEG_DISABLE;
576 if (val & BMCR_FULLDPLX)
577 phydev->duplex = DUPLEX_FULL;
578 else
579 phydev->duplex = DUPLEX_HALF;
580 if (val & BMCR_SPEED1000)
581 phydev->speed = SPEED_1000;
582 else if (val & BMCR_SPEED100)
583 phydev->speed = SPEED_100;
584 else phydev->speed = SPEED_10;
586 else {
587 if (phydev->autoneg == AUTONEG_DISABLE)
588 change_autoneg = true;
589 phydev->autoneg = AUTONEG_ENABLE;
591 break;
592 case MII_ADVERTISE:
593 phydev->advertising = mii_adv_to_ethtool_adv_t(val);
594 change_autoneg = true;
595 break;
596 default:
597 /* do nothing */
598 break;
602 mdiobus_write(phydev->mdio.bus, mii_data->phy_id,
603 mii_data->reg_num, val);
605 if (mii_data->phy_id == phydev->mdio.addr &&
606 mii_data->reg_num == MII_BMCR &&
607 val & BMCR_RESET)
608 return phy_init_hw(phydev);
610 if (change_autoneg)
611 return phy_start_aneg(phydev);
613 return 0;
615 case SIOCSHWTSTAMP:
616 if (phydev->drv && phydev->drv->hwtstamp)
617 return phydev->drv->hwtstamp(phydev, ifr);
618 /* fall through */
620 default:
621 return -EOPNOTSUPP;
624 EXPORT_SYMBOL(phy_mii_ioctl);
627 * phy_start_aneg_priv - start auto-negotiation for this PHY device
628 * @phydev: the phy_device struct
629 * @sync: indicate whether we should wait for the workqueue cancelation
631 * Description: Sanitizes the settings (if we're not autonegotiating
632 * them), and then calls the driver's config_aneg function.
633 * If the PHYCONTROL Layer is operating, we change the state to
634 * reflect the beginning of Auto-negotiation or forcing.
636 static int phy_start_aneg_priv(struct phy_device *phydev, bool sync)
638 bool trigger = 0;
639 int err;
641 if (!phydev->drv)
642 return -EIO;
644 mutex_lock(&phydev->lock);
646 if (AUTONEG_DISABLE == phydev->autoneg)
647 phy_sanitize_settings(phydev);
649 /* Invalidate LP advertising flags */
650 phydev->lp_advertising = 0;
652 err = phydev->drv->config_aneg(phydev);
653 if (err < 0)
654 goto out_unlock;
656 if (phydev->state != PHY_HALTED) {
657 if (AUTONEG_ENABLE == phydev->autoneg) {
658 phydev->state = PHY_AN;
659 phydev->link_timeout = PHY_AN_TIMEOUT;
660 } else {
661 phydev->state = PHY_FORCING;
662 phydev->link_timeout = PHY_FORCE_TIMEOUT;
666 /* Re-schedule a PHY state machine to check PHY status because
667 * negotiation may already be done and aneg interrupt may not be
668 * generated.
670 if (phy_interrupt_is_valid(phydev) && (phydev->state == PHY_AN)) {
671 err = phy_aneg_done(phydev);
672 if (err > 0) {
673 trigger = true;
674 err = 0;
678 out_unlock:
679 mutex_unlock(&phydev->lock);
681 if (trigger)
682 phy_trigger_machine(phydev, sync);
684 return err;
688 * phy_start_aneg - start auto-negotiation for this PHY device
689 * @phydev: the phy_device struct
691 * Description: Sanitizes the settings (if we're not autonegotiating
692 * them), and then calls the driver's config_aneg function.
693 * If the PHYCONTROL Layer is operating, we change the state to
694 * reflect the beginning of Auto-negotiation or forcing.
696 int phy_start_aneg(struct phy_device *phydev)
698 return phy_start_aneg_priv(phydev, true);
700 EXPORT_SYMBOL(phy_start_aneg);
703 * phy_start_machine - start PHY state machine tracking
704 * @phydev: the phy_device struct
706 * Description: The PHY infrastructure can run a state machine
707 * which tracks whether the PHY is starting up, negotiating,
708 * etc. This function starts the timer which tracks the state
709 * of the PHY. If you want to maintain your own state machine,
710 * do not call this function.
712 void phy_start_machine(struct phy_device *phydev)
714 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
718 * phy_trigger_machine - trigger the state machine to run
720 * @phydev: the phy_device struct
721 * @sync: indicate whether we should wait for the workqueue cancelation
723 * Description: There has been a change in state which requires that the
724 * state machine runs.
727 void phy_trigger_machine(struct phy_device *phydev, bool sync)
729 if (sync)
730 cancel_delayed_work_sync(&phydev->state_queue);
731 else
732 cancel_delayed_work(&phydev->state_queue);
733 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
737 * phy_stop_machine - stop the PHY state machine tracking
738 * @phydev: target phy_device struct
740 * Description: Stops the state machine timer, sets the state to UP
741 * (unless it wasn't up yet). This function must be called BEFORE
742 * phy_detach.
744 void phy_stop_machine(struct phy_device *phydev)
746 cancel_delayed_work_sync(&phydev->state_queue);
748 mutex_lock(&phydev->lock);
749 if (phydev->state > PHY_UP && phydev->state != PHY_HALTED)
750 phydev->state = PHY_UP;
751 mutex_unlock(&phydev->lock);
755 * phy_error - enter HALTED state for this PHY device
756 * @phydev: target phy_device struct
758 * Moves the PHY to the HALTED state in response to a read
759 * or write error, and tells the controller the link is down.
760 * Must not be called from interrupt context, or while the
761 * phydev->lock is held.
763 static void phy_error(struct phy_device *phydev)
765 mutex_lock(&phydev->lock);
766 phydev->state = PHY_HALTED;
767 mutex_unlock(&phydev->lock);
769 phy_trigger_machine(phydev, false);
773 * phy_interrupt - PHY interrupt handler
774 * @irq: interrupt line
775 * @phy_dat: phy_device pointer
777 * Description: When a PHY interrupt occurs, the handler disables
778 * interrupts, and uses phy_change to handle the interrupt.
780 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
782 struct phy_device *phydev = phy_dat;
784 if (PHY_HALTED == phydev->state)
785 return IRQ_NONE; /* It can't be ours. */
787 disable_irq_nosync(irq);
788 atomic_inc(&phydev->irq_disable);
790 phy_change(phydev);
792 return IRQ_HANDLED;
796 * phy_enable_interrupts - Enable the interrupts from the PHY side
797 * @phydev: target phy_device struct
799 static int phy_enable_interrupts(struct phy_device *phydev)
801 int err = phy_clear_interrupt(phydev);
803 if (err < 0)
804 return err;
806 return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
810 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
811 * @phydev: target phy_device struct
813 static int phy_disable_interrupts(struct phy_device *phydev)
815 int err;
817 /* Disable PHY interrupts */
818 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
819 if (err)
820 goto phy_err;
822 /* Clear the interrupt */
823 err = phy_clear_interrupt(phydev);
824 if (err)
825 goto phy_err;
827 return 0;
829 phy_err:
830 phy_error(phydev);
832 return err;
836 * phy_start_interrupts - request and enable interrupts for a PHY device
837 * @phydev: target phy_device struct
839 * Description: Request the interrupt for the given PHY.
840 * If this fails, then we set irq to PHY_POLL.
841 * Otherwise, we enable the interrupts in the PHY.
842 * This should only be called with a valid IRQ number.
843 * Returns 0 on success or < 0 on error.
845 int phy_start_interrupts(struct phy_device *phydev)
847 atomic_set(&phydev->irq_disable, 0);
848 if (request_threaded_irq(phydev->irq, NULL, phy_interrupt,
849 IRQF_ONESHOT | IRQF_SHARED,
850 phydev_name(phydev), phydev) < 0) {
851 pr_warn("%s: Can't get IRQ %d (PHY)\n",
852 phydev->mdio.bus->name, phydev->irq);
853 phydev->irq = PHY_POLL;
854 return 0;
857 return phy_enable_interrupts(phydev);
859 EXPORT_SYMBOL(phy_start_interrupts);
862 * phy_stop_interrupts - disable interrupts from a PHY device
863 * @phydev: target phy_device struct
865 int phy_stop_interrupts(struct phy_device *phydev)
867 int err = phy_disable_interrupts(phydev);
869 if (err)
870 phy_error(phydev);
872 free_irq(phydev->irq, phydev);
874 /* If work indeed has been cancelled, disable_irq() will have
875 * been left unbalanced from phy_interrupt() and enable_irq()
876 * has to be called so that other devices on the line work.
878 while (atomic_dec_return(&phydev->irq_disable) >= 0)
879 enable_irq(phydev->irq);
881 return err;
883 EXPORT_SYMBOL(phy_stop_interrupts);
886 * phy_change - Called by the phy_interrupt to handle PHY changes
887 * @phydev: phy_device struct that interrupted
889 void phy_change(struct phy_device *phydev)
891 if (phy_interrupt_is_valid(phydev)) {
892 if (phydev->drv->did_interrupt &&
893 !phydev->drv->did_interrupt(phydev))
894 goto ignore;
896 if (phy_disable_interrupts(phydev))
897 goto phy_err;
900 mutex_lock(&phydev->lock);
901 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
902 phydev->state = PHY_CHANGELINK;
903 mutex_unlock(&phydev->lock);
905 if (phy_interrupt_is_valid(phydev)) {
906 atomic_dec(&phydev->irq_disable);
907 enable_irq(phydev->irq);
909 /* Reenable interrupts */
910 if (PHY_HALTED != phydev->state &&
911 phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
912 goto irq_enable_err;
915 /* reschedule state queue work to run as soon as possible */
916 phy_trigger_machine(phydev, true);
917 return;
919 ignore:
920 atomic_dec(&phydev->irq_disable);
921 enable_irq(phydev->irq);
922 return;
924 irq_enable_err:
925 disable_irq(phydev->irq);
926 atomic_inc(&phydev->irq_disable);
927 phy_err:
928 phy_error(phydev);
932 * phy_change_work - Scheduled by the phy_mac_interrupt to handle PHY changes
933 * @work: work_struct that describes the work to be done
935 void phy_change_work(struct work_struct *work)
937 struct phy_device *phydev =
938 container_of(work, struct phy_device, phy_queue);
940 phy_change(phydev);
944 * phy_stop - Bring down the PHY link, and stop checking the status
945 * @phydev: target phy_device struct
947 void phy_stop(struct phy_device *phydev)
949 mutex_lock(&phydev->lock);
951 if (PHY_HALTED == phydev->state)
952 goto out_unlock;
954 if (phy_interrupt_is_valid(phydev)) {
955 /* Disable PHY Interrupts */
956 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
958 /* Clear any pending interrupts */
959 phy_clear_interrupt(phydev);
962 phydev->state = PHY_HALTED;
964 out_unlock:
965 mutex_unlock(&phydev->lock);
967 /* Cannot call flush_scheduled_work() here as desired because
968 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
969 * will not reenable interrupts.
972 EXPORT_SYMBOL(phy_stop);
975 * phy_start - start or restart a PHY device
976 * @phydev: target phy_device struct
978 * Description: Indicates the attached device's readiness to
979 * handle PHY-related work. Used during startup to start the
980 * PHY, and after a call to phy_stop() to resume operation.
981 * Also used to indicate the MDIO bus has cleared an error
982 * condition.
984 void phy_start(struct phy_device *phydev)
986 bool do_resume = false;
987 int err = 0;
989 mutex_lock(&phydev->lock);
991 switch (phydev->state) {
992 case PHY_STARTING:
993 phydev->state = PHY_PENDING;
994 break;
995 case PHY_READY:
996 phydev->state = PHY_UP;
997 break;
998 case PHY_HALTED:
999 /* make sure interrupts are re-enabled for the PHY */
1000 if (phydev->irq != PHY_POLL) {
1001 err = phy_enable_interrupts(phydev);
1002 if (err < 0)
1003 break;
1006 phydev->state = PHY_RESUMING;
1007 do_resume = true;
1008 break;
1009 default:
1010 break;
1012 mutex_unlock(&phydev->lock);
1014 /* if phy was suspended, bring the physical link up again */
1015 if (do_resume)
1016 phy_resume(phydev);
1018 phy_trigger_machine(phydev, true);
1020 EXPORT_SYMBOL(phy_start);
1022 static void phy_adjust_link(struct phy_device *phydev)
1024 phydev->adjust_link(phydev->attached_dev);
1025 phy_led_trigger_change_speed(phydev);
1029 * phy_state_machine - Handle the state machine
1030 * @work: work_struct that describes the work to be done
1032 void phy_state_machine(struct work_struct *work)
1034 struct delayed_work *dwork = to_delayed_work(work);
1035 struct phy_device *phydev =
1036 container_of(dwork, struct phy_device, state_queue);
1037 bool needs_aneg = false, do_suspend = false;
1038 enum phy_state old_state;
1039 int err = 0;
1040 int old_link;
1042 mutex_lock(&phydev->lock);
1044 old_state = phydev->state;
1046 if (phydev->drv && phydev->drv->link_change_notify)
1047 phydev->drv->link_change_notify(phydev);
1049 switch (phydev->state) {
1050 case PHY_DOWN:
1051 case PHY_STARTING:
1052 case PHY_READY:
1053 case PHY_PENDING:
1054 break;
1055 case PHY_UP:
1056 needs_aneg = true;
1058 phydev->link_timeout = PHY_AN_TIMEOUT;
1060 break;
1061 case PHY_AN:
1062 err = phy_read_status(phydev);
1063 if (err < 0)
1064 break;
1066 /* If the link is down, give up on negotiation for now */
1067 if (!phydev->link) {
1068 phydev->state = PHY_NOLINK;
1069 netif_carrier_off(phydev->attached_dev);
1070 phy_adjust_link(phydev);
1071 break;
1074 /* Check if negotiation is done. Break if there's an error */
1075 err = phy_aneg_done(phydev);
1076 if (err < 0)
1077 break;
1079 /* If AN is done, we're running */
1080 if (err > 0) {
1081 phydev->state = PHY_RUNNING;
1082 netif_carrier_on(phydev->attached_dev);
1083 phy_adjust_link(phydev);
1085 } else if (0 == phydev->link_timeout--)
1086 needs_aneg = true;
1087 break;
1088 case PHY_NOLINK:
1089 if (phy_interrupt_is_valid(phydev))
1090 break;
1092 err = phy_read_status(phydev);
1093 if (err)
1094 break;
1096 if (phydev->link) {
1097 if (AUTONEG_ENABLE == phydev->autoneg) {
1098 err = phy_aneg_done(phydev);
1099 if (err < 0)
1100 break;
1102 if (!err) {
1103 phydev->state = PHY_AN;
1104 phydev->link_timeout = PHY_AN_TIMEOUT;
1105 break;
1108 phydev->state = PHY_RUNNING;
1109 netif_carrier_on(phydev->attached_dev);
1110 phy_adjust_link(phydev);
1112 break;
1113 case PHY_FORCING:
1114 err = genphy_update_link(phydev);
1115 if (err)
1116 break;
1118 if (phydev->link) {
1119 phydev->state = PHY_RUNNING;
1120 netif_carrier_on(phydev->attached_dev);
1121 } else {
1122 if (0 == phydev->link_timeout--)
1123 needs_aneg = true;
1126 phy_adjust_link(phydev);
1127 break;
1128 case PHY_RUNNING:
1129 /* Only register a CHANGE if we are polling and link changed
1130 * since latest checking.
1132 if (phydev->irq == PHY_POLL) {
1133 old_link = phydev->link;
1134 err = phy_read_status(phydev);
1135 if (err)
1136 break;
1138 if (old_link != phydev->link)
1139 phydev->state = PHY_CHANGELINK;
1142 * Failsafe: check that nobody set phydev->link=0 between two
1143 * poll cycles, otherwise we won't leave RUNNING state as long
1144 * as link remains down.
1146 if (!phydev->link && phydev->state == PHY_RUNNING) {
1147 phydev->state = PHY_CHANGELINK;
1148 phydev_err(phydev, "no link in PHY_RUNNING\n");
1150 break;
1151 case PHY_CHANGELINK:
1152 err = phy_read_status(phydev);
1153 if (err)
1154 break;
1156 if (phydev->link) {
1157 phydev->state = PHY_RUNNING;
1158 netif_carrier_on(phydev->attached_dev);
1159 } else {
1160 phydev->state = PHY_NOLINK;
1161 netif_carrier_off(phydev->attached_dev);
1164 phy_adjust_link(phydev);
1166 if (phy_interrupt_is_valid(phydev))
1167 err = phy_config_interrupt(phydev,
1168 PHY_INTERRUPT_ENABLED);
1169 break;
1170 case PHY_HALTED:
1171 if (phydev->link) {
1172 phydev->link = 0;
1173 netif_carrier_off(phydev->attached_dev);
1174 phy_adjust_link(phydev);
1175 do_suspend = true;
1177 break;
1178 case PHY_RESUMING:
1179 if (AUTONEG_ENABLE == phydev->autoneg) {
1180 err = phy_aneg_done(phydev);
1181 if (err < 0)
1182 break;
1184 /* err > 0 if AN is done.
1185 * Otherwise, it's 0, and we're still waiting for AN
1187 if (err > 0) {
1188 err = phy_read_status(phydev);
1189 if (err)
1190 break;
1192 if (phydev->link) {
1193 phydev->state = PHY_RUNNING;
1194 netif_carrier_on(phydev->attached_dev);
1195 } else {
1196 phydev->state = PHY_NOLINK;
1198 phy_adjust_link(phydev);
1199 } else {
1200 phydev->state = PHY_AN;
1201 phydev->link_timeout = PHY_AN_TIMEOUT;
1203 } else {
1204 err = phy_read_status(phydev);
1205 if (err)
1206 break;
1208 if (phydev->link) {
1209 phydev->state = PHY_RUNNING;
1210 netif_carrier_on(phydev->attached_dev);
1211 } else {
1212 phydev->state = PHY_NOLINK;
1214 phy_adjust_link(phydev);
1216 break;
1219 mutex_unlock(&phydev->lock);
1221 if (needs_aneg)
1222 err = phy_start_aneg_priv(phydev, false);
1223 else if (do_suspend)
1224 phy_suspend(phydev);
1226 if (err < 0)
1227 phy_error(phydev);
1229 phydev_dbg(phydev, "PHY state change %s -> %s\n",
1230 phy_state_to_str(old_state),
1231 phy_state_to_str(phydev->state));
1233 /* Only re-schedule a PHY state machine change if we are polling the
1234 * PHY, if PHY_IGNORE_INTERRUPT is set, then we will be moving
1235 * between states from phy_mac_interrupt()
1237 if (phydev->irq == PHY_POLL)
1238 queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
1239 PHY_STATE_TIME * HZ);
1243 * phy_mac_interrupt - MAC says the link has changed
1244 * @phydev: phy_device struct with changed link
1245 * @new_link: Link is Up/Down.
1247 * Description: The MAC layer is able indicate there has been a change
1248 * in the PHY link status. Set the new link status, and trigger the
1249 * state machine, work a work queue.
1251 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
1253 phydev->link = new_link;
1255 /* Trigger a state machine change */
1256 queue_work(system_power_efficient_wq, &phydev->phy_queue);
1258 EXPORT_SYMBOL(phy_mac_interrupt);
1261 * phy_init_eee - init and check the EEE feature
1262 * @phydev: target phy_device struct
1263 * @clk_stop_enable: PHY may stop the clock during LPI
1265 * Description: it checks if the Energy-Efficient Ethernet (EEE)
1266 * is supported by looking at the MMD registers 3.20 and 7.60/61
1267 * and it programs the MMD register 3.0 setting the "Clock stop enable"
1268 * bit if required.
1270 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
1272 if (!phydev->drv)
1273 return -EIO;
1275 /* According to 802.3az,the EEE is supported only in full duplex-mode.
1277 if (phydev->duplex == DUPLEX_FULL) {
1278 int eee_lp, eee_cap, eee_adv;
1279 u32 lp, cap, adv;
1280 int status;
1282 /* Read phy status to properly get the right settings */
1283 status = phy_read_status(phydev);
1284 if (status)
1285 return status;
1287 /* First check if the EEE ability is supported */
1288 eee_cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1289 if (eee_cap <= 0)
1290 goto eee_exit_err;
1292 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
1293 if (!cap)
1294 goto eee_exit_err;
1296 /* Check which link settings negotiated and verify it in
1297 * the EEE advertising registers.
1299 eee_lp = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1300 if (eee_lp <= 0)
1301 goto eee_exit_err;
1303 eee_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1304 if (eee_adv <= 0)
1305 goto eee_exit_err;
1307 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
1308 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
1309 if (!phy_check_valid(phydev->speed, phydev->duplex, lp & adv))
1310 goto eee_exit_err;
1312 if (clk_stop_enable) {
1313 /* Configure the PHY to stop receiving xMII
1314 * clock while it is signaling LPI.
1316 int val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1);
1317 if (val < 0)
1318 return val;
1320 val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1321 phy_write_mmd(phydev, MDIO_MMD_PCS, MDIO_CTRL1, val);
1324 return 0; /* EEE supported */
1326 eee_exit_err:
1327 return -EPROTONOSUPPORT;
1329 EXPORT_SYMBOL(phy_init_eee);
1332 * phy_get_eee_err - report the EEE wake error count
1333 * @phydev: target phy_device struct
1335 * Description: it is to report the number of time where the PHY
1336 * failed to complete its normal wake sequence.
1338 int phy_get_eee_err(struct phy_device *phydev)
1340 if (!phydev->drv)
1341 return -EIO;
1343 return phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_WK_ERR);
1345 EXPORT_SYMBOL(phy_get_eee_err);
1348 * phy_ethtool_get_eee - get EEE supported and status
1349 * @phydev: target phy_device struct
1350 * @data: ethtool_eee data
1352 * Description: it reportes the Supported/Advertisement/LP Advertisement
1353 * capabilities.
1355 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1357 int val;
1359 if (!phydev->drv)
1360 return -EIO;
1362 /* Get Supported EEE */
1363 val = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1364 if (val < 0)
1365 return val;
1366 data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1368 /* Get advertisement EEE */
1369 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1370 if (val < 0)
1371 return val;
1372 data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1374 /* Get LP advertisement EEE */
1375 val = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_LPABLE);
1376 if (val < 0)
1377 return val;
1378 data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1380 return 0;
1382 EXPORT_SYMBOL(phy_ethtool_get_eee);
1385 * phy_ethtool_set_eee - set EEE supported and status
1386 * @phydev: target phy_device struct
1387 * @data: ethtool_eee data
1389 * Description: it is to program the Advertisement EEE register.
1391 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1393 int cap, old_adv, adv, ret;
1395 if (!phydev->drv)
1396 return -EIO;
1398 /* Get Supported EEE */
1399 cap = phy_read_mmd(phydev, MDIO_MMD_PCS, MDIO_PCS_EEE_ABLE);
1400 if (cap < 0)
1401 return cap;
1403 old_adv = phy_read_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV);
1404 if (old_adv < 0)
1405 return old_adv;
1407 adv = ethtool_adv_to_mmd_eee_adv_t(data->advertised) & cap;
1409 /* Mask prohibited EEE modes */
1410 adv &= ~phydev->eee_broken_modes;
1412 if (old_adv != adv) {
1413 ret = phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, adv);
1414 if (ret < 0)
1415 return ret;
1417 /* Restart autonegotiation so the new modes get sent to the
1418 * link partner.
1420 ret = phy_restart_aneg(phydev);
1421 if (ret < 0)
1422 return ret;
1425 return 0;
1427 EXPORT_SYMBOL(phy_ethtool_set_eee);
1429 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1431 if (phydev->drv && phydev->drv->set_wol)
1432 return phydev->drv->set_wol(phydev, wol);
1434 return -EOPNOTSUPP;
1436 EXPORT_SYMBOL(phy_ethtool_set_wol);
1438 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1440 if (phydev->drv && phydev->drv->get_wol)
1441 phydev->drv->get_wol(phydev, wol);
1443 EXPORT_SYMBOL(phy_ethtool_get_wol);
1445 int phy_ethtool_get_link_ksettings(struct net_device *ndev,
1446 struct ethtool_link_ksettings *cmd)
1448 struct phy_device *phydev = ndev->phydev;
1450 if (!phydev)
1451 return -ENODEV;
1453 phy_ethtool_ksettings_get(phydev, cmd);
1455 return 0;
1457 EXPORT_SYMBOL(phy_ethtool_get_link_ksettings);
1459 int phy_ethtool_set_link_ksettings(struct net_device *ndev,
1460 const struct ethtool_link_ksettings *cmd)
1462 struct phy_device *phydev = ndev->phydev;
1464 if (!phydev)
1465 return -ENODEV;
1467 return phy_ethtool_ksettings_set(phydev, cmd);
1469 EXPORT_SYMBOL(phy_ethtool_set_link_ksettings);
1471 int phy_ethtool_nway_reset(struct net_device *ndev)
1473 struct phy_device *phydev = ndev->phydev;
1475 if (!phydev)
1476 return -ENODEV;
1478 if (!phydev->drv)
1479 return -EIO;
1481 return phy_restart_aneg(phydev);
1483 EXPORT_SYMBOL(phy_ethtool_nway_reset);