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>
29 #include <asm/portmux.h>
31 /* Bit masks for NFC_CTL */
33 #define WR_DLY 0xf /* Write Strobe Delay */
34 #define RD_DLY 0xf0 /* Read Strobe Delay */
35 #define NWIDTH 0x100 /* NAND Data Width */
36 #define PG_SIZE 0x200 /* Page Size */
38 /* Bit masks for NFC_STAT */
40 #define NBUSY 0x1 /* Not Busy */
41 #define WB_FULL 0x2 /* Write Buffer Full */
42 #define PG_WR_STAT 0x4 /* Page Write Pending */
43 #define PG_RD_STAT 0x8 /* Page Read Pending */
44 #define WB_EMPTY 0x10 /* Write Buffer Empty */
46 /* Bit masks for NFC_IRQSTAT */
48 #define NBUSYIRQ 0x1 /* Not Busy IRQ */
49 #define WB_OVF 0x2 /* Write Buffer Overflow */
50 #define WB_EDGE 0x4 /* Write Buffer Edge Detect */
51 #define RD_RDY 0x8 /* Read Data Ready */
52 #define WR_DONE 0x10 /* Page Write Done */
54 #define NAND_IS_512() (CONFIG_BFIN_NFC_CTL_VAL & 0x200)
57 * hardware specific access to control-lines
59 static void bfin_nfc_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl)
63 if (cmd == NAND_CMD_NONE)
66 while (bfin_read_NFC_STAT() & WB_FULL)
70 bfin_write_NFC_CMD(cmd);
72 bfin_write_NFC_ADDR(cmd);
76 static int bfin_nfc_devready(struct mtd_info *mtd)
79 return (bfin_read_NFC_STAT() & NBUSY) ? 1 : 0;
83 * PIO mode for buffer writing and reading
85 static void bfin_nfc_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
92 * Data reads are requested by first writing to NFC_DATA_RD
93 * and then reading back from NFC_READ.
95 for (i = 0; i < len; ++i) {
96 while (bfin_read_NFC_STAT() & WB_FULL)
100 /* Contents do not matter */
101 bfin_write_NFC_DATA_RD(0x0000);
104 while (!(bfin_read_NFC_IRQSTAT() & RD_RDY))
108 buf[i] = bfin_read_NFC_READ();
110 bfin_write_NFC_IRQSTAT(RD_RDY);
114 static uint8_t bfin_nfc_read_byte(struct mtd_info *mtd)
119 bfin_nfc_read_buf(mtd, &val, 1);
123 static void bfin_nfc_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
129 for (i = 0; i < len; ++i) {
130 while (bfin_read_NFC_STAT() & WB_FULL)
134 bfin_write_NFC_DATA_WR(buf[i]);
137 /* Wait for the buffer to drain before we return */
138 while (!(bfin_read_NFC_STAT() & WB_EMPTY))
145 * These allow the bfin to use the controller's ECC
146 * generator block to ECC the data as it passes through
150 * ECC error correction function
152 static int bfin_nfc_correct_data_256(struct mtd_info *mtd, u_char *dat,
153 u_char *read_ecc, u_char *calc_ecc)
157 unsigned short failing_bit, failing_byte;
162 calced = calc_ecc[0] | (calc_ecc[1] << 8) | (calc_ecc[2] << 16);
163 stored = read_ecc[0] | (read_ecc[1] << 8) | (read_ecc[2] << 16);
165 syndrome[0] = (calced ^ stored);
168 * syndrome 0: all zero
172 if (!syndrome[0] || !calced || !stored)
176 * sysdrome 0: only one bit is one
177 * ECC data was incorrect
180 if (hweight32(syndrome[0]) == 1)
183 syndrome[1] = (calced & 0x7FF) ^ (stored & 0x7FF);
184 syndrome[2] = (calced & 0x7FF) ^ ((calced >> 11) & 0x7FF);
185 syndrome[3] = (stored & 0x7FF) ^ ((stored >> 11) & 0x7FF);
186 syndrome[4] = syndrome[2] ^ syndrome[3];
189 * sysdrome 0: exactly 11 bits are one, each parity
190 * and parity' pair is 1 & 0 or 0 & 1.
191 * 1-bit correctable error
194 if (hweight32(syndrome[0]) == 11 && syndrome[4] == 0x7FF) {
195 failing_bit = syndrome[1] & 0x7;
196 failing_byte = syndrome[1] >> 0x3;
197 data = *(dat + failing_byte);
198 data = data ^ (0x1 << failing_bit);
199 *(dat + failing_byte) = data;
205 * sysdrome 0: random data
206 * More than 1-bit error, non-correctable error
207 * Discard data, mark bad block
213 static int bfin_nfc_correct_data(struct mtd_info *mtd, u_char *dat,
214 u_char *read_ecc, u_char *calc_ecc)
220 ret = bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
222 /* If page size is 512, correct second 256 bytes */
227 ret |= bfin_nfc_correct_data_256(mtd, dat, read_ecc, calc_ecc);
233 static void reset_ecc(void)
235 bfin_write_NFC_RST(0x1);
236 while (bfin_read_NFC_RST() & 1)
240 static void bfin_nfc_enable_hwecc(struct mtd_info *mtd, int mode)
245 static int bfin_nfc_calculate_ecc(struct mtd_info *mtd,
246 const u_char *dat, u_char *ecc_code)
254 /* first 4 bytes ECC code for 256 page size */
255 ecc0 = bfin_read_NFC_ECC0();
256 ecc1 = bfin_read_NFC_ECC1();
258 code[0] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
260 /* first 3 bytes in ecc_code for 256 page size */
262 memcpy(ecc_code, p, 3);
264 /* second 4 bytes ECC code for 512 page size */
266 ecc0 = bfin_read_NFC_ECC2();
267 ecc1 = bfin_read_NFC_ECC3();
268 code[1] = (ecc0 & 0x7FF) | ((ecc1 & 0x7FF) << 11);
270 /* second 3 bytes in ecc_code for second 256
271 * bytes of 512 page size
273 p = (u8 *) (code + 1);
274 memcpy((ecc_code + 3), p, 3);
282 #ifdef CONFIG_BFIN_NFC_BOOTROM_ECC
283 # define BOOTROM_ECC 1
285 # define BOOTROM_ECC 0
288 static uint8_t bbt_pattern[] = { 0xff };
290 static struct nand_bbt_descr bootrom_bbt = {
294 .pattern = bbt_pattern,
297 static struct nand_ecclayout bootrom_ecclayout = {
300 0x8 * 0, 0x8 * 0 + 1, 0x8 * 0 + 2,
301 0x8 * 1, 0x8 * 1 + 1, 0x8 * 1 + 2,
302 0x8 * 2, 0x8 * 2 + 1, 0x8 * 2 + 2,
303 0x8 * 3, 0x8 * 3 + 1, 0x8 * 3 + 2,
304 0x8 * 4, 0x8 * 4 + 1, 0x8 * 4 + 2,
305 0x8 * 5, 0x8 * 5 + 1, 0x8 * 5 + 2,
306 0x8 * 6, 0x8 * 6 + 1, 0x8 * 6 + 2,
307 0x8 * 7, 0x8 * 7 + 1, 0x8 * 7 + 2
322 * Board-specific NAND initialization. The following members of the
323 * argument are board-specific (per include/linux/mtd/nand.h):
324 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
325 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
326 * - cmd_ctrl: hardwarespecific function for accesing control-lines
327 * - dev_ready: hardwarespecific function for accesing device ready/busy line
328 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
329 * only be provided if a hardware ECC is available
330 * - ecc.mode: mode of ecc, see defines
331 * - chip_delay: chip dependent delay for transfering data from array to
333 * - options: various chip options. They can partly be set to inform
334 * nand_scan about special functionality. See the defines for further
336 * Members with a "?" were not set in the merged testing-NAND branch,
337 * so they are not set here either.
339 int board_nand_init(struct nand_chip *chip)
341 const unsigned short pins[] = {
342 P_NAND_CE, P_NAND_RB, P_NAND_D0, P_NAND_D1, P_NAND_D2,
343 P_NAND_D3, P_NAND_D4, P_NAND_D5, P_NAND_D6, P_NAND_D7,
344 P_NAND_WE, P_NAND_RE, P_NAND_CLE, P_NAND_ALE, 0,
349 /* set width/ecc/timings/etc... */
350 bfin_write_NFC_CTL(CONFIG_BFIN_NFC_CTL_VAL);
352 /* clear interrupt status */
353 bfin_write_NFC_IRQMASK(0x0);
354 bfin_write_NFC_IRQSTAT(0xffff);
356 /* enable GPIO function enable register */
357 peripheral_request_list(pins, "bfin_nand");
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;
364 #ifdef CONFIG_BFIN_NFC_NO_HW_ECC
371 chip->badblock_pattern = &bootrom_bbt;
372 chip->ecc.layout = &bootrom_ecclayout;
374 if (!NAND_IS_512()) {
376 chip->ecc.size = 256;
379 chip->ecc.size = 512;
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;
386 chip->ecc.mode = NAND_ECC_SOFT;
387 chip->dev_ready = bfin_nfc_devready;
388 chip->chip_delay = 0;