Commit df6e36ed authored by Takahiro Kuwano's avatar Takahiro Kuwano Committed by Tudor Ambarus

mtd: spi-nor: core: get rid of SNOR_LAST_REGION flag

Introduce n_regions in spi_nor_erase_map structure and remove
SNOR_LAST_REGION flag. Loop logics that depend on the flag are also
reworked to use n_regions as loop condition.
Signed-off-by: default avatarTakahiro Kuwano <Takahiro.Kuwano@infineon.com>
Suggested-by: default avatarTudor Ambarus <tudor.ambarus@linaro.org>
Suggested-by: default avatarMichael Walle <mwalle@kernel.org>
Reviewed-by: default avatarMichael Walle <mwalle@kernel.org>
Link: https://lore.kernel.org/r/eded84294bd81e966d6f423e578fc2cfb9a4a5b6.1708404584.git.Takahiro.Kuwano@infineon.com
[ta: update spi_nor_init_erase_cmd_list() and break the for loop sooner.]
Signed-off-by: default avatarTudor Ambarus <tudor.ambarus@linaro.org>
parent 0e164238
...@@ -1573,52 +1573,6 @@ spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map, ...@@ -1573,52 +1573,6 @@ spi_nor_find_best_erase_type(const struct spi_nor_erase_map *map,
return NULL; return NULL;
} }
static u64 spi_nor_region_is_last(const struct spi_nor_erase_region *region)
{
return region->flags & SNOR_LAST_REGION;
}
/**
* spi_nor_region_next() - get the next spi nor region
* @region: pointer to a structure that describes a SPI NOR erase region
*
* Return: the next spi nor region or NULL if last region.
*/
struct spi_nor_erase_region *
spi_nor_region_next(struct spi_nor_erase_region *region)
{
if (spi_nor_region_is_last(region))
return NULL;
region++;
return region;
}
/**
* spi_nor_find_erase_region() - find the region of the serial flash memory in
* which the offset fits
* @map: the erase map of the SPI NOR
* @addr: offset in the serial flash memory
*
* Return: a pointer to the spi_nor_erase_region struct, ERR_PTR(-errno)
* otherwise.
*/
static struct spi_nor_erase_region *
spi_nor_find_erase_region(const struct spi_nor_erase_map *map, u64 addr)
{
struct spi_nor_erase_region *region = map->regions;
u64 region_end = region->offset + region->size;
while (addr < region->offset || addr >= region_end) {
region = spi_nor_region_next(region);
if (!region)
return ERR_PTR(-EINVAL);
region_end = region->offset + region->size;
}
return region;
}
/** /**
* spi_nor_init_erase_cmd() - initialize an erase command * spi_nor_init_erase_cmd() - initialize an erase command
* @region: pointer to a structure that describes a SPI NOR erase region * @region: pointer to a structure that describes a SPI NOR erase region
...@@ -1685,44 +1639,36 @@ static int spi_nor_init_erase_cmd_list(struct spi_nor *nor, ...@@ -1685,44 +1639,36 @@ static int spi_nor_init_erase_cmd_list(struct spi_nor *nor,
struct spi_nor_erase_region *region; struct spi_nor_erase_region *region;
struct spi_nor_erase_command *cmd = NULL; struct spi_nor_erase_command *cmd = NULL;
u64 region_end; u64 region_end;
unsigned int i;
int ret = -EINVAL; int ret = -EINVAL;
region = spi_nor_find_erase_region(map, addr); for (i = 0; i < map->n_regions && len; i++) {
if (IS_ERR(region)) region = &map->regions[i];
return PTR_ERR(region); region_end = region->offset + region->size;
region_end = region->offset + region->size;
while (len) { while (len && addr >= region->offset && addr < region_end) {
erase = spi_nor_find_best_erase_type(map, region, addr, len); erase = spi_nor_find_best_erase_type(map, region, addr,
if (!erase) len);
goto destroy_erase_cmd_list; if (!erase)
if (prev_erase != erase ||
erase->size != cmd->size ||
region->flags & SNOR_OVERLAID_REGION) {
cmd = spi_nor_init_erase_cmd(region, erase);
if (IS_ERR(cmd)) {
ret = PTR_ERR(cmd);
goto destroy_erase_cmd_list; goto destroy_erase_cmd_list;
}
list_add_tail(&cmd->list, erase_list);
} else {
cmd->count++;
}
addr += cmd->size; if (prev_erase != erase || erase->size != cmd->size ||
len -= cmd->size; region->flags & SNOR_OVERLAID_REGION) {
cmd = spi_nor_init_erase_cmd(region, erase);
if (IS_ERR(cmd)) {
ret = PTR_ERR(cmd);
goto destroy_erase_cmd_list;
}
list_add_tail(&cmd->list, erase_list);
} else {
cmd->count++;
}
if (len && addr >= region_end) { len -= cmd->size;
region = spi_nor_region_next(region); addr += cmd->size;
if (!region) prev_erase = erase;
goto destroy_erase_cmd_list;
region_end = region->offset + region->size;
} }
prev_erase = erase;
} }
return 0; return 0;
...@@ -2463,8 +2409,8 @@ void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map, ...@@ -2463,8 +2409,8 @@ void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
map->uniform_region.offset = 0; map->uniform_region.offset = 0;
map->uniform_region.size = flash_size; map->uniform_region.size = flash_size;
map->uniform_region.erase_mask = erase_mask; map->uniform_region.erase_mask = erase_mask;
map->uniform_region.flags = SNOR_LAST_REGION;
map->regions = &map->uniform_region; map->regions = &map->uniform_region;
map->n_regions = 1;
} }
int spi_nor_post_bfpt_fixups(struct spi_nor *nor, int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
......
...@@ -245,8 +245,7 @@ struct spi_nor_erase_command { ...@@ -245,8 +245,7 @@ struct spi_nor_erase_command {
* inside this region. The erase types are sorted in * inside this region. The erase types are sorted in
* ascending order with the smallest Erase Type size being * ascending order with the smallest Erase Type size being
* at BIT(0). * at BIT(0).
* @flags: flags to determine if this region is overlaid, if this * @flags: flags to determine if this region is overlaid.
* region is the last in the SPI NOR flash memory
*/ */
struct spi_nor_erase_region { struct spi_nor_erase_region {
u64 offset; u64 offset;
...@@ -257,8 +256,7 @@ struct spi_nor_erase_region { ...@@ -257,8 +256,7 @@ struct spi_nor_erase_region {
#define SNOR_ERASE_TYPE_MAX 4 #define SNOR_ERASE_TYPE_MAX 4
#define SNOR_LAST_REGION BIT(0) #define SNOR_OVERLAID_REGION BIT(0)
#define SNOR_OVERLAID_REGION BIT(1)
/** /**
* struct spi_nor_erase_map - Structure to describe the SPI NOR erase map * struct spi_nor_erase_map - Structure to describe the SPI NOR erase map
...@@ -271,11 +269,13 @@ struct spi_nor_erase_region { ...@@ -271,11 +269,13 @@ struct spi_nor_erase_region {
* The erase types are sorted in ascending order, with the * The erase types are sorted in ascending order, with the
* smallest Erase Type size being the first member in the * smallest Erase Type size being the first member in the
* erase_type array. * erase_type array.
* @n_regions: number of erase regions.
*/ */
struct spi_nor_erase_map { struct spi_nor_erase_map {
struct spi_nor_erase_region *regions; struct spi_nor_erase_region *regions;
struct spi_nor_erase_region uniform_region; struct spi_nor_erase_region uniform_region;
struct spi_nor_erase_type erase_type[SNOR_ERASE_TYPE_MAX]; struct spi_nor_erase_type erase_type[SNOR_ERASE_TYPE_MAX];
unsigned int n_regions;
}; };
/** /**
...@@ -667,8 +667,6 @@ void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode, ...@@ -667,8 +667,6 @@ void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size, void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
u8 opcode); u8 opcode);
void spi_nor_mask_erase_type(struct spi_nor_erase_type *erase); void spi_nor_mask_erase_type(struct spi_nor_erase_type *erase);
struct spi_nor_erase_region *
spi_nor_region_next(struct spi_nor_erase_region *region);
void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map, void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
u8 erase_mask, u64 flash_size); u8 erase_mask, u64 flash_size);
......
...@@ -78,10 +78,10 @@ static int spi_nor_params_show(struct seq_file *s, void *data) ...@@ -78,10 +78,10 @@ static int spi_nor_params_show(struct seq_file *s, void *data)
struct spi_nor *nor = s->private; struct spi_nor *nor = s->private;
struct spi_nor_flash_parameter *params = nor->params; struct spi_nor_flash_parameter *params = nor->params;
struct spi_nor_erase_map *erase_map = &params->erase_map; struct spi_nor_erase_map *erase_map = &params->erase_map;
struct spi_nor_erase_region *region; struct spi_nor_erase_region *region = erase_map->regions;
const struct flash_info *info = nor->info; const struct flash_info *info = nor->info;
char buf[16], *str; char buf[16], *str;
int i; unsigned int i;
seq_printf(s, "name\t\t%s\n", info->name); seq_printf(s, "name\t\t%s\n", info->name);
seq_printf(s, "id\t\t%*ph\n", SPI_NOR_MAX_ID_LEN, nor->id); seq_printf(s, "id\t\t%*ph\n", SPI_NOR_MAX_ID_LEN, nor->id);
...@@ -144,13 +144,11 @@ static int spi_nor_params_show(struct seq_file *s, void *data) ...@@ -144,13 +144,11 @@ static int spi_nor_params_show(struct seq_file *s, void *data)
seq_puts(s, "\nsector map\n"); seq_puts(s, "\nsector map\n");
seq_puts(s, " region (in hex) | erase mask | flags\n"); seq_puts(s, " region (in hex) | erase mask | flags\n");
seq_puts(s, " ------------------+------------+----------\n"); seq_puts(s, " ------------------+------------+----------\n");
for (region = erase_map->regions; for (i = 0; i < erase_map->n_regions; i++) {
region; u64 start = region[i].offset;
region = spi_nor_region_next(region)) { u64 end = start + region[i].size - 1;
u64 start = region->offset; u8 erase_mask = region[i].erase_mask;
u64 end = start + region->size - 1; u8 flags = region[i].flags;
u8 erase_mask = region->erase_mask;
u8 flags = region->flags;
seq_printf(s, " %08llx-%08llx | [%c%c%c%c] | %s\n", seq_printf(s, " %08llx-%08llx | [%c%c%c%c] | %s\n",
start, end, start, end,
......
...@@ -390,15 +390,14 @@ static void spi_nor_regions_sort_erase_types(struct spi_nor_erase_map *map) ...@@ -390,15 +390,14 @@ static void spi_nor_regions_sort_erase_types(struct spi_nor_erase_map *map)
{ {
struct spi_nor_erase_region *region = map->regions; struct spi_nor_erase_region *region = map->regions;
u8 sorted_erase_mask; u8 sorted_erase_mask;
unsigned int i;
while (region) { for (i = 0; i < map->n_regions; i++) {
sorted_erase_mask = spi_nor_sort_erase_mask(map, sorted_erase_mask =
region->erase_mask); spi_nor_sort_erase_mask(map, region[i].erase_mask);
/* Overwrite erase mask. */ /* Overwrite erase mask. */
region->erase_mask = sorted_erase_mask; region[i].erase_mask = sorted_erase_mask;
region = spi_nor_region_next(region);
} }
} }
...@@ -801,11 +800,6 @@ static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt, ...@@ -801,11 +800,6 @@ static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
return ret; return ret;
} }
static void spi_nor_region_mark_end(struct spi_nor_erase_region *region)
{
region->flags |= SNOR_LAST_REGION;
}
static void spi_nor_region_mark_overlay(struct spi_nor_erase_region *region) static void spi_nor_region_mark_overlay(struct spi_nor_erase_region *region)
{ {
region->flags |= SNOR_OVERLAID_REGION; region->flags |= SNOR_OVERLAID_REGION;
...@@ -863,6 +857,7 @@ static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor, ...@@ -863,6 +857,7 @@ static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
if (!region) if (!region)
return -ENOMEM; return -ENOMEM;
map->regions = region; map->regions = region;
map->n_regions = region_count;
uniform_erase_type = 0xff; uniform_erase_type = 0xff;
regions_erase_type = 0; regions_erase_type = 0;
...@@ -891,7 +886,6 @@ static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor, ...@@ -891,7 +886,6 @@ static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
offset = region[i].offset + region[i].size; offset = region[i].offset + region[i].size;
} }
spi_nor_region_mark_end(&region[i - 1]);
save_uniform_erase_type = map->uniform_region.erase_mask; save_uniform_erase_type = map->uniform_region.erase_mask;
map->uniform_region.erase_mask = map->uniform_region.erase_mask =
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment