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
;
50 struct resource
*area
;
51 struct platdata_mtd_ram
*pdata
;
56 * device private data to struct platram_info conversion
59 static inline struct platram_info
*to_platram_info(struct platform_device
*dev
)
61 return (struct platram_info
*)platform_get_drvdata(dev
);
66 * call the platform device's set rw/ro control
72 static inline void platram_setrw(struct platram_info
*info
, int to
)
74 if (info
->pdata
== NULL
)
77 if (info
->pdata
->set_rw
!= NULL
)
78 (info
->pdata
->set_rw
)(info
->dev
, to
);
83 * called to remove the device from the driver's control
86 static int platram_remove(struct platform_device
*pdev
)
88 struct platram_info
*info
= to_platram_info(pdev
);
90 platform_set_drvdata(pdev
, NULL
);
92 dev_dbg(&pdev
->dev
, "removing device\n");
98 #ifdef CONFIG_MTD_PARTITIONS
99 if (info
->partitions
) {
100 del_mtd_partitions(info
->mtd
);
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%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
: (char *)pdev
->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
, pdev
->name
);
184 if (info
->area
== NULL
) {
185 dev_err(&pdev
->dev
, "failed to request memory region\n");
190 /* remap the memory area */
192 info
->map
.virt
= ioremap(res
->start
, info
->map
.size
);
193 dev_dbg(&pdev
->dev
, "virt %p, %lu bytes\n", info
->map
.virt
, info
->map
.size
);
195 if (info
->map
.virt
== NULL
) {
196 dev_err(&pdev
->dev
, "failed to ioremap() region\n");
201 simple_map_init(&info
->map
);
203 dev_dbg(&pdev
->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(&pdev
->dev
, "failed to probe for map_ram\n");
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
};
226 probes
= (const char **)pdata
->probes
;
228 err
= parse_mtd_partitions(info
->mtd
, probes
,
229 &info
->partitions
, 0);
231 err
= add_mtd_partitions(info
->mtd
, info
->partitions
,
235 #endif /* CONFIG_MTD_PARTITIONS */
237 if (add_mtd_device(info
->mtd
)) {
238 dev_err(&pdev
->dev
, "add_mtd_device() failed\n");
242 dev_info(&pdev
->dev
, "registered mtd device\n");
246 platram_remove(pdev
);
251 /* device driver info */
253 static struct platform_driver platram_driver
= {
254 .probe
= platram_probe
,
255 .remove
= platram_remove
,
258 .owner
= THIS_MODULE
,
262 /* module init/exit */
264 static int __init
platram_init(void)
266 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
267 return platform_driver_register(&platram_driver
);
270 static void __exit
platram_exit(void)
272 platform_driver_unregister(&platram_driver
);
275 module_init(platram_init
);
276 module_exit(platram_exit
);
278 MODULE_LICENSE("GPL");
279 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
280 MODULE_DESCRIPTION("MTD platform RAM map driver");