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