MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / mtd / maps / plat-ram.c
blob5f6146c3d0a2c433d735a8c37e38c88d04a8025e
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>
35 #include <linux/kdev_t.h>
36 #include <linux/major.h>
37 #include <linux/root_dev.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/mtd/map.h>
41 #include <linux/mtd/partitions.h>
42 #include <linux/mtd/plat-ram.h>
44 #include <asm/io.h>
46 /* private structure for each mtd platform ram device created */
48 struct platram_info {
49 struct device *dev;
50 struct mtd_info *mtd;
51 struct map_info map;
52 struct mtd_partition *partitions;
53 struct platdata_mtd_ram *pdata;
56 /* to_platram_info()
58 * device private data to struct platram_info conversion
61 static inline struct platram_info *to_platram_info(struct platform_device *dev)
63 return (struct platram_info *)platform_get_drvdata(dev);
66 /* platram_setrw
68 * call the platform device's set rw/ro control
70 * to = 0 => read-only
71 * = 1 => read-write
74 static inline void platram_setrw(struct platram_info *info, int to)
76 if (info->pdata == NULL)
77 return;
79 if (info->pdata->set_rw != NULL)
80 (info->pdata->set_rw)(info->dev, to);
83 /* platram_remove
85 * called to remove the device from the driver's control
88 static int platram_remove(struct platform_device *pdev)
90 struct platram_info *info = to_platram_info(pdev);
92 platform_set_drvdata(pdev, NULL);
94 dev_dbg(&pdev->dev, "removing device\n");
96 if (info == NULL)
97 return 0;
99 if (info->mtd) {
100 #ifdef CONFIG_MTD_PARTITIONS
101 del_mtd_partitions(info->mtd);
102 if (info->partitions)
103 kfree(info->partitions);
104 #endif
105 del_mtd_device(info->mtd);
106 map_destroy(info->mtd);
109 /* ensure ram is left read-only */
111 platram_setrw(info, PLATRAM_RO);
113 /* release resources */
115 if (info->map.virt != NULL)
116 iounmap(info->map.virt);
118 kfree(info);
120 return 0;
123 /* platram_probe
125 * called from device drive system when a device matching our
126 * driver is found.
129 static int platram_probe(struct platform_device *pdev)
131 struct platdata_mtd_ram *pdata;
132 struct platram_info *info;
133 struct resource *res;
134 int err = 0;
136 dev_dbg(&pdev->dev, "probe entered\n");
138 if (pdev->dev.platform_data == NULL) {
139 dev_err(&pdev->dev, "no platform data supplied\n");
140 err = -ENOENT;
141 goto exit_error;
144 pdata = pdev->dev.platform_data;
146 info = kmalloc(sizeof(*info), GFP_KERNEL);
147 if (info == NULL) {
148 dev_err(&pdev->dev, "no memory for flash info\n");
149 err = -ENOMEM;
150 goto exit_error;
153 memset(info, 0, sizeof(*info));
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%lx)\n", res, res->start);
171 /* setup map parameters */
173 info->map.phys = res->start;
174 info->map.size = (res->end - res->start) + 1;
175 info->map.name = pdata->mapname != NULL ? pdata->mapname : (char *)pdev->name;
176 info->map.bankwidth = pdata->bankwidth;
178 /* remap the memory area */
180 info->map.virt = ioremap(res->start, info->map.size);
181 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
183 if (info->map.virt == NULL) {
184 dev_err(&pdev->dev, "failed to ioremap() region\n");
185 err = -EIO;
186 goto exit_free;
189 simple_map_init(&info->map);
191 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
193 /* probe for the right mtd map driver */
195 info->mtd = do_map_probe("map_ram" , &info->map);
196 if (info->mtd == NULL) {
197 dev_err(&pdev->dev, "failed to probe for map_ram\n");
198 err = -ENOMEM;
199 goto exit_free;
202 info->mtd->owner = THIS_MODULE;
204 platram_setrw(info, PLATRAM_RW);
206 /* check to see if there are any available partitions, or wether
207 * to add this device whole */
209 #ifdef CONFIG_MTD_PARTITIONS
210 if (pdata->nr_partitions > 0) {
211 err = add_mtd_partitions(info->mtd, pdata->partitions,
212 pdata->nr_partitions);
213 if (err < 0)
214 goto exit_free;
215 } else if (pdata->probes) {
216 err = parse_mtd_partitions(info->mtd, pdata->probes,
217 &info->partitions, 0);
218 if (err > 0) {
219 err = add_mtd_partitions(info->mtd, info->partitions,
220 err);
222 if (err < 0)
223 goto exit_free;
225 #endif /* CONFIG_MTD_PARTITIONS */
227 if (add_mtd_device(info->mtd)) {
228 dev_err(&pdev->dev, "add_mtd_device() failed\n");
229 err = -ENOMEM;
230 goto exit_free;
233 dev_info(&pdev->dev, "registered mtd device\n");
235 if (pdata->root_dev)
236 ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, info->mtd->index);
238 return 0;
240 exit_free:
241 platram_remove(pdev);
242 exit_error:
243 return err;
246 /* device driver info */
248 static struct platform_driver platram_driver = {
249 .probe = platram_probe,
250 .remove = platram_remove,
251 .driver = {
252 .name = "mtd-ram",
253 .owner = THIS_MODULE,
257 /* module init/exit */
259 static int __init platram_init(void)
261 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
262 return platform_driver_register(&platram_driver);
265 static void __exit platram_exit(void)
267 platform_driver_unregister(&platram_driver);
270 module_init(platram_init);
271 module_exit(platram_exit);
273 MODULE_LICENSE("GPL");
274 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
275 MODULE_DESCRIPTION("MTD platform RAM map driver");