Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006-2008 |
| 3 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <nand.h> |
| 10 | #include <asm/io.h> |
Ilya Yanok | 4e69962 | 2011-11-28 06:37:37 +0000 | [diff] [blame] | 11 | #include <linux/mtd/nand_ecc.h> |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 12 | |
| 13 | static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS; |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 14 | static struct mtd_info *mtd; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 15 | static struct nand_chip nand_chip; |
| 16 | |
Stefano Babic | 533607c | 2011-12-15 10:55:37 +0100 | [diff] [blame] | 17 | #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \ |
| 18 | CONFIG_SYS_NAND_ECCSIZE) |
| 19 | #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES) |
| 20 | |
| 21 | |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 22 | #if (CONFIG_SYS_NAND_PAGE_SIZE <= 512) |
| 23 | /* |
| 24 | * NAND command for small page NAND devices (512) |
| 25 | */ |
| 26 | static int nand_command(int block, int page, uint32_t offs, |
| 27 | u8 cmd) |
| 28 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame^] | 29 | struct nand_chip *this = mtd_to_nand(mtd); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 30 | int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT; |
| 31 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 32 | while (!this->dev_ready(mtd)) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 33 | ; |
| 34 | |
| 35 | /* Begin command latch cycle */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 36 | this->cmd_ctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 37 | /* Set ALE and clear CLE to start address cycle */ |
| 38 | /* Column address */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 39 | this->cmd_ctrl(mtd, offs, NAND_CTRL_ALE | NAND_CTRL_CHANGE); |
| 40 | this->cmd_ctrl(mtd, page_addr & 0xff, NAND_CTRL_ALE); /* A[16:9] */ |
| 41 | this->cmd_ctrl(mtd, (page_addr >> 8) & 0xff, |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 42 | NAND_CTRL_ALE); /* A[24:17] */ |
| 43 | #ifdef CONFIG_SYS_NAND_4_ADDR_CYCLE |
| 44 | /* One more address cycle for devices > 32MiB */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 45 | this->cmd_ctrl(mtd, (page_addr >> 16) & 0x0f, |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 46 | NAND_CTRL_ALE); /* A[28:25] */ |
| 47 | #endif |
| 48 | /* Latch in address */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 49 | this->cmd_ctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Wait a while for the data to be ready |
| 53 | */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 54 | while (!this->dev_ready(mtd)) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 55 | ; |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | #else |
| 60 | /* |
| 61 | * NAND command for large page NAND devices (2k) |
| 62 | */ |
| 63 | static int nand_command(int block, int page, uint32_t offs, |
| 64 | u8 cmd) |
| 65 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame^] | 66 | struct nand_chip *this = mtd_to_nand(mtd); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 67 | int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT; |
| 68 | void (*hwctrl)(struct mtd_info *mtd, int cmd, |
| 69 | unsigned int ctrl) = this->cmd_ctrl; |
| 70 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 71 | while (!this->dev_ready(mtd)) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 72 | ; |
| 73 | |
| 74 | /* Emulate NAND_CMD_READOOB */ |
| 75 | if (cmd == NAND_CMD_READOOB) { |
| 76 | offs += CONFIG_SYS_NAND_PAGE_SIZE; |
| 77 | cmd = NAND_CMD_READ0; |
| 78 | } |
| 79 | |
| 80 | /* Shift the offset from byte addressing to word addressing. */ |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 81 | if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd)) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 82 | offs >>= 1; |
| 83 | |
| 84 | /* Begin command latch cycle */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 85 | hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 86 | /* Set ALE and clear CLE to start address cycle */ |
| 87 | /* Column address */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 88 | hwctrl(mtd, offs & 0xff, |
| 89 | NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */ |
| 90 | hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */ |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 91 | /* Row address */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 92 | hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE); /* A[19:12] */ |
| 93 | hwctrl(mtd, ((page_addr >> 8) & 0xff), |
| 94 | NAND_CTRL_ALE); /* A[27:20] */ |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 95 | #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE |
| 96 | /* One more address cycle for devices > 128MiB */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 97 | hwctrl(mtd, (page_addr >> 16) & 0x0f, |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 98 | NAND_CTRL_ALE); /* A[31:28] */ |
| 99 | #endif |
| 100 | /* Latch in address */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 101 | hwctrl(mtd, NAND_CMD_READSTART, |
| 102 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
| 103 | hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Wait a while for the data to be ready |
| 107 | */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 108 | while (!this->dev_ready(mtd)) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 109 | ; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | static int nand_is_bad_block(int block) |
| 116 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame^] | 117 | struct nand_chip *this = mtd_to_nand(mtd); |
Vladimir Zapolskiy | cb8183d | 2015-07-18 01:47:08 +0300 | [diff] [blame] | 118 | u_char bb_data[2]; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 119 | |
| 120 | nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, |
| 121 | NAND_CMD_READOOB); |
| 122 | |
| 123 | /* |
| 124 | * Read one byte (or two if it's a 16 bit chip). |
| 125 | */ |
| 126 | if (this->options & NAND_BUSWIDTH_16) { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 127 | this->read_buf(mtd, bb_data, 2); |
Vladimir Zapolskiy | cb8183d | 2015-07-18 01:47:08 +0300 | [diff] [blame] | 128 | if (bb_data[0] != 0xff || bb_data[1] != 0xff) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 129 | return 1; |
| 130 | } else { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 131 | this->read_buf(mtd, bb_data, 1); |
Vladimir Zapolskiy | cb8183d | 2015-07-18 01:47:08 +0300 | [diff] [blame] | 132 | if (bb_data[0] != 0xff) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 139 | #if defined(CONFIG_SYS_NAND_HW_ECC_OOBFIRST) |
| 140 | static int nand_read_page(int block, int page, uchar *dst) |
| 141 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame^] | 142 | struct nand_chip *this = mtd_to_nand(mtd); |
Stefano Babic | 533607c | 2011-12-15 10:55:37 +0100 | [diff] [blame] | 143 | u_char ecc_calc[ECCTOTAL]; |
| 144 | u_char ecc_code[ECCTOTAL]; |
| 145 | u_char oob_data[CONFIG_SYS_NAND_OOBSIZE]; |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 146 | int i; |
| 147 | int eccsize = CONFIG_SYS_NAND_ECCSIZE; |
| 148 | int eccbytes = CONFIG_SYS_NAND_ECCBYTES; |
Stefano Babic | 533607c | 2011-12-15 10:55:37 +0100 | [diff] [blame] | 149 | int eccsteps = ECCSTEPS; |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 150 | uint8_t *p = dst; |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 151 | |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 152 | nand_command(block, page, 0, NAND_CMD_READOOB); |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 153 | this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE); |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 154 | nand_command(block, page, 0, NAND_CMD_READ0); |
| 155 | |
| 156 | /* Pick the ECC bytes out of the oob data */ |
Stefano Babic | 533607c | 2011-12-15 10:55:37 +0100 | [diff] [blame] | 157 | for (i = 0; i < ECCTOTAL; i++) |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 158 | ecc_code[i] = oob_data[nand_ecc_pos[i]]; |
| 159 | |
| 160 | |
| 161 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 162 | this->ecc.hwctl(mtd, NAND_ECC_READ); |
| 163 | this->read_buf(mtd, p, eccsize); |
| 164 | this->ecc.calculate(mtd, p, &ecc_calc[i]); |
| 165 | this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | #else |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 171 | static int nand_read_page(int block, int page, void *dst) |
| 172 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame^] | 173 | struct nand_chip *this = mtd_to_nand(mtd); |
Stefano Babic | 533607c | 2011-12-15 10:55:37 +0100 | [diff] [blame] | 174 | u_char ecc_calc[ECCTOTAL]; |
| 175 | u_char ecc_code[ECCTOTAL]; |
| 176 | u_char oob_data[CONFIG_SYS_NAND_OOBSIZE]; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 177 | int i; |
| 178 | int eccsize = CONFIG_SYS_NAND_ECCSIZE; |
| 179 | int eccbytes = CONFIG_SYS_NAND_ECCBYTES; |
Stefano Babic | 533607c | 2011-12-15 10:55:37 +0100 | [diff] [blame] | 180 | int eccsteps = ECCSTEPS; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 181 | uint8_t *p = dst; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 182 | |
| 183 | nand_command(block, page, 0, NAND_CMD_READ0); |
| 184 | |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 185 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Ilya Yanok | 4e69962 | 2011-11-28 06:37:37 +0000 | [diff] [blame] | 186 | if (this->ecc.mode != NAND_ECC_SOFT) |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 187 | this->ecc.hwctl(mtd, NAND_ECC_READ); |
| 188 | this->read_buf(mtd, p, eccsize); |
| 189 | this->ecc.calculate(mtd, p, &ecc_calc[i]); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 190 | } |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 191 | this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 192 | |
| 193 | /* Pick the ECC bytes out of the oob data */ |
Stefano Babic | 533607c | 2011-12-15 10:55:37 +0100 | [diff] [blame] | 194 | for (i = 0; i < ECCTOTAL; i++) |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 195 | ecc_code[i] = oob_data[nand_ecc_pos[i]]; |
| 196 | |
Stefano Babic | 533607c | 2011-12-15 10:55:37 +0100 | [diff] [blame] | 197 | eccsteps = ECCSTEPS; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 198 | p = dst; |
| 199 | |
| 200 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 201 | /* No chance to do something with the possible error message |
| 202 | * from correct_data(). We just hope that all possible errors |
| 203 | * are corrected by this routine. |
| 204 | */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 205 | this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
Heiko Schocher | 44bd9b3 | 2011-11-01 20:00:30 +0000 | [diff] [blame] | 210 | #endif |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 211 | |
| 212 | int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst) |
| 213 | { |
| 214 | unsigned int block, lastblock; |
| 215 | unsigned int page; |
| 216 | |
| 217 | /* |
| 218 | * offs has to be aligned to a page address! |
| 219 | */ |
| 220 | block = offs / CONFIG_SYS_NAND_BLOCK_SIZE; |
| 221 | lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE; |
| 222 | page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE; |
| 223 | |
| 224 | while (block <= lastblock) { |
| 225 | if (!nand_is_bad_block(block)) { |
| 226 | /* |
| 227 | * Skip bad blocks |
| 228 | */ |
| 229 | while (page < CONFIG_SYS_NAND_PAGE_COUNT) { |
| 230 | nand_read_page(block, page, dst); |
| 231 | dst += CONFIG_SYS_NAND_PAGE_SIZE; |
| 232 | page++; |
| 233 | } |
| 234 | |
| 235 | page = 0; |
| 236 | } else { |
| 237 | lastblock++; |
| 238 | } |
| 239 | |
| 240 | block++; |
| 241 | } |
| 242 | |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | /* nand_init() - initialize data to make nand usable by SPL */ |
| 247 | void nand_init(void) |
| 248 | { |
| 249 | /* |
| 250 | * Init board specific nand support |
| 251 | */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 252 | mtd = &nand_chip.mtd; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 253 | nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W = |
| 254 | (void __iomem *)CONFIG_SYS_NAND_BASE; |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 255 | board_nand_init(&nand_chip); |
| 256 | |
Ilya Yanok | 4e69962 | 2011-11-28 06:37:37 +0000 | [diff] [blame] | 257 | #ifdef CONFIG_SPL_NAND_SOFTECC |
| 258 | if (nand_chip.ecc.mode == NAND_ECC_SOFT) { |
| 259 | nand_chip.ecc.calculate = nand_calculate_ecc; |
| 260 | nand_chip.ecc.correct = nand_correct_data; |
| 261 | } |
| 262 | #endif |
| 263 | |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 264 | if (nand_chip.select_chip) |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 265 | nand_chip.select_chip(mtd, 0); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | /* Unselect after operation */ |
| 269 | void nand_deselect(void) |
| 270 | { |
| 271 | if (nand_chip.select_chip) |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 272 | nand_chip.select_chip(mtd, -1); |
Simon Schwarz | 4f62e98 | 2011-09-14 15:30:16 -0400 | [diff] [blame] | 273 | } |