blob: 4da993dfcfd89e38c3d1103b5853eed7397316b5 [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>
Andy Fleming00db8182005-07-30 19:31:23 -040019#include <linux/string.h>
20#include <linux/errno.h>
21#include <linux/unistd.h>
22#include <linux/slab.h>
23#include <linux/interrupt.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <linux/skbuff.h>
29#include <linux/spinlock.h>
30#include <linux/mm.h>
31#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040032#include <linux/mii.h>
33#include <linux/ethtool.h>
34#include <linux/phy.h>
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +010035#include <linux/timer.h>
36#include <linux/workqueue.h>
Andy Fleming00db8182005-07-30 19:31:23 -040037
38#include <asm/io.h>
39#include <asm/irq.h>
40#include <asm/uaccess.h>
41
Randy Dunlapb3df0da2007-03-06 02:41:48 -080042/**
43 * phy_print_status - Convenience function to print out the current phy status
44 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -050045 */
46void phy_print_status(struct phy_device *phydev)
47{
Kumar Galaa4d00f12006-01-11 11:27:33 -080048 pr_info("PHY: %s - Link is %s", phydev->dev.bus_id,
Andy Fleminge1393452005-08-24 18:46:21 -050049 phydev->link ? "Up" : "Down");
50 if (phydev->link)
51 printk(" - %d/%s", phydev->speed,
52 DUPLEX_FULL == phydev->duplex ?
53 "Full" : "Half");
54
55 printk("\n");
56}
57EXPORT_SYMBOL(phy_print_status);
Andy Fleming00db8182005-07-30 19:31:23 -040058
59
Randy Dunlapb3df0da2007-03-06 02:41:48 -080060/**
61 * phy_read - Convenience function for reading a given PHY register
62 * @phydev: the phy_device struct
63 * @regnum: register number to read
64 *
65 * NOTE: MUST NOT be called from interrupt context,
Andy Fleming00db8182005-07-30 19:31:23 -040066 * because the bus read/write functions may wait for an interrupt
Randy Dunlapb3df0da2007-03-06 02:41:48 -080067 * to conclude the operation.
68 */
Andy Fleming00db8182005-07-30 19:31:23 -040069int phy_read(struct phy_device *phydev, u16 regnum)
70{
71 int retval;
72 struct mii_bus *bus = phydev->bus;
73
74 spin_lock_bh(&bus->mdio_lock);
75 retval = bus->read(bus, phydev->addr, regnum);
76 spin_unlock_bh(&bus->mdio_lock);
77
78 return retval;
79}
80EXPORT_SYMBOL(phy_read);
81
Randy Dunlapb3df0da2007-03-06 02:41:48 -080082/**
83 * phy_write - Convenience function for writing a given PHY register
84 * @phydev: the phy_device struct
85 * @regnum: register number to write
86 * @val: value to write to @regnum
87 *
88 * NOTE: MUST NOT be called from interrupt context,
89 * because the bus read/write functions may wait for an interrupt
90 * to conclude the operation.
91 */
Andy Fleming00db8182005-07-30 19:31:23 -040092int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
93{
94 int err;
95 struct mii_bus *bus = phydev->bus;
96
97 spin_lock_bh(&bus->mdio_lock);
98 err = bus->write(bus, phydev->addr, regnum, val);
99 spin_unlock_bh(&bus->mdio_lock);
100
101 return err;
102}
103EXPORT_SYMBOL(phy_write);
104
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800105/**
106 * phy_clear_interrupt - Ack the phy device's interrupt
107 * @phydev: the phy_device struct
108 *
109 * If the @phydev driver has an ack_interrupt function, call it to
110 * ack and clear the phy device's interrupt.
111 *
112 * Returns 0 on success on < 0 on error.
113 */
Andy Fleming00db8182005-07-30 19:31:23 -0400114int phy_clear_interrupt(struct phy_device *phydev)
115{
116 int err = 0;
117
118 if (phydev->drv->ack_interrupt)
119 err = phydev->drv->ack_interrupt(phydev);
120
121 return err;
122}
123
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800124/**
125 * phy_config_interrupt - configure the PHY device for the requested interrupts
126 * @phydev: the phy_device struct
127 * @interrupts: interrupt flags to configure for this @phydev
128 *
129 * Returns 0 on success on < 0 on error.
130 */
Andy Fleming00db8182005-07-30 19:31:23 -0400131int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
132{
133 int err = 0;
134
135 phydev->interrupts = interrupts;
136 if (phydev->drv->config_intr)
137 err = phydev->drv->config_intr(phydev);
138
139 return err;
140}
141
142
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800143/**
144 * phy_aneg_done - return auto-negotiation status
145 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400146 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800147 * Description: Reads the status register and returns 0 either if
Andy Fleming00db8182005-07-30 19:31:23 -0400148 * auto-negotiation is incomplete, or if there was an error.
149 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
150 */
151static inline int phy_aneg_done(struct phy_device *phydev)
152{
153 int retval;
154
155 retval = phy_read(phydev, MII_BMSR);
156
157 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
158}
159
Andy Fleming00db8182005-07-30 19:31:23 -0400160/* A structure for mapping a particular speed and duplex
161 * combination to a particular SUPPORTED and ADVERTISED value */
162struct phy_setting {
163 int speed;
164 int duplex;
165 u32 setting;
166};
167
168/* A mapping of all SUPPORTED settings to speed/duplex */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500169static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400170 {
171 .speed = 10000,
172 .duplex = DUPLEX_FULL,
173 .setting = SUPPORTED_10000baseT_Full,
174 },
175 {
176 .speed = SPEED_1000,
177 .duplex = DUPLEX_FULL,
178 .setting = SUPPORTED_1000baseT_Full,
179 },
180 {
181 .speed = SPEED_1000,
182 .duplex = DUPLEX_HALF,
183 .setting = SUPPORTED_1000baseT_Half,
184 },
185 {
186 .speed = SPEED_100,
187 .duplex = DUPLEX_FULL,
188 .setting = SUPPORTED_100baseT_Full,
189 },
190 {
191 .speed = SPEED_100,
192 .duplex = DUPLEX_HALF,
193 .setting = SUPPORTED_100baseT_Half,
194 },
195 {
196 .speed = SPEED_10,
197 .duplex = DUPLEX_FULL,
198 .setting = SUPPORTED_10baseT_Full,
199 },
200 {
201 .speed = SPEED_10,
202 .duplex = DUPLEX_HALF,
203 .setting = SUPPORTED_10baseT_Half,
204 },
205};
206
Denis Chengff8ac602007-09-02 18:30:18 +0800207#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
Andy Fleming00db8182005-07-30 19:31:23 -0400208
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800209/**
210 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
211 * @speed: speed to match
212 * @duplex: duplex to match
Andy Fleming00db8182005-07-30 19:31:23 -0400213 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800214 * Description: Searches the settings array for the setting which
Andy Fleming00db8182005-07-30 19:31:23 -0400215 * matches the desired speed and duplex, and returns the index
216 * of that setting. Returns the index of the last setting if
217 * none of the others match.
218 */
219static inline int phy_find_setting(int speed, int duplex)
220{
221 int idx = 0;
222
223 while (idx < ARRAY_SIZE(settings) &&
224 (settings[idx].speed != speed ||
225 settings[idx].duplex != duplex))
226 idx++;
227
228 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
229}
230
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800231/**
232 * phy_find_valid - find a PHY setting that matches the requested features mask
233 * @idx: The first index in settings[] to search
234 * @features: A mask of the valid settings
Andy Fleming00db8182005-07-30 19:31:23 -0400235 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800236 * Description: Returns the index of the first valid setting less
Andy Fleming00db8182005-07-30 19:31:23 -0400237 * than or equal to the one pointed to by idx, as determined by
238 * the mask in features. Returns the index of the last setting
239 * if nothing else matches.
240 */
241static inline int phy_find_valid(int idx, u32 features)
242{
243 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
244 idx++;
245
246 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
247}
248
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800249/**
250 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
251 * @phydev: the target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400252 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800253 * Description: Make sure the PHY is set to supported speeds and
Andy Fleming00db8182005-07-30 19:31:23 -0400254 * duplexes. Drop down by one in this order: 1000/FULL,
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800255 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
Andy Fleming00db8182005-07-30 19:31:23 -0400256 */
Andy Fleminge1393452005-08-24 18:46:21 -0500257void phy_sanitize_settings(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400258{
259 u32 features = phydev->supported;
260 int idx;
261
262 /* Sanitize settings based on PHY capabilities */
263 if ((features & SUPPORTED_Autoneg) == 0)
Domen Puncer163642a2007-08-07 12:12:41 +0200264 phydev->autoneg = AUTONEG_DISABLE;
Andy Fleming00db8182005-07-30 19:31:23 -0400265
266 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
267 features);
268
269 phydev->speed = settings[idx].speed;
270 phydev->duplex = settings[idx].duplex;
271}
Andy Fleminge1393452005-08-24 18:46:21 -0500272EXPORT_SYMBOL(phy_sanitize_settings);
Andy Fleming00db8182005-07-30 19:31:23 -0400273
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800274/**
275 * phy_ethtool_sset - generic ethtool sset function, handles all the details
276 * @phydev: target phy_device struct
277 * @cmd: ethtool_cmd
Andy Fleming00db8182005-07-30 19:31:23 -0400278 *
279 * A few notes about parameter checking:
280 * - We don't set port or transceiver, so we don't care what they
281 * were set to.
282 * - phy_start_aneg() will make sure forced settings are sane, and
283 * choose the next best ones from the ones selected, so we don't
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800284 * care if ethtool tries to give us bad values.
Andy Fleming00db8182005-07-30 19:31:23 -0400285 */
286int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
287{
288 if (cmd->phy_address != phydev->addr)
289 return -EINVAL;
290
291 /* We make sure that we don't pass unsupported
292 * values in to the PHY */
293 cmd->advertising &= phydev->supported;
294
295 /* Verify the settings we care about. */
296 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
297 return -EINVAL;
298
299 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
300 return -EINVAL;
301
302 if (cmd->autoneg == AUTONEG_DISABLE
303 && ((cmd->speed != SPEED_1000
304 && cmd->speed != SPEED_100
305 && cmd->speed != SPEED_10)
306 || (cmd->duplex != DUPLEX_HALF
307 && cmd->duplex != DUPLEX_FULL)))
308 return -EINVAL;
309
310 phydev->autoneg = cmd->autoneg;
311
312 phydev->speed = cmd->speed;
313
314 phydev->advertising = cmd->advertising;
315
316 if (AUTONEG_ENABLE == cmd->autoneg)
317 phydev->advertising |= ADVERTISED_Autoneg;
318 else
319 phydev->advertising &= ~ADVERTISED_Autoneg;
320
321 phydev->duplex = cmd->duplex;
322
323 /* Restart the PHY */
324 phy_start_aneg(phydev);
325
326 return 0;
327}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600328EXPORT_SYMBOL(phy_ethtool_sset);
Andy Fleming00db8182005-07-30 19:31:23 -0400329
330int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
331{
332 cmd->supported = phydev->supported;
333
334 cmd->advertising = phydev->advertising;
335
336 cmd->speed = phydev->speed;
337 cmd->duplex = phydev->duplex;
338 cmd->port = PORT_MII;
339 cmd->phy_address = phydev->addr;
340 cmd->transceiver = XCVR_EXTERNAL;
341 cmd->autoneg = phydev->autoneg;
342
343 return 0;
344}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600345EXPORT_SYMBOL(phy_ethtool_gset);
Andy Fleming00db8182005-07-30 19:31:23 -0400346
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800347/**
348 * phy_mii_ioctl - generic PHY MII ioctl interface
349 * @phydev: the phy_device struct
350 * @mii_data: MII ioctl data
351 * @cmd: ioctl cmd to execute
352 *
353 * Note that this function is currently incompatible with the
Andy Fleming00db8182005-07-30 19:31:23 -0400354 * PHYCONTROL layer. It changes registers without regard to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800355 * current state. Use at own risk.
Andy Fleming00db8182005-07-30 19:31:23 -0400356 */
357int phy_mii_ioctl(struct phy_device *phydev,
358 struct mii_ioctl_data *mii_data, int cmd)
359{
360 u16 val = mii_data->val_in;
361
362 switch (cmd) {
363 case SIOCGMIIPHY:
364 mii_data->phy_id = phydev->addr;
365 break;
366 case SIOCGMIIREG:
367 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
368 break;
369
370 case SIOCSMIIREG:
371 if (!capable(CAP_NET_ADMIN))
372 return -EPERM;
373
374 if (mii_data->phy_id == phydev->addr) {
375 switch(mii_data->reg_num) {
376 case MII_BMCR:
Domen Puncer163642a2007-08-07 12:12:41 +0200377 if ((val & (BMCR_RESET|BMCR_ANENABLE)) == 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400378 phydev->autoneg = AUTONEG_DISABLE;
379 else
380 phydev->autoneg = AUTONEG_ENABLE;
381 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
382 phydev->duplex = DUPLEX_FULL;
383 else
384 phydev->duplex = DUPLEX_HALF;
Shan Lu024a0a32007-03-06 02:42:03 -0800385 if ((!phydev->autoneg) &&
386 (val & BMCR_SPEED1000))
387 phydev->speed = SPEED_1000;
388 else if ((!phydev->autoneg) &&
389 (val & BMCR_SPEED100))
390 phydev->speed = SPEED_100;
Andy Fleming00db8182005-07-30 19:31:23 -0400391 break;
392 case MII_ADVERTISE:
393 phydev->advertising = val;
394 break;
395 default:
396 /* do nothing */
397 break;
398 }
399 }
400
401 phy_write(phydev, mii_data->reg_num, val);
402
403 if (mii_data->reg_num == MII_BMCR
404 && val & BMCR_RESET
405 && phydev->drv->config_init)
406 phydev->drv->config_init(phydev);
407 break;
408 }
409
410 return 0;
411}
Domen Puncer680e9fe2007-09-17 22:21:40 +0200412EXPORT_SYMBOL(phy_mii_ioctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400413
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800414/**
415 * phy_start_aneg - start auto-negotiation for this PHY device
416 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500417 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800418 * Description: Sanitizes the settings (if we're not autonegotiating
419 * them), and then calls the driver's config_aneg function.
420 * If the PHYCONTROL Layer is operating, we change the state to
421 * reflect the beginning of Auto-negotiation or forcing.
Andy Fleminge1393452005-08-24 18:46:21 -0500422 */
423int phy_start_aneg(struct phy_device *phydev)
424{
425 int err;
426
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700427 spin_lock_bh(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500428
429 if (AUTONEG_DISABLE == phydev->autoneg)
430 phy_sanitize_settings(phydev);
431
432 err = phydev->drv->config_aneg(phydev);
433
Andy Fleminge1393452005-08-24 18:46:21 -0500434 if (err < 0)
435 goto out_unlock;
436
437 if (phydev->state != PHY_HALTED) {
438 if (AUTONEG_ENABLE == phydev->autoneg) {
439 phydev->state = PHY_AN;
440 phydev->link_timeout = PHY_AN_TIMEOUT;
441 } else {
442 phydev->state = PHY_FORCING;
443 phydev->link_timeout = PHY_FORCE_TIMEOUT;
444 }
445 }
446
447out_unlock:
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700448 spin_unlock_bh(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500449 return err;
450}
451EXPORT_SYMBOL(phy_start_aneg);
452
453
David Howellsc4028952006-11-22 14:57:56 +0000454static void phy_change(struct work_struct *work);
Andy Fleminge1393452005-08-24 18:46:21 -0500455static void phy_timer(unsigned long data);
456
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800457/**
458 * phy_start_machine - start PHY state machine tracking
459 * @phydev: the phy_device struct
460 * @handler: callback function for state change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400461 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800462 * Description: The PHY infrastructure can run a state machine
Andy Fleming00db8182005-07-30 19:31:23 -0400463 * which tracks whether the PHY is starting up, negotiating,
464 * etc. This function starts the timer which tracks the state
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800465 * of the PHY. If you want to be notified when the state changes,
466 * pass in the callback @handler, otherwise, pass NULL. If you
Andy Fleming00db8182005-07-30 19:31:23 -0400467 * want to maintain your own state machine, do not call this
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800468 * function.
469 */
Andy Fleming00db8182005-07-30 19:31:23 -0400470void phy_start_machine(struct phy_device *phydev,
471 void (*handler)(struct net_device *))
472{
473 phydev->adjust_state = handler;
474
475 init_timer(&phydev->phy_timer);
476 phydev->phy_timer.function = &phy_timer;
477 phydev->phy_timer.data = (unsigned long) phydev;
478 mod_timer(&phydev->phy_timer, jiffies + HZ);
479}
480
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800481/**
482 * phy_stop_machine - stop the PHY state machine tracking
483 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400484 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800485 * Description: Stops the state machine timer, sets the state to UP
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400486 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400487 * phy_detach.
488 */
489void phy_stop_machine(struct phy_device *phydev)
490{
491 del_timer_sync(&phydev->phy_timer);
492
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700493 spin_lock_bh(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400494 if (phydev->state > PHY_UP)
495 phydev->state = PHY_UP;
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700496 spin_unlock_bh(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400497
Andy Fleming00db8182005-07-30 19:31:23 -0400498 phydev->adjust_state = NULL;
499}
500
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800501/**
502 * phy_force_reduction - reduce PHY speed/duplex settings by one step
503 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500504 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800505 * Description: Reduces the speed/duplex settings by one notch,
506 * in this order--
507 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
508 * The function bottoms out at 10/HALF.
Andy Fleminge1393452005-08-24 18:46:21 -0500509 */
510static void phy_force_reduction(struct phy_device *phydev)
511{
512 int idx;
513
514 idx = phy_find_setting(phydev->speed, phydev->duplex);
515
516 idx++;
517
518 idx = phy_find_valid(idx, phydev->supported);
519
520 phydev->speed = settings[idx].speed;
521 phydev->duplex = settings[idx].duplex;
522
523 pr_info("Trying %d/%s\n", phydev->speed,
524 DUPLEX_FULL == phydev->duplex ?
525 "FULL" : "HALF");
526}
527
528
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800529/**
530 * phy_error - enter HALTED state for this PHY device
531 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400532 *
533 * Moves the PHY to the HALTED state in response to a read
534 * or write error, and tells the controller the link is down.
535 * Must not be called from interrupt context, or while the
536 * phydev->lock is held.
537 */
538void phy_error(struct phy_device *phydev)
539{
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700540 spin_lock_bh(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400541 phydev->state = PHY_HALTED;
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700542 spin_unlock_bh(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400543}
544
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800545/**
546 * phy_interrupt - PHY interrupt handler
547 * @irq: interrupt line
548 * @phy_dat: phy_device pointer
Andy Fleminge1393452005-08-24 18:46:21 -0500549 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800550 * Description: When a PHY interrupt occurs, the handler disables
Andy Fleminge1393452005-08-24 18:46:21 -0500551 * interrupts, and schedules a work task to clear the interrupt.
552 */
David Howells7d12e782006-10-05 14:55:46 +0100553static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500554{
555 struct phy_device *phydev = phy_dat;
556
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100557 if (PHY_HALTED == phydev->state)
558 return IRQ_NONE; /* It can't be ours. */
559
Andy Fleminge1393452005-08-24 18:46:21 -0500560 /* The MDIO bus is not allowed to be written in interrupt
561 * context, so we need to disable the irq here. A work
562 * queue will write the PHY to disable and clear the
563 * interrupt, and then reenable the irq line. */
564 disable_irq_nosync(irq);
565
566 schedule_work(&phydev->phy_queue);
567
568 return IRQ_HANDLED;
569}
570
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800571/**
572 * phy_enable_interrupts - Enable the interrupts from the PHY side
573 * @phydev: target phy_device struct
574 */
Andy Fleminge1393452005-08-24 18:46:21 -0500575int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400576{
577 int err;
578
Andy Fleminge1393452005-08-24 18:46:21 -0500579 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400580
Andy Fleminge1393452005-08-24 18:46:21 -0500581 if (err < 0)
582 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400583
Andy Fleminge1393452005-08-24 18:46:21 -0500584 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400585
586 return err;
587}
Andy Fleminge1393452005-08-24 18:46:21 -0500588EXPORT_SYMBOL(phy_enable_interrupts);
Andy Fleming00db8182005-07-30 19:31:23 -0400589
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800590/**
591 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
592 * @phydev: target phy_device struct
593 */
Andy Fleminge1393452005-08-24 18:46:21 -0500594int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400595{
596 int err;
597
598 /* Disable PHY interrupts */
599 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
600
601 if (err)
602 goto phy_err;
603
604 /* Clear the interrupt */
605 err = phy_clear_interrupt(phydev);
606
607 if (err)
608 goto phy_err;
609
610 return 0;
611
612phy_err:
613 phy_error(phydev);
614
615 return err;
616}
Andy Fleminge1393452005-08-24 18:46:21 -0500617EXPORT_SYMBOL(phy_disable_interrupts);
618
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800619/**
620 * phy_start_interrupts - request and enable interrupts for a PHY device
621 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500622 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800623 * Description: Request the interrupt for the given PHY.
624 * If this fails, then we set irq to PHY_POLL.
Andy Fleminge1393452005-08-24 18:46:21 -0500625 * Otherwise, we enable the interrupts in the PHY.
Andy Fleminge1393452005-08-24 18:46:21 -0500626 * This should only be called with a valid IRQ number.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800627 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500628 */
629int phy_start_interrupts(struct phy_device *phydev)
630{
631 int err = 0;
632
David Howellsc4028952006-11-22 14:57:56 +0000633 INIT_WORK(&phydev->phy_queue, phy_change);
Andy Fleminge1393452005-08-24 18:46:21 -0500634
635 if (request_irq(phydev->irq, phy_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700636 IRQF_SHARED,
Andy Fleminge1393452005-08-24 18:46:21 -0500637 "phy_interrupt",
638 phydev) < 0) {
639 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
640 phydev->bus->name,
641 phydev->irq);
642 phydev->irq = PHY_POLL;
643 return 0;
644 }
645
646 err = phy_enable_interrupts(phydev);
647
648 return err;
649}
650EXPORT_SYMBOL(phy_start_interrupts);
651
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800652/**
653 * phy_stop_interrupts - disable interrupts from a PHY device
654 * @phydev: target phy_device struct
655 */
Andy Fleminge1393452005-08-24 18:46:21 -0500656int phy_stop_interrupts(struct phy_device *phydev)
657{
658 int err;
659
660 err = phy_disable_interrupts(phydev);
661
662 if (err)
663 phy_error(phydev);
664
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100665 /*
Andrew Mortond0758bc2007-05-09 02:34:00 -0700666 * Finish any pending work; we might have been scheduled to be called
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700667 * from keventd ourselves, but cancel_work_sync() handles that.
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100668 */
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700669 cancel_work_sync(&phydev->phy_queue);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100670
Andy Fleminge1393452005-08-24 18:46:21 -0500671 free_irq(phydev->irq, phydev);
672
673 return err;
674}
675EXPORT_SYMBOL(phy_stop_interrupts);
676
677
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800678/**
679 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
680 * @work: work_struct that describes the work to be done
681 */
David Howellsc4028952006-11-22 14:57:56 +0000682static void phy_change(struct work_struct *work)
Andy Fleminge1393452005-08-24 18:46:21 -0500683{
684 int err;
David Howellsc4028952006-11-22 14:57:56 +0000685 struct phy_device *phydev =
686 container_of(work, struct phy_device, phy_queue);
Andy Fleminge1393452005-08-24 18:46:21 -0500687
688 err = phy_disable_interrupts(phydev);
689
690 if (err)
691 goto phy_err;
692
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700693 spin_lock_bh(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500694 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
695 phydev->state = PHY_CHANGELINK;
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700696 spin_unlock_bh(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500697
698 enable_irq(phydev->irq);
699
700 /* Reenable interrupts */
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100701 if (PHY_HALTED != phydev->state)
702 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500703
704 if (err)
705 goto irq_enable_err;
706
707 return;
708
709irq_enable_err:
710 disable_irq(phydev->irq);
711phy_err:
712 phy_error(phydev);
713}
714
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800715/**
716 * phy_stop - Bring down the PHY link, and stop checking the status
717 * @phydev: target phy_device struct
718 */
Andy Fleminge1393452005-08-24 18:46:21 -0500719void phy_stop(struct phy_device *phydev)
720{
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700721 spin_lock_bh(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500722
723 if (PHY_HALTED == phydev->state)
724 goto out_unlock;
725
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100726 phydev->state = PHY_HALTED;
Andy Fleminge1393452005-08-24 18:46:21 -0500727
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100728 if (phydev->irq != PHY_POLL) {
Andy Fleminge1393452005-08-24 18:46:21 -0500729 /* Disable PHY Interrupts */
730 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500731
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100732 /* Clear any pending interrupts */
733 phy_clear_interrupt(phydev);
734 }
Andy Fleminge1393452005-08-24 18:46:21 -0500735
736out_unlock:
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700737 spin_unlock_bh(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100738
739 /*
740 * Cannot call flush_scheduled_work() here as desired because
741 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
742 * will not reenable interrupts.
743 */
Andy Fleminge1393452005-08-24 18:46:21 -0500744}
745
746
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800747/**
748 * phy_start - start or restart a PHY device
749 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500750 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800751 * Description: Indicates the attached device's readiness to
Andy Fleminge1393452005-08-24 18:46:21 -0500752 * handle PHY-related work. Used during startup to start the
753 * PHY, and after a call to phy_stop() to resume operation.
754 * Also used to indicate the MDIO bus has cleared an error
755 * condition.
756 */
757void phy_start(struct phy_device *phydev)
758{
Hans-Jürgen Koch026d7912007-08-31 14:30:08 +0200759 spin_lock_bh(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500760
761 switch (phydev->state) {
762 case PHY_STARTING:
763 phydev->state = PHY_PENDING;
764 break;
765 case PHY_READY:
766 phydev->state = PHY_UP;
767 break;
768 case PHY_HALTED:
769 phydev->state = PHY_RESUMING;
770 default:
771 break;
772 }
Hans-Jürgen Koch026d7912007-08-31 14:30:08 +0200773 spin_unlock_bh(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500774}
775EXPORT_SYMBOL(phy_stop);
776EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400777
Andy Fleming00db8182005-07-30 19:31:23 -0400778/* PHY timer which handles the state machine */
779static void phy_timer(unsigned long data)
780{
781 struct phy_device *phydev = (struct phy_device *)data;
782 int needs_aneg = 0;
783 int err = 0;
784
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700785 spin_lock_bh(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400786
787 if (phydev->adjust_state)
788 phydev->adjust_state(phydev->attached_dev);
789
790 switch(phydev->state) {
791 case PHY_DOWN:
792 case PHY_STARTING:
793 case PHY_READY:
794 case PHY_PENDING:
795 break;
796 case PHY_UP:
797 needs_aneg = 1;
798
799 phydev->link_timeout = PHY_AN_TIMEOUT;
800
801 break;
802 case PHY_AN:
Andy Fleming6b655522006-10-16 16:19:17 -0500803 err = phy_read_status(phydev);
804
805 if (err < 0)
806 break;
807
808 /* If the link is down, give up on
809 * negotiation for now */
810 if (!phydev->link) {
811 phydev->state = PHY_NOLINK;
812 netif_carrier_off(phydev->attached_dev);
813 phydev->adjust_link(phydev->attached_dev);
814 break;
815 }
816
Andy Fleming00db8182005-07-30 19:31:23 -0400817 /* Check if negotiation is done. Break
818 * if there's an error */
819 err = phy_aneg_done(phydev);
820 if (err < 0)
821 break;
822
Andy Fleming6b655522006-10-16 16:19:17 -0500823 /* If AN is done, we're running */
Andy Fleming00db8182005-07-30 19:31:23 -0400824 if (err > 0) {
Andy Fleming6b655522006-10-16 16:19:17 -0500825 phydev->state = PHY_RUNNING;
826 netif_carrier_on(phydev->attached_dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400827 phydev->adjust_link(phydev->attached_dev);
828
829 } else if (0 == phydev->link_timeout--) {
Andy Fleming6b655522006-10-16 16:19:17 -0500830 int idx;
Andy Fleming00db8182005-07-30 19:31:23 -0400831
832 needs_aneg = 1;
Andy Fleming6b655522006-10-16 16:19:17 -0500833 /* If we have the magic_aneg bit,
834 * we try again */
835 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
836 break;
837
838 /* The timer expired, and we still
839 * don't have a setting, so we try
840 * forcing it until we find one that
841 * works, starting from the fastest speed,
842 * and working our way down */
843 idx = phy_find_valid(0, phydev->supported);
844
845 phydev->speed = settings[idx].speed;
846 phydev->duplex = settings[idx].duplex;
847
848 phydev->autoneg = AUTONEG_DISABLE;
849
850 pr_info("Trying %d/%s\n", phydev->speed,
851 DUPLEX_FULL ==
852 phydev->duplex ?
853 "FULL" : "HALF");
Andy Fleming00db8182005-07-30 19:31:23 -0400854 }
855 break;
856 case PHY_NOLINK:
857 err = phy_read_status(phydev);
858
859 if (err)
860 break;
861
862 if (phydev->link) {
863 phydev->state = PHY_RUNNING;
864 netif_carrier_on(phydev->attached_dev);
865 phydev->adjust_link(phydev->attached_dev);
866 }
867 break;
868 case PHY_FORCING:
Andy Fleming6b655522006-10-16 16:19:17 -0500869 err = genphy_update_link(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400870
871 if (err)
872 break;
873
874 if (phydev->link) {
875 phydev->state = PHY_RUNNING;
876 netif_carrier_on(phydev->attached_dev);
877 } else {
878 if (0 == phydev->link_timeout--) {
879 phy_force_reduction(phydev);
880 needs_aneg = 1;
881 }
882 }
883
884 phydev->adjust_link(phydev->attached_dev);
885 break;
886 case PHY_RUNNING:
887 /* Only register a CHANGE if we are
888 * polling */
889 if (PHY_POLL == phydev->irq)
890 phydev->state = PHY_CHANGELINK;
891 break;
892 case PHY_CHANGELINK:
893 err = phy_read_status(phydev);
894
895 if (err)
896 break;
897
898 if (phydev->link) {
899 phydev->state = PHY_RUNNING;
900 netif_carrier_on(phydev->attached_dev);
901 } else {
902 phydev->state = PHY_NOLINK;
903 netif_carrier_off(phydev->attached_dev);
904 }
905
906 phydev->adjust_link(phydev->attached_dev);
907
908 if (PHY_POLL != phydev->irq)
909 err = phy_config_interrupt(phydev,
910 PHY_INTERRUPT_ENABLED);
911 break;
912 case PHY_HALTED:
913 if (phydev->link) {
914 phydev->link = 0;
915 netif_carrier_off(phydev->attached_dev);
916 phydev->adjust_link(phydev->attached_dev);
917 }
918 break;
919 case PHY_RESUMING:
920
921 err = phy_clear_interrupt(phydev);
922
923 if (err)
924 break;
925
926 err = phy_config_interrupt(phydev,
927 PHY_INTERRUPT_ENABLED);
928
929 if (err)
930 break;
931
932 if (AUTONEG_ENABLE == phydev->autoneg) {
933 err = phy_aneg_done(phydev);
934 if (err < 0)
935 break;
936
937 /* err > 0 if AN is done.
938 * Otherwise, it's 0, and we're
939 * still waiting for AN */
940 if (err > 0) {
941 phydev->state = PHY_RUNNING;
942 } else {
943 phydev->state = PHY_AN;
944 phydev->link_timeout = PHY_AN_TIMEOUT;
945 }
946 } else
947 phydev->state = PHY_RUNNING;
948 break;
949 }
950
Maciej W. Rozycki9ff8c682007-09-28 22:42:12 -0700951 spin_unlock_bh(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400952
953 if (needs_aneg)
954 err = phy_start_aneg(phydev);
955
956 if (err < 0)
957 phy_error(phydev);
958
959 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
960}
961