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 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 42 | static struct phy_driver genphy_driver; |
| 43 | extern int mdio_bus_init(void); |
| 44 | extern void mdio_bus_exit(void); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 45 | |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 46 | void phy_device_free(struct phy_device *phydev) |
| 47 | { |
| 48 | kfree(phydev); |
| 49 | } |
| 50 | |
| 51 | static void phy_device_release(struct device *dev) |
| 52 | { |
| 53 | phy_device_free(to_phy_device(dev)); |
| 54 | } |
| 55 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 56 | struct phy_device* phy_device_create(struct mii_bus *bus, int addr, int phy_id) |
| 57 | { |
| 58 | struct phy_device *dev; |
| 59 | /* We allocate the device, and initialize the |
| 60 | * default values */ |
Robert P. J. Day | cd86128 | 2006-12-13 00:34:52 -0800 | [diff] [blame] | 61 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 62 | |
| 63 | if (NULL == dev) |
| 64 | return (struct phy_device*) PTR_ERR((void*)-ENOMEM); |
| 65 | |
Anton Vorontsov | 6f4a7f4 | 2007-12-04 16:17:33 +0300 | [diff] [blame] | 66 | dev->dev.release = phy_device_release; |
| 67 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 68 | dev->speed = 0; |
| 69 | dev->duplex = -1; |
| 70 | dev->pause = dev->asym_pause = 0; |
| 71 | dev->link = 1; |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 72 | dev->interface = PHY_INTERFACE_MODE_GMII; |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 73 | |
| 74 | dev->autoneg = AUTONEG_ENABLE; |
| 75 | |
| 76 | dev->addr = addr; |
| 77 | dev->phy_id = phy_id; |
| 78 | dev->bus = bus; |
| 79 | |
| 80 | dev->state = PHY_DOWN; |
| 81 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 82 | mutex_init(&dev->lock); |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 83 | |
| 84 | return dev; |
| 85 | } |
| 86 | EXPORT_SYMBOL(phy_device_create); |
| 87 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 88 | /** |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 89 | * get_phy_id - reads the specified addr for its ID. |
| 90 | * @bus: the target MII bus |
| 91 | * @addr: PHY address on the MII bus |
| 92 | * @phy_id: where to store the ID retrieved. |
| 93 | * |
| 94 | * Description: Reads the ID registers of the PHY at @addr on the |
| 95 | * @bus, stores it in @phy_id and returns zero on success. |
| 96 | */ |
| 97 | int get_phy_id(struct mii_bus *bus, int addr, u32 *phy_id) |
| 98 | { |
| 99 | int phy_reg; |
| 100 | |
| 101 | /* Grab the bits from PHYIR1, and put them |
| 102 | * in the upper half */ |
| 103 | phy_reg = bus->read(bus, addr, MII_PHYSID1); |
| 104 | |
| 105 | if (phy_reg < 0) |
| 106 | return -EIO; |
| 107 | |
| 108 | *phy_id = (phy_reg & 0xffff) << 16; |
| 109 | |
| 110 | /* Grab the bits from PHYIR2, and put them in the lower half */ |
| 111 | phy_reg = bus->read(bus, addr, MII_PHYSID2); |
| 112 | |
| 113 | if (phy_reg < 0) |
| 114 | return -EIO; |
| 115 | |
| 116 | *phy_id |= (phy_reg & 0xffff); |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /** |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 122 | * get_phy_device - reads the specified PHY device and returns its @phy_device struct |
| 123 | * @bus: the target MII bus |
| 124 | * @addr: PHY address on the MII bus |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 125 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 126 | * Description: Reads the ID registers of the PHY at @addr on the |
| 127 | * @bus, then allocates and returns the phy_device to represent it. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 128 | */ |
| 129 | struct phy_device * get_phy_device(struct mii_bus *bus, int addr) |
| 130 | { |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 131 | struct phy_device *dev = NULL; |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 132 | u32 phy_id; |
| 133 | int r; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 134 | |
Paul Gortmaker | cac1f3c | 2008-04-15 12:49:21 -0400 | [diff] [blame] | 135 | r = get_phy_id(bus, addr, &phy_id); |
| 136 | if (r) |
| 137 | return ERR_PTR(r); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 138 | |
| 139 | /* If the phy_id is all Fs, there is no device there */ |
| 140 | if (0xffffffff == phy_id) |
| 141 | return NULL; |
| 142 | |
Vitaly Bordug | 11b0bac | 2006-08-14 23:00:29 -0700 | [diff] [blame] | 143 | dev = phy_device_create(bus, addr, phy_id); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 144 | |
| 145 | return dev; |
| 146 | } |
| 147 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 148 | /** |
| 149 | * phy_prepare_link - prepares the PHY layer to monitor link status |
| 150 | * @phydev: target phy_device struct |
| 151 | * @handler: callback function for link status change notifications |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 152 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 153 | * Description: Tells the PHY infrastructure to handle the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 154 | * gory details on monitoring link status (whether through |
| 155 | * polling or an interrupt), and to call back to the |
| 156 | * connected device driver when the link status changes. |
| 157 | * If you want to monitor your own link state, don't call |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 158 | * this function. |
| 159 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 160 | void phy_prepare_link(struct phy_device *phydev, |
| 161 | void (*handler)(struct net_device *)) |
| 162 | { |
| 163 | phydev->adjust_link = handler; |
| 164 | } |
| 165 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 166 | /** |
| 167 | * phy_connect - connect an ethernet device to a PHY device |
| 168 | * @dev: the network device to connect |
| 169 | * @phy_id: the PHY device to connect |
| 170 | * @handler: callback function for state change notifications |
| 171 | * @flags: PHY device's dev_flags |
| 172 | * @interface: PHY device's interface |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 173 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 174 | * Description: Convenience function for connecting ethernet |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 175 | * devices to PHY devices. The default behavior is for |
| 176 | * the PHY infrastructure to handle everything, and only notify |
| 177 | * the connected driver when the link status changes. If you |
| 178 | * don't want, or can't use the provided functionality, you may |
| 179 | * choose to call only the subset of functions which provide |
| 180 | * the desired functionality. |
| 181 | */ |
| 182 | struct phy_device * phy_connect(struct net_device *dev, const char *phy_id, |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 183 | void (*handler)(struct net_device *), u32 flags, |
Randy Dunlap | 1a16893 | 2007-02-05 10:44:20 -0800 | [diff] [blame] | 184 | phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 185 | { |
| 186 | struct phy_device *phydev; |
| 187 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 188 | phydev = phy_attach(dev, phy_id, flags, interface); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 189 | |
| 190 | if (IS_ERR(phydev)) |
| 191 | return phydev; |
| 192 | |
| 193 | phy_prepare_link(phydev, handler); |
| 194 | |
| 195 | phy_start_machine(phydev, NULL); |
| 196 | |
| 197 | if (phydev->irq > 0) |
| 198 | phy_start_interrupts(phydev); |
| 199 | |
| 200 | return phydev; |
| 201 | } |
| 202 | EXPORT_SYMBOL(phy_connect); |
| 203 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 204 | /** |
| 205 | * phy_disconnect - disable interrupts, stop state machine, and detach a PHY device |
| 206 | * @phydev: target phy_device struct |
| 207 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 208 | void phy_disconnect(struct phy_device *phydev) |
| 209 | { |
| 210 | if (phydev->irq > 0) |
| 211 | phy_stop_interrupts(phydev); |
| 212 | |
| 213 | phy_stop_machine(phydev); |
| 214 | |
| 215 | phydev->adjust_link = NULL; |
| 216 | |
| 217 | phy_detach(phydev); |
| 218 | } |
| 219 | EXPORT_SYMBOL(phy_disconnect); |
| 220 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 221 | static int phy_compare_id(struct device *dev, void *data) |
| 222 | { |
| 223 | return strcmp((char *)data, dev->bus_id) ? 0 : 1; |
| 224 | } |
| 225 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 226 | /** |
| 227 | * phy_attach - attach a network device to a particular PHY device |
| 228 | * @dev: network device to attach |
| 229 | * @phy_id: PHY device to attach |
| 230 | * @flags: PHY device's dev_flags |
| 231 | * @interface: PHY device's interface |
| 232 | * |
| 233 | * Description: Called by drivers to attach to a particular PHY |
| 234 | * device. The phy_device is found, and properly hooked up |
| 235 | * to the phy_driver. If no driver is attached, then the |
| 236 | * genphy_driver is used. The phy_device is given a ptr to |
| 237 | * the attaching device, and given a callback for link status |
| 238 | * change. The phy_device is returned to the attaching driver. |
| 239 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 240 | struct phy_device *phy_attach(struct net_device *dev, |
Randy Dunlap | 1a16893 | 2007-02-05 10:44:20 -0800 | [diff] [blame] | 241 | const char *phy_id, u32 flags, phy_interface_t interface) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 242 | { |
| 243 | struct bus_type *bus = &mdio_bus_type; |
| 244 | struct phy_device *phydev; |
| 245 | struct device *d; |
| 246 | |
| 247 | /* Search the list of PHY devices on the mdio bus for the |
| 248 | * PHY with the requested name */ |
| 249 | d = bus_find_device(bus, NULL, (void *)phy_id, phy_compare_id); |
| 250 | |
| 251 | if (d) { |
| 252 | phydev = to_phy_device(d); |
| 253 | } else { |
| 254 | printk(KERN_ERR "%s not found\n", phy_id); |
| 255 | return ERR_PTR(-ENODEV); |
| 256 | } |
| 257 | |
| 258 | /* Assume that if there is no driver, that it doesn't |
| 259 | * exist, and we should use the genphy driver. */ |
| 260 | if (NULL == d->driver) { |
| 261 | int err; |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 262 | d->driver = &genphy_driver.driver; |
| 263 | |
| 264 | err = d->driver->probe(d); |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 265 | if (err >= 0) |
| 266 | err = device_bind_driver(d); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 267 | |
Jeff Garzik | b7a00ec | 2006-10-01 07:27:46 -0400 | [diff] [blame] | 268 | if (err) |
| 269 | return ERR_PTR(err); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (phydev->attached_dev) { |
| 273 | printk(KERN_ERR "%s: %s already attached\n", |
| 274 | dev->name, phy_id); |
| 275 | return ERR_PTR(-EBUSY); |
| 276 | } |
| 277 | |
| 278 | phydev->attached_dev = dev; |
| 279 | |
| 280 | phydev->dev_flags = flags; |
| 281 | |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 282 | phydev->interface = interface; |
| 283 | |
| 284 | /* Do initial configuration here, now that |
| 285 | * we have certain key parameters |
| 286 | * (dev_flags and interface) */ |
| 287 | if (phydev->drv->config_init) { |
| 288 | int err; |
| 289 | |
| 290 | err = phydev->drv->config_init(phydev); |
| 291 | |
| 292 | if (err < 0) |
| 293 | return ERR_PTR(err); |
| 294 | } |
| 295 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 296 | return phydev; |
| 297 | } |
| 298 | EXPORT_SYMBOL(phy_attach); |
| 299 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 300 | /** |
| 301 | * phy_detach - detach a PHY device from its network device |
| 302 | * @phydev: target phy_device struct |
| 303 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 304 | void phy_detach(struct phy_device *phydev) |
| 305 | { |
| 306 | phydev->attached_dev = NULL; |
| 307 | |
| 308 | /* If the device had no specific driver before (i.e. - it |
| 309 | * was using the generic driver), we unbind the device |
| 310 | * from the generic driver so that there's a chance a |
| 311 | * real driver could be loaded */ |
Greg Kroah-Hartman | 87aebe0 | 2007-04-09 11:52:31 -0400 | [diff] [blame] | 312 | if (phydev->dev.driver == &genphy_driver.driver) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 313 | device_release_driver(&phydev->dev); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 314 | } |
| 315 | EXPORT_SYMBOL(phy_detach); |
| 316 | |
| 317 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 318 | /* Generic PHY support and helper functions */ |
| 319 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 320 | /** |
| 321 | * genphy_config_advert - sanitize and advertise auto-negotation parameters |
| 322 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 323 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 324 | * Description: Writes MII_ADVERTISE with the appropriate values, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 325 | * after sanitizing the values to make sure we only advertise |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 326 | * what is supported. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 327 | */ |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 328 | int genphy_config_advert(struct phy_device *phydev) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 329 | { |
| 330 | u32 advertise; |
| 331 | int adv; |
| 332 | int err; |
| 333 | |
| 334 | /* Only allow advertising what |
| 335 | * this PHY supports */ |
| 336 | phydev->advertising &= phydev->supported; |
| 337 | advertise = phydev->advertising; |
| 338 | |
| 339 | /* Setup standard advertisement */ |
| 340 | adv = phy_read(phydev, MII_ADVERTISE); |
| 341 | |
| 342 | if (adv < 0) |
| 343 | return adv; |
| 344 | |
| 345 | adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP | |
| 346 | ADVERTISE_PAUSE_ASYM); |
| 347 | if (advertise & ADVERTISED_10baseT_Half) |
| 348 | adv |= ADVERTISE_10HALF; |
| 349 | if (advertise & ADVERTISED_10baseT_Full) |
| 350 | adv |= ADVERTISE_10FULL; |
| 351 | if (advertise & ADVERTISED_100baseT_Half) |
| 352 | adv |= ADVERTISE_100HALF; |
| 353 | if (advertise & ADVERTISED_100baseT_Full) |
| 354 | adv |= ADVERTISE_100FULL; |
| 355 | if (advertise & ADVERTISED_Pause) |
| 356 | adv |= ADVERTISE_PAUSE_CAP; |
| 357 | if (advertise & ADVERTISED_Asym_Pause) |
| 358 | adv |= ADVERTISE_PAUSE_ASYM; |
| 359 | |
| 360 | err = phy_write(phydev, MII_ADVERTISE, adv); |
| 361 | |
| 362 | if (err < 0) |
| 363 | return err; |
| 364 | |
| 365 | /* Configure gigabit if it's supported */ |
| 366 | if (phydev->supported & (SUPPORTED_1000baseT_Half | |
| 367 | SUPPORTED_1000baseT_Full)) { |
| 368 | adv = phy_read(phydev, MII_CTRL1000); |
| 369 | |
| 370 | if (adv < 0) |
| 371 | return adv; |
| 372 | |
| 373 | adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF); |
| 374 | if (advertise & SUPPORTED_1000baseT_Half) |
| 375 | adv |= ADVERTISE_1000HALF; |
| 376 | if (advertise & SUPPORTED_1000baseT_Full) |
| 377 | adv |= ADVERTISE_1000FULL; |
| 378 | err = phy_write(phydev, MII_CTRL1000, adv); |
| 379 | |
| 380 | if (err < 0) |
| 381 | return err; |
| 382 | } |
| 383 | |
| 384 | return adv; |
| 385 | } |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 386 | EXPORT_SYMBOL(genphy_config_advert); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 387 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 388 | /** |
| 389 | * genphy_setup_forced - configures/forces speed/duplex from @phydev |
| 390 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 391 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 392 | * Description: Configures MII_BMCR to force speed/duplex |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 393 | * to the values in phydev. Assumes that the values are valid. |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 394 | * Please see phy_sanitize_settings(). |
| 395 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 396 | int genphy_setup_forced(struct phy_device *phydev) |
| 397 | { |
Domen Puncer | bc1e0a0 | 2007-08-17 08:54:45 +0200 | [diff] [blame] | 398 | int ctl = 0; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 399 | |
| 400 | phydev->pause = phydev->asym_pause = 0; |
| 401 | |
| 402 | if (SPEED_1000 == phydev->speed) |
| 403 | ctl |= BMCR_SPEED1000; |
| 404 | else if (SPEED_100 == phydev->speed) |
| 405 | ctl |= BMCR_SPEED100; |
| 406 | |
| 407 | if (DUPLEX_FULL == phydev->duplex) |
| 408 | ctl |= BMCR_FULLDPLX; |
| 409 | |
| 410 | ctl = phy_write(phydev, MII_BMCR, ctl); |
| 411 | |
| 412 | if (ctl < 0) |
| 413 | return ctl; |
| 414 | |
| 415 | /* We just reset the device, so we'd better configure any |
| 416 | * settings the PHY requires to operate */ |
| 417 | if (phydev->drv->config_init) |
| 418 | ctl = phydev->drv->config_init(phydev); |
| 419 | |
| 420 | return ctl; |
| 421 | } |
| 422 | |
| 423 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 424 | /** |
| 425 | * genphy_restart_aneg - Enable and Restart Autonegotiation |
| 426 | * @phydev: target phy_device struct |
| 427 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 428 | int genphy_restart_aneg(struct phy_device *phydev) |
| 429 | { |
| 430 | int ctl; |
| 431 | |
| 432 | ctl = phy_read(phydev, MII_BMCR); |
| 433 | |
| 434 | if (ctl < 0) |
| 435 | return ctl; |
| 436 | |
| 437 | ctl |= (BMCR_ANENABLE | BMCR_ANRESTART); |
| 438 | |
| 439 | /* Don't isolate the PHY if we're negotiating */ |
| 440 | ctl &= ~(BMCR_ISOLATE); |
| 441 | |
| 442 | ctl = phy_write(phydev, MII_BMCR, ctl); |
| 443 | |
| 444 | return ctl; |
| 445 | } |
| 446 | |
| 447 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 448 | /** |
| 449 | * genphy_config_aneg - restart auto-negotiation or write BMCR |
| 450 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 451 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 452 | * Description: If auto-negotiation is enabled, we configure the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 453 | * advertising, and then restart auto-negotiation. If it is not |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 454 | * enabled, then we write the BMCR. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 455 | */ |
| 456 | int genphy_config_aneg(struct phy_device *phydev) |
| 457 | { |
| 458 | int err = 0; |
| 459 | |
| 460 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 461 | err = genphy_config_advert(phydev); |
| 462 | |
| 463 | if (err < 0) |
| 464 | return err; |
| 465 | |
| 466 | err = genphy_restart_aneg(phydev); |
| 467 | } else |
| 468 | err = genphy_setup_forced(phydev); |
| 469 | |
| 470 | return err; |
| 471 | } |
| 472 | EXPORT_SYMBOL(genphy_config_aneg); |
| 473 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 474 | /** |
| 475 | * genphy_update_link - update link status in @phydev |
| 476 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 477 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 478 | * Description: Update the value in phydev->link to reflect the |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 479 | * current link value. In order to do this, we need to read |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 480 | * the status register twice, keeping the second value. |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 481 | */ |
| 482 | int genphy_update_link(struct phy_device *phydev) |
| 483 | { |
| 484 | int status; |
| 485 | |
| 486 | /* Do a fake read */ |
| 487 | status = phy_read(phydev, MII_BMSR); |
| 488 | |
| 489 | if (status < 0) |
| 490 | return status; |
| 491 | |
| 492 | /* Read link and autonegotiation status */ |
| 493 | status = phy_read(phydev, MII_BMSR); |
| 494 | |
| 495 | if (status < 0) |
| 496 | return status; |
| 497 | |
| 498 | if ((status & BMSR_LSTATUS) == 0) |
| 499 | phydev->link = 0; |
| 500 | else |
| 501 | phydev->link = 1; |
| 502 | |
| 503 | return 0; |
| 504 | } |
Andy Fleming | 6b65552 | 2006-10-16 16:19:17 -0500 | [diff] [blame] | 505 | EXPORT_SYMBOL(genphy_update_link); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 506 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 507 | /** |
| 508 | * genphy_read_status - check the link status and update current link state |
| 509 | * @phydev: target phy_device struct |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 510 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 511 | * Description: Check the link, then figure out the current state |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 512 | * by comparing what we advertise with what the link partner |
| 513 | * advertises. Start by checking the gigabit possibilities, |
| 514 | * then move on to 10/100. |
| 515 | */ |
| 516 | int genphy_read_status(struct phy_device *phydev) |
| 517 | { |
| 518 | int adv; |
| 519 | int err; |
| 520 | int lpa; |
| 521 | int lpagb = 0; |
| 522 | |
| 523 | /* Update the link, but return if there |
| 524 | * was an error */ |
| 525 | err = genphy_update_link(phydev); |
| 526 | if (err) |
| 527 | return err; |
| 528 | |
| 529 | if (AUTONEG_ENABLE == phydev->autoneg) { |
| 530 | if (phydev->supported & (SUPPORTED_1000baseT_Half |
| 531 | | SUPPORTED_1000baseT_Full)) { |
| 532 | lpagb = phy_read(phydev, MII_STAT1000); |
| 533 | |
| 534 | if (lpagb < 0) |
| 535 | return lpagb; |
| 536 | |
| 537 | adv = phy_read(phydev, MII_CTRL1000); |
| 538 | |
| 539 | if (adv < 0) |
| 540 | return adv; |
| 541 | |
| 542 | lpagb &= adv << 2; |
| 543 | } |
| 544 | |
| 545 | lpa = phy_read(phydev, MII_LPA); |
| 546 | |
| 547 | if (lpa < 0) |
| 548 | return lpa; |
| 549 | |
| 550 | adv = phy_read(phydev, MII_ADVERTISE); |
| 551 | |
| 552 | if (adv < 0) |
| 553 | return adv; |
| 554 | |
| 555 | lpa &= adv; |
| 556 | |
| 557 | phydev->speed = SPEED_10; |
| 558 | phydev->duplex = DUPLEX_HALF; |
| 559 | phydev->pause = phydev->asym_pause = 0; |
| 560 | |
| 561 | if (lpagb & (LPA_1000FULL | LPA_1000HALF)) { |
| 562 | phydev->speed = SPEED_1000; |
| 563 | |
| 564 | if (lpagb & LPA_1000FULL) |
| 565 | phydev->duplex = DUPLEX_FULL; |
| 566 | } else if (lpa & (LPA_100FULL | LPA_100HALF)) { |
| 567 | phydev->speed = SPEED_100; |
| 568 | |
| 569 | if (lpa & LPA_100FULL) |
| 570 | phydev->duplex = DUPLEX_FULL; |
| 571 | } else |
| 572 | if (lpa & LPA_10FULL) |
| 573 | phydev->duplex = DUPLEX_FULL; |
| 574 | |
| 575 | if (phydev->duplex == DUPLEX_FULL){ |
| 576 | phydev->pause = lpa & LPA_PAUSE_CAP ? 1 : 0; |
| 577 | phydev->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0; |
| 578 | } |
| 579 | } else { |
| 580 | int bmcr = phy_read(phydev, MII_BMCR); |
| 581 | if (bmcr < 0) |
| 582 | return bmcr; |
| 583 | |
| 584 | if (bmcr & BMCR_FULLDPLX) |
| 585 | phydev->duplex = DUPLEX_FULL; |
| 586 | else |
| 587 | phydev->duplex = DUPLEX_HALF; |
| 588 | |
| 589 | if (bmcr & BMCR_SPEED1000) |
| 590 | phydev->speed = SPEED_1000; |
| 591 | else if (bmcr & BMCR_SPEED100) |
| 592 | phydev->speed = SPEED_100; |
| 593 | else |
| 594 | phydev->speed = SPEED_10; |
| 595 | |
| 596 | phydev->pause = phydev->asym_pause = 0; |
| 597 | } |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | EXPORT_SYMBOL(genphy_read_status); |
| 602 | |
| 603 | static int genphy_config_init(struct phy_device *phydev) |
| 604 | { |
Eric Sesterhenn | 84c22d7 | 2006-09-25 16:39:22 -0700 | [diff] [blame] | 605 | int val; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 606 | u32 features; |
| 607 | |
| 608 | /* For now, I'll claim that the generic driver supports |
| 609 | * all possible port types */ |
| 610 | features = (SUPPORTED_TP | SUPPORTED_MII |
| 611 | | SUPPORTED_AUI | SUPPORTED_FIBRE | |
| 612 | SUPPORTED_BNC); |
| 613 | |
| 614 | /* Do we support autonegotiation? */ |
| 615 | val = phy_read(phydev, MII_BMSR); |
| 616 | |
| 617 | if (val < 0) |
| 618 | return val; |
| 619 | |
| 620 | if (val & BMSR_ANEGCAPABLE) |
| 621 | features |= SUPPORTED_Autoneg; |
| 622 | |
| 623 | if (val & BMSR_100FULL) |
| 624 | features |= SUPPORTED_100baseT_Full; |
| 625 | if (val & BMSR_100HALF) |
| 626 | features |= SUPPORTED_100baseT_Half; |
| 627 | if (val & BMSR_10FULL) |
| 628 | features |= SUPPORTED_10baseT_Full; |
| 629 | if (val & BMSR_10HALF) |
| 630 | features |= SUPPORTED_10baseT_Half; |
| 631 | |
| 632 | if (val & BMSR_ESTATEN) { |
| 633 | val = phy_read(phydev, MII_ESTATUS); |
| 634 | |
| 635 | if (val < 0) |
| 636 | return val; |
| 637 | |
| 638 | if (val & ESTATUS_1000_TFULL) |
| 639 | features |= SUPPORTED_1000baseT_Full; |
| 640 | if (val & ESTATUS_1000_THALF) |
| 641 | features |= SUPPORTED_1000baseT_Half; |
| 642 | } |
| 643 | |
| 644 | phydev->supported = features; |
| 645 | phydev->advertising = features; |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 651 | /** |
| 652 | * phy_probe - probe and init a PHY device |
| 653 | * @dev: device to probe and init |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 654 | * |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 655 | * Description: Take care of setting up the phy_device structure, |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 656 | * set the state to READY (the driver's init function should |
| 657 | * set it to STARTING if needed). |
| 658 | */ |
| 659 | static int phy_probe(struct device *dev) |
| 660 | { |
| 661 | struct phy_device *phydev; |
| 662 | struct phy_driver *phydrv; |
| 663 | struct device_driver *drv; |
| 664 | int err = 0; |
| 665 | |
| 666 | phydev = to_phy_device(dev); |
| 667 | |
| 668 | /* Make sure the driver is held. |
| 669 | * XXX -- Is this correct? */ |
| 670 | drv = get_driver(phydev->dev.driver); |
| 671 | phydrv = to_phy_driver(drv); |
| 672 | phydev->drv = phydrv; |
| 673 | |
| 674 | /* Disable the interrupt if the PHY doesn't support it */ |
| 675 | if (!(phydrv->flags & PHY_HAS_INTERRUPT)) |
| 676 | phydev->irq = PHY_POLL; |
| 677 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 678 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 679 | |
| 680 | /* Start out supporting everything. Eventually, |
| 681 | * a controller will attach, and may modify one |
| 682 | * or both of these values */ |
| 683 | phydev->supported = phydrv->features; |
| 684 | phydev->advertising = phydrv->features; |
| 685 | |
| 686 | /* Set the state to READY by default */ |
| 687 | phydev->state = PHY_READY; |
| 688 | |
| 689 | if (phydev->drv->probe) |
| 690 | err = phydev->drv->probe(phydev); |
| 691 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 692 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 693 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 694 | return err; |
Andy Fleming | e8a2b6a | 2006-12-01 12:01:06 -0600 | [diff] [blame] | 695 | |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | static int phy_remove(struct device *dev) |
| 699 | { |
| 700 | struct phy_device *phydev; |
| 701 | |
| 702 | phydev = to_phy_device(dev); |
| 703 | |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 704 | mutex_lock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 705 | phydev->state = PHY_DOWN; |
Nate Case | 35b5f6b | 2008-01-29 10:05:09 -0600 | [diff] [blame] | 706 | mutex_unlock(&phydev->lock); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 707 | |
| 708 | if (phydev->drv->remove) |
| 709 | phydev->drv->remove(phydev); |
| 710 | |
| 711 | put_driver(dev->driver); |
| 712 | phydev->drv = NULL; |
| 713 | |
| 714 | return 0; |
| 715 | } |
| 716 | |
Randy Dunlap | b3df0da | 2007-03-06 02:41:48 -0800 | [diff] [blame] | 717 | /** |
| 718 | * phy_driver_register - register a phy_driver with the PHY layer |
| 719 | * @new_driver: new phy_driver to register |
| 720 | */ |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 721 | int phy_driver_register(struct phy_driver *new_driver) |
| 722 | { |
| 723 | int retval; |
| 724 | |
| 725 | memset(&new_driver->driver, 0, sizeof(new_driver->driver)); |
| 726 | new_driver->driver.name = new_driver->name; |
| 727 | new_driver->driver.bus = &mdio_bus_type; |
| 728 | new_driver->driver.probe = phy_probe; |
| 729 | new_driver->driver.remove = phy_remove; |
| 730 | |
| 731 | retval = driver_register(&new_driver->driver); |
| 732 | |
| 733 | if (retval) { |
| 734 | printk(KERN_ERR "%s: Error %d in registering driver\n", |
| 735 | new_driver->name, retval); |
| 736 | |
| 737 | return retval; |
| 738 | } |
| 739 | |
Olof Johansson | f2511f1 | 2007-11-04 16:09:23 -0600 | [diff] [blame] | 740 | pr_debug("%s: Registered new driver\n", new_driver->name); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | EXPORT_SYMBOL(phy_driver_register); |
| 745 | |
| 746 | void phy_driver_unregister(struct phy_driver *drv) |
| 747 | { |
| 748 | driver_unregister(&drv->driver); |
| 749 | } |
| 750 | EXPORT_SYMBOL(phy_driver_unregister); |
| 751 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 752 | static struct phy_driver genphy_driver = { |
| 753 | .phy_id = 0xffffffff, |
| 754 | .phy_id_mask = 0xffffffff, |
| 755 | .name = "Generic PHY", |
| 756 | .config_init = genphy_config_init, |
| 757 | .features = 0, |
| 758 | .config_aneg = genphy_config_aneg, |
| 759 | .read_status = genphy_read_status, |
| 760 | .driver = {.owner= THIS_MODULE, }, |
| 761 | }; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 762 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 763 | static int __init phy_init(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 764 | { |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 765 | int rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 766 | |
| 767 | rc = mdio_bus_init(); |
| 768 | if (rc) |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 769 | return rc; |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 770 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 771 | rc = phy_driver_register(&genphy_driver); |
| 772 | if (rc) |
| 773 | mdio_bus_exit(); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 774 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 775 | return rc; |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 776 | } |
| 777 | |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 778 | static void __exit phy_exit(void) |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 779 | { |
| 780 | phy_driver_unregister(&genphy_driver); |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 781 | mdio_bus_exit(); |
Andy Fleming | 00db818 | 2005-07-30 19:31:23 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Andy Fleming | e139345 | 2005-08-24 18:46:21 -0500 | [diff] [blame] | 784 | subsys_initcall(phy_init); |
Jeff Garzik | 67c4f3f | 2005-08-11 02:07:25 -0400 | [diff] [blame] | 785 | module_exit(phy_exit); |