2 +++ b/drivers/mtd/nand/rbppc_nand.c
5 + * Copyright (C) 2008-2009 Noah Fontes <nfontes@transtruct.org>
6 + * Copyright (C) 2009 Michael Guntsche <mike@it-loops.com>
7 + * Copyright (C) Mikrotik 2007
9 + * This program is free software; you can redistribute it and/or modify it
10 + * under the terms of the GNU General Public License as published by the
11 + * Free Software Foundation; either version 2 of the License, or (at your
12 + * option) any later version.
15 +#include <linux/init.h>
16 +#include <linux/mtd/nand.h>
17 +#include <linux/mtd/mtd.h>
18 +#include <linux/mtd/partitions.h>
19 +#include <linux/of_platform.h>
20 +#include <asm/of_platform.h>
21 +#include <asm/of_device.h>
22 +#include <linux/delay.h>
25 +#define DRV_NAME "rbppc_nand"
26 +#define DRV_VERSION "0.0.2"
28 +static struct mtd_info rmtd;
29 +static struct nand_chip rnand;
31 +struct rbppc_nand_info {
40 + unsigned gpio_ctrls;
43 +/* We must use the OOB layout from yaffs 1 if we want this to be recognized
44 + * properly. Borrowed from the OpenWRT patches for the RB532.
46 + * See <https://dev.openwrt.org/browser/trunk/target/linux/rb532/
47 + * patches-2.6.28/025-rb532_nand_fixup.patch> for more details.
49 +static struct nand_ecclayout rbppc_nand_oob_16 = {
51 + .eccpos = { 8, 9, 10, 13, 14, 15 },
53 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
56 +static struct mtd_partition rbppc_nand_partition_info[] = {
60 + size: 4 * 1024 * 1024,
64 + offset: MTDPART_OFS_NXTBLK,
65 + size: MTDPART_SIZ_FULL,
69 +static int rbppc_nand_dev_ready(struct mtd_info *mtd) {
70 + struct nand_chip *chip = mtd->priv;
71 + struct rbppc_nand_info *priv = chip->priv;
73 + return in_be32(priv->gpi) & priv->gpio_rdy;
76 +static void rbppc_nand_cmd_ctrl(struct mtd_info *mtd, int cmd, unsigned int ctrl) {
77 + struct nand_chip *chip = mtd->priv;
78 + struct rbppc_nand_info *priv = chip->priv;
80 + if (ctrl & NAND_CTRL_CHANGE) {
81 + unsigned val = in_be32(priv->gpo);
82 + if (!(val & priv->gpio_nce)) {
83 + /* make sure Local Bus has done NAND operations */
84 + readb(priv->localbus);
87 + if (ctrl & NAND_CLE) {
88 + val |= priv->gpio_cle;
90 + val &= ~priv->gpio_cle;
92 + if (ctrl & NAND_ALE) {
93 + val |= priv->gpio_ale;
95 + val &= ~priv->gpio_ale;
97 + if (!(ctrl & NAND_NCE)) {
98 + val |= priv->gpio_nce;
100 + val &= ~priv->gpio_nce;
102 + out_be32(priv->gpo, val);
104 + /* make sure GPIO output has changed */
105 + val ^= in_be32(priv->gpo);
106 + if (val & priv->gpio_ctrls) {
107 + printk(KERN_ERR "rbppc_nand_hwcontrol: NAND GPO change failed 0x%08x\n", val);
111 + if (cmd != NAND_CMD_NONE) writeb(cmd, chip->IO_ADDR_W);
114 +static void rbppc_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
116 + struct nand_chip *chip = mtd->priv;
117 + memcpy(buf, chip->IO_ADDR_R, len);
120 +static unsigned init_ok = 0;
122 +static int rbppc_nand_probe(struct of_device *pdev,
123 + const struct of_device_id *match)
125 + struct device_node *gpio;
126 + struct device_node *nnand;
127 + struct resource res;
128 + struct rbppc_nand_info *info;
130 + const unsigned *rdy, *nce, *cle, *ale;
132 + printk(KERN_INFO "rbppc_nand_probe: MikroTik RouterBOARD 600 series NAND driver, version " DRV_VERSION "\n");
134 + info = kmalloc(sizeof(*info), GFP_KERNEL);
136 + rdy = of_get_property(pdev->node, "rdy", NULL);
137 + nce = of_get_property(pdev->node, "nce", NULL);
138 + cle = of_get_property(pdev->node, "cle", NULL);
139 + ale = of_get_property(pdev->node, "ale", NULL);
141 + if (!rdy || !nce || !cle || !ale) {
142 + printk(KERN_ERR "rbppc_nand_probe: GPIO properties are missing\n");
145 + if (rdy[0] != nce[0] || rdy[0] != cle[0] || rdy[0] != ale[0]) {
146 + printk(KERN_ERR "rbppc_nand_probe: Different GPIOs are not supported\n");
150 + gpio = of_find_node_by_phandle(rdy[0]);
152 + printk(KERN_ERR "rbppc_nand_probe: No GPIO<%x> node found\n", *rdy);
155 + if (of_address_to_resource(gpio, 0, &res)) {
156 + printk(KERN_ERR "rbppc_nand_probe: No reg property in GPIO found\n");
159 + info->gpo = ioremap_nocache(res.start, res.end - res.start + 1);
161 + if (!of_address_to_resource(gpio, 1, &res)) {
162 + info->gpi = ioremap_nocache(res.start, res.end - res.start + 1);
164 + info->gpi = info->gpo;
168 + info->gpio_rdy = 1 << (31 - rdy[1]);
169 + info->gpio_nce = 1 << (31 - nce[1]);
170 + info->gpio_cle = 1 << (31 - cle[1]);
171 + info->gpio_ale = 1 << (31 - ale[1]);
172 + info->gpio_ctrls = info->gpio_nce | info->gpio_cle | info->gpio_ale;
174 + nnand = of_find_node_by_name(NULL, "nnand");
176 + printk("rbppc_nand_probe: No nNAND found\n");
179 + if (of_address_to_resource(nnand, 0, &res)) {
180 + printk("rbppc_nand_probe: No reg property in nNAND found\n");
183 + of_node_put(nnand);
184 + info->localbus = ioremap_nocache(res.start, res.end - res.start + 1);
186 + if (of_address_to_resource(pdev->node, 0, &res)) {
187 + printk("rbppc_nand_probe: No reg property found\n");
190 + baddr = ioremap_nocache(res.start, res.end - res.start + 1);
192 + memset(&rnand, 0, sizeof(rnand));
193 + rnand.cmd_ctrl = rbppc_nand_cmd_ctrl;
194 + rnand.dev_ready = rbppc_nand_dev_ready;
195 + rnand.read_buf = rbppc_nand_read_buf;
196 + rnand.IO_ADDR_W = baddr;
197 + rnand.IO_ADDR_R = baddr;
200 + memset(&rmtd, 0, sizeof(rmtd));
201 + rnand.ecc.mode = NAND_ECC_SOFT;
202 + rnand.ecc.layout = &rbppc_nand_oob_16;
203 + rnand.chip_delay = 25;
204 + rnand.options |= NAND_NO_AUTOINCR;
205 + rmtd.priv = &rnand;
206 + rmtd.owner = THIS_MODULE;
208 + if (nand_scan(&rmtd, 1) && nand_scan(&rmtd, 1) && nand_scan(&rmtd, 1) && nand_scan(&rmtd, 1)) {
209 + printk(KERN_ERR "rbppc_nand_probe: RouterBOARD NAND device not found\n");
213 + add_mtd_partitions(&rmtd, rbppc_nand_partition_info, 2);
222 +static struct of_device_id rbppc_nand_ids[] = {
223 + { .name = "nand", },
227 +static struct of_platform_driver rbppc_nand_driver = {
229 + .probe = rbppc_nand_probe,
230 + .match_table = rbppc_nand_ids,
232 + .name = "rbppc-nand",
233 + .owner = THIS_MODULE,
237 +static int __init rbppc_nand_init(void)
239 + return of_register_platform_driver(&rbppc_nand_driver);
242 +static void __exit rbppc_nand_exit(void)
244 + of_unregister_platform_driver(&rbppc_nand_driver);
247 +MODULE_AUTHOR("Mikrotikls SIA");
248 +MODULE_AUTHOR("Noah Fontes");
249 +MODULE_AUTHOR("Michael Guntsche");
250 +MODULE_DESCRIPTION("MikroTik RouterBOARD 600 series NAND driver");
251 +MODULE_LICENSE("GPL");
252 +MODULE_VERSION(DRV_VERSION);
254 +module_init(rbppc_nand_init);
255 +module_exit(rbppc_nand_exit);