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 * 999f2a8aeSBen Dooks * This program is free software; you can redistribute it and/or modify 1099f2a8aeSBen Dooks * it under the terms of the GNU General Public License as published by 1199f2a8aeSBen Dooks * the Free Software Foundation; either version 2 of the License, or 1299f2a8aeSBen Dooks * (at your option) any later version. 1399f2a8aeSBen Dooks * 1499f2a8aeSBen Dooks * This program is distributed in the hope that it will be useful, 1599f2a8aeSBen Dooks * but WITHOUT ANY WARRANTY; without even the implied warranty of 1699f2a8aeSBen Dooks * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1799f2a8aeSBen Dooks * GNU General Public License for more details. 1899f2a8aeSBen Dooks * 1999f2a8aeSBen Dooks * You should have received a copy of the GNU General Public License 2099f2a8aeSBen Dooks * along with this program; if not, write to the Free Software 2199f2a8aeSBen Dooks * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 2299f2a8aeSBen Dooks */ 2399f2a8aeSBen Dooks 2499f2a8aeSBen Dooks #include <linux/module.h> 2599f2a8aeSBen Dooks #include <linux/types.h> 2699f2a8aeSBen Dooks #include <linux/init.h> 2799f2a8aeSBen Dooks #include <linux/kernel.h> 2899f2a8aeSBen Dooks #include <linux/string.h> 2999f2a8aeSBen Dooks #include <linux/ioport.h> 3099f2a8aeSBen Dooks #include <linux/device.h> 314e57b681STim Schmielau #include <linux/slab.h> 32d052d1beSRussell King #include <linux/platform_device.h> 3399f2a8aeSBen Dooks 3499f2a8aeSBen Dooks #include <linux/mtd/mtd.h> 3599f2a8aeSBen Dooks #include <linux/mtd/map.h> 3699f2a8aeSBen Dooks #include <linux/mtd/partitions.h> 3799f2a8aeSBen Dooks #include <linux/mtd/plat-ram.h> 3899f2a8aeSBen Dooks 3999f2a8aeSBen Dooks #include <asm/io.h> 4099f2a8aeSBen Dooks 4199f2a8aeSBen Dooks /* private structure for each mtd platform ram device created */ 4299f2a8aeSBen Dooks 4399f2a8aeSBen Dooks struct platram_info { 4499f2a8aeSBen Dooks struct device *dev; 4599f2a8aeSBen Dooks struct mtd_info *mtd; 4699f2a8aeSBen Dooks struct map_info map; 4799f2a8aeSBen Dooks struct mtd_partition *partitions; 4875757006SFlorian Fainelli bool free_partitions; 4999f2a8aeSBen Dooks struct resource *area; 5099f2a8aeSBen Dooks struct platdata_mtd_ram *pdata; 5199f2a8aeSBen Dooks }; 5299f2a8aeSBen Dooks 5399f2a8aeSBen Dooks /* to_platram_info() 5499f2a8aeSBen Dooks * 5599f2a8aeSBen Dooks * device private data to struct platram_info conversion 5699f2a8aeSBen Dooks */ 5799f2a8aeSBen Dooks 583ae5eaecSRussell King static inline struct platram_info *to_platram_info(struct platform_device *dev) 5999f2a8aeSBen Dooks { 603ae5eaecSRussell King return (struct platram_info *)platform_get_drvdata(dev); 6199f2a8aeSBen Dooks } 6299f2a8aeSBen Dooks 6399f2a8aeSBen Dooks /* platram_setrw 6499f2a8aeSBen Dooks * 6599f2a8aeSBen Dooks * call the platform device's set rw/ro control 6699f2a8aeSBen Dooks * 6799f2a8aeSBen Dooks * to = 0 => read-only 6899f2a8aeSBen Dooks * = 1 => read-write 6999f2a8aeSBen Dooks */ 7099f2a8aeSBen Dooks 7199f2a8aeSBen Dooks static inline void platram_setrw(struct platram_info *info, int to) 7299f2a8aeSBen Dooks { 7399f2a8aeSBen Dooks if (info->pdata == NULL) 7499f2a8aeSBen Dooks return; 7599f2a8aeSBen Dooks 7699f2a8aeSBen Dooks if (info->pdata->set_rw != NULL) 7799f2a8aeSBen Dooks (info->pdata->set_rw)(info->dev, to); 7899f2a8aeSBen Dooks } 7999f2a8aeSBen Dooks 8099f2a8aeSBen Dooks /* platram_remove 8199f2a8aeSBen Dooks * 8299f2a8aeSBen Dooks * called to remove the device from the driver's control 8399f2a8aeSBen Dooks */ 8499f2a8aeSBen Dooks 853ae5eaecSRussell King static int platram_remove(struct platform_device *pdev) 8699f2a8aeSBen Dooks { 873ae5eaecSRussell King struct platram_info *info = to_platram_info(pdev); 8899f2a8aeSBen Dooks 893ae5eaecSRussell King platform_set_drvdata(pdev, NULL); 9099f2a8aeSBen Dooks 913ae5eaecSRussell King dev_dbg(&pdev->dev, "removing device\n"); 9299f2a8aeSBen Dooks 9399f2a8aeSBen Dooks if (info == NULL) 9499f2a8aeSBen Dooks return 0; 9599f2a8aeSBen Dooks 9699f2a8aeSBen Dooks if (info->mtd) { 9799f2a8aeSBen Dooks #ifdef CONFIG_MTD_PARTITIONS 9899f2a8aeSBen Dooks if (info->partitions) { 9999f2a8aeSBen Dooks del_mtd_partitions(info->mtd); 10075757006SFlorian Fainelli if (info->free_partitions) 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; 17975757006SFlorian Fainelli info->map.name = pdata->mapname != NULL ? 18075757006SFlorian Fainelli (char *)pdata->mapname : (char *)pdev->name; 18199f2a8aeSBen Dooks info->map.bankwidth = pdata->bankwidth; 18299f2a8aeSBen Dooks 18399f2a8aeSBen Dooks /* register our usage of the memory area */ 18499f2a8aeSBen Dooks 1853ae5eaecSRussell King info->area = request_mem_region(res->start, info->map.size, pdev->name); 18699f2a8aeSBen Dooks if (info->area == NULL) { 1873ae5eaecSRussell King dev_err(&pdev->dev, "failed to request memory region\n"); 18899f2a8aeSBen Dooks err = -EIO; 18999f2a8aeSBen Dooks goto exit_free; 19099f2a8aeSBen Dooks } 19199f2a8aeSBen Dooks 19299f2a8aeSBen Dooks /* remap the memory area */ 19399f2a8aeSBen Dooks 19499f2a8aeSBen Dooks info->map.virt = ioremap(res->start, info->map.size); 1953ae5eaecSRussell King dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size); 19699f2a8aeSBen Dooks 19799f2a8aeSBen Dooks if (info->map.virt == NULL) { 1983ae5eaecSRussell King dev_err(&pdev->dev, "failed to ioremap() region\n"); 19999f2a8aeSBen Dooks err = -EIO; 20099f2a8aeSBen Dooks goto exit_free; 20199f2a8aeSBen Dooks } 20299f2a8aeSBen Dooks 20399f2a8aeSBen Dooks simple_map_init(&info->map); 20499f2a8aeSBen Dooks 2053ae5eaecSRussell King dev_dbg(&pdev->dev, "initialised map, probing for mtd\n"); 20699f2a8aeSBen Dooks 20775757006SFlorian Fainelli /* probe for the right mtd map driver 20875757006SFlorian Fainelli * supplied by the platform_data struct */ 20999f2a8aeSBen Dooks 210a01e035eSHarvey Harrison if (pdata->map_probes) { 21175757006SFlorian Fainelli const char **map_probes = pdata->map_probes; 21275757006SFlorian Fainelli 21375757006SFlorian Fainelli for ( ; !info->mtd && *map_probes; map_probes++) 21475757006SFlorian Fainelli info->mtd = do_map_probe(*map_probes , &info->map); 21575757006SFlorian Fainelli } 21675757006SFlorian Fainelli /* fallback to map_ram */ 21775757006SFlorian Fainelli else 21899f2a8aeSBen Dooks info->mtd = do_map_probe("map_ram", &info->map); 21975757006SFlorian Fainelli 22099f2a8aeSBen Dooks if (info->mtd == NULL) { 2213ae5eaecSRussell King dev_err(&pdev->dev, "failed to probe for map_ram\n"); 22299f2a8aeSBen Dooks err = -ENOMEM; 22399f2a8aeSBen Dooks goto exit_free; 22499f2a8aeSBen Dooks } 22599f2a8aeSBen Dooks 22699f2a8aeSBen Dooks info->mtd->owner = THIS_MODULE; 22787f39f04SDavid Brownell info->mtd->dev.parent = &pdev->dev; 22899f2a8aeSBen Dooks 22999f2a8aeSBen Dooks platram_setrw(info, PLATRAM_RW); 23099f2a8aeSBen Dooks 23199f2a8aeSBen Dooks /* check to see if there are any available partitions, or wether 23299f2a8aeSBen Dooks * to add this device whole */ 23399f2a8aeSBen Dooks 23499f2a8aeSBen Dooks #ifdef CONFIG_MTD_PARTITIONS 23575757006SFlorian Fainelli if (!pdata->nr_partitions) { 23675757006SFlorian Fainelli /* try to probe using the supplied probe type */ 23775757006SFlorian Fainelli if (pdata->probes) { 23875757006SFlorian Fainelli err = parse_mtd_partitions(info->mtd, pdata->probes, 23999f2a8aeSBen Dooks &info->partitions, 0); 24075757006SFlorian Fainelli info->free_partitions = 1; 24175757006SFlorian Fainelli if (err > 0) 24275757006SFlorian Fainelli err = add_mtd_partitions(info->mtd, 24375757006SFlorian Fainelli info->partitions, err); 24499f2a8aeSBen Dooks } 24599f2a8aeSBen Dooks } 24675757006SFlorian Fainelli /* use the static mapping */ 24775757006SFlorian Fainelli else 24875757006SFlorian Fainelli err = add_mtd_partitions(info->mtd, pdata->partitions, 24975757006SFlorian Fainelli pdata->nr_partitions); 25099f2a8aeSBen Dooks #endif /* CONFIG_MTD_PARTITIONS */ 25199f2a8aeSBen Dooks 25299f2a8aeSBen Dooks if (add_mtd_device(info->mtd)) { 2533ae5eaecSRussell King dev_err(&pdev->dev, "add_mtd_device() failed\n"); 25499f2a8aeSBen Dooks err = -ENOMEM; 25599f2a8aeSBen Dooks } 25699f2a8aeSBen Dooks 25775757006SFlorian Fainelli if (!err) 2583ae5eaecSRussell King dev_info(&pdev->dev, "registered mtd device\n"); 25975757006SFlorian Fainelli 26099f2a8aeSBen Dooks return err; 26199f2a8aeSBen Dooks 26299f2a8aeSBen Dooks exit_free: 2633ae5eaecSRussell King platram_remove(pdev); 26499f2a8aeSBen Dooks exit_error: 26599f2a8aeSBen Dooks return err; 26699f2a8aeSBen Dooks } 26799f2a8aeSBen Dooks 26899f2a8aeSBen Dooks /* device driver info */ 26999f2a8aeSBen Dooks 27041d867c9SKay Sievers /* work with hotplug and coldplug */ 27141d867c9SKay Sievers MODULE_ALIAS("platform:mtd-ram"); 27241d867c9SKay Sievers 2733ae5eaecSRussell King static struct platform_driver platram_driver = { 27499f2a8aeSBen Dooks .probe = platram_probe, 27599f2a8aeSBen Dooks .remove = platram_remove, 2763ae5eaecSRussell King .driver = { 2773ae5eaecSRussell King .name = "mtd-ram", 2783ae5eaecSRussell King .owner = THIS_MODULE, 2793ae5eaecSRussell King }, 28099f2a8aeSBen Dooks }; 28199f2a8aeSBen Dooks 28299f2a8aeSBen Dooks /* module init/exit */ 28399f2a8aeSBen Dooks 28499f2a8aeSBen Dooks static int __init platram_init(void) 28599f2a8aeSBen Dooks { 28699f2a8aeSBen Dooks printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n"); 2873ae5eaecSRussell King return platform_driver_register(&platram_driver); 28899f2a8aeSBen Dooks } 28999f2a8aeSBen Dooks 29099f2a8aeSBen Dooks static void __exit platram_exit(void) 29199f2a8aeSBen Dooks { 2923ae5eaecSRussell King platform_driver_unregister(&platram_driver); 29399f2a8aeSBen Dooks } 29499f2a8aeSBen Dooks 29599f2a8aeSBen Dooks module_init(platram_init); 29699f2a8aeSBen Dooks module_exit(platram_exit); 29799f2a8aeSBen Dooks 29899f2a8aeSBen Dooks MODULE_LICENSE("GPL"); 29999f2a8aeSBen Dooks MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); 30099f2a8aeSBen Dooks MODULE_DESCRIPTION("MTD platform RAM map driver"); 301