]>
Commit | Line | Data |
---|---|---|
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> | |
05c3e68f | 11 | #include <dm.h> |
6e0d26c0 | 12 | #include <environment.h> |
c609719b | 13 | #include <net.h> |
d9785c14 | 14 | #include <miiphy.h> |
5f184715 | 15 | #include <phy.h> |
75d9a45c | 16 | #include <asm/errno.h> |
05c3e68f | 17 | #include <dm/device-internal.h> |
6e0d26c0 | 18 | #include <dm/uclass-internal.h> |
c609719b | 19 | |
d2eaec60 JH |
20 | DECLARE_GLOBAL_DATA_PTR; |
21 | ||
3f6e6993 MF |
22 | void eth_parse_enetaddr(const char *addr, uchar *enetaddr) |
23 | { | |
24 | char *end; | |
25 | int i; | |
26 | ||
27 | for (i = 0; i < 6; ++i) { | |
28 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; | |
29 | if (addr) | |
30 | addr = (*end) ? end + 1 : end; | |
31 | } | |
32 | } | |
33 | ||
219cc94a | 34 | int eth_getenv_enetaddr(const char *name, uchar *enetaddr) |
3f6e6993 MF |
35 | { |
36 | eth_parse_enetaddr(getenv(name), enetaddr); | |
0adb5b76 | 37 | return is_valid_ethaddr(enetaddr); |
3f6e6993 MF |
38 | } |
39 | ||
219cc94a | 40 | int eth_setenv_enetaddr(const char *name, const uchar *enetaddr) |
3f6e6993 MF |
41 | { |
42 | char buf[20]; | |
43 | ||
44 | sprintf(buf, "%pM", enetaddr); | |
45 | ||
46 | return setenv(name, buf); | |
47 | } | |
86848a74 | 48 | |
7616e785 SG |
49 | int eth_getenv_enetaddr_by_index(const char *base_name, int index, |
50 | uchar *enetaddr) | |
86848a74 MF |
51 | { |
52 | char enetvar[32]; | |
7616e785 | 53 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
86848a74 MF |
54 | return eth_getenv_enetaddr(enetvar, enetaddr); |
55 | } | |
3f6e6993 | 56 | |
154177e1 | 57 | static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, |
c88ef3c1 RH |
58 | uchar *enetaddr) |
59 | { | |
60 | char enetvar[32]; | |
61 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); | |
62 | return eth_setenv_enetaddr(enetvar, enetaddr); | |
63 | } | |
64 | ||
ecee9324 BW |
65 | static int eth_mac_skip(int index) |
66 | { | |
67 | char enetvar[15]; | |
68 | char *skip_state; | |
ff819a3a | 69 | |
ecee9324 | 70 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
ff819a3a JH |
71 | skip_state = getenv(enetvar); |
72 | return skip_state != NULL; | |
ecee9324 BW |
73 | } |
74 | ||
84eb1fba JH |
75 | static void eth_current_changed(void); |
76 | ||
3bc42700 SG |
77 | /* |
78 | * CPU and board-specific Ethernet initializations. Aliased function | |
79 | * signals caller to move on | |
80 | */ | |
81 | static int __def_eth_init(bd_t *bis) | |
82 | { | |
83 | return -1; | |
84 | } | |
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"))); | |
87 | ||
88 | static void eth_common_init(void) | |
89 | { | |
90 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); | |
91 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB) | |
92 | miiphy_init(); | |
93 | #endif | |
94 | ||
95 | #ifdef CONFIG_PHYLIB | |
96 | phy_init(); | |
97 | #endif | |
98 | ||
3bc42700 SG |
99 | /* |
100 | * If board-specific initialization exists, call it. | |
101 | * If not, call a CPU-specific one | |
102 | */ | |
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"); | |
109 | } else { | |
d8f79afa | 110 | #ifndef CONFIG_DM_ETH |
3bc42700 | 111 | printf("Net Initialization Skipped\n"); |
d8f79afa | 112 | #endif |
3bc42700 SG |
113 | } |
114 | } | |
115 | ||
05c3e68f JH |
116 | #ifdef CONFIG_DM_ETH |
117 | /** | |
118 | * struct eth_device_priv - private structure for each Ethernet device | |
119 | * | |
120 | * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t) | |
121 | */ | |
122 | struct eth_device_priv { | |
123 | enum eth_state_t state; | |
124 | }; | |
125 | ||
126 | /** | |
127 | * struct eth_uclass_priv - The structure attached to the uclass itself | |
128 | * | |
129 | * @current: The Ethernet device that the network functions are using | |
130 | */ | |
131 | struct eth_uclass_priv { | |
132 | struct udevice *current; | |
133 | }; | |
134 | ||
60304592 JH |
135 | /* eth_errno - This stores the most recent failure code from DM functions */ |
136 | static int eth_errno; | |
137 | ||
05c3e68f JH |
138 | static struct eth_uclass_priv *eth_get_uclass_priv(void) |
139 | { | |
140 | struct uclass *uc; | |
141 | ||
142 | uclass_get(UCLASS_ETH, &uc); | |
143 | assert(uc); | |
144 | return uc->priv; | |
145 | } | |
146 | ||
147 | static void eth_set_current_to_next(void) | |
148 | { | |
149 | struct eth_uclass_priv *uc_priv; | |
150 | ||
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); | |
156 | } | |
157 | ||
60304592 JH |
158 | /* |
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. | |
163 | */ | |
05c3e68f JH |
164 | struct udevice *eth_get_dev(void) |
165 | { | |
166 | struct eth_uclass_priv *uc_priv; | |
167 | ||
168 | uc_priv = eth_get_uclass_priv(); | |
169 | if (!uc_priv->current) | |
60304592 | 170 | eth_errno = uclass_first_device(UCLASS_ETH, |
05c3e68f JH |
171 | &uc_priv->current); |
172 | return uc_priv->current; | |
173 | } | |
174 | ||
60304592 JH |
175 | /* |
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. | |
179 | */ | |
05c3e68f JH |
180 | static void eth_set_dev(struct udevice *dev) |
181 | { | |
ac1d3138 | 182 | if (dev && !device_active(dev)) { |
60304592 | 183 | eth_errno = device_probe(dev); |
ac1d3138 BM |
184 | if (eth_errno) |
185 | dev = NULL; | |
186 | } | |
187 | ||
05c3e68f JH |
188 | eth_get_uclass_priv()->current = dev; |
189 | } | |
190 | ||
e58780dc JH |
191 | /* |
192 | * Find the udevice that either has the name passed in as devname or has an | |
193 | * alias named devname. | |
194 | */ | |
195 | struct udevice *eth_get_dev_by_name(const char *devname) | |
196 | { | |
197 | int seq = -1; | |
198 | char *endp = NULL; | |
199 | const char *startp = NULL; | |
200 | struct udevice *it; | |
201 | struct uclass *uc; | |
e408c421 | 202 | int len = strlen("eth"); |
e58780dc JH |
203 | |
204 | /* Must be longer than 3 to be an alias */ | |
e408c421 BM |
205 | if (!strncmp(devname, "eth", len) && strlen(devname) > len) { |
206 | startp = devname + len; | |
e58780dc JH |
207 | seq = simple_strtoul(startp, &endp, 10); |
208 | } | |
209 | ||
210 | uclass_get(UCLASS_ETH, &uc); | |
211 | uclass_foreach_dev(it, uc) { | |
60304592 JH |
212 | /* |
213 | * We need the seq to be valid, so try to probe it. | |
214 | * If the probe fails, the seq will not match since it will be | |
215 | * -1 instead of what we are looking for. | |
216 | * We don't care about errors from probe here. Either they won't | |
217 | * match an alias or it will match a literal name and we'll pick | |
218 | * up the error when we try to probe again in eth_set_dev(). | |
219 | */ | |
ac1d3138 BM |
220 | if (device_probe(it)) |
221 | continue; | |
222 | /* Check for the name or the sequence number to match */ | |
e58780dc JH |
223 | if (strcmp(it->name, devname) == 0 || |
224 | (endp > startp && it->seq == seq)) | |
225 | return it; | |
226 | } | |
227 | ||
228 | return NULL; | |
229 | } | |
230 | ||
05c3e68f JH |
231 | unsigned char *eth_get_ethaddr(void) |
232 | { | |
233 | struct eth_pdata *pdata; | |
234 | ||
235 | if (eth_get_dev()) { | |
236 | pdata = eth_get_dev()->platdata; | |
237 | return pdata->enetaddr; | |
238 | } | |
239 | ||
240 | return NULL; | |
241 | } | |
242 | ||
243 | /* Set active state without calling start on the driver */ | |
244 | int eth_init_state_only(void) | |
245 | { | |
246 | struct udevice *current; | |
247 | struct eth_device_priv *priv; | |
248 | ||
249 | current = eth_get_dev(); | |
250 | if (!current || !device_active(current)) | |
251 | return -EINVAL; | |
252 | ||
253 | priv = current->uclass_priv; | |
254 | priv->state = ETH_STATE_ACTIVE; | |
255 | ||
256 | return 0; | |
257 | } | |
258 | ||
259 | /* Set passive state without calling stop on the driver */ | |
260 | void eth_halt_state_only(void) | |
261 | { | |
262 | struct udevice *current; | |
263 | struct eth_device_priv *priv; | |
264 | ||
265 | current = eth_get_dev(); | |
266 | if (!current || !device_active(current)) | |
267 | return; | |
268 | ||
269 | priv = current->uclass_priv; | |
270 | priv->state = ETH_STATE_PASSIVE; | |
271 | } | |
272 | ||
273 | int eth_get_dev_index(void) | |
274 | { | |
275 | if (eth_get_dev()) | |
276 | return eth_get_dev()->seq; | |
277 | return -1; | |
278 | } | |
279 | ||
f566c994 JH |
280 | static int eth_write_hwaddr(struct udevice *dev) |
281 | { | |
282 | struct eth_pdata *pdata = dev->platdata; | |
283 | int ret = 0; | |
284 | ||
285 | if (!dev || !device_active(dev)) | |
286 | return -EINVAL; | |
287 | ||
288 | /* seq is valid since the device is active */ | |
289 | if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) { | |
290 | if (!is_valid_ethaddr(pdata->enetaddr)) { | |
291 | printf("\nError: %s address %pM illegal value\n", | |
292 | dev->name, pdata->enetaddr); | |
293 | return -EINVAL; | |
294 | } | |
295 | ||
b86f795a SG |
296 | /* |
297 | * Drivers are allowed to decide not to implement this at | |
298 | * run-time. E.g. Some devices may use it and some may not. | |
299 | */ | |
f566c994 | 300 | ret = eth_get_ops(dev)->write_hwaddr(dev); |
b86f795a SG |
301 | if (ret == -ENOSYS) |
302 | ret = 0; | |
f566c994 JH |
303 | if (ret) |
304 | printf("\nWarning: %s failed to set MAC address\n", | |
305 | dev->name); | |
306 | } | |
307 | ||
308 | return ret; | |
309 | } | |
310 | ||
6e0d26c0 JH |
311 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
312 | int flags) | |
313 | { | |
314 | int index; | |
315 | int retval; | |
316 | struct udevice *dev; | |
317 | ||
318 | /* look for an index after "eth" */ | |
319 | index = simple_strtoul(name + 3, NULL, 10); | |
320 | ||
321 | retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev); | |
322 | if (!retval) { | |
323 | struct eth_pdata *pdata = dev->platdata; | |
324 | switch (op) { | |
325 | case env_op_create: | |
326 | case env_op_overwrite: | |
327 | eth_parse_enetaddr(value, pdata->enetaddr); | |
328 | break; | |
329 | case env_op_delete: | |
330 | memset(pdata->enetaddr, 0, 6); | |
331 | } | |
332 | } | |
333 | ||
334 | return 0; | |
335 | } | |
336 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); | |
337 | ||
05c3e68f JH |
338 | int eth_init(void) |
339 | { | |
4cdc2c8c BM |
340 | char *ethact = getenv("ethact"); |
341 | char *ethrotate = getenv("ethrotate"); | |
342 | struct udevice *current = NULL; | |
05c3e68f | 343 | struct udevice *old_current; |
60304592 | 344 | int ret = -ENODEV; |
05c3e68f | 345 | |
4cdc2c8c BM |
346 | /* |
347 | * When 'ethrotate' variable is set to 'no' and 'ethact' variable | |
348 | * is already set to an ethernet device, we should stick to 'ethact'. | |
349 | */ | |
350 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) { | |
351 | if (ethact) { | |
352 | current = eth_get_dev_by_name(ethact); | |
353 | if (!current) | |
354 | return -EINVAL; | |
355 | } | |
356 | } | |
357 | ||
05c3e68f | 358 | if (!current) { |
4cdc2c8c BM |
359 | current = eth_get_dev(); |
360 | if (!current) { | |
361 | printf("No ethernet found.\n"); | |
362 | return -ENODEV; | |
363 | } | |
05c3e68f JH |
364 | } |
365 | ||
366 | old_current = current; | |
367 | do { | |
ac1d3138 BM |
368 | if (current) { |
369 | debug("Trying %s\n", current->name); | |
370 | ||
371 | if (device_active(current)) { | |
372 | ret = eth_get_ops(current)->start(current); | |
373 | if (ret >= 0) { | |
374 | struct eth_device_priv *priv = | |
375 | current->uclass_priv; | |
376 | ||
377 | priv->state = ETH_STATE_ACTIVE; | |
378 | return 0; | |
379 | } | |
380 | } else { | |
381 | ret = eth_errno; | |
05c3e68f | 382 | } |
ac1d3138 BM |
383 | |
384 | debug("FAIL\n"); | |
ff819a3a | 385 | } else { |
ac1d3138 | 386 | debug("PROBE FAIL\n"); |
ff819a3a | 387 | } |
60304592 | 388 | |
60304592 JH |
389 | /* |
390 | * If ethrotate is enabled, this will change "current", | |
391 | * otherwise we will drop out of this while loop immediately | |
392 | */ | |
05c3e68f | 393 | eth_try_another(0); |
60304592 | 394 | /* This will ensure the new "current" attempted to probe */ |
05c3e68f JH |
395 | current = eth_get_dev(); |
396 | } while (old_current != current); | |
397 | ||
60304592 | 398 | return ret; |
05c3e68f JH |
399 | } |
400 | ||
401 | void eth_halt(void) | |
402 | { | |
403 | struct udevice *current; | |
404 | struct eth_device_priv *priv; | |
405 | ||
406 | current = eth_get_dev(); | |
407 | if (!current || !device_active(current)) | |
408 | return; | |
409 | ||
410 | eth_get_ops(current)->stop(current); | |
411 | priv = current->uclass_priv; | |
412 | priv->state = ETH_STATE_PASSIVE; | |
413 | } | |
414 | ||
eaa8a195 BN |
415 | int eth_is_active(struct udevice *dev) |
416 | { | |
417 | struct eth_device_priv *priv; | |
418 | ||
419 | if (!dev || !device_active(dev)) | |
420 | return 0; | |
421 | ||
422 | priv = dev_get_uclass_priv(dev); | |
423 | return priv->state == ETH_STATE_ACTIVE; | |
424 | } | |
425 | ||
05c3e68f JH |
426 | int eth_send(void *packet, int length) |
427 | { | |
428 | struct udevice *current; | |
60304592 | 429 | int ret; |
05c3e68f JH |
430 | |
431 | current = eth_get_dev(); | |
432 | if (!current) | |
433 | return -ENODEV; | |
434 | ||
435 | if (!device_active(current)) | |
436 | return -EINVAL; | |
437 | ||
60304592 JH |
438 | ret = eth_get_ops(current)->send(current, packet, length); |
439 | if (ret < 0) { | |
440 | /* We cannot completely return the error at present */ | |
441 | debug("%s: send() returned error %d\n", __func__, ret); | |
442 | } | |
443 | return ret; | |
05c3e68f JH |
444 | } |
445 | ||
446 | int eth_rx(void) | |
447 | { | |
448 | struct udevice *current; | |
17591405 | 449 | uchar *packet; |
a1ca92ea | 450 | int flags; |
17591405 JH |
451 | int ret; |
452 | int i; | |
05c3e68f JH |
453 | |
454 | current = eth_get_dev(); | |
455 | if (!current) | |
456 | return -ENODEV; | |
457 | ||
458 | if (!device_active(current)) | |
459 | return -EINVAL; | |
460 | ||
17591405 | 461 | /* Process up to 32 packets at one time */ |
a1ca92ea | 462 | flags = ETH_RECV_CHECK_DEVICE; |
17591405 | 463 | for (i = 0; i < 32; i++) { |
a1ca92ea SG |
464 | ret = eth_get_ops(current)->recv(current, flags, &packet); |
465 | flags = 0; | |
17591405 JH |
466 | if (ret > 0) |
467 | net_process_received_packet(packet, ret); | |
63c9729a JH |
468 | if (ret >= 0 && eth_get_ops(current)->free_pkt) |
469 | eth_get_ops(current)->free_pkt(current, packet, ret); | |
470 | if (ret <= 0) | |
17591405 JH |
471 | break; |
472 | } | |
473 | if (ret == -EAGAIN) | |
474 | ret = 0; | |
60304592 JH |
475 | if (ret < 0) { |
476 | /* We cannot completely return the error at present */ | |
477 | debug("%s: recv() returned error %d\n", __func__, ret); | |
478 | } | |
17591405 | 479 | return ret; |
05c3e68f JH |
480 | } |
481 | ||
05c3e68f JH |
482 | int eth_initialize(void) |
483 | { | |
484 | int num_devices = 0; | |
485 | struct udevice *dev; | |
486 | ||
3bc42700 | 487 | eth_common_init(); |
05c3e68f JH |
488 | |
489 | /* | |
490 | * Devices need to write the hwaddr even if not started so that Linux | |
491 | * will have access to the hwaddr that u-boot stored for the device. | |
492 | * This is accomplished by attempting to probe each device and calling | |
493 | * their write_hwaddr() operation. | |
494 | */ | |
495 | uclass_first_device(UCLASS_ETH, &dev); | |
496 | if (!dev) { | |
497 | printf("No ethernet found.\n"); | |
498 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); | |
499 | } else { | |
6536b9bb JH |
500 | char *ethprime = getenv("ethprime"); |
501 | struct udevice *prime_dev = NULL; | |
502 | ||
503 | if (ethprime) | |
504 | prime_dev = eth_get_dev_by_name(ethprime); | |
505 | if (prime_dev) { | |
506 | eth_set_dev(prime_dev); | |
507 | eth_current_changed(); | |
508 | } else { | |
509 | eth_set_dev(NULL); | |
510 | } | |
511 | ||
05c3e68f JH |
512 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
513 | do { | |
514 | if (num_devices) | |
515 | printf(", "); | |
516 | ||
517 | printf("eth%d: %s", dev->seq, dev->name); | |
518 | ||
6536b9bb JH |
519 | if (ethprime && dev == prime_dev) |
520 | printf(" [PRIME]"); | |
521 | ||
05c3e68f JH |
522 | eth_write_hwaddr(dev); |
523 | ||
524 | uclass_next_device(&dev); | |
525 | num_devices++; | |
526 | } while (dev); | |
527 | ||
528 | putc('\n'); | |
529 | } | |
530 | ||
531 | return num_devices; | |
532 | } | |
533 | ||
534 | static int eth_post_bind(struct udevice *dev) | |
535 | { | |
536 | if (strchr(dev->name, ' ')) { | |
537 | printf("\nError: eth device name \"%s\" has a space!\n", | |
538 | dev->name); | |
539 | return -EINVAL; | |
540 | } | |
541 | ||
542 | return 0; | |
543 | } | |
544 | ||
545 | static int eth_pre_unbind(struct udevice *dev) | |
546 | { | |
547 | /* Don't hang onto a pointer that is going away */ | |
548 | if (dev == eth_get_uclass_priv()->current) | |
549 | eth_set_dev(NULL); | |
550 | ||
551 | return 0; | |
552 | } | |
553 | ||
554 | static int eth_post_probe(struct udevice *dev) | |
555 | { | |
556 | struct eth_device_priv *priv = dev->uclass_priv; | |
557 | struct eth_pdata *pdata = dev->platdata; | |
558 | unsigned char env_enetaddr[6]; | |
559 | ||
94067580 MS |
560 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
561 | struct eth_ops *ops = eth_get_ops(dev); | |
562 | static int reloc_done; | |
563 | ||
564 | if (!reloc_done) { | |
565 | if (ops->start) | |
566 | ops->start += gd->reloc_off; | |
567 | if (ops->send) | |
568 | ops->send += gd->reloc_off; | |
569 | if (ops->recv) | |
570 | ops->recv += gd->reloc_off; | |
571 | if (ops->free_pkt) | |
572 | ops->free_pkt += gd->reloc_off; | |
573 | if (ops->stop) | |
574 | ops->stop += gd->reloc_off; | |
575 | #ifdef CONFIG_MCAST_TFTP | |
576 | if (ops->mcast) | |
577 | ops->mcast += gd->reloc_off; | |
578 | #endif | |
579 | if (ops->write_hwaddr) | |
580 | ops->write_hwaddr += gd->reloc_off; | |
581 | if (ops->read_rom_hwaddr) | |
582 | ops->read_rom_hwaddr += gd->reloc_off; | |
583 | ||
584 | reloc_done++; | |
585 | } | |
586 | #endif | |
587 | ||
05c3e68f JH |
588 | priv->state = ETH_STATE_INIT; |
589 | ||
590 | /* Check if the device has a MAC address in ROM */ | |
591 | if (eth_get_ops(dev)->read_rom_hwaddr) | |
592 | eth_get_ops(dev)->read_rom_hwaddr(dev); | |
593 | ||
594 | eth_getenv_enetaddr_by_index("eth", dev->seq, env_enetaddr); | |
0adb5b76 JH |
595 | if (!is_zero_ethaddr(env_enetaddr)) { |
596 | if (!is_zero_ethaddr(pdata->enetaddr) && | |
05c3e68f JH |
597 | memcmp(pdata->enetaddr, env_enetaddr, 6)) { |
598 | printf("\nWarning: %s MAC addresses don't match:\n", | |
599 | dev->name); | |
600 | printf("Address in SROM is %pM\n", | |
601 | pdata->enetaddr); | |
602 | printf("Address in environment is %pM\n", | |
603 | env_enetaddr); | |
604 | } | |
605 | ||
606 | /* Override the ROM MAC address */ | |
607 | memcpy(pdata->enetaddr, env_enetaddr, 6); | |
0adb5b76 | 608 | } else if (is_valid_ethaddr(pdata->enetaddr)) { |
05c3e68f JH |
609 | eth_setenv_enetaddr_by_index("eth", dev->seq, pdata->enetaddr); |
610 | printf("\nWarning: %s using MAC address from ROM\n", | |
611 | dev->name); | |
0adb5b76 | 612 | } else if (is_zero_ethaddr(pdata->enetaddr)) { |
bef1014b JH |
613 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
614 | net_random_ethaddr(pdata->enetaddr); | |
615 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", | |
616 | dev->name, dev->seq, pdata->enetaddr); | |
617 | #else | |
05c3e68f JH |
618 | printf("\nError: %s address not set.\n", |
619 | dev->name); | |
620 | return -EINVAL; | |
bef1014b | 621 | #endif |
05c3e68f JH |
622 | } |
623 | ||
624 | return 0; | |
625 | } | |
626 | ||
627 | static int eth_pre_remove(struct udevice *dev) | |
628 | { | |
a16edabe BM |
629 | struct eth_pdata *pdata = dev->platdata; |
630 | ||
05c3e68f JH |
631 | eth_get_ops(dev)->stop(dev); |
632 | ||
a16edabe BM |
633 | /* clear the MAC address */ |
634 | memset(pdata->enetaddr, 0, 6); | |
635 | ||
05c3e68f JH |
636 | return 0; |
637 | } | |
638 | ||
639 | UCLASS_DRIVER(eth) = { | |
640 | .name = "eth", | |
641 | .id = UCLASS_ETH, | |
642 | .post_bind = eth_post_bind, | |
643 | .pre_unbind = eth_pre_unbind, | |
644 | .post_probe = eth_post_probe, | |
645 | .pre_remove = eth_pre_remove, | |
646 | .priv_auto_alloc_size = sizeof(struct eth_uclass_priv), | |
647 | .per_device_auto_alloc_size = sizeof(struct eth_device_priv), | |
e58780dc | 648 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
05c3e68f | 649 | }; |
eaa8a195 | 650 | #endif /* #ifdef CONFIG_DM_ETH */ |
05c3e68f JH |
651 | |
652 | #ifndef CONFIG_DM_ETH | |
dd35479a | 653 | |
f85b6071 | 654 | #ifdef CONFIG_API |
f85b6071 RJ |
655 | static struct { |
656 | uchar data[PKTSIZE]; | |
657 | int length; | |
658 | } eth_rcv_bufs[PKTBUFSRX]; | |
659 | ||
66c7385a | 660 | static unsigned int eth_rcv_current, eth_rcv_last; |
f85b6071 RJ |
661 | #endif |
662 | ||
f8be7d65 JH |
663 | static struct eth_device *eth_devices; |
664 | struct eth_device *eth_current; | |
c609719b | 665 | |
84eb1fba JH |
666 | static void eth_set_current_to_next(void) |
667 | { | |
668 | eth_current = eth_current->next; | |
669 | } | |
670 | ||
e58780dc JH |
671 | static void eth_set_dev(struct eth_device *dev) |
672 | { | |
673 | eth_current = dev; | |
674 | } | |
675 | ||
d7fb9bcf | 676 | struct eth_device *eth_get_dev_by_name(const char *devname) |
63ff004c MB |
677 | { |
678 | struct eth_device *dev, *target_dev; | |
679 | ||
7e7f903f HR |
680 | BUG_ON(devname == NULL); |
681 | ||
63ff004c MB |
682 | if (!eth_devices) |
683 | return NULL; | |
684 | ||
685 | dev = eth_devices; | |
686 | target_dev = NULL; | |
687 | do { | |
688 | if (strcmp(devname, dev->name) == 0) { | |
689 | target_dev = dev; | |
690 | break; | |
691 | } | |
692 | dev = dev->next; | |
693 | } while (dev != eth_devices); | |
694 | ||
695 | return target_dev; | |
696 | } | |
697 | ||
9e56986a AF |
698 | struct eth_device *eth_get_dev_by_index(int index) |
699 | { | |
700 | struct eth_device *dev, *target_dev; | |
9e56986a AF |
701 | |
702 | if (!eth_devices) | |
703 | return NULL; | |
704 | ||
705 | dev = eth_devices; | |
706 | target_dev = NULL; | |
707 | do { | |
fea7dcae | 708 | if (dev->index == index) { |
9e56986a AF |
709 | target_dev = dev; |
710 | break; | |
711 | } | |
712 | dev = dev->next; | |
9e56986a AF |
713 | } while (dev != eth_devices); |
714 | ||
715 | return target_dev; | |
716 | } | |
717 | ||
66c7385a | 718 | int eth_get_dev_index(void) |
c609719b | 719 | { |
66c7385a | 720 | if (!eth_current) |
fea7dcae | 721 | return -1; |
c609719b | 722 | |
fea7dcae | 723 | return eth_current->index; |
c609719b WD |
724 | } |
725 | ||
6e0d26c0 JH |
726 | static int on_ethaddr(const char *name, const char *value, enum env_op op, |
727 | int flags) | |
728 | { | |
729 | int index; | |
730 | struct eth_device *dev; | |
731 | ||
732 | if (!eth_devices) | |
733 | return 0; | |
734 | ||
735 | /* look for an index after "eth" */ | |
736 | index = simple_strtoul(name + 3, NULL, 10); | |
737 | ||
738 | dev = eth_devices; | |
739 | do { | |
740 | if (dev->index == index) { | |
741 | switch (op) { | |
742 | case env_op_create: | |
743 | case env_op_overwrite: | |
744 | eth_parse_enetaddr(value, dev->enetaddr); | |
745 | break; | |
746 | case env_op_delete: | |
747 | memset(dev->enetaddr, 0, 6); | |
748 | } | |
749 | } | |
7aba0f2c | 750 | dev = dev->next; |
6e0d26c0 JH |
751 | } while (dev != eth_devices); |
752 | ||
753 | return 0; | |
754 | } | |
755 | U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr); | |
756 | ||
7616e785 SG |
757 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
758 | int eth_number) | |
759 | { | |
760 | unsigned char env_enetaddr[6]; | |
761 | int ret = 0; | |
762 | ||
69376644 | 763 | eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
7616e785 | 764 | |
0adb5b76 JH |
765 | if (!is_zero_ethaddr(env_enetaddr)) { |
766 | if (!is_zero_ethaddr(dev->enetaddr) && | |
4c7c65af | 767 | memcmp(dev->enetaddr, env_enetaddr, 6)) { |
7616e785 | 768 | printf("\nWarning: %s MAC addresses don't match:\n", |
ff819a3a | 769 | dev->name); |
7616e785 | 770 | printf("Address in SROM is %pM\n", |
ff819a3a | 771 | dev->enetaddr); |
7616e785 | 772 | printf("Address in environment is %pM\n", |
ff819a3a | 773 | env_enetaddr); |
7616e785 SG |
774 | } |
775 | ||
776 | memcpy(dev->enetaddr, env_enetaddr, 6); | |
0adb5b76 | 777 | } else if (is_valid_ethaddr(dev->enetaddr)) { |
c88ef3c1 RH |
778 | eth_setenv_enetaddr_by_index(base_name, eth_number, |
779 | dev->enetaddr); | |
780 | printf("\nWarning: %s using MAC address from net device\n", | |
ff819a3a | 781 | dev->name); |
0adb5b76 | 782 | } else if (is_zero_ethaddr(dev->enetaddr)) { |
bef1014b JH |
783 | #ifdef CONFIG_NET_RANDOM_ETHADDR |
784 | net_random_ethaddr(dev->enetaddr); | |
785 | printf("\nWarning: %s (eth%d) using random MAC address - %pM\n", | |
786 | dev->name, eth_number, dev->enetaddr); | |
787 | #else | |
75d9a45c PM |
788 | printf("\nError: %s address not set.\n", |
789 | dev->name); | |
790 | return -EINVAL; | |
bef1014b | 791 | #endif |
7616e785 SG |
792 | } |
793 | ||
75d9a45c | 794 | if (dev->write_hwaddr && !eth_mac_skip(eth_number)) { |
0adb5b76 | 795 | if (!is_valid_ethaddr(dev->enetaddr)) { |
75d9a45c | 796 | printf("\nError: %s address %pM illegal value\n", |
ff819a3a | 797 | dev->name, dev->enetaddr); |
75d9a45c PM |
798 | return -EINVAL; |
799 | } | |
460f949f | 800 | |
7616e785 | 801 | ret = dev->write_hwaddr(dev); |
75d9a45c | 802 | if (ret) |
ff819a3a JH |
803 | printf("\nWarning: %s failed to set MAC address\n", |
804 | dev->name); | |
460f949f | 805 | } |
7616e785 SG |
806 | |
807 | return ret; | |
808 | } | |
809 | ||
89d48367 SG |
810 | int eth_register(struct eth_device *dev) |
811 | { | |
812 | struct eth_device *d; | |
66c7385a | 813 | static int index; |
58c583b6 | 814 | |
f6add132 | 815 | assert(strlen(dev->name) < sizeof(dev->name)); |
58c583b6 | 816 | |
89d48367 | 817 | if (!eth_devices) { |
ff819a3a JH |
818 | eth_devices = dev; |
819 | eth_current = dev; | |
89d48367 | 820 | eth_current_changed(); |
c609719b | 821 | } else { |
66c7385a | 822 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
aba4b69d | 823 | ; |
c609719b WD |
824 | d->next = dev; |
825 | } | |
826 | ||
827 | dev->state = ETH_STATE_INIT; | |
828 | dev->next = eth_devices; | |
fea7dcae | 829 | dev->index = index++; |
c609719b WD |
830 | |
831 | return 0; | |
832 | } | |
833 | ||
e7e982d6 VP |
834 | int eth_unregister(struct eth_device *dev) |
835 | { | |
836 | struct eth_device *cur; | |
837 | ||
838 | /* No device */ | |
839 | if (!eth_devices) | |
05324a48 | 840 | return -ENODEV; |
e7e982d6 VP |
841 | |
842 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; | |
843 | cur = cur->next) | |
844 | ; | |
845 | ||
846 | /* Device not found */ | |
847 | if (cur->next != dev) | |
05324a48 | 848 | return -ENODEV; |
e7e982d6 VP |
849 | |
850 | cur->next = dev->next; | |
851 | ||
852 | if (eth_devices == dev) | |
853 | eth_devices = dev->next == eth_devices ? NULL : dev->next; | |
854 | ||
855 | if (eth_current == dev) { | |
856 | eth_current = eth_devices; | |
857 | eth_current_changed(); | |
858 | } | |
859 | ||
860 | return 0; | |
861 | } | |
862 | ||
d2eaec60 | 863 | int eth_initialize(void) |
c609719b | 864 | { |
fea7dcae | 865 | int num_devices = 0; |
3bc42700 | 866 | |
c609719b WD |
867 | eth_devices = NULL; |
868 | eth_current = NULL; | |
3bc42700 | 869 | eth_common_init(); |
d9785c14 | 870 | |
c609719b | 871 | if (!eth_devices) { |
66c7385a | 872 | puts("No ethernet found.\n"); |
770605e4 | 873 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
c609719b WD |
874 | } else { |
875 | struct eth_device *dev = eth_devices; | |
66c7385a | 876 | char *ethprime = getenv("ethprime"); |
c609719b | 877 | |
770605e4 | 878 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
c609719b | 879 | do { |
fea7dcae | 880 | if (dev->index) |
66c7385a | 881 | puts(", "); |
c609719b WD |
882 | |
883 | printf("%s", dev->name); | |
884 | ||
66c7385a | 885 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
c609719b | 886 | eth_current = dev; |
66c7385a | 887 | puts(" [PRIME]"); |
c609719b WD |
888 | } |
889 | ||
1384f3bb | 890 | if (strchr(dev->name, ' ')) |
66c7385a JH |
891 | puts("\nWarning: eth device name has a space!" |
892 | "\n"); | |
1384f3bb | 893 | |
75d9a45c | 894 | eth_write_hwaddr(dev, "eth", dev->index); |
c609719b | 895 | |
c609719b | 896 | dev = dev->next; |
fea7dcae | 897 | num_devices++; |
66c7385a | 898 | } while (dev != eth_devices); |
c609719b | 899 | |
89d48367 | 900 | eth_current_changed(); |
66c7385a | 901 | putc('\n'); |
c609719b WD |
902 | } |
903 | ||
fea7dcae | 904 | return num_devices; |
c609719b WD |
905 | } |
906 | ||
53a5c424 DU |
907 | #ifdef CONFIG_MCAST_TFTP |
908 | /* Multicast. | |
909 | * mcast_addr: multicast ipaddr from which multicast Mac is made | |
85eb5caf | 910 | * join: 1=join, 0=leave. |
53a5c424 | 911 | */ |
049a95a7 | 912 | int eth_mcast_join(struct in_addr mcast_ip, int join) |
53a5c424 | 913 | { |
66c7385a | 914 | u8 mcast_mac[6]; |
85eb5caf | 915 | if (!eth_current || !eth_current->mcast) |
53a5c424 | 916 | return -1; |
049a95a7 JH |
917 | mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff; |
918 | mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff; | |
919 | mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f; | |
53a5c424 DU |
920 | mcast_mac[2] = 0x5e; |
921 | mcast_mac[1] = 0x0; | |
922 | mcast_mac[0] = 0x1; | |
923 | return eth_current->mcast(eth_current, mcast_mac, join); | |
924 | } | |
925 | ||
85eb5caf WD |
926 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
927 | * and this is the ethernet-crc method needed for TSEC -- and perhaps | |
53a5c424 DU |
928 | * some other adapter -- hash tables |
929 | */ | |
930 | #define CRCPOLY_LE 0xedb88320 | |
66c7385a | 931 | u32 ether_crc(size_t len, unsigned char const *p) |
53a5c424 DU |
932 | { |
933 | int i; | |
934 | u32 crc; | |
935 | crc = ~0; | |
936 | while (len--) { | |
937 | crc ^= *p++; | |
938 | for (i = 0; i < 8; i++) | |
939 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); | |
940 | } | |
941 | /* an reverse the bits, cuz of way they arrive -- last-first */ | |
942 | crc = (crc >> 16) | (crc << 16); | |
943 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); | |
944 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); | |
945 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); | |
946 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); | |
947 | return crc; | |
948 | } | |
949 | ||
950 | #endif | |
951 | ||
c609719b | 952 | |
d2eaec60 | 953 | int eth_init(void) |
c609719b | 954 | { |
6e0d26c0 | 955 | struct eth_device *old_current; |
c609719b | 956 | |
6bc11388 | 957 | if (!eth_current) { |
66c7385a | 958 | puts("No ethernet found.\n"); |
05324a48 | 959 | return -ENODEV; |
6bc11388 | 960 | } |
c609719b WD |
961 | |
962 | old_current = eth_current; | |
963 | do { | |
0ebf04c6 | 964 | debug("Trying %s\n", eth_current->name); |
c609719b | 965 | |
d2eaec60 | 966 | if (eth_current->init(eth_current, gd->bd) >= 0) { |
c609719b WD |
967 | eth_current->state = ETH_STATE_ACTIVE; |
968 | ||
505be87a | 969 | return 0; |
c609719b | 970 | } |
0ebf04c6 | 971 | debug("FAIL\n"); |
c609719b WD |
972 | |
973 | eth_try_another(0); | |
974 | } while (old_current != eth_current); | |
975 | ||
05324a48 | 976 | return -ETIMEDOUT; |
c609719b WD |
977 | } |
978 | ||
979 | void eth_halt(void) | |
980 | { | |
981 | if (!eth_current) | |
982 | return; | |
983 | ||
984 | eth_current->halt(eth_current); | |
985 | ||
986 | eth_current->state = ETH_STATE_PASSIVE; | |
987 | } | |
988 | ||
eaa8a195 BN |
989 | int eth_is_active(struct eth_device *dev) |
990 | { | |
991 | return dev && dev->state == ETH_STATE_ACTIVE; | |
992 | } | |
993 | ||
db288a96 | 994 | int eth_send(void *packet, int length) |
c609719b WD |
995 | { |
996 | if (!eth_current) | |
05324a48 | 997 | return -ENODEV; |
c609719b WD |
998 | |
999 | return eth_current->send(eth_current, packet, length); | |
1000 | } | |
1001 | ||
1002 | int eth_rx(void) | |
1003 | { | |
1004 | if (!eth_current) | |
05324a48 | 1005 | return -ENODEV; |
c609719b WD |
1006 | |
1007 | return eth_current->recv(eth_current); | |
1008 | } | |
05c3e68f | 1009 | #endif /* ifndef CONFIG_DM_ETH */ |
c609719b | 1010 | |
f85b6071 | 1011 | #ifdef CONFIG_API |
db288a96 | 1012 | static void eth_save_packet(void *packet, int length) |
f85b6071 | 1013 | { |
db288a96 | 1014 | char *p = packet; |
f85b6071 RJ |
1015 | int i; |
1016 | ||
1017 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) | |
1018 | return; | |
1019 | ||
1020 | if (PKTSIZE < length) | |
1021 | return; | |
1022 | ||
1023 | for (i = 0; i < length; i++) | |
1024 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; | |
1025 | ||
1026 | eth_rcv_bufs[eth_rcv_last].length = length; | |
1027 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; | |
1028 | } | |
1029 | ||
db288a96 | 1030 | int eth_receive(void *packet, int length) |
f85b6071 | 1031 | { |
db288a96 | 1032 | char *p = packet; |
f85b6071 RJ |
1033 | void *pp = push_packet; |
1034 | int i; | |
1035 | ||
1036 | if (eth_rcv_current == eth_rcv_last) { | |
1037 | push_packet = eth_save_packet; | |
1038 | eth_rx(); | |
1039 | push_packet = pp; | |
1040 | ||
1041 | if (eth_rcv_current == eth_rcv_last) | |
1042 | return -1; | |
1043 | } | |
1044 | ||
46c07bcf | 1045 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
f85b6071 RJ |
1046 | |
1047 | for (i = 0; i < length; i++) | |
1048 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; | |
1049 | ||
1050 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; | |
1051 | return length; | |
1052 | } | |
1053 | #endif /* CONFIG_API */ | |
1054 | ||
84eb1fba JH |
1055 | static void eth_current_changed(void) |
1056 | { | |
1057 | char *act = getenv("ethact"); | |
a671c4f2 BM |
1058 | char *ethrotate; |
1059 | ||
1060 | /* | |
1061 | * The call to eth_get_dev() below has a side effect of rotating | |
1062 | * ethernet device if uc_priv->current == NULL. This is not what | |
1063 | * we want when 'ethrotate' variable is 'no'. | |
1064 | */ | |
1065 | ethrotate = getenv("ethrotate"); | |
1066 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) | |
1067 | return; | |
1068 | ||
84eb1fba JH |
1069 | /* update current ethernet name */ |
1070 | if (eth_get_dev()) { | |
1071 | if (act == NULL || strcmp(act, eth_get_name()) != 0) | |
1072 | setenv("ethact", eth_get_name()); | |
1073 | } | |
1074 | /* | |
1075 | * remove the variable completely if there is no active | |
1076 | * interface | |
1077 | */ | |
1078 | else if (act != NULL) | |
1079 | setenv("ethact", NULL); | |
1080 | } | |
1081 | ||
c609719b WD |
1082 | void eth_try_another(int first_restart) |
1083 | { | |
05c3e68f | 1084 | static void *first_failed; |
c650e1be | 1085 | char *ethrotate; |
e1692577 MF |
1086 | |
1087 | /* | |
1088 | * Do not rotate between network interfaces when | |
1089 | * 'ethrotate' variable is set to 'no'. | |
1090 | */ | |
66c7385a JH |
1091 | ethrotate = getenv("ethrotate"); |
1092 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) | |
e1692577 | 1093 | return; |
c609719b | 1094 | |
84eb1fba | 1095 | if (!eth_get_dev()) |
c609719b WD |
1096 | return; |
1097 | ||
66c7385a | 1098 | if (first_restart) |
84eb1fba | 1099 | first_failed = eth_get_dev(); |
c609719b | 1100 | |
84eb1fba | 1101 | eth_set_current_to_next(); |
c609719b | 1102 | |
89d48367 | 1103 | eth_current_changed(); |
a3d991bd | 1104 | |
84eb1fba | 1105 | if (first_failed == eth_get_dev()) |
bc0571fc | 1106 | net_restart_wrap = 1; |
c609719b WD |
1107 | } |
1108 | ||
a3d991bd WD |
1109 | void eth_set_current(void) |
1110 | { | |
66c7385a JH |
1111 | static char *act; |
1112 | static int env_changed_id; | |
2f70c49e | 1113 | int env_id; |
a3d991bd | 1114 | |
2f70c49e HS |
1115 | env_id = get_env_id(); |
1116 | if ((act == NULL) || (env_changed_id != env_id)) { | |
1117 | act = getenv("ethact"); | |
1118 | env_changed_id = env_id; | |
1119 | } | |
6536b9bb JH |
1120 | |
1121 | if (act == NULL) { | |
1122 | char *ethprime = getenv("ethprime"); | |
1123 | void *dev = NULL; | |
1124 | ||
1125 | if (ethprime) | |
1126 | dev = eth_get_dev_by_name(ethprime); | |
1127 | if (dev) | |
1128 | eth_set_dev(dev); | |
1129 | else | |
1130 | eth_set_dev(NULL); | |
1131 | } else { | |
e58780dc | 1132 | eth_set_dev(eth_get_dev_by_name(act)); |
6536b9bb | 1133 | } |
a3d991bd | 1134 | |
89d48367 | 1135 | eth_current_changed(); |
a3d991bd | 1136 | } |
a3d991bd | 1137 | |
84eb1fba | 1138 | const char *eth_get_name(void) |
5bb226e8 | 1139 | { |
84eb1fba | 1140 | return eth_get_dev() ? eth_get_dev()->name : "unknown"; |
5bb226e8 | 1141 | } |