blob: e250a430f333aa66d1774346377efdc93b908d3f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00002/*
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05003 * (C) Copyright 2001-2015
wdenkc6097192002-11-03 00:24:07 +00004 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Joe Hershbergerc7eceaf2015-03-22 17:09:10 -05005 * Joe Hershberger, National Instruments
wdenkc6097192002-11-03 00:24:07 +00006 */
7
8#include <common.h>
9#include <command.h>
Joe Hershberger7e0b0152015-05-20 14:27:26 -050010#include <environment.h>
wdenkc6097192002-11-03 00:24:07 +000011#include <net.h>
Andy Flemingaecf6fc2011-04-08 02:10:27 -050012#include <phy.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090013#include <linux/errno.h>
Simon Glass0a9bde12016-01-17 14:51:57 -070014#include "eth_internal.h"
wdenkc6097192002-11-03 00:24:07 +000015
Joe Hershberger3dbe17e2015-03-22 17:09:06 -050016DECLARE_GLOBAL_DATA_PTR;
17
Simon Glassb6121f22015-04-05 16:07:37 -060018/*
19 * CPU and board-specific Ethernet initializations. Aliased function
20 * signals caller to move on
21 */
22static int __def_eth_init(bd_t *bis)
23{
24 return -1;
25}
26int cpu_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
27int board_eth_init(bd_t *bis) __attribute__((weak, alias("__def_eth_init")));
28
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010029#ifdef CONFIG_API
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010030static struct {
31 uchar data[PKTSIZE];
32 int length;
33} eth_rcv_bufs[PKTBUFSRX];
34
Joe Hershberger5956ded2012-05-15 08:59:07 +000035static unsigned int eth_rcv_current, eth_rcv_last;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +010036#endif
37
Joe Hershberger9f374062012-08-03 10:59:08 +000038static struct eth_device *eth_devices;
39struct eth_device *eth_current;
wdenkc6097192002-11-03 00:24:07 +000040
Simon Glassd9582c32016-01-17 14:51:59 -070041void eth_set_current_to_next(void)
Joe Hershbergere1e8a8c2015-03-22 17:09:03 -050042{
43 eth_current = eth_current->next;
44}
45
Simon Glassd9582c32016-01-17 14:51:59 -070046void eth_set_dev(struct eth_device *dev)
Joe Hershberger279d2f62015-03-22 17:09:16 -050047{
48 eth_current = dev;
49}
50
Ben Warren97824d62010-07-29 12:56:11 -070051struct eth_device *eth_get_dev_by_name(const char *devname)
Marian Balakowiczaab8c492005-10-28 22:30:33 +020052{
53 struct eth_device *dev, *target_dev;
54
Helmut Raiger153bf3a2011-08-22 00:17:17 +000055 BUG_ON(devname == NULL);
56
Marian Balakowiczaab8c492005-10-28 22:30:33 +020057 if (!eth_devices)
58 return NULL;
59
60 dev = eth_devices;
61 target_dev = NULL;
62 do {
63 if (strcmp(devname, dev->name) == 0) {
64 target_dev = dev;
65 break;
66 }
67 dev = dev->next;
68 } while (dev != eth_devices);
69
70 return target_dev;
71}
72
Andy Fleminge7fd34b2009-02-11 15:07:24 -060073struct eth_device *eth_get_dev_by_index(int index)
74{
75 struct eth_device *dev, *target_dev;
Andy Fleminge7fd34b2009-02-11 15:07:24 -060076
77 if (!eth_devices)
78 return NULL;
79
80 dev = eth_devices;
81 target_dev = NULL;
82 do {
Michael Walle391da4c2011-10-27 11:31:35 +000083 if (dev->index == index) {
Andy Fleminge7fd34b2009-02-11 15:07:24 -060084 target_dev = dev;
85 break;
86 }
87 dev = dev->next;
Andy Fleminge7fd34b2009-02-11 15:07:24 -060088 } while (dev != eth_devices);
89
90 return target_dev;
91}
92
Joe Hershberger5956ded2012-05-15 08:59:07 +000093int eth_get_dev_index(void)
wdenkc6097192002-11-03 00:24:07 +000094{
Joe Hershberger5956ded2012-05-15 08:59:07 +000095 if (!eth_current)
Michael Walle391da4c2011-10-27 11:31:35 +000096 return -1;
wdenkc6097192002-11-03 00:24:07 +000097
Michael Walle391da4c2011-10-27 11:31:35 +000098 return eth_current->index;
wdenkc6097192002-11-03 00:24:07 +000099}
100
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500101static int on_ethaddr(const char *name, const char *value, enum env_op op,
102 int flags)
103{
104 int index;
105 struct eth_device *dev;
106
107 if (!eth_devices)
108 return 0;
109
110 /* look for an index after "eth" */
111 index = simple_strtoul(name + 3, NULL, 10);
112
113 dev = eth_devices;
114 do {
115 if (dev->index == index) {
116 switch (op) {
117 case env_op_create:
118 case env_op_overwrite:
119 eth_parse_enetaddr(value, dev->enetaddr);
Marek Vasut0ef21482016-11-12 16:28:40 +0100120 eth_write_hwaddr(dev, "eth", dev->index);
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500121 break;
122 case env_op_delete:
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100123 memset(dev->enetaddr, 0, ARP_HLEN);
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500124 }
125 }
Gong Qianyua4f23972015-08-31 11:34:43 +0800126 dev = dev->next;
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500127 } while (dev != eth_devices);
128
129 return 0;
130}
131U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
132
Simon Glass62b36c92011-06-13 16:13:10 -0700133int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
134 int eth_number)
135{
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100136 unsigned char env_enetaddr[ARP_HLEN];
Simon Glass62b36c92011-06-13 16:13:10 -0700137 int ret = 0;
138
Simon Glass399a9ce2017-08-03 12:22:14 -0600139 eth_env_get_enetaddr_by_index(base_name, eth_number, env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700140
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500141 if (!is_zero_ethaddr(env_enetaddr)) {
142 if (!is_zero_ethaddr(dev->enetaddr) &&
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100143 memcmp(dev->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glass62b36c92011-06-13 16:13:10 -0700144 printf("\nWarning: %s MAC addresses don't match:\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500145 dev->name);
Simon Glass62b36c92011-06-13 16:13:10 -0700146 printf("Address in SROM is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500147 dev->enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700148 printf("Address in environment is %pM\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500149 env_enetaddr);
Simon Glass62b36c92011-06-13 16:13:10 -0700150 }
151
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100152 memcpy(dev->enetaddr, env_enetaddr, ARP_HLEN);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500153 } else if (is_valid_ethaddr(dev->enetaddr)) {
Simon Glass8551d552017-08-03 12:22:11 -0600154 eth_env_set_enetaddr_by_index(base_name, eth_number,
155 dev->enetaddr);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500156 } else if (is_zero_ethaddr(dev->enetaddr)) {
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500157#ifdef CONFIG_NET_RANDOM_ETHADDR
158 net_random_ethaddr(dev->enetaddr);
159 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
160 dev->name, eth_number, dev->enetaddr);
161#else
Pavel Machek222981f2014-07-13 10:27:02 +0200162 printf("\nError: %s address not set.\n",
163 dev->name);
164 return -EINVAL;
Joe Hershberger2dc2b5d2015-05-04 14:55:13 -0500165#endif
Simon Glass62b36c92011-06-13 16:13:10 -0700166 }
167
Pavel Machek222981f2014-07-13 10:27:02 +0200168 if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500169 if (!is_valid_ethaddr(dev->enetaddr)) {
Pavel Machek222981f2014-07-13 10:27:02 +0200170 printf("\nError: %s address %pM illegal value\n",
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500171 dev->name, dev->enetaddr);
Pavel Machek222981f2014-07-13 10:27:02 +0200172 return -EINVAL;
173 }
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000174
Simon Glass62b36c92011-06-13 16:13:10 -0700175 ret = dev->write_hwaddr(dev);
Pavel Machek222981f2014-07-13 10:27:02 +0200176 if (ret)
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500177 printf("\nWarning: %s failed to set MAC address\n",
178 dev->name);
Benoît Thébaudeau050219e2012-08-10 07:56:21 +0000179 }
Simon Glass62b36c92011-06-13 16:13:10 -0700180
181 return ret;
182}
183
Simon Glass1e9961d2011-02-16 11:14:33 -0800184int eth_register(struct eth_device *dev)
185{
186 struct eth_device *d;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000187 static int index;
Michal Simek5b4945b2011-08-29 23:30:13 +0000188
Mike Frysinger6b300dc2011-11-10 14:11:04 +0000189 assert(strlen(dev->name) < sizeof(dev->name));
Michal Simek5b4945b2011-08-29 23:30:13 +0000190
Simon Glass1e9961d2011-02-16 11:14:33 -0800191 if (!eth_devices) {
Joe Hershbergerc10c0b12015-04-08 01:41:19 -0500192 eth_devices = dev;
193 eth_current = dev;
Simon Glass1e9961d2011-02-16 11:14:33 -0800194 eth_current_changed();
wdenkc6097192002-11-03 00:24:07 +0000195 } else {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000196 for (d = eth_devices; d->next != eth_devices; d = d->next)
Detlev Zundeld46bcd32010-03-31 17:56:08 +0200197 ;
wdenkc6097192002-11-03 00:24:07 +0000198 d->next = dev;
199 }
200
201 dev->state = ETH_STATE_INIT;
202 dev->next = eth_devices;
Michael Walle391da4c2011-10-27 11:31:35 +0000203 dev->index = index++;
wdenkc6097192002-11-03 00:24:07 +0000204
205 return 0;
206}
207
Vincent Palatin451be502012-01-09 08:32:36 +0000208int eth_unregister(struct eth_device *dev)
209{
210 struct eth_device *cur;
211
212 /* No device */
213 if (!eth_devices)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500214 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000215
216 for (cur = eth_devices; cur->next != eth_devices && cur->next != dev;
217 cur = cur->next)
218 ;
219
220 /* Device not found */
221 if (cur->next != dev)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500222 return -ENODEV;
Vincent Palatin451be502012-01-09 08:32:36 +0000223
224 cur->next = dev->next;
225
226 if (eth_devices == dev)
227 eth_devices = dev->next == eth_devices ? NULL : dev->next;
228
229 if (eth_current == dev) {
230 eth_current = eth_devices;
231 eth_current_changed();
232 }
233
234 return 0;
235}
236
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500237int eth_initialize(void)
wdenkc6097192002-11-03 00:24:07 +0000238{
Michael Walle391da4c2011-10-27 11:31:35 +0000239 int num_devices = 0;
Simon Glassb6121f22015-04-05 16:07:37 -0600240
wdenkc6097192002-11-03 00:24:07 +0000241 eth_devices = NULL;
242 eth_current = NULL;
Simon Glassb6121f22015-04-05 16:07:37 -0600243 eth_common_init();
Simon Glass0a9bde12016-01-17 14:51:57 -0700244 /*
245 * If board-specific initialization exists, call it.
246 * If not, call a CPU-specific one
247 */
248 if (board_eth_init != __def_eth_init) {
249 if (board_eth_init(gd->bd) < 0)
250 printf("Board Net Initialization Failed\n");
251 } else if (cpu_eth_init != __def_eth_init) {
252 if (cpu_eth_init(gd->bd) < 0)
253 printf("CPU Net Initialization Failed\n");
254 } else {
255 printf("Net Initialization Skipped\n");
256 }
Marian Balakowiczcbdd1c82005-11-30 18:06:04 +0100257
wdenkc6097192002-11-03 00:24:07 +0000258 if (!eth_devices) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000259 puts("No ethernet found.\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000260 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
wdenkc6097192002-11-03 00:24:07 +0000261 } else {
262 struct eth_device *dev = eth_devices;
Simon Glass64b723f2017-08-03 12:22:12 -0600263 char *ethprime = env_get("ethprime");
wdenkc6097192002-11-03 00:24:07 +0000264
Simon Glass0169e6b2012-02-13 13:51:18 +0000265 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
wdenkc6097192002-11-03 00:24:07 +0000266 do {
Michael Walle391da4c2011-10-27 11:31:35 +0000267 if (dev->index)
Joe Hershberger5956ded2012-05-15 08:59:07 +0000268 puts(", ");
wdenkc6097192002-11-03 00:24:07 +0000269
270 printf("%s", dev->name);
271
Joe Hershberger5956ded2012-05-15 08:59:07 +0000272 if (ethprime && strcmp(dev->name, ethprime) == 0) {
wdenkc6097192002-11-03 00:24:07 +0000273 eth_current = dev;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000274 puts(" [PRIME]");
wdenkc6097192002-11-03 00:24:07 +0000275 }
276
Mike Frysinger2843ed92010-06-09 22:10:48 -0400277 if (strchr(dev->name, ' '))
Joe Hershberger5956ded2012-05-15 08:59:07 +0000278 puts("\nWarning: eth device name has a space!"
279 "\n");
Mike Frysinger2843ed92010-06-09 22:10:48 -0400280
Pavel Machek222981f2014-07-13 10:27:02 +0200281 eth_write_hwaddr(dev, "eth", dev->index);
wdenkc6097192002-11-03 00:24:07 +0000282
wdenkc6097192002-11-03 00:24:07 +0000283 dev = dev->next;
Michael Walle391da4c2011-10-27 11:31:35 +0000284 num_devices++;
Joe Hershberger5956ded2012-05-15 08:59:07 +0000285 } while (dev != eth_devices);
wdenkc6097192002-11-03 00:24:07 +0000286
Simon Glass1e9961d2011-02-16 11:14:33 -0800287 eth_current_changed();
Joe Hershberger5956ded2012-05-15 08:59:07 +0000288 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000289 }
290
Michael Walle391da4c2011-10-27 11:31:35 +0000291 return num_devices;
wdenkc6097192002-11-03 00:24:07 +0000292}
293
David Updegraff7280da72007-06-11 10:41:07 -0500294/* Multicast.
295 * mcast_addr: multicast ipaddr from which multicast Mac is made
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200296 * join: 1=join, 0=leave.
David Updegraff7280da72007-06-11 10:41:07 -0500297 */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500298int eth_mcast_join(struct in_addr mcast_ip, int join)
David Updegraff7280da72007-06-11 10:41:07 -0500299{
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100300 u8 mcast_mac[ARP_HLEN];
Wolfgang Denk627f5c32007-08-14 09:47:27 +0200301 if (!eth_current || !eth_current->mcast)
David Updegraff7280da72007-06-11 10:41:07 -0500302 return -1;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500303 mcast_mac[5] = htonl(mcast_ip.s_addr) & 0xff;
304 mcast_mac[4] = (htonl(mcast_ip.s_addr)>>8) & 0xff;
305 mcast_mac[3] = (htonl(mcast_ip.s_addr)>>16) & 0x7f;
David Updegraff7280da72007-06-11 10:41:07 -0500306 mcast_mac[2] = 0x5e;
307 mcast_mac[1] = 0x0;
308 mcast_mac[0] = 0x1;
309 return eth_current->mcast(eth_current, mcast_mac, join);
310}
311
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500312int eth_init(void)
wdenkc6097192002-11-03 00:24:07 +0000313{
Joe Hershberger7e0b0152015-05-20 14:27:26 -0500314 struct eth_device *old_current;
wdenkc6097192002-11-03 00:24:07 +0000315
Stefan Roese11da2be2008-03-04 17:40:41 +0100316 if (!eth_current) {
Joe Hershberger5956ded2012-05-15 08:59:07 +0000317 puts("No ethernet found.\n");
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500318 return -ENODEV;
Stefan Roese11da2be2008-03-04 17:40:41 +0100319 }
wdenkc6097192002-11-03 00:24:07 +0000320
321 old_current = eth_current;
322 do {
Robin Getz9e0a4d62009-07-23 03:01:03 -0400323 debug("Trying %s\n", eth_current->name);
wdenkc6097192002-11-03 00:24:07 +0000324
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500325 if (eth_current->init(eth_current, gd->bd) >= 0) {
wdenkc6097192002-11-03 00:24:07 +0000326 eth_current->state = ETH_STATE_ACTIVE;
327
Upakul Barkakatyaed33582007-11-29 12:16:13 +0530328 return 0;
wdenkc6097192002-11-03 00:24:07 +0000329 }
Robin Getz9e0a4d62009-07-23 03:01:03 -0400330 debug("FAIL\n");
wdenkc6097192002-11-03 00:24:07 +0000331
332 eth_try_another(0);
333 } while (old_current != eth_current);
334
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500335 return -ETIMEDOUT;
wdenkc6097192002-11-03 00:24:07 +0000336}
337
338void eth_halt(void)
339{
340 if (!eth_current)
341 return;
342
343 eth_current->halt(eth_current);
344
345 eth_current->state = ETH_STATE_PASSIVE;
346}
347
Bernhard Nortmann8de9d122015-09-14 15:29:43 +0200348int eth_is_active(struct eth_device *dev)
349{
350 return dev && dev->state == ETH_STATE_ACTIVE;
351}
352
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000353int eth_send(void *packet, int length)
wdenkc6097192002-11-03 00:24:07 +0000354{
355 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500356 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000357
358 return eth_current->send(eth_current, packet, length);
359}
360
361int eth_rx(void)
362{
363 if (!eth_current)
Joe Hershbergerd0ce3412015-03-22 17:09:04 -0500364 return -ENODEV;
wdenkc6097192002-11-03 00:24:07 +0000365
366 return eth_current->recv(eth_current);
367}
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100368
369#ifdef CONFIG_API
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000370static void eth_save_packet(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100371{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000372 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100373 int i;
374
375 if ((eth_rcv_last+1) % PKTBUFSRX == eth_rcv_current)
376 return;
377
378 if (PKTSIZE < length)
379 return;
380
381 for (i = 0; i < length; i++)
382 eth_rcv_bufs[eth_rcv_last].data[i] = p[i];
383
384 eth_rcv_bufs[eth_rcv_last].length = length;
385 eth_rcv_last = (eth_rcv_last + 1) % PKTBUFSRX;
386}
387
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000388int eth_receive(void *packet, int length)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100389{
Joe Hershberger4b7747e2012-05-15 08:59:04 +0000390 char *p = packet;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100391 void *pp = push_packet;
392 int i;
393
394 if (eth_rcv_current == eth_rcv_last) {
395 push_packet = eth_save_packet;
396 eth_rx();
397 push_packet = pp;
398
399 if (eth_rcv_current == eth_rcv_last)
400 return -1;
401 }
402
Michael Walledf7c98a2012-06-22 11:24:28 +0000403 length = min(eth_rcv_bufs[eth_rcv_current].length, length);
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100404
405 for (i = 0; i < length; i++)
406 p[i] = eth_rcv_bufs[eth_rcv_current].data[i];
407
408 eth_rcv_current = (eth_rcv_current + 1) % PKTBUFSRX;
409 return length;
410}
411#endif /* CONFIG_API */