Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 |
| 4 | * Konstantin Kozhevnikov, Cogent Embedded |
| 5 | * |
| 6 | * based on nand_spl_simple code |
| 7 | * |
| 8 | * (C) Copyright 2006-2008 |
| 9 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <nand.h> |
Sean Anderson | 11a4c70 | 2023-11-04 16:37:41 -0400 | [diff] [blame] | 14 | #include <system-constants.h> |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 15 | #include <asm/io.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 17 | #include <linux/mtd/nand_ecc.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 18 | #include <linux/mtd/rawnand.h> |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 19 | |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 20 | static int nand_ecc_pos[] = CFG_SYS_NAND_ECCPOS; |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 21 | static struct mtd_info *mtd; |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 22 | static struct nand_chip nand_chip; |
| 23 | |
| 24 | #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \ |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 25 | CFG_SYS_NAND_ECCSIZE) |
| 26 | #define ECCTOTAL (ECCSTEPS * CFG_SYS_NAND_ECCBYTES) |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 27 | |
| 28 | |
| 29 | /* |
| 30 | * NAND command for large page NAND devices (2k) |
| 31 | */ |
| 32 | static int nand_command(int block, int page, uint32_t offs, |
| 33 | u8 cmd) |
| 34 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 35 | struct nand_chip *this = mtd_to_nand(mtd); |
Sean Anderson | 11a4c70 | 2023-11-04 16:37:41 -0400 | [diff] [blame] | 36 | int page_addr = page + block * SYS_NAND_BLOCK_PAGES; |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 37 | void (*hwctrl)(struct mtd_info *mtd, int cmd, |
| 38 | unsigned int ctrl) = this->cmd_ctrl; |
| 39 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 40 | while (!this->dev_ready(mtd)) |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 41 | ; |
| 42 | |
| 43 | /* Emulate NAND_CMD_READOOB */ |
| 44 | if (cmd == NAND_CMD_READOOB) { |
| 45 | offs += CONFIG_SYS_NAND_PAGE_SIZE; |
| 46 | cmd = NAND_CMD_READ0; |
| 47 | } |
| 48 | |
| 49 | /* Begin command latch cycle */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 50 | hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 51 | |
| 52 | if (cmd == NAND_CMD_RESET) { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 53 | hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Cooper Jr., Franklin | dc2b941 | 2017-04-06 15:54:14 -0500 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Apply this short delay always to ensure that we do wait |
| 57 | * tWB in any case on any machine. |
| 58 | */ |
| 59 | ndelay(150); |
| 60 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 61 | while (!this->dev_ready(mtd)) |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 62 | ; |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | /* Shift the offset from byte addressing to word addressing. */ |
Brian Norris | 6767522 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 67 | if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd)) |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 68 | offs >>= 1; |
| 69 | |
| 70 | /* Set ALE and clear CLE to start address cycle */ |
| 71 | /* Column address */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 72 | hwctrl(mtd, offs & 0xff, |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 73 | NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 74 | hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */ |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 75 | /* Row address */ |
Rostislav Lisovy | 278d903 | 2014-09-09 15:54:30 +0200 | [diff] [blame] | 76 | if (cmd != NAND_CMD_RNDOUT) { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 77 | hwctrl(mtd, (page_addr & 0xff), |
Rostislav Lisovy | 278d903 | 2014-09-09 15:54:30 +0200 | [diff] [blame] | 78 | NAND_CTRL_ALE); /* A[19:12] */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 79 | hwctrl(mtd, ((page_addr >> 8) & 0xff), |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 80 | NAND_CTRL_ALE); /* A[27:20] */ |
| 81 | #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE |
Rostislav Lisovy | 278d903 | 2014-09-09 15:54:30 +0200 | [diff] [blame] | 82 | /* One more address cycle for devices > 128MiB */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 83 | hwctrl(mtd, (page_addr >> 16) & 0x0f, |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 84 | NAND_CTRL_ALE); /* A[31:28] */ |
| 85 | #endif |
Rostislav Lisovy | 278d903 | 2014-09-09 15:54:30 +0200 | [diff] [blame] | 86 | } |
| 87 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 88 | hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 89 | |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 90 | |
Cooper Jr., Franklin | dc2b941 | 2017-04-06 15:54:14 -0500 | [diff] [blame] | 91 | /* |
| 92 | * Program and erase have their own busy handlers status, sequential |
| 93 | * in and status need no delay. |
| 94 | */ |
| 95 | switch (cmd) { |
| 96 | case NAND_CMD_CACHEDPROG: |
| 97 | case NAND_CMD_PAGEPROG: |
| 98 | case NAND_CMD_ERASE1: |
| 99 | case NAND_CMD_ERASE2: |
| 100 | case NAND_CMD_SEQIN: |
| 101 | case NAND_CMD_RNDIN: |
| 102 | case NAND_CMD_STATUS: |
| 103 | return 0; |
| 104 | |
| 105 | case NAND_CMD_RNDOUT: |
| 106 | /* No ready / busy check necessary */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 107 | hwctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_CTRL_CLE | |
Cooper Jr., Franklin | dc2b941 | 2017-04-06 15:54:14 -0500 | [diff] [blame] | 108 | NAND_CTRL_CHANGE); |
| 109 | hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
| 110 | return 0; |
| 111 | |
| 112 | case NAND_CMD_READ0: |
| 113 | /* Latch in address */ |
| 114 | hwctrl(mtd, NAND_CMD_READSTART, |
| 115 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 116 | hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Cooper Jr., Franklin | dc2b941 | 2017-04-06 15:54:14 -0500 | [diff] [blame] | 119 | /* |
| 120 | * Apply this short delay always to ensure that we do wait tWB in |
| 121 | * any case on any machine. |
| 122 | */ |
| 123 | ndelay(150); |
| 124 | |
| 125 | while (!this->dev_ready(mtd)) |
| 126 | ; |
| 127 | |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static int nand_is_bad_block(int block) |
| 132 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 133 | struct nand_chip *this = mtd_to_nand(mtd); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 134 | |
| 135 | nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, |
| 136 | NAND_CMD_READOOB); |
| 137 | |
| 138 | /* |
| 139 | * Read one byte (or two if it's a 16 bit chip). |
| 140 | */ |
| 141 | if (this->options & NAND_BUSWIDTH_16) { |
| 142 | if (readw(this->IO_ADDR_R) != 0xffff) |
| 143 | return 1; |
| 144 | } else { |
| 145 | if (readb(this->IO_ADDR_R) != 0xff) |
| 146 | return 1; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static int nand_read_page(int block, int page, void *dst) |
| 153 | { |
Scott Wood | 17fed14 | 2016-05-30 13:57:56 -0500 | [diff] [blame] | 154 | struct nand_chip *this = mtd_to_nand(mtd); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 155 | u_char ecc_calc[ECCTOTAL]; |
| 156 | u_char ecc_code[ECCTOTAL]; |
| 157 | u_char oob_data[CONFIG_SYS_NAND_OOBSIZE]; |
| 158 | int i; |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 159 | int eccsize = CFG_SYS_NAND_ECCSIZE; |
| 160 | int eccbytes = CFG_SYS_NAND_ECCBYTES; |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 161 | int eccsteps = ECCSTEPS; |
| 162 | uint8_t *p = dst; |
| 163 | uint32_t data_pos = 0; |
| 164 | uint8_t *oob = &oob_data[0] + nand_ecc_pos[0]; |
| 165 | uint32_t oob_pos = eccsize * eccsteps + nand_ecc_pos[0]; |
| 166 | |
| 167 | nand_command(block, page, 0, NAND_CMD_READ0); |
| 168 | |
| 169 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 170 | this->ecc.hwctl(mtd, NAND_ECC_READ); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 171 | nand_command(block, page, data_pos, NAND_CMD_RNDOUT); |
| 172 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 173 | this->read_buf(mtd, p, eccsize); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 174 | |
| 175 | nand_command(block, page, oob_pos, NAND_CMD_RNDOUT); |
| 176 | |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 177 | this->read_buf(mtd, oob, eccbytes); |
| 178 | this->ecc.calculate(mtd, p, &ecc_calc[i]); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 179 | |
| 180 | data_pos += eccsize; |
| 181 | oob_pos += eccbytes; |
| 182 | oob += eccbytes; |
| 183 | } |
| 184 | |
| 185 | /* Pick the ECC bytes out of the oob data */ |
| 186 | for (i = 0; i < ECCTOTAL; i++) |
| 187 | ecc_code[i] = oob_data[nand_ecc_pos[i]]; |
| 188 | |
| 189 | eccsteps = ECCSTEPS; |
| 190 | p = dst; |
| 191 | |
| 192 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 193 | /* No chance to do something with the possible error message |
| 194 | * from correct_data(). We just hope that all possible errors |
| 195 | * are corrected by this routine. |
| 196 | */ |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 197 | this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 203 | /* nand_init() - initialize data to make nand usable by SPL */ |
| 204 | void nand_init(void) |
| 205 | { |
| 206 | /* |
| 207 | * Init board specific nand support |
| 208 | */ |
Boris Brezillon | 3b5f884 | 2016-06-15 20:56:10 +0200 | [diff] [blame] | 209 | mtd = nand_to_mtd(&nand_chip); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 210 | nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W = |
Tom Rini | b421349 | 2022-11-12 17:36:51 -0500 | [diff] [blame] | 211 | (void __iomem *)CFG_SYS_NAND_BASE; |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 212 | board_nand_init(&nand_chip); |
| 213 | |
| 214 | if (nand_chip.select_chip) |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 215 | nand_chip.select_chip(mtd, 0); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 216 | |
| 217 | /* NAND chip may require reset after power-on */ |
| 218 | nand_command(0, 0, 0, NAND_CMD_RESET); |
| 219 | } |
| 220 | |
Sean Anderson | 8805f9d | 2023-11-04 16:37:44 -0400 | [diff] [blame] | 221 | unsigned int nand_page_size(void) |
| 222 | { |
| 223 | return nand_to_mtd(&nand_chip)->writesize; |
| 224 | } |
| 225 | |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 226 | /* Unselect after operation */ |
| 227 | void nand_deselect(void) |
| 228 | { |
| 229 | if (nand_chip.select_chip) |
Scott Wood | 2c1b7e1 | 2016-05-30 13:57:55 -0500 | [diff] [blame] | 230 | nand_chip.select_chip(mtd, -1); |
Ilya Yanok | 15d67a5 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 231 | } |
Ladislav Michl | c6a4200 | 2017-04-16 15:31:59 +0200 | [diff] [blame] | 232 | |
| 233 | #include "nand_spl_loaders.c" |