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