Skip to contentSkip to conversation

arm: zynq: read mac address from SPI flash memory

RFC PATCH New Luis Araneda 2 participants 3 messages Aug 14, 2018 – Aug 16, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 arm: zynq: read mac address from SPI flash memory

Implement a method for reading the MAC address from an SPI flash memory.
In particular, this method is used by the Zybo Z7 board to read the MAC address from the OTP region in the SPI NOR memory

---

I'm trying to implement the reading of the MAC address of the Zybo Z7 board from the OTP region of its SPI NOR memory.

I took some ideas from Digilent's fork, but the commit was marked as not upstremeable. That's why I'm asking for comments.

If the code can't be merged, ideas on how this can be implemented are welcome.

Thanks,

Luis Araneda.

---
 board/xilinx/zynq/board.c      | 28 ++++++++++++++++++++++++++++
 configs/zynq_zybo_z7_defconfig |  3 +++
 drivers/misc/Kconfig           | 17 +++++++++++++++++
 3 files changed, 48 insertions(+)
board/xilinx/zynq/board.c +28 -0
index 614d93c082..a252c38956 100644
@@ -6,9 +6,12 @@
 
 #include <common.h>
 #include <dm/uclass.h>
+#include <dm/device.h>
+#include <dm/device-internal.h>
 #include <fdtdec.h>
 #include <fpga.h>
 #include <mmc.h>
+#include <spi_flash.h>
 #include <watchdog.h>
 #include <wdt.h>
 #include <zynqpl.h>
@@ -87,6 +90,31 @@ int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
 		printf("I2C EEPROM MAC address read failed\n");
 #endif
 
+#if defined(CONFIG_MAC_ADDR_IN_SPI_FLASH)
+	struct spi_flash *flash;
+	struct udevice *dev;
+	int ret;
+
+	ret = spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS,
+				     CONFIG_SF_DEFAULT_CS,
+				     0, 0, &dev);
+	if (ret) {
+		printf("SPI(bus:%u cs:%u) probe failed\n",
+		       CONFIG_SF_DEFAULT_BUS,
+		       CONFIG_SF_DEFAULT_CS);
+		return 0;
+	}
+
+	flash = dev_get_uclass_priv(dev);
+	flash->read_cmd = CONFIG_MAC_ADDR_SPI_FLASH_READ_CMD;
+
+	if (spi_flash_read_dm(dev,
+			      CONFIG_MAC_ADDR_SPI_FLASH_DATA_OFFSET,
+			      6, ethaddr))
+		printf("SPI MAC address read failed\n");
+
+	device_remove(dev, DM_REMOVE_NORMAL);
+#endif
 	return 0;
 }
 
configs/zynq_zybo_z7_defconfig +3 -0
index ad44e772aa..ca402e3231 100644
@@ -44,6 +44,9 @@ CONFIG_DM_GPIO=y
 CONFIG_SYS_I2C_ZYNQ=y
 CONFIG_ZYNQ_I2C0=y
 CONFIG_ZYNQ_I2C1=y
+CONFIG_MAC_ADDR_IN_SPI_FLASH=y
+CONFIG_MAC_ADDR_SPI_FLASH_READ_CMD=0x4b
+CONFIG_MAC_ADDR_SPI_FLASH_DATA_OFFSET=0x20
 CONFIG_MMC_SDHCI=y
 CONFIG_MMC_SDHCI_ZYNQ=y
 CONFIG_SPI_FLASH=y
drivers/misc/Kconfig +17 -0
index c031dfde9d..40cec81e66 100644
@@ -272,6 +272,23 @@ config SYS_I2C_EEPROM_ADDR_OVERFLOW
 
 endif
 
+config MAC_ADDR_IN_SPI_FLASH
+	bool "MAC address in SPI flash"
+	help
+	  Read MAC address from an SPI flash memory
+
+if MAC_ADDR_IN_SPI_FLASH
+
+config MAC_ADDR_SPI_FLASH_READ_CMD
+	hex "Read command for the SPI flash memory"
+	default 0
+
+config MAC_ADDR_SPI_FLASH_DATA_OFFSET
+	hex "Offset of MAC data in SPI flash memory"
+	default 0
+
+endif
+
 config GDSYS_RXAUI_CTRL
 	bool "Enable gdsys RXAUI control driver"
 	depends on MISC
Signature · 1 line
2.18.0
#1 lore raw

RFC PATCH 1/1 arm: zynq: read mac address from SPI flash memory

Hi Luis, +Jagan,

Quoted 6 lines · Luis Araneda

Implement a method for reading the MAC address from an SPI flash memory.
In particular, this method is used by the Zybo Z7 board to read the MAC address from the OTP region in the SPI NOR memory

Quoted 8 lines

---

I'm trying to implement the reading of the MAC address of the Zybo Z7 board from the OTP region of its SPI NOR memory.

I took some ideas from Digilent's fork, but the commit was marked as not upstremeable. That's why I'm asking for comments.

I have done this code long time ago because there was a work which wasn't finished. Also upstreaming takes some time to finish.

Quoted 68 lines

If the code can't be merged, ideas on how this can be implemented are welcome.

Thanks,

Luis Araneda.

---
 board/xilinx/zynq/board.c      | 28 ++++++++++++++++++++++++++++
 configs/zynq_zybo_z7_defconfig |  3 +++
 drivers/misc/Kconfig           | 17 +++++++++++++++++
 3 files changed, 48 insertions(+)
board/xilinx/zynq/board.c +28 -0
index 614d93c082..a252c38956 100644
@@ -6,9 +6,12 @@
 
 #include <common.h>
 #include <dm/uclass.h>
+#include <dm/device.h>
+#include <dm/device-internal.h>
 #include <fdtdec.h>
 #include <fpga.h>
 #include <mmc.h>
+#include <spi_flash.h>
 #include <watchdog.h>
 #include <wdt.h>
 #include <zynqpl.h>
@@ -87,6 +90,31 @@ int zynq_board_read_rom_ethaddr(unsigned char *ethaddr)
 		printf("I2C EEPROM MAC address read failed\n");
 #endif
 
+#if defined(CONFIG_MAC_ADDR_IN_SPI_FLASH)
+	struct spi_flash *flash;
+	struct udevice *dev;
+	int ret;
+
+	ret = spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS,
+				     CONFIG_SF_DEFAULT_CS,
+				     0, 0, &dev);
+	if (ret) {
+		printf("SPI(bus:%u cs:%u) probe failed\n",
+		       CONFIG_SF_DEFAULT_BUS,
+		       CONFIG_SF_DEFAULT_CS);
+		return 0;
+	}
+
+	flash = dev_get_uclass_priv(dev);
+	flash->read_cmd = CONFIG_MAC_ADDR_SPI_FLASH_READ_CMD;
+
+	if (spi_flash_read_dm(dev,
+			      CONFIG_MAC_ADDR_SPI_FLASH_DATA_OFFSET,
+			      6, ethaddr))
+		printf("SPI MAC address read failed\n");
+
+	device_remove(dev, DM_REMOVE_NORMAL);
+#endif
 	return 0;
 }
 
configs/zynq_zybo_z7_defconfig +0 -0
index ad44e772aa..ca402e3231 100644

configs/zynq_zybo_z7_defconfig CONFIG_DM_GPIO=y · line 48

Quoted 5 lines
configs/zynq_zybo_z7_defconfig +2 -0
@@ -44,6 +44,9 @@ CONFIG_DM_GPIO=y
 CONFIG_SYS_I2C_ZYNQ=y
 CONFIG_ZYNQ_I2C0=y
 CONFIG_ZYNQ_I2C1=y
+CONFIG_MAC_ADDR_IN_SPI_FLASH=y
+CONFIG_MAC_ADDR_SPI_FLASH_READ_CMD=0x4b

I am doing spi but isn't this any standard OTP read command which should put to spi framework.

Quoted 23 lines
configs/zynq_zybo_z7_defconfig +1 -0
+CONFIG_MAC_ADDR_SPI_FLASH_DATA_OFFSET=0x20
 CONFIG_MMC_SDHCI=y
 CONFIG_MMC_SDHCI_ZYNQ=y
 CONFIG_SPI_FLASH=y
drivers/misc/Kconfig +10 -0
index c031dfde9d..40cec81e66 100644
@@ -272,6 +272,23 @@ config SYS_I2C_EEPROM_ADDR_OVERFLOW
 
 endif
 
+config MAC_ADDR_IN_SPI_FLASH
+	bool "MAC address in SPI flash"
+	help
+	  Read MAC address from an SPI flash memory
+
+if MAC_ADDR_IN_SPI_FLASH
+
+config MAC_ADDR_SPI_FLASH_READ_CMD
+	hex "Read command for the SPI flash memory"
+	default 0

drivers/misc/Kconfig config SYS_I2C_EEPROM_ADDR_OVERFLOW · line 294

Quoted 10 lines
drivers/misc/Kconfig +7 -0
+
+config MAC_ADDR_SPI_FLASH_DATA_OFFSET
+	hex "Offset of MAC data in SPI flash memory"
+	default 0
+
+endif
+
 config GDSYS_RXAUI_CTRL
 	bool "Enable gdsys RXAUI control driver"
 	depends on MISC

Thanks,
Michal

#2 lore raw

RFC PATCH 1/1 arm: zynq: read mac address from SPI flash memory

Hi,

Quoted 35 lines · Michal Simek
Quoted 34 lines · Luis Araneda

Implement a method for reading the MAC address from an SPI flash memory.
In particular, this method is used by the Zybo Z7 board to read the MAC address from the OTP region in the SPI NOR memory
[...]

board/xilinx/zynq/board.c +25 -0
+#if defined(CONFIG_MAC_ADDR_IN_SPI_FLASH)
+     struct spi_flash *flash;
+     struct udevice *dev;
+     int ret;
+
+     ret = spi_flash_probe_bus_cs(CONFIG_SF_DEFAULT_BUS,
+                                  CONFIG_SF_DEFAULT_CS,
+                                  0, 0, &dev);
+     if (ret) {
+             printf("SPI(bus:%u cs:%u) probe failed\n",
+                    CONFIG_SF_DEFAULT_BUS,
+                    CONFIG_SF_DEFAULT_CS);
+             return 0;
+     }
+
+     flash = dev_get_uclass_priv(dev);
+     flash->read_cmd = CONFIG_MAC_ADDR_SPI_FLASH_READ_CMD;
+
+     if (spi_flash_read_dm(dev,
+                           CONFIG_MAC_ADDR_SPI_FLASH_DATA_OFFSET,
+                           6, ethaddr))
+             printf("SPI MAC address read failed\n");
+
+     device_remove(dev, DM_REMOVE_NORMAL);
+#endif
      return 0;
 }
Quoted 13 lines

configs/zynq_zybo_z7_defconfig CONFIG_DM_GPIO=y · line 48

Quoted 10 lines
configs/zynq_zybo_z7_defconfig +2 -0
index ad44e772aa..ca402e3231 100644
@@ -44,6 +44,9 @@ CONFIG_DM_GPIO=y
 CONFIG_SYS_I2C_ZYNQ=y
 CONFIG_ZYNQ_I2C0=y
 CONFIG_ZYNQ_I2C1=y
+CONFIG_MAC_ADDR_IN_SPI_FLASH=y
+CONFIG_MAC_ADDR_SPI_FLASH_READ_CMD=0x4b

I am doing spi but isn't this any standard OTP read command which should put to spi framework.

I'm not sure if there is an standard OTP read command, they might be vendor/model specific.
Jagan, is this possible currently, or might it be covered by the new SPI-mem/SPI-NAND code?

In order to keep this moving, I would like to get some specific question answered:
1: Should the final code be in the "zynq_board_read_rom_ethaddr" function like it is now?
2: On an ideal solution, should I remove the probing and read_cmd, and call a function to read from OTP at a certain offset? In that case the CONFIG_MAC_ADDR_SPI_FLASH_READ_CMD would't be necessary.
3: Is the CONFIG_MAC_ADDR_IN_SPI_FLASH an acceptable solution to enable the feature?

Thanks,

Luis Araneda.