]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
c609719b | 2 | /* |
05c3e68f | 3 | * (C) Copyright 2001-2015 |
c609719b | 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
05c3e68f | 5 | * Joe Hershberger, National Instruments |
c609719b WD |
6 | */ |
7 | ||
8 | #include <common.h> | |
9 | #include <command.h> | |
6e0d26c0 | 10 | #include <environment.h> |
c609719b | 11 | #include <net.h> |
5f184715 | 12 | #include <phy.h> |
1221ce45 | 13 | #include <linux/errno.h> |
818f91eb | 14 | #include "eth_internal.h" |
c609719b | 15 | |
d2eaec60 JH |
16 | DECLARE_GLOBAL_DATA_PTR; |
17 | ||
3bc42700 SG |
18 | /* |
19 | * CPU and board-specific Ethernet initializations. Aliased function | |
20 | * signals caller to move on | |
21 | */ | |
22 | static int __def_eth_init(bd_t *bis) | |
23 | { | |
24 | return -1; | |
25 | } | |
26 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); | |
27 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); | |
28 | ||
f85b6071 | 29 | #ifdef CONFIG_API |
f85b6071 RJ |
30 | static struct { |
31 | uchar data[PKTSIZE]; | |
32 | int length; | |
33 | } eth_rcv_bufs[PKTBUFSRX]; | |
34 | ||
66c7385a | 35 | static unsigned int eth_rcv_current, eth_rcv_last; |
f85b6071 RJ |
36 | #endif |
37 | ||
f8be7d65 JH |
38 | static struct eth_device *eth_devices; |
39 | struct eth_device *eth_current; | |
c609719b | 40 | |
8607a6bf | 41 | void eth_set_current_to_next(void) |
84eb1fba JH |
42 | { |
43 | eth_current = eth_current->next; | |
44 | } | |
45 | ||
8607a6bf | 46 | void eth_set_dev(struct eth_device *dev) |
e58780dc JH |
47 | { |
48 | eth_current = dev; | |
49 | } | |
50 | ||
d7fb9bcf | 51 | struct eth_device *eth_get_dev_by_name(const char *devname) |
63ff004c MB |
52 | { |
53 | struct eth_device *dev, *target_dev; | |
54 | ||
7e7f903f HR |
55 | BUG_ON(devname == NULL); |
56 | ||
63ff004c MB |
57 | if (!eth_devices) |
58 | return NULL; | |
59 | ||
60 | dev = eth_devices; | |
61 | target_dev = NULL; | |
62 | do { | |
63 | if (strcmp(devname, dev->name) == 0) { | |
64 | target_dev = dev; | |
65 | break; | |
66 | } | |
67 | dev = dev->next; | |
68 | } while (dev != eth_devices); | |
69 | ||
70 | return target_dev; | |
71 | } | |
72 | ||
9e56986a AF |
73 | struct eth_device *eth_get_dev_by_index(int index) |
74 | { | |
75 | struct eth_device *dev, *target_dev; | |
9e56986a AF |
76 | |
77 | if (!eth_devices) | |
78 | return NULL; | |
79 | ||
80 | dev = eth_devices; | |
81 | target_dev = NULL; | |
82 | do { | |
fea7dcae | 83 | if (dev->index == index) { |
9e56986a AF |
84 | target_dev = dev; |
85 | break; | |
86 | } | |
87 | dev = dev->next; | |
9e56986a AF |
88 | } while (dev != eth_devices); |
89 | ||
90 | return target_dev; | |
91 | } | |
92 | ||
66c7385a | 93 | int eth_get_dev_index(void) |
c609719b | 94 | { |
66c7385a | 95 | if (!eth_current) |
fea7dcae | 96 | return -1; |
c609719b | 97 | |
fea7dcae | 98 | return eth_current->index; |
c609719b WD |
99 | } |
100 | ||
6e0d26c0 JH |
101 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
102 | int flags) | |
103 | { | |
104 | int index; | |
105 | struct eth_device *dev; | |
106 | ||
107 | if (!eth_devices) | |
108 | return 0; | |
109 | ||
110 | /* look for an index after "eth" */ | |
111 | index = simple_strtoul(name + 3, NULL, 10); | |
112 | ||
113 | dev = eth_devices; | |
114 | do { | |
115 | if (dev->index == index) { | |
116 | switch (op) { | |
117 | case env_op_create: | |
118 | case env_op_overwrite: | |
119 | eth_parse_enetaddr(value, dev->enetaddr); | |
73d570a7 | 120 | eth_write_hwaddr(dev, "eth", dev->index); |
6e0d26c0 JH |
121 | break; |
122 | case env_op_delete: | |
a40db6d5 | 123 | memset(dev->enetaddr, 0, ARP_HLEN); |
6e0d26c0 JH |
124 | } |
125 | } | |
7aba0f2c | 126 | dev = dev->next; |
6e0d26c0 JH |
127 | } while (dev != eth_devices); |
128 | ||
129 | return 0; | |
130 | } | |
131 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); | |
132 | ||
7616e785 SG |
133 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
134 | int eth_number) | |
135 | { | |
a40db6d5 | 136 | unsigned char env_enetaddr[ARP_HLEN]; |
7616e785 SG |
137 | int ret = 0; |
138 | ||
35affd7a | 139 | eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
7616e785 | 140 | |
0adb5b76 JH |
141 | if (!is_zero_ethaddr(env_enetaddr)) { |
142 | if (!is_zero_ethaddr(dev->enetaddr) && | |
a40db6d5 | 143 | memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) { |
7616e785 | 144 | printf("\nWarning: %s MAC addresses don't match:\n", |
ff819a3a | 145 | dev->name); |
7616e785 | 146 | printf("Address in SROM is %pM\n", |
ff819a3a | 147 | dev->enetaddr); |
7616e785 | 148 | printf("Address in environment is %pM\n", |
ff819a3a | 149 | env_enetaddr); |
7616e785 SG |
150 | } |
151 | ||
a40db6d5 | 152 | memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN); |
0adb5b76 | 153 | } else if (is_valid_ethaddr(dev->enetaddr)) { |
fd1e959e SG |
154 | eth_env_set_enetaddr_by_index(base_name, eth_number, |
155 | dev->enetaddr); | |
0adb5b76 | 156 | } else if (is_zero_ethaddr(dev->enetaddr)) { |
bef1014b JH |
157 | #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 | |
75d9a45c PM |
162 | printf("\nError: %s address not set.\n", |
163 | dev->name); | |
164 | return -EINVAL; | |
bef1014b | 165 | #endif |
7616e785 SG |
166 | } |
167 | ||
75d9a45c | 168 | if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { |
0adb5b76 | 169 | if (!is_valid_ethaddr(dev->enetaddr)) { |
75d9a45c | 170 | printf("\nError: %s address %pM illegal value\n", |
ff819a3a | 171 | dev->name, dev->enetaddr); |
75d9a45c PM |
172 | return -EINVAL; |
173 | } | |
460f949f | 174 | |
7616e785 | 175 | ret = dev->write_hwaddr(dev); |
75d9a45c | 176 | if (ret) |
ff819a3a JH |
177 | printf("\nWarning: %s failed to set MAC address\n", |
178 | dev->name); | |
460f949f | 179 | } |
7616e785 SG |
180 | |
181 | return ret; | |
182 | } | |
183 | ||
89d48367 SG |
184 | int eth_register(struct eth_device *dev) |
185 | { | |
186 | struct eth_device *d; | |
66c7385a | 187 | static int index; |
58c583b6 | 188 | |
f6add132 | 189 | assert(strlen(dev->name) < sizeof(dev->name)); |
58c583b6 | 190 | |
89d48367 | 191 | if (!eth_devices) { |
ff819a3a JH |
192 | eth_devices = dev; |
193 | eth_current = dev; | |
89d48367 | 194 | eth_current_changed(); |
c609719b | 195 | } else { |
66c7385a | 196 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
aba4b69d | 197 | ; |
c609719b WD |
198 | d->next = dev; |
199 | } | |
200 | ||
201 | dev->state = ETH_STATE_INIT; | |
202 | dev->next = eth_devices; | |
fea7dcae | 203 | dev->index = index++; |
c609719b WD |
204 | |
205 | return 0; | |
206 | } | |
207 | ||
e7e982d6 VP |
208 | int eth_unregister(struct eth_device *dev) |
209 | { | |
210 | struct eth_device *cur; | |
211 | ||
212 | /* No device */ | |
213 | if (!eth_devices) | |
05324a48 | 214 | return -ENODEV; |
e7e982d6 VP |
215 | |
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) | |
05324a48 | 222 | return -ENODEV; |
e7e982d6 VP |
223 | |
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 | ||
d2eaec60 | 237 | int eth_initialize(void) |
c609719b | 238 | { |
fea7dcae | 239 | int num_devices = 0; |
3bc42700 | 240 | |
c609719b WD |
241 | eth_devices = NULL; |
242 | eth_current = NULL; | |
3bc42700 | 243 | eth_common_init(); |
818f91eb SG |
244 | /* |
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 | } | |
d9785c14 | 257 | |
c609719b | 258 | if (!eth_devices) { |
66c7385a | 259 | puts("No ethernet found.\n"); |
770605e4 | 260 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
c609719b WD |
261 | } else { |
262 | struct eth_device *dev = eth_devices; | |
00caae6d | 263 | char *ethprime = env_get("ethprime"); |
c609719b | 264 | |
770605e4 | 265 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
c609719b | 266 | do { |
fea7dcae | 267 | if (dev->index) |
66c7385a | 268 | puts(", "); |
c609719b WD |
269 | |
270 | printf("%s", dev->name); | |
271 | ||
66c7385a | 272 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
c609719b | 273 | eth_current = dev; |
66c7385a | 274 | puts(" [PRIME]"); |
c609719b WD |
275 | } |
276 | ||
1384f3bb | 277 | if (strchr(dev->name, ' ')) |
66c7385a JH |
278 | puts("\nWarning: eth device name has a space!" |
279 | "\n"); | |
1384f3bb | 280 | |
75d9a45c | 281 | eth_write_hwaddr(dev, "eth", dev->index); |
c609719b | 282 | |
c609719b | 283 | dev = dev->next; |
fea7dcae | 284 | num_devices++; |
66c7385a | 285 | } while (dev != eth_devices); |
c609719b | 286 | |
89d48367 | 287 | eth_current_changed(); |
66c7385a | 288 | putc('\n'); |
c609719b WD |
289 | } |
290 | ||
fea7dcae | 291 | return num_devices; |
c609719b WD |
292 | } |
293 | ||
53a5c424 DU |
294 | /* Multicast. |
295 | * mcast_addr: multicast ipaddr from which multicast Mac is made | |
85eb5caf | 296 | * join: 1=join, 0=leave. |
53a5c424 | 297 | */ |
049a95a7 | 298 | int eth_mcast_join(struct in_addr mcast_ip, int join) |
53a5c424 | 299 | { |
a40db6d5 | 300 | u8 mcast_mac[ARP_HLEN]; |
85eb5caf | 301 | if (!eth_current || !eth_current->mcast) |
53a5c424 | 302 | return -1; |
049a95a7 JH |
303 | mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff; |
304 | mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff; | |
305 | mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f; | |
53a5c424 DU |
306 | mcast_mac[2] = 0x5e; |
307 | mcast_mac[1] = 0x0; | |
308 | mcast_mac[0] = 0x1; | |
309 | return eth_current->mcast(eth_current, mcast_mac, join); | |
310 | } | |
311 | ||
d2eaec60 | 312 | int eth_init(void) |
c609719b | 313 | { |
6e0d26c0 | 314 | struct eth_device *old_current; |
c609719b | 315 | |
6bc11388 | 316 | if (!eth_current) { |
66c7385a | 317 | puts("No ethernet found.\n"); |
05324a48 | 318 | return -ENODEV; |
6bc11388 | 319 | } |
c609719b WD |
320 | |
321 | old_current = eth_current; | |
322 | do { | |
0ebf04c6 | 323 | debug("Trying %s\n", eth_current->name); |
c609719b | 324 | |
d2eaec60 | 325 | if (eth_current->init(eth_current, gd->bd) >= 0) { |
c609719b WD |
326 | eth_current->state = ETH_STATE_ACTIVE; |
327 | ||
505be87a | 328 | return 0; |
c609719b | 329 | } |
0ebf04c6 | 330 | debug("FAIL\n"); |
c609719b WD |
331 | |
332 | eth_try_another(0); | |
333 | } while (old_current != eth_current); | |
334 | ||
05324a48 | 335 | return -ETIMEDOUT; |
c609719b WD |
336 | } |
337 | ||
338 | void eth_halt(void) | |
339 | { | |
340 | if (!eth_current) | |
341 | return; | |
342 | ||
343 | eth_current->halt(eth_current); | |
344 | ||
345 | eth_current->state = ETH_STATE_PASSIVE; | |
346 | } | |
347 | ||
eaa8a195 BN |
348 | int eth_is_active(struct eth_device *dev) |
349 | { | |
350 | return dev && dev->state == ETH_STATE_ACTIVE; | |
351 | } | |
352 | ||
db288a96 | 353 | int eth_send(void *packet, int length) |
c609719b WD |
354 | { |
355 | if (!eth_current) | |
05324a48 | 356 | return -ENODEV; |
c609719b WD |
357 | |
358 | return eth_current->send(eth_current, packet, length); | |
359 | } | |
360 | ||
361 | int eth_rx(void) | |
362 | { | |
363 | if (!eth_current) | |
05324a48 | 364 | return -ENODEV; |
c609719b WD |
365 | |
366 | return eth_current->recv(eth_current); | |
367 | } | |
368 | ||
f85b6071 | 369 | #ifdef CONFIG_API |
db288a96 | 370 | static void eth_save_packet(void *packet, int length) |
f85b6071 | 371 | { |
db288a96 | 372 | char *p = packet; |
f85b6071 RJ |
373 | int i; |
374 | ||
375 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) | |
376 | return; | |
377 | ||
378 | if (PKTSIZE < length) | |
379 | return; | |
380 | ||
381 | for (i = 0; i < length; i++) | |
382 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; | |
383 | ||
384 | eth_rcv_bufs[eth_rcv_last].length = length; | |
385 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; | |
386 | } | |
387 | ||
db288a96 | 388 | int eth_receive(void *packet, int length) |
f85b6071 | 389 | { |
db288a96 | 390 | char *p = packet; |
f85b6071 RJ |
391 | void *pp = push_packet; |
392 | int i; | |
393 | ||
394 | if (eth_rcv_current == eth_rcv_last) { | |
395 | push_packet = eth_save_packet; | |
396 | eth_rx(); | |
397 | push_packet = pp; | |
398 | ||
399 | if (eth_rcv_current == eth_rcv_last) | |
400 | return -1; | |
401 | } | |
402 | ||
46c07bcf | 403 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
f85b6071 RJ |
404 | |
405 | for (i = 0; i < length; i++) | |
406 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; | |
407 | ||
408 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; | |
409 | return length; | |
410 | } | |
411 | #endif /* CONFIG_API */ |