Linux 3.12.39
[linux/fpc-iii.git] / drivers / mtd / nand / plat_nand.c
blobcad4cdc9df399a70c77640be5a634b837996790d
1 /*
2 * Generic NAND driver
4 * Author: Vitaly Wool <vitalywool@gmail.com>
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.
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>
20 struct plat_nand_data {
21 struct nand_chip chip;
22 struct mtd_info mtd;
23 void __iomem *io_base;
26 static const char *part_probe_types[] = { "cmdlinepart", NULL };
29 * Probe for the NAND device.
31 static int plat_nand_probe(struct platform_device *pdev)
33 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
34 struct mtd_part_parser_data ppdata;
35 struct plat_nand_data *data;
36 struct resource *res;
37 const char **part_types;
38 int err = 0;
40 if (!pdata) {
41 dev_err(&pdev->dev, "platform_nand_data is missing\n");
42 return -EINVAL;
45 if (pdata->chip.nr_chips < 1) {
46 dev_err(&pdev->dev, "invalid number of chips specified\n");
47 return -EINVAL;
50 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
51 if (!res)
52 return -ENXIO;
54 /* Allocate memory for the device structure (and zero it) */
55 data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL);
56 if (!data) {
57 dev_err(&pdev->dev, "failed to allocate device structure.\n");
58 return -ENOMEM;
61 if (!request_mem_region(res->start, resource_size(res),
62 dev_name(&pdev->dev))) {
63 dev_err(&pdev->dev, "request_mem_region failed\n");
64 err = -EBUSY;
65 goto out_free;
68 data->io_base = ioremap(res->start, resource_size(res));
69 if (data->io_base == NULL) {
70 dev_err(&pdev->dev, "ioremap failed\n");
71 err = -EIO;
72 goto out_release_io;
75 data->chip.priv = &data;
76 data->mtd.priv = &data->chip;
77 data->mtd.owner = THIS_MODULE;
78 data->mtd.name = dev_name(&pdev->dev);
80 data->chip.IO_ADDR_R = data->io_base;
81 data->chip.IO_ADDR_W = data->io_base;
82 data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
83 data->chip.dev_ready = pdata->ctrl.dev_ready;
84 data->chip.select_chip = pdata->ctrl.select_chip;
85 data->chip.write_buf = pdata->ctrl.write_buf;
86 data->chip.read_buf = pdata->ctrl.read_buf;
87 data->chip.read_byte = pdata->ctrl.read_byte;
88 data->chip.chip_delay = pdata->chip.chip_delay;
89 data->chip.options |= pdata->chip.options;
90 data->chip.bbt_options |= pdata->chip.bbt_options;
92 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
93 data->chip.ecc.layout = pdata->chip.ecclayout;
94 data->chip.ecc.mode = NAND_ECC_SOFT;
96 platform_set_drvdata(pdev, data);
98 /* Handle any platform specific setup */
99 if (pdata->ctrl.probe) {
100 err = pdata->ctrl.probe(pdev);
101 if (err)
102 goto out;
105 /* Scan to find existence of the device */
106 if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
107 err = -ENXIO;
108 goto out;
111 part_types = pdata->chip.part_probe_types ? : part_probe_types;
113 ppdata.of_node = pdev->dev.of_node;
114 err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
115 pdata->chip.partitions,
116 pdata->chip.nr_partitions);
118 if (!err)
119 return err;
121 nand_release(&data->mtd);
122 out:
123 if (pdata->ctrl.remove)
124 pdata->ctrl.remove(pdev);
125 iounmap(data->io_base);
126 out_release_io:
127 release_mem_region(res->start, resource_size(res));
128 out_free:
129 kfree(data);
130 return err;
134 * Remove a NAND device.
136 static int plat_nand_remove(struct platform_device *pdev)
138 struct plat_nand_data *data = platform_get_drvdata(pdev);
139 struct platform_nand_data *pdata = dev_get_platdata(&pdev->dev);
140 struct resource *res;
142 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
144 nand_release(&data->mtd);
145 if (pdata->ctrl.remove)
146 pdata->ctrl.remove(pdev);
147 iounmap(data->io_base);
148 release_mem_region(res->start, resource_size(res));
149 kfree(data);
151 return 0;
154 static const struct of_device_id plat_nand_match[] = {
155 { .compatible = "gen_nand" },
158 MODULE_DEVICE_TABLE(of, plat_nand_match);
160 static struct platform_driver plat_nand_driver = {
161 .probe = plat_nand_probe,
162 .remove = plat_nand_remove,
163 .driver = {
164 .name = "gen_nand",
165 .owner = THIS_MODULE,
166 .of_match_table = plat_nand_match,
170 module_platform_driver(plat_nand_driver);
172 MODULE_LICENSE("GPL");
173 MODULE_AUTHOR("Vitaly Wool");
174 MODULE_DESCRIPTION("Simple generic NAND driver");
175 MODULE_ALIAS("platform:gen_nand");