blob: f6a0835b4b7f1b7e899a1a7953c40f8f551a99f5 [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();
78 return (bfin_read_NFC_STAT() & NBUSY ? 1 : 0);
79}
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);
101
102 while (!(bfin_read_NFC_IRQSTAT() & RD_RDY))
103 if (ctrlc())
104 return;
105
106 buf[i] = bfin_read_NFC_READ();
107
108 bfin_write_NFC_IRQSTAT(RD_RDY);
109 }
110}
111
112static uint8_t bfin_nfc_read_byte(struct mtd_info *mtd)
113{
114 pr_stamp();
115
116 uint8_t val;
117 bfin_nfc_read_buf(mtd, &val, 1);
118 return val;
119}
120
121static void bfin_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
122{
123 pr_stamp();
124
125 int i;
126
127 for (i = 0; i < len; ++i) {
128 while (bfin_read_NFC_STAT() & WB_FULL)
129 if (ctrlc())
130 return;
131
132 bfin_write_NFC_DATA_WR(buf[i]);
133 }
134}
135
136/*
137 * ECC functions
138 * These allow the bfin to use the controller's ECC
139 * generator block to ECC the data as it passes through
140 */
141
142/*
143 * ECC error correction function
144 */
145static int bfin_nfc_correct_data_256(struct mtd_info *mtd, u_char *dat,
146 u_char *read_ecc, u_char *calc_ecc)
147{
148 u32 syndrome[5];
149 u32 calced, stored;
150 unsigned short failing_bit, failing_byte;
151 u_char data;
152
153 pr_stamp();
154
155 calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
156 stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
157
158 syndrome[0] = (calced ^ stored);
159
160 /*
161 * syndrome 0: all zero
162 * No error in data
163 * No action
164 */
165 if (!syndrome[0] || !calced || !stored)
166 return 0;
167
168 /*
169 * sysdrome 0: only one bit is one
170 * ECC data was incorrect
171 * No action
172 */
173 if (hweight32(syndrome[0]) == 1)
174 return 1;
175
176 syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
177 syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
178 syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
179 syndrome[4] = syndrome[2] ^ syndrome[3];
180
181 /*
182 * sysdrome 0: exactly 11 bits are one, each parity
183 * and parity' pair is 1 & 0 or 0 & 1.
184 * 1-bit correctable error
185 * Correct the error
186 */
187 if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
188 failing_bit = syndrome[1] & 0x7;
189 failing_byte = syndrome[1] >> 0x3;
190 data = *(dat + failing_byte);
191 data = data ^ (0x1 << failing_bit);
192 *(dat + failing_byte) = data;
193
194 return 0;
195 }
196
197 /*
198 * sysdrome 0: random data
199 * More than 1-bit error, non-correctable error
200 * Discard data, mark bad block
201 */
202
203 return 1;
204}
205
206static int bfin_nfc_correct_data(struct mtd_info *mtd, u_char *dat,
207 u_char *read_ecc, u_char *calc_ecc)
208{
209 int ret;
210
211 pr_stamp();
212
213 ret = bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
214
215 /* If page size is 512, correct second 256 bytes */
216 if (NAND_IS_512()) {
217 dat += 256;
218 read_ecc += 8;
219 calc_ecc += 8;
220 ret |= bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
221 }
222
223 return ret;
224}
225
226static void reset_ecc(void)
227{
228 bfin_write_NFC_RST(0x1);
229 while (bfin_read_NFC_RST() & 1)
230 continue;
231}
232
233static void bfin_nfc_enable_hwecc(struct mtd_info *mtd, int mode)
234{
235 reset_ecc();
236}
237
238static int bfin_nfc_calculate_ecc(struct mtd_info *mtd,
239 const u_char *dat, u_char *ecc_code)
240{
241 u16 ecc0, ecc1;
242 u32 code[2];
243 u8 *p;
244
245 pr_stamp();
246
247 /* first 4 bytes ECC code for 256 page size */
248 ecc0 = bfin_read_NFC_ECC0();
249 ecc1 = bfin_read_NFC_ECC1();
250
251 code[0] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
252
253 /* first 3 bytes in ecc_code for 256 page size */
254 p = (u8 *) code;
255 memcpy(ecc_code, p, 3);
256
257 /* second 4 bytes ECC code for 512 page size */
258 if (NAND_IS_512()) {
259 ecc0 = bfin_read_NFC_ECC2();
260 ecc1 = bfin_read_NFC_ECC3();
261 code[1] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
262
263 /* second 3 bytes in ecc_code for second 256
264 * bytes of 512 page size
265 */
266 p = (u8 *) (code + 1);
267 memcpy((ecc_code + 3), p, 3);
268 }
269
270 reset_ecc();
271
272 return 0;
273}
274
275#ifdef CONFIG_BFIN_NFC_BOOTROM_ECC
276# define BOOTROM_ECC 1
277#else
278# define BOOTROM_ECC 0
279#endif
280
281static uint8_t bbt_pattern[] = { 0xff };
282
283static struct nand_bbt_descr bootrom_bbt = {
284 .options = 0,
285 .offs = 63,
286 .len = 1,
287 .pattern = bbt_pattern,
288};
289
290static struct nand_ecclayout bootrom_ecclayout = {
291 .eccbytes = 24,
292 .eccpos = {
293 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
294 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
295 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
296 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
297 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
298 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
299 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
300 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
301 },
302 .oobfree = {
303 { 0x8 * 0 + 3, 5 },
304 { 0x8 * 1 + 3, 5 },
305 { 0x8 * 2 + 3, 5 },
306 { 0x8 * 3 + 3, 5 },
307 { 0x8 * 4 + 3, 5 },
308 { 0x8 * 5 + 3, 5 },
309 { 0x8 * 6 + 3, 5 },
310 { 0x8 * 7 + 3, 5 },
311 }
312};
313
314/*
315 * Board-specific NAND initialization. The following members of the
316 * argument are board-specific (per include/linux/mtd/nand.h):
317 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
318 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
319 * - cmd_ctrl: hardwarespecific function for accesing control-lines
320 * - dev_ready: hardwarespecific function for accesing device ready/busy line
321 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
322 * only be provided if a hardware ECC is available
323 * - ecc.mode: mode of ecc, see defines
324 * - chip_delay: chip dependent delay for transfering data from array to
325 * read regs (tR)
326 * - options: various chip options. They can partly be set to inform
327 * nand_scan about special functionality. See the defines for further
328 * explanation
329 * Members with a "?" were not set in the merged testing-NAND branch,
330 * so they are not set here either.
331 */
332int board_nand_init(struct nand_chip *chip)
333{
334 pr_stamp();
335
336 /* set width/ecc/timings/etc... */
337 bfin_write_NFC_CTL(CONFIG_BFIN_NFC_CTL_VAL);
338
339 /* clear interrupt status */
340 bfin_write_NFC_IRQMASK(0x0);
341 bfin_write_NFC_IRQSTAT(0xffff);
342
343 /* enable GPIO function enable register */
344#ifdef __ADSPBF54x__
345 bfin_write_PORTJ_FER(bfin_read_PORTJ_FER() | 6);
346#elif defined(__ADSPBF52x__)
347 bfin_write_PORTH_FER(bfin_read_PORTH_FER() | 0xFCFF);
348 bfin_write_PORTH_MUX(0);
349#else
350# error no support for this variant
351#endif
352
353 chip->cmd_ctrl = bfin_nfc_cmd_ctrl;
354 chip->read_buf = bfin_nfc_read_buf;
355 chip->write_buf = bfin_nfc_write_buf;
356 chip->read_byte = bfin_nfc_read_byte;
357
358#ifdef CONFIG_BFIN_NFC_NO_HW_ECC
359# define ECC_HW 0
360#else
361# define ECC_HW 1
362#endif
363 if (ECC_HW) {
364 if (BOOTROM_ECC) {
365 chip->badblock_pattern = &bootrom_bbt;
366 chip->ecc.layout = &bootrom_ecclayout;
367 }
368 if (!NAND_IS_512()) {
369 chip->ecc.bytes = 3;
370 chip->ecc.size = 256;
371 } else {
372 chip->ecc.bytes = 6;
373 chip->ecc.size = 512;
374 }
375 chip->ecc.mode = NAND_ECC_HW;
376 chip->ecc.calculate = bfin_nfc_calculate_ecc;
377 chip->ecc.correct = bfin_nfc_correct_data;
378 chip->ecc.hwctl = bfin_nfc_enable_hwecc;
379 } else
380 chip->ecc.mode = NAND_ECC_SOFT;
381 chip->dev_ready = bfin_nfc_devready;
382 chip->chip_delay = 0;
383
384 return 0;
385}