2 * (C) Copyright 2001-2015
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 * Joe Hershberger, National Instruments
6 * SPDX-License-Identifier: GPL-2.0+
12 #include <environment.h>
16 #include <asm/errno.h>
17 #include <dm/device-internal.h>
18 #include <dm/uclass-internal.h>
20 DECLARE_GLOBAL_DATA_PTR
;
22 void eth_parse_enetaddr(const char *addr
, uchar
*enetaddr
)
27 for (i
= 0; i
< 6; ++i
) {
28 enetaddr
[i
] = addr
? simple_strtoul(addr
, &end
, 16) : 0;
30 addr
= (*end
) ? end
+ 1 : end
;
34 int eth_getenv_enetaddr(const char *name
, uchar
*enetaddr
)
36 eth_parse_enetaddr(getenv(name
), enetaddr
);
37 return is_valid_ethaddr(enetaddr
);
40 int eth_setenv_enetaddr(const char *name
, const uchar
*enetaddr
)
44 sprintf(buf
, "%pM", enetaddr
);
46 return setenv(name
, buf
);
49 int eth_getenv_enetaddr_by_index(const char *base_name
, int index
,
53 sprintf(enetvar
, index
? "%s%daddr" : "%saddr", base_name
, index
);
54 return eth_getenv_enetaddr(enetvar
, enetaddr
);
57 static inline int eth_setenv_enetaddr_by_index(const char *base_name
, int index
,
61 sprintf(enetvar
, index
? "%s%daddr" : "%saddr", base_name
, index
);
62 return eth_setenv_enetaddr(enetvar
, enetaddr
);
65 static int eth_mac_skip(int index
)
70 sprintf(enetvar
, index
? "eth%dmacskip" : "ethmacskip", index
);
71 skip_state
= getenv(enetvar
);
72 return skip_state
!= NULL
;
75 static void eth_current_changed(void);
78 * CPU and board-specific Ethernet initializations. Aliased function
79 * signals caller to move on
81 static int __def_eth_init(bd_t
*bis
)
85 int cpu_eth_init(bd_t
*bis
) __attribute__((weak
, alias("__def_eth_init")));
86 int board_eth_init(bd_t
*bis
) __attribute__((weak
, alias("__def_eth_init")));
88 static void eth_common_init(void)
90 bootstage_mark(BOOTSTAGE_ID_NET_ETH_START
);
91 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
100 * If board-specific initialization exists, call it.
101 * If not, call a CPU-specific one
103 if (board_eth_init
!= __def_eth_init
) {
104 if (board_eth_init(gd
->bd
) < 0)
105 printf("Board Net Initialization Failed\n");
106 } else if (cpu_eth_init
!= __def_eth_init
) {
107 if (cpu_eth_init(gd
->bd
) < 0)
108 printf("CPU Net Initialization Failed\n");
110 #ifndef CONFIG_DM_ETH
111 printf("Net Initialization Skipped\n");
118 * struct eth_device_priv - private structure for each Ethernet device
120 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
122 struct eth_device_priv
{
123 enum eth_state_t state
;
127 * struct eth_uclass_priv - The structure attached to the uclass itself
129 * @current: The Ethernet device that the network functions are using
131 struct eth_uclass_priv
{
132 struct udevice
*current
;
135 /* eth_errno - This stores the most recent failure code from DM functions */
136 static int eth_errno
;
138 static struct eth_uclass_priv
*eth_get_uclass_priv(void)
142 uclass_get(UCLASS_ETH
, &uc
);
147 static void eth_set_current_to_next(void)
149 struct eth_uclass_priv
*uc_priv
;
151 uc_priv
= eth_get_uclass_priv();
152 if (uc_priv
->current
)
153 uclass_next_device(&uc_priv
->current
);
154 if (!uc_priv
->current
)
155 uclass_first_device(UCLASS_ETH
, &uc_priv
->current
);
159 * Typically this will simply return the active device.
160 * In the case where the most recent active device was unset, this will attempt
161 * to return the first device. If that device doesn't exist or fails to probe,
162 * this function will return NULL.
164 struct udevice
*eth_get_dev(void)
166 struct eth_uclass_priv
*uc_priv
;
168 uc_priv
= eth_get_uclass_priv();
169 if (!uc_priv
->current
)
170 eth_errno
= uclass_first_device(UCLASS_ETH
,
172 return uc_priv
->current
;
176 * Typically this will just store a device pointer.
177 * In case it was not probed, we will attempt to do so.
178 * dev may be NULL to unset the active device.
180 static void eth_set_dev(struct udevice
*dev
)
182 if (dev
&& !device_active(dev
))
183 eth_errno
= device_probe(dev
);
184 eth_get_uclass_priv()->current
= dev
;
188 * Find the udevice that either has the name passed in as devname or has an
189 * alias named devname.
191 struct udevice
*eth_get_dev_by_name(const char *devname
)
195 const char *startp
= NULL
;
198 int len
= strlen("eth");
200 /* Must be longer than 3 to be an alias */
201 if (!strncmp(devname
, "eth", len
) && strlen(devname
) > len
) {
202 startp
= devname
+ len
;
203 seq
= simple_strtoul(startp
, &endp
, 10);
206 uclass_get(UCLASS_ETH
, &uc
);
207 uclass_foreach_dev(it
, uc
) {
209 * We need the seq to be valid, so try to probe it.
210 * If the probe fails, the seq will not match since it will be
211 * -1 instead of what we are looking for.
212 * We don't care about errors from probe here. Either they won't
213 * match an alias or it will match a literal name and we'll pick
214 * up the error when we try to probe again in eth_set_dev().
218 * Check for the name or the sequence number to match
220 if (strcmp(it
->name
, devname
) == 0 ||
221 (endp
> startp
&& it
->seq
== seq
))
228 unsigned char *eth_get_ethaddr(void)
230 struct eth_pdata
*pdata
;
233 pdata
= eth_get_dev()->platdata
;
234 return pdata
->enetaddr
;
240 /* Set active state without calling start on the driver */
241 int eth_init_state_only(void)
243 struct udevice
*current
;
244 struct eth_device_priv
*priv
;
246 current
= eth_get_dev();
247 if (!current
|| !device_active(current
))
250 priv
= current
->uclass_priv
;
251 priv
->state
= ETH_STATE_ACTIVE
;
256 /* Set passive state without calling stop on the driver */
257 void eth_halt_state_only(void)
259 struct udevice
*current
;
260 struct eth_device_priv
*priv
;
262 current
= eth_get_dev();
263 if (!current
|| !device_active(current
))
266 priv
= current
->uclass_priv
;
267 priv
->state
= ETH_STATE_PASSIVE
;
270 int eth_get_dev_index(void)
273 return eth_get_dev()->seq
;
277 static int eth_write_hwaddr(struct udevice
*dev
)
279 struct eth_pdata
*pdata
= dev
->platdata
;
282 if (!dev
|| !device_active(dev
))
285 /* seq is valid since the device is active */
286 if (eth_get_ops(dev
)->write_hwaddr
&& !eth_mac_skip(dev
->seq
)) {
287 if (!is_valid_ethaddr(pdata
->enetaddr
)) {
288 printf("\nError: %s address %pM illegal value\n",
289 dev
->name
, pdata
->enetaddr
);
294 * Drivers are allowed to decide not to implement this at
295 * run-time. E.g. Some devices may use it and some may not.
297 ret
= eth_get_ops(dev
)->write_hwaddr(dev
);
301 printf("\nWarning: %s failed to set MAC address\n",
308 static int on_ethaddr(const char *name
, const char *value
, enum env_op op
,
315 /* look for an index after "eth" */
316 index
= simple_strtoul(name
+ 3, NULL
, 10);
318 retval
= uclass_find_device_by_seq(UCLASS_ETH
, index
, false, &dev
);
320 struct eth_pdata
*pdata
= dev
->platdata
;
323 case env_op_overwrite
:
324 eth_parse_enetaddr(value
, pdata
->enetaddr
);
327 memset(pdata
->enetaddr
, 0, 6);
333 U_BOOT_ENV_CALLBACK(ethaddr
, on_ethaddr
);
337 struct udevice
*current
;
338 struct udevice
*old_current
;
341 current
= eth_get_dev();
343 printf("No ethernet found.\n");
347 old_current
= current
;
349 debug("Trying %s\n", current
->name
);
351 if (device_active(current
)) {
352 ret
= eth_get_ops(current
)->start(current
);
354 struct eth_device_priv
*priv
=
355 current
->uclass_priv
;
357 priv
->state
= ETH_STATE_ACTIVE
;
367 * If ethrotate is enabled, this will change "current",
368 * otherwise we will drop out of this while loop immediately
371 /* This will ensure the new "current" attempted to probe */
372 current
= eth_get_dev();
373 } while (old_current
!= current
);
380 struct udevice
*current
;
381 struct eth_device_priv
*priv
;
383 current
= eth_get_dev();
384 if (!current
|| !device_active(current
))
387 eth_get_ops(current
)->stop(current
);
388 priv
= current
->uclass_priv
;
389 priv
->state
= ETH_STATE_PASSIVE
;
392 int eth_is_active(struct udevice
*dev
)
394 struct eth_device_priv
*priv
;
396 if (!dev
|| !device_active(dev
))
399 priv
= dev_get_uclass_priv(dev
);
400 return priv
->state
== ETH_STATE_ACTIVE
;
403 int eth_send(void *packet
, int length
)
405 struct udevice
*current
;
408 current
= eth_get_dev();
412 if (!device_active(current
))
415 ret
= eth_get_ops(current
)->send(current
, packet
, length
);
417 /* We cannot completely return the error at present */
418 debug("%s: send() returned error %d\n", __func__
, ret
);
425 struct udevice
*current
;
431 current
= eth_get_dev();
435 if (!device_active(current
))
438 /* Process up to 32 packets at one time */
439 flags
= ETH_RECV_CHECK_DEVICE
;
440 for (i
= 0; i
< 32; i
++) {
441 ret
= eth_get_ops(current
)->recv(current
, flags
, &packet
);
444 net_process_received_packet(packet
, ret
);
445 if (ret
>= 0 && eth_get_ops(current
)->free_pkt
)
446 eth_get_ops(current
)->free_pkt(current
, packet
, ret
);
453 /* We cannot completely return the error at present */
454 debug("%s: recv() returned error %d\n", __func__
, ret
);
459 int eth_initialize(void)
467 * Devices need to write the hwaddr even if not started so that Linux
468 * will have access to the hwaddr that u-boot stored for the device.
469 * This is accomplished by attempting to probe each device and calling
470 * their write_hwaddr() operation.
472 uclass_first_device(UCLASS_ETH
, &dev
);
474 printf("No ethernet found.\n");
475 bootstage_error(BOOTSTAGE_ID_NET_ETH_START
);
477 char *ethprime
= getenv("ethprime");
478 struct udevice
*prime_dev
= NULL
;
481 prime_dev
= eth_get_dev_by_name(ethprime
);
483 eth_set_dev(prime_dev
);
484 eth_current_changed();
489 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT
);
494 printf("eth%d: %s", dev
->seq
, dev
->name
);
496 if (ethprime
&& dev
== prime_dev
)
499 eth_write_hwaddr(dev
);
501 uclass_next_device(&dev
);
511 static int eth_post_bind(struct udevice
*dev
)
513 if (strchr(dev
->name
, ' ')) {
514 printf("\nError: eth device name \"%s\" has a space!\n",
522 static int eth_pre_unbind(struct udevice
*dev
)
524 /* Don't hang onto a pointer that is going away */
525 if (dev
== eth_get_uclass_priv()->current
)
531 static int eth_post_probe(struct udevice
*dev
)
533 struct eth_device_priv
*priv
= dev
->uclass_priv
;
534 struct eth_pdata
*pdata
= dev
->platdata
;
535 unsigned char env_enetaddr
[6];
537 priv
->state
= ETH_STATE_INIT
;
539 /* Check if the device has a MAC address in ROM */
540 if (eth_get_ops(dev
)->read_rom_hwaddr
)
541 eth_get_ops(dev
)->read_rom_hwaddr(dev
);
543 eth_getenv_enetaddr_by_index("eth", dev
->seq
, env_enetaddr
);
544 if (!is_zero_ethaddr(env_enetaddr
)) {
545 if (!is_zero_ethaddr(pdata
->enetaddr
) &&
546 memcmp(pdata
->enetaddr
, env_enetaddr
, 6)) {
547 printf("\nWarning: %s MAC addresses don't match:\n",
549 printf("Address in SROM is %pM\n",
551 printf("Address in environment is %pM\n",
555 /* Override the ROM MAC address */
556 memcpy(pdata
->enetaddr
, env_enetaddr
, 6);
557 } else if (is_valid_ethaddr(pdata
->enetaddr
)) {
558 eth_setenv_enetaddr_by_index("eth", dev
->seq
, pdata
->enetaddr
);
559 printf("\nWarning: %s using MAC address from ROM\n",
561 } else if (is_zero_ethaddr(pdata
->enetaddr
)) {
562 #ifdef CONFIG_NET_RANDOM_ETHADDR
563 net_random_ethaddr(pdata
->enetaddr
);
564 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
565 dev
->name
, dev
->seq
, pdata
->enetaddr
);
567 printf("\nError: %s address not set.\n",
576 static int eth_pre_remove(struct udevice
*dev
)
578 struct eth_pdata
*pdata
= dev
->platdata
;
580 eth_get_ops(dev
)->stop(dev
);
582 /* clear the MAC address */
583 memset(pdata
->enetaddr
, 0, 6);
588 UCLASS_DRIVER(eth
) = {
591 .post_bind
= eth_post_bind
,
592 .pre_unbind
= eth_pre_unbind
,
593 .post_probe
= eth_post_probe
,
594 .pre_remove
= eth_pre_remove
,
595 .priv_auto_alloc_size
= sizeof(struct eth_uclass_priv
),
596 .per_device_auto_alloc_size
= sizeof(struct eth_device_priv
),
597 .flags
= DM_UC_FLAG_SEQ_ALIAS
,
599 #endif /* #ifdef CONFIG_DM_ETH */
601 #ifndef CONFIG_DM_ETH
607 } eth_rcv_bufs
[PKTBUFSRX
];
609 static unsigned int eth_rcv_current
, eth_rcv_last
;
612 static struct eth_device
*eth_devices
;
613 struct eth_device
*eth_current
;
615 static void eth_set_current_to_next(void)
617 eth_current
= eth_current
->next
;
620 static void eth_set_dev(struct eth_device
*dev
)
625 struct eth_device
*eth_get_dev_by_name(const char *devname
)
627 struct eth_device
*dev
, *target_dev
;
629 BUG_ON(devname
== NULL
);
637 if (strcmp(devname
, dev
->name
) == 0) {
642 } while (dev
!= eth_devices
);
647 struct eth_device
*eth_get_dev_by_index(int index
)
649 struct eth_device
*dev
, *target_dev
;
657 if (dev
->index
== index
) {
662 } while (dev
!= eth_devices
);
667 int eth_get_dev_index(void)
672 return eth_current
->index
;
675 static int on_ethaddr(const char *name
, const char *value
, enum env_op op
,
679 struct eth_device
*dev
;
684 /* look for an index after "eth" */
685 index
= simple_strtoul(name
+ 3, NULL
, 10);
689 if (dev
->index
== index
) {
692 case env_op_overwrite
:
693 eth_parse_enetaddr(value
, dev
->enetaddr
);
696 memset(dev
->enetaddr
, 0, 6);
700 } while (dev
!= eth_devices
);
704 U_BOOT_ENV_CALLBACK(ethaddr
, on_ethaddr
);
706 int eth_write_hwaddr(struct eth_device
*dev
, const char *base_name
,
709 unsigned char env_enetaddr
[6];
712 eth_getenv_enetaddr_by_index(base_name
, eth_number
, env_enetaddr
);
714 if (!is_zero_ethaddr(env_enetaddr
)) {
715 if (!is_zero_ethaddr(dev
->enetaddr
) &&
716 memcmp(dev
->enetaddr
, env_enetaddr
, 6)) {
717 printf("\nWarning: %s MAC addresses don't match:\n",
719 printf("Address in SROM is %pM\n",
721 printf("Address in environment is %pM\n",
725 memcpy(dev
->enetaddr
, env_enetaddr
, 6);
726 } else if (is_valid_ethaddr(dev
->enetaddr
)) {
727 eth_setenv_enetaddr_by_index(base_name
, eth_number
,
729 printf("\nWarning: %s using MAC address from net device\n",
731 } else if (is_zero_ethaddr(dev
->enetaddr
)) {
732 #ifdef CONFIG_NET_RANDOM_ETHADDR
733 net_random_ethaddr(dev
->enetaddr
);
734 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
735 dev
->name
, eth_number
, dev
->enetaddr
);
737 printf("\nError: %s address not set.\n",
743 if (dev
->write_hwaddr
&& !eth_mac_skip(eth_number
)) {
744 if (!is_valid_ethaddr(dev
->enetaddr
)) {
745 printf("\nError: %s address %pM illegal value\n",
746 dev
->name
, dev
->enetaddr
);
750 ret
= dev
->write_hwaddr(dev
);
752 printf("\nWarning: %s failed to set MAC address\n",
759 int eth_register(struct eth_device
*dev
)
761 struct eth_device
*d
;
764 assert(strlen(dev
->name
) < sizeof(dev
->name
));
769 eth_current_changed();
771 for (d
= eth_devices
; d
->next
!= eth_devices
; d
= d
->next
)
776 dev
->state
= ETH_STATE_INIT
;
777 dev
->next
= eth_devices
;
778 dev
->index
= index
++;
783 int eth_unregister(struct eth_device
*dev
)
785 struct eth_device
*cur
;
791 for (cur
= eth_devices
; cur
->next
!= eth_devices
&& cur
->next
!= dev
;
795 /* Device not found */
796 if (cur
->next
!= dev
)
799 cur
->next
= dev
->next
;
801 if (eth_devices
== dev
)
802 eth_devices
= dev
->next
== eth_devices
? NULL
: dev
->next
;
804 if (eth_current
== dev
) {
805 eth_current
= eth_devices
;
806 eth_current_changed();
812 int eth_initialize(void)
821 puts("No ethernet found.\n");
822 bootstage_error(BOOTSTAGE_ID_NET_ETH_START
);
824 struct eth_device
*dev
= eth_devices
;
825 char *ethprime
= getenv("ethprime");
827 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT
);
832 printf("%s", dev
->name
);
834 if (ethprime
&& strcmp(dev
->name
, ethprime
) == 0) {
839 if (strchr(dev
->name
, ' '))
840 puts("\nWarning: eth device name has a space!"
843 eth_write_hwaddr(dev
, "eth", dev
->index
);
847 } while (dev
!= eth_devices
);
849 eth_current_changed();
856 #ifdef CONFIG_MCAST_TFTP
858 * mcast_addr: multicast ipaddr from which multicast Mac is made
859 * join: 1=join, 0=leave.
861 int eth_mcast_join(struct in_addr mcast_ip
, int join
)
864 if (!eth_current
|| !eth_current
->mcast
)
866 mcast_mac
[5] = htonl(mcast_ip
.s_addr
) & 0xff;
867 mcast_mac
[4] = (htonl(mcast_ip
.s_addr
)>>8) & 0xff;
868 mcast_mac
[3] = (htonl(mcast_ip
.s_addr
)>>16) & 0x7f;
872 return eth_current
->mcast(eth_current
, mcast_mac
, join
);
875 /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
876 * and this is the ethernet-crc method needed for TSEC -- and perhaps
877 * some other adapter -- hash tables
879 #define CRCPOLY_LE 0xedb88320
880 u32
ether_crc(size_t len
, unsigned char const *p
)
887 for (i
= 0; i
< 8; i
++)
888 crc
= (crc
>> 1) ^ ((crc
& 1) ? CRCPOLY_LE
: 0);
890 /* an reverse the bits, cuz of way they arrive -- last-first */
891 crc
= (crc
>> 16) | (crc
<< 16);
892 crc
= (crc
>> 8 & 0x00ff00ff) | (crc
<< 8 & 0xff00ff00);
893 crc
= (crc
>> 4 & 0x0f0f0f0f) | (crc
<< 4 & 0xf0f0f0f0);
894 crc
= (crc
>> 2 & 0x33333333) | (crc
<< 2 & 0xcccccccc);
895 crc
= (crc
>> 1 & 0x55555555) | (crc
<< 1 & 0xaaaaaaaa);
904 struct eth_device
*old_current
;
907 puts("No ethernet found.\n");
911 old_current
= eth_current
;
913 debug("Trying %s\n", eth_current
->name
);
915 if (eth_current
->init(eth_current
, gd
->bd
) >= 0) {
916 eth_current
->state
= ETH_STATE_ACTIVE
;
923 } while (old_current
!= eth_current
);
933 eth_current
->halt(eth_current
);
935 eth_current
->state
= ETH_STATE_PASSIVE
;
938 int eth_is_active(struct eth_device
*dev
)
940 return dev
&& dev
->state
== ETH_STATE_ACTIVE
;
943 int eth_send(void *packet
, int length
)
948 return eth_current
->send(eth_current
, packet
, length
);
956 return eth_current
->recv(eth_current
);
958 #endif /* ifndef CONFIG_DM_ETH */
961 static void eth_save_packet(void *packet
, int length
)
966 if ((eth_rcv_last
+1) % PKTBUFSRX
== eth_rcv_current
)
969 if (PKTSIZE
< length
)
972 for (i
= 0; i
< length
; i
++)
973 eth_rcv_bufs
[eth_rcv_last
].data
[i
] = p
[i
];
975 eth_rcv_bufs
[eth_rcv_last
].length
= length
;
976 eth_rcv_last
= (eth_rcv_last
+ 1) % PKTBUFSRX
;
979 int eth_receive(void *packet
, int length
)
982 void *pp
= push_packet
;
985 if (eth_rcv_current
== eth_rcv_last
) {
986 push_packet
= eth_save_packet
;
990 if (eth_rcv_current
== eth_rcv_last
)
994 length
= min(eth_rcv_bufs
[eth_rcv_current
].length
, length
);
996 for (i
= 0; i
< length
; i
++)
997 p
[i
] = eth_rcv_bufs
[eth_rcv_current
].data
[i
];
999 eth_rcv_current
= (eth_rcv_current
+ 1) % PKTBUFSRX
;
1002 #endif /* CONFIG_API */
1004 static void eth_current_changed(void)
1006 char *act
= getenv("ethact");
1007 /* update current ethernet name */
1008 if (eth_get_dev()) {
1009 if (act
== NULL
|| strcmp(act
, eth_get_name()) != 0)
1010 setenv("ethact", eth_get_name());
1013 * remove the variable completely if there is no active
1016 else if (act
!= NULL
)
1017 setenv("ethact", NULL
);
1020 void eth_try_another(int first_restart
)
1022 static void *first_failed
;
1026 * Do not rotate between network interfaces when
1027 * 'ethrotate' variable is set to 'no'.
1029 ethrotate
= getenv("ethrotate");
1030 if ((ethrotate
!= NULL
) && (strcmp(ethrotate
, "no") == 0))
1037 first_failed
= eth_get_dev();
1039 eth_set_current_to_next();
1041 eth_current_changed();
1043 if (first_failed
== eth_get_dev())
1044 net_restart_wrap
= 1;
1047 void eth_set_current(void)
1050 static int env_changed_id
;
1053 env_id
= get_env_id();
1054 if ((act
== NULL
) || (env_changed_id
!= env_id
)) {
1055 act
= getenv("ethact");
1056 env_changed_id
= env_id
;
1060 char *ethprime
= getenv("ethprime");
1064 dev
= eth_get_dev_by_name(ethprime
);
1070 eth_set_dev(eth_get_dev_by_name(act
));
1073 eth_current_changed();
1076 const char *eth_get_name(void)
1078 return eth_get_dev() ? eth_get_dev()->name
: "unknown";