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
;
49 #ifdef CONFIG_MTD_CONCAT
50 if (info
->cmtd
!= info
->mtd
[0]) {
51 del_mtd_device(info
->cmtd
);
52 mtd_concat_destroy(info
->cmtd
);
56 for (i
= 0; i
< MAX_RESOURCES
; i
++) {
57 if (info
->mtd
[i
] != NULL
) {
58 #ifdef CONFIG_MTD_PARTITIONS
60 del_mtd_partitions(info
->mtd
[i
]);
62 } else if (physmap_data
->nr_parts
) {
63 del_mtd_partitions(info
->mtd
[i
]);
65 del_mtd_device(info
->mtd
[i
]);
68 del_mtd_device(info
->mtd
[i
]);
70 map_destroy(info
->mtd
[i
]);
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
};
81 static int physmap_flash_probe(struct platform_device
*dev
)
83 struct physmap_flash_data
*physmap_data
;
84 struct physmap_flash_info
*info
;
85 const char **probe_type
;
88 int devices_found
= 0;
90 physmap_data
= dev
->dev
.platform_data
;
91 if (physmap_data
== NULL
)
94 info
= devm_kzalloc(&dev
->dev
, sizeof(struct physmap_flash_info
),
101 platform_set_drvdata(dev
, info
);
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
);
108 if (!devm_request_mem_region(&dev
->dev
,
109 dev
->resource
[i
].start
,
110 dev
->resource
[i
].end
- dev
->resource
[i
].start
+ 1,
112 dev_err(&dev
->dev
, "Could not reserve memory region\n");
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
;
123 info
->map
[i
].virt
= devm_ioremap(&dev
->dev
, info
->map
[i
].phys
,
125 if (info
->map
[i
].virt
== NULL
) {
126 dev_err(&dev
->dev
, "Failed to ioremap flash region\n");
131 simple_map_init(&info
->map
[i
]);
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");
143 info
->mtd
[i
]->owner
= THIS_MODULE
;
146 if (devices_found
== 1) {
147 info
->cmtd
= info
->mtd
[0];
148 } else if (devices_found
> 1) {
150 * We detected multiple devices. Concatenate them together.
152 #ifdef CONFIG_MTD_CONCAT
153 info
->cmtd
= mtd_concat_create(info
->mtd
, devices_found
, dev
->dev
.bus_id
);
154 if (info
->cmtd
== NULL
)
157 printk(KERN_ERR
"physmap-flash: multiple devices "
158 "found but MTD concat support disabled.\n");
165 #ifdef CONFIG_MTD_PARTITIONS
166 err
= parse_mtd_partitions(info
->cmtd
, part_probe_types
, &info
->parts
, 0);
168 add_mtd_partitions(info
->cmtd
, info
->parts
, err
);
172 if (physmap_data
->nr_parts
) {
173 printk(KERN_NOTICE
"Using physmap partition information\n");
174 add_mtd_partitions(info
->cmtd
, physmap_data
->parts
,
175 physmap_data
->nr_parts
);
180 add_mtd_device(info
->cmtd
);
184 physmap_flash_remove(dev
);
189 static int physmap_flash_suspend(struct platform_device
*dev
, pm_message_t state
)
191 struct physmap_flash_info
*info
= platform_get_drvdata(dev
);
195 for (i
= 0; i
< MAX_RESOURCES
&& info
->mtd
[i
]; i
++)
196 if (info
->mtd
[i
]->suspend
) {
197 ret
= info
->mtd
[i
]->suspend(info
->mtd
[i
]);
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
]);
213 static int physmap_flash_resume(struct platform_device
*dev
)
215 struct physmap_flash_info
*info
= platform_get_drvdata(dev
);
218 for (i
= 0; i
< MAX_RESOURCES
&& info
->mtd
[i
]; i
++)
219 if (info
->mtd
[i
]->resume
)
220 info
->mtd
[i
]->resume(info
->mtd
[i
]);
225 static void physmap_flash_shutdown(struct platform_device
*dev
)
227 struct physmap_flash_info
*info
= platform_get_drvdata(dev
);
230 for (i
= 0; i
< MAX_RESOURCES
&& info
->mtd
[i
]; i
++)
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
]);
236 #define physmap_flash_suspend NULL
237 #define physmap_flash_resume NULL
238 #define physmap_flash_shutdown NULL
241 static struct platform_driver physmap_flash_driver
= {
242 .probe
= physmap_flash_probe
,
243 .remove
= physmap_flash_remove
,
244 .suspend
= physmap_flash_suspend
,
245 .resume
= physmap_flash_resume
,
246 .shutdown
= physmap_flash_shutdown
,
248 .name
= "physmap-flash",
249 .owner
= THIS_MODULE
,
254 #ifdef CONFIG_MTD_PHYSMAP_LEN
255 #if CONFIG_MTD_PHYSMAP_LEN != 0
256 #warning using PHYSMAP compat code
257 #define PHYSMAP_COMPAT
261 #ifdef PHYSMAP_COMPAT
262 static struct physmap_flash_data physmap_flash_data
= {
263 .width
= CONFIG_MTD_PHYSMAP_BANKWIDTH
,
266 static struct resource physmap_flash_resource
= {
267 .start
= CONFIG_MTD_PHYSMAP_START
,
268 .end
= CONFIG_MTD_PHYSMAP_START
+ CONFIG_MTD_PHYSMAP_LEN
- 1,
269 .flags
= IORESOURCE_MEM
,
272 static struct platform_device physmap_flash
= {
273 .name
= "physmap-flash",
276 .platform_data
= &physmap_flash_data
,
279 .resource
= &physmap_flash_resource
,
282 void physmap_configure(unsigned long addr
, unsigned long size
,
283 int bankwidth
, void (*set_vpp
)(struct map_info
*, int))
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
;
291 #ifdef CONFIG_MTD_PARTITIONS
292 void physmap_set_partitions(struct mtd_partition
*parts
, int num_parts
)
294 physmap_flash_data
.nr_parts
= num_parts
;
295 physmap_flash_data
.parts
= parts
;
300 static int __init
physmap_init(void)
304 err
= platform_driver_register(&physmap_flash_driver
);
305 #ifdef PHYSMAP_COMPAT
307 platform_device_register(&physmap_flash
);
313 static void __exit
physmap_exit(void)
315 #ifdef PHYSMAP_COMPAT
316 platform_device_unregister(&physmap_flash
);
318 platform_driver_unregister(&physmap_flash_driver
);
321 module_init(physmap_init
);
322 module_exit(physmap_exit
);
324 MODULE_LICENSE("GPL");
325 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
326 MODULE_DESCRIPTION("Generic configurable MTD map driver");
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");