MIPS: BCM47XX: Use mtd as an alternative way/API to get NVRAM content
Subsystems (2)
Rafał Miłecki
maintainerRFC 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
@@ -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
Hauke Mehrtens
maintainerQuoted 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
Signed-off-by Rafał Miłecki <[email protected]>--- arch/mips/bcm47xx/nvram.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)arch/mips/bcm47xx/nvram.c +40
-0@@ -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 ; }
Rafał Miłecki
maintainerQuoted 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.
Hauke Mehrtens
maintainerQuoted 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.
Signed-off-by Rafał Miłecki <[email protected]>--- arch/mips/bcm47xx/nvram.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)arch/mips/bcm47xx/nvram.c +8
-0@@ -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 ; }