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/rawnand.h>
19 20 21 22 23 24 25 26 27 28
#include <linux/mtd/partitions.h>

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

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

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

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

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

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

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

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

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

	platform_set_drvdata(pdev, data);

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

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

91
	part_types = pdata->chip.part_probe_types;
92

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

97 98
	if (!err)
		return err;
99

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

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

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

	return 0;
}

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

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

137
module_platform_driver(plat_nand_driver);
138 139 140 141

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