blob: d6d7ceeafdd73b02ca1133f4742fe592304d7125 [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 Hershberger6e0d26c2015-05-20 14:27:26 -050011#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000012#include <net.h>
Andy Fleming5f184712011-04-08 02:10:27 -050013#include <phy.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090014#include <linux/errno.h>
Simon Glass818f91e2016-01-17 14:51:57 -070015#include "eth_internal.h"
wdenkc6097192002-11-03 00:24:07 +000016
Joe Hershbergerd2eaec62015-03-22 17:09:06 -050017DECLARE_GLOBAL_DATA_PTR;
18
Simon Glass3bc42702015-04-05 16:07:37 -060019/*
20 * CPU and board-specific Ethernet initializations. Aliased function
21 * signals caller to move on
22 */
23static int __def_eth_init(bd_t *bis)
24{
25 return -1;
26}
27int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
28int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
29
Rafal Jaworowskif85b6072007-12-27 18:19:02 +010030#ifdef CONFIG_API
Rafal Jaworowskif85b6072007-12-27 18:19:02 +010031static struct {
32 uchar data[PKTSIZE];
33 int length;
34} eth_rcv_bufs[PKTBUFSRX];
35
Joe Hershberger66c73852012-05-15 08:59:07 +000036static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +010037#endif
38
Joe Hershbergerf8be7d62012-08-03 10:59:08 +000039static struct eth_device *eth_devices;
40struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +000041
Simon Glass8607a6b2016-01-17 14:51:59 -070042void eth_set_current_to_next(void)
Joe Hershberger84eb1fb2015-03-22 17:09:03 -050043{
44 eth_current = eth_current->next;
45}
46
Simon Glass8607a6b2016-01-17 14:51:59 -070047void eth_set_dev(struct eth_device *dev)
Joe Hershbergere58780d2015-03-22 17:09:16 -050048{
49 eth_current = dev;
50}
51
Ben Warrend7fb9bc2010-07-29 12:56:11 -070052struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowicz63ff0042005-10-28 22:30:33 +020053{
54 struct eth_device *dev, *target_dev;
55
Helmut Raiger7e7f9032011-08-22 00:17:17 +000056 BUG_ON(devname == NULL);
57
Marian Balakowicz63ff0042005-10-28 22:30:33 +020058 if (!eth_devices)
59 return NULL;
60
61 dev = eth_devices;
62 target_dev = NULL;
63 do {
64 if (strcmp(devname, dev->name) == 0) {
65 target_dev = dev;
66 break;
67 }
68 dev = dev->next;
69 } while (dev != eth_devices);
70
71 return target_dev;
72}
73
Andy Fleming9e569862009-02-11 15:07:24 -060074struct eth_device *eth_get_dev_by_index(int index)
75{
76 struct eth_device *dev, *target_dev;
Andy Fleming9e569862009-02-11 15:07:24 -060077
78 if (!eth_devices)
79 return NULL;
80
81 dev = eth_devices;
82 target_dev = NULL;
83 do {
Michael Wallefea7dca2011-10-27 11:31:35 +000084 if (dev->index == index) {
Andy Fleming9e569862009-02-11 15:07:24 -060085 target_dev = dev;
86 break;
87 }
88 dev = dev->next;
Andy Fleming9e569862009-02-11 15:07:24 -060089 } while (dev != eth_devices);
90
91 return target_dev;
92}
93
Joe Hershberger66c73852012-05-15 08:59:07 +000094int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +000095{
Joe Hershberger66c73852012-05-15 08:59:07 +000096 if (!eth_current)
Michael Wallefea7dca2011-10-27 11:31:35 +000097 return -1;
wdenkc6097192002-11-03 00:24:07 +000098
Michael Wallefea7dca2011-10-27 11:31:35 +000099 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +0000100}
101
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500102static int on_ethaddr(const char *name, const char *value, enum env_op op,
103 int flags)
104{
105 int index;
106 struct eth_device *dev;
107
108 if (!eth_devices)
109 return 0;
110
111 /* look for an index after "eth" */
112 index = simple_strtoul(name + 3, NULL, 10);
113
114 dev = eth_devices;
115 do {
116 if (dev->index == index) {
117 switch (op) {
118 case env_op_create:
119 case env_op_overwrite:
120 eth_parse_enetaddr(value, dev->enetaddr);
121 break;
122 case env_op_delete:
123 memset(dev->enetaddr, 0, 6);
124 }
125 }
Gong Qianyu7aba0f22015-08-31 11:34:43 +0800126 dev = dev->next;
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500127 } while (dev != eth_devices);
128
129 return 0;
130}
131U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
132
Simon Glass7616e782011-06-13 16:13:10 -0700133int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
134 int eth_number)
135{
136 unsigned char env_enetaddr[6];
137 int ret = 0;
138
Eric Miao69376642012-01-18 22:56:33 +0000139 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700140
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500141 if (!is_zero_ethaddr(env_enetaddr)) {
142 if (!is_zero_ethaddr(dev->enetaddr) &&
Joe Hershberger4c7c65a2015-03-22 17:09:01 -0500143 memcmp(dev->enetaddr, env_enetaddr, 6)) {
Simon Glass7616e782011-06-13 16:13:10 -0700144 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500145 dev->name);
Simon Glass7616e782011-06-13 16:13:10 -0700146 printf("Address in SROM is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500147 dev->enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700148 printf("Address in environment is %pM\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500149 env_enetaddr);
Simon Glass7616e782011-06-13 16:13:10 -0700150 }
151
152 memcpy(dev->enetaddr, env_enetaddr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500153 } else if (is_valid_ethaddr(dev->enetaddr)) {
Rob Herringc88ef3c2012-04-14 18:06:49 +0000154 eth_setenv_enetaddr_by_index(base_name, eth_number,
155 dev->enetaddr);
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500156 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershbergerbef10142015-05-04 14:55:13 -0500157#ifdef CONFIG_NET_RANDOM_ETHADDR
158 net_random_ethaddr(dev->enetaddr);
159 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
160 dev->name, eth_number, dev->enetaddr);
161#else
Pavel Machek75d9a452014-07-13 10:27:02 +0200162 printf("\nError: %s address not set.\n",
163 dev->name);
164 return -EINVAL;
Joe Hershbergerbef10142015-05-04 14:55:13 -0500165#endif
Simon Glass7616e782011-06-13 16:13:10 -0700166 }
167
Pavel Machek75d9a452014-07-13 10:27:02 +0200168 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500169 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek75d9a452014-07-13 10:27:02 +0200170 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerff819a32015-04-08 01:41:19 -0500171 dev->name, dev->enetaddr);
Pavel Machek75d9a452014-07-13 10:27:02 +0200172 return -EINVAL;
173 }
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000174
Simon Glass7616e782011-06-13 16:13:10 -0700175 ret = dev->write_hwaddr(dev);
Pavel Machek75d9a452014-07-13 10:27:02 +0200176 if (ret)
Joe Hershbergerff819a32015-04-08 01:41:19 -0500177 printf("\nWarning: %s failed to set MAC address\n",
178 dev->name);
Benoît Thébaudeau460f9492012-08-10 07:56:21 +0000179 }
Simon Glass7616e782011-06-13 16:13:10 -0700180
181 return ret;
182}
183
Simon Glass89d48362011-02-16 11:14:33 -0800184int eth_register(struct eth_device *dev)
185{
186 struct eth_device *d;
Joe Hershberger66c73852012-05-15 08:59:07 +0000187 static int index;
Michal Simek58c583b2011-08-29 23:30:13 +0000188
Mike Frysingerf6add132011-11-10 14:11:04 +0000189 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek58c583b2011-08-29 23:30:13 +0000190
Simon Glass89d48362011-02-16 11:14:33 -0800191 if (!eth_devices) {
Joe Hershbergerff819a32015-04-08 01:41:19 -0500192 eth_devices = dev;
193 eth_current = dev;
Simon Glass89d48362011-02-16 11:14:33 -0800194 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000195 } else {
Joe Hershberger66c73852012-05-15 08:59:07 +0000196 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundelaba4b692010-03-31 17:56:08 +0200197 ;
wdenkc6097192002-11-03 00:24:07 +0000198 d->next = dev;
199 }
200
201 dev->state = ETH_STATE_INIT;
202 dev->next = eth_devices;
Michael Wallefea7dca2011-10-27 11:31:35 +0000203 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000204
205 return 0;
206}
207
Vincent Palatine7e982d2012-01-09 08:32:36 +0000208int eth_unregister(struct eth_device *dev)
209{
210 struct eth_device *cur;
211
212 /* No device */
213 if (!eth_devices)
Joe Hershberger05324a42015-03-22 17:09:04 -0500214 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000215
216 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
217 cur = cur->next)
218 ;
219
220 /* Device not found */
221 if (cur->next != dev)
Joe Hershberger05324a42015-03-22 17:09:04 -0500222 return -ENODEV;
Vincent Palatine7e982d2012-01-09 08:32:36 +0000223
224 cur->next = dev->next;
225
226 if (eth_devices == dev)
227 eth_devices = dev->next == eth_devices ? NULL : dev->next;
228
229 if (eth_current == dev) {
230 eth_current = eth_devices;
231 eth_current_changed();
232 }
233
234 return 0;
235}
236
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500237int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000238{
Michael Wallefea7dca2011-10-27 11:31:35 +0000239 int num_devices = 0;
Simon Glass3bc42702015-04-05 16:07:37 -0600240
wdenkc6097192002-11-03 00:24:07 +0000241 eth_devices = NULL;
242 eth_current = NULL;
Simon Glass3bc42702015-04-05 16:07:37 -0600243 eth_common_init();
Simon Glass818f91e2016-01-17 14:51:57 -0700244 /*
245 * If board-specific initialization exists, call it.
246 * If not, call a CPU-specific one
247 */
248 if (board_eth_init != __def_eth_init) {
249 if (board_eth_init(gd->bd) < 0)
250 printf("Board Net Initialization Failed\n");
251 } else if (cpu_eth_init != __def_eth_init) {
252 if (cpu_eth_init(gd->bd) < 0)
253 printf("CPU Net Initialization Failed\n");
254 } else {
255 printf("Net Initialization Skipped\n");
256 }
Marian Balakowiczd9785c12005-11-30 18:06:04 +0100257
wdenkc6097192002-11-03 00:24:07 +0000258 if (!eth_devices) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000259 puts("No ethernet found.\n");
Simon Glass770605e2012-02-13 13:51:18 +0000260 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000261 } else {
262 struct eth_device *dev = eth_devices;
Joe Hershberger66c73852012-05-15 08:59:07 +0000263 char *ethprime = getenv("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000264
Simon Glass770605e2012-02-13 13:51:18 +0000265 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000266 do {
Michael Wallefea7dca2011-10-27 11:31:35 +0000267 if (dev->index)
Joe Hershberger66c73852012-05-15 08:59:07 +0000268 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000269
270 printf("%s", dev->name);
271
Joe Hershberger66c73852012-05-15 08:59:07 +0000272 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000273 eth_current = dev;
Joe Hershberger66c73852012-05-15 08:59:07 +0000274 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000275 }
276
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400277 if (strchr(dev->name, ' '))
Joe Hershberger66c73852012-05-15 08:59:07 +0000278 puts("\nWarning: eth device name has a space!"
279 "\n");
Mike Frysinger1384f3b2010-06-09 22:10:48 -0400280
Pavel Machek75d9a452014-07-13 10:27:02 +0200281 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000282
wdenkc6097192002-11-03 00:24:07 +0000283 dev = dev->next;
Michael Wallefea7dca2011-10-27 11:31:35 +0000284 num_devices++;
Joe Hershberger66c73852012-05-15 08:59:07 +0000285 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000286
Simon Glass89d48362011-02-16 11:14:33 -0800287 eth_current_changed();
Joe Hershberger66c73852012-05-15 08:59:07 +0000288 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000289 }
290
Michael Wallefea7dca2011-10-27 11:31:35 +0000291 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000292}
293
David Updegraff53a5c422007-06-11 10:41:07 -0500294#ifdef CONFIG_MCAST_TFTP
295/* Multicast.
296 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200297 * join: 1=join, 0=leave.
David Updegraff53a5c422007-06-11 10:41:07 -0500298 */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500299int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff53a5c422007-06-11 10:41:07 -0500300{
Joe Hershberger66c73852012-05-15 08:59:07 +0000301 u8 mcast_mac[6];
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200302 if (!eth_current || !eth_current->mcast)
David Updegraff53a5c422007-06-11 10:41:07 -0500303 return -1;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500304 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
305 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
306 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff53a5c422007-06-11 10:41:07 -0500307 mcast_mac[2] = 0x5e;
308 mcast_mac[1] = 0x0;
309 mcast_mac[0] = 0x1;
310 return eth_current->mcast(eth_current, mcast_mac, join);
311}
312
Wolfgang Denk85eb5ca2007-08-14 09:47:27 +0200313/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
314 * and this is the ethernet-crc method needed for TSEC -- and perhaps
David Updegraff53a5c422007-06-11 10:41:07 -0500315 * some other adapter -- hash tables
316 */
317#define CRCPOLY_LE 0xedb88320
Joe Hershberger66c73852012-05-15 08:59:07 +0000318u32 ether_crc(size_t len, unsigned char const *p)
David Updegraff53a5c422007-06-11 10:41:07 -0500319{
320 int i;
321 u32 crc;
322 crc = ~0;
323 while (len--) {
324 crc ^= *p++;
325 for (i = 0; i < 8; i++)
326 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
327 }
328 /* an reverse the bits, cuz of way they arrive -- last-first */
329 crc = (crc >> 16) | (crc << 16);
330 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
331 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
332 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
333 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
334 return crc;
335}
336
337#endif
338
wdenkc6097192002-11-03 00:24:07 +0000339
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500340int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000341{
Joe Hershberger6e0d26c2015-05-20 14:27:26 -0500342 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000343
Stefan Roese6bc11382008-03-04 17:40:41 +0100344 if (!eth_current) {
Joe Hershberger66c73852012-05-15 08:59:07 +0000345 puts("No ethernet found.\n");
Joe Hershberger05324a42015-03-22 17:09:04 -0500346 return -ENODEV;
Stefan Roese6bc11382008-03-04 17:40:41 +0100347 }
wdenkc6097192002-11-03 00:24:07 +0000348
349 old_current = eth_current;
350 do {
Robin Getz0ebf04c2009-07-23 03:01:03 -0400351 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000352
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500353 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000354 eth_current->state = ETH_STATE_ACTIVE;
355
Upakul Barkakaty505be872007-11-29 12:16:13 +0530356 return 0;
wdenkc6097192002-11-03 00:24:07 +0000357 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400358 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000359
360 eth_try_another(0);
361 } while (old_current != eth_current);
362
Joe Hershberger05324a42015-03-22 17:09:04 -0500363 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000364}
365
366void eth_halt(void)
367{
368 if (!eth_current)
369 return;
370
371 eth_current->halt(eth_current);
372
373 eth_current->state = ETH_STATE_PASSIVE;
374}
375
Bernhard Nortmanneaa8a192015-09-14 15:29:43 +0200376int eth_is_active(struct eth_device *dev)
377{
378 return dev && dev->state == ETH_STATE_ACTIVE;
379}
380
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000381int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000382{
383 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500384 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000385
386 return eth_current->send(eth_current, packet, length);
387}
388
389int eth_rx(void)
390{
391 if (!eth_current)
Joe Hershberger05324a42015-03-22 17:09:04 -0500392 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000393
394 return eth_current->recv(eth_current);
395}
396
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100397#ifdef CONFIG_API
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000398static void eth_save_packet(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100399{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000400 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100401 int i;
402
403 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
404 return;
405
406 if (PKTSIZE < length)
407 return;
408
409 for (i = 0; i < length; i++)
410 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
411
412 eth_rcv_bufs[eth_rcv_last].length = length;
413 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
414}
415
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000416int eth_receive(void *packet, int length)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100417{
Joe Hershbergerdb288a92012-05-15 08:59:04 +0000418 char *p = packet;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100419 void *pp = push_packet;
420 int i;
421
422 if (eth_rcv_current == eth_rcv_last) {
423 push_packet = eth_save_packet;
424 eth_rx();
425 push_packet = pp;
426
427 if (eth_rcv_current == eth_rcv_last)
428 return -1;
429 }
430
Michael Walle46c07bc2012-06-22 11:24:28 +0000431 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100432
433 for (i = 0; i < length; i++)
434 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
435
436 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
437 return length;
438}
439#endif /* CONFIG_API */