plat_nand.c 3.3 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 17 18 19 20 21 22 23

struct plat_nand_data {
	struct nand_chip	chip;
	void __iomem		*io_base;
};

/*
 * Probe for the NAND device.
 */
B
Bill Pemberton 已提交
24
static int plat_nand_probe(struct platform_device *pdev)
25
{
J
Jingoo Han 已提交
26
	struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
27
	struct plat_nand_data *data;
28
	struct mtd_info *mtd;
29
	struct resource *res;
30
	const char **part_types;
31 32
	int err = 0;

33 34 35 36 37
	if (!pdata) {
		dev_err(&pdev->dev, "platform_nand_data is missing\n");
		return -EINVAL;
	}

38 39 40 41 42
	if (pdata->chip.nr_chips < 1) {
		dev_err(&pdev->dev, "invalid number of chips specified\n");
		return -EINVAL;
	}

43
	/* Allocate memory for the device structure (and zero it) */
44 45
	data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
			    GFP_KERNEL);
46
	if (!data)
47 48
		return -ENOMEM;

49
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
50 51 52
	data->io_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(data->io_base))
		return PTR_ERR(data->io_base);
53

54
	nand_set_flash_node(&data->chip, pdev->dev.of_node);
55 56
	mtd = nand_to_mtd(&data->chip);
	mtd->dev.parent = &pdev->dev;
57

58 59
	data->chip.legacy.IO_ADDR_R = data->io_base;
	data->chip.legacy.IO_ADDR_W = data->io_base;
60
	data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
61
	data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
62
	data->chip.legacy.select_chip = pdata->ctrl.select_chip;
63 64
	data->chip.legacy.write_buf = pdata->ctrl.write_buf;
	data->chip.legacy.read_buf = pdata->ctrl.read_buf;
65
	data->chip.legacy.chip_delay = pdata->chip.chip_delay;
66
	data->chip.options |= pdata->chip.options;
67
	data->chip.bbt_options |= pdata->chip.bbt_options;
68 69

	data->chip.ecc.mode = NAND_ECC_SOFT;
70
	data->chip.ecc.algo = NAND_ECC_HAMMING;
71 72 73

	platform_set_drvdata(pdev, data);

74 75
	/* Handle any platform specific setup */
	if (pdata->ctrl.probe) {
76 77
		err = pdata->ctrl.probe(pdev);
		if (err)
78 79 80
			goto out;
	}

L
Lucas De Marchi 已提交
81
	/* Scan to find existence of the device */
82
	err = nand_scan(&data->chip, pdata->chip.nr_chips);
83
	if (err)
84 85
		goto out;

86
	part_types = pdata->chip.part_probe_types;
87

88
	err = mtd_device_parse_register(mtd, part_types, NULL,
89 90
					pdata->chip.partitions,
					pdata->chip.nr_partitions);
91

92 93
	if (!err)
		return err;
94

95
	nand_cleanup(&data->chip);
96
out:
97 98
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
99
	return err;
100 101 102 103 104
}

/*
 * Remove a NAND device.
 */
B
Bill Pemberton 已提交
105
static int plat_nand_remove(struct platform_device *pdev)
106 107
{
	struct plat_nand_data *data = platform_get_drvdata(pdev);
J
Jingoo Han 已提交
108
	struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
109

110
	nand_release(&data->chip);
111 112
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
113 114 115 116

	return 0;
}

117 118 119 120 121 122
static const struct of_device_id plat_nand_match[] = {
	{ .compatible = "gen_nand" },
	{},
};
MODULE_DEVICE_TABLE(of, plat_nand_match);

123
static struct platform_driver plat_nand_driver = {
124
	.probe	= plat_nand_probe,
B
Bill Pemberton 已提交
125
	.remove	= plat_nand_remove,
126 127 128
	.driver	= {
		.name		= "gen_nand",
		.of_match_table = plat_nand_match,
129 130 131
	},
};

132
module_platform_driver(plat_nand_driver);
133 134 135 136

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