2 * Driver for Blackfin on-chip NAND controller.
4 * Enter bugs at http://blackfin.uclinux.org/
6 * Copyright (c) 2007-2008 Analog Devices Inc.
8 * Licensed under the GPL-2 or later.
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 ?
21 # define pr_stamp() printf("%s:%s:%i: here i am\n", __FILE__, __func__, __LINE__)
28 #include <asm/blackfin.h>
30 /* Bit masks for NFC_CTL */
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 */
37 /* Bit masks for NFC_STAT */
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 */
45 /* Bit masks for NFC_IRQSTAT */
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 */
53 #define NAND_IS_512() (CONFIG_BFIN_NFC_CTL_VAL & 0x200)
56 * hardware specific access to control-lines
58 static void bfin_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
62 if (cmd == NAND_CMD_NONE)
65 while (bfin_read_NFC_STAT() & WB_FULL)
69 bfin_write_NFC_CMD(cmd);
71 bfin_write_NFC_ADDR(cmd);
75 int bfin_nfc_devready(struct mtd_info *mtd)
78 return (bfin_read_NFC_STAT() & NBUSY ? 1 : 0);
82 * PIO mode for buffer writing and reading
84 static void bfin_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
91 * Data reads are requested by first writing to NFC_DATA_RD
92 * and then reading back from NFC_READ.
94 for (i = 0; i < len; ++i) {
95 while (bfin_read_NFC_STAT() & WB_FULL)
99 /* Contents do not matter */
100 bfin_write_NFC_DATA_RD(0x0000);
103 while (!(bfin_read_NFC_IRQSTAT() & RD_RDY))
107 buf[i] = bfin_read_NFC_READ();
109 bfin_write_NFC_IRQSTAT(RD_RDY);
113 static uint8_t bfin_nfc_read_byte(struct mtd_info *mtd)
118 bfin_nfc_read_buf(mtd, &val, 1);
122 static void bfin_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
128 for (i = 0; i < len; ++i) {
129 while (bfin_read_NFC_STAT() & WB_FULL)
133 bfin_write_NFC_DATA_WR(buf[i]);
139 * These allow the bfin to use the controller's ECC
140 * generator block to ECC the data as it passes through
144 * ECC error correction function
146 static int bfin_nfc_correct_data_256(struct mtd_info *mtd, u_char *dat,
147 u_char *read_ecc, u_char *calc_ecc)
151 unsigned short failing_bit, failing_byte;
156 calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
157 stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
159 syndrome[0] = (calced ^ stored);
162 * syndrome 0: all zero
166 if (!syndrome[0] || !calced || !stored)
170 * sysdrome 0: only one bit is one
171 * ECC data was incorrect
174 if (hweight32(syndrome[0]) == 1)
177 syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
178 syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
179 syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
180 syndrome[4] = syndrome[2] ^ syndrome[3];
183 * sysdrome 0: exactly 11 bits are one, each parity
184 * and parity' pair is 1 & 0 or 0 & 1.
185 * 1-bit correctable error
188 if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
189 failing_bit = syndrome[1] & 0x7;
190 failing_byte = syndrome[1] >> 0x3;
191 data = *(dat + failing_byte);
192 data = data ^ (0x1 << failing_bit);
193 *(dat + failing_byte) = data;
199 * sysdrome 0: random data
200 * More than 1-bit error, non-correctable error
201 * Discard data, mark bad block
207 static int bfin_nfc_correct_data(struct mtd_info *mtd, u_char *dat,
208 u_char *read_ecc, u_char *calc_ecc)
214 ret = bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
216 /* If page size is 512, correct second 256 bytes */
221 ret |= bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
227 static void reset_ecc(void)
229 bfin_write_NFC_RST(0x1);
230 while (bfin_read_NFC_RST() & 1)
234 static void bfin_nfc_enable_hwecc(struct mtd_info *mtd, int mode)
239 static int bfin_nfc_calculate_ecc(struct mtd_info *mtd,
240 const u_char *dat, u_char *ecc_code)
248 /* first 4 bytes ECC code for 256 page size */
249 ecc0 = bfin_read_NFC_ECC0();
250 ecc1 = bfin_read_NFC_ECC1();
252 code[0] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
254 /* first 3 bytes in ecc_code for 256 page size */
256 memcpy(ecc_code, p, 3);
258 /* second 4 bytes ECC code for 512 page size */
260 ecc0 = bfin_read_NFC_ECC2();
261 ecc1 = bfin_read_NFC_ECC3();
262 code[1] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
264 /* second 3 bytes in ecc_code for second 256
265 * bytes of 512 page size
267 p = (u8 *) (code + 1);
268 memcpy((ecc_code + 3), p, 3);
276 #ifdef CONFIG_BFIN_NFC_BOOTROM_ECC
277 # define BOOTROM_ECC 1
279 # define BOOTROM_ECC 0
282 static uint8_t bbt_pattern[] = { 0xff };
284 static struct nand_bbt_descr bootrom_bbt = {
288 .pattern = bbt_pattern,
291 static struct nand_ecclayout bootrom_ecclayout = {
294 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
295 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
296 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
297 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
298 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
299 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
300 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
301 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
316 * Board-specific NAND initialization. The following members of the
317 * argument are board-specific (per include/linux/mtd/nand.h):
318 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
319 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
320 * - cmd_ctrl: hardwarespecific function for accesing control-lines
321 * - dev_ready: hardwarespecific function for accesing device ready/busy line
322 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
323 * only be provided if a hardware ECC is available
324 * - ecc.mode: mode of ecc, see defines
325 * - chip_delay: chip dependent delay for transfering data from array to
327 * - options: various chip options. They can partly be set to inform
328 * nand_scan about special functionality. See the defines for further
330 * Members with a "?" were not set in the merged testing-NAND branch,
331 * so they are not set here either.
333 int board_nand_init(struct nand_chip *chip)
337 /* set width/ecc/timings/etc... */
338 bfin_write_NFC_CTL(CONFIG_BFIN_NFC_CTL_VAL);
340 /* clear interrupt status */
341 bfin_write_NFC_IRQMASK(0x0);
342 bfin_write_NFC_IRQSTAT(0xffff);
344 /* enable GPIO function enable register */
346 bfin_write_PORTJ_FER(bfin_read_PORTJ_FER() | 6);
347 #elif defined(__ADSPBF52x__)
348 bfin_write_PORTH_FER(bfin_read_PORTH_FER() | 0xFCFF);
349 bfin_write_PORTH_MUX(0);
351 # error no support for this variant
354 chip->cmd_ctrl = bfin_nfc_cmd_ctrl;
355 chip->read_buf = bfin_nfc_read_buf;
356 chip->write_buf = bfin_nfc_write_buf;
357 chip->read_byte = bfin_nfc_read_byte;
359 #ifdef CONFIG_BFIN_NFC_NO_HW_ECC
366 chip->badblock_pattern = &bootrom_bbt;
367 chip->ecc.layout = &bootrom_ecclayout;
369 if (!NAND_IS_512()) {
371 chip->ecc.size = 256;
374 chip->ecc.size = 512;
376 chip->ecc.mode = NAND_ECC_HW;
377 chip->ecc.calculate = bfin_nfc_calculate_ecc;
378 chip->ecc.correct = bfin_nfc_correct_data;
379 chip->ecc.hwctl = bfin_nfc_enable_hwecc;
381 chip->ecc.mode = NAND_ECC_SOFT;
382 chip->dev_ready = bfin_nfc_devready;
383 chip->chip_delay = 0;