blob: da97095087c1575fbbb9b0d02170ccef615f10cb [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
Joe Hershberger05c3e682015-03-22 17:09:10 -050085#ifdef CONFIG_DM_ETH
86/**
87 * struct eth_device_priv - private structure for each Ethernet device
88 *
89 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
90 */
91struct eth_device_priv {
92 enum eth_state_t state;
93};
94
95/**
96 * struct eth_uclass_priv - The structure attached to the uclass itself
97 *
98 * @current: The Ethernet device that the network functions are using
99 */
100struct eth_uclass_priv {
101 struct udevice *current;
102};
103
Joe Hershberger60304592015-03-22 17:09:24 -0500104/* eth_errno - This stores the most recent failure code from DM functions */
105static int eth_errno;
106
Joe Hershberger05c3e682015-03-22 17:09:10 -0500107static struct eth_uclass_priv *eth_get_uclass_priv(void)
108{
109 struct uclass *uc;
110
111 uclass_get(UCLASS_ETH, &uc);
112 assert(uc);
113 return uc->priv;
114}
115
116static void eth_set_current_to_next(void)
117{
118 struct eth_uclass_priv *uc_priv;
119
120 uc_priv = eth_get_uclass_priv();
121 if (uc_priv->current)
122 uclass_next_device(&uc_priv->current);
123 if (!uc_priv->current)
124 uclass_first_device(UCLASS_ETH, &uc_priv->current);
125}
126
Joe Hershberger60304592015-03-22 17:09:24 -0500127/*
128 * Typically this will simply return the active device.
129 * In the case where the most recent active device was unset, this will attempt
130 * to return the first device. If that device doesn't exist or fails to probe,
131 * this function will return NULL.
132 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500133struct udevice *eth_get_dev(void)
134{
135 struct eth_uclass_priv *uc_priv;
136
137 uc_priv = eth_get_uclass_priv();
138 if (!uc_priv->current)
Joe Hershberger60304592015-03-22 17:09:24 -0500139 eth_errno = uclass_first_device(UCLASS_ETH,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500140 &uc_priv->current);
141 return uc_priv->current;
142}
143
Joe Hershberger60304592015-03-22 17:09:24 -0500144/*
145 * Typically this will just store a device pointer.
146 * In case it was not probed, we will attempt to do so.
147 * dev may be NULL to unset the active device.
148 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500149static void eth_set_dev(struct udevice *dev)
150{
Joe Hershberger60304592015-03-22 17:09:24 -0500151 if (dev && !device_active(dev))
152 eth_errno = device_probe(dev);
Joe Hershberger05c3e682015-03-22 17:09:10 -0500153 eth_get_uclass_priv()->current = dev;
154}
155
Joe Hershbergere58780d2015-03-22 17:09:16 -0500156/*
157 * Find the udevice that either has the name passed in as devname or has an
158 * alias named devname.
159 */
160struct udevice *eth_get_dev_by_name(const char *devname)
161{
162 int seq = -1;
163 char *endp = NULL;
164 const char *startp = NULL;
165 struct udevice *it;
166 struct uclass *uc;
167
168 /* Must be longer than 3 to be an alias */
169 if (strlen(devname) > strlen("eth")) {
170 startp = devname + strlen("eth");
171 seq = simple_strtoul(startp, &endp, 10);
172 }
173
174 uclass_get(UCLASS_ETH, &uc);
175 uclass_foreach_dev(it, uc) {
Joe Hershberger60304592015-03-22 17:09:24 -0500176 /*
177 * We need the seq to be valid, so try to probe it.
178 * If the probe fails, the seq will not match since it will be
179 * -1 instead of what we are looking for.
180 * We don't care about errors from probe here. Either they won't
181 * match an alias or it will match a literal name and we'll pick
182 * up the error when we try to probe again in eth_set_dev().
183 */
Joe Hershbergere58780d2015-03-22 17:09:16 -0500184 device_probe(it);
185 /*
186 * Check for the name or the sequence number to match
187 */
188 if (strcmp(it->name, devname) == 0 ||
189 (endp > startp && it->seq == seq))
190 return it;
191 }
192
193 return NULL;
194}
195
Joe Hershberger05c3e682015-03-22 17:09:10 -0500196unsigned char *eth_get_ethaddr(void)
197{
198 struct eth_pdata *pdata;
199
200 if (eth_get_dev()) {
201 pdata = eth_get_dev()->platdata;
202 return pdata->enetaddr;
203 }
204
205 return NULL;
206}
207
208/* Set active state without calling start on the driver */
209int eth_init_state_only(void)
210{
211 struct udevice *current;
212 struct eth_device_priv *priv;
213
214 current = eth_get_dev();
215 if (!current || !device_active(current))
216 return -EINVAL;
217
218 priv = current->uclass_priv;
219 priv->state = ETH_STATE_ACTIVE;
220
221 return 0;
222}
223
224/* Set passive state without calling stop on the driver */
225void eth_halt_state_only(void)
226{
227 struct udevice *current;
228 struct eth_device_priv *priv;
229
230 current = eth_get_dev();
231 if (!current || !device_active(current))
232 return;
233
234 priv = current->uclass_priv;
235 priv->state = ETH_STATE_PASSIVE;
236}
237
238int eth_get_dev_index(void)
239{
240 if (eth_get_dev())
241 return eth_get_dev()->seq;
242 return -1;
243}
244
245int eth_init(void)
246{
247 struct udevice *current;
248 struct udevice *old_current;
Joe Hershberger60304592015-03-22 17:09:24 -0500249 int ret = -ENODEV;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500250
251 current = eth_get_dev();
252 if (!current) {
253 printf("No ethernet found.\n");
254 return -ENODEV;
255 }
256
257 old_current = current;
258 do {
259 debug("Trying %s\n", current->name);
260
261 if (device_active(current)) {
262 uchar env_enetaddr[6];
263 struct eth_pdata *pdata = current->platdata;
264
265 /* Sync environment with network device */
266 if (eth_getenv_enetaddr_by_index("eth", current->seq,
267 env_enetaddr))
268 memcpy(pdata->enetaddr, env_enetaddr, 6);
269 else
270 memset(pdata->enetaddr, 0, 6);
271
Joe Hershberger60304592015-03-22 17:09:24 -0500272 ret = eth_get_ops(current)->start(current);
273 if (ret >= 0) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500274 struct eth_device_priv *priv =
275 current->uclass_priv;
276
277 priv->state = ETH_STATE_ACTIVE;
278 return 0;
279 }
Joe Hershbergerff819a32015-04-08 01:41:19 -0500280 } else {
Joe Hershberger60304592015-03-22 17:09:24 -0500281 ret = eth_errno;
Joe Hershbergerff819a32015-04-08 01:41:19 -0500282 }
Joe Hershberger60304592015-03-22 17:09:24 -0500283
Joe Hershberger05c3e682015-03-22 17:09:10 -0500284 debug("FAIL\n");
285
Joe Hershberger60304592015-03-22 17:09:24 -0500286 /*
287 * If ethrotate is enabled, this will change "current",
288 * otherwise we will drop out of this while loop immediately
289 */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500290 eth_try_another(0);
Joe Hershberger60304592015-03-22 17:09:24 -0500291 /* This will ensure the new "current" attempted to probe */
Joe Hershberger05c3e682015-03-22 17:09:10 -0500292 current = eth_get_dev();
293 } while (old_current != current);
294
Joe Hershberger60304592015-03-22 17:09:24 -0500295 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500296}
297
298void eth_halt(void)
299{
300 struct udevice *current;
301 struct eth_device_priv *priv;
302
303 current = eth_get_dev();
304 if (!current || !device_active(current))
305 return;
306
307 eth_get_ops(current)->stop(current);
308 priv = current->uclass_priv;
309 priv->state = ETH_STATE_PASSIVE;
310}
311
312int eth_send(void *packet, int length)
313{
314 struct udevice *current;
Joe Hershberger60304592015-03-22 17:09:24 -0500315 int ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500316
317 current = eth_get_dev();
318 if (!current)
319 return -ENODEV;
320
321 if (!device_active(current))
322 return -EINVAL;
323
Joe Hershberger60304592015-03-22 17:09:24 -0500324 ret = eth_get_ops(current)->send(current, packet, length);
325 if (ret < 0) {
326 /* We cannot completely return the error at present */
327 debug("%s: send() returned error %d\n", __func__, ret);
328 }
329 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500330}
331
332int eth_rx(void)
333{
334 struct udevice *current;
Joe Hershberger17591402015-03-22 17:09:12 -0500335 uchar *packet;
336 int ret;
337 int i;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500338
339 current = eth_get_dev();
340 if (!current)
341 return -ENODEV;
342
343 if (!device_active(current))
344 return -EINVAL;
345
Joe Hershberger17591402015-03-22 17:09:12 -0500346 /* Process up to 32 packets at one time */
347 for (i = 0; i < 32; i++) {
348 ret = eth_get_ops(current)->recv(current, &packet);
349 if (ret > 0)
350 net_process_received_packet(packet, ret);
Joe Hershberger63c97292015-04-03 20:09:46 -0500351 if (ret >= 0 && eth_get_ops(current)->free_pkt)
352 eth_get_ops(current)->free_pkt(current, packet, ret);
353 if (ret <= 0)
Joe Hershberger17591402015-03-22 17:09:12 -0500354 break;
355 }
356 if (ret == -EAGAIN)
357 ret = 0;
Joe Hershberger60304592015-03-22 17:09:24 -0500358 if (ret < 0) {
359 /* We cannot completely return the error at present */
360 debug("%s: recv() returned error %d\n", __func__, ret);
361 }
Joe Hershberger17591402015-03-22 17:09:12 -0500362 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500363}
364
365static int eth_write_hwaddr(struct udevice *dev)
366{
367 struct eth_pdata *pdata = dev->platdata;
368 int ret = 0;
369
370 if (!dev || !device_active(dev))
371 return -EINVAL;
372
373 /* seq is valid since the device is active */
374 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500375 if (!is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500376 printf("\nError: %s address %pM illegal value\n",
377 dev->name, pdata->enetaddr);
378 return -EINVAL;
379 }
380
381 ret = eth_get_ops(dev)->write_hwaddr(dev);
382 if (ret)
383 printf("\nWarning: %s failed to set MAC address\n",
384 dev->name);
385 }
386
387 return ret;
388}
389
390int eth_initialize(void)
391{
392 int num_devices = 0;
393 struct udevice *dev;
394
395 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
396 eth_env_init();
397
398 /*
399 * Devices need to write the hwaddr even if not started so that Linux
400 * will have access to the hwaddr that u-boot stored for the device.
401 * This is accomplished by attempting to probe each device and calling
402 * their write_hwaddr() operation.
403 */
404 uclass_first_device(UCLASS_ETH, &dev);
405 if (!dev) {
406 printf("No ethernet found.\n");
407 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
408 } else {
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500409 char *ethprime = getenv("ethprime");
410 struct udevice *prime_dev = NULL;
411
412 if (ethprime)
413 prime_dev = eth_get_dev_by_name(ethprime);
414 if (prime_dev) {
415 eth_set_dev(prime_dev);
416 eth_current_changed();
417 } else {
418 eth_set_dev(NULL);
419 }
420
Joe Hershberger05c3e682015-03-22 17:09:10 -0500421 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
422 do {
423 if (num_devices)
424 printf(", ");
425
426 printf("eth%d: %s", dev->seq, dev->name);
427
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500428 if (ethprime && dev == prime_dev)
429 printf(" [PRIME]");
430
Joe Hershberger05c3e682015-03-22 17:09:10 -0500431 eth_write_hwaddr(dev);
432
433 uclass_next_device(&dev);
434 num_devices++;
435 } while (dev);
436
437 putc('\n');
438 }
439
440 return num_devices;
441}
442
443static int eth_post_bind(struct udevice *dev)
444{
445 if (strchr(dev->name, ' ')) {
446 printf("\nError: eth device name \"%s\" has a space!\n",
447 dev->name);
448 return -EINVAL;
449 }
450
451 return 0;
452}
453
454static int eth_pre_unbind(struct udevice *dev)
455{
456 /* Don't hang onto a pointer that is going away */
457 if (dev == eth_get_uclass_priv()->current)
458 eth_set_dev(NULL);
459
460 return 0;
461}
462
463static int eth_post_probe(struct udevice *dev)
464{
465 struct eth_device_priv *priv = dev->uclass_priv;
466 struct eth_pdata *pdata = dev->platdata;
467 unsigned char env_enetaddr[6];
468
469 priv->state = ETH_STATE_INIT;
470
471 /* Check if the device has a MAC address in ROM */
472 if (eth_get_ops(dev)->read_rom_hwaddr)
473 eth_get_ops(dev)->read_rom_hwaddr(dev);
474
475 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500476 if (!is_zero_ethaddr(env_enetaddr)) {
477 if (!is_zero_ethaddr(pdata->enetaddr) &&
Joe Hershberger05c3e682015-03-22 17:09:10 -0500478 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
479 printf("\nWarning: %s MAC addresses don't match:\n",
480 dev->name);
481 printf("Address in SROM is %pM\n",
482 pdata->enetaddr);
483 printf("Address in environment is %pM\n",
484 env_enetaddr);
485 }
486
487 /* Override the ROM MAC address */
488 memcpy(pdata->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500489 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500490 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
491 printf("\nWarning: %s using MAC address from ROM\n",
492 dev->name);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500493 } else if (is_zero_ethaddr(pdata->enetaddr)) {
Joe Hershberger05c3e682015-03-22 17:09:10 -0500494 printf("\nError: %s address not set.\n",
495 dev->name);
496 return -EINVAL;
497 }
498
499 return 0;
500}
501
502static int eth_pre_remove(struct udevice *dev)
503{
504 eth_get_ops(dev)->stop(dev);
505
506 return 0;
507}
508
509UCLASS_DRIVER(eth) = {
510 .name = "eth",
511 .id = UCLASS_ETH,
512 .post_bind = eth_post_bind,
513 .pre_unbind = eth_pre_unbind,
514 .post_probe = eth_post_probe,
515 .pre_remove = eth_pre_remove,
516 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
517 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershbergere58780d2015-03-22 17:09:16 -0500518 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500519};
520#endif
521
522#ifndef CONFIG_DM_ETH
Ben Warrendd354792008-06-23 22:57:27 -0700523/*
524 * CPU and board-specific Ethernet initializations. Aliased function
525 * signals caller to move on
526 */
527static int __def_eth_init(bd_t *bis)
528{
529 return -1;
530}
Peter Tyserf9a109b2009-04-20 11:08:46 -0500531int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
532int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
Ben Warrendd354792008-06-23 22:57:27 -0700533
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100534#ifdef CONFIG_API
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100535static struct {
536 uchar data[PKTSIZE];
537 int length;
538} eth_rcv_bufs[PKTBUFSRX];
539
Joe Hershberger66c73852012-05-15 08:59:07 +0000540static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100541#endif
542
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000543static struct eth_device *eth_devices;
544struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000545
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500546static void eth_set_current_to_next(void)
547{
548 eth_current = eth_current->next;
549}
550
Joe Hershbergere58780d2015-03-22 17:09:16 -0500551static void eth_set_dev(struct eth_device *dev)
552{
553 eth_current = dev;
554}
555
Ben Warrend7fb9bc2010-07-29 12:56:11 -0700556struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200557{
558 struct eth_device *dev, *target_dev;
559
Helmut Raiger7e7f9032011-08-22 00:17:17 +0000560 BUG_ON(devname == NULL);
561
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200562 if (!eth_devices)
563 return NULL;
564
565 dev = eth_devices;
566 target_dev = NULL;
567 do {
568 if (strcmp(devname, dev->name) == 0) {
569 target_dev = dev;
570 break;
571 }
572 dev = dev->next;
573 } while (dev != eth_devices);
574
575 return target_dev;
576}
577
Andy Fleming9e569862009-02-11 15:07:24 -0600578struct eth_device *eth_get_dev_by_index(int index)
579{
580 struct eth_device *dev, *target_dev;
Andy Fleming9e569862009-02-11 15:07:24 -0600581
582 if (!eth_devices)
583 return NULL;
584
585 dev = eth_devices;
586 target_dev = NULL;
587 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000588 if (dev->index == index) {
Andy Fleming9e569862009-02-11 15:07:24 -0600589 target_dev = dev;
590 break;
591 }
592 dev = dev->next;
Andy Fleming9e569862009-02-11 15:07:24 -0600593 } while (dev != eth_devices);
594
595 return target_dev;
596}
597
Joe Hershberger66c73852012-05-15 08:59:07 +0000598int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000599{
Joe Hershberger66c73852012-05-15 08:59:07 +0000600 if (!eth_current)
Michael Wallefea7dca2011-10-27 11:31:35 +0000601 return -1;
wdenkc6097192002-11-03 00:24:07 +0000602
Michael Wallefea7dca2011-10-27 11:31:35 +0000603 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000604}
605
Simon Glass7616e782011-06-13 16:13:10 -0700606int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
607 int eth_number)
608{
609 unsigned char env_enetaddr[6];
610 int ret = 0;
611
Eric Miao69376642012-01-18 22:56:33 +0000612 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700613
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500614 if (!is_zero_ethaddr(env_enetaddr)) {
615 if (!is_zero_ethaddr(dev->enetaddr) &&
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500616 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass7616e782011-06-13 16:13:10 -0700617 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500618 dev->name);
Simon Glass7616e782011-06-13 16:13:10 -0700619 printf("Address in SROM is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500620 dev->enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700621 printf("Address in environment is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500622 env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700623 }
624
625 memcpy(dev->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500626 } else if (is_valid_ethaddr(dev->enetaddr)) {
Rob Herringc88ef3c2012-04-14 18:06:49 +0000627 eth_setenv_enetaddr_by_index(base_name, eth_number,
628 dev->enetaddr);
629 printf("\nWarning: %s using MAC address from net device\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500630 dev->name);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500631 } else if (is_zero_ethaddr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200632 printf("\nError: %s address not set.\n",
633 dev->name);
634 return -EINVAL;
Simon Glass7616e782011-06-13 16:13:10 -0700635 }
636
Pavel Machek75d9a452014-07-13 10:27:02 +0200637 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500638 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200639 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500640 dev->name, dev->enetaddr);
Pavel Machek75d9a452014-07-13 10:27:02 +0200641 return -EINVAL;
642 }
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000643
Simon Glass7616e782011-06-13 16:13:10 -0700644 ret = dev->write_hwaddr(dev);
Pavel Machek75d9a452014-07-13 10:27:02 +0200645 if (ret)
Joe Hershbergerff819a32015-04-08 01:41:19 -0500646 printf("\nWarning: %s failed to set MAC address\n",
647 dev->name);
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000648 }
Simon Glass7616e782011-06-13 16:13:10 -0700649
650 return ret;
651}
652
Simon Glass89d48362011-02-16 11:14:33 -0800653int eth_register(struct eth_device *dev)
654{
655 struct eth_device *d;
Joe Hershberger66c73852012-05-15 08:59:07 +0000656 static int index;
Michal Simek58c583b2011-08-29 23:30:13 +0000657
Mike Frysingerf6add132011-11-10 14:11:04 +0000658 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek58c583b2011-08-29 23:30:13 +0000659
Simon Glass89d48362011-02-16 11:14:33 -0800660 if (!eth_devices) {
Joe Hershbergerff819a32015-04-08 01:41:19 -0500661 eth_devices = dev;
662 eth_current = dev;
Simon Glass89d48362011-02-16 11:14:33 -0800663 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000664 } else {
Joe Hershberger66c73852012-05-15 08:59:07 +0000665 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundelaba4b692010-03-31 17:56:08 +0200666 ;
wdenkc6097192002-11-03 00:24:07 +0000667 d->next = dev;
668 }
669
670 dev->state = ETH_STATE_INIT;
671 dev->next = eth_devices;
Michael Wallefea7dca2011-10-27 11:31:35 +0000672 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000673
674 return 0;
675}
676
Vincent Palatine7e982d2012-01-09 08:32:36 +0000677int eth_unregister(struct eth_device *dev)
678{
679 struct eth_device *cur;
680
681 /* No device */
682 if (!eth_devices)
Joe Hershberger05324a42015-03-22 17:09:04 -0500683 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000684
685 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
686 cur = cur->next)
687 ;
688
689 /* Device not found */
690 if (cur->next != dev)
Joe Hershberger05324a42015-03-22 17:09:04 -0500691 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000692
693 cur->next = dev->next;
694
695 if (eth_devices == dev)
696 eth_devices = dev->next == eth_devices ? NULL : dev->next;
697
698 if (eth_current == dev) {
699 eth_current = eth_devices;
700 eth_current_changed();
701 }
702
703 return 0;
704}
705
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500706int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000707{
Michael Wallefea7dca2011-10-27 11:31:35 +0000708 int num_devices = 0;
wdenkc6097192002-11-03 00:24:07 +0000709 eth_devices = NULL;
710 eth_current = NULL;
711
Simon Glass770605e2012-02-13 13:51:18 +0000712 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
Alexey Brodkin27ee59a2014-01-10 19:58:11 +0400713#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100714 miiphy_init();
715#endif
Andy Fleming5f184712011-04-08 02:10:27 -0500716
717#ifdef CONFIG_PHYLIB
718 phy_init();
719#endif
720
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500721 eth_env_init();
Mike Frysingerde301222012-04-04 18:53:41 +0000722
Ben Warren8ad25bf2010-08-31 23:05:04 -0700723 /*
724 * If board-specific initialization exists, call it.
725 * If not, call a CPU-specific one
726 */
727 if (board_eth_init != __def_eth_init) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500728 if (board_eth_init(gd->bd) < 0)
Ben Warren8ad25bf2010-08-31 23:05:04 -0700729 printf("Board Net Initialization Failed\n");
730 } else if (cpu_eth_init != __def_eth_init) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500731 if (cpu_eth_init(gd->bd) < 0)
Ben Warren8ad25bf2010-08-31 23:05:04 -0700732 printf("CPU Net Initialization Failed\n");
Joe Hershbergerff819a32015-04-08 01:41:19 -0500733 } else {
Ben Warren8ad25bf2010-08-31 23:05:04 -0700734 printf("Net Initialization Skipped\n");
Joe Hershbergerff819a32015-04-08 01:41:19 -0500735 }
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100736
wdenkc6097192002-11-03 00:24:07 +0000737 if (!eth_devices) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000738 puts("No ethernet found.\n");
Simon Glass770605e2012-02-13 13:51:18 +0000739 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000740 } else {
741 struct eth_device *dev = eth_devices;
Joe Hershberger66c73852012-05-15 08:59:07 +0000742 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000743
Simon Glass770605e2012-02-13 13:51:18 +0000744 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000745 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000746 if (dev->index)
Joe Hershberger66c73852012-05-15 08:59:07 +0000747 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000748
749 printf("%s", dev->name);
750
Joe Hershberger66c73852012-05-15 08:59:07 +0000751 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000752 eth_current = dev;
Joe Hershberger66c73852012-05-15 08:59:07 +0000753 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000754 }
755
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400756 if (strchr(dev->name, ' '))
Joe Hershberger66c73852012-05-15 08:59:07 +0000757 puts("\nWarning: eth device name has a space!"
758 "\n");
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400759
Pavel Machek75d9a452014-07-13 10:27:02 +0200760 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000761
wdenkc6097192002-11-03 00:24:07 +0000762 dev = dev->next;
Michael Wallefea7dca2011-10-27 11:31:35 +0000763 num_devices++;
Joe Hershberger66c73852012-05-15 08:59:07 +0000764 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000765
Simon Glass89d48362011-02-16 11:14:33 -0800766 eth_current_changed();
Joe Hershberger66c73852012-05-15 08:59:07 +0000767 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000768 }
769
Michael Wallefea7dca2011-10-27 11:31:35 +0000770 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000771}
772
David Updegraff53a5c422007-06-11 10:41:07 -0500773#ifdef CONFIG_MCAST_TFTP
774/* Multicast.
775 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200776 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500777 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500778int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff53a5c422007-06-11 10:41:07 -0500779{
Joe Hershberger66c73852012-05-15 08:59:07 +0000780 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200781 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500782 return -1;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500783 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
784 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
785 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff53a5c422007-06-11 10:41:07 -0500786 mcast_mac[2] = 0x5e;
787 mcast_mac[1] = 0x0;
788 mcast_mac[0] = 0x1;
789 return eth_current->mcast(eth_current, mcast_mac, join);
790}
791
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200792/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
793 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500794 * some other adapter -- hash tables
795 */
796#define CRCPOLY_LE 0xedb88320
Joe Hershberger66c73852012-05-15 08:59:07 +0000797u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff53a5c422007-06-11 10:41:07 -0500798{
799 int i;
800 u32 crc;
801 crc = ~0;
802 while (len--) {
803 crc ^= *p++;
804 for (i = 0; i < 8; i++)
805 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
806 }
807 /* an reverse the bits, cuz of way they arrive -- last-first */
808 crc = (crc >> 16) | (crc << 16);
809 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
810 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
811 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
812 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
813 return crc;
814}
815
816#endif
817
wdenkc6097192002-11-03 00:24:07 +0000818
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500819int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000820{
Mike Frysinger86848a72009-07-15 21:31:28 -0400821 struct eth_device *old_current, *dev;
wdenkc6097192002-11-03 00:24:07 +0000822
Stefan Roese6bc11382008-03-04 17:40:41 +0100823 if (!eth_current) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000824 puts("No ethernet found.\n");
Joe Hershberger05324a42015-03-22 17:09:04 -0500825 return -ENODEV;
Stefan Roese6bc11382008-03-04 17:40:41 +0100826 }
wdenkc6097192002-11-03 00:24:07 +0000827
Mike Frysinger86848a72009-07-15 21:31:28 -0400828 /* Sync environment with network devices */
Mike Frysinger86848a72009-07-15 21:31:28 -0400829 dev = eth_devices;
830 do {
831 uchar env_enetaddr[6];
832
Michael Wallefea7dca2011-10-27 11:31:35 +0000833 if (eth_getenv_enetaddr_by_index("eth", dev->index,
Simon Glass7616e782011-06-13 16:13:10 -0700834 env_enetaddr))
Mike Frysinger86848a72009-07-15 21:31:28 -0400835 memcpy(dev->enetaddr, env_enetaddr, 6);
836
Mike Frysinger86848a72009-07-15 21:31:28 -0400837 dev = dev->next;
838 } while (dev != eth_devices);
839
wdenkc6097192002-11-03 00:24:07 +0000840 old_current = eth_current;
841 do {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400842 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000843
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500844 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000845 eth_current->state = ETH_STATE_ACTIVE;
846
Upakul Barkakaty505be872007-11-29 12:16:13 +0530847 return 0;
wdenkc6097192002-11-03 00:24:07 +0000848 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400849 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000850
851 eth_try_another(0);
852 } while (old_current != eth_current);
853
Joe Hershberger05324a42015-03-22 17:09:04 -0500854 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000855}
856
857void eth_halt(void)
858{
859 if (!eth_current)
860 return;
861
862 eth_current->halt(eth_current);
863
864 eth_current->state = ETH_STATE_PASSIVE;
865}
866
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000867int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000868{
869 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500870 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000871
872 return eth_current->send(eth_current, packet, length);
873}
874
875int eth_rx(void)
876{
877 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500878 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000879
880 return eth_current->recv(eth_current);
881}
Joe Hershberger05c3e682015-03-22 17:09:10 -0500882#endif /* ifndef CONFIG_DM_ETH */
wdenkc6097192002-11-03 00:24:07 +0000883
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100884#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000885static void eth_save_packet(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100886{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000887 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100888 int i;
889
890 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
891 return;
892
893 if (PKTSIZE < length)
894 return;
895
896 for (i = 0; i < length; i++)
897 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
898
899 eth_rcv_bufs[eth_rcv_last].length = length;
900 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
901}
902
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000903int eth_receive(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100904{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000905 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100906 void *pp = push_packet;
907 int i;
908
909 if (eth_rcv_current == eth_rcv_last) {
910 push_packet = eth_save_packet;
911 eth_rx();
912 push_packet = pp;
913
914 if (eth_rcv_current == eth_rcv_last)
915 return -1;
916 }
917
Michael Walle46c07bc2012-06-22 11:24:28 +0000918 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100919
920 for (i = 0; i < length; i++)
921 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
922
923 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
924 return length;
925}
926#endif /* CONFIG_API */
927
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500928static void eth_current_changed(void)
929{
930 char *act = getenv("ethact");
931 /* update current ethernet name */
932 if (eth_get_dev()) {
933 if (act == NULL || strcmp(act, eth_get_name()) != 0)
934 setenv("ethact", eth_get_name());
935 }
936 /*
937 * remove the variable completely if there is no active
938 * interface
939 */
940 else if (act != NULL)
941 setenv("ethact", NULL);
942}
943
wdenkc6097192002-11-03 00:24:07 +0000944void eth_try_another(int first_restart)
945{
Joe Hershberger05c3e682015-03-22 17:09:10 -0500946 static void *first_failed;
Remy Bohmerc650e1b2011-02-19 20:15:14 +0100947 char *ethrotate;
Matthias Fuchse1692572008-01-17 07:45:05 +0100948
949 /*
950 * Do not rotate between network interfaces when
951 * 'ethrotate' variable is set to 'no'.
952 */
Joe Hershberger66c73852012-05-15 08:59:07 +0000953 ethrotate = getenv("ethrotate");
954 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchse1692572008-01-17 07:45:05 +0100955 return;
wdenkc6097192002-11-03 00:24:07 +0000956
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500957 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +0000958 return;
959
Joe Hershberger66c73852012-05-15 08:59:07 +0000960 if (first_restart)
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500961 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +0000962
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500963 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +0000964
Simon Glass89d48362011-02-16 11:14:33 -0800965 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +0000966
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500967 if (first_failed == eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +0000968 NetRestartWrap = 1;
wdenkc6097192002-11-03 00:24:07 +0000969}
970
wdenka3d991b2004-04-15 21:48:45 +0000971void eth_set_current(void)
972{
Joe Hershberger66c73852012-05-15 08:59:07 +0000973 static char *act;
974 static int env_changed_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100975 int env_id;
wdenka3d991b2004-04-15 21:48:45 +0000976
Heiko Schocher2f70c492009-02-10 09:38:52 +0100977 env_id = get_env_id();
978 if ((act == NULL) || (env_changed_id != env_id)) {
979 act = getenv("ethact");
980 env_changed_id = env_id;
981 }
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500982
983 if (act == NULL) {
984 char *ethprime = getenv("ethprime");
985 void *dev = NULL;
986
987 if (ethprime)
988 dev = eth_get_dev_by_name(ethprime);
989 if (dev)
990 eth_set_dev(dev);
991 else
992 eth_set_dev(NULL);
993 } else {
Joe Hershbergere58780d2015-03-22 17:09:16 -0500994 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500995 }
wdenka3d991b2004-04-15 21:48:45 +0000996
Simon Glass89d48362011-02-16 11:14:33 -0800997 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +0000998}
wdenka3d991b2004-04-15 21:48:45 +0000999
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001000const char *eth_get_name(void)
wdenk5bb226e2003-11-17 21:14:37 +00001001{
Joe Hershberger84eb1fb2015-03-22 17:09:03 -05001002 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenk5bb226e2003-11-17 21:14:37 +00001003}