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