blob: 4e16c6f5bdd5a7642b4fff683e2e05e2aadf7267 [file] [log] [blame]
Vitaly Wool711fdf62007-05-06 19:31:18 +04001/*
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;
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 */
Uwe Kleine-König9d632872009-03-28 00:26:58 +010033static int __devinit plat_nand_probe(struct platform_device *pdev)
Vitaly Wool711fdf62007-05-06 19:31:18 +040034{
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;
Kay Sievers475b44c2009-01-06 10:44:38 -080057 data->mtd.name = dev_name(&pdev->dev);
Vitaly Wool711fdf62007-05-06 19:31:18 +040058
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;
Alexander Clouterd6fed9e2009-05-11 19:28:01 +010064 data->chip.write_buf = pdata->ctrl.write_buf;
65 data->chip.read_buf = pdata->ctrl.read_buf;
Vitaly Wool711fdf62007-05-06 19:31:18 +040066 data->chip.chip_delay = pdata->chip.chip_delay;
67 data->chip.options |= pdata->chip.options;
68
69 data->chip.ecc.hwctl = pdata->ctrl.hwcontrol;
70 data->chip.ecc.layout = pdata->chip.ecclayout;
71 data->chip.ecc.mode = NAND_ECC_SOFT;
72
73 platform_set_drvdata(pdev, data);
74
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -070075 /* Handle any platform specific setup */
76 if (pdata->ctrl.probe) {
77 res = pdata->ctrl.probe(pdev);
78 if (res)
79 goto out;
80 }
81
Vitaly Wool711fdf62007-05-06 19:31:18 +040082 /* Scan to find existance of the device */
83 if (nand_scan(&data->mtd, 1)) {
84 res = -ENXIO;
85 goto out;
86 }
87
88#ifdef CONFIG_MTD_PARTITIONS
89 if (pdata->chip.part_probe_types) {
90 res = parse_mtd_partitions(&data->mtd,
91 pdata->chip.part_probe_types,
92 &data->parts, 0);
93 if (res > 0) {
94 add_mtd_partitions(&data->mtd, data->parts, res);
95 return 0;
96 }
97 }
H Hartley Sweetenf36e20c2009-05-12 13:46:59 -070098 if (pdata->chip.set_parts)
99 pdata->chip.set_parts(data->mtd.size, &pdata->chip);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400100 if (pdata->chip.partitions) {
101 data->parts = pdata->chip.partitions;
102 res = add_mtd_partitions(&data->mtd, data->parts,
103 pdata->chip.nr_partitions);
104 } else
105#endif
106 res = add_mtd_device(&data->mtd);
107
108 if (!res)
109 return res;
110
111 nand_release(&data->mtd);
112out:
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700113 if (pdata->ctrl.remove)
114 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400115 platform_set_drvdata(pdev, NULL);
116 iounmap(data->io_base);
117 kfree(data);
118 return res;
119}
120
121/*
122 * Remove a NAND device.
123 */
124static int __devexit plat_nand_remove(struct platform_device *pdev)
125{
126 struct plat_nand_data *data = platform_get_drvdata(pdev);
127 struct platform_nand_data *pdata = pdev->dev.platform_data;
Vitaly Wool711fdf62007-05-06 19:31:18 +0400128
129 nand_release(&data->mtd);
130#ifdef CONFIG_MTD_PARTITIONS
131 if (data->parts && data->parts != pdata->chip.partitions)
132 kfree(data->parts);
133#endif
H Hartley Sweetenbf95efd2009-05-12 13:46:58 -0700134 if (pdata->ctrl.remove)
135 pdata->ctrl.remove(pdev);
Vitaly Wool711fdf62007-05-06 19:31:18 +0400136 iounmap(data->io_base);
137 kfree(data);
138
139 return 0;
140}
141
142static struct platform_driver plat_nand_driver = {
143 .probe = plat_nand_probe,
Thomas Chou24b5ce22009-04-21 12:27:34 +0800144 .remove = __devexit_p(plat_nand_remove),
Vitaly Wool711fdf62007-05-06 19:31:18 +0400145 .driver = {
146 .name = "gen_nand",
147 .owner = THIS_MODULE,
148 },
149};
150
151static int __init plat_nand_init(void)
152{
153 return platform_driver_register(&plat_nand_driver);
154}
155
156static void __exit plat_nand_exit(void)
157{
158 platform_driver_unregister(&plat_nand_driver);
159}
160
161module_init(plat_nand_init);
162module_exit(plat_nand_exit);
163
164MODULE_LICENSE("GPL");
165MODULE_AUTHOR("Vitaly Wool");
166MODULE_DESCRIPTION("Simple generic NAND driver");
Kay Sievers1ff18422008-04-18 13:44:27 -0700167MODULE_ALIAS("platform:gen_nand");