1 --- a/drivers/mtd/nand/Kconfig
2 +++ b/drivers/mtd/nand/Kconfig
3 @@ -432,6 +432,13 @@ config MTD_NAND_PLATFORM
4 devices. You will need to provide platform-specific functions
7 +config MTD_NAND_RB_PPC
8 + tristate "MikroTik RB333/600 NAND support"
11 + This option enables support for the NAND device on MikroTik
12 + RouterBOARD 333/600 series boards.
15 tristate "MTD driver for Olympus MAUSB-10 and Fujifilm DPC-R1"
17 --- a/drivers/mtd/nand/Makefile
18 +++ b/drivers/mtd/nand/Makefile
19 @@ -31,6 +31,7 @@ obj-$(CONFIG_MTD_NAND_CM_X270) += cmx27
20 obj-$(CONFIG_MTD_NAND_PXA3xx) += pxa3xx_nand.o
21 obj-$(CONFIG_MTD_NAND_TMIO) += tmio_nand.o
22 obj-$(CONFIG_MTD_NAND_PLATFORM) += plat_nand.o
23 +obj-$(CONFIG_MTD_NAND_RB_PPC) += rbppc_nand.o
24 obj-$(CONFIG_MTD_ALAUDA) += alauda.o
25 obj-$(CONFIG_MTD_NAND_PASEMI) += pasemi_nand.o
26 obj-$(CONFIG_MTD_NAND_ORION) += orion_nand.o
28 +++ b/drivers/mtd/nand/rbppc_nand.c
31 + * Copyright (C) 2008-2009 Noah Fontes <nfontes@transtruct.org>
32 + * Copyright (C) 2009 Michael Guntsche <mike@it-loops.com>
33 + * Copyright (C) Mikrotik 2007
35 + * This program is free software; you can redistribute it and/or modify it
36 + * under the terms of the GNU General Public License as published by the
37 + * Free Software Foundation; either version 2 of the License, or (at your
38 + * option) any later version.
41 +#include <linux/init.h>
42 +#include <linux/module.h>
43 +#include <linux/mtd/nand.h>
44 +#include <linux/mtd/mtd.h>
45 +#include <linux/mtd/partitions.h>
46 +#include <linux/of_platform.h>
47 +#include <linux/of_device.h>
48 +#include <linux/slab.h>
49 +#include <linux/delay.h>
52 +#define DRV_NAME "rbppc_nand"
53 +#define DRV_VERSION "0.0.2"
55 +static struct mtd_info rmtd;
56 +static struct nand_chip rnand;
58 +struct rbppc_nand_info {
67 + unsigned gpio_ctrls;
70 +/* We must use the OOB layout from yaffs 1 if we want this to be recognized
71 + * properly. Borrowed from the OpenWRT patches for the RB532.
73 + * See <https://dev.openwrt.org/browser/trunk/target/linux/rb532/
74 + * patches-2.6.28/025-rb532_nand_fixup.patch> for more details.
76 +static struct nand_ecclayout rbppc_nand_oob_16 = {
78 + .eccpos = { 8, 9, 10, 13, 14, 15 },
80 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
83 +static struct mtd_partition rbppc_nand_partition_info[] = {
87 + size: 4 * 1024 * 1024,
91 + offset: MTDPART_OFS_NXTBLK,
92 + size: MTDPART_SIZ_FULL,
96 +static int rbppc_nand_dev_ready(struct mtd_info *mtd) {
97 + struct nand_chip *chip = mtd->priv;
98 + struct rbppc_nand_info *priv = chip->priv;
100 + return in_be32(priv->gpi) & priv->gpio_rdy;
103 +static void rbppc_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl) {
104 + struct nand_chip *chip = mtd->priv;
105 + struct rbppc_nand_info *priv = chip->priv;
107 + if (ctrl & NAND_CTRL_CHANGE) {
108 + unsigned val = in_be32(priv->gpo);
109 + if (!(val & priv->gpio_nce)) {
110 + /* make sure Local Bus has done NAND operations */
111 + readb(priv->localbus);
114 + if (ctrl & NAND_CLE) {
115 + val |= priv->gpio_cle;
117 + val &= ~priv->gpio_cle;
119 + if (ctrl & NAND_ALE) {
120 + val |= priv->gpio_ale;
122 + val &= ~priv->gpio_ale;
124 + if (!(ctrl & NAND_NCE)) {
125 + val |= priv->gpio_nce;
127 + val &= ~priv->gpio_nce;
129 + out_be32(priv->gpo, val);
131 + /* make sure GPIO output has changed */
132 + val ^= in_be32(priv->gpo);
133 + if (val & priv->gpio_ctrls) {
134 + printk(KERN_ERR "rbppc_nand_hwcontrol: NAND GPO change failed 0x%08x\n", val);
138 + if (cmd != NAND_CMD_NONE) writeb(cmd, chip->IO_ADDR_W);
141 +static void rbppc_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
143 + struct nand_chip *chip = mtd->priv;
144 + memcpy(buf, chip->IO_ADDR_R, len);
147 +static unsigned init_ok = 0;
149 +static int rbppc_nand_probe(struct platform_device *pdev)
151 + struct device_node *gpio;
152 + struct device_node *nnand;
153 + struct resource res;
154 + struct rbppc_nand_info *info;
156 + const unsigned *rdy, *nce, *cle, *ale;
158 + printk(KERN_INFO "rbppc_nand_probe: MikroTik RouterBOARD 333/600 series NAND driver, version " DRV_VERSION "\n");
160 + info = kmalloc(sizeof(*info), GFP_KERNEL);
162 + rdy = of_get_property(pdev->dev.of_node, "rdy", NULL);
163 + nce = of_get_property(pdev->dev.of_node, "nce", NULL);
164 + cle = of_get_property(pdev->dev.of_node, "cle", NULL);
165 + ale = of_get_property(pdev->dev.of_node, "ale", NULL);
167 + if (!rdy || !nce || !cle || !ale) {
168 + printk(KERN_ERR "rbppc_nand_probe: GPIO properties are missing\n");
171 + if (rdy[0] != nce[0] || rdy[0] != cle[0] || rdy[0] != ale[0]) {
172 + printk(KERN_ERR "rbppc_nand_probe: Different GPIOs are not supported\n");
176 + gpio = of_find_node_by_phandle(rdy[0]);
178 + printk(KERN_ERR "rbppc_nand_probe: No GPIO<%x> node found\n", *rdy);
181 + if (of_address_to_resource(gpio, 0, &res)) {
182 + printk(KERN_ERR "rbppc_nand_probe: No reg property in GPIO found\n");
185 + info->gpo = ioremap_nocache(res.start, res.end - res.start + 1);
187 + if (!of_address_to_resource(gpio, 1, &res)) {
188 + info->gpi = ioremap_nocache(res.start, res.end - res.start + 1);
190 + info->gpi = info->gpo;
194 + info->gpio_rdy = 1 << (31 - rdy[1]);
195 + info->gpio_nce = 1 << (31 - nce[1]);
196 + info->gpio_cle = 1 << (31 - cle[1]);
197 + info->gpio_ale = 1 << (31 - ale[1]);
198 + info->gpio_ctrls = info->gpio_nce | info->gpio_cle | info->gpio_ale;
200 + nnand = of_find_node_by_name(NULL, "nnand");
202 + printk("rbppc_nand_probe: No nNAND found\n");
205 + if (of_address_to_resource(nnand, 0, &res)) {
206 + printk("rbppc_nand_probe: No reg property in nNAND found\n");
209 + of_node_put(nnand);
210 + info->localbus = ioremap_nocache(res.start, res.end - res.start + 1);
212 + if (of_address_to_resource(pdev->dev.of_node, 0, &res)) {
213 + printk("rbppc_nand_probe: No reg property found\n");
216 + baddr = ioremap_nocache(res.start, res.end - res.start + 1);
218 + memset(&rnand, 0, sizeof(rnand));
219 + rnand.cmd_ctrl = rbppc_nand_cmd_ctrl;
220 + rnand.dev_ready = rbppc_nand_dev_ready;
221 + rnand.read_buf = rbppc_nand_read_buf;
222 + rnand.IO_ADDR_W = baddr;
223 + rnand.IO_ADDR_R = baddr;
226 + memset(&rmtd, 0, sizeof(rmtd));
227 + rnand.ecc.mode = NAND_ECC_SOFT;
228 + rnand.ecc.layout = &rbppc_nand_oob_16;
229 + rnand.chip_delay = 25;
230 + rmtd.priv = &rnand;
231 + rmtd.owner = THIS_MODULE;
233 + if (nand_scan(&rmtd, 1) && nand_scan(&rmtd, 1) && nand_scan(&rmtd, 1) && nand_scan(&rmtd, 1)) {
234 + printk(KERN_ERR "rbppc_nand_probe: RouterBOARD NAND device not found\n");
238 + mtd_device_parse_register(&rmtd, NULL, 0, rbppc_nand_partition_info, 2);
247 +static struct of_device_id rbppc_nand_ids[] = {
248 + { .name = "nand", },
252 +static struct platform_driver rbppc_nand_driver = {
253 + .probe = rbppc_nand_probe,
255 + .name = "rbppc-nand",
256 + .owner = THIS_MODULE,
257 + .of_match_table = rbppc_nand_ids,
261 +static int __init rbppc_nand_init(void)
263 + return platform_driver_register(&rbppc_nand_driver);
266 +static void __exit rbppc_nand_exit(void)
268 + platform_driver_unregister(&rbppc_nand_driver);
271 +MODULE_AUTHOR("Mikrotikls SIA");
272 +MODULE_AUTHOR("Noah Fontes");
273 +MODULE_AUTHOR("Michael Guntsche");
274 +MODULE_DESCRIPTION("MikroTik RouterBOARD 333/600 series NAND driver");
275 +MODULE_LICENSE("GPL");
276 +MODULE_VERSION(DRV_VERSION);
278 +module_init(rbppc_nand_init);
279 +module_exit(rbppc_nand_exit);