From: Rafał Miłecki <rafal@milecki.pl>
This patchset refactors driver part finding and reading NVRAM.
It been tested on BCM4706. Updated code checks the same offsets as
before. Driver still finds & copies NVRAM content.
It's a new patchset replacing previous single-patch attempt:
[PATCH V2 mips/linux.git] firmware: bcm47xx_nvram: refactor finding & reading NVRAM
Rafał Miłecki (5):
firmware: bcm47xx_nvram: rename finding function and its variables
firmware: bcm47xx_nvram: add helper checking for NVRAM
firmware: bcm47xx_nvram: extract code copying NVRAM
firmware: bcm47xx_nvram: look for NVRAM with for instead of while
firmware: bcm47xx_nvram: inline code checking NVRAM size
drivers/firmware/broadcom/bcm47xx_nvram.c | 92 ++++++++++++-----------
1 file changed, 47 insertions(+), 45 deletions(-)
--
2.26.2
From: Rafał Miłecki <rafal@milecki.pl>
1. Use "bcm47xx_" function name prefix for consistency
2. It takes flash start as argument so s/iobase/flash_start/
3. "off" was used for finding flash end so just call it "flash_size"
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
drivers/firmware/broadcom/bcm47xx_nvram.c | 24 ++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
@@ -61,25 +63,25 @@ static int nvram_find_and_copy(void __iomem *iobase, u32 lim)}/* TODO: when nvram is on nand flash check for bad blocks first. */-off=FLASH_MIN;-while(off<=lim){+flash_size=FLASH_MIN;+while(flash_size<=res_size){/* Windowed flash access */-size=find_nvram_size(iobase+off);+size=find_nvram_size(flash_start+flash_size);if(size){-header=(structnvram_header*)(iobase+off-size);+header=(structnvram_header*)(flash_start+flash_size-size);gotofound;}-off<<=1;+flash_size<<=1;}/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */-header=(structnvram_header*)(iobase+4096);+header=(structnvram_header*)(flash_start+4096);if(header->magic==NVRAM_MAGIC){size=NVRAM_SPACE;gotofound;}-header=(structnvram_header*)(iobase+1024);+header=(structnvram_header*)(flash_start+1024);if(header->magic==NVRAM_MAGIC){size=NVRAM_SPACE;gotofound;
@@ -124,7 +126,7 @@ int bcm47xx_nvram_init_from_mem(u32 base, u32 lim)if(!iobase)return-ENOMEM;-err=nvram_find_and_copy(iobase,lim);+err=bcm47xx_nvram_find_and_copy(iobase,lim);iounmap(iobase);
From: Rafał Miłecki <rafal@milecki.pl>
This simplifies function finding NVRAM. It doesn't directly deal with
NVRAM structure anymore and is a bit smaller.
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
drivers/firmware/broadcom/bcm47xx_nvram.c | 43 +++++++++++++----------
1 file changed, 25 insertions(+), 18 deletions(-)
@@ -95,23 +118,7 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_sizreturn-ENXIO;found:-header=(structnvram_header*)(flash_start+offset);-__ioread32_copy(nvram_buf,header,sizeof(*header)/4);-nvram_len=((structnvram_header*)(nvram_buf))->len;-size=res_size-offset;-if(nvram_len>size){-pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");-nvram_len=size;-}-if(nvram_len>=NVRAM_SPACE){-pr_err("nvram on flash (%zu bytes) is bigger than the reserved space in memory, will just copy the first %i bytes\n",-nvram_len,NVRAM_SPACE-1);-nvram_len=NVRAM_SPACE-1;-}-/* proceed reading data after header */-__ioread32_copy(nvram_buf+sizeof(*header),header+1,-DIV_ROUND_UP(nvram_len,4));-nvram_buf[NVRAM_SPACE-1]='\0';+bcm47xx_nvram_copy(flash_start+offset,res_size-offset);return0;}
@@ -68,31 +75,30 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz/* Windowed flash access */size=find_nvram_size(flash_start+flash_size);if(size){-header=(structnvram_header*)(flash_start+flash_size-size);+offset=flash_size-size;gotofound;}flash_size<<=1;}/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */-header=(structnvram_header*)(flash_start+4096);-if(header->magic==NVRAM_MAGIC){-size=NVRAM_SPACE;++offset=4096;+if(bcm47xx_nvram_is_valid(flash_start+offset))gotofound;-}-header=(structnvram_header*)(flash_start+1024);-if(header->magic==NVRAM_MAGIC){-size=NVRAM_SPACE;+offset=1024;+if(bcm47xx_nvram_is_valid(flash_start+offset))gotofound;-}pr_err("no nvram found\n");return-ENXIO;found:+header=(structnvram_header*)(flash_start+offset);__ioread32_copy(nvram_buf,header,sizeof(*header)/4);nvram_len=((structnvram_header*)(nvram_buf))->len;+size=res_size-offset;if(nvram_len>size){pr_err("The nvram size according to the header seems to be bigger than the partition on flash\n");nvram_len=size;
@@ -93,15 +93,13 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz}/* TODO: when nvram is on nand flash check for bad blocks first. */-flash_size=FLASH_MIN;-while(flash_size<=res_size){+for(flash_size=FLASH_MIN;flash_size<=res_size;flash_size<<=1){/* Windowed flash access */size=find_nvram_size(flash_start+flash_size);if(size){offset=flash_size-size;gotofound;}-flash_size<<=1;}/* Try embedded NVRAM at 4 KB and 1 KB as last resorts */
From: Rafał Miłecki <rafal@milecki.pl>
Separated function was not improving code quality much (or at all).
Moreover it expected possible flash end address as argument and it was
returning NVRAM size.
The new code always operates on offsets which means less logic and less
calculations.
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
drivers/firmware/broadcom/bcm47xx_nvram.c | 25 +++++++----------------
1 file changed, 7 insertions(+), 18 deletions(-)
@@ -93,12 +81,13 @@ static int bcm47xx_nvram_find_and_copy(void __iomem *flash_start, size_t res_siz}/* TODO: when nvram is on nand flash check for bad blocks first. */++/* Try every possible flash size and check for NVRAM at its end */for(flash_size=FLASH_MIN;flash_size<=res_size;flash_size<<=1){-/* Windowed flash access */-size=find_nvram_size(flash_start+flash_size);-if(size){-offset=flash_size-size;-gotofound;+for(i=0;i<ARRAY_SIZE(nvram_sizes);i++){+offset=flash_size-nvram_sizes[i];+if(bcm47xx_nvram_is_valid(flash_start+offset))+gotofound;}}
From: Thomas Bogendoerfer <tsbogend@alpha.franken.de> Date: 2021-03-12 15:04:04
On Mon, Mar 08, 2021 at 10:03:15AM +0100, Rafał Miłecki wrote:
From: Rafał Miłecki <rafal@milecki.pl>
This patchset refactors driver part finding and reading NVRAM.
It been tested on BCM4706. Updated code checks the same offsets as
before. Driver still finds & copies NVRAM content.
It's a new patchset replacing previous single-patch attempt:
[PATCH V2 mips/linux.git] firmware: bcm47xx_nvram: refactor finding & reading NVRAM
Rafał Miłecki (5):
firmware: bcm47xx_nvram: rename finding function and its variables
firmware: bcm47xx_nvram: add helper checking for NVRAM
firmware: bcm47xx_nvram: extract code copying NVRAM
firmware: bcm47xx_nvram: look for NVRAM with for instead of while
firmware: bcm47xx_nvram: inline code checking NVRAM size
drivers/firmware/broadcom/bcm47xx_nvram.c | 92 ++++++++++++-----------
1 file changed, 47 insertions(+), 45 deletions(-)
series applied to mips-next.
Thomas.
--
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea. [ RFC1925, 2.3 ]