]> git.ipfire.org Git - people/ms/u-boot.git/blame - net/eth_legacy.c
arcv2: Set IOC aperture so it covers available DDR
[people/ms/u-boot.git] / net / eth_legacy.c
CommitLineData
c609719b 1/*
05c3e68f 2 * (C) Copyright 2001-2015
c609719b 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
05c3e68f 4 * Joe Hershberger, National Instruments
c609719b 5 *
1a459660 6 * SPDX-License-Identifier: GPL-2.0+
c609719b
WD
7 */
8
9#include <common.h>
10#include <command.h>
6e0d26c0 11#include <environment.h>
c609719b 12#include <net.h>
5f184715 13#include <phy.h>
1221ce45 14#include <linux/errno.h>
818f91eb 15#include "eth_internal.h"
c609719b 16
d2eaec60
JH
17DECLARE_GLOBAL_DATA_PTR;
18
3bc42700
SG
19/*
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
f85b6071 30#ifdef CONFIG_API
f85b6071
RJ
31static struct {
32 uchar data[PKTSIZE];
33 int length;
34} eth_rcv_bufs[PKTBUFSRX];
35
66c7385a 36static unsigned int eth_rcv_current, eth_rcv_last;
f85b6071
RJ
37#endif
38
f8be7d65
JH
39static struct eth_device *eth_devices;
40struct eth_device *eth_current;
c609719b 41
8607a6bf 42void eth_set_current_to_next(void)
84eb1fba
JH
43{
44 eth_current = eth_current->next;
45}
46
8607a6bf 47void eth_set_dev(struct eth_device *dev)
e58780dc
JH
48{
49 eth_current = dev;
50}
51
d7fb9bcf 52struct eth_device *eth_get_dev_by_name(const char *devname)
63ff004c
MB
53{
54 struct eth_device *dev, *target_dev;
55
7e7f903f
HR
56 BUG_ON(devname == NULL);
57
63ff004c
MB
58 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
9e56986a
AF
74struct eth_device *eth_get_dev_by_index(int index)
75{
76 struct eth_device *dev, *target_dev;
9e56986a
AF
77
78 if (!eth_devices)
79 return NULL;
80
81 dev = eth_devices;
82 target_dev = NULL;
83 do {
fea7dcae 84 if (dev->index == index) {
9e56986a
AF
85 target_dev = dev;
86 break;
87 }
88 dev = dev->next;
9e56986a
AF
89 } while (dev != eth_devices);
90
91 return target_dev;
92}
93
66c7385a 94int eth_get_dev_index(void)
c609719b 95{
66c7385a 96 if (!eth_current)
fea7dcae 97 return -1;
c609719b 98
fea7dcae 99 return eth_current->index;
c609719b
WD
100}
101
6e0d26c0
JH
102static 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);
73d570a7 121 eth_write_hwaddr(dev, "eth", dev->index);
6e0d26c0
JH
122 break;
123 case env_op_delete:
a40db6d5 124 memset(dev->enetaddr, 0, ARP_HLEN);
6e0d26c0
JH
125 }
126 }
7aba0f2c 127 dev = dev->next;
6e0d26c0
JH
128 } while (dev != eth_devices);
129
130 return 0;
131}
132U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
133
7616e785
SG
134int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
135 int eth_number)
136{
a40db6d5 137 unsigned char env_enetaddr[ARP_HLEN];
7616e785
SG
138 int ret = 0;
139
69376644 140 eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
7616e785 141
0adb5b76
JH
142 if (!is_zero_ethaddr(env_enetaddr)) {
143 if (!is_zero_ethaddr(dev->enetaddr) &&
a40db6d5 144 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
7616e785 145 printf("\nWarning: %s MAC addresses don't match:\n",
ff819a3a 146 dev->name);
7616e785 147 printf("Address in SROM is %pM\n",
ff819a3a 148 dev->enetaddr);
7616e785 149 printf("Address in environment is %pM\n",
ff819a3a 150 env_enetaddr);
7616e785
SG
151 }
152
a40db6d5 153 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
0adb5b76 154 } else if (is_valid_ethaddr(dev->enetaddr)) {
c88ef3c1
RH
155 eth_setenv_enetaddr_by_index(base_name, eth_number,
156 dev->enetaddr);
0adb5b76 157 } else if (is_zero_ethaddr(dev->enetaddr)) {
bef1014b
JH
158#ifdef CONFIG_NET_RANDOM_ETHADDR
159 net_random_ethaddr(dev->enetaddr);
160 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
161 dev->name, eth_number, dev->enetaddr);
162#else
75d9a45c
PM
163 printf("\nError: %s address not set.\n",
164 dev->name);
165 return -EINVAL;
bef1014b 166#endif
7616e785
SG
167 }
168
75d9a45c 169 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
0adb5b76 170 if (!is_valid_ethaddr(dev->enetaddr)) {
75d9a45c 171 printf("\nError: %s address %pM illegal value\n",
ff819a3a 172 dev->name, dev->enetaddr);
75d9a45c
PM
173 return -EINVAL;
174 }
460f949f 175
7616e785 176 ret = dev->write_hwaddr(dev);
75d9a45c 177 if (ret)
ff819a3a
JH
178 printf("\nWarning: %s failed to set MAC address\n",
179 dev->name);
460f949f 180 }
7616e785
SG
181
182 return ret;
183}
184
89d48367
SG
185int eth_register(struct eth_device *dev)
186{
187 struct eth_device *d;
66c7385a 188 static int index;
58c583b6 189
f6add132 190 assert(strlen(dev->name) < sizeof(dev->name));
58c583b6 191
89d48367 192 if (!eth_devices) {
ff819a3a
JH
193 eth_devices = dev;
194 eth_current = dev;
89d48367 195 eth_current_changed();
c609719b 196 } else {
66c7385a 197 for (d = eth_devices; d->next != eth_devices; d = d->next)
aba4b69d 198 ;
c609719b
WD
199 d->next = dev;
200 }
201
202 dev->state = ETH_STATE_INIT;
203 dev->next = eth_devices;
fea7dcae 204 dev->index = index++;
c609719b
WD
205
206 return 0;
207}
208
e7e982d6
VP
209int eth_unregister(struct eth_device *dev)
210{
211 struct eth_device *cur;
212
213 /* No device */
214 if (!eth_devices)
05324a48 215 return -ENODEV;
e7e982d6
VP
216
217 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
218 cur = cur->next)
219 ;
220
221 /* Device not found */
222 if (cur->next != dev)
05324a48 223 return -ENODEV;
e7e982d6
VP
224
225 cur->next = dev->next;
226
227 if (eth_devices == dev)
228 eth_devices = dev->next == eth_devices ? NULL : dev->next;
229
230 if (eth_current == dev) {
231 eth_current = eth_devices;
232 eth_current_changed();
233 }
234
235 return 0;
236}
237
d2eaec60 238int eth_initialize(void)
c609719b 239{
fea7dcae 240 int num_devices = 0;
3bc42700 241
c609719b
WD
242 eth_devices = NULL;
243 eth_current = NULL;
3bc42700 244 eth_common_init();
818f91eb
SG
245 /*
246 * If board-specific initialization exists, call it.
247 * If not, call a CPU-specific one
248 */
249 if (board_eth_init != __def_eth_init) {
250 if (board_eth_init(gd->bd) < 0)
251 printf("Board Net Initialization Failed\n");
252 } else if (cpu_eth_init != __def_eth_init) {
253 if (cpu_eth_init(gd->bd) < 0)
254 printf("CPU Net Initialization Failed\n");
255 } else {
256 printf("Net Initialization Skipped\n");
257 }
d9785c14 258
c609719b 259 if (!eth_devices) {
66c7385a 260 puts("No ethernet found.\n");
770605e4 261 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
c609719b
WD
262 } else {
263 struct eth_device *dev = eth_devices;
66c7385a 264 char *ethprime = getenv("ethprime");
c609719b 265
770605e4 266 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
c609719b 267 do {
fea7dcae 268 if (dev->index)
66c7385a 269 puts(", ");
c609719b
WD
270
271 printf("%s", dev->name);
272
66c7385a 273 if (ethprime && strcmp(dev->name, ethprime) == 0) {
c609719b 274 eth_current = dev;
66c7385a 275 puts(" [PRIME]");
c609719b
WD
276 }
277
1384f3bb 278 if (strchr(dev->name, ' '))
66c7385a
JH
279 puts("\nWarning: eth device name has a space!"
280 "\n");
1384f3bb 281
75d9a45c 282 eth_write_hwaddr(dev, "eth", dev->index);
c609719b 283
c609719b 284 dev = dev->next;
fea7dcae 285 num_devices++;
66c7385a 286 } while (dev != eth_devices);
c609719b 287
89d48367 288 eth_current_changed();
66c7385a 289 putc('\n');
c609719b
WD
290 }
291
fea7dcae 292 return num_devices;
c609719b
WD
293}
294
53a5c424
DU
295#ifdef CONFIG_MCAST_TFTP
296/* Multicast.
297 * mcast_addr: multicast ipaddr from which multicast Mac is made
85eb5caf 298 * join: 1=join, 0=leave.
53a5c424 299 */
049a95a7 300int eth_mcast_join(struct in_addr mcast_ip, int join)
53a5c424 301{
a40db6d5 302 u8 mcast_mac[ARP_HLEN];
85eb5caf 303 if (!eth_current || !eth_current->mcast)
53a5c424 304 return -1;
049a95a7
JH
305 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
306 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
307 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
53a5c424
DU
308 mcast_mac[2] = 0x5e;
309 mcast_mac[1] = 0x0;
310 mcast_mac[0] = 0x1;
311 return eth_current->mcast(eth_current, mcast_mac, join);
312}
313
85eb5caf
WD
314/* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
315 * and this is the ethernet-crc method needed for TSEC -- and perhaps
53a5c424
DU
316 * some other adapter -- hash tables
317 */
318#define CRCPOLY_LE 0xedb88320
66c7385a 319u32 ether_crc(size_t len, unsigned char const *p)
53a5c424
DU
320{
321 int i;
322 u32 crc;
323 crc = ~0;
324 while (len--) {
325 crc ^= *p++;
326 for (i = 0; i < 8; i++)
327 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
328 }
329 /* an reverse the bits, cuz of way they arrive -- last-first */
330 crc = (crc >> 16) | (crc << 16);
331 crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
332 crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
333 crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
334 crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
335 return crc;
336}
337
338#endif
339
c609719b 340
d2eaec60 341int eth_init(void)
c609719b 342{
6e0d26c0 343 struct eth_device *old_current;
c609719b 344
6bc11388 345 if (!eth_current) {
66c7385a 346 puts("No ethernet found.\n");
05324a48 347 return -ENODEV;
6bc11388 348 }
c609719b
WD
349
350 old_current = eth_current;
351 do {
0ebf04c6 352 debug("Trying %s\n", eth_current->name);
c609719b 353
d2eaec60 354 if (eth_current->init(eth_current, gd->bd) >= 0) {
c609719b
WD
355 eth_current->state = ETH_STATE_ACTIVE;
356
505be87a 357 return 0;
c609719b 358 }
0ebf04c6 359 debug("FAIL\n");
c609719b
WD
360
361 eth_try_another(0);
362 } while (old_current != eth_current);
363
05324a48 364 return -ETIMEDOUT;
c609719b
WD
365}
366
367void eth_halt(void)
368{
369 if (!eth_current)
370 return;
371
372 eth_current->halt(eth_current);
373
374 eth_current->state = ETH_STATE_PASSIVE;
375}
376
eaa8a195
BN
377int eth_is_active(struct eth_device *dev)
378{
379 return dev && dev->state == ETH_STATE_ACTIVE;
380}
381
db288a96 382int eth_send(void *packet, int length)
c609719b
WD
383{
384 if (!eth_current)
05324a48 385 return -ENODEV;
c609719b
WD
386
387 return eth_current->send(eth_current, packet, length);
388}
389
390int eth_rx(void)
391{
392 if (!eth_current)
05324a48 393 return -ENODEV;
c609719b
WD
394
395 return eth_current->recv(eth_current);
396}
397
f85b6071 398#ifdef CONFIG_API
db288a96 399static void eth_save_packet(void *packet, int length)
f85b6071 400{
db288a96 401 char *p = packet;
f85b6071
RJ
402 int i;
403
404 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
405 return;
406
407 if (PKTSIZE < length)
408 return;
409
410 for (i = 0; i < length; i++)
411 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
412
413 eth_rcv_bufs[eth_rcv_last].length = length;
414 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
415}
416
db288a96 417int eth_receive(void *packet, int length)
f85b6071 418{
db288a96 419 char *p = packet;
f85b6071
RJ
420 void *pp = push_packet;
421 int i;
422
423 if (eth_rcv_current == eth_rcv_last) {
424 push_packet = eth_save_packet;
425 eth_rx();
426 push_packet = pp;
427
428 if (eth_rcv_current == eth_rcv_last)
429 return -1;
430 }
431
46c07bcf 432 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
f85b6071
RJ
433
434 for (i = 0; i < length; i++)
435 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
436
437 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
438 return length;
439}
440#endif /* CONFIG_API */