2 * NAND flash driver for the MikroTik RouterBOARD 750
4 * Copyright (C) 2010-2012 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
11 #include <linux/version.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,14,0)
15 #include <linux/mtd/nand.h>
17 #include <linux/mtd/rawnand.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/platform_device.h>
23 #include <linux/slab.h>
25 #include <asm/mach-ath79/ar71xx_regs.h>
26 #include <asm/mach-ath79/ath79.h>
27 #include <asm/mach-ath79/mach-rb750.h>
29 #define DRV_NAME "rb750-nand"
30 #define DRV_VERSION "0.1.0"
31 #define DRV_DESC "NAND flash driver for the RouterBOARD 750"
33 #define RB750_NAND_IO0 BIT(RB750_GPIO_NAND_IO0)
34 #define RB750_NAND_ALE BIT(RB750_GPIO_NAND_ALE)
35 #define RB750_NAND_CLE BIT(RB750_GPIO_NAND_CLE)
36 #define RB750_NAND_NRE BIT(RB750_GPIO_NAND_NRE)
37 #define RB750_NAND_NWE BIT(RB750_GPIO_NAND_NWE)
38 #define RB750_NAND_RDY BIT(RB750_GPIO_NAND_RDY)
40 #define RB750_NAND_DATA_SHIFT 1
41 #define RB750_NAND_DATA_BITS (0xff << RB750_NAND_DATA_SHIFT)
42 #define RB750_NAND_INPUT_BITS (RB750_NAND_DATA_BITS | RB750_NAND_RDY)
43 #define RB750_NAND_OUTPUT_BITS (RB750_NAND_ALE | RB750_NAND_CLE | \
44 RB750_NAND_NRE | RB750_NAND_NWE)
46 struct rb750_nand_info {
47 struct nand_chip chip;
48 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
51 struct rb7xx_nand_platform_data *pdata;
54 static inline struct rb750_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
56 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
57 return container_of(mtd, struct rb750_nand_info, mtd);
59 struct nand_chip *chip = mtd_to_nand(mtd);
61 return container_of(chip, struct rb750_nand_info, chip);
65 static struct mtd_info *rbinfo_to_mtd(struct rb750_nand_info *nfc)
67 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
70 return nand_to_mtd(&nfc->chip);
74 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
76 * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
77 * will not be able to find the kernel that we load.
79 static struct nand_ecclayout rb750_nand_ecclayout = {
81 .eccpos = { 8, 9, 10, 13, 14, 15 },
83 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
88 static int rb750_ooblayout_ecc(struct mtd_info *mtd, int section,
89 struct mtd_oob_region *oobregion)
93 oobregion->offset = 8;
94 oobregion->length = 3;
97 oobregion->offset = 13;
98 oobregion->length = 3;
105 static int rb750_ooblayout_free(struct mtd_info *mtd, int section,
106 struct mtd_oob_region *oobregion)
110 oobregion->offset = 0;
111 oobregion->length = 4;
114 oobregion->offset = 4;
115 oobregion->length = 1;
118 oobregion->offset = 6;
119 oobregion->length = 2;
122 oobregion->offset = 11;
123 oobregion->length = 2;
130 static const struct mtd_ooblayout_ops rb750_nand_ecclayout_ops = {
131 .ecc = rb750_ooblayout_ecc,
132 .free = rb750_ooblayout_free,
136 static struct mtd_partition rb750_nand_partitions[] = {
140 .size = (256 * 1024),
141 .mask_flags = MTD_WRITEABLE,
144 .offset = (256 * 1024),
145 .size = (4 * 1024 * 1024) - (256 * 1024),
148 .offset = MTDPART_OFS_NXTBLK,
149 .size = MTDPART_SIZ_FULL,
153 static void rb750_nand_write(const u8 *buf, unsigned len)
155 void __iomem *base = ath79_gpio_base;
160 /* set data lines to output mode */
161 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
162 __raw_writel(t | RB750_NAND_DATA_BITS, base + AR71XX_GPIO_REG_OE);
164 out = __raw_readl(base + AR71XX_GPIO_REG_OUT);
165 out &= ~(RB750_NAND_DATA_BITS | RB750_NAND_NWE);
166 for (i = 0; i != len; i++) {
170 data <<= RB750_NAND_DATA_SHIFT;
172 __raw_writel(data, base + AR71XX_GPIO_REG_OUT);
174 __raw_writel(data | RB750_NAND_NWE, base + AR71XX_GPIO_REG_OUT);
176 __raw_readl(base + AR71XX_GPIO_REG_OUT);
179 /* set data lines to input mode */
180 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
181 __raw_writel(t & ~RB750_NAND_DATA_BITS, base + AR71XX_GPIO_REG_OE);
183 __raw_readl(base + AR71XX_GPIO_REG_OE);
186 static void rb750_nand_read(u8 *read_buf, unsigned len)
188 void __iomem *base = ath79_gpio_base;
191 for (i = 0; i < len; i++) {
194 /* activate RE line */
195 __raw_writel(RB750_NAND_NRE, base + AR71XX_GPIO_REG_CLEAR);
197 __raw_readl(base + AR71XX_GPIO_REG_CLEAR);
199 /* read input lines */
200 data = __raw_readl(base + AR71XX_GPIO_REG_IN) >>
201 RB750_NAND_DATA_SHIFT;
203 /* deactivate RE line */
204 __raw_writel(RB750_NAND_NRE, base + AR71XX_GPIO_REG_SET);
210 static void rb750_nand_select_chip(struct mtd_info *mtd, int chip)
212 struct rb750_nand_info *rbinfo = mtd_to_rbinfo(mtd);
213 void __iomem *base = ath79_gpio_base;
217 rbinfo->pdata->enable_pins();
219 /* set input mode for data lines */
220 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
221 __raw_writel(t & ~RB750_NAND_INPUT_BITS,
222 base + AR71XX_GPIO_REG_OE);
224 /* deactivate RE and WE lines */
225 __raw_writel(RB750_NAND_NRE | RB750_NAND_NWE,
226 base + AR71XX_GPIO_REG_SET);
228 (void) __raw_readl(base + AR71XX_GPIO_REG_SET);
230 /* activate CE line */
231 __raw_writel(rbinfo->pdata->nce_line,
232 base + AR71XX_GPIO_REG_CLEAR);
234 /* deactivate CE line */
235 __raw_writel(rbinfo->pdata->nce_line,
236 base + AR71XX_GPIO_REG_SET);
238 (void) __raw_readl(base + AR71XX_GPIO_REG_SET);
240 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
241 __raw_writel(t | RB750_NAND_IO0 | RB750_NAND_RDY,
242 base + AR71XX_GPIO_REG_OE);
244 rbinfo->pdata->disable_pins();
248 static int rb750_nand_dev_ready(struct mtd_info *mtd)
250 void __iomem *base = ath79_gpio_base;
252 return !!(__raw_readl(base + AR71XX_GPIO_REG_IN) & RB750_NAND_RDY);
255 static void rb750_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
258 if (ctrl & NAND_CTRL_CHANGE) {
259 void __iomem *base = ath79_gpio_base;
262 t = __raw_readl(base + AR71XX_GPIO_REG_OUT);
264 t &= ~(RB750_NAND_CLE | RB750_NAND_ALE);
265 t |= (ctrl & NAND_CLE) ? RB750_NAND_CLE : 0;
266 t |= (ctrl & NAND_ALE) ? RB750_NAND_ALE : 0;
268 __raw_writel(t, base + AR71XX_GPIO_REG_OUT);
270 __raw_readl(base + AR71XX_GPIO_REG_OUT);
273 if (cmd != NAND_CMD_NONE) {
275 rb750_nand_write(&t, 1);
279 static u8 rb750_nand_read_byte(struct mtd_info *mtd)
282 rb750_nand_read(&data, 1);
286 static void rb750_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
288 rb750_nand_read(buf, len);
291 static void rb750_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
293 rb750_nand_write(buf, len);
296 static void __init rb750_nand_gpio_init(struct rb750_nand_info *info)
298 void __iomem *base = ath79_gpio_base;
302 out = __raw_readl(base + AR71XX_GPIO_REG_OUT);
304 /* setup output levels */
305 __raw_writel(RB750_NAND_NCE | RB750_NAND_NRE | RB750_NAND_NWE,
306 base + AR71XX_GPIO_REG_SET);
308 __raw_writel(RB750_NAND_ALE | RB750_NAND_CLE,
309 base + AR71XX_GPIO_REG_CLEAR);
311 /* setup input lines */
312 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
313 __raw_writel(t & ~(RB750_NAND_INPUT_BITS), base + AR71XX_GPIO_REG_OE);
315 /* setup output lines */
316 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
317 t |= RB750_NAND_OUTPUT_BITS;
318 t |= info->pdata->nce_line;
319 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
321 info->pdata->latch_change(~out & RB750_NAND_IO0, out & RB750_NAND_IO0);
324 static int rb750_nand_probe(struct platform_device *pdev)
326 struct rb750_nand_info *info;
327 struct rb7xx_nand_platform_data *pdata;
328 struct mtd_info *mtd;
331 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
333 pdata = pdev->dev.platform_data;
337 info = kzalloc(sizeof(*info), GFP_KERNEL);
341 info->chip.priv = &info;
343 mtd = rbinfo_to_mtd(info);
344 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
345 mtd->priv = &info->chip;
347 mtd->owner = THIS_MODULE;
349 info->chip.select_chip = rb750_nand_select_chip;
350 info->chip.cmd_ctrl = rb750_nand_cmd_ctrl;
351 info->chip.dev_ready = rb750_nand_dev_ready;
352 info->chip.read_byte = rb750_nand_read_byte;
353 info->chip.write_buf = rb750_nand_write_buf;
354 info->chip.read_buf = rb750_nand_read_buf;
356 info->chip.chip_delay = 25;
357 info->chip.ecc.mode = NAND_ECC_SOFT;
358 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
359 info->chip.ecc.algo = NAND_ECC_HAMMING;
361 info->chip.options = NAND_NO_SUBPAGE_WRITE;
365 platform_set_drvdata(pdev, info);
367 rb750_nand_gpio_init(info);
369 ret = nand_scan_ident(mtd, 1, NULL);
375 if (mtd->writesize == 512)
376 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
377 info->chip.ecc.layout = &rb750_nand_ecclayout;
379 mtd_set_ooblayout(mtd, &rb750_nand_ecclayout_ops);
382 ret = nand_scan_tail(mtd);
385 goto err_set_drvdata;
388 ret = mtd_device_register(mtd, rb750_nand_partitions,
389 ARRAY_SIZE(rb750_nand_partitions));
391 goto err_release_nand;
398 platform_set_drvdata(pdev, NULL);
404 static int rb750_nand_remove(struct platform_device *pdev)
406 struct rb750_nand_info *info = platform_get_drvdata(pdev);
408 nand_release(rbinfo_to_mtd(info));
409 platform_set_drvdata(pdev, NULL);
415 static struct platform_driver rb750_nand_driver = {
416 .probe = rb750_nand_probe,
417 .remove = rb750_nand_remove,
420 .owner = THIS_MODULE,
424 static int __init rb750_nand_init(void)
426 return platform_driver_register(&rb750_nand_driver);
429 static void __exit rb750_nand_exit(void)
431 platform_driver_unregister(&rb750_nand_driver);
434 module_init(rb750_nand_init);
435 module_exit(rb750_nand_exit);
437 MODULE_DESCRIPTION(DRV_DESC);
438 MODULE_VERSION(DRV_VERSION);
439 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
440 MODULE_LICENSE("GPL v2");