Skip to contentSkip to conversation

RFC: [PATCH V3 0/9 Integration of SmartMedia/xD into mtd subsystem

PATCH New Maxim Levitsky 2 participants 13 messages Jan 12, 2010 – Jan 15, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

This is third version of my patches.

Changes since V2:

* Resolved write speed problem, now write speed is around 1 MB/s more that twice the speed of windows driver.

* Made the FTL less dependent on mtd driver.
It now uses standard mtd functions for bad block handling. Also made initial device scan be safer against damaged media.

* FTL doesn't depend on common module any more.
This makes Kconfig much cleaner, and makes it possible to use the FTl without enabling the nand support.
Common module is cleaned up a lot.

Now, everything works, everything is fast (except maybe initial zone scan which takes around 2 seconds...)

Still bear in mind that FTL can destroy data, especially if your flash card is worn out.

I consider this version to be merge worthy.

Best regards,
Maxim Levitsky

#1 lore raw

PATCH 1/9 MTD: call remove notifiers before removing the device

Quoted 1 line

From 1855da2a21882c803e5dfcfb556fff5bc6989c31 Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Fri, 8 Jan 2010 15:14:20 +0200
Subject: [PATCH 1/9] MTD: call remove notifiers before removing the device

This is first step in implementing proper hotplug support.
Now all users of the mtd device have a chance to put the mtd device when they are notified to do so, and they have to do so to make hotplug work.

---
 drivers/mtd/mtdcore.c |   21 +++++++++++----------
 1 files changed, 11 insertions(+), 10 deletions(-)
drivers/mtd/mtdcore.c +11 -10
index c356c0a..87ff83f 100644
@@ -341,31 +341,32 @@ int add_mtd_device(struct mtd_info *mtd)
 int del_mtd_device (struct mtd_info *mtd)
 {
 	int ret;
+	struct mtd_notifier *not;
 
 	mutex_lock(&mtd_table_mutex);
 
 	if (mtd_table[mtd->index] != mtd) {
 		ret = -ENODEV;
-	} else if (mtd->usecount) {
+		goto out_error;
+	}
+
+	/* No need to get a refcount on the module containing
+		the notifier, since we hold the mtd_table_mutex */
+	list_for_each_entry(not, &mtd_notifiers, list)
+		not->remove(mtd);
+
+	if (mtd->usecount) {
 		printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
 		       mtd->index, mtd->name, mtd->usecount);
 		ret = -EBUSY;
 	} else {
-		struct mtd_notifier *not;
-
 		device_unregister(&mtd->dev);
-
-		/* No need to get a refcount on the module containing
-		   the notifier, since we hold the mtd_table_mutex */
-		list_for_each_entry(not, &mtd_notifiers, list)
-			not->remove(mtd);
-
 		mtd_table[mtd->index] = NULL;
-
 		module_put(THIS_MODULE);
 		ret = 0;
 	}
 
+out_error:
 	mutex_unlock(&mtd_table_mutex);
 	return ret;
 }
Signature · 1 line
1.6.3.3
#2 lore raw

PATCH 2/9 MTD: create lockless versions of {get,put}_mtd_device

In reply to Maxim Levitsky
Quoted 1 line

From 1531249dd2c0e33d727ed8cbd0719eb4e9af2266 Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Fri, 8 Jan 2010 15:51:22 +0200
Subject: [PATCH 2/9] MTD: create lockless versions of {get,put}_mtd_device

This will be used to resolve deadlock in block translation layer.

---
 drivers/mtd/mtdcore.c   |   61 ++++++++++++++++++++++++++++++-----------------
 include/linux/mtd/mtd.h |    3 +-
 2 files changed, 41 insertions(+), 23 deletions(-)
drivers/mtd/mtdcore.c +39 -22
index 87ff83f..96b4a05 100644
@@ -454,27 +454,38 @@ struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
 			ret = NULL;
 	}
 
-	if (!ret)
-		goto out_unlock;
-
-	if (!try_module_get(ret->owner))
-		goto out_unlock;
-
-	if (ret->get_device) {
-		err = ret->get_device(ret);
-		if (err)
-			goto out_put;
+	if (!ret) {
+		ret = ERR_PTR(err);
+		goto out;
 	}
 
-	ret->usecount++;
+	err = __get_mtd_device(ret);
+	if (err)
+		ret = ERR_PTR(err);
+out:
 	mutex_unlock(&mtd_table_mutex);
 	return ret;
+}
 
-out_put:
-	module_put(ret->owner);
-out_unlock:
-	mutex_unlock(&mtd_table_mutex);
-	return ERR_PTR(err);
+
+int __get_mtd_device(struct mtd_info *mtd)
+{
+	int err;
+
+	if (!try_module_get(mtd->owner))
+		return -ENODEV;
+
+	if (mtd->get_device) {
+
+		err = mtd->get_device(mtd);
+
+		if (err) {
+			module_put(mtd->owner);
+			return err;
+		}
+	}
+	mtd->usecount++;
+	return 0;
 }
 
 /**
@@ -525,18 +536,22 @@ out_unlock:
 
 void put_mtd_device(struct mtd_info *mtd)
 {
-	int c;
-
 	mutex_lock(&mtd_table_mutex);
-	c = --mtd->usecount;
-	if (mtd->put_device)
-		mtd->put_device(mtd);
+	__put_mtd_device(mtd);
 	mutex_unlock(&mtd_table_mutex);
-	BUG_ON(c < 0);
 
 	module_put(mtd->owner);
 }
 
+void __put_mtd_device(struct mtd_info *mtd)
+{
+	--mtd->usecount;
+	BUG_ON(mtd->usecount < 0);
+
+	if (mtd->put_device)
+		mtd->put_device(mtd);
+}
+
 /* default_mtd_writev - default mtd writev method for MTD devices that
  *			don't implement their own
  */
@@ -570,7 +585,9 @@ EXPORT_SYMBOL_GPL(add_mtd_device);
 EXPORT_SYMBOL_GPL(del_mtd_device);
 EXPORT_SYMBOL_GPL(get_mtd_device);
 EXPORT_SYMBOL_GPL(get_mtd_device_nm);
+EXPORT_SYMBOL_GPL(__get_mtd_device);
 EXPORT_SYMBOL_GPL(put_mtd_device);
+EXPORT_SYMBOL_GPL(__put_mtd_device);
 EXPORT_SYMBOL_GPL(register_mtd_user);
 EXPORT_SYMBOL_GPL(unregister_mtd_user);
 EXPORT_SYMBOL_GPL(default_mtd_writev);
include/linux/mtd/mtd.h +2 -1
index 0f32a9b..662d747 100644
@@ -290,8 +290,9 @@ extern int add_mtd_device(struct mtd_info *mtd);
 extern int del_mtd_device (struct mtd_info *mtd);
 
 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
+extern int __get_mtd_device(struct mtd_info *mtd);
+extern void __put_mtd_device(struct mtd_info *mtd);
 extern struct mtd_info *get_mtd_device_nm(const char *name);
-
 extern void put_mtd_device(struct mtd_info *mtd);
 
 
Signature · 1 line
1.6.3.3
#3 lore raw

PATCH 3/9 MTD: blkdevs: make hotplug work

In reply to Maxim Levitsky
Quoted 1 line

From 427c68fd6cb0fd228dbf8eebc832f63799b119ca Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Fri, 8 Jan 2010 15:52:12 +0200
Subject: [PATCH 3/9] MTD: blkdevs: make hotplug work

This changes the blkdev common module for translation layers to survive when underlying mtd device disappears.

To do so the following conceptual changes were made:

* disk queue and thread are now one per mtd device This was it is easy to flush and destroy the queue

* the struct mtd_blktrans_dev will now be freed automaticly when last user of the device quits.
All existing translation layers are adjusted

* ->open and release function of the translation layer will never be called twise or more in the row.
This makes code simplier.
Also the ->release will be called just before mtd device disappears This and above is the only visable changes on the outside.

Tested with mtdblock, ssfdc and my own sm_ftl on top of physicly hotplugable nand card.

---
 drivers/mtd/ftl.c            |    1 -
 drivers/mtd/inftlcore.c      |    1 -
 drivers/mtd/mtd_blkdevs.c    |  237 ++++++++++++++++++++++++++---------------
 drivers/mtd/mtdblock.c       |    1 -
 drivers/mtd/mtdblock_ro.c    |    1 -
 drivers/mtd/mtdcore.c        |    3 +-
 drivers/mtd/nftlcore.c       |    1 -
 drivers/mtd/rfd_ftl.c        |    1 -
 drivers/mtd/ssfdc.c          |    1 -
 include/linux/mtd/blktrans.h |   13 ++-
 10 files changed, 160 insertions(+), 100 deletions(-)
drivers/mtd/ftl.c +0 -1
index e56d6b4..62da9eb 100644
@@ -1082,7 +1082,6 @@ static void ftl_remove_dev(struct mtd_blktrans_dev *dev)
 {
 	del_mtd_blktrans_dev(dev);
 	ftl_freepart((partition_t *)dev);
-	kfree(dev);
 }
 
 static struct mtd_blktrans_ops ftl_tr = {
drivers/mtd/inftlcore.c +0 -1
index 8aca552..015a7fe 100755
@@ -139,7 +139,6 @@ static void inftl_remove_dev(struct mtd_blktrans_dev *dev)
 
 	kfree(inftl->PUtable);
 	kfree(inftl->VUtable);
-	kfree(inftl);
 }
 
 /*
drivers/mtd/mtd_blkdevs.c +150 -87

400 lines not on this page · raw message

drivers/mtd/mtdblock.c +0 -1
index 9f41b1a..d8322cc 100644
@@ -368,7 +368,6 @@ static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
 {
 	del_mtd_blktrans_dev(dev);
-	kfree(dev);
 }
 
 static struct mtd_blktrans_ops mtdblock_tr = {
drivers/mtd/mtdblock_ro.c +0 -1
index 852165f..54ff288 100644
@@ -49,7 +49,6 @@ static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
 static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
 {
 	del_mtd_blktrans_dev(dev);
-	kfree(dev);
 }
 
 static struct mtd_blktrans_ops mtdblock_tr = {
drivers/mtd/mtdcore.c +2 -1
index 96b4a05..0e86208 100644
@@ -540,7 +540,6 @@ void put_mtd_device(struct mtd_info *mtd)
 	__put_mtd_device(mtd);
 	mutex_unlock(&mtd_table_mutex);
 
-	module_put(mtd->owner);
 }
 
 void __put_mtd_device(struct mtd_info *mtd)
@@ -550,6 +549,8 @@ void __put_mtd_device(struct mtd_info *mtd)
 
 	if (mtd->put_device)
 		mtd->put_device(mtd);
+
+	module_put(mtd->owner);
 }
 
 /* default_mtd_writev - default mtd writev method for MTD devices that
drivers/mtd/nftlcore.c +0 -1
index 1002e18..a4578bf 100644
@@ -126,7 +126,6 @@ static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
 	del_mtd_blktrans_dev(dev);
 	kfree(nftl->ReplUnitTable);
 	kfree(nftl->EUNtable);
-	kfree(nftl);
 }
 
 /*
drivers/mtd/rfd_ftl.c +0 -1
index d2aa9c4..63b83c0 100644
@@ -817,7 +817,6 @@ static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
 	vfree(part->sector_map);
 	kfree(part->header_cache);
 	kfree(part->blocks);
-	kfree(part);
 }
 
 static struct mtd_blktrans_ops rfd_ftl_tr = {
drivers/mtd/ssfdc.c +0 -1
index 3f67e00..81c4ecd 100644
@@ -375,7 +375,6 @@ static void ssfdcr_remove_dev(struct mtd_blktrans_dev *dev)
 
 	del_mtd_blktrans_dev(dev);
 	kfree(ssfdc->logic_block_map);
-	kfree(ssfdc);
 }
 
 static int ssfdcr_readsect(struct mtd_blktrans_dev *dev,
include/linux/mtd/blktrans.h +8 -5
index 8b4aa05..054b053 100644
@@ -24,10 +24,15 @@ struct mtd_blktrans_dev {
 	int devnum;
 	unsigned long size;
 	int readonly;
-	void *blkcore_priv; /* gendisk in 2.5, devfs_handle in 2.4 */
-};
+	int deleted;
+	int open;
 
-struct blkcore_priv; /* Differs for 2.4 and 2.5 kernels; private */
+	struct gendisk *disk;
+	struct task_struct *thread;
+	struct request_queue *rq;
+	spinlock_t queue_lock;
+	void *priv;
+};
 
 struct mtd_blktrans_ops {
 	char *name;
@@ -60,8 +65,6 @@ struct mtd_blktrans_ops {
 	struct list_head devs;
 	struct list_head list;
 	struct module *owner;
-
-	struct mtd_blkcore_priv *blkcore_priv;
 };
 
 extern int register_mtd_blktrans(struct mtd_blktrans_ops *tr);
Signature · 1 line
1.6.3.3
#4 lore raw

PATCH 4/9 MTD: make mtdtrans thread freezeable.

In reply to Maxim Levitsky
Quoted 1 line

From c50bb924c12c742e53d66837ad8be48d93fc5fbe Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Tue, 12 Jan 2010 20:59:27 +0200
Subject: [PATCH 4/9] MTD: make mtdtrans thread freezeable.

This makes the mtd blktrans thread enter the freezer in between accesses to nand device.
This will ensure that we aren't suspening the system in the middle of page read/write and even worse erase, which is a bad idea.

---
 drivers/mtd/mtd_blkdevs.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
drivers/mtd/mtd_blkdevs.c +7 -7
index 04a875f..db996d6 100644
@@ -20,6 +20,7 @@
 #include <linux/init.h>
 #include <linux/mutex.h>
 #include <linux/kthread.h>
+#include <linux/freezer.h>
 #include <asm/uaccess.h>
 
 #include "mtdcore.h"
@@ -79,36 +80,35 @@ static int mtd_blktrans_thread(void *arg)
 	struct request_queue *rq = dev->rq;
 	struct request *req = NULL;
 
-	spin_lock_irq(rq->queue_lock);
+	set_freezable();
 
 	while (!kthread_should_stop()) {
 		int res;
 
+		try_to_freeze();
+
+		spin_lock_irq(rq->queue_lock);
 		if (!req && !(req = blk_fetch_request(rq))) {
 			set_current_state(TASK_INTERRUPTIBLE);
 			spin_unlock_irq(rq->queue_lock);
 			schedule();
-			spin_lock_irq(rq->queue_lock);
 			continue;
 		}
-
 		spin_unlock_irq(rq->queue_lock);
 
+
 		mutex_lock(&dev->lock);
 		res = do_blktrans_request(dev->tr, dev, req);
 		mutex_unlock(&dev->lock);
 
 		spin_lock_irq(rq->queue_lock);
-
 		if (!__blk_end_request_cur(req, res))
 			req = NULL;
+		spin_unlock_irq(rq->queue_lock);
 	}
 
 	if (req)
 		__blk_end_request_all(req, -EIO);
-
-	spin_unlock_irq(rq->queue_lock);
-
 	return 0;
 }
 
Signature · 1 line
1.6.3.3
#5 lore raw

PATCH 5/9 MTD: export few functions from nand_base.c

In reply to Maxim Levitsky
Quoted 1 line

From 5af23e4a8b1459ae53f7c39d7f3890b75ebf51a5 Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Tue, 12 Jan 2010 20:59:31 +0200
Subject: [PATCH 5/9] MTD: export few functions from nand_base.c

This exports:

nand_do_read_oob
nand_do_write_oob
nand_get_device
nand_put_device

This functions will be used to implement custom oob based bad block handling in upcoming smartmedia common module

---
 drivers/mtd/nand/nand_base.c |   18 +++++++++---------
 include/linux/mtd/nand.h     |   12 ++++++++++++
 2 files changed, 21 insertions(+), 9 deletions(-)
drivers/mtd/nand/nand_base.c +9 -9
index 8f2958f..13d1d67 100644
@@ -96,11 +96,6 @@ static struct nand_ecclayout nand_oob_128 = {
 		 .length = 78}}
 };
 
-static int nand_get_device(struct nand_chip *chip, struct mtd_info *mtd,
-			   int new_state);
-
-static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
-			     struct mtd_oob_ops *ops);
 
 /*
  * For devices which display every fart in the system on a separate LED. Is
@@ -114,7 +109,7 @@ DEFINE_LED_TRIGGER(nand_led_trigger);
  *
  * Deselect, release chip lock and wake up anyone waiting on the device
  */
-static void nand_release_device(struct mtd_info *mtd)
+void nand_release_device(struct mtd_info *mtd)
 {
 	struct nand_chip *chip = mtd->priv;
 
@@ -128,6 +123,7 @@ static void nand_release_device(struct mtd_info *mtd)
 	wake_up(&chip->controller->wq);
 	spin_unlock(&chip->controller->lock);
 }
+EXPORT_SYMBOL_GPL(nand_release_device);
 
 /**
  * nand_read_byte - [DEFAULT] read one byte from the chip
@@ -721,7 +717,7 @@ static void panic_nand_get_device(struct nand_chip *chip,
  *
  * Get the device and lock it for exclusive access
  */
-static int
+int
 nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
 {
 	spinlock_t *lock = &chip->controller->lock;
@@ -756,6 +752,7 @@ nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
 	remove_wait_queue(wq, &wait);
 	goto retry;
 }
+EXPORT_SYMBOL_GPL(nand_get_device);
 
 /**
  * panic_nand_wait - [GENERIC]  wait until the command is done
@@ -1536,7 +1533,7 @@ static int nand_write_oob_syndrome(struct mtd_info *mtd,
  *
  * NAND read out-of-band data from the spare area
  */
-static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
+int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
 			    struct mtd_oob_ops *ops)
 {
 	int page, realpage, chipnr, sndcmd = 1;
@@ -1620,6 +1617,7 @@ static int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
 	ops->oobretlen = ops->ooblen;
 	return 0;
 }
+EXPORT_SYMBOL_GPL(nand_do_read_oob);
 
 /**
  * nand_read_oob - [MTD Interface] NAND read data and/or out-of-band
@@ -2110,7 +2108,7 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
  *
  * NAND write out-of-band
  */
-static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
+int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
 			     struct mtd_oob_ops *ops)
 {
 	int chipnr, page, status, len;
@@ -2181,6 +2179,7 @@ static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(nand_do_write_oob);
 
 /**
  * nand_write_oob - [MTD Interface] NAND write data and/or out-of-band
@@ -3082,6 +3081,7 @@ EXPORT_SYMBOL_GPL(nand_scan_ident);
 EXPORT_SYMBOL_GPL(nand_scan_tail);
 EXPORT_SYMBOL_GPL(nand_release);
 
+
 static int __init nand_base_init(void)
 {
 	led_trigger_register_simple("nand-disk", &nand_led_trigger);
include/linux/mtd/nand.h +12 -0
index ccab9df..8c2333a 100644
@@ -469,6 +469,18 @@ extern int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
 extern int nand_do_read(struct mtd_info *mtd, loff_t from, size_t len,
 			size_t * retlen, uint8_t * buf);
 
+extern int nand_do_read_oob(struct mtd_info *mtd, loff_t from,
+			    struct mtd_oob_ops *ops);
+
+extern int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
+			     struct mtd_oob_ops *ops);
+
+extern int nand_get_device(struct nand_chip *chip,
+			struct mtd_info *mtd, int new_state);
+
+extern void nand_release_device(struct mtd_info *mtd);
+
+
 /**
  * struct platform_nand_chip - chip level device structure
  * @nr_chips:		max. number of chips to scan for
Signature · 1 line
1.6.3.3
#6 lore raw

PATCH 6/9 MTD: common module for smartmedia/xD support

In reply to Maxim Levitsky
Quoted 1 line

From 612ced620304d1991cca9c4f4f49b257d0931fdf Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Tue, 12 Jan 2010 21:09:29 +0200
Subject: [PATCH 6/9] MTD: common module for smartmedia/xD support

This small module implements few helpers that are usefull for nand drivers for SmartMedia/xD card readers.

---
 drivers/mtd/nand/Kconfig     |    9 +++
 drivers/mtd/nand/Makefile    |    1 +
 drivers/mtd/nand/sm_common.c |  126 ++++++++++++++++++++++++++++++++++++++++++
 drivers/mtd/nand/sm_common.h |   46 +++++++++++++++
 4 files changed, 182 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mtd/nand/sm_common.c
 create mode 100644 drivers/mtd/nand/sm_common.h
drivers/mtd/nand/Kconfig +9 -0
index 677cd53..13c1fb2 100644
@@ -18,6 +18,10 @@ config MTD_NAND_VERIFY_WRITE
 	  device thinks the write was successful, a bit could have been
 	  flipped accidentally due to device wear or something else.
 
+config MTD_NAND_SMARTMEDIA
+	boolean
+	default n
+
 config MTD_NAND_ECC_SMC
 	bool "NAND ECC Smart Media byte order"
 	default n
@@ -25,6 +29,11 @@ config MTD_NAND_ECC_SMC
 	  Software ECC according to the Smart Media Specification.
 	  The original Linux implementation had byte 0 and 1 swapped.
 
+config MTD_SM_COMMON
+	select MTD_NAND_SMARTMEDIA
+	tristate
+	default n
+
 config MTD_NAND_MUSEUM_IDS
 	bool "Enable chip ids for obsolete ancient NAND devices"
 	depends on MTD_NAND
drivers/mtd/nand/Makefile +1 -0
index 1407bd1..09891f6 100644
@@ -4,6 +4,7 @@
 
 obj-$(CONFIG_MTD_NAND)			+= nand.o nand_ecc.o
 obj-$(CONFIG_MTD_NAND_IDS)		+= nand_ids.o
+obj-$(CONFIG_MTD_SM_COMMON) 		+= sm_common.o
 
 obj-$(CONFIG_MTD_NAND_CAFE)		+= cafe_nand.o
 obj-$(CONFIG_MTD_NAND_SPIA)		+= spia.o
drivers/mtd/nand/sm_common.c new file +126 -0

126 lines not on this page · raw message

drivers/mtd/nand/sm_common.h new file +46 -0

46 lines not on this page · raw message

Signature · 1 line
1.6.3.3
#7 lore raw

PATCH 7/9 MTD: add few workarounds to nand system for SmartMedia/xD chips.

In reply to Maxim Levitsky
Quoted 1 line

From 43f5dddd8787ffe32b27cb63ac3e7b72895a3d14 Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Tue, 12 Jan 2010 21:09:34 +0200
Subject: [PATCH 7/9] MTD: add few workarounds to nand system for SmartMedia/xD chips.

* Add an option NAND_SMARTMEDIA that can be set by nand driver If set, it will cause separate ID table to be used, which includes mask rom devices and new xD cards

* Workaround for wrong write protect status on some xD cards

---
 drivers/mtd/nand/nand_base.c |   30 ++++++++++++++++++++++--------
 drivers/mtd/nand/nand_ids.c  |   39 +++++++++++++++++++++++++++++++++++++++
 drivers/mtd/nand/sm_common.c |    2 +-
 include/linux/mtd/nand.h     |   11 +++++++++++
 4 files changed, 73 insertions(+), 9 deletions(-)
drivers/mtd/nand/nand_base.c +22 -8
index 13d1d67..90409f6 100644
@@ -397,9 +397,18 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
 static int nand_check_wp(struct mtd_info *mtd)
 {
 	struct nand_chip *chip = mtd->priv;
+	int wp;
+
+	/* broken xD cards report WP despite beeing writable */
+	if (chip->options & NAND_BROKEN_XD)
+		return 0;
+
 	/* Check the WP bit */
 	chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
-	return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
+	wp = (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
+
+
+	return wp;
 }
 
 /**
@@ -2624,14 +2633,18 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 	}
 
 	/* Lookup the flash id */
-	for (i = 0; nand_flash_ids[i].name != NULL; i++) {
-		if (dev_id == nand_flash_ids[i].id) {
-			type =  &nand_flash_ids[i];
+#ifdef CONFIG_MTD_NAND_SMARTMEDIA
+	if (chip->options & NAND_SMARTMEDIA)
+		type = nand_smartmedia_flash_ids;
+	else
+#endif
+		type = nand_flash_ids;
+
+	for (i = 0; type->name != NULL; type++)
+		if (dev_id == type->id)
 			break;
-		}
-	}
 
-	if (!type)
+	if (!type->name)
 		return ERR_PTR(-ENODEV);
 
 	if (!mtd->name)
@@ -2988,7 +3001,8 @@ int nand_scan_tail(struct mtd_info *mtd)
 
 	/* Fill in remaining MTD driver data */
 	mtd->type = MTD_NANDFLASH;
-	mtd->flags = MTD_CAP_NANDFLASH;
+	mtd->flags = chip->options & NAND_ROM ? MTD_CAP_ROM :
+							MTD_CAP_NANDFLASH;
 	mtd->erase = nand_erase;
 	mtd->point = NULL;
 	mtd->unpoint = NULL;
drivers/mtd/nand/nand_ids.c +39 -0
index 69ee2c9..799c080 100644
@@ -127,6 +127,45 @@ struct nand_flash_dev nand_flash_ids[] = {
 	{NULL,}
 };
 
+#ifdef CONFIG_MTD_NAND_SMARTMEDIA
+struct nand_flash_dev nand_smartmedia_flash_ids[] = {
+
+	/* SmartMedia */
+	{"SmartMedia 1MiB 5V",		0x6e, 256, 1, 0x1000, 0},
+	{"SmartMedia 1MiB 3,3V",	0xe8, 256, 1, 0x1000, 0},
+	{"SmartMedia 1MiB 3,3V",	0xec, 256, 1, 0x1000, 0},
+	{"SmartMedia 2MiB 3,3V",	0xea, 256, 2, 0x1000, 0},
+	{"SmartMedia 2MiB 5V",		0x64, 256, 2, 0x1000, 0},
+	{"SmartMedia 2MiB 3,3V ROM",	0x5d, 512, 2, 0x2000, NAND_ROM},
+	{"SmartMedia 4MiB 3,3V",	0xe3, 512, 4, 0x2000, 0},
+	{"SmartMedia 4MiB 3,3/5V",	0xe5, 512, 4, 0x2000, 0},
+	{"SmartMedia 4MiB 5V",		0x6b, 512, 4, 0x2000, 0},
+	{"SmartMedia 4MiB 3,3V ROM",	0xd5, 512, 4, 0x2000, NAND_ROM},
+	{"SmartMedia 8MiB 3,3V",	0xe6, 512, 8, 0x2000, 0},
+	{"SmartMedia 8MiB 3,3V ROM",	0xd6, 512, 8, 0x2000, NAND_ROM},
+
+#define XD_TYPEM	NAND_NO_AUTOINCR | NAND_BROKEN_XD
+	/* xD / SmartMedia */
+	{"SmartMedia/xD 16MiB 3,3V",	0x73, 512, 16, 0x4000, 0},
+	{"SmartMedia 16MiB 3,3V ROM",	0x57, 512, 16, 0x4000, NAND_ROM},
+	{"SmartMedia/xD 32MiB 3,3V",	0x75, 512, 32, 0x4000, 0},
+	{"SmartMedia 32MiB 3,3V ROM",	0x58, 512, 32, 0x4000, NAND_ROM},
+	{"SmartMedia/xD 64MiB 3,3V",	0x76, 512, 64, 0x4000, 0},
+	{"SmartMedia 64MiB 3,3V ROM",	0xd9, 512, 64, 0x4000, NAND_ROM},
+	{"SmartMedia/xD 128MiB 3,3V",	0x79, 512, 128, 0x4000, 0},
+	{"SmartMedia 128MiB 3,3V ROM",	0xda, 512, 128, 0x4000, NAND_ROM},
+	{"SmartMedia/xD 256MiB 3,3V",	0x71, 512, 256, 0x4000, XD_TYPEM},
+	{"SmartMedia 256MiB 3,3V ROM",	0x5b, 512, 256, 0x4000, NAND_ROM},
+
+	/* xD only */
+	{"xD 512MiB 3,3V",		0xDC, 512, 512, 0x4000, XD_TYPEM},
+	{"xD 1GiB 3,3V",		0xD3, 512, 1024, 0x4000, XD_TYPEM},
+	{"xD 2GiB 3,3V",		0xD5, 512, 2048, 0x4000, XD_TYPEM},
+	{NULL,}
+};
+EXPORT_SYMBOL(nand_smartmedia_flash_ids);
+#endif
+
 /*
 *	Manufacturer ID list
 */
drivers/mtd/nand/sm_common.c +1 -1
index 55ae3ff..8df170d 100644
@@ -91,7 +91,7 @@ int sm_register_device(struct mtd_info *mtd)
 	struct nand_chip *chip = (struct nand_chip *)mtd->priv;
 	int ret;
 
-	chip->options |= NAND_SKIP_BBTSCAN;
+	chip->options |= NAND_SKIP_BBTSCAN | NAND_SMARTMEDIA;
 
 	/* Scan for card properties */
 	ret = nand_scan_ident(mtd, 1);
include/linux/mtd/nand.h +11 -0
index 8c2333a..bb25cd8 100644
@@ -170,6 +170,12 @@ typedef enum {
 /* Chip does not allow subpage writes */
 #define NAND_NO_SUBPAGE_WRITE	0x00000200
 
+/* Device is one of 'new' xD cards that expose fake nand command set */
+#define NAND_BROKEN_XD		0x00000400
+
+/* Device behaves just like nand, but is readonly */
+#define NAND_ROM		0x00000800
+
 /* Options valid for Samsung large page devices */
 #define NAND_SAMSUNG_LP_OPTIONS \
 	(NAND_NO_PADDING | NAND_CACHEPRG | NAND_COPYBACK)
@@ -195,9 +201,13 @@ typedef enum {
 /* This option is defined if the board driver allocates its own buffers
    (e.g. because it needs them DMA-coherent */
 #define NAND_OWN_BUFFERS	0x00040000
+
 /* Chip may not exist, so silence any errors in scan */
 #define NAND_SCAN_SILENT_NODEV	0x00080000
 
+/* controller supports only xD/SmartMedia cards*/
+#define NAND_SMARTMEDIA		0x00100000
+
 /* Options set by nand scan */
 /* Nand scan has allocated controller struct */
 #define NAND_CONTROLLER_ALLOC	0x80000000
@@ -458,6 +468,7 @@ struct nand_manufacturers {
 };
 
 extern struct nand_flash_dev nand_flash_ids[];
+extern struct nand_flash_dev nand_smartmedia_flash_ids[];
 extern struct nand_manufacturers nand_manuf_ids[];
 
 extern int nand_scan_bbt(struct mtd_info *mtd, struct nand_bbt_descr *bd);
Signature · 1 line
1.6.3.3
#8 lore raw

PATCH 8/9 MTD: Add nand driver for ricoh xD/SmartMedia reader

In reply to Maxim Levitsky
Quoted 1 line

From ff647dfdddaf4fafdc51fd320055a602b7353af0 Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Tue, 12 Jan 2010 21:09:35 +0200
Subject: [PATCH 8/9] MTD: Add nand driver for ricoh xD/SmartMedia reader

This adds a driver for Ricoh xD card reader with PCI id 0x0852

Since the reader is a part of larger mulifunction chip, it is hard to determine the correct model name. Might be R5C852.

Driver is complete, but bewere of the fact that some (probably only type M) xD cards are 'fake' which means that they have an on board CPU and expose emulated nand command set

These cards don't even store the oob area on the flash, but generate it on the fly from something else.

Thus they demand to have proper values written in the oob area, and therefore only useful with SmartMedia FTL.

---
 MAINTAINERS               |    6 +
 drivers/mtd/nand/Kconfig  |   11 +
 drivers/mtd/nand/Makefile |    1 +
 drivers/mtd/nand/r822.c   | 1059 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/mtd/nand/r822.h   |  150 +++++++
 include/linux/pci_ids.h   |    2 +
 6 files changed, 1229 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mtd/nand/r822.c
 create mode 100644 drivers/mtd/nand/r822.h
MAINTAINERS +6 -0
index 8f0109b..81c451b 100644
@@ -4585,6 +4585,12 @@ S:	Maintained
 F:	Documentation/rfkill.txt
 F:	net/rfkill/
 
+RICOH SMARTMEDIA/XD DRIVER
+M:	Maxim Levitsky <[email protected]>
+S:	Maintained
+F:	drivers/mtd/nand/r822.c
+F:	drivers/mtd/nand/r822.h
+
 RISCOM8 DRIVER
 S:	Orphan
 F:	Documentation/serial/riscom8.txt
drivers/mtd/nand/Kconfig +11 -0
index 13c1fb2..20803fa 100644
@@ -113,6 +113,17 @@ config MTD_NAND_TS7250
 config MTD_NAND_IDS
 	tristate
 
+config MTD_NAND_RICOH
+	tristate "Ricoh xD card reader"
+	default n
+	select MTD_SM_COMMON
+	help
+	  Enable support for Ricoh xD card reader
+	  You also need to enable ether
+	  NAND SSFDC (SmartMedia) read only translation layer' or new
+	  expermental, readwrite
+	  'SmartMedia/xD new translation layer'
+
 config MTD_NAND_AU1550
 	tristate "Au1550/1200 NAND support"
 	depends on SOC_AU1200 || SOC_AU1550
drivers/mtd/nand/Makefile +1 -0
index 09891f6..dc7c0bb 100644
@@ -43,5 +43,6 @@ obj-$(CONFIG_MTD_NAND_TXX9NDFMC)	+= txx9ndfmc.o
 obj-$(CONFIG_MTD_NAND_W90P910)		+= w90p910_nand.o
 obj-$(CONFIG_MTD_NAND_NOMADIK)		+= nomadik_nand.o
 obj-$(CONFIG_MTD_NAND_BCM_UMI)		+= bcm_umi_nand.o nand_bcm_umi.o
+obj-$(CONFIG_MTD_NAND_RICOH)		+= r822.o
 
 nand-objs := nand_base.o nand_bbt.o
drivers/mtd/nand/r822.c new file +1059 -0

1,059 lines not on this page · raw message

drivers/mtd/nand/r822.h new file +150 -0

150 lines not on this page · raw message

include/linux/pci_ids.h +2 -0
index cca8a04..cef49f2 100644
@@ -1523,6 +1523,8 @@
 #define PCI_DEVICE_ID_RICOH_R5C822	0x0822
 #define PCI_DEVICE_ID_RICOH_R5C832	0x0832
 #define PCI_DEVICE_ID_RICOH_R5C843	0x0843
+#define PCI_DEVICE_ID_RICOH_R5C852	0x0852
+
 
 #define PCI_VENDOR_ID_DLINK		0x1186
 #define PCI_DEVICE_ID_DLINK_DGE510T	0x4c00
Signature · 1 line
1.6.3.3
#9 lore raw

PATCH 9/9 MTD: Add new SmartMedia/xD FTL

In reply to Maxim Levitsky
Quoted 1 line

From 764ff2c5d8f563f5acb9bb7e6b20112652769714 Mon Sep 17 00:00:00 2001

From: Maxim Levitsky <[email protected]>
Date: Tue, 12 Jan 2010 21:09:35 +0200
Subject: [PATCH 9/9] MTD: Add new SmartMedia/xD FTL

This implements new readwrite SmartMedia/xd FTL.

To work properly, nand driver must define an oob layout that contains reserved area and both copies of the LBA
(8 bytes total)
Also mtd driver should support proper ECC and badblock verification based on hidden oob parts

---
 drivers/mtd/Kconfig  |   11 +
 drivers/mtd/Makefile |    1 +
 drivers/mtd/sm_ftl.c | 1019 ++++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/mtd/sm_ftl.h |   70 ++++
 4 files changed, 1101 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mtd/sm_ftl.c
 create mode 100644 drivers/mtd/sm_ftl.h
drivers/mtd/Kconfig +11 -0
index ecf90f5..00d2fe4 100644
@@ -304,6 +304,17 @@ config SSFDC
 	  This enables read only access to SmartMedia formatted NAND
 	  flash. You can mount it with FAT file system.
 
+
+config SM_FTL
+	tristate "SmartMedia/xD new translation layer"
+	depends on EXPERIMENTAL
+	help
+	  This enables new and very EXPERMENTAL support for SmartMedia/xD
+	  FTL (Flash tanslation layer)
+	  Write support isn't yet well tested, therefore this code IS likely to
+	  eat your card, so please don't use it together with valuable data.
+	  Use readonly driver (CONFIG_SSFDC) instead.
+
 config MTD_OOPS
 	tristate "Log panic/oops to an MTD buffer"
 	depends on MTD
drivers/mtd/Makefile +1 -0
index 82d1e4d..d53357b 100644
@@ -24,6 +24,7 @@ obj-$(CONFIG_NFTL)		+= nftl.o
 obj-$(CONFIG_INFTL)		+= inftl.o
 obj-$(CONFIG_RFD_FTL)		+= rfd_ftl.o
 obj-$(CONFIG_SSFDC)		+= ssfdc.o
+obj-$(CONFIG_SM_FTL)		+= sm_ftl.o
 obj-$(CONFIG_MTD_OOPS)		+= mtdoops.o
 
 nftl-objs		:= nftlcore.o nftlmount.o
drivers/mtd/sm_ftl.c new file +1019 -0

1,019 lines not on this page · raw message

drivers/mtd/sm_ftl.h new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
1.6.3.3
#10 lore raw
In reply to Maxim Levitsky
Quoted 19 lines · Maxim Levitsky

This is third version of my patches.

Changes since V2:

* Resolved write speed problem, now write speed is around 1 MB/s more that twice the speed of windows driver.

* Made the FTL less dependent on mtd driver.
It now uses standard mtd functions for bad block handling. Also made initial device scan be safer against damaged media.

* FTL doesn't depend on common module any more.
This makes Kconfig much cleaner, and makes it possible to use the FTl without enabling the nand support.
Common module is cleaned up a lot.

Now, everything works, everything is fast (except maybe initial zone scan which takes around 2 seconds...)

Quoted 7 lines

Still bear in mind that FTL can destroy data, especially if your flash card is worn out.

I consider this version to be merge worthy.

Best regards,
Maxim Levitsky

Now that everything works fine, what shall I do next to get that merged?

Best regards,
Maxim Levitsky

#11 lore raw
Quoted 4 lines
Quoted 2 lines

Still bear in mind that FTL can destroy data,

especially if your flash

Quoted 1 line

card is worn out.

I sort of worry about this particular issue.

When I was doing my FTL, handling of bad blocks was one of the major (if not the primary) implementation problems.

      __________________________________________________________________________________

See what's on at the movies in your area. Find out now: http://au.movies.yahoo.com/session-times/

#12 lore raw
Quoted 9 lines · Alex Dubov
Quoted 4 lines
Quoted 2 lines

Still bear in mind that FTL can destroy data,

especially if your flash

Quoted 1 line

card is worn out.

I sort of worry about this particular issue.

When I was doing my FTL, handling of bad blocks was one of the major (if not the primary) implementation problems.

This is more or less an disclaimer.
If I compare what windows driver does versus mine, I do much more checking.

My FTL maybe is a bit erase happy, as it tries to erase blocks it found errors that likely not result of wear but a cut in the power.

Here is a summary of algorithm that I do for scanning:

1. Test is block is marked as bad.
Here I don't cheat and read whole block, because data_status could be set for a specific sector.
However this is a bit slow, because it causes oob of all sectors to be read.

2. Read LBA of each sector. Due to mtd layering this makes me read oob of each sector again.
If I find a sector with lba that can't be read or a sector that has a different lba that first, I erase the block. This I might revisit.

3. Update the LBA->physical table. If I see that this LBA is taken, I check again both candidatures, and this time I also read the sector contents, which will fail if there are unrecoverable ECC errors. If one of sectors turns out bad this way I pick the other one, if not I just erase one of the sectors (this is likely a powerfalll situation too)

Every time an erase is made, hardware will return errors if failed When this happens I mark block as bad.

Best regards,
Maxim Levitsky