]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mtd/nand/omap2.c
OMAP2EVM: twl4030 keypad Kconfig dependency fix
[linux-2.6-omap-h63xx.git] / drivers / mtd / nand / omap2.c
1 /*
2  * drivers/mtd/nand/omap2.c
3  *
4  * Copyright (c) 2004 Texas Instruments, Jian Zhang <jzhang@ti.com>
5  * Copyright (c) 2004 Micron Technology Inc.
6  * Copyright (c) 2004 David Brownell
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/platform_device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/delay.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/nand.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/io.h>
20
21 #include <asm/dma.h>
22
23 #include <asm/arch/gpmc.h>
24 #include <asm/arch/nand.h>
25
26 #define GPMC_IRQ_STATUS         0x18
27 #define GPMC_ECC_CONFIG         0x1F4
28 #define GPMC_ECC_CONTROL        0x1F8
29 #define GPMC_ECC_SIZE_CONFIG    0x1FC
30 #define GPMC_ECC1_RESULT        0x200
31
32 #define DRIVER_NAME     "omap2-nand"
33 #define NAND_IO_SIZE    SZ_4K
34
35 #define NAND_WP_ON      1
36 #define NAND_WP_OFF     0
37 #define NAND_WP_BIT     0x00000010
38 #define WR_RD_PIN_MONITORING    0x00600000
39
40 #define GPMC_BUF_FULL   0x00000001
41 #define GPMC_BUF_EMPTY  0x00000000
42
43 #define NAND_Ecc_P1e            (1 << 0)
44 #define NAND_Ecc_P2e            (1 << 1)
45 #define NAND_Ecc_P4e            (1 << 2)
46 #define NAND_Ecc_P8e            (1 << 3)
47 #define NAND_Ecc_P16e           (1 << 4)
48 #define NAND_Ecc_P32e           (1 << 5)
49 #define NAND_Ecc_P64e           (1 << 6)
50 #define NAND_Ecc_P128e          (1 << 7)
51 #define NAND_Ecc_P256e          (1 << 8)
52 #define NAND_Ecc_P512e          (1 << 9)
53 #define NAND_Ecc_P1024e         (1 << 10)
54 #define NAND_Ecc_P2048e         (1 << 11)
55
56 #define NAND_Ecc_P1o            (1 << 16)
57 #define NAND_Ecc_P2o            (1 << 17)
58 #define NAND_Ecc_P4o            (1 << 18)
59 #define NAND_Ecc_P8o            (1 << 19)
60 #define NAND_Ecc_P16o           (1 << 20)
61 #define NAND_Ecc_P32o           (1 << 21)
62 #define NAND_Ecc_P64o           (1 << 22)
63 #define NAND_Ecc_P128o          (1 << 23)
64 #define NAND_Ecc_P256o          (1 << 24)
65 #define NAND_Ecc_P512o          (1 << 25)
66 #define NAND_Ecc_P1024o         (1 << 26)
67 #define NAND_Ecc_P2048o         (1 << 27)
68
69 #define TF(value)       (value ? 1 : 0)
70
71 #define P2048e(a)       (TF(a & NAND_Ecc_P2048e)        << 0)
72 #define P2048o(a)       (TF(a & NAND_Ecc_P2048o)        << 1)
73 #define P1e(a)          (TF(a & NAND_Ecc_P1e)           << 2)
74 #define P1o(a)          (TF(a & NAND_Ecc_P1o)           << 3)
75 #define P2e(a)          (TF(a & NAND_Ecc_P2e)           << 4)
76 #define P2o(a)          (TF(a & NAND_Ecc_P2o)           << 5)
77 #define P4e(a)          (TF(a & NAND_Ecc_P4e)           << 6)
78 #define P4o(a)          (TF(a & NAND_Ecc_P4o)           << 7)
79
80 #define P8e(a)          (TF(a & NAND_Ecc_P8e)           << 0)
81 #define P8o(a)          (TF(a & NAND_Ecc_P8o)           << 1)
82 #define P16e(a)         (TF(a & NAND_Ecc_P16e)          << 2)
83 #define P16o(a)         (TF(a & NAND_Ecc_P16o)          << 3)
84 #define P32e(a)         (TF(a & NAND_Ecc_P32e)          << 4)
85 #define P32o(a)         (TF(a & NAND_Ecc_P32o)          << 5)
86 #define P64e(a)         (TF(a & NAND_Ecc_P64e)          << 6)
87 #define P64o(a)         (TF(a & NAND_Ecc_P64o)          << 7)
88
89 #define P128e(a)        (TF(a & NAND_Ecc_P128e)         << 0)
90 #define P128o(a)        (TF(a & NAND_Ecc_P128o)         << 1)
91 #define P256e(a)        (TF(a & NAND_Ecc_P256e)         << 2)
92 #define P256o(a)        (TF(a & NAND_Ecc_P256o)         << 3)
93 #define P512e(a)        (TF(a & NAND_Ecc_P512e)         << 4)
94 #define P512o(a)        (TF(a & NAND_Ecc_P512o)         << 5)
95 #define P1024e(a)       (TF(a & NAND_Ecc_P1024e)        << 6)
96 #define P1024o(a)       (TF(a & NAND_Ecc_P1024o)        << 7)
97
98 #define P8e_s(a)        (TF(a & NAND_Ecc_P8e)           << 0)
99 #define P8o_s(a)        (TF(a & NAND_Ecc_P8o)           << 1)
100 #define P16e_s(a)       (TF(a & NAND_Ecc_P16e)          << 2)
101 #define P16o_s(a)       (TF(a & NAND_Ecc_P16o)          << 3)
102 #define P1e_s(a)        (TF(a & NAND_Ecc_P1e)           << 4)
103 #define P1o_s(a)        (TF(a & NAND_Ecc_P1o)           << 5)
104 #define P2e_s(a)        (TF(a & NAND_Ecc_P2e)           << 6)
105 #define P2o_s(a)        (TF(a & NAND_Ecc_P2o)           << 7)
106
107 #define P4e_s(a)        (TF(a & NAND_Ecc_P4e)           << 0)
108 #define P4o_s(a)        (TF(a & NAND_Ecc_P4o)           << 1)
109
110 #ifdef CONFIG_MTD_PARTITIONS
111 static const char *part_probes[] = { "cmdlinepart", NULL };
112 #endif
113
114 struct omap_nand_info {
115         struct nand_hw_control          controller;
116         struct omap_nand_platform_data  *pdata;
117         struct mtd_info                 mtd;
118         struct mtd_partition            *parts;
119         struct nand_chip                nand;
120         struct platform_device          *pdev;
121
122         int                             gpmc_cs;
123         unsigned long                   phys_base;
124         void __iomem                    *gpmc_cs_baseaddr;
125         void __iomem                    *gpmc_baseaddr;
126 };
127
128 /*
129  * omap_nand_wp - This function enable or disable the Write Protect feature on
130  * NAND device
131  * @mtd: MTD device structure
132  * @mode: WP ON/OFF
133  */
134 static void omap_nand_wp(struct mtd_info *mtd, int mode)
135 {
136         struct omap_nand_info *info = container_of(mtd,
137                                                 struct omap_nand_info, mtd);
138
139         unsigned long config = __raw_readl(info->gpmc_baseaddr + GPMC_CONFIG);
140
141         if (mode)
142                 config &= ~(NAND_WP_BIT);       /* WP is ON */
143         else
144                 config |= (NAND_WP_BIT);        /* WP is OFF */
145
146         __raw_writel(config, (info->gpmc_baseaddr + GPMC_CONFIG));
147 }
148
149 /*
150  * hardware specific access to control-lines
151  * NOTE: boards may use different bits for these!!
152  *
153  * ctrl:
154  * NAND_NCE: bit 0 - don't care
155  * NAND_CLE: bit 1 -> Command Latch
156  * NAND_ALE: bit 2 -> Address Latch
157  */
158 static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
159 {
160         struct omap_nand_info *info = container_of(mtd,
161                                         struct omap_nand_info, mtd);
162         switch (ctrl) {
163         case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
164                 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
165                                                 GPMC_CS_NAND_COMMAND;
166                 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
167                                                 GPMC_CS_NAND_DATA;
168                 break;
169
170         case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
171                 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
172                                                 GPMC_CS_NAND_ADDRESS;
173                 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
174                                                 GPMC_CS_NAND_DATA;
175                 break;
176
177         case NAND_CTRL_CHANGE | NAND_NCE:
178                 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
179                                                 GPMC_CS_NAND_DATA;
180                 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
181                                                 GPMC_CS_NAND_DATA;
182                 break;
183         }
184
185         if (cmd != NAND_CMD_NONE)
186                 __raw_writeb(cmd, info->nand.IO_ADDR_W);
187 }
188
189 /*
190  * omap_read_buf - read data from NAND controller into buffer
191  * @mtd: MTD device structure
192  * @buf: buffer to store date
193  * @len: number of bytes to read
194  */
195 static void omap_read_buf(struct mtd_info *mtd, u_char *buf, int len)
196 {
197         struct omap_nand_info *info = container_of(mtd,
198                                         struct omap_nand_info, mtd);
199         u16 *p = (u16 *) buf;
200
201         len >>= 1;
202
203         while (len--)
204                 *p++ = cpu_to_le16(readw(info->nand.IO_ADDR_R));
205 }
206
207 /*
208  * omap_write_buf - write buffer to NAND controller
209  * @mtd: MTD device structure
210  * @buf: data buffer
211  * @len: number of bytes to write
212  */
213 static void omap_write_buf(struct mtd_info *mtd, const u_char * buf, int len)
214 {
215         struct omap_nand_info *info = container_of(mtd,
216                                                 struct omap_nand_info, mtd);
217         u16 *p = (u16 *) buf;
218
219         len >>= 1;
220
221         while (len--) {
222                 writew(cpu_to_le16(*p++), info->nand.IO_ADDR_W);
223
224                 while (GPMC_BUF_EMPTY == (readl(info->gpmc_baseaddr +
225                                                 GPMC_STATUS) & GPMC_BUF_FULL));
226         }
227 }
228 /*
229  * omap_verify_buf - Verify chip data against buffer
230  * @mtd: MTD device structure
231  * @buf: buffer containing the data to compare
232  * @len: number of bytes to compare
233  */
234 static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
235 {
236         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
237                                                         mtd);
238         u16 *p = (u16 *) buf;
239
240         len >>= 1;
241
242         while (len--) {
243
244                 if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
245                         return -EFAULT;
246         }
247
248         return 0;
249 }
250
251 #ifdef CONFIG_MTD_NAND_OMAP_HWECC
252 /*
253  * omap_hwecc_init-Initialize the Hardware ECC for NAND flash in GPMC controller
254  * @mtd: MTD device structure
255  */
256 static void omap_hwecc_init(struct mtd_info *mtd)
257 {
258         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
259                                                         mtd);
260         register struct nand_chip *chip = mtd->priv;
261         unsigned long val = 0x0;
262
263         /* Read from ECC Control Register */
264         val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_CONTROL);
265         /* Clear all ECC | Enable Reg1 */
266         val = ((0x00000001<<8) | 0x00000001);
267         __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
268
269         /* Read from ECC Size Config Register */
270         val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_SIZE_CONFIG);
271         /* ECCSIZE1=512 | Select eccResultsize[0-3] */
272         val = ((((chip->ecc.size >> 1) - 1) << 22) | (0x0000000F));
273         __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_SIZE_CONFIG);
274 }
275
276 /*
277  * gen_true_ecc - This function will generate true ECC value, which can be used
278  * when correcting data read from NAND flash memory core
279  * @ecc_buf: buffer to store ecc code
280  */
281 static void gen_true_ecc(u8 *ecc_buf)
282 {
283         u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
284                 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
285
286         ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
287                         P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
288         ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
289                         P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
290         ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
291                         P1e(tmp) | P2048o(tmp) | P2048e(tmp));
292 }
293
294 /*
295  * omap_compare_ecc - This function compares two ECC's and indicates if there
296  * is an error. If the error can be corrected it will be corrected to the
297  * buffer
298  * @ecc_data1:  ecc code from nand spare area
299  * @ecc_data2:  ecc code from hardware register obtained from hardware ecc
300  * @page_data:  page data
301  */
302 static int omap_compare_ecc(u8 *ecc_data1,      /* read from NAND memory */
303                             u8 *ecc_data2,      /* read from register */
304                             u8 *page_data)
305 {
306         uint    i;
307         u8      tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
308         u8      comp0_bit[8], comp1_bit[8], comp2_bit[8];
309         u8      ecc_bit[24];
310         u8      ecc_sum = 0;
311         u8      find_bit = 0;
312         uint    find_byte = 0;
313         int     isEccFF;
314
315         isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
316
317         gen_true_ecc(ecc_data1);
318         gen_true_ecc(ecc_data2);
319
320         for (i = 0; i <= 2; i++) {
321                 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
322                 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
323         }
324
325         for (i = 0; i < 8; i++) {
326                 tmp0_bit[i]     = *ecc_data1 % 2;
327                 *ecc_data1      = *ecc_data1 / 2;
328         }
329
330         for (i = 0; i < 8; i++) {
331                 tmp1_bit[i]      = *(ecc_data1 + 1) % 2;
332                 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
333         }
334
335         for (i = 0; i < 8; i++) {
336                 tmp2_bit[i]      = *(ecc_data1 + 2) % 2;
337                 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
338         }
339
340         for (i = 0; i < 8; i++) {
341                 comp0_bit[i]     = *ecc_data2 % 2;
342                 *ecc_data2       = *ecc_data2 / 2;
343         }
344
345         for (i = 0; i < 8; i++) {
346                 comp1_bit[i]     = *(ecc_data2 + 1) % 2;
347                 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
348         }
349
350         for (i = 0; i < 8; i++) {
351                 comp2_bit[i]     = *(ecc_data2 + 2) % 2;
352                 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
353         }
354
355         for (i = 0; i < 6; i++)
356                 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
357
358         for (i = 0; i < 8; i++)
359                 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
360
361         for (i = 0; i < 8; i++)
362                 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
363
364         ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
365         ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
366
367         for (i = 0; i < 24; i++)
368                 ecc_sum += ecc_bit[i];
369
370         switch (ecc_sum) {
371         case 0:
372                 /* Not reached because this function is not called if
373                  *  ECC values are equal
374                  */
375                 return 0;
376
377         case 1:
378                 /* Uncorrectable error */
379                 DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
380                 return -1;
381
382         case 11:
383                 /* UN-Correctable error */
384                 DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR B\n");
385                 return -1;
386
387         case 12:
388                 /* Correctable error */
389                 find_byte = (ecc_bit[23] << 8) +
390                             (ecc_bit[21] << 7) +
391                             (ecc_bit[19] << 6) +
392                             (ecc_bit[17] << 5) +
393                             (ecc_bit[15] << 4) +
394                             (ecc_bit[13] << 3) +
395                             (ecc_bit[11] << 2) +
396                             (ecc_bit[9]  << 1) +
397                             ecc_bit[7];
398
399                 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
400
401                 DEBUG(MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at "
402                                 "offset: %d, bit: %d\n", find_byte, find_bit);
403
404                 page_data[find_byte] ^= (1 << find_bit);
405
406                 return 0;
407         default:
408                 if (isEccFF) {
409                         if (ecc_data2[0] == 0 &&
410                             ecc_data2[1] == 0 &&
411                             ecc_data2[2] == 0)
412                                 return 0;
413                 }
414                 DEBUG(MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
415                 return -1;
416         }
417 }
418
419 /*
420  * omap_correct_data - Compares the ecc read from nand spare area with ECC
421  * registers values and corrects one bit error if it has occured
422  * @mtd: MTD device structure
423  * @dat: page data
424  * @read_ecc: ecc read from nand flash
425  * @calc_ecc: ecc read from ECC registers
426  */
427 static int omap_correct_data(struct mtd_info *mtd, u_char * dat,
428                                 u_char * read_ecc, u_char * calc_ecc)
429 {
430         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
431                                                         mtd);
432         int blockCnt = 0, i = 0, ret = 0;
433
434         /* Ex NAND_ECC_HW12_2048 */
435         if ((info->nand.ecc.mode == NAND_ECC_HW) &&
436                         (info->nand.ecc.size  == 2048))
437                 blockCnt = 4;
438         else
439                 blockCnt = 1;
440
441         for (i = 0; i < blockCnt; i++) {
442                 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
443                         ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
444                         if (ret < 0) return ret;
445                 }
446                 read_ecc += 3;
447                 calc_ecc += 3;
448                 dat      += 512;
449         }
450         return 0;
451 }
452
453 /*
454  * omap_calcuate_ecc - Generate non-inverted ECC bytes.
455  * Using noninverted ECC can be considered ugly since writing a blank
456  * page ie. padding will clear the ECC bytes. This is no problem as long
457  * nobody is trying to write data on the seemingly unused page. Reading
458  * an erased page will produce an ECC mismatch between generated and read
459  * ECC bytes that has to be dealt with separately.
460  * @mtd: MTD device structure
461  * @dat: The pointer to data on which ecc is computed
462  * @ecc_code: The ecc_code buffer
463  */
464 static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
465                                 u_char *ecc_code)
466 {
467         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
468                                                         mtd);
469         unsigned long val = 0x0;
470         unsigned long reg;
471
472         /* Start Reading from HW ECC1_Result = 0x200 */
473         reg = (unsigned long)(info->gpmc_baseaddr + GPMC_ECC1_RESULT);
474         val = __raw_readl(reg);
475         *ecc_code++ = val;          /* P128e, ..., P1e */
476         *ecc_code++ = val >> 16;    /* P128o, ..., P1o */
477         /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
478         *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
479         reg += 4;
480
481         return 0;
482 }
483
484 /*
485  * omap_enable_hwecc - This function enables the hardware ecc functionality
486  * @mtd: MTD device structure
487  * @mode: Read/Write mode
488  */
489 static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
490 {
491         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
492                                                         mtd);
493         register struct nand_chip *chip = mtd->priv;
494         unsigned int dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
495         unsigned long val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_CONFIG);
496
497         switch (mode) {
498         case NAND_ECC_READ    :
499                 __raw_writel(0x101, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
500                 /* (ECC 16 or 8 bit col) | ( CS  )  | ECC Enable */
501                 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
502                 break;
503         case NAND_ECC_READSYN :
504                  __raw_writel(0x100, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
505                 /* (ECC 16 or 8 bit col) | ( CS  )  | ECC Enable */
506                 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
507                 break;
508         case NAND_ECC_WRITE   :
509                 __raw_writel(0x101, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
510                 /* (ECC 16 or 8 bit col) | ( CS  )  | ECC Enable */
511                 val = (dev_width << 7) | (info->gpmc_cs << 1) | (0x1);
512                 break;
513         default:
514                 DEBUG(MTD_DEBUG_LEVEL0, "Error: Unrecognized Mode[%d]!\n",
515                                         mode);
516                 break;
517         }
518
519         __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_CONFIG);
520 }
521 #endif
522
523 /*
524  * omap_wait - Wait function is called during Program and erase
525  * operations and the way it is called from MTD layer, we should wait
526  * till the NAND chip is ready after the programming/erase operation
527  * has completed.
528  * @mtd: MTD device structure
529  * @chip: NAND Chip structure
530  */
531 static int omap_wait(struct mtd_info *mtd, struct nand_chip *chip)
532 {
533         register struct nand_chip *this = mtd->priv;
534         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
535                                                         mtd);
536         int status = 0;
537
538         this->IO_ADDR_W = (void *) info->gpmc_cs_baseaddr +
539                                                 GPMC_CS_NAND_COMMAND;
540         this->IO_ADDR_R = (void *) info->gpmc_cs_baseaddr + GPMC_CS_NAND_DATA;
541
542         while (!(status & 0x40)) {
543                  __raw_writeb(NAND_CMD_STATUS & 0xFF, this->IO_ADDR_W);
544                 status = __raw_readb(this->IO_ADDR_R);
545         }
546         return status;
547 }
548
549 /*
550  * omap_dev_ready - calls the platform specific dev_ready function
551  * @mtd: MTD device structure
552  */
553 static int omap_dev_ready(struct mtd_info *mtd)
554 {
555         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
556                                                         mtd);
557         unsigned int val = __raw_readl(info->gpmc_baseaddr + GPMC_IRQ_STATUS);
558
559         if ((val & 0x100) == 0x100) {
560                 /* Clear IRQ Interrupt */
561                 val |= 0x100;
562                 val &= ~(0x0);
563                 __raw_writel(val, info->gpmc_baseaddr + GPMC_IRQ_STATUS);
564         } else {
565                 unsigned int cnt = 0;
566                 while (cnt++ < 0x1FF) {
567                         if  ((val & 0x100) == 0x100)
568                                 return 0;
569                         val = __raw_readl(info->gpmc_baseaddr +
570                                                         GPMC_IRQ_STATUS);
571                 }
572         }
573
574         return 1;
575 }
576
577 static int __devinit omap_nand_probe(struct platform_device *pdev)
578 {
579         struct omap_nand_info           *info;
580         struct omap_nand_platform_data  *pdata;
581         int                             err;
582         unsigned long                   val;
583
584
585         pdata = pdev->dev.platform_data;
586         if (pdata == NULL) {
587                 dev_err(&pdev->dev, "platform data missing\n");
588                 return -ENODEV;
589         }
590
591         info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
592         if (!info) return -ENOMEM;
593
594         platform_set_drvdata(pdev, info);
595
596         spin_lock_init(&info->controller.lock);
597         init_waitqueue_head(&info->controller.wq);
598
599         info->pdev = pdev;
600
601         info->gpmc_cs           = pdata->cs;
602         info->gpmc_baseaddr     = pdata->gpmc_baseaddr;
603         info->gpmc_cs_baseaddr  = pdata->gpmc_cs_baseaddr;
604
605         info->mtd.priv          = &info->nand;
606         info->mtd.name          = pdev->dev.bus_id;
607         info->mtd.owner         = THIS_MODULE;
608
609         err = gpmc_cs_request(info->gpmc_cs, NAND_IO_SIZE, &info->phys_base);
610         if (err < 0) {
611                 dev_err(&pdev->dev, "Cannot request GPMC CS\n");
612                 goto out_free_info;
613         }
614
615         /* Enable RD PIN Monitoring Reg */
616         if (pdata->dev_ready) {
617                 val  = gpmc_cs_read_reg(info->gpmc_cs, GPMC_CS_CONFIG1);
618                 val |= WR_RD_PIN_MONITORING;
619                 gpmc_cs_write_reg(info->gpmc_cs, GPMC_CS_CONFIG1, val);
620         }
621
622         val  = gpmc_cs_read_reg(info->gpmc_cs, GPMC_CS_CONFIG7);
623         val &= ~(0xf << 8);
624         val |=  (0xc & 0xf) << 8;
625         gpmc_cs_write_reg(info->gpmc_cs, GPMC_CS_CONFIG7, val);
626
627         /* NAND write protect off */
628         omap_nand_wp(&info->mtd, NAND_WP_OFF);
629
630         if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
631                                 pdev->dev.driver->name)) {
632                 err = -EBUSY;
633                 goto out_free_cs;
634         }
635
636         info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
637         if (!info->nand.IO_ADDR_R) {
638                 err = -ENOMEM;
639                 goto out_release_mem_region;
640         }
641         info->nand.controller = &info->controller;
642
643         info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
644         info->nand.cmd_ctrl  = omap_hwcontrol;
645
646         info->nand.read_buf   = omap_read_buf;
647         info->nand.write_buf  = omap_write_buf;
648         info->nand.verify_buf = omap_verify_buf;
649
650         /*
651         * If RDY/BSY line is connected to OMAP then use the omap ready funcrtion
652         * and the generic nand_wait function which reads the status register
653         * after monitoring the RDY/BSY line.Otherwise use a standard chip delay
654         * which is slightly more than tR (AC Timing) of the NAND device and read
655         * status register until you get a failure or success
656         */
657         if (pdata->dev_ready) {
658                 info->nand.dev_ready = omap_dev_ready;
659                 info->nand.chip_delay = 0;
660         } else {
661                 info->nand.waitfunc = omap_wait;
662                 info->nand.chip_delay = 50;
663         }
664
665         info->nand.options  |= NAND_SKIP_BBTSCAN;
666         if ((gpmc_cs_read_reg(info->gpmc_cs, GPMC_CS_CONFIG1) & 0x3000)
667                                                                 == 0x1000)
668                 info->nand.options  |= NAND_BUSWIDTH_16;
669
670 #ifdef CONFIG_MTD_NAND_OMAP_HWECC
671         info->nand.ecc.bytes            = 3;
672         info->nand.ecc.size             = 512;
673         info->nand.ecc.calculate        = omap_calculate_ecc;
674         info->nand.ecc.hwctl            = omap_enable_hwecc;
675         info->nand.ecc.correct          = omap_correct_data;
676         info->nand.ecc.mode             = NAND_ECC_HW;
677
678         /* init HW ECC */
679         omap_hwecc_init(&info->mtd);
680 #else
681         info->nand.ecc.mode = NAND_ECC_SOFT;
682 #endif
683
684         /* DIP switches on some boards change between 8 and 16 bit
685          * bus widths for flash.  Try the other width if the first try fails.
686          */
687         if (nand_scan(&info->mtd, 1)) {
688                 info->nand.options ^= NAND_BUSWIDTH_16;
689                 if (nand_scan(&info->mtd, 1)) {
690                         err = -ENXIO;
691                         goto out_release_mem_region;
692                 }
693         }
694
695 #ifdef CONFIG_MTD_PARTITIONS
696         err = parse_mtd_partitions(&info->mtd, part_probes, &info->parts, 0);
697         if (err > 0)
698                 add_mtd_partitions(&info->mtd, info->parts, err);
699         else if (pdata->parts)
700                 add_mtd_partitions(&info->mtd, pdata->parts, pdata->nr_parts);
701         else
702 #endif
703                 add_mtd_device(&info->mtd);
704
705         platform_set_drvdata(pdev, &info->mtd);
706
707         return 0;
708
709 out_release_mem_region:
710         release_mem_region(info->phys_base, NAND_IO_SIZE);
711 out_free_cs:
712         gpmc_cs_free(info->gpmc_cs);
713 out_free_info:
714         kfree(info);
715
716         return err;
717 }
718
719 static int omap_nand_remove(struct platform_device *pdev)
720 {
721         struct mtd_info *mtd = platform_get_drvdata(pdev);
722         struct omap_nand_info *info = mtd->priv;
723
724         platform_set_drvdata(pdev, NULL);
725         /* Release NAND device, its internal structures and partitions */
726         nand_release(&info->mtd);
727         iounmap(info->nand.IO_ADDR_R);
728         kfree(&info->mtd);
729         return 0;
730 }
731
732 static struct platform_driver omap_nand_driver = {
733         .probe          = omap_nand_probe,
734         .remove         = omap_nand_remove,
735         .driver         = {
736                 .name   = DRIVER_NAME,
737                 .owner  = THIS_MODULE,
738         },
739 };
740 MODULE_ALIAS(DRIVER_NAME);
741
742 static int __init omap_nand_init(void)
743 {
744         printk(KERN_INFO "%s driver initializing\n", DRIVER_NAME);
745         return platform_driver_register(&omap_nand_driver);
746 }
747
748 static void __exit omap_nand_exit(void)
749 {
750         platform_driver_unregister(&omap_nand_driver);
751 }
752
753 module_init(omap_nand_init);
754 module_exit(omap_nand_exit);
755
756 MODULE_LICENSE("GPL");
757 MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");