wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * This provides a bit-banged interface to the ethernet MII management |
| 26 | * channel. |
| 27 | */ |
| 28 | |
| 29 | #include <common.h> |
| 30 | #include <miiphy.h> |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 31 | #include <phy.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 32 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 33 | #include <asm/types.h> |
| 34 | #include <linux/list.h> |
| 35 | #include <malloc.h> |
| 36 | #include <net.h> |
| 37 | |
| 38 | /* local debug macro */ |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 39 | #undef MII_DEBUG |
| 40 | |
| 41 | #undef debug |
| 42 | #ifdef MII_DEBUG |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 43 | #define debug(fmt, args...) printf(fmt, ##args) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 44 | #else |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 45 | #define debug(fmt, args...) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 46 | #endif /* MII_DEBUG */ |
| 47 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 48 | static struct list_head mii_devs; |
| 49 | static struct mii_dev *current_mii; |
| 50 | |
Mike Frysinger | 24a9008 | 2010-07-27 18:35:09 -0400 | [diff] [blame] | 51 | /* |
| 52 | * Lookup the mii_dev struct by the registered device name. |
| 53 | */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 54 | struct mii_dev *miiphy_get_dev_by_name(const char *devname) |
Mike Frysinger | 24a9008 | 2010-07-27 18:35:09 -0400 | [diff] [blame] | 55 | { |
| 56 | struct list_head *entry; |
| 57 | struct mii_dev *dev; |
| 58 | |
| 59 | if (!devname) { |
| 60 | printf("NULL device name!\n"); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | list_for_each(entry, &mii_devs) { |
| 65 | dev = list_entry(entry, struct mii_dev, link); |
| 66 | if (strcmp(dev->name, devname) == 0) |
| 67 | return dev; |
| 68 | } |
| 69 | |
Mike Frysinger | 24a9008 | 2010-07-27 18:35:09 -0400 | [diff] [blame] | 70 | return NULL; |
| 71 | } |
| 72 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 73 | /***************************************************************************** |
| 74 | * |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 75 | * Initialize global data. Need to be called before any other miiphy routine. |
| 76 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 77 | void miiphy_init(void) |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 78 | { |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 79 | INIT_LIST_HEAD(&mii_devs); |
Larry Johnson | 81b974b | 2007-10-31 11:21:29 -0500 | [diff] [blame] | 80 | current_mii = NULL; |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 83 | static int legacy_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg) |
| 84 | { |
| 85 | unsigned short val; |
| 86 | int ret; |
| 87 | struct legacy_mii_dev *ldev = bus->priv; |
| 88 | |
| 89 | ret = ldev->read(bus->name, addr, reg, &val); |
| 90 | |
| 91 | return ret ? -1 : (int)val; |
| 92 | } |
| 93 | |
| 94 | static int legacy_miiphy_write(struct mii_dev *bus, int addr, int devad, |
| 95 | int reg, u16 val) |
| 96 | { |
| 97 | struct legacy_mii_dev *ldev = bus->priv; |
| 98 | |
| 99 | return ldev->write(bus->name, addr, reg, val); |
| 100 | } |
| 101 | |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 102 | /***************************************************************************** |
| 103 | * |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 104 | * Register read and write MII access routines for the device <name>. |
| 105 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 106 | void miiphy_register(const char *name, |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 107 | int (*read)(const char *devname, unsigned char addr, |
Larry Johnson | 81b974b | 2007-10-31 11:21:29 -0500 | [diff] [blame] | 108 | unsigned char reg, unsigned short *value), |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 109 | int (*write)(const char *devname, unsigned char addr, |
Larry Johnson | 81b974b | 2007-10-31 11:21:29 -0500 | [diff] [blame] | 110 | unsigned char reg, unsigned short value)) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 111 | { |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 112 | struct mii_dev *new_dev; |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 113 | struct legacy_mii_dev *ldev; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 114 | unsigned int name_len; |
| 115 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 116 | /* check if we have unique name */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 117 | new_dev = miiphy_get_dev_by_name(name); |
Mike Frysinger | 24a9008 | 2010-07-27 18:35:09 -0400 | [diff] [blame] | 118 | if (new_dev) { |
| 119 | printf("miiphy_register: non unique device name '%s'\n", name); |
| 120 | return; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /* allocate memory */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 124 | name_len = strlen(name); |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 125 | if (name_len > MDIO_NAME_LEN - 1) { |
| 126 | /* Hopefully this won't happen, but if it does, we'll know */ |
| 127 | printf("miiphy_register: MDIO name was longer than %d\n", |
| 128 | MDIO_NAME_LEN); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | new_dev = mdio_alloc(); |
| 133 | ldev = malloc(sizeof(*ldev)); |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 134 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 135 | if (new_dev == NULL || ldev == NULL) { |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 136 | printf("miiphy_register: cannot allocate memory for '%s'\n", |
Larry Johnson | 81b974b | 2007-10-31 11:21:29 -0500 | [diff] [blame] | 137 | name); |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 138 | return; |
| 139 | } |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 140 | |
| 141 | /* initalize mii_dev struct fields */ |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 142 | new_dev->read = legacy_miiphy_read; |
| 143 | new_dev->write = legacy_miiphy_write; |
| 144 | sprintf(new_dev->name, name); |
| 145 | ldev->read = read; |
| 146 | ldev->write = write; |
| 147 | new_dev->priv = ldev; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 148 | |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 149 | debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n", |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 150 | new_dev->name, ldev->read, ldev->write); |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 151 | |
| 152 | /* add it to the list */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 153 | list_add_tail(&new_dev->link, &mii_devs); |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 154 | |
| 155 | if (!current_mii) |
| 156 | current_mii = new_dev; |
| 157 | } |
| 158 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 159 | struct mii_dev *mdio_alloc(void) |
| 160 | { |
| 161 | struct mii_dev *bus; |
| 162 | |
| 163 | bus = malloc(sizeof(*bus)); |
| 164 | if (!bus) |
| 165 | return bus; |
| 166 | |
| 167 | memset(bus, 0, sizeof(*bus)); |
| 168 | |
| 169 | /* initalize mii_dev struct fields */ |
| 170 | INIT_LIST_HEAD(&bus->link); |
| 171 | |
| 172 | return bus; |
| 173 | } |
| 174 | |
| 175 | int mdio_register(struct mii_dev *bus) |
| 176 | { |
| 177 | if (!bus || !bus->name || !bus->read || !bus->write) |
| 178 | return -1; |
| 179 | |
| 180 | /* check if we have unique name */ |
| 181 | if (miiphy_get_dev_by_name(bus->name)) { |
| 182 | printf("mdio_register: non unique device name '%s'\n", |
| 183 | bus->name); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | /* add it to the list */ |
| 188 | list_add_tail(&bus->link, &mii_devs); |
| 189 | |
| 190 | if (!current_mii) |
| 191 | current_mii = bus; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | void mdio_list_devices(void) |
| 197 | { |
| 198 | struct list_head *entry; |
| 199 | |
| 200 | list_for_each(entry, &mii_devs) { |
| 201 | int i; |
| 202 | struct mii_dev *bus = list_entry(entry, struct mii_dev, link); |
| 203 | |
| 204 | printf("%s:\n", bus->name); |
| 205 | |
| 206 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
| 207 | struct phy_device *phydev = bus->phymap[i]; |
| 208 | |
| 209 | if (phydev) { |
| 210 | printf("%d - %s", i, phydev->drv->name); |
| 211 | |
| 212 | if (phydev->dev) |
| 213 | printf(" <--> %s\n", phydev->dev->name); |
| 214 | else |
| 215 | printf("\n"); |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 221 | int miiphy_set_current_dev(const char *devname) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 222 | { |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 223 | struct mii_dev *dev; |
| 224 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 225 | dev = miiphy_get_dev_by_name(devname); |
Mike Frysinger | 24a9008 | 2010-07-27 18:35:09 -0400 | [diff] [blame] | 226 | if (dev) { |
| 227 | current_mii = dev; |
| 228 | return 0; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 231 | printf("No such device: %s\n", devname); |
| 232 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 233 | return 1; |
| 234 | } |
| 235 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 236 | struct mii_dev *mdio_get_current_dev(void) |
| 237 | { |
| 238 | return current_mii; |
| 239 | } |
| 240 | |
| 241 | struct phy_device *mdio_phydev_for_ethname(const char *ethname) |
| 242 | { |
| 243 | struct list_head *entry; |
| 244 | struct mii_dev *bus; |
| 245 | |
| 246 | list_for_each(entry, &mii_devs) { |
| 247 | int i; |
| 248 | bus = list_entry(entry, struct mii_dev, link); |
| 249 | |
| 250 | for (i = 0; i < PHY_MAX_ADDR; i++) { |
| 251 | if (!bus->phymap[i] || !bus->phymap[i]->dev) |
| 252 | continue; |
| 253 | |
| 254 | if (strcmp(bus->phymap[i]->dev->name, ethname) == 0) |
| 255 | return bus->phymap[i]; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | printf("%s is not a known ethernet\n", ethname); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 263 | const char *miiphy_get_current_dev(void) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 264 | { |
| 265 | if (current_mii) |
| 266 | return current_mii->name; |
| 267 | |
| 268 | return NULL; |
| 269 | } |
| 270 | |
Mike Frysinger | bd17e7a | 2010-07-27 18:35:10 -0400 | [diff] [blame] | 271 | static struct mii_dev *miiphy_get_active_dev(const char *devname) |
| 272 | { |
| 273 | /* If the current mii is the one we want, return it */ |
| 274 | if (current_mii) |
| 275 | if (strcmp(current_mii->name, devname) == 0) |
| 276 | return current_mii; |
| 277 | |
| 278 | /* Otherwise, set the active one to the one we want */ |
| 279 | if (miiphy_set_current_dev(devname)) |
| 280 | return NULL; |
| 281 | else |
| 282 | return current_mii; |
| 283 | } |
| 284 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 285 | /***************************************************************************** |
| 286 | * |
| 287 | * Read to variable <value> from the PHY attached to device <devname>, |
| 288 | * use PHY address <addr> and register <reg>. |
| 289 | * |
| 290 | * Returns: |
| 291 | * 0 on success |
| 292 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 293 | int miiphy_read(const char *devname, unsigned char addr, unsigned char reg, |
Larry Johnson | 81b974b | 2007-10-31 11:21:29 -0500 | [diff] [blame] | 294 | unsigned short *value) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 295 | { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 296 | struct mii_dev *bus; |
Anatolij Gustschin | 1bbce3a | 2011-04-30 02:17:44 +0000 | [diff] [blame] | 297 | int ret; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 298 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 299 | bus = miiphy_get_active_dev(devname); |
Anatolij Gustschin | 1bbce3a | 2011-04-30 02:17:44 +0000 | [diff] [blame] | 300 | if (!bus) |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 301 | return 1; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 302 | |
Anatolij Gustschin | 1bbce3a | 2011-04-30 02:17:44 +0000 | [diff] [blame] | 303 | ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg); |
| 304 | if (ret < 0) |
| 305 | return 1; |
| 306 | |
| 307 | *value = (unsigned short)ret; |
| 308 | return 0; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /***************************************************************************** |
| 312 | * |
| 313 | * Write <value> to the PHY attached to device <devname>, |
| 314 | * use PHY address <addr> and register <reg>. |
| 315 | * |
| 316 | * Returns: |
| 317 | * 0 on success |
| 318 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 319 | int miiphy_write(const char *devname, unsigned char addr, unsigned char reg, |
Larry Johnson | 81b974b | 2007-10-31 11:21:29 -0500 | [diff] [blame] | 320 | unsigned short value) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 321 | { |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 322 | struct mii_dev *bus; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 323 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 324 | bus = miiphy_get_active_dev(devname); |
| 325 | if (bus) |
| 326 | return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value); |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 327 | |
Mike Frysinger | 24a9008 | 2010-07-27 18:35:09 -0400 | [diff] [blame] | 328 | return 1; |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /***************************************************************************** |
| 332 | * |
| 333 | * Print out list of registered MII capable devices. |
| 334 | */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 335 | void miiphy_listdev(void) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 336 | { |
| 337 | struct list_head *entry; |
| 338 | struct mii_dev *dev; |
| 339 | |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 340 | puts("MII devices: "); |
| 341 | list_for_each(entry, &mii_devs) { |
| 342 | dev = list_entry(entry, struct mii_dev, link); |
| 343 | printf("'%s' ", dev->name); |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 344 | } |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 345 | puts("\n"); |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 346 | |
| 347 | if (current_mii) |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 348 | printf("Current device: '%s'\n", current_mii->name); |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 349 | } |
| 350 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 351 | /***************************************************************************** |
| 352 | * |
| 353 | * Read the OUI, manufacture's model number, and revision number. |
| 354 | * |
| 355 | * OUI: 22 bits (unsigned int) |
| 356 | * Model: 6 bits (unsigned char) |
| 357 | * Revision: 4 bits (unsigned char) |
| 358 | * |
| 359 | * Returns: |
| 360 | * 0 on success |
| 361 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 362 | int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui, |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 363 | unsigned char *model, unsigned char *rev) |
| 364 | { |
| 365 | unsigned int reg = 0; |
wdenk | f4cec3f | 2003-12-06 23:20:41 +0000 | [diff] [blame] | 366 | unsigned short tmp; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 367 | |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 368 | if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) { |
| 369 | debug("PHY ID register 2 read failed\n"); |
| 370 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 371 | } |
wdenk | f4cec3f | 2003-12-06 23:20:41 +0000 | [diff] [blame] | 372 | reg = tmp; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 373 | |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 374 | debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg); |
Shinya Kuribayashi | 49e42af | 2008-01-19 10:25:59 +0900 | [diff] [blame] | 375 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 376 | if (reg == 0xFFFF) { |
| 377 | /* No physical device present at this address */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 378 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 381 | if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) { |
| 382 | debug("PHY ID register 1 read failed\n"); |
| 383 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 384 | } |
wdenk | f4cec3f | 2003-12-06 23:20:41 +0000 | [diff] [blame] | 385 | reg |= tmp << 16; |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 386 | debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg); |
Shinya Kuribayashi | 49e42af | 2008-01-19 10:25:59 +0900 | [diff] [blame] | 387 | |
Larry Johnson | 81b974b | 2007-10-31 11:21:29 -0500 | [diff] [blame] | 388 | *oui = (reg >> 10); |
| 389 | *model = (unsigned char)((reg >> 4) & 0x0000003F); |
| 390 | *rev = (unsigned char)(reg & 0x0000000F); |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 391 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 394 | #ifndef CONFIG_PHYLIB |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 395 | /***************************************************************************** |
| 396 | * |
| 397 | * Reset the PHY. |
| 398 | * Returns: |
| 399 | * 0 on success |
| 400 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 401 | int miiphy_reset(const char *devname, unsigned char addr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 402 | { |
| 403 | unsigned short reg; |
Stefan Roese | 2e53636 | 2010-02-02 13:43:48 +0100 | [diff] [blame] | 404 | int timeout = 500; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 405 | |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 406 | if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) { |
| 407 | debug("PHY status read failed\n"); |
| 408 | return -1; |
Wolfgang Denk | 8ff63c2 | 2005-08-12 23:15:53 +0200 | [diff] [blame] | 409 | } |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 410 | if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) { |
| 411 | debug("PHY reset failed\n"); |
| 412 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 413 | } |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 414 | #ifdef CONFIG_PHY_RESET_DELAY |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 415 | udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */ |
wdenk | 2cefd15 | 2004-02-08 22:55:38 +0000 | [diff] [blame] | 416 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 417 | /* |
| 418 | * Poll the control register for the reset bit to go to 0 (it is |
| 419 | * auto-clearing). This should happen within 0.5 seconds per the |
| 420 | * IEEE spec. |
| 421 | */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 422 | reg = 0x8000; |
Stefan Roese | 2e53636 | 2010-02-02 13:43:48 +0100 | [diff] [blame] | 423 | while (((reg & 0x8000) != 0) && timeout--) { |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 424 | if (miiphy_read(devname, addr, MII_BMCR, ®) != 0) { |
Stefan Roese | 2e53636 | 2010-02-02 13:43:48 +0100 | [diff] [blame] | 425 | debug("PHY status read failed\n"); |
| 426 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 427 | } |
Stefan Roese | 2e53636 | 2010-02-02 13:43:48 +0100 | [diff] [blame] | 428 | udelay(1000); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 429 | } |
| 430 | if ((reg & 0x8000) == 0) { |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 431 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 432 | } else { |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 433 | puts("PHY reset timed out\n"); |
| 434 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 435 | } |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 436 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 437 | } |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 438 | #endif /* !PHYLIB */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 439 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 440 | /***************************************************************************** |
| 441 | * |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 442 | * Determine the ethernet speed (10/100/1000). Return 10 on error. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 443 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 444 | int miiphy_speed(const char *devname, unsigned char addr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 445 | { |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 446 | u16 bmcr, anlpar; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 447 | |
wdenk | eec9a3d | 2004-03-23 23:20:24 +0000 | [diff] [blame] | 448 | #if defined(CONFIG_PHY_GIGE) |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 449 | u16 btsr; |
| 450 | |
| 451 | /* |
| 452 | * Check for 1000BASE-X. If it is supported, then assume that the speed |
| 453 | * is 1000. |
| 454 | */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 455 | if (miiphy_is_1000base_x(devname, addr)) |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 456 | return _1000BASET; |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 457 | |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 458 | /* |
| 459 | * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set. |
| 460 | */ |
| 461 | /* Check for 1000BASE-T. */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 462 | if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) { |
| 463 | printf("PHY 1000BT status"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 464 | goto miiphy_read_failed; |
| 465 | } |
| 466 | if (btsr != 0xFFFF && |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 467 | (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD))) |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 468 | return _1000BASET; |
wdenk | eec9a3d | 2004-03-23 23:20:24 +0000 | [diff] [blame] | 469 | #endif /* CONFIG_PHY_GIGE */ |
wdenk | ed2ac4b | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 470 | |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 471 | /* Check Basic Management Control Register first. */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 472 | if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) { |
| 473 | printf("PHY speed"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 474 | goto miiphy_read_failed; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 475 | } |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 476 | /* Check if auto-negotiation is on. */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 477 | if (bmcr & BMCR_ANENABLE) { |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 478 | /* Get auto-negotiation results. */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 479 | if (miiphy_read(devname, addr, MII_LPA, &anlpar)) { |
| 480 | printf("PHY AN speed"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 481 | goto miiphy_read_failed; |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 482 | } |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 483 | return (anlpar & LPA_100) ? _100BASET : _10BASET; |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 484 | } |
| 485 | /* Get speed from basic control settings. */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 486 | return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET; |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 487 | |
Michael Zaidman | 6dbca5f | 2010-02-28 16:28:25 +0200 | [diff] [blame] | 488 | miiphy_read_failed: |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 489 | printf(" read failed, assuming 10BASE-T\n"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 490 | return _10BASET; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 491 | } |
| 492 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 493 | /***************************************************************************** |
| 494 | * |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 495 | * Determine full/half duplex. Return half on error. |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 496 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 497 | int miiphy_duplex(const char *devname, unsigned char addr) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 498 | { |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 499 | u16 bmcr, anlpar; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 500 | |
wdenk | eec9a3d | 2004-03-23 23:20:24 +0000 | [diff] [blame] | 501 | #if defined(CONFIG_PHY_GIGE) |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 502 | u16 btsr; |
| 503 | |
| 504 | /* Check for 1000BASE-X. */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 505 | if (miiphy_is_1000base_x(devname, addr)) { |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 506 | /* 1000BASE-X */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 507 | if (miiphy_read(devname, addr, MII_LPA, &anlpar)) { |
| 508 | printf("1000BASE-X PHY AN duplex"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 509 | goto miiphy_read_failed; |
wdenk | ed2ac4b | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 512 | /* |
| 513 | * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set. |
| 514 | */ |
| 515 | /* Check for 1000BASE-T. */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 516 | if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) { |
| 517 | printf("PHY 1000BT status"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 518 | goto miiphy_read_failed; |
| 519 | } |
| 520 | if (btsr != 0xFFFF) { |
| 521 | if (btsr & PHY_1000BTSR_1000FD) { |
| 522 | return FULL; |
| 523 | } else if (btsr & PHY_1000BTSR_1000HD) { |
| 524 | return HALF; |
| 525 | } |
| 526 | } |
wdenk | eec9a3d | 2004-03-23 23:20:24 +0000 | [diff] [blame] | 527 | #endif /* CONFIG_PHY_GIGE */ |
wdenk | ed2ac4b | 2004-03-14 18:23:55 +0000 | [diff] [blame] | 528 | |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 529 | /* Check Basic Management Control Register first. */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 530 | if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) { |
| 531 | puts("PHY duplex"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 532 | goto miiphy_read_failed; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 533 | } |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 534 | /* Check if auto-negotiation is on. */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 535 | if (bmcr & BMCR_ANENABLE) { |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 536 | /* Get auto-negotiation results. */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 537 | if (miiphy_read(devname, addr, MII_LPA, &anlpar)) { |
| 538 | puts("PHY AN duplex"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 539 | goto miiphy_read_failed; |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 540 | } |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 541 | return (anlpar & (LPA_10FULL | LPA_100FULL)) ? |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 542 | FULL : HALF; |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 543 | } |
| 544 | /* Get speed from basic control settings. */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 545 | return (bmcr & BMCR_FULLDPLX) ? FULL : HALF; |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 546 | |
Michael Zaidman | 6dbca5f | 2010-02-28 16:28:25 +0200 | [diff] [blame] | 547 | miiphy_read_failed: |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 548 | printf(" read failed, assuming half duplex\n"); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 549 | return HALF; |
| 550 | } |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 551 | |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 552 | /***************************************************************************** |
| 553 | * |
| 554 | * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/ |
| 555 | * 1000BASE-T, or on error. |
| 556 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 557 | int miiphy_is_1000base_x(const char *devname, unsigned char addr) |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 558 | { |
| 559 | #if defined(CONFIG_PHY_GIGE) |
| 560 | u16 exsr; |
| 561 | |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 562 | if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) { |
| 563 | printf("PHY extended status read failed, assuming no " |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 564 | "1000BASE-X\n"); |
| 565 | return 0; |
| 566 | } |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 567 | return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH)); |
Larry Johnson | 966a80b | 2007-11-01 08:46:50 -0500 | [diff] [blame] | 568 | #else |
| 569 | return 0; |
| 570 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 573 | #ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 574 | /***************************************************************************** |
| 575 | * |
| 576 | * Determine link status |
| 577 | */ |
Mike Frysinger | 5ff5fdb | 2010-07-27 18:35:08 -0400 | [diff] [blame] | 578 | int miiphy_link(const char *devname, unsigned char addr) |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 579 | { |
| 580 | unsigned short reg; |
| 581 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 582 | /* dummy read; needed to latch some phys */ |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 583 | (void)miiphy_read(devname, addr, MII_BMSR, ®); |
| 584 | if (miiphy_read(devname, addr, MII_BMSR, ®)) { |
| 585 | puts("MII_BMSR read failed, assuming no link\n"); |
| 586 | return 0; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* Determine if a link is active */ |
Mike Frysinger | d63ee71 | 2010-12-23 15:40:12 -0500 | [diff] [blame] | 590 | if ((reg & BMSR_LSTATUS) != 0) { |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 591 | return 1; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 592 | } else { |
Andy Fleming | aea0c3e | 2011-04-07 14:38:35 -0500 | [diff] [blame] | 593 | return 0; |
wdenk | 49c3f67 | 2003-10-08 22:33:00 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
| 596 | #endif |