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/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mtd/nand.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/partitions.h>
16 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/version.h>
21 #include <asm/mach-ath79/ar71xx_regs.h>
22 #include <asm/mach-ath79/ath79.h>
23 #include <asm/mach-ath79/mach-rb750.h>
25 #define DRV_NAME "rb750-nand"
26 #define DRV_VERSION "0.1.0"
27 #define DRV_DESC "NAND flash driver for the RouterBOARD 750"
29 #define RB750_NAND_IO0 BIT(RB750_GPIO_NAND_IO0)
30 #define RB750_NAND_ALE BIT(RB750_GPIO_NAND_ALE)
31 #define RB750_NAND_CLE BIT(RB750_GPIO_NAND_CLE)
32 #define RB750_NAND_NRE BIT(RB750_GPIO_NAND_NRE)
33 #define RB750_NAND_NWE BIT(RB750_GPIO_NAND_NWE)
34 #define RB750_NAND_RDY BIT(RB750_GPIO_NAND_RDY)
36 #define RB750_NAND_DATA_SHIFT 1
37 #define RB750_NAND_DATA_BITS (0xff << RB750_NAND_DATA_SHIFT)
38 #define RB750_NAND_INPUT_BITS (RB750_NAND_DATA_BITS | RB750_NAND_RDY)
39 #define RB750_NAND_OUTPUT_BITS (RB750_NAND_ALE | RB750_NAND_CLE | \
40 RB750_NAND_NRE | RB750_NAND_NWE)
42 struct rb750_nand_info {
43 struct nand_chip chip;
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
47 struct rb7xx_nand_platform_data *pdata;
50 static inline struct rb750_nand_info *mtd_to_rbinfo(struct mtd_info *mtd)
52 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
53 return container_of(mtd, struct rb750_nand_info, mtd);
55 struct nand_chip *chip = mtd_to_nand(mtd);
57 return container_of(chip, struct rb750_nand_info, chip);
61 static struct mtd_info *rbinfo_to_mtd(struct rb750_nand_info *nfc)
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
66 return nand_to_mtd(&nfc->chip);
70 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
72 * We need to use the OLD Yaffs-1 OOB layout, otherwise the RB bootloader
73 * will not be able to find the kernel that we load.
75 static struct nand_ecclayout rb750_nand_ecclayout = {
77 .eccpos = { 8, 9, 10, 13, 14, 15 },
79 .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
84 static int rb750_ooblayout_ecc(struct mtd_info *mtd, int section,
85 struct mtd_oob_region *oobregion)
89 oobregion->offset = 8;
90 oobregion->length = 3;
93 oobregion->offset = 13;
94 oobregion->length = 3;
101 static int rb750_ooblayout_free(struct mtd_info *mtd, int section,
102 struct mtd_oob_region *oobregion)
106 oobregion->offset = 0;
107 oobregion->length = 4;
110 oobregion->offset = 4;
111 oobregion->length = 1;
114 oobregion->offset = 6;
115 oobregion->length = 2;
118 oobregion->offset = 11;
119 oobregion->length = 2;
126 static const struct mtd_ooblayout_ops rb750_nand_ecclayout_ops = {
127 .ecc = rb750_ooblayout_ecc,
128 .free = rb750_ooblayout_free,
132 static struct mtd_partition rb750_nand_partitions[] = {
136 .size = (256 * 1024),
137 .mask_flags = MTD_WRITEABLE,
140 .offset = (256 * 1024),
141 .size = (4 * 1024 * 1024) - (256 * 1024),
144 .offset = MTDPART_OFS_NXTBLK,
145 .size = MTDPART_SIZ_FULL,
149 static void rb750_nand_write(const u8 *buf, unsigned len)
151 void __iomem *base = ath79_gpio_base;
156 /* set data lines to output mode */
157 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
158 __raw_writel(t | RB750_NAND_DATA_BITS, base + AR71XX_GPIO_REG_OE);
160 out = __raw_readl(base + AR71XX_GPIO_REG_OUT);
161 out &= ~(RB750_NAND_DATA_BITS | RB750_NAND_NWE);
162 for (i = 0; i != len; i++) {
166 data <<= RB750_NAND_DATA_SHIFT;
168 __raw_writel(data, base + AR71XX_GPIO_REG_OUT);
170 __raw_writel(data | RB750_NAND_NWE, base + AR71XX_GPIO_REG_OUT);
172 __raw_readl(base + AR71XX_GPIO_REG_OUT);
175 /* set data lines to input mode */
176 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
177 __raw_writel(t & ~RB750_NAND_DATA_BITS, base + AR71XX_GPIO_REG_OE);
179 __raw_readl(base + AR71XX_GPIO_REG_OE);
182 static void rb750_nand_read(u8 *read_buf, unsigned len)
184 void __iomem *base = ath79_gpio_base;
187 for (i = 0; i < len; i++) {
190 /* activate RE line */
191 __raw_writel(RB750_NAND_NRE, base + AR71XX_GPIO_REG_CLEAR);
193 __raw_readl(base + AR71XX_GPIO_REG_CLEAR);
195 /* read input lines */
196 data = __raw_readl(base + AR71XX_GPIO_REG_IN) >>
197 RB750_NAND_DATA_SHIFT;
199 /* deactivate RE line */
200 __raw_writel(RB750_NAND_NRE, base + AR71XX_GPIO_REG_SET);
206 static void rb750_nand_select_chip(struct mtd_info *mtd, int chip)
208 struct rb750_nand_info *rbinfo = mtd_to_rbinfo(mtd);
209 void __iomem *base = ath79_gpio_base;
213 rbinfo->pdata->enable_pins();
215 /* set input mode for data lines */
216 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
217 __raw_writel(t & ~RB750_NAND_INPUT_BITS,
218 base + AR71XX_GPIO_REG_OE);
220 /* deactivate RE and WE lines */
221 __raw_writel(RB750_NAND_NRE | RB750_NAND_NWE,
222 base + AR71XX_GPIO_REG_SET);
224 (void) __raw_readl(base + AR71XX_GPIO_REG_SET);
226 /* activate CE line */
227 __raw_writel(rbinfo->pdata->nce_line,
228 base + AR71XX_GPIO_REG_CLEAR);
230 /* deactivate CE line */
231 __raw_writel(rbinfo->pdata->nce_line,
232 base + AR71XX_GPIO_REG_SET);
234 (void) __raw_readl(base + AR71XX_GPIO_REG_SET);
236 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
237 __raw_writel(t | RB750_NAND_IO0 | RB750_NAND_RDY,
238 base + AR71XX_GPIO_REG_OE);
240 rbinfo->pdata->disable_pins();
244 static int rb750_nand_dev_ready(struct mtd_info *mtd)
246 void __iomem *base = ath79_gpio_base;
248 return !!(__raw_readl(base + AR71XX_GPIO_REG_IN) & RB750_NAND_RDY);
251 static void rb750_nand_cmd_ctrl(struct mtd_info *mtd, int cmd,
254 if (ctrl & NAND_CTRL_CHANGE) {
255 void __iomem *base = ath79_gpio_base;
258 t = __raw_readl(base + AR71XX_GPIO_REG_OUT);
260 t &= ~(RB750_NAND_CLE | RB750_NAND_ALE);
261 t |= (ctrl & NAND_CLE) ? RB750_NAND_CLE : 0;
262 t |= (ctrl & NAND_ALE) ? RB750_NAND_ALE : 0;
264 __raw_writel(t, base + AR71XX_GPIO_REG_OUT);
266 __raw_readl(base + AR71XX_GPIO_REG_OUT);
269 if (cmd != NAND_CMD_NONE) {
271 rb750_nand_write(&t, 1);
275 static u8 rb750_nand_read_byte(struct mtd_info *mtd)
278 rb750_nand_read(&data, 1);
282 static void rb750_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
284 rb750_nand_read(buf, len);
287 static void rb750_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
289 rb750_nand_write(buf, len);
292 static void __init rb750_nand_gpio_init(struct rb750_nand_info *info)
294 void __iomem *base = ath79_gpio_base;
298 out = __raw_readl(base + AR71XX_GPIO_REG_OUT);
300 /* setup output levels */
301 __raw_writel(RB750_NAND_NCE | RB750_NAND_NRE | RB750_NAND_NWE,
302 base + AR71XX_GPIO_REG_SET);
304 __raw_writel(RB750_NAND_ALE | RB750_NAND_CLE,
305 base + AR71XX_GPIO_REG_CLEAR);
307 /* setup input lines */
308 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
309 __raw_writel(t & ~(RB750_NAND_INPUT_BITS), base + AR71XX_GPIO_REG_OE);
311 /* setup output lines */
312 t = __raw_readl(base + AR71XX_GPIO_REG_OE);
313 t |= RB750_NAND_OUTPUT_BITS;
314 t |= info->pdata->nce_line;
315 __raw_writel(t, base + AR71XX_GPIO_REG_OE);
317 info->pdata->latch_change(~out & RB750_NAND_IO0, out & RB750_NAND_IO0);
320 static int rb750_nand_probe(struct platform_device *pdev)
322 struct rb750_nand_info *info;
323 struct rb7xx_nand_platform_data *pdata;
324 struct mtd_info *mtd;
327 printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n");
329 pdata = pdev->dev.platform_data;
333 info = kzalloc(sizeof(*info), GFP_KERNEL);
337 info->chip.priv = &info;
339 mtd = rbinfo_to_mtd(info);
340 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
341 mtd->priv = &info->chip;
343 mtd->owner = THIS_MODULE;
345 info->chip.select_chip = rb750_nand_select_chip;
346 info->chip.cmd_ctrl = rb750_nand_cmd_ctrl;
347 info->chip.dev_ready = rb750_nand_dev_ready;
348 info->chip.read_byte = rb750_nand_read_byte;
349 info->chip.write_buf = rb750_nand_write_buf;
350 info->chip.read_buf = rb750_nand_read_buf;
352 info->chip.chip_delay = 25;
353 info->chip.ecc.mode = NAND_ECC_SOFT;
354 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,6,0)
355 info->chip.ecc.algo = NAND_ECC_HAMMING;
357 info->chip.options = NAND_NO_SUBPAGE_WRITE;
361 platform_set_drvdata(pdev, info);
363 rb750_nand_gpio_init(info);
365 ret = nand_scan_ident(mtd, 1, NULL);
371 if (mtd->writesize == 512)
372 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,6,0)
373 info->chip.ecc.layout = &rb750_nand_ecclayout;
375 mtd_set_ooblayout(mtd, &rb750_nand_ecclayout_ops);
378 ret = nand_scan_tail(mtd);
381 goto err_set_drvdata;
384 ret = mtd_device_register(mtd, rb750_nand_partitions,
385 ARRAY_SIZE(rb750_nand_partitions));
387 goto err_release_nand;
394 platform_set_drvdata(pdev, NULL);
400 static int rb750_nand_remove(struct platform_device *pdev)
402 struct rb750_nand_info *info = platform_get_drvdata(pdev);
404 nand_release(rbinfo_to_mtd(info));
405 platform_set_drvdata(pdev, NULL);
411 static struct platform_driver rb750_nand_driver = {
412 .probe = rb750_nand_probe,
413 .remove = rb750_nand_remove,
416 .owner = THIS_MODULE,
420 static int __init rb750_nand_init(void)
422 return platform_driver_register(&rb750_nand_driver);
425 static void __exit rb750_nand_exit(void)
427 platform_driver_unregister(&rb750_nand_driver);
430 module_init(rb750_nand_init);
431 module_exit(rb750_nand_exit);
433 MODULE_DESCRIPTION(DRV_DESC);
434 MODULE_VERSION(DRV_VERSION);
435 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
436 MODULE_LICENSE("GPL v2");