blob: 88237bdb525503d7f9fa9d53462ec6cc6ab75bd7 [file] [log] [blame]
Andy Fleming00db8182005-07-30 19:31:23 -04001/*
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.
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +010010 * Copyright (c) 2006 Maciej W. Rozycki
Andy Fleming00db8182005-07-30 19:31:23 -040011 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
16 *
17 */
Andy Fleming00db8182005-07-30 19:31:23 -040018#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>
Andy Fleming00db8182005-07-30 19:31:23 -040033#include <linux/mii.h>
34#include <linux/ethtool.h>
35#include <linux/phy.h>
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +010036#include <linux/timer.h>
37#include <linux/workqueue.h>
Andy Fleming00db8182005-07-30 19:31:23 -040038
39#include <asm/io.h>
40#include <asm/irq.h>
41#include <asm/uaccess.h>
42
Andy Fleminge1393452005-08-24 18:46:21 -050043/* Convenience function to print out the current phy status
44 */
45void phy_print_status(struct phy_device *phydev)
46{
Kumar Galaa4d00f12006-01-11 11:27:33 -080047 pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,
Andy Fleminge1393452005-08-24 18:46:21 -050048 phydev->link ? "Up" : "Down");
49 if (phydev->link)
50 printk(" - %d/%s", phydev->speed,
51 DUPLEX_FULL == phydev->duplex ?
52 "Full" : "Half");
53
54 printk("\n");
55}
56EXPORT_SYMBOL(phy_print_status);
Andy Fleming00db8182005-07-30 19:31:23 -040057
58
59/* Convenience functions for reading/writing a given PHY
60 * register. They MUST NOT be called from interrupt context,
61 * because the bus read/write functions may wait for an interrupt
62 * to conclude the operation. */
63int phy_read(struct phy_device *phydev, u16 regnum)
64{
65 int retval;
66 struct mii_bus *bus = phydev->bus;
67
68 spin_lock_bh(&bus->mdio_lock);
69 retval = bus->read(bus, phydev->addr, regnum);
70 spin_unlock_bh(&bus->mdio_lock);
71
72 return retval;
73}
74EXPORT_SYMBOL(phy_read);
75
76int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
77{
78 int err;
79 struct mii_bus *bus = phydev->bus;
80
81 spin_lock_bh(&bus->mdio_lock);
82 err = bus->write(bus, phydev->addr, regnum, val);
83 spin_unlock_bh(&bus->mdio_lock);
84
85 return err;
86}
87EXPORT_SYMBOL(phy_write);
88
89
90int phy_clear_interrupt(struct phy_device *phydev)
91{
92 int err = 0;
93
94 if (phydev->drv->ack_interrupt)
95 err = phydev->drv->ack_interrupt(phydev);
96
97 return err;
98}
99
100
101int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
102{
103 int err = 0;
104
105 phydev->interrupts = interrupts;
106 if (phydev->drv->config_intr)
107 err = phydev->drv->config_intr(phydev);
108
109 return err;
110}
111
112
113/* phy_aneg_done
114 *
115 * description: Reads the status register and returns 0 either if
116 * auto-negotiation is incomplete, or if there was an error.
117 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
118 */
119static inline int phy_aneg_done(struct phy_device *phydev)
120{
121 int retval;
122
123 retval = phy_read(phydev, MII_BMSR);
124
125 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
126}
127
Andy Fleming00db8182005-07-30 19:31:23 -0400128/* A structure for mapping a particular speed and duplex
129 * combination to a particular SUPPORTED and ADVERTISED value */
130struct phy_setting {
131 int speed;
132 int duplex;
133 u32 setting;
134};
135
136/* A mapping of all SUPPORTED settings to speed/duplex */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500137static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400138 {
139 .speed = 10000,
140 .duplex = DUPLEX_FULL,
141 .setting = SUPPORTED_10000baseT_Full,
142 },
143 {
144 .speed = SPEED_1000,
145 .duplex = DUPLEX_FULL,
146 .setting = SUPPORTED_1000baseT_Full,
147 },
148 {
149 .speed = SPEED_1000,
150 .duplex = DUPLEX_HALF,
151 .setting = SUPPORTED_1000baseT_Half,
152 },
153 {
154 .speed = SPEED_100,
155 .duplex = DUPLEX_FULL,
156 .setting = SUPPORTED_100baseT_Full,
157 },
158 {
159 .speed = SPEED_100,
160 .duplex = DUPLEX_HALF,
161 .setting = SUPPORTED_100baseT_Half,
162 },
163 {
164 .speed = SPEED_10,
165 .duplex = DUPLEX_FULL,
166 .setting = SUPPORTED_10baseT_Full,
167 },
168 {
169 .speed = SPEED_10,
170 .duplex = DUPLEX_HALF,
171 .setting = SUPPORTED_10baseT_Half,
172 },
173};
174
175#define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
176
177/* phy_find_setting
178 *
179 * description: Searches the settings array for the setting which
180 * matches the desired speed and duplex, and returns the index
181 * of that setting. Returns the index of the last setting if
182 * none of the others match.
183 */
184static inline int phy_find_setting(int speed, int duplex)
185{
186 int idx = 0;
187
188 while (idx < ARRAY_SIZE(settings) &&
189 (settings[idx].speed != speed ||
190 settings[idx].duplex != duplex))
191 idx++;
192
193 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
194}
195
196/* phy_find_valid
197 * idx: The first index in settings[] to search
198 * features: A mask of the valid settings
199 *
200 * description: Returns the index of the first valid setting less
201 * than or equal to the one pointed to by idx, as determined by
202 * the mask in features. Returns the index of the last setting
203 * if nothing else matches.
204 */
205static inline int phy_find_valid(int idx, u32 features)
206{
207 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
208 idx++;
209
210 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
211}
212
213/* phy_sanitize_settings
214 *
215 * description: Make sure the PHY is set to supported speeds and
216 * duplexes. Drop down by one in this order: 1000/FULL,
217 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
218 */
Andy Fleminge1393452005-08-24 18:46:21 -0500219void phy_sanitize_settings(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400220{
221 u32 features = phydev->supported;
222 int idx;
223
224 /* Sanitize settings based on PHY capabilities */
225 if ((features & SUPPORTED_Autoneg) == 0)
226 phydev->autoneg = 0;
227
228 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
229 features);
230
231 phydev->speed = settings[idx].speed;
232 phydev->duplex = settings[idx].duplex;
233}
Andy Fleminge1393452005-08-24 18:46:21 -0500234EXPORT_SYMBOL(phy_sanitize_settings);
Andy Fleming00db8182005-07-30 19:31:23 -0400235
236/* phy_ethtool_sset:
237 * A generic ethtool sset function. Handles all the details
238 *
239 * A few notes about parameter checking:
240 * - We don't set port or transceiver, so we don't care what they
241 * were set to.
242 * - phy_start_aneg() will make sure forced settings are sane, and
243 * choose the next best ones from the ones selected, so we don't
244 * care if ethtool tries to give us bad values
Andy Fleminge1393452005-08-24 18:46:21 -0500245 *
Andy Fleming00db8182005-07-30 19:31:23 -0400246 */
247int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
248{
249 if (cmd->phy_address != phydev->addr)
250 return -EINVAL;
251
252 /* We make sure that we don't pass unsupported
253 * values in to the PHY */
254 cmd->advertising &= phydev->supported;
255
256 /* Verify the settings we care about. */
257 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
258 return -EINVAL;
259
260 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
261 return -EINVAL;
262
263 if (cmd->autoneg == AUTONEG_DISABLE
264 && ((cmd->speed != SPEED_1000
265 && cmd->speed != SPEED_100
266 && cmd->speed != SPEED_10)
267 || (cmd->duplex != DUPLEX_HALF
268 && cmd->duplex != DUPLEX_FULL)))
269 return -EINVAL;
270
271 phydev->autoneg = cmd->autoneg;
272
273 phydev->speed = cmd->speed;
274
275 phydev->advertising = cmd->advertising;
276
277 if (AUTONEG_ENABLE == cmd->autoneg)
278 phydev->advertising |= ADVERTISED_Autoneg;
279 else
280 phydev->advertising &= ~ADVERTISED_Autoneg;
281
282 phydev->duplex = cmd->duplex;
283
284 /* Restart the PHY */
285 phy_start_aneg(phydev);
286
287 return 0;
288}
289
290int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
291{
292 cmd->supported = phydev->supported;
293
294 cmd->advertising = phydev->advertising;
295
296 cmd->speed = phydev->speed;
297 cmd->duplex = phydev->duplex;
298 cmd->port = PORT_MII;
299 cmd->phy_address = phydev->addr;
300 cmd->transceiver = XCVR_EXTERNAL;
301 cmd->autoneg = phydev->autoneg;
302
303 return 0;
304}
305
306
307/* Note that this function is currently incompatible with the
308 * PHYCONTROL layer. It changes registers without regard to
309 * current state. Use at own risk
310 */
311int phy_mii_ioctl(struct phy_device *phydev,
312 struct mii_ioctl_data *mii_data, int cmd)
313{
314 u16 val = mii_data->val_in;
315
316 switch (cmd) {
317 case SIOCGMIIPHY:
318 mii_data->phy_id = phydev->addr;
319 break;
320 case SIOCGMIIREG:
321 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
322 break;
323
324 case SIOCSMIIREG:
325 if (!capable(CAP_NET_ADMIN))
326 return -EPERM;
327
328 if (mii_data->phy_id == phydev->addr) {
329 switch(mii_data->reg_num) {
330 case MII_BMCR:
331 if (val & (BMCR_RESET|BMCR_ANENABLE))
332 phydev->autoneg = AUTONEG_DISABLE;
333 else
334 phydev->autoneg = AUTONEG_ENABLE;
335 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
336 phydev->duplex = DUPLEX_FULL;
337 else
338 phydev->duplex = DUPLEX_HALF;
339 break;
340 case MII_ADVERTISE:
341 phydev->advertising = val;
342 break;
343 default:
344 /* do nothing */
345 break;
346 }
347 }
348
349 phy_write(phydev, mii_data->reg_num, val);
350
351 if (mii_data->reg_num == MII_BMCR
352 && val & BMCR_RESET
353 && phydev->drv->config_init)
354 phydev->drv->config_init(phydev);
355 break;
356 }
357
358 return 0;
359}
360
Andy Fleminge1393452005-08-24 18:46:21 -0500361/* phy_start_aneg
362 *
363 * description: Sanitizes the settings (if we're not
364 * autonegotiating them), and then calls the driver's
365 * config_aneg function. If the PHYCONTROL Layer is operating,
366 * we change the state to reflect the beginning of
367 * Auto-negotiation or forcing.
368 */
369int phy_start_aneg(struct phy_device *phydev)
370{
371 int err;
372
373 spin_lock(&phydev->lock);
374
375 if (AUTONEG_DISABLE == phydev->autoneg)
376 phy_sanitize_settings(phydev);
377
378 err = phydev->drv->config_aneg(phydev);
379
Andy Fleminge1393452005-08-24 18:46:21 -0500380 if (err < 0)
381 goto out_unlock;
382
383 if (phydev->state != PHY_HALTED) {
384 if (AUTONEG_ENABLE == phydev->autoneg) {
385 phydev->state = PHY_AN;
386 phydev->link_timeout = PHY_AN_TIMEOUT;
387 } else {
388 phydev->state = PHY_FORCING;
389 phydev->link_timeout = PHY_FORCE_TIMEOUT;
390 }
391 }
392
393out_unlock:
Andy Fleminge1393452005-08-24 18:46:21 -0500394 spin_unlock(&phydev->lock);
395 return err;
396}
397EXPORT_SYMBOL(phy_start_aneg);
398
399
Andy Fleminge1393452005-08-24 18:46:21 -0500400static void phy_change(void *data);
401static void phy_timer(unsigned long data);
402
Andy Fleming00db8182005-07-30 19:31:23 -0400403/* phy_start_machine:
404 *
405 * description: The PHY infrastructure can run a state machine
406 * which tracks whether the PHY is starting up, negotiating,
407 * etc. This function starts the timer which tracks the state
408 * of the PHY. If you want to be notified when the state
409 * changes, pass in the callback, otherwise, pass NULL. If you
410 * want to maintain your own state machine, do not call this
411 * function. */
412void phy_start_machine(struct phy_device *phydev,
413 void (*handler)(struct net_device *))
414{
415 phydev->adjust_state = handler;
416
417 init_timer(&phydev->phy_timer);
418 phydev->phy_timer.function = &phy_timer;
419 phydev->phy_timer.data = (unsigned long) phydev;
420 mod_timer(&phydev->phy_timer, jiffies + HZ);
421}
422
423/* phy_stop_machine
424 *
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400425 * description: Stops the state machine timer, sets the state to UP
426 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400427 * phy_detach.
428 */
429void phy_stop_machine(struct phy_device *phydev)
430{
431 del_timer_sync(&phydev->phy_timer);
432
433 spin_lock(&phydev->lock);
434 if (phydev->state > PHY_UP)
435 phydev->state = PHY_UP;
436 spin_unlock(&phydev->lock);
437
Andy Fleming00db8182005-07-30 19:31:23 -0400438 phydev->adjust_state = NULL;
439}
440
Andy Fleminge1393452005-08-24 18:46:21 -0500441/* phy_force_reduction
442 *
443 * description: Reduces the speed/duplex settings by
444 * one notch. The order is so:
445 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
446 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
447 */
448static void phy_force_reduction(struct phy_device *phydev)
449{
450 int idx;
451
452 idx = phy_find_setting(phydev->speed, phydev->duplex);
453
454 idx++;
455
456 idx = phy_find_valid(idx, phydev->supported);
457
458 phydev->speed = settings[idx].speed;
459 phydev->duplex = settings[idx].duplex;
460
461 pr_info("Trying %d/%s\n", phydev->speed,
462 DUPLEX_FULL == phydev->duplex ?
463 "FULL" : "HALF");
464}
465
466
Andy Fleming00db8182005-07-30 19:31:23 -0400467/* phy_error:
468 *
469 * Moves the PHY to the HALTED state in response to a read
470 * or write error, and tells the controller the link is down.
471 * Must not be called from interrupt context, or while the
472 * phydev->lock is held.
473 */
474void phy_error(struct phy_device *phydev)
475{
476 spin_lock(&phydev->lock);
477 phydev->state = PHY_HALTED;
478 spin_unlock(&phydev->lock);
479}
480
Andy Fleminge1393452005-08-24 18:46:21 -0500481/* phy_interrupt
482 *
483 * description: When a PHY interrupt occurs, the handler disables
484 * interrupts, and schedules a work task to clear the interrupt.
485 */
David Howells7d12e782006-10-05 14:55:46 +0100486static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500487{
488 struct phy_device *phydev = phy_dat;
489
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100490 if (PHY_HALTED == phydev->state)
491 return IRQ_NONE; /* It can't be ours. */
492
Andy Fleminge1393452005-08-24 18:46:21 -0500493 /* The MDIO bus is not allowed to be written in interrupt
494 * context, so we need to disable the irq here. A work
495 * queue will write the PHY to disable and clear the
496 * interrupt, and then reenable the irq line. */
497 disable_irq_nosync(irq);
498
499 schedule_work(&phydev->phy_queue);
500
501 return IRQ_HANDLED;
502}
503
504/* Enable the interrupts from the PHY side */
505int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400506{
507 int err;
508
Andy Fleminge1393452005-08-24 18:46:21 -0500509 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400510
Andy Fleminge1393452005-08-24 18:46:21 -0500511 if (err < 0)
512 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400513
Andy Fleminge1393452005-08-24 18:46:21 -0500514 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400515
516 return err;
517}
Andy Fleminge1393452005-08-24 18:46:21 -0500518EXPORT_SYMBOL(phy_enable_interrupts);
Andy Fleming00db8182005-07-30 19:31:23 -0400519
520/* Disable the PHY interrupts from the PHY side */
Andy Fleminge1393452005-08-24 18:46:21 -0500521int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400522{
523 int err;
524
525 /* Disable PHY interrupts */
526 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
527
528 if (err)
529 goto phy_err;
530
531 /* Clear the interrupt */
532 err = phy_clear_interrupt(phydev);
533
534 if (err)
535 goto phy_err;
536
537 return 0;
538
539phy_err:
540 phy_error(phydev);
541
542 return err;
543}
Andy Fleminge1393452005-08-24 18:46:21 -0500544EXPORT_SYMBOL(phy_disable_interrupts);
545
546/* phy_start_interrupts
547 *
548 * description: Request the interrupt for the given PHY. If
549 * this fails, then we set irq to PHY_POLL.
550 * Otherwise, we enable the interrupts in the PHY.
551 * Returns 0 on success.
552 * This should only be called with a valid IRQ number.
553 */
554int phy_start_interrupts(struct phy_device *phydev)
555{
556 int err = 0;
557
558 INIT_WORK(&phydev->phy_queue, phy_change, phydev);
559
560 if (request_irq(phydev->irq, phy_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700561 IRQF_SHARED,
Andy Fleminge1393452005-08-24 18:46:21 -0500562 "phy_interrupt",
563 phydev) < 0) {
564 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
565 phydev->bus->name,
566 phydev->irq);
567 phydev->irq = PHY_POLL;
568 return 0;
569 }
570
571 err = phy_enable_interrupts(phydev);
572
573 return err;
574}
575EXPORT_SYMBOL(phy_start_interrupts);
576
577int phy_stop_interrupts(struct phy_device *phydev)
578{
579 int err;
580
581 err = phy_disable_interrupts(phydev);
582
583 if (err)
584 phy_error(phydev);
585
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100586 /*
587 * Finish any pending work; we might have been scheduled
588 * to be called from keventd ourselves, though.
589 */
590 if (!current_is_keventd())
591 flush_scheduled_work();
592
Andy Fleminge1393452005-08-24 18:46:21 -0500593 free_irq(phydev->irq, phydev);
594
595 return err;
596}
597EXPORT_SYMBOL(phy_stop_interrupts);
598
599
600/* Scheduled by the phy_interrupt/timer to handle PHY changes */
601static void phy_change(void *data)
602{
603 int err;
604 struct phy_device *phydev = data;
605
606 err = phy_disable_interrupts(phydev);
607
608 if (err)
609 goto phy_err;
610
611 spin_lock(&phydev->lock);
612 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
613 phydev->state = PHY_CHANGELINK;
614 spin_unlock(&phydev->lock);
615
616 enable_irq(phydev->irq);
617
618 /* Reenable interrupts */
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100619 if (PHY_HALTED != phydev->state)
620 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500621
622 if (err)
623 goto irq_enable_err;
624
625 return;
626
627irq_enable_err:
628 disable_irq(phydev->irq);
629phy_err:
630 phy_error(phydev);
631}
632
633/* Bring down the PHY link, and stop checking the status. */
634void phy_stop(struct phy_device *phydev)
635{
636 spin_lock(&phydev->lock);
637
638 if (PHY_HALTED == phydev->state)
639 goto out_unlock;
640
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100641 phydev->state = PHY_HALTED;
Andy Fleminge1393452005-08-24 18:46:21 -0500642
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100643 if (phydev->irq != PHY_POLL) {
Andy Fleminge1393452005-08-24 18:46:21 -0500644 /* Disable PHY Interrupts */
645 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500646
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100647 /* Clear any pending interrupts */
648 phy_clear_interrupt(phydev);
649 }
Andy Fleminge1393452005-08-24 18:46:21 -0500650
651out_unlock:
652 spin_unlock(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100653
654 /*
655 * Cannot call flush_scheduled_work() here as desired because
656 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
657 * will not reenable interrupts.
658 */
Andy Fleminge1393452005-08-24 18:46:21 -0500659}
660
661
662/* phy_start
663 *
664 * description: Indicates the attached device's readiness to
665 * handle PHY-related work. Used during startup to start the
666 * PHY, and after a call to phy_stop() to resume operation.
667 * Also used to indicate the MDIO bus has cleared an error
668 * condition.
669 */
670void phy_start(struct phy_device *phydev)
671{
672 spin_lock(&phydev->lock);
673
674 switch (phydev->state) {
675 case PHY_STARTING:
676 phydev->state = PHY_PENDING;
677 break;
678 case PHY_READY:
679 phydev->state = PHY_UP;
680 break;
681 case PHY_HALTED:
682 phydev->state = PHY_RESUMING;
683 default:
684 break;
685 }
686 spin_unlock(&phydev->lock);
687}
688EXPORT_SYMBOL(phy_stop);
689EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400690
Andy Fleming00db8182005-07-30 19:31:23 -0400691/* PHY timer which handles the state machine */
692static void phy_timer(unsigned long data)
693{
694 struct phy_device *phydev = (struct phy_device *)data;
695 int needs_aneg = 0;
696 int err = 0;
697
698 spin_lock(&phydev->lock);
699
700 if (phydev->adjust_state)
701 phydev->adjust_state(phydev->attached_dev);
702
703 switch(phydev->state) {
704 case PHY_DOWN:
705 case PHY_STARTING:
706 case PHY_READY:
707 case PHY_PENDING:
708 break;
709 case PHY_UP:
710 needs_aneg = 1;
711
712 phydev->link_timeout = PHY_AN_TIMEOUT;
713
714 break;
715 case PHY_AN:
Andy Fleming6b655522006-10-16 16:19:17 -0500716 err = phy_read_status(phydev);
717
718 if (err < 0)
719 break;
720
721 /* If the link is down, give up on
722 * negotiation for now */
723 if (!phydev->link) {
724 phydev->state = PHY_NOLINK;
725 netif_carrier_off(phydev->attached_dev);
726 phydev->adjust_link(phydev->attached_dev);
727 break;
728 }
729
Andy Fleming00db8182005-07-30 19:31:23 -0400730 /* Check if negotiation is done. Break
731 * if there's an error */
732 err = phy_aneg_done(phydev);
733 if (err < 0)
734 break;
735
Andy Fleming6b655522006-10-16 16:19:17 -0500736 /* If AN is done, we're running */
Andy Fleming00db8182005-07-30 19:31:23 -0400737 if (err > 0) {
Andy Fleming6b655522006-10-16 16:19:17 -0500738 phydev->state = PHY_RUNNING;
739 netif_carrier_on(phydev->attached_dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400740 phydev->adjust_link(phydev->attached_dev);
741
742 } else if (0 == phydev->link_timeout--) {
Andy Fleming6b655522006-10-16 16:19:17 -0500743 int idx;
Andy Fleming00db8182005-07-30 19:31:23 -0400744
745 needs_aneg = 1;
Andy Fleming6b655522006-10-16 16:19:17 -0500746 /* If we have the magic_aneg bit,
747 * we try again */
748 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
749 break;
750
751 /* The timer expired, and we still
752 * don't have a setting, so we try
753 * forcing it until we find one that
754 * works, starting from the fastest speed,
755 * and working our way down */
756 idx = phy_find_valid(0, phydev->supported);
757
758 phydev->speed = settings[idx].speed;
759 phydev->duplex = settings[idx].duplex;
760
761 phydev->autoneg = AUTONEG_DISABLE;
762
763 pr_info("Trying %d/%s\n", phydev->speed,
764 DUPLEX_FULL ==
765 phydev->duplex ?
766 "FULL" : "HALF");
Andy Fleming00db8182005-07-30 19:31:23 -0400767 }
768 break;
769 case PHY_NOLINK:
770 err = phy_read_status(phydev);
771
772 if (err)
773 break;
774
775 if (phydev->link) {
776 phydev->state = PHY_RUNNING;
777 netif_carrier_on(phydev->attached_dev);
778 phydev->adjust_link(phydev->attached_dev);
779 }
780 break;
781 case PHY_FORCING:
Andy Fleming6b655522006-10-16 16:19:17 -0500782 err = genphy_update_link(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400783
784 if (err)
785 break;
786
787 if (phydev->link) {
788 phydev->state = PHY_RUNNING;
789 netif_carrier_on(phydev->attached_dev);
790 } else {
791 if (0 == phydev->link_timeout--) {
792 phy_force_reduction(phydev);
793 needs_aneg = 1;
794 }
795 }
796
797 phydev->adjust_link(phydev->attached_dev);
798 break;
799 case PHY_RUNNING:
800 /* Only register a CHANGE if we are
801 * polling */
802 if (PHY_POLL == phydev->irq)
803 phydev->state = PHY_CHANGELINK;
804 break;
805 case PHY_CHANGELINK:
806 err = phy_read_status(phydev);
807
808 if (err)
809 break;
810
811 if (phydev->link) {
812 phydev->state = PHY_RUNNING;
813 netif_carrier_on(phydev->attached_dev);
814 } else {
815 phydev->state = PHY_NOLINK;
816 netif_carrier_off(phydev->attached_dev);
817 }
818
819 phydev->adjust_link(phydev->attached_dev);
820
821 if (PHY_POLL != phydev->irq)
822 err = phy_config_interrupt(phydev,
823 PHY_INTERRUPT_ENABLED);
824 break;
825 case PHY_HALTED:
826 if (phydev->link) {
827 phydev->link = 0;
828 netif_carrier_off(phydev->attached_dev);
829 phydev->adjust_link(phydev->attached_dev);
830 }
831 break;
832 case PHY_RESUMING:
833
834 err = phy_clear_interrupt(phydev);
835
836 if (err)
837 break;
838
839 err = phy_config_interrupt(phydev,
840 PHY_INTERRUPT_ENABLED);
841
842 if (err)
843 break;
844
845 if (AUTONEG_ENABLE == phydev->autoneg) {
846 err = phy_aneg_done(phydev);
847 if (err < 0)
848 break;
849
850 /* err > 0 if AN is done.
851 * Otherwise, it's 0, and we're
852 * still waiting for AN */
853 if (err > 0) {
854 phydev->state = PHY_RUNNING;
855 } else {
856 phydev->state = PHY_AN;
857 phydev->link_timeout = PHY_AN_TIMEOUT;
858 }
859 } else
860 phydev->state = PHY_RUNNING;
861 break;
862 }
863
864 spin_unlock(&phydev->lock);
865
866 if (needs_aneg)
867 err = phy_start_aneg(phydev);
868
869 if (err < 0)
870 phy_error(phydev);
871
872 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
873}
874