Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/phy/phy_device.c |
| 3 | * |
| 4 | * Framework for finding and configuring PHYs. |
| 5 | * Also contains generic PHY driver |
| 6 | * |
| 7 | * Author: Andy Fleming |
| 8 | * |
| 9 | * Copyright (c) 2004 Freescale Semiconductor, Inc. |
| 10 | * |
| 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. |
| 15 | * |
| 16 | */ |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 17 | |
| 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 21 | #include <linux/string.h> |
| 22 | #include <linux/errno.h> |
| 23 | #include <linux/unistd.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/interrupt.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/delay.h> |
| 28 | #include <linux/netdevice.h> |
| 29 | #include <linux/etherdevice.h> |
| 30 | #include <linux/skbuff.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 31 | #include <linux/mm.h> |
| 32 | #include <linux/module.h> |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 33 | #include <linux/mii.h> |
| 34 | #include <linux/ethtool.h> |
| 35 | #include <linux/phy.h> |
| 36 | |
| 37 | #include <asm/io.h> |
| 38 | #include <asm/irq.h> |
| 39 | #include <asm/uaccess.h> |
| 40 | |
Olaf Hering | afcceaa | 2005-12-14 00:33:49 +0100 | [diff] [blame] | 41 | MODULE_DESCRIPTION("PHY library"); |
| 42 | MODULE_AUTHOR("Andy Fleming"); |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 45 | void phy_device_free(struct phy_device *phydev) |
| 46 | { |
| 47 | kfree(phydev); |
| 48 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 49 | EXPORT_SYMBOL(phy_device_free); |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 50 | |
| 51 | static void phy_device_release(struct device *dev) |
| 52 | { |
| 53 | phy_device_free(to_phy_device(dev)); |
| 54 | } |
| 55 | |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 56 | static struct phy_driver genphy_driver; |
| 57 | extern int mdio_bus_init(void); |
| 58 | extern void mdio_bus_exit(void); |
| 59 | |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 60 | static LIST_HEAD(phy_fixup_list); |
| 61 | static DEFINE_MUTEX(phy_fixup_lock); |
| 62 | |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 63 | static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
| 64 | u32 flags, phy_interface_t interface); |
| 65 | |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 66 | /* |
| 67 | * Creates a new phy_fixup and adds it to the list |
| 68 | * @bus_id: A string which matches phydev->dev.bus_id (or PHY_ANY_ID) |
| 69 | * @phy_uid: Used to match against phydev->phy_id (the UID of the PHY) |
| 70 | * It can also be PHY_ANY_UID |
| 71 | * @phy_uid_mask: Applied to phydev->phy_id and fixup->phy_uid before |
| 72 | * comparison |
| 73 | * @run: The actual code to be run when a matching PHY is found |
| 74 | */ |
| 75 | int phy_register_fixup(const char *bus_id, u32 phy_uid, u32 phy_uid_mask, |
| 76 | int (*run)(struct phy_device *)) |
| 77 | { |
| 78 | struct phy_fixup *fixup; |
| 79 | |
| 80 | fixup = kzalloc(sizeof(struct phy_fixup), GFP_KERNEL); |
| 81 | if (!fixup) |
| 82 | return -ENOMEM; |
| 83 | |
Kay Sievers | fb28ad35 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 84 | strlcpy(fixup->bus_id, bus_id, sizeof(fixup->bus_id)); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 85 | fixup->phy_uid = phy_uid; |
| 86 | fixup->phy_uid_mask = phy_uid_mask; |
| 87 | fixup->run = run; |
| 88 | |
| 89 | mutex_lock(&phy_fixup_lock); |
| 90 | list_add_tail(&fixup->list, &phy_fixup_list); |
| 91 | mutex_unlock(&phy_fixup_lock); |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | EXPORT_SYMBOL(phy_register_fixup); |
| 96 | |
| 97 | /* Registers a fixup to be run on any PHY with the UID in phy_uid */ |
| 98 | int phy_register_fixup_for_uid(u32 phy_uid, u32 phy_uid_mask, |
| 99 | int (*run)(struct phy_device *)) |
| 100 | { |
| 101 | return phy_register_fixup(PHY_ANY_ID, phy_uid, phy_uid_mask, run); |
| 102 | } |
| 103 | EXPORT_SYMBOL(phy_register_fixup_for_uid); |
| 104 | |
| 105 | /* Registers a fixup to be run on the PHY with id string bus_id */ |
| 106 | int phy_register_fixup_for_id(const char *bus_id, |
| 107 | int (*run)(struct phy_device *)) |
| 108 | { |
| 109 | return phy_register_fixup(bus_id, PHY_ANY_UID, 0xffffffff, run); |
| 110 | } |
| 111 | EXPORT_SYMBOL(phy_register_fixup_for_id); |
| 112 | |
| 113 | /* |
| 114 | * Returns 1 if fixup matches phydev in bus_id and phy_uid. |
| 115 | * Fixups can be set to match any in one or more fields. |
| 116 | */ |
| 117 | static int phy_needs_fixup(struct phy_device *phydev, struct phy_fixup *fixup) |
| 118 | { |
Kay Sievers | fb28ad35 | 2008-11-10 13:55:14 -0800 | [diff] [blame] | 119 | if (strcmp(fixup->bus_id, dev_name(&phydev->dev)) != 0) |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 120 | if (strcmp(fixup->bus_id, PHY_ANY_ID) != 0) |
| 121 | return 0; |
| 122 | |
| 123 | if ((fixup->phy_uid & fixup->phy_uid_mask) != |
| 124 | (phydev->phy_id & fixup->phy_uid_mask)) |
| 125 | if (fixup->phy_uid != PHY_ANY_UID) |
| 126 | return 0; |
| 127 | |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | /* Runs any matching fixups for this phydev */ |
| 132 | int phy_scan_fixups(struct phy_device *phydev) |
| 133 | { |
| 134 | struct phy_fixup *fixup; |
| 135 | |
| 136 | mutex_lock(&phy_fixup_lock); |
| 137 | list_for_each_entry(fixup, &phy_fixup_list, list) { |
| 138 | if (phy_needs_fixup(phydev, fixup)) { |
| 139 | int err; |
| 140 | |
| 141 | err = fixup->run(phydev); |
| 142 | |
Jiri Slaby | bc23283 | 2009-07-13 11:23:39 +0000 | [diff] [blame] | 143 | if (err < 0) { |
| 144 | mutex_unlock(&phy_fixup_lock); |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 145 | return err; |
Jiri Slaby | bc23283 | 2009-07-13 11:23:39 +0000 | [diff] [blame] | 146 | } |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | mutex_unlock(&phy_fixup_lock); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | EXPORT_SYMBOL(phy_scan_fixups); |
| 154 | |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 155 | static struct phy_device* phy_device_create(struct mii_bus *bus, |
| 156 | int addr, int phy_id) |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 157 | { |
| 158 | struct phy_device *dev; |
David Woodhouse | 8626d3b | 2010-04-02 01:05:27 +0000 | [diff] [blame] | 159 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 160 | /* We allocate the device, and initialize the |
| 161 | * default values */ |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 162 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 163 | |
| 164 | if (NULL == dev) |
| 165 | return (struct phy_device*) PTR_ERR((void*)-ENOMEM); |
| 166 | |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 167 | dev->dev.release = phy_device_release; |
| 168 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 169 | dev->speed = 0; |
| 170 | dev->duplex = -1; |
| 171 | dev->pause = dev->asym_pause = 0; |
| 172 | dev->link = 1; |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 173 | dev->interface = PHY_INTERFACE_MODE_GMII; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 174 | |
| 175 | dev->autoneg = AUTONEG_ENABLE; |
| 176 | |
| 177 | dev->addr = addr; |
| 178 | dev->phy_id = phy_id; |
| 179 | dev->bus = bus; |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 180 | dev->dev.parent = bus->parent; |
| 181 | dev->dev.bus = &mdio_bus_type; |
| 182 | dev->irq = bus->irq != NULL ? bus->irq[addr] : PHY_POLL; |
| 183 | dev_set_name(&dev->dev, PHY_ID_FMT, bus->id, addr); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 184 | |
| 185 | dev->state = PHY_DOWN; |
| 186 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 187 | mutex_init(&dev->lock); |
Anton Vorontsov | 4f9c85a | 2010-01-18 05:37:16 +0000 | [diff] [blame] | 188 | INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 189 | |
David Woodhouse | 8626d3b | 2010-04-02 01:05:27 +0000 | [diff] [blame] | 190 | /* Request the appropriate module unconditionally; don't |
| 191 | bother trying to do so only if it isn't already loaded, |
| 192 | because that gets complicated. A hotplug event would have |
| 193 | done an unconditional modprobe anyway. |
| 194 | We don't do normal hotplug because it won't work for MDIO |
| 195 | -- because it relies on the device staying around for long |
| 196 | enough for the driver to get loaded. With MDIO, the NIC |
| 197 | driver will get bored and give up as soon as it finds that |
| 198 | there's no driver _already_ loaded. */ |
| 199 | request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id)); |
| 200 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 201 | return dev; |
| 202 | } |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 203 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 204 | /** |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 205 | * get_phy_id - reads the specified addr for its ID. |
| 206 | * @bus: the target MII bus |
| 207 | * @addr: PHY address on the MII bus |
| 208 | * @phy_id: where to store the ID retrieved. |
| 209 | * |
| 210 | * Description: Reads the ID registers of the PHY at @addr on the |
| 211 | * @bus, stores it in @phy_id and returns zero on success. |
| 212 | */ |
David Daney | 82251de | 2012-05-15 10:46:52 +0000 | [diff] [blame] | 213 | static int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id) |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 214 | { |
| 215 | int phy_reg; |
| 216 | |
| 217 | /* Grab the bits from PHYIR1, and put them |
| 218 | * in the upper half */ |
David Daney | 6fe3264 | 2011-09-30 11:51:22 +0000 | [diff] [blame] | 219 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID1); |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 220 | |
| 221 | if (phy_reg < 0) |
| 222 | return -EIO; |
| 223 | |
| 224 | *phy_id = (phy_reg & 0xffff) << 16; |
| 225 | |
| 226 | /* Grab the bits from PHYIR2, and put them in the lower half */ |
David Daney | 6fe3264 | 2011-09-30 11:51:22 +0000 | [diff] [blame] | 227 | phy_reg = mdiobus_read(bus, addr, MII_PHYSID2); |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 228 | |
| 229 | if (phy_reg < 0) |
| 230 | return -EIO; |
| 231 | |
| 232 | *phy_id |= (phy_reg & 0xffff); |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 238 | * get_phy_device - reads the specified PHY device and returns its @phy_device struct |
| 239 | * @bus: the target MII bus |
| 240 | * @addr: PHY address on the MII bus |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 241 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 242 | * Description: Reads the ID registers of the PHY at @addr on the |
| 243 | * @bus, then allocates and returns the phy_device to represent it. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 244 | */ |
| 245 | struct phy_device * get_phy_device(struct mii_bus *bus, int addr) |
| 246 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 247 | struct phy_device *dev = NULL; |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 248 | u32 phy_id; |
| 249 | int r; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 250 | |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 251 | r = get_phy_id(bus, addr, &phy_id); |
| 252 | if (r) |
| 253 | return ERR_PTR(r); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 254 | |
Giuseppe Cavallaro | 6436cbc | 2008-11-20 20:43:18 -0800 | [diff] [blame] | 255 | /* If the phy_id is mostly Fs, there is no device there */ |
| 256 | if ((phy_id & 0x1fffffff) == 0x1fffffff) |
| 257 | return NULL; |
| 258 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 259 | dev = phy_device_create(bus, addr, phy_id); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 260 | |
| 261 | return dev; |
| 262 | } |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 263 | EXPORT_SYMBOL(get_phy_device); |
| 264 | |
| 265 | /** |
| 266 | * phy_device_register - Register the phy device on the MDIO bus |
Randy Dunlap | 1d4ac5d | 2009-06-16 16:56:33 +0000 | [diff] [blame] | 267 | * @phydev: phy_device structure to be added to the MDIO bus |
Grant Likely | 4dea547 | 2009-04-25 12:52:46 +0000 | [diff] [blame] | 268 | */ |
| 269 | int phy_device_register(struct phy_device *phydev) |
| 270 | { |
| 271 | int err; |
| 272 | |
| 273 | /* Don't register a phy if one is already registered at this |
| 274 | * address */ |
| 275 | if (phydev->bus->phy_map[phydev->addr]) |
| 276 | return -EINVAL; |
| 277 | phydev->bus->phy_map[phydev->addr] = phydev; |
| 278 | |
| 279 | /* Run all of the fixups for this PHY */ |
| 280 | phy_scan_fixups(phydev); |
| 281 | |
| 282 | err = device_register(&phydev->dev); |
| 283 | if (err) { |
| 284 | pr_err("phy %d failed to register\n", phydev->addr); |
| 285 | goto out; |
| 286 | } |
| 287 | |
| 288 | return 0; |
| 289 | |
| 290 | out: |
| 291 | phydev->bus->phy_map[phydev->addr] = NULL; |
| 292 | return err; |
| 293 | } |
| 294 | EXPORT_SYMBOL(phy_device_register); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 295 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 296 | /** |
Jiri Pirko | f8f76db | 2010-02-04 10:23:02 -0800 | [diff] [blame] | 297 | * phy_find_first - finds the first PHY device on the bus |
| 298 | * @bus: the target MII bus |
| 299 | */ |
| 300 | struct phy_device *phy_find_first(struct mii_bus *bus) |
| 301 | { |
| 302 | int addr; |
| 303 | |
| 304 | for (addr = 0; addr < PHY_MAX_ADDR; addr++) { |
| 305 | if (bus->phy_map[addr]) |
| 306 | return bus->phy_map[addr]; |
| 307 | } |
| 308 | return NULL; |
| 309 | } |
| 310 | EXPORT_SYMBOL(phy_find_first); |
| 311 | |
| 312 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 313 | * phy_prepare_link - prepares the PHY layer to monitor link status |
| 314 | * @phydev: target phy_device struct |
| 315 | * @handler: callback function for link status change notifications |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 316 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 317 | * Description: Tells the PHY infrastructure to handle the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 318 | * gory details on monitoring link status (whether through |
| 319 | * polling or an interrupt), and to call back to the |
| 320 | * connected device driver when the link status changes. |
| 321 | * If you want to monitor your own link state, don't call |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 322 | * this function. |
| 323 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 324 | static void phy_prepare_link(struct phy_device *phydev, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 325 | void (*handler)(struct net_device *)) |
| 326 | { |
| 327 | phydev->adjust_link = handler; |
| 328 | } |
| 329 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 330 | /** |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 331 | * phy_connect_direct - connect an ethernet device to a specific phy_device |
| 332 | * @dev: the network device to connect |
| 333 | * @phydev: the pointer to the phy device |
| 334 | * @handler: callback function for state change notifications |
| 335 | * @flags: PHY device's dev_flags |
| 336 | * @interface: PHY device's interface |
| 337 | */ |
| 338 | int phy_connect_direct(struct net_device *dev, struct phy_device *phydev, |
| 339 | void (*handler)(struct net_device *), u32 flags, |
| 340 | phy_interface_t interface) |
| 341 | { |
| 342 | int rc; |
| 343 | |
| 344 | rc = phy_attach_direct(dev, phydev, flags, interface); |
| 345 | if (rc) |
| 346 | return rc; |
| 347 | |
| 348 | phy_prepare_link(phydev, handler); |
| 349 | phy_start_machine(phydev, NULL); |
| 350 | if (phydev->irq > 0) |
| 351 | phy_start_interrupts(phydev); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | EXPORT_SYMBOL(phy_connect_direct); |
| 356 | |
| 357 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 358 | * phy_connect - connect an ethernet device to a PHY device |
| 359 | * @dev: the network device to connect |
Randy Dunlap | 5d12b13 | 2008-04-28 10:58:22 -0700 | [diff] [blame] | 360 | * @bus_id: the id string of the PHY device to connect |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 361 | * @handler: callback function for state change notifications |
| 362 | * @flags: PHY device's dev_flags |
| 363 | * @interface: PHY device's interface |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 364 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 365 | * Description: Convenience function for connecting ethernet |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 366 | * devices to PHY devices. The default behavior is for |
| 367 | * the PHY infrastructure to handle everything, and only notify |
| 368 | * the connected driver when the link status changes. If you |
| 369 | * don't want, or can't use the provided functionality, you may |
| 370 | * choose to call only the subset of functions which provide |
| 371 | * the desired functionality. |
| 372 | */ |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 373 | struct phy_device * phy_connect(struct net_device *dev, const char *bus_id, |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 374 | void (*handler)(struct net_device *), u32 flags, |
Randy Dunlap | 1a16893 | 2007-02-05 10:44:20 -0800 | [diff] [blame] | 375 | phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 376 | { |
| 377 | struct phy_device *phydev; |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 378 | struct device *d; |
| 379 | int rc; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 380 | |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 381 | /* Search the list of PHY devices on the mdio bus for the |
| 382 | * PHY with the requested name */ |
| 383 | d = bus_find_device_by_name(&mdio_bus_type, NULL, bus_id); |
| 384 | if (!d) { |
| 385 | pr_err("PHY %s not found\n", bus_id); |
| 386 | return ERR_PTR(-ENODEV); |
| 387 | } |
| 388 | phydev = to_phy_device(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 389 | |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 390 | rc = phy_connect_direct(dev, phydev, handler, flags, interface); |
| 391 | if (rc) |
| 392 | return ERR_PTR(rc); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 393 | |
| 394 | return phydev; |
| 395 | } |
| 396 | EXPORT_SYMBOL(phy_connect); |
| 397 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 398 | /** |
| 399 | * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device |
| 400 | * @phydev: target phy_device struct |
| 401 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 402 | void phy_disconnect(struct phy_device *phydev) |
| 403 | { |
| 404 | if (phydev->irq > 0) |
| 405 | phy_stop_interrupts(phydev); |
| 406 | |
| 407 | phy_stop_machine(phydev); |
| 408 | |
| 409 | phydev->adjust_link = NULL; |
| 410 | |
| 411 | phy_detach(phydev); |
| 412 | } |
| 413 | EXPORT_SYMBOL(phy_disconnect); |
| 414 | |
Anton Vorontsov | 2f5cb43 | 2009-12-30 08:23:30 +0000 | [diff] [blame] | 415 | int phy_init_hw(struct phy_device *phydev) |
| 416 | { |
| 417 | int ret; |
| 418 | |
| 419 | if (!phydev->drv || !phydev->drv->config_init) |
| 420 | return 0; |
| 421 | |
| 422 | ret = phy_scan_fixups(phydev); |
| 423 | if (ret < 0) |
| 424 | return ret; |
| 425 | |
| 426 | return phydev->drv->config_init(phydev); |
| 427 | } |
| 428 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 429 | /** |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 430 | * phy_attach_direct - attach a network device to a given PHY device pointer |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 431 | * @dev: network device to attach |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 432 | * @phydev: Pointer to phy_device to attach |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 433 | * @flags: PHY device's dev_flags |
| 434 | * @interface: PHY device's interface |
| 435 | * |
| 436 | * Description: Called by drivers to attach to a particular PHY |
| 437 | * device. The phy_device is found, and properly hooked up |
| 438 | * to the phy_driver. If no driver is attached, then the |
| 439 | * genphy_driver is used. The phy_device is given a ptr to |
| 440 | * the attaching device, and given a callback for link status |
| 441 | * change. The phy_device is returned to the attaching driver. |
| 442 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 443 | static int phy_attach_direct(struct net_device *dev, struct phy_device *phydev, |
| 444 | u32 flags, phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 445 | { |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 446 | struct device *d = &phydev->dev; |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 447 | int err; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 448 | |
| 449 | /* Assume that if there is no driver, that it doesn't |
| 450 | * exist, and we should use the genphy driver. */ |
| 451 | if (NULL == d->driver) { |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 452 | d->driver = &genphy_driver.driver; |
| 453 | |
| 454 | err = d->driver->probe(d); |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 455 | if (err >= 0) |
| 456 | err = device_bind_driver(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 457 | |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 458 | if (err) |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 459 | return err; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | if (phydev->attached_dev) { |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 463 | dev_err(&dev->dev, "PHY already attached\n"); |
| 464 | return -EBUSY; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | phydev->attached_dev = dev; |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 468 | dev->phydev = phydev; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 469 | |
| 470 | phydev->dev_flags = flags; |
| 471 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 472 | phydev->interface = interface; |
| 473 | |
Anton Vorontsov | ef24b16 | 2010-08-24 14:46:12 -0700 | [diff] [blame] | 474 | phydev->state = PHY_READY; |
| 475 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 476 | /* Do initial configuration here, now that |
| 477 | * we have certain key parameters |
| 478 | * (dev_flags and interface) */ |
Marc Kleine-Budde | d005a09 | 2011-03-28 14:54:08 +0000 | [diff] [blame] | 479 | err = phy_init_hw(phydev); |
| 480 | if (err) |
| 481 | phy_detach(phydev); |
| 482 | |
| 483 | return err; |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 484 | } |
Grant Likely | fa94f6d | 2009-04-25 12:52:51 +0000 | [diff] [blame] | 485 | |
| 486 | /** |
| 487 | * phy_attach - attach a network device to a particular PHY device |
| 488 | * @dev: network device to attach |
| 489 | * @bus_id: Bus ID of PHY device to attach |
| 490 | * @flags: PHY device's dev_flags |
| 491 | * @interface: PHY device's interface |
| 492 | * |
| 493 | * Description: Same as phy_attach_direct() except that a PHY bus_id |
| 494 | * string is passed instead of a pointer to a struct phy_device. |
| 495 | */ |
| 496 | struct phy_device *phy_attach(struct net_device *dev, |
| 497 | const char *bus_id, u32 flags, phy_interface_t interface) |
| 498 | { |
| 499 | struct bus_type *bus = &mdio_bus_type; |
| 500 | struct phy_device *phydev; |
| 501 | struct device *d; |
| 502 | int rc; |
| 503 | |
| 504 | /* Search the list of PHY devices on the mdio bus for the |
| 505 | * PHY with the requested name */ |
| 506 | d = bus_find_device_by_name(bus, NULL, bus_id); |
| 507 | if (!d) { |
| 508 | pr_err("PHY %s not found\n", bus_id); |
| 509 | return ERR_PTR(-ENODEV); |
| 510 | } |
| 511 | phydev = to_phy_device(d); |
| 512 | |
| 513 | rc = phy_attach_direct(dev, phydev, flags, interface); |
| 514 | if (rc) |
| 515 | return ERR_PTR(rc); |
| 516 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 517 | return phydev; |
| 518 | } |
| 519 | EXPORT_SYMBOL(phy_attach); |
| 520 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 521 | /** |
| 522 | * phy_detach - detach a PHY device from its network device |
| 523 | * @phydev: target phy_device struct |
| 524 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 525 | void phy_detach(struct phy_device *phydev) |
| 526 | { |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 527 | phydev->attached_dev->phydev = NULL; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 528 | phydev->attached_dev = NULL; |
| 529 | |
| 530 | /* If the device had no specific driver before (i.e. - it |
| 531 | * was using the generic driver), we unbind the device |
| 532 | * from the generic driver so that there's a chance a |
| 533 | * real driver could be loaded */ |
Greg Kroah-Hartman | 87aebe0 | 2007-04-09 11:52:31 -0400 | [diff] [blame] | 534 | if (phydev->dev.driver == &genphy_driver.driver) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 535 | device_release_driver(&phydev->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 536 | } |
| 537 | EXPORT_SYMBOL(phy_detach); |
| 538 | |
| 539 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 540 | /* Generic PHY support and helper functions */ |
| 541 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 542 | /** |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 543 | * genphy_config_advert - sanitize and advertise auto-negotiation parameters |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 544 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 545 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 546 | * Description: Writes MII_ADVERTISE with the appropriate values, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 547 | * after sanitizing the values to make sure we only advertise |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 548 | * what is supported. Returns < 0 on error, 0 if the PHY's advertisement |
| 549 | * hasn't changed, and > 0 if it has changed. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 550 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 551 | static int genphy_config_advert(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 552 | { |
| 553 | u32 advertise; |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 554 | int oldadv, adv; |
| 555 | int err, changed = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 556 | |
| 557 | /* Only allow advertising what |
| 558 | * this PHY supports */ |
| 559 | phydev->advertising &= phydev->supported; |
| 560 | advertise = phydev->advertising; |
| 561 | |
| 562 | /* Setup standard advertisement */ |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 563 | oldadv = adv = phy_read(phydev, MII_ADVERTISE); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 564 | |
| 565 | if (adv < 0) |
| 566 | return adv; |
| 567 | |
Matt Carlson | 28011cf | 2011-11-16 18:36:59 -0500 | [diff] [blame] | 568 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 569 | ADVERTISE_PAUSE_ASYM); |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 570 | adv |= ethtool_adv_to_mii_adv_t(advertise); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 571 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 572 | if (adv != oldadv) { |
| 573 | err = phy_write(phydev, MII_ADVERTISE, adv); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 574 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 575 | if (err < 0) |
| 576 | return err; |
| 577 | changed = 1; |
| 578 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 579 | |
| 580 | /* Configure gigabit if it's supported */ |
| 581 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
| 582 | SUPPORTED_1000baseT_Full)) { |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 583 | oldadv = adv = phy_read(phydev, MII_CTRL1000); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 584 | |
| 585 | if (adv < 0) |
| 586 | return adv; |
| 587 | |
| 588 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
Matt Carlson | 37f0702 | 2011-11-17 14:30:55 +0000 | [diff] [blame] | 589 | adv |= ethtool_adv_to_mii_ctrl1000_t(advertise); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 590 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 591 | if (adv != oldadv) { |
| 592 | err = phy_write(phydev, MII_CTRL1000, adv); |
| 593 | |
| 594 | if (err < 0) |
| 595 | return err; |
| 596 | changed = 1; |
| 597 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 598 | } |
| 599 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 600 | return changed; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 601 | } |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 602 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 603 | /** |
| 604 | * genphy_setup_forced - configures/forces speed/duplex from @phydev |
| 605 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 606 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 607 | * Description: Configures MII_BMCR to force speed/duplex |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 608 | * to the values in phydev. Assumes that the values are valid. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 609 | * Please see phy_sanitize_settings(). |
| 610 | */ |
stephen hemminger | 89ff05e | 2010-10-21 08:37:41 +0000 | [diff] [blame] | 611 | static int genphy_setup_forced(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 612 | { |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 613 | int err; |
Domen Puncer | bc1e0a0 | 2007-08-17 08:54:45 +0200 | [diff] [blame] | 614 | int ctl = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 615 | |
| 616 | phydev->pause = phydev->asym_pause = 0; |
| 617 | |
| 618 | if (SPEED_1000 == phydev->speed) |
| 619 | ctl |= BMCR_SPEED1000; |
| 620 | else if (SPEED_100 == phydev->speed) |
| 621 | ctl |= BMCR_SPEED100; |
| 622 | |
| 623 | if (DUPLEX_FULL == phydev->duplex) |
| 624 | ctl |= BMCR_FULLDPLX; |
| 625 | |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 626 | err = phy_write(phydev, MII_BMCR, ctl); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 627 | |
Andy Fleming | f62220d | 2008-04-18 17:29:54 -0500 | [diff] [blame] | 628 | return err; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 632 | /** |
| 633 | * genphy_restart_aneg - Enable and Restart Autonegotiation |
| 634 | * @phydev: target phy_device struct |
| 635 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 636 | int genphy_restart_aneg(struct phy_device *phydev) |
| 637 | { |
| 638 | int ctl; |
| 639 | |
| 640 | ctl = phy_read(phydev, MII_BMCR); |
| 641 | |
| 642 | if (ctl < 0) |
| 643 | return ctl; |
| 644 | |
| 645 | ctl |= (BMCR_ANENABLE | BMCR_ANRESTART); |
| 646 | |
| 647 | /* Don't isolate the PHY if we're negotiating */ |
| 648 | ctl &= ~(BMCR_ISOLATE); |
| 649 | |
| 650 | ctl = phy_write(phydev, MII_BMCR, ctl); |
| 651 | |
| 652 | return ctl; |
| 653 | } |
Adrian Bunk | 892871d | 2008-10-13 18:48:09 -0700 | [diff] [blame] | 654 | EXPORT_SYMBOL(genphy_restart_aneg); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 655 | |
| 656 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 657 | /** |
| 658 | * genphy_config_aneg - restart auto-negotiation or write BMCR |
| 659 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 660 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 661 | * Description: If auto-negotiation is enabled, we configure the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 662 | * advertising, and then restart auto-negotiation. If it is not |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 663 | * enabled, then we write the BMCR. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 664 | */ |
| 665 | int genphy_config_aneg(struct phy_device *phydev) |
| 666 | { |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 667 | int result; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 668 | |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 669 | if (AUTONEG_ENABLE != phydev->autoneg) |
| 670 | return genphy_setup_forced(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 671 | |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 672 | result = genphy_config_advert(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 673 | |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 674 | if (result < 0) /* error */ |
| 675 | return result; |
| 676 | |
| 677 | if (result == 0) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 678 | /* Advertisement hasn't changed, but maybe aneg was never on to |
Trent Piepho | de339c2 | 2008-11-19 15:52:41 -0800 | [diff] [blame] | 679 | * begin with? Or maybe phy was isolated? */ |
| 680 | int ctl = phy_read(phydev, MII_BMCR); |
| 681 | |
| 682 | if (ctl < 0) |
| 683 | return ctl; |
| 684 | |
| 685 | if (!(ctl & BMCR_ANENABLE) || (ctl & BMCR_ISOLATE)) |
| 686 | result = 1; /* do restart aneg */ |
| 687 | } |
| 688 | |
| 689 | /* Only restart aneg if we are advertising something different |
| 690 | * than we were before. */ |
| 691 | if (result > 0) |
| 692 | result = genphy_restart_aneg(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 693 | |
Trent Piepho | 51e2a38 | 2008-09-24 10:55:46 +0000 | [diff] [blame] | 694 | return result; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 695 | } |
| 696 | EXPORT_SYMBOL(genphy_config_aneg); |
| 697 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 698 | /** |
| 699 | * genphy_update_link - update link status in @phydev |
| 700 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 701 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 702 | * Description: Update the value in phydev->link to reflect the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 703 | * current link value. In order to do this, we need to read |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 704 | * the status register twice, keeping the second value. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 705 | */ |
| 706 | int genphy_update_link(struct phy_device *phydev) |
| 707 | { |
| 708 | int status; |
| 709 | |
| 710 | /* Do a fake read */ |
| 711 | status = phy_read(phydev, MII_BMSR); |
| 712 | |
| 713 | if (status < 0) |
| 714 | return status; |
| 715 | |
| 716 | /* Read link and autonegotiation status */ |
| 717 | status = phy_read(phydev, MII_BMSR); |
| 718 | |
| 719 | if (status < 0) |
| 720 | return status; |
| 721 | |
| 722 | if ((status & BMSR_LSTATUS) == 0) |
| 723 | phydev->link = 0; |
| 724 | else |
| 725 | phydev->link = 1; |
| 726 | |
| 727 | return 0; |
| 728 | } |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 729 | EXPORT_SYMBOL(genphy_update_link); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 730 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 731 | /** |
| 732 | * genphy_read_status - check the link status and update current link state |
| 733 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 734 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 735 | * Description: Check the link, then figure out the current state |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 736 | * by comparing what we advertise with what the link partner |
| 737 | * advertises. Start by checking the gigabit possibilities, |
| 738 | * then move on to 10/100. |
| 739 | */ |
| 740 | int genphy_read_status(struct phy_device *phydev) |
| 741 | { |
| 742 | int adv; |
| 743 | int err; |
| 744 | int lpa; |
| 745 | int lpagb = 0; |
| 746 | |
| 747 | /* Update the link, but return if there |
| 748 | * was an error */ |
| 749 | err = genphy_update_link(phydev); |
| 750 | if (err) |
| 751 | return err; |
| 752 | |
| 753 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 754 | if (phydev->supported & (SUPPORTED_1000baseT_Half |
| 755 | | SUPPORTED_1000baseT_Full)) { |
| 756 | lpagb = phy_read(phydev, MII_STAT1000); |
| 757 | |
| 758 | if (lpagb < 0) |
| 759 | return lpagb; |
| 760 | |
| 761 | adv = phy_read(phydev, MII_CTRL1000); |
| 762 | |
| 763 | if (adv < 0) |
| 764 | return adv; |
| 765 | |
| 766 | lpagb &= adv << 2; |
| 767 | } |
| 768 | |
| 769 | lpa = phy_read(phydev, MII_LPA); |
| 770 | |
| 771 | if (lpa < 0) |
| 772 | return lpa; |
| 773 | |
| 774 | adv = phy_read(phydev, MII_ADVERTISE); |
| 775 | |
| 776 | if (adv < 0) |
| 777 | return adv; |
| 778 | |
| 779 | lpa &= adv; |
| 780 | |
| 781 | phydev->speed = SPEED_10; |
| 782 | phydev->duplex = DUPLEX_HALF; |
| 783 | phydev->pause = phydev->asym_pause = 0; |
| 784 | |
| 785 | if (lpagb & (LPA_1000FULL | LPA_1000HALF)) { |
| 786 | phydev->speed = SPEED_1000; |
| 787 | |
| 788 | if (lpagb & LPA_1000FULL) |
| 789 | phydev->duplex = DUPLEX_FULL; |
| 790 | } else if (lpa & (LPA_100FULL | LPA_100HALF)) { |
| 791 | phydev->speed = SPEED_100; |
| 792 | |
| 793 | if (lpa & LPA_100FULL) |
| 794 | phydev->duplex = DUPLEX_FULL; |
| 795 | } else |
| 796 | if (lpa & LPA_10FULL) |
| 797 | phydev->duplex = DUPLEX_FULL; |
| 798 | |
| 799 | if (phydev->duplex == DUPLEX_FULL){ |
| 800 | phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0; |
| 801 | phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0; |
| 802 | } |
| 803 | } else { |
| 804 | int bmcr = phy_read(phydev, MII_BMCR); |
| 805 | if (bmcr < 0) |
| 806 | return bmcr; |
| 807 | |
| 808 | if (bmcr & BMCR_FULLDPLX) |
| 809 | phydev->duplex = DUPLEX_FULL; |
| 810 | else |
| 811 | phydev->duplex = DUPLEX_HALF; |
| 812 | |
| 813 | if (bmcr & BMCR_SPEED1000) |
| 814 | phydev->speed = SPEED_1000; |
| 815 | else if (bmcr & BMCR_SPEED100) |
| 816 | phydev->speed = SPEED_100; |
| 817 | else |
| 818 | phydev->speed = SPEED_10; |
| 819 | |
| 820 | phydev->pause = phydev->asym_pause = 0; |
| 821 | } |
| 822 | |
| 823 | return 0; |
| 824 | } |
| 825 | EXPORT_SYMBOL(genphy_read_status); |
| 826 | |
| 827 | static int genphy_config_init(struct phy_device *phydev) |
| 828 | { |
Eric Sesterhenn | 84c22d7 | 2006-09-25 16:39:22 -0700 | [diff] [blame] | 829 | int val; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 830 | u32 features; |
| 831 | |
| 832 | /* For now, I'll claim that the generic driver supports |
| 833 | * all possible port types */ |
| 834 | features = (SUPPORTED_TP | SUPPORTED_MII |
| 835 | | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 836 | SUPPORTED_BNC); |
| 837 | |
| 838 | /* Do we support autonegotiation? */ |
| 839 | val = phy_read(phydev, MII_BMSR); |
| 840 | |
| 841 | if (val < 0) |
| 842 | return val; |
| 843 | |
| 844 | if (val & BMSR_ANEGCAPABLE) |
| 845 | features |= SUPPORTED_Autoneg; |
| 846 | |
| 847 | if (val & BMSR_100FULL) |
| 848 | features |= SUPPORTED_100baseT_Full; |
| 849 | if (val & BMSR_100HALF) |
| 850 | features |= SUPPORTED_100baseT_Half; |
| 851 | if (val & BMSR_10FULL) |
| 852 | features |= SUPPORTED_10baseT_Full; |
| 853 | if (val & BMSR_10HALF) |
| 854 | features |= SUPPORTED_10baseT_Half; |
| 855 | |
| 856 | if (val & BMSR_ESTATEN) { |
| 857 | val = phy_read(phydev, MII_ESTATUS); |
| 858 | |
| 859 | if (val < 0) |
| 860 | return val; |
| 861 | |
| 862 | if (val & ESTATUS_1000_TFULL) |
| 863 | features |= SUPPORTED_1000baseT_Full; |
| 864 | if (val & ESTATUS_1000_THALF) |
| 865 | features |= SUPPORTED_1000baseT_Half; |
| 866 | } |
| 867 | |
| 868 | phydev->supported = features; |
| 869 | phydev->advertising = features; |
| 870 | |
| 871 | return 0; |
| 872 | } |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 873 | int genphy_suspend(struct phy_device *phydev) |
| 874 | { |
| 875 | int value; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 876 | |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 877 | mutex_lock(&phydev->lock); |
| 878 | |
| 879 | value = phy_read(phydev, MII_BMCR); |
| 880 | phy_write(phydev, MII_BMCR, (value | BMCR_PDOWN)); |
| 881 | |
| 882 | mutex_unlock(&phydev->lock); |
| 883 | |
| 884 | return 0; |
| 885 | } |
| 886 | EXPORT_SYMBOL(genphy_suspend); |
| 887 | |
| 888 | int genphy_resume(struct phy_device *phydev) |
| 889 | { |
| 890 | int value; |
| 891 | |
| 892 | mutex_lock(&phydev->lock); |
| 893 | |
| 894 | value = phy_read(phydev, MII_BMCR); |
| 895 | phy_write(phydev, MII_BMCR, (value & ~BMCR_PDOWN)); |
| 896 | |
| 897 | mutex_unlock(&phydev->lock); |
| 898 | |
| 899 | return 0; |
| 900 | } |
| 901 | EXPORT_SYMBOL(genphy_resume); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 902 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 903 | /** |
| 904 | * phy_probe - probe and init a PHY device |
| 905 | * @dev: device to probe and init |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 906 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 907 | * Description: Take care of setting up the phy_device structure, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 908 | * set the state to READY (the driver's init function should |
| 909 | * set it to STARTING if needed). |
| 910 | */ |
| 911 | static int phy_probe(struct device *dev) |
| 912 | { |
| 913 | struct phy_device *phydev; |
| 914 | struct phy_driver *phydrv; |
| 915 | struct device_driver *drv; |
| 916 | int err = 0; |
| 917 | |
| 918 | phydev = to_phy_device(dev); |
| 919 | |
Alan Stern | f3ff924 | 2012-01-24 13:35:24 -0500 | [diff] [blame] | 920 | drv = phydev->dev.driver; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 921 | phydrv = to_phy_driver(drv); |
| 922 | phydev->drv = phydrv; |
| 923 | |
| 924 | /* Disable the interrupt if the PHY doesn't support it */ |
| 925 | if (!(phydrv->flags & PHY_HAS_INTERRUPT)) |
| 926 | phydev->irq = PHY_POLL; |
| 927 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 928 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 929 | |
| 930 | /* Start out supporting everything. Eventually, |
| 931 | * a controller will attach, and may modify one |
| 932 | * or both of these values */ |
| 933 | phydev->supported = phydrv->features; |
| 934 | phydev->advertising = phydrv->features; |
| 935 | |
| 936 | /* Set the state to READY by default */ |
| 937 | phydev->state = PHY_READY; |
| 938 | |
| 939 | if (phydev->drv->probe) |
| 940 | err = phydev->drv->probe(phydev); |
| 941 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 942 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 943 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 944 | return err; |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 945 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | static int phy_remove(struct device *dev) |
| 949 | { |
| 950 | struct phy_device *phydev; |
| 951 | |
| 952 | phydev = to_phy_device(dev); |
| 953 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 954 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 955 | phydev->state = PHY_DOWN; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 956 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 957 | |
| 958 | if (phydev->drv->remove) |
| 959 | phydev->drv->remove(phydev); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 960 | phydev->drv = NULL; |
| 961 | |
| 962 | return 0; |
| 963 | } |
| 964 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 965 | /** |
| 966 | * phy_driver_register - register a phy_driver with the PHY layer |
| 967 | * @new_driver: new phy_driver to register |
| 968 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 969 | int phy_driver_register(struct phy_driver *new_driver) |
| 970 | { |
| 971 | int retval; |
| 972 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 973 | new_driver->driver.name = new_driver->name; |
| 974 | new_driver->driver.bus = &mdio_bus_type; |
| 975 | new_driver->driver.probe = phy_probe; |
| 976 | new_driver->driver.remove = phy_remove; |
| 977 | |
| 978 | retval = driver_register(&new_driver->driver); |
| 979 | |
| 980 | if (retval) { |
Joe Perches | 8d24248 | 2012-06-09 07:49:07 +0000 | [diff] [blame] | 981 | pr_err("%s: Error %d in registering driver\n", |
| 982 | new_driver->name, retval); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 983 | |
| 984 | return retval; |
| 985 | } |
| 986 | |
Olof Johansson | f2511f1 | 2007-11-04 16:09:23 -0600 | [diff] [blame] | 987 | pr_debug("%s: Registered new driver\n", new_driver->name); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 988 | |
| 989 | return 0; |
| 990 | } |
| 991 | EXPORT_SYMBOL(phy_driver_register); |
| 992 | |
| 993 | void phy_driver_unregister(struct phy_driver *drv) |
| 994 | { |
| 995 | driver_unregister(&drv->driver); |
| 996 | } |
| 997 | EXPORT_SYMBOL(phy_driver_unregister); |
| 998 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 999 | static struct phy_driver genphy_driver = { |
| 1000 | .phy_id = 0xffffffff, |
| 1001 | .phy_id_mask = 0xffffffff, |
| 1002 | .name = "Generic PHY", |
| 1003 | .config_init = genphy_config_init, |
| 1004 | .features = 0, |
| 1005 | .config_aneg = genphy_config_aneg, |
| 1006 | .read_status = genphy_read_status, |
Giuseppe Cavallaro | 0f0ca34 | 2008-11-28 16:24:56 -0800 | [diff] [blame] | 1007 | .suspend = genphy_suspend, |
| 1008 | .resume = genphy_resume, |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1009 | .driver = {.owner= THIS_MODULE, }, |
| 1010 | }; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1011 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1012 | static int __init phy_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1013 | { |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1014 | int rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1015 | |
| 1016 | rc = mdio_bus_init(); |
| 1017 | if (rc) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1018 | return rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1019 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1020 | rc = phy_driver_register(&genphy_driver); |
| 1021 | if (rc) |
| 1022 | mdio_bus_exit(); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1023 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1024 | return rc; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1025 | } |
| 1026 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1027 | static void __exit phy_exit(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1028 | { |
| 1029 | phy_driver_unregister(&genphy_driver); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1030 | mdio_bus_exit(); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 1031 | } |
| 1032 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 1033 | subsys_initcall(phy_init); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 1034 | module_exit(phy_exit); |