Skip to contentSkip to conversation

mtd: nand: Add driver for M-sys / Sandisk diskonchip G4

PATCH In review Mike Dunn 2 participants 7 messages Nov 4, 2011 – Nov 11, 2011

Subsystems (2)
lore
Share & tools
lore

Mike Dunn

author
#0 lore raw

PATCH v3 1/1 mtd: nand: Add driver for M-sys / Sandisk diskonchip G4

This is a nand driver for the diskonchip G4 in my Palm Treo680. It's been fairly well tested; it passes the nandtest utility in mtd-utils, and also the kernel tests mtd_pagetest and mtd_readtest. Common mtd-utils work as well (nanddump, nandwrite, flash_erase, ...). A ubifs was created on it and seems to be working well, though more stress testing is needed.

Since version 2:

- Replace DEBUG macros defined in mtd.h with dev_dbg (intended for V2). Compiles against l2-mtd kernel (tested against mtd-2.6 kernel).

- Fix oversight I noticed which would cause read errors in user oob bytes not to be corrected (and array out-of-bounds to occur)

- Implement Ivan's suggestion for detecting bit flips on read of a blank page.

---

Still hoping for some comments ragarding suitability of the nand interface for these devices.

 drivers/mtd/nand/Kconfig  |   12 +
 drivers/mtd/nand/Makefile |    1 +
 drivers/mtd/nand/docg4.c  | 1220 +++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mtd/docg4.h |   24 +
 4 files changed, 1257 insertions(+), 0 deletions(-)
 create mode 100644 drivers/mtd/nand/docg4.c
 create mode 100644 include/linux/mtd/docg4.h
drivers/mtd/nand/Kconfig +12 -0
index 4c34252..abb5a33 100644
@@ -319,6 +319,18 @@ config MTD_NAND_DISKONCHIP_BBTWRITE
 	  load time (assuming you build diskonchip as a module) with the module
 	  parameter "inftl_bbt_write=1".
 
+config MTD_NAND_DOCG4
+	tristate "Support for DiskOnChip G4 (EXPERIMENTAL)"
+	depends on EXPERIMENTAL
+	select BCH
+	select BITREVERSE
+	help
+	  Support for diskonchip G4 nand flash, found in several smartphones,
+	  such as the Palm Treo680 and HTC Prophet.
+
+	  There is currently no support for the saftl filesystem; however a
+	  ubifs has been succesfully tested on the device.
+
 config MTD_NAND_SHARPSL
 	tristate "Support for NAND Flash on Sharp SL Series (C7xx + others)"
 	depends on ARCH_PXA
drivers/mtd/nand/Makefile +1 -0
index 5745d83..70993e7 100644
@@ -20,6 +20,7 @@ obj-$(CONFIG_MTD_NAND_PPCHAMELEONEVB)	+= ppchameleonevb.o
 obj-$(CONFIG_MTD_NAND_S3C2410)		+= s3c2410.o
 obj-$(CONFIG_MTD_NAND_DAVINCI)		+= davinci_nand.o
 obj-$(CONFIG_MTD_NAND_DISKONCHIP)	+= diskonchip.o
+obj-$(CONFIG_MTD_NAND_DOCG4)		+= docg4.o
 obj-$(CONFIG_MTD_NAND_FSMC)		+= fsmc_nand.o
 obj-$(CONFIG_MTD_NAND_H1900)		+= h1910.o
 obj-$(CONFIG_MTD_NAND_RTC_FROM4)	+= rtc_from4.o
drivers/mtd/nand/docg4.c new file +1220 -0

1,220 lines not on this page · raw message

include/linux/mtd/docg4.h new file +24 -0
new file mode 100644
index 0000000..654699c
@@ -0,0 +1,24 @@
+/*
+ *  Copyright (C) 2011 Mike Dunn <[email protected]>
+ *
+ * Nand mtd driver for M-Systems DiskOnChip G4 device
+ *
+ * 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
+ */
+
+struct docg4_nand_platform_data {
+	struct mtd_partition *partitions;
+	unsigned int nr_partitions;
+};
Signature · 1 line
1.7.3.4
#1 lore raw
Quoted 5 lines · Mike Dunn

This is a nand driver for the diskonchip G4 in my Palm Treo680. It's been fairly well tested; it passes the nandtest utility in mtd-utils, and also the kernel tests mtd_pagetest and mtd_readtest. Common mtd-utils work as well (nanddump, nandwrite, flash_erase, ...). A ubifs was created on it and seems to be working well, though more stress testing is needed.

Hi Mike,

drivers/mtd/nand/docg4.c line 212

Quoted 6 lines
drivers/mtd/nand/docg4.c new file +6 -0
+static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
+{
+	int i;
+	struct nand_chip *nand = mtd->priv;
+	uint16_t *p = (uint16_t *) buf;
+	len >>= 1;

Is it granted that len is an even number ? Or did you mean here docg4_read_buf16 as a function name (on the same naming as the writing one below) ?

...zip...

drivers/mtd/nand/docg4.c line 512

Quoted 13 lines
drivers/mtd/nand/docg4.c new file +13 -0
+	writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
+	writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	docg4_wait(mtd, nand);
+	writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
+	writew(DOC_CMD_READ_STATUS, docptr + DOC_FLASHCOMMAND);
+	writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);

Wouldn't that be better doc_nop(docptr, 5) ?

...zip...

drivers/mtd/nand/docg4.c

Quoted 6 lines
drivers/mtd/nand/docg4.c new file +6 -0
+static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
+			   int page)
+{
+	/*
+	 * This is not really supported, because MLC nand must write oob bytes
+	 * at the same time as page data.

I don't think that's true. The docg3 is an MLC, with NAND memory, and page data can be written, and the in a subsequent write oob data can be written. I think it's more the NAND kernel interface which drives that (and maybe NAND interface specification, I don't know).

drivers/mtd/nand/docg4.c line 932

Quoted 10 lines
drivers/mtd/nand/docg4.c new file +10 -0
+static int __init read_factory_bbt(struct mtd_info *mtd)
+{
+	/*
+	 * The device contains a factory bad block table on page 4, but the
+	 * table is not updated by this driver.  Instead, this function is
+	 * called during initialization to read it and update the memory-based
+	 * bbt accordingly.
+	 */
+
+	/* TODO: figure out how to interpret the table; mine is all ff's */

If it's the same as on docg3, each bit is a marker for one block, and the following formula could apply:

          is_good = bbt[block >> 3] & (1 << (block & 0x7));

drivers/mtd/nand/docg4.c line 1164

Quoted 11 lines
drivers/mtd/nand/docg4.c new file +11 -0
+	retval = mtd_device_register(mtd, NULL, 0);
+	if (retval)
+		goto fail;
+
+	if (pdata->nr_partitions > 0) {
+		int i;
+		for (i = 0; i < pdata->nr_partitions; i++)
+			pdata->partitions[i].ecclayout = &docg4_oobinfo;
+		retval = mtd_device_register(mtd, pdata->partitions,
+					     pdata->nr_partitions);
+	}

Why not use mtd_device_parse_register(), which will handle handle partitions and device registration at the same time ?

Cheers.

Signature · 1 line
Robert
author
#2 lore raw

Hi Robert, thanks for taking a look and commenting.

Quoted 9 lines · Robert Jarzmik
Quoted 5 lines · Mike Dunn

This is a nand driver for the diskonchip G4 in my Palm Treo680. It's been fairly well tested; it passes the nandtest utility in mtd-utils, and also the kernel tests mtd_pagetest and mtd_readtest. Common mtd-utils work as well (nanddump, nandwrite, flash_erase, ...). A ubifs was created on it and seems to be working well, though more stress testing is needed.

Hi Mike,

Quoted 8 lines

drivers/mtd/nand/docg4.c line 212

Quoted 6 lines
drivers/mtd/nand/docg4.c new file +6 -0
+static void docg4_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
+{
+	int i;
+	struct nand_chip *nand = mtd->priv;
+	uint16_t *p = (uint16_t *) buf;
+	len >>= 1;

Is it granted that len is an even number ? Or did you mean here docg4_read_buf16 as a function name (on the same naming as the writing one below) ?

Yes, len is even, because nand_base knows that buswidth is 16 bits. Actually this is not my code. This and docg4_write_buf16() were cut verbatum from nand_base.c and renamed. I had to duplicate this code because nand_scan_ident() is not called and the defaults are set within that call chain (inappropriate place, IMHO). The functions are not exported. Fortunately these two trivial functions are the only instances of redundant code. Good catch, though. I should be consistent and call it docg4_read_buf16(), and/or add a comment explaining count refers to 16 bit half-words.

Quoted 16 lines

...zip...

drivers/mtd/nand/docg4.c line 512

Quoted 13 lines
drivers/mtd/nand/docg4.c new file +13 -0
+	writew(DOCG4_SEQ_PAGEPROG, docptr + DOC_FLASHSEQUENCE);
+	writew(DOC_CMD_PROG_CYCLE2, docptr + DOC_FLASHCOMMAND);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	docg4_wait(mtd, nand);
+	writew(DOCG4_SEQ_FLUSH, docptr + DOC_FLASHSEQUENCE);
+	writew(DOC_CMD_READ_STATUS, docptr + DOC_FLASHCOMMAND);
+	writew(DOC_ECCCONF0_READ_MODE | 4, docptr + DOC_ECCCONF0);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);

Wouldn't that be better doc_nop(docptr, 5) ?

No. If it's a function that loops, you're inserting too much delay due to loop overhead (unless the compiler unrolls it, but you don't know that) and you may as well use some generic delay function and not bother with the nop register at all. I wanted to use the precise delay observed in the TrueFFS code to (1) avoid too much delay for the sake of performance, (2) in case the timing is critical and too much delay would cause problems, or (3) "nop" really isn't what it says (i.e. no operation). If there were a C preprocessor directive equivalent to the assembly .rept directive, I would use it.

Quoted 12 lines

...zip...

drivers/mtd/nand/docg4.c

Quoted 6 lines
drivers/mtd/nand/docg4.c new file +6 -0
+static int docg4_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
+			   int page)
+{
+	/*
+	 * This is not really supported, because MLC nand must write oob bytes
+	 * at the same time as page data.

I don't think that's true. The docg3 is an MLC, with NAND memory, and page data can be written, and the in a subsequent write oob data can be written. I think it's more the NAND kernel interface which drives that (and maybe NAND interface specification, I don't know).

No actually ecc.write_oob - which this function defines - is a nand interface function. I never observed oob-only writes while reverse engineering (read oob-only yes, but not write). Can you write oob-only on the G3? Even if it were possible, the nand_write utility wants to write the oob *before* the page data. This hack allows that utility to work. Maybe the comment should say "oob can't be written before the page data".

Quoted 12 lines

drivers/mtd/nand/docg4.c line 932

Quoted 10 lines
drivers/mtd/nand/docg4.c new file +10 -0
+static int __init read_factory_bbt(struct mtd_info *mtd)
+{
+	/*
+	 * The device contains a factory bad block table on page 4, but the
+	 * table is not updated by this driver.  Instead, this function is
+	 * called during initialization to read it and update the memory-based
+	 * bbt accordingly.
+	 */
+
+	/* TODO: figure out how to interpret the table; mine is all ff's */

If it's the same as on docg3, each bit is a marker for one block, and the following formula could apply:

Quoted 1 line
          is_good = bbt[block >> 3] & (1 << (block & 0x7));

Thanks. Do any of your devices have bad blocks marked in this table? Do you know how to modify the table?

Quoted 5 lines
Quoted 5 lines
drivers/mtd/nand/docg4.c new file +5 -0
+	retval = mtd_device_register(mtd, NULL, 0);
+	if (retval)
+		goto fail;
+
+	if (pdata->nr_partitions > 0) {
Quoted 8 lines

drivers/mtd/nand/docg4.c line 1164

Quoted 6 lines
drivers/mtd/nand/docg4.c new file +6 -0
+		int i;
+		for (i = 0; i < pdata->nr_partitions; i++)
+			pdata->partitions[i].ecclayout = &docg4_oobinfo;
+		retval = mtd_device_register(mtd, pdata->partitions,
+					     pdata->nr_partitions);
+	}

Why not use mtd_device_parse_register(), which will handle handle partitions and device registration at the same time ?

Because I didn't know about it :-)

Thanks again,
Mike

#3 lore raw
Quoted 11 lines · Mike Dunn

Hi Robert, thanks for taking a look and commenting.

Quoted 6 lines · Robert Jarzmik

drivers/mtd/nand/docg4.c line 510

Quoted 5 lines
drivers/mtd/nand/docg4.c new file +5 -0
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);

Wouldn't that be better doc_nop(docptr, 5) ?

Quoted 8 lines

No. If it's a function that loops, you're inserting too much delay due to loop overhead (unless the compiler unrolls it, but you don't know that) and you may as well use some generic delay function and not bother with the nop register at all. I wanted to use the precise delay observed in the TrueFFS code to (1) avoid too much delay for the sake of performance, (2) in case the timing is critical and too much delay would cause problems, or (3) "nop" really isn't what it says (i.e. no operation). If there were a C preprocessor directive equivalent to the assembly .rept directive, I would use it.

As you wish, but :
(a) The nops are here to add a minimum delay
(b) The performance would not be an issue, and yes, the compiler could unroll the loop as the number is a compile time constant
(c) nop operation is a delay, that's actually how I understand it
(d) it does work well on docg3
(e) and you can do as you wish, it's your driver :)

Quoted 6 lines

No actually ecc.write_oob - which this function defines - is a nand interface function. I never observed oob-only writes while reverse engineering (read oob-only yes, but not write). Can you write oob-only on the G3? Even if it were possible, the nand_write utility wants to write the oob *before* the page data. This hack allows that utility to work. Maybe the comment should say "oob can't be written before the page data".

Yes, I can write OOB only, page only, or both.

And I have the same problem with nandwrite.
Now consider you have 2 nandwrites working in parallel :
nandwrite1 nandwrite2
write_oob(OOB1)

                      write_oob(OOB2)

write_page(page1) ------------------------> OOB1 was overrun
write_page(page2)

That really really bothers me. It's not about your code, I think you have no choice. My concern is rather about nandwrite utility. My point is that it could have better used write_oob(page1, OOB1).
And in that respect, I think there's something broken in it.

Quoted 7 lines
Quoted 3 lines

If it's the same as on docg3, each bit is a marker for one block, and the following formula could apply:

          is_good = bbt[block >> 3] & (1 << (block & 0x7));

Thanks. Do any of your devices have bad blocks marked in this table? Do you know how to modify the table?

No, mine G3 has no such blocks.
And I feel we can't modify them. After all, they are in the OTP area. My guess is that they are set up at chip creation as initial bad blocks (ie. factory bad blocks).
I think there is a difference between bad block (factory bad blocks) and worn out blocks (after erases, having more bitflips that ECC can fix).

Cheers.

Signature · 1 line
Robert
author
#4 lore raw
Quoted 20 lines · Robert Jarzmik
Quoted 10 lines · Mike Dunn

Hi Robert, thanks for taking a look and commenting.

Quoted 6 lines · Robert Jarzmik

drivers/mtd/nand/docg4.c line 510

Quoted 5 lines
drivers/mtd/nand/docg4.c new file +5 -0
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);
+	doc_nop(docptr);

Wouldn't that be better doc_nop(docptr, 5) ?

Quoted 8 lines

No. If it's a function that loops, you're inserting too much delay due to loop overhead (unless the compiler unrolls it, but you don't know that) and you may as well use some generic delay function and not bother with the nop register at all. I wanted to use the precise delay observed in the TrueFFS code to (1) avoid too much delay for the sake of performance, (2) in case the timing is critical and too much delay would cause problems, or (3) "nop" really isn't what it says (i.e. no operation). If there were a C preprocessor directive equivalent to the assembly .rept directive, I would use it.

Quoted 7 lines

As you wish, but :
(a) The nops are here to add a minimum delay
(b) The performance would not be an issue, and yes, the compiler could unroll the loop as the number is a compile time constant
(c) nop operation is a delay, that's actually how I understand it
(d) it does work well on docg3
(e) and you can do as you wish, it's your driver :)

Then it's probably just a delay, as advertised. I wanted to be paranoid and duplicate exactly what I was observing during PalmOS operation. In that case, why not dispense with the nop reg altogether and use some short generic delay? Anyway, minor issue.

Quoted 7 lines
Quoted 6 lines

No actually ecc.write_oob - which this function defines - is a nand interface function. I never observed oob-only writes while reverse engineering (read oob-only yes, but not write). Can you write oob-only on the G3? Even if it were possible, the nand_write utility wants to write the oob *before* the page data. This hack allows that utility to work. Maybe the comment should say "oob can't be written before the page data".

Yes, I can write OOB only, page only, or both.

Interesting. We took separate paths in this reverse engineering effort. I observed activity during operation of the native OS using TrueFFS library, and you engaged more in trial-and-error, guided by inspection of disassembled code (if I'm not mistaken). You may have made some more discoveries beyond my observation. I have to inspect your code. I know that reading oob-only was a different "sequence" than a normal page read. Never saw oob-only write.

Quoted 9 lines

And I have the same problem with nandwrite.

Now consider you have 2 nandwrites working in parallel :

   nandwrite1         nandwrite2
   write_oob(OOB1)
                      write_oob(OOB2)
   write_page(page1) ------------------------> OOB1 was overrun
                      write_page(page2)
Quoted 4 lines

That really really bothers me. It's not about your code, I think you have no choice. My concern is rather about nandwrite utility. My point is that it could have better used write_oob(page1, OOB1).
And in that respect, I think there's something broken in it.

Ah, yes. Never considered this. If I understand you correctly, you are pointing out the fact that my hack for deferring oob write until after the page data is written breaks when multiple nandwrite processes are running. I haven't tested with access from multiple processes yet. But yes, the hack assumes only one process is accessing the device.

Another problem with my hack is that the oob-only write is quietly ignored if it is not immediately followed by a write of the data on the same page. One of the mtd tests in the kernel tests oob-only writes, and it fails miserably because they are all ignored. Should probably take this hack out, and fix nand_write, or just use a simplified, fixed version. Yes, mtd_write should not assume oob can be written first. Even if you can write oob-only, you can't subsequently write the page data (with or without writing its ecc bytes), can you?

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

If it's the same as on docg3, each bit is a marker for one block, and the following formula could apply:

          is_good = bbt[block >> 3] & (1 << (block & 0x7));

Thanks. Do any of your devices have bad blocks marked in this table? Do you know how to modify the table?

No, mine G3 has no such blocks.

Then how do you know it's one bit per block?

Quoted 5 lines

And I feel we can't modify them. After all, they are in the OTP area. My guess is that they are set up at chip creation as initial bad blocks (ie. factory bad blocks).
I think there is a difference between bad block (factory bad blocks) and worn out blocks (after erases, having more bitflips that ECC can fix).

You're probably right. I had no ambitions of trying to update the table anyway. Only to read it and update the bbt table in RAM accordingly,

Thanks,
Mike

#5 lore raw
Quoted 7 lines · Mike Dunn

On 11/10/2011 02:06 PM, Robert Jarzmik wrote:
Interesting. We took separate paths in this reverse engineering effort. I observed activity during operation of the native OS using TrueFFS library, and you engaged more in trial-and-error, guided by inspection of disassembled code (if I'm not mistaken). You may have made some more discoveries beyond my observation. I have to inspect your code. I know that reading oob-only was a different "sequence" than a normal page read. Never saw oob-only write.

Ah, that seems different from docg3.
In docg3, look at doc_read_page_prepare(), and pay attention to the "offset" parameter of the function. This one allows you to "seek" in the page directly to the OOB area.

For the write, look at doc_write_seek(), and pay attention to the "ofs" parameter of the function. And the write length (ie. the number of bytes written the IO space) _can_ be 16, 512, or even 528).

Quoted 5 lines

Ah, yes. Never considered this. If I understand you correctly, you are pointing out the fact that my hack for deferring oob write until after the page data is written breaks when multiple nandwrite processes are running. I haven't tested with access from multiple processes yet. But yes, the hack assumes only one process is accessing the device.

Alas, I did exactly as you did, driven by nandwrite :)

Quoted 2 lines

Even if you can write oob-only, you can't subsequently
write the page data (with or without writing its ecc bytes), can you?

I don't know. I tried to write the OOB only, and the page only, or both, but in 1 page_write call. I never tried to do it in 2 page_program cycles ... I'll try to make one write (without the oob, in raw mode), and then one write with the oob to check.

Quoted 2 lines
Quoted 1 line

No, mine G3 has no such blocks.

Then how do you know it's one bit per block?

When I was retro-engineering the SAFTL partition scheme, I found out that when UBoot (proprietary mioa701 bootloader) reads a binary partition (in my case MSIPL), it always loads page4 of bi-block(0,1), and then scans from block (5,6) onward for the first block where :
page[block >> 3] & (1 << (block & 0x7)) == 1
Then, if the first block was 10 for example, the SAFTL header will be looked in blocks (10..2048), and not (5..2048).

Now, in block(5,6) I have the SAFTL header (I know because of the CNAND tag), you can check on my wiki.

So the only logical conclusion for skipping these first blocks is that they actually are bad blocks.

Quoted 2 lines

You're probably right. I had no ambitions of trying to update the table anyway. Only to read it and update the bbt table in RAM accordingly,

Yes, that's a good review comment you could do on docg3 driver which behaves ... poorly in this area :)

Cheers.

Signature · 1 line
Robert