Commit | Line | Data |
---|---|---|
00db8189 AF |
1 | /* |
2 | * drivers/net/phy/phy.c | |
3 | * | |
4 | * Framework for configuring and reading PHY devices | |
5 | * Based on code in sungem_phy.c and gianfar_phy.c | |
6 | * | |
7 | * Author: Andy Fleming | |
8 | * | |
9 | * Copyright (c) 2004 Freescale Semiconductor, Inc. | |
0ac49527 | 10 | * Copyright (c) 2006, 2007 Maciej W. Rozycki |
00db8189 AF |
11 | * |
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 | */ | |
00db8189 | 18 | #include <linux/kernel.h> |
00db8189 AF |
19 | #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> | |
00db8189 AF |
32 | #include <linux/mii.h> |
33 | #include <linux/ethtool.h> | |
34 | #include <linux/phy.h> | |
3c3070d7 MR |
35 | #include <linux/timer.h> |
36 | #include <linux/workqueue.h> | |
00db8189 | 37 | |
0ac49527 | 38 | #include <asm/atomic.h> |
00db8189 AF |
39 | #include <asm/io.h> |
40 | #include <asm/irq.h> | |
41 | #include <asm/uaccess.h> | |
42 | ||
b3df0da8 RD |
43 | /** |
44 | * phy_print_status - Convenience function to print out the current phy status | |
45 | * @phydev: the phy_device struct | |
e1393456 AF |
46 | */ |
47 | void phy_print_status(struct phy_device *phydev) | |
48 | { | |
a4d00f17 | 49 | pr_info("PHY: %s - Link is %s", phydev->dev.bus_id, |
e1393456 AF |
50 | phydev->link ? "Up" : "Down"); |
51 | if (phydev->link) | |
52 | printk(" - %d/%s", phydev->speed, | |
53 | DUPLEX_FULL == phydev->duplex ? | |
54 | "Full" : "Half"); | |
55 | ||
56 | printk("\n"); | |
57 | } | |
58 | EXPORT_SYMBOL(phy_print_status); | |
00db8189 AF |
59 | |
60 | ||
b3df0da8 RD |
61 | /** |
62 | * phy_read - Convenience function for reading a given PHY register | |
63 | * @phydev: the phy_device struct | |
64 | * @regnum: register number to read | |
65 | * | |
66 | * NOTE: MUST NOT be called from interrupt context, | |
00db8189 | 67 | * because the bus read/write functions may wait for an interrupt |
b3df0da8 RD |
68 | * to conclude the operation. |
69 | */ | |
00db8189 AF |
70 | int phy_read(struct phy_device *phydev, u16 regnum) |
71 | { | |
72 | int retval; | |
73 | struct mii_bus *bus = phydev->bus; | |
74 | ||
75 | spin_lock_bh(&bus->mdio_lock); | |
76 | retval = bus->read(bus, phydev->addr, regnum); | |
77 | spin_unlock_bh(&bus->mdio_lock); | |
78 | ||
79 | return retval; | |
80 | } | |
81 | EXPORT_SYMBOL(phy_read); | |
82 | ||
b3df0da8 RD |
83 | /** |
84 | * phy_write - Convenience function for writing a given PHY register | |
85 | * @phydev: the phy_device struct | |
86 | * @regnum: register number to write | |
87 | * @val: value to write to @regnum | |
88 | * | |
89 | * NOTE: MUST NOT be called from interrupt context, | |
90 | * because the bus read/write functions may wait for an interrupt | |
91 | * to conclude the operation. | |
92 | */ | |
00db8189 AF |
93 | int phy_write(struct phy_device *phydev, u16 regnum, u16 val) |
94 | { | |
95 | int err; | |
96 | struct mii_bus *bus = phydev->bus; | |
97 | ||
98 | spin_lock_bh(&bus->mdio_lock); | |
99 | err = bus->write(bus, phydev->addr, regnum, val); | |
100 | spin_unlock_bh(&bus->mdio_lock); | |
101 | ||
102 | return err; | |
103 | } | |
104 | EXPORT_SYMBOL(phy_write); | |
105 | ||
b3df0da8 RD |
106 | /** |
107 | * phy_clear_interrupt - Ack the phy device's interrupt | |
108 | * @phydev: the phy_device struct | |
109 | * | |
110 | * If the @phydev driver has an ack_interrupt function, call it to | |
111 | * ack and clear the phy device's interrupt. | |
112 | * | |
113 | * Returns 0 on success on < 0 on error. | |
114 | */ | |
00db8189 AF |
115 | int phy_clear_interrupt(struct phy_device *phydev) |
116 | { | |
117 | int err = 0; | |
118 | ||
119 | if (phydev->drv->ack_interrupt) | |
120 | err = phydev->drv->ack_interrupt(phydev); | |
121 | ||
122 | return err; | |
123 | } | |
124 | ||
b3df0da8 RD |
125 | /** |
126 | * phy_config_interrupt - configure the PHY device for the requested interrupts | |
127 | * @phydev: the phy_device struct | |
128 | * @interrupts: interrupt flags to configure for this @phydev | |
129 | * | |
130 | * Returns 0 on success on < 0 on error. | |
131 | */ | |
00db8189 AF |
132 | int phy_config_interrupt(struct phy_device *phydev, u32 interrupts) |
133 | { | |
134 | int err = 0; | |
135 | ||
136 | phydev->interrupts = interrupts; | |
137 | if (phydev->drv->config_intr) | |
138 | err = phydev->drv->config_intr(phydev); | |
139 | ||
140 | return err; | |
141 | } | |
142 | ||
143 | ||
b3df0da8 RD |
144 | /** |
145 | * phy_aneg_done - return auto-negotiation status | |
146 | * @phydev: target phy_device struct | |
00db8189 | 147 | * |
b3df0da8 | 148 | * Description: Reads the status register and returns 0 either if |
00db8189 AF |
149 | * auto-negotiation is incomplete, or if there was an error. |
150 | * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done. | |
151 | */ | |
152 | static inline int phy_aneg_done(struct phy_device *phydev) | |
153 | { | |
154 | int retval; | |
155 | ||
156 | retval = phy_read(phydev, MII_BMSR); | |
157 | ||
158 | return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE); | |
159 | } | |
160 | ||
00db8189 AF |
161 | /* A structure for mapping a particular speed and duplex |
162 | * combination to a particular SUPPORTED and ADVERTISED value */ | |
163 | struct phy_setting { | |
164 | int speed; | |
165 | int duplex; | |
166 | u32 setting; | |
167 | }; | |
168 | ||
169 | /* A mapping of all SUPPORTED settings to speed/duplex */ | |
f71e1309 | 170 | static const struct phy_setting settings[] = { |
00db8189 AF |
171 | { |
172 | .speed = 10000, | |
173 | .duplex = DUPLEX_FULL, | |
174 | .setting = SUPPORTED_10000baseT_Full, | |
175 | }, | |
176 | { | |
177 | .speed = SPEED_1000, | |
178 | .duplex = DUPLEX_FULL, | |
179 | .setting = SUPPORTED_1000baseT_Full, | |
180 | }, | |
181 | { | |
182 | .speed = SPEED_1000, | |
183 | .duplex = DUPLEX_HALF, | |
184 | .setting = SUPPORTED_1000baseT_Half, | |
185 | }, | |
186 | { | |
187 | .speed = SPEED_100, | |
188 | .duplex = DUPLEX_FULL, | |
189 | .setting = SUPPORTED_100baseT_Full, | |
190 | }, | |
191 | { | |
192 | .speed = SPEED_100, | |
193 | .duplex = DUPLEX_HALF, | |
194 | .setting = SUPPORTED_100baseT_Half, | |
195 | }, | |
196 | { | |
197 | .speed = SPEED_10, | |
198 | .duplex = DUPLEX_FULL, | |
199 | .setting = SUPPORTED_10baseT_Full, | |
200 | }, | |
201 | { | |
202 | .speed = SPEED_10, | |
203 | .duplex = DUPLEX_HALF, | |
204 | .setting = SUPPORTED_10baseT_Half, | |
205 | }, | |
206 | }; | |
207 | ||
ff8ac609 | 208 | #define MAX_NUM_SETTINGS ARRAY_SIZE(settings) |
00db8189 | 209 | |
b3df0da8 RD |
210 | /** |
211 | * phy_find_setting - find a PHY settings array entry that matches speed & duplex | |
212 | * @speed: speed to match | |
213 | * @duplex: duplex to match | |
00db8189 | 214 | * |
b3df0da8 | 215 | * Description: Searches the settings array for the setting which |
00db8189 AF |
216 | * matches the desired speed and duplex, and returns the index |
217 | * of that setting. Returns the index of the last setting if | |
218 | * none of the others match. | |
219 | */ | |
220 | static inline int phy_find_setting(int speed, int duplex) | |
221 | { | |
222 | int idx = 0; | |
223 | ||
224 | while (idx < ARRAY_SIZE(settings) && | |
225 | (settings[idx].speed != speed || | |
226 | settings[idx].duplex != duplex)) | |
227 | idx++; | |
228 | ||
229 | return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1; | |
230 | } | |
231 | ||
b3df0da8 RD |
232 | /** |
233 | * phy_find_valid - find a PHY setting that matches the requested features mask | |
234 | * @idx: The first index in settings[] to search | |
235 | * @features: A mask of the valid settings | |
00db8189 | 236 | * |
b3df0da8 | 237 | * Description: Returns the index of the first valid setting less |
00db8189 AF |
238 | * than or equal to the one pointed to by idx, as determined by |
239 | * the mask in features. Returns the index of the last setting | |
240 | * if nothing else matches. | |
241 | */ | |
242 | static inline int phy_find_valid(int idx, u32 features) | |
243 | { | |
244 | while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features)) | |
245 | idx++; | |
246 | ||
247 | return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1; | |
248 | } | |
249 | ||
b3df0da8 RD |
250 | /** |
251 | * phy_sanitize_settings - make sure the PHY is set to supported speed and duplex | |
252 | * @phydev: the target phy_device struct | |
00db8189 | 253 | * |
b3df0da8 | 254 | * Description: Make sure the PHY is set to supported speeds and |
00db8189 | 255 | * duplexes. Drop down by one in this order: 1000/FULL, |
b3df0da8 | 256 | * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. |
00db8189 | 257 | */ |
e1393456 | 258 | void phy_sanitize_settings(struct phy_device *phydev) |
00db8189 AF |
259 | { |
260 | u32 features = phydev->supported; | |
261 | int idx; | |
262 | ||
263 | /* Sanitize settings based on PHY capabilities */ | |
264 | if ((features & SUPPORTED_Autoneg) == 0) | |
163642a2 | 265 | phydev->autoneg = AUTONEG_DISABLE; |
00db8189 AF |
266 | |
267 | idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex), | |
268 | features); | |
269 | ||
270 | phydev->speed = settings[idx].speed; | |
271 | phydev->duplex = settings[idx].duplex; | |
272 | } | |
e1393456 | 273 | EXPORT_SYMBOL(phy_sanitize_settings); |
00db8189 | 274 | |
b3df0da8 RD |
275 | /** |
276 | * phy_ethtool_sset - generic ethtool sset function, handles all the details | |
277 | * @phydev: target phy_device struct | |
278 | * @cmd: ethtool_cmd | |
00db8189 AF |
279 | * |
280 | * A few notes about parameter checking: | |
281 | * - We don't set port or transceiver, so we don't care what they | |
282 | * were set to. | |
283 | * - phy_start_aneg() will make sure forced settings are sane, and | |
284 | * choose the next best ones from the ones selected, so we don't | |
b3df0da8 | 285 | * care if ethtool tries to give us bad values. |
00db8189 AF |
286 | */ |
287 | int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd) | |
288 | { | |
289 | if (cmd->phy_address != phydev->addr) | |
290 | return -EINVAL; | |
291 | ||
292 | /* We make sure that we don't pass unsupported | |
293 | * values in to the PHY */ | |
294 | cmd->advertising &= phydev->supported; | |
295 | ||
296 | /* Verify the settings we care about. */ | |
297 | if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE) | |
298 | return -EINVAL; | |
299 | ||
300 | if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0) | |
301 | return -EINVAL; | |
302 | ||
303 | if (cmd->autoneg == AUTONEG_DISABLE | |
304 | && ((cmd->speed != SPEED_1000 | |
305 | && cmd->speed != SPEED_100 | |
306 | && cmd->speed != SPEED_10) | |
307 | || (cmd->duplex != DUPLEX_HALF | |
308 | && cmd->duplex != DUPLEX_FULL))) | |
309 | return -EINVAL; | |
310 | ||
311 | phydev->autoneg = cmd->autoneg; | |
312 | ||
313 | phydev->speed = cmd->speed; | |
314 | ||
315 | phydev->advertising = cmd->advertising; | |
316 | ||
317 | if (AUTONEG_ENABLE == cmd->autoneg) | |
318 | phydev->advertising |= ADVERTISED_Autoneg; | |
319 | else | |
320 | phydev->advertising &= ~ADVERTISED_Autoneg; | |
321 | ||
322 | phydev->duplex = cmd->duplex; | |
323 | ||
324 | /* Restart the PHY */ | |
325 | phy_start_aneg(phydev); | |
326 | ||
327 | return 0; | |
328 | } | |
9f6d55d0 | 329 | EXPORT_SYMBOL(phy_ethtool_sset); |
00db8189 AF |
330 | |
331 | int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd) | |
332 | { | |
333 | cmd->supported = phydev->supported; | |
334 | ||
335 | cmd->advertising = phydev->advertising; | |
336 | ||
337 | cmd->speed = phydev->speed; | |
338 | cmd->duplex = phydev->duplex; | |
339 | cmd->port = PORT_MII; | |
340 | cmd->phy_address = phydev->addr; | |
341 | cmd->transceiver = XCVR_EXTERNAL; | |
342 | cmd->autoneg = phydev->autoneg; | |
343 | ||
344 | return 0; | |
345 | } | |
9f6d55d0 | 346 | EXPORT_SYMBOL(phy_ethtool_gset); |
00db8189 | 347 | |
b3df0da8 RD |
348 | /** |
349 | * phy_mii_ioctl - generic PHY MII ioctl interface | |
350 | * @phydev: the phy_device struct | |
351 | * @mii_data: MII ioctl data | |
352 | * @cmd: ioctl cmd to execute | |
353 | * | |
354 | * Note that this function is currently incompatible with the | |
00db8189 | 355 | * PHYCONTROL layer. It changes registers without regard to |
b3df0da8 | 356 | * current state. Use at own risk. |
00db8189 AF |
357 | */ |
358 | int phy_mii_ioctl(struct phy_device *phydev, | |
359 | struct mii_ioctl_data *mii_data, int cmd) | |
360 | { | |
361 | u16 val = mii_data->val_in; | |
362 | ||
363 | switch (cmd) { | |
364 | case SIOCGMIIPHY: | |
365 | mii_data->phy_id = phydev->addr; | |
366 | break; | |
367 | case SIOCGMIIREG: | |
368 | mii_data->val_out = phy_read(phydev, mii_data->reg_num); | |
369 | break; | |
370 | ||
371 | case SIOCSMIIREG: | |
372 | if (!capable(CAP_NET_ADMIN)) | |
373 | return -EPERM; | |
374 | ||
375 | if (mii_data->phy_id == phydev->addr) { | |
376 | switch(mii_data->reg_num) { | |
377 | case MII_BMCR: | |
163642a2 | 378 | if ((val & (BMCR_RESET|BMCR_ANENABLE)) == 0) |
00db8189 AF |
379 | phydev->autoneg = AUTONEG_DISABLE; |
380 | else | |
381 | phydev->autoneg = AUTONEG_ENABLE; | |
382 | if ((!phydev->autoneg) && (val & BMCR_FULLDPLX)) | |
383 | phydev->duplex = DUPLEX_FULL; | |
384 | else | |
385 | phydev->duplex = DUPLEX_HALF; | |
024a0a3c SL |
386 | if ((!phydev->autoneg) && |
387 | (val & BMCR_SPEED1000)) | |
388 | phydev->speed = SPEED_1000; | |
389 | else if ((!phydev->autoneg) && | |
390 | (val & BMCR_SPEED100)) | |
391 | phydev->speed = SPEED_100; | |
00db8189 AF |
392 | break; |
393 | case MII_ADVERTISE: | |
394 | phydev->advertising = val; | |
395 | break; | |
396 | default: | |
397 | /* do nothing */ | |
398 | break; | |
399 | } | |
400 | } | |
401 | ||
402 | phy_write(phydev, mii_data->reg_num, val); | |
403 | ||
404 | if (mii_data->reg_num == MII_BMCR | |
405 | && val & BMCR_RESET | |
406 | && phydev->drv->config_init) | |
407 | phydev->drv->config_init(phydev); | |
408 | break; | |
dda93b48 DW |
409 | |
410 | default: | |
411 | return -ENOTTY; | |
00db8189 AF |
412 | } |
413 | ||
414 | return 0; | |
415 | } | |
680e9fe9 | 416 | EXPORT_SYMBOL(phy_mii_ioctl); |
00db8189 | 417 | |
b3df0da8 RD |
418 | /** |
419 | * phy_start_aneg - start auto-negotiation for this PHY device | |
420 | * @phydev: the phy_device struct | |
e1393456 | 421 | * |
b3df0da8 RD |
422 | * Description: Sanitizes the settings (if we're not autonegotiating |
423 | * them), and then calls the driver's config_aneg function. | |
424 | * If the PHYCONTROL Layer is operating, we change the state to | |
425 | * reflect the beginning of Auto-negotiation or forcing. | |
e1393456 AF |
426 | */ |
427 | int phy_start_aneg(struct phy_device *phydev) | |
428 | { | |
429 | int err; | |
430 | ||
9ff8c68b | 431 | spin_lock_bh(&phydev->lock); |
e1393456 AF |
432 | |
433 | if (AUTONEG_DISABLE == phydev->autoneg) | |
434 | phy_sanitize_settings(phydev); | |
435 | ||
436 | err = phydev->drv->config_aneg(phydev); | |
437 | ||
e1393456 AF |
438 | if (err < 0) |
439 | goto out_unlock; | |
440 | ||
441 | if (phydev->state != PHY_HALTED) { | |
442 | if (AUTONEG_ENABLE == phydev->autoneg) { | |
443 | phydev->state = PHY_AN; | |
444 | phydev->link_timeout = PHY_AN_TIMEOUT; | |
445 | } else { | |
446 | phydev->state = PHY_FORCING; | |
447 | phydev->link_timeout = PHY_FORCE_TIMEOUT; | |
448 | } | |
449 | } | |
450 | ||
451 | out_unlock: | |
9ff8c68b | 452 | spin_unlock_bh(&phydev->lock); |
e1393456 AF |
453 | return err; |
454 | } | |
455 | EXPORT_SYMBOL(phy_start_aneg); | |
456 | ||
457 | ||
c4028958 | 458 | static void phy_change(struct work_struct *work); |
e1393456 AF |
459 | static void phy_timer(unsigned long data); |
460 | ||
b3df0da8 RD |
461 | /** |
462 | * phy_start_machine - start PHY state machine tracking | |
463 | * @phydev: the phy_device struct | |
464 | * @handler: callback function for state change notifications | |
00db8189 | 465 | * |
b3df0da8 | 466 | * Description: The PHY infrastructure can run a state machine |
00db8189 AF |
467 | * which tracks whether the PHY is starting up, negotiating, |
468 | * etc. This function starts the timer which tracks the state | |
b3df0da8 RD |
469 | * of the PHY. If you want to be notified when the state changes, |
470 | * pass in the callback @handler, otherwise, pass NULL. If you | |
00db8189 | 471 | * want to maintain your own state machine, do not call this |
b3df0da8 RD |
472 | * function. |
473 | */ | |
00db8189 AF |
474 | void phy_start_machine(struct phy_device *phydev, |
475 | void (*handler)(struct net_device *)) | |
476 | { | |
477 | phydev->adjust_state = handler; | |
478 | ||
479 | init_timer(&phydev->phy_timer); | |
480 | phydev->phy_timer.function = &phy_timer; | |
481 | phydev->phy_timer.data = (unsigned long) phydev; | |
482 | mod_timer(&phydev->phy_timer, jiffies + HZ); | |
483 | } | |
484 | ||
b3df0da8 RD |
485 | /** |
486 | * phy_stop_machine - stop the PHY state machine tracking | |
487 | * @phydev: target phy_device struct | |
00db8189 | 488 | * |
b3df0da8 | 489 | * Description: Stops the state machine timer, sets the state to UP |
817acf5e | 490 | * (unless it wasn't up yet). This function must be called BEFORE |
00db8189 AF |
491 | * phy_detach. |
492 | */ | |
493 | void phy_stop_machine(struct phy_device *phydev) | |
494 | { | |
495 | del_timer_sync(&phydev->phy_timer); | |
496 | ||
9ff8c68b | 497 | spin_lock_bh(&phydev->lock); |
00db8189 AF |
498 | if (phydev->state > PHY_UP) |
499 | phydev->state = PHY_UP; | |
9ff8c68b | 500 | spin_unlock_bh(&phydev->lock); |
00db8189 | 501 | |
00db8189 AF |
502 | phydev->adjust_state = NULL; |
503 | } | |
504 | ||
b3df0da8 RD |
505 | /** |
506 | * phy_force_reduction - reduce PHY speed/duplex settings by one step | |
507 | * @phydev: target phy_device struct | |
e1393456 | 508 | * |
b3df0da8 RD |
509 | * Description: Reduces the speed/duplex settings by one notch, |
510 | * in this order-- | |
511 | * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF. | |
512 | * The function bottoms out at 10/HALF. | |
e1393456 AF |
513 | */ |
514 | static void phy_force_reduction(struct phy_device *phydev) | |
515 | { | |
516 | int idx; | |
517 | ||
518 | idx = phy_find_setting(phydev->speed, phydev->duplex); | |
519 | ||
520 | idx++; | |
521 | ||
522 | idx = phy_find_valid(idx, phydev->supported); | |
523 | ||
524 | phydev->speed = settings[idx].speed; | |
525 | phydev->duplex = settings[idx].duplex; | |
526 | ||
527 | pr_info("Trying %d/%s\n", phydev->speed, | |
528 | DUPLEX_FULL == phydev->duplex ? | |
529 | "FULL" : "HALF"); | |
530 | } | |
531 | ||
532 | ||
b3df0da8 RD |
533 | /** |
534 | * phy_error - enter HALTED state for this PHY device | |
535 | * @phydev: target phy_device struct | |
00db8189 AF |
536 | * |
537 | * Moves the PHY to the HALTED state in response to a read | |
538 | * or write error, and tells the controller the link is down. | |
539 | * Must not be called from interrupt context, or while the | |
540 | * phydev->lock is held. | |
541 | */ | |
542 | void phy_error(struct phy_device *phydev) | |
543 | { | |
9ff8c68b | 544 | spin_lock_bh(&phydev->lock); |
00db8189 | 545 | phydev->state = PHY_HALTED; |
9ff8c68b | 546 | spin_unlock_bh(&phydev->lock); |
00db8189 AF |
547 | } |
548 | ||
b3df0da8 RD |
549 | /** |
550 | * phy_interrupt - PHY interrupt handler | |
551 | * @irq: interrupt line | |
552 | * @phy_dat: phy_device pointer | |
e1393456 | 553 | * |
b3df0da8 | 554 | * Description: When a PHY interrupt occurs, the handler disables |
e1393456 AF |
555 | * interrupts, and schedules a work task to clear the interrupt. |
556 | */ | |
7d12e780 | 557 | static irqreturn_t phy_interrupt(int irq, void *phy_dat) |
e1393456 AF |
558 | { |
559 | struct phy_device *phydev = phy_dat; | |
560 | ||
3c3070d7 MR |
561 | if (PHY_HALTED == phydev->state) |
562 | return IRQ_NONE; /* It can't be ours. */ | |
563 | ||
e1393456 AF |
564 | /* The MDIO bus is not allowed to be written in interrupt |
565 | * context, so we need to disable the irq here. A work | |
566 | * queue will write the PHY to disable and clear the | |
567 | * interrupt, and then reenable the irq line. */ | |
568 | disable_irq_nosync(irq); | |
0ac49527 | 569 | atomic_inc(&phydev->irq_disable); |
e1393456 AF |
570 | |
571 | schedule_work(&phydev->phy_queue); | |
572 | ||
573 | return IRQ_HANDLED; | |
574 | } | |
575 | ||
b3df0da8 RD |
576 | /** |
577 | * phy_enable_interrupts - Enable the interrupts from the PHY side | |
578 | * @phydev: target phy_device struct | |
579 | */ | |
e1393456 | 580 | int phy_enable_interrupts(struct phy_device *phydev) |
00db8189 AF |
581 | { |
582 | int err; | |
583 | ||
e1393456 | 584 | err = phy_clear_interrupt(phydev); |
00db8189 | 585 | |
e1393456 AF |
586 | if (err < 0) |
587 | return err; | |
00db8189 | 588 | |
e1393456 | 589 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); |
00db8189 AF |
590 | |
591 | return err; | |
592 | } | |
e1393456 | 593 | EXPORT_SYMBOL(phy_enable_interrupts); |
00db8189 | 594 | |
b3df0da8 RD |
595 | /** |
596 | * phy_disable_interrupts - Disable the PHY interrupts from the PHY side | |
597 | * @phydev: target phy_device struct | |
598 | */ | |
e1393456 | 599 | int phy_disable_interrupts(struct phy_device *phydev) |
00db8189 AF |
600 | { |
601 | int err; | |
602 | ||
603 | /* Disable PHY interrupts */ | |
604 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); | |
605 | ||
606 | if (err) | |
607 | goto phy_err; | |
608 | ||
609 | /* Clear the interrupt */ | |
610 | err = phy_clear_interrupt(phydev); | |
611 | ||
612 | if (err) | |
613 | goto phy_err; | |
614 | ||
615 | return 0; | |
616 | ||
617 | phy_err: | |
618 | phy_error(phydev); | |
619 | ||
620 | return err; | |
621 | } | |
e1393456 AF |
622 | EXPORT_SYMBOL(phy_disable_interrupts); |
623 | ||
b3df0da8 RD |
624 | /** |
625 | * phy_start_interrupts - request and enable interrupts for a PHY device | |
626 | * @phydev: target phy_device struct | |
e1393456 | 627 | * |
b3df0da8 RD |
628 | * Description: Request the interrupt for the given PHY. |
629 | * If this fails, then we set irq to PHY_POLL. | |
e1393456 | 630 | * Otherwise, we enable the interrupts in the PHY. |
e1393456 | 631 | * This should only be called with a valid IRQ number. |
b3df0da8 | 632 | * Returns 0 on success or < 0 on error. |
e1393456 AF |
633 | */ |
634 | int phy_start_interrupts(struct phy_device *phydev) | |
635 | { | |
636 | int err = 0; | |
637 | ||
c4028958 | 638 | INIT_WORK(&phydev->phy_queue, phy_change); |
e1393456 | 639 | |
0ac49527 | 640 | atomic_set(&phydev->irq_disable, 0); |
e1393456 | 641 | if (request_irq(phydev->irq, phy_interrupt, |
1fb9df5d | 642 | IRQF_SHARED, |
e1393456 AF |
643 | "phy_interrupt", |
644 | phydev) < 0) { | |
645 | printk(KERN_WARNING "%s: Can't get IRQ %d (PHY)\n", | |
646 | phydev->bus->name, | |
647 | phydev->irq); | |
648 | phydev->irq = PHY_POLL; | |
649 | return 0; | |
650 | } | |
651 | ||
652 | err = phy_enable_interrupts(phydev); | |
653 | ||
654 | return err; | |
655 | } | |
656 | EXPORT_SYMBOL(phy_start_interrupts); | |
657 | ||
b3df0da8 RD |
658 | /** |
659 | * phy_stop_interrupts - disable interrupts from a PHY device | |
660 | * @phydev: target phy_device struct | |
661 | */ | |
e1393456 AF |
662 | int phy_stop_interrupts(struct phy_device *phydev) |
663 | { | |
664 | int err; | |
665 | ||
666 | err = phy_disable_interrupts(phydev); | |
667 | ||
668 | if (err) | |
669 | phy_error(phydev); | |
670 | ||
0ac49527 MR |
671 | free_irq(phydev->irq, phydev); |
672 | ||
3c3070d7 | 673 | /* |
0ac49527 MR |
674 | * Cannot call flush_scheduled_work() here as desired because |
675 | * of rtnl_lock(), but we do not really care about what would | |
676 | * be done, except from enable_irq(), so cancel any work | |
677 | * possibly pending and take care of the matter below. | |
3c3070d7 | 678 | */ |
28e53bdd | 679 | cancel_work_sync(&phydev->phy_queue); |
0ac49527 MR |
680 | /* |
681 | * If work indeed has been cancelled, disable_irq() will have | |
682 | * been left unbalanced from phy_interrupt() and enable_irq() | |
683 | * has to be called so that other devices on the line work. | |
684 | */ | |
685 | while (atomic_dec_return(&phydev->irq_disable) >= 0) | |
686 | enable_irq(phydev->irq); | |
e1393456 AF |
687 | |
688 | return err; | |
689 | } | |
690 | EXPORT_SYMBOL(phy_stop_interrupts); | |
691 | ||
692 | ||
b3df0da8 RD |
693 | /** |
694 | * phy_change - Scheduled by the phy_interrupt/timer to handle PHY changes | |
695 | * @work: work_struct that describes the work to be done | |
696 | */ | |
c4028958 | 697 | static void phy_change(struct work_struct *work) |
e1393456 AF |
698 | { |
699 | int err; | |
c4028958 DH |
700 | struct phy_device *phydev = |
701 | container_of(work, struct phy_device, phy_queue); | |
e1393456 AF |
702 | |
703 | err = phy_disable_interrupts(phydev); | |
704 | ||
705 | if (err) | |
706 | goto phy_err; | |
707 | ||
9ff8c68b | 708 | spin_lock_bh(&phydev->lock); |
e1393456 AF |
709 | if ((PHY_RUNNING == phydev->state) || (PHY_NOLINK == phydev->state)) |
710 | phydev->state = PHY_CHANGELINK; | |
9ff8c68b | 711 | spin_unlock_bh(&phydev->lock); |
e1393456 | 712 | |
0ac49527 | 713 | atomic_dec(&phydev->irq_disable); |
e1393456 AF |
714 | enable_irq(phydev->irq); |
715 | ||
716 | /* Reenable interrupts */ | |
3c3070d7 MR |
717 | if (PHY_HALTED != phydev->state) |
718 | err = phy_config_interrupt(phydev, PHY_INTERRUPT_ENABLED); | |
e1393456 AF |
719 | |
720 | if (err) | |
721 | goto irq_enable_err; | |
722 | ||
723 | return; | |
724 | ||
725 | irq_enable_err: | |
726 | disable_irq(phydev->irq); | |
0ac49527 | 727 | atomic_inc(&phydev->irq_disable); |
e1393456 AF |
728 | phy_err: |
729 | phy_error(phydev); | |
730 | } | |
731 | ||
b3df0da8 RD |
732 | /** |
733 | * phy_stop - Bring down the PHY link, and stop checking the status | |
734 | * @phydev: target phy_device struct | |
735 | */ | |
e1393456 AF |
736 | void phy_stop(struct phy_device *phydev) |
737 | { | |
9ff8c68b | 738 | spin_lock_bh(&phydev->lock); |
e1393456 AF |
739 | |
740 | if (PHY_HALTED == phydev->state) | |
741 | goto out_unlock; | |
742 | ||
3c3070d7 | 743 | if (phydev->irq != PHY_POLL) { |
e1393456 AF |
744 | /* Disable PHY Interrupts */ |
745 | phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED); | |
e1393456 | 746 | |
3c3070d7 MR |
747 | /* Clear any pending interrupts */ |
748 | phy_clear_interrupt(phydev); | |
749 | } | |
e1393456 | 750 | |
6daf6531 MR |
751 | phydev->state = PHY_HALTED; |
752 | ||
e1393456 | 753 | out_unlock: |
9ff8c68b | 754 | spin_unlock_bh(&phydev->lock); |
3c3070d7 MR |
755 | |
756 | /* | |
757 | * Cannot call flush_scheduled_work() here as desired because | |
758 | * of rtnl_lock(), but PHY_HALTED shall guarantee phy_change() | |
759 | * will not reenable interrupts. | |
760 | */ | |
e1393456 AF |
761 | } |
762 | ||
763 | ||
b3df0da8 RD |
764 | /** |
765 | * phy_start - start or restart a PHY device | |
766 | * @phydev: target phy_device struct | |
e1393456 | 767 | * |
b3df0da8 | 768 | * Description: Indicates the attached device's readiness to |
e1393456 AF |
769 | * handle PHY-related work. Used during startup to start the |
770 | * PHY, and after a call to phy_stop() to resume operation. | |
771 | * Also used to indicate the MDIO bus has cleared an error | |
772 | * condition. | |
773 | */ | |
774 | void phy_start(struct phy_device *phydev) | |
775 | { | |
026d7917 | 776 | spin_lock_bh(&phydev->lock); |
e1393456 AF |
777 | |
778 | switch (phydev->state) { | |
779 | case PHY_STARTING: | |
780 | phydev->state = PHY_PENDING; | |
781 | break; | |
782 | case PHY_READY: | |
783 | phydev->state = PHY_UP; | |
784 | break; | |
785 | case PHY_HALTED: | |
786 | phydev->state = PHY_RESUMING; | |
787 | default: | |
788 | break; | |
789 | } | |
026d7917 | 790 | spin_unlock_bh(&phydev->lock); |
e1393456 AF |
791 | } |
792 | EXPORT_SYMBOL(phy_stop); | |
793 | EXPORT_SYMBOL(phy_start); | |
67c4f3fa | 794 | |
00db8189 AF |
795 | /* PHY timer which handles the state machine */ |
796 | static void phy_timer(unsigned long data) | |
797 | { | |
798 | struct phy_device *phydev = (struct phy_device *)data; | |
799 | int needs_aneg = 0; | |
800 | int err = 0; | |
801 | ||
9ff8c68b | 802 | spin_lock_bh(&phydev->lock); |
00db8189 AF |
803 | |
804 | if (phydev->adjust_state) | |
805 | phydev->adjust_state(phydev->attached_dev); | |
806 | ||
807 | switch(phydev->state) { | |
808 | case PHY_DOWN: | |
809 | case PHY_STARTING: | |
810 | case PHY_READY: | |
811 | case PHY_PENDING: | |
812 | break; | |
813 | case PHY_UP: | |
814 | needs_aneg = 1; | |
815 | ||
816 | phydev->link_timeout = PHY_AN_TIMEOUT; | |
817 | ||
818 | break; | |
819 | case PHY_AN: | |
6b655529 AF |
820 | err = phy_read_status(phydev); |
821 | ||
822 | if (err < 0) | |
823 | break; | |
824 | ||
825 | /* If the link is down, give up on | |
826 | * negotiation for now */ | |
827 | if (!phydev->link) { | |
828 | phydev->state = PHY_NOLINK; | |
829 | netif_carrier_off(phydev->attached_dev); | |
830 | phydev->adjust_link(phydev->attached_dev); | |
831 | break; | |
832 | } | |
833 | ||
00db8189 AF |
834 | /* Check if negotiation is done. Break |
835 | * if there's an error */ | |
836 | err = phy_aneg_done(phydev); | |
837 | if (err < 0) | |
838 | break; | |
839 | ||
6b655529 | 840 | /* If AN is done, we're running */ |
00db8189 | 841 | if (err > 0) { |
6b655529 AF |
842 | phydev->state = PHY_RUNNING; |
843 | netif_carrier_on(phydev->attached_dev); | |
844 | phydev->adjust_link(phydev->attached_dev); | |
00db8189 | 845 | |
6b655529 AF |
846 | } else if (0 == phydev->link_timeout--) { |
847 | int idx; | |
00db8189 | 848 | |
6b655529 AF |
849 | needs_aneg = 1; |
850 | /* If we have the magic_aneg bit, | |
851 | * we try again */ | |
852 | if (phydev->drv->flags & PHY_HAS_MAGICANEG) | |
00db8189 AF |
853 | break; |
854 | ||
6b655529 AF |
855 | /* The timer expired, and we still |
856 | * don't have a setting, so we try | |
857 | * forcing it until we find one that | |
858 | * works, starting from the fastest speed, | |
859 | * and working our way down */ | |
860 | idx = phy_find_valid(0, phydev->supported); | |
00db8189 | 861 | |
6b655529 AF |
862 | phydev->speed = settings[idx].speed; |
863 | phydev->duplex = settings[idx].duplex; | |
00db8189 | 864 | |
6b655529 | 865 | phydev->autoneg = AUTONEG_DISABLE; |
00db8189 | 866 | |
6b655529 AF |
867 | pr_info("Trying %d/%s\n", phydev->speed, |
868 | DUPLEX_FULL == | |
869 | phydev->duplex ? | |
870 | "FULL" : "HALF"); | |
00db8189 AF |
871 | } |
872 | break; | |
873 | case PHY_NOLINK: | |
874 | err = phy_read_status(phydev); | |
875 | ||
876 | if (err) | |
877 | break; | |
878 | ||
879 | if (phydev->link) { | |
880 | phydev->state = PHY_RUNNING; | |
881 | netif_carrier_on(phydev->attached_dev); | |
882 | phydev->adjust_link(phydev->attached_dev); | |
883 | } | |
884 | break; | |
885 | case PHY_FORCING: | |
6b655529 | 886 | err = genphy_update_link(phydev); |
00db8189 AF |
887 | |
888 | if (err) | |
889 | break; | |
890 | ||
891 | if (phydev->link) { | |
892 | phydev->state = PHY_RUNNING; | |
893 | netif_carrier_on(phydev->attached_dev); | |
894 | } else { | |
895 | if (0 == phydev->link_timeout--) { | |
896 | phy_force_reduction(phydev); | |
897 | needs_aneg = 1; | |
898 | } | |
899 | } | |
900 | ||
901 | phydev->adjust_link(phydev->attached_dev); | |
902 | break; | |
903 | case PHY_RUNNING: | |
904 | /* Only register a CHANGE if we are | |
905 | * polling */ | |
906 | if (PHY_POLL == phydev->irq) | |
907 | phydev->state = PHY_CHANGELINK; | |
908 | break; | |
909 | case PHY_CHANGELINK: | |
910 | err = phy_read_status(phydev); | |
911 | ||
912 | if (err) | |
913 | break; | |
914 | ||
915 | if (phydev->link) { | |
916 | phydev->state = PHY_RUNNING; | |
917 | netif_carrier_on(phydev->attached_dev); | |
918 | } else { | |
919 | phydev->state = PHY_NOLINK; | |
920 | netif_carrier_off(phydev->attached_dev); | |
921 | } | |
922 | ||
923 | phydev->adjust_link(phydev->attached_dev); | |
924 | ||
925 | if (PHY_POLL != phydev->irq) | |
926 | err = phy_config_interrupt(phydev, | |
927 | PHY_INTERRUPT_ENABLED); | |
928 | break; | |
929 | case PHY_HALTED: | |
930 | if (phydev->link) { | |
931 | phydev->link = 0; | |
932 | netif_carrier_off(phydev->attached_dev); | |
933 | phydev->adjust_link(phydev->attached_dev); | |
934 | } | |
935 | break; | |
936 | case PHY_RESUMING: | |
937 | ||
938 | err = phy_clear_interrupt(phydev); | |
939 | ||
940 | if (err) | |
941 | break; | |
942 | ||
943 | err = phy_config_interrupt(phydev, | |
944 | PHY_INTERRUPT_ENABLED); | |
945 | ||
946 | if (err) | |
947 | break; | |
948 | ||
949 | if (AUTONEG_ENABLE == phydev->autoneg) { | |
950 | err = phy_aneg_done(phydev); | |
951 | if (err < 0) | |
952 | break; | |
953 | ||
954 | /* err > 0 if AN is done. | |
955 | * Otherwise, it's 0, and we're | |
956 | * still waiting for AN */ | |
957 | if (err > 0) { | |
958 | phydev->state = PHY_RUNNING; | |
959 | } else { | |
960 | phydev->state = PHY_AN; | |
961 | phydev->link_timeout = PHY_AN_TIMEOUT; | |
962 | } | |
963 | } else | |
964 | phydev->state = PHY_RUNNING; | |
965 | break; | |
966 | } | |
967 | ||
9ff8c68b | 968 | spin_unlock_bh(&phydev->lock); |
00db8189 AF |
969 | |
970 | if (needs_aneg) | |
971 | err = phy_start_aneg(phydev); | |
972 | ||
973 | if (err < 0) | |
974 | phy_error(phydev); | |
975 | ||
976 | mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ); | |
977 | } | |
978 |