i.MX6: nand: add nandbcb update command
Subsystems (5)
PATCH v2 1/2 i.MX6: nand: add nandbcb update command
Writing/updating boot image in nand device is not straight forward in i.MX6 platform and it requires boot control block(BCB) to be configured.
It becomes difficult to use uboot 'nand' command to write BCB since it requires platform specific attributes need to be taken care of.
It is even difficult to use existing msx-nand.c driver by incorporating BCB attributes like mxs_dma_desc does because it requires change in mtd and nand command.
So, cmd_nandbcb implemented in arch/arm/mach-imx
BCB contains two data structures, Firmware Configuration Block(FCB) and Discovered Bad Block Table(DBBT). FCB has nand timings, DBBT search area, page address of primary and secondary firmware.
On summary, nandbcb update will
- erase the entire partition
- create BCB by creating 4 FCB/BDDT block followed by 2 FW blocks based on partition size and erasesize.
- fill FCB/DBBT structures
- write FW/SPL in FW0 and FW1(same image in two times)
- write FCB/DBBT in first 4 blocks
for nand boot, up on reset bootrom look for FCB structure in first block's if FCB found the nand timings are loaded for further reads. once FCB read done, DTTB will load and finally primary or secondary firmware will load which is boot image.
See Section 4 from doc/README.imx6 for more usage information.
---
Changes for v2:
- Fixed commit message notes
- Updated proper commit message
- Update doc/README.imx6 with NAND boot details
- Fixed long length variable names.
- Fixed Gigantic variable name.
- NULL checks for kzalloc
- Move Kconfig option in separate patch
- Fixed checkpatch warninigs
arch/arm/include/asm/mach-imx/imx-nandbcb.h | 107 ++++++++ arch/arm/include/asm/mach-imx/mxs-nand.h | 15 ++ arch/arm/mach-imx/Makefile | 1 + arch/arm/mach-imx/cmd_nandbcb.c | 371 ++++++++++++++++++++++++++++ doc/README.imx6 | 77 ++++++ drivers/mtd/nand/mxs_nand.c | 8 +- 6 files changed, 575 insertions(+), 4 deletions(-) create mode 100644 arch/arm/include/asm/mach-imx/imx-nandbcb.h create mode 100644 arch/arm/include/asm/mach-imx/mxs-nand.h create mode 100644 arch/arm/mach-imx/cmd_nandbcb.c
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +107 -0
@@ -0,0 +1,107 @@ +/* +* Copyright (C) 2017 Jagan Teki <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0+ +*/ + +#ifndef _IMX_NAND_BCB_H_ +#define _IMX_NAND_BCB_H_ + +#define FCB_FINGERPRINT 0x20424346/* 'FCB' */ +#define FCB_VERSION_1 0x01000000 + +#define DBBT_FINGERPRINT2 0x54424244/* 'DBBT' */ +#define DBBT_VERSION_1 0x01000000 + +struct dbbt_block { +u32 checksum;/* reserved on i.MX6 */ +u32 fingerprint; +u32 version; +u32 numberbb;/* reserved on i.MX6 */ +u32 dbbtnumofpages; +}; + +struct fcb_block { +u32 checksum;/* First fingerprint in first byte */ +u32 fingerprint;/* 2nd fingerprint at byte 4 */ +u32 version;/* 3rd fingerprint at byte 8 */ +u8 datasetup; +u8 datahold; +u8 addresssetup; +u8 dsample_time; + +/* These are for application use only and not for ROM. */ +u8 nandtiming; +u8 rea; +u8 rloh; +u8 rhoh; +u32 pagesize;/* 2048 for 2K pages, 4096 for 4K pages */ +u32 oob_pagesize;/* 2112 for 2K pages, 4314 for 4K pages */ +u32 sectors;/* Number of 2K sections per block */ +u32 nr_nand;/* Total Number of NANDs - not used by ROM */ +u32 nr_die;/* Number of separate chips in this NAND */ +u32 celltype;/* MLC or SLC */ +u32 ecc_type;/* Type of ECC, can be one of BCH-0-20 */ +u32 ecc_nr;/* Number of bytes for Block0 - BCH */ + +/* Block size in bytes for all blocks other than Block0 - BCH */ +u32 ecc_size; +u32 ecc_level;/* Ecc level for Block 0 - BCH */ +u32 meta_size;/* Metadata size - BCH */ +/* Number of blocks per page for ROM use - BCH */ +u32 nr_blocks; +u32 ecc_type_sdk;/* Type of ECC, can be one of BCH-0-20 */ +u32 ecc_nr_sdk;/* Number of bytes for Block0 - BCH */ +/* Block size in bytes for all blocks other than Block0 - BCH */ +u32 ecc_size_sdk; +u32 ecc_level_sdk;/* Ecc level for Block 0 - BCH */ +/* Number of blocks per page for SDK use - BCH */ +u32 nr_blocks_sdk; +u32 meta_size_sdk;/* Metadata size - BCH */ +u32 erase_th;/* To set into BCH_MODE register */ + +/* 0: normal boot +* 1: to load patch starting next to FCB +*/ +u32 bootpatch; +u32 patch_size;/* Size of patch in sectors */ +u32 fw1_start;/* Firmware image starts on this sector */ +u32 fw2_start;/* Secondary FW Image starting Sector */ +u32 fw1_pages;/* Number of sectors in firmware image */ +u32 fw2_pages;/* Number of sector in secondary FW image */ +u32 dbbt_start;/* Page address where dbbt search area begins */ + +/* Byte in page data that have manufacturer marked bad block marker, +* this will be swapped with metadata[0] to complete page data. +*/ +u32 bb_byte; + +/* For BCH ECC sizes other than 8 and 16 the bad block marker does not +* start at 0th bit of bb_byte. This field is used to get to +* the start bit of bad block marker byte with in bb_byte +*/ +u32 bb_start_bit; + +/* FCB value that gives byte offset for +* bad block marker on physical NAND page +*/ +u32 phy_offset; +u32 bchtype; + +u32 readlatency; +u32 preambledelay; +u32 cedelay; +u32 postambledelay; +u32 cmdaddpause; +u32 datapause; +u32 tmspeed; +u32 busytimeout; + +/* the flag to enable (1)/disable(0) bi swap */ +u32 disbbm; + +/* The swap position of main area in spare area */ +u32 spare_offset; +}; + +#endif /* _IMX_NAND_BCB_H_ */
arch/arm/include/asm/mach-imx/mxs-nand.h new file +15 -0
@@ -0,0 +1,15 @@ +/* +* Copyright (C) 2017 Jagan Teki <[email protected]> +* Copyright (C) 2016 Sergey Kubushyn <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0+ +*/ + +#ifndef _MXS_NAND_ +#define _MXS_NAND_ + +uint32_t mxs_nand_get_ecc_strength (u32 page_data_size,uint32_t page_oob_size); +uint32_t mxs_nand_mark_byte_offset (struct mtd_info *mtd); +uint32_t mxs_nand_mark_bit_offset (struct mtd_info *mtd); + +#endif /* _MXS_NAND_ */
arch/arm/mach-imx/Makefile +1 -0
@@ -56,6 +56,7 @@ ifneq ($(CONFIG_SPL_BUILD),y) obj-$(CONFIG_CMD_BMODE) += cmd_bmode.o obj-$(CONFIG_CMD_HDMIDETECT) += cmd_hdmidet.o obj-$(CONFIG_CMD_DEKBLOB) += cmd_dek.o +obj-$(CONFIG_CMD_NANDBCB) += cmd_nandbcb.o endif PLUGIN = board/$(BOARDDIR)/plugin
arch/arm/mach-imx/cmd_nandbcb.c new file +371 -0
371 lines not on this page · raw message
doc/README.imx6 +77 -0
80 lines not on this page · raw message
drivers/mtd/nand/mxs_nand.c +4 -4
@@ -26,6 +26,7 @@#include <asm/mach-imx/regs-gpmi.h> #include <asm/arch/sys_proto.h> #include <asm/mach-imx/dma.h> +#include <asm/mach-imx/mxs-nand.h> #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
@@ -150,8 +151,7 @@ static uint32_t mxs_nand_aux_status_offset(void)return (MXS_NAND_METADATA_SIZE +0x3 ) & ~0x3 ; } -static inline uint32_t mxs_nand_get_ecc_strength (uint32_t page_data_size, -uint32_t page_oob_size) +uint32_t mxs_nand_get_ecc_strength (u32 page_data_size,uint32_t page_oob_size) {int ecc_strength;int max_ecc_strength_supported;
@@ -226,14 +226,14 @@ static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,return block_mark_bit_offset; } -static uint32_t mxs_nand_mark_byte_offset (struct mtd_info *mtd) +uint32_t mxs_nand_mark_byte_offset (struct mtd_info *mtd) {uint32_t ecc_strength; ecc_strength =mxs_nand_get_ecc_strength (mtd->writesize, mtd->oobsize);return mxs_nand_get_mark_offset (mtd->writesize, ecc_strength) >>3 ; } -static uint32_t mxs_nand_mark_bit_offset (struct mtd_info *mtd) +uint32_t mxs_nand_mark_bit_offset (struct mtd_info *mtd) {uint32_t ecc_strength; ecc_strength =mxs_nand_get_ecc_strength (mtd->writesize, mtd->oobsize);
Signature · 1 line
2.7.4
PATCH v2 2/2 arm: i.MX: Add CMD_NANDBCB Kconfig entry
Add Kconfig entry for CMD_NANDBCB, and default y on i.MX6 platform with NAND_MXS defined.
---
Changes for v2:
- New patch
arch/arm/mach-imx/Kconfig | 11 +++++++++++ 1 file changed, 11 insertions(+)
arch/arm/mach-imx/Kconfig +11 -0
@@ -71,6 +71,17 @@ config CMD_HDMIDETECT This enables the 'hdmidet' command which detects if an HDMI monitor is connected. +config CMD_NANDBCB + bool "i.MX6 NAND Boot Control Block(BCB) command" + depends on NAND && CMD_MTDPARTS + default y if ARCH_MX6 && NAND_MXS + help + Unlike normal 'nand write/erase' commands, this command update + Boot Control Block(BCB) for i.MX6 platform NAND IP's. + + This is similar to kobs-ng, which is used in Linux as seprate + rootfs package. + config NXP_BOARD_REVISION bool "Read NXP board revision from fuses" depends on ARCH_MX6 || ARCH_MX7
Signature · 1 line
2.7.4
PATCH v2 2/2 arm: i.MX: Add CMD_NANDBCB Kconfig entry
Quoted 18 lines · Jagan Teki
Add
Kconfigentry forCMD_NANDBCB, and default y on i.MX6 platform withNAND_MXSdefined.Signed-off-by Jagan Teki <[email protected]>---
Changes for v2:
- New patch
arch/arm/mach-imx/Kconfig| 11 +++++++++++1 file changed, 11 insertions(+)arch/arm/mach-imx/Kconfig +0
-0@@ -71,6 +71,17 @@ config CMD_HDMIDETECT This enables the 'hdmidet' command which detects if an HDMI monitor is connected.
arch/arm/mach-imx/Kconfig config CMD_HDMIDETECT · line 82
Quoted 10 lines
arch/arm/mach-imx/Kconfig +9
-0+config CMD_NANDBCB + bool "i.MX6 NAND Boot Control Block(BCB) command" + depends on NAND && CMD_MTDPARTS + default y if ARCH_MX6 && NAND_MXS + help + Unlike normal 'nand write/erase' commands, this command update + Boot Control Block(BCB) for i.MX6 platform NAND IP's. + + This is similar to kobs-ng, which is used in Linux as seprate
^^^^^^^ Typo here
Quoted 7 lines
arch/arm/mach-imx/Kconfig +2
-0+ rootfs package. +config
NXP_BOARD_REVISIONbool "Read NXP board revision from fuses" depends on ARCH_MX6 || ARCH_MX7Signature · 1 line
2.7.4
---
******************************************************************
* KSI at home KOI8 Net < > The impossible we do immediately. *
* Las Vegas NV, USA < > Miracles require 24-hour notice. *
******************************************************************
Fabio Estevam
reviewerPATCH v2 1/2 i.MX6: nand: add nandbcb update command
In reply to Jagan TekiHi Jagan,
Quoted 10 lines · Jagan Teki
---
Changes for v2:
- Fixed commit message notes
- Updated proper commit message
- Update doc/README.imx6 with NAND boot details
- Fixed long length variable names.
- Fixed Gigantic variable name.
- NULL checks for kzalloc
- MoveKconfigoption in separate patch
- Fixed checkpatch warninigs
Thanks for reworking this patch. It looks much better!
Quoted 73 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h | 107 ++++++++ arch/arm/include/asm/mach-imx/mxs-nand.h | 15 ++ arch/arm/mach-imx/Makefile | 1 + arch/arm/mach-imx/cmd_nandbcb.c | 371 ++++++++++++++++++++++++++++ doc/README.imx6 | 77 ++++++ drivers/mtd/nand/mxs_nand.c | 8 +- 6 files changed, 575 insertions(+), 4 deletions(-) create mode 100644 arch/arm/include/asm/mach-imx/imx-nandbcb.h create mode 100644 arch/arm/include/asm/mach-imx/mxs-nand.h create mode 100644 arch/arm/mach-imx/cmd_nandbcb.carch/arm/include/asm/mach-imx/imx-nandbcb.h new file +55
-0@@ -0,0 +1,107 @@ +/* +* Copyright (C) 2017 Jagan Teki <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0+ +*/ + +#ifndef _IMX_NAND_BCB_H_ +#define _IMX_NAND_BCB_H_ + +#define FCB_FINGERPRINT 0x20424346 /* 'FCB' */ +#define FCB_VERSION_1 0x01000000 + +#define DBBT_FINGERPRINT2 0x54424244 /* 'DBBT' */ +#define DBBT_VERSION_1 0x01000000 + +struct dbbt_block { + u32 checksum; /* reserved on i.MX6 */ + u32 fingerprint; + u32 version; + u32 numberbb; /* reserved on i.MX6 */ + u32 dbbtnumofpages; +}; + +struct fcb_block { + u32 checksum; /* First fingerprint in first byte */ + u32 fingerprint; /* 2nd fingerprint at byte 4 */ + u32 version; /* 3rd fingerprint at byte 8 */ + u8 datasetup; + u8 datahold; + u8 addresssetup; + u8 dsample_time; + + /* These are for application use only and not for ROM. */ + u8 nandtiming; + u8 rea; + u8 rloh; + u8 rhoh; + u32 pagesize; /* 2048 for 2K pages, 4096 for 4K pages */ + u32 oob_pagesize; /* 2112 for 2K pages, 4314 for 4K pages */ + u32 sectors; /* Number of 2K sections per block */ + u32 nr_nand; /* Total Number of NANDs - not used by ROM */ + u32 nr_die; /* Number of separate chips in this NAND */ + u32 celltype; /* MLC or SLC */ + u32 ecc_type; /* Type of ECC, can be one of BCH-0-20 */ + u32 ecc_nr; /* Number of bytes for Block0 - BCH */ + + /* Block size in bytes for all blocks other than Block0 - BCH */ + u32 ecc_size; + u32 ecc_level; /* Ecc level for Block 0 - BCH */ + u32 meta_size; /* Metadata size - BCH */ + /* Number of blocks per page for ROM use - BCH */ + u32 nr_blocks; + u32 ecc_type_sdk; /* Type of ECC, can be one of BCH-0-20 */ + u32 ecc_nr_sdk; /* Number of bytes for Block0 - BCH */ + /* Block size in bytes for all blocks other than Block0 - BCH */
arch/arm/include/asm/mach-imx/imx-nandbcb.h line 65
Quoted 10 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +10
-0+u32 ecc_size_sdk; +u32 ecc_level_sdk;/* Ecc level for Block 0 - BCH */ +/* Number of blocks per page for SDK use - BCH */ +u32 nr_blocks_sdk; +u32 meta_size_sdk;/* Metadata size - BCH */ +u32 erase_th;/* To set into BCH_MODE register */ + +/* 0: normal boot +* 1: to load patch starting next to FCB +*/
Multi-line style is wrong here.
Quoted 9 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +9
-0+static int nandbcb_update (struct mtd_info *mtd,loff_t off,size_t size, +size_t maxsize,const u_char *buf) +{ +nand_erase_options_t opts; +struct fcb_block *fcb; +struct dbbt_block *dbbt; +loff_t fw1_off, fw2_off; +void *fwbuf, *fcb_raw_page, *dbbt_page, *dbbt_data_page; +int nr_blks, nr_blks_fcb, nr_blks_fw, fw1_blk, fw2_blk;
arch/arm/include/asm/mach-imx/imx-nandbcb.h line 84
Quoted 10 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +10
-0+size_t fwsize, dummy; +int i, ret; + +/* erase */ +memset (&opts,0 ,sizeof (opts)); + opts.offset = off; + opts.length = maxsize -1 ; + ret =nand_erase_opts (mtd, &opts); +if (ret) { +printf ("%s: erase failed\n" , __func__);
You could also show the 'ret' value for helping debugging.
Quoted 26 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +26
-0+return ret; + } + +/* +* Reference documentation from i.MX6DQRM section 8.5.2.2 +* +* Nand Boot Control Block(BCB) contains two data structures, +* - Firmware Configuration Block(FCB) +* - Discovered Bad Block Table(DBBT) +* +* FCB contains, +* - nand timings +* - DBBT search page address, +* - start page address of primary firmware +* - start page address of secondary firmware +* +* setup fcb: +* - number of blocks = mtd partition size / mtd erasesize +* - two firmware blocks, primary and secondary +* - first 4 block for FCB/DBBT +* - rest split in half for primary and secondary firmware +* - same firmware will write two times +*/ + nr_blks_fcb =4 ; + nr_blks = maxsize / mtd->erasesize; + nr_blks_fw = (nr_blks - nr_blks_fcb) /2 ;
arch/arm/include/asm/mach-imx/imx-nandbcb.h
Quoted 10 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +10
-0+ fw1_blk = nr_blks_fcb; + fw2_blk = fw1_blk + nr_blks_fw; + +/* write fw */ + fwsize =ALIGN (size +FLASH_OFFSET_STANDARD + mtd->writesize, + mtd->writesize); + fwbuf =kzalloc (fwsize,GFP_KERNEL ); +if (!fwbuf) { +debug ("failed to allocate fwbuf\n" ); +return -EFAULT ;
We usually return -ENOMEM when kzalloc() fails.
Quoted 15 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +15
-0+ } + +memcpy (fwbuf +FLASH_OFFSET_STANDARD , buf, size); + fw1_off = fw1_blk * mtd->erasesize; + ret =nand_write_skip_bad (mtd, fw1_off, &fwsize,NULL , maxsize, + (u_char *)fwbuf,WITH_WR_VERIFY ); +printf ("NAND fw write: 0x%llx offset, 0x%x bytes written: %s\n" , + fw1_off, fwsize, ret ?"ERROR" :"OK" ); +if (ret) + goto fw_err; + +/* write fw, 2nd time */ + fw2_off = fw2_blk * mtd->erasesize; + ret =nand_write_skip_bad (mtd, fw2_off, &fwsize,NULL , maxsize, + (u_char *)fwbuf,WITH_WR_VERIFY );
arch/arm/include/asm/mach-imx/imx-nandbcb.h
Quoted 10 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +10
-0+printf ("NAND fw write: 0x%llx offset, 0x%x bytes written: %s\n" , + fw2_off, fwsize, ret ?"ERROR" :"OK" ); +if (ret) + goto fw_err; + +/* fill fcb */ + fcb =kzalloc (sizeof (*fcb),GFP_KERNEL ); +if (!fcb) { +debug ("failed to allocate fcb\n" ); +return -EFAULT ;
You should better do:
ret = -ENOMEM;
goto XXXX
so that you can free the previously allocated kzalloc()
arch/arm/include/asm/mach-imx/imx-nandbcb.h
Quoted 13 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +13
-0+ } + + fcb->fw1_start = (fw1_blk * mtd->erasesize) / mtd->writesize; + fcb->fw2_start = (fw2_blk * mtd->erasesize) / mtd->writesize; + fcb->fw1_pages = size / mtd->writesize +1 ; + fcb->fw2_pages = fcb->fw1_pages; +fill_fcb (fcb, mtd); + +/* fill dbbt */ + dbbt_page =kzalloc (mtd->writesize,GFP_KERNEL ); +if (!dbbt_page) { +debug ("failed to allocate dbbt_page\n" ); +return -EFAULT ;
Ditto.
arch/arm/include/asm/mach-imx/imx-nandbcb.h
Quoted 6 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +6
-0+ } + + dbbt_data_page =kzalloc (mtd->writesize,GFP_KERNEL ); +if (!dbbt_data_page) { +debug ("failed to allocate dbbt_data_page\n" ); +return -EFAULT ;
Ditto.
Quoted 7 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +7
-0+ } + + dbbt = dbbt_page; + dbbt->checksum =0 ; + dbbt->fingerprint =DBBT_FINGERPRINT2 ; + dbbt->version =DBBT_VERSION_1 ; + ret =dbbt_fill_data (mtd, dbbt_data_page, nr_blks);
arch/arm/include/asm/mach-imx/imx-nandbcb.h
Quoted 10 lines
arch/arm/include/asm/mach-imx/imx-nandbcb.h new file +10
-0+if (ret <0 ) + goto dbbt_err; +else if (ret >0 ) + dbbt->dbbtnumofpages =1 ; + +/* write fcb/dbbt */ + fcb_raw_page =kzalloc (mtd->writesize + mtd->oobsize,GFP_KERNEL ); +if (!fcb_raw_page) { +debug ("failed to allocate fcb_raw_page\n" ); +return -EFAULT ;
Ditto.