blob: b280f87ccbc8c0e7619f61ff4c63b5c3d4986a02 [file] [log] [blame]
Ilya Yanok5846b112012-11-06 13:06:34 +00001/*
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 Denk1a459662013-07-08 09:37:19 +020010 * SPDX-License-Identifier: GPL-2.0+
Ilya Yanok5846b112012-11-06 13:06:34 +000011 */
12
13#include <common.h>
14#include <nand.h>
15#include <asm/io.h>
16#include <linux/mtd/nand_ecc.h>
17
18static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
Scott Woodb616d9b2016-05-30 13:57:55 -050019static struct mtd_info *mtd;
Ilya Yanok5846b112012-11-06 13:06:34 +000020static 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 */
30static int nand_command(int block, int page, uint32_t offs,
31 u8 cmd)
32{
Scott Woodb616d9b2016-05-30 13:57:55 -050033 struct nand_chip *this = mtd->priv;
Ilya Yanok5846b112012-11-06 13:06:34 +000034 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
Scott Woodb616d9b2016-05-30 13:57:55 -050038 while (!this->dev_ready(mtd))
Ilya Yanok5846b112012-11-06 13:06:34 +000039 ;
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 */
Scott Woodb616d9b2016-05-30 13:57:55 -050048 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Ilya Yanok5846b112012-11-06 13:06:34 +000049
50 if (cmd == NAND_CMD_RESET) {
Scott Woodb616d9b2016-05-30 13:57:55 -050051 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
52 while (!this->dev_ready(mtd))
Ilya Yanok5846b112012-11-06 13:06:34 +000053 ;
54 return 0;
55 }
56
57 /* Shift the offset from byte addressing to word addressing. */
Brian Norris27ce9e42014-05-06 00:46:17 +053058 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
Ilya Yanok5846b112012-11-06 13:06:34 +000059 offs >>= 1;
60
61 /* Set ALE and clear CLE to start address cycle */
62 /* Column address */
Scott Woodb616d9b2016-05-30 13:57:55 -050063 hwctrl(mtd, offs & 0xff,
Ilya Yanok5846b112012-11-06 13:06:34 +000064 NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
Scott Woodb616d9b2016-05-30 13:57:55 -050065 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
Ilya Yanok5846b112012-11-06 13:06:34 +000066 /* Row address */
Rostislav Lisovy2f665942014-09-09 15:54:30 +020067 if (cmd != NAND_CMD_RNDOUT) {
Scott Woodb616d9b2016-05-30 13:57:55 -050068 hwctrl(mtd, (page_addr & 0xff),
Rostislav Lisovy2f665942014-09-09 15:54:30 +020069 NAND_CTRL_ALE); /* A[19:12] */
Scott Woodb616d9b2016-05-30 13:57:55 -050070 hwctrl(mtd, ((page_addr >> 8) & 0xff),
Ilya Yanok5846b112012-11-06 13:06:34 +000071 NAND_CTRL_ALE); /* A[27:20] */
72#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
Rostislav Lisovy2f665942014-09-09 15:54:30 +020073 /* One more address cycle for devices > 128MiB */
Scott Woodb616d9b2016-05-30 13:57:55 -050074 hwctrl(mtd, (page_addr >> 16) & 0x0f,
Ilya Yanok5846b112012-11-06 13:06:34 +000075 NAND_CTRL_ALE); /* A[31:28] */
76#endif
Rostislav Lisovy2f665942014-09-09 15:54:30 +020077 }
78
Scott Woodb616d9b2016-05-30 13:57:55 -050079 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Ilya Yanok5846b112012-11-06 13:06:34 +000080
81 if (cmd == NAND_CMD_READ0) {
82 /* Latch in address */
Scott Woodb616d9b2016-05-30 13:57:55 -050083 hwctrl(mtd, NAND_CMD_READSTART,
Ilya Yanok5846b112012-11-06 13:06:34 +000084 NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Scott Woodb616d9b2016-05-30 13:57:55 -050085 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Ilya Yanok5846b112012-11-06 13:06:34 +000086
87 /*
88 * Wait a while for the data to be ready
89 */
Scott Woodb616d9b2016-05-30 13:57:55 -050090 while (!this->dev_ready(mtd))
Ilya Yanok5846b112012-11-06 13:06:34 +000091 ;
92 } else if (cmd == NAND_CMD_RNDOUT) {
Scott Woodb616d9b2016-05-30 13:57:55 -050093 hwctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_CTRL_CLE |
Ilya Yanok5846b112012-11-06 13:06:34 +000094 NAND_CTRL_CHANGE);
Scott Woodb616d9b2016-05-30 13:57:55 -050095 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Ilya Yanok5846b112012-11-06 13:06:34 +000096 }
97
98 return 0;
99}
100
101static int nand_is_bad_block(int block)
102{
Scott Woodb616d9b2016-05-30 13:57:55 -0500103 struct nand_chip *this = mtd->priv;
Ilya Yanok5846b112012-11-06 13:06:34 +0000104
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
122static int nand_read_page(int block, int page, void *dst)
123{
Scott Woodb616d9b2016-05-30 13:57:55 -0500124 struct nand_chip *this = mtd->priv;
Ilya Yanok5846b112012-11-06 13:06:34 +0000125 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) {
Scott Woodb616d9b2016-05-30 13:57:55 -0500140 this->ecc.hwctl(mtd, NAND_ECC_READ);
Ilya Yanok5846b112012-11-06 13:06:34 +0000141 nand_command(block, page, data_pos, NAND_CMD_RNDOUT);
142
Scott Woodb616d9b2016-05-30 13:57:55 -0500143 this->read_buf(mtd, p, eccsize);
Ilya Yanok5846b112012-11-06 13:06:34 +0000144
145 nand_command(block, page, oob_pos, NAND_CMD_RNDOUT);
146
Scott Woodb616d9b2016-05-30 13:57:55 -0500147 this->read_buf(mtd, oob, eccbytes);
148 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Ilya Yanok5846b112012-11-06 13:06:34 +0000149
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 */
Scott Woodb616d9b2016-05-30 13:57:55 -0500167 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Ilya Yanok5846b112012-11-06 13:06:34 +0000168 }
169
170 return 0;
171}
172
173int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
174{
175 unsigned int block, lastblock;
Lokesh Vutla09852942016-05-24 10:34:41 +0530176 unsigned int page, page_offset;
Ilya Yanok5846b112012-11-06 13:06:34 +0000177
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;
Lokesh Vutla09852942016-05-24 10:34:41 +0530184 page_offset = offs % CONFIG_SYS_NAND_PAGE_SIZE;
Ilya Yanok5846b112012-11-06 13:06:34 +0000185
186 while (block <= lastblock) {
187 if (!nand_is_bad_block(block)) {
188 /*
189 * Skip bad blocks
190 */
191 while (page < CONFIG_SYS_NAND_PAGE_COUNT) {
192 nand_read_page(block, page, dst);
Lokesh Vutla09852942016-05-24 10:34:41 +0530193 /*
194 * When offs is not aligned to page address the
195 * extra offset is copied to dst as well. Copy
196 * the image such that its first byte will be
197 * at the dst.
198 */
199 if (unlikely(page_offset)) {
200 memmove(dst, dst + page_offset,
201 CONFIG_SYS_NAND_PAGE_SIZE);
202 dst = (void *)((int)dst - page_offset);
203 page_offset = 0;
204 }
Ilya Yanok5846b112012-11-06 13:06:34 +0000205 dst += CONFIG_SYS_NAND_PAGE_SIZE;
206 page++;
207 }
208
209 page = 0;
210 } else {
211 lastblock++;
212 }
213
214 block++;
215 }
216
217 return 0;
218}
219
220/* nand_init() - initialize data to make nand usable by SPL */
221void nand_init(void)
222{
223 /*
224 * Init board specific nand support
225 */
Scott Woodb616d9b2016-05-30 13:57:55 -0500226 mtd = &nand_chip.mtd;
Ilya Yanok5846b112012-11-06 13:06:34 +0000227 nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
228 (void __iomem *)CONFIG_SYS_NAND_BASE;
229 board_nand_init(&nand_chip);
230
231 if (nand_chip.select_chip)
Scott Woodb616d9b2016-05-30 13:57:55 -0500232 nand_chip.select_chip(mtd, 0);
Ilya Yanok5846b112012-11-06 13:06:34 +0000233
234 /* NAND chip may require reset after power-on */
235 nand_command(0, 0, 0, NAND_CMD_RESET);
236}
237
238/* Unselect after operation */
239void nand_deselect(void)
240{
241 if (nand_chip.select_chip)
Scott Woodb616d9b2016-05-30 13:57:55 -0500242 nand_chip.select_chip(mtd, -1);
Ilya Yanok5846b112012-11-06 13:06:34 +0000243}