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> |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 23 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 36 | static int physmap_flash_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 38 | struct physmap_flash_info *info; |
| 39 | struct physmap_flash_data *physmap_data; |
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 | info = platform_get_drvdata(dev); |
| 42 | if (info == NULL) |
| 43 | return 0; |
| 44 | platform_set_drvdata(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 46 | physmap_data = dev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 48 | if (info->mtd != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #ifdef CONFIG_MTD_PARTITIONS |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 58 | #else |
| 59 | del_mtd_device(info->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | #endif |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 61 | map_destroy(info->mtd); |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 64 | if (info->map.virt != NULL) |
Al Viro | afc12d3 | 2006-10-10 22:46:37 +0100 | [diff] [blame] | 65 | iounmap(info->map.virt); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 66 | |
| 67 | if (info->res != NULL) { |
| 68 | release_resource(info->res); |
| 69 | kfree(info->res); |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 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) |
| 81 | { |
| 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 | |
Andrew Morton | f24ff6b | 2006-06-09 15:12:34 +0100 | [diff] [blame] | 91 | printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n", |
Randy Dunlap | 998a43e | 2006-11-28 23:40:46 +0000 | [diff] [blame] | 92 | (unsigned long long)(dev->resource->end - dev->resource->start + 1), |
Andrew Morton | f24ff6b | 2006-06-09 15:12:34 +0100 | [diff] [blame] | 93 | (unsigned long long)dev->resource->start); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 94 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 95 | info = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 96 | if (info == NULL) { |
| 97 | err = -ENOMEM; |
| 98 | goto err_out; |
| 99 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 100 | |
| 101 | platform_set_drvdata(dev, info); |
| 102 | |
| 103 | info->res = request_mem_region(dev->resource->start, |
| 104 | dev->resource->end - dev->resource->start + 1, |
| 105 | dev->dev.bus_id); |
| 106 | if (info->res == NULL) { |
| 107 | dev_err(&dev->dev, "Could not reserve memory region\n"); |
| 108 | err = -ENOMEM; |
| 109 | goto err_out; |
| 110 | } |
| 111 | |
| 112 | info->map.name = dev->dev.bus_id; |
| 113 | info->map.phys = dev->resource->start; |
| 114 | info->map.size = dev->resource->end - dev->resource->start + 1; |
| 115 | info->map.bankwidth = physmap_data->width; |
| 116 | info->map.set_vpp = physmap_data->set_vpp; |
| 117 | |
| 118 | info->map.virt = ioremap(info->map.phys, info->map.size); |
| 119 | if (info->map.virt == NULL) { |
| 120 | dev_err(&dev->dev, "Failed to ioremap flash region\n"); |
| 121 | err = EIO; |
| 122 | goto err_out; |
| 123 | } |
| 124 | |
| 125 | simple_map_init(&info->map); |
| 126 | |
| 127 | probe_type = rom_probe_types; |
| 128 | for (; info->mtd == NULL && *probe_type != NULL; probe_type++) |
| 129 | info->mtd = do_map_probe(*probe_type, &info->map); |
| 130 | if (info->mtd == NULL) { |
| 131 | dev_err(&dev->dev, "map_probe failed\n"); |
| 132 | err = -ENXIO; |
| 133 | goto err_out; |
| 134 | } |
| 135 | info->mtd->owner = THIS_MODULE; |
| 136 | |
| 137 | #ifdef CONFIG_MTD_PARTITIONS |
| 138 | err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0); |
| 139 | if (err > 0) { |
| 140 | add_mtd_partitions(info->mtd, info->parts, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 144 | if (physmap_data->nr_parts) { |
| 145 | printk(KERN_NOTICE "Using physmap partition information\n"); |
| 146 | add_mtd_partitions(info->mtd, physmap_data->parts, |
| 147 | physmap_data->nr_parts); |
| 148 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 152 | add_mtd_device(info->mtd); |
| 153 | return 0; |
| 154 | |
| 155 | err_out: |
| 156 | physmap_flash_remove(dev); |
| 157 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 160 | #ifdef CONFIG_PM |
| 161 | static int physmap_flash_suspend(struct platform_device *dev, pm_message_t state) |
| 162 | { |
| 163 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
| 164 | int ret = 0; |
| 165 | |
| 166 | if (info) |
| 167 | ret = info->mtd->suspend(info->mtd); |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | static int physmap_flash_resume(struct platform_device *dev) |
| 173 | { |
| 174 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
| 175 | if (info) |
| 176 | info->mtd->resume(info->mtd); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static void physmap_flash_shutdown(struct platform_device *dev) |
| 181 | { |
| 182 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
| 183 | if (info && info->mtd->suspend(info->mtd) == 0) |
| 184 | info->mtd->resume(info->mtd); |
| 185 | } |
| 186 | #endif |
| 187 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 188 | static struct platform_driver physmap_flash_driver = { |
| 189 | .probe = physmap_flash_probe, |
| 190 | .remove = physmap_flash_remove, |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 191 | #ifdef CONFIG_PM |
| 192 | .suspend = physmap_flash_suspend, |
| 193 | .resume = physmap_flash_resume, |
| 194 | .shutdown = physmap_flash_shutdown, |
| 195 | #endif |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 196 | .driver = { |
| 197 | .name = "physmap-flash", |
| 198 | }, |
| 199 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 202 | #ifdef CONFIG_MTD_PHYSMAP_LEN |
| 203 | #if CONFIG_MTD_PHYSMAP_LEN != 0 |
| 204 | #warning using PHYSMAP compat code |
| 205 | #define PHYSMAP_COMPAT |
| 206 | #endif |
| 207 | #endif |
| 208 | |
| 209 | #ifdef PHYSMAP_COMPAT |
| 210 | static struct physmap_flash_data physmap_flash_data = { |
| 211 | .width = CONFIG_MTD_PHYSMAP_BANKWIDTH, |
| 212 | }; |
| 213 | |
| 214 | static struct resource physmap_flash_resource = { |
| 215 | .start = CONFIG_MTD_PHYSMAP_START, |
Sascha Hauer | 6d4f822 | 2006-06-27 14:38:15 +0100 | [diff] [blame] | 216 | .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 217 | .flags = IORESOURCE_MEM, |
| 218 | }; |
| 219 | |
| 220 | static struct platform_device physmap_flash = { |
| 221 | .name = "physmap-flash", |
| 222 | .id = 0, |
| 223 | .dev = { |
| 224 | .platform_data = &physmap_flash_data, |
| 225 | }, |
| 226 | .num_resources = 1, |
| 227 | .resource = &physmap_flash_resource, |
| 228 | }; |
| 229 | |
| 230 | void physmap_configure(unsigned long addr, unsigned long size, |
| 231 | int bankwidth, void (*set_vpp)(struct map_info *, int)) |
| 232 | { |
| 233 | physmap_flash_resource.start = addr; |
| 234 | physmap_flash_resource.end = addr + size - 1; |
| 235 | physmap_flash_data.width = bankwidth; |
| 236 | physmap_flash_data.set_vpp = set_vpp; |
| 237 | } |
| 238 | |
| 239 | #ifdef CONFIG_MTD_PARTITIONS |
| 240 | void physmap_set_partitions(struct mtd_partition *parts, int num_parts) |
| 241 | { |
| 242 | physmap_flash_data.nr_parts = num_parts; |
| 243 | physmap_flash_data.parts = parts; |
| 244 | } |
| 245 | #endif |
| 246 | #endif |
| 247 | |
| 248 | static int __init physmap_init(void) |
| 249 | { |
| 250 | int err; |
| 251 | |
| 252 | err = platform_driver_register(&physmap_flash_driver); |
| 253 | #ifdef PHYSMAP_COMPAT |
| 254 | if (err == 0) |
| 255 | platform_device_register(&physmap_flash); |
| 256 | #endif |
| 257 | |
| 258 | return err; |
| 259 | } |
| 260 | |
| 261 | static void __exit physmap_exit(void) |
| 262 | { |
| 263 | #ifdef PHYSMAP_COMPAT |
| 264 | platform_device_unregister(&physmap_flash); |
| 265 | #endif |
| 266 | platform_driver_unregister(&physmap_flash_driver); |
| 267 | } |
| 268 | |
| 269 | module_init(physmap_init); |
| 270 | module_exit(physmap_exit); |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | MODULE_LICENSE("GPL"); |
| 273 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 274 | MODULE_DESCRIPTION("Generic configurable MTD map driver"); |