Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
1da177e4 LT |
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> | |
73566edf LB |
15 | #include <linux/device.h> |
16 | #include <linux/platform_device.h> | |
1da177e4 LT |
17 | #include <linux/mtd/mtd.h> |
18 | #include <linux/mtd/map.h> | |
1da177e4 | 19 | #include <linux/mtd/partitions.h> |
2b9175c1 | 20 | #include <linux/mtd/physmap.h> |
df66e716 | 21 | #include <linux/mtd/concat.h> |
3136e903 | 22 | #include <linux/io.h> |
1da177e4 | 23 | |
df66e716 SR |
24 | #define MAX_RESOURCES 4 |
25 | ||
73566edf | 26 | struct physmap_flash_info { |
df66e716 SR |
27 | struct mtd_info *mtd[MAX_RESOURCES]; |
28 | struct mtd_info *cmtd; | |
29 | struct map_info map[MAX_RESOURCES]; | |
73566edf LB |
30 | #ifdef CONFIG_MTD_PARTITIONS |
31 | int nr_parts; | |
32 | struct mtd_partition *parts; | |
33 | #endif | |
1da177e4 LT |
34 | }; |
35 | ||
73566edf LB |
36 | static int physmap_flash_remove(struct platform_device *dev) |
37 | { | |
38 | struct physmap_flash_info *info; | |
39 | struct physmap_flash_data *physmap_data; | |
df66e716 | 40 | int i; |
73566edf LB |
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 | ||
df66e716 SR |
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) { | |
1da177e4 | 58 | #ifdef CONFIG_MTD_PARTITIONS |
df66e716 SR |
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 | } | |
73566edf | 67 | #else |
df66e716 | 68 | del_mtd_device(info->mtd[i]); |
73566edf | 69 | #endif |
df66e716 SR |
70 | map_destroy(info->mtd[i]); |
71 | } | |
73566edf | 72 | } |
73566edf | 73 | return 0; |
1da177e4 | 74 | } |
1da177e4 | 75 | |
73566edf LB |
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) | |
1da177e4 | 82 | { |
73566edf LB |
83 | struct physmap_flash_data *physmap_data; |
84 | struct physmap_flash_info *info; | |
85 | const char **probe_type; | |
df66e716 SR |
86 | int err = 0; |
87 | int i; | |
88 | int devices_found = 0; | |
73566edf LB |
89 | |
90 | physmap_data = dev->dev.platform_data; | |
91 | if (physmap_data == NULL) | |
92 | return -ENODEV; | |
93 | ||
3136e903 AN |
94 | info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info), |
95 | GFP_KERNEL); | |
73566edf LB |
96 | if (info == NULL) { |
97 | err = -ENOMEM; | |
98 | goto err_out; | |
99 | } | |
1da177e4 | 100 | |
73566edf | 101 | platform_set_drvdata(dev, info); |
1da177e4 | 102 | |
df66e716 SR |
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 | ||
3136e903 AN |
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->dev.bus_id)) { | |
df66e716 SR |
112 | dev_err(&dev->dev, "Could not reserve memory region\n"); |
113 | err = -ENOMEM; | |
114 | goto err_out; | |
115 | } | |
1da177e4 | 116 | |
df66e716 SR |
117 | info->map[i].name = dev->dev.bus_id; |
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 | ||
3136e903 AN |
123 | info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys, |
124 | info->map[i].size); | |
df66e716 SR |
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 | } | |
73566edf | 130 | |
df66e716 | 131 | simple_map_init(&info->map[i]); |
1da177e4 | 132 | |
df66e716 SR |
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 | } | |
73566edf | 145 | |
df66e716 SR |
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->dev.bus_id); | |
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"); | |
73566edf | 159 | err = -ENXIO; |
df66e716 | 160 | #endif |
1da177e4 | 161 | } |
df66e716 SR |
162 | if (err) |
163 | goto err_out; | |
1da177e4 LT |
164 | |
165 | #ifdef CONFIG_MTD_PARTITIONS | |
df66e716 | 166 | err = parse_mtd_partitions(info->cmtd, part_probe_types, &info->parts, 0); |
73566edf | 167 | if (err > 0) { |
df66e716 | 168 | add_mtd_partitions(info->cmtd, info->parts, err); |
73566edf LB |
169 | return 0; |
170 | } | |
1da177e4 | 171 | |
73566edf LB |
172 | if (physmap_data->nr_parts) { |
173 | printk(KERN_NOTICE "Using physmap partition information\n"); | |
df66e716 SR |
174 | add_mtd_partitions(info->cmtd, physmap_data->parts, |
175 | physmap_data->nr_parts); | |
73566edf LB |
176 | return 0; |
177 | } | |
178 | #endif | |
179 | ||
df66e716 | 180 | add_mtd_device(info->cmtd); |
73566edf LB |
181 | return 0; |
182 | ||
183 | err_out: | |
184 | physmap_flash_remove(dev); | |
185 | return err; | |
186 | } | |
187 | ||
17c2dae3 LB |
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; | |
df66e716 | 193 | int i; |
17c2dae3 | 194 | |
4a5691c0 | 195 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
4b5e33a7 UKK |
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 | } | |
17c2dae3 LB |
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); | |
df66e716 SR |
216 | int i; |
217 | ||
4a5691c0 | 218 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
7b249191 RJ |
219 | if (info->mtd[i]->resume) |
220 | info->mtd[i]->resume(info->mtd[i]); | |
4a5691c0 | 221 | |
17c2dae3 LB |
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); | |
df66e716 SR |
228 | int i; |
229 | ||
4a5691c0 | 230 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
7b249191 RJ |
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]); | |
17c2dae3 | 234 | } |
d5476689 | 235 | #else |
236 | #define physmap_flash_suspend NULL | |
237 | #define physmap_flash_resume NULL | |
238 | #define physmap_flash_shutdown NULL | |
17c2dae3 LB |
239 | #endif |
240 | ||
73566edf LB |
241 | static struct platform_driver physmap_flash_driver = { |
242 | .probe = physmap_flash_probe, | |
243 | .remove = physmap_flash_remove, | |
17c2dae3 LB |
244 | .suspend = physmap_flash_suspend, |
245 | .resume = physmap_flash_resume, | |
246 | .shutdown = physmap_flash_shutdown, | |
73566edf LB |
247 | .driver = { |
248 | .name = "physmap-flash", | |
41d867c9 | 249 | .owner = THIS_MODULE, |
73566edf LB |
250 | }, |
251 | }; | |
1da177e4 | 252 | |
1da177e4 | 253 | |
73566edf LB |
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 | |
1da177e4 | 259 | #endif |
1da177e4 | 260 | |
73566edf LB |
261 | #ifdef PHYSMAP_COMPAT |
262 | static struct physmap_flash_data physmap_flash_data = { | |
263 | .width = CONFIG_MTD_PHYSMAP_BANKWIDTH, | |
264 | }; | |
1da177e4 | 265 | |
73566edf LB |
266 | static struct resource physmap_flash_resource = { |
267 | .start = CONFIG_MTD_PHYSMAP_START, | |
6d4f8224 | 268 | .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1, |
73566edf LB |
269 | .flags = IORESOURCE_MEM, |
270 | }; | |
1da177e4 | 271 | |
73566edf LB |
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)) | |
1da177e4 | 284 | { |
73566edf LB |
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 | ||
1da177e4 | 291 | #ifdef CONFIG_MTD_PARTITIONS |
73566edf LB |
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 | |
1da177e4 | 298 | #endif |
1da177e4 | 299 | |
73566edf LB |
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; | |
1da177e4 LT |
311 | } |
312 | ||
73566edf LB |
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 | } | |
1da177e4 | 320 | |
73566edf LB |
321 | module_init(physmap_init); |
322 | module_exit(physmap_exit); | |
1da177e4 LT |
323 | |
324 | MODULE_LICENSE("GPL"); | |
325 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); | |
326 | MODULE_DESCRIPTION("Generic configurable MTD map driver"); | |
41d867c9 KS |
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 |