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