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 platfrom device based RAM map
9 * $Id: plat-ram.c,v 1.7 2005/11/07 11:14:28 gleixner Exp $
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/string.h>
31 #include <linux/ioport.h>
32 #include <linux/device.h>
33 #include <linux/slab.h>
34 #include <linux/platform_device.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/map.h>
38 #include <linux/mtd/partitions.h>
39 #include <linux/mtd/plat-ram.h>
43 /* private structure for each mtd platform ram device created */
49 struct mtd_partition *partitions;
51 struct resource *area;
52 struct platdata_mtd_ram *pdata;
57 * device private data to struct platram_info conversion
60 static inline struct platram_info *to_platram_info(struct platform_device *dev)
62 return (struct platram_info *)platform_get_drvdata(dev);
67 * call the platform device's set rw/ro control
73 static inline void platram_setrw(struct platram_info *info, int to)
75 if (info->pdata == NULL)
78 if (info->pdata->set_rw != NULL)
79 (info->pdata->set_rw)(info->dev, to);
84 * called to remove the device from the driver's control
87 static int platram_remove(struct platform_device *pdev)
89 struct platram_info *info = to_platram_info(pdev);
91 platform_set_drvdata(pdev, NULL);
93 dev_dbg(&pdev->dev, "removing device\n");
99 #ifdef CONFIG_MTD_PARTITIONS
100 if (info->partitions) {
101 del_mtd_partitions(info->mtd);
102 if (info->free_partitions)
103 kfree(info->partitions);
106 del_mtd_device(info->mtd);
107 map_destroy(info->mtd);
110 /* ensure ram is left read-only */
112 platram_setrw(info, PLATRAM_RO);
114 /* release resources */
117 release_resource(info->area);
121 if (info->map.virt != NULL)
122 iounmap(info->map.virt);
131 * called from device drive system when a device matching our
135 static int platram_probe(struct platform_device *pdev)
137 struct platdata_mtd_ram *pdata;
138 struct platram_info *info;
139 struct resource *res;
142 dev_dbg(&pdev->dev, "probe entered\n");
144 if (pdev->dev.platform_data == NULL) {
145 dev_err(&pdev->dev, "no platform data supplied\n");
150 pdata = pdev->dev.platform_data;
152 info = kzalloc(sizeof(*info), GFP_KERNEL);
154 dev_err(&pdev->dev, "no memory for flash info\n");
159 platform_set_drvdata(pdev, info);
161 info->dev = &pdev->dev;
164 /* get the resource for the memory mapping */
166 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
169 dev_err(&pdev->dev, "no memory resource specified\n");
174 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
175 (unsigned long long)res->start);
177 /* setup map parameters */
179 info->map.phys = res->start;
180 info->map.size = (res->end - res->start) + 1;
181 info->map.name = pdata->mapname != NULL ?
182 (char *)pdata->mapname : (char *)pdev->name;
183 info->map.bankwidth = pdata->bankwidth;
185 /* register our usage of the memory area */
187 info->area = request_mem_region(res->start, info->map.size, pdev->name);
188 if (info->area == NULL) {
189 dev_err(&pdev->dev, "failed to request memory region\n");
194 /* remap the memory area */
196 info->map.virt = ioremap(res->start, info->map.size);
197 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
199 if (info->map.virt == NULL) {
200 dev_err(&pdev->dev, "failed to ioremap() region\n");
205 simple_map_init(&info->map);
207 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
209 /* probe for the right mtd map driver
210 * supplied by the platform_data struct */
212 if (pdata->map_probes != 0) {
213 const char **map_probes = pdata->map_probes;
215 for ( ; !info->mtd && *map_probes; map_probes++)
216 info->mtd = do_map_probe(*map_probes , &info->map);
218 /* fallback to map_ram */
220 info->mtd = do_map_probe("map_ram", &info->map);
222 if (info->mtd == NULL) {
223 dev_err(&pdev->dev, "failed to probe for map_ram\n");
228 info->mtd->owner = THIS_MODULE;
230 platram_setrw(info, PLATRAM_RW);
232 /* check to see if there are any available partitions, or wether
233 * to add this device whole */
235 #ifdef CONFIG_MTD_PARTITIONS
236 if (!pdata->nr_partitions) {
237 /* try to probe using the supplied probe type */
239 err = parse_mtd_partitions(info->mtd, pdata->probes,
240 &info->partitions, 0);
241 info->free_partitions = 1;
243 err = add_mtd_partitions(info->mtd,
244 info->partitions, err);
247 /* use the static mapping */
249 err = add_mtd_partitions(info->mtd, pdata->partitions,
250 pdata->nr_partitions);
251 #endif /* CONFIG_MTD_PARTITIONS */
253 if (add_mtd_device(info->mtd)) {
254 dev_err(&pdev->dev, "add_mtd_device() failed\n");
259 dev_info(&pdev->dev, "registered mtd device\n");
264 platram_remove(pdev);
269 /* device driver info */
271 /* work with hotplug and coldplug */
272 MODULE_ALIAS("platform:mtd-ram");
274 static struct platform_driver platram_driver = {
275 .probe = platram_probe,
276 .remove = platram_remove,
279 .owner = THIS_MODULE,
283 /* module init/exit */
285 static int __init platram_init(void)
287 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
288 return platform_driver_register(&platram_driver);
291 static void __exit platram_exit(void)
293 platform_driver_unregister(&platram_driver);
296 module_init(platram_init);
297 module_exit(platram_exit);
299 MODULE_LICENSE("GPL");
300 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
301 MODULE_DESCRIPTION("MTD platform RAM map driver");