2 * NAND flash driver for the MikroTik RouterBoard 4xx series
4 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7 * This file was based on the driver for Linux 2.6.22 published by
8 * MikroTik for their RouterBoard 4xx series devices.
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.
15 #include <linux/version.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
20 #include <linux/mtd/nand.h>
22 #include <linux/mtd/rawnand.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/partitions.h>
26 #include <linux/platform_device.h>
27 #include <linux/delay.h>
29 #include <linux/gpio.h>
30 #include <linux/slab.h>
32 #include <asm/mach-ath79/ath79.h>
33 #include <asm/mach-ath79/rb4xx_cpld.h>
35 #define DRV_NAME "rb4xx-nand"
36 #define DRV_VERSION "0.2.0"
37 #define DRV_DESC "NAND flash driver for RouterBoard 4xx series"
39 #define RB4XX_NAND_GPIO_READY 5
40 #define RB4XX_NAND_GPIO_ALE 37
41 #define RB4XX_NAND_GPIO_CLE 38
42 #define RB4XX_NAND_GPIO_NCE 39
44 struct rb4xx_nand_info
{
45 struct nand_chip chip
;
46 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
51 static inline struct rb4xx_nand_info
*mtd_to_rbinfo(struct mtd_info
*mtd
)
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
54 return container_of(mtd
, struct rb4xx_nand_info
, mtd
);
56 struct nand_chip
*chip
= mtd_to_nand(mtd
);
58 return container_of(chip
, struct rb4xx_nand_info
, chip
);
62 static struct mtd_info
*rbinfo_to_mtd(struct rb4xx_nand_info
*nfc
)
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
67 return nand_to_mtd(&nfc
->chip
);
71 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
73 * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
74 * will not be able to find the kernel that we load.
76 static struct nand_ecclayout rb4xx_nand_ecclayout
= {
78 .eccpos
= { 8, 9, 10, 13, 14, 15 },
80 .oobfree
= { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
85 static int rb4xx_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
86 struct mtd_oob_region
*oobregion
)
90 oobregion
->offset
= 8;
91 oobregion
->length
= 3;
94 oobregion
->offset
= 13;
95 oobregion
->length
= 3;
102 static int rb4xx_ooblayout_free(struct mtd_info
*mtd
, int section
,
103 struct mtd_oob_region
*oobregion
)
107 oobregion
->offset
= 0;
108 oobregion
->length
= 4;
111 oobregion
->offset
= 4;
112 oobregion
->length
= 1;
115 oobregion
->offset
= 6;
116 oobregion
->length
= 2;
119 oobregion
->offset
= 11;
120 oobregion
->length
= 2;
127 static const struct mtd_ooblayout_ops rb4xx_nand_ecclayout_ops
= {
128 .ecc
= rb4xx_ooblayout_ecc
,
129 .free
= rb4xx_ooblayout_free
,
133 static struct mtd_partition rb4xx_nand_partitions
[] = {
137 .size
= (256 * 1024),
138 .mask_flags
= MTD_WRITEABLE
,
142 .offset
= (256 * 1024),
143 .size
= (4 * 1024 * 1024) - (256 * 1024),
147 .offset
= MTDPART_OFS_NXTBLK
,
148 .size
= MTDPART_SIZ_FULL
,
152 static int rb4xx_nand_dev_ready(struct mtd_info
*mtd
)
154 return gpio_get_value_cansleep(RB4XX_NAND_GPIO_READY
);
157 static void rb4xx_nand_write_cmd(unsigned char cmd
)
159 unsigned char data
= cmd
;
162 err
= rb4xx_cpld_write(&data
, 1);
164 pr_err("rb4xx_nand: write cmd failed, err=%d\n", err
);
167 static void rb4xx_nand_cmd_ctrl(struct mtd_info
*mtd
, int cmd
,
170 if (ctrl
& NAND_CTRL_CHANGE
) {
171 gpio_set_value_cansleep(RB4XX_NAND_GPIO_CLE
,
172 (ctrl
& NAND_CLE
) ? 1 : 0);
173 gpio_set_value_cansleep(RB4XX_NAND_GPIO_ALE
,
174 (ctrl
& NAND_ALE
) ? 1 : 0);
175 gpio_set_value_cansleep(RB4XX_NAND_GPIO_NCE
,
176 (ctrl
& NAND_NCE
) ? 0 : 1);
179 if (cmd
!= NAND_CMD_NONE
)
180 rb4xx_nand_write_cmd(cmd
);
183 static unsigned char rb4xx_nand_read_byte(struct mtd_info
*mtd
)
185 unsigned char data
= 0;
188 err
= rb4xx_cpld_read(&data
, 1);
190 pr_err("rb4xx_nand: read data failed, err=%d\n", err
);
197 static void rb4xx_nand_write_buf(struct mtd_info
*mtd
, const unsigned char *buf
,
202 err
= rb4xx_cpld_write(buf
, len
);
204 pr_err("rb4xx_nand: write buf failed, err=%d\n", err
);
207 static void rb4xx_nand_read_buf(struct mtd_info
*mtd
, unsigned char *buf
,
212 err
= rb4xx_cpld_read(buf
, len
);
214 pr_err("rb4xx_nand: read buf failed, err=%d\n", err
);
217 static int rb4xx_nand_probe(struct platform_device
*pdev
)
219 struct rb4xx_nand_info
*info
;
220 struct mtd_info
*mtd
;
223 printk(KERN_INFO DRV_DESC
" version " DRV_VERSION
"\n");
225 ret
= gpio_request(RB4XX_NAND_GPIO_READY
, "NAND RDY");
227 dev_err(&pdev
->dev
, "unable to request gpio %d\n",
228 RB4XX_NAND_GPIO_READY
);
232 ret
= gpio_direction_input(RB4XX_NAND_GPIO_READY
);
234 dev_err(&pdev
->dev
, "unable to set input mode on gpio %d\n",
235 RB4XX_NAND_GPIO_READY
);
236 goto err_free_gpio_ready
;
239 ret
= gpio_request(RB4XX_NAND_GPIO_ALE
, "NAND ALE");
241 dev_err(&pdev
->dev
, "unable to request gpio %d\n",
242 RB4XX_NAND_GPIO_ALE
);
243 goto err_free_gpio_ready
;
246 ret
= gpio_direction_output(RB4XX_NAND_GPIO_ALE
, 0);
248 dev_err(&pdev
->dev
, "unable to set output mode on gpio %d\n",
249 RB4XX_NAND_GPIO_ALE
);
250 goto err_free_gpio_ale
;
253 ret
= gpio_request(RB4XX_NAND_GPIO_CLE
, "NAND CLE");
255 dev_err(&pdev
->dev
, "unable to request gpio %d\n",
256 RB4XX_NAND_GPIO_CLE
);
257 goto err_free_gpio_ale
;
260 ret
= gpio_direction_output(RB4XX_NAND_GPIO_CLE
, 0);
262 dev_err(&pdev
->dev
, "unable to set output mode on gpio %d\n",
263 RB4XX_NAND_GPIO_CLE
);
264 goto err_free_gpio_cle
;
267 ret
= gpio_request(RB4XX_NAND_GPIO_NCE
, "NAND NCE");
269 dev_err(&pdev
->dev
, "unable to request gpio %d\n",
270 RB4XX_NAND_GPIO_NCE
);
271 goto err_free_gpio_cle
;
274 ret
= gpio_direction_output(RB4XX_NAND_GPIO_NCE
, 1);
276 dev_err(&pdev
->dev
, "unable to set output mode on gpio %d\n",
277 RB4XX_NAND_GPIO_ALE
);
278 goto err_free_gpio_nce
;
281 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
283 dev_err(&pdev
->dev
, "rb4xx-nand: no memory for private data\n");
285 goto err_free_gpio_nce
;
288 info
->chip
.priv
= &info
;
289 mtd
= rbinfo_to_mtd(info
);
291 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
292 mtd
->priv
= &info
->chip
;
294 mtd
->owner
= THIS_MODULE
;
296 info
->chip
.cmd_ctrl
= rb4xx_nand_cmd_ctrl
;
297 info
->chip
.dev_ready
= rb4xx_nand_dev_ready
;
298 info
->chip
.read_byte
= rb4xx_nand_read_byte
;
299 info
->chip
.write_buf
= rb4xx_nand_write_buf
;
300 info
->chip
.read_buf
= rb4xx_nand_read_buf
;
302 info
->chip
.chip_delay
= 25;
303 info
->chip
.ecc
.mode
= NAND_ECC_SOFT
;
304 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
305 info
->chip
.ecc
.algo
= NAND_ECC_HAMMING
;
307 info
->chip
.options
= NAND_NO_SUBPAGE_WRITE
;
309 platform_set_drvdata(pdev
, info
);
311 ret
= nand_scan_ident(mtd
, 1, NULL
);
317 if (mtd
->writesize
== 512)
318 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
319 info
->chip
.ecc
.layout
= &rb4xx_nand_ecclayout
;
321 mtd_set_ooblayout(mtd
, &rb4xx_nand_ecclayout_ops
);
324 ret
= nand_scan_tail(mtd
);
327 goto err_set_drvdata
;
330 mtd_device_register(mtd
, rb4xx_nand_partitions
,
331 ARRAY_SIZE(rb4xx_nand_partitions
));
333 goto err_release_nand
;
340 platform_set_drvdata(pdev
, NULL
);
344 gpio_free(RB4XX_NAND_GPIO_NCE
);
346 gpio_free(RB4XX_NAND_GPIO_CLE
);
348 gpio_free(RB4XX_NAND_GPIO_ALE
);
350 gpio_free(RB4XX_NAND_GPIO_READY
);
355 static int rb4xx_nand_remove(struct platform_device
*pdev
)
357 struct rb4xx_nand_info
*info
= platform_get_drvdata(pdev
);
359 nand_release(rbinfo_to_mtd(info
));
360 platform_set_drvdata(pdev
, NULL
);
362 gpio_free(RB4XX_NAND_GPIO_NCE
);
363 gpio_free(RB4XX_NAND_GPIO_CLE
);
364 gpio_free(RB4XX_NAND_GPIO_ALE
);
365 gpio_free(RB4XX_NAND_GPIO_READY
);
370 static struct platform_driver rb4xx_nand_driver
= {
371 .probe
= rb4xx_nand_probe
,
372 .remove
= rb4xx_nand_remove
,
375 .owner
= THIS_MODULE
,
379 static int __init
rb4xx_nand_init(void)
381 return platform_driver_register(&rb4xx_nand_driver
);
384 static void __exit
rb4xx_nand_exit(void)
386 platform_driver_unregister(&rb4xx_nand_driver
);
389 module_init(rb4xx_nand_init
);
390 module_exit(rb4xx_nand_exit
);
392 MODULE_DESCRIPTION(DRV_DESC
);
393 MODULE_VERSION(DRV_VERSION
);
394 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
395 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
396 MODULE_LICENSE("GPL v2");