Commit | Line | Data |
---|---|---|
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> |
73566edf | 23 | #include <asm/io.h> |
1da177e4 | 24 | |
73566edf LB |
25 | struct physmap_flash_info { |
26 | struct mtd_info *mtd; | |
27 | struct map_info map; | |
28 | struct resource *res; | |
29 | #ifdef CONFIG_MTD_PARTITIONS | |
30 | int nr_parts; | |
31 | struct mtd_partition *parts; | |
32 | #endif | |
1da177e4 LT |
33 | }; |
34 | ||
73566edf LB |
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 | ||
41 | info = platform_get_drvdata(dev); | |
42 | if (info == NULL) | |
43 | return 0; | |
44 | platform_set_drvdata(dev, NULL); | |
45 | ||
46 | physmap_data = dev->dev.platform_data; | |
47 | ||
48 | if (info->mtd != NULL) { | |
1da177e4 | 49 | #ifdef CONFIG_MTD_PARTITIONS |
73566edf LB |
50 | if (info->nr_parts) { |
51 | del_mtd_partitions(info->mtd); | |
52 | kfree(info->parts); | |
53 | } else if (physmap_data->nr_parts) { | |
54 | del_mtd_partitions(info->mtd); | |
55 | } else { | |
56 | del_mtd_device(info->mtd); | |
57 | } | |
58 | #else | |
59 | del_mtd_device(info->mtd); | |
60 | #endif | |
61 | map_destroy(info->mtd); | |
62 | } | |
1da177e4 | 63 | |
73566edf | 64 | if (info->map.virt != NULL) |
afc12d30 | 65 | iounmap(info->map.virt); |
1da177e4 | 66 | |
73566edf LB |
67 | if (info->res != NULL) { |
68 | release_resource(info->res); | |
69 | kfree(info->res); | |
70 | } | |
1da177e4 | 71 | |
73566edf | 72 | return 0; |
1da177e4 | 73 | } |
1da177e4 | 74 | |
73566edf LB |
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 }; | |
78 | #endif | |
79 | ||
80 | static int physmap_flash_probe(struct platform_device *dev) | |
1da177e4 | 81 | { |
73566edf LB |
82 | struct physmap_flash_data *physmap_data; |
83 | struct physmap_flash_info *info; | |
84 | const char **probe_type; | |
85 | int err; | |
86 | ||
87 | physmap_data = dev->dev.platform_data; | |
88 | if (physmap_data == NULL) | |
89 | return -ENODEV; | |
90 | ||
f24ff6bf AM |
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); | |
73566edf LB |
94 | |
95 | info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL); | |
96 | if (info == NULL) { | |
97 | err = -ENOMEM; | |
98 | goto err_out; | |
99 | } | |
100 | memset(info, 0, sizeof(*info)); | |
1da177e4 | 101 | |
73566edf | 102 | platform_set_drvdata(dev, info); |
1da177e4 | 103 | |
73566edf LB |
104 | info->res = request_mem_region(dev->resource->start, |
105 | dev->resource->end - dev->resource->start + 1, | |
106 | dev->dev.bus_id); | |
107 | if (info->res == NULL) { | |
108 | dev_err(&dev->dev, "Could not reserve memory region\n"); | |
109 | err = -ENOMEM; | |
110 | goto err_out; | |
1da177e4 LT |
111 | } |
112 | ||
73566edf LB |
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; | |
118 | ||
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"); | |
122 | err = EIO; | |
123 | goto err_out; | |
124 | } | |
1da177e4 | 125 | |
73566edf LB |
126 | simple_map_init(&info->map); |
127 | ||
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"); | |
133 | err = -ENXIO; | |
134 | goto err_out; | |
1da177e4 | 135 | } |
73566edf | 136 | info->mtd->owner = THIS_MODULE; |
1da177e4 LT |
137 | |
138 | #ifdef CONFIG_MTD_PARTITIONS | |
73566edf LB |
139 | err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0); |
140 | if (err > 0) { | |
141 | add_mtd_partitions(info->mtd, info->parts, err); | |
142 | return 0; | |
143 | } | |
1da177e4 | 144 | |
73566edf LB |
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); | |
149 | return 0; | |
150 | } | |
151 | #endif | |
152 | ||
153 | add_mtd_device(info->mtd); | |
154 | return 0; | |
155 | ||
156 | err_out: | |
157 | physmap_flash_remove(dev); | |
158 | return err; | |
159 | } | |
160 | ||
17c2dae3 LB |
161 | #ifdef CONFIG_PM |
162 | static int physmap_flash_suspend(struct platform_device *dev, pm_message_t state) | |
163 | { | |
164 | struct physmap_flash_info *info = platform_get_drvdata(dev); | |
165 | int ret = 0; | |
166 | ||
167 | if (info) | |
168 | ret = info->mtd->suspend(info->mtd); | |
169 | ||
170 | return ret; | |
171 | } | |
172 | ||
173 | static int physmap_flash_resume(struct platform_device *dev) | |
174 | { | |
175 | struct physmap_flash_info *info = platform_get_drvdata(dev); | |
176 | if (info) | |
177 | info->mtd->resume(info->mtd); | |
178 | return 0; | |
179 | } | |
180 | ||
181 | static void physmap_flash_shutdown(struct platform_device *dev) | |
182 | { | |
183 | struct physmap_flash_info *info = platform_get_drvdata(dev); | |
184 | if (info && info->mtd->suspend(info->mtd) == 0) | |
185 | info->mtd->resume(info->mtd); | |
186 | } | |
187 | #endif | |
188 | ||
73566edf LB |
189 | static struct platform_driver physmap_flash_driver = { |
190 | .probe = physmap_flash_probe, | |
191 | .remove = physmap_flash_remove, | |
17c2dae3 LB |
192 | #ifdef CONFIG_PM |
193 | .suspend = physmap_flash_suspend, | |
194 | .resume = physmap_flash_resume, | |
195 | .shutdown = physmap_flash_shutdown, | |
196 | #endif | |
73566edf LB |
197 | .driver = { |
198 | .name = "physmap-flash", | |
199 | }, | |
200 | }; | |
1da177e4 | 201 | |
1da177e4 | 202 | |
73566edf LB |
203 | #ifdef CONFIG_MTD_PHYSMAP_LEN |
204 | #if CONFIG_MTD_PHYSMAP_LEN != 0 | |
205 | #warning using PHYSMAP compat code | |
206 | #define PHYSMAP_COMPAT | |
207 | #endif | |
1da177e4 | 208 | #endif |
1da177e4 | 209 | |
73566edf LB |
210 | #ifdef PHYSMAP_COMPAT |
211 | static struct physmap_flash_data physmap_flash_data = { | |
212 | .width = CONFIG_MTD_PHYSMAP_BANKWIDTH, | |
213 | }; | |
1da177e4 | 214 | |
73566edf LB |
215 | static struct resource physmap_flash_resource = { |
216 | .start = CONFIG_MTD_PHYSMAP_START, | |
6d4f8224 | 217 | .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1, |
73566edf LB |
218 | .flags = IORESOURCE_MEM, |
219 | }; | |
1da177e4 | 220 | |
73566edf LB |
221 | static struct platform_device physmap_flash = { |
222 | .name = "physmap-flash", | |
223 | .id = 0, | |
224 | .dev = { | |
225 | .platform_data = &physmap_flash_data, | |
226 | }, | |
227 | .num_resources = 1, | |
228 | .resource = &physmap_flash_resource, | |
229 | }; | |
230 | ||
231 | void physmap_configure(unsigned long addr, unsigned long size, | |
232 | int bankwidth, void (*set_vpp)(struct map_info *, int)) | |
1da177e4 | 233 | { |
73566edf LB |
234 | physmap_flash_resource.start = addr; |
235 | physmap_flash_resource.end = addr + size - 1; | |
236 | physmap_flash_data.width = bankwidth; | |
237 | physmap_flash_data.set_vpp = set_vpp; | |
238 | } | |
239 | ||
1da177e4 | 240 | #ifdef CONFIG_MTD_PARTITIONS |
73566edf LB |
241 | void physmap_set_partitions(struct mtd_partition *parts, int num_parts) |
242 | { | |
243 | physmap_flash_data.nr_parts = num_parts; | |
244 | physmap_flash_data.parts = parts; | |
245 | } | |
246 | #endif | |
1da177e4 | 247 | #endif |
1da177e4 | 248 | |
73566edf LB |
249 | static int __init physmap_init(void) |
250 | { | |
251 | int err; | |
252 | ||
253 | err = platform_driver_register(&physmap_flash_driver); | |
254 | #ifdef PHYSMAP_COMPAT | |
255 | if (err == 0) | |
256 | platform_device_register(&physmap_flash); | |
257 | #endif | |
258 | ||
259 | return err; | |
1da177e4 LT |
260 | } |
261 | ||
73566edf LB |
262 | static void __exit physmap_exit(void) |
263 | { | |
264 | #ifdef PHYSMAP_COMPAT | |
265 | platform_device_unregister(&physmap_flash); | |
266 | #endif | |
267 | platform_driver_unregister(&physmap_flash_driver); | |
268 | } | |
1da177e4 | 269 | |
73566edf LB |
270 | module_init(physmap_init); |
271 | module_exit(physmap_exit); | |
1da177e4 LT |
272 | |
273 | MODULE_LICENSE("GPL"); | |
274 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
275 | MODULE_DESCRIPTION("Generic configurable MTD map driver"); |