Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2012 |
| 3 | * Konstantin Kozhevnikov, Cogent Embedded |
| 4 | * |
| 5 | * based on nand_spl_simple code |
| 6 | * |
| 7 | * (C) Copyright 2006-2008 |
| 8 | * Stefan Roese, DENX Software Engineering, sr@denx.de. |
| 9 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
| 14 | #include <nand.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <linux/mtd/nand_ecc.h> |
| 17 | |
| 18 | static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS; |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 19 | nand_info_t nand_info[1]; |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 20 | static struct nand_chip nand_chip; |
| 21 | |
| 22 | #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \ |
| 23 | CONFIG_SYS_NAND_ECCSIZE) |
| 24 | #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES) |
| 25 | |
| 26 | |
| 27 | /* |
| 28 | * NAND command for large page NAND devices (2k) |
| 29 | */ |
| 30 | static int nand_command(int block, int page, uint32_t offs, |
| 31 | u8 cmd) |
| 32 | { |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 33 | struct nand_chip *this = nand_info[0].priv; |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 34 | int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT; |
| 35 | void (*hwctrl)(struct mtd_info *mtd, int cmd, |
| 36 | unsigned int ctrl) = this->cmd_ctrl; |
| 37 | |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 38 | while (!this->dev_ready(&nand_info[0])) |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 39 | ; |
| 40 | |
| 41 | /* Emulate NAND_CMD_READOOB */ |
| 42 | if (cmd == NAND_CMD_READOOB) { |
| 43 | offs += CONFIG_SYS_NAND_PAGE_SIZE; |
| 44 | cmd = NAND_CMD_READ0; |
| 45 | } |
| 46 | |
| 47 | /* Begin command latch cycle */ |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 48 | hwctrl(&nand_info[0], cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 49 | |
| 50 | if (cmd == NAND_CMD_RESET) { |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 51 | hwctrl(&nand_info[0], NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
| 52 | while (!this->dev_ready(&nand_info[0])) |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 53 | ; |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* Shift the offset from byte addressing to word addressing. */ |
Brian Norris | 27ce9e4 | 2014-05-06 00:46:17 +0530 | [diff] [blame] | 58 | if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd)) |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 59 | offs >>= 1; |
| 60 | |
| 61 | /* Set ALE and clear CLE to start address cycle */ |
| 62 | /* Column address */ |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 63 | hwctrl(&nand_info[0], offs & 0xff, |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 64 | NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */ |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 65 | hwctrl(&nand_info[0], (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */ |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 66 | /* Row address */ |
Rostislav Lisovy | 2f66594 | 2014-09-09 15:54:30 +0200 | [diff] [blame] | 67 | if (cmd != NAND_CMD_RNDOUT) { |
| 68 | hwctrl(&nand_info[0], (page_addr & 0xff), |
| 69 | NAND_CTRL_ALE); /* A[19:12] */ |
| 70 | hwctrl(&nand_info[0], ((page_addr >> 8) & 0xff), |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 71 | NAND_CTRL_ALE); /* A[27:20] */ |
| 72 | #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE |
Rostislav Lisovy | 2f66594 | 2014-09-09 15:54:30 +0200 | [diff] [blame] | 73 | /* One more address cycle for devices > 128MiB */ |
| 74 | hwctrl(&nand_info[0], (page_addr >> 16) & 0x0f, |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 75 | NAND_CTRL_ALE); /* A[31:28] */ |
| 76 | #endif |
Rostislav Lisovy | 2f66594 | 2014-09-09 15:54:30 +0200 | [diff] [blame] | 77 | } |
| 78 | |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 79 | hwctrl(&nand_info[0], NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 80 | |
| 81 | if (cmd == NAND_CMD_READ0) { |
| 82 | /* Latch in address */ |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 83 | hwctrl(&nand_info[0], NAND_CMD_READSTART, |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 84 | NAND_CTRL_CLE | NAND_CTRL_CHANGE); |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 85 | hwctrl(&nand_info[0], NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Wait a while for the data to be ready |
| 89 | */ |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 90 | while (!this->dev_ready(&nand_info[0])) |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 91 | ; |
| 92 | } else if (cmd == NAND_CMD_RNDOUT) { |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 93 | hwctrl(&nand_info[0], NAND_CMD_RNDOUTSTART, NAND_CTRL_CLE | |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 94 | NAND_CTRL_CHANGE); |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 95 | hwctrl(&nand_info[0], NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static int nand_is_bad_block(int block) |
| 102 | { |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 103 | struct nand_chip *this = nand_info[0].priv; |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 104 | |
| 105 | nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, |
| 106 | NAND_CMD_READOOB); |
| 107 | |
| 108 | /* |
| 109 | * Read one byte (or two if it's a 16 bit chip). |
| 110 | */ |
| 111 | if (this->options & NAND_BUSWIDTH_16) { |
| 112 | if (readw(this->IO_ADDR_R) != 0xffff) |
| 113 | return 1; |
| 114 | } else { |
| 115 | if (readb(this->IO_ADDR_R) != 0xff) |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int nand_read_page(int block, int page, void *dst) |
| 123 | { |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 124 | struct nand_chip *this = nand_info[0].priv; |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 125 | u_char ecc_calc[ECCTOTAL]; |
| 126 | u_char ecc_code[ECCTOTAL]; |
| 127 | u_char oob_data[CONFIG_SYS_NAND_OOBSIZE]; |
| 128 | int i; |
| 129 | int eccsize = CONFIG_SYS_NAND_ECCSIZE; |
| 130 | int eccbytes = CONFIG_SYS_NAND_ECCBYTES; |
| 131 | int eccsteps = ECCSTEPS; |
| 132 | uint8_t *p = dst; |
| 133 | uint32_t data_pos = 0; |
| 134 | uint8_t *oob = &oob_data[0] + nand_ecc_pos[0]; |
| 135 | uint32_t oob_pos = eccsize * eccsteps + nand_ecc_pos[0]; |
| 136 | |
| 137 | nand_command(block, page, 0, NAND_CMD_READ0); |
| 138 | |
| 139 | for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 140 | this->ecc.hwctl(&nand_info[0], NAND_ECC_READ); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 141 | nand_command(block, page, data_pos, NAND_CMD_RNDOUT); |
| 142 | |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 143 | this->read_buf(&nand_info[0], p, eccsize); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 144 | |
| 145 | nand_command(block, page, oob_pos, NAND_CMD_RNDOUT); |
| 146 | |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 147 | this->read_buf(&nand_info[0], oob, eccbytes); |
| 148 | this->ecc.calculate(&nand_info[0], p, &ecc_calc[i]); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 149 | |
| 150 | data_pos += eccsize; |
| 151 | oob_pos += eccbytes; |
| 152 | oob += eccbytes; |
| 153 | } |
| 154 | |
| 155 | /* Pick the ECC bytes out of the oob data */ |
| 156 | for (i = 0; i < ECCTOTAL; i++) |
| 157 | ecc_code[i] = oob_data[nand_ecc_pos[i]]; |
| 158 | |
| 159 | eccsteps = ECCSTEPS; |
| 160 | p = dst; |
| 161 | |
| 162 | for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) { |
| 163 | /* No chance to do something with the possible error message |
| 164 | * from correct_data(). We just hope that all possible errors |
| 165 | * are corrected by this routine. |
| 166 | */ |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 167 | this->ecc.correct(&nand_info[0], p, &ecc_code[i], &ecc_calc[i]); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst) |
| 174 | { |
| 175 | unsigned int block, lastblock; |
| 176 | unsigned int page; |
| 177 | |
| 178 | /* |
| 179 | * offs has to be aligned to a page address! |
| 180 | */ |
| 181 | block = offs / CONFIG_SYS_NAND_BLOCK_SIZE; |
| 182 | lastblock = (offs + size - 1) / CONFIG_SYS_NAND_BLOCK_SIZE; |
| 183 | page = (offs % CONFIG_SYS_NAND_BLOCK_SIZE) / CONFIG_SYS_NAND_PAGE_SIZE; |
| 184 | |
| 185 | while (block <= lastblock) { |
| 186 | if (!nand_is_bad_block(block)) { |
| 187 | /* |
| 188 | * Skip bad blocks |
| 189 | */ |
| 190 | while (page < CONFIG_SYS_NAND_PAGE_COUNT) { |
| 191 | nand_read_page(block, page, dst); |
| 192 | dst += CONFIG_SYS_NAND_PAGE_SIZE; |
| 193 | page++; |
| 194 | } |
| 195 | |
| 196 | page = 0; |
| 197 | } else { |
| 198 | lastblock++; |
| 199 | } |
| 200 | |
| 201 | block++; |
| 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 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 | */ |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 213 | nand_info[0].priv = &nand_chip; |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 214 | nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W = |
| 215 | (void __iomem *)CONFIG_SYS_NAND_BASE; |
| 216 | board_nand_init(&nand_chip); |
| 217 | |
| 218 | if (nand_chip.select_chip) |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 219 | nand_chip.select_chip(&nand_info[0], 0); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 220 | |
| 221 | /* NAND chip may require reset after power-on */ |
| 222 | nand_command(0, 0, 0, NAND_CMD_RESET); |
| 223 | } |
| 224 | |
| 225 | /* Unselect after operation */ |
| 226 | void nand_deselect(void) |
| 227 | { |
| 228 | if (nand_chip.select_chip) |
Tom Rini | 6dd3b56 | 2014-03-28 12:03:36 -0400 | [diff] [blame] | 229 | nand_chip.select_chip(&nand_info[0], -1); |
Ilya Yanok | 5846b11 | 2012-11-06 13:06:34 +0000 | [diff] [blame] | 230 | } |