summaryrefslogtreecommitdiffstats
path: root/target/linux/ath79/files/drivers/mtd/nand/raw/nand_rb4xx.c
blob: 22e2660b38f91ca280debb78c4cfd10f944fdd72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// SPDX-License-Identifier: GPL-2.0-only
/*
 * NAND driver for the MikroTik RouterBoard 4xx series
 *
 * Copyright (C) 2008-2011 Gabor Juhos <juhosg@openwrt.org>
 * Copyright (C) 2008 Imre Kaloz <kaloz@openwrt.org>
 * Copyright (C) 2015 Bert Vermeulen <bert@biot.com>
 * Copyright (C) 2020 Christopher Hill <ch6574@gmail.com>
 *
 * This file was based on the driver for Linux 2.6.22 published by
 * MikroTik for their RouterBoard 4xx series devices.
 *
 * N.B. driver probe reports "DMA mask not set" warnings which are
 * an artifact of using a platform_driver as an MFD device child.
 * See conversation here https://lkml.org/lkml/2020/4/28/675
 */
#include <linux/platform_device.h>
#include <linux/mtd/rawnand.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/version.h>

#include <mfd/rb4xx-cpld.h>

struct rb4xx_nand {
	struct rb4xx_cpld *cpld;
	struct device *dev;

	struct nand_chip chip;
	struct gpio_desc *ale;
	struct gpio_desc *cle;
	struct gpio_desc *nce;
	struct gpio_desc *rdy;
};

static int rb4xx_ooblayout_ecc(struct mtd_info *mtd, int section,
			       struct mtd_oob_region *oobregion)
{
	switch (section) {
	case 0:
		oobregion->offset = 8;
		oobregion->length = 3;
		return 0;
	case 1:
		oobregion->offset = 13;
		oobregion->length = 3;
		return 0;
	default:
		return -ERANGE;
	}
}

static int rb4xx_ooblayout_free(struct mtd_info *mtd, int section,
				struct mtd_oob_region *oobregion)
{
	switch (section) {
	case 0:
		oobregion->offset = 0;
		oobregion->length = 4;
		return 0;
	case 1:
		oobregion->offset = 4;
		oobregion->length = 1;
		return 0;
	case 2:
		oobregion->offset = 6;
		oobregion->length = 2;
		return 0;
	case 3:
		oobregion->offset = 11;
		oobregion->length = 2;
		return 0;
	default:
		return -ERANGE;
	}
}

static const struct mtd_ooblayout_ops rb4xx_nand_ecclayout_ops = {
	.ecc = rb4xx_ooblayout_ecc,
	.free = rb4xx_ooblayout_free,
};

static u8 rb4xx_nand_read_byte(struct nand_chip *chip)
{
	struct rb4xx_nand *nand = chip->priv;
	struct rb4xx_cpld *cpld = nand->cpld;
	u8 data;
	int ret;

	ret = cpld->read_nand(cpld, &data, 1);
	if (unlikely(ret))
		return 0xff;

	return data;
}

static void rb4xx_nand_write_buf(struct nand_chip *chip, const u8 *buf, int len)
{
	struct rb4xx_nand *nand = chip->priv;
	struct rb4xx_cpld *cpld = nand->cpld;

	cpld->write_nand(cpld, buf, len);
}

static void rb4xx_nand_read_buf(struct nand_chip *chip, u8 *buf, int len)
{
	struct rb4xx_nand *nand = chip->priv;
	struct rb4xx_cpld *cpld = nand->cpld;

	cpld->read_nand(cpld, buf, len);
}

static void rb4xx_nand_cmd_ctrl(struct nand_chip *chip, int dat,
				unsigned int ctrl)
{
	struct rb4xx_nand *nand = chip->priv;
	struct rb4xx_cpld *cpld = nand->cpld;
	u8 data = dat;

	if (ctrl & NAND_CTRL_CHANGE) {
		gpiod_set_value_cansleep(nand->cle, !!(ctrl & NAND_CLE));
		gpiod_set_value_cansleep(nand->ale, !!(ctrl & NAND_ALE));
		gpiod_set_value_cansleep(nand->nce,  !(ctrl & NAND_NCE));
	}

	if (dat != NAND_CMD_NONE)
		cpld->write_nand(cpld, &data, 1);
}

static int rb4xx_nand_dev_ready(struct nand_chip *chip)
{
	struct rb4xx_nand *nand = chip->priv;

	return gpiod_get_value_cansleep(nand->rdy);
}

static int rb4xx_nand_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device *parent = dev->parent;
	struct rb4xx_nand *nand;
	struct mtd_info *mtd;
	int ret;

	if (!parent)
		return -ENODEV;

	nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
	if (!nand)
		return -ENOMEM;

	platform_set_drvdata(pdev, nand);
	nand->cpld	= dev_get_drvdata(parent);
	nand->dev	= dev;

	nand->ale = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW);
	if (IS_ERR(nand->ale))
		dev_err(dev, "missing gpio ALE: %ld\n", PTR_ERR(nand->ale));

	nand->cle = devm_gpiod_get_index(dev, NULL, 1, GPIOD_OUT_LOW);
	if (IS_ERR(nand->cle))
		dev_err(dev, "missing gpio CLE: %ld\n", PTR_ERR(nand->cle));

	nand->nce = devm_gpiod_get_index(dev, NULL, 2, GPIOD_OUT_LOW);
	if (IS_ERR(nand->nce))
		dev_err(dev, "missing gpio nCE: %ld\n", PTR_ERR(nand->nce));

	nand->rdy = devm_gpiod_get_index(dev, NULL, 3, GPIOD_IN);
	if (IS_ERR(nand->rdy))
		dev_err(dev, "missing gpio RDY: %ld\n", PTR_ERR(nand->rdy));

	if (IS_ERR(nand->ale) || IS_ERR(nand->cle) ||
	    IS_ERR(nand->nce) || IS_ERR(nand->rdy))
		return -ENOENT;

	gpiod_set_consumer_name(nand->ale, "mikrotik:nand:ALE");
	gpiod_set_consumer_name(nand->cle, "mikrotik:nand:CLE");
	gpiod_set_consumer_name(nand->nce, "mikrotik:nand:nCE");
	gpiod_set_consumer_name(nand->rdy, "mikrotik:nand:RDY");

	mtd = nand_to_mtd(&nand->chip);
	mtd->priv	= nand;
	mtd->owner	= THIS_MODULE;
	mtd->dev.parent	= dev;
	mtd_set_of_node(mtd, dev->of_node);

	if (mtd->writesize == 512)
		mtd_set_ooblayout(mtd, &rb4xx_nand_ecclayout_ops);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,9,0)
	nand->chip.ecc.engine_type	= NAND_ECC_ENGINE_TYPE_SOFT;
	nand->chip.ecc.algo		= NAND_ECC_ALGO_HAMMING;
#else
	nand->chip.ecc.mode		= NAND_ECC_SOFT;
	nand->chip.ecc.algo		= NAND_ECC_HAMMING;
#endif
	nand->chip.options		= NAND_NO_SUBPAGE_WRITE;
	nand->chip.priv			= nand;

	nand->chip.legacy.read_byte	= rb4xx_nand_read_byte;
	nand->chip.legacy.write_buf	= rb4xx_nand_write_buf;
	nand->chip.legacy.read_buf	= rb4xx_nand_read_buf;
	nand->chip.legacy.cmd_ctrl	= rb4xx_nand_cmd_ctrl;
	nand->chip.legacy.dev_ready	= rb4xx_nand_dev_ready;
	nand->chip.legacy.chip_delay	= 25;

	ret = nand_scan(&nand->chip, 1);
	if (ret)
		return -ENXIO;

	ret = mtd_device_register(mtd, NULL, 0);
	if (ret) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,8,0)
		mtd_device_unregister(nand_to_mtd(&nand->chip));
		nand_cleanup(&nand->chip);
#else
		nand_release(&nand->chip);
#endif
		return ret;
	}

	return 0;
}

static int rb4xx_nand_remove(struct platform_device *pdev)
{
	struct rb4xx_nand *nand = platform_get_drvdata(pdev);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,8,0)
		mtd_device_unregister(nand_to_mtd(&nand->chip));
		nand_cleanup(&nand->chip);
#else
		nand_release(&nand->chip);
#endif

	return 0;
}

static const struct platform_device_id rb4xx_nand_id_table[] = {
	{ "mikrotik,rb4xx-nand", },
	{ },
};
MODULE_DEVICE_TABLE(platform, rb4xx_nand_id_table);

static struct platform_driver rb4xx_nand_driver = {
	.probe = rb4xx_nand_probe,
	.remove = rb4xx_nand_remove,
	.id_table = rb4xx_nand_id_table,
	.driver = {
		.name = "rb4xx-nand",
	},
};

module_platform_driver(rb4xx_nand_driver);

MODULE_DESCRIPTION("Mikrotik RB4xx NAND driver");
MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
MODULE_AUTHOR("Imre Kaloz <kaloz@openwrt.org>");
MODULE_AUTHOR("Bert Vermeulen <bert@biot.com>");
MODULE_AUTHOR("Christopher Hill <ch6574@gmail.com");
MODULE_LICENSE("GPL v2");