Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jbarnes...
[pandora-kernel.git] / drivers / mtd / nand / davinci_nand.c
1 /*
2  * davinci_nand.c - NAND Flash Driver for DaVinci family chips
3  *
4  * Copyright © 2006 Texas Instruments.
5  *
6  * Port to 2.6.23 Copyright © 2008 by:
7  *   Sander Huijsen <Shuijsen@optelecom-nkf.com>
8  *   Troy Kisky <troy.kisky@boundarydevices.com>
9  *   Dirk Behme <Dirk.Behme@gmail.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/err.h>
31 #include <linux/clk.h>
32 #include <linux/io.h>
33 #include <linux/mtd/nand.h>
34 #include <linux/mtd/partitions.h>
35
36 #include <mach/nand.h>
37
38 #include <asm/mach-types.h>
39
40
41 /*
42  * This is a device driver for the NAND flash controller found on the
43  * various DaVinci family chips.  It handles up to four SoC chipselects,
44  * and some flavors of secondary chipselect (e.g. based on A12) as used
45  * with multichip packages.
46  *
47  * The 1-bit ECC hardware is supported, as well as the newer 4-bit ECC
48  * available on chips like the DM355 and OMAP-L137 and needed with the
49  * more error-prone MLC NAND chips.
50  *
51  * This driver assumes EM_WAIT connects all the NAND devices' RDY/nBUSY
52  * outputs in a "wire-AND" configuration, with no per-chip signals.
53  */
54 struct davinci_nand_info {
55         struct mtd_info         mtd;
56         struct nand_chip        chip;
57         struct nand_ecclayout   ecclayout;
58
59         struct device           *dev;
60         struct clk              *clk;
61         bool                    partitioned;
62
63         bool                    is_readmode;
64
65         void __iomem            *base;
66         void __iomem            *vaddr;
67
68         uint32_t                ioaddr;
69         uint32_t                current_cs;
70
71         uint32_t                mask_chipsel;
72         uint32_t                mask_ale;
73         uint32_t                mask_cle;
74
75         uint32_t                core_chipsel;
76 };
77
78 static DEFINE_SPINLOCK(davinci_nand_lock);
79 static bool ecc4_busy;
80
81 #define to_davinci_nand(m) container_of(m, struct davinci_nand_info, mtd)
82
83
84 static inline unsigned int davinci_nand_readl(struct davinci_nand_info *info,
85                 int offset)
86 {
87         return __raw_readl(info->base + offset);
88 }
89
90 static inline void davinci_nand_writel(struct davinci_nand_info *info,
91                 int offset, unsigned long value)
92 {
93         __raw_writel(value, info->base + offset);
94 }
95
96 /*----------------------------------------------------------------------*/
97
98 /*
99  * Access to hardware control lines:  ALE, CLE, secondary chipselect.
100  */
101
102 static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
103                                    unsigned int ctrl)
104 {
105         struct davinci_nand_info        *info = to_davinci_nand(mtd);
106         uint32_t                        addr = info->current_cs;
107         struct nand_chip                *nand = mtd->priv;
108
109         /* Did the control lines change? */
110         if (ctrl & NAND_CTRL_CHANGE) {
111                 if ((ctrl & NAND_CTRL_CLE) == NAND_CTRL_CLE)
112                         addr |= info->mask_cle;
113                 else if ((ctrl & NAND_CTRL_ALE) == NAND_CTRL_ALE)
114                         addr |= info->mask_ale;
115
116                 nand->IO_ADDR_W = (void __iomem __force *)addr;
117         }
118
119         if (cmd != NAND_CMD_NONE)
120                 iowrite8(cmd, nand->IO_ADDR_W);
121 }
122
123 static void nand_davinci_select_chip(struct mtd_info *mtd, int chip)
124 {
125         struct davinci_nand_info        *info = to_davinci_nand(mtd);
126         uint32_t                        addr = info->ioaddr;
127
128         /* maybe kick in a second chipselect */
129         if (chip > 0)
130                 addr |= info->mask_chipsel;
131         info->current_cs = addr;
132
133         info->chip.IO_ADDR_W = (void __iomem __force *)addr;
134         info->chip.IO_ADDR_R = info->chip.IO_ADDR_W;
135 }
136
137 /*----------------------------------------------------------------------*/
138
139 /*
140  * 1-bit hardware ECC ... context maintained for each core chipselect
141  */
142
143 static inline uint32_t nand_davinci_readecc_1bit(struct mtd_info *mtd)
144 {
145         struct davinci_nand_info *info = to_davinci_nand(mtd);
146
147         return davinci_nand_readl(info, NANDF1ECC_OFFSET
148                         + 4 * info->core_chipsel);
149 }
150
151 static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode)
152 {
153         struct davinci_nand_info *info;
154         uint32_t nandcfr;
155         unsigned long flags;
156
157         info = to_davinci_nand(mtd);
158
159         /* Reset ECC hardware */
160         nand_davinci_readecc_1bit(mtd);
161
162         spin_lock_irqsave(&davinci_nand_lock, flags);
163
164         /* Restart ECC hardware */
165         nandcfr = davinci_nand_readl(info, NANDFCR_OFFSET);
166         nandcfr |= BIT(8 + info->core_chipsel);
167         davinci_nand_writel(info, NANDFCR_OFFSET, nandcfr);
168
169         spin_unlock_irqrestore(&davinci_nand_lock, flags);
170 }
171
172 /*
173  * Read hardware ECC value and pack into three bytes
174  */
175 static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
176                                       const u_char *dat, u_char *ecc_code)
177 {
178         unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
179         unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
180
181         /* invert so that erased block ecc is correct */
182         ecc24 = ~ecc24;
183         ecc_code[0] = (u_char)(ecc24);
184         ecc_code[1] = (u_char)(ecc24 >> 8);
185         ecc_code[2] = (u_char)(ecc24 >> 16);
186
187         return 0;
188 }
189
190 static int nand_davinci_correct_1bit(struct mtd_info *mtd, u_char *dat,
191                                      u_char *read_ecc, u_char *calc_ecc)
192 {
193         struct nand_chip *chip = mtd->priv;
194         uint32_t eccNand = read_ecc[0] | (read_ecc[1] << 8) |
195                                           (read_ecc[2] << 16);
196         uint32_t eccCalc = calc_ecc[0] | (calc_ecc[1] << 8) |
197                                           (calc_ecc[2] << 16);
198         uint32_t diff = eccCalc ^ eccNand;
199
200         if (diff) {
201                 if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
202                         /* Correctable error */
203                         if ((diff >> (12 + 3)) < chip->ecc.size) {
204                                 dat[diff >> (12 + 3)] ^= BIT((diff >> 12) & 7);
205                                 return 1;
206                         } else {
207                                 return -1;
208                         }
209                 } else if (!(diff & (diff - 1))) {
210                         /* Single bit ECC error in the ECC itself,
211                          * nothing to fix */
212                         return 1;
213                 } else {
214                         /* Uncorrectable error */
215                         return -1;
216                 }
217
218         }
219         return 0;
220 }
221
222 /*----------------------------------------------------------------------*/
223
224 /*
225  * 4-bit hardware ECC ... context maintained over entire AEMIF
226  *
227  * This is a syndrome engine, but we avoid NAND_ECC_HW_SYNDROME
228  * since that forces use of a problematic "infix OOB" layout.
229  * Among other things, it trashes manufacturer bad block markers.
230  * Also, and specific to this hardware, it ECC-protects the "prepad"
231  * in the OOB ... while having ECC protection for parts of OOB would
232  * seem useful, the current MTD stack sometimes wants to update the
233  * OOB without recomputing ECC.
234  */
235
236 static void nand_davinci_hwctl_4bit(struct mtd_info *mtd, int mode)
237 {
238         struct davinci_nand_info *info = to_davinci_nand(mtd);
239         unsigned long flags;
240         u32 val;
241
242         spin_lock_irqsave(&davinci_nand_lock, flags);
243
244         /* Start 4-bit ECC calculation for read/write */
245         val = davinci_nand_readl(info, NANDFCR_OFFSET);
246         val &= ~(0x03 << 4);
247         val |= (info->core_chipsel << 4) | BIT(12);
248         davinci_nand_writel(info, NANDFCR_OFFSET, val);
249
250         info->is_readmode = (mode == NAND_ECC_READ);
251
252         spin_unlock_irqrestore(&davinci_nand_lock, flags);
253 }
254
255 /* Read raw ECC code after writing to NAND. */
256 static void
257 nand_davinci_readecc_4bit(struct davinci_nand_info *info, u32 code[4])
258 {
259         const u32 mask = 0x03ff03ff;
260
261         code[0] = davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET) & mask;
262         code[1] = davinci_nand_readl(info, NAND_4BIT_ECC2_OFFSET) & mask;
263         code[2] = davinci_nand_readl(info, NAND_4BIT_ECC3_OFFSET) & mask;
264         code[3] = davinci_nand_readl(info, NAND_4BIT_ECC4_OFFSET) & mask;
265 }
266
267 /* Terminate read ECC; or return ECC (as bytes) of data written to NAND. */
268 static int nand_davinci_calculate_4bit(struct mtd_info *mtd,
269                 const u_char *dat, u_char *ecc_code)
270 {
271         struct davinci_nand_info *info = to_davinci_nand(mtd);
272         u32 raw_ecc[4], *p;
273         unsigned i;
274
275         /* After a read, terminate ECC calculation by a dummy read
276          * of some 4-bit ECC register.  ECC covers everything that
277          * was read; correct() just uses the hardware state, so
278          * ecc_code is not needed.
279          */
280         if (info->is_readmode) {
281                 davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
282                 return 0;
283         }
284
285         /* Pack eight raw 10-bit ecc values into ten bytes, making
286          * two passes which each convert four values (in upper and
287          * lower halves of two 32-bit words) into five bytes.  The
288          * ROM boot loader uses this same packing scheme.
289          */
290         nand_davinci_readecc_4bit(info, raw_ecc);
291         for (i = 0, p = raw_ecc; i < 2; i++, p += 2) {
292                 *ecc_code++ =   p[0]        & 0xff;
293                 *ecc_code++ = ((p[0] >>  8) & 0x03) | ((p[0] >> 14) & 0xfc);
294                 *ecc_code++ = ((p[0] >> 22) & 0x0f) | ((p[1] <<  4) & 0xf0);
295                 *ecc_code++ = ((p[1] >>  4) & 0x3f) | ((p[1] >> 10) & 0xc0);
296                 *ecc_code++ =  (p[1] >> 18) & 0xff;
297         }
298
299         return 0;
300 }
301
302 /* Correct up to 4 bits in data we just read, using state left in the
303  * hardware plus the ecc_code computed when it was first written.
304  */
305 static int nand_davinci_correct_4bit(struct mtd_info *mtd,
306                 u_char *data, u_char *ecc_code, u_char *null)
307 {
308         int i;
309         struct davinci_nand_info *info = to_davinci_nand(mtd);
310         unsigned short ecc10[8];
311         unsigned short *ecc16;
312         u32 syndrome[4];
313         unsigned num_errors, corrected;
314
315         /* All bytes 0xff?  It's an erased page; ignore its ECC. */
316         for (i = 0; i < 10; i++) {
317                 if (ecc_code[i] != 0xff)
318                         goto compare;
319         }
320         return 0;
321
322 compare:
323         /* Unpack ten bytes into eight 10 bit values.  We know we're
324          * little-endian, and use type punning for less shifting/masking.
325          */
326         if (WARN_ON(0x01 & (unsigned) ecc_code))
327                 return -EINVAL;
328         ecc16 = (unsigned short *)ecc_code;
329
330         ecc10[0] =  (ecc16[0] >>  0) & 0x3ff;
331         ecc10[1] = ((ecc16[0] >> 10) & 0x3f) | ((ecc16[1] << 6) & 0x3c0);
332         ecc10[2] =  (ecc16[1] >>  4) & 0x3ff;
333         ecc10[3] = ((ecc16[1] >> 14) & 0x3)  | ((ecc16[2] << 2) & 0x3fc);
334         ecc10[4] =  (ecc16[2] >>  8)         | ((ecc16[3] << 8) & 0x300);
335         ecc10[5] =  (ecc16[3] >>  2) & 0x3ff;
336         ecc10[6] = ((ecc16[3] >> 12) & 0xf)  | ((ecc16[4] << 4) & 0x3f0);
337         ecc10[7] =  (ecc16[4] >>  6) & 0x3ff;
338
339         /* Tell ECC controller about the expected ECC codes. */
340         for (i = 7; i >= 0; i--)
341                 davinci_nand_writel(info, NAND_4BIT_ECC_LOAD_OFFSET, ecc10[i]);
342
343         /* Allow time for syndrome calculation ... then read it.
344          * A syndrome of all zeroes 0 means no detected errors.
345          */
346         davinci_nand_readl(info, NANDFSR_OFFSET);
347         nand_davinci_readecc_4bit(info, syndrome);
348         if (!(syndrome[0] | syndrome[1] | syndrome[2] | syndrome[3]))
349                 return 0;
350
351         /* Start address calculation, and wait for it to complete.
352          * We _could_ start reading more data while this is working,
353          * to speed up the overall page read.
354          */
355         davinci_nand_writel(info, NANDFCR_OFFSET,
356                         davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13));
357         for (;;) {
358                 u32     fsr = davinci_nand_readl(info, NANDFSR_OFFSET);
359
360                 switch ((fsr >> 8) & 0x0f) {
361                 case 0:         /* no error, should not happen */
362                         return 0;
363                 case 1:         /* five or more errors detected */
364                         return -EIO;
365                 case 2:         /* error addresses computed */
366                 case 3:
367                         num_errors = 1 + ((fsr >> 16) & 0x03);
368                         goto correct;
369                 default:        /* still working on it */
370                         cpu_relax();
371                         continue;
372                 }
373         }
374
375 correct:
376         /* correct each error */
377         for (i = 0, corrected = 0; i < num_errors; i++) {
378                 int error_address, error_value;
379
380                 if (i > 1) {
381                         error_address = davinci_nand_readl(info,
382                                                 NAND_ERR_ADD2_OFFSET);
383                         error_value = davinci_nand_readl(info,
384                                                 NAND_ERR_ERRVAL2_OFFSET);
385                 } else {
386                         error_address = davinci_nand_readl(info,
387                                                 NAND_ERR_ADD1_OFFSET);
388                         error_value = davinci_nand_readl(info,
389                                                 NAND_ERR_ERRVAL1_OFFSET);
390                 }
391
392                 if (i & 1) {
393                         error_address >>= 16;
394                         error_value >>= 16;
395                 }
396                 error_address &= 0x3ff;
397                 error_address = (512 + 7) - error_address;
398
399                 if (error_address < 512) {
400                         data[error_address] ^= error_value;
401                         corrected++;
402                 }
403         }
404
405         return corrected;
406 }
407
408 /*----------------------------------------------------------------------*/
409
410 /*
411  * NOTE:  NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's
412  * how these chips are normally wired.  This translates to both 8 and 16
413  * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4).
414  *
415  * For now we assume that configuration, or any other one which ignores
416  * the two LSBs for NAND access ... so we can issue 32-bit reads/writes
417  * and have that transparently morphed into multiple NAND operations.
418  */
419 static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
420 {
421         struct nand_chip *chip = mtd->priv;
422
423         if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
424                 ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
425         else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)
426                 ioread16_rep(chip->IO_ADDR_R, buf, len >> 1);
427         else
428                 ioread8_rep(chip->IO_ADDR_R, buf, len);
429 }
430
431 static void nand_davinci_write_buf(struct mtd_info *mtd,
432                 const uint8_t *buf, int len)
433 {
434         struct nand_chip *chip = mtd->priv;
435
436         if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
437                 iowrite32_rep(chip->IO_ADDR_R, buf, len >> 2);
438         else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)
439                 iowrite16_rep(chip->IO_ADDR_R, buf, len >> 1);
440         else
441                 iowrite8_rep(chip->IO_ADDR_R, buf, len);
442 }
443
444 /*
445  * Check hardware register for wait status. Returns 1 if device is ready,
446  * 0 if it is still busy.
447  */
448 static int nand_davinci_dev_ready(struct mtd_info *mtd)
449 {
450         struct davinci_nand_info *info = to_davinci_nand(mtd);
451
452         return davinci_nand_readl(info, NANDFSR_OFFSET) & BIT(0);
453 }
454
455 static void __init nand_dm6446evm_flash_init(struct davinci_nand_info *info)
456 {
457         uint32_t regval, a1cr;
458
459         /*
460          * NAND FLASH timings @ PLL1 == 459 MHz
461          *  - AEMIF.CLK freq   = PLL1/6 = 459/6 = 76.5 MHz
462          *  - AEMIF.CLK period = 1/76.5 MHz = 13.1 ns
463          */
464         regval = 0
465                 | (0 << 31)           /* selectStrobe */
466                 | (0 << 30)           /* extWait (never with NAND) */
467                 | (1 << 26)           /* writeSetup      10 ns */
468                 | (3 << 20)           /* writeStrobe     40 ns */
469                 | (1 << 17)           /* writeHold       10 ns */
470                 | (0 << 13)           /* readSetup       10 ns */
471                 | (3 << 7)            /* readStrobe      60 ns */
472                 | (0 << 4)            /* readHold        10 ns */
473                 | (3 << 2)            /* turnAround      ?? ns */
474                 | (0 << 0)            /* asyncSize       8-bit bus */
475                 ;
476         a1cr = davinci_nand_readl(info, A1CR_OFFSET);
477         if (a1cr != regval) {
478                 dev_dbg(info->dev, "Warning: NAND config: Set A1CR " \
479                        "reg to 0x%08x, was 0x%08x, should be done by " \
480                        "bootloader.\n", regval, a1cr);
481                 davinci_nand_writel(info, A1CR_OFFSET, regval);
482         }
483 }
484
485 /*----------------------------------------------------------------------*/
486
487 /* An ECC layout for using 4-bit ECC with small-page flash, storing
488  * ten ECC bytes plus the manufacturer's bad block marker byte, and
489  * and not overlapping the default BBT markers.
490  */
491 static struct nand_ecclayout hwecc4_small __initconst = {
492         .eccbytes = 10,
493         .eccpos = { 0, 1, 2, 3, 4,
494                 /* offset 5 holds the badblock marker */
495                 6, 7,
496                 13, 14, 15, },
497         .oobfree = {
498                 {.offset = 8, .length = 5, },
499                 {.offset = 16, },
500         },
501 };
502
503
504 static int __init nand_davinci_probe(struct platform_device *pdev)
505 {
506         struct davinci_nand_pdata       *pdata = pdev->dev.platform_data;
507         struct davinci_nand_info        *info;
508         struct resource                 *res1;
509         struct resource                 *res2;
510         void __iomem                    *vaddr;
511         void __iomem                    *base;
512         int                             ret;
513         uint32_t                        val;
514         nand_ecc_modes_t                ecc_mode;
515
516         /* insist on board-specific configuration */
517         if (!pdata)
518                 return -ENODEV;
519
520         /* which external chipselect will we be managing? */
521         if (pdev->id < 0 || pdev->id > 3)
522                 return -ENODEV;
523
524         info = kzalloc(sizeof(*info), GFP_KERNEL);
525         if (!info) {
526                 dev_err(&pdev->dev, "unable to allocate memory\n");
527                 ret = -ENOMEM;
528                 goto err_nomem;
529         }
530
531         platform_set_drvdata(pdev, info);
532
533         res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
534         res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
535         if (!res1 || !res2) {
536                 dev_err(&pdev->dev, "resource missing\n");
537                 ret = -EINVAL;
538                 goto err_nomem;
539         }
540
541         vaddr = ioremap(res1->start, res1->end - res1->start);
542         base = ioremap(res2->start, res2->end - res2->start);
543         if (!vaddr || !base) {
544                 dev_err(&pdev->dev, "ioremap failed\n");
545                 ret = -EINVAL;
546                 goto err_ioremap;
547         }
548
549         info->dev               = &pdev->dev;
550         info->base              = base;
551         info->vaddr             = vaddr;
552
553         info->mtd.priv          = &info->chip;
554         info->mtd.name          = dev_name(&pdev->dev);
555         info->mtd.owner         = THIS_MODULE;
556
557         info->mtd.dev.parent    = &pdev->dev;
558
559         info->chip.IO_ADDR_R    = vaddr;
560         info->chip.IO_ADDR_W    = vaddr;
561         info->chip.chip_delay   = 0;
562         info->chip.select_chip  = nand_davinci_select_chip;
563
564         /* options such as NAND_USE_FLASH_BBT or 16-bit widths */
565         info->chip.options      = pdata->options;
566
567         info->ioaddr            = (uint32_t __force) vaddr;
568
569         info->current_cs        = info->ioaddr;
570         info->core_chipsel      = pdev->id;
571         info->mask_chipsel      = pdata->mask_chipsel;
572
573         /* use nandboot-capable ALE/CLE masks by default */
574         info->mask_ale          = pdata->mask_cle ? : MASK_ALE;
575         info->mask_cle          = pdata->mask_cle ? : MASK_CLE;
576
577         /* Set address of hardware control function */
578         info->chip.cmd_ctrl     = nand_davinci_hwcontrol;
579         info->chip.dev_ready    = nand_davinci_dev_ready;
580
581         /* Speed up buffer I/O */
582         info->chip.read_buf     = nand_davinci_read_buf;
583         info->chip.write_buf    = nand_davinci_write_buf;
584
585         /* Use board-specific ECC config */
586         ecc_mode                = pdata->ecc_mode;
587
588         ret = -EINVAL;
589         switch (ecc_mode) {
590         case NAND_ECC_NONE:
591         case NAND_ECC_SOFT:
592                 pdata->ecc_bits = 0;
593                 break;
594         case NAND_ECC_HW:
595                 if (pdata->ecc_bits == 4) {
596                         /* No sanity checks:  CPUs must support this,
597                          * and the chips may not use NAND_BUSWIDTH_16.
598                          */
599
600                         /* No sharing 4-bit hardware between chipselects yet */
601                         spin_lock_irq(&davinci_nand_lock);
602                         if (ecc4_busy)
603                                 ret = -EBUSY;
604                         else
605                                 ecc4_busy = true;
606                         spin_unlock_irq(&davinci_nand_lock);
607
608                         if (ret == -EBUSY)
609                                 goto err_ecc;
610
611                         info->chip.ecc.calculate = nand_davinci_calculate_4bit;
612                         info->chip.ecc.correct = nand_davinci_correct_4bit;
613                         info->chip.ecc.hwctl = nand_davinci_hwctl_4bit;
614                         info->chip.ecc.bytes = 10;
615                 } else {
616                         info->chip.ecc.calculate = nand_davinci_calculate_1bit;
617                         info->chip.ecc.correct = nand_davinci_correct_1bit;
618                         info->chip.ecc.hwctl = nand_davinci_hwctl_1bit;
619                         info->chip.ecc.bytes = 3;
620                 }
621                 info->chip.ecc.size = 512;
622                 break;
623         default:
624                 ret = -EINVAL;
625                 goto err_ecc;
626         }
627         info->chip.ecc.mode = ecc_mode;
628
629         info->clk = clk_get(&pdev->dev, "aemif");
630         if (IS_ERR(info->clk)) {
631                 ret = PTR_ERR(info->clk);
632                 dev_dbg(&pdev->dev, "unable to get AEMIF clock, err %d\n", ret);
633                 goto err_clk;
634         }
635
636         ret = clk_enable(info->clk);
637         if (ret < 0) {
638                 dev_dbg(&pdev->dev, "unable to enable AEMIF clock, err %d\n",
639                         ret);
640                 goto err_clk_enable;
641         }
642
643         /* EMIF timings should normally be set by the boot loader,
644          * especially after boot-from-NAND.  The *only* reason to
645          * have this special casing for the DM6446 EVM is to work
646          * with boot-from-NOR ... with CS0 manually re-jumpered
647          * (after startup) so it addresses the NAND flash, not NOR.
648          * Even for dev boards, that's unusually rude...
649          */
650         if (machine_is_davinci_evm())
651                 nand_dm6446evm_flash_init(info);
652
653         spin_lock_irq(&davinci_nand_lock);
654
655         /* put CSxNAND into NAND mode */
656         val = davinci_nand_readl(info, NANDFCR_OFFSET);
657         val |= BIT(info->core_chipsel);
658         davinci_nand_writel(info, NANDFCR_OFFSET, val);
659
660         spin_unlock_irq(&davinci_nand_lock);
661
662         /* Scan to find existence of the device(s) */
663         ret = nand_scan_ident(&info->mtd, pdata->mask_chipsel ? 2 : 1);
664         if (ret < 0) {
665                 dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
666                 goto err_scan;
667         }
668
669         /* Update ECC layout if needed ... for 1-bit HW ECC, the default
670          * is OK, but it allocates 6 bytes when only 3 are needed (for
671          * each 512 bytes).  For the 4-bit HW ECC, that default is not
672          * usable:  10 bytes are needed, not 6.
673          */
674         if (pdata->ecc_bits == 4) {
675                 int     chunks = info->mtd.writesize / 512;
676
677                 if (!chunks || info->mtd.oobsize < 16) {
678                         dev_dbg(&pdev->dev, "too small\n");
679                         ret = -EINVAL;
680                         goto err_scan;
681                 }
682
683                 /* For small page chips, preserve the manufacturer's
684                  * badblock marking data ... and make sure a flash BBT
685                  * table marker fits in the free bytes.
686                  */
687                 if (chunks == 1) {
688                         info->ecclayout = hwecc4_small;
689                         info->ecclayout.oobfree[1].length =
690                                 info->mtd.oobsize - 16;
691                         goto syndrome_done;
692                 }
693
694                 /* For large page chips we'll be wanting to use a
695                  * not-yet-implemented mode that reads OOB data
696                  * before reading the body of the page, to avoid
697                  * the "infix OOB" model of NAND_ECC_HW_SYNDROME
698                  * (and preserve manufacturer badblock markings).
699                  */
700                 dev_warn(&pdev->dev, "no 4-bit ECC support yet "
701                                 "for large page NAND\n");
702                 ret = -EIO;
703                 goto err_scan;
704
705 syndrome_done:
706                 info->chip.ecc.layout = &info->ecclayout;
707         }
708
709         ret = nand_scan_tail(&info->mtd);
710         if (ret < 0)
711                 goto err_scan;
712
713         if (mtd_has_partitions()) {
714                 struct mtd_partition    *mtd_parts = NULL;
715                 int                     mtd_parts_nb = 0;
716
717                 if (mtd_has_cmdlinepart()) {
718                         static const char *probes[] __initconst =
719                                 { "cmdlinepart", NULL };
720
721                         mtd_parts_nb = parse_mtd_partitions(&info->mtd, probes,
722                                                             &mtd_parts, 0);
723                 }
724
725                 if (mtd_parts_nb <= 0) {
726                         mtd_parts = pdata->parts;
727                         mtd_parts_nb = pdata->nr_parts;
728                 }
729
730                 /* Register any partitions */
731                 if (mtd_parts_nb > 0) {
732                         ret = add_mtd_partitions(&info->mtd,
733                                         mtd_parts, mtd_parts_nb);
734                         if (ret == 0)
735                                 info->partitioned = true;
736                 }
737
738         } else if (pdata->nr_parts) {
739                 dev_warn(&pdev->dev, "ignoring %d default partitions on %s\n",
740                                 pdata->nr_parts, info->mtd.name);
741         }
742
743         /* If there's no partition info, just package the whole chip
744          * as a single MTD device.
745          */
746         if (!info->partitioned)
747                 ret = add_mtd_device(&info->mtd) ? -ENODEV : 0;
748
749         if (ret < 0)
750                 goto err_scan;
751
752         val = davinci_nand_readl(info, NRCSR_OFFSET);
753         dev_info(&pdev->dev, "controller rev. %d.%d\n",
754                (val >> 8) & 0xff, val & 0xff);
755
756         return 0;
757
758 err_scan:
759         clk_disable(info->clk);
760
761 err_clk_enable:
762         clk_put(info->clk);
763
764         spin_lock_irq(&davinci_nand_lock);
765         if (ecc_mode == NAND_ECC_HW_SYNDROME)
766                 ecc4_busy = false;
767         spin_unlock_irq(&davinci_nand_lock);
768
769 err_ecc:
770 err_clk:
771 err_ioremap:
772         if (base)
773                 iounmap(base);
774         if (vaddr)
775                 iounmap(vaddr);
776
777 err_nomem:
778         kfree(info);
779         return ret;
780 }
781
782 static int __exit nand_davinci_remove(struct platform_device *pdev)
783 {
784         struct davinci_nand_info *info = platform_get_drvdata(pdev);
785         int status;
786
787         if (mtd_has_partitions() && info->partitioned)
788                 status = del_mtd_partitions(&info->mtd);
789         else
790                 status = del_mtd_device(&info->mtd);
791
792         spin_lock_irq(&davinci_nand_lock);
793         if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME)
794                 ecc4_busy = false;
795         spin_unlock_irq(&davinci_nand_lock);
796
797         iounmap(info->base);
798         iounmap(info->vaddr);
799
800         nand_release(&info->mtd);
801
802         clk_disable(info->clk);
803         clk_put(info->clk);
804
805         kfree(info);
806
807         return 0;
808 }
809
810 static struct platform_driver nand_davinci_driver = {
811         .remove         = __exit_p(nand_davinci_remove),
812         .driver         = {
813                 .name   = "davinci_nand",
814         },
815 };
816 MODULE_ALIAS("platform:davinci_nand");
817
818 static int __init nand_davinci_init(void)
819 {
820         return platform_driver_probe(&nand_davinci_driver, nand_davinci_probe);
821 }
822 module_init(nand_davinci_init);
823
824 static void __exit nand_davinci_exit(void)
825 {
826         platform_driver_unregister(&nand_davinci_driver);
827 }
828 module_exit(nand_davinci_exit);
829
830 MODULE_LICENSE("GPL");
831 MODULE_AUTHOR("Texas Instruments");
832 MODULE_DESCRIPTION("Davinci NAND flash driver");
833