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> |
| 21 | #include <linux/config.h> |
| 22 | #include <linux/mtd/partitions.h> |
Adrian Bunk | 2b9175c | 2005-11-29 14:49:38 +0000 | [diff] [blame] | 23 | #include <linux/mtd/physmap.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 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 26 | struct physmap_flash_info { |
| 27 | struct mtd_info *mtd; |
| 28 | struct map_info map; |
| 29 | struct resource *res; |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 37 | static int physmap_flash_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 39 | struct physmap_flash_info *info; |
| 40 | struct physmap_flash_data *physmap_data; |
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 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 49 | if (info->mtd != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | #ifdef CONFIG_MTD_PARTITIONS |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 51 | if (info->nr_parts) { |
| 52 | del_mtd_partitions(info->mtd); |
| 53 | kfree(info->parts); |
| 54 | } else if (physmap_data->nr_parts) { |
| 55 | del_mtd_partitions(info->mtd); |
| 56 | } else { |
| 57 | del_mtd_device(info->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 59 | #else |
| 60 | del_mtd_device(info->mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | #endif |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 62 | map_destroy(info->mtd); |
| 63 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 65 | if (info->map.virt != NULL) |
| 66 | iounmap((void *)info->map.virt); |
| 67 | |
| 68 | if (info->res != NULL) { |
| 69 | release_resource(info->res); |
| 70 | kfree(info->res); |
| 71 | } |
| 72 | |
| 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; |
| 86 | int err; |
| 87 | |
| 88 | physmap_data = dev->dev.platform_data; |
| 89 | if (physmap_data == NULL) |
| 90 | return -ENODEV; |
| 91 | |
Andrew Morton | f24ff6b | 2006-06-09 15:12:34 +0100 | [diff] [blame] | 92 | printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n", |
| 93 | (unsigned long long)dev->resource->end - dev->resource->start + 1, |
| 94 | (unsigned long long)dev->resource->start); |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 95 | |
| 96 | info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL); |
| 97 | if (info == NULL) { |
| 98 | err = -ENOMEM; |
| 99 | goto err_out; |
| 100 | } |
| 101 | memset(info, 0, sizeof(*info)); |
| 102 | |
| 103 | platform_set_drvdata(dev, info); |
| 104 | |
| 105 | info->res = request_mem_region(dev->resource->start, |
| 106 | dev->resource->end - dev->resource->start + 1, |
| 107 | dev->dev.bus_id); |
| 108 | if (info->res == NULL) { |
| 109 | dev_err(&dev->dev, "Could not reserve memory region\n"); |
| 110 | err = -ENOMEM; |
| 111 | goto err_out; |
| 112 | } |
| 113 | |
| 114 | info->map.name = dev->dev.bus_id; |
| 115 | info->map.phys = dev->resource->start; |
| 116 | info->map.size = dev->resource->end - dev->resource->start + 1; |
| 117 | info->map.bankwidth = physmap_data->width; |
| 118 | info->map.set_vpp = physmap_data->set_vpp; |
| 119 | |
| 120 | info->map.virt = ioremap(info->map.phys, info->map.size); |
| 121 | if (info->map.virt == NULL) { |
| 122 | dev_err(&dev->dev, "Failed to ioremap flash region\n"); |
| 123 | err = EIO; |
| 124 | goto err_out; |
| 125 | } |
| 126 | |
| 127 | simple_map_init(&info->map); |
| 128 | |
| 129 | probe_type = rom_probe_types; |
| 130 | for (; info->mtd == NULL && *probe_type != NULL; probe_type++) |
| 131 | info->mtd = do_map_probe(*probe_type, &info->map); |
| 132 | if (info->mtd == NULL) { |
| 133 | dev_err(&dev->dev, "map_probe failed\n"); |
| 134 | err = -ENXIO; |
| 135 | goto err_out; |
| 136 | } |
| 137 | info->mtd->owner = THIS_MODULE; |
| 138 | |
| 139 | #ifdef CONFIG_MTD_PARTITIONS |
| 140 | err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0); |
| 141 | if (err > 0) { |
| 142 | add_mtd_partitions(info->mtd, info->parts, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 146 | if (physmap_data->nr_parts) { |
| 147 | printk(KERN_NOTICE "Using physmap partition information\n"); |
| 148 | add_mtd_partitions(info->mtd, physmap_data->parts, |
| 149 | physmap_data->nr_parts); |
| 150 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 154 | add_mtd_device(info->mtd); |
| 155 | return 0; |
| 156 | |
| 157 | err_out: |
| 158 | physmap_flash_remove(dev); |
| 159 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 162 | static struct platform_driver physmap_flash_driver = { |
| 163 | .probe = physmap_flash_probe, |
| 164 | .remove = physmap_flash_remove, |
| 165 | .driver = { |
| 166 | .name = "physmap-flash", |
| 167 | }, |
| 168 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 171 | #ifdef CONFIG_MTD_PHYSMAP_LEN |
| 172 | #if CONFIG_MTD_PHYSMAP_LEN != 0 |
| 173 | #warning using PHYSMAP compat code |
| 174 | #define PHYSMAP_COMPAT |
| 175 | #endif |
| 176 | #endif |
| 177 | |
| 178 | #ifdef PHYSMAP_COMPAT |
| 179 | static struct physmap_flash_data physmap_flash_data = { |
| 180 | .width = CONFIG_MTD_PHYSMAP_BANKWIDTH, |
| 181 | }; |
| 182 | |
| 183 | static struct resource physmap_flash_resource = { |
| 184 | .start = CONFIG_MTD_PHYSMAP_START, |
Sascha Hauer | 6d4f822 | 2006-06-27 14:38:15 +0100 | [diff] [blame^] | 185 | .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1, |
Lennert Buytenhek | 73566ed | 2006-05-07 17:16:36 +0100 | [diff] [blame] | 186 | .flags = IORESOURCE_MEM, |
| 187 | }; |
| 188 | |
| 189 | static struct platform_device physmap_flash = { |
| 190 | .name = "physmap-flash", |
| 191 | .id = 0, |
| 192 | .dev = { |
| 193 | .platform_data = &physmap_flash_data, |
| 194 | }, |
| 195 | .num_resources = 1, |
| 196 | .resource = &physmap_flash_resource, |
| 197 | }; |
| 198 | |
| 199 | void physmap_configure(unsigned long addr, unsigned long size, |
| 200 | int bankwidth, void (*set_vpp)(struct map_info *, int)) |
| 201 | { |
| 202 | physmap_flash_resource.start = addr; |
| 203 | physmap_flash_resource.end = addr + size - 1; |
| 204 | physmap_flash_data.width = bankwidth; |
| 205 | physmap_flash_data.set_vpp = set_vpp; |
| 206 | } |
| 207 | |
| 208 | #ifdef CONFIG_MTD_PARTITIONS |
| 209 | void physmap_set_partitions(struct mtd_partition *parts, int num_parts) |
| 210 | { |
| 211 | physmap_flash_data.nr_parts = num_parts; |
| 212 | physmap_flash_data.parts = parts; |
| 213 | } |
| 214 | #endif |
| 215 | #endif |
| 216 | |
| 217 | static int __init physmap_init(void) |
| 218 | { |
| 219 | int err; |
| 220 | |
| 221 | err = platform_driver_register(&physmap_flash_driver); |
| 222 | #ifdef PHYSMAP_COMPAT |
| 223 | if (err == 0) |
| 224 | platform_device_register(&physmap_flash); |
| 225 | #endif |
| 226 | |
| 227 | return err; |
| 228 | } |
| 229 | |
| 230 | static void __exit physmap_exit(void) |
| 231 | { |
| 232 | #ifdef PHYSMAP_COMPAT |
| 233 | platform_device_unregister(&physmap_flash); |
| 234 | #endif |
| 235 | platform_driver_unregister(&physmap_flash_driver); |
| 236 | } |
| 237 | |
| 238 | module_init(physmap_init); |
| 239 | module_exit(physmap_exit); |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | MODULE_LICENSE("GPL"); |
| 242 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 243 | MODULE_DESCRIPTION("Generic configurable MTD map driver"); |