blob: 7d49f6a6b726ccaaf8bcd01e2f1e56ab6b1a1033 [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>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +01008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Overview:
14 * This is a device driver for the NAND flash device found on the
15 * Amstrad E3 (Delta).
16 */
17
18#include <linux/slab.h>
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/delay.h>
22#include <linux/mtd/mtd.h>
23#include <linux/mtd/nand.h>
24#include <linux/mtd/partitions.h>
25#include <asm/io.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/hardware.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010027#include <asm/sizes.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/gpio.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/board-ams-delta.h>
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010030
31/*
32 * MTD structure for E3 (Delta)
33 */
34static struct mtd_info *ams_delta_mtd = NULL;
35
36#define NAND_MASK (AMS_DELTA_LATCH2_NAND_NRE | AMS_DELTA_LATCH2_NAND_NWE | AMS_DELTA_LATCH2_NAND_CLE | AMS_DELTA_LATCH2_NAND_ALE | AMS_DELTA_LATCH2_NAND_NCE | AMS_DELTA_LATCH2_NAND_NWP)
37
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010038/*
39 * Define partitions for flash devices
40 */
41
42static struct mtd_partition partition_info[] = {
43 { .name = "Kernel",
44 .offset = 0,
45 .size = 3 * SZ_1M + SZ_512K },
46 { .name = "u-boot",
47 .offset = 3 * SZ_1M + SZ_512K,
48 .size = SZ_256K },
49 { .name = "u-boot params",
50 .offset = 3 * SZ_1M + SZ_512K + SZ_256K,
51 .size = SZ_256K },
52 { .name = "Amstrad LDR",
53 .offset = 4 * SZ_1M,
54 .size = SZ_256K },
55 { .name = "File system",
56 .offset = 4 * SZ_1M + 1 * SZ_256K,
57 .size = 27 * SZ_1M },
58 { .name = "PBL reserved",
59 .offset = 32 * SZ_1M - 3 * SZ_256K,
60 .size = 3 * SZ_256K },
61};
62
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010063static void ams_delta_write_byte(struct mtd_info *mtd, u_char byte)
64{
65 struct nand_chip *this = mtd->priv;
66
Tony Lindgren61755562009-08-28 10:50:34 -070067 omap_writew(0, (OMAP1_MPUIO_BASE + OMAP_MPUIO_IO_CNTL));
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010068 omap_writew(byte, this->IO_ADDR_W);
69 ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NWE, 0);
70 ndelay(40);
71 ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NWE,
72 AMS_DELTA_LATCH2_NAND_NWE);
73}
74
75static u_char ams_delta_read_byte(struct mtd_info *mtd)
76{
77 u_char res;
78 struct nand_chip *this = mtd->priv;
79
80 ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NRE, 0);
81 ndelay(40);
Tony Lindgren61755562009-08-28 10:50:34 -070082 omap_writew(~0, (OMAP1_MPUIO_BASE + OMAP_MPUIO_IO_CNTL));
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +010083 res = omap_readw(this->IO_ADDR_R);
84 ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_NRE,
85 AMS_DELTA_LATCH2_NAND_NRE);
86
87 return res;
88}
89
90static void ams_delta_write_buf(struct mtd_info *mtd, const u_char *buf,
91 int len)
92{
93 int i;
94
95 for (i=0; i<len; i++)
96 ams_delta_write_byte(mtd, buf[i]);
97}
98
99static void ams_delta_read_buf(struct mtd_info *mtd, u_char *buf, int len)
100{
101 int i;
102
103 for (i=0; i<len; i++)
104 buf[i] = ams_delta_read_byte(mtd);
105}
106
107static int ams_delta_verify_buf(struct mtd_info *mtd, const u_char *buf,
108 int len)
109{
110 int i;
111
112 for (i=0; i<len; i++)
113 if (buf[i] != ams_delta_read_byte(mtd))
114 return -EFAULT;
115
116 return 0;
117}
118
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200119/*
120 * Command control function
121 *
122 * ctrl:
123 * NAND_NCE: bit 0 -> bit 2
124 * NAND_CLE: bit 1 -> bit 7
125 * NAND_ALE: bit 2 -> bit 6
126 */
127static void ams_delta_hwcontrol(struct mtd_info *mtd, int cmd,
128 unsigned int ctrl)
129{
130
131 if (ctrl & NAND_CTRL_CHANGE) {
132 unsigned long bits;
133
Jonathan McDowellfb8d81e2006-08-27 01:23:41 -0700134 bits = (~ctrl & NAND_NCE) ? AMS_DELTA_LATCH2_NAND_NCE : 0;
135 bits |= (ctrl & NAND_CLE) ? AMS_DELTA_LATCH2_NAND_CLE : 0;
136 bits |= (ctrl & NAND_ALE) ? AMS_DELTA_LATCH2_NAND_ALE : 0;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200137
Jonathan McDowellfb8d81e2006-08-27 01:23:41 -0700138 ams_delta_latch2_write(AMS_DELTA_LATCH2_NAND_CLE |
139 AMS_DELTA_LATCH2_NAND_ALE |
140 AMS_DELTA_LATCH2_NAND_NCE, bits);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200141 }
142
143 if (cmd != NAND_CMD_NONE)
144 ams_delta_write_byte(mtd, cmd);
145}
146
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100147static int ams_delta_nand_ready(struct mtd_info *mtd)
148{
David Brownell93a22f82008-10-15 22:03:15 -0700149 return gpio_get_value(AMS_DELTA_GPIO_PIN_NAND_RB);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100150}
151
152/*
153 * Main initialization routine
154 */
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100155static int __devinit ams_delta_init(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100156{
157 struct nand_chip *this;
158 int err = 0;
159
160 /* Allocate memory for MTD device structure and private data */
161 ams_delta_mtd = kmalloc(sizeof(struct mtd_info) +
162 sizeof(struct nand_chip), GFP_KERNEL);
163 if (!ams_delta_mtd) {
164 printk (KERN_WARNING "Unable to allocate E3 NAND MTD device structure.\n");
165 err = -ENOMEM;
166 goto out;
167 }
168
169 ams_delta_mtd->owner = THIS_MODULE;
170
171 /* Get pointer to private data */
172 this = (struct nand_chip *) (&ams_delta_mtd[1]);
173
174 /* Initialize structures */
175 memset(ams_delta_mtd, 0, sizeof(struct mtd_info));
176 memset(this, 0, sizeof(struct nand_chip));
177
178 /* Link the private data with the MTD structure */
179 ams_delta_mtd->priv = this;
180
181 /* Set address of NAND IO lines */
Tony Lindgren61755562009-08-28 10:50:34 -0700182 this->IO_ADDR_R = (OMAP1_MPUIO_BASE + OMAP_MPUIO_INPUT_LATCH);
183 this->IO_ADDR_W = (OMAP1_MPUIO_BASE + OMAP_MPUIO_OUTPUT);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100184 this->read_byte = ams_delta_read_byte;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100185 this->write_buf = ams_delta_write_buf;
186 this->read_buf = ams_delta_read_buf;
187 this->verify_buf = ams_delta_verify_buf;
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200188 this->cmd_ctrl = ams_delta_hwcontrol;
David Brownell93a22f82008-10-15 22:03:15 -0700189 if (gpio_request(AMS_DELTA_GPIO_PIN_NAND_RB, "nand_rdy") == 0) {
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100190 this->dev_ready = ams_delta_nand_ready;
191 } else {
192 this->dev_ready = NULL;
193 printk(KERN_NOTICE "Couldn't request gpio for Delta NAND ready.\n");
194 }
195 /* 25 us command delay time */
196 this->chip_delay = 30;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +0200197 this->ecc.mode = NAND_ECC_SOFT;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100198
199 /* Set chip enabled, but */
200 ams_delta_latch2_write(NAND_MASK, AMS_DELTA_LATCH2_NAND_NRE |
201 AMS_DELTA_LATCH2_NAND_NWE |
202 AMS_DELTA_LATCH2_NAND_NCE |
203 AMS_DELTA_LATCH2_NAND_NWP);
204
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +0200205 /* Scan to find existance of the device */
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100206 if (nand_scan(ams_delta_mtd, 1)) {
207 err = -ENXIO;
208 goto out_mtd;
209 }
210
211 /* Register the partitions */
212 add_mtd_partitions(ams_delta_mtd, partition_info,
213 ARRAY_SIZE(partition_info));
214
215 goto out;
216
217 out_mtd:
218 kfree(ams_delta_mtd);
219 out:
220 return err;
221}
222
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100223/*
224 * Clean up routine
225 */
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100226static int __devexit ams_delta_cleanup(struct platform_device *pdev)
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100227{
228 /* Release resources, unregister device */
229 nand_release(ams_delta_mtd);
230
231 /* Free the MTD device structure */
232 kfree(ams_delta_mtd);
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100233
234 return 0;
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100235}
Janusz Krzysztofik7e95d1f2010-12-14 21:09:40 +0100236
237static struct platform_driver ams_delta_nand_driver = {
238 .probe = ams_delta_init,
239 .remove = __devexit_p(ams_delta_cleanup),
240 .driver = {
241 .name = "ams-delta-nand",
242 .owner = THIS_MODULE,
243 },
244};
245
246static int __init ams_delta_nand_init(void)
247{
248 return platform_driver_register(&ams_delta_nand_driver);
249}
250module_init(ams_delta_nand_init);
251
252static void __exit ams_delta_nand_exit(void)
253{
254 platform_driver_unregister(&ams_delta_nand_driver);
255}
256module_exit(ams_delta_nand_exit);
Jonathan McDowell3d12c0c2006-05-21 18:11:55 +0100257
258MODULE_LICENSE("GPL");
259MODULE_AUTHOR("Jonathan McDowell <noodles@earth.li>");
260MODULE_DESCRIPTION("Glue layer for NAND flash on Amstrad E3 (Delta)");