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 * 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>
41 /* private structure for each mtd platform ram device created */
47 struct mtd_partition *partitions;
49 struct resource *area;
50 struct platdata_mtd_ram *pdata;
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);
65 * call the platform device's set rw/ro control
71 static inline void platram_setrw(struct platram_info *info, int to)
73 if (info->pdata == NULL)
76 if (info->pdata->set_rw != NULL)
77 (info->pdata->set_rw)(info->dev, to);
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");
97 #ifdef CONFIG_MTD_PARTITIONS
98 if (info->partitions) {
99 del_mtd_partitions(info->mtd);
100 if (info->free_partitions)
101 kfree(info->partitions);
104 del_mtd_device(info->mtd);
105 map_destroy(info->mtd);
108 /* ensure ram is left read-only */
110 platram_setrw(info, PLATRAM_RO);
112 /* release resources */
115 release_resource(info->area);
119 if (info->map.virt != NULL)
120 iounmap(info->map.virt);
129 * called from device drive system when a device matching our
133 static int platram_probe(struct platform_device *pdev)
135 struct platdata_mtd_ram *pdata;
136 struct platram_info *info;
137 struct resource *res;
140 dev_dbg(&pdev->dev, "probe entered\n");
142 if (pdev->dev.platform_data == NULL) {
143 dev_err(&pdev->dev, "no platform data supplied\n");
148 pdata = pdev->dev.platform_data;
150 info = kzalloc(sizeof(*info), GFP_KERNEL);
152 dev_err(&pdev->dev, "no memory for flash info\n");
157 platform_set_drvdata(pdev, info);
159 info->dev = &pdev->dev;
162 /* get the resource for the memory mapping */
164 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
167 dev_err(&pdev->dev, "no memory resource specified\n");
172 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
173 (unsigned long long)res->start);
175 /* setup map parameters */
177 info->map.phys = res->start;
178 info->map.size = (res->end - res->start) + 1;
179 info->map.name = pdata->mapname != NULL ?
180 (char *)pdata->mapname : (char *)pdev->name;
181 info->map.bankwidth = pdata->bankwidth;
183 /* register our usage of the memory area */
185 info->area = request_mem_region(res->start, info->map.size, pdev->name);
186 if (info->area == NULL) {
187 dev_err(&pdev->dev, "failed to request memory region\n");
192 /* remap the memory area */
194 info->map.virt = ioremap(res->start, info->map.size);
195 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
197 if (info->map.virt == NULL) {
198 dev_err(&pdev->dev, "failed to ioremap() region\n");
203 simple_map_init(&info->map);
205 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
207 /* probe for the right mtd map driver
208 * supplied by the platform_data struct */
210 if (pdata->map_probes) {
211 const char **map_probes = pdata->map_probes;
213 for ( ; !info->mtd && *map_probes; map_probes++)
214 info->mtd = do_map_probe(*map_probes , &info->map);
216 /* fallback to map_ram */
218 info->mtd = do_map_probe("map_ram", &info->map);
220 if (info->mtd == NULL) {
221 dev_err(&pdev->dev, "failed to probe for map_ram\n");
226 info->mtd->owner = THIS_MODULE;
228 platram_setrw(info, PLATRAM_RW);
230 /* check to see if there are any available partitions, or wether
231 * to add this device whole */
233 #ifdef CONFIG_MTD_PARTITIONS
234 if (!pdata->nr_partitions) {
235 /* try to probe using the supplied probe type */
237 err = parse_mtd_partitions(info->mtd, pdata->probes,
238 &info->partitions, 0);
239 info->free_partitions = 1;
241 err = add_mtd_partitions(info->mtd,
242 info->partitions, err);
245 /* use the static mapping */
247 err = add_mtd_partitions(info->mtd, pdata->partitions,
248 pdata->nr_partitions);
249 #endif /* CONFIG_MTD_PARTITIONS */
251 if (add_mtd_device(info->mtd)) {
252 dev_err(&pdev->dev, "add_mtd_device() failed\n");
257 dev_info(&pdev->dev, "registered mtd device\n");
262 platram_remove(pdev);
267 /* device driver info */
269 /* work with hotplug and coldplug */
270 MODULE_ALIAS("platform:mtd-ram");
272 static struct platform_driver platram_driver = {
273 .probe = platram_probe,
274 .remove = platram_remove,
277 .owner = THIS_MODULE,
281 /* module init/exit */
283 static int __init platram_init(void)
285 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
286 return platform_driver_register(&platram_driver);
289 static void __exit platram_exit(void)
291 platform_driver_unregister(&platram_driver);
294 module_init(platram_init);
295 module_exit(platram_exit);
297 MODULE_LICENSE("GPL");
298 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
299 MODULE_DESCRIPTION("MTD platform RAM map driver");