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