]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mtd/nand/omap-hw.c
MTD: OMAP: Add various OMAP drivers
[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                 omap_set_dma_src_params(dma_ch, OMAP_DMA_PORT_EMIFF,
237                                         OMAP_DMA_AMODE_POST_INC,
238                                         virt_to_phys(addr));
239 //              omap_set_dma_src_burst_mode(dma_ch, OMAP_DMA_DATA_BURST_4);
240                 /* Set POSTWRITE bit */
241                 nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) | (1 << 16));
242         } else {
243                 omap_set_dma_src_params(dma_ch, OMAP_DMA_PORT_TIPB,
244                                         OMAP_DMA_AMODE_CONSTANT, fifo_reg);
245                 omap_set_dma_dest_params(dma_ch, OMAP_DMA_PORT_EMIFF,
246                                          OMAP_DMA_AMODE_POST_INC,
247                                          virt_to_phys(addr));
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         init_completion(&comp);
255
256         len = u32_count << 2;
257         consistent_sync(addr, len, DMA_TO_DEVICE);
258         omap_start_dma(dma_ch);
259         wait_for_completion(&comp);
260         omap_free_dma(dma_ch);
261         if (!is_write)
262                 consistent_sync(addr, len, DMA_FROM_DEVICE);
263
264         nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) & ~((1 << 16) | (1 << 17)));
265         return 0;
266 }
267
268 static void fifo_read(u32 *out, unsigned int len)
269 {
270         const int block_size = 16;
271         unsigned long status_reg, fifo_reg;
272         int c;
273
274         status_reg = omap_nand_base + NND_STATUS;
275         fifo_reg = omap_nand_base + NND_FIFO;
276         len = len * 4 / block_size;
277         nand_write_reg(NND_FIFOCTRL, (block_size << 24) | len);
278         nand_write_reg(NND_STATUS, 0x0f);
279         nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) | (1 << 17));
280         c = block_size / 4;
281         while (len--) {
282                 int i;
283
284                 while ((__raw_readl(status_reg) & (1 << 2)) == 0);
285                 __raw_writel(0x0f, status_reg);
286                 for (i = 0; i < c; i++) {
287                         u32 l = __raw_readl(fifo_reg);
288                         *out++ = l;
289                 }
290         }
291         nand_write_reg(NND_CTRL, nand_read_reg(NND_CTRL) & ~(1 << 17));
292         nand_write_reg(NND_STATUS, 0x0f);
293 }
294
295 static void omap_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
296 {
297         unsigned long access_reg;
298
299         if (likely(((unsigned long) buf & 3) == 0 && (len & 3) == 0)) {
300                 int u32_count = len >> 2;
301                 u32 *dest = (u32 *) buf;
302                 /* If the transfer is big enough and the length divisible by
303                  * 16, we try to use DMA transfer, or FIFO copy in case of
304                  * DMA failure (e.g. all channels busy) */
305                 if (u32_count > 64 && (u32_count & 3) == 0) {
306 #if 1
307                         if (omap_nand_dma_transfer(mtd, buf, u32_count, 0) == 0)
308                                 return;
309 #endif
310                         /* In case of an error, fallback to FIFO copy */
311                         fifo_read((u32 *) buf, u32_count);
312                         return;
313                 }
314                 access_reg = omap_nand_base + NND_ACCESS;
315                 /* Small buffers we just read directly */
316                 while (u32_count--)
317                         *dest++ = __raw_readl(access_reg);
318         } else {
319                 /* If we're not word-aligned, we use byte copy */
320                 access_reg = omap_nand_base + NND_ACCESS;
321                 while (len--)
322                         *buf++ = __raw_readb(access_reg);
323         }
324 }
325
326 static void omap_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
327 {
328         if (likely(((unsigned long) buf & 3) == 0 && (len & 3) == 0)) {
329                 const u32 *src = (const u32 *) buf;
330
331                 len >>= 2;
332 #if 0
333                 /* If the transfer is big enough and length divisible by 16,
334                  * we try to use DMA transfer. */
335                 if (len > 256 / 4 && (len & 3) == 0) {
336                         if (omap_nand_dma_transfer(mtd, (void *) buf, len, 1) == 0)
337                                 return;
338                         /* In case of an error, fallback to CPU copy */
339                 }
340 #endif
341                 while (len--)
342                         nand_write_reg(NND_ACCESS, *src++);
343         } else {
344                 while (len--)
345                         nand_write_reg8(NND_ACCESS, *buf++);
346         }
347 }
348
349 static int omap_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
350 {
351         if (likely(((unsigned long) buf & 3) == 0 && (len & 3) == 0)) {
352                 const u32 *dest = (const u32 *) buf;
353                 len >>= 2;
354                 while (len--)
355                         if (*dest++ != nand_read_reg(NND_ACCESS))
356                                 return -EFAULT;
357         } else {
358                 while (len--)
359                         if (*buf++ != nand_read_reg8(NND_ACCESS))
360                                 return -EFAULT;
361         }
362         return 0;
363 }
364
365 static u_char omap_nand_read_byte(struct mtd_info *mtd)
366 {
367         return nand_read_reg8(NND_ACCESS);
368 }
369
370 static void omap_nand_write_byte(struct mtd_info *mtd, u_char byte)
371 {
372         nand_write_reg8(NND_ACCESS, byte);
373 }
374
375 static int omap_nand_dev_ready(struct mtd_info *mtd)
376 {
377         u32 l;
378
379         l = nand_read_reg(NND_READY);
380         return l & 0x01;
381 }
382
383 static int nand_write_command(u8 cmd, u32 addr, int addr_valid)
384 {
385         if (addr_valid) {
386                 nand_write_reg(NND_ADDR_SRC, addr);
387                 nand_write_reg8(NND_COMMAND, cmd);
388         } else {
389                 nand_write_reg(NND_ADDR_SRC, 0);
390                 nand_write_reg8(NND_COMMAND_SEC, cmd);
391         }
392         while (!omap_nand_dev_ready(NULL));
393         return 0;
394 }
395
396 /*
397  * Send command to NAND device
398  */
399 static void omap_nand_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
400 {
401         struct nand_chip *this = mtd->priv;
402
403         /*
404          * Write out the command to the device.
405          */
406         if (command == NAND_CMD_SEQIN) {
407                 int readcmd;
408
409                 if (column >= mtd->oobblock) {
410                         /* OOB area */
411                         column -= mtd->oobblock;
412                         readcmd = NAND_CMD_READOOB;
413                 } else if (column < 256) {
414                         /* First 256 bytes --> READ0 */
415                         readcmd = NAND_CMD_READ0;
416                 } else {
417                         column -= 256;
418                         readcmd = NAND_CMD_READ1;
419                 }
420                 nand_write_command(readcmd, 0, 0);
421         }
422
423         switch (command) {
424         case NAND_CMD_RESET:
425         case NAND_CMD_PAGEPROG:
426         case NAND_CMD_STATUS:
427         case NAND_CMD_ERASE2:
428                 nand_write_command(command, 0, 0);
429                 break;
430
431         case NAND_CMD_ERASE1:
432                 nand_write_command(command, ((page_addr & 0xFFFFFF00) << 1) | (page_addr & 0XFF), 1);
433                 break;
434
435         default:
436                 nand_write_command(command, (page_addr << this->page_shift) | column, 1);
437         }
438 }
439
440 static void omap_nand_command_lp(struct mtd_info *mtd, unsigned command, int column, int page_addr)
441 {
442         struct nand_chip *this = mtd->priv;
443
444         if (command == NAND_CMD_READOOB) {
445                 column += mtd->oobblock;
446                 command = NAND_CMD_READ0;
447         }
448         switch (command) {
449         case NAND_CMD_RESET:
450         case NAND_CMD_PAGEPROG:
451         case NAND_CMD_STATUS:
452         case NAND_CMD_ERASE2:           
453                 nand_write_command(command, 0, 0);
454                 break;
455         case NAND_CMD_ERASE1:
456                 nand_write_command(command, page_addr << this->page_shift >> 11, 1);
457                 break;
458         default:
459                 nand_write_command(command, (page_addr << 16) | column, 1);
460         }
461         if (command == NAND_CMD_READ0)
462                 nand_write_command(NAND_CMD_READSTART, 0, 0);
463 }
464
465 /*
466  * Generate non-inverted ECC bytes.
467  *
468  * Using noninverted ECC can be considered ugly since writing a blank
469  * page ie. padding will clear the ECC bytes. This is no problem as long
470  * nobody is trying to write data on the seemingly unused page.
471  *
472  * Reading an erased page will produce an ECC mismatch between
473  * generated and read ECC bytes that has to be dealt with separately.
474  */
475 static int omap_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
476 {
477         u32 l;
478         int reg;
479         int n;
480         struct nand_chip *this = mtd->priv;
481
482         if (this->eccmode == NAND_ECC_HW12_2048)
483                 n = 4;
484         else
485                 n = 1;
486         reg = NND_ECC_START;
487         while (n--) {
488                 l = nand_read_reg(reg);
489                 *ecc_code++ = l;          // P128e, ..., P1e
490                 *ecc_code++ = l >> 16;    // P128o, ..., P1o
491                 // P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e
492                 *ecc_code++ = ((l >> 8) & 0x0f) | ((l >> 20) & 0xf0);
493                 reg += 4;
494         }
495         return 0;
496 }
497
498 /*
499  * This function will generate true ECC value, which can be used
500  * when correcting data read from NAND flash memory core
501  */
502 static void gen_true_ecc(u8 *ecc_buf)
503 {
504         u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
505
506         ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) | P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp) );
507         ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) | P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
508         ecc_buf[2] = ~( P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) | P1e(tmp) | P2048o(tmp) | P2048e(tmp));
509 }
510
511 /*
512  * This function compares two ECC's and indicates if there is an error.
513  * If the error can be corrected it will be corrected to the buffer
514  */
515 static int omap_nand_compare_ecc(u8 *ecc_data1,   /* read from NAND memory */
516                                  u8 *ecc_data2,   /* read from register */
517                                  u8 *page_data)
518 {
519         uint   i;
520         u8     tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
521         u8     comp0_bit[8], comp1_bit[8], comp2_bit[8];
522         u8     ecc_bit[24];
523         u8     ecc_sum = 0;
524         u8     find_bit = 0;
525         uint   find_byte = 0;
526         int    isEccFF;
527
528         isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
529
530         gen_true_ecc(ecc_data1);
531         gen_true_ecc(ecc_data2);
532
533         for (i = 0; i <= 2; i++) {
534                 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
535                 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
536         }
537
538         for (i = 0; i < 8; i++) {
539                 tmp0_bit[i]      = *ecc_data1 % 2;
540                 *ecc_data1       = *ecc_data1 / 2;
541         }
542
543         for (i = 0; i < 8; i++) {
544                 tmp1_bit[i]      = *(ecc_data1 + 1) % 2;
545                 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
546         }
547
548         for (i = 0; i < 8; i++) {
549                 tmp2_bit[i]      = *(ecc_data1 + 2) % 2;
550                 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
551         }
552
553         for (i = 0; i < 8; i++) {
554                 comp0_bit[i]     = *ecc_data2 % 2;
555                 *ecc_data2       = *ecc_data2 / 2;
556         }
557
558         for (i = 0; i < 8; i++) {
559                 comp1_bit[i]     = *(ecc_data2 + 1) % 2;
560                 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
561         }
562
563         for (i = 0; i < 8; i++) {
564                 comp2_bit[i]     = *(ecc_data2 + 2) % 2;
565                 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
566         }
567
568         for (i = 0; i< 6; i++ )
569                 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
570
571         for (i = 0; i < 8; i++)
572                 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
573
574         for (i = 0; i < 8; i++)
575                 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
576
577         ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
578         ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
579
580         for (i = 0; i < 24; i++)
581                 ecc_sum += ecc_bit[i];
582
583         switch (ecc_sum) {
584         case 0:
585                 /* Not reached because this function is not called if
586                    ECC values are equal */
587                 return 0;
588
589         case 1:
590                 /* Uncorrectable error */
591                 DEBUG (MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
592                 return -1;
593
594         case 12:
595                 /* Correctable error */
596                 find_byte = (ecc_bit[23] << 8) + 
597                             (ecc_bit[21] << 7) + 
598                             (ecc_bit[19] << 6) +
599                             (ecc_bit[17] << 5) +
600                             (ecc_bit[15] << 4) +
601                             (ecc_bit[13] << 3) +
602                             (ecc_bit[11] << 2) +
603                             (ecc_bit[9]  << 1) +
604                             ecc_bit[7];
605
606                 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
607
608                 DEBUG (MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at offset: %d, bit: %d\n", find_byte, find_bit);
609
610                 page_data[find_byte] ^= (1 << find_bit);
611
612                 return 0;
613         default:
614                 if (isEccFF) {
615                         if (ecc_data2[0] == 0 && ecc_data2[1] == 0 && ecc_data2[2] == 0)
616                                 return 0;
617                 } 
618                 DEBUG (MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
619                 return -1;
620         }
621 }
622
623 static int omap_nand_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
624 {
625         struct nand_chip *this;
626         int block_count = 0, i, r;
627
628         this = mtd->priv;
629         if (this->eccmode == NAND_ECC_HW12_2048)
630                 block_count = 4;
631         else
632                 block_count = 1;
633         for (i = 0; i < block_count; i++) {
634                 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
635                         r = omap_nand_compare_ecc(read_ecc, calc_ecc, dat);
636                         if (r < 0)
637                                 return r;
638                 }
639                 read_ecc += 3;
640                 calc_ecc += 3;
641                 dat += 512;
642         }
643         return 0;
644 }
645
646 static void omap_nand_enable_hwecc(struct mtd_info *mtd, int mode)
647 {
648         nand_write_reg(NND_RESET, 0x01);
649 }
650
651 static int omap_nand_scan_bbt(struct mtd_info *mtd)
652 {
653         return 0;
654 }
655
656 #ifdef CONFIG_MTD_CMDLINE_PARTS
657
658 extern int mtdpart_setup(char *);
659
660 static int __init add_dynamic_parts(struct mtd_info *mtd)
661 {
662         static const char *part_parsers[] = { "cmdlinepart", NULL };
663         struct mtd_partition *parts;
664         const struct omap_flash_part_config *cfg;
665         char *part_str = NULL;
666         size_t part_str_len;
667         int c;
668
669         cfg = omap_get_var_config(OMAP_TAG_FLASH_PART, &part_str_len);
670         if (cfg != NULL) {
671                 part_str = kmalloc(part_str_len + 1, GFP_KERNEL);
672                 if (part_str == NULL)
673                         return -ENOMEM;
674                 memcpy(part_str, cfg->part_table, part_str_len);
675                 part_str[part_str_len] = '\0';
676                 mtdpart_setup(part_str);
677         }
678         c = parse_mtd_partitions(omap_mtd, part_parsers, &parts, 0);
679         if (part_str != NULL) {
680                 mtdpart_setup(NULL);
681                 kfree(part_str);
682         }
683         if (c <= 0)
684                 return -1;
685
686         add_mtd_partitions(mtd, parts, c);
687
688         return 0;
689 }
690
691 #else
692
693 static inline int add_dynamic_parts(struct mtd_info *mtd)
694 {
695         return -1;
696 }
697
698 #endif
699
700 static inline int calc_psc(int ns, int cycle_ps)
701 {
702         return (ns * 1000 + (cycle_ps - 1)) / cycle_ps;
703 }
704
705 static void set_psc_regs(int psc_ns, int psc1_ns, int psc2_ns)
706 {
707         int psc[3], i;
708         unsigned long rate, ps;
709
710         rate = clk_get_rate(omap_nand_clk);
711         ps = 1000000000 / (rate / 1000);
712         psc[0] = calc_psc(psc_ns, ps);
713         psc[1] = calc_psc(psc1_ns, ps);
714         psc[2] = calc_psc(psc2_ns, ps);
715         for (i = 0; i < 3; i++) {
716                 if (psc[i] == 0)
717                         psc[i] = 1;
718                 else if (psc[i] > 256)
719                         psc[i] = 256;
720         }
721         nand_write_reg(NND_PSC_CLK, psc[0] - 1);
722         nand_write_reg(NND_PSC1_CLK, psc[1] - 1);
723         nand_write_reg(NND_PSC2_CLK, psc[2] - 1);
724         printk(KERN_INFO "omap-hw-nand: using PSC values %d, %d, %d\n", psc[0], psc[1], psc[2]);
725 }
726
727 /*
728  * Main initialization routine
729  */
730 static int __init omap_nand_init(void)
731 {
732         struct nand_chip *this;
733         int err = 0;
734         u32 l;
735
736         omap_nand_clk = clk_get(NULL, "armper_ck");
737         BUG_ON(omap_nand_clk == NULL);
738         clk_use(omap_nand_clk);
739
740         l = nand_read_reg(NND_REVISION);        
741         printk(KERN_INFO "omap-hw-nand: OMAP NAND Controller rev. %d.%d\n", l>>4, l & 0xf);
742
743         /* Reset the NAND Controller */
744         nand_write_reg(NND_SYSCFG, 0x02);
745         while ((nand_read_reg(NND_SYSSTATUS) & 0x01) == 0);
746
747         /* No Prefetch, no postwrite, write prot & enable pairs disabled,
748            addres counter set to send 4 byte addresses to flash,
749            A8 is set not to be sent to flash (erase addre needs formatting),
750            choose little endian, enable 512 byte ECC logic,        
751          */
752         nand_write_reg(NND_CTRL, 0xFF01);
753
754         /* Allocate memory for MTD device structure and private data */
755         omap_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
756         if (!omap_mtd) {
757                 printk(KERN_WARNING "omap-hw-nand: Unable to allocate OMAP NAND MTD device structure.\n");
758                 err = -ENOMEM;
759                 goto free_clock;
760         }
761
762         /* Get pointer to private data */
763         this = (struct nand_chip *) (&omap_mtd[1]);
764
765         /* Initialize structures */
766         memset((char *) omap_mtd, 0, sizeof(struct mtd_info));
767         memset((char *) this, 0, sizeof(struct nand_chip));
768
769         /* Link the private data with the MTD structure */
770         omap_mtd->priv = this;
771         omap_mtd->name = "omap-nand";
772
773         /* Used from chip select and nand_command() */
774         this->read_byte = omap_nand_read_byte;
775         this->write_byte = omap_nand_write_byte;
776
777         this->select_chip = omap_nand_select_chip;
778         this->dev_ready = omap_nand_dev_ready;
779         this->chip_delay = 0;
780         this->eccmode = NAND_ECC_HW3_512; 
781         this->cmdfunc = omap_nand_command;
782         this->write_buf = omap_nand_write_buf;
783         this->read_buf = omap_nand_read_buf;
784         this->verify_buf = omap_nand_verify_buf;
785         this->calculate_ecc = omap_nand_calculate_ecc;
786         this->correct_data = omap_nand_correct_data;
787         this->enable_hwecc = omap_nand_enable_hwecc;
788         this->scan_bbt = omap_nand_scan_bbt;
789
790         nand_write_reg(NND_PSC_CLK, 10);
791         /* Scan to find existance of the device */
792         if (nand_scan(omap_mtd, 1)) {
793                 err = -ENXIO;
794                 goto out_mtd;
795         }
796
797         set_psc_regs(25, 15, 35);
798         if (this->page_shift == 11) {
799                 this->cmdfunc = omap_nand_command_lp;
800                 l = nand_read_reg(NND_CTRL);
801                 l |= 1 << 4; /* Set the A8 bit in CTRL reg */
802                 nand_write_reg(NND_CTRL, l);
803                 this->eccmode = NAND_ECC_HW12_2048;
804                 this->eccsteps = 1;
805                 this->eccsize = 2048;
806                 this->eccbytes = 12;
807                 omap_mtd->eccsize = 2048;
808                 nand_write_reg(NND_ECC_SELECT, 6);
809         }
810
811         this->options |= NAND_NO_AUTOINCR;
812
813         err = add_dynamic_parts(omap_mtd);
814         if (err < 0) {
815                 printk(KERN_ERR "omap-hw-nand: no partitions defined\n");
816                 err = -ENODEV;
817                 nand_release(omap_mtd);
818                 goto out_mtd;
819         }
820         /* init completed */
821         return 0;
822 out_mtd:
823         kfree(omap_mtd);
824 free_clock:
825         clk_put(omap_nand_clk);
826         return err;
827 }
828
829 module_init(omap_nand_init);
830
831 /*
832  * Clean up routine
833  */
834 static void __exit omap_nand_cleanup (void)
835 {
836         clk_unuse(omap_nand_clk);
837         clk_put(omap_nand_clk);
838         nand_release(omap_mtd);
839         kfree(omap_mtd);
840 }
841
842 module_exit(omap_nand_cleanup);
843