eth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. /*
  2. * (C) Copyright 2001-2004
  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. #include <common.h>
  24. #include <command.h>
  25. #include <net.h>
  26. #include <miiphy.h>
  27. #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI)
  28. #ifdef CFG_GT_6426x
  29. extern int gt6426x_eth_initialize(bd_t *bis);
  30. #endif
  31. extern int au1x00_enet_initialize(bd_t*);
  32. extern int dc21x4x_initialize(bd_t*);
  33. extern int e1000_initialize(bd_t*);
  34. extern int eepro100_initialize(bd_t*);
  35. extern int eth_3com_initialize(bd_t*);
  36. extern int fec_initialize(bd_t*);
  37. extern int inca_switch_initialize(bd_t*);
  38. extern int mpc5xxx_fec_initialize(bd_t*);
  39. extern int mpc512x_fec_initialize(bd_t*);
  40. extern int mpc8220_fec_initialize(bd_t*);
  41. extern int mv6436x_eth_initialize(bd_t *);
  42. extern int mv6446x_eth_initialize(bd_t *);
  43. extern int natsemi_initialize(bd_t*);
  44. extern int ns8382x_initialize(bd_t*);
  45. extern int pcnet_initialize(bd_t*);
  46. extern int plb2800_eth_initialize(bd_t*);
  47. extern int ppc_4xx_eth_initialize(bd_t *);
  48. extern int rtl8139_initialize(bd_t*);
  49. extern int rtl8169_initialize(bd_t*);
  50. extern int scc_initialize(bd_t*);
  51. extern int skge_initialize(bd_t*);
  52. extern int tsi108_eth_initialize(bd_t*);
  53. extern int uli526x_initialize(bd_t *);
  54. extern int tsec_initialize(bd_t*, int, char *);
  55. extern int npe_initialize(bd_t *);
  56. extern int uec_initialize(int);
  57. extern int bfin_EMAC_initialize(bd_t *);
  58. extern int atstk1000_eth_initialize(bd_t *);
  59. extern int atngw100_eth_initialize(bd_t *);
  60. extern int mcffec_initialize(bd_t*);
  61. extern int mcdmafec_initialize(bd_t*);
  62. extern int at91cap9_eth_initialize(bd_t *);
  63. #ifdef CONFIG_API
  64. extern void (*push_packet)(volatile void *, int);
  65. static struct {
  66. uchar data[PKTSIZE];
  67. int length;
  68. } eth_rcv_bufs[PKTBUFSRX];
  69. static unsigned int eth_rcv_current = 0, eth_rcv_last = 0;
  70. #endif
  71. static struct eth_device *eth_devices, *eth_current;
  72. struct eth_device *eth_get_dev(void)
  73. {
  74. return eth_current;
  75. }
  76. struct eth_device *eth_get_dev_by_name(char *devname)
  77. {
  78. struct eth_device *dev, *target_dev;
  79. if (!eth_devices)
  80. return NULL;
  81. dev = eth_devices;
  82. target_dev = NULL;
  83. do {
  84. if (strcmp(devname, dev->name) == 0) {
  85. target_dev = dev;
  86. break;
  87. }
  88. dev = dev->next;
  89. } while (dev != eth_devices);
  90. return target_dev;
  91. }
  92. int eth_get_dev_index (void)
  93. {
  94. struct eth_device *dev;
  95. int num = 0;
  96. if (!eth_devices) {
  97. return (-1);
  98. }
  99. for (dev = eth_devices; dev; dev = dev->next) {
  100. if (dev == eth_current)
  101. break;
  102. ++num;
  103. }
  104. if (dev) {
  105. return (num);
  106. }
  107. return (0);
  108. }
  109. int eth_register(struct eth_device* dev)
  110. {
  111. struct eth_device *d;
  112. if (!eth_devices) {
  113. eth_current = eth_devices = dev;
  114. #ifdef CONFIG_NET_MULTI
  115. /* update current ethernet name */
  116. {
  117. char *act = getenv("ethact");
  118. if (act == NULL || strcmp(act, eth_current->name) != 0)
  119. setenv("ethact", eth_current->name);
  120. }
  121. #endif
  122. } else {
  123. for (d=eth_devices; d->next!=eth_devices; d=d->next);
  124. d->next = dev;
  125. }
  126. dev->state = ETH_STATE_INIT;
  127. dev->next = eth_devices;
  128. return 0;
  129. }
  130. int eth_initialize(bd_t *bis)
  131. {
  132. char enetvar[32];
  133. unsigned char env_enetaddr[6];
  134. int i, eth_number = 0;
  135. char *tmp, *end;
  136. eth_devices = NULL;
  137. eth_current = NULL;
  138. show_boot_progress (64);
  139. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  140. miiphy_init();
  141. #endif
  142. #if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750)
  143. mv6436x_eth_initialize(bis);
  144. #endif
  145. #if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx)
  146. mv6446x_eth_initialize(bis);
  147. #endif
  148. #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000)
  149. ppc_4xx_eth_initialize(bis);
  150. #endif
  151. #ifdef CONFIG_INCA_IP_SWITCH
  152. inca_switch_initialize(bis);
  153. #endif
  154. #ifdef CONFIG_PLB2800_ETHER
  155. plb2800_eth_initialize(bis);
  156. #endif
  157. #ifdef SCC_ENET
  158. scc_initialize(bis);
  159. #endif
  160. #if defined(CONFIG_MPC5xxx_FEC)
  161. mpc5xxx_fec_initialize(bis);
  162. #endif
  163. #if defined(CONFIG_MPC512x_FEC)
  164. mpc512x_fec_initialize (bis);
  165. #endif
  166. #if defined(CONFIG_MPC8220_FEC)
  167. mpc8220_fec_initialize(bis);
  168. #endif
  169. #if defined(CONFIG_SK98)
  170. skge_initialize(bis);
  171. #endif
  172. #if defined(CONFIG_TSEC1)
  173. tsec_initialize(bis, 0, CONFIG_TSEC1_NAME);
  174. #endif
  175. #if defined(CONFIG_TSEC2)
  176. tsec_initialize(bis, 1, CONFIG_TSEC2_NAME);
  177. #endif
  178. #if defined(CONFIG_MPC85XX_FEC)
  179. tsec_initialize(bis, 2, CONFIG_MPC85XX_FEC_NAME);
  180. #else
  181. # if defined(CONFIG_TSEC3)
  182. tsec_initialize(bis, 2, CONFIG_TSEC3_NAME);
  183. # endif
  184. # if defined(CONFIG_TSEC4)
  185. tsec_initialize(bis, 3, CONFIG_TSEC4_NAME);
  186. # endif
  187. #endif
  188. #if defined(CONFIG_UEC_ETH1)
  189. uec_initialize(0);
  190. #endif
  191. #if defined(CONFIG_UEC_ETH2)
  192. uec_initialize(1);
  193. #endif
  194. #if defined(CONFIG_UEC_ETH3)
  195. uec_initialize(2);
  196. #endif
  197. #if defined(CONFIG_UEC_ETH4)
  198. uec_initialize(3);
  199. #endif
  200. #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
  201. fec_initialize(bis);
  202. #endif
  203. #if defined(CONFIG_AU1X00)
  204. au1x00_enet_initialize(bis);
  205. #endif
  206. #if defined(CONFIG_IXP4XX_NPE)
  207. npe_initialize(bis);
  208. #endif
  209. #ifdef CONFIG_E1000
  210. e1000_initialize(bis);
  211. #endif
  212. #ifdef CONFIG_EEPRO100
  213. eepro100_initialize(bis);
  214. #endif
  215. #ifdef CONFIG_TULIP
  216. dc21x4x_initialize(bis);
  217. #endif
  218. #ifdef CONFIG_3COM
  219. eth_3com_initialize(bis);
  220. #endif
  221. #ifdef CONFIG_PCNET
  222. pcnet_initialize(bis);
  223. #endif
  224. #ifdef CFG_GT_6426x
  225. gt6426x_eth_initialize(bis);
  226. #endif
  227. #ifdef CONFIG_NATSEMI
  228. natsemi_initialize(bis);
  229. #endif
  230. #ifdef CONFIG_NS8382X
  231. ns8382x_initialize(bis);
  232. #endif
  233. #if defined(CONFIG_TSI108_ETH)
  234. tsi108_eth_initialize(bis);
  235. #endif
  236. #if defined(CONFIG_ULI526X)
  237. uli526x_initialize(bis);
  238. #endif
  239. #if defined(CONFIG_RTL8139)
  240. rtl8139_initialize(bis);
  241. #endif
  242. #if defined(CONFIG_RTL8169)
  243. rtl8169_initialize(bis);
  244. #endif
  245. #if defined(CONFIG_BF537)
  246. bfin_EMAC_initialize(bis);
  247. #endif
  248. #if defined(CONFIG_ATSTK1000)
  249. atstk1000_eth_initialize(bis);
  250. #endif
  251. #if defined(CONFIG_ATNGW100)
  252. atngw100_eth_initialize(bis);
  253. #endif
  254. #if defined(CONFIG_MCFFEC)
  255. mcffec_initialize(bis);
  256. #endif
  257. #if defined(CONFIG_FSLDMAFEC)
  258. mcdmafec_initialize(bis);
  259. #endif
  260. #if defined(CONFIG_AT91CAP9)
  261. at91cap9_eth_initialize(bis);
  262. #endif
  263. if (!eth_devices) {
  264. puts ("No ethernet found.\n");
  265. show_boot_progress (-64);
  266. } else {
  267. struct eth_device *dev = eth_devices;
  268. char *ethprime = getenv ("ethprime");
  269. show_boot_progress (65);
  270. do {
  271. if (eth_number)
  272. puts (", ");
  273. printf("%s", dev->name);
  274. if (ethprime && strcmp (dev->name, ethprime) == 0) {
  275. eth_current = dev;
  276. puts (" [PRIME]");
  277. }
  278. sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
  279. tmp = getenv (enetvar);
  280. for (i=0; i<6; i++) {
  281. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  282. if (tmp)
  283. tmp = (*end) ? end+1 : end;
  284. }
  285. if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
  286. if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
  287. memcmp(dev->enetaddr, env_enetaddr, 6))
  288. {
  289. printf ("\nWarning: %s MAC addresses don't match:\n",
  290. dev->name);
  291. printf ("Address in SROM is "
  292. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  293. dev->enetaddr[0], dev->enetaddr[1],
  294. dev->enetaddr[2], dev->enetaddr[3],
  295. dev->enetaddr[4], dev->enetaddr[5]);
  296. printf ("Address in environment is "
  297. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  298. env_enetaddr[0], env_enetaddr[1],
  299. env_enetaddr[2], env_enetaddr[3],
  300. env_enetaddr[4], env_enetaddr[5]);
  301. }
  302. memcpy(dev->enetaddr, env_enetaddr, 6);
  303. }
  304. eth_number++;
  305. dev = dev->next;
  306. } while(dev != eth_devices);
  307. #ifdef CONFIG_NET_MULTI
  308. /* update current ethernet name */
  309. if (eth_current) {
  310. char *act = getenv("ethact");
  311. if (act == NULL || strcmp(act, eth_current->name) != 0)
  312. setenv("ethact", eth_current->name);
  313. } else
  314. setenv("ethact", NULL);
  315. #endif
  316. putc ('\n');
  317. }
  318. return eth_number;
  319. }
  320. void eth_set_enetaddr(int num, char *addr) {
  321. struct eth_device *dev;
  322. unsigned char enetaddr[6];
  323. char *end;
  324. int i;
  325. debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
  326. if (!eth_devices)
  327. return;
  328. for (i=0; i<6; i++) {
  329. enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
  330. if (addr)
  331. addr = (*end) ? end+1 : end;
  332. }
  333. dev = eth_devices;
  334. while(num-- > 0) {
  335. dev = dev->next;
  336. if (dev == eth_devices)
  337. return;
  338. }
  339. debug ( "Setting new HW address on %s\n"
  340. "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n",
  341. dev->name,
  342. enetaddr[0], enetaddr[1],
  343. enetaddr[2], enetaddr[3],
  344. enetaddr[4], enetaddr[5]);
  345. memcpy(dev->enetaddr, enetaddr, 6);
  346. }
  347. #ifdef CONFIG_MCAST_TFTP
  348. /* Multicast.
  349. * mcast_addr: multicast ipaddr from which multicast Mac is made
  350. * join: 1=join, 0=leave.
  351. */
  352. int eth_mcast_join( IPaddr_t mcast_ip, u8 join)
  353. {
  354. u8 mcast_mac[6];
  355. if (!eth_current || !eth_current->mcast)
  356. return -1;
  357. mcast_mac[5] = htonl(mcast_ip) & 0xff;
  358. mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff;
  359. mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f;
  360. mcast_mac[2] = 0x5e;
  361. mcast_mac[1] = 0x0;
  362. mcast_mac[0] = 0x1;
  363. return eth_current->mcast(eth_current, mcast_mac, join);
  364. }
  365. /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c
  366. * and this is the ethernet-crc method needed for TSEC -- and perhaps
  367. * some other adapter -- hash tables
  368. */
  369. #define CRCPOLY_LE 0xedb88320
  370. u32 ether_crc (size_t len, unsigned char const *p)
  371. {
  372. int i;
  373. u32 crc;
  374. crc = ~0;
  375. while (len--) {
  376. crc ^= *p++;
  377. for (i = 0; i < 8; i++)
  378. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  379. }
  380. /* an reverse the bits, cuz of way they arrive -- last-first */
  381. crc = (crc >> 16) | (crc << 16);
  382. crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00);
  383. crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0);
  384. crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc);
  385. crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa);
  386. return crc;
  387. }
  388. #endif
  389. int eth_init(bd_t *bis)
  390. {
  391. struct eth_device* old_current;
  392. if (!eth_current) {
  393. puts ("No ethernet found.\n");
  394. return -1;
  395. }
  396. old_current = eth_current;
  397. do {
  398. debug ("Trying %s\n", eth_current->name);
  399. if (eth_current->init(eth_current,bis) >= 0) {
  400. eth_current->state = ETH_STATE_ACTIVE;
  401. return 0;
  402. }
  403. debug ("FAIL\n");
  404. eth_try_another(0);
  405. } while (old_current != eth_current);
  406. return -1;
  407. }
  408. void eth_halt(void)
  409. {
  410. if (!eth_current)
  411. return;
  412. eth_current->halt(eth_current);
  413. eth_current->state = ETH_STATE_PASSIVE;
  414. }
  415. int eth_send(volatile void *packet, int length)
  416. {
  417. if (!eth_current)
  418. return -1;
  419. return eth_current->send(eth_current, packet, length);
  420. }
  421. int eth_rx(void)
  422. {
  423. if (!eth_current)
  424. return -1;
  425. return eth_current->recv(eth_current);
  426. }
  427. #ifdef CONFIG_API
  428. static void eth_save_packet(volatile void *packet, int length)
  429. {
  430. volatile char *p = packet;
  431. int i;
  432. if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
  433. return;
  434. if (PKTSIZE < length)
  435. return;
  436. for (i = 0; i < length; i++)
  437. eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
  438. eth_rcv_bufs[eth_rcv_last].length = length;
  439. eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
  440. }
  441. int eth_receive(volatile void *packet, int length)
  442. {
  443. volatile char *p = packet;
  444. void *pp = push_packet;
  445. int i;
  446. if (eth_rcv_current == eth_rcv_last) {
  447. push_packet = eth_save_packet;
  448. eth_rx();
  449. push_packet = pp;
  450. if (eth_rcv_current == eth_rcv_last)
  451. return -1;
  452. }
  453. if (length < eth_rcv_bufs[eth_rcv_current].length)
  454. return -1;
  455. length = eth_rcv_bufs[eth_rcv_current].length;
  456. for (i = 0; i < length; i++)
  457. p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
  458. eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
  459. return length;
  460. }
  461. #endif /* CONFIG_API */
  462. void eth_try_another(int first_restart)
  463. {
  464. static struct eth_device *first_failed = NULL;
  465. char *ethrotate;
  466. /*
  467. * Do not rotate between network interfaces when
  468. * 'ethrotate' variable is set to 'no'.
  469. */
  470. if (((ethrotate = getenv ("ethrotate")) != NULL) &&
  471. (strcmp(ethrotate, "no") == 0))
  472. return;
  473. if (!eth_current)
  474. return;
  475. if (first_restart) {
  476. first_failed = eth_current;
  477. }
  478. eth_current = eth_current->next;
  479. #ifdef CONFIG_NET_MULTI
  480. /* update current ethernet name */
  481. {
  482. char *act = getenv("ethact");
  483. if (act == NULL || strcmp(act, eth_current->name) != 0)
  484. setenv("ethact", eth_current->name);
  485. }
  486. #endif
  487. if (first_failed == eth_current) {
  488. NetRestartWrap = 1;
  489. }
  490. }
  491. #ifdef CONFIG_NET_MULTI
  492. void eth_set_current(void)
  493. {
  494. char *act;
  495. struct eth_device* old_current;
  496. if (!eth_current) /* XXX no current */
  497. return;
  498. act = getenv("ethact");
  499. if (act != NULL) {
  500. old_current = eth_current;
  501. do {
  502. if (strcmp(eth_current->name, act) == 0)
  503. return;
  504. eth_current = eth_current->next;
  505. } while (old_current != eth_current);
  506. }
  507. setenv("ethact", eth_current->name);
  508. }
  509. #endif
  510. char *eth_get_name (void)
  511. {
  512. return (eth_current ? eth_current->name : "unknown");
  513. }
  514. #elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI)
  515. extern int at91rm9200_miiphy_initialize(bd_t *bis);
  516. extern int emac4xx_miiphy_initialize(bd_t *bis);
  517. extern int mcf52x2_miiphy_initialize(bd_t *bis);
  518. extern int ns7520_miiphy_initialize(bd_t *bis);
  519. extern int dm644x_eth_miiphy_initialize(bd_t *bis);
  520. int eth_initialize(bd_t *bis)
  521. {
  522. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  523. miiphy_init();
  524. #endif
  525. #if defined(CONFIG_AT91RM9200)
  526. at91rm9200_miiphy_initialize(bis);
  527. #endif
  528. #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) \
  529. && !defined(CONFIG_AP1000) && !defined(CONFIG_405)
  530. emac4xx_miiphy_initialize(bis);
  531. #endif
  532. #if defined(CONFIG_MCF52x2)
  533. mcf52x2_miiphy_initialize(bis);
  534. #endif
  535. #if defined(CONFIG_NETARM)
  536. ns7520_miiphy_initialize(bis);
  537. #endif
  538. #if defined(CONFIG_DRIVER_TI_EMAC)
  539. dm644x_eth_miiphy_initialize(bis);
  540. #endif
  541. return 0;
  542. }
  543. #endif