]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mtd/nand/omap-hw.c
Merge branch 'omap-fixes'
[linux-2.6-omap-h63xx.git] / drivers / mtd / nand / omap-hw.c
1 /*
2  *  drivers/mtd/nand/omap-hw.c
3  *
4  *  This is the MTD driver for OMAP1710 internal HW NAND controller.
5  *
6  *  Copyright (C) 2004-2006 Nokia Corporation
7  *
8  *  Author: Jarkko Lavinen <jarkko.lavinen@nokia.com> and
9  *          Juha Yrjölä <juha.yrjola@nokia.com>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU General Public License version 2 as published by
13  * the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18  * more details.
19  *
20  * You should have received a copy of the GNU General Public License along with
21  * this program; see the file COPYING. If not, write to the Free Software
22  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  *
24  */
25
26 #include <linux/slab.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
31 #include <linux/sched.h>
32 #include <linux/types.h>
33 #include <linux/wait.h>
34 #include <linux/spinlock.h>
35 #include <linux/interrupt.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/nand.h>
38 #include <linux/mtd/partitions.h>
39 #include <linux/mtd/nand_ecc.h>
40 #include <linux/dma-mapping.h>
41 #include <linux/clk.h>
42
43 #include <asm/io.h>
44
45 #include <mach/board.h>
46 #include <mach/dma.h>
47
48 #define NAND_BASE               0xfffbcc00
49 #define NND_REVISION            0x00
50 #define NND_ACCESS              0x04
51 #define NND_ADDR_SRC            0x08
52 #define NND_CTRL                0x10
53 #define NND_MASK                0x14
54 #define NND_STATUS              0x18
55 #define NND_READY               0x1c
56 #define NND_COMMAND             0x20
57 #define NND_COMMAND_SEC         0x24
58 #define NND_ECC_SELECT          0x28
59 #define NND_ECC_START           0x2c
60 #define NND_ECC_9               0x4c
61 #define NND_RESET               0x50
62 #define NND_FIFO                0x54
63 #define NND_FIFOCTRL            0x58
64 #define NND_PSC_CLK             0x5c
65 #define NND_SYSTEST             0x60
66 #define NND_SYSCFG              0x64
67 #define NND_SYSSTATUS           0x68
68 #define NND_FIFOTEST1           0x6c
69 #define NND_FIFOTEST2           0x70
70 #define NND_FIFOTEST3           0x74
71 #define NND_FIFOTEST4           0x78
72 #define NND_PSC1_CLK            0x8c
73 #define NND_PSC2_CLK            0x90
74
75
76 #define NND_CMD_READ1_LOWER     0x00
77 #define NND_CMD_WRITE1_LOWER    0x00
78 #define NND_CMD_READ1_UPPER     0x01
79 #define NND_CMD_WRITE1_UPPER    0x01
80 #define NND_CMD_PROGRAM_END     0x10
81 #define NND_CMD_READ2_SPARE     0x50
82 #define NND_CMD_WRITE2_SPARE    0x50
83 #define NND_CMD_ERASE           0x60
84 #define NND_CMD_STATUS          0x70
85 #define NND_CMD_PROGRAM         0x80
86 #define NND_CMD_READ_ID         0x90
87 #define NND_CMD_ERASE_END       0xD0
88 #define NND_CMD_RESET           0xFF
89
90
91 #define NAND_Ecc_P1e            (1 << 0)
92 #define NAND_Ecc_P2e            (1 << 1)
93 #define NAND_Ecc_P4e            (1 << 2)
94 #define NAND_Ecc_P8e            (1 << 3)
95 #define NAND_Ecc_P16e           (1 << 4)
96 #define NAND_Ecc_P32e           (1 << 5)
97 #define NAND_Ecc_P64e           (1 << 6)
98 #define NAND_Ecc_P128e          (1 << 7)
99 #define NAND_Ecc_P256e          (1 << 8)
100 #define NAND_Ecc_P512e          (1 << 9)
101 #define NAND_Ecc_P1024e         (1 << 10)
102 #define NAND_Ecc_P2048e         (1 << 11)
103
104 #define NAND_Ecc_P1o            (1 << 16)
105 #define NAND_Ecc_P2o            (1 << 17)
106 #define NAND_Ecc_P4o            (1 << 18)
107 #define NAND_Ecc_P8o            (1 << 19)
108 #define NAND_Ecc_P16o           (1 << 20)
109 #define NAND_Ecc_P32o           (1 << 21)
110 #define NAND_Ecc_P64o           (1 << 22)
111 #define NAND_Ecc_P128o          (1 << 23)
112 #define NAND_Ecc_P256o          (1 << 24)
113 #define NAND_Ecc_P512o          (1 << 25)
114 #define NAND_Ecc_P1024o         (1 << 26)
115 #define NAND_Ecc_P2048o         (1 << 27)
116
117 #define TF(value)       (value ? 1 : 0)
118
119 #define P2048e(a)       (TF(a & NAND_Ecc_P2048e)        << 0 )
120 #define P2048o(a)       (TF(a & NAND_Ecc_P2048o)        << 1 )
121 #define P1e(a)          (TF(a & NAND_Ecc_P1e)           << 2 )
122 #define P1o(a)          (TF(a & NAND_Ecc_P1o)           << 3 )
123 #define P2e(a)          (TF(a & NAND_Ecc_P2e)           << 4 )
124 #define P2o(a)          (TF(a & NAND_Ecc_P2o)           << 5 )
125 #define P4e(a)          (TF(a & NAND_Ecc_P4e)           << 6 )
126 #define P4o(a)          (TF(a & NAND_Ecc_P4o)           << 7 )
127
128 #define P8e(a)          (TF(a & NAND_Ecc_P8e)           << 0 )
129 #define P8o(a)          (TF(a & NAND_Ecc_P8o)           << 1 )
130 #define P16e(a)         (TF(a & NAND_Ecc_P16e)          << 2 )
131 #define P16o(a)         (TF(a & NAND_Ecc_P16o)          << 3 )
132 #define P32e(a)         (TF(a & NAND_Ecc_P32e)          << 4 )
133 #define P32o(a)         (TF(a & NAND_Ecc_P32o)          << 5 )
134 #define P64e(a)         (TF(a & NAND_Ecc_P64e)          << 6 )
135 #define P64o(a)         (TF(a & NAND_Ecc_P64o)          << 7 )
136
137 #define P128e(a)        (TF(a & NAND_Ecc_P128e)         << 0 )
138 #define P128o(a)        (TF(a & NAND_Ecc_P128o)         << 1 )
139 #define P256e(a)        (TF(a & NAND_Ecc_P256e)         << 2 )
140 #define P256o(a)        (TF(a & NAND_Ecc_P256o)         << 3 )
141 #define P512e(a)        (TF(a & NAND_Ecc_P512e)         << 4 )
142 #define P512o(a)        (TF(a & NAND_Ecc_P512o)         << 5 )
143 #define P1024e(a)       (TF(a & NAND_Ecc_P1024e)        << 6 )
144 #define P1024o(a)       (TF(a & NAND_Ecc_P1024o)        << 7 )
145
146 #define P8e_s(a)        (TF(a & NAND_Ecc_P8e)           << 0 )
147 #define P8o_s(a)        (TF(a & NAND_Ecc_P8o)           << 1 )
148 #define P16e_s(a)       (TF(a & NAND_Ecc_P16e)          << 2 )
149 #define P16o_s(a)       (TF(a & NAND_Ecc_P16o)          << 3 )
150 #define P1e_s(a)        (TF(a & NAND_Ecc_P1e)           << 4 )
151 #define P1o_s(a)        (TF(a & NAND_Ecc_P1o)           << 5 )
152 #define P2e_s(a)        (TF(a & NAND_Ecc_P2e)           << 6 )
153 #define P2o_s(a)        (TF(a & NAND_Ecc_P2o)           << 7 )
154
155 #define P4e_s(a)        (TF(a & NAND_Ecc_P4e)           << 0 )
156 #define P4o_s(a)        (TF(a & NAND_Ecc_P4o)           << 1 )
157
158 extern struct nand_oobinfo jffs2_oobinfo;
159
160 /*
161  * MTD structure for OMAP board
162  */
163 static struct mtd_info *omap_mtd;
164 static struct clk *omap_nand_clk;
165 static int omap_nand_dma_ch;
166 static struct completion omap_nand_dma_comp;
167 static unsigned long omap_nand_base = OMAP1_IO_ADDRESS(NAND_BASE);
168
169 static inline u32 nand_read_reg(int idx)
170 {
171         return __raw_readl(omap_nand_base + idx);
172 }
173
174 static inline void nand_write_reg(int idx, u32 val)
175 {
176         __raw_writel(val, omap_nand_base + idx);
177 }
178
179 static inline u8 nand_read_reg8(int idx)
180 {
181         return __raw_readb(omap_nand_base + idx);
182 }
183
184 static inline void nand_write_reg8(int idx, u8 val)
185 {
186         __raw_writeb(val, omap_nand_base + idx);
187 }
188
189 static void omap_nand_select_chip(struct mtd_info *mtd, int chip)
190 {
191         u32 l;
192
193         switch(chip) {
194         case -1:
195                 l = nand_read_reg(NND_CTRL);
196                 l |= (1 << 8) | (1 << 10) | (1 << 12) | (1 << 14);
197                 nand_write_reg(NND_CTRL, l);
198                 break;
199         case 0:
200                 /* Also CS1, CS2, CS4 would be available */
201                 l = nand_read_reg(NND_CTRL);
202                 l &= ~(1 << 8);
203                 nand_write_reg(NND_CTRL, l);
204                 break;
205         default:
206                 BUG();
207         }
208 }
209
210 static void nand_dma_cb(int lch, u16 ch_status, void *data)
211 {
212         complete((struct completion *) data);
213 }
214
215 static void omap_nand_dma_transfer(struct mtd_info *mtd, void *addr,
216                                          unsigned int u32_count, int is_write)
217 {
218         const int block_size = 16;
219         unsigned int block_count, len;
220         int dma_ch;
221         unsigned long fifo_reg, timeout, jiffies_before, jiffies_spent;
222         static unsigned long max_jiffies = 0;
223
224         dma_ch = omap_nand_dma_ch;
225         block_count = u32_count * 4 / block_size;
226         nand_write_reg(NND_STATUS, 0x0f);
227         nand_write_reg(NND_FIFOCTRL, (block_size << 24) | block_count);
228         fifo_reg = NAND_BASE + NND_FIFO;
229         if (is_write) {
230                 omap_set_dma_dest_params(dma_ch, OMAP_DMA_PORT_TIPB,
231                                          OMAP_DMA_AMODE_CONSTANT, fifo_reg,
232                                          0, 0);
233                 omap_set_dma_src_params(dma_ch, OMAP_DMA_PORT_EMIFF,
234                                         OMAP_DMA_AMODE_POST_INC,
235                                         virt_to_phys(addr),
236                                         0, 0);
237 //              omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
238                 /* Set POSTWRITE bit */
239                 nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) | (1 << 16));
240         } else {
241                 omap_set_dma_src_params(dma_ch, OMAP_DMA_PORT_TIPB,
242                                         OMAP_DMA_AMODE_CONSTANT, fifo_reg,
243                                         0, 0);
244                 omap_set_dma_dest_params(dma_ch, OMAP_DMA_PORT_EMIFF,
245                                          OMAP_DMA_AMODE_POST_INC,
246                                          virt_to_phys(addr),
247                                          0, 0);
248 //              omap_set_dma_dest_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_8);
249                 /* Set PREFETCH bit */
250                 nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) | (1 << 17));
251         }
252         omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32, block_size / 4,
253                                      block_count, OMAP_DMA_SYNC_FRAME,
254                                      0, 0);
255         init_completion(&omap_nand_dma_comp);
256
257         len = u32_count << 2;
258         dma_cache_maint(addr, len, DMA_TO_DEVICE);
259         omap_start_dma(dma_ch);
260         jiffies_before = jiffies;
261         timeout = wait_for_completion_timeout(&omap_nand_dma_comp,
262                                               msecs_to_jiffies(1000));
263         jiffies_spent = (unsigned long)((long)jiffies - (long)jiffies_before);
264         if (jiffies_spent > max_jiffies)
265                 max_jiffies = jiffies_spent;
266
267         if (timeout == 0) {
268                 printk(KERN_WARNING "omap-hw-nand: DMA timeout after %u ms, max. seen latency %u ms\n",
269                        jiffies_to_msecs(jiffies_spent),
270                        jiffies_to_msecs(max_jiffies));
271         }
272         if (!is_write)
273                 dma_cache_maint(addr, len, DMA_FROM_DEVICE);
274
275         nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) & ~((1 << 16) | (1 << 17)));
276 }
277
278 static void fifo_read(u32 *out, unsigned int len)
279 {
280         const int block_size = 16;
281         unsigned long status_reg, fifo_reg;
282         int c;
283
284         status_reg = omap_nand_base + NND_STATUS;
285         fifo_reg = omap_nand_base + NND_FIFO;
286         len = len * 4 / block_size;
287         nand_write_reg(NND_FIFOCTRL, (block_size << 24) | len);
288         nand_write_reg(NND_STATUS, 0x0f);
289         nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) | (1 << 17));
290         c = block_size / 4;
291         while (len--) {
292                 int i;
293
294                 while ((__raw_readl(status_reg) & (1 << 2)) == 0);
295                 __raw_writel(0x0f, status_reg);
296                 for (i = 0; i < c; i++) {
297                         u32 l = __raw_readl(fifo_reg);
298                         *out++ = l;
299                 }
300         }
301         nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) & ~(1 << 17));
302         nand_write_reg(NND_STATUS, 0x0f);
303 }
304
305 static void omap_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
306 {
307         unsigned long access_reg;
308
309         if (likely(((unsigned long) buf & 3) == 0 && (len & 3) == 0)) {
310                 int u32_count = len >> 2;
311                 u32 *dest = (u32 *) buf;
312                 /* If the transfer is big enough and the length divisible by
313                  * 16, we try to use DMA transfer, or FIFO copy in case of
314                  * DMA failure (e.g. all channels busy) */
315                 if (u32_count > 64 && (u32_count & 3) == 0) {
316                         if (omap_nand_dma_ch >= 0) {
317                                 omap_nand_dma_transfer(mtd, buf, u32_count, 0);
318                                 return;
319                         }
320                         /* In case of an error, fallback to FIFO copy */
321                         fifo_read((u32 *) buf, u32_count);
322                         return;
323                 }
324                 access_reg = omap_nand_base + NND_ACCESS;
325                 /* Small buffers we just read directly */
326                 while (u32_count--)
327                         *dest++ = __raw_readl(access_reg);
328         } else {
329                 /* If we're not word-aligned, we use byte copy */
330                 access_reg = omap_nand_base + NND_ACCESS;
331                 while (len--)
332                         *buf++ = __raw_readb(access_reg);
333         }
334 }
335
336 static void omap_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
337 {
338         if (likely(((unsigned long) buf & 3) == 0 && (len & 3) == 0)) {
339                 const u32 *src = (const u32 *) buf;
340
341                 len >>= 2;
342 #if 0
343                 /* If the transfer is big enough and length divisible by 16,
344                  * we try to use DMA transfer. */
345                 if (len > 256 / 4 && (len & 3) == 0) {
346                         if (omap_nand_dma_transfer(mtd, (void *) buf, len, 1) == 0)
347                                 return;
348                         /* In case of an error, fallback to CPU copy */
349                 }
350 #endif
351                 while (len--)
352                         nand_write_reg(NND_ACCESS, *src++);
353         } else {
354                 while (len--)
355                         nand_write_reg8(NND_ACCESS, *buf++);
356         }
357 }
358
359 static int omap_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
360 {
361         if (likely(((unsigned long) buf & 3) == 0 && (len & 3) == 0)) {
362                 const u32 *dest = (const u32 *) buf;
363                 len >>= 2;
364                 while (len--)
365                         if (*dest++ != nand_read_reg(NND_ACCESS))
366                                 return -EFAULT;
367         } else {
368                 while (len--)
369                         if (*buf++ != nand_read_reg8(NND_ACCESS))
370                                 return -EFAULT;
371         }
372         return 0;
373 }
374
375 static u_char omap_nand_read_byte(struct mtd_info *mtd)
376 {
377         return nand_read_reg8(NND_ACCESS);
378 }
379
380 static int omap_nand_dev_ready(struct mtd_info *mtd)
381 {
382         u32 l;
383
384         l = nand_read_reg(NND_READY);
385         return l & 0x01;
386 }
387
388 static int nand_write_command(u8 cmd, u32 addr, int addr_valid)
389 {
390         if (addr_valid) {
391                 nand_write_reg(NND_ADDR_SRC, addr);
392                 nand_write_reg8(NND_COMMAND, cmd);
393         } else {
394                 nand_write_reg(NND_ADDR_SRC, 0);
395                 nand_write_reg8(NND_COMMAND_SEC, cmd);
396         }
397         while (!omap_nand_dev_ready(NULL));
398         return 0;
399 }
400
401 /*
402  * Send command to NAND device
403  */
404 static void omap_nand_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
405 {
406         struct nand_chip *this = mtd->priv;
407
408         /*
409          * Write out the command to the device.
410          */
411         if (command == NAND_CMD_SEQIN) {
412                 int readcmd;
413
414                 if (column >= mtd->writesize) {
415                         /* OOB area */
416                         column -= mtd->writesize;
417                         readcmd = NAND_CMD_READOOB;
418                 } else if (column < 256) {
419                         /* First 256 bytes --> READ0 */
420                         readcmd = NAND_CMD_READ0;
421                 } else {
422                         column -= 256;
423                         readcmd = NAND_CMD_READ1;
424                 }
425                 nand_write_command(readcmd, 0, 0);
426         }
427         switch (command) {
428         case NAND_CMD_RESET:
429         case NAND_CMD_PAGEPROG:
430         case NAND_CMD_STATUS:
431         case NAND_CMD_ERASE2:
432                 nand_write_command(command, 0, 0);
433                 break;
434         case NAND_CMD_ERASE1:
435                 nand_write_command(command, ((page_addr & 0xFFFFFF00) << 1) | (page_addr & 0XFF), 1);
436                 break;
437         default:
438                 nand_write_command(command, (page_addr << this->page_shift) | column, 1);
439         }
440 }
441
442 static void omap_nand_command_lp(struct mtd_info *mtd, unsigned command, int column, int page_addr)
443 {
444         struct nand_chip *this = mtd->priv;
445
446         if (command == NAND_CMD_READOOB) {
447                 column += mtd->writesize;
448                 command = NAND_CMD_READ0;
449         }
450         switch (command) {
451         case NAND_CMD_RESET:
452         case NAND_CMD_PAGEPROG:
453         case NAND_CMD_STATUS:
454         case NAND_CMD_ERASE2:           
455                 nand_write_command(command, 0, 0);
456                 break;
457         case NAND_CMD_ERASE1:
458                 nand_write_command(command, page_addr << this->page_shift >> 11, 1);
459                 break;
460         default:
461                 nand_write_command(command, (page_addr << 16) | column, 1);
462         }
463         if (command == NAND_CMD_READ0)
464                 nand_write_command(NAND_CMD_READSTART, 0, 0);
465 }
466
467 /*
468  * Generate non-inverted ECC bytes.
469  *
470  * Using noninverted ECC can be considered ugly since writing a blank
471  * page ie. padding will clear the ECC bytes. This is no problem as long
472  * nobody is trying to write data on the seemingly unused page.
473  *
474  * Reading an erased page will produce an ECC mismatch between
475  * generated and read ECC bytes that has to be dealt with separately.
476  */
477 static int omap_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
478 {
479         u32 l;
480         int reg;
481         int n;
482         struct nand_chip *this = mtd->priv;
483
484         /* Ex NAND_ECC_HW12_2048 */
485         if ((this->ecc.mode == NAND_ECC_HW) && (this->ecc.size  == 2048))
486                 n = 4;
487         else
488                 n = 1;
489         reg = NND_ECC_START;
490         while (n--) {
491                 l = nand_read_reg(reg);
492                 *ecc_code++ = l;          // P128e, ..., P1e
493                 *ecc_code++ = l >> 16;    // P128o, ..., P1o
494                 // P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e
495                 *ecc_code++ = ((l >> 8) & 0x0f) | ((l >> 20) & 0xf0);
496                 reg += 4;
497         }
498         return 0;
499 }
500
501 /*
502  * This function will generate true ECC value, which can be used
503  * when correcting data read from NAND flash memory core
504  */
505 static void gen_true_ecc(u8 *ecc_buf)
506 {
507         u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
508
509         ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) | P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp) );
510         ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) | P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
511         ecc_buf[2] = ~( P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) | P1e(tmp) | P2048o(tmp) | P2048e(tmp));
512 }
513
514 /*
515  * This function compares two ECC's and indicates if there is an error.
516  * If the error can be corrected it will be corrected to the buffer
517  */
518 static int omap_nand_compare_ecc(u8 *ecc_data1,   /* read from NAND memory */
519                                  u8 *ecc_data2,   /* read from register */
520                                  u8 *page_data)
521 {
522         uint   i;
523         u8     tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
524         u8     comp0_bit[8], comp1_bit[8], comp2_bit[8];
525         u8     ecc_bit[24];
526         u8     ecc_sum = 0;
527         u8     find_bit = 0;
528         uint   find_byte = 0;
529         int    isEccFF;
530
531         isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
532
533         gen_true_ecc(ecc_data1);
534         gen_true_ecc(ecc_data2);
535
536         for (i = 0; i <= 2; i++) {
537                 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
538                 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
539         }
540
541         for (i = 0; i < 8; i++) {
542                 tmp0_bit[i]      = *ecc_data1 % 2;
543                 *ecc_data1       = *ecc_data1 / 2;
544         }
545
546         for (i = 0; i < 8; i++) {
547                 tmp1_bit[i]      = *(ecc_data1 + 1) % 2;
548                 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
549         }
550
551         for (i = 0; i < 8; i++) {
552                 tmp2_bit[i]      = *(ecc_data1 + 2) % 2;
553                 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
554         }
555
556         for (i = 0; i < 8; i++) {
557                 comp0_bit[i]     = *ecc_data2 % 2;
558                 *ecc_data2       = *ecc_data2 / 2;
559         }
560
561         for (i = 0; i < 8; i++) {
562                 comp1_bit[i]     = *(ecc_data2 + 1) % 2;
563                 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
564         }
565
566         for (i = 0; i < 8; i++) {
567                 comp2_bit[i]     = *(ecc_data2 + 2) % 2;
568                 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
569         }
570
571         for (i = 0; i< 6; i++ )
572                 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
573
574         for (i = 0; i < 8; i++)
575                 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
576
577         for (i = 0; i < 8; i++)
578                 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
579
580         ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
581         ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
582
583         for (i = 0; i < 24; i++)
584                 ecc_sum += ecc_bit[i];
585
586         switch (ecc_sum) {
587         case 0:
588                 /* Not reached because this function is not called if
589                    ECC values are equal */
590                 return 0;
591
592         case 1:
593                 /* Uncorrectable error */
594                 DEBUG (MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
595                 return -1;
596
597         case 12:
598                 /* Correctable error */
599                 find_byte = (ecc_bit[23] << 8) + 
600                             (ecc_bit[21] << 7) + 
601                             (ecc_bit[19] << 6) +
602                             (ecc_bit[17] << 5) +
603                             (ecc_bit[15] << 4) +
604                             (ecc_bit[13] << 3) +
605                             (ecc_bit[11] << 2) +
606                             (ecc_bit[9]  << 1) +
607                             ecc_bit[7];
608
609                 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
610
611                 DEBUG (MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at offset: %d, bit: %d\n", find_byte, find_bit);
612
613                 page_data[find_byte] ^= (1 << find_bit);
614
615                 return 0;
616         default:
617                 if (isEccFF) {
618                         if (ecc_data2[0] == 0 && ecc_data2[1] == 0 && ecc_data2[2] == 0)
619                                 return 0;
620                 } 
621                 DEBUG (MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
622                 return -1;
623         }
624 }
625
626 static int omap_nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
627 {
628         struct nand_chip *this;
629         int block_count = 0, i, r;
630
631         this = mtd->priv;
632         /* Ex NAND_ECC_HW12_2048 */
633         if ((this->ecc.mode == NAND_ECC_HW) && (this->ecc.size  == 2048))
634                 block_count = 4;
635         else
636                 block_count = 1;
637         for (i = 0; i < block_count; i++) {
638                 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
639                         r = omap_nand_compare_ecc(read_ecc, calc_ecc, dat);
640                         if (r < 0)
641                                 return r;
642                 }
643                 read_ecc += 3;
644                 calc_ecc += 3;
645                 dat += 512;
646         }
647         return 0;
648 }
649
650 static void omap_nand_enable_hwecc(struct mtd_info *mtd, int mode)
651 {
652         nand_write_reg(NND_RESET, 0x01);
653 }
654
655 #ifdef CONFIG_MTD_CMDLINE_PARTS
656
657 extern int mtdpart_setup(char *);
658
659 static int __init add_dynamic_parts(struct mtd_info *mtd)
660 {
661         static const char *part_parsers[] = { "cmdlinepart", NULL };
662         struct mtd_partition *parts;
663         const struct omap_flash_part_str_config *cfg;
664         char *part_str = NULL;
665         size_t part_str_len;
666         int c;
667
668         cfg = omap_get_var_config(OMAP_TAG_FLASH_PART_STR, &part_str_len);
669         if (cfg != NULL) {
670                 part_str = kmalloc(part_str_len + 1, GFP_KERNEL);
671                 if (part_str == NULL)
672                         return -ENOMEM;
673                 memcpy(part_str, cfg->part_table, part_str_len);
674                 part_str[part_str_len] = '\0';
675                 mtdpart_setup(part_str);
676         }
677         c = parse_mtd_partitions(omap_mtd, part_parsers, &parts, 0);
678         if (part_str != NULL) {
679                 mtdpart_setup(NULL);
680                 kfree(part_str);
681         }
682         if (c <= 0)
683                 return -1;
684
685         add_mtd_partitions(mtd, parts, c);
686
687         return 0;
688 }
689
690 #else
691
692 static inline int add_dynamic_parts(struct mtd_info *mtd)
693 {
694         return -1;
695 }
696
697 #endif
698
699 static inline int calc_psc(int ns, int cycle_ps)
700 {
701         return (ns * 1000 + (cycle_ps - 1)) / cycle_ps;
702 }
703
704 static void set_psc_regs(int psc_ns, int psc1_ns, int psc2_ns)
705 {
706         int psc[3], i;
707         unsigned long rate, ps;
708
709         rate = clk_get_rate(omap_nand_clk);
710         ps = 1000000000 / (rate / 1000);
711         psc[0] = calc_psc(psc_ns, ps);
712         psc[1] = calc_psc(psc1_ns, ps);
713         psc[2] = calc_psc(psc2_ns, ps);
714         for (i = 0; i < 3; i++) {
715                 if (psc[i] < 2)
716                         psc[i] = 2;
717                 else if (psc[i] > 256)
718                         psc[i] = 256;
719         }
720         nand_write_reg(NND_PSC_CLK, psc[0] - 1);
721         nand_write_reg(NND_PSC1_CLK, psc[1] - 1);
722         nand_write_reg(NND_PSC2_CLK, psc[2] - 1);
723         printk(KERN_INFO "omap-hw-nand: using PSC values %d, %d, %d\n", psc[0], psc[1], psc[2]);
724 }
725
726 /*
727  * Main initialization routine
728  */
729 static int __init omap_nand_init(void)
730 {
731         struct nand_chip *this;
732         int err = 0;
733         u32 l;
734
735         omap_nand_clk = clk_get(NULL, "armper_ck");
736         BUG_ON(omap_nand_clk == NULL);
737         clk_enable(omap_nand_clk);
738
739         l = nand_read_reg(NND_REVISION);        
740         printk(KERN_INFO "omap-hw-nand: OMAP NAND Controller rev. %d.%d\n", l>>4, l & 0xf);
741
742         /* Reset the NAND Controller */
743         nand_write_reg(NND_SYSCFG, 0x02);
744         while ((nand_read_reg(NND_SYSSTATUS) & 0x01) == 0);
745
746         /* No Prefetch, no postwrite, write prot & enable pairs disabled,
747            addres counter set to send 4 byte addresses to flash,
748            A8 is set not to be sent to flash (erase addre needs formatting),
749            choose little endian, enable 512 byte ECC logic,        
750          */
751         nand_write_reg(NND_CTRL, 0xFF01);
752
753         /* Allocate memory for MTD device structure and private data */
754         omap_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
755         if (!omap_mtd) {
756                 printk(KERN_WARNING "omap-hw-nand: Unable to allocate OMAP NAND MTD device structure.\n");
757                 err = -ENOMEM;
758                 goto free_clock;
759         }
760 #if 1
761         err = omap_request_dma(OMAP_DMA_NAND, "NAND", nand_dma_cb,
762                                &omap_nand_dma_comp, &omap_nand_dma_ch);
763         if (err < 0) {
764                 printk(KERN_WARNING "omap-hw-nand: Unable to reserve DMA channel\n");
765                 omap_nand_dma_ch = -1;
766         }
767 #else
768         omap_nand_dma_ch = -1;
769 #endif
770         /* Get pointer to private data */
771         this = (struct nand_chip *) (&omap_mtd[1]);
772
773         /* Initialize structures */
774         memset((char *) omap_mtd, 0, sizeof(struct mtd_info));
775         memset((char *) this, 0, sizeof(struct nand_chip));
776
777         /* Link the private data with the MTD structure */
778         omap_mtd->priv = this;
779         omap_mtd->name = "omap-nand";
780
781         this->options = NAND_SKIP_BBTSCAN;
782
783         /* Used from chip select and nand_command() */
784         this->read_byte = omap_nand_read_byte;
785
786         this->select_chip   = omap_nand_select_chip;
787         this->dev_ready     = omap_nand_dev_ready;
788         this->chip_delay    = 0;
789         this->ecc.mode      = NAND_ECC_HW;
790         this->ecc.bytes     = 3;
791         this->ecc.size      = 512;
792         this->cmdfunc       = omap_nand_command;
793         this->write_buf     = omap_nand_write_buf;
794         this->read_buf      = omap_nand_read_buf;
795         this->verify_buf    = omap_nand_verify_buf;
796         this->ecc.calculate = omap_nand_calculate_ecc;
797         this->ecc.correct   = omap_nand_correct_data;
798         this->ecc.hwctl     = omap_nand_enable_hwecc;
799
800         nand_write_reg(NND_SYSCFG, 0x1); /* Enable auto idle */
801         nand_write_reg(NND_PSC_CLK, 10);
802         /* Scan to find existance of the device */
803         if (nand_scan(omap_mtd, 1)) {
804                 err = -ENXIO;
805                 goto out_mtd;
806         }
807
808         set_psc_regs(25, 15, 35);
809         if (this->page_shift == 11) {
810                 this->cmdfunc = omap_nand_command_lp;
811                 l = nand_read_reg(NND_CTRL);
812                 l |= 1 << 4; /* Set the A8 bit in CTRL reg */
813                 nand_write_reg(NND_CTRL, l);
814                 this->ecc.mode = NAND_ECC_HW;
815                 this->ecc.steps = 1;
816                 this->ecc.size = 2048;
817                 this->ecc.bytes = 12;
818                 nand_write_reg(NND_ECC_SELECT, 6);
819         }
820
821         /* We have to do bbt scanning ourselves */
822         if (this->scan_bbt (omap_mtd)) {
823                 err = -ENXIO;
824                 goto out_mtd;
825         }
826
827         err = add_dynamic_parts(omap_mtd);
828         if (err < 0) {
829                 printk(KERN_ERR "omap-hw-nand: no partitions defined\n");
830                 err = -ENODEV;
831                 nand_release(omap_mtd);
832                 goto out_mtd;
833         }
834         /* init completed */
835         return 0;
836 out_mtd:
837         if (omap_nand_dma_ch >= 0)
838                 omap_free_dma(omap_nand_dma_ch);
839         kfree(omap_mtd);
840 free_clock:
841         clk_put(omap_nand_clk);
842         return err;
843 }
844
845 module_init(omap_nand_init);
846
847 /*
848  * Clean up routine
849  */
850 static void __exit omap_nand_cleanup (void)
851 {
852         clk_disable(omap_nand_clk);
853         clk_put(omap_nand_clk);
854         nand_release(omap_mtd);
855         kfree(omap_mtd);
856 }
857
858 module_exit(omap_nand_cleanup);
859