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