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