blob: e499b588368fd9dcf9f04a6619976923e9d30a53 [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);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200117 return;
118 }
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200119
120 /* initalize mii_dev struct fields */
Andy Fleming5f184712011-04-08 02:10:27 -0500121 new_dev->read = legacy_miiphy_read;
122 new_dev->write = legacy_miiphy_write;
Laurence Withers07c07632011-07-14 23:21:45 +0000123 strncpy(new_dev->name, name, MDIO_NAME_LEN);
124 new_dev->name[MDIO_NAME_LEN - 1] = 0;
Andy Fleming5f184712011-04-08 02:10:27 -0500125 ldev->read = read;
126 ldev->write = write;
127 new_dev->priv = ldev;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200128
Andy Fleming16a53232011-04-07 14:38:35 -0500129 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
Andy Fleming5f184712011-04-08 02:10:27 -0500130 new_dev->name, ldev->read, ldev->write);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200131
132 /* add it to the list */
Andy Fleming16a53232011-04-07 14:38:35 -0500133 list_add_tail(&new_dev->link, &mii_devs);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200134
135 if (!current_mii)
136 current_mii = new_dev;
137}
138
Andy Fleming5f184712011-04-08 02:10:27 -0500139struct mii_dev *mdio_alloc(void)
140{
141 struct mii_dev *bus;
142
143 bus = malloc(sizeof(*bus));
144 if (!bus)
145 return bus;
146
147 memset(bus, 0, sizeof(*bus));
148
149 /* initalize mii_dev struct fields */
150 INIT_LIST_HEAD(&bus->link);
151
152 return bus;
153}
154
Bin Mengcb6baca2015-10-07 21:32:37 -0700155void mdio_free(struct mii_dev *bus)
156{
157 free(bus);
158}
159
Andy Fleming5f184712011-04-08 02:10:27 -0500160int mdio_register(struct mii_dev *bus)
161{
162 if (!bus || !bus->name || !bus->read || !bus->write)
163 return -1;
164
165 /* check if we have unique name */
166 if (miiphy_get_dev_by_name(bus->name)) {
167 printf("mdio_register: non unique device name '%s'\n",
168 bus->name);
169 return -1;
170 }
171
172 /* add it to the list */
173 list_add_tail(&bus->link, &mii_devs);
174
175 if (!current_mii)
176 current_mii = bus;
177
178 return 0;
179}
180
Bin Mengcb6baca2015-10-07 21:32:37 -0700181int mdio_unregister(struct mii_dev *bus)
182{
183 if (!bus)
184 return 0;
185
186 /* delete it from the list */
187 list_del(&bus->link);
188
189 if (current_mii == bus)
190 current_mii = NULL;
191
192 return 0;
193}
194
Andy Fleming5f184712011-04-08 02:10:27 -0500195void mdio_list_devices(void)
196{
197 struct list_head *entry;
198
199 list_for_each(entry, &mii_devs) {
200 int i;
201 struct mii_dev *bus = list_entry(entry, struct mii_dev, link);
202
203 printf("%s:\n", bus->name);
204
205 for (i = 0; i < PHY_MAX_ADDR; i++) {
206 struct phy_device *phydev = bus->phymap[i];
207
208 if (phydev) {
209 printf("%d - %s", i, phydev->drv->name);
210
211 if (phydev->dev)
212 printf(" <--> %s\n", phydev->dev->name);
213 else
214 printf("\n");
215 }
216 }
217 }
218}
219
Mike Frysinger5700bb62010-07-27 18:35:08 -0400220int miiphy_set_current_dev(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200221{
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200222 struct mii_dev *dev;
223
Andy Fleming5f184712011-04-08 02:10:27 -0500224 dev = miiphy_get_dev_by_name(devname);
Mike Frysinger0daac972010-07-27 18:35:09 -0400225 if (dev) {
226 current_mii = dev;
227 return 0;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200228 }
229
Andy Fleming5f184712011-04-08 02:10:27 -0500230 printf("No such device: %s\n", devname);
231
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200232 return 1;
233}
234
Andy Fleming5f184712011-04-08 02:10:27 -0500235struct mii_dev *mdio_get_current_dev(void)
236{
237 return current_mii;
238}
239
240struct phy_device *mdio_phydev_for_ethname(const char *ethname)
241{
242 struct list_head *entry;
243 struct mii_dev *bus;
244
245 list_for_each(entry, &mii_devs) {
246 int i;
247 bus = list_entry(entry, struct mii_dev, link);
248
249 for (i = 0; i < PHY_MAX_ADDR; i++) {
250 if (!bus->phymap[i] || !bus->phymap[i]->dev)
251 continue;
252
253 if (strcmp(bus->phymap[i]->dev->name, ethname) == 0)
254 return bus->phymap[i];
255 }
256 }
257
258 printf("%s is not a known ethernet\n", ethname);
259 return NULL;
260}
261
Mike Frysinger5700bb62010-07-27 18:35:08 -0400262const char *miiphy_get_current_dev(void)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200263{
264 if (current_mii)
265 return current_mii->name;
266
267 return NULL;
268}
269
Mike Frysingerede16ea2010-07-27 18:35:10 -0400270static struct mii_dev *miiphy_get_active_dev(const char *devname)
271{
272 /* If the current mii is the one we want, return it */
273 if (current_mii)
274 if (strcmp(current_mii->name, devname) == 0)
275 return current_mii;
276
277 /* Otherwise, set the active one to the one we want */
278 if (miiphy_set_current_dev(devname))
279 return NULL;
280 else
281 return current_mii;
282}
283
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200284/*****************************************************************************
285 *
286 * Read to variable <value> from the PHY attached to device <devname>,
287 * use PHY address <addr> and register <reg>.
288 *
Andy Fleming1cdabc42011-10-31 09:46:13 -0500289 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
290 *
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200291 * Returns:
292 * 0 on success
293 */
Wolfgang Denkf915c932011-12-07 08:35:14 +0100294int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
Larry Johnson298035d2007-10-31 11:21:29 -0500295 unsigned short *value)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200296{
Andy Fleming5f184712011-04-08 02:10:27 -0500297 struct mii_dev *bus;
Anatolij Gustschind67d5d52011-04-30 02:17:44 +0000298 int ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200299
Andy Fleming5f184712011-04-08 02:10:27 -0500300 bus = miiphy_get_active_dev(devname);
Anatolij Gustschind67d5d52011-04-30 02:17:44 +0000301 if (!bus)
Andy Fleming5f184712011-04-08 02:10:27 -0500302 return 1;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200303
Anatolij Gustschind67d5d52011-04-30 02:17:44 +0000304 ret = bus->read(bus, addr, MDIO_DEVAD_NONE, reg);
305 if (ret < 0)
306 return 1;
307
308 *value = (unsigned short)ret;
309 return 0;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200310}
311
312/*****************************************************************************
313 *
314 * Write <value> to the PHY attached to device <devname>,
315 * use PHY address <addr> and register <reg>.
316 *
Andy Fleming1cdabc42011-10-31 09:46:13 -0500317 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
318 *
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200319 * Returns:
320 * 0 on success
321 */
Wolfgang Denkf915c932011-12-07 08:35:14 +0100322int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
Larry Johnson298035d2007-10-31 11:21:29 -0500323 unsigned short value)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200324{
Andy Fleming5f184712011-04-08 02:10:27 -0500325 struct mii_dev *bus;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200326
Andy Fleming5f184712011-04-08 02:10:27 -0500327 bus = miiphy_get_active_dev(devname);
328 if (bus)
329 return bus->write(bus, addr, MDIO_DEVAD_NONE, reg, value);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200330
Mike Frysinger0daac972010-07-27 18:35:09 -0400331 return 1;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200332}
333
334/*****************************************************************************
335 *
336 * Print out list of registered MII capable devices.
337 */
Andy Fleming16a53232011-04-07 14:38:35 -0500338void miiphy_listdev(void)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200339{
340 struct list_head *entry;
341 struct mii_dev *dev;
342
Andy Fleming16a53232011-04-07 14:38:35 -0500343 puts("MII devices: ");
344 list_for_each(entry, &mii_devs) {
345 dev = list_entry(entry, struct mii_dev, link);
346 printf("'%s' ", dev->name);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200347 }
Andy Fleming16a53232011-04-07 14:38:35 -0500348 puts("\n");
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200349
350 if (current_mii)
Andy Fleming16a53232011-04-07 14:38:35 -0500351 printf("Current device: '%s'\n", current_mii->name);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200352}
353
wdenkc6097192002-11-03 00:24:07 +0000354/*****************************************************************************
355 *
356 * Read the OUI, manufacture's model number, and revision number.
357 *
358 * OUI: 22 bits (unsigned int)
359 * Model: 6 bits (unsigned char)
360 * Revision: 4 bits (unsigned char)
361 *
Andy Fleming1cdabc42011-10-31 09:46:13 -0500362 * This API is deprecated.
363 *
wdenkc6097192002-11-03 00:24:07 +0000364 * Returns:
365 * 0 on success
366 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400367int miiphy_info(const char *devname, unsigned char addr, unsigned int *oui,
wdenkc6097192002-11-03 00:24:07 +0000368 unsigned char *model, unsigned char *rev)
369{
370 unsigned int reg = 0;
wdenk8bf3b002003-12-06 23:20:41 +0000371 unsigned short tmp;
wdenkc6097192002-11-03 00:24:07 +0000372
Andy Fleming16a53232011-04-07 14:38:35 -0500373 if (miiphy_read(devname, addr, MII_PHYSID2, &tmp) != 0) {
374 debug("PHY ID register 2 read failed\n");
375 return -1;
wdenkc6097192002-11-03 00:24:07 +0000376 }
wdenk8bf3b002003-12-06 23:20:41 +0000377 reg = tmp;
wdenkc6097192002-11-03 00:24:07 +0000378
Andy Fleming16a53232011-04-07 14:38:35 -0500379 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
Shinya Kuribayashi26c7bab2008-01-19 10:25:59 +0900380
wdenkc6097192002-11-03 00:24:07 +0000381 if (reg == 0xFFFF) {
382 /* No physical device present at this address */
Andy Fleming16a53232011-04-07 14:38:35 -0500383 return -1;
wdenkc6097192002-11-03 00:24:07 +0000384 }
385
Andy Fleming16a53232011-04-07 14:38:35 -0500386 if (miiphy_read(devname, addr, MII_PHYSID1, &tmp) != 0) {
387 debug("PHY ID register 1 read failed\n");
388 return -1;
wdenkc6097192002-11-03 00:24:07 +0000389 }
wdenk8bf3b002003-12-06 23:20:41 +0000390 reg |= tmp << 16;
Andy Fleming16a53232011-04-07 14:38:35 -0500391 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
Shinya Kuribayashi26c7bab2008-01-19 10:25:59 +0900392
Larry Johnson298035d2007-10-31 11:21:29 -0500393 *oui = (reg >> 10);
394 *model = (unsigned char)((reg >> 4) & 0x0000003F);
395 *rev = (unsigned char)(reg & 0x0000000F);
Andy Fleming16a53232011-04-07 14:38:35 -0500396 return 0;
wdenkc6097192002-11-03 00:24:07 +0000397}
398
Andy Fleming5f184712011-04-08 02:10:27 -0500399#ifndef CONFIG_PHYLIB
wdenkc6097192002-11-03 00:24:07 +0000400/*****************************************************************************
401 *
402 * Reset the PHY.
Andy Fleming1cdabc42011-10-31 09:46:13 -0500403 *
404 * This API is deprecated. Use PHYLIB.
405 *
wdenkc6097192002-11-03 00:24:07 +0000406 * Returns:
407 * 0 on success
408 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400409int miiphy_reset(const char *devname, unsigned char addr)
wdenkc6097192002-11-03 00:24:07 +0000410{
411 unsigned short reg;
Stefan Roeseab5a0dc2010-02-02 13:43:48 +0100412 int timeout = 500;
wdenkc6097192002-11-03 00:24:07 +0000413
Andy Fleming16a53232011-04-07 14:38:35 -0500414 if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
415 debug("PHY status read failed\n");
416 return -1;
Wolfgang Denkf89920c2005-08-12 23:15:53 +0200417 }
Andy Fleming16a53232011-04-07 14:38:35 -0500418 if (miiphy_write(devname, addr, MII_BMCR, reg | BMCR_RESET) != 0) {
419 debug("PHY reset failed\n");
420 return -1;
wdenkc6097192002-11-03 00:24:07 +0000421 }
wdenk5653fc32004-02-08 22:55:38 +0000422#ifdef CONFIG_PHY_RESET_DELAY
Andy Fleming16a53232011-04-07 14:38:35 -0500423 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
wdenk5653fc32004-02-08 22:55:38 +0000424#endif
wdenkc6097192002-11-03 00:24:07 +0000425 /*
426 * Poll the control register for the reset bit to go to 0 (it is
427 * auto-clearing). This should happen within 0.5 seconds per the
428 * IEEE spec.
429 */
wdenkc6097192002-11-03 00:24:07 +0000430 reg = 0x8000;
Stefan Roeseab5a0dc2010-02-02 13:43:48 +0100431 while (((reg & 0x8000) != 0) && timeout--) {
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500432 if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
Stefan Roeseab5a0dc2010-02-02 13:43:48 +0100433 debug("PHY status read failed\n");
434 return -1;
wdenkc6097192002-11-03 00:24:07 +0000435 }
Stefan Roeseab5a0dc2010-02-02 13:43:48 +0100436 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +0000437 }
438 if ((reg & 0x8000) == 0) {
Andy Fleming16a53232011-04-07 14:38:35 -0500439 return 0;
wdenkc6097192002-11-03 00:24:07 +0000440 } else {
Andy Fleming16a53232011-04-07 14:38:35 -0500441 puts("PHY reset timed out\n");
442 return -1;
wdenkc6097192002-11-03 00:24:07 +0000443 }
Andy Fleming16a53232011-04-07 14:38:35 -0500444 return 0;
wdenkc6097192002-11-03 00:24:07 +0000445}
Andy Fleming5f184712011-04-08 02:10:27 -0500446#endif /* !PHYLIB */
wdenkc6097192002-11-03 00:24:07 +0000447
wdenkc6097192002-11-03 00:24:07 +0000448/*****************************************************************************
449 *
Larry Johnson71bc6e62007-11-01 08:46:50 -0500450 * Determine the ethernet speed (10/100/1000). Return 10 on error.
wdenkc6097192002-11-03 00:24:07 +0000451 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400452int miiphy_speed(const char *devname, unsigned char addr)
wdenkc6097192002-11-03 00:24:07 +0000453{
Larry Johnson71bc6e62007-11-01 08:46:50 -0500454 u16 bmcr, anlpar;
wdenkc6097192002-11-03 00:24:07 +0000455
wdenk6fb6af62004-03-23 23:20:24 +0000456#if defined(CONFIG_PHY_GIGE)
Larry Johnson71bc6e62007-11-01 08:46:50 -0500457 u16 btsr;
458
459 /*
460 * Check for 1000BASE-X. If it is supported, then assume that the speed
461 * is 1000.
462 */
Andy Fleming16a53232011-04-07 14:38:35 -0500463 if (miiphy_is_1000base_x(devname, addr))
Larry Johnson71bc6e62007-11-01 08:46:50 -0500464 return _1000BASET;
Andy Fleming16a53232011-04-07 14:38:35 -0500465
Larry Johnson71bc6e62007-11-01 08:46:50 -0500466 /*
467 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
468 */
469 /* Check for 1000BASE-T. */
Andy Fleming16a53232011-04-07 14:38:35 -0500470 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
471 printf("PHY 1000BT status");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500472 goto miiphy_read_failed;
473 }
474 if (btsr != 0xFFFF &&
Andy Fleming16a53232011-04-07 14:38:35 -0500475 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
Larry Johnson71bc6e62007-11-01 08:46:50 -0500476 return _1000BASET;
wdenk6fb6af62004-03-23 23:20:24 +0000477#endif /* CONFIG_PHY_GIGE */
wdenk855a4962004-03-14 18:23:55 +0000478
wdenka56bd922004-06-06 23:13:55 +0000479 /* Check Basic Management Control Register first. */
Andy Fleming16a53232011-04-07 14:38:35 -0500480 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
481 printf("PHY speed");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500482 goto miiphy_read_failed;
wdenkc6097192002-11-03 00:24:07 +0000483 }
wdenka56bd922004-06-06 23:13:55 +0000484 /* Check if auto-negotiation is on. */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500485 if (bmcr & BMCR_ANENABLE) {
wdenka56bd922004-06-06 23:13:55 +0000486 /* Get auto-negotiation results. */
Andy Fleming16a53232011-04-07 14:38:35 -0500487 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
488 printf("PHY AN speed");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500489 goto miiphy_read_failed;
wdenka56bd922004-06-06 23:13:55 +0000490 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500491 return (anlpar & LPA_100) ? _100BASET : _10BASET;
wdenka56bd922004-06-06 23:13:55 +0000492 }
493 /* Get speed from basic control settings. */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500494 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
wdenka56bd922004-06-06 23:13:55 +0000495
Michael Zaidman5f841952010-02-28 16:28:25 +0200496miiphy_read_failed:
Andy Fleming16a53232011-04-07 14:38:35 -0500497 printf(" read failed, assuming 10BASE-T\n");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500498 return _10BASET;
wdenkc6097192002-11-03 00:24:07 +0000499}
500
wdenkc6097192002-11-03 00:24:07 +0000501/*****************************************************************************
502 *
Larry Johnson71bc6e62007-11-01 08:46:50 -0500503 * Determine full/half duplex. Return half on error.
wdenkc6097192002-11-03 00:24:07 +0000504 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400505int miiphy_duplex(const char *devname, unsigned char addr)
wdenkc6097192002-11-03 00:24:07 +0000506{
Larry Johnson71bc6e62007-11-01 08:46:50 -0500507 u16 bmcr, anlpar;
wdenkc6097192002-11-03 00:24:07 +0000508
wdenk6fb6af62004-03-23 23:20:24 +0000509#if defined(CONFIG_PHY_GIGE)
Larry Johnson71bc6e62007-11-01 08:46:50 -0500510 u16 btsr;
511
512 /* Check for 1000BASE-X. */
Andy Fleming16a53232011-04-07 14:38:35 -0500513 if (miiphy_is_1000base_x(devname, addr)) {
Larry Johnson71bc6e62007-11-01 08:46:50 -0500514 /* 1000BASE-X */
Andy Fleming16a53232011-04-07 14:38:35 -0500515 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
516 printf("1000BASE-X PHY AN duplex");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500517 goto miiphy_read_failed;
518 }
519 }
520 /*
521 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
522 */
523 /* Check for 1000BASE-T. */
Andy Fleming16a53232011-04-07 14:38:35 -0500524 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
525 printf("PHY 1000BT status");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500526 goto miiphy_read_failed;
527 }
528 if (btsr != 0xFFFF) {
529 if (btsr & PHY_1000BTSR_1000FD) {
530 return FULL;
531 } else if (btsr & PHY_1000BTSR_1000HD) {
532 return HALF;
wdenk855a4962004-03-14 18:23:55 +0000533 }
534 }
wdenk6fb6af62004-03-23 23:20:24 +0000535#endif /* CONFIG_PHY_GIGE */
wdenk855a4962004-03-14 18:23:55 +0000536
wdenka56bd922004-06-06 23:13:55 +0000537 /* Check Basic Management Control Register first. */
Andy Fleming16a53232011-04-07 14:38:35 -0500538 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
539 puts("PHY duplex");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500540 goto miiphy_read_failed;
wdenkc6097192002-11-03 00:24:07 +0000541 }
wdenka56bd922004-06-06 23:13:55 +0000542 /* Check if auto-negotiation is on. */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500543 if (bmcr & BMCR_ANENABLE) {
wdenka56bd922004-06-06 23:13:55 +0000544 /* Get auto-negotiation results. */
Andy Fleming16a53232011-04-07 14:38:35 -0500545 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
546 puts("PHY AN duplex");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500547 goto miiphy_read_failed;
wdenka56bd922004-06-06 23:13:55 +0000548 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500549 return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
Larry Johnson71bc6e62007-11-01 08:46:50 -0500550 FULL : HALF;
wdenka56bd922004-06-06 23:13:55 +0000551 }
552 /* Get speed from basic control settings. */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500553 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
wdenka56bd922004-06-06 23:13:55 +0000554
Michael Zaidman5f841952010-02-28 16:28:25 +0200555miiphy_read_failed:
Andy Fleming16a53232011-04-07 14:38:35 -0500556 printf(" read failed, assuming half duplex\n");
Larry Johnson71bc6e62007-11-01 08:46:50 -0500557 return HALF;
558}
559
560/*****************************************************************************
561 *
562 * Return 1 if PHY supports 1000BASE-X, 0 if PHY supports 10BASE-T/100BASE-TX/
563 * 1000BASE-T, or on error.
564 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400565int miiphy_is_1000base_x(const char *devname, unsigned char addr)
Larry Johnson71bc6e62007-11-01 08:46:50 -0500566{
567#if defined(CONFIG_PHY_GIGE)
568 u16 exsr;
569
Andy Fleming16a53232011-04-07 14:38:35 -0500570 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
571 printf("PHY extended status read failed, assuming no "
Larry Johnson71bc6e62007-11-01 08:46:50 -0500572 "1000BASE-X\n");
573 return 0;
574 }
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500575 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
Larry Johnson71bc6e62007-11-01 08:46:50 -0500576#else
577 return 0;
578#endif
wdenkc6097192002-11-03 00:24:07 +0000579}
580
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200581#ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
wdenkfc3e2162003-10-08 22:33:00 +0000582/*****************************************************************************
583 *
584 * Determine link status
585 */
Mike Frysinger5700bb62010-07-27 18:35:08 -0400586int miiphy_link(const char *devname, unsigned char addr)
wdenkfc3e2162003-10-08 22:33:00 +0000587{
588 unsigned short reg;
589
wdenka3d991b2004-04-15 21:48:45 +0000590 /* dummy read; needed to latch some phys */
Andy Fleming16a53232011-04-07 14:38:35 -0500591 (void)miiphy_read(devname, addr, MII_BMSR, &reg);
592 if (miiphy_read(devname, addr, MII_BMSR, &reg)) {
593 puts("MII_BMSR read failed, assuming no link\n");
594 return 0;
wdenkfc3e2162003-10-08 22:33:00 +0000595 }
596
597 /* Determine if a link is active */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500598 if ((reg & BMSR_LSTATUS) != 0) {
Andy Fleming16a53232011-04-07 14:38:35 -0500599 return 1;
wdenkfc3e2162003-10-08 22:33:00 +0000600 } else {
Andy Fleming16a53232011-04-07 14:38:35 -0500601 return 0;
wdenkfc3e2162003-10-08 22:33:00 +0000602 }
603}
604#endif