3 * Konstantin Kozhevnikov, Cogent Embedded
5 * based on nand_spl_simple code
7 * (C) Copyright 2006-2008
8 * Stefan Roese, DENX Software Engineering, sr@denx.de.
10 * SPDX-License-Identifier: GPL-2.0+
16 #include <linux/mtd/nand_ecc.h>
18 static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
19 static struct mtd_info *mtd;
20 static struct nand_chip nand_chip;
22 #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
23 CONFIG_SYS_NAND_ECCSIZE)
24 #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
28 * NAND command for large page NAND devices (2k)
30 static int nand_command(int block, int page, uint32_t offs,
33 struct nand_chip *this = mtd_to_nand(mtd);
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;
38 while (!this->dev_ready(mtd))
41 /* Emulate NAND_CMD_READOOB */
42 if (cmd == NAND_CMD_READOOB) {
43 offs += CONFIG_SYS_NAND_PAGE_SIZE;
47 /* Begin command latch cycle */
48 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
50 if (cmd == NAND_CMD_RESET) {
51 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
54 * Apply this short delay always to ensure that we do wait
55 * tWB in any case on any machine.
59 while (!this->dev_ready(mtd))
64 /* Shift the offset from byte addressing to word addressing. */
65 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
68 /* Set ALE and clear CLE to start address cycle */
70 hwctrl(mtd, offs & 0xff,
71 NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
72 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
74 if (cmd != NAND_CMD_RNDOUT) {
75 hwctrl(mtd, (page_addr & 0xff),
76 NAND_CTRL_ALE); /* A[19:12] */
77 hwctrl(mtd, ((page_addr >> 8) & 0xff),
78 NAND_CTRL_ALE); /* A[27:20] */
79 #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
80 /* One more address cycle for devices > 128MiB */
81 hwctrl(mtd, (page_addr >> 16) & 0x0f,
82 NAND_CTRL_ALE); /* A[31:28] */
86 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
90 * Program and erase have their own busy handlers status, sequential
91 * in and status need no delay.
94 case NAND_CMD_CACHEDPROG:
95 case NAND_CMD_PAGEPROG:
100 case NAND_CMD_STATUS:
103 case NAND_CMD_RNDOUT:
104 /* No ready / busy check necessary */
105 hwctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_CTRL_CLE |
107 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
111 /* Latch in address */
112 hwctrl(mtd, NAND_CMD_READSTART,
113 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
114 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
118 * Apply this short delay always to ensure that we do wait tWB in
119 * any case on any machine.
123 while (!this->dev_ready(mtd))
129 static int nand_is_bad_block(int block)
131 struct nand_chip *this = mtd_to_nand(mtd);
133 nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
137 * Read one byte (or two if it's a 16 bit chip).
139 if (this->options & NAND_BUSWIDTH_16) {
140 if (readw(this->IO_ADDR_R) != 0xffff)
143 if (readb(this->IO_ADDR_R) != 0xff)
150 static int nand_read_page(int block, int page, void *dst)
152 struct nand_chip *this = mtd_to_nand(mtd);
153 u_char ecc_calc[ECCTOTAL];
154 u_char ecc_code[ECCTOTAL];
155 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
157 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
158 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
159 int eccsteps = ECCSTEPS;
161 uint32_t data_pos = 0;
162 uint8_t *oob = &oob_data[0] + nand_ecc_pos[0];
163 uint32_t oob_pos = eccsize * eccsteps + nand_ecc_pos[0];
165 nand_command(block, page, 0, NAND_CMD_READ0);
167 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
168 this->ecc.hwctl(mtd, NAND_ECC_READ);
169 nand_command(block, page, data_pos, NAND_CMD_RNDOUT);
171 this->read_buf(mtd, p, eccsize);
173 nand_command(block, page, oob_pos, NAND_CMD_RNDOUT);
175 this->read_buf(mtd, oob, eccbytes);
176 this->ecc.calculate(mtd, p, &ecc_calc[i]);
183 /* Pick the ECC bytes out of the oob data */
184 for (i = 0; i < ECCTOTAL; i++)
185 ecc_code[i] = oob_data[nand_ecc_pos[i]];
190 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
191 /* No chance to do something with the possible error message
192 * from correct_data(). We just hope that all possible errors
193 * are corrected by this routine.
195 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
201 /* nand_init() - initialize data to make nand usable by SPL */
205 * Init board specific nand support
207 mtd = nand_to_mtd(&nand_chip);
208 nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
209 (void __iomem *)CONFIG_SYS_NAND_BASE;
210 board_nand_init(&nand_chip);
212 if (nand_chip.select_chip)
213 nand_chip.select_chip(mtd, 0);
215 /* NAND chip may require reset after power-on */
216 nand_command(0, 0, 0, NAND_CMD_RESET);
219 /* Unselect after operation */
220 void nand_deselect(void)
222 if (nand_chip.select_chip)
223 nand_chip.select_chip(mtd, -1);
226 #include "nand_spl_loaders.c"