185bef04dfee963aed5cfcd48e05a37b7869ec79
[openwrt/svn-archive/archive.git] / target / linux / ar71xx / files / drivers / mtd / nand / rb4xx_nand.c
1 /*
2 * NAND flash driver for the MikroTik RouterBoard 4xx series
3 *
4 * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
6 *
7 * This file was based on the driver for Linux 2.6.22 published by
8 * MikroTik for their RouterBoard 4xx series devices.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 */
14
15 #include <linux/init.h>
16 #include <linux/mtd/nand.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/platform_device.h>
20 #include <linux/delay.h>
21 #include <linux/io.h>
22 #include <linux/gpio.h>
23 #include <linux/slab.h>
24
25 #include <asm/mach-ar71xx/ar71xx.h>
26 #include <asm/mach-ar71xx/rb4xx_cpld.h>
27
28 #define DRV_NAME "rb4xx-nand"
29 #define DRV_VERSION "0.2.0"
30 #define DRV_DESC "NAND flash driver for RouterBoard 4xx series"
31
32 #define RB4XX_NAND_GPIO_READY 5
33 #define RB4XX_NAND_GPIO_ALE 37
34 #define RB4XX_NAND_GPIO_CLE 38
35 #define RB4XX_NAND_GPIO_NCE 39
36
37 struct rb4xx_nand_info {
38 struct nand_chip chip;
39 struct mtd_info mtd;
40 };
41
42 /*
43 * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
44 * will not be able to find the kernel that we load.
45 */
46 static struct nand_ecclayout rb4xx_nand_ecclayout = {
47 .eccbytes = 6,
48 .eccpos = { 8, 9, 10, 13, 14, 15 },
49 .oobavail = 9,
50 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
51 };
52
53 static struct mtd_partition rb4xx_nand_partitions[] = {
54 {
55 .name = "booter",
56 .offset = 0,
57 .size = (256 * 1024),
58 .mask_flags = MTD_WRITEABLE,
59 },
60 {
61 .name = "kernel",
62 .offset = (256 * 1024),
63 .size = (6 * 1024 * 1024) - (256 * 1024),
64 },
65 {
66 .name = "rootfs",
67 .offset = MTDPART_OFS_NXTBLK,
68 .size = MTDPART_SIZ_FULL,
69 },
70 };
71
72 static int rb4xx_nand_dev_ready(struct mtd_info *mtd)
73 {
74 return gpio_get_value(RB4XX_NAND_GPIO_READY);
75 }
76
77 static void rb4xx_nand_write_cmd(unsigned char cmd)
78 {
79 unsigned char data = cmd;
80 int err;
81
82 err = rb4xx_cpld_write(&data, 1);
83 if (err)
84 pr_err("rb4xx_nand: write cmd failed, err=%d\n", err);
85 }
86
87 static void rb4xx_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
88 unsigned int ctrl)
89 {
90 if (ctrl & NAND_CTRL_CHANGE) {
91 gpio_set_value(RB4XX_NAND_GPIO_CLE, (ctrl & NAND_CLE) ? 1 : 0);
92 gpio_set_value(RB4XX_NAND_GPIO_ALE, (ctrl & NAND_ALE) ? 1 : 0);
93 gpio_set_value(RB4XX_NAND_GPIO_NCE, (ctrl & NAND_NCE) ? 0 : 1);
94 }
95
96 if (cmd != NAND_CMD_NONE)
97 rb4xx_nand_write_cmd(cmd);
98 }
99
100 static unsigned char rb4xx_nand_read_byte(struct mtd_info *mtd)
101 {
102 unsigned char data = 0;
103 int err;
104
105 err = rb4xx_cpld_read(&data, NULL, 1);
106 if (err) {
107 pr_err("rb4xx_nand: read data failed, err=%d\n", err);
108 data = 0xff;
109 }
110
111 return data;
112 }
113
114 static void rb4xx_nand_write_buf(struct mtd_info *mtd, const unsigned char *buf,
115 int len)
116 {
117 int err;
118
119 err = rb4xx_cpld_write(buf, len);
120 if (err)
121 pr_err("rb4xx_nand: write buf failed, err=%d\n", err);
122 }
123
124 static void rb4xx_nand_read_buf(struct mtd_info *mtd, unsigned char *buf,
125 int len)
126 {
127 int err;
128
129 err = rb4xx_cpld_read(buf, NULL, len);
130 if (err)
131 pr_err("rb4xx_nand: read buf failed, err=%d\n", err);
132 }
133
134 static int __init rb4xx_nand_probe(struct platform_device *pdev)
135 {
136 struct rb4xx_nand_info *info;
137 int ret;
138
139 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
140
141 ret = gpio_request(RB4XX_NAND_GPIO_READY, "NAND RDY");
142 if (ret) {
143 dev_err(&pdev->dev, "unable to request gpio %d\n",
144 RB4XX_NAND_GPIO_READY);
145 goto err;
146 }
147
148 ret = gpio_direction_input(RB4XX_NAND_GPIO_READY);
149 if (ret) {
150 dev_err(&pdev->dev, "unable to set input mode on gpio %d\n",
151 RB4XX_NAND_GPIO_READY);
152 goto err_free_gpio_ready;
153 }
154
155 ret = gpio_request(RB4XX_NAND_GPIO_ALE, "NAND ALE");
156 if (ret) {
157 dev_err(&pdev->dev, "unable to request gpio %d\n",
158 RB4XX_NAND_GPIO_ALE);
159 goto err_free_gpio_ready;
160 }
161
162 ret = gpio_direction_output(RB4XX_NAND_GPIO_ALE, 0);
163 if (ret) {
164 dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
165 RB4XX_NAND_GPIO_ALE);
166 goto err_free_gpio_ale;
167 }
168
169 ret = gpio_request(RB4XX_NAND_GPIO_CLE, "NAND CLE");
170 if (ret) {
171 dev_err(&pdev->dev, "unable to request gpio %d\n",
172 RB4XX_NAND_GPIO_CLE);
173 goto err_free_gpio_ale;
174 }
175
176 ret = gpio_direction_output(RB4XX_NAND_GPIO_CLE, 0);
177 if (ret) {
178 dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
179 RB4XX_NAND_GPIO_CLE);
180 goto err_free_gpio_cle;
181 }
182
183 ret = gpio_request(RB4XX_NAND_GPIO_NCE, "NAND NCE");
184 if (ret) {
185 dev_err(&pdev->dev, "unable to request gpio %d\n",
186 RB4XX_NAND_GPIO_NCE);
187 goto err_free_gpio_cle;
188 }
189
190 ret = gpio_direction_output(RB4XX_NAND_GPIO_NCE, 1);
191 if (ret) {
192 dev_err(&pdev->dev, "unable to set output mode on gpio %d\n",
193 RB4XX_NAND_GPIO_ALE);
194 goto err_free_gpio_nce;
195 }
196
197 info = kzalloc(sizeof(*info), GFP_KERNEL);
198 if (!info) {
199 dev_err(&pdev->dev, "rb4xx-nand: no memory for private data\n");
200 ret = -ENOMEM;
201 goto err_free_gpio_nce;
202 }
203
204 info->chip.priv = &info;
205 info->mtd.priv = &info->chip;
206 info->mtd.owner = THIS_MODULE;
207
208 info->chip.cmd_ctrl = rb4xx_nand_cmd_ctrl;
209 info->chip.dev_ready = rb4xx_nand_dev_ready;
210 info->chip.read_byte = rb4xx_nand_read_byte;
211 info->chip.write_buf = rb4xx_nand_write_buf;
212 info->chip.read_buf = rb4xx_nand_read_buf;
213 #if 0
214 info->chip.verify_buf = rb4xx_nand_verify_buf;
215 #endif
216
217 info->chip.chip_delay = 25;
218 info->chip.ecc.mode = NAND_ECC_SOFT;
219 info->chip.options |= NAND_NO_AUTOINCR;
220
221 platform_set_drvdata(pdev, info);
222
223 ret = nand_scan_ident(&info->mtd, 1);
224 if (ret) {
225 ret = -ENXIO;
226 goto err_free_info;
227 }
228
229 if (info->mtd.writesize == 512)
230 info->chip.ecc.layout = &rb4xx_nand_ecclayout;
231
232 ret = nand_scan_tail(&info->mtd);
233 if (ret) {
234 return -ENXIO;
235 goto err_set_drvdata;
236 }
237
238 #ifdef CONFIG_MTD_PARTITIONS
239 ret = add_mtd_partitions(&info->mtd, rb4xx_nand_partitions,
240 ARRAY_SIZE(rb4xx_nand_partitions));
241 #else
242 ret = add_mtd_device(&info->mtd);
243 #endif
244 if (ret)
245 goto err_release_nand;
246
247 return 0;
248
249 err_release_nand:
250 nand_release(&info->mtd);
251 err_set_drvdata:
252 platform_set_drvdata(pdev, NULL);
253 err_free_info:
254 kfree(info);
255 err_free_gpio_nce:
256 gpio_free(RB4XX_NAND_GPIO_NCE);
257 err_free_gpio_cle:
258 gpio_free(RB4XX_NAND_GPIO_CLE);
259 err_free_gpio_ale:
260 gpio_free(RB4XX_NAND_GPIO_ALE);
261 err_free_gpio_ready:
262 gpio_free(RB4XX_NAND_GPIO_READY);
263 err:
264 return ret;
265 }
266
267 static int __devexit rb4xx_nand_remove(struct platform_device *pdev)
268 {
269 struct rb4xx_nand_info *info = platform_get_drvdata(pdev);
270
271 nand_release(&info->mtd);
272 platform_set_drvdata(pdev, NULL);
273 kfree(info);
274 gpio_free(RB4XX_NAND_GPIO_NCE);
275 gpio_free(RB4XX_NAND_GPIO_CLE);
276 gpio_free(RB4XX_NAND_GPIO_ALE);
277 gpio_free(RB4XX_NAND_GPIO_READY);
278
279 return 0;
280 }
281
282 static struct platform_driver rb4xx_nand_driver = {
283 .probe = rb4xx_nand_probe,
284 .remove = __devexit_p(rb4xx_nand_remove),
285 .driver = {
286 .name = DRV_NAME,
287 .owner = THIS_MODULE,
288 },
289 };
290
291 static int __init rb4xx_nand_init(void)
292 {
293 return platform_driver_register(&rb4xx_nand_driver);
294 }
295
296 static void __exit rb4xx_nand_exit(void)
297 {
298 platform_driver_unregister(&rb4xx_nand_driver);
299 }
300
301 module_init(rb4xx_nand_init);
302 module_exit(rb4xx_nand_exit);
303
304 MODULE_DESCRIPTION(DRV_DESC);
305 MODULE_VERSION(DRV_VERSION);
306 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
307 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
308 MODULE_LICENSE("GPL v2");