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 platfrom device based RAM map 8 * 9 * $Id: plat-ram.c,v 1.3 2005/03/19 22:41:27 gleixner Exp $ 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 * You should have received a copy of the GNU General Public License 22 * along with this program; if not, write to the Free Software 23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 24 */ 25 26 #include <linux/module.h> 27 #include <linux/types.h> 28 #include <linux/init.h> 29 #include <linux/kernel.h> 30 #include <linux/string.h> 31 #include <linux/ioport.h> 32 #include <linux/device.h> 33 #include <linux/slab.h> 34 35 #include <linux/mtd/mtd.h> 36 #include <linux/mtd/map.h> 37 #include <linux/mtd/partitions.h> 38 #include <linux/mtd/plat-ram.h> 39 40 #include <asm/io.h> 41 42 /* private structure for each mtd platform ram device created */ 43 44 struct platram_info { 45 struct device *dev; 46 struct mtd_info *mtd; 47 struct map_info map; 48 struct mtd_partition *partitions; 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 58 static inline struct platram_info *to_platram_info(struct device *dev) 59 { 60 return (struct platram_info *)dev_get_drvdata(dev); 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 85 static int platram_remove(struct device *dev) 86 { 87 struct platram_info *info = to_platram_info(dev); 88 89 dev_set_drvdata(dev, NULL); 90 91 dev_dbg(dev, "removing device\n"); 92 93 if (info == NULL) 94 return 0; 95 96 if (info->mtd) { 97 #ifdef CONFIG_MTD_PARTITIONS 98 if (info->partitions) { 99 del_mtd_partitions(info->mtd); 100 kfree(info->partitions); 101 } 102 #endif 103 del_mtd_device(info->mtd); 104 map_destroy(info->mtd); 105 } 106 107 /* ensure ram is left read-only */ 108 109 platram_setrw(info, PLATRAM_RO); 110 111 /* release resources */ 112 113 if (info->area) { 114 release_resource(info->area); 115 kfree(info->area); 116 } 117 118 if (info->map.virt != NULL) 119 iounmap(info->map.virt); 120 121 kfree(info); 122 123 return 0; 124 } 125 126 /* platram_probe 127 * 128 * called from device drive system when a device matching our 129 * driver is found. 130 */ 131 132 static int platram_probe(struct device *dev) 133 { 134 struct platform_device *pd = to_platform_device(dev); 135 struct platdata_mtd_ram *pdata; 136 struct platram_info *info; 137 struct resource *res; 138 int err = 0; 139 140 dev_dbg(dev, "probe entered\n"); 141 142 if (dev->platform_data == NULL) { 143 dev_err(dev, "no platform data supplied\n"); 144 err = -ENOENT; 145 goto exit_error; 146 } 147 148 pdata = dev->platform_data; 149 150 info = kmalloc(sizeof(*info), GFP_KERNEL); 151 if (info == NULL) { 152 dev_err(dev, "no memory for flash info\n"); 153 err = -ENOMEM; 154 goto exit_error; 155 } 156 157 memset(info, 0, sizeof(*info)); 158 dev_set_drvdata(dev, info); 159 160 info->dev = dev; 161 info->pdata = pdata; 162 163 /* get the resource for the memory mapping */ 164 165 res = platform_get_resource(pd, IORESOURCE_MEM, 0); 166 167 if (res == NULL) { 168 dev_err(dev, "no memory resource specified\n"); 169 err = -ENOENT; 170 goto exit_free; 171 } 172 173 dev_dbg(dev, "got platform resource %p (0x%lx)\n", res, res->start); 174 175 /* setup map parameters */ 176 177 info->map.phys = res->start; 178 info->map.size = (res->end - res->start) + 1; 179 info->map.name = pdata->mapname != NULL ? pdata->mapname : pd->name; 180 info->map.bankwidth = pdata->bankwidth; 181 182 /* register our usage of the memory area */ 183 184 info->area = request_mem_region(res->start, info->map.size, pd->name); 185 if (info->area == NULL) { 186 dev_err(dev, "failed to request memory region\n"); 187 err = -EIO; 188 goto exit_free; 189 } 190 191 /* remap the memory area */ 192 193 info->map.virt = ioremap(res->start, info->map.size); 194 dev_dbg(dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size); 195 196 if (info->map.virt == NULL) { 197 dev_err(dev, "failed to ioremap() region\n"); 198 err = -EIO; 199 goto exit_free; 200 } 201 202 simple_map_init(&info->map); 203 204 dev_dbg(dev, "initialised map, probing for mtd\n"); 205 206 /* probe for the right mtd map driver */ 207 208 info->mtd = do_map_probe("map_ram" , &info->map); 209 if (info->mtd == NULL) { 210 dev_err(dev, "failed to probe for map_ram\n"); 211 err = -ENOMEM; 212 goto exit_free; 213 } 214 215 info->mtd->owner = THIS_MODULE; 216 217 platram_setrw(info, PLATRAM_RW); 218 219 /* check to see if there are any available partitions, or wether 220 * to add this device whole */ 221 222 #ifdef CONFIG_MTD_PARTITIONS 223 if (pdata->nr_partitions > 0) { 224 const char **probes = { NULL }; 225 226 if (pdata->probes) 227 probes = (const char **)pdata->probes; 228 229 err = parse_mtd_partitions(info->mtd, probes, 230 &info->partitions, 0); 231 if (err > 0) { 232 err = add_mtd_partitions(info->mtd, info->partitions, 233 err); 234 } 235 } 236 #endif /* CONFIG_MTD_PARTITIONS */ 237 238 if (add_mtd_device(info->mtd)) { 239 dev_err(dev, "add_mtd_device() failed\n"); 240 err = -ENOMEM; 241 } 242 243 dev_info(dev, "registered mtd device\n"); 244 return err; 245 246 exit_free: 247 platram_remove(dev); 248 exit_error: 249 return err; 250 } 251 252 /* device driver info */ 253 254 static struct device_driver platram_driver = { 255 .name = "mtd-ram", 256 .bus = &platform_bus_type, 257 .probe = platram_probe, 258 .remove = platram_remove, 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 driver_register(&platram_driver); 267 } 268 269 static void __exit platram_exit(void) 270 { 271 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