Skip to contentSkip to conversation

Patches to support Ramtron FRAMs

PATCH New Reinhard Meyer 2 participants 14 messages Oct 5, 2010 – Oct 13, 2010 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

v5 cover Patches to support Ramtron FRAMs

Patch 1 adds generalisation to spi_flash.c to allow for table driven probing of JEDEC and NON-JEDEC conformant devices

Patch 2 adds the actual Ramtron support

Patch 2 requires Patch 1 to be applied first.

#1 lore raw

PATCH v5 1/2 MTD/SPI: introduce table driven probing

for JEDEC devices without and with extension bytes for non JEDEC devices

---
 drivers/mtd/spi/spi_flash.c |  137 +++++++++++++++++++++++++++++--------------
 1 files changed, 93 insertions(+), 44 deletions(-)
drivers/mtd/spi/spi_flash.c +93 -44
index ea875dc..e19f47d 100644
@@ -1,8 +1,26 @@
 /*
- * SPI flash interface
+ * (C) Copyright 2010
+ * Reinhard Meyer, EMK Elektronik, reinhard.meyer at emk-elektronik.de
  *
  * Copyright (C) 2008 Atmel Corporation
- * Licensed under the GPL-2 or later.
+ *
+ * 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>
@@ -96,13 +114,59 @@ int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
 	return ret;
 }
 
+/*
+ * the following table holds all device probe functions
+ *
+ * shift:  number of continuation bytes before the ID
+ * idcode: the expected IDCODE or 0xff for non JEDEC devices
+ * probe:  the function to call
+ *
+ * non JEDEC devices should be ordered in the table such that
+ * the probe functions with best detection algorithms come first
+ *
+ * several matching entries are permitted, they will be tried
+ * in sequence until a probe function returns non NULL
+ *
+ * IDBUF_LEN may be redefined if a device detection requires
+ * more then 5 bytes read.
+ */
+#define IDBUF_LEN 5
+const struct {
+	const u8 shift;
+	const u8 idcode;
+	struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
+} flashes[] = {
+#ifdef CONFIG_SPI_FLASH_SPANSION
+	{ 0, 0x01, spi_flash_probe_spansion, },
+#endif
+#ifdef CONFIG_SPI_FLASH_ATMEL
+	{ 0, 0x1F, spi_flash_probe_atmel, },
+#endif
+#ifdef CONFIG_SPI_FLASH_MACRONIX
+	{ 0, 0xc2, spi_flash_probe_macronix, },
+#endif
+#ifdef CONFIG_SPI_FLASH_WINBOND
+	{ 0, 0xef, spi_flash_probe_winbond, },
+#endif
+#ifdef CONFIG_SPI_FLASH_STMICRO
+	{ 0, 0x20, spi_flash_probe_stmicro, },
+	{ 0, 0xff, spi_flash_probe_stmicro, },
+#endif
+#ifdef CONFIG_SPI_FLASH_SST
+	{ 0, 0xBF, spi_flash_probe_sst, },
+#endif
+};
+
 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 		unsigned int max_hz, unsigned int spi_mode)
 {
 	struct spi_slave *spi;
-	struct spi_flash *flash;
+	struct spi_flash *flash = NULL;
 	int ret;
-	u8 idcode[5];
+	int shift;
+	int i;
+	u8 idbuf[IDBUF_LEN];
+	u8 *idp;
 
 	spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
 	if (!spi) {
@@ -117,53 +181,38 @@ struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 	}
 
 	/* Read the ID codes */
-	ret = spi_flash_cmd(spi, CMD_READ_ID, &idcode, sizeof(idcode));
+	ret = spi_flash_cmd(spi, CMD_READ_ID, &idbuf, sizeof(idbuf));
 	if (ret)
 		goto err_read_id;
 
-	debug("SF: Got idcode %02x %02x %02x %02x %02x\n", idcode[0],
-			idcode[1], idcode[2], idcode[3], idcode[4]);
-
-	switch (idcode[0]) {
-#ifdef CONFIG_SPI_FLASH_SPANSION
-	case 0x01:
-		flash = spi_flash_probe_spansion(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_ATMEL
-	case 0x1F:
-		flash = spi_flash_probe_atmel(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_MACRONIX
-	case 0xc2:
-		flash = spi_flash_probe_macronix(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_WINBOND
-	case 0xef:
-		flash = spi_flash_probe_winbond(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_STMICRO
-	case 0x20:
-	case 0xff: /* Let the stmicro func handle non-JEDEC ids */
-		flash = spi_flash_probe_stmicro(spi, idcode);
-		break;
+#ifdef DEBUG
+	printf("SF: Got idcodes\n");
+	print_buffer(0, idbuf, 1, sizeof(idbuf), 0);
 #endif
-#ifdef CONFIG_SPI_FLASH_SST
-	case 0xBF:
-		flash = spi_flash_probe_sst(spi, idcode);
-		break;
-#endif
-	default:
-		printf("SF: Unsupported manufacturer %02X\n", idcode[0]);
-		flash = NULL;
-		break;
+
+	/*
+	 * count the number of continuation bytes, but
+	 * leave at least 3 bytes to the end of the buffer untouched
+	 */
+	for (shift = 0, idp = idbuf;
+		shift < (sizeof(idbuf) - 3) && *idp == 0x7f;
+			shift++, idp++)
+		;
+
+	/* search the table for matches in shift and id */
+	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 (!flash)
+	if (!flash) {
+		printf("SF: Unsupported manufacturer %02X\n", *idp);
 		goto err_manufacturer_probe;
+	}
 
 	spi_release_bus(spi);
 
Signature · 1 line
1.5.6.5
#2 lore raw

PATCH v5 2/2 MTD/SPI: add support for Ramtron FRAMs

Supports most types that support Read-Id and the FM25H20.

---
 drivers/mtd/spi/Makefile             |    1 +
 drivers/mtd/spi/ramtron.c            |  319 ++++++++++++++++++++++++++++++++++
 drivers/mtd/spi/spi_flash.c          |    8 +
 drivers/mtd/spi/spi_flash_internal.h |    1 +
 4 files changed, 329 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mtd/spi/ramtron.c
drivers/mtd/spi/Makefile +1 -0
index 4f11b36..a082ca7 100644
@@ -32,6 +32,7 @@ COBJS-$(CONFIG_SPI_FLASH_SPANSION)	+= spansion.o
 COBJS-$(CONFIG_SPI_FLASH_SST)	+= sst.o
 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	:= $(COBJS-y)
drivers/mtd/spi/ramtron.c new file +319 -0

319 lines not on this page · raw message

drivers/mtd/spi/spi_flash.c +8 -0
index e19f47d..3f8c8e1 100644
@@ -155,6 +155,14 @@ const struct {
 #ifdef CONFIG_SPI_FLASH_SST
 	{ 0, 0xBF, spi_flash_probe_sst, },
 #endif
+#ifdef CONFIG_SPI_FRAM_RAMTRON
+	{ 6, 0xc2, spi_fram_probe_ramtron, },
+# ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
+	{ 0, 0xff, spi_fram_probe_ramtron, },
+# endif
+# undef IDBUF_LEN
+# define IDBUF_LEN 9 /* we need to read 6+3 bytes */
+#endif
 };
 
 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
drivers/mtd/spi/spi_flash_internal.h +1 -0
index 08546fb..9bc43dd 100644
@@ -50,3 +50,4 @@ 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_fram_probe_ramtron(struct spi_slave *spi, u8 *idcode);
Signature · 1 line
1.5.6.5
#3 lore raw

PATCH v5 2/2 MTD/SPI: add support for Ramtron FRAMs

Quoted 1 line · On Tuesday, October 05, 2010 10:56:40 Reinhard Meyer wrote:

Supports most types that support Read-Id and the FM25H20.

you'll need to tweak the probe func to handle the full idcode buffer (see the patch i posted), but otherwise this looks fine to me
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20101011/2c65735c/attachment.pgp

#4 lore raw

PATCH v5 1/2 MTD/SPI: introduce table driven probing

In reply to Reinhard Meyer
Quoted 8 lines · On Tuesday, October 05, 2010 10:56:39 Reinhard Meyer wrote:
drivers/mtd/spi/spi_flash.c +8 -0
+	/*
+	 * count the number of continuation bytes, but
+	 * leave at least 3 bytes to the end of the buffer untouched
+	 */
+	for (shift = 0, idp = idbuf;
+		shift < (sizeof(idbuf) - 3) && *idp == 0x7f;
+			shift++, idp++)
+		;

drivers/mtd/spi/spi_flash.c struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, · line 210

Quoted 10 lines
drivers/mtd/spi/spi_flash.c +9 -0
+
+	/* search the table for matches in shift and id */
+	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;
+		}
 	}

thinking about this some more, i see two problems. (1) we've been providing 4 bytes to probe funcs (like the spansion), so 3 is too small. (2) all of the probe devices today expect to get the whole idcode buffer. so if we're going to change this API, we need to fix all the probe funcs too. but that is a lot of changes to roll into one commit, so let's first focus on converting to a table without changing any probe semantics.

see the follow up patch that addresses these issues and some other stuff. -mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20101011/a8a0608b/attachment.pgp

#5 lore raw

PATCH v5 1/2 MTD/SPI: introduce table driven probing

Dear Mike Frysinger,

Quoted 18 lines · On Tuesday, October 05, 2010 10:56:39 Reinhard Meyer wrote:
Quoted 17 lines
drivers/mtd/spi/spi_flash.c +17 -0
+	/*
+	 * count the number of continuation bytes, but
+	 * leave at least 3 bytes to the end of the buffer untouched
+	 */
+	for (shift = 0, idp = idbuf;
+		shift < (sizeof(idbuf) - 3) && *idp == 0x7f;
+			shift++, idp++)
+		;
+
+	/* search the table for matches in shift and id */
+	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;
+		}
Quoted 8 lines
Quoted 1 line

}

thinking about this some more, i see two problems. (1) we've been providing 4 bytes to probe funcs (like the spansion), so 3 is too small. (2) all of the probe devices today expect to get the whole idcode buffer. so if we're going to change this API, we need to fix all the probe funcs too. but that is a lot of changes to roll into one commit, so let's first focus on converting to a table without changing any probe semantics.

NONE of the existing functions work with expansion, so they all get the unshifted, full ID buffer.

If you provide the unshifted buffer to the function, the function should get the shift amount as well. Otherwise the function will again have look how many 0x7e's to skip....

Reinhard

#6 lore raw

PATCH v5 1/2 MTD/SPI: introduce table driven probing

Quoted 31 lines · On Monday, October 11, 2010 03:56:26 Reinhard Meyer wrote:

Dear Mike Frysinger,

Quoted 19 lines · On Tuesday, October 05, 2010 10:56:39 Reinhard Meyer wrote:
Quoted 18 lines
drivers/mtd/spi/spi_flash.c +17 -0
+	/*
+	 * count the number of continuation bytes, but
+	 * leave at least 3 bytes to the end of the buffer untouched
+	 */
+	for (shift = 0, idp = idbuf;
+		shift < (sizeof(idbuf) - 3) && *idp == 0x7f;
+			shift++, idp++)
+		;
+
+	/* search the table for matches in shift and id */
+	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;
+		}
Quoted 9 lines
Quoted 1 line

}

thinking about this some more, i see two problems. (1) we've been providing 4 bytes to probe funcs (like the spansion), so 3 is too small.
(2) all of the probe devices today expect to get the whole idcode
buffer. so if we're going to change this API, we need to fix all the probe funcs too. but that is a lot of changes to roll into one commit, so let's first focus on converting to a table without changing any probe semantics.

Quoted 2 lines

NONE of the existing functions work with expansion, so they all get the unshifted, full ID buffer.

err, right. was thinking it also shifted off the manu id byte and not just
the continuation bytes.

i'll tweak the patch i posted and pull in your ramtron patch.

Quoted 3 lines

If you provide the unshifted buffer to the function, the function should get the shift amount as well. Otherwise the function will again have look how many 0x7e's to skip....

not really. the probe funcs already know the layout of the idcode, so starting at a constant offset of 3 is the same as starting at 1.
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20101011/ceea04b1/attachment.pgp

#7 lore raw

PATCH v5 1/2 MTD/SPI: introduce table driven probing

Dear Mike Frysinger,

Quoted 7 lines
Quoted 2 lines

NONE of the existing functions work with expansion, so they all get the unshifted, full ID buffer.

err, right. was thinking it also shifted off the manu id byte and not just
the continuation bytes.

i'll tweak the patch i posted and pull in your ramtron patch.

Thanks.

Quoted 7 lines
Quoted 4 lines

If you provide the unshifted buffer to the function, the function should get the shift amount as well. Otherwise the function will again have look how many 0x7e's to skip....

not really. the probe funcs already know the layout of the idcode, so starting at a constant offset of 3 is the same as starting at 1.

True. I was already contemplating the possibility of one probe function having to deal with different amounts of shift... But this would not work safe in either method right now.

(Assuming the unlikely case that 7e 7e 7e 55 and 7e 7e 55 have to be handled by the same probe function...)

This would indeed favour passing the unshifted ID buffer for such functions to find out the amount of 7f's. If you favour that, I am game with it. But I will resubmit the ramtron part only when the new probe is in mainline and I have rebased my tree to it.

Reinhard

#8 lore raw

PATCH v5 1/2 MTD/SPI: introduce table driven probing

ive posted the patches that work for me and include the latest feedback. please double check they still work for ramtron, and i'll push them over to wolfgang. or try my sf branch as it is latest master + those branches. -mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20101012/93218c1c/attachment.pgp

#9 lore raw

PATCH 1/1 sf: turn probe switch into a lookup table

In reply to Reinhard Meyer

From: Reinhard Meyer <[email protected]>

This allows for arbitrarily long manufacturer ids following the JEDEC standard of 0x7f continuation bytes. It also makes adding new entries easier as it's just one element in an array. The downside is that it increases code size a bit, but we're talking less than 100 bytes.

---
 drivers/mtd/spi/spi_flash.c |  123 +++++++++++++++++++++++++++---------------
 1 files changed, 79 insertions(+), 44 deletions(-)
drivers/mtd/spi/spi_flash.c +79 -44
index ea875dc..0aacc9c 100644
@@ -2,6 +2,8 @@
  * SPI flash interface
  *
  * Copyright (C) 2008 Atmel Corporation
+ * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
+ *
  * Licensed under the GPL-2 or later.
  */
 
@@ -96,13 +98,65 @@ int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
 	return ret;
 }
 
+/*
+ * The following table holds all device probe functions
+ *
+ * shift:  number of continuation bytes before the ID
+ * idcode: the expected IDCODE or 0xff for non JEDEC devices
+ * probe:  the function to call
+ *
+ * Non JEDEC devices should be ordered in the table such that
+ * the probe functions with best detection algorithms come first.
+ *
+ * Several matching entries are permitted, they will be tried
+ * in sequence until a probe function returns non NULL.
+ *
+ * IDCODE_MANU_LEN may be redefined if a device detection requires
+ * more then 2 bytes (continuation bytes + manu id) of the idcode
+ * read.  IDCODE_PART_LEN generally shouldn't be changed.  This is
+ * the max number of bytes probe functions may examine when looking
+ * up part-specific identification info.
+ */
+#define IDCODE_MANU_LEN 1
+#define IDCODE_PART_LEN 4
+static const struct {
+	const u8 shift;
+	const u8 idcode;
+	struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
+} flashes[] = {
+	/* Keep it sorted by define name */
+#ifdef CONFIG_SPI_FLASH_ATMEL
+	{ 0, 0x1f, spi_flash_probe_atmel, },
+#endif
+#ifdef CONFIG_SPI_FLASH_MACRONIX
+	{ 0, 0xc2, spi_flash_probe_macronix, },
+#endif
+#ifdef CONFIG_SPI_FLASH_SPANSION
+	{ 0, 0x01, spi_flash_probe_spansion, },
+#endif
+#ifdef CONFIG_SPI_FLASH_SST
+	{ 0, 0xbf, spi_flash_probe_sst, },
+#endif
+#ifdef CONFIG_SPI_FLASH_STMICRO
+	{ 0, 0x20, spi_flash_probe_stmicro, },
+#endif
+#ifdef CONFIG_SPI_FLASH_WINBOND
+	{ 0, 0xef, spi_flash_probe_winbond, },
+#endif
+	/* Keep it sorted by best detection */
+#ifdef CONFIG_SPI_FLASH_STMICRO
+	{ 0, 0xff, spi_flash_probe_stmicro, },
+#endif
+};
+#define IDCODE_LEN (IDCODE_MANU_LEN + IDCODE_PART_LEN)
+
 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 		unsigned int max_hz, unsigned int spi_mode)
 {
 	struct spi_slave *spi;
-	struct spi_flash *flash;
-	int ret;
-	u8 idcode[5];
+	struct spi_flash *flash = NULL;
+	int ret, i, shift;
+	u8 idcode[IDCODE_LEN], *idp;
 
 	spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
 	if (!spi) {
@@ -117,53 +171,34 @@ struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 	}
 
 	/* Read the ID codes */
-	ret = spi_flash_cmd(spi, CMD_READ_ID, &idcode, sizeof(idcode));
+	ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
 	if (ret)
 		goto err_read_id;
 
-	debug("SF: Got idcode %02x %02x %02x %02x %02x\n", idcode[0],
-			idcode[1], idcode[2], idcode[3], idcode[4]);
-
-	switch (idcode[0]) {
-#ifdef CONFIG_SPI_FLASH_SPANSION
-	case 0x01:
-		flash = spi_flash_probe_spansion(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_ATMEL
-	case 0x1F:
-		flash = spi_flash_probe_atmel(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_MACRONIX
-	case 0xc2:
-		flash = spi_flash_probe_macronix(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_WINBOND
-	case 0xef:
-		flash = spi_flash_probe_winbond(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_STMICRO
-	case 0x20:
-	case 0xff: /* Let the stmicro func handle non-JEDEC ids */
-		flash = spi_flash_probe_stmicro(spi, idcode);
-		break;
+#ifdef DEBUG
+	printf("SF: Got idcodes\n");
+	print_buffer(0, idcode, 1, sizeof(idcode), 0);
 #endif
-#ifdef CONFIG_SPI_FLASH_SST
-	case 0xBF:
-		flash = spi_flash_probe_sst(spi, idcode);
-		break;
-#endif
-	default:
-		printf("SF: Unsupported manufacturer %02X\n", idcode[0]);
-		flash = NULL;
-		break;
-	}
 
-	if (!flash)
+	/* count the number of continuation bytes */
+	for (shift = 0, idp = idcode;
+	     shift < IDCODE_MANU_LEN && *idp == 0x7f;
+	     ++shift, ++idp)
+		continue;
+
+	/* search the table for matches in shift and id */
+	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, idcode);
+			if (flash)
+				break;
+		}
+
+	if (!flash) {
+		printf("SF: Unsupported manufacturer %02x\n", *idp);
 		goto err_manufacturer_probe;
+	}
 
 	spi_release_bus(spi);
 
Signature · 1 line
1.7.3.1
#10 lore raw

PATCH 1/2 sf: turn probe switch into a lookup table

In reply to Reinhard Meyer

From: Reinhard Meyer <[email protected]>

This allows for arbitrarily long manufacturer ids following the JEDEC standard of 0x7f continuation bytes. It also makes adding new entries easier as it's just one element in an array. The downside is that it increases code size a bit, but we're talking ~50 bytes.

---
v2
- restore passing shifted idcode buffer to probe
- tweak defines further so gcc optimizes better

 drivers/mtd/spi/spi_flash.c |  126 ++++++++++++++++++++++++++++---------------
 1 files changed, 82 insertions(+), 44 deletions(-)
drivers/mtd/spi/spi_flash.c +82 -44
index ea875dc..b4ba1dd 100644
@@ -2,6 +2,8 @@
  * SPI flash interface
  *
  * Copyright (C) 2008 Atmel Corporation
+ * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
+ *
  * Licensed under the GPL-2 or later.
  */
 
@@ -96,13 +98,68 @@ int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
 	return ret;
 }
 
+/*
+ * The following table holds all device probe functions
+ *
+ * shift:  number of continuation bytes before the ID
+ * idcode: the expected IDCODE or 0xff for non JEDEC devices
+ * probe:  the function to call
+ *
+ * Non JEDEC devices should be ordered in the table such that
+ * the probe functions with best detection algorithms come first.
+ *
+ * Several matching entries are permitted, they will be tried
+ * in sequence until a probe function returns non NULL.
+ *
+ * IDCODE_CONT_LEN may be redefined if a device needs to declare a
+ * larger "shift" value.  IDCODE_PART_LEN generally shouldn't be
+ * changed.  This is the max number of bytes probe functions may
+ * examine when looking up part-specific identification info.
+ *
+ * Probe functions will be given the idcode buffer starting at their
+ * manu id byte (the "idcode" in the table below).  In other words,
+ * all of the continuation bytes will be skipped (the "shift" below).
+ */
+#define IDCODE_CONT_LEN 0
+#define IDCODE_PART_LEN 5
+static const struct {
+	const u8 shift;
+	const u8 idcode;
+	struct spi_flash *(*probe) (struct spi_slave *spi, u8 *idcode);
+} flashes[] = {
+	/* Keep it sorted by define name */
+#ifdef CONFIG_SPI_FLASH_ATMEL
+	{ 0, 0x1f, spi_flash_probe_atmel, },
+#endif
+#ifdef CONFIG_SPI_FLASH_MACRONIX
+	{ 0, 0xc2, spi_flash_probe_macronix, },
+#endif
+#ifdef CONFIG_SPI_FLASH_SPANSION
+	{ 0, 0x01, spi_flash_probe_spansion, },
+#endif
+#ifdef CONFIG_SPI_FLASH_SST
+	{ 0, 0xbf, spi_flash_probe_sst, },
+#endif
+#ifdef CONFIG_SPI_FLASH_STMICRO
+	{ 0, 0x20, spi_flash_probe_stmicro, },
+#endif
+#ifdef CONFIG_SPI_FLASH_WINBOND
+	{ 0, 0xef, spi_flash_probe_winbond, },
+#endif
+	/* Keep it sorted by best detection */
+#ifdef CONFIG_SPI_FLASH_STMICRO
+	{ 0, 0xff, spi_flash_probe_stmicro, },
+#endif
+};
+#define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
+
 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 		unsigned int max_hz, unsigned int spi_mode)
 {
 	struct spi_slave *spi;
-	struct spi_flash *flash;
-	int ret;
-	u8 idcode[5];
+	struct spi_flash *flash = NULL;
+	int ret, i, shift;
+	u8 idcode[IDCODE_LEN], *idp;
 
 	spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
 	if (!spi) {
@@ -117,53 +174,34 @@ struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 	}
 
 	/* Read the ID codes */
-	ret = spi_flash_cmd(spi, CMD_READ_ID, &idcode, sizeof(idcode));
+	ret = spi_flash_cmd(spi, CMD_READ_ID, idcode, sizeof(idcode));
 	if (ret)
 		goto err_read_id;
 
-	debug("SF: Got idcode %02x %02x %02x %02x %02x\n", idcode[0],
-			idcode[1], idcode[2], idcode[3], idcode[4]);
-
-	switch (idcode[0]) {
-#ifdef CONFIG_SPI_FLASH_SPANSION
-	case 0x01:
-		flash = spi_flash_probe_spansion(spi, idcode);
-		break;
+#ifdef DEBUG
+	printf("SF: Got idcodes\n");
+	print_buffer(0, idcode, 1, sizeof(idcode), 0);
 #endif
-#ifdef CONFIG_SPI_FLASH_ATMEL
-	case 0x1F:
-		flash = spi_flash_probe_atmel(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_MACRONIX
-	case 0xc2:
-		flash = spi_flash_probe_macronix(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_WINBOND
-	case 0xef:
-		flash = spi_flash_probe_winbond(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_STMICRO
-	case 0x20:
-	case 0xff: /* Let the stmicro func handle non-JEDEC ids */
-		flash = spi_flash_probe_stmicro(spi, idcode);
-		break;
-#endif
-#ifdef CONFIG_SPI_FLASH_SST
-	case 0xBF:
-		flash = spi_flash_probe_sst(spi, idcode);
-		break;
-#endif
-	default:
-		printf("SF: Unsupported manufacturer %02X\n", idcode[0]);
-		flash = NULL;
-		break;
-	}
 
-	if (!flash)
+	/* count the number of continuation bytes */
+	for (shift = 0, idp = idcode;
+	     shift < IDCODE_CONT_LEN && *idp == 0x7f;
+	     ++shift, ++idp)
+		continue;
+
+	/* search the table for matches in shift and id */
+	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 (!flash) {
+		printf("SF: Unsupported manufacturer %02x\n", *idp);
 		goto err_manufacturer_probe;
+	}
 
 	spi_release_bus(spi);
 
Signature · 1 line
1.7.3.1
#11 lore raw

PATCH 2/2 sf: ramtron: new spi fram driver

In reply to Reinhard Meyer

From: Reinhard Meyer <[email protected]>

Supports most types that support Read-Id and the FM25H20.

---
 drivers/mtd/spi/Makefile             |    1 +
 drivers/mtd/spi/ramtron.c            |  319 ++++++++++++++++++++++++++++++++++
 drivers/mtd/spi/spi_flash.c          |    8 +
 drivers/mtd/spi/spi_flash_internal.h |    1 +
 4 files changed, 329 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mtd/spi/ramtron.c
drivers/mtd/spi/Makefile +1 -0
index 4f11b36..a082ca7 100644
@@ -32,6 +32,7 @@ COBJS-$(CONFIG_SPI_FLASH_SPANSION)	+= spansion.o
 COBJS-$(CONFIG_SPI_FLASH_SST)	+= sst.o
 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	:= $(COBJS-y)
drivers/mtd/spi/ramtron.c new file +319 -0

319 lines not on this page · raw message

drivers/mtd/spi/spi_flash.c +8 -0
index b4ba1dd..ab02ef3 100644
@@ -146,10 +146,18 @@ static const struct {
 #ifdef CONFIG_SPI_FLASH_WINBOND
 	{ 0, 0xef, spi_flash_probe_winbond, },
 #endif
+#ifdef CONFIG_SPI_FRAM_RAMTRON
+	{ 6, 0xc2, spi_fram_probe_ramtron, },
+# undef IDCODE_CONT_LEN
+# define IDCODE_CONT_LEN 6
+#endif
 	/* Keep it sorted by best detection */
 #ifdef CONFIG_SPI_FLASH_STMICRO
 	{ 0, 0xff, spi_flash_probe_stmicro, },
 #endif
+#ifdef CONFIG_SPI_FRAM_RAMTRON_NON_JEDEC
+	{ 0, 0xff, spi_fram_probe_ramtron, },
+#endif
 };
 #define IDCODE_LEN (IDCODE_CONT_LEN + IDCODE_PART_LEN)
 
drivers/mtd/spi/spi_flash_internal.h +1 -0
index 08546fb..9bc43dd 100644
@@ -50,3 +50,4 @@ 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_fram_probe_ramtron(struct spi_slave *spi, u8 *idcode);
Signature · 1 line
1.7.3.1
#12 lore raw

PATCH v5 2/2 sf: ramtron: new spi fram driver

Dear Mike Frysinger,

Quoted 7 lines

From: Reinhard Meyer <[email protected]>

Supports most types that support Read-Id and the FM25H20.

---

[snippebedip]

drivers/mtd/spi/spi_flash.c static const struct { · line 153

Quoted 5 lines
drivers/mtd/spi/spi_flash.c +5 -0
+#ifdef CONFIG_SPI_FRAM_RAMTRON
+	{ 6, 0xc2, spi_fram_probe_ramtron, },
+# undef IDCODE_CONT_LEN
+# define IDCODE_CONT_LEN 6
+#endif

That code should only increase the IDCODE_CONT_LEN, not set it to a value. It might have been set larger by a previous #ifdef.

+#ifdef CONFIG_SPI_FRAM_RAMTRON
+ { 6, 0xc2, spi_fram_probe_ramtron, },
+# if IDCODE_CONT_LEN < 6
+# undef IDCODE_CONT_LEN
+# define IDCODE_CONT_LEN 6
+# endif
+#endif

Even "nicer" and maybe better understandable could be:

+#ifdef CONFIG_SPI_FRAM_RAMTRON
+# define RAMTRON_CONT_LEN 6
+ { RAMTRON_CONT_LEN, 0xc2, spi_fram_probe_ramtron, },
+# if IDCODE_CONT_LEN < RAMTRON_CONT_LEN
+# undef IDCODE_CONT_LEN
+# define IDCODE_CONT_LEN RAMTRON_CONT_LEN
+# endif
+#endif

I cannot test all this right now. Maybe you put just the probing part (1/2) into mainline, once all my local ahead-of-MASTER and posted patches are in mainline, I'll do a big rebase and fix session here and post a new ramtron patch.

Reinhard

#13 lore raw

PATCH 2/2 sf: ramtron: new spi fram driver

Quoted 10 lines · On Wednesday, October 13, 2010 03:44:52 Reinhard Meyer wrote:

drivers/mtd/spi/spi_flash.c static const struct { · line 153

Quoted 5 lines · Mike Frysinger
drivers/mtd/spi/spi_flash.c +5 -0
+#ifdef CONFIG_SPI_FRAM_RAMTRON
+	{ 6, 0xc2, spi_fram_probe_ramtron, },
+# undef IDCODE_CONT_LEN
+# define IDCODE_CONT_LEN 6
+#endif

That code should only increase the IDCODE_CONT_LEN, not set it to a value. It might have been set larger by a previous #ifdef.

i noticed that, but this isnt any different from your original patch, and i figured we could delay worrying about that when someone else proposed something that wanted to change the size. whichever you want.

Review

Quoted 4 lines

Even "nicer" and maybe better understandable could be:

(quoted diff) +2 -0
+#ifdef CONFIG_SPI_FRAM_RAMTRON
+# define RAMTRON_CONT_LEN 6

i'd prefer the first version. i dont think this gains us much.

Quoted 4 lines

I cannot test all this right now. Maybe you put just the probing part (1/2) into mainline, once all my local ahead-of-MASTER and posted patches are in mainline, I'll do a big rebase and fix session here and post a new ramtron patch.

OK
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20101013/7fae5e17/attachment.pgp