Commit | Line | Data |
---|---|---|
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 | ||
20 | struct plat_nand_data { | |
21 | struct nand_chip chip; | |
22 | struct mtd_info mtd; | |
23 | void __iomem *io_base; | |
711fdf62 VW |
24 | int nr_parts; |
25 | struct mtd_partition *parts; | |
711fdf62 VW |
26 | }; |
27 | ||
28 | /* | |
29 | * Probe for the NAND device. | |
30 | */ | |
9d63287a | 31 | static int __devinit plat_nand_probe(struct platform_device *pdev) |
711fdf62 VW |
32 | { |
33 | struct platform_nand_data *pdata = pdev->dev.platform_data; | |
34 | struct plat_nand_data *data; | |
2d098a72 HS |
35 | struct resource *res; |
36 | int err = 0; | |
37 | ||
01cd2aba MV |
38 | if (pdata->chip.nr_chips < 1) { |
39 | dev_err(&pdev->dev, "invalid number of chips specified\n"); | |
40 | return -EINVAL; | |
41 | } | |
42 | ||
2d098a72 HS |
43 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
44 | if (!res) | |
45 | return -ENXIO; | |
711fdf62 VW |
46 | |
47 | /* Allocate memory for the device structure (and zero it) */ | |
48 | data = kzalloc(sizeof(struct plat_nand_data), GFP_KERNEL); | |
49 | if (!data) { | |
50 | dev_err(&pdev->dev, "failed to allocate device structure.\n"); | |
51 | return -ENOMEM; | |
52 | } | |
53 | ||
2d098a72 HS |
54 | if (!request_mem_region(res->start, resource_size(res), |
55 | dev_name(&pdev->dev))) { | |
56 | dev_err(&pdev->dev, "request_mem_region failed\n"); | |
57 | err = -EBUSY; | |
58 | goto out_free; | |
59 | } | |
60 | ||
61 | data->io_base = ioremap(res->start, resource_size(res)); | |
711fdf62 VW |
62 | if (data->io_base == NULL) { |
63 | dev_err(&pdev->dev, "ioremap failed\n"); | |
2d098a72 HS |
64 | err = -EIO; |
65 | goto out_release_io; | |
711fdf62 VW |
66 | } |
67 | ||
68 | data->chip.priv = &data; | |
69 | data->mtd.priv = &data->chip; | |
70 | data->mtd.owner = THIS_MODULE; | |
475b44c1 | 71 | data->mtd.name = dev_name(&pdev->dev); |
711fdf62 VW |
72 | |
73 | data->chip.IO_ADDR_R = data->io_base; | |
74 | data->chip.IO_ADDR_W = data->io_base; | |
75 | data->chip.cmd_ctrl = pdata->ctrl.cmd_ctrl; | |
76 | data->chip.dev_ready = pdata->ctrl.dev_ready; | |
77 | data->chip.select_chip = pdata->ctrl.select_chip; | |
d6fed9e9 AC |
78 | data->chip.write_buf = pdata->ctrl.write_buf; |
79 | data->chip.read_buf = pdata->ctrl.read_buf; | |
711fdf62 VW |
80 | data->chip.chip_delay = pdata->chip.chip_delay; |
81 | data->chip.options |= pdata->chip.options; | |
82 | ||
83 | data->chip.ecc.hwctl = pdata->ctrl.hwcontrol; | |
84 | data->chip.ecc.layout = pdata->chip.ecclayout; | |
85 | data->chip.ecc.mode = NAND_ECC_SOFT; | |
86 | ||
87 | platform_set_drvdata(pdev, data); | |
88 | ||
bf95efd4 HS |
89 | /* Handle any platform specific setup */ |
90 | if (pdata->ctrl.probe) { | |
2d098a72 HS |
91 | err = pdata->ctrl.probe(pdev); |
92 | if (err) | |
bf95efd4 HS |
93 | goto out; |
94 | } | |
95 | ||
25985edc | 96 | /* Scan to find existence of the device */ |
81cbb0b1 | 97 | if (nand_scan(&data->mtd, pdata->chip.nr_chips)) { |
2d098a72 | 98 | err = -ENXIO; |
711fdf62 VW |
99 | goto out; |
100 | } | |
101 | ||
711fdf62 | 102 | if (pdata->chip.part_probe_types) { |
2d098a72 | 103 | err = parse_mtd_partitions(&data->mtd, |
711fdf62 VW |
104 | pdata->chip.part_probe_types, |
105 | &data->parts, 0); | |
2d098a72 | 106 | if (err > 0) { |
3ee904f2 | 107 | mtd_device_register(&data->mtd, data->parts, err); |
711fdf62 VW |
108 | return 0; |
109 | } | |
110 | } | |
f36e20c0 HS |
111 | if (pdata->chip.set_parts) |
112 | pdata->chip.set_parts(data->mtd.size, &pdata->chip); | |
711fdf62 VW |
113 | if (pdata->chip.partitions) { |
114 | data->parts = pdata->chip.partitions; | |
3ee904f2 | 115 | err = mtd_device_register(&data->mtd, data->parts, |
711fdf62 VW |
116 | pdata->chip.nr_partitions); |
117 | } else | |
3ee904f2 | 118 | err = mtd_device_register(&data->mtd, NULL, 0); |
711fdf62 | 119 | |
2d098a72 HS |
120 | if (!err) |
121 | return err; | |
711fdf62 VW |
122 | |
123 | nand_release(&data->mtd); | |
124 | out: | |
bf95efd4 HS |
125 | if (pdata->ctrl.remove) |
126 | pdata->ctrl.remove(pdev); | |
711fdf62 VW |
127 | platform_set_drvdata(pdev, NULL); |
128 | iounmap(data->io_base); | |
2d098a72 HS |
129 | out_release_io: |
130 | release_mem_region(res->start, resource_size(res)); | |
131 | out_free: | |
711fdf62 | 132 | kfree(data); |
2d098a72 | 133 | return err; |
711fdf62 VW |
134 | } |
135 | ||
136 | /* | |
137 | * Remove a NAND device. | |
138 | */ | |
139 | static int __devexit plat_nand_remove(struct platform_device *pdev) | |
140 | { | |
141 | struct plat_nand_data *data = platform_get_drvdata(pdev); | |
142 | struct platform_nand_data *pdata = pdev->dev.platform_data; | |
2d098a72 HS |
143 | struct resource *res; |
144 | ||
145 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
711fdf62 VW |
146 | |
147 | nand_release(&data->mtd); | |
711fdf62 VW |
148 | if (data->parts && data->parts != pdata->chip.partitions) |
149 | kfree(data->parts); | |
bf95efd4 HS |
150 | if (pdata->ctrl.remove) |
151 | pdata->ctrl.remove(pdev); | |
711fdf62 | 152 | iounmap(data->io_base); |
2d098a72 | 153 | release_mem_region(res->start, resource_size(res)); |
711fdf62 VW |
154 | kfree(data); |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
159 | static struct platform_driver plat_nand_driver = { | |
160 | .probe = plat_nand_probe, | |
24b5ce20 | 161 | .remove = __devexit_p(plat_nand_remove), |
711fdf62 VW |
162 | .driver = { |
163 | .name = "gen_nand", | |
164 | .owner = THIS_MODULE, | |
165 | }, | |
166 | }; | |
167 | ||
168 | static int __init plat_nand_init(void) | |
169 | { | |
170 | return platform_driver_register(&plat_nand_driver); | |
171 | } | |
172 | ||
173 | static void __exit plat_nand_exit(void) | |
174 | { | |
175 | platform_driver_unregister(&plat_nand_driver); | |
176 | } | |
177 | ||
178 | module_init(plat_nand_init); | |
179 | module_exit(plat_nand_exit); | |
180 | ||
181 | MODULE_LICENSE("GPL"); | |
182 | MODULE_AUTHOR("Vitaly Wool"); | |
183 | MODULE_DESCRIPTION("Simple generic NAND driver"); | |
1ff18422 | 184 | MODULE_ALIAS("platform:gen_nand"); |