blob: 7e41957185072123a81468309081ec4586730867 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2001
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8/*
9 * This provides a bit-banged interface to the ethernet MII management
10 * channel.
11 */
12
13#include <common.h>
Simon Glassc74c8e62015-04-05 16:07:39 -060014#include <dm.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <miiphy.h>
Andy Fleming5f184712011-04-08 02:10:27 -050016#include <phy.h>
wdenkc6097192002-11-03 00:24:07 +000017
Marian Balakowicz63ff0042005-10-28 22:30:33 +020018#include <asm/types.h>
19#include <linux/list.h>
20#include <malloc.h>
21#include <net.h>
22
23/* local debug macro */
Marian Balakowicz63ff0042005-10-28 22:30:33 +020024#undef MII_DEBUG
25
26#undef debug
27#ifdef MII_DEBUG
Andy Fleming16a53232011-04-07 14:38:35 -050028#define debug(fmt, args...) printf(fmt, ##args)
Marian Balakowicz63ff0042005-10-28 22:30:33 +020029#else
Andy Fleming16a53232011-04-07 14:38:35 -050030#define debug(fmt, args...)
Marian Balakowicz63ff0042005-10-28 22:30:33 +020031#endif /* MII_DEBUG */
32
Marian Balakowicz63ff0042005-10-28 22:30:33 +020033static struct list_head mii_devs;
34static struct mii_dev *current_mii;
35
Mike Frysinger0daac972010-07-27 18:35:09 -040036/*
37 * Lookup the mii_dev struct by the registered device name.
38 */
Andy Fleming5f184712011-04-08 02:10:27 -050039struct mii_dev *miiphy_get_dev_by_name(const char *devname)
Mike Frysinger0daac972010-07-27 18:35:09 -040040{
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 Frysinger0daac972010-07-27 18:35:09 -040055 return NULL;
56}
57
Marian Balakowicz63ff0042005-10-28 22:30:33 +020058/*****************************************************************************
59 *
Marian Balakowiczd9785c12005-11-30 18:06:04 +010060 * Initialize global data. Need to be called before any other miiphy routine.
61 */
Mike Frysinger5700bb62010-07-27 18:35:08 -040062void miiphy_init(void)
Marian Balakowiczd9785c12005-11-30 18:06:04 +010063{
Andy Fleming16a53232011-04-07 14:38:35 -050064 INIT_LIST_HEAD(&mii_devs);
Larry Johnson298035d2007-10-31 11:21:29 -050065 current_mii = NULL;
Marian Balakowiczd9785c12005-11-30 18:06:04 +010066}
67
Andy Fleming5f184712011-04-08 02:10:27 -050068static int legacy_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
69{
70 unsigned short val;
71 int ret;
72 struct legacy_mii_dev *ldev = bus->priv;
73
74 ret = ldev->read(bus->name, addr, reg, &val);
75
76 return ret ? -1 : (int)val;
77}
78
79static int legacy_miiphy_write(struct mii_dev *bus, int addr, int devad,
80 int reg, u16 val)
81{
82 struct legacy_mii_dev *ldev = bus->priv;
83
84 return ldev->write(bus->name, addr, reg, val);
85}
86
Marian Balakowiczd9785c12005-11-30 18:06:04 +010087/*****************************************************************************
88 *
Marian Balakowicz63ff0042005-10-28 22:30:33 +020089 * Register read and write MII access routines for the device <name>.
Andy Fleming1cdabc42011-10-31 09:46:13 -050090 * This API is now deprecated. Please use mdio_alloc and mdio_register, instead.
Marian Balakowicz63ff0042005-10-28 22:30:33 +020091 */
Mike Frysinger5700bb62010-07-27 18:35:08 -040092void miiphy_register(const char *name,
Andy Fleming16a53232011-04-07 14:38:35 -050093 int (*read)(const char *devname, unsigned char addr,
Wolfgang Denkf915c932011-12-07 08:35:14 +010094 unsigned char reg, unsigned short *value),
Andy Fleming16a53232011-04-07 14:38:35 -050095 int (*write)(const char *devname, unsigned char addr,
Wolfgang Denkf915c932011-12-07 08:35:14 +010096 unsigned char reg, unsigned short value))
Marian Balakowicz63ff0042005-10-28 22:30:33 +020097{
Marian Balakowicz63ff0042005-10-28 22:30:33 +020098 struct mii_dev *new_dev;
Andy Fleming5f184712011-04-08 02:10:27 -050099 struct legacy_mii_dev *ldev;
Laurence Withers07c07632011-07-14 23:21:45 +0000100
101 BUG_ON(strlen(name) >= MDIO_NAME_LEN);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200102
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200103 /* check if we have unique name */
Andy Fleming5f184712011-04-08 02:10:27 -0500104 new_dev = miiphy_get_dev_by_name(name);
Mike Frysinger0daac972010-07-27 18:35:09 -0400105 if (new_dev) {
106 printf("miiphy_register: non unique device name '%s'\n", name);
107 return;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200108 }
109
110 /* allocate memory */
Andy Fleming5f184712011-04-08 02:10:27 -0500111 new_dev = mdio_alloc();
112 ldev = malloc(sizeof(*ldev));
113
114 if (new_dev == NULL || ldev == NULL) {
Andy Fleming16a53232011-04-07 14:38:35 -0500115 printf("miiphy_register: cannot allocate memory for '%s'\n",
Larry Johnson298035d2007-10-31 11:21:29 -0500116 name);
Peng Fan746da1b2015-11-26 10:26:59 +0800117 free(ldev);
118 mdio_free(new_dev);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200119 return;
120 }
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200121
122 /* initalize mii_dev struct fields */
Andy Fleming5f184712011-04-08 02:10:27 -0500123 new_dev->read = legacy_miiphy_read;
124 new_dev->write = legacy_miiphy_write;
Laurence Withers07c07632011-07-14 23:21:45 +0000125 strncpy(new_dev->name, name, MDIO_NAME_LEN);
126 new_dev->name[MDIO_NAME_LEN - 1] = 0;
Andy Fleming5f184712011-04-08 02:10:27 -0500127 ldev->read = read;
128 ldev->write = write;
129 new_dev->priv = ldev;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200130
Andy Fleming16a53232011-04-07 14:38:35 -0500131 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
Andy Fleming5f184712011-04-08 02:10:27 -0500132 new_dev->name, ldev->read, ldev->write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200133
134 /* add it to the list */
Andy Fleming16a53232011-04-07 14:38:35 -0500135 list_add_tail(&new_dev->link, &mii_devs);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200136
137 if (!current_mii)
138 current_mii = new_dev;
139}
140
Andy Fleming5f184712011-04-08 02:10:27 -0500141struct mii_dev *mdio_alloc(void)
142{
143 struct mii_dev *bus;
144
145 bus = malloc(sizeof(*bus));
146 if (!bus)
147 return bus;
148
149 memset(bus, 0, sizeof(*bus));
150
151 /* initalize mii_dev struct fields */
152 INIT_LIST_HEAD(&bus->link);
153
154 return bus;
155}
156
Bin Mengcb6baca2015-10-07 21:32:37 -0700157void mdio_free(struct mii_dev *bus)
158{
159 free(bus);
160}
161
Andy Fleming5f184712011-04-08 02:10:27 -0500162int mdio_register(struct mii_dev *bus)
163{
Peng Fand39449b2015-11-24 17:03:47 +0800164 if (!bus || !bus->read || !bus->write)
Andy Fleming5f184712011-04-08 02:10:27 -0500165 return -1;
166
167 /* check if we have unique name */
168 if (miiphy_get_dev_by_name(bus->name)) {
169 printf("mdio_register: non unique device name '%s'\n",
170 bus->name);
171 return -1;
172 }
173
174 /* add it to the list */
175 list_add_tail(&bus->link, &mii_devs);
176
177 if (!current_mii)
178 current_mii = bus;
179
180 return 0;
181}
182
Bin Mengcb6baca2015-10-07 21:32:37 -0700183int mdio_unregister(struct mii_dev *bus)
184{
185 if (!bus)
186 return 0;
187
188 /* delete it from the list */
189 list_del(&bus->link);
190
191 if (current_mii == bus)
192 current_mii = NULL;
193
194 return 0;
195}
196
Andy Fleming5f184712011-04-08 02:10:27 -0500197void mdio_list_devices(void)
198{
199 struct list_head *entry;
200
201 list_for_each(entry, &mii_devs) {
202 int i;
203 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
204
205 printf("%s:\n", bus->name);
206
207 for (i = 0; i < PHY_MAX_ADDR; i++) {
208 struct phy_device *phydev = bus->phymap[i];
209
210 if (phydev) {
211 printf("%d - %s", i, phydev->drv->name);
212
213 if (phydev->dev)
214 printf(" <--> %s\n", phydev->dev->name);
215 else
216 printf("\n");
217 }
218 }
219 }
220}
221
Mike Frysinger5700bb62010-07-27 18:35:08 -0400222int miiphy_set_current_dev(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200223{
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200224 struct mii_dev *dev;
225
Andy Fleming5f184712011-04-08 02:10:27 -0500226 dev = miiphy_get_dev_by_name(devname);
Mike Frysinger0daac972010-07-27 18:35:09 -0400227 if (dev) {
228 current_mii = dev;
229 return 0;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200230 }
231
Andy Fleming5f184712011-04-08 02:10:27 -0500232 printf("No such device: %s\n", devname);
233
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200234 return 1;
235}
236
Andy Fleming5f184712011-04-08 02:10:27 -0500237struct mii_dev *mdio_get_current_dev(void)
238{
239 return current_mii;
240}
241
242struct phy_device *mdio_phydev_for_ethname(const char *ethname)
243{
244 struct list_head *entry;
245 struct mii_dev *bus;
246
247 list_for_each(entry, &mii_devs) {
248 int i;
249 bus = list_entry(entry, struct mii_dev, link);
250
251 for (i = 0; i < PHY_MAX_ADDR; i++) {
252 if (!bus->phymap[i] || !bus->phymap[i]->dev)
253 continue;
254
255 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
256 return bus->phymap[i];
257 }
258 }
259
260 printf("%s is not a known ethernet\n", ethname);
261 return NULL;
262}
263
Mike Frysinger5700bb62010-07-27 18:35:08 -0400264const char *miiphy_get_current_dev(void)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200265{
266 if (current_mii)
267 return current_mii->name;
268
269 return NULL;
270}
271
Mike Frysingerede16ea2010-07-27 18:35:10 -0400272static struct mii_dev *miiphy_get_active_dev(const char *devname)
273{
274 /* If the current mii is the one we want, return it */
275 if (current_mii)
276 if (strcmp(current_mii->name, devname) == 0)
277 return current_mii;
278
279 /* Otherwise, set the active one to the one we want */
280 if (miiphy_set_current_dev(devname))
281 return NULL;
282 else
283 return current_mii;
284}
285
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200286/*****************************************************************************
287 *
288 * Read to variable <value> from the PHY attached to device <devname>,
289 * use PHY address <addr> and register <reg>.
290 *
Andy Fleming1cdabc42011-10-31 09:46:13 -0500291 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
292 *
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200293 * Returns:
294 * 0 on success
295 */
Wolfgang Denkf915c932011-12-07 08:35:14 +0100296int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
Larry Johnson298035d2007-10-31 11:21:29 -0500297 unsigned short *value)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200298{
Andy Fleming5f184712011-04-08 02:10:27 -0500299 struct mii_dev *bus;
Anatolij Gustschind67d5d52011-04-30 02:17:44 +0000300 int ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200301
Andy Fleming5f184712011-04-08 02:10:27 -0500302 bus = miiphy_get_active_dev(devname);
Anatolij Gustschind67d5d52011-04-30 02:17:44 +0000303 if (!bus)
Andy Fleming5f184712011-04-08 02:10:27 -0500304 return 1;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200305
Anatolij Gustschind67d5d52011-04-30 02:17:44 +0000306 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
307 if (ret < 0)
308 return 1;
309
310 *value = (unsigned short)ret;
311 return 0;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200312}
313
314/*****************************************************************************
315 *
316 * Write <value> to the PHY attached to device <devname>,
317 * use PHY address <addr> and register <reg>.
318 *
Andy Fleming1cdabc42011-10-31 09:46:13 -0500319 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
320 *
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200321 * Returns:
322 * 0 on success
323 */
Wolfgang Denkf915c932011-12-07 08:35:14 +0100324int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
Larry Johnson298035d2007-10-31 11:21:29 -0500325 unsigned short value)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200326{
Andy Fleming5f184712011-04-08 02:10:27 -0500327 struct mii_dev *bus;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200328
Andy Fleming5f184712011-04-08 02:10:27 -0500329 bus = miiphy_get_active_dev(devname);
330 if (bus)
331 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200332
Mike Frysinger0daac972010-07-27 18:35:09 -0400333 return 1;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200334}
335
336/*****************************************************************************
337 *
338 * Print out list of registered MII capable devices.
339 */
Andy Fleming16a53232011-04-07 14:38:35 -0500340void miiphy_listdev(void)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200341{
342 struct list_head *entry;
343 struct mii_dev *dev;
344
Andy Fleming16a53232011-04-07 14:38:35 -0500345 puts("MII devices: ");
346 list_for_each(entry, &mii_devs) {
347 dev = list_entry(entry, struct mii_dev, link);
348 printf("'%s' ", dev->name);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200349 }
Andy Fleming16a53232011-04-07 14:38:35 -0500350 puts("\n");
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200351
352 if (current_mii)
Andy Fleming16a53232011-04-07 14:38:35 -0500353 printf("Current device: '%s'\n", current_mii->name);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200354}
355
wdenkc6097192002-11-03 00:24:07 +0000356/*****************************************************************************
357 *
358 * Read the OUI, manufacture's model number, and revision number.
359 *
360 * OUI: 22 bits (unsigned int)
361 * Model: 6 bits (unsigned char)
362 * Revision: 4 bits (unsigned char)
363 *
Andy Fleming1cdabc42011-10-31 09:46:13 -0500364 * This API is deprecated.
365 *
wdenkc6097192002-11-03 00:24:07 +0000366 * Returns:
367 * 0 on success
368 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400369int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
wdenkc6097192002-11-03 00:24:07 +0000370 unsigned char *model, unsigned char *rev)
371{
372 unsigned int reg = 0;
wdenk8bf3b002003-12-06 23:20:41 +0000373 unsigned short tmp;
wdenkc6097192002-11-03 00:24:07 +0000374
Andy Fleming16a53232011-04-07 14:38:35 -0500375 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
376 debug("PHY ID register 2 read failed\n");
377 return -1;
wdenkc6097192002-11-03 00:24:07 +0000378 }
wdenk8bf3b002003-12-06 23:20:41 +0000379 reg = tmp;
wdenkc6097192002-11-03 00:24:07 +0000380
Andy Fleming16a53232011-04-07 14:38:35 -0500381 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
Shinya Kuribayashi26c7bab2008-01-19 10:25:59 +0900382
wdenkc6097192002-11-03 00:24:07 +0000383 if (reg == 0xFFFF) {
384 /* No physical device present at this address */
Andy Fleming16a53232011-04-07 14:38:35 -0500385 return -1;
wdenkc6097192002-11-03 00:24:07 +0000386 }
387
Andy Fleming16a53232011-04-07 14:38:35 -0500388 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
389 debug("PHY ID register 1 read failed\n");
390 return -1;
wdenkc6097192002-11-03 00:24:07 +0000391 }
wdenk8bf3b002003-12-06 23:20:41 +0000392 reg |= tmp << 16;
Andy Fleming16a53232011-04-07 14:38:35 -0500393 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
Shinya Kuribayashi26c7bab2008-01-19 10:25:59 +0900394
Larry Johnson298035d2007-10-31 11:21:29 -0500395 *oui = (reg >> 10);
396 *model = (unsigned char)((reg >> 4) & 0x0000003F);
397 *rev = (unsigned char)(reg & 0x0000000F);
Andy Fleming16a53232011-04-07 14:38:35 -0500398 return 0;
wdenkc6097192002-11-03 00:24:07 +0000399}
400
Andy Fleming5f184712011-04-08 02:10:27 -0500401#ifndef CONFIG_PHYLIB
wdenkc6097192002-11-03 00:24:07 +0000402/*****************************************************************************
403 *
404 * Reset the PHY.
Andy Fleming1cdabc42011-10-31 09:46:13 -0500405 *
406 * This API is deprecated. Use PHYLIB.
407 *
wdenkc6097192002-11-03 00:24:07 +0000408 * Returns:
409 * 0 on success
410 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400411int miiphy_reset(const char *devname, unsigned char addr)
wdenkc6097192002-11-03 00:24:07 +0000412{
413 unsigned short reg;
Stefan Roeseab5a0dc2010-02-02 13:43:48 +0100414 int timeout = 500;
wdenkc6097192002-11-03 00:24:07 +0000415
Andy Fleming16a53232011-04-07 14:38:35 -0500416 if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
417 debug("PHY status read failed\n");
418 return -1;
Wolfgang Denkf89920c2005-08-12 23:15:53 +0200419 }
Andy Fleming16a53232011-04-07 14:38:35 -0500420 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
421 debug("PHY reset failed\n");
422 return -1;
wdenkc6097192002-11-03 00:24:07 +0000423 }
wdenk5653fc32004-02-08 22:55:38 +0000424#ifdef CONFIG_PHY_RESET_DELAY
Andy Fleming16a53232011-04-07 14:38:35 -0500425 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
wdenk5653fc32004-02-08 22:55:38 +0000426#endif
wdenkc6097192002-11-03 00:24:07 +0000427 /*
428 * Poll the control register for the reset bit to go to 0 (it is
429 * auto-clearing). This should happen within 0.5 seconds per the
430 * IEEE spec.
431 */
wdenkc6097192002-11-03 00:24:07 +0000432 reg = 0x8000;
Stefan Roeseab5a0dc2010-02-02 13:43:48 +0100433 while (((reg & 0x8000) != 0) && timeout--) {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500434 if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
Stefan Roeseab5a0dc2010-02-02 13:43:48 +0100435 debug("PHY status read failed\n");
436 return -1;
wdenkc6097192002-11-03 00:24:07 +0000437 }
Stefan Roeseab5a0dc2010-02-02 13:43:48 +0100438 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +0000439 }
440 if ((reg & 0x8000) == 0) {
Andy Fleming16a53232011-04-07 14:38:35 -0500441 return 0;
wdenkc6097192002-11-03 00:24:07 +0000442 } else {
Andy Fleming16a53232011-04-07 14:38:35 -0500443 puts("PHY reset timed out\n");
444 return -1;
wdenkc6097192002-11-03 00:24:07 +0000445 }
Andy Fleming16a53232011-04-07 14:38:35 -0500446 return 0;
wdenkc6097192002-11-03 00:24:07 +0000447}
Andy Fleming5f184712011-04-08 02:10:27 -0500448#endif /* !PHYLIB */
wdenkc6097192002-11-03 00:24:07 +0000449
wdenkc6097192002-11-03 00:24:07 +0000450/*****************************************************************************
451 *
Larry Johnson71bc6e62007-11-01 08:46:50 -0500452 * Determine the ethernet speed (10/100/1000). Return 10 on error.
wdenkc6097192002-11-03 00:24:07 +0000453 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400454int miiphy_speed(const char *devname, unsigned char addr)
wdenkc6097192002-11-03 00:24:07 +0000455{
Larry Johnson71bc6e62007-11-01 08:46:50 -0500456 u16 bmcr, anlpar;
wdenkc6097192002-11-03 00:24:07 +0000457
wdenk6fb6af62004-03-23 23:20:24 +0000458#if defined(CONFIG_PHY_GIGE)
Larry Johnson71bc6e62007-11-01 08:46:50 -0500459 u16 btsr;
460
461 /*
462 * Check for 1000BASE-X. If it is supported, then assume that the speed
463 * is 1000.
464 */
Andy Fleming16a53232011-04-07 14:38:35 -0500465 if (miiphy_is_1000base_x(devname, addr))
Larry Johnson71bc6e62007-11-01 08:46:50 -0500466 return _1000BASET;
Andy Fleming16a53232011-04-07 14:38:35 -0500467
Larry Johnson71bc6e62007-11-01 08:46:50 -0500468 /*
469 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
470 */
471 /* Check for 1000BASE-T. */
Andy Fleming16a53232011-04-07 14:38:35 -0500472 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
473 printf("PHY 1000BT status");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500474 goto miiphy_read_failed;
475 }
476 if (btsr != 0xFFFF &&
Andy Fleming16a53232011-04-07 14:38:35 -0500477 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
Larry Johnson71bc6e62007-11-01 08:46:50 -0500478 return _1000BASET;
wdenk6fb6af62004-03-23 23:20:24 +0000479#endif /* CONFIG_PHY_GIGE */
wdenk855a4962004-03-14 18:23:55 +0000480
wdenka56bd922004-06-06 23:13:55 +0000481 /* Check Basic Management Control Register first. */
Andy Fleming16a53232011-04-07 14:38:35 -0500482 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
483 printf("PHY speed");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500484 goto miiphy_read_failed;
wdenkc6097192002-11-03 00:24:07 +0000485 }
wdenka56bd922004-06-06 23:13:55 +0000486 /* Check if auto-negotiation is on. */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500487 if (bmcr & BMCR_ANENABLE) {
wdenka56bd922004-06-06 23:13:55 +0000488 /* Get auto-negotiation results. */
Andy Fleming16a53232011-04-07 14:38:35 -0500489 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
490 printf("PHY AN speed");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500491 goto miiphy_read_failed;
wdenka56bd922004-06-06 23:13:55 +0000492 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500493 return (anlpar & LPA_100) ? _100BASET : _10BASET;
wdenka56bd922004-06-06 23:13:55 +0000494 }
495 /* Get speed from basic control settings. */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500496 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
wdenka56bd922004-06-06 23:13:55 +0000497
Michael Zaidman5f841952010-02-28 16:28:25 +0200498miiphy_read_failed:
Andy Fleming16a53232011-04-07 14:38:35 -0500499 printf(" read failed, assuming 10BASE-T\n");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500500 return _10BASET;
wdenkc6097192002-11-03 00:24:07 +0000501}
502
wdenkc6097192002-11-03 00:24:07 +0000503/*****************************************************************************
504 *
Larry Johnson71bc6e62007-11-01 08:46:50 -0500505 * Determine full/half duplex. Return half on error.
wdenkc6097192002-11-03 00:24:07 +0000506 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400507int miiphy_duplex(const char *devname, unsigned char addr)
wdenkc6097192002-11-03 00:24:07 +0000508{
Larry Johnson71bc6e62007-11-01 08:46:50 -0500509 u16 bmcr, anlpar;
wdenkc6097192002-11-03 00:24:07 +0000510
wdenk6fb6af62004-03-23 23:20:24 +0000511#if defined(CONFIG_PHY_GIGE)
Larry Johnson71bc6e62007-11-01 08:46:50 -0500512 u16 btsr;
513
514 /* Check for 1000BASE-X. */
Andy Fleming16a53232011-04-07 14:38:35 -0500515 if (miiphy_is_1000base_x(devname, addr)) {
Larry Johnson71bc6e62007-11-01 08:46:50 -0500516 /* 1000BASE-X */
Andy Fleming16a53232011-04-07 14:38:35 -0500517 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
518 printf("1000BASE-X PHY AN duplex");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500519 goto miiphy_read_failed;
520 }
521 }
522 /*
523 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
524 */
525 /* Check for 1000BASE-T. */
Andy Fleming16a53232011-04-07 14:38:35 -0500526 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
527 printf("PHY 1000BT status");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500528 goto miiphy_read_failed;
529 }
530 if (btsr != 0xFFFF) {
531 if (btsr & PHY_1000BTSR_1000FD) {
532 return FULL;
533 } else if (btsr & PHY_1000BTSR_1000HD) {
534 return HALF;
wdenk855a4962004-03-14 18:23:55 +0000535 }
536 }
wdenk6fb6af62004-03-23 23:20:24 +0000537#endif /* CONFIG_PHY_GIGE */
wdenk855a4962004-03-14 18:23:55 +0000538
wdenka56bd922004-06-06 23:13:55 +0000539 /* Check Basic Management Control Register first. */
Andy Fleming16a53232011-04-07 14:38:35 -0500540 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
541 puts("PHY duplex");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500542 goto miiphy_read_failed;
wdenkc6097192002-11-03 00:24:07 +0000543 }
wdenka56bd922004-06-06 23:13:55 +0000544 /* Check if auto-negotiation is on. */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500545 if (bmcr & BMCR_ANENABLE) {
wdenka56bd922004-06-06 23:13:55 +0000546 /* Get auto-negotiation results. */
Andy Fleming16a53232011-04-07 14:38:35 -0500547 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
548 puts("PHY AN duplex");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500549 goto miiphy_read_failed;
wdenka56bd922004-06-06 23:13:55 +0000550 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500551 return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
Larry Johnson71bc6e62007-11-01 08:46:50 -0500552 FULL : HALF;
wdenka56bd922004-06-06 23:13:55 +0000553 }
554 /* Get speed from basic control settings. */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500555 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
wdenka56bd922004-06-06 23:13:55 +0000556
Michael Zaidman5f841952010-02-28 16:28:25 +0200557miiphy_read_failed:
Andy Fleming16a53232011-04-07 14:38:35 -0500558 printf(" read failed, assuming half duplex\n");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500559 return HALF;
560}
561
562/*****************************************************************************
563 *
564 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
565 * 1000BASE-T, or on error.
566 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400567int miiphy_is_1000base_x(const char *devname, unsigned char addr)
Larry Johnson71bc6e62007-11-01 08:46:50 -0500568{
569#if defined(CONFIG_PHY_GIGE)
570 u16 exsr;
571
Andy Fleming16a53232011-04-07 14:38:35 -0500572 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
573 printf("PHY extended status read failed, assuming no "
Larry Johnson71bc6e62007-11-01 08:46:50 -0500574 "1000BASE-X\n");
575 return 0;
576 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500577 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
Larry Johnson71bc6e62007-11-01 08:46:50 -0500578#else
579 return 0;
580#endif
wdenkc6097192002-11-03 00:24:07 +0000581}
582
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200583#ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
wdenkfc3e2162003-10-08 22:33:00 +0000584/*****************************************************************************
585 *
586 * Determine link status
587 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400588int miiphy_link(const char *devname, unsigned char addr)
wdenkfc3e2162003-10-08 22:33:00 +0000589{
590 unsigned short reg;
591
wdenka3d991b2004-04-15 21:48:45 +0000592 /* dummy read; needed to latch some phys */
Andy Fleming16a53232011-04-07 14:38:35 -0500593 (void)miiphy_read(devname, addr, MII_BMSR, &reg);
594 if (miiphy_read(devname, addr, MII_BMSR, &reg)) {
595 puts("MII_BMSR read failed, assuming no link\n");
596 return 0;
wdenkfc3e2162003-10-08 22:33:00 +0000597 }
598
599 /* Determine if a link is active */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500600 if ((reg & BMSR_LSTATUS) != 0) {
Andy Fleming16a53232011-04-07 14:38:35 -0500601 return 1;
wdenkfc3e2162003-10-08 22:33:00 +0000602 } else {
Andy Fleming16a53232011-04-07 14:38:35 -0500603 return 0;
wdenkfc3e2162003-10-08 22:33:00 +0000604 }
605}
606#endif