aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/arm/mach-ep93xx/include/mach/ts72xx.h19
-rw-r--r--arch/arm/mach-ep93xx/ts72xx.c188
-rw-r--r--drivers/mtd/chips/cfi_cmdset_0001.c6
-rw-r--r--drivers/mtd/chips/cfi_cmdset_0002.c20
-rw-r--r--drivers/mtd/devices/pmc551.c4
-rw-r--r--drivers/mtd/ftl.c1
-rwxr-xr-xdrivers/mtd/inftlcore.c1
-rw-r--r--drivers/mtd/inftlmount.c7
-rw-r--r--drivers/mtd/maps/bfin-async-flash.c16
-rw-r--r--drivers/mtd/maps/ceiva.c2
-rw-r--r--drivers/mtd/maps/ixp4xx.c4
-rw-r--r--drivers/mtd/maps/physmap_of.c2
-rw-r--r--drivers/mtd/mtd_blkdevs.c333
-rw-r--r--drivers/mtd/mtdblock.c73
-rw-r--r--drivers/mtd/mtdblock_ro.c4
-rw-r--r--drivers/mtd/mtdchar.c12
-rw-r--r--drivers/mtd/mtdcore.c284
-rw-r--r--drivers/mtd/mtdcore.h7
-rw-r--r--drivers/mtd/mtdoops.c5
-rw-r--r--drivers/mtd/mtdsuper.c18
-rw-r--r--drivers/mtd/nand/Kconfig23
-rw-r--r--drivers/mtd/nand/Makefile5
-rw-r--r--drivers/mtd/nand/alauda.c2
-rw-r--r--drivers/mtd/nand/atmel_nand.c2
-rw-r--r--drivers/mtd/nand/au1550nd.c12
-rw-r--r--drivers/mtd/nand/bcm_umi_nand.c2
-rw-r--r--drivers/mtd/nand/cafe_nand.c4
-rw-r--r--drivers/mtd/nand/davinci_nand.c6
-rw-r--r--drivers/mtd/nand/fsl_elbc_nand.c4
-rw-r--r--drivers/mtd/nand/fsl_upm.c4
-rw-r--r--drivers/mtd/nand/gpio.c12
-rw-r--r--drivers/mtd/nand/mpc5121_nfc.c916
-rw-r--r--drivers/mtd/nand/mxc_nand.c22
-rw-r--r--drivers/mtd/nand/nand_base.c312
-rw-r--r--drivers/mtd/nand/nand_bbt.c26
-rw-r--r--drivers/mtd/nand/nand_bcm_umi.h71
-rw-r--r--drivers/mtd/nand/nandsim.c11
-rw-r--r--drivers/mtd/nand/nomadik_nand.c6
-rw-r--r--drivers/mtd/nand/nuc900_nand.c (renamed from drivers/mtd/nand/w90p910_nand.c)144
-rw-r--r--drivers/mtd/nand/omap2.c16
-rw-r--r--drivers/mtd/nand/orion_nand.c10
-rw-r--r--drivers/mtd/nand/pasemi_nand.c2
-rw-r--r--drivers/mtd/nand/s3c2410.c5
-rw-r--r--drivers/mtd/nand/sh_flctl.c4
-rw-r--r--drivers/mtd/nand/sm_common.c143
-rw-r--r--drivers/mtd/nand/sm_common.h61
-rw-r--r--drivers/mtd/nand/socrates_nand.c4
-rw-r--r--drivers/mtd/nand/tmio_nand.c14
-rw-r--r--drivers/mtd/nand/ts7250.c207
-rw-r--r--drivers/mtd/nand/txx9ndfmc.c2
-rw-r--r--drivers/mtd/nftlcore.c1
-rw-r--r--drivers/mtd/onenand/omap2.c4
-rw-r--r--drivers/mtd/rfd_ftl.c1
-rw-r--r--drivers/mtd/ssfdc.c1
-rw-r--r--fs/jffs2/fs.c10
-rw-r--r--fs/jffs2/nodelist.h8
-rw-r--r--include/linux/jffs2.h4
-rw-r--r--include/linux/mtd/blktrans.h15
-rw-r--r--include/linux/mtd/cfi.h14
-rw-r--r--include/linux/mtd/mtd.h8
-rw-r--r--include/linux/mtd/nand.h21
-rw-r--r--lib/idr.c2
62 files changed, 2218 insertions, 929 deletions
diff --git a/arch/arm/mach-ep93xx/include/mach/ts72xx.h b/arch/arm/mach-ep93xx/include/mach/ts72xx.h
index 3bd934e9a7f1..61c0e132c63e 100644
--- a/arch/arm/mach-ep93xx/include/mach/ts72xx.h
+++ b/arch/arm/mach-ep93xx/include/mach/ts72xx.h
@@ -9,9 +9,6 @@
9 * febff000 22000000 4K model number register 9 * febff000 22000000 4K model number register
10 * febfe000 22400000 4K options register 10 * febfe000 22400000 4K options register
11 * febfd000 22800000 4K options register #2 11 * febfd000 22800000 4K options register #2
12 * febfc000 [67]0000000 4K NAND data register
13 * febfb000 [67]0400000 4K NAND control register
14 * febfa000 [67]0800000 4K NAND busy register
15 * febf9000 10800000 4K TS-5620 RTC index register 12 * febf9000 10800000 4K TS-5620 RTC index register
16 * febf8000 11700000 4K TS-5620 RTC data register 13 * febf8000 11700000 4K TS-5620 RTC data register
17 */ 14 */
@@ -41,22 +38,6 @@
41#define TS72XX_OPTIONS2_TS9420_BOOT 0x02 38#define TS72XX_OPTIONS2_TS9420_BOOT 0x02
42 39
43 40
44#define TS72XX_NAND1_DATA_PHYS_BASE 0x60000000
45#define TS72XX_NAND2_DATA_PHYS_BASE 0x70000000
46#define TS72XX_NAND_DATA_VIRT_BASE 0xfebfc000
47#define TS72XX_NAND_DATA_SIZE 0x00001000
48
49#define TS72XX_NAND1_CONTROL_PHYS_BASE 0x60400000
50#define TS72XX_NAND2_CONTROL_PHYS_BASE 0x70400000
51#define TS72XX_NAND_CONTROL_VIRT_BASE 0xfebfb000
52#define TS72XX_NAND_CONTROL_SIZE 0x00001000
53
54#define TS72XX_NAND1_BUSY_PHYS_BASE 0x60800000
55#define TS72XX_NAND2_BUSY_PHYS_BASE 0x70800000
56#define TS72XX_NAND_BUSY_VIRT_BASE 0xfebfa000
57#define TS72XX_NAND_BUSY_SIZE 0x00001000
58
59
60#define TS72XX_RTC_INDEX_VIRT_BASE 0xfebf9000 41#define TS72XX_RTC_INDEX_VIRT_BASE 0xfebf9000
61#define TS72XX_RTC_INDEX_PHYS_BASE 0x10800000 42#define TS72XX_RTC_INDEX_PHYS_BASE 0x10800000
62#define TS72XX_RTC_INDEX_SIZE 0x00001000 43#define TS72XX_RTC_INDEX_SIZE 0x00001000
diff --git a/arch/arm/mach-ep93xx/ts72xx.c b/arch/arm/mach-ep93xx/ts72xx.c
index 259f7822ba52..47a86f07831d 100644
--- a/arch/arm/mach-ep93xx/ts72xx.c
+++ b/arch/arm/mach-ep93xx/ts72xx.c
@@ -10,12 +10,16 @@
10 * your option) any later version. 10 * your option) any later version.
11 */ 11 */
12 12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
13#include <linux/kernel.h> 15#include <linux/kernel.h>
14#include <linux/init.h> 16#include <linux/init.h>
15#include <linux/platform_device.h> 17#include <linux/platform_device.h>
16#include <linux/io.h> 18#include <linux/io.h>
17#include <linux/m48t86.h> 19#include <linux/m48t86.h>
18#include <linux/mtd/physmap.h> 20#include <linux/mtd/physmap.h>
21#include <linux/mtd/nand.h>
22#include <linux/mtd/partitions.h>
19 23
20#include <mach/hardware.h> 24#include <mach/hardware.h>
21#include <mach/ts72xx.h> 25#include <mach/ts72xx.h>
@@ -54,92 +58,162 @@ static struct map_desc ts72xx_io_desc[] __initdata = {
54 } 58 }
55}; 59};
56 60
57static struct map_desc ts72xx_nand_io_desc[] __initdata = { 61static void __init ts72xx_map_io(void)
58 { 62{
59 .virtual = TS72XX_NAND_DATA_VIRT_BASE, 63 ep93xx_map_io();
60 .pfn = __phys_to_pfn(TS72XX_NAND1_DATA_PHYS_BASE), 64 iotable_init(ts72xx_io_desc, ARRAY_SIZE(ts72xx_io_desc));
61 .length = TS72XX_NAND_DATA_SIZE, 65}
62 .type = MT_DEVICE, 66
63 }, { 67
64 .virtual = TS72XX_NAND_CONTROL_VIRT_BASE, 68/*************************************************************************
65 .pfn = __phys_to_pfn(TS72XX_NAND1_CONTROL_PHYS_BASE), 69 * NAND flash
66 .length = TS72XX_NAND_CONTROL_SIZE, 70 *************************************************************************/
67 .type = MT_DEVICE, 71#define TS72XX_NAND_CONTROL_ADDR_LINE 22 /* 0xN0400000 */
68 }, { 72#define TS72XX_NAND_BUSY_ADDR_LINE 23 /* 0xN0800000 */
69 .virtual = TS72XX_NAND_BUSY_VIRT_BASE, 73
70 .pfn = __phys_to_pfn(TS72XX_NAND1_BUSY_PHYS_BASE), 74static void ts72xx_nand_hwcontrol(struct mtd_info *mtd,
71 .length = TS72XX_NAND_BUSY_SIZE, 75 int cmd, unsigned int ctrl)
72 .type = MT_DEVICE, 76{
77 struct nand_chip *chip = mtd->priv;
78
79 if (ctrl & NAND_CTRL_CHANGE) {
80 void __iomem *addr = chip->IO_ADDR_R;
81 unsigned char bits;
82
83 addr += (1 << TS72XX_NAND_CONTROL_ADDR_LINE);
84
85 bits = __raw_readb(addr) & ~0x07;
86 bits |= (ctrl & NAND_NCE) << 2; /* bit 0 -> bit 2 */
87 bits |= (ctrl & NAND_CLE); /* bit 1 -> bit 1 */
88 bits |= (ctrl & NAND_ALE) >> 2; /* bit 2 -> bit 0 */
89
90 __raw_writeb(bits, addr);
73 } 91 }
74};
75 92
76static struct map_desc ts72xx_alternate_nand_io_desc[] __initdata = { 93 if (cmd != NAND_CMD_NONE)
94 __raw_writeb(cmd, chip->IO_ADDR_W);
95}
96
97static int ts72xx_nand_device_ready(struct mtd_info *mtd)
98{
99 struct nand_chip *chip = mtd->priv;
100 void __iomem *addr = chip->IO_ADDR_R;
101
102 addr += (1 << TS72XX_NAND_BUSY_ADDR_LINE);
103
104 return !!(__raw_readb(addr) & 0x20);
105}
106
107static const char *ts72xx_nand_part_probes[] = { "cmdlinepart", NULL };
108
109#define TS72XX_BOOTROM_PART_SIZE (SZ_16K)
110#define TS72XX_REDBOOT_PART_SIZE (SZ_2M + SZ_1M)
111
112static struct mtd_partition ts72xx_nand_parts[] = {
77 { 113 {
78 .virtual = TS72XX_NAND_DATA_VIRT_BASE, 114 .name = "TS-BOOTROM",
79 .pfn = __phys_to_pfn(TS72XX_NAND2_DATA_PHYS_BASE), 115 .offset = 0,
80 .length = TS72XX_NAND_DATA_SIZE, 116 .size = TS72XX_BOOTROM_PART_SIZE,
81 .type = MT_DEVICE, 117 .mask_flags = MTD_WRITEABLE, /* force read-only */
82 }, { 118 }, {
83 .virtual = TS72XX_NAND_CONTROL_VIRT_BASE, 119 .name = "Linux",
84 .pfn = __phys_to_pfn(TS72XX_NAND2_CONTROL_PHYS_BASE), 120 .offset = MTDPART_OFS_APPEND,
85 .length = TS72XX_NAND_CONTROL_SIZE, 121 .size = 0, /* filled in later */
86 .type = MT_DEVICE,
87 }, { 122 }, {
88 .virtual = TS72XX_NAND_BUSY_VIRT_BASE, 123 .name = "RedBoot",
89 .pfn = __phys_to_pfn(TS72XX_NAND2_BUSY_PHYS_BASE), 124 .offset = MTDPART_OFS_APPEND,
90 .length = TS72XX_NAND_BUSY_SIZE, 125 .size = MTDPART_SIZ_FULL,
91 .type = MT_DEVICE, 126 .mask_flags = MTD_WRITEABLE, /* force read-only */
92 } 127 },
93}; 128};
94 129
95static void __init ts72xx_map_io(void) 130static void ts72xx_nand_set_parts(uint64_t size,
131 struct platform_nand_chip *chip)
96{ 132{
97 ep93xx_map_io(); 133 /* Factory TS-72xx boards only come with 32MiB or 128MiB NAND options */
98 iotable_init(ts72xx_io_desc, ARRAY_SIZE(ts72xx_io_desc)); 134 if (size == SZ_32M || size == SZ_128M) {
135 /* Set the "Linux" partition size */
136 ts72xx_nand_parts[1].size = size - TS72XX_REDBOOT_PART_SIZE;
99 137
100 /* 138 chip->partitions = ts72xx_nand_parts;
101 * The TS-7200 has NOR flash, the other models have NAND flash. 139 chip->nr_partitions = ARRAY_SIZE(ts72xx_nand_parts);
102 */ 140 } else {
103 if (!board_is_ts7200()) { 141 pr_warning("Unknown nand disk size:%lluMiB\n", size >> 20);
104 if (is_ts9420_installed()) {
105 iotable_init(ts72xx_alternate_nand_io_desc,
106 ARRAY_SIZE(ts72xx_alternate_nand_io_desc));
107 } else {
108 iotable_init(ts72xx_nand_io_desc,
109 ARRAY_SIZE(ts72xx_nand_io_desc));
110 }
111 } 142 }
112} 143}
113 144
145static struct platform_nand_data ts72xx_nand_data = {
146 .chip = {
147 .nr_chips = 1,
148 .chip_offset = 0,
149 .chip_delay = 15,
150 .part_probe_types = ts72xx_nand_part_probes,
151 .set_parts = ts72xx_nand_set_parts,
152 },
153 .ctrl = {
154 .cmd_ctrl = ts72xx_nand_hwcontrol,
155 .dev_ready = ts72xx_nand_device_ready,
156 },
157};
158
159static struct resource ts72xx_nand_resource[] = {
160 {
161 .start = 0, /* filled in later */
162 .end = 0, /* filled in later */
163 .flags = IORESOURCE_MEM,
164 },
165};
166
167static struct platform_device ts72xx_nand_flash = {
168 .name = "gen_nand",
169 .id = -1,
170 .dev.platform_data = &ts72xx_nand_data,
171 .resource = ts72xx_nand_resource,
172 .num_resources = ARRAY_SIZE(ts72xx_nand_resource),
173};
174
175
114/************************************************************************* 176/*************************************************************************
115 * NOR flash (TS-7200 only) 177 * NOR flash (TS-7200 only)
116 *************************************************************************/ 178 *************************************************************************/
117static struct physmap_flash_data ts72xx_flash_data = { 179static struct physmap_flash_data ts72xx_nor_data = {
118 .width = 2, 180 .width = 2,
119}; 181};
120 182
121static struct resource ts72xx_flash_resource = { 183static struct resource ts72xx_nor_resource = {
122 .start = EP93XX_CS6_PHYS_BASE, 184 .start = EP93XX_CS6_PHYS_BASE,
123 .end = EP93XX_CS6_PHYS_BASE + SZ_16M - 1, 185 .end = EP93XX_CS6_PHYS_BASE + SZ_16M - 1,
124 .flags = IORESOURCE_MEM, 186 .flags = IORESOURCE_MEM,
125}; 187};
126 188
127static struct platform_device ts72xx_flash = { 189static struct platform_device ts72xx_nor_flash = {
128 .name = "physmap-flash", 190 .name = "physmap-flash",
129 .id = 0, 191 .id = 0,
130 .dev = { 192 .dev.platform_data = &ts72xx_nor_data,
131 .platform_data = &ts72xx_flash_data, 193 .resource = &ts72xx_nor_resource,
132 }, 194 .num_resources = 1,
133 .num_resources = 1,
134 .resource = &ts72xx_flash_resource,
135}; 195};
136 196
137static void __init ts72xx_register_flash(void) 197static void __init ts72xx_register_flash(void)
138{ 198{
139 if (board_is_ts7200()) 199 if (board_is_ts7200()) {
140 platform_device_register(&ts72xx_flash); 200 platform_device_register(&ts72xx_nor_flash);
201 } else {
202 resource_size_t start;
203
204 if (is_ts9420_installed())
205 start = EP93XX_CS7_PHYS_BASE;
206 else
207 start = EP93XX_CS6_PHYS_BASE;
208
209 ts72xx_nand_resource[0].start = start;
210 ts72xx_nand_resource[0].end = start + SZ_16M - 1;
211
212 platform_device_register(&ts72xx_nand_flash);
213 }
141} 214}
142 215
216
143static unsigned char ts72xx_rtc_readbyte(unsigned long addr) 217static unsigned char ts72xx_rtc_readbyte(unsigned long addr)
144{ 218{
145 __raw_writeb(addr, TS72XX_RTC_INDEX_VIRT_BASE); 219 __raw_writeb(addr, TS72XX_RTC_INDEX_VIRT_BASE);
diff --git a/drivers/mtd/chips/cfi_cmdset_0001.c b/drivers/mtd/chips/cfi_cmdset_0001.c
index 5fbf29e1e64f..92530433c11c 100644
--- a/drivers/mtd/chips/cfi_cmdset_0001.c
+++ b/drivers/mtd/chips/cfi_cmdset_0001.c
@@ -615,10 +615,8 @@ static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
615 return mtd; 615 return mtd;
616 616
617 setup_err: 617 setup_err:
618 if(mtd) { 618 kfree(mtd->eraseregions);
619 kfree(mtd->eraseregions); 619 kfree(mtd);
620 kfree(mtd);
621 }
622 kfree(cfi->cmdset_priv); 620 kfree(cfi->cmdset_priv);
623 return NULL; 621 return NULL;
624} 622}
diff --git a/drivers/mtd/chips/cfi_cmdset_0002.c b/drivers/mtd/chips/cfi_cmdset_0002.c
index f3600e8d5382..ea2a7f66ddf9 100644
--- a/drivers/mtd/chips/cfi_cmdset_0002.c
+++ b/drivers/mtd/chips/cfi_cmdset_0002.c
@@ -43,10 +43,6 @@
43 43
44#define MAX_WORD_RETRIES 3 44#define MAX_WORD_RETRIES 3
45 45
46#define MANUFACTURER_AMD 0x0001
47#define MANUFACTURER_ATMEL 0x001F
48#define MANUFACTURER_MACRONIX 0x00C2
49#define MANUFACTURER_SST 0x00BF
50#define SST49LF004B 0x0060 46#define SST49LF004B 0x0060
51#define SST49LF040B 0x0050 47#define SST49LF040B 0x0050
52#define SST49LF008A 0x005a 48#define SST49LF008A 0x005a
@@ -168,7 +164,7 @@ static void fixup_amd_bootblock(struct mtd_info *mtd, void* param)
168 * This reduces the risk of false detection due to 164 * This reduces the risk of false detection due to
169 * the 8-bit device ID. 165 * the 8-bit device ID.
170 */ 166 */
171 (cfi->mfr == MANUFACTURER_MACRONIX)) { 167 (cfi->mfr == CFI_MFR_MACRONIX)) {
172 DEBUG(MTD_DEBUG_LEVEL1, 168 DEBUG(MTD_DEBUG_LEVEL1,
173 "%s: Macronix MX29LV400C with bottom boot block" 169 "%s: Macronix MX29LV400C with bottom boot block"
174 " detected\n", map->name); 170 " detected\n", map->name);
@@ -286,7 +282,7 @@ static struct cfi_fixup cfi_fixup_table[] = {
286 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL }, 282 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri, NULL },
287#ifdef AMD_BOOTLOC_BUG 283#ifdef AMD_BOOTLOC_BUG
288 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL }, 284 { CFI_MFR_AMD, CFI_ID_ANY, fixup_amd_bootblock, NULL },
289 { MANUFACTURER_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL }, 285 { CFI_MFR_MACRONIX, CFI_ID_ANY, fixup_amd_bootblock, NULL },
290#endif 286#endif
291 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, }, 287 { CFI_MFR_AMD, 0x0050, fixup_use_secsi, NULL, },
292 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, }, 288 { CFI_MFR_AMD, 0x0053, fixup_use_secsi, NULL, },
@@ -304,9 +300,9 @@ static struct cfi_fixup cfi_fixup_table[] = {
304 { 0, 0, NULL, NULL } 300 { 0, 0, NULL, NULL }
305}; 301};
306static struct cfi_fixup jedec_fixup_table[] = { 302static struct cfi_fixup jedec_fixup_table[] = {
307 { MANUFACTURER_SST, SST49LF004B, fixup_use_fwh_lock, NULL, }, 303 { CFI_MFR_SST, SST49LF004B, fixup_use_fwh_lock, NULL, },
308 { MANUFACTURER_SST, SST49LF040B, fixup_use_fwh_lock, NULL, }, 304 { CFI_MFR_SST, SST49LF040B, fixup_use_fwh_lock, NULL, },
309 { MANUFACTURER_SST, SST49LF008A, fixup_use_fwh_lock, NULL, }, 305 { CFI_MFR_SST, SST49LF008A, fixup_use_fwh_lock, NULL, },
310 { 0, 0, NULL, NULL } 306 { 0, 0, NULL, NULL }
311}; 307};
312 308
@@ -494,10 +490,8 @@ static struct mtd_info *cfi_amdstd_setup(struct mtd_info *mtd)
494 return mtd; 490 return mtd;
495 491
496 setup_err: 492 setup_err:
497 if(mtd) { 493 kfree(mtd->eraseregions);
498 kfree(mtd->eraseregions); 494 kfree(mtd);
499 kfree(mtd);
500 }
501 kfree(cfi->cmdset_priv); 495 kfree(cfi->cmdset_priv);
502 kfree(cfi->cfiq); 496 kfree(cfi->cfiq);
503 return NULL; 497 return NULL;
diff --git a/drivers/mtd/devices/pmc551.c b/drivers/mtd/devices/pmc551.c
index d2fd550f7e09..fc8ea0a57ac2 100644
--- a/drivers/mtd/devices/pmc551.c
+++ b/drivers/mtd/devices/pmc551.c
@@ -668,7 +668,7 @@ static int __init init_pmc551(void)
668{ 668{
669 struct pci_dev *PCI_Device = NULL; 669 struct pci_dev *PCI_Device = NULL;
670 struct mypriv *priv; 670 struct mypriv *priv;
671 int count, found = 0; 671 int found = 0;
672 struct mtd_info *mtd; 672 struct mtd_info *mtd;
673 u32 length = 0; 673 u32 length = 0;
674 674
@@ -695,7 +695,7 @@ static int __init init_pmc551(void)
695 /* 695 /*
696 * PCU-bus chipset probe. 696 * PCU-bus chipset probe.
697 */ 697 */
698 for (count = 0; count < MAX_MTD_DEVICES; count++) { 698 for (;;) {
699 699
700 if ((PCI_Device = pci_get_device(PCI_VENDOR_ID_V3_SEMI, 700 if ((PCI_Device = pci_get_device(PCI_VENDOR_ID_V3_SEMI,
701 PCI_DEVICE_ID_V3_SEMI_V370PDC, 701 PCI_DEVICE_ID_V3_SEMI_V370PDC,
diff --git a/drivers/mtd/ftl.c b/drivers/mtd/ftl.c
index e56d6b42f020..62da9eb7032b 100644
--- a/drivers/mtd/ftl.c
+++ b/drivers/mtd/ftl.c
@@ -1082,7 +1082,6 @@ static void ftl_remove_dev(struct mtd_blktrans_dev *dev)
1082{ 1082{
1083 del_mtd_blktrans_dev(dev); 1083 del_mtd_blktrans_dev(dev);
1084 ftl_freepart((partition_t *)dev); 1084 ftl_freepart((partition_t *)dev);
1085 kfree(dev);
1086} 1085}
1087 1086
1088static struct mtd_blktrans_ops ftl_tr = { 1087static struct mtd_blktrans_ops ftl_tr = {
diff --git a/drivers/mtd/inftlcore.c b/drivers/mtd/inftlcore.c
index 8aca5523a337..015a7fe1b6ee 100755
--- a/drivers/mtd/inftlcore.c
+++ b/drivers/mtd/inftlcore.c
@@ -139,7 +139,6 @@ static void inftl_remove_dev(struct mtd_blktrans_dev *dev)
139 139
140 kfree(inftl->PUtable); 140 kfree(inftl->PUtable);
141 kfree(inftl->VUtable); 141 kfree(inftl->VUtable);
142 kfree(inftl);
143} 142}
144 143
145/* 144/*
diff --git a/drivers/mtd/inftlmount.c b/drivers/mtd/inftlmount.c
index 32e82aef3e53..8f988d7d3c5c 100644
--- a/drivers/mtd/inftlmount.c
+++ b/drivers/mtd/inftlmount.c
@@ -100,9 +100,10 @@ static int find_boot_record(struct INFTLrecord *inftl)
100 } 100 }
101 101
102 /* To be safer with BIOS, also use erase mark as discriminant */ 102 /* To be safer with BIOS, also use erase mark as discriminant */
103 if ((ret = inftl_read_oob(mtd, block * inftl->EraseSize + 103 ret = inftl_read_oob(mtd,
104 SECTORSIZE + 8, 8, &retlen, 104 block * inftl->EraseSize + SECTORSIZE + 8,
105 (char *)&h1) < 0)) { 105 8, &retlen,(char *)&h1);
106 if (ret < 0) {
106 printk(KERN_WARNING "INFTL: ANAND header found at " 107 printk(KERN_WARNING "INFTL: ANAND header found at "
107 "0x%x in mtd%d, but OOB data read failed " 108 "0x%x in mtd%d, but OOB data read failed "
108 "(err %d)\n", block * inftl->EraseSize, 109 "(err %d)\n", block * inftl->EraseSize,
diff --git a/drivers/mtd/maps/bfin-async-flash.c b/drivers/mtd/maps/bfin-async-flash.c
index a7c808b577d3..5824fd49800c 100644
--- a/drivers/mtd/maps/bfin-async-flash.c
+++ b/drivers/mtd/maps/bfin-async-flash.c
@@ -69,7 +69,7 @@ static void switch_back(struct async_state *state)
69 local_irq_restore(state->irq_flags); 69 local_irq_restore(state->irq_flags);
70} 70}
71 71
72static map_word bfin_read(struct map_info *map, unsigned long ofs) 72static map_word bfin_flash_read(struct map_info *map, unsigned long ofs)
73{ 73{
74 struct async_state *state = (struct async_state *)map->map_priv_1; 74 struct async_state *state = (struct async_state *)map->map_priv_1;
75 uint16_t word; 75 uint16_t word;
@@ -85,7 +85,7 @@ static map_word bfin_read(struct map_info *map, unsigned long ofs)
85 return test; 85 return test;
86} 86}
87 87
88static void bfin_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len) 88static void bfin_flash_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
89{ 89{
90 struct async_state *state = (struct async_state *)map->map_priv_1; 90 struct async_state *state = (struct async_state *)map->map_priv_1;
91 91
@@ -96,7 +96,7 @@ static void bfin_copy_from(struct map_info *map, void *to, unsigned long from, s
96 switch_back(state); 96 switch_back(state);
97} 97}
98 98
99static void bfin_write(struct map_info *map, map_word d1, unsigned long ofs) 99static void bfin_flash_write(struct map_info *map, map_word d1, unsigned long ofs)
100{ 100{
101 struct async_state *state = (struct async_state *)map->map_priv_1; 101 struct async_state *state = (struct async_state *)map->map_priv_1;
102 uint16_t d; 102 uint16_t d;
@@ -111,7 +111,7 @@ static void bfin_write(struct map_info *map, map_word d1, unsigned long ofs)
111 switch_back(state); 111 switch_back(state);
112} 112}
113 113
114static void bfin_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len) 114static void bfin_flash_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
115{ 115{
116 struct async_state *state = (struct async_state *)map->map_priv_1; 116 struct async_state *state = (struct async_state *)map->map_priv_1;
117 117
@@ -140,10 +140,10 @@ static int __devinit bfin_flash_probe(struct platform_device *pdev)
140 return -ENOMEM; 140 return -ENOMEM;
141 141
142 state->map.name = DRIVER_NAME; 142 state->map.name = DRIVER_NAME;
143 state->map.read = bfin_read; 143 state->map.read = bfin_flash_read;
144 state->map.copy_from = bfin_copy_from; 144 state->map.copy_from = bfin_flash_copy_from;
145 state->map.write = bfin_write; 145 state->map.write = bfin_flash_write;
146 state->map.copy_to = bfin_copy_to; 146 state->map.copy_to = bfin_flash_copy_to;
147 state->map.bankwidth = pdata->width; 147 state->map.bankwidth = pdata->width;
148 state->map.size = memory->end - memory->start + 1; 148 state->map.size = memory->end - memory->start + 1;
149 state->map.virt = (void __iomem *)memory->start; 149 state->map.virt = (void __iomem *)memory->start;
diff --git a/drivers/mtd/maps/ceiva.c b/drivers/mtd/maps/ceiva.c
index d41f34766e53..c09f4f57093e 100644
--- a/drivers/mtd/maps/ceiva.c
+++ b/drivers/mtd/maps/ceiva.c
@@ -253,7 +253,7 @@ static void __exit clps_destroy_mtd(struct clps_info *clps, struct mtd_info *mtd
253 253
254static int __init clps_setup_flash(void) 254static int __init clps_setup_flash(void)
255{ 255{
256 int nr; 256 int nr = 0;
257 257
258#ifdef CONFIG_ARCH_CEIVA 258#ifdef CONFIG_ARCH_CEIVA
259 if (machine_is_ceiva()) { 259 if (machine_is_ceiva()) {
diff --git a/drivers/mtd/maps/ixp4xx.c b/drivers/mtd/maps/ixp4xx.c
index 7b0515297411..7513d90fee6f 100644
--- a/drivers/mtd/maps/ixp4xx.c
+++ b/drivers/mtd/maps/ixp4xx.c
@@ -107,8 +107,8 @@ static void ixp4xx_copy_from(struct map_info *map, void *to,
107 return; 107 return;
108 108
109 if (from & 1) { 109 if (from & 1) {
110 *dest++ = BYTE1(flash_read16(src)); 110 *dest++ = BYTE1(flash_read16(src-1));
111 src++; 111 src++;
112 --len; 112 --len;
113 } 113 }
114 114
diff --git a/drivers/mtd/maps/physmap_of.c b/drivers/mtd/maps/physmap_of.c
index 61e4eb48bb2d..1d91333010b1 100644
--- a/drivers/mtd/maps/physmap_of.c
+++ b/drivers/mtd/maps/physmap_of.c
@@ -217,7 +217,7 @@ static int __devinit of_flash_probe(struct of_device *dev,
217 217
218 dev_set_drvdata(&dev->dev, info); 218 dev_set_drvdata(&dev->dev, info);
219 219
220 mtd_list = kzalloc(sizeof(struct mtd_info) * count, GFP_KERNEL); 220 mtd_list = kzalloc(sizeof(*mtd_list) * count, GFP_KERNEL);
221 if (!mtd_list) 221 if (!mtd_list)
222 goto err_flash_remove; 222 goto err_flash_remove;
223 223
diff --git a/drivers/mtd/mtd_blkdevs.c b/drivers/mtd/mtd_blkdevs.c
index c82e09bbc5fd..9dd23d6acbb6 100644
--- a/drivers/mtd/mtd_blkdevs.c
+++ b/drivers/mtd/mtd_blkdevs.c
@@ -14,7 +14,6 @@
14#include <linux/mtd/mtd.h> 14#include <linux/mtd/mtd.h>
15#include <linux/blkdev.h> 15#include <linux/blkdev.h>
16#include <linux/blkpg.h> 16#include <linux/blkpg.h>
17#include <linux/freezer.h>
18#include <linux/spinlock.h> 17#include <linux/spinlock.h>
19#include <linux/hdreg.h> 18#include <linux/hdreg.h>
20#include <linux/init.h> 19#include <linux/init.h>
@@ -25,12 +24,41 @@
25#include "mtdcore.h" 24#include "mtdcore.h"
26 25
27static LIST_HEAD(blktrans_majors); 26static LIST_HEAD(blktrans_majors);
27static DEFINE_MUTEX(blktrans_ref_mutex);
28
29void blktrans_dev_release(struct kref *kref)
30{
31 struct mtd_blktrans_dev *dev =
32 container_of(kref, struct mtd_blktrans_dev, ref);
33
34 dev->disk->private_data = NULL;
35 put_disk(dev->disk);
36 list_del(&dev->list);
37 kfree(dev);
38}
39
40static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
41{
42 struct mtd_blktrans_dev *dev;
43
44 mutex_lock(&blktrans_ref_mutex);
45 dev = disk->private_data;
46
47 if (!dev)
48 goto unlock;
49 kref_get(&dev->ref);
50unlock:
51 mutex_unlock(&blktrans_ref_mutex);
52 return dev;
53}
54
55void blktrans_dev_put(struct mtd_blktrans_dev *dev)
56{
57 mutex_lock(&blktrans_ref_mutex);
58 kref_put(&dev->ref, blktrans_dev_release);
59 mutex_unlock(&blktrans_ref_mutex);
60}
28 61
29struct mtd_blkcore_priv {
30 struct task_struct *thread;
31 struct request_queue *rq;
32 spinlock_t queue_lock;
33};
34 62
35static int do_blktrans_request(struct mtd_blktrans_ops *tr, 63static int do_blktrans_request(struct mtd_blktrans_ops *tr,
36 struct mtd_blktrans_dev *dev, 64 struct mtd_blktrans_dev *dev,
@@ -61,7 +89,6 @@ static int do_blktrans_request(struct mtd_blktrans_ops *tr,
61 return -EIO; 89 return -EIO;
62 rq_flush_dcache_pages(req); 90 rq_flush_dcache_pages(req);
63 return 0; 91 return 0;
64
65 case WRITE: 92 case WRITE:
66 if (!tr->writesect) 93 if (!tr->writesect)
67 return -EIO; 94 return -EIO;
@@ -71,7 +98,6 @@ static int do_blktrans_request(struct mtd_blktrans_ops *tr,
71 if (tr->writesect(dev, block, buf)) 98 if (tr->writesect(dev, block, buf))
72 return -EIO; 99 return -EIO;
73 return 0; 100 return 0;
74
75 default: 101 default:
76 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req)); 102 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
77 return -EIO; 103 return -EIO;
@@ -80,14 +106,13 @@ static int do_blktrans_request(struct mtd_blktrans_ops *tr,
80 106
81static int mtd_blktrans_thread(void *arg) 107static int mtd_blktrans_thread(void *arg)
82{ 108{
83 struct mtd_blktrans_ops *tr = arg; 109 struct mtd_blktrans_dev *dev = arg;
84 struct request_queue *rq = tr->blkcore_priv->rq; 110 struct request_queue *rq = dev->rq;
85 struct request *req = NULL; 111 struct request *req = NULL;
86 112
87 spin_lock_irq(rq->queue_lock); 113 spin_lock_irq(rq->queue_lock);
88 114
89 while (!kthread_should_stop()) { 115 while (!kthread_should_stop()) {
90 struct mtd_blktrans_dev *dev;
91 int res; 116 int res;
92 117
93 if (!req && !(req = blk_fetch_request(rq))) { 118 if (!req && !(req = blk_fetch_request(rq))) {
@@ -98,13 +123,10 @@ static int mtd_blktrans_thread(void *arg)
98 continue; 123 continue;
99 } 124 }
100 125
101 dev = req->rq_disk->private_data;
102 tr = dev->tr;
103
104 spin_unlock_irq(rq->queue_lock); 126 spin_unlock_irq(rq->queue_lock);
105 127
106 mutex_lock(&dev->lock); 128 mutex_lock(&dev->lock);
107 res = do_blktrans_request(tr, dev, req); 129 res = do_blktrans_request(dev->tr, dev, req);
108 mutex_unlock(&dev->lock); 130 mutex_unlock(&dev->lock);
109 131
110 spin_lock_irq(rq->queue_lock); 132 spin_lock_irq(rq->queue_lock);
@@ -123,81 +145,112 @@ static int mtd_blktrans_thread(void *arg)
123 145
124static void mtd_blktrans_request(struct request_queue *rq) 146static void mtd_blktrans_request(struct request_queue *rq)
125{ 147{
126 struct mtd_blktrans_ops *tr = rq->queuedata; 148 struct mtd_blktrans_dev *dev;
127 wake_up_process(tr->blkcore_priv->thread); 149 struct request *req = NULL;
128}
129 150
151 dev = rq->queuedata;
152
153 if (!dev)
154 while ((req = blk_fetch_request(rq)) != NULL)
155 __blk_end_request_all(req, -ENODEV);
156 else
157 wake_up_process(dev->thread);
158}
130 159
131static int blktrans_open(struct block_device *bdev, fmode_t mode) 160static int blktrans_open(struct block_device *bdev, fmode_t mode)
132{ 161{
133 struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data; 162 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
134 struct mtd_blktrans_ops *tr = dev->tr; 163 int ret;
135 int ret = -ENODEV; 164
136 165 if (!dev)
137 if (!get_mtd_device(NULL, dev->mtd->index)) 166 return -ERESTARTSYS;
138 goto out; 167
139 168 mutex_lock(&dev->lock);
140 if (!try_module_get(tr->owner)) 169
141 goto out_tr; 170 if (!dev->mtd) {
142 171 ret = -ENXIO;
143 /* FIXME: Locking. A hot pluggable device can go away 172 goto unlock;
144 (del_mtd_device can be called for it) without its module
145 being unloaded. */
146 dev->mtd->usecount++;
147
148 ret = 0;
149 if (tr->open && (ret = tr->open(dev))) {
150 dev->mtd->usecount--;
151 put_mtd_device(dev->mtd);
152 out_tr:
153 module_put(tr->owner);
154 } 173 }
155 out: 174
175 ret = !dev->open++ && dev->tr->open ? dev->tr->open(dev) : 0;
176
177 /* Take another reference on the device so it won't go away till
178 last release */
179 if (!ret)
180 kref_get(&dev->ref);
181unlock:
182 mutex_unlock(&dev->lock);
183 blktrans_dev_put(dev);
156 return ret; 184 return ret;
157} 185}
158 186
159static int blktrans_release(struct gendisk *disk, fmode_t mode) 187static int blktrans_release(struct gendisk *disk, fmode_t mode)
160{ 188{
161 struct mtd_blktrans_dev *dev = disk->private_data; 189 struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
162 struct mtd_blktrans_ops *tr = dev->tr; 190 int ret = -ENXIO;
163 int ret = 0;
164 191
165 if (tr->release) 192 if (!dev)
166 ret = tr->release(dev); 193 return ret;
167 194
168 if (!ret) { 195 mutex_lock(&dev->lock);
169 dev->mtd->usecount--; 196
170 put_mtd_device(dev->mtd); 197 /* Release one reference, we sure its not the last one here*/
171 module_put(tr->owner); 198 kref_put(&dev->ref, blktrans_dev_release);
172 } 199
200 if (!dev->mtd)
201 goto unlock;
173 202
203 ret = !--dev->open && dev->tr->release ? dev->tr->release(dev) : 0;
204unlock:
205 mutex_unlock(&dev->lock);
206 blktrans_dev_put(dev);
174 return ret; 207 return ret;
175} 208}
176 209
177static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo) 210static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
178{ 211{
179 struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data; 212 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
213 int ret = -ENXIO;
180 214
181 if (dev->tr->getgeo) 215 if (!dev)
182 return dev->tr->getgeo(dev, geo); 216 return ret;
183 return -ENOTTY; 217
218 mutex_lock(&dev->lock);
219
220 if (!dev->mtd)
221 goto unlock;
222
223 ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : 0;
224unlock:
225 mutex_unlock(&dev->lock);
226 blktrans_dev_put(dev);
227 return ret;
184} 228}
185 229
186static int blktrans_ioctl(struct block_device *bdev, fmode_t mode, 230static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
187 unsigned int cmd, unsigned long arg) 231 unsigned int cmd, unsigned long arg)
188{ 232{
189 struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data; 233 struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
190 struct mtd_blktrans_ops *tr = dev->tr; 234 int ret = -ENXIO;
235
236 if (!dev)
237 return ret;
238
239 mutex_lock(&dev->lock);
240
241 if (!dev->mtd)
242 goto unlock;
191 243
192 switch (cmd) { 244 switch (cmd) {
193 case BLKFLSBUF: 245 case BLKFLSBUF:
194 if (tr->flush) 246 ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
195 return tr->flush(dev);
196 /* The core code did the work, we had nothing to do. */
197 return 0;
198 default: 247 default:
199 return -ENOTTY; 248 ret = -ENOTTY;
200 } 249 }
250unlock:
251 mutex_unlock(&dev->lock);
252 blktrans_dev_put(dev);
253 return ret;
201} 254}
202 255
203static const struct block_device_operations mtd_blktrans_ops = { 256static const struct block_device_operations mtd_blktrans_ops = {
@@ -214,12 +267,14 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
214 struct mtd_blktrans_dev *d; 267 struct mtd_blktrans_dev *d;
215 int last_devnum = -1; 268 int last_devnum = -1;
216 struct gendisk *gd; 269 struct gendisk *gd;
270 int ret;
217 271
218 if (mutex_trylock(&mtd_table_mutex)) { 272 if (mutex_trylock(&mtd_table_mutex)) {
219 mutex_unlock(&mtd_table_mutex); 273 mutex_unlock(&mtd_table_mutex);
220 BUG(); 274 BUG();
221 } 275 }
222 276
277 mutex_lock(&blktrans_ref_mutex);
223 list_for_each_entry(d, &tr->devs, list) { 278 list_for_each_entry(d, &tr->devs, list) {
224 if (new->devnum == -1) { 279 if (new->devnum == -1) {
225 /* Use first free number */ 280 /* Use first free number */
@@ -231,6 +286,7 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
231 } 286 }
232 } else if (d->devnum == new->devnum) { 287 } else if (d->devnum == new->devnum) {
233 /* Required number taken */ 288 /* Required number taken */
289 mutex_unlock(&blktrans_ref_mutex);
234 return -EBUSY; 290 return -EBUSY;
235 } else if (d->devnum > new->devnum) { 291 } else if (d->devnum > new->devnum) {
236 /* Required number was free */ 292 /* Required number was free */
@@ -239,24 +295,38 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
239 } 295 }
240 last_devnum = d->devnum; 296 last_devnum = d->devnum;
241 } 297 }
298
299 ret = -EBUSY;
242 if (new->devnum == -1) 300 if (new->devnum == -1)
243 new->devnum = last_devnum+1; 301 new->devnum = last_devnum+1;
244 302
245 if ((new->devnum << tr->part_bits) > 256) { 303 /* Check that the device and any partitions will get valid
246 return -EBUSY; 304 * minor numbers and that the disk naming code below can cope
305 * with this number. */
306 if (new->devnum > (MINORMASK >> tr->part_bits) ||
307 (tr->part_bits && new->devnum >= 27 * 26)) {
308 mutex_unlock(&blktrans_ref_mutex);
309 goto error1;
247 } 310 }
248 311
249 list_add_tail(&new->list, &tr->devs); 312 list_add_tail(&new->list, &tr->devs);
250 added: 313 added:
314 mutex_unlock(&blktrans_ref_mutex);
315
251 mutex_init(&new->lock); 316 mutex_init(&new->lock);
317 kref_init(&new->ref);
252 if (!tr->writesect) 318 if (!tr->writesect)
253 new->readonly = 1; 319 new->readonly = 1;
254 320
321 /* Create gendisk */
322 ret = -ENOMEM;
255 gd = alloc_disk(1 << tr->part_bits); 323 gd = alloc_disk(1 << tr->part_bits);
256 if (!gd) { 324
257 list_del(&new->list); 325 if (!gd)
258 return -ENOMEM; 326 goto error2;
259 } 327
328 new->disk = gd;
329 gd->private_data = new;
260 gd->major = tr->major; 330 gd->major = tr->major;
261 gd->first_minor = (new->devnum) << tr->part_bits; 331 gd->first_minor = (new->devnum) << tr->part_bits;
262 gd->fops = &mtd_blktrans_ops; 332 gd->fops = &mtd_blktrans_ops;
@@ -274,13 +344,35 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
274 snprintf(gd->disk_name, sizeof(gd->disk_name), 344 snprintf(gd->disk_name, sizeof(gd->disk_name),
275 "%s%d", tr->name, new->devnum); 345 "%s%d", tr->name, new->devnum);
276 346
277 /* 2.5 has capacity in units of 512 bytes while still
278 having BLOCK_SIZE_BITS set to 10. Just to keep us amused. */
279 set_capacity(gd, (new->size * tr->blksize) >> 9); 347 set_capacity(gd, (new->size * tr->blksize) >> 9);
280 348
281 gd->private_data = new; 349 /* Create the request queue */
282 new->blkcore_priv = gd; 350 spin_lock_init(&new->queue_lock);
283 gd->queue = tr->blkcore_priv->rq; 351 new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
352
353 if (!new->rq)
354 goto error3;
355
356 new->rq->queuedata = new;
357 blk_queue_logical_block_size(new->rq, tr->blksize);
358
359 if (tr->discard)
360 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
361 new->rq);
362
363 gd->queue = new->rq;
364
365 __get_mtd_device(new->mtd);
366 __module_get(tr->owner);
367
368 /* Create processing thread */
369 /* TODO: workqueue ? */
370 new->thread = kthread_run(mtd_blktrans_thread, new,
371 "%s%d", tr->name, new->mtd->index);
372 if (IS_ERR(new->thread)) {
373 ret = PTR_ERR(new->thread);
374 goto error4;
375 }
284 gd->driverfs_dev = &new->mtd->dev; 376 gd->driverfs_dev = &new->mtd->dev;
285 377
286 if (new->readonly) 378 if (new->readonly)
@@ -288,21 +380,64 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
288 380
289 add_disk(gd); 381 add_disk(gd);
290 382
383 if (new->disk_attributes)
384 sysfs_create_group(&disk_to_dev(gd)->kobj,
385 new->disk_attributes);
291 return 0; 386 return 0;
387error4:
388 module_put(tr->owner);
389 __put_mtd_device(new->mtd);
390 blk_cleanup_queue(new->rq);
391error3:
392 put_disk(new->disk);
393error2:
394 list_del(&new->list);
395error1:
396 kfree(new);
397 return ret;
292} 398}
293 399
294int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old) 400int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
295{ 401{
402 unsigned long flags;
403
296 if (mutex_trylock(&mtd_table_mutex)) { 404 if (mutex_trylock(&mtd_table_mutex)) {
297 mutex_unlock(&mtd_table_mutex); 405 mutex_unlock(&mtd_table_mutex);
298 BUG(); 406 BUG();
299 } 407 }
300 408
301 list_del(&old->list); 409 /* Stop new requests to arrive */
410 del_gendisk(old->disk);
411
412 if (old->disk_attributes)
413 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
414 old->disk_attributes);
415
416 /* Stop the thread */
417 kthread_stop(old->thread);
418
419 /* Kill current requests */
420 spin_lock_irqsave(&old->queue_lock, flags);
421 old->rq->queuedata = NULL;
422 blk_start_queue(old->rq);
423 spin_unlock_irqrestore(&old->queue_lock, flags);
424 blk_cleanup_queue(old->rq);
425
426 /* Ask trans driver for release to the mtd device */
427 mutex_lock(&old->lock);
428 if (old->open && old->tr->release) {
429 old->tr->release(old);
430 old->open = 0;
431 }
432
433 __put_mtd_device(old->mtd);
434 module_put(old->tr->owner);
302 435
303 del_gendisk(old->blkcore_priv); 436 /* At that point, we don't touch the mtd anymore */
304 put_disk(old->blkcore_priv); 437 old->mtd = NULL;
305 438
439 mutex_unlock(&old->lock);
440 blktrans_dev_put(old);
306 return 0; 441 return 0;
307} 442}
308 443
@@ -335,7 +470,8 @@ static struct mtd_notifier blktrans_notifier = {
335 470
336int register_mtd_blktrans(struct mtd_blktrans_ops *tr) 471int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
337{ 472{
338 int ret, i; 473 struct mtd_info *mtd;
474 int ret;
339 475
340 /* Register the notifier if/when the first device type is 476 /* Register the notifier if/when the first device type is
341 registered, to prevent the link/init ordering from fucking 477 registered, to prevent the link/init ordering from fucking
@@ -343,9 +479,6 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
343 if (!blktrans_notifier.list.next) 479 if (!blktrans_notifier.list.next)
344 register_mtd_user(&blktrans_notifier); 480 register_mtd_user(&blktrans_notifier);
345 481
346 tr->blkcore_priv = kzalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);
347 if (!tr->blkcore_priv)
348 return -ENOMEM;
349 482
350 mutex_lock(&mtd_table_mutex); 483 mutex_lock(&mtd_table_mutex);
351 484
@@ -353,49 +486,20 @@ int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
353 if (ret) { 486 if (ret) {
354 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n", 487 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
355 tr->name, tr->major, ret); 488 tr->name, tr->major, ret);
356 kfree(tr->blkcore_priv);
357 mutex_unlock(&mtd_table_mutex); 489 mutex_unlock(&mtd_table_mutex);
358 return ret; 490 return ret;
359 } 491 }
360 spin_lock_init(&tr->blkcore_priv->queue_lock);
361
362 tr->blkcore_priv->rq = blk_init_queue(mtd_blktrans_request, &tr->blkcore_priv->queue_lock);
363 if (!tr->blkcore_priv->rq) {
364 unregister_blkdev(tr->major, tr->name);
365 kfree(tr->blkcore_priv);
366 mutex_unlock(&mtd_table_mutex);
367 return -ENOMEM;
368 }
369
370 tr->blkcore_priv->rq->queuedata = tr;
371 blk_queue_logical_block_size(tr->blkcore_priv->rq, tr->blksize);
372 if (tr->discard)
373 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
374 tr->blkcore_priv->rq);
375 492
376 tr->blkshift = ffs(tr->blksize) - 1; 493 tr->blkshift = ffs(tr->blksize) - 1;
377 494
378 tr->blkcore_priv->thread = kthread_run(mtd_blktrans_thread, tr,
379 "%sd", tr->name);
380 if (IS_ERR(tr->blkcore_priv->thread)) {
381 ret = PTR_ERR(tr->blkcore_priv->thread);
382 blk_cleanup_queue(tr->blkcore_priv->rq);
383 unregister_blkdev(tr->major, tr->name);
384 kfree(tr->blkcore_priv);
385 mutex_unlock(&mtd_table_mutex);
386 return ret;
387 }
388
389 INIT_LIST_HEAD(&tr->devs); 495 INIT_LIST_HEAD(&tr->devs);
390 list_add(&tr->list, &blktrans_majors); 496 list_add(&tr->list, &blktrans_majors);
391 497
392 for (i=0; i<MAX_MTD_DEVICES; i++) { 498 mtd_for_each_device(mtd)
393 if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT) 499 if (mtd->type != MTD_ABSENT)
394 tr->add_mtd(tr, mtd_table[i]); 500 tr->add_mtd(tr, mtd);
395 }
396 501
397 mutex_unlock(&mtd_table_mutex); 502 mutex_unlock(&mtd_table_mutex);
398
399 return 0; 503 return 0;
400} 504}
401 505
@@ -405,22 +509,15 @@ int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
405 509
406 mutex_lock(&mtd_table_mutex); 510 mutex_lock(&mtd_table_mutex);
407 511
408 /* Clean up the kernel thread */
409 kthread_stop(tr->blkcore_priv->thread);
410
411 /* Remove it from the list of active majors */ 512 /* Remove it from the list of active majors */
412 list_del(&tr->list); 513 list_del(&tr->list);
413 514
414 list_for_each_entry_safe(dev, next, &tr->devs, list) 515 list_for_each_entry_safe(dev, next, &tr->devs, list)
415 tr->remove_dev(dev); 516 tr->remove_dev(dev);
416 517
417 blk_cleanup_queue(tr->blkcore_priv->rq);
418 unregister_blkdev(tr->major, tr->name); 518 unregister_blkdev(tr->major, tr->name);
419
420 mutex_unlock(&mtd_table_mutex); 519 mutex_unlock(&mtd_table_mutex);
421 520
422 kfree(tr->blkcore_priv);
423
424 BUG_ON(!list_empty(&tr->devs)); 521 BUG_ON(!list_empty(&tr->devs));
425 return 0; 522 return 0;
426} 523}
diff --git a/drivers/mtd/mtdblock.c b/drivers/mtd/mtdblock.c
index 9f41b1a853c1..7ce30a239ada 100644
--- a/drivers/mtd/mtdblock.c
+++ b/drivers/mtd/mtdblock.c
@@ -19,15 +19,15 @@
19#include <linux/mutex.h> 19#include <linux/mutex.h>
20 20
21 21
22static struct mtdblk_dev { 22struct mtdblk_dev {
23 struct mtd_info *mtd; 23 struct mtd_blktrans_dev mbd;
24 int count; 24 int count;
25 struct mutex cache_mutex; 25 struct mutex cache_mutex;
26 unsigned char *cache_data; 26 unsigned char *cache_data;
27 unsigned long cache_offset; 27 unsigned long cache_offset;
28 unsigned int cache_size; 28 unsigned int cache_size;
29 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state; 29 enum { STATE_EMPTY, STATE_CLEAN, STATE_DIRTY } cache_state;
30} *mtdblks[MAX_MTD_DEVICES]; 30};
31 31
32static struct mutex mtdblks_lock; 32static struct mutex mtdblks_lock;
33 33
@@ -98,7 +98,7 @@ static int erase_write (struct mtd_info *mtd, unsigned long pos,
98 98
99static int write_cached_data (struct mtdblk_dev *mtdblk) 99static int write_cached_data (struct mtdblk_dev *mtdblk)
100{ 100{
101 struct mtd_info *mtd = mtdblk->mtd; 101 struct mtd_info *mtd = mtdblk->mbd.mtd;
102 int ret; 102 int ret;
103 103
104 if (mtdblk->cache_state != STATE_DIRTY) 104 if (mtdblk->cache_state != STATE_DIRTY)
@@ -128,7 +128,7 @@ static int write_cached_data (struct mtdblk_dev *mtdblk)
128static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos, 128static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
129 int len, const char *buf) 129 int len, const char *buf)
130{ 130{
131 struct mtd_info *mtd = mtdblk->mtd; 131 struct mtd_info *mtd = mtdblk->mbd.mtd;
132 unsigned int sect_size = mtdblk->cache_size; 132 unsigned int sect_size = mtdblk->cache_size;
133 size_t retlen; 133 size_t retlen;
134 int ret; 134 int ret;
@@ -198,7 +198,7 @@ static int do_cached_write (struct mtdblk_dev *mtdblk, unsigned long pos,
198static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos, 198static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
199 int len, char *buf) 199 int len, char *buf)
200{ 200{
201 struct mtd_info *mtd = mtdblk->mtd; 201 struct mtd_info *mtd = mtdblk->mbd.mtd;
202 unsigned int sect_size = mtdblk->cache_size; 202 unsigned int sect_size = mtdblk->cache_size;
203 size_t retlen; 203 size_t retlen;
204 int ret; 204 int ret;
@@ -244,16 +244,16 @@ static int do_cached_read (struct mtdblk_dev *mtdblk, unsigned long pos,
244static int mtdblock_readsect(struct mtd_blktrans_dev *dev, 244static int mtdblock_readsect(struct mtd_blktrans_dev *dev,
245 unsigned long block, char *buf) 245 unsigned long block, char *buf)
246{ 246{
247 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum]; 247 struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
248 return do_cached_read(mtdblk, block<<9, 512, buf); 248 return do_cached_read(mtdblk, block<<9, 512, buf);
249} 249}
250 250
251static int mtdblock_writesect(struct mtd_blktrans_dev *dev, 251static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
252 unsigned long block, char *buf) 252 unsigned long block, char *buf)
253{ 253{
254 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum]; 254 struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
255 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) { 255 if (unlikely(!mtdblk->cache_data && mtdblk->cache_size)) {
256 mtdblk->cache_data = vmalloc(mtdblk->mtd->erasesize); 256 mtdblk->cache_data = vmalloc(mtdblk->mbd.mtd->erasesize);
257 if (!mtdblk->cache_data) 257 if (!mtdblk->cache_data)
258 return -EINTR; 258 return -EINTR;
259 /* -EINTR is not really correct, but it is the best match 259 /* -EINTR is not really correct, but it is the best match
@@ -266,37 +266,26 @@ static int mtdblock_writesect(struct mtd_blktrans_dev *dev,
266 266
267static int mtdblock_open(struct mtd_blktrans_dev *mbd) 267static int mtdblock_open(struct mtd_blktrans_dev *mbd)
268{ 268{
269 struct mtdblk_dev *mtdblk; 269 struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
270 struct mtd_info *mtd = mbd->mtd;
271 int dev = mbd->devnum;
272 270
273 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n"); 271 DEBUG(MTD_DEBUG_LEVEL1,"mtdblock_open\n");
274 272
275 mutex_lock(&mtdblks_lock); 273 mutex_lock(&mtdblks_lock);
276 if (mtdblks[dev]) { 274 if (mtdblk->count) {
277 mtdblks[dev]->count++; 275 mtdblk->count++;
278 mutex_unlock(&mtdblks_lock); 276 mutex_unlock(&mtdblks_lock);
279 return 0; 277 return 0;
280 } 278 }
281 279
282 /* OK, it's not open. Create cache info for it */ 280 /* OK, it's not open. Create cache info for it */
283 mtdblk = kzalloc(sizeof(struct mtdblk_dev), GFP_KERNEL);
284 if (!mtdblk) {
285 mutex_unlock(&mtdblks_lock);
286 return -ENOMEM;
287 }
288
289 mtdblk->count = 1; 281 mtdblk->count = 1;
290 mtdblk->mtd = mtd;
291
292 mutex_init(&mtdblk->cache_mutex); 282 mutex_init(&mtdblk->cache_mutex);
293 mtdblk->cache_state = STATE_EMPTY; 283 mtdblk->cache_state = STATE_EMPTY;
294 if ( !(mtdblk->mtd->flags & MTD_NO_ERASE) && mtdblk->mtd->erasesize) { 284 if (!(mbd->mtd->flags & MTD_NO_ERASE) && mbd->mtd->erasesize) {
295 mtdblk->cache_size = mtdblk->mtd->erasesize; 285 mtdblk->cache_size = mbd->mtd->erasesize;
296 mtdblk->cache_data = NULL; 286 mtdblk->cache_data = NULL;
297 } 287 }
298 288
299 mtdblks[dev] = mtdblk;
300 mutex_unlock(&mtdblks_lock); 289 mutex_unlock(&mtdblks_lock);
301 290
302 DEBUG(MTD_DEBUG_LEVEL1, "ok\n"); 291 DEBUG(MTD_DEBUG_LEVEL1, "ok\n");
@@ -306,8 +295,7 @@ static int mtdblock_open(struct mtd_blktrans_dev *mbd)
306 295
307static int mtdblock_release(struct mtd_blktrans_dev *mbd) 296static int mtdblock_release(struct mtd_blktrans_dev *mbd)
308{ 297{
309 int dev = mbd->devnum; 298 struct mtdblk_dev *mtdblk = container_of(mbd, struct mtdblk_dev, mbd);
310 struct mtdblk_dev *mtdblk = mtdblks[dev];
311 299
312 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n"); 300 DEBUG(MTD_DEBUG_LEVEL1, "mtdblock_release\n");
313 301
@@ -318,12 +306,10 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
318 mutex_unlock(&mtdblk->cache_mutex); 306 mutex_unlock(&mtdblk->cache_mutex);
319 307
320 if (!--mtdblk->count) { 308 if (!--mtdblk->count) {
321 /* It was the last usage. Free the device */ 309 /* It was the last usage. Free the cache */
322 mtdblks[dev] = NULL; 310 if (mbd->mtd->sync)
323 if (mtdblk->mtd->sync) 311 mbd->mtd->sync(mbd->mtd);
324 mtdblk->mtd->sync(mtdblk->mtd);
325 vfree(mtdblk->cache_data); 312 vfree(mtdblk->cache_data);
326 kfree(mtdblk);
327 } 313 }
328 314
329 mutex_unlock(&mtdblks_lock); 315 mutex_unlock(&mtdblks_lock);
@@ -335,40 +321,41 @@ static int mtdblock_release(struct mtd_blktrans_dev *mbd)
335 321
336static int mtdblock_flush(struct mtd_blktrans_dev *dev) 322static int mtdblock_flush(struct mtd_blktrans_dev *dev)
337{ 323{
338 struct mtdblk_dev *mtdblk = mtdblks[dev->devnum]; 324 struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
339 325
340 mutex_lock(&mtdblk->cache_mutex); 326 mutex_lock(&mtdblk->cache_mutex);
341 write_cached_data(mtdblk); 327 write_cached_data(mtdblk);
342 mutex_unlock(&mtdblk->cache_mutex); 328 mutex_unlock(&mtdblk->cache_mutex);
343 329
344 if (mtdblk->mtd->sync) 330 if (dev->mtd->sync)
345 mtdblk->mtd->sync(mtdblk->mtd); 331 dev->mtd->sync(dev->mtd);
346 return 0; 332 return 0;
347} 333}
348 334
349static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd) 335static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
350{ 336{
351 struct mtd_blktrans_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL); 337 struct mtdblk_dev *dev = kzalloc(sizeof(*dev), GFP_KERNEL);
352 338
353 if (!dev) 339 if (!dev)
354 return; 340 return;
355 341
356 dev->mtd = mtd; 342 dev->mbd.mtd = mtd;
357 dev->devnum = mtd->index; 343 dev->mbd.devnum = mtd->index;
358 344
359 dev->size = mtd->size >> 9; 345 dev->mbd.size = mtd->size >> 9;
360 dev->tr = tr; 346 dev->mbd.tr = tr;
361 347
362 if (!(mtd->flags & MTD_WRITEABLE)) 348 if (!(mtd->flags & MTD_WRITEABLE))
363 dev->readonly = 1; 349 dev->mbd.readonly = 1;
364 350
365 add_mtd_blktrans_dev(dev); 351 if (add_mtd_blktrans_dev(&dev->mbd))
352 kfree(dev);
366} 353}
367 354
368static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev) 355static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
369{ 356{
357 struct mtdblk_dev *mtdblk = container_of(dev, struct mtdblk_dev, mbd);
370 del_mtd_blktrans_dev(dev); 358 del_mtd_blktrans_dev(dev);
371 kfree(dev);
372} 359}
373 360
374static struct mtd_blktrans_ops mtdblock_tr = { 361static struct mtd_blktrans_ops mtdblock_tr = {
diff --git a/drivers/mtd/mtdblock_ro.c b/drivers/mtd/mtdblock_ro.c
index 852165f8b1c3..d0d3f79f9d03 100644
--- a/drivers/mtd/mtdblock_ro.c
+++ b/drivers/mtd/mtdblock_ro.c
@@ -43,13 +43,13 @@ static void mtdblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
43 dev->tr = tr; 43 dev->tr = tr;
44 dev->readonly = 1; 44 dev->readonly = 1;
45 45
46 add_mtd_blktrans_dev(dev); 46 if (add_mtd_blktrans_dev(dev))
47 kfree(dev);
47} 48}
48 49
49static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev) 50static void mtdblock_remove_dev(struct mtd_blktrans_dev *dev)
50{ 51{
51 del_mtd_blktrans_dev(dev); 52 del_mtd_blktrans_dev(dev);
52 kfree(dev);
53} 53}
54 54
55static struct mtd_blktrans_ops mtdblock_tr = { 55static struct mtd_blktrans_ops mtdblock_tr = {
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 5b081cb84351..c355491d1326 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -67,9 +67,6 @@ static int mtd_open(struct inode *inode, struct file *file)
67 67
68 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n"); 68 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
69 69
70 if (devnum >= MAX_MTD_DEVICES)
71 return -ENODEV;
72
73 /* You can't open the RO devices RW */ 70 /* You can't open the RO devices RW */
74 if ((file->f_mode & FMODE_WRITE) && (minor & 1)) 71 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
75 return -EACCES; 72 return -EACCES;
@@ -373,7 +370,7 @@ static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
373 if (!mtd->write_oob) 370 if (!mtd->write_oob)
374 ret = -EOPNOTSUPP; 371 ret = -EOPNOTSUPP;
375 else 372 else
376 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : EFAULT; 373 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
377 374
378 if (ret) 375 if (ret)
379 return ret; 376 return ret;
@@ -482,7 +479,7 @@ static int mtd_ioctl(struct inode *inode, struct file *file,
482 { 479 {
483 uint32_t ur_idx; 480 uint32_t ur_idx;
484 struct mtd_erase_region_info *kr; 481 struct mtd_erase_region_info *kr;
485 struct region_info_user *ur = (struct region_info_user *) argp; 482 struct region_info_user __user *ur = argp;
486 483
487 if (get_user(ur_idx, &(ur->regionindex))) 484 if (get_user(ur_idx, &(ur->regionindex)))
488 return -EFAULT; 485 return -EFAULT;
@@ -958,7 +955,8 @@ static int __init init_mtdchar(void)
958{ 955{
959 int status; 956 int status;
960 957
961 status = register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops); 958 status = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
959 "mtd", &mtd_fops);
962 if (status < 0) { 960 if (status < 0) {
963 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n", 961 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
964 MTD_CHAR_MAJOR); 962 MTD_CHAR_MAJOR);
@@ -969,7 +967,7 @@ static int __init init_mtdchar(void)
969 967
970static void __exit cleanup_mtdchar(void) 968static void __exit cleanup_mtdchar(void)
971{ 969{
972 unregister_chrdev(MTD_CHAR_MAJOR, "mtd"); 970 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
973} 971}
974 972
975module_init(init_mtdchar); 973module_init(init_mtdchar);
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index c356c0a30c3e..70a78587c3c0 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -17,6 +17,7 @@
17#include <linux/init.h> 17#include <linux/init.h>
18#include <linux/mtd/compatmac.h> 18#include <linux/mtd/compatmac.h>
19#include <linux/proc_fs.h> 19#include <linux/proc_fs.h>
20#include <linux/idr.h>
20 21
21#include <linux/mtd/mtd.h> 22#include <linux/mtd/mtd.h>
22#include "internal.h" 23#include "internal.h"
@@ -33,13 +34,18 @@ static struct class mtd_class = {
33 .resume = mtd_cls_resume, 34 .resume = mtd_cls_resume,
34}; 35};
35 36
37static DEFINE_IDR(mtd_idr);
38
36/* These are exported solely for the purpose of mtd_blkdevs.c. You 39/* These are exported solely for the purpose of mtd_blkdevs.c. You
37 should not use them for _anything_ else */ 40 should not use them for _anything_ else */
38DEFINE_MUTEX(mtd_table_mutex); 41DEFINE_MUTEX(mtd_table_mutex);
39struct mtd_info *mtd_table[MAX_MTD_DEVICES];
40
41EXPORT_SYMBOL_GPL(mtd_table_mutex); 42EXPORT_SYMBOL_GPL(mtd_table_mutex);
42EXPORT_SYMBOL_GPL(mtd_table); 43
44struct mtd_info *__mtd_next_device(int i)
45{
46 return idr_get_next(&mtd_idr, &i);
47}
48EXPORT_SYMBOL_GPL(__mtd_next_device);
43 49
44static LIST_HEAD(mtd_notifiers); 50static LIST_HEAD(mtd_notifiers);
45 51
@@ -235,13 +241,13 @@ static struct device_type mtd_devtype = {
235 * Add a device to the list of MTD devices present in the system, and 241 * Add a device to the list of MTD devices present in the system, and
236 * notify each currently active MTD 'user' of its arrival. Returns 242 * notify each currently active MTD 'user' of its arrival. Returns
237 * zero on success or 1 on failure, which currently will only happen 243 * zero on success or 1 on failure, which currently will only happen
238 * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16) 244 * if there is insufficient memory or a sysfs error.
239 * or there's a sysfs error.
240 */ 245 */
241 246
242int add_mtd_device(struct mtd_info *mtd) 247int add_mtd_device(struct mtd_info *mtd)
243{ 248{
244 int i; 249 struct mtd_notifier *not;
250 int i, error;
245 251
246 if (!mtd->backing_dev_info) { 252 if (!mtd->backing_dev_info) {
247 switch (mtd->type) { 253 switch (mtd->type) {
@@ -260,71 +266,74 @@ int add_mtd_device(struct mtd_info *mtd)
260 BUG_ON(mtd->writesize == 0); 266 BUG_ON(mtd->writesize == 0);
261 mutex_lock(&mtd_table_mutex); 267 mutex_lock(&mtd_table_mutex);
262 268
263 for (i=0; i < MAX_MTD_DEVICES; i++) 269 do {
264 if (!mtd_table[i]) { 270 if (!idr_pre_get(&mtd_idr, GFP_KERNEL))
265 struct mtd_notifier *not; 271 goto fail_locked;
266 272 error = idr_get_new(&mtd_idr, mtd, &i);
267 mtd_table[i] = mtd; 273 } while (error == -EAGAIN);
268 mtd->index = i;
269 mtd->usecount = 0;
270
271 if (is_power_of_2(mtd->erasesize))
272 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
273 else
274 mtd->erasesize_shift = 0;
275
276 if (is_power_of_2(mtd->writesize))
277 mtd->writesize_shift = ffs(mtd->writesize) - 1;
278 else
279 mtd->writesize_shift = 0;
280
281 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
282 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
283
284 /* Some chips always power up locked. Unlock them now */
285 if ((mtd->flags & MTD_WRITEABLE)
286 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
287 if (mtd->unlock(mtd, 0, mtd->size))
288 printk(KERN_WARNING
289 "%s: unlock failed, "
290 "writes may not work\n",
291 mtd->name);
292 }
293 274
294 /* Caller should have set dev.parent to match the 275 if (error)
295 * physical device. 276 goto fail_locked;
296 */
297 mtd->dev.type = &mtd_devtype;
298 mtd->dev.class = &mtd_class;
299 mtd->dev.devt = MTD_DEVT(i);
300 dev_set_name(&mtd->dev, "mtd%d", i);
301 dev_set_drvdata(&mtd->dev, mtd);
302 if (device_register(&mtd->dev) != 0) {
303 mtd_table[i] = NULL;
304 break;
305 }
306 277
307 if (MTD_DEVT(i)) 278 mtd->index = i;
308 device_create(&mtd_class, mtd->dev.parent, 279 mtd->usecount = 0;
309 MTD_DEVT(i) + 1, 280
310 NULL, "mtd%dro", i); 281 if (is_power_of_2(mtd->erasesize))
311 282 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
312 DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name); 283 else
313 /* No need to get a refcount on the module containing 284 mtd->erasesize_shift = 0;
314 the notifier, since we hold the mtd_table_mutex */ 285
315 list_for_each_entry(not, &mtd_notifiers, list) 286 if (is_power_of_2(mtd->writesize))
316 not->add(mtd); 287 mtd->writesize_shift = ffs(mtd->writesize) - 1;
317 288 else
318 mutex_unlock(&mtd_table_mutex); 289 mtd->writesize_shift = 0;
319 /* We _know_ we aren't being removed, because 290
320 our caller is still holding us here. So none 291 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
321 of this try_ nonsense, and no bitching about it 292 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
322 either. :) */ 293
323 __module_get(THIS_MODULE); 294 /* Some chips always power up locked. Unlock them now */
324 return 0; 295 if ((mtd->flags & MTD_WRITEABLE)
325 } 296 && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
297 if (mtd->unlock(mtd, 0, mtd->size))
298 printk(KERN_WARNING
299 "%s: unlock failed, writes may not work\n",
300 mtd->name);
301 }
302
303 /* Caller should have set dev.parent to match the
304 * physical device.
305 */
306 mtd->dev.type = &mtd_devtype;
307 mtd->dev.class = &mtd_class;
308 mtd->dev.devt = MTD_DEVT(i);
309 dev_set_name(&mtd->dev, "mtd%d", i);
310 dev_set_drvdata(&mtd->dev, mtd);
311 if (device_register(&mtd->dev) != 0)
312 goto fail_added;
313
314 if (MTD_DEVT(i))
315 device_create(&mtd_class, mtd->dev.parent,
316 MTD_DEVT(i) + 1,
317 NULL, "mtd%dro", i);
318
319 DEBUG(0, "mtd: Giving out device %d to %s\n", i, mtd->name);
320 /* No need to get a refcount on the module containing
321 the notifier, since we hold the mtd_table_mutex */
322 list_for_each_entry(not, &mtd_notifiers, list)
323 not->add(mtd);
326 324
327 mutex_unlock(&mtd_table_mutex); 325 mutex_unlock(&mtd_table_mutex);
326 /* We _know_ we aren't being removed, because
327 our caller is still holding us here. So none
328 of this try_ nonsense, and no bitching about it
329 either. :) */
330 __module_get(THIS_MODULE);
331 return 0;
332
333fail_added:
334 idr_remove(&mtd_idr, i);
335fail_locked:
336 mutex_unlock(&mtd_table_mutex);
328 return 1; 337 return 1;
329} 338}
330 339
@@ -341,31 +350,34 @@ int add_mtd_device(struct mtd_info *mtd)
341int del_mtd_device (struct mtd_info *mtd) 350int del_mtd_device (struct mtd_info *mtd)
342{ 351{
343 int ret; 352 int ret;
353 struct mtd_notifier *not;
344 354
345 mutex_lock(&mtd_table_mutex); 355 mutex_lock(&mtd_table_mutex);
346 356
347 if (mtd_table[mtd->index] != mtd) { 357 if (idr_find(&mtd_idr, mtd->index) != mtd) {
348 ret = -ENODEV; 358 ret = -ENODEV;
349 } else if (mtd->usecount) { 359 goto out_error;
360 }
361
362 /* No need to get a refcount on the module containing
363 the notifier, since we hold the mtd_table_mutex */
364 list_for_each_entry(not, &mtd_notifiers, list)
365 not->remove(mtd);
366
367 if (mtd->usecount) {
350 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n", 368 printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
351 mtd->index, mtd->name, mtd->usecount); 369 mtd->index, mtd->name, mtd->usecount);
352 ret = -EBUSY; 370 ret = -EBUSY;
353 } else { 371 } else {
354 struct mtd_notifier *not;
355
356 device_unregister(&mtd->dev); 372 device_unregister(&mtd->dev);
357 373
358 /* No need to get a refcount on the module containing 374 idr_remove(&mtd_idr, mtd->index);
359 the notifier, since we hold the mtd_table_mutex */
360 list_for_each_entry(not, &mtd_notifiers, list)
361 not->remove(mtd);
362
363 mtd_table[mtd->index] = NULL;
364 375
365 module_put(THIS_MODULE); 376 module_put(THIS_MODULE);
366 ret = 0; 377 ret = 0;
367 } 378 }
368 379
380out_error:
369 mutex_unlock(&mtd_table_mutex); 381 mutex_unlock(&mtd_table_mutex);
370 return ret; 382 return ret;
371} 383}
@@ -381,7 +393,7 @@ int del_mtd_device (struct mtd_info *mtd)
381 393
382void register_mtd_user (struct mtd_notifier *new) 394void register_mtd_user (struct mtd_notifier *new)
383{ 395{
384 int i; 396 struct mtd_info *mtd;
385 397
386 mutex_lock(&mtd_table_mutex); 398 mutex_lock(&mtd_table_mutex);
387 399
@@ -389,9 +401,8 @@ void register_mtd_user (struct mtd_notifier *new)
389 401
390 __module_get(THIS_MODULE); 402 __module_get(THIS_MODULE);
391 403
392 for (i=0; i< MAX_MTD_DEVICES; i++) 404 mtd_for_each_device(mtd)
393 if (mtd_table[i]) 405 new->add(mtd);
394 new->add(mtd_table[i]);
395 406
396 mutex_unlock(&mtd_table_mutex); 407 mutex_unlock(&mtd_table_mutex);
397} 408}
@@ -408,15 +419,14 @@ void register_mtd_user (struct mtd_notifier *new)
408 419
409int unregister_mtd_user (struct mtd_notifier *old) 420int unregister_mtd_user (struct mtd_notifier *old)
410{ 421{
411 int i; 422 struct mtd_info *mtd;
412 423
413 mutex_lock(&mtd_table_mutex); 424 mutex_lock(&mtd_table_mutex);
414 425
415 module_put(THIS_MODULE); 426 module_put(THIS_MODULE);
416 427
417 for (i=0; i< MAX_MTD_DEVICES; i++) 428 mtd_for_each_device(mtd)
418 if (mtd_table[i]) 429 old->remove(mtd);
419 old->remove(mtd_table[i]);
420 430
421 list_del(&old->list); 431 list_del(&old->list);
422 mutex_unlock(&mtd_table_mutex); 432 mutex_unlock(&mtd_table_mutex);
@@ -438,42 +448,56 @@ int unregister_mtd_user (struct mtd_notifier *old)
438 448
439struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num) 449struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
440{ 450{
441 struct mtd_info *ret = NULL; 451 struct mtd_info *ret = NULL, *other;
442 int i, err = -ENODEV; 452 int err = -ENODEV;
443 453
444 mutex_lock(&mtd_table_mutex); 454 mutex_lock(&mtd_table_mutex);
445 455
446 if (num == -1) { 456 if (num == -1) {
447 for (i=0; i< MAX_MTD_DEVICES; i++) 457 mtd_for_each_device(other) {
448 if (mtd_table[i] == mtd) 458 if (other == mtd) {
449 ret = mtd_table[i]; 459 ret = mtd;
450 } else if (num >= 0 && num < MAX_MTD_DEVICES) { 460 break;
451 ret = mtd_table[num]; 461 }
462 }
463 } else if (num >= 0) {
464 ret = idr_find(&mtd_idr, num);
452 if (mtd && mtd != ret) 465 if (mtd && mtd != ret)
453 ret = NULL; 466 ret = NULL;
454 } 467 }
455 468
456 if (!ret) 469 if (!ret) {
457 goto out_unlock; 470 ret = ERR_PTR(err);
458 471 goto out;
459 if (!try_module_get(ret->owner))
460 goto out_unlock;
461
462 if (ret->get_device) {
463 err = ret->get_device(ret);
464 if (err)
465 goto out_put;
466 } 472 }
467 473
468 ret->usecount++; 474 err = __get_mtd_device(ret);
475 if (err)
476 ret = ERR_PTR(err);
477out:
469 mutex_unlock(&mtd_table_mutex); 478 mutex_unlock(&mtd_table_mutex);
470 return ret; 479 return ret;
480}
471 481
472out_put: 482
473 module_put(ret->owner); 483int __get_mtd_device(struct mtd_info *mtd)
474out_unlock: 484{
475 mutex_unlock(&mtd_table_mutex); 485 int err;
476 return ERR_PTR(err); 486
487 if (!try_module_get(mtd->owner))
488 return -ENODEV;
489
490 if (mtd->get_device) {
491
492 err = mtd->get_device(mtd);
493
494 if (err) {
495 module_put(mtd->owner);
496 return err;
497 }
498 }
499 mtd->usecount++;
500 return 0;
477} 501}
478 502
479/** 503/**
@@ -487,14 +511,14 @@ out_unlock:
487 511
488struct mtd_info *get_mtd_device_nm(const char *name) 512struct mtd_info *get_mtd_device_nm(const char *name)
489{ 513{
490 int i, err = -ENODEV; 514 int err = -ENODEV;
491 struct mtd_info *mtd = NULL; 515 struct mtd_info *mtd = NULL, *other;
492 516
493 mutex_lock(&mtd_table_mutex); 517 mutex_lock(&mtd_table_mutex);
494 518
495 for (i = 0; i < MAX_MTD_DEVICES; i++) { 519 mtd_for_each_device(other) {
496 if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) { 520 if (!strcmp(name, other->name)) {
497 mtd = mtd_table[i]; 521 mtd = other;
498 break; 522 break;
499 } 523 }
500 } 524 }
@@ -524,14 +548,19 @@ out_unlock:
524 548
525void put_mtd_device(struct mtd_info *mtd) 549void put_mtd_device(struct mtd_info *mtd)
526{ 550{
527 int c;
528
529 mutex_lock(&mtd_table_mutex); 551 mutex_lock(&mtd_table_mutex);
530 c = --mtd->usecount; 552 __put_mtd_device(mtd);
553 mutex_unlock(&mtd_table_mutex);
554
555}
556
557void __put_mtd_device(struct mtd_info *mtd)
558{
559 --mtd->usecount;
560 BUG_ON(mtd->usecount < 0);
561
531 if (mtd->put_device) 562 if (mtd->put_device)
532 mtd->put_device(mtd); 563 mtd->put_device(mtd);
533 mutex_unlock(&mtd_table_mutex);
534 BUG_ON(c < 0);
535 564
536 module_put(mtd->owner); 565 module_put(mtd->owner);
537} 566}
@@ -569,7 +598,9 @@ EXPORT_SYMBOL_GPL(add_mtd_device);
569EXPORT_SYMBOL_GPL(del_mtd_device); 598EXPORT_SYMBOL_GPL(del_mtd_device);
570EXPORT_SYMBOL_GPL(get_mtd_device); 599EXPORT_SYMBOL_GPL(get_mtd_device);
571EXPORT_SYMBOL_GPL(get_mtd_device_nm); 600EXPORT_SYMBOL_GPL(get_mtd_device_nm);
601EXPORT_SYMBOL_GPL(__get_mtd_device);
572EXPORT_SYMBOL_GPL(put_mtd_device); 602EXPORT_SYMBOL_GPL(put_mtd_device);
603EXPORT_SYMBOL_GPL(__put_mtd_device);
573EXPORT_SYMBOL_GPL(register_mtd_user); 604EXPORT_SYMBOL_GPL(register_mtd_user);
574EXPORT_SYMBOL_GPL(unregister_mtd_user); 605EXPORT_SYMBOL_GPL(unregister_mtd_user);
575EXPORT_SYMBOL_GPL(default_mtd_writev); 606EXPORT_SYMBOL_GPL(default_mtd_writev);
@@ -581,14 +612,9 @@ EXPORT_SYMBOL_GPL(default_mtd_writev);
581 612
582static struct proc_dir_entry *proc_mtd; 613static struct proc_dir_entry *proc_mtd;
583 614
584static inline int mtd_proc_info (char *buf, int i) 615static inline int mtd_proc_info(char *buf, struct mtd_info *this)
585{ 616{
586 struct mtd_info *this = mtd_table[i]; 617 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", this->index,
587
588 if (!this)
589 return 0;
590
591 return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
592 (unsigned long long)this->size, 618 (unsigned long long)this->size,
593 this->erasesize, this->name); 619 this->erasesize, this->name);
594} 620}
@@ -596,15 +622,15 @@ static inline int mtd_proc_info (char *buf, int i)
596static int mtd_read_proc (char *page, char **start, off_t off, int count, 622static int mtd_read_proc (char *page, char **start, off_t off, int count,
597 int *eof, void *data_unused) 623 int *eof, void *data_unused)
598{ 624{
599 int len, l, i; 625 struct mtd_info *mtd;
626 int len, l;
600 off_t begin = 0; 627 off_t begin = 0;
601 628
602 mutex_lock(&mtd_table_mutex); 629 mutex_lock(&mtd_table_mutex);
603 630
604 len = sprintf(page, "dev: size erasesize name\n"); 631 len = sprintf(page, "dev: size erasesize name\n");
605 for (i=0; i< MAX_MTD_DEVICES; i++) { 632 mtd_for_each_device(mtd) {
606 633 l = mtd_proc_info(page + len, mtd);
607 l = mtd_proc_info(page + len, i);
608 len += l; 634 len += l;
609 if (len+begin > off+count) 635 if (len+begin > off+count)
610 goto done; 636 goto done;
diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h
index a33251f4b872..6a64fdebc898 100644
--- a/drivers/mtd/mtdcore.h
+++ b/drivers/mtd/mtdcore.h
@@ -8,4 +8,9 @@
8 should not use them for _anything_ else */ 8 should not use them for _anything_ else */
9 9
10extern struct mutex mtd_table_mutex; 10extern struct mutex mtd_table_mutex;
11extern struct mtd_info *mtd_table[MAX_MTD_DEVICES]; 11extern struct mtd_info *__mtd_next_device(int i);
12
13#define mtd_for_each_device(mtd) \
14 for ((mtd) = __mtd_next_device(0); \
15 (mtd) != NULL; \
16 (mtd) = __mtd_next_device(mtd->index + 1))
diff --git a/drivers/mtd/mtdoops.c b/drivers/mtd/mtdoops.c
index 92e12df0917f..328313c3dccb 100644
--- a/drivers/mtd/mtdoops.c
+++ b/drivers/mtd/mtdoops.c
@@ -429,11 +429,6 @@ static int __init mtdoops_init(void)
429 mtd_index = simple_strtoul(mtddev, &endp, 0); 429 mtd_index = simple_strtoul(mtddev, &endp, 0);
430 if (*endp == '\0') 430 if (*endp == '\0')
431 cxt->mtd_index = mtd_index; 431 cxt->mtd_index = mtd_index;
432 if (cxt->mtd_index > MAX_MTD_DEVICES) {
433 printk(KERN_ERR "mtdoops: invalid mtd device number (%u) given\n",
434 mtd_index);
435 return -EINVAL;
436 }
437 432
438 cxt->oops_buf = vmalloc(record_size); 433 cxt->oops_buf = vmalloc(record_size);
439 if (!cxt->oops_buf) { 434 if (!cxt->oops_buf) {
diff --git a/drivers/mtd/mtdsuper.c b/drivers/mtd/mtdsuper.c
index af8b42e0a55b..d2570523d703 100644
--- a/drivers/mtd/mtdsuper.c
+++ b/drivers/mtd/mtdsuper.c
@@ -150,18 +150,12 @@ int get_sb_mtd(struct file_system_type *fs_type, int flags,
150 DEBUG(1, "MTDSB: mtd:%%s, name \"%s\"\n", 150 DEBUG(1, "MTDSB: mtd:%%s, name \"%s\"\n",
151 dev_name + 4); 151 dev_name + 4);
152 152
153 for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) { 153 mtd = get_mtd_device_nm(dev_name + 4);
154 mtd = get_mtd_device(NULL, mtdnr); 154 if (!IS_ERR(mtd))
155 if (!IS_ERR(mtd)) { 155 return get_sb_mtd_aux(
156 if (!strcmp(mtd->name, dev_name + 4)) 156 fs_type, flags,
157 return get_sb_mtd_aux( 157 dev_name, data, mtd,
158 fs_type, flags, 158 fill_super, mnt);
159 dev_name, data, mtd,
160 fill_super, mnt);
161
162 put_mtd_device(mtd);
163 }
164 }
165 159
166 printk(KERN_NOTICE "MTD:" 160 printk(KERN_NOTICE "MTD:"
167 " MTD device with name \"%s\" not found.\n", 161 " MTD device with name \"%s\" not found.\n",
diff --git a/drivers/mtd/nand/Kconfig b/drivers/mtd/nand/Kconfig
index 677cd53f18c3..7a67218e86fc 100644
--- a/drivers/mtd/nand/Kconfig
+++ b/drivers/mtd/nand/Kconfig
@@ -25,6 +25,10 @@ config MTD_NAND_ECC_SMC
25 Software ECC according to the Smart Media Specification. 25 Software ECC according to the Smart Media Specification.
26 The original Linux implementation had byte 0 and 1 swapped. 26 The original Linux implementation had byte 0 and 1 swapped.
27 27
28config MTD_SM_COMMON
29 tristate
30 default n
31
28config MTD_NAND_MUSEUM_IDS 32config MTD_NAND_MUSEUM_IDS
29 bool "Enable chip ids for obsolete ancient NAND devices" 33 bool "Enable chip ids for obsolete ancient NAND devices"
30 depends on MTD_NAND 34 depends on MTD_NAND
@@ -95,12 +99,6 @@ config MTD_NAND_OMAP_PREFETCH_DMA
95 or in DMA interrupt mode. 99 or in DMA interrupt mode.
96 Say y for DMA mode or MPU mode will be used 100 Say y for DMA mode or MPU mode will be used
97 101
98config MTD_NAND_TS7250
99 tristate "NAND Flash device on TS-7250 board"
100 depends on MACH_TS72XX
101 help
102 Support for NAND flash on Technologic Systems TS-7250 platform.
103
104config MTD_NAND_IDS 102config MTD_NAND_IDS
105 tristate 103 tristate
106 104
@@ -442,6 +440,13 @@ config MTD_NAND_FSL_UPM
442 Enables support for NAND Flash chips wired onto Freescale PowerPC 440 Enables support for NAND Flash chips wired onto Freescale PowerPC
443 processor localbus with User-Programmable Machine support. 441 processor localbus with User-Programmable Machine support.
444 442
443config MTD_NAND_MPC5121_NFC
444 tristate "MPC5121 built-in NAND Flash Controller support"
445 depends on PPC_MPC512x
446 help
447 This enables the driver for the NAND flash controller on the
448 MPC5121 SoC.
449
445config MTD_NAND_MXC 450config MTD_NAND_MXC
446 tristate "MXC NAND support" 451 tristate "MXC NAND support"
447 depends on ARCH_MX2 || ARCH_MX3 452 depends on ARCH_MX2 || ARCH_MX3
@@ -481,11 +486,11 @@ config MTD_NAND_SOCRATES
481 help 486 help
482 Enables support for NAND Flash chips wired onto Socrates board. 487 Enables support for NAND Flash chips wired onto Socrates board.
483 488
484config MTD_NAND_W90P910 489config MTD_NAND_NUC900
485 tristate "Support for NAND on w90p910 evaluation board." 490 tristate "Support for NAND on Nuvoton NUC9xx/w90p910 evaluation boards."
486 depends on ARCH_W90X900 && MTD_PARTITIONS 491 depends on ARCH_W90X900 && MTD_PARTITIONS
487 help 492 help
488 This enables the driver for the NAND Flash on evaluation board based 493 This enables the driver for the NAND Flash on evaluation board based
489 on w90p910. 494 on w90p910 / NUC9xx.
490 495
491endif # MTD_NAND 496endif # MTD_NAND
diff --git a/drivers/mtd/nand/Makefile b/drivers/mtd/nand/Makefile
index 1407bd144015..f39d0b6ed42c 100644
--- a/drivers/mtd/nand/Makefile
+++ b/drivers/mtd/nand/Makefile
@@ -4,6 +4,7 @@
4 4
5obj-$(CONFIG_MTD_NAND) += nand.o nand_ecc.o 5obj-$(CONFIG_MTD_NAND) += nand.o nand_ecc.o
6obj-$(CONFIG_MTD_NAND_IDS) += nand_ids.o 6obj-$(CONFIG_MTD_NAND_IDS) += nand_ids.o
7obj-$(CONFIG_MTD_SM_COMMON) += sm_common.o
7 8
8obj-$(CONFIG_MTD_NAND_CAFE) += cafe_nand.o 9obj-$(CONFIG_MTD_NAND_CAFE) += cafe_nand.o
9obj-$(CONFIG_MTD_NAND_SPIA) += spia.o 10obj-$(CONFIG_MTD_NAND_SPIA) += spia.o
@@ -19,7 +20,6 @@ obj-$(CONFIG_MTD_NAND_DISKONCHIP) += diskonchip.o
19obj-$(CONFIG_MTD_NAND_H1900) += h1910.o 20obj-$(CONFIG_MTD_NAND_H1900) += h1910.o
20obj-$(CONFIG_MTD_NAND_RTC_FROM4) += rtc_from4.o 21obj-$(CONFIG_MTD_NAND_RTC_FROM4) += rtc_from4.o
21obj-$(CONFIG_MTD_NAND_SHARPSL) += sharpsl.o 22obj-$(CONFIG_MTD_NAND_SHARPSL) += sharpsl.o
22obj-$(CONFIG_MTD_NAND_TS7250) += ts7250.o
23obj-$(CONFIG_MTD_NAND_NANDSIM) += nandsim.o 23obj-$(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
@@ -39,8 +39,9 @@ obj-$(CONFIG_MTD_NAND_SH_FLCTL) += sh_flctl.o
39obj-$(CONFIG_MTD_NAND_MXC) += mxc_nand.o 39obj-$(CONFIG_MTD_NAND_MXC) += mxc_nand.o
40obj-$(CONFIG_MTD_NAND_SOCRATES) += socrates_nand.o 40obj-$(CONFIG_MTD_NAND_SOCRATES) += socrates_nand.o
41obj-$(CONFIG_MTD_NAND_TXX9NDFMC) += txx9ndfmc.o 41obj-$(CONFIG_MTD_NAND_TXX9NDFMC) += txx9ndfmc.o
42obj-$(CONFIG_MTD_NAND_W90P910) += w90p910_nand.o 42obj-$(CONFIG_MTD_NAND_NUC900) += nuc900_nand.o
43obj-$(CONFIG_MTD_NAND_NOMADIK) += nomadik_nand.o 43obj-$(CONFIG_MTD_NAND_NOMADIK) += nomadik_nand.o
44obj-$(CONFIG_MTD_NAND_BCM_UMI) += bcm_umi_nand.o nand_bcm_umi.o 44obj-$(CONFIG_MTD_NAND_BCM_UMI) += bcm_umi_nand.o nand_bcm_umi.o
45obj-$(CONFIG_MTD_NAND_MPC5121_NFC) += mpc5121_nfc.o
45 46
46nand-objs := nand_base.o nand_bbt.o 47nand-objs := nand_base.o nand_bbt.o
diff --git a/drivers/mtd/nand/alauda.c b/drivers/mtd/nand/alauda.c
index 2d6773281fd9..8691e0482ed2 100644
--- a/drivers/mtd/nand/alauda.c
+++ b/drivers/mtd/nand/alauda.c
@@ -49,7 +49,7 @@
49 49
50#define TIMEOUT HZ 50#define TIMEOUT HZ
51 51
52static struct usb_device_id alauda_table [] = { 52static const struct usb_device_id alauda_table[] = {
53 { USB_DEVICE(0x0584, 0x0008) }, /* Fujifilm DPC-R1 */ 53 { USB_DEVICE(0x0584, 0x0008) }, /* Fujifilm DPC-R1 */
54 { USB_DEVICE(0x07b4, 0x010a) }, /* Olympus MAUSB-10 */ 54 { USB_DEVICE(0x07b4, 0x010a) }, /* Olympus MAUSB-10 */
55 { } 55 { }
diff --git a/drivers/mtd/nand/atmel_nand.c b/drivers/mtd/nand/atmel_nand.c
index 524e6c9e0672..04d30887ca7f 100644
--- a/drivers/mtd/nand/atmel_nand.c
+++ b/drivers/mtd/nand/atmel_nand.c
@@ -474,7 +474,7 @@ static int __init atmel_nand_probe(struct platform_device *pdev)
474 } 474 }
475 475
476 /* first scan to find the device and get the page size */ 476 /* first scan to find the device and get the page size */
477 if (nand_scan_ident(mtd, 1)) { 477 if (nand_scan_ident(mtd, 1, NULL)) {
478 res = -ENXIO; 478 res = -ENXIO;
479 goto err_scan_ident; 479 goto err_scan_ident;
480 } 480 }
diff --git a/drivers/mtd/nand/au1550nd.c b/drivers/mtd/nand/au1550nd.c
index 92c334ff4508..58c55db504e2 100644
--- a/drivers/mtd/nand/au1550nd.c
+++ b/drivers/mtd/nand/au1550nd.c
@@ -450,7 +450,7 @@ static int __init au1xxx_nand_init(void)
450 u32 nand_phys; 450 u32 nand_phys;
451 451
452 /* Allocate memory for MTD device structure and private data */ 452 /* Allocate memory for MTD device structure and private data */
453 au1550_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL); 453 au1550_mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
454 if (!au1550_mtd) { 454 if (!au1550_mtd) {
455 printk("Unable to allocate NAND MTD dev structure.\n"); 455 printk("Unable to allocate NAND MTD dev structure.\n");
456 return -ENOMEM; 456 return -ENOMEM;
@@ -459,10 +459,6 @@ static int __init au1xxx_nand_init(void)
459 /* Get pointer to private data */ 459 /* Get pointer to private data */
460 this = (struct nand_chip *)(&au1550_mtd[1]); 460 this = (struct nand_chip *)(&au1550_mtd[1]);
461 461
462 /* Initialize structures */
463 memset(au1550_mtd, 0, sizeof(struct mtd_info));
464 memset(this, 0, sizeof(struct nand_chip));
465
466 /* Link the private data with the MTD structure */ 462 /* Link the private data with the MTD structure */
467 au1550_mtd->priv = this; 463 au1550_mtd->priv = this;
468 au1550_mtd->owner = THIS_MODULE; 464 au1550_mtd->owner = THIS_MODULE;
@@ -542,7 +538,7 @@ static int __init au1xxx_nand_init(void)
542 } 538 }
543 nand_phys = (mem_staddr << 4) & 0xFFFC0000; 539 nand_phys = (mem_staddr << 4) & 0xFFFC0000;
544 540
545 p_nand = (void __iomem *)ioremap(nand_phys, 0x1000); 541 p_nand = ioremap(nand_phys, 0x1000);
546 542
547 /* make controller and MTD agree */ 543 /* make controller and MTD agree */
548 if (NAND_CS == 0) 544 if (NAND_CS == 0)
@@ -587,7 +583,7 @@ static int __init au1xxx_nand_init(void)
587 return 0; 583 return 0;
588 584
589 outio: 585 outio:
590 iounmap((void *)p_nand); 586 iounmap(p_nand);
591 587
592 outmem: 588 outmem:
593 kfree(au1550_mtd); 589 kfree(au1550_mtd);
@@ -608,7 +604,7 @@ static void __exit au1550_cleanup(void)
608 kfree(au1550_mtd); 604 kfree(au1550_mtd);
609 605
610 /* Unmap */ 606 /* Unmap */
611 iounmap((void *)p_nand); 607 iounmap(p_nand);
612} 608}
613 609
614module_exit(au1550_cleanup); 610module_exit(au1550_cleanup);
diff --git a/drivers/mtd/nand/bcm_umi_nand.c b/drivers/mtd/nand/bcm_umi_nand.c
index 087bcd745bb7..5ff90b7e565e 100644
--- a/drivers/mtd/nand/bcm_umi_nand.c
+++ b/drivers/mtd/nand/bcm_umi_nand.c
@@ -446,7 +446,7 @@ static int __devinit bcm_umi_nand_probe(struct platform_device *pdev)
446 * layout we'll be using. 446 * layout we'll be using.
447 */ 447 */
448 448
449 err = nand_scan_ident(board_mtd, 1); 449 err = nand_scan_ident(board_mtd, 1, NULL);
450 if (err) { 450 if (err) {
451 printk(KERN_ERR "nand_scan failed: %d\n", err); 451 printk(KERN_ERR "nand_scan failed: %d\n", err);
452 iounmap(bcm_umi_io_base); 452 iounmap(bcm_umi_io_base);
diff --git a/drivers/mtd/nand/cafe_nand.c b/drivers/mtd/nand/cafe_nand.c
index c828d9ac7bd7..01a6fe1c7805 100644
--- a/drivers/mtd/nand/cafe_nand.c
+++ b/drivers/mtd/nand/cafe_nand.c
@@ -761,7 +761,7 @@ static int __devinit cafe_nand_probe(struct pci_dev *pdev,
761 cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK)); 761 cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
762 762
763 /* Scan to find existence of the device */ 763 /* Scan to find existence of the device */
764 if (nand_scan_ident(mtd, 2)) { 764 if (nand_scan_ident(mtd, 2, NULL)) {
765 err = -ENXIO; 765 err = -ENXIO;
766 goto out_irq; 766 goto out_irq;
767 } 767 }
@@ -848,7 +848,7 @@ static void __devexit cafe_nand_remove(struct pci_dev *pdev)
848 kfree(mtd); 848 kfree(mtd);
849} 849}
850 850
851static struct pci_device_id cafe_nand_tbl[] = { 851static const struct pci_device_id cafe_nand_tbl[] = {
852 { PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_88ALP01_NAND, 852 { PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_88ALP01_NAND,
853 PCI_ANY_ID, PCI_ANY_ID }, 853 PCI_ANY_ID, PCI_ANY_ID },
854 { } 854 { }
diff --git a/drivers/mtd/nand/davinci_nand.c b/drivers/mtd/nand/davinci_nand.c
index fe3eba87de40..45bb931c0848 100644
--- a/drivers/mtd/nand/davinci_nand.c
+++ b/drivers/mtd/nand/davinci_nand.c
@@ -566,8 +566,8 @@ static int __init nand_davinci_probe(struct platform_device *pdev)
566 goto err_nomem; 566 goto err_nomem;
567 } 567 }
568 568
569 vaddr = ioremap(res1->start, res1->end - res1->start); 569 vaddr = ioremap(res1->start, resource_size(res1));
570 base = ioremap(res2->start, res2->end - res2->start); 570 base = ioremap(res2->start, resource_size(res2));
571 if (!vaddr || !base) { 571 if (!vaddr || !base) {
572 dev_err(&pdev->dev, "ioremap failed\n"); 572 dev_err(&pdev->dev, "ioremap failed\n");
573 ret = -EINVAL; 573 ret = -EINVAL;
@@ -690,7 +690,7 @@ static int __init nand_davinci_probe(struct platform_device *pdev)
690 spin_unlock_irq(&davinci_nand_lock); 690 spin_unlock_irq(&davinci_nand_lock);
691 691
692 /* Scan to find existence of the device(s) */ 692 /* Scan to find existence of the device(s) */
693 ret = nand_scan_ident(&info->mtd, pdata->mask_chipsel ? 2 : 1); 693 ret = nand_scan_ident(&info->mtd, pdata->mask_chipsel ? 2 : 1, NULL);
694 if (ret < 0) { 694 if (ret < 0) {
695 dev_dbg(&pdev->dev, "no NAND chip(s) found\n"); 695 dev_dbg(&pdev->dev, "no NAND chip(s) found\n");
696 goto err_scan; 696 goto err_scan;
diff --git a/drivers/mtd/nand/fsl_elbc_nand.c b/drivers/mtd/nand/fsl_elbc_nand.c
index ae30fb6eed97..3f38fb8e6666 100644
--- a/drivers/mtd/nand/fsl_elbc_nand.c
+++ b/drivers/mtd/nand/fsl_elbc_nand.c
@@ -874,7 +874,7 @@ static int __devinit fsl_elbc_chip_probe(struct fsl_elbc_ctrl *ctrl,
874 priv->ctrl = ctrl; 874 priv->ctrl = ctrl;
875 priv->dev = ctrl->dev; 875 priv->dev = ctrl->dev;
876 876
877 priv->vbase = ioremap(res.start, res.end - res.start + 1); 877 priv->vbase = ioremap(res.start, resource_size(&res));
878 if (!priv->vbase) { 878 if (!priv->vbase) {
879 dev_err(ctrl->dev, "failed to map chip region\n"); 879 dev_err(ctrl->dev, "failed to map chip region\n");
880 ret = -ENOMEM; 880 ret = -ENOMEM;
@@ -891,7 +891,7 @@ static int __devinit fsl_elbc_chip_probe(struct fsl_elbc_ctrl *ctrl,
891 if (ret) 891 if (ret)
892 goto err; 892 goto err;
893 893
894 ret = nand_scan_ident(&priv->mtd, 1); 894 ret = nand_scan_ident(&priv->mtd, 1, NULL);
895 if (ret) 895 if (ret)
896 goto err; 896 goto err;
897 897
diff --git a/drivers/mtd/nand/fsl_upm.c b/drivers/mtd/nand/fsl_upm.c
index 071a60cb4204..d721ec055cbf 100644
--- a/drivers/mtd/nand/fsl_upm.c
+++ b/drivers/mtd/nand/fsl_upm.c
@@ -302,7 +302,7 @@ static int __devinit fun_probe(struct of_device *ofdev,
302 FSL_UPM_WAIT_WRITE_BYTE; 302 FSL_UPM_WAIT_WRITE_BYTE;
303 303
304 fun->io_base = devm_ioremap_nocache(&ofdev->dev, io_res.start, 304 fun->io_base = devm_ioremap_nocache(&ofdev->dev, io_res.start,
305 io_res.end - io_res.start + 1); 305 resource_size(&io_res));
306 if (!fun->io_base) { 306 if (!fun->io_base) {
307 ret = -ENOMEM; 307 ret = -ENOMEM;
308 goto err2; 308 goto err2;
@@ -349,7 +349,7 @@ static int __devexit fun_remove(struct of_device *ofdev)
349 return 0; 349 return 0;
350} 350}
351 351
352static struct of_device_id of_fun_match[] = { 352static const struct of_device_id of_fun_match[] = {
353 { .compatible = "fsl,upm-nand" }, 353 { .compatible = "fsl,upm-nand" },
354 {}, 354 {},
355}; 355};
diff --git a/drivers/mtd/nand/gpio.c b/drivers/mtd/nand/gpio.c
index 8f902e75aa85..0cde618bcc1e 100644
--- a/drivers/mtd/nand/gpio.c
+++ b/drivers/mtd/nand/gpio.c
@@ -181,11 +181,11 @@ static int __devexit gpio_nand_remove(struct platform_device *dev)
181 res = platform_get_resource(dev, IORESOURCE_MEM, 1); 181 res = platform_get_resource(dev, IORESOURCE_MEM, 1);
182 iounmap(gpiomtd->io_sync); 182 iounmap(gpiomtd->io_sync);
183 if (res) 183 if (res)
184 release_mem_region(res->start, res->end - res->start + 1); 184 release_mem_region(res->start, resource_size(res));
185 185
186 res = platform_get_resource(dev, IORESOURCE_MEM, 0); 186 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
187 iounmap(gpiomtd->nand_chip.IO_ADDR_R); 187 iounmap(gpiomtd->nand_chip.IO_ADDR_R);
188 release_mem_region(res->start, res->end - res->start + 1); 188 release_mem_region(res->start, resource_size(res));
189 189
190 if (gpio_is_valid(gpiomtd->plat.gpio_nwp)) 190 if (gpio_is_valid(gpiomtd->plat.gpio_nwp))
191 gpio_set_value(gpiomtd->plat.gpio_nwp, 0); 191 gpio_set_value(gpiomtd->plat.gpio_nwp, 0);
@@ -208,14 +208,14 @@ static void __iomem *request_and_remap(struct resource *res, size_t size,
208{ 208{
209 void __iomem *ptr; 209 void __iomem *ptr;
210 210
211 if (!request_mem_region(res->start, res->end - res->start + 1, name)) { 211 if (!request_mem_region(res->start, resource_size(res), name)) {
212 *err = -EBUSY; 212 *err = -EBUSY;
213 return NULL; 213 return NULL;
214 } 214 }
215 215
216 ptr = ioremap(res->start, size); 216 ptr = ioremap(res->start, size);
217 if (!ptr) { 217 if (!ptr) {
218 release_mem_region(res->start, res->end - res->start + 1); 218 release_mem_region(res->start, resource_size(res));
219 *err = -ENOMEM; 219 *err = -ENOMEM;
220 } 220 }
221 return ptr; 221 return ptr;
@@ -338,10 +338,10 @@ err_nwp:
338err_nce: 338err_nce:
339 iounmap(gpiomtd->io_sync); 339 iounmap(gpiomtd->io_sync);
340 if (res1) 340 if (res1)
341 release_mem_region(res1->start, res1->end - res1->start + 1); 341 release_mem_region(res1->start, resource_size(res1));
342err_sync: 342err_sync:
343 iounmap(gpiomtd->nand_chip.IO_ADDR_R); 343 iounmap(gpiomtd->nand_chip.IO_ADDR_R);
344 release_mem_region(res0->start, res0->end - res0->start + 1); 344 release_mem_region(res0->start, resource_size(res0));
345err_map: 345err_map:
346 kfree(gpiomtd); 346 kfree(gpiomtd);
347 return ret; 347 return ret;
diff --git a/drivers/mtd/nand/mpc5121_nfc.c b/drivers/mtd/nand/mpc5121_nfc.c
new file mode 100644
index 000000000000..d7333f4dae86
--- /dev/null
+++ b/drivers/mtd/nand/mpc5121_nfc.c
@@ -0,0 +1,916 @@
1/*
2 * Copyright 2004-2008 Freescale Semiconductor, Inc.
3 * Copyright 2009 Semihalf.
4 *
5 * Approved as OSADL project by a majority of OSADL members and funded
6 * by OSADL membership fees in 2009; for details see www.osadl.org.
7 *
8 * Based on original driver from Freescale Semiconductor
9 * written by John Rigby <jrigby@freescale.com> on basis
10 * of drivers/mtd/nand/mxc_nand.c. Reworked and extended
11 * Piotr Ziecik <kosmo@semihalf.com>.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 * MA 02110-1301, USA.
26 */
27
28#include <linux/module.h>
29#include <linux/clk.h>
30#include <linux/delay.h>
31#include <linux/init.h>
32#include <linux/interrupt.h>
33#include <linux/io.h>
34#include <linux/mtd/mtd.h>
35#include <linux/mtd/nand.h>
36#include <linux/mtd/partitions.h>
37#include <linux/of_device.h>
38#include <linux/of_platform.h>
39
40#include <asm/mpc5121.h>
41
42/* Addresses for NFC MAIN RAM BUFFER areas */
43#define NFC_MAIN_AREA(n) ((n) * 0x200)
44
45/* Addresses for NFC SPARE BUFFER areas */
46#define NFC_SPARE_BUFFERS 8
47#define NFC_SPARE_LEN 0x40
48#define NFC_SPARE_AREA(n) (0x1000 + ((n) * NFC_SPARE_LEN))
49
50/* MPC5121 NFC registers */
51#define NFC_BUF_ADDR 0x1E04
52#define NFC_FLASH_ADDR 0x1E06
53#define NFC_FLASH_CMD 0x1E08
54#define NFC_CONFIG 0x1E0A
55#define NFC_ECC_STATUS1 0x1E0C
56#define NFC_ECC_STATUS2 0x1E0E
57#define NFC_SPAS 0x1E10
58#define NFC_WRPROT 0x1E12
59#define NFC_NF_WRPRST 0x1E18
60#define NFC_CONFIG1 0x1E1A
61#define NFC_CONFIG2 0x1E1C
62#define NFC_UNLOCKSTART_BLK0 0x1E20
63#define NFC_UNLOCKEND_BLK0 0x1E22
64#define NFC_UNLOCKSTART_BLK1 0x1E24
65#define NFC_UNLOCKEND_BLK1 0x1E26
66#define NFC_UNLOCKSTART_BLK2 0x1E28
67#define NFC_UNLOCKEND_BLK2 0x1E2A
68#define NFC_UNLOCKSTART_BLK3 0x1E2C
69#define NFC_UNLOCKEND_BLK3 0x1E2E
70
71/* Bit Definitions: NFC_BUF_ADDR */
72#define NFC_RBA_MASK (7 << 0)
73#define NFC_ACTIVE_CS_SHIFT 5
74#define NFC_ACTIVE_CS_MASK (3 << NFC_ACTIVE_CS_SHIFT)
75
76/* Bit Definitions: NFC_CONFIG */
77#define NFC_BLS_UNLOCKED (1 << 1)
78
79/* Bit Definitions: NFC_CONFIG1 */
80#define NFC_ECC_4BIT (1 << 0)
81#define NFC_FULL_PAGE_DMA (1 << 1)
82#define NFC_SPARE_ONLY (1 << 2)
83#define NFC_ECC_ENABLE (1 << 3)
84#define NFC_INT_MASK (1 << 4)
85#define NFC_BIG_ENDIAN (1 << 5)
86#define NFC_RESET (1 << 6)
87#define NFC_CE (1 << 7)
88#define NFC_ONE_CYCLE (1 << 8)
89#define NFC_PPB_32 (0 << 9)
90#define NFC_PPB_64 (1 << 9)
91#define NFC_PPB_128 (2 << 9)
92#define NFC_PPB_256 (3 << 9)
93#define NFC_PPB_MASK (3 << 9)
94#define NFC_FULL_PAGE_INT (1 << 11)
95
96/* Bit Definitions: NFC_CONFIG2 */
97#define NFC_COMMAND (1 << 0)
98#define NFC_ADDRESS (1 << 1)
99#define NFC_INPUT (1 << 2)
100#define NFC_OUTPUT (1 << 3)
101#define NFC_ID (1 << 4)
102#define NFC_STATUS (1 << 5)
103#define NFC_CMD_FAIL (1 << 15)
104#define NFC_INT (1 << 15)
105
106/* Bit Definitions: NFC_WRPROT */
107#define NFC_WPC_LOCK_TIGHT (1 << 0)
108#define NFC_WPC_LOCK (1 << 1)
109#define NFC_WPC_UNLOCK (1 << 2)
110
111#define DRV_NAME "mpc5121_nfc"
112
113/* Timeouts */
114#define NFC_RESET_TIMEOUT 1000 /* 1 ms */
115#define NFC_TIMEOUT (HZ / 10) /* 1/10 s */
116
117struct mpc5121_nfc_prv {
118 struct mtd_info mtd;
119 struct nand_chip chip;
120 int irq;
121 void __iomem *regs;
122 struct clk *clk;
123 wait_queue_head_t irq_waitq;
124 uint column;
125 int spareonly;
126 void __iomem *csreg;
127 struct device *dev;
128};
129
130static void mpc5121_nfc_done(struct mtd_info *mtd);
131
132#ifdef CONFIG_MTD_PARTITIONS
133static const char *mpc5121_nfc_pprobes[] = { "cmdlinepart", NULL };
134#endif
135
136/* Read NFC register */
137static inline u16 nfc_read(struct mtd_info *mtd, uint reg)
138{
139 struct nand_chip *chip = mtd->priv;
140 struct mpc5121_nfc_prv *prv = chip->priv;
141
142 return in_be16(prv->regs + reg);
143}
144
145/* Write NFC register */
146static inline void nfc_write(struct mtd_info *mtd, uint reg, u16 val)
147{
148 struct nand_chip *chip = mtd->priv;
149 struct mpc5121_nfc_prv *prv = chip->priv;
150
151 out_be16(prv->regs + reg, val);
152}
153
154/* Set bits in NFC register */
155static inline void nfc_set(struct mtd_info *mtd, uint reg, u16 bits)
156{
157 nfc_write(mtd, reg, nfc_read(mtd, reg) | bits);
158}
159
160/* Clear bits in NFC register */
161static inline void nfc_clear(struct mtd_info *mtd, uint reg, u16 bits)
162{
163 nfc_write(mtd, reg, nfc_read(mtd, reg) & ~bits);
164}
165
166/* Invoke address cycle */
167static inline void mpc5121_nfc_send_addr(struct mtd_info *mtd, u16 addr)
168{
169 nfc_write(mtd, NFC_FLASH_ADDR, addr);
170 nfc_write(mtd, NFC_CONFIG2, NFC_ADDRESS);
171 mpc5121_nfc_done(mtd);
172}
173
174/* Invoke command cycle */
175static inline void mpc5121_nfc_send_cmd(struct mtd_info *mtd, u16 cmd)
176{
177 nfc_write(mtd, NFC_FLASH_CMD, cmd);
178 nfc_write(mtd, NFC_CONFIG2, NFC_COMMAND);
179 mpc5121_nfc_done(mtd);
180}
181
182/* Send data from NFC buffers to NAND flash */
183static inline void mpc5121_nfc_send_prog_page(struct mtd_info *mtd)
184{
185 nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
186 nfc_write(mtd, NFC_CONFIG2, NFC_INPUT);
187 mpc5121_nfc_done(mtd);
188}
189
190/* Receive data from NAND flash */
191static inline void mpc5121_nfc_send_read_page(struct mtd_info *mtd)
192{
193 nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
194 nfc_write(mtd, NFC_CONFIG2, NFC_OUTPUT);
195 mpc5121_nfc_done(mtd);
196}
197
198/* Receive ID from NAND flash */
199static inline void mpc5121_nfc_send_read_id(struct mtd_info *mtd)
200{
201 nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
202 nfc_write(mtd, NFC_CONFIG2, NFC_ID);
203 mpc5121_nfc_done(mtd);
204}
205
206/* Receive status from NAND flash */
207static inline void mpc5121_nfc_send_read_status(struct mtd_info *mtd)
208{
209 nfc_clear(mtd, NFC_BUF_ADDR, NFC_RBA_MASK);
210 nfc_write(mtd, NFC_CONFIG2, NFC_STATUS);
211 mpc5121_nfc_done(mtd);
212}
213
214/* NFC interrupt handler */
215static irqreturn_t mpc5121_nfc_irq(int irq, void *data)
216{
217 struct mtd_info *mtd = data;
218 struct nand_chip *chip = mtd->priv;
219 struct mpc5121_nfc_prv *prv = chip->priv;
220
221 nfc_set(mtd, NFC_CONFIG1, NFC_INT_MASK);
222 wake_up(&prv->irq_waitq);
223
224 return IRQ_HANDLED;
225}
226
227/* Wait for operation complete */
228static void mpc5121_nfc_done(struct mtd_info *mtd)
229{
230 struct nand_chip *chip = mtd->priv;
231 struct mpc5121_nfc_prv *prv = chip->priv;
232 int rv;
233
234 if ((nfc_read(mtd, NFC_CONFIG2) & NFC_INT) == 0) {
235 nfc_clear(mtd, NFC_CONFIG1, NFC_INT_MASK);
236 rv = wait_event_timeout(prv->irq_waitq,
237 (nfc_read(mtd, NFC_CONFIG2) & NFC_INT), NFC_TIMEOUT);
238
239 if (!rv)
240 dev_warn(prv->dev,
241 "Timeout while waiting for interrupt.\n");
242 }
243
244 nfc_clear(mtd, NFC_CONFIG2, NFC_INT);
245}
246
247/* Do address cycle(s) */
248static void mpc5121_nfc_addr_cycle(struct mtd_info *mtd, int column, int page)
249{
250 struct nand_chip *chip = mtd->priv;
251 u32 pagemask = chip->pagemask;
252
253 if (column != -1) {
254 mpc5121_nfc_send_addr(mtd, column);
255 if (mtd->writesize > 512)
256 mpc5121_nfc_send_addr(mtd, column >> 8);
257 }
258
259 if (page != -1) {
260 do {
261 mpc5121_nfc_send_addr(mtd, page & 0xFF);
262 page >>= 8;
263 pagemask >>= 8;
264 } while (pagemask);
265 }
266}
267
268/* Control chip select signals */
269static void mpc5121_nfc_select_chip(struct mtd_info *mtd, int chip)
270{
271 if (chip < 0) {
272 nfc_clear(mtd, NFC_CONFIG1, NFC_CE);
273 return;
274 }
275
276 nfc_clear(mtd, NFC_BUF_ADDR, NFC_ACTIVE_CS_MASK);
277 nfc_set(mtd, NFC_BUF_ADDR, (chip << NFC_ACTIVE_CS_SHIFT) &
278 NFC_ACTIVE_CS_MASK);
279 nfc_set(mtd, NFC_CONFIG1, NFC_CE);
280}
281
282/* Init external chip select logic on ADS5121 board */
283static int ads5121_chipselect_init(struct mtd_info *mtd)
284{
285 struct nand_chip *chip = mtd->priv;
286 struct mpc5121_nfc_prv *prv = chip->priv;
287 struct device_node *dn;
288
289 dn = of_find_compatible_node(NULL, NULL, "fsl,mpc5121ads-cpld");
290 if (dn) {
291 prv->csreg = of_iomap(dn, 0);
292 of_node_put(dn);
293 if (!prv->csreg)
294 return -ENOMEM;
295
296 /* CPLD Register 9 controls NAND /CE Lines */
297 prv->csreg += 9;
298 return 0;
299 }
300
301 return -EINVAL;
302}
303
304/* Control chips select signal on ADS5121 board */
305static void ads5121_select_chip(struct mtd_info *mtd, int chip)
306{
307 struct nand_chip *nand = mtd->priv;
308 struct mpc5121_nfc_prv *prv = nand->priv;
309 u8 v;
310
311 v = in_8(prv->csreg);
312 v |= 0x0F;
313
314 if (chip >= 0) {
315 mpc5121_nfc_select_chip(mtd, 0);
316 v &= ~(1 << chip);
317 } else
318 mpc5121_nfc_select_chip(mtd, -1);
319
320 out_8(prv->csreg, v);
321}
322
323/* Read NAND Ready/Busy signal */
324static int mpc5121_nfc_dev_ready(struct mtd_info *mtd)
325{
326 /*
327 * NFC handles ready/busy signal internally. Therefore, this function
328 * always returns status as ready.
329 */
330 return 1;
331}
332
333/* Write command to NAND flash */
334static void mpc5121_nfc_command(struct mtd_info *mtd, unsigned command,
335 int column, int page)
336{
337 struct nand_chip *chip = mtd->priv;
338 struct mpc5121_nfc_prv *prv = chip->priv;
339
340 prv->column = (column >= 0) ? column : 0;
341 prv->spareonly = 0;
342
343 switch (command) {
344 case NAND_CMD_PAGEPROG:
345 mpc5121_nfc_send_prog_page(mtd);
346 break;
347 /*
348 * NFC does not support sub-page reads and writes,
349 * so emulate them using full page transfers.
350 */
351 case NAND_CMD_READ0:
352 column = 0;
353 break;
354
355 case NAND_CMD_READ1:
356 prv->column += 256;
357 command = NAND_CMD_READ0;
358 column = 0;
359 break;
360
361 case NAND_CMD_READOOB:
362 prv->spareonly = 1;
363 command = NAND_CMD_READ0;
364 column = 0;
365 break;
366
367 case NAND_CMD_SEQIN:
368 mpc5121_nfc_command(mtd, NAND_CMD_READ0, column, page);
369 column = 0;
370 break;
371
372 case NAND_CMD_ERASE1:
373 case NAND_CMD_ERASE2:
374 case NAND_CMD_READID:
375 case NAND_CMD_STATUS:
376 break;
377
378 default:
379 return;
380 }
381
382 mpc5121_nfc_send_cmd(mtd, command);
383 mpc5121_nfc_addr_cycle(mtd, column, page);
384
385 switch (command) {
386 case NAND_CMD_READ0:
387 if (mtd->writesize > 512)
388 mpc5121_nfc_send_cmd(mtd, NAND_CMD_READSTART);
389 mpc5121_nfc_send_read_page(mtd);
390 break;
391
392 case NAND_CMD_READID:
393 mpc5121_nfc_send_read_id(mtd);
394 break;
395
396 case NAND_CMD_STATUS:
397 mpc5121_nfc_send_read_status(mtd);
398 if (chip->options & NAND_BUSWIDTH_16)
399 prv->column = 1;
400 else
401 prv->column = 0;
402 break;
403 }
404}
405
406/* Copy data from/to NFC spare buffers. */
407static void mpc5121_nfc_copy_spare(struct mtd_info *mtd, uint offset,
408 u8 *buffer, uint size, int wr)
409{
410 struct nand_chip *nand = mtd->priv;
411 struct mpc5121_nfc_prv *prv = nand->priv;
412 uint o, s, sbsize, blksize;
413
414 /*
415 * NAND spare area is available through NFC spare buffers.
416 * The NFC divides spare area into (page_size / 512) chunks.
417 * Each chunk is placed into separate spare memory area, using
418 * first (spare_size / num_of_chunks) bytes of the buffer.
419 *
420 * For NAND device in which the spare area is not divided fully
421 * by the number of chunks, number of used bytes in each spare
422 * buffer is rounded down to the nearest even number of bytes,
423 * and all remaining bytes are added to the last used spare area.
424 *
425 * For more information read section 26.6.10 of MPC5121e
426 * Microcontroller Reference Manual, Rev. 3.
427 */
428
429 /* Calculate number of valid bytes in each spare buffer */
430 sbsize = (mtd->oobsize / (mtd->writesize / 512)) & ~1;
431
432 while (size) {
433 /* Calculate spare buffer number */
434 s = offset / sbsize;
435 if (s > NFC_SPARE_BUFFERS - 1)
436 s = NFC_SPARE_BUFFERS - 1;
437
438 /*
439 * Calculate offset to requested data block in selected spare
440 * buffer and its size.
441 */
442 o = offset - (s * sbsize);
443 blksize = min(sbsize - o, size);
444
445 if (wr)
446 memcpy_toio(prv->regs + NFC_SPARE_AREA(s) + o,
447 buffer, blksize);
448 else
449 memcpy_fromio(buffer,
450 prv->regs + NFC_SPARE_AREA(s) + o, blksize);
451
452 buffer += blksize;
453 offset += blksize;
454 size -= blksize;
455 };
456}
457
458/* Copy data from/to NFC main and spare buffers */
459static void mpc5121_nfc_buf_copy(struct mtd_info *mtd, u_char *buf, int len,
460 int wr)
461{
462 struct nand_chip *chip = mtd->priv;
463 struct mpc5121_nfc_prv *prv = chip->priv;
464 uint c = prv->column;
465 uint l;
466
467 /* Handle spare area access */
468 if (prv->spareonly || c >= mtd->writesize) {
469 /* Calculate offset from beginning of spare area */
470 if (c >= mtd->writesize)
471 c -= mtd->writesize;
472
473 prv->column += len;
474 mpc5121_nfc_copy_spare(mtd, c, buf, len, wr);
475 return;
476 }
477
478 /*
479 * Handle main area access - limit copy length to prevent
480 * crossing main/spare boundary.
481 */
482 l = min((uint)len, mtd->writesize - c);
483 prv->column += l;
484
485 if (wr)
486 memcpy_toio(prv->regs + NFC_MAIN_AREA(0) + c, buf, l);
487 else
488 memcpy_fromio(buf, prv->regs + NFC_MAIN_AREA(0) + c, l);
489
490 /* Handle crossing main/spare boundary */
491 if (l != len) {
492 buf += l;
493 len -= l;
494 mpc5121_nfc_buf_copy(mtd, buf, len, wr);
495 }
496}
497
498/* Read data from NFC buffers */
499static void mpc5121_nfc_read_buf(struct mtd_info *mtd, u_char *buf, int len)
500{
501 mpc5121_nfc_buf_copy(mtd, buf, len, 0);
502}
503
504/* Write data to NFC buffers */
505static void mpc5121_nfc_write_buf(struct mtd_info *mtd,
506 const u_char *buf, int len)
507{
508 mpc5121_nfc_buf_copy(mtd, (u_char *)buf, len, 1);
509}
510
511/* Compare buffer with NAND flash */
512static int mpc5121_nfc_verify_buf(struct mtd_info *mtd,
513 const u_char *buf, int len)
514{
515 u_char tmp[256];
516 uint bsize;
517
518 while (len) {
519 bsize = min(len, 256);
520 mpc5121_nfc_read_buf(mtd, tmp, bsize);
521
522 if (memcmp(buf, tmp, bsize))
523 return 1;
524
525 buf += bsize;
526 len -= bsize;
527 }
528
529 return 0;
530}
531
532/* Read byte from NFC buffers */
533static u8 mpc5121_nfc_read_byte(struct mtd_info *mtd)
534{
535 u8 tmp;
536
537 mpc5121_nfc_read_buf(mtd, &tmp, sizeof(tmp));
538
539 return tmp;
540}
541
542/* Read word from NFC buffers */
543static u16 mpc5121_nfc_read_word(struct mtd_info *mtd)
544{
545 u16 tmp;
546
547 mpc5121_nfc_read_buf(mtd, (u_char *)&tmp, sizeof(tmp));
548
549 return tmp;
550}
551
552/*
553 * Read NFC configuration from Reset Config Word
554 *
555 * NFC is configured during reset in basis of information stored
556 * in Reset Config Word. There is no other way to set NAND block
557 * size, spare size and bus width.
558 */
559static int mpc5121_nfc_read_hw_config(struct mtd_info *mtd)
560{
561 struct nand_chip *chip = mtd->priv;
562 struct mpc5121_nfc_prv *prv = chip->priv;
563 struct mpc512x_reset_module *rm;
564 struct device_node *rmnode;
565 uint rcw_pagesize = 0;
566 uint rcw_sparesize = 0;
567 uint rcw_width;
568 uint rcwh;
569 uint romloc, ps;
570
571 rmnode = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-reset");
572 if (!rmnode) {
573 dev_err(prv->dev, "Missing 'fsl,mpc5121-reset' "
574 "node in device tree!\n");
575 return -ENODEV;
576 }
577
578 rm = of_iomap(rmnode, 0);
579 if (!rm) {
580 dev_err(prv->dev, "Error mapping reset module node!\n");
581 return -EBUSY;
582 }
583
584 rcwh = in_be32(&rm->rcwhr);
585
586 /* Bit 6: NFC bus width */
587 rcw_width = ((rcwh >> 6) & 0x1) ? 2 : 1;
588
589 /* Bit 7: NFC Page/Spare size */
590 ps = (rcwh >> 7) & 0x1;
591
592 /* Bits [22:21]: ROM Location */
593 romloc = (rcwh >> 21) & 0x3;
594
595 /* Decode RCW bits */
596 switch ((ps << 2) | romloc) {
597 case 0x00:
598 case 0x01:
599 rcw_pagesize = 512;
600 rcw_sparesize = 16;
601 break;
602 case 0x02:
603 case 0x03:
604 rcw_pagesize = 4096;
605 rcw_sparesize = 128;
606 break;
607 case 0x04:
608 case 0x05:
609 rcw_pagesize = 2048;
610 rcw_sparesize = 64;
611 break;
612 case 0x06:
613 case 0x07:
614 rcw_pagesize = 4096;
615 rcw_sparesize = 218;
616 break;
617 }
618
619 mtd->writesize = rcw_pagesize;
620 mtd->oobsize = rcw_sparesize;
621 if (rcw_width == 2)
622 chip->options |= NAND_BUSWIDTH_16;
623
624 dev_notice(prv->dev, "Configured for "
625 "%u-bit NAND, page size %u "
626 "with %u spare.\n",
627 rcw_width * 8, rcw_pagesize,
628 rcw_sparesize);
629 iounmap(rm);
630 of_node_put(rmnode);
631 return 0;
632}
633
634/* Free driver resources */
635static void mpc5121_nfc_free(struct device *dev, struct mtd_info *mtd)
636{
637 struct nand_chip *chip = mtd->priv;
638 struct mpc5121_nfc_prv *prv = chip->priv;
639
640 if (prv->clk) {
641 clk_disable(prv->clk);
642 clk_put(prv->clk);
643 }
644
645 if (prv->csreg)
646 iounmap(prv->csreg);
647}
648
649static int __devinit mpc5121_nfc_probe(struct of_device *op,
650 const struct of_device_id *match)
651{
652 struct device_node *rootnode, *dn = op->node;
653 struct device *dev = &op->dev;
654 struct mpc5121_nfc_prv *prv;
655 struct resource res;
656 struct mtd_info *mtd;
657#ifdef CONFIG_MTD_PARTITIONS
658 struct mtd_partition *parts;
659#endif
660 struct nand_chip *chip;
661 unsigned long regs_paddr, regs_size;
662 const uint *chips_no;
663 int resettime = 0;
664 int retval = 0;
665 int rev, len;
666
667 /*
668 * Check SoC revision. This driver supports only NFC
669 * in MPC5121 revision 2.
670 */
671 rev = (mfspr(SPRN_SVR) >> 4) & 0xF;
672 if (rev != 2) {
673 dev_err(dev, "SoC revision %u is not supported!\n", rev);
674 return -ENXIO;
675 }
676
677 prv = devm_kzalloc(dev, sizeof(*prv), GFP_KERNEL);
678 if (!prv) {
679 dev_err(dev, "Memory exhausted!\n");
680 return -ENOMEM;
681 }
682
683 mtd = &prv->mtd;
684 chip = &prv->chip;
685
686 mtd->priv = chip;
687 chip->priv = prv;
688 prv->dev = dev;
689
690 /* Read NFC configuration from Reset Config Word */
691 retval = mpc5121_nfc_read_hw_config(mtd);
692 if (retval) {
693 dev_err(dev, "Unable to read NFC config!\n");
694 return retval;
695 }
696
697 prv->irq = irq_of_parse_and_map(dn, 0);
698 if (prv->irq == NO_IRQ) {
699 dev_err(dev, "Error mapping IRQ!\n");
700 return -EINVAL;
701 }
702
703 retval = of_address_to_resource(dn, 0, &res);
704 if (retval) {
705 dev_err(dev, "Error parsing memory region!\n");
706 return retval;
707 }
708
709 chips_no = of_get_property(dn, "chips", &len);
710 if (!chips_no || len != sizeof(*chips_no)) {
711 dev_err(dev, "Invalid/missing 'chips' property!\n");
712 return -EINVAL;
713 }
714
715 regs_paddr = res.start;
716 regs_size = res.end - res.start + 1;
717
718 if (!devm_request_mem_region(dev, regs_paddr, regs_size, DRV_NAME)) {
719 dev_err(dev, "Error requesting memory region!\n");
720 return -EBUSY;
721 }
722
723 prv->regs = devm_ioremap(dev, regs_paddr, regs_size);
724 if (!prv->regs) {
725 dev_err(dev, "Error mapping memory region!\n");
726 return -ENOMEM;
727 }
728
729 mtd->name = "MPC5121 NAND";
730 chip->dev_ready = mpc5121_nfc_dev_ready;
731 chip->cmdfunc = mpc5121_nfc_command;
732 chip->read_byte = mpc5121_nfc_read_byte;
733 chip->read_word = mpc5121_nfc_read_word;
734 chip->read_buf = mpc5121_nfc_read_buf;
735 chip->write_buf = mpc5121_nfc_write_buf;
736 chip->verify_buf = mpc5121_nfc_verify_buf;
737 chip->select_chip = mpc5121_nfc_select_chip;
738 chip->options = NAND_NO_AUTOINCR | NAND_USE_FLASH_BBT;
739 chip->ecc.mode = NAND_ECC_SOFT;
740
741 /* Support external chip-select logic on ADS5121 board */
742 rootnode = of_find_node_by_path("/");
743 if (of_device_is_compatible(rootnode, "fsl,mpc5121ads")) {
744 retval = ads5121_chipselect_init(mtd);
745 if (retval) {
746 dev_err(dev, "Chipselect init error!\n");
747 of_node_put(rootnode);
748 return retval;
749 }
750
751 chip->select_chip = ads5121_select_chip;
752 }
753 of_node_put(rootnode);
754
755 /* Enable NFC clock */
756 prv->clk = clk_get(dev, "nfc_clk");
757 if (!prv->clk) {
758 dev_err(dev, "Unable to acquire NFC clock!\n");
759 retval = -ENODEV;
760 goto error;
761 }
762
763 clk_enable(prv->clk);
764
765 /* Reset NAND Flash controller */
766 nfc_set(mtd, NFC_CONFIG1, NFC_RESET);
767 while (nfc_read(mtd, NFC_CONFIG1) & NFC_RESET) {
768 if (resettime++ >= NFC_RESET_TIMEOUT) {
769 dev_err(dev, "Timeout while resetting NFC!\n");
770 retval = -EINVAL;
771 goto error;
772 }
773
774 udelay(1);
775 }
776
777 /* Enable write to NFC memory */
778 nfc_write(mtd, NFC_CONFIG, NFC_BLS_UNLOCKED);
779
780 /* Enable write to all NAND pages */
781 nfc_write(mtd, NFC_UNLOCKSTART_BLK0, 0x0000);
782 nfc_write(mtd, NFC_UNLOCKEND_BLK0, 0xFFFF);
783 nfc_write(mtd, NFC_WRPROT, NFC_WPC_UNLOCK);
784
785 /*
786 * Setup NFC:
787 * - Big Endian transfers,
788 * - Interrupt after full page read/write.
789 */
790 nfc_write(mtd, NFC_CONFIG1, NFC_BIG_ENDIAN | NFC_INT_MASK |
791 NFC_FULL_PAGE_INT);
792
793 /* Set spare area size */
794 nfc_write(mtd, NFC_SPAS, mtd->oobsize >> 1);
795
796 init_waitqueue_head(&prv->irq_waitq);
797 retval = devm_request_irq(dev, prv->irq, &mpc5121_nfc_irq, 0, DRV_NAME,
798 mtd);
799 if (retval) {
800 dev_err(dev, "Error requesting IRQ!\n");
801 goto error;
802 }
803
804 /* Detect NAND chips */
805 if (nand_scan(mtd, *chips_no)) {
806 dev_err(dev, "NAND Flash not found !\n");
807 devm_free_irq(dev, prv->irq, mtd);
808 retval = -ENXIO;
809 goto error;
810 }
811
812 /* Set erase block size */
813 switch (mtd->erasesize / mtd->writesize) {
814 case 32:
815 nfc_set(mtd, NFC_CONFIG1, NFC_PPB_32);
816 break;
817
818 case 64:
819 nfc_set(mtd, NFC_CONFIG1, NFC_PPB_64);
820 break;
821
822 case 128:
823 nfc_set(mtd, NFC_CONFIG1, NFC_PPB_128);
824 break;
825
826 case 256:
827 nfc_set(mtd, NFC_CONFIG1, NFC_PPB_256);
828 break;
829
830 default:
831 dev_err(dev, "Unsupported NAND flash!\n");
832 devm_free_irq(dev, prv->irq, mtd);
833 retval = -ENXIO;
834 goto error;
835 }
836
837 dev_set_drvdata(dev, mtd);
838
839 /* Register device in MTD */
840#ifdef CONFIG_MTD_PARTITIONS
841 retval = parse_mtd_partitions(mtd, mpc5121_nfc_pprobes, &parts, 0);
842#ifdef CONFIG_MTD_OF_PARTS
843 if (retval == 0)
844 retval = of_mtd_parse_partitions(dev, dn, &parts);
845#endif
846 if (retval < 0) {
847 dev_err(dev, "Error parsing MTD partitions!\n");
848 devm_free_irq(dev, prv->irq, mtd);
849 retval = -EINVAL;
850 goto error;
851 }
852
853 if (retval > 0)
854 retval = add_mtd_partitions(mtd, parts, retval);
855 else
856#endif
857 retval = add_mtd_device(mtd);
858
859 if (retval) {
860 dev_err(dev, "Error adding MTD device!\n");
861 devm_free_irq(dev, prv->irq, mtd);
862 goto error;
863 }
864
865 return 0;
866error:
867 mpc5121_nfc_free(dev, mtd);
868 return retval;
869}
870
871static int __devexit mpc5121_nfc_remove(struct of_device *op)
872{
873 struct device *dev = &op->dev;
874 struct mtd_info *mtd = dev_get_drvdata(dev);
875 struct nand_chip *chip = mtd->priv;
876 struct mpc5121_nfc_prv *prv = chip->priv;
877
878 nand_release(mtd);
879 devm_free_irq(dev, prv->irq, mtd);
880 mpc5121_nfc_free(dev, mtd);
881
882 return 0;
883}
884
885static struct of_device_id mpc5121_nfc_match[] __devinitdata = {
886 { .compatible = "fsl,mpc5121-nfc", },
887 {},
888};
889
890static struct of_platform_driver mpc5121_nfc_driver = {
891 .match_table = mpc5121_nfc_match,
892 .probe = mpc5121_nfc_probe,
893 .remove = __devexit_p(mpc5121_nfc_remove),
894 .driver = {
895 .name = DRV_NAME,
896 .owner = THIS_MODULE,
897 },
898};
899
900static int __init mpc5121_nfc_init(void)
901{
902 return of_register_platform_driver(&mpc5121_nfc_driver);
903}
904
905module_init(mpc5121_nfc_init);
906
907static void __exit mpc5121_nfc_cleanup(void)
908{
909 of_unregister_platform_driver(&mpc5121_nfc_driver);
910}
911
912module_exit(mpc5121_nfc_cleanup);
913
914MODULE_AUTHOR("Freescale Semiconductor, Inc.");
915MODULE_DESCRIPTION("MPC5121 NAND MTD driver");
916MODULE_LICENSE("GPL");
diff --git a/drivers/mtd/nand/mxc_nand.c b/drivers/mtd/nand/mxc_nand.c
index 45dec5770da0..474a09e53131 100644
--- a/drivers/mtd/nand/mxc_nand.c
+++ b/drivers/mtd/nand/mxc_nand.c
@@ -638,6 +638,7 @@ static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
638 638
639 case NAND_CMD_ERASE1: 639 case NAND_CMD_ERASE1:
640 case NAND_CMD_ERASE2: 640 case NAND_CMD_ERASE2:
641 case NAND_CMD_RESET:
641 send_cmd(host, command, false); 642 send_cmd(host, command, false);
642 mxc_do_addr_cycle(mtd, column, page_addr); 643 mxc_do_addr_cycle(mtd, column, page_addr);
643 644
@@ -818,7 +819,7 @@ static int __init mxcnd_probe(struct platform_device *pdev)
818 } 819 }
819 820
820 /* first scan to find the device and get the page size */ 821 /* first scan to find the device and get the page size */
821 if (nand_scan_ident(mtd, 1)) { 822 if (nand_scan_ident(mtd, 1, NULL)) {
822 err = -ENXIO; 823 err = -ENXIO;
823 goto escan; 824 goto escan;
824 } 825 }
@@ -886,11 +887,14 @@ static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
886 int ret = 0; 887 int ret = 0;
887 888
888 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n"); 889 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
889 if (mtd) { 890
890 ret = mtd->suspend(mtd); 891 ret = mtd->suspend(mtd);
891 /* Disable the NFC clock */ 892
892 clk_disable(host->clk); 893 /*
893 } 894 * nand_suspend locks the device for exclusive access, so
895 * the clock must already be off.
896 */
897 BUG_ON(!ret && host->clk_act);
894 898
895 return ret; 899 return ret;
896} 900}
@@ -904,11 +908,7 @@ static int mxcnd_resume(struct platform_device *pdev)
904 908
905 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n"); 909 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
906 910
907 if (mtd) { 911 mtd->resume(mtd);
908 /* Enable the NFC clock */
909 clk_enable(host->clk);
910 mtd->resume(mtd);
911 }
912 912
913 return ret; 913 return ret;
914} 914}
diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index 8f2958fe2148..b9dc65c7253c 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -108,6 +108,35 @@ static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
108 */ 108 */
109DEFINE_LED_TRIGGER(nand_led_trigger); 109DEFINE_LED_TRIGGER(nand_led_trigger);
110 110
111static int check_offs_len(struct mtd_info *mtd,
112 loff_t ofs, uint64_t len)
113{
114 struct nand_chip *chip = mtd->priv;
115 int ret = 0;
116
117 /* Start address must align on block boundary */
118 if (ofs & ((1 << chip->phys_erase_shift) - 1)) {
119 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
120 ret = -EINVAL;
121 }
122
123 /* Length must align on block boundary */
124 if (len & ((1 << chip->phys_erase_shift) - 1)) {
125 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
126 __func__);
127 ret = -EINVAL;
128 }
129
130 /* Do not allow past end of device */
131 if (ofs + len > mtd->size) {
132 DEBUG(MTD_DEBUG_LEVEL0, "%s: Past end of device\n",
133 __func__);
134 ret = -EINVAL;
135 }
136
137 return ret;
138}
139
111/** 140/**
112 * nand_release_device - [GENERIC] release chip 141 * nand_release_device - [GENERIC] release chip
113 * @mtd: MTD device structure 142 * @mtd: MTD device structure
@@ -335,14 +364,18 @@ static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
335 bad = cpu_to_le16(chip->read_word(mtd)); 364 bad = cpu_to_le16(chip->read_word(mtd));
336 if (chip->badblockpos & 0x1) 365 if (chip->badblockpos & 0x1)
337 bad >>= 8; 366 bad >>= 8;
338 if ((bad & 0xFF) != 0xff) 367 else
339 res = 1; 368 bad &= 0xFF;
340 } else { 369 } else {
341 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page); 370 chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
342 if (chip->read_byte(mtd) != 0xff) 371 bad = chip->read_byte(mtd);
343 res = 1;
344 } 372 }
345 373
374 if (likely(chip->badblockbits == 8))
375 res = bad != 0xFF;
376 else
377 res = hweight8(bad) < chip->badblockbits;
378
346 if (getchip) 379 if (getchip)
347 nand_release_device(mtd); 380 nand_release_device(mtd);
348 381
@@ -401,6 +434,11 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
401static int nand_check_wp(struct mtd_info *mtd) 434static int nand_check_wp(struct mtd_info *mtd)
402{ 435{
403 struct nand_chip *chip = mtd->priv; 436 struct nand_chip *chip = mtd->priv;
437
438 /* broken xD cards report WP despite being writable */
439 if (chip->options & NAND_BROKEN_XD)
440 return 0;
441
404 /* Check the WP bit */ 442 /* Check the WP bit */
405 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1); 443 chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
406 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1; 444 return (chip->read_byte(mtd) & NAND_STATUS_WP) ? 0 : 1;
@@ -744,9 +782,6 @@ nand_get_device(struct nand_chip *chip, struct mtd_info *mtd, int new_state)
744 chip->state = FL_PM_SUSPENDED; 782 chip->state = FL_PM_SUSPENDED;
745 spin_unlock(lock); 783 spin_unlock(lock);
746 return 0; 784 return 0;
747 } else {
748 spin_unlock(lock);
749 return -EAGAIN;
750 } 785 }
751 } 786 }
752 set_current_state(TASK_UNINTERRUPTIBLE); 787 set_current_state(TASK_UNINTERRUPTIBLE);
@@ -835,6 +870,168 @@ static int nand_wait(struct mtd_info *mtd, struct nand_chip *chip)
835} 870}
836 871
837/** 872/**
873 * __nand_unlock - [REPLACABLE] unlocks specified locked blockes
874 *
875 * @param mtd - mtd info
876 * @param ofs - offset to start unlock from
877 * @param len - length to unlock
878 * @invert - when = 0, unlock the range of blocks within the lower and
879 * upper boundary address
880 * whne = 1, unlock the range of blocks outside the boundaries
881 * of the lower and upper boundary address
882 *
883 * @return - unlock status
884 */
885static int __nand_unlock(struct mtd_info *mtd, loff_t ofs,
886 uint64_t len, int invert)
887{
888 int ret = 0;
889 int status, page;
890 struct nand_chip *chip = mtd->priv;
891
892 /* Submit address of first page to unlock */
893 page = ofs >> chip->page_shift;
894 chip->cmdfunc(mtd, NAND_CMD_UNLOCK1, -1, page & chip->pagemask);
895
896 /* Submit address of last page to unlock */
897 page = (ofs + len) >> chip->page_shift;
898 chip->cmdfunc(mtd, NAND_CMD_UNLOCK2, -1,
899 (page | invert) & chip->pagemask);
900
901 /* Call wait ready function */
902 status = chip->waitfunc(mtd, chip);
903 udelay(1000);
904 /* See if device thinks it succeeded */
905 if (status & 0x01) {
906 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
907 __func__, status);
908 ret = -EIO;
909 }
910
911 return ret;
912}
913
914/**
915 * nand_unlock - [REPLACABLE] unlocks specified locked blockes
916 *
917 * @param mtd - mtd info
918 * @param ofs - offset to start unlock from
919 * @param len - length to unlock
920 *
921 * @return - unlock status
922 */
923int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
924{
925 int ret = 0;
926 int chipnr;
927 struct nand_chip *chip = mtd->priv;
928
929 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
930 __func__, (unsigned long long)ofs, len);
931
932 if (check_offs_len(mtd, ofs, len))
933 ret = -EINVAL;
934
935 /* Align to last block address if size addresses end of the device */
936 if (ofs + len == mtd->size)
937 len -= mtd->erasesize;
938
939 nand_get_device(chip, mtd, FL_UNLOCKING);
940
941 /* Shift to get chip number */
942 chipnr = ofs >> chip->chip_shift;
943
944 chip->select_chip(mtd, chipnr);
945
946 /* Check, if it is write protected */
947 if (nand_check_wp(mtd)) {
948 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
949 __func__);
950 ret = -EIO;
951 goto out;
952 }
953
954 ret = __nand_unlock(mtd, ofs, len, 0);
955
956out:
957 /* de-select the NAND device */
958 chip->select_chip(mtd, -1);
959
960 nand_release_device(mtd);
961
962 return ret;
963}
964
965/**
966 * nand_lock - [REPLACABLE] locks all blockes present in the device
967 *
968 * @param mtd - mtd info
969 * @param ofs - offset to start unlock from
970 * @param len - length to unlock
971 *
972 * @return - lock status
973 *
974 * This feature is not support in many NAND parts. 'Micron' NAND parts
975 * do have this feature, but it allows only to lock all blocks not for
976 * specified range for block.
977 *
978 * Implementing 'lock' feature by making use of 'unlock', for now.
979 */
980int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
981{
982 int ret = 0;
983 int chipnr, status, page;
984 struct nand_chip *chip = mtd->priv;
985
986 DEBUG(MTD_DEBUG_LEVEL3, "%s: start = 0x%012llx, len = %llu\n",
987 __func__, (unsigned long long)ofs, len);
988
989 if (check_offs_len(mtd, ofs, len))
990 ret = -EINVAL;
991
992 nand_get_device(chip, mtd, FL_LOCKING);
993
994 /* Shift to get chip number */
995 chipnr = ofs >> chip->chip_shift;
996
997 chip->select_chip(mtd, chipnr);
998
999 /* Check, if it is write protected */
1000 if (nand_check_wp(mtd)) {
1001 DEBUG(MTD_DEBUG_LEVEL0, "%s: Device is write protected!!!\n",
1002 __func__);
1003 status = MTD_ERASE_FAILED;
1004 ret = -EIO;
1005 goto out;
1006 }
1007
1008 /* Submit address of first page to lock */
1009 page = ofs >> chip->page_shift;
1010 chip->cmdfunc(mtd, NAND_CMD_LOCK, -1, page & chip->pagemask);
1011
1012 /* Call wait ready function */
1013 status = chip->waitfunc(mtd, chip);
1014 udelay(1000);
1015 /* See if device thinks it succeeded */
1016 if (status & 0x01) {
1017 DEBUG(MTD_DEBUG_LEVEL0, "%s: Error status = 0x%08x\n",
1018 __func__, status);
1019 ret = -EIO;
1020 goto out;
1021 }
1022
1023 ret = __nand_unlock(mtd, ofs, len, 0x1);
1024
1025out:
1026 /* de-select the NAND device */
1027 chip->select_chip(mtd, -1);
1028
1029 nand_release_device(mtd);
1030
1031 return ret;
1032}
1033
1034/**
838 * nand_read_page_raw - [Intern] read raw page data without ecc 1035 * nand_read_page_raw - [Intern] read raw page data without ecc
839 * @mtd: mtd info structure 1036 * @mtd: mtd info structure
840 * @chip: nand chip info structure 1037 * @chip: nand chip info structure
@@ -1232,6 +1429,9 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1232 int ret = 0; 1429 int ret = 0;
1233 uint32_t readlen = ops->len; 1430 uint32_t readlen = ops->len;
1234 uint32_t oobreadlen = ops->ooblen; 1431 uint32_t oobreadlen = ops->ooblen;
1432 uint32_t max_oobsize = ops->mode == MTD_OOB_AUTO ?
1433 mtd->oobavail : mtd->oobsize;
1434
1235 uint8_t *bufpoi, *oob, *buf; 1435 uint8_t *bufpoi, *oob, *buf;
1236 1436
1237 stats = mtd->ecc_stats; 1437 stats = mtd->ecc_stats;
@@ -1282,18 +1482,14 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
1282 buf += bytes; 1482 buf += bytes;
1283 1483
1284 if (unlikely(oob)) { 1484 if (unlikely(oob)) {
1285 /* Raw mode does data:oob:data:oob */ 1485
1286 if (ops->mode != MTD_OOB_RAW) { 1486 int toread = min(oobreadlen, max_oobsize);
1287 int toread = min(oobreadlen, 1487
1288 chip->ecc.layout->oobavail); 1488 if (toread) {
1289 if (toread) { 1489 oob = nand_transfer_oob(chip,
1290 oob = nand_transfer_oob(chip, 1490 oob, ops, toread);
1291 oob, ops, toread); 1491 oobreadlen -= toread;
1292 oobreadlen -= toread; 1492 }
1293 }
1294 } else
1295 buf = nand_transfer_oob(chip,
1296 buf, ops, mtd->oobsize);
1297 } 1493 }
1298 1494
1299 if (!(chip->options & NAND_NO_READRDY)) { 1495 if (!(chip->options & NAND_NO_READRDY)) {
@@ -1880,11 +2076,9 @@ static int nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1880 * @oob: oob data buffer 2076 * @oob: oob data buffer
1881 * @ops: oob ops structure 2077 * @ops: oob ops structure
1882 */ 2078 */
1883static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, 2079static uint8_t *nand_fill_oob(struct nand_chip *chip, uint8_t *oob, size_t len,
1884 struct mtd_oob_ops *ops) 2080 struct mtd_oob_ops *ops)
1885{ 2081{
1886 size_t len = ops->ooblen;
1887
1888 switch(ops->mode) { 2082 switch(ops->mode) {
1889 2083
1890 case MTD_OOB_PLACE: 2084 case MTD_OOB_PLACE:
@@ -1939,6 +2133,11 @@ static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1939 int chipnr, realpage, page, blockmask, column; 2133 int chipnr, realpage, page, blockmask, column;
1940 struct nand_chip *chip = mtd->priv; 2134 struct nand_chip *chip = mtd->priv;
1941 uint32_t writelen = ops->len; 2135 uint32_t writelen = ops->len;
2136
2137 uint32_t oobwritelen = ops->ooblen;
2138 uint32_t oobmaxlen = ops->mode == MTD_OOB_AUTO ?
2139 mtd->oobavail : mtd->oobsize;
2140
1942 uint8_t *oob = ops->oobbuf; 2141 uint8_t *oob = ops->oobbuf;
1943 uint8_t *buf = ops->datbuf; 2142 uint8_t *buf = ops->datbuf;
1944 int ret, subpage; 2143 int ret, subpage;
@@ -1980,6 +2179,10 @@ static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1980 if (likely(!oob)) 2179 if (likely(!oob))
1981 memset(chip->oob_poi, 0xff, mtd->oobsize); 2180 memset(chip->oob_poi, 0xff, mtd->oobsize);
1982 2181
2182 /* Don't allow multipage oob writes with offset */
2183 if (ops->ooboffs && (ops->ooboffs + ops->ooblen > oobmaxlen))
2184 return -EINVAL;
2185
1983 while(1) { 2186 while(1) {
1984 int bytes = mtd->writesize; 2187 int bytes = mtd->writesize;
1985 int cached = writelen > bytes && page != blockmask; 2188 int cached = writelen > bytes && page != blockmask;
@@ -1995,8 +2198,11 @@ static int nand_do_write_ops(struct mtd_info *mtd, loff_t to,
1995 wbuf = chip->buffers->databuf; 2198 wbuf = chip->buffers->databuf;
1996 } 2199 }
1997 2200
1998 if (unlikely(oob)) 2201 if (unlikely(oob)) {
1999 oob = nand_fill_oob(chip, oob, ops); 2202 size_t len = min(oobwritelen, oobmaxlen);
2203 oob = nand_fill_oob(chip, oob, len, ops);
2204 oobwritelen -= len;
2205 }
2000 2206
2001 ret = chip->write_page(mtd, chip, wbuf, page, cached, 2207 ret = chip->write_page(mtd, chip, wbuf, page, cached,
2002 (ops->mode == MTD_OOB_RAW)); 2208 (ops->mode == MTD_OOB_RAW));
@@ -2170,7 +2376,7 @@ static int nand_do_write_oob(struct mtd_info *mtd, loff_t to,
2170 chip->pagebuf = -1; 2376 chip->pagebuf = -1;
2171 2377
2172 memset(chip->oob_poi, 0xff, mtd->oobsize); 2378 memset(chip->oob_poi, 0xff, mtd->oobsize);
2173 nand_fill_oob(chip, ops->oobbuf, ops); 2379 nand_fill_oob(chip, ops->oobbuf, ops->ooblen, ops);
2174 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask); 2380 status = chip->ecc.write_oob(mtd, chip, page & chip->pagemask);
2175 memset(chip->oob_poi, 0xff, mtd->oobsize); 2381 memset(chip->oob_poi, 0xff, mtd->oobsize);
2176 2382
@@ -2293,25 +2499,8 @@ int nand_erase_nand(struct mtd_info *mtd, struct erase_info *instr,
2293 __func__, (unsigned long long)instr->addr, 2499 __func__, (unsigned long long)instr->addr,
2294 (unsigned long long)instr->len); 2500 (unsigned long long)instr->len);
2295 2501
2296 /* Start address must align on block boundary */ 2502 if (check_offs_len(mtd, instr->addr, instr->len))
2297 if (instr->addr & ((1 << chip->phys_erase_shift) - 1)) {
2298 DEBUG(MTD_DEBUG_LEVEL0, "%s: Unaligned address\n", __func__);
2299 return -EINVAL; 2503 return -EINVAL;
2300 }
2301
2302 /* Length must align on block boundary */
2303 if (instr->len & ((1 << chip->phys_erase_shift) - 1)) {
2304 DEBUG(MTD_DEBUG_LEVEL0, "%s: Length not block aligned\n",
2305 __func__);
2306 return -EINVAL;
2307 }
2308
2309 /* Do not allow erase past end of device */
2310 if ((instr->len + instr->addr) > mtd->size) {
2311 DEBUG(MTD_DEBUG_LEVEL0, "%s: Erase past end of device\n",
2312 __func__);
2313 return -EINVAL;
2314 }
2315 2504
2316 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; 2505 instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
2317 2506
@@ -2582,10 +2771,10 @@ static void nand_set_defaults(struct nand_chip *chip, int busw)
2582 */ 2771 */
2583static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd, 2772static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2584 struct nand_chip *chip, 2773 struct nand_chip *chip,
2585 int busw, int *maf_id) 2774 int busw, int *maf_id,
2775 struct nand_flash_dev *type)
2586{ 2776{
2587 struct nand_flash_dev *type = NULL; 2777 int dev_id, maf_idx;
2588 int i, dev_id, maf_idx;
2589 int tmp_id, tmp_manf; 2778 int tmp_id, tmp_manf;
2590 2779
2591 /* Select the device */ 2780 /* Select the device */
@@ -2624,15 +2813,14 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2624 return ERR_PTR(-ENODEV); 2813 return ERR_PTR(-ENODEV);
2625 } 2814 }
2626 2815
2627 /* Lookup the flash id */
2628 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
2629 if (dev_id == nand_flash_ids[i].id) {
2630 type = &nand_flash_ids[i];
2631 break;
2632 }
2633 }
2634
2635 if (!type) 2816 if (!type)
2817 type = nand_flash_ids;
2818
2819 for (; type->name != NULL; type++)
2820 if (dev_id == type->id)
2821 break;
2822
2823 if (!type->name)
2636 return ERR_PTR(-ENODEV); 2824 return ERR_PTR(-ENODEV);
2637 2825
2638 if (!mtd->name) 2826 if (!mtd->name)
@@ -2704,6 +2892,7 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2704 /* Set the bad block position */ 2892 /* Set the bad block position */
2705 chip->badblockpos = mtd->writesize > 512 ? 2893 chip->badblockpos = mtd->writesize > 512 ?
2706 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS; 2894 NAND_LARGE_BADBLOCK_POS : NAND_SMALL_BADBLOCK_POS;
2895 chip->badblockbits = 8;
2707 2896
2708 /* Get chip options, preserve non chip based options */ 2897 /* Get chip options, preserve non chip based options */
2709 chip->options &= ~NAND_CHIPOPTIONS_MSK; 2898 chip->options &= ~NAND_CHIPOPTIONS_MSK;
@@ -2741,13 +2930,15 @@ static struct nand_flash_dev *nand_get_flash_type(struct mtd_info *mtd,
2741 * nand_scan_ident - [NAND Interface] Scan for the NAND device 2930 * nand_scan_ident - [NAND Interface] Scan for the NAND device
2742 * @mtd: MTD device structure 2931 * @mtd: MTD device structure
2743 * @maxchips: Number of chips to scan for 2932 * @maxchips: Number of chips to scan for
2933 * @table: Alternative NAND ID table
2744 * 2934 *
2745 * This is the first phase of the normal nand_scan() function. It 2935 * This is the first phase of the normal nand_scan() function. It
2746 * reads the flash ID and sets up MTD fields accordingly. 2936 * reads the flash ID and sets up MTD fields accordingly.
2747 * 2937 *
2748 * The mtd->owner field must be set to the module of the caller. 2938 * The mtd->owner field must be set to the module of the caller.
2749 */ 2939 */
2750int nand_scan_ident(struct mtd_info *mtd, int maxchips) 2940int nand_scan_ident(struct mtd_info *mtd, int maxchips,
2941 struct nand_flash_dev *table)
2751{ 2942{
2752 int i, busw, nand_maf_id; 2943 int i, busw, nand_maf_id;
2753 struct nand_chip *chip = mtd->priv; 2944 struct nand_chip *chip = mtd->priv;
@@ -2759,7 +2950,7 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips)
2759 nand_set_defaults(chip, busw); 2950 nand_set_defaults(chip, busw);
2760 2951
2761 /* Read the flash type */ 2952 /* Read the flash type */
2762 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id); 2953 type = nand_get_flash_type(mtd, chip, busw, &nand_maf_id, table);
2763 2954
2764 if (IS_ERR(type)) { 2955 if (IS_ERR(type)) {
2765 if (!(chip->options & NAND_SCAN_SILENT_NODEV)) 2956 if (!(chip->options & NAND_SCAN_SILENT_NODEV))
@@ -2989,7 +3180,8 @@ int nand_scan_tail(struct mtd_info *mtd)
2989 3180
2990 /* Fill in remaining MTD driver data */ 3181 /* Fill in remaining MTD driver data */
2991 mtd->type = MTD_NANDFLASH; 3182 mtd->type = MTD_NANDFLASH;
2992 mtd->flags = MTD_CAP_NANDFLASH; 3183 mtd->flags = (chip->options & NAND_ROM) ? MTD_CAP_ROM :
3184 MTD_CAP_NANDFLASH;
2993 mtd->erase = nand_erase; 3185 mtd->erase = nand_erase;
2994 mtd->point = NULL; 3186 mtd->point = NULL;
2995 mtd->unpoint = NULL; 3187 mtd->unpoint = NULL;
@@ -3050,7 +3242,7 @@ int nand_scan(struct mtd_info *mtd, int maxchips)
3050 BUG(); 3242 BUG();
3051 } 3243 }
3052 3244
3053 ret = nand_scan_ident(mtd, maxchips); 3245 ret = nand_scan_ident(mtd, maxchips, NULL);
3054 if (!ret) 3246 if (!ret)
3055 ret = nand_scan_tail(mtd); 3247 ret = nand_scan_tail(mtd);
3056 return ret; 3248 return ret;
@@ -3077,6 +3269,8 @@ void nand_release(struct mtd_info *mtd)
3077 kfree(chip->buffers); 3269 kfree(chip->buffers);
3078} 3270}
3079 3271
3272EXPORT_SYMBOL_GPL(nand_lock);
3273EXPORT_SYMBOL_GPL(nand_unlock);
3080EXPORT_SYMBOL_GPL(nand_scan); 3274EXPORT_SYMBOL_GPL(nand_scan);
3081EXPORT_SYMBOL_GPL(nand_scan_ident); 3275EXPORT_SYMBOL_GPL(nand_scan_ident);
3082EXPORT_SYMBOL_GPL(nand_scan_tail); 3276EXPORT_SYMBOL_GPL(nand_scan_tail);
diff --git a/drivers/mtd/nand/nand_bbt.c b/drivers/mtd/nand/nand_bbt.c
index 55c23e5cd210..387c45c366fe 100644
--- a/drivers/mtd/nand/nand_bbt.c
+++ b/drivers/mtd/nand/nand_bbt.c
@@ -237,15 +237,33 @@ static int scan_read_raw(struct mtd_info *mtd, uint8_t *buf, loff_t offs,
237 size_t len) 237 size_t len)
238{ 238{
239 struct mtd_oob_ops ops; 239 struct mtd_oob_ops ops;
240 int res;
240 241
241 ops.mode = MTD_OOB_RAW; 242 ops.mode = MTD_OOB_RAW;
242 ops.ooboffs = 0; 243 ops.ooboffs = 0;
243 ops.ooblen = mtd->oobsize; 244 ops.ooblen = mtd->oobsize;
244 ops.oobbuf = buf;
245 ops.datbuf = buf;
246 ops.len = len;
247 245
248 return mtd->read_oob(mtd, offs, &ops); 246
247 while (len > 0) {
248 if (len <= mtd->writesize) {
249 ops.oobbuf = buf + len;
250 ops.datbuf = buf;
251 ops.len = len;
252 return mtd->read_oob(mtd, offs, &ops);
253 } else {
254 ops.oobbuf = buf + mtd->writesize;
255 ops.datbuf = buf;
256 ops.len = mtd->writesize;
257 res = mtd->read_oob(mtd, offs, &ops);
258
259 if (res)
260 return res;
261 }
262
263 buf += mtd->oobsize + mtd->writesize;
264 len -= mtd->writesize;
265 }
266 return 0;
249} 267}
250 268
251/* 269/*
diff --git a/drivers/mtd/nand/nand_bcm_umi.h b/drivers/mtd/nand/nand_bcm_umi.h
index 7cec2cd97854..198b304d6f72 100644
--- a/drivers/mtd/nand/nand_bcm_umi.h
+++ b/drivers/mtd/nand/nand_bcm_umi.h
@@ -167,18 +167,27 @@ static inline void nand_bcm_umi_bch_read_oobEcc(uint32_t pageSize,
167 int numToRead = 16; /* There are 16 bytes per sector in the OOB */ 167 int numToRead = 16; /* There are 16 bytes per sector in the OOB */
168 168
169 /* ECC is already paused when this function is called */ 169 /* ECC is already paused when this function is called */
170 if (pageSize != NAND_DATA_ACCESS_SIZE) {
171 /* skip BI */
172#if defined(__KERNEL__) && !defined(STANDALONE)
173 *oobp++ = REG_NAND_DATA8;
174#else
175 REG_NAND_DATA8;
176#endif
177 numToRead--;
178 }
170 179
171 if (pageSize == NAND_DATA_ACCESS_SIZE) { 180 while (numToRead > numEccBytes) {
172 while (numToRead > numEccBytes) { 181 /* skip free oob region */
173 /* skip free oob region */
174#if defined(__KERNEL__) && !defined(STANDALONE) 182#if defined(__KERNEL__) && !defined(STANDALONE)
175 *oobp++ = REG_NAND_DATA8; 183 *oobp++ = REG_NAND_DATA8;
176#else 184#else
177 REG_NAND_DATA8; 185 REG_NAND_DATA8;
178#endif 186#endif
179 numToRead--; 187 numToRead--;
180 } 188 }
181 189
190 if (pageSize == NAND_DATA_ACCESS_SIZE) {
182 /* read ECC bytes before BI */ 191 /* read ECC bytes before BI */
183 nand_bcm_umi_bch_resume_read_ecc_calc(); 192 nand_bcm_umi_bch_resume_read_ecc_calc();
184 193
@@ -190,6 +199,7 @@ static inline void nand_bcm_umi_bch_read_oobEcc(uint32_t pageSize,
190#else 199#else
191 eccCalc[eccPos++] = REG_NAND_DATA8; 200 eccCalc[eccPos++] = REG_NAND_DATA8;
192#endif 201#endif
202 numToRead--;
193 } 203 }
194 204
195 nand_bcm_umi_bch_pause_read_ecc_calc(); 205 nand_bcm_umi_bch_pause_read_ecc_calc();
@@ -204,49 +214,18 @@ static inline void nand_bcm_umi_bch_read_oobEcc(uint32_t pageSize,
204 numToRead--; 214 numToRead--;
205 } 215 }
206 216
207 /* read ECC bytes */ 217 }
208 nand_bcm_umi_bch_resume_read_ecc_calc(); 218 /* read ECC bytes */
209 while (numToRead) { 219 nand_bcm_umi_bch_resume_read_ecc_calc();
210#if defined(__KERNEL__) && !defined(STANDALONE) 220 while (numToRead) {
211 *oobp = REG_NAND_DATA8;
212 eccCalc[eccPos++] = *oobp;
213 oobp++;
214#else
215 eccCalc[eccPos++] = REG_NAND_DATA8;
216#endif
217 numToRead--;
218 }
219 } else {
220 /* skip BI */
221#if defined(__KERNEL__) && !defined(STANDALONE) 221#if defined(__KERNEL__) && !defined(STANDALONE)
222 *oobp++ = REG_NAND_DATA8; 222 *oobp = REG_NAND_DATA8;
223 eccCalc[eccPos++] = *oobp;
224 oobp++;
223#else 225#else
224 REG_NAND_DATA8; 226 eccCalc[eccPos++] = REG_NAND_DATA8;
225#endif 227#endif
226 numToRead--; 228 numToRead--;
227
228 while (numToRead > numEccBytes) {
229 /* skip free oob region */
230#if defined(__KERNEL__) && !defined(STANDALONE)
231 *oobp++ = REG_NAND_DATA8;
232#else
233 REG_NAND_DATA8;
234#endif
235 numToRead--;
236 }
237
238 /* read ECC bytes */
239 nand_bcm_umi_bch_resume_read_ecc_calc();
240 while (numToRead) {
241#if defined(__KERNEL__) && !defined(STANDALONE)
242 *oobp = REG_NAND_DATA8;
243 eccCalc[eccPos++] = *oobp;
244 oobp++;
245#else
246 eccCalc[eccPos++] = REG_NAND_DATA8;
247#endif
248 numToRead--;
249 }
250 } 229 }
251} 230}
252 231
diff --git a/drivers/mtd/nand/nandsim.c b/drivers/mtd/nand/nandsim.c
index 7281000fef2d..8a0a5d16e0eb 100644
--- a/drivers/mtd/nand/nandsim.c
+++ b/drivers/mtd/nand/nandsim.c
@@ -80,6 +80,9 @@
80#ifndef CONFIG_NANDSIM_DBG 80#ifndef CONFIG_NANDSIM_DBG
81#define CONFIG_NANDSIM_DBG 0 81#define CONFIG_NANDSIM_DBG 0
82#endif 82#endif
83#ifndef CONFIG_NANDSIM_MAX_PARTS
84#define CONFIG_NANDSIM_MAX_PARTS 32
85#endif
83 86
84static uint first_id_byte = CONFIG_NANDSIM_FIRST_ID_BYTE; 87static uint first_id_byte = CONFIG_NANDSIM_FIRST_ID_BYTE;
85static uint second_id_byte = CONFIG_NANDSIM_SECOND_ID_BYTE; 88static uint second_id_byte = CONFIG_NANDSIM_SECOND_ID_BYTE;
@@ -94,7 +97,7 @@ static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
94static uint do_delays = CONFIG_NANDSIM_DO_DELAYS; 97static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
95static uint log = CONFIG_NANDSIM_LOG; 98static uint log = CONFIG_NANDSIM_LOG;
96static uint dbg = CONFIG_NANDSIM_DBG; 99static uint dbg = CONFIG_NANDSIM_DBG;
97static unsigned long parts[MAX_MTD_DEVICES]; 100static unsigned long parts[CONFIG_NANDSIM_MAX_PARTS];
98static unsigned int parts_num; 101static unsigned int parts_num;
99static char *badblocks = NULL; 102static char *badblocks = NULL;
100static char *weakblocks = NULL; 103static char *weakblocks = NULL;
@@ -135,8 +138,8 @@ MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read I
135MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)"); 138MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)");
136MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds"); 139MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
137MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)"); 140MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
138MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanodeconds)"); 141MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanoseconds)");
139MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanodeconds)"); 142MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanoseconds)");
140MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)"); 143MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
141MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero"); 144MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
142MODULE_PARM_DESC(log, "Perform logging if not zero"); 145MODULE_PARM_DESC(log, "Perform logging if not zero");
@@ -288,7 +291,7 @@ union ns_mem {
288 * The structure which describes all the internal simulator data. 291 * The structure which describes all the internal simulator data.
289 */ 292 */
290struct nandsim { 293struct nandsim {
291 struct mtd_partition partitions[MAX_MTD_DEVICES]; 294 struct mtd_partition partitions[CONFIG_NANDSIM_MAX_PARTS];
292 unsigned int nbparts; 295 unsigned int nbparts;
293 296
294 uint busw; /* flash chip bus width (8 or 16) */ 297 uint busw; /* flash chip bus width (8 or 16) */
diff --git a/drivers/mtd/nand/nomadik_nand.c b/drivers/mtd/nand/nomadik_nand.c
index 66123419f65d..59cbf66607c7 100644
--- a/drivers/mtd/nand/nomadik_nand.c
+++ b/drivers/mtd/nand/nomadik_nand.c
@@ -104,21 +104,21 @@ static int nomadik_nand_probe(struct platform_device *pdev)
104 ret = -EIO; 104 ret = -EIO;
105 goto err_unmap; 105 goto err_unmap;
106 } 106 }
107 host->addr_va = ioremap(res->start, res->end - res->start + 1); 107 host->addr_va = ioremap(res->start, resource_size(res));
108 108
109 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data"); 109 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_data");
110 if (!res) { 110 if (!res) {
111 ret = -EIO; 111 ret = -EIO;
112 goto err_unmap; 112 goto err_unmap;
113 } 113 }
114 host->data_va = ioremap(res->start, res->end - res->start + 1); 114 host->data_va = ioremap(res->start, resource_size(res));
115 115
116 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd"); 116 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand_cmd");
117 if (!res) { 117 if (!res) {
118 ret = -EIO; 118 ret = -EIO;
119 goto err_unmap; 119 goto err_unmap;
120 } 120 }
121 host->cmd_va = ioremap(res->start, res->end - res->start + 1); 121 host->cmd_va = ioremap(res->start, resource_size(res));
122 122
123 if (!host->addr_va || !host->data_va || !host->cmd_va) { 123 if (!host->addr_va || !host->data_va || !host->cmd_va) {
124 ret = -ENOMEM; 124 ret = -ENOMEM;
diff --git a/drivers/mtd/nand/w90p910_nand.c b/drivers/mtd/nand/nuc900_nand.c
index 7680e731348a..6eddf7361ed7 100644
--- a/drivers/mtd/nand/w90p910_nand.c
+++ b/drivers/mtd/nand/nuc900_nand.c
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2009 Nuvoton technology corporation. 2 * Copyright © 2009 Nuvoton technology corporation.
3 * 3 *
4 * Wan ZongShun <mcuos.com@gmail.com> 4 * Wan ZongShun <mcuos.com@gmail.com>
5 * 5 *
@@ -55,7 +55,7 @@
55#define write_addr_reg(dev, val) \ 55#define write_addr_reg(dev, val) \
56 __raw_writel((val), (dev)->reg + REG_SMADDR) 56 __raw_writel((val), (dev)->reg + REG_SMADDR)
57 57
58struct w90p910_nand { 58struct nuc900_nand {
59 struct mtd_info mtd; 59 struct mtd_info mtd;
60 struct nand_chip chip; 60 struct nand_chip chip;
61 void __iomem *reg; 61 void __iomem *reg;
@@ -76,49 +76,49 @@ static const struct mtd_partition partitions[] = {
76 } 76 }
77}; 77};
78 78
79static unsigned char w90p910_nand_read_byte(struct mtd_info *mtd) 79static unsigned char nuc900_nand_read_byte(struct mtd_info *mtd)
80{ 80{
81 unsigned char ret; 81 unsigned char ret;
82 struct w90p910_nand *nand; 82 struct nuc900_nand *nand;
83 83
84 nand = container_of(mtd, struct w90p910_nand, mtd); 84 nand = container_of(mtd, struct nuc900_nand, mtd);
85 85
86 ret = (unsigned char)read_data_reg(nand); 86 ret = (unsigned char)read_data_reg(nand);
87 87
88 return ret; 88 return ret;
89} 89}
90 90
91static void w90p910_nand_read_buf(struct mtd_info *mtd, 91static void nuc900_nand_read_buf(struct mtd_info *mtd,
92 unsigned char *buf, int len) 92 unsigned char *buf, int len)
93{ 93{
94 int i; 94 int i;
95 struct w90p910_nand *nand; 95 struct nuc900_nand *nand;
96 96
97 nand = container_of(mtd, struct w90p910_nand, mtd); 97 nand = container_of(mtd, struct nuc900_nand, mtd);
98 98
99 for (i = 0; i < len; i++) 99 for (i = 0; i < len; i++)
100 buf[i] = (unsigned char)read_data_reg(nand); 100 buf[i] = (unsigned char)read_data_reg(nand);
101} 101}
102 102
103static void w90p910_nand_write_buf(struct mtd_info *mtd, 103static void nuc900_nand_write_buf(struct mtd_info *mtd,
104 const unsigned char *buf, int len) 104 const unsigned char *buf, int len)
105{ 105{
106 int i; 106 int i;
107 struct w90p910_nand *nand; 107 struct nuc900_nand *nand;
108 108
109 nand = container_of(mtd, struct w90p910_nand, mtd); 109 nand = container_of(mtd, struct nuc900_nand, mtd);
110 110
111 for (i = 0; i < len; i++) 111 for (i = 0; i < len; i++)
112 write_data_reg(nand, buf[i]); 112 write_data_reg(nand, buf[i]);
113} 113}
114 114
115static int w90p910_verify_buf(struct mtd_info *mtd, 115static int nuc900_verify_buf(struct mtd_info *mtd,
116 const unsigned char *buf, int len) 116 const unsigned char *buf, int len)
117{ 117{
118 int i; 118 int i;
119 struct w90p910_nand *nand; 119 struct nuc900_nand *nand;
120 120
121 nand = container_of(mtd, struct w90p910_nand, mtd); 121 nand = container_of(mtd, struct nuc900_nand, mtd);
122 122
123 for (i = 0; i < len; i++) { 123 for (i = 0; i < len; i++) {
124 if (buf[i] != (unsigned char)read_data_reg(nand)) 124 if (buf[i] != (unsigned char)read_data_reg(nand))
@@ -128,7 +128,7 @@ static int w90p910_verify_buf(struct mtd_info *mtd,
128 return 0; 128 return 0;
129} 129}
130 130
131static int w90p910_check_rb(struct w90p910_nand *nand) 131static int nuc900_check_rb(struct nuc900_nand *nand)
132{ 132{
133 unsigned int val; 133 unsigned int val;
134 spin_lock(&nand->lock); 134 spin_lock(&nand->lock);
@@ -139,24 +139,24 @@ static int w90p910_check_rb(struct w90p910_nand *nand)
139 return val; 139 return val;
140} 140}
141 141
142static int w90p910_nand_devready(struct mtd_info *mtd) 142static int nuc900_nand_devready(struct mtd_info *mtd)
143{ 143{
144 struct w90p910_nand *nand; 144 struct nuc900_nand *nand;
145 int ready; 145 int ready;
146 146
147 nand = container_of(mtd, struct w90p910_nand, mtd); 147 nand = container_of(mtd, struct nuc900_nand, mtd);
148 148
149 ready = (w90p910_check_rb(nand)) ? 1 : 0; 149 ready = (nuc900_check_rb(nand)) ? 1 : 0;
150 return ready; 150 return ready;
151} 151}
152 152
153static void w90p910_nand_command_lp(struct mtd_info *mtd, 153static void nuc900_nand_command_lp(struct mtd_info *mtd, unsigned int command,
154 unsigned int command, int column, int page_addr) 154 int column, int page_addr)
155{ 155{
156 register struct nand_chip *chip = mtd->priv; 156 register struct nand_chip *chip = mtd->priv;
157 struct w90p910_nand *nand; 157 struct nuc900_nand *nand;
158 158
159 nand = container_of(mtd, struct w90p910_nand, mtd); 159 nand = container_of(mtd, struct nuc900_nand, mtd);
160 160
161 if (command == NAND_CMD_READOOB) { 161 if (command == NAND_CMD_READOOB) {
162 column += mtd->writesize; 162 column += mtd->writesize;
@@ -212,7 +212,7 @@ static void w90p910_nand_command_lp(struct mtd_info *mtd,
212 write_cmd_reg(nand, NAND_CMD_STATUS); 212 write_cmd_reg(nand, NAND_CMD_STATUS);
213 write_cmd_reg(nand, command); 213 write_cmd_reg(nand, command);
214 214
215 while (!w90p910_check_rb(nand)) 215 while (!nuc900_check_rb(nand))
216 ; 216 ;
217 217
218 return; 218 return;
@@ -241,7 +241,7 @@ static void w90p910_nand_command_lp(struct mtd_info *mtd,
241} 241}
242 242
243 243
244static void w90p910_nand_enable(struct w90p910_nand *nand) 244static void nuc900_nand_enable(struct nuc900_nand *nand)
245{ 245{
246 unsigned int val; 246 unsigned int val;
247 spin_lock(&nand->lock); 247 spin_lock(&nand->lock);
@@ -262,37 +262,37 @@ static void w90p910_nand_enable(struct w90p910_nand *nand)
262 spin_unlock(&nand->lock); 262 spin_unlock(&nand->lock);
263} 263}
264 264
265static int __devinit w90p910_nand_probe(struct platform_device *pdev) 265static int __devinit nuc900_nand_probe(struct platform_device *pdev)
266{ 266{
267 struct w90p910_nand *w90p910_nand; 267 struct nuc900_nand *nuc900_nand;
268 struct nand_chip *chip; 268 struct nand_chip *chip;
269 int retval; 269 int retval;
270 struct resource *res; 270 struct resource *res;
271 271
272 retval = 0; 272 retval = 0;
273 273
274 w90p910_nand = kzalloc(sizeof(struct w90p910_nand), GFP_KERNEL); 274 nuc900_nand = kzalloc(sizeof(struct nuc900_nand), GFP_KERNEL);
275 if (!w90p910_nand) 275 if (!nuc900_nand)
276 return -ENOMEM; 276 return -ENOMEM;
277 chip = &(w90p910_nand->chip); 277 chip = &(nuc900_nand->chip);
278 278
279 w90p910_nand->mtd.priv = chip; 279 nuc900_nand->mtd.priv = chip;
280 w90p910_nand->mtd.owner = THIS_MODULE; 280 nuc900_nand->mtd.owner = THIS_MODULE;
281 spin_lock_init(&w90p910_nand->lock); 281 spin_lock_init(&nuc900_nand->lock);
282 282
283 w90p910_nand->clk = clk_get(&pdev->dev, NULL); 283 nuc900_nand->clk = clk_get(&pdev->dev, NULL);
284 if (IS_ERR(w90p910_nand->clk)) { 284 if (IS_ERR(nuc900_nand->clk)) {
285 retval = -ENOENT; 285 retval = -ENOENT;
286 goto fail1; 286 goto fail1;
287 } 287 }
288 clk_enable(w90p910_nand->clk); 288 clk_enable(nuc900_nand->clk);
289 289
290 chip->cmdfunc = w90p910_nand_command_lp; 290 chip->cmdfunc = nuc900_nand_command_lp;
291 chip->dev_ready = w90p910_nand_devready; 291 chip->dev_ready = nuc900_nand_devready;
292 chip->read_byte = w90p910_nand_read_byte; 292 chip->read_byte = nuc900_nand_read_byte;
293 chip->write_buf = w90p910_nand_write_buf; 293 chip->write_buf = nuc900_nand_write_buf;
294 chip->read_buf = w90p910_nand_read_buf; 294 chip->read_buf = nuc900_nand_read_buf;
295 chip->verify_buf = w90p910_verify_buf; 295 chip->verify_buf = nuc900_verify_buf;
296 chip->chip_delay = 50; 296 chip->chip_delay = 50;
297 chip->options = 0; 297 chip->options = 0;
298 chip->ecc.mode = NAND_ECC_SOFT; 298 chip->ecc.mode = NAND_ECC_SOFT;
@@ -308,75 +308,75 @@ static int __devinit w90p910_nand_probe(struct platform_device *pdev)
308 goto fail1; 308 goto fail1;
309 } 309 }
310 310
311 w90p910_nand->reg = ioremap(res->start, resource_size(res)); 311 nuc900_nand->reg = ioremap(res->start, resource_size(res));
312 if (!w90p910_nand->reg) { 312 if (!nuc900_nand->reg) {
313 retval = -ENOMEM; 313 retval = -ENOMEM;
314 goto fail2; 314 goto fail2;
315 } 315 }
316 316
317 w90p910_nand_enable(w90p910_nand); 317 nuc900_nand_enable(nuc900_nand);
318 318
319 if (nand_scan(&(w90p910_nand->mtd), 1)) { 319 if (nand_scan(&(nuc900_nand->mtd), 1)) {
320 retval = -ENXIO; 320 retval = -ENXIO;
321 goto fail3; 321 goto fail3;
322 } 322 }
323 323
324 add_mtd_partitions(&(w90p910_nand->mtd), partitions, 324 add_mtd_partitions(&(nuc900_nand->mtd), partitions,
325 ARRAY_SIZE(partitions)); 325 ARRAY_SIZE(partitions));
326 326
327 platform_set_drvdata(pdev, w90p910_nand); 327 platform_set_drvdata(pdev, nuc900_nand);
328 328
329 return retval; 329 return retval;
330 330
331fail3: iounmap(w90p910_nand->reg); 331fail3: iounmap(nuc900_nand->reg);
332fail2: release_mem_region(res->start, resource_size(res)); 332fail2: release_mem_region(res->start, resource_size(res));
333fail1: kfree(w90p910_nand); 333fail1: kfree(nuc900_nand);
334 return retval; 334 return retval;
335} 335}
336 336
337static int __devexit w90p910_nand_remove(struct platform_device *pdev) 337static int __devexit nuc900_nand_remove(struct platform_device *pdev)
338{ 338{
339 struct w90p910_nand *w90p910_nand = platform_get_drvdata(pdev); 339 struct nuc900_nand *nuc900_nand = platform_get_drvdata(pdev);
340 struct resource *res; 340 struct resource *res;
341 341
342 iounmap(w90p910_nand->reg); 342 iounmap(nuc900_nand->reg);
343 343
344 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 344 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
345 release_mem_region(res->start, resource_size(res)); 345 release_mem_region(res->start, resource_size(res));
346 346
347 clk_disable(w90p910_nand->clk); 347 clk_disable(nuc900_nand->clk);
348 clk_put(w90p910_nand->clk); 348 clk_put(nuc900_nand->clk);
349 349
350 kfree(w90p910_nand); 350 kfree(nuc900_nand);
351 351
352 platform_set_drvdata(pdev, NULL); 352 platform_set_drvdata(pdev, NULL);
353 353
354 return 0; 354 return 0;
355} 355}
356 356
357static struct platform_driver w90p910_nand_driver = { 357static struct platform_driver nuc900_nand_driver = {
358 .probe = w90p910_nand_probe, 358 .probe = nuc900_nand_probe,
359 .remove = __devexit_p(w90p910_nand_remove), 359 .remove = __devexit_p(nuc900_nand_remove),
360 .driver = { 360 .driver = {
361 .name = "w90p910-fmi", 361 .name = "nuc900-fmi",
362 .owner = THIS_MODULE, 362 .owner = THIS_MODULE,
363 }, 363 },
364}; 364};
365 365
366static int __init w90p910_nand_init(void) 366static int __init nuc900_nand_init(void)
367{ 367{
368 return platform_driver_register(&w90p910_nand_driver); 368 return platform_driver_register(&nuc900_nand_driver);
369} 369}
370 370
371static void __exit w90p910_nand_exit(void) 371static void __exit nuc900_nand_exit(void)
372{ 372{
373 platform_driver_unregister(&w90p910_nand_driver); 373 platform_driver_unregister(&nuc900_nand_driver);
374} 374}
375 375
376module_init(w90p910_nand_init); 376module_init(nuc900_nand_init);
377module_exit(w90p910_nand_exit); 377module_exit(nuc900_nand_exit);
378 378
379MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>"); 379MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
380MODULE_DESCRIPTION("w90p910 nand driver!"); 380MODULE_DESCRIPTION("w90p910/NUC9xx nand driver!");
381MODULE_LICENSE("GPL"); 381MODULE_LICENSE("GPL");
382MODULE_ALIAS("platform:w90p910-fmi"); 382MODULE_ALIAS("platform:nuc900-fmi");
diff --git a/drivers/mtd/nand/omap2.c b/drivers/mtd/nand/omap2.c
index 1bb799f0125c..7df303aed8a4 100644
--- a/drivers/mtd/nand/omap2.c
+++ b/drivers/mtd/nand/omap2.c
@@ -295,11 +295,14 @@ static void omap_read_buf_pref(struct mtd_info *mtd, u_char *buf, int len)
295 u32 *p = (u32 *)buf; 295 u32 *p = (u32 *)buf;
296 296
297 /* take care of subpage reads */ 297 /* take care of subpage reads */
298 for (; len % 4 != 0; ) { 298 if (len % 4) {
299 *buf++ = __raw_readb(info->nand.IO_ADDR_R); 299 if (info->nand.options & NAND_BUSWIDTH_16)
300 len--; 300 omap_read_buf16(mtd, buf, len % 4);
301 else
302 omap_read_buf8(mtd, buf, len % 4);
303 p = (u32 *) (buf + len % 4);
304 len -= len % 4;
301 } 305 }
302 p = (u32 *) buf;
303 306
304 /* configure and start prefetch transfer */ 307 /* configure and start prefetch transfer */
305 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x0); 308 ret = gpmc_prefetch_enable(info->gpmc_cs, 0x0, len, 0x0);
@@ -505,7 +508,7 @@ static void omap_write_buf_dma_pref(struct mtd_info *mtd,
505 omap_write_buf_pref(mtd, buf, len); 508 omap_write_buf_pref(mtd, buf, len);
506 else 509 else
507 /* start transfer in DMA mode */ 510 /* start transfer in DMA mode */
508 omap_nand_dma_transfer(mtd, buf, len, 0x1); 511 omap_nand_dma_transfer(mtd, (u_char *) buf, len, 0x1);
509} 512}
510 513
511/** 514/**
@@ -1054,7 +1057,8 @@ out_free_info:
1054static int omap_nand_remove(struct platform_device *pdev) 1057static int omap_nand_remove(struct platform_device *pdev)
1055{ 1058{
1056 struct mtd_info *mtd = platform_get_drvdata(pdev); 1059 struct mtd_info *mtd = platform_get_drvdata(pdev);
1057 struct omap_nand_info *info = mtd->priv; 1060 struct omap_nand_info *info = container_of(mtd, struct omap_nand_info,
1061 mtd);
1058 1062
1059 platform_set_drvdata(pdev, NULL); 1063 platform_set_drvdata(pdev, NULL);
1060 if (use_dma) 1064 if (use_dma)
diff --git a/drivers/mtd/nand/orion_nand.c b/drivers/mtd/nand/orion_nand.c
index f59c07427af3..f16050c61c5c 100644
--- a/drivers/mtd/nand/orion_nand.c
+++ b/drivers/mtd/nand/orion_nand.c
@@ -74,6 +74,7 @@ static int __init orion_nand_probe(struct platform_device *pdev)
74 struct mtd_info *mtd; 74 struct mtd_info *mtd;
75 struct nand_chip *nc; 75 struct nand_chip *nc;
76 struct orion_nand_data *board; 76 struct orion_nand_data *board;
77 struct resource *res;
77 void __iomem *io_base; 78 void __iomem *io_base;
78 int ret = 0; 79 int ret = 0;
79#ifdef CONFIG_MTD_PARTITIONS 80#ifdef CONFIG_MTD_PARTITIONS
@@ -89,8 +90,13 @@ static int __init orion_nand_probe(struct platform_device *pdev)
89 } 90 }
90 mtd = (struct mtd_info *)(nc + 1); 91 mtd = (struct mtd_info *)(nc + 1);
91 92
92 io_base = ioremap(pdev->resource[0].start, 93 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
93 pdev->resource[0].end - pdev->resource[0].start + 1); 94 if (!res) {
95 ret = -ENODEV;
96 goto no_res;
97 }
98
99 io_base = ioremap(res->start, resource_size(res));
94 if (!io_base) { 100 if (!io_base) {
95 printk(KERN_ERR "orion_nand: ioremap failed\n"); 101 printk(KERN_ERR "orion_nand: ioremap failed\n");
96 ret = -EIO; 102 ret = -EIO;
diff --git a/drivers/mtd/nand/pasemi_nand.c b/drivers/mtd/nand/pasemi_nand.c
index a8b9376cf324..090a05c12cbe 100644
--- a/drivers/mtd/nand/pasemi_nand.c
+++ b/drivers/mtd/nand/pasemi_nand.c
@@ -209,7 +209,7 @@ static int __devexit pasemi_nand_remove(struct of_device *ofdev)
209 return 0; 209 return 0;
210} 210}
211 211
212static struct of_device_id pasemi_nand_match[] = 212static const struct of_device_id pasemi_nand_match[] =
213{ 213{
214 { 214 {
215 .compatible = "pasemi,localbus-nand", 215 .compatible = "pasemi,localbus-nand",
diff --git a/drivers/mtd/nand/s3c2410.c b/drivers/mtd/nand/s3c2410.c
index fa6e9c7fe511..dc02dcd0c08f 100644
--- a/drivers/mtd/nand/s3c2410.c
+++ b/drivers/mtd/nand/s3c2410.c
@@ -957,7 +957,7 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)
957 957
958 /* currently we assume we have the one resource */ 958 /* currently we assume we have the one resource */
959 res = pdev->resource; 959 res = pdev->resource;
960 size = res->end - res->start + 1; 960 size = resource_size(res);
961 961
962 info->area = request_mem_region(res->start, size, pdev->name); 962 info->area = request_mem_region(res->start, size, pdev->name);
963 963
@@ -1013,7 +1013,8 @@ static int s3c24xx_nand_probe(struct platform_device *pdev)
1013 s3c2410_nand_init_chip(info, nmtd, sets); 1013 s3c2410_nand_init_chip(info, nmtd, sets);
1014 1014
1015 nmtd->scan_res = nand_scan_ident(&nmtd->mtd, 1015 nmtd->scan_res = nand_scan_ident(&nmtd->mtd,
1016 (sets) ? sets->nr_chips : 1); 1016 (sets) ? sets->nr_chips : 1,
1017 NULL);
1017 1018
1018 if (nmtd->scan_res == 0) { 1019 if (nmtd->scan_res == 0) {
1019 s3c2410_nand_update_chip(info, nmtd); 1020 s3c2410_nand_update_chip(info, nmtd);
diff --git a/drivers/mtd/nand/sh_flctl.c b/drivers/mtd/nand/sh_flctl.c
index 02bef21f2e4b..dbc09a81866e 100644
--- a/drivers/mtd/nand/sh_flctl.c
+++ b/drivers/mtd/nand/sh_flctl.c
@@ -797,7 +797,7 @@ static int __init flctl_probe(struct platform_device *pdev)
797 goto err; 797 goto err;
798 } 798 }
799 799
800 flctl->reg = ioremap(res->start, res->end - res->start + 1); 800 flctl->reg = ioremap(res->start, resource_size(res));
801 if (flctl->reg == NULL) { 801 if (flctl->reg == NULL) {
802 printk(KERN_ERR "%s: ioremap error.\n", __func__); 802 printk(KERN_ERR "%s: ioremap error.\n", __func__);
803 ret = -ENOMEM; 803 ret = -ENOMEM;
@@ -825,7 +825,7 @@ static int __init flctl_probe(struct platform_device *pdev)
825 nand->select_chip = flctl_select_chip; 825 nand->select_chip = flctl_select_chip;
826 nand->cmdfunc = flctl_cmdfunc; 826 nand->cmdfunc = flctl_cmdfunc;
827 827
828 ret = nand_scan_ident(flctl_mtd, 1); 828 ret = nand_scan_ident(flctl_mtd, 1, NULL);
829 if (ret) 829 if (ret)
830 goto err; 830 goto err;
831 831
diff --git a/drivers/mtd/nand/sm_common.c b/drivers/mtd/nand/sm_common.c
new file mode 100644
index 000000000000..aae0b9acd7ae
--- /dev/null
+++ b/drivers/mtd/nand/sm_common.c
@@ -0,0 +1,143 @@
1/*
2 * Copyright © 2009 - Maxim Levitsky
3 * Common routines & support for xD format
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/kernel.h>
10#include <linux/mtd/nand.h>
11#include "sm_common.h"
12
13static struct nand_ecclayout nand_oob_sm = {
14 .eccbytes = 6,
15 .eccpos = {8, 9, 10, 13, 14, 15},
16 .oobfree = {
17 {.offset = 0 , .length = 4}, /* reserved */
18 {.offset = 6 , .length = 2}, /* LBA1 */
19 {.offset = 11, .length = 2} /* LBA2 */
20 }
21};
22
23/* NOTE: This layout is is not compatabable with SmartMedia, */
24/* because the 256 byte devices have page depenent oob layout */
25/* However it does preserve the bad block markers */
26/* If you use smftl, it will bypass this and work correctly */
27/* If you not, then you break SmartMedia compliance anyway */
28
29static struct nand_ecclayout nand_oob_sm_small = {
30 .eccbytes = 3,
31 .eccpos = {0, 1, 2},
32 .oobfree = {
33 {.offset = 3 , .length = 2}, /* reserved */
34 {.offset = 6 , .length = 2}, /* LBA1 */
35 }
36};
37
38
39static int sm_block_markbad(struct mtd_info *mtd, loff_t ofs)
40{
41 struct mtd_oob_ops ops;
42 struct sm_oob oob;
43 int ret, error = 0;
44
45 memset(&oob, -1, SM_OOB_SIZE);
46 oob.block_status = 0x0F;
47
48 /* As long as this function is called on erase block boundaries
49 it will work correctly for 256 byte nand */
50 ops.mode = MTD_OOB_PLACE;
51 ops.ooboffs = 0;
52 ops.ooblen = mtd->oobsize;
53 ops.oobbuf = (void *)&oob;
54 ops.datbuf = NULL;
55
56
57 ret = mtd->write_oob(mtd, ofs, &ops);
58 if (ret < 0 || ops.oobretlen != SM_OOB_SIZE) {
59 printk(KERN_NOTICE
60 "sm_common: can't mark sector at %i as bad\n",
61 (int)ofs);
62 error = -EIO;
63 } else
64 mtd->ecc_stats.badblocks++;
65
66 return error;
67}
68
69
70static struct nand_flash_dev nand_smartmedia_flash_ids[] = {
71
72 /* SmartMedia */
73 {"SmartMedia 1MiB 5V", 0x6e, 256, 1, 0x1000, 0},
74 {"SmartMedia 1MiB 3,3V", 0xe8, 256, 1, 0x1000, 0},
75 {"SmartMedia 1MiB 3,3V", 0xec, 256, 1, 0x1000, 0},
76 {"SmartMedia 2MiB 3,3V", 0xea, 256, 2, 0x1000, 0},
77 {"SmartMedia 2MiB 5V", 0x64, 256, 2, 0x1000, 0},
78 {"SmartMedia 2MiB 3,3V ROM", 0x5d, 512, 2, 0x2000, NAND_ROM},
79 {"SmartMedia 4MiB 3,3V", 0xe3, 512, 4, 0x2000, 0},
80 {"SmartMedia 4MiB 3,3/5V", 0xe5, 512, 4, 0x2000, 0},
81 {"SmartMedia 4MiB 5V", 0x6b, 512, 4, 0x2000, 0},
82 {"SmartMedia 4MiB 3,3V ROM", 0xd5, 512, 4, 0x2000, NAND_ROM},
83 {"SmartMedia 8MiB 3,3V", 0xe6, 512, 8, 0x2000, 0},
84 {"SmartMedia 8MiB 3,3V ROM", 0xd6, 512, 8, 0x2000, NAND_ROM},
85
86#define XD_TYPEM (NAND_NO_AUTOINCR | NAND_BROKEN_XD)
87 /* xD / SmartMedia */
88 {"SmartMedia/xD 16MiB 3,3V", 0x73, 512, 16, 0x4000, 0},
89 {"SmartMedia 16MiB 3,3V ROM", 0x57, 512, 16, 0x4000, NAND_ROM},
90 {"SmartMedia/xD 32MiB 3,3V", 0x75, 512, 32, 0x4000, 0},
91 {"SmartMedia 32MiB 3,3V ROM", 0x58, 512, 32, 0x4000, NAND_ROM},
92 {"SmartMedia/xD 64MiB 3,3V", 0x76, 512, 64, 0x4000, 0},
93 {"SmartMedia 64MiB 3,3V ROM", 0xd9, 512, 64, 0x4000, NAND_ROM},
94 {"SmartMedia/xD 128MiB 3,3V", 0x79, 512, 128, 0x4000, 0},
95 {"SmartMedia 128MiB 3,3V ROM", 0xda, 512, 128, 0x4000, NAND_ROM},
96 {"SmartMedia/xD 256MiB 3,3V", 0x71, 512, 256, 0x4000, XD_TYPEM},
97 {"SmartMedia 256MiB 3,3V ROM", 0x5b, 512, 256, 0x4000, NAND_ROM},
98
99 /* xD only */
100 {"xD 512MiB 3,3V", 0xDC, 512, 512, 0x4000, XD_TYPEM},
101 {"xD 1GiB 3,3V", 0xD3, 512, 1024, 0x4000, XD_TYPEM},
102 {"xD 2GiB 3,3V", 0xD5, 512, 2048, 0x4000, XD_TYPEM},
103 {NULL,}
104};
105
106int sm_register_device(struct mtd_info *mtd)
107{
108 struct nand_chip *chip = (struct nand_chip *)mtd->priv;
109 int ret;
110
111 chip->options |= NAND_SKIP_BBTSCAN;
112
113 /* Scan for card properties */
114 ret = nand_scan_ident(mtd, 1, nand_smartmedia_flash_ids);
115
116 if (ret)
117 return ret;
118
119 /* Bad block marker postion */
120 chip->badblockpos = 0x05;
121 chip->badblockbits = 7;
122 chip->block_markbad = sm_block_markbad;
123
124 /* ECC layout */
125 if (mtd->writesize == SM_SECTOR_SIZE)
126 chip->ecc.layout = &nand_oob_sm;
127 else if (mtd->writesize == SM_SMALL_PAGE)
128 chip->ecc.layout = &nand_oob_sm_small;
129 else
130 return -ENODEV;
131
132 ret = nand_scan_tail(mtd);
133
134 if (ret)
135 return ret;
136
137 return add_mtd_device(mtd);
138}
139EXPORT_SYMBOL_GPL(sm_register_device);
140
141MODULE_LICENSE("GPL");
142MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
143MODULE_DESCRIPTION("Common SmartMedia/xD functions");
diff --git a/drivers/mtd/nand/sm_common.h b/drivers/mtd/nand/sm_common.h
new file mode 100644
index 000000000000..7c03314bdac5
--- /dev/null
+++ b/drivers/mtd/nand/sm_common.h
@@ -0,0 +1,61 @@
1/*
2 * Copyright © 2009 - Maxim Levitsky
3 * Common routines & support for SmartMedia/xD format
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9#include <linux/bitops.h>
10#include <linux/mtd/mtd.h>
11
12/* Full oob structure as written on the flash */
13struct sm_oob {
14 uint32_t reserved;
15 uint8_t data_status;
16 uint8_t block_status;
17 uint8_t lba_copy1[2];
18 uint8_t ecc2[3];
19 uint8_t lba_copy2[2];
20 uint8_t ecc1[3];
21} __attribute__((packed));
22
23
24/* one sector is always 512 bytes, but it can consist of two nand pages */
25#define SM_SECTOR_SIZE 512
26
27/* oob area is also 16 bytes, but might be from two pages */
28#define SM_OOB_SIZE 16
29
30/* This is maximum zone size, and all devices that have more that one zone
31 have this size */
32#define SM_MAX_ZONE_SIZE 1024
33
34/* support for small page nand */
35#define SM_SMALL_PAGE 256
36#define SM_SMALL_OOB_SIZE 8
37
38
39extern int sm_register_device(struct mtd_info *mtd);
40
41
42inline int sm_sector_valid(struct sm_oob *oob)
43{
44 return hweight16(oob->data_status) >= 5;
45}
46
47inline int sm_block_valid(struct sm_oob *oob)
48{
49 return hweight16(oob->block_status) >= 7;
50}
51
52inline int sm_block_erased(struct sm_oob *oob)
53{
54 static const uint32_t erased_pattern[4] = {
55 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
56
57 /* First test for erased block */
58 if (!memcmp(oob, erased_pattern, sizeof(*oob)))
59 return 1;
60 return 0;
61}
diff --git a/drivers/mtd/nand/socrates_nand.c b/drivers/mtd/nand/socrates_nand.c
index a4519a7bd683..b37cbde6e7db 100644
--- a/drivers/mtd/nand/socrates_nand.c
+++ b/drivers/mtd/nand/socrates_nand.c
@@ -220,7 +220,7 @@ static int __devinit socrates_nand_probe(struct of_device *ofdev,
220 dev_set_drvdata(&ofdev->dev, host); 220 dev_set_drvdata(&ofdev->dev, host);
221 221
222 /* first scan to find the device and get the page size */ 222 /* first scan to find the device and get the page size */
223 if (nand_scan_ident(mtd, 1)) { 223 if (nand_scan_ident(mtd, 1, NULL)) {
224 res = -ENXIO; 224 res = -ENXIO;
225 goto out; 225 goto out;
226 } 226 }
@@ -290,7 +290,7 @@ static int __devexit socrates_nand_remove(struct of_device *ofdev)
290 return 0; 290 return 0;
291} 291}
292 292
293static struct of_device_id socrates_nand_match[] = 293static const struct of_device_id socrates_nand_match[] =
294{ 294{
295 { 295 {
296 .compatible = "abb,socrates-nand", 296 .compatible = "abb,socrates-nand",
diff --git a/drivers/mtd/nand/tmio_nand.c b/drivers/mtd/nand/tmio_nand.c
index 92c73344a669..65fa46957dbb 100644
--- a/drivers/mtd/nand/tmio_nand.c
+++ b/drivers/mtd/nand/tmio_nand.c
@@ -318,7 +318,7 @@ static int tmio_nand_correct_data(struct mtd_info *mtd, unsigned char *buf,
318 318
319static int tmio_hw_init(struct platform_device *dev, struct tmio_nand *tmio) 319static int tmio_hw_init(struct platform_device *dev, struct tmio_nand *tmio)
320{ 320{
321 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; 321 struct mfd_cell *cell = dev_get_platdata(&dev->dev);
322 int ret; 322 int ret;
323 323
324 if (cell->enable) { 324 if (cell->enable) {
@@ -362,7 +362,7 @@ static int tmio_hw_init(struct platform_device *dev, struct tmio_nand *tmio)
362 362
363static void tmio_hw_stop(struct platform_device *dev, struct tmio_nand *tmio) 363static void tmio_hw_stop(struct platform_device *dev, struct tmio_nand *tmio)
364{ 364{
365 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; 365 struct mfd_cell *cell = dev_get_platdata(&dev->dev);
366 366
367 tmio_iowrite8(FCR_MODE_POWER_OFF, tmio->fcr + FCR_MODE); 367 tmio_iowrite8(FCR_MODE_POWER_OFF, tmio->fcr + FCR_MODE);
368 if (cell->disable) 368 if (cell->disable)
@@ -371,7 +371,7 @@ static void tmio_hw_stop(struct platform_device *dev, struct tmio_nand *tmio)
371 371
372static int tmio_probe(struct platform_device *dev) 372static int tmio_probe(struct platform_device *dev)
373{ 373{
374 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; 374 struct mfd_cell *cell = dev_get_platdata(&dev->dev);
375 struct tmio_nand_data *data = cell->driver_data; 375 struct tmio_nand_data *data = cell->driver_data;
376 struct resource *fcr = platform_get_resource(dev, 376 struct resource *fcr = platform_get_resource(dev,
377 IORESOURCE_MEM, 0); 377 IORESOURCE_MEM, 0);
@@ -404,14 +404,14 @@ static int tmio_probe(struct platform_device *dev)
404 mtd->priv = nand_chip; 404 mtd->priv = nand_chip;
405 mtd->name = "tmio-nand"; 405 mtd->name = "tmio-nand";
406 406
407 tmio->ccr = ioremap(ccr->start, ccr->end - ccr->start + 1); 407 tmio->ccr = ioremap(ccr->start, resource_size(ccr));
408 if (!tmio->ccr) { 408 if (!tmio->ccr) {
409 retval = -EIO; 409 retval = -EIO;
410 goto err_iomap_ccr; 410 goto err_iomap_ccr;
411 } 411 }
412 412
413 tmio->fcr_base = fcr->start & 0xfffff; 413 tmio->fcr_base = fcr->start & 0xfffff;
414 tmio->fcr = ioremap(fcr->start, fcr->end - fcr->start + 1); 414 tmio->fcr = ioremap(fcr->start, resource_size(fcr));
415 if (!tmio->fcr) { 415 if (!tmio->fcr) {
416 retval = -EIO; 416 retval = -EIO;
417 goto err_iomap_fcr; 417 goto err_iomap_fcr;
@@ -515,7 +515,7 @@ static int tmio_remove(struct platform_device *dev)
515#ifdef CONFIG_PM 515#ifdef CONFIG_PM
516static int tmio_suspend(struct platform_device *dev, pm_message_t state) 516static int tmio_suspend(struct platform_device *dev, pm_message_t state)
517{ 517{
518 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; 518 struct mfd_cell *cell = dev_get_platdata(&dev->dev);
519 519
520 if (cell->suspend) 520 if (cell->suspend)
521 cell->suspend(dev); 521 cell->suspend(dev);
@@ -526,7 +526,7 @@ static int tmio_suspend(struct platform_device *dev, pm_message_t state)
526 526
527static int tmio_resume(struct platform_device *dev) 527static int tmio_resume(struct platform_device *dev)
528{ 528{
529 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data; 529 struct mfd_cell *cell = dev_get_platdata(&dev->dev);
530 530
531 /* FIXME - is this required or merely another attack of the broken 531 /* FIXME - is this required or merely another attack of the broken
532 * SHARP platform? Looks suspicious. 532 * SHARP platform? Looks suspicious.
diff --git a/drivers/mtd/nand/ts7250.c b/drivers/mtd/nand/ts7250.c
deleted file mode 100644
index 0f5562aeedc1..000000000000
--- a/drivers/mtd/nand/ts7250.c
+++ /dev/null
@@ -1,207 +0,0 @@
1/*
2 * drivers/mtd/nand/ts7250.c
3 *
4 * Copyright (C) 2004 Technologic Systems (support@embeddedARM.com)
5 *
6 * Derived from drivers/mtd/nand/edb7312.c
7 * Copyright (C) 2004 Marius Gröger (mag@sysgo.de)
8 *
9 * Derived from drivers/mtd/nand/autcpu12.c
10 * Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
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 * TS-7250 board which utilizes a Samsung 32 Mbyte part.
19 */
20
21#include <linux/slab.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/nand.h>
26#include <linux/mtd/partitions.h>
27#include <linux/io.h>
28
29#include <mach/hardware.h>
30#include <mach/ts72xx.h>
31
32#include <asm/sizes.h>
33#include <asm/mach-types.h>
34
35/*
36 * MTD structure for TS7250 board
37 */
38static struct mtd_info *ts7250_mtd = NULL;
39
40#ifdef CONFIG_MTD_PARTITIONS
41static const char *part_probes[] = { "cmdlinepart", NULL };
42
43#define NUM_PARTITIONS 3
44
45/*
46 * Define static partitions for flash device
47 */
48static struct mtd_partition partition_info32[] = {
49 {
50 .name = "TS-BOOTROM",
51 .offset = 0x00000000,
52 .size = 0x00004000,
53 }, {
54 .name = "Linux",
55 .offset = 0x00004000,
56 .size = 0x01d00000,
57 }, {
58 .name = "RedBoot",
59 .offset = 0x01d04000,
60 .size = 0x002fc000,
61 },
62};
63
64/*
65 * Define static partitions for flash device
66 */
67static struct mtd_partition partition_info128[] = {
68 {
69 .name = "TS-BOOTROM",
70 .offset = 0x00000000,
71 .size = 0x00004000,
72 }, {
73 .name = "Linux",
74 .offset = 0x00004000,
75 .size = 0x07d00000,
76 }, {
77 .name = "RedBoot",
78 .offset = 0x07d04000,
79 .size = 0x002fc000,
80 },
81};
82#endif
83
84
85/*
86 * hardware specific access to control-lines
87 *
88 * ctrl:
89 * NAND_NCE: bit 0 -> bit 2
90 * NAND_CLE: bit 1 -> bit 1
91 * NAND_ALE: bit 2 -> bit 0
92 */
93static void ts7250_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
94{
95 struct nand_chip *chip = mtd->priv;
96
97 if (ctrl & NAND_CTRL_CHANGE) {
98 unsigned long addr = TS72XX_NAND_CONTROL_VIRT_BASE;
99 unsigned char bits;
100
101 bits = (ctrl & NAND_NCE) << 2;
102 bits |= ctrl & NAND_CLE;
103 bits |= (ctrl & NAND_ALE) >> 2;
104
105 __raw_writeb((__raw_readb(addr) & ~0x7) | bits, addr);
106 }
107
108 if (cmd != NAND_CMD_NONE)
109 writeb(cmd, chip->IO_ADDR_W);
110}
111
112/*
113 * read device ready pin
114 */
115static int ts7250_device_ready(struct mtd_info *mtd)
116{
117 return __raw_readb(TS72XX_NAND_BUSY_VIRT_BASE) & 0x20;
118}
119
120/*
121 * Main initialization routine
122 */
123static int __init ts7250_init(void)
124{
125 struct nand_chip *this;
126 const char *part_type = 0;
127 int mtd_parts_nb = 0;
128 struct mtd_partition *mtd_parts = 0;
129
130 if (!machine_is_ts72xx() || board_is_ts7200())
131 return -ENXIO;
132
133 /* Allocate memory for MTD device structure and private data */
134 ts7250_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
135 if (!ts7250_mtd) {
136 printk("Unable to allocate TS7250 NAND MTD device structure.\n");
137 return -ENOMEM;
138 }
139
140 /* Get pointer to private data */
141 this = (struct nand_chip *)(&ts7250_mtd[1]);
142
143 /* Initialize structures */
144 memset(ts7250_mtd, 0, sizeof(struct mtd_info));
145 memset(this, 0, sizeof(struct nand_chip));
146
147 /* Link the private data with the MTD structure */
148 ts7250_mtd->priv = this;
149 ts7250_mtd->owner = THIS_MODULE;
150
151 /* insert callbacks */
152 this->IO_ADDR_R = (void *)TS72XX_NAND_DATA_VIRT_BASE;
153 this->IO_ADDR_W = (void *)TS72XX_NAND_DATA_VIRT_BASE;
154 this->cmd_ctrl = ts7250_hwcontrol;
155 this->dev_ready = ts7250_device_ready;
156 this->chip_delay = 15;
157 this->ecc.mode = NAND_ECC_SOFT;
158
159 printk("Searching for NAND flash...\n");
160 /* Scan to find existence of the device */
161 if (nand_scan(ts7250_mtd, 1)) {
162 kfree(ts7250_mtd);
163 return -ENXIO;
164 }
165#ifdef CONFIG_MTD_PARTITIONS
166 ts7250_mtd->name = "ts7250-nand";
167 mtd_parts_nb = parse_mtd_partitions(ts7250_mtd, part_probes, &mtd_parts, 0);
168 if (mtd_parts_nb > 0)
169 part_type = "command line";
170 else
171 mtd_parts_nb = 0;
172#endif
173 if (mtd_parts_nb == 0) {
174 mtd_parts = partition_info32;
175 if (ts7250_mtd->size >= (128 * 0x100000))
176 mtd_parts = partition_info128;
177 mtd_parts_nb = NUM_PARTITIONS;
178 part_type = "static";
179 }
180
181 /* Register the partitions */
182 printk(KERN_NOTICE "Using %s partition definition\n", part_type);
183 add_mtd_partitions(ts7250_mtd, mtd_parts, mtd_parts_nb);
184
185 /* Return happy */
186 return 0;
187}
188
189module_init(ts7250_init);
190
191/*
192 * Clean up routine
193 */
194static void __exit ts7250_cleanup(void)
195{
196 /* Unregister the device */
197 del_mtd_device(ts7250_mtd);
198
199 /* Free the MTD device structure */
200 kfree(ts7250_mtd);
201}
202
203module_exit(ts7250_cleanup);
204
205MODULE_LICENSE("GPL");
206MODULE_AUTHOR("Jesse Off <joff@embeddedARM.com>");
207MODULE_DESCRIPTION("MTD map driver for Technologic Systems TS-7250 board");
diff --git a/drivers/mtd/nand/txx9ndfmc.c b/drivers/mtd/nand/txx9ndfmc.c
index 863513c3b69a..054a41c0ef4a 100644
--- a/drivers/mtd/nand/txx9ndfmc.c
+++ b/drivers/mtd/nand/txx9ndfmc.c
@@ -274,7 +274,7 @@ static int txx9ndfmc_nand_scan(struct mtd_info *mtd)
274 struct nand_chip *chip = mtd->priv; 274 struct nand_chip *chip = mtd->priv;
275 int ret; 275 int ret;
276 276
277 ret = nand_scan_ident(mtd, 1); 277 ret = nand_scan_ident(mtd, 1, NULL);
278 if (!ret) { 278 if (!ret) {
279 if (mtd->writesize >= 512) { 279 if (mtd->writesize >= 512) {
280 chip->ecc.size = mtd->writesize; 280 chip->ecc.size = mtd->writesize;
diff --git a/drivers/mtd/nftlcore.c b/drivers/mtd/nftlcore.c
index 1002e1882996..a4578bf903aa 100644
--- a/drivers/mtd/nftlcore.c
+++ b/drivers/mtd/nftlcore.c
@@ -126,7 +126,6 @@ static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
126 del_mtd_blktrans_dev(dev); 126 del_mtd_blktrans_dev(dev);
127 kfree(nftl->ReplUnitTable); 127 kfree(nftl->ReplUnitTable);
128 kfree(nftl->EUNtable); 128 kfree(nftl->EUNtable);
129 kfree(nftl);
130} 129}
131 130
132/* 131/*
diff --git a/drivers/mtd/onenand/omap2.c b/drivers/mtd/onenand/omap2.c
index 75f38b95811e..dfbab6c72b74 100644
--- a/drivers/mtd/onenand/omap2.c
+++ b/drivers/mtd/onenand/omap2.c
@@ -308,7 +308,7 @@ static int omap3_onenand_read_bufferram(struct mtd_info *mtd, int area,
308 goto out_copy; 308 goto out_copy;
309 309
310 /* panic_write() may be in an interrupt context */ 310 /* panic_write() may be in an interrupt context */
311 if (in_interrupt()) 311 if (in_interrupt() || oops_in_progress)
312 goto out_copy; 312 goto out_copy;
313 313
314 if (buf >= high_memory) { 314 if (buf >= high_memory) {
@@ -385,7 +385,7 @@ static int omap3_onenand_write_bufferram(struct mtd_info *mtd, int area,
385 goto out_copy; 385 goto out_copy;
386 386
387 /* panic_write() may be in an interrupt context */ 387 /* panic_write() may be in an interrupt context */
388 if (in_interrupt()) 388 if (in_interrupt() || oops_in_progress)
389 goto out_copy; 389 goto out_copy;
390 390
391 if (buf >= high_memory) { 391 if (buf >= high_memory) {
diff --git a/drivers/mtd/rfd_ftl.c b/drivers/mtd/rfd_ftl.c
index d2aa9c46530f..63b83c0d9a13 100644
--- a/drivers/mtd/rfd_ftl.c
+++ b/drivers/mtd/rfd_ftl.c
@@ -817,7 +817,6 @@ static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
817 vfree(part->sector_map); 817 vfree(part->sector_map);
818 kfree(part->header_cache); 818 kfree(part->header_cache);
819 kfree(part->blocks); 819 kfree(part->blocks);
820 kfree(part);
821} 820}
822 821
823static struct mtd_blktrans_ops rfd_ftl_tr = { 822static struct mtd_blktrans_ops rfd_ftl_tr = {
diff --git a/drivers/mtd/ssfdc.c b/drivers/mtd/ssfdc.c
index 3f67e00d98e0..81c4ecdc11f5 100644
--- a/drivers/mtd/ssfdc.c
+++ b/drivers/mtd/ssfdc.c
@@ -375,7 +375,6 @@ static void ssfdcr_remove_dev(struct mtd_blktrans_dev *dev)
375 375
376 del_mtd_blktrans_dev(dev); 376 del_mtd_blktrans_dev(dev);
377 kfree(ssfdc->logic_block_map); 377 kfree(ssfdc->logic_block_map);
378 kfree(ssfdc);
379} 378}
380 379
381static int ssfdcr_readsect(struct mtd_blktrans_dev *dev, 380static int ssfdcr_readsect(struct mtd_blktrans_dev *dev,
diff --git a/fs/jffs2/fs.c b/fs/jffs2/fs.c
index 3451a81b2142..86e0821fc989 100644
--- a/fs/jffs2/fs.c
+++ b/fs/jffs2/fs.c
@@ -313,8 +313,8 @@ struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
313 case S_IFBLK: 313 case S_IFBLK:
314 case S_IFCHR: 314 case S_IFCHR:
315 /* Read the device numbers from the media */ 315 /* Read the device numbers from the media */
316 if (f->metadata->size != sizeof(jdev.old) && 316 if (f->metadata->size != sizeof(jdev.old_id) &&
317 f->metadata->size != sizeof(jdev.new)) { 317 f->metadata->size != sizeof(jdev.new_id)) {
318 printk(KERN_NOTICE "Device node has strange size %d\n", f->metadata->size); 318 printk(KERN_NOTICE "Device node has strange size %d\n", f->metadata->size);
319 goto error_io; 319 goto error_io;
320 } 320 }
@@ -325,10 +325,10 @@ struct inode *jffs2_iget(struct super_block *sb, unsigned long ino)
325 printk(KERN_NOTICE "Read device numbers for inode %lu failed\n", (unsigned long)inode->i_ino); 325 printk(KERN_NOTICE "Read device numbers for inode %lu failed\n", (unsigned long)inode->i_ino);
326 goto error; 326 goto error;
327 } 327 }
328 if (f->metadata->size == sizeof(jdev.old)) 328 if (f->metadata->size == sizeof(jdev.old_id))
329 rdev = old_decode_dev(je16_to_cpu(jdev.old)); 329 rdev = old_decode_dev(je16_to_cpu(jdev.old_id));
330 else 330 else
331 rdev = new_decode_dev(je32_to_cpu(jdev.new)); 331 rdev = new_decode_dev(je32_to_cpu(jdev.new_id));
332 332
333 case S_IFSOCK: 333 case S_IFSOCK:
334 case S_IFIFO: 334 case S_IFIFO:
diff --git a/fs/jffs2/nodelist.h b/fs/jffs2/nodelist.h
index 507ed6ec1847..36d7a849ee2c 100644
--- a/fs/jffs2/nodelist.h
+++ b/fs/jffs2/nodelist.h
@@ -312,11 +312,11 @@ static inline int jffs2_blocks_use_vmalloc(struct jffs2_sb_info *c)
312static inline int jffs2_encode_dev(union jffs2_device_node *jdev, dev_t rdev) 312static inline int jffs2_encode_dev(union jffs2_device_node *jdev, dev_t rdev)
313{ 313{
314 if (old_valid_dev(rdev)) { 314 if (old_valid_dev(rdev)) {
315 jdev->old = cpu_to_je16(old_encode_dev(rdev)); 315 jdev->old_id = cpu_to_je16(old_encode_dev(rdev));
316 return sizeof(jdev->old); 316 return sizeof(jdev->old_id);
317 } else { 317 } else {
318 jdev->new = cpu_to_je32(new_encode_dev(rdev)); 318 jdev->new_id = cpu_to_je32(new_encode_dev(rdev));
319 return sizeof(jdev->new); 319 return sizeof(jdev->new_id);
320 } 320 }
321} 321}
322 322
diff --git a/include/linux/jffs2.h b/include/linux/jffs2.h
index 2b32d638147d..0874ab59ffef 100644
--- a/include/linux/jffs2.h
+++ b/include/linux/jffs2.h
@@ -215,8 +215,8 @@ union jffs2_node_union
215 215
216/* Data payload for device nodes. */ 216/* Data payload for device nodes. */
217union jffs2_device_node { 217union jffs2_device_node {
218 jint16_t old; 218 jint16_t old_id;
219 jint32_t new; 219 jint32_t new_id;
220}; 220};
221 221
222#endif /* __LINUX_JFFS2_H__ */ 222#endif /* __LINUX_JFFS2_H__ */
diff --git a/include/linux/mtd/blktrans.h b/include/linux/mtd/blktrans.h
index 8b4aa0523db7..b481ccd7ff3c 100644
--- a/include/linux/mtd/blktrans.h
+++ b/include/linux/mtd/blktrans.h
@@ -9,6 +9,8 @@
9#define __MTD_TRANS_H__ 9#define __MTD_TRANS_H__
10 10
11#include <linux/mutex.h> 11#include <linux/mutex.h>
12#include <linux/kref.h>
13#include <linux/sysfs.h>
12 14
13struct hd_geometry; 15struct hd_geometry;
14struct mtd_info; 16struct mtd_info;
@@ -24,11 +26,16 @@ struct mtd_blktrans_dev {
24 int devnum; 26 int devnum;
25 unsigned long size; 27 unsigned long size;
26 int readonly; 28 int readonly;
27 void *blkcore_priv; /* gendisk in 2.5, devfs_handle in 2.4 */ 29 int open;
30 struct kref ref;
31 struct gendisk *disk;
32 struct attribute_group *disk_attributes;
33 struct task_struct *thread;
34 struct request_queue *rq;
35 spinlock_t queue_lock;
36 void *priv;
28}; 37};
29 38
30struct blkcore_priv; /* Differs for 2.4 and 2.5 kernels; private */
31
32struct mtd_blktrans_ops { 39struct mtd_blktrans_ops {
33 char *name; 40 char *name;
34 int major; 41 int major;
@@ -60,8 +67,6 @@ struct mtd_blktrans_ops {
60 struct list_head devs; 67 struct list_head devs;
61 struct list_head list; 68 struct list_head list;
62 struct module *owner; 69 struct module *owner;
63
64 struct mtd_blkcore_priv *blkcore_priv;
65}; 70};
66 71
67extern int register_mtd_blktrans(struct mtd_blktrans_ops *tr); 72extern int register_mtd_blktrans(struct mtd_blktrans_ops *tr);
diff --git a/include/linux/mtd/cfi.h b/include/linux/mtd/cfi.h
index df89f4275232..cee05b1e62b1 100644
--- a/include/linux/mtd/cfi.h
+++ b/include/linux/mtd/cfi.h
@@ -297,7 +297,7 @@ static inline uint32_t cfi_build_cmd_addr(uint32_t cmd_ofs,
297 * and 32bit devices on 16 bit busses 297 * and 32bit devices on 16 bit busses
298 * set the low bit of the alternating bit sequence of the address. 298 * set the low bit of the alternating bit sequence of the address.
299 */ 299 */
300 if (((type * interleave) > bankwidth) && ((uint8_t)cmd_ofs == 0xaa)) 300 if (((type * interleave) > bankwidth) && ((cmd_ofs & 0xff) == 0xaa))
301 addr |= (type >> 1)*interleave; 301 addr |= (type >> 1)*interleave;
302 302
303 return addr; 303 return addr;
@@ -518,11 +518,13 @@ struct cfi_fixup {
518#define CFI_MFR_ANY 0xffff 518#define CFI_MFR_ANY 0xffff
519#define CFI_ID_ANY 0xffff 519#define CFI_ID_ANY 0xffff
520 520
521#define CFI_MFR_AMD 0x0001 521#define CFI_MFR_AMD 0x0001
522#define CFI_MFR_INTEL 0x0089 522#define CFI_MFR_ATMEL 0x001F
523#define CFI_MFR_ATMEL 0x001F 523#define CFI_MFR_INTEL 0x0089
524#define CFI_MFR_SAMSUNG 0x00EC 524#define CFI_MFR_MACRONIX 0x00C2
525#define CFI_MFR_ST 0x0020 /* STMicroelectronics */ 525#define CFI_MFR_SAMSUNG 0x00EC
526#define CFI_MFR_SST 0x00BF
527#define CFI_MFR_ST 0x0020 /* STMicroelectronics */
526 528
527void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup* fixups); 529void cfi_fixup(struct mtd_info *mtd, struct cfi_fixup* fixups);
528 530
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 0f32a9b6ff55..5326435a7571 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -20,7 +20,6 @@
20 20
21#define MTD_CHAR_MAJOR 90 21#define MTD_CHAR_MAJOR 90
22#define MTD_BLOCK_MAJOR 31 22#define MTD_BLOCK_MAJOR 31
23#define MAX_MTD_DEVICES 32
24 23
25#define MTD_ERASE_PENDING 0x01 24#define MTD_ERASE_PENDING 0x01
26#define MTD_ERASING 0x02 25#define MTD_ERASING 0x02
@@ -61,9 +60,7 @@ struct mtd_erase_region_info {
61 * MTD_OOB_PLACE: oob data are placed at the given offset 60 * MTD_OOB_PLACE: oob data are placed at the given offset
62 * MTD_OOB_AUTO: oob data are automatically placed at the free areas 61 * MTD_OOB_AUTO: oob data are automatically placed at the free areas
63 * which are defined by the ecclayout 62 * which are defined by the ecclayout
64 * MTD_OOB_RAW: mode to read raw data+oob in one chunk. The oob data 63 * MTD_OOB_RAW: mode to read oob and data without doing ECC checking
65 * is inserted into the data. Thats a raw image of the
66 * flash contents.
67 */ 64 */
68typedef enum { 65typedef enum {
69 MTD_OOB_PLACE, 66 MTD_OOB_PLACE,
@@ -290,8 +287,9 @@ extern int add_mtd_device(struct mtd_info *mtd);
290extern int del_mtd_device (struct mtd_info *mtd); 287extern int del_mtd_device (struct mtd_info *mtd);
291 288
292extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num); 289extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
290extern int __get_mtd_device(struct mtd_info *mtd);
291extern void __put_mtd_device(struct mtd_info *mtd);
293extern struct mtd_info *get_mtd_device_nm(const char *name); 292extern struct mtd_info *get_mtd_device_nm(const char *name);
294
295extern void put_mtd_device(struct mtd_info *mtd); 293extern void put_mtd_device(struct mtd_info *mtd);
296 294
297 295
diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
index ccab9dfc5217..8bdacb885f90 100644
--- a/include/linux/mtd/nand.h
+++ b/include/linux/mtd/nand.h
@@ -25,11 +25,13 @@
25#include <linux/mtd/bbm.h> 25#include <linux/mtd/bbm.h>
26 26
27struct mtd_info; 27struct mtd_info;
28struct nand_flash_dev;
28/* Scan and identify a NAND device */ 29/* Scan and identify a NAND device */
29extern int nand_scan (struct mtd_info *mtd, int max_chips); 30extern int nand_scan (struct mtd_info *mtd, int max_chips);
30/* Separate phases of nand_scan(), allowing board driver to intervene 31/* Separate phases of nand_scan(), allowing board driver to intervene
31 * and override command or ECC setup according to flash type */ 32 * and override command or ECC setup according to flash type */
32extern int nand_scan_ident(struct mtd_info *mtd, int max_chips); 33extern int nand_scan_ident(struct mtd_info *mtd, int max_chips,
34 struct nand_flash_dev *table);
33extern int nand_scan_tail(struct mtd_info *mtd); 35extern int nand_scan_tail(struct mtd_info *mtd);
34 36
35/* Free resources held by the NAND device */ 37/* Free resources held by the NAND device */
@@ -38,6 +40,12 @@ extern void nand_release (struct mtd_info *mtd);
38/* Internal helper for board drivers which need to override command function */ 40/* Internal helper for board drivers which need to override command function */
39extern void nand_wait_ready(struct mtd_info *mtd); 41extern void nand_wait_ready(struct mtd_info *mtd);
40 42
43/* locks all blockes present in the device */
44extern int nand_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
45
46/* unlocks specified locked blockes */
47extern int nand_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
48
41/* The maximum number of NAND chips in an array */ 49/* The maximum number of NAND chips in an array */
42#define NAND_MAX_CHIPS 8 50#define NAND_MAX_CHIPS 8
43 51
@@ -82,6 +90,10 @@ extern void nand_wait_ready(struct mtd_info *mtd);
82#define NAND_CMD_ERASE2 0xd0 90#define NAND_CMD_ERASE2 0xd0
83#define NAND_CMD_RESET 0xff 91#define NAND_CMD_RESET 0xff
84 92
93#define NAND_CMD_LOCK 0x2a
94#define NAND_CMD_UNLOCK1 0x23
95#define NAND_CMD_UNLOCK2 0x24
96
85/* Extended commands for large page devices */ 97/* Extended commands for large page devices */
86#define NAND_CMD_READSTART 0x30 98#define NAND_CMD_READSTART 0x30
87#define NAND_CMD_RNDOUTSTART 0xE0 99#define NAND_CMD_RNDOUTSTART 0xE0
@@ -170,6 +182,12 @@ typedef enum {
170/* Chip does not allow subpage writes */ 182/* Chip does not allow subpage writes */
171#define NAND_NO_SUBPAGE_WRITE 0x00000200 183#define NAND_NO_SUBPAGE_WRITE 0x00000200
172 184
185/* Device is one of 'new' xD cards that expose fake nand command set */
186#define NAND_BROKEN_XD 0x00000400
187
188/* Device behaves just like nand, but is readonly */
189#define NAND_ROM 0x00000800
190
173/* Options valid for Samsung large page devices */ 191/* Options valid for Samsung large page devices */
174#define NAND_SAMSUNG_LP_OPTIONS \ 192#define NAND_SAMSUNG_LP_OPTIONS \
175 (NAND_NO_PADDING | NAND_CACHEPRG | NAND_COPYBACK) 193 (NAND_NO_PADDING | NAND_CACHEPRG | NAND_COPYBACK)
@@ -391,6 +409,7 @@ struct nand_chip {
391 int subpagesize; 409 int subpagesize;
392 uint8_t cellinfo; 410 uint8_t cellinfo;
393 int badblockpos; 411 int badblockpos;
412 int badblockbits;
394 413
395 flstate_t state; 414 flstate_t state;
396 415
diff --git a/lib/idr.c b/lib/idr.c
index 0dc782216d4b..5ee8b1a57370 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -623,7 +623,7 @@ void *idr_get_next(struct idr *idp, int *nextidp)
623 } 623 }
624 return NULL; 624 return NULL;
625} 625}
626 626EXPORT_SYMBOL(idr_get_next);
627 627
628 628
629/** 629/**