2 * Copyright © 2004 Texas Instruments, Jian Zhang <jzhang@ti.com>
3 * Copyright © 2004 Micron Technology Inc.
4 * Copyright © 2004 David Brownell
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/platform_device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/delay.h>
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/jiffies.h>
17 #include <linux/sched.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/nand.h>
20 #include <linux/mtd/partitions.h>
22 #include <linux/slab.h>
25 #include <plat/gpmc.h>
26 #include <plat/nand.h>
28 #define DRIVER_NAME "omap2-nand"
29 #define OMAP_NAND_TIMEOUT_MS 5000
31 #define NAND_Ecc_P1e (1 << 0)
32 #define NAND_Ecc_P2e (1 << 1)
33 #define NAND_Ecc_P4e (1 << 2)
34 #define NAND_Ecc_P8e (1 << 3)
35 #define NAND_Ecc_P16e (1 << 4)
36 #define NAND_Ecc_P32e (1 << 5)
37 #define NAND_Ecc_P64e (1 << 6)
38 #define NAND_Ecc_P128e (1 << 7)
39 #define NAND_Ecc_P256e (1 << 8)
40 #define NAND_Ecc_P512e (1 << 9)
41 #define NAND_Ecc_P1024e (1 << 10)
42 #define NAND_Ecc_P2048e (1 << 11)
44 #define NAND_Ecc_P1o (1 << 16)
45 #define NAND_Ecc_P2o (1 << 17)
46 #define NAND_Ecc_P4o (1 << 18)
47 #define NAND_Ecc_P8o (1 << 19)
48 #define NAND_Ecc_P16o (1 << 20)
49 #define NAND_Ecc_P32o (1 << 21)
50 #define NAND_Ecc_P64o (1 << 22)
51 #define NAND_Ecc_P128o (1 << 23)
52 #define NAND_Ecc_P256o (1 << 24)
53 #define NAND_Ecc_P512o (1 << 25)
54 #define NAND_Ecc_P1024o (1 << 26)
55 #define NAND_Ecc_P2048o (1 << 27)
57 #define TF(value) (value ? 1 : 0)
59 #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
60 #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
61 #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
62 #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
63 #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
64 #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
65 #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
66 #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
68 #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
69 #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
70 #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
71 #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
72 #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
73 #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
74 #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
75 #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
77 #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
78 #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
79 #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
80 #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
81 #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
82 #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
83 #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
84 #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
86 #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
87 #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
88 #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
89 #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
90 #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
91 #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
92 #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
93 #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
95 #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
96 #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
98 static const char *part_probes
[] = { "cmdlinepart", NULL
};
100 /* oob info generated runtime depending on ecc algorithm and layout selected */
101 static struct nand_ecclayout omap_oobinfo
;
102 /* Define some generic bad / good block scan pattern which are used
103 * while scanning a device for factory marked good / bad blocks
105 static uint8_t scan_ff_pattern
[] = { 0xff };
106 static struct nand_bbt_descr bb_descrip_flashbased
= {
107 .options
= NAND_BBT_SCANEMPTY
| NAND_BBT_SCANALLPAGES
,
110 .pattern
= scan_ff_pattern
,
114 struct omap_nand_info
{
115 struct nand_hw_control controller
;
116 struct omap_nand_platform_data
*pdata
;
118 struct mtd_partition
*parts
;
119 struct nand_chip nand
;
120 struct platform_device
*pdev
;
123 unsigned long phys_base
;
124 struct completion comp
;
128 OMAP_NAND_IO_READ
= 0, /* read */
129 OMAP_NAND_IO_WRITE
, /* write */
136 * omap_hwcontrol - hardware specific access to control-lines
137 * @mtd: MTD device structure
138 * @cmd: command to device
140 * NAND_NCE: bit 0 -> don't care
141 * NAND_CLE: bit 1 -> Command Latch
142 * NAND_ALE: bit 2 -> Address Latch
144 * NOTE: boards may use different bits for these!!
146 static void omap_hwcontrol(struct mtd_info
*mtd
, int cmd
, unsigned int ctrl
)
148 struct omap_nand_info
*info
= container_of(mtd
,
149 struct omap_nand_info
, mtd
);
151 if (cmd
!= NAND_CMD_NONE
) {
153 gpmc_nand_write(info
->gpmc_cs
, GPMC_NAND_COMMAND
, cmd
);
155 else if (ctrl
& NAND_ALE
)
156 gpmc_nand_write(info
->gpmc_cs
, GPMC_NAND_ADDRESS
, cmd
);
159 gpmc_nand_write(info
->gpmc_cs
, GPMC_NAND_DATA
, cmd
);
164 * omap_read_buf8 - read data from NAND controller into buffer
165 * @mtd: MTD device structure
166 * @buf: buffer to store date
167 * @len: number of bytes to read
169 static void omap_read_buf8(struct mtd_info
*mtd
, u_char
*buf
, int len
)
171 struct nand_chip
*nand
= mtd
->priv
;
173 ioread8_rep(nand
->IO_ADDR_R
, buf
, len
);
177 * omap_write_buf8 - write buffer to NAND controller
178 * @mtd: MTD device structure
180 * @len: number of bytes to write
182 static void omap_write_buf8(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
184 struct omap_nand_info
*info
= container_of(mtd
,
185 struct omap_nand_info
, mtd
);
186 u_char
*p
= (u_char
*)buf
;
190 iowrite8(*p
++, info
->nand
.IO_ADDR_W
);
191 /* wait until buffer is available for write */
193 status
= gpmc_read_status(GPMC_STATUS_BUFFER
);
199 * omap_read_buf16 - read data from NAND controller into buffer
200 * @mtd: MTD device structure
201 * @buf: buffer to store date
202 * @len: number of bytes to read
204 static void omap_read_buf16(struct mtd_info
*mtd
, u_char
*buf
, int len
)
206 struct nand_chip
*nand
= mtd
->priv
;
208 ioread16_rep(nand
->IO_ADDR_R
, buf
, len
/ 2);
212 * omap_write_buf16 - write buffer to NAND controller
213 * @mtd: MTD device structure
215 * @len: number of bytes to write
217 static void omap_write_buf16(struct mtd_info
*mtd
, const u_char
* buf
, int len
)
219 struct omap_nand_info
*info
= container_of(mtd
,
220 struct omap_nand_info
, mtd
);
221 u16
*p
= (u16
*) buf
;
223 /* FIXME try bursts of writesw() or DMA ... */
227 iowrite16(*p
++, info
->nand
.IO_ADDR_W
);
228 /* wait until buffer is available for write */
230 status
= gpmc_read_status(GPMC_STATUS_BUFFER
);
236 * omap_read_buf_pref - read data from NAND controller into buffer
237 * @mtd: MTD device structure
238 * @buf: buffer to store date
239 * @len: number of bytes to read
241 static void omap_read_buf_pref(struct mtd_info
*mtd
, u_char
*buf
, int len
)
243 struct omap_nand_info
*info
= container_of(mtd
,
244 struct omap_nand_info
, mtd
);
245 uint32_t r_count
= 0;
249 /* take care of subpage reads */
251 if (info
->nand
.options
& NAND_BUSWIDTH_16
)
252 omap_read_buf16(mtd
, buf
, len
% 4);
254 omap_read_buf8(mtd
, buf
, len
% 4);
255 p
= (u32
*) (buf
+ len
% 4);
259 /* configure and start prefetch transfer */
260 ret
= gpmc_prefetch_enable(info
->gpmc_cs
,
261 PREFETCH_FIFOTHRESHOLD_MAX
, 0x0, len
, 0x0);
263 /* PFPW engine is busy, use cpu copy method */
264 if (info
->nand
.options
& NAND_BUSWIDTH_16
)
265 omap_read_buf16(mtd
, (u_char
*)p
, len
);
267 omap_read_buf8(mtd
, (u_char
*)p
, len
);
270 r_count
= gpmc_read_status(GPMC_PREFETCH_FIFO_CNT
);
271 r_count
= r_count
>> 2;
272 ioread32_rep(info
->nand
.IO_ADDR_R
, p
, r_count
);
276 /* disable and stop the PFPW engine */
277 gpmc_prefetch_reset(info
->gpmc_cs
);
282 * omap_write_buf_pref - write buffer to NAND controller
283 * @mtd: MTD device structure
285 * @len: number of bytes to write
287 static void omap_write_buf_pref(struct mtd_info
*mtd
,
288 const u_char
*buf
, int len
)
290 struct omap_nand_info
*info
= container_of(mtd
,
291 struct omap_nand_info
, mtd
);
292 uint32_t w_count
= 0;
295 unsigned long tim
, limit
;
297 /* take care of subpage writes */
299 writeb(*buf
, info
->nand
.IO_ADDR_W
);
300 p
= (u16
*)(buf
+ 1);
304 /* configure and start prefetch transfer */
305 ret
= gpmc_prefetch_enable(info
->gpmc_cs
,
306 PREFETCH_FIFOTHRESHOLD_MAX
, 0x0, len
, 0x1);
308 /* PFPW engine is busy, use cpu copy method */
309 if (info
->nand
.options
& NAND_BUSWIDTH_16
)
310 omap_write_buf16(mtd
, (u_char
*)p
, len
);
312 omap_write_buf8(mtd
, (u_char
*)p
, len
);
315 w_count
= gpmc_read_status(GPMC_PREFETCH_FIFO_CNT
);
316 w_count
= w_count
>> 1;
317 for (i
= 0; (i
< w_count
) && len
; i
++, len
-= 2)
318 iowrite16(*p
++, info
->nand
.IO_ADDR_W
);
320 /* wait for data to flushed-out before reset the prefetch */
322 limit
= (loops_per_jiffy
*
323 msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS
));
324 while (gpmc_read_status(GPMC_PREFETCH_COUNT
) && (tim
++ < limit
))
327 /* disable and stop the PFPW engine */
328 gpmc_prefetch_reset(info
->gpmc_cs
);
333 * omap_nand_dma_cb: callback on the completion of dma transfer
334 * @lch: logical channel
335 * @ch_satuts: channel status
336 * @data: pointer to completion data structure
338 static void omap_nand_dma_cb(int lch
, u16 ch_status
, void *data
)
340 complete((struct completion
*) data
);
344 * omap_nand_dma_transfer: configer and start dma transfer
345 * @mtd: MTD device structure
346 * @addr: virtual address in RAM of source/destination
347 * @len: number of data bytes to be transferred
348 * @is_write: flag for read/write operation
350 static inline int omap_nand_dma_transfer(struct mtd_info
*mtd
, void *addr
,
351 unsigned int len
, int is_write
)
353 struct omap_nand_info
*info
= container_of(mtd
,
354 struct omap_nand_info
, mtd
);
355 enum dma_data_direction dir
= is_write
? DMA_TO_DEVICE
:
359 unsigned long tim
, limit
;
361 /* The fifo depth is 64 bytes max.
362 * But configure the FIFO-threahold to 32 to get a sync at each frame
363 * and frame length is 32 bytes.
365 int buf_len
= len
>> 6;
367 if (addr
>= high_memory
) {
370 if (((size_t)addr
& PAGE_MASK
) !=
371 ((size_t)(addr
+ len
- 1) & PAGE_MASK
))
373 p1
= vmalloc_to_page(addr
);
376 addr
= page_address(p1
) + ((size_t)addr
& ~PAGE_MASK
);
379 dma_addr
= dma_map_single(&info
->pdev
->dev
, addr
, len
, dir
);
380 if (dma_mapping_error(&info
->pdev
->dev
, dma_addr
)) {
381 dev_err(&info
->pdev
->dev
,
382 "Couldn't DMA map a %d byte buffer\n", len
);
387 omap_set_dma_dest_params(info
->dma_ch
, 0, OMAP_DMA_AMODE_CONSTANT
,
388 info
->phys_base
, 0, 0);
389 omap_set_dma_src_params(info
->dma_ch
, 0, OMAP_DMA_AMODE_POST_INC
,
391 omap_set_dma_transfer_params(info
->dma_ch
, OMAP_DMA_DATA_TYPE_S32
,
392 0x10, buf_len
, OMAP_DMA_SYNC_FRAME
,
393 OMAP24XX_DMA_GPMC
, OMAP_DMA_DST_SYNC
);
395 omap_set_dma_src_params(info
->dma_ch
, 0, OMAP_DMA_AMODE_CONSTANT
,
396 info
->phys_base
, 0, 0);
397 omap_set_dma_dest_params(info
->dma_ch
, 0, OMAP_DMA_AMODE_POST_INC
,
399 omap_set_dma_transfer_params(info
->dma_ch
, OMAP_DMA_DATA_TYPE_S32
,
400 0x10, buf_len
, OMAP_DMA_SYNC_FRAME
,
401 OMAP24XX_DMA_GPMC
, OMAP_DMA_SRC_SYNC
);
403 /* configure and start prefetch transfer */
404 ret
= gpmc_prefetch_enable(info
->gpmc_cs
,
405 PREFETCH_FIFOTHRESHOLD_MAX
, 0x1, len
, is_write
);
407 /* PFPW engine is busy, use cpu copy method */
410 init_completion(&info
->comp
);
412 omap_start_dma(info
->dma_ch
);
414 /* setup and start DMA using dma_addr */
415 wait_for_completion(&info
->comp
);
417 limit
= (loops_per_jiffy
* msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS
));
418 while (gpmc_read_status(GPMC_PREFETCH_COUNT
) && (tim
++ < limit
))
421 /* disable and stop the PFPW engine */
422 gpmc_prefetch_reset(info
->gpmc_cs
);
424 dma_unmap_single(&info
->pdev
->dev
, dma_addr
, len
, dir
);
428 if (info
->nand
.options
& NAND_BUSWIDTH_16
)
429 is_write
== 0 ? omap_read_buf16(mtd
, (u_char
*) addr
, len
)
430 : omap_write_buf16(mtd
, (u_char
*) addr
, len
);
432 is_write
== 0 ? omap_read_buf8(mtd
, (u_char
*) addr
, len
)
433 : omap_write_buf8(mtd
, (u_char
*) addr
, len
);
438 * omap_read_buf_dma_pref - read data from NAND controller into buffer
439 * @mtd: MTD device structure
440 * @buf: buffer to store date
441 * @len: number of bytes to read
443 static void omap_read_buf_dma_pref(struct mtd_info
*mtd
, u_char
*buf
, int len
)
445 if (len
<= mtd
->oobsize
)
446 omap_read_buf_pref(mtd
, buf
, len
);
448 /* start transfer in DMA mode */
449 omap_nand_dma_transfer(mtd
, buf
, len
, 0x0);
453 * omap_write_buf_dma_pref - write buffer to NAND controller
454 * @mtd: MTD device structure
456 * @len: number of bytes to write
458 static void omap_write_buf_dma_pref(struct mtd_info
*mtd
,
459 const u_char
*buf
, int len
)
461 if (len
<= mtd
->oobsize
)
462 omap_write_buf_pref(mtd
, buf
, len
);
464 /* start transfer in DMA mode */
465 omap_nand_dma_transfer(mtd
, (u_char
*) buf
, len
, 0x1);
469 * omap_nand_irq - GMPC irq handler
470 * @this_irq: gpmc irq number
471 * @dev: omap_nand_info structure pointer is passed here
473 static irqreturn_t
omap_nand_irq(int this_irq
, void *dev
)
475 struct omap_nand_info
*info
= (struct omap_nand_info
*) dev
;
479 irq_stat
= gpmc_read_status(GPMC_GET_IRQ_STATUS
);
480 bytes
= gpmc_read_status(GPMC_PREFETCH_FIFO_CNT
);
481 bytes
= bytes
& 0xFFFC; /* io in multiple of 4 bytes */
482 if (info
->iomode
== OMAP_NAND_IO_WRITE
) { /* checks for write io */
486 if (info
->buf_len
&& (info
->buf_len
< bytes
))
487 bytes
= info
->buf_len
;
488 else if (!info
->buf_len
)
490 iowrite32_rep(info
->nand
.IO_ADDR_W
,
491 (u32
*)info
->buf
, bytes
>> 2);
492 info
->buf
= info
->buf
+ bytes
;
493 info
->buf_len
-= bytes
;
496 ioread32_rep(info
->nand
.IO_ADDR_R
,
497 (u32
*)info
->buf
, bytes
>> 2);
498 info
->buf
= info
->buf
+ bytes
;
503 gpmc_cs_configure(info
->gpmc_cs
, GPMC_SET_IRQ_STATUS
, irq_stat
);
508 complete(&info
->comp
);
510 gpmc_cs_configure(info
->gpmc_cs
, GPMC_ENABLE_IRQ
, 0);
513 gpmc_cs_configure(info
->gpmc_cs
, GPMC_SET_IRQ_STATUS
, irq_stat
);
519 * omap_read_buf_irq_pref - read data from NAND controller into buffer
520 * @mtd: MTD device structure
521 * @buf: buffer to store date
522 * @len: number of bytes to read
524 static void omap_read_buf_irq_pref(struct mtd_info
*mtd
, u_char
*buf
, int len
)
526 struct omap_nand_info
*info
= container_of(mtd
,
527 struct omap_nand_info
, mtd
);
530 if (len
<= mtd
->oobsize
) {
531 omap_read_buf_pref(mtd
, buf
, len
);
535 info
->iomode
= OMAP_NAND_IO_READ
;
537 init_completion(&info
->comp
);
539 /* configure and start prefetch transfer */
540 ret
= gpmc_prefetch_enable(info
->gpmc_cs
,
541 PREFETCH_FIFOTHRESHOLD_MAX
/2, 0x0, len
, 0x0);
543 /* PFPW engine is busy, use cpu copy method */
548 gpmc_cs_configure(info
->gpmc_cs
, GPMC_ENABLE_IRQ
,
549 (GPMC_IRQ_FIFOEVENTENABLE
| GPMC_IRQ_COUNT_EVENT
));
551 /* waiting for read to complete */
552 wait_for_completion(&info
->comp
);
554 /* disable and stop the PFPW engine */
555 gpmc_prefetch_reset(info
->gpmc_cs
);
559 if (info
->nand
.options
& NAND_BUSWIDTH_16
)
560 omap_read_buf16(mtd
, buf
, len
);
562 omap_read_buf8(mtd
, buf
, len
);
566 * omap_write_buf_irq_pref - write buffer to NAND controller
567 * @mtd: MTD device structure
569 * @len: number of bytes to write
571 static void omap_write_buf_irq_pref(struct mtd_info
*mtd
,
572 const u_char
*buf
, int len
)
574 struct omap_nand_info
*info
= container_of(mtd
,
575 struct omap_nand_info
, mtd
);
577 unsigned long tim
, limit
;
579 if (len
<= mtd
->oobsize
) {
580 omap_write_buf_pref(mtd
, buf
, len
);
584 info
->iomode
= OMAP_NAND_IO_WRITE
;
585 info
->buf
= (u_char
*) buf
;
586 init_completion(&info
->comp
);
588 /* configure and start prefetch transfer : size=24 */
589 ret
= gpmc_prefetch_enable(info
->gpmc_cs
,
590 (PREFETCH_FIFOTHRESHOLD_MAX
* 3) / 8, 0x0, len
, 0x1);
592 /* PFPW engine is busy, use cpu copy method */
597 gpmc_cs_configure(info
->gpmc_cs
, GPMC_ENABLE_IRQ
,
598 (GPMC_IRQ_FIFOEVENTENABLE
| GPMC_IRQ_COUNT_EVENT
));
600 /* waiting for write to complete */
601 wait_for_completion(&info
->comp
);
602 /* wait for data to flushed-out before reset the prefetch */
604 limit
= (loops_per_jiffy
* msecs_to_jiffies(OMAP_NAND_TIMEOUT_MS
));
605 while (gpmc_read_status(GPMC_PREFETCH_COUNT
) && (tim
++ < limit
))
608 /* disable and stop the PFPW engine */
609 gpmc_prefetch_reset(info
->gpmc_cs
);
613 if (info
->nand
.options
& NAND_BUSWIDTH_16
)
614 omap_write_buf16(mtd
, buf
, len
);
616 omap_write_buf8(mtd
, buf
, len
);
620 * omap_verify_buf - Verify chip data against buffer
621 * @mtd: MTD device structure
622 * @buf: buffer containing the data to compare
623 * @len: number of bytes to compare
625 static int omap_verify_buf(struct mtd_info
*mtd
, const u_char
* buf
, int len
)
627 struct omap_nand_info
*info
= container_of(mtd
, struct omap_nand_info
,
629 u16
*p
= (u16
*) buf
;
633 if (*p
++ != cpu_to_le16(readw(info
->nand
.IO_ADDR_R
)))
641 * gen_true_ecc - This function will generate true ECC value
642 * @ecc_buf: buffer to store ecc code
644 * This generated true ECC value can be used when correcting
645 * data read from NAND flash memory core
647 static void gen_true_ecc(u8
*ecc_buf
)
649 u32 tmp
= ecc_buf
[0] | (ecc_buf
[1] << 16) |
650 ((ecc_buf
[2] & 0xF0) << 20) | ((ecc_buf
[2] & 0x0F) << 8);
652 ecc_buf
[0] = ~(P64o(tmp
) | P64e(tmp
) | P32o(tmp
) | P32e(tmp
) |
653 P16o(tmp
) | P16e(tmp
) | P8o(tmp
) | P8e(tmp
));
654 ecc_buf
[1] = ~(P1024o(tmp
) | P1024e(tmp
) | P512o(tmp
) | P512e(tmp
) |
655 P256o(tmp
) | P256e(tmp
) | P128o(tmp
) | P128e(tmp
));
656 ecc_buf
[2] = ~(P4o(tmp
) | P4e(tmp
) | P2o(tmp
) | P2e(tmp
) | P1o(tmp
) |
657 P1e(tmp
) | P2048o(tmp
) | P2048e(tmp
));
661 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
662 * @ecc_data1: ecc code from nand spare area
663 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
664 * @page_data: page data
666 * This function compares two ECC's and indicates if there is an error.
667 * If the error can be corrected it will be corrected to the buffer.
668 * If there is no error, %0 is returned. If there is an error but it
669 * was corrected, %1 is returned. Otherwise, %-1 is returned.
671 static int omap_compare_ecc(u8
*ecc_data1
, /* read from NAND memory */
672 u8
*ecc_data2
, /* read from register */
676 u8 tmp0_bit
[8], tmp1_bit
[8], tmp2_bit
[8];
677 u8 comp0_bit
[8], comp1_bit
[8], comp2_bit
[8];
684 isEccFF
= ((*(u32
*)ecc_data1
& 0xFFFFFF) == 0xFFFFFF);
686 gen_true_ecc(ecc_data1
);
687 gen_true_ecc(ecc_data2
);
689 for (i
= 0; i
<= 2; i
++) {
690 *(ecc_data1
+ i
) = ~(*(ecc_data1
+ i
));
691 *(ecc_data2
+ i
) = ~(*(ecc_data2
+ i
));
694 for (i
= 0; i
< 8; i
++) {
695 tmp0_bit
[i
] = *ecc_data1
% 2;
696 *ecc_data1
= *ecc_data1
/ 2;
699 for (i
= 0; i
< 8; i
++) {
700 tmp1_bit
[i
] = *(ecc_data1
+ 1) % 2;
701 *(ecc_data1
+ 1) = *(ecc_data1
+ 1) / 2;
704 for (i
= 0; i
< 8; i
++) {
705 tmp2_bit
[i
] = *(ecc_data1
+ 2) % 2;
706 *(ecc_data1
+ 2) = *(ecc_data1
+ 2) / 2;
709 for (i
= 0; i
< 8; i
++) {
710 comp0_bit
[i
] = *ecc_data2
% 2;
711 *ecc_data2
= *ecc_data2
/ 2;
714 for (i
= 0; i
< 8; i
++) {
715 comp1_bit
[i
] = *(ecc_data2
+ 1) % 2;
716 *(ecc_data2
+ 1) = *(ecc_data2
+ 1) / 2;
719 for (i
= 0; i
< 8; i
++) {
720 comp2_bit
[i
] = *(ecc_data2
+ 2) % 2;
721 *(ecc_data2
+ 2) = *(ecc_data2
+ 2) / 2;
724 for (i
= 0; i
< 6; i
++)
725 ecc_bit
[i
] = tmp2_bit
[i
+ 2] ^ comp2_bit
[i
+ 2];
727 for (i
= 0; i
< 8; i
++)
728 ecc_bit
[i
+ 6] = tmp0_bit
[i
] ^ comp0_bit
[i
];
730 for (i
= 0; i
< 8; i
++)
731 ecc_bit
[i
+ 14] = tmp1_bit
[i
] ^ comp1_bit
[i
];
733 ecc_bit
[22] = tmp2_bit
[0] ^ comp2_bit
[0];
734 ecc_bit
[23] = tmp2_bit
[1] ^ comp2_bit
[1];
736 for (i
= 0; i
< 24; i
++)
737 ecc_sum
+= ecc_bit
[i
];
741 /* Not reached because this function is not called if
742 * ECC values are equal
747 /* Uncorrectable error */
748 DEBUG(MTD_DEBUG_LEVEL0
, "ECC UNCORRECTED_ERROR 1\n");
752 /* UN-Correctable error */
753 DEBUG(MTD_DEBUG_LEVEL0
, "ECC UNCORRECTED_ERROR B\n");
757 /* Correctable error */
758 find_byte
= (ecc_bit
[23] << 8) +
768 find_bit
= (ecc_bit
[5] << 2) + (ecc_bit
[3] << 1) + ecc_bit
[1];
770 DEBUG(MTD_DEBUG_LEVEL0
, "Correcting single bit ECC error at "
771 "offset: %d, bit: %d\n", find_byte
, find_bit
);
773 page_data
[find_byte
] ^= (1 << find_bit
);
778 if (ecc_data2
[0] == 0 &&
783 DEBUG(MTD_DEBUG_LEVEL0
, "UNCORRECTED_ERROR default\n");
789 * omap_correct_data - Compares the ECC read with HW generated ECC
790 * @mtd: MTD device structure
792 * @read_ecc: ecc read from nand flash
793 * @calc_ecc: ecc read from HW ECC registers
795 * Compares the ecc read from nand spare area with ECC registers values
796 * and if ECC's mismatched, it will call 'omap_compare_ecc' for error
797 * detection and correction. If there are no errors, %0 is returned. If
798 * there were errors and all of the errors were corrected, the number of
799 * corrected errors is returned. If uncorrectable errors exist, %-1 is
802 static int omap_correct_data(struct mtd_info
*mtd
, u_char
*dat
,
803 u_char
*read_ecc
, u_char
*calc_ecc
)
805 struct omap_nand_info
*info
= container_of(mtd
, struct omap_nand_info
,
807 int blockCnt
= 0, i
= 0, ret
= 0;
810 /* Ex NAND_ECC_HW12_2048 */
811 if ((info
->nand
.ecc
.mode
== NAND_ECC_HW
) &&
812 (info
->nand
.ecc
.size
== 2048))
817 for (i
= 0; i
< blockCnt
; i
++) {
818 if (memcmp(read_ecc
, calc_ecc
, 3) != 0) {
819 ret
= omap_compare_ecc(read_ecc
, calc_ecc
, dat
);
822 /* keep track of the number of corrected errors */
833 * omap_calcuate_ecc - Generate non-inverted ECC bytes.
834 * @mtd: MTD device structure
835 * @dat: The pointer to data on which ecc is computed
836 * @ecc_code: The ecc_code buffer
838 * Using noninverted ECC can be considered ugly since writing a blank
839 * page ie. padding will clear the ECC bytes. This is no problem as long
840 * nobody is trying to write data on the seemingly unused page. Reading
841 * an erased page will produce an ECC mismatch between generated and read
842 * ECC bytes that has to be dealt with separately.
844 static int omap_calculate_ecc(struct mtd_info
*mtd
, const u_char
*dat
,
847 struct omap_nand_info
*info
= container_of(mtd
, struct omap_nand_info
,
849 return gpmc_calculate_ecc(info
->gpmc_cs
, dat
, ecc_code
);
853 * omap_enable_hwecc - This function enables the hardware ecc functionality
854 * @mtd: MTD device structure
855 * @mode: Read/Write mode
857 static void omap_enable_hwecc(struct mtd_info
*mtd
, int mode
)
859 struct omap_nand_info
*info
= container_of(mtd
, struct omap_nand_info
,
861 struct nand_chip
*chip
= mtd
->priv
;
862 unsigned int dev_width
= (chip
->options
& NAND_BUSWIDTH_16
) ? 1 : 0;
864 gpmc_enable_hwecc(info
->gpmc_cs
, mode
, dev_width
, info
->nand
.ecc
.size
);
868 * omap_wait - wait until the command is done
869 * @mtd: MTD device structure
870 * @chip: NAND Chip structure
872 * Wait function is called during Program and erase operations and
873 * the way it is called from MTD layer, we should wait till the NAND
874 * chip is ready after the programming/erase operation has completed.
876 * Erase can take up to 400ms and program up to 20ms according to
877 * general NAND and SmartMedia specs
879 static int omap_wait(struct mtd_info
*mtd
, struct nand_chip
*chip
)
881 struct nand_chip
*this = mtd
->priv
;
882 struct omap_nand_info
*info
= container_of(mtd
, struct omap_nand_info
,
884 unsigned long timeo
= jiffies
;
885 int status
= NAND_STATUS_FAIL
, state
= this->state
;
887 if (state
== FL_ERASING
)
888 timeo
+= (HZ
* 400) / 1000;
890 timeo
+= (HZ
* 20) / 1000;
892 gpmc_nand_write(info
->gpmc_cs
,
893 GPMC_NAND_COMMAND
, (NAND_CMD_STATUS
& 0xFF));
894 while (time_before(jiffies
, timeo
)) {
895 status
= gpmc_nand_read(info
->gpmc_cs
, GPMC_NAND_DATA
);
896 if (status
& NAND_STATUS_READY
)
904 * omap_dev_ready - calls the platform specific dev_ready function
905 * @mtd: MTD device structure
907 static int omap_dev_ready(struct mtd_info
*mtd
)
909 unsigned int val
= 0;
910 struct omap_nand_info
*info
= container_of(mtd
, struct omap_nand_info
,
913 val
= gpmc_read_status(GPMC_GET_IRQ_STATUS
);
914 if ((val
& 0x100) == 0x100) {
915 /* Clear IRQ Interrupt */
918 gpmc_cs_configure(info
->gpmc_cs
, GPMC_SET_IRQ_STATUS
, val
);
920 unsigned int cnt
= 0;
921 while (cnt
++ < 0x1FF) {
922 if ((val
& 0x100) == 0x100)
924 val
= gpmc_read_status(GPMC_GET_IRQ_STATUS
);
931 static int __devinit
omap_nand_probe(struct platform_device
*pdev
)
933 struct omap_nand_info
*info
;
934 struct omap_nand_platform_data
*pdata
;
938 pdata
= pdev
->dev
.platform_data
;
940 dev_err(&pdev
->dev
, "platform data missing\n");
944 info
= kzalloc(sizeof(struct omap_nand_info
), GFP_KERNEL
);
948 platform_set_drvdata(pdev
, info
);
950 spin_lock_init(&info
->controller
.lock
);
951 init_waitqueue_head(&info
->controller
.wq
);
955 info
->gpmc_cs
= pdata
->cs
;
956 info
->phys_base
= pdata
->phys_base
;
958 info
->mtd
.priv
= &info
->nand
;
959 info
->mtd
.name
= dev_name(&pdev
->dev
);
960 info
->mtd
.owner
= THIS_MODULE
;
962 info
->nand
.options
= pdata
->devsize
;
963 info
->nand
.options
|= NAND_SKIP_BBTSCAN
;
965 /* NAND write protect off */
966 gpmc_cs_configure(info
->gpmc_cs
, GPMC_CONFIG_WP
, 0);
968 if (!request_mem_region(info
->phys_base
, NAND_IO_SIZE
,
969 pdev
->dev
.driver
->name
)) {
974 info
->nand
.IO_ADDR_R
= ioremap(info
->phys_base
, NAND_IO_SIZE
);
975 if (!info
->nand
.IO_ADDR_R
) {
977 goto out_release_mem_region
;
980 info
->nand
.controller
= &info
->controller
;
982 info
->nand
.IO_ADDR_W
= info
->nand
.IO_ADDR_R
;
983 info
->nand
.cmd_ctrl
= omap_hwcontrol
;
986 * If RDY/BSY line is connected to OMAP then use the omap ready
987 * funcrtion and the generic nand_wait function which reads the status
988 * register after monitoring the RDY/BSY line.Otherwise use a standard
989 * chip delay which is slightly more than tR (AC Timing) of the NAND
990 * device and read status register until you get a failure or success
992 if (pdata
->dev_ready
) {
993 info
->nand
.dev_ready
= omap_dev_ready
;
994 info
->nand
.chip_delay
= 0;
996 info
->nand
.waitfunc
= omap_wait
;
997 info
->nand
.chip_delay
= 50;
1000 switch (pdata
->xfer_type
) {
1001 case NAND_OMAP_PREFETCH_POLLED
:
1002 info
->nand
.read_buf
= omap_read_buf_pref
;
1003 info
->nand
.write_buf
= omap_write_buf_pref
;
1006 case NAND_OMAP_POLLED
:
1007 if (info
->nand
.options
& NAND_BUSWIDTH_16
) {
1008 info
->nand
.read_buf
= omap_read_buf16
;
1009 info
->nand
.write_buf
= omap_write_buf16
;
1011 info
->nand
.read_buf
= omap_read_buf8
;
1012 info
->nand
.write_buf
= omap_write_buf8
;
1016 case NAND_OMAP_PREFETCH_DMA
:
1017 err
= omap_request_dma(OMAP24XX_DMA_GPMC
, "NAND",
1018 omap_nand_dma_cb
, &info
->comp
, &info
->dma_ch
);
1021 dev_err(&pdev
->dev
, "DMA request failed!\n");
1022 goto out_release_mem_region
;
1024 omap_set_dma_dest_burst_mode(info
->dma_ch
,
1025 OMAP_DMA_DATA_BURST_16
);
1026 omap_set_dma_src_burst_mode(info
->dma_ch
,
1027 OMAP_DMA_DATA_BURST_16
);
1029 info
->nand
.read_buf
= omap_read_buf_dma_pref
;
1030 info
->nand
.write_buf
= omap_write_buf_dma_pref
;
1034 case NAND_OMAP_PREFETCH_IRQ
:
1035 err
= request_irq(pdata
->gpmc_irq
,
1036 omap_nand_irq
, IRQF_SHARED
, "gpmc-nand", info
);
1038 dev_err(&pdev
->dev
, "requesting irq(%d) error:%d",
1039 pdata
->gpmc_irq
, err
);
1040 goto out_release_mem_region
;
1042 info
->gpmc_irq
= pdata
->gpmc_irq
;
1043 info
->nand
.read_buf
= omap_read_buf_irq_pref
;
1044 info
->nand
.write_buf
= omap_write_buf_irq_pref
;
1050 "xfer_type(%d) not supported!\n", pdata
->xfer_type
);
1052 goto out_release_mem_region
;
1055 info
->nand
.verify_buf
= omap_verify_buf
;
1057 /* selsect the ecc type */
1058 if (pdata
->ecc_opt
== OMAP_ECC_HAMMING_CODE_DEFAULT
)
1059 info
->nand
.ecc
.mode
= NAND_ECC_SOFT
;
1060 else if ((pdata
->ecc_opt
== OMAP_ECC_HAMMING_CODE_HW
) ||
1061 (pdata
->ecc_opt
== OMAP_ECC_HAMMING_CODE_HW_ROMCODE
)) {
1062 info
->nand
.ecc
.bytes
= 3;
1063 info
->nand
.ecc
.size
= 512;
1064 info
->nand
.ecc
.calculate
= omap_calculate_ecc
;
1065 info
->nand
.ecc
.hwctl
= omap_enable_hwecc
;
1066 info
->nand
.ecc
.correct
= omap_correct_data
;
1067 info
->nand
.ecc
.mode
= NAND_ECC_HW
;
1070 /* DIP switches on some boards change between 8 and 16 bit
1071 * bus widths for flash. Try the other width if the first try fails.
1073 if (nand_scan_ident(&info
->mtd
, 1, NULL
)) {
1074 info
->nand
.options
^= NAND_BUSWIDTH_16
;
1075 if (nand_scan_ident(&info
->mtd
, 1, NULL
)) {
1077 goto out_release_mem_region
;
1081 /* rom code layout */
1082 if (pdata
->ecc_opt
== OMAP_ECC_HAMMING_CODE_HW_ROMCODE
) {
1084 if (info
->nand
.options
& NAND_BUSWIDTH_16
)
1088 info
->nand
.badblock_pattern
= &bb_descrip_flashbased
;
1090 omap_oobinfo
.eccbytes
= 3 * (info
->mtd
.oobsize
/16);
1091 for (i
= 0; i
< omap_oobinfo
.eccbytes
; i
++)
1092 omap_oobinfo
.eccpos
[i
] = i
+offset
;
1094 omap_oobinfo
.oobfree
->offset
= offset
+ omap_oobinfo
.eccbytes
;
1095 omap_oobinfo
.oobfree
->length
= info
->mtd
.oobsize
-
1096 (offset
+ omap_oobinfo
.eccbytes
);
1098 info
->nand
.ecc
.layout
= &omap_oobinfo
;
1101 /* second phase scan */
1102 if (nand_scan_tail(&info
->mtd
)) {
1104 goto out_release_mem_region
;
1107 err
= parse_mtd_partitions(&info
->mtd
, part_probes
, &info
->parts
, 0);
1109 mtd_device_register(&info
->mtd
, info
->parts
, err
);
1110 else if (pdata
->parts
)
1111 mtd_device_register(&info
->mtd
, pdata
->parts
, pdata
->nr_parts
);
1113 mtd_device_register(&info
->mtd
, NULL
, 0);
1115 platform_set_drvdata(pdev
, &info
->mtd
);
1119 out_release_mem_region
:
1120 release_mem_region(info
->phys_base
, NAND_IO_SIZE
);
1127 static int omap_nand_remove(struct platform_device
*pdev
)
1129 struct mtd_info
*mtd
= platform_get_drvdata(pdev
);
1130 struct omap_nand_info
*info
= container_of(mtd
, struct omap_nand_info
,
1133 platform_set_drvdata(pdev
, NULL
);
1134 if (info
->dma_ch
!= -1)
1135 omap_free_dma(info
->dma_ch
);
1138 free_irq(info
->gpmc_irq
, info
);
1140 /* Release NAND device, its internal structures and partitions */
1141 nand_release(&info
->mtd
);
1142 iounmap(info
->nand
.IO_ADDR_R
);
1147 static struct platform_driver omap_nand_driver
= {
1148 .probe
= omap_nand_probe
,
1149 .remove
= omap_nand_remove
,
1151 .name
= DRIVER_NAME
,
1152 .owner
= THIS_MODULE
,
1156 static int __init
omap_nand_init(void)
1158 pr_info("%s driver initializing\n", DRIVER_NAME
);
1160 return platform_driver_register(&omap_nand_driver
);
1163 static void __exit
omap_nand_exit(void)
1165 platform_driver_unregister(&omap_nand_driver
);
1168 module_init(omap_nand_init
);
1169 module_exit(omap_nand_exit
);
1171 MODULE_ALIAS("platform:" DRIVER_NAME
);
1172 MODULE_LICENSE("GPL");
1173 MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");