Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/djbw/async_tx
[linux-2.6] / drivers / mtd / maps / physmap.c
1 /*
2  * Normal mappings of chips in physical memory
3  *
4  * Copyright (C) 2003 MontaVista Software Inc.
5  * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
6  *
7  * 031022 - [jsun] add run-time configure and partition setup
8  */
9
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/map.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/mtd/physmap.h>
21 #include <linux/mtd/concat.h>
22 #include <linux/io.h>
23
24 #define MAX_RESOURCES           4
25
26 struct physmap_flash_info {
27         struct mtd_info         *mtd[MAX_RESOURCES];
28         struct mtd_info         *cmtd;
29         struct map_info         map[MAX_RESOURCES];
30 #ifdef CONFIG_MTD_PARTITIONS
31         int                     nr_parts;
32         struct mtd_partition    *parts;
33 #endif
34 };
35
36 static int physmap_flash_remove(struct platform_device *dev)
37 {
38         struct physmap_flash_info *info;
39         struct physmap_flash_data *physmap_data;
40         int i;
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 #ifdef CONFIG_MTD_CONCAT
50         if (info->cmtd != info->mtd[0]) {
51                 del_mtd_device(info->cmtd);
52                 mtd_concat_destroy(info->cmtd);
53         }
54 #endif
55
56         for (i = 0; i < MAX_RESOURCES; i++) {
57                 if (info->mtd[i] != NULL) {
58 #ifdef CONFIG_MTD_PARTITIONS
59                         if (info->nr_parts) {
60                                 del_mtd_partitions(info->mtd[i]);
61                                 kfree(info->parts);
62                         } else if (physmap_data->nr_parts) {
63                                 del_mtd_partitions(info->mtd[i]);
64                         } else {
65                                 del_mtd_device(info->mtd[i]);
66                         }
67 #else
68                         del_mtd_device(info->mtd[i]);
69 #endif
70                         map_destroy(info->mtd[i]);
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 = 0;
87         int i;
88         int devices_found = 0;
89
90         physmap_data = dev->dev.platform_data;
91         if (physmap_data == NULL)
92                 return -ENODEV;
93
94         info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
95                             GFP_KERNEL);
96         if (info == NULL) {
97                 err = -ENOMEM;
98                 goto err_out;
99         }
100
101         platform_set_drvdata(dev, info);
102
103         for (i = 0; i < dev->num_resources; i++) {
104                 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
105                        (unsigned long long)(dev->resource[i].end - dev->resource[i].start + 1),
106                        (unsigned long long)dev->resource[i].start);
107
108                 if (!devm_request_mem_region(&dev->dev,
109                         dev->resource[i].start,
110                         dev->resource[i].end - dev->resource[i].start + 1,
111                         dev_name(&dev->dev))) {
112                         dev_err(&dev->dev, "Could not reserve memory region\n");
113                         err = -ENOMEM;
114                         goto err_out;
115                 }
116
117                 info->map[i].name = dev_name(&dev->dev);
118                 info->map[i].phys = dev->resource[i].start;
119                 info->map[i].size = dev->resource[i].end - dev->resource[i].start + 1;
120                 info->map[i].bankwidth = physmap_data->width;
121                 info->map[i].set_vpp = physmap_data->set_vpp;
122
123                 info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
124                                                  info->map[i].size);
125                 if (info->map[i].virt == NULL) {
126                         dev_err(&dev->dev, "Failed to ioremap flash region\n");
127                         err = EIO;
128                         goto err_out;
129                 }
130
131                 simple_map_init(&info->map[i]);
132
133                 probe_type = rom_probe_types;
134                 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
135                         info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
136                 if (info->mtd[i] == NULL) {
137                         dev_err(&dev->dev, "map_probe failed\n");
138                         err = -ENXIO;
139                         goto err_out;
140                 } else {
141                         devices_found++;
142                 }
143                 info->mtd[i]->owner = THIS_MODULE;
144         }
145
146         if (devices_found == 1) {
147                 info->cmtd = info->mtd[0];
148         } else if (devices_found > 1) {
149                 /*
150                  * We detected multiple devices. Concatenate them together.
151                  */
152 #ifdef CONFIG_MTD_CONCAT
153                 info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
154                 if (info->cmtd == NULL)
155                         err = -ENXIO;
156 #else
157                 printk(KERN_ERR "physmap-flash: multiple devices "
158                        "found but MTD concat support disabled.\n");
159                 err = -ENXIO;
160 #endif
161         }
162         if (err)
163                 goto err_out;
164
165 #ifdef CONFIG_MTD_PARTITIONS
166         err = parse_mtd_partitions(info->cmtd, part_probe_types, &info->parts, 0);
167         if (err > 0) {
168                 add_mtd_partitions(info->cmtd, info->parts, err);
169                 return 0;
170         }
171
172         if (physmap_data->nr_parts) {
173                 printk(KERN_NOTICE "Using physmap partition information\n");
174                 add_mtd_partitions(info->cmtd, physmap_data->parts,
175                                    physmap_data->nr_parts);
176                 return 0;
177         }
178 #endif
179
180         add_mtd_device(info->cmtd);
181         return 0;
182
183 err_out:
184         physmap_flash_remove(dev);
185         return err;
186 }
187
188 #ifdef CONFIG_PM
189 static int physmap_flash_suspend(struct platform_device *dev, pm_message_t state)
190 {
191         struct physmap_flash_info *info = platform_get_drvdata(dev);
192         int ret = 0;
193         int i;
194
195         for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
196                 if (info->mtd[i]->suspend) {
197                         ret = info->mtd[i]->suspend(info->mtd[i]);
198                         if (ret)
199                                 goto fail;
200                 }
201
202         return 0;
203 fail:
204         for (--i; i >= 0; --i)
205                 if (info->mtd[i]->suspend) {
206                         BUG_ON(!info->mtd[i]->resume);
207                         info->mtd[i]->resume(info->mtd[i]);
208                 }
209
210         return ret;
211 }
212
213 static int physmap_flash_resume(struct platform_device *dev)
214 {
215         struct physmap_flash_info *info = platform_get_drvdata(dev);
216         int i;
217
218         for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
219                 if (info->mtd[i]->resume)
220                         info->mtd[i]->resume(info->mtd[i]);
221
222         return 0;
223 }
224
225 static void physmap_flash_shutdown(struct platform_device *dev)
226 {
227         struct physmap_flash_info *info = platform_get_drvdata(dev);
228         int i;
229
230         for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
231                 if (info->mtd[i]->suspend && info->mtd[i]->resume)
232                         if (info->mtd[i]->suspend(info->mtd[i]) == 0)
233                                 info->mtd[i]->resume(info->mtd[i]);
234 }
235 #else
236 #define physmap_flash_suspend NULL
237 #define physmap_flash_resume NULL
238 #define physmap_flash_shutdown NULL
239 #endif
240
241 static struct platform_driver physmap_flash_driver = {
242         .probe          = physmap_flash_probe,
243         .remove         = physmap_flash_remove,
244         .suspend        = physmap_flash_suspend,
245         .resume         = physmap_flash_resume,
246         .shutdown       = physmap_flash_shutdown,
247         .driver         = {
248                 .name   = "physmap-flash",
249                 .owner  = THIS_MODULE,
250         },
251 };
252
253
254 #ifdef CONFIG_MTD_PHYSMAP_LEN
255 #if CONFIG_MTD_PHYSMAP_LEN != 0
256 #warning using PHYSMAP compat code
257 #define PHYSMAP_COMPAT
258 #endif
259 #endif
260
261 #ifdef PHYSMAP_COMPAT
262 static struct physmap_flash_data physmap_flash_data = {
263         .width          = CONFIG_MTD_PHYSMAP_BANKWIDTH,
264 };
265
266 static struct resource physmap_flash_resource = {
267         .start          = CONFIG_MTD_PHYSMAP_START,
268         .end            = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
269         .flags          = IORESOURCE_MEM,
270 };
271
272 static struct platform_device physmap_flash = {
273         .name           = "physmap-flash",
274         .id             = 0,
275         .dev            = {
276                 .platform_data  = &physmap_flash_data,
277         },
278         .num_resources  = 1,
279         .resource       = &physmap_flash_resource,
280 };
281
282 void physmap_configure(unsigned long addr, unsigned long size,
283                 int bankwidth, void (*set_vpp)(struct map_info *, int))
284 {
285         physmap_flash_resource.start = addr;
286         physmap_flash_resource.end = addr + size - 1;
287         physmap_flash_data.width = bankwidth;
288         physmap_flash_data.set_vpp = set_vpp;
289 }
290
291 #ifdef CONFIG_MTD_PARTITIONS
292 void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
293 {
294         physmap_flash_data.nr_parts = num_parts;
295         physmap_flash_data.parts = parts;
296 }
297 #endif
298 #endif
299
300 static int __init physmap_init(void)
301 {
302         int err;
303
304         err = platform_driver_register(&physmap_flash_driver);
305 #ifdef PHYSMAP_COMPAT
306         if (err == 0)
307                 platform_device_register(&physmap_flash);
308 #endif
309
310         return err;
311 }
312
313 static void __exit physmap_exit(void)
314 {
315 #ifdef PHYSMAP_COMPAT
316         platform_device_unregister(&physmap_flash);
317 #endif
318         platform_driver_unregister(&physmap_flash_driver);
319 }
320
321 module_init(physmap_init);
322 module_exit(physmap_exit);
323
324 MODULE_LICENSE("GPL");
325 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
326 MODULE_DESCRIPTION("Generic configurable MTD map driver");
327
328 /* legacy platform drivers can't hotplug or coldplg */
329 #ifndef PHYSMAP_COMPAT
330 /* work with hotplug and coldplug */
331 MODULE_ALIAS("platform:physmap-flash");
332 #endif
333