1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Ingenic JZ47xx NAND driver
4  *
5  * Copyright (c) 2015 Imagination Technologies
6  * Author: Alex Smith <alex.smith@imgtec.com>
7  */
8 
9 #include <linux/delay.h>
10 #include <linux/init.h>
11 #include <linux/io.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/rawnand.h>
22 #include <linux/mtd/partitions.h>
23 
24 #include <linux/jz4780-nemc.h>
25 
26 #include "ingenic_ecc.h"
27 
28 #define DRV_NAME	"ingenic-nand"
29 
30 struct jz_soc_info {
31 	unsigned long data_offset;
32 	unsigned long addr_offset;
33 	unsigned long cmd_offset;
34 	const struct mtd_ooblayout_ops *oob_layout;
35 };
36 
37 struct ingenic_nand_cs {
38 	unsigned int bank;
39 	void __iomem *base;
40 };
41 
42 struct ingenic_nfc {
43 	struct device *dev;
44 	struct ingenic_ecc *ecc;
45 	const struct jz_soc_info *soc_info;
46 	struct nand_controller controller;
47 	unsigned int num_banks;
48 	struct list_head chips;
49 	struct ingenic_nand_cs cs[];
50 };
51 
52 struct ingenic_nand {
53 	struct nand_chip chip;
54 	struct list_head chip_list;
55 
56 	struct gpio_desc *busy_gpio;
57 	struct gpio_desc *wp_gpio;
58 	unsigned int reading: 1;
59 };
60 
61 static inline struct ingenic_nand *to_ingenic_nand(struct mtd_info *mtd)
62 {
63 	return container_of(mtd_to_nand(mtd), struct ingenic_nand, chip);
64 }
65 
66 static inline struct ingenic_nfc *to_ingenic_nfc(struct nand_controller *ctrl)
67 {
68 	return container_of(ctrl, struct ingenic_nfc, controller);
69 }
70 
71 static int qi_lb60_ooblayout_ecc(struct mtd_info *mtd, int section,
72 				 struct mtd_oob_region *oobregion)
73 {
74 	struct nand_chip *chip = mtd_to_nand(mtd);
75 	struct nand_ecc_ctrl *ecc = &chip->ecc;
76 
77 	if (section || !ecc->total)
78 		return -ERANGE;
79 
80 	oobregion->length = ecc->total;
81 	oobregion->offset = 12;
82 
83 	return 0;
84 }
85 
86 static int qi_lb60_ooblayout_free(struct mtd_info *mtd, int section,
87 				  struct mtd_oob_region *oobregion)
88 {
89 	struct nand_chip *chip = mtd_to_nand(mtd);
90 	struct nand_ecc_ctrl *ecc = &chip->ecc;
91 
92 	if (section)
93 		return -ERANGE;
94 
95 	oobregion->length = mtd->oobsize - ecc->total - 12;
96 	oobregion->offset = 12 + ecc->total;
97 
98 	return 0;
99 }
100 
101 static const struct mtd_ooblayout_ops qi_lb60_ooblayout_ops = {
102 	.ecc = qi_lb60_ooblayout_ecc,
103 	.free = qi_lb60_ooblayout_free,
104 };
105 
106 static int jz4725b_ooblayout_ecc(struct mtd_info *mtd, int section,
107 				 struct mtd_oob_region *oobregion)
108 {
109 	struct nand_chip *chip = mtd_to_nand(mtd);
110 	struct nand_ecc_ctrl *ecc = &chip->ecc;
111 
112 	if (section || !ecc->total)
113 		return -ERANGE;
114 
115 	oobregion->length = ecc->total;
116 	oobregion->offset = 3;
117 
118 	return 0;
119 }
120 
121 static int jz4725b_ooblayout_free(struct mtd_info *mtd, int section,
122 				  struct mtd_oob_region *oobregion)
123 {
124 	struct nand_chip *chip = mtd_to_nand(mtd);
125 	struct nand_ecc_ctrl *ecc = &chip->ecc;
126 
127 	if (section)
128 		return -ERANGE;
129 
130 	oobregion->length = mtd->oobsize - ecc->total - 3;
131 	oobregion->offset = 3 + ecc->total;
132 
133 	return 0;
134 }
135 
136 static const struct mtd_ooblayout_ops jz4725b_ooblayout_ops = {
137 	.ecc = jz4725b_ooblayout_ecc,
138 	.free = jz4725b_ooblayout_free,
139 };
140 
141 static void ingenic_nand_ecc_hwctl(struct nand_chip *chip, int mode)
142 {
143 	struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
144 
145 	nand->reading = (mode == NAND_ECC_READ);
146 }
147 
148 static int ingenic_nand_ecc_calculate(struct nand_chip *chip, const u8 *dat,
149 				      u8 *ecc_code)
150 {
151 	struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
152 	struct ingenic_nfc *nfc = to_ingenic_nfc(nand->chip.controller);
153 	struct ingenic_ecc_params params;
154 
155 	/*
156 	 * Don't need to generate the ECC when reading, the ECC engine does it
157 	 * for us as part of decoding/correction.
158 	 */
159 	if (nand->reading)
160 		return 0;
161 
162 	params.size = nand->chip.ecc.size;
163 	params.bytes = nand->chip.ecc.bytes;
164 	params.strength = nand->chip.ecc.strength;
165 
166 	return ingenic_ecc_calculate(nfc->ecc, &params, dat, ecc_code);
167 }
168 
169 static int ingenic_nand_ecc_correct(struct nand_chip *chip, u8 *dat,
170 				    u8 *read_ecc, u8 *calc_ecc)
171 {
172 	struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
173 	struct ingenic_nfc *nfc = to_ingenic_nfc(nand->chip.controller);
174 	struct ingenic_ecc_params params;
175 
176 	params.size = nand->chip.ecc.size;
177 	params.bytes = nand->chip.ecc.bytes;
178 	params.strength = nand->chip.ecc.strength;
179 
180 	return ingenic_ecc_correct(nfc->ecc, &params, dat, read_ecc);
181 }
182 
183 static int ingenic_nand_attach_chip(struct nand_chip *chip)
184 {
185 	struct mtd_info *mtd = nand_to_mtd(chip);
186 	struct ingenic_nfc *nfc = to_ingenic_nfc(chip->controller);
187 	int eccbytes;
188 
189 	if (chip->ecc.strength == 4) {
190 		/* JZ4740 uses 9 bytes of ECC to correct maximum 4 errors */
191 		chip->ecc.bytes = 9;
192 	} else {
193 		chip->ecc.bytes = fls((1 + 8) * chip->ecc.size)	*
194 				  (chip->ecc.strength / 8);
195 	}
196 
197 	switch (chip->ecc.mode) {
198 	case NAND_ECC_HW:
199 		if (!nfc->ecc) {
200 			dev_err(nfc->dev, "HW ECC selected, but ECC controller not found\n");
201 			return -ENODEV;
202 		}
203 
204 		chip->ecc.hwctl = ingenic_nand_ecc_hwctl;
205 		chip->ecc.calculate = ingenic_nand_ecc_calculate;
206 		chip->ecc.correct = ingenic_nand_ecc_correct;
207 		fallthrough;
208 	case NAND_ECC_SOFT:
209 		dev_info(nfc->dev, "using %s (strength %d, size %d, bytes %d)\n",
210 			 (nfc->ecc) ? "hardware ECC" : "software ECC",
211 			 chip->ecc.strength, chip->ecc.size, chip->ecc.bytes);
212 		break;
213 	case NAND_ECC_NONE:
214 		dev_info(nfc->dev, "not using ECC\n");
215 		break;
216 	default:
217 		dev_err(nfc->dev, "ECC mode %d not supported\n",
218 			chip->ecc.mode);
219 		return -EINVAL;
220 	}
221 
222 	/* The NAND core will generate the ECC layout for SW ECC */
223 	if (chip->ecc.mode != NAND_ECC_HW)
224 		return 0;
225 
226 	/* Generate ECC layout. ECC codes are right aligned in the OOB area. */
227 	eccbytes = mtd->writesize / chip->ecc.size * chip->ecc.bytes;
228 
229 	if (eccbytes > mtd->oobsize - 2) {
230 		dev_err(nfc->dev,
231 			"invalid ECC config: required %d ECC bytes, but only %d are available",
232 			eccbytes, mtd->oobsize - 2);
233 		return -EINVAL;
234 	}
235 
236 	/*
237 	 * The generic layout for BBT markers will most likely overlap with our
238 	 * ECC bytes in the OOB, so move the BBT markers outside the OOB area.
239 	 */
240 	if (chip->bbt_options & NAND_BBT_USE_FLASH)
241 		chip->bbt_options |= NAND_BBT_NO_OOB;
242 
243 	/* For legacy reasons we use a different layout on the qi,lb60 board. */
244 	if (of_machine_is_compatible("qi,lb60"))
245 		mtd_set_ooblayout(mtd, &qi_lb60_ooblayout_ops);
246 	else
247 		mtd_set_ooblayout(mtd, nfc->soc_info->oob_layout);
248 
249 	return 0;
250 }
251 
252 static int ingenic_nand_exec_instr(struct nand_chip *chip,
253 				   struct ingenic_nand_cs *cs,
254 				   const struct nand_op_instr *instr)
255 {
256 	struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
257 	struct ingenic_nfc *nfc = to_ingenic_nfc(chip->controller);
258 	unsigned int i;
259 
260 	switch (instr->type) {
261 	case NAND_OP_CMD_INSTR:
262 		writeb(instr->ctx.cmd.opcode,
263 		       cs->base + nfc->soc_info->cmd_offset);
264 		return 0;
265 	case NAND_OP_ADDR_INSTR:
266 		for (i = 0; i < instr->ctx.addr.naddrs; i++)
267 			writeb(instr->ctx.addr.addrs[i],
268 			       cs->base + nfc->soc_info->addr_offset);
269 		return 0;
270 	case NAND_OP_DATA_IN_INSTR:
271 		if (instr->ctx.data.force_8bit ||
272 		    !(chip->options & NAND_BUSWIDTH_16))
273 			ioread8_rep(cs->base + nfc->soc_info->data_offset,
274 				    instr->ctx.data.buf.in,
275 				    instr->ctx.data.len);
276 		else
277 			ioread16_rep(cs->base + nfc->soc_info->data_offset,
278 				     instr->ctx.data.buf.in,
279 				     instr->ctx.data.len);
280 		return 0;
281 	case NAND_OP_DATA_OUT_INSTR:
282 		if (instr->ctx.data.force_8bit ||
283 		    !(chip->options & NAND_BUSWIDTH_16))
284 			iowrite8_rep(cs->base + nfc->soc_info->data_offset,
285 				     instr->ctx.data.buf.out,
286 				     instr->ctx.data.len);
287 		else
288 			iowrite16_rep(cs->base + nfc->soc_info->data_offset,
289 				      instr->ctx.data.buf.out,
290 				      instr->ctx.data.len);
291 		return 0;
292 	case NAND_OP_WAITRDY_INSTR:
293 		if (!nand->busy_gpio)
294 			return nand_soft_waitrdy(chip,
295 						 instr->ctx.waitrdy.timeout_ms);
296 
297 		return nand_gpio_waitrdy(chip, nand->busy_gpio,
298 					 instr->ctx.waitrdy.timeout_ms);
299 	default:
300 		break;
301 	}
302 
303 	return -EINVAL;
304 }
305 
306 static int ingenic_nand_exec_op(struct nand_chip *chip,
307 				const struct nand_operation *op,
308 				bool check_only)
309 {
310 	struct ingenic_nand *nand = to_ingenic_nand(nand_to_mtd(chip));
311 	struct ingenic_nfc *nfc = to_ingenic_nfc(nand->chip.controller);
312 	struct ingenic_nand_cs *cs;
313 	unsigned int i;
314 	int ret = 0;
315 
316 	if (check_only)
317 		return 0;
318 
319 	cs = &nfc->cs[op->cs];
320 	jz4780_nemc_assert(nfc->dev, cs->bank, true);
321 	for (i = 0; i < op->ninstrs; i++) {
322 		ret = ingenic_nand_exec_instr(chip, cs, &op->instrs[i]);
323 		if (ret)
324 			break;
325 
326 		if (op->instrs[i].delay_ns)
327 			ndelay(op->instrs[i].delay_ns);
328 	}
329 	jz4780_nemc_assert(nfc->dev, cs->bank, false);
330 
331 	return ret;
332 }
333 
334 static const struct nand_controller_ops ingenic_nand_controller_ops = {
335 	.attach_chip = ingenic_nand_attach_chip,
336 	.exec_op = ingenic_nand_exec_op,
337 };
338 
339 static int ingenic_nand_init_chip(struct platform_device *pdev,
340 				  struct ingenic_nfc *nfc,
341 				  struct device_node *np,
342 				  unsigned int chipnr)
343 {
344 	struct device *dev = &pdev->dev;
345 	struct ingenic_nand *nand;
346 	struct ingenic_nand_cs *cs;
347 	struct nand_chip *chip;
348 	struct mtd_info *mtd;
349 	const __be32 *reg;
350 	int ret = 0;
351 
352 	cs = &nfc->cs[chipnr];
353 
354 	reg = of_get_property(np, "reg", NULL);
355 	if (!reg)
356 		return -EINVAL;
357 
358 	cs->bank = be32_to_cpu(*reg);
359 
360 	jz4780_nemc_set_type(nfc->dev, cs->bank, JZ4780_NEMC_BANK_NAND);
361 
362 	cs->base = devm_platform_ioremap_resource(pdev, chipnr);
363 	if (IS_ERR(cs->base))
364 		return PTR_ERR(cs->base);
365 
366 	nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
367 	if (!nand)
368 		return -ENOMEM;
369 
370 	nand->busy_gpio = devm_gpiod_get_optional(dev, "rb", GPIOD_IN);
371 
372 	if (IS_ERR(nand->busy_gpio)) {
373 		ret = PTR_ERR(nand->busy_gpio);
374 		dev_err(dev, "failed to request busy GPIO: %d\n", ret);
375 		return ret;
376 	}
377 
378 	/*
379 	 * The rb-gpios semantics was undocumented and qi,lb60 (along with
380 	 * the ingenic driver) got it wrong. The active state encodes the
381 	 * NAND ready state, which is high level. Since there's no signal
382 	 * inverter on this board, it should be active-high. Let's fix that
383 	 * here for older DTs so we can re-use the generic nand_gpio_waitrdy()
384 	 * helper, and be consistent with what other drivers do.
385 	 */
386 	if (of_machine_is_compatible("qi,lb60") &&
387 	    gpiod_is_active_low(nand->busy_gpio))
388 		gpiod_toggle_active_low(nand->busy_gpio);
389 
390 	nand->wp_gpio = devm_gpiod_get_optional(dev, "wp", GPIOD_OUT_LOW);
391 
392 	if (IS_ERR(nand->wp_gpio)) {
393 		ret = PTR_ERR(nand->wp_gpio);
394 		dev_err(dev, "failed to request WP GPIO: %d\n", ret);
395 		return ret;
396 	}
397 
398 	chip = &nand->chip;
399 	mtd = nand_to_mtd(chip);
400 	mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%s.%d", dev_name(dev),
401 				   cs->bank);
402 	if (!mtd->name)
403 		return -ENOMEM;
404 	mtd->dev.parent = dev;
405 
406 	chip->options = NAND_NO_SUBPAGE_WRITE;
407 	chip->ecc.mode = NAND_ECC_HW;
408 	chip->controller = &nfc->controller;
409 	nand_set_flash_node(chip, np);
410 
411 	chip->controller->ops = &ingenic_nand_controller_ops;
412 	ret = nand_scan(chip, 1);
413 	if (ret)
414 		return ret;
415 
416 	ret = mtd_device_register(mtd, NULL, 0);
417 	if (ret) {
418 		nand_cleanup(chip);
419 		return ret;
420 	}
421 
422 	list_add_tail(&nand->chip_list, &nfc->chips);
423 
424 	return 0;
425 }
426 
427 static void ingenic_nand_cleanup_chips(struct ingenic_nfc *nfc)
428 {
429 	struct ingenic_nand *ingenic_chip;
430 	struct nand_chip *chip;
431 	int ret;
432 
433 	while (!list_empty(&nfc->chips)) {
434 		ingenic_chip = list_first_entry(&nfc->chips,
435 						struct ingenic_nand, chip_list);
436 		chip = &ingenic_chip->chip;
437 		ret = mtd_device_unregister(nand_to_mtd(chip));
438 		WARN_ON(ret);
439 		nand_cleanup(chip);
440 		list_del(&ingenic_chip->chip_list);
441 	}
442 }
443 
444 static int ingenic_nand_init_chips(struct ingenic_nfc *nfc,
445 				   struct platform_device *pdev)
446 {
447 	struct device *dev = &pdev->dev;
448 	struct device_node *np;
449 	int i = 0;
450 	int ret;
451 	int num_chips = of_get_child_count(dev->of_node);
452 
453 	if (num_chips > nfc->num_banks) {
454 		dev_err(dev, "found %d chips but only %d banks\n",
455 			num_chips, nfc->num_banks);
456 		return -EINVAL;
457 	}
458 
459 	for_each_child_of_node(dev->of_node, np) {
460 		ret = ingenic_nand_init_chip(pdev, nfc, np, i);
461 		if (ret) {
462 			ingenic_nand_cleanup_chips(nfc);
463 			of_node_put(np);
464 			return ret;
465 		}
466 
467 		i++;
468 	}
469 
470 	return 0;
471 }
472 
473 static int ingenic_nand_probe(struct platform_device *pdev)
474 {
475 	struct device *dev = &pdev->dev;
476 	unsigned int num_banks;
477 	struct ingenic_nfc *nfc;
478 	int ret;
479 
480 	num_banks = jz4780_nemc_num_banks(dev);
481 	if (num_banks == 0) {
482 		dev_err(dev, "no banks found\n");
483 		return -ENODEV;
484 	}
485 
486 	nfc = devm_kzalloc(dev, struct_size(nfc, cs, num_banks), GFP_KERNEL);
487 	if (!nfc)
488 		return -ENOMEM;
489 
490 	nfc->soc_info = device_get_match_data(dev);
491 	if (!nfc->soc_info)
492 		return -EINVAL;
493 
494 	/*
495 	 * Check for ECC HW before we call nand_scan_ident, to prevent us from
496 	 * having to call it again if the ECC driver returns -EPROBE_DEFER.
497 	 */
498 	nfc->ecc = of_ingenic_ecc_get(dev->of_node);
499 	if (IS_ERR(nfc->ecc))
500 		return PTR_ERR(nfc->ecc);
501 
502 	nfc->dev = dev;
503 	nfc->num_banks = num_banks;
504 
505 	nand_controller_init(&nfc->controller);
506 	INIT_LIST_HEAD(&nfc->chips);
507 
508 	ret = ingenic_nand_init_chips(nfc, pdev);
509 	if (ret) {
510 		if (nfc->ecc)
511 			ingenic_ecc_release(nfc->ecc);
512 		return ret;
513 	}
514 
515 	platform_set_drvdata(pdev, nfc);
516 	return 0;
517 }
518 
519 static int ingenic_nand_remove(struct platform_device *pdev)
520 {
521 	struct ingenic_nfc *nfc = platform_get_drvdata(pdev);
522 
523 	if (nfc->ecc)
524 		ingenic_ecc_release(nfc->ecc);
525 
526 	ingenic_nand_cleanup_chips(nfc);
527 
528 	return 0;
529 }
530 
531 static const struct jz_soc_info jz4740_soc_info = {
532 	.data_offset = 0x00000000,
533 	.cmd_offset = 0x00008000,
534 	.addr_offset = 0x00010000,
535 	.oob_layout = &nand_ooblayout_lp_ops,
536 };
537 
538 static const struct jz_soc_info jz4725b_soc_info = {
539 	.data_offset = 0x00000000,
540 	.cmd_offset = 0x00008000,
541 	.addr_offset = 0x00010000,
542 	.oob_layout = &jz4725b_ooblayout_ops,
543 };
544 
545 static const struct jz_soc_info jz4780_soc_info = {
546 	.data_offset = 0x00000000,
547 	.cmd_offset = 0x00400000,
548 	.addr_offset = 0x00800000,
549 	.oob_layout = &nand_ooblayout_lp_ops,
550 };
551 
552 static const struct of_device_id ingenic_nand_dt_match[] = {
553 	{ .compatible = "ingenic,jz4740-nand", .data = &jz4740_soc_info },
554 	{ .compatible = "ingenic,jz4725b-nand", .data = &jz4725b_soc_info },
555 	{ .compatible = "ingenic,jz4780-nand", .data = &jz4780_soc_info },
556 	{},
557 };
558 MODULE_DEVICE_TABLE(of, ingenic_nand_dt_match);
559 
560 static struct platform_driver ingenic_nand_driver = {
561 	.probe		= ingenic_nand_probe,
562 	.remove		= ingenic_nand_remove,
563 	.driver	= {
564 		.name	= DRV_NAME,
565 		.of_match_table = of_match_ptr(ingenic_nand_dt_match),
566 	},
567 };
568 module_platform_driver(ingenic_nand_driver);
569 
570 MODULE_AUTHOR("Alex Smith <alex@alex-smith.me.uk>");
571 MODULE_AUTHOR("Harvey Hunt <harveyhuntnexus@gmail.com>");
572 MODULE_DESCRIPTION("Ingenic JZ47xx NAND driver");
573 MODULE_LICENSE("GPL v2");
574