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