[ARM] pxa: update defconfig for Verdex Pro
[linux-2.6/verdex.git] / drivers / mtd / nand / davinci_nand.c
blobf13f5b9afaf744f0ef6e234bf45115c01a83b219
1 /*
2 * davinci_nand.c - NAND Flash Driver for DaVinci family chips
4 * Copyright © 2006 Texas Instruments.
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>
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.
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.
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.
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>
36 #include <mach/nand.h>
38 #include <asm/mach-types.h>
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.
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.
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.
54 struct davinci_nand_info {
55 struct mtd_info mtd;
56 struct nand_chip chip;
57 struct nand_ecclayout ecclayout;
59 struct device *dev;
60 struct clk *clk;
61 bool partitioned;
63 bool is_readmode;
65 void __iomem *base;
66 void __iomem *vaddr;
68 uint32_t ioaddr;
69 uint32_t current_cs;
71 uint32_t mask_chipsel;
72 uint32_t mask_ale;
73 uint32_t mask_cle;
75 uint32_t core_chipsel;
78 static DEFINE_SPINLOCK(davinci_nand_lock);
79 static bool ecc4_busy;
81 #define to_davinci_nand(m) container_of(m, struct davinci_nand_info, mtd)
84 static inline unsigned int davinci_nand_readl(struct davinci_nand_info *info,
85 int offset)
87 return __raw_readl(info->base + offset);
90 static inline void davinci_nand_writel(struct davinci_nand_info *info,
91 int offset, unsigned long value)
93 __raw_writel(value, info->base + offset);
96 /*----------------------------------------------------------------------*/
99 * Access to hardware control lines: ALE, CLE, secondary chipselect.
102 static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd,
103 unsigned int ctrl)
105 struct davinci_nand_info *info = to_davinci_nand(mtd);
106 uint32_t addr = info->current_cs;
107 struct nand_chip *nand = mtd->priv;
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;
116 nand->IO_ADDR_W = (void __iomem __force *)addr;
119 if (cmd != NAND_CMD_NONE)
120 iowrite8(cmd, nand->IO_ADDR_W);
123 static void nand_davinci_select_chip(struct mtd_info *mtd, int chip)
125 struct davinci_nand_info *info = to_davinci_nand(mtd);
126 uint32_t addr = info->ioaddr;
128 /* maybe kick in a second chipselect */
129 if (chip > 0)
130 addr |= info->mask_chipsel;
131 info->current_cs = addr;
133 info->chip.IO_ADDR_W = (void __iomem __force *)addr;
134 info->chip.IO_ADDR_R = info->chip.IO_ADDR_W;
137 /*----------------------------------------------------------------------*/
140 * 1-bit hardware ECC ... context maintained for each core chipselect
143 static inline uint32_t nand_davinci_readecc_1bit(struct mtd_info *mtd)
145 struct davinci_nand_info *info = to_davinci_nand(mtd);
147 return davinci_nand_readl(info, NANDF1ECC_OFFSET
148 + 4 * info->core_chipsel);
151 static void nand_davinci_hwctl_1bit(struct mtd_info *mtd, int mode)
153 struct davinci_nand_info *info;
154 uint32_t nandcfr;
155 unsigned long flags;
157 info = to_davinci_nand(mtd);
159 /* Reset ECC hardware */
160 nand_davinci_readecc_1bit(mtd);
162 spin_lock_irqsave(&davinci_nand_lock, flags);
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);
169 spin_unlock_irqrestore(&davinci_nand_lock, flags);
173 * Read hardware ECC value and pack into three bytes
175 static int nand_davinci_calculate_1bit(struct mtd_info *mtd,
176 const u_char *dat, u_char *ecc_code)
178 unsigned int ecc_val = nand_davinci_readecc_1bit(mtd);
179 unsigned int ecc24 = (ecc_val & 0x0fff) | ((ecc_val & 0x0fff0000) >> 4);
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);
187 return 0;
190 static int nand_davinci_correct_1bit(struct mtd_info *mtd, u_char *dat,
191 u_char *read_ecc, u_char *calc_ecc)
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;
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;
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;
219 return 0;
222 /*----------------------------------------------------------------------*/
225 * 4-bit hardware ECC ... context maintained over entire AEMIF
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.
236 static void nand_davinci_hwctl_4bit(struct mtd_info *mtd, int mode)
238 struct davinci_nand_info *info = to_davinci_nand(mtd);
239 unsigned long flags;
240 u32 val;
242 spin_lock_irqsave(&davinci_nand_lock, flags);
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);
250 info->is_readmode = (mode == NAND_ECC_READ);
252 spin_unlock_irqrestore(&davinci_nand_lock, flags);
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])
259 const u32 mask = 0x03ff03ff;
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;
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)
271 struct davinci_nand_info *info = to_davinci_nand(mtd);
272 u32 raw_ecc[4], *p;
273 unsigned i;
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.
280 if (info->is_readmode) {
281 davinci_nand_readl(info, NAND_4BIT_ECC1_OFFSET);
282 return 0;
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.
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;
299 return 0;
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.
305 static int nand_davinci_correct_4bit(struct mtd_info *mtd,
306 u_char *data, u_char *ecc_code, u_char *null)
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;
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;
320 return 0;
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.
326 if (WARN_ON(0x01 & (unsigned) ecc_code))
327 return -EINVAL;
328 ecc16 = (unsigned short *)ecc_code;
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;
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]);
343 /* Allow time for syndrome calculation ... then read it.
344 * A syndrome of all zeroes 0 means no detected errors.
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;
352 * Clear any previous address calculation by doing a dummy read of an
353 * error address register.
355 davinci_nand_readl(info, NAND_ERR_ADD1_OFFSET);
357 /* Start address calculation, and wait for it to complete.
358 * We _could_ start reading more data while this is working,
359 * to speed up the overall page read.
361 davinci_nand_writel(info, NANDFCR_OFFSET,
362 davinci_nand_readl(info, NANDFCR_OFFSET) | BIT(13));
363 for (;;) {
364 u32 fsr = davinci_nand_readl(info, NANDFSR_OFFSET);
366 switch ((fsr >> 8) & 0x0f) {
367 case 0: /* no error, should not happen */
368 davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
369 return 0;
370 case 1: /* five or more errors detected */
371 davinci_nand_readl(info, NAND_ERR_ERRVAL1_OFFSET);
372 return -EIO;
373 case 2: /* error addresses computed */
374 case 3:
375 num_errors = 1 + ((fsr >> 16) & 0x03);
376 goto correct;
377 default: /* still working on it */
378 cpu_relax();
379 continue;
383 correct:
384 /* correct each error */
385 for (i = 0, corrected = 0; i < num_errors; i++) {
386 int error_address, error_value;
388 if (i > 1) {
389 error_address = davinci_nand_readl(info,
390 NAND_ERR_ADD2_OFFSET);
391 error_value = davinci_nand_readl(info,
392 NAND_ERR_ERRVAL2_OFFSET);
393 } else {
394 error_address = davinci_nand_readl(info,
395 NAND_ERR_ADD1_OFFSET);
396 error_value = davinci_nand_readl(info,
397 NAND_ERR_ERRVAL1_OFFSET);
400 if (i & 1) {
401 error_address >>= 16;
402 error_value >>= 16;
404 error_address &= 0x3ff;
405 error_address = (512 + 7) - error_address;
407 if (error_address < 512) {
408 data[error_address] ^= error_value;
409 corrected++;
413 return corrected;
416 /*----------------------------------------------------------------------*/
419 * NOTE: NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's
420 * how these chips are normally wired. This translates to both 8 and 16
421 * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4).
423 * For now we assume that configuration, or any other one which ignores
424 * the two LSBs for NAND access ... so we can issue 32-bit reads/writes
425 * and have that transparently morphed into multiple NAND operations.
427 static void nand_davinci_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
429 struct nand_chip *chip = mtd->priv;
431 if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
432 ioread32_rep(chip->IO_ADDR_R, buf, len >> 2);
433 else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)
434 ioread16_rep(chip->IO_ADDR_R, buf, len >> 1);
435 else
436 ioread8_rep(chip->IO_ADDR_R, buf, len);
439 static void nand_davinci_write_buf(struct mtd_info *mtd,
440 const uint8_t *buf, int len)
442 struct nand_chip *chip = mtd->priv;
444 if ((0x03 & ((unsigned)buf)) == 0 && (0x03 & len) == 0)
445 iowrite32_rep(chip->IO_ADDR_R, buf, len >> 2);
446 else if ((0x01 & ((unsigned)buf)) == 0 && (0x01 & len) == 0)
447 iowrite16_rep(chip->IO_ADDR_R, buf, len >> 1);
448 else
449 iowrite8_rep(chip->IO_ADDR_R, buf, len);
453 * Check hardware register for wait status. Returns 1 if device is ready,
454 * 0 if it is still busy.
456 static int nand_davinci_dev_ready(struct mtd_info *mtd)
458 struct davinci_nand_info *info = to_davinci_nand(mtd);
460 return davinci_nand_readl(info, NANDFSR_OFFSET) & BIT(0);
463 static void __init nand_dm6446evm_flash_init(struct davinci_nand_info *info)
465 uint32_t regval, a1cr;
468 * NAND FLASH timings @ PLL1 == 459 MHz
469 * - AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz
470 * - AEMIF.CLK period = 1/76.5 MHz = 13.1 ns
472 regval = 0
473 | (0 << 31) /* selectStrobe */
474 | (0 << 30) /* extWait (never with NAND) */
475 | (1 << 26) /* writeSetup 10 ns */
476 | (3 << 20) /* writeStrobe 40 ns */
477 | (1 << 17) /* writeHold 10 ns */
478 | (0 << 13) /* readSetup 10 ns */
479 | (3 << 7) /* readStrobe 60 ns */
480 | (0 << 4) /* readHold 10 ns */
481 | (3 << 2) /* turnAround ?? ns */
482 | (0 << 0) /* asyncSize 8-bit bus */
484 a1cr = davinci_nand_readl(info, A1CR_OFFSET);
485 if (a1cr != regval) {
486 dev_dbg(info->dev, "Warning: NAND config: Set A1CR " \
487 "reg to 0x%08x, was 0x%08x, should be done by " \
488 "bootloader.\n", regval, a1cr);
489 davinci_nand_writel(info, A1CR_OFFSET, regval);
493 /*----------------------------------------------------------------------*/
495 /* An ECC layout for using 4-bit ECC with small-page flash, storing
496 * ten ECC bytes plus the manufacturer's bad block marker byte, and
497 * and not overlapping the default BBT markers.
499 static struct nand_ecclayout hwecc4_small __initconst = {
500 .eccbytes = 10,
501 .eccpos = { 0, 1, 2, 3, 4,
502 /* offset 5 holds the badblock marker */
503 6, 7,
504 13, 14, 15, },
505 .oobfree = {
506 {.offset = 8, .length = 5, },
507 {.offset = 16, },
511 /* An ECC layout for using 4-bit ECC with large-page (2048bytes) flash,
512 * storing ten ECC bytes plus the manufacturer's bad block marker byte,
513 * and not overlapping the default BBT markers.
515 static struct nand_ecclayout hwecc4_2048 __initconst = {
516 .eccbytes = 40,
517 .eccpos = {
518 /* at the end of spare sector */
519 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
520 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
521 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
522 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
524 .oobfree = {
525 /* 2 bytes at offset 0 hold manufacturer badblock markers */
526 {.offset = 2, .length = 22, },
527 /* 5 bytes at offset 8 hold BBT markers */
528 /* 8 bytes at offset 16 hold JFFS2 clean markers */
532 static int __init nand_davinci_probe(struct platform_device *pdev)
534 struct davinci_nand_pdata *pdata = pdev->dev.platform_data;
535 struct davinci_nand_info *info;
536 struct resource *res1;
537 struct resource *res2;
538 void __iomem *vaddr;
539 void __iomem *base;
540 int ret;
541 uint32_t val;
542 nand_ecc_modes_t ecc_mode;
544 /* insist on board-specific configuration */
545 if (!pdata)
546 return -ENODEV;
548 /* which external chipselect will we be managing? */
549 if (pdev->id < 0 || pdev->id > 3)
550 return -ENODEV;
552 info = kzalloc(sizeof(*info), GFP_KERNEL);
553 if (!info) {
554 dev_err(&pdev->dev, "unable to allocate memory\n");
555 ret = -ENOMEM;
556 goto err_nomem;
559 platform_set_drvdata(pdev, info);
561 res1 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
562 res2 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
563 if (!res1 || !res2) {
564 dev_err(&pdev->dev, "resource missing\n");
565 ret = -EINVAL;
566 goto err_nomem;
569 vaddr = ioremap(res1->start, res1->end - res1->start);
570 base = ioremap(res2->start, res2->end - res2->start);
571 if (!vaddr || !base) {
572 dev_err(&pdev->dev, "ioremap failed\n");
573 ret = -EINVAL;
574 goto err_ioremap;
577 info->dev = &pdev->dev;
578 info->base = base;
579 info->vaddr = vaddr;
581 info->mtd.priv = &info->chip;
582 info->mtd.name = dev_name(&pdev->dev);
583 info->mtd.owner = THIS_MODULE;
585 info->mtd.dev.parent = &pdev->dev;
587 info->chip.IO_ADDR_R = vaddr;
588 info->chip.IO_ADDR_W = vaddr;
589 info->chip.chip_delay = 0;
590 info->chip.select_chip = nand_davinci_select_chip;
592 /* options such as NAND_USE_FLASH_BBT or 16-bit widths */
593 info->chip.options = pdata->options;
595 info->ioaddr = (uint32_t __force) vaddr;
597 info->current_cs = info->ioaddr;
598 info->core_chipsel = pdev->id;
599 info->mask_chipsel = pdata->mask_chipsel;
601 /* use nandboot-capable ALE/CLE masks by default */
602 info->mask_ale = pdata->mask_cle ? : MASK_ALE;
603 info->mask_cle = pdata->mask_cle ? : MASK_CLE;
605 /* Set address of hardware control function */
606 info->chip.cmd_ctrl = nand_davinci_hwcontrol;
607 info->chip.dev_ready = nand_davinci_dev_ready;
609 /* Speed up buffer I/O */
610 info->chip.read_buf = nand_davinci_read_buf;
611 info->chip.write_buf = nand_davinci_write_buf;
613 /* Use board-specific ECC config */
614 ecc_mode = pdata->ecc_mode;
616 ret = -EINVAL;
617 switch (ecc_mode) {
618 case NAND_ECC_NONE:
619 case NAND_ECC_SOFT:
620 pdata->ecc_bits = 0;
621 break;
622 case NAND_ECC_HW:
623 if (pdata->ecc_bits == 4) {
624 /* No sanity checks: CPUs must support this,
625 * and the chips may not use NAND_BUSWIDTH_16.
628 /* No sharing 4-bit hardware between chipselects yet */
629 spin_lock_irq(&davinci_nand_lock);
630 if (ecc4_busy)
631 ret = -EBUSY;
632 else
633 ecc4_busy = true;
634 spin_unlock_irq(&davinci_nand_lock);
636 if (ret == -EBUSY)
637 goto err_ecc;
639 info->chip.ecc.calculate = nand_davinci_calculate_4bit;
640 info->chip.ecc.correct = nand_davinci_correct_4bit;
641 info->chip.ecc.hwctl = nand_davinci_hwctl_4bit;
642 info->chip.ecc.bytes = 10;
643 } else {
644 info->chip.ecc.calculate = nand_davinci_calculate_1bit;
645 info->chip.ecc.correct = nand_davinci_correct_1bit;
646 info->chip.ecc.hwctl = nand_davinci_hwctl_1bit;
647 info->chip.ecc.bytes = 3;
649 info->chip.ecc.size = 512;
650 break;
651 default:
652 ret = -EINVAL;
653 goto err_ecc;
655 info->chip.ecc.mode = ecc_mode;
657 info->clk = clk_get(&pdev->dev, "aemif");
658 if (IS_ERR(info->clk)) {
659 ret = PTR_ERR(info->clk);
660 dev_dbg(&pdev->dev, "unable to get AEMIF clock, err %d\n", ret);
661 goto err_clk;
664 ret = clk_enable(info->clk);
665 if (ret < 0) {
666 dev_dbg(&pdev->dev, "unable to enable AEMIF clock, err %d\n",
667 ret);
668 goto err_clk_enable;
671 /* EMIF timings should normally be set by the boot loader,
672 * especially after boot-from-NAND. The *only* reason to
673 * have this special casing for the DM6446 EVM is to work
674 * with boot-from-NOR ... with CS0 manually re-jumpered
675 * (after startup) so it addresses the NAND flash, not NOR.
676 * Even for dev boards, that's unusually rude...
678 if (machine_is_davinci_evm())
679 nand_dm6446evm_flash_init(info);
681 spin_lock_irq(&davinci_nand_lock);
683 /* put CSxNAND into NAND mode */
684 val = davinci_nand_readl(info, NANDFCR_OFFSET);
685 val |= BIT(info->core_chipsel);
686 davinci_nand_writel(info, NANDFCR_OFFSET, val);
688 spin_unlock_irq(&davinci_nand_lock);
690 /* Scan to find existence of the device(s) */
691 ret = nand_scan_ident(&info->mtd, pdata->mask_chipsel ? 2 : 1);
692 if (ret < 0) {
693 dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
694 goto err_scan;
697 /* Update ECC layout if needed ... for 1-bit HW ECC, the default
698 * is OK, but it allocates 6 bytes when only 3 are needed (for
699 * each 512 bytes). For the 4-bit HW ECC, that default is not
700 * usable: 10 bytes are needed, not 6.
702 if (pdata->ecc_bits == 4) {
703 int chunks = info->mtd.writesize / 512;
705 if (!chunks || info->mtd.oobsize < 16) {
706 dev_dbg(&pdev->dev, "too small\n");
707 ret = -EINVAL;
708 goto err_scan;
711 /* For small page chips, preserve the manufacturer's
712 * badblock marking data ... and make sure a flash BBT
713 * table marker fits in the free bytes.
715 if (chunks == 1) {
716 info->ecclayout = hwecc4_small;
717 info->ecclayout.oobfree[1].length =
718 info->mtd.oobsize - 16;
719 goto syndrome_done;
721 if (chunks == 4) {
722 info->ecclayout = hwecc4_2048;
723 info->chip.ecc.mode = NAND_ECC_HW_OOB_FIRST;
724 goto syndrome_done;
727 /* 4KiB page chips are not yet supported. The eccpos from
728 * nand_ecclayout cannot hold 80 bytes and change to eccpos[]
729 * breaks userspace ioctl interface with mtd-utils. Once we
730 * resolve this issue, NAND_ECC_HW_OOB_FIRST mode can be used
731 * for the 4KiB page chips.
733 dev_warn(&pdev->dev, "no 4-bit ECC support yet "
734 "for 4KiB-page NAND\n");
735 ret = -EIO;
736 goto err_scan;
738 syndrome_done:
739 info->chip.ecc.layout = &info->ecclayout;
742 ret = nand_scan_tail(&info->mtd);
743 if (ret < 0)
744 goto err_scan;
746 if (mtd_has_partitions()) {
747 struct mtd_partition *mtd_parts = NULL;
748 int mtd_parts_nb = 0;
750 if (mtd_has_cmdlinepart()) {
751 static const char *probes[] __initconst =
752 { "cmdlinepart", NULL };
754 mtd_parts_nb = parse_mtd_partitions(&info->mtd, probes,
755 &mtd_parts, 0);
758 if (mtd_parts_nb <= 0) {
759 mtd_parts = pdata->parts;
760 mtd_parts_nb = pdata->nr_parts;
763 /* Register any partitions */
764 if (mtd_parts_nb > 0) {
765 ret = add_mtd_partitions(&info->mtd,
766 mtd_parts, mtd_parts_nb);
767 if (ret == 0)
768 info->partitioned = true;
771 } else if (pdata->nr_parts) {
772 dev_warn(&pdev->dev, "ignoring %d default partitions on %s\n",
773 pdata->nr_parts, info->mtd.name);
776 /* If there's no partition info, just package the whole chip
777 * as a single MTD device.
779 if (!info->partitioned)
780 ret = add_mtd_device(&info->mtd) ? -ENODEV : 0;
782 if (ret < 0)
783 goto err_scan;
785 val = davinci_nand_readl(info, NRCSR_OFFSET);
786 dev_info(&pdev->dev, "controller rev. %d.%d\n",
787 (val >> 8) & 0xff, val & 0xff);
789 return 0;
791 err_scan:
792 clk_disable(info->clk);
794 err_clk_enable:
795 clk_put(info->clk);
797 spin_lock_irq(&davinci_nand_lock);
798 if (ecc_mode == NAND_ECC_HW_SYNDROME)
799 ecc4_busy = false;
800 spin_unlock_irq(&davinci_nand_lock);
802 err_ecc:
803 err_clk:
804 err_ioremap:
805 if (base)
806 iounmap(base);
807 if (vaddr)
808 iounmap(vaddr);
810 err_nomem:
811 kfree(info);
812 return ret;
815 static int __exit nand_davinci_remove(struct platform_device *pdev)
817 struct davinci_nand_info *info = platform_get_drvdata(pdev);
818 int status;
820 if (mtd_has_partitions() && info->partitioned)
821 status = del_mtd_partitions(&info->mtd);
822 else
823 status = del_mtd_device(&info->mtd);
825 spin_lock_irq(&davinci_nand_lock);
826 if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME)
827 ecc4_busy = false;
828 spin_unlock_irq(&davinci_nand_lock);
830 iounmap(info->base);
831 iounmap(info->vaddr);
833 nand_release(&info->mtd);
835 clk_disable(info->clk);
836 clk_put(info->clk);
838 kfree(info);
840 return 0;
843 static struct platform_driver nand_davinci_driver = {
844 .remove = __exit_p(nand_davinci_remove),
845 .driver = {
846 .name = "davinci_nand",
849 MODULE_ALIAS("platform:davinci_nand");
851 static int __init nand_davinci_init(void)
853 return platform_driver_probe(&nand_davinci_driver, nand_davinci_probe);
855 module_init(nand_davinci_init);
857 static void __exit nand_davinci_exit(void)
859 platform_driver_unregister(&nand_davinci_driver);
861 module_exit(nand_davinci_exit);
863 MODULE_LICENSE("GPL");
864 MODULE_AUTHOR("Texas Instruments");
865 MODULE_DESCRIPTION("Davinci NAND flash driver");