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