summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/nand
diff options
context:
space:
mode:
authorMasahiro Yamada <yamada.masahiro@socionext.com>2017-03-30 02:45:50 -0400
committerBoris Brezillon <boris.brezillon@free-electrons.com>2017-04-25 08:18:33 -0400
commit20d48595f8857c9b7e0d31d9734ebe18d63faea1 (patch)
tree96548aa5cc8ca01c0dd597a88a37deca02df67c4 /drivers/mtd/nand
parent8927ad394b0653329184863e3d44958f67705e84 (diff)
mtd: nand: denali: fix bitflips calculation in handle_ecc()
This function is wrong in multiple ways: [1] Counting corrected bytes instead of corrected bits. The following code is counting the number of corrected _bytes_. /* correct the ECC error */ buf[offset] ^= err_cor_value; mtd->ecc_stats.corrected++; bitflips++; What the core framework expects is the number of corrected _bits_. They can be different if multiple bitflips occur within one byte. [2] total number of errors instead of max of per-sector errors The core framework expects that corrected errors are counted per sector, then the max value should be taken. The current code simply iterates over the whole page, i.e. counts the total number of correction in the page. This means "too many bitflips" is triggered earlier than it should be, i.e. the NAND device is worn out sooner. Besides those bugs, this function is unreadable due to the deep nesting. Notice the whole code in this function is wrapped in if (irq_status & INTR__ECC_ERR), so this conditional can be moved out of the function. Also, use shorter names for local variables. Re-work the function to fix all the issues. Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com> Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com>
Diffstat (limited to 'drivers/mtd/nand')
-rw-r--r--drivers/mtd/nand/denali.c141
1 files changed, 71 insertions, 70 deletions
diff --git a/drivers/mtd/nand/denali.c b/drivers/mtd/nand/denali.c
index 65cf7cccedbe..c5c150a95fb6 100644
--- a/drivers/mtd/nand/denali.c
+++ b/drivers/mtd/nand/denali.c
@@ -836,80 +836,80 @@ static bool is_erased(uint8_t *buf, int len)
836#define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12) 836#define ECC_SECTOR(x) (((x) & ECC_ERROR_ADDRESS__SECTOR_NR) >> 12)
837#define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET)) 837#define ECC_BYTE(x) (((x) & ECC_ERROR_ADDRESS__OFFSET))
838#define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK) 838#define ECC_CORRECTION_VALUE(x) ((x) & ERR_CORRECTION_INFO__BYTEMASK)
839#define ECC_ERROR_CORRECTABLE(x) (!((x) & ERR_CORRECTION_INFO__ERROR_TYPE)) 839#define ECC_ERROR_UNCORRECTABLE(x) ((x) & ERR_CORRECTION_INFO__ERROR_TYPE)
840#define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8) 840#define ECC_ERR_DEVICE(x) (((x) & ERR_CORRECTION_INFO__DEVICE_NR) >> 8)
841#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO) 841#define ECC_LAST_ERR(x) ((x) & ERR_CORRECTION_INFO__LAST_ERR_INFO)
842 842
843static bool handle_ecc(struct denali_nand_info *denali, uint8_t *buf, 843static int handle_ecc(struct mtd_info *mtd, struct denali_nand_info *denali,
844 uint32_t irq_status, unsigned int *max_bitflips) 844 uint8_t *buf, bool *check_erased_page)
845{ 845{
846 bool check_erased_page = false;
847 unsigned int bitflips = 0; 846 unsigned int bitflips = 0;
847 unsigned int max_bitflips = 0;
848 uint32_t err_addr, err_cor_info;
849 unsigned int err_byte, err_sector, err_device;
850 uint8_t err_cor_value;
851 unsigned int prev_sector = 0;
848 852
849 if (irq_status & INTR__ECC_ERR) { 853 /* read the ECC errors. we'll ignore them for now */
850 /* read the ECC errors. we'll ignore them for now */ 854 denali_set_intr_modes(denali, false);
851 uint32_t err_address, err_correction_info, err_byte, 855
852 err_sector, err_device, err_correction_value; 856 do {
853 denali_set_intr_modes(denali, false); 857 err_addr = ioread32(denali->flash_reg + ECC_ERROR_ADDRESS);
854 858 err_sector = ECC_SECTOR(err_addr);
855 do { 859 err_byte = ECC_BYTE(err_addr);
856 err_address = ioread32(denali->flash_reg + 860
857 ECC_ERROR_ADDRESS); 861 err_cor_info = ioread32(denali->flash_reg + ERR_CORRECTION_INFO);
858 err_sector = ECC_SECTOR(err_address); 862 err_cor_value = ECC_CORRECTION_VALUE(err_cor_info);
859 err_byte = ECC_BYTE(err_address); 863 err_device = ECC_ERR_DEVICE(err_cor_info);
860 864
861 err_correction_info = ioread32(denali->flash_reg + 865 /* reset the bitflip counter when crossing ECC sector */
862 ERR_CORRECTION_INFO); 866 if (err_sector != prev_sector)
863 err_correction_value = 867 bitflips = 0;
864 ECC_CORRECTION_VALUE(err_correction_info); 868
865 err_device = ECC_ERR_DEVICE(err_correction_info); 869 if (ECC_ERROR_UNCORRECTABLE(err_cor_info)) {
866 870 /*
867 if (ECC_ERROR_CORRECTABLE(err_correction_info)) { 871 * if the error is not correctable, need to look at the
868 /* 872 * page to see if it is an erased page. if so, then
869 * If err_byte is larger than ECC_SECTOR_SIZE, 873 * it's not a real ECC error
870 * means error happened in OOB, so we ignore 874 */
871 * it. It's no need for us to correct it 875 *check_erased_page = true;
872 * err_device is represented the NAND error 876 } else if (err_byte < ECC_SECTOR_SIZE) {
873 * bits are happened in if there are more 877 /*
874 * than one NAND connected. 878 * If err_byte is larger than ECC_SECTOR_SIZE, means error
875 */ 879 * happened in OOB, so we ignore it. It's no need for
876 if (err_byte < ECC_SECTOR_SIZE) { 880 * us to correct it err_device is represented the NAND
877 struct mtd_info *mtd = 881 * error bits are happened in if there are more than
878 nand_to_mtd(&denali->nand); 882 * one NAND connected.
879 int offset; 883 */
880 884 int offset;
881 offset = (err_sector * 885 unsigned int flips_in_byte;
882 ECC_SECTOR_SIZE + 886
883 err_byte) * 887 offset = (err_sector * ECC_SECTOR_SIZE + err_byte) *
884 denali->devnum + 888 denali->devnum + err_device;
885 err_device; 889
886 /* correct the ECC error */ 890 /* correct the ECC error */
887 buf[offset] ^= err_correction_value; 891 flips_in_byte = hweight8(buf[offset] ^ err_cor_value);
888 mtd->ecc_stats.corrected++; 892 buf[offset] ^= err_cor_value;
889 bitflips++; 893 mtd->ecc_stats.corrected += flips_in_byte;
890 } 894 bitflips += flips_in_byte;
891 } else { 895
892 /* 896 max_bitflips = max(max_bitflips, bitflips);
893 * if the error is not correctable, need to 897 }
894 * look at the page to see if it is an erased 898
895 * page. if so, then it's not a real ECC error 899 prev_sector = err_sector;
896 */ 900 } while (!ECC_LAST_ERR(err_cor_info));
897 check_erased_page = true; 901
898 } 902 /*
899 } while (!ECC_LAST_ERR(err_correction_info)); 903 * Once handle all ecc errors, controller will trigger a
900 /* 904 * ECC_TRANSACTION_DONE interrupt, so here just wait for
901 * Once handle all ecc errors, controller will triger 905 * a while for this interrupt
902 * a ECC_TRANSACTION_DONE interrupt, so here just wait 906 */
903 * for a while for this interrupt 907 while (!(read_interrupt_status(denali) & INTR__ECC_TRANSACTION_DONE))
904 */ 908 cpu_relax();
905 while (!(read_interrupt_status(denali) & 909 clear_interrupts(denali);
906 INTR__ECC_TRANSACTION_DONE)) 910 denali_set_intr_modes(denali, true);
907 cpu_relax(); 911
908 clear_interrupts(denali); 912 return max_bitflips;
909 denali_set_intr_modes(denali, true);
910 }
911 *max_bitflips = bitflips;
912 return check_erased_page;
913} 913}
914 914
915/* programs the controller to either enable/disable DMA transfers */ 915/* programs the controller to either enable/disable DMA transfers */
@@ -1045,7 +1045,7 @@ static int denali_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1045static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip, 1045static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1046 uint8_t *buf, int oob_required, int page) 1046 uint8_t *buf, int oob_required, int page)
1047{ 1047{
1048 unsigned int max_bitflips; 1048 unsigned int max_bitflips = 0;
1049 struct denali_nand_info *denali = mtd_to_denali(mtd); 1049 struct denali_nand_info *denali = mtd_to_denali(mtd);
1050 1050
1051 dma_addr_t addr = denali->buf.dma_buf; 1051 dma_addr_t addr = denali->buf.dma_buf;
@@ -1077,7 +1077,8 @@ static int denali_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1077 1077
1078 memcpy(buf, denali->buf.buf, mtd->writesize); 1078 memcpy(buf, denali->buf.buf, mtd->writesize);
1079 1079
1080 check_erased_page = handle_ecc(denali, buf, irq_status, &max_bitflips); 1080 if (irq_status & INTR__ECC_ERR)
1081 max_bitflips = handle_ecc(mtd, denali, buf, &check_erased_page);
1081 denali_enable_dma(denali, false); 1082 denali_enable_dma(denali, false);
1082 1083
1083 if (check_erased_page) { 1084 if (check_erased_page) {