Skip to contentSkip to conversation

mtd: rawnand: add NVIDIA Tegra NAND flash support

PATCH Superseded Stefan Agner 2 participants 11 messages Jun 17, 2018 – Jun 18, 2018

Subsystems (5)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Stefan Agner

author maintainer
#0 lore raw

v5 cover mtd: rawnand: add NVIDIA Tegra NAND flash support

Changes definitly calm down, most noteably probably the changes around checking whether a page is empty if the stack reports ECC errors.. I verified the code using raw nandwrites with OOB to simulate an empty page which has some bits flipped in the OOB area, everthing seems to work as I would expect it.

For now I do not check extra OOB bytes since those are at variable locations depending on algorithm.

--
Stefan

Changes since v1:
- Split controller and NAND chip structure
- Add BCH support
- Allow to select algorithm and strength using device tree
- Improve HW ECC error reporting and use DEC_STATUS_BUF only
- Use SPDX license identifier
- Use per algorithm mtd_ooblayout_ops
- Use setup_data_interface callback for NAND timing configuration

Changes since v2:
- Set clock rate using assigned-clocks
- Use BIT() macro
- Fix and improve timing calculation
- Improve ECC error handling
- Store OOB layout for tag area in Tegra chip structure
- Update/fix bindings
- Use more specific variable names (replace "value")
- Introduce nand-is-boot-medium
- Choose sensible ECC strenght automatically
- Use wait_for_completion_timeout
- Print register dump on completion timeout
- Unify tegra_nand_(read|write)_page in tegra_nand_page_xfer

Changes since v3:
- Implement tegra_nand_(read|write)_raw using DMA
- Implement tegra_nand_(read|write)_oob using DMA
- Name registers according to Tegra 2 Technical Reference Manual (v02p)
- Use wait_for_completion_io_timeout to account for IO
- Get chip select id from device tree reg property
- Clear interrupts and reinit wait queues in case command/DMA times out
- Set default MTD name after nand_set_flash_node
- Move MODULE_DEVICE_TABLE after declaration of tegra_nand_of_match
- Make (rs|bch)_strength static

Changes since v4:
- Pass OOB area to nand_check_erased_ecc_chunk
- Pass algorithm specific bits_per_step to tegra_nand_get_strength
- Store ECC layout in chip structure
- Fix pointer assignment (use NULL)
- Removed obsolete header delay.h
- Fixed newlines
- Use non-_io variant of wait_for_completion_timeout

Lucas Stach (1):
ARM: dts: tegra: add Tegra20 NAND flash controller node

Stefan Agner (5):
mtd: rawnand: add Reed-Solomon error correction algorithm
mtd: rawnand: add an option to specify NAND chip as a boot device
mtd: rawnand: tegra: add devicetree binding
mtd: rawnand: add NVIDIA Tegra NAND Flash controller driver
ARM: dts: tegra: enable NAND flash on Colibri T20

 .../devicetree/bindings/mtd/nand.txt          |    6 +-
 .../bindings/mtd/nvidia-tegra20-nand.txt      |   64 +
 MAINTAINERS                                   |    7 +
 arch/arm/boot/dts/tegra20-colibri-512.dtsi    |   16 +
 arch/arm/boot/dts/tegra20.dtsi                |   15 +
 drivers/mtd/nand/raw/Kconfig                  |    6 +
 drivers/mtd/nand/raw/Makefile                 |    1 +
 drivers/mtd/nand/raw/nand_base.c              |    4 +
 drivers/mtd/nand/raw/tegra_nand.c             | 1268 +++++++++++++++++
 include/linux/mtd/rawnand.h                   |    7 +
 10 files changed, 1393 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/mtd/nvidia-tegra20-nand.txt
 create mode 100644 drivers/mtd/nand/raw/tegra_nand.c
Signature · 1 line
2.17.1
author maintainer
#1 lore raw

PATCH v5 1/6 mtd: rawnand: add Reed-Solomon error correction algorithm

Add Reed-Solomon (RS) to the enumeration of ECC algorithms.

---
 Documentation/devicetree/bindings/mtd/nand.txt | 2 +-
 drivers/mtd/nand/raw/nand_base.c               | 1 +
 include/linux/mtd/rawnand.h                    | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/mtd/nand.txt +1 -1
index 8bb11d809429d..eaef8c657aa5c 100644
@@ -25,7 +25,7 @@ Optional NAND chip properties:
 		  Deprecated values:
 		  "soft_bch": use "soft" and nand-ecc-algo instead
 - nand-ecc-algo: string, algorithm of NAND ECC.
-		 Supported values are: "hamming", "bch".
+		 Valid values are: "hamming", "bch", "rs".
 - nand-bus-width : 8 or 16 bus width if not present 8
 - nand-on-flash-bbt: boolean to enable on flash bbt option if not present false
 
drivers/mtd/nand/raw/nand_base.c +1 -0
index 10c4f9919850c..47e9cb9063da4 100644
@@ -5777,6 +5777,7 @@ static int of_get_nand_ecc_mode(struct device_node *np)
 static const char * const nand_ecc_algos[] = {
 	[NAND_ECC_HAMMING]	= "hamming",
 	[NAND_ECC_BCH]		= "bch",
+	[NAND_ECC_RS]		= "rs",
 };
 
 static int of_get_nand_ecc_algo(struct device_node *np)
include/linux/mtd/rawnand.h +1 -0
index 3e8ec3b8a39c7..2d9cb7acbc3d8 100644
@@ -121,6 +121,7 @@ enum nand_ecc_algo {
 	NAND_ECC_UNKNOWN,
 	NAND_ECC_HAMMING,
 	NAND_ECC_BCH,
+	NAND_ECC_RS,
 };
 
 /*
Signature · 1 line
2.17.1
author maintainer
#2 lore raw

PATCH v5 2/6 mtd: rawnand: add an option to specify NAND chip as a boot device

In reply to Stefan Agner

Allow to define a NAND chip as a boot device. This can be helpful for the selection of the ECC algorithm and strength in case the boot ROM supports only a subset of controller provided options.

---
 Documentation/devicetree/bindings/mtd/nand.txt | 4 ++++
 drivers/mtd/nand/raw/nand_base.c               | 3 +++
 include/linux/mtd/rawnand.h                    | 6 ++++++
 3 files changed, 13 insertions(+)
Documentation/devicetree/bindings/mtd/nand.txt +4 -0
index eaef8c657aa5c..e949c778e9837 100644
@@ -43,6 +43,10 @@ Optional NAND chip properties:
 		     This is particularly useful when only the in-band area is
 		     used by the upper layers, and you want to make your NAND
 		     as reliable as possible.
+- nand-is-boot-medium: Whether the NAND chip is a boot medium. Drivers might use
+		       this information to select ECC algorithms supported by
+		       the boot ROM or similar restrictions.
+
 - nand-rb: shall contain the native Ready/Busy ids.
 
 The ECC strength and ECC step size properties define the correction capability
drivers/mtd/nand/raw/nand_base.c +3 -0
index 47e9cb9063da4..e70bf328c64e4 100644
@@ -5859,6 +5859,9 @@ static int nand_dt_init(struct nand_chip *chip)
 	if (of_get_nand_bus_width(dn) == 16)
 		chip->options |= NAND_BUSWIDTH_16;
 
+	if (of_property_read_bool(dn, "nand-is-boot-medium"))
+		chip->options |= NAND_IS_BOOT_MEDIUM;
+
 	if (of_get_nand_on_flash_bbt(dn))
 		chip->bbt_options |= NAND_BBT_USE_FLASH;
 
include/linux/mtd/rawnand.h +6 -0
index 2d9cb7acbc3d8..80aeeca03f36b 100644
@@ -219,6 +219,12 @@ enum nand_ecc_algo {
  */
 #define NAND_WAIT_TCCS		0x00200000
 
+/*
+ * Whether the NAND chip is a boot medium. Drivers might use this information
+ * to select ECC algorithms supported by the boot ROM or similar restrictions.
+ */
+#define NAND_IS_BOOT_MEDIUM	0x00400000
+
 /* Options set by nand scan */
 /* Nand scan has allocated controller struct */
 #define NAND_CONTROLLER_ALLOC	0x80000000
Signature · 1 line
2.17.1
author maintainer
#3 lore raw

PATCH v5 3/6 mtd: rawnand: tegra: add devicetree binding

In reply to Stefan Agner

This adds the devicetree binding for the Tegra 2 NAND flash controller.

---
 .../bindings/mtd/nvidia-tegra20-nand.txt      | 64 +++++++++++++++++++
 1 file changed, 64 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mtd/nvidia-tegra20-nand.txt
Documentation/devicetree/bindings/mtd/nvidia-tegra20-nand.txt new file +64 -0
new file mode 100644
index 0000000000000..1c351362f3a96
@@ -0,0 +1,64 @@
+NVIDIA Tegra NAND Flash controller
+
+Required properties:
+- compatible: Must be one of:
+  - "nvidia,tegra20-nand"
+- reg: MMIO address range
+- interrupts: interrupt output of the NFC controller
+- clocks: Must contain an entry for each entry in clock-names.
+  See ../clocks/clock-bindings.txt for details.
+- clock-names: Must include the following entries:
+  - nand
+- resets: Must contain an entry for each entry in reset-names.
+  See ../reset/reset.txt for details.
+- reset-names: Must include the following entries:
+  - nand
+
+Optional children nodes:
+Individual NAND chips are children of the NAND controller node. Currently
+only one NAND chip supported.
+
+Required children node properties:
+- reg: An integer ranging from 1 to 6 representing the CS line to use.
+
+Optional children node properties:
+- nand-ecc-mode: String, operation mode of the NAND ecc mode. Currently only
+		 "hw" is supported.
+- nand-ecc-algo: string, algorithm of NAND ECC.
+		 Supported values with "hw" ECC mode are: "rs", "bch".
+- nand-bus-width : See nand.txt
+- nand-on-flash-bbt: See nand.txt
+- nand-ecc-strength: integer representing the number of bits to correct
+		     per ECC step (always 512). Supported strength using HW ECC
+		     modes are:
+		     - RS: 4, 6, 8
+		     - BCH: 4, 8, 14, 16
+- nand-ecc-maximize: See nand.txt
+- nand-is-boot-medium: Makes sure only ECC strengths supported by the boot ROM
+		       are choosen.
+- wp-gpios: GPIO specifier for the write protect pin.
+
+Optional child node of NAND chip nodes:
+Partitions: see partition.txt
+
+  Example:
+	nand-controller@70008000 {
+		compatible = "nvidia,tegra20-nand";
+		reg = <0x70008000 0x100>;
+		interrupts = <GIC_SPI 24 IRQ_TYPE_LEVEL_HIGH>;
+		clocks = <&tegra_car TEGRA20_CLK_NDFLASH>;
+		clock-names = "nand";
+		resets = <&tegra_car 13>;
+		reset-names = "nand";
+
+		nand@0 {
+			reg = <0>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			nand-bus-width = <8>;
+			nand-on-flash-bbt;
+			nand-ecc-algo = "bch";
+			nand-ecc-strength = <8>;
+			wp-gpios = <&gpio TEGRA_GPIO(S, 0) GPIO_ACTIVE_LOW>;
+		};
+	};
Signature · 1 line
2.17.1
author maintainer
#4 lore raw

PATCH v5 4/6 mtd: rawnand: add NVIDIA Tegra NAND Flash controller driver

In reply to Stefan Agner

Add support for the NAND flash controller found on NVIDIA Tegra 2 SoCs. This implementation does not make use of the command queue feature. Regular operations using ->exec_op() use PIO mode for data transfers. Raw, ECC and OOB read/writes make use of the DMA mode for data transfer.

---
 MAINTAINERS                       |    7 +
 drivers/mtd/nand/raw/Kconfig      |    6 +
 drivers/mtd/nand/raw/Makefile     |    1 +
 drivers/mtd/nand/raw/tegra_nand.c | 1268 +++++++++++++++++++++++++++++
 4 files changed, 1282 insertions(+)
 create mode 100644 drivers/mtd/nand/raw/tegra_nand.c
MAINTAINERS +7 -0
index 9d5eeff51b5fd..d62bf7dc714d2 100644
@@ -14040,6 +14040,13 @@ M:	Laxman Dewangan <[email protected]>
 S:	Supported
 F:	drivers/input/keyboard/tegra-kbc.c
 
+TEGRA NAND DRIVER
+M:	Stefan Agner <[email protected]>
+M:	Lucas Stach <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/mtd/nvidia-tegra20-nand.txt
+F:	drivers/mtd/nand/raw/tegra_nand.c
+
 TEGRA PWM DRIVER
 M:	Thierry Reding <[email protected]>
 S:	Supported
drivers/mtd/nand/raw/Kconfig +6 -0
index 6871ff0fd300b..472a870cfe4b0 100644
@@ -530,4 +530,10 @@ config MTD_NAND_MTK
 	  Enables support for NAND controller on MTK SoCs.
 	  This controller is found on mt27xx, mt81xx, mt65xx SoCs.
 
+config MTD_NAND_TEGRA
+	tristate "Support for NAND controller on NVIDIA Tegra"
+	depends on ARCH_TEGRA || COMPILE_TEST
+	help
+	  Enables support for NAND flash controller on NVIDIA Tegra SoC.
+
 endif # MTD_NAND
drivers/mtd/nand/raw/Makefile +1 -0
index 165b7ef9e9a18..d5a5f9832b887 100644
@@ -56,6 +56,7 @@ obj-$(CONFIG_MTD_NAND_HISI504)	        += hisi504_nand.o
 obj-$(CONFIG_MTD_NAND_BRCMNAND)		+= brcmnand/
 obj-$(CONFIG_MTD_NAND_QCOM)		+= qcom_nandc.o
 obj-$(CONFIG_MTD_NAND_MTK)		+= mtk_ecc.o mtk_nand.o
+obj-$(CONFIG_MTD_NAND_TEGRA)		+= tegra_nand.o
 
 nand-objs := nand_base.o nand_bbt.o nand_timings.o nand_ids.o
 nand-objs += nand_amd.o
drivers/mtd/nand/raw/tegra_nand.c new file +1268 -0

1,268 lines not on this page · raw message

Signature · 1 line
2.17.1
author maintainer
#5 lore raw

PATCH v5 5/6 ARM: dts: tegra: add Tegra20 NAND flash controller node

In reply to Stefan Agner

From: Lucas Stach <[email protected]>

Add basic controller device tree node to be extended by individual boards. Use the assigned-clocks mechanism to set NDFLASH clock to a sensible default rate of 150MHz.

---
 arch/arm/boot/dts/tegra20.dtsi | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
arch/arm/boot/dts/tegra20.dtsi +15 -0
index 983dd5c147945..a75fe7fec7912 100644
@@ -425,6 +425,21 @@
 		status = "disabled";
 	};
 
+	nand-controller@70008000 {
+		compatible = "nvidia,tegra20-nand";
+		reg = <0x70008000 0x100>;
+		#address-cells = <1>;
+		#size-cells = <0>;
+		interrupts = <GIC_SPI 24 IRQ_TYPE_LEVEL_HIGH>;
+		clocks = <&tegra_car TEGRA20_CLK_NDFLASH>;
+		clock-names = "nand";
+		resets = <&tegra_car 13>;
+		reset-names = "nand";
+		assigned-clocks = <&tegra_car TEGRA20_CLK_NDFLASH>;
+		assigned-clock-rates = <150000000>;
+		status = "disabled";
+	};
+
 	pwm: pwm@7000a000 {
 		compatible = "nvidia,tegra20-pwm";
 		reg = <0x7000a000 0x100>;
Signature · 1 line
2.17.1
author maintainer
#6 lore raw

PATCH v5 6/6 ARM: dts: tegra: enable NAND flash on Colibri T20

In reply to Stefan Agner

This enables the on-module ONFI conformant NAND flash.

---
 arch/arm/boot/dts/tegra20-colibri-512.dtsi | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
arch/arm/boot/dts/tegra20-colibri-512.dtsi +16 -0
index 5c202b3e3bb1d..0c283fb772148 100644
@@ -462,6 +462,22 @@
 		};
 	};
 
+	nand-controller@70008000 {
+		status = "okay";
+
+		nand@0 {
+			reg = <0>;
+			#address-cells = <1>;
+			#size-cells = <1>;
+			nand-bus-width = <8>;
+			nand-on-flash-bbt;
+			nand-ecc-algo = "bch";
+			nand-is-boot-medium;
+			nand-ecc-maximize;
+			wp-gpios = <&gpio TEGRA_GPIO(S, 0) GPIO_ACTIVE_LOW>;
+		};
+	};
+
 	usb@c5004000 {
 		status = "okay";
 		nvidia,phy-reset-gpio = <&gpio TEGRA_GPIO(V, 1)
Signature · 1 line
2.17.1
#7 lore raw
In reply to Stefan Agner
Quoted 8 lines · On Sun, 17 Jun 2018 22:45:59 +0200 Stefan Agner <[email protected]> wrote:

Changes definitly calm down, most noteably probably the changes around checking whether a page is empty if the stack reports ECC errors.. I verified the code using raw nandwrites with OOB to simulate an empty page which has some bits flipped in the OOB area, everthing seems to work as I would expect it.

For now I do not check extra OOB bytes since those are at variable locations depending on algorithm.

Hm, if you expose them as free OOB bytes, you should also check them, otherwise you might end up with corrupted data without noticing it. Note that, depending on whether those free OOB bytes are ECC-protected or not, you should change the way you do the check:

- non-protected OOB bytes: all bytes should be 0xff (no bitflips allowed)
- data+free OOB bytes protected by the same ECC bytes: you should pass the free OOB bytes buffer to nand_check_erased_ecc_chunk() along with the data and ECC buffers
- free OOB bytes have their own ECC bytes: call nand_check_erased_ecc_chunk() separately and pass it the ECC + free OOB buffers.

Quoted 71 lines

--
Stefan

Changes since v1:
- Split controller and NAND chip structure
- Add BCH support
- Allow to select algorithm and strength using device tree
- Improve HW ECC error reporting and use DEC_STATUS_BUF only
- Use SPDX license identifier
- Use per algorithm mtd_ooblayout_ops
- Use setup_data_interface callback for NAND timing configuration

Changes since v2:
- Set clock rate using assigned-clocks
- Use BIT() macro
- Fix and improve timing calculation
- Improve ECC error handling
- Store OOB layout for tag area in Tegra chip structure
- Update/fix bindings
- Use more specific variable names (replace "value")
- Introduce nand-is-boot-medium
- Choose sensible ECC strenght automatically
- Use wait_for_completion_timeout
- Print register dump on completion timeout
- Unify tegra_nand_(read|write)_page in tegra_nand_page_xfer

Changes since v3:
- Implement tegra_nand_(read|write)_raw using DMA
- Implement tegra_nand_(read|write)_oob using DMA
- Name registers according to Tegra 2 Technical Reference Manual (v02p)
- Use wait_for_completion_io_timeout to account for IO
- Get chip select id from device tree reg property
- Clear interrupts and reinit wait queues in case command/DMA times out
- Set default MTD name after nand_set_flash_node
- Move MODULE_DEVICE_TABLE after declaration of tegra_nand_of_match
- Make (rs|bch)_strength static

Changes since v4:
- Pass OOB area to nand_check_erased_ecc_chunk
- Pass algorithm specific bits_per_step to tegra_nand_get_strength
- Store ECC layout in chip structure
- Fix pointer assignment (use NULL)
- Removed obsolete header delay.h
- Fixed newlines
- Use non-_io variant of wait_for_completion_timeout

Lucas Stach (1):
ARM: dts: tegra: add Tegra20 NAND flash controller node

Stefan Agner (5):
mtd: rawnand: add Reed-Solomon error correction algorithm
mtd: rawnand: add an option to specify NAND chip as a boot device
mtd: rawnand: tegra: add devicetree binding
mtd: rawnand: add NVIDIA Tegra NAND Flash controller driver
ARM: dts: tegra: enable NAND flash on Colibri T20

 .../devicetree/bindings/mtd/nand.txt          |    6 +-
 .../bindings/mtd/nvidia-tegra20-nand.txt      |   64 +
 MAINTAINERS                                   |    7 +
 arch/arm/boot/dts/tegra20-colibri-512.dtsi    |   16 +
 arch/arm/boot/dts/tegra20.dtsi                |   15 +
 drivers/mtd/nand/raw/Kconfig                  |    6 +
 drivers/mtd/nand/raw/Makefile                 |    1 +
 drivers/mtd/nand/raw/nand_base.c              |    4 +
 drivers/mtd/nand/raw/tegra_nand.c             | 1268 +++++++++++++++++
 include/linux/mtd/rawnand.h                   |    7 +
 10 files changed, 1393 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/mtd/nvidia-tegra20-nand.txt
 create mode 100644 drivers/mtd/nand/raw/tegra_nand.c
author maintainer
#8 lore raw
Quoted 17 lines · Boris Brezillon
Quoted 8 lines · On Sun, 17 Jun 2018 22:45:59 +0200 Stefan Agner <[email protected]> wrote:

Changes definitly calm down, most noteably probably the changes around checking whether a page is empty if the stack reports ECC errors.. I verified the code using raw nandwrites with OOB to simulate an empty page which has some bits flipped in the OOB area, everthing seems to work as I would expect it.

For now I do not check extra OOB bytes since those are at variable locations depending on algorithm.

Hm, if you expose them as free OOB bytes, you should also check them, otherwise you might end up with corrupted data without noticing it. Note that, depending on whether those free OOB bytes are ECC-protected or not, you should change the way you do the check:

Quoted 8 lines

- non-protected OOB bytes: all bytes should be 0xff (no bitflips allowed)
- data+free OOB bytes protected by the same ECC bytes: you should pass the free OOB bytes buffer to nand_check_erased_ecc_chunk() along with the data and ECC buffers
- free OOB bytes have their own ECC bytes: call nand_check_erased_ecc_chunk() separately and pass it the ECC + free OOB buffers.

This graphic taken from the public Tegra 2 Technical Reference Manual is quite useful:
https://imgur.com/a/0Hqzbkc

Tegra basically has all of the above, which makes the whole business really tricky...

I am not sure if we really could do variant 1, non-protected OOB, but since we have the option of protected OOB, we probably anyway would do that.

RS/Hamming implements variant 3.

BCH implements variant 2. OOB is protected with the last data buffer.

So this would require a algorithm depending implementation, which is probably not a big deal.

But there is one more issue with BCH: Only if extra data are actually transferred, tag space is actually allocated. If no tag bytes are transferred, parity follows immediately skip bytes. As far as I know the MTD stacks OOB layout assumes that is always the same layout, no matter whether we write extra OOB data or not. For the Tegra NAND controller this would mean that we have to always transfer tag bytes and therefor penalize the use case we are most interested in (which is no extra OOB bytes, since UBI does not make use of it)...

Furthermore I realized that testing is not easily possible since nandwrite with --oob seems not to make use of "oob_required" in the main page write but issues a separate OOB write command. I did not found a way to issue a write from user space which sets oob_required...

Due to all this I rather prefer to not implement extra OOB support at this point.

How do I do this properly? Set mtd_ooblayout_ops.free to NULL?

--
Stefan

Quoted 72 lines
Quoted 72 lines

--
Stefan

Changes since v1:
- Split controller and NAND chip structure
- Add BCH support
- Allow to select algorithm and strength using device tree
- Improve HW ECC error reporting and use DEC_STATUS_BUF only
- Use SPDX license identifier
- Use per algorithm mtd_ooblayout_ops
- Use setup_data_interface callback for NAND timing configuration

Changes since v2:
- Set clock rate using assigned-clocks
- Use BIT() macro
- Fix and improve timing calculation
- Improve ECC error handling
- Store OOB layout for tag area in Tegra chip structure
- Update/fix bindings
- Use more specific variable names (replace "value")
- Introduce nand-is-boot-medium
- Choose sensible ECC strenght automatically
- Use wait_for_completion_timeout
- Print register dump on completion timeout
- Unify tegra_nand_(read|write)_page in tegra_nand_page_xfer

Changes since v3:
- Implement tegra_nand_(read|write)_raw using DMA
- Implement tegra_nand_(read|write)_oob using DMA
- Name registers according to Tegra 2 Technical Reference Manual (v02p)
- Use wait_for_completion_io_timeout to account for IO
- Get chip select id from device tree reg property
- Clear interrupts and reinit wait queues in case command/DMA times out
- Set default MTD name after nand_set_flash_node
- Move MODULE_DEVICE_TABLE after declaration of tegra_nand_of_match
- Make (rs|bch)_strength static

Changes since v4:
- Pass OOB area to nand_check_erased_ecc_chunk
- Pass algorithm specific bits_per_step to tegra_nand_get_strength
- Store ECC layout in chip structure
- Fix pointer assignment (use NULL)
- Removed obsolete header delay.h
- Fixed newlines
- Use non-_io variant of wait_for_completion_timeout

Lucas Stach (1):
ARM: dts: tegra: add Tegra20 NAND flash controller node

Stefan Agner (5):
mtd: rawnand: add Reed-Solomon error correction algorithm
mtd: rawnand: add an option to specify NAND chip as a boot device
mtd: rawnand: tegra: add devicetree binding
mtd: rawnand: add NVIDIA Tegra NAND Flash controller driver
ARM: dts: tegra: enable NAND flash on Colibri T20

 .../devicetree/bindings/mtd/nand.txt          |    6 +-
 .../bindings/mtd/nvidia-tegra20-nand.txt      |   64 +
 MAINTAINERS                                   |    7 +
 arch/arm/boot/dts/tegra20-colibri-512.dtsi    |   16 +
 arch/arm/boot/dts/tegra20.dtsi                |   15 +
 drivers/mtd/nand/raw/Kconfig                  |    6 +
 drivers/mtd/nand/raw/Makefile                 |    1 +
 drivers/mtd/nand/raw/nand_base.c              |    4 +
 drivers/mtd/nand/raw/tegra_nand.c             | 1268 +++++++++++++++++
 include/linux/mtd/rawnand.h                   |    7 +
 10 files changed, 1393 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/devicetree/bindings/mtd/nvidia-tegra20-nand.txt
 create mode 100644 drivers/mtd/nand/raw/tegra_nand.c
#9 lore raw

Hi Stefan,

Quoted 27 lines · On Mon, 18 Jun 2018 12:51:52 +0200 Stefan Agner <[email protected]> wrote:
Quoted 17 lines · Boris Brezillon
Quoted 8 lines · On Sun, 17 Jun 2018 22:45:59 +0200 Stefan Agner <[email protected]> wrote:

Changes definitly calm down, most noteably probably the changes around checking whether a page is empty if the stack reports ECC errors.. I verified the code using raw nandwrites with OOB to simulate an empty page which has some bits flipped in the OOB area, everthing seems to work as I would expect it.

For now I do not check extra OOB bytes since those are at variable locations depending on algorithm.

Hm, if you expose them as free OOB bytes, you should also check them, otherwise you might end up with corrupted data without noticing it. Note that, depending on whether those free OOB bytes are ECC-protected or not, you should change the way you do the check:

Quoted 8 lines

- non-protected OOB bytes: all bytes should be 0xff (no bitflips allowed)
- data+free OOB bytes protected by the same ECC bytes: you should pass the free OOB bytes buffer to nand_check_erased_ecc_chunk() along with the data and ECC buffers
- free OOB bytes have their own ECC bytes: call nand_check_erased_ecc_chunk() separately and pass it the ECC + free OOB buffers.

Quoted 3 lines

This graphic taken from the public Tegra 2 Technical Reference Manual is quite useful:
https://imgur.com/a/0Hqzbkc

Thanks for sharing this doc.

Quoted 3 lines

Tegra basically has all of the above, which makes the whole business really tricky...

I'm not sure. Are "Skip bytes" protected by "main data parity bytes"?

AFAICT, you have "Tag bytes" that fall in case #3 and "Remaining spare bytes" that fall in case #1. If "Skip bytes" are protected by the "main data parity bytes", then it falls in case #2, otherwise it probably goes in case #1.

Quoted 4 lines

I am not sure if we really could do variant 1, non-protected OOB, but since we have the option of protected OOB, we probably anyway would do that.

That's up to you, but in this case, you should not declare those bytes as free (didn't check what is currently done in the driver).

Quoted 2 lines

RS/Hamming implements variant 3.

It seems to be a mix of #1 and #3, but I'm not sure (see above).

Quoted 2 lines

BCH implements variant 2.

I'd say it's a mix of #1 (skip + remaining bytes) and #2(tag bytes).

Quoted 1 line

OOB is protected with the last data buffer.

That would be weird, but maybe you're right. HW ECC engine usually split the OOB area in X portions, X being the number of ECC steps needed to cover a NAND page, and then have ECC bytes cover a sub-portion of data+OOB.

For example, for a NAND page of 2k with 64 bytes of OOB, and assuming the ECC step is 512bytes, you usually have something like:

[512(data)+8(protected-oob)+8(ecc)] x 4

Quoted 3 lines

So this would require a algorithm depending implementation, which is probably not a big deal.

True.

Quoted 9 lines

But there is one more issue with BCH: Only if extra data are actually transferred, tag space is actually allocated. If no tag bytes are transferred, parity follows immediately skip bytes. As far as I know the MTD stacks OOB layout assumes that is always the same layout, no matter whether we write extra OOB data or not. For the Tegra NAND controller this would mean that we have to always transfer tag bytes and therefor penalize the use case we are most interested in (which is no extra OOB bytes, since UBI does not make use of it)...

Hm, given the amount of tag bytes I don't think you'll have a huge penalty, so I'd recommend always sending those bytes. Alternatively, you could decide that you never want to have those tag bytes and expose none of them.

Quoted 5 lines

Furthermore I realized that testing is not easily possible since nandwrite with --oob seems not to make use of "oob_required" in the main page write but issues a separate OOB write command. I did not found a way to issue a write from user space which sets oob_required...

Maybe it's time to patch those tools. The ioctl exists, so it's just a matter of using it in nandwrite/mtd-utils.

Quoted 3 lines

Due to all this I rather prefer to not implement extra OOB support at this point.

I'm fine with that, but that means no JFFS2 support, as I think JFFS2 wants to place some of its metadata in the OOB area. Also, I fear it will be a mess to add support for that kind of things without breaking existing setup afterwards, so, by taking this decision you're pretty much saying that this controller will never expose free OOB bytes. That's not a problem from my PoV, but I want you to be aware of that.

Quoted 2 lines

How do I do this properly? Set mtd_ooblayout_ops.free to NULL?

Just implement a dummy function that returns -ERANGE.

Regards,

Boris

author maintainer
#10 lore raw

Hi Boris,

Quoted 42 lines · Boris Brezillon

Hi Stefan,

Quoted 27 lines · On Mon, 18 Jun 2018 12:51:52 +0200 Stefan Agner <[email protected]> wrote:
Quoted 17 lines · Boris Brezillon
Quoted 8 lines · On Sun, 17 Jun 2018 22:45:59 +0200 Stefan Agner <[email protected]> wrote:

Changes definitly calm down, most noteably probably the changes around checking whether a page is empty if the stack reports ECC errors.. I verified the code using raw nandwrites with OOB to simulate an empty page which has some bits flipped in the OOB area, everthing seems to work as I would expect it.

For now I do not check extra OOB bytes since those are at variable locations depending on algorithm.

Hm, if you expose them as free OOB bytes, you should also check them, otherwise you might end up with corrupted data without noticing it. Note that, depending on whether those free OOB bytes are ECC-protected or not, you should change the way you do the check:

Quoted 8 lines

- non-protected OOB bytes: all bytes should be 0xff (no bitflips allowed)
- data+free OOB bytes protected by the same ECC bytes: you should pass the free OOB bytes buffer to nand_check_erased_ecc_chunk() along with the data and ECC buffers
- free OOB bytes have their own ECC bytes: call nand_check_erased_ecc_chunk() separately and pass it the ECC + free OOB buffers.

Quoted 3 lines

This graphic taken from the public Tegra 2 Technical Reference Manual is quite useful:
https://imgur.com/a/0Hqzbkc

Thanks for sharing this doc.

Quoted 3 lines

Tegra basically has all of the above, which makes the whole business really tricky...

Quoted 7 lines

I'm not sure. Are "Skip bytes" protected by "main data parity bytes"?

AFAICT, you have "Tag bytes" that fall in case #3 and "Remaining spare bytes" that fall in case #1. If "Skip bytes" are protected by the "main data parity bytes", then it falls in case #2, otherwise it probably goes in case #1.

Skip bytes are not protected. I think they are mainly meant to skip the bad block marker. Only 4, 8, 12 or 16 bytes are supported.

Quoted 25 lines
Quoted 4 lines

I am not sure if we really could do variant 1, non-protected OOB, but since we have the option of protected OOB, we probably anyway would do that.

That's up to you, but in this case, you should not declare those bytes as free (didn't check what is currently done in the driver).

Quoted 2 lines

RS/Hamming implements variant 3.

It seems to be a mix of #1 and #3, but I'm not sure (see above).

Quoted 2 lines

BCH implements variant 2.

I'd say it's a mix of #1 (skip + remaining bytes) and #2(tag bytes).

Quoted 1 line

OOB is protected with the last data buffer.

That would be weird, but maybe you're right. HW ECC engine usually split the OOB area in X portions, X being the number of ECC steps needed to cover a NAND page, and then have ECC bytes cover a sub-portion of data+OOB.

Quoted 5 lines

For example, for a NAND page of 2k with 64 bytes of OOB, and assuming the ECC step is 512bytes, you usually have something like:

[512(data)+8(protected-oob)+8(ecc)] x 4

The TRM explicitly states so:

"BCH mode Error correction
- Error correction with involving spare only transfers is not supported. ECC calculation of last sub-page includes tag data in spare area.
- Error correction with Main only transfers is supported.
- Maximum possible length of Tag data size is 252 bytes."

Quoted 34 lines
Quoted 3 lines

So this would require a algorithm depending implementation, which is probably not a big deal.

True.

Quoted 9 lines

But there is one more issue with BCH: Only if extra data are actually transferred, tag space is actually allocated. If no tag bytes are transferred, parity follows immediately skip bytes. As far as I know the MTD stacks OOB layout assumes that is always the same layout, no matter whether we write extra OOB data or not. For the Tegra NAND controller this would mean that we have to always transfer tag bytes and therefor penalize the use case we are most interested in (which is no extra OOB bytes, since UBI does not make use of it)...

Hm, given the amount of tag bytes I don't think you'll have a huge penalty, so I'd recommend always sending those bytes. Alternatively, you could decide that you never want to have those tag bytes and expose none of them.

Quoted 5 lines

Furthermore I realized that testing is not easily possible since nandwrite with --oob seems not to make use of "oob_required" in the main page write but issues a separate OOB write command. I did not found a way to issue a write from user space which sets oob_required...

Maybe it's time to patch those tools. The ioctl exists, so it's just a matter of using it in nandwrite/mtd-utils.

Quoted 3 lines

Due to all this I rather prefer to not implement extra OOB support at this point.

Quoted 7 lines

I'm fine with that, but that means no JFFS2 support, as I think JFFS2 wants to place some of its metadata in the OOB area. Also, I fear it will be a mess to add support for that kind of things without breaking existing setup afterwards, so, by taking this decision you're pretty much saying that this controller will never expose free OOB bytes. That's not a problem from my PoV, but I want you to be aware of that.

We already operate without extra OOB byte support in our downstream BSP. I'd rather have a easy upgrade path today...

Another issue I just realized: The boot ROM only supports BCH without tag bytes... So at least the boot loader has to be written without tag bytes.

Quoted 5 lines
Quoted 2 lines

How do I do this properly? Set mtd_ooblayout_ops.free to NULL?

Just implement a dummy function that returns -ERANGE.

Ok, I will go with this then.

Also, thanks for all your valuable feedback, really appreciated!

--
Stefan