blob: b6c2af3de6a9deacdf9b29b8432dcc9de95590e5 [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
101static struct eth_uclass_priv *eth_get_uclass_priv(void)
102{
103 struct uclass *uc;
104
105 uclass_get(UCLASS_ETH, &uc);
106 assert(uc);
107 return uc->priv;
108}
109
110static void eth_set_current_to_next(void)
111{
112 struct eth_uclass_priv *uc_priv;
113
114 uc_priv = eth_get_uclass_priv();
115 if (uc_priv->current)
116 uclass_next_device(&uc_priv->current);
117 if (!uc_priv->current)
118 uclass_first_device(UCLASS_ETH, &uc_priv->current);
119}
120
121struct udevice *eth_get_dev(void)
122{
123 struct eth_uclass_priv *uc_priv;
124
125 uc_priv = eth_get_uclass_priv();
126 if (!uc_priv->current)
127 uclass_first_device(UCLASS_ETH,
128 &uc_priv->current);
129 return uc_priv->current;
130}
131
132static void eth_set_dev(struct udevice *dev)
133{
134 device_probe(dev);
135 eth_get_uclass_priv()->current = dev;
136}
137
Joe Hershbergere58780d2015-03-22 17:09:16 -0500138/*
139 * Find the udevice that either has the name passed in as devname or has an
140 * alias named devname.
141 */
142struct udevice *eth_get_dev_by_name(const char *devname)
143{
144 int seq = -1;
145 char *endp = NULL;
146 const char *startp = NULL;
147 struct udevice *it;
148 struct uclass *uc;
149
150 /* Must be longer than 3 to be an alias */
151 if (strlen(devname) > strlen("eth")) {
152 startp = devname + strlen("eth");
153 seq = simple_strtoul(startp, &endp, 10);
154 }
155
156 uclass_get(UCLASS_ETH, &uc);
157 uclass_foreach_dev(it, uc) {
158 /* We need the seq to be valid, so make sure it's probed */
159 device_probe(it);
160 /*
161 * Check for the name or the sequence number to match
162 */
163 if (strcmp(it->name, devname) == 0 ||
164 (endp > startp && it->seq == seq))
165 return it;
166 }
167
168 return NULL;
169}
170
Joe Hershberger05c3e682015-03-22 17:09:10 -0500171unsigned char *eth_get_ethaddr(void)
172{
173 struct eth_pdata *pdata;
174
175 if (eth_get_dev()) {
176 pdata = eth_get_dev()->platdata;
177 return pdata->enetaddr;
178 }
179
180 return NULL;
181}
182
183/* Set active state without calling start on the driver */
184int eth_init_state_only(void)
185{
186 struct udevice *current;
187 struct eth_device_priv *priv;
188
189 current = eth_get_dev();
190 if (!current || !device_active(current))
191 return -EINVAL;
192
193 priv = current->uclass_priv;
194 priv->state = ETH_STATE_ACTIVE;
195
196 return 0;
197}
198
199/* Set passive state without calling stop on the driver */
200void eth_halt_state_only(void)
201{
202 struct udevice *current;
203 struct eth_device_priv *priv;
204
205 current = eth_get_dev();
206 if (!current || !device_active(current))
207 return;
208
209 priv = current->uclass_priv;
210 priv->state = ETH_STATE_PASSIVE;
211}
212
213int eth_get_dev_index(void)
214{
215 if (eth_get_dev())
216 return eth_get_dev()->seq;
217 return -1;
218}
219
220int eth_init(void)
221{
222 struct udevice *current;
223 struct udevice *old_current;
224
225 current = eth_get_dev();
226 if (!current) {
227 printf("No ethernet found.\n");
228 return -ENODEV;
229 }
230
231 old_current = current;
232 do {
233 debug("Trying %s\n", current->name);
234
235 if (device_active(current)) {
236 uchar env_enetaddr[6];
237 struct eth_pdata *pdata = current->platdata;
238
239 /* Sync environment with network device */
240 if (eth_getenv_enetaddr_by_index("eth", current->seq,
241 env_enetaddr))
242 memcpy(pdata->enetaddr, env_enetaddr, 6);
243 else
244 memset(pdata->enetaddr, 0, 6);
245
246 if (eth_get_ops(current)->start(current) >= 0) {
247 struct eth_device_priv *priv =
248 current->uclass_priv;
249
250 priv->state = ETH_STATE_ACTIVE;
251 return 0;
252 }
253 }
254 debug("FAIL\n");
255
256 /* This will ensure the new "current" attempted to probe */
257 eth_try_another(0);
258 current = eth_get_dev();
259 } while (old_current != current);
260
261 return -ENODEV;
262}
263
264void eth_halt(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 eth_get_ops(current)->stop(current);
274 priv = current->uclass_priv;
275 priv->state = ETH_STATE_PASSIVE;
276}
277
278int eth_send(void *packet, int length)
279{
280 struct udevice *current;
281
282 current = eth_get_dev();
283 if (!current)
284 return -ENODEV;
285
286 if (!device_active(current))
287 return -EINVAL;
288
289 return eth_get_ops(current)->send(current, packet, length);
290}
291
292int eth_rx(void)
293{
294 struct udevice *current;
Joe Hershberger17591402015-03-22 17:09:12 -0500295 uchar *packet;
296 int ret;
297 int i;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500298
299 current = eth_get_dev();
300 if (!current)
301 return -ENODEV;
302
303 if (!device_active(current))
304 return -EINVAL;
305
Joe Hershberger17591402015-03-22 17:09:12 -0500306 /* Process up to 32 packets at one time */
307 for (i = 0; i < 32; i++) {
308 ret = eth_get_ops(current)->recv(current, &packet);
309 if (ret > 0)
310 net_process_received_packet(packet, ret);
311 else
312 break;
313 }
314 if (ret == -EAGAIN)
315 ret = 0;
316 return ret;
Joe Hershberger05c3e682015-03-22 17:09:10 -0500317}
318
319static int eth_write_hwaddr(struct udevice *dev)
320{
321 struct eth_pdata *pdata = dev->platdata;
322 int ret = 0;
323
324 if (!dev || !device_active(dev))
325 return -EINVAL;
326
327 /* seq is valid since the device is active */
328 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
329 if (!is_valid_ether_addr(pdata->enetaddr)) {
330 printf("\nError: %s address %pM illegal value\n",
331 dev->name, pdata->enetaddr);
332 return -EINVAL;
333 }
334
335 ret = eth_get_ops(dev)->write_hwaddr(dev);
336 if (ret)
337 printf("\nWarning: %s failed to set MAC address\n",
338 dev->name);
339 }
340
341 return ret;
342}
343
344int eth_initialize(void)
345{
346 int num_devices = 0;
347 struct udevice *dev;
348
349 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
350 eth_env_init();
351
352 /*
353 * Devices need to write the hwaddr even if not started so that Linux
354 * will have access to the hwaddr that u-boot stored for the device.
355 * This is accomplished by attempting to probe each device and calling
356 * their write_hwaddr() operation.
357 */
358 uclass_first_device(UCLASS_ETH, &dev);
359 if (!dev) {
360 printf("No ethernet found.\n");
361 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
362 } else {
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500363 char *ethprime = getenv("ethprime");
364 struct udevice *prime_dev = NULL;
365
366 if (ethprime)
367 prime_dev = eth_get_dev_by_name(ethprime);
368 if (prime_dev) {
369 eth_set_dev(prime_dev);
370 eth_current_changed();
371 } else {
372 eth_set_dev(NULL);
373 }
374
Joe Hershberger05c3e682015-03-22 17:09:10 -0500375 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
376 do {
377 if (num_devices)
378 printf(", ");
379
380 printf("eth%d: %s", dev->seq, dev->name);
381
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500382 if (ethprime && dev == prime_dev)
383 printf(" [PRIME]");
384
Joe Hershberger05c3e682015-03-22 17:09:10 -0500385 eth_write_hwaddr(dev);
386
387 uclass_next_device(&dev);
388 num_devices++;
389 } while (dev);
390
391 putc('\n');
392 }
393
394 return num_devices;
395}
396
397static int eth_post_bind(struct udevice *dev)
398{
399 if (strchr(dev->name, ' ')) {
400 printf("\nError: eth device name \"%s\" has a space!\n",
401 dev->name);
402 return -EINVAL;
403 }
404
405 return 0;
406}
407
408static int eth_pre_unbind(struct udevice *dev)
409{
410 /* Don't hang onto a pointer that is going away */
411 if (dev == eth_get_uclass_priv()->current)
412 eth_set_dev(NULL);
413
414 return 0;
415}
416
417static int eth_post_probe(struct udevice *dev)
418{
419 struct eth_device_priv *priv = dev->uclass_priv;
420 struct eth_pdata *pdata = dev->platdata;
421 unsigned char env_enetaddr[6];
422
423 priv->state = ETH_STATE_INIT;
424
425 /* Check if the device has a MAC address in ROM */
426 if (eth_get_ops(dev)->read_rom_hwaddr)
427 eth_get_ops(dev)->read_rom_hwaddr(dev);
428
429 eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr);
430 if (!is_zero_ether_addr(env_enetaddr)) {
431 if (!is_zero_ether_addr(pdata->enetaddr) &&
432 memcmp(pdata->enetaddr, env_enetaddr, 6)) {
433 printf("\nWarning: %s MAC addresses don't match:\n",
434 dev->name);
435 printf("Address in SROM is %pM\n",
436 pdata->enetaddr);
437 printf("Address in environment is %pM\n",
438 env_enetaddr);
439 }
440
441 /* Override the ROM MAC address */
442 memcpy(pdata->enetaddr, env_enetaddr, 6);
443 } else if (is_valid_ether_addr(pdata->enetaddr)) {
444 eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
445 printf("\nWarning: %s using MAC address from ROM\n",
446 dev->name);
447 } else if (is_zero_ether_addr(pdata->enetaddr)) {
448 printf("\nError: %s address not set.\n",
449 dev->name);
450 return -EINVAL;
451 }
452
453 return 0;
454}
455
456static int eth_pre_remove(struct udevice *dev)
457{
458 eth_get_ops(dev)->stop(dev);
459
460 return 0;
461}
462
463UCLASS_DRIVER(eth) = {
464 .name = "eth",
465 .id = UCLASS_ETH,
466 .post_bind = eth_post_bind,
467 .pre_unbind = eth_pre_unbind,
468 .post_probe = eth_post_probe,
469 .pre_remove = eth_pre_remove,
470 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
471 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
Joe Hershbergere58780d2015-03-22 17:09:16 -0500472 .flags = DM_UC_FLAG_SEQ_ALIAS,
Joe Hershberger05c3e682015-03-22 17:09:10 -0500473};
474#endif
475
476#ifndef CONFIG_DM_ETH
Ben Warrendd354792008-06-23 22:57:27 -0700477/*
478 * CPU and board-specific Ethernet initializations. Aliased function
479 * signals caller to move on
480 */
481static int __def_eth_init(bd_t *bis)
482{
483 return -1;
484}
Peter Tyserf9a109b2009-04-20 11:08:46 -0500485int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
486int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
Ben Warrendd354792008-06-23 22:57:27 -0700487
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100488#ifdef CONFIG_API
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100489static struct {
490 uchar data[PKTSIZE];
491 int length;
492} eth_rcv_bufs[PKTBUFSRX];
493
Joe Hershberger66c73852012-05-15 08:59:07 +0000494static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100495#endif
496
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000497static struct eth_device *eth_devices;
498struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +0000499
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500500static void eth_set_current_to_next(void)
501{
502 eth_current = eth_current->next;
503}
504
Joe Hershbergere58780d2015-03-22 17:09:16 -0500505static void eth_set_dev(struct eth_device *dev)
506{
507 eth_current = dev;
508}
509
Ben Warrend7fb9bc2010-07-29 12:56:11 -0700510struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200511{
512 struct eth_device *dev, *target_dev;
513
Helmut Raiger7e7f9032011-08-22 00:17:17 +0000514 BUG_ON(devname == NULL);
515
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200516 if (!eth_devices)
517 return NULL;
518
519 dev = eth_devices;
520 target_dev = NULL;
521 do {
522 if (strcmp(devname, dev->name) == 0) {
523 target_dev = dev;
524 break;
525 }
526 dev = dev->next;
527 } while (dev != eth_devices);
528
529 return target_dev;
530}
531
Andy Fleming9e569862009-02-11 15:07:24 -0600532struct eth_device *eth_get_dev_by_index(int index)
533{
534 struct eth_device *dev, *target_dev;
Andy Fleming9e569862009-02-11 15:07:24 -0600535
536 if (!eth_devices)
537 return NULL;
538
539 dev = eth_devices;
540 target_dev = NULL;
541 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000542 if (dev->index == index) {
Andy Fleming9e569862009-02-11 15:07:24 -0600543 target_dev = dev;
544 break;
545 }
546 dev = dev->next;
Andy Fleming9e569862009-02-11 15:07:24 -0600547 } while (dev != eth_devices);
548
549 return target_dev;
550}
551
Joe Hershberger66c73852012-05-15 08:59:07 +0000552int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +0000553{
Joe Hershberger66c73852012-05-15 08:59:07 +0000554 if (!eth_current)
Michael Wallefea7dca2011-10-27 11:31:35 +0000555 return -1;
wdenkc6097192002-11-03 00:24:07 +0000556
Michael Wallefea7dca2011-10-27 11:31:35 +0000557 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000558}
559
Simon Glass7616e782011-06-13 16:13:10 -0700560int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
561 int eth_number)
562{
563 unsigned char env_enetaddr[6];
564 int ret = 0;
565
Eric Miao69376642012-01-18 22:56:33 +0000566 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700567
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500568 if (!is_zero_ether_addr(env_enetaddr)) {
569 if (!is_zero_ether_addr(dev->enetaddr) &&
570 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass7616e782011-06-13 16:13:10 -0700571 printf("\nWarning: %s MAC addresses don't match:\n",
572 dev->name);
573 printf("Address in SROM is %pM\n",
574 dev->enetaddr);
575 printf("Address in environment is %pM\n",
576 env_enetaddr);
577 }
578
579 memcpy(dev->enetaddr, env_enetaddr, 6);
Rob Herringc88ef3c2012-04-14 18:06:49 +0000580 } else if (is_valid_ether_addr(dev->enetaddr)) {
581 eth_setenv_enetaddr_by_index(base_name, eth_number,
582 dev->enetaddr);
583 printf("\nWarning: %s using MAC address from net device\n",
584 dev->name);
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500585 } else if (is_zero_ether_addr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200586 printf("\nError: %s address not set.\n",
587 dev->name);
588 return -EINVAL;
Simon Glass7616e782011-06-13 16:13:10 -0700589 }
590
Pavel Machek75d9a452014-07-13 10:27:02 +0200591 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
592 if (!is_valid_ether_addr(dev->enetaddr)) {
593 printf("\nError: %s address %pM illegal value\n",
594 dev->name, dev->enetaddr);
595 return -EINVAL;
596 }
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000597
Simon Glass7616e782011-06-13 16:13:10 -0700598 ret = dev->write_hwaddr(dev);
Pavel Machek75d9a452014-07-13 10:27:02 +0200599 if (ret)
600 printf("\nWarning: %s failed to set MAC address\n", dev->name);
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000601 }
Simon Glass7616e782011-06-13 16:13:10 -0700602
603 return ret;
604}
605
Simon Glass89d48362011-02-16 11:14:33 -0800606int eth_register(struct eth_device *dev)
607{
608 struct eth_device *d;
Joe Hershberger66c73852012-05-15 08:59:07 +0000609 static int index;
Michal Simek58c583b2011-08-29 23:30:13 +0000610
Mike Frysingerf6add132011-11-10 14:11:04 +0000611 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek58c583b2011-08-29 23:30:13 +0000612
Simon Glass89d48362011-02-16 11:14:33 -0800613 if (!eth_devices) {
614 eth_current = eth_devices = dev;
615 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000616 } else {
Joe Hershberger66c73852012-05-15 08:59:07 +0000617 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundelaba4b692010-03-31 17:56:08 +0200618 ;
wdenkc6097192002-11-03 00:24:07 +0000619 d->next = dev;
620 }
621
622 dev->state = ETH_STATE_INIT;
623 dev->next = eth_devices;
Michael Wallefea7dca2011-10-27 11:31:35 +0000624 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000625
626 return 0;
627}
628
Vincent Palatine7e982d2012-01-09 08:32:36 +0000629int eth_unregister(struct eth_device *dev)
630{
631 struct eth_device *cur;
632
633 /* No device */
634 if (!eth_devices)
Joe Hershberger05324a42015-03-22 17:09:04 -0500635 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000636
637 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
638 cur = cur->next)
639 ;
640
641 /* Device not found */
642 if (cur->next != dev)
Joe Hershberger05324a42015-03-22 17:09:04 -0500643 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000644
645 cur->next = dev->next;
646
647 if (eth_devices == dev)
648 eth_devices = dev->next == eth_devices ? NULL : dev->next;
649
650 if (eth_current == dev) {
651 eth_current = eth_devices;
652 eth_current_changed();
653 }
654
655 return 0;
656}
657
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500658int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000659{
Michael Wallefea7dca2011-10-27 11:31:35 +0000660 int num_devices = 0;
wdenkc6097192002-11-03 00:24:07 +0000661 eth_devices = NULL;
662 eth_current = NULL;
663
Simon Glass770605e2012-02-13 13:51:18 +0000664 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
Alexey Brodkin27ee59a2014-01-10 19:58:11 +0400665#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100666 miiphy_init();
667#endif
Andy Fleming5f184712011-04-08 02:10:27 -0500668
669#ifdef CONFIG_PHYLIB
670 phy_init();
671#endif
672
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500673 eth_env_init();
Mike Frysingerde301222012-04-04 18:53:41 +0000674
Ben Warren8ad25bf2010-08-31 23:05:04 -0700675 /*
676 * If board-specific initialization exists, call it.
677 * If not, call a CPU-specific one
678 */
679 if (board_eth_init != __def_eth_init) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500680 if (board_eth_init(gd->bd) < 0)
Ben Warren8ad25bf2010-08-31 23:05:04 -0700681 printf("Board Net Initialization Failed\n");
682 } else if (cpu_eth_init != __def_eth_init) {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500683 if (cpu_eth_init(gd->bd) < 0)
Ben Warren8ad25bf2010-08-31 23:05:04 -0700684 printf("CPU Net Initialization Failed\n");
685 } else
686 printf("Net Initialization Skipped\n");
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100687
wdenkc6097192002-11-03 00:24:07 +0000688 if (!eth_devices) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000689 puts("No ethernet found.\n");
Simon Glass770605e2012-02-13 13:51:18 +0000690 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000691 } else {
692 struct eth_device *dev = eth_devices;
Joe Hershberger66c73852012-05-15 08:59:07 +0000693 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000694
Simon Glass770605e2012-02-13 13:51:18 +0000695 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000696 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000697 if (dev->index)
Joe Hershberger66c73852012-05-15 08:59:07 +0000698 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000699
700 printf("%s", dev->name);
701
Joe Hershberger66c73852012-05-15 08:59:07 +0000702 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000703 eth_current = dev;
Joe Hershberger66c73852012-05-15 08:59:07 +0000704 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000705 }
706
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400707 if (strchr(dev->name, ' '))
Joe Hershberger66c73852012-05-15 08:59:07 +0000708 puts("\nWarning: eth device name has a space!"
709 "\n");
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400710
Pavel Machek75d9a452014-07-13 10:27:02 +0200711 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000712
wdenkc6097192002-11-03 00:24:07 +0000713 dev = dev->next;
Michael Wallefea7dca2011-10-27 11:31:35 +0000714 num_devices++;
Joe Hershberger66c73852012-05-15 08:59:07 +0000715 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000716
Simon Glass89d48362011-02-16 11:14:33 -0800717 eth_current_changed();
Joe Hershberger66c73852012-05-15 08:59:07 +0000718 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000719 }
720
Michael Wallefea7dca2011-10-27 11:31:35 +0000721 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000722}
723
David Updegraff53a5c422007-06-11 10:41:07 -0500724#ifdef CONFIG_MCAST_TFTP
725/* Multicast.
726 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200727 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500728 */
Joe Hershbergerfce69002015-03-22 17:09:05 -0500729int eth_mcast_join(IPaddr_t mcast_ip, int join)
David Updegraff53a5c422007-06-11 10:41:07 -0500730{
Joe Hershberger66c73852012-05-15 08:59:07 +0000731 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200732 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500733 return -1;
734 mcast_mac[5] = htonl(mcast_ip) & 0xff;
735 mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
736 mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
737 mcast_mac[2] = 0x5e;
738 mcast_mac[1] = 0x0;
739 mcast_mac[0] = 0x1;
740 return eth_current->mcast(eth_current, mcast_mac, join);
741}
742
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200743/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
744 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500745 * some other adapter -- hash tables
746 */
747#define CRCPOLY_LE 0xedb88320
Joe Hershberger66c73852012-05-15 08:59:07 +0000748u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff53a5c422007-06-11 10:41:07 -0500749{
750 int i;
751 u32 crc;
752 crc = ~0;
753 while (len--) {
754 crc ^= *p++;
755 for (i = 0; i < 8; i++)
756 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
757 }
758 /* an reverse the bits, cuz of way they arrive -- last-first */
759 crc = (crc >> 16) | (crc << 16);
760 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
761 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
762 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
763 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
764 return crc;
765}
766
767#endif
768
wdenkc6097192002-11-03 00:24:07 +0000769
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500770int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000771{
Mike Frysinger86848a72009-07-15 21:31:28 -0400772 struct eth_device *old_current, *dev;
wdenkc6097192002-11-03 00:24:07 +0000773
Stefan Roese6bc11382008-03-04 17:40:41 +0100774 if (!eth_current) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000775 puts("No ethernet found.\n");
Joe Hershberger05324a42015-03-22 17:09:04 -0500776 return -ENODEV;
Stefan Roese6bc11382008-03-04 17:40:41 +0100777 }
wdenkc6097192002-11-03 00:24:07 +0000778
Mike Frysinger86848a72009-07-15 21:31:28 -0400779 /* Sync environment with network devices */
Mike Frysinger86848a72009-07-15 21:31:28 -0400780 dev = eth_devices;
781 do {
782 uchar env_enetaddr[6];
783
Michael Wallefea7dca2011-10-27 11:31:35 +0000784 if (eth_getenv_enetaddr_by_index("eth", dev->index,
Simon Glass7616e782011-06-13 16:13:10 -0700785 env_enetaddr))
Mike Frysinger86848a72009-07-15 21:31:28 -0400786 memcpy(dev->enetaddr, env_enetaddr, 6);
787
Mike Frysinger86848a72009-07-15 21:31:28 -0400788 dev = dev->next;
789 } while (dev != eth_devices);
790
wdenkc6097192002-11-03 00:24:07 +0000791 old_current = eth_current;
792 do {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400793 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000794
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500795 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000796 eth_current->state = ETH_STATE_ACTIVE;
797
Upakul Barkakaty505be872007-11-29 12:16:13 +0530798 return 0;
wdenkc6097192002-11-03 00:24:07 +0000799 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400800 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000801
802 eth_try_another(0);
803 } while (old_current != eth_current);
804
Joe Hershberger05324a42015-03-22 17:09:04 -0500805 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000806}
807
808void eth_halt(void)
809{
810 if (!eth_current)
811 return;
812
813 eth_current->halt(eth_current);
814
815 eth_current->state = ETH_STATE_PASSIVE;
816}
817
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000818int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000819{
820 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500821 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000822
823 return eth_current->send(eth_current, packet, length);
824}
825
826int eth_rx(void)
827{
828 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500829 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000830
831 return eth_current->recv(eth_current);
832}
Joe Hershberger05c3e682015-03-22 17:09:10 -0500833#endif /* ifndef CONFIG_DM_ETH */
wdenkc6097192002-11-03 00:24:07 +0000834
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100835#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000836static void eth_save_packet(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100837{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000838 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100839 int i;
840
841 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
842 return;
843
844 if (PKTSIZE < length)
845 return;
846
847 for (i = 0; i < length; i++)
848 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
849
850 eth_rcv_bufs[eth_rcv_last].length = length;
851 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
852}
853
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000854int eth_receive(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100855{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000856 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100857 void *pp = push_packet;
858 int i;
859
860 if (eth_rcv_current == eth_rcv_last) {
861 push_packet = eth_save_packet;
862 eth_rx();
863 push_packet = pp;
864
865 if (eth_rcv_current == eth_rcv_last)
866 return -1;
867 }
868
Michael Walle46c07bc2012-06-22 11:24:28 +0000869 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100870
871 for (i = 0; i < length; i++)
872 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
873
874 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
875 return length;
876}
877#endif /* CONFIG_API */
878
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500879static void eth_current_changed(void)
880{
881 char *act = getenv("ethact");
882 /* update current ethernet name */
883 if (eth_get_dev()) {
884 if (act == NULL || strcmp(act, eth_get_name()) != 0)
885 setenv("ethact", eth_get_name());
886 }
887 /*
888 * remove the variable completely if there is no active
889 * interface
890 */
891 else if (act != NULL)
892 setenv("ethact", NULL);
893}
894
wdenkc6097192002-11-03 00:24:07 +0000895void eth_try_another(int first_restart)
896{
Joe Hershberger05c3e682015-03-22 17:09:10 -0500897 static void *first_failed;
Remy Bohmerc650e1b2011-02-19 20:15:14 +0100898 char *ethrotate;
Matthias Fuchse1692572008-01-17 07:45:05 +0100899
900 /*
901 * Do not rotate between network interfaces when
902 * 'ethrotate' variable is set to 'no'.
903 */
Joe Hershberger66c73852012-05-15 08:59:07 +0000904 ethrotate = getenv("ethrotate");
905 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
Matthias Fuchse1692572008-01-17 07:45:05 +0100906 return;
wdenkc6097192002-11-03 00:24:07 +0000907
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500908 if (!eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +0000909 return;
910
Joe Hershberger66c73852012-05-15 08:59:07 +0000911 if (first_restart)
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500912 first_failed = eth_get_dev();
wdenkc6097192002-11-03 00:24:07 +0000913
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500914 eth_set_current_to_next();
wdenkc6097192002-11-03 00:24:07 +0000915
Simon Glass89d48362011-02-16 11:14:33 -0800916 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +0000917
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500918 if (first_failed == eth_get_dev())
wdenkc6097192002-11-03 00:24:07 +0000919 NetRestartWrap = 1;
wdenkc6097192002-11-03 00:24:07 +0000920}
921
wdenka3d991b2004-04-15 21:48:45 +0000922void eth_set_current(void)
923{
Joe Hershberger66c73852012-05-15 08:59:07 +0000924 static char *act;
925 static int env_changed_id;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100926 int env_id;
wdenka3d991b2004-04-15 21:48:45 +0000927
Heiko Schocher2f70c492009-02-10 09:38:52 +0100928 env_id = get_env_id();
929 if ((act == NULL) || (env_changed_id != env_id)) {
930 act = getenv("ethact");
931 env_changed_id = env_id;
932 }
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500933
934 if (act == NULL) {
935 char *ethprime = getenv("ethprime");
936 void *dev = NULL;
937
938 if (ethprime)
939 dev = eth_get_dev_by_name(ethprime);
940 if (dev)
941 eth_set_dev(dev);
942 else
943 eth_set_dev(NULL);
944 } else {
Joe Hershbergere58780d2015-03-22 17:09:16 -0500945 eth_set_dev(eth_get_dev_by_name(act));
Joe Hershberger6536b9b2015-03-22 17:09:17 -0500946 }
wdenka3d991b2004-04-15 21:48:45 +0000947
Simon Glass89d48362011-02-16 11:14:33 -0800948 eth_current_changed();
wdenka3d991b2004-04-15 21:48:45 +0000949}
wdenka3d991b2004-04-15 21:48:45 +0000950
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500951const char *eth_get_name(void)
wdenk5bb226e2003-11-17 21:14:37 +0000952{
Joe Hershberger84eb1fb2015-03-22 17:09:03 -0500953 return eth_get_dev() ? eth_get_dev()->name : "unknown";
wdenk5bb226e2003-11-17 21:14:37 +0000954}