[U-Boot] [RFC PATCH 1/2] sandbox: Add test SPI flash driver

Simon Glass sjg at chromium.org
Tue Oct 25 15:30:38 CEST 2011


This adds a new test driver for SPI flash. Oddly it can operate without
a SPI interface, since we don't want this to get in the way of our
testing/emulation.

Signed-off-by: Simon Glass <sjg at chromium.org>
---
 drivers/mtd/spi/Makefile     |    1 +
 drivers/mtd/spi/spi_flash.c  |   16 +++++++-
 drivers/mtd/spi/test_flash.c |   88 ++++++++++++++++++++++++++++++++++++++++++
 include/spi_flash.h          |    6 +++
 4 files changed, 110 insertions(+), 1 deletions(-)
 create mode 100644 drivers/mtd/spi/test_flash.c

diff --git a/drivers/mtd/spi/Makefile b/drivers/mtd/spi/Makefile
index 57112af..a091513 100644
--- a/drivers/mtd/spi/Makefile
+++ b/drivers/mtd/spi/Makefile
@@ -35,6 +35,7 @@ COBJS-$(CONFIG_SPI_FLASH_STMICRO)	+= stmicro.o
 COBJS-$(CONFIG_SPI_FLASH_WINBOND)	+= winbond.o
 COBJS-$(CONFIG_SPI_FRAM_RAMTRON)	+= ramtron.o
 COBJS-$(CONFIG_SPI_M95XXX) += eeprom_m95xxx.o
+COBJS-$(CONFIG_SPI_FLASH_TEST) += test_flash.o
 
 COBJS	:= $(COBJS-y)
 SRCS	:= $(COBJS:.o=.c)
diff --git a/drivers/mtd/spi/spi_flash.c b/drivers/mtd/spi/spi_flash.c
index ced4c94..5756b5f 100644
--- a/drivers/mtd/spi/spi_flash.c
+++ b/drivers/mtd/spi/spi_flash.c
@@ -23,6 +23,8 @@ static void spi_flash_addr(u32 addr, u8 *cmd)
 	cmd[3] = addr >> 0;
 }
 
+#ifndef CONFIG_NO_SPI
+
 static int spi_flash_read_write(struct spi_slave *spi,
 				const u8 *cmd, size_t cmd_len,
 				const u8 *data_out, u8 *data_in,
@@ -241,6 +243,9 @@ int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
 	return ret;
 }
 
+#endif /* CONFIG_NO_SPI */
+
+
 /*
  * The following table holds all device probe functions
  *
@@ -310,8 +315,14 @@ static const struct {
 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 		unsigned int max_hz, unsigned int spi_mode)
 {
+#ifdef CONFIG_SPI_FLASH_TEST
+	if (bus == CONFIG_SPI_FLASH_TEST_BUS &&
+			cs == CONFIG_SPI_FLASH_TEST_CS)
+		return board_setup_test_flash();
+#endif
+#ifndef CONFIG_NO_SPI
+	struct spi_flash *flash;
 	struct spi_slave *spi;
-	struct spi_flash *flash = NULL;
 	int ret, i, shift;
 	u8 idcode[IDCODE_LEN], *idp;
 
@@ -370,11 +381,14 @@ err_read_id:
 	spi_release_bus(spi);
 err_claim_bus:
 	spi_free_slave(spi);
+#endif
 	return NULL;
 }
 
 void spi_flash_free(struct spi_flash *flash)
 {
+#ifndef CONFIG_NO_SPI
 	spi_free_slave(flash->spi);
+#endif
 	free(flash);
 }
diff --git a/drivers/mtd/spi/test_flash.c b/drivers/mtd/spi/test_flash.c
new file mode 100644
index 0000000..e0027ee
--- /dev/null
+++ b/drivers/mtd/spi/test_flash.c
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2011 The Chromium OS Authors.
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <common.h>
+#include <malloc.h>
+#include <spi_flash.h>
+
+struct test_flash_info {
+	unsigned page_size;	/* Page size of the test flash */
+	ulong size;		/* Total size of the test flash */
+	char *buf;		/* Buffer holding contents */
+};
+
+static int test_read(struct spi_flash *flash, u32 offset,
+		size_t len, void *buf)
+{
+	struct test_flash_info *info = flash->priv;
+
+	memcpy(buf, info->buf + offset, len);
+	return 0;
+}
+
+static int test_write(struct spi_flash *flash, u32 offset,
+		size_t len, const void *buf)
+{
+	return -1;
+}
+
+static int test_erase(struct spi_flash *flash, u32 offset,
+		size_t len)
+{
+	return -1;
+}
+
+struct spi_flash *spi_flash_probe_test(struct test_flash_info *info)
+{
+	struct spi_flash *flash;
+
+	flash = malloc(sizeof(struct spi_flash));
+	if (!flash) {
+		debug("test_flash: Failed to allocate memory\n");
+		return NULL;
+	}
+
+	flash->read = test_read;
+	flash->write = test_write;
+	flash->erase = test_erase;
+	flash->name = "test";
+	flash->size = info->size;
+	flash->priv = info;
+
+	printf("SF: Detected %s with page size %u, total ",
+	       flash->name, info->page_size);
+	print_size(flash->size, "\n");
+
+	return flash;
+}
+
+struct test_flash_info *spi_flash_test_setup(unsigned page_size, ulong size)
+{
+	struct test_flash_info *info;
+
+	info = malloc(sizeof(struct test_flash_info));
+	if (!info)
+		return NULL;
+	info->page_size = page_size;
+	info->size = size;
+	info->buf = malloc(size);
+	return info;
+}
diff --git a/include/spi_flash.h b/include/spi_flash.h
index 2671ab5..5611222 100644
--- a/include/spi_flash.h
+++ b/include/spi_flash.h
@@ -38,6 +38,8 @@ struct spi_flash {
 	/* Erase (sector) size */
 	u32		sector_size;
 
+	void		*priv;		/* Used by flash driver */
+
 	int		(*read)(struct spi_flash *flash, u32 offset,
 				size_t len, void *buf);
 	int		(*write)(struct spi_flash *flash, u32 offset,
@@ -68,4 +70,8 @@ static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
 	return flash->erase(flash, offset, len);
 }
 
+/* The SPI flash test interface */
+struct test_flash_info;
+struct spi_flash *spi_flash_probe_test(struct test_flash_info *info);
+struct test_flash_info *spi_flash_test_setup(unsigned page_size, ulong size);
 #endif /* _SPI_FLASH_H_ */
-- 
1.7.3.1



More information about the U-Boot mailing list