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