]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
board: atmel: sama5d27_wlsom1_ek: Set ethaddr from spi-nor flash
authorTudor Ambarus <tudor.ambarus@microchip.com>
Wed, 13 Nov 2019 15:42:54 +0000 (15:42 +0000)
committerEugen Hristev <eugen.hristev@microchip.com>
Tue, 17 Dec 2019 07:49:05 +0000 (09:49 +0200)
The SST26VF064BEUI spi-nor flash is programmed at the factory with a
globally unique address stored in the SFDP vendor parameter table and
it is permanently writeprotected. Retrieve the EUI-48 address and set it
as ethaddr env.

Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
arch/arm/mach-at91/include/mach/at91_common.h
board/atmel/common/Makefile
board/atmel/common/mac-spi-nor.c [new file with mode: 0644]
board/atmel/sama5d27_wlsom1_ek/sama5d27_wlsom1_ek.c

index e929b5e1d207e53286f0ddbd8f52b68014a45395..01e00c508a8a40abed82f29e4af387ecdce2fed9 100644 (file)
@@ -40,6 +40,7 @@ void configure_ddrcfg_input_buffers(bool open);
 #endif
 
 int at91_set_ethaddr(int offset);
+void at91_spi_nor_set_ethaddr(void);
 int at91_video_show_board_info(void);
 
 #endif /* AT91_COMMON_H */
index 4de0912f22e64e58317fde8da7ed51418b69f43c..6bc8cabb8d6d2b28e7ea9280ffa475a188c7e850 100644 (file)
@@ -5,4 +5,5 @@
 
 obj-y += board.o
 obj-$(CONFIG_I2C_EEPROM) += mac_eeprom.o
+obj-$(CONFIG_SPI_FLASH_SFDP_SUPPORT) += mac-spi-nor.o
 obj-$(CONFIG_DM_VIDEO) += video_display.o
diff --git a/board/atmel/common/mac-spi-nor.c b/board/atmel/common/mac-spi-nor.c
new file mode 100644 (file)
index 0000000..9634367
--- /dev/null
@@ -0,0 +1,127 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (C) 2019 Microchip Technology Inc. and its subsidiaries
+ *
+ * Author: Tudor Ambarus <tudor.ambarus@microchip.com>
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <env.h>
+#include <linux/mtd/spi-nor.h>
+#include <netdev.h>
+
+#define ETH_ADDR_SIZE                  6
+
+#ifdef CONFIG_SPI_FLASH_SST
+#define SFDP_MICROCHIP_MANUF_ID                0xbf
+#define SFDP_MICROCHIP_MEM_TYPE                0x26
+#define SFDP_MICROCHIP_DEV_ID          0x43
+
+#define SFDP_MICROCHIP_EUI_OFFSET      0x60
+#define SFDP_MICROCHIP_EUI48           0x30
+
+struct sst26vf064beui {
+       u8 manufacturer_id;
+       u8 memory_type;
+       u8 device_id;
+       u8 reserved;
+};
+
+/**
+ * sst26vf064beui_check() - Check the validity of the EUI-48 information from
+ * the sst26vf064beui SPI NOR Microchip SFDP table.
+ * @manufacturer_sfdp: pointer to the Microchip manufacturer specific SFDP
+ *                     table.
+ *
+ * Return: 0 on success, -errno otherwise.
+ */
+static int sst26vf064beui_check(const u8 *manufacturer_sfdp)
+{
+       struct sst26vf064beui *sst26vf064beui =
+               (struct sst26vf064beui *)manufacturer_sfdp;
+
+       if (sst26vf064beui->manufacturer_id != SFDP_MICROCHIP_MANUF_ID)
+               return -EINVAL;
+
+       if (sst26vf064beui->memory_type != SFDP_MICROCHIP_MEM_TYPE)
+               return -EINVAL;
+
+       if (sst26vf064beui->device_id != SFDP_MICROCHIP_DEV_ID)
+               return -EINVAL;
+
+       /*
+        * Check if the EUI-48 MAC address is programmed in the next six address
+        * locations.
+        */
+       if (manufacturer_sfdp[SFDP_MICROCHIP_EUI_OFFSET] !=
+           SFDP_MICROCHIP_EUI48)
+               return -EINVAL;
+
+       return 0;
+}
+
+/**
+ * sst26vf064beui_get_ethaddr() - Get the ethernet address from the
+ * sst26vf064beui SPI NOR Microchip SFDP table.
+ * @manufacturer_sfdp: pointer to the Microchip manufacturer specific SFDP
+ *                     table.
+ * @ethaddr:           pointer where to fill the ethernet address
+ * @size:              size of the ethernet address.
+ *
+ * Return: 0 on success, -errno otherwise.
+ */
+static int sst26vf064beui_get_ethaddr(const u8 *manufacturer_sfdp,
+                                     u8 *ethaddr, size_t size)
+{
+       u64 eui_table[2];
+       u64 *p = (u64 *)&manufacturer_sfdp[SFDP_MICROCHIP_EUI_OFFSET];
+       int i, ret;
+
+       ret = sst26vf064beui_check(manufacturer_sfdp);
+       if (ret)
+               return ret;
+
+       for (i = 0; i < 2; i++)
+               eui_table[i] = le64_to_cpu(p[i]);
+
+       /* Ethaddr starts at offset one. */
+       memcpy(ethaddr, &((u8 *)eui_table)[1], size);
+
+       return 0;
+}
+#endif
+
+/**
+ * at91_spi_nor_set_ethaddr() - Retrieve and set the ethernet address from the
+ * SPI NOR manufacturer specific SFDP table.
+ */
+void at91_spi_nor_set_ethaddr(void)
+{
+       struct udevice *dev;
+       struct spi_nor *nor;
+       const char *ethaddr_name = "ethaddr";
+       u8 ethaddr[ETH_ADDR_SIZE] = {0};
+
+       if (env_get(ethaddr_name))
+               return;
+
+       if (uclass_first_device_err(UCLASS_SPI_FLASH, &dev))
+               return;
+
+       nor = dev_get_uclass_priv(dev);
+       if (!nor)
+               return;
+
+       if (!nor->manufacturer_sfdp)
+               return;
+
+#ifdef CONFIG_SPI_FLASH_SST
+       if (sst26vf064beui_get_ethaddr(nor->manufacturer_sfdp, ethaddr,
+                                      ETH_ADDR_SIZE))
+               return;
+#endif
+
+       if (is_valid_ethaddr(ethaddr))
+               eth_env_set_enetaddr(ethaddr_name, ethaddr);
+}
index fda06c824d53593c8a03460aa7786e4d173c20c4..fc563ebb7150e84a3464018440753afa60683e08 100644 (file)
@@ -68,6 +68,9 @@ int board_init(void)
 #ifdef CONFIG_MISC_INIT_R
 int misc_init_r(void)
 {
+#ifdef CONFIG_SPI_FLASH_SFDP_SUPPORT
+       at91_spi_nor_set_ethaddr();
+#endif
        return 0;
 }
 #endif