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