diff options
author | Furquan Shaikh <furquan@chromium.org> | 2017-05-15 14:35:15 -0700 |
---|---|---|
committer | Furquan Shaikh <furquan@google.com> | 2017-05-19 21:21:47 +0200 |
commit | 30221b45e02f0be8940debd8ad5690c77d6a97a6 (patch) | |
tree | 0771086cabe7259abef22d80a29377d2661bc795 /src | |
parent | fc1a123aa7392fe7900b466e6a6f089733fec1ee (diff) |
drivers/spi/spi_flash: Pass in flash structure to fill in probe
Instead of making all SPI drivers allocate space for a spi_flash
structure and fill it in, udpate the API to allow callers to pass in a
spi_flash structure that can be filled by the flash drivers as
required. This also cleans up the interface so that the callers can
maintain and free the space for spi_flash structure as required.
BUG=b:38330715
Change-Id: If6f1b403731466525c4690777d9b32ce778eb563
Signed-off-by: Furquan Shaikh <furquan@chromium.org>
Reviewed-on: https://review.coreboot.org/19705
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Diffstat (limited to 'src')
25 files changed, 261 insertions, 271 deletions
diff --git a/src/cpu/amd/pi/spi.c b/src/cpu/amd/pi/spi.c index 71b5d95444..8ed0a377c9 100644 --- a/src/cpu/amd/pi/spi.c +++ b/src/cpu/amd/pi/spi.c @@ -21,23 +21,22 @@ void spi_SaveS3info(u32 pos, u32 size, u8 *buf, u32 len) { - struct spi_flash *flash; + struct spi_flash flash; spi_init(); - flash = spi_flash_probe(0, 0); - if (!flash) { + if (spi_flash_probe(0, 0, &flash)) { printk(BIOS_DEBUG, "Could not find SPI device\n"); /* Dont make flow stop. */ return; } - spi_flash_volatile_group_begin(flash); + spi_flash_volatile_group_begin(&flash); - spi_flash_erase(flash, pos, size); - spi_flash_write(flash, pos, sizeof(len), &len); - spi_flash_write(flash, pos + sizeof(len), len, buf); + spi_flash_erase(&flash, pos, size); + spi_flash_write(&flash, pos, sizeof(len), &len); + spi_flash_write(&flash, pos + sizeof(len), len, buf); - spi_flash_volatile_group_end(flash); + spi_flash_volatile_group_end(&flash); return; } diff --git a/src/drivers/intel/fsp1_0/fastboot_cache.c b/src/drivers/intel/fsp1_0/fastboot_cache.c index b57f382b27..906b356b90 100644 --- a/src/drivers/intel/fsp1_0/fastboot_cache.c +++ b/src/drivers/intel/fsp1_0/fastboot_cache.c @@ -157,6 +157,7 @@ void update_mrc_cache(void *unused) struct mrc_data_container *current = cbmem_find(CBMEM_ID_MRCDATA); struct mrc_data_container *cache, *cache_base; u32 cache_size; + struct spi_flash flash; if (!current) { printk(BIOS_ERR, "No fast boot cache in cbmem. Can't update flash.\n"); @@ -189,8 +190,7 @@ void update_mrc_cache(void *unused) /* 1. use spi_flash_probe() to find the flash, then... */ spi_init(); - struct spi_flash *flash = spi_flash_probe(0, 0); - if (!flash) { + if (spi_flash_probe(0, 0, &flash)) { printk(BIOS_DEBUG, "Could not find SPI device\n"); return; } @@ -209,7 +209,8 @@ void update_mrc_cache(void *unused) "Need to erase the MRC cache region of %d bytes at %p\n", cache_size, cache_base); - spi_flash_erase(flash, to_flash_offset(cache_base), cache_size); + spi_flash_erase(&flash, to_flash_offset(cache_base), + cache_size); /* we will start at the beginning again */ cache = cache_base; @@ -217,7 +218,7 @@ void update_mrc_cache(void *unused) /* 4. write mrc data with spi_flash_write() */ printk(BIOS_DEBUG, "Write MRC cache update to flash at %p\n", cache); - spi_flash_write(flash, to_flash_offset(cache), + spi_flash_write(&flash, to_flash_offset(cache), current->mrc_data_size + sizeof(*current), current); } diff --git a/src/drivers/spi/adesto.c b/src/drivers/spi/adesto.c index c5c68b546b..d062c36594 100644 --- a/src/drivers/spi/adesto.c +++ b/src/drivers/spi/adesto.c @@ -126,10 +126,10 @@ out: return ret; } -struct spi_flash *spi_flash_probe_adesto(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_adesto(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct adesto_spi_flash_params *params; - struct spi_flash *flash; unsigned int i; for (i = 0; i < ARRAY_SIZE(adesto_spi_flash_table); i++) { @@ -141,13 +141,7 @@ struct spi_flash *spi_flash_probe_adesto(struct spi_slave *spi, u8 *idcode) if (i == ARRAY_SIZE(adesto_spi_flash_table)) { printk(BIOS_WARNING, "SF: Unsupported adesto ID %02x%02x\n", idcode[1], idcode[2]); - return NULL; - } - - flash = malloc(sizeof(*flash)); - if (!flash) { - printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); - return NULL; + return -1; } memcpy(&flash->spi, spi, sizeof(*spi)); @@ -167,5 +161,5 @@ struct spi_flash *spi_flash_probe_adesto(struct spi_slave *spi, u8 *idcode) flash->internal_read = spi_flash_cmd_read_fast; #endif - return flash; + return 0; } diff --git a/src/drivers/spi/amic.c b/src/drivers/spi/amic.c index 60843608f5..cf5a29601d 100644 --- a/src/drivers/spi/amic.c +++ b/src/drivers/spi/amic.c @@ -109,11 +109,11 @@ out: return ret; } -struct spi_flash *spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct amic_spi_flash_params *params; unsigned int i; - struct spi_flash *flash; for (i = 0; i < ARRAY_SIZE(amic_spi_flash_table); i++) { params = &amic_spi_flash_table[i]; @@ -124,13 +124,7 @@ struct spi_flash *spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode) if (i == ARRAY_SIZE(amic_spi_flash_table)) { printk(BIOS_WARNING, "SF: Unsupported AMIC ID %02x%02x\n", idcode[1], idcode[2]); - return NULL; - } - - flash = malloc(sizeof(*flash)); - if (!flash) { - printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); - return NULL; + return -1; } memcpy(&flash->spi, spi, sizeof(*spi)); @@ -151,5 +145,5 @@ struct spi_flash *spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode) flash->internal_read = spi_flash_cmd_read_fast; #endif - return flash; + return 0; } diff --git a/src/drivers/spi/atmel.c b/src/drivers/spi/atmel.c index 4f614ff283..edc172e65f 100644 --- a/src/drivers/spi/atmel.c +++ b/src/drivers/spi/atmel.c @@ -154,11 +154,11 @@ out: return ret; } -struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct atmel_spi_flash_params *params; unsigned int i; - struct spi_flash *flash; for (i = 0; i < ARRAY_SIZE(atmel_spi_flash_table); i++) { params = &atmel_spi_flash_table[i]; @@ -169,13 +169,7 @@ struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode) if (i == ARRAY_SIZE(atmel_spi_flash_table)) { printk(BIOS_WARNING, "SF: Unsupported Atmel ID %02x%02x\n", idcode[1], idcode[2]); - return NULL; - } - - flash = malloc(sizeof(*flash)); - if (!flash) { - printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); - return NULL; + return -1; } memcpy(&flash->spi, spi, sizeof(*spi)); @@ -196,5 +190,5 @@ struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode) flash->internal_read = spi_flash_cmd_read_fast; #endif - return flash; + return 0; } diff --git a/src/drivers/spi/boot_device_rw_nommap.c b/src/drivers/spi/boot_device_rw_nommap.c index e7d9b9a944..64d81c5b00 100644 --- a/src/drivers/spi/boot_device_rw_nommap.c +++ b/src/drivers/spi/boot_device_rw_nommap.c @@ -17,13 +17,15 @@ #include <boot_device.h> #include <spi_flash.h> #include <spi-generic.h> +#include <stdint.h> -static struct spi_flash *sfg CAR_GLOBAL; +static struct spi_flash sfg CAR_GLOBAL; +static bool sfg_init_done CAR_GLOBAL; static ssize_t spi_readat(const struct region_device *rd, void *b, size_t offset, size_t size) { - struct spi_flash *sf = car_get_var(sfg); + struct spi_flash *sf = car_get_var_ptr(&sfg); if (sf == NULL) return -1; @@ -37,7 +39,7 @@ static ssize_t spi_readat(const struct region_device *rd, void *b, static ssize_t spi_writeat(const struct region_device *rd, const void *b, size_t offset, size_t size) { - struct spi_flash *sf = car_get_var(sfg); + struct spi_flash *sf = car_get_var_ptr(&sfg); if (sf == NULL) return -1; @@ -51,7 +53,7 @@ static ssize_t spi_writeat(const struct region_device *rd, const void *b, static ssize_t spi_eraseat(const struct region_device *rd, size_t offset, size_t size) { - struct spi_flash *sf = car_get_var(sfg); + struct spi_flash *sf = car_get_var_ptr(&sfg); if (sf == NULL) return -1; @@ -76,13 +78,14 @@ static void boot_device_rw_init(void) const int bus = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS; const int cs = 0; - if (car_get_var(sfg) != NULL) + if (car_get_var(sfg_init_done) == true) return; /* Ensure any necessary setup is performed by the drivers. */ spi_init(); - car_set_var(sfg, spi_flash_probe(bus, cs)); + if (!spi_flash_probe(bus, cs, car_get_var_ptr(&sfg))) + car_set_var(sfg_init_done, true); } const struct region_device *boot_device_rw(void) @@ -90,7 +93,7 @@ const struct region_device *boot_device_rw(void) /* Probe for the SPI flash device if not already done. */ boot_device_rw_init(); - if (car_get_var(sfg) == NULL) + if (car_get_var(sfg_init_done) != true) return NULL; return &spi_rw; @@ -99,5 +102,9 @@ const struct region_device *boot_device_rw(void) const struct spi_flash *boot_device_spi_flash(void) { boot_device_rw_init(); - return car_get_var(sfg); + + if (car_get_var(sfg_init_done) != true) + return NULL; + + return car_get_var_ptr(&sfg); } diff --git a/src/drivers/spi/cbfs_spi.c b/src/drivers/spi/cbfs_spi.c index ae2994c257..3e5f2af889 100644 --- a/src/drivers/spi/cbfs_spi.c +++ b/src/drivers/spi/cbfs_spi.c @@ -24,9 +24,11 @@ #include <spi_flash.h> #include <symbols.h> #include <cbmem.h> +#include <stdint.h> #include <timer.h> -static struct spi_flash *spi_flash_info; +static struct spi_flash spi_flash_info; +static bool spi_flash_init_done; /* * Set this to 1 to debug SPI speed, 0 to disable it @@ -47,7 +49,7 @@ static ssize_t spi_readat(const struct region_device *rd, void *b, if (show) stopwatch_init(&sw); - if (spi_flash_read(spi_flash_info, offset, size, b)) + if (spi_flash_read(&spi_flash_info, offset, size, b)) return -1; if (show) { long usecs; @@ -68,7 +70,7 @@ static ssize_t spi_readat(const struct region_device *rd, void *b, static ssize_t spi_writeat(const struct region_device *rd, const void *b, size_t offset, size_t size) { - if (spi_flash_write(spi_flash_info, offset, size, b)) + if (spi_flash_write(&spi_flash_info, offset, size, b)) return -1; return size; } @@ -76,7 +78,7 @@ static ssize_t spi_writeat(const struct region_device *rd, const void *b, static ssize_t spi_eraseat(const struct region_device *rd, size_t offset, size_t size) { - if (spi_flash_erase(spi_flash_info, offset, size)) + if (spi_flash_erase(&spi_flash_info, offset, size)) return -1; return size; } @@ -112,10 +114,13 @@ void boot_device_init(void) int bus = CONFIG_BOOT_DEVICE_SPI_FLASH_BUS; int cs = 0; - if (spi_flash_info != NULL) + if (spi_flash_init_done == true) return; - spi_flash_info = spi_flash_probe(bus, cs); + if (spi_flash_probe(bus, cs, &spi_flash_info)) + return; + + spi_flash_init_done = true; mmap_helper_device_init(&mdev, _cbfs_cache, _cbfs_cache_size); } @@ -123,7 +128,7 @@ void boot_device_init(void) /* Return the CBFS boot device. */ const struct region_device *boot_device_ro(void) { - if (spi_flash_info == NULL) + if (spi_flash_init_done != true) return NULL; return &mdev.rdev; @@ -138,5 +143,9 @@ const struct region_device *boot_device_rw(void) const struct spi_flash *boot_device_spi_flash(void) { boot_device_init(); - return spi_flash_info; + + if (spi_flash_init_done != true) + return NULL; + + return &spi_flash_info; } diff --git a/src/drivers/spi/eon.c b/src/drivers/spi/eon.c index 8ad5f84bc8..f5a3af6892 100644 --- a/src/drivers/spi/eon.c +++ b/src/drivers/spi/eon.c @@ -126,10 +126,10 @@ out: return ret; } -struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct eon_spi_flash_params *params; - struct spi_flash *flash; unsigned int i; for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) { @@ -141,13 +141,7 @@ struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) if (i == ARRAY_SIZE(eon_spi_flash_table)) { printk(BIOS_WARNING, "SF: Unsupported EON ID %#02x%02x\n", idcode[1], idcode[2]); - return NULL; - } - - flash = malloc(sizeof(*flash)); - if (!flash) { - printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); - return NULL; + return -1; } memcpy(&flash->spi, spi, sizeof(*spi)); @@ -164,5 +158,5 @@ struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) flash->internal_status = spi_flash_cmd_status; flash->internal_read = spi_flash_cmd_read_fast; - return flash; + return 0; } diff --git a/src/drivers/spi/gigadevice.c b/src/drivers/spi/gigadevice.c index bd6d6edb28..0b7173d608 100644 --- a/src/drivers/spi/gigadevice.c +++ b/src/drivers/spi/gigadevice.c @@ -170,9 +170,8 @@ out: return ret; } -static struct spi_flash flash; - -struct spi_flash *spi_flash_probe_gigadevice(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_gigadevice(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct gigadevice_spi_flash_params *params; unsigned int i; @@ -187,28 +186,28 @@ struct spi_flash *spi_flash_probe_gigadevice(struct spi_slave *spi, u8 *idcode) printk(BIOS_WARNING, "SF gigadevice.c: Unsupported ID %#02x%02x\n", idcode[1], idcode[2]); - return NULL; + return -1; } - memcpy(&flash.spi, spi, sizeof(*spi)); - flash.name = params->name; + memcpy(&flash->spi, spi, sizeof(*spi)); + flash->name = params->name; /* Assuming power-of-two page size initially. */ - flash.page_size = 1 << params->l2_page_size; - flash.sector_size = flash.page_size * params->pages_per_sector; - flash.size = flash.sector_size * params->sectors_per_block * + flash->page_size = 1 << params->l2_page_size; + flash->sector_size = flash->page_size * params->pages_per_sector; + flash->size = flash->sector_size * params->sectors_per_block * params->nr_blocks; - flash.erase_cmd = CMD_GD25_SE; - flash.status_cmd = CMD_GD25_RDSR; + flash->erase_cmd = CMD_GD25_SE; + flash->status_cmd = CMD_GD25_RDSR; - flash.internal_write = gigadevice_write; - flash.internal_erase = spi_flash_cmd_erase; - flash.internal_status = spi_flash_cmd_status; + flash->internal_write = gigadevice_write; + flash->internal_erase = spi_flash_cmd_erase; + flash->internal_status = spi_flash_cmd_status; #if CONFIG_SPI_FLASH_NO_FAST_READ - flash.internal_read = spi_flash_cmd_read_slow; + flash->internal_read = spi_flash_cmd_read_slow; #else - flash.internal_read = spi_flash_cmd_read_fast; + flash->internal_read = spi_flash_cmd_read_fast; #endif - return &flash; + return 0; } diff --git a/src/drivers/spi/macronix.c b/src/drivers/spi/macronix.c index 6e17719fa8..87b6fd7c42 100644 --- a/src/drivers/spi/macronix.c +++ b/src/drivers/spi/macronix.c @@ -192,9 +192,8 @@ static int macronix_write(const struct spi_flash *flash, u32 offset, size_t len, return ret; } -static struct spi_flash flash; - -struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct macronix_spi_flash_params *params; unsigned int i; @@ -208,26 +207,26 @@ struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode) if (i == ARRAY_SIZE(macronix_spi_flash_table)) { printk(BIOS_WARNING, "SF: Unsupported Macronix ID %04x\n", id); - return NULL; + return -1; } - memcpy(&flash.spi, spi, sizeof(*spi)); - flash.name = params->name; - flash.page_size = params->page_size; - flash.sector_size = params->page_size * params->pages_per_sector; - flash.size = flash.sector_size * params->sectors_per_block * + memcpy(&flash->spi, spi, sizeof(*spi)); + flash->name = params->name; + flash->page_size = params->page_size; + flash->sector_size = params->page_size * params->pages_per_sector; + flash->size = flash->sector_size * params->sectors_per_block * params->nr_blocks; - flash.erase_cmd = CMD_MX25XX_SE; - flash.status_cmd = CMD_MX25XX_RDSR; + flash->erase_cmd = CMD_MX25XX_SE; + flash->status_cmd = CMD_MX25XX_RDSR; - flash.internal_write = macronix_write; - flash.internal_erase = spi_flash_cmd_erase; - flash.internal_status = spi_flash_cmd_status; + flash->internal_write = macronix_write; + flash->internal_erase = spi_flash_cmd_erase; + flash->internal_status = spi_flash_cmd_status; #if CONFIG_SPI_FLASH_NO_FAST_READ - flash.internal_read = spi_flash_cmd_read_slow; + flash->internal_read = spi_flash_cmd_read_slow; #else - flash.internal_read = spi_flash_cmd_read_fast; + flash->internal_read = spi_flash_cmd_read_fast; #endif - return &flash; + return 0; } diff --git a/src/drivers/spi/spansion.c b/src/drivers/spi/spansion.c index 8f9e96687d..a376b6e0a4 100644 --- a/src/drivers/spi/spansion.c +++ b/src/drivers/spi/spansion.c @@ -246,9 +246,8 @@ static int spansion_write(const struct spi_flash *flash, u32 offset, size_t len, return ret; } -static struct spi_flash flash; - -struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct spansion_spi_flash_params *params; unsigned int i; @@ -263,21 +262,21 @@ struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode) printk(BIOS_WARNING, "SF: Unsupported SPANSION ID %02x %02x %02x %02x %02x\n", idcode[0], idcode[1], idcode[2], idcode[3], idcode[4]); - return NULL; + return -1; } - memcpy(&flash.spi, spi, sizeof(*spi)); - flash.name = params->name; - flash.page_size = params->page_size; - flash.sector_size = params->page_size * params->pages_per_sector; - flash.size = flash.sector_size * params->nr_sectors; - flash.erase_cmd = CMD_S25FLXX_SE; - flash.status_cmd = CMD_S25FLXX_RDSR; + memcpy(&flash->spi, spi, sizeof(*spi)); + flash->name = params->name; + flash->page_size = params->page_size; + flash->sector_size = params->page_size * params->pages_per_sector; + flash->size = flash->sector_size * params->nr_sectors; + flash->erase_cmd = CMD_S25FLXX_SE; + flash->status_cmd = CMD_S25FLXX_RDSR; - flash.internal_write = spansion_write; - flash.internal_erase = spi_flash_cmd_erase; - flash.internal_read = spi_flash_cmd_read_slow; - flash.internal_status = spi_flash_cmd_status; + flash->internal_write = spansion_write; + flash->internal_erase = spi_flash_cmd_erase; + flash->internal_read = spi_flash_cmd_read_slow; + flash->internal_status = spi_flash_cmd_status; - return &flash; + return 0; } diff --git a/src/drivers/spi/spi_flash.c b/src/drivers/spi/spi_flash.c index e84df73c29..701033aaf3 100644 --- a/src/drivers/spi/spi_flash.c +++ b/src/drivers/spi/spi_flash.c @@ -240,7 +240,8 @@ int spi_flash_cmd_status(const struct spi_flash *flash, u8 *reg) static struct { const u8 shift; const u8 idcode; - struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode); + int (*probe) (struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); } flashes[] = { /* Keep it sorted by define name */ #if CONFIG_SPI_FLASH_AMIC @@ -280,24 +281,24 @@ static struct { }; #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN) -struct spi_flash * +int __attribute__((weak)) spi_flash_programmer_probe(struct spi_slave *spi, - int force) + int force, + struct spi_flash *flash) { /* Default weak implementation. Do nothing. */ - return NULL; + return -1; } -static struct spi_flash *__spi_flash_probe(struct spi_slave *spi) +static int __spi_flash_probe(struct spi_slave *spi, struct spi_flash *flash) { int ret, i, shift; u8 idcode[IDCODE_LEN], *idp; - struct spi_flash *flash = NULL; /* Read the ID codes */ ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode)); if (ret) - return NULL; + return -1; if (IS_ENABLED(CONFIG_DEBUG_SPI_FLASH)) { printk(BIOS_SPEW, "SF: Got idcode: "); @@ -317,45 +318,44 @@ static struct spi_flash *__spi_flash_probe(struct spi_slave *spi) for (i = 0; i < ARRAY_SIZE(flashes); ++i) if (flashes[i].shift == shift && flashes[i].idcode == *idp) { /* we have a match, call probe */ - flash = flashes[i].probe(spi, idp); - if (flash) - break; + if (flashes[i].probe(spi, idp, flash) == 0) + return 0; } - return flash; + /* No match, return error. */ + return -1; } -struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs) +int spi_flash_probe(unsigned int bus, unsigned int cs, struct spi_flash *flash) { struct spi_slave spi; - struct spi_flash *flash; if (spi_setup_slave(bus, cs, &spi)) { printk(BIOS_WARNING, "SF: Failed to set up slave\n"); - return NULL; + return -1; } /* Try special programmer probe if any (without force). */ - flash = spi_flash_programmer_probe(&spi, 0); + if (spi_flash_programmer_probe(&spi, 0, flash) == 0) + goto flash_found; /* If flash is not found, try generic spi flash probe. */ - if (!flash) - flash = __spi_flash_probe(&spi); + if (__spi_flash_probe(&spi, flash) == 0) + goto flash_found; /* If flash is not yet found, force special programmer probe if any. */ - if (!flash) - flash = spi_flash_programmer_probe(&spi, 1); + if (spi_flash_programmer_probe(&spi, 1, flash) == 0) + goto flash_found; /* Give up -- nothing more to try if flash is not found. */ - if (!flash) { - printk(BIOS_WARNING, "SF: Unsupported manufacturer!\n"); - return NULL; - } + printk(BIOS_WARNING, "SF: Unsupported manufacturer!\n"); + return -1; +flash_found: printk(BIOS_INFO, "SF: Detected %s with sector size 0x%x, total 0x%x\n", flash->name, flash->sector_size, flash->size); - return flash; + return 0; } int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len, diff --git a/src/drivers/spi/spi_flash_internal.h b/src/drivers/spi/spi_flash_internal.h index 9b517238b2..4ad0b09ca3 100644 --- a/src/drivers/spi/spi_flash_internal.h +++ b/src/drivers/spi/spi_flash_internal.h @@ -64,17 +64,27 @@ int spi_flash_cmd_erase(const struct spi_flash *flash, u32 offset, size_t len); int spi_flash_cmd_status(const struct spi_flash *flash, u8 *reg); /* Manufacturer-specific probe functions */ -struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_flash_probe_gigadevice(struct spi_slave *spi, - u8 *idcode); -struct spi_flash *spi_flash_probe_adesto(struct spi_slave *spi, u8 *idcode); -struct spi_flash *spi_fram_probe_ramtron(struct spi_slave *spi, u8 *idcode); +int spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_amic(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_atmel(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_stmicro(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_gigadevice(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_flash_probe_adesto(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); +int spi_fram_probe_ramtron(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash); #endif /* SPI_FLASH_INTERNAL_H */ diff --git a/src/drivers/spi/sst.c b/src/drivers/spi/sst.c index 0683846ce9..bf211571e1 100644 --- a/src/drivers/spi/sst.c +++ b/src/drivers/spi/sst.c @@ -314,11 +314,10 @@ sst_unlock(const struct spi_flash *flash) return ret; } -struct spi_flash * -spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct sst_spi_flash_params *params; - struct spi_flash *flash; size_t i; for (i = 0; i < ARRAY_SIZE(sst_spi_flash_table); ++i) { @@ -329,13 +328,7 @@ spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode) if (i == ARRAY_SIZE(sst_spi_flash_table)) { printk(BIOS_WARNING, "SF: Unsupported SST ID %02x\n", idcode[1]); - return NULL; - } - - flash = malloc(sizeof(*flash)); - if (!flash) { - printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); - return NULL; + return -1; } memcpy(&flash->spi, spi, sizeof(*spi)); @@ -353,5 +346,5 @@ spi_flash_probe_sst(struct spi_slave *spi, u8 *idcode) /* Flash powers up read-only, so clear BP# bits */ sst_unlock(flash); - return flash; + return 0; } diff --git a/src/drivers/spi/stmicro.c b/src/drivers/spi/stmicro.c index 2ec9f18376..b7ceba21a8 100644 --- a/src/drivers/spi/stmicro.c +++ b/src/drivers/spi/stmicro.c @@ -222,9 +222,8 @@ out: return ret; } -static struct spi_flash flash; - -struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode) +int spi_flash_probe_stmicro(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct stmicro_spi_flash_params *params; unsigned int i; @@ -232,13 +231,13 @@ struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode) if (idcode[0] == 0xff) { i = spi_flash_cmd(spi, CMD_M25PXX_RES, idcode, 4); if (i) - return NULL; + return -1; if ((idcode[3] & 0xf0) == 0x10) { idcode[0] = 0x20; idcode[1] = 0x20; idcode[2] = idcode[3] + 1; } else - return NULL; + return -1; } for (i = 0; i < ARRAY_SIZE(stmicro_spi_flash_table); i++) { @@ -251,19 +250,19 @@ struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode) if (i == ARRAY_SIZE(stmicro_spi_flash_table)) { printk(BIOS_WARNING, "SF: Unsupported STMicro ID %02x%02x\n", idcode[1], idcode[2]); - return NULL; + return -1; } - memcpy(&flash.spi, spi, sizeof(*spi)); - flash.name = params->name; - flash.page_size = params->page_size; - flash.sector_size = params->page_size * params->pages_per_sector; - flash.size = flash.sector_size * params->nr_sectors; - flash.erase_cmd = params->op_erase; + memcpy(&flash->spi, spi, sizeof(*spi)); + flash->name = params->name; + flash->page_size = params->page_size; + flash->sector_size = params->page_size * params->pages_per_sector; + flash->size = flash->sector_size * params->nr_sectors; + flash->erase_cmd = params->op_erase; - flash.internal_write = stmicro_write; - flash.internal_erase = spi_flash_cmd_erase; - flash.internal_read = spi_flash_cmd_read_fast; + flash->internal_write = stmicro_write; + flash->internal_erase = spi_flash_cmd_erase; + flash->internal_read = spi_flash_cmd_read_fast; - return &flash; + return 0; } diff --git a/src/drivers/spi/winbond.c b/src/drivers/spi/winbond.c index 3f167c0d06..c9d4aad4d6 100644 --- a/src/drivers/spi/winbond.c +++ b/src/drivers/spi/winbond.c @@ -184,9 +184,8 @@ out: return ret; } -static struct spi_flash flash; - -struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode) +int spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode, + struct spi_flash *flash) { const struct winbond_spi_flash_params *params; unsigned int i; @@ -200,27 +199,27 @@ struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode) if (i == ARRAY_SIZE(winbond_spi_flash_table)) { printk(BIOS_WARNING, "SF: Unsupported Winbond ID %02x%02x\n", idcode[1], idcode[2]); - return NULL; + return -1; } - memcpy(&flash.spi, spi, sizeof(*spi)); - flash.name = params->name; + memcpy(&flash->spi, spi, sizeof(*spi)); + flash->name = params->name; /* Assuming power-of-two page size initially. */ - flash.page_size = 1 << params->l2_page_size; - flash.sector_size = flash.page_size * params->pages_per_sector; - flash.size = flash.sector_size * params->sectors_per_block * + flash->page_size = 1 << params->l2_page_size; + flash->sector_size = flash->page_size * params->pages_per_sector; + flash->size = flash->sector_size * params->sectors_per_block * params->nr_blocks; - flash.erase_cmd = CMD_W25_SE; - flash.status_cmd = CMD_W25_RDSR; + flash->erase_cmd = CMD_W25_SE; + flash->status_cmd = CMD_W25_RDSR; - flash.internal_write = winbond_write; - flash.internal_erase = spi_flash_cmd_erase; - flash.internal_status = spi_flash_cmd_status; + flash->internal_write = winbond_write; + flash->internal_erase = spi_flash_cmd_erase; + flash->internal_status = spi_flash_cmd_status; #if CONFIG_SPI_FLASH_NO_FAST_READ - flash.internal_read = spi_flash_cmd_read_slow; + flash->internal_read = spi_flash_cmd_read_slow; #else - flash.internal_read = spi_flash_cmd_read_fast; + flash->internal_read = spi_flash_cmd_read_fast; #endif - return &flash; + return 0; } diff --git a/src/include/spi_flash.h b/src/include/spi_flash.h index a72a13e0f8..3b18717a86 100644 --- a/src/include/spi_flash.h +++ b/src/include/spi_flash.h @@ -50,14 +50,36 @@ struct spi_flash { void lb_spi_flash(struct lb_header *header); /* SPI Flash Driver Public API */ -struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs); + +/* + * Probe for SPI flash chip on given SPI bus and chip select and fill info in + * spi_flash structure. + * + * Params: + * bus = SPI Bus # for the flash chip + * cs = Chip select # for the flash chip + * flash = Pointer to spi flash structure that needs to be filled + * + * Return value: + * 0 = success + * non-zero = error + */ +int spi_flash_probe(unsigned int bus, unsigned int cs, struct spi_flash *flash); /* * Specialized probing performed by platform. This is a weak function which can * be overriden by platform driver. + * + * Params: * spi = Pointer to spi_slave structure. * force = Indicates if the platform driver can skip specialized probing. + * flash = Pointer to spi_flash structure that needs to be filled. + * + * Return value: + * 0 = success + * non-zero = error */ -struct spi_flash *spi_flash_programmer_probe(struct spi_slave *spi, int force); +int spi_flash_programmer_probe(struct spi_slave *spi, int force, + struct spi_flash *flash); /* All the following functions return 0 on success and non-zero on error. */ int spi_flash_read(const struct spi_flash *flash, u32 offset, size_t len, diff --git a/src/northbridge/amd/agesa/oem_s3.c b/src/northbridge/amd/agesa/oem_s3.c index e3d58c21d0..02c384ac8b 100644 --- a/src/northbridge/amd/agesa/oem_s3.c +++ b/src/northbridge/amd/agesa/oem_s3.c @@ -96,20 +96,19 @@ AGESA_STATUS OemS3LateRestore(AMD_S3_PARAMS *dataBlock) static int spi_SaveS3info(u32 pos, u32 size, u8 *buf, u32 len) { #if IS_ENABLED(CONFIG_SPI_FLASH) - struct spi_flash *flash; + struct spi_flash flash; spi_init(); - flash = spi_flash_probe(0, 0); - if (!flash) + if (spi_flash_probe(0, 0, &flash)) return -1; - spi_flash_volatile_group_begin(flash); + spi_flash_volatile_group_begin(&flash); - spi_flash_erase(flash, pos, size); - spi_flash_write(flash, pos, sizeof(len), &len); - spi_flash_write(flash, pos + sizeof(len), len, buf); + spi_flash_erase(&flash, pos, size); + spi_flash_write(&flash, pos, sizeof(len), &len); + spi_flash_write(&flash, pos + sizeof(len), len, buf); - spi_flash_volatile_group_end(flash); + spi_flash_volatile_group_end(&flash); return 0; #else return -1; diff --git a/src/northbridge/amd/amdmct/mct_ddr3/s3utils.c b/src/northbridge/amd/amdmct/mct_ddr3/s3utils.c index f69b6c4496..4100b2637d 100644 --- a/src/northbridge/amd/amdmct/mct_ddr3/s3utils.c +++ b/src/northbridge/amd/amdmct/mct_ddr3/s3utils.c @@ -1100,7 +1100,7 @@ int8_t save_mct_information_to_nvram(void) printk(BIOS_DEBUG, "Writing AMD DCT configuration to Flash\n"); - struct spi_flash *flash; + struct spi_flash flash; ssize_t s3nv_offset; struct amd_s3_persistent_data *persistent_data; @@ -1140,23 +1140,22 @@ int8_t save_mct_information_to_nvram(void) /* Initialize SPI and detect devices */ spi_init(); - flash = spi_flash_probe(0, 0); - if (!flash) { + if (spi_flash_probe(0, 0, &flash)) { printk(BIOS_DEBUG, "Could not find SPI device\n"); return -1; } - spi_flash_volatile_group_begin(flash); + spi_flash_volatile_group_begin(&flash); /* Erase and write data structure */ - spi_flash_erase(flash, s3nv_offset, CONFIG_S3_DATA_SIZE); - spi_flash_write(flash, s3nv_offset, + spi_flash_erase(&flash, s3nv_offset, CONFIG_S3_DATA_SIZE); + spi_flash_write(&flash, s3nv_offset, sizeof(struct amd_s3_persistent_data), persistent_data); /* Deallocate temporary data structures */ free(persistent_data); - spi_flash_volatile_group_end(flash); + spi_flash_volatile_group_end(&flash); /* Allow training bypass if DIMM configuration is unchanged on next boot */ nvram = 1; diff --git a/src/northbridge/intel/common/mrc_cache.c b/src/northbridge/intel/common/mrc_cache.c index a15812311b..2fc8d96ee5 100644 --- a/src/northbridge/intel/common/mrc_cache.c +++ b/src/northbridge/intel/common/mrc_cache.c @@ -160,6 +160,7 @@ static void update_mrc_cache(void *unused) struct mrc_data_container *cache, *cache_base; u32 cache_size; int ret; + struct spi_flash flash; if (!current) { printk(BIOS_ERR, "No MRC cache in cbmem. Can't update flash.\n"); @@ -192,8 +193,7 @@ static void update_mrc_cache(void *unused) // 1. use spi_flash_probe() to find the flash, then spi_init(); - struct spi_flash *flash = spi_flash_probe(0, 0); - if (!flash) { + if (spi_flash_probe(0, 0, &flash)) { printk(BIOS_DEBUG, "Could not find SPI device\n"); return; } @@ -212,7 +212,7 @@ static void update_mrc_cache(void *unused) "Need to erase the MRC cache region of %d bytes at %p\n", cache_size, cache_base); - spi_flash_erase(flash, to_flash_offset(flash, cache_base), + spi_flash_erase(&flash, to_flash_offset(&flash, cache_base), cache_size); /* we will start at the beginning again */ @@ -221,7 +221,7 @@ static void update_mrc_cache(void *unused) // 4. write mrc data with flash->write() printk(BIOS_DEBUG, "Finally: write MRC cache update to flash at %p\n", cache); - ret = spi_flash_write(flash, to_flash_offset(flash, cache), + ret = spi_flash_write(&flash, to_flash_offset(&flash, cache), current->mrc_data_size + sizeof(*current), current); if (ret) diff --git a/src/soc/broadcom/cygnus/ddr_init.c b/src/soc/broadcom/cygnus/ddr_init.c index b6bbf4a86f..5c4c985366 100644 --- a/src/soc/broadcom/cygnus/ddr_init.c +++ b/src/soc/broadcom/cygnus/ddr_init.c @@ -717,7 +717,7 @@ static int write_shmoo_to_flash(void *buf, int length) static int write_shmoo_to_flash(void *buf, int length) { - struct spi_flash *flash; + struct spi_flash flash; int erase = 0; volatile uint32_t *flptr; int i, j, ret = 0; @@ -734,13 +734,7 @@ static int write_shmoo_to_flash(void *buf, int length) } /* Probe flash */ - flash = spi_flash_probe( - CONFIG_ENV_SPI_BUS, - CONFIG_ENV_SPI_CS, - CONFIG_ENV_SPI_MAX_HZ, - CONFIG_ENV_SPI_MODE - ); - if (!flash) { + if (spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS, &flash)) { printk(BIOS_ERR, "Failed to initialize SPI flash for saving Shmoo values!\n"); return -1; } @@ -748,26 +742,22 @@ static int write_shmoo_to_flash(void *buf, int length) /* Erase if necessary */ if (erase) { ret = spi_flash_erase( - flash, + &flash, offset / flash->sector_size * flash->sector_size, flash->sector_size ); if (ret) { printk(BIOS_ERR, "SPI flash erase failed, error=%d\n", ret); - spi_flash_free(flash); return ret; } } /* Write data */ - ret = spi_flash_write(flash, offset, length, buf); + ret = spi_flash_write(&flash, offset, length, buf); if (ret) { printk(BIOS_ERR, "SPI flash write failed, error=%d\n", ret); } - /* Free flash instance */ - spi_flash_free(flash); - return ret; } diff --git a/src/soc/intel/common/block/fast_spi/fast_spi_flash.c b/src/soc/intel/common/block/fast_spi/fast_spi_flash.c index 96a808d423..9f973b91ef 100644 --- a/src/soc/intel/common/block/fast_spi/fast_spi_flash.c +++ b/src/soc/intel/common/block/fast_spi/fast_spi_flash.c @@ -169,9 +169,6 @@ static size_t get_xfer_len(const struct spi_flash *flash, uint32_t addr, } -/* Flash device operations. */ -static struct spi_flash boot_flash CAR_GLOBAL; - static int fast_spi_flash_erase(const struct spi_flash *flash, uint32_t offset, size_t len) { @@ -283,14 +280,12 @@ static int fast_spi_flash_status(const struct spi_flash *flash, * The size of the flash component is always taken from density field in the * SFDP table. FLCOMP.C0DEN is no longer used by the Flash Controller. */ -struct spi_flash *spi_flash_programmer_probe(struct spi_slave *dev, int force) +int spi_flash_programmer_probe(struct spi_slave *dev, + int force, struct spi_flash *flash) { BOILERPLATE_CREATE_CTX(ctx); - struct spi_flash *flash; uint32_t flash_bits; - flash = car_get_var_ptr(&boot_flash); - /* * bytes = (bits + 1) / 8; * But we need to do the addition in a way which doesn't overflow for @@ -317,7 +312,7 @@ struct spi_flash *spi_flash_programmer_probe(struct spi_slave *dev, int force) flash->internal_read = fast_spi_flash_read; flash->internal_status = fast_spi_flash_status; - return flash; + return 0; } int spi_flash_get_fpr_info(struct fpr_info *info) diff --git a/src/soc/intel/fsp_baytrail/nvm.c b/src/soc/intel/fsp_baytrail/nvm.c index 6f2fdbbd6a..a4a7998a0f 100644 --- a/src/soc/intel/fsp_baytrail/nvm.c +++ b/src/soc/intel/fsp_baytrail/nvm.c @@ -21,25 +21,27 @@ #include <spi-generic.h> #include <spi_flash.h> #include <soc/nvm.h> +#include <stdint.h> /* This module assumes the flash is memory mapped just below 4GiB in the * address space for reading. Also this module assumes an area it erased * when all bytes read as all 0xff's. */ -static struct spi_flash *flash; +static struct spi_flash flash; +static bool spi_flash_init_done; static int nvm_init(void) { - if (flash != NULL) + if (spi_flash_init_done == true) return 0; spi_init(); - flash = spi_flash_probe(0, 0); - if (!flash) { + if (spi_flash_probe(0, 0, &flash)) { printk(BIOS_DEBUG, "Could not find SPI device\n"); return -1; } + spi_flash_init_done = true; return 0; } @@ -67,7 +69,7 @@ int nvm_erase(void *start, size_t size) { if (nvm_init() < 0) return -1; - spi_flash_erase(flash, to_flash_offset(start), size); + spi_flash_erase(&flash, to_flash_offset(start), size); return 0; } @@ -76,6 +78,6 @@ int nvm_write(void *start, const void *data, size_t size) { if (nvm_init() < 0) return -1; - spi_flash_write(flash, to_flash_offset(start), size, data); + spi_flash_write(&flash, to_flash_offset(start), size, data); return 0; } diff --git a/src/soc/mediatek/mt8173/flash_controller.c b/src/soc/mediatek/mt8173/flash_controller.c index 09df1a47f7..29a1c2f9a7 100644 --- a/src/soc/mediatek/mt8173/flash_controller.c +++ b/src/soc/mediatek/mt8173/flash_controller.c @@ -228,24 +228,24 @@ static int nor_erase(const struct spi_flash *flash, u32 offset, size_t len) return 0; } -struct spi_flash *spi_flash_programmer_probe(struct spi_slave *spi, int force) +int spi_flash_programmer_probe(struct spi_slave *spi, + int force, struct spi_flash *flash) { - static struct spi_flash flash; static int done; if (done) - return &flash; + return 0; write32(&mt8173_nor->wrprot, SFLASH_COMMAND_ENABLE); - memcpy(&flash.spi, spi, sizeof(*spi)); - flash.name = "mt8173 flash controller"; - flash.internal_write = nor_write; - flash.internal_erase = nor_erase; - flash.internal_read = nor_read; - flash.internal_status = 0; - flash.sector_size = 0x1000; - flash.erase_cmd = SECTOR_ERASE_CMD; - flash.size = CONFIG_ROM_SIZE; + memcpy(&flash->spi, spi, sizeof(*spi)); + flash->name = "mt8173 flash controller"; + flash->internal_write = nor_write; + flash->internal_erase = nor_erase; + flash->internal_read = nor_read; + flash->internal_status = 0; + flash->sector_size = 0x1000; + flash->erase_cmd = SECTOR_ERASE_CMD; + flash->size = CONFIG_ROM_SIZE; done = 1; - return &flash; + return 0; } diff --git a/src/southbridge/intel/common/spi.c b/src/southbridge/intel/common/spi.c index 11318f7ec0..effcc80f1f 100644 --- a/src/southbridge/intel/common/spi.c +++ b/src/southbridge/intel/common/spi.c @@ -899,10 +899,9 @@ static int ich_hwseq_write(const struct spi_flash *flash, u32 addr, size_t len, return 0; } - -struct spi_flash *spi_flash_programmer_probe(struct spi_slave *spi, int force) +int spi_flash_programmer_probe(struct spi_slave *spi, + int force, struct spi_flash *flash) { - struct spi_flash *flash = NULL; uint32_t flcomp; /* @@ -911,13 +910,7 @@ struct spi_flash *spi_flash_programmer_probe(struct spi_slave *spi, int force) * 2. Specialized probing is forced by SPI flash driver. */ if (!spi_is_multichip() && !force) - return NULL; - - flash = malloc(sizeof(*flash)); - if (!flash) { - printk(BIOS_WARNING, "SF: Failed to allocate memory\n"); - return NULL; - } + return -1; memcpy(&flash->spi, spi, sizeof(*spi)); flash->name = "Opaque HW-sequencing"; @@ -951,5 +944,5 @@ struct spi_flash *spi_flash_programmer_probe(struct spi_slave *spi, int force) flash->size += 1 << (19 + ((flcomp >> 3) & 7)); printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size); - return flash; + return 0; } |