blob: 20cc82c781370ec5fb2a1e27652c3e7e0769c862 [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. Rozycki0ac49522007-09-28 22:42:14 -070010 * Copyright (c) 2006, 2007 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>
Andy Fleming00db8182005-07-30 19:31:23 -040029#include <linux/mm.h>
30#include <linux/module.h>
Andy Fleming00db8182005-07-30 19:31:23 -040031#include <linux/mii.h>
32#include <linux/ethtool.h>
33#include <linux/phy.h>
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +010034#include <linux/timer.h>
35#include <linux/workqueue.h>
Andy Fleming00db8182005-07-30 19:31:23 -040036
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -070037#include <asm/atomic.h>
Andy Fleming00db8182005-07-30 19:31:23 -040038#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
Nate Case35b5f6b2008-01-29 10:05:09 -060074 BUG_ON(in_interrupt());
75
76 mutex_lock(&bus->mdio_lock);
Andy Fleming00db8182005-07-30 19:31:23 -040077 retval = bus->read(bus, phydev->addr, regnum);
Nate Case35b5f6b2008-01-29 10:05:09 -060078 mutex_unlock(&bus->mdio_lock);
Andy Fleming00db8182005-07-30 19:31:23 -040079
80 return retval;
81}
82EXPORT_SYMBOL(phy_read);
83
Randy Dunlapb3df0da2007-03-06 02:41:48 -080084/**
85 * phy_write - Convenience function for writing a given PHY register
86 * @phydev: the phy_device struct
87 * @regnum: register number to write
88 * @val: value to write to @regnum
89 *
90 * NOTE: MUST NOT be called from interrupt context,
91 * because the bus read/write functions may wait for an interrupt
92 * to conclude the operation.
93 */
Andy Fleming00db8182005-07-30 19:31:23 -040094int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
95{
96 int err;
97 struct mii_bus *bus = phydev->bus;
98
Nate Case35b5f6b2008-01-29 10:05:09 -060099 BUG_ON(in_interrupt());
100
101 mutex_lock(&bus->mdio_lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400102 err = bus->write(bus, phydev->addr, regnum, val);
Nate Case35b5f6b2008-01-29 10:05:09 -0600103 mutex_unlock(&bus->mdio_lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400104
105 return err;
106}
107EXPORT_SYMBOL(phy_write);
108
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800109/**
110 * phy_clear_interrupt - Ack the phy device's interrupt
111 * @phydev: the phy_device struct
112 *
113 * If the @phydev driver has an ack_interrupt function, call it to
114 * ack and clear the phy device's interrupt.
115 *
116 * Returns 0 on success on < 0 on error.
117 */
Andy Fleming00db8182005-07-30 19:31:23 -0400118int phy_clear_interrupt(struct phy_device *phydev)
119{
120 int err = 0;
121
122 if (phydev->drv->ack_interrupt)
123 err = phydev->drv->ack_interrupt(phydev);
124
125 return err;
126}
127
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800128/**
129 * phy_config_interrupt - configure the PHY device for the requested interrupts
130 * @phydev: the phy_device struct
131 * @interrupts: interrupt flags to configure for this @phydev
132 *
133 * Returns 0 on success on < 0 on error.
134 */
Andy Fleming00db8182005-07-30 19:31:23 -0400135int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
136{
137 int err = 0;
138
139 phydev->interrupts = interrupts;
140 if (phydev->drv->config_intr)
141 err = phydev->drv->config_intr(phydev);
142
143 return err;
144}
145
146
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800147/**
148 * phy_aneg_done - return auto-negotiation status
149 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400150 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800151 * Description: Reads the status register and returns 0 either if
Andy Fleming00db8182005-07-30 19:31:23 -0400152 * auto-negotiation is incomplete, or if there was an error.
153 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
154 */
155static inline int phy_aneg_done(struct phy_device *phydev)
156{
157 int retval;
158
159 retval = phy_read(phydev, MII_BMSR);
160
161 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
162}
163
Andy Fleming00db8182005-07-30 19:31:23 -0400164/* A structure for mapping a particular speed and duplex
165 * combination to a particular SUPPORTED and ADVERTISED value */
166struct phy_setting {
167 int speed;
168 int duplex;
169 u32 setting;
170};
171
172/* A mapping of all SUPPORTED settings to speed/duplex */
Arjan van de Venf71e1302006-03-03 21:33:57 -0500173static const struct phy_setting settings[] = {
Andy Fleming00db8182005-07-30 19:31:23 -0400174 {
175 .speed = 10000,
176 .duplex = DUPLEX_FULL,
177 .setting = SUPPORTED_10000baseT_Full,
178 },
179 {
180 .speed = SPEED_1000,
181 .duplex = DUPLEX_FULL,
182 .setting = SUPPORTED_1000baseT_Full,
183 },
184 {
185 .speed = SPEED_1000,
186 .duplex = DUPLEX_HALF,
187 .setting = SUPPORTED_1000baseT_Half,
188 },
189 {
190 .speed = SPEED_100,
191 .duplex = DUPLEX_FULL,
192 .setting = SUPPORTED_100baseT_Full,
193 },
194 {
195 .speed = SPEED_100,
196 .duplex = DUPLEX_HALF,
197 .setting = SUPPORTED_100baseT_Half,
198 },
199 {
200 .speed = SPEED_10,
201 .duplex = DUPLEX_FULL,
202 .setting = SUPPORTED_10baseT_Full,
203 },
204 {
205 .speed = SPEED_10,
206 .duplex = DUPLEX_HALF,
207 .setting = SUPPORTED_10baseT_Half,
208 },
209};
210
Denis Chengff8ac602007-09-02 18:30:18 +0800211#define MAX_NUM_SETTINGS ARRAY_SIZE(settings)
Andy Fleming00db8182005-07-30 19:31:23 -0400212
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800213/**
214 * phy_find_setting - find a PHY settings array entry that matches speed & duplex
215 * @speed: speed to match
216 * @duplex: duplex to match
Andy Fleming00db8182005-07-30 19:31:23 -0400217 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800218 * Description: Searches the settings array for the setting which
Andy Fleming00db8182005-07-30 19:31:23 -0400219 * matches the desired speed and duplex, and returns the index
220 * of that setting. Returns the index of the last setting if
221 * none of the others match.
222 */
223static inline int phy_find_setting(int speed, int duplex)
224{
225 int idx = 0;
226
227 while (idx < ARRAY_SIZE(settings) &&
228 (settings[idx].speed != speed ||
229 settings[idx].duplex != duplex))
230 idx++;
231
232 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
233}
234
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800235/**
236 * phy_find_valid - find a PHY setting that matches the requested features mask
237 * @idx: The first index in settings[] to search
238 * @features: A mask of the valid settings
Andy Fleming00db8182005-07-30 19:31:23 -0400239 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800240 * Description: Returns the index of the first valid setting less
Andy Fleming00db8182005-07-30 19:31:23 -0400241 * than or equal to the one pointed to by idx, as determined by
242 * the mask in features. Returns the index of the last setting
243 * if nothing else matches.
244 */
245static inline int phy_find_valid(int idx, u32 features)
246{
247 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
248 idx++;
249
250 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
251}
252
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800253/**
254 * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex
255 * @phydev: the target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400256 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800257 * Description: Make sure the PHY is set to supported speeds and
Andy Fleming00db8182005-07-30 19:31:23 -0400258 * duplexes. Drop down by one in this order: 1000/FULL,
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800259 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
Andy Fleming00db8182005-07-30 19:31:23 -0400260 */
Andy Fleminge1393452005-08-24 18:46:21 -0500261void phy_sanitize_settings(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400262{
263 u32 features = phydev->supported;
264 int idx;
265
266 /* Sanitize settings based on PHY capabilities */
267 if ((features & SUPPORTED_Autoneg) == 0)
Domen Puncer163642a2007-08-07 12:12:41 +0200268 phydev->autoneg = AUTONEG_DISABLE;
Andy Fleming00db8182005-07-30 19:31:23 -0400269
270 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
271 features);
272
273 phydev->speed = settings[idx].speed;
274 phydev->duplex = settings[idx].duplex;
275}
Andy Fleminge1393452005-08-24 18:46:21 -0500276EXPORT_SYMBOL(phy_sanitize_settings);
Andy Fleming00db8182005-07-30 19:31:23 -0400277
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800278/**
279 * phy_ethtool_sset - generic ethtool sset function, handles all the details
280 * @phydev: target phy_device struct
281 * @cmd: ethtool_cmd
Andy Fleming00db8182005-07-30 19:31:23 -0400282 *
283 * A few notes about parameter checking:
284 * - We don't set port or transceiver, so we don't care what they
285 * were set to.
286 * - phy_start_aneg() will make sure forced settings are sane, and
287 * choose the next best ones from the ones selected, so we don't
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800288 * care if ethtool tries to give us bad values.
Andy Fleming00db8182005-07-30 19:31:23 -0400289 */
290int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
291{
292 if (cmd->phy_address != phydev->addr)
293 return -EINVAL;
294
295 /* We make sure that we don't pass unsupported
296 * values in to the PHY */
297 cmd->advertising &= phydev->supported;
298
299 /* Verify the settings we care about. */
300 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
301 return -EINVAL;
302
303 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
304 return -EINVAL;
305
306 if (cmd->autoneg == AUTONEG_DISABLE
307 && ((cmd->speed != SPEED_1000
308 && cmd->speed != SPEED_100
309 && cmd->speed != SPEED_10)
310 || (cmd->duplex != DUPLEX_HALF
311 && cmd->duplex != DUPLEX_FULL)))
312 return -EINVAL;
313
314 phydev->autoneg = cmd->autoneg;
315
316 phydev->speed = cmd->speed;
317
318 phydev->advertising = cmd->advertising;
319
320 if (AUTONEG_ENABLE == cmd->autoneg)
321 phydev->advertising |= ADVERTISED_Autoneg;
322 else
323 phydev->advertising &= ~ADVERTISED_Autoneg;
324
325 phydev->duplex = cmd->duplex;
326
327 /* Restart the PHY */
328 phy_start_aneg(phydev);
329
330 return 0;
331}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600332EXPORT_SYMBOL(phy_ethtool_sset);
Andy Fleming00db8182005-07-30 19:31:23 -0400333
334int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
335{
336 cmd->supported = phydev->supported;
337
338 cmd->advertising = phydev->advertising;
339
340 cmd->speed = phydev->speed;
341 cmd->duplex = phydev->duplex;
342 cmd->port = PORT_MII;
343 cmd->phy_address = phydev->addr;
344 cmd->transceiver = XCVR_EXTERNAL;
345 cmd->autoneg = phydev->autoneg;
346
347 return 0;
348}
Kumar Gala9f6d55d2007-01-20 16:38:26 -0600349EXPORT_SYMBOL(phy_ethtool_gset);
Andy Fleming00db8182005-07-30 19:31:23 -0400350
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800351/**
352 * phy_mii_ioctl - generic PHY MII ioctl interface
353 * @phydev: the phy_device struct
354 * @mii_data: MII ioctl data
355 * @cmd: ioctl cmd to execute
356 *
357 * Note that this function is currently incompatible with the
Andy Fleming00db8182005-07-30 19:31:23 -0400358 * PHYCONTROL layer. It changes registers without regard to
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800359 * current state. Use at own risk.
Andy Fleming00db8182005-07-30 19:31:23 -0400360 */
361int phy_mii_ioctl(struct phy_device *phydev,
362 struct mii_ioctl_data *mii_data, int cmd)
363{
364 u16 val = mii_data->val_in;
365
366 switch (cmd) {
367 case SIOCGMIIPHY:
368 mii_data->phy_id = phydev->addr;
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000369 /* fall through */
370
Andy Fleming00db8182005-07-30 19:31:23 -0400371 case SIOCGMIIREG:
372 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
373 break;
374
375 case SIOCSMIIREG:
376 if (!capable(CAP_NET_ADMIN))
377 return -EPERM;
378
379 if (mii_data->phy_id == phydev->addr) {
380 switch(mii_data->reg_num) {
381 case MII_BMCR:
Domen Puncer163642a2007-08-07 12:12:41 +0200382 if ((val & (BMCR_RESET|BMCR_ANENABLE)) == 0)
Andy Fleming00db8182005-07-30 19:31:23 -0400383 phydev->autoneg = AUTONEG_DISABLE;
384 else
385 phydev->autoneg = AUTONEG_ENABLE;
386 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
387 phydev->duplex = DUPLEX_FULL;
388 else
389 phydev->duplex = DUPLEX_HALF;
Shan Lu024a0a32007-03-06 02:42:03 -0800390 if ((!phydev->autoneg) &&
391 (val & BMCR_SPEED1000))
392 phydev->speed = SPEED_1000;
393 else if ((!phydev->autoneg) &&
394 (val & BMCR_SPEED100))
395 phydev->speed = SPEED_100;
Andy Fleming00db8182005-07-30 19:31:23 -0400396 break;
397 case MII_ADVERTISE:
398 phydev->advertising = val;
399 break;
400 default:
401 /* do nothing */
402 break;
403 }
404 }
405
406 phy_write(phydev, mii_data->reg_num, val);
407
408 if (mii_data->reg_num == MII_BMCR
409 && val & BMCR_RESET
Andy Flemingf62220d2008-04-18 17:29:54 -0500410 && phydev->drv->config_init) {
411 phy_scan_fixups(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400412 phydev->drv->config_init(phydev);
Andy Flemingf62220d2008-04-18 17:29:54 -0500413 }
Andy Fleming00db8182005-07-30 19:31:23 -0400414 break;
David Woodhousedda93b42007-11-28 19:56:34 +0000415
416 default:
Lennert Buytenhekc6d6a512008-09-18 03:06:52 +0000417 return -EOPNOTSUPP;
Andy Fleming00db8182005-07-30 19:31:23 -0400418 }
419
420 return 0;
421}
Domen Puncer680e9fe2007-09-17 22:21:40 +0200422EXPORT_SYMBOL(phy_mii_ioctl);
Andy Fleming00db8182005-07-30 19:31:23 -0400423
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800424/**
425 * phy_start_aneg - start auto-negotiation for this PHY device
426 * @phydev: the phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500427 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800428 * Description: Sanitizes the settings (if we're not autonegotiating
429 * them), and then calls the driver's config_aneg function.
430 * If the PHYCONTROL Layer is operating, we change the state to
431 * reflect the beginning of Auto-negotiation or forcing.
Andy Fleminge1393452005-08-24 18:46:21 -0500432 */
433int phy_start_aneg(struct phy_device *phydev)
434{
435 int err;
436
Nate Case35b5f6b2008-01-29 10:05:09 -0600437 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500438
439 if (AUTONEG_DISABLE == phydev->autoneg)
440 phy_sanitize_settings(phydev);
441
442 err = phydev->drv->config_aneg(phydev);
443
Andy Fleminge1393452005-08-24 18:46:21 -0500444 if (err < 0)
445 goto out_unlock;
446
447 if (phydev->state != PHY_HALTED) {
448 if (AUTONEG_ENABLE == phydev->autoneg) {
449 phydev->state = PHY_AN;
450 phydev->link_timeout = PHY_AN_TIMEOUT;
451 } else {
452 phydev->state = PHY_FORCING;
453 phydev->link_timeout = PHY_FORCE_TIMEOUT;
454 }
455 }
456
457out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600458 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500459 return err;
460}
461EXPORT_SYMBOL(phy_start_aneg);
462
463
David Howellsc4028952006-11-22 14:57:56 +0000464static void phy_change(struct work_struct *work);
Nate Case35b5f6b2008-01-29 10:05:09 -0600465static void phy_state_machine(struct work_struct *work);
Andy Fleminge1393452005-08-24 18:46:21 -0500466static void phy_timer(unsigned long data);
467
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800468/**
469 * phy_start_machine - start PHY state machine tracking
470 * @phydev: the phy_device struct
471 * @handler: callback function for state change notifications
Andy Fleming00db8182005-07-30 19:31:23 -0400472 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800473 * Description: The PHY infrastructure can run a state machine
Andy Fleming00db8182005-07-30 19:31:23 -0400474 * which tracks whether the PHY is starting up, negotiating,
475 * etc. This function starts the timer which tracks the state
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800476 * of the PHY. If you want to be notified when the state changes,
477 * pass in the callback @handler, otherwise, pass NULL. If you
Andy Fleming00db8182005-07-30 19:31:23 -0400478 * want to maintain your own state machine, do not call this
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800479 * function.
480 */
Andy Fleming00db8182005-07-30 19:31:23 -0400481void phy_start_machine(struct phy_device *phydev,
482 void (*handler)(struct net_device *))
483{
484 phydev->adjust_state = handler;
485
Nate Case35b5f6b2008-01-29 10:05:09 -0600486 INIT_WORK(&phydev->state_queue, phy_state_machine);
Andy Fleming00db8182005-07-30 19:31:23 -0400487 init_timer(&phydev->phy_timer);
488 phydev->phy_timer.function = &phy_timer;
489 phydev->phy_timer.data = (unsigned long) phydev;
490 mod_timer(&phydev->phy_timer, jiffies + HZ);
491}
492
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800493/**
494 * phy_stop_machine - stop the PHY state machine tracking
495 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400496 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800497 * Description: Stops the state machine timer, sets the state to UP
Sergei Shtylylov817acf52006-07-26 00:53:53 +0400498 * (unless it wasn't up yet). This function must be called BEFORE
Andy Fleming00db8182005-07-30 19:31:23 -0400499 * phy_detach.
500 */
501void phy_stop_machine(struct phy_device *phydev)
502{
503 del_timer_sync(&phydev->phy_timer);
Nate Case35b5f6b2008-01-29 10:05:09 -0600504 cancel_work_sync(&phydev->state_queue);
Andy Fleming00db8182005-07-30 19:31:23 -0400505
Nate Case35b5f6b2008-01-29 10:05:09 -0600506 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400507 if (phydev->state > PHY_UP)
508 phydev->state = PHY_UP;
Nate Case35b5f6b2008-01-29 10:05:09 -0600509 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400510
Andy Fleming00db8182005-07-30 19:31:23 -0400511 phydev->adjust_state = NULL;
512}
513
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800514/**
515 * phy_force_reduction - reduce PHY speed/duplex settings by one step
516 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500517 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800518 * Description: Reduces the speed/duplex settings by one notch,
519 * in this order--
520 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF.
521 * The function bottoms out at 10/HALF.
Andy Fleminge1393452005-08-24 18:46:21 -0500522 */
523static void phy_force_reduction(struct phy_device *phydev)
524{
525 int idx;
526
527 idx = phy_find_setting(phydev->speed, phydev->duplex);
528
529 idx++;
530
531 idx = phy_find_valid(idx, phydev->supported);
532
533 phydev->speed = settings[idx].speed;
534 phydev->duplex = settings[idx].duplex;
535
536 pr_info("Trying %d/%s\n", phydev->speed,
537 DUPLEX_FULL == phydev->duplex ?
538 "FULL" : "HALF");
539}
540
541
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800542/**
543 * phy_error - enter HALTED state for this PHY device
544 * @phydev: target phy_device struct
Andy Fleming00db8182005-07-30 19:31:23 -0400545 *
546 * Moves the PHY to the HALTED state in response to a read
547 * or write error, and tells the controller the link is down.
548 * Must not be called from interrupt context, or while the
549 * phydev->lock is held.
550 */
Andy Fleming9b9a8bf2008-05-02 13:00:51 -0500551static void phy_error(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400552{
Nate Case35b5f6b2008-01-29 10:05:09 -0600553 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400554 phydev->state = PHY_HALTED;
Nate Case35b5f6b2008-01-29 10:05:09 -0600555 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400556}
557
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800558/**
559 * phy_interrupt - PHY interrupt handler
560 * @irq: interrupt line
561 * @phy_dat: phy_device pointer
Andy Fleminge1393452005-08-24 18:46:21 -0500562 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800563 * Description: When a PHY interrupt occurs, the handler disables
Andy Fleminge1393452005-08-24 18:46:21 -0500564 * interrupts, and schedules a work task to clear the interrupt.
565 */
David Howells7d12e782006-10-05 14:55:46 +0100566static irqreturn_t phy_interrupt(int irq, void *phy_dat)
Andy Fleminge1393452005-08-24 18:46:21 -0500567{
568 struct phy_device *phydev = phy_dat;
569
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100570 if (PHY_HALTED == phydev->state)
571 return IRQ_NONE; /* It can't be ours. */
572
Andy Fleminge1393452005-08-24 18:46:21 -0500573 /* The MDIO bus is not allowed to be written in interrupt
574 * context, so we need to disable the irq here. A work
575 * queue will write the PHY to disable and clear the
576 * interrupt, and then reenable the irq line. */
577 disable_irq_nosync(irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700578 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500579
580 schedule_work(&phydev->phy_queue);
581
582 return IRQ_HANDLED;
583}
584
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800585/**
586 * phy_enable_interrupts - Enable the interrupts from the PHY side
587 * @phydev: target phy_device struct
588 */
Andy Fleminge1393452005-08-24 18:46:21 -0500589int phy_enable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400590{
591 int err;
592
Andy Fleminge1393452005-08-24 18:46:21 -0500593 err = phy_clear_interrupt(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400594
Andy Fleminge1393452005-08-24 18:46:21 -0500595 if (err < 0)
596 return err;
Andy Fleming00db8182005-07-30 19:31:23 -0400597
Andy Fleminge1393452005-08-24 18:46:21 -0500598 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleming00db8182005-07-30 19:31:23 -0400599
600 return err;
601}
Andy Fleminge1393452005-08-24 18:46:21 -0500602EXPORT_SYMBOL(phy_enable_interrupts);
Andy Fleming00db8182005-07-30 19:31:23 -0400603
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800604/**
605 * phy_disable_interrupts - Disable the PHY interrupts from the PHY side
606 * @phydev: target phy_device struct
607 */
Andy Fleminge1393452005-08-24 18:46:21 -0500608int phy_disable_interrupts(struct phy_device *phydev)
Andy Fleming00db8182005-07-30 19:31:23 -0400609{
610 int err;
611
612 /* Disable PHY interrupts */
613 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
614
615 if (err)
616 goto phy_err;
617
618 /* Clear the interrupt */
619 err = phy_clear_interrupt(phydev);
620
621 if (err)
622 goto phy_err;
623
624 return 0;
625
626phy_err:
627 phy_error(phydev);
628
629 return err;
630}
Andy Fleminge1393452005-08-24 18:46:21 -0500631EXPORT_SYMBOL(phy_disable_interrupts);
632
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800633/**
634 * phy_start_interrupts - request and enable interrupts for a PHY device
635 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500636 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800637 * Description: Request the interrupt for the given PHY.
638 * If this fails, then we set irq to PHY_POLL.
Andy Fleminge1393452005-08-24 18:46:21 -0500639 * Otherwise, we enable the interrupts in the PHY.
Andy Fleminge1393452005-08-24 18:46:21 -0500640 * This should only be called with a valid IRQ number.
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800641 * Returns 0 on success or < 0 on error.
Andy Fleminge1393452005-08-24 18:46:21 -0500642 */
643int phy_start_interrupts(struct phy_device *phydev)
644{
645 int err = 0;
646
David Howellsc4028952006-11-22 14:57:56 +0000647 INIT_WORK(&phydev->phy_queue, phy_change);
Andy Fleminge1393452005-08-24 18:46:21 -0500648
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700649 atomic_set(&phydev->irq_disable, 0);
Andy Fleminge1393452005-08-24 18:46:21 -0500650 if (request_irq(phydev->irq, phy_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700651 IRQF_SHARED,
Andy Fleminge1393452005-08-24 18:46:21 -0500652 "phy_interrupt",
653 phydev) < 0) {
654 printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n",
655 phydev->bus->name,
656 phydev->irq);
657 phydev->irq = PHY_POLL;
658 return 0;
659 }
660
661 err = phy_enable_interrupts(phydev);
662
663 return err;
664}
665EXPORT_SYMBOL(phy_start_interrupts);
666
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800667/**
668 * phy_stop_interrupts - disable interrupts from a PHY device
669 * @phydev: target phy_device struct
670 */
Andy Fleminge1393452005-08-24 18:46:21 -0500671int phy_stop_interrupts(struct phy_device *phydev)
672{
673 int err;
674
675 err = phy_disable_interrupts(phydev);
676
677 if (err)
678 phy_error(phydev);
679
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700680 free_irq(phydev->irq, phydev);
681
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100682 /*
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700683 * Cannot call flush_scheduled_work() here as desired because
684 * of rtnl_lock(), but we do not really care about what would
685 * be done, except from enable_irq(), so cancel any work
686 * possibly pending and take care of the matter below.
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100687 */
Oleg Nesterov28e53bd2007-05-09 02:34:22 -0700688 cancel_work_sync(&phydev->phy_queue);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700689 /*
690 * If work indeed has been cancelled, disable_irq() will have
691 * been left unbalanced from phy_interrupt() and enable_irq()
692 * has to be called so that other devices on the line work.
693 */
694 while (atomic_dec_return(&phydev->irq_disable) >= 0)
695 enable_irq(phydev->irq);
Andy Fleminge1393452005-08-24 18:46:21 -0500696
697 return err;
698}
699EXPORT_SYMBOL(phy_stop_interrupts);
700
701
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800702/**
703 * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes
704 * @work: work_struct that describes the work to be done
705 */
David Howellsc4028952006-11-22 14:57:56 +0000706static void phy_change(struct work_struct *work)
Andy Fleminge1393452005-08-24 18:46:21 -0500707{
708 int err;
David Howellsc4028952006-11-22 14:57:56 +0000709 struct phy_device *phydev =
710 container_of(work, struct phy_device, phy_queue);
Andy Fleminge1393452005-08-24 18:46:21 -0500711
712 err = phy_disable_interrupts(phydev);
713
714 if (err)
715 goto phy_err;
716
Nate Case35b5f6b2008-01-29 10:05:09 -0600717 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500718 if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state))
719 phydev->state = PHY_CHANGELINK;
Nate Case35b5f6b2008-01-29 10:05:09 -0600720 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500721
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700722 atomic_dec(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500723 enable_irq(phydev->irq);
724
725 /* Reenable interrupts */
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100726 if (PHY_HALTED != phydev->state)
727 err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500728
729 if (err)
730 goto irq_enable_err;
731
Trent Piepho0acb2832008-10-08 15:46:57 -0700732 /* Stop timer and run the state queue now. The work function for
733 * state_queue will start the timer up again.
734 */
735 del_timer(&phydev->phy_timer);
736 schedule_work(&phydev->state_queue);
737
Andy Fleminge1393452005-08-24 18:46:21 -0500738 return;
739
740irq_enable_err:
741 disable_irq(phydev->irq);
Maciej W. Rozycki0ac49522007-09-28 22:42:14 -0700742 atomic_inc(&phydev->irq_disable);
Andy Fleminge1393452005-08-24 18:46:21 -0500743phy_err:
744 phy_error(phydev);
745}
746
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800747/**
748 * phy_stop - Bring down the PHY link, and stop checking the status
749 * @phydev: target phy_device struct
750 */
Andy Fleminge1393452005-08-24 18:46:21 -0500751void phy_stop(struct phy_device *phydev)
752{
Nate Case35b5f6b2008-01-29 10:05:09 -0600753 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500754
755 if (PHY_HALTED == phydev->state)
756 goto out_unlock;
757
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100758 if (phydev->irq != PHY_POLL) {
Andy Fleminge1393452005-08-24 18:46:21 -0500759 /* Disable PHY Interrupts */
760 phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
Andy Fleminge1393452005-08-24 18:46:21 -0500761
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100762 /* Clear any pending interrupts */
763 phy_clear_interrupt(phydev);
764 }
Andy Fleminge1393452005-08-24 18:46:21 -0500765
Maciej W. Rozycki6daf6532007-09-28 22:42:15 -0700766 phydev->state = PHY_HALTED;
767
Andy Fleminge1393452005-08-24 18:46:21 -0500768out_unlock:
Nate Case35b5f6b2008-01-29 10:05:09 -0600769 mutex_unlock(&phydev->lock);
Maciej W. Rozycki3c3070d2006-10-03 16:18:35 +0100770
771 /*
772 * Cannot call flush_scheduled_work() here as desired because
773 * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change()
774 * will not reenable interrupts.
775 */
Andy Fleminge1393452005-08-24 18:46:21 -0500776}
777
778
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800779/**
780 * phy_start - start or restart a PHY device
781 * @phydev: target phy_device struct
Andy Fleminge1393452005-08-24 18:46:21 -0500782 *
Randy Dunlapb3df0da2007-03-06 02:41:48 -0800783 * Description: Indicates the attached device's readiness to
Andy Fleminge1393452005-08-24 18:46:21 -0500784 * handle PHY-related work. Used during startup to start the
785 * PHY, and after a call to phy_stop() to resume operation.
786 * Also used to indicate the MDIO bus has cleared an error
787 * condition.
788 */
789void phy_start(struct phy_device *phydev)
790{
Nate Case35b5f6b2008-01-29 10:05:09 -0600791 mutex_lock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500792
793 switch (phydev->state) {
794 case PHY_STARTING:
795 phydev->state = PHY_PENDING;
796 break;
797 case PHY_READY:
798 phydev->state = PHY_UP;
799 break;
800 case PHY_HALTED:
801 phydev->state = PHY_RESUMING;
802 default:
803 break;
804 }
Nate Case35b5f6b2008-01-29 10:05:09 -0600805 mutex_unlock(&phydev->lock);
Andy Fleminge1393452005-08-24 18:46:21 -0500806}
807EXPORT_SYMBOL(phy_stop);
808EXPORT_SYMBOL(phy_start);
Jeff Garzik67c4f3f2005-08-11 02:07:25 -0400809
Nate Case35b5f6b2008-01-29 10:05:09 -0600810/**
811 * phy_state_machine - Handle the state machine
812 * @work: work_struct that describes the work to be done
813 *
814 * Description: Scheduled by the state_queue workqueue each time
815 * phy_timer is triggered.
816 */
817static void phy_state_machine(struct work_struct *work)
Andy Fleming00db8182005-07-30 19:31:23 -0400818{
Nate Case35b5f6b2008-01-29 10:05:09 -0600819 struct phy_device *phydev =
820 container_of(work, struct phy_device, state_queue);
Andy Fleming00db8182005-07-30 19:31:23 -0400821 int needs_aneg = 0;
822 int err = 0;
823
Nate Case35b5f6b2008-01-29 10:05:09 -0600824 mutex_lock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400825
826 if (phydev->adjust_state)
827 phydev->adjust_state(phydev->attached_dev);
828
829 switch(phydev->state) {
830 case PHY_DOWN:
831 case PHY_STARTING:
832 case PHY_READY:
833 case PHY_PENDING:
834 break;
835 case PHY_UP:
836 needs_aneg = 1;
837
838 phydev->link_timeout = PHY_AN_TIMEOUT;
839
840 break;
841 case PHY_AN:
Andy Fleming6b655522006-10-16 16:19:17 -0500842 err = phy_read_status(phydev);
843
844 if (err < 0)
845 break;
846
847 /* If the link is down, give up on
848 * negotiation for now */
849 if (!phydev->link) {
850 phydev->state = PHY_NOLINK;
851 netif_carrier_off(phydev->attached_dev);
852 phydev->adjust_link(phydev->attached_dev);
853 break;
854 }
855
Andy Fleming00db8182005-07-30 19:31:23 -0400856 /* Check if negotiation is done. Break
857 * if there's an error */
858 err = phy_aneg_done(phydev);
859 if (err < 0)
860 break;
861
Andy Fleming6b655522006-10-16 16:19:17 -0500862 /* If AN is done, we're running */
Andy Fleming00db8182005-07-30 19:31:23 -0400863 if (err > 0) {
Andy Fleming6b655522006-10-16 16:19:17 -0500864 phydev->state = PHY_RUNNING;
865 netif_carrier_on(phydev->attached_dev);
Andy Fleming00db8182005-07-30 19:31:23 -0400866 phydev->adjust_link(phydev->attached_dev);
867
868 } else if (0 == phydev->link_timeout--) {
Andy Fleming6b655522006-10-16 16:19:17 -0500869 int idx;
Andy Fleming00db8182005-07-30 19:31:23 -0400870
871 needs_aneg = 1;
Andy Fleming6b655522006-10-16 16:19:17 -0500872 /* If we have the magic_aneg bit,
873 * we try again */
874 if (phydev->drv->flags & PHY_HAS_MAGICANEG)
875 break;
876
877 /* The timer expired, and we still
878 * don't have a setting, so we try
879 * forcing it until we find one that
880 * works, starting from the fastest speed,
881 * and working our way down */
882 idx = phy_find_valid(0, phydev->supported);
883
884 phydev->speed = settings[idx].speed;
885 phydev->duplex = settings[idx].duplex;
886
887 phydev->autoneg = AUTONEG_DISABLE;
888
889 pr_info("Trying %d/%s\n", phydev->speed,
890 DUPLEX_FULL ==
891 phydev->duplex ?
892 "FULL" : "HALF");
Andy Fleming00db8182005-07-30 19:31:23 -0400893 }
894 break;
895 case PHY_NOLINK:
896 err = phy_read_status(phydev);
897
898 if (err)
899 break;
900
901 if (phydev->link) {
902 phydev->state = PHY_RUNNING;
903 netif_carrier_on(phydev->attached_dev);
904 phydev->adjust_link(phydev->attached_dev);
905 }
906 break;
907 case PHY_FORCING:
Andy Fleming6b655522006-10-16 16:19:17 -0500908 err = genphy_update_link(phydev);
Andy Fleming00db8182005-07-30 19:31:23 -0400909
910 if (err)
911 break;
912
913 if (phydev->link) {
914 phydev->state = PHY_RUNNING;
915 netif_carrier_on(phydev->attached_dev);
916 } else {
917 if (0 == phydev->link_timeout--) {
918 phy_force_reduction(phydev);
919 needs_aneg = 1;
920 }
921 }
922
923 phydev->adjust_link(phydev->attached_dev);
924 break;
925 case PHY_RUNNING:
926 /* Only register a CHANGE if we are
927 * polling */
928 if (PHY_POLL == phydev->irq)
929 phydev->state = PHY_CHANGELINK;
930 break;
931 case PHY_CHANGELINK:
932 err = phy_read_status(phydev);
933
934 if (err)
935 break;
936
937 if (phydev->link) {
938 phydev->state = PHY_RUNNING;
939 netif_carrier_on(phydev->attached_dev);
940 } else {
941 phydev->state = PHY_NOLINK;
942 netif_carrier_off(phydev->attached_dev);
943 }
944
945 phydev->adjust_link(phydev->attached_dev);
946
947 if (PHY_POLL != phydev->irq)
948 err = phy_config_interrupt(phydev,
949 PHY_INTERRUPT_ENABLED);
950 break;
951 case PHY_HALTED:
952 if (phydev->link) {
953 phydev->link = 0;
954 netif_carrier_off(phydev->attached_dev);
955 phydev->adjust_link(phydev->attached_dev);
956 }
957 break;
958 case PHY_RESUMING:
959
960 err = phy_clear_interrupt(phydev);
961
962 if (err)
963 break;
964
965 err = phy_config_interrupt(phydev,
966 PHY_INTERRUPT_ENABLED);
967
968 if (err)
969 break;
970
971 if (AUTONEG_ENABLE == phydev->autoneg) {
972 err = phy_aneg_done(phydev);
973 if (err < 0)
974 break;
975
976 /* err > 0 if AN is done.
977 * Otherwise, it's 0, and we're
978 * still waiting for AN */
979 if (err > 0) {
980 phydev->state = PHY_RUNNING;
981 } else {
982 phydev->state = PHY_AN;
983 phydev->link_timeout = PHY_AN_TIMEOUT;
984 }
985 } else
986 phydev->state = PHY_RUNNING;
987 break;
988 }
989
Nate Case35b5f6b2008-01-29 10:05:09 -0600990 mutex_unlock(&phydev->lock);
Andy Fleming00db8182005-07-30 19:31:23 -0400991
992 if (needs_aneg)
993 err = phy_start_aneg(phydev);
994
995 if (err < 0)
996 phy_error(phydev);
997
998 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);
999}
1000
Nate Case35b5f6b2008-01-29 10:05:09 -06001001/* PHY timer which schedules the state machine work */
1002static void phy_timer(unsigned long data)
1003{
1004 struct phy_device *phydev = (struct phy_device *)data;
1005
1006 /*
1007 * PHY I/O operations can potentially sleep so we ensure that
1008 * it's done from a process context
1009 */
1010 schedule_work(&phydev->state_queue);
1011}