blob: 73416951f4c1f6bdd52b53c8f2ebe1967fae4d47 [file] [log] [blame]
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01001/*
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
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +01007 * Converted to platform driver by Janusz Krzysztofik <jkrzyszt@tis.icnet.pl>
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +01008 * Partially stolen from drivers/mtd/nand/plat_nand.c
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01009 *
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>
Russell Kinga09e64f2008-08-05 16:14:15 +010027#include <mach/hardware.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010028#include <asm/sizes.h>
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010029#include <linux/gpio.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070030#include <plat/board-ams-delta.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010031
32/*
33 * MTD structure for E3 (Delta)
34 */
35static struct mtd_info *ams_delta_mtd = NULL;
36
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010037/*
38 * Define partitions for flash devices
39 */
40
41static 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
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010062static void ams_delta_write_byte(struct mtd_info *mtd, u_char byte)
63{
64 struct nand_chip *this = mtd->priv;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +010065 void __iomem *io_base = this->priv;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010066
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +010067 writew(0, io_base + OMAP_MPUIO_IO_CNTL);
68 writew(byte, this->IO_ADDR_W);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010069 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010070 ndelay(40);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010071 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NWE, 1);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010072}
73
74static u_char ams_delta_read_byte(struct mtd_info *mtd)
75{
76 u_char res;
77 struct nand_chip *this = mtd->priv;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +010078 void __iomem *io_base = this->priv;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010079
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010080 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 0);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010081 ndelay(40);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +010082 writew(~0, io_base + OMAP_MPUIO_IO_CNTL);
83 res = readw(this->IO_ADDR_R);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +010084 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NRE, 1);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010085
86 return res;
87}
88
89static 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
98static 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
106static int ams_delta_verify_buf(struct mtd_info *mtd, const u_char *buf,
107 int len)
108{
109 int i;
110
111 for (i=0; i<len; i++)
112 if (buf[i] != ams_delta_read_byte(mtd))
113 return -EFAULT;
114
115 return 0;
116}
117
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200118/*
119 * Command control function
120 *
121 * ctrl:
122 * NAND_NCE: bit 0 -> bit 2
123 * NAND_CLE: bit 1 -> bit 7
124 * NAND_ALE: bit 2 -> bit 6
125 */
126static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd,
127 unsigned int ctrl)
128{
129
130 if (ctrl & NAND_CTRL_CHANGE) {
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100131 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_NCE,
132 (ctrl & NAND_NCE) == 0);
133 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_CLE,
134 (ctrl & NAND_CLE) != 0);
135 gpio_set_value(AMS_DELTA_GPIO_PIN_NAND_ALE,
136 (ctrl & NAND_ALE) != 0);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200137 }
138
139 if (cmd != NAND_CMD_NONE)
140 ams_delta_write_byte(mtd, cmd);
141}
142
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100143static int ams_delta_nand_ready(struct mtd_info *mtd)
144{
David Brownell93a22f82008-10-15 22:03:15 -0700145 return gpio_get_value(AMS_DELTA_GPIO_PIN_NAND_RB);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100146}
147
Janusz Krzysztofikda564a02012-02-10 17:48:43 +0100148static const struct gpio _mandatory_gpio[] = {
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100149 {
150 .gpio = AMS_DELTA_GPIO_PIN_NAND_NCE,
151 .flags = GPIOF_OUT_INIT_HIGH,
152 .label = "nand_nce",
153 },
154 {
155 .gpio = AMS_DELTA_GPIO_PIN_NAND_NRE,
156 .flags = GPIOF_OUT_INIT_HIGH,
157 .label = "nand_nre",
158 },
159 {
160 .gpio = AMS_DELTA_GPIO_PIN_NAND_NWP,
161 .flags = GPIOF_OUT_INIT_HIGH,
162 .label = "nand_nwp",
163 },
164 {
165 .gpio = AMS_DELTA_GPIO_PIN_NAND_NWE,
166 .flags = GPIOF_OUT_INIT_HIGH,
167 .label = "nand_nwe",
168 },
169 {
170 .gpio = AMS_DELTA_GPIO_PIN_NAND_ALE,
171 .flags = GPIOF_OUT_INIT_LOW,
172 .label = "nand_ale",
173 },
174 {
175 .gpio = AMS_DELTA_GPIO_PIN_NAND_CLE,
176 .flags = GPIOF_OUT_INIT_LOW,
177 .label = "nand_cle",
178 },
179};
180
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100181/*
182 * Main initialization routine
183 */
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100184static int __devinit ams_delta_init(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100185{
186 struct nand_chip *this;
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100187 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
188 void __iomem *io_base;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100189 int err = 0;
190
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100191 if (!res)
192 return -ENXIO;
193
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100194 /* Allocate memory for MTD device structure and private data */
195 ams_delta_mtd = kmalloc(sizeof(struct mtd_info) +
196 sizeof(struct nand_chip), GFP_KERNEL);
197 if (!ams_delta_mtd) {
198 printk (KERN_WARNING "Unable to allocate E3 NAND MTD device structure.\n");
199 err = -ENOMEM;
200 goto out;
201 }
202
203 ams_delta_mtd->owner = THIS_MODULE;
204
205 /* Get pointer to private data */
206 this = (struct nand_chip *) (&ams_delta_mtd[1]);
207
208 /* Initialize structures */
209 memset(ams_delta_mtd, 0, sizeof(struct mtd_info));
210 memset(this, 0, sizeof(struct nand_chip));
211
212 /* Link the private data with the MTD structure */
213 ams_delta_mtd->priv = this;
214
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100215 if (!request_mem_region(res->start, resource_size(res),
216 dev_name(&pdev->dev))) {
217 dev_err(&pdev->dev, "request_mem_region failed\n");
218 err = -EBUSY;
219 goto out_free;
220 }
221
222 io_base = ioremap(res->start, resource_size(res));
223 if (io_base == NULL) {
224 dev_err(&pdev->dev, "ioremap failed\n");
225 err = -EIO;
226 goto out_release_io;
227 }
228
229 this->priv = io_base;
230
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100231 /* Set address of NAND IO lines */
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100232 this->IO_ADDR_R = io_base + OMAP_MPUIO_INPUT_LATCH;
233 this->IO_ADDR_W = io_base + OMAP_MPUIO_OUTPUT;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100234 this->read_byte = ams_delta_read_byte;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100235 this->write_buf = ams_delta_write_buf;
236 this->read_buf = ams_delta_read_buf;
237 this->verify_buf = ams_delta_verify_buf;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200238 this->cmd_ctrl = ams_delta_hwcontrol;
David Brownell93a22f82008-10-15 22:03:15 -0700239 if (gpio_request(AMS_DELTA_GPIO_PIN_NAND_RB, "nand_rdy") == 0) {
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100240 this->dev_ready = ams_delta_nand_ready;
241 } else {
242 this->dev_ready = NULL;
243 printk(KERN_NOTICE "Couldn't request gpio for Delta NAND ready.\n");
244 }
245 /* 25 us command delay time */
246 this->chip_delay = 30;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200247 this->ecc.mode = NAND_ECC_SOFT;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100248
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100249 platform_set_drvdata(pdev, io_base);
250
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100251 /* Set chip enabled, but */
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100252 err = gpio_request_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
253 if (err)
254 goto out_gpio;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100255
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300256 /* Scan to find existence of the device */
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100257 if (nand_scan(ams_delta_mtd, 1)) {
258 err = -ENXIO;
259 goto out_mtd;
260 }
261
262 /* Register the partitions */
Jamie Ilesee0e87b2011-05-23 10:23:40 +0100263 mtd_device_register(ams_delta_mtd, partition_info,
264 ARRAY_SIZE(partition_info));
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100265
266 goto out;
267
268 out_mtd:
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100269 gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
270out_gpio:
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100271 platform_set_drvdata(pdev, NULL);
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100272 gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100273 iounmap(io_base);
274out_release_io:
275 release_mem_region(res->start, resource_size(res));
276out_free:
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100277 kfree(ams_delta_mtd);
278 out:
279 return err;
280}
281
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100282/*
283 * Clean up routine
284 */
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100285static int __devexit ams_delta_cleanup(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100286{
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100287 void __iomem *io_base = platform_get_drvdata(pdev);
288 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
289
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100290 /* Release resources, unregister device */
291 nand_release(ams_delta_mtd);
292
Janusz Krzysztofik68f06762011-12-20 00:08:55 +0100293 gpio_free_array(_mandatory_gpio, ARRAY_SIZE(_mandatory_gpio));
294 gpio_free(AMS_DELTA_GPIO_PIN_NAND_RB);
Janusz Krzysztofikeaca4912010-12-15 15:43:44 +0100295 iounmap(io_base);
296 release_mem_region(res->start, resource_size(res));
297
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100298 /* Free the MTD device structure */
299 kfree(ams_delta_mtd);
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100300
301 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100302}
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100303
304static struct platform_driver ams_delta_nand_driver = {
305 .probe = ams_delta_init,
306 .remove = __devexit_p(ams_delta_cleanup),
307 .driver = {
308 .name = "ams-delta-nand",
309 .owner = THIS_MODULE,
310 },
311};
312
Axel Linf99640d2011-11-27 20:45:03 +0800313module_platform_driver(ams_delta_nand_driver);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100314
315MODULE_LICENSE("GPL");
316MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
317MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");