blob: c46a8c30500130bff53f4766d15724500c94bfdb [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
Joe Hershberger05c3e682015-03-22 17:09:10 -05002 * (C) Copyright 2001-2015
wdenkc6097192002-11-03 00:24:07 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Joe Hershberger05c3e682015-03-22 17:09:10 -05004 * Joe Hershberger, National Instruments
wdenkc6097192002-11-03 00:24:07 +00005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00007 */
8
9#include <common.h>
10#include <command.h>
Joe Hershberger05c3e682015-03-22 17:09:10 -050011#include <dm.h>
Joe Hershberger6e0d26c2015-05-20 14:27:26 -050012#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000013#include <net.h>
Marian Balakowiczd9785c12005-11-30 18:06:04 +010014#include <miiphy.h>
Andy Fleming5f184712011-04-08 02:10:27 -050015#include <phy.h>
Pavel Machek75d9a452014-07-13 10:27:02 +020016#include <asm/errno.h>
Joe Hershberger05c3e682015-03-22 17:09:10 -050017#include <dm/device-internal.h>
Joe Hershberger6e0d26c2015-05-20 14:27:26 -050018#include <dm/uclass-internal.h>
wdenkc6097192002-11-03 00:24:07 +000019
Joe Hershbergerd2eaec62015-03-22 17:09:06 -050020DECLARE_GLOBAL_DATA_PTR;
21
Mike Frysinger3f6e6992009-01-29 19:43:44 -050022void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
23{
24 char *end;
25 int i;
26
27 for (i = 0; i < 6; ++i) {
28 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
29 if (addr)
30 addr = (*end) ? end + 1 : end;
31 }
32}
33
34int eth_getenv_enetaddr(char *name, uchar *enetaddr)
35{
36 eth_parse_enetaddr(getenv(name), enetaddr);
Joe Hershberger0adb5b72015-04-08 01:41:04 -050037 return is_valid_ethaddr(enetaddr);
Mike Frysinger3f6e6992009-01-29 19:43:44 -050038}
39
40int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
41{
42 char buf[20];
43
44 sprintf(buf, "%pM", enetaddr);
45
46 return setenv(name, buf);
47}
Mike Frysinger86848a72009-07-15 21:31:28 -040048
Simon Glass7616e782011-06-13 16:13:10 -070049int eth_getenv_enetaddr_by_index(const char *base_name, int index,
50 uchar *enetaddr)
Mike Frysinger86848a72009-07-15 21:31:28 -040051{
52 char enetvar[32];
Simon Glass7616e782011-06-13 16:13:10 -070053 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Mike Frysinger86848a72009-07-15 21:31:28 -040054 return eth_getenv_enetaddr(enetvar, enetaddr);
55}
Mike Frysinger3f6e6992009-01-29 19:43:44 -050056
Joe Hershberger154177e2012-07-10 16:23:22 -050057static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
Rob Herringc88ef3c2012-04-14 18:06:49 +000058 uchar *enetaddr)
59{
60 char enetvar[32];
61 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
62 return eth_setenv_enetaddr(enetvar, enetaddr);
63}
64
Ben Warrenecee9322010-04-26 11:11:46 -070065static int eth_mac_skip(int index)
66{
67 char enetvar[15];
68 char *skip_state;
Joe Hershbergerff819a32015-04-08 01:41:19 -050069
Ben Warrenecee9322010-04-26 11:11:46 -070070 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
Joe Hershbergerff819a32015-04-08 01:41:19 -050071 skip_state = getenv(enetvar);
72 return skip_state != NULL;
Ben Warrenecee9322010-04-26 11:11:46 -070073}
74
Joe Hershberger84eb1fb2015-03-22 17:09:03 -050075static void eth_current_changed(void);
76
Simon Glass3bc42702015-04-05 16:07:37 -060077/*
78 * CPU and board-specific Ethernet initializations. Aliased function
79 * signals caller to move on
80 */
81static int __def_eth_init(bd_t *bis)
82{
83 return -1;
84}
85int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
86int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
87
88static void eth_common_init(void)
89{
90 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
91#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
92 miiphy_init();
93#endif
94
95#ifdef CONFIG_PHYLIB
96 phy_init();
97#endif
98
Simon Glass3bc42702015-04-05 16:07:37 -060099 /*
100 * If board-specific initialization exists, call it.
101 * If not, call a CPU-specific one
102 */
103 if (board_eth_init != __def_eth_init) {
104 if (board_eth_init(gd->bd) < 0)
105 printf("Board Net Initialization Failed\n");
106 } else if (cpu_eth_init != __def_eth_init) {
107 if (cpu_eth_init(gd->bd) < 0)
108 printf("CPU Net Initialization Failed\n");
109 } else {
Bin Mengd8f79af2015-08-27 22:25:52 -0700110#ifndef CONFIG_DM_ETH
Simon Glass3bc42702015-04-05 16:07:37 -0600111 printf("Net Initialization Skipped\n");
Bin Mengd8f79af2015-08-27 22:25:52 -0700112#endif
Simon Glass3bc42702015-04-05 16:07:37 -0600113 }
114}
115
Joe Hershberger05c3e682015-03-22 17:09:10 -0500116#ifdef CONFIG_DM_ETH
117/**
118 * struct eth_device_priv - private structure for each Ethernet device
119 *
120 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
121 */
122struct eth_device_priv {
123 enum eth_state_t state;
124};
125
126/**
127 * struct eth_uclass_priv - The structure attached to the uclass itself
128 *
129 * @current: The Ethernet device that the network functions are using
130 */
131struct eth_uclass_priv {
132 struct udevice *current;
133};
134
Joe Hershberger60304592015-03-22 17:09:24 -0500135/* eth_errno - This stores the most recent failure code from DM functions */
136static int eth_errno;
137
Joe Hershberger05c3e682015-03-22 17:09:10 -0500138static struct eth_uclass_priv *eth_get_uclass_priv(void)
139{
140 struct uclass *uc;
141
142 uclass_get(UCLASS_ETH, &uc);
143 assert(uc);
144 return uc->priv;
145}
146
147static void eth_set_current_to_next(void)
148{
149 struct eth_uclass_priv *uc_priv;
150
151 uc_priv = eth_get_uclass_priv();
152 if (uc_priv->current)
153 uclass_next_device(&uc_priv->current);
154 if (!uc_priv->current)
155 uclass_first_device(UCLASS_ETH, &uc_priv->current);
156}
157
Joe Hershberger60304592015-03-22 17:09:24 -0500158/*
159 * Typically this will simply return the active device.
160 * In the case where the most recent active device was unset, this will attempt
161 * to return the first device. If that device doesn't exist or fails to probe,
162 * this function will return NULL.
163 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500164struct udevice *eth_get_dev(void)
165{
166 struct eth_uclass_priv *uc_priv;
167
168 uc_priv = eth_get_uclass_priv();
169 if (!uc_priv->current)
Joe Hershberger60304592015-03-22 17:09:24 -0500170 eth_errno = uclass_first_device(UCLASS_ETH,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500171 &uc_priv->current);
172 return uc_priv->current;
173}
174
Joe Hershberger60304592015-03-22 17:09:24 -0500175/*
176 * Typically this will just store a device pointer.
177 * In case it was not probed, we will attempt to do so.
178 * dev may be NULL to unset the active device.
179 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500180static void eth_set_dev(struct udevice *dev)
181{
Joe Hershberger60304592015-03-22 17:09:24 -0500182 if (dev && !device_active(dev))
183 eth_errno = device_probe(dev);
Joe Hershberger05c3e682015-03-22 17:09:10 -0500184 eth_get_uclass_priv()->current = dev;
185}
186
Joe Hershbergere58780d2015-03-22 17:09:16 -0500187/*
188 * Find the udevice that either has the name passed in as devname or has an
189 * alias named devname.
190 */
191struct udevice *eth_get_dev_by_name(const char *devname)
192{
193 int seq = -1;
194 char *endp = NULL;
195 const char *startp = NULL;
196 struct udevice *it;
197 struct uclass *uc;
198
199 /* Must be longer than 3 to be an alias */
200 if (strlen(devname) > strlen("eth")) {
201 startp = devname + strlen("eth");
202 seq = simple_strtoul(startp, &endp, 10);
203 }
204
205 uclass_get(UCLASS_ETH, &uc);
206 uclass_foreach_dev(it, uc) {
Joe Hershberger60304592015-03-22 17:09:24 -0500207 /*
208 * We need the seq to be valid, so try to probe it.
209 * If the probe fails, the seq will not match since it will be
210 * -1 instead of what we are looking for.
211 * We don't care about errors from probe here. Either they won't
212 * match an alias or it will match a literal name and we'll pick
213 * up the error when we try to probe again in eth_set_dev().
214 */
Joe Hershbergere58780d2015-03-22 17:09:16 -0500215 device_probe(it);
216 /*
217 * Check for the name or the sequence number to match
218 */
219 if (strcmp(it->name, devname) == 0 ||
220 (endp > startp && it->seq == seq))
221 return it;
222 }
223
224 return NULL;
225}
226
Joe Hershberger05c3e682015-03-22 17:09:10 -0500227unsigned char *eth_get_ethaddr(void)
228{
229 struct eth_pdata *pdata;
230
231 if (eth_get_dev()) {
232 pdata = eth_get_dev()->platdata;
233 return pdata->enetaddr;
234 }
235
236 return NULL;
237}
238
239/* Set active state without calling start on the driver */
240int eth_init_state_only(void)
241{
242 struct udevice *current;
243 struct eth_device_priv *priv;
244
245 current = eth_get_dev();
246 if (!current || !device_active(current))
247 return -EINVAL;
248
249 priv = current->uclass_priv;
250 priv->state = ETH_STATE_ACTIVE;
251
252 return 0;
253}
254
255/* Set passive state without calling stop on the driver */
256void eth_halt_state_only(void)
257{
258 struct udevice *current;
259 struct eth_device_priv *priv;
260
261 current = eth_get_dev();
262 if (!current || !device_active(current))
263 return;
264
265 priv = current->uclass_priv;
266 priv->state = ETH_STATE_PASSIVE;
267}
268
269int eth_get_dev_index(void)
270{
271 if (eth_get_dev())
272 return eth_get_dev()->seq;
273 return -1;
274}
275
Joe Hershbergerf566c992015-03-24 02:41:49 -0500276static int eth_write_hwaddr(struct udevice *dev)
277{
278 struct eth_pdata *pdata = dev->platdata;
279 int ret = 0;
280
281 if (!dev || !device_active(dev))
282 return -EINVAL;
283
284 /* seq is valid since the device is active */
285 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
286 if (!is_valid_ethaddr(pdata->enetaddr)) {
287 printf("\nError: %s address %pM illegal value\n",
288 dev->name, pdata->enetaddr);
289 return -EINVAL;
290 }
291
Simon Glassb86f7952015-07-06 16:47:55 -0600292 /*
293 * Drivers are allowed to decide not to implement this at
294 * run-time. E.g. Some devices may use it and some may not.
295 */
Joe Hershbergerf566c992015-03-24 02:41:49 -0500296 ret = eth_get_ops(dev)->write_hwaddr(dev);
Simon Glassb86f7952015-07-06 16:47:55 -0600297 if (ret == -ENOSYS)
298 ret = 0;
Joe Hershbergerf566c992015-03-24 02:41:49 -0500299 if (ret)
300 printf("\nWarning: %s failed to set MAC address\n",
301 dev->name);
302 }
303
304 return ret;
305}
306
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500307static int on_ethaddr(const char *name, const char *value, enum env_op op,
308 int flags)
309{
310 int index;
311 int retval;
312 struct udevice *dev;
313
314 /* look for an index after "eth" */
315 index = simple_strtoul(name + 3, NULL, 10);
316
317 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
318 if (!retval) {
319 struct eth_pdata *pdata = dev->platdata;
320 switch (op) {
321 case env_op_create:
322 case env_op_overwrite:
323 eth_parse_enetaddr(value, pdata->enetaddr);
324 break;
325 case env_op_delete:
326 memset(pdata->enetaddr, 0, 6);
327 }
328 }
329
330 return 0;
331}
332U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
333
Joe Hershberger05c3e682015-03-22 17:09:10 -0500334int eth_init(void)
335{
336 struct udevice *current;
337 struct udevice *old_current;
Joe Hershberger60304592015-03-22 17:09:24 -0500338 int ret = -ENODEV;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500339
340 current = eth_get_dev();
341 if (!current) {
342 printf("No ethernet found.\n");
343 return -ENODEV;
344 }
345
346 old_current = current;
347 do {
348 debug("Trying %s\n", current->name);
349
350 if (device_active(current)) {
Joe Hershberger60304592015-03-22 17:09:24 -0500351 ret = eth_get_ops(current)->start(current);
352 if (ret >= 0) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500353 struct eth_device_priv *priv =
354 current->uclass_priv;
355
356 priv->state = ETH_STATE_ACTIVE;
357 return 0;
358 }
Joe Hershbergerff819a32015-04-08 01:41:19 -0500359 } else {
Joe Hershberger60304592015-03-22 17:09:24 -0500360 ret = eth_errno;
Joe Hershbergerff819a32015-04-08 01:41:19 -0500361 }
Joe Hershberger60304592015-03-22 17:09:24 -0500362
Joe Hershberger05c3e682015-03-22 17:09:10 -0500363 debug("FAIL\n");
364
Joe Hershberger60304592015-03-22 17:09:24 -0500365 /*
366 * If ethrotate is enabled, this will change "current",
367 * otherwise we will drop out of this while loop immediately
368 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500369 eth_try_another(0);
Joe Hershberger60304592015-03-22 17:09:24 -0500370 /* This will ensure the new "current" attempted to probe */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500371 current = eth_get_dev();
372 } while (old_current != current);
373
Joe Hershberger60304592015-03-22 17:09:24 -0500374 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500375}
376
377void eth_halt(void)
378{
379 struct udevice *current;
380 struct eth_device_priv *priv;
381
382 current = eth_get_dev();
383 if (!current || !device_active(current))
384 return;
385
386 eth_get_ops(current)->stop(current);
387 priv = current->uclass_priv;
388 priv->state = ETH_STATE_PASSIVE;
389}
390
391int eth_send(void *packet, int length)
392{
393 struct udevice *current;
Joe Hershberger60304592015-03-22 17:09:24 -0500394 int ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500395
396 current = eth_get_dev();
397 if (!current)
398 return -ENODEV;
399
400 if (!device_active(current))
401 return -EINVAL;
402
Joe Hershberger60304592015-03-22 17:09:24 -0500403 ret = eth_get_ops(current)->send(current, packet, length);
404 if (ret < 0) {
405 /* We cannot completely return the error at present */
406 debug("%s: send() returned error %d\n", __func__, ret);
407 }
408 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500409}
410
411int eth_rx(void)
412{
413 struct udevice *current;
Joe Hershberger17591402015-03-22 17:09:12 -0500414 uchar *packet;
Simon Glassa1ca92e2015-07-06 16:47:49 -0600415 int flags;
Joe Hershberger17591402015-03-22 17:09:12 -0500416 int ret;
417 int i;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500418
419 current = eth_get_dev();
420 if (!current)
421 return -ENODEV;
422
423 if (!device_active(current))
424 return -EINVAL;
425
Joe Hershberger17591402015-03-22 17:09:12 -0500426 /* Process up to 32 packets at one time */
Simon Glassa1ca92e2015-07-06 16:47:49 -0600427 flags = ETH_RECV_CHECK_DEVICE;
Joe Hershberger17591402015-03-22 17:09:12 -0500428 for (i = 0; i < 32; i++) {
Simon Glassa1ca92e2015-07-06 16:47:49 -0600429 ret = eth_get_ops(current)->recv(current, flags, &packet);
430 flags = 0;
Joe Hershberger17591402015-03-22 17:09:12 -0500431 if (ret > 0)
432 net_process_received_packet(packet, ret);
Joe Hershberger63c97292015-04-03 20:09:46 -0500433 if (ret >= 0 && eth_get_ops(current)->free_pkt)
434 eth_get_ops(current)->free_pkt(current, packet, ret);
435 if (ret <= 0)
Joe Hershberger17591402015-03-22 17:09:12 -0500436 break;
437 }
438 if (ret == -EAGAIN)
439 ret = 0;
Joe Hershberger60304592015-03-22 17:09:24 -0500440 if (ret < 0) {
441 /* We cannot completely return the error at present */
442 debug("%s: recv() returned error %d\n", __func__, ret);
443 }
Joe Hershberger17591402015-03-22 17:09:12 -0500444 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500445}
446
Joe Hershberger05c3e682015-03-22 17:09:10 -0500447int eth_initialize(void)
448{
449 int num_devices = 0;
450 struct udevice *dev;
451
Simon Glass3bc42702015-04-05 16:07:37 -0600452 eth_common_init();
Joe Hershberger05c3e682015-03-22 17:09:10 -0500453
454 /*
455 * Devices need to write the hwaddr even if not started so that Linux
456 * will have access to the hwaddr that u-boot stored for the device.
457 * This is accomplished by attempting to probe each device and calling
458 * their write_hwaddr() operation.
459 */
460 uclass_first_device(UCLASS_ETH, &dev);
461 if (!dev) {
462 printf("No ethernet found.\n");
463 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
464 } else {
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500465 char *ethprime = getenv("ethprime");
466 struct udevice *prime_dev = NULL;
467
468 if (ethprime)
469 prime_dev = eth_get_dev_by_name(ethprime);
470 if (prime_dev) {
471 eth_set_dev(prime_dev);
472 eth_current_changed();
473 } else {
474 eth_set_dev(NULL);
475 }
476
Joe Hershberger05c3e682015-03-22 17:09:10 -0500477 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
478 do {
479 if (num_devices)
480 printf(", ");
481
482 printf("eth%d: %s", dev->seq, dev->name);
483
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500484 if (ethprime && dev == prime_dev)
485 printf(" [PRIME]");
486
Joe Hershberger05c3e682015-03-22 17:09:10 -0500487 eth_write_hwaddr(dev);
488
489 uclass_next_device(&dev);
490 num_devices++;
491 } while (dev);
492
493 putc('\n');
494 }
495
496 return num_devices;
497}
498
499static int eth_post_bind(struct udevice *dev)
500{
501 if (strchr(dev->name, ' ')) {
502 printf("\nError: eth device name \"%s\" has a space!\n",
503 dev->name);
504 return -EINVAL;
505 }
506
507 return 0;
508}
509
510static int eth_pre_unbind(struct udevice *dev)
511{
512 /* Don't hang onto a pointer that is going away */
513 if (dev == eth_get_uclass_priv()->current)
514 eth_set_dev(NULL);
515
516 return 0;
517}
518
519static int eth_post_probe(struct udevice *dev)
520{
521 struct eth_device_priv *priv = dev->uclass_priv;
522 struct eth_pdata *pdata = dev->platdata;
523 unsigned char env_enetaddr[6];
524
525 priv->state = ETH_STATE_INIT;
526
527 /* Check if the device has a MAC address in ROM */
528 if (eth_get_ops(dev)->read_rom_hwaddr)
529 eth_get_ops(dev)->read_rom_hwaddr(dev);
530
531 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500532 if (!is_zero_ethaddr(env_enetaddr)) {
533 if (!is_zero_ethaddr(pdata->enetaddr) &&
Joe Hershberger05c3e682015-03-22 17:09:10 -0500534 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
535 printf("\nWarning: %s MAC addresses don't match:\n",
536 dev->name);
537 printf("Address in SROM is %pM\n",
538 pdata->enetaddr);
539 printf("Address in environment is %pM\n",
540 env_enetaddr);
541 }
542
543 /* Override the ROM MAC address */
544 memcpy(pdata->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500545 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500546 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
547 printf("\nWarning: %s using MAC address from ROM\n",
548 dev->name);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500549 } else if (is_zero_ethaddr(pdata->enetaddr)) {
Joe Hershbergerbef10142015-05-04 14:55:13 -0500550#ifdef CONFIG_NET_RANDOM_ETHADDR
551 net_random_ethaddr(pdata->enetaddr);
552 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
553 dev->name, dev->seq, pdata->enetaddr);
554#else
Joe Hershberger05c3e682015-03-22 17:09:10 -0500555 printf("\nError: %s address not set.\n",
556 dev->name);
557 return -EINVAL;
Joe Hershbergerbef10142015-05-04 14:55:13 -0500558#endif
Joe Hershberger05c3e682015-03-22 17:09:10 -0500559 }
560
561 return 0;
562}
563
564static int eth_pre_remove(struct udevice *dev)
565{
566 eth_get_ops(dev)->stop(dev);
567
568 return 0;
569}
570
571UCLASS_DRIVER(eth) = {
572 .name = "eth",
573 .id = UCLASS_ETH,
574 .post_bind = eth_post_bind,
575 .pre_unbind = eth_pre_unbind,
576 .post_probe = eth_post_probe,
577 .pre_remove = eth_pre_remove,
578 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
579 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershbergere58780d2015-03-22 17:09:16 -0500580 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500581};
582#endif
583
584#ifndef CONFIG_DM_ETH
Ben Warrendd354792008-06-23 22:57:27 -0700585
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100586#ifdef CONFIG_API
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100587static struct {
588 uchar data[PKTSIZE];
589 int length;
590} eth_rcv_bufs[PKTBUFSRX];
591
Joe Hershberger66c73852012-05-15 08:59:07 +0000592static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100593#endif
594
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000595static struct eth_device *eth_devices;
596struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000597
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500598static void eth_set_current_to_next(void)
599{
600 eth_current = eth_current->next;
601}
602
Joe Hershbergere58780d2015-03-22 17:09:16 -0500603static void eth_set_dev(struct eth_device *dev)
604{
605 eth_current = dev;
606}
607
Ben Warrend7fb9bc2010-07-29 12:56:11 -0700608struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200609{
610 struct eth_device *dev, *target_dev;
611
Helmut Raiger7e7f9032011-08-22 00:17:17 +0000612 BUG_ON(devname == NULL);
613
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200614 if (!eth_devices)
615 return NULL;
616
617 dev = eth_devices;
618 target_dev = NULL;
619 do {
620 if (strcmp(devname, dev->name) == 0) {
621 target_dev = dev;
622 break;
623 }
624 dev = dev->next;
625 } while (dev != eth_devices);
626
627 return target_dev;
628}
629
Andy Fleming9e569862009-02-11 15:07:24 -0600630struct eth_device *eth_get_dev_by_index(int index)
631{
632 struct eth_device *dev, *target_dev;
Andy Fleming9e569862009-02-11 15:07:24 -0600633
634 if (!eth_devices)
635 return NULL;
636
637 dev = eth_devices;
638 target_dev = NULL;
639 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000640 if (dev->index == index) {
Andy Fleming9e569862009-02-11 15:07:24 -0600641 target_dev = dev;
642 break;
643 }
644 dev = dev->next;
Andy Fleming9e569862009-02-11 15:07:24 -0600645 } while (dev != eth_devices);
646
647 return target_dev;
648}
649
Joe Hershberger66c73852012-05-15 08:59:07 +0000650int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000651{
Joe Hershberger66c73852012-05-15 08:59:07 +0000652 if (!eth_current)
Michael Wallefea7dca2011-10-27 11:31:35 +0000653 return -1;
wdenkc6097192002-11-03 00:24:07 +0000654
Michael Wallefea7dca2011-10-27 11:31:35 +0000655 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000656}
657
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500658static int on_ethaddr(const char *name, const char *value, enum env_op op,
659 int flags)
660{
661 int index;
662 struct eth_device *dev;
663
664 if (!eth_devices)
665 return 0;
666
667 /* look for an index after "eth" */
668 index = simple_strtoul(name + 3, NULL, 10);
669
670 dev = eth_devices;
671 do {
672 if (dev->index == index) {
673 switch (op) {
674 case env_op_create:
675 case env_op_overwrite:
676 eth_parse_enetaddr(value, dev->enetaddr);
677 break;
678 case env_op_delete:
679 memset(dev->enetaddr, 0, 6);
680 }
681 }
682 } while (dev != eth_devices);
683
684 return 0;
685}
686U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
687
Simon Glass7616e782011-06-13 16:13:10 -0700688int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
689 int eth_number)
690{
691 unsigned char env_enetaddr[6];
692 int ret = 0;
693
Eric Miao69376642012-01-18 22:56:33 +0000694 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700695
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500696 if (!is_zero_ethaddr(env_enetaddr)) {
697 if (!is_zero_ethaddr(dev->enetaddr) &&
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500698 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass7616e782011-06-13 16:13:10 -0700699 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500700 dev->name);
Simon Glass7616e782011-06-13 16:13:10 -0700701 printf("Address in SROM is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500702 dev->enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700703 printf("Address in environment is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500704 env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700705 }
706
707 memcpy(dev->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500708 } else if (is_valid_ethaddr(dev->enetaddr)) {
Rob Herringc88ef3c2012-04-14 18:06:49 +0000709 eth_setenv_enetaddr_by_index(base_name, eth_number,
710 dev->enetaddr);
711 printf("\nWarning: %s using MAC address from net device\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500712 dev->name);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500713 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershbergerbef10142015-05-04 14:55:13 -0500714#ifdef CONFIG_NET_RANDOM_ETHADDR
715 net_random_ethaddr(dev->enetaddr);
716 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
717 dev->name, eth_number, dev->enetaddr);
718#else
Pavel Machek75d9a452014-07-13 10:27:02 +0200719 printf("\nError: %s address not set.\n",
720 dev->name);
721 return -EINVAL;
Joe Hershbergerbef10142015-05-04 14:55:13 -0500722#endif
Simon Glass7616e782011-06-13 16:13:10 -0700723 }
724
Pavel Machek75d9a452014-07-13 10:27:02 +0200725 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500726 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200727 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500728 dev->name, dev->enetaddr);
Pavel Machek75d9a452014-07-13 10:27:02 +0200729 return -EINVAL;
730 }
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000731
Simon Glass7616e782011-06-13 16:13:10 -0700732 ret = dev->write_hwaddr(dev);
Pavel Machek75d9a452014-07-13 10:27:02 +0200733 if (ret)
Joe Hershbergerff819a32015-04-08 01:41:19 -0500734 printf("\nWarning: %s failed to set MAC address\n",
735 dev->name);
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000736 }
Simon Glass7616e782011-06-13 16:13:10 -0700737
738 return ret;
739}
740
Simon Glass89d48362011-02-16 11:14:33 -0800741int eth_register(struct eth_device *dev)
742{
743 struct eth_device *d;
Joe Hershberger66c73852012-05-15 08:59:07 +0000744 static int index;
Michal Simek58c583b2011-08-29 23:30:13 +0000745
Mike Frysingerf6add132011-11-10 14:11:04 +0000746 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek58c583b2011-08-29 23:30:13 +0000747
Simon Glass89d48362011-02-16 11:14:33 -0800748 if (!eth_devices) {
Joe Hershbergerff819a32015-04-08 01:41:19 -0500749 eth_devices = dev;
750 eth_current = dev;
Simon Glass89d48362011-02-16 11:14:33 -0800751 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000752 } else {
Joe Hershberger66c73852012-05-15 08:59:07 +0000753 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundelaba4b692010-03-31 17:56:08 +0200754 ;
wdenkc6097192002-11-03 00:24:07 +0000755 d->next = dev;
756 }
757
758 dev->state = ETH_STATE_INIT;
759 dev->next = eth_devices;
Michael Wallefea7dca2011-10-27 11:31:35 +0000760 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000761
762 return 0;
763}
764
Vincent Palatine7e982d2012-01-09 08:32:36 +0000765int eth_unregister(struct eth_device *dev)
766{
767 struct eth_device *cur;
768
769 /* No device */
770 if (!eth_devices)
Joe Hershberger05324a42015-03-22 17:09:04 -0500771 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000772
773 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
774 cur = cur->next)
775 ;
776
777 /* Device not found */
778 if (cur->next != dev)
Joe Hershberger05324a42015-03-22 17:09:04 -0500779 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000780
781 cur->next = dev->next;
782
783 if (eth_devices == dev)
784 eth_devices = dev->next == eth_devices ? NULL : dev->next;
785
786 if (eth_current == dev) {
787 eth_current = eth_devices;
788 eth_current_changed();
789 }
790
791 return 0;
792}
793
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500794int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000795{
Michael Wallefea7dca2011-10-27 11:31:35 +0000796 int num_devices = 0;
Simon Glass3bc42702015-04-05 16:07:37 -0600797
wdenkc6097192002-11-03 00:24:07 +0000798 eth_devices = NULL;
799 eth_current = NULL;
Simon Glass3bc42702015-04-05 16:07:37 -0600800 eth_common_init();
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100801
wdenkc6097192002-11-03 00:24:07 +0000802 if (!eth_devices) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000803 puts("No ethernet found.\n");
Simon Glass770605e2012-02-13 13:51:18 +0000804 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000805 } else {
806 struct eth_device *dev = eth_devices;
Joe Hershberger66c73852012-05-15 08:59:07 +0000807 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000808
Simon Glass770605e2012-02-13 13:51:18 +0000809 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000810 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000811 if (dev->index)
Joe Hershberger66c73852012-05-15 08:59:07 +0000812 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000813
814 printf("%s", dev->name);
815
Joe Hershberger66c73852012-05-15 08:59:07 +0000816 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000817 eth_current = dev;
Joe Hershberger66c73852012-05-15 08:59:07 +0000818 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000819 }
820
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400821 if (strchr(dev->name, ' '))
Joe Hershberger66c73852012-05-15 08:59:07 +0000822 puts("\nWarning: eth device name has a space!"
823 "\n");
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400824
Pavel Machek75d9a452014-07-13 10:27:02 +0200825 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000826
wdenkc6097192002-11-03 00:24:07 +0000827 dev = dev->next;
Michael Wallefea7dca2011-10-27 11:31:35 +0000828 num_devices++;
Joe Hershberger66c73852012-05-15 08:59:07 +0000829 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000830
Simon Glass89d48362011-02-16 11:14:33 -0800831 eth_current_changed();
Joe Hershberger66c73852012-05-15 08:59:07 +0000832 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000833 }
834
Michael Wallefea7dca2011-10-27 11:31:35 +0000835 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000836}
837
David Updegraff53a5c422007-06-11 10:41:07 -0500838#ifdef CONFIG_MCAST_TFTP
839/* Multicast.
840 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200841 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500842 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500843int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff53a5c422007-06-11 10:41:07 -0500844{
Joe Hershberger66c73852012-05-15 08:59:07 +0000845 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200846 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500847 return -1;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500848 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
849 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
850 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff53a5c422007-06-11 10:41:07 -0500851 mcast_mac[2] = 0x5e;
852 mcast_mac[1] = 0x0;
853 mcast_mac[0] = 0x1;
854 return eth_current->mcast(eth_current, mcast_mac, join);
855}
856
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200857/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
858 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500859 * some other adapter -- hash tables
860 */
861#define CRCPOLY_LE 0xedb88320
Joe Hershberger66c73852012-05-15 08:59:07 +0000862u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff53a5c422007-06-11 10:41:07 -0500863{
864 int i;
865 u32 crc;
866 crc = ~0;
867 while (len--) {
868 crc ^= *p++;
869 for (i = 0; i < 8; i++)
870 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
871 }
872 /* an reverse the bits, cuz of way they arrive -- last-first */
873 crc = (crc >> 16) | (crc << 16);
874 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
875 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
876 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
877 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
878 return crc;
879}
880
881#endif
882
wdenkc6097192002-11-03 00:24:07 +0000883
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500884int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000885{
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500886 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000887
Stefan Roese6bc11382008-03-04 17:40:41 +0100888 if (!eth_current) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000889 puts("No ethernet found.\n");
Joe Hershberger05324a42015-03-22 17:09:04 -0500890 return -ENODEV;
Stefan Roese6bc11382008-03-04 17:40:41 +0100891 }
wdenkc6097192002-11-03 00:24:07 +0000892
893 old_current = eth_current;
894 do {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400895 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000896
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500897 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000898 eth_current->state = ETH_STATE_ACTIVE;
899
Upakul Barkakaty505be872007-11-29 12:16:13 +0530900 return 0;
wdenkc6097192002-11-03 00:24:07 +0000901 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400902 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000903
904 eth_try_another(0);
905 } while (old_current != eth_current);
906
Joe Hershberger05324a42015-03-22 17:09:04 -0500907 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000908}
909
910void eth_halt(void)
911{
912 if (!eth_current)
913 return;
914
915 eth_current->halt(eth_current);
916
917 eth_current->state = ETH_STATE_PASSIVE;
918}
919
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000920int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000921{
922 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500923 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000924
925 return eth_current->send(eth_current, packet, length);
926}
927
928int eth_rx(void)
929{
930 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500931 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000932
933 return eth_current->recv(eth_current);
934}
Joe Hershberger05c3e682015-03-22 17:09:10 -0500935#endif /* ifndef CONFIG_DM_ETH */
wdenkc6097192002-11-03 00:24:07 +0000936
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100937#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000938static void eth_save_packet(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100939{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000940 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100941 int i;
942
943 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
944 return;
945
946 if (PKTSIZE < length)
947 return;
948
949 for (i = 0; i < length; i++)
950 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
951
952 eth_rcv_bufs[eth_rcv_last].length = length;
953 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
954}
955
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000956int eth_receive(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100957{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000958 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100959 void *pp = push_packet;
960 int i;
961
962 if (eth_rcv_current == eth_rcv_last) {
963 push_packet = eth_save_packet;
964 eth_rx();
965 push_packet = pp;
966
967 if (eth_rcv_current == eth_rcv_last)
968 return -1;
969 }
970
Michael Walle46c07bc2012-06-22 11:24:28 +0000971 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100972
973 for (i = 0; i < length; i++)
974 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
975
976 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
977 return length;
978}
979#endif /* CONFIG_API */
980
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500981static void eth_current_changed(void)
982{
983 char *act = getenv("ethact");
984 /* update current ethernet name */
985 if (eth_get_dev()) {
986 if (act == NULL || strcmp(act, eth_get_name()) != 0)
987 setenv("ethact", eth_get_name());
988 }
989 /*
990 * remove the variable completely if there is no active
991 * interface
992 */
993 else if (act != NULL)
994 setenv("ethact", NULL);
995}
996
wdenkc6097192002-11-03 00:24:07 +0000997void eth_try_another(int first_restart)
998{
Joe Hershberger05c3e682015-03-22 17:09:10 -0500999 static void *first_failed;
Remy Bohmerc650e1b2011-02-19 20:15:14 +01001000 char *ethrotate;
Matthias Fuchse1692572008-01-17 07:45:05 +01001001
1002 /*
1003 * Do not rotate between network interfaces when
1004 * 'ethrotate' variable is set to 'no'.
1005 */
Joe Hershberger66c73852012-05-15 08:59:07 +00001006 ethrotate = getenv("ethrotate");
1007 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchse1692572008-01-17 07:45:05 +01001008 return;
wdenkc6097192002-11-03 00:24:07 +00001009
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001010 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +00001011 return;
1012
Joe Hershberger66c73852012-05-15 08:59:07 +00001013 if (first_restart)
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001014 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +00001015
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001016 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +00001017
Simon Glass89d48362011-02-16 11:14:33 -08001018 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +00001019
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001020 if (first_failed == eth_get_dev())
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001021 net_restart_wrap = 1;
wdenkc6097192002-11-03 00:24:07 +00001022}
1023
wdenka3d991b2004-04-15 21:48:45 +00001024void eth_set_current(void)
1025{
Joe Hershberger66c73852012-05-15 08:59:07 +00001026 static char *act;
1027 static int env_changed_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +01001028 int env_id;
wdenka3d991b2004-04-15 21:48:45 +00001029
Heiko Schocher2f70c492009-02-10 09:38:52 +01001030 env_id = get_env_id();
1031 if ((act == NULL) || (env_changed_id != env_id)) {
1032 act = getenv("ethact");
1033 env_changed_id = env_id;
1034 }
Joe Hershberger6536b9b2015-03-22 17:09:17 -05001035
1036 if (act == NULL) {
1037 char *ethprime = getenv("ethprime");
1038 void *dev = NULL;
1039
1040 if (ethprime)
1041 dev = eth_get_dev_by_name(ethprime);
1042 if (dev)
1043 eth_set_dev(dev);
1044 else
1045 eth_set_dev(NULL);
1046 } else {
Joe Hershbergere58780d2015-03-22 17:09:16 -05001047 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershberger6536b9b2015-03-22 17:09:17 -05001048 }
wdenka3d991b2004-04-15 21:48:45 +00001049
Simon Glass89d48362011-02-16 11:14:33 -08001050 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +00001051}
wdenka3d991b2004-04-15 21:48:45 +00001052
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001053const char *eth_get_name(void)
wdenk5bb226e2003-11-17 21:14:37 +00001054{
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001055 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenk5bb226e2003-11-17 21:14:37 +00001056}