Skip to contentSkip to conversation

MIPS: BCM47XX: Use mtd as an alternative way/API to get NVRAM content

RFC PATCH In review Rafał Miłecki 2 participants 4 messages Sep 3, 2014 – Sep 13, 2014

Subsystems (2)
lore
Share & tools
lore

Rafał Miłecki

author maintainer
#0 lore raw

RFC PATCH 1/1 MIPS: BCM47XX: Use mtd as an alternative way/API to get NVRAM content

NVRAM can be read using magic memory offset, but after all it's just a flash partition. On platforms where NVRAM isn't needed early we can get it using mtd subsystem.

---
 arch/mips/bcm47xx/nvram.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
arch/mips/bcm47xx/nvram.c +40 -0
index 8ea2116..9ab74db 100644
@@ -16,6 +16,7 @@
 #include <linux/ssb/ssb.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
+#include <linux/mtd/mtd.h>
 #include <asm/addrspace.h>
 #include <bcm47xx_nvram.h>
 #include <asm/mach-bcm47xx/bcm47xx.h>
@@ -148,6 +149,13 @@ static int nvram_init_bcma(void)
 
 static int nvram_init(void)
 {
+#ifdef CONFIG_MTD
+	struct mtd_info *mtd;
+	struct nvram_header header;
+	size_t bytes_read;
+	int i;
+#endif
+
 	switch (bcm47xx_bus_type) {
 #ifdef CONFIG_BCM47XX_SSB
 	case BCM47XX_BUS_TYPE_SSB:
@@ -158,6 +166,38 @@ static int nvram_init(void)
 		return nvram_init_bcma();
 #endif
 	}
+
+#ifdef CONFIG_MTD
+	mtd = get_mtd_device_nm("nvram");
+	if (IS_ERR(mtd))
+		return -ENODEV;
+
+	for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
+		loff_t from = mtd->size - nvram_sizes[i];
+
+		if (from < 0)
+			continue;
+
+		if (mtd_read(mtd, from, sizeof(header), &bytes_read,
+			     (uint8_t *)&header) < 0)
+			continue;
+		if (header.magic == NVRAM_HEADER) {
+			u8 *dst = (uint8_t *)nvram_buf;
+			size_t len = header.len;
+
+			if (header.len > NVRAM_SPACE) {
+				pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
+				       header.len, NVRAM_SPACE);
+				len = NVRAM_SPACE;
+			}
+
+			if (mtd_read(mtd, from, len, &bytes_read, dst) < 0)
+				continue;
+			memset(dst + bytes_read, 0x0, NVRAM_SPACE - bytes_read);
+		}
+	}
+#endif
+
 	return -ENXIO;
 }
 
Signature · 1 line
1.8.4.5
maintainer
#1 lore raw
Quoted 3 lines · Rafał Miłecki

NVRAM can be read using magic memory offset, but after all it's just a flash partition. On platforms where NVRAM isn't needed early we can get it using mtd subsystem.

Where would this work?

Do you plan the following initialization order in bcma on arm and mips?

1. bcma SoC version
2. bcma bus scan
3. flash driver registration
4. nvram read
5. sprom generation from nvram and attaching to bcma
6. registration of the other bcam cores (wifi, ...)

The nvram is memory mapped on every SoC, just on devices with nand flash booting we would need something to check for bad blocks.

Quoted 72 lines
---
 arch/mips/bcm47xx/nvram.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
arch/mips/bcm47xx/nvram.c +40 -0
index 8ea2116..9ab74db 100644
@@ -16,6 +16,7 @@
 #include <linux/ssb/ssb.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
+#include <linux/mtd/mtd.h>
 #include <asm/addrspace.h>
 #include <bcm47xx_nvram.h>
 #include <asm/mach-bcm47xx/bcm47xx.h>
@@ -148,6 +149,13 @@ static int nvram_init_bcma(void)
 
 static int nvram_init(void)
 {
+#ifdef CONFIG_MTD
+	struct mtd_info *mtd;
+	struct nvram_header header;
+	size_t bytes_read;
+	int i;
+#endif
+
 	switch (bcm47xx_bus_type) {
 #ifdef CONFIG_BCM47XX_SSB
 	case BCM47XX_BUS_TYPE_SSB:
@@ -158,6 +166,38 @@ static int nvram_init(void)
 		return nvram_init_bcma();
 #endif
 	}
+
+#ifdef CONFIG_MTD
+	mtd = get_mtd_device_nm("nvram");
+	if (IS_ERR(mtd))
+		return -ENODEV;
+
+	for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
+		loff_t from = mtd->size - nvram_sizes[i];
+
+		if (from < 0)
+			continue;
+
+		if (mtd_read(mtd, from, sizeof(header), &bytes_read,
+			     (uint8_t *)&header) < 0)
+			continue;
+		if (header.magic == NVRAM_HEADER) {
+			u8 *dst = (uint8_t *)nvram_buf;
+			size_t len = header.len;
+
+			if (header.len > NVRAM_SPACE) {
+				pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
+				       header.len, NVRAM_SPACE);
+				len = NVRAM_SPACE;
+			}
+
+			if (mtd_read(mtd, from, len, &bytes_read, dst) < 0)
+				continue;
+			memset(dst + bytes_read, 0x0, NVRAM_SPACE - bytes_read);
+		}
+	}
+#endif
+
 	return -ENXIO;
 }
 
author maintainer
#2 lore raw
Quoted 16 lines · Hauke Mehrtens
Quoted 3 lines · Rafał Miłecki

NVRAM can be read using magic memory offset, but after all it's just a flash partition. On platforms where NVRAM isn't needed early we can get it using mtd subsystem.

Where would this work?

Do you plan the following initialization order in bcma on arm and mips?

1. bcma SoC version
2. bcma bus scan
3. flash driver registration
4. nvram read
5. sprom generation from nvram and attaching to bcma
6. registration of the other bcam cores (wifi, ...)

Quoted 2 lines

The nvram is memory mapped on every SoC, just on devices with nand flash booting we would need something to check for bad blocks.

The order you noted is probably quite accurate.

Indeed, I want to use "mtd" subsystem because of NAND. Mapping NAND flash content to the memory is nice, but it's reliable for few first sections/blocks only. The rest of flash may contain bad blocks and we should read it being aware of that.

maintainer
#3 lore raw
In reply to Rafał Miłecki
Quoted 36 lines · Rafał Miłecki

NVRAM can be read using magic memory offset, but after all it's just a flash partition. On platforms where NVRAM isn't needed early we can get it using mtd subsystem.

---
 arch/mips/bcm47xx/nvram.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
arch/mips/bcm47xx/nvram.c +8 -0
index 8ea2116..9ab74db 100644
@@ -16,6 +16,7 @@
 #include <linux/ssb/ssb.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
+#include <linux/mtd/mtd.h>
 #include <asm/addrspace.h>
 #include <bcm47xx_nvram.h>
 #include <asm/mach-bcm47xx/bcm47xx.h>
@@ -148,6 +149,13 @@ static int nvram_init_bcma(void)
 
 static int nvram_init(void)
 {
+#ifdef CONFIG_MTD
+	struct mtd_info *mtd;
+	struct nvram_header header;
+	size_t bytes_read;
+	int i;
+#endif
+
 	switch (bcm47xx_bus_type) {
 #ifdef CONFIG_BCM47XX_SSB
 	case BCM47XX_BUS_TYPE_SSB:

arch/mips/bcm47xx/nvram.c static int nvram_init(void) · line 170

Quoted 5 lines
arch/mips/bcm47xx/nvram.c +2 -0
@@ -158,6 +166,38 @@ static int nvram_init(void)
 		return nvram_init_bcma();
 #endif
 	}
+
+#ifdef CONFIG_MTD

Could you put this into an extra function so we do not have so many #ifdefs.

arch/mips/bcm47xx/nvram.c static int nvram_init(void) · line 182

Quoted 12 lines
arch/mips/bcm47xx/nvram.c +12 -0
+	mtd = get_mtd_device_nm("nvram");
+	if (IS_ERR(mtd))
+		return -ENODEV;
+
+	for (i = 0; i < ARRAY_SIZE(nvram_sizes); i++) {
+		loff_t from = mtd->size - nvram_sizes[i];
+
+		if (from < 0)
+			continue;
+
+		if (mtd_read(mtd, from, sizeof(header), &bytes_read,
+			     (uint8_t *)&header) < 0)

I do not like complex function calls in if, like "if(mtd_read(...))" could you use this instead:
err = mtd_read(..);
if (err)

Quoted 22 lines
arch/mips/bcm47xx/nvram.c +18 -0
+			continue;
+		if (header.magic == NVRAM_HEADER) {
+			u8 *dst = (uint8_t *)nvram_buf;
+			size_t len = header.len;
+
+			if (header.len > NVRAM_SPACE) {
+				pr_err("nvram on flash (%i bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",
+				       header.len, NVRAM_SPACE);
+				len = NVRAM_SPACE;
+			}
+
+			if (mtd_read(mtd, from, len, &bytes_read, dst) < 0)
+				continue;
+			memset(dst + bytes_read, 0x0, NVRAM_SPACE - bytes_read);
+		}
+	}
+#endif
+
 	return -ENXIO;
 }