plat_nand.c 3.4 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/platnand.h>
19 20 21 22 23 24 25 26 27

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

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

37 38 39 40 41
	if (!pdata) {
		dev_err(&pdev->dev, "platform_nand_data is missing\n");
		return -EINVAL;
	}

42 43 44 45 46
	if (pdata->chip.nr_chips < 1) {
		dev_err(&pdev->dev, "invalid number of chips specified\n");
		return -EINVAL;
	}

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

53
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
54 55 56
	data->io_base = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(data->io_base))
		return PTR_ERR(data->io_base);
57

58
	nand_set_flash_node(&data->chip, pdev->dev.of_node);
59 60
	mtd = nand_to_mtd(&data->chip);
	mtd->dev.parent = &pdev->dev;
61

62 63
	data->chip.legacy.IO_ADDR_R = data->io_base;
	data->chip.legacy.IO_ADDR_W = data->io_base;
64
	data->chip.legacy.cmd_ctrl = pdata->ctrl.cmd_ctrl;
65
	data->chip.legacy.dev_ready = pdata->ctrl.dev_ready;
66
	data->chip.select_chip = pdata->ctrl.select_chip;
67 68
	data->chip.legacy.write_buf = pdata->ctrl.write_buf;
	data->chip.legacy.read_buf = pdata->ctrl.read_buf;
69
	data->chip.legacy.chip_delay = pdata->chip.chip_delay;
70
	data->chip.options |= pdata->chip.options;
71
	data->chip.bbt_options |= pdata->chip.bbt_options;
72 73

	data->chip.ecc.mode = NAND_ECC_SOFT;
74
	data->chip.ecc.algo = NAND_ECC_HAMMING;
75 76 77

	platform_set_drvdata(pdev, data);

78 79
	/* Handle any platform specific setup */
	if (pdata->ctrl.probe) {
80 81
		err = pdata->ctrl.probe(pdev);
		if (err)
82 83 84
			goto out;
	}

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

90
	part_types = pdata->chip.part_probe_types;
91

92
	err = mtd_device_parse_register(mtd, part_types, NULL,
93 94
					pdata->chip.partitions,
					pdata->chip.nr_partitions);
95

96 97
	if (!err)
		return err;
98

99
	nand_release(&data->chip);
100
out:
101 102
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
103
	return err;
104 105 106 107 108
}

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

114
	nand_release(&data->chip);
115 116
	if (pdata->ctrl.remove)
		pdata->ctrl.remove(pdev);
117 118 119 120

	return 0;
}

121 122 123 124 125 126
static const struct of_device_id plat_nand_match[] = {
	{ .compatible = "gen_nand" },
	{},
};
MODULE_DEVICE_TABLE(of, plat_nand_match);

127
static struct platform_driver plat_nand_driver = {
128
	.probe	= plat_nand_probe,
B
Bill Pemberton 已提交
129
	.remove	= plat_nand_remove,
130 131 132
	.driver	= {
		.name		= "gen_nand",
		.of_match_table = plat_nand_match,
133 134 135
	},
};

136
module_platform_driver(plat_nand_driver);
137 138 139 140

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