wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Detlev Zundel | d46bcd3 | 2010-03-31 17:56:08 +0200 | [diff] [blame] | 2 | * (C) Copyright 2001-2010 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
| 10 | #include <net.h> |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 11 | #include <miiphy.h> |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 12 | #include <phy.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 13 | |
Mike Frysinger | c34319c | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 14 | void eth_parse_enetaddr(const char *addr, uchar *enetaddr) |
| 15 | { |
| 16 | char *end; |
| 17 | int i; |
| 18 | |
| 19 | for (i = 0; i < 6; ++i) { |
| 20 | enetaddr[i] = addr ? simple_strtoul(addr, &end, 16) : 0; |
| 21 | if (addr) |
| 22 | addr = (*end) ? end + 1 : end; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | int eth_getenv_enetaddr(char *name, uchar *enetaddr) |
| 27 | { |
| 28 | eth_parse_enetaddr(getenv(name), enetaddr); |
| 29 | return is_valid_ether_addr(enetaddr); |
| 30 | } |
| 31 | |
| 32 | int eth_setenv_enetaddr(char *name, const uchar *enetaddr) |
| 33 | { |
| 34 | char buf[20]; |
| 35 | |
| 36 | sprintf(buf, "%pM", enetaddr); |
| 37 | |
| 38 | return setenv(name, buf); |
| 39 | } |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 40 | |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 41 | int eth_getenv_enetaddr_by_index(const char *base_name, int index, |
| 42 | uchar *enetaddr) |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 43 | { |
| 44 | char enetvar[32]; |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 45 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 46 | return eth_getenv_enetaddr(enetvar, enetaddr); |
| 47 | } |
Mike Frysinger | c34319c | 2009-01-29 19:43:44 -0500 | [diff] [blame] | 48 | |
Joe Hershberger | 6c6fe5f | 2012-07-10 16:23:22 -0500 | [diff] [blame] | 49 | static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index, |
Rob Herring | c2938c0 | 2012-04-14 18:06:49 +0000 | [diff] [blame] | 50 | uchar *enetaddr) |
| 51 | { |
| 52 | char enetvar[32]; |
| 53 | sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index); |
| 54 | return eth_setenv_enetaddr(enetvar, enetaddr); |
| 55 | } |
| 56 | |
| 57 | |
Ben Warren | 6db991a | 2010-04-26 11:11:46 -0700 | [diff] [blame] | 58 | static int eth_mac_skip(int index) |
| 59 | { |
| 60 | char enetvar[15]; |
| 61 | char *skip_state; |
| 62 | sprintf(enetvar, index ? "eth%dmacskip" : "ethmacskip", index); |
| 63 | return ((skip_state = getenv(enetvar)) != NULL); |
| 64 | } |
| 65 | |
Michael Walle | 59d103a | 2012-06-05 11:33:16 +0000 | [diff] [blame] | 66 | #ifdef CONFIG_RANDOM_MACADDR |
| 67 | void eth_random_enetaddr(uchar *enetaddr) |
| 68 | { |
| 69 | uint32_t rval; |
| 70 | |
| 71 | srand(get_timer(0)); |
| 72 | |
| 73 | rval = rand(); |
| 74 | enetaddr[0] = rval & 0xff; |
| 75 | enetaddr[1] = (rval >> 8) & 0xff; |
| 76 | enetaddr[2] = (rval >> 16) & 0xff; |
| 77 | |
| 78 | rval = rand(); |
| 79 | enetaddr[3] = rval & 0xff; |
| 80 | enetaddr[4] = (rval >> 8) & 0xff; |
| 81 | enetaddr[5] = (rval >> 16) & 0xff; |
| 82 | |
| 83 | /* make sure it's local and unicast */ |
| 84 | enetaddr[0] = (enetaddr[0] | 0x02) & ~0x01; |
| 85 | } |
| 86 | #endif |
| 87 | |
Ben Warren | d448a49 | 2008-06-23 22:57:27 -0700 | [diff] [blame] | 88 | /* |
| 89 | * CPU and board-specific Ethernet initializations. Aliased function |
| 90 | * signals caller to move on |
| 91 | */ |
| 92 | static int __def_eth_init(bd_t *bis) |
| 93 | { |
| 94 | return -1; |
| 95 | } |
Peter Tyser | 21d2cd2 | 2009-04-20 11:08:46 -0500 | [diff] [blame] | 96 | int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
| 97 | int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init"))); |
Ben Warren | d448a49 | 2008-06-23 22:57:27 -0700 | [diff] [blame] | 98 | |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 99 | #ifdef CONFIG_API |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 100 | static struct { |
| 101 | uchar data[PKTSIZE]; |
| 102 | int length; |
| 103 | } eth_rcv_bufs[PKTBUFSRX]; |
| 104 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 105 | static unsigned int eth_rcv_current, eth_rcv_last; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 106 | #endif |
| 107 | |
Joe Hershberger | 9f37406 | 2012-08-03 10:59:08 +0000 | [diff] [blame] | 108 | static struct eth_device *eth_devices; |
| 109 | struct eth_device *eth_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 110 | |
Ben Warren | 97824d6 | 2010-07-29 12:56:11 -0700 | [diff] [blame] | 111 | struct eth_device *eth_get_dev_by_name(const char *devname) |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 112 | { |
| 113 | struct eth_device *dev, *target_dev; |
| 114 | |
Helmut Raiger | 153bf3a | 2011-08-22 00:17:17 +0000 | [diff] [blame] | 115 | BUG_ON(devname == NULL); |
| 116 | |
Marian Balakowicz | aab8c49 | 2005-10-28 22:30:33 +0200 | [diff] [blame] | 117 | if (!eth_devices) |
| 118 | return NULL; |
| 119 | |
| 120 | dev = eth_devices; |
| 121 | target_dev = NULL; |
| 122 | do { |
| 123 | if (strcmp(devname, dev->name) == 0) { |
| 124 | target_dev = dev; |
| 125 | break; |
| 126 | } |
| 127 | dev = dev->next; |
| 128 | } while (dev != eth_devices); |
| 129 | |
| 130 | return target_dev; |
| 131 | } |
| 132 | |
Andy Fleming | e7fd34b | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 133 | struct eth_device *eth_get_dev_by_index(int index) |
| 134 | { |
| 135 | struct eth_device *dev, *target_dev; |
Andy Fleming | e7fd34b | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 136 | |
| 137 | if (!eth_devices) |
| 138 | return NULL; |
| 139 | |
| 140 | dev = eth_devices; |
| 141 | target_dev = NULL; |
| 142 | do { |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 143 | if (dev->index == index) { |
Andy Fleming | e7fd34b | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 144 | target_dev = dev; |
| 145 | break; |
| 146 | } |
| 147 | dev = dev->next; |
Andy Fleming | e7fd34b | 2009-02-11 15:07:24 -0600 | [diff] [blame] | 148 | } while (dev != eth_devices); |
| 149 | |
| 150 | return target_dev; |
| 151 | } |
| 152 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 153 | int eth_get_dev_index(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 154 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 155 | if (!eth_current) |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 156 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 157 | |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 158 | return eth_current->index; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 161 | static void eth_current_changed(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 162 | { |
Mike Frysinger | a23230c | 2011-10-02 10:01:27 +0000 | [diff] [blame] | 163 | char *act = getenv("ethact"); |
| 164 | /* update current ethernet name */ |
| 165 | if (eth_current) { |
| 166 | if (act == NULL || strcmp(act, eth_current->name) != 0) |
| 167 | setenv("ethact", eth_current->name); |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 168 | } |
Mike Frysinger | a23230c | 2011-10-02 10:01:27 +0000 | [diff] [blame] | 169 | /* |
| 170 | * remove the variable completely if there is no active |
| 171 | * interface |
| 172 | */ |
| 173 | else if (act != NULL) |
| 174 | setenv("ethact", NULL); |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 177 | int eth_write_hwaddr(struct eth_device *dev, const char *base_name, |
| 178 | int eth_number) |
| 179 | { |
| 180 | unsigned char env_enetaddr[6]; |
| 181 | int ret = 0; |
| 182 | |
Eric Miao | ae97ef6 | 2012-01-18 22:56:33 +0000 | [diff] [blame] | 183 | eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr); |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 184 | |
| 185 | if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) { |
| 186 | if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) && |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 187 | memcmp(dev->enetaddr, env_enetaddr, 6)) { |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 188 | printf("\nWarning: %s MAC addresses don't match:\n", |
| 189 | dev->name); |
| 190 | printf("Address in SROM is %pM\n", |
| 191 | dev->enetaddr); |
| 192 | printf("Address in environment is %pM\n", |
| 193 | env_enetaddr); |
| 194 | } |
| 195 | |
| 196 | memcpy(dev->enetaddr, env_enetaddr, 6); |
Rob Herring | c2938c0 | 2012-04-14 18:06:49 +0000 | [diff] [blame] | 197 | } else if (is_valid_ether_addr(dev->enetaddr)) { |
| 198 | eth_setenv_enetaddr_by_index(base_name, eth_number, |
| 199 | dev->enetaddr); |
| 200 | printf("\nWarning: %s using MAC address from net device\n", |
| 201 | dev->name); |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (dev->write_hwaddr && |
Benoît Thébaudeau | 050219e | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 205 | !eth_mac_skip(eth_number)) { |
| 206 | if (!is_valid_ether_addr(dev->enetaddr)) |
| 207 | return -1; |
| 208 | |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 209 | ret = dev->write_hwaddr(dev); |
Benoît Thébaudeau | 050219e | 2012-08-10 07:56:21 +0000 | [diff] [blame] | 210 | } |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 215 | int eth_register(struct eth_device *dev) |
| 216 | { |
| 217 | struct eth_device *d; |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 218 | static int index; |
Michal Simek | 5b4945b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 219 | |
Mike Frysinger | 6b300dc | 2011-11-10 14:11:04 +0000 | [diff] [blame] | 220 | assert(strlen(dev->name) < sizeof(dev->name)); |
Michal Simek | 5b4945b | 2011-08-29 23:30:13 +0000 | [diff] [blame] | 221 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 222 | if (!eth_devices) { |
| 223 | eth_current = eth_devices = dev; |
| 224 | eth_current_changed(); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 225 | } else { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 226 | for (d = eth_devices; d->next != eth_devices; d = d->next) |
Detlev Zundel | d46bcd3 | 2010-03-31 17:56:08 +0200 | [diff] [blame] | 227 | ; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 228 | d->next = dev; |
| 229 | } |
| 230 | |
| 231 | dev->state = ETH_STATE_INIT; |
| 232 | dev->next = eth_devices; |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 233 | dev->index = index++; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Vincent Palatin | 451be50 | 2012-01-09 08:32:36 +0000 | [diff] [blame] | 238 | int eth_unregister(struct eth_device *dev) |
| 239 | { |
| 240 | struct eth_device *cur; |
| 241 | |
| 242 | /* No device */ |
| 243 | if (!eth_devices) |
| 244 | return -1; |
| 245 | |
| 246 | for (cur = eth_devices; cur->next != eth_devices && cur->next != dev; |
| 247 | cur = cur->next) |
| 248 | ; |
| 249 | |
| 250 | /* Device not found */ |
| 251 | if (cur->next != dev) |
| 252 | return -1; |
| 253 | |
| 254 | cur->next = dev->next; |
| 255 | |
| 256 | if (eth_devices == dev) |
| 257 | eth_devices = dev->next == eth_devices ? NULL : dev->next; |
| 258 | |
| 259 | if (eth_current == dev) { |
| 260 | eth_current = eth_devices; |
| 261 | eth_current_changed(); |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Mike Frysinger | 36fe7db | 2012-04-04 18:53:41 +0000 | [diff] [blame] | 267 | static void eth_env_init(bd_t *bis) |
| 268 | { |
| 269 | const char *s; |
| 270 | |
| 271 | if ((s = getenv("bootfile")) != NULL) |
| 272 | copy_filename(BootFile, s, sizeof(BootFile)); |
| 273 | } |
| 274 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 275 | int eth_initialize(bd_t *bis) |
| 276 | { |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 277 | int num_devices = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 278 | eth_devices = NULL; |
| 279 | eth_current = NULL; |
| 280 | |
Simon Glass | 0169e6b | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 281 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_START); |
Jon Loeliger | 54f35c2 | 2007-07-09 17:45:14 -0500 | [diff] [blame] | 282 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 283 | miiphy_init(); |
| 284 | #endif |
Andy Fleming | aecf6fc | 2011-04-08 02:10:27 -0500 | [diff] [blame] | 285 | |
| 286 | #ifdef CONFIG_PHYLIB |
| 287 | phy_init(); |
| 288 | #endif |
| 289 | |
Mike Frysinger | 36fe7db | 2012-04-04 18:53:41 +0000 | [diff] [blame] | 290 | eth_env_init(bis); |
| 291 | |
Ben Warren | cfdc8e0 | 2010-08-31 23:05:04 -0700 | [diff] [blame] | 292 | /* |
| 293 | * If board-specific initialization exists, call it. |
| 294 | * If not, call a CPU-specific one |
| 295 | */ |
| 296 | if (board_eth_init != __def_eth_init) { |
| 297 | if (board_eth_init(bis) < 0) |
| 298 | printf("Board Net Initialization Failed\n"); |
| 299 | } else if (cpu_eth_init != __def_eth_init) { |
| 300 | if (cpu_eth_init(bis) < 0) |
| 301 | printf("CPU Net Initialization Failed\n"); |
| 302 | } else |
| 303 | printf("Net Initialization Skipped\n"); |
Marian Balakowicz | cbdd1c8 | 2005-11-30 18:06:04 +0100 | [diff] [blame] | 304 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 305 | if (!eth_devices) { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 306 | puts("No ethernet found.\n"); |
Simon Glass | 0169e6b | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 307 | bootstage_error(BOOTSTAGE_ID_NET_ETH_START); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 308 | } else { |
| 309 | struct eth_device *dev = eth_devices; |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 310 | char *ethprime = getenv("ethprime"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 311 | |
Simon Glass | 0169e6b | 2012-02-13 13:51:18 +0000 | [diff] [blame] | 312 | bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 313 | do { |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 314 | if (dev->index) |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 315 | puts(", "); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 316 | |
| 317 | printf("%s", dev->name); |
| 318 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 319 | if (ethprime && strcmp(dev->name, ethprime) == 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 320 | eth_current = dev; |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 321 | puts(" [PRIME]"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Mike Frysinger | 2843ed9 | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 324 | if (strchr(dev->name, ' ')) |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 325 | puts("\nWarning: eth device name has a space!" |
| 326 | "\n"); |
Mike Frysinger | 2843ed9 | 2010-06-09 22:10:48 -0400 | [diff] [blame] | 327 | |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 328 | if (eth_write_hwaddr(dev, "eth", dev->index)) |
Philip Balister | dde025c | 2011-09-07 01:57:48 +0000 | [diff] [blame] | 329 | puts("\nWarning: failed to set MAC address\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 330 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 331 | dev = dev->next; |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 332 | num_devices++; |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 333 | } while (dev != eth_devices); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 334 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 335 | eth_current_changed(); |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 336 | putc('\n'); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 339 | return num_devices; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 340 | } |
| 341 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 342 | #ifdef CONFIG_MCAST_TFTP |
| 343 | /* Multicast. |
| 344 | * mcast_addr: multicast ipaddr from which multicast Mac is made |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 345 | * join: 1=join, 0=leave. |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 346 | */ |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 347 | int eth_mcast_join(IPaddr_t mcast_ip, u8 join) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 348 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 349 | u8 mcast_mac[6]; |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 350 | if (!eth_current || !eth_current->mcast) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 351 | return -1; |
| 352 | mcast_mac[5] = htonl(mcast_ip) & 0xff; |
| 353 | mcast_mac[4] = (htonl(mcast_ip)>>8) & 0xff; |
| 354 | mcast_mac[3] = (htonl(mcast_ip)>>16) & 0x7f; |
| 355 | mcast_mac[2] = 0x5e; |
| 356 | mcast_mac[1] = 0x0; |
| 357 | mcast_mac[0] = 0x1; |
| 358 | return eth_current->mcast(eth_current, mcast_mac, join); |
| 359 | } |
| 360 | |
Wolfgang Denk | 627f5c3 | 2007-08-14 09:47:27 +0200 | [diff] [blame] | 361 | /* the 'way' for ethernet-CRC-32. Spliced in from Linux lib/crc32.c |
| 362 | * and this is the ethernet-crc method needed for TSEC -- and perhaps |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 363 | * some other adapter -- hash tables |
| 364 | */ |
| 365 | #define CRCPOLY_LE 0xedb88320 |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 366 | u32 ether_crc(size_t len, unsigned char const *p) |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 367 | { |
| 368 | int i; |
| 369 | u32 crc; |
| 370 | crc = ~0; |
| 371 | while (len--) { |
| 372 | crc ^= *p++; |
| 373 | for (i = 0; i < 8; i++) |
| 374 | crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0); |
| 375 | } |
| 376 | /* an reverse the bits, cuz of way they arrive -- last-first */ |
| 377 | crc = (crc >> 16) | (crc << 16); |
| 378 | crc = (crc >> 8 & 0x00ff00ff) | (crc << 8 & 0xff00ff00); |
| 379 | crc = (crc >> 4 & 0x0f0f0f0f) | (crc << 4 & 0xf0f0f0f0); |
| 380 | crc = (crc >> 2 & 0x33333333) | (crc << 2 & 0xcccccccc); |
| 381 | crc = (crc >> 1 & 0x55555555) | (crc << 1 & 0xaaaaaaaa); |
| 382 | return crc; |
| 383 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 384 | |
David Updegraff | 7280da7 | 2007-06-11 10:41:07 -0500 | [diff] [blame] | 385 | #endif |
| 386 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 387 | |
| 388 | int eth_init(bd_t *bis) |
| 389 | { |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 390 | struct eth_device *old_current, *dev; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 391 | |
Stefan Roese | 11da2be | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 392 | if (!eth_current) { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 393 | puts("No ethernet found.\n"); |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 394 | return -1; |
Stefan Roese | 11da2be | 2008-03-04 17:40:41 +0100 | [diff] [blame] | 395 | } |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 396 | |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 397 | /* Sync environment with network devices */ |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 398 | dev = eth_devices; |
| 399 | do { |
| 400 | uchar env_enetaddr[6]; |
| 401 | |
Michael Walle | 391da4c | 2011-10-27 11:31:35 +0000 | [diff] [blame] | 402 | if (eth_getenv_enetaddr_by_index("eth", dev->index, |
Simon Glass | 62b36c9 | 2011-06-13 16:13:10 -0700 | [diff] [blame] | 403 | env_enetaddr)) |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 404 | memcpy(dev->enetaddr, env_enetaddr, 6); |
| 405 | |
Mike Frysinger | e129d56 | 2009-07-15 21:31:28 -0400 | [diff] [blame] | 406 | dev = dev->next; |
| 407 | } while (dev != eth_devices); |
| 408 | |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 409 | old_current = eth_current; |
| 410 | do { |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 411 | debug("Trying %s\n", eth_current->name); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 412 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 413 | if (eth_current->init(eth_current, bis) >= 0) { |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 414 | eth_current->state = ETH_STATE_ACTIVE; |
| 415 | |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 416 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 417 | } |
Robin Getz | 9e0a4d6 | 2009-07-23 03:01:03 -0400 | [diff] [blame] | 418 | debug("FAIL\n"); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 419 | |
| 420 | eth_try_another(0); |
| 421 | } while (old_current != eth_current); |
| 422 | |
Upakul Barkakaty | aed3358 | 2007-11-29 12:16:13 +0530 | [diff] [blame] | 423 | return -1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | void eth_halt(void) |
| 427 | { |
| 428 | if (!eth_current) |
| 429 | return; |
| 430 | |
| 431 | eth_current->halt(eth_current); |
| 432 | |
| 433 | eth_current->state = ETH_STATE_PASSIVE; |
| 434 | } |
| 435 | |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 436 | int eth_send(void *packet, int length) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 437 | { |
| 438 | if (!eth_current) |
| 439 | return -1; |
| 440 | |
| 441 | return eth_current->send(eth_current, packet, length); |
| 442 | } |
| 443 | |
| 444 | int eth_rx(void) |
| 445 | { |
| 446 | if (!eth_current) |
| 447 | return -1; |
| 448 | |
| 449 | return eth_current->recv(eth_current); |
| 450 | } |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 451 | |
| 452 | #ifdef CONFIG_API |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 453 | static void eth_save_packet(void *packet, int length) |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 454 | { |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 455 | char *p = packet; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 456 | int i; |
| 457 | |
| 458 | if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current) |
| 459 | return; |
| 460 | |
| 461 | if (PKTSIZE < length) |
| 462 | return; |
| 463 | |
| 464 | for (i = 0; i < length; i++) |
| 465 | eth_rcv_bufs[eth_rcv_last].data[i] = p[i]; |
| 466 | |
| 467 | eth_rcv_bufs[eth_rcv_last].length = length; |
| 468 | eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX; |
| 469 | } |
| 470 | |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 471 | int eth_receive(void *packet, int length) |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 472 | { |
Joe Hershberger | 4b7747e | 2012-05-15 08:59:04 +0000 | [diff] [blame] | 473 | char *p = packet; |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 474 | void *pp = push_packet; |
| 475 | int i; |
| 476 | |
| 477 | if (eth_rcv_current == eth_rcv_last) { |
| 478 | push_packet = eth_save_packet; |
| 479 | eth_rx(); |
| 480 | push_packet = pp; |
| 481 | |
| 482 | if (eth_rcv_current == eth_rcv_last) |
| 483 | return -1; |
| 484 | } |
| 485 | |
Michael Walle | df7c98a | 2012-06-22 11:24:28 +0000 | [diff] [blame] | 486 | length = min(eth_rcv_bufs[eth_rcv_current].length, length); |
Rafal Jaworowski | 1f2c9a4 | 2007-12-27 18:19:02 +0100 | [diff] [blame] | 487 | |
| 488 | for (i = 0; i < length; i++) |
| 489 | p[i] = eth_rcv_bufs[eth_rcv_current].data[i]; |
| 490 | |
| 491 | eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX; |
| 492 | return length; |
| 493 | } |
| 494 | #endif /* CONFIG_API */ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 495 | |
| 496 | void eth_try_another(int first_restart) |
| 497 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 498 | static struct eth_device *first_failed; |
Remy Bohmer | 0fce515 | 2011-02-19 20:15:14 +0100 | [diff] [blame] | 499 | char *ethrotate; |
Matthias Fuchs | 204f0ec | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 500 | |
| 501 | /* |
| 502 | * Do not rotate between network interfaces when |
| 503 | * 'ethrotate' variable is set to 'no'. |
| 504 | */ |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 505 | ethrotate = getenv("ethrotate"); |
| 506 | if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) |
Matthias Fuchs | 204f0ec | 2008-01-17 07:45:05 +0100 | [diff] [blame] | 507 | return; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 508 | |
| 509 | if (!eth_current) |
| 510 | return; |
| 511 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 512 | if (first_restart) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 513 | first_failed = eth_current; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 514 | |
| 515 | eth_current = eth_current->next; |
| 516 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 517 | eth_current_changed(); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 518 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 519 | if (first_failed == eth_current) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 520 | NetRestartWrap = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 521 | } |
| 522 | |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 523 | void eth_set_current(void) |
| 524 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 525 | static char *act; |
| 526 | static int env_changed_id; |
| 527 | struct eth_device *old_current; |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 528 | int env_id; |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 529 | |
| 530 | if (!eth_current) /* XXX no current */ |
| 531 | return; |
| 532 | |
Heiko Schocher | 0c303fa | 2009-02-10 09:38:52 +0100 | [diff] [blame] | 533 | env_id = get_env_id(); |
| 534 | if ((act == NULL) || (env_changed_id != env_id)) { |
| 535 | act = getenv("ethact"); |
| 536 | env_changed_id = env_id; |
| 537 | } |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 538 | if (act != NULL) { |
| 539 | old_current = eth_current; |
| 540 | do { |
| 541 | if (strcmp(eth_current->name, act) == 0) |
| 542 | return; |
| 543 | eth_current = eth_current->next; |
| 544 | } while (old_current != eth_current); |
| 545 | } |
| 546 | |
Simon Glass | 1e9961d | 2011-02-16 11:14:33 -0800 | [diff] [blame] | 547 | eth_current_changed(); |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 548 | } |
wdenk | 145d2c1 | 2004-04-15 21:48:45 +0000 | [diff] [blame] | 549 | |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 550 | char *eth_get_name(void) |
wdenk | c6abb7e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 551 | { |
Joe Hershberger | 5956ded | 2012-05-15 08:59:07 +0000 | [diff] [blame] | 552 | return eth_current ? eth_current->name : "unknown"; |
wdenk | c6abb7e | 2003-11-17 21:14:37 +0000 | [diff] [blame] | 553 | } |