blob: 13b7723bb47e545bdf480ccd65c2654f1e74ba35 [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>
wdenkc6097192002-11-03 00:24:07 +000012#include <net.h>
Marian Balakowiczd9785c12005-11-30 18:06:04 +010013#include <miiphy.h>
Andy Fleming5f184712011-04-08 02:10:27 -050014#include <phy.h>
Pavel Machek75d9a452014-07-13 10:27:02 +020015#include <asm/errno.h>
Joe Hershberger05c3e682015-03-22 17:09:10 -050016#include <dm/device-internal.h>
wdenkc6097192002-11-03 00:24:07 +000017
Joe Hershbergerd2eaec62015-03-22 17:09:06 -050018DECLARE_GLOBAL_DATA_PTR;
19
Mike Frysinger3f6e6992009-01-29 19:43:44 -050020void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
21{
22 char *end;
23 int i;
24
25 for (i = 0; i < 6; ++i) {
26 enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
27 if (addr)
28 addr = (*end) ? end + 1 : end;
29 }
30}
31
32int eth_getenv_enetaddr(char *name, uchar *enetaddr)
33{
34 eth_parse_enetaddr(getenv(name), enetaddr);
35 return is_valid_ether_addr(enetaddr);
36}
37
38int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
39{
40 char buf[20];
41
42 sprintf(buf, "%pM", enetaddr);
43
44 return setenv(name, buf);
45}
Mike Frysinger86848a72009-07-15 21:31:28 -040046
Simon Glass7616e782011-06-13 16:13:10 -070047int eth_getenv_enetaddr_by_index(const char *base_name, int index,
48 uchar *enetaddr)
Mike Frysinger86848a72009-07-15 21:31:28 -040049{
50 char enetvar[32];
Simon Glass7616e782011-06-13 16:13:10 -070051 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
Mike Frysinger86848a72009-07-15 21:31:28 -040052 return eth_getenv_enetaddr(enetvar, enetaddr);
53}
Mike Frysinger3f6e6992009-01-29 19:43:44 -050054
Joe Hershberger154177e2012-07-10 16:23:22 -050055static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
Rob Herringc88ef3c2012-04-14 18:06:49 +000056 uchar *enetaddr)
57{
58 char enetvar[32];
59 sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
60 return eth_setenv_enetaddr(enetvar, enetaddr);
61}
62
Joe Hershberger84eb1fb2015-03-22 17:09:03 -050063static void eth_env_init(void)
64{
65 const char *s;
66
67 s = getenv("bootfile");
68 if (s != NULL)
69 copy_filename(BootFile, s, sizeof(BootFile));
70}
Rob Herringc88ef3c2012-04-14 18:06:49 +000071
Ben Warrenecee9322010-04-26 11:11:46 -070072static int eth_mac_skip(int index)
73{
74 char enetvar[15];
75 char *skip_state;
76 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
77 return ((skip_state = getenv(enetvar)) != NULL);
78}
79
Joe Hershberger84eb1fb2015-03-22 17:09:03 -050080static void eth_current_changed(void);
81
Joe Hershberger05c3e682015-03-22 17:09:10 -050082#ifdef CONFIG_DM_ETH
83/**
84 * struct eth_device_priv - private structure for each Ethernet device
85 *
86 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
87 */
88struct eth_device_priv {
89 enum eth_state_t state;
90};
91
92/**
93 * struct eth_uclass_priv - The structure attached to the uclass itself
94 *
95 * @current: The Ethernet device that the network functions are using
96 */
97struct eth_uclass_priv {
98 struct udevice *current;
99};
100
Joe Hershberger60304592015-03-22 17:09:24 -0500101/* eth_errno - This stores the most recent failure code from DM functions */
102static int eth_errno;
103
Joe Hershberger05c3e682015-03-22 17:09:10 -0500104static struct eth_uclass_priv *eth_get_uclass_priv(void)
105{
106 struct uclass *uc;
107
108 uclass_get(UCLASS_ETH, &uc);
109 assert(uc);
110 return uc->priv;
111}
112
113static void eth_set_current_to_next(void)
114{
115 struct eth_uclass_priv *uc_priv;
116
117 uc_priv = eth_get_uclass_priv();
118 if (uc_priv->current)
119 uclass_next_device(&uc_priv->current);
120 if (!uc_priv->current)
121 uclass_first_device(UCLASS_ETH, &uc_priv->current);
122}
123
Joe Hershberger60304592015-03-22 17:09:24 -0500124/*
125 * Typically this will simply return the active device.
126 * In the case where the most recent active device was unset, this will attempt
127 * to return the first device. If that device doesn't exist or fails to probe,
128 * this function will return NULL.
129 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500130struct udevice *eth_get_dev(void)
131{
132 struct eth_uclass_priv *uc_priv;
133
134 uc_priv = eth_get_uclass_priv();
135 if (!uc_priv->current)
Joe Hershberger60304592015-03-22 17:09:24 -0500136 eth_errno = uclass_first_device(UCLASS_ETH,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500137 &uc_priv->current);
138 return uc_priv->current;
139}
140
Joe Hershberger60304592015-03-22 17:09:24 -0500141/*
142 * Typically this will just store a device pointer.
143 * In case it was not probed, we will attempt to do so.
144 * dev may be NULL to unset the active device.
145 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500146static void eth_set_dev(struct udevice *dev)
147{
Joe Hershberger60304592015-03-22 17:09:24 -0500148 if (dev && !device_active(dev))
149 eth_errno = device_probe(dev);
Joe Hershberger05c3e682015-03-22 17:09:10 -0500150 eth_get_uclass_priv()->current = dev;
151}
152
Joe Hershbergere58780d2015-03-22 17:09:16 -0500153/*
154 * Find the udevice that either has the name passed in as devname or has an
155 * alias named devname.
156 */
157struct udevice *eth_get_dev_by_name(const char *devname)
158{
159 int seq = -1;
160 char *endp = NULL;
161 const char *startp = NULL;
162 struct udevice *it;
163 struct uclass *uc;
164
165 /* Must be longer than 3 to be an alias */
166 if (strlen(devname) > strlen("eth")) {
167 startp = devname + strlen("eth");
168 seq = simple_strtoul(startp, &endp, 10);
169 }
170
171 uclass_get(UCLASS_ETH, &uc);
172 uclass_foreach_dev(it, uc) {
Joe Hershberger60304592015-03-22 17:09:24 -0500173 /*
174 * We need the seq to be valid, so try to probe it.
175 * If the probe fails, the seq will not match since it will be
176 * -1 instead of what we are looking for.
177 * We don't care about errors from probe here. Either they won't
178 * match an alias or it will match a literal name and we'll pick
179 * up the error when we try to probe again in eth_set_dev().
180 */
Joe Hershbergere58780d2015-03-22 17:09:16 -0500181 device_probe(it);
182 /*
183 * Check for the name or the sequence number to match
184 */
185 if (strcmp(it->name, devname) == 0 ||
186 (endp > startp && it->seq == seq))
187 return it;
188 }
189
190 return NULL;
191}
192
Joe Hershberger05c3e682015-03-22 17:09:10 -0500193unsigned char *eth_get_ethaddr(void)
194{
195 struct eth_pdata *pdata;
196
197 if (eth_get_dev()) {
198 pdata = eth_get_dev()->platdata;
199 return pdata->enetaddr;
200 }
201
202 return NULL;
203}
204
205/* Set active state without calling start on the driver */
206int eth_init_state_only(void)
207{
208 struct udevice *current;
209 struct eth_device_priv *priv;
210
211 current = eth_get_dev();
212 if (!current || !device_active(current))
213 return -EINVAL;
214
215 priv = current->uclass_priv;
216 priv->state = ETH_STATE_ACTIVE;
217
218 return 0;
219}
220
221/* Set passive state without calling stop on the driver */
222void eth_halt_state_only(void)
223{
224 struct udevice *current;
225 struct eth_device_priv *priv;
226
227 current = eth_get_dev();
228 if (!current || !device_active(current))
229 return;
230
231 priv = current->uclass_priv;
232 priv->state = ETH_STATE_PASSIVE;
233}
234
235int eth_get_dev_index(void)
236{
237 if (eth_get_dev())
238 return eth_get_dev()->seq;
239 return -1;
240}
241
242int eth_init(void)
243{
244 struct udevice *current;
245 struct udevice *old_current;
Joe Hershberger60304592015-03-22 17:09:24 -0500246 int ret = -ENODEV;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500247
248 current = eth_get_dev();
249 if (!current) {
250 printf("No ethernet found.\n");
251 return -ENODEV;
252 }
253
254 old_current = current;
255 do {
256 debug("Trying %s\n", current->name);
257
258 if (device_active(current)) {
259 uchar env_enetaddr[6];
260 struct eth_pdata *pdata = current->platdata;
261
262 /* Sync environment with network device */
263 if (eth_getenv_enetaddr_by_index("eth", current->seq,
264 env_enetaddr))
265 memcpy(pdata->enetaddr, env_enetaddr, 6);
266 else
267 memset(pdata->enetaddr, 0, 6);
268
Joe Hershberger60304592015-03-22 17:09:24 -0500269 ret = eth_get_ops(current)->start(current);
270 if (ret >= 0) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500271 struct eth_device_priv *priv =
272 current->uclass_priv;
273
274 priv->state = ETH_STATE_ACTIVE;
275 return 0;
276 }
Joe Hershberger60304592015-03-22 17:09:24 -0500277 } else
278 ret = eth_errno;
279
Joe Hershberger05c3e682015-03-22 17:09:10 -0500280 debug("FAIL\n");
281
Joe Hershberger60304592015-03-22 17:09:24 -0500282 /*
283 * If ethrotate is enabled, this will change "current",
284 * otherwise we will drop out of this while loop immediately
285 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500286 eth_try_another(0);
Joe Hershberger60304592015-03-22 17:09:24 -0500287 /* This will ensure the new "current" attempted to probe */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500288 current = eth_get_dev();
289 } while (old_current != current);
290
Joe Hershberger60304592015-03-22 17:09:24 -0500291 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500292}
293
294void eth_halt(void)
295{
296 struct udevice *current;
297 struct eth_device_priv *priv;
298
299 current = eth_get_dev();
300 if (!current || !device_active(current))
301 return;
302
303 eth_get_ops(current)->stop(current);
304 priv = current->uclass_priv;
305 priv->state = ETH_STATE_PASSIVE;
306}
307
308int eth_send(void *packet, int length)
309{
310 struct udevice *current;
Joe Hershberger60304592015-03-22 17:09:24 -0500311 int ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500312
313 current = eth_get_dev();
314 if (!current)
315 return -ENODEV;
316
317 if (!device_active(current))
318 return -EINVAL;
319
Joe Hershberger60304592015-03-22 17:09:24 -0500320 ret = eth_get_ops(current)->send(current, packet, length);
321 if (ret < 0) {
322 /* We cannot completely return the error at present */
323 debug("%s: send() returned error %d\n", __func__, ret);
324 }
325 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500326}
327
328int eth_rx(void)
329{
330 struct udevice *current;
Joe Hershberger17591402015-03-22 17:09:12 -0500331 uchar *packet;
332 int ret;
333 int i;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500334
335 current = eth_get_dev();
336 if (!current)
337 return -ENODEV;
338
339 if (!device_active(current))
340 return -EINVAL;
341
Joe Hershberger17591402015-03-22 17:09:12 -0500342 /* Process up to 32 packets at one time */
343 for (i = 0; i < 32; i++) {
344 ret = eth_get_ops(current)->recv(current, &packet);
345 if (ret > 0)
346 net_process_received_packet(packet, ret);
347 else
348 break;
349 }
350 if (ret == -EAGAIN)
351 ret = 0;
Joe Hershberger60304592015-03-22 17:09:24 -0500352 if (ret < 0) {
353 /* We cannot completely return the error at present */
354 debug("%s: recv() returned error %d\n", __func__, ret);
355 }
Joe Hershberger17591402015-03-22 17:09:12 -0500356 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500357}
358
359static int eth_write_hwaddr(struct udevice *dev)
360{
361 struct eth_pdata *pdata = dev->platdata;
362 int ret = 0;
363
364 if (!dev || !device_active(dev))
365 return -EINVAL;
366
367 /* seq is valid since the device is active */
368 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
369 if (!is_valid_ether_addr(pdata->enetaddr)) {
370 printf("\nError: %s address %pM illegal value\n",
371 dev->name, pdata->enetaddr);
372 return -EINVAL;
373 }
374
375 ret = eth_get_ops(dev)->write_hwaddr(dev);
376 if (ret)
377 printf("\nWarning: %s failed to set MAC address\n",
378 dev->name);
379 }
380
381 return ret;
382}
383
384int eth_initialize(void)
385{
386 int num_devices = 0;
387 struct udevice *dev;
388
389 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
390 eth_env_init();
391
392 /*
393 * Devices need to write the hwaddr even if not started so that Linux
394 * will have access to the hwaddr that u-boot stored for the device.
395 * This is accomplished by attempting to probe each device and calling
396 * their write_hwaddr() operation.
397 */
398 uclass_first_device(UCLASS_ETH, &dev);
399 if (!dev) {
400 printf("No ethernet found.\n");
401 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
402 } else {
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500403 char *ethprime = getenv("ethprime");
404 struct udevice *prime_dev = NULL;
405
406 if (ethprime)
407 prime_dev = eth_get_dev_by_name(ethprime);
408 if (prime_dev) {
409 eth_set_dev(prime_dev);
410 eth_current_changed();
411 } else {
412 eth_set_dev(NULL);
413 }
414
Joe Hershberger05c3e682015-03-22 17:09:10 -0500415 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
416 do {
417 if (num_devices)
418 printf(", ");
419
420 printf("eth%d: %s", dev->seq, dev->name);
421
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500422 if (ethprime && dev == prime_dev)
423 printf(" [PRIME]");
424
Joe Hershberger05c3e682015-03-22 17:09:10 -0500425 eth_write_hwaddr(dev);
426
427 uclass_next_device(&dev);
428 num_devices++;
429 } while (dev);
430
431 putc('\n');
432 }
433
434 return num_devices;
435}
436
437static int eth_post_bind(struct udevice *dev)
438{
439 if (strchr(dev->name, ' ')) {
440 printf("\nError: eth device name \"%s\" has a space!\n",
441 dev->name);
442 return -EINVAL;
443 }
444
445 return 0;
446}
447
448static int eth_pre_unbind(struct udevice *dev)
449{
450 /* Don't hang onto a pointer that is going away */
451 if (dev == eth_get_uclass_priv()->current)
452 eth_set_dev(NULL);
453
454 return 0;
455}
456
457static int eth_post_probe(struct udevice *dev)
458{
459 struct eth_device_priv *priv = dev->uclass_priv;
460 struct eth_pdata *pdata = dev->platdata;
461 unsigned char env_enetaddr[6];
462
463 priv->state = ETH_STATE_INIT;
464
465 /* Check if the device has a MAC address in ROM */
466 if (eth_get_ops(dev)->read_rom_hwaddr)
467 eth_get_ops(dev)->read_rom_hwaddr(dev);
468
469 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
470 if (!is_zero_ether_addr(env_enetaddr)) {
471 if (!is_zero_ether_addr(pdata->enetaddr) &&
472 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
473 printf("\nWarning: %s MAC addresses don't match:\n",
474 dev->name);
475 printf("Address in SROM is %pM\n",
476 pdata->enetaddr);
477 printf("Address in environment is %pM\n",
478 env_enetaddr);
479 }
480
481 /* Override the ROM MAC address */
482 memcpy(pdata->enetaddr, env_enetaddr, 6);
483 } else if (is_valid_ether_addr(pdata->enetaddr)) {
484 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
485 printf("\nWarning: %s using MAC address from ROM\n",
486 dev->name);
487 } else if (is_zero_ether_addr(pdata->enetaddr)) {
488 printf("\nError: %s address not set.\n",
489 dev->name);
490 return -EINVAL;
491 }
492
493 return 0;
494}
495
496static int eth_pre_remove(struct udevice *dev)
497{
498 eth_get_ops(dev)->stop(dev);
499
500 return 0;
501}
502
503UCLASS_DRIVER(eth) = {
504 .name = "eth",
505 .id = UCLASS_ETH,
506 .post_bind = eth_post_bind,
507 .pre_unbind = eth_pre_unbind,
508 .post_probe = eth_post_probe,
509 .pre_remove = eth_pre_remove,
510 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
511 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershbergere58780d2015-03-22 17:09:16 -0500512 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500513};
514#endif
515
516#ifndef CONFIG_DM_ETH
Ben Warrendd354792008-06-23 22:57:27 -0700517/*
518 * CPU and board-specific Ethernet initializations. Aliased function
519 * signals caller to move on
520 */
521static int __def_eth_init(bd_t *bis)
522{
523 return -1;
524}
Peter Tyserf9a109b2009-04-20 11:08:46 -0500525int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
526int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
Ben Warrendd354792008-06-23 22:57:27 -0700527
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100528#ifdef CONFIG_API
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100529static struct {
530 uchar data[PKTSIZE];
531 int length;
532} eth_rcv_bufs[PKTBUFSRX];
533
Joe Hershberger66c73852012-05-15 08:59:07 +0000534static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100535#endif
536
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000537static struct eth_device *eth_devices;
538struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000539
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500540static void eth_set_current_to_next(void)
541{
542 eth_current = eth_current->next;
543}
544
Joe Hershbergere58780d2015-03-22 17:09:16 -0500545static void eth_set_dev(struct eth_device *dev)
546{
547 eth_current = dev;
548}
549
Ben Warrend7fb9bc2010-07-29 12:56:11 -0700550struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200551{
552 struct eth_device *dev, *target_dev;
553
Helmut Raiger7e7f9032011-08-22 00:17:17 +0000554 BUG_ON(devname == NULL);
555
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200556 if (!eth_devices)
557 return NULL;
558
559 dev = eth_devices;
560 target_dev = NULL;
561 do {
562 if (strcmp(devname, dev->name) == 0) {
563 target_dev = dev;
564 break;
565 }
566 dev = dev->next;
567 } while (dev != eth_devices);
568
569 return target_dev;
570}
571
Andy Fleming9e569862009-02-11 15:07:24 -0600572struct eth_device *eth_get_dev_by_index(int index)
573{
574 struct eth_device *dev, *target_dev;
Andy Fleming9e569862009-02-11 15:07:24 -0600575
576 if (!eth_devices)
577 return NULL;
578
579 dev = eth_devices;
580 target_dev = NULL;
581 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000582 if (dev->index == index) {
Andy Fleming9e569862009-02-11 15:07:24 -0600583 target_dev = dev;
584 break;
585 }
586 dev = dev->next;
Andy Fleming9e569862009-02-11 15:07:24 -0600587 } while (dev != eth_devices);
588
589 return target_dev;
590}
591
Joe Hershberger66c73852012-05-15 08:59:07 +0000592int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000593{
Joe Hershberger66c73852012-05-15 08:59:07 +0000594 if (!eth_current)
Michael Wallefea7dca2011-10-27 11:31:35 +0000595 return -1;
wdenkc6097192002-11-03 00:24:07 +0000596
Michael Wallefea7dca2011-10-27 11:31:35 +0000597 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000598}
599
Simon Glass7616e782011-06-13 16:13:10 -0700600int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
601 int eth_number)
602{
603 unsigned char env_enetaddr[6];
604 int ret = 0;
605
Eric Miao69376642012-01-18 22:56:33 +0000606 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700607
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500608 if (!is_zero_ether_addr(env_enetaddr)) {
609 if (!is_zero_ether_addr(dev->enetaddr) &&
610 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass7616e782011-06-13 16:13:10 -0700611 printf("\nWarning: %s MAC addresses don't match:\n",
612 dev->name);
613 printf("Address in SROM is %pM\n",
614 dev->enetaddr);
615 printf("Address in environment is %pM\n",
616 env_enetaddr);
617 }
618
619 memcpy(dev->enetaddr, env_enetaddr, 6);
Rob Herringc88ef3c2012-04-14 18:06:49 +0000620 } else if (is_valid_ether_addr(dev->enetaddr)) {
621 eth_setenv_enetaddr_by_index(base_name, eth_number,
622 dev->enetaddr);
623 printf("\nWarning: %s using MAC address from net device\n",
624 dev->name);
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500625 } else if (is_zero_ether_addr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200626 printf("\nError: %s address not set.\n",
627 dev->name);
628 return -EINVAL;
Simon Glass7616e782011-06-13 16:13:10 -0700629 }
630
Pavel Machek75d9a452014-07-13 10:27:02 +0200631 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
632 if (!is_valid_ether_addr(dev->enetaddr)) {
633 printf("\nError: %s address %pM illegal value\n",
634 dev->name, dev->enetaddr);
635 return -EINVAL;
636 }
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000637
Simon Glass7616e782011-06-13 16:13:10 -0700638 ret = dev->write_hwaddr(dev);
Pavel Machek75d9a452014-07-13 10:27:02 +0200639 if (ret)
640 printf("\nWarning: %s failed to set MAC address\n", dev->name);
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000641 }
Simon Glass7616e782011-06-13 16:13:10 -0700642
643 return ret;
644}
645
Simon Glass89d48362011-02-16 11:14:33 -0800646int eth_register(struct eth_device *dev)
647{
648 struct eth_device *d;
Joe Hershberger66c73852012-05-15 08:59:07 +0000649 static int index;
Michal Simek58c583b2011-08-29 23:30:13 +0000650
Mike Frysingerf6add132011-11-10 14:11:04 +0000651 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek58c583b2011-08-29 23:30:13 +0000652
Simon Glass89d48362011-02-16 11:14:33 -0800653 if (!eth_devices) {
654 eth_current = eth_devices = dev;
655 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000656 } else {
Joe Hershberger66c73852012-05-15 08:59:07 +0000657 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundelaba4b692010-03-31 17:56:08 +0200658 ;
wdenkc6097192002-11-03 00:24:07 +0000659 d->next = dev;
660 }
661
662 dev->state = ETH_STATE_INIT;
663 dev->next = eth_devices;
Michael Wallefea7dca2011-10-27 11:31:35 +0000664 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000665
666 return 0;
667}
668
Vincent Palatine7e982d2012-01-09 08:32:36 +0000669int eth_unregister(struct eth_device *dev)
670{
671 struct eth_device *cur;
672
673 /* No device */
674 if (!eth_devices)
Joe Hershberger05324a42015-03-22 17:09:04 -0500675 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000676
677 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
678 cur = cur->next)
679 ;
680
681 /* Device not found */
682 if (cur->next != dev)
Joe Hershberger05324a42015-03-22 17:09:04 -0500683 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000684
685 cur->next = dev->next;
686
687 if (eth_devices == dev)
688 eth_devices = dev->next == eth_devices ? NULL : dev->next;
689
690 if (eth_current == dev) {
691 eth_current = eth_devices;
692 eth_current_changed();
693 }
694
695 return 0;
696}
697
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500698int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000699{
Michael Wallefea7dca2011-10-27 11:31:35 +0000700 int num_devices = 0;
wdenkc6097192002-11-03 00:24:07 +0000701 eth_devices = NULL;
702 eth_current = NULL;
703
Simon Glass770605e2012-02-13 13:51:18 +0000704 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
Alexey Brodkin27ee59a2014-01-10 19:58:11 +0400705#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100706 miiphy_init();
707#endif
Andy Fleming5f184712011-04-08 02:10:27 -0500708
709#ifdef CONFIG_PHYLIB
710 phy_init();
711#endif
712
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500713 eth_env_init();
Mike Frysingerde301222012-04-04 18:53:41 +0000714
Ben Warren8ad25bf2010-08-31 23:05:04 -0700715 /*
716 * If board-specific initialization exists, call it.
717 * If not, call a CPU-specific one
718 */
719 if (board_eth_init != __def_eth_init) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500720 if (board_eth_init(gd->bd) < 0)
Ben Warren8ad25bf2010-08-31 23:05:04 -0700721 printf("Board Net Initialization Failed\n");
722 } else if (cpu_eth_init != __def_eth_init) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500723 if (cpu_eth_init(gd->bd) < 0)
Ben Warren8ad25bf2010-08-31 23:05:04 -0700724 printf("CPU Net Initialization Failed\n");
725 } else
726 printf("Net Initialization Skipped\n");
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100727
wdenkc6097192002-11-03 00:24:07 +0000728 if (!eth_devices) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000729 puts("No ethernet found.\n");
Simon Glass770605e2012-02-13 13:51:18 +0000730 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000731 } else {
732 struct eth_device *dev = eth_devices;
Joe Hershberger66c73852012-05-15 08:59:07 +0000733 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000734
Simon Glass770605e2012-02-13 13:51:18 +0000735 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000736 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000737 if (dev->index)
Joe Hershberger66c73852012-05-15 08:59:07 +0000738 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000739
740 printf("%s", dev->name);
741
Joe Hershberger66c73852012-05-15 08:59:07 +0000742 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000743 eth_current = dev;
Joe Hershberger66c73852012-05-15 08:59:07 +0000744 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000745 }
746
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400747 if (strchr(dev->name, ' '))
Joe Hershberger66c73852012-05-15 08:59:07 +0000748 puts("\nWarning: eth device name has a space!"
749 "\n");
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400750
Pavel Machek75d9a452014-07-13 10:27:02 +0200751 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000752
wdenkc6097192002-11-03 00:24:07 +0000753 dev = dev->next;
Michael Wallefea7dca2011-10-27 11:31:35 +0000754 num_devices++;
Joe Hershberger66c73852012-05-15 08:59:07 +0000755 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000756
Simon Glass89d48362011-02-16 11:14:33 -0800757 eth_current_changed();
Joe Hershberger66c73852012-05-15 08:59:07 +0000758 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000759 }
760
Michael Wallefea7dca2011-10-27 11:31:35 +0000761 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000762}
763
David Updegraff53a5c422007-06-11 10:41:07 -0500764#ifdef CONFIG_MCAST_TFTP
765/* Multicast.
766 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200767 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500768 */
Joe Hershbergerfce69002015-03-22 17:09:05 -0500769int eth_mcast_join(IPaddr_t mcast_ip, int join)
David Updegraff53a5c422007-06-11 10:41:07 -0500770{
Joe Hershberger66c73852012-05-15 08:59:07 +0000771 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200772 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500773 return -1;
774 mcast_mac[5] = htonl(mcast_ip) & 0xff;
775 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
776 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
777 mcast_mac[2] = 0x5e;
778 mcast_mac[1] = 0x0;
779 mcast_mac[0] = 0x1;
780 return eth_current->mcast(eth_current, mcast_mac, join);
781}
782
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200783/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
784 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500785 * some other adapter -- hash tables
786 */
787#define CRCPOLY_LE 0xedb88320
Joe Hershberger66c73852012-05-15 08:59:07 +0000788u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff53a5c422007-06-11 10:41:07 -0500789{
790 int i;
791 u32 crc;
792 crc = ~0;
793 while (len--) {
794 crc ^= *p++;
795 for (i = 0; i < 8; i++)
796 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
797 }
798 /* an reverse the bits, cuz of way they arrive -- last-first */
799 crc = (crc >> 16) | (crc << 16);
800 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
801 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
802 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
803 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
804 return crc;
805}
806
807#endif
808
wdenkc6097192002-11-03 00:24:07 +0000809
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500810int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000811{
Mike Frysinger86848a72009-07-15 21:31:28 -0400812 struct eth_device *old_current, *dev;
wdenkc6097192002-11-03 00:24:07 +0000813
Stefan Roese6bc11382008-03-04 17:40:41 +0100814 if (!eth_current) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000815 puts("No ethernet found.\n");
Joe Hershberger05324a42015-03-22 17:09:04 -0500816 return -ENODEV;
Stefan Roese6bc11382008-03-04 17:40:41 +0100817 }
wdenkc6097192002-11-03 00:24:07 +0000818
Mike Frysinger86848a72009-07-15 21:31:28 -0400819 /* Sync environment with network devices */
Mike Frysinger86848a72009-07-15 21:31:28 -0400820 dev = eth_devices;
821 do {
822 uchar env_enetaddr[6];
823
Michael Wallefea7dca2011-10-27 11:31:35 +0000824 if (eth_getenv_enetaddr_by_index("eth", dev->index,
Simon Glass7616e782011-06-13 16:13:10 -0700825 env_enetaddr))
Mike Frysinger86848a72009-07-15 21:31:28 -0400826 memcpy(dev->enetaddr, env_enetaddr, 6);
827
Mike Frysinger86848a72009-07-15 21:31:28 -0400828 dev = dev->next;
829 } while (dev != eth_devices);
830
wdenkc6097192002-11-03 00:24:07 +0000831 old_current = eth_current;
832 do {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400833 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000834
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500835 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000836 eth_current->state = ETH_STATE_ACTIVE;
837
Upakul Barkakaty505be872007-11-29 12:16:13 +0530838 return 0;
wdenkc6097192002-11-03 00:24:07 +0000839 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400840 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000841
842 eth_try_another(0);
843 } while (old_current != eth_current);
844
Joe Hershberger05324a42015-03-22 17:09:04 -0500845 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000846}
847
848void eth_halt(void)
849{
850 if (!eth_current)
851 return;
852
853 eth_current->halt(eth_current);
854
855 eth_current->state = ETH_STATE_PASSIVE;
856}
857
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000858int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000859{
860 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500861 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000862
863 return eth_current->send(eth_current, packet, length);
864}
865
866int eth_rx(void)
867{
868 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500869 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000870
871 return eth_current->recv(eth_current);
872}
Joe Hershberger05c3e682015-03-22 17:09:10 -0500873#endif /* ifndef CONFIG_DM_ETH */
wdenkc6097192002-11-03 00:24:07 +0000874
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100875#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000876static void eth_save_packet(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100877{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000878 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100879 int i;
880
881 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
882 return;
883
884 if (PKTSIZE < length)
885 return;
886
887 for (i = 0; i < length; i++)
888 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
889
890 eth_rcv_bufs[eth_rcv_last].length = length;
891 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
892}
893
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000894int eth_receive(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100895{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000896 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100897 void *pp = push_packet;
898 int i;
899
900 if (eth_rcv_current == eth_rcv_last) {
901 push_packet = eth_save_packet;
902 eth_rx();
903 push_packet = pp;
904
905 if (eth_rcv_current == eth_rcv_last)
906 return -1;
907 }
908
Michael Walle46c07bc2012-06-22 11:24:28 +0000909 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100910
911 for (i = 0; i < length; i++)
912 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
913
914 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
915 return length;
916}
917#endif /* CONFIG_API */
918
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500919static void eth_current_changed(void)
920{
921 char *act = getenv("ethact");
922 /* update current ethernet name */
923 if (eth_get_dev()) {
924 if (act == NULL || strcmp(act, eth_get_name()) != 0)
925 setenv("ethact", eth_get_name());
926 }
927 /*
928 * remove the variable completely if there is no active
929 * interface
930 */
931 else if (act != NULL)
932 setenv("ethact", NULL);
933}
934
wdenkc6097192002-11-03 00:24:07 +0000935void eth_try_another(int first_restart)
936{
Joe Hershberger05c3e682015-03-22 17:09:10 -0500937 static void *first_failed;
Remy Bohmerc650e1b2011-02-19 20:15:14 +0100938 char *ethrotate;
Matthias Fuchse1692572008-01-17 07:45:05 +0100939
940 /*
941 * Do not rotate between network interfaces when
942 * 'ethrotate' variable is set to 'no'.
943 */
Joe Hershberger66c73852012-05-15 08:59:07 +0000944 ethrotate = getenv("ethrotate");
945 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchse1692572008-01-17 07:45:05 +0100946 return;
wdenkc6097192002-11-03 00:24:07 +0000947
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500948 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +0000949 return;
950
Joe Hershberger66c73852012-05-15 08:59:07 +0000951 if (first_restart)
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500952 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +0000953
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500954 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +0000955
Simon Glass89d48362011-02-16 11:14:33 -0800956 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +0000957
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500958 if (first_failed == eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +0000959 NetRestartWrap = 1;
wdenkc6097192002-11-03 00:24:07 +0000960}
961
wdenka3d991b2004-04-15 21:48:45 +0000962void eth_set_current(void)
963{
Joe Hershberger66c73852012-05-15 08:59:07 +0000964 static char *act;
965 static int env_changed_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100966 int env_id;
wdenka3d991b2004-04-15 21:48:45 +0000967
Heiko Schocher2f70c492009-02-10 09:38:52 +0100968 env_id = get_env_id();
969 if ((act == NULL) || (env_changed_id != env_id)) {
970 act = getenv("ethact");
971 env_changed_id = env_id;
972 }
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500973
974 if (act == NULL) {
975 char *ethprime = getenv("ethprime");
976 void *dev = NULL;
977
978 if (ethprime)
979 dev = eth_get_dev_by_name(ethprime);
980 if (dev)
981 eth_set_dev(dev);
982 else
983 eth_set_dev(NULL);
984 } else {
Joe Hershbergere58780d2015-03-22 17:09:16 -0500985 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500986 }
wdenka3d991b2004-04-15 21:48:45 +0000987
Simon Glass89d48362011-02-16 11:14:33 -0800988 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +0000989}
wdenka3d991b2004-04-15 21:48:45 +0000990
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500991const char *eth_get_name(void)
wdenk5bb226e2003-11-17 21:14:37 +0000992{
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500993 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenk5bb226e2003-11-17 21:14:37 +0000994}