blob: cfa9b535c8a2f8456c31c47e637757604e7042f5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sergey Kubushyne8f39122007-08-10 20:26:18 +02002/*
3 * NAND driver for TI DaVinci based boards.
4 *
5 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
6 *
7 * Based on Linux DaVinci NAND driver by TI. Original copyright follows:
8 */
9
10/*
11 *
Miquel Raynal1f1ae152018-08-16 17:30:07 +020012 * linux/drivers/mtd/nand/raw/nand_davinci.c
Sergey Kubushyne8f39122007-08-10 20:26:18 +020013 *
14 * NAND Flash Driver
15 *
16 * Copyright (C) 2006 Texas Instruments.
17 *
18 * ----------------------------------------------------------------------------
19 *
Sergey Kubushyne8f39122007-08-10 20:26:18 +020020 * ----------------------------------------------------------------------------
21 *
22 * Overview:
23 * This is a device driver for the NAND flash device found on the
24 * DaVinci board which utilizes the Samsung k9k2g08 part.
25 *
26 Modifications:
27 ver. 1.0: Feb 2005, Vinod/Sudhakar
28 -
Sergey Kubushyne8f39122007-08-10 20:26:18 +020029 */
30
31#include <common.h>
William Juul52c07962007-10-31 13:53:06 +010032#include <asm/io.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020033#include <nand.h>
Khoronzhuk, Ivan753a00a2014-06-07 04:22:52 +030034#include <asm/ti-common/davinci_nand.h>
Sergey Kubushyne8f39122007-08-10 20:26:18 +020035
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -040036/* Definitions for 4-bit hardware ECC */
37#define NAND_TIMEOUT 10240
38#define NAND_ECC_BUSY 0xC
39#define NAND_4BITECC_MASK 0x03FF03FF
40#define EMIF_NANDFSR_ECC_STATE_MASK 0x00000F00
41#define ECC_STATE_NO_ERR 0x0
42#define ECC_STATE_TOO_MANY_ERRS 0x1
43#define ECC_STATE_ERR_CORR_COMP_P 0x2
44#define ECC_STATE_ERR_CORR_COMP_N 0x3
45
Nick Thompson27794722009-12-16 11:15:58 +000046/*
47 * Exploit the little endianness of the ARM to do multi-byte transfers
48 * per device read. This can perform over twice as quickly as individual
49 * byte transfers when buffer alignment is conducive.
50 *
51 * NOTE: This only works if the NAND is not connected to the 2 LSBs of
52 * the address bus. On Davinci EVM platforms this has always been true.
53 */
54static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
55{
Scott Wood17fed142016-05-30 13:57:56 -050056 struct nand_chip *chip = mtd_to_nand(mtd);
Nick Thompson27794722009-12-16 11:15:58 +000057 const u32 *nand = chip->IO_ADDR_R;
58
59 /* Make sure that buf is 32 bit aligned */
60 if (((int)buf & 0x3) != 0) {
61 if (((int)buf & 0x1) != 0) {
62 if (len) {
63 *buf = readb(nand);
64 buf += 1;
65 len--;
66 }
67 }
68
69 if (((int)buf & 0x3) != 0) {
70 if (len >= 2) {
71 *(u16 *)buf = readw(nand);
72 buf += 2;
73 len -= 2;
74 }
75 }
76 }
77
78 /* copy aligned data */
79 while (len >= 4) {
Cyril Chemparathyc4e72242010-03-17 10:03:10 -040080 *(u32 *)buf = __raw_readl(nand);
Nick Thompson27794722009-12-16 11:15:58 +000081 buf += 4;
82 len -= 4;
83 }
84
85 /* mop up any remaining bytes */
86 if (len) {
87 if (len >= 2) {
88 *(u16 *)buf = readw(nand);
89 buf += 2;
90 len -= 2;
91 }
92
93 if (len)
94 *buf = readb(nand);
95 }
96}
97
98static void nand_davinci_write_buf(struct mtd_info *mtd, const uint8_t *buf,
99 int len)
100{
Scott Wood17fed142016-05-30 13:57:56 -0500101 struct nand_chip *chip = mtd_to_nand(mtd);
Nick Thompson27794722009-12-16 11:15:58 +0000102 const u32 *nand = chip->IO_ADDR_W;
103
104 /* Make sure that buf is 32 bit aligned */
105 if (((int)buf & 0x3) != 0) {
106 if (((int)buf & 0x1) != 0) {
107 if (len) {
108 writeb(*buf, nand);
109 buf += 1;
110 len--;
111 }
112 }
113
114 if (((int)buf & 0x3) != 0) {
115 if (len >= 2) {
116 writew(*(u16 *)buf, nand);
117 buf += 2;
118 len -= 2;
119 }
120 }
121 }
122
123 /* copy aligned data */
124 while (len >= 4) {
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400125 __raw_writel(*(u32 *)buf, nand);
Nick Thompson27794722009-12-16 11:15:58 +0000126 buf += 4;
127 len -= 4;
128 }
129
130 /* mop up any remaining bytes */
131 if (len) {
132 if (len >= 2) {
133 writew(*(u16 *)buf, nand);
134 buf += 2;
135 len -= 2;
136 }
137
138 if (len)
139 writeb(*buf, nand);
140 }
141}
142
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400143static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
144 unsigned int ctrl)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200145{
Scott Wood17fed142016-05-30 13:57:56 -0500146 struct nand_chip *this = mtd_to_nand(mtd);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200147 u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
148
William Juul52c07962007-10-31 13:53:06 +0100149 if (ctrl & NAND_CTRL_CHANGE) {
Nick Thompson27794722009-12-16 11:15:58 +0000150 IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
151
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400152 if (ctrl & NAND_CLE)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200153 IO_ADDR_W |= MASK_CLE;
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400154 if (ctrl & NAND_ALE)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200155 IO_ADDR_W |= MASK_ALE;
William Juul52c07962007-10-31 13:53:06 +0100156 this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200157 }
158
William Juul9e9c2c12007-11-09 13:32:30 +0100159 if (cmd != NAND_CMD_NONE)
Nick Thompson27794722009-12-16 11:15:58 +0000160 writeb(cmd, IO_ADDR_W);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200161}
162
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200163#ifdef CONFIG_SYS_NAND_HW_ECC
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200164
Laurence Withers0357a762011-09-26 16:02:30 +0000165static u_int32_t nand_davinci_readecc(struct mtd_info *mtd)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200166{
Laurence Withers0357a762011-09-26 16:02:30 +0000167 u_int32_t ecc = 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200168
Laurence Withers0357a762011-09-26 16:02:30 +0000169 ecc = __raw_readl(&(davinci_emif_regs->nandfecc[
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400170 CONFIG_SYS_NAND_CS - 2]));
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200171
Laurence Withers0357a762011-09-26 16:02:30 +0000172 return ecc;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200173}
174
Laurence Withers0357a762011-09-26 16:02:30 +0000175static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200176{
Laurence Withers0357a762011-09-26 16:02:30 +0000177 u_int32_t val;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200178
Laurence Withers0357a762011-09-26 16:02:30 +0000179 /* reading the ECC result register resets the ECC calculation */
180 nand_davinci_readecc(mtd);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200181
Laurence Withers0357a762011-09-26 16:02:30 +0000182 val = __raw_readl(&davinci_emif_regs->nandfcr);
183 val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
184 val |= DAVINCI_NANDFCR_1BIT_ECC_START(CONFIG_SYS_NAND_CS);
185 __raw_writel(val, &davinci_emif_regs->nandfcr);
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200186}
187
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400188static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
189 u_char *ecc_code)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200190{
191 u_int32_t tmp;
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400192
Laurence Withers0357a762011-09-26 16:02:30 +0000193 tmp = nand_davinci_readecc(mtd);
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400194
195 /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
196 * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
197 tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
198
199 /* Invert so that erased block ECC is correct */
200 tmp = ~tmp;
201
202 *ecc_code++ = tmp;
203 *ecc_code++ = tmp >> 8;
204 *ecc_code++ = tmp >> 16;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200205
David Brownell962d4c12009-04-28 13:19:53 -0700206 /* NOTE: the above code matches mainline Linux:
207 * .PQR.stu ==> ~PQRstu
208 *
209 * MontaVista/TI kernels encode those bytes differently, use
210 * complicated (and allegedly sometimes-wrong) correction code,
211 * and usually shipped with U-Boot that uses software ECC:
212 * .PQR.stu ==> PsQRtu
213 *
214 * If you need MV/TI compatible NAND I/O in U-Boot, it should
215 * be possible to (a) change the mangling above, (b) reverse
216 * that mangling in nand_davinci_correct_data() below.
217 */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200218
David Brownell962d4c12009-04-28 13:19:53 -0700219 return 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200220}
221
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400222static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat,
223 u_char *read_ecc, u_char *calc_ecc)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200224{
Scott Wood17fed142016-05-30 13:57:56 -0500225 struct nand_chip *this = mtd_to_nand(mtd);
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400226 u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
227 (read_ecc[2] << 16);
228 u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
229 (calc_ecc[2] << 16);
230 u_int32_t diff = ecc_calc ^ ecc_nand;
231
232 if (diff) {
233 if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
234 /* Correctable error */
235 if ((diff >> (12 + 3)) < this->ecc.size) {
236 uint8_t find_bit = 1 << ((diff >> 12) & 7);
237 uint32_t find_byte = diff >> (12 + 3);
238
239 dat[find_byte] ^= find_bit;
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900240 pr_debug("Correcting single "
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400241 "bit ECC error at offset: %d, bit: "
242 "%d\n", find_byte, find_bit);
243 return 1;
244 } else {
Scott Wood52ab7ce2016-05-30 13:57:58 -0500245 return -EBADMSG;
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400246 }
247 } else if (!(diff & (diff - 1))) {
248 /* Single bit ECC error in the ECC itself,
249 nothing to fix */
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900250 pr_debug("Single bit ECC error in " "ECC.\n");
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400251 return 1;
252 } else {
253 /* Uncorrectable error */
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900254 pr_debug("ECC UNCORRECTED_ERROR 1\n");
Scott Wood52ab7ce2016-05-30 13:57:58 -0500255 return -EBADMSG;
Hugo Villeneuve73dc0e42008-08-30 17:06:55 -0400256 }
257 }
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400258 return 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200259}
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200260#endif /* CONFIG_SYS_NAND_HW_ECC */
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200261
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400262#ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
263static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
Sandeep Paulrajd3482862009-11-19 23:04:42 -0500264#if defined(CONFIG_SYS_NAND_PAGE_2K)
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400265 .eccbytes = 40,
Heiko Schocher56596e12013-09-06 05:21:23 +0200266#ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC
267 .eccpos = {
268 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
269 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
270 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
271 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
272 },
273 .oobfree = {
274 {2, 4}, {16, 6}, {32, 6}, {48, 6},
275 },
276#else
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400277 .eccpos = {
278 24, 25, 26, 27, 28,
279 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
280 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
281 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
282 59, 60, 61, 62, 63,
283 },
284 .oobfree = {
285 {.offset = 2, .length = 22, },
286 },
Heiko Schocher56596e12013-09-06 05:21:23 +0200287#endif /* #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC */
Sandeep Paulrajd3482862009-11-19 23:04:42 -0500288#elif defined(CONFIG_SYS_NAND_PAGE_4K)
289 .eccbytes = 80,
290 .eccpos = {
291 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
292 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
293 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
294 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
295 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
296 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
297 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
298 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
299 },
300 .oobfree = {
301 {.offset = 2, .length = 46, },
302 },
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400303#endif
304};
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400305
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300306#if defined CONFIG_KEYSTONE_RBL_NAND
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300307static struct nand_ecclayout nand_keystone_rbl_4bit_layout_oobfirst = {
Khoronzhuk, Ivan9016c6f2014-09-02 00:20:02 +0300308#if defined(CONFIG_SYS_NAND_PAGE_2K)
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300309 .eccbytes = 40,
310 .eccpos = {
311 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
312 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
313 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
314 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
315 },
316 .oobfree = {
317 {.offset = 2, .length = 4, },
318 {.offset = 16, .length = 6, },
319 {.offset = 32, .length = 6, },
320 {.offset = 48, .length = 6, },
321 },
322#elif defined(CONFIG_SYS_NAND_PAGE_4K)
323 .eccbytes = 80,
324 .eccpos = {
325 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
326 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
327 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
328 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
329 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
330 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
331 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
332 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
333 },
334 .oobfree = {
335 {.offset = 2, .length = 4, },
336 {.offset = 16, .length = 6, },
337 {.offset = 32, .length = 6, },
338 {.offset = 48, .length = 6, },
339 {.offset = 64, .length = 6, },
340 {.offset = 80, .length = 6, },
341 {.offset = 96, .length = 6, },
342 {.offset = 112, .length = 6, },
343 },
344#endif
345};
346
347#ifdef CONFIG_SYS_NAND_PAGE_2K
348#define CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE CONFIG_KEYSTONE_NAND_MAX_RBL_SIZE >> 11
349#elif defined(CONFIG_SYS_NAND_PAGE_4K)
350#define CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE CONFIG_KEYSTONE_NAND_MAX_RBL_SIZE >> 12
351#endif
352
353/**
354 * nand_davinci_write_page - write one page
355 * @mtd: MTD device structure
356 * @chip: NAND chip descriptor
357 * @buf: the data to write
358 * @oob_required: must write chip->oob_poi to OOB
359 * @page: page number to write
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300360 * @raw: use _raw version of write_page
361 */
362static int nand_davinci_write_page(struct mtd_info *mtd, struct nand_chip *chip,
Khoronzhuk, Ivand5c65802014-09-06 22:17:07 +0300363 uint32_t offset, int data_len,
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300364 const uint8_t *buf, int oob_required,
Boris Brezillonb9bf43c2017-11-22 02:38:24 +0900365 int page, int raw)
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300366{
367 int status;
368 int ret = 0;
369 struct nand_ecclayout *saved_ecc_layout;
370
371 /* save current ECC layout and assign Keystone RBL ECC layout */
372 if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
373 saved_ecc_layout = chip->ecc.layout;
374 chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst;
375 mtd->oobavail = chip->ecc.layout->oobavail;
376 }
377
378 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
379
Scott Wood46e13102016-05-30 13:57:57 -0500380 if (unlikely(raw)) {
381 status = chip->ecc.write_page_raw(mtd, chip, buf,
382 oob_required, page);
383 } else {
384 status = chip->ecc.write_page(mtd, chip, buf,
385 oob_required, page);
386 }
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300387
388 if (status < 0) {
389 ret = status;
390 goto err;
391 }
392
393 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
394 status = chip->waitfunc(mtd, chip);
395
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300396 if (status & NAND_STATUS_FAIL) {
397 ret = -EIO;
398 goto err;
399 }
400
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300401err:
402 /* restore ECC layout */
403 if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
404 chip->ecc.layout = saved_ecc_layout;
405 mtd->oobavail = saved_ecc_layout->oobavail;
406 }
407
408 return ret;
409}
410
411/**
412 * nand_davinci_read_page_hwecc - hardware ECC based page read function
413 * @mtd: mtd info structure
414 * @chip: nand chip info structure
415 * @buf: buffer to store read data
416 * @oob_required: caller requires OOB data read to chip->oob_poi
417 * @page: page number to read
418 *
419 * Not for syndrome calculating ECC controllers which need a special oob layout.
420 */
421static int nand_davinci_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
422 uint8_t *buf, int oob_required, int page)
423{
424 int i, eccsize = chip->ecc.size;
425 int eccbytes = chip->ecc.bytes;
426 int eccsteps = chip->ecc.steps;
427 uint32_t *eccpos;
428 uint8_t *p = buf;
429 uint8_t *ecc_code = chip->buffers->ecccode;
430 uint8_t *ecc_calc = chip->buffers->ecccalc;
431 struct nand_ecclayout *saved_ecc_layout = chip->ecc.layout;
432
433 /* save current ECC layout and assign Keystone RBL ECC layout */
434 if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
435 chip->ecc.layout = &nand_keystone_rbl_4bit_layout_oobfirst;
436 mtd->oobavail = chip->ecc.layout->oobavail;
437 }
438
439 eccpos = chip->ecc.layout->eccpos;
440
441 /* Read the OOB area first */
442 chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
443 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
444 chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
445
446 for (i = 0; i < chip->ecc.total; i++)
447 ecc_code[i] = chip->oob_poi[eccpos[i]];
448
449 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
450 int stat;
451
452 chip->ecc.hwctl(mtd, NAND_ECC_READ);
453 chip->read_buf(mtd, p, eccsize);
454 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
455
456 stat = chip->ecc.correct(mtd, p, &ecc_code[i], NULL);
457 if (stat < 0)
458 mtd->ecc_stats.failed++;
459 else
460 mtd->ecc_stats.corrected += stat;
461 }
462
463 /* restore ECC layout */
464 if (page < CONFIG_KEYSTONE_NAND_MAX_RBL_PAGE) {
465 chip->ecc.layout = saved_ecc_layout;
466 mtd->oobavail = saved_ecc_layout->oobavail;
467 }
468
469 return 0;
470}
471#endif /* CONFIG_KEYSTONE_RBL_NAND */
472
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400473static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
474{
475 u32 val;
476
477 switch (mode) {
478 case NAND_ECC_WRITE:
479 case NAND_ECC_READ:
480 /*
481 * Start a new ECC calculation for reading or writing 512 bytes
482 * of data.
483 */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400484 val = __raw_readl(&davinci_emif_regs->nandfcr);
Nick Thompson789c8872009-12-12 12:12:26 -0500485 val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK;
Nick Thompsonff641522009-12-12 12:13:10 -0500486 val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
Nick Thompson789c8872009-12-12 12:12:26 -0500487 val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CONFIG_SYS_NAND_CS);
488 val |= DAVINCI_NANDFCR_4BIT_ECC_START;
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400489 __raw_writel(val, &davinci_emif_regs->nandfcr);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400490 break;
491 case NAND_ECC_READSYN:
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400492 val = __raw_readl(&davinci_emif_regs->nand4bitecc[0]);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400493 break;
494 default:
495 break;
496 }
497}
498
499static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
500{
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400501 int i;
502
503 for (i = 0; i < 4; i++) {
504 ecc[i] = __raw_readl(&davinci_emif_regs->nand4bitecc[i]) &
505 NAND_4BITECC_MASK;
506 }
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400507
508 return 0;
509}
510
511static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
512 const uint8_t *dat,
513 uint8_t *ecc_code)
514{
Nick Thompson27794722009-12-16 11:15:58 +0000515 unsigned int hw_4ecc[4];
516 unsigned int i;
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400517
518 nand_davinci_4bit_readecc(mtd, hw_4ecc);
519
520 /*Convert 10 bit ecc value to 8 bit */
Nick Thompson27794722009-12-16 11:15:58 +0000521 for (i = 0; i < 2; i++) {
522 unsigned int hw_ecc_low = hw_4ecc[i * 2];
523 unsigned int hw_ecc_hi = hw_4ecc[(i * 2) + 1];
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400524
525 /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
Nick Thompson27794722009-12-16 11:15:58 +0000526 *ecc_code++ = hw_ecc_low & 0xFF;
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400527
528 /*
529 * Take 2 bits as LSB bits from val1 (count1=0) or val5
530 * (count1=1) and 6 bits from val2 (count1=0) or
531 * val5 (count1=1)
532 */
Nick Thompson27794722009-12-16 11:15:58 +0000533 *ecc_code++ =
534 ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400535
536 /*
537 * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
538 * 4 bits from val3 (count1=0) or val6 (count1=1)
539 */
Nick Thompson27794722009-12-16 11:15:58 +0000540 *ecc_code++ =
541 ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400542
543 /*
544 * Take 6 bits from val3(count1=0) or val6 (count1=1) and
545 * 2 bits from val4 (count1=0) or val7 (count1=1)
546 */
Nick Thompson27794722009-12-16 11:15:58 +0000547 *ecc_code++ =
548 ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400549
550 /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
Nick Thompson27794722009-12-16 11:15:58 +0000551 *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400552 }
Nick Thompson27794722009-12-16 11:15:58 +0000553
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400554 return 0;
555}
556
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400557static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
558 uint8_t *read_ecc, uint8_t *calc_ecc)
559{
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400560 int i;
Nick Thompson27794722009-12-16 11:15:58 +0000561 unsigned int hw_4ecc[4];
562 unsigned int iserror;
563 unsigned short *ecc16;
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400564 unsigned int numerrors, erroraddress, errorvalue;
565 u32 val;
566
567 /*
568 * Check for an ECC where all bytes are 0xFF. If this is the case, we
569 * will assume we are looking at an erased page and we should ignore
570 * the ECC.
571 */
572 for (i = 0; i < 10; i++) {
573 if (read_ecc[i] != 0xFF)
574 break;
575 }
576 if (i == 10)
577 return 0;
578
579 /* Convert 8 bit in to 10 bit */
Nick Thompson27794722009-12-16 11:15:58 +0000580 ecc16 = (unsigned short *)&read_ecc[0];
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400581
Nick Thompson27794722009-12-16 11:15:58 +0000582 /*
583 * Write the parity values in the NAND Flash 4-bit ECC Load register.
584 * Write each parity value one at a time starting from 4bit_ecc_val8
585 * to 4bit_ecc_val1.
586 */
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400587
Nick Thompson27794722009-12-16 11:15:58 +0000588 /*Take 2 bits from 8th byte and 8 bits from 9th byte */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400589 __raw_writel(((ecc16[4]) >> 6) & 0x3FF,
590 &davinci_emif_regs->nand4biteccload);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400591
Nick Thompson27794722009-12-16 11:15:58 +0000592 /* Take 4 bits from 7th byte and 6 bits from 8th byte */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400593 __raw_writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0),
594 &davinci_emif_regs->nand4biteccload);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400595
Nick Thompson27794722009-12-16 11:15:58 +0000596 /* Take 6 bits from 6th byte and 4 bits from 7th byte */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400597 __raw_writel((ecc16[3] >> 2) & 0x3FF,
598 &davinci_emif_regs->nand4biteccload);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400599
600 /* Take 8 bits from 5th byte and 2 bits from 6th byte */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400601 __raw_writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300),
602 &davinci_emif_regs->nand4biteccload);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400603
Nick Thompson27794722009-12-16 11:15:58 +0000604 /*Take 2 bits from 3rd byte and 8 bits from 4th byte */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400605 __raw_writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC),
606 &davinci_emif_regs->nand4biteccload);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400607
Nick Thompson27794722009-12-16 11:15:58 +0000608 /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400609 __raw_writel(((ecc16[1]) >> 4) & 0x3FF,
610 &davinci_emif_regs->nand4biteccload);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400611
Nick Thompson27794722009-12-16 11:15:58 +0000612 /* Take 6 bits from 1st byte and 4 bits from 2nd byte */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400613 __raw_writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0),
614 &davinci_emif_regs->nand4biteccload);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400615
Nick Thompson27794722009-12-16 11:15:58 +0000616 /* Take 10 bits from 0th and 1st bytes */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400617 __raw_writel((ecc16[0]) & 0x3FF,
618 &davinci_emif_regs->nand4biteccload);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400619
620 /*
621 * Perform a dummy read to the EMIF Revision Code and Status register.
622 * This is required to ensure time for syndrome calculation after
623 * writing the ECC values in previous step.
624 */
625
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400626 val = __raw_readl(&davinci_emif_regs->nandfsr);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400627
628 /*
629 * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers.
630 * A syndrome value of 0 means no bit errors. If the syndrome is
631 * non-zero then go further otherwise return.
632 */
633 nand_davinci_4bit_readecc(mtd, hw_4ecc);
634
Nick Thompson27794722009-12-16 11:15:58 +0000635 if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400636 return 0;
637
638 /*
639 * Clear any previous address calculation by doing a dummy read of an
640 * error address register.
641 */
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400642 val = __raw_readl(&davinci_emif_regs->nanderradd1);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400643
644 /*
645 * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
646 * register to 1.
647 */
Ben Gardinerdfd19ea2010-10-14 17:26:17 -0400648 __raw_writel(DAVINCI_NANDFCR_4BIT_CALC_START,
649 &davinci_emif_regs->nandfcr);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400650
651 /*
Wolfram Sang328d0552010-09-09 13:54:41 +0200652 * Wait for the corr_state field (bits 8 to 11) in the
653 * NAND Flash Status register to be not equal to 0x0, 0x1, 0x2, or 0x3.
654 * Otherwise ECC calculation has not even begun and the next loop might
655 * fail because of a false positive!
656 */
657 i = NAND_TIMEOUT;
658 do {
659 val = __raw_readl(&davinci_emif_regs->nandfsr);
660 val &= 0xc00;
661 i--;
662 } while ((i > 0) && !val);
663
664 /*
665 * Wait for the corr_state field (bits 8 to 11) in the
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400666 * NAND Flash Status register to be equal to 0x0, 0x1, 0x2, or 0x3.
667 */
668 i = NAND_TIMEOUT;
669 do {
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400670 val = __raw_readl(&davinci_emif_regs->nandfsr);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400671 val &= 0xc00;
672 i--;
673 } while ((i > 0) && val);
674
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400675 iserror = __raw_readl(&davinci_emif_regs->nandfsr);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400676 iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
677 iserror = iserror >> 8;
678
679 /*
680 * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be
681 * corrected (five or more errors). The number of errors
682 * calculated (err_num field) differs from the number of errors
683 * searched. ECC_STATE_ERR_CORR_COMP_P (0x2) means error
684 * correction complete (errors on bit 8 or 9).
685 * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction
686 * complete (error exists).
687 */
688
689 if (iserror == ECC_STATE_NO_ERR) {
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400690 val = __raw_readl(&davinci_emif_regs->nanderrval1);
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400691 return 0;
692 } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400693 val = __raw_readl(&davinci_emif_regs->nanderrval1);
Scott Wood52ab7ce2016-05-30 13:57:58 -0500694 return -EBADMSG;
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400695 }
696
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400697 numerrors = ((__raw_readl(&davinci_emif_regs->nandfsr) >> 16)
698 & 0x3) + 1;
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400699
700 /* Read the error address, error value and correct */
701 for (i = 0; i < numerrors; i++) {
702 if (i > 1) {
703 erroraddress =
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400704 ((__raw_readl(&davinci_emif_regs->nanderradd2) >>
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400705 (16 * (i & 1))) & 0x3FF);
706 erroraddress = ((512 + 7) - erroraddress);
707 errorvalue =
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400708 ((__raw_readl(&davinci_emif_regs->nanderrval2) >>
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400709 (16 * (i & 1))) & 0xFF);
710 } else {
711 erroraddress =
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400712 ((__raw_readl(&davinci_emif_regs->nanderradd1) >>
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400713 (16 * (i & 1))) & 0x3FF);
714 erroraddress = ((512 + 7) - erroraddress);
715 errorvalue =
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400716 ((__raw_readl(&davinci_emif_regs->nanderrval1) >>
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400717 (16 * (i & 1))) & 0xFF);
718 }
719 /* xor the corrupt data with error value */
720 if (erroraddress < 512)
721 dat[erroraddress] ^= errorvalue;
722 }
723
724 return numerrors;
725}
Scott Woodce630312009-09-28 16:33:18 -0500726#endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400727
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200728static int nand_davinci_dev_ready(struct mtd_info *mtd)
729{
Cyril Chemparathyc4e72242010-03-17 10:03:10 -0400730 return __raw_readl(&davinci_emif_regs->nandfsr) & 0x1;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200731}
732
David Brownellf4b0b9d2009-05-10 15:43:01 -0700733void davinci_nand_init(struct nand_chip *nand)
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200734{
Khoronzhuk, Ivan2f64e472014-07-04 15:03:25 +0300735#if defined CONFIG_KEYSTONE_RBL_NAND
736 int i;
737 struct nand_ecclayout *layout;
738
739 layout = &nand_keystone_rbl_4bit_layout_oobfirst;
740 layout->oobavail = 0;
741 for (i = 0; layout->oobfree[i].length &&
742 i < ARRAY_SIZE(layout->oobfree); i++)
743 layout->oobavail += layout->oobfree[i].length;
744
745 nand->write_page = nand_davinci_write_page;
746 nand->ecc.read_page = nand_davinci_read_page_hwecc;
747#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200748 nand->chip_delay = 0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200749#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
Sergey Lapin3a38a552013-01-14 03:46:50 +0000750 nand->bbt_options |= NAND_BBT_USE_FLASH;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200751#endif
Karicheri, Muralidharanc1dc61b2014-04-04 13:16:50 -0400752#ifdef CONFIG_SYS_NAND_NO_SUBPAGE_WRITE
753 nand->options |= NAND_NO_SUBPAGE_WRITE;
754#endif
Fabien Parentddc00942016-11-29 14:31:29 +0100755#ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
756 nand->options |= NAND_BUSWIDTH_16;
757#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200758#ifdef CONFIG_SYS_NAND_HW_ECC
William Juul9e9c2c12007-11-09 13:32:30 +0100759 nand->ecc.mode = NAND_ECC_HW;
William Juul9e9c2c12007-11-09 13:32:30 +0100760 nand->ecc.size = 512;
761 nand->ecc.bytes = 3;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000762 nand->ecc.strength = 1;
William Juul52c07962007-10-31 13:53:06 +0100763 nand->ecc.calculate = nand_davinci_calculate_ecc;
764 nand->ecc.correct = nand_davinci_correct_data;
William Juulb76ec382007-11-08 10:39:53 +0100765 nand->ecc.hwctl = nand_davinci_enable_hwecc;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200766#else
William Juul9e9c2c12007-11-09 13:32:30 +0100767 nand->ecc.mode = NAND_ECC_SOFT;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200768#endif /* CONFIG_SYS_NAND_HW_ECC */
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400769#ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
770 nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
771 nand->ecc.size = 512;
772 nand->ecc.bytes = 10;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000773 nand->ecc.strength = 4;
Sandeep Paulrajbfeb0fd2009-08-18 10:10:42 -0400774 nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
775 nand->ecc.correct = nand_davinci_4bit_correct_data;
776 nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
777 nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
778#endif
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200779 /* Set address of hardware control function */
William Juul52c07962007-10-31 13:53:06 +0100780 nand->cmd_ctrl = nand_davinci_hwcontrol;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200781
Nick Thompson27794722009-12-16 11:15:58 +0000782 nand->read_buf = nand_davinci_read_buf;
783 nand->write_buf = nand_davinci_write_buf;
784
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200785 nand->dev_ready = nand_davinci_dev_ready;
David Brownellf4b0b9d2009-05-10 15:43:01 -0700786}
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200787
David Brownellf4b0b9d2009-05-10 15:43:01 -0700788int board_nand_init(struct nand_chip *chip) __attribute__((weak));
789
790int board_nand_init(struct nand_chip *chip)
791{
792 davinci_nand_init(chip);
793 return 0;
Sergey Kubushyne8f39122007-08-10 20:26:18 +0200794}