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