diff options
-rw-r--r-- | arch/arm/mach-davinci/include/mach/nand.h | 8 | ||||
-rw-r--r-- | drivers/mtd/nand/davinci_nand.c | 304 |
2 files changed, 297 insertions, 15 deletions
diff --git a/arch/arm/mach-davinci/include/mach/nand.h b/arch/arm/mach-davinci/include/mach/nand.h index aa482841270b..b520c4b5678a 100644 --- a/arch/arm/mach-davinci/include/mach/nand.h +++ b/arch/arm/mach-davinci/include/mach/nand.h | |||
@@ -68,10 +68,14 @@ struct davinci_nand_pdata { /* platform_data */ | |||
68 | 68 | ||
69 | /* none == NAND_ECC_NONE (strongly *not* advised!!) | 69 | /* none == NAND_ECC_NONE (strongly *not* advised!!) |
70 | * soft == NAND_ECC_SOFT | 70 | * soft == NAND_ECC_SOFT |
71 | * 1-bit == NAND_ECC_HW | 71 | * else == NAND_ECC_HW, according to ecc_bits |
72 | * 4-bit == NAND_ECC_HW_SYNDROME (not on all chips) | 72 | * |
73 | * All DaVinci-family chips support 1-bit hardware ECC. | ||
74 | * Newer ones also support 4-bit ECC, but are awkward | ||
75 | * using it with large page chips. | ||
73 | */ | 76 | */ |
74 | nand_ecc_modes_t ecc_mode; | 77 | nand_ecc_modes_t ecc_mode; |
78 | u8 ecc_bits; | ||
75 | 79 | ||
76 | /* e.g. NAND_BUSWIDTH_16 or NAND_USE_FLASH_BBT */ | 80 | /* e.g. NAND_BUSWIDTH_16 or NAND_USE_FLASH_BBT */ |
77 | unsigned options; | 81 | unsigned options; |
diff --git a/drivers/mtd/nand/davinci_nand.c b/drivers/mtd/nand/davinci_nand.c index 68b747584bc8..cb5a05e01531 100644 --- a/drivers/mtd/nand/davinci_nand.c +++ b/drivers/mtd/nand/davinci_nand.c | |||
@@ -44,7 +44,7 @@ | |||
44 | * and some flavors of secondary chipselect (e.g. based on A12) as used | 44 | * and some flavors of secondary chipselect (e.g. based on A12) as used |
45 | * with multichip packages. | 45 | * with multichip packages. |
46 | * | 46 | * |
47 | * The 1-bit ECC hardware is supported, but not yet the newer 4-bit ECC | 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 | 48 | * available on chips like the DM355 and OMAP-L137 and needed with the |
49 | * more error-prone MLC NAND chips. | 49 | * more error-prone MLC NAND chips. |
50 | * | 50 | * |
@@ -54,11 +54,14 @@ | |||
54 | struct davinci_nand_info { | 54 | struct davinci_nand_info { |
55 | struct mtd_info mtd; | 55 | struct mtd_info mtd; |
56 | struct nand_chip chip; | 56 | struct nand_chip chip; |
57 | struct nand_ecclayout ecclayout; | ||
57 | 58 | ||
58 | struct device *dev; | 59 | struct device *dev; |
59 | struct clk *clk; | 60 | struct clk *clk; |
60 | bool partitioned; | 61 | bool partitioned; |
61 | 62 | ||
63 | bool is_readmode; | ||
64 | |||
62 | void __iomem *base; | 65 | void __iomem *base; |
63 | void __iomem *vaddr; | 66 | void __iomem *vaddr; |
64 | 67 | ||
@@ -73,6 +76,7 @@ struct davinci_nand_info { | |||
73 | }; | 76 | }; |
74 | 77 | ||
75 | static DEFINE_SPINLOCK(davinci_nand_lock); | 78 | static DEFINE_SPINLOCK(davinci_nand_lock); |
79 | static bool ecc4_busy; | ||
76 | 80 | ||
77 | #define to_davinci_nand(m) container_of(m, struct davinci_nand_info, mtd) | 81 | #define to_davinci_nand(m) container_of(m, struct davinci_nand_info, mtd) |
78 | 82 | ||
@@ -218,6 +222,192 @@ static int nand_davinci_correct_1bit(struct mtd_info *mtd, u_char *dat, | |||
218 | /*----------------------------------------------------------------------*/ | 222 | /*----------------------------------------------------------------------*/ |
219 | 223 | ||
220 | /* | 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 | /* | ||
221 | * NOTE: NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's | 411 | * NOTE: NAND boot requires ALE == EM_A[1], CLE == EM_A[2], so that's |
222 | * how these chips are normally wired. This translates to both 8 and 16 | 412 | * how these chips are normally wired. This translates to both 8 and 16 |
223 | * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4). | 413 | * bit busses using ALE == BIT(3) in byte addresses, and CLE == BIT(4). |
@@ -294,6 +484,23 @@ static void __init nand_dm6446evm_flash_init(struct davinci_nand_info *info) | |||
294 | 484 | ||
295 | /*----------------------------------------------------------------------*/ | 485 | /*----------------------------------------------------------------------*/ |
296 | 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 | |||
297 | static int __init nand_davinci_probe(struct platform_device *pdev) | 504 | static int __init nand_davinci_probe(struct platform_device *pdev) |
298 | { | 505 | { |
299 | struct davinci_nand_pdata *pdata = pdev->dev.platform_data; | 506 | struct davinci_nand_pdata *pdata = pdev->dev.platform_data; |
@@ -378,24 +585,41 @@ static int __init nand_davinci_probe(struct platform_device *pdev) | |||
378 | /* Use board-specific ECC config */ | 585 | /* Use board-specific ECC config */ |
379 | ecc_mode = pdata->ecc_mode; | 586 | ecc_mode = pdata->ecc_mode; |
380 | 587 | ||
588 | ret = -EINVAL; | ||
381 | switch (ecc_mode) { | 589 | switch (ecc_mode) { |
382 | case NAND_ECC_NONE: | 590 | case NAND_ECC_NONE: |
383 | case NAND_ECC_SOFT: | 591 | case NAND_ECC_SOFT: |
592 | pdata->ecc_bits = 0; | ||
384 | break; | 593 | break; |
385 | case NAND_ECC_HW: | 594 | case NAND_ECC_HW: |
386 | info->chip.ecc.calculate = nand_davinci_calculate_1bit; | 595 | if (pdata->ecc_bits == 4) { |
387 | info->chip.ecc.correct = nand_davinci_correct_1bit; | 596 | /* No sanity checks: CPUs must support this, |
388 | info->chip.ecc.hwctl = nand_davinci_hwctl_1bit; | 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 | } | ||
389 | info->chip.ecc.size = 512; | 621 | info->chip.ecc.size = 512; |
390 | info->chip.ecc.bytes = 3; | ||
391 | break; | 622 | break; |
392 | case NAND_ECC_HW_SYNDROME: | ||
393 | /* FIXME implement */ | ||
394 | info->chip.ecc.size = 512; | ||
395 | info->chip.ecc.bytes = 10; | ||
396 | |||
397 | dev_warn(&pdev->dev, "4-bit ECC nyet supported\n"); | ||
398 | /* FALL THROUGH */ | ||
399 | default: | 623 | default: |
400 | ret = -EINVAL; | 624 | ret = -EINVAL; |
401 | goto err_ecc; | 625 | goto err_ecc; |
@@ -435,12 +659,56 @@ static int __init nand_davinci_probe(struct platform_device *pdev) | |||
435 | spin_unlock_irq(&davinci_nand_lock); | 659 | spin_unlock_irq(&davinci_nand_lock); |
436 | 660 | ||
437 | /* Scan to find existence of the device(s) */ | 661 | /* Scan to find existence of the device(s) */ |
438 | ret = nand_scan(&info->mtd, pdata->mask_chipsel ? 2 : 1); | 662 | ret = nand_scan_ident(&info->mtd, pdata->mask_chipsel ? 2 : 1); |
439 | if (ret < 0) { | 663 | if (ret < 0) { |
440 | dev_dbg(&pdev->dev, "no NAND chip(s) found\n"); | 664 | dev_dbg(&pdev->dev, "no NAND chip(s) found\n"); |
441 | goto err_scan; | 665 | goto err_scan; |
442 | } | 666 | } |
443 | 667 | ||
668 | /* Update ECC layout if needed ... for 1-bit HW ECC, the default | ||
669 | * is OK, but it allocates 6 bytes when only 3 are needed (for | ||
670 | * each 512 bytes). For the 4-bit HW ECC, that default is not | ||
671 | * usable: 10 bytes are needed, not 6. | ||
672 | */ | ||
673 | if (pdata->ecc_bits == 4) { | ||
674 | int chunks = info->mtd.writesize / 512; | ||
675 | |||
676 | if (!chunks || info->mtd.oobsize < 16) { | ||
677 | dev_dbg(&pdev->dev, "too small\n"); | ||
678 | ret = -EINVAL; | ||
679 | goto err_scan; | ||
680 | } | ||
681 | |||
682 | /* For small page chips, preserve the manufacturer's | ||
683 | * badblock marking data ... and make sure a flash BBT | ||
684 | * table marker fits in the free bytes. | ||
685 | */ | ||
686 | if (chunks == 1) { | ||
687 | info->ecclayout = hwecc4_small; | ||
688 | info->ecclayout.oobfree[1].length = | ||
689 | info->mtd.oobsize - 16; | ||
690 | goto syndrome_done; | ||
691 | } | ||
692 | |||
693 | /* For large page chips we'll be wanting to use a | ||
694 | * not-yet-implemented mode that reads OOB data | ||
695 | * before reading the body of the page, to avoid | ||
696 | * the "infix OOB" model of NAND_ECC_HW_SYNDROME | ||
697 | * (and preserve manufacturer badblock markings). | ||
698 | */ | ||
699 | dev_warn(&pdev->dev, "no 4-bit ECC support yet " | ||
700 | "for large page NAND\n"); | ||
701 | ret = -EIO; | ||
702 | goto err_scan; | ||
703 | |||
704 | syndrome_done: | ||
705 | info->chip.ecc.layout = &info->ecclayout; | ||
706 | } | ||
707 | |||
708 | ret = nand_scan_tail(&info->mtd); | ||
709 | if (ret < 0) | ||
710 | goto err_scan; | ||
711 | |||
444 | if (mtd_has_partitions()) { | 712 | if (mtd_has_partitions()) { |
445 | struct mtd_partition *mtd_parts = NULL; | 713 | struct mtd_partition *mtd_parts = NULL; |
446 | int mtd_parts_nb = 0; | 714 | int mtd_parts_nb = 0; |
@@ -503,6 +771,11 @@ err_scan: | |||
503 | err_clk_enable: | 771 | err_clk_enable: |
504 | clk_put(info->clk); | 772 | clk_put(info->clk); |
505 | 773 | ||
774 | spin_lock_irq(&davinci_nand_lock); | ||
775 | if (ecc_mode == NAND_ECC_HW_SYNDROME) | ||
776 | ecc4_busy = false; | ||
777 | spin_unlock_irq(&davinci_nand_lock); | ||
778 | |||
506 | err_ecc: | 779 | err_ecc: |
507 | err_clk: | 780 | err_clk: |
508 | err_ioremap: | 781 | err_ioremap: |
@@ -526,6 +799,11 @@ static int __exit nand_davinci_remove(struct platform_device *pdev) | |||
526 | else | 799 | else |
527 | status = del_mtd_device(&info->mtd); | 800 | status = del_mtd_device(&info->mtd); |
528 | 801 | ||
802 | spin_lock_irq(&davinci_nand_lock); | ||
803 | if (info->chip.ecc.mode == NAND_ECC_HW_SYNDROME) | ||
804 | ecc4_busy = false; | ||
805 | spin_unlock_irq(&davinci_nand_lock); | ||
806 | |||
529 | iounmap(info->base); | 807 | iounmap(info->base); |
530 | iounmap(info->vaddr); | 808 | iounmap(info->vaddr); |
531 | 809 | ||