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