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/kernel.h> 27 #include <linux/string.h> 28 #include <linux/ioport.h> 29 #include <linux/device.h> 30 #include <linux/slab.h> 31 #include <linux/platform_device.h> 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; 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 55 static inline struct platram_info *to_platram_info(struct platform_device *dev) 56 { 57 return platform_get_drvdata(dev); 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 82 static int platram_remove(struct platform_device *pdev) 83 { 84 struct platram_info *info = to_platram_info(pdev); 85 86 dev_dbg(&pdev->dev, "removing device\n"); 87 88 if (info == NULL) 89 return 0; 90 91 if (info->mtd) { 92 mtd_device_unregister(info->mtd); 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); 109 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 121 static int platram_probe(struct platform_device *pdev) 122 { 123 struct platdata_mtd_ram *pdata; 124 struct platram_info *info; 125 struct resource *res; 126 int err = 0; 127 128 dev_dbg(&pdev->dev, "probe entered\n"); 129 130 if (dev_get_platdata(&pdev->dev) == NULL) { 131 dev_err(&pdev->dev, "no platform data supplied\n"); 132 err = -ENOENT; 133 goto exit_error; 134 } 135 136 pdata = dev_get_platdata(&pdev->dev); 137 138 info = kzalloc(sizeof(*info), GFP_KERNEL); 139 if (info == NULL) { 140 dev_err(&pdev->dev, "no memory for flash info\n"); 141 err = -ENOMEM; 142 goto exit_error; 143 } 144 145 platform_set_drvdata(pdev, info); 146 147 info->dev = &pdev->dev; 148 info->pdata = pdata; 149 150 /* get the resource for the memory mapping */ 151 152 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 153 154 if (res == NULL) { 155 dev_err(&pdev->dev, "no memory resource specified\n"); 156 err = -ENOENT; 157 goto exit_free; 158 } 159 160 dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res, 161 (unsigned long long)res->start); 162 163 /* setup map parameters */ 164 165 info->map.phys = res->start; 166 info->map.size = resource_size(res); 167 info->map.name = pdata->mapname != NULL ? 168 (char *)pdata->mapname : (char *)pdev->name; 169 info->map.bankwidth = pdata->bankwidth; 170 171 /* register our usage of the memory area */ 172 173 info->area = request_mem_region(res->start, info->map.size, pdev->name); 174 if (info->area == NULL) { 175 dev_err(&pdev->dev, "failed to request memory region\n"); 176 err = -EIO; 177 goto exit_free; 178 } 179 180 /* remap the memory area */ 181 182 info->map.virt = ioremap(res->start, info->map.size); 183 dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size); 184 185 if (info->map.virt == NULL) { 186 dev_err(&pdev->dev, "failed to ioremap() region\n"); 187 err = -EIO; 188 goto exit_free; 189 } 190 191 simple_map_init(&info->map); 192 193 dev_dbg(&pdev->dev, "initialised map, probing for mtd\n"); 194 195 /* probe for the right mtd map driver 196 * supplied by the platform_data struct */ 197 198 if (pdata->map_probes) { 199 const char * const *map_probes = pdata->map_probes; 200 201 for ( ; !info->mtd && *map_probes; map_probes++) 202 info->mtd = do_map_probe(*map_probes , &info->map); 203 } 204 /* fallback to map_ram */ 205 else 206 info->mtd = do_map_probe("map_ram", &info->map); 207 208 if (info->mtd == NULL) { 209 dev_err(&pdev->dev, "failed to probe for map_ram\n"); 210 err = -ENOMEM; 211 goto exit_free; 212 } 213 214 info->mtd->owner = THIS_MODULE; 215 info->mtd->dev.parent = &pdev->dev; 216 217 platram_setrw(info, PLATRAM_RW); 218 219 /* check to see if there are any available partitions, or whether 220 * to add this device whole */ 221 222 err = mtd_device_parse_register(info->mtd, pdata->probes, NULL, 223 pdata->partitions, 224 pdata->nr_partitions); 225 if (!err) 226 dev_info(&pdev->dev, "registered mtd device\n"); 227 228 if (pdata->nr_partitions) { 229 /* add the whole device. */ 230 err = mtd_device_register(info->mtd, NULL, 0); 231 if (err) { 232 dev_err(&pdev->dev, 233 "failed to register the entire device\n"); 234 } 235 } 236 237 return err; 238 239 exit_free: 240 platram_remove(pdev); 241 exit_error: 242 return err; 243 } 244 245 /* device driver info */ 246 247 /* work with hotplug and coldplug */ 248 MODULE_ALIAS("platform:mtd-ram"); 249 250 static struct platform_driver platram_driver = { 251 .probe = platram_probe, 252 .remove = platram_remove, 253 .driver = { 254 .name = "mtd-ram", 255 .owner = THIS_MODULE, 256 }, 257 }; 258 259 module_platform_driver(platram_driver); 260 261 MODULE_LICENSE("GPL"); 262 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); 263 MODULE_DESCRIPTION("MTD platform RAM map driver"); 264