aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/nand
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/nand')
-rw-r--r--drivers/mtd/nand/Kconfig63
-rw-r--r--drivers/mtd/nand/Makefile1
-rw-r--r--drivers/mtd/nand/cafe.c25
-rw-r--r--drivers/mtd/nand/cmx270_nand.c267
-rw-r--r--drivers/mtd/nand/nand_base.c8
-rw-r--r--drivers/mtd/nand/nand_ids.c4
-rw-r--r--drivers/mtd/nand/nandsim.c518
7 files changed, 816 insertions, 70 deletions
diff --git a/drivers/mtd/nand/Kconfig b/drivers/mtd/nand/Kconfig
index 2d12dcdd740c..d05873b8c155 100644
--- a/drivers/mtd/nand/Kconfig
+++ b/drivers/mtd/nand/Kconfig
@@ -1,10 +1,7 @@
1# drivers/mtd/nand/Kconfig 1# drivers/mtd/nand/Kconfig
2# $Id: Kconfig,v 1.35 2005/11/07 11:14:30 gleixner Exp $ 2# $Id: Kconfig,v 1.35 2005/11/07 11:14:30 gleixner Exp $
3 3
4menu "NAND Flash Device Drivers" 4menuconfig MTD_NAND
5 depends on MTD!=n
6
7config MTD_NAND
8 tristate "NAND Device Support" 5 tristate "NAND Device Support"
9 depends on MTD 6 depends on MTD
10 select MTD_NAND_IDS 7 select MTD_NAND_IDS
@@ -13,9 +10,10 @@ config MTD_NAND
13 devices. For further information see 10 devices. For further information see
14 <http://www.linux-mtd.infradead.org/doc/nand.html>. 11 <http://www.linux-mtd.infradead.org/doc/nand.html>.
15 12
13if MTD_NAND
14
16config MTD_NAND_VERIFY_WRITE 15config MTD_NAND_VERIFY_WRITE
17 bool "Verify NAND page writes" 16 bool "Verify NAND page writes"
18 depends on MTD_NAND
19 help 17 help
20 This adds an extra check when data is written to the flash. The 18 This adds an extra check when data is written to the flash. The
21 NAND flash device internally checks only bits transitioning 19 NAND flash device internally checks only bits transitioning
@@ -25,53 +23,61 @@ config MTD_NAND_VERIFY_WRITE
25 23
26config MTD_NAND_ECC_SMC 24config MTD_NAND_ECC_SMC
27 bool "NAND ECC Smart Media byte order" 25 bool "NAND ECC Smart Media byte order"
28 depends on MTD_NAND
29 default n 26 default n
30 help 27 help
31 Software ECC according to the Smart Media Specification. 28 Software ECC according to the Smart Media Specification.
32 The original Linux implementation had byte 0 and 1 swapped. 29 The original Linux implementation had byte 0 and 1 swapped.
33 30
31config MTD_NAND_MUSEUM_IDS
32 bool "Enable chip ids for obsolete ancient NAND devices"
33 depends on MTD_NAND
34 default n
35 help
36 Enable this option only when your board has first generation
37 NAND chips (page size 256 byte, erase size 4-8KiB). The IDs
38 of these chips were reused by later, larger chips.
39
34config MTD_NAND_AUTCPU12 40config MTD_NAND_AUTCPU12
35 tristate "SmartMediaCard on autronix autcpu12 board" 41 tristate "SmartMediaCard on autronix autcpu12 board"
36 depends on MTD_NAND && ARCH_AUTCPU12 42 depends on ARCH_AUTCPU12
37 help 43 help
38 This enables the driver for the autronix autcpu12 board to 44 This enables the driver for the autronix autcpu12 board to
39 access the SmartMediaCard. 45 access the SmartMediaCard.
40 46
41config MTD_NAND_EDB7312 47config MTD_NAND_EDB7312
42 tristate "Support for Cirrus Logic EBD7312 evaluation board" 48 tristate "Support for Cirrus Logic EBD7312 evaluation board"
43 depends on MTD_NAND && ARCH_EDB7312 49 depends on ARCH_EDB7312
44 help 50 help
45 This enables the driver for the Cirrus Logic EBD7312 evaluation 51 This enables the driver for the Cirrus Logic EBD7312 evaluation
46 board to access the onboard NAND Flash. 52 board to access the onboard NAND Flash.
47 53
48config MTD_NAND_H1900 54config MTD_NAND_H1900
49 tristate "iPAQ H1900 flash" 55 tristate "iPAQ H1900 flash"
50 depends on MTD_NAND && ARCH_PXA && MTD_PARTITIONS 56 depends on ARCH_PXA && MTD_PARTITIONS
51 help 57 help
52 This enables the driver for the iPAQ h1900 flash. 58 This enables the driver for the iPAQ h1900 flash.
53 59
54config MTD_NAND_SPIA 60config MTD_NAND_SPIA
55 tristate "NAND Flash device on SPIA board" 61 tristate "NAND Flash device on SPIA board"
56 depends on ARCH_P720T && MTD_NAND 62 depends on ARCH_P720T
57 help 63 help
58 If you had to ask, you don't have one. Say 'N'. 64 If you had to ask, you don't have one. Say 'N'.
59 65
60config MTD_NAND_AMS_DELTA 66config MTD_NAND_AMS_DELTA
61 tristate "NAND Flash device on Amstrad E3" 67 tristate "NAND Flash device on Amstrad E3"
62 depends on MACH_AMS_DELTA && MTD_NAND 68 depends on MACH_AMS_DELTA
63 help 69 help
64 Support for NAND flash on Amstrad E3 (Delta). 70 Support for NAND flash on Amstrad E3 (Delta).
65 71
66config MTD_NAND_TOTO 72config MTD_NAND_TOTO
67 tristate "NAND Flash device on TOTO board" 73 tristate "NAND Flash device on TOTO board"
68 depends on ARCH_OMAP && MTD_NAND && BROKEN 74 depends on ARCH_OMAP && BROKEN
69 help 75 help
70 Support for NAND flash on Texas Instruments Toto platform. 76 Support for NAND flash on Texas Instruments Toto platform.
71 77
72config MTD_NAND_TS7250 78config MTD_NAND_TS7250
73 tristate "NAND Flash device on TS-7250 board" 79 tristate "NAND Flash device on TS-7250 board"
74 depends on MACH_TS72XX && MTD_NAND 80 depends on MACH_TS72XX
75 help 81 help
76 Support for NAND flash on Technologic Systems TS-7250 platform. 82 Support for NAND flash on Technologic Systems TS-7250 platform.
77 83
@@ -80,14 +86,14 @@ config MTD_NAND_IDS
80 86
81config MTD_NAND_AU1550 87config MTD_NAND_AU1550
82 tristate "Au1550/1200 NAND support" 88 tristate "Au1550/1200 NAND support"
83 depends on (SOC_AU1200 || SOC_AU1550) && MTD_NAND 89 depends on SOC_AU1200 || SOC_AU1550
84 help 90 help
85 This enables the driver for the NAND flash controller on the 91 This enables the driver for the NAND flash controller on the
86 AMD/Alchemy 1550 SOC. 92 AMD/Alchemy 1550 SOC.
87 93
88config MTD_NAND_RTC_FROM4 94config MTD_NAND_RTC_FROM4
89 tristate "Renesas Flash ROM 4-slot interface board (FROM_BOARD4)" 95 tristate "Renesas Flash ROM 4-slot interface board (FROM_BOARD4)"
90 depends on MTD_NAND && SH_SOLUTION_ENGINE 96 depends on SH_SOLUTION_ENGINE
91 select REED_SOLOMON 97 select REED_SOLOMON
92 select REED_SOLOMON_DEC8 98 select REED_SOLOMON_DEC8
93 select BITREVERSE 99 select BITREVERSE
@@ -97,13 +103,13 @@ config MTD_NAND_RTC_FROM4
97 103
98config MTD_NAND_PPCHAMELEONEVB 104config MTD_NAND_PPCHAMELEONEVB
99 tristate "NAND Flash device on PPChameleonEVB board" 105 tristate "NAND Flash device on PPChameleonEVB board"
100 depends on PPCHAMELEONEVB && MTD_NAND && BROKEN 106 depends on PPCHAMELEONEVB && BROKEN
101 help 107 help
102 This enables the NAND flash driver on the PPChameleon EVB Board. 108 This enables the NAND flash driver on the PPChameleon EVB Board.
103 109
104config MTD_NAND_S3C2410 110config MTD_NAND_S3C2410
105 tristate "NAND Flash support for S3C2410/S3C2440 SoC" 111 tristate "NAND Flash support for S3C2410/S3C2440 SoC"
106 depends on ARCH_S3C2410 && MTD_NAND 112 depends on ARCH_S3C2410
107 help 113 help
108 This enables the NAND flash controller on the S3C2410 and S3C2440 114 This enables the NAND flash controller on the S3C2410 and S3C2440
109 SoCs 115 SoCs
@@ -128,7 +134,7 @@ config MTD_NAND_S3C2410_HWECC
128 134
129config MTD_NAND_NDFC 135config MTD_NAND_NDFC
130 tristate "NDFC NanD Flash Controller" 136 tristate "NDFC NanD Flash Controller"
131 depends on MTD_NAND && 44x 137 depends on 44x
132 select MTD_NAND_ECC_SMC 138 select MTD_NAND_ECC_SMC
133 help 139 help
134 NDFC Nand Flash Controllers are integrated in EP44x SoCs 140 NDFC Nand Flash Controllers are integrated in EP44x SoCs
@@ -145,7 +151,7 @@ config MTD_NAND_S3C2410_CLKSTOP
145 151
146config MTD_NAND_DISKONCHIP 152config MTD_NAND_DISKONCHIP
147 tristate "DiskOnChip 2000, Millennium and Millennium Plus (NAND reimplementation) (EXPERIMENTAL)" 153 tristate "DiskOnChip 2000, Millennium and Millennium Plus (NAND reimplementation) (EXPERIMENTAL)"
148 depends on MTD_NAND && EXPERIMENTAL 154 depends on EXPERIMENTAL
149 select REED_SOLOMON 155 select REED_SOLOMON
150 select REED_SOLOMON_DEC16 156 select REED_SOLOMON_DEC16
151 help 157 help
@@ -215,11 +221,11 @@ config MTD_NAND_DISKONCHIP_BBTWRITE
215 221
216config MTD_NAND_SHARPSL 222config MTD_NAND_SHARPSL
217 tristate "Support for NAND Flash on Sharp SL Series (C7xx + others)" 223 tristate "Support for NAND Flash on Sharp SL Series (C7xx + others)"
218 depends on MTD_NAND && ARCH_PXA 224 depends on ARCH_PXA
219 225
220config MTD_NAND_BASLER_EXCITE 226config MTD_NAND_BASLER_EXCITE
221 tristate "Support for NAND Flash on Basler eXcite" 227 tristate "Support for NAND Flash on Basler eXcite"
222 depends on MTD_NAND && BASLER_EXCITE 228 depends on BASLER_EXCITE
223 help 229 help
224 This enables the driver for the NAND flash device found on the 230 This enables the driver for the NAND flash device found on the
225 Basler eXcite Smart Camera. If built as a module, the driver 231 Basler eXcite Smart Camera. If built as a module, the driver
@@ -227,14 +233,14 @@ config MTD_NAND_BASLER_EXCITE
227 233
228config MTD_NAND_CAFE 234config MTD_NAND_CAFE
229 tristate "NAND support for OLPC CAFÉ chip" 235 tristate "NAND support for OLPC CAFÉ chip"
230 depends on MTD_NAND && PCI 236 depends on PCI
231 help 237 help
232 Use NAND flash attached to the CAFÉ chip designed for the $100 238 Use NAND flash attached to the CAFÉ chip designed for the $100
233 laptop. 239 laptop.
234 240
235config MTD_NAND_CS553X 241config MTD_NAND_CS553X
236 tristate "NAND support for CS5535/CS5536 (AMD Geode companion chip)" 242 tristate "NAND support for CS5535/CS5536 (AMD Geode companion chip)"
237 depends on MTD_NAND && X86_32 && (X86_PC || X86_GENERICARCH) 243 depends on X86_32 && (X86_PC || X86_GENERICARCH)
238 help 244 help
239 The CS553x companion chips for the AMD Geode processor 245 The CS553x companion chips for the AMD Geode processor
240 include NAND flash controllers with built-in hardware ECC 246 include NAND flash controllers with built-in hardware ECC
@@ -247,16 +253,21 @@ config MTD_NAND_CS553X
247 253
248config MTD_NAND_AT91 254config MTD_NAND_AT91
249 bool "Support for NAND Flash / SmartMedia on AT91" 255 bool "Support for NAND Flash / SmartMedia on AT91"
250 depends on MTD_NAND && ARCH_AT91 256 depends on ARCH_AT91
251 help 257 help
252 Enables support for NAND Flash / Smart Media Card interface 258 Enables support for NAND Flash / Smart Media Card interface
253 on Atmel AT91 processors. 259 on Atmel AT91 processors.
254 260
261config MTD_NAND_CM_X270
262 tristate "Support for NAND Flash on CM-X270 modules"
263 depends on MTD_NAND && MACH_ARMCORE
264
265
255config MTD_NAND_NANDSIM 266config MTD_NAND_NANDSIM
256 tristate "Support for NAND Flash Simulator" 267 tristate "Support for NAND Flash Simulator"
257 depends on MTD_NAND && MTD_PARTITIONS 268 depends on MTD_PARTITIONS
258 help 269 help
259 The simulator may simulate various NAND flash chips for the 270 The simulator may simulate various NAND flash chips for the
260 MTD nand layer. 271 MTD nand layer.
261 272
262endmenu 273endif # MTD_NAND
diff --git a/drivers/mtd/nand/Makefile b/drivers/mtd/nand/Makefile
index 80f1dfc77949..6872031a3fb2 100644
--- a/drivers/mtd/nand/Makefile
+++ b/drivers/mtd/nand/Makefile
@@ -24,6 +24,7 @@ obj-$(CONFIG_MTD_NAND_NANDSIM) += nandsim.o
24obj-$(CONFIG_MTD_NAND_CS553X) += cs553x_nand.o 24obj-$(CONFIG_MTD_NAND_CS553X) += cs553x_nand.o
25obj-$(CONFIG_MTD_NAND_NDFC) += ndfc.o 25obj-$(CONFIG_MTD_NAND_NDFC) += ndfc.o
26obj-$(CONFIG_MTD_NAND_AT91) += at91_nand.o 26obj-$(CONFIG_MTD_NAND_AT91) += at91_nand.o
27obj-$(CONFIG_MTD_NAND_CM_X270) += cmx270_nand.o
27obj-$(CONFIG_MTD_NAND_BASLER_EXCITE) += excite_nandflash.o 28obj-$(CONFIG_MTD_NAND_BASLER_EXCITE) += excite_nandflash.o
28 29
29nand-objs := nand_base.o nand_bbt.o 30nand-objs := nand_base.o nand_bbt.o
diff --git a/drivers/mtd/nand/cafe.c b/drivers/mtd/nand/cafe.c
index fd6bb3ed40df..c328a7514510 100644
--- a/drivers/mtd/nand/cafe.c
+++ b/drivers/mtd/nand/cafe.c
@@ -530,7 +530,6 @@ static int __devinit cafe_nand_probe(struct pci_dev *pdev,
530{ 530{
531 struct mtd_info *mtd; 531 struct mtd_info *mtd;
532 struct cafe_priv *cafe; 532 struct cafe_priv *cafe;
533 uint32_t timing1, timing2, timing3;
534 uint32_t ctrl; 533 uint32_t ctrl;
535 int err = 0; 534 int err = 0;
536 535
@@ -587,21 +586,19 @@ static int __devinit cafe_nand_probe(struct pci_dev *pdev,
587 } 586 }
588 587
589 if (numtimings == 3) { 588 if (numtimings == 3) {
590 timing1 = timing[0];
591 timing2 = timing[1];
592 timing3 = timing[2];
593 cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n", 589 cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
594 timing1, timing2, timing3); 590 timing[0], timing[1], timing[2]);
595 } else { 591 } else {
596 timing1 = cafe_readl(cafe, NAND_TIMING1); 592 timing[0] = cafe_readl(cafe, NAND_TIMING1);
597 timing2 = cafe_readl(cafe, NAND_TIMING2); 593 timing[1] = cafe_readl(cafe, NAND_TIMING2);
598 timing3 = cafe_readl(cafe, NAND_TIMING3); 594 timing[2] = cafe_readl(cafe, NAND_TIMING3);
599 595
600 if (timing1 | timing2 | timing3) { 596 if (timing[0] | timing[1] | timing[2]) {
601 cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n", timing1, timing2, timing3); 597 cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n",
598 timing[0], timing[1], timing[2]);
602 } else { 599 } else {
603 dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n"); 600 dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
604 timing1 = timing2 = timing3 = 0xffffffff; 601 timing[0] = timing[1] = timing[2] = 0xffffffff;
605 } 602 }
606 } 603 }
607 604
@@ -609,9 +606,9 @@ static int __devinit cafe_nand_probe(struct pci_dev *pdev,
609 cafe_writel(cafe, 1, NAND_RESET); 606 cafe_writel(cafe, 1, NAND_RESET);
610 cafe_writel(cafe, 0, NAND_RESET); 607 cafe_writel(cafe, 0, NAND_RESET);
611 608
612 cafe_writel(cafe, timing1, NAND_TIMING1); 609 cafe_writel(cafe, timing[0], NAND_TIMING1);
613 cafe_writel(cafe, timing2, NAND_TIMING2); 610 cafe_writel(cafe, timing[1], NAND_TIMING2);
614 cafe_writel(cafe, timing3, NAND_TIMING3); 611 cafe_writel(cafe, timing[2], NAND_TIMING3);
615 612
616 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK); 613 cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
617 err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED, 614 err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED,
diff --git a/drivers/mtd/nand/cmx270_nand.c b/drivers/mtd/nand/cmx270_nand.c
new file mode 100644
index 000000000000..cb663ef245d5
--- /dev/null
+++ b/drivers/mtd/nand/cmx270_nand.c
@@ -0,0 +1,267 @@
1/*
2 * linux/drivers/mtd/nand/cmx270-nand.c
3 *
4 * Copyright (C) 2006 Compulab, Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
6 *
7 * Derived from drivers/mtd/nand/h1910.c
8 * Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
9 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 * Overview:
17 * This is a device driver for the NAND flash device found on the
18 * CM-X270 board.
19 */
20
21#include <linux/mtd/nand.h>
22#include <linux/mtd/partitions.h>
23
24#include <asm/io.h>
25#include <asm/irq.h>
26
27#include <asm/arch/hardware.h>
28#include <asm/arch/pxa-regs.h>
29
30#define GPIO_NAND_CS (11)
31#define GPIO_NAND_RB (89)
32
33/* This macro needed to ensure in-order operation of GPIO and local
34 * bus. Without both asm command and dummy uncached read there're
35 * states when NAND access is broken. I've looked for such macro(s) in
36 * include/asm-arm but found nothing approptiate.
37 * dmac_clean_range is close, but is makes cache invalidation
38 * unnecessary here and it cannot be used in module
39 */
40#define DRAIN_WB() \
41 do { \
42 unsigned char dummy; \
43 asm volatile ("mcr p15, 0, r0, c7, c10, 4":::"r0"); \
44 dummy=*((unsigned char*)UNCACHED_ADDR); \
45 } while(0)
46
47/* MTD structure for CM-X270 board */
48static struct mtd_info *cmx270_nand_mtd;
49
50/* remaped IO address of the device */
51static void __iomem *cmx270_nand_io;
52
53/*
54 * Define static partitions for flash device
55 */
56static struct mtd_partition partition_info[] = {
57 [0] = {
58 .name = "cmx270-0",
59 .offset = 0,
60 .size = MTDPART_SIZ_FULL
61 }
62};
63#define NUM_PARTITIONS (ARRAY_SIZE(partition_info))
64
65const char *part_probes[] = { "cmdlinepart", NULL };
66
67static u_char cmx270_read_byte(struct mtd_info *mtd)
68{
69 struct nand_chip *this = mtd->priv;
70
71 return (readl(this->IO_ADDR_R) >> 16);
72}
73
74static void cmx270_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
75{
76 int i;
77 struct nand_chip *this = mtd->priv;
78
79 for (i=0; i<len; i++)
80 writel((*buf++ << 16), this->IO_ADDR_W);
81}
82
83static void cmx270_read_buf(struct mtd_info *mtd, u_char *buf, int len)
84{
85 int i;
86 struct nand_chip *this = mtd->priv;
87
88 for (i=0; i<len; i++)
89 *buf++ = readl(this->IO_ADDR_R) >> 16;
90}
91
92static int cmx270_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
93{
94 int i;
95 struct nand_chip *this = mtd->priv;
96
97 for (i=0; i<len; i++)
98 if (buf[i] != (u_char)(readl(this->IO_ADDR_R) >> 16))
99 return -EFAULT;
100
101 return 0;
102}
103
104static inline void nand_cs_on(void)
105{
106 GPCR(GPIO_NAND_CS) = GPIO_bit(GPIO_NAND_CS);
107}
108
109static void nand_cs_off(void)
110{
111 DRAIN_WB();
112
113 GPSR(GPIO_NAND_CS) = GPIO_bit(GPIO_NAND_CS);
114}
115
116/*
117 * hardware specific access to control-lines
118 */
119static void cmx270_hwcontrol(struct mtd_info *mtd, int dat,
120 unsigned int ctrl)
121{
122 struct nand_chip* this = mtd->priv;
123 unsigned int nandaddr = (unsigned int)this->IO_ADDR_W;
124
125 DRAIN_WB();
126
127 if (ctrl & NAND_CTRL_CHANGE) {
128 if ( ctrl & NAND_ALE )
129 nandaddr |= (1 << 3);
130 else
131 nandaddr &= ~(1 << 3);
132 if ( ctrl & NAND_CLE )
133 nandaddr |= (1 << 2);
134 else
135 nandaddr &= ~(1 << 2);
136 if ( ctrl & NAND_NCE )
137 nand_cs_on();
138 else
139 nand_cs_off();
140 }
141
142 DRAIN_WB();
143 this->IO_ADDR_W = (void __iomem*)nandaddr;
144 if (dat != NAND_CMD_NONE)
145 writel((dat << 16), this->IO_ADDR_W);
146
147 DRAIN_WB();
148}
149
150/*
151 * read device ready pin
152 */
153static int cmx270_device_ready(struct mtd_info *mtd)
154{
155 DRAIN_WB();
156
157 return (GPLR(GPIO_NAND_RB) & GPIO_bit(GPIO_NAND_RB));
158}
159
160/*
161 * Main initialization routine
162 */
163static int cmx270_init(void)
164{
165 struct nand_chip *this;
166 const char *part_type;
167 struct mtd_partition *mtd_parts;
168 int mtd_parts_nb = 0;
169 int ret;
170
171 /* Allocate memory for MTD device structure and private data */
172 cmx270_nand_mtd = kzalloc(sizeof(struct mtd_info) +
173 sizeof(struct nand_chip),
174 GFP_KERNEL);
175 if (!cmx270_nand_mtd) {
176 printk("Unable to allocate CM-X270 NAND MTD device structure.\n");
177 return -ENOMEM;
178 }
179
180 cmx270_nand_io = ioremap(PXA_CS1_PHYS, 12);
181 if (!cmx270_nand_io) {
182 printk("Unable to ioremap NAND device\n");
183 ret = -EINVAL;
184 goto err1;
185 }
186
187 /* Get pointer to private data */
188 this = (struct nand_chip *)(&cmx270_nand_mtd[1]);
189
190 /* Link the private data with the MTD structure */
191 cmx270_nand_mtd->owner = THIS_MODULE;
192 cmx270_nand_mtd->priv = this;
193
194 /* insert callbacks */
195 this->IO_ADDR_R = cmx270_nand_io;
196 this->IO_ADDR_W = cmx270_nand_io;
197 this->cmd_ctrl = cmx270_hwcontrol;
198 this->dev_ready = cmx270_device_ready;
199
200 /* 15 us command delay time */
201 this->chip_delay = 20;
202 this->ecc.mode = NAND_ECC_SOFT;
203
204 /* read/write functions */
205 this->read_byte = cmx270_read_byte;
206 this->read_buf = cmx270_read_buf;
207 this->write_buf = cmx270_write_buf;
208 this->verify_buf = cmx270_verify_buf;
209
210 /* Scan to find existence of the device */
211 if (nand_scan (cmx270_nand_mtd, 1)) {
212 printk(KERN_NOTICE "No NAND device\n");
213 ret = -ENXIO;
214 goto err2;
215 }
216
217#ifdef CONFIG_MTD_CMDLINE_PARTS
218 mtd_parts_nb = parse_mtd_partitions(cmx270_nand_mtd, part_probes,
219 &mtd_parts, 0);
220 if (mtd_parts_nb > 0)
221 part_type = "command line";
222 else
223 mtd_parts_nb = 0;
224#endif
225 if (!mtd_parts_nb) {
226 mtd_parts = partition_info;
227 mtd_parts_nb = NUM_PARTITIONS;
228 part_type = "static";
229 }
230
231 /* Register the partitions */
232 printk(KERN_NOTICE "Using %s partition definition\n", part_type);
233 ret = add_mtd_partitions(cmx270_nand_mtd, mtd_parts, mtd_parts_nb);
234 if (ret)
235 goto err2;
236
237 /* Return happy */
238 return 0;
239
240err2:
241 iounmap(cmx270_nand_io);
242err1:
243 kfree(cmx270_nand_mtd);
244
245 return ret;
246
247}
248module_init(cmx270_init);
249
250/*
251 * Clean up routine
252 */
253static void cmx270_cleanup(void)
254{
255 /* Release resources, unregister device */
256 nand_release(cmx270_nand_mtd);
257
258 iounmap(cmx270_nand_io);
259
260 /* Free the MTD device structure */
261 kfree (cmx270_nand_mtd);
262}
263module_exit(cmx270_cleanup);
264
265MODULE_LICENSE("GPL");
266MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
267MODULE_DESCRIPTION("NAND flash driver for Compulab CM-X270 Module");
diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index 6af37b8cff65..04de315e4937 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -312,7 +312,7 @@ static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
312 /* Select the NAND device */ 312 /* Select the NAND device */
313 chip->select_chip(mtd, chipnr); 313 chip->select_chip(mtd, chipnr);
314 } else 314 } else
315 page = (int)ofs; 315 page = (int)(ofs >> chip->page_shift);
316 316
317 if (chip->options & NAND_BUSWIDTH_16) { 317 if (chip->options & NAND_BUSWIDTH_16) {
318 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE, 318 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
@@ -350,7 +350,7 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
350 int block, ret; 350 int block, ret;
351 351
352 /* Get block number */ 352 /* Get block number */
353 block = ((int)ofs) >> chip->bbt_erase_shift; 353 block = (int)(ofs >> chip->bbt_erase_shift);
354 if (chip->bbt) 354 if (chip->bbt)
355 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1); 355 chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
356 356
@@ -771,7 +771,7 @@ static int nand_read_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
771 uint8_t *ecc_code = chip->buffers->ecccode; 771 uint8_t *ecc_code = chip->buffers->ecccode;
772 int *eccpos = chip->ecc.layout->eccpos; 772 int *eccpos = chip->ecc.layout->eccpos;
773 773
774 nand_read_page_raw(mtd, chip, buf); 774 chip->ecc.read_page_raw(mtd, chip, buf);
775 775
776 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) 776 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
777 chip->ecc.calculate(mtd, p, &ecc_calc[i]); 777 chip->ecc.calculate(mtd, p, &ecc_calc[i]);
@@ -1426,7 +1426,7 @@ static void nand_write_page_swecc(struct mtd_info *mtd, struct nand_chip *chip,
1426 for (i = 0; i < chip->ecc.total; i++) 1426 for (i = 0; i < chip->ecc.total; i++)
1427 chip->oob_poi[eccpos[i]] = ecc_calc[i]; 1427 chip->oob_poi[eccpos[i]] = ecc_calc[i];
1428 1428
1429 nand_write_page_raw(mtd, chip, buf); 1429 chip->ecc.write_page_raw(mtd, chip, buf);
1430} 1430}
1431 1431
1432/** 1432/**
diff --git a/drivers/mtd/nand/nand_ids.c b/drivers/mtd/nand/nand_ids.c
index 2e2cdf2fc91d..2fc674a190cf 100644
--- a/drivers/mtd/nand/nand_ids.c
+++ b/drivers/mtd/nand/nand_ids.c
@@ -24,6 +24,8 @@
24* 512 512 Byte page size 24* 512 512 Byte page size
25*/ 25*/
26struct nand_flash_dev nand_flash_ids[] = { 26struct nand_flash_dev nand_flash_ids[] = {
27
28#ifdef CONFIG_MTD_NAND_MUSEUM_IDS
27 {"NAND 1MiB 5V 8-bit", 0x6e, 256, 1, 0x1000, 0}, 29 {"NAND 1MiB 5V 8-bit", 0x6e, 256, 1, 0x1000, 0},
28 {"NAND 2MiB 5V 8-bit", 0x64, 256, 2, 0x1000, 0}, 30 {"NAND 2MiB 5V 8-bit", 0x64, 256, 2, 0x1000, 0},
29 {"NAND 4MiB 5V 8-bit", 0x6b, 512, 4, 0x2000, 0}, 31 {"NAND 4MiB 5V 8-bit", 0x6b, 512, 4, 0x2000, 0},
@@ -39,6 +41,7 @@ struct nand_flash_dev nand_flash_ids[] = {
39 {"NAND 8MiB 3,3V 8-bit", 0xe6, 512, 8, 0x2000, 0}, 41 {"NAND 8MiB 3,3V 8-bit", 0xe6, 512, 8, 0x2000, 0},
40 {"NAND 8MiB 1,8V 16-bit", 0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16}, 42 {"NAND 8MiB 1,8V 16-bit", 0x49, 512, 8, 0x2000, NAND_BUSWIDTH_16},
41 {"NAND 8MiB 3,3V 16-bit", 0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16}, 43 {"NAND 8MiB 3,3V 16-bit", 0x59, 512, 8, 0x2000, NAND_BUSWIDTH_16},
44#endif
42 45
43 {"NAND 16MiB 1,8V 8-bit", 0x33, 512, 16, 0x4000, 0}, 46 {"NAND 16MiB 1,8V 8-bit", 0x33, 512, 16, 0x4000, 0},
44 {"NAND 16MiB 3,3V 8-bit", 0x73, 512, 16, 0x4000, 0}, 47 {"NAND 16MiB 3,3V 8-bit", 0x73, 512, 16, 0x4000, 0},
@@ -137,6 +140,7 @@ struct nand_manufacturers nand_manuf_ids[] = {
137 {NAND_MFR_RENESAS, "Renesas"}, 140 {NAND_MFR_RENESAS, "Renesas"},
138 {NAND_MFR_STMICRO, "ST Micro"}, 141 {NAND_MFR_STMICRO, "ST Micro"},
139 {NAND_MFR_HYNIX, "Hynix"}, 142 {NAND_MFR_HYNIX, "Hynix"},
143 {NAND_MFR_MICRON, "Micron"},
140 {0x0, "Unknown"} 144 {0x0, "Unknown"}
141}; 145};
142 146
diff --git a/drivers/mtd/nand/nandsim.c b/drivers/mtd/nand/nandsim.c
index c3bca9590ad2..205df0f771fe 100644
--- a/drivers/mtd/nand/nandsim.c
+++ b/drivers/mtd/nand/nandsim.c
@@ -37,6 +37,8 @@
37#include <linux/mtd/nand.h> 37#include <linux/mtd/nand.h>
38#include <linux/mtd/partitions.h> 38#include <linux/mtd/partitions.h>
39#include <linux/delay.h> 39#include <linux/delay.h>
40#include <linux/list.h>
41#include <linux/random.h>
40 42
41/* Default simulator parameters values */ 43/* Default simulator parameters values */
42#if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \ 44#if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
@@ -90,6 +92,15 @@ static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
90static uint do_delays = CONFIG_NANDSIM_DO_DELAYS; 92static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
91static uint log = CONFIG_NANDSIM_LOG; 93static uint log = CONFIG_NANDSIM_LOG;
92static uint dbg = CONFIG_NANDSIM_DBG; 94static uint dbg = CONFIG_NANDSIM_DBG;
95static unsigned long parts[MAX_MTD_DEVICES];
96static unsigned int parts_num;
97static char *badblocks = NULL;
98static char *weakblocks = NULL;
99static char *weakpages = NULL;
100static unsigned int bitflips = 0;
101static char *gravepages = NULL;
102static unsigned int rptwear = 0;
103static unsigned int overridesize = 0;
93 104
94module_param(first_id_byte, uint, 0400); 105module_param(first_id_byte, uint, 0400);
95module_param(second_id_byte, uint, 0400); 106module_param(second_id_byte, uint, 0400);
@@ -104,8 +115,16 @@ module_param(bus_width, uint, 0400);
104module_param(do_delays, uint, 0400); 115module_param(do_delays, uint, 0400);
105module_param(log, uint, 0400); 116module_param(log, uint, 0400);
106module_param(dbg, uint, 0400); 117module_param(dbg, uint, 0400);
107 118module_param_array(parts, ulong, &parts_num, 0400);
108MODULE_PARM_DESC(first_id_byte, "The fist byte returned by NAND Flash 'read ID' command (manufaturer ID)"); 119module_param(badblocks, charp, 0400);
120module_param(weakblocks, charp, 0400);
121module_param(weakpages, charp, 0400);
122module_param(bitflips, uint, 0400);
123module_param(gravepages, charp, 0400);
124module_param(rptwear, uint, 0400);
125module_param(overridesize, uint, 0400);
126
127MODULE_PARM_DESC(first_id_byte, "The first byte returned by NAND Flash 'read ID' command (manufacturer ID)");
109MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID)"); 128MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID)");
110MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command"); 129MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command");
111MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command"); 130MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command");
@@ -118,6 +137,23 @@ MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
118MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero"); 137MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
119MODULE_PARM_DESC(log, "Perform logging if not zero"); 138MODULE_PARM_DESC(log, "Perform logging if not zero");
120MODULE_PARM_DESC(dbg, "Output debug information if not zero"); 139MODULE_PARM_DESC(dbg, "Output debug information if not zero");
140MODULE_PARM_DESC(parts, "Partition sizes (in erase blocks) separated by commas");
141/* Page and erase block positions for the following parameters are independent of any partitions */
142MODULE_PARM_DESC(badblocks, "Erase blocks that are initially marked bad, separated by commas");
143MODULE_PARM_DESC(weakblocks, "Weak erase blocks [: remaining erase cycles (defaults to 3)]"
144 " separated by commas e.g. 113:2 means eb 113"
145 " can be erased only twice before failing");
146MODULE_PARM_DESC(weakpages, "Weak pages [: maximum writes (defaults to 3)]"
147 " separated by commas e.g. 1401:2 means page 1401"
148 " can be written only twice before failing");
149MODULE_PARM_DESC(bitflips, "Maximum number of random bit flips per page (zero by default)");
150MODULE_PARM_DESC(gravepages, "Pages that lose data [: maximum reads (defaults to 3)]"
151 " separated by commas e.g. 1401:2 means page 1401"
152 " can be read only twice before failing");
153MODULE_PARM_DESC(rptwear, "Number of erases inbetween reporting wear, if not zero");
154MODULE_PARM_DESC(overridesize, "Specifies the NAND Flash size overriding the ID bytes. "
155 "The size is specified in erase blocks and as the exponent of a power of two"
156 " e.g. 5 means a size of 32 erase blocks");
121 157
122/* The largest possible page size */ 158/* The largest possible page size */
123#define NS_LARGEST_PAGE_SIZE 2048 159#define NS_LARGEST_PAGE_SIZE 2048
@@ -131,9 +167,11 @@ MODULE_PARM_DESC(dbg, "Output debug information if not zero");
131#define NS_DBG(args...) \ 167#define NS_DBG(args...) \
132 do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0) 168 do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0)
133#define NS_WARN(args...) \ 169#define NS_WARN(args...) \
134 do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warnig: " args); } while(0) 170 do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warning: " args); } while(0)
135#define NS_ERR(args...) \ 171#define NS_ERR(args...) \
136 do { printk(KERN_ERR NS_OUTPUT_PREFIX " errorr: " args); } while(0) 172 do { printk(KERN_ERR NS_OUTPUT_PREFIX " error: " args); } while(0)
173#define NS_INFO(args...) \
174 do { printk(KERN_INFO NS_OUTPUT_PREFIX " " args); } while(0)
137 175
138/* Busy-wait delay macros (microseconds, milliseconds) */ 176/* Busy-wait delay macros (microseconds, milliseconds) */
139#define NS_UDELAY(us) \ 177#define NS_UDELAY(us) \
@@ -238,7 +276,8 @@ union ns_mem {
238 * The structure which describes all the internal simulator data. 276 * The structure which describes all the internal simulator data.
239 */ 277 */
240struct nandsim { 278struct nandsim {
241 struct mtd_partition part; 279 struct mtd_partition partitions[MAX_MTD_DEVICES];
280 unsigned int nbparts;
242 281
243 uint busw; /* flash chip bus width (8 or 16) */ 282 uint busw; /* flash chip bus width (8 or 16) */
244 u_char ids[4]; /* chip's ID bytes */ 283 u_char ids[4]; /* chip's ID bytes */
@@ -338,6 +377,38 @@ static struct nandsim_operations {
338 STATE_DATAOUT, STATE_READY}} 377 STATE_DATAOUT, STATE_READY}}
339}; 378};
340 379
380struct weak_block {
381 struct list_head list;
382 unsigned int erase_block_no;
383 unsigned int max_erases;
384 unsigned int erases_done;
385};
386
387static LIST_HEAD(weak_blocks);
388
389struct weak_page {
390 struct list_head list;
391 unsigned int page_no;
392 unsigned int max_writes;
393 unsigned int writes_done;
394};
395
396static LIST_HEAD(weak_pages);
397
398struct grave_page {
399 struct list_head list;
400 unsigned int page_no;
401 unsigned int max_reads;
402 unsigned int reads_done;
403};
404
405static LIST_HEAD(grave_pages);
406
407static unsigned long *erase_block_wear = NULL;
408static unsigned int wear_eb_count = 0;
409static unsigned long total_wear = 0;
410static unsigned int rptwear_cnt = 0;
411
341/* MTD structure for NAND controller */ 412/* MTD structure for NAND controller */
342static struct mtd_info *nsmtd; 413static struct mtd_info *nsmtd;
343 414
@@ -381,6 +452,13 @@ static void free_device(struct nandsim *ns)
381 } 452 }
382} 453}
383 454
455static char *get_partition_name(int i)
456{
457 char buf[64];
458 sprintf(buf, "NAND simulator partition %d", i);
459 return kstrdup(buf, GFP_KERNEL);
460}
461
384/* 462/*
385 * Initialize the nandsim structure. 463 * Initialize the nandsim structure.
386 * 464 *
@@ -390,7 +468,9 @@ static int init_nandsim(struct mtd_info *mtd)
390{ 468{
391 struct nand_chip *chip = (struct nand_chip *)mtd->priv; 469 struct nand_chip *chip = (struct nand_chip *)mtd->priv;
392 struct nandsim *ns = (struct nandsim *)(chip->priv); 470 struct nandsim *ns = (struct nandsim *)(chip->priv);
393 int i; 471 int i, ret = 0;
472 u_int32_t remains;
473 u_int32_t next_offset;
394 474
395 if (NS_IS_INITIALIZED(ns)) { 475 if (NS_IS_INITIALIZED(ns)) {
396 NS_ERR("init_nandsim: nandsim is already initialized\n"); 476 NS_ERR("init_nandsim: nandsim is already initialized\n");
@@ -448,6 +528,40 @@ static int init_nandsim(struct mtd_info *mtd)
448 } 528 }
449 } 529 }
450 530
531 /* Fill the partition_info structure */
532 if (parts_num > ARRAY_SIZE(ns->partitions)) {
533 NS_ERR("too many partitions.\n");
534 ret = -EINVAL;
535 goto error;
536 }
537 remains = ns->geom.totsz;
538 next_offset = 0;
539 for (i = 0; i < parts_num; ++i) {
540 unsigned long part = parts[i];
541 if (!part || part > remains / ns->geom.secsz) {
542 NS_ERR("bad partition size.\n");
543 ret = -EINVAL;
544 goto error;
545 }
546 ns->partitions[i].name = get_partition_name(i);
547 ns->partitions[i].offset = next_offset;
548 ns->partitions[i].size = part * ns->geom.secsz;
549 next_offset += ns->partitions[i].size;
550 remains -= ns->partitions[i].size;
551 }
552 ns->nbparts = parts_num;
553 if (remains) {
554 if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
555 NS_ERR("too many partitions.\n");
556 ret = -EINVAL;
557 goto error;
558 }
559 ns->partitions[i].name = get_partition_name(i);
560 ns->partitions[i].offset = next_offset;
561 ns->partitions[i].size = remains;
562 ns->nbparts += 1;
563 }
564
451 /* Detect how many ID bytes the NAND chip outputs */ 565 /* Detect how many ID bytes the NAND chip outputs */
452 for (i = 0; nand_flash_ids[i].name != NULL; i++) { 566 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
453 if (second_id_byte != nand_flash_ids[i].id) 567 if (second_id_byte != nand_flash_ids[i].id)
@@ -474,7 +588,7 @@ static int init_nandsim(struct mtd_info *mtd)
474 printk("sector address bytes: %u\n", ns->geom.secaddrbytes); 588 printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
475 printk("options: %#x\n", ns->options); 589 printk("options: %#x\n", ns->options);
476 590
477 if (alloc_device(ns) != 0) 591 if ((ret = alloc_device(ns)) != 0)
478 goto error; 592 goto error;
479 593
480 /* Allocate / initialize the internal buffer */ 594 /* Allocate / initialize the internal buffer */
@@ -482,21 +596,17 @@ static int init_nandsim(struct mtd_info *mtd)
482 if (!ns->buf.byte) { 596 if (!ns->buf.byte) {
483 NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n", 597 NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
484 ns->geom.pgszoob); 598 ns->geom.pgszoob);
599 ret = -ENOMEM;
485 goto error; 600 goto error;
486 } 601 }
487 memset(ns->buf.byte, 0xFF, ns->geom.pgszoob); 602 memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
488 603
489 /* Fill the partition_info structure */
490 ns->part.name = "NAND simulator partition";
491 ns->part.offset = 0;
492 ns->part.size = ns->geom.totsz;
493
494 return 0; 604 return 0;
495 605
496error: 606error:
497 free_device(ns); 607 free_device(ns);
498 608
499 return -ENOMEM; 609 return ret;
500} 610}
501 611
502/* 612/*
@@ -510,6 +620,287 @@ static void free_nandsim(struct nandsim *ns)
510 return; 620 return;
511} 621}
512 622
623static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
624{
625 char *w;
626 int zero_ok;
627 unsigned int erase_block_no;
628 loff_t offset;
629
630 if (!badblocks)
631 return 0;
632 w = badblocks;
633 do {
634 zero_ok = (*w == '0' ? 1 : 0);
635 erase_block_no = simple_strtoul(w, &w, 0);
636 if (!zero_ok && !erase_block_no) {
637 NS_ERR("invalid badblocks.\n");
638 return -EINVAL;
639 }
640 offset = erase_block_no * ns->geom.secsz;
641 if (mtd->block_markbad(mtd, offset)) {
642 NS_ERR("invalid badblocks.\n");
643 return -EINVAL;
644 }
645 if (*w == ',')
646 w += 1;
647 } while (*w);
648 return 0;
649}
650
651static int parse_weakblocks(void)
652{
653 char *w;
654 int zero_ok;
655 unsigned int erase_block_no;
656 unsigned int max_erases;
657 struct weak_block *wb;
658
659 if (!weakblocks)
660 return 0;
661 w = weakblocks;
662 do {
663 zero_ok = (*w == '0' ? 1 : 0);
664 erase_block_no = simple_strtoul(w, &w, 0);
665 if (!zero_ok && !erase_block_no) {
666 NS_ERR("invalid weakblocks.\n");
667 return -EINVAL;
668 }
669 max_erases = 3;
670 if (*w == ':') {
671 w += 1;
672 max_erases = simple_strtoul(w, &w, 0);
673 }
674 if (*w == ',')
675 w += 1;
676 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
677 if (!wb) {
678 NS_ERR("unable to allocate memory.\n");
679 return -ENOMEM;
680 }
681 wb->erase_block_no = erase_block_no;
682 wb->max_erases = max_erases;
683 list_add(&wb->list, &weak_blocks);
684 } while (*w);
685 return 0;
686}
687
688static int erase_error(unsigned int erase_block_no)
689{
690 struct weak_block *wb;
691
692 list_for_each_entry(wb, &weak_blocks, list)
693 if (wb->erase_block_no == erase_block_no) {
694 if (wb->erases_done >= wb->max_erases)
695 return 1;
696 wb->erases_done += 1;
697 return 0;
698 }
699 return 0;
700}
701
702static int parse_weakpages(void)
703{
704 char *w;
705 int zero_ok;
706 unsigned int page_no;
707 unsigned int max_writes;
708 struct weak_page *wp;
709
710 if (!weakpages)
711 return 0;
712 w = weakpages;
713 do {
714 zero_ok = (*w == '0' ? 1 : 0);
715 page_no = simple_strtoul(w, &w, 0);
716 if (!zero_ok && !page_no) {
717 NS_ERR("invalid weakpagess.\n");
718 return -EINVAL;
719 }
720 max_writes = 3;
721 if (*w == ':') {
722 w += 1;
723 max_writes = simple_strtoul(w, &w, 0);
724 }
725 if (*w == ',')
726 w += 1;
727 wp = kzalloc(sizeof(*wp), GFP_KERNEL);
728 if (!wp) {
729 NS_ERR("unable to allocate memory.\n");
730 return -ENOMEM;
731 }
732 wp->page_no = page_no;
733 wp->max_writes = max_writes;
734 list_add(&wp->list, &weak_pages);
735 } while (*w);
736 return 0;
737}
738
739static int write_error(unsigned int page_no)
740{
741 struct weak_page *wp;
742
743 list_for_each_entry(wp, &weak_pages, list)
744 if (wp->page_no == page_no) {
745 if (wp->writes_done >= wp->max_writes)
746 return 1;
747 wp->writes_done += 1;
748 return 0;
749 }
750 return 0;
751}
752
753static int parse_gravepages(void)
754{
755 char *g;
756 int zero_ok;
757 unsigned int page_no;
758 unsigned int max_reads;
759 struct grave_page *gp;
760
761 if (!gravepages)
762 return 0;
763 g = gravepages;
764 do {
765 zero_ok = (*g == '0' ? 1 : 0);
766 page_no = simple_strtoul(g, &g, 0);
767 if (!zero_ok && !page_no) {
768 NS_ERR("invalid gravepagess.\n");
769 return -EINVAL;
770 }
771 max_reads = 3;
772 if (*g == ':') {
773 g += 1;
774 max_reads = simple_strtoul(g, &g, 0);
775 }
776 if (*g == ',')
777 g += 1;
778 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
779 if (!gp) {
780 NS_ERR("unable to allocate memory.\n");
781 return -ENOMEM;
782 }
783 gp->page_no = page_no;
784 gp->max_reads = max_reads;
785 list_add(&gp->list, &grave_pages);
786 } while (*g);
787 return 0;
788}
789
790static int read_error(unsigned int page_no)
791{
792 struct grave_page *gp;
793
794 list_for_each_entry(gp, &grave_pages, list)
795 if (gp->page_no == page_no) {
796 if (gp->reads_done >= gp->max_reads)
797 return 1;
798 gp->reads_done += 1;
799 return 0;
800 }
801 return 0;
802}
803
804static void free_lists(void)
805{
806 struct list_head *pos, *n;
807 list_for_each_safe(pos, n, &weak_blocks) {
808 list_del(pos);
809 kfree(list_entry(pos, struct weak_block, list));
810 }
811 list_for_each_safe(pos, n, &weak_pages) {
812 list_del(pos);
813 kfree(list_entry(pos, struct weak_page, list));
814 }
815 list_for_each_safe(pos, n, &grave_pages) {
816 list_del(pos);
817 kfree(list_entry(pos, struct grave_page, list));
818 }
819 kfree(erase_block_wear);
820}
821
822static int setup_wear_reporting(struct mtd_info *mtd)
823{
824 size_t mem;
825
826 if (!rptwear)
827 return 0;
828 wear_eb_count = mtd->size / mtd->erasesize;
829 mem = wear_eb_count * sizeof(unsigned long);
830 if (mem / sizeof(unsigned long) != wear_eb_count) {
831 NS_ERR("Too many erase blocks for wear reporting\n");
832 return -ENOMEM;
833 }
834 erase_block_wear = kzalloc(mem, GFP_KERNEL);
835 if (!erase_block_wear) {
836 NS_ERR("Too many erase blocks for wear reporting\n");
837 return -ENOMEM;
838 }
839 return 0;
840}
841
842static void update_wear(unsigned int erase_block_no)
843{
844 unsigned long wmin = -1, wmax = 0, avg;
845 unsigned long deciles[10], decile_max[10], tot = 0;
846 unsigned int i;
847
848 if (!erase_block_wear)
849 return;
850 total_wear += 1;
851 if (total_wear == 0)
852 NS_ERR("Erase counter total overflow\n");
853 erase_block_wear[erase_block_no] += 1;
854 if (erase_block_wear[erase_block_no] == 0)
855 NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
856 rptwear_cnt += 1;
857 if (rptwear_cnt < rptwear)
858 return;
859 rptwear_cnt = 0;
860 /* Calc wear stats */
861 for (i = 0; i < wear_eb_count; ++i) {
862 unsigned long wear = erase_block_wear[i];
863 if (wear < wmin)
864 wmin = wear;
865 if (wear > wmax)
866 wmax = wear;
867 tot += wear;
868 }
869 for (i = 0; i < 9; ++i) {
870 deciles[i] = 0;
871 decile_max[i] = (wmax * (i + 1) + 5) / 10;
872 }
873 deciles[9] = 0;
874 decile_max[9] = wmax;
875 for (i = 0; i < wear_eb_count; ++i) {
876 int d;
877 unsigned long wear = erase_block_wear[i];
878 for (d = 0; d < 10; ++d)
879 if (wear <= decile_max[d]) {
880 deciles[d] += 1;
881 break;
882 }
883 }
884 avg = tot / wear_eb_count;
885 /* Output wear report */
886 NS_INFO("*** Wear Report ***\n");
887 NS_INFO("Total numbers of erases: %lu\n", tot);
888 NS_INFO("Number of erase blocks: %u\n", wear_eb_count);
889 NS_INFO("Average number of erases: %lu\n", avg);
890 NS_INFO("Maximum number of erases: %lu\n", wmax);
891 NS_INFO("Minimum number of erases: %lu\n", wmin);
892 for (i = 0; i < 10; ++i) {
893 unsigned long from = (i ? decile_max[i - 1] + 1 : 0);
894 if (from > decile_max[i])
895 continue;
896 NS_INFO("Number of ebs with erase counts from %lu to %lu : %lu\n",
897 from,
898 decile_max[i],
899 deciles[i]);
900 }
901 NS_INFO("*** End of Wear Report ***\n");
902}
903
513/* 904/*
514 * Returns the string representation of 'state' state. 905 * Returns the string representation of 'state' state.
515 */ 906 */
@@ -822,9 +1213,31 @@ static void read_page(struct nandsim *ns, int num)
822 NS_DBG("read_page: page %d not allocated\n", ns->regs.row); 1213 NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
823 memset(ns->buf.byte, 0xFF, num); 1214 memset(ns->buf.byte, 0xFF, num);
824 } else { 1215 } else {
1216 unsigned int page_no = ns->regs.row;
825 NS_DBG("read_page: page %d allocated, reading from %d\n", 1217 NS_DBG("read_page: page %d allocated, reading from %d\n",
826 ns->regs.row, ns->regs.column + ns->regs.off); 1218 ns->regs.row, ns->regs.column + ns->regs.off);
1219 if (read_error(page_no)) {
1220 int i;
1221 memset(ns->buf.byte, 0xFF, num);
1222 for (i = 0; i < num; ++i)
1223 ns->buf.byte[i] = random32();
1224 NS_WARN("simulating read error in page %u\n", page_no);
1225 return;
1226 }
827 memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num); 1227 memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
1228 if (bitflips && random32() < (1 << 22)) {
1229 int flips = 1;
1230 if (bitflips > 1)
1231 flips = (random32() % (int) bitflips) + 1;
1232 while (flips--) {
1233 int pos = random32() % (num * 8);
1234 ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
1235 NS_WARN("read_page: flipping bit %d in page %d "
1236 "reading from %d ecc: corrected=%u failed=%u\n",
1237 pos, ns->regs.row, ns->regs.column + ns->regs.off,
1238 nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
1239 }
1240 }
828 } 1241 }
829} 1242}
830 1243
@@ -883,6 +1296,7 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
883{ 1296{
884 int num; 1297 int num;
885 int busdiv = ns->busw == 8 ? 1 : 2; 1298 int busdiv = ns->busw == 8 ? 1 : 2;
1299 unsigned int erase_block_no, page_no;
886 1300
887 action &= ACTION_MASK; 1301 action &= ACTION_MASK;
888 1302
@@ -942,14 +1356,24 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
942 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column; 1356 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
943 ns->regs.column = 0; 1357 ns->regs.column = 0;
944 1358
1359 erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
1360
945 NS_DBG("do_state_action: erase sector at address %#x, off = %d\n", 1361 NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
946 ns->regs.row, NS_RAW_OFFSET(ns)); 1362 ns->regs.row, NS_RAW_OFFSET(ns));
947 NS_LOG("erase sector %d\n", ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift)); 1363 NS_LOG("erase sector %u\n", erase_block_no);
948 1364
949 erase_sector(ns); 1365 erase_sector(ns);
950 1366
951 NS_MDELAY(erase_delay); 1367 NS_MDELAY(erase_delay);
952 1368
1369 if (erase_block_wear)
1370 update_wear(erase_block_no);
1371
1372 if (erase_error(erase_block_no)) {
1373 NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
1374 return -1;
1375 }
1376
953 break; 1377 break;
954 1378
955 case ACTION_PRGPAGE: 1379 case ACTION_PRGPAGE:
@@ -972,6 +1396,8 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
972 if (prog_page(ns, num) == -1) 1396 if (prog_page(ns, num) == -1)
973 return -1; 1397 return -1;
974 1398
1399 page_no = ns->regs.row;
1400
975 NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n", 1401 NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
976 num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off); 1402 num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
977 NS_LOG("programm page %d\n", ns->regs.row); 1403 NS_LOG("programm page %d\n", ns->regs.row);
@@ -979,6 +1405,11 @@ static int do_state_action(struct nandsim *ns, uint32_t action)
979 NS_UDELAY(programm_delay); 1405 NS_UDELAY(programm_delay);
980 NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv); 1406 NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
981 1407
1408 if (write_error(page_no)) {
1409 NS_WARN("simulating write failure in page %u\n", page_no);
1410 return -1;
1411 }
1412
982 break; 1413 break;
983 1414
984 case ACTION_ZEROOFF: 1415 case ACTION_ZEROOFF:
@@ -1503,7 +1934,7 @@ static int __init ns_init_module(void)
1503{ 1934{
1504 struct nand_chip *chip; 1935 struct nand_chip *chip;
1505 struct nandsim *nand; 1936 struct nandsim *nand;
1506 int retval = -ENOMEM; 1937 int retval = -ENOMEM, i;
1507 1938
1508 if (bus_width != 8 && bus_width != 16) { 1939 if (bus_width != 8 && bus_width != 16) {
1509 NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width); 1940 NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
@@ -1533,6 +1964,8 @@ static int __init ns_init_module(void)
1533 chip->verify_buf = ns_nand_verify_buf; 1964 chip->verify_buf = ns_nand_verify_buf;
1534 chip->read_word = ns_nand_read_word; 1965 chip->read_word = ns_nand_read_word;
1535 chip->ecc.mode = NAND_ECC_SOFT; 1966 chip->ecc.mode = NAND_ECC_SOFT;
1967 /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
1968 /* and 'badblocks' parameters to work */
1536 chip->options |= NAND_SKIP_BBTSCAN; 1969 chip->options |= NAND_SKIP_BBTSCAN;
1537 1970
1538 /* 1971 /*
@@ -1557,6 +1990,15 @@ static int __init ns_init_module(void)
1557 1990
1558 nsmtd->owner = THIS_MODULE; 1991 nsmtd->owner = THIS_MODULE;
1559 1992
1993 if ((retval = parse_weakblocks()) != 0)
1994 goto error;
1995
1996 if ((retval = parse_weakpages()) != 0)
1997 goto error;
1998
1999 if ((retval = parse_gravepages()) != 0)
2000 goto error;
2001
1560 if ((retval = nand_scan(nsmtd, 1)) != 0) { 2002 if ((retval = nand_scan(nsmtd, 1)) != 0) {
1561 NS_ERR("can't register NAND Simulator\n"); 2003 NS_ERR("can't register NAND Simulator\n");
1562 if (retval > 0) 2004 if (retval > 0)
@@ -1564,23 +2006,44 @@ static int __init ns_init_module(void)
1564 goto error; 2006 goto error;
1565 } 2007 }
1566 2008
1567 if ((retval = init_nandsim(nsmtd)) != 0) { 2009 if (overridesize) {
1568 NS_ERR("scan_bbt: can't initialize the nandsim structure\n"); 2010 u_int32_t new_size = nsmtd->erasesize << overridesize;
1569 goto error; 2011 if (new_size >> overridesize != nsmtd->erasesize) {
2012 NS_ERR("overridesize is too big\n");
2013 goto err_exit;
2014 }
2015 /* N.B. This relies on nand_scan not doing anything with the size before we change it */
2016 nsmtd->size = new_size;
2017 chip->chipsize = new_size;
2018 chip->chip_shift = ffs(new_size) - 1;
1570 } 2019 }
1571 2020
1572 if ((retval = nand_default_bbt(nsmtd)) != 0) { 2021 if ((retval = setup_wear_reporting(nsmtd)) != 0)
1573 free_nandsim(nand); 2022 goto err_exit;
1574 goto error; 2023
1575 } 2024 if ((retval = init_nandsim(nsmtd)) != 0)
2025 goto err_exit;
1576 2026
1577 /* Register NAND as one big partition */ 2027 if ((retval = parse_badblocks(nand, nsmtd)) != 0)
1578 add_mtd_partitions(nsmtd, &nand->part, 1); 2028 goto err_exit;
2029
2030 if ((retval = nand_default_bbt(nsmtd)) != 0)
2031 goto err_exit;
2032
2033 /* Register NAND partitions */
2034 if ((retval = add_mtd_partitions(nsmtd, &nand->partitions[0], nand->nbparts)) != 0)
2035 goto err_exit;
1579 2036
1580 return 0; 2037 return 0;
1581 2038
2039err_exit:
2040 free_nandsim(nand);
2041 nand_release(nsmtd);
2042 for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
2043 kfree(nand->partitions[i].name);
1582error: 2044error:
1583 kfree(nsmtd); 2045 kfree(nsmtd);
2046 free_lists();
1584 2047
1585 return retval; 2048 return retval;
1586} 2049}
@@ -1593,10 +2056,14 @@ module_init(ns_init_module);
1593static void __exit ns_cleanup_module(void) 2056static void __exit ns_cleanup_module(void)
1594{ 2057{
1595 struct nandsim *ns = (struct nandsim *)(((struct nand_chip *)nsmtd->priv)->priv); 2058 struct nandsim *ns = (struct nandsim *)(((struct nand_chip *)nsmtd->priv)->priv);
2059 int i;
1596 2060
1597 free_nandsim(ns); /* Free nandsim private resources */ 2061 free_nandsim(ns); /* Free nandsim private resources */
1598 nand_release(nsmtd); /* Unregisterd drived */ 2062 nand_release(nsmtd); /* Unregister driver */
2063 for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
2064 kfree(ns->partitions[i].name);
1599 kfree(nsmtd); /* Free other structures */ 2065 kfree(nsmtd); /* Free other structures */
2066 free_lists();
1600} 2067}
1601 2068
1602module_exit(ns_cleanup_module); 2069module_exit(ns_cleanup_module);
@@ -1604,4 +2071,3 @@ module_exit(ns_cleanup_module);
1604MODULE_LICENSE ("GPL"); 2071MODULE_LICENSE ("GPL");
1605MODULE_AUTHOR ("Artem B. Bityuckiy"); 2072MODULE_AUTHOR ("Artem B. Bityuckiy");
1606MODULE_DESCRIPTION ("The NAND flash simulator"); 2073MODULE_DESCRIPTION ("The NAND flash simulator");
1607