blob: 4cef242ab8b76b9e6fae0aba0558b415397c9ab0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdb9391e2016-01-17 14:52:00 -07002/*
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
Peng Fand537b702017-05-11 10:21:48 +08006 * Copyright 2017 NXP
7 *
Simon Glassdb9391e2016-01-17 14:52:00 -07008 */
9
10#include <common.h>
11#include <dm.h>
12#include <environment.h>
13#include <net.h>
14#include <dm/device-internal.h>
15#include <dm/uclass-internal.h>
16#include "eth_internal.h"
17
Simon Glassa7c45ec2016-01-30 15:45:14 -070018DECLARE_GLOBAL_DATA_PTR;
19
Simon Glassdb9391e2016-01-17 14:52:00 -070020/**
21 * struct eth_device_priv - private structure for each Ethernet device
22 *
23 * @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
24 */
25struct eth_device_priv {
26 enum eth_state_t state;
27};
28
29/**
30 * struct eth_uclass_priv - The structure attached to the uclass itself
31 *
32 * @current: The Ethernet device that the network functions are using
33 */
34struct eth_uclass_priv {
35 struct udevice *current;
36};
37
38/* eth_errno - This stores the most recent failure code from DM functions */
39static int eth_errno;
40
41static struct eth_uclass_priv *eth_get_uclass_priv(void)
42{
43 struct uclass *uc;
Peng Fand537b702017-05-11 10:21:48 +080044 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -070045
Peng Fand537b702017-05-11 10:21:48 +080046 ret = uclass_get(UCLASS_ETH, &uc);
47 if (ret)
48 return NULL;
49
Simon Glassdb9391e2016-01-17 14:52:00 -070050 assert(uc);
51 return uc->priv;
52}
53
54void eth_set_current_to_next(void)
55{
56 struct eth_uclass_priv *uc_priv;
57
58 uc_priv = eth_get_uclass_priv();
59 if (uc_priv->current)
60 uclass_next_device(&uc_priv->current);
61 if (!uc_priv->current)
62 uclass_first_device(UCLASS_ETH, &uc_priv->current);
63}
64
65/*
66 * Typically this will simply return the active device.
67 * In the case where the most recent active device was unset, this will attempt
68 * to return the first device. If that device doesn't exist or fails to probe,
69 * this function will return NULL.
70 */
71struct udevice *eth_get_dev(void)
72{
73 struct eth_uclass_priv *uc_priv;
74
75 uc_priv = eth_get_uclass_priv();
76 if (!uc_priv->current)
77 eth_errno = uclass_first_device(UCLASS_ETH,
78 &uc_priv->current);
79 return uc_priv->current;
80}
81
82/*
83 * Typically this will just store a device pointer.
84 * In case it was not probed, we will attempt to do so.
85 * dev may be NULL to unset the active device.
86 */
87void eth_set_dev(struct udevice *dev)
88{
89 if (dev && !device_active(dev)) {
90 eth_errno = device_probe(dev);
91 if (eth_errno)
92 dev = NULL;
93 }
94
95 eth_get_uclass_priv()->current = dev;
96}
97
98/*
99 * Find the udevice that either has the name passed in as devname or has an
100 * alias named devname.
101 */
102struct udevice *eth_get_dev_by_name(const char *devname)
103{
104 int seq = -1;
105 char *endp = NULL;
106 const char *startp = NULL;
107 struct udevice *it;
108 struct uclass *uc;
109 int len = strlen("eth");
Peng Fand537b702017-05-11 10:21:48 +0800110 int ret;
Simon Glassdb9391e2016-01-17 14:52:00 -0700111
112 /* Must be longer than 3 to be an alias */
113 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
114 startp = devname + len;
115 seq = simple_strtoul(startp, &endp, 10);
116 }
117
Peng Fand537b702017-05-11 10:21:48 +0800118 ret = uclass_get(UCLASS_ETH, &uc);
119 if (ret)
120 return NULL;
121
Simon Glassdb9391e2016-01-17 14:52:00 -0700122 uclass_foreach_dev(it, uc) {
123 /*
124 * We need the seq to be valid, so try to probe it.
125 * If the probe fails, the seq will not match since it will be
126 * -1 instead of what we are looking for.
127 * We don't care about errors from probe here. Either they won't
128 * match an alias or it will match a literal name and we'll pick
129 * up the error when we try to probe again in eth_set_dev().
130 */
131 if (device_probe(it))
132 continue;
133 /* Check for the name or the sequence number to match */
134 if (strcmp(it->name, devname) == 0 ||
135 (endp > startp && it->seq == seq))
136 return it;
137 }
138
139 return NULL;
140}
141
142unsigned char *eth_get_ethaddr(void)
143{
144 struct eth_pdata *pdata;
145
146 if (eth_get_dev()) {
147 pdata = eth_get_dev()->platdata;
148 return pdata->enetaddr;
149 }
150
151 return NULL;
152}
153
154/* Set active state without calling start on the driver */
155int eth_init_state_only(void)
156{
157 struct udevice *current;
158 struct eth_device_priv *priv;
159
160 current = eth_get_dev();
161 if (!current || !device_active(current))
162 return -EINVAL;
163
164 priv = current->uclass_priv;
165 priv->state = ETH_STATE_ACTIVE;
166
167 return 0;
168}
169
170/* Set passive state without calling stop on the driver */
171void eth_halt_state_only(void)
172{
173 struct udevice *current;
174 struct eth_device_priv *priv;
175
176 current = eth_get_dev();
177 if (!current || !device_active(current))
178 return;
179
180 priv = current->uclass_priv;
181 priv->state = ETH_STATE_PASSIVE;
182}
183
184int eth_get_dev_index(void)
185{
186 if (eth_get_dev())
187 return eth_get_dev()->seq;
188 return -1;
189}
190
191static int eth_write_hwaddr(struct udevice *dev)
192{
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200193 struct eth_pdata *pdata;
Simon Glassdb9391e2016-01-17 14:52:00 -0700194 int ret = 0;
195
196 if (!dev || !device_active(dev))
197 return -EINVAL;
198
199 /* seq is valid since the device is active */
200 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
xypron.glpk@gmx.dec08248d2017-05-16 05:07:01 +0200201 pdata = dev->platdata;
Simon Glassdb9391e2016-01-17 14:52:00 -0700202 if (!is_valid_ethaddr(pdata->enetaddr)) {
203 printf("\nError: %s address %pM illegal value\n",
204 dev->name, pdata->enetaddr);
205 return -EINVAL;
206 }
207
208 /*
209 * Drivers are allowed to decide not to implement this at
210 * run-time. E.g. Some devices may use it and some may not.
211 */
212 ret = eth_get_ops(dev)->write_hwaddr(dev);
213 if (ret == -ENOSYS)
214 ret = 0;
215 if (ret)
216 printf("\nWarning: %s failed to set MAC address\n",
217 dev->name);
218 }
219
220 return ret;
221}
222
223static int on_ethaddr(const char *name, const char *value, enum env_op op,
224 int flags)
225{
226 int index;
227 int retval;
228 struct udevice *dev;
229
230 /* look for an index after "eth" */
231 index = simple_strtoul(name + 3, NULL, 10);
232
233 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
234 if (!retval) {
235 struct eth_pdata *pdata = dev->platdata;
236 switch (op) {
237 case env_op_create:
238 case env_op_overwrite:
239 eth_parse_enetaddr(value, pdata->enetaddr);
Hannes Schmelzerc86ff7f2016-09-02 14:48:17 +0200240 eth_write_hwaddr(dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700241 break;
242 case env_op_delete:
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100243 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700244 }
245 }
246
247 return 0;
248}
249U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
250
251int eth_init(void)
252{
Simon Glass00caae62017-08-03 12:22:12 -0600253 char *ethact = env_get("ethact");
254 char *ethrotate = env_get("ethrotate");
Simon Glassdb9391e2016-01-17 14:52:00 -0700255 struct udevice *current = NULL;
256 struct udevice *old_current;
257 int ret = -ENODEV;
258
259 /*
260 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
261 * is already set to an ethernet device, we should stick to 'ethact'.
262 */
263 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
264 if (ethact) {
265 current = eth_get_dev_by_name(ethact);
266 if (!current)
267 return -EINVAL;
268 }
269 }
270
271 if (!current) {
272 current = eth_get_dev();
273 if (!current) {
274 printf("No ethernet found.\n");
275 return -ENODEV;
276 }
277 }
278
279 old_current = current;
280 do {
281 if (current) {
282 debug("Trying %s\n", current->name);
283
284 if (device_active(current)) {
285 ret = eth_get_ops(current)->start(current);
286 if (ret >= 0) {
287 struct eth_device_priv *priv =
288 current->uclass_priv;
289
290 priv->state = ETH_STATE_ACTIVE;
291 return 0;
292 }
293 } else {
294 ret = eth_errno;
295 }
296
297 debug("FAIL\n");
298 } else {
299 debug("PROBE FAIL\n");
300 }
301
302 /*
303 * If ethrotate is enabled, this will change "current",
304 * otherwise we will drop out of this while loop immediately
305 */
306 eth_try_another(0);
307 /* This will ensure the new "current" attempted to probe */
308 current = eth_get_dev();
309 } while (old_current != current);
310
311 return ret;
312}
313
314void eth_halt(void)
315{
316 struct udevice *current;
317 struct eth_device_priv *priv;
318
319 current = eth_get_dev();
Joe Hershberger68acb512018-07-02 14:47:46 -0500320 if (!current || !eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700321 return;
322
323 eth_get_ops(current)->stop(current);
324 priv = current->uclass_priv;
Jean-Jacques Hiblotc3211702018-08-09 16:17:41 +0200325 if (priv)
326 priv->state = ETH_STATE_PASSIVE;
Simon Glassdb9391e2016-01-17 14:52:00 -0700327}
328
329int eth_is_active(struct udevice *dev)
330{
331 struct eth_device_priv *priv;
332
333 if (!dev || !device_active(dev))
334 return 0;
335
336 priv = dev_get_uclass_priv(dev);
337 return priv->state == ETH_STATE_ACTIVE;
338}
339
340int eth_send(void *packet, int length)
341{
342 struct udevice *current;
343 int ret;
344
345 current = eth_get_dev();
346 if (!current)
347 return -ENODEV;
348
Alexander Grafa532e2f2018-03-15 15:07:09 +0100349 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700350 return -EINVAL;
351
352 ret = eth_get_ops(current)->send(current, packet, length);
353 if (ret < 0) {
354 /* We cannot completely return the error at present */
355 debug("%s: send() returned error %d\n", __func__, ret);
356 }
357 return ret;
358}
359
360int eth_rx(void)
361{
362 struct udevice *current;
363 uchar *packet;
364 int flags;
365 int ret;
366 int i;
367
368 current = eth_get_dev();
369 if (!current)
370 return -ENODEV;
371
Alexander Grafa532e2f2018-03-15 15:07:09 +0100372 if (!eth_is_active(current))
Simon Glassdb9391e2016-01-17 14:52:00 -0700373 return -EINVAL;
374
375 /* Process up to 32 packets at one time */
376 flags = ETH_RECV_CHECK_DEVICE;
377 for (i = 0; i < 32; i++) {
378 ret = eth_get_ops(current)->recv(current, flags, &packet);
379 flags = 0;
380 if (ret > 0)
381 net_process_received_packet(packet, ret);
382 if (ret >= 0 && eth_get_ops(current)->free_pkt)
383 eth_get_ops(current)->free_pkt(current, packet, ret);
384 if (ret <= 0)
385 break;
386 }
387 if (ret == -EAGAIN)
388 ret = 0;
389 if (ret < 0) {
390 /* We cannot completely return the error at present */
391 debug("%s: recv() returned error %d\n", __func__, ret);
392 }
393 return ret;
394}
395
396int eth_initialize(void)
397{
398 int num_devices = 0;
399 struct udevice *dev;
400
401 eth_common_init();
402
403 /*
404 * Devices need to write the hwaddr even if not started so that Linux
405 * will have access to the hwaddr that u-boot stored for the device.
406 * This is accomplished by attempting to probe each device and calling
407 * their write_hwaddr() operation.
408 */
Mario Six3ce43042018-04-27 14:52:56 +0200409 uclass_first_device_check(UCLASS_ETH, &dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700410 if (!dev) {
411 printf("No ethernet found.\n");
412 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
413 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600414 char *ethprime = env_get("ethprime");
Simon Glassdb9391e2016-01-17 14:52:00 -0700415 struct udevice *prime_dev = NULL;
416
417 if (ethprime)
418 prime_dev = eth_get_dev_by_name(ethprime);
419 if (prime_dev) {
420 eth_set_dev(prime_dev);
421 eth_current_changed();
422 } else {
423 eth_set_dev(NULL);
424 }
425
426 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
427 do {
428 if (num_devices)
429 printf(", ");
430
431 printf("eth%d: %s", dev->seq, dev->name);
432
433 if (ethprime && dev == prime_dev)
434 printf(" [PRIME]");
435
436 eth_write_hwaddr(dev);
437
Mario Six3ce43042018-04-27 14:52:56 +0200438 uclass_next_device_check(&dev);
Simon Glassdb9391e2016-01-17 14:52:00 -0700439 num_devices++;
440 } while (dev);
441
442 putc('\n');
443 }
444
445 return num_devices;
446}
447
448static int eth_post_bind(struct udevice *dev)
449{
450 if (strchr(dev->name, ' ')) {
451 printf("\nError: eth device name \"%s\" has a space!\n",
452 dev->name);
453 return -EINVAL;
454 }
455
456 return 0;
457}
458
459static int eth_pre_unbind(struct udevice *dev)
460{
461 /* Don't hang onto a pointer that is going away */
462 if (dev == eth_get_uclass_priv()->current)
463 eth_set_dev(NULL);
464
465 return 0;
466}
467
468static int eth_post_probe(struct udevice *dev)
469{
470 struct eth_device_priv *priv = dev->uclass_priv;
471 struct eth_pdata *pdata = dev->platdata;
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100472 unsigned char env_enetaddr[ARP_HLEN];
Simon Glassdb9391e2016-01-17 14:52:00 -0700473
474#if defined(CONFIG_NEEDS_MANUAL_RELOC)
475 struct eth_ops *ops = eth_get_ops(dev);
476 static int reloc_done;
477
478 if (!reloc_done) {
479 if (ops->start)
480 ops->start += gd->reloc_off;
481 if (ops->send)
482 ops->send += gd->reloc_off;
483 if (ops->recv)
484 ops->recv += gd->reloc_off;
485 if (ops->free_pkt)
486 ops->free_pkt += gd->reloc_off;
487 if (ops->stop)
488 ops->stop += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700489 if (ops->mcast)
490 ops->mcast += gd->reloc_off;
Simon Glassdb9391e2016-01-17 14:52:00 -0700491 if (ops->write_hwaddr)
492 ops->write_hwaddr += gd->reloc_off;
493 if (ops->read_rom_hwaddr)
494 ops->read_rom_hwaddr += gd->reloc_off;
495
496 reloc_done++;
497 }
498#endif
499
500 priv->state = ETH_STATE_INIT;
501
502 /* Check if the device has a MAC address in ROM */
503 if (eth_get_ops(dev)->read_rom_hwaddr)
504 eth_get_ops(dev)->read_rom_hwaddr(dev);
505
Simon Glass35affd72017-08-03 12:22:14 -0600506 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700507 if (!is_zero_ethaddr(env_enetaddr)) {
508 if (!is_zero_ethaddr(pdata->enetaddr) &&
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100509 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700510 printf("\nWarning: %s MAC addresses don't match:\n",
511 dev->name);
oliver@schinagl.nl26d40b02016-11-25 16:30:23 +0100512 printf("Address in ROM is %pM\n",
Simon Glassdb9391e2016-01-17 14:52:00 -0700513 pdata->enetaddr);
514 printf("Address in environment is %pM\n",
515 env_enetaddr);
516 }
517
518 /* Override the ROM MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100519 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700520 } else if (is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassfd1e9592017-08-03 12:22:11 -0600521 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
Simon Glassdb9391e2016-01-17 14:52:00 -0700522 printf("\nWarning: %s using MAC address from ROM\n",
523 dev->name);
Siva Durga Prasad Paladuguaa555fe2016-11-02 12:52:13 +0100524 } else if (is_zero_ethaddr(pdata->enetaddr) ||
525 !is_valid_ethaddr(pdata->enetaddr)) {
Simon Glassdb9391e2016-01-17 14:52:00 -0700526#ifdef CONFIG_NET_RANDOM_ETHADDR
527 net_random_ethaddr(pdata->enetaddr);
528 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
529 dev->name, dev->seq, pdata->enetaddr);
530#else
531 printf("\nError: %s address not set.\n",
532 dev->name);
533 return -EINVAL;
534#endif
535 }
536
537 return 0;
538}
539
540static int eth_pre_remove(struct udevice *dev)
541{
542 struct eth_pdata *pdata = dev->platdata;
543
544 eth_get_ops(dev)->stop(dev);
545
546 /* clear the MAC address */
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100547 memset(pdata->enetaddr, 0, ARP_HLEN);
Simon Glassdb9391e2016-01-17 14:52:00 -0700548
549 return 0;
550}
551
552UCLASS_DRIVER(eth) = {
553 .name = "eth",
554 .id = UCLASS_ETH,
555 .post_bind = eth_post_bind,
556 .pre_unbind = eth_pre_unbind,
557 .post_probe = eth_post_probe,
558 .pre_remove = eth_pre_remove,
559 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
560 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
561 .flags = DM_UC_FLAG_SEQ_ALIAS,
562};