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