2 * $Id: physmap.c,v 1.39 2005/11/29 14:49:36 gleixner Exp $
4 * Normal mappings of chips in physical memory
6 * Copyright (C) 2003 MontaVista Software Inc.
7 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
9 * 031022 - [jsun] add run-time configure and partition setup
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/map.h>
21 #include <linux/config.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/physmap.h>
26 struct physmap_flash_info {
30 #ifdef CONFIG_MTD_PARTITIONS
32 struct mtd_partition *parts;
37 static int physmap_flash_remove(struct platform_device *dev)
39 struct physmap_flash_info *info;
40 struct physmap_flash_data *physmap_data;
42 info = platform_get_drvdata(dev);
45 platform_set_drvdata(dev, NULL);
47 physmap_data = dev->dev.platform_data;
49 if (info->mtd != NULL) {
50 #ifdef CONFIG_MTD_PARTITIONS
52 del_mtd_partitions(info->mtd);
54 } else if (physmap_data->nr_parts) {
55 del_mtd_partitions(info->mtd);
57 del_mtd_device(info->mtd);
60 del_mtd_device(info->mtd);
62 map_destroy(info->mtd);
65 if (info->map.virt != NULL)
66 iounmap((void *)info->map.virt);
68 if (info->res != NULL) {
69 release_resource(info->res);
76 static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
77 #ifdef CONFIG_MTD_PARTITIONS
78 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
81 static int physmap_flash_probe(struct platform_device *dev)
83 struct physmap_flash_data *physmap_data;
84 struct physmap_flash_info *info;
85 const char **probe_type;
88 physmap_data = dev->dev.platform_data;
89 if (physmap_data == NULL)
92 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
93 (unsigned long long)dev->resource->end - dev->resource->start + 1,
94 (unsigned long long)dev->resource->start);
96 info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
101 memset(info, 0, sizeof(*info));
103 platform_set_drvdata(dev, info);
105 info->res = request_mem_region(dev->resource->start,
106 dev->resource->end - dev->resource->start + 1,
108 if (info->res == NULL) {
109 dev_err(&dev->dev, "Could not reserve memory region\n");
114 info->map.name = dev->dev.bus_id;
115 info->map.phys = dev->resource->start;
116 info->map.size = dev->resource->end - dev->resource->start + 1;
117 info->map.bankwidth = physmap_data->width;
118 info->map.set_vpp = physmap_data->set_vpp;
120 info->map.virt = ioremap(info->map.phys, info->map.size);
121 if (info->map.virt == NULL) {
122 dev_err(&dev->dev, "Failed to ioremap flash region\n");
127 simple_map_init(&info->map);
129 probe_type = rom_probe_types;
130 for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
131 info->mtd = do_map_probe(*probe_type, &info->map);
132 if (info->mtd == NULL) {
133 dev_err(&dev->dev, "map_probe failed\n");
137 info->mtd->owner = THIS_MODULE;
139 #ifdef CONFIG_MTD_PARTITIONS
140 err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
142 add_mtd_partitions(info->mtd, info->parts, err);
146 if (physmap_data->nr_parts) {
147 printk(KERN_NOTICE "Using physmap partition information\n");
148 add_mtd_partitions(info->mtd, physmap_data->parts,
149 physmap_data->nr_parts);
154 add_mtd_device(info->mtd);
158 physmap_flash_remove(dev);
162 static struct platform_driver physmap_flash_driver = {
163 .probe = physmap_flash_probe,
164 .remove = physmap_flash_remove,
166 .name = "physmap-flash",
171 #ifdef CONFIG_MTD_PHYSMAP_LEN
172 #if CONFIG_MTD_PHYSMAP_LEN != 0
173 #warning using PHYSMAP compat code
174 #define PHYSMAP_COMPAT
178 #ifdef PHYSMAP_COMPAT
179 static struct physmap_flash_data physmap_flash_data = {
180 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
183 static struct resource physmap_flash_resource = {
184 .start = CONFIG_MTD_PHYSMAP_START,
185 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN,
186 .flags = IORESOURCE_MEM,
189 static struct platform_device physmap_flash = {
190 .name = "physmap-flash",
193 .platform_data = &physmap_flash_data,
196 .resource = &physmap_flash_resource,
199 void physmap_configure(unsigned long addr, unsigned long size,
200 int bankwidth, void (*set_vpp)(struct map_info *, int))
202 physmap_flash_resource.start = addr;
203 physmap_flash_resource.end = addr + size - 1;
204 physmap_flash_data.width = bankwidth;
205 physmap_flash_data.set_vpp = set_vpp;
208 #ifdef CONFIG_MTD_PARTITIONS
209 void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
211 physmap_flash_data.nr_parts = num_parts;
212 physmap_flash_data.parts = parts;
217 static int __init physmap_init(void)
221 err = platform_driver_register(&physmap_flash_driver);
222 #ifdef PHYSMAP_COMPAT
224 platform_device_register(&physmap_flash);
230 static void __exit physmap_exit(void)
232 #ifdef PHYSMAP_COMPAT
233 platform_device_unregister(&physmap_flash);
235 platform_driver_unregister(&physmap_flash_driver);
238 module_init(physmap_init);
239 module_exit(physmap_exit);
241 MODULE_LICENSE("GPL");
242 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
243 MODULE_DESCRIPTION("Generic configurable MTD map driver");