Merge branch 'for-3.5-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[firefly-linux-kernel-4.4.55.git] / drivers / mtd / nand / plat_nand.c
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
20 struct plat_nand_data {
21         struct nand_chip        chip;
22         struct mtd_info         mtd;
23         void __iomem            *io_base;
24 };
25
26 static const char *part_probe_types[] = { "cmdlinepart", NULL };
27
28 /*
29  * Probe for the NAND device.
30  */
31 static int __devinit plat_nand_probe(struct platform_device *pdev)
32 {
33         struct platform_nand_data *pdata = pdev->dev.platform_data;
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;
39
40         if (pdata->chip.nr_chips < 1) {
41                 dev_err(&pdev->dev, "invalid number of chips specified\n");
42                 return -EINVAL;
43         }
44
45         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
46         if (!res)
47                 return -ENXIO;
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
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));
64         if (data->io_base == NULL) {
65                 dev_err(&pdev->dev, "ioremap failed\n");
66                 err = -EIO;
67                 goto out_release_io;
68         }
69
70         data->chip.priv = &data;
71         data->mtd.priv = &data->chip;
72         data->mtd.owner = THIS_MODULE;
73         data->mtd.name = dev_name(&pdev->dev);
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;
80         data->chip.write_buf = pdata->ctrl.write_buf;
81         data->chip.read_buf = pdata->ctrl.read_buf;
82         data->chip.read_byte = pdata->ctrl.read_byte;
83         data->chip.chip_delay = pdata->chip.chip_delay;
84         data->chip.options |= pdata->chip.options;
85         data->chip.bbt_options |= pdata->chip.bbt_options;
86
87         data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
88         data->chip.ecc.layout = pdata->chip.ecclayout;
89         data->chip.ecc.mode = NAND_ECC_SOFT;
90
91         platform_set_drvdata(pdev, data);
92
93         /* Handle any platform specific setup */
94         if (pdata->ctrl.probe) {
95                 err = pdata->ctrl.probe(pdev);
96                 if (err)
97                         goto out;
98         }
99
100         /* Scan to find existence of the device */
101         if (nand_scan(&data->mtd, pdata->chip.nr_chips)) {
102                 err = -ENXIO;
103                 goto out;
104         }
105
106         part_types = pdata->chip.part_probe_types ? : part_probe_types;
107
108         ppdata.of_node = pdev->dev.of_node;
109         err = mtd_device_parse_register(&data->mtd, part_types, &ppdata,
110                                         pdata->chip.partitions,
111                                         pdata->chip.nr_partitions);
112
113         if (!err)
114                 return err;
115
116         nand_release(&data->mtd);
117 out:
118         if (pdata->ctrl.remove)
119                 pdata->ctrl.remove(pdev);
120         platform_set_drvdata(pdev, NULL);
121         iounmap(data->io_base);
122 out_release_io:
123         release_mem_region(res->start, resource_size(res));
124 out_free:
125         kfree(data);
126         return err;
127 }
128
129 /*
130  * Remove a NAND device.
131  */
132 static int __devexit plat_nand_remove(struct platform_device *pdev)
133 {
134         struct plat_nand_data *data = platform_get_drvdata(pdev);
135         struct platform_nand_data *pdata = pdev->dev.platform_data;
136         struct resource *res;
137
138         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139
140         nand_release(&data->mtd);
141         if (pdata->ctrl.remove)
142                 pdata->ctrl.remove(pdev);
143         iounmap(data->io_base);
144         release_mem_region(res->start, resource_size(res));
145         kfree(data);
146
147         return 0;
148 }
149
150 static const struct of_device_id plat_nand_match[] = {
151         { .compatible = "gen_nand" },
152         {},
153 };
154 MODULE_DEVICE_TABLE(of, plat_nand_match);
155
156 static struct platform_driver plat_nand_driver = {
157         .probe  = plat_nand_probe,
158         .remove = __devexit_p(plat_nand_remove),
159         .driver = {
160                 .name           = "gen_nand",
161                 .owner          = THIS_MODULE,
162                 .of_match_table = plat_nand_match,
163         },
164 };
165
166 module_platform_driver(plat_nand_driver);
167
168 MODULE_LICENSE("GPL");
169 MODULE_AUTHOR("Vitaly Wool");
170 MODULE_DESCRIPTION("Simple generic NAND driver");
171 MODULE_ALIAS("platform:gen_nand");