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 * 7947af294SStefan Weil * Generic platform 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 resource *area; 4899f2a8aeSBen Dooks struct platdata_mtd_ram *pdata; 4999f2a8aeSBen Dooks }; 5099f2a8aeSBen Dooks 5199f2a8aeSBen Dooks /* to_platram_info() 5299f2a8aeSBen Dooks * 5399f2a8aeSBen Dooks * device private data to struct platram_info conversion 5499f2a8aeSBen Dooks */ 5599f2a8aeSBen Dooks 563ae5eaecSRussell King static inline struct platram_info *to_platram_info(struct platform_device *dev) 5799f2a8aeSBen Dooks { 5814ac0785SJingoo Han return platform_get_drvdata(dev); 5999f2a8aeSBen Dooks } 6099f2a8aeSBen Dooks 6199f2a8aeSBen Dooks /* platram_setrw 6299f2a8aeSBen Dooks * 6399f2a8aeSBen Dooks * call the platform device's set rw/ro control 6499f2a8aeSBen Dooks * 6599f2a8aeSBen Dooks * to = 0 => read-only 6699f2a8aeSBen Dooks * = 1 => read-write 6799f2a8aeSBen Dooks */ 6899f2a8aeSBen Dooks 6999f2a8aeSBen Dooks static inline void platram_setrw(struct platram_info *info, int to) 7099f2a8aeSBen Dooks { 7199f2a8aeSBen Dooks if (info->pdata == NULL) 7299f2a8aeSBen Dooks return; 7399f2a8aeSBen Dooks 7499f2a8aeSBen Dooks if (info->pdata->set_rw != NULL) 7599f2a8aeSBen Dooks (info->pdata->set_rw)(info->dev, to); 7699f2a8aeSBen Dooks } 7799f2a8aeSBen Dooks 7899f2a8aeSBen Dooks /* platram_remove 7999f2a8aeSBen Dooks * 8099f2a8aeSBen Dooks * called to remove the device from the driver's control 8199f2a8aeSBen Dooks */ 8299f2a8aeSBen Dooks 833ae5eaecSRussell King static int platram_remove(struct platform_device *pdev) 8499f2a8aeSBen Dooks { 853ae5eaecSRussell King struct platram_info *info = to_platram_info(pdev); 8699f2a8aeSBen Dooks 873ae5eaecSRussell King dev_dbg(&pdev->dev, "removing device\n"); 8899f2a8aeSBen Dooks 8999f2a8aeSBen Dooks if (info == NULL) 9099f2a8aeSBen Dooks return 0; 9199f2a8aeSBen Dooks 9299f2a8aeSBen Dooks if (info->mtd) { 93095dd500SJamie Iles mtd_device_unregister(info->mtd); 9499f2a8aeSBen Dooks map_destroy(info->mtd); 9599f2a8aeSBen Dooks } 9699f2a8aeSBen Dooks 9799f2a8aeSBen Dooks /* ensure ram is left read-only */ 9899f2a8aeSBen Dooks 9999f2a8aeSBen Dooks platram_setrw(info, PLATRAM_RO); 10099f2a8aeSBen Dooks 10199f2a8aeSBen Dooks /* release resources */ 10299f2a8aeSBen Dooks 10399f2a8aeSBen Dooks if (info->area) { 10499f2a8aeSBen Dooks release_resource(info->area); 10599f2a8aeSBen Dooks kfree(info->area); 10699f2a8aeSBen Dooks } 10799f2a8aeSBen Dooks 10899f2a8aeSBen Dooks if (info->map.virt != NULL) 10999f2a8aeSBen Dooks iounmap(info->map.virt); 11099f2a8aeSBen Dooks 11199f2a8aeSBen Dooks kfree(info); 11299f2a8aeSBen Dooks 11399f2a8aeSBen Dooks return 0; 11499f2a8aeSBen Dooks } 11599f2a8aeSBen Dooks 11699f2a8aeSBen Dooks /* platram_probe 11799f2a8aeSBen Dooks * 11899f2a8aeSBen Dooks * called from device drive system when a device matching our 11999f2a8aeSBen Dooks * driver is found. 12099f2a8aeSBen Dooks */ 12199f2a8aeSBen Dooks 1223ae5eaecSRussell King static int platram_probe(struct platform_device *pdev) 12399f2a8aeSBen Dooks { 12499f2a8aeSBen Dooks struct platdata_mtd_ram *pdata; 12599f2a8aeSBen Dooks struct platram_info *info; 12699f2a8aeSBen Dooks struct resource *res; 12799f2a8aeSBen Dooks int err = 0; 12899f2a8aeSBen Dooks 1293ae5eaecSRussell King dev_dbg(&pdev->dev, "probe entered\n"); 13099f2a8aeSBen Dooks 131d20d5a57SJingoo Han if (dev_get_platdata(&pdev->dev) == NULL) { 1323ae5eaecSRussell King dev_err(&pdev->dev, "no platform data supplied\n"); 13399f2a8aeSBen Dooks err = -ENOENT; 13499f2a8aeSBen Dooks goto exit_error; 13599f2a8aeSBen Dooks } 13699f2a8aeSBen Dooks 137d20d5a57SJingoo Han pdata = dev_get_platdata(&pdev->dev); 13899f2a8aeSBen Dooks 13995b93a0cSBurman Yan info = kzalloc(sizeof(*info), GFP_KERNEL); 14099f2a8aeSBen Dooks if (info == NULL) { 1413ae5eaecSRussell King dev_err(&pdev->dev, "no memory for flash info\n"); 14299f2a8aeSBen Dooks err = -ENOMEM; 14399f2a8aeSBen Dooks goto exit_error; 14499f2a8aeSBen Dooks } 14599f2a8aeSBen Dooks 1463ae5eaecSRussell King platform_set_drvdata(pdev, info); 14799f2a8aeSBen Dooks 1483ae5eaecSRussell King info->dev = &pdev->dev; 14999f2a8aeSBen Dooks info->pdata = pdata; 15099f2a8aeSBen Dooks 15199f2a8aeSBen Dooks /* get the resource for the memory mapping */ 15299f2a8aeSBen Dooks 1533ae5eaecSRussell King res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 15499f2a8aeSBen Dooks 15599f2a8aeSBen Dooks if (res == NULL) { 1563ae5eaecSRussell King dev_err(&pdev->dev, "no memory resource specified\n"); 15799f2a8aeSBen Dooks err = -ENOENT; 15899f2a8aeSBen Dooks goto exit_free; 15999f2a8aeSBen Dooks } 16099f2a8aeSBen Dooks 16106d63cc5SRandy Dunlap dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res, 16206d63cc5SRandy Dunlap (unsigned long long)res->start); 16399f2a8aeSBen Dooks 16499f2a8aeSBen Dooks /* setup map parameters */ 16599f2a8aeSBen Dooks 16699f2a8aeSBen Dooks info->map.phys = res->start; 16776d6a479SWolfram Sang info->map.size = resource_size(res); 16875757006SFlorian Fainelli info->map.name = pdata->mapname != NULL ? 16975757006SFlorian Fainelli (char *)pdata->mapname : (char *)pdev->name; 17099f2a8aeSBen Dooks info->map.bankwidth = pdata->bankwidth; 17199f2a8aeSBen Dooks 17299f2a8aeSBen Dooks /* register our usage of the memory area */ 17399f2a8aeSBen Dooks 1743ae5eaecSRussell King info->area = request_mem_region(res->start, info->map.size, pdev->name); 17599f2a8aeSBen Dooks if (info->area == NULL) { 1763ae5eaecSRussell King dev_err(&pdev->dev, "failed to request memory region\n"); 17799f2a8aeSBen Dooks err = -EIO; 17899f2a8aeSBen Dooks goto exit_free; 17999f2a8aeSBen Dooks } 18099f2a8aeSBen Dooks 18199f2a8aeSBen Dooks /* remap the memory area */ 18299f2a8aeSBen Dooks 18399f2a8aeSBen Dooks info->map.virt = ioremap(res->start, info->map.size); 1843ae5eaecSRussell King dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size); 18599f2a8aeSBen Dooks 18699f2a8aeSBen Dooks if (info->map.virt == NULL) { 1873ae5eaecSRussell King dev_err(&pdev->dev, "failed to ioremap() region\n"); 18899f2a8aeSBen Dooks err = -EIO; 18999f2a8aeSBen Dooks goto exit_free; 19099f2a8aeSBen Dooks } 19199f2a8aeSBen Dooks 19299f2a8aeSBen Dooks simple_map_init(&info->map); 19399f2a8aeSBen Dooks 1943ae5eaecSRussell King dev_dbg(&pdev->dev, "initialised map, probing for mtd\n"); 19599f2a8aeSBen Dooks 19675757006SFlorian Fainelli /* probe for the right mtd map driver 19775757006SFlorian Fainelli * supplied by the platform_data struct */ 19899f2a8aeSBen Dooks 199a01e035eSHarvey Harrison if (pdata->map_probes) { 200d50dcb1dSArtem Bityutskiy const char * const *map_probes = pdata->map_probes; 20175757006SFlorian Fainelli 20275757006SFlorian Fainelli for ( ; !info->mtd && *map_probes; map_probes++) 20375757006SFlorian Fainelli info->mtd = do_map_probe(*map_probes , &info->map); 20475757006SFlorian Fainelli } 20575757006SFlorian Fainelli /* fallback to map_ram */ 20675757006SFlorian Fainelli else 20799f2a8aeSBen Dooks info->mtd = do_map_probe("map_ram", &info->map); 20875757006SFlorian Fainelli 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; 21687f39f04SDavid Brownell info->mtd->dev.parent = &pdev->dev; 21799f2a8aeSBen Dooks 21899f2a8aeSBen Dooks platram_setrw(info, PLATRAM_RW); 21999f2a8aeSBen Dooks 22048fc7f7eSAdam Buchbinder /* check to see if there are any available partitions, or whether 22199f2a8aeSBen Dooks * to add this device whole */ 22299f2a8aeSBen Dooks 22342d7fbe2SArtem Bityutskiy err = mtd_device_parse_register(info->mtd, pdata->probes, NULL, 22442d7fbe2SArtem Bityutskiy pdata->partitions, 22542d7fbe2SArtem Bityutskiy pdata->nr_partitions); 22675757006SFlorian Fainelli if (!err) 2273ae5eaecSRussell King dev_info(&pdev->dev, "registered mtd device\n"); 22875757006SFlorian Fainelli 229c3298791SIlya Yanok if (pdata->nr_partitions) { 230095dd500SJamie Iles /* add the whole device. */ 231095dd500SJamie Iles err = mtd_device_register(info->mtd, NULL, 0); 232c3298791SIlya Yanok if (err) { 233c3298791SIlya Yanok dev_err(&pdev->dev, 234c3298791SIlya Yanok "failed to register the entire device\n"); 235c3298791SIlya Yanok } 236c3298791SIlya Yanok } 237095dd500SJamie Iles 23899f2a8aeSBen Dooks return err; 23999f2a8aeSBen Dooks 24099f2a8aeSBen Dooks exit_free: 2413ae5eaecSRussell King platram_remove(pdev); 24299f2a8aeSBen Dooks exit_error: 24399f2a8aeSBen Dooks return err; 24499f2a8aeSBen Dooks } 24599f2a8aeSBen Dooks 24699f2a8aeSBen Dooks /* device driver info */ 24799f2a8aeSBen Dooks 24841d867c9SKay Sievers /* work with hotplug and coldplug */ 24941d867c9SKay Sievers MODULE_ALIAS("platform:mtd-ram"); 25041d867c9SKay Sievers 2513ae5eaecSRussell King static struct platform_driver platram_driver = { 25299f2a8aeSBen Dooks .probe = platram_probe, 25399f2a8aeSBen Dooks .remove = platram_remove, 2543ae5eaecSRussell King .driver = { 2553ae5eaecSRussell King .name = "mtd-ram", 2563ae5eaecSRussell King .owner = THIS_MODULE, 2573ae5eaecSRussell King }, 25899f2a8aeSBen Dooks }; 25999f2a8aeSBen Dooks 26098a7c747SSachin Kamat module_platform_driver(platram_driver); 26199f2a8aeSBen Dooks 26299f2a8aeSBen Dooks MODULE_LICENSE("GPL"); 26399f2a8aeSBen Dooks MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); 26499f2a8aeSBen Dooks MODULE_DESCRIPTION("MTD platform RAM map driver"); 265