2 * Platform independend driver for JZ4740.
4 * Copyright (c) 2007 Ingenic Semiconductor Inc.
5 * Author: <jlwei@ingenic.cn>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
16 #include <asm/jz4740.h>
18 #define JZ_NAND_DATA_ADDR ((void __iomem *)0xB8000000)
19 #define JZ_NAND_CMD_ADDR (JZ_NAND_DATA_ADDR + 0x8000)
20 #define JZ_NAND_ADDR_ADDR (JZ_NAND_DATA_ADDR + 0x10000)
22 #define BIT(x) (1 << (x))
23 #define JZ_NAND_ECC_CTRL_ENCODING BIT(3)
24 #define JZ_NAND_ECC_CTRL_RS BIT(2)
25 #define JZ_NAND_ECC_CTRL_RESET BIT(1)
26 #define JZ_NAND_ECC_CTRL_ENABLE BIT(0)
28 #define EMC_SMCR1_OPT_NAND 0x094c4400
29 /* Optimize the timing of nand */
31 static struct jz4740_emc * emc = (struct jz4740_emc *)JZ4740_EMC_BASE;
33 static struct nand_ecclayout qi_lb60_ecclayout_2gb = {
36 12, 13, 14, 15, 16, 17, 18, 19,
37 20, 21, 22, 23, 24, 25, 26, 27,
38 28, 29, 30, 31, 32, 33, 34, 35,
39 36, 37, 38, 39, 40, 41, 42, 43,
40 44, 45, 46, 47, 48, 49, 50, 51,
41 52, 53, 54, 55, 56, 57, 58, 59,
42 60, 61, 62, 63, 64, 65, 66, 67,
43 68, 69, 70, 71, 72, 73, 74, 75,
44 76, 77, 78, 79, 80, 81, 82, 83 },
52 static int is_reading;
54 static void jz_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
56 struct nand_chip *this = mtd->priv;
59 if (ctrl & NAND_CTRL_CHANGE) {
61 this->IO_ADDR_W = JZ_NAND_ADDR_ADDR;
62 else if (ctrl & NAND_CLE)
63 this->IO_ADDR_W = JZ_NAND_CMD_ADDR;
65 this->IO_ADDR_W = JZ_NAND_DATA_ADDR;
67 reg = readl(&emc->nfcsr);
69 reg |= EMC_NFCSR_NFCE1;
71 reg &= ~EMC_NFCSR_NFCE1;
72 writel(reg, &emc->nfcsr);
75 if (cmd != NAND_CMD_NONE)
76 writeb(cmd, this->IO_ADDR_W);
79 static int jz_nand_device_ready(struct mtd_info *mtd)
81 return (readl(GPIO_PXPIN(2)) & 0x40000000) ? 1 : 0;
84 void board_nand_select_device(struct nand_chip *nand, int chip)
87 * Don't use "chip" to address the NAND device,
88 * generate the cs from the address where it is encoded.
92 static int jz_nand_rs_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
102 status = readl(&emc->nfints);
103 } while (!(status & EMC_NFINTS_ENCF));
106 writel(readl(&emc->nfecr) & ~EMC_NFECR_ECCE, &emc->nfecr);
108 for (i = 0; i < 9; i++)
109 ecc_code[i] = readb(&emc->nfpar[i]);
114 static void jz_nand_hwctl(struct mtd_info *mtd, int mode)
118 writel(0, &emc->nfints);
119 reg = readl(&emc->nfecr);
120 reg |= JZ_NAND_ECC_CTRL_RESET;
121 reg |= JZ_NAND_ECC_CTRL_ENABLE;
122 reg |= JZ_NAND_ECC_CTRL_RS;
126 reg &= ~JZ_NAND_ECC_CTRL_ENCODING;
130 reg |= JZ_NAND_ECC_CTRL_ENCODING;
137 writel(reg, &emc->nfecr);
140 /* Correct 1~9-bit errors in 512-bytes data */
141 static void jz_rs_correct(unsigned char *dat, int idx, int mask)
147 i = idx + (idx >> 3);
151 mask <<= (idx & 0x7);
153 dat[i] ^= mask & 0xff;
155 dat[i + 1] ^= (mask >> 8) & 0xff;
158 static int jz_nand_rs_correct_data(struct mtd_info *mtd, u_char *dat,
159 u_char *read_ecc, u_char *calc_ecc)
162 uint32_t errcnt, index, mask, status;
165 const uint8_t all_ff_ecc[] = {
166 0xcd, 0x9d, 0x90, 0x58, 0xf4, 0x8b, 0xff, 0xb7, 0x6f };
168 if (read_ecc[0] == 0xff && read_ecc[1] == 0xff &&
169 read_ecc[2] == 0xff && read_ecc[3] == 0xff &&
170 read_ecc[4] == 0xff && read_ecc[5] == 0xff &&
171 read_ecc[6] == 0xff && read_ecc[7] == 0xff &&
172 read_ecc[8] == 0xff) {
173 for (k = 0; k < 9; k++)
174 writeb(all_ff_ecc[k], &emc->nfpar[k]);
176 for (k = 0; k < 9; k++)
177 writeb(read_ecc[k], &emc->nfpar[k]);
180 writel(readl(&emc->nfecr) | EMC_NFECR_PRDY, &emc->nfecr);
182 /* Wait for completion */
184 status = readl(&emc->nfints);
185 } while (!(status & EMC_NFINTS_DECF));
188 writel(readl(&emc->nfecr) & ~EMC_NFECR_ECCE, &emc->nfecr);
191 if (!(status & EMC_NFINTS_ERR))
194 if (status & EMC_NFINTS_UNCOR) {
195 printf("uncorrectable ecc\n");
199 errcnt = (status & EMC_NFINTS_ERRCNT_MASK) >> EMC_NFINTS_ERRCNT_BIT;
203 index = (readl(&emc->nferr[3]) & EMC_NFERR_INDEX_MASK) >>
205 mask = (readl(&emc->nferr[3]) & EMC_NFERR_MASK_MASK) >>
207 jz_rs_correct(dat, index, mask);
209 index = (readl(&emc->nferr[2]) & EMC_NFERR_INDEX_MASK) >>
211 mask = (readl(&emc->nferr[2]) & EMC_NFERR_MASK_MASK) >>
213 jz_rs_correct(dat, index, mask);
215 index = (readl(&emc->nferr[1]) & EMC_NFERR_INDEX_MASK) >>
217 mask = (readl(&emc->nferr[1]) & EMC_NFERR_MASK_MASK) >>
219 jz_rs_correct(dat, index, mask);
221 index = (readl(&emc->nferr[0]) & EMC_NFERR_INDEX_MASK) >>
223 mask = (readl(&emc->nferr[0]) & EMC_NFERR_MASK_MASK) >>
225 jz_rs_correct(dat, index, mask);
234 * Main initialization routine
236 int board_nand_init(struct nand_chip *nand)
240 reg = readl(&emc->nfcsr);
241 reg |= EMC_NFCSR_NFE1; /* EMC setup, Set NFE bit */
242 writel(reg, &emc->nfcsr);
244 writel(EMC_SMCR1_OPT_NAND, &emc->smcr[1]);
246 nand->IO_ADDR_R = JZ_NAND_DATA_ADDR;
247 nand->IO_ADDR_W = JZ_NAND_DATA_ADDR;
248 nand->cmd_ctrl = jz_nand_cmd_ctrl;
249 nand->dev_ready = jz_nand_device_ready;
250 nand->ecc.hwctl = jz_nand_hwctl;
251 nand->ecc.correct = jz_nand_rs_correct_data;
252 nand->ecc.calculate = jz_nand_rs_calculate_ecc;
253 nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
254 nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
255 nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
256 nand->ecc.layout = &qi_lb60_ecclayout_2gb;
257 nand->chip_delay = 50;
258 nand->options = NAND_USE_FLASH_BBT;