Skip to content

Commit 0554eff

Browse files
mwalleambarus
authored andcommitted
mtd: spi-nor: convert .n_sectors to .size
.n_sectors is rarely used. In fact it is only used in swp.c and to calculate the flash size in the core. The use in swp.c might be converted to use the (largest) flash erase size. For now, we just locally calculate the sector size. Simplify the flash_info database and set the size of the flash directly. This also let us use the SZ_x macros. Verified that there's no flash that specifies BP and sector size of zero to make sure we avoid a division by zero in spi_nor_get_min_prot_length_sr(). We'll protect from a possible division by zero in a further patch by introducing a default value for sector_size. Signed-off-by: Michael Walle <mwalle@kernel.org> Link: https://lore.kernel.org/r/20230807-mtd-flash-info-db-rework-v3-5-e60548861b10@kernel.org Signed-off-by: Tudor Ambarus <tudor.ambarus@linaro.org>
1 parent afbfb8c commit 0554eff

4 files changed

Lines changed: 12 additions & 11 deletions

File tree

drivers/mtd/spi-nor/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2999,7 +2999,7 @@ static void spi_nor_init_default_params(struct spi_nor *nor)
29992999

30003000
/* Set SPI NOR sizes. */
30013001
params->writesize = 1;
3002-
params->size = (u64)info->sector_size * info->n_sectors;
3002+
params->size = info->size;
30033003
params->bank_size = params->size;
30043004
params->page_size = info->page_size;
30053005

drivers/mtd/spi-nor/core.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,9 +443,9 @@ struct spi_nor_fixups {
443443
* @id: the flash's ID bytes. The first three bytes are the
444444
* JEDIC ID. JEDEC ID zero means "no ID" (mostly older chips).
445445
* @id_len: the number of bytes of ID.
446+
* @size: the size of the flash in bytes.
446447
* @sector_size: the size listed here is what works with SPINOR_OP_SE, which
447448
* isn't necessarily called a "sector" by the vendor.
448-
* @n_sectors: the number of sectors.
449449
* @n_banks: the number of banks.
450450
* @page_size: the flash's page size.
451451
* @addr_nbytes: number of address bytes to send.
@@ -505,8 +505,8 @@ struct flash_info {
505505
char *name;
506506
u8 id[SPI_NOR_MAX_ID_LEN];
507507
u8 id_len;
508+
size_t size;
508509
unsigned sector_size;
509-
u16 n_sectors;
510510
u16 page_size;
511511
u8 n_banks;
512512
u8 addr_nbytes;
@@ -556,8 +556,8 @@ struct flash_info {
556556
.id_len = 6
557557

558558
#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors, _n_banks) \
559+
.size = (_sector_size) * (_n_sectors), \
559560
.sector_size = (_sector_size), \
560-
.n_sectors = (_n_sectors), \
561561
.page_size = 256, \
562562
.n_banks = (_n_banks)
563563

@@ -575,8 +575,8 @@ struct flash_info {
575575
SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),
576576

577577
#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_nbytes) \
578+
.size = (_sector_size) * (_n_sectors), \
578579
.sector_size = (_sector_size), \
579-
.n_sectors = (_n_sectors), \
580580
.page_size = (_page_size), \
581581
.n_banks = 1, \
582582
.addr_nbytes = (_addr_nbytes), \

drivers/mtd/spi-nor/swp.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ static u8 spi_nor_get_sr_tb_mask(struct spi_nor *nor)
3434
static u64 spi_nor_get_min_prot_length_sr(struct spi_nor *nor)
3535
{
3636
unsigned int bp_slots, bp_slots_needed;
37+
unsigned int sector_size = nor->info->sector_size;
38+
u64 n_sectors = div_u64(nor->params->size, sector_size);
3739
u8 mask = spi_nor_get_sr_bp_mask(nor);
3840

3941
/* Reserved one for "protect none" and one for "protect all". */
4042
bp_slots = (1 << hweight8(mask)) - 2;
41-
bp_slots_needed = ilog2(nor->info->n_sectors);
43+
bp_slots_needed = ilog2(n_sectors);
4244

4345
if (bp_slots_needed > bp_slots)
44-
return nor->info->sector_size <<
45-
(bp_slots_needed - bp_slots);
46+
return sector_size << (bp_slots_needed - bp_slots);
4647
else
47-
return nor->info->sector_size;
48+
return sector_size;
4849
}
4950

5051
static void spi_nor_get_locked_range_sr(struct spi_nor *nor, u8 sr, loff_t *ofs,

drivers/mtd/spi-nor/xilinx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
#define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
2525
SPI_NOR_ID(_jedec_id, 0), \
26+
.size = 8 * (_page_size) * (_n_sectors), \
2627
.sector_size = (8 * (_page_size)), \
27-
.n_sectors = (_n_sectors), \
2828
.page_size = (_page_size), \
2929
.n_banks = 1, \
3030
.flags = SPI_NOR_NO_FR
@@ -138,7 +138,7 @@ static int xilinx_nor_setup(struct spi_nor *nor,
138138
page_size = (nor->params->page_size == 264) ? 256 : 512;
139139
nor->params->page_size = page_size;
140140
nor->mtd.writebufsize = page_size;
141-
nor->params->size = 8 * page_size * nor->info->n_sectors;
141+
nor->params->size = nor->info->size;
142142
nor->mtd.erasesize = 8 * page_size;
143143
} else {
144144
/* Flash in Default addressing mode */

0 commit comments

Comments
 (0)