From 3bd1ced3d69890f5158f64050d45e390a4ab0d7b Mon Sep 17 00:00:00 2001
From: Tuomas Kulve <tuomas@kulve.fi>
Date: Sun, 27 Apr 2008 20:03:20 +0300
Subject: [PATCH] Applied flash.patch and uImage-in-own-partition.patch. Increased kernel partition to 2M.

---
 drivers/mtd/maps/Kconfig         |    7 ++
 drivers/mtd/maps/Makefile        |    1 +
 drivers/mtd/maps/gumstix-flash.c |  140 ++++++++++++++++++++++++++++++++++++++
 drivers/mtd/mtdpart.c            |    7 ++-
 include/linux/mtd/partitions.h   |    1 +
 5 files changed, 155 insertions(+), 1 deletions(-)
 create mode 100644 drivers/mtd/maps/gumstix-flash.c

diff --git a/drivers/mtd/maps/Kconfig b/drivers/mtd/maps/Kconfig
index 1bd69aa..d50a971 100644
--- a/drivers/mtd/maps/Kconfig
+++ b/drivers/mtd/maps/Kconfig
@@ -166,6 +166,13 @@ config MTD_PXA2XX
 	help
 	  This provides a driver for the NOR flash attached to a PXA2xx chip.
 
+config MTD_GUMSTIX
+	tristate "CFI Flash device mapped on Gumstix"
+	depends on ARCH_GUMSTIX && MTD_CFI_INTELEXT && MTD_PARTITIONS
+	help
+	  This provides a driver for the on-board flash of the Gumstix
+	  single board computers.
+
 config MTD_OCTAGON
 	tristate "JEDEC Flash device mapped on Octagon 5066 SBC"
 	depends on X86 && MTD_JEDEC && MTD_COMPLEX_MAPPINGS
diff --git a/drivers/mtd/maps/Makefile b/drivers/mtd/maps/Makefile
index a9cbe80..38f8ace 100644
--- a/drivers/mtd/maps/Makefile
+++ b/drivers/mtd/maps/Makefile
@@ -21,6 +21,7 @@ obj-$(CONFIG_MTD_ICHXROM)	+= ichxrom.o
 obj-$(CONFIG_MTD_CK804XROM)	+= ck804xrom.o
 obj-$(CONFIG_MTD_TSUNAMI)	+= tsunami_flash.o
 obj-$(CONFIG_MTD_PXA2XX)	+= pxa2xx-flash.o
+obj-$(CONFIG_MTD_GUMSTIX)	+= gumstix-flash.o
 obj-$(CONFIG_MTD_MBX860)	+= mbx860.o
 obj-$(CONFIG_MTD_CEIVA)		+= ceiva.o
 obj-$(CONFIG_MTD_OCTAGON)	+= octagon-5066.o
diff --git a/drivers/mtd/maps/gumstix-flash.c b/drivers/mtd/maps/gumstix-flash.c
new file mode 100644
index 0000000..5b4b021
--- /dev/null
+++ b/drivers/mtd/maps/gumstix-flash.c
@@ -0,0 +1,140 @@
+/*
+ * Map driver for the Gumstix platform
+ *
+ * Author:	Craig Hughes
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/module.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/map.h>
+#include <linux/mtd/partitions.h>
+#include <asm/io.h>
+#include <asm/hardware.h>
+#include <asm/arch/gumstix.h>
+
+
+#define ROM_ADDR	0x00000000
+#define FLASH_ADDR	0x00000000
+
+#define WINDOW_SIZE 	64*1024*1024
+
+static struct map_info gumstix_flash_maps[1] = { {
+	.name =		"Gumstix Flash ROM",
+	.size =		WINDOW_SIZE,
+	.phys =		FLASH_ADDR,
+	.bankwidth =	2,
+} };
+
+static struct mtd_partition gumstix_flash_partitions[] = {
+	{
+		.name =		"Bootloader",
+		.size =		0x00040000,
+		.offset =	FLASH_ADDR
+	},{
+		.name =		"RootFS",
+		.size =		MTDPART_SIZ_REMAINDER,
+		.offset =	MTDPART_OFS_NXTBLK
+	},{
+		.name =		"Kernel",
+		.size =		0x00200000,
+		.offset =	MTDPART_OFS_NXTBLK
+	}
+};
+
+static struct mtd_info *mymtds[1];
+static struct mtd_partition *parsed_parts[1];
+static int nr_parsed_parts[1];
+
+static const char *probes[] = { NULL };
+
+static int __init gumstix_flashmap_init(void)
+{
+	int ret = 0, i;
+
+	for (i = 0; i < 1; i++) {
+		gumstix_flash_maps[i].virt = ioremap(gumstix_flash_maps[i].phys, WINDOW_SIZE);
+		if (!gumstix_flash_maps[i].virt) {
+			printk(KERN_WARNING "Failed to ioremap %s\n", gumstix_flash_maps[i].name);
+			if (!ret)
+				ret = -ENOMEM;
+			continue;
+		}
+		simple_map_init(&gumstix_flash_maps[i]);
+
+		printk(KERN_NOTICE "Probing %s at physical address 0x%08lx (%d-bit bankwidth)\n",
+		       gumstix_flash_maps[i].name, gumstix_flash_maps[i].phys, 
+		       gumstix_flash_maps[i].bankwidth * 8);
+
+		mymtds[i] = do_map_probe("cfi_probe", &gumstix_flash_maps[i]);
+		
+		if (!mymtds[i]) {
+			iounmap((void *)gumstix_flash_maps[i].virt);
+			if (gumstix_flash_maps[i].cached)
+				iounmap(gumstix_flash_maps[i].cached);
+			if (!ret)
+				ret = -EIO;
+			continue;
+		}
+		mymtds[i]->owner = THIS_MODULE;
+
+		ret = parse_mtd_partitions(mymtds[i], probes,
+					   &parsed_parts[i], 0);
+
+		if (ret > 0)
+			nr_parsed_parts[i] = ret;
+	}
+
+	if (!mymtds[0])
+		return ret;
+	
+	for (i = 0; i < 1; i++) {
+		if (!mymtds[i]) {
+			printk(KERN_WARNING "%s is absent. Skipping\n", gumstix_flash_maps[i].name);
+		} else if (nr_parsed_parts[i]) {
+			add_mtd_partitions(mymtds[i], parsed_parts[i], nr_parsed_parts[i]);
+		} else if (!i) {
+			printk("Using static partitions on %s\n", gumstix_flash_maps[i].name);
+			add_mtd_partitions(mymtds[i], gumstix_flash_partitions, ARRAY_SIZE(gumstix_flash_partitions));
+		} else {
+			printk("Registering %s as whole device\n", gumstix_flash_maps[i].name);
+			add_mtd_device(mymtds[i]);
+		}
+	}
+	return 0;
+}
+
+static void __exit gumstix_flashmap_cleanup(void)
+{
+	int i;
+	for (i = 0; i < 1; i++) {
+		if (!mymtds[i])
+			continue;
+
+		if (nr_parsed_parts[i] || !i)
+			del_mtd_partitions(mymtds[i]);
+		else
+			del_mtd_device(mymtds[i]);			
+
+		map_destroy(mymtds[i]);
+		iounmap((void *)gumstix_flash_maps[i].virt);
+		if (gumstix_flash_maps[i].cached)
+			iounmap(gumstix_flash_maps[i].cached);
+
+		if (parsed_parts[i])
+			kfree(parsed_parts[i]);
+	}
+}
+
+module_init(gumstix_flashmap_init);
+module_exit(gumstix_flashmap_cleanup);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Gumstix, Inc. <gumstix-users@lists.sf.net>");
+MODULE_DESCRIPTION("MTD map driver for the Gumstix Platform");
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index c66902d..7d73fed 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -335,7 +335,7 @@ int add_mtd_partitions(struct mtd_info *master,
 {
 	struct mtd_part *slave;
 	u_int32_t cur_offset = 0;
-	int i;
+	int i,j;
 
 	printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
 
@@ -425,6 +425,11 @@ int add_mtd_partitions(struct mtd_info *master,
 		}
 		if (slave->mtd.size == MTDPART_SIZ_FULL)
 			slave->mtd.size = master->size - slave->offset;
+		if (slave->mtd.size == MTDPART_SIZ_REMAINDER)
+		{
+			slave->mtd.size = master->size - slave->offset;
+			for(j=i+1; j<nbparts; j++) slave->mtd.size -= parts[j].size;
+		}
 		cur_offset = slave->offset + slave->mtd.size;
 
 		printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
diff --git a/include/linux/mtd/partitions.h b/include/linux/mtd/partitions.h
index 7c37d7e..bcc35c8 100644
--- a/include/linux/mtd/partitions.h
+++ b/include/linux/mtd/partitions.h
@@ -47,6 +47,7 @@ struct mtd_partition {
 
 #define MTDPART_OFS_NXTBLK	(-2)
 #define MTDPART_OFS_APPEND	(-1)
+#define MTDPART_SIZ_REMAINDER	(-1)
 #define MTDPART_SIZ_FULL	(0)
 
 
-- 
1.5.3.7


