[PATCH] docs: fix misinformation about overcommit_memory
[linux-2.6.git] / drivers / mtd / maps / plat-ram.c
blob118b04544cad36b11307b1d560f418e1b885b533
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.3 2005/03/19 22:41:27 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>
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 struct resource *area;
49 struct platdata_mtd_ram *pdata;
52 /* to_platram_info()
54 * device private data to struct platram_info conversion
57 static inline struct platram_info *to_platram_info(struct device *dev)
59 return (struct platram_info *)dev_get_drvdata(dev);
62 /* platram_setrw
64 * call the platform device's set rw/ro control
66 * to = 0 => read-only
67 * = 1 => read-write
70 static inline void platram_setrw(struct platram_info *info, int to)
72 if (info->pdata == NULL)
73 return;
75 if (info->pdata->set_rw != NULL)
76 (info->pdata->set_rw)(info->dev, to);
79 /* platram_remove
81 * called to remove the device from the driver's control
84 static int platram_remove(struct device *dev)
86 struct platram_info *info = to_platram_info(dev);
88 dev_set_drvdata(dev, NULL);
90 dev_dbg(dev, "removing device\n");
92 if (info == NULL)
93 return 0;
95 if (info->mtd) {
96 #ifdef CONFIG_MTD_PARTITIONS
97 if (info->partitions) {
98 del_mtd_partitions(info->mtd);
99 kfree(info->partitions);
101 #endif
102 del_mtd_device(info->mtd);
103 map_destroy(info->mtd);
106 /* ensure ram is left read-only */
108 platram_setrw(info, PLATRAM_RO);
110 /* release resources */
112 if (info->area) {
113 release_resource(info->area);
114 kfree(info->area);
117 if (info->map.virt != NULL)
118 iounmap(info->map.virt);
120 kfree(info);
122 return 0;
125 /* platram_probe
127 * called from device drive system when a device matching our
128 * driver is found.
131 static int platram_probe(struct device *dev)
133 struct platform_device *pd = to_platform_device(dev);
134 struct platdata_mtd_ram *pdata;
135 struct platram_info *info;
136 struct resource *res;
137 int err = 0;
139 dev_dbg(dev, "probe entered\n");
141 if (dev->platform_data == NULL) {
142 dev_err(dev, "no platform data supplied\n");
143 err = -ENOENT;
144 goto exit_error;
147 pdata = dev->platform_data;
149 info = kmalloc(sizeof(*info), GFP_KERNEL);
150 if (info == NULL) {
151 dev_err(dev, "no memory for flash info\n");
152 err = -ENOMEM;
153 goto exit_error;
156 memset(info, 0, sizeof(*info));
157 dev_set_drvdata(dev, info);
159 info->dev = dev;
160 info->pdata = pdata;
162 /* get the resource for the memory mapping */
164 res = platform_get_resource(pd, IORESOURCE_MEM, 0);
166 if (res == NULL) {
167 dev_err(dev, "no memory resource specified\n");
168 err = -ENOENT;
169 goto exit_free;
172 dev_dbg(dev, "got platform resource %p (0x%lx)\n", res, res->start);
174 /* setup map parameters */
176 info->map.phys = res->start;
177 info->map.size = (res->end - res->start) + 1;
178 info->map.name = pdata->mapname != NULL ? pdata->mapname : pd->name;
179 info->map.bankwidth = pdata->bankwidth;
181 /* register our usage of the memory area */
183 info->area = request_mem_region(res->start, info->map.size, pd->name);
184 if (info->area == NULL) {
185 dev_err(dev, "failed to request memory region\n");
186 err = -EIO;
187 goto exit_free;
190 /* remap the memory area */
192 info->map.virt = ioremap(res->start, info->map.size);
193 dev_dbg(dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
195 if (info->map.virt == NULL) {
196 dev_err(dev, "failed to ioremap() region\n");
197 err = -EIO;
198 goto exit_free;
201 simple_map_init(&info->map);
203 dev_dbg(dev, "initialised map, probing for mtd\n");
205 /* probe for the right mtd map driver */
207 info->mtd = do_map_probe("map_ram" , &info->map);
208 if (info->mtd == NULL) {
209 dev_err(dev, "failed to probe for map_ram\n");
210 err = -ENOMEM;
211 goto exit_free;
214 info->mtd->owner = THIS_MODULE;
216 platram_setrw(info, PLATRAM_RW);
218 /* check to see if there are any available partitions, or wether
219 * to add this device whole */
221 #ifdef CONFIG_MTD_PARTITIONS
222 if (pdata->nr_partitions > 0) {
223 const char **probes = { NULL };
225 if (pdata->probes)
226 probes = (const char **)pdata->probes;
228 err = parse_mtd_partitions(info->mtd, probes,
229 &info->partitions, 0);
230 if (err > 0) {
231 err = add_mtd_partitions(info->mtd, info->partitions,
232 err);
235 #endif /* CONFIG_MTD_PARTITIONS */
237 if (add_mtd_device(info->mtd)) {
238 dev_err(dev, "add_mtd_device() failed\n");
239 err = -ENOMEM;
242 dev_info(dev, "registered mtd device\n");
243 return err;
245 exit_free:
246 platram_remove(dev);
247 exit_error:
248 return err;
251 /* device driver info */
253 static struct device_driver platram_driver = {
254 .name = "mtd-ram",
255 .bus = &platform_bus_type,
256 .probe = platram_probe,
257 .remove = platram_remove,
260 /* module init/exit */
262 static int __init platram_init(void)
264 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
265 return driver_register(&platram_driver);
268 static void __exit platram_exit(void)
270 driver_unregister(&platram_driver);
273 module_init(platram_init);
274 module_exit(platram_exit);
276 MODULE_LICENSE("GPL");
277 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
278 MODULE_DESCRIPTION("MTD platform RAM map driver");