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]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 32 | static int physmap_flash_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | { |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 34 | struct physmap_flash_info *info; |
| 35 | struct physmap_flash_data *physmap_data; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 36 | int i; |
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 | info = platform_get_drvdata(dev); |
| 39 | if (info == NULL) |
| 40 | return 0; |
| 41 | platform_set_drvdata(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 43 | physmap_data = dev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
H Hartley Sweeten | 8ce110a | 2009-10-20 12:23:33 -0400 | [diff] [blame] | 45 | if (info->cmtd) { |
Jamie Iles | 984e6d8 | 2011-05-23 10:22:45 +0100 | [diff] [blame] | 46 | mtd_device_unregister(info->cmtd); |
H Hartley Sweeten | 8ce110a | 2009-10-20 12:23:33 -0400 | [diff] [blame] | 47 | if (info->cmtd != info->mtd[0]) |
| 48 | mtd_concat_destroy(info->cmtd); |
H Hartley Sweeten | 8ce110a | 2009-10-20 12:23:33 -0400 | [diff] [blame] | 49 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 51 | for (i = 0; i < MAX_RESOURCES; i++) { |
Atsushi Nemoto | e480814 | 2009-02-11 13:12:17 -0800 | [diff] [blame] | 52 | if (info->mtd[i] != NULL) |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 53 | map_destroy(info->mtd[i]); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 54 | } |
Marc Zyngier | b7281ca | 2011-05-18 10:51:48 +0100 | [diff] [blame] | 55 | |
| 56 | if (physmap_data->exit) |
| 57 | physmap_data->exit(dev); |
| 58 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
Marc Zyngier | 667f390 | 2011-05-18 10:51:55 +0100 | [diff] [blame] | 62 | static void physmap_set_vpp(struct map_info *map, int state) |
| 63 | { |
| 64 | struct platform_device *pdev; |
| 65 | struct physmap_flash_data *physmap_data; |
| 66 | |
| 67 | pdev = (struct platform_device *)map->map_priv_1; |
| 68 | physmap_data = pdev->dev.platform_data; |
| 69 | |
| 70 | if (physmap_data->set_vpp) |
| 71 | physmap_data->set_vpp(pdev, state); |
| 72 | } |
| 73 | |
Alexey Korolev | d814083 | 2008-12-16 18:22:39 +0000 | [diff] [blame] | 74 | static const char *rom_probe_types[] = { |
| 75 | "cfi_probe", |
| 76 | "jedec_probe", |
| 77 | "qinfo_probe", |
| 78 | "map_rom", |
| 79 | NULL }; |
Marc Zyngier | 667f390 | 2011-05-18 10:51:55 +0100 | [diff] [blame] | 80 | static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", "afs", |
Marc Zyngier | b7281ca | 2011-05-18 10:51:48 +0100 | [diff] [blame] | 81 | NULL }; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 82 | |
| 83 | static int physmap_flash_probe(struct platform_device *dev) |
| 84 | { |
| 85 | struct physmap_flash_data *physmap_data; |
| 86 | struct physmap_flash_info *info; |
| 87 | const char **probe_type; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 88 | int err = 0; |
| 89 | int i; |
| 90 | int devices_found = 0; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 91 | |
| 92 | physmap_data = dev->dev.platform_data; |
| 93 | if (physmap_data == NULL) |
| 94 | return -ENODEV; |
| 95 | |
Atsushi Nemoto | 3136e90 | 2008-11-26 10:26:29 +0000 | [diff] [blame] | 96 | info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info), |
| 97 | GFP_KERNEL); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 98 | if (info == NULL) { |
| 99 | err = -ENOMEM; |
| 100 | goto err_out; |
| 101 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 102 | |
Marc Zyngier | b7281ca | 2011-05-18 10:51:48 +0100 | [diff] [blame] | 103 | if (physmap_data->init) { |
| 104 | err = physmap_data->init(dev); |
| 105 | if (err) |
| 106 | goto err_out; |
| 107 | } |
| 108 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 109 | platform_set_drvdata(dev, info); |
| 110 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 111 | for (i = 0; i < dev->num_resources; i++) { |
| 112 | printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n", |
H Hartley Sweeten | 1d90d2c | 2010-06-14 11:57:54 -0500 | [diff] [blame] | 113 | (unsigned long long)resource_size(&dev->resource[i]), |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 114 | (unsigned long long)dev->resource[i].start); |
| 115 | |
Atsushi Nemoto | 3136e90 | 2008-11-26 10:26:29 +0000 | [diff] [blame] | 116 | if (!devm_request_mem_region(&dev->dev, |
| 117 | dev->resource[i].start, |
H Hartley Sweeten | 1d90d2c | 2010-06-14 11:57:54 -0500 | [diff] [blame] | 118 | resource_size(&dev->resource[i]), |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 119 | dev_name(&dev->dev))) { |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 120 | dev_err(&dev->dev, "Could not reserve memory region\n"); |
| 121 | err = -ENOMEM; |
| 122 | goto err_out; |
| 123 | } |
| 124 | |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 125 | info->map[i].name = dev_name(&dev->dev); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 126 | info->map[i].phys = dev->resource[i].start; |
H Hartley Sweeten | 1d90d2c | 2010-06-14 11:57:54 -0500 | [diff] [blame] | 127 | info->map[i].size = resource_size(&dev->resource[i]); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 128 | info->map[i].bankwidth = physmap_data->width; |
Marc Zyngier | 667f390 | 2011-05-18 10:51:55 +0100 | [diff] [blame] | 129 | info->map[i].set_vpp = physmap_set_vpp; |
Alexey Korolev | d814083 | 2008-12-16 18:22:39 +0000 | [diff] [blame] | 130 | info->map[i].pfow_base = physmap_data->pfow_base; |
Marc Zyngier | 667f390 | 2011-05-18 10:51:55 +0100 | [diff] [blame] | 131 | info->map[i].map_priv_1 = (unsigned long)dev; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 132 | |
Atsushi Nemoto | 3136e90 | 2008-11-26 10:26:29 +0000 | [diff] [blame] | 133 | info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys, |
| 134 | info->map[i].size); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 135 | if (info->map[i].virt == NULL) { |
| 136 | dev_err(&dev->dev, "Failed to ioremap flash region\n"); |
Roel Kluin | 895fb49 | 2009-11-11 21:47:06 +0100 | [diff] [blame] | 137 | err = -EIO; |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 138 | goto err_out; |
| 139 | } |
| 140 | |
| 141 | simple_map_init(&info->map[i]); |
| 142 | |
| 143 | probe_type = rom_probe_types; |
Barry Song | 78ef7fa | 2010-01-15 15:50:14 +0800 | [diff] [blame] | 144 | if (physmap_data->probe_type == NULL) { |
| 145 | for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++) |
| 146 | info->mtd[i] = do_map_probe(*probe_type, &info->map[i]); |
| 147 | } else |
| 148 | info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]); |
| 149 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 150 | if (info->mtd[i] == NULL) { |
| 151 | dev_err(&dev->dev, "map_probe failed\n"); |
| 152 | err = -ENXIO; |
| 153 | goto err_out; |
| 154 | } else { |
| 155 | devices_found++; |
| 156 | } |
| 157 | info->mtd[i]->owner = THIS_MODULE; |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 158 | info->mtd[i]->dev.parent = &dev->dev; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 161 | if (devices_found == 1) { |
| 162 | info->cmtd = info->mtd[0]; |
| 163 | } else if (devices_found > 1) { |
| 164 | /* |
| 165 | * We detected multiple devices. Concatenate them together. |
| 166 | */ |
Kay Sievers | 160bbab | 2008-12-23 10:00:14 +0000 | [diff] [blame] | 167 | 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] | 168 | if (info->cmtd == NULL) |
| 169 | err = -ENXIO; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 170 | } |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 171 | if (err) |
| 172 | goto err_out; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 173 | |
Dmitry Eremin-Solenikov | d45fd12 | 2011-06-02 17:59:58 +0400 | [diff] [blame] | 174 | mtd_device_parse_register(info->cmtd, part_probe_types, 0, |
| 175 | physmap_data->parts, physmap_data->nr_parts); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 176 | return 0; |
| 177 | |
| 178 | err_out: |
| 179 | physmap_flash_remove(dev); |
| 180 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 183 | #ifdef CONFIG_PM |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 184 | static void physmap_flash_shutdown(struct platform_device *dev) |
| 185 | { |
| 186 | struct physmap_flash_info *info = platform_get_drvdata(dev); |
Stefan Roese | df66e71 | 2008-02-01 15:26:54 +0100 | [diff] [blame] | 187 | int i; |
| 188 | |
Anton Vorontsov | 4a5691c | 2008-03-28 14:16:09 -0700 | [diff] [blame] | 189 | for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++) |
Robert Jarzmik | 7b24919 | 2008-07-22 09:39:00 +0200 | [diff] [blame] | 190 | if (info->mtd[i]->suspend && info->mtd[i]->resume) |
| 191 | if (info->mtd[i]->suspend(info->mtd[i]) == 0) |
| 192 | info->mtd[i]->resume(info->mtd[i]); |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 193 | } |
akpm@linux-foundation.org | d547668 | 2008-02-03 12:56:03 -0800 | [diff] [blame] | 194 | #else |
akpm@linux-foundation.org | d547668 | 2008-02-03 12:56:03 -0800 | [diff] [blame] | 195 | #define physmap_flash_shutdown NULL |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 196 | #endif |
| 197 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 198 | static struct platform_driver physmap_flash_driver = { |
| 199 | .probe = physmap_flash_probe, |
| 200 | .remove = physmap_flash_remove, |
Lennert Buytenhek | 17c2dae | 2006-09-21 23:16:48 +0200 | [diff] [blame] | 201 | .shutdown = physmap_flash_shutdown, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 202 | .driver = { |
| 203 | .name = "physmap-flash", |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 204 | .owner = THIS_MODULE, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 205 | }, |
| 206 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | |
Mike Frysinger | dcb3e13 | 2008-12-01 14:23:40 -0800 | [diff] [blame] | 209 | #ifdef CONFIG_MTD_PHYSMAP_COMPAT |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 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 | }; |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 229 | #endif |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 230 | |
| 231 | static int __init physmap_init(void) |
| 232 | { |
| 233 | int err; |
| 234 | |
| 235 | err = platform_driver_register(&physmap_flash_driver); |
Mike Frysinger | dcb3e13 | 2008-12-01 14:23:40 -0800 | [diff] [blame] | 236 | #ifdef CONFIG_MTD_PHYSMAP_COMPAT |
H Hartley Sweeten | 1ca5d2f | 2010-04-02 17:46:30 -0500 | [diff] [blame] | 237 | if (err == 0) { |
| 238 | err = platform_device_register(&physmap_flash); |
| 239 | if (err) |
| 240 | platform_driver_unregister(&physmap_flash_driver); |
| 241 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 242 | #endif |
| 243 | |
| 244 | return err; |
| 245 | } |
| 246 | |
| 247 | static void __exit physmap_exit(void) |
| 248 | { |
Mike Frysinger | dcb3e13 | 2008-12-01 14:23:40 -0800 | [diff] [blame] | 249 | #ifdef CONFIG_MTD_PHYSMAP_COMPAT |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 250 | platform_device_unregister(&physmap_flash); |
| 251 | #endif |
| 252 | platform_driver_unregister(&physmap_flash_driver); |
| 253 | } |
| 254 | |
| 255 | module_init(physmap_init); |
| 256 | module_exit(physmap_exit); |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | MODULE_LICENSE("GPL"); |
| 259 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 260 | MODULE_DESCRIPTION("Generic configurable MTD map driver"); |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 261 | |
| 262 | /* legacy platform drivers can't hotplug or coldplg */ |
Mike Frysinger | dcb3e13 | 2008-12-01 14:23:40 -0800 | [diff] [blame] | 263 | #ifndef CONFIG_MTD_PHYSMAP_COMPAT |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 264 | /* work with hotplug and coldplug */ |
| 265 | MODULE_ALIAS("platform:physmap-flash"); |
| 266 | #endif |