1/*
2 * Generic NAND driver
3 *
4 * Author: Vitaly Wool <vitalywool@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 */
11
12#include <linux/err.h>
13#include <linux/io.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/slab.h>
17#include <linux/mtd/mtd.h>
18#include <linux/mtd/nand.h>
19#include <linux/mtd/partitions.h>
20
21struct plat_nand_data {
22	struct nand_chip	chip;
23	struct mtd_info		mtd;
24	void __iomem		*io_base;
25};
26
27static const char *part_probe_types[] = { "cmdlinepart", NULL };
28
29/*
30 * Probe for the NAND device.
31 */
32static int plat_nand_probe(struct platform_device *pdev)
33{
34	struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
35	struct mtd_part_parser_data ppdata;
36	struct plat_nand_data *data;
37	struct resource *res;
38	const char **part_types;
39	int err = 0;
40
41	if (!pdata) {
42		dev_err(&pdev->dev, "platform_nand_data is missing\n");
43		return -EINVAL;
44	}
45
46	if (pdata->chip.nr_chips < 1) {
47		dev_err(&pdev->dev, "invalid number of chips specified\n");
48		return -EINVAL;
49	}
50
51	/* Allocate memory for the device structure (and zero it) */
52	data = devm_kzalloc(&pdev->dev, sizeof(struct plat_nand_data),
53			    GFP_KERNEL);
54	if (!data)
55		return -ENOMEM;
56
57	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
58	data->io_base = devm_ioremap_resource(&pdev->dev, res);
59	if (IS_ERR(data->io_base))
60		return PTR_ERR(data->io_base);
61
62	data->chip.priv = &data;
63	data->mtd.priv = &data->chip;
64	data->mtd.owner = THIS_MODULE;
65	data->mtd.name = dev_name(&pdev->dev);
66
67	data->chip.IO_ADDR_R = data->io_base;
68	data->chip.IO_ADDR_W = data->io_base;
69	data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
70	data->chip.dev_ready = pdata->ctrl.dev_ready;
71	data->chip.select_chip = pdata->ctrl.select_chip;
72	data->chip.write_buf = pdata->ctrl.write_buf;
73	data->chip.read_buf = pdata->ctrl.read_buf;
74	data->chip.read_byte = pdata->ctrl.read_byte;
75	data->chip.chip_delay = pdata->chip.chip_delay;
76	data->chip.options |= pdata->chip.options;
77	data->chip.bbt_options |= pdata->chip.bbt_options;
78
79	data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
80	data->chip.ecc.layout = pdata->chip.ecclayout;
81	data->chip.ecc.mode = NAND_ECC_SOFT;
82
83	platform_set_drvdata(pdev, data);
84
85	/* Handle any platform specific setup */
86	if (pdata->ctrl.probe) {
87		err = pdata->ctrl.probe(pdev);
88		if (err)
89			goto out;
90	}
91
92	/* Scan to find existence of the device */
93	if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
94		err = -ENXIO;
95		goto out;
96	}
97
98	part_types = pdata->chip.part_probe_types ? : part_probe_types;
99
100	ppdata.of_node = pdev->dev.of_node;
101	err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
102					pdata->chip.partitions,
103					pdata->chip.nr_partitions);
104
105	if (!err)
106		return err;
107
108	nand_release(&data->mtd);
109out:
110	if (pdata->ctrl.remove)
111		pdata->ctrl.remove(pdev);
112	return err;
113}
114
115/*
116 * Remove a NAND device.
117 */
118static int plat_nand_remove(struct platform_device *pdev)
119{
120	struct plat_nand_data *data = platform_get_drvdata(pdev);
121	struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
122
123	nand_release(&data->mtd);
124	if (pdata->ctrl.remove)
125		pdata->ctrl.remove(pdev);
126
127	return 0;
128}
129
130static const struct of_device_id plat_nand_match[] = {
131	{ .compatible = "gen_nand" },
132	{},
133};
134MODULE_DEVICE_TABLE(of, plat_nand_match);
135
136static struct platform_driver plat_nand_driver = {
137	.probe	= plat_nand_probe,
138	.remove	= plat_nand_remove,
139	.driver	= {
140		.name		= "gen_nand",
141		.of_match_table = plat_nand_match,
142	},
143};
144
145module_platform_driver(plat_nand_driver);
146
147MODULE_LICENSE("GPL");
148MODULE_AUTHOR("Vitaly Wool");
149MODULE_DESCRIPTION("Simple generic NAND driver");
150MODULE_ALIAS("platform:gen_nand");
151