blob: 8e6acfef89c25170f79728aad5e1ab3c9d7445b4 [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);
Joe Hershberger0adb5b72015-04-08 01:41:04 -050035 return is_valid_ethaddr(enetaddr);
Mike Frysinger3f6e6992009-01-29 19:43:44 -050036}
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)
Joe Hershberger14111572015-04-08 01:41:02 -050069 copy_filename(net_boot_file_name, s,
70 sizeof(net_boot_file_name));
Joe Hershberger84eb1fb2015-03-22 17:09:03 -050071}
Rob Herringc88ef3c2012-04-14 18:06:49 +000072
Ben Warrenecee9322010-04-26 11:11:46 -070073static int eth_mac_skip(int index)
74{
75 char enetvar[15];
76 char *skip_state;
Joe Hershbergerff819a32015-04-08 01:41:19 -050077
Ben Warrenecee9322010-04-26 11:11:46 -070078 sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index);
Joe Hershbergerff819a32015-04-08 01:41:19 -050079 skip_state = getenv(enetvar);
80 return skip_state != NULL;
Ben Warrenecee9322010-04-26 11:11:46 -070081}
82
Joe Hershberger84eb1fb2015-03-22 17:09:03 -050083static void eth_current_changed(void);
84
Simon Glass3bc42702015-04-05 16:07:37 -060085/*
86 * CPU and board-specific Ethernet initializations. Aliased function
87 * signals caller to move on
88 */
89static int __def_eth_init(bd_t *bis)
90{
91 return -1;
92}
93int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
94int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
95
96static void eth_common_init(void)
97{
98 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
99#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
100 miiphy_init();
101#endif
102
103#ifdef CONFIG_PHYLIB
104 phy_init();
105#endif
106
107 eth_env_init();
108
109 /*
110 * If board-specific initialization exists, call it.
111 * If not, call a CPU-specific one
112 */
113 if (board_eth_init != __def_eth_init) {
114 if (board_eth_init(gd->bd) < 0)
115 printf("Board Net Initialization Failed\n");
116 } else if (cpu_eth_init != __def_eth_init) {
117 if (cpu_eth_init(gd->bd) < 0)
118 printf("CPU Net Initialization Failed\n");
119 } else {
120 printf("Net Initialization Skipped\n");
121 }
122}
123
Joe Hershberger05c3e682015-03-22 17:09:10 -0500124#ifdef CONFIG_DM_ETH
125/**
126 * struct eth_device_priv - private structure for each Ethernet device
127 *
128 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
129 */
130struct eth_device_priv {
131 enum eth_state_t state;
132};
133
134/**
135 * struct eth_uclass_priv - The structure attached to the uclass itself
136 *
137 * @current: The Ethernet device that the network functions are using
138 */
139struct eth_uclass_priv {
140 struct udevice *current;
141};
142
Joe Hershberger60304592015-03-22 17:09:24 -0500143/* eth_errno - This stores the most recent failure code from DM functions */
144static int eth_errno;
145
Joe Hershberger05c3e682015-03-22 17:09:10 -0500146static struct eth_uclass_priv *eth_get_uclass_priv(void)
147{
148 struct uclass *uc;
149
150 uclass_get(UCLASS_ETH, &uc);
151 assert(uc);
152 return uc->priv;
153}
154
155static void eth_set_current_to_next(void)
156{
157 struct eth_uclass_priv *uc_priv;
158
159 uc_priv = eth_get_uclass_priv();
160 if (uc_priv->current)
161 uclass_next_device(&uc_priv->current);
162 if (!uc_priv->current)
163 uclass_first_device(UCLASS_ETH, &uc_priv->current);
164}
165
Joe Hershberger60304592015-03-22 17:09:24 -0500166/*
167 * Typically this will simply return the active device.
168 * In the case where the most recent active device was unset, this will attempt
169 * to return the first device. If that device doesn't exist or fails to probe,
170 * this function will return NULL.
171 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500172struct udevice *eth_get_dev(void)
173{
174 struct eth_uclass_priv *uc_priv;
175
176 uc_priv = eth_get_uclass_priv();
177 if (!uc_priv->current)
Joe Hershberger60304592015-03-22 17:09:24 -0500178 eth_errno = uclass_first_device(UCLASS_ETH,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500179 &uc_priv->current);
180 return uc_priv->current;
181}
182
Joe Hershberger60304592015-03-22 17:09:24 -0500183/*
184 * Typically this will just store a device pointer.
185 * In case it was not probed, we will attempt to do so.
186 * dev may be NULL to unset the active device.
187 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500188static void eth_set_dev(struct udevice *dev)
189{
Joe Hershberger60304592015-03-22 17:09:24 -0500190 if (dev && !device_active(dev))
191 eth_errno = device_probe(dev);
Joe Hershberger05c3e682015-03-22 17:09:10 -0500192 eth_get_uclass_priv()->current = dev;
193}
194
Joe Hershbergere58780d2015-03-22 17:09:16 -0500195/*
196 * Find the udevice that either has the name passed in as devname or has an
197 * alias named devname.
198 */
199struct udevice *eth_get_dev_by_name(const char *devname)
200{
201 int seq = -1;
202 char *endp = NULL;
203 const char *startp = NULL;
204 struct udevice *it;
205 struct uclass *uc;
206
207 /* Must be longer than 3 to be an alias */
208 if (strlen(devname) > strlen("eth")) {
209 startp = devname + strlen("eth");
210 seq = simple_strtoul(startp, &endp, 10);
211 }
212
213 uclass_get(UCLASS_ETH, &uc);
214 uclass_foreach_dev(it, uc) {
Joe Hershberger60304592015-03-22 17:09:24 -0500215 /*
216 * We need the seq to be valid, so try to probe it.
217 * If the probe fails, the seq will not match since it will be
218 * -1 instead of what we are looking for.
219 * We don't care about errors from probe here. Either they won't
220 * match an alias or it will match a literal name and we'll pick
221 * up the error when we try to probe again in eth_set_dev().
222 */
Joe Hershbergere58780d2015-03-22 17:09:16 -0500223 device_probe(it);
224 /*
225 * Check for the name or the sequence number to match
226 */
227 if (strcmp(it->name, devname) == 0 ||
228 (endp > startp && it->seq == seq))
229 return it;
230 }
231
232 return NULL;
233}
234
Joe Hershberger05c3e682015-03-22 17:09:10 -0500235unsigned char *eth_get_ethaddr(void)
236{
237 struct eth_pdata *pdata;
238
239 if (eth_get_dev()) {
240 pdata = eth_get_dev()->platdata;
241 return pdata->enetaddr;
242 }
243
244 return NULL;
245}
246
247/* Set active state without calling start on the driver */
248int eth_init_state_only(void)
249{
250 struct udevice *current;
251 struct eth_device_priv *priv;
252
253 current = eth_get_dev();
254 if (!current || !device_active(current))
255 return -EINVAL;
256
257 priv = current->uclass_priv;
258 priv->state = ETH_STATE_ACTIVE;
259
260 return 0;
261}
262
263/* Set passive state without calling stop on the driver */
264void eth_halt_state_only(void)
265{
266 struct udevice *current;
267 struct eth_device_priv *priv;
268
269 current = eth_get_dev();
270 if (!current || !device_active(current))
271 return;
272
273 priv = current->uclass_priv;
274 priv->state = ETH_STATE_PASSIVE;
275}
276
277int eth_get_dev_index(void)
278{
279 if (eth_get_dev())
280 return eth_get_dev()->seq;
281 return -1;
282}
283
284int eth_init(void)
285{
286 struct udevice *current;
287 struct udevice *old_current;
Joe Hershberger60304592015-03-22 17:09:24 -0500288 int ret = -ENODEV;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500289
290 current = eth_get_dev();
291 if (!current) {
292 printf("No ethernet found.\n");
293 return -ENODEV;
294 }
295
296 old_current = current;
297 do {
298 debug("Trying %s\n", current->name);
299
300 if (device_active(current)) {
301 uchar env_enetaddr[6];
302 struct eth_pdata *pdata = current->platdata;
303
304 /* Sync environment with network device */
305 if (eth_getenv_enetaddr_by_index("eth", current->seq,
306 env_enetaddr))
307 memcpy(pdata->enetaddr, env_enetaddr, 6);
308 else
309 memset(pdata->enetaddr, 0, 6);
310
Joe Hershberger60304592015-03-22 17:09:24 -0500311 ret = eth_get_ops(current)->start(current);
312 if (ret >= 0) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500313 struct eth_device_priv *priv =
314 current->uclass_priv;
315
316 priv->state = ETH_STATE_ACTIVE;
317 return 0;
318 }
Joe Hershbergerff819a32015-04-08 01:41:19 -0500319 } else {
Joe Hershberger60304592015-03-22 17:09:24 -0500320 ret = eth_errno;
Joe Hershbergerff819a32015-04-08 01:41:19 -0500321 }
Joe Hershberger60304592015-03-22 17:09:24 -0500322
Joe Hershberger05c3e682015-03-22 17:09:10 -0500323 debug("FAIL\n");
324
Joe Hershberger60304592015-03-22 17:09:24 -0500325 /*
326 * If ethrotate is enabled, this will change "current",
327 * otherwise we will drop out of this while loop immediately
328 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500329 eth_try_another(0);
Joe Hershberger60304592015-03-22 17:09:24 -0500330 /* This will ensure the new "current" attempted to probe */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500331 current = eth_get_dev();
332 } while (old_current != current);
333
Joe Hershberger60304592015-03-22 17:09:24 -0500334 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500335}
336
337void eth_halt(void)
338{
339 struct udevice *current;
340 struct eth_device_priv *priv;
341
342 current = eth_get_dev();
343 if (!current || !device_active(current))
344 return;
345
346 eth_get_ops(current)->stop(current);
347 priv = current->uclass_priv;
348 priv->state = ETH_STATE_PASSIVE;
349}
350
351int eth_send(void *packet, int length)
352{
353 struct udevice *current;
Joe Hershberger60304592015-03-22 17:09:24 -0500354 int ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500355
356 current = eth_get_dev();
357 if (!current)
358 return -ENODEV;
359
360 if (!device_active(current))
361 return -EINVAL;
362
Joe Hershberger60304592015-03-22 17:09:24 -0500363 ret = eth_get_ops(current)->send(current, packet, length);
364 if (ret < 0) {
365 /* We cannot completely return the error at present */
366 debug("%s: send() returned error %d\n", __func__, ret);
367 }
368 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500369}
370
371int eth_rx(void)
372{
373 struct udevice *current;
Joe Hershberger17591402015-03-22 17:09:12 -0500374 uchar *packet;
375 int ret;
376 int i;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500377
378 current = eth_get_dev();
379 if (!current)
380 return -ENODEV;
381
382 if (!device_active(current))
383 return -EINVAL;
384
Joe Hershberger17591402015-03-22 17:09:12 -0500385 /* Process up to 32 packets at one time */
386 for (i = 0; i < 32; i++) {
387 ret = eth_get_ops(current)->recv(current, &packet);
388 if (ret > 0)
389 net_process_received_packet(packet, ret);
Joe Hershberger63c97292015-04-03 20:09:46 -0500390 if (ret >= 0 && eth_get_ops(current)->free_pkt)
391 eth_get_ops(current)->free_pkt(current, packet, ret);
392 if (ret <= 0)
Joe Hershberger17591402015-03-22 17:09:12 -0500393 break;
394 }
395 if (ret == -EAGAIN)
396 ret = 0;
Joe Hershberger60304592015-03-22 17:09:24 -0500397 if (ret < 0) {
398 /* We cannot completely return the error at present */
399 debug("%s: recv() returned error %d\n", __func__, ret);
400 }
Joe Hershberger17591402015-03-22 17:09:12 -0500401 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500402}
403
404static int eth_write_hwaddr(struct udevice *dev)
405{
406 struct eth_pdata *pdata = dev->platdata;
407 int ret = 0;
408
409 if (!dev || !device_active(dev))
410 return -EINVAL;
411
412 /* seq is valid since the device is active */
413 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500414 if (!is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500415 printf("\nError: %s address %pM illegal value\n",
416 dev->name, pdata->enetaddr);
417 return -EINVAL;
418 }
419
420 ret = eth_get_ops(dev)->write_hwaddr(dev);
421 if (ret)
422 printf("\nWarning: %s failed to set MAC address\n",
423 dev->name);
424 }
425
426 return ret;
427}
428
429int eth_initialize(void)
430{
431 int num_devices = 0;
432 struct udevice *dev;
433
Simon Glass3bc42702015-04-05 16:07:37 -0600434 eth_common_init();
Joe Hershberger05c3e682015-03-22 17:09:10 -0500435
436 /*
437 * Devices need to write the hwaddr even if not started so that Linux
438 * will have access to the hwaddr that u-boot stored for the device.
439 * This is accomplished by attempting to probe each device and calling
440 * their write_hwaddr() operation.
441 */
442 uclass_first_device(UCLASS_ETH, &dev);
443 if (!dev) {
444 printf("No ethernet found.\n");
445 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
446 } else {
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500447 char *ethprime = getenv("ethprime");
448 struct udevice *prime_dev = NULL;
449
450 if (ethprime)
451 prime_dev = eth_get_dev_by_name(ethprime);
452 if (prime_dev) {
453 eth_set_dev(prime_dev);
454 eth_current_changed();
455 } else {
456 eth_set_dev(NULL);
457 }
458
Joe Hershberger05c3e682015-03-22 17:09:10 -0500459 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
460 do {
461 if (num_devices)
462 printf(", ");
463
464 printf("eth%d: %s", dev->seq, dev->name);
465
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500466 if (ethprime && dev == prime_dev)
467 printf(" [PRIME]");
468
Joe Hershberger05c3e682015-03-22 17:09:10 -0500469 eth_write_hwaddr(dev);
470
471 uclass_next_device(&dev);
472 num_devices++;
473 } while (dev);
474
475 putc('\n');
476 }
477
478 return num_devices;
479}
480
481static int eth_post_bind(struct udevice *dev)
482{
483 if (strchr(dev->name, ' ')) {
484 printf("\nError: eth device name \"%s\" has a space!\n",
485 dev->name);
486 return -EINVAL;
487 }
488
489 return 0;
490}
491
492static int eth_pre_unbind(struct udevice *dev)
493{
494 /* Don't hang onto a pointer that is going away */
495 if (dev == eth_get_uclass_priv()->current)
496 eth_set_dev(NULL);
497
498 return 0;
499}
500
501static int eth_post_probe(struct udevice *dev)
502{
503 struct eth_device_priv *priv = dev->uclass_priv;
504 struct eth_pdata *pdata = dev->platdata;
505 unsigned char env_enetaddr[6];
506
507 priv->state = ETH_STATE_INIT;
508
509 /* Check if the device has a MAC address in ROM */
510 if (eth_get_ops(dev)->read_rom_hwaddr)
511 eth_get_ops(dev)->read_rom_hwaddr(dev);
512
513 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500514 if (!is_zero_ethaddr(env_enetaddr)) {
515 if (!is_zero_ethaddr(pdata->enetaddr) &&
Joe Hershberger05c3e682015-03-22 17:09:10 -0500516 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
517 printf("\nWarning: %s MAC addresses don't match:\n",
518 dev->name);
519 printf("Address in SROM is %pM\n",
520 pdata->enetaddr);
521 printf("Address in environment is %pM\n",
522 env_enetaddr);
523 }
524
525 /* Override the ROM MAC address */
526 memcpy(pdata->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500527 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500528 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
529 printf("\nWarning: %s using MAC address from ROM\n",
530 dev->name);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500531 } else if (is_zero_ethaddr(pdata->enetaddr)) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500532 printf("\nError: %s address not set.\n",
533 dev->name);
534 return -EINVAL;
535 }
536
537 return 0;
538}
539
540static int eth_pre_remove(struct udevice *dev)
541{
542 eth_get_ops(dev)->stop(dev);
543
544 return 0;
545}
546
547UCLASS_DRIVER(eth) = {
548 .name = "eth",
549 .id = UCLASS_ETH,
550 .post_bind = eth_post_bind,
551 .pre_unbind = eth_pre_unbind,
552 .post_probe = eth_post_probe,
553 .pre_remove = eth_pre_remove,
554 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
555 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershbergere58780d2015-03-22 17:09:16 -0500556 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500557};
558#endif
559
560#ifndef CONFIG_DM_ETH
Ben Warrendd354792008-06-23 22:57:27 -0700561
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100562#ifdef CONFIG_API
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100563static struct {
564 uchar data[PKTSIZE];
565 int length;
566} eth_rcv_bufs[PKTBUFSRX];
567
Joe Hershberger66c73852012-05-15 08:59:07 +0000568static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100569#endif
570
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000571static struct eth_device *eth_devices;
572struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000573
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500574static void eth_set_current_to_next(void)
575{
576 eth_current = eth_current->next;
577}
578
Joe Hershbergere58780d2015-03-22 17:09:16 -0500579static void eth_set_dev(struct eth_device *dev)
580{
581 eth_current = dev;
582}
583
Ben Warrend7fb9bc2010-07-29 12:56:11 -0700584struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200585{
586 struct eth_device *dev, *target_dev;
587
Helmut Raiger7e7f9032011-08-22 00:17:17 +0000588 BUG_ON(devname == NULL);
589
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200590 if (!eth_devices)
591 return NULL;
592
593 dev = eth_devices;
594 target_dev = NULL;
595 do {
596 if (strcmp(devname, dev->name) == 0) {
597 target_dev = dev;
598 break;
599 }
600 dev = dev->next;
601 } while (dev != eth_devices);
602
603 return target_dev;
604}
605
Andy Fleming9e569862009-02-11 15:07:24 -0600606struct eth_device *eth_get_dev_by_index(int index)
607{
608 struct eth_device *dev, *target_dev;
Andy Fleming9e569862009-02-11 15:07:24 -0600609
610 if (!eth_devices)
611 return NULL;
612
613 dev = eth_devices;
614 target_dev = NULL;
615 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000616 if (dev->index == index) {
Andy Fleming9e569862009-02-11 15:07:24 -0600617 target_dev = dev;
618 break;
619 }
620 dev = dev->next;
Andy Fleming9e569862009-02-11 15:07:24 -0600621 } while (dev != eth_devices);
622
623 return target_dev;
624}
625
Joe Hershberger66c73852012-05-15 08:59:07 +0000626int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000627{
Joe Hershberger66c73852012-05-15 08:59:07 +0000628 if (!eth_current)
Michael Wallefea7dca2011-10-27 11:31:35 +0000629 return -1;
wdenkc6097192002-11-03 00:24:07 +0000630
Michael Wallefea7dca2011-10-27 11:31:35 +0000631 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000632}
633
Simon Glass7616e782011-06-13 16:13:10 -0700634int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
635 int eth_number)
636{
637 unsigned char env_enetaddr[6];
638 int ret = 0;
639
Eric Miao69376642012-01-18 22:56:33 +0000640 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700641
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500642 if (!is_zero_ethaddr(env_enetaddr)) {
643 if (!is_zero_ethaddr(dev->enetaddr) &&
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500644 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass7616e782011-06-13 16:13:10 -0700645 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500646 dev->name);
Simon Glass7616e782011-06-13 16:13:10 -0700647 printf("Address in SROM is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500648 dev->enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700649 printf("Address in environment is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500650 env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700651 }
652
653 memcpy(dev->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500654 } else if (is_valid_ethaddr(dev->enetaddr)) {
Rob Herringc88ef3c2012-04-14 18:06:49 +0000655 eth_setenv_enetaddr_by_index(base_name, eth_number,
656 dev->enetaddr);
657 printf("\nWarning: %s using MAC address from net device\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500658 dev->name);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500659 } else if (is_zero_ethaddr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200660 printf("\nError: %s address not set.\n",
661 dev->name);
662 return -EINVAL;
Simon Glass7616e782011-06-13 16:13:10 -0700663 }
664
Pavel Machek75d9a452014-07-13 10:27:02 +0200665 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500666 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200667 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500668 dev->name, dev->enetaddr);
Pavel Machek75d9a452014-07-13 10:27:02 +0200669 return -EINVAL;
670 }
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000671
Simon Glass7616e782011-06-13 16:13:10 -0700672 ret = dev->write_hwaddr(dev);
Pavel Machek75d9a452014-07-13 10:27:02 +0200673 if (ret)
Joe Hershbergerff819a32015-04-08 01:41:19 -0500674 printf("\nWarning: %s failed to set MAC address\n",
675 dev->name);
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000676 }
Simon Glass7616e782011-06-13 16:13:10 -0700677
678 return ret;
679}
680
Simon Glass89d48362011-02-16 11:14:33 -0800681int eth_register(struct eth_device *dev)
682{
683 struct eth_device *d;
Joe Hershberger66c73852012-05-15 08:59:07 +0000684 static int index;
Michal Simek58c583b2011-08-29 23:30:13 +0000685
Mike Frysingerf6add132011-11-10 14:11:04 +0000686 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek58c583b2011-08-29 23:30:13 +0000687
Simon Glass89d48362011-02-16 11:14:33 -0800688 if (!eth_devices) {
Joe Hershbergerff819a32015-04-08 01:41:19 -0500689 eth_devices = dev;
690 eth_current = dev;
Simon Glass89d48362011-02-16 11:14:33 -0800691 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000692 } else {
Joe Hershberger66c73852012-05-15 08:59:07 +0000693 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundelaba4b692010-03-31 17:56:08 +0200694 ;
wdenkc6097192002-11-03 00:24:07 +0000695 d->next = dev;
696 }
697
698 dev->state = ETH_STATE_INIT;
699 dev->next = eth_devices;
Michael Wallefea7dca2011-10-27 11:31:35 +0000700 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000701
702 return 0;
703}
704
Vincent Palatine7e982d2012-01-09 08:32:36 +0000705int eth_unregister(struct eth_device *dev)
706{
707 struct eth_device *cur;
708
709 /* No device */
710 if (!eth_devices)
Joe Hershberger05324a42015-03-22 17:09:04 -0500711 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000712
713 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
714 cur = cur->next)
715 ;
716
717 /* Device not found */
718 if (cur->next != dev)
Joe Hershberger05324a42015-03-22 17:09:04 -0500719 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000720
721 cur->next = dev->next;
722
723 if (eth_devices == dev)
724 eth_devices = dev->next == eth_devices ? NULL : dev->next;
725
726 if (eth_current == dev) {
727 eth_current = eth_devices;
728 eth_current_changed();
729 }
730
731 return 0;
732}
733
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500734int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000735{
Michael Wallefea7dca2011-10-27 11:31:35 +0000736 int num_devices = 0;
Simon Glass3bc42702015-04-05 16:07:37 -0600737
wdenkc6097192002-11-03 00:24:07 +0000738 eth_devices = NULL;
739 eth_current = NULL;
Simon Glass3bc42702015-04-05 16:07:37 -0600740 eth_common_init();
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100741
wdenkc6097192002-11-03 00:24:07 +0000742 if (!eth_devices) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000743 puts("No ethernet found.\n");
Simon Glass770605e2012-02-13 13:51:18 +0000744 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000745 } else {
746 struct eth_device *dev = eth_devices;
Joe Hershberger66c73852012-05-15 08:59:07 +0000747 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000748
Simon Glass770605e2012-02-13 13:51:18 +0000749 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000750 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000751 if (dev->index)
Joe Hershberger66c73852012-05-15 08:59:07 +0000752 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000753
754 printf("%s", dev->name);
755
Joe Hershberger66c73852012-05-15 08:59:07 +0000756 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000757 eth_current = dev;
Joe Hershberger66c73852012-05-15 08:59:07 +0000758 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000759 }
760
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400761 if (strchr(dev->name, ' '))
Joe Hershberger66c73852012-05-15 08:59:07 +0000762 puts("\nWarning: eth device name has a space!"
763 "\n");
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400764
Pavel Machek75d9a452014-07-13 10:27:02 +0200765 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000766
wdenkc6097192002-11-03 00:24:07 +0000767 dev = dev->next;
Michael Wallefea7dca2011-10-27 11:31:35 +0000768 num_devices++;
Joe Hershberger66c73852012-05-15 08:59:07 +0000769 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000770
Simon Glass89d48362011-02-16 11:14:33 -0800771 eth_current_changed();
Joe Hershberger66c73852012-05-15 08:59:07 +0000772 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000773 }
774
Michael Wallefea7dca2011-10-27 11:31:35 +0000775 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000776}
777
David Updegraff53a5c422007-06-11 10:41:07 -0500778#ifdef CONFIG_MCAST_TFTP
779/* Multicast.
780 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200781 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500782 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500783int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff53a5c422007-06-11 10:41:07 -0500784{
Joe Hershberger66c73852012-05-15 08:59:07 +0000785 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200786 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500787 return -1;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500788 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
789 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
790 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff53a5c422007-06-11 10:41:07 -0500791 mcast_mac[2] = 0x5e;
792 mcast_mac[1] = 0x0;
793 mcast_mac[0] = 0x1;
794 return eth_current->mcast(eth_current, mcast_mac, join);
795}
796
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200797/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
798 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500799 * some other adapter -- hash tables
800 */
801#define CRCPOLY_LE 0xedb88320
Joe Hershberger66c73852012-05-15 08:59:07 +0000802u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff53a5c422007-06-11 10:41:07 -0500803{
804 int i;
805 u32 crc;
806 crc = ~0;
807 while (len--) {
808 crc ^= *p++;
809 for (i = 0; i < 8; i++)
810 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
811 }
812 /* an reverse the bits, cuz of way they arrive -- last-first */
813 crc = (crc >> 16) | (crc << 16);
814 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
815 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
816 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
817 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
818 return crc;
819}
820
821#endif
822
wdenkc6097192002-11-03 00:24:07 +0000823
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500824int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000825{
Mike Frysinger86848a72009-07-15 21:31:28 -0400826 struct eth_device *old_current, *dev;
wdenkc6097192002-11-03 00:24:07 +0000827
Stefan Roese6bc11382008-03-04 17:40:41 +0100828 if (!eth_current) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000829 puts("No ethernet found.\n");
Joe Hershberger05324a42015-03-22 17:09:04 -0500830 return -ENODEV;
Stefan Roese6bc11382008-03-04 17:40:41 +0100831 }
wdenkc6097192002-11-03 00:24:07 +0000832
Mike Frysinger86848a72009-07-15 21:31:28 -0400833 /* Sync environment with network devices */
Mike Frysinger86848a72009-07-15 21:31:28 -0400834 dev = eth_devices;
835 do {
836 uchar env_enetaddr[6];
837
Michael Wallefea7dca2011-10-27 11:31:35 +0000838 if (eth_getenv_enetaddr_by_index("eth", dev->index,
Simon Glass7616e782011-06-13 16:13:10 -0700839 env_enetaddr))
Mike Frysinger86848a72009-07-15 21:31:28 -0400840 memcpy(dev->enetaddr, env_enetaddr, 6);
841
Mike Frysinger86848a72009-07-15 21:31:28 -0400842 dev = dev->next;
843 } while (dev != eth_devices);
844
wdenkc6097192002-11-03 00:24:07 +0000845 old_current = eth_current;
846 do {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400847 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000848
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500849 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000850 eth_current->state = ETH_STATE_ACTIVE;
851
Upakul Barkakaty505be872007-11-29 12:16:13 +0530852 return 0;
wdenkc6097192002-11-03 00:24:07 +0000853 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400854 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000855
856 eth_try_another(0);
857 } while (old_current != eth_current);
858
Joe Hershberger05324a42015-03-22 17:09:04 -0500859 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000860}
861
862void eth_halt(void)
863{
864 if (!eth_current)
865 return;
866
867 eth_current->halt(eth_current);
868
869 eth_current->state = ETH_STATE_PASSIVE;
870}
871
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000872int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000873{
874 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500875 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000876
877 return eth_current->send(eth_current, packet, length);
878}
879
880int eth_rx(void)
881{
882 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500883 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000884
885 return eth_current->recv(eth_current);
886}
Joe Hershberger05c3e682015-03-22 17:09:10 -0500887#endif /* ifndef CONFIG_DM_ETH */
wdenkc6097192002-11-03 00:24:07 +0000888
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100889#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000890static void eth_save_packet(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100891{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000892 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100893 int i;
894
895 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
896 return;
897
898 if (PKTSIZE < length)
899 return;
900
901 for (i = 0; i < length; i++)
902 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
903
904 eth_rcv_bufs[eth_rcv_last].length = length;
905 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
906}
907
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000908int eth_receive(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100909{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000910 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100911 void *pp = push_packet;
912 int i;
913
914 if (eth_rcv_current == eth_rcv_last) {
915 push_packet = eth_save_packet;
916 eth_rx();
917 push_packet = pp;
918
919 if (eth_rcv_current == eth_rcv_last)
920 return -1;
921 }
922
Michael Walle46c07bc2012-06-22 11:24:28 +0000923 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100924
925 for (i = 0; i < length; i++)
926 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
927
928 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
929 return length;
930}
931#endif /* CONFIG_API */
932
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500933static void eth_current_changed(void)
934{
935 char *act = getenv("ethact");
936 /* update current ethernet name */
937 if (eth_get_dev()) {
938 if (act == NULL || strcmp(act, eth_get_name()) != 0)
939 setenv("ethact", eth_get_name());
940 }
941 /*
942 * remove the variable completely if there is no active
943 * interface
944 */
945 else if (act != NULL)
946 setenv("ethact", NULL);
947}
948
wdenkc6097192002-11-03 00:24:07 +0000949void eth_try_another(int first_restart)
950{
Joe Hershberger05c3e682015-03-22 17:09:10 -0500951 static void *first_failed;
Remy Bohmerc650e1b2011-02-19 20:15:14 +0100952 char *ethrotate;
Matthias Fuchse1692572008-01-17 07:45:05 +0100953
954 /*
955 * Do not rotate between network interfaces when
956 * 'ethrotate' variable is set to 'no'.
957 */
Joe Hershberger66c73852012-05-15 08:59:07 +0000958 ethrotate = getenv("ethrotate");
959 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchse1692572008-01-17 07:45:05 +0100960 return;
wdenkc6097192002-11-03 00:24:07 +0000961
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500962 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +0000963 return;
964
Joe Hershberger66c73852012-05-15 08:59:07 +0000965 if (first_restart)
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500966 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +0000967
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500968 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +0000969
Simon Glass89d48362011-02-16 11:14:33 -0800970 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +0000971
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500972 if (first_failed == eth_get_dev())
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500973 net_restart_wrap = 1;
wdenkc6097192002-11-03 00:24:07 +0000974}
975
wdenka3d991b2004-04-15 21:48:45 +0000976void eth_set_current(void)
977{
Joe Hershberger66c73852012-05-15 08:59:07 +0000978 static char *act;
979 static int env_changed_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100980 int env_id;
wdenka3d991b2004-04-15 21:48:45 +0000981
Heiko Schocher2f70c492009-02-10 09:38:52 +0100982 env_id = get_env_id();
983 if ((act == NULL) || (env_changed_id != env_id)) {
984 act = getenv("ethact");
985 env_changed_id = env_id;
986 }
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500987
988 if (act == NULL) {
989 char *ethprime = getenv("ethprime");
990 void *dev = NULL;
991
992 if (ethprime)
993 dev = eth_get_dev_by_name(ethprime);
994 if (dev)
995 eth_set_dev(dev);
996 else
997 eth_set_dev(NULL);
998 } else {
Joe Hershbergere58780d2015-03-22 17:09:16 -0500999 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershberger6536b9b2015-03-22 17:09:17 -05001000 }
wdenka3d991b2004-04-15 21:48:45 +00001001
Simon Glass89d48362011-02-16 11:14:33 -08001002 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +00001003}
wdenka3d991b2004-04-15 21:48:45 +00001004
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001005const char *eth_get_name(void)
wdenk5bb226e2003-11-17 21:14:37 +00001006{
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001007 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenk5bb226e2003-11-17 21:14:37 +00001008}