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; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 47 | struct resource *area; |
| 48 | struct platdata_mtd_ram *pdata; |
| 49 | }; |
| 50 | |
| 51 | /* to_platram_info() |
| 52 | * |
| 53 | * device private data to struct platram_info conversion |
| 54 | */ |
| 55 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 56 | static inline struct platram_info *to_platram_info(struct platform_device *dev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 57 | { |
Jingoo Han | 14ac078 | 2013-09-09 14:42:52 +0900 | [diff] [blame] | 58 | return platform_get_drvdata(dev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* platram_setrw |
| 62 | * |
| 63 | * call the platform device's set rw/ro control |
| 64 | * |
| 65 | * to = 0 => read-only |
| 66 | * = 1 => read-write |
| 67 | */ |
| 68 | |
| 69 | static inline void platram_setrw(struct platram_info *info, int to) |
| 70 | { |
| 71 | if (info->pdata == NULL) |
| 72 | return; |
| 73 | |
| 74 | if (info->pdata->set_rw != NULL) |
| 75 | (info->pdata->set_rw)(info->dev, to); |
| 76 | } |
| 77 | |
| 78 | /* platram_remove |
| 79 | * |
| 80 | * called to remove the device from the driver's control |
| 81 | */ |
| 82 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 83 | static int platram_remove(struct platform_device *pdev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 84 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 85 | struct platram_info *info = to_platram_info(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 | dev_dbg(&pdev->dev, "removing device\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 88 | |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 89 | if (info == NULL) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 90 | return 0; |
| 91 | |
| 92 | if (info->mtd) { |
Jamie Iles | 095dd50 | 2011-05-23 10:23:07 +0100 | [diff] [blame] | 93 | mtd_device_unregister(info->mtd); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 94 | map_destroy(info->mtd); |
| 95 | } |
| 96 | |
| 97 | /* ensure ram is left read-only */ |
| 98 | |
| 99 | platram_setrw(info, PLATRAM_RO); |
| 100 | |
| 101 | /* release resources */ |
| 102 | |
| 103 | if (info->area) { |
| 104 | release_resource(info->area); |
| 105 | kfree(info->area); |
| 106 | } |
| 107 | |
| 108 | if (info->map.virt != NULL) |
| 109 | iounmap(info->map.virt); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 110 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 111 | kfree(info); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | /* platram_probe |
| 117 | * |
| 118 | * called from device drive system when a device matching our |
| 119 | * driver is found. |
| 120 | */ |
| 121 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 122 | static int platram_probe(struct platform_device *pdev) |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 123 | { |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 124 | struct platdata_mtd_ram *pdata; |
| 125 | struct platram_info *info; |
| 126 | struct resource *res; |
| 127 | int err = 0; |
| 128 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 129 | dev_dbg(&pdev->dev, "probe entered\n"); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 130 | |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 131 | if (dev_get_platdata(&pdev->dev) == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 132 | dev_err(&pdev->dev, "no platform data supplied\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 133 | err = -ENOENT; |
| 134 | goto exit_error; |
| 135 | } |
| 136 | |
Jingoo Han | d20d5a5 | 2013-07-30 17:18:06 +0900 | [diff] [blame] | 137 | pdata = dev_get_platdata(&pdev->dev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 138 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 139 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 140 | if (info == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 141 | dev_err(&pdev->dev, "no memory for flash info\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 142 | err = -ENOMEM; |
| 143 | goto exit_error; |
| 144 | } |
| 145 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 146 | platform_set_drvdata(pdev, info); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 147 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 148 | info->dev = &pdev->dev; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 149 | info->pdata = pdata; |
| 150 | |
| 151 | /* get the resource for the memory mapping */ |
| 152 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 153 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 154 | |
| 155 | if (res == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 156 | dev_err(&pdev->dev, "no memory resource specified\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 157 | err = -ENOENT; |
| 158 | goto exit_free; |
| 159 | } |
| 160 | |
Randy Dunlap | 06d63cc | 2007-04-25 22:41:34 -0700 | [diff] [blame] | 161 | dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res, |
| 162 | (unsigned long long)res->start); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 163 | |
| 164 | /* setup map parameters */ |
| 165 | |
| 166 | info->map.phys = res->start; |
Wolfram Sang | 76d6a47 | 2009-07-17 17:47:37 +0200 | [diff] [blame] | 167 | info->map.size = resource_size(res); |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 168 | info->map.name = pdata->mapname != NULL ? |
| 169 | (char *)pdata->mapname : (char *)pdev->name; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 170 | info->map.bankwidth = pdata->bankwidth; |
| 171 | |
| 172 | /* register our usage of the memory area */ |
| 173 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 174 | info->area = request_mem_region(res->start, info->map.size, pdev->name); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 175 | if (info->area == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 176 | dev_err(&pdev->dev, "failed to request memory region\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 177 | err = -EIO; |
| 178 | goto exit_free; |
| 179 | } |
| 180 | |
| 181 | /* remap the memory area */ |
| 182 | |
| 183 | info->map.virt = ioremap(res->start, info->map.size); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 184 | 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] | 185 | |
| 186 | if (info->map.virt == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 187 | dev_err(&pdev->dev, "failed to ioremap() region\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 188 | err = -EIO; |
| 189 | goto exit_free; |
| 190 | } |
| 191 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 192 | simple_map_init(&info->map); |
| 193 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 194 | dev_dbg(&pdev->dev, "initialised map, probing for mtd\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 195 | |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 196 | /* probe for the right mtd map driver |
| 197 | * supplied by the platform_data struct */ |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 198 | |
Harvey Harrison | a01e035 | 2008-04-28 16:50:04 -0700 | [diff] [blame] | 199 | if (pdata->map_probes) { |
Artem Bityutskiy | d50dcb1 | 2013-03-12 10:28:31 +0200 | [diff] [blame] | 200 | const char * const *map_probes = pdata->map_probes; |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 201 | |
| 202 | for ( ; !info->mtd && *map_probes; map_probes++) |
| 203 | info->mtd = do_map_probe(*map_probes , &info->map); |
| 204 | } |
| 205 | /* fallback to map_ram */ |
| 206 | else |
| 207 | info->mtd = do_map_probe("map_ram", &info->map); |
| 208 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 209 | if (info->mtd == NULL) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 210 | dev_err(&pdev->dev, "failed to probe for map_ram\n"); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 211 | err = -ENOMEM; |
| 212 | goto exit_free; |
| 213 | } |
| 214 | |
| 215 | info->mtd->owner = THIS_MODULE; |
David Brownell | 87f39f0 | 2009-03-26 00:42:50 -0700 | [diff] [blame] | 216 | info->mtd->dev.parent = &pdev->dev; |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 217 | |
| 218 | platram_setrw(info, PLATRAM_RW); |
| 219 | |
Adam Buchbinder | 48fc7f7 | 2012-09-19 21:48:00 -0400 | [diff] [blame] | 220 | /* check to see if there are any available partitions, or whether |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 221 | * to add this device whole */ |
| 222 | |
Artem Bityutskiy | 42d7fbe | 2012-03-09 19:24:26 +0200 | [diff] [blame] | 223 | err = mtd_device_parse_register(info->mtd, pdata->probes, NULL, |
| 224 | pdata->partitions, |
| 225 | pdata->nr_partitions); |
Florian Fainelli | 75757006 | 2008-03-03 18:30:24 +0100 | [diff] [blame] | 226 | if (!err) |
| 227 | dev_info(&pdev->dev, "registered mtd device\n"); |
| 228 | |
Ilya Yanok | c329879 | 2011-12-13 00:37:56 +0100 | [diff] [blame] | 229 | if (pdata->nr_partitions) { |
| 230 | /* add the whole device. */ |
| 231 | err = mtd_device_register(info->mtd, NULL, 0); |
| 232 | if (err) { |
| 233 | dev_err(&pdev->dev, |
| 234 | "failed to register the entire device\n"); |
| 235 | } |
| 236 | } |
Jamie Iles | 095dd50 | 2011-05-23 10:23:07 +0100 | [diff] [blame] | 237 | |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 238 | return err; |
| 239 | |
| 240 | exit_free: |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 241 | platram_remove(pdev); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 242 | exit_error: |
| 243 | return err; |
| 244 | } |
| 245 | |
| 246 | /* device driver info */ |
| 247 | |
Kay Sievers | 41d867c | 2008-04-18 13:44:26 -0700 | [diff] [blame] | 248 | /* work with hotplug and coldplug */ |
| 249 | MODULE_ALIAS("platform:mtd-ram"); |
| 250 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 251 | static struct platform_driver platram_driver = { |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 252 | .probe = platram_probe, |
| 253 | .remove = platram_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 254 | .driver = { |
| 255 | .name = "mtd-ram", |
| 256 | .owner = THIS_MODULE, |
| 257 | }, |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | /* module init/exit */ |
| 261 | |
| 262 | static int __init platram_init(void) |
| 263 | { |
| 264 | printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n"); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 265 | return platform_driver_register(&platram_driver); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | static void __exit platram_exit(void) |
| 269 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 270 | platform_driver_unregister(&platram_driver); |
Ben Dooks | 99f2a8aea | 2005-01-24 00:37:04 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | module_init(platram_init); |
| 274 | module_exit(platram_exit); |
| 275 | |
| 276 | MODULE_LICENSE("GPL"); |
| 277 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); |
| 278 | MODULE_DESCRIPTION("MTD platform RAM map driver"); |