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