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>
46 /* private structure for each mtd platform ram device created */
52 struct mtd_partition
*partitions
;
53 struct platdata_mtd_ram
*pdata
;
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
);
68 * call the platform device's set rw/ro control
74 static inline void platram_setrw(struct platram_info
*info
, int to
)
76 if (info
->pdata
== NULL
)
79 if (info
->pdata
->set_rw
!= NULL
)
80 (info
->pdata
->set_rw
)(info
->dev
, to
);
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");
100 #ifdef CONFIG_MTD_PARTITIONS
101 del_mtd_partitions(info
->mtd
);
102 if (info
->partitions
)
103 kfree(info
->partitions
);
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
);
125 * called from device drive system when a device matching our
129 static int platram_probe(struct platform_device
*pdev
)
131 struct platdata_mtd_ram
*pdata
;
132 struct platram_info
*info
;
133 struct resource
*res
;
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");
144 pdata
= pdev
->dev
.platform_data
;
146 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
148 dev_err(&pdev
->dev
, "no memory for flash info\n");
153 memset(info
, 0, sizeof(*info
));
154 platform_set_drvdata(pdev
, info
);
156 info
->dev
= &pdev
->dev
;
159 /* get the resource for the memory mapping */
161 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
164 dev_err(&pdev
->dev
, "no memory resource specified\n");
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");
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");
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
);
215 } else if (pdata
->probes
) {
216 err
= parse_mtd_partitions(info
->mtd
, pdata
->probes
,
217 &info
->partitions
, 0);
219 err
= add_mtd_partitions(info
->mtd
, info
->partitions
,
225 #endif /* CONFIG_MTD_PARTITIONS */
227 if (add_mtd_device(info
->mtd
)) {
228 dev_err(&pdev
->dev
, "add_mtd_device() failed\n");
233 dev_info(&pdev
->dev
, "registered mtd device\n");
236 ROOT_DEV
= MKDEV(MTD_BLOCK_MAJOR
, info
->mtd
->index
);
241 platram_remove(pdev
);
246 /* device driver info */
248 static struct platform_driver platram_driver
= {
249 .probe
= platram_probe
,
250 .remove
= platram_remove
,
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");