199f2a8aeSBen Dooks /* drivers/mtd/maps/plat-ram.c 299f2a8aeSBen Dooks * 399f2a8aeSBen Dooks * (c) 2004-2005 Simtec Electronics 499f2a8aeSBen Dooks * http://www.simtec.co.uk/products/SWLINUX/ 599f2a8aeSBen Dooks * Ben Dooks <ben@simtec.co.uk> 699f2a8aeSBen Dooks * 799f2a8aeSBen Dooks * Generic platfrom device based RAM map 899f2a8aeSBen Dooks * 969f34c98SThomas Gleixner * $Id: plat-ram.c,v 1.7 2005/11/07 11:14:28 gleixner Exp $ 1099f2a8aeSBen Dooks * 1199f2a8aeSBen Dooks * This program is free software; you can redistribute it and/or modify 1299f2a8aeSBen Dooks * it under the terms of the GNU General Public License as published by 1399f2a8aeSBen Dooks * the Free Software Foundation; either version 2 of the License, or 1499f2a8aeSBen Dooks * (at your option) any later version. 1599f2a8aeSBen Dooks * 1699f2a8aeSBen Dooks * This program is distributed in the hope that it will be useful, 1799f2a8aeSBen Dooks * but WITHOUT ANY WARRANTY; without even the implied warranty of 1899f2a8aeSBen Dooks * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1999f2a8aeSBen Dooks * GNU General Public License for more details. 2099f2a8aeSBen Dooks * 2199f2a8aeSBen Dooks * You should have received a copy of the GNU General Public License 2299f2a8aeSBen Dooks * along with this program; if not, write to the Free Software 2399f2a8aeSBen Dooks * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 2499f2a8aeSBen Dooks */ 2599f2a8aeSBen Dooks 2699f2a8aeSBen Dooks #include <linux/module.h> 2799f2a8aeSBen Dooks #include <linux/types.h> 2899f2a8aeSBen Dooks #include <linux/init.h> 2999f2a8aeSBen Dooks #include <linux/kernel.h> 3099f2a8aeSBen Dooks #include <linux/string.h> 3199f2a8aeSBen Dooks #include <linux/ioport.h> 3299f2a8aeSBen Dooks #include <linux/device.h> 334e57b681STim Schmielau #include <linux/slab.h> 34d052d1beSRussell King #include <linux/platform_device.h> 3599f2a8aeSBen Dooks 3699f2a8aeSBen Dooks #include <linux/mtd/mtd.h> 3799f2a8aeSBen Dooks #include <linux/mtd/map.h> 3899f2a8aeSBen Dooks #include <linux/mtd/partitions.h> 3999f2a8aeSBen Dooks #include <linux/mtd/plat-ram.h> 4099f2a8aeSBen Dooks 4199f2a8aeSBen Dooks #include <asm/io.h> 4299f2a8aeSBen Dooks 4399f2a8aeSBen Dooks /* private structure for each mtd platform ram device created */ 4499f2a8aeSBen Dooks 4599f2a8aeSBen Dooks struct platram_info { 4699f2a8aeSBen Dooks struct device *dev; 4799f2a8aeSBen Dooks struct mtd_info *mtd; 4899f2a8aeSBen Dooks struct map_info map; 4999f2a8aeSBen Dooks struct mtd_partition *partitions; 5099f2a8aeSBen Dooks struct resource *area; 5199f2a8aeSBen Dooks struct platdata_mtd_ram *pdata; 5299f2a8aeSBen Dooks }; 5399f2a8aeSBen Dooks 5499f2a8aeSBen Dooks /* to_platram_info() 5599f2a8aeSBen Dooks * 5699f2a8aeSBen Dooks * device private data to struct platram_info conversion 5799f2a8aeSBen Dooks */ 5899f2a8aeSBen Dooks 593ae5eaecSRussell King static inline struct platram_info *to_platram_info(struct platform_device *dev) 6099f2a8aeSBen Dooks { 613ae5eaecSRussell King return (struct platram_info *)platform_get_drvdata(dev); 6299f2a8aeSBen Dooks } 6399f2a8aeSBen Dooks 6499f2a8aeSBen Dooks /* platram_setrw 6599f2a8aeSBen Dooks * 6699f2a8aeSBen Dooks * call the platform device's set rw/ro control 6799f2a8aeSBen Dooks * 6899f2a8aeSBen Dooks * to = 0 => read-only 6999f2a8aeSBen Dooks * = 1 => read-write 7099f2a8aeSBen Dooks */ 7199f2a8aeSBen Dooks 7299f2a8aeSBen Dooks static inline void platram_setrw(struct platram_info *info, int to) 7399f2a8aeSBen Dooks { 7499f2a8aeSBen Dooks if (info->pdata == NULL) 7599f2a8aeSBen Dooks return; 7699f2a8aeSBen Dooks 7799f2a8aeSBen Dooks if (info->pdata->set_rw != NULL) 7899f2a8aeSBen Dooks (info->pdata->set_rw)(info->dev, to); 7999f2a8aeSBen Dooks } 8099f2a8aeSBen Dooks 8199f2a8aeSBen Dooks /* platram_remove 8299f2a8aeSBen Dooks * 8399f2a8aeSBen Dooks * called to remove the device from the driver's control 8499f2a8aeSBen Dooks */ 8599f2a8aeSBen Dooks 863ae5eaecSRussell King static int platram_remove(struct platform_device *pdev) 8799f2a8aeSBen Dooks { 883ae5eaecSRussell King struct platram_info *info = to_platram_info(pdev); 8999f2a8aeSBen Dooks 903ae5eaecSRussell King platform_set_drvdata(pdev, NULL); 9199f2a8aeSBen Dooks 923ae5eaecSRussell King dev_dbg(&pdev->dev, "removing device\n"); 9399f2a8aeSBen Dooks 9499f2a8aeSBen Dooks if (info == NULL) 9599f2a8aeSBen Dooks return 0; 9699f2a8aeSBen Dooks 9799f2a8aeSBen Dooks if (info->mtd) { 9899f2a8aeSBen Dooks #ifdef CONFIG_MTD_PARTITIONS 9999f2a8aeSBen Dooks if (info->partitions) { 10099f2a8aeSBen Dooks del_mtd_partitions(info->mtd); 10199f2a8aeSBen Dooks kfree(info->partitions); 10299f2a8aeSBen Dooks } 10399f2a8aeSBen Dooks #endif 10499f2a8aeSBen Dooks del_mtd_device(info->mtd); 10599f2a8aeSBen Dooks map_destroy(info->mtd); 10699f2a8aeSBen Dooks } 10799f2a8aeSBen Dooks 10899f2a8aeSBen Dooks /* ensure ram is left read-only */ 10999f2a8aeSBen Dooks 11099f2a8aeSBen Dooks platram_setrw(info, PLATRAM_RO); 11199f2a8aeSBen Dooks 11299f2a8aeSBen Dooks /* release resources */ 11399f2a8aeSBen Dooks 11499f2a8aeSBen Dooks if (info->area) { 11599f2a8aeSBen Dooks release_resource(info->area); 11699f2a8aeSBen Dooks kfree(info->area); 11799f2a8aeSBen Dooks } 11899f2a8aeSBen Dooks 11999f2a8aeSBen Dooks if (info->map.virt != NULL) 12099f2a8aeSBen Dooks iounmap(info->map.virt); 12199f2a8aeSBen Dooks 12299f2a8aeSBen Dooks kfree(info); 12399f2a8aeSBen Dooks 12499f2a8aeSBen Dooks return 0; 12599f2a8aeSBen Dooks } 12699f2a8aeSBen Dooks 12799f2a8aeSBen Dooks /* platram_probe 12899f2a8aeSBen Dooks * 12999f2a8aeSBen Dooks * called from device drive system when a device matching our 13099f2a8aeSBen Dooks * driver is found. 13199f2a8aeSBen Dooks */ 13299f2a8aeSBen Dooks 1333ae5eaecSRussell King static int platram_probe(struct platform_device *pdev) 13499f2a8aeSBen Dooks { 13599f2a8aeSBen Dooks struct platdata_mtd_ram *pdata; 13699f2a8aeSBen Dooks struct platram_info *info; 13799f2a8aeSBen Dooks struct resource *res; 13899f2a8aeSBen Dooks int err = 0; 13999f2a8aeSBen Dooks 1403ae5eaecSRussell King dev_dbg(&pdev->dev, "probe entered\n"); 14199f2a8aeSBen Dooks 1423ae5eaecSRussell King if (pdev->dev.platform_data == NULL) { 1433ae5eaecSRussell King dev_err(&pdev->dev, "no platform data supplied\n"); 14499f2a8aeSBen Dooks err = -ENOENT; 14599f2a8aeSBen Dooks goto exit_error; 14699f2a8aeSBen Dooks } 14799f2a8aeSBen Dooks 1483ae5eaecSRussell King pdata = pdev->dev.platform_data; 14999f2a8aeSBen Dooks 15095b93a0cSBurman Yan info = kzalloc(sizeof(*info), GFP_KERNEL); 15199f2a8aeSBen Dooks if (info == NULL) { 1523ae5eaecSRussell King dev_err(&pdev->dev, "no memory for flash info\n"); 15399f2a8aeSBen Dooks err = -ENOMEM; 15499f2a8aeSBen Dooks goto exit_error; 15599f2a8aeSBen Dooks } 15699f2a8aeSBen Dooks 1573ae5eaecSRussell King platform_set_drvdata(pdev, info); 15899f2a8aeSBen Dooks 1593ae5eaecSRussell King info->dev = &pdev->dev; 16099f2a8aeSBen Dooks info->pdata = pdata; 16199f2a8aeSBen Dooks 16299f2a8aeSBen Dooks /* get the resource for the memory mapping */ 16399f2a8aeSBen Dooks 1643ae5eaecSRussell King res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 16599f2a8aeSBen Dooks 16699f2a8aeSBen Dooks if (res == NULL) { 1673ae5eaecSRussell King dev_err(&pdev->dev, "no memory resource specified\n"); 16899f2a8aeSBen Dooks err = -ENOENT; 16999f2a8aeSBen Dooks goto exit_free; 17099f2a8aeSBen Dooks } 17199f2a8aeSBen Dooks 17206d63cc5SRandy Dunlap dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res, 17306d63cc5SRandy Dunlap (unsigned long long)res->start); 17499f2a8aeSBen Dooks 17599f2a8aeSBen Dooks /* setup map parameters */ 17699f2a8aeSBen Dooks 17799f2a8aeSBen Dooks info->map.phys = res->start; 17899f2a8aeSBen Dooks info->map.size = (res->end - res->start) + 1; 1793ae5eaecSRussell King info->map.name = pdata->mapname != NULL ? pdata->mapname : (char *)pdev->name; 18099f2a8aeSBen Dooks info->map.bankwidth = pdata->bankwidth; 18199f2a8aeSBen Dooks 18299f2a8aeSBen Dooks /* register our usage of the memory area */ 18399f2a8aeSBen Dooks 1843ae5eaecSRussell King info->area = request_mem_region(res->start, info->map.size, pdev->name); 18599f2a8aeSBen Dooks if (info->area == NULL) { 1863ae5eaecSRussell King dev_err(&pdev->dev, "failed to request memory region\n"); 18799f2a8aeSBen Dooks err = -EIO; 18899f2a8aeSBen Dooks goto exit_free; 18999f2a8aeSBen Dooks } 19099f2a8aeSBen Dooks 19199f2a8aeSBen Dooks /* remap the memory area */ 19299f2a8aeSBen Dooks 19399f2a8aeSBen Dooks info->map.virt = ioremap(res->start, info->map.size); 1943ae5eaecSRussell King dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size); 19599f2a8aeSBen Dooks 19699f2a8aeSBen Dooks if (info->map.virt == NULL) { 1973ae5eaecSRussell King dev_err(&pdev->dev, "failed to ioremap() region\n"); 19899f2a8aeSBen Dooks err = -EIO; 19999f2a8aeSBen Dooks goto exit_free; 20099f2a8aeSBen Dooks } 20199f2a8aeSBen Dooks 20299f2a8aeSBen Dooks simple_map_init(&info->map); 20399f2a8aeSBen Dooks 2043ae5eaecSRussell King dev_dbg(&pdev->dev, "initialised map, probing for mtd\n"); 20599f2a8aeSBen Dooks 20699f2a8aeSBen Dooks /* probe for the right mtd map driver */ 20799f2a8aeSBen Dooks 20899f2a8aeSBen Dooks info->mtd = do_map_probe("map_ram" , &info->map); 20999f2a8aeSBen Dooks if (info->mtd == NULL) { 2103ae5eaecSRussell King dev_err(&pdev->dev, "failed to probe for map_ram\n"); 21199f2a8aeSBen Dooks err = -ENOMEM; 21299f2a8aeSBen Dooks goto exit_free; 21399f2a8aeSBen Dooks } 21499f2a8aeSBen Dooks 21599f2a8aeSBen Dooks info->mtd->owner = THIS_MODULE; 21699f2a8aeSBen Dooks 21799f2a8aeSBen Dooks platram_setrw(info, PLATRAM_RW); 21899f2a8aeSBen Dooks 21999f2a8aeSBen Dooks /* check to see if there are any available partitions, or wether 22099f2a8aeSBen Dooks * to add this device whole */ 22199f2a8aeSBen Dooks 22299f2a8aeSBen Dooks #ifdef CONFIG_MTD_PARTITIONS 22399f2a8aeSBen Dooks if (pdata->nr_partitions > 0) { 22499f2a8aeSBen Dooks const char **probes = { NULL }; 22599f2a8aeSBen Dooks 22699f2a8aeSBen Dooks if (pdata->probes) 22799f2a8aeSBen Dooks probes = (const char **)pdata->probes; 22899f2a8aeSBen Dooks 22999f2a8aeSBen Dooks err = parse_mtd_partitions(info->mtd, probes, 23099f2a8aeSBen Dooks &info->partitions, 0); 23199f2a8aeSBen Dooks if (err > 0) { 23299f2a8aeSBen Dooks err = add_mtd_partitions(info->mtd, info->partitions, 23399f2a8aeSBen Dooks err); 23499f2a8aeSBen Dooks } 23599f2a8aeSBen Dooks } 23699f2a8aeSBen Dooks #endif /* CONFIG_MTD_PARTITIONS */ 23799f2a8aeSBen Dooks 23899f2a8aeSBen Dooks if (add_mtd_device(info->mtd)) { 2393ae5eaecSRussell King dev_err(&pdev->dev, "add_mtd_device() failed\n"); 24099f2a8aeSBen Dooks err = -ENOMEM; 24199f2a8aeSBen Dooks } 24299f2a8aeSBen Dooks 2433ae5eaecSRussell King dev_info(&pdev->dev, "registered mtd device\n"); 24499f2a8aeSBen Dooks return err; 24599f2a8aeSBen Dooks 24699f2a8aeSBen Dooks exit_free: 2473ae5eaecSRussell King platram_remove(pdev); 24899f2a8aeSBen Dooks exit_error: 24999f2a8aeSBen Dooks return err; 25099f2a8aeSBen Dooks } 25199f2a8aeSBen Dooks 25299f2a8aeSBen Dooks /* device driver info */ 25399f2a8aeSBen Dooks 2543ae5eaecSRussell King static struct platform_driver platram_driver = { 25599f2a8aeSBen Dooks .probe = platram_probe, 25699f2a8aeSBen Dooks .remove = platram_remove, 2573ae5eaecSRussell King .driver = { 2583ae5eaecSRussell King .name = "mtd-ram", 2593ae5eaecSRussell King .owner = THIS_MODULE, 2603ae5eaecSRussell King }, 26199f2a8aeSBen Dooks }; 26299f2a8aeSBen Dooks 26399f2a8aeSBen Dooks /* module init/exit */ 26499f2a8aeSBen Dooks 26599f2a8aeSBen Dooks static int __init platram_init(void) 26699f2a8aeSBen Dooks { 26799f2a8aeSBen Dooks printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n"); 2683ae5eaecSRussell King return platform_driver_register(&platram_driver); 26999f2a8aeSBen Dooks } 27099f2a8aeSBen Dooks 27199f2a8aeSBen Dooks static void __exit platram_exit(void) 27299f2a8aeSBen Dooks { 2733ae5eaecSRussell King platform_driver_unregister(&platram_driver); 27499f2a8aeSBen Dooks } 27599f2a8aeSBen Dooks 27699f2a8aeSBen Dooks module_init(platram_init); 27799f2a8aeSBen Dooks module_exit(platram_exit); 27899f2a8aeSBen Dooks 27999f2a8aeSBen Dooks MODULE_LICENSE("GPL"); 28099f2a8aeSBen Dooks MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); 28199f2a8aeSBen Dooks MODULE_DESCRIPTION("MTD platform RAM map driver"); 282