Linux 2.6.34-rc3
[pohmelfs.git] / drivers / mtd / nand / omap2.c
blob26aec00801848590333a62252599c5406a345904
1 /*
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.
9 */
11 #include <linux/platform_device.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/delay.h>
14 #include <linux/jiffies.h>
15 #include <linux/sched.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/nand.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/io.h>
21 #include <plat/dma.h>
22 #include <plat/gpmc.h>
23 #include <plat/nand.h>
25 #define GPMC_IRQ_STATUS 0x18
26 #define GPMC_ECC_CONFIG 0x1F4
27 #define GPMC_ECC_CONTROL 0x1F8
28 #define GPMC_ECC_SIZE_CONFIG 0x1FC
29 #define GPMC_ECC1_RESULT 0x200
31 #define DRIVER_NAME "omap2-nand"
33 #define NAND_WP_OFF 0
34 #define NAND_WP_BIT 0x00000010
36 #define GPMC_BUF_FULL 0x00000001
37 #define GPMC_BUF_EMPTY 0x00000000
39 #define NAND_Ecc_P1e (1 << 0)
40 #define NAND_Ecc_P2e (1 << 1)
41 #define NAND_Ecc_P4e (1 << 2)
42 #define NAND_Ecc_P8e (1 << 3)
43 #define NAND_Ecc_P16e (1 << 4)
44 #define NAND_Ecc_P32e (1 << 5)
45 #define NAND_Ecc_P64e (1 << 6)
46 #define NAND_Ecc_P128e (1 << 7)
47 #define NAND_Ecc_P256e (1 << 8)
48 #define NAND_Ecc_P512e (1 << 9)
49 #define NAND_Ecc_P1024e (1 << 10)
50 #define NAND_Ecc_P2048e (1 << 11)
52 #define NAND_Ecc_P1o (1 << 16)
53 #define NAND_Ecc_P2o (1 << 17)
54 #define NAND_Ecc_P4o (1 << 18)
55 #define NAND_Ecc_P8o (1 << 19)
56 #define NAND_Ecc_P16o (1 << 20)
57 #define NAND_Ecc_P32o (1 << 21)
58 #define NAND_Ecc_P64o (1 << 22)
59 #define NAND_Ecc_P128o (1 << 23)
60 #define NAND_Ecc_P256o (1 << 24)
61 #define NAND_Ecc_P512o (1 << 25)
62 #define NAND_Ecc_P1024o (1 << 26)
63 #define NAND_Ecc_P2048o (1 << 27)
65 #define TF(value) (value ? 1 : 0)
67 #define P2048e(a) (TF(a & NAND_Ecc_P2048e) << 0)
68 #define P2048o(a) (TF(a & NAND_Ecc_P2048o) << 1)
69 #define P1e(a) (TF(a & NAND_Ecc_P1e) << 2)
70 #define P1o(a) (TF(a & NAND_Ecc_P1o) << 3)
71 #define P2e(a) (TF(a & NAND_Ecc_P2e) << 4)
72 #define P2o(a) (TF(a & NAND_Ecc_P2o) << 5)
73 #define P4e(a) (TF(a & NAND_Ecc_P4e) << 6)
74 #define P4o(a) (TF(a & NAND_Ecc_P4o) << 7)
76 #define P8e(a) (TF(a & NAND_Ecc_P8e) << 0)
77 #define P8o(a) (TF(a & NAND_Ecc_P8o) << 1)
78 #define P16e(a) (TF(a & NAND_Ecc_P16e) << 2)
79 #define P16o(a) (TF(a & NAND_Ecc_P16o) << 3)
80 #define P32e(a) (TF(a & NAND_Ecc_P32e) << 4)
81 #define P32o(a) (TF(a & NAND_Ecc_P32o) << 5)
82 #define P64e(a) (TF(a & NAND_Ecc_P64e) << 6)
83 #define P64o(a) (TF(a & NAND_Ecc_P64o) << 7)
85 #define P128e(a) (TF(a & NAND_Ecc_P128e) << 0)
86 #define P128o(a) (TF(a & NAND_Ecc_P128o) << 1)
87 #define P256e(a) (TF(a & NAND_Ecc_P256e) << 2)
88 #define P256o(a) (TF(a & NAND_Ecc_P256o) << 3)
89 #define P512e(a) (TF(a & NAND_Ecc_P512e) << 4)
90 #define P512o(a) (TF(a & NAND_Ecc_P512o) << 5)
91 #define P1024e(a) (TF(a & NAND_Ecc_P1024e) << 6)
92 #define P1024o(a) (TF(a & NAND_Ecc_P1024o) << 7)
94 #define P8e_s(a) (TF(a & NAND_Ecc_P8e) << 0)
95 #define P8o_s(a) (TF(a & NAND_Ecc_P8o) << 1)
96 #define P16e_s(a) (TF(a & NAND_Ecc_P16e) << 2)
97 #define P16o_s(a) (TF(a & NAND_Ecc_P16o) << 3)
98 #define P1e_s(a) (TF(a & NAND_Ecc_P1e) << 4)
99 #define P1o_s(a) (TF(a & NAND_Ecc_P1o) << 5)
100 #define P2e_s(a) (TF(a & NAND_Ecc_P2e) << 6)
101 #define P2o_s(a) (TF(a & NAND_Ecc_P2o) << 7)
103 #define P4e_s(a) (TF(a & NAND_Ecc_P4e) << 0)
104 #define P4o_s(a) (TF(a & NAND_Ecc_P4o) << 1)
106 #ifdef CONFIG_MTD_PARTITIONS
107 static const char *part_probes[] = { "cmdlinepart", NULL };
108 #endif
110 #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH
111 static int use_prefetch = 1;
113 /* "modprobe ... use_prefetch=0" etc */
114 module_param(use_prefetch, bool, 0);
115 MODULE_PARM_DESC(use_prefetch, "enable/disable use of PREFETCH");
117 #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH_DMA
118 static int use_dma = 1;
120 /* "modprobe ... use_dma=0" etc */
121 module_param(use_dma, bool, 0);
122 MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
123 #else
124 const int use_dma;
125 #endif
126 #else
127 const int use_prefetch;
128 const int use_dma;
129 #endif
131 struct omap_nand_info {
132 struct nand_hw_control controller;
133 struct omap_nand_platform_data *pdata;
134 struct mtd_info mtd;
135 struct mtd_partition *parts;
136 struct nand_chip nand;
137 struct platform_device *pdev;
139 int gpmc_cs;
140 unsigned long phys_base;
141 void __iomem *gpmc_cs_baseaddr;
142 void __iomem *gpmc_baseaddr;
143 void __iomem *nand_pref_fifo_add;
144 struct completion comp;
145 int dma_ch;
149 * omap_nand_wp - This function enable or disable the Write Protect feature
150 * @mtd: MTD device structure
151 * @mode: WP ON/OFF
153 static void omap_nand_wp(struct mtd_info *mtd, int mode)
155 struct omap_nand_info *info = container_of(mtd,
156 struct omap_nand_info, mtd);
158 unsigned long config = __raw_readl(info->gpmc_baseaddr + GPMC_CONFIG);
160 if (mode)
161 config &= ~(NAND_WP_BIT); /* WP is ON */
162 else
163 config |= (NAND_WP_BIT); /* WP is OFF */
165 __raw_writel(config, (info->gpmc_baseaddr + GPMC_CONFIG));
169 * omap_hwcontrol - hardware specific access to control-lines
170 * @mtd: MTD device structure
171 * @cmd: command to device
172 * @ctrl:
173 * NAND_NCE: bit 0 -> don't care
174 * NAND_CLE: bit 1 -> Command Latch
175 * NAND_ALE: bit 2 -> Address Latch
177 * NOTE: boards may use different bits for these!!
179 static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
181 struct omap_nand_info *info = container_of(mtd,
182 struct omap_nand_info, mtd);
183 switch (ctrl) {
184 case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
185 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
186 GPMC_CS_NAND_COMMAND;
187 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
188 GPMC_CS_NAND_DATA;
189 break;
191 case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
192 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
193 GPMC_CS_NAND_ADDRESS;
194 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
195 GPMC_CS_NAND_DATA;
196 break;
198 case NAND_CTRL_CHANGE | NAND_NCE:
199 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
200 GPMC_CS_NAND_DATA;
201 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
202 GPMC_CS_NAND_DATA;
203 break;
206 if (cmd != NAND_CMD_NONE)
207 __raw_writeb(cmd, info->nand.IO_ADDR_W);
211 * omap_read_buf8 - read data from NAND controller into buffer
212 * @mtd: MTD device structure
213 * @buf: buffer to store date
214 * @len: number of bytes to read
216 static void omap_read_buf8(struct mtd_info *mtd, u_char *buf, int len)
218 struct nand_chip *nand = mtd->priv;
220 ioread8_rep(nand->IO_ADDR_R, buf, len);
224 * omap_write_buf8 - write buffer to NAND controller
225 * @mtd: MTD device structure
226 * @buf: data buffer
227 * @len: number of bytes to write
229 static void omap_write_buf8(struct mtd_info *mtd, const u_char *buf, int len)
231 struct omap_nand_info *info = container_of(mtd,
232 struct omap_nand_info, mtd);
233 u_char *p = (u_char *)buf;
235 while (len--) {
236 iowrite8(*p++, info->nand.IO_ADDR_W);
237 while (GPMC_BUF_EMPTY == (readl(info->gpmc_baseaddr +
238 GPMC_STATUS) & GPMC_BUF_FULL));
243 * omap_read_buf16 - read data from NAND controller into buffer
244 * @mtd: MTD device structure
245 * @buf: buffer to store date
246 * @len: number of bytes to read
248 static void omap_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
250 struct nand_chip *nand = mtd->priv;
252 ioread16_rep(nand->IO_ADDR_R, buf, len / 2);
256 * omap_write_buf16 - write buffer to NAND controller
257 * @mtd: MTD device structure
258 * @buf: data buffer
259 * @len: number of bytes to write
261 static void omap_write_buf16(struct mtd_info *mtd, const u_char * buf, int len)
263 struct omap_nand_info *info = container_of(mtd,
264 struct omap_nand_info, mtd);
265 u16 *p = (u16 *) buf;
267 /* FIXME try bursts of writesw() or DMA ... */
268 len >>= 1;
270 while (len--) {
271 iowrite16(*p++, info->nand.IO_ADDR_W);
273 while (GPMC_BUF_EMPTY == (readl(info->gpmc_baseaddr +
274 GPMC_STATUS) & GPMC_BUF_FULL))
280 * omap_read_buf_pref - read data from NAND controller into buffer
281 * @mtd: MTD device structure
282 * @buf: buffer to store date
283 * @len: number of bytes to read
285 static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
287 struct omap_nand_info *info = container_of(mtd,
288 struct omap_nand_info, mtd);
289 uint32_t pfpw_status = 0, r_count = 0;
290 int ret = 0;
291 u32 *p = (u32 *)buf;
293 /* take care of subpage reads */
294 for (; len % 4 != 0; ) {
295 *buf++ = __raw_readb(info->nand.IO_ADDR_R);
296 len--;
298 p = (u32 *) buf;
300 /* configure and start prefetch transfer */
301 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x0);
302 if (ret) {
303 /* PFPW engine is busy, use cpu copy method */
304 if (info->nand.options & NAND_BUSWIDTH_16)
305 omap_read_buf16(mtd, buf, len);
306 else
307 omap_read_buf8(mtd, buf, len);
308 } else {
309 do {
310 pfpw_status = gpmc_prefetch_status();
311 r_count = ((pfpw_status >> 24) & 0x7F) >> 2;
312 ioread32_rep(info->nand_pref_fifo_add, p, r_count);
313 p += r_count;
314 len -= r_count << 2;
315 } while (len);
317 /* disable and stop the PFPW engine */
318 gpmc_prefetch_reset();
323 * omap_write_buf_pref - write buffer to NAND controller
324 * @mtd: MTD device structure
325 * @buf: data buffer
326 * @len: number of bytes to write
328 static void omap_write_buf_pref(struct mtd_info *mtd,
329 const u_char *buf, int len)
331 struct omap_nand_info *info = container_of(mtd,
332 struct omap_nand_info, mtd);
333 uint32_t pfpw_status = 0, w_count = 0;
334 int i = 0, ret = 0;
335 u16 *p = (u16 *) buf;
337 /* take care of subpage writes */
338 if (len % 2 != 0) {
339 writeb(*buf, info->nand.IO_ADDR_R);
340 p = (u16 *)(buf + 1);
341 len--;
344 /* configure and start prefetch transfer */
345 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x1);
346 if (ret) {
347 /* PFPW engine is busy, use cpu copy method */
348 if (info->nand.options & NAND_BUSWIDTH_16)
349 omap_write_buf16(mtd, buf, len);
350 else
351 omap_write_buf8(mtd, buf, len);
352 } else {
353 pfpw_status = gpmc_prefetch_status();
354 while (pfpw_status & 0x3FFF) {
355 w_count = ((pfpw_status >> 24) & 0x7F) >> 1;
356 for (i = 0; (i < w_count) && len; i++, len -= 2)
357 iowrite16(*p++, info->nand_pref_fifo_add);
358 pfpw_status = gpmc_prefetch_status();
361 /* disable and stop the PFPW engine */
362 gpmc_prefetch_reset();
366 #ifdef CONFIG_MTD_NAND_OMAP_PREFETCH_DMA
368 * omap_nand_dma_cb: callback on the completion of dma transfer
369 * @lch: logical channel
370 * @ch_satuts: channel status
371 * @data: pointer to completion data structure
373 static void omap_nand_dma_cb(int lch, u16 ch_status, void *data)
375 complete((struct completion *) data);
379 * omap_nand_dma_transfer: configer and start dma transfer
380 * @mtd: MTD device structure
381 * @addr: virtual address in RAM of source/destination
382 * @len: number of data bytes to be transferred
383 * @is_write: flag for read/write operation
385 static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
386 unsigned int len, int is_write)
388 struct omap_nand_info *info = container_of(mtd,
389 struct omap_nand_info, mtd);
390 uint32_t prefetch_status = 0;
391 enum dma_data_direction dir = is_write ? DMA_TO_DEVICE :
392 DMA_FROM_DEVICE;
393 dma_addr_t dma_addr;
394 int ret;
396 /* The fifo depth is 64 bytes. We have a sync at each frame and frame
397 * length is 64 bytes.
399 int buf_len = len >> 6;
401 if (addr >= high_memory) {
402 struct page *p1;
404 if (((size_t)addr & PAGE_MASK) !=
405 ((size_t)(addr + len - 1) & PAGE_MASK))
406 goto out_copy;
407 p1 = vmalloc_to_page(addr);
408 if (!p1)
409 goto out_copy;
410 addr = page_address(p1) + ((size_t)addr & ~PAGE_MASK);
413 dma_addr = dma_map_single(&info->pdev->dev, addr, len, dir);
414 if (dma_mapping_error(&info->pdev->dev, dma_addr)) {
415 dev_err(&info->pdev->dev,
416 "Couldn't DMA map a %d byte buffer\n", len);
417 goto out_copy;
420 if (is_write) {
421 omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
422 info->phys_base, 0, 0);
423 omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
424 dma_addr, 0, 0);
425 omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
426 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
427 OMAP24XX_DMA_GPMC, OMAP_DMA_DST_SYNC);
428 } else {
429 omap_set_dma_src_params(info->dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
430 info->phys_base, 0, 0);
431 omap_set_dma_dest_params(info->dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
432 dma_addr, 0, 0);
433 omap_set_dma_transfer_params(info->dma_ch, OMAP_DMA_DATA_TYPE_S32,
434 0x10, buf_len, OMAP_DMA_SYNC_FRAME,
435 OMAP24XX_DMA_GPMC, OMAP_DMA_SRC_SYNC);
437 /* configure and start prefetch transfer */
438 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x1, len, is_write);
439 if (ret)
440 /* PFPW engine is busy, use cpu copy methode */
441 goto out_copy;
443 init_completion(&info->comp);
445 omap_start_dma(info->dma_ch);
447 /* setup and start DMA using dma_addr */
448 wait_for_completion(&info->comp);
450 while (0x3fff & (prefetch_status = gpmc_prefetch_status()))
452 /* disable and stop the PFPW engine */
453 gpmc_prefetch_reset();
455 dma_unmap_single(&info->pdev->dev, dma_addr, len, dir);
456 return 0;
458 out_copy:
459 if (info->nand.options & NAND_BUSWIDTH_16)
460 is_write == 0 ? omap_read_buf16(mtd, (u_char *) addr, len)
461 : omap_write_buf16(mtd, (u_char *) addr, len);
462 else
463 is_write == 0 ? omap_read_buf8(mtd, (u_char *) addr, len)
464 : omap_write_buf8(mtd, (u_char *) addr, len);
465 return 0;
467 #else
468 static void omap_nand_dma_cb(int lch, u16 ch_status, void *data) {}
469 static inline int omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
470 unsigned int len, int is_write)
472 return 0;
474 #endif
477 * omap_read_buf_dma_pref - read data from NAND controller into buffer
478 * @mtd: MTD device structure
479 * @buf: buffer to store date
480 * @len: number of bytes to read
482 static void omap_read_buf_dma_pref(struct mtd_info *mtd, u_char *buf, int len)
484 if (len <= mtd->oobsize)
485 omap_read_buf_pref(mtd, buf, len);
486 else
487 /* start transfer in DMA mode */
488 omap_nand_dma_transfer(mtd, buf, len, 0x0);
492 * omap_write_buf_dma_pref - write buffer to NAND controller
493 * @mtd: MTD device structure
494 * @buf: data buffer
495 * @len: number of bytes to write
497 static void omap_write_buf_dma_pref(struct mtd_info *mtd,
498 const u_char *buf, int len)
500 if (len <= mtd->oobsize)
501 omap_write_buf_pref(mtd, buf, len);
502 else
503 /* start transfer in DMA mode */
504 omap_nand_dma_transfer(mtd, buf, len, 0x1);
508 * omap_verify_buf - Verify chip data against buffer
509 * @mtd: MTD device structure
510 * @buf: buffer containing the data to compare
511 * @len: number of bytes to compare
513 static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
515 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
516 mtd);
517 u16 *p = (u16 *) buf;
519 len >>= 1;
520 while (len--) {
521 if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
522 return -EFAULT;
525 return 0;
528 #ifdef CONFIG_MTD_NAND_OMAP_HWECC
530 * omap_hwecc_init - Initialize the HW ECC for NAND flash in GPMC controller
531 * @mtd: MTD device structure
533 static void omap_hwecc_init(struct mtd_info *mtd)
535 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
536 mtd);
537 struct nand_chip *chip = mtd->priv;
538 unsigned long val = 0x0;
540 /* Read from ECC Control Register */
541 val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_CONTROL);
542 /* Clear all ECC | Enable Reg1 */
543 val = ((0x00000001<<8) | 0x00000001);
544 __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
546 /* Read from ECC Size Config Register */
547 val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_SIZE_CONFIG);
548 /* ECCSIZE1=512 | Select eccResultsize[0-3] */
549 val = ((((chip->ecc.size >> 1) - 1) << 22) | (0x0000000F));
550 __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_SIZE_CONFIG);
554 * gen_true_ecc - This function will generate true ECC value
555 * @ecc_buf: buffer to store ecc code
557 * This generated true ECC value can be used when correcting
558 * data read from NAND flash memory core
560 static void gen_true_ecc(u8 *ecc_buf)
562 u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
563 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
565 ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
566 P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
567 ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
568 P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
569 ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
570 P1e(tmp) | P2048o(tmp) | P2048e(tmp));
574 * omap_compare_ecc - Detect (2 bits) and correct (1 bit) error in data
575 * @ecc_data1: ecc code from nand spare area
576 * @ecc_data2: ecc code from hardware register obtained from hardware ecc
577 * @page_data: page data
579 * This function compares two ECC's and indicates if there is an error.
580 * If the error can be corrected it will be corrected to the buffer.
582 static int omap_compare_ecc(u8 *ecc_data1, /* read from NAND memory */
583 u8 *ecc_data2, /* read from register */
584 u8 *page_data)
586 uint i;
587 u8 tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
588 u8 comp0_bit[8], comp1_bit[8], comp2_bit[8];
589 u8 ecc_bit[24];
590 u8 ecc_sum = 0;
591 u8 find_bit = 0;
592 uint find_byte = 0;
593 int isEccFF;
595 isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
597 gen_true_ecc(ecc_data1);
598 gen_true_ecc(ecc_data2);
600 for (i = 0; i <= 2; i++) {
601 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
602 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
605 for (i = 0; i < 8; i++) {
606 tmp0_bit[i] = *ecc_data1 % 2;
607 *ecc_data1 = *ecc_data1 / 2;
610 for (i = 0; i < 8; i++) {
611 tmp1_bit[i] = *(ecc_data1 + 1) % 2;
612 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
615 for (i = 0; i < 8; i++) {
616 tmp2_bit[i] = *(ecc_data1 + 2) % 2;
617 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
620 for (i = 0; i < 8; i++) {
621 comp0_bit[i] = *ecc_data2 % 2;
622 *ecc_data2 = *ecc_data2 / 2;
625 for (i = 0; i < 8; i++) {
626 comp1_bit[i] = *(ecc_data2 + 1) % 2;
627 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
630 for (i = 0; i < 8; i++) {
631 comp2_bit[i] = *(ecc_data2 + 2) % 2;
632 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
635 for (i = 0; i < 6; i++)
636 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
638 for (i = 0; i < 8; i++)
639 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
641 for (i = 0; i < 8; i++)
642 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
644 ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
645 ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
647 for (i = 0; i < 24; i++)
648 ecc_sum += ecc_bit[i];
650 switch (ecc_sum) {
651 case 0:
652 /* Not reached because this function is not called if
653 * ECC values are equal
655 return 0;
657 case 1:
658 /* Uncorrectable error */
659 DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
660 return -1;
662 case 11:
663 /* UN-Correctable error */
664 DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR B\n");
665 return -1;
667 case 12:
668 /* Correctable error */
669 find_byte = (ecc_bit[23] << 8) +
670 (ecc_bit[21] << 7) +
671 (ecc_bit[19] << 6) +
672 (ecc_bit[17] << 5) +
673 (ecc_bit[15] << 4) +
674 (ecc_bit[13] << 3) +
675 (ecc_bit[11] << 2) +
676 (ecc_bit[9] << 1) +
677 ecc_bit[7];
679 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
681 DEBUG(MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at "
682 "offset: %d, bit: %d\n", find_byte, find_bit);
684 page_data[find_byte] ^= (1 << find_bit);
686 return 0;
687 default:
688 if (isEccFF) {
689 if (ecc_data2[0] == 0 &&
690 ecc_data2[1] == 0 &&
691 ecc_data2[2] == 0)
692 return 0;
694 DEBUG(MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
695 return -1;
700 * omap_correct_data - Compares the ECC read with HW generated ECC
701 * @mtd: MTD device structure
702 * @dat: page data
703 * @read_ecc: ecc read from nand flash
704 * @calc_ecc: ecc read from HW ECC registers
706 * Compares the ecc read from nand spare area with ECC registers values
707 * and if ECC's mismached, it will call 'omap_compare_ecc' for error detection
708 * and correction.
710 static int omap_correct_data(struct mtd_info *mtd, u_char *dat,
711 u_char *read_ecc, u_char *calc_ecc)
713 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
714 mtd);
715 int blockCnt = 0, i = 0, ret = 0;
717 /* Ex NAND_ECC_HW12_2048 */
718 if ((info->nand.ecc.mode == NAND_ECC_HW) &&
719 (info->nand.ecc.size == 2048))
720 blockCnt = 4;
721 else
722 blockCnt = 1;
724 for (i = 0; i < blockCnt; i++) {
725 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
726 ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
727 if (ret < 0)
728 return ret;
730 read_ecc += 3;
731 calc_ecc += 3;
732 dat += 512;
734 return 0;
738 * omap_calcuate_ecc - Generate non-inverted ECC bytes.
739 * @mtd: MTD device structure
740 * @dat: The pointer to data on which ecc is computed
741 * @ecc_code: The ecc_code buffer
743 * Using noninverted ECC can be considered ugly since writing a blank
744 * page ie. padding will clear the ECC bytes. This is no problem as long
745 * nobody is trying to write data on the seemingly unused page. Reading
746 * an erased page will produce an ECC mismatch between generated and read
747 * ECC bytes that has to be dealt with separately.
749 static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
750 u_char *ecc_code)
752 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
753 mtd);
754 unsigned long val = 0x0;
755 unsigned long reg;
757 /* Start Reading from HW ECC1_Result = 0x200 */
758 reg = (unsigned long)(info->gpmc_baseaddr + GPMC_ECC1_RESULT);
759 val = __raw_readl(reg);
760 *ecc_code++ = val; /* P128e, ..., P1e */
761 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
762 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
763 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
764 reg += 4;
766 return 0;
770 * omap_enable_hwecc - This function enables the hardware ecc functionality
771 * @mtd: MTD device structure
772 * @mode: Read/Write mode
774 static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
776 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
777 mtd);
778 struct nand_chip *chip = mtd->priv;
779 unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
780 unsigned long val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_CONFIG);
782 switch (mode) {
783 case NAND_ECC_READ:
784 __raw_writel(0x101, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
785 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
786 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
787 break;
788 case NAND_ECC_READSYN:
789 __raw_writel(0x100, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
790 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
791 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
792 break;
793 case NAND_ECC_WRITE:
794 __raw_writel(0x101, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
795 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
796 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
797 break;
798 default:
799 DEBUG(MTD_DEBUG_LEVEL0, "Error: Unrecognized Mode[%d]!\n",
800 mode);
801 break;
804 __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_CONFIG);
806 #endif
809 * omap_wait - wait until the command is done
810 * @mtd: MTD device structure
811 * @chip: NAND Chip structure
813 * Wait function is called during Program and erase operations and
814 * the way it is called from MTD layer, we should wait till the NAND
815 * chip is ready after the programming/erase operation has completed.
817 * Erase can take up to 400ms and program up to 20ms according to
818 * general NAND and SmartMedia specs
820 static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
822 struct nand_chip *this = mtd->priv;
823 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
824 mtd);
825 unsigned long timeo = jiffies;
826 int status = NAND_STATUS_FAIL, state = this->state;
828 if (state == FL_ERASING)
829 timeo += (HZ * 400) / 1000;
830 else
831 timeo += (HZ * 20) / 1000;
833 this->IO_ADDR_W = (void *) info->gpmc_cs_baseaddr +
834 GPMC_CS_NAND_COMMAND;
835 this->IO_ADDR_R = (void *) info->gpmc_cs_baseaddr + GPMC_CS_NAND_DATA;
837 __raw_writeb(NAND_CMD_STATUS & 0xFF, this->IO_ADDR_W);
839 while (time_before(jiffies, timeo)) {
840 status = __raw_readb(this->IO_ADDR_R);
841 if (status & NAND_STATUS_READY)
842 break;
843 cond_resched();
845 return status;
849 * omap_dev_ready - calls the platform specific dev_ready function
850 * @mtd: MTD device structure
852 static int omap_dev_ready(struct mtd_info *mtd)
854 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
855 mtd);
856 unsigned int val = __raw_readl(info->gpmc_baseaddr + GPMC_IRQ_STATUS);
858 if ((val & 0x100) == 0x100) {
859 /* Clear IRQ Interrupt */
860 val |= 0x100;
861 val &= ~(0x0);
862 __raw_writel(val, info->gpmc_baseaddr + GPMC_IRQ_STATUS);
863 } else {
864 unsigned int cnt = 0;
865 while (cnt++ < 0x1FF) {
866 if ((val & 0x100) == 0x100)
867 return 0;
868 val = __raw_readl(info->gpmc_baseaddr +
869 GPMC_IRQ_STATUS);
873 return 1;
876 static int __devinit omap_nand_probe(struct platform_device *pdev)
878 struct omap_nand_info *info;
879 struct omap_nand_platform_data *pdata;
880 int err;
882 pdata = pdev->dev.platform_data;
883 if (pdata == NULL) {
884 dev_err(&pdev->dev, "platform data missing\n");
885 return -ENODEV;
888 info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
889 if (!info)
890 return -ENOMEM;
892 platform_set_drvdata(pdev, info);
894 spin_lock_init(&info->controller.lock);
895 init_waitqueue_head(&info->controller.wq);
897 info->pdev = pdev;
899 info->gpmc_cs = pdata->cs;
900 info->gpmc_baseaddr = pdata->gpmc_baseaddr;
901 info->gpmc_cs_baseaddr = pdata->gpmc_cs_baseaddr;
902 info->phys_base = pdata->phys_base;
904 info->mtd.priv = &info->nand;
905 info->mtd.name = dev_name(&pdev->dev);
906 info->mtd.owner = THIS_MODULE;
908 info->nand.options |= pdata->devsize ? NAND_BUSWIDTH_16 : 0;
909 info->nand.options |= NAND_SKIP_BBTSCAN;
911 /* NAND write protect off */
912 omap_nand_wp(&info->mtd, NAND_WP_OFF);
914 if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
915 pdev->dev.driver->name)) {
916 err = -EBUSY;
917 goto out_free_info;
920 info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
921 if (!info->nand.IO_ADDR_R) {
922 err = -ENOMEM;
923 goto out_release_mem_region;
926 info->nand.controller = &info->controller;
928 info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
929 info->nand.cmd_ctrl = omap_hwcontrol;
932 * If RDY/BSY line is connected to OMAP then use the omap ready
933 * funcrtion and the generic nand_wait function which reads the status
934 * register after monitoring the RDY/BSY line.Otherwise use a standard
935 * chip delay which is slightly more than tR (AC Timing) of the NAND
936 * device and read status register until you get a failure or success
938 if (pdata->dev_ready) {
939 info->nand.dev_ready = omap_dev_ready;
940 info->nand.chip_delay = 0;
941 } else {
942 info->nand.waitfunc = omap_wait;
943 info->nand.chip_delay = 50;
946 if (use_prefetch) {
947 /* copy the virtual address of nand base for fifo access */
948 info->nand_pref_fifo_add = info->nand.IO_ADDR_R;
950 info->nand.read_buf = omap_read_buf_pref;
951 info->nand.write_buf = omap_write_buf_pref;
952 if (use_dma) {
953 err = omap_request_dma(OMAP24XX_DMA_GPMC, "NAND",
954 omap_nand_dma_cb, &info->comp, &info->dma_ch);
955 if (err < 0) {
956 info->dma_ch = -1;
957 printk(KERN_WARNING "DMA request failed."
958 " Non-dma data transfer mode\n");
959 } else {
960 omap_set_dma_dest_burst_mode(info->dma_ch,
961 OMAP_DMA_DATA_BURST_16);
962 omap_set_dma_src_burst_mode(info->dma_ch,
963 OMAP_DMA_DATA_BURST_16);
965 info->nand.read_buf = omap_read_buf_dma_pref;
966 info->nand.write_buf = omap_write_buf_dma_pref;
969 } else {
970 if (info->nand.options & NAND_BUSWIDTH_16) {
971 info->nand.read_buf = omap_read_buf16;
972 info->nand.write_buf = omap_write_buf16;
973 } else {
974 info->nand.read_buf = omap_read_buf8;
975 info->nand.write_buf = omap_write_buf8;
978 info->nand.verify_buf = omap_verify_buf;
980 #ifdef CONFIG_MTD_NAND_OMAP_HWECC
981 info->nand.ecc.bytes = 3;
982 info->nand.ecc.size = 512;
983 info->nand.ecc.calculate = omap_calculate_ecc;
984 info->nand.ecc.hwctl = omap_enable_hwecc;
985 info->nand.ecc.correct = omap_correct_data;
986 info->nand.ecc.mode = NAND_ECC_HW;
988 /* init HW ECC */
989 omap_hwecc_init(&info->mtd);
990 #else
991 info->nand.ecc.mode = NAND_ECC_SOFT;
992 #endif
994 /* DIP switches on some boards change between 8 and 16 bit
995 * bus widths for flash. Try the other width if the first try fails.
997 if (nand_scan(&info->mtd, 1)) {
998 info->nand.options ^= NAND_BUSWIDTH_16;
999 if (nand_scan(&info->mtd, 1)) {
1000 err = -ENXIO;
1001 goto out_release_mem_region;
1005 #ifdef CONFIG_MTD_PARTITIONS
1006 err = parse_mtd_partitions(&info->mtd, part_probes, &info->parts, 0);
1007 if (err > 0)
1008 add_mtd_partitions(&info->mtd, info->parts, err);
1009 else if (pdata->parts)
1010 add_mtd_partitions(&info->mtd, pdata->parts, pdata->nr_parts);
1011 else
1012 #endif
1013 add_mtd_device(&info->mtd);
1015 platform_set_drvdata(pdev, &info->mtd);
1017 return 0;
1019 out_release_mem_region:
1020 release_mem_region(info->phys_base, NAND_IO_SIZE);
1021 out_free_info:
1022 kfree(info);
1024 return err;
1027 static int omap_nand_remove(struct platform_device *pdev)
1029 struct mtd_info *mtd = platform_get_drvdata(pdev);
1030 struct omap_nand_info *info = mtd->priv;
1032 platform_set_drvdata(pdev, NULL);
1033 if (use_dma)
1034 omap_free_dma(info->dma_ch);
1036 /* Release NAND device, its internal structures and partitions */
1037 nand_release(&info->mtd);
1038 iounmap(info->nand_pref_fifo_add);
1039 kfree(&info->mtd);
1040 return 0;
1043 static struct platform_driver omap_nand_driver = {
1044 .probe = omap_nand_probe,
1045 .remove = omap_nand_remove,
1046 .driver = {
1047 .name = DRIVER_NAME,
1048 .owner = THIS_MODULE,
1052 static int __init omap_nand_init(void)
1054 printk(KERN_INFO "%s driver initializing\n", DRIVER_NAME);
1056 /* This check is required if driver is being
1057 * loaded run time as a module
1059 if ((1 == use_dma) && (0 == use_prefetch)) {
1060 printk(KERN_INFO"Wrong parameters: 'use_dma' can not be 1 "
1061 "without use_prefetch'. Prefetch will not be"
1062 " used in either mode (mpu or dma)\n");
1064 return platform_driver_register(&omap_nand_driver);
1067 static void __exit omap_nand_exit(void)
1069 platform_driver_unregister(&omap_nand_driver);
1072 module_init(omap_nand_init);
1073 module_exit(omap_nand_exit);
1075 MODULE_ALIAS(DRIVER_NAME);
1076 MODULE_LICENSE("GPL");
1077 MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");