Dirk Behme | 778933f | 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 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | */ |
| 23 | |
| 24 | #include <common.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/errno.h> |
| 27 | #include <asm/arch/mem.h> |
| 28 | #include <asm/arch/omap_gpmc.h> |
| 29 | #include <linux/mtd/nand_ecc.h> |
Stefano Babic | aade579 | 2012-03-21 23:56:17 +0000 | [diff] [blame^] | 30 | #include <linux/compiler.h> |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 31 | #include <nand.h> |
| 32 | |
| 33 | static uint8_t cs; |
Stefano Babic | aade579 | 2012-03-21 23:56:17 +0000 | [diff] [blame^] | 34 | static __maybe_unused struct nand_ecclayout hw_nand_oob = |
| 35 | GPMC_NAND_HW_ECC_LAYOUT; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * omap_nand_hwcontrol - Set the address pointers corretly for the |
| 39 | * following address/data/command operation |
| 40 | */ |
| 41 | static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd, |
| 42 | uint32_t ctrl) |
| 43 | { |
| 44 | register struct nand_chip *this = mtd->priv; |
| 45 | |
| 46 | /* |
| 47 | * Point the IO_ADDR to DATA and ADDRESS registers instead |
| 48 | * of chip address |
| 49 | */ |
| 50 | switch (ctrl) { |
| 51 | case NAND_CTRL_CHANGE | NAND_CTRL_CLE: |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 52 | this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 53 | break; |
| 54 | case NAND_CTRL_CHANGE | NAND_CTRL_ALE: |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 55 | this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 56 | break; |
| 57 | case NAND_CTRL_CHANGE | NAND_NCE: |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 58 | this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 59 | break; |
| 60 | } |
| 61 | |
| 62 | if (cmd != NAND_CMD_NONE) |
| 63 | writeb(cmd, this->IO_ADDR_W); |
| 64 | } |
| 65 | |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 66 | #ifdef CONFIG_SPL_BUILD |
| 67 | /* Check wait pin as dev ready indicator */ |
| 68 | int omap_spl_dev_ready(struct mtd_info *mtd) |
| 69 | { |
| 70 | return gpmc_cfg->status & (1 << 8); |
| 71 | } |
| 72 | #endif |
| 73 | |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 74 | /* |
| 75 | * omap_hwecc_init - Initialize the Hardware ECC for NAND flash in |
| 76 | * GPMC controller |
| 77 | * @mtd: MTD device structure |
| 78 | * |
| 79 | */ |
Stefano Babic | aade579 | 2012-03-21 23:56:17 +0000 | [diff] [blame^] | 80 | static void __maybe_unused omap_hwecc_init(struct nand_chip *chip) |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 81 | { |
| 82 | /* |
| 83 | * Init ECC Control Register |
| 84 | * Clear all ECC | Enable Reg1 |
| 85 | */ |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 86 | writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control); |
| 87 | writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL, &gpmc_cfg->ecc_size_config); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /* |
| 91 | * gen_true_ecc - This function will generate true ECC value, which |
| 92 | * can be used when correcting data read from NAND flash memory core |
| 93 | * |
| 94 | * @ecc_buf: buffer to store ecc code |
| 95 | * |
| 96 | * @return: re-formatted ECC value |
| 97 | */ |
| 98 | static uint32_t gen_true_ecc(uint8_t *ecc_buf) |
| 99 | { |
| 100 | return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) | |
| 101 | ((ecc_buf[2] & 0x0F) << 8); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * omap_correct_data - Compares the ecc read from nand spare area with ECC |
| 106 | * registers values and corrects one bit error if it has occured |
| 107 | * Further details can be had from OMAP TRM and the following selected links: |
| 108 | * http://en.wikipedia.org/wiki/Hamming_code |
| 109 | * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf |
| 110 | * |
| 111 | * @mtd: MTD device structure |
| 112 | * @dat: page data |
| 113 | * @read_ecc: ecc read from nand flash |
| 114 | * @calc_ecc: ecc read from ECC registers |
| 115 | * |
| 116 | * @return 0 if data is OK or corrected, else returns -1 |
| 117 | */ |
Stefano Babic | aade579 | 2012-03-21 23:56:17 +0000 | [diff] [blame^] | 118 | 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] | 119 | uint8_t *read_ecc, uint8_t *calc_ecc) |
| 120 | { |
| 121 | uint32_t orig_ecc, new_ecc, res, hm; |
| 122 | uint16_t parity_bits, byte; |
| 123 | uint8_t bit; |
| 124 | |
| 125 | /* Regenerate the orginal ECC */ |
| 126 | orig_ecc = gen_true_ecc(read_ecc); |
| 127 | new_ecc = gen_true_ecc(calc_ecc); |
| 128 | /* Get the XOR of real ecc */ |
| 129 | res = orig_ecc ^ new_ecc; |
| 130 | if (res) { |
| 131 | /* Get the hamming width */ |
| 132 | hm = hweight32(res); |
| 133 | /* Single bit errors can be corrected! */ |
| 134 | if (hm == 12) { |
| 135 | /* Correctable data! */ |
| 136 | parity_bits = res >> 16; |
| 137 | bit = (parity_bits & 0x7); |
| 138 | byte = (parity_bits >> 3) & 0x1FF; |
| 139 | /* Flip the bit to correct */ |
| 140 | dat[byte] ^= (0x1 << bit); |
| 141 | } else if (hm == 1) { |
| 142 | printf("Error: Ecc is wrong\n"); |
| 143 | /* ECC itself is corrupted */ |
| 144 | return 2; |
| 145 | } else { |
| 146 | /* |
| 147 | * hm distance != parity pairs OR one, could mean 2 bit |
| 148 | * error OR potentially be on a blank page.. |
| 149 | * orig_ecc: contains spare area data from nand flash. |
| 150 | * new_ecc: generated ecc while reading data area. |
| 151 | * Note: if the ecc = 0, all data bits from which it was |
| 152 | * generated are 0xFF. |
| 153 | * The 3 byte(24 bits) ecc is generated per 512byte |
| 154 | * chunk of a page. If orig_ecc(from spare area) |
| 155 | * is 0xFF && new_ecc(computed now from data area)=0x0, |
| 156 | * this means that data area is 0xFF and spare area is |
| 157 | * 0xFF. A sure sign of a erased page! |
| 158 | */ |
| 159 | if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000)) |
| 160 | return 0; |
| 161 | printf("Error: Bad compare! failed\n"); |
| 162 | /* detected 2 bit error */ |
| 163 | return -1; |
| 164 | } |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * omap_calculate_ecc - Generate non-inverted ECC bytes. |
| 171 | * |
| 172 | * Using noninverted ECC can be considered ugly since writing a blank |
| 173 | * page ie. padding will clear the ECC bytes. This is no problem as |
| 174 | * long nobody is trying to write data on the seemingly unused page. |
| 175 | * Reading an erased page will produce an ECC mismatch between |
| 176 | * generated and read ECC bytes that has to be dealt with separately. |
| 177 | * E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC |
| 178 | * is used, the result of read will be 0x0 while the ECC offsets of the |
| 179 | * spare area will be 0xFF which will result in an ECC mismatch. |
| 180 | * @mtd: MTD structure |
| 181 | * @dat: unused |
| 182 | * @ecc_code: ecc_code buffer |
| 183 | */ |
Stefano Babic | aade579 | 2012-03-21 23:56:17 +0000 | [diff] [blame^] | 184 | static int __maybe_unused omap_calculate_ecc(struct mtd_info *mtd, |
| 185 | const uint8_t *dat, uint8_t *ecc_code) |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 186 | { |
| 187 | u_int32_t val; |
| 188 | |
| 189 | /* Start Reading from HW ECC1_Result = 0x200 */ |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 190 | val = readl(&gpmc_cfg->ecc1_result); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 191 | |
| 192 | ecc_code[0] = val & 0xFF; |
| 193 | ecc_code[1] = (val >> 16) & 0xFF; |
| 194 | ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0); |
| 195 | |
| 196 | /* |
| 197 | * Stop reading anymore ECC vals and clear old results |
| 198 | * enable will be called if more reads are required |
| 199 | */ |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 200 | writel(0x000, &gpmc_cfg->ecc_config); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * omap_enable_ecc - This function enables the hardware ecc functionality |
| 207 | * @mtd: MTD device structure |
| 208 | * @mode: Read/Write mode |
| 209 | */ |
Stefano Babic | aade579 | 2012-03-21 23:56:17 +0000 | [diff] [blame^] | 210 | static void __maybe_unused omap_enable_hwecc(struct mtd_info *mtd, int32_t mode) |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 211 | { |
| 212 | struct nand_chip *chip = mtd->priv; |
| 213 | uint32_t val, dev_width = (chip->options & NAND_BUSWIDTH_16) >> 1; |
| 214 | |
| 215 | switch (mode) { |
| 216 | case NAND_ECC_READ: |
| 217 | case NAND_ECC_WRITE: |
| 218 | /* Clear the ecc result registers, select ecc reg as 1 */ |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 219 | writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 220 | |
| 221 | /* |
| 222 | * Size 0 = 0xFF, Size1 is 0xFF - both are 512 bytes |
| 223 | * tell all regs to generate size0 sized regs |
| 224 | * we just have a single ECC engine for all CS |
| 225 | */ |
| 226 | writel(ECCSIZE1 | ECCSIZE0 | ECCSIZE0SEL, |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 227 | &gpmc_cfg->ecc_size_config); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 228 | val = (dev_width << 7) | (cs << 1) | (0x1); |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 229 | writel(val, &gpmc_cfg->ecc_config); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 230 | break; |
| 231 | default: |
| 232 | printf("Error: Unrecognized Mode[%d]!\n", mode); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 237 | #ifndef CONFIG_SPL_BUILD |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 238 | /* |
| 239 | * omap_nand_switch_ecc - switch the ECC operation b/w h/w ecc and s/w ecc. |
| 240 | * The default is to come up on s/w ecc |
| 241 | * |
| 242 | * @hardware - 1 -switch to h/w ecc, 0 - s/w ecc |
| 243 | * |
| 244 | */ |
| 245 | void omap_nand_switch_ecc(int32_t hardware) |
| 246 | { |
| 247 | struct nand_chip *nand; |
| 248 | struct mtd_info *mtd; |
| 249 | |
| 250 | if (nand_curr_device < 0 || |
| 251 | nand_curr_device >= CONFIG_SYS_MAX_NAND_DEVICE || |
| 252 | !nand_info[nand_curr_device].name) { |
| 253 | printf("Error: Can't switch ecc, no devices available\n"); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | mtd = &nand_info[nand_curr_device]; |
| 258 | nand = mtd->priv; |
| 259 | |
| 260 | nand->options |= NAND_OWN_BUFFERS; |
| 261 | |
| 262 | /* Reset ecc interface */ |
| 263 | nand->ecc.read_page = NULL; |
| 264 | nand->ecc.write_page = NULL; |
| 265 | nand->ecc.read_oob = NULL; |
| 266 | nand->ecc.write_oob = NULL; |
| 267 | nand->ecc.hwctl = NULL; |
| 268 | nand->ecc.correct = NULL; |
| 269 | nand->ecc.calculate = NULL; |
| 270 | |
| 271 | /* Setup the ecc configurations again */ |
| 272 | if (hardware) { |
| 273 | nand->ecc.mode = NAND_ECC_HW; |
| 274 | nand->ecc.layout = &hw_nand_oob; |
| 275 | nand->ecc.size = 512; |
| 276 | nand->ecc.bytes = 3; |
| 277 | nand->ecc.hwctl = omap_enable_hwecc; |
| 278 | nand->ecc.correct = omap_correct_data; |
| 279 | nand->ecc.calculate = omap_calculate_ecc; |
| 280 | omap_hwecc_init(nand); |
| 281 | printf("HW ECC selected\n"); |
| 282 | } else { |
| 283 | nand->ecc.mode = NAND_ECC_SOFT; |
| 284 | /* Use mtd default settings */ |
| 285 | nand->ecc.layout = NULL; |
| 286 | printf("SW ECC selected\n"); |
| 287 | } |
| 288 | |
| 289 | /* Update NAND handling after ECC mode switch */ |
| 290 | nand_scan_tail(mtd); |
| 291 | |
| 292 | nand->options &= ~NAND_OWN_BUFFERS; |
| 293 | } |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 294 | #endif /* CONFIG_SPL_BUILD */ |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * Board-specific NAND initialization. The following members of the |
| 298 | * argument are board-specific: |
| 299 | * - IO_ADDR_R: address to read the 8 I/O lines of the flash device |
| 300 | * - IO_ADDR_W: address to write the 8 I/O lines of the flash device |
| 301 | * - cmd_ctrl: hardwarespecific function for accesing control-lines |
| 302 | * - waitfunc: hardwarespecific function for accesing device ready/busy line |
| 303 | * - ecc.hwctl: function to enable (reset) hardware ecc generator |
| 304 | * - ecc.mode: mode of ecc, see defines |
| 305 | * - chip_delay: chip dependent delay for transfering data from array to |
| 306 | * read regs (tR) |
| 307 | * - options: various chip options. They can partly be set to inform |
| 308 | * nand_scan about special functionality. See the defines for further |
| 309 | * explanation |
| 310 | */ |
| 311 | int board_nand_init(struct nand_chip *nand) |
| 312 | { |
| 313 | int32_t gpmc_config = 0; |
| 314 | cs = 0; |
| 315 | |
| 316 | /* |
| 317 | * xloader/Uboot's gpmc configuration would have configured GPMC for |
| 318 | * nand type of memory. The following logic scans and latches on to the |
| 319 | * first CS with NAND type memory. |
| 320 | * TBD: need to make this logic generic to handle multiple CS NAND |
| 321 | * devices. |
| 322 | */ |
| 323 | while (cs < GPMC_MAX_CS) { |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 324 | /* Check if NAND type is set */ |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 325 | if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) { |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 326 | /* Found it!! */ |
| 327 | break; |
| 328 | } |
| 329 | cs++; |
| 330 | } |
| 331 | if (cs >= GPMC_MAX_CS) { |
| 332 | printf("NAND: Unable to find NAND settings in " |
| 333 | "GPMC Configuration - quitting\n"); |
| 334 | return -ENODEV; |
| 335 | } |
| 336 | |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 337 | gpmc_config = readl(&gpmc_cfg->config); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 338 | /* Disable Write protect */ |
| 339 | gpmc_config |= 0x10; |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 340 | writel(gpmc_config, &gpmc_cfg->config); |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 341 | |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 342 | nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat; |
| 343 | nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd; |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 344 | |
| 345 | nand->cmd_ctrl = omap_nand_hwcontrol; |
| 346 | nand->options = NAND_NO_PADDING | NAND_CACHEPRG | NAND_NO_AUTOINCR; |
| 347 | /* If we are 16 bit dev, our gpmc config tells us that */ |
Dirk Behme | a4becd6 | 2009-08-08 09:30:22 +0200 | [diff] [blame] | 348 | if ((readl(&gpmc_cfg->cs[cs].config1) & 0x3000) == 0x1000) |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 349 | nand->options |= NAND_BUSWIDTH_16; |
| 350 | |
| 351 | nand->chip_delay = 100; |
| 352 | /* Default ECC mode */ |
Ilya Yanok | d7bca05 | 2011-11-28 06:37:38 +0000 | [diff] [blame] | 353 | #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_NAND_SOFTECC) |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 354 | nand->ecc.mode = NAND_ECC_SOFT; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 355 | #else |
| 356 | nand->ecc.mode = NAND_ECC_HW; |
| 357 | nand->ecc.layout = &hw_nand_oob; |
| 358 | nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE; |
| 359 | nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES; |
| 360 | nand->ecc.hwctl = omap_enable_hwecc; |
| 361 | nand->ecc.correct = omap_correct_data; |
| 362 | nand->ecc.calculate = omap_calculate_ecc; |
| 363 | omap_hwecc_init(nand); |
Ilya Yanok | d7bca05 | 2011-11-28 06:37:38 +0000 | [diff] [blame] | 364 | #endif |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 365 | |
Ilya Yanok | d7bca05 | 2011-11-28 06:37:38 +0000 | [diff] [blame] | 366 | #ifdef CONFIG_SPL_BUILD |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 367 | if (nand->options & NAND_BUSWIDTH_16) |
| 368 | nand->read_buf = nand_read_buf16; |
| 369 | else |
| 370 | nand->read_buf = nand_read_buf; |
| 371 | nand->dev_ready = omap_spl_dev_ready; |
| 372 | #endif |
Dirk Behme | 778933f | 2008-12-14 09:47:16 +0100 | [diff] [blame] | 373 | |
| 374 | return 0; |
| 375 | } |