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