[coreboot-gerrit] Change in coreboot[master]: drivers/spi/spi_flash: Pass in flash structure to fill in probe

Furquan Shaikh (Code Review) gerrit at coreboot.org
Fri May 19 21:21:52 CEST 2017


Furquan Shaikh has submitted this change and it was merged. ( https://review.coreboot.org/19705 )

Change subject: drivers/spi/spi_flash: Pass in flash structure to fill in probe
......................................................................


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 at chromium.org>
Reviewed-on: https://review.coreboot.org/19705
Tested-by: build bot (Jenkins) <no-reply at coreboot.org>
Reviewed-by: Aaron Durbin <adurbin at chromium.org>
---
M src/cpu/amd/pi/spi.c
M src/drivers/intel/fsp1_0/fastboot_cache.c
M src/drivers/spi/adesto.c
M src/drivers/spi/amic.c
M src/drivers/spi/atmel.c
M src/drivers/spi/boot_device_rw_nommap.c
M src/drivers/spi/cbfs_spi.c
M src/drivers/spi/eon.c
M src/drivers/spi/gigadevice.c
M src/drivers/spi/macronix.c
M src/drivers/spi/spansion.c
M src/drivers/spi/spi_flash.c
M src/drivers/spi/spi_flash_internal.h
M src/drivers/spi/sst.c
M src/drivers/spi/stmicro.c
M src/drivers/spi/winbond.c
M src/include/spi_flash.h
M src/northbridge/amd/agesa/oem_s3.c
M src/northbridge/amd/amdmct/mct_ddr3/s3utils.c
M src/northbridge/intel/common/mrc_cache.c
M src/soc/broadcom/cygnus/ddr_init.c
M src/soc/intel/common/block/fast_spi/fast_spi_flash.c
M src/soc/intel/fsp_baytrail/nvm.c
M src/soc/mediatek/mt8173/flash_controller.c
M src/southbridge/intel/common/spi.c
25 files changed, 261 insertions(+), 271 deletions(-)

Approvals:
  Aaron Durbin: Looks good to me, approved
  build bot (Jenkins): Verified



diff --git a/src/cpu/amd/pi/spi.c b/src/cpu/amd/pi/spi.c
index 71b5d95..8ed0a37 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 b57f382..906b356 100644
--- a/src/drivers/intel/fsp1_0/fastboot_cache.c
+++ b/src/drivers/intel/fsp1_0/fastboot_cache.c
@@ -157,6 +157,7 @@
 	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 @@
 
 	/*  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 @@
 		       "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 @@
 	/*  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 c5c68b5..d062c36 100644
--- a/src/drivers/spi/adesto.c
+++ b/src/drivers/spi/adesto.c
@@ -126,10 +126,10 @@
 	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 @@
 	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 @@
 	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 6084360..cf5a296 100644
--- a/src/drivers/spi/amic.c
+++ b/src/drivers/spi/amic.c
@@ -109,11 +109,11 @@
 	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 @@
 	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 @@
 	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 4f614ff..edc172e 100644
--- a/src/drivers/spi/atmel.c
+++ b/src/drivers/spi/atmel.c
@@ -154,11 +154,11 @@
 	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 @@
 	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 @@
 	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 e7d9b9a..64d81c5 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_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_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 @@
 	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 @@
 	/* 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 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 ae2994c..3e5f2af 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 @@
 
 	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_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_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 @@
 	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 @@
 /* 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 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 8ad5f84..f5a3af6 100644
--- a/src/drivers/spi/eon.c
+++ b/src/drivers/spi/eon.c
@@ -126,10 +126,10 @@
 	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 @@
 	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 @@
 	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 bd6d6ed..0b7173d 100644
--- a/src/drivers/spi/gigadevice.c
+++ b/src/drivers/spi/gigadevice.c
@@ -170,9 +170,8 @@
 	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 @@
 		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 6e17719..87b6fd7 100644
--- a/src/drivers/spi/macronix.c
+++ b/src/drivers/spi/macronix.c
@@ -192,9 +192,8 @@
 	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 @@
 
 	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 8f9e966..a376b6e 100644
--- a/src/drivers/spi/spansion.c
+++ b/src/drivers/spi/spansion.c
@@ -246,9 +246,8 @@
 	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 @@
 		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 e84df73..701033a 100644
--- a/src/drivers/spi/spi_flash.c
+++ b/src/drivers/spi/spi_flash.c
@@ -240,7 +240,8 @@
 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 @@
 };
 #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 @@
 	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 9b51723..4ad0b09 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_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 0683846..bf21157 100644
--- a/src/drivers/spi/sst.c
+++ b/src/drivers/spi/sst.c
@@ -314,11 +314,10 @@
 	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 @@
 
 	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 @@
 	/* 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 2ec9f18..b7ceba2 100644
--- a/src/drivers/spi/stmicro.c
+++ b/src/drivers/spi/stmicro.c
@@ -222,9 +222,8 @@
 	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 @@
 	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 @@
 	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 3f167c0..c9d4aad 100644
--- a/src/drivers/spi/winbond.c
+++ b/src/drivers/spi/winbond.c
@@ -184,9 +184,8 @@
 	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 @@
 	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 a72a13e..3b18717 100644
--- a/src/include/spi_flash.h
+++ b/src/include/spi_flash.h
@@ -50,14 +50,36 @@
 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 e3d58c2..02c384a 100644
--- a/src/northbridge/amd/agesa/oem_s3.c
+++ b/src/northbridge/amd/agesa/oem_s3.c
@@ -96,20 +96,19 @@
 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 f69b6c4..4100b26 100644
--- a/src/northbridge/amd/amdmct/mct_ddr3/s3utils.c
+++ b/src/northbridge/amd/amdmct/mct_ddr3/s3utils.c
@@ -1100,7 +1100,7 @@
 
 	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 @@
 
 	/* 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 a158123..2fc8d96 100644
--- a/src/northbridge/intel/common/mrc_cache.c
+++ b/src/northbridge/intel/common/mrc_cache.c
@@ -160,6 +160,7 @@
 	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 @@
 
 	//  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 @@
 		       "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 @@
 	//  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 b6bbf4a..5c4c985 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)
 {
-	struct spi_flash *flash;
+	struct spi_flash flash;
 	int erase = 0;
 	volatile uint32_t *flptr;
 	int i, j, ret = 0;
@@ -734,13 +734,7 @@
 	}
 
 	/* 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,25 +742,21 @@
 	/* 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 96a808d..9f973b9 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 @@
 }
 
 
-/* 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,13 +280,11 @@
  * 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;
@@ -317,7 +312,7 @@
 	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 6f2fdbb..a4a7998 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 @@
 {
 	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 @@
 {
 	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 09df1a4..29a1c2f 100644
--- a/src/soc/mediatek/mt8173/flash_controller.c
+++ b/src/soc/mediatek/mt8173/flash_controller.c
@@ -228,24 +228,24 @@
 	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 11318f7..effcc80 100644
--- a/src/southbridge/intel/common/spi.c
+++ b/src/southbridge/intel/common/spi.c
@@ -899,10 +899,9 @@
 	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 @@
 	 * 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 @@
 		flash->size += 1 << (19 + ((flcomp >> 3) & 7));
 	printk (BIOS_DEBUG, "flash size 0x%x bytes\n", flash->size);
 
-	return flash;
+	return 0;
 }

-- 
To view, visit https://review.coreboot.org/19705
To unsubscribe, visit https://review.coreboot.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If6f1b403731466525c4690777d9b32ce778eb563
Gerrit-PatchSet: 10
Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-Owner: Furquan Shaikh <furquan at google.com>
Gerrit-Reviewer: Aaron Durbin <adurbin at chromium.org>
Gerrit-Reviewer: Duncan Laurie <dlaurie at chromium.org>
Gerrit-Reviewer: Furquan Shaikh <furquan at google.com>
Gerrit-Reviewer: Paul Menzel <paulepanter at users.sourceforge.net>
Gerrit-Reviewer: build bot (Jenkins) <no-reply at coreboot.org>



More information about the coreboot-gerrit mailing list