mtd: Support slc-mode for Toshiba TC58TEG5DCLTA00
Subsystems (2)
cover mtd: Support slc-mode for Toshiba TC58TEG5DCLTA00
From: Chris Morgan <[email protected]>
Add support for slc-mode implemented in Linux for the Toshiba TC58TEG5DCLTA00 NAND flash found on the NTC CHIP. This requires the addition of a paired-pages scheme, a new parameter for MTD partitions of slc-mode, and setting the correct paired-pages scheme for the TC58TEG5DCLTA00 flash chip. I will likely need some help making sure this is consistent with the quality expected of upstream, as well as solving some remaining issues (such as ubifs partitions not working with slc-mode set).
Chris Morgan (2):
mtd: Add partial support for Linux slc-mode for MLC NAND
mtd: Add pairing info for Toshiba TC58TEG5DCLTA00 NAND
drivers/mtd/mtdcore.c | 320 ++++++++++++++++++++++++++++--- drivers/mtd/mtdpart.c | 7 + drivers/mtd/nand/raw/nand_base.c | 59 ++++++ include/linux/mtd/mtd.h | 126 ++++++++++++ include/linux/mtd/partitions.h | 2 + include/mtd/mtd-abi.h | 1 + 6 files changed, 491 insertions(+), 24 deletions(-)
Signature · 1 line
2.30.2
RFC 1/2 mtd: Add partial support for Linux slc-mode for MLC NAND
From: Chris Morgan <[email protected]>
Add support for setting the slc-mode parameter for a given MLC NAND device. This allows a given device to write only to the first set of paried pages to help reduce data corruption and increase reliability of the NAND.
I haven't tested these patches extensively yet, but thus far I have been able to boot a FIT image written directly to an MTD partition from within Linux via U-boot where the slc-mode parameter is set. I am not able to attach a ubifs partition generated in Linux with the slc-mode set.
As I am unfamiliar with NAND and have limited experience with the MTD subsystems, please let me know what else I can do to improve the driver (and possibly get it where ubifs can work).
Thank you.
--- drivers/mtd/mtdcore.c | 320 ++++++++++++++++++++++++++++--- drivers/mtd/mtdpart.c | 7 + drivers/mtd/nand/raw/nand_base.c | 50 +++++ include/linux/mtd/mtd.h | 126 ++++++++++++ include/linux/mtd/partitions.h | 2 + include/mtd/mtd-abi.h | 1 + 6 files changed, 482 insertions(+), 24 deletions(-)
drivers/mtd/mtdcore.c +296 -24
@@ -397,6 +397,114 @@ static struct device_type mtd_devtype = { };#endif +/** +* mtd_wunit_to_pairing_info - get pairing information of a wunit +* @mtd: pointer to new MTD device info structure +* @wunit: write unit we are interested in +* @info: returned pairing information +* +* Retrieve pairing information associated to the wunit. +* This is mainly useful when dealing with MLC/TLC NANDs where pages can be +* paired together, and where programming a page may influence the page it is +* paired with. +* The notion of page is replaced by the term wunit (write-unit) to stay +* consistent with the ->writesize field. +* +* The @wunit argument can be extracted from an absolute offset using +* mtd_offset_to_wunit(). @info is filled with the pairing information attached +* to @wunit. +* +* From the pairing info the MTD user can find all the wunits paired with +* @wunit using the following loop: +* +* for (i = 0; i < mtd_pairing_groups(mtd); i++) { +* info.pair = i; +* mtd_pairing_info_to_wunit(mtd, &info); +* ... +* } +*/ +int mtd_wunit_to_pairing_info (struct mtd_info *mtd,int wunit, +struct mtd_pairing_info *info) +{ +struct mtd_info *master =mtd_get_master (mtd); +int npairs =mtd_wunit_per_eb (master) /mtd_pairing_groups (master); + +if (wunit <0 || wunit >= npairs) +return -EINVAL ; + +if (master->pairing && master->pairing->get_info) +return master->pairing->get_info (master, wunit, info); + + info->group =0 ; + info->pair = wunit; + +return 0 ; +} +EXPORT_SYMBOL_GPL (mtd_wunit_to_pairing_info); + +/** +* mtd_pairing_info_to_wunit - get wunit from pairing information +* @mtd: pointer to new MTD device info structure +* @info: pairing information struct +* +* Returns a positive number representing the wunit associated to the info +* struct, or a negative error code. +* +* This is the reverse of mtd_wunit_to_pairing_info(), and can help one to +* iterate over all wunits of a given pair (see mtd_wunit_to_pairing_info() +* doc). +* +* It can also be used to only program the first page of each pair (i.e. +* page attached to group 0), which allows one to use an MLC NAND in +* software-emulated SLC mode: +* +* info.group = 0; +* npairs = mtd_wunit_per_eb(mtd) / mtd_pairing_groups(mtd); +* for (info.pair = 0; info.pair < npairs; info.pair++) { +* wunit = mtd_pairing_info_to_wunit(mtd, &info); +* mtd_write(mtd, mtd_wunit_to_offset(mtd, blkoffs, wunit), +* mtd->writesize, &retlen, buf + (i * mtd->writesize)); +* } +*/ +int mtd_pairing_info_to_wunit (struct mtd_info *mtd, +const struct mtd_pairing_info *info) +{ +struct mtd_info *master =mtd_get_master (mtd); +int ngroups =mtd_pairing_groups (master); +int npairs =mtd_wunit_per_eb (master) / ngroups; + +if (!info || info->pair <0 || info->pair >= npairs || + info->group <0 || info->group >= ngroups) +return -EINVAL ; + +if (master->pairing && master->pairing->get_wunit) +return mtd->pairing->get_wunit (master, info); + +return info->pair; +} +EXPORT_SYMBOL_GPL (mtd_pairing_info_to_wunit); + +/** +* mtd_pairing_groups - get the number of pairing groups +* @mtd: pointer to new MTD device info structure +* +* Returns the number of pairing groups. +* +* This number is usually equal to the number of bits exposed by a single +* cell, and can be used in conjunction with mtd_pairing_info_to_wunit() +* to iterate over all pages of a given pair. +*/ +int mtd_pairing_groups (struct mtd_info *mtd) +{ +struct mtd_info *master =mtd_get_master (mtd); + +if (!master->pairing || !master->pairing->ngroups) +return 1 ; + +return master->pairing->ngroups; +} +EXPORT_SYMBOL_GPL (mtd_pairing_groups); +/** * add_mtd_device - register an MTD device * @mtd: pointer to new MTD device info structure
@@ -412,6 +520,7 @@ int add_mtd_device(struct mtd_info *mtd)#ifndef __UBOOT__struct mtd_notifier *not;#endif +struct mtd_info *master =mtd_get_master (mtd);int i, error;#ifndef __UBOOT__
@@ -431,6 +540,20 @@ int add_mtd_device(struct mtd_info *mtd)#endif BUG_ON (mtd->writesize ==0 ); + +/* +* MTD_SLC_ON_MLC_EMULATION can only be set on partitions, when the +* master is an MLC NAND and has a proper pairing scheme defined. +* We also reject masters that implement ->_writev() for now, because +* NAND controller drivers don't implement this hook, and adding the +* SLC -> MLC address/length conversion to this path is useless if we +* don't have a user. +*/ +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION && + (!mtd_is_partition (mtd) || master->type !=MTD_MLCNANDFLASH || + !master->pairing)) +return -EINVAL ; +mutex_lock (&mtd_table_mutex); i =idr_alloc (&mtd_idr, mtd,0 ,0 ,GFP_KERNEL );
@@ -446,6 +569,14 @@ int add_mtd_device(struct mtd_info *mtd)if (mtd->bitflip_threshold ==0 ) mtd->bitflip_threshold = mtd->ecc_strength; +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) { +int ngroups =mtd_pairing_groups (master); + + mtd->erasesize /= ngroups; + mtd->size = (u64 )mtd_div_by_eb (mtd->size, master) * + mtd->erasesize; + } +if (is_power_of_2 (mtd->erasesize)) mtd->erasesize_shift =ffs (mtd->erasesize) -1 ;else
@@ -880,17 +1011,29 @@ EXPORT_SYMBOL_GPL(__put_mtd_device);*/ int mtd_erase (struct mtd_info *mtd,struct erase_info *instr) { +struct mtd_info *master =mtd_get_master (mtd); +struct erase_info adjinstr;if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)return -EINVAL ;if (!(mtd->flags &MTD_WRITEABLE ))return -EROFS ; instr->fail_addr =MTD_FAIL_ADDR_UNKNOWN ; + adjinstr = *instr; + +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) { + adjinstr.addr = (loff_t )mtd_div_by_eb (instr->addr, mtd) * + master->erasesize; + adjinstr.len = ((u64 )mtd_div_by_eb (instr->addr + instr->len, mtd) * + master->erasesize) - + adjinstr.addr; + } +if (!instr->len) { - instr->state =MTD_ERASE_DONE ; -mtd_erase_callback (instr); + adjinstr.state =MTD_ERASE_DONE ; +mtd_erase_callback (&adjinstr);return 0 ; } -return mtd->_erase (mtd, instr); +return master->_erase (master, &adjinstr); }EXPORT_SYMBOL_GPL (mtd_erase);
@@ -1066,6 +1209,101 @@ static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,return 0 ; } +static int mtd_read_oob_std (struct mtd_info *mtd,loff_t from, +struct mtd_oob_ops *ops) +{ +struct mtd_info *master =mtd_get_master (mtd); +int ret; + + from =mtd_get_master_ofs (mtd, from); +if (master->_read_oob) + ret = master->_read_oob (master, from, ops); +else + ret = master->_read (master, from, ops->len, &ops->retlen, + ops->datbuf); + +return ret; +} + +static int mtd_write_oob_std (struct mtd_info *mtd,loff_t to, +struct mtd_oob_ops *ops) +{ +struct mtd_info *master =mtd_get_master (mtd); +int ret; + + to =mtd_get_master_ofs (mtd, to); +if (master->_write_oob) + ret = master->_write_oob (master, to, ops); +else + ret = master->_write (master, to, ops->len, &ops->retlen, + ops->datbuf); + +return ret; +} + +static int mtd_io_emulated_slc (struct mtd_info *mtd,loff_t start,bool read, +struct mtd_oob_ops *ops) +{ +struct mtd_info *master =mtd_get_master (mtd); +int ngroups =mtd_pairing_groups (master); +int npairs =mtd_wunit_per_eb (master) / ngroups; +struct mtd_oob_ops adjops = *ops; +unsigned int wunit, oobavail; +struct mtd_pairing_info info; +int max_bitflips =0 ; +u32 ebofs, pageofs; +loff_t base, pos; + + ebofs =mtd_mod_by_eb (start, mtd); + base = (loff_t )mtd_div_by_eb (start, mtd) * master->erasesize; + info.group =0 ; + info.pair =mtd_div_by_ws (ebofs, mtd); + pageofs =mtd_mod_by_ws (ebofs, mtd); + oobavail =mtd_oobavail (mtd, ops); + +while (ops->retlen < ops->len || ops->oobretlen < ops->ooblen) { +int ret; + +if (info.pair >= npairs) { + info.pair =0 ; + base += master->erasesize; + } + + wunit =mtd_pairing_info_to_wunit (master, &info); + pos =mtd_wunit_to_offset (mtd, base, wunit); + + adjops.len = ops->len - ops->retlen; +if (adjops.len > mtd->writesize - pageofs) + adjops.len = mtd->writesize - pageofs; + + adjops.ooblen = ops->ooblen - ops->oobretlen; +if (adjops.ooblen > oobavail - adjops.ooboffs) + adjops.ooblen = oobavail - adjops.ooboffs; + +if (read) { + ret =mtd_read_oob_std (mtd, pos + pageofs, &adjops); +if (ret >0 ) + max_bitflips =max (max_bitflips, ret); + }else { + ret =mtd_write_oob_std (mtd, pos + pageofs, &adjops); + } + +if (ret <0 ) +return ret; + + max_bitflips =max (max_bitflips, ret); + ops->retlen += adjops.retlen; + ops->oobretlen += adjops.oobretlen; + adjops.datbuf += adjops.retlen; + adjops.oobbuf += adjops.oobretlen; + adjops.ooboffs =0 ; + pageofs =0 ; + info.pair++; + } + +return max_bitflips; +} +int mtd_read_oob (struct mtd_info *mtd,loff_t from,struct mtd_oob_ops *ops) {int ret_code;
@@ -1079,11 +1317,10 @@ int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)if (!mtd->_read_oob && (!mtd->_read || ops->oobbuf))return -EOPNOTSUPP ; -if (mtd->_read_oob) - ret_code = mtd->_read_oob (mtd, from, ops); +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) + ret_code =mtd_io_emulated_slc (mtd, from, true, ops);else - ret_code = mtd->_read (mtd, from, ops->len, &ops->retlen, - ops->datbuf); + ret_code =mtd_read_oob_std (mtd, from, ops);/* * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
@@ -1117,11 +1354,11 @@ int mtd_write_oob(struct mtd_info *mtd, loff_t to,if (!mtd->_write_oob && (!mtd->_write || ops->oobbuf))return -EOPNOTSUPP ; -if (mtd->_write_oob) -return mtd->_write_oob (mtd, to, ops); -else -return mtd->_write (mtd, to, ops->len, &ops->retlen, - ops->datbuf); +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) +return mtd_io_emulated_slc (mtd, to, false, ops); + +return mtd_write_oob_std (mtd, to, ops); + }EXPORT_SYMBOL_GPL (mtd_write_oob);
@@ -1571,69 +1808,104 @@ EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);/* Chip-supported device locking */ int mtd_lock (struct mtd_info *mtd,loff_t ofs,uint64_t len) { -if (!mtd->_lock) +struct mtd_info *master =mtd_get_master (mtd); + +if (!master->_lock)return -EOPNOTSUPP ;if (ofs <0 || ofs > mtd->size || len > mtd->size - ofs)return -EINVAL ;if (!len)return 0 ; -return mtd->_lock (mtd, ofs, len); +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) { + ofs = (loff_t )mtd_div_by_eb (ofs, mtd) * master->erasesize; + len = (u64 )mtd_div_by_eb (len, mtd) * master->erasesize; + } + +return master->_lock (master,mtd_get_master_ofs (mtd, ofs), len); }EXPORT_SYMBOL_GPL (mtd_lock);int mtd_unlock (struct mtd_info *mtd,loff_t ofs,uint64_t len) { -if (!mtd->_unlock) +struct mtd_info *master =mtd_get_master (mtd); + +if (!master->_unlock)return -EOPNOTSUPP ;if (ofs <0 || ofs > mtd->size || len > mtd->size - ofs)return -EINVAL ;if (!len)return 0 ; -return mtd->_unlock (mtd, ofs, len); +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) { + ofs = (loff_t )mtd_div_by_eb (ofs, mtd) * master->erasesize; + len = (u64 )mtd_div_by_eb (len, mtd) * master->erasesize; + } + +return master->_unlock (master,mtd_get_master_ofs (mtd, ofs), len); }EXPORT_SYMBOL_GPL (mtd_unlock);int mtd_is_locked (struct mtd_info *mtd,loff_t ofs,uint64_t len) { -if (!mtd->_is_locked) +struct mtd_info *master =mtd_get_master (mtd); + +if (!master->_is_locked)return -EOPNOTSUPP ;if (ofs <0 || ofs > mtd->size || len > mtd->size - ofs)return -EINVAL ;if (!len)return 0 ; -return mtd->_is_locked (mtd, ofs, len); +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) { + ofs = (loff_t )mtd_div_by_eb (ofs, mtd) * master->erasesize; + len = (u64 )mtd_div_by_eb (len, mtd) * master->erasesize; + } + +return master->_is_locked (master,mtd_get_master_ofs (mtd, ofs), len); }EXPORT_SYMBOL_GPL (mtd_is_locked);int mtd_block_isreserved (struct mtd_info *mtd,loff_t ofs) { +struct mtd_info *master =mtd_get_master (mtd); +if (ofs <0 || ofs > mtd->size)return -EINVAL ; -if (!mtd->_block_isreserved) +if (!master->_block_isreserved)return 0 ; -return mtd->_block_isreserved (mtd, ofs); +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) + ofs = (loff_t )mtd_div_by_eb (ofs, mtd) * master->erasesize; +return master->_block_isreserved (master,mtd_get_master_ofs (mtd, ofs)); }EXPORT_SYMBOL_GPL (mtd_block_isreserved);int mtd_block_isbad (struct mtd_info *mtd,loff_t ofs) { +struct mtd_info *master =mtd_get_master (mtd); +if (ofs <0 || ofs > mtd->size)return -EINVAL ; -if (!mtd->_block_isbad) +if (!master->_block_isbad)return 0 ; -return mtd->_block_isbad (mtd, ofs); +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) + ofs = (loff_t )mtd_div_by_eb (ofs, mtd) * master->erasesize; + +return master->_block_isbad (master,mtd_get_master_ofs (mtd, ofs)); }EXPORT_SYMBOL_GPL (mtd_block_isbad);int mtd_block_markbad (struct mtd_info *mtd,loff_t ofs) { -if (!mtd->_block_markbad) +struct mtd_info *master =mtd_get_master (mtd); + +if (!master->_block_markbad)return -EOPNOTSUPP ;if (ofs <0 || ofs > mtd->size)return -EINVAL ;if (!(mtd->flags &MTD_WRITEABLE ))return -EROFS ; -return mtd->_block_markbad (mtd, ofs); +if (mtd->flags &MTD_SLC_ON_MLC_EMULATION ) + ofs = (loff_t )mtd_div_by_eb (ofs, mtd) * master->erasesize; + +return master->_block_markbad (master,mtd_get_master_ofs (mtd, ofs)); }EXPORT_SYMBOL_GPL (mtd_block_markbad);
drivers/mtd/mtdpart.c +7 -0
@@ -148,6 +148,12 @@ static int mtd_parse_partition(const char **_mtdparts, mtdparts +=2 ; } +/* if slc is found use emulated SLC mode on this partition*/ +if (!strncmp (mtdparts,"slc" ,3 )) { + partition->add_flags |=MTD_SLC_ON_MLC_EMULATION ; + mtdparts +=3 ; + } +/* Check for a potential next partition definition */ if (*mtdparts ==',' ) {if (partition->size ==MTD_SIZE_REMAINING ) {
@@ -591,6 +597,7 @@ static struct mtd_info *allocate_partition(struct mtd_info *master,/* set up the MTD object for this partition */ slave->type = master->type; slave->flags = master->flags & ~part->mask_flags; + slave->flags |= part->add_flags; slave->size = part->size; slave->writesize = master->writesize; slave->writebufsize = master->writebufsize;
drivers/mtd/nand/raw/nand_base.c +50 -0
56 lines not on this page · raw message
include/linux/mtd/mtd.h +126 -0
161 lines not on this page · raw message
include/linux/mtd/partitions.h +2 -0
@@ -31,6 +31,7 @@* master MTD flag set for the corresponding MTD partition. * For example, to force a read-only partition, simply adding * MTD_WRITEABLE to the mask_flags will do the trick. +* add_flags: contains flags to add to the parent flags * * Note: writeable partitions require their size and offset be * erasesize aligned (e.g. use MTDPART_OFS_NEXTBLK).
@@ -41,6 +42,7 @@ struct mtd_partition {uint64_t size;/* partition size */ uint64_t offset;/* offset within the master MTD space */ uint32_t mask_flags;/* master MTD flags to mask out for this partition */ +uint32_t add_flags;/* flags to add to the partition */ struct nand_ecclayout *ecclayout;/* out of band layout for this partition (NAND only) */ };
include/mtd/mtd-abi.h +1 -0
@@ -94,6 +94,7 @@ struct mtd_write_req {#define MTD_BIT_WRITEABLE 0x800/* Single bits can be flipped */ #define MTD_NO_ERASE 0x1000/* No erase necessary */ #define MTD_POWERUP_LOCK 0x2000/* Always locked after reset */ +#define MTD_SLC_ON_MLC_EMULATION 0x4000/* Emulate SLC behavior on MLC NANDs */ /* Some common devices / combinations of capabilities */ #define MTD_CAP_ROM 0
Signature · 1 line
2.30.2
RFC 2/2 mtd: Add pairing info for Toshiba TC58TEG5DCLTA00 NAND
In reply to Chris MorganFrom: Chris Morgan <[email protected]>
Add pairing info for the Toshiba TC58TEG5DCLTA00 NAND so that we may utilize slc-mode emulation for enhanced reliability and compatibility with upstream Linux.
--- drivers/mtd/nand/raw/nand_base.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/mtd/nand/raw/nand_base.c +9 -0
@@ -4549,6 +4549,15 @@ ident_done:break ; } +/* Special handling for TC58TEG5DCLTA00 to match upstream Linux */ +if (id_data[0 ] ==NAND_MFR_TOSHIBA && id_data[1 ] ==0xd7 + && id_data[2 ] ==0x84 && id_data[3 ] ==0x93 + && id_data[4 ] ==0x72 && id_data[5 ] ==0x51 + && id_data[6 ] ==0x08 && id_data[7 ] ==0x04 ) { + chip->options |=NAND_NEED_SCRAMBLING ; +mtd_set_pairing_scheme (mtd, &dist3_pairing_scheme); + } +if (chip->options &NAND_BUSWIDTH_AUTO ) {WARN_ON (chip->options &NAND_BUSWIDTH_16 ); chip->options |= busw;
Signature · 1 line
2.30.2