Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 15 | #include <linux/device.h> |
| 16 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/mtd/mtd.h> |
| 18 | #include <linux/mtd/map.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/mtd/partitions.h> |
Adrian Bunk | 2b9175c | 2005-11-29 14:49:38 +0000 | [diff] [blame] | 20 | #include <linux/mtd/physmap.h> |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 21 | #include <linux/mtd/concat.h> |
Atsushi Nemoto | 3136e90 | 2008-11-26 10:26:29 +0000 | [diff] [blame] | 22 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 24 | #define MAX_RESOURCES 4 |
| 25 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 26 | struct physmap_flash_info { |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 27 | struct mtd_info *mtd[MAX_RESOURCES]; |
| 28 | struct mtd_info *cmtd; |
| 29 | struct map_info map[MAX_RESOURCES]; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 30 | #ifdef CONFIG_MTD_PARTITIONS |
| 31 | int nr_parts; |
| 32 | struct mtd_partition *parts; |
| 33 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | }; |
| 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; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 40 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 42 | info = platform_get_drvdata(dev); |
| 43 | if (info == NULL) |
| 44 | return 0; |
| 45 | platform_set_drvdata(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 47 | physmap_data = dev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 49 | #ifdef CONFIG_MTD_CONCAT |
| 50 | if (info->cmtd != info->mtd[0]) { |
| 51 | del_mtd_device(info->cmtd); |
| 52 | mtd_concat_destroy(info->cmtd); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 53 | } |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 54 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 56 | for (i = 0; i < MAX_RESOURCES; i++) { |
| 57 | if (info->mtd[i] != NULL) { |
| 58 | #ifdef CONFIG_MTD_PARTITIONS |
| 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 | } |
| 67 | #else |
| 68 | del_mtd_device(info->mtd[i]); |
| 69 | #endif |
| 70 | map_destroy(info->mtd[i]); |
| 71 | } |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 72 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 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) |
| 82 | { |
| 83 | struct physmap_flash_data *physmap_data; |
| 84 | struct physmap_flash_info *info; |
| 85 | const char **probe_type; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 86 | int err = 0; |
| 87 | int i; |
| 88 | int devices_found = 0; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 89 | |
| 90 | physmap_data = dev->dev.platform_data; |
| 91 | if (physmap_data == NULL) |
| 92 | return -ENODEV; |
| 93 | |
Atsushi Nemoto | 3136e90 | 2008-11-26 10:26:29 +0000 | [diff] [blame] | 94 | info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info), |
| 95 | 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 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 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 | |
Atsushi Nemoto | 3136e90 | 2008-11-26 10:26:29 +0000 | [diff] [blame] | 108 | if (!devm_request_mem_region(&dev->dev, |
| 109 | dev->resource[i].start, |
| 110 | dev->resource[i].end - dev->resource[i].start + 1, |
Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 111 | dev_name(&dev->dev))) { |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 112 | dev_err(&dev->dev, "Could not reserve memory region\n"); |
| 113 | err = -ENOMEM; |
| 114 | goto err_out; |
| 115 | } |
| 116 | |
Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 117 | info->map[i].name = dev_name(&dev->dev); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 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 | |
Atsushi Nemoto | 3136e90 | 2008-11-26 10:26:29 +0000 | [diff] [blame] | 123 | info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys, |
| 124 | info->map[i].size); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 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 | } |
| 130 | |
| 131 | simple_map_init(&info->map[i]); |
| 132 | |
| 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; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 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 |
Kay Sievers | 475b44c | 2009-01-06 10:44:38 -0800 | [diff] [blame] | 153 | info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev)); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 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"); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 159 | err = -ENXIO; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 160 | #endif |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 161 | } |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 162 | if (err) |
| 163 | goto err_out; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 164 | |
| 165 | #ifdef CONFIG_MTD_PARTITIONS |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 166 | err = parse_mtd_partitions(info->cmtd, part_probe_types, &info->parts, 0); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 167 | if (err > 0) { |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 168 | add_mtd_partitions(info->cmtd, info->parts, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 172 | if (physmap_data->nr_parts) { |
| 173 | printk(KERN_NOTICE "Using physmap partition information\n"); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 174 | add_mtd_partitions(info->cmtd, physmap_data->parts, |
| 175 | physmap_data->nr_parts); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 176 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 180 | add_mtd_device(info->cmtd); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 181 | return 0; |
| 182 | |
| 183 | err_out: |
| 184 | physmap_flash_remove(dev); |
| 185 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 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; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 193 | int i; |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 194 | |
Anton Vorontsov | 4a5691c | 2008-03-28 14:16:09 -0700 | [diff] [blame] | 195 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
Uwe Kleine-König | 4b5e33a | 2008-07-22 09:39:01 +0200 | [diff] [blame] | 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 | } |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 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); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 216 | int i; |
| 217 | |
Anton Vorontsov | 4a5691c | 2008-03-28 14:16:09 -0700 | [diff] [blame] | 218 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
Robert Jarzmik | 7b24919 | 2008-07-22 09:39:00 +0200 | [diff] [blame] | 219 | if (info->mtd[i]->resume) |
| 220 | info->mtd[i]->resume(info->mtd[i]); |
Anton Vorontsov | 4a5691c | 2008-03-28 14:16:09 -0700 | [diff] [blame] | 221 | |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 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); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 228 | int i; |
| 229 | |
Anton Vorontsov | 4a5691c | 2008-03-28 14:16:09 -0700 | [diff] [blame] | 230 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
Robert Jarzmik | 7b24919 | 2008-07-22 09:39:00 +0200 | [diff] [blame] | 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]); |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 234 | } |
akpm@linux-foundation.org | d547668 | 2008-02-03 12:56:03 -0800 | [diff] [blame] | 235 | #else |
| 236 | #define physmap_flash_suspend NULL |
| 237 | #define physmap_flash_resume NULL |
| 238 | #define physmap_flash_shutdown NULL |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 239 | #endif |
| 240 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 241 | static struct platform_driver physmap_flash_driver = { |
| 242 | .probe = physmap_flash_probe, |
| 243 | .remove = physmap_flash_remove, |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 244 | .suspend = physmap_flash_suspend, |
| 245 | .resume = physmap_flash_resume, |
| 246 | .shutdown = physmap_flash_shutdown, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 247 | .driver = { |
| 248 | .name = "physmap-flash", |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 249 | .owner = THIS_MODULE, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 250 | }, |
| 251 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 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 |
| 259 | #endif |
| 260 | |
| 261 | #ifdef PHYSMAP_COMPAT |
| 262 | static struct physmap_flash_data physmap_flash_data = { |
| 263 | .width = CONFIG_MTD_PHYSMAP_BANKWIDTH, |
| 264 | }; |
| 265 | |
| 266 | static struct resource physmap_flash_resource = { |
| 267 | .start = CONFIG_MTD_PHYSMAP_START, |
Sascha Hauer | 6d4f822 | 2006-06-27 14:38:15 +0100 | [diff] [blame] | 268 | .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 269 | .flags = IORESOURCE_MEM, |
| 270 | }; |
| 271 | |
| 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)) |
| 284 | { |
| 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 | |
| 291 | #ifdef CONFIG_MTD_PARTITIONS |
| 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 |
| 298 | #endif |
| 299 | |
| 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; |
| 311 | } |
| 312 | |
| 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 | } |
| 320 | |
| 321 | module_init(physmap_init); |
| 322 | module_exit(physmap_exit); |
| 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | MODULE_LICENSE("GPL"); |
| 325 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 326 | MODULE_DESCRIPTION("Generic configurable MTD map driver"); |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 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 | |