2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-01-17 21:52:00 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2001-2015
|
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
* Joe Hershberger, National Instruments
|
|
|
|
*/
|
|
|
|
|
2021-07-20 18:15:30 +00:00
|
|
|
#define LOG_CATEGORY UCLASS_ETH
|
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
#include <common.h>
|
2022-04-25 05:31:15 +00:00
|
|
|
#include <bootdev.h>
|
2020-05-10 17:40:00 +00:00
|
|
|
#include <bootstage.h>
|
2016-01-17 21:52:00 +00:00
|
|
|
#include <dm.h>
|
2019-08-01 15:46:51 +00:00
|
|
|
#include <env.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2016-01-17 21:52:00 +00:00
|
|
|
#include <net.h>
|
2022-05-05 17:11:41 +00:00
|
|
|
#include <nvmem.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2016-01-17 21:52:00 +00:00
|
|
|
#include <dm/device-internal.h>
|
|
|
|
#include <dm/uclass-internal.h>
|
2019-07-18 18:43:30 +00:00
|
|
|
#include <net/pcap.h>
|
2016-01-17 21:52:00 +00:00
|
|
|
#include "eth_internal.h"
|
2020-05-03 14:41:14 +00:00
|
|
|
#include <eth_phy.h>
|
2016-01-17 21:52:00 +00:00
|
|
|
|
2016-01-30 22:45:14 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
/**
|
|
|
|
* struct eth_device_priv - private structure for each Ethernet device
|
|
|
|
*
|
|
|
|
* @state: The state of the Ethernet MAC driver (defined by enum eth_state_t)
|
|
|
|
*/
|
|
|
|
struct eth_device_priv {
|
|
|
|
enum eth_state_t state;
|
2020-11-04 13:45:14 +00:00
|
|
|
bool running;
|
2016-01-17 21:52:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct eth_uclass_priv - The structure attached to the uclass itself
|
|
|
|
*
|
|
|
|
* @current: The Ethernet device that the network functions are using
|
2023-01-17 17:47:28 +00:00
|
|
|
* @no_bootdevs: true to skip binding Ethernet bootdevs (this is a negative flag
|
|
|
|
* so that the default value enables it)
|
2016-01-17 21:52:00 +00:00
|
|
|
*/
|
|
|
|
struct eth_uclass_priv {
|
|
|
|
struct udevice *current;
|
2023-01-17 17:47:28 +00:00
|
|
|
bool no_bootdevs;
|
2016-01-17 21:52:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* eth_errno - This stores the most recent failure code from DM functions */
|
|
|
|
static int eth_errno;
|
|
|
|
|
2023-03-06 14:53:42 +00:00
|
|
|
/* board-specific Ethernet Interface initializations. */
|
|
|
|
__weak int board_interface_eth_init(struct udevice *dev,
|
|
|
|
phy_interface_t interface_type)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
static struct eth_uclass_priv *eth_get_uclass_priv(void)
|
|
|
|
{
|
|
|
|
struct uclass *uc;
|
2020-05-03 14:41:13 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = uclass_get(UCLASS_ETH, &uc);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
2016-01-17 21:52:00 +00:00
|
|
|
|
|
|
|
assert(uc);
|
2020-12-23 02:30:28 +00:00
|
|
|
return uclass_get_priv(uc);
|
2016-01-17 21:52:00 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 17:47:28 +00:00
|
|
|
void eth_set_enable_bootdevs(bool enable)
|
|
|
|
{
|
|
|
|
struct eth_uclass_priv *priv = eth_get_uclass_priv();
|
|
|
|
|
|
|
|
if (priv)
|
|
|
|
priv->no_bootdevs = !enable;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
void eth_set_current_to_next(void)
|
|
|
|
{
|
|
|
|
struct eth_uclass_priv *uc_priv;
|
|
|
|
|
|
|
|
uc_priv = eth_get_uclass_priv();
|
|
|
|
if (uc_priv->current)
|
|
|
|
uclass_next_device(&uc_priv->current);
|
|
|
|
if (!uc_priv->current)
|
|
|
|
uclass_first_device(UCLASS_ETH, &uc_priv->current);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Typically this will simply return the active device.
|
|
|
|
* In the case where the most recent active device was unset, this will attempt
|
2021-02-24 16:30:44 +00:00
|
|
|
* to return the device with sequence id 0 (which can be configured by the
|
|
|
|
* device tree). If this fails, fall back to just getting the first device.
|
|
|
|
* The latter is non-deterministic and depends on the order of the probing.
|
|
|
|
* If that device doesn't exist or fails to probe, this function will return
|
|
|
|
* NULL.
|
2016-01-17 21:52:00 +00:00
|
|
|
*/
|
|
|
|
struct udevice *eth_get_dev(void)
|
|
|
|
{
|
|
|
|
struct eth_uclass_priv *uc_priv;
|
|
|
|
|
|
|
|
uc_priv = eth_get_uclass_priv();
|
2020-09-12 21:45:43 +00:00
|
|
|
if (!uc_priv)
|
|
|
|
return NULL;
|
|
|
|
|
2021-02-24 16:30:44 +00:00
|
|
|
if (!uc_priv->current) {
|
|
|
|
eth_errno = uclass_get_device_by_seq(UCLASS_ETH, 0,
|
|
|
|
&uc_priv->current);
|
|
|
|
if (eth_errno)
|
2022-10-12 19:57:59 +00:00
|
|
|
eth_errno = uclass_first_device_err(UCLASS_ETH,
|
|
|
|
&uc_priv->current);
|
2022-10-12 19:58:02 +00:00
|
|
|
if (eth_errno)
|
|
|
|
uc_priv->current = NULL;
|
2021-02-24 16:30:44 +00:00
|
|
|
}
|
2016-01-17 21:52:00 +00:00
|
|
|
return uc_priv->current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Typically this will just store a device pointer.
|
|
|
|
* In case it was not probed, we will attempt to do so.
|
|
|
|
* dev may be NULL to unset the active device.
|
|
|
|
*/
|
|
|
|
void eth_set_dev(struct udevice *dev)
|
|
|
|
{
|
|
|
|
if (dev && !device_active(dev)) {
|
|
|
|
eth_errno = device_probe(dev);
|
|
|
|
if (eth_errno)
|
|
|
|
dev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
eth_get_uclass_priv()->current = dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the udevice that either has the name passed in as devname or has an
|
|
|
|
* alias named devname.
|
|
|
|
*/
|
|
|
|
struct udevice *eth_get_dev_by_name(const char *devname)
|
|
|
|
{
|
|
|
|
int seq = -1;
|
|
|
|
char *endp = NULL;
|
|
|
|
const char *startp = NULL;
|
|
|
|
struct udevice *it;
|
|
|
|
struct uclass *uc;
|
|
|
|
int len = strlen("eth");
|
2020-05-03 14:41:13 +00:00
|
|
|
int ret;
|
2016-01-17 21:52:00 +00:00
|
|
|
|
|
|
|
/* Must be longer than 3 to be an alias */
|
|
|
|
if (!strncmp(devname, "eth", len) && strlen(devname) > len) {
|
|
|
|
startp = devname + len;
|
2021-07-24 15:03:30 +00:00
|
|
|
seq = dectoul(startp, &endp);
|
2016-01-17 21:52:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 14:41:13 +00:00
|
|
|
ret = uclass_get(UCLASS_ETH, &uc);
|
|
|
|
if (ret)
|
|
|
|
return NULL;
|
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
uclass_foreach_dev(it, uc) {
|
|
|
|
/*
|
|
|
|
* We don't care about errors from probe here. Either they won't
|
|
|
|
* match an alias or it will match a literal name and we'll pick
|
|
|
|
* up the error when we try to probe again in eth_set_dev().
|
|
|
|
*/
|
|
|
|
if (device_probe(it))
|
|
|
|
continue;
|
|
|
|
/* Check for the name or the sequence number to match */
|
|
|
|
if (strcmp(it->name, devname) == 0 ||
|
2020-12-17 04:20:07 +00:00
|
|
|
(endp > startp && dev_seq(it) == seq))
|
2016-01-17 21:52:00 +00:00
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *eth_get_ethaddr(void)
|
|
|
|
{
|
|
|
|
struct eth_pdata *pdata;
|
|
|
|
|
|
|
|
if (eth_get_dev()) {
|
2020-12-23 02:30:28 +00:00
|
|
|
pdata = dev_get_plat(eth_get_dev());
|
2016-01-17 21:52:00 +00:00
|
|
|
return pdata->enetaddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set active state without calling start on the driver */
|
|
|
|
int eth_init_state_only(void)
|
|
|
|
{
|
|
|
|
struct udevice *current;
|
|
|
|
struct eth_device_priv *priv;
|
|
|
|
|
|
|
|
current = eth_get_dev();
|
|
|
|
if (!current || !device_active(current))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2020-12-23 02:30:28 +00:00
|
|
|
priv = dev_get_uclass_priv(current);
|
2016-01-17 21:52:00 +00:00
|
|
|
priv->state = ETH_STATE_ACTIVE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set passive state without calling stop on the driver */
|
|
|
|
void eth_halt_state_only(void)
|
|
|
|
{
|
|
|
|
struct udevice *current;
|
|
|
|
struct eth_device_priv *priv;
|
|
|
|
|
|
|
|
current = eth_get_dev();
|
|
|
|
if (!current || !device_active(current))
|
|
|
|
return;
|
|
|
|
|
2020-12-23 02:30:28 +00:00
|
|
|
priv = dev_get_uclass_priv(current);
|
2016-01-17 21:52:00 +00:00
|
|
|
priv->state = ETH_STATE_PASSIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int eth_get_dev_index(void)
|
|
|
|
{
|
|
|
|
if (eth_get_dev())
|
2020-12-17 04:20:07 +00:00
|
|
|
return dev_seq(eth_get_dev());
|
2016-01-17 21:52:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eth_write_hwaddr(struct udevice *dev)
|
|
|
|
{
|
2017-05-16 03:07:01 +00:00
|
|
|
struct eth_pdata *pdata;
|
2016-01-17 21:52:00 +00:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!dev || !device_active(dev))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* seq is valid since the device is active */
|
2020-12-17 04:20:07 +00:00
|
|
|
if (eth_get_ops(dev)->write_hwaddr && !eth_mac_skip(dev_seq(dev))) {
|
2020-12-23 02:30:28 +00:00
|
|
|
pdata = dev_get_plat(dev);
|
2016-01-17 21:52:00 +00:00
|
|
|
if (!is_valid_ethaddr(pdata->enetaddr)) {
|
|
|
|
printf("\nError: %s address %pM illegal value\n",
|
|
|
|
dev->name, pdata->enetaddr);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Drivers are allowed to decide not to implement this at
|
|
|
|
* run-time. E.g. Some devices may use it and some may not.
|
|
|
|
*/
|
|
|
|
ret = eth_get_ops(dev)->write_hwaddr(dev);
|
|
|
|
if (ret == -ENOSYS)
|
|
|
|
ret = 0;
|
|
|
|
if (ret)
|
|
|
|
printf("\nWarning: %s failed to set MAC address\n",
|
|
|
|
dev->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int on_ethaddr(const char *name, const char *value, enum env_op op,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
int retval;
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
/* look for an index after "eth" */
|
2021-07-24 15:03:30 +00:00
|
|
|
index = dectoul(name + 3, NULL);
|
2016-01-17 21:52:00 +00:00
|
|
|
|
2020-12-17 04:20:29 +00:00
|
|
|
retval = uclass_find_device_by_seq(UCLASS_ETH, index, &dev);
|
2016-01-17 21:52:00 +00:00
|
|
|
if (!retval) {
|
2020-12-23 02:30:28 +00:00
|
|
|
struct eth_pdata *pdata = dev_get_plat(dev);
|
2016-01-17 21:52:00 +00:00
|
|
|
switch (op) {
|
|
|
|
case env_op_create:
|
|
|
|
case env_op_overwrite:
|
2019-09-14 00:21:16 +00:00
|
|
|
string_to_enetaddr(value, pdata->enetaddr);
|
2016-09-02 12:48:17 +00:00
|
|
|
eth_write_hwaddr(dev);
|
2016-01-17 21:52:00 +00:00
|
|
|
break;
|
|
|
|
case env_op_delete:
|
2016-11-25 15:30:19 +00:00
|
|
|
memset(pdata->enetaddr, 0, ARP_HLEN);
|
2016-01-17 21:52:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
U_BOOT_ENV_CALLBACK(ethaddr, on_ethaddr);
|
|
|
|
|
|
|
|
int eth_init(void)
|
|
|
|
{
|
2017-08-03 18:22:12 +00:00
|
|
|
char *ethact = env_get("ethact");
|
|
|
|
char *ethrotate = env_get("ethrotate");
|
2016-01-17 21:52:00 +00:00
|
|
|
struct udevice *current = NULL;
|
|
|
|
struct udevice *old_current;
|
|
|
|
int ret = -ENODEV;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When 'ethrotate' variable is set to 'no' and 'ethact' variable
|
|
|
|
* is already set to an ethernet device, we should stick to 'ethact'.
|
|
|
|
*/
|
|
|
|
if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0)) {
|
|
|
|
if (ethact) {
|
|
|
|
current = eth_get_dev_by_name(ethact);
|
|
|
|
if (!current)
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current) {
|
|
|
|
current = eth_get_dev();
|
|
|
|
if (!current) {
|
2020-09-14 09:00:18 +00:00
|
|
|
log_err("No ethernet found.\n");
|
2016-01-17 21:52:00 +00:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
old_current = current;
|
|
|
|
do {
|
|
|
|
if (current) {
|
|
|
|
debug("Trying %s\n", current->name);
|
|
|
|
|
|
|
|
if (device_active(current)) {
|
|
|
|
ret = eth_get_ops(current)->start(current);
|
|
|
|
if (ret >= 0) {
|
|
|
|
struct eth_device_priv *priv =
|
2020-12-23 02:30:28 +00:00
|
|
|
dev_get_uclass_priv(current);
|
2016-01-17 21:52:00 +00:00
|
|
|
|
|
|
|
priv->state = ETH_STATE_ACTIVE;
|
2020-11-04 13:45:14 +00:00
|
|
|
priv->running = true;
|
2016-01-17 21:52:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = eth_errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("FAIL\n");
|
|
|
|
} else {
|
|
|
|
debug("PROBE FAIL\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If ethrotate is enabled, this will change "current",
|
|
|
|
* otherwise we will drop out of this while loop immediately
|
|
|
|
*/
|
|
|
|
eth_try_another(0);
|
|
|
|
/* This will ensure the new "current" attempted to probe */
|
|
|
|
current = eth_get_dev();
|
|
|
|
} while (old_current != current);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void eth_halt(void)
|
|
|
|
{
|
|
|
|
struct udevice *current;
|
|
|
|
struct eth_device_priv *priv;
|
|
|
|
|
|
|
|
current = eth_get_dev();
|
2020-11-04 13:45:14 +00:00
|
|
|
if (!current)
|
2016-01-17 21:52:00 +00:00
|
|
|
return;
|
|
|
|
|
2020-12-23 02:30:28 +00:00
|
|
|
priv = dev_get_uclass_priv(current);
|
2020-11-04 13:45:14 +00:00
|
|
|
if (!priv || !priv->running)
|
|
|
|
return;
|
|
|
|
|
|
|
|
eth_get_ops(current)->stop(current);
|
|
|
|
priv->state = ETH_STATE_PASSIVE;
|
|
|
|
priv->running = false;
|
2016-01-17 21:52:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int eth_is_active(struct udevice *dev)
|
|
|
|
{
|
|
|
|
struct eth_device_priv *priv;
|
|
|
|
|
|
|
|
if (!dev || !device_active(dev))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
priv = dev_get_uclass_priv(dev);
|
|
|
|
return priv->state == ETH_STATE_ACTIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
int eth_send(void *packet, int length)
|
|
|
|
{
|
|
|
|
struct udevice *current;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
current = eth_get_dev();
|
|
|
|
if (!current)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2018-03-15 14:07:09 +00:00
|
|
|
if (!eth_is_active(current))
|
2016-01-17 21:52:00 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
ret = eth_get_ops(current)->send(current, packet, length);
|
|
|
|
if (ret < 0) {
|
|
|
|
/* We cannot completely return the error at present */
|
|
|
|
debug("%s: send() returned error %d\n", __func__, ret);
|
|
|
|
}
|
2019-07-18 18:43:30 +00:00
|
|
|
#if defined(CONFIG_CMD_PCAP)
|
|
|
|
if (ret >= 0)
|
|
|
|
pcap_post(packet, length, true);
|
|
|
|
#endif
|
2016-01-17 21:52:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int eth_rx(void)
|
|
|
|
{
|
|
|
|
struct udevice *current;
|
|
|
|
uchar *packet;
|
|
|
|
int flags;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
current = eth_get_dev();
|
|
|
|
if (!current)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2018-03-15 14:07:09 +00:00
|
|
|
if (!eth_is_active(current))
|
2016-01-17 21:52:00 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/* Process up to 32 packets at one time */
|
|
|
|
flags = ETH_RECV_CHECK_DEVICE;
|
2020-10-07 09:03:30 +00:00
|
|
|
for (i = 0; i < ETH_PACKETS_BATCH_RECV; i++) {
|
2016-01-17 21:52:00 +00:00
|
|
|
ret = eth_get_ops(current)->recv(current, flags, &packet);
|
|
|
|
flags = 0;
|
|
|
|
if (ret > 0)
|
|
|
|
net_process_received_packet(packet, ret);
|
|
|
|
if (ret >= 0 && eth_get_ops(current)->free_pkt)
|
|
|
|
eth_get_ops(current)->free_pkt(current, packet, ret);
|
|
|
|
if (ret <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ret == -EAGAIN)
|
|
|
|
ret = 0;
|
|
|
|
if (ret < 0) {
|
|
|
|
/* We cannot completely return the error at present */
|
|
|
|
debug("%s: recv() returned error %d\n", __func__, ret);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int eth_initialize(void)
|
|
|
|
{
|
|
|
|
int num_devices = 0;
|
|
|
|
struct udevice *dev;
|
|
|
|
|
|
|
|
eth_common_init();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Devices need to write the hwaddr even if not started so that Linux
|
|
|
|
* will have access to the hwaddr that u-boot stored for the device.
|
|
|
|
* This is accomplished by attempting to probe each device and calling
|
|
|
|
* their write_hwaddr() operation.
|
|
|
|
*/
|
2018-04-27 12:52:56 +00:00
|
|
|
uclass_first_device_check(UCLASS_ETH, &dev);
|
2016-01-17 21:52:00 +00:00
|
|
|
if (!dev) {
|
2020-09-14 09:00:18 +00:00
|
|
|
log_err("No ethernet found.\n");
|
2016-01-17 21:52:00 +00:00
|
|
|
bootstage_error(BOOTSTAGE_ID_NET_ETH_START);
|
|
|
|
} else {
|
2017-08-03 18:22:12 +00:00
|
|
|
char *ethprime = env_get("ethprime");
|
2016-01-17 21:52:00 +00:00
|
|
|
struct udevice *prime_dev = NULL;
|
|
|
|
|
|
|
|
if (ethprime)
|
|
|
|
prime_dev = eth_get_dev_by_name(ethprime);
|
|
|
|
if (prime_dev) {
|
|
|
|
eth_set_dev(prime_dev);
|
|
|
|
eth_current_changed();
|
|
|
|
} else {
|
|
|
|
eth_set_dev(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
|
|
|
|
do {
|
2020-12-17 04:20:16 +00:00
|
|
|
if (device_active(dev)) {
|
2019-10-21 23:03:10 +00:00
|
|
|
if (num_devices)
|
|
|
|
printf(", ");
|
2016-01-17 21:52:00 +00:00
|
|
|
|
2020-12-17 04:20:07 +00:00
|
|
|
printf("eth%d: %s", dev_seq(dev), dev->name);
|
2016-01-17 21:52:00 +00:00
|
|
|
|
2019-10-21 23:03:10 +00:00
|
|
|
if (ethprime && dev == prime_dev)
|
|
|
|
printf(" [PRIME]");
|
|
|
|
}
|
2016-01-17 21:52:00 +00:00
|
|
|
|
|
|
|
eth_write_hwaddr(dev);
|
|
|
|
|
2020-12-17 04:20:16 +00:00
|
|
|
if (device_active(dev))
|
2019-10-21 23:03:10 +00:00
|
|
|
num_devices++;
|
2018-04-27 12:52:56 +00:00
|
|
|
uclass_next_device_check(&dev);
|
2016-01-17 21:52:00 +00:00
|
|
|
} while (dev);
|
|
|
|
|
2019-10-21 23:03:10 +00:00
|
|
|
if (!num_devices)
|
2020-09-14 09:00:18 +00:00
|
|
|
log_err("No ethernet found.\n");
|
2016-01-17 21:52:00 +00:00
|
|
|
putc('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_devices;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eth_post_bind(struct udevice *dev)
|
|
|
|
{
|
2023-01-17 17:47:28 +00:00
|
|
|
struct eth_uclass_priv *priv = uclass_get_priv(dev->uclass);
|
2022-04-25 05:31:15 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
if (strchr(dev->name, ' ')) {
|
|
|
|
printf("\nError: eth device name \"%s\" has a space!\n",
|
|
|
|
dev->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-05-03 14:41:14 +00:00
|
|
|
#ifdef CONFIG_DM_ETH_PHY
|
|
|
|
eth_phy_binds_nodes(dev);
|
|
|
|
#endif
|
2023-01-17 17:47:28 +00:00
|
|
|
if (CONFIG_IS_ENABLED(BOOTDEV_ETH) && !priv->no_bootdevs) {
|
2022-04-25 05:31:15 +00:00
|
|
|
ret = bootdev_setup_for_dev(dev, "eth_bootdev");
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("bootdev", ret);
|
|
|
|
}
|
2020-05-03 14:41:14 +00:00
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eth_pre_unbind(struct udevice *dev)
|
|
|
|
{
|
|
|
|
/* Don't hang onto a pointer that is going away */
|
|
|
|
if (dev == eth_get_uclass_priv()->current)
|
|
|
|
eth_set_dev(NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:59:57 +00:00
|
|
|
static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
|
|
|
|
{
|
2021-01-14 03:29:47 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
2019-05-20 15:59:57 +00:00
|
|
|
const uint8_t *p;
|
2022-05-05 17:11:41 +00:00
|
|
|
struct nvmem_cell mac_cell;
|
2019-05-20 15:59:57 +00:00
|
|
|
|
|
|
|
p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
|
|
|
|
if (!p)
|
|
|
|
p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
|
|
|
|
|
2022-05-05 17:11:41 +00:00
|
|
|
if (p) {
|
|
|
|
memcpy(mac, p, ARP_HLEN);
|
|
|
|
return true;
|
|
|
|
}
|
2019-05-20 15:59:57 +00:00
|
|
|
|
2022-05-05 17:11:41 +00:00
|
|
|
if (nvmem_cell_get_by_name(dev, "mac-address", &mac_cell))
|
|
|
|
return false;
|
2019-05-20 15:59:57 +00:00
|
|
|
|
2022-05-05 17:11:41 +00:00
|
|
|
return !nvmem_cell_read(&mac_cell, mac, ARP_HLEN);
|
2019-05-20 15:59:57 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
static int eth_post_probe(struct udevice *dev)
|
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct eth_device_priv *priv = dev_get_uclass_priv(dev);
|
|
|
|
struct eth_pdata *pdata = dev_get_plat(dev);
|
2016-11-25 15:30:19 +00:00
|
|
|
unsigned char env_enetaddr[ARP_HLEN];
|
2020-03-16 10:36:17 +00:00
|
|
|
char *source = "DT";
|
2016-01-17 21:52:00 +00:00
|
|
|
|
|
|
|
priv->state = ETH_STATE_INIT;
|
2020-11-04 13:45:14 +00:00
|
|
|
priv->running = false;
|
2016-01-17 21:52:00 +00:00
|
|
|
|
2019-05-20 15:59:57 +00:00
|
|
|
/* Check if the device has a valid MAC address in device tree */
|
|
|
|
if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
|
|
|
|
!is_valid_ethaddr(pdata->enetaddr)) {
|
2020-03-16 10:36:17 +00:00
|
|
|
source = "ROM";
|
2019-05-20 15:59:57 +00:00
|
|
|
/* Check if the device has a MAC address in ROM */
|
|
|
|
if (eth_get_ops(dev)->read_rom_hwaddr)
|
|
|
|
eth_get_ops(dev)->read_rom_hwaddr(dev);
|
|
|
|
}
|
2016-01-17 21:52:00 +00:00
|
|
|
|
2020-12-17 04:20:07 +00:00
|
|
|
eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
|
2016-01-17 21:52:00 +00:00
|
|
|
if (!is_zero_ethaddr(env_enetaddr)) {
|
|
|
|
if (!is_zero_ethaddr(pdata->enetaddr) &&
|
2016-11-25 15:30:19 +00:00
|
|
|
memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
|
2016-01-17 21:52:00 +00:00
|
|
|
printf("\nWarning: %s MAC addresses don't match:\n",
|
|
|
|
dev->name);
|
2020-03-16 10:36:17 +00:00
|
|
|
printf("Address in %s is\t\t%pM\n",
|
|
|
|
source, pdata->enetaddr);
|
|
|
|
printf("Address in environment is\t%pM\n",
|
2016-01-17 21:52:00 +00:00
|
|
|
env_enetaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Override the ROM MAC address */
|
2016-11-25 15:30:19 +00:00
|
|
|
memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
|
2016-01-17 21:52:00 +00:00
|
|
|
} else if (is_valid_ethaddr(pdata->enetaddr)) {
|
2020-12-17 04:20:07 +00:00
|
|
|
eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
|
|
|
|
pdata->enetaddr);
|
2016-11-02 11:52:13 +00:00
|
|
|
} else if (is_zero_ethaddr(pdata->enetaddr) ||
|
|
|
|
!is_valid_ethaddr(pdata->enetaddr)) {
|
2016-01-17 21:52:00 +00:00
|
|
|
#ifdef CONFIG_NET_RANDOM_ETHADDR
|
|
|
|
net_random_ethaddr(pdata->enetaddr);
|
|
|
|
printf("\nWarning: %s (eth%d) using random MAC address - %pM\n",
|
2020-12-17 04:20:07 +00:00
|
|
|
dev->name, dev_seq(dev), pdata->enetaddr);
|
2022-01-11 09:28:09 +00:00
|
|
|
eth_env_set_enetaddr_by_index("eth", dev_seq(dev),
|
|
|
|
pdata->enetaddr);
|
2016-01-17 21:52:00 +00:00
|
|
|
#else
|
|
|
|
printf("\nError: %s address not set.\n",
|
|
|
|
dev->name);
|
|
|
|
return -EINVAL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:59:56 +00:00
|
|
|
eth_write_hwaddr(dev);
|
|
|
|
|
2016-01-17 21:52:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int eth_pre_remove(struct udevice *dev)
|
|
|
|
{
|
2020-12-23 02:30:28 +00:00
|
|
|
struct eth_pdata *pdata = dev_get_plat(dev);
|
2016-01-17 21:52:00 +00:00
|
|
|
|
|
|
|
eth_get_ops(dev)->stop(dev);
|
|
|
|
|
|
|
|
/* clear the MAC address */
|
2016-11-25 15:30:19 +00:00
|
|
|
memset(pdata->enetaddr, 0, ARP_HLEN);
|
2016-01-17 21:52:00 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-25 15:51:11 +00:00
|
|
|
UCLASS_DRIVER(ethernet) = {
|
|
|
|
.name = "ethernet",
|
2016-01-17 21:52:00 +00:00
|
|
|
.id = UCLASS_ETH,
|
|
|
|
.post_bind = eth_post_bind,
|
|
|
|
.pre_unbind = eth_pre_unbind,
|
|
|
|
.post_probe = eth_post_probe,
|
|
|
|
.pre_remove = eth_pre_remove,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct eth_uclass_priv),
|
|
|
|
.per_device_auto = sizeof(struct eth_device_priv),
|
2016-01-17 21:52:00 +00:00
|
|
|
.flags = DM_UC_FLAG_SEQ_ALIAS,
|
|
|
|
};
|