ath79: add Mikrotik rb4xx series drivers
[oweals/openwrt.git] / target / linux / ath79 / files / drivers / mtd / nand / raw / nand_rb4xx.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * NAND driver for the MikroTik RouterBoard 4xx series
4  *
5  * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
6  * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
7  * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
8  * Copyright (C) 2020 Christopher Hill <ch6574@gmail.com>
9  *
10  * This file was based on the driver for Linux 2.6.22 published by
11  * MikroTik for their RouterBoard 4xx series devices.
12  *
13  * N.B. driver probe reports "DMA mask not set" warnings which are
14  * an artifact of using a platform_driver as an MFD device child.
15  * See conversation here https://lkml.org/lkml/2020/4/28/675
16  */
17 #include <linux/platform_device.h>
18 #include <linux/mtd/rawnand.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/module.h>
21 #include <linux/of_device.h>
22 #include <linux/version.h>
23
24 #include <mfd/rb4xx-cpld.h>
25
26 struct rb4xx_nand {
27         struct rb4xx_cpld *cpld;
28         struct device *dev;
29
30         struct nand_chip chip;
31         struct gpio_desc *ale;
32         struct gpio_desc *cle;
33         struct gpio_desc *nce;
34         struct gpio_desc *rdy;
35 };
36
37 static int rb4xx_ooblayout_ecc(struct mtd_info *mtd, int section,
38                                struct mtd_oob_region *oobregion)
39 {
40         switch (section) {
41         case 0:
42                 oobregion->offset = 8;
43                 oobregion->length = 3;
44                 return 0;
45         case 1:
46                 oobregion->offset = 13;
47                 oobregion->length = 3;
48                 return 0;
49         default:
50                 return -ERANGE;
51         }
52 }
53
54 static int rb4xx_ooblayout_free(struct mtd_info *mtd, int section,
55                                 struct mtd_oob_region *oobregion)
56 {
57         switch (section) {
58         case 0:
59                 oobregion->offset = 0;
60                 oobregion->length = 4;
61                 return 0;
62         case 1:
63                 oobregion->offset = 4;
64                 oobregion->length = 1;
65                 return 0;
66         case 2:
67                 oobregion->offset = 6;
68                 oobregion->length = 2;
69                 return 0;
70         case 3:
71                 oobregion->offset = 11;
72                 oobregion->length = 2;
73                 return 0;
74         default:
75                 return -ERANGE;
76         }
77 }
78
79 static const struct mtd_ooblayout_ops rb4xx_nand_ecclayout_ops = {
80         .ecc = rb4xx_ooblayout_ecc,
81         .free = rb4xx_ooblayout_free,
82 };
83
84 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
85 static uint8_t rb4xx_nand_read_byte(struct mtd_info *mtd)
86 {
87         struct rb4xx_nand *nand = mtd->priv;
88 #else
89 static u8 rb4xx_nand_read_byte(struct nand_chip *chip)
90 {
91         struct rb4xx_nand *nand = chip->priv;
92 #endif
93         struct rb4xx_cpld *cpld = nand->cpld;
94         u8 data;
95         int ret;
96
97         ret = cpld->read_nand(cpld, &data, 1);
98         if (unlikely(ret))
99                 return 0xff;
100
101         return data;
102 }
103
104 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
105 static void rb4xx_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
106                                  int len)
107 {
108         struct rb4xx_nand *nand = mtd->priv;
109 #else
110 static void rb4xx_nand_write_buf(struct nand_chip *chip, const u8 *buf, int len)
111 {
112         struct rb4xx_nand *nand = chip->priv;
113 #endif
114         struct rb4xx_cpld *cpld = nand->cpld;
115
116         cpld->write_nand(cpld, buf, len);
117 }
118
119 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
120 static void rb4xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
121 {
122         struct rb4xx_nand *nand = mtd->priv;
123 #else
124 static void rb4xx_nand_read_buf(struct nand_chip *chip, u8 *buf, int len)
125 {
126         struct rb4xx_nand *nand = chip->priv;
127 #endif
128         struct rb4xx_cpld *cpld = nand->cpld;
129
130         cpld->read_nand(cpld, buf, len);
131 }
132
133 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
134 static void rb4xx_nand_cmd_ctrl(struct mtd_info *mtd, int dat, unsigned int ctrl)
135 {
136         struct rb4xx_nand *nand = mtd->priv;
137 #else
138 static void rb4xx_nand_cmd_ctrl(struct nand_chip *chip, int dat,
139                                 unsigned int ctrl)
140 {
141         struct rb4xx_nand *nand = chip->priv;
142 #endif
143         struct rb4xx_cpld *cpld = nand->cpld;
144         u8 data = dat;
145
146         if (ctrl & NAND_CTRL_CHANGE) {
147                 gpiod_set_value_cansleep(nand->cle, !!(ctrl & NAND_CLE));
148                 gpiod_set_value_cansleep(nand->ale, !!(ctrl & NAND_ALE));
149                 gpiod_set_value_cansleep(nand->nce,  !(ctrl & NAND_NCE));
150         }
151
152         if (dat != NAND_CMD_NONE)
153                 cpld->write_nand(cpld, &data, 1);
154 }
155
156 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
157 static int rb4xx_nand_dev_ready(struct mtd_info *mtd)
158 {
159         struct rb4xx_nand *nand = mtd->priv;
160 #else
161 static int rb4xx_nand_dev_ready(struct nand_chip *chip)
162 {
163         struct rb4xx_nand *nand = chip->priv;
164 #endif
165
166         return gpiod_get_value_cansleep(nand->rdy);
167 }
168
169 static int rb4xx_nand_probe(struct platform_device *pdev)
170 {
171         struct device *dev = &pdev->dev;
172         struct device *parent = dev->parent;
173         struct rb4xx_nand *nand;
174         struct mtd_info *mtd;
175         int ret;
176
177         if (!parent)
178                 return -ENODEV;
179
180         nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
181         if (!nand)
182                 return -ENOMEM;
183
184         platform_set_drvdata(pdev, nand);
185         nand->cpld      = dev_get_drvdata(parent);
186         nand->dev       = dev;
187
188         nand->ale = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
189         if (IS_ERR(nand->ale))
190                 dev_err(dev, "missing gpio ALE: %ld\n", PTR_ERR(nand->ale));
191
192         nand->cle = devm_gpiod_get_index(dev, NULL, 1, GPIOD_OUT_LOW);
193         if (IS_ERR(nand->cle))
194                 dev_err(dev, "missing gpio CLE: %ld\n", PTR_ERR(nand->cle));
195
196         nand->nce = devm_gpiod_get_index(dev, NULL, 2, GPIOD_OUT_LOW);
197         if (IS_ERR(nand->nce))
198                 dev_err(dev, "missing gpio nCE: %ld\n", PTR_ERR(nand->nce));
199
200         nand->rdy = devm_gpiod_get_index(dev, NULL, 3, GPIOD_IN);
201         if (IS_ERR(nand->rdy))
202                 dev_err(dev, "missing gpio RDY: %ld\n", PTR_ERR(nand->rdy));
203
204         if (IS_ERR(nand->ale) || IS_ERR(nand->cle) ||
205             IS_ERR(nand->nce) || IS_ERR(nand->rdy))
206                 return -ENOENT;
207
208         gpiod_set_consumer_name(nand->ale, "mikrotik:nand:ALE");
209         gpiod_set_consumer_name(nand->cle, "mikrotik:nand:CLE");
210         gpiod_set_consumer_name(nand->nce, "mikrotik:nand:nCE");
211         gpiod_set_consumer_name(nand->rdy, "mikrotik:nand:RDY");
212
213         mtd = nand_to_mtd(&nand->chip);
214         mtd->priv       = nand;
215         mtd->owner      = THIS_MODULE;
216         mtd->dev.parent = dev;
217         mtd_set_of_node(mtd, dev->of_node);
218
219         if (mtd->writesize == 512)
220                 mtd_set_ooblayout(mtd, &rb4xx_nand_ecclayout_ops);
221
222         nand->chip.ecc.mode     = NAND_ECC_SOFT;
223         nand->chip.ecc.algo     = NAND_ECC_HAMMING;
224         nand->chip.options      = NAND_NO_SUBPAGE_WRITE;
225         nand->chip.priv         = nand;
226
227 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
228         nand->chip.read_byte    = rb4xx_nand_read_byte;
229         nand->chip.write_buf    = rb4xx_nand_write_buf;
230         nand->chip.read_buf     = rb4xx_nand_read_buf;
231         nand->chip.cmd_ctrl     = rb4xx_nand_cmd_ctrl;
232         nand->chip.dev_ready    = rb4xx_nand_dev_ready;
233         nand->chip.chip_delay   = 25;
234
235         ret = nand_scan(mtd, 1);
236 #else
237         nand->chip.legacy.read_byte     = rb4xx_nand_read_byte;
238         nand->chip.legacy.write_buf     = rb4xx_nand_write_buf;
239         nand->chip.legacy.read_buf      = rb4xx_nand_read_buf;
240         nand->chip.legacy.cmd_ctrl      = rb4xx_nand_cmd_ctrl;
241         nand->chip.legacy.dev_ready     = rb4xx_nand_dev_ready;
242         nand->chip.legacy.chip_delay    = 25;
243
244         ret = nand_scan(&nand->chip, 1);
245 #endif
246         if (ret)
247                 return -ENXIO;
248
249         ret = mtd_device_register(mtd, NULL, 0);
250         if (ret) {
251 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
252                 nand_release(mtd);
253 #else
254                 nand_release(&nand->chip);
255 #endif
256                 return ret;
257         }
258
259         return 0;
260 }
261
262 static int rb4xx_nand_remove(struct platform_device *pdev)
263 {
264         struct rb4xx_nand *nand = platform_get_drvdata(pdev);
265
266 #if LINUX_VERSION_CODE < KERNEL_VERSION(4,20,0)
267         nand_release(nand_to_mtd(&nand->chip));
268 #else
269         nand_release(&nand->chip);
270 #endif
271
272         return 0;
273 }
274
275 static const struct platform_device_id rb4xx_nand_id_table[] = {
276         { "mikrotik,rb4xx-nand", },
277         { },
278 };
279 MODULE_DEVICE_TABLE(platform, rb4xx_nand_id_table);
280
281 static struct platform_driver rb4xx_nand_driver = {
282         .probe = rb4xx_nand_probe,
283         .remove = rb4xx_nand_remove,
284         .id_table = rb4xx_nand_id_table,
285         .driver = {
286                 .name = "rb4xx-nand",
287         },
288 };
289
290 module_platform_driver(rb4xx_nand_driver);
291
292 MODULE_DESCRIPTION("Mikrotik RB4xx NAND driver");
293 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
294 MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
295 MODULE_AUTHOR("Bert Vermeulen <bert@biot.com>");
296 MODULE_AUTHOR("Christopher Hill <ch6574@gmail.com");
297 MODULE_LICENSE("GPL v2");