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