eth.c 12 KB

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