wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
wdenk | 1ebf41e | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 2 | * (C) Copyright 2001-2004 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 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> |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 27 | #include <miiphy.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 28 | |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 29 | #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 30 | |
Ben Warren | d448a49 | 2008-06-23 22:57:27 -0700 | [diff] [blame] | 31 | /* |
| 32 | * CPU and board-specific Ethernet initializations. Aliased function |
| 33 | * signals caller to move on |
| 34 | */ |
| 35 | static int __def_eth_init(bd_t *bis) |
| 36 | { |
| 37 | return -1; |
| 38 | } |
| 39 | int cpu_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init"))); |
| 40 | int board_eth_init(bd_t *bis) __attribute((weak, alias("__def_eth_init"))); |
| 41 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 42 | #ifdef CFG_GT_6426x |
| 43 | extern int gt6426x_eth_initialize(bd_t *bis); |
| 44 | #endif |
| 45 | |
wdenk | 5da7f2f | 2004-01-03 00:43:19 +0000 | [diff] [blame] | 46 | extern int au1x00_enet_initialize(bd_t*); |
| 47 | extern int dc21x4x_initialize(bd_t*); |
wdenk | 4e112c1 | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 48 | extern int e1000_initialize(bd_t*); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 49 | extern int eepro100_initialize(bd_t*); |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 50 | extern int eth_3com_initialize(bd_t*); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 51 | extern int fec_initialize(bd_t*); |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 52 | extern int inca_switch_initialize(bd_t*); |
wdenk | 21136db | 2003-07-16 21:53:01 +0000 | [diff] [blame] | 53 | extern int mpc5xxx_fec_initialize(bd_t*); |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 54 | extern int mpc512x_fec_initialize(bd_t*); |
wdenk | 337f565 | 2004-10-28 00:09:35 +0000 | [diff] [blame] | 55 | extern int mpc8220_fec_initialize(bd_t*); |
wdenk | 5da7f2f | 2004-01-03 00:43:19 +0000 | [diff] [blame] | 56 | extern int mv6436x_eth_initialize(bd_t *); |
| 57 | extern int mv6446x_eth_initialize(bd_t *); |
| 58 | extern int natsemi_initialize(bd_t*); |
wdenk | 5da7f2f | 2004-01-03 00:43:19 +0000 | [diff] [blame] | 59 | extern int pcnet_initialize(bd_t*); |
| 60 | extern int plb2800_eth_initialize(bd_t*); |
| 61 | extern int ppc_4xx_eth_initialize(bd_t *); |
wdenk | 5da7f2f | 2004-01-03 00:43:19 +0000 | [diff] [blame] | 62 | extern int scc_initialize(bd_t*); |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 63 | extern int npe_initialize(bd_t *); |
Dave Liu | e732e9c | 2006-11-03 12:11:15 -0600 | [diff] [blame] | 64 | extern int uec_initialize(int); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 66 | #ifdef CONFIG_API |
| 67 | extern void (*push_packet)(volatile void *, int); |
| 68 | |
| 69 | static struct { |
| 70 | uchar data[PKTSIZE]; |
| 71 | int length; |
| 72 | } eth_rcv_bufs[PKTBUFSRX]; |
| 73 | |
| 74 | static unsigned int eth_rcv_current = 0, eth_rcv_last = 0; |
| 75 | #endif |
| 76 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 77 | static struct eth_device *eth_devices, *eth_current; |
| 78 | |
| 79 | struct eth_device *eth_get_dev(void) |
| 80 | { |
| 81 | return eth_current; |
| 82 | } |
| 83 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 84 | struct eth_device *eth_get_dev_by_name(char *devname) |
| 85 | { |
| 86 | struct eth_device *dev, *target_dev; |
| 87 | |
| 88 | if (!eth_devices) |
| 89 | return NULL; |
| 90 | |
| 91 | dev = eth_devices; |
| 92 | target_dev = NULL; |
| 93 | do { |
| 94 | if (strcmp(devname, dev->name) == 0) { |
| 95 | target_dev = dev; |
| 96 | break; |
| 97 | } |
| 98 | dev = dev->next; |
| 99 | } while (dev != eth_devices); |
| 100 | |
| 101 | return target_dev; |
| 102 | } |
| 103 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 104 | int eth_get_dev_index (void) |
| 105 | { |
| 106 | struct eth_device *dev; |
| 107 | int num = 0; |
| 108 | |
| 109 | if (!eth_devices) { |
| 110 | return (-1); |
| 111 | } |
| 112 | |
| 113 | for (dev = eth_devices; dev; dev = dev->next) { |
| 114 | if (dev == eth_current) |
| 115 | break; |
| 116 | ++num; |
| 117 | } |
| 118 | |
| 119 | if (dev) { |
| 120 | return (num); |
| 121 | } |
| 122 | |
| 123 | return (0); |
| 124 | } |
| 125 | |
| 126 | int eth_register(struct eth_device* dev) |
| 127 | { |
| 128 | struct eth_device *d; |
| 129 | |
| 130 | if (!eth_devices) { |
| 131 | eth_current = eth_devices = dev; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 132 | #ifdef CONFIG_NET_MULTI |
| 133 | /* update current ethernet name */ |
| 134 | { |
| 135 | char *act = getenv("ethact"); |
| 136 | if (act == NULL || strcmp(act, eth_current->name) != 0) |
| 137 | setenv("ethact", eth_current->name); |
| 138 | } |
| 139 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 140 | } else { |
| 141 | for (d=eth_devices; d->next!=eth_devices; d=d->next); |
| 142 | d->next = dev; |
| 143 | } |
| 144 | |
| 145 | dev->state = ETH_STATE_INIT; |
| 146 | dev->next = eth_devices; |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int eth_initialize(bd_t *bis) |
| 152 | { |
Shinya Kuribayashi | db7101e | 2007-11-19 20:27:04 +0900 | [diff] [blame] | 153 | char enetvar[32]; |
| 154 | unsigned char env_enetaddr[6]; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 155 | int i, eth_number = 0; |
| 156 | char *tmp, *end; |
| 157 | |
| 158 | eth_devices = NULL; |
| 159 | eth_current = NULL; |
| 160 | |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 161 | show_boot_progress (64); |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 162 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 163 | miiphy_init(); |
| 164 | #endif |
Ben Warren | d448a49 | 2008-06-23 22:57:27 -0700 | [diff] [blame] | 165 | /* Try board-specific initialization first. If it fails or isn't |
| 166 | * present, try the cpu-specific initialization */ |
| 167 | if (board_eth_init(bis) < 0) |
| 168 | cpu_eth_init(bis); |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 169 | |
Stefan Roese | 45993ea | 2006-11-29 15:42:37 +0100 | [diff] [blame] | 170 | #if defined(CONFIG_DB64360) || defined(CONFIG_CPCI750) |
wdenk | 5da7f2f | 2004-01-03 00:43:19 +0000 | [diff] [blame] | 171 | mv6436x_eth_initialize(bis); |
| 172 | #endif |
Stefan Roese | 45993ea | 2006-11-29 15:42:37 +0100 | [diff] [blame] | 173 | #if defined(CONFIG_DB64460) || defined(CONFIG_P3Mx) |
wdenk | 5da7f2f | 2004-01-03 00:43:19 +0000 | [diff] [blame] | 174 | mv6446x_eth_initialize(bis); |
| 175 | #endif |
Wolfgang Denk | 56811f6 | 2005-10-09 01:04:33 +0200 | [diff] [blame] | 176 | #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) && !defined(CONFIG_AP1000) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 177 | ppc_4xx_eth_initialize(bis); |
wdenk | 7793444 | 2003-06-05 19:37:36 +0000 | [diff] [blame] | 178 | #endif |
wdenk | 4fc9569 | 2003-02-28 00:49:47 +0000 | [diff] [blame] | 179 | #ifdef CONFIG_INCA_IP_SWITCH |
| 180 | inca_switch_initialize(bis); |
| 181 | #endif |
wdenk | b02744a | 2003-04-05 00:53:31 +0000 | [diff] [blame] | 182 | #ifdef CONFIG_PLB2800_ETHER |
| 183 | plb2800_eth_initialize(bis); |
| 184 | #endif |
wdenk | 63a10be | 2004-05-08 20:33:20 +0000 | [diff] [blame] | 185 | #ifdef SCC_ENET |
| 186 | scc_initialize(bis); |
| 187 | #endif |
wdenk | 63a10be | 2004-05-08 20:33:20 +0000 | [diff] [blame] | 188 | #if defined(CONFIG_MPC5xxx_FEC) |
| 189 | mpc5xxx_fec_initialize(bis); |
| 190 | #endif |
Rafal Jaworowski | d3a02c3 | 2007-07-27 14:43:59 +0200 | [diff] [blame] | 191 | #if defined(CONFIG_MPC512x_FEC) |
| 192 | mpc512x_fec_initialize (bis); |
| 193 | #endif |
wdenk | 5c71a7a | 2005-05-16 15:23:22 +0000 | [diff] [blame] | 194 | #if defined(CONFIG_MPC8220_FEC) |
wdenk | 337f565 | 2004-10-28 00:09:35 +0000 | [diff] [blame] | 195 | mpc8220_fec_initialize(bis); |
| 196 | #endif |
Dave Liu | e732e9c | 2006-11-03 12:11:15 -0600 | [diff] [blame] | 197 | #if defined(CONFIG_UEC_ETH1) |
| 198 | uec_initialize(0); |
| 199 | #endif |
| 200 | #if defined(CONFIG_UEC_ETH2) |
| 201 | uec_initialize(1); |
| 202 | #endif |
Joakim Tjernlund | 4e48320 | 2007-12-06 16:43:40 +0100 | [diff] [blame] | 203 | #if defined(CONFIG_UEC_ETH3) |
| 204 | uec_initialize(2); |
| 205 | #endif |
David Saada | f397883 | 2008-01-15 10:40:24 +0200 | [diff] [blame] | 206 | #if defined(CONFIG_UEC_ETH4) |
| 207 | uec_initialize(3); |
| 208 | #endif |
Jon Loeliger | 5c8aa97 | 2006-04-26 17:58:56 -0500 | [diff] [blame] | 209 | |
Stefan Roese | 0955402 | 2005-11-30 13:06:40 +0100 | [diff] [blame] | 210 | #if defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FCC) |
| 211 | fec_initialize(bis); |
| 212 | #endif |
wdenk | 63a10be | 2004-05-08 20:33:20 +0000 | [diff] [blame] | 213 | #if defined(CONFIG_AU1X00) |
| 214 | au1x00_enet_initialize(bis); |
| 215 | #endif |
Wolfgang Denk | 4646d2a | 2006-05-30 15:56:48 +0200 | [diff] [blame] | 216 | #if defined(CONFIG_IXP4XX_NPE) |
| 217 | npe_initialize(bis); |
| 218 | #endif |
wdenk | 4e112c1 | 2003-06-03 23:54:09 +0000 | [diff] [blame] | 219 | #ifdef CONFIG_E1000 |
| 220 | e1000_initialize(bis); |
| 221 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 222 | #ifdef CONFIG_EEPRO100 |
| 223 | eepro100_initialize(bis); |
| 224 | #endif |
| 225 | #ifdef CONFIG_TULIP |
| 226 | dc21x4x_initialize(bis); |
| 227 | #endif |
wdenk | 452cfd6 | 2002-11-19 11:04:11 +0000 | [diff] [blame] | 228 | #ifdef CONFIG_3COM |
| 229 | eth_3com_initialize(bis); |
| 230 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 231 | #ifdef CONFIG_PCNET |
| 232 | pcnet_initialize(bis); |
| 233 | #endif |
| 234 | #ifdef CFG_GT_6426x |
| 235 | gt6426x_eth_initialize(bis); |
| 236 | #endif |
| 237 | #ifdef CONFIG_NATSEMI |
| 238 | natsemi_initialize(bis); |
| 239 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 240 | if (!eth_devices) { |
| 241 | puts ("No ethernet found.\n"); |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 242 | show_boot_progress (-64); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 243 | } else { |
| 244 | struct eth_device *dev = eth_devices; |
| 245 | char *ethprime = getenv ("ethprime"); |
| 246 | |
Heiko Schocher | 8a8ec53 | 2007-07-13 09:54:17 +0200 | [diff] [blame] | 247 | show_boot_progress (65); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 248 | do { |
| 249 | if (eth_number) |
| 250 | puts (", "); |
| 251 | |
| 252 | printf("%s", dev->name); |
| 253 | |
| 254 | if (ethprime && strcmp (dev->name, ethprime) == 0) { |
| 255 | eth_current = dev; |
| 256 | puts (" [PRIME]"); |
| 257 | } |
| 258 | |
| 259 | sprintf(enetvar, eth_number ? "eth%daddr" : "ethaddr", eth_number); |
| 260 | tmp = getenv (enetvar); |
| 261 | |
| 262 | for (i=0; i<6; i++) { |
| 263 | env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0; |
| 264 | if (tmp) |
| 265 | tmp = (*end) ? end+1 : end; |
| 266 | } |
| 267 | |
| 268 | if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { |
| 269 | if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && |
| 270 | memcmp(dev->enetaddr, env_enetaddr, 6)) |
| 271 | { |
wdenk | 63a10be | 2004-05-08 20:33:20 +0000 | [diff] [blame] | 272 | printf ("\nWarning: %s MAC addresses don't match:\n", |
| 273 | dev->name); |
| 274 | printf ("Address in SROM is " |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 275 | "%02X:%02X:%02X:%02X:%02X:%02X\n", |
| 276 | dev->enetaddr[0], dev->enetaddr[1], |
| 277 | dev->enetaddr[2], dev->enetaddr[3], |
| 278 | dev->enetaddr[4], dev->enetaddr[5]); |
wdenk | 63a10be | 2004-05-08 20:33:20 +0000 | [diff] [blame] | 279 | printf ("Address in environment is " |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 280 | "%02X:%02X:%02X:%02X:%02X:%02X\n", |
| 281 | env_enetaddr[0], env_enetaddr[1], |
| 282 | env_enetaddr[2], env_enetaddr[3], |
| 283 | env_enetaddr[4], env_enetaddr[5]); |
| 284 | } |
| 285 | |
| 286 | memcpy(dev->enetaddr, env_enetaddr, 6); |
| 287 | } |
| 288 | |
| 289 | eth_number++; |
| 290 | dev = dev->next; |
| 291 | } while(dev != eth_devices); |
| 292 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 293 | #ifdef CONFIG_NET_MULTI |
| 294 | /* update current ethernet name */ |
| 295 | if (eth_current) { |
| 296 | char *act = getenv("ethact"); |
| 297 | if (act == NULL || strcmp(act, eth_current->name) != 0) |
| 298 | setenv("ethact", eth_current->name); |
| 299 | } else |
| 300 | setenv("ethact", NULL); |
| 301 | #endif |
| 302 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 303 | putc ('\n'); |
| 304 | } |
| 305 | |
| 306 | return eth_number; |
| 307 | } |
| 308 | |
| 309 | void eth_set_enetaddr(int num, char *addr) { |
| 310 | struct eth_device *dev; |
| 311 | unsigned char enetaddr[6]; |
| 312 | char *end; |
| 313 | int i; |
| 314 | |
wdenk | 1ebf41e | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 315 | debug ("eth_set_enetaddr(num=%d, addr=%s)\n", num, addr); |
| 316 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 317 | if (!eth_devices) |
| 318 | return; |
| 319 | |
| 320 | for (i=0; i<6; i++) { |
| 321 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; |
| 322 | if (addr) |
| 323 | addr = (*end) ? end+1 : end; |
| 324 | } |
| 325 | |
| 326 | dev = eth_devices; |
| 327 | while(num-- > 0) { |
| 328 | dev = dev->next; |
| 329 | |
| 330 | if (dev == eth_devices) |
| 331 | return; |
| 332 | } |
| 333 | |
wdenk | 1ebf41e | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 334 | debug ( "Setting new HW address on %s\n" |
| 335 | "New Address is %02X:%02X:%02X:%02X:%02X:%02X\n", |
| 336 | dev->name, |
Wolfgang Denk | b6ea630 | 2005-09-25 17:05:57 +0200 | [diff] [blame] | 337 | enetaddr[0], enetaddr[1], |
| 338 | enetaddr[2], enetaddr[3], |
| 339 | enetaddr[4], enetaddr[5]); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | |
| 341 | memcpy(dev->enetaddr, enetaddr, 6); |
| 342 | } |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 343 | #ifdef CONFIG_MCAST_TFTP |
| 344 | /* Multicast. |
| 345 | * mcast_addr: multicast ipaddr from which multicast Mac is made |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 346 | * join: 1=join, 0=leave. |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 347 | */ |
| 348 | int eth_mcast_join( IPaddr_t mcast_ip, u8 join) |
| 349 | { |
| 350 | u8 mcast_mac[6]; |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 351 | if (!eth_current || !eth_current->mcast) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 352 | return -1; |
| 353 | mcast_mac[5] = htonl(mcast_ip) & 0xff; |
| 354 | mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff; |
| 355 | mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f; |
| 356 | mcast_mac[2] = 0x5e; |
| 357 | mcast_mac[1] = 0x0; |
| 358 | mcast_mac[0] = 0x1; |
| 359 | return eth_current->mcast(eth_current, mcast_mac, join); |
| 360 | } |
| 361 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 362 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
| 363 | * and this is the ethernet-crc method needed for TSEC -- and perhaps |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 364 | * some other adapter -- hash tables |
| 365 | */ |
| 366 | #define CRCPOLY_LE 0xedb88320 |
| 367 | u32 ether_crc (size_t len, unsigned char const *p) |
| 368 | { |
| 369 | int i; |
| 370 | u32 crc; |
| 371 | crc = ~0; |
| 372 | while (len--) { |
| 373 | crc ^= *p++; |
| 374 | for (i = 0; i < 8; i++) |
| 375 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); |
| 376 | } |
| 377 | /* an reverse the bits, cuz of way they arrive -- last-first */ |
| 378 | crc = (crc >> 16) | (crc << 16); |
| 379 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); |
| 380 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); |
| 381 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); |
| 382 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); |
| 383 | return crc; |
| 384 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 385 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 386 | #endif |
| 387 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 388 | |
| 389 | int eth_init(bd_t *bis) |
| 390 | { |
| 391 | struct eth_device* old_current; |
| 392 | |
Stefan Roese | 11da2be | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 393 | if (!eth_current) { |
| 394 | puts ("No ethernet found.\n"); |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 395 | return -1; |
Stefan Roese | 11da2be | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 396 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 397 | |
| 398 | old_current = eth_current; |
| 399 | do { |
wdenk | 1ebf41e | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 400 | debug ("Trying %s\n", eth_current->name); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 401 | |
Ben Warren | de9fcb5 | 2008-01-09 18:15:53 -0500 | [diff] [blame] | 402 | if (eth_current->init(eth_current,bis) >= 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 403 | eth_current->state = ETH_STATE_ACTIVE; |
| 404 | |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 405 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 406 | } |
wdenk | 1ebf41e | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 407 | debug ("FAIL\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 408 | |
| 409 | eth_try_another(0); |
| 410 | } while (old_current != eth_current); |
| 411 | |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 412 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | void eth_halt(void) |
| 416 | { |
| 417 | if (!eth_current) |
| 418 | return; |
| 419 | |
| 420 | eth_current->halt(eth_current); |
| 421 | |
| 422 | eth_current->state = ETH_STATE_PASSIVE; |
| 423 | } |
| 424 | |
| 425 | int eth_send(volatile void *packet, int length) |
| 426 | { |
| 427 | if (!eth_current) |
| 428 | return -1; |
| 429 | |
| 430 | return eth_current->send(eth_current, packet, length); |
| 431 | } |
| 432 | |
| 433 | int eth_rx(void) |
| 434 | { |
| 435 | if (!eth_current) |
| 436 | return -1; |
| 437 | |
| 438 | return eth_current->recv(eth_current); |
| 439 | } |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 440 | |
| 441 | #ifdef CONFIG_API |
| 442 | static void eth_save_packet(volatile void *packet, int length) |
| 443 | { |
| 444 | volatile char *p = packet; |
| 445 | int i; |
| 446 | |
| 447 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) |
| 448 | return; |
| 449 | |
| 450 | if (PKTSIZE < length) |
| 451 | return; |
| 452 | |
| 453 | for (i = 0; i < length; i++) |
| 454 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; |
| 455 | |
| 456 | eth_rcv_bufs[eth_rcv_last].length = length; |
| 457 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; |
| 458 | } |
| 459 | |
| 460 | int eth_receive(volatile void *packet, int length) |
| 461 | { |
| 462 | volatile char *p = packet; |
| 463 | void *pp = push_packet; |
| 464 | int i; |
| 465 | |
| 466 | if (eth_rcv_current == eth_rcv_last) { |
| 467 | push_packet = eth_save_packet; |
| 468 | eth_rx(); |
| 469 | push_packet = pp; |
| 470 | |
| 471 | if (eth_rcv_current == eth_rcv_last) |
| 472 | return -1; |
| 473 | } |
| 474 | |
| 475 | if (length < eth_rcv_bufs[eth_rcv_current].length) |
| 476 | return -1; |
| 477 | |
| 478 | length = eth_rcv_bufs[eth_rcv_current].length; |
| 479 | |
| 480 | for (i = 0; i < length; i++) |
| 481 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; |
| 482 | |
| 483 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; |
| 484 | return length; |
| 485 | } |
| 486 | #endif /* CONFIG_API */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 487 | |
| 488 | void eth_try_another(int first_restart) |
| 489 | { |
| 490 | static struct eth_device *first_failed = NULL; |
Matthias Fuchs | 204f0ec | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 491 | char *ethrotate; |
| 492 | |
| 493 | /* |
| 494 | * Do not rotate between network interfaces when |
| 495 | * 'ethrotate' variable is set to 'no'. |
| 496 | */ |
| 497 | if (((ethrotate = getenv ("ethrotate")) != NULL) && |
| 498 | (strcmp(ethrotate, "no") == 0)) |
| 499 | return; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 500 | |
| 501 | if (!eth_current) |
| 502 | return; |
| 503 | |
wdenk | 63a10be | 2004-05-08 20:33:20 +0000 | [diff] [blame] | 504 | if (first_restart) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 505 | first_failed = eth_current; |
| 506 | } |
| 507 | |
| 508 | eth_current = eth_current->next; |
| 509 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 510 | #ifdef CONFIG_NET_MULTI |
| 511 | /* update current ethernet name */ |
| 512 | { |
| 513 | char *act = getenv("ethact"); |
| 514 | if (act == NULL || strcmp(act, eth_current->name) != 0) |
| 515 | setenv("ethact", eth_current->name); |
| 516 | } |
| 517 | #endif |
| 518 | |
wdenk | 63a10be | 2004-05-08 20:33:20 +0000 | [diff] [blame] | 519 | if (first_failed == eth_current) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 520 | NetRestartWrap = 1; |
| 521 | } |
| 522 | } |
| 523 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 524 | #ifdef CONFIG_NET_MULTI |
| 525 | void eth_set_current(void) |
| 526 | { |
| 527 | char *act; |
| 528 | struct eth_device* old_current; |
| 529 | |
| 530 | if (!eth_current) /* XXX no current */ |
| 531 | return; |
| 532 | |
| 533 | act = getenv("ethact"); |
| 534 | if (act != NULL) { |
| 535 | old_current = eth_current; |
| 536 | do { |
| 537 | if (strcmp(eth_current->name, act) == 0) |
| 538 | return; |
| 539 | eth_current = eth_current->next; |
| 540 | } while (old_current != eth_current); |
| 541 | } |
| 542 | |
| 543 | setenv("ethact", eth_current->name); |
| 544 | } |
| 545 | #endif |
| 546 | |
wdenk | c6abb7e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 547 | char *eth_get_name (void) |
| 548 | { |
| 549 | return (eth_current ? eth_current->name : "unknown"); |
| 550 | } |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 551 | #elif defined(CONFIG_CMD_NET) && !defined(CONFIG_NET_MULTI) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 552 | |
| 553 | extern int at91rm9200_miiphy_initialize(bd_t *bis); |
| 554 | extern int emac4xx_miiphy_initialize(bd_t *bis); |
| 555 | extern int mcf52x2_miiphy_initialize(bd_t *bis); |
| 556 | extern int ns7520_miiphy_initialize(bd_t *bis); |
Jean-Christophe PLAGNIOL-VILLARD | bcf268b | 2008-08-31 04:45:42 +0200 | [diff] [blame] | 557 | extern int davinci_eth_miiphy_initialize(bd_t *bis); |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 558 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 559 | |
| 560 | int eth_initialize(bd_t *bis) |
| 561 | { |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 562 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 563 | miiphy_init(); |
| 564 | #endif |
| 565 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 566 | #if defined(CONFIG_AT91RM9200) |
| 567 | at91rm9200_miiphy_initialize(bis); |
| 568 | #endif |
| 569 | #if defined(CONFIG_4xx) && !defined(CONFIG_IOP480) \ |
| 570 | && !defined(CONFIG_AP1000) && !defined(CONFIG_405) |
| 571 | emac4xx_miiphy_initialize(bis); |
| 572 | #endif |
| 573 | #if defined(CONFIG_MCF52x2) |
| 574 | mcf52x2_miiphy_initialize(bis); |
| 575 | #endif |
Wolfgang Denk | 81490f4 | 2008-07-13 23:07:35 +0200 | [diff] [blame] | 576 | #if defined(CONFIG_DRIVER_NS7520_ETHERNET) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 577 | ns7520_miiphy_initialize(bis); |
| 578 | #endif |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 579 | #if defined(CONFIG_DRIVER_TI_EMAC) |
Sandeep Paulraj | 4b26f05 | 2008-08-31 00:39:46 +0200 | [diff] [blame] | 580 | davinci_eth_miiphy_initialize(bis); |
Sergey Kubushyn | e8f3912 | 2007-08-10 20:26:18 +0200 | [diff] [blame] | 581 | #endif |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 582 | return 0; |
| 583 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 584 | #endif |