Merge branch 'bjorn-initcall-cleanup' into release
[linux-2.6] / 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 #ifdef CONFIG_MTD_PARTITIONS
25         int                     nr_parts;
26         struct mtd_partition    *parts;
27 #endif
28 };
29
30 /*
31  * Probe for the NAND device.
32  */
33 static int __init plat_nand_probe(struct platform_device *pdev)
34 {
35         struct platform_nand_data *pdata = pdev->dev.platform_data;
36         struct plat_nand_data *data;
37         int res = 0;
38
39         /* Allocate memory for the device structure (and zero it) */
40         data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL);
41         if (!data) {
42                 dev_err(&pdev->dev, "failed to allocate device structure.\n");
43                 return -ENOMEM;
44         }
45
46         data->io_base = ioremap(pdev->resource[0].start,
47                                 pdev->resource[0].end - pdev->resource[0].start + 1);
48         if (data->io_base == NULL) {
49                 dev_err(&pdev->dev, "ioremap failed\n");
50                 kfree(data);
51                 return -EIO;
52         }
53
54         data->chip.priv = &data;
55         data->mtd.priv = &data->chip;
56         data->mtd.owner = THIS_MODULE;
57         data->mtd.name = dev_name(&pdev->dev);
58
59         data->chip.IO_ADDR_R = data->io_base;
60         data->chip.IO_ADDR_W = data->io_base;
61         data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl;
62         data->chip.dev_ready = pdata->ctrl.dev_ready;
63         data->chip.select_chip = pdata->ctrl.select_chip;
64         data->chip.chip_delay = pdata->chip.chip_delay;
65         data->chip.options |= pdata->chip.options;
66
67         data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
68         data->chip.ecc.layout = pdata->chip.ecclayout;
69         data->chip.ecc.mode = NAND_ECC_SOFT;
70
71         platform_set_drvdata(pdev, data);
72
73         /* Scan to find existance of the device */
74         if (nand_scan(&data->mtd, 1)) {
75                 res = -ENXIO;
76                 goto out;
77         }
78
79 #ifdef CONFIG_MTD_PARTITIONS
80         if (pdata->chip.part_probe_types) {
81                 res = parse_mtd_partitions(&data->mtd,
82                                         pdata->chip.part_probe_types,
83                                         &data->parts, 0);
84                 if (res > 0) {
85                         add_mtd_partitions(&data->mtd, data->parts, res);
86                         return 0;
87                 }
88         }
89         if (pdata->chip.partitions) {
90                 data->parts = pdata->chip.partitions;
91                 res = add_mtd_partitions(&data->mtd, data->parts,
92                         pdata->chip.nr_partitions);
93         } else
94 #endif
95         res = add_mtd_device(&data->mtd);
96
97         if (!res)
98                 return res;
99
100         nand_release(&data->mtd);
101 out:
102         platform_set_drvdata(pdev, NULL);
103         iounmap(data->io_base);
104         kfree(data);
105         return res;
106 }
107
108 /*
109  * Remove a NAND device.
110  */
111 static int __devexit plat_nand_remove(struct platform_device *pdev)
112 {
113         struct plat_nand_data *data = platform_get_drvdata(pdev);
114 #ifdef CONFIG_MTD_PARTITIONS
115         struct platform_nand_data *pdata = pdev->dev.platform_data;
116 #endif
117
118         nand_release(&data->mtd);
119 #ifdef CONFIG_MTD_PARTITIONS
120         if (data->parts && data->parts != pdata->chip.partitions)
121                 kfree(data->parts);
122 #endif
123         iounmap(data->io_base);
124         kfree(data);
125
126         return 0;
127 }
128
129 static struct platform_driver plat_nand_driver = {
130         .probe          = plat_nand_probe,
131         .remove         = plat_nand_remove,
132         .driver         = {
133                 .name   = "gen_nand",
134                 .owner  = THIS_MODULE,
135         },
136 };
137
138 static int __init plat_nand_init(void)
139 {
140         return platform_driver_register(&plat_nand_driver);
141 }
142
143 static void __exit plat_nand_exit(void)
144 {
145         platform_driver_unregister(&plat_nand_driver);
146 }
147
148 module_init(plat_nand_init);
149 module_exit(plat_nand_exit);
150
151 MODULE_LICENSE("GPL");
152 MODULE_AUTHOR("Vitaly Wool");
153 MODULE_DESCRIPTION("Simple generic NAND driver");
154 MODULE_ALIAS("platform:gen_nand");