]> Pileus Git - ~andy/linux/blob - drivers/net/phy/phy.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[~andy/linux] / 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, 0, "phy_interrupt",
537                         phydev) < 0) {
538                 pr_warn("%s: Can't get IRQ %d (PHY)\n",
539                         phydev->bus->name, phydev->irq);
540                 phydev->irq = PHY_POLL;
541                 return 0;
542         }
543
544         return phy_enable_interrupts(phydev);
545 }
546 EXPORT_SYMBOL(phy_start_interrupts);
547
548 /**
549  * phy_stop_interrupts - disable interrupts from a PHY device
550  * @phydev: target phy_device struct
551  */
552 int phy_stop_interrupts(struct phy_device *phydev)
553 {
554         int err = phy_disable_interrupts(phydev);
555
556         if (err)
557                 phy_error(phydev);
558
559         free_irq(phydev->irq, phydev);
560
561         /* Cannot call flush_scheduled_work() here as desired because
562          * of rtnl_lock(), but we do not really care about what would
563          * be done, except from enable_irq(), so cancel any work
564          * possibly pending and take care of the matter below.
565          */
566         cancel_work_sync(&phydev->phy_queue);
567         /* If work indeed has been cancelled, disable_irq() will have
568          * been left unbalanced from phy_interrupt() and enable_irq()
569          * has to be called so that other devices on the line work.
570          */
571         while (atomic_dec_return(&phydev->irq_disable) >= 0)
572                 enable_irq(phydev->irq);
573
574         return err;
575 }
576 EXPORT_SYMBOL(phy_stop_interrupts);
577
578 /**
579  * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
580  * @work: work_struct that describes the work to be done
581  */
582 void phy_change(struct work_struct *work)
583 {
584         struct phy_device *phydev =
585                 container_of(work, struct phy_device, phy_queue);
586
587         if (phydev->drv->did_interrupt &&
588             !phydev->drv->did_interrupt(phydev))
589                 goto ignore;
590
591         if (phy_disable_interrupts(phydev))
592                 goto phy_err;
593
594         mutex_lock(&phydev->lock);
595         if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
596                 phydev->state = PHY_CHANGELINK;
597         mutex_unlock(&phydev->lock);
598
599         atomic_dec(&phydev->irq_disable);
600         enable_irq(phydev->irq);
601
602         /* Reenable interrupts */
603         if (PHY_HALTED != phydev->state &&
604             phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED))
605                 goto irq_enable_err;
606
607         /* reschedule state queue work to run as soon as possible */
608         cancel_delayed_work_sync(&phydev->state_queue);
609         queue_delayed_work(system_power_efficient_wq, &phydev->state_queue, 0);
610         return;
611
612 ignore:
613         atomic_dec(&phydev->irq_disable);
614         enable_irq(phydev->irq);
615         return;
616
617 irq_enable_err:
618         disable_irq(phydev->irq);
619         atomic_inc(&phydev->irq_disable);
620 phy_err:
621         phy_error(phydev);
622 }
623
624 /**
625  * phy_stop - Bring down the PHY link, and stop checking the status
626  * @phydev: target phy_device struct
627  */
628 void phy_stop(struct phy_device *phydev)
629 {
630         mutex_lock(&phydev->lock);
631
632         if (PHY_HALTED == phydev->state)
633                 goto out_unlock;
634
635         if (phy_interrupt_is_valid(phydev)) {
636                 /* Disable PHY Interrupts */
637                 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
638
639                 /* Clear any pending interrupts */
640                 phy_clear_interrupt(phydev);
641         }
642
643         phydev->state = PHY_HALTED;
644
645 out_unlock:
646         mutex_unlock(&phydev->lock);
647
648         /* Cannot call flush_scheduled_work() here as desired because
649          * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
650          * will not reenable interrupts.
651          */
652 }
653 EXPORT_SYMBOL(phy_stop);
654
655 /**
656  * phy_start - start or restart a PHY device
657  * @phydev: target phy_device struct
658  *
659  * Description: Indicates the attached device's readiness to
660  *   handle PHY-related work.  Used during startup to start the
661  *   PHY, and after a call to phy_stop() to resume operation.
662  *   Also used to indicate the MDIO bus has cleared an error
663  *   condition.
664  */
665 void phy_start(struct phy_device *phydev)
666 {
667         mutex_lock(&phydev->lock);
668
669         switch (phydev->state) {
670         case PHY_STARTING:
671                 phydev->state = PHY_PENDING;
672                 break;
673         case PHY_READY:
674                 phydev->state = PHY_UP;
675                 break;
676         case PHY_HALTED:
677                 phydev->state = PHY_RESUMING;
678         default:
679                 break;
680         }
681         mutex_unlock(&phydev->lock);
682 }
683 EXPORT_SYMBOL(phy_start);
684
685 /**
686  * phy_state_machine - Handle the state machine
687  * @work: work_struct that describes the work to be done
688  */
689 void phy_state_machine(struct work_struct *work)
690 {
691         struct delayed_work *dwork = to_delayed_work(work);
692         struct phy_device *phydev =
693                         container_of(dwork, struct phy_device, state_queue);
694         int needs_aneg = 0, do_suspend = 0;
695         int err = 0;
696
697         mutex_lock(&phydev->lock);
698
699         switch (phydev->state) {
700         case PHY_DOWN:
701         case PHY_STARTING:
702         case PHY_READY:
703         case PHY_PENDING:
704                 break;
705         case PHY_UP:
706                 needs_aneg = 1;
707
708                 phydev->link_timeout = PHY_AN_TIMEOUT;
709
710                 break;
711         case PHY_AN:
712                 err = phy_read_status(phydev);
713                 if (err < 0)
714                         break;
715
716                 /* If the link is down, give up on negotiation for now */
717                 if (!phydev->link) {
718                         phydev->state = PHY_NOLINK;
719                         netif_carrier_off(phydev->attached_dev);
720                         phydev->adjust_link(phydev->attached_dev);
721                         break;
722                 }
723
724                 /* Check if negotiation is done.  Break if there's an error */
725                 err = phy_aneg_done(phydev);
726                 if (err < 0)
727                         break;
728
729                 /* If AN is done, we're running */
730                 if (err > 0) {
731                         phydev->state = PHY_RUNNING;
732                         netif_carrier_on(phydev->attached_dev);
733                         phydev->adjust_link(phydev->attached_dev);
734
735                 } else if (0 == phydev->link_timeout--) {
736                         needs_aneg = 1;
737                         /* If we have the magic_aneg bit, we try again */
738                         if (phydev->drv->flags & PHY_HAS_MAGICANEG)
739                                 break;
740                 }
741                 break;
742         case PHY_NOLINK:
743                 err = phy_read_status(phydev);
744                 if (err)
745                         break;
746
747                 if (phydev->link) {
748                         phydev->state = PHY_RUNNING;
749                         netif_carrier_on(phydev->attached_dev);
750                         phydev->adjust_link(phydev->attached_dev);
751                 }
752                 break;
753         case PHY_FORCING:
754                 err = genphy_update_link(phydev);
755                 if (err)
756                         break;
757
758                 if (phydev->link) {
759                         phydev->state = PHY_RUNNING;
760                         netif_carrier_on(phydev->attached_dev);
761                 } else {
762                         if (0 == phydev->link_timeout--)
763                                 needs_aneg = 1;
764                 }
765
766                 phydev->adjust_link(phydev->attached_dev);
767                 break;
768         case PHY_RUNNING:
769                 /* Only register a CHANGE if we are
770                  * polling or ignoring interrupts
771                  */
772                 if (!phy_interrupt_is_valid(phydev))
773                         phydev->state = PHY_CHANGELINK;
774                 break;
775         case PHY_CHANGELINK:
776                 err = phy_read_status(phydev);
777                 if (err)
778                         break;
779
780                 if (phydev->link) {
781                         phydev->state = PHY_RUNNING;
782                         netif_carrier_on(phydev->attached_dev);
783                 } else {
784                         phydev->state = PHY_NOLINK;
785                         netif_carrier_off(phydev->attached_dev);
786                 }
787
788                 phydev->adjust_link(phydev->attached_dev);
789
790                 if (phy_interrupt_is_valid(phydev))
791                         err = phy_config_interrupt(phydev,
792                                                    PHY_INTERRUPT_ENABLED);
793                 break;
794         case PHY_HALTED:
795                 if (phydev->link) {
796                         phydev->link = 0;
797                         netif_carrier_off(phydev->attached_dev);
798                         phydev->adjust_link(phydev->attached_dev);
799                         do_suspend = 1;
800                 }
801                 break;
802         case PHY_RESUMING:
803                 err = phy_clear_interrupt(phydev);
804                 if (err)
805                         break;
806
807                 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
808                 if (err)
809                         break;
810
811                 if (AUTONEG_ENABLE == phydev->autoneg) {
812                         err = phy_aneg_done(phydev);
813                         if (err < 0)
814                                 break;
815
816                         /* err > 0 if AN is done.
817                          * Otherwise, it's 0, and we're  still waiting for AN
818                          */
819                         if (err > 0) {
820                                 err = phy_read_status(phydev);
821                                 if (err)
822                                         break;
823
824                                 if (phydev->link) {
825                                         phydev->state = PHY_RUNNING;
826                                         netif_carrier_on(phydev->attached_dev);
827                                 } else  {
828                                         phydev->state = PHY_NOLINK;
829                                 }
830                                 phydev->adjust_link(phydev->attached_dev);
831                         } else {
832                                 phydev->state = PHY_AN;
833                                 phydev->link_timeout = PHY_AN_TIMEOUT;
834                         }
835                 } else {
836                         err = phy_read_status(phydev);
837                         if (err)
838                                 break;
839
840                         if (phydev->link) {
841                                 phydev->state = PHY_RUNNING;
842                                 netif_carrier_on(phydev->attached_dev);
843                         } else  {
844                                 phydev->state = PHY_NOLINK;
845                         }
846                         phydev->adjust_link(phydev->attached_dev);
847                 }
848                 break;
849         }
850
851         mutex_unlock(&phydev->lock);
852
853         if (needs_aneg)
854                 err = phy_start_aneg(phydev);
855
856         if (do_suspend)
857                 phy_suspend(phydev);
858
859         if (err < 0)
860                 phy_error(phydev);
861
862         queue_delayed_work(system_power_efficient_wq, &phydev->state_queue,
863                            PHY_STATE_TIME * HZ);
864 }
865
866 void phy_mac_interrupt(struct phy_device *phydev, int new_link)
867 {
868         cancel_work_sync(&phydev->phy_queue);
869         phydev->link = new_link;
870         schedule_work(&phydev->phy_queue);
871 }
872 EXPORT_SYMBOL(phy_mac_interrupt);
873
874 static inline void mmd_phy_indirect(struct mii_bus *bus, int prtad, int devad,
875                                     int addr)
876 {
877         /* Write the desired MMD Devad */
878         bus->write(bus, addr, MII_MMD_CTRL, devad);
879
880         /* Write the desired MMD register address */
881         bus->write(bus, addr, MII_MMD_DATA, prtad);
882
883         /* Select the Function : DATA with no post increment */
884         bus->write(bus, addr, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR));
885 }
886
887 /**
888  * phy_read_mmd_indirect - reads data from the MMD registers
889  * @bus: the target MII bus
890  * @prtad: MMD Address
891  * @devad: MMD DEVAD
892  * @addr: PHY address on the MII bus
893  *
894  * Description: it reads data from the MMD registers (clause 22 to access to
895  * clause 45) of the specified phy address.
896  * To read these register we have:
897  * 1) Write reg 13 // DEVAD
898  * 2) Write reg 14 // MMD Address
899  * 3) Write reg 13 // MMD Data Command for MMD DEVAD
900  * 3) Read  reg 14 // Read MMD data
901  */
902 static int phy_read_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
903                                  int addr)
904 {
905         mmd_phy_indirect(bus, prtad, devad, addr);
906
907         /* Read the content of the MMD's selected register */
908         return bus->read(bus, addr, MII_MMD_DATA);
909 }
910
911 /**
912  * phy_write_mmd_indirect - writes data to the MMD registers
913  * @bus: the target MII bus
914  * @prtad: MMD Address
915  * @devad: MMD DEVAD
916  * @addr: PHY address on the MII bus
917  * @data: data to write in the MMD register
918  *
919  * Description: Write data from the MMD registers of the specified
920  * phy address.
921  * To write these register we have:
922  * 1) Write reg 13 // DEVAD
923  * 2) Write reg 14 // MMD Address
924  * 3) Write reg 13 // MMD Data Command for MMD DEVAD
925  * 3) Write reg 14 // Write MMD data
926  */
927 static void phy_write_mmd_indirect(struct mii_bus *bus, int prtad, int devad,
928                                    int addr, u32 data)
929 {
930         mmd_phy_indirect(bus, prtad, devad, addr);
931
932         /* Write the data into MMD's selected register */
933         bus->write(bus, addr, MII_MMD_DATA, data);
934 }
935
936 /**
937  * phy_init_eee - init and check the EEE feature
938  * @phydev: target phy_device struct
939  * @clk_stop_enable: PHY may stop the clock during LPI
940  *
941  * Description: it checks if the Energy-Efficient Ethernet (EEE)
942  * is supported by looking at the MMD registers 3.20 and 7.60/61
943  * and it programs the MMD register 3.0 setting the "Clock stop enable"
944  * bit if required.
945  */
946 int phy_init_eee(struct phy_device *phydev, bool clk_stop_enable)
947 {
948         /* According to 802.3az,the EEE is supported only in full duplex-mode.
949          * Also EEE feature is active when core is operating with MII, GMII
950          * or RGMII.
951          */
952         if ((phydev->duplex == DUPLEX_FULL) &&
953             ((phydev->interface == PHY_INTERFACE_MODE_MII) ||
954             (phydev->interface == PHY_INTERFACE_MODE_GMII) ||
955             (phydev->interface == PHY_INTERFACE_MODE_RGMII))) {
956                 int eee_lp, eee_cap, eee_adv;
957                 u32 lp, cap, adv;
958                 int idx, status;
959
960                 /* Read phy status to properly get the right settings */
961                 status = phy_read_status(phydev);
962                 if (status)
963                         return status;
964
965                 /* First check if the EEE ability is supported */
966                 eee_cap = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
967                                                 MDIO_MMD_PCS, phydev->addr);
968                 if (eee_cap < 0)
969                         return eee_cap;
970
971                 cap = mmd_eee_cap_to_ethtool_sup_t(eee_cap);
972                 if (!cap)
973                         return -EPROTONOSUPPORT;
974
975                 /* Check which link settings negotiated and verify it in
976                  * the EEE advertising registers.
977                  */
978                 eee_lp = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
979                                                MDIO_MMD_AN, phydev->addr);
980                 if (eee_lp < 0)
981                         return eee_lp;
982
983                 eee_adv = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
984                                                 MDIO_MMD_AN, phydev->addr);
985                 if (eee_adv < 0)
986                         return eee_adv;
987
988                 adv = mmd_eee_adv_to_ethtool_adv_t(eee_adv);
989                 lp = mmd_eee_adv_to_ethtool_adv_t(eee_lp);
990                 idx = phy_find_setting(phydev->speed, phydev->duplex);
991                 if (!(lp & adv & settings[idx].setting))
992                         return -EPROTONOSUPPORT;
993
994                 if (clk_stop_enable) {
995                         /* Configure the PHY to stop receiving xMII
996                          * clock while it is signaling LPI.
997                          */
998                         int val = phy_read_mmd_indirect(phydev->bus, MDIO_CTRL1,
999                                                         MDIO_MMD_PCS,
1000                                                         phydev->addr);
1001                         if (val < 0)
1002                                 return val;
1003
1004                         val |= MDIO_PCS_CTRL1_CLKSTOP_EN;
1005                         phy_write_mmd_indirect(phydev->bus, MDIO_CTRL1,
1006                                                MDIO_MMD_PCS, phydev->addr, val);
1007                 }
1008
1009                 return 0; /* EEE supported */
1010         }
1011
1012         return -EPROTONOSUPPORT;
1013 }
1014 EXPORT_SYMBOL(phy_init_eee);
1015
1016 /**
1017  * phy_get_eee_err - report the EEE wake error count
1018  * @phydev: target phy_device struct
1019  *
1020  * Description: it is to report the number of time where the PHY
1021  * failed to complete its normal wake sequence.
1022  */
1023 int phy_get_eee_err(struct phy_device *phydev)
1024 {
1025         return phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_WK_ERR,
1026                                      MDIO_MMD_PCS, phydev->addr);
1027 }
1028 EXPORT_SYMBOL(phy_get_eee_err);
1029
1030 /**
1031  * phy_ethtool_get_eee - get EEE supported and status
1032  * @phydev: target phy_device struct
1033  * @data: ethtool_eee data
1034  *
1035  * Description: it reportes the Supported/Advertisement/LP Advertisement
1036  * capabilities.
1037  */
1038 int phy_ethtool_get_eee(struct phy_device *phydev, struct ethtool_eee *data)
1039 {
1040         int val;
1041
1042         /* Get Supported EEE */
1043         val = phy_read_mmd_indirect(phydev->bus, MDIO_PCS_EEE_ABLE,
1044                                     MDIO_MMD_PCS, phydev->addr);
1045         if (val < 0)
1046                 return val;
1047         data->supported = mmd_eee_cap_to_ethtool_sup_t(val);
1048
1049         /* Get advertisement EEE */
1050         val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV,
1051                                     MDIO_MMD_AN, phydev->addr);
1052         if (val < 0)
1053                 return val;
1054         data->advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1055
1056         /* Get LP advertisement EEE */
1057         val = phy_read_mmd_indirect(phydev->bus, MDIO_AN_EEE_LPABLE,
1058                                     MDIO_MMD_AN, phydev->addr);
1059         if (val < 0)
1060                 return val;
1061         data->lp_advertised = mmd_eee_adv_to_ethtool_adv_t(val);
1062
1063         return 0;
1064 }
1065 EXPORT_SYMBOL(phy_ethtool_get_eee);
1066
1067 /**
1068  * phy_ethtool_set_eee - set EEE supported and status
1069  * @phydev: target phy_device struct
1070  * @data: ethtool_eee data
1071  *
1072  * Description: it is to program the Advertisement EEE register.
1073  */
1074 int phy_ethtool_set_eee(struct phy_device *phydev, struct ethtool_eee *data)
1075 {
1076         int val = ethtool_adv_to_mmd_eee_adv_t(data->advertised);
1077
1078         phy_write_mmd_indirect(phydev->bus, MDIO_AN_EEE_ADV, MDIO_MMD_AN,
1079                                phydev->addr, val);
1080
1081         return 0;
1082 }
1083 EXPORT_SYMBOL(phy_ethtool_set_eee);
1084
1085 int phy_ethtool_set_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1086 {
1087         if (phydev->drv->set_wol)
1088                 return phydev->drv->set_wol(phydev, wol);
1089
1090         return -EOPNOTSUPP;
1091 }
1092 EXPORT_SYMBOL(phy_ethtool_set_wol);
1093
1094 void phy_ethtool_get_wol(struct phy_device *phydev, struct ethtool_wolinfo *wol)
1095 {
1096         if (phydev->drv->get_wol)
1097                 phydev->drv->get_wol(phydev, wol);
1098 }
1099 EXPORT_SYMBOL(phy_ethtool_get_wol);