Received: from mail.onstor.com (66.201.51.107) by exch1.onstor.net
 (10.0.0.225) with Microsoft SMTP Server id 8.1.311.2; Tue, 20 Jan 2009
 06:12:49 -0800
Received: from chiesmta2-1.messageone.com ([216.203.30.55]) by mail.onstor.com
 with Microsoft SMTPSVC(6.0.3790.3959);	 Tue, 20 Jan 2009 06:12:42 -0800
Received: from ftp.linux-mips.org (ftp.linux-mips.org [213.58.128.207])	by
 chiesmta2-1.messageone.com (8.13.8/8.13.8) with ESMTP id n0KECirD005014	for
 <andy.sharp@onstor.com>; Tue, 20 Jan 2009 08:12:44 -0600
Received: from localhost.localdomain ([127.0.0.1]:19335 "EHLO
	ftp.linux-mips.org") by ftp.linux-mips.org with ESMTP	id S21366221AbZATOMg
 (ORCPT <rfc822;andy.sharp@onstor.com>);	Tue, 20 Jan 2009 14:12:36 +0000
Received: with ECARTIS (v1.0.0; list linux-mips); Tue, 20 Jan 2009 14:12:20
 +0000 (GMT)
Received: from mba.ocn.ne.jp ([122.1.235.107]:29921 "HELO smtp.mba.ocn.ne.jp")
	by ftp.linux-mips.org with SMTP id S21366218AbZATOMR (ORCPT
	<rfc822;linux-mips@linux-mips.org>); Tue, 20 Jan 2009 14:12:17 +0000
Received: from localhost.localdomain (p1210-ipad205funabasi.chiba.ocn.ne.jp
 [222.146.96.210])	by smtp.mba.ocn.ne.jp (Postfix) with ESMTP	id 9CE15AA85;
 Tue, 20 Jan 2009 23:12:12 +0900 (JST)
From: Atsushi Nemoto <anemo@mba.ocn.ne.jp>
To: "linux-mips@linux-mips.org" <linux-mips@linux-mips.org>
CC: "ralf@linux-mips.org" <ralf@linux-mips.org>,
	"linux-mtd@lists.infradead.org" <linux-mtd@lists.infradead.org>,
	"dwmw2@infradead.org" <dwmw2@infradead.org>, "linux-kernel@vger.kernel.org"
	<linux-kernel@vger.kernel.org>
Sender: "linux-mips-bounce@linux-mips.org" <linux-mips-bounce@linux-mips.org>
Date: Tue, 20 Jan 2009 06:12:16 -0800
Subject: [PATCH 2/5] RBTX4939: Add MTD support
Thread-Topic: [PATCH 2/5] RBTX4939: Add MTD support
Thread-Index: Acl7CS1MPZNG1VwBS4KKKqbiRuvIKQ==
Message-ID: <1232460738-4714-2-git-send-email-anemo@mba.ocn.ne.jp>
Accept-Language: en-US
Content-Language: en-US
X-MS-Exchange-Organization-AuthAs: Internal
X-MS-Exchange-Organization-AuthMechanism: 0b
X-MS-Exchange-Organization-AuthSource: exch1.onstor.net
X-MS-Has-Attach:
X-Auto-Response-Suppress: All
X-MS-TNEF-Correlator:
x-originalarrivaltime: 20 Jan 2009 14:12:42.0197 (UTC)
 FILETIME=[288C3850:01C97B09]
errors-to: linux-mips-bounce@linux-mips.org
x-ems-proccessed: 2K3Xl1OQTInXD6xxuA8z3Q==
x-ems-stamp: qOjH2KRom+9oMVnymaVoCw==
x-messageone-virus-version: vendor=fsecure
 engine=4.65.7400:2.4.4,1.2.40,4.0.164
 definitions=2009-01-20_06:2009-01-18,2009-01-20,2009-01-20 signatures=0
x-messageone-virus-scanned: Clean
x-messageone-envelope-sender: linux-mips-bounce@linux-mips.org
x-messageone-spam-details: rule=m773emszm_notspam policy=m773emszm score=0
 spamscore=0 ipscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam
 adjust=0 reason=mlx engine=3.1.0-0810130000 definitions=main-0901200058
x-messageone-spam-score: 0
x-messageone-spam-bar:
x-list: linux-mips
x-archive-position: 21786
x-ecartis-version: Ecartis v1.0.0
x-original-sender: anemo@mba.ocn.ne.jp
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
MIME-Version: 1.0

Add platform support for NOR flash chips on RBTX4939 board.
This board has complex flash mappings, controlled by its DIPSW setting.

Signed-off-by: Atsushi Nemoto <anemo@mba.ocn.ne.jp>
---
 arch/mips/include/asm/txx9/rbtx4939.h |    9 ++
 arch/mips/txx9/rbtx4939/setup.c       |  157 +++++++++++++++++++++++++++++=
++++
 2 files changed, 166 insertions(+), 0 deletions(-)

diff --git a/arch/mips/include/asm/txx9/rbtx4939.h b/arch/mips/include/asm/=
txx9/rbtx4939.h
index 1acf428..e517899 100644
--- a/arch/mips/include/asm/txx9/rbtx4939.h
+++ b/arch/mips/include/asm/txx9/rbtx4939.h
@@ -130,4 +130,13 @@
 void rbtx4939_prom_init(void);
 void rbtx4939_irq_setup(void);
=20
+struct mtd_partition;
+struct map_info;
+struct rbtx4939_flash_data {
+	unsigned int width;
+	unsigned int nr_parts;
+	struct mtd_partition *parts;
+	void (*map_init)(struct map_info *map);
+};
+
 #endif /* __ASM_TXX9_RBTX4939_H */
diff --git a/arch/mips/txx9/rbtx4939/setup.c b/arch/mips/txx9/rbtx4939/setu=
p.c
index 74839f2..eee8763 100644
--- a/arch/mips/txx9/rbtx4939/setup.c
+++ b/arch/mips/txx9/rbtx4939/setup.c
@@ -16,6 +16,9 @@
 #include <linux/leds.h>
 #include <linux/interrupt.h>
 #include <linux/smc91x.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+#include <linux/mtd/map.h>
 #include <asm/reboot.h>
 #include <asm/txx9/generic.h>
 #include <asm/txx9/pci.h>
@@ -282,6 +285,159 @@ static void rbtx4939_7segled_putc(unsigned int pos, u=
nsigned char val)
 	__rbtx4939_7segled_putc(pos, val);
 }
=20
+#if defined(CONFIG_MTD_RBTX4939) || defined(CONFIG_MTD_RBTX4939_MODULE)
+/* special mapping for boot rom */
+static unsigned long rbtx4939_flash_fixup_ofs(unsigned long ofs)
+{
+	u8 bdipsw =3D readb(rbtx4939_bdipsw_addr) & 0x0f;
+	unsigned char shift;
+
+	if (bdipsw & 8) {
+		/* BOOT Mode: USER ROM1 / USER ROM2 */
+		shift =3D bdipsw & 3;
+		/* rotate A[23:22] */
+		return (ofs & ~0xc00000) | ((((ofs >> 22) + shift) & 3) << 22);
+	}
+#ifdef __BIG_ENDIAN
+	if (bdipsw =3D=3D 0)
+		/* BOOT Mode: Monitor ROM */
+		ofs ^=3D 0x400000;	/* swap A[22] */
+#endif
+	return ofs;
+}
+
+static map_word rbtx4939_flash_read16(struct map_info *map, unsigned long =
ofs)
+{
+	map_word r;
+
+	ofs =3D rbtx4939_flash_fixup_ofs(ofs);
+	r.x[0] =3D __raw_readw(map->virt + ofs);
+	return r;
+}
+
+static void rbtx4939_flash_write16(struct map_info *map, const map_word da=
tum,
+				   unsigned long ofs)
+{
+	ofs =3D rbtx4939_flash_fixup_ofs(ofs);
+	__raw_writew(datum.x[0], map->virt + ofs);
+	mb();	/* see inline_map_write() in mtd/map.h */
+}
+
+static void rbtx4939_flash_copy_from(struct map_info *map, void *to,
+				     unsigned long from, ssize_t len)
+{
+	u8 bdipsw =3D readb(rbtx4939_bdipsw_addr) & 0x0f;
+	unsigned char shift;
+	ssize_t curlen;
+
+	from +=3D (unsigned long)map->virt;
+	if (bdipsw & 8) {
+		/* BOOT Mode: USER ROM1 / USER ROM2 */
+		shift =3D bdipsw & 3;
+		while (len) {
+			curlen =3D min((unsigned long)len,
+				     0x400000 -	(from & (0x400000 - 1)));
+			memcpy(to,
+			       (void *)((from & ~0xc00000) |
+					((((from >> 22) + shift) & 3) << 22)),
+			       curlen);
+			len -=3D curlen;
+			from +=3D curlen;
+			to +=3D curlen;
+		}
+		return;
+	}
+#ifdef __BIG_ENDIAN
+	if (bdipsw =3D=3D 0) {
+		/* BOOT Mode: Monitor ROM */
+		while (len) {
+			curlen =3D min((unsigned long)len,
+				     0x400000 - (from & (0x400000 - 1)));
+			memcpy(to, (void *)(from ^ 0x400000), curlen);
+			len -=3D curlen;
+			from +=3D curlen;
+			to +=3D curlen;
+		}
+		return;
+	}
+#endif
+	memcpy(to, (void *)from, len);
+}
+
+static void rbtx4939_flash_map_init(struct map_info *map)
+{
+	map->read =3D rbtx4939_flash_read16;
+	map->write =3D rbtx4939_flash_write16;
+	map->copy_from =3D rbtx4939_flash_copy_from;
+}
+
+static void __init rbtx4939_mtd_init(void)
+{
+	static struct {
+		struct platform_device dev;
+		struct resource res;
+		struct rbtx4939_flash_data data;
+	} pdevs[4];
+	int i;
+	static char names[4][8];
+	static struct mtd_partition parts[4];
+	struct rbtx4939_flash_data *boot_pdata =3D &pdevs[0].data;
+	u8 bdipsw =3D readb(rbtx4939_bdipsw_addr) & 0x0f;
+
+	if (bdipsw & 8) {
+		/* BOOT Mode: USER ROM1 / USER ROM2 */
+		boot_pdata->nr_parts =3D 4;
+		for (i =3D 0; i < boot_pdata->nr_parts; i++) {
+			sprintf(names[i], "img%d", 4 - i);
+			parts[i].name =3D names[i];
+			parts[i].size =3D 0x400000;
+			parts[i].offset =3D MTDPART_OFS_NXTBLK;
+		}
+	} else if (bdipsw =3D=3D 0) {
+		/* BOOT Mode: Monitor ROM */
+		boot_pdata->nr_parts =3D 2;
+		strcpy(names[0], "big");
+		strcpy(names[1], "little");
+		for (i =3D 0; i < boot_pdata->nr_parts; i++) {
+			parts[i].name =3D names[i];
+			parts[i].size =3D 0x400000;
+			parts[i].offset =3D MTDPART_OFS_NXTBLK;
+		}
+	} else {
+		/* BOOT Mode: ROM Emulator */
+		boot_pdata->nr_parts =3D 2;
+		parts[0].name =3D "boot";
+		parts[0].offset =3D 0xc00000;
+		parts[0].size =3D 0x400000;
+		parts[1].name =3D "user";
+		parts[1].offset =3D 0;
+		parts[1].size =3D 0xc00000;
+	}
+	boot_pdata->parts =3D parts;
+	boot_pdata->map_init =3D rbtx4939_flash_map_init;
+
+	for (i =3D 0; i < ARRAY_SIZE(pdevs); i++) {
+		struct resource *r =3D &pdevs[i].res;
+		struct platform_device *dev =3D &pdevs[i].dev;
+
+		r->start =3D 0x1f000000 - i * 0x1000000;
+		r->end =3D r->start + 0x1000000 - 1;
+		r->flags =3D IORESOURCE_MEM;
+		pdevs[i].data.width =3D 2;
+		dev->num_resources =3D 1;
+		dev->resource =3D r;
+		dev->id =3D i;
+		dev->name =3D "rbtx4939-flash";
+		dev->dev.platform_data =3D &pdevs[i].data;
+		platform_device_register(dev);
+	}
+}
+#else
+static void __init rbtx4939_mtd_init(void)
+{
+}
+#endif
+
 static void __init rbtx4939_arch_init(void)
 {
 	rbtx4939_pci_setup();
@@ -333,6 +489,7 @@ static void __init rbtx4939_device_init(void)
 	    platform_device_add_data(pdev, &smc_pdata, sizeof(smc_pdata)) ||
 	    platform_device_add(pdev))
 		platform_device_put(pdev);
+	rbtx4939_mtd_init();
 	/* TC58DVM82A1FT: tDH=3D10ns, tWP=3DtRP=3DtREADID=3D35ns */
 	tx4939_ndfmc_init(10, 35,
 			  (1 << 1) | (1 << 2),
--=20
1.5.6.3


