Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2004-2008 Texas Instruments, <www.ti.com> |
| 4 | * Rohit Choraria <rohitkc@ti.com> |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 9 | #include <asm/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 10 | #include <linux/errno.h> |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 11 | #include <asm/arch/mem.h> |
pekon gupta | 5bbb099 | 2013-11-22 16:53:29 +0530 | [diff] [blame] | 12 | #include <linux/mtd/omap_gpmc.h> |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 13 | #include <linux/mtd/nand_ecc.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 14 | #include <linux/mtd/rawnand.h> |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 15 | #include <linux/bch.h> |
Stefano Babic | aade579 | 2012-03-21 23:56:17 +0000 | [diff] [blame] | 16 | #include <linux/compiler.h> |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 17 | #include <nand.h> |
pekon gupta | 7295fe8 | 2013-11-22 16:53:30 +0530 | [diff] [blame] | 18 | #include <linux/mtd/omap_elm.h> |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 19 | |
| 20 | #define BADBLOCK_MARKER_LENGTH 2 |
| 21 | #define SECTOR_BYTES 512 |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 22 | #define ECCCLEAR (0x1 << 8) |
| 23 | #define ECCRESULTREG1 (0x1 << 0) |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 24 | /* 4 bit padding to make byte aligned, 56 = 52 + 4 */ |
| 25 | #define BCH4_BIT_PAD 4 |
| 26 | |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 27 | #ifdef CONFIG_BCH |
| 28 | static u8 bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2, |
| 29 | 0x97, 0x79, 0xe5, 0x24, 0xb5}; |
| 30 | #endif |
Rostislav Lisovy | 77cdf8a | 2014-09-02 16:23:58 +0200 | [diff] [blame] | 31 | static uint8_t cs_next; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 32 | static __maybe_unused struct nand_ecclayout omap_ecclayout; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 33 | |
Michal Sojka | 2616007 | 2015-02-17 17:08:37 +0100 | [diff] [blame] | 34 | #if defined(CONFIG_NAND_OMAP_GPMC_WSCFG) |
| 35 | static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] = |
| 36 | { CONFIG_NAND_OMAP_GPMC_WSCFG }; |
| 37 | #else |
| 38 | /* wscfg is preset to zero since its a static variable */ |
| 39 | static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE]; |
| 40 | #endif |
| 41 | |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 42 | /* |
Rostislav Lisovy | 77cdf8a | 2014-09-02 16:23:58 +0200 | [diff] [blame] | 43 | * Driver configurations |
| 44 | */ |
| 45 | struct omap_nand_info { |
| 46 | struct bch_control *control; |
| 47 | enum omap_ecc ecc_scheme; |
Michal Sojka | 2616007 | 2015-02-17 17:08:37 +0100 | [diff] [blame] | 48 | uint8_t cs; |
| 49 | uint8_t ws; /* wait status pin (0,1) */ |
Rostislav Lisovy | 77cdf8a | 2014-09-02 16:23:58 +0200 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /* We are wasting a bit of memory but al least we are safe */ |
| 53 | static struct omap_nand_info omap_nand_info[GPMC_MAX_CS]; |
| 54 | |
| 55 | /* |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 56 | * omap_nand_hwcontrol - Set the address pointers corretly for the |
| 57 | * following address/data/command operation |
| 58 | */ |
| 59 | static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd, |
| 60 | uint32_t ctrl) |
| 61 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 62 | register struct nand_chip *this = mtd_to_nand(mtd); |
| 63 | struct omap_nand_info *info = nand_get_controller_data(this); |
Rostislav Lisovy | 77cdf8a | 2014-09-02 16:23:58 +0200 | [diff] [blame] | 64 | int cs = info->cs; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Point the IO_ADDR to DATA and ADDRESS registers instead |
| 68 | * of chip address |
| 69 | */ |
| 70 | switch (ctrl) { |
| 71 | case NAND_CTRL_CHANGE | NAND_CTRL_CLE: |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 72 | this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 73 | break; |
| 74 | case NAND_CTRL_CHANGE | NAND_CTRL_ALE: |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 75 | this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 76 | break; |
| 77 | case NAND_CTRL_CHANGE | NAND_NCE: |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 78 | this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | |
| 82 | if (cmd != NAND_CMD_NONE) |
| 83 | writeb(cmd, this->IO_ADDR_W); |
| 84 | } |
| 85 | |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 86 | /* Check wait pin as dev ready indicator */ |
Stefan Roese | e05972f | 2014-11-13 03:43:39 +0100 | [diff] [blame] | 87 | static int omap_dev_ready(struct mtd_info *mtd) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 88 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 89 | register struct nand_chip *this = mtd_to_nand(mtd); |
| 90 | struct omap_nand_info *info = nand_get_controller_data(this); |
Michal Sojka | 2616007 | 2015-02-17 17:08:37 +0100 | [diff] [blame] | 91 | return gpmc_cfg->status & (1 << (8 + info->ws)); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 92 | } |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * gen_true_ecc - This function will generate true ECC value, which |
| 96 | * can be used when correcting data read from NAND flash memory core |
| 97 | * |
| 98 | * @ecc_buf: buffer to store ecc code |
| 99 | * |
| 100 | * @return: re-formatted ECC value |
| 101 | */ |
| 102 | static uint32_t gen_true_ecc(uint8_t *ecc_buf) |
| 103 | { |
| 104 | return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) | |
| 105 | ((ecc_buf[2] & 0x0F) << 8); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * omap_correct_data - Compares the ecc read from nand spare area with ECC |
Vagrant Cascadian | edfdb99 | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 110 | * registers values and corrects one bit error if it has occurred |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 111 | * Further details can be had from OMAP TRM and the following selected links: |
| 112 | * http://en.wikipedia.org/wiki/Hamming_code |
| 113 | * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf |
| 114 | * |
| 115 | * @mtd: MTD device structure |
| 116 | * @dat: page data |
| 117 | * @read_ecc: ecc read from nand flash |
| 118 | * @calc_ecc: ecc read from ECC registers |
| 119 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 120 | * Return: 0 if data is OK or corrected, else returns -1 |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 121 | */ |
Stefano Babic | aade579 | 2012-03-21 23:56:17 +0000 | [diff] [blame] | 122 | static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat, |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 123 | uint8_t *read_ecc, uint8_t *calc_ecc) |
| 124 | { |
| 125 | uint32_t orig_ecc, new_ecc, res, hm; |
| 126 | uint16_t parity_bits, byte; |
| 127 | uint8_t bit; |
| 128 | |
| 129 | /* Regenerate the orginal ECC */ |
| 130 | orig_ecc = gen_true_ecc(read_ecc); |
| 131 | new_ecc = gen_true_ecc(calc_ecc); |
| 132 | /* Get the XOR of real ecc */ |
| 133 | res = orig_ecc ^ new_ecc; |
| 134 | if (res) { |
| 135 | /* Get the hamming width */ |
| 136 | hm = hweight32(res); |
| 137 | /* Single bit errors can be corrected! */ |
| 138 | if (hm == 12) { |
| 139 | /* Correctable data! */ |
| 140 | parity_bits = res >> 16; |
| 141 | bit = (parity_bits & 0x7); |
| 142 | byte = (parity_bits >> 3) & 0x1FF; |
| 143 | /* Flip the bit to correct */ |
| 144 | dat[byte] ^= (0x1 << bit); |
| 145 | } else if (hm == 1) { |
| 146 | printf("Error: Ecc is wrong\n"); |
| 147 | /* ECC itself is corrupted */ |
| 148 | return 2; |
| 149 | } else { |
| 150 | /* |
| 151 | * hm distance != parity pairs OR one, could mean 2 bit |
| 152 | * error OR potentially be on a blank page.. |
| 153 | * orig_ecc: contains spare area data from nand flash. |
| 154 | * new_ecc: generated ecc while reading data area. |
| 155 | * Note: if the ecc = 0, all data bits from which it was |
| 156 | * generated are 0xFF. |
| 157 | * The 3 byte(24 bits) ecc is generated per 512byte |
| 158 | * chunk of a page. If orig_ecc(from spare area) |
| 159 | * is 0xFF && new_ecc(computed now from data area)=0x0, |
| 160 | * this means that data area is 0xFF and spare area is |
| 161 | * 0xFF. A sure sign of a erased page! |
| 162 | */ |
| 163 | if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000)) |
| 164 | return 0; |
| 165 | printf("Error: Bad compare! failed\n"); |
| 166 | /* detected 2 bit error */ |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 167 | return -EBADMSG; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 174 | * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 175 | * @mtd: MTD device structure |
| 176 | * @mode: Read/Write mode |
| 177 | */ |
| 178 | __maybe_unused |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 179 | static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode) |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 180 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 181 | struct nand_chip *nand = mtd_to_nand(mtd); |
| 182 | struct omap_nand_info *info = nand_get_controller_data(nand); |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 183 | unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0; |
| 184 | unsigned int ecc_algo = 0; |
| 185 | unsigned int bch_type = 0; |
| 186 | unsigned int eccsize1 = 0x00, eccsize0 = 0x00, bch_wrapmode = 0x00; |
| 187 | u32 ecc_size_config_val = 0; |
| 188 | u32 ecc_config_val = 0; |
Rostislav Lisovy | 77cdf8a | 2014-09-02 16:23:58 +0200 | [diff] [blame] | 189 | int cs = info->cs; |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 190 | |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 191 | /* configure GPMC for specific ecc-scheme */ |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 192 | switch (info->ecc_scheme) { |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 193 | case OMAP_ECC_HAM1_CODE_SW: |
| 194 | return; |
| 195 | case OMAP_ECC_HAM1_CODE_HW: |
| 196 | ecc_algo = 0x0; |
| 197 | bch_type = 0x0; |
| 198 | bch_wrapmode = 0x00; |
| 199 | eccsize0 = 0xFF; |
| 200 | eccsize1 = 0xFF; |
| 201 | break; |
| 202 | case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW: |
| 203 | case OMAP_ECC_BCH8_CODE_HW: |
| 204 | ecc_algo = 0x1; |
| 205 | bch_type = 0x1; |
| 206 | if (mode == NAND_ECC_WRITE) { |
| 207 | bch_wrapmode = 0x01; |
| 208 | eccsize0 = 0; /* extra bits in nibbles per sector */ |
| 209 | eccsize1 = 28; /* OOB bits in nibbles per sector */ |
| 210 | } else { |
| 211 | bch_wrapmode = 0x01; |
| 212 | eccsize0 = 26; /* ECC bits in nibbles per sector */ |
| 213 | eccsize1 = 2; /* non-ECC bits in nibbles per sector */ |
Stefan Roese | 7a12cc6 | 2013-12-05 07:58:06 +0100 | [diff] [blame] | 214 | } |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 215 | break; |
pekon gupta | 046cf86 | 2014-06-02 17:14:42 +0530 | [diff] [blame] | 216 | case OMAP_ECC_BCH16_CODE_HW: |
| 217 | ecc_algo = 0x1; |
| 218 | bch_type = 0x2; |
| 219 | if (mode == NAND_ECC_WRITE) { |
| 220 | bch_wrapmode = 0x01; |
| 221 | eccsize0 = 0; /* extra bits in nibbles per sector */ |
| 222 | eccsize1 = 52; /* OOB bits in nibbles per sector */ |
| 223 | } else { |
| 224 | bch_wrapmode = 0x01; |
| 225 | eccsize0 = 52; /* ECC bits in nibbles per sector */ |
| 226 | eccsize1 = 0; /* non-ECC bits in nibbles per sector */ |
| 227 | } |
| 228 | break; |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 229 | default: |
| 230 | return; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 231 | } |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 232 | /* Clear ecc and enable bits */ |
| 233 | writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control); |
| 234 | /* Configure ecc size for BCH */ |
| 235 | ecc_size_config_val = (eccsize1 << 22) | (eccsize0 << 12); |
| 236 | writel(ecc_size_config_val, &gpmc_cfg->ecc_size_config); |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 237 | |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 238 | /* Configure device details for BCH engine */ |
| 239 | ecc_config_val = ((ecc_algo << 16) | /* HAM1 | BCHx */ |
| 240 | (bch_type << 12) | /* BCH4/BCH8/BCH16 */ |
| 241 | (bch_wrapmode << 8) | /* wrap mode */ |
| 242 | (dev_width << 7) | /* bus width */ |
| 243 | (0x0 << 4) | /* number of sectors */ |
| 244 | (cs << 1) | /* ECC CS */ |
| 245 | (0x1)); /* enable ECC */ |
| 246 | writel(ecc_config_val, &gpmc_cfg->ecc_config); |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 250 | * omap_calculate_ecc - Read ECC result |
| 251 | * @mtd: MTD structure |
| 252 | * @dat: unused |
| 253 | * @ecc_code: ecc_code buffer |
| 254 | * Using noninverted ECC can be considered ugly since writing a blank |
| 255 | * page ie. padding will clear the ECC bytes. This is no problem as |
| 256 | * long nobody is trying to write data on the seemingly unused page. |
| 257 | * Reading an erased page will produce an ECC mismatch between |
| 258 | * generated and read ECC bytes that has to be dealt with separately. |
| 259 | * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC |
| 260 | * is used, the result of read will be 0x0 while the ECC offsets of the |
| 261 | * spare area will be 0xFF which will result in an ECC mismatch. |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 262 | */ |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 263 | static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat, |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 264 | uint8_t *ecc_code) |
| 265 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 266 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 267 | struct omap_nand_info *info = nand_get_controller_data(chip); |
Ladislav Michl | d5b1c27 | 2016-07-12 20:28:16 +0200 | [diff] [blame] | 268 | const uint32_t *ptr; |
| 269 | uint32_t val = 0; |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 270 | int8_t i = 0, j; |
| 271 | |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 272 | switch (info->ecc_scheme) { |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 273 | case OMAP_ECC_HAM1_CODE_HW: |
| 274 | val = readl(&gpmc_cfg->ecc1_result); |
| 275 | ecc_code[0] = val & 0xFF; |
| 276 | ecc_code[1] = (val >> 16) & 0xFF; |
| 277 | ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0); |
| 278 | break; |
| 279 | #ifdef CONFIG_BCH |
| 280 | case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW: |
| 281 | #endif |
| 282 | case OMAP_ECC_BCH8_CODE_HW: |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 283 | ptr = &gpmc_cfg->bch_result_0_3[0].bch_result_x[3]; |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 284 | val = readl(ptr); |
| 285 | ecc_code[i++] = (val >> 0) & 0xFF; |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 286 | ptr--; |
| 287 | for (j = 0; j < 3; j++) { |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 288 | val = readl(ptr); |
| 289 | ecc_code[i++] = (val >> 24) & 0xFF; |
| 290 | ecc_code[i++] = (val >> 16) & 0xFF; |
| 291 | ecc_code[i++] = (val >> 8) & 0xFF; |
| 292 | ecc_code[i++] = (val >> 0) & 0xFF; |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 293 | ptr--; |
| 294 | } |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 295 | break; |
pekon gupta | 046cf86 | 2014-06-02 17:14:42 +0530 | [diff] [blame] | 296 | case OMAP_ECC_BCH16_CODE_HW: |
| 297 | val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[2]); |
| 298 | ecc_code[i++] = (val >> 8) & 0xFF; |
| 299 | ecc_code[i++] = (val >> 0) & 0xFF; |
| 300 | val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[1]); |
| 301 | ecc_code[i++] = (val >> 24) & 0xFF; |
| 302 | ecc_code[i++] = (val >> 16) & 0xFF; |
| 303 | ecc_code[i++] = (val >> 8) & 0xFF; |
| 304 | ecc_code[i++] = (val >> 0) & 0xFF; |
| 305 | val = readl(&gpmc_cfg->bch_result_4_6[0].bch_result_x[0]); |
| 306 | ecc_code[i++] = (val >> 24) & 0xFF; |
| 307 | ecc_code[i++] = (val >> 16) & 0xFF; |
| 308 | ecc_code[i++] = (val >> 8) & 0xFF; |
| 309 | ecc_code[i++] = (val >> 0) & 0xFF; |
| 310 | for (j = 3; j >= 0; j--) { |
| 311 | val = readl(&gpmc_cfg->bch_result_0_3[0].bch_result_x[j] |
| 312 | ); |
| 313 | ecc_code[i++] = (val >> 24) & 0xFF; |
| 314 | ecc_code[i++] = (val >> 16) & 0xFF; |
| 315 | ecc_code[i++] = (val >> 8) & 0xFF; |
| 316 | ecc_code[i++] = (val >> 0) & 0xFF; |
| 317 | } |
| 318 | break; |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 319 | default: |
| 320 | return -EINVAL; |
| 321 | } |
| 322 | /* ECC scheme specific syndrome customizations */ |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 323 | switch (info->ecc_scheme) { |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 324 | case OMAP_ECC_HAM1_CODE_HW: |
| 325 | break; |
| 326 | #ifdef CONFIG_BCH |
| 327 | case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW: |
| 328 | |
| 329 | for (i = 0; i < chip->ecc.bytes; i++) |
| 330 | *(ecc_code + i) = *(ecc_code + i) ^ |
| 331 | bch8_polynomial[i]; |
| 332 | break; |
| 333 | #endif |
| 334 | case OMAP_ECC_BCH8_CODE_HW: |
| 335 | ecc_code[chip->ecc.bytes - 1] = 0x00; |
| 336 | break; |
pekon gupta | 046cf86 | 2014-06-02 17:14:42 +0530 | [diff] [blame] | 337 | case OMAP_ECC_BCH16_CODE_HW: |
| 338 | break; |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 339 | default: |
| 340 | return -EINVAL; |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 341 | } |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 342 | return 0; |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Jeroen Hofstee | f2bf571 | 2015-05-30 10:11:23 +0200 | [diff] [blame] | 345 | #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH |
| 346 | |
| 347 | #define PREFETCH_CONFIG1_CS_SHIFT 24 |
| 348 | #define PREFETCH_FIFOTHRESHOLD_MAX 0x40 |
| 349 | #define PREFETCH_FIFOTHRESHOLD(val) ((val) << 8) |
| 350 | #define PREFETCH_STATUS_COUNT(val) (val & 0x00003fff) |
| 351 | #define PREFETCH_STATUS_FIFO_CNT(val) ((val >> 24) & 0x7F) |
| 352 | #define ENABLE_PREFETCH (1 << 7) |
| 353 | |
| 354 | /** |
| 355 | * omap_prefetch_enable - configures and starts prefetch transfer |
| 356 | * @fifo_th: fifo threshold to be used for read/ write |
| 357 | * @count: number of bytes to be transferred |
| 358 | * @is_write: prefetch read(0) or write post(1) mode |
| 359 | * @cs: chip select to use |
| 360 | */ |
| 361 | static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs) |
| 362 | { |
| 363 | uint32_t val; |
| 364 | |
| 365 | if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) |
| 366 | return -EINVAL; |
| 367 | |
| 368 | if (readl(&gpmc_cfg->prefetch_control)) |
| 369 | return -EBUSY; |
| 370 | |
| 371 | /* Set the amount of bytes to be prefetched */ |
| 372 | writel(count, &gpmc_cfg->prefetch_config2); |
| 373 | |
| 374 | val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) | |
| 375 | PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH; |
| 376 | writel(val, &gpmc_cfg->prefetch_config1); |
| 377 | |
| 378 | /* Start the prefetch engine */ |
| 379 | writel(1, &gpmc_cfg->prefetch_control); |
| 380 | |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * omap_prefetch_reset - disables and stops the prefetch engine |
| 386 | */ |
| 387 | static void omap_prefetch_reset(void) |
| 388 | { |
| 389 | writel(0, &gpmc_cfg->prefetch_control); |
| 390 | writel(0, &gpmc_cfg->prefetch_config1); |
| 391 | } |
| 392 | |
| 393 | static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len) |
| 394 | { |
| 395 | int ret; |
| 396 | uint32_t cnt; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 397 | struct omap_nand_info *info = nand_get_controller_data(chip); |
Jeroen Hofstee | f2bf571 | 2015-05-30 10:11:23 +0200 | [diff] [blame] | 398 | |
| 399 | ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs); |
| 400 | if (ret < 0) |
| 401 | return ret; |
| 402 | |
| 403 | do { |
| 404 | int i; |
| 405 | |
| 406 | cnt = readl(&gpmc_cfg->prefetch_status); |
| 407 | cnt = PREFETCH_STATUS_FIFO_CNT(cnt); |
| 408 | |
| 409 | for (i = 0; i < cnt / 4; i++) { |
| 410 | *buf++ = readl(CONFIG_SYS_NAND_BASE); |
| 411 | len -= 4; |
| 412 | } |
| 413 | } while (len); |
| 414 | |
| 415 | omap_prefetch_reset(); |
| 416 | |
| 417 | return 0; |
| 418 | } |
| 419 | |
Jeroen Hofstee | 5e67ac7 | 2015-05-30 10:11:24 +0200 | [diff] [blame] | 420 | static inline void omap_nand_read(struct mtd_info *mtd, uint8_t *buf, int len) |
| 421 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 422 | struct nand_chip *chip = mtd_to_nand(mtd); |
Jeroen Hofstee | 5e67ac7 | 2015-05-30 10:11:24 +0200 | [diff] [blame] | 423 | |
| 424 | if (chip->options & NAND_BUSWIDTH_16) |
| 425 | nand_read_buf16(mtd, buf, len); |
| 426 | else |
| 427 | nand_read_buf(mtd, buf, len); |
| 428 | } |
| 429 | |
| 430 | static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len) |
Jeroen Hofstee | f2bf571 | 2015-05-30 10:11:23 +0200 | [diff] [blame] | 431 | { |
| 432 | int ret; |
| 433 | uint32_t head, tail; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 434 | struct nand_chip *chip = mtd_to_nand(mtd); |
Jeroen Hofstee | f2bf571 | 2015-05-30 10:11:23 +0200 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * If the destination buffer is unaligned, start with reading |
| 438 | * the overlap byte-wise. |
| 439 | */ |
| 440 | head = ((uint32_t) buf) % 4; |
| 441 | if (head) { |
Jeroen Hofstee | 5e67ac7 | 2015-05-30 10:11:24 +0200 | [diff] [blame] | 442 | omap_nand_read(mtd, buf, head); |
Jeroen Hofstee | f2bf571 | 2015-05-30 10:11:23 +0200 | [diff] [blame] | 443 | buf += head; |
| 444 | len -= head; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * Only transfer multiples of 4 bytes in a pre-fetched fashion. |
| 449 | * If there's a residue, care for it byte-wise afterwards. |
| 450 | */ |
| 451 | tail = len % 4; |
| 452 | |
Jeroen Hofstee | 5e67ac7 | 2015-05-30 10:11:24 +0200 | [diff] [blame] | 453 | ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail); |
Jeroen Hofstee | f2bf571 | 2015-05-30 10:11:23 +0200 | [diff] [blame] | 454 | if (ret < 0) { |
| 455 | /* fallback in case the prefetch engine is busy */ |
Jeroen Hofstee | 5e67ac7 | 2015-05-30 10:11:24 +0200 | [diff] [blame] | 456 | omap_nand_read(mtd, buf, len); |
Jeroen Hofstee | f2bf571 | 2015-05-30 10:11:23 +0200 | [diff] [blame] | 457 | } else if (tail) { |
| 458 | buf += len - tail; |
Jeroen Hofstee | 5e67ac7 | 2015-05-30 10:11:24 +0200 | [diff] [blame] | 459 | omap_nand_read(mtd, buf, tail); |
Jeroen Hofstee | f2bf571 | 2015-05-30 10:11:23 +0200 | [diff] [blame] | 460 | } |
| 461 | } |
| 462 | #endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */ |
| 463 | |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 464 | #ifdef CONFIG_NAND_OMAP_ELM |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 465 | /* |
Jeroen Hofstee | 7936907 | 2014-10-08 22:57:42 +0200 | [diff] [blame] | 466 | * omap_reverse_list - re-orders list elements in reverse order [internal] |
| 467 | * @list: pointer to start of list |
| 468 | * @length: length of list |
| 469 | */ |
| 470 | static void omap_reverse_list(u8 *list, unsigned int length) |
| 471 | { |
| 472 | unsigned int i, j; |
| 473 | unsigned int half_length = length / 2; |
| 474 | u8 tmp; |
| 475 | for (i = 0, j = length - 1; i < half_length; i++, j--) { |
| 476 | tmp = list[i]; |
| 477 | list[i] = list[j]; |
| 478 | list[j] = tmp; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /* |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 483 | * omap_correct_data_bch - Compares the ecc read from nand spare area |
Vagrant Cascadian | edfdb99 | 2016-04-30 19:18:00 -0700 | [diff] [blame] | 484 | * with ECC registers values and corrects one bit error if it has occurred |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 485 | * |
| 486 | * @mtd: MTD device structure |
| 487 | * @dat: page data |
| 488 | * @read_ecc: ecc read from nand flash (ignored) |
| 489 | * @calc_ecc: ecc read from ECC registers |
| 490 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 491 | * Return: 0 if data is OK or corrected, else returns -1 |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 492 | */ |
| 493 | static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat, |
| 494 | uint8_t *read_ecc, uint8_t *calc_ecc) |
| 495 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 496 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 497 | struct omap_nand_info *info = nand_get_controller_data(chip); |
pekon gupta | 3c43c5b | 2014-04-11 12:55:34 +0530 | [diff] [blame] | 498 | struct nand_ecc_ctrl *ecc = &chip->ecc; |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 499 | uint32_t error_count = 0, error_max; |
pekon gupta | 046cf86 | 2014-06-02 17:14:42 +0530 | [diff] [blame] | 500 | uint32_t error_loc[ELM_MAX_ERROR_COUNT]; |
pekon gupta | 9d4b747 | 2014-04-11 12:55:32 +0530 | [diff] [blame] | 501 | enum bch_level bch_type; |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 502 | uint32_t i, ecc_flag = 0; |
Guido Martínez | 20b27be | 2015-01-02 14:49:10 -0300 | [diff] [blame] | 503 | uint8_t count; |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 504 | uint32_t byte_pos, bit_pos; |
Guido Martínez | 20b27be | 2015-01-02 14:49:10 -0300 | [diff] [blame] | 505 | int err = 0; |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 506 | |
| 507 | /* check calculated ecc */ |
pekon gupta | 3c43c5b | 2014-04-11 12:55:34 +0530 | [diff] [blame] | 508 | for (i = 0; i < ecc->bytes && !ecc_flag; i++) { |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 509 | if (calc_ecc[i] != 0x00) |
David Rivshin | efa7853 | 2021-11-18 13:25:24 -0500 | [diff] [blame] | 510 | goto not_ecc_match; |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 511 | } |
David Rivshin | efa7853 | 2021-11-18 13:25:24 -0500 | [diff] [blame] | 512 | return 0; |
| 513 | not_ecc_match: |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 514 | |
David Rivshin | efa7853 | 2021-11-18 13:25:24 -0500 | [diff] [blame] | 515 | /* check for whether it's an erased-page */ |
| 516 | for (i = 0; i < ecc->bytes; i++) { |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 517 | if (read_ecc[i] != 0xff) |
David Rivshin | efa7853 | 2021-11-18 13:25:24 -0500 | [diff] [blame] | 518 | goto not_erased; |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 519 | } |
David Rivshin | efa7853 | 2021-11-18 13:25:24 -0500 | [diff] [blame] | 520 | for (i = 0; i < SECTOR_BYTES; i++) { |
| 521 | if (dat[i] != 0xff) |
| 522 | goto not_erased; |
| 523 | } |
| 524 | return 0; |
| 525 | not_erased: |
| 526 | |
| 527 | /* |
| 528 | * Check for whether it's an erased page with a correctable |
| 529 | * number of bitflips. Erased pages have all 1's in the data, |
| 530 | * so we just compute the number of 0 bits in the data and |
| 531 | * see if it's under the correction threshold. |
| 532 | * |
| 533 | * NOTE: The check for a perfect erased page above is faster for |
| 534 | * the more common case, even though it's logically redundant. |
| 535 | */ |
| 536 | for (i = 0; i < ecc->bytes; i++) |
| 537 | error_count += hweight8(~read_ecc[i]); |
| 538 | |
| 539 | for (i = 0; i < SECTOR_BYTES; i++) |
| 540 | error_count += hweight8(~dat[i]); |
| 541 | |
| 542 | if (error_count <= ecc->strength) { |
| 543 | memset(read_ecc, 0xFF, ecc->bytes); |
| 544 | memset(dat, 0xFF, SECTOR_BYTES); |
| 545 | debug("nand: %u bit-flip(s) corrected in erased page\n", |
| 546 | error_count); |
| 547 | return error_count; |
| 548 | } |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 549 | |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 550 | /* |
| 551 | * while reading ECC result we read it in big endian. |
| 552 | * Hence while loading to ELM we have rotate to get the right endian. |
| 553 | */ |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 554 | switch (info->ecc_scheme) { |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 555 | case OMAP_ECC_BCH8_CODE_HW: |
pekon gupta | 9d4b747 | 2014-04-11 12:55:32 +0530 | [diff] [blame] | 556 | bch_type = BCH_8_BIT; |
pekon gupta | 3c43c5b | 2014-04-11 12:55:34 +0530 | [diff] [blame] | 557 | omap_reverse_list(calc_ecc, ecc->bytes - 1); |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 558 | break; |
pekon gupta | 046cf86 | 2014-06-02 17:14:42 +0530 | [diff] [blame] | 559 | case OMAP_ECC_BCH16_CODE_HW: |
| 560 | bch_type = BCH_16_BIT; |
| 561 | omap_reverse_list(calc_ecc, ecc->bytes); |
| 562 | break; |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 563 | default: |
| 564 | return -EINVAL; |
| 565 | } |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 566 | /* use elm module to check for errors */ |
pekon gupta | 9d4b747 | 2014-04-11 12:55:32 +0530 | [diff] [blame] | 567 | elm_config(bch_type); |
David Rivshin | efa7853 | 2021-11-18 13:25:24 -0500 | [diff] [blame] | 568 | error_count = 0; |
pekon gupta | cfe6b8a | 2014-04-11 12:55:35 +0530 | [diff] [blame] | 569 | err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc); |
| 570 | if (err) |
| 571 | return err; |
| 572 | |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 573 | /* correct bch error */ |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 574 | for (count = 0; count < error_count; count++) { |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 575 | switch (info->ecc_scheme) { |
pekon gupta | 9d4b747 | 2014-04-11 12:55:32 +0530 | [diff] [blame] | 576 | case OMAP_ECC_BCH8_CODE_HW: |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 577 | /* 14th byte in ECC is reserved to match ROM layout */ |
pekon gupta | 3c43c5b | 2014-04-11 12:55:34 +0530 | [diff] [blame] | 578 | error_max = SECTOR_BYTES + (ecc->bytes - 1); |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 579 | break; |
pekon gupta | 046cf86 | 2014-06-02 17:14:42 +0530 | [diff] [blame] | 580 | case OMAP_ECC_BCH16_CODE_HW: |
| 581 | error_max = SECTOR_BYTES + ecc->bytes; |
| 582 | break; |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 583 | default: |
| 584 | return -EINVAL; |
| 585 | } |
| 586 | byte_pos = error_max - (error_loc[count] / 8) - 1; |
| 587 | bit_pos = error_loc[count] % 8; |
| 588 | if (byte_pos < SECTOR_BYTES) { |
| 589 | dat[byte_pos] ^= 1 << bit_pos; |
Ezequiel García | 69cf8ad | 2015-10-04 18:34:42 -0300 | [diff] [blame] | 590 | debug("nand: bit-flip corrected @data=%d\n", byte_pos); |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 591 | } else if (byte_pos < error_max) { |
Belisko Marek | 9ab5414 | 2014-04-25 12:00:07 +0200 | [diff] [blame] | 592 | read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos; |
Ezequiel García | 69cf8ad | 2015-10-04 18:34:42 -0300 | [diff] [blame] | 593 | debug("nand: bit-flip corrected @oob=%d\n", byte_pos - |
pekon gupta | b0f750a | 2013-11-19 11:02:17 +0530 | [diff] [blame] | 594 | SECTOR_BYTES); |
| 595 | } else { |
| 596 | err = -EBADMSG; |
| 597 | printf("nand: error: invalid bit-flip location\n"); |
| 598 | } |
| 599 | } |
| 600 | return (err) ? err : error_count; |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 601 | } |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 602 | |
| 603 | /** |
| 604 | * omap_read_page_bch - hardware ecc based page read function |
| 605 | * @mtd: mtd info structure |
| 606 | * @chip: nand chip info structure |
| 607 | * @buf: buffer to store read data |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 608 | * @oob_required: caller expects OOB data read to chip->oob_poi |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 609 | * @page: page number to read |
| 610 | * |
| 611 | */ |
| 612 | static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip, |
Sergey Lapin | 3a38a55 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 613 | uint8_t *buf, int oob_required, int page) |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 614 | { |
| 615 | int i, eccsize = chip->ecc.size; |
| 616 | int eccbytes = chip->ecc.bytes; |
| 617 | int eccsteps = chip->ecc.steps; |
| 618 | uint8_t *p = buf; |
| 619 | uint8_t *ecc_calc = chip->buffers->ecccalc; |
| 620 | uint8_t *ecc_code = chip->buffers->ecccode; |
| 621 | uint32_t *eccpos = chip->ecc.layout->eccpos; |
| 622 | uint8_t *oob = chip->oob_poi; |
| 623 | uint32_t data_pos; |
| 624 | uint32_t oob_pos; |
| 625 | |
| 626 | data_pos = 0; |
| 627 | /* oob area start */ |
| 628 | oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0]; |
| 629 | oob += chip->ecc.layout->eccpos[0]; |
| 630 | |
| 631 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize, |
| 632 | oob += eccbytes) { |
| 633 | chip->ecc.hwctl(mtd, NAND_ECC_READ); |
| 634 | /* read data */ |
Rostislav Lisovy | a9ee70a | 2014-09-02 17:00:30 +0200 | [diff] [blame] | 635 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, data_pos, -1); |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 636 | chip->read_buf(mtd, p, eccsize); |
| 637 | |
| 638 | /* read respective ecc from oob area */ |
Rostislav Lisovy | a9ee70a | 2014-09-02 17:00:30 +0200 | [diff] [blame] | 639 | chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1); |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 640 | chip->read_buf(mtd, oob, eccbytes); |
| 641 | /* read syndrome */ |
| 642 | chip->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 643 | |
| 644 | data_pos += eccsize; |
| 645 | oob_pos += eccbytes; |
| 646 | } |
| 647 | |
| 648 | for (i = 0; i < chip->ecc.total; i++) |
| 649 | ecc_code[i] = chip->oob_poi[eccpos[i]]; |
| 650 | |
| 651 | eccsteps = chip->ecc.steps; |
| 652 | p = buf; |
| 653 | |
| 654 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 655 | int stat; |
| 656 | |
| 657 | stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
| 658 | if (stat < 0) |
| 659 | mtd->ecc_stats.failed++; |
| 660 | else |
| 661 | mtd->ecc_stats.corrected += stat; |
| 662 | } |
| 663 | return 0; |
| 664 | } |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 665 | #endif /* CONFIG_NAND_OMAP_ELM */ |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 666 | |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 667 | /* |
| 668 | * OMAP3 BCH8 support (with BCH library) |
| 669 | */ |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 670 | #ifdef CONFIG_BCH |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 671 | /** |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 672 | * omap_correct_data_bch_sw - Decode received data and correct errors |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 673 | * @mtd: MTD device structure |
| 674 | * @data: page data |
| 675 | * @read_ecc: ecc read from nand flash |
| 676 | * @calc_ecc: ecc read from HW ECC registers |
| 677 | */ |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 678 | static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data, |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 679 | u_char *read_ecc, u_char *calc_ecc) |
| 680 | { |
| 681 | int i, count; |
| 682 | /* cannot correct more than 8 errors */ |
| 683 | unsigned int errloc[8]; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 684 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 685 | struct omap_nand_info *info = nand_get_controller_data(chip); |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 686 | |
Ladislav Michl | 6cc5185 | 2017-01-09 11:15:14 +0100 | [diff] [blame] | 687 | count = decode_bch(info->control, NULL, SECTOR_BYTES, |
| 688 | read_ecc, calc_ecc, NULL, errloc); |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 689 | if (count > 0) { |
| 690 | /* correct errors */ |
| 691 | for (i = 0; i < count; i++) { |
| 692 | /* correct data only, not ecc bytes */ |
Ladislav Michl | 6cc5185 | 2017-01-09 11:15:14 +0100 | [diff] [blame] | 693 | if (errloc[i] < SECTOR_BYTES << 3) |
| 694 | data[errloc[i] >> 3] ^= 1 << (errloc[i] & 7); |
Ezequiel García | 69cf8ad | 2015-10-04 18:34:42 -0300 | [diff] [blame] | 695 | debug("corrected bitflip %u\n", errloc[i]); |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 696 | #ifdef DEBUG |
| 697 | puts("read_ecc: "); |
| 698 | /* |
| 699 | * BCH8 have 13 bytes of ECC; BCH4 needs adoption |
| 700 | * here! |
| 701 | */ |
| 702 | for (i = 0; i < 13; i++) |
| 703 | printf("%02x ", read_ecc[i]); |
| 704 | puts("\n"); |
| 705 | puts("calc_ecc: "); |
| 706 | for (i = 0; i < 13; i++) |
| 707 | printf("%02x ", calc_ecc[i]); |
| 708 | puts("\n"); |
| 709 | #endif |
| 710 | } |
| 711 | } else if (count < 0) { |
| 712 | puts("ecc unrecoverable error\n"); |
| 713 | } |
| 714 | return count; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * omap_free_bch - Release BCH ecc resources |
| 719 | * @mtd: MTD device structure |
| 720 | */ |
| 721 | static void __maybe_unused omap_free_bch(struct mtd_info *mtd) |
| 722 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 723 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 724 | struct omap_nand_info *info = nand_get_controller_data(chip); |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 725 | |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 726 | if (info->control) { |
| 727 | free_bch(info->control); |
| 728 | info->control = NULL; |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 731 | #endif /* CONFIG_BCH */ |
| 732 | |
| 733 | /** |
| 734 | * omap_select_ecc_scheme - configures driver for particular ecc-scheme |
| 735 | * @nand: NAND chip device structure |
| 736 | * @ecc_scheme: ecc scheme to configure |
| 737 | * @pagesize: number of main-area bytes per page of NAND device |
| 738 | * @oobsize: number of OOB/spare bytes per page of NAND device |
| 739 | */ |
| 740 | static int omap_select_ecc_scheme(struct nand_chip *nand, |
| 741 | enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 742 | struct omap_nand_info *info = nand_get_controller_data(nand); |
Nikita Kiryanov | e816789 | 2013-12-16 19:19:01 +0200 | [diff] [blame] | 743 | struct nand_ecclayout *ecclayout = &omap_ecclayout; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 744 | int eccsteps = pagesize / SECTOR_BYTES; |
| 745 | int i; |
| 746 | |
| 747 | switch (ecc_scheme) { |
| 748 | case OMAP_ECC_HAM1_CODE_SW: |
| 749 | debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n"); |
| 750 | /* For this ecc-scheme, ecc.bytes, ecc.layout, ... are |
| 751 | * initialized in nand_scan_tail(), so just set ecc.mode */ |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 752 | info->control = NULL; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 753 | nand->ecc.mode = NAND_ECC_SOFT; |
| 754 | nand->ecc.layout = NULL; |
Nikita Kiryanov | 4110c82 | 2013-12-12 15:19:31 +0200 | [diff] [blame] | 755 | nand->ecc.size = 0; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 756 | break; |
| 757 | |
| 758 | case OMAP_ECC_HAM1_CODE_HW: |
| 759 | debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n"); |
| 760 | /* check ecc-scheme requirements before updating ecc info */ |
| 761 | if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) { |
| 762 | printf("nand: error: insufficient OOB: require=%d\n", ( |
| 763 | (3 * eccsteps) + BADBLOCK_MARKER_LENGTH)); |
| 764 | return -EINVAL; |
| 765 | } |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 766 | info->control = NULL; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 767 | /* populate ecc specific fields */ |
Nikita Kiryanov | 2e18ff2 | 2013-12-17 15:18:01 +0200 | [diff] [blame] | 768 | memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl)); |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 769 | nand->ecc.mode = NAND_ECC_HW; |
| 770 | nand->ecc.strength = 1; |
| 771 | nand->ecc.size = SECTOR_BYTES; |
| 772 | nand->ecc.bytes = 3; |
| 773 | nand->ecc.hwctl = omap_enable_hwecc; |
| 774 | nand->ecc.correct = omap_correct_data; |
| 775 | nand->ecc.calculate = omap_calculate_ecc; |
| 776 | /* define ecc-layout */ |
| 777 | ecclayout->eccbytes = nand->ecc.bytes * eccsteps; |
pekon gupta | f0aff02 | 2013-12-05 17:54:21 +0530 | [diff] [blame] | 778 | for (i = 0; i < ecclayout->eccbytes; i++) { |
| 779 | if (nand->options & NAND_BUSWIDTH_16) |
| 780 | ecclayout->eccpos[i] = i + 2; |
| 781 | else |
| 782 | ecclayout->eccpos[i] = i + 1; |
| 783 | } |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 784 | ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH; |
| 785 | ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes - |
| 786 | BADBLOCK_MARKER_LENGTH; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 787 | break; |
| 788 | |
| 789 | case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW: |
| 790 | #ifdef CONFIG_BCH |
| 791 | debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n"); |
| 792 | /* check ecc-scheme requirements before updating ecc info */ |
| 793 | if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) { |
| 794 | printf("nand: error: insufficient OOB: require=%d\n", ( |
| 795 | (13 * eccsteps) + BADBLOCK_MARKER_LENGTH)); |
| 796 | return -EINVAL; |
| 797 | } |
| 798 | /* check if BCH S/W library can be used for error detection */ |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 799 | info->control = init_bch(13, 8, 0x201b); |
| 800 | if (!info->control) { |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 801 | printf("nand: error: could not init_bch()\n"); |
| 802 | return -ENODEV; |
| 803 | } |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 804 | /* populate ecc specific fields */ |
Nikita Kiryanov | 2e18ff2 | 2013-12-17 15:18:01 +0200 | [diff] [blame] | 805 | memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl)); |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 806 | nand->ecc.mode = NAND_ECC_HW; |
| 807 | nand->ecc.strength = 8; |
| 808 | nand->ecc.size = SECTOR_BYTES; |
| 809 | nand->ecc.bytes = 13; |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 810 | nand->ecc.hwctl = omap_enable_hwecc; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 811 | nand->ecc.correct = omap_correct_data_bch_sw; |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 812 | nand->ecc.calculate = omap_calculate_ecc; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 813 | /* define ecc-layout */ |
| 814 | ecclayout->eccbytes = nand->ecc.bytes * eccsteps; |
| 815 | ecclayout->eccpos[0] = BADBLOCK_MARKER_LENGTH; |
| 816 | for (i = 1; i < ecclayout->eccbytes; i++) { |
| 817 | if (i % nand->ecc.bytes) |
| 818 | ecclayout->eccpos[i] = |
| 819 | ecclayout->eccpos[i - 1] + 1; |
| 820 | else |
| 821 | ecclayout->eccpos[i] = |
| 822 | ecclayout->eccpos[i - 1] + 2; |
| 823 | } |
| 824 | ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH; |
| 825 | ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes - |
| 826 | BADBLOCK_MARKER_LENGTH; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 827 | break; |
| 828 | #else |
| 829 | printf("nand: error: CONFIG_BCH required for ECC\n"); |
| 830 | return -EINVAL; |
| 831 | #endif |
| 832 | |
| 833 | case OMAP_ECC_BCH8_CODE_HW: |
| 834 | #ifdef CONFIG_NAND_OMAP_ELM |
| 835 | debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n"); |
| 836 | /* check ecc-scheme requirements before updating ecc info */ |
| 837 | if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) { |
| 838 | printf("nand: error: insufficient OOB: require=%d\n", ( |
| 839 | (14 * eccsteps) + BADBLOCK_MARKER_LENGTH)); |
| 840 | return -EINVAL; |
| 841 | } |
| 842 | /* intialize ELM for ECC error detection */ |
| 843 | elm_init(); |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 844 | info->control = NULL; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 845 | /* populate ecc specific fields */ |
Nikita Kiryanov | 2e18ff2 | 2013-12-17 15:18:01 +0200 | [diff] [blame] | 846 | memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl)); |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 847 | nand->ecc.mode = NAND_ECC_HW; |
| 848 | nand->ecc.strength = 8; |
| 849 | nand->ecc.size = SECTOR_BYTES; |
| 850 | nand->ecc.bytes = 14; |
pekon gupta | eff10ee | 2013-11-19 11:02:15 +0530 | [diff] [blame] | 851 | nand->ecc.hwctl = omap_enable_hwecc; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 852 | nand->ecc.correct = omap_correct_data_bch; |
pekon gupta | 03742c9 | 2013-11-19 11:02:16 +0530 | [diff] [blame] | 853 | nand->ecc.calculate = omap_calculate_ecc; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 854 | nand->ecc.read_page = omap_read_page_bch; |
| 855 | /* define ecc-layout */ |
| 856 | ecclayout->eccbytes = nand->ecc.bytes * eccsteps; |
| 857 | for (i = 0; i < ecclayout->eccbytes; i++) |
| 858 | ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH; |
| 859 | ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH; |
| 860 | ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes - |
| 861 | BADBLOCK_MARKER_LENGTH; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 862 | break; |
| 863 | #else |
| 864 | printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n"); |
| 865 | return -EINVAL; |
| 866 | #endif |
| 867 | |
pekon gupta | 046cf86 | 2014-06-02 17:14:42 +0530 | [diff] [blame] | 868 | case OMAP_ECC_BCH16_CODE_HW: |
| 869 | #ifdef CONFIG_NAND_OMAP_ELM |
| 870 | debug("nand: using OMAP_ECC_BCH16_CODE_HW\n"); |
| 871 | /* check ecc-scheme requirements before updating ecc info */ |
| 872 | if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) { |
| 873 | printf("nand: error: insufficient OOB: require=%d\n", ( |
| 874 | (26 * eccsteps) + BADBLOCK_MARKER_LENGTH)); |
| 875 | return -EINVAL; |
| 876 | } |
| 877 | /* intialize ELM for ECC error detection */ |
| 878 | elm_init(); |
| 879 | /* populate ecc specific fields */ |
| 880 | nand->ecc.mode = NAND_ECC_HW; |
| 881 | nand->ecc.size = SECTOR_BYTES; |
| 882 | nand->ecc.bytes = 26; |
| 883 | nand->ecc.strength = 16; |
| 884 | nand->ecc.hwctl = omap_enable_hwecc; |
| 885 | nand->ecc.correct = omap_correct_data_bch; |
| 886 | nand->ecc.calculate = omap_calculate_ecc; |
| 887 | nand->ecc.read_page = omap_read_page_bch; |
| 888 | /* define ecc-layout */ |
| 889 | ecclayout->eccbytes = nand->ecc.bytes * eccsteps; |
| 890 | for (i = 0; i < ecclayout->eccbytes; i++) |
| 891 | ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH; |
| 892 | ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH; |
| 893 | ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes - |
| 894 | BADBLOCK_MARKER_LENGTH; |
| 895 | break; |
| 896 | #else |
| 897 | printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n"); |
| 898 | return -EINVAL; |
| 899 | #endif |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 900 | default: |
| 901 | debug("nand: error: ecc scheme not enabled or supported\n"); |
| 902 | return -EINVAL; |
| 903 | } |
Nikita Kiryanov | e816789 | 2013-12-16 19:19:01 +0200 | [diff] [blame] | 904 | |
| 905 | /* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */ |
| 906 | if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW) |
| 907 | nand->ecc.layout = ecclayout; |
| 908 | |
pekon gupta | aa16848 | 2014-04-11 12:55:33 +0530 | [diff] [blame] | 909 | info->ecc_scheme = ecc_scheme; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 910 | return 0; |
| 911 | } |
Andreas Bießmann | 82a6547 | 2013-04-05 04:55:21 +0000 | [diff] [blame] | 912 | |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 913 | #ifndef CONFIG_SPL_BUILD |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 914 | /* |
Andreas Bießmann | 1e4eccf | 2013-04-04 23:52:50 +0000 | [diff] [blame] | 915 | * omap_nand_switch_ecc - switch the ECC operation between different engines |
| 916 | * (h/w and s/w) and different algorithms (hamming and BCHx) |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 917 | * |
Andreas Bießmann | 1e4eccf | 2013-04-04 23:52:50 +0000 | [diff] [blame] | 918 | * @hardware - true if one of the HW engines should be used |
| 919 | * @eccstrength - the number of bits that could be corrected |
| 920 | * (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16) |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 921 | */ |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 922 | int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength) |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 923 | { |
| 924 | struct nand_chip *nand; |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 925 | struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device); |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 926 | int err = 0; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 927 | |
Mugunthan V N | 7b670cc | 2017-06-26 19:12:51 -0500 | [diff] [blame] | 928 | if (!mtd) { |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 929 | printf("nand: error: no NAND devices found\n"); |
| 930 | return -ENODEV; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 933 | nand = mtd_to_nand(mtd); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 934 | nand->options |= NAND_OWN_BUFFERS; |
Jeroen Hofstee | 96306f2 | 2014-01-15 17:58:54 +0100 | [diff] [blame] | 935 | nand->options &= ~NAND_SUBPAGE_READ; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 936 | /* Setup the ecc configurations again */ |
Andreas Bießmann | 1e4eccf | 2013-04-04 23:52:50 +0000 | [diff] [blame] | 937 | if (hardware) { |
| 938 | if (eccstrength == 1) { |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 939 | err = omap_select_ecc_scheme(nand, |
| 940 | OMAP_ECC_HAM1_CODE_HW, |
| 941 | mtd->writesize, mtd->oobsize); |
| 942 | } else if (eccstrength == 8) { |
| 943 | err = omap_select_ecc_scheme(nand, |
| 944 | OMAP_ECC_BCH8_CODE_HW, |
| 945 | mtd->writesize, mtd->oobsize); |
Heiko Schocher | 5bf904c | 2016-06-07 08:55:42 +0200 | [diff] [blame] | 946 | } else if (eccstrength == 16) { |
| 947 | err = omap_select_ecc_scheme(nand, |
| 948 | OMAP_ECC_BCH16_CODE_HW, |
| 949 | mtd->writesize, mtd->oobsize); |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 950 | } else { |
| 951 | printf("nand: error: unsupported ECC scheme\n"); |
| 952 | return -EINVAL; |
Andreas Bießmann | 1e4eccf | 2013-04-04 23:52:50 +0000 | [diff] [blame] | 953 | } |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 954 | } else { |
Ash Charles | 4a5faa8 | 2015-02-18 11:25:11 -0800 | [diff] [blame] | 955 | if (eccstrength == 1) { |
| 956 | err = omap_select_ecc_scheme(nand, |
| 957 | OMAP_ECC_HAM1_CODE_SW, |
| 958 | mtd->writesize, mtd->oobsize); |
| 959 | } else if (eccstrength == 8) { |
| 960 | err = omap_select_ecc_scheme(nand, |
| 961 | OMAP_ECC_BCH8_CODE_HW_DETECTION_SW, |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 962 | mtd->writesize, mtd->oobsize); |
Ash Charles | 4a5faa8 | 2015-02-18 11:25:11 -0800 | [diff] [blame] | 963 | } else { |
| 964 | printf("nand: error: unsupported ECC scheme\n"); |
| 965 | return -EINVAL; |
| 966 | } |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | /* Update NAND handling after ECC mode switch */ |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 970 | if (!err) |
| 971 | err = nand_scan_tail(mtd); |
| 972 | return err; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 973 | } |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 974 | #endif /* CONFIG_SPL_BUILD */ |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 975 | |
| 976 | /* |
| 977 | * Board-specific NAND initialization. The following members of the |
| 978 | * argument are board-specific: |
| 979 | * - IO_ADDR_R: address to read the 8 I/O lines of the flash device |
| 980 | * - IO_ADDR_W: address to write the 8 I/O lines of the flash device |
| 981 | * - cmd_ctrl: hardwarespecific function for accesing control-lines |
| 982 | * - waitfunc: hardwarespecific function for accesing device ready/busy line |
| 983 | * - ecc.hwctl: function to enable (reset) hardware ecc generator |
| 984 | * - ecc.mode: mode of ecc, see defines |
| 985 | * - chip_delay: chip dependent delay for transfering data from array to |
| 986 | * read regs (tR) |
| 987 | * - options: various chip options. They can partly be set to inform |
| 988 | * nand_scan about special functionality. See the defines for further |
| 989 | * explanation |
| 990 | */ |
| 991 | int board_nand_init(struct nand_chip *nand) |
| 992 | { |
| 993 | int32_t gpmc_config = 0; |
Rostislav Lisovy | 77cdf8a | 2014-09-02 16:23:58 +0200 | [diff] [blame] | 994 | int cs = cs_next++; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 995 | int err = 0; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 996 | /* |
| 997 | * xloader/Uboot's gpmc configuration would have configured GPMC for |
| 998 | * nand type of memory. The following logic scans and latches on to the |
| 999 | * first CS with NAND type memory. |
| 1000 | * TBD: need to make this logic generic to handle multiple CS NAND |
| 1001 | * devices. |
| 1002 | */ |
| 1003 | while (cs < GPMC_MAX_CS) { |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 1004 | /* Check if NAND type is set */ |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 1005 | if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) { |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 1006 | /* Found it!! */ |
| 1007 | break; |
| 1008 | } |
| 1009 | cs++; |
| 1010 | } |
| 1011 | if (cs >= GPMC_MAX_CS) { |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 1012 | printf("nand: error: Unable to find NAND settings in " |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 1013 | "GPMC Configuration - quitting\n"); |
| 1014 | return -ENODEV; |
| 1015 | } |
| 1016 | |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 1017 | gpmc_config = readl(&gpmc_cfg->config); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 1018 | /* Disable Write protect */ |
| 1019 | gpmc_config |= 0x10; |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 1020 | writel(gpmc_config, &gpmc_cfg->config); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 1021 | |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 1022 | nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat; |
| 1023 | nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd; |
Rostislav Lisovy | 77cdf8a | 2014-09-02 16:23:58 +0200 | [diff] [blame] | 1024 | omap_nand_info[cs].control = NULL; |
| 1025 | omap_nand_info[cs].cs = cs; |
Michal Sojka | 2616007 | 2015-02-17 17:08:37 +0100 | [diff] [blame] | 1026 | omap_nand_info[cs].ws = wscfg[cs]; |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 1027 | nand_set_controller_data(nand, &omap_nand_info[cs]); |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 1028 | nand->cmd_ctrl = omap_nand_hwcontrol; |
| 1029 | nand->options |= NAND_NO_PADDING | NAND_CACHEPRG; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 1030 | nand->chip_delay = 100; |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 1031 | nand->ecc.layout = &omap_ecclayout; |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 1032 | |
pekon gupta | 6250faf | 2014-05-06 00:46:19 +0530 | [diff] [blame] | 1033 | /* configure driver and controller based on NAND device bus-width */ |
| 1034 | gpmc_config = readl(&gpmc_cfg->cs[cs].config1); |
| 1035 | #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT) |
| 1036 | nand->options |= NAND_BUSWIDTH_16; |
| 1037 | writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1); |
| 1038 | #else |
| 1039 | nand->options &= ~NAND_BUSWIDTH_16; |
| 1040 | writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1); |
| 1041 | #endif |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 1042 | /* select ECC scheme */ |
pekon gupta | 3ef4973 | 2013-11-18 19:03:01 +0530 | [diff] [blame] | 1043 | #if defined(CONFIG_NAND_OMAP_ECCSCHEME) |
| 1044 | err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME, |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 1045 | CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE); |
pekon gupta | 3ef4973 | 2013-11-18 19:03:01 +0530 | [diff] [blame] | 1046 | #else |
| 1047 | /* pagesize and oobsize are not required to configure sw ecc-scheme */ |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 1048 | err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW, |
| 1049 | 0, 0); |
Mansoor Ahamed | e561251 | 2012-11-06 13:06:33 +0000 | [diff] [blame] | 1050 | #endif |
pekon gupta | 6bd91a8 | 2013-11-18 19:03:00 +0530 | [diff] [blame] | 1051 | if (err) |
| 1052 | return err; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 1053 | |
Egli, Samuel | 8645fd9 | 2015-02-13 15:47:10 +0100 | [diff] [blame] | 1054 | #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH |
Jeroen Hofstee | 5e67ac7 | 2015-05-30 10:11:24 +0200 | [diff] [blame] | 1055 | nand->read_buf = omap_nand_read_prefetch; |
Egli, Samuel | 8645fd9 | 2015-02-13 15:47:10 +0100 | [diff] [blame] | 1056 | #else |
Jeroen Hofstee | 5e67ac7 | 2015-05-30 10:11:24 +0200 | [diff] [blame] | 1057 | if (nand->options & NAND_BUSWIDTH_16) |
| 1058 | nand->read_buf = nand_read_buf16; |
Egli, Samuel | 8645fd9 | 2015-02-13 15:47:10 +0100 | [diff] [blame] | 1059 | else |
| 1060 | nand->read_buf = nand_read_buf; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 1061 | #endif |
Stefan Roese | e05972f | 2014-11-13 03:43:39 +0100 | [diff] [blame] | 1062 | |
| 1063 | nand->dev_ready = omap_dev_ready; |
| 1064 | |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 1065 | return 0; |
| 1066 | } |