18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Overview:
48c2ecf20Sopenharmony_ci *   Platform independent driver for NDFC (NanD Flash Controller)
58c2ecf20Sopenharmony_ci *   integrated into EP440 cores
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *   Ported to an OF platform driver by Sean MacLennan
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *   The NDFC supports multiple chips, but this driver only supports a
108c2ecf20Sopenharmony_ci *   single chip since I do not have access to any boards with
118c2ecf20Sopenharmony_ci *   multiple chips.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci *  Author: Thomas Gleixner
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci *  Copyright 2006 IBM
168c2ecf20Sopenharmony_ci *  Copyright 2008 PIKA Technologies
178c2ecf20Sopenharmony_ci *    Sean MacLennan <smaclennan@pikatech.com>
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_ci#include <linux/module.h>
208c2ecf20Sopenharmony_ci#include <linux/mtd/rawnand.h>
218c2ecf20Sopenharmony_ci#include <linux/mtd/nand_ecc.h>
228c2ecf20Sopenharmony_ci#include <linux/mtd/partitions.h>
238c2ecf20Sopenharmony_ci#include <linux/mtd/ndfc.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h>
268c2ecf20Sopenharmony_ci#include <linux/of_address.h>
278c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
288c2ecf20Sopenharmony_ci#include <asm/io.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define NDFC_MAX_CS    4
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_cistruct ndfc_controller {
338c2ecf20Sopenharmony_ci	struct platform_device *ofdev;
348c2ecf20Sopenharmony_ci	void __iomem *ndfcbase;
358c2ecf20Sopenharmony_ci	struct nand_chip chip;
368c2ecf20Sopenharmony_ci	int chip_select;
378c2ecf20Sopenharmony_ci	struct nand_controller ndfc_control;
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic struct ndfc_controller ndfc_ctrl[NDFC_MAX_CS];
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic void ndfc_select_chip(struct nand_chip *nchip, int chip)
438c2ecf20Sopenharmony_ci{
448c2ecf20Sopenharmony_ci	uint32_t ccr;
458c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc = nand_get_controller_data(nchip);
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
488c2ecf20Sopenharmony_ci	if (chip >= 0) {
498c2ecf20Sopenharmony_ci		ccr &= ~NDFC_CCR_BS_MASK;
508c2ecf20Sopenharmony_ci		ccr |= NDFC_CCR_BS(chip + ndfc->chip_select);
518c2ecf20Sopenharmony_ci	} else
528c2ecf20Sopenharmony_ci		ccr |= NDFC_CCR_RESET_CE;
538c2ecf20Sopenharmony_ci	out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
548c2ecf20Sopenharmony_ci}
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic void ndfc_hwcontrol(struct nand_chip *chip, int cmd, unsigned int ctrl)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc = nand_get_controller_data(chip);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (cmd == NAND_CMD_NONE)
618c2ecf20Sopenharmony_ci		return;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	if (ctrl & NAND_CLE)
648c2ecf20Sopenharmony_ci		writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_CMD);
658c2ecf20Sopenharmony_ci	else
668c2ecf20Sopenharmony_ci		writel(cmd & 0xFF, ndfc->ndfcbase + NDFC_ALE);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic int ndfc_ready(struct nand_chip *chip)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc = nand_get_controller_data(chip);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	return in_be32(ndfc->ndfcbase + NDFC_STAT) & NDFC_STAT_IS_READY;
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic void ndfc_enable_hwecc(struct nand_chip *chip, int mode)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	uint32_t ccr;
798c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc = nand_get_controller_data(chip);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	ccr = in_be32(ndfc->ndfcbase + NDFC_CCR);
828c2ecf20Sopenharmony_ci	ccr |= NDFC_CCR_RESET_ECC;
838c2ecf20Sopenharmony_ci	out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
848c2ecf20Sopenharmony_ci	wmb();
858c2ecf20Sopenharmony_ci}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistatic int ndfc_calculate_ecc(struct nand_chip *chip,
888c2ecf20Sopenharmony_ci			      const u_char *dat, u_char *ecc_code)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc = nand_get_controller_data(chip);
918c2ecf20Sopenharmony_ci	uint32_t ecc;
928c2ecf20Sopenharmony_ci	uint8_t *p = (uint8_t *)&ecc;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	wmb();
958c2ecf20Sopenharmony_ci	ecc = in_be32(ndfc->ndfcbase + NDFC_ECC);
968c2ecf20Sopenharmony_ci	/* The NDFC uses Smart Media (SMC) bytes order */
978c2ecf20Sopenharmony_ci	ecc_code[0] = p[1];
988c2ecf20Sopenharmony_ci	ecc_code[1] = p[2];
998c2ecf20Sopenharmony_ci	ecc_code[2] = p[3];
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return 0;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/*
1058c2ecf20Sopenharmony_ci * Speedups for buffer read/write/verify
1068c2ecf20Sopenharmony_ci *
1078c2ecf20Sopenharmony_ci * NDFC allows 32bit read/write of data. So we can speed up the buffer
1088c2ecf20Sopenharmony_ci * functions. No further checking, as nand_base will always read/write
1098c2ecf20Sopenharmony_ci * page aligned.
1108c2ecf20Sopenharmony_ci */
1118c2ecf20Sopenharmony_cistatic void ndfc_read_buf(struct nand_chip *chip, uint8_t *buf, int len)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc = nand_get_controller_data(chip);
1148c2ecf20Sopenharmony_ci	uint32_t *p = (uint32_t *) buf;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	for(;len > 0; len -= 4)
1178c2ecf20Sopenharmony_ci		*p++ = in_be32(ndfc->ndfcbase + NDFC_DATA);
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_cistatic void ndfc_write_buf(struct nand_chip *chip, const uint8_t *buf, int len)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc = nand_get_controller_data(chip);
1238c2ecf20Sopenharmony_ci	uint32_t *p = (uint32_t *) buf;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	for(;len > 0; len -= 4)
1268c2ecf20Sopenharmony_ci		out_be32(ndfc->ndfcbase + NDFC_DATA, *p++);
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/*
1308c2ecf20Sopenharmony_ci * Initialize chip structure
1318c2ecf20Sopenharmony_ci */
1328c2ecf20Sopenharmony_cistatic int ndfc_chip_init(struct ndfc_controller *ndfc,
1338c2ecf20Sopenharmony_ci			  struct device_node *node)
1348c2ecf20Sopenharmony_ci{
1358c2ecf20Sopenharmony_ci	struct device_node *flash_np;
1368c2ecf20Sopenharmony_ci	struct nand_chip *chip = &ndfc->chip;
1378c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
1388c2ecf20Sopenharmony_ci	int ret;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	chip->legacy.IO_ADDR_R = ndfc->ndfcbase + NDFC_DATA;
1418c2ecf20Sopenharmony_ci	chip->legacy.IO_ADDR_W = ndfc->ndfcbase + NDFC_DATA;
1428c2ecf20Sopenharmony_ci	chip->legacy.cmd_ctrl = ndfc_hwcontrol;
1438c2ecf20Sopenharmony_ci	chip->legacy.dev_ready = ndfc_ready;
1448c2ecf20Sopenharmony_ci	chip->legacy.select_chip = ndfc_select_chip;
1458c2ecf20Sopenharmony_ci	chip->legacy.chip_delay = 50;
1468c2ecf20Sopenharmony_ci	chip->controller = &ndfc->ndfc_control;
1478c2ecf20Sopenharmony_ci	chip->legacy.read_buf = ndfc_read_buf;
1488c2ecf20Sopenharmony_ci	chip->legacy.write_buf = ndfc_write_buf;
1498c2ecf20Sopenharmony_ci	chip->ecc.correct = nand_correct_data;
1508c2ecf20Sopenharmony_ci	chip->ecc.hwctl = ndfc_enable_hwecc;
1518c2ecf20Sopenharmony_ci	chip->ecc.calculate = ndfc_calculate_ecc;
1528c2ecf20Sopenharmony_ci	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
1538c2ecf20Sopenharmony_ci	chip->ecc.size = 256;
1548c2ecf20Sopenharmony_ci	chip->ecc.bytes = 3;
1558c2ecf20Sopenharmony_ci	chip->ecc.strength = 1;
1568c2ecf20Sopenharmony_ci	nand_set_controller_data(chip, ndfc);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	mtd->dev.parent = &ndfc->ofdev->dev;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	flash_np = of_get_next_child(node, NULL);
1618c2ecf20Sopenharmony_ci	if (!flash_np)
1628c2ecf20Sopenharmony_ci		return -ENODEV;
1638c2ecf20Sopenharmony_ci	nand_set_flash_node(chip, flash_np);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	mtd->name = kasprintf(GFP_KERNEL, "%s.%pOFn", dev_name(&ndfc->ofdev->dev),
1668c2ecf20Sopenharmony_ci			      flash_np);
1678c2ecf20Sopenharmony_ci	if (!mtd->name) {
1688c2ecf20Sopenharmony_ci		ret = -ENOMEM;
1698c2ecf20Sopenharmony_ci		goto err;
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	ret = nand_scan(chip, 1);
1738c2ecf20Sopenharmony_ci	if (ret)
1748c2ecf20Sopenharmony_ci		goto err;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	ret = mtd_device_register(mtd, NULL, 0);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cierr:
1798c2ecf20Sopenharmony_ci	of_node_put(flash_np);
1808c2ecf20Sopenharmony_ci	if (ret)
1818c2ecf20Sopenharmony_ci		kfree(mtd->name);
1828c2ecf20Sopenharmony_ci	return ret;
1838c2ecf20Sopenharmony_ci}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_cistatic int ndfc_probe(struct platform_device *ofdev)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc;
1888c2ecf20Sopenharmony_ci	const __be32 *reg;
1898c2ecf20Sopenharmony_ci	u32 ccr;
1908c2ecf20Sopenharmony_ci	u32 cs;
1918c2ecf20Sopenharmony_ci	int err, len;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	/* Read the reg property to get the chip select */
1948c2ecf20Sopenharmony_ci	reg = of_get_property(ofdev->dev.of_node, "reg", &len);
1958c2ecf20Sopenharmony_ci	if (reg == NULL || len != 12) {
1968c2ecf20Sopenharmony_ci		dev_err(&ofdev->dev, "unable read reg property (%d)\n", len);
1978c2ecf20Sopenharmony_ci		return -ENOENT;
1988c2ecf20Sopenharmony_ci	}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	cs = be32_to_cpu(reg[0]);
2018c2ecf20Sopenharmony_ci	if (cs >= NDFC_MAX_CS) {
2028c2ecf20Sopenharmony_ci		dev_err(&ofdev->dev, "invalid CS number (%d)\n", cs);
2038c2ecf20Sopenharmony_ci		return -EINVAL;
2048c2ecf20Sopenharmony_ci	}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	ndfc = &ndfc_ctrl[cs];
2078c2ecf20Sopenharmony_ci	ndfc->chip_select = cs;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	nand_controller_init(&ndfc->ndfc_control);
2108c2ecf20Sopenharmony_ci	ndfc->ofdev = ofdev;
2118c2ecf20Sopenharmony_ci	dev_set_drvdata(&ofdev->dev, ndfc);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	ndfc->ndfcbase = of_iomap(ofdev->dev.of_node, 0);
2148c2ecf20Sopenharmony_ci	if (!ndfc->ndfcbase) {
2158c2ecf20Sopenharmony_ci		dev_err(&ofdev->dev, "failed to get memory\n");
2168c2ecf20Sopenharmony_ci		return -EIO;
2178c2ecf20Sopenharmony_ci	}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	ccr = NDFC_CCR_BS(ndfc->chip_select);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/* It is ok if ccr does not exist - just default to 0 */
2228c2ecf20Sopenharmony_ci	reg = of_get_property(ofdev->dev.of_node, "ccr", NULL);
2238c2ecf20Sopenharmony_ci	if (reg)
2248c2ecf20Sopenharmony_ci		ccr |= be32_to_cpup(reg);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	out_be32(ndfc->ndfcbase + NDFC_CCR, ccr);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* Set the bank settings if given */
2298c2ecf20Sopenharmony_ci	reg = of_get_property(ofdev->dev.of_node, "bank-settings", NULL);
2308c2ecf20Sopenharmony_ci	if (reg) {
2318c2ecf20Sopenharmony_ci		int offset = NDFC_BCFG0 + (ndfc->chip_select << 2);
2328c2ecf20Sopenharmony_ci		out_be32(ndfc->ndfcbase + offset, be32_to_cpup(reg));
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	err = ndfc_chip_init(ndfc, ofdev->dev.of_node);
2368c2ecf20Sopenharmony_ci	if (err) {
2378c2ecf20Sopenharmony_ci		iounmap(ndfc->ndfcbase);
2388c2ecf20Sopenharmony_ci		return err;
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	return 0;
2428c2ecf20Sopenharmony_ci}
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_cistatic int ndfc_remove(struct platform_device *ofdev)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct ndfc_controller *ndfc = dev_get_drvdata(&ofdev->dev);
2478c2ecf20Sopenharmony_ci	struct nand_chip *chip = &ndfc->chip;
2488c2ecf20Sopenharmony_ci	struct mtd_info *mtd = nand_to_mtd(chip);
2498c2ecf20Sopenharmony_ci	int ret;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	ret = mtd_device_unregister(mtd);
2528c2ecf20Sopenharmony_ci	WARN_ON(ret);
2538c2ecf20Sopenharmony_ci	nand_cleanup(chip);
2548c2ecf20Sopenharmony_ci	kfree(mtd->name);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	return 0;
2578c2ecf20Sopenharmony_ci}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cistatic const struct of_device_id ndfc_match[] = {
2608c2ecf20Sopenharmony_ci	{ .compatible = "ibm,ndfc", },
2618c2ecf20Sopenharmony_ci	{}
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ndfc_match);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic struct platform_driver ndfc_driver = {
2668c2ecf20Sopenharmony_ci	.driver = {
2678c2ecf20Sopenharmony_ci		.name = "ndfc",
2688c2ecf20Sopenharmony_ci		.of_match_table = ndfc_match,
2698c2ecf20Sopenharmony_ci	},
2708c2ecf20Sopenharmony_ci	.probe = ndfc_probe,
2718c2ecf20Sopenharmony_ci	.remove = ndfc_remove,
2728c2ecf20Sopenharmony_ci};
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cimodule_platform_driver(ndfc_driver);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2778c2ecf20Sopenharmony_ciMODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
2788c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("OF Platform driver for NDFC");
279