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/mtd/partitions.h>
22 #include <linux/mtd/physmap.h>
25 struct physmap_flash_info {
29 #ifdef CONFIG_MTD_PARTITIONS
31 struct mtd_partition *parts;
36 static int physmap_flash_remove(struct platform_device *dev)
38 struct physmap_flash_info *info;
39 struct physmap_flash_data *physmap_data;
41 info = platform_get_drvdata(dev);
44 platform_set_drvdata(dev, NULL);
46 physmap_data = dev->dev.platform_data;
48 if (info->mtd != NULL) {
49 #ifdef CONFIG_MTD_PARTITIONS
51 del_mtd_partitions(info->mtd);
53 } else if (physmap_data->nr_parts) {
54 del_mtd_partitions(info->mtd);
56 del_mtd_device(info->mtd);
59 del_mtd_device(info->mtd);
61 map_destroy(info->mtd);
64 if (info->map.virt != NULL)
65 iounmap((void *)info->map.virt);
67 if (info->res != NULL) {
68 release_resource(info->res);
75 static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
76 #ifdef CONFIG_MTD_PARTITIONS
77 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
80 static int physmap_flash_probe(struct platform_device *dev)
82 struct physmap_flash_data *physmap_data;
83 struct physmap_flash_info *info;
84 const char **probe_type;
87 physmap_data = dev->dev.platform_data;
88 if (physmap_data == NULL)
91 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
92 (unsigned long long)dev->resource->end - dev->resource->start + 1,
93 (unsigned long long)dev->resource->start);
95 info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
100 memset(info, 0, sizeof(*info));
102 platform_set_drvdata(dev, info);
104 info->res = request_mem_region(dev->resource->start,
105 dev->resource->end - dev->resource->start + 1,
107 if (info->res == NULL) {
108 dev_err(&dev->dev, "Could not reserve memory region\n");
113 info->map.name = dev->dev.bus_id;
114 info->map.phys = dev->resource->start;
115 info->map.size = dev->resource->end - dev->resource->start + 1;
116 info->map.bankwidth = physmap_data->width;
117 info->map.set_vpp = physmap_data->set_vpp;
119 info->map.virt = ioremap(info->map.phys, info->map.size);
120 if (info->map.virt == NULL) {
121 dev_err(&dev->dev, "Failed to ioremap flash region\n");
126 simple_map_init(&info->map);
128 probe_type = rom_probe_types;
129 for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
130 info->mtd = do_map_probe(*probe_type, &info->map);
131 if (info->mtd == NULL) {
132 dev_err(&dev->dev, "map_probe failed\n");
136 info->mtd->owner = THIS_MODULE;
138 #ifdef CONFIG_MTD_PARTITIONS
139 err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
141 add_mtd_partitions(info->mtd, info->parts, err);
145 if (physmap_data->nr_parts) {
146 printk(KERN_NOTICE "Using physmap partition information\n");
147 add_mtd_partitions(info->mtd, physmap_data->parts,
148 physmap_data->nr_parts);
153 add_mtd_device(info->mtd);
157 physmap_flash_remove(dev);
161 static struct platform_driver physmap_flash_driver = {
162 .probe = physmap_flash_probe,
163 .remove = physmap_flash_remove,
165 .name = "physmap-flash",
170 #ifdef CONFIG_MTD_PHYSMAP_LEN
171 #if CONFIG_MTD_PHYSMAP_LEN != 0
172 #warning using PHYSMAP compat code
173 #define PHYSMAP_COMPAT
177 #ifdef PHYSMAP_COMPAT
178 static struct physmap_flash_data physmap_flash_data = {
179 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
182 static struct resource physmap_flash_resource = {
183 .start = CONFIG_MTD_PHYSMAP_START,
184 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
185 .flags = IORESOURCE_MEM,
188 static struct platform_device physmap_flash = {
189 .name = "physmap-flash",
192 .platform_data = &physmap_flash_data,
195 .resource = &physmap_flash_resource,
198 void physmap_configure(unsigned long addr, unsigned long size,
199 int bankwidth, void (*set_vpp)(struct map_info *, int))
201 physmap_flash_resource.start = addr;
202 physmap_flash_resource.end = addr + size - 1;
203 physmap_flash_data.width = bankwidth;
204 physmap_flash_data.set_vpp = set_vpp;
207 #ifdef CONFIG_MTD_PARTITIONS
208 void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
210 physmap_flash_data.nr_parts = num_parts;
211 physmap_flash_data.parts = parts;
216 static int __init physmap_init(void)
220 err = platform_driver_register(&physmap_flash_driver);
221 #ifdef PHYSMAP_COMPAT
223 platform_device_register(&physmap_flash);
229 static void __exit physmap_exit(void)
231 #ifdef PHYSMAP_COMPAT
232 platform_device_unregister(&physmap_flash);
234 platform_driver_unregister(&physmap_flash_driver);
237 module_init(physmap_init);
238 module_exit(physmap_exit);
240 MODULE_LICENSE("GPL");
241 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
242 MODULE_DESCRIPTION("Generic configurable MTD map driver");