2 * drivers/net/phy/phy.c
4 * Framework for configuring and reading PHY devices
5 * Based on code in sungem_phy.c and gianfar_phy.c
9 * Copyright (c) 2004 Freescale Semiconductor, Inc.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/config.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/string.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/slab.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/spinlock.h>
32 #include <linux/module.h>
33 #include <linux/version.h>
34 #include <linux/mii.h>
35 #include <linux/ethtool.h>
36 #include <linux/phy.h>
40 #include <asm/uaccess.h>
42 static void phy_timer(unsigned long data);
43 static int phy_disable_interrupts(struct phy_device *phydev);
44 static void phy_sanitize_settings(struct phy_device *phydev);
45 static int phy_stop_interrupts(struct phy_device *phydev);
48 /* Convenience functions for reading/writing a given PHY
49 * register. They MUST NOT be called from interrupt context,
50 * because the bus read/write functions may wait for an interrupt
51 * to conclude the operation. */
52 int phy_read(struct phy_device *phydev, u16 regnum)
55 struct mii_bus *bus = phydev->bus;
57 spin_lock_bh(&bus->mdio_lock);
58 retval = bus->read(bus, phydev->addr, regnum);
59 spin_unlock_bh(&bus->mdio_lock);
63 EXPORT_SYMBOL(phy_read);
65 int phy_write(struct phy_device *phydev, u16 regnum, u16 val)
68 struct mii_bus *bus = phydev->bus;
70 spin_lock_bh(&bus->mdio_lock);
71 err = bus->write(bus, phydev->addr, regnum, val);
72 spin_unlock_bh(&bus->mdio_lock);
76 EXPORT_SYMBOL(phy_write);
79 int phy_clear_interrupt(struct phy_device *phydev)
83 if (phydev->drv->ack_interrupt)
84 err = phydev->drv->ack_interrupt(phydev);
90 int phy_config_interrupt(struct phy_device *phydev, u32 interrupts)
94 phydev->interrupts = interrupts;
95 if (phydev->drv->config_intr)
96 err = phydev->drv->config_intr(phydev);
104 * description: Reads the status register and returns 0 either if
105 * auto-negotiation is incomplete, or if there was an error.
106 * Returns BMSR_ANEGCOMPLETE if auto-negotiation is done.
108 static inline int phy_aneg_done(struct phy_device *phydev)
112 retval = phy_read(phydev, MII_BMSR);
114 return (retval < 0) ? retval : (retval & BMSR_ANEGCOMPLETE);
119 * description: Calls the PHY driver's config_aneg, and then
120 * sets the PHY state to PHY_AN if auto-negotiation is enabled,
121 * and to PHY_FORCING if auto-negotiation is disabled. Unless
122 * the PHY is currently HALTED.
124 static int phy_start_aneg(struct phy_device *phydev)
128 spin_lock(&phydev->lock);
130 if (AUTONEG_DISABLE == phydev->autoneg)
131 phy_sanitize_settings(phydev);
133 err = phydev->drv->config_aneg(phydev);
138 if (phydev->state != PHY_HALTED) {
139 if (AUTONEG_ENABLE == phydev->autoneg) {
140 phydev->state = PHY_AN;
141 phydev->link_timeout = PHY_AN_TIMEOUT;
143 phydev->state = PHY_FORCING;
144 phydev->link_timeout = PHY_FORCE_TIMEOUT;
149 spin_unlock(&phydev->lock);
153 /* A structure for mapping a particular speed and duplex
154 * combination to a particular SUPPORTED and ADVERTISED value */
161 /* A mapping of all SUPPORTED settings to speed/duplex */
162 static struct phy_setting settings[] = {
165 .duplex = DUPLEX_FULL,
166 .setting = SUPPORTED_10000baseT_Full,
170 .duplex = DUPLEX_FULL,
171 .setting = SUPPORTED_1000baseT_Full,
175 .duplex = DUPLEX_HALF,
176 .setting = SUPPORTED_1000baseT_Half,
180 .duplex = DUPLEX_FULL,
181 .setting = SUPPORTED_100baseT_Full,
185 .duplex = DUPLEX_HALF,
186 .setting = SUPPORTED_100baseT_Half,
190 .duplex = DUPLEX_FULL,
191 .setting = SUPPORTED_10baseT_Full,
195 .duplex = DUPLEX_HALF,
196 .setting = SUPPORTED_10baseT_Half,
200 #define MAX_NUM_SETTINGS (sizeof(settings)/sizeof(struct phy_setting))
204 * description: Searches the settings array for the setting which
205 * matches the desired speed and duplex, and returns the index
206 * of that setting. Returns the index of the last setting if
207 * none of the others match.
209 static inline int phy_find_setting(int speed, int duplex)
213 while (idx < ARRAY_SIZE(settings) &&
214 (settings[idx].speed != speed ||
215 settings[idx].duplex != duplex))
218 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
222 * idx: The first index in settings[] to search
223 * features: A mask of the valid settings
225 * description: Returns the index of the first valid setting less
226 * than or equal to the one pointed to by idx, as determined by
227 * the mask in features. Returns the index of the last setting
228 * if nothing else matches.
230 static inline int phy_find_valid(int idx, u32 features)
232 while (idx < MAX_NUM_SETTINGS && !(settings[idx].setting & features))
235 return idx < MAX_NUM_SETTINGS ? idx : MAX_NUM_SETTINGS - 1;
238 /* phy_sanitize_settings
240 * description: Make sure the PHY is set to supported speeds and
241 * duplexes. Drop down by one in this order: 1000/FULL,
242 * 1000/HALF, 100/FULL, 100/HALF, 10/FULL, 10/HALF
244 static void phy_sanitize_settings(struct phy_device *phydev)
246 u32 features = phydev->supported;
249 /* Sanitize settings based on PHY capabilities */
250 if ((features & SUPPORTED_Autoneg) == 0)
253 idx = phy_find_valid(phy_find_setting(phydev->speed, phydev->duplex),
256 phydev->speed = settings[idx].speed;
257 phydev->duplex = settings[idx].duplex;
260 /* phy_force_reduction
262 * description: Reduces the speed/duplex settings by
263 * one notch. The order is so:
264 * 1000/FULL, 1000/HALF, 100/FULL, 100/HALF,
265 * 10/FULL, 10/HALF. The function bottoms out at 10/HALF.
267 static void phy_force_reduction(struct phy_device *phydev)
271 idx = phy_find_setting(phydev->speed, phydev->duplex);
275 idx = phy_find_valid(idx, phydev->supported);
277 phydev->speed = settings[idx].speed;
278 phydev->duplex = settings[idx].duplex;
280 pr_info("Trying %d/%s\n", phydev->speed,
281 DUPLEX_FULL == phydev->duplex ?
286 * A generic ethtool sset function. Handles all the details
288 * A few notes about parameter checking:
289 * - We don't set port or transceiver, so we don't care what they
291 * - phy_start_aneg() will make sure forced settings are sane, and
292 * choose the next best ones from the ones selected, so we don't
293 * care if ethtool tries to give us bad values
295 int phy_ethtool_sset(struct phy_device *phydev, struct ethtool_cmd *cmd)
297 if (cmd->phy_address != phydev->addr)
300 /* We make sure that we don't pass unsupported
301 * values in to the PHY */
302 cmd->advertising &= phydev->supported;
304 /* Verify the settings we care about. */
305 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
308 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
311 if (cmd->autoneg == AUTONEG_DISABLE
312 && ((cmd->speed != SPEED_1000
313 && cmd->speed != SPEED_100
314 && cmd->speed != SPEED_10)
315 || (cmd->duplex != DUPLEX_HALF
316 && cmd->duplex != DUPLEX_FULL)))
319 phydev->autoneg = cmd->autoneg;
321 phydev->speed = cmd->speed;
323 phydev->advertising = cmd->advertising;
325 if (AUTONEG_ENABLE == cmd->autoneg)
326 phydev->advertising |= ADVERTISED_Autoneg;
328 phydev->advertising &= ~ADVERTISED_Autoneg;
330 phydev->duplex = cmd->duplex;
332 /* Restart the PHY */
333 phy_start_aneg(phydev);
338 int phy_ethtool_gset(struct phy_device *phydev, struct ethtool_cmd *cmd)
340 cmd->supported = phydev->supported;
342 cmd->advertising = phydev->advertising;
344 cmd->speed = phydev->speed;
345 cmd->duplex = phydev->duplex;
346 cmd->port = PORT_MII;
347 cmd->phy_address = phydev->addr;
348 cmd->transceiver = XCVR_EXTERNAL;
349 cmd->autoneg = phydev->autoneg;
355 /* Note that this function is currently incompatible with the
356 * PHYCONTROL layer. It changes registers without regard to
357 * current state. Use at own risk
359 int phy_mii_ioctl(struct phy_device *phydev,
360 struct mii_ioctl_data *mii_data, int cmd)
362 u16 val = mii_data->val_in;
366 mii_data->phy_id = phydev->addr;
369 mii_data->val_out = phy_read(phydev, mii_data->reg_num);
373 if (!capable(CAP_NET_ADMIN))
376 if (mii_data->phy_id == phydev->addr) {
377 switch(mii_data->reg_num) {
379 if (val & (BMCR_RESET|BMCR_ANENABLE))
380 phydev->autoneg = AUTONEG_DISABLE;
382 phydev->autoneg = AUTONEG_ENABLE;
383 if ((!phydev->autoneg) && (val & BMCR_FULLDPLX))
384 phydev->duplex = DUPLEX_FULL;
386 phydev->duplex = DUPLEX_HALF;
389 phydev->advertising = val;
397 phy_write(phydev, mii_data->reg_num, val);
399 if (mii_data->reg_num == MII_BMCR
401 && phydev->drv->config_init)
402 phydev->drv->config_init(phydev);
409 /* phy_start_machine:
411 * description: The PHY infrastructure can run a state machine
412 * which tracks whether the PHY is starting up, negotiating,
413 * etc. This function starts the timer which tracks the state
414 * of the PHY. If you want to be notified when the state
415 * changes, pass in the callback, otherwise, pass NULL. If you
416 * want to maintain your own state machine, do not call this
418 void phy_start_machine(struct phy_device *phydev,
419 void (*handler)(struct net_device *))
421 phydev->adjust_state = handler;
423 init_timer(&phydev->phy_timer);
424 phydev->phy_timer.function = &phy_timer;
425 phydev->phy_timer.data = (unsigned long) phydev;
426 mod_timer(&phydev->phy_timer, jiffies + HZ);
431 * description: Stops the state machine timer, sets the state to
432 * UP (unless it wasn't up yet), and then frees the interrupt,
433 * if it is in use. This function must be called BEFORE
436 void phy_stop_machine(struct phy_device *phydev)
438 del_timer_sync(&phydev->phy_timer);
440 spin_lock(&phydev->lock);
441 if (phydev->state > PHY_UP)
442 phydev->state = PHY_UP;
443 spin_unlock(&phydev->lock);
445 if (phydev->irq != PHY_POLL)
446 phy_stop_interrupts(phydev);
448 phydev->adjust_state = NULL;
453 * Moves the PHY to the HALTED state in response to a read
454 * or write error, and tells the controller the link is down.
455 * Must not be called from interrupt context, or while the
456 * phydev->lock is held.
458 void phy_error(struct phy_device *phydev)
460 spin_lock(&phydev->lock);
461 phydev->state = PHY_HALTED;
462 spin_unlock(&phydev->lock);
465 static int phy_stop_interrupts(struct phy_device *phydev)
469 err = phy_disable_interrupts(phydev);
474 free_irq(phydev->irq, phydev);
479 /* Disable the PHY interrupts from the PHY side */
480 static int phy_disable_interrupts(struct phy_device *phydev)
484 /* Disable PHY interrupts */
485 err = phy_config_interrupt(phydev, PHY_INTERRUPT_DISABLED);
490 /* Clear the interrupt */
491 err = phy_clear_interrupt(phydev);
504 /* PHY timer which handles the state machine */
505 static void phy_timer(unsigned long data)
507 struct phy_device *phydev = (struct phy_device *)data;
511 spin_lock(&phydev->lock);
513 if (phydev->adjust_state)
514 phydev->adjust_state(phydev->attached_dev);
516 switch(phydev->state) {
525 phydev->link_timeout = PHY_AN_TIMEOUT;
529 /* Check if negotiation is done. Break
530 * if there's an error */
531 err = phy_aneg_done(phydev);
535 /* If auto-negotiation is done, we change to
536 * either RUNNING, or NOLINK */
538 err = phy_read_status(phydev);
544 phydev->state = PHY_RUNNING;
545 netif_carrier_on(phydev->attached_dev);
547 phydev->state = PHY_NOLINK;
548 netif_carrier_off(phydev->attached_dev);
551 phydev->adjust_link(phydev->attached_dev);
553 } else if (0 == phydev->link_timeout--) {
554 /* The counter expired, so either we
555 * switch to forced mode, or the
556 * magic_aneg bit exists, and we try aneg
558 if (!(phydev->drv->flags & PHY_HAS_MAGICANEG)) {
561 /* We'll start from the
562 * fastest speed, and work
564 idx = phy_find_valid(0,
567 phydev->speed = settings[idx].speed;
568 phydev->duplex = settings[idx].duplex;
570 phydev->autoneg = AUTONEG_DISABLE;
571 phydev->state = PHY_FORCING;
572 phydev->link_timeout =
575 pr_info("Trying %d/%s\n",
586 err = phy_read_status(phydev);
592 phydev->state = PHY_RUNNING;
593 netif_carrier_on(phydev->attached_dev);
594 phydev->adjust_link(phydev->attached_dev);
598 err = phy_read_status(phydev);
604 phydev->state = PHY_RUNNING;
605 netif_carrier_on(phydev->attached_dev);
607 if (0 == phydev->link_timeout--) {
608 phy_force_reduction(phydev);
613 phydev->adjust_link(phydev->attached_dev);
616 /* Only register a CHANGE if we are
618 if (PHY_POLL == phydev->irq)
619 phydev->state = PHY_CHANGELINK;
622 err = phy_read_status(phydev);
628 phydev->state = PHY_RUNNING;
629 netif_carrier_on(phydev->attached_dev);
631 phydev->state = PHY_NOLINK;
632 netif_carrier_off(phydev->attached_dev);
635 phydev->adjust_link(phydev->attached_dev);
637 if (PHY_POLL != phydev->irq)
638 err = phy_config_interrupt(phydev,
639 PHY_INTERRUPT_ENABLED);
644 netif_carrier_off(phydev->attached_dev);
645 phydev->adjust_link(phydev->attached_dev);
650 err = phy_clear_interrupt(phydev);
655 err = phy_config_interrupt(phydev,
656 PHY_INTERRUPT_ENABLED);
661 if (AUTONEG_ENABLE == phydev->autoneg) {
662 err = phy_aneg_done(phydev);
666 /* err > 0 if AN is done.
667 * Otherwise, it's 0, and we're
668 * still waiting for AN */
670 phydev->state = PHY_RUNNING;
672 phydev->state = PHY_AN;
673 phydev->link_timeout = PHY_AN_TIMEOUT;
676 phydev->state = PHY_RUNNING;
680 spin_unlock(&phydev->lock);
683 err = phy_start_aneg(phydev);
688 mod_timer(&phydev->phy_timer, jiffies + PHY_STATE_TIME * HZ);