eth.c 5.7 KB

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