Skip to content

Commit 9ec4bbc

Browse files
bbrezillonambarus
authored andcommitted
mtd: spi-nor: Add the concept of SPI NOR manufacturer driver
Declare a spi_nor_manufacturer struct and add basic building blocks to move manufacturer specific code outside of the core. Signed-off-by: Boris Brezillon <bbrezillon@kernel.org> Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com> Reviewed-by: Vignesh Raghavendra <vigneshr@ti.com>
1 parent 4f50e10 commit 9ec4bbc

3 files changed

Lines changed: 89 additions & 11 deletions

File tree

‎drivers/mtd/spi-nor/core.c‎

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2474,8 +2474,26 @@ static const struct flash_info spi_nor_ids[] = {
24742474
{ },
24752475
};
24762476

2477+
static const struct spi_nor_manufacturer *manufacturers[0];
2478+
2479+
static const struct flash_info *
2480+
spi_nor_search_part_by_id(const struct flash_info *parts, unsigned int nparts,
2481+
const u8 *id)
2482+
{
2483+
unsigned int i;
2484+
2485+
for (i = 0; i < nparts; i++) {
2486+
if (parts[i].id_len &&
2487+
!memcmp(parts[i].id, id, parts[i].id_len))
2488+
return &parts[i];
2489+
}
2490+
2491+
return NULL;
2492+
}
2493+
24772494
static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
24782495
{
2496+
const struct flash_info *info;
24792497
u8 *id = nor->bouncebuf;
24802498
unsigned int i;
24812499
int ret;
@@ -2497,11 +2515,21 @@ static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
24972515
return ERR_PTR(ret);
24982516
}
24992517

2500-
for (i = 0; i < ARRAY_SIZE(spi_nor_ids) - 1; i++) {
2501-
if (spi_nor_ids[i].id_len &&
2502-
!memcmp(spi_nor_ids[i].id, id, spi_nor_ids[i].id_len))
2503-
return &spi_nor_ids[i];
2518+
for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
2519+
info = spi_nor_search_part_by_id(manufacturers[i]->parts,
2520+
manufacturers[i]->nparts,
2521+
id);
2522+
if (info) {
2523+
nor->manufacturer = manufacturers[i];
2524+
return info;
2525+
}
25042526
}
2527+
2528+
info = spi_nor_search_part_by_id(spi_nor_ids,
2529+
ARRAY_SIZE(spi_nor_ids) - 1, id);
2530+
if (info)
2531+
return info;
2532+
25052533
dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
25062534
SPI_NOR_MAX_ID_LEN, id);
25072535
return ERR_PTR(-ENODEV);
@@ -2987,6 +3015,16 @@ int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
29873015
const struct sfdp_bfpt *bfpt,
29883016
struct spi_nor_flash_parameter *params)
29893017
{
3018+
int ret;
3019+
3020+
if (nor->manufacturer && nor->manufacturer->fixups &&
3021+
nor->manufacturer->fixups->post_bfpt) {
3022+
ret = nor->manufacturer->fixups->post_bfpt(nor, bfpt_header,
3023+
bfpt, params);
3024+
if (ret)
3025+
return ret;
3026+
}
3027+
29903028
if (nor->info->fixups && nor->info->fixups->post_bfpt)
29913029
return nor->info->fixups->post_bfpt(nor, bfpt_header, bfpt,
29923030
params);
@@ -3296,6 +3334,10 @@ static void spi_nor_manufacturer_init_params(struct spi_nor *nor)
32963334
break;
32973335
}
32983336

3337+
if (nor->manufacturer && nor->manufacturer->fixups &&
3338+
nor->manufacturer->fixups->default_init)
3339+
nor->manufacturer->fixups->default_init(nor);
3340+
32993341
if (nor->info->fixups && nor->info->fixups->default_init)
33003342
nor->info->fixups->default_init(nor);
33013343
}
@@ -3455,6 +3497,10 @@ static void spi_nor_post_sfdp_fixups(struct spi_nor *nor)
34553497
if (nor->info->flags & SPI_S3AN)
34563498
s3an_post_sfdp_fixups(nor);
34573499

3500+
if (nor->manufacturer && nor->manufacturer->fixups &&
3501+
nor->manufacturer->fixups->post_sfdp)
3502+
nor->manufacturer->fixups->post_sfdp(nor);
3503+
34583504
if (nor->info->fixups && nor->info->fixups->post_sfdp)
34593505
nor->info->fixups->post_sfdp(nor);
34603506
}
@@ -3617,15 +3663,25 @@ void spi_nor_restore(struct spi_nor *nor)
36173663
}
36183664
EXPORT_SYMBOL_GPL(spi_nor_restore);
36193665

3620-
static const struct flash_info *spi_nor_match_id(const char *name)
3666+
static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
3667+
const char *name)
36213668
{
3622-
const struct flash_info *id = spi_nor_ids;
3669+
unsigned int i, j;
36233670

3624-
while (id->name) {
3625-
if (!strcmp(name, id->name))
3626-
return id;
3627-
id++;
3671+
for (i = 0; i < ARRAY_SIZE(spi_nor_ids) - 1; i++) {
3672+
if (!strcmp(name, spi_nor_ids[i].name))
3673+
return &spi_nor_ids[i];
36283674
}
3675+
3676+
for (i = 0; i < ARRAY_SIZE(manufacturers); i++) {
3677+
for (j = 0; j < manufacturers[i]->nparts; j++) {
3678+
if (!strcmp(name, manufacturers[i]->parts[j].name)) {
3679+
nor->manufacturer = manufacturers[i];
3680+
return &manufacturers[i]->parts[j];
3681+
}
3682+
}
3683+
}
3684+
36293685
return NULL;
36303686
}
36313687

@@ -3672,7 +3728,7 @@ static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
36723728
const struct flash_info *info = NULL;
36733729

36743730
if (name)
3675-
info = spi_nor_match_id(name);
3731+
info = spi_nor_match_id(nor, name);
36763732
/* Try to auto-detect if chip name wasn't specified or not found */
36773733
if (!info)
36783734
info = spi_nor_read_id(nor);

‎drivers/mtd/spi-nor/core.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,20 @@ struct flash_info {
152152
.addr_width = 3, \
153153
.flags = SPI_NOR_NO_FR | SPI_S3AN,
154154

155+
/**
156+
* struct spi_nor_manufacturer - SPI NOR manufacturer object
157+
* @name: manufacturer name
158+
* @parts: array of parts supported by this manufacturer
159+
* @nparts: number of entries in the parts array
160+
* @fixups: hooks called at various points in time during spi_nor_scan()
161+
*/
162+
struct spi_nor_manufacturer {
163+
const char *name;
164+
const struct flash_info *parts;
165+
unsigned int nparts;
166+
const struct spi_nor_fixups *fixups;
167+
};
168+
155169
int spi_nor_write_enable(struct spi_nor *nor);
156170
int spi_nor_write_disable(struct spi_nor *nor);
157171
int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable);

‎include/linux/mtd/spi-nor.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,12 @@ struct spi_nor_flash_parameter {
554554
*/
555555
struct flash_info;
556556

557+
/**
558+
* struct spi_nor_manufacturer - Forward declaration of a structure used
559+
* internally by the core and manufacturer drivers.
560+
*/
561+
struct spi_nor_manufacturer;
562+
557563
/**
558564
* struct spi_nor - Structure for defining a the SPI NOR layer
559565
* @mtd: point to a mtd_info structure
@@ -564,6 +570,7 @@ struct flash_info;
564570
* layer is not DMA-able
565571
* @bouncebuf_size: size of the bounce buffer
566572
* @info: spi-nor part JDEC MFR id and other info
573+
* @manufacturer: spi-nor manufacturer
567574
* @page_size: the page size of the SPI NOR
568575
* @addr_width: number of address bytes
569576
* @erase_opcode: the opcode for erasing a sector
@@ -591,6 +598,7 @@ struct spi_nor {
591598
u8 *bouncebuf;
592599
size_t bouncebuf_size;
593600
const struct flash_info *info;
601+
const struct spi_nor_manufacturer *manufacturer;
594602
u32 page_size;
595603
u8 addr_width;
596604
u8 erase_opcode;

0 commit comments

Comments
 (0)