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