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 * 7 * Generic platform device based RAM map 8 * 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 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> 31 #include <linux/slab.h> 32 #include <linux/platform_device.h> 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 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 56 static inline struct platram_info *to_platram_info(struct platform_device *dev) 57 { 58 return (struct platram_info *)platform_get_drvdata(dev); 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 83 static int platram_remove(struct platform_device *pdev) 84 { 85 struct platram_info *info = to_platram_info(pdev); 86 87 platform_set_drvdata(pdev, NULL); 88 89 dev_dbg(&pdev->dev, "removing device\n"); 90 91 if (info == NULL) 92 return 0; 93 94 if (info->mtd) { 95 mtd_device_unregister(info->mtd); 96 map_destroy(info->mtd); 97 } 98 99 /* ensure ram is left read-only */ 100 101 platram_setrw(info, PLATRAM_RO); 102 103 /* release resources */ 104 105 if (info->area) { 106 release_resource(info->area); 107 kfree(info->area); 108 } 109 110 if (info->map.virt != NULL) 111 iounmap(info->map.virt); 112 113 kfree(info); 114 115 return 0; 116 } 117 118 /* platram_probe 119 * 120 * called from device drive system when a device matching our 121 * driver is found. 122 */ 123 124 static int platram_probe(struct platform_device *pdev) 125 { 126 struct platdata_mtd_ram *pdata; 127 struct platram_info *info; 128 struct resource *res; 129 int err = 0; 130 131 dev_dbg(&pdev->dev, "probe entered\n"); 132 133 if (pdev->dev.platform_data == NULL) { 134 dev_err(&pdev->dev, "no platform data supplied\n"); 135 err = -ENOENT; 136 goto exit_error; 137 } 138 139 pdata = pdev->dev.platform_data; 140 141 info = kzalloc(sizeof(*info), GFP_KERNEL); 142 if (info == NULL) { 143 dev_err(&pdev->dev, "no memory for flash info\n"); 144 err = -ENOMEM; 145 goto exit_error; 146 } 147 148 platform_set_drvdata(pdev, info); 149 150 info->dev = &pdev->dev; 151 info->pdata = pdata; 152 153 /* get the resource for the memory mapping */ 154 155 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 156 157 if (res == NULL) { 158 dev_err(&pdev->dev, "no memory resource specified\n"); 159 err = -ENOENT; 160 goto exit_free; 161 } 162 163 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res, 164 (unsigned long long)res->start); 165 166 /* setup map parameters */ 167 168 info->map.phys = res->start; 169 info->map.size = resource_size(res); 170 info->map.name = pdata->mapname != NULL ? 171 (char *)pdata->mapname : (char *)pdev->name; 172 info->map.bankwidth = pdata->bankwidth; 173 174 /* register our usage of the memory area */ 175 176 info->area = request_mem_region(res->start, info->map.size, pdev->name); 177 if (info->area == NULL) { 178 dev_err(&pdev->dev, "failed to request memory region\n"); 179 err = -EIO; 180 goto exit_free; 181 } 182 183 /* remap the memory area */ 184 185 info->map.virt = ioremap(res->start, info->map.size); 186 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size); 187 188 if (info->map.virt == NULL) { 189 dev_err(&pdev->dev, "failed to ioremap() region\n"); 190 err = -EIO; 191 goto exit_free; 192 } 193 194 simple_map_init(&info->map); 195 196 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n"); 197 198 /* probe for the right mtd map driver 199 * supplied by the platform_data struct */ 200 201 if (pdata->map_probes) { 202 const char **map_probes = pdata->map_probes; 203 204 for ( ; !info->mtd && *map_probes; map_probes++) 205 info->mtd = do_map_probe(*map_probes , &info->map); 206 } 207 /* fallback to map_ram */ 208 else 209 info->mtd = do_map_probe("map_ram", &info->map); 210 211 if (info->mtd == NULL) { 212 dev_err(&pdev->dev, "failed to probe for map_ram\n"); 213 err = -ENOMEM; 214 goto exit_free; 215 } 216 217 info->mtd->owner = THIS_MODULE; 218 info->mtd->dev.parent = &pdev->dev; 219 220 platram_setrw(info, PLATRAM_RW); 221 222 /* check to see if there are any available partitions, or wether 223 * to add this device whole */ 224 225 err = mtd_device_parse_register(info->mtd, pdata->probes, 0, 226 pdata->partitions, pdata->nr_partitions); 227 if (!err) 228 dev_info(&pdev->dev, "registered mtd device\n"); 229 230 if (pdata->nr_partitions) { 231 /* add the whole device. */ 232 err = mtd_device_register(info->mtd, NULL, 0); 233 if (err) { 234 dev_err(&pdev->dev, 235 "failed to register the entire device\n"); 236 } 237 } 238 239 return err; 240 241 exit_free: 242 platram_remove(pdev); 243 exit_error: 244 return err; 245 } 246 247 /* device driver info */ 248 249 /* work with hotplug and coldplug */ 250 MODULE_ALIAS("platform:mtd-ram"); 251 252 static struct platform_driver platram_driver = { 253 .probe = platram_probe, 254 .remove = platram_remove, 255 .driver = { 256 .name = "mtd-ram", 257 .owner = THIS_MODULE, 258 }, 259 }; 260 261 /* module init/exit */ 262 263 static int __init platram_init(void) 264 { 265 printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n"); 266 return platform_driver_register(&platram_driver); 267 } 268 269 static void __exit platram_exit(void) 270 { 271 platform_driver_unregister(&platram_driver); 272 } 273 274 module_init(platram_init); 275 module_exit(platram_exit); 276 277 MODULE_LICENSE("GPL"); 278 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); 279 MODULE_DESCRIPTION("MTD platform RAM map driver"); 280