Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Adrian Bunk | 2b9175c | 2005-11-29 14:49:38 +0000 | [diff] [blame] | 2 | * $Id: physmap.c,v 1.39 2005/11/29 14:49:36 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 17 | #include <linux/device.h> |
| 18 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/mtd/mtd.h> |
| 20 | #include <linux/mtd/map.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/mtd/partitions.h> |
Adrian Bunk | 2b9175c | 2005-11-29 14:49:38 +0000 | [diff] [blame] | 22 | #include <linux/mtd/physmap.h> |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 23 | #include <linux/mtd/concat.h> |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 24 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 26 | #define MAX_RESOURCES 4 |
| 27 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 28 | struct physmap_flash_info { |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 29 | struct mtd_info *mtd[MAX_RESOURCES]; |
| 30 | struct mtd_info *cmtd; |
| 31 | struct map_info map[MAX_RESOURCES]; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 32 | struct resource *res; |
| 33 | #ifdef CONFIG_MTD_PARTITIONS |
| 34 | int nr_parts; |
| 35 | struct mtd_partition *parts; |
| 36 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 39 | static int physmap_flash_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 41 | struct physmap_flash_info *info; |
| 42 | struct physmap_flash_data *physmap_data; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 43 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 45 | info = platform_get_drvdata(dev); |
| 46 | if (info == NULL) |
| 47 | return 0; |
| 48 | platform_set_drvdata(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 50 | physmap_data = dev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 52 | #ifdef CONFIG_MTD_CONCAT |
| 53 | if (info->cmtd != info->mtd[0]) { |
| 54 | del_mtd_device(info->cmtd); |
| 55 | mtd_concat_destroy(info->cmtd); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 56 | } |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 57 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 59 | for (i = 0; i < MAX_RESOURCES; i++) { |
| 60 | if (info->mtd[i] != NULL) { |
| 61 | #ifdef CONFIG_MTD_PARTITIONS |
| 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 | } |
| 70 | #else |
| 71 | del_mtd_device(info->mtd[i]); |
| 72 | #endif |
| 73 | map_destroy(info->mtd[i]); |
| 74 | } |
| 75 | |
| 76 | if (info->map[i].virt != NULL) |
| 77 | iounmap(info->map[i].virt); |
| 78 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 79 | |
| 80 | if (info->res != NULL) { |
| 81 | release_resource(info->res); |
| 82 | kfree(info->res); |
| 83 | } |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL }; |
| 89 | #ifdef CONFIG_MTD_PARTITIONS |
| 90 | static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL }; |
| 91 | #endif |
| 92 | |
| 93 | static int physmap_flash_probe(struct platform_device *dev) |
| 94 | { |
| 95 | struct physmap_flash_data *physmap_data; |
| 96 | struct physmap_flash_info *info; |
| 97 | const char **probe_type; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 98 | int err = 0; |
| 99 | int i; |
| 100 | int devices_found = 0; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 101 | |
| 102 | physmap_data = dev->dev.platform_data; |
| 103 | if (physmap_data == NULL) |
| 104 | return -ENODEV; |
| 105 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 106 | info = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 107 | if (info == NULL) { |
| 108 | err = -ENOMEM; |
| 109 | goto err_out; |
| 110 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 111 | |
| 112 | platform_set_drvdata(dev, info); |
| 113 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 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 | } |
| 127 | |
| 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 | } |
| 140 | |
| 141 | simple_map_init(&info->map[i]); |
| 142 | |
| 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; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 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"); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 169 | err = -ENXIO; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 170 | #endif |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 171 | } |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 172 | if (err) |
| 173 | goto err_out; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 174 | |
| 175 | #ifdef CONFIG_MTD_PARTITIONS |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 176 | err = parse_mtd_partitions(info->cmtd, part_probe_types, &info->parts, 0); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 177 | if (err > 0) { |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 178 | add_mtd_partitions(info->cmtd, info->parts, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | } |
| 181 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 182 | if (physmap_data->nr_parts) { |
| 183 | printk(KERN_NOTICE "Using physmap partition information\n"); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 184 | add_mtd_partitions(info->cmtd, physmap_data->parts, |
| 185 | physmap_data->nr_parts); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 186 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 190 | add_mtd_device(info->cmtd); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 191 | return 0; |
| 192 | |
| 193 | err_out: |
| 194 | physmap_flash_remove(dev); |
| 195 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 198 | #ifdef CONFIG_PM |
| 199 | static 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; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 203 | int i; |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 204 | |
Anton Vorontsov | 4a5691c | 2008-03-28 14:16:09 -0700 | [diff] [blame] | 205 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
| 206 | ret |= info->mtd[i]->suspend(info->mtd[i]); |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | static int physmap_flash_resume(struct platform_device *dev) |
| 212 | { |
| 213 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 214 | int i; |
| 215 | |
Anton Vorontsov | 4a5691c | 2008-03-28 14:16:09 -0700 | [diff] [blame] | 216 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
| 217 | info->mtd[i]->resume(info->mtd[i]); |
| 218 | |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static void physmap_flash_shutdown(struct platform_device *dev) |
| 223 | { |
| 224 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 225 | int i; |
| 226 | |
Anton Vorontsov | 4a5691c | 2008-03-28 14:16:09 -0700 | [diff] [blame] | 227 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
| 228 | if (info->mtd[i]->suspend(info->mtd[i]) == 0) |
akpm@linux-foundation.org | d547668 | 2008-02-03 12:56:03 -0800 | [diff] [blame] | 229 | info->mtd[i]->resume(info->mtd[i]); |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 230 | } |
akpm@linux-foundation.org | d547668 | 2008-02-03 12:56:03 -0800 | [diff] [blame] | 231 | #else |
| 232 | #define physmap_flash_suspend NULL |
| 233 | #define physmap_flash_resume NULL |
| 234 | #define physmap_flash_shutdown NULL |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 235 | #endif |
| 236 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 237 | static struct platform_driver physmap_flash_driver = { |
| 238 | .probe = physmap_flash_probe, |
| 239 | .remove = physmap_flash_remove, |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 240 | .suspend = physmap_flash_suspend, |
| 241 | .resume = physmap_flash_resume, |
| 242 | .shutdown = physmap_flash_shutdown, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 243 | .driver = { |
| 244 | .name = "physmap-flash", |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 245 | .owner = THIS_MODULE, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 246 | }, |
| 247 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
| 249 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 250 | #ifdef CONFIG_MTD_PHYSMAP_LEN |
| 251 | #if CONFIG_MTD_PHYSMAP_LEN != 0 |
| 252 | #warning using PHYSMAP compat code |
| 253 | #define PHYSMAP_COMPAT |
| 254 | #endif |
| 255 | #endif |
| 256 | |
| 257 | #ifdef PHYSMAP_COMPAT |
| 258 | static struct physmap_flash_data physmap_flash_data = { |
| 259 | .width = CONFIG_MTD_PHYSMAP_BANKWIDTH, |
| 260 | }; |
| 261 | |
| 262 | static struct resource physmap_flash_resource = { |
| 263 | .start = CONFIG_MTD_PHYSMAP_START, |
Sascha Hauer | 6d4f822 | 2006-06-27 14:38:15 +0100 | [diff] [blame] | 264 | .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 265 | .flags = IORESOURCE_MEM, |
| 266 | }; |
| 267 | |
| 268 | static struct platform_device physmap_flash = { |
| 269 | .name = "physmap-flash", |
| 270 | .id = 0, |
| 271 | .dev = { |
| 272 | .platform_data = &physmap_flash_data, |
| 273 | }, |
| 274 | .num_resources = 1, |
| 275 | .resource = &physmap_flash_resource, |
| 276 | }; |
| 277 | |
| 278 | void physmap_configure(unsigned long addr, unsigned long size, |
| 279 | int bankwidth, void (*set_vpp)(struct map_info *, int)) |
| 280 | { |
| 281 | physmap_flash_resource.start = addr; |
| 282 | physmap_flash_resource.end = addr + size - 1; |
| 283 | physmap_flash_data.width = bankwidth; |
| 284 | physmap_flash_data.set_vpp = set_vpp; |
| 285 | } |
| 286 | |
| 287 | #ifdef CONFIG_MTD_PARTITIONS |
| 288 | void physmap_set_partitions(struct mtd_partition *parts, int num_parts) |
| 289 | { |
| 290 | physmap_flash_data.nr_parts = num_parts; |
| 291 | physmap_flash_data.parts = parts; |
| 292 | } |
| 293 | #endif |
| 294 | #endif |
| 295 | |
| 296 | static int __init physmap_init(void) |
| 297 | { |
| 298 | int err; |
| 299 | |
| 300 | err = platform_driver_register(&physmap_flash_driver); |
| 301 | #ifdef PHYSMAP_COMPAT |
| 302 | if (err == 0) |
| 303 | platform_device_register(&physmap_flash); |
| 304 | #endif |
| 305 | |
| 306 | return err; |
| 307 | } |
| 308 | |
| 309 | static void __exit physmap_exit(void) |
| 310 | { |
| 311 | #ifdef PHYSMAP_COMPAT |
| 312 | platform_device_unregister(&physmap_flash); |
| 313 | #endif |
| 314 | platform_driver_unregister(&physmap_flash_driver); |
| 315 | } |
| 316 | |
| 317 | module_init(physmap_init); |
| 318 | module_exit(physmap_exit); |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | MODULE_LICENSE("GPL"); |
| 321 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 322 | MODULE_DESCRIPTION("Generic configurable MTD map driver"); |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 323 | |
| 324 | /* legacy platform drivers can't hotplug or coldplg */ |
| 325 | #ifndef PHYSMAP_COMPAT |
| 326 | /* work with hotplug and coldplug */ |
| 327 | MODULE_ALIAS("platform:physmap-flash"); |
| 328 | #endif |
| 329 | |