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 platform 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 resource
*area
;
48 struct platdata_mtd_ram
*pdata
;
53 * device private data to struct platram_info conversion
56 static inline struct platram_info
*to_platram_info(struct platform_device
*dev
)
58 return (struct platram_info
*)platform_get_drvdata(dev
);
63 * call the platform device's set rw/ro control
69 static inline void platram_setrw(struct platram_info
*info
, int to
)
71 if (info
->pdata
== NULL
)
74 if (info
->pdata
->set_rw
!= NULL
)
75 (info
->pdata
->set_rw
)(info
->dev
, to
);
80 * called to remove the device from the driver's control
83 static int platram_remove(struct platform_device
*pdev
)
85 struct platram_info
*info
= to_platram_info(pdev
);
87 dev_dbg(&pdev
->dev
, "removing device\n");
93 mtd_device_unregister(info
->mtd
);
94 map_destroy(info
->mtd
);
97 /* ensure ram is left read-only */
99 platram_setrw(info
, PLATRAM_RO
);
101 /* release resources */
104 release_resource(info
->area
);
108 if (info
->map
.virt
!= NULL
)
109 iounmap(info
->map
.virt
);
118 * called from device drive system when a device matching our
122 static int platram_probe(struct platform_device
*pdev
)
124 struct platdata_mtd_ram
*pdata
;
125 struct platram_info
*info
;
126 struct resource
*res
;
129 dev_dbg(&pdev
->dev
, "probe entered\n");
131 if (dev_get_platdata(&pdev
->dev
) == NULL
) {
132 dev_err(&pdev
->dev
, "no platform data supplied\n");
137 pdata
= dev_get_platdata(&pdev
->dev
);
139 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
141 dev_err(&pdev
->dev
, "no memory for flash info\n");
146 platform_set_drvdata(pdev
, info
);
148 info
->dev
= &pdev
->dev
;
151 /* get the resource for the memory mapping */
153 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
156 dev_err(&pdev
->dev
, "no memory resource specified\n");
161 dev_dbg(&pdev
->dev
, "got platform resource %p (0x%llx)\n", res
,
162 (unsigned long long)res
->start
);
164 /* setup map parameters */
166 info
->map
.phys
= res
->start
;
167 info
->map
.size
= resource_size(res
);
168 info
->map
.name
= pdata
->mapname
!= NULL
?
169 (char *)pdata
->mapname
: (char *)pdev
->name
;
170 info
->map
.bankwidth
= pdata
->bankwidth
;
172 /* register our usage of the memory area */
174 info
->area
= request_mem_region(res
->start
, info
->map
.size
, pdev
->name
);
175 if (info
->area
== NULL
) {
176 dev_err(&pdev
->dev
, "failed to request memory region\n");
181 /* remap the memory area */
183 info
->map
.virt
= ioremap(res
->start
, info
->map
.size
);
184 dev_dbg(&pdev
->dev
, "virt %p, %lu bytes\n", info
->map
.virt
, info
->map
.size
);
186 if (info
->map
.virt
== NULL
) {
187 dev_err(&pdev
->dev
, "failed to ioremap() region\n");
192 simple_map_init(&info
->map
);
194 dev_dbg(&pdev
->dev
, "initialised map, probing for mtd\n");
196 /* probe for the right mtd map driver
197 * supplied by the platform_data struct */
199 if (pdata
->map_probes
) {
200 const char * const *map_probes
= pdata
->map_probes
;
202 for ( ; !info
->mtd
&& *map_probes
; map_probes
++)
203 info
->mtd
= do_map_probe(*map_probes
, &info
->map
);
205 /* fallback to map_ram */
207 info
->mtd
= do_map_probe("map_ram", &info
->map
);
209 if (info
->mtd
== NULL
) {
210 dev_err(&pdev
->dev
, "failed to probe for map_ram\n");
215 info
->mtd
->owner
= THIS_MODULE
;
216 info
->mtd
->dev
.parent
= &pdev
->dev
;
218 platram_setrw(info
, PLATRAM_RW
);
220 /* check to see if there are any available partitions, or whether
221 * to add this device whole */
223 err
= mtd_device_parse_register(info
->mtd
, pdata
->probes
, NULL
,
225 pdata
->nr_partitions
);
227 dev_info(&pdev
->dev
, "registered mtd device\n");
229 if (pdata
->nr_partitions
) {
230 /* add the whole device. */
231 err
= mtd_device_register(info
->mtd
, NULL
, 0);
234 "failed to register the entire device\n");
241 platram_remove(pdev
);
246 /* device driver info */
248 /* work with hotplug and coldplug */
249 MODULE_ALIAS("platform:mtd-ram");
251 static struct platform_driver platram_driver
= {
252 .probe
= platram_probe
,
253 .remove
= platram_remove
,
256 .owner
= THIS_MODULE
,
260 /* module init/exit */
262 static int __init
platram_init(void)
264 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
265 return platform_driver_register(&platram_driver
);
268 static void __exit
platram_exit(void)
270 platform_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");