public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] Export CFI Flash to the MTD and support it in UBI
@ 2008-11-17 14:57 Piotr Ziecik
  2008-11-17 14:57 ` [U-Boot] [PATCH 1/3] cfi-mtd: Add cfi-mtd driver Piotr Ziecik
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Piotr Ziecik @ 2008-11-17 14:57 UTC (permalink / raw)
  To: u-boot

Following patches allow for CFI Flash support in UBI and other MTD based
parts of U-Boot. This is done by adding cfi_mtd driver which exports
CFI Flash to the MTD layer.

First patch adds cfi_mtd driver to the U-Boot. Second cleans printf()
in MTD layer. The last one adds proof-of-concept CFI Flash support to the UBI.

The baseline of the following patches is the 'testing' branch in the 'u-boot-ubi' repo.

Best regards,
Piotr Ziecik

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [U-Boot] [PATCH 1/3] cfi-mtd: Add cfi-mtd driver.
  2008-11-17 14:57 [U-Boot] Export CFI Flash to the MTD and support it in UBI Piotr Ziecik
@ 2008-11-17 14:57 ` Piotr Ziecik
  2008-11-24 10:09   ` Stefan Roese
  2008-11-17 14:57 ` [U-Boot] [PATCH 2/3] mtd: Remove a printf() from add_mtd_device() Piotr Ziecik
  2008-11-17 14:58 ` [U-Boot] [PATCH 3/3] ubi: Add proof-of-concept CFI flash support Piotr Ziecik
  2 siblings, 1 reply; 7+ messages in thread
From: Piotr Ziecik @ 2008-11-17 14:57 UTC (permalink / raw)
  To: u-boot

Add cfi-mtd driver, which exports CFI flash to MTD layer.
This allows CFI flash devices to be used from MTD layer.

Building of the new driver is controlled by CONFIG_FLASH_CFI_MTD
option. Initialization is done by calling cfi_mtd_init() from
flash_init().

Signed-off-by: Piotr Ziecik <kosmo@semihalf.com>
---
This patch requires following (already posted) patches:
- flash: Exported flash_sector_size() function.
- flash: Added interface for flash verbosity control.

 README                  |    5 +
 drivers/mtd/Makefile    |    1 +
 drivers/mtd/cfi_flash.c |    5 +
 drivers/mtd/cfi_mtd.c   |  202 +++++++++++++++++++++++++++++++++++++++++++++++
 include/flash.h         |    8 ++
 5 files changed, 221 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mtd/cfi_mtd.c

diff --git a/README b/README
index ebee20f..e43dbb1 100644
--- a/README
+++ b/README
@@ -2155,6 +2155,11 @@ Configuration Settings:
 		This option also enables the building of the cfi_flash driver
 		in the drivers directory
 
+- CONFIG_FLASH_CFI_MTD
+		This option enables the building of the cfi_mtd driver
+		in the drivers directory. The driver exports CFI flash
+		to the MTD layer.
+
 - CONFIG_SYS_FLASH_USE_BUFFER_WRITE
 		Use buffered writes to flash.
 
diff --git a/drivers/mtd/Makefile b/drivers/mtd/Makefile
index d225a68..b665a97 100644
--- a/drivers/mtd/Makefile
+++ b/drivers/mtd/Makefile
@@ -28,6 +28,7 @@ LIB	:= $(obj)libmtd.a
 COBJS-$(CONFIG_CMD_UBI) += mtdcore.o mtdpart.o
 COBJS-$(CONFIG_HAS_DATAFLASH) += at45.o
 COBJS-$(CONFIG_FLASH_CFI_DRIVER) += cfi_flash.o
+COBJS-$(CONFIG_FLASH_CFI_MTD) += cfi_mtd.o
 COBJS-$(CONFIG_HAS_DATAFLASH) += dataflash.o
 COBJS-$(CONFIG_FLASH_CFI_LEGACY) += jedec_flash.o
 COBJS-$(CONFIG_MW_EEPROM) += mw_eeprom.o
diff --git a/drivers/mtd/cfi_flash.c b/drivers/mtd/cfi_flash.c
index d828e77..13dc320 100644
--- a/drivers/mtd/cfi_flash.c
+++ b/drivers/mtd/cfi_flash.c
@@ -2068,5 +2068,10 @@ unsigned long flash_init (void)
 			       flash_get_info(apl[i].start));
 	}
 #endif
+
+#ifdef CONFIG_FLASH_CFI_MTD
+	cfi_mtd_init();
+#endif
+
 	return (size);
 }
diff --git a/drivers/mtd/cfi_mtd.c b/drivers/mtd/cfi_mtd.c
new file mode 100644
index 0000000..cf82d92
--- /dev/null
+++ b/drivers/mtd/cfi_mtd.c
@@ -0,0 +1,202 @@
+/*
+ * (C) Copyright 2008 Semihalf
+ *
+ * Written by: Piotr Ziecik <kosmo@semihalf.com>
+ *
+ * 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 <flash.h>
+
+#include <asm/errno.h>
+#include <linux/mtd/mtd.h>
+
+extern flash_info_t flash_info[];
+
+static struct mtd_info cfi_mtd_info[CONFIG_SYS_MAX_FLASH_BANKS];
+
+static int cfi_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
+{
+	flash_info_t *fi = mtd->priv;
+	size_t a_start = fi->start[0] + instr->addr;
+	size_t a_end = a_start + instr->len;
+	int s_first = -1;
+	int s_last = -1;
+	int error, sect;
+
+	for (sect = 0; sect < fi->sector_count - 1; sect++) {
+		if (a_start == fi->start[sect])
+			s_first = sect;
+
+		if (a_end == fi->start[sect + 1]) {
+			s_last = sect;
+			break;
+		}
+	}
+
+	if (s_first >= 0 && s_first <= s_last) {
+		instr->state = MTD_ERASING;
+
+		flash_set_verbose(0);
+		error = flash_erase(fi, s_first, s_last);
+		flash_set_verbose(1);
+
+		if (error) {
+			instr->state = MTD_ERASE_FAILED;
+			return -EIO;
+		}
+
+		instr->state = MTD_ERASE_DONE;
+		mtd_erase_callback(instr);
+		return 0;
+	}
+
+	return -EINVAL;
+}
+
+static int cfi_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
+	size_t *retlen, u_char *buf)
+{
+	flash_info_t *fi = mtd->priv;
+	u_char *f = (u_char*)(fi->start[0]) + from;
+
+	memcpy(buf, f, len);
+	*retlen = len;
+
+	return 0;
+}
+
+static int cfi_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
+	size_t *retlen, const u_char *buf)
+{
+	flash_info_t *fi = mtd->priv;
+	u_long t = fi->start[0] + to;
+	int error;
+
+	flash_set_verbose(0);
+	error = write_buff(fi, (u_char*)buf, t, len);
+	flash_set_verbose(1);
+
+	if (!error) {
+		*retlen = len;
+		return 0;
+	}
+
+	return -EIO;
+}
+
+static void cfi_mtd_sync(struct mtd_info *mtd)
+{
+	/*
+	 * This function should wait until all pending operations
+	 * finish. However this driver is fully synchronous, so
+	 * this function returns immediately
+	 */
+}
+
+static int cfi_mtd_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
+{
+	flash_info_t *fi = mtd->priv;
+
+	flash_set_verbose(0);
+	flash_protect(FLAG_PROTECT_SET, fi->start[0] + ofs,
+					fi->start[0] + ofs + len - 1, fi);
+	flash_set_verbose(1);
+
+	return 0;
+}
+
+static int cfi_mtd_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
+{
+	flash_info_t *fi = mtd->priv;
+
+	flash_set_verbose(0);
+	flash_protect(FLAG_PROTECT_CLEAR, fi->start[0] + ofs,
+					fi->start[0] + ofs + len - 1, fi);
+	flash_set_verbose(1);
+
+	return 0;
+}
+
+static int cfi_mtd_set_erasesize(struct mtd_info *mtd, flash_info_t *fi)
+{
+	int sect_size = 0;
+	int sect;
+
+	for (sect = 0; sect < fi->sector_count; sect++) {
+		if (!sect_size) {
+			sect_size = flash_sector_size(fi, sect);
+			continue;
+		}
+
+		if (sect_size != flash_sector_size(fi, sect)) {
+			sect_size = 0;
+			break;
+		}
+	}
+
+	if (!sect_size) {
+		puts("cfi-mtd: devices with multiple sector sizes are"
+							"not supported\n");
+		return -EINVAL;
+	}
+
+	mtd->erasesize = sect_size;
+
+	return 0;
+}
+
+int cfi_mtd_init(void)
+{
+	struct mtd_info *mtd;
+	flash_info_t *fi;
+	int error, i;
+
+	for (i = 0; i < CONFIG_SYS_MAX_FLASH_BANKS; i++) {
+		fi = &flash_info[i];
+		mtd = &cfi_mtd_info[i];
+
+		memset(mtd, 0, sizeof(struct mtd_info));
+
+		error = cfi_mtd_set_erasesize(mtd, fi);
+		if (error)
+			continue;
+
+		mtd->name		= CFI_MTD_DEV_NAME;
+		mtd->type		= MTD_NORFLASH;
+		mtd->flags		= MTD_CAP_NORFLASH;
+		mtd->size		= fi->size;
+		mtd->writesize		= 1;
+
+		mtd->erase		= cfi_mtd_erase;
+		mtd->read		= cfi_mtd_read;
+		mtd->write		= cfi_mtd_write;
+		mtd->sync		= cfi_mtd_sync;
+		mtd->lock		= cfi_mtd_lock;
+		mtd->unlock		= cfi_mtd_unlock;
+		mtd->priv		= fi;
+
+		if (add_mtd_device(mtd))
+			return -ENOMEM;
+	}
+
+	return 0;
+}
diff --git a/include/flash.h b/include/flash.h
index 05fa572..6e2981c 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -86,6 +86,9 @@ typedef unsigned long flash_sect_t;
 
 /* convert between bit value and numeric value */
 #define CFI_FLASH_SHIFT_WIDTH	3
+
+/* cfi-mtd device name */
+#define	CFI_MTD_DEV_NAME	"cfi-mtd"
 /* Prototypes */
 
 extern unsigned long flash_init (void);
@@ -103,6 +106,11 @@ extern int flash_write (char *, ulong, ulong);
 extern flash_info_t *addr2info (ulong);
 extern int write_buff (flash_info_t *info, uchar *src, ulong addr, ulong cnt);
 
+/* drivers/mtd/cfi_mtd.c */
+#ifdef CONFIG_FLASH_CFI_MTD
+extern int cfi_mtd_init(void);
+#endif
+
 /* board/?/flash.c */
 #if defined(CONFIG_SYS_FLASH_PROTECTION)
 extern int flash_real_protect(flash_info_t *info, long sector, int prot);
-- 
1.5.6.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [U-Boot] [PATCH 2/3] mtd: Remove a printf() from add_mtd_device().
  2008-11-17 14:57 [U-Boot] Export CFI Flash to the MTD and support it in UBI Piotr Ziecik
  2008-11-17 14:57 ` [U-Boot] [PATCH 1/3] cfi-mtd: Add cfi-mtd driver Piotr Ziecik
@ 2008-11-17 14:57 ` Piotr Ziecik
  2008-11-24 10:35   ` Stefan Roese
  2008-11-17 14:58 ` [U-Boot] [PATCH 3/3] ubi: Add proof-of-concept CFI flash support Piotr Ziecik
  2 siblings, 1 reply; 7+ messages in thread
From: Piotr Ziecik @ 2008-11-17 14:57 UTC (permalink / raw)
  To: u-boot

Remove a printf() from add_mtd_device(), which produces spurious output.

Signed-off-by: Piotr Ziecik <kosmo@semihalf.com>
---
 drivers/mtd/mtdcore.c |    2 --
 1 files changed, 0 insertions(+), 2 deletions(-)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 64bd7d4..6eb52ed 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -25,8 +25,6 @@ int add_mtd_device(struct mtd_info *mtd)
 			mtd->index = i;
 			mtd->usecount = 0;
 
-			printf("mtd: Giving out device %d to %s\n",
-				i, mtd->name);
 			/* No need to get a refcount on the module containing
 			   the notifier, since we hold the mtd_table_mutex */
 
-- 
1.5.6.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [U-Boot] [PATCH 3/3] ubi: Add proof-of-concept CFI flash support
  2008-11-17 14:57 [U-Boot] Export CFI Flash to the MTD and support it in UBI Piotr Ziecik
  2008-11-17 14:57 ` [U-Boot] [PATCH 1/3] cfi-mtd: Add cfi-mtd driver Piotr Ziecik
  2008-11-17 14:57 ` [U-Boot] [PATCH 2/3] mtd: Remove a printf() from add_mtd_device() Piotr Ziecik
@ 2008-11-17 14:58 ` Piotr Ziecik
  2008-11-24 10:35   ` Stefan Roese
  2 siblings, 1 reply; 7+ messages in thread
From: Piotr Ziecik @ 2008-11-17 14:58 UTC (permalink / raw)
  To: u-boot

With this patch UBI can be used on CFI flash chips.

Signed-off-by: Piotr Ziecik <kosmo@semihalf.com>
---
For example to create UBI volume on empty flash partition
(partition 0 on NOR device 0) use following commands:

=> setenv mtdids nor0=nor
=> mtdparts add nor0 8m at 32m nor-part
=> saveenv
=> ubi part nor nor0,0
=> ubi create ubi-vol
=> ubi info layout

 common/cmd_ubi.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)

diff --git a/common/cmd_ubi.c b/common/cmd_ubi.c
index a656786..4217fa0 100644
--- a/common/cmd_ubi.c
+++ b/common/cmd_ubi.c
@@ -24,6 +24,7 @@
 #define DEV_TYPE_NONE		0
 #define DEV_TYPE_NAND		1
 #define DEV_TYPE_ONENAND	2
+#define DEV_TYPE_NOR		3
 
 /* Private own data */
 static struct ubi_device *ubi;
@@ -485,6 +486,11 @@ static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 			ubi_dev.mtd_info = &nand_info[ubi_dev.nr];
 		}
 #endif
+		if (strcmp(argv[2], "nor") == 0) {
+			strcpy(ubi_dev.dev_name, "NOR");
+			ubi_dev.type = DEV_TYPE_NOR;
+			ubi_dev.mtd_info = get_mtd_device_nm(CFI_MTD_DEV_NAME);
+		}
 #if defined(CONFIG_CMD_ONENAND)
 		if (strcmp(argv[2], "onenand") == 0) {
 			strcpy(ubi_dev.dev_name, "OneNAND");
@@ -606,7 +612,7 @@ static int do_ubi(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
 
 U_BOOT_CMD(ubi, 6, 1, do_ubi,
 	"ubi      - ubi commands\n",
-        "part [nand|onenand] [part]"
+        "part [nand|nor|onenand] [part]"
 		" - Show or set current partition\n"
 	"info [l[ayout]]"
 		" - Display volume and ubi layout information\n"
-- 
1.5.6.1

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [U-Boot] [PATCH 1/3] cfi-mtd: Add cfi-mtd driver.
  2008-11-17 14:57 ` [U-Boot] [PATCH 1/3] cfi-mtd: Add cfi-mtd driver Piotr Ziecik
@ 2008-11-24 10:09   ` Stefan Roese
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Roese @ 2008-11-24 10:09 UTC (permalink / raw)
  To: u-boot

On Monday 17 November 2008, Piotr Ziecik wrote:
> Add cfi-mtd driver, which exports CFI flash to MTD layer.
> This allows CFI flash devices to be used from MTD layer.
>
> Building of the new driver is controlled by CONFIG_FLASH_CFI_MTD
> option. Initialization is done by calling cfi_mtd_init() from
> flash_init().
>
> Signed-off-by: Piotr Ziecik <kosmo@semihalf.com>

Applied to u-boot-cfi-flash. Thanks.

Best regards,
Stefan

=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80  Email: office at denx.de
=====================================================================

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [U-Boot] [PATCH 2/3] mtd: Remove a printf() from add_mtd_device().
  2008-11-17 14:57 ` [U-Boot] [PATCH 2/3] mtd: Remove a printf() from add_mtd_device() Piotr Ziecik
@ 2008-11-24 10:35   ` Stefan Roese
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Roese @ 2008-11-24 10:35 UTC (permalink / raw)
  To: u-boot

On Monday 17 November 2008, Piotr Ziecik wrote:
> Remove a printf() from add_mtd_device(), which produces spurious output.
>
> Signed-off-by: Piotr Ziecik <kosmo@semihalf.com>

Applied to ubi. Thanks.

Best regards,
Stefan

=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80  Email: office at denx.de
=====================================================================

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [U-Boot] [PATCH 3/3] ubi: Add proof-of-concept CFI flash support
  2008-11-17 14:58 ` [U-Boot] [PATCH 3/3] ubi: Add proof-of-concept CFI flash support Piotr Ziecik
@ 2008-11-24 10:35   ` Stefan Roese
  0 siblings, 0 replies; 7+ messages in thread
From: Stefan Roese @ 2008-11-24 10:35 UTC (permalink / raw)
  To: u-boot

On Monday 17 November 2008, Piotr Ziecik wrote:
> With this patch UBI can be used on CFI flash chips.
>
> Signed-off-by: Piotr Ziecik <kosmo@semihalf.com>

Applied to ubi. Thanks.

Best regards,
Stefan

=====================================================================
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-0 Fax: +49-8142-66989-80  Email: office at denx.de
=====================================================================

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-11-24 10:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-11-17 14:57 [U-Boot] Export CFI Flash to the MTD and support it in UBI Piotr Ziecik
2008-11-17 14:57 ` [U-Boot] [PATCH 1/3] cfi-mtd: Add cfi-mtd driver Piotr Ziecik
2008-11-24 10:09   ` Stefan Roese
2008-11-17 14:57 ` [U-Boot] [PATCH 2/3] mtd: Remove a printf() from add_mtd_device() Piotr Ziecik
2008-11-24 10:35   ` Stefan Roese
2008-11-17 14:58 ` [U-Boot] [PATCH 3/3] ubi: Add proof-of-concept CFI flash support Piotr Ziecik
2008-11-24 10:35   ` Stefan Roese

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox