[MTD] onenand: Add panic_write function to the onenand driver
[deliverable/linux.git] / drivers / mtd / maps / physmap.c
CommitLineData
1da177e4 1/*
2b9175c1 2 * $Id: physmap.c,v 1.39 2005/11/29 14:49:36 gleixner Exp $
1da177e4
LT
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>
73566edf
LB
17#include <linux/device.h>
18#include <linux/platform_device.h>
1da177e4
LT
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/map.h>
1da177e4 21#include <linux/mtd/partitions.h>
2b9175c1 22#include <linux/mtd/physmap.h>
df66e716 23#include <linux/mtd/concat.h>
73566edf 24#include <asm/io.h>
1da177e4 25
df66e716
SR
26#define MAX_RESOURCES 4
27
73566edf 28struct physmap_flash_info {
df66e716
SR
29 struct mtd_info *mtd[MAX_RESOURCES];
30 struct mtd_info *cmtd;
31 struct map_info map[MAX_RESOURCES];
73566edf
LB
32 struct resource *res;
33#ifdef CONFIG_MTD_PARTITIONS
34 int nr_parts;
35 struct mtd_partition *parts;
36#endif
1da177e4
LT
37};
38
73566edf
LB
39static int physmap_flash_remove(struct platform_device *dev)
40{
41 struct physmap_flash_info *info;
42 struct physmap_flash_data *physmap_data;
df66e716 43 int i;
73566edf
LB
44
45 info = platform_get_drvdata(dev);
46 if (info == NULL)
47 return 0;
48 platform_set_drvdata(dev, NULL);
49
50 physmap_data = dev->dev.platform_data;
51
df66e716
SR
52#ifdef CONFIG_MTD_CONCAT
53 if (info->cmtd != info->mtd[0]) {
54 del_mtd_device(info->cmtd);
55 mtd_concat_destroy(info->cmtd);
56 }
57#endif
58
59 for (i = 0; i < MAX_RESOURCES; i++) {
60 if (info->mtd[i] != NULL) {
1da177e4 61#ifdef CONFIG_MTD_PARTITIONS
df66e716
SR
62 if (info->nr_parts) {
63 del_mtd_partitions(info->mtd[i]);
64 kfree(info->parts);
65 } else if (physmap_data->nr_parts) {
66 del_mtd_partitions(info->mtd[i]);
67 } else {
68 del_mtd_device(info->mtd[i]);
69 }
73566edf 70#else
df66e716 71 del_mtd_device(info->mtd[i]);
73566edf 72#endif
df66e716
SR
73 map_destroy(info->mtd[i]);
74 }
1da177e4 75
df66e716
SR
76 if (info->map[i].virt != NULL)
77 iounmap(info->map[i].virt);
78 }
1da177e4 79
73566edf
LB
80 if (info->res != NULL) {
81 release_resource(info->res);
82 kfree(info->res);
83 }
1da177e4 84
73566edf 85 return 0;
1da177e4 86}
1da177e4 87
73566edf
LB
88static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
89#ifdef CONFIG_MTD_PARTITIONS
90static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
91#endif
92
93static int physmap_flash_probe(struct platform_device *dev)
1da177e4 94{
73566edf
LB
95 struct physmap_flash_data *physmap_data;
96 struct physmap_flash_info *info;
97 const char **probe_type;
df66e716
SR
98 int err = 0;
99 int i;
100 int devices_found = 0;
73566edf
LB
101
102 physmap_data = dev->dev.platform_data;
103 if (physmap_data == NULL)
104 return -ENODEV;
105
95b93a0c 106 info = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
73566edf
LB
107 if (info == NULL) {
108 err = -ENOMEM;
109 goto err_out;
110 }
1da177e4 111
73566edf 112 platform_set_drvdata(dev, info);
1da177e4 113
df66e716
SR
114 for (i = 0; i < dev->num_resources; i++) {
115 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
116 (unsigned long long)(dev->resource[i].end - dev->resource[i].start + 1),
117 (unsigned long long)dev->resource[i].start);
118
119 info->res = request_mem_region(dev->resource[i].start,
120 dev->resource[i].end - dev->resource[i].start + 1,
121 dev->dev.bus_id);
122 if (info->res == NULL) {
123 dev_err(&dev->dev, "Could not reserve memory region\n");
124 err = -ENOMEM;
125 goto err_out;
126 }
1da177e4 127
df66e716
SR
128 info->map[i].name = dev->dev.bus_id;
129 info->map[i].phys = dev->resource[i].start;
130 info->map[i].size = dev->resource[i].end - dev->resource[i].start + 1;
131 info->map[i].bankwidth = physmap_data->width;
132 info->map[i].set_vpp = physmap_data->set_vpp;
133
134 info->map[i].virt = ioremap(info->map[i].phys, info->map[i].size);
135 if (info->map[i].virt == NULL) {
136 dev_err(&dev->dev, "Failed to ioremap flash region\n");
137 err = EIO;
138 goto err_out;
139 }
73566edf 140
df66e716 141 simple_map_init(&info->map[i]);
1da177e4 142
df66e716
SR
143 probe_type = rom_probe_types;
144 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
145 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
146 if (info->mtd[i] == NULL) {
147 dev_err(&dev->dev, "map_probe failed\n");
148 err = -ENXIO;
149 goto err_out;
150 } else {
151 devices_found++;
152 }
153 info->mtd[i]->owner = THIS_MODULE;
154 }
73566edf 155
df66e716
SR
156 if (devices_found == 1) {
157 info->cmtd = info->mtd[0];
158 } else if (devices_found > 1) {
159 /*
160 * We detected multiple devices. Concatenate them together.
161 */
162#ifdef CONFIG_MTD_CONCAT
163 info->cmtd = mtd_concat_create(info->mtd, devices_found, dev->dev.bus_id);
164 if (info->cmtd == NULL)
165 err = -ENXIO;
166#else
167 printk(KERN_ERR "physmap-flash: multiple devices "
168 "found but MTD concat support disabled.\n");
73566edf 169 err = -ENXIO;
df66e716 170#endif
1da177e4 171 }
df66e716
SR
172 if (err)
173 goto err_out;
1da177e4
LT
174
175#ifdef CONFIG_MTD_PARTITIONS
df66e716 176 err = parse_mtd_partitions(info->cmtd, part_probe_types, &info->parts, 0);
73566edf 177 if (err > 0) {
df66e716 178 add_mtd_partitions(info->cmtd, info->parts, err);
73566edf
LB
179 return 0;
180 }
1da177e4 181
73566edf
LB
182 if (physmap_data->nr_parts) {
183 printk(KERN_NOTICE "Using physmap partition information\n");
df66e716
SR
184 add_mtd_partitions(info->cmtd, physmap_data->parts,
185 physmap_data->nr_parts);
73566edf
LB
186 return 0;
187 }
188#endif
189
df66e716 190 add_mtd_device(info->cmtd);
73566edf
LB
191 return 0;
192
193err_out:
194 physmap_flash_remove(dev);
195 return err;
196}
197
17c2dae3
LB
198#ifdef CONFIG_PM
199static int physmap_flash_suspend(struct platform_device *dev, pm_message_t state)
200{
201 struct physmap_flash_info *info = platform_get_drvdata(dev);
202 int ret = 0;
df66e716 203 int i;
17c2dae3
LB
204
205 if (info)
df66e716
SR
206 for (i = 0; i < MAX_RESOURCES; i++)
207 ret |= info->mtd[i].suspend(info->mtd[i]);
17c2dae3
LB
208
209 return ret;
210}
211
212static int physmap_flash_resume(struct platform_device *dev)
213{
214 struct physmap_flash_info *info = platform_get_drvdata(dev);
df66e716
SR
215 int i;
216
17c2dae3 217 if (info)
df66e716
SR
218 for (i = 0; i < MAX_RESOURCES; i++)
219 info->mtd[i].resume(info->mtd[i]);
17c2dae3
LB
220 return 0;
221}
222
223static void physmap_flash_shutdown(struct platform_device *dev)
224{
225 struct physmap_flash_info *info = platform_get_drvdata(dev);
df66e716
SR
226 int i;
227
228 for (i = 0; i < MAX_RESOURCES; i++)
229 if (info && info->mtd[i].suspend(info->mtd[i]) == 0)
230 info->mtd[i].resume(info->mtd[i]);
17c2dae3
LB
231}
232#endif
233
73566edf
LB
234static struct platform_driver physmap_flash_driver = {
235 .probe = physmap_flash_probe,
236 .remove = physmap_flash_remove,
17c2dae3
LB
237#ifdef CONFIG_PM
238 .suspend = physmap_flash_suspend,
239 .resume = physmap_flash_resume,
240 .shutdown = physmap_flash_shutdown,
241#endif
73566edf
LB
242 .driver = {
243 .name = "physmap-flash",
244 },
245};
1da177e4 246
1da177e4 247
73566edf
LB
248#ifdef CONFIG_MTD_PHYSMAP_LEN
249#if CONFIG_MTD_PHYSMAP_LEN != 0
250#warning using PHYSMAP compat code
251#define PHYSMAP_COMPAT
252#endif
1da177e4 253#endif
1da177e4 254
73566edf
LB
255#ifdef PHYSMAP_COMPAT
256static struct physmap_flash_data physmap_flash_data = {
257 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
258};
1da177e4 259
73566edf
LB
260static struct resource physmap_flash_resource = {
261 .start = CONFIG_MTD_PHYSMAP_START,
6d4f8224 262 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
73566edf
LB
263 .flags = IORESOURCE_MEM,
264};
1da177e4 265
73566edf
LB
266static struct platform_device physmap_flash = {
267 .name = "physmap-flash",
268 .id = 0,
269 .dev = {
270 .platform_data = &physmap_flash_data,
271 },
272 .num_resources = 1,
273 .resource = &physmap_flash_resource,
274};
275
276void physmap_configure(unsigned long addr, unsigned long size,
277 int bankwidth, void (*set_vpp)(struct map_info *, int))
1da177e4 278{
73566edf
LB
279 physmap_flash_resource.start = addr;
280 physmap_flash_resource.end = addr + size - 1;
281 physmap_flash_data.width = bankwidth;
282 physmap_flash_data.set_vpp = set_vpp;
283}
284
1da177e4 285#ifdef CONFIG_MTD_PARTITIONS
73566edf
LB
286void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
287{
288 physmap_flash_data.nr_parts = num_parts;
289 physmap_flash_data.parts = parts;
290}
291#endif
1da177e4 292#endif
1da177e4 293
73566edf
LB
294static int __init physmap_init(void)
295{
296 int err;
297
298 err = platform_driver_register(&physmap_flash_driver);
299#ifdef PHYSMAP_COMPAT
300 if (err == 0)
301 platform_device_register(&physmap_flash);
302#endif
303
304 return err;
1da177e4
LT
305}
306
73566edf
LB
307static void __exit physmap_exit(void)
308{
309#ifdef PHYSMAP_COMPAT
310 platform_device_unregister(&physmap_flash);
311#endif
312 platform_driver_unregister(&physmap_flash_driver);
313}
1da177e4 314
73566edf
LB
315module_init(physmap_init);
316module_exit(physmap_exit);
1da177e4
LT
317
318MODULE_LICENSE("GPL");
319MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
320MODULE_DESCRIPTION("Generic configurable MTD map driver");
This page took 0.243973 seconds and 5 git commands to generate.