]> err.no Git - linux-2.6/blob - drivers/net/phy/phy.c
Merge upstream into 'upstream' branch of netdev-2.6.git.
[linux-2.6] / drivers / net / phy / phy.c
1 /*
2  * drivers/net/phy/phy.c
3  *
4  * Framework for configuring and reading PHY devices
5  * Based on code in sungem_phy.c and gianfar_phy.c
6  *
7  * Author: Andy Fleming
8  *
9  * Copyright (c) 2004 Freescale Semiconductor, Inc.
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
15  *
16  */
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
31 #include <linux/mm.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/phy.h>
37
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #include <asm/uaccess.h>
41
42 static void phy_timer(unsigned long data);
43 static int phy_disable_interrupts(struct phy_device *phydev);
44 static void phy_sanitize_settings(struct phy_device *phydev);
45 static int phy_stop_interrupts(struct phy_device *phydev);
46
47
48 /* Convenience functions for reading/writing a given PHY
49  * register. They MUST NOT be called from interrupt context,
50  * because the bus read/write functions may wait for an interrupt
51  * to conclude the operation. */
52 int phy_read(struct phy_device *phydev, u16 regnum)
53 {
54         int retval;
55         struct mii_bus *bus = phydev->bus;
56
57         spin_lock_bh(&bus->mdio_lock);
58         retval = bus->read(bus, phydev->addr, regnum);
59         spin_unlock_bh(&bus->mdio_lock);
60
61         return retval;
62 }
63 EXPORT_SYMBOL(phy_read);
64
65 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
66 {
67         int err;
68         struct mii_bus *bus = phydev->bus;
69
70         spin_lock_bh(&bus->mdio_lock);
71         err = bus->write(bus, phydev->addr, regnum, val);
72         spin_unlock_bh(&bus->mdio_lock);
73
74         return err;
75 }
76 EXPORT_SYMBOL(phy_write);
77
78
79 int phy_clear_interrupt(struct phy_device *phydev)
80 {
81         int err = 0;
82
83         if (phydev->drv->ack_interrupt)
84                 err = phydev->drv->ack_interrupt(phydev);
85
86         return err;
87 }
88
89
90 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
91 {
92         int err = 0;
93
94         phydev->interrupts = interrupts;
95         if (phydev->drv->config_intr)
96                 err = phydev->drv->config_intr(phydev);
97
98         return err;
99 }
100
101
102 /* phy_aneg_done
103  *
104  * description: Reads the status register and returns 0 either if
105  *   auto-negotiation is incomplete, or if there was an error.
106  *   Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
107  */
108 static inline int phy_aneg_done(struct phy_device *phydev)
109 {
110         int retval;
111
112         retval = phy_read(phydev, MII_BMSR);
113
114         return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
115 }
116
117 /* phy_start_aneg
118  *
119  * description: Calls the PHY driver's config_aneg, and then
120  *   sets the PHY state to PHY_AN if auto-negotiation is enabled,
121  *   and to PHY_FORCING if auto-negotiation is disabled. Unless
122  *   the PHY is currently HALTED.
123  */
124 static int phy_start_aneg(struct phy_device *phydev)
125 {
126         int err;
127
128         spin_lock(&phydev->lock);
129
130         if (AUTONEG_DISABLE == phydev->autoneg)
131                 phy_sanitize_settings(phydev);
132
133         err = phydev->drv->config_aneg(phydev);
134
135         if (err < 0)
136                 goto out_unlock;
137
138         if (phydev->state != PHY_HALTED) {
139                 if (AUTONEG_ENABLE == phydev->autoneg) {
140                         phydev->state = PHY_AN;
141                         phydev->link_timeout = PHY_AN_TIMEOUT;
142                 } else {
143                         phydev->state = PHY_FORCING;
144                         phydev->link_timeout = PHY_FORCE_TIMEOUT;
145                 }
146         }
147
148 out_unlock:
149         spin_unlock(&phydev->lock);
150         return err;
151 }
152
153 /* A structure for mapping a particular speed and duplex
154  * combination to a particular SUPPORTED and ADVERTISED value */
155 struct phy_setting {
156         int speed;
157         int duplex;
158         u32 setting;
159 };
160
161 /* A mapping of all SUPPORTED settings to speed/duplex */
162 static struct phy_setting settings[] = {
163         {
164                 .speed = 10000,
165                 .duplex = DUPLEX_FULL,
166                 .setting = SUPPORTED_10000baseT_Full,
167         },
168         {
169                 .speed = SPEED_1000,
170                 .duplex = DUPLEX_FULL,
171                 .setting = SUPPORTED_1000baseT_Full,
172         },
173         {
174                 .speed = SPEED_1000,
175                 .duplex = DUPLEX_HALF,
176                 .setting = SUPPORTED_1000baseT_Half,
177         },
178         {
179                 .speed = SPEED_100,
180                 .duplex = DUPLEX_FULL,
181                 .setting = SUPPORTED_100baseT_Full,
182         },
183         {
184                 .speed = SPEED_100,
185                 .duplex = DUPLEX_HALF,
186                 .setting = SUPPORTED_100baseT_Half,
187         },
188         {
189                 .speed = SPEED_10,
190                 .duplex = DUPLEX_FULL,
191                 .setting = SUPPORTED_10baseT_Full,
192         },
193         {
194                 .speed = SPEED_10,
195                 .duplex = DUPLEX_HALF,
196                 .setting = SUPPORTED_10baseT_Half,
197         },
198 };
199
200 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
201
202 /* phy_find_setting
203  *
204  * description: Searches the settings array for the setting which
205  *   matches the desired speed and duplex, and returns the index
206  *   of that setting.  Returns the index of the last setting if
207  *   none of the others match.
208  */
209 static inline int phy_find_setting(int speed, int duplex)
210 {
211         int idx = 0;
212
213         while (idx < ARRAY_SIZE(settings) &&
214                         (settings[idx].speed != speed ||
215                         settings[idx].duplex != duplex))
216                 idx++;
217
218         return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
219 }
220
221 /* phy_find_valid
222  * idx: The first index in settings[] to search
223  * features: A mask of the valid settings
224  *
225  * description: Returns the index of the first valid setting less
226  *   than or equal to the one pointed to by idx, as determined by
227  *   the mask in features.  Returns the index of the last setting
228  *   if nothing else matches.
229  */
230 static inline int phy_find_valid(int idx, u32 features)
231 {
232         while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
233                 idx++;
234
235         return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
236 }
237
238 /* phy_sanitize_settings
239  *
240  * description: Make sure the PHY is set to supported speeds and
241  *   duplexes.  Drop down by one in this order:  1000/FULL,
242  *   1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
243  */
244 static void phy_sanitize_settings(struct phy_device *phydev)
245 {
246         u32 features = phydev->supported;
247         int idx;
248
249         /* Sanitize settings based on PHY capabilities */
250         if ((features & SUPPORTED_Autoneg) == 0)
251                 phydev->autoneg = 0;
252
253         idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
254                         features);
255
256         phydev->speed = settings[idx].speed;
257         phydev->duplex = settings[idx].duplex;
258 }
259
260 /* phy_force_reduction
261  *
262  * description: Reduces the speed/duplex settings by
263  *   one notch.  The order is so:
264  *   1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
265  *   10/FULL, 10/HALF.  The function bottoms out at 10/HALF.
266  */
267 static void phy_force_reduction(struct phy_device *phydev)
268 {
269         int idx;
270
271         idx = phy_find_setting(phydev->speed, phydev->duplex);
272         
273         idx++;
274
275         idx = phy_find_valid(idx, phydev->supported);
276
277         phydev->speed = settings[idx].speed;
278         phydev->duplex = settings[idx].duplex;
279
280         pr_info("Trying %d/%s\n", phydev->speed,
281                         DUPLEX_FULL == phydev->duplex ?
282                         "FULL" : "HALF");
283 }
284
285 /* phy_ethtool_sset:
286  * A generic ethtool sset function.  Handles all the details
287  *
288  * A few notes about parameter checking:
289  * - We don't set port or transceiver, so we don't care what they
290  *   were set to.
291  * - phy_start_aneg() will make sure forced settings are sane, and
292  *   choose the next best ones from the ones selected, so we don't
293  *   care if ethtool tries to give us bad values
294  */
295 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
296 {
297         if (cmd->phy_address != phydev->addr)
298                 return -EINVAL;
299
300         /* We make sure that we don't pass unsupported
301          * values in to the PHY */
302         cmd->advertising &= phydev->supported;
303
304         /* Verify the settings we care about. */
305         if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
306                 return -EINVAL;
307
308         if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
309                 return -EINVAL;
310
311         if (cmd->autoneg == AUTONEG_DISABLE
312                         && ((cmd->speed != SPEED_1000
313                                         && cmd->speed != SPEED_100
314                                         && cmd->speed != SPEED_10)
315                                 || (cmd->duplex != DUPLEX_HALF
316                                         && cmd->duplex != DUPLEX_FULL)))
317                 return -EINVAL;
318
319         phydev->autoneg = cmd->autoneg;
320
321         phydev->speed = cmd->speed;
322
323         phydev->advertising = cmd->advertising;
324
325         if (AUTONEG_ENABLE == cmd->autoneg)
326                 phydev->advertising |= ADVERTISED_Autoneg;
327         else
328                 phydev->advertising &= ~ADVERTISED_Autoneg;
329
330         phydev->duplex = cmd->duplex;
331
332         /* Restart the PHY */
333         phy_start_aneg(phydev);
334
335         return 0;
336 }
337
338 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
339 {
340         cmd->supported = phydev->supported;
341
342         cmd->advertising = phydev->advertising;
343
344         cmd->speed = phydev->speed;
345         cmd->duplex = phydev->duplex;
346         cmd->port = PORT_MII;
347         cmd->phy_address = phydev->addr;
348         cmd->transceiver = XCVR_EXTERNAL;
349         cmd->autoneg = phydev->autoneg;
350
351         return 0;
352 }
353
354
355 /* Note that this function is currently incompatible with the
356  * PHYCONTROL layer.  It changes registers without regard to
357  * current state.  Use at own risk
358  */
359 int phy_mii_ioctl(struct phy_device *phydev,
360                 struct mii_ioctl_data *mii_data, int cmd)
361 {
362         u16 val = mii_data->val_in;
363
364         switch (cmd) {
365         case SIOCGMIIPHY:
366                 mii_data->phy_id = phydev->addr;
367                 break;
368         case SIOCGMIIREG:
369                 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
370                 break;
371
372         case SIOCSMIIREG:
373                 if (!capable(CAP_NET_ADMIN))
374                         return -EPERM;
375
376                 if (mii_data->phy_id == phydev->addr) {
377                         switch(mii_data->reg_num) {
378                         case MII_BMCR:
379                                 if (val & (BMCR_RESET|BMCR_ANENABLE))
380                                         phydev->autoneg = AUTONEG_DISABLE;
381                                 else
382                                         phydev->autoneg = AUTONEG_ENABLE;
383                                 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
384                                         phydev->duplex = DUPLEX_FULL;
385                                 else
386                                         phydev->duplex = DUPLEX_HALF;
387                                 break;
388                         case MII_ADVERTISE:
389                                 phydev->advertising = val;
390                                 break;
391                         default:
392                                 /* do nothing */
393                                 break;
394                         }
395                 }
396
397                 phy_write(phydev, mii_data->reg_num, val);
398                 
399                 if (mii_data->reg_num == MII_BMCR 
400                                 && val & BMCR_RESET
401                                 && phydev->drv->config_init)
402                         phydev->drv->config_init(phydev);
403                 break;
404         }
405
406         return 0;
407 }
408
409 /* phy_start_machine:
410  *
411  * description: The PHY infrastructure can run a state machine
412  *   which tracks whether the PHY is starting up, negotiating,
413  *   etc.  This function starts the timer which tracks the state
414  *   of the PHY.  If you want to be notified when the state
415  *   changes, pass in the callback, otherwise, pass NULL.  If you
416  *   want to maintain your own state machine, do not call this
417  *   function. */
418 void phy_start_machine(struct phy_device *phydev,
419                 void (*handler)(struct net_device *))
420 {
421         phydev->adjust_state = handler;
422
423         init_timer(&phydev->phy_timer);
424         phydev->phy_timer.function = &phy_timer;
425         phydev->phy_timer.data = (unsigned long) phydev;
426         mod_timer(&phydev->phy_timer, jiffies + HZ);
427 }
428
429 /* phy_stop_machine
430  *
431  * description: Stops the state machine timer, sets the state to
432  *   UP (unless it wasn't up yet), and then frees the interrupt,
433  *   if it is in use. This function must be called BEFORE
434  *   phy_detach.
435  */
436 void phy_stop_machine(struct phy_device *phydev)
437 {
438         del_timer_sync(&phydev->phy_timer);
439
440         spin_lock(&phydev->lock);
441         if (phydev->state > PHY_UP)
442                 phydev->state = PHY_UP;
443         spin_unlock(&phydev->lock);
444
445         if (phydev->irq != PHY_POLL)
446                 phy_stop_interrupts(phydev);
447
448         phydev->adjust_state = NULL;
449 }
450
451 /* phy_error:
452  *
453  * Moves the PHY to the HALTED state in response to a read
454  * or write error, and tells the controller the link is down.
455  * Must not be called from interrupt context, or while the
456  * phydev->lock is held.
457  */
458 void phy_error(struct phy_device *phydev)
459 {
460         spin_lock(&phydev->lock);
461         phydev->state = PHY_HALTED;
462         spin_unlock(&phydev->lock);
463 }
464
465 static int phy_stop_interrupts(struct phy_device *phydev)
466 {
467         int err;
468
469         err = phy_disable_interrupts(phydev);
470
471         if (err)
472                 phy_error(phydev);
473
474         free_irq(phydev->irq, phydev);
475
476         return err;
477 }
478
479 /* Disable the PHY interrupts from the PHY side */
480 static int phy_disable_interrupts(struct phy_device *phydev)
481 {
482         int err;
483
484         /* Disable PHY interrupts */
485         err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
486
487         if (err)
488                 goto phy_err;
489
490         /* Clear the interrupt */
491         err = phy_clear_interrupt(phydev);
492
493         if (err)
494                 goto phy_err;
495
496         return 0;
497
498 phy_err:
499         phy_error(phydev);
500
501         return err;
502 }
503
504 /* PHY timer which handles the state machine */
505 static void phy_timer(unsigned long data)
506 {
507         struct phy_device *phydev = (struct phy_device *)data;
508         int needs_aneg = 0;
509         int err = 0;
510
511         spin_lock(&phydev->lock);
512
513         if (phydev->adjust_state)
514                 phydev->adjust_state(phydev->attached_dev);
515
516         switch(phydev->state) {
517                 case PHY_DOWN:
518                 case PHY_STARTING:
519                 case PHY_READY:
520                 case PHY_PENDING:
521                         break;
522                 case PHY_UP:
523                         needs_aneg = 1;
524
525                         phydev->link_timeout = PHY_AN_TIMEOUT;
526
527                         break;
528                 case PHY_AN:
529                         /* Check if negotiation is done.  Break
530                          * if there's an error */
531                         err = phy_aneg_done(phydev);
532                         if (err < 0)
533                                 break;
534
535                         /* If auto-negotiation is done, we change to
536                          * either RUNNING, or NOLINK */
537                         if (err > 0) {
538                                 err = phy_read_status(phydev);
539
540                                 if (err)
541                                         break;
542
543                                 if (phydev->link) {
544                                         phydev->state = PHY_RUNNING;
545                                         netif_carrier_on(phydev->attached_dev);
546                                 } else {
547                                         phydev->state = PHY_NOLINK;
548                                         netif_carrier_off(phydev->attached_dev);
549                                 }
550
551                                 phydev->adjust_link(phydev->attached_dev);
552
553                         } else if (0 == phydev->link_timeout--) {
554                                 /* The counter expired, so either we
555                                  * switch to forced mode, or the
556                                  * magic_aneg bit exists, and we try aneg
557                                  * again */
558                                 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
559                                         int idx;
560
561                                         /* We'll start from the
562                                          * fastest speed, and work
563                                          * our way down */
564                                         idx = phy_find_valid(0,
565                                                         phydev->supported);
566
567                                         phydev->speed = settings[idx].speed;
568                                         phydev->duplex = settings[idx].duplex;
569                                         
570                                         phydev->autoneg = AUTONEG_DISABLE;
571                                         phydev->state = PHY_FORCING;
572                                         phydev->link_timeout =
573                                                 PHY_FORCE_TIMEOUT;
574
575                                         pr_info("Trying %d/%s\n",
576                                                         phydev->speed,
577                                                         DUPLEX_FULL ==
578                                                         phydev->duplex ?
579                                                         "FULL" : "HALF");
580                                 }
581
582                                 needs_aneg = 1;
583                         }
584                         break;
585                 case PHY_NOLINK:
586                         err = phy_read_status(phydev);
587
588                         if (err)
589                                 break;
590
591                         if (phydev->link) {
592                                 phydev->state = PHY_RUNNING;
593                                 netif_carrier_on(phydev->attached_dev);
594                                 phydev->adjust_link(phydev->attached_dev);
595                         }
596                         break;
597                 case PHY_FORCING:
598                         err = phy_read_status(phydev);
599
600                         if (err)
601                                 break;
602
603                         if (phydev->link) {
604                                 phydev->state = PHY_RUNNING;
605                                 netif_carrier_on(phydev->attached_dev);
606                         } else {
607                                 if (0 == phydev->link_timeout--) {
608                                         phy_force_reduction(phydev);
609                                         needs_aneg = 1;
610                                 }
611                         }
612
613                         phydev->adjust_link(phydev->attached_dev);
614                         break;
615                 case PHY_RUNNING:
616                         /* Only register a CHANGE if we are
617                          * polling */
618                         if (PHY_POLL == phydev->irq)
619                                 phydev->state = PHY_CHANGELINK;
620                         break;
621                 case PHY_CHANGELINK:
622                         err = phy_read_status(phydev);
623
624                         if (err)
625                                 break;
626
627                         if (phydev->link) {
628                                 phydev->state = PHY_RUNNING;
629                                 netif_carrier_on(phydev->attached_dev);
630                         } else {
631                                 phydev->state = PHY_NOLINK;
632                                 netif_carrier_off(phydev->attached_dev);
633                         }
634
635                         phydev->adjust_link(phydev->attached_dev);
636
637                         if (PHY_POLL != phydev->irq)
638                                 err = phy_config_interrupt(phydev,
639                                                 PHY_INTERRUPT_ENABLED);
640                         break;
641                 case PHY_HALTED:
642                         if (phydev->link) {
643                                 phydev->link = 0;
644                                 netif_carrier_off(phydev->attached_dev);
645                                 phydev->adjust_link(phydev->attached_dev);
646                         }
647                         break;
648                 case PHY_RESUMING:
649
650                         err = phy_clear_interrupt(phydev);
651
652                         if (err)
653                                 break;
654
655                         err = phy_config_interrupt(phydev,
656                                         PHY_INTERRUPT_ENABLED);
657
658                         if (err)
659                                 break;
660
661                         if (AUTONEG_ENABLE == phydev->autoneg) {
662                                 err = phy_aneg_done(phydev);
663                                 if (err < 0)
664                                         break;
665
666                                 /* err > 0 if AN is done.
667                                  * Otherwise, it's 0, and we're
668                                  * still waiting for AN */
669                                 if (err > 0) {
670                                         phydev->state = PHY_RUNNING;
671                                 } else {
672                                         phydev->state = PHY_AN;
673                                         phydev->link_timeout = PHY_AN_TIMEOUT;
674                                 }
675                         } else
676                                 phydev->state = PHY_RUNNING;
677                         break;
678         }
679
680         spin_unlock(&phydev->lock);
681
682         if (needs_aneg)
683                 err = phy_start_aneg(phydev);
684
685         if (err < 0)
686                 phy_error(phydev);
687
688         mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
689 }
690