]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mtd/nand/omap2.c
MTD: NAND: Add omap support for 2K page nand
[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 static int hw_ecc = 1;
115
116 /* new oob placement block for use with hardware ecc generation */
117 static struct nand_ecclayout omap_hw_eccoob = {
118         .eccbytes = 12,
119         .eccpos = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
120         .oobfree = {{16, 32}, {33, 63} },
121 };
122
123 struct omap_nand_info {
124         struct nand_hw_control          controller;
125         struct nand_platform_data       *pdata;
126         struct mtd_info                 mtd;
127         struct mtd_partition            *parts;
128         struct nand_chip                nand;
129         struct platform_device          *pdev;
130
131         int                             gpmc_cs;
132         unsigned long                   phys_base;
133         void __iomem                    *gpmc_cs_baseaddr;
134         void __iomem                    *gpmc_baseaddr;
135 };
136 static void omap_nand_wp(struct mtd_info *mtd, int mode)
137 {
138         struct omap_nand_info *info = container_of(mtd,
139                                                 struct omap_nand_info, mtd);
140
141         unsigned long config = __raw_readl(info->gpmc_baseaddr + GPMC_CONFIG);
142
143         if (mode)
144                 config &= ~(NAND_WP_BIT);       /* WP is ON */
145         else
146                 config |= (NAND_WP_BIT);        /* WP is OFF */
147
148         __raw_writel(config, (info->gpmc_baseaddr + GPMC_CONFIG));
149 }
150
151 /*
152  * hardware specific access to control-lines
153  * NOTE: boards may use different bits for these!!
154  *
155  * ctrl:
156  * NAND_NCE: bit 0 - don't care
157  * NAND_CLE: bit 1 -> Command Latch
158  * NAND_ALE: bit 2 -> Address Latch
159  */
160 static void omap_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
161 {
162         struct omap_nand_info *info = container_of(mtd,
163                                         struct omap_nand_info, mtd);
164         switch (ctrl) {
165         case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
166                 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
167                                                 GPMC_CS_NAND_COMMAND;
168                 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
169                                                 GPMC_CS_NAND_DATA;
170                 break;
171
172         case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
173                 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
174                                                 GPMC_CS_NAND_ADDRESS;
175                 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
176                                                 GPMC_CS_NAND_DATA;
177                 break;
178
179         case NAND_CTRL_CHANGE | NAND_NCE:
180                 info->nand.IO_ADDR_W = info->gpmc_cs_baseaddr +
181                                                 GPMC_CS_NAND_DATA;
182                 info->nand.IO_ADDR_R = info->gpmc_cs_baseaddr +
183                                                 GPMC_CS_NAND_DATA;
184                 break;
185         }
186
187         if (cmd != NAND_CMD_NONE)
188                 __raw_writeb(cmd, info->nand.IO_ADDR_W);
189 }
190
191 /*
192 * omap_read_buf - read data from NAND controller into buffer
193 * @mtd: MTD device structure
194 * @buf: buffer to store date
195 * @len: number of bytes to read
196 */
197 static void omap_read_buf(struct mtd_info *mtd, u_char *buf, int len)
198 {
199         struct omap_nand_info *info = container_of(mtd,
200                                         struct omap_nand_info, mtd);
201         u16 *p = (u16 *) buf;
202
203         len >>= 1;
204
205         while (len--)
206                 *p++ = cpu_to_le16(readw(info->nand.IO_ADDR_R));
207 }
208
209 /*
210 * omap_write_buf - write buffer to NAND controller
211 * @mtd: MTD device structure
212 * @buf: data buffer
213 * @len: number of bytes to write
214 */
215 static void omap_write_buf(struct mtd_info *mtd, const u_char * buf, int len)
216 {
217         struct omap_nand_info *info = container_of(mtd,
218                                                 struct omap_nand_info, mtd);
219         u16 *p = (u16 *) buf;
220
221         len >>= 1;
222
223         while (len--) {
224                 writew(cpu_to_le16(*p++), info->nand.IO_ADDR_W);
225
226                 while (GPMC_BUF_EMPTY == (readl(info->gpmc_baseaddr +
227                                                 GPMC_STATUS) & GPMC_BUF_FULL));
228         }
229 }
230 /*
231  * omap_verify_buf - Verify chip data against buffer
232  * @mtd: MTD device structure
233  * @buf: buffer containing the data to compare
234  * @len: number of bytes to compare
235  */
236 static int omap_verify_buf(struct mtd_info *mtd, const u_char * buf, int len)
237 {
238         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
239                                                         mtd);
240         u16 *p = (u16 *) buf;
241
242         len >>= 1;
243
244         while (len--) {
245
246                 if (*p++ != cpu_to_le16(readw(info->nand.IO_ADDR_R)))
247                         return -EFAULT;
248         }
249
250         return 0;
251 }
252
253 static void omap_hwecc_init(struct mtd_info *mtd)
254 {
255         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
256                                                         mtd);
257         unsigned long val = 0x0;
258
259         /* Read from ECC Control Register */
260         val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_CONTROL);
261         /* Clear all ECC | Enable Reg1 */
262         val = ((0x00000001<<8) | 0x00000001);
263         __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
264
265         /* Read from ECC Size Config Register */
266         val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_SIZE_CONFIG);
267         /* ECCSIZE1=512 | ECCSIZE0=8bytes | Select eccResultsize[0123] */
268         val = ((0x000000FF<<22) | (0x00000003<<12) | (0x0000000F));
269         __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_SIZE_CONFIG);
270
271
272 }
273
274 /*
275  * This function will generate true ECC value, which can be used
276  * when correcting data read from NAND flash memory core
277  */
278 static void gen_true_ecc(u8 *ecc_buf)
279 {
280         u32 tmp = ecc_buf[0] | (ecc_buf[1] << 16) |
281                 ((ecc_buf[2] & 0xF0) << 20) | ((ecc_buf[2] & 0x0F) << 8);
282
283         ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) |
284                         P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
285         ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) |
286                         P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
287         ecc_buf[2] = ~(P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) |
288                         P1e(tmp) | P2048o(tmp) | P2048e(tmp));
289 }
290
291 /*
292  * This function compares two ECC's and indicates if there is an error.
293  * If the error can be corrected it will be corrected to the buffer
294  */
295 static int omap_compare_ecc(u8 *ecc_data1,      /* read from NAND memory */
296                             u8 *ecc_data2,      /* read from register */
297                             u8 *page_data)
298 {
299         uint    i;
300         u8      tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
301         u8      comp0_bit[8], comp1_bit[8], comp2_bit[8];
302         u8      ecc_bit[24];
303         u8      ecc_sum = 0;
304         u8      find_bit = 0;
305         uint    find_byte = 0;
306         int     isEccFF;
307
308         isEccFF = ((*(u32 *)ecc_data1 & 0xFFFFFF) == 0xFFFFFF);
309
310         gen_true_ecc(ecc_data1);
311         gen_true_ecc(ecc_data2);
312
313         for (i = 0; i <= 2; i++) {
314                 *(ecc_data1 + i) = ~(*(ecc_data1 + i));
315                 *(ecc_data2 + i) = ~(*(ecc_data2 + i));
316         }
317
318         for (i = 0; i < 8; i++) {
319                 tmp0_bit[i]     = *ecc_data1 % 2;
320                 *ecc_data1      = *ecc_data1 / 2;
321         }
322
323         for (i = 0; i < 8; i++) {
324                 tmp1_bit[i]      = *(ecc_data1 + 1) % 2;
325                 *(ecc_data1 + 1) = *(ecc_data1 + 1) / 2;
326         }
327
328         for (i = 0; i < 8; i++) {
329                 tmp2_bit[i]      = *(ecc_data1 + 2) % 2;
330                 *(ecc_data1 + 2) = *(ecc_data1 + 2) / 2;
331         }
332
333         for (i = 0; i < 8; i++) {
334                 comp0_bit[i]     = *ecc_data2 % 2;
335                 *ecc_data2       = *ecc_data2 / 2;
336         }
337
338         for (i = 0; i < 8; i++) {
339                 comp1_bit[i]     = *(ecc_data2 + 1) % 2;
340                 *(ecc_data2 + 1) = *(ecc_data2 + 1) / 2;
341         }
342
343         for (i = 0; i < 8; i++) {
344                 comp2_bit[i]     = *(ecc_data2 + 2) % 2;
345                 *(ecc_data2 + 2) = *(ecc_data2 + 2) / 2;
346         }
347
348         for (i = 0; i < 6; i++)
349                 ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
350
351         for (i = 0; i < 8; i++)
352                 ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
353
354         for (i = 0; i < 8; i++)
355                 ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
356
357         ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
358         ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
359
360         for (i = 0; i < 24; i++)
361                 ecc_sum += ecc_bit[i];
362
363         switch (ecc_sum) {
364         case 0:
365                 /* Not reached because this function is not called if
366                  *  ECC values are equal
367                  */
368                 return 0;
369
370         case 1:
371                 /* Uncorrectable error */
372                 DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
373                 return -1;
374
375         case 11:
376                 /* UN-Correctable error */
377                 DEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR B\n");
378                 return -1;
379
380         case 12:
381                 /* Correctable error */
382                 find_byte = (ecc_bit[23] << 8) +
383                             (ecc_bit[21] << 7) +
384                             (ecc_bit[19] << 6) +
385                             (ecc_bit[17] << 5) +
386                             (ecc_bit[15] << 4) +
387                             (ecc_bit[13] << 3) +
388                             (ecc_bit[11] << 2) +
389                             (ecc_bit[9]  << 1) +
390                             ecc_bit[7];
391
392                 find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
393
394                 DEBUG(MTD_DEBUG_LEVEL0, "Correcting single bit ECC error at "
395                                 "offset: %d, bit: %d\n", find_byte, find_bit);
396
397                 page_data[find_byte] ^= (1 << find_bit);
398
399                 return 0;
400         default:
401                 if (isEccFF) {
402                         if (ecc_data2[0] == 0 &&
403                             ecc_data2[1] == 0 &&
404                             ecc_data2[2] == 0)
405                                 return 0;
406                 }
407                 DEBUG(MTD_DEBUG_LEVEL0, "UNCORRECTED_ERROR default\n");
408                 return -1;
409         }
410 }
411
412 static int omap_correct_data(struct mtd_info *mtd, u_char * dat,
413                                 u_char * read_ecc, u_char * calc_ecc)
414 {
415         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
416                                                         mtd);
417         int blockCnt = 0, i = 0, ret = 0;
418
419         /* Ex NAND_ECC_HW12_2048 */
420         if ((info->nand.ecc.mode == NAND_ECC_HW) &&
421                         (info->nand.ecc.size  == 2048))
422                 blockCnt = 4;
423         else
424                 blockCnt = 1;
425
426         for (i = 0; i < blockCnt; i++) {
427                 if (memcmp(read_ecc, calc_ecc, 3) != 0) {
428                         ret = omap_compare_ecc(read_ecc, calc_ecc, dat);
429                         if (ret < 0) return ret;
430                 }
431                 read_ecc += 3;
432                 calc_ecc += 3;
433                 dat      += 512;
434         }
435         return 0;
436 }
437
438 /*
439 ** Generate non-inverted ECC bytes.
440 **
441 ** Using noninverted ECC can be considered ugly since writing a blank
442 ** page ie. padding will clear the ECC bytes. This is no problem as long
443 ** nobody is trying to write data on the seemingly unused page.
444 **
445 ** Reading an erased page will produce an ECC mismatch between
446 ** generated and read ECC bytes that has to be dealt with separately.
447 */
448 static int omap_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
449                                 u_char *ecc_code)
450 {
451         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
452                                                         mtd);
453         unsigned long val = 0x0;
454         unsigned long reg, n;
455
456         /* Ex NAND_ECC_HW12_2048 */
457         if ((info->nand.ecc.mode == NAND_ECC_HW) &&
458                 (info->nand.ecc.size  == 2048))
459                 n = 4;
460         else
461                 n = 1;
462
463         /* Start Reading from HW ECC1_Result = 0x200 */
464         reg = (unsigned long)(info->gpmc_baseaddr + GPMC_ECC1_RESULT);
465         while (n--) {
466                 val = __raw_readl(reg);
467                 *ecc_code++ = val;              /* P128e, ..., P1e */
468                 *ecc_code++ = val >> 16;        /* P128o, ..., P1o */
469                 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
470                 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
471                 reg += 4;
472         }
473
474         return 0;
475 } /* omap_calculate_ecc */
476
477 static void omap_enable_hwecc(struct mtd_info *mtd, int mode)
478 {
479         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
480                                                         mtd);
481         unsigned long val = __raw_readl(info->gpmc_baseaddr + GPMC_ECC_CONFIG);
482
483         switch (mode) {
484         case NAND_ECC_READ    :
485                 __raw_writel(0x101, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
486                 /* ECC 16 bit col) | ( CS 0 )  | ECC Enable */
487                 val = (1 << 7) | (0x0) | (0x1) ;
488                 break;
489         case NAND_ECC_READSYN :
490                 __raw_writel(0x100, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
491                 /* ECC 16 bit col) | ( CS 0 )  | ECC Enable */
492                 val = (1 << 7) | (0x0) | (0x1) ;
493                 break;
494         case NAND_ECC_WRITE   :
495                 __raw_writel(0x101, info->gpmc_baseaddr + GPMC_ECC_CONTROL);
496                 /* ECC 16 bit col) | ( CS 0 )  | ECC Enable */
497                 val = (1 << 7) | (0x0) | (0x1) ;
498                 break;
499         default:
500                 DEBUG(MTD_DEBUG_LEVEL0, "Error: Unrecognized Mode[%d]!\n",
501                                         mode);
502                 break;
503         }
504
505         __raw_writel(val, info->gpmc_baseaddr + GPMC_ECC_CONFIG);
506 }
507
508 static int omap_dev_ready(struct mtd_info *mtd)
509 {
510         struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
511                                                         mtd);
512         unsigned int val = __raw_readl(info->gpmc_baseaddr + GPMC_IRQ_STATUS);
513
514         if ((val & 0x100) == 0x100) {
515                 /* Clear IRQ Interrupt */
516                 val |= 0x100;
517                 val &= ~(0x0);
518                 __raw_writel(val, info->gpmc_baseaddr + GPMC_IRQ_STATUS);
519         } else {
520                 unsigned int cnt = 0;
521                 while (cnt++ < 0x1FF) {
522                         if  ((val & 0x100) == 0x100)
523                                 return 0;
524                         val = __raw_readl(info->gpmc_baseaddr +
525                                                         GPMC_IRQ_STATUS);
526                 }
527         }
528
529         return 1;
530 }
531
532 static int __devinit omap_nand_probe(struct platform_device *pdev)
533 {
534         struct omap_nand_info           *info;
535         struct omap_nand_platform_data  *pdata;
536         int                             err;
537         unsigned long val;
538
539
540         pdata = pdev->dev.platform_data;
541         if (pdata == NULL) {
542                 dev_err(&pdev->dev, "platform data missing\n");
543                 return -ENODEV;
544         }
545
546         info = kzalloc(sizeof(struct omap_nand_info), GFP_KERNEL);
547         if (!info) return -ENOMEM;
548
549         platform_set_drvdata(pdev, info);
550
551         spin_lock_init(&info->controller.lock);
552         init_waitqueue_head(&info->controller.wq);
553
554         info->pdev = pdev;
555
556         info->gpmc_cs           = pdata->cs;
557         info->gpmc_baseaddr     = pdata->gpmc_baseaddr;
558         info->gpmc_cs_baseaddr  = pdata->gpmc_cs_baseaddr;
559
560         info->mtd.priv          = &info->nand;
561         info->mtd.name          = pdev->dev.bus_id;
562         info->mtd.owner         = THIS_MODULE;
563
564         err = gpmc_cs_request(info->gpmc_cs, NAND_IO_SIZE, &info->phys_base);
565         if (err < 0) {
566                 dev_err(&pdev->dev, "Cannot request GPMC CS\n");
567                 goto out_free_info;
568         }
569
570         /* Enable RD PIN Monitoring Reg */
571         val  = gpmc_cs_read_reg(info->gpmc_cs, GPMC_CS_CONFIG1);
572         val |= WR_RD_PIN_MONITORING;
573         gpmc_cs_write_reg(info->gpmc_cs, GPMC_CS_CONFIG1, val);
574
575         val  = gpmc_cs_read_reg(info->gpmc_cs, GPMC_CS_CONFIG7);
576         val &= ~(0xf << 8);
577         val |=  (0xc & 0xf) << 8;
578         gpmc_cs_write_reg(info->gpmc_cs, GPMC_CS_CONFIG7, val);
579
580         if (!request_mem_region(info->phys_base, NAND_IO_SIZE,
581                                 pdev->dev.driver->name)) {
582                 err = -EBUSY;
583                 goto out_free_cs;
584         }
585
586         info->nand.IO_ADDR_R = ioremap(info->phys_base, NAND_IO_SIZE);
587         if (!info->nand.IO_ADDR_R) {
588                 err = -ENOMEM;
589                 goto out_release_mem_region;
590         }
591         info->nand.controller = &info->controller;
592
593         info->nand.IO_ADDR_W = info->nand.IO_ADDR_R;
594         info->nand.cmd_ctrl  = omap_hwcontrol;
595
596         info->nand.read_buf   = omap_read_buf;
597         info->nand.write_buf  = omap_write_buf;
598         info->nand.verify_buf = omap_verify_buf;
599
600         info->nand.dev_ready  = omap_dev_ready;
601         info->nand.chip_delay = 0;
602
603         /* Options */
604         info->nand.options   = NAND_BUSWIDTH_16;
605         info->nand.options  |= NAND_SKIP_BBTSCAN;
606
607         if (hw_ecc) {
608                 /* init HW ECC */
609                 omap_hwecc_init(&info->mtd);
610
611                 info->nand.ecc.calculate = omap_calculate_ecc;
612                 info->nand.ecc.hwctl     = omap_enable_hwecc;
613                 info->nand.ecc.correct   = omap_correct_data;
614                 info->nand.ecc.mode      = NAND_ECC_HW;
615                 info->nand.ecc.bytes     = 12;
616                 info->nand.ecc.size      = 2048;
617                 info->nand.ecc.layout    = &omap_hw_eccoob;
618
619         } else {
620                 info->nand.ecc.mode = NAND_ECC_SOFT;
621         }
622
623
624         /* DIP switches on some boards change between 8 and 16 bit
625          * bus widths for flash.  Try the other width if the first try fails.
626          */
627         if (nand_scan(&info->mtd, 1)) {
628                 info->nand.options ^= NAND_BUSWIDTH_16;
629                 if (nand_scan(&info->mtd, 1)) {
630                         err = -ENXIO;
631                         goto out_release_mem_region;
632                 }
633         }
634
635 #ifdef CONFIG_MTD_PARTITIONS
636         err = parse_mtd_partitions(&info->mtd, part_probes, &info->parts, 0);
637         if (err > 0)
638                 add_mtd_partitions(&info->mtd, info->parts, err);
639         else if (err < 0 && pdata->parts)
640                 add_mtd_partitions(&info->mtd, pdata->parts, pdata->nr_parts);
641         else
642 #endif
643                 add_mtd_device(&info->mtd);
644
645         omap_nand_wp(&info->mtd, NAND_WP_OFF);
646
647         platform_set_drvdata(pdev, &info->mtd);
648
649         return 0;
650
651 out_release_mem_region:
652         release_mem_region(info->phys_base, NAND_IO_SIZE);
653 out_free_cs:
654         gpmc_cs_free(info->gpmc_cs);
655 out_free_info:
656         kfree(info);
657
658         return err;
659 }
660
661 static int omap_nand_remove(struct platform_device *pdev)
662 {
663         struct mtd_info *mtd = platform_get_drvdata(pdev);
664         struct omap_nand_info *info = mtd->priv;
665
666         platform_set_drvdata(pdev, NULL);
667         /* Release NAND device, its internal structures and partitions */
668         nand_release(&info->mtd);
669         iounmap(info->nand.IO_ADDR_R);
670         kfree(&info->mtd);
671         return 0;
672 }
673
674 static struct platform_driver omap_nand_driver = {
675         .probe          = omap_nand_probe,
676         .remove         = omap_nand_remove,
677         .driver         = {
678                 .name   = DRIVER_NAME,
679                 .owner  = THIS_MODULE,
680         },
681 };
682 MODULE_ALIAS(DRIVER_NAME);
683
684 static int __init omap_nand_init(void)
685 {
686         printk(KERN_INFO "%s driver initializing\n", DRIVER_NAME);
687         return platform_driver_register(&omap_nand_driver);
688 }
689
690 static void __exit omap_nand_exit(void)
691 {
692         platform_driver_unregister(&omap_nand_driver);
693 }
694
695 module_init(omap_nand_init);
696 module_exit(omap_nand_exit);
697
698 MODULE_LICENSE("GPL");
699 MODULE_DESCRIPTION("Glue layer for NAND flash on TI OMAP boards");