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