[MTD] Convert physmap to platform driver
[safe/jmp/linux-2.6] / 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 #include <asm/mach/flash.h>
26
27 struct physmap_flash_info {
28         struct mtd_info         *mtd;
29         struct map_info         map;
30         struct resource         *res;
31 #ifdef CONFIG_MTD_PARTITIONS
32         int                     nr_parts;
33         struct mtd_partition    *parts;
34 #endif
35 };
36
37
38 static int physmap_flash_remove(struct platform_device *dev)
39 {
40         struct physmap_flash_info *info;
41         struct physmap_flash_data *physmap_data;
42
43         info = platform_get_drvdata(dev);
44         if (info == NULL)
45                 return 0;
46         platform_set_drvdata(dev, NULL);
47
48         physmap_data = dev->dev.platform_data;
49
50         if (info->mtd != NULL) {
51 #ifdef CONFIG_MTD_PARTITIONS
52                 if (info->nr_parts) {
53                         del_mtd_partitions(info->mtd);
54                         kfree(info->parts);
55                 } else if (physmap_data->nr_parts) {
56                         del_mtd_partitions(info->mtd);
57                 } else {
58                         del_mtd_device(info->mtd);
59                 }
60 #else
61                 del_mtd_device(info->mtd);
62 #endif
63                 map_destroy(info->mtd);
64         }
65
66         if (info->map.virt != NULL)
67                 iounmap((void *)info->map.virt);
68
69         if (info->res != NULL) {
70                 release_resource(info->res);
71                 kfree(info->res);
72         }
73
74         return 0;
75 }
76
77 static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
78 #ifdef CONFIG_MTD_PARTITIONS
79 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
80 #endif
81
82 static int physmap_flash_probe(struct platform_device *dev)
83 {
84         struct physmap_flash_data *physmap_data;
85         struct physmap_flash_info *info;
86         const char **probe_type;
87         int err;
88
89         physmap_data = dev->dev.platform_data;
90         if (physmap_data == NULL)
91                 return -ENODEV;
92
93         printk(KERN_NOTICE "physmap platform flash device: %.8lx at %.8lx\n",
94                 dev->resource->end - dev->resource->start + 1,
95                 dev->resource->start);
96
97         info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
98         if (info == NULL) {
99                 err = -ENOMEM;
100                 goto err_out;
101         }
102         memset(info, 0, sizeof(*info));
103
104         platform_set_drvdata(dev, info);
105
106         info->res = request_mem_region(dev->resource->start,
107                         dev->resource->end - dev->resource->start + 1,
108                         dev->dev.bus_id);
109         if (info->res == NULL) {
110                 dev_err(&dev->dev, "Could not reserve memory region\n");
111                 err = -ENOMEM;
112                 goto err_out;
113         }
114
115         info->map.name = dev->dev.bus_id;
116         info->map.phys = dev->resource->start;
117         info->map.size = dev->resource->end - dev->resource->start + 1;
118         info->map.bankwidth = physmap_data->width;
119         info->map.set_vpp = physmap_data->set_vpp;
120
121         info->map.virt = ioremap(info->map.phys, info->map.size);
122         if (info->map.virt == NULL) {
123                 dev_err(&dev->dev, "Failed to ioremap flash region\n");
124                 err = EIO;
125                 goto err_out;
126         }
127
128         simple_map_init(&info->map);
129
130         probe_type = rom_probe_types;
131         for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
132                 info->mtd = do_map_probe(*probe_type, &info->map);
133         if (info->mtd == NULL) {
134                 dev_err(&dev->dev, "map_probe failed\n");
135                 err = -ENXIO;
136                 goto err_out;
137         }
138         info->mtd->owner = THIS_MODULE;
139
140 #ifdef CONFIG_MTD_PARTITIONS
141         err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
142         if (err > 0) {
143                 add_mtd_partitions(info->mtd, info->parts, err);
144                 return 0;
145         }
146
147         if (physmap_data->nr_parts) {
148                 printk(KERN_NOTICE "Using physmap partition information\n");
149                 add_mtd_partitions(info->mtd, physmap_data->parts,
150                                                 physmap_data->nr_parts);
151                 return 0;
152         }
153 #endif
154
155         add_mtd_device(info->mtd);
156         return 0;
157
158 err_out:
159         physmap_flash_remove(dev);
160         return err;
161 }
162
163 static struct platform_driver physmap_flash_driver = {
164         .probe          = physmap_flash_probe,
165         .remove         = physmap_flash_remove,
166         .driver         = {
167                 .name   = "physmap-flash",
168         },
169 };
170
171
172 #ifdef CONFIG_MTD_PHYSMAP_LEN
173 #if CONFIG_MTD_PHYSMAP_LEN != 0
174 #warning using PHYSMAP compat code
175 #define PHYSMAP_COMPAT
176 #endif
177 #endif
178
179 #ifdef PHYSMAP_COMPAT
180 static struct physmap_flash_data physmap_flash_data = {
181         .width          = CONFIG_MTD_PHYSMAP_BANKWIDTH,
182 };
183
184 static struct resource physmap_flash_resource = {
185         .start          = CONFIG_MTD_PHYSMAP_START,
186         .end            = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN,
187         .flags          = IORESOURCE_MEM,
188 };
189
190 static struct platform_device physmap_flash = {
191         .name           = "physmap-flash",
192         .id             = 0,
193         .dev            = {
194                 .platform_data  = &physmap_flash_data,
195         },
196         .num_resources  = 1,
197         .resource       = &physmap_flash_resource,
198 };
199
200 void physmap_configure(unsigned long addr, unsigned long size,
201                 int bankwidth, void (*set_vpp)(struct map_info *, int))
202 {
203         physmap_flash_resource.start = addr;
204         physmap_flash_resource.end = addr + size - 1;
205         physmap_flash_data.width = bankwidth;
206         physmap_flash_data.set_vpp = set_vpp;
207 }
208
209 #ifdef CONFIG_MTD_PARTITIONS
210 void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
211 {
212         physmap_flash_data.nr_parts = num_parts;
213         physmap_flash_data.parts = parts;
214 }
215 #endif
216 #endif
217
218 static int __init physmap_init(void)
219 {
220         int err;
221
222         err = platform_driver_register(&physmap_flash_driver);
223 #ifdef PHYSMAP_COMPAT
224         if (err == 0)
225                 platform_device_register(&physmap_flash);
226 #endif
227
228         return err;
229 }
230
231 static void __exit physmap_exit(void)
232 {
233 #ifdef PHYSMAP_COMPAT
234         platform_device_unregister(&physmap_flash);
235 #endif
236         platform_driver_unregister(&physmap_flash_driver);
237 }
238
239 module_init(physmap_init);
240 module_exit(physmap_exit);
241
242 MODULE_LICENSE("GPL");
243 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
244 MODULE_DESCRIPTION("Generic configurable MTD map driver");