plat_nand.c 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Generic NAND driver
 *
 * Author: Vitaly Wool <vitalywool@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */

12
#include <linux/err.h>
13 14 15 16 17
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/mtd/mtd.h>
18
#include <linux/mtd/rawnand.h>
19 20 21 22 23 24 25
#include <linux/mtd/partitions.h>

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

26 27 28 29 30 31 32
static int plat_nand_dev_ready(struct mtd_info *mtd)
{
	struct platform_nand_data *pdata = dev_get_platdata(mtd->dev.parent);

	return pdata->ctrl.dev_ready(mtd_to_nand(mtd));
}

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

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

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

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

61
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
62 63 64
	data->io_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(data->io_base))
		return PTR_ERR(data->io_base);
65

66
	nand_set_flash_node(&data->chip, pdev->dev.of_node);
67 68
	mtd = nand_to_mtd(&data->chip);
	mtd->dev.parent = &pdev->dev;
69 70 71

	data->chip.IO_ADDR_R = data->io_base;
	data->chip.IO_ADDR_W = data->io_base;
72
	data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
73 74 75 76

	if (pdata->ctrl.dev_ready)
		data->chip.dev_ready = plat_nand_dev_ready;

77
	data->chip.select_chip = pdata->ctrl.select_chip;
78
	data->chip.write_buf = pdata->ctrl.write_buf;
79
	data->chip.read_buf = pdata->ctrl.read_buf;
80 81
	data->chip.chip_delay = pdata->chip.chip_delay;
	data->chip.options |= pdata->chip.options;
82
	data->chip.bbt_options |= pdata->chip.bbt_options;
83 84

	data->chip.ecc.mode = NAND_ECC_SOFT;
85
	data->chip.ecc.algo = NAND_ECC_HAMMING;
86 87 88

	platform_set_drvdata(pdev, data);

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

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

101
	part_types = pdata->chip.part_probe_types;
102

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

107 108
	if (!err)
		return err;
109

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

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

125
	nand_release(&data->chip);
126 127
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
128 129 130 131

	return 0;
}

132 133 134 135 136 137
static const struct of_device_id plat_nand_match[] = {
	{ .compatible = "gen_nand" },
	{},
};
MODULE_DEVICE_TABLE(of, plat_nand_match);

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

147
module_platform_driver(plat_nand_driver);
148 149 150 151

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