blob: 6d3d45019cab85586d103c4c6f654f1afaade925 [file] [log] [blame]
Mike Frysingerbe9d8c72008-11-26 21:43:06 -05001/*
2 * Driver for Blackfin on-chip NAND controller.
3 *
4 * Enter bugs at http://blackfin.uclinux.org/
5 *
6 * Copyright (c) 2007-2008 Analog Devices Inc.
7 *
8 * Licensed under the GPL-2 or later.
9 */
10
11/* TODO:
12 * - move bit defines into mach-common/bits/nand.h
13 * - try and replace all IRQSTAT usage with STAT polling
14 * - have software ecc mode use same algo as hw ecc ?
15 */
16
17#include <common.h>
18#include <asm/io.h>
19
20#ifdef DEBUG
21# define pr_stamp() printf("%s:%s:%i: here i am\n", __FILE__, __func__, __LINE__)
22#else
23# define pr_stamp()
24#endif
25
26#include <nand.h>
27
28#include <asm/blackfin.h>
29
30/* Bit masks for NFC_CTL */
31
32#define WR_DLY 0xf /* Write Strobe Delay */
33#define RD_DLY 0xf0 /* Read Strobe Delay */
34#define NWIDTH 0x100 /* NAND Data Width */
35#define PG_SIZE 0x200 /* Page Size */
36
37/* Bit masks for NFC_STAT */
38
39#define NBUSY 0x1 /* Not Busy */
40#define WB_FULL 0x2 /* Write Buffer Full */
41#define PG_WR_STAT 0x4 /* Page Write Pending */
42#define PG_RD_STAT 0x8 /* Page Read Pending */
43#define WB_EMPTY 0x10 /* Write Buffer Empty */
44
45/* Bit masks for NFC_IRQSTAT */
46
47#define NBUSYIRQ 0x1 /* Not Busy IRQ */
48#define WB_OVF 0x2 /* Write Buffer Overflow */
49#define WB_EDGE 0x4 /* Write Buffer Edge Detect */
50#define RD_RDY 0x8 /* Read Data Ready */
51#define WR_DONE 0x10 /* Page Write Done */
52
53#define NAND_IS_512() (CONFIG_BFIN_NFC_CTL_VAL & 0x200)
54
55/*
56 * hardware specific access to control-lines
57 */
58static void bfin_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
59{
60 pr_stamp();
61
62 if (cmd == NAND_CMD_NONE)
63 return;
64
65 while (bfin_read_NFC_STAT() & WB_FULL)
66 continue;
67
68 if (ctrl & NAND_CLE)
69 bfin_write_NFC_CMD(cmd);
70 else
71 bfin_write_NFC_ADDR(cmd);
72 SSYNC();
73}
74
75int bfin_nfc_devready(struct mtd_info *mtd)
76{
77 pr_stamp();
Andrew Caldwellc941b772010-05-07 15:10:07 -040078 return (bfin_read_NFC_STAT() & NBUSY) ? 1 : 0;
Mike Frysingerbe9d8c72008-11-26 21:43:06 -050079}
80
81/*
82 * PIO mode for buffer writing and reading
83 */
84static void bfin_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
85{
86 pr_stamp();
87
88 int i;
89
90 /*
91 * Data reads are requested by first writing to NFC_DATA_RD
92 * and then reading back from NFC_READ.
93 */
94 for (i = 0; i < len; ++i) {
95 while (bfin_read_NFC_STAT() & WB_FULL)
96 if (ctrlc())
97 return;
98
99 /* Contents do not matter */
100 bfin_write_NFC_DATA_RD(0x0000);
Graf Yanga343ba82009-04-04 07:45:57 -0400101 SSYNC();
Mike Frysingerbe9d8c72008-11-26 21:43:06 -0500102
103 while (!(bfin_read_NFC_IRQSTAT() & RD_RDY))
104 if (ctrlc())
105 return;
106
107 buf[i] = bfin_read_NFC_READ();
108
109 bfin_write_NFC_IRQSTAT(RD_RDY);
110 }
111}
112
113static uint8_t bfin_nfc_read_byte(struct mtd_info *mtd)
114{
115 pr_stamp();
116
117 uint8_t val;
118 bfin_nfc_read_buf(mtd, &val, 1);
119 return val;
120}
121
122static void bfin_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
123{
124 pr_stamp();
125
126 int i;
127
128 for (i = 0; i < len; ++i) {
129 while (bfin_read_NFC_STAT() & WB_FULL)
130 if (ctrlc())
131 return;
132
133 bfin_write_NFC_DATA_WR(buf[i]);
134 }
Andrew Caldwellc941b772010-05-07 15:10:07 -0400135
136 /* Wait for the buffer to drain before we return */
137 while (!(bfin_read_NFC_STAT() & WB_EMPTY))
138 if (ctrlc())
139 return;
Mike Frysingerbe9d8c72008-11-26 21:43:06 -0500140}
141
142/*
143 * ECC functions
144 * These allow the bfin to use the controller's ECC
145 * generator block to ECC the data as it passes through
146 */
147
148/*
149 * ECC error correction function
150 */
151static int bfin_nfc_correct_data_256(struct mtd_info *mtd, u_char *dat,
152 u_char *read_ecc, u_char *calc_ecc)
153{
154 u32 syndrome[5];
155 u32 calced, stored;
156 unsigned short failing_bit, failing_byte;
157 u_char data;
158
159 pr_stamp();
160
161 calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
162 stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
163
164 syndrome[0] = (calced ^ stored);
165
166 /*
167 * syndrome 0: all zero
168 * No error in data
169 * No action
170 */
171 if (!syndrome[0] || !calced || !stored)
172 return 0;
173
174 /*
175 * sysdrome 0: only one bit is one
176 * ECC data was incorrect
177 * No action
178 */
179 if (hweight32(syndrome[0]) == 1)
180 return 1;
181
182 syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
183 syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
184 syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
185 syndrome[4] = syndrome[2] ^ syndrome[3];
186
187 /*
188 * sysdrome 0: exactly 11 bits are one, each parity
189 * and parity' pair is 1 & 0 or 0 & 1.
190 * 1-bit correctable error
191 * Correct the error
192 */
193 if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
194 failing_bit = syndrome[1] & 0x7;
195 failing_byte = syndrome[1] >> 0x3;
196 data = *(dat + failing_byte);
197 data = data ^ (0x1 << failing_bit);
198 *(dat + failing_byte) = data;
199
200 return 0;
201 }
202
203 /*
204 * sysdrome 0: random data
205 * More than 1-bit error, non-correctable error
206 * Discard data, mark bad block
207 */
208
209 return 1;
210}
211
212static int bfin_nfc_correct_data(struct mtd_info *mtd, u_char *dat,
213 u_char *read_ecc, u_char *calc_ecc)
214{
215 int ret;
216
217 pr_stamp();
218
219 ret = bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
220
221 /* If page size is 512, correct second 256 bytes */
222 if (NAND_IS_512()) {
223 dat += 256;
224 read_ecc += 8;
225 calc_ecc += 8;
226 ret |= bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
227 }
228
229 return ret;
230}
231
232static void reset_ecc(void)
233{
234 bfin_write_NFC_RST(0x1);
235 while (bfin_read_NFC_RST() & 1)
236 continue;
237}
238
239static void bfin_nfc_enable_hwecc(struct mtd_info *mtd, int mode)
240{
241 reset_ecc();
242}
243
244static int bfin_nfc_calculate_ecc(struct mtd_info *mtd,
245 const u_char *dat, u_char *ecc_code)
246{
247 u16 ecc0, ecc1;
248 u32 code[2];
249 u8 *p;
250
251 pr_stamp();
252
253 /* first 4 bytes ECC code for 256 page size */
254 ecc0 = bfin_read_NFC_ECC0();
255 ecc1 = bfin_read_NFC_ECC1();
256
257 code[0] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
258
259 /* first 3 bytes in ecc_code for 256 page size */
260 p = (u8 *) code;
261 memcpy(ecc_code, p, 3);
262
263 /* second 4 bytes ECC code for 512 page size */
264 if (NAND_IS_512()) {
265 ecc0 = bfin_read_NFC_ECC2();
266 ecc1 = bfin_read_NFC_ECC3();
267 code[1] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
268
269 /* second 3 bytes in ecc_code for second 256
270 * bytes of 512 page size
271 */
272 p = (u8 *) (code + 1);
273 memcpy((ecc_code + 3), p, 3);
274 }
275
276 reset_ecc();
277
278 return 0;
279}
280
281#ifdef CONFIG_BFIN_NFC_BOOTROM_ECC
282# define BOOTROM_ECC 1
283#else
284# define BOOTROM_ECC 0
285#endif
286
287static uint8_t bbt_pattern[] = { 0xff };
288
289static struct nand_bbt_descr bootrom_bbt = {
290 .options = 0,
291 .offs = 63,
292 .len = 1,
293 .pattern = bbt_pattern,
294};
295
296static struct nand_ecclayout bootrom_ecclayout = {
297 .eccbytes = 24,
298 .eccpos = {
299 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
300 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
301 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
302 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
303 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
304 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
305 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
306 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
307 },
308 .oobfree = {
309 { 0x8 * 0 + 3, 5 },
310 { 0x8 * 1 + 3, 5 },
311 { 0x8 * 2 + 3, 5 },
312 { 0x8 * 3 + 3, 5 },
313 { 0x8 * 4 + 3, 5 },
314 { 0x8 * 5 + 3, 5 },
315 { 0x8 * 6 + 3, 5 },
316 { 0x8 * 7 + 3, 5 },
317 }
318};
319
320/*
321 * Board-specific NAND initialization. The following members of the
322 * argument are board-specific (per include/linux/mtd/nand.h):
323 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
324 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
325 * - cmd_ctrl: hardwarespecific function for accesing control-lines
326 * - dev_ready: hardwarespecific function for accesing device ready/busy line
327 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
328 * only be provided if a hardware ECC is available
329 * - ecc.mode: mode of ecc, see defines
330 * - chip_delay: chip dependent delay for transfering data from array to
331 * read regs (tR)
332 * - options: various chip options. They can partly be set to inform
333 * nand_scan about special functionality. See the defines for further
334 * explanation
335 * Members with a "?" were not set in the merged testing-NAND branch,
336 * so they are not set here either.
337 */
338int board_nand_init(struct nand_chip *chip)
339{
340 pr_stamp();
341
342 /* set width/ecc/timings/etc... */
343 bfin_write_NFC_CTL(CONFIG_BFIN_NFC_CTL_VAL);
344
345 /* clear interrupt status */
346 bfin_write_NFC_IRQMASK(0x0);
347 bfin_write_NFC_IRQSTAT(0xffff);
348
349 /* enable GPIO function enable register */
350#ifdef __ADSPBF54x__
351 bfin_write_PORTJ_FER(bfin_read_PORTJ_FER() | 6);
352#elif defined(__ADSPBF52x__)
353 bfin_write_PORTH_FER(bfin_read_PORTH_FER() | 0xFCFF);
354 bfin_write_PORTH_MUX(0);
355#else
356# error no support for this variant
357#endif
358
359 chip->cmd_ctrl = bfin_nfc_cmd_ctrl;
360 chip->read_buf = bfin_nfc_read_buf;
361 chip->write_buf = bfin_nfc_write_buf;
362 chip->read_byte = bfin_nfc_read_byte;
363
364#ifdef CONFIG_BFIN_NFC_NO_HW_ECC
365# define ECC_HW 0
366#else
367# define ECC_HW 1
368#endif
369 if (ECC_HW) {
370 if (BOOTROM_ECC) {
371 chip->badblock_pattern = &bootrom_bbt;
372 chip->ecc.layout = &bootrom_ecclayout;
373 }
374 if (!NAND_IS_512()) {
375 chip->ecc.bytes = 3;
376 chip->ecc.size = 256;
377 } else {
378 chip->ecc.bytes = 6;
379 chip->ecc.size = 512;
380 }
381 chip->ecc.mode = NAND_ECC_HW;
382 chip->ecc.calculate = bfin_nfc_calculate_ecc;
383 chip->ecc.correct = bfin_nfc_correct_data;
384 chip->ecc.hwctl = bfin_nfc_enable_hwecc;
385 } else
386 chip->ecc.mode = NAND_ECC_SOFT;
387 chip->dev_ready = bfin_nfc_devready;
388 chip->chip_delay = 0;
389
390 return 0;
391}