eth.c 12 KB

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