mtd: kill MTD_NAND_VERIFY_WRITE
[profile/ivi/kernel-adaptation-intel-automotive.git] / drivers / mtd / nand / ams-delta.c
1 /*
2  *  drivers/mtd/nand/ams-delta.c
3  *
4  *  Copyright (C) 2006 Jonathan McDowell <noodles@earth.li>
5  *
6  *  Derived from drivers/mtd/toto.c
7  *  Converted to platform driver by Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
8  *  Partially stolen from drivers/mtd/nand/plat_nand.c
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  *  Overview:
15  *   This is a device driver for the NAND flash device found on the
16  *   Amstrad E3 (Delta).
17  */
18
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/delay.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/nand.h>
25 #include <linux/mtd/partitions.h>
26 #include <asm/io.h>
27 #include <mach/hardware.h>
28 #include <asm/sizes.h>
29 #include <linux/gpio.h>
30 #include <plat/board-ams-delta.h>
31
32 /*
33  * MTD structure for E3 (Delta)
34  */
35 static struct mtd_info *ams_delta_mtd = NULL;
36
37 /*
38  * Define partitions for flash devices
39  */
40
41 static struct mtd_partition partition_info[] = {
42         { .name         = "Kernel",
43           .offset       = 0,
44           .size         = 3 * SZ_1M + SZ_512K },
45         { .name         = "u-boot",
46           .offset       = 3 * SZ_1M + SZ_512K,
47           .size         = SZ_256K },
48         { .name         = "u-boot params",
49           .offset       = 3 * SZ_1M + SZ_512K + SZ_256K,
50           .size         = SZ_256K },
51         { .name         = "Amstrad LDR",
52           .offset       = 4 * SZ_1M,
53           .size         = SZ_256K },
54         { .name         = "File system",
55           .offset       = 4 * SZ_1M + 1 * SZ_256K,
56           .size         = 27 * SZ_1M },
57         { .name         = "PBL reserved",
58           .offset       = 32 * SZ_1M - 3 * SZ_256K,
59           .size         =  3 * SZ_256K },
60 };
61
62 static void ams_delta_write_byte(struct mtd_info *mtd, u_char byte)
63 {
64         struct nand_chip *this = mtd->priv;
65         void __iomem *io_base = this->priv;
66
67         writew(0, io_base + OMAP_MPUIO_IO_CNTL);
68         writew(byte, this->IO_ADDR_W);
69         gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 0);
70         ndelay(40);
71         gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 1);
72 }
73
74 static u_char ams_delta_read_byte(struct mtd_info *mtd)
75 {
76         u_char res;
77         struct nand_chip *this = mtd->priv;
78         void __iomem *io_base = this->priv;
79
80         gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 0);
81         ndelay(40);
82         writew(~0, io_base + OMAP_MPUIO_IO_CNTL);
83         res = readw(this->IO_ADDR_R);
84         gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 1);
85
86         return res;
87 }
88
89 static void ams_delta_write_buf(struct mtd_info *mtd, const u_char *buf,
90                                 int len)
91 {
92         int i;
93
94         for (i=0; i<len; i++)
95                 ams_delta_write_byte(mtd, buf[i]);
96 }
97
98 static void ams_delta_read_buf(struct mtd_info *mtd, u_char *buf, int len)
99 {
100         int i;
101
102         for (i=0; i<len; i++)
103                 buf[i] = ams_delta_read_byte(mtd);
104 }
105
106 /*
107  * Command control function
108  *
109  * ctrl:
110  * NAND_NCE: bit 0 -> bit 2
111  * NAND_CLE: bit 1 -> bit 7
112  * NAND_ALE: bit 2 -> bit 6
113  */
114 static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd,
115                                 unsigned int ctrl)
116 {
117
118         if (ctrl & NAND_CTRL_CHANGE) {
119                 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NCE,
120                                 (ctrl & NAND_NCE) == 0);
121                 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_CLE,
122                                 (ctrl & NAND_CLE) != 0);
123                 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_ALE,
124                                 (ctrl & NAND_ALE) != 0);
125         }
126
127         if (cmd != NAND_CMD_NONE)
128                 ams_delta_write_byte(mtd, cmd);
129 }
130
131 static int ams_delta_nand_ready(struct mtd_info *mtd)
132 {
133         return gpio_get_value(AMS_DELTA_GPIO_PIN_NAND_RB);
134 }
135
136 static const struct gpio _mandatory_gpio[] = {
137         {
138                 .gpio   = AMS_DELTA_GPIO_PIN_NAND_NCE,
139                 .flags  = GPIOF_OUT_INIT_HIGH,
140                 .label  = "nand_nce",
141         },
142         {
143                 .gpio   = AMS_DELTA_GPIO_PIN_NAND_NRE,
144                 .flags  = GPIOF_OUT_INIT_HIGH,
145                 .label  = "nand_nre",
146         },
147         {
148                 .gpio   = AMS_DELTA_GPIO_PIN_NAND_NWP,
149                 .flags  = GPIOF_OUT_INIT_HIGH,
150                 .label  = "nand_nwp",
151         },
152         {
153                 .gpio   = AMS_DELTA_GPIO_PIN_NAND_NWE,
154                 .flags  = GPIOF_OUT_INIT_HIGH,
155                 .label  = "nand_nwe",
156         },
157         {
158                 .gpio   = AMS_DELTA_GPIO_PIN_NAND_ALE,
159                 .flags  = GPIOF_OUT_INIT_LOW,
160                 .label  = "nand_ale",
161         },
162         {
163                 .gpio   = AMS_DELTA_GPIO_PIN_NAND_CLE,
164                 .flags  = GPIOF_OUT_INIT_LOW,
165                 .label  = "nand_cle",
166         },
167 };
168
169 /*
170  * Main initialization routine
171  */
172 static int __devinit ams_delta_init(struct platform_device *pdev)
173 {
174         struct nand_chip *this;
175         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
176         void __iomem *io_base;
177         int err = 0;
178
179         if (!res)
180                 return -ENXIO;
181
182         /* Allocate memory for MTD device structure and private data */
183         ams_delta_mtd = kmalloc(sizeof(struct mtd_info) +
184                                 sizeof(struct nand_chip), GFP_KERNEL);
185         if (!ams_delta_mtd) {
186                 printk (KERN_WARNING "Unable to allocate E3 NAND MTD device structure.\n");
187                 err = -ENOMEM;
188                 goto out;
189         }
190
191         ams_delta_mtd->owner = THIS_MODULE;
192
193         /* Get pointer to private data */
194         this = (struct nand_chip *) (&ams_delta_mtd[1]);
195
196         /* Initialize structures */
197         memset(ams_delta_mtd, 0, sizeof(struct mtd_info));
198         memset(this, 0, sizeof(struct nand_chip));
199
200         /* Link the private data with the MTD structure */
201         ams_delta_mtd->priv = this;
202
203         /*
204          * Don't try to request the memory region from here,
205          * it should have been already requested from the
206          * gpio-omap driver and requesting it again would fail.
207          */
208
209         io_base = ioremap(res->start, resource_size(res));
210         if (io_base == NULL) {
211                 dev_err(&pdev->dev, "ioremap failed\n");
212                 err = -EIO;
213                 goto out_free;
214         }
215
216         this->priv = io_base;
217
218         /* Set address of NAND IO lines */
219         this->IO_ADDR_R = io_base + OMAP_MPUIO_INPUT_LATCH;
220         this->IO_ADDR_W = io_base + OMAP_MPUIO_OUTPUT;
221         this->read_byte = ams_delta_read_byte;
222         this->write_buf = ams_delta_write_buf;
223         this->read_buf = ams_delta_read_buf;
224         this->cmd_ctrl = ams_delta_hwcontrol;
225         if (gpio_request(AMS_DELTA_GPIO_PIN_NAND_RB, "nand_rdy") == 0) {
226                 this->dev_ready = ams_delta_nand_ready;
227         } else {
228                 this->dev_ready = NULL;
229                 printk(KERN_NOTICE "Couldn't request gpio for Delta NAND ready.\n");
230         }
231         /* 25 us command delay time */
232         this->chip_delay = 30;
233         this->ecc.mode = NAND_ECC_SOFT;
234
235         platform_set_drvdata(pdev, io_base);
236
237         /* Set chip enabled, but  */
238         err = gpio_request_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
239         if (err)
240                 goto out_gpio;
241
242         /* Scan to find existence of the device */
243         if (nand_scan(ams_delta_mtd, 1)) {
244                 err = -ENXIO;
245                 goto out_mtd;
246         }
247
248         /* Register the partitions */
249         mtd_device_register(ams_delta_mtd, partition_info,
250                             ARRAY_SIZE(partition_info));
251
252         goto out;
253
254  out_mtd:
255         gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
256 out_gpio:
257         platform_set_drvdata(pdev, NULL);
258         gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
259         iounmap(io_base);
260 out_free:
261         kfree(ams_delta_mtd);
262  out:
263         return err;
264 }
265
266 /*
267  * Clean up routine
268  */
269 static int __devexit ams_delta_cleanup(struct platform_device *pdev)
270 {
271         void __iomem *io_base = platform_get_drvdata(pdev);
272
273         /* Release resources, unregister device */
274         nand_release(ams_delta_mtd);
275
276         gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
277         gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
278         iounmap(io_base);
279
280         /* Free the MTD device structure */
281         kfree(ams_delta_mtd);
282
283         return 0;
284 }
285
286 static struct platform_driver ams_delta_nand_driver = {
287         .probe          = ams_delta_init,
288         .remove         = __devexit_p(ams_delta_cleanup),
289         .driver         = {
290                 .name   = "ams-delta-nand",
291                 .owner  = THIS_MODULE,
292         },
293 };
294
295 module_platform_driver(ams_delta_nand_driver);
296
297 MODULE_LICENSE("GPL");
298 MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
299 MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");