Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 1 | /* drivers/mtd/maps/plat-ram.c |
| 2 | * |
| 3 | * (c) 2004-2005 Simtec Electronics |
| 4 | * http://www.simtec.co.uk/products/SWLINUX/ |
| 5 | * Ben Dooks <ben@simtec.co.uk> |
| 6 | * |
Stefan Weil | 947af29 | 2010-01-07 00:03:52 +0100 | [diff] [blame] | 7 | * Generic platform device based RAM map |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 8 | * |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | */ |
| 23 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 24 | #include <linux/module.h> |
| 25 | #include <linux/types.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/string.h> |
| 29 | #include <linux/ioport.h> |
| 30 | #include <linux/device.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 31 | #include <linux/slab.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 32 | #include <linux/platform_device.h> |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 33 | |
| 34 | #include <linux/mtd/mtd.h> |
| 35 | #include <linux/mtd/map.h> |
| 36 | #include <linux/mtd/partitions.h> |
| 37 | #include <linux/mtd/plat-ram.h> |
| 38 | |
| 39 | #include <asm/io.h> |
| 40 | |
| 41 | /* private structure for each mtd platform ram device created */ |
| 42 | |
| 43 | struct platram_info { |
| 44 | struct device *dev; |
| 45 | struct mtd_info *mtd; |
| 46 | struct map_info map; |
| 47 | struct mtd_partition *partitions; |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 48 | bool free_partitions; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 49 | struct resource *area; |
| 50 | struct platdata_mtd_ram *pdata; |
| 51 | }; |
| 52 | |
| 53 | /* to_platram_info() |
| 54 | * |
| 55 | * device private data to struct platram_info conversion |
| 56 | */ |
| 57 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 58 | static inline struct platram_info *to_platram_info(struct platform_device *dev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 59 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 60 | return (struct platram_info *)platform_get_drvdata(dev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* platram_setrw |
| 64 | * |
| 65 | * call the platform device's set rw/ro control |
| 66 | * |
| 67 | * to = 0 => read-only |
| 68 | * = 1 => read-write |
| 69 | */ |
| 70 | |
| 71 | static inline void platram_setrw(struct platram_info *info, int to) |
| 72 | { |
| 73 | if (info->pdata == NULL) |
| 74 | return; |
| 75 | |
| 76 | if (info->pdata->set_rw != NULL) |
| 77 | (info->pdata->set_rw)(info->dev, to); |
| 78 | } |
| 79 | |
| 80 | /* platram_remove |
| 81 | * |
| 82 | * called to remove the device from the driver's control |
| 83 | */ |
| 84 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 85 | static int platram_remove(struct platform_device *pdev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 86 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 87 | struct platram_info *info = to_platram_info(pdev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 88 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 89 | platform_set_drvdata(pdev, NULL); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 90 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 91 | dev_dbg(&pdev->dev, "removing device\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 92 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 93 | if (info == NULL) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 94 | return 0; |
| 95 | |
| 96 | if (info->mtd) { |
| 97 | #ifdef CONFIG_MTD_PARTITIONS |
| 98 | if (info->partitions) { |
| 99 | del_mtd_partitions(info->mtd); |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 100 | if (info->free_partitions) |
| 101 | kfree(info->partitions); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 102 | } |
| 103 | #endif |
| 104 | del_mtd_device(info->mtd); |
| 105 | map_destroy(info->mtd); |
| 106 | } |
| 107 | |
| 108 | /* ensure ram is left read-only */ |
| 109 | |
| 110 | platram_setrw(info, PLATRAM_RO); |
| 111 | |
| 112 | /* release resources */ |
| 113 | |
| 114 | if (info->area) { |
| 115 | release_resource(info->area); |
| 116 | kfree(info->area); |
| 117 | } |
| 118 | |
| 119 | if (info->map.virt != NULL) |
| 120 | iounmap(info->map.virt); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 121 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 122 | kfree(info); |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /* platram_probe |
| 128 | * |
| 129 | * called from device drive system when a device matching our |
| 130 | * driver is found. |
| 131 | */ |
| 132 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 133 | static int platram_probe(struct platform_device *pdev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 134 | { |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 135 | struct platdata_mtd_ram *pdata; |
| 136 | struct platram_info *info; |
| 137 | struct resource *res; |
| 138 | int err = 0; |
| 139 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 140 | dev_dbg(&pdev->dev, "probe entered\n"); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 141 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 142 | if (pdev->dev.platform_data == NULL) { |
| 143 | dev_err(&pdev->dev, "no platform data supplied\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 144 | err = -ENOENT; |
| 145 | goto exit_error; |
| 146 | } |
| 147 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 148 | pdata = pdev->dev.platform_data; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 149 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 150 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 151 | if (info == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 152 | dev_err(&pdev->dev, "no memory for flash info\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 153 | err = -ENOMEM; |
| 154 | goto exit_error; |
| 155 | } |
| 156 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 157 | platform_set_drvdata(pdev, info); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 158 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 159 | info->dev = &pdev->dev; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 160 | info->pdata = pdata; |
| 161 | |
| 162 | /* get the resource for the memory mapping */ |
| 163 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 164 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 165 | |
| 166 | if (res == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 167 | dev_err(&pdev->dev, "no memory resource specified\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 168 | err = -ENOENT; |
| 169 | goto exit_free; |
| 170 | } |
| 171 | |
Randy Dunlap | 06d63cc | 2007-04-25 22:41:34 -0700 | [diff] [blame] | 172 | dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res, |
| 173 | (unsigned long long)res->start); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 174 | |
| 175 | /* setup map parameters */ |
| 176 | |
| 177 | info->map.phys = res->start; |
Wolfram Sang | 76d6a47 | 2009-07-17 17:47:37 +0200 | [diff] [blame] | 178 | info->map.size = resource_size(res); |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 179 | info->map.name = pdata->mapname != NULL ? |
| 180 | (char *)pdata->mapname : (char *)pdev->name; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 181 | info->map.bankwidth = pdata->bankwidth; |
| 182 | |
| 183 | /* register our usage of the memory area */ |
| 184 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 185 | info->area = request_mem_region(res->start, info->map.size, pdev->name); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 186 | if (info->area == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 187 | dev_err(&pdev->dev, "failed to request memory region\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 188 | err = -EIO; |
| 189 | goto exit_free; |
| 190 | } |
| 191 | |
| 192 | /* remap the memory area */ |
| 193 | |
| 194 | info->map.virt = ioremap(res->start, info->map.size); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 195 | dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 196 | |
| 197 | if (info->map.virt == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 198 | dev_err(&pdev->dev, "failed to ioremap() region\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 199 | err = -EIO; |
| 200 | goto exit_free; |
| 201 | } |
| 202 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 203 | simple_map_init(&info->map); |
| 204 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 205 | dev_dbg(&pdev->dev, "initialised map, probing for mtd\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 206 | |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 207 | /* probe for the right mtd map driver |
| 208 | * supplied by the platform_data struct */ |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 209 | |
Harvey Harrison | a01e035 | 2008-04-28 16:50:04 -0700 | [diff] [blame] | 210 | if (pdata->map_probes) { |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 211 | const char **map_probes = pdata->map_probes; |
| 212 | |
| 213 | for ( ; !info->mtd && *map_probes; map_probes++) |
| 214 | info->mtd = do_map_probe(*map_probes , &info->map); |
| 215 | } |
| 216 | /* fallback to map_ram */ |
| 217 | else |
| 218 | info->mtd = do_map_probe("map_ram", &info->map); |
| 219 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 220 | if (info->mtd == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 221 | dev_err(&pdev->dev, "failed to probe for map_ram\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 222 | err = -ENOMEM; |
| 223 | goto exit_free; |
| 224 | } |
| 225 | |
| 226 | info->mtd->owner = THIS_MODULE; |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 227 | info->mtd->dev.parent = &pdev->dev; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 228 | |
| 229 | platram_setrw(info, PLATRAM_RW); |
| 230 | |
| 231 | /* check to see if there are any available partitions, or wether |
| 232 | * to add this device whole */ |
| 233 | |
| 234 | #ifdef CONFIG_MTD_PARTITIONS |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 235 | if (!pdata->nr_partitions) { |
| 236 | /* try to probe using the supplied probe type */ |
| 237 | if (pdata->probes) { |
| 238 | err = parse_mtd_partitions(info->mtd, pdata->probes, |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 239 | &info->partitions, 0); |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 240 | info->free_partitions = 1; |
| 241 | if (err > 0) |
| 242 | err = add_mtd_partitions(info->mtd, |
| 243 | info->partitions, err); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 246 | /* use the static mapping */ |
| 247 | else |
| 248 | err = add_mtd_partitions(info->mtd, pdata->partitions, |
| 249 | pdata->nr_partitions); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 250 | #endif /* CONFIG_MTD_PARTITIONS */ |
| 251 | |
| 252 | if (add_mtd_device(info->mtd)) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 253 | dev_err(&pdev->dev, "add_mtd_device() failed\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 254 | err = -ENOMEM; |
| 255 | } |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 256 | |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 257 | if (!err) |
| 258 | dev_info(&pdev->dev, "registered mtd device\n"); |
| 259 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 260 | return err; |
| 261 | |
| 262 | exit_free: |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 263 | platram_remove(pdev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 264 | exit_error: |
| 265 | return err; |
| 266 | } |
| 267 | |
| 268 | /* device driver info */ |
| 269 | |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 270 | /* work with hotplug and coldplug */ |
| 271 | MODULE_ALIAS("platform:mtd-ram"); |
| 272 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 273 | static struct platform_driver platram_driver = { |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 274 | .probe = platram_probe, |
| 275 | .remove = platram_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 276 | .driver = { |
| 277 | .name = "mtd-ram", |
| 278 | .owner = THIS_MODULE, |
| 279 | }, |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 280 | }; |
| 281 | |
| 282 | /* module init/exit */ |
| 283 | |
| 284 | static int __init platram_init(void) |
| 285 | { |
| 286 | printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n"); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 287 | return platform_driver_register(&platram_driver); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void __exit platram_exit(void) |
| 291 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 292 | platform_driver_unregister(&platram_driver); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | module_init(platram_init); |
| 296 | module_exit(platram_exit); |
| 297 | |
| 298 | MODULE_LICENSE("GPL"); |
| 299 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 300 | MODULE_DESCRIPTION("MTD platform RAM map driver"); |