Merge rsync://rsync.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[pandora-kernel.git] / drivers / mtd / maps / physmap.c
1 /*
2  * $Id: physmap.c,v 1.39 2005/11/29 14:49:36 gleixner Exp $
3  *
4  * Normal mappings of chips in physical memory
5  *
6  * Copyright (C) 2003 MontaVista Software Inc.
7  * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
8  *
9  * 031022 - [jsun] add run-time configure and partition setup
10  */
11
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>
24 #include <asm/io.h>
25
26 struct physmap_flash_info {
27         struct mtd_info         *mtd;
28         struct map_info         map;
29         struct resource         *res;
30 #ifdef CONFIG_MTD_PARTITIONS
31         int                     nr_parts;
32         struct mtd_partition    *parts;
33 #endif
34 };
35
36
37 static int physmap_flash_remove(struct platform_device *dev)
38 {
39         struct physmap_flash_info *info;
40         struct physmap_flash_data *physmap_data;
41
42         info = platform_get_drvdata(dev);
43         if (info == NULL)
44                 return 0;
45         platform_set_drvdata(dev, NULL);
46
47         physmap_data = dev->dev.platform_data;
48
49         if (info->mtd != NULL) {
50 #ifdef CONFIG_MTD_PARTITIONS
51                 if (info->nr_parts) {
52                         del_mtd_partitions(info->mtd);
53                         kfree(info->parts);
54                 } else if (physmap_data->nr_parts) {
55                         del_mtd_partitions(info->mtd);
56                 } else {
57                         del_mtd_device(info->mtd);
58                 }
59 #else
60                 del_mtd_device(info->mtd);
61 #endif
62                 map_destroy(info->mtd);
63         }
64
65         if (info->map.virt != NULL)
66                 iounmap((void *)info->map.virt);
67
68         if (info->res != NULL) {
69                 release_resource(info->res);
70                 kfree(info->res);
71         }
72
73         return 0;
74 }
75
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 };
79 #endif
80
81 static int physmap_flash_probe(struct platform_device *dev)
82 {
83         struct physmap_flash_data *physmap_data;
84         struct physmap_flash_info *info;
85         const char **probe_type;
86         int err;
87
88         physmap_data = dev->dev.platform_data;
89         if (physmap_data == NULL)
90                 return -ENODEV;
91
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);
95
96         info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
97         if (info == NULL) {
98                 err = -ENOMEM;
99                 goto err_out;
100         }
101         memset(info, 0, sizeof(*info));
102
103         platform_set_drvdata(dev, info);
104
105         info->res = request_mem_region(dev->resource->start,
106                         dev->resource->end - dev->resource->start + 1,
107                         dev->dev.bus_id);
108         if (info->res == NULL) {
109                 dev_err(&dev->dev, "Could not reserve memory region\n");
110                 err = -ENOMEM;
111                 goto err_out;
112         }
113
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;
119
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");
123                 err = EIO;
124                 goto err_out;
125         }
126
127         simple_map_init(&info->map);
128
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");
134                 err = -ENXIO;
135                 goto err_out;
136         }
137         info->mtd->owner = THIS_MODULE;
138
139 #ifdef CONFIG_MTD_PARTITIONS
140         err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
141         if (err > 0) {
142                 add_mtd_partitions(info->mtd, info->parts, err);
143                 return 0;
144         }
145
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);
150                 return 0;
151         }
152 #endif
153
154         add_mtd_device(info->mtd);
155         return 0;
156
157 err_out:
158         physmap_flash_remove(dev);
159         return err;
160 }
161
162 static struct platform_driver physmap_flash_driver = {
163         .probe          = physmap_flash_probe,
164         .remove         = physmap_flash_remove,
165         .driver         = {
166                 .name   = "physmap-flash",
167         },
168 };
169
170
171 #ifdef CONFIG_MTD_PHYSMAP_LEN
172 #if CONFIG_MTD_PHYSMAP_LEN != 0
173 #warning using PHYSMAP compat code
174 #define PHYSMAP_COMPAT
175 #endif
176 #endif
177
178 #ifdef PHYSMAP_COMPAT
179 static struct physmap_flash_data physmap_flash_data = {
180         .width          = CONFIG_MTD_PHYSMAP_BANKWIDTH,
181 };
182
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,
187 };
188
189 static struct platform_device physmap_flash = {
190         .name           = "physmap-flash",
191         .id             = 0,
192         .dev            = {
193                 .platform_data  = &physmap_flash_data,
194         },
195         .num_resources  = 1,
196         .resource       = &physmap_flash_resource,
197 };
198
199 void physmap_configure(unsigned long addr, unsigned long size,
200                 int bankwidth, void (*set_vpp)(struct map_info *, int))
201 {
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;
206 }
207
208 #ifdef CONFIG_MTD_PARTITIONS
209 void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
210 {
211         physmap_flash_data.nr_parts = num_parts;
212         physmap_flash_data.parts = parts;
213 }
214 #endif
215 #endif
216
217 static int __init physmap_init(void)
218 {
219         int err;
220
221         err = platform_driver_register(&physmap_flash_driver);
222 #ifdef PHYSMAP_COMPAT
223         if (err == 0)
224                 platform_device_register(&physmap_flash);
225 #endif
226
227         return err;
228 }
229
230 static void __exit physmap_exit(void)
231 {
232 #ifdef PHYSMAP_COMPAT
233         platform_device_unregister(&physmap_flash);
234 #endif
235         platform_driver_unregister(&physmap_flash_driver);
236 }
237
238 module_init(physmap_init);
239 module_exit(physmap_exit);
240
241 MODULE_LICENSE("GPL");
242 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
243 MODULE_DESCRIPTION("Generic configurable MTD map driver");