2 * Normal mappings of chips in physical memory
4 * Copyright (C) 2003 MontaVista Software Inc.
5 * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
7 * 031022 - [jsun] add run-time configure and partition setup
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>
24 #define MAX_RESOURCES 4
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
32 struct mtd_partition *parts;
36 static int physmap_flash_remove(struct platform_device *dev)
38 struct physmap_flash_info *info;
39 struct physmap_flash_data *physmap_data;
42 info = platform_get_drvdata(dev);
45 platform_set_drvdata(dev, NULL);
47 physmap_data = dev->dev.platform_data;
50 #ifdef CONFIG_MTD_PARTITIONS
51 if (info->nr_parts || physmap_data->nr_parts) {
52 del_mtd_partitions(info->cmtd);
57 del_mtd_device(info->cmtd);
60 del_mtd_device(info->cmtd);
62 if (info->cmtd != info->mtd[0])
63 mtd_concat_destroy(info->cmtd);
66 for (i = 0; i < MAX_RESOURCES; i++) {
67 if (info->mtd[i] != NULL)
68 map_destroy(info->mtd[i]);
73 static const char *rom_probe_types[] = {
79 #ifdef CONFIG_MTD_PARTITIONS
80 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
83 static int physmap_flash_probe(struct platform_device *dev)
85 struct physmap_flash_data *physmap_data;
86 struct physmap_flash_info *info;
87 const char **probe_type;
90 int devices_found = 0;
92 physmap_data = dev->dev.platform_data;
93 if (physmap_data == NULL)
96 info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
103 platform_set_drvdata(dev, info);
105 for (i = 0; i < dev->num_resources; i++) {
106 printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
107 (unsigned long long)resource_size(&dev->resource[i]),
108 (unsigned long long)dev->resource[i].start);
110 if (!devm_request_mem_region(&dev->dev,
111 dev->resource[i].start,
112 resource_size(&dev->resource[i]),
113 dev_name(&dev->dev))) {
114 dev_err(&dev->dev, "Could not reserve memory region\n");
119 info->map[i].name = dev_name(&dev->dev);
120 info->map[i].phys = dev->resource[i].start;
121 info->map[i].size = resource_size(&dev->resource[i]);
122 info->map[i].bankwidth = physmap_data->width;
123 info->map[i].set_vpp = physmap_data->set_vpp;
124 info->map[i].pfow_base = physmap_data->pfow_base;
126 info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
128 if (info->map[i].virt == NULL) {
129 dev_err(&dev->dev, "Failed to ioremap flash region\n");
134 simple_map_init(&info->map[i]);
136 probe_type = rom_probe_types;
137 if (physmap_data->probe_type == NULL) {
138 for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
139 info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
141 info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
143 if (info->mtd[i] == NULL) {
144 dev_err(&dev->dev, "map_probe failed\n");
150 info->mtd[i]->owner = THIS_MODULE;
151 info->mtd[i]->dev.parent = &dev->dev;
154 if (devices_found == 1) {
155 info->cmtd = info->mtd[0];
156 } else if (devices_found > 1) {
158 * We detected multiple devices. Concatenate them together.
160 info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
161 if (info->cmtd == NULL)
167 #ifdef CONFIG_MTD_PARTITIONS
168 err = parse_mtd_partitions(info->cmtd, part_probe_types,
171 add_mtd_partitions(info->cmtd, info->parts, err);
172 info->nr_parts = err;
176 if (physmap_data->nr_parts) {
177 printk(KERN_NOTICE "Using physmap partition information\n");
178 add_mtd_partitions(info->cmtd, physmap_data->parts,
179 physmap_data->nr_parts);
184 add_mtd_device(info->cmtd);
188 physmap_flash_remove(dev);
193 static void physmap_flash_shutdown(struct platform_device *dev)
195 struct physmap_flash_info *info = platform_get_drvdata(dev);
198 for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
199 if (info->mtd[i]->suspend && info->mtd[i]->resume)
200 if (info->mtd[i]->suspend(info->mtd[i]) == 0)
201 info->mtd[i]->resume(info->mtd[i]);
204 #define physmap_flash_shutdown NULL
207 static struct platform_driver physmap_flash_driver = {
208 .probe = physmap_flash_probe,
209 .remove = physmap_flash_remove,
210 .shutdown = physmap_flash_shutdown,
212 .name = "physmap-flash",
213 .owner = THIS_MODULE,
218 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
219 static struct physmap_flash_data physmap_flash_data = {
220 .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
223 static struct resource physmap_flash_resource = {
224 .start = CONFIG_MTD_PHYSMAP_START,
225 .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
226 .flags = IORESOURCE_MEM,
229 static struct platform_device physmap_flash = {
230 .name = "physmap-flash",
233 .platform_data = &physmap_flash_data,
236 .resource = &physmap_flash_resource,
239 void physmap_configure(unsigned long addr, unsigned long size,
240 int bankwidth, void (*set_vpp)(struct map_info *, int))
242 physmap_flash_resource.start = addr;
243 physmap_flash_resource.end = addr + size - 1;
244 physmap_flash_data.width = bankwidth;
245 physmap_flash_data.set_vpp = set_vpp;
248 #ifdef CONFIG_MTD_PARTITIONS
249 void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
251 physmap_flash_data.nr_parts = num_parts;
252 physmap_flash_data.parts = parts;
257 static int __init physmap_init(void)
261 err = platform_driver_register(&physmap_flash_driver);
262 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
264 err = platform_device_register(&physmap_flash);
266 platform_driver_unregister(&physmap_flash_driver);
273 static void __exit physmap_exit(void)
275 #ifdef CONFIG_MTD_PHYSMAP_COMPAT
276 platform_device_unregister(&physmap_flash);
278 platform_driver_unregister(&physmap_flash_driver);
281 module_init(physmap_init);
282 module_exit(physmap_exit);
284 MODULE_LICENSE("GPL");
285 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
286 MODULE_DESCRIPTION("Generic configurable MTD map driver");
288 /* legacy platform drivers can't hotplug or coldplg */
289 #ifndef CONFIG_MTD_PHYSMAP_COMPAT
290 /* work with hotplug and coldplug */
291 MODULE_ALIAS("platform:physmap-flash");