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 Denkd79de1d2013-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 Glassdbad3462015-04-05 16:07:39 -060014#include <dm.h>
wdenkc6097192002-11-03 00:24:07 +000015#include <miiphy.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050016#include <phy.h>
wdenkc6097192002-11-03 00:24:07 +000017
Marian Balakowiczaab8c492005-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 Balakowiczaab8c492005-10-28 22:30:33 +020024#undef MII_DEBUG
25
26#undef debug
27#ifdef MII_DEBUG
Andy Flemingaea0c3e2011-04-07 14:38:35 -050028#define debug(fmt, args...) printf(fmt, ##args)
Marian Balakowiczaab8c492005-10-28 22:30:33 +020029#else
Andy Flemingaea0c3e2011-04-07 14:38:35 -050030#define debug(fmt, args...)
Marian Balakowiczaab8c492005-10-28 22:30:33 +020031#endif /* MII_DEBUG */
32
Marian Balakowiczaab8c492005-10-28 22:30:33 +020033static struct list_head mii_devs;
34static struct mii_dev *current_mii;
35
Mike Frysinger24a90082010-07-27 18:35:09 -040036/*
37 * Lookup the mii_dev struct by the registered device name.
38 */
Andy Flemingaecf6fc2011-04-08 02:10:27 -050039struct mii_dev *miiphy_get_dev_by_name(const char *devname)
Mike Frysinger24a90082010-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 Frysinger24a90082010-07-27 18:35:09 -040055 return NULL;
56}
57
Marian Balakowiczaab8c492005-10-28 22:30:33 +020058/*****************************************************************************
59 *
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +010060 * Initialize global data. Need to be called before any other miiphy routine.
61 */
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -040062void miiphy_init(void)
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +010063{
Andy Flemingaea0c3e2011-04-07 14:38:35 -050064 INIT_LIST_HEAD(&mii_devs);
Larry Johnson81b974b2007-10-31 11:21:29 -050065 current_mii = NULL;
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +010066}
67
Andy Flemingaecf6fc2011-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 Balakowiczcbdd1c82005-11-30 18:06:04 +010087/*****************************************************************************
88 *
Marian Balakowiczaab8c492005-10-28 22:30:33 +020089 * Register read and write MII access routines for the device <name>.
Andy Fleming896a7172011-10-31 09:46:13 -050090 * This API is now deprecated. Please use mdio_alloc and mdio_register, instead.
Marian Balakowiczaab8c492005-10-28 22:30:33 +020091 */
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -040092void miiphy_register(const char *name,
Andy Flemingaea0c3e2011-04-07 14:38:35 -050093 int (*read)(const char *devname, unsigned char addr,
Wolfgang Denk934fcb62011-12-07 08:35:14 +010094 unsigned char reg, unsigned short *value),
Andy Flemingaea0c3e2011-04-07 14:38:35 -050095 int (*write)(const char *devname, unsigned char addr,
Wolfgang Denk934fcb62011-12-07 08:35:14 +010096 unsigned char reg, unsigned short value))
Marian Balakowiczaab8c492005-10-28 22:30:33 +020097{
Marian Balakowiczaab8c492005-10-28 22:30:33 +020098 struct mii_dev *new_dev;
Andy Flemingaecf6fc2011-04-08 02:10:27 -050099 struct legacy_mii_dev *ldev;
Laurence Withersb69e3372011-07-14 23:21:45 +0000100
101 BUG_ON(strlen(name) >= MDIO_NAME_LEN);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200102
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200103 /* check if we have unique name */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500104 new_dev = miiphy_get_dev_by_name(name);
Mike Frysinger24a90082010-07-27 18:35:09 -0400105 if (new_dev) {
106 printf("miiphy_register: non unique device name '%s'\n", name);
107 return;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200108 }
109
110 /* allocate memory */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500111 new_dev = mdio_alloc();
112 ldev = malloc(sizeof(*ldev));
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200113
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500114 if (new_dev == NULL || ldev == NULL) {
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500115 printf("miiphy_register: cannot allocate memory for '%s'\n",
Larry Johnson81b974b2007-10-31 11:21:29 -0500116 name);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200117 return;
118 }
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200119
120 /* initalize mii_dev struct fields */
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500121 new_dev->read = legacy_miiphy_read;
122 new_dev->write = legacy_miiphy_write;
Laurence Withersb69e3372011-07-14 23:21:45 +0000123 strncpy(new_dev->name, name, MDIO_NAME_LEN);
124 new_dev->name[MDIO_NAME_LEN - 1] = 0;
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500125 ldev->read = read;
126 ldev->write = write;
127 new_dev->priv = ldev;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200128
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500129 debug("miiphy_register: added '%s', read=0x%08lx, write=0x%08lx\n",
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500130 new_dev->name, ldev->read, ldev->write);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200131
132 /* add it to the list */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500133 list_add_tail(&new_dev->link, &mii_devs);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200134
135 if (!current_mii)
136 current_mii = new_dev;
137}
138
Andy Flemingaecf6fc2011-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 Menga961e1f2015-10-07 21:32:37 -0700155void mdio_free(struct mii_dev *bus)
156{
157 free(bus);
158}
159
Andy Flemingaecf6fc2011-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 Menga961e1f2015-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 Flemingaecf6fc2011-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 Frysinger5ff5fdb2010-07-27 18:35:08 -0400220int miiphy_set_current_dev(const char *devname)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200221{
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200222 struct mii_dev *dev;
223
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500224 dev = miiphy_get_dev_by_name(devname);
Mike Frysinger24a90082010-07-27 18:35:09 -0400225 if (dev) {
226 current_mii = dev;
227 return 0;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200228 }
229
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500230 printf("No such device: %s\n", devname);
231
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200232 return 1;
233}
234
Andy Flemingaecf6fc2011-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 Frysinger5ff5fdb2010-07-27 18:35:08 -0400262const char *miiphy_get_current_dev(void)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200263{
264 if (current_mii)
265 return current_mii->name;
266
267 return NULL;
268}
269
Mike Frysingerbd17e7a2010-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 Balakowiczaab8c492005-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 Fleming896a7172011-10-31 09:46:13 -0500289 * This API is deprecated. Use phy_read on a phy_device found via phy_connect
290 *
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200291 * Returns:
292 * 0 on success
293 */
Wolfgang Denk934fcb62011-12-07 08:35:14 +0100294int miiphy_read(const char *devname, unsigned char addr, unsigned char reg,
Larry Johnson81b974b2007-10-31 11:21:29 -0500295 unsigned short *value)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200296{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500297 struct mii_dev *bus;
Anatolij Gustschin1bbce3a2011-04-30 02:17:44 +0000298 int ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200299
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500300 bus = miiphy_get_active_dev(devname);
Anatolij Gustschin1bbce3a2011-04-30 02:17:44 +0000301 if (!bus)
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500302 return 1;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200303
Anatolij Gustschin1bbce3a2011-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 Balakowiczaab8c492005-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 Fleming896a7172011-10-31 09:46:13 -0500317 * This API is deprecated. Use phy_write on a phy_device found by phy_connect
318 *
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200319 * Returns:
320 * 0 on success
321 */
Wolfgang Denk934fcb62011-12-07 08:35:14 +0100322int miiphy_write(const char *devname, unsigned char addr, unsigned char reg,
Larry Johnson81b974b2007-10-31 11:21:29 -0500323 unsigned short value)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200324{
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500325 struct mii_dev *bus;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200326
Andy Flemingaecf6fc2011-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 Balakowiczaab8c492005-10-28 22:30:33 +0200330
Mike Frysinger24a90082010-07-27 18:35:09 -0400331 return 1;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200332}
333
334/*****************************************************************************
335 *
336 * Print out list of registered MII capable devices.
337 */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500338void miiphy_listdev(void)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200339{
340 struct list_head *entry;
341 struct mii_dev *dev;
342
Andy Flemingaea0c3e2011-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 Balakowiczaab8c492005-10-28 22:30:33 +0200347 }
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500348 puts("\n");
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200349
350 if (current_mii)
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500351 printf("Current device: '%s'\n", current_mii->name);
Marian Balakowiczaab8c492005-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 Fleming896a7172011-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 Frysinger5ff5fdb2010-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;
wdenkf4cec3f2003-12-06 23:20:41 +0000371 unsigned short tmp;
wdenkc6097192002-11-03 00:24:07 +0000372
Andy Flemingaea0c3e2011-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 }
wdenkf4cec3f2003-12-06 23:20:41 +0000377 reg = tmp;
wdenkc6097192002-11-03 00:24:07 +0000378
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500379 debug("MII_PHYSID2 @ 0x%x = 0x%04x\n", addr, reg);
Shinya Kuribayashi49e42af2008-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 Flemingaea0c3e2011-04-07 14:38:35 -0500383 return -1;
wdenkc6097192002-11-03 00:24:07 +0000384 }
385
Andy Flemingaea0c3e2011-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 }
wdenkf4cec3f2003-12-06 23:20:41 +0000390 reg |= tmp << 16;
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500391 debug("PHY_PHYIDR[1,2] @ 0x%x = 0x%08x\n", addr, reg);
Shinya Kuribayashi49e42af2008-01-19 10:25:59 +0900392
Larry Johnson81b974b2007-10-31 11:21:29 -0500393 *oui = (reg >> 10);
394 *model = (unsigned char)((reg >> 4) & 0x0000003F);
395 *rev = (unsigned char)(reg & 0x0000000F);
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500396 return 0;
wdenkc6097192002-11-03 00:24:07 +0000397}
398
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500399#ifndef CONFIG_PHYLIB
wdenkc6097192002-11-03 00:24:07 +0000400/*****************************************************************************
401 *
402 * Reset the PHY.
Andy Fleming896a7172011-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 Frysinger5ff5fdb2010-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 Roese2e536362010-02-02 13:43:48 +0100412 int timeout = 500;
wdenkc6097192002-11-03 00:24:07 +0000413
Andy Flemingaea0c3e2011-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 Denk8ff63c22005-08-12 23:15:53 +0200417 }
Andy Flemingaea0c3e2011-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 }
wdenk2cefd152004-02-08 22:55:38 +0000422#ifdef CONFIG_PHY_RESET_DELAY
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500423 udelay(CONFIG_PHY_RESET_DELAY); /* Intel LXT971A needs this */
wdenk2cefd152004-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 Roese2e536362010-02-02 13:43:48 +0100431 while (((reg & 0x8000) != 0) && timeout--) {
Mike Frysingerd63ee712010-12-23 15:40:12 -0500432 if (miiphy_read(devname, addr, MII_BMCR, &reg) != 0) {
Stefan Roese2e536362010-02-02 13:43:48 +0100433 debug("PHY status read failed\n");
434 return -1;
wdenkc6097192002-11-03 00:24:07 +0000435 }
Stefan Roese2e536362010-02-02 13:43:48 +0100436 udelay(1000);
wdenkc6097192002-11-03 00:24:07 +0000437 }
438 if ((reg & 0x8000) == 0) {
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500439 return 0;
wdenkc6097192002-11-03 00:24:07 +0000440 } else {
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500441 puts("PHY reset timed out\n");
442 return -1;
wdenkc6097192002-11-03 00:24:07 +0000443 }
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500444 return 0;
wdenkc6097192002-11-03 00:24:07 +0000445}
Andy Flemingaecf6fc2011-04-08 02:10:27 -0500446#endif /* !PHYLIB */
wdenkc6097192002-11-03 00:24:07 +0000447
wdenkc6097192002-11-03 00:24:07 +0000448/*****************************************************************************
449 *
Larry Johnson966a80b2007-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 Frysinger5ff5fdb2010-07-27 18:35:08 -0400452int miiphy_speed(const char *devname, unsigned char addr)
wdenkc6097192002-11-03 00:24:07 +0000453{
Larry Johnson966a80b2007-11-01 08:46:50 -0500454 u16 bmcr, anlpar;
wdenkc6097192002-11-03 00:24:07 +0000455
wdenkeec9a3d2004-03-23 23:20:24 +0000456#if defined(CONFIG_PHY_GIGE)
Larry Johnson966a80b2007-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 Flemingaea0c3e2011-04-07 14:38:35 -0500463 if (miiphy_is_1000base_x(devname, addr))
Larry Johnson966a80b2007-11-01 08:46:50 -0500464 return _1000BASET;
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500465
Larry Johnson966a80b2007-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 Flemingaea0c3e2011-04-07 14:38:35 -0500470 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
471 printf("PHY 1000BT status");
Larry Johnson966a80b2007-11-01 08:46:50 -0500472 goto miiphy_read_failed;
473 }
474 if (btsr != 0xFFFF &&
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500475 (btsr & (PHY_1000BTSR_1000FD | PHY_1000BTSR_1000HD)))
Larry Johnson966a80b2007-11-01 08:46:50 -0500476 return _1000BASET;
wdenkeec9a3d2004-03-23 23:20:24 +0000477#endif /* CONFIG_PHY_GIGE */
wdenked2ac4b2004-03-14 18:23:55 +0000478
wdenke3a06802004-06-06 23:13:55 +0000479 /* Check Basic Management Control Register first. */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500480 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
481 printf("PHY speed");
Larry Johnson966a80b2007-11-01 08:46:50 -0500482 goto miiphy_read_failed;
wdenkc6097192002-11-03 00:24:07 +0000483 }
wdenke3a06802004-06-06 23:13:55 +0000484 /* Check if auto-negotiation is on. */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500485 if (bmcr & BMCR_ANENABLE) {
wdenke3a06802004-06-06 23:13:55 +0000486 /* Get auto-negotiation results. */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500487 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
488 printf("PHY AN speed");
Larry Johnson966a80b2007-11-01 08:46:50 -0500489 goto miiphy_read_failed;
wdenke3a06802004-06-06 23:13:55 +0000490 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500491 return (anlpar & LPA_100) ? _100BASET : _10BASET;
wdenke3a06802004-06-06 23:13:55 +0000492 }
493 /* Get speed from basic control settings. */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500494 return (bmcr & BMCR_SPEED100) ? _100BASET : _10BASET;
wdenke3a06802004-06-06 23:13:55 +0000495
Michael Zaidman6dbca5f2010-02-28 16:28:25 +0200496miiphy_read_failed:
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500497 printf(" read failed, assuming 10BASE-T\n");
Larry Johnson966a80b2007-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 Johnson966a80b2007-11-01 08:46:50 -0500503 * Determine full/half duplex. Return half on error.
wdenkc6097192002-11-03 00:24:07 +0000504 */
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -0400505int miiphy_duplex(const char *devname, unsigned char addr)
wdenkc6097192002-11-03 00:24:07 +0000506{
Larry Johnson966a80b2007-11-01 08:46:50 -0500507 u16 bmcr, anlpar;
wdenkc6097192002-11-03 00:24:07 +0000508
wdenkeec9a3d2004-03-23 23:20:24 +0000509#if defined(CONFIG_PHY_GIGE)
Larry Johnson966a80b2007-11-01 08:46:50 -0500510 u16 btsr;
511
512 /* Check for 1000BASE-X. */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500513 if (miiphy_is_1000base_x(devname, addr)) {
Larry Johnson966a80b2007-11-01 08:46:50 -0500514 /* 1000BASE-X */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500515 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
516 printf("1000BASE-X PHY AN duplex");
Larry Johnson966a80b2007-11-01 08:46:50 -0500517 goto miiphy_read_failed;
wdenked2ac4b2004-03-14 18:23:55 +0000518 }
519 }
Larry Johnson966a80b2007-11-01 08:46:50 -0500520 /*
521 * No 1000BASE-X, so assume 1000BASE-T/100BASE-TX/10BASE-T register set.
522 */
523 /* Check for 1000BASE-T. */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500524 if (miiphy_read(devname, addr, MII_STAT1000, &btsr)) {
525 printf("PHY 1000BT status");
Larry Johnson966a80b2007-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;
533 }
534 }
wdenkeec9a3d2004-03-23 23:20:24 +0000535#endif /* CONFIG_PHY_GIGE */
wdenked2ac4b2004-03-14 18:23:55 +0000536
wdenke3a06802004-06-06 23:13:55 +0000537 /* Check Basic Management Control Register first. */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500538 if (miiphy_read(devname, addr, MII_BMCR, &bmcr)) {
539 puts("PHY duplex");
Larry Johnson966a80b2007-11-01 08:46:50 -0500540 goto miiphy_read_failed;
wdenkc6097192002-11-03 00:24:07 +0000541 }
wdenke3a06802004-06-06 23:13:55 +0000542 /* Check if auto-negotiation is on. */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500543 if (bmcr & BMCR_ANENABLE) {
wdenke3a06802004-06-06 23:13:55 +0000544 /* Get auto-negotiation results. */
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500545 if (miiphy_read(devname, addr, MII_LPA, &anlpar)) {
546 puts("PHY AN duplex");
Larry Johnson966a80b2007-11-01 08:46:50 -0500547 goto miiphy_read_failed;
wdenke3a06802004-06-06 23:13:55 +0000548 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500549 return (anlpar & (LPA_10FULL | LPA_100FULL)) ?
Larry Johnson966a80b2007-11-01 08:46:50 -0500550 FULL : HALF;
wdenke3a06802004-06-06 23:13:55 +0000551 }
552 /* Get speed from basic control settings. */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500553 return (bmcr & BMCR_FULLDPLX) ? FULL : HALF;
Larry Johnson966a80b2007-11-01 08:46:50 -0500554
Michael Zaidman6dbca5f2010-02-28 16:28:25 +0200555miiphy_read_failed:
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500556 printf(" read failed, assuming half duplex\n");
Larry Johnson966a80b2007-11-01 08:46:50 -0500557 return HALF;
558}
wdenke3a06802004-06-06 23:13:55 +0000559
Larry Johnson966a80b2007-11-01 08:46:50 -0500560/*****************************************************************************
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 Frysinger5ff5fdb2010-07-27 18:35:08 -0400565int miiphy_is_1000base_x(const char *devname, unsigned char addr)
Larry Johnson966a80b2007-11-01 08:46:50 -0500566{
567#if defined(CONFIG_PHY_GIGE)
568 u16 exsr;
569
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500570 if (miiphy_read(devname, addr, MII_ESTATUS, &exsr)) {
571 printf("PHY extended status read failed, assuming no "
Larry Johnson966a80b2007-11-01 08:46:50 -0500572 "1000BASE-X\n");
573 return 0;
574 }
Mike Frysingerd63ee712010-12-23 15:40:12 -0500575 return 0 != (exsr & (ESTATUS_1000XF | ESTATUS_1000XH));
Larry Johnson966a80b2007-11-01 08:46:50 -0500576#else
577 return 0;
578#endif
wdenkc6097192002-11-03 00:24:07 +0000579}
580
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200581#ifdef CONFIG_SYS_FAULT_ECHO_LINK_DOWN
wdenk49c3f672003-10-08 22:33:00 +0000582/*****************************************************************************
583 *
584 * Determine link status
585 */
Mike Frysinger5ff5fdb2010-07-27 18:35:08 -0400586int miiphy_link(const char *devname, unsigned char addr)
wdenk49c3f672003-10-08 22:33:00 +0000587{
588 unsigned short reg;
589
wdenk145d2c12004-04-15 21:48:45 +0000590 /* dummy read; needed to latch some phys */
Andy Flemingaea0c3e2011-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;
wdenk49c3f672003-10-08 22:33:00 +0000595 }
596
597 /* Determine if a link is active */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500598 if ((reg & BMSR_LSTATUS) != 0) {
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500599 return 1;
wdenk49c3f672003-10-08 22:33:00 +0000600 } else {
Andy Flemingaea0c3e2011-04-07 14:38:35 -0500601 return 0;
wdenk49c3f672003-10-08 22:33:00 +0000602 }
603}
604#endif