blob: b473f59269bed9789b4bb613ea44fa350cc258dc [file] [log] [blame]
Fuchsia firmware team46a8da22021-12-15 10:56:53 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2001-2015
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Joe Hershberger, National Instruments
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <environment.h>
11#include <net.h>
12#include <dm/device-internal.h>
13#include <dm/uclass-internal.h>
14#include "eth_internal.h"
15#include <amlogic/keyunify.h>
16#include <asm/arch/cpu_id.h>
17
18DECLARE_GLOBAL_DATA_PTR;
19
20/**
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;
44
45 uclass_get(UCLASS_ETH, &uc);
46 assert(uc);
47 return uc->priv;
48}
49
50void eth_set_current_to_next(void)
51{
52 struct eth_uclass_priv *uc_priv;
53
54 uc_priv = eth_get_uclass_priv();
55 if (uc_priv->current)
56 uclass_next_device(&uc_priv->current);
57 if (!uc_priv->current)
58 uclass_first_device(UCLASS_ETH, &uc_priv->current);
59}
60
61/*
62 * Typically this will simply return the active device.
63 * In the case where the most recent active device was unset, this will attempt
64 * to return the first device. If that device doesn't exist or fails to probe,
65 * this function will return NULL.
66 */
67struct udevice *eth_get_dev(void)
68{
69 struct eth_uclass_priv *uc_priv;
70
71 uc_priv = eth_get_uclass_priv();
72 if (!uc_priv->current)
73 eth_errno = uclass_first_device(UCLASS_ETH,
74 &uc_priv->current);
75 return uc_priv->current;
76}
77
78/*
79 * Typically this will just store a device pointer.
80 * In case it was not probed, we will attempt to do so.
81 * dev may be NULL to unset the active device.
82 */
83void eth_set_dev(struct udevice *dev)
84{
85 if (dev && !device_active(dev)) {
86 eth_errno = device_probe(dev);
87 if (eth_errno)
88 dev = NULL;
89 }
90
91 eth_get_uclass_priv()->current = dev;
92}
93
94/*
95 * Find the udevice that either has the name passed in as devname or has an
96 * alias named devname.
97 */
98struct udevice *eth_get_dev_by_name(const char *devname)
99{
100 int seq = -1;
101 char *endp = NULL;
102 const char *startp = NULL;
103 struct udevice *it;
104 struct uclass *uc;
105 int len = strlen("eth");
106
107 /* Must be longer than 3 to be an alias */
108 if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
109 startp = devname + len;
110 seq = simple_strtoul(startp, &endp, 10);
111 }
112
113 uclass_get(UCLASS_ETH, &uc);
114 uclass_foreach_dev(it, uc) {
115 /*
116 * We need the seq to be valid, so try to probe it.
117 * If the probe fails, the seq will not match since it will be
118 * -1 instead of what we are looking for.
119 * We don't care about errors from probe here. Either they won't
120 * match an alias or it will match a literal name and we'll pick
121 * up the error when we try to probe again in eth_set_dev().
122 */
123 if (device_probe(it))
124 continue;
125 /* Check for the name or the sequence number to match */
126 if (strcmp(it->name, devname) == 0 ||
127 (endp > startp && it->seq == seq))
128 return it;
129 }
130
131 return NULL;
132}
133
134unsigned char *eth_get_ethaddr(void)
135{
136 struct eth_pdata *pdata;
137
138 if (eth_get_dev()) {
139 pdata = eth_get_dev()->platdata;
140 return pdata->enetaddr;
141 }
142
143 return NULL;
144}
145
146/* Set active state without calling start on the driver */
147int eth_init_state_only(void)
148{
149 struct udevice *current;
150 struct eth_device_priv *priv;
151
152 current = eth_get_dev();
153 if (!current || !device_active(current))
154 return -EINVAL;
155
156 priv = current->uclass_priv;
157 priv->state = ETH_STATE_ACTIVE;
158
159 return 0;
160}
161
162/* Set passive state without calling stop on the driver */
163void eth_halt_state_only(void)
164{
165 struct udevice *current;
166 struct eth_device_priv *priv;
167
168 current = eth_get_dev();
169 if (!current || !device_active(current))
170 return;
171
172 priv = current->uclass_priv;
173 priv->state = ETH_STATE_PASSIVE;
174}
175
176int eth_get_dev_index(void)
177{
178 if (eth_get_dev())
179 return eth_get_dev()->seq;
180 return -1;
181}
182
183static int eth_write_hwaddr(struct udevice *dev)
184{
185 struct eth_pdata *pdata;
186 int ret = 0;
187
188 if (!dev || !device_active(dev))
189 return -EINVAL;
190
191 /* seq is valid since the device is active */
192 if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev->seq)) {
193 pdata = dev->platdata;
194 if (!is_valid_ethaddr(pdata->enetaddr)) {
195 printf("\nError: %s address %pM illegal value\n",
196 dev->name, pdata->enetaddr);
197 return -EINVAL;
198 }
199
200 /*
201 * Drivers are allowed to decide not to implement this at
202 * run-time. E.g. Some devices may use it and some may not.
203 */
204 ret = eth_get_ops(dev)->write_hwaddr(dev);
205 if (ret == -ENOSYS)
206 ret = 0;
207 if (ret)
208 printf("\nWarning: %s failed to set MAC address\n",
209 dev->name);
210 }
211
212 return ret;
213}
214
215static int on_ethaddr(const char *name, const char *value, enum env_op op,
216 int flags)
217{
218 int index;
219 int retval;
220 struct udevice *dev;
221
222 /* look for an index after "eth" */
223 index = simple_strtoul(name + 3, NULL, 10);
224
225 retval = uclass_find_device_by_seq(UCLASS_ETH, index, false, &dev);
226 if (!retval) {
227 struct eth_pdata *pdata = dev->platdata;
228 switch (op) {
229 case env_op_create:
230 case env_op_overwrite:
231 eth_parse_enetaddr(value, pdata->enetaddr);
232 eth_write_hwaddr(dev);
233 break;
234 case env_op_delete:
235 memset(pdata->enetaddr, 0, ARP_HLEN);
236 }
237 }
238
239 return 0;
240}
241U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
242
243int eth_init(void)
244{
245 char *ethact = env_get("ethact");
246 char *ethrotate = env_get("ethrotate");
247 struct udevice *current = NULL;
248 struct udevice *old_current;
249 int ret = -ENODEV;
250
251 /*
252 * When 'ethrotate' variable is set to 'no' and 'ethact' variable
253 * is already set to an ethernet device, we should stick to 'ethact'.
254 */
255 if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
256 if (ethact) {
257 current = eth_get_dev_by_name(ethact);
258 if (!current)
259 return -EINVAL;
260 }
261 }
262
263 if (!current) {
264 current = eth_get_dev();
265 if (!current) {
266 printf("No ethernet found.\n");
267 return -ENODEV;
268 }
269 }
270
271 old_current = current;
272 do {
273 if (current) {
274 debug("Trying %s\n", current->name);
275
276 if (device_active(current)) {
277 ret = eth_get_ops(current)->start(current);
278 if (ret >= 0) {
279 struct eth_device_priv *priv =
280 current->uclass_priv;
281
282 priv->state = ETH_STATE_ACTIVE;
283 return 0;
284 }
285 } else {
286 ret = eth_errno;
287 }
288
289 debug("FAIL\n");
290 } else {
291 debug("PROBE FAIL\n");
292 }
293
294 /*
295 * If ethrotate is enabled, this will change "current",
296 * otherwise we will drop out of this while loop immediately
297 */
298 eth_try_another(0);
299 /* This will ensure the new "current" attempted to probe */
300 current = eth_get_dev();
301 } while (old_current != current);
302
303 return ret;
304}
305
306void eth_halt(void)
307{
308 struct udevice *current;
309 struct eth_device_priv *priv;
310
311 current = eth_get_dev();
312 if (!current || !device_active(current))
313 return;
314
315 eth_get_ops(current)->stop(current);
316 priv = current->uclass_priv;
317 priv->state = ETH_STATE_PASSIVE;
318}
319
320int eth_is_active(struct udevice *dev)
321{
322 struct eth_device_priv *priv;
323
324 if (!dev || !device_active(dev))
325 return 0;
326
327 priv = dev_get_uclass_priv(dev);
328 return priv->state == ETH_STATE_ACTIVE;
329}
330
331int eth_send(void *packet, int length)
332{
333 struct udevice *current;
334 int ret;
335
336 current = eth_get_dev();
337 if (!current)
338 return -ENODEV;
339
340 if (!eth_is_active(current))
341 return -EINVAL;
342
343 ret = eth_get_ops(current)->send(current, packet, length);
344 if (ret < 0) {
345 /* We cannot completely return the error at present */
346 debug("%s: send() returned error %d\n", __func__, ret);
347 }
348 return ret;
349}
350
351int eth_rx(void)
352{
353 struct udevice *current;
354 uchar *packet;
355 int flags;
356 int ret;
357 int i;
358
359 current = eth_get_dev();
360 if (!current)
361 return -ENODEV;
362
363 if (!eth_is_active(current))
364 return -EINVAL;
365
366 /* Process up to 32 packets at one time */
367 flags = ETH_RECV_CHECK_DEVICE;
368 for (i = 0; i < 32; i++) {
369 ret = eth_get_ops(current)->recv(current, flags, &packet);
370 flags = 0;
371 if (ret > 0)
372 net_process_received_packet(packet, ret);
373 if (ret >= 0 && eth_get_ops(current)->free_pkt)
374 eth_get_ops(current)->free_pkt(current, packet, ret);
375 if (ret <= 0)
376 break;
377 }
378 if (ret == -EAGAIN)
379 ret = 0;
380 if (ret < 0) {
381 /* We cannot completely return the error at present */
382 debug("%s: recv() returned error %d\n", __func__, ret);
383 }
384 return ret;
385}
386
387int eth_initialize(void)
388{
389 int num_devices = 0;
390 struct udevice *dev;
391
392 eth_common_init();
393
394 /*
395 * Devices need to write the hwaddr even if not started so that Linux
396 * will have access to the hwaddr that u-boot stored for the device.
397 * This is accomplished by attempting to probe each device and calling
398 * their write_hwaddr() operation.
399 */
400 uclass_first_device(UCLASS_ETH, &dev);
401 if (!dev) {
402 printf("No ethernet found.\n");
403 bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
404 } else {
405 char *ethprime = env_get("ethprime");
406 struct udevice *prime_dev = NULL;
407
408 if (ethprime)
409 prime_dev = eth_get_dev_by_name(ethprime);
410 if (prime_dev) {
411 eth_set_dev(prime_dev);
412 eth_current_changed();
413 } else {
414 eth_set_dev(NULL);
415 }
416
417 bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
418 do {
419 if (num_devices)
420 printf(", ");
421
422 printf("eth%d: %s", dev->seq, dev->name);
423
424 if (ethprime && dev == prime_dev)
425 printf(" [PRIME]");
426
427 eth_write_hwaddr(dev);
428
429 uclass_next_device(&dev);
430 num_devices++;
431 } while (dev);
432
433 putc('\n');
434 }
435
436 return num_devices;
437}
438
439static int eth_post_bind(struct udevice *dev)
440{
441 if (strchr(dev->name, ' ')) {
442 printf("\nError: eth device name \"%s\" has a space!\n",
443 dev->name);
444 return -EINVAL;
445 }
446
447 return 0;
448}
449
450static int eth_pre_unbind(struct udevice *dev)
451{
452 /* Don't hang onto a pointer that is going away */
453 if (dev == eth_get_uclass_priv()->current)
454 eth_set_dev(NULL);
455
456 return 0;
457}
458
459static int eth_get_efuse_mac(struct udevice *dev)
460{
461 struct eth_pdata *pdata = dev->platdata;
462#ifndef CONFIG_UNIFY_KEY_MANAGE
463 debug("\nWarning: %s MAC addresses is not from dtb\n",
464 dev->name);
465 return -1;
466#else
467#define MAC_MAX_LEN 17
468 int i = 0;
469 int err = 0, exist = 0;
470 ssize_t keysize = 0;
471 const char* seedNum = "0x1234";
472 unsigned char buf[MAC_MAX_LEN+1] = {0};
473
474 err = key_unify_init(seedNum, NULL);
475 if (err)
476 return err;
477
478 err = key_unify_query_exist("mac", &exist);
479 if (err || (!exist))
480 return -EEXIST;
481
482 err = key_unify_query_size("mac", &keysize);
483 if (err)
484 return err;
485
486 if (keysize != MAC_MAX_LEN) {
487 return -EINVAL;
488 }
489
490 err = key_unify_read("mac", buf, keysize);
491 if (err)
492 return err;
493
494 for (i=0; i<6; i++) {
495 buf[i*3 + 2] = '\0';
496 pdata->enetaddr[i] = simple_strtoul((char *)&buf[i*3], NULL, 16);
497 }
498
499 return key_unify_uninit();
500#endif
501}
502static int eth_post_probe(struct udevice *dev)
503{
504 struct eth_device_priv *priv = dev->uclass_priv;
505 struct eth_pdata *pdata = dev->platdata;
506 unsigned char env_enetaddr[ARP_HLEN];
507
508#if defined(CONFIG_NEEDS_MANUAL_RELOC)
509 struct eth_ops *ops = eth_get_ops(dev);
510 static int reloc_done;
511
512 if (!reloc_done) {
513 if (ops->start)
514 ops->start += gd->reloc_off;
515 if (ops->send)
516 ops->send += gd->reloc_off;
517 if (ops->recv)
518 ops->recv += gd->reloc_off;
519 if (ops->free_pkt)
520 ops->free_pkt += gd->reloc_off;
521 if (ops->stop)
522 ops->stop += gd->reloc_off;
523#ifdef CONFIG_MCAST_TFTP
524 if (ops->mcast)
525 ops->mcast += gd->reloc_off;
526#endif
527 if (ops->write_hwaddr)
528 ops->write_hwaddr += gd->reloc_off;
529 if (ops->read_rom_hwaddr)
530 ops->read_rom_hwaddr += gd->reloc_off;
531
532 reloc_done++;
533 }
534#endif
535
536 priv->state = ETH_STATE_INIT;
537
538 /* Check if the device has a MAC address in ROM */
539 if (eth_get_ops(dev)->read_rom_hwaddr)
540 eth_get_ops(dev)->read_rom_hwaddr(dev);
541
542 eth_get_efuse_mac(dev);
543 if (is_valid_ethaddr(pdata->enetaddr)) {
544 eth_env_set_enetaddr_by_index("eth", ARP_HLEN,
545 pdata->enetaddr);
546 } else {
547 uint8_t buff[16];
548 if (get_chip_id(&buff[0], sizeof(buff)) == 0) {
549 sprintf((char *)env_enetaddr,"02:%02x:%02x:%02x:%02x:%02x",buff[8],
550 buff[7],buff[6],buff[5],buff[4]);
551 printf("MACADDR:%s(from chipid)\n",env_enetaddr);
552 env_set("ethaddr",(const char *)env_enetaddr);
553 }
554
555 eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
556 if (!is_zero_ethaddr(env_enetaddr)) {
557 if (!is_zero_ethaddr(pdata->enetaddr) &&
558 memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
559 printf("\nWarning: %s MAC addresses don't match:\n",
560 dev->name);
561 printf("Address in ROM is %pM\n",
562 pdata->enetaddr);
563 printf("Address in environment is %pM\n",
564 env_enetaddr);
565 }
566
567 /* Override the ROM MAC address */
568 memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
569 } else if (is_valid_ethaddr(pdata->enetaddr)) {
570 eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
571 printf("\nWarning: %s using MAC address from ROM\n",
572 dev->name);
573 } else if (is_zero_ethaddr(pdata->enetaddr) ||
574 !is_valid_ethaddr(pdata->enetaddr)) {
575#ifdef CONFIG_NET_RANDOM_ETHADDR
576 net_random_ethaddr(pdata->enetaddr);
577 printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
578 dev->name, dev->seq, pdata->enetaddr);
579#else
580 printf("\nError: %s address not set.\n",
581 dev->name);
582 return -EINVAL;
583#endif
584 }
585 }
586 return 0;
587}
588
589static int eth_pre_remove(struct udevice *dev)
590{
591 struct eth_pdata *pdata = dev->platdata;
592
593 eth_get_ops(dev)->stop(dev);
594
595 /* clear the MAC address */
596 memset(pdata->enetaddr, 0, ARP_HLEN);
597
598 return 0;
599}
600
601UCLASS_DRIVER(eth) = {
602 .name = "eth",
603 .id = UCLASS_ETH,
604 .post_bind = eth_post_bind,
605 .pre_unbind = eth_pre_unbind,
606 .post_probe = eth_post_probe,
607 .pre_remove = eth_pre_remove,
608 .priv_auto_alloc_size = sizeof(struct eth_uclass_priv),
609 .per_device_auto_alloc_size = sizeof(struct eth_device_priv),
610 .flags = DM_UC_FLAG_SEQ_ALIAS,
611};