eth.c 11 KB

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