xref: /openbmc/linux/drivers/net/phy/phy.c (revision 8e9356c6)
1 /* Framework for configuring and reading PHY devices
2  * Based on code in sungem_phy.c and gianfar_phy.c
3  *
4  * Author: Andy Fleming
5  *
6  * Copyright (c) 2004 Freescale Semiconductor, Inc.
7  * Copyright (c) 2006, 2007  Maciej W. Rozycki
8  *
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.
13  *
14  */
15 
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 
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/timer.h>
33 #include <linux/workqueue.h>
34 #include <linux/mdio.h>
35 #include <linux/io.h>
36 #include <linux/uaccess.h>
37 #include <linux/atomic.h>
38 
39 #include <asm/irq.h>
40 
41 /**
42  * phy_print_status - Convenience function to print out the current phy status
43  * @phydev: the phy_device struct
44  */
45 void phy_print_status(struct phy_device *phydev)
46 {
47 	if (phydev->link) {
48 		pr_info("%s - Link is Up - %d/%s\n",
49 			dev_name(&phydev->dev),
50 			phydev->speed,
51 			DUPLEX_FULL == phydev->duplex ? "Full" : "Half");
52 	} else	{
53 		pr_info("%s - Link is Down\n", dev_name(&phydev->dev));
54 	}
55 }
56 EXPORT_SYMBOL(phy_print_status);
57 
58 /**
59  * phy_clear_interrupt - Ack the phy device's interrupt
60  * @phydev: the phy_device struct
61  *
62  * If the @phydev driver has an ack_interrupt function, call it to
63  * ack and clear the phy device's interrupt.
64  *
65  * Returns 0 on success on < 0 on error.
66  */
67 static int phy_clear_interrupt(struct phy_device *phydev)
68 {
69 	if (phydev->drv->ack_interrupt)
70 		return phydev->drv->ack_interrupt(phydev);
71 
72 	return 0;
73 }
74 
75 /**
76  * phy_config_interrupt - configure the PHY device for the requested interrupts
77  * @phydev: the phy_device struct
78  * @interrupts: interrupt flags to configure for this @phydev
79  *
80  * Returns 0 on success on < 0 on error.
81  */
82 static int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
83 {
84 	phydev->interrupts = interrupts;
85 	if (phydev->drv->config_intr)
86 		return phydev->drv->config_intr(phydev);
87 
88 	return 0;
89 }
90 
91 
92 /**
93  * phy_aneg_done - return auto-negotiation status
94  * @phydev: target phy_device struct
95  *
96  * Description: Reads the status register and returns 0 either if
97  *   auto-negotiation is incomplete, or if there was an error.
98  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
99  */
100 static inline int phy_aneg_done(struct phy_device *phydev)
101 {
102 	int retval = phy_read(phydev, MII_BMSR);
103 
104 	return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
105 }
106 
107 /* A structure for mapping a particular speed and duplex
108  * combination to a particular SUPPORTED and ADVERTISED value
109  */
110 struct phy_setting {
111 	int speed;
112 	int duplex;
113 	u32 setting;
114 };
115 
116 /* A mapping of all SUPPORTED settings to speed/duplex */
117 static const struct phy_setting settings[] = {
118 	{
119 		.speed = 10000,
120 		.duplex = DUPLEX_FULL,
121 		.setting = SUPPORTED_10000baseT_Full,
122 	},
123 	{
124 		.speed = SPEED_1000,
125 		.duplex = DUPLEX_FULL,
126 		.setting = SUPPORTED_1000baseT_Full,
127 	},
128 	{
129 		.speed = SPEED_1000,
130 		.duplex = DUPLEX_HALF,
131 		.setting = SUPPORTED_1000baseT_Half,
132 	},
133 	{
134 		.speed = SPEED_100,
135 		.duplex = DUPLEX_FULL,
136 		.setting = SUPPORTED_100baseT_Full,
137 	},
138 	{
139 		.speed = SPEED_100,
140 		.duplex = DUPLEX_HALF,
141 		.setting = SUPPORTED_100baseT_Half,
142 	},
143 	{
144 		.speed = SPEED_10,
145 		.duplex = DUPLEX_FULL,
146 		.setting = SUPPORTED_10baseT_Full,
147 	},
148 	{
149 		.speed = SPEED_10,
150 		.duplex = DUPLEX_HALF,
151 		.setting = SUPPORTED_10baseT_Half,
152 	},
153 };
154 
155 #define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
156 
157 /**
158  * phy_find_setting - find a PHY settings array entry that matches speed & duplex
159  * @speed: speed to match
160  * @duplex: duplex to match
161  *
162  * Description: Searches the settings array for the setting which
163  *   matches the desired speed and duplex, and returns the index
164  *   of that setting.  Returns the index of the last setting if
165  *   none of the others match.
166  */
167 static inline int phy_find_setting(int speed, int duplex)
168 {
169 	int idx = 0;
170 
171 	while (idx < ARRAY_SIZE(settings) &&
172 	       (settings[idx].speed != speed || settings[idx].duplex != duplex))
173 		idx++;
174 
175 	return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
176 }
177 
178 /**
179  * phy_find_valid - find a PHY setting that matches the requested features mask
180  * @idx: The first index in settings[] to search
181  * @features: A mask of the valid settings
182  *
183  * Description: Returns the index of the first valid setting less
184  *   than or equal to the one pointed to by idx, as determined by
185  *   the mask in features.  Returns the index of the last setting
186  *   if nothing else matches.
187  */
188 static inline int phy_find_valid(int idx, u32 features)
189 {
190 	while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
191 		idx++;
192 
193 	return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
194 }
195 
196 /**
197  * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
198  * @phydev: the target phy_device struct
199  *
200  * Description: Make sure the PHY is set to supported speeds and
201  *   duplexes.  Drop down by one in this order:  1000/FULL,
202  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
203  */
204 static void phy_sanitize_settings(struct phy_device *phydev)
205 {
206 	u32 features = phydev->supported;
207 	int idx;
208 
209 	/* Sanitize settings based on PHY capabilities */
210 	if ((features & SUPPORTED_Autoneg) == 0)
211 		phydev->autoneg = AUTONEG_DISABLE;
212 
213 	idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
214 			features);
215 
216 	phydev->speed = settings[idx].speed;
217 	phydev->duplex = settings[idx].duplex;
218 }
219 
220 /**
221  * phy_ethtool_sset - generic ethtool sset function, handles all the details
222  * @phydev: target phy_device struct
223  * @cmd: ethtool_cmd
224  *
225  * A few notes about parameter checking:
226  * - We don't set port or transceiver, so we don't care what they
227  *   were set to.
228  * - phy_start_aneg() will make sure forced settings are sane, and
229  *   choose the next best ones from the ones selected, so we don't
230  *   care if ethtool tries to give us bad values.
231  */
232 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
233 {
234 	u32 speed = ethtool_cmd_speed(cmd);
235 
236 	if (cmd->phy_address != phydev->addr)
237 		return -EINVAL;
238 
239 	/* We make sure that we don't pass unsupported values in to the PHY */
240 	cmd->advertising &= phydev->supported;
241 
242 	/* Verify the settings we care about. */
243 	if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
244 		return -EINVAL;
245 
246 	if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
247 		return -EINVAL;
248 
249 	if (cmd->autoneg == AUTONEG_DISABLE &&
250 	    ((speed != SPEED_1000 &&
251 	      speed != SPEED_100 &&
252 	      speed != SPEED_10) ||
253 	     (cmd->duplex != DUPLEX_HALF &&
254 	      cmd->duplex != DUPLEX_FULL)))
255 		return -EINVAL;
256 
257 	phydev->autoneg = cmd->autoneg;
258 
259 	phydev->speed = speed;
260 
261 	phydev->advertising = cmd->advertising;
262 
263 	if (AUTONEG_ENABLE == cmd->autoneg)
264 		phydev->advertising |= ADVERTISED_Autoneg;
265 	else
266 		phydev->advertising &= ~ADVERTISED_Autoneg;
267 
268 	phydev->duplex = cmd->duplex;
269 
270 	/* Restart the PHY */
271 	phy_start_aneg(phydev);
272 
273 	return 0;
274 }
275 EXPORT_SYMBOL(phy_ethtool_sset);
276 
277 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
278 {
279 	cmd->supported = phydev->supported;
280 
281 	cmd->advertising = phydev->advertising;
282 	cmd->lp_advertising = phydev->lp_advertising;
283 
284 	ethtool_cmd_speed_set(cmd, phydev->speed);
285 	cmd->duplex = phydev->duplex;
286 	cmd->port = PORT_MII;
287 	cmd->phy_address = phydev->addr;
288 	cmd->transceiver = phy_is_internal(phydev) ?
289 		XCVR_INTERNAL : XCVR_EXTERNAL;
290 	cmd->autoneg = phydev->autoneg;
291 
292 	return 0;
293 }
294 EXPORT_SYMBOL(phy_ethtool_gset);
295 
296 /**
297  * phy_mii_ioctl - generic PHY MII ioctl interface
298  * @phydev: the phy_device struct
299  * @ifr: &struct ifreq for socket ioctl's
300  * @cmd: ioctl cmd to execute
301  *
302  * Note that this function is currently incompatible with the
303  * PHYCONTROL layer.  It changes registers without regard to
304  * current state.  Use at own risk.
305  */
306 int phy_mii_ioctl(struct phy_device *phydev, struct ifreq *ifr, int cmd)
307 {
308 	struct mii_ioctl_data *mii_data = if_mii(ifr);
309 	u16 val = mii_data->val_in;
310 
311 	switch (cmd) {
312 	case SIOCGMIIPHY:
313 		mii_data->phy_id = phydev->addr;
314 		/* fall through */
315 
316 	case SIOCGMIIREG:
317 		mii_data->val_out = mdiobus_read(phydev->bus, mii_data->phy_id,
318 						 mii_data->reg_num);
319 		return 0;
320 
321 	case SIOCSMIIREG:
322 		if (mii_data->phy_id == phydev->addr) {
323 			switch (mii_data->reg_num) {
324 			case MII_BMCR:
325 				if ((val & (BMCR_RESET | BMCR_ANENABLE)) == 0)
326 					phydev->autoneg = AUTONEG_DISABLE;
327 				else
328 					phydev->autoneg = AUTONEG_ENABLE;
329 				if (!phydev->autoneg && (val & BMCR_FULLDPLX))
330 					phydev->duplex = DUPLEX_FULL;
331 				else
332 					phydev->duplex = DUPLEX_HALF;
333 				if (!phydev->autoneg && (val & BMCR_SPEED1000))
334 					phydev->speed = SPEED_1000;
335 				else if (!phydev->autoneg &&
336 					 (val & BMCR_SPEED100))
337 					phydev->speed = SPEED_100;
338 				break;
339 			case MII_ADVERTISE:
340 				phydev->advertising = val;
341 				break;
342 			default:
343 				/* do nothing */
344 				break;
345 			}
346 		}
347 
348 		mdiobus_write(phydev->bus, mii_data->phy_id,
349 			      mii_data->reg_num, val);
350 
351 		if (mii_data->reg_num == MII_BMCR &&
352 		    val & BMCR_RESET)
353 			return phy_init_hw(phydev);
354 		return 0;
355 
356 	case SIOCSHWTSTAMP:
357 		if (phydev->drv->hwtstamp)
358 			return phydev->drv->hwtstamp(phydev, ifr);
359 		/* fall through */
360 
361 	default:
362 		return -EOPNOTSUPP;
363 	}
364 }
365 EXPORT_SYMBOL(phy_mii_ioctl);
366 
367 /**
368  * phy_start_aneg - start auto-negotiation for this PHY device
369  * @phydev: the phy_device struct
370  *
371  * Description: Sanitizes the settings (if we're not autonegotiating
372  *   them), and then calls the driver's config_aneg function.
373  *   If the PHYCONTROL Layer is operating, we change the state to
374  *   reflect the beginning of Auto-negotiation or forcing.
375  */
376 int phy_start_aneg(struct phy_device *phydev)
377 {
378 	int err;
379 
380 	mutex_lock(&phydev->lock);
381 
382 	if (AUTONEG_DISABLE == phydev->autoneg)
383 		phy_sanitize_settings(phydev);
384 
385 	err = phydev->drv->config_aneg(phydev);
386 	if (err < 0)
387 		goto out_unlock;
388 
389 	if (phydev->state != PHY_HALTED) {
390 		if (AUTONEG_ENABLE == phydev->autoneg) {
391 			phydev->state = PHY_AN;
392 			phydev->link_timeout = PHY_AN_TIMEOUT;
393 		} else {
394 			phydev->state = PHY_FORCING;
395 			phydev->link_timeout = PHY_FORCE_TIMEOUT;
396 		}
397 	}
398 
399 out_unlock:
400 	mutex_unlock(&phydev->lock);
401 	return err;
402 }
403 EXPORT_SYMBOL(phy_start_aneg);
404 
405 /**
406  * phy_start_machine - start PHY state machine tracking
407  * @phydev: the phy_device struct
408  *
409  * Description: The PHY infrastructure can run a state machine
410  *   which tracks whether the PHY is starting up, negotiating,
411  *   etc.  This function starts the timer which tracks the state
412  *   of the PHY.  If you want to maintain your own state machine,
413  *   do not call this function.
414  */
415 void phy_start_machine(struct phy_device *phydev)
416 {
417 	queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, HZ);
418 }
419 
420 /**
421  * phy_stop_machine - stop the PHY state machine tracking
422  * @phydev: target phy_device struct
423  *
424  * Description: Stops the state machine timer, sets the state to UP
425  *   (unless it wasn't up yet). This function must be called BEFORE
426  *   phy_detach.
427  */
428 void phy_stop_machine(struct phy_device *phydev)
429 {
430 	cancel_delayed_work_sync(&phydev->state_queue);
431 
432 	mutex_lock(&phydev->lock);
433 	if (phydev->state > PHY_UP)
434 		phydev->state = PHY_UP;
435 	mutex_unlock(&phydev->lock);
436 }
437 
438 /**
439  * phy_error - enter HALTED state for this PHY device
440  * @phydev: target phy_device struct
441  *
442  * Moves the PHY to the HALTED state in response to a read
443  * or write error, and tells the controller the link is down.
444  * Must not be called from interrupt context, or while the
445  * phydev->lock is held.
446  */
447 static void phy_error(struct phy_device *phydev)
448 {
449 	mutex_lock(&phydev->lock);
450 	phydev->state = PHY_HALTED;
451 	mutex_unlock(&phydev->lock);
452 }
453 
454 /**
455  * phy_interrupt - PHY interrupt handler
456  * @irq: interrupt line
457  * @phy_dat: phy_device pointer
458  *
459  * Description: When a PHY interrupt occurs, the handler disables
460  * interrupts, and schedules a work task to clear the interrupt.
461  */
462 static irqreturn_t phy_interrupt(int irq, void *phy_dat)
463 {
464 	struct phy_device *phydev = phy_dat;
465 
466 	if (PHY_HALTED == phydev->state)
467 		return IRQ_NONE;		/* It can't be ours.  */
468 
469 	/* The MDIO bus is not allowed to be written in interrupt
470 	 * context, so we need to disable the irq here.  A work
471 	 * queue will write the PHY to disable and clear the
472 	 * interrupt, and then reenable the irq line.
473 	 */
474 	disable_irq_nosync(irq);
475 	atomic_inc(&phydev->irq_disable);
476 
477 	queue_work(system_power_efficient_wq, &phydev->phy_queue);
478 
479 	return IRQ_HANDLED;
480 }
481 
482 /**
483  * phy_enable_interrupts - Enable the interrupts from the PHY side
484  * @phydev: target phy_device struct
485  */
486 static int phy_enable_interrupts(struct phy_device *phydev)
487 {
488 	int err = phy_clear_interrupt(phydev);
489 
490 	if (err < 0)
491 		return err;
492 
493 	return phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
494 }
495 
496 /**
497  * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
498  * @phydev: target phy_device struct
499  */
500 static int phy_disable_interrupts(struct phy_device *phydev)
501 {
502 	int err;
503 
504 	/* Disable PHY interrupts */
505 	err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
506 	if (err)
507 		goto phy_err;
508 
509 	/* Clear the interrupt */
510 	err = phy_clear_interrupt(phydev);
511 	if (err)
512 		goto phy_err;
513 
514 	return 0;
515 
516 phy_err:
517 	phy_error(phydev);
518 
519 	return err;
520 }
521 
522 /**
523  * phy_start_interrupts - request and enable interrupts for a PHY device
524  * @phydev: target phy_device struct
525  *
526  * Description: Request the interrupt for the given PHY.
527  *   If this fails, then we set irq to PHY_POLL.
528  *   Otherwise, we enable the interrupts in the PHY.
529  *   This should only be called with a valid IRQ number.
530  *   Returns 0 on success or < 0 on error.
531  */
532 int phy_start_interrupts(struct phy_device *phydev)
533 {
534 	atomic_set(&phydev->irq_disable, 0);
535 	if (request_irq(phydev->irq, phy_interrupt, 0, "phy_interrupt",
536 			phydev) < 0) {
537 		pr_warn("%s: Can't get IRQ %d (PHY)\n",
538 			phydev->bus->name, phydev->irq);
539 		phydev->irq = PHY_POLL;
540 		return 0;
541 	}
542 
543 	return phy_enable_interrupts(phydev);
544 }
545 EXPORT_SYMBOL(phy_start_interrupts);
546 
547 /**
548  * phy_stop_interrupts - disable interrupts from a PHY device
549  * @phydev: target phy_device struct
550  */
551 int phy_stop_interrupts(struct phy_device *phydev)
552 {
553 	int err = phy_disable_interrupts(phydev);
554 
555 	if (err)
556 		phy_error(phydev);
557 
558 	free_irq(phydev->irq, phydev);
559 
560 	/* Cannot call flush_scheduled_work() here as desired because
561 	 * of rtnl_lock(), but we do not really care about what would
562 	 * be done, except from enable_irq(), so cancel any work
563 	 * possibly pending and take care of the matter below.
564 	 */
565 	cancel_work_sync(&phydev->phy_queue);
566 	/* If work indeed has been cancelled, disable_irq() will have
567 	 * been left unbalanced from phy_interrupt() and enable_irq()
568 	 * has to be called so that other devices on the line work.
569 	 */
570 	while (atomic_dec_return(&phydev->irq_disable) >= 0)
571 		enable_irq(phydev->irq);
572 
573 	return err;
574 }
575 EXPORT_SYMBOL(phy_stop_interrupts);
576 
577 /**
578  * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
579  * @work: work_struct that describes the work to be done
580  */
581 void phy_change(struct work_struct *work)
582 {
583 	struct phy_device *phydev =
584 		container_of(work, struct phy_device, phy_queue);
585 
586 	if (phydev->drv->did_interrupt &&
587 	    !phydev->drv->did_interrupt(phydev))
588 		goto ignore;
589 
590 	if (phy_disable_interrupts(phydev))
591 		goto phy_err;
592 
593 	mutex_lock(&phydev->lock);
594 	if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
595 		phydev->state = PHY_CHANGELINK;
596 	mutex_unlock(&phydev->lock);
597 
598 	atomic_dec(&phydev->irq_disable);
599 	enable_irq(phydev->irq);
600 
601 	/* Reenable interrupts */
602 	if (PHY_HALTED != phydev->state &&
603 	    phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
604 		goto irq_enable_err;
605 
606 	/* reschedule state queue work to run as soon as possible */
607 	cancel_delayed_work_sync(&phydev->state_queue);
608 	queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
609 	return;
610 
611 ignore:
612 	atomic_dec(&phydev->irq_disable);
613 	enable_irq(phydev->irq);
614 	return;
615 
616 irq_enable_err:
617 	disable_irq(phydev->irq);
618 	atomic_inc(&phydev->irq_disable);
619 phy_err:
620 	phy_error(phydev);
621 }
622 
623 /**
624  * phy_stop - Bring down the PHY link, and stop checking the status
625  * @phydev: target phy_device struct
626  */
627 void phy_stop(struct phy_device *phydev)
628 {
629 	mutex_lock(&phydev->lock);
630 
631 	if (PHY_HALTED == phydev->state)
632 		goto out_unlock;
633 
634 	if (phy_interrupt_is_valid(phydev)) {
635 		/* Disable PHY Interrupts */
636 		phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
637 
638 		/* Clear any pending interrupts */
639 		phy_clear_interrupt(phydev);
640 	}
641 
642 	phydev->state = PHY_HALTED;
643 
644 out_unlock:
645 	mutex_unlock(&phydev->lock);
646 
647 	/* Cannot call flush_scheduled_work() here as desired because
648 	 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
649 	 * will not reenable interrupts.
650 	 */
651 }
652 EXPORT_SYMBOL(phy_stop);
653 
654 /**
655  * phy_start - start or restart a PHY device
656  * @phydev: target phy_device struct
657  *
658  * Description: Indicates the attached device's readiness to
659  *   handle PHY-related work.  Used during startup to start the
660  *   PHY, and after a call to phy_stop() to resume operation.
661  *   Also used to indicate the MDIO bus has cleared an error
662  *   condition.
663  */
664 void phy_start(struct phy_device *phydev)
665 {
666 	mutex_lock(&phydev->lock);
667 
668 	switch (phydev->state) {
669 	case PHY_STARTING:
670 		phydev->state = PHY_PENDING;
671 		break;
672 	case PHY_READY:
673 		phydev->state = PHY_UP;
674 		break;
675 	case PHY_HALTED:
676 		phydev->state = PHY_RESUMING;
677 	default:
678 		break;
679 	}
680 	mutex_unlock(&phydev->lock);
681 }
682 EXPORT_SYMBOL(phy_start);
683 
684 /**
685  * phy_state_machine - Handle the state machine
686  * @work: work_struct that describes the work to be done
687  */
688 void phy_state_machine(struct work_struct *work)
689 {
690 	struct delayed_work *dwork = to_delayed_work(work);
691 	struct phy_device *phydev =
692 			container_of(dwork, struct phy_device, state_queue);
693 	int needs_aneg = 0, do_suspend = 0;
694 	int err = 0;
695 
696 	mutex_lock(&phydev->lock);
697 
698 	switch (phydev->state) {
699 	case PHY_DOWN:
700 	case PHY_STARTING:
701 	case PHY_READY:
702 	case PHY_PENDING:
703 		break;
704 	case PHY_UP:
705 		needs_aneg = 1;
706 
707 		phydev->link_timeout = PHY_AN_TIMEOUT;
708 
709 		break;
710 	case PHY_AN:
711 		err = phy_read_status(phydev);
712 		if (err < 0)
713 			break;
714 
715 		/* If the link is down, give up on negotiation for now */
716 		if (!phydev->link) {
717 			phydev->state = PHY_NOLINK;
718 			netif_carrier_off(phydev->attached_dev);
719 			phydev->adjust_link(phydev->attached_dev);
720 			break;
721 		}
722 
723 		/* Check if negotiation is done.  Break if there's an error */
724 		err = phy_aneg_done(phydev);
725 		if (err < 0)
726 			break;
727 
728 		/* If AN is done, we're running */
729 		if (err > 0) {
730 			phydev->state = PHY_RUNNING;
731 			netif_carrier_on(phydev->attached_dev);
732 			phydev->adjust_link(phydev->attached_dev);
733 
734 		} else if (0 == phydev->link_timeout--) {
735 			needs_aneg = 1;
736 			/* If we have the magic_aneg bit, we try again */
737 			if (phydev->drv->flags & PHY_HAS_MAGICANEG)
738 				break;
739 		}
740 		break;
741 	case PHY_NOLINK:
742 		err = phy_read_status(phydev);
743 		if (err)
744 			break;
745 
746 		if (phydev->link) {
747 			phydev->state = PHY_RUNNING;
748 			netif_carrier_on(phydev->attached_dev);
749 			phydev->adjust_link(phydev->attached_dev);
750 		}
751 		break;
752 	case PHY_FORCING:
753 		err = genphy_update_link(phydev);
754 		if (err)
755 			break;
756 
757 		if (phydev->link) {
758 			phydev->state = PHY_RUNNING;
759 			netif_carrier_on(phydev->attached_dev);
760 		} else {
761 			if (0 == phydev->link_timeout--)
762 				needs_aneg = 1;
763 		}
764 
765 		phydev->adjust_link(phydev->attached_dev);
766 		break;
767 	case PHY_RUNNING:
768 		/* Only register a CHANGE if we are
769 		 * polling or ignoring interrupts
770 		 */
771 		if (!phy_interrupt_is_valid(phydev))
772 			phydev->state = PHY_CHANGELINK;
773 		break;
774 	case PHY_CHANGELINK:
775 		err = phy_read_status(phydev);
776 		if (err)
777 			break;
778 
779 		if (phydev->link) {
780 			phydev->state = PHY_RUNNING;
781 			netif_carrier_on(phydev->attached_dev);
782 		} else {
783 			phydev->state = PHY_NOLINK;
784 			netif_carrier_off(phydev->attached_dev);
785 		}
786 
787 		phydev->adjust_link(phydev->attached_dev);
788 
789 		if (phy_interrupt_is_valid(phydev))
790 			err = phy_config_interrupt(phydev,
791 						   PHY_INTERRUPT_ENABLED);
792 		break;
793 	case PHY_HALTED:
794 		if (phydev->link) {
795 			phydev->link = 0;
796 			netif_carrier_off(phydev->attached_dev);
797 			phydev->adjust_link(phydev->attached_dev);
798 			do_suspend = 1;
799 		}
800 		break;
801 	case PHY_RESUMING:
802 		err = phy_clear_interrupt(phydev);
803 		if (err)
804 			break;
805 
806 		err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
807 		if (err)
808 			break;
809 
810 		if (AUTONEG_ENABLE == phydev->autoneg) {
811 			err = phy_aneg_done(phydev);
812 			if (err < 0)
813 				break;
814 
815 			/* err > 0 if AN is done.
816 			 * Otherwise, it's 0, and we're  still waiting for AN
817 			 */
818 			if (err > 0) {
819 				err = phy_read_status(phydev);
820 				if (err)
821 					break;
822 
823 				if (phydev->link) {
824 					phydev->state = PHY_RUNNING;
825 					netif_carrier_on(phydev->attached_dev);
826 				} else	{
827 					phydev->state = PHY_NOLINK;
828 				}
829 				phydev->adjust_link(phydev->attached_dev);
830 			} else {
831 				phydev->state = PHY_AN;
832 				phydev->link_timeout = PHY_AN_TIMEOUT;
833 			}
834 		} else {
835 			err = phy_read_status(phydev);
836 			if (err)
837 				break;
838 
839 			if (phydev->link) {
840 				phydev->state = PHY_RUNNING;
841 				netif_carrier_on(phydev->attached_dev);
842 			} else	{
843 				phydev->state = PHY_NOLINK;
844 			}
845 			phydev->adjust_link(phydev->attached_dev);
846 		}
847 		break;
848 	}
849 
850 	mutex_unlock(&phydev->lock);
851 
852 	if (needs_aneg)
853 		err = phy_start_aneg(phydev);
854 
855 	if (do_suspend)
856 		phy_suspend(phydev);
857 
858 	if (err < 0)
859 		phy_error(phydev);
860 
861 	queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
862 			   PHY_STATE_TIME * HZ);
863 }
864 
865 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
866 {
867 	cancel_work_sync(&phydev->phy_queue);
868 	phydev->link = new_link;
869 	schedule_work(&phydev->phy_queue);
870 }
871 EXPORT_SYMBOL(phy_mac_interrupt);
872 
873 static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
874 				    int addr)
875 {
876 	/* Write the desired MMD Devad */
877 	bus->write(bus, addr, MII_MMD_CTRL, devad);
878 
879 	/* Write the desired MMD register address */
880 	bus->write(bus, addr, MII_MMD_DATA, prtad);
881 
882 	/* Select the Function : DATA with no post increment */
883 	bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
884 }
885 
886 /**
887  * phy_read_mmd_indirect - reads data from the MMD registers
888  * @bus: the target MII bus
889  * @prtad: MMD Address
890  * @devad: MMD DEVAD
891  * @addr: PHY address on the MII bus
892  *
893  * Description: it reads data from the MMD registers (clause 22 to access to
894  * clause 45) of the specified phy address.
895  * To read these register we have:
896  * 1) Write reg 13 // DEVAD
897  * 2) Write reg 14 // MMD Address
898  * 3) Write reg 13 // MMD Data Command for MMD DEVAD
899  * 3) Read  reg 14 // Read MMD data
900  */
901 static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
902 				 int addr)
903 {
904 	mmd_phy_indirect(bus, prtad, devad, addr);
905 
906 	/* Read the content of the MMD's selected register */
907 	return bus->read(bus, addr, MII_MMD_DATA);
908 }
909 
910 /**
911  * phy_write_mmd_indirect - writes data to the MMD registers
912  * @bus: the target MII bus
913  * @prtad: MMD Address
914  * @devad: MMD DEVAD
915  * @addr: PHY address on the MII bus
916  * @data: data to write in the MMD register
917  *
918  * Description: Write data from the MMD registers of the specified
919  * phy address.
920  * To write these register we have:
921  * 1) Write reg 13 // DEVAD
922  * 2) Write reg 14 // MMD Address
923  * 3) Write reg 13 // MMD Data Command for MMD DEVAD
924  * 3) Write reg 14 // Write MMD data
925  */
926 static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
927 				   int addr, u32 data)
928 {
929 	mmd_phy_indirect(bus, prtad, devad, addr);
930 
931 	/* Write the data into MMD's selected register */
932 	bus->write(bus, addr, MII_MMD_DATA, data);
933 }
934 
935 /**
936  * phy_init_eee - init and check the EEE feature
937  * @phydev: target phy_device struct
938  * @clk_stop_enable: PHY may stop the clock during LPI
939  *
940  * Description: it checks if the Energy-Efficient Ethernet (EEE)
941  * is supported by looking at the MMD registers 3.20 and 7.60/61
942  * and it programs the MMD register 3.0 setting the "Clock stop enable"
943  * bit if required.
944  */
945 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
946 {
947 	/* According to 802.3az,the EEE is supported only in full duplex-mode.
948 	 * Also EEE feature is active when core is operating with MII, GMII
949 	 * or RGMII.
950 	 */
951 	if ((phydev->duplex == DUPLEX_FULL) &&
952 	    ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
953 	    (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
954 	    (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
955 		int eee_lp, eee_cap, eee_adv;
956 		u32 lp, cap, adv;
957 		int idx, status;
958 
959 		/* Read phy status to properly get the right settings */
960 		status = phy_read_status(phydev);
961 		if (status)
962 			return status;
963 
964 		/* First check if the EEE ability is supported */
965 		eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
966 						MDIO_MMD_PCS, phydev->addr);
967 		if (eee_cap < 0)
968 			return eee_cap;
969 
970 		cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
971 		if (!cap)
972 			return -EPROTONOSUPPORT;
973 
974 		/* Check which link settings negotiated and verify it in
975 		 * the EEE advertising registers.
976 		 */
977 		eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
978 					       MDIO_MMD_AN, phydev->addr);
979 		if (eee_lp < 0)
980 			return eee_lp;
981 
982 		eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
983 						MDIO_MMD_AN, phydev->addr);
984 		if (eee_adv < 0)
985 			return eee_adv;
986 
987 		adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
988 		lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
989 		idx = phy_find_setting(phydev->speed, phydev->duplex);
990 		if (!(lp & adv & settings[idx].setting))
991 			return -EPROTONOSUPPORT;
992 
993 		if (clk_stop_enable) {
994 			/* Configure the PHY to stop receiving xMII
995 			 * clock while it is signaling LPI.
996 			 */
997 			int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
998 							MDIO_MMD_PCS,
999 							phydev->addr);
1000 			if (val < 0)
1001 				return val;
1002 
1003 			val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1004 			phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
1005 					       MDIO_MMD_PCS, phydev->addr, val);
1006 		}
1007 
1008 		return 0; /* EEE supported */
1009 	}
1010 
1011 	return -EPROTONOSUPPORT;
1012 }
1013 EXPORT_SYMBOL(phy_init_eee);
1014 
1015 /**
1016  * phy_get_eee_err - report the EEE wake error count
1017  * @phydev: target phy_device struct
1018  *
1019  * Description: it is to report the number of time where the PHY
1020  * failed to complete its normal wake sequence.
1021  */
1022 int phy_get_eee_err(struct phy_device *phydev)
1023 {
1024 	return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
1025 				     MDIO_MMD_PCS, phydev->addr);
1026 }
1027 EXPORT_SYMBOL(phy_get_eee_err);
1028 
1029 /**
1030  * phy_ethtool_get_eee - get EEE supported and status
1031  * @phydev: target phy_device struct
1032  * @data: ethtool_eee data
1033  *
1034  * Description: it reportes the Supported/Advertisement/LP Advertisement
1035  * capabilities.
1036  */
1037 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1038 {
1039 	int val;
1040 
1041 	/* Get Supported EEE */
1042 	val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
1043 				    MDIO_MMD_PCS, phydev->addr);
1044 	if (val < 0)
1045 		return val;
1046 	data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1047 
1048 	/* Get advertisement EEE */
1049 	val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
1050 				    MDIO_MMD_AN, phydev->addr);
1051 	if (val < 0)
1052 		return val;
1053 	data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1054 
1055 	/* Get LP advertisement EEE */
1056 	val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
1057 				    MDIO_MMD_AN, phydev->addr);
1058 	if (val < 0)
1059 		return val;
1060 	data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1061 
1062 	return 0;
1063 }
1064 EXPORT_SYMBOL(phy_ethtool_get_eee);
1065 
1066 /**
1067  * phy_ethtool_set_eee - set EEE supported and status
1068  * @phydev: target phy_device struct
1069  * @data: ethtool_eee data
1070  *
1071  * Description: it is to program the Advertisement EEE register.
1072  */
1073 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1074 {
1075 	int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
1076 
1077 	phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
1078 			       phydev->addr, val);
1079 
1080 	return 0;
1081 }
1082 EXPORT_SYMBOL(phy_ethtool_set_eee);
1083 
1084 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1085 {
1086 	if (phydev->drv->set_wol)
1087 		return phydev->drv->set_wol(phydev, wol);
1088 
1089 	return -EOPNOTSUPP;
1090 }
1091 EXPORT_SYMBOL(phy_ethtool_set_wol);
1092 
1093 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1094 {
1095 	if (phydev->drv->get_wol)
1096 		phydev->drv->get_wol(phydev, wol);
1097 }
1098 EXPORT_SYMBOL(phy_ethtool_get_wol);
1099