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