eth.c 11 KB

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