eth.c 13 KB

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