From f9ab44c271fbd82a5702b6ba067fa90e33a30089 Mon Sep 17 00:00:00 2001
From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
Date: Wed, 7 Nov 2012 15:29:27 +0100
Subject: sf: add malloc-free probe functions dedicated for SPL

Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>

--- a/drivers/mtd/spi/spi_flash.c
+++ b/drivers/mtd/spi/spi_flash.c
@@ -339,11 +339,11 @@ static struct {
 DECLARE_GLOBAL_DATA_PTR;
 #endif
 
-struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
-		unsigned int max_hz, unsigned int spi_mode)
+int spi_flash_probe_spl(struct spi_flash *flash, unsigned int bus,
+			unsigned int cs, unsigned int max_hz,
+			unsigned int spi_mode)
 {
 	struct spi_slave *spi;
-	struct spi_flash *flash;
 	int ret, i, shift;
 	u8 idcode[IDCODE_LEN], *idp;
 #ifdef CONFIG_NEEDS_MANUAL_RELOC
@@ -359,8 +359,8 @@ struct spi_flash *spi_flash_probe(unsign
 
 	spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
 	if (!spi) {
-		printf("SF: Failed to set up slave\n");
-		return NULL;
+		debug("SF: Failed to set up slave\n");
+		return -1;
 	}
 
 	ret = spi_claim_bus(spi);
@@ -379,13 +379,6 @@ struct spi_flash *spi_flash_probe(unsign
 	print_buffer(0, idcode, 1, sizeof(idcode), 0);
 #endif
 
-	flash = malloc(sizeof(*flash));
-	if (!flash) {
-		debug("SF: failed to alloc memory\n");
-		goto err_malloc;
-	}
-
-	memset(flash, 0, sizeof(*flash));
 	flash->spi = spi;
 
 	/* count the number of continuation bytes */
@@ -404,30 +397,58 @@ struct spi_flash *spi_flash_probe(unsign
 		}
 
 	if (ret <= 0) {
-		printf("SF: Unsupported manufacturer %02x\n", *idp);
+		debug("SF: Unsupported manufacturer %02x\n", *idp);
 		goto err_manufacturer_probe;
 	}
 
-	printf("SF: Detected %s with page size ", flash->name);
-	print_size(flash->sector_size, ", total ");
-	print_size(flash->size, "\n");
-
 	spi_release_bus(spi);
 
-	return flash;
+	return 0;
 
 err_manufacturer_probe:
-	free(flash);
-err_malloc:
 err_read_id:
 	spi_release_bus(spi);
 err_claim_bus:
 	spi_free_slave(spi);
+
+	return ret;
+}
+
+struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
+		unsigned int max_hz, unsigned int spi_mode)
+{
+	struct spi_flash *flash;
+	int ret;
+
+	flash = malloc(sizeof(*flash));
+	if (!flash) {
+		debug("SF: Failed to malloc spi_flash\n");
+		return NULL;
+	}
+	memset(flash, 0, sizeof(*flash));
+
+	ret = spi_flash_probe_spl(flash, bus, cs, max_hz, spi_mode);
+	if (ret)
+		goto err_probe;
+
+	printf("SF:    %s, page size ", flash->name);
+	print_size(flash->sector_size, ", total ");
+	print_size(flash->size, "\n");
+
+	return flash;
+
+err_probe:
+	free(flash);
 	return NULL;
 }
 
-void spi_flash_free(struct spi_flash *flash)
+void spi_flash_free_spl(struct spi_flash *flash)
 {
 	spi_free_slave(flash->spi);
+}
+
+void spi_flash_free(struct spi_flash *flash)
+{
+	spi_flash_free_spl(flash);
 	free(flash);
 }
--- a/include/spi_flash.h
+++ b/include/spi_flash.h
@@ -52,6 +52,11 @@ struct spi_flash *spi_flash_probe(unsign
 		unsigned int max_hz, unsigned int spi_mode);
 void spi_flash_free(struct spi_flash *flash);
 
+int spi_flash_probe_spl(struct spi_flash *flash, unsigned int bus,
+			unsigned int cs, unsigned int max_hz,
+			unsigned int spi_mode);
+void spi_flash_free_spl(struct spi_flash *flash);
+
 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
 		size_t len, void *buf)
 {
