| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /*====================================================================== | 
|  | 2 |  | 
|  | 3 | drivers/mtd/maps/integrator-flash.c: ARM Integrator flash map driver | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 4 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | Copyright (C) 2000 ARM Limited | 
|  | 6 | Copyright (C) 2003 Deep Blue Solutions Ltd. | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 7 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | This program is free software; you can redistribute it and/or modify | 
|  | 9 | it under the terms of the GNU General Public License as published by | 
|  | 10 | the Free Software Foundation; either version 2 of the License, or | 
|  | 11 | (at your option) any later version. | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 12 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | This program is distributed in the hope that it will be useful, | 
|  | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 16 | GNU General Public License for more details. | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 17 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License | 
|  | 19 | along with this program; if not, write to the Free Software | 
|  | 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | This is access code for flashes using ARM's flash partitioning | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | standards. | 
|  | 24 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | ======================================================================*/ | 
|  | 26 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/module.h> | 
|  | 28 | #include <linux/types.h> | 
|  | 29 | #include <linux/kernel.h> | 
|  | 30 | #include <linux/slab.h> | 
|  | 31 | #include <linux/ioport.h> | 
| Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 32 | #include <linux/platform_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/init.h> | 
| Russell King | 9973022 | 2009-03-25 10:21:35 +0000 | [diff] [blame] | 34 | #include <linux/io.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | #include <linux/mtd/mtd.h> | 
|  | 37 | #include <linux/mtd/map.h> | 
|  | 38 | #include <linux/mtd/partitions.h> | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 39 | #include <linux/mtd/concat.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | #include <asm/mach/flash.h> | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 42 | #include <mach/hardware.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/system.h> | 
|  | 44 |  | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 45 | struct armflash_subdev_info { | 
| David Woodhouse | 17659c6 | 2009-06-24 15:35:15 +0100 | [diff] [blame] | 46 | char			*name; | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 47 | struct mtd_info		*mtd; | 
|  | 48 | struct map_info		map; | 
|  | 49 | struct flash_platform_data *plat; | 
|  | 50 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | struct armflash_info { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | struct resource		*res; | 
|  | 54 | struct mtd_partition	*parts; | 
|  | 55 | struct mtd_info		*mtd; | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 56 | int			nr_subdev; | 
|  | 57 | struct armflash_subdev_info subdev[0]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | }; | 
|  | 59 |  | 
|  | 60 | static void armflash_set_vpp(struct map_info *map, int on) | 
|  | 61 | { | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 62 | struct armflash_subdev_info *info = | 
|  | 63 | container_of(map, struct armflash_subdev_info, map); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | if (info->plat && info->plat->set_vpp) | 
|  | 66 | info->plat->set_vpp(on); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | static const char *probes[] = { "cmdlinepart", "RedBoot", "afs", NULL }; | 
|  | 70 |  | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 71 | static int armflash_subdev_probe(struct armflash_subdev_info *subdev, | 
|  | 72 | struct resource *res) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 74 | struct flash_platform_data *plat = subdev->plat; | 
|  | 75 | resource_size_t size = res->end - res->start + 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | void __iomem *base; | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 77 | int err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 |  | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 79 | if (!request_mem_region(res->start, size, subdev->name)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | err = -EBUSY; | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 81 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | base = ioremap(res->start, size); | 
|  | 85 | if (!base) { | 
|  | 86 | err = -ENOMEM; | 
|  | 87 | goto no_mem; | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | /* | 
|  | 91 | * look for CFI based flash parts fitted to this board | 
|  | 92 | */ | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 93 | subdev->map.size	= size; | 
|  | 94 | subdev->map.bankwidth	= plat->width; | 
|  | 95 | subdev->map.phys	= res->start; | 
|  | 96 | subdev->map.virt	= base; | 
|  | 97 | subdev->map.name	= subdev->name; | 
|  | 98 | subdev->map.set_vpp	= armflash_set_vpp; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 |  | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 100 | simple_map_init(&subdev->map); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | /* | 
|  | 103 | * Also, the CFI layer automatically works out what size | 
|  | 104 | * of chips we have, and does the necessary identification | 
|  | 105 | * for us automatically. | 
|  | 106 | */ | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 107 | subdev->mtd = do_map_probe(plat->map_name, &subdev->map); | 
|  | 108 | if (!subdev->mtd) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | err = -ENXIO; | 
|  | 110 | goto no_device; | 
|  | 111 | } | 
|  | 112 |  | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 113 | subdev->mtd->owner = THIS_MODULE; | 
|  | 114 |  | 
|  | 115 | /* Successful? */ | 
|  | 116 | if (err == 0) | 
|  | 117 | return err; | 
|  | 118 |  | 
|  | 119 | if (subdev->mtd) | 
|  | 120 | map_destroy(subdev->mtd); | 
|  | 121 | no_device: | 
|  | 122 | iounmap(base); | 
|  | 123 | no_mem: | 
|  | 124 | release_mem_region(res->start, size); | 
|  | 125 | out: | 
|  | 126 | return err; | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | static void armflash_subdev_remove(struct armflash_subdev_info *subdev) | 
|  | 130 | { | 
|  | 131 | if (subdev->mtd) | 
|  | 132 | map_destroy(subdev->mtd); | 
|  | 133 | if (subdev->map.virt) | 
|  | 134 | iounmap(subdev->map.virt); | 
| David Woodhouse | 17659c6 | 2009-06-24 15:35:15 +0100 | [diff] [blame] | 135 | kfree(subdev->name); | 
|  | 136 | subdev->name = NULL; | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 137 | release_mem_region(subdev->map.phys, subdev->map.size); | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | static int armflash_probe(struct platform_device *dev) | 
|  | 141 | { | 
|  | 142 | struct flash_platform_data *plat = dev->dev.platform_data; | 
|  | 143 | unsigned int size; | 
|  | 144 | struct armflash_info *info; | 
|  | 145 | int i, nr, err; | 
|  | 146 |  | 
|  | 147 | /* Count the number of devices */ | 
|  | 148 | for (nr = 0; ; nr++) | 
|  | 149 | if (!platform_get_resource(dev, IORESOURCE_MEM, nr)) | 
|  | 150 | break; | 
|  | 151 | if (nr == 0) { | 
|  | 152 | err = -ENODEV; | 
|  | 153 | goto out; | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | size = sizeof(struct armflash_info) + | 
|  | 157 | sizeof(struct armflash_subdev_info) * nr; | 
|  | 158 | info = kzalloc(size, GFP_KERNEL); | 
|  | 159 | if (!info) { | 
|  | 160 | err = -ENOMEM; | 
|  | 161 | goto out; | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | if (plat && plat->init) { | 
|  | 165 | err = plat->init(); | 
|  | 166 | if (err) | 
|  | 167 | goto no_resource; | 
|  | 168 | } | 
|  | 169 |  | 
|  | 170 | for (i = 0; i < nr; i++) { | 
|  | 171 | struct armflash_subdev_info *subdev = &info->subdev[i]; | 
|  | 172 | struct resource *res; | 
|  | 173 |  | 
|  | 174 | res = platform_get_resource(dev, IORESOURCE_MEM, i); | 
|  | 175 | if (!res) | 
|  | 176 | break; | 
|  | 177 |  | 
|  | 178 | if (nr == 1) | 
|  | 179 | /* No MTD concatenation, just use the default name */ | 
| David Woodhouse | 17659c6 | 2009-06-24 15:35:15 +0100 | [diff] [blame] | 180 | subdev->name = kstrdup(dev_name(&dev->dev), GFP_KERNEL); | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 181 | else | 
| David Woodhouse | 17659c6 | 2009-06-24 15:35:15 +0100 | [diff] [blame] | 182 | subdev->name = kasprintf(GFP_KERNEL, "%s-%d", | 
|  | 183 | dev_name(&dev->dev), i); | 
|  | 184 | if (!subdev->name) { | 
|  | 185 | err = -ENOMEM; | 
|  | 186 | break; | 
|  | 187 | } | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 188 | subdev->plat = plat; | 
|  | 189 |  | 
|  | 190 | err = armflash_subdev_probe(subdev, res); | 
| David Woodhouse | 17659c6 | 2009-06-24 15:35:15 +0100 | [diff] [blame] | 191 | if (err) { | 
|  | 192 | kfree(subdev->name); | 
|  | 193 | subdev->name = NULL; | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 194 | break; | 
| David Woodhouse | 17659c6 | 2009-06-24 15:35:15 +0100 | [diff] [blame] | 195 | } | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 196 | } | 
|  | 197 | info->nr_subdev = i; | 
|  | 198 |  | 
|  | 199 | if (err) | 
|  | 200 | goto subdev_err; | 
|  | 201 |  | 
|  | 202 | if (info->nr_subdev == 1) | 
|  | 203 | info->mtd = info->subdev[0].mtd; | 
|  | 204 | else if (info->nr_subdev > 1) { | 
|  | 205 | #ifdef CONFIG_MTD_CONCAT | 
|  | 206 | struct mtd_info *cdev[info->nr_subdev]; | 
|  | 207 |  | 
|  | 208 | /* | 
|  | 209 | * We detected multiple devices.  Concatenate them together. | 
|  | 210 | */ | 
|  | 211 | for (i = 0; i < info->nr_subdev; i++) | 
|  | 212 | cdev[i] = info->subdev[i].mtd; | 
|  | 213 |  | 
|  | 214 | info->mtd = mtd_concat_create(cdev, info->nr_subdev, | 
|  | 215 | dev_name(&dev->dev)); | 
|  | 216 | if (info->mtd == NULL) | 
|  | 217 | err = -ENXIO; | 
|  | 218 | #else | 
|  | 219 | printk(KERN_ERR "armflash: multiple devices found but " | 
|  | 220 | "MTD concat support disabled.\n"); | 
|  | 221 | err = -ENXIO; | 
|  | 222 | #endif | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | if (err < 0) | 
|  | 226 | goto cleanup; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 |  | 
|  | 228 | err = parse_mtd_partitions(info->mtd, probes, &info->parts, 0); | 
|  | 229 | if (err > 0) { | 
|  | 230 | err = add_mtd_partitions(info->mtd, info->parts, err); | 
|  | 231 | if (err) | 
|  | 232 | printk(KERN_ERR | 
|  | 233 | "mtd partition registration failed: %d\n", err); | 
|  | 234 | } | 
|  | 235 |  | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 236 | if (err == 0) { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 237 | platform_set_drvdata(dev, info); | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 238 | return err; | 
|  | 239 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 |  | 
|  | 241 | /* | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 242 | * We got an error, free all resources. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | */ | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 244 | cleanup: | 
|  | 245 | if (info->mtd) { | 
|  | 246 | del_mtd_partitions(info->mtd); | 
|  | 247 | #ifdef CONFIG_MTD_CONCAT | 
|  | 248 | if (info->mtd != info->subdev[0].mtd) | 
|  | 249 | mtd_concat_destroy(info->mtd); | 
|  | 250 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 252 | kfree(info->parts); | 
|  | 253 | subdev_err: | 
|  | 254 | for (i = info->nr_subdev - 1; i >= 0; i--) | 
|  | 255 | armflash_subdev_remove(&info->subdev[i]); | 
|  | 256 | no_resource: | 
|  | 257 | if (plat && plat->exit) | 
|  | 258 | plat->exit(); | 
|  | 259 | kfree(info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | out: | 
|  | 261 | return err; | 
|  | 262 | } | 
|  | 263 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 264 | static int armflash_remove(struct platform_device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 266 | struct armflash_info *info = platform_get_drvdata(dev); | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 267 | struct flash_platform_data *plat = dev->dev.platform_data; | 
|  | 268 | int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 270 | platform_set_drvdata(dev, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 |  | 
|  | 272 | if (info) { | 
|  | 273 | if (info->mtd) { | 
|  | 274 | del_mtd_partitions(info->mtd); | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 275 | #ifdef CONFIG_MTD_CONCAT | 
|  | 276 | if (info->mtd != info->subdev[0].mtd) | 
|  | 277 | mtd_concat_destroy(info->mtd); | 
|  | 278 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } | 
| Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 280 | kfree(info->parts); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 |  | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 282 | for (i = info->nr_subdev - 1; i >= 0; i--) | 
|  | 283 | armflash_subdev_remove(&info->subdev[i]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 |  | 
| Catalin Marinas | 9fd1e8f | 2009-04-23 17:41:10 +0100 | [diff] [blame] | 285 | if (plat && plat->exit) | 
|  | 286 | plat->exit(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 |  | 
|  | 288 | kfree(info); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | return 0; | 
|  | 292 | } | 
|  | 293 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 294 | static struct platform_driver armflash_driver = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | .probe		= armflash_probe, | 
|  | 296 | .remove		= armflash_remove, | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 297 | .driver		= { | 
|  | 298 | .name	= "armflash", | 
| Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 299 | .owner	= THIS_MODULE, | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 300 | }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | }; | 
|  | 302 |  | 
|  | 303 | static int __init armflash_init(void) | 
|  | 304 | { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 305 | return platform_driver_register(&armflash_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } | 
|  | 307 |  | 
|  | 308 | static void __exit armflash_exit(void) | 
|  | 309 | { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 310 | platform_driver_unregister(&armflash_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } | 
|  | 312 |  | 
|  | 313 | module_init(armflash_init); | 
|  | 314 | module_exit(armflash_exit); | 
|  | 315 |  | 
|  | 316 | MODULE_AUTHOR("ARM Ltd"); | 
|  | 317 | MODULE_DESCRIPTION("ARM Integrator CFI map driver"); | 
|  | 318 | MODULE_LICENSE("GPL"); | 
| Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 319 | MODULE_ALIAS("platform:armflash"); |