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