Skip to contentSkip to conversation

RFC on large number of hacks in mtd core files

PATCH Mason 4 participants 12 messages Jan 22, 2016 – Jan 30, 2016

Subsystems (3)
lore
Share & tools
lore

Mason

author
#0 lore raw

Hello everyone,

As I've mentioned in a previous thread, I'm supposed to port a NAND controller driver from 3.4 to 4.4. But this is complicated by the fact that several core files have been modified along the way.

Here's the full picture:

$ git diff --stat v3.4.39 custom-3.4 drivers/mtd
 drivers/mtd/Kconfig                 |   15 +-
 drivers/mtd/chips/cfi_cmdset_0002.c |  129 ++-
 drivers/mtd/devices/Kconfig         |    6 +
 drivers/mtd/devices/Makefile        |    3 +-
 drivers/mtd/devices/m25p80.c        |  100 +-
 drivers/mtd/devices/spiflash.c      | 2131 +++++++++++++++++++++++++++++++++++
 drivers/mtd/maps/Kconfig            |   11 +-
 drivers/mtd/maps/physmap.c          |  202 +++-
 drivers/mtd/mtdchar.c               |   54 +
 drivers/mtd/mtdcore.c               |   63 +-
 drivers/mtd/mtdpart.c               |    9 +
 drivers/mtd/nand/Kconfig            |   46 +-
 drivers/mtd/nand/Makefile           |   11 +-
 drivers/mtd/nand/monza_nand.c       |  614 ++++++++++
 drivers/mtd/nand/nand_base.c        | 1226 +++++++++++++++++++-
 drivers/mtd/nand/nand_bch_ecc.c     |  311 +++++
 drivers/mtd/nand/nand_bch_ecc.h     |   11 +
 drivers/mtd/nand/nand_ids.c         |  253 ++++-
 drivers/mtd/nand/nandsim.c          |    2 +-
 drivers/mtd/nand/smp8xxx_nand.c     | 2009 +++++++++++++++++++++++++++++++++
 drivers/mtd/ubi/misc.c              |    7 +
 21 files changed, 7164 insertions(+), 49 deletions(-)

Here's the actual diff, with non-core changes omitted.
I have also deleted two irrelevant large diff blocks in nand_base.c CONFIG_VENDOR_DTV can be ignored, it's an unrelated project.

AFAICT, most of this code should not be in core files.

drivers/mtd/Kconfig +14 -1
index 27143e042af5..f50826842364 100644
@@ -1,6 +1,7 @@
 menuconfig MTD
 	tristate "Memory Technology Device (MTD) support"
 	depends on GENERIC_IO
+	depends on (!VENDOR_DTV || (VENDOR_DTV_ROM_SLC || VENDOR_DTV_ROM_MLC))
 	help
 	  Memory Technology Devices are flash, RAM and similar chips, often
 	  used for solid state file systems on embedded devices. This option
@@ -22,9 +23,21 @@ config MTD_TESTS
 
 	  WARNING: some of the tests will ERASE entire MTD device which they
 	  test. Do not use these tests unless you really know what you do.
+config MTD_PARTITIONS
+	bool "MTD partitioning support"
+	help
+	 If you have a device which needs to divide its flash chip(s) up
+	 into multiple 'partitions', each of which appears to the user as
+	 a separate MTD device, you require this option to be enabled. If
+	 unsure, say 'Y'.
+
+	 Note, however, that you don't need this option for the DiskOnChip
+	 devices. Partitioning on NFTL 'devices' is a different - that's the
+	 'normal' form of partitioning used on a block device.
 
 config MTD_REDBOOT_PARTS
 	tristate "RedBoot partition table parsing"
+	depends on !TANGOX
 	---help---
 	  RedBoot is a ROM monitor and bootloader which deals with multiple
 	  'images' in flash devices by putting a table one of the erase
@@ -75,7 +88,7 @@ endif # MTD_REDBOOT_PARTS
 
 config MTD_CMDLINE_PARTS
 	bool "Command line partition table parsing"
-	depends on MTD = "y"
+	depends on MTD = "y" && !XENV_PARTITION
 	---help---
 	  Allow generic configuration of the MTD partition tables via the kernel
 	  command line. Multiple flash resources are supported for hardware where
drivers/mtd/chips/cfi_cmdset_0002.c +125 -4

297 lines not on this page · raw message

drivers/mtd/devices/m25p80.c +99 -1

130 lines not on this page · raw message

drivers/mtd/maps/Kconfig +6 -5
index 8af67cfd671a..7354f465b430 100644
@@ -4,6 +4,7 @@ menu "Mapping drivers for chip access"
 
 config MTD_COMPLEX_MAPPINGS
 	bool "Support non-linear mappings of flash chips"
+	depends on MTD && !TANGOX_XENV_READ
 	help
 	  This causes the chip drivers to allow for complicated
 	  paged mappings of flash chips.
@@ -24,7 +25,7 @@ config MTD_PHYSMAP
 
 config MTD_PHYSMAP_COMPAT
 	bool "Physmap compat support"
-	depends on MTD_PHYSMAP
+	depends on MTD_PHYSMAP && !TANGOX_XENV_READ
 	default n
 	help
 	  Setup a simple mapping via the Kconfig options.  Normally the
@@ -35,7 +36,7 @@ config MTD_PHYSMAP_COMPAT
 
 config MTD_PHYSMAP_START
 	hex "Physical start address of flash mapping"
-	depends on MTD_PHYSMAP_COMPAT
+	depends on MTD_PHYSMAP_COMPAT && !TANGOX_XENV_READ
 	default "0x8000000"
 	help
 	  This is the physical memory location at which the flash chips
@@ -45,8 +46,8 @@ config MTD_PHYSMAP_START
 
 config MTD_PHYSMAP_LEN
 	hex "Physical length of flash mapping"
-	depends on MTD_PHYSMAP_COMPAT
-	default "0"
+	depends on MTD_PHYSMAP_COMPAT && !TANGOX_XENV_READ
+	default "0x4000000"
 	help
 	  This is the total length of the mapping of the flash chips on
 	  your particular board. If there is space, or aliases, in the
@@ -57,7 +58,7 @@ config MTD_PHYSMAP_LEN
 
 config MTD_PHYSMAP_BANKWIDTH
 	int "Bank width in octets"
-	depends on MTD_PHYSMAP_COMPAT
+	depends on MTD_PHYSMAP_COMPAT && !TANGOX_XENV_READ
 	default "2"
 	help
 	  This is the total width of the data bus of the flash devices
drivers/mtd/maps/physmap.c +199 -3

256 lines not on this page · raw message

drivers/mtd/mtdchar.c +54 -0

66 lines not on this page · raw message

drivers/mtd/mtdcore.c +61 -2

93 lines not on this page · raw message

drivers/mtd/mtdpart.c +9 -0
index bf24aa77175d..148cfefa045d 100644
@@ -345,6 +345,7 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
 {
 	struct mtd_part *slave;
 	char *name;
+	int i;
 
 	/* allocate the partition structure */
 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
@@ -366,6 +367,11 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
 	slave->mtd.oobsize = master->oobsize;
 	slave->mtd.oobavail = master->oobavail;
 	slave->mtd.subpage_sft = master->subpage_sft;
+	slave->mtd.nand_type = master->nand_type;
+	slave->mtd.nand_manufacturer = master->nand_manufacturer;
+	slave->mtd.onfi_version = master->onfi_version;
+	for (i = 0; i < 8; i++)
+		slave->mtd.id_data[i] = master->id_data[i];
 
 	slave->mtd.name = name;
 	slave->mtd.owner = master->owner;
@@ -646,6 +652,9 @@ int add_mtd_partitions(struct mtd_info *master,
 
 	return 0;
 }
+#ifdef CONFIG_VENDOR_DTV
+EXPORT_SYMBOL(add_mtd_partitions);
+#endif
 
 static DEFINE_SPINLOCK(part_parser_lock);
 static LIST_HEAD(part_parsers);
drivers/mtd/nand/Kconfig +43 -3

66 lines not on this page · raw message

drivers/mtd/nand/Makefile +10 -1
index d4b4d8739bd8..696b8ddfd8f4 100644
@@ -7,12 +7,15 @@ obj-$(CONFIG_MTD_NAND_ECC)		+= nand_ecc.o
 obj-$(CONFIG_MTD_NAND_BCH)		+= nand_bch.o
 obj-$(CONFIG_MTD_NAND_IDS)		+= nand_ids.o
 obj-$(CONFIG_MTD_SM_COMMON) 		+= sm_common.o
-
+ifdef CONFIG_MTD_NAND
+obj-$(CONFIG_ARCH_VENDOR_SX6)		+= monza_nand.o
+endif
 obj-$(CONFIG_MTD_NAND_CAFE)		+= cafe_nand.o
 obj-$(CONFIG_MTD_NAND_SPIA)		+= spia.o
 obj-$(CONFIG_MTD_NAND_AMS_DELTA)	+= ams-delta.o
 obj-$(CONFIG_MTD_NAND_AUTCPU12)		+= autcpu12.o
 obj-$(CONFIG_MTD_NAND_DENALI)		+= denali.o
+obj-$(CONFIG_MTD_NAND_TANGOX)		+= smp8xxx_nand.o
 obj-$(CONFIG_MTD_NAND_AU1550)		+= au1550nd.o
 obj-$(CONFIG_MTD_NAND_BF5XX)		+= bf5xx_nand.o
 obj-$(CONFIG_MTD_NAND_PPCHAMELEONEVB)	+= ppchameleonevb.o
@@ -53,3 +56,9 @@ obj-$(CONFIG_MTD_NAND_JZ4740)		+= jz4740_nand.o
 obj-$(CONFIG_MTD_NAND_GPMI_NAND)	+= gpmi-nand/
 
 nand-objs := nand_base.o nand_bbt.o
+
+ifeq ($(CONFIG_VENDOR_DTV),y)
+ifdef CONFIG_MTD_NAND_MLC_BCH
+nand-objs += nand_bch_ecc.o
+endif
+endif
drivers/mtd/nand/nand_base.c +857 -25

1,300 lines not on this page · raw message

drivers/mtd/nand/nand_ids.c +251 -2

305 lines not on this page · raw message

drivers/mtd/nand/nandsim.c +1 -1
index b9cbd65f49b5..a3b0336dc374 100644
@@ -654,7 +654,7 @@ static int init_nandsim(struct mtd_info *mtd)
 	}
 
 	/* Detect how many ID bytes the NAND chip outputs */
-        for (i = 0; nand_flash_ids[i].name != NULL; i++) {
+        for (i = 0; (nand_flash_ids[i].name != NULL) && (nand_flash_ids[i].id != 0); i++) {
                 if (second_id_byte != nand_flash_ids[i].id)
                         continue;
 		if (!(nand_flash_ids[i].options & NAND_NO_AUTOINCR))
drivers/mtd/ubi/misc.c +7 -0
old mode 100644
new mode 100755
index f6a7d7ac4b98..475fd0063b12
@@ -63,6 +63,13 @@ int ubi_check_volume(struct ubi_device *ubi, int vol_id)
 	void *buf;
 	int err = 0, i;
 	struct ubi_volume *vol = ubi->volumes[vol_id];
+#ifdef CONFIG_VENDOR_DTV
+	/* 
+	   Do not check the volume when the volume is static,in order to reduce the 
+	   time of attach mtd device when the device include big static volume
+	*/
+	return 0;
+#endif
 
 	if (vol->vol_type != UBI_STATIC_VOLUME)
 		return 0;
#1 lore raw
Quoted 23 lines · Mason

Hello everyone,

As I've mentioned in a previous thread, I'm supposed to port a NAND controller driver from 3.4 to 4.4. But this is complicated by the fact that several core files have been modified along the way.

Here's the full picture:

$ git diff --stat v3.4.39 custom-3.4 drivers/mtd
 drivers/mtd/Kconfig                 |   15 +-
 drivers/mtd/chips/cfi_cmdset_0002.c |  129 ++-
 drivers/mtd/devices/Kconfig         |    6 +
 drivers/mtd/devices/Makefile        |    3 +-
 drivers/mtd/devices/m25p80.c        |  100 +-
 drivers/mtd/devices/spiflash.c      | 2131 +++++++++++++++++++++++++++++++++++
 drivers/mtd/maps/Kconfig            |   11 +-
 drivers/mtd/maps/physmap.c          |  202 +++-
 drivers/mtd/mtdchar.c               |   54 +
 drivers/mtd/mtdcore.c               |   63 +-
 drivers/mtd/mtdpart.c               |    9 +
 drivers/mtd/nand/Kconfig            |   46 +-
 drivers/mtd/nand/Makefile           |   11 +-
 drivers/mtd/nand/monza_nand.c       |  614 ++++++++++
Quoted 8 lines
 drivers/mtd/nand/nand_base.c        | 1226 +++++++++++++++++++-
 drivers/mtd/nand/nand_bch_ecc.c     |  311 +++++
 drivers/mtd/nand/nand_bch_ecc.h     |   11 +
 drivers/mtd/nand/nand_ids.c         |  253 ++++-
 drivers/mtd/nand/nandsim.c          |    2 +-
 drivers/mtd/nand/smp8xxx_nand.c     | 2009 +++++++++++++++++++++++++++++++++
 drivers/mtd/ubi/misc.c              |    7 +
 21 files changed, 7164 insertions(+), 49 deletions(-)

^^ This doesn't make sense. You've changed over 7000 lines, but your entire email is less than 3000 lines. From that alone, I can see you haven't given us the whole diff...

And just looking at the diff stat, you might want to clear up a few things:

(1) Are you really just talking about supporting a *NAND* driver? If so, then you probably don't need your new drivers/mtd/devices/spiflash.c. You also probably don't need to touch most things outside of drivers/mtd/nand/, like drivers/mtd/chips/cfi_cmdset_0002.c, drivers/mtd/maps/physmap.c, drivers/mtd/devices/m25p80.c, etc.

(2) How many NAND drivers do you actually have? I see two "new" NAND
drivers: drivers/mtd/nand/smp8xxx_nand.c and drivers/mtd/nand/monza_nand.c.

Quoted 3 lines

Here's the actual diff, with non-core changes omitted.

Ah, I see why the diffstat vs diff discrepancy.

Others may feel more inclined to humor you, but by omitting the drivers themselves, it's hard to really get the whole picture.

[snip the rest]

Brian

author
#2 lore raw

Hello Brian,

Quoted 34 lines · Brian Norris
Quoted 23 lines · Mason

Hello everyone,

As I've mentioned in a previous thread, I'm supposed to port a NAND controller driver from 3.4 to 4.4. But this is complicated by the fact that several core files have been modified along the way.

Here's the full picture:

$ git diff --stat v3.4.39 custom-3.4 drivers/mtd
 drivers/mtd/Kconfig                 |   15 +-
 drivers/mtd/chips/cfi_cmdset_0002.c |  129 ++-
 drivers/mtd/devices/Kconfig         |    6 +
 drivers/mtd/devices/Makefile        |    3 +-
 drivers/mtd/devices/m25p80.c        |  100 +-
 drivers/mtd/devices/spiflash.c      | 2131 +++++++++++++++++++++++++++++++++++
 drivers/mtd/maps/Kconfig            |   11 +-
 drivers/mtd/maps/physmap.c          |  202 +++-
 drivers/mtd/mtdchar.c               |   54 +
 drivers/mtd/mtdcore.c               |   63 +-
 drivers/mtd/mtdpart.c               |    9 +
 drivers/mtd/nand/Kconfig            |   46 +-
 drivers/mtd/nand/Makefile           |   11 +-
 drivers/mtd/nand/monza_nand.c       |  614 ++++++++++
Quoted 8 lines
 drivers/mtd/nand/nand_base.c        | 1226 +++++++++++++++++++-
 drivers/mtd/nand/nand_bch_ecc.c     |  311 +++++
 drivers/mtd/nand/nand_bch_ecc.h     |   11 +
 drivers/mtd/nand/nand_ids.c         |  253 ++++-
 drivers/mtd/nand/nandsim.c          |    2 +-
 drivers/mtd/nand/smp8xxx_nand.c     | 2009 +++++++++++++++++++++++++++++++++
 drivers/mtd/ubi/misc.c              |    7 +
 21 files changed, 7164 insertions(+), 49 deletions(-)
Quoted 3 lines

^^ This doesn't make sense. You've changed over 7000 lines, but your entire email is less than 3000 lines. From that alone, I can see you haven't given us the whole diff...

Sorry for the confusion. My intention was to provide a "complete picture" in the diff stat, then trim "useless" stuff from the actual diff, which is still huge :-(

Quoted 8 lines

And just looking at the diff stat, you might want to clear up a few things:

(1) Are you really just talking about supporting a *NAND* driver? If so, then you probably don't need your new drivers/mtd/devices/spiflash.c. You also probably don't need to touch most things outside of drivers/mtd/nand/, like drivers/mtd/chips/cfi_cmdset_0002.c, drivers/mtd/maps/physmap.c, drivers/mtd/devices/m25p80.c, etc.

You're right, I should have been clearer.

I am trying to port the smp8xxx_nand.c driver to 4.4 in such a way that porting to future kernels won't be such a pain.

This means examining changes to "core" files and either
- deleting unnecessary changes
- merging them back into smp8xxx_nand.c
- submitting them upstream

You seem to be saying that none of the changes outside of drivers/mtd/nand are required for a NAND controller driver? That would be great news for me.

Bear in mind that the essence of the driver is "quick and dirty". So it wouldn't surprise me to find required code outside of drivers/mtd/nand :-(

I see that m25p80.c is in mtd/devices.
This has nothing to do with the NAND controller drivers?

I thought it was a type of NAND chip, and any NAND controller might be connected to one such chip.

Quoted 3 lines

(2) How many NAND drivers do you actually have? I see two "new" NAND
drivers: drivers/mtd/nand/smp8xxx_nand.c and drivers/mtd/nand/monza_nand.c.

Please ignore:
monza_nand.c nand_bch_ecc.* spiflash.c

Quoted 6 lines
Quoted 1 line

Here's the actual diff, with non-core changes omitted.

Ah, I see why the diffstat vs diff discrepancy.

Others may feel more inclined to humor you, but by omitting the drivers themselves, it's hard to really get the whole picture.

I understand.

I'll spend a few hours to clean up the diffs and keep only what I think is relevant. (Risk being that I delete the wrong stuff, since I can't test the driver.)

Regards.

author
#3 lore raw
Quoted 1 line · Mason

I'll spend a few hours to clean up the diffs and keep only what seems relevant.

OK, here's my second attempt at presenting the information. I'm grateful if anyone has comments regarding the changes in core files, or even the driver itself.

Regards.

$ git diff --stat -p v3.4.39 HEAD drivers/mtd include/linux/mtd
 drivers/mtd/Kconfig                 |   14 +-
 drivers/mtd/chips/cfi_cmdset_0002.c |  127 ++-
 drivers/mtd/devices/m25p80.c        |  100 +-
 drivers/mtd/maps/Kconfig            |   11 +-
 drivers/mtd/maps/physmap.c          |  193 +++-
 drivers/mtd/mtdchar.c               |   54 +
 drivers/mtd/mtdcore.c               |   48 +-
 drivers/mtd/mtdpart.c               |    6 +
 drivers/mtd/nand/Kconfig            |   18 +-
 drivers/mtd/nand/Makefile           |    1 +
 drivers/mtd/nand/nand_base.c        |  230 +++-
 drivers/mtd/nand/nand_ids.c         |   24 +-
 drivers/mtd/nand/nandsim.c          |    2 +-
 drivers/mtd/nand/smp8xxx_nand.c     | 1974 +++++++++++++++++++++++++++++++++++
 include/linux/mtd/map.h             |    1 +
 include/linux/mtd/mtd.h             |    6 +
 include/linux/mtd/nand.h            |   10 +
 17 files changed, 2776 insertions(+), 43 deletions(-)
drivers/mtd/Kconfig +13 -1
index 27143e042af5..a2661a490f3c 100644
@@ -22,9 +22,21 @@ config MTD_TESTS
 
 	  WARNING: some of the tests will ERASE entire MTD device which they
 	  test. Do not use these tests unless you really know what you do.
+config MTD_PARTITIONS
+	bool "MTD partitioning support"
+	help
+	 If you have a device which needs to divide its flash chip(s) up
+	 into multiple 'partitions', each of which appears to the user as
+	 a separate MTD device, you require this option to be enabled. If
+	 unsure, say 'Y'.
+
+	 Note, however, that you don't need this option for the DiskOnChip
+	 devices. Partitioning on NFTL 'devices' is a different - that's the
+	 'normal' form of partitioning used on a block device.
 
 config MTD_REDBOOT_PARTS
 	tristate "RedBoot partition table parsing"
+	depends on !TANGOX
 	---help---
 	  RedBoot is a ROM monitor and bootloader which deals with multiple
 	  'images' in flash devices by putting a table one of the erase
@@ -75,7 +87,7 @@ endif # MTD_REDBOOT_PARTS
 
 config MTD_CMDLINE_PARTS
 	bool "Command line partition table parsing"
-	depends on MTD = "y"
+	depends on MTD = "y" && !XENV_PARTITION
 	---help---
 	  Allow generic configuration of the MTD partition tables via the kernel
 	  command line. Multiple flash resources are supported for hardware where
drivers/mtd/chips/cfi_cmdset_0002.c +124 -3

290 lines not on this page · raw message

drivers/mtd/devices/m25p80.c +99 -1

130 lines not on this page · raw message

drivers/mtd/maps/Kconfig +6 -5
index 8af67cfd671a..7354f465b430 100644
@@ -4,6 +4,7 @@ menu "Mapping drivers for chip access"
 
 config MTD_COMPLEX_MAPPINGS
 	bool "Support non-linear mappings of flash chips"
+	depends on MTD && !TANGOX_XENV_READ
 	help
 	  This causes the chip drivers to allow for complicated
 	  paged mappings of flash chips.
@@ -24,7 +25,7 @@ config MTD_PHYSMAP
 
 config MTD_PHYSMAP_COMPAT
 	bool "Physmap compat support"
-	depends on MTD_PHYSMAP
+	depends on MTD_PHYSMAP && !TANGOX_XENV_READ
 	default n
 	help
 	  Setup a simple mapping via the Kconfig options.  Normally the
@@ -35,7 +36,7 @@ config MTD_PHYSMAP_COMPAT
 
 config MTD_PHYSMAP_START
 	hex "Physical start address of flash mapping"
-	depends on MTD_PHYSMAP_COMPAT
+	depends on MTD_PHYSMAP_COMPAT && !TANGOX_XENV_READ
 	default "0x8000000"
 	help
 	  This is the physical memory location at which the flash chips
@@ -45,8 +46,8 @@ config MTD_PHYSMAP_START
 
 config MTD_PHYSMAP_LEN
 	hex "Physical length of flash mapping"
-	depends on MTD_PHYSMAP_COMPAT
-	default "0"
+	depends on MTD_PHYSMAP_COMPAT && !TANGOX_XENV_READ
+	default "0x4000000"
 	help
 	  This is the total length of the mapping of the flash chips on
 	  your particular board. If there is space, or aliases, in the
@@ -57,7 +58,7 @@ config MTD_PHYSMAP_LEN
 
 config MTD_PHYSMAP_BANKWIDTH
 	int "Bank width in octets"
-	depends on MTD_PHYSMAP_COMPAT
+	depends on MTD_PHYSMAP_COMPAT && !TANGOX_XENV_READ
 	default "2"
 	help
 	  This is the total width of the data bus of the flash devices
drivers/mtd/maps/physmap.c +190 -3

247 lines not on this page · raw message

drivers/mtd/mtdchar.c +54 -0

66 lines not on this page · raw message

drivers/mtd/mtdcore.c +47 -1

66 lines not on this page · raw message

drivers/mtd/mtdpart.c +6 -0
index bf24aa77175d..2e8ba4091dbb 100644
@@ -345,6 +345,7 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
 {
 	struct mtd_part *slave;
 	char *name;
+	int i;
 
 	/* allocate the partition structure */
 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
@@ -366,6 +367,11 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
 	slave->mtd.oobsize = master->oobsize;
 	slave->mtd.oobavail = master->oobavail;
 	slave->mtd.subpage_sft = master->subpage_sft;
+	slave->mtd.nand_type = master->nand_type;
+	slave->mtd.nand_manufacturer = master->nand_manufacturer;
+	slave->mtd.onfi_version = master->onfi_version;
+	for (i = 0; i < 8; i++)
+		slave->mtd.id_data[i] = master->id_data[i];
 
 	slave->mtd.name = name;
 	slave->mtd.owner = master->owner;
drivers/mtd/nand/Kconfig +15 -3
index 7d17cecad69d..e27256885ec1 100644
@@ -1,3 +1,10 @@
+config MTD_NAND_IDS
+	tristate "Include chip ids for known NAND devices."
+	depends on MTD
+	help
+	  Useful for NAND drivers that do not use the NAND subsystem but
+	  still like to take advantage of the known chip information.
+
 config MTD_NAND_ECC
 	tristate
 
@@ -83,6 +90,14 @@ config MTD_NAND_DENALI_SCRATCH_REG_ADDR
           scratch register here to enable this feature. On Intel Moorestown
           boards, the scratch register is at 0xFF108018.
 
+config MTD_NAND_TANGOX
+        tristate "TANGOX NAND Device Support"
+        depends on TANGO3 || TANGO4
+        select MTD_PARTITIONS
+        default m
+        help
+          Support TANGOX NAND Flash in the NAND flash reserved zone.
+
 config MTD_NAND_H1900
 	tristate "iPAQ H1900 flash"
 	depends on ARCH_PXA && BROKEN
@@ -115,9 +130,6 @@ config MTD_NAND_OMAP2
           Support for NAND flash on Texas Instruments OMAP2, OMAP3 and OMAP4
 	  platforms.
 
-config MTD_NAND_IDS
-	tristate
-
 config MTD_NAND_RICOH
 	tristate "Ricoh xD card reader"
 	default n
drivers/mtd/nand/Makefile +1 -0
index d4b4d8739bd8..1a2ce2cca9f6 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_MTD_NAND_SPIA)		+= spia.o
 obj-$(CONFIG_MTD_NAND_AMS_DELTA)	+= ams-delta.o
 obj-$(CONFIG_MTD_NAND_AUTCPU12)		+= autcpu12.o
 obj-$(CONFIG_MTD_NAND_DENALI)		+= denali.o
+obj-$(CONFIG_MTD_NAND_TANGOX)		+= smp8xxx_nand.o
 obj-$(CONFIG_MTD_NAND_AU1550)		+= au1550nd.o
 obj-$(CONFIG_MTD_NAND_BF5XX)		+= bf5xx_nand.o
 obj-$(CONFIG_MTD_NAND_PPCHAMELEONEVB)	+= ppchameleonevb.o
drivers/mtd/nand/nand_base.c +206 -24

477 lines not on this page · raw message

drivers/mtd/nand/nand_ids.c +23 -1
index af4fe8ca7b5e..2f14ff5efb4c 100644
@@ -107,22 +107,26 @@ struct nand_flash_dev nand_flash_ids[] = {
 	/* 8 Gigabit */
 	{"NAND 1GiB 1,8V 8-bit",	0xA3, 0, 1024, 0, LP_OPTIONS},
 	{"NAND 1GiB 3,3V 8-bit",	0xD3, 0, 1024, 0, LP_OPTIONS},
+	{"NAND 1GiB 3,3V 8-bit",	0x38, 0, 1024, 0, LP_OPTIONS},
 	{"NAND 1GiB 1,8V 16-bit",	0xB3, 0, 1024, 0, LP_OPTIONS16},
 	{"NAND 1GiB 3,3V 16-bit",	0xC3, 0, 1024, 0, LP_OPTIONS16},
 
 	/* 16 Gigabit */
 	{"NAND 2GiB 1,8V 8-bit",	0xA5, 0, 2048, 0, LP_OPTIONS},
 	{"NAND 2GiB 3,3V 8-bit",	0xD5, 0, 2048, 0, LP_OPTIONS},
+	{"NAND 2GiB 3,3V 8-bit",	0x48, 0, 2048, 0, LP_OPTIONS},
 	{"NAND 2GiB 1,8V 16-bit",	0xB5, 0, 2048, 0, LP_OPTIONS16},
 	{"NAND 2GiB 3,3V 16-bit",	0xC5, 0, 2048, 0, LP_OPTIONS16},
 
 	/* 32 Gigabit */
+	{"NAND 4GiB 3,3V 8-bit",	0x68, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 1,8V 8-bit",	0xA7, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 3,3V 8-bit",	0xD7, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 1,8V 16-bit",	0xB7, 0, 4096, 0, LP_OPTIONS16},
 	{"NAND 4GiB 3,3V 16-bit",	0xC7, 0, 4096, 0, LP_OPTIONS16},
 
 	/* 64 Gigabit */
+	{"NAND 8GiB 3,3V 8-bit",	0x88, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 1,8V 8-bit",	0xAE, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 3,3V 8-bit",	0xDE, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 1,8V 16-bit",	0xBE, 0, 8192, 0, LP_OPTIONS16},
@@ -135,6 +139,7 @@ struct nand_flash_dev nand_flash_ids[] = {
 	{"NAND 16GiB 3,3V 16-bit",	0x4A, 0, 16384, 0, LP_OPTIONS16},
 
 	/* 256 Gigabit */
+	{"NAND 32GiB 3,3V 8-bit",	0xA8, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 1,8V 8-bit",	0x1C, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 3,3V 8-bit",	0x3C, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 1,8V 16-bit",	0x2C, 0, 32768, 0, LP_OPTIONS16},
@@ -161,7 +166,22 @@ struct nand_flash_dev nand_flash_ids[] = {
 	 BBT_AUTO_REFRESH
 	},
 
-	{NULL,}
+	/*
+	 * Fill-in gaps, may be refilled at the runtime
+	 */
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+
+	/*
+	 * Terminates the table
+	 */
+	{NULL, },
 };
 
 /*
@@ -178,6 +198,8 @@ struct nand_manufacturers nand_manuf_ids[] = {
 	{NAND_MFR_MICRON, "Micron"},
 	{NAND_MFR_AMD, "AMD"},
 	{NAND_MFR_MACRONIX, "Macronix"},
+	{NAND_MFR_ESMT, "ESMT"},
+	{NAND_MFR_MIRA, "MIRA"},
 	{0x0, "Unknown"}
 };
 
drivers/mtd/nand/nandsim.c +1 -1
index b9cbd65f49b5..a3b0336dc374 100644
@@ -654,7 +654,7 @@ static int init_nandsim(struct mtd_info *mtd)
 	}
 
 	/* Detect how many ID bytes the NAND chip outputs */
-        for (i = 0; nand_flash_ids[i].name != NULL; i++) {
+        for (i = 0; (nand_flash_ids[i].name != NULL) && (nand_flash_ids[i].id != 0); i++) {
                 if (second_id_byte != nand_flash_ids[i].id)
                         continue;
 		if (!(nand_flash_ids[i].options & NAND_NO_AUTOINCR))
drivers/mtd/nand/smp8xxx_nand.c new file +1974 -0

1,524 lines not on this page · raw message

include/linux/mtd/map.h +1 -0
index 3595a0236b0f..cc3fc12a2fc3 100644

Lines of this file are not shown. View the raw message.

include/linux/mtd/mtd.h +6 -0
index cf5ea8cdcf8e..90b5caf8cacd 100644

Lines of this file are not shown. View the raw message.

include/linux/mtd/nand.h +10 -0
index 248351344718..f58f617ea562 100644

Lines of this file are not shown. View the raw message.

The rest of this diff is not shown. View the raw message.

Part of the diff is not shown here. View the raw message.

#4 lore raw
Quoted 5 lines · Mason
Quoted 1 line

I'll spend a few hours to clean up the diffs and keep only what seems relevant.

OK, here's my second attempt at presenting the information. I'm grateful if anyone has comments regarding the changes in core files, or even the driver itself.

One general comment: all these #ifdef CONFIG_TANGOX / #else / #endif constructs do not cope well with multi-platform kernels. The same is true for the CONFIG_MTD_NAND_BBM.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author
#5 lore raw
Quoted 9 lines · Geert Uytterhoeven
Quoted 3 lines · Mason

OK, here's my second attempt at presenting the information. I'm grateful if anyone has comments regarding the changes in core files, or even the driver itself.

One general comment: all these #ifdef CONFIG_TANGOX / #else / #endif constructs do not cope well with multi-platform kernels. The same is true for the CONFIG_MTD_NAND_BBM.

First of all, thanks for taking a look at the awful patch I sent. I just want to stress that I have no intention of submitting anything like this.

Perhaps it would help if I asked specific questions.

About devices/m25p80.c : the header mentions
"MTD SPI driver for ST M25Pxx (and similar) serial flash chips"

I don't know anything about SPI. Is SPI the bus connecting Flash chips to the Flash controller?

If Brian's comment that I can ignore changes outside mtd/nand is correct, then I should only need to focus on mtd/nand/nand_base.c and mtd/nand/nand_ids.c

What about those NAND_MFR defines?

+#define NAND_MFR_ESMT 0x92
+#define NAND_MFR_MIRA 0xc8

I see that include/linux/mtd/nand.h has

#define NAND_MFR_EON 0x92

ESMT apparently stands for
Elite Semiconductor Memory Technology Inc
Apparently ESMT bought Eon a few months ago.
http://technews.co/2015/11/11/taiwans-elite-semiconductor-to-absorb-rival-eon-silicon/

So I guess it's expected that these two manufacturers share the same ID?

As for the MIRA ID... Looks like this patch is relevant:
http://lists.infradead.org/pipermail/linux-mtd/2014-December/056765.html
+#define NAND_MFR_GIGADEVICE 0xc8

BTW, did you guys see the awful bug in the nand_ids.c part? Several whitespace-only strings were added, with the intent of writing them at run-time! But I'll bet the compiler uses the same array for all of them.

Regards.

#6 lore raw
Quoted 11 lines · Mason

I see that include/linux/mtd/nand.h has

#define NAND_MFR_EON 0x92

ESMT apparently stands for
Elite Semiconductor Memory Technology Inc Apparently ESMT bought Eon a few months ago.
http://technews.co/2015/11/11/taiwans-elite-semiconductor-to-absorb-rival-eon-silicon/

So I guess it's expected that these two manufacturers share the same ID?

Yes. You can add a comment to the #define to explain the link to ESMT.

Quoted 3 lines

As for the MIRA ID... Looks like this patch is relevant:
http://lists.infradead.org/pipermail/linux-mtd/2014-December/056765.html
+#define NAND_MFR_GIGADEVICE 0xc8

Probably.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#7 lore raw
In reply to Mason

Hi Mason,

Just ignored all the changes that were not related to NAND or mtdcore (i.e. drivers/mtd/{chips, maps, device})

Quoted 41 lines · On Mon, 25 Jan 2016 18:34:50 +0100 Mason <[email protected]> wrote:
Quoted 1 line · Mason

I'll spend a few hours to clean up the diffs and keep only what seems relevant.

OK, here's my second attempt at presenting the information. I'm grateful if anyone has comments regarding the changes in core files, or even the driver itself.

Regards.

$ git diff --stat -p v3.4.39 HEAD drivers/mtd include/linux/mtd
 drivers/mtd/Kconfig                 |   14 +-
 drivers/mtd/chips/cfi_cmdset_0002.c |  127 ++-
 drivers/mtd/devices/m25p80.c        |  100 +-
 drivers/mtd/maps/Kconfig            |   11 +-
 drivers/mtd/maps/physmap.c          |  193 +++-
 drivers/mtd/mtdchar.c               |   54 +
 drivers/mtd/mtdcore.c               |   48 +-
 drivers/mtd/mtdpart.c               |    6 +
 drivers/mtd/nand/Kconfig            |   18 +-
 drivers/mtd/nand/Makefile           |    1 +
 drivers/mtd/nand/nand_base.c        |  230 +++-
 drivers/mtd/nand/nand_ids.c         |   24 +-
 drivers/mtd/nand/nandsim.c          |    2 +-
 drivers/mtd/nand/smp8xxx_nand.c     | 1974 +++++++++++++++++++++++++++++++++++
 include/linux/mtd/map.h             |    1 +
 include/linux/mtd/mtd.h             |    6 +
 include/linux/mtd/nand.h            |   10 +
 17 files changed, 2776 insertions(+), 43 deletions(-)
drivers/mtd/Kconfig +1 -0
index 27143e042af5..a2661a490f3c 100644
@@ -22,9 +22,21 @@ config MTD_TESTS
 
 	  WARNING: some of the tests will ERASE entire MTD device which they
 	  test. Do not use these tests unless you really know what you do.
+config MTD_PARTITIONS

drivers/mtd/Kconfig config MTD_TESTS · line 35

Quoted 10 lines
drivers/mtd/Kconfig +10 -0
+	bool "MTD partitioning support"
+	help
+	 If you have a device which needs to divide its flash chip(s) up
+	 into multiple 'partitions', each of which appears to the user as
+	 a separate MTD device, you require this option to be enabled. If
+	 unsure, say 'Y'.
+
+	 Note, however, that you don't need this option for the DiskOnChip
+	 devices. Partitioning on NFTL 'devices' is a different - that's the
+	 'normal' form of partitioning used on a block device.

This Kconfig option is already available in mainline.

drivers/mtd/Kconfig config MTD_TESTS · line 39

Quoted 4 lines
drivers/mtd/Kconfig +1 -0
 config MTD_REDBOOT_PARTS
 	tristate "RedBoot partition table parsing"
+	depends on !TANGOX

Why that? If you don't want REBOOT support just don't enable it in your config.

drivers/mtd/Kconfig endif # MTD_REDBOOT_PARTS · line 90

Quoted 9 lines
drivers/mtd/Kconfig +1 -1
 	---help---
 	  RedBoot is a ROM monitor and bootloader which deals with multiple
 	  'images' in flash devices by putting a table one of the erase
@@ -75,7 +87,7 @@ endif # MTD_REDBOOT_PARTS
 
 config MTD_CMDLINE_PARTS
 	bool "Command line partition table parsing"
-	depends on MTD = "y"
+	depends on MTD = "y" && !XENV_PARTITION

I don't see any definition of XENV_PARTITIONS in you diff, and even if there was one, why would it be incompatible with partitions defined in the kernel cmdline?

Quoted 3 lines
drivers/mtd/Kconfig +0 -0
 	---help---
 	  Allow generic configuration of the MTD partition tables via the kernel
 	  command line. Multiple flash resources are supported for hardware where

[...]

Quoted 59 lines
drivers/mtd/mtdchar.c +44 -0
index f2f482bec573..8219c1ce0f6b 100644
@@ -620,6 +620,8 @@ static int mtdchar_write_ioctl(struct mtd_info *mtd,
 	return ret;
 }
 
+#define MEMERASEFORCE  _IOW('M', 20, struct erase_info_user)
+
 static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 {
 	struct mtd_file_info *mfi = file->private_data;
@@ -751,6 +753,58 @@ static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 		break;
 	}
 
+	case MEMERASEFORCE:
+	{
+		struct erase_info *erase;
+
+		if(!(file->f_mode & 2))
+			return -EPERM;
+
+		erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
+		if (!erase)
+			ret = -ENOMEM;
+		else {
+			wait_queue_head_t waitq;
+			DECLARE_WAITQUEUE(wait, current);
+
+			init_waitqueue_head(&waitq);
+
+			if (copy_from_user(&erase->addr, argp,
+				    sizeof(struct erase_info_user))) {
+				kfree(erase);
+				return -EFAULT;
+			}
+			erase->mtd = mtd;
+			erase->callback = mtdchar_erase_callback;
+			erase->priv = (unsigned long)&waitq;
+			erase->retries = 0x73092215;
+
+			/*
+			  FIXME: Allow INTERRUPTIBLE. Which means
+			  not having the wait_queue head on the stack.
+
+			  If the wq_head is on the stack, and we
+			  leave because we got interrupted, then the
+			  wq_head is no longer there when the
+			  callback routine tries to wake us up.
+			*/
+			ret = mtd_erase(mtd, erase);
+			if (!ret) {
+				set_current_state(TASK_UNINTERRUPTIBLE);
+				add_wait_queue(&waitq, &wait);
+				if (erase->state != MTD_ERASE_DONE &&
+				    erase->state != MTD_ERASE_FAILED)
+					schedule();

drivers/mtd/mtdchar.c static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg) · line 807

Quoted 10 lines
drivers/mtd/mtdchar.c +10 -0
+				remove_wait_queue(&waitq, &wait);
+				set_current_state(TASK_RUNNING);
+
+				ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
+			}
+			kfree(erase);
+		}
+		break;
+	}
+

Support for bad block erasure has been discussed several times, AFAIR no decision was taken (or maybe it was decided that it was safer to not allow it).

The problem here is that BBM (bad block markers) which are placed by the NAND vendor can be erased too, and if you loose this information you can start reusing a block that was really bad. ITOH, because some NAND controller have weird page layout in which BBM are replaced by in-band data or ECC bytes. So sometime it's useful to be able to force erasure of bad blocks.
Anyway, until we agree on something I suggest you drop this feature and use the nand scrub command in u-boot (if you are using u-boot ;)), which is doing exactly what you're trying to do.

Quoted 58 lines
drivers/mtd/mtdchar.c +0 -0
 	case MEMWRITEOOB:
 	{
 		struct mtd_oob_buf buf;
drivers/mtd/mtdcore.c +43 -0
index c837507dfb1c..83085b6169d6 100644
@@ -250,6 +250,49 @@ static ssize_t mtd_name_show(struct device *dev,
 }
 static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
 
+static ssize_t mtd_nand_type_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE, "%s\n", mtd->nand_type);
+}
+static DEVICE_ATTR(nand_type, S_IRUGO, mtd_nand_type_show, NULL);
+
+static ssize_t mtd_nand_manufacturer_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE, "%s\n", mtd->nand_manufacturer);
+}
+static DEVICE_ATTR(nand_manufacturer, S_IRUGO, mtd_nand_manufacturer_show, NULL);
+
+static ssize_t mtd_nand_onfi_version_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE, "%s\n", mtd->onfi_version);
+}
+static DEVICE_ATTR(onfi_version, S_IRUGO, mtd_nand_onfi_version_show, NULL);
+
+static ssize_t mtd_nand_id_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct mtd_info *mtd = dev_get_drvdata(dev);
+
+	return snprintf(buf, PAGE_SIZE, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n", mtd->id_data[0]
+																			   , mtd->id_data[1]
+																			   , mtd->id_data[2]
+																			   , mtd->id_data[3]
+																			   , mtd->id_data[4]
+																			   , mtd->id_data[5]
+																			   , mtd->id_data[6]
+																			   , mtd->id_data[7]);
+}
+static DEVICE_ATTR(nand_id, S_IRUGO, mtd_nand_id_show, NULL);
+
 static struct attribute *mtd_attrs[] = {
 	&dev_attr_type.attr,
 	&dev_attr_flags.attr,

drivers/mtd/mtdcore.c static struct attribute *mtd_attrs[] = { · line 311

Quoted 9 lines
drivers/mtd/mtdcore.c +4 -0
@@ -260,6 +303,10 @@ static struct attribute *mtd_attrs[] = {
 	&dev_attr_oobsize.attr,
 	&dev_attr_numeraseregions.attr,
 	&dev_attr_name.attr,
+	&dev_attr_nand_type.attr,
+	&dev_attr_nand_manufacturer.attr,
+	&dev_attr_nand_id.attr,
+	&dev_attr_onfi_version.attr,
 	NULL,
 };

Hm, you're adding NAND specific files in the middle of generic mtd sysfs files. I'm not saying that those information are not interesting, but maybe they could be exposed in debugfs, and they should definitely be exposed by nand_base.c not mtdcore.c.

Anyway, those changes are definitely not required to support your NAND controller, so you can simply drop them.

drivers/mtd/mtdcore.c int add_mtd_device(struct mtd_info *mtd) · line 324

Quoted 6 lines
drivers/mtd/mtdcore.c +0 -1
@@ -321,7 +368,6 @@ int add_mtd_device(struct mtd_info *mtd)
 
 	mtd->index = i;
 	mtd->usecount = 0;
-

Useless blank line removal.

Quoted 16 lines
drivers/mtd/mtdcore.c +0 -0
 	if (is_power_of_2(mtd->erasesize))
 		mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
 	else
drivers/mtd/mtdpart.c +1 -0
index bf24aa77175d..2e8ba4091dbb 100644
@@ -345,6 +345,7 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
 {
 	struct mtd_part *slave;
 	char *name;
+	int i;
 
 	/* allocate the partition structure */
 	slave = kzalloc(sizeof(*slave), GFP_KERNEL);

drivers/mtd/mtdpart.c static struct mtd_part *allocate_partition(struct mtd_info *master, · line 374

Quoted 8 lines
drivers/mtd/mtdpart.c +5 -0
@@ -366,6 +367,11 @@ static struct mtd_part *allocate_partition(struct mtd_info *master,
 	slave->mtd.oobsize = master->oobsize;
 	slave->mtd.oobavail = master->oobavail;
 	slave->mtd.subpage_sft = master->subpage_sft;
+	slave->mtd.nand_type = master->nand_type;
+	slave->mtd.nand_manufacturer = master->nand_manufacturer;
+	slave->mtd.onfi_version = master->onfi_version;
+	for (i = 0; i < 8; i++)
+		slave->mtd.id_data[i] = master->id_data[i];

Those changes can be dropped too if you drop the sysfs entries.

Quoted 8 lines
drivers/mtd/mtdpart.c +0 -0
 	slave->mtd.name = name;
 	slave->mtd.owner = master->owner;
drivers/mtd/nand/Kconfig +0 -0
index 7d17cecad69d..e27256885ec1 100644

drivers/mtd/nand/Kconfig line 7

Quoted 7 lines
drivers/mtd/nand/Kconfig +7 -0
@@ -1,3 +1,10 @@
+config MTD_NAND_IDS
+	tristate "Include chip ids for known NAND devices."
+	depends on MTD
+	help
+	  Useful for NAND drivers that do not use the NAND subsystem but
+	  still like to take advantage of the known chip information.
+

This option already exists and is selected by MTD_NAND, no need to add a description for it (we don't want users to see this option). So again, this can be dropped.

drivers/mtd/nand/Kconfig config MTD_NAND_DENALI_SCRATCH_REG_ADDR · line 95

Quoted 10 lines
drivers/mtd/nand/Kconfig +3 -0
 config MTD_NAND_ECC
 	tristate
 
@@ -83,6 +90,14 @@ config MTD_NAND_DENALI_SCRATCH_REG_ADDR
           scratch register here to enable this feature. On Intel Moorestown
           boards, the scratch register is at 0xFF108018.
 
+config MTD_NAND_TANGOX
+        tristate "TANGOX NAND Device Support"
+        depends on TANGO3 || TANGO4

Maybe you can add COMPILE_TEST too.

drivers/mtd/nand/Kconfig config MTD_NAND_DENALI_SCRATCH_REG_ADDR · line 96

Quoted 1 line
drivers/mtd/nand/Kconfig +1 -0
+        select MTD_PARTITIONS

Let the user choose whether it wants MTD_PARTITIONS support or not.

Quoted 8 lines
drivers/mtd/nand/Kconfig +4 -0
+        default m
+        help
+          Support TANGOX NAND Flash in the NAND flash reserved zone.
+
 config MTD_NAND_H1900
 	tristate "iPAQ H1900 flash"
 	depends on ARCH_PXA && BROKEN

drivers/mtd/nand/Kconfig config MTD_NAND_OMAP2 · line 120

Quoted 6 lines
drivers/mtd/nand/Kconfig +0 -3
@@ -115,9 +130,6 @@ config MTD_NAND_OMAP2
           Support for NAND flash on Texas Instruments OMAP2, OMAP3 and OMAP4
 	  platforms.
 
-config MTD_NAND_IDS
-	tristate
-

Should be dropped too.

Quoted 20 lines
drivers/mtd/nand/Kconfig +0 -0
 config MTD_NAND_RICOH
 	tristate "Ricoh xD card reader"
 	default n
drivers/mtd/nand/Makefile +1 -0
index d4b4d8739bd8..1a2ce2cca9f6 100644
@@ -13,6 +13,7 @@ obj-$(CONFIG_MTD_NAND_SPIA)		+= spia.o
 obj-$(CONFIG_MTD_NAND_AMS_DELTA)	+= ams-delta.o
 obj-$(CONFIG_MTD_NAND_AUTCPU12)		+= autcpu12.o
 obj-$(CONFIG_MTD_NAND_DENALI)		+= denali.o
+obj-$(CONFIG_MTD_NAND_TANGOX)		+= smp8xxx_nand.o
 obj-$(CONFIG_MTD_NAND_AU1550)		+= au1550nd.o
 obj-$(CONFIG_MTD_NAND_BF5XX)		+= bf5xx_nand.o
 obj-$(CONFIG_MTD_NAND_PPCHAMELEONEVB)	+= ppchameleonevb.o
drivers/mtd/nand/nand_base.c +0 -0
index eb9f5fb02eef..7a98ccef937c 100644

drivers/mtd/nand/nand_base.c line 53

Quoted 7 lines
drivers/mtd/nand/nand_base.c +3 -0
@@ -47,7 +47,10 @@
 #include <linux/bitops.h>
 #include <linux/leds.h>
 #include <linux/io.h>
+
+#ifdef CONFIG_MTD_PARTITIONS
 #include <linux/mtd/partitions.h>
+#endif

You don't need to make this inclusion conditional.

Quoted 5 lines
drivers/mtd/nand/nand_base.c +0 -0
 /* Define default oob placement schemes for large and small page devices */
 static struct nand_ecclayout nand_oob_8 = {
@@ -64,8 +67,11 @@ static struct nand_ecclayout nand_oob_16 = {
 	.eccbytes = 6,

drivers/mtd/nand/nand_base.c static struct nand_ecclayout nand_oob_16 = { · line 75

Quoted 10 lines
drivers/mtd/nand/nand_base.c +5 -2
 	.eccpos = {0, 1, 2, 3, 6, 7},
 	.oobfree = {
-		{.offset = 8,
-		 . length = 8} }
+#ifdef CONFIG_MTD_NAND_BBM
+		{.offset = 9, . length = 7}}		//nand bbm config
+#else
+		{.offset = 8, . length = 8}}
+#endif
 };

If you need to define your own ooblayout then it should be done at the NAND controller, and you should not make it conditional on some config option.

drivers/mtd/nand/nand_base.c static struct nand_ecclayout nand_oob_64 = { · line 88

Quoted 13 lines
drivers/mtd/nand/nand_base.c +5 -2
 static struct nand_ecclayout nand_oob_64 = {
@@ -75,8 +81,11 @@ static struct nand_ecclayout nand_oob_64 = {
 		   48, 49, 50, 51, 52, 53, 54, 55,
 		   56, 57, 58, 59, 60, 61, 62, 63},
 	.oobfree = {
-		{.offset = 2,
-		 .length = 38} }
+#ifdef CONFIG_MTD_NAND_BBM
+		{.offset = 3,.length = 37}}     //nand bbm config
+#else
+		{.offset = 2,.length = 38}}
+#endif

Ditto.

drivers/mtd/nand/nand_base.c static int check_offs_len(struct mtd_info *mtd, · line 140

Quoted 13 lines
drivers/mtd/nand/nand_base.c +6 -0
 };
 
 static struct nand_ecclayout nand_oob_128 = {
@@ -123,6 +132,12 @@ static int check_offs_len(struct mtd_info *mtd,
 		ret = -EINVAL;
 	}
 
+	/* Do not allow past end of device */
+	if (ofs + len > mtd->size) {
+		pr_debug( "%s: Past end of device\n",__func__);
+		ret = -EINVAL;
+	}
+

This is already checked at the MTD level, so I don't think you need that.

drivers/mtd/nand/nand_base.c static void nand_command(struct mtd_info *mtd, unsigned int command, · line 668

Quoted 12 lines
drivers/mtd/nand/nand_base.c +4 -0
 	return ret;
 }
 
@@ -646,7 +661,11 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
 	 * Apply this short delay always to ensure that we do wait tWB in
 	 * any case on any machine.
 	 */
+#ifdef CONFIG_TANGOX
+	udelay(1); /* needs to make it much longer than tWB */
+#else
 	ndelay(100);
+#endif

Yes, this delay here should probably depends on the NAND timings attached to the NAND device, but anyway, I don't think this should be done like this, so just drop it for the moment, and we'll find a way to express that differently.

BTW, could you explain why you need it to sleep for more than tWB? I think this is because you do not wait for the controller to acknowledge that the command has been sent to the NAND device, can you check that?

drivers/mtd/nand/nand_base.c static void nand_command_lp(struct mtd_info *mtd, unsigned int command, · line 794

Quoted 12 lines
drivers/mtd/nand/nand_base.c +4 -0
 	nand_wait_ready(mtd);
 }
@@ -768,7 +787,11 @@ static void nand_command_lp(struct mtd_info *mtd, unsigned int command,
 	 * Apply this short delay always to ensure that we do wait tWB in
 	 * any case on any machine.
 	 */
+#ifdef CONFIG_TANGOX
+	udelay(1); /* needs to make it much longer than tWB */
+#else
 	ndelay(100);
+#endif

Ditto.

Quoted 12 lines
drivers/mtd/nand/nand_base.c +1 -0
 	nand_wait_ready(mtd);
 }
@@ -803,6 +826,7 @@ nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
 	spinlock_t *lock = &chip->controller->lock;
 	wait_queue_head_t *wq = &chip->controller->wq;
 	DECLARE_WAITQUEUE(wait, current);
+
 retry:
 	spin_lock(lock);
 

drivers/mtd/nand/nand_base.c static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip) · line 913

Quoted 8 lines
drivers/mtd/nand/nand_base.c +4 -0
@@ -882,7 +906,11 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
 	 * Apply this short delay always to ensure that we do wait tWB in any
 	 * case on any machine.
 	 */
+#ifdef CONFIG_TANGOX
+	udelay(1); /* needs to make it much longer than tWB */
+#else
 	ndelay(100);
+#endif

Ditto.

drivers/mtd/nand/nand_base.c static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, · line 1529

Quoted 13 lines
drivers/mtd/nand/nand_base.c +2 -0
 	if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
 		chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
@@ -1493,15 +1521,18 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
 		if (realpage != chip->pagebuf || oob) {
 			bufpoi = aligned ? buf : chip->buffers->databuf;
 
+#if defined(CONFIG_TANGOX)
 			if (likely(sndcmd)) {
 				chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
 				sndcmd = 0;
 			}
+#endif

I don't see this conditional path in mainline, but anyway, you shouldn't change that: the READ0 command in send during the ->cmdfunc() call, and if you need to trigger another READ0 command, then you should probably do that in you ecc->read_xxx() implementations.

Quoted 11 lines
drivers/mtd/nand/nand_base.c +2 -1
 			/* Now read the page into the buffer */
-			if (unlikely(ops->mode == MTD_OPS_RAW))
+			if (unlikely(ops->mode == MTD_OPS_RAW)){
 				ret = chip->ecc.read_page_raw(mtd, chip,
 							      bufpoi, page);
+			}
 			else if (!aligned && NAND_SUBPAGE_READ(chip) && !oob)
 				ret = chip->ecc.read_subpage(mtd, chip,
 							col, bytes, bufpoi);

drivers/mtd/nand/nand_base.c static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, · line 1650

Quoted 6 lines
drivers/mtd/nand/nand_base.c +3 -0
@@ -1614,6 +1645,12 @@ static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
 	struct mtd_oob_ops ops;
 	int ret;
 
+	/* Do not allow reads past end of device */
+	if ((from + len) > mtd->size)
+		return -EINVAL;

Already controlled by the MTD layer.

drivers/mtd/nand/nand_base.c static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, · line 1653

Quoted 3 lines
drivers/mtd/nand/nand_base.c +3 -0
+	if (!len)
+		return 0;
+

And this one too.

Quoted 12 lines
drivers/mtd/nand/nand_base.c +1 -0
 	nand_get_device(chip, mtd, FL_READING);
 	ops.len = len;
 	ops.datbuf = buf;
@@ -2541,6 +2578,7 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
 	struct nand_chip *chip = mtd->priv;
 	loff_t rewrite_bbt[NAND_MAX_CHIPS] = {0};
 	unsigned int bbt_masked_page = 0xffffffff;
+	int force_erase = 0;
 	loff_t len;
 
 	pr_debug("%s: start = 0x%012llx, len = %llu\n",

drivers/mtd/nand/nand_base.c int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, · line 2593

Quoted 6 lines
drivers/mtd/nand/nand_base.c +3 -0
@@ -2550,6 +2588,9 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
 	if (check_offs_len(mtd, instr->addr, instr->len))
 		return -EINVAL;
 
+	if (instr->retries == 0x73092215)
+		force_erase = 1;
+

As I said, not sure this is a good idea to allow bad block erasure, and even if we decide to do we'll probably not use this ->retries = MAGICVAL trick.

Quoted 8 lines
drivers/mtd/nand/nand_base.c +1 -0
 	/* Grab the lock and see if the device is available */
 	nand_get_device(chip, mtd, FL_ERASING);
 
@@ -2586,14 +2627,16 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
 	instr->state = MTD_ERASING;
 
 	while (len) {
+#ifndef CONFIG_MTD_NAND_BBM

drivers/mtd/nand/nand_base.c int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr, · line 2639

Quoted 10 lines
drivers/mtd/nand/nand_base.c +2 -1
 		/* Check if we have a bad block, we do not erase bad blocks! */
-		if (nand_block_checkbad(mtd, ((loff_t) page) <<
+		if (!force_erase && nand_block_checkbad(mtd, ((loff_t) page) <<
 					chip->page_shift, 0, allowbbt)) {
 			pr_warn("%s: attempt to erase a bad block at page 0x%08x\n",
 				    __func__, page);
 			instr->state = MTD_ERASE_FAILED;
 			goto erase_exit;
 		}
+#endif

Same here.

drivers/mtd/nand/nand_base.c static void nand_sync(struct mtd_info *mtd) · line 2761

Quoted 10 lines
drivers/mtd/nand/nand_base.c +3 -0
 		/*
 		 * Invalidate the page cache, if we erase the block which
@@ -2713,6 +2756,9 @@ static void nand_sync(struct mtd_info *mtd)
  */
 static int nand_block_isbad(struct mtd_info *mtd, loff_t offs)
 {
+	/* Check for invalid offset */
+	if (offs > mtd->size)
+		return -EINVAL;

Already checked in mtdcore.

Quoted 13 lines
drivers/mtd/nand/nand_base.c +6 -0
 	return nand_block_checkbad(mtd, offs, 1, 0);
 }
 
@@ -2851,6 +2897,22 @@ static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
 		chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
 		return 0;
 
+#ifdef CONFIG_TANGOX
+	chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
+	for (i = 0; i < 3; i++) {
+		int j = 0;
+
+		for ( j = 0; j < sizeof(*p); j++ ) {

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2915

Quoted 10 lines
drivers/mtd/nand/nand_base.c +10 -0
+			*(((uint8_t *)p)+j) = chip->read_byte(mtd);
+		}
+
+		if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
+				le16_to_cpu(p->crc)) {
+			pr_info("ONFI param page %d valid\n", i);
+			break;
+		}
+	}
+#else

This fix is available in mainline, so you can drop it.

Quoted 17 lines
drivers/mtd/nand/nand_base.c +2 -0
 	chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
 	for (i = 0; i < 3; i++) {
 		chip->read_buf(mtd, (uint8_t *)p, sizeof(*p));
@@ -2860,6 +2922,7 @@ static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
 			break;
 		}
 	}
+#endif
 
 	if (i == 3)
 		return 0;
@@ -2888,16 +2951,29 @@ static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
 	sanitize_string(p->model, sizeof(p->model));
 	if (!mtd->name)
 		mtd->name = p->model;
+
 	mtd->writesize = le32_to_cpu(p->byte_per_page);

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2964

Quoted 10 lines
drivers/mtd/nand/nand_base.c +9 -1
-	mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
+
+	/*
+	 * pages_per_block and blocks_per_lun may not be a power-of-2 size
+	 * (don't ask me who thought of this...). MTD assumes that these
+	 * dimensions will be power-of-2, so just truncate the remaining area.
+	 */
+	mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
+	mtd->erasesize *= mtd->writesize;
+

Hm, I'd like to have a real example (all the chips I've seen so far are using power-of-2 here). And even if that's the case, then this means we should patch nand_base to deal with that.

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2968

Quoted 5 lines
drivers/mtd/nand/nand_base.c +3 -1
 	mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
-	chip->chipsize = le32_to_cpu(p->blocks_per_lun);
+
+	/* See erasesize comment */
+	chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);

Ditto.

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2976

Quoted 9 lines
drivers/mtd/nand/nand_base.c +3 -1
 	chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
 	*busw = 0;
 	if (le16_to_cpu(p->features) & 1)
 		*busw = NAND_BUSWIDTH_16;
 
-	chip->options |= NAND_NO_READRDY | NAND_NO_AUTOINCR;
+	chip->options &= ~NAND_CHIPOPTIONS_MSK;
+	chip->options |= (NAND_NO_READRDY |
+			NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;

Hm, can this really happens? Can we really have something set in ->options before entering this function?

Quoted 67 lines
drivers/mtd/nand/nand_base.c +10 -6
 	pr_info("ONFI flash detected\n");
 	return 1;
@@ -2915,6 +2991,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 	int i, maf_idx;
 	u8 id_data[8];
 	int ret;
+	char onfi_version[3];
 
 	/* Select the device */
 	chip->select_chip(mtd, 0);
@@ -2941,7 +3018,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 
 	chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
 
-	for (i = 0; i < 2; i++)
+	for (i = 0; i < 8; i++)
 		id_data[i] = chip->read_byte(mtd);
 
 	if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
@@ -2954,12 +3031,12 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 	if (!type)
 		type = nand_flash_ids;
 
-	for (; type->name != NULL; type++)
+	for (; (type->name != NULL) && (type->id != 0); type++)
 		if (*dev_id == type->id)
 			break;
 
 	chip->onfi_version = 0;
-	if (!type->name || !type->pagesize) {
+	if (!type->name || !type->id || !type->pagesize) {
 		/* Check is chip is ONFI compliant */
 		ret = nand_flash_detect_onfi(mtd, chip, &busw);
 		if (ret)
@@ -2973,7 +3050,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 	for (i = 0; i < 8; i++)
 		id_data[i] = chip->read_byte(mtd);
 
-	if (!type->name)
+	if ((!type->name) || (!type->id))
 		return ERR_PTR(-ENODEV);
 
 	if (!mtd->name)
@@ -2995,14 +3072,17 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 		 * Field definitions are in the following datasheets:
 		 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
 		 * New style   (6 byte ID): Samsung K9GBG08U0M (p.40)
+		 * Micron      (5 byte ID): Micron MT29F16G08MAA (p.24)
+		 *      Note: Micron rule is based on heuristics for
+		 *            newer chips
 		 *
 		 * Check for wraparound + Samsung ID + nonzero 6th byte
 		 * to decide what to do.
 		 */
-		if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
+		if ((id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
 				id_data[0] == NAND_MFR_SAMSUNG &&
 				(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
-				id_data[5] != 0x00) {
+				id_data[5] != 0x00) || (id_data[0] == NAND_MFR_MIRA)) {
 			/* Calc pagesize */
 			mtd->writesize = 2048 << (extid & 0x03);
 			extid >>= 2;
@@ -3026,19 +3106,47 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 			mtd->erasesize = (128 * 1024) <<
 				(((extid >> 1) & 0x04) | (extid & 0x03));

drivers/mtd/nand/nand_base.c static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, · line 3114

Quoted 10 lines
drivers/mtd/nand/nand_base.c +2 -3
 			busw = 0;
-		} else {
+		} else if (id_data[0] == NAND_MFR_ESMT) {
 			/* Calc pagesize */
 			mtd->writesize = 1024 << (extid & 0x03);
 			extid >>= 2;
 			/* Calc oobsize */
-			mtd->oobsize = (8 << (extid & 0x01)) *
-				(mtd->writesize >> 9);
+			mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize / 512) ;

Exactly the same as the operation you removed, so the NAND_MFR_ESMT case is just the default case.

Quoted 16 lines
drivers/mtd/nand/nand_base.c +10 -3
 			extid >>= 2;
-			/* Calc blocksize. Blocksize is multiples of 64KiB */
+			/* Calc blocksize */
 			mtd->erasesize = (64 * 1024) << (extid & 0x03);
+			busw = 0;
+		} else {
+			/* Calc pagesize */
+			mtd->writesize = 1024 << (extid & 0x03);
 			extid >>= 2;
-			/* Get buswidth information */
-			busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
+
+			/* Check for 5 byte ID + Micron + read more 0x00 */
+			if (id_data[0] == NAND_MFR_MICRON && id_data[4] != 0x00
+					&& mtd->writesize >= 4096
+					&& id_data[5] == 0x00

drivers/mtd/nand/nand_base.c static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, · line 3137

Quoted 10 lines
drivers/mtd/nand/nand_base.c +10 -0
+					&& id_data[6] == 0x00) {
+				/* OOB is 218B/224B per 4KiB pagesize */
+				mtd->oobsize = ((extid & 0x03) == 0x03 ? 218 :
+						224) << (mtd->writesize >> 13);
+				extid >>= 3;
+				/* Blocksize is multiple of 64KiB */
+				mtd->erasesize = mtd->writesize <<
+					(extid & 0x03) << 6;
+				/* All Micron have busw x8? */
+				printk("[%s] All Micron : %d\n", __func__, extid);

Maybe this handling is needed, I'll try to have a look at the Micron datasheet. Anyway, this should be moved in nand_decode_ext_id().

Quoted 32 lines
drivers/mtd/nand/nand_base.c +16 -2
+				busw = 0;
+			} else {
+				/* Calc oobsize */
+				mtd->oobsize = (8 << (extid & 0x01)) *
+					(mtd->writesize >> 9);
+				extid >>= 2;
+				/* Calc blocksize (multiples of 64KiB) */
+				mtd->erasesize = (64 * 1024) << (extid & 0x03);
+				extid >>= 2;
+				/* Get buswidth information */
+				busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
+			}
 		}
 	} else {
 		/*
@@ -3062,8 +3170,9 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 			mtd->erasesize <<= ((id_data[3] & 0x03) << 1);
 		}
 	}
-	/* Get chip options */
-	chip->options |= type->options;
+	/* Get chip options, preserve non chip based options */
+	chip->options &= ~NAND_CHIPOPTIONS_MSK;
+	chip->options |= type->options & NAND_CHIPOPTIONS_MSK;
 
 	/*
 	 * Check if chip is not a Samsung device. Do not clear the
@@ -3129,10 +3238,14 @@ ident_done:
 	 */
 	if ((chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
 			(*maf_id == NAND_MFR_SAMSUNG ||
+			 *maf_id == NAND_MFR_MIRA ||

drivers/mtd/nand/nand_base.c ident_done: · line 3251

Quoted 10 lines
drivers/mtd/nand/nand_base.c +3 -0
+			 *maf_id == NAND_MFR_ESMT ||
 			 *maf_id == NAND_MFR_HYNIX))
 		chip->bbt_options |= NAND_BBT_SCANLASTPAGE;
 	else if ((!(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
 				(*maf_id == NAND_MFR_SAMSUNG ||
+				 *maf_id == NAND_MFR_MIRA ||
+				 *maf_id == NAND_MFR_ESMT ||
 				 *maf_id == NAND_MFR_HYNIX ||
 				 *maf_id == NAND_MFR_TOSHIBA ||
 				 *maf_id == NAND_MFR_AMD ||

Yep, this change is needed (if this is really the case of course). I'll check the datasheet to be sure.

Quoted 39 lines
drivers/mtd/nand/nand_base.c +28 -0
@@ -3141,6 +3254,7 @@ ident_done:
 			 *maf_id == NAND_MFR_MICRON))
 		chip->bbt_options |= NAND_BBT_SCAN2NDPAGE;
 
+
 	/* Check for AND chips with 4 page planes */
 	if (chip->options & NAND_4PAGE_ARRAY)
 		chip->erase_cmd = multi_erase_cmd;
@@ -3156,7 +3270,42 @@ ident_done:
 		nand_manuf_ids[maf_idx].name,
 		chip->onfi_version ? chip->onfi_params.model : type->name);
 
+	if (chip->onfi_version)
+		snprintf(onfi_version, sizeof(onfi_version), "%d.%d",
+					chip->onfi_version / 10,
+					chip->onfi_version % 10);
+	else
+		snprintf(onfi_version, sizeof(onfi_version), "%s", "0");
+
+	/* ID Data Mapping */
+	for (i = 0; i < 8; i++)
+	{
+		mtd->id_data[i] = id_data[i];
+	}
+
+	mtd->onfi_version = kstrdup(onfi_version, GFP_KERNEL);
+	if (!mtd->onfi_version)
+		return ERR_PTR(-ENOMEM);
+
+	mtd->nand_manufacturer = kstrdup(nand_manuf_ids[maf_idx].name, GFP_KERNEL);
+	if (!mtd->nand_manufacturer) {
+		ret = -ENOMEM;
+		goto out_onfi_version;
+	}
+
+	mtd->nand_type = kstrdup(type->name, GFP_KERNEL);
+	if (!mtd->nand_type) {
+		ret = -ENOMEM;
+		goto out_nand_type;

drivers/mtd/nand/nand_base.c ident_done: · line 3309

Quoted 10 lines
drivers/mtd/nand/nand_base.c +8 -0
+	}
+
 	return type;
+
+out_nand_type:
+	kfree(mtd->nand_type);
+out_onfi_version:
+	kfree(mtd->onfi_version);
+	return ERR_PTR(ret);
 }

You can drop all the above chunk.

Quoted 30 lines
drivers/mtd/nand/nand_base.c +8 -1
 /**
@@ -3176,6 +3325,7 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
 	int i, busw, nand_maf_id, nand_dev_id;
 	struct nand_chip *chip = mtd->priv;
 	struct nand_flash_dev *type;
+	int err;
 
 	/* Get buswidth to select the correct functions */
 	busw = chip->options & NAND_BUSWIDTH_16;
@@ -3190,7 +3340,8 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
 		if (!(chip->options & NAND_SCAN_SILENT_NODEV))
 			pr_warn("No NAND device found\n");
 		chip->select_chip(mtd, -1);
-		return PTR_ERR(type);
+		err = PTR_ERR(type);
+		goto out_error;
 	}
 
 	/* Check for a chip array */
@@ -3212,7 +3363,20 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
 	chip->numchips = i;
 	mtd->size = i * chip->chipsize;
 
+	chip->maf_id = nand_maf_id;
+	chip->dev_id = type->id;
+
 	return 0;
+
+out_error:

drivers/mtd/nand/nand_base.c int nand_scan_ident(struct mtd_info *mtd, int maxchips, · line 3381

Quoted 10 lines
drivers/mtd/nand/nand_base.c +8 -0
+	if (mtd->nand_type)
+		kfree(mtd->nand_type);
+	if (mtd->nand_manufacturer)
+		kfree(mtd->nand_manufacturer);
+	if (mtd->onfi_version)
+		kfree(mtd->onfi_version);
+
+	return err;
 }
 EXPORT_SYMBOL(nand_scan_ident);

Ditto.

Quoted 8 lines
drivers/mtd/nand/nand_base.c +3 -0
@@ -3259,6 +3423,19 @@ int nand_scan_tail(struct mtd_info *mtd)
 		case 128:
 			chip->ecc.layout = &nand_oob_128;
 			break;
+		case 224:
+			chip->ecc.layout = &nand_oob_mlcbch_224;
+			//8 bit bch ecc for Micron 1G flash, 224 oobsize use 128 for ecc

drivers/mtd/nand/nand_base.c int nand_scan_tail(struct mtd_info *mtd) · line 3438

Quoted 10 lines
drivers/mtd/nand/nand_base.c +10 -0
+			//for(i=224-112,k=0;i<224;k++,i++)
+			//	chip->ecc.layout->eccpos[k]=i;
+			//chip->ecc.layout->oobfree[0].offset=3;
+			//chip->ecc.layout->oobfree[0].length=(224-112-3);
+			break;
+#ifndef CONFIG_MTD_NAND_ECC_512
+		case 448:
+			chip->ecc.layout = &nand_oob_mlcbch_448;
+			break;
+#endif

Let's see if we can build those layout dynamically...

Quoted 12 lines
drivers/mtd/nand/nand_base.c +1 -0
 		default:
 			pr_warn("No oob scheme defined for oobsize %d\n",
 				   mtd->oobsize);
@@ -3526,6 +3703,7 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
 	ret = nand_scan_ident(mtd, maxchips, NULL);
 	if (!ret)
 		ret = nand_scan_tail(mtd);
+
 	return ret;
 }
 EXPORT_SYMBOL(nand_scan);

drivers/mtd/nand/nand_base.c void nand_release(struct mtd_info *mtd) · line 3737

Quoted 8 lines
drivers/mtd/nand/nand_base.c +4 -0
@@ -3552,6 +3730,10 @@ void nand_release(struct mtd_info *mtd)
 	if (chip->badblock_pattern && chip->badblock_pattern->options
 			& NAND_BBT_DYNAMICSTRUCT)
 		kfree(chip->badblock_pattern);
+
+	kfree(mtd->nand_type);
+	kfree(mtd->nand_manufacturer);
+	kfree(mtd->onfi_version);
 }

Drop that too.

Quoted 34 lines
drivers/mtd/nand/nand_base.c +0 -0
 EXPORT_SYMBOL_GPL(nand_release);
 
drivers/mtd/nand/nand_ids.c +4 -0
index af4fe8ca7b5e..2f14ff5efb4c 100644
@@ -107,22 +107,26 @@ struct nand_flash_dev nand_flash_ids[] = {
 	/* 8 Gigabit */
 	{"NAND 1GiB 1,8V 8-bit",	0xA3, 0, 1024, 0, LP_OPTIONS},
 	{"NAND 1GiB 3,3V 8-bit",	0xD3, 0, 1024, 0, LP_OPTIONS},
+	{"NAND 1GiB 3,3V 8-bit",	0x38, 0, 1024, 0, LP_OPTIONS},
 	{"NAND 1GiB 1,8V 16-bit",	0xB3, 0, 1024, 0, LP_OPTIONS16},
 	{"NAND 1GiB 3,3V 16-bit",	0xC3, 0, 1024, 0, LP_OPTIONS16},
 
 	/* 16 Gigabit */
 	{"NAND 2GiB 1,8V 8-bit",	0xA5, 0, 2048, 0, LP_OPTIONS},
 	{"NAND 2GiB 3,3V 8-bit",	0xD5, 0, 2048, 0, LP_OPTIONS},
+	{"NAND 2GiB 3,3V 8-bit",	0x48, 0, 2048, 0, LP_OPTIONS},
 	{"NAND 2GiB 1,8V 16-bit",	0xB5, 0, 2048, 0, LP_OPTIONS16},
 	{"NAND 2GiB 3,3V 16-bit",	0xC5, 0, 2048, 0, LP_OPTIONS16},
 
 	/* 32 Gigabit */
+	{"NAND 4GiB 3,3V 8-bit",	0x68, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 1,8V 8-bit",	0xA7, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 3,3V 8-bit",	0xD7, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 1,8V 16-bit",	0xB7, 0, 4096, 0, LP_OPTIONS16},
 	{"NAND 4GiB 3,3V 16-bit",	0xC7, 0, 4096, 0, LP_OPTIONS16},
 
 	/* 64 Gigabit */
+	{"NAND 8GiB 3,3V 8-bit",	0x88, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 1,8V 8-bit",	0xAE, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 3,3V 8-bit",	0xDE, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 1,8V 16-bit",	0xBE, 0, 8192, 0, LP_OPTIONS16},

drivers/mtd/nand/nand_ids.c struct nand_flash_dev nand_flash_ids[] = { · line 145

Quoted 7 lines
drivers/mtd/nand/nand_ids.c +1 -0
@@ -135,6 +139,7 @@ struct nand_flash_dev nand_flash_ids[] = {
 	{"NAND 16GiB 3,3V 16-bit",	0x4A, 0, 16384, 0, LP_OPTIONS16},
 
 	/* 256 Gigabit */
+	{"NAND 32GiB 3,3V 8-bit",	0xA8, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 1,8V 8-bit",	0x1C, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 3,3V 8-bit",	0x3C, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 1,8V 16-bit",	0x2C, 0, 32768, 0, LP_OPTIONS16},

Maybe those chips should be defined with full IDs. Again, I need to look at the datasheet to give a definitive answer.

Quoted 12 lines
drivers/mtd/nand/nand_ids.c +7 -1
@@ -161,7 +166,22 @@ struct nand_flash_dev nand_flash_ids[] = {
 	 BBT_AUTO_REFRESH
 	},
 
-	{NULL,}
+	/*
+	 * Fill-in gaps, may be refilled at the runtime
+	 */
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },

drivers/mtd/nand/nand_ids.c struct nand_flash_dev nand_flash_ids[] = { · line 185

Quoted 10 lines
drivers/mtd/nand/nand_ids.c +9 -0
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+
+	/*
+	 * Terminates the table
+	 */
+	{NULL, },
 };

Let's say I didn't see that. Please remove it :).

drivers/mtd/nand/nand_ids.c struct nand_manufacturers nand_manuf_ids[] = { · line 201

Quoted 7 lines
drivers/mtd/nand/nand_ids.c +1 -0
 /*
@@ -178,6 +198,8 @@ struct nand_manufacturers nand_manuf_ids[] = {
 	{NAND_MFR_MICRON, "Micron"},
 	{NAND_MFR_AMD, "AMD"},
 	{NAND_MFR_MACRONIX, "Macronix"},
+	{NAND_MFR_ESMT, "ESMT"},

There's already an entry for EON, so putting the "EON/ESMT" should be good.

Quoted 9 lines
drivers/mtd/nand/nand_ids.c +1 -0
+	{NAND_MFR_MIRA, "MIRA"},
 	{0x0, "Unknown"}
 };
 
drivers/mtd/nand/nandsim.c +0 -0
index b9cbd65f49b5..a3b0336dc374 100644

drivers/mtd/nand/nandsim.c static int init_nandsim(struct mtd_info *mtd) · line 660

Quoted 8 lines
drivers/mtd/nand/nandsim.c +1 -1
@@ -654,7 +654,7 @@ static int init_nandsim(struct mtd_info *mtd)
 	}
 
 	/* Detect how many ID bytes the NAND chip outputs */
-        for (i = 0; nand_flash_ids[i].name != NULL; i++) {
+        for (i = 0; (nand_flash_ids[i].name != NULL) && (nand_flash_ids[i].id != 0); i++) {
                 if (second_id_byte != nand_flash_ids[i].id)
                         continue;
 		if (!(nand_flash_ids[i].options & NAND_NO_AUTOINCR))

You don't need that one.

drivers/mtd/nand/smp8xxx_nand.c

Quoted 5 lines
drivers/mtd/nand/smp8xxx_nand.c new file +0 -0
new file mode 100644
index 000000000000..769144ffca44

I'll try to review the nand driver soon, stay tuned ;).

[...]

Quoted 5 lines
include/linux/mtd/mtd.h +0 -0
index cf5ea8cdcf8e..90b5caf8cacd 100644

include/linux/mtd/mtd.h struct mtd_info { · line 165

Quoted 9 lines
include/linux/mtd/mtd.h +6 -0
@@ -157,6 +157,12 @@ struct mtd_info {
 	unsigned int erasesize_mask;
 	unsigned int writesize_mask;
 
+	/* NAND related attributes */
+	const char *nand_type;
+	const char *nand_manufacturer;
+	const char *onfi_version;
+	u8 id_data[8];
+

Drop those field.

Quoted 8 lines
include/linux/mtd/mtd.h +0 -0
 	// Kernel-only stuff starts here.
 	const char *name;
 	int index;
include/linux/mtd/nand.h +0 -0
index 248351344718..f58f617ea562 100644

include/linux/mtd/nand.h typedef enum { · line 220

Quoted 6 lines
include/linux/mtd/nand.h +3 -0
@@ -215,6 +215,9 @@ typedef enum {
 #define NAND_SUBPAGE_READ(chip) ((chip->ecc.mode == NAND_ECC_SOFT) \
 					&& (chip->page_shift > 9))
 
+/* Mask to zero out the chip options, which come from the id table */
+#define NAND_CHIPOPTIONS_MSK	(0x0000ffff & ~NAND_NO_AUTOINCR)
+

I'm pretty sure you don't need this macro.

Quoted 13 lines
include/linux/mtd/nand.h +2 -0
 /* Non chip related options */
 /* This option skips the bbt scan during initialization. */
 #define NAND_SKIP_BBTSCAN	0x00010000
@@ -471,6 +474,8 @@ struct nand_buffers {
  * @controller:		[REPLACEABLE] a pointer to a hardware controller
  *			structure which is shared among multiple independent
  *			devices.
+ * @maf_id:		[OPTOINAL] manufacture ID 
+ * @dev_id:		[OPTIONAL] device ID
  * @priv:		[OPTIONAL] pointer to private chip data
  * @errstat:		[OPTIONAL] hardware specific function to perform
  *			additional error status checks (determine if errors are

include/linux/mtd/nand.h struct nand_chip { · line 550

Quoted 6 lines
include/linux/mtd/nand.h +3 -0
@@ -540,6 +545,9 @@ struct nand_chip {
 
 	struct nand_bbt_descr *badblock_pattern;
 
+	int maf_id;
+	int dev_id;
+

Let's drop those fields for now.

include/linux/mtd/nand.h struct nand_chip { · line 567

Quoted 8 lines
include/linux/mtd/nand.h +1 -0
 	void *priv;
 };
 
@@ -556,6 +564,8 @@ struct nand_chip {
 #define NAND_MFR_MICRON		0x2c
 #define NAND_MFR_AMD		0x01
 #define NAND_MFR_MACRONIX	0xc2
+#define NAND_MFR_ESMT		0x92

As Geert said, not sure we have to redefine a new macro, just say that EON and ESMT are the same.

Quoted 9 lines
include/linux/mtd/nand.h +1 -0
+#define NAND_MFR_MIRA		0xc8
 
 /**
  * struct nand_flash_dev - NAND Flash Device ID Structure

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

Signature · 3 lines
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
#8 lore raw
Quoted 23 lines · Boris Brezillon
Quoted 6 lines · On Mon, 25 Jan 2016 18:34:50 +0100 Mason <[email protected]> wrote:
drivers/mtd/nand/nand_base.c +1 -0
@@ -2888,16 +2951,29 @@ static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
 	sanitize_string(p->model, sizeof(p->model));
 	if (!mtd->name)
 		mtd->name = p->model;
+
 	mtd->writesize = le32_to_cpu(p->byte_per_page);

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2964

Quoted 10 lines
drivers/mtd/nand/nand_base.c +9 -1
-	mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
+
+	/*
+	 * pages_per_block and blocks_per_lun may not be a power-of-2 size
+	 * (don't ask me who thought of this...). MTD assumes that these
+	 * dimensions will be power-of-2, so just truncate the remaining area.
+	 */
+	mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
+	mtd->erasesize *= mtd->writesize;
+

Hm, I'd like to have a real example (all the chips I've seen so far are using power-of-2 here). And even if that's the case, then this means we should patch nand_base to deal with that.

Quoted 7 lines

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2968

Quoted 5 lines
drivers/mtd/nand/nand_base.c +3 -1
 	mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
-	chip->chipsize = le32_to_cpu(p->blocks_per_lun);
+
+	/* See erasesize comment */
+	chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);

Ditto.

It looks like he borrowed these from this commit of mine:

commit 4355b70cf48363c50a9de450b01178c83aba8f6a

Author: Brian Norris <[email protected]>
Date:   Tue Aug 27 18:45:10 2013 -0700

mtd: nand: hack ONFI for non-power-of-2 dimensions

As evidenced in the commit subject ("hack"), it's debatably a shortcut that could have been dealt with in other ways.

Brian

#9 lore raw
Quoted 34 lines · On Fri, 29 Jan 2016 10:14:29 -0800 Brian Norris <[email protected]>…
Quoted 23 lines · Boris Brezillon
Quoted 6 lines · On Mon, 25 Jan 2016 18:34:50 +0100 Mason <[email protected]> wrote:
drivers/mtd/nand/nand_base.c +1 -0
@@ -2888,16 +2951,29 @@ static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
 	sanitize_string(p->model, sizeof(p->model));
 	if (!mtd->name)
 		mtd->name = p->model;
+
 	mtd->writesize = le32_to_cpu(p->byte_per_page);

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2964

Quoted 10 lines
drivers/mtd/nand/nand_base.c +9 -1
-	mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
+
+	/*
+	 * pages_per_block and blocks_per_lun may not be a power-of-2 size
+	 * (don't ask me who thought of this...). MTD assumes that these
+	 * dimensions will be power-of-2, so just truncate the remaining area.
+	 */
+	mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
+	mtd->erasesize *= mtd->writesize;
+

Hm, I'd like to have a real example (all the chips I've seen so far are using power-of-2 here). And even if that's the case, then this means we should patch nand_base to deal with that.

Quoted 7 lines

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2968

Quoted 5 lines
drivers/mtd/nand/nand_base.c +3 -1
 	mtd->oobsize = le16_to_cpu(p->spare_bytes_per_page);
-	chip->chipsize = le32_to_cpu(p->blocks_per_lun);
+
+	/* See erasesize comment */
+	chip->chipsize = 1 << (fls(le32_to_cpu(p->blocks_per_lun)) - 1);

Ditto.

It looks like he borrowed these from this commit of mine:

Quoted 8 lines

commit 4355b70cf48363c50a9de450b01178c83aba8f6a

Author: Brian Norris <[email protected]>
Date:   Tue Aug 27 18:45:10 2013 -0700

mtd: nand: hack ONFI for non-power-of-2 dimensions

As evidenced in the commit subject ("hack"), it's debatably a shortcut that could have been dealt with in other ways.

Oh, interesting, so I was definitely wrong about that, thanks for pointing this out.

Signature · 3 lines
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
author
#10 lore raw
In reply to Boris Brezillon

Wow... you guys are truly amazing! Thanks.

Quoted 4 lines · Boris Brezillon

Hi Mason,

Just ignored all the changes that were not related to NAND or mtdcore (i.e. drivers/mtd/{chips, maps, device})

Are you telling me to ignore them? Or are you saying you ignored them? Because you know they are irrelevant?

Quoted 29 lines · Mason
Quoted 28 lines
$ git diff --stat -p v3.4.39 HEAD drivers/mtd include/linux/mtd
 drivers/mtd/Kconfig                 |   14 +-
 drivers/mtd/chips/cfi_cmdset_0002.c |  127 ++-
 drivers/mtd/devices/m25p80.c        |  100 +-
 drivers/mtd/maps/Kconfig            |   11 +-
 drivers/mtd/maps/physmap.c          |  193 +++-
 drivers/mtd/mtdchar.c               |   54 +
 drivers/mtd/mtdcore.c               |   48 +-
 drivers/mtd/mtdpart.c               |    6 +
 drivers/mtd/nand/Kconfig            |   18 +-
 drivers/mtd/nand/Makefile           |    1 +
 drivers/mtd/nand/nand_base.c        |  230 +++-
 drivers/mtd/nand/nand_ids.c         |   24 +-
 drivers/mtd/nand/nandsim.c          |    2 +-
 drivers/mtd/nand/smp8xxx_nand.c     | 1974 +++++++++++++++++++++++++++++++++++
 include/linux/mtd/map.h             |    1 +
 include/linux/mtd/mtd.h             |    6 +
 include/linux/mtd/nand.h            |   10 +
 17 files changed, 2776 insertions(+), 43 deletions(-)
drivers/mtd/Kconfig +0 -0
index 27143e042af5..a2661a490f3c 100644
@@ -22,9 +22,21 @@ config MTD_TESTS
 
Quoted 15 lines

drivers/mtd/Kconfig config MTD_TESTS · line 35

Quoted 13 lines
drivers/mtd/Kconfig +11 -0
 	  WARNING: some of the tests will ERASE entire MTD device which they
 	  test. Do not use these tests unless you really know what you do.
+config MTD_PARTITIONS
+	bool "MTD partitioning support"
+	help
+	 If you have a device which needs to divide its flash chip(s) up
+	 into multiple 'partitions', each of which appears to the user as
+	 a separate MTD device, you require this option to be enabled. If
+	 unsure, say 'Y'.
+
+	 Note, however, that you don't need this option for the DiskOnChip
+	 devices. Partitioning on NFTL 'devices' is a different - that's the
+	 'normal' form of partitioning used on a block device.

This Kconfig option is already available in mainline.

Are you referring to MTD_PARTITIONED_MASTER?

commit 727dc612c46b8f3858537ea23805b3e897cf127e

Author: Dan Ehrenberg <[email protected]>
Date:   Thu Apr 2 15:15:10 2015 -0700

Merged in v4.1 AFAICT.

Quoted 6 lines

drivers/mtd/Kconfig config MTD_TESTS · line 39

Quoted 3 lines
drivers/mtd/Kconfig +1 -0
 config MTD_REDBOOT_PARTS
 	tristate "RedBoot partition table parsing"
+	depends on !TANGOX

Why that? If you don't want REBOOT support just don't enable it in your config.

That makes sense.

Quoted 38 lines

drivers/mtd/Kconfig endif # MTD_REDBOOT_PARTS · line 90

Quoted 9 lines
drivers/mtd/Kconfig +1 -1
 	---help---
 	  RedBoot is a ROM monitor and bootloader which deals with multiple
 	  'images' in flash devices by putting a table one of the erase
@@ -75,7 +87,7 @@ endif # MTD_REDBOOT_PARTS
 
 config MTD_CMDLINE_PARTS
 	bool "Command line partition table parsing"
-	depends on MTD = "y"
+	depends on MTD = "y" && !XENV_PARTITION

I don't see any definition of XENV_PARTITIONS in you diff, and even if there was one, why would it be incompatible with partitions defined in the kernel cmdline?

Quoted 3 lines
drivers/mtd/Kconfig +0 -0
 	---help---
 	  Allow generic configuration of the MTD partition tables via the kernel
 	  command line. Multiple flash resources are supported for hardware where

[...]

drivers/mtd/mtdchar.c static int mtdchar_write_ioctl(struct mtd_info *mtd, · line 627

Quoted 13 lines
drivers/mtd/mtdchar.c +2 -0
index f2f482bec573..8219c1ce0f6b 100644
@@ -620,6 +620,8 @@ static int mtdchar_write_ioctl(struct mtd_info *mtd,
 	return ret;
 }
 
+#define MEMERASEFORCE  _IOW('M', 20, struct erase_info_user)
+
 static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 {
 	struct mtd_file_info *mfi = file->private_data;

Support for bad block erasure has been discussed several times, AFAIR no decision was taken (or maybe it was decided that it was safer to not allow it).

Quoted 9 lines

The problem here is that BBM (bad block markers) which are placed by the NAND vendor can be erased too, and if you loose this information you can start reusing a block that was really bad. ITOH, because some NAND controller have weird page layout in which BBM are replaced by in-band data or ECC bytes. So sometime it's useful to be able to force erasure of bad blocks.
Anyway, until we agree on something I suggest you drop this feature and use the nand scrub command in u-boot (if you are using u-boot ;)), which is doing exactly what you're trying to do.

Our dev boards have u-boot, but the production boards don't, as far as I understand.

Quoted 16 lines

drivers/mtd/mtdchar.c static struct attribute *mtd_attrs[] = { · line 311

Quoted 10 lines
drivers/mtd/mtdchar.c +4 -0
@@ -260,6 +303,10 @@ static struct attribute *mtd_attrs[] = {
 	&dev_attr_oobsize.attr,
 	&dev_attr_numeraseregions.attr,
 	&dev_attr_name.attr,
+	&dev_attr_nand_type.attr,
+	&dev_attr_nand_manufacturer.attr,
+	&dev_attr_nand_id.attr,
+	&dev_attr_onfi_version.attr,
 	NULL,
 };

Hm, you're adding NAND specific files in the middle of generic mtd sysfs files. I'm not saying that those information are not interesting, but maybe they could be exposed in debugfs, and they should definitely be exposed by nand_base.c not mtdcore.c.

Quoted 2 lines

Anyway, those changes are definitely not required to support your NAND controller, so you can simply drop them.

OK.

Quoted 7 lines

drivers/mtd/mtdchar.c int add_mtd_device(struct mtd_info *mtd) · line 324

Quoted 5 lines
drivers/mtd/mtdchar.c +0 -1
@@ -321,7 +368,6 @@ int add_mtd_device(struct mtd_info *mtd)
 
 	mtd->index = i;
 	mtd->usecount = 0;
-

Useless blank line removal.

If I had known someone was going to take so close a look, I would have cleaned up these silly diffs, sorry.

Quoted 24 lines

drivers/mtd/nand/Kconfig line 7

Quoted 12 lines
drivers/mtd/nand/Kconfig +7 -0
index 7d17cecad69d..e27256885ec1 100644
@@ -1,3 +1,10 @@
+config MTD_NAND_IDS
+	tristate "Include chip ids for known NAND devices."
+	depends on MTD
+	help
+	  Useful for NAND drivers that do not use the NAND subsystem but
+	  still like to take advantage of the known chip information.
+

This option already exists and is selected by MTD_NAND, no need to add a description for it (we don't want users to see this option). So again, this can be dropped.

Quoted 7 lines
drivers/mtd/nand/Kconfig +0 -0
 config MTD_NAND_ECC
 	tristate
 
@@ -83,6 +90,14 @@ config MTD_NAND_DENALI_SCRATCH_REG_ADDR
           scratch register here to enable this feature. On Intel Moorestown
           boards, the scratch register is at 0xFF108018.
 
Quoted 5 lines

drivers/mtd/nand/Kconfig config MTD_NAND_DENALI_SCRATCH_REG_ADDR · line 95

Quoted 3 lines
drivers/mtd/nand/Kconfig +3 -0
+config MTD_NAND_TANGOX
+        tristate "TANGOX NAND Device Support"
+        depends on TANGO3 || TANGO4

Maybe you can add COMPILE_TEST too.

You mean if I try to submit the driver, right?

Quoted 26 lines

drivers/mtd/nand/nand_base.c line 53

Quoted 12 lines
drivers/mtd/nand/nand_base.c +3 -0
index eb9f5fb02eef..7a98ccef937c 100644
@@ -47,7 +47,10 @@
 #include <linux/bitops.h>
 #include <linux/leds.h>
 #include <linux/io.h>
+
+#ifdef CONFIG_MTD_PARTITIONS
 #include <linux/mtd/partitions.h>
+#endif

You don't need to make this inclusion conditional.

Quoted 11 lines
drivers/mtd/nand/nand_base.c +2 -2
 /* Define default oob placement schemes for large and small page devices */
 static struct nand_ecclayout nand_oob_8 = {
@@ -64,8 +67,11 @@ static struct nand_ecclayout nand_oob_16 = {
 	.eccbytes = 6,
 	.eccpos = {0, 1, 2, 3, 6, 7},
 	.oobfree = {
-		{.offset = 8,
-		 . length = 8} }
+#ifdef CONFIG_MTD_NAND_BBM
+		{.offset = 9, . length = 7}}		//nand bbm config
Quoted 8 lines

drivers/mtd/nand/nand_base.c static struct nand_ecclayout nand_oob_16 = { · line 75

Quoted 4 lines
drivers/mtd/nand/nand_base.c +3 -0
+#else
+		{.offset = 8, . length = 8}}
+#endif
 };

If you need to define your own ooblayout then it should be done at the NAND controller, and you should not make it conditional on some config option.

Hmmm, I think the CONFIG_MTD_NAND_BBM is for a different product, so it can be ignored.

Quoted 17 lines

drivers/mtd/nand/nand_base.c static int check_offs_len(struct mtd_info *mtd, · line 140

Quoted 10 lines
drivers/mtd/nand/nand_base.c +6 -0
@@ -123,6 +132,12 @@ static int check_offs_len(struct mtd_info *mtd,
 		ret = -EINVAL;
 	}
 
+	/* Do not allow past end of device */
+	if (ofs + len > mtd->size) {
+		pr_debug( "%s: Past end of device\n",__func__);
+		ret = -EINVAL;
+	}
+

This is already checked at the MTD level, so I don't think you need that.

Quoted 3 lines
drivers/mtd/nand/nand_base.c +0 -0
 	return ret;
 }
 
Quoted 14 lines

drivers/mtd/nand/nand_base.c static void nand_command(struct mtd_info *mtd, unsigned int command, · line 668

Quoted 9 lines
drivers/mtd/nand/nand_base.c +4 -0
@@ -646,7 +661,11 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
 	 * Apply this short delay always to ensure that we do wait tWB in
 	 * any case on any machine.
 	 */
+#ifdef CONFIG_TANGOX
+	udelay(1); /* needs to make it much longer than tWB */
+#else
 	ndelay(100);
+#endif

Yes, this delay here should probably depends on the NAND timings attached to the NAND device, but anyway, I don't think this should be done like this, so just drop it for the moment, and we'll find a way to express that differently.

There's a lot of cargo cult delays all over our drivers.

Quoted 4 lines

BTW, could you explain why you need it to sleep for more than tWB? I think this is because you do not wait for the controller to acknowledge that the command has been sent to the NAND device, can you check that?

Could it be that, on some setups, ndelay(100) is equivalent to no wait at all?

Quoted 6 lines
Quoted 6 lines
drivers/mtd/nand/nand_base.c +0 -0
 	if ((state == FL_ERASING) && (chip->options & NAND_IS_AND))
 		chip->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
@@ -1493,15 +1521,18 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
 		if (realpage != chip->pagebuf || oob) {
 			bufpoi = aligned ? buf : chip->buffers->databuf;
 
Quoted 11 lines

drivers/mtd/nand/nand_base.c static int nand_do_read_ops(struct mtd_info *mtd, loff_t from, · line 1529

Quoted 6 lines
drivers/mtd/nand/nand_base.c +2 -0
+#if defined(CONFIG_TANGOX)
 			if (likely(sndcmd)) {
 				chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
 				sndcmd = 0;
 			}
+#endif

I don't see this conditional path in mainline, but anyway, you shouldn't change that: the READ0 command in send during the ->cmdfunc() call, and if you need to trigger another READ0 command, then you should probably do that in you ecc->read_xxx() implementations.

This is archeology. We are talking about the 3.4 kernel, released almost 4 years ago.

Quoted 3 lines

As I said, not sure this is a good idea to allow bad block erasure, and even if we decide to do we'll probably not use this ->retries = MAGICVAL trick.

<smirk>

Quoted 17 lines
Quoted 17 lines
drivers/mtd/nand/nand_base.c +10 -0
 	return nand_block_checkbad(mtd, offs, 1, 0);
 }
 
@@ -2851,6 +2897,22 @@ static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip,
 		chip->read_byte(mtd) != 'F' || chip->read_byte(mtd) != 'I')
 		return 0;
 
+#ifdef CONFIG_TANGOX
+	chip->cmdfunc(mtd, NAND_CMD_PARAM, 0, -1);
+	for (i = 0; i < 3; i++) {
+		int j = 0;
+
+		for ( j = 0; j < sizeof(*p); j++ ) {
+			*(((uint8_t *)p)+j) = chip->read_byte(mtd);
+		}
+
+		if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) ==
Quoted 8 lines

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2915

Quoted 6 lines
drivers/mtd/nand/nand_base.c +6 -0
+				le16_to_cpu(p->crc)) {
+			pr_info("ONFI param page %d valid\n", i);
+			break;
+		}
+	}
+#else

This fix is available in mainline, so you can drop it.

I got into a heated exchange over this patch (specifically how one should use git cherry-pick).

Quoted 7 lines
Quoted 7 lines
drivers/mtd/nand/nand_base.c +5 -1
 	mtd->writesize = le32_to_cpu(p->byte_per_page);
-	mtd->erasesize = le32_to_cpu(p->pages_per_block) * mtd->writesize;
+
+	/*
+	 * pages_per_block and blocks_per_lun may not be a power-of-2 size
+	 * (don't ask me who thought of this...). MTD assumes that these
+	 * dimensions will be power-of-2, so just truncate the remaining area.
Quoted 8 lines

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2964

Quoted 4 lines
drivers/mtd/nand/nand_base.c +4 -0
+	 */
+	mtd->erasesize = 1 << (fls(le32_to_cpu(p->pages_per_block)) - 1);
+	mtd->erasesize *= mtd->writesize;
+

Hm, I'd like to have a real example (all the chips I've seen so far are using power-of-2 here). And even if that's the case, then this means we should patch nand_base to deal with that.

As Brian pointed out, this patch didn't come from here. The well-thought out comment is a dead giveaway.

Quoted 5 lines
Quoted 5 lines
drivers/mtd/nand/nand_base.c +0 -0
 	chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
 	*busw = 0;
 	if (le16_to_cpu(p->features) & 1)
 		*busw = NAND_BUSWIDTH_16;
Quoted 7 lines

drivers/mtd/nand/nand_base.c static int nand_flash_detect_onfi(struct mtd_info *mtd, struct nand_chip *chip, · line 2976

Quoted 4 lines
drivers/mtd/nand/nand_base.c +3 -1
-	chip->options |= NAND_NO_READRDY | NAND_NO_AUTOINCR;
+	chip->options &= ~NAND_CHIPOPTIONS_MSK;
+	chip->options |= (NAND_NO_READRDY |
+			NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;

Hm, can this really happens? Can we really have something set in ->options before entering this function?

Are you asking me?

Quoted 7 lines

drivers/mtd/nand/nand_base.c static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, · line 3077

Quoted 7 lines
drivers/mtd/nand/nand_base.c +3 -0
@@ -2995,14 +3072,17 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 		 * Field definitions are in the following datasheets:
 		 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
 		 * New style   (6 byte ID): Samsung K9GBG08U0M (p.40)
+		 * Micron      (5 byte ID): Micron MT29F16G08MAA (p.24)
+		 *      Note: Micron rule is based on heuristics for
+		 *            newer chips

http://lists.infradead.org/pipermail/linux-mtd/2010-July/031068.html

Quoted 51 lines
Quoted 16 lines
drivers/mtd/nand/nand_base.c +2 -2
 		 *
 		 * Check for wraparound + Samsung ID + nonzero 6th byte
 		 * to decide what to do.
 		 */
-		if (id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
+		if ((id_data[0] == id_data[6] && id_data[1] == id_data[7] &&
 				id_data[0] == NAND_MFR_SAMSUNG &&
 				(chip->cellinfo & NAND_CI_CELLTYPE_MSK) &&
-				id_data[5] != 0x00) {
+				id_data[5] != 0x00) || (id_data[0] == NAND_MFR_MIRA)) {
 			/* Calc pagesize */
 			mtd->writesize = 2048 << (extid & 0x03);
 			extid >>= 2;
@@ -3026,19 +3106,47 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 			mtd->erasesize = (128 * 1024) <<
 				(((extid >> 1) & 0x04) | (extid & 0x03));

drivers/mtd/nand/nand_base.c static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, · line 3114

Quoted 10 lines
drivers/mtd/nand/nand_base.c +2 -3
 			busw = 0;
-		} else {
+		} else if (id_data[0] == NAND_MFR_ESMT) {
 			/* Calc pagesize */
 			mtd->writesize = 1024 << (extid & 0x03);
 			extid >>= 2;
 			/* Calc oobsize */
-			mtd->oobsize = (8 << (extid & 0x01)) *
-				(mtd->writesize >> 9);
+			mtd->oobsize = (8 << (extid & 0x01)) * (mtd->writesize / 512) ;

Exactly the same as the operation you removed, so the NAND_MFR_ESMT case is just the default case.

Quoted 21 lines
drivers/mtd/nand/nand_base.c +15 -3
 			extid >>= 2;
-			/* Calc blocksize. Blocksize is multiples of 64KiB */
+			/* Calc blocksize */
 			mtd->erasesize = (64 * 1024) << (extid & 0x03);
+			busw = 0;
+		} else {
+			/* Calc pagesize */
+			mtd->writesize = 1024 << (extid & 0x03);
 			extid >>= 2;
-			/* Get buswidth information */
-			busw = (extid & 0x01) ? NAND_BUSWIDTH_16 : 0;
+
+			/* Check for 5 byte ID + Micron + read more 0x00 */
+			if (id_data[0] == NAND_MFR_MICRON && id_data[4] != 0x00
+					&& mtd->writesize >= 4096
+					&& id_data[5] == 0x00
+					&& id_data[6] == 0x00) {
+				/* OOB is 218B/224B per 4KiB pagesize */
+				mtd->oobsize = ((extid & 0x03) == 0x03 ? 218 :
+						224) << (mtd->writesize >> 13);
+				extid >>= 3;
Quoted 8 lines

drivers/mtd/nand/nand_base.c static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, · line 3137

Quoted 5 lines
drivers/mtd/nand/nand_base.c +5 -0
+				/* Blocksize is multiple of 64KiB */
+				mtd->erasesize = mtd->writesize <<
+					(extid & 0x03) << 6;
+				/* All Micron have busw x8? */
+				printk("[%s] All Micron : %d\n", __func__, extid);

Maybe this handling is needed, I'll try to have a look at the Micron datasheet. Anyway, this should be moved in nand_decode_ext_id().

I think this is also part of Brian's patch:
http://lists.infradead.org/pipermail/linux-mtd/2010-July/031068.html

Quoted 35 lines
Quoted 35 lines
drivers/mtd/nand/nand_base.c +30 -0
@@ -3156,7 +3270,42 @@ ident_done:
 		nand_manuf_ids[maf_idx].name,
 		chip->onfi_version ? chip->onfi_params.model : type->name);
 
+	if (chip->onfi_version)
+		snprintf(onfi_version, sizeof(onfi_version), "%d.%d",
+					chip->onfi_version / 10,
+					chip->onfi_version % 10);
+	else
+		snprintf(onfi_version, sizeof(onfi_version), "%s", "0");
+
+	/* ID Data Mapping */
+	for (i = 0; i < 8; i++)
+	{
+		mtd->id_data[i] = id_data[i];
+	}
+
+	mtd->onfi_version = kstrdup(onfi_version, GFP_KERNEL);
+	if (!mtd->onfi_version)
+		return ERR_PTR(-ENOMEM);
+
+	mtd->nand_manufacturer = kstrdup(nand_manuf_ids[maf_idx].name, GFP_KERNEL);
+	if (!mtd->nand_manufacturer) {
+		ret = -ENOMEM;
+		goto out_onfi_version;
+	}
+
+	mtd->nand_type = kstrdup(type->name, GFP_KERNEL);
+	if (!mtd->nand_type) {
+		ret = -ENOMEM;
+		goto out_nand_type;
+	}
+
 	return type;
+
Quoted 8 lines

drivers/mtd/nand/nand_base.c ident_done: · line 3309

Quoted 6 lines
drivers/mtd/nand/nand_base.c +5 -0
+out_nand_type:
+	kfree(mtd->nand_type);
+out_onfi_version:
+	kfree(mtd->onfi_version);
+	return ERR_PTR(ret);
 }

You can drop all the above chunk.

I thought you'd written "all the above junk" :-)

Quoted 11 lines
Quoted 11 lines
drivers/mtd/nand/nand_base.c +7 -0
@@ -3259,6 +3423,19 @@ int nand_scan_tail(struct mtd_info *mtd)
 		case 128:
 			chip->ecc.layout = &nand_oob_128;
 			break;
+		case 224:
+			chip->ecc.layout = &nand_oob_mlcbch_224;
+			//8 bit bch ecc for Micron 1G flash, 224 oobsize use 128 for ecc
+			//for(i=224-112,k=0;i<224;k++,i++)
+			//	chip->ecc.layout->eccpos[k]=i;
+			//chip->ecc.layout->oobfree[0].offset=3;
+			//chip->ecc.layout->oobfree[0].length=(224-112-3);
Quoted 8 lines

drivers/mtd/nand/nand_base.c int nand_scan_tail(struct mtd_info *mtd) · line 3438

Quoted 6 lines
drivers/mtd/nand/nand_base.c +6 -0
+			break;
+#ifndef CONFIG_MTD_NAND_ECC_512
+		case 448:
+			chip->ecc.layout = &nand_oob_mlcbch_448;
+			break;
+#endif

Let's see if we can build those layout dynamically...

You mean compute the array at run-time, instead of having it pre-computed and hard-coded?

Quoted 12 lines
Quoted 12 lines
drivers/mtd/nand/nand_ids.c +1 -0
index af4fe8ca7b5e..2f14ff5efb4c 100644
@@ -107,22 +107,26 @@ struct nand_flash_dev nand_flash_ids[] = {
 	/* 8 Gigabit */
 	{"NAND 1GiB 1,8V 8-bit",	0xA3, 0, 1024, 0, LP_OPTIONS},
 	{"NAND 1GiB 3,3V 8-bit",	0xD3, 0, 1024, 0, LP_OPTIONS},
+	{"NAND 1GiB 3,3V 8-bit",	0x38, 0, 1024, 0, LP_OPTIONS},
 	{"NAND 1GiB 1,8V 16-bit",	0xB3, 0, 1024, 0, LP_OPTIONS16},
 	{"NAND 1GiB 3,3V 16-bit",	0xC3, 0, 1024, 0, LP_OPTIONS16},
 
Quoted 4 lines

drivers/mtd/nand/nand_ids.c struct nand_flash_dev nand_flash_ids[] = { · line 117

Quoted 4 lines
drivers/mtd/nand/nand_ids.c +1 -0
 	/* 16 Gigabit */
 	{"NAND 2GiB 1,8V 8-bit",	0xA5, 0, 2048, 0, LP_OPTIONS},
 	{"NAND 2GiB 3,3V 8-bit",	0xD5, 0, 2048, 0, LP_OPTIONS},
+	{"NAND 2GiB 3,3V 8-bit",	0x48, 0, 2048, 0, LP_OPTIONS},

At least this one comes from Brian's patch.

Quoted 43 lines
Quoted 16 lines
drivers/mtd/nand/nand_ids.c +2 -0
 	{"NAND 2GiB 1,8V 16-bit",	0xB5, 0, 2048, 0, LP_OPTIONS16},
 	{"NAND 2GiB 3,3V 16-bit",	0xC5, 0, 2048, 0, LP_OPTIONS16},
 
 	/* 32 Gigabit */
+	{"NAND 4GiB 3,3V 8-bit",	0x68, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 1,8V 8-bit",	0xA7, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 3,3V 8-bit",	0xD7, 0, 4096, 0, LP_OPTIONS},
 	{"NAND 4GiB 1,8V 16-bit",	0xB7, 0, 4096, 0, LP_OPTIONS16},
 	{"NAND 4GiB 3,3V 16-bit",	0xC7, 0, 4096, 0, LP_OPTIONS16},
 
 	/* 64 Gigabit */
+	{"NAND 8GiB 3,3V 8-bit",	0x88, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 1,8V 8-bit",	0xAE, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 3,3V 8-bit",	0xDE, 0, 8192, 0, LP_OPTIONS},
 	{"NAND 8GiB 1,8V 16-bit",	0xBE, 0, 8192, 0, LP_OPTIONS16},

drivers/mtd/nand/nand_ids.c struct nand_flash_dev nand_flash_ids[] = { · line 145

Quoted 7 lines
drivers/mtd/nand/nand_ids.c +1 -0
@@ -135,6 +139,7 @@ struct nand_flash_dev nand_flash_ids[] = {
 	{"NAND 16GiB 3,3V 16-bit",	0x4A, 0, 16384, 0, LP_OPTIONS16},
 
 	/* 256 Gigabit */
+	{"NAND 32GiB 3,3V 8-bit",	0xA8, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 1,8V 8-bit",	0x1C, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 3,3V 8-bit",	0x3C, 0, 32768, 0, LP_OPTIONS},
 	{"NAND 32GiB 1,8V 16-bit",	0x2C, 0, 32768, 0, LP_OPTIONS16},

Maybe those chips should be defined with full IDs. Again, I need to look at the datasheet to give a definitive answer.

Quoted 16 lines
drivers/mtd/nand/nand_ids.c +11 -1
@@ -161,7 +166,22 @@ struct nand_flash_dev nand_flash_ids[] = {
 	 BBT_AUTO_REFRESH
 	},
 
-	{NULL,}
+	/*
+	 * Fill-in gaps, may be refilled at the runtime
+	 */
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
Quoted 8 lines

drivers/mtd/nand/nand_ids.c struct nand_flash_dev nand_flash_ids[] = { · line 185

Quoted 6 lines
drivers/mtd/nand/nand_ids.c +5 -0
+
+	/*
+	 * Terminates the table
+	 */
+	{NULL, },
 };

Let's say I didn't see that. Please remove it :).

"Interesting" isn't it?

Quoted 8 lines

drivers/mtd/nand/nand_ids.c struct nand_manufacturers nand_manuf_ids[] = { · line 201

Quoted 5 lines
drivers/mtd/nand/nand_ids.c +1 -0
@@ -178,6 +198,8 @@ struct nand_manufacturers nand_manuf_ids[] = {
 	{NAND_MFR_MICRON, "Micron"},
 	{NAND_MFR_AMD, "AMD"},
 	{NAND_MFR_MACRONIX, "Macronix"},
+	{NAND_MFR_ESMT, "ESMT"},

There's already an entry for EON, so putting the "EON/ESMT" should be good.

The IDs are unique?
There have been less than 256 manufacturers, ever?

Quoted 59 lines
Quoted 5 lines
include/linux/mtd/mtd.h +0 -0
index cf5ea8cdcf8e..90b5caf8cacd 100644

include/linux/mtd/mtd.h struct mtd_info { · line 165

Quoted 9 lines
include/linux/mtd/mtd.h +6 -0
@@ -157,6 +157,12 @@ struct mtd_info {
 	unsigned int erasesize_mask;
 	unsigned int writesize_mask;
 
+	/* NAND related attributes */
+	const char *nand_type;
+	const char *nand_manufacturer;
+	const char *onfi_version;
+	u8 id_data[8];
+

Drop those field.

Quoted 8 lines
include/linux/mtd/mtd.h +0 -0
 	// Kernel-only stuff starts here.
 	const char *name;
 	int index;
include/linux/mtd/nand.h +0 -0
index 248351344718..f58f617ea562 100644

include/linux/mtd/nand.h typedef enum { · line 220

Quoted 6 lines
include/linux/mtd/nand.h +3 -0
@@ -215,6 +215,9 @@ typedef enum {
 #define NAND_SUBPAGE_READ(chip) ((chip->ecc.mode == NAND_ECC_SOFT) \
 					&& (chip->page_shift > 9))
 
+/* Mask to zero out the chip options, which come from the id table */
+#define NAND_CHIPOPTIONS_MSK	(0x0000ffff & ~NAND_NO_AUTOINCR)
+

I'm pretty sure you don't need this macro.

Quoted 13 lines
include/linux/mtd/nand.h +2 -0
 /* Non chip related options */
 /* This option skips the bbt scan during initialization. */
 #define NAND_SKIP_BBTSCAN	0x00010000
@@ -471,6 +474,8 @@ struct nand_buffers {
  * @controller:		[REPLACEABLE] a pointer to a hardware controller
  *			structure which is shared among multiple independent
  *			devices.
+ * @maf_id:		[OPTOINAL] manufacture ID 
+ * @dev_id:		[OPTIONAL] device ID
  * @priv:		[OPTIONAL] pointer to private chip data
  * @errstat:		[OPTIONAL] hardware specific function to perform
  *			additional error status checks (determine if errors are

include/linux/mtd/nand.h struct nand_chip { · line 550

Quoted 6 lines
include/linux/mtd/nand.h +3 -0
@@ -540,6 +545,9 @@ struct nand_chip {
 
 	struct nand_bbt_descr *badblock_pattern;
 
+	int maf_id;
+	int dev_id;
+

Let's drop those fields for now.

Quoted 3 lines
include/linux/mtd/nand.h +0 -0
 	void *priv;
 };
 
Quoted 8 lines

include/linux/mtd/nand.h struct nand_chip { · line 567

Quoted 5 lines
include/linux/mtd/nand.h +1 -0
@@ -556,6 +564,8 @@ struct nand_chip {
 #define NAND_MFR_MICRON		0x2c
 #define NAND_MFR_AMD		0x01
 #define NAND_MFR_MACRONIX	0xc2
+#define NAND_MFR_ESMT		0x92

As Geert said, not sure we have to redefine a new macro, just say that EON and ESMT are the same.

But if someone expects ESMT, are they supposed to know EON was swallowed by ESMT?

Quoted 1 line

include/linux/mtd/nand.h struct nand_chip { · line 568

Quoted 1 line
include/linux/mtd/nand.h +1 -0
+#define NAND_MFR_MIRA		0xc8

What about this one?

http://www.deutron.com.tw/miles.htm
2002 MIRA renamed as Deutron focus on DRAM spot market.

http://www.datasheetspdf.com/PDF/PSU8GA30AT/897320/1

Anyway, many many thanks for having taken such a close look at this patch.

Regards.

#11 lore raw

Hi Mason,

Quoted 11 lines · On Sat, 30 Jan 2016 00:25:58 +0100 Mason <[email protected]> wrote:

Wow... you guys are truly amazing! Thanks.

Quoted 4 lines · Boris Brezillon

Hi Mason,

Just ignored all the changes that were not related to NAND or mtdcore (i.e. drivers/mtd/{chips, maps, device})

Are you telling me to ignore them? Or are you saying you ignored them? Because you know they are irrelevant?

I'm saying that I didn't review those changes, because as Brian said, if the only thing you want is to add support for your NAND controller, then you don't need them.
So yes, you can drop them too.

Quoted 46 lines
Quoted 30 lines · Mason
Quoted 28 lines
$ git diff --stat -p v3.4.39 HEAD drivers/mtd include/linux/mtd
 drivers/mtd/Kconfig                 |   14 +-
 drivers/mtd/chips/cfi_cmdset_0002.c |  127 ++-
 drivers/mtd/devices/m25p80.c        |  100 +-
 drivers/mtd/maps/Kconfig            |   11 +-
 drivers/mtd/maps/physmap.c          |  193 +++-
 drivers/mtd/mtdchar.c               |   54 +
 drivers/mtd/mtdcore.c               |   48 +-
 drivers/mtd/mtdpart.c               |    6 +
 drivers/mtd/nand/Kconfig            |   18 +-
 drivers/mtd/nand/Makefile           |    1 +
 drivers/mtd/nand/nand_base.c        |  230 +++-
 drivers/mtd/nand/nand_ids.c         |   24 +-
 drivers/mtd/nand/nandsim.c          |    2 +-
 drivers/mtd/nand/smp8xxx_nand.c     | 1974 +++++++++++++++++++++++++++++++++++
 include/linux/mtd/map.h             |    1 +
 include/linux/mtd/mtd.h             |    6 +
 include/linux/mtd/nand.h            |   10 +
 17 files changed, 2776 insertions(+), 43 deletions(-)
drivers/mtd/Kconfig +0 -0
index 27143e042af5..a2661a490f3c 100644
@@ -22,9 +22,21 @@ config MTD_TESTS
 
Quoted 15 lines

drivers/mtd/Kconfig config MTD_TESTS · line 35

Quoted 13 lines
drivers/mtd/Kconfig +11 -0
 	  WARNING: some of the tests will ERASE entire MTD device which they
 	  test. Do not use these tests unless you really know what you do.
+config MTD_PARTITIONS
+	bool "MTD partitioning support"
+	help
+	 If you have a device which needs to divide its flash chip(s) up
+	 into multiple 'partitions', each of which appears to the user as
+	 a separate MTD device, you require this option to be enabled. If
+	 unsure, say 'Y'.
+
+	 Note, however, that you don't need this option for the DiskOnChip
+	 devices. Partitioning on NFTL 'devices' is a different - that's the
+	 'normal' form of partitioning used on a block device.

This Kconfig option is already available in mainline.

Quoted 1 line

Are you referring to MTD_PARTITIONED_MASTER?

Oh, you're right, I thought there was an option, but mtdpart.c is compiled when you enable MTD support. Anyway, I don't think we need an extra option to disable MTD_PARTITIONS, so you should still drop those changes.

[...]

Quoted 28 lines
Quoted 18 lines

drivers/mtd/mtdchar.c static int mtdchar_write_ioctl(struct mtd_info *mtd, · line 627

Quoted 13 lines
drivers/mtd/mtdchar.c +2 -0
index f2f482bec573..8219c1ce0f6b 100644
@@ -620,6 +620,8 @@ static int mtdchar_write_ioctl(struct mtd_info *mtd,
 	return ret;
 }
 
+#define MEMERASEFORCE  _IOW('M', 20, struct erase_info_user)
+
 static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
 {
 	struct mtd_file_info *mfi = file->private_data;

Support for bad block erasure has been discussed several times, AFAIR no decision was taken (or maybe it was decided that it was safer to not allow it).

Quoted 9 lines

The problem here is that BBM (bad block markers) which are placed by the NAND vendor can be erased too, and if you loose this information you can start reusing a block that was really bad. ITOH, because some NAND controller have weird page layout in which BBM are replaced by in-band data or ECC bytes. So sometime it's useful to be able to force erasure of bad blocks.
Anyway, until we agree on something I suggest you drop this feature and use the nand scrub command in u-boot (if you are using u-boot ;)), which is doing exactly what you're trying to do.

Quoted 2 lines

Our dev boards have u-boot, but the production boards don't, as far as I understand.

Ok, but let's keep that for later (you don't need this feature to support your NAND controller).

[...]

Quoted 25 lines
Quoted 25 lines

drivers/mtd/nand/Kconfig line 7

Quoted 13 lines
drivers/mtd/nand/Kconfig +7 -0
index 7d17cecad69d..e27256885ec1 100644
@@ -1,3 +1,10 @@
+config MTD_NAND_IDS
+	tristate "Include chip ids for known NAND devices."
+	depends on MTD
+	help
+	  Useful for NAND drivers that do not use the NAND subsystem but
+	  still like to take advantage of the known chip information.
+

This option already exists and is selected by MTD_NAND, no need to add a description for it (we don't want users to see this option). So again, this can be dropped.

Quoted 7 lines
drivers/mtd/nand/Kconfig +0 -0
 config MTD_NAND_ECC
 	tristate
 
@@ -83,6 +90,14 @@ config MTD_NAND_DENALI_SCRATCH_REG_ADDR
           scratch register here to enable this feature. On Intel Moorestown
           boards, the scratch register is at 0xFF108018.
 
Quoted 7 lines
Quoted 5 lines

drivers/mtd/nand/Kconfig config MTD_NAND_DENALI_SCRATCH_REG_ADDR · line 95

Quoted 3 lines
drivers/mtd/nand/Kconfig +3 -0
+config MTD_NAND_TANGOX
+        tristate "TANGOX NAND Device Support"
+        depends on TANGO3 || TANGO4

Maybe you can add COMPILE_TEST too.

You mean if I try to submit the driver, right?

Yes. Wasn't it the point of the RFC?

[...]

Quoted 18 lines
Quoted 15 lines

drivers/mtd/nand/Kconfig static void nand_command(struct mtd_info *mtd, unsigned int command, · line 668

Quoted 10 lines
drivers/mtd/nand/Kconfig +4 -0
@@ -646,7 +661,11 @@ static void nand_command(struct mtd_info *mtd, unsigned int command,
 	 * Apply this short delay always to ensure that we do wait tWB in
 	 * any case on any machine.
 	 */
+#ifdef CONFIG_TANGOX
+	udelay(1); /* needs to make it much longer than tWB */
+#else
 	ndelay(100);
+#endif

Yes, this delay here should probably depends on the NAND timings attached to the NAND device, but anyway, I don't think this should be done like this, so just drop it for the moment, and we'll find a way to express that differently.

There's a lot of cargo cult delays all over our drivers.

Quoted 7 lines
Quoted 4 lines

BTW, could you explain why you need it to sleep for more than tWB? I think this is because you do not wait for the controller to acknowledge that the command has been sent to the NAND device, can you check that?

Could it be that, on some setups, ndelay(100) is equivalent to no wait at all?

Hm, it should not, but maybe. Anyway, I'll look at the driver code to check that you're waiting for the HW ack before returning from ->cmd_ctrl().

[...]

Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
drivers/mtd/nand/Kconfig +0 -0
 	chip->chipsize *= (uint64_t)mtd->erasesize * p->lun_count;
 	*busw = 0;
 	if (le16_to_cpu(p->features) & 1)

*busw = NAND_BUSWIDTH_16;

Quoted 7 lines

drivers/mtd/nand/Kconfig

Quoted 4 lines
drivers/mtd/nand/Kconfig +3 -1
-	chip->options |= NAND_NO_READRDY | NAND_NO_AUTOINCR;
+	chip->options &= ~NAND_CHIPOPTIONS_MSK;
+	chip->options |= (NAND_NO_READRDY |
+			NAND_NO_AUTOINCR) & NAND_CHIPOPTIONS_MSK;

Hm, can this really happens? Can we really have something set in ->options before entering this function?

Quoted 1 line

Are you asking me?

Yes, and also kinda asking Brian too. But I think this field is set to 0 if you allocate your nand_chip struct with kzalloc().

Quoted 10 lines
Quoted 8 lines

drivers/mtd/nand/Kconfig static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, · line 3077

Quoted 8 lines
drivers/mtd/nand/Kconfig +3 -0
@@ -2995,14 +3072,17 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
 		 * Field definitions are in the following datasheets:
 		 * Old style (4,5 byte ID): Samsung K9GAG08U0M (p.32)
 		 * New style   (6 byte ID): Samsung K9GBG08U0M (p.40)
+		 * Micron      (5 byte ID): Micron MT29F16G08MAA (p.24)
+		 *      Note: Micron rule is based on heuristics for
+		 *            newer chips

http://lists.infradead.org/pipermail/linux-mtd/2010-July/031068.html

Okay, so according to the patch those chips are ONFI compliant, so I guess you don't need this. Just make sure your driver supports ONFI detection (which should be the case if you're implementing ->cmd_ctrl()).

[...]

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
drivers/mtd/nand/Kconfig +7 -0
@@ -3259,6 +3423,19 @@ int nand_scan_tail(struct mtd_info *mtd)
 		case 128:
 			chip->ecc.layout = &nand_oob_128;
 			break;
+		case 224:
+			chip->ecc.layout = &nand_oob_mlcbch_224;
+			//8 bit bch ecc for Micron 1G flash, 224 oobsize use 128 for ecc
+			//for(i=224-112,k=0;i<224;k++,i++)
+			//	chip->ecc.layout->eccpos[k]=i;
+			//chip->ecc.layout->oobfree[0].offset=3;
+			//chip->ecc.layout->oobfree[0].length=(224-112-3);
Quoted 8 lines

drivers/mtd/nand/Kconfig int nand_scan_tail(struct mtd_info *mtd) · line 3438

Quoted 6 lines
drivers/mtd/nand/Kconfig +6 -0
+			break;
+#ifndef CONFIG_MTD_NAND_ECC_512
+		case 448:
+			chip->ecc.layout = &nand_oob_mlcbch_448;
+			break;
+#endif

Let's see if we can build those layout dynamically...

Quoted 2 lines

You mean compute the array at run-time, instead of having it pre-computed and hard-coded?

Yes.

[...]

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
drivers/mtd/nand/Kconfig +11 -1
-	{NULL,}
+	/*
+	 * Fill-in gaps, may be refilled at the runtime
+	 */
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
+	{"                                ", 0, },
Quoted 8 lines

drivers/mtd/nand/Kconfig

Quoted 6 lines
drivers/mtd/nand/Kconfig +5 -0
+
+	/*
+	 * Terminates the table
+	 */
+	{NULL, },
 };

Let's say I didn't see that. Please remove it :).

Quoted 1 line

"Interesting" isn't it?

Yep :).

Quoted 11 lines
Quoted 9 lines

drivers/mtd/nand/Kconfig struct nand_manufacturers nand_manuf_ids[] = { · line 201

Quoted 6 lines
drivers/mtd/nand/Kconfig +1 -0
@@ -178,6 +198,8 @@ struct nand_manufacturers nand_manuf_ids[] = {
 	{NAND_MFR_MICRON, "Micron"},
 	{NAND_MFR_AMD, "AMD"},
 	{NAND_MFR_MACRONIX, "Macronix"},
+	{NAND_MFR_ESMT, "ESMT"},

There's already an entry for EON, so putting the "EON/ESMT" should be good.

The IDs are unique?

Yes.

Quoted 1 line

There have been less than 256 manufacturers, ever?

Apparently, far less than 256 according to the ids defined in the header ;).

[...]

Quoted 10 lines
Quoted 9 lines

drivers/mtd/nand/Kconfig struct nand_chip { · line 567

Quoted 6 lines
drivers/mtd/nand/Kconfig +1 -0
@@ -556,6 +564,8 @@ struct nand_chip {
 #define NAND_MFR_MICRON		0x2c
 #define NAND_MFR_AMD		0x01
 #define NAND_MFR_MACRONIX	0xc2
+#define NAND_MFR_ESMT		0x92

As Geert said, not sure we have to redefine a new macro, just say that EON and ESMT are the same.

Quoted 2 lines

But if someone expects ESMT, are they supposed to know EON was swallowed by ESMT?

Hm, they'll eventually find out pretty quickly that the ESMT manufacturer id and EON are the same.

Quoted 4 lines
Quoted 2 lines

drivers/mtd/nand/Kconfig struct nand_chip { · line 568

Quoted 2 lines
drivers/mtd/nand/Kconfig +1 -0
+#define NAND_MFR_MIRA		0xc8

What about this one?

This one should be added, yes.

Quoted 8 lines

http://www.deutron.com.tw/miles.htm
2002 MIRA renamed as Deutron focus on DRAM spot market.

http://www.datasheetspdf.com/PDF/PSU8GA30AT/897320/1

Anyway, many many thanks for having taken such a close look at this patch.

No problem.

Best Regards,

Boris

Signature · 3 lines
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com