plat_nand.c 3.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7
/*
 * Generic NAND driver
 *
 * Author: Vitaly Wool <vitalywool@gmail.com>
 */

8
#include <linux/err.h>
9 10 11 12 13
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/mtd/mtd.h>
14
#include <linux/mtd/platnand.h>
15 16

struct plat_nand_data {
17
	struct nand_controller	controller;
18 19 20 21
	struct nand_chip	chip;
	void __iomem		*io_base;
};

22 23 24 25 26 27 28 29 30 31 32 33
static int plat_nand_attach_chip(struct nand_chip *chip)
{
	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
	chip->ecc.algo = NAND_ECC_ALGO_HAMMING;

	return 0;
}

static const struct nand_controller_ops plat_nand_ops = {
	.attach_chip = plat_nand_attach_chip,
};

34 35 36
/*
 * Probe for the NAND device.
 */
B
Bill Pemberton 已提交
37
static int plat_nand_probe(struct platform_device *pdev)
38
{
J
Jingoo Han 已提交
39
	struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
40
	struct plat_nand_data *data;
41
	struct mtd_info *mtd;
42
	struct resource *res;
43
	const char **part_types;
44 45
	int err = 0;

46 47 48 49 50
	if (!pdata) {
		dev_err(&pdev->dev, "platform_nand_data is missing\n");
		return -EINVAL;
	}

51 52 53 54 55
	if (pdata->chip.nr_chips < 1) {
		dev_err(&pdev->dev, "invalid number of chips specified\n");
		return -EINVAL;
	}

56
	/* Allocate memory for the device structure (and zero it) */
57 58
	data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
			    GFP_KERNEL);
59
	if (!data)
60 61
		return -ENOMEM;

62 63 64 65
	data->controller.ops = &plat_nand_ops;
	nand_controller_init(&data->controller);
	data->chip.controller = &data->controller;

66
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
67 68 69
	data->io_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(data->io_base))
		return PTR_ERR(data->io_base);
70

71
	nand_set_flash_node(&data->chip, pdev->dev.of_node);
72 73
	mtd = nand_to_mtd(&data->chip);
	mtd->dev.parent = &pdev->dev;
74

75 76
	data->chip.legacy.IO_ADDR_R = data->io_base;
	data->chip.legacy.IO_ADDR_W = data->io_base;
77
	data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
78
	data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
79
	data->chip.legacy.select_chip = pdata->ctrl.select_chip;
80 81
	data->chip.legacy.write_buf = pdata->ctrl.write_buf;
	data->chip.legacy.read_buf = pdata->ctrl.read_buf;
82
	data->chip.legacy.chip_delay = pdata->chip.chip_delay;
83
	data->chip.options |= pdata->chip.options;
84
	data->chip.bbt_options |= pdata->chip.bbt_options;
85 86 87

	platform_set_drvdata(pdev, data);

88 89
	/* Handle any platform specific setup */
	if (pdata->ctrl.probe) {
90 91
		err = pdata->ctrl.probe(pdev);
		if (err)
92 93 94
			goto out;
	}

L
Lucas De Marchi 已提交
95
	/* Scan to find existence of the device */
96
	err = nand_scan(&data->chip, pdata->chip.nr_chips);
97
	if (err)
98 99
		goto out;

100
	part_types = pdata->chip.part_probe_types;
101

102
	err = mtd_device_parse_register(mtd, part_types, NULL,
103 104
					pdata->chip.partitions,
					pdata->chip.nr_partitions);
105

106 107
	if (!err)
		return err;
108

109
	nand_cleanup(&data->chip);
110
out:
111 112
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
113
	return err;
114 115 116 117 118
}

/*
 * Remove a NAND device.
 */
B
Bill Pemberton 已提交
119
static int plat_nand_remove(struct platform_device *pdev)
120 121
{
	struct plat_nand_data *data = platform_get_drvdata(pdev);
J
Jingoo Han 已提交
122
	struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
123 124
	struct nand_chip *chip = &data->chip;
	int ret;
125

126 127 128
	ret = mtd_device_unregister(nand_to_mtd(chip));
	WARN_ON(ret);
	nand_cleanup(chip);
129 130
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
131 132 133 134

	return 0;
}

135 136 137 138 139 140
static const struct of_device_id plat_nand_match[] = {
	{ .compatible = "gen_nand" },
	{},
};
MODULE_DEVICE_TABLE(of, plat_nand_match);

141
static struct platform_driver plat_nand_driver = {
142
	.probe	= plat_nand_probe,
B
Bill Pemberton 已提交
143
	.remove	= plat_nand_remove,
144 145 146
	.driver	= {
		.name		= "gen_nand",
		.of_match_table = plat_nand_match,
147 148 149
	},
};

150
module_platform_driver(plat_nand_driver);
151 152 153 154

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Vitaly Wool");
MODULE_DESCRIPTION("Simple generic NAND driver");
155
MODULE_ALIAS("platform:gen_nand");