blob: 305e68ad49369f5a291380a5c0fe3ded51aa799d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sergey Kubushync74b2102007-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 *
12 * linux/drivers/mtd/nand/nand_davinci.c
13 *
14 * NAND Flash Driver
15 *
16 * Copyright (C) 2006 Texas Instruments.
17 *
18 * ----------------------------------------------------------------------------
19 *
Sergey Kubushync74b2102007-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 Kubushync74b2102007-08-10 20:26:18 +020029 */
30
31#include <common.h>
William Juulcfa460a2007-10-31 13:53:06 +010032#include <asm/io.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020033#include <nand.h>
Khoronzhuk, Ivan3e01ed02014-06-07 04:22:52 +030034#include <asm/ti-common/davinci_nand.h>
Sergey Kubushync74b2102007-08-10 20:26:18 +020035
Sandeep Paulraj77b351c2009-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 Thompson20da6f42009-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 Wood17cb4b82016-05-30 13:57:56 -050056 struct nand_chip *chip = mtd_to_nand(mtd);
Nick Thompson20da6f42009-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 Chemparathycc41a592010-03-17 10:03:10 -040080 *(u32 *)buf = __raw_readl(nand);
Nick Thompson20da6f42009-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 Wood17cb4b82016-05-30 13:57:56 -0500101 struct nand_chip *chip = mtd_to_nand(mtd);
Nick Thompson20da6f42009-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 Chemparathycc41a592010-03-17 10:03:10 -0400125 __raw_writel(*(u32 *)buf, nand);
Nick Thompson20da6f42009-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 Chemparathycc41a592010-03-17 10:03:10 -0400143static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
144 unsigned int ctrl)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200145{
Scott Wood17cb4b82016-05-30 13:57:56 -0500146 struct nand_chip *this = mtd_to_nand(mtd);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200147 u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
148
William Juulcfa460a2007-10-31 13:53:06 +0100149 if (ctrl & NAND_CTRL_CHANGE) {
Nick Thompson20da6f42009-12-16 11:15:58 +0000150 IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
151
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400152 if (ctrl & NAND_CLE)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200153 IO_ADDR_W |= MASK_CLE;
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400154 if (ctrl & NAND_ALE)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200155 IO_ADDR_W |= MASK_ALE;
William Juulcfa460a2007-10-31 13:53:06 +0100156 this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200157 }
158
William Juul5e1dae52007-11-09 13:32:30 +0100159 if (cmd != NAND_CMD_NONE)
Nick Thompson20da6f42009-12-16 11:15:58 +0000160 writeb(cmd, IO_ADDR_W);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200161}
162
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200163#ifdef CONFIG_SYS_NAND_HW_ECC
Sergey Kubushync74b2102007-08-10 20:26:18 +0200164
Laurence Withers60161942011-09-26 16:02:30 +0000165static u_int32_t nand_davinci_readecc(struct mtd_info *mtd)
166{
167 u_int32_t ecc = 0;
168
169 ecc = __raw_readl(&(davinci_emif_regs->nandfecc[
170 CONFIG_SYS_NAND_CS - 2]));
171
172 return ecc;
173}
174
Sergey Kubushync74b2102007-08-10 20:26:18 +0200175static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
176{
Nick Thompson97f4eb82009-12-12 12:12:26 -0500177 u_int32_t val;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200178
Laurence Withers60161942011-09-26 16:02:30 +0000179 /* reading the ECC result register resets the ECC calculation */
180 nand_davinci_readecc(mtd);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200181
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400182 val = __raw_readl(&davinci_emif_regs->nandfcr);
Nick Thompson26be2c52009-12-12 12:13:10 -0500183 val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
Nick Thompson97f4eb82009-12-12 12:12:26 -0500184 val |= DAVINCI_NANDFCR_1BIT_ECC_START(CONFIG_SYS_NAND_CS);
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400185 __raw_writel(val, &davinci_emif_regs->nandfcr);
Sergey Kubushync74b2102007-08-10 20:26:18 +0200186}
187
Cyril Chemparathycc41a592010-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 Kubushync74b2102007-08-10 20:26:18 +0200190{
191 u_int32_t tmp;
Hugo Villeneuve9b05aa72008-08-30 17:06:55 -0400192
Laurence Withers60161942011-09-26 16:02:30 +0000193 tmp = nand_davinci_readecc(mtd);
Hugo Villeneuve9b05aa72008-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;
David Brownell6e29ed82009-04-28 13:19:53 -0700205
206 /* 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 */
218
219 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200220}
221
Cyril Chemparathycc41a592010-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 Kubushync74b2102007-08-10 20:26:18 +0200224{
Scott Wood17cb4b82016-05-30 13:57:56 -0500225 struct nand_chip *this = mtd_to_nand(mtd);
Hugo Villeneuve9b05aa72008-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 Yamada166cae22017-10-18 00:10:48 +0900240 pr_debug("Correcting single "
Hugo Villeneuve9b05aa72008-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 Woodceee07b2016-05-30 13:57:58 -0500245 return -EBADMSG;
Hugo Villeneuve9b05aa72008-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 Yamada166cae22017-10-18 00:10:48 +0900250 pr_debug("Single bit ECC error in " "ECC.\n");
Hugo Villeneuve9b05aa72008-08-30 17:06:55 -0400251 return 1;
252 } else {
253 /* Uncorrectable error */
Masahiro Yamada166cae22017-10-18 00:10:48 +0900254 pr_debug("ECC UNCORRECTED_ERROR 1\n");
Scott Woodceee07b2016-05-30 13:57:58 -0500255 return -EBADMSG;
Hugo Villeneuve9b05aa72008-08-30 17:06:55 -0400256 }
257 }
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400258 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200259}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200260#endif /* CONFIG_SYS_NAND_HW_ECC */
Sergey Kubushync74b2102007-08-10 20:26:18 +0200261
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400262#ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
263static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
Sandeep Paulraj10a5a7992009-11-19 23:04:42 -0500264#if defined(CONFIG_SYS_NAND_PAGE_2K)
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400265 .eccbytes = 40,
Heiko Schocher2fff63c2013-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 Paulraj77b351c2009-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 Schocher2fff63c2013-09-06 05:21:23 +0200287#endif /* #ifdef CONFIG_NAND_6BYTES_OOB_FREE_10BYTES_ECC */
Sandeep Paulraj10a5a7992009-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 Paulraj77b351c2009-08-18 10:10:42 -0400303#endif
304};
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400305
Khoronzhuk, Ivan67ac6ff2014-07-04 15:03:25 +0300306#if defined CONFIG_KEYSTONE_RBL_NAND
Khoronzhuk, Ivan67ac6ff2014-07-04 15:03:25 +0300307static struct nand_ecclayout nand_keystone_rbl_4bit_layout_oobfirst = {
Khoronzhuk, Ivanfc12a1f2014-09-02 00:20:02 +0300308#if defined(CONFIG_SYS_NAND_PAGE_2K)
Khoronzhuk, Ivan67ac6ff2014-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, Ivan67ac6ff2014-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, Ivan72061112014-09-06 22:17:07 +0300363 uint32_t offset, int data_len,
Khoronzhuk, Ivan67ac6ff2014-07-04 15:03:25 +0300364 const uint8_t *buf, int oob_required,
Boris Brezillon4d755962017-11-22 02:38:24 +0900365 int page, int raw)
Khoronzhuk, Ivan67ac6ff2014-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 Wood81c77252016-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, Ivan67ac6ff2014-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, Ivan67ac6ff2014-07-04 15:03:25 +0300396 if (status & NAND_STATUS_FAIL) {
397 ret = -EIO;
398 goto err;
399 }
400
Khoronzhuk, Ivan67ac6ff2014-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 Paulraj77b351c2009-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 Chemparathycc41a592010-03-17 10:03:10 -0400484 val = __raw_readl(&davinci_emif_regs->nandfcr);
Nick Thompson97f4eb82009-12-12 12:12:26 -0500485 val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK;
Nick Thompson26be2c52009-12-12 12:13:10 -0500486 val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
Nick Thompson97f4eb82009-12-12 12:12:26 -0500487 val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CONFIG_SYS_NAND_CS);
488 val |= DAVINCI_NANDFCR_4BIT_ECC_START;
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400489 __raw_writel(val, &davinci_emif_regs->nandfcr);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400490 break;
491 case NAND_ECC_READSYN:
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400492 val = __raw_readl(&davinci_emif_regs->nand4bitecc[0]);
Sandeep Paulraj77b351c2009-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 Chemparathycc41a592010-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 Paulraj77b351c2009-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 Thompson20da6f42009-12-16 11:15:58 +0000515 unsigned int hw_4ecc[4];
516 unsigned int i;
Sandeep Paulraj77b351c2009-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 Thompson20da6f42009-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 Paulraj77b351c2009-08-18 10:10:42 -0400524
525 /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
Nick Thompson20da6f42009-12-16 11:15:58 +0000526 *ecc_code++ = hw_ecc_low & 0xFF;
Sandeep Paulraj77b351c2009-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 Thompson20da6f42009-12-16 11:15:58 +0000533 *ecc_code++ =
534 ((hw_ecc_low >> 8) & 0x3) | ((hw_ecc_low >> 14) & 0xFC);
Sandeep Paulraj77b351c2009-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 Thompson20da6f42009-12-16 11:15:58 +0000540 *ecc_code++ =
541 ((hw_ecc_low >> 22) & 0xF) | ((hw_ecc_hi << 4) & 0xF0);
Sandeep Paulraj77b351c2009-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 Thompson20da6f42009-12-16 11:15:58 +0000547 *ecc_code++ =
548 ((hw_ecc_hi >> 4) & 0x3F) | ((hw_ecc_hi >> 10) & 0xC0);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400549
550 /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
Nick Thompson20da6f42009-12-16 11:15:58 +0000551 *ecc_code++ = (hw_ecc_hi >> 18) & 0xFF;
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400552 }
Nick Thompson20da6f42009-12-16 11:15:58 +0000553
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400554 return 0;
555}
556
Sandeep Paulraj77b351c2009-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 Paulraj77b351c2009-08-18 10:10:42 -0400560 int i;
Nick Thompson20da6f42009-12-16 11:15:58 +0000561 unsigned int hw_4ecc[4];
562 unsigned int iserror;
563 unsigned short *ecc16;
Sandeep Paulraj77b351c2009-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 Thompson20da6f42009-12-16 11:15:58 +0000580 ecc16 = (unsigned short *)&read_ecc[0];
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400581
582 /*
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 */
Nick Thompson20da6f42009-12-16 11:15:58 +0000587
588 /*Take 2 bits from 8th byte and 8 bits from 9th byte */
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400589 __raw_writel(((ecc16[4]) >> 6) & 0x3FF,
590 &davinci_emif_regs->nand4biteccload);
Nick Thompson20da6f42009-12-16 11:15:58 +0000591
592 /* Take 4 bits from 7th byte and 6 bits from 8th byte */
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400593 __raw_writel((((ecc16[3]) >> 12) & 0xF) | ((((ecc16[4])) << 4) & 0x3F0),
594 &davinci_emif_regs->nand4biteccload);
Nick Thompson20da6f42009-12-16 11:15:58 +0000595
596 /* Take 6 bits from 6th byte and 4 bits from 7th byte */
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400597 __raw_writel((ecc16[3] >> 2) & 0x3FF,
598 &davinci_emif_regs->nand4biteccload);
Nick Thompson20da6f42009-12-16 11:15:58 +0000599
600 /* Take 8 bits from 5th byte and 2 bits from 6th byte */
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400601 __raw_writel(((ecc16[2]) >> 8) | ((((ecc16[3])) << 8) & 0x300),
602 &davinci_emif_regs->nand4biteccload);
Nick Thompson20da6f42009-12-16 11:15:58 +0000603
604 /*Take 2 bits from 3rd byte and 8 bits from 4th byte */
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400605 __raw_writel((((ecc16[1]) >> 14) & 0x3) | ((((ecc16[2])) << 2) & 0x3FC),
606 &davinci_emif_regs->nand4biteccload);
Nick Thompson20da6f42009-12-16 11:15:58 +0000607
608 /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400609 __raw_writel(((ecc16[1]) >> 4) & 0x3FF,
610 &davinci_emif_regs->nand4biteccload);
Nick Thompson20da6f42009-12-16 11:15:58 +0000611
612 /* Take 6 bits from 1st byte and 4 bits from 2nd byte */
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400613 __raw_writel((((ecc16[0]) >> 10) & 0x3F) | (((ecc16[1]) << 6) & 0x3C0),
614 &davinci_emif_regs->nand4biteccload);
Nick Thompson20da6f42009-12-16 11:15:58 +0000615
616 /* Take 10 bits from 0th and 1st bytes */
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400617 __raw_writel((ecc16[0]) & 0x3FF,
618 &davinci_emif_regs->nand4biteccload);
Sandeep Paulraj77b351c2009-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 Chemparathycc41a592010-03-17 10:03:10 -0400626 val = __raw_readl(&davinci_emif_regs->nandfsr);
Sandeep Paulraj77b351c2009-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 Thompson20da6f42009-12-16 11:15:58 +0000635 if (!(hw_4ecc[0] | hw_4ecc[1] | hw_4ecc[2] | hw_4ecc[3]))
Sandeep Paulraj77b351c2009-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 Chemparathycc41a592010-03-17 10:03:10 -0400642 val = __raw_readl(&davinci_emif_regs->nanderradd1);
Sandeep Paulraj77b351c2009-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 Gardiner10d6ac92010-10-14 17:26:17 -0400648 __raw_writel(DAVINCI_NANDFCR_4BIT_CALC_START,
649 &davinci_emif_regs->nandfcr);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400650
651 /*
Wolfram Sang1075b072010-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 Paulraj77b351c2009-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 Chemparathycc41a592010-03-17 10:03:10 -0400670 val = __raw_readl(&davinci_emif_regs->nandfsr);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400671 val &= 0xc00;
672 i--;
673 } while ((i > 0) && val);
674
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400675 iserror = __raw_readl(&davinci_emif_regs->nandfsr);
Sandeep Paulraj77b351c2009-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 Chemparathycc41a592010-03-17 10:03:10 -0400690 val = __raw_readl(&davinci_emif_regs->nanderrval1);
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400691 return 0;
692 } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400693 val = __raw_readl(&davinci_emif_regs->nanderrval1);
Scott Woodceee07b2016-05-30 13:57:58 -0500694 return -EBADMSG;
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400695 }
696
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400697 numerrors = ((__raw_readl(&davinci_emif_regs->nandfsr) >> 16)
698 & 0x3) + 1;
Sandeep Paulraj77b351c2009-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 Chemparathycc41a592010-03-17 10:03:10 -0400704 ((__raw_readl(&davinci_emif_regs->nanderradd2) >>
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400705 (16 * (i & 1))) & 0x3FF);
706 erroraddress = ((512 + 7) - erroraddress);
707 errorvalue =
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400708 ((__raw_readl(&davinci_emif_regs->nanderrval2) >>
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400709 (16 * (i & 1))) & 0xFF);
710 } else {
711 erroraddress =
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400712 ((__raw_readl(&davinci_emif_regs->nanderradd1) >>
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400713 (16 * (i & 1))) & 0x3FF);
714 erroraddress = ((512 + 7) - erroraddress);
715 errorvalue =
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400716 ((__raw_readl(&davinci_emif_regs->nanderrval1) >>
Sandeep Paulraj77b351c2009-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 Woodd44e9c12009-09-28 16:33:18 -0500726#endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400727
Sergey Kubushync74b2102007-08-10 20:26:18 +0200728static int nand_davinci_dev_ready(struct mtd_info *mtd)
729{
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400730 return __raw_readl(&davinci_emif_regs->nandfsr) & 0x1;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200731}
732
733static void nand_flash_init(void)
734{
David Brownellfcb77472009-04-28 13:19:50 -0700735 /* This is for DM6446 EVM and *very* similar. DO NOT GROW THIS!
736 * Instead, have your board_init() set EMIF timings, based on its
737 * knowledge of the clocks and what devices are hooked up ... and
738 * don't even do that unless no UBL handled it.
739 */
David Brownelled727d32009-07-13 16:29:04 -0700740#ifdef CONFIG_SOC_DM644X
Wolfgang Denk950a3922008-04-11 15:11:26 +0200741 u_int32_t acfg1 = 0x3ffffffc;
Wolfgang Denk950a3922008-04-11 15:11:26 +0200742
743 /*------------------------------------------------------------------*
744 * NAND FLASH CHIP TIMEOUT @ 459 MHz *
745 * *
746 * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
747 * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
748 * *
749 *------------------------------------------------------------------*/
750 acfg1 = 0
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400751 | (0 << 31) /* selectStrobe */
752 | (0 << 30) /* extWait */
753 | (1 << 26) /* writeSetup 10 ns */
754 | (3 << 20) /* writeStrobe 40 ns */
755 | (1 << 17) /* writeHold 10 ns */
756 | (1 << 13) /* readSetup 10 ns */
757 | (5 << 7) /* readStrobe 60 ns */
758 | (1 << 4) /* readHold 10 ns */
759 | (3 << 2) /* turnAround ?? ns */
760 | (0 << 0) /* asyncSize 8-bit bus */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200761 ;
Wolfgang Denk950a3922008-04-11 15:11:26 +0200762
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400763 __raw_writel(acfg1, &davinci_emif_regs->ab1cr); /* CS2 */
Thomas Langed583ef52009-06-20 11:02:17 +0200764
Cyril Chemparathycc41a592010-03-17 10:03:10 -0400765 /* NAND flash on CS2 */
766 __raw_writel(0x00000101, &davinci_emif_regs->nandfcr);
David Brownellfcb77472009-04-28 13:19:50 -0700767#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200768}
769
David Brownell154b5482009-05-10 15:43:01 -0700770void davinci_nand_init(struct nand_chip *nand)
Sergey Kubushync74b2102007-08-10 20:26:18 +0200771{
Khoronzhuk, Ivan67ac6ff2014-07-04 15:03:25 +0300772#if defined CONFIG_KEYSTONE_RBL_NAND
773 int i;
774 struct nand_ecclayout *layout;
775
776 layout = &nand_keystone_rbl_4bit_layout_oobfirst;
777 layout->oobavail = 0;
778 for (i = 0; layout->oobfree[i].length &&
779 i < ARRAY_SIZE(layout->oobfree); i++)
780 layout->oobavail += layout->oobfree[i].length;
781
782 nand->write_page = nand_davinci_write_page;
783 nand->ecc.read_page = nand_davinci_read_page_hwecc;
784#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200785 nand->chip_delay = 0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200786#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
Sergey Lapindfe64e22013-01-14 03:46:50 +0000787 nand->bbt_options |= NAND_BBT_USE_FLASH;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200788#endif
Karicheri, Muralidharan999d7d32014-04-04 13:16:50 -0400789#ifdef CONFIG_SYS_NAND_NO_SUBPAGE_WRITE
790 nand->options |= NAND_NO_SUBPAGE_WRITE;
791#endif
Fabien Parentcf07d392016-11-29 14:31:29 +0100792#ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
793 nand->options |= NAND_BUSWIDTH_16;
794#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200795#ifdef CONFIG_SYS_NAND_HW_ECC
William Juul5e1dae52007-11-09 13:32:30 +0100796 nand->ecc.mode = NAND_ECC_HW;
William Juul5e1dae52007-11-09 13:32:30 +0100797 nand->ecc.size = 512;
798 nand->ecc.bytes = 3;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000799 nand->ecc.strength = 1;
William Juulcfa460a2007-10-31 13:53:06 +0100800 nand->ecc.calculate = nand_davinci_calculate_ecc;
801 nand->ecc.correct = nand_davinci_correct_data;
William Juul4cbb6512007-11-08 10:39:53 +0100802 nand->ecc.hwctl = nand_davinci_enable_hwecc;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200803#else
William Juul5e1dae52007-11-09 13:32:30 +0100804 nand->ecc.mode = NAND_ECC_SOFT;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200805#endif /* CONFIG_SYS_NAND_HW_ECC */
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400806#ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
807 nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
808 nand->ecc.size = 512;
809 nand->ecc.bytes = 10;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000810 nand->ecc.strength = 4;
Sandeep Paulraj77b351c2009-08-18 10:10:42 -0400811 nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
812 nand->ecc.correct = nand_davinci_4bit_correct_data;
813 nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
814 nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
815#endif
Sergey Kubushync74b2102007-08-10 20:26:18 +0200816 /* Set address of hardware control function */
William Juulcfa460a2007-10-31 13:53:06 +0100817 nand->cmd_ctrl = nand_davinci_hwcontrol;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200818
Nick Thompson20da6f42009-12-16 11:15:58 +0000819 nand->read_buf = nand_davinci_read_buf;
820 nand->write_buf = nand_davinci_write_buf;
821
Sergey Kubushync74b2102007-08-10 20:26:18 +0200822 nand->dev_ready = nand_davinci_dev_ready;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200823
824 nand_flash_init();
David Brownell154b5482009-05-10 15:43:01 -0700825}
Sergey Kubushync74b2102007-08-10 20:26:18 +0200826
David Brownell154b5482009-05-10 15:43:01 -0700827int board_nand_init(struct nand_chip *chip) __attribute__((weak));
828
829int board_nand_init(struct nand_chip *chip)
830{
831 davinci_nand_init(chip);
832 return 0;
Sergey Kubushync74b2102007-08-10 20:26:18 +0200833}