lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001-2010 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <command.h> |
| 26 | #include <net.h> |
| 27 | #include <miiphy.h> |
| 28 | #include <phy.h> |
| 29 | #include <asm/arch/gmac.h> |
| 30 | |
| 31 | |
| 32 | void eth_parse_enetaddr(const char *addr, uchar *enetaddr) |
| 33 | { |
| 34 | char *end; |
| 35 | int i; |
| 36 | |
| 37 | for (i = 0; i < 6; ++i) { |
| 38 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; |
| 39 | if (addr) |
| 40 | addr = (*end) ? end + 1 : end; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | int eth_getenv_enetaddr(char *name, uchar *enetaddr) |
| 45 | { |
| 46 | eth_parse_enetaddr(getenv(name), enetaddr); |
| 47 | return is_valid_ether_addr(enetaddr); |
| 48 | } |
| 49 | |
| 50 | int eth_setenv_enetaddr(char *name, const uchar *enetaddr) |
| 51 | { |
| 52 | char buf[20]; |
| 53 | |
| 54 | sprintf(buf, "%pM", enetaddr); |
| 55 | |
| 56 | return setenv(name, buf); |
| 57 | } |
| 58 | |
| 59 | int eth_getenv_enetaddr_by_index(const char *base_name, int index, |
| 60 | uchar *enetaddr) |
| 61 | { |
| 62 | char enetvar[32]; |
| 63 | if(index){ |
| 64 | sprintf(enetvar, "%s%daddr", base_name, index); |
| 65 | }else{ |
| 66 | sprintf(enetvar, "%saddr", base_name); |
| 67 | } |
| 68 | return eth_getenv_enetaddr(enetvar, enetaddr); |
| 69 | } |
| 70 | |
| 71 | static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, |
| 72 | uchar *enetaddr) |
| 73 | { |
| 74 | char enetvar[32]; |
| 75 | if(index){ |
| 76 | sprintf(enetvar, "%s%daddr", base_name, index); |
| 77 | }else{ |
| 78 | sprintf(enetvar, "%saddr", base_name); |
| 79 | } |
| 80 | return eth_setenv_enetaddr(enetvar, enetaddr); |
| 81 | } |
| 82 | |
| 83 | static int eth_mac_skip(int index) |
| 84 | { |
| 85 | char enetvar[32]; |
| 86 | char *skip_state; |
| 87 | if(index){ |
| 88 | sprintf(enetvar, "eth%dmacskip", index); |
| 89 | }else{ |
| 90 | sprintf(enetvar, "ethmacskip"); |
| 91 | } |
| 92 | return ((skip_state = getenv(enetvar)) != NULL); |
| 93 | } |
| 94 | |
| 95 | #ifdef CONFIG_RANDOM_MACADDR |
| 96 | void eth_random_enetaddr(uchar *enetaddr) |
| 97 | { |
| 98 | uint32_t rval; |
| 99 | |
| 100 | srand(get_timer(0)); |
| 101 | |
| 102 | rval = rand(); |
| 103 | enetaddr[0] = rval & 0xff; |
| 104 | enetaddr[1] = (rval >> 8) & 0xff; |
| 105 | enetaddr[2] = (rval >> 16) & 0xff; |
| 106 | |
| 107 | rval = rand(); |
| 108 | enetaddr[3] = rval & 0xff; |
| 109 | enetaddr[4] = (rval >> 8) & 0xff; |
| 110 | enetaddr[5] = (rval >> 16) & 0xff; |
| 111 | |
| 112 | /* make sure it's local and unicast */ |
| 113 | enetaddr[0] = (enetaddr[0] | 0x02) & ~0x01; |
| 114 | } |
| 115 | #endif |
| 116 | |
| 117 | /* |
| 118 | * CPU and board-specific Ethernet initializations. Aliased function |
| 119 | * signals caller to move on |
| 120 | */ |
| 121 | static int __def_eth_init(bd_t *bis) |
| 122 | { |
| 123 | return -1; |
| 124 | } |
| 125 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 126 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 127 | |
| 128 | #ifdef CONFIG_API |
| 129 | static struct { |
| 130 | uchar data[PKTSIZE]; |
| 131 | int length; |
| 132 | } eth_rcv_bufs[PKTBUFSRX]; |
| 133 | |
| 134 | static unsigned int eth_rcv_current, eth_rcv_last; |
| 135 | #endif |
| 136 | |
| 137 | static struct eth_device *eth_devices; |
| 138 | struct eth_device *eth_current; |
| 139 | |
| 140 | struct eth_device *eth_get_dev_by_name(const char *devname) |
| 141 | { |
| 142 | struct eth_device *dev, *target_dev; |
| 143 | |
| 144 | BUG_ON(devname == NULL); |
| 145 | |
| 146 | if (!eth_devices) |
| 147 | return NULL; |
| 148 | |
| 149 | dev = eth_devices; |
| 150 | target_dev = NULL; |
| 151 | do { |
| 152 | if (strcmp(devname, dev->name) == 0) { |
| 153 | target_dev = dev; |
| 154 | break; |
| 155 | } |
| 156 | dev = dev->next; |
| 157 | } while (dev != eth_devices); |
| 158 | |
| 159 | return target_dev; |
| 160 | } |
| 161 | |
| 162 | struct eth_device *eth_get_dev_by_index(int index) |
| 163 | { |
| 164 | struct eth_device *dev, *target_dev; |
| 165 | |
| 166 | if (!eth_devices) |
| 167 | return NULL; |
| 168 | |
| 169 | dev = eth_devices; |
| 170 | target_dev = NULL; |
| 171 | do { |
| 172 | if (dev->index == index) { |
| 173 | target_dev = dev; |
| 174 | break; |
| 175 | } |
| 176 | dev = dev->next; |
| 177 | } while (dev != eth_devices); |
| 178 | |
| 179 | return target_dev; |
| 180 | } |
| 181 | |
| 182 | int eth_get_dev_index(void) |
| 183 | { |
| 184 | if (!eth_current) |
| 185 | return -1; |
| 186 | |
| 187 | return eth_current->index; |
| 188 | } |
| 189 | |
| 190 | static void eth_current_changed(void) |
| 191 | { |
| 192 | char *act = getenv("ethact"); |
| 193 | /* update current ethernet name */ |
| 194 | if (eth_current) { |
| 195 | if (act == NULL || strcmp(act, eth_current->name) != 0) |
| 196 | setenv("ethact", eth_current->name); |
| 197 | } |
| 198 | /* |
| 199 | * remove the variable completely if there is no active |
| 200 | * interface |
| 201 | */ |
| 202 | else if (act != NULL) |
| 203 | setenv("ethact", NULL); |
| 204 | } |
| 205 | |
| 206 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
| 207 | int eth_number) |
| 208 | { |
| 209 | unsigned char env_enetaddr[6]; |
| 210 | int ret = 0; |
| 211 | |
| 212 | eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
| 213 | |
| 214 | if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { |
| 215 | if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && |
| 216 | memcmp(dev->enetaddr, env_enetaddr, 6)) { |
| 217 | printf("\nWarning: %s MAC addresses don't match:\n", |
| 218 | dev->name); |
| 219 | printf("Address in SROM is %pM\n", |
| 220 | dev->enetaddr); |
| 221 | printf("Address in environment is %pM\n", |
| 222 | env_enetaddr); |
| 223 | } |
| 224 | |
| 225 | memcpy(dev->enetaddr, env_enetaddr, 6); |
| 226 | } else if (is_valid_ether_addr(dev->enetaddr)) { |
| 227 | eth_setenv_enetaddr_by_index(base_name, eth_number, |
| 228 | dev->enetaddr); |
| 229 | printf("\nWarning: %s using MAC address from net device\n", |
| 230 | dev->name); |
| 231 | } |
| 232 | |
| 233 | if (dev->write_hwaddr && |
| 234 | !eth_mac_skip(eth_number)) { |
| 235 | if (!is_valid_ether_addr(dev->enetaddr)) |
| 236 | return -1; |
| 237 | |
| 238 | ret = dev->write_hwaddr(dev); |
| 239 | } |
| 240 | |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | int eth_register(struct eth_device *dev) |
| 245 | { |
| 246 | struct eth_device *d; |
| 247 | static int index; |
| 248 | |
| 249 | assert(strlen(dev->name) < sizeof(dev->name)); |
| 250 | |
| 251 | if (!eth_devices) { |
| 252 | eth_current = eth_devices = dev; |
| 253 | eth_current_changed(); |
| 254 | } else { |
| 255 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
| 256 | ; |
| 257 | d->next = dev; |
| 258 | } |
| 259 | |
| 260 | dev->state = ETH_STATE_INIT; |
| 261 | dev->next = eth_devices; |
| 262 | dev->index = index++; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | int eth_unregister(struct eth_device *dev) |
| 268 | { |
| 269 | struct eth_device *cur; |
| 270 | |
| 271 | /* No device */ |
| 272 | if (!eth_devices) |
| 273 | return -1; |
| 274 | |
| 275 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; |
| 276 | cur = cur->next) |
| 277 | ; |
| 278 | |
| 279 | /* Device not found */ |
| 280 | if (cur->next != dev) |
| 281 | return -1; |
| 282 | |
| 283 | cur->next = dev->next; |
| 284 | |
| 285 | if (eth_devices == dev) |
| 286 | eth_devices = dev->next == eth_devices ? NULL : dev->next; |
| 287 | |
| 288 | if (eth_current == dev) { |
| 289 | eth_current = eth_devices; |
| 290 | eth_current_changed(); |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | static void eth_env_init(bd_t *bis) |
| 297 | { |
| 298 | const char *s; |
| 299 | |
| 300 | if ((s = getenv("bootfile")) != NULL) |
| 301 | copy_filename(BootFile, s, sizeof(BootFile)); |
| 302 | } |
| 303 | |
| 304 | int eth_initialize(bd_t *bis) |
| 305 | { |
| 306 | int num_devices = 0; |
| 307 | eth_devices = NULL; |
| 308 | eth_current = NULL; |
| 309 | |
| 310 | //bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); |
| 311 | printf("bootstage net eth start\n"); |
| 312 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
| 313 | //miiphy_init(); |
| 314 | #endif |
| 315 | |
| 316 | #ifdef CONFIG_PHYLIB |
| 317 | //phy_init(); |
| 318 | #endif |
| 319 | |
| 320 | gmac_phy_init(); |
| 321 | eth_env_init(bis); |
| 322 | |
| 323 | /* |
| 324 | * If board-specific initialization exists, call it. |
| 325 | * If not, call a CPU-specific one |
| 326 | */ |
| 327 | if (board_eth_init != __def_eth_init) { |
| 328 | if (board_eth_init(bis) < 0) |
| 329 | printf("Board Net Initialization Failed\n"); |
| 330 | } else if (cpu_eth_init != __def_eth_init) { |
| 331 | if (cpu_eth_init(bis) < 0) |
| 332 | printf("CPU Net Initialization Failed\n"); |
| 333 | } else |
| 334 | printf("Net Initialization Skipped\n"); |
| 335 | |
| 336 | if (!eth_devices) { |
| 337 | puts("No ethernet found.\n"); |
| 338 | //bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
| 339 | } else { |
| 340 | struct eth_device *dev = eth_devices; |
| 341 | char *ethprime = getenv("ethprime"); |
| 342 | |
| 343 | //bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
| 344 | puts("bootstage net eth init\n"); |
| 345 | do { |
| 346 | if (dev->index) |
| 347 | puts(", "); |
| 348 | |
| 349 | printf("%s", dev->name); |
| 350 | |
| 351 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
| 352 | eth_current = dev; |
| 353 | puts(" [PRIME]"); |
| 354 | } |
| 355 | |
| 356 | if (strchr(dev->name, ' ')) |
| 357 | puts("\nWarning: eth device name has a space!" |
| 358 | "\n"); |
| 359 | |
| 360 | if (eth_write_hwaddr(dev, "eth", dev->index)) |
| 361 | puts("\nWarning: failed to set MAC address\n"); |
| 362 | |
| 363 | dev = dev->next; |
| 364 | num_devices++; |
| 365 | } while (dev != eth_devices); |
| 366 | |
| 367 | eth_current_changed(); |
| 368 | putc('\n'); |
| 369 | } |
| 370 | |
| 371 | return num_devices; |
| 372 | } |
| 373 | |
| 374 | #ifdef CONFIG_MCAST_TFTP |
| 375 | /* Multicast. |
| 376 | * mcast_addr: multicast ipaddr from which multicast Mac is made |
| 377 | * join: 1=join, 0=leave. |
| 378 | */ |
| 379 | int eth_mcast_join(IPaddr_t mcast_ip, u8 join) |
| 380 | { |
| 381 | u8 mcast_mac[6]; |
| 382 | if (!eth_current || !eth_current->mcast) |
| 383 | return -1; |
| 384 | mcast_mac[5] = htonl(mcast_ip) & 0xff; |
| 385 | mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff; |
| 386 | mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f; |
| 387 | mcast_mac[2] = 0x5e; |
| 388 | mcast_mac[1] = 0x0; |
| 389 | mcast_mac[0] = 0x1; |
| 390 | return eth_current->mcast(eth_current, mcast_mac, join); |
| 391 | } |
| 392 | |
| 393 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
| 394 | * and this is the ethernet-crc method needed for TSEC -- and perhaps |
| 395 | * some other adapter -- hash tables |
| 396 | */ |
| 397 | #define CRCPOLY_LE 0xedb88320 |
| 398 | u32 ether_crc(size_t len, unsigned char const *p) |
| 399 | { |
| 400 | int i; |
| 401 | u32 crc; |
| 402 | crc = ~0; |
| 403 | while (len--) { |
| 404 | crc ^= *p++; |
| 405 | for (i = 0; i < 8; i++) |
| 406 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); |
| 407 | } |
| 408 | /* an reverse the bits, cuz of way they arrive -- last-first */ |
| 409 | crc = (crc >> 16) | (crc << 16); |
| 410 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); |
| 411 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); |
| 412 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); |
| 413 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); |
| 414 | return crc; |
| 415 | } |
| 416 | |
| 417 | #endif |
| 418 | |
| 419 | |
| 420 | int eth_init(bd_t *bis) |
| 421 | { |
| 422 | struct eth_device *old_current, *dev; |
| 423 | |
| 424 | if (!eth_current) { |
| 425 | puts("No ethernet found.\n"); |
| 426 | return -1; |
| 427 | } |
| 428 | |
| 429 | /* Sync environment with network devices */ |
| 430 | dev = eth_devices; |
| 431 | do { |
| 432 | uchar env_enetaddr[6]; |
| 433 | |
| 434 | if (eth_getenv_enetaddr_by_index("eth", dev->index, |
| 435 | env_enetaddr)) |
| 436 | memcpy(dev->enetaddr, env_enetaddr, 6); |
| 437 | |
| 438 | dev = dev->next; |
| 439 | } while (dev != eth_devices); |
| 440 | |
| 441 | old_current = eth_current; |
| 442 | do { |
| 443 | debug("Trying %s\n", eth_current->name); |
| 444 | |
| 445 | if (eth_current->init(eth_current, bis) >= 0) { |
| 446 | eth_current->state = ETH_STATE_ACTIVE; |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | debug("FAIL\n"); |
| 451 | |
| 452 | eth_try_another(0); |
| 453 | } while (old_current != eth_current); |
| 454 | |
| 455 | return -1; |
| 456 | } |
| 457 | |
| 458 | void eth_halt(void) |
| 459 | { |
| 460 | if (!eth_current) |
| 461 | return; |
| 462 | |
| 463 | eth_current->halt(eth_current); |
| 464 | |
| 465 | eth_current->state = ETH_STATE_PASSIVE; |
| 466 | } |
| 467 | |
| 468 | int eth_send(void *packet, int length) |
| 469 | { |
| 470 | if (!eth_current) |
| 471 | return -1; |
| 472 | |
| 473 | return eth_current->send(eth_current, packet, length); |
| 474 | } |
| 475 | |
| 476 | int eth_rx(void) |
| 477 | { |
| 478 | if (!eth_current) |
| 479 | return -1; |
| 480 | |
| 481 | return eth_current->recv(eth_current); |
| 482 | } |
| 483 | |
| 484 | #ifdef CONFIG_API |
| 485 | static void eth_save_packet(void *packet, int length) |
| 486 | { |
| 487 | char *p = packet; |
| 488 | int i; |
| 489 | |
| 490 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) |
| 491 | return; |
| 492 | |
| 493 | if (PKTSIZE < length) |
| 494 | return; |
| 495 | |
| 496 | for (i = 0; i < length; i++) |
| 497 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; |
| 498 | |
| 499 | eth_rcv_bufs[eth_rcv_last].length = length; |
| 500 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; |
| 501 | } |
| 502 | |
| 503 | int eth_receive(void *packet, int length) |
| 504 | { |
| 505 | char *p = packet; |
| 506 | void *pp = push_packet; |
| 507 | int i; |
| 508 | |
| 509 | if (eth_rcv_current == eth_rcv_last) { |
| 510 | push_packet = eth_save_packet; |
| 511 | eth_rx(); |
| 512 | push_packet = pp; |
| 513 | |
| 514 | if (eth_rcv_current == eth_rcv_last) |
| 515 | return -1; |
| 516 | } |
| 517 | |
| 518 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
| 519 | |
| 520 | for (i = 0; i < length; i++) |
| 521 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; |
| 522 | |
| 523 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; |
| 524 | return length; |
| 525 | } |
| 526 | #endif /* CONFIG_API */ |
| 527 | |
| 528 | void eth_try_another(int first_restart) |
| 529 | { |
| 530 | static struct eth_device *first_failed; |
| 531 | char *ethrotate; |
| 532 | |
| 533 | /* |
| 534 | * Do not rotate between network interfaces when |
| 535 | * 'ethrotate' variable is set to 'no'. |
| 536 | */ |
| 537 | ethrotate = getenv("ethrotate"); |
| 538 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
| 539 | return; |
| 540 | |
| 541 | if (!eth_current) |
| 542 | return; |
| 543 | |
| 544 | if (first_restart) |
| 545 | first_failed = eth_current; |
| 546 | |
| 547 | eth_current = eth_current->next; |
| 548 | |
| 549 | eth_current_changed(); |
| 550 | |
| 551 | if (first_failed == eth_current) |
| 552 | NetRestartWrap = 1; |
| 553 | } |
| 554 | |
| 555 | void eth_set_current(void) |
| 556 | { |
| 557 | static char *act; |
| 558 | static int env_changed_id; |
| 559 | struct eth_device *old_current; |
| 560 | int env_id; |
| 561 | |
| 562 | if (!eth_current) /* XXX no current */ |
| 563 | return; |
| 564 | |
| 565 | env_id = get_env_id(); |
| 566 | if ((act == NULL) || (env_changed_id != env_id)) { |
| 567 | act = getenv("ethact"); |
| 568 | env_changed_id = env_id; |
| 569 | } |
| 570 | if (act != NULL) { |
| 571 | old_current = eth_current; |
| 572 | do { |
| 573 | if (strcmp(eth_current->name, act) == 0) |
| 574 | return; |
| 575 | eth_current = eth_current->next; |
| 576 | } while (old_current != eth_current); |
| 577 | } |
| 578 | |
| 579 | eth_current_changed(); |
| 580 | } |
| 581 | |
| 582 | char *eth_get_name(void) |
| 583 | { |
| 584 | return eth_current ? eth_current->name : "unknown"; |
| 585 | } |