QE/FHCI: fixed the CONTROL bug
[zen-stable.git] / drivers / mtd / maps / plat-ram.c
blob9ca1eccba4bc2e70b0d5e7ea6c3be5d014f6da5e
1 /* drivers/mtd/maps/plat-ram.c
3 * (c) 2004-2005 Simtec Electronics
4 * http://www.simtec.co.uk/products/SWLINUX/
5 * Ben Dooks <ben@simtec.co.uk>
7 * Generic platform device based RAM map
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/string.h>
29 #include <linux/ioport.h>
30 #include <linux/device.h>
31 #include <linux/slab.h>
32 #include <linux/platform_device.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/map.h>
36 #include <linux/mtd/partitions.h>
37 #include <linux/mtd/plat-ram.h>
39 #include <asm/io.h>
41 /* private structure for each mtd platform ram device created */
43 struct platram_info {
44 struct device *dev;
45 struct mtd_info *mtd;
46 struct map_info map;
47 struct mtd_partition *partitions;
48 bool free_partitions;
49 struct resource *area;
50 struct platdata_mtd_ram *pdata;
53 /* to_platram_info()
55 * device private data to struct platram_info conversion
58 static inline struct platram_info *to_platram_info(struct platform_device *dev)
60 return (struct platram_info *)platform_get_drvdata(dev);
63 /* platram_setrw
65 * call the platform device's set rw/ro control
67 * to = 0 => read-only
68 * = 1 => read-write
71 static inline void platram_setrw(struct platram_info *info, int to)
73 if (info->pdata == NULL)
74 return;
76 if (info->pdata->set_rw != NULL)
77 (info->pdata->set_rw)(info->dev, to);
80 /* platram_remove
82 * called to remove the device from the driver's control
85 static int platram_remove(struct platform_device *pdev)
87 struct platram_info *info = to_platram_info(pdev);
89 platform_set_drvdata(pdev, NULL);
91 dev_dbg(&pdev->dev, "removing device\n");
93 if (info == NULL)
94 return 0;
96 if (info->mtd) {
97 mtd_device_unregister(info->mtd);
98 if (info->partitions) {
99 if (info->free_partitions)
100 kfree(info->partitions);
102 map_destroy(info->mtd);
105 /* ensure ram is left read-only */
107 platram_setrw(info, PLATRAM_RO);
109 /* release resources */
111 if (info->area) {
112 release_resource(info->area);
113 kfree(info->area);
116 if (info->map.virt != NULL)
117 iounmap(info->map.virt);
119 kfree(info);
121 return 0;
124 /* platram_probe
126 * called from device drive system when a device matching our
127 * driver is found.
130 static int platram_probe(struct platform_device *pdev)
132 struct platdata_mtd_ram *pdata;
133 struct platram_info *info;
134 struct resource *res;
135 int err = 0;
137 dev_dbg(&pdev->dev, "probe entered\n");
139 if (pdev->dev.platform_data == NULL) {
140 dev_err(&pdev->dev, "no platform data supplied\n");
141 err = -ENOENT;
142 goto exit_error;
145 pdata = pdev->dev.platform_data;
147 info = kzalloc(sizeof(*info), GFP_KERNEL);
148 if (info == NULL) {
149 dev_err(&pdev->dev, "no memory for flash info\n");
150 err = -ENOMEM;
151 goto exit_error;
154 platform_set_drvdata(pdev, info);
156 info->dev = &pdev->dev;
157 info->pdata = pdata;
159 /* get the resource for the memory mapping */
161 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
163 if (res == NULL) {
164 dev_err(&pdev->dev, "no memory resource specified\n");
165 err = -ENOENT;
166 goto exit_free;
169 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
170 (unsigned long long)res->start);
172 /* setup map parameters */
174 info->map.phys = res->start;
175 info->map.size = resource_size(res);
176 info->map.name = pdata->mapname != NULL ?
177 (char *)pdata->mapname : (char *)pdev->name;
178 info->map.bankwidth = pdata->bankwidth;
180 /* register our usage of the memory area */
182 info->area = request_mem_region(res->start, info->map.size, pdev->name);
183 if (info->area == NULL) {
184 dev_err(&pdev->dev, "failed to request memory region\n");
185 err = -EIO;
186 goto exit_free;
189 /* remap the memory area */
191 info->map.virt = ioremap(res->start, info->map.size);
192 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
194 if (info->map.virt == NULL) {
195 dev_err(&pdev->dev, "failed to ioremap() region\n");
196 err = -EIO;
197 goto exit_free;
200 simple_map_init(&info->map);
202 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
204 /* probe for the right mtd map driver
205 * supplied by the platform_data struct */
207 if (pdata->map_probes) {
208 const char **map_probes = pdata->map_probes;
210 for ( ; !info->mtd && *map_probes; map_probes++)
211 info->mtd = do_map_probe(*map_probes , &info->map);
213 /* fallback to map_ram */
214 else
215 info->mtd = do_map_probe("map_ram", &info->map);
217 if (info->mtd == NULL) {
218 dev_err(&pdev->dev, "failed to probe for map_ram\n");
219 err = -ENOMEM;
220 goto exit_free;
223 info->mtd->owner = THIS_MODULE;
224 info->mtd->dev.parent = &pdev->dev;
226 platram_setrw(info, PLATRAM_RW);
228 /* check to see if there are any available partitions, or wether
229 * to add this device whole */
231 if (!pdata->nr_partitions) {
232 /* try to probe using the supplied probe type */
233 if (pdata->probes) {
234 err = parse_mtd_partitions(info->mtd, pdata->probes,
235 &info->partitions, 0);
236 info->free_partitions = 1;
237 if (err > 0)
238 err = mtd_device_register(info->mtd,
239 info->partitions, err);
242 /* use the static mapping */
243 else
244 err = mtd_device_register(info->mtd, pdata->partitions,
245 pdata->nr_partitions);
246 if (!err)
247 dev_info(&pdev->dev, "registered mtd device\n");
249 /* add the whole device. */
250 err = mtd_device_register(info->mtd, NULL, 0);
251 if (err)
252 dev_err(&pdev->dev, "failed to register the entire device\n");
254 return err;
256 exit_free:
257 platram_remove(pdev);
258 exit_error:
259 return err;
262 /* device driver info */
264 /* work with hotplug and coldplug */
265 MODULE_ALIAS("platform:mtd-ram");
267 static struct platform_driver platram_driver = {
268 .probe = platram_probe,
269 .remove = platram_remove,
270 .driver = {
271 .name = "mtd-ram",
272 .owner = THIS_MODULE,
276 /* module init/exit */
278 static int __init platram_init(void)
280 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
281 return platform_driver_register(&platram_driver);
284 static void __exit platram_exit(void)
286 platform_driver_unregister(&platram_driver);
289 module_init(platram_init);
290 module_exit(platram_exit);
292 MODULE_LICENSE("GPL");
293 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
294 MODULE_DESCRIPTION("MTD platform RAM map driver");