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