[PATCH v2 3/4] ARM: novena: Read Ethernet MAC address from EEPROM

Marco Felsch m.felsch at pengutronix.de
Wed Jan 25 12:07:14 PST 2023


On 23-01-26, John Watts wrote:
> The Novena has an EEPROM used for storing information about the board,
> including the Ethernet MAC address.
> 
> The reference for the EEPROM fields is the novena-eeprom source code.
> 
> Signed-off-by: John Watts <contact at jookia.org>
> ---
>  arch/arm/boards/novena/board.c | 88 ++++++++++++++++++++++++++++++++++
>  arch/arm/dts/imx6q-novena.dts  | 15 ++++++
>  2 files changed, 103 insertions(+)
> 
> diff --git a/arch/arm/boards/novena/board.c b/arch/arm/boards/novena/board.c
> index e247f8b8c3..24cde1c0d4 100644
> --- a/arch/arm/boards/novena/board.c
> +++ b/arch/arm/boards/novena/board.c
> @@ -3,9 +3,97 @@
>  
>  #include <common.h>
>  #include <deep-probe.h>
> +#include <fs.h>
> +#include <libfile.h>
> +#include <net.h>
> +
> +struct novena_eeprom {
> +	uint8_t signature[6]; /* 'Novena' */
> +	uint8_t version; /* 1 or 2, not checked */
> +	uint8_t page_size; /* v2 only: EEPROM read/write page */
> +	uint32_t serial; /* 32-bit serial number */
> +	uint8_t mac[6]; /* Gigabit MAC address */
> +	uint16_t features; /* features */
> +	/* ... extra fields omitted ... */
> +} __packed;
> +
> +static void power_on_audio_codec(void)
> +{
> +	int rc = of_devices_ensure_probed_by_name("regulator-audio-codec");
> +
> +	if (rc < 0)
> +		pr_err("Unable to power on audio codec: %s\n", strerror(-rc));
> +}
> +
> +static struct novena_eeprom *novena_read_eeprom(void)
> +{
> +	size_t read;
> +	loff_t max = sizeof(struct novena_eeprom);
> +	void *eeprom;
> +	int rc;
> +
> +	/*
> +	 * When powered off the audio codec pulls down the EEPROM's I2C line.
> +	 * Power it on so we can actually read data.
> +	 */
> +	power_on_audio_codec();
> +
> +	rc = of_device_ensure_probed_by_alias("eeprom0");
> +	if (rc < 0) {
> +		pr_err("Unable to probe eeprom0: %s\n", strerror(-rc));
> +		return NULL;
> +	}
> +
> +	rc = read_file_2("/dev/eeprom0", &read, &eeprom, max);

You never free the eeprom buffer.

> +
> +	if (rc < 0 && rc != -EFBIG) {
> +		pr_err("Unable to read Novena EEPROM: %s\n", strerror(-rc));
> +		return NULL;
> +	} else if (read != max) {
> +		pr_err("Short read from Novena EEPROM?\n");
> +		return NULL;
> +	} else {
> +		return eeprom;
> +	}

Last else can be dropped and instad:

	return eeprom;
> +}
> +
> +static bool novena_check_eeprom(struct novena_eeprom *eeprom)
> +{
> +	char *sig = eeprom->signature;
> +	size_t size = sizeof(eeprom->signature);
> +
> +	if (memcmp("Novena", sig, size) != 0) {
> +		pr_err("Unknown Novena EEPROM signature\n");
> +		return false;
> +	}
> +
> +	return true;
> +}
> +
> +static void novena_set_mac(struct novena_eeprom *eeprom)
> +{
> +	struct device_node *dnode;
> +
> +	dnode = of_find_node_by_alias(of_get_root_node(), "ethernet0");
> +	if (dnode)
> +		of_eth_register_ethaddr(dnode, eeprom->mac);
> +	else
> +		pr_err("Unable to find ethernet node\n");
> +}
> +
> +static void novena_try_eeprom(void)
> +{
> +	struct novena_eeprom *eeprom = novena_read_eeprom();
> +
> +	if (!eeprom || !novena_check_eeprom(eeprom))
> +		return;
> +
> +	novena_set_mac(eeprom);
> +}
>  
>  static int novena_probe(struct device *dev)
>  {
> +	novena_try_eeprom();

It's not wrong to move it into a sub-function but IMO at least this
function can be part of the probe() function. Also I would do the
device_ensured_probe function here and just have one function which I
would call: novena_set_mac(). This way it's a bit easier to follow the
code. But that's just my oppinion.

> +
>  	return 0;
>  }
>  
> diff --git a/arch/arm/dts/imx6q-novena.dts b/arch/arm/dts/imx6q-novena.dts
> index 07471cb132..095dcde0d5 100644
> --- a/arch/arm/dts/imx6q-novena.dts
> +++ b/arch/arm/dts/imx6q-novena.dts
> @@ -2,3 +2,18 @@
>  // SPDX-FileCopyrightText: 2023 John Watts
>  
>  #include <arm/imx6q-novena.dts>
> +
> +/ {
> +	aliases {
> +		eeprom0 = &eeprom;
> +	};
> +};
> +
> +&i2c3 {
> +	eeprom: eeprom at 56 {
> +		compatible = "24c512";
> +		reg = <0x56>;
> +		pagesize = <128>;
> +		status = "okay";

status can be dropped, "okay" is the default for the dtc.

Regards,
  Marco

> +	};
> +};
> -- 
> 2.39.0
> 
> 
> 



More information about the barebox mailing list