eth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * (C) Copyright 2001, 2002
  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. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI)
  27. #ifdef CFG_GT_6426x
  28. extern int gt6426x_eth_initialize(bd_t *bis);
  29. #endif
  30. extern int eepro100_initialize(bd_t*);
  31. extern int natsemi_initialize(bd_t*);
  32. extern int ns8382x_initialize(bd_t*);
  33. extern int dc21x4x_initialize(bd_t*);
  34. extern int eth_3com_initialize(bd_t*);
  35. extern int pcnet_initialize(bd_t*);
  36. extern int fec_initialize(bd_t*);
  37. extern int scc_initialize(bd_t*);
  38. extern int inca_switch_initialize(bd_t*);
  39. static struct eth_device *eth_devices, *eth_current;
  40. struct eth_device *eth_get_dev(void)
  41. {
  42. return eth_current;
  43. }
  44. int eth_get_dev_index (void)
  45. {
  46. struct eth_device *dev;
  47. int num = 0;
  48. if (!eth_devices) {
  49. return (-1);
  50. }
  51. for (dev = eth_devices; dev; dev = dev->next) {
  52. if (dev == eth_current)
  53. break;
  54. ++num;
  55. }
  56. if (dev) {
  57. return (num);
  58. }
  59. return (0);
  60. }
  61. int eth_register(struct eth_device* dev)
  62. {
  63. struct eth_device *d;
  64. if (!eth_devices) {
  65. eth_current = eth_devices = dev;
  66. } else {
  67. for (d=eth_devices; d->next!=eth_devices; d=d->next);
  68. d->next = dev;
  69. }
  70. dev->state = ETH_STATE_INIT;
  71. dev->next = eth_devices;
  72. return 0;
  73. }
  74. int eth_initialize(bd_t *bis)
  75. {
  76. unsigned char enetvar[32], env_enetaddr[6];
  77. int i, eth_number = 0;
  78. char *tmp, *end;
  79. eth_devices = NULL;
  80. eth_current = NULL;
  81. #ifdef CONFIG_INCA_IP_SWITCH
  82. inca_switch_initialize(bis);
  83. #endif
  84. #ifdef CONFIG_EEPRO100
  85. eepro100_initialize(bis);
  86. #endif
  87. #ifdef CONFIG_TULIP
  88. dc21x4x_initialize(bis);
  89. #endif
  90. #ifdef CONFIG_3COM
  91. eth_3com_initialize(bis);
  92. #endif
  93. #ifdef CONFIG_PCNET
  94. pcnet_initialize(bis);
  95. #endif
  96. #ifdef CFG_GT_6426x
  97. gt6426x_eth_initialize(bis);
  98. #endif
  99. #ifdef CONFIG_NATSEMI
  100. natsemi_initialize(bis);
  101. #endif
  102. #ifdef CONFIG_NS8382X
  103. ns8382x_initialize(bis);
  104. #endif
  105. #ifdef SCC_ENET
  106. scc_initialize(bis);
  107. #endif
  108. #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC)
  109. fec_initialize(bis);
  110. #endif
  111. if (!eth_devices) {
  112. puts ("No ethernet found.\n");
  113. } else {
  114. struct eth_device *dev = eth_devices;
  115. char *ethprime = getenv ("ethprime");
  116. do {
  117. if (eth_number)
  118. puts (", ");
  119. printf("%s", dev->name);
  120. if (ethprime && strcmp (dev->name, ethprime) == 0) {
  121. eth_current = dev;
  122. puts (" [PRIME]");
  123. }
  124. sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number);
  125. tmp = getenv (enetvar);
  126. for (i=0; i<6; i++) {
  127. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  128. if (tmp)
  129. tmp = (*end) ? end+1 : end;
  130. }
  131. if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
  132. if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
  133. memcmp(dev->enetaddr, env_enetaddr, 6))
  134. {
  135. printf("\nWarning: %s MAC addresses don't match:\n", dev->name);
  136. printf("Address in SROM is "
  137. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  138. dev->enetaddr[0], dev->enetaddr[1],
  139. dev->enetaddr[2], dev->enetaddr[3],
  140. dev->enetaddr[4], dev->enetaddr[5]);
  141. printf("Address in environment is "
  142. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  143. env_enetaddr[0], env_enetaddr[1],
  144. env_enetaddr[2], env_enetaddr[3],
  145. env_enetaddr[4], env_enetaddr[5]);
  146. }
  147. memcpy(dev->enetaddr, env_enetaddr, 6);
  148. }
  149. eth_number++;
  150. dev = dev->next;
  151. } while(dev != eth_devices);
  152. putc ('\n');
  153. }
  154. return eth_number;
  155. }
  156. void eth_set_enetaddr(int num, char *addr) {
  157. struct eth_device *dev;
  158. unsigned char enetaddr[6];
  159. char *end;
  160. int i;
  161. #ifdef DEBUG
  162. printf("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr);
  163. #endif
  164. if (!eth_devices)
  165. return;
  166. for (i=0; i<6; i++) {
  167. enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0;
  168. if (addr)
  169. addr = (*end) ? end+1 : end;
  170. }
  171. dev = eth_devices;
  172. while(num-- > 0) {
  173. dev = dev->next;
  174. if (dev == eth_devices)
  175. return;
  176. }
  177. #ifdef DEBUG
  178. printf("Setting new HW address on %s\n", dev->name);
  179. printf("New Address is "
  180. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  181. dev->enetaddr[0], dev->enetaddr[1],
  182. dev->enetaddr[2], dev->enetaddr[3],
  183. dev->enetaddr[4], dev->enetaddr[5]);
  184. #endif
  185. memcpy(dev->enetaddr, enetaddr, 6);
  186. }
  187. int eth_init(bd_t *bis)
  188. {
  189. struct eth_device* old_current;
  190. if (!eth_current)
  191. return 0;
  192. old_current = eth_current;
  193. do {
  194. #ifdef DEBUG
  195. printf("Trying %s\n", eth_current->name);
  196. #endif
  197. if (eth_current->init(eth_current, bis)) {
  198. eth_current->state = ETH_STATE_ACTIVE;
  199. return 1;
  200. }
  201. #ifdef DEBUG
  202. puts ("FAIL\n");
  203. #endif
  204. eth_try_another(0);
  205. } while (old_current != eth_current);
  206. return 0;
  207. }
  208. void eth_halt(void)
  209. {
  210. if (!eth_current)
  211. return;
  212. eth_current->halt(eth_current);
  213. eth_current->state = ETH_STATE_PASSIVE;
  214. }
  215. int eth_send(volatile void *packet, int length)
  216. {
  217. if (!eth_current)
  218. return -1;
  219. return eth_current->send(eth_current, packet, length);
  220. }
  221. int eth_rx(void)
  222. {
  223. if (!eth_current)
  224. return -1;
  225. return eth_current->recv(eth_current);
  226. }
  227. void eth_try_another(int first_restart)
  228. {
  229. static struct eth_device *first_failed = NULL;
  230. if (!eth_current)
  231. return;
  232. if (first_restart)
  233. {
  234. first_failed = eth_current;
  235. }
  236. eth_current = eth_current->next;
  237. if (first_failed == eth_current)
  238. {
  239. NetRestartWrap = 1;
  240. }
  241. }
  242. #endif