OF: MTD: make plat_nand loadable from DT
[deliverable/linux.git] / drivers / mtd / nand / plat_nand.c
CommitLineData
711fdf62
VW
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/io.h>
13#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/slab.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mtd/nand.h>
18#include <linux/mtd/partitions.h>
19
20struct plat_nand_data {
21 struct nand_chip chip;
22 struct mtd_info mtd;
23 void __iomem *io_base;
711fdf62
VW
24};
25
f2e5a244
HS
26static const char *part_probe_types[] = { "cmdlinepart", NULL };
27
711fdf62
VW
28/*
29 * Probe for the NAND device.
30 */
9d63287a 31static int __devinit plat_nand_probe(struct platform_device *pdev)
711fdf62
VW
32{
33 struct platform_nand_data *pdata = pdev->dev.platform_data;
a4f20351 34 struct mtd_part_parser_data ppdata;
711fdf62 35 struct plat_nand_data *data;
2d098a72 36 struct resource *res;
f2e5a244 37 const char **part_types;
2d098a72
HS
38 int err = 0;
39
01cd2aba
MV
40 if (pdata->chip.nr_chips < 1) {
41 dev_err(&pdev->dev, "invalid number of chips specified\n");
42 return -EINVAL;
43 }
44
2d098a72
HS
45 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
46 if (!res)
47 return -ENXIO;
711fdf62
VW
48
49 /* Allocate memory for the device structure (and zero it) */
50 data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL);
51 if (!data) {
52 dev_err(&pdev->dev, "failed to allocate device structure.\n");
53 return -ENOMEM;
54 }
55
2d098a72
HS
56 if (!request_mem_region(res->start, resource_size(res),
57 dev_name(&pdev->dev))) {
58 dev_err(&pdev->dev, "request_mem_region failed\n");
59 err = -EBUSY;
60 goto out_free;
61 }
62
63 data->io_base = ioremap(res->start, resource_size(res));
711fdf62
VW
64 if (data->io_base == NULL) {
65 dev_err(&pdev->dev, "ioremap failed\n");
2d098a72
HS
66 err = -EIO;
67 goto out_release_io;
711fdf62
VW
68 }
69
70 data->chip.priv = &data;
71 data->mtd.priv = &data->chip;
72 data->mtd.owner = THIS_MODULE;
475b44c1 73 data->mtd.name = dev_name(&pdev->dev);
711fdf62
VW
74
75 data->chip.IO_ADDR_R = data->io_base;
76 data->chip.IO_ADDR_W = data->io_base;
77 data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
78 data->chip.dev_ready = pdata->ctrl.dev_ready;
79 data->chip.select_chip = pdata->ctrl.select_chip;
d6fed9e9
AC
80 data->chip.write_buf = pdata->ctrl.write_buf;
81 data->chip.read_buf = pdata->ctrl.read_buf;
711fdf62
VW
82 data->chip.chip_delay = pdata->chip.chip_delay;
83 data->chip.options |= pdata->chip.options;
a40f7341 84 data->chip.bbt_options |= pdata->chip.bbt_options;
711fdf62
VW
85
86 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
87 data->chip.ecc.layout = pdata->chip.ecclayout;
88 data->chip.ecc.mode = NAND_ECC_SOFT;
89
90 platform_set_drvdata(pdev, data);
91
bf95efd4
HS
92 /* Handle any platform specific setup */
93 if (pdata->ctrl.probe) {
2d098a72
HS
94 err = pdata->ctrl.probe(pdev);
95 if (err)
bf95efd4
HS
96 goto out;
97 }
98
25985edc 99 /* Scan to find existence of the device */
81cbb0b1 100 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
2d098a72 101 err = -ENXIO;
711fdf62
VW
102 goto out;
103 }
104
f2e5a244
HS
105 part_types = pdata->chip.part_probe_types ? : part_probe_types;
106
a4f20351
JC
107 ppdata.of_node = pdev->dev.of_node;
108 err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
42d7fbe2
AB
109 pdata->chip.partitions,
110 pdata->chip.nr_partitions);
711fdf62 111
2d098a72
HS
112 if (!err)
113 return err;
711fdf62
VW
114
115 nand_release(&data->mtd);
116out:
bf95efd4
HS
117 if (pdata->ctrl.remove)
118 pdata->ctrl.remove(pdev);
711fdf62
VW
119 platform_set_drvdata(pdev, NULL);
120 iounmap(data->io_base);
2d098a72
HS
121out_release_io:
122 release_mem_region(res->start, resource_size(res));
123out_free:
711fdf62 124 kfree(data);
2d098a72 125 return err;
711fdf62
VW
126}
127
128/*
129 * Remove a NAND device.
130 */
131static int __devexit plat_nand_remove(struct platform_device *pdev)
132{
133 struct plat_nand_data *data = platform_get_drvdata(pdev);
134 struct platform_nand_data *pdata = pdev->dev.platform_data;
2d098a72
HS
135 struct resource *res;
136
137 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
711fdf62
VW
138
139 nand_release(&data->mtd);
bf95efd4
HS
140 if (pdata->ctrl.remove)
141 pdata->ctrl.remove(pdev);
711fdf62 142 iounmap(data->io_base);
2d098a72 143 release_mem_region(res->start, resource_size(res));
711fdf62
VW
144 kfree(data);
145
146 return 0;
147}
148
a4f20351
JC
149static const struct of_device_id plat_nand_match[] = {
150 { .compatible = "gen_nand" },
151 {},
152};
153MODULE_DEVICE_TABLE(of, plat_nand_match);
154
711fdf62 155static struct platform_driver plat_nand_driver = {
a4f20351
JC
156 .probe = plat_nand_probe,
157 .remove = __devexit_p(plat_nand_remove),
158 .driver = {
159 .name = "gen_nand",
160 .owner = THIS_MODULE,
161 .of_match_table = plat_nand_match,
711fdf62
VW
162 },
163};
164
f99640de 165module_platform_driver(plat_nand_driver);
711fdf62
VW
166
167MODULE_LICENSE("GPL");
168MODULE_AUTHOR("Vitaly Wool");
169MODULE_DESCRIPTION("Simple generic NAND driver");
1ff18422 170MODULE_ALIAS("platform:gen_nand");
This page took 0.377769 seconds and 5 git commands to generate.