xref: /openbmc/linux/drivers/mtd/maps/plat-ram.c (revision 3e174049)
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  *
93e174049SThomas Gleixner  * $Id: plat-ram.c,v 1.6 2005/11/07 00:52:24 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 
5999f2a8aeSBen Dooks static inline struct platram_info *to_platram_info(struct device *dev)
6099f2a8aeSBen Dooks {
6199f2a8aeSBen Dooks 	return (struct platram_info *)dev_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 
8699f2a8aeSBen Dooks static int platram_remove(struct device *dev)
8799f2a8aeSBen Dooks {
8899f2a8aeSBen Dooks 	struct platram_info *info = to_platram_info(dev);
8999f2a8aeSBen Dooks 
9099f2a8aeSBen Dooks 	dev_set_drvdata(dev, NULL);
9199f2a8aeSBen Dooks 
9299f2a8aeSBen Dooks 	dev_dbg(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 
13399f2a8aeSBen Dooks static int platram_probe(struct device *dev)
13499f2a8aeSBen Dooks {
13599f2a8aeSBen Dooks 	struct platform_device *pd = to_platform_device(dev);
13699f2a8aeSBen Dooks 	struct platdata_mtd_ram	*pdata;
13799f2a8aeSBen Dooks 	struct platram_info *info;
13899f2a8aeSBen Dooks 	struct resource *res;
13999f2a8aeSBen Dooks 	int err = 0;
14099f2a8aeSBen Dooks 
14199f2a8aeSBen Dooks 	dev_dbg(dev, "probe entered\n");
14299f2a8aeSBen Dooks 
14399f2a8aeSBen Dooks 	if (dev->platform_data == NULL) {
14499f2a8aeSBen Dooks 		dev_err(dev, "no platform data supplied\n");
14599f2a8aeSBen Dooks 		err = -ENOENT;
14699f2a8aeSBen Dooks 		goto exit_error;
14799f2a8aeSBen Dooks 	}
14899f2a8aeSBen Dooks 
14999f2a8aeSBen Dooks 	pdata = dev->platform_data;
15099f2a8aeSBen Dooks 
15199f2a8aeSBen Dooks 	info = kmalloc(sizeof(*info), GFP_KERNEL);
15299f2a8aeSBen Dooks 	if (info == NULL) {
15399f2a8aeSBen Dooks 		dev_err(dev, "no memory for flash info\n");
15499f2a8aeSBen Dooks 		err = -ENOMEM;
15599f2a8aeSBen Dooks 		goto exit_error;
15699f2a8aeSBen Dooks 	}
15799f2a8aeSBen Dooks 
158a921e28bSThomas Gleixner 	memset(info, 0, sizeof(*info));
15999f2a8aeSBen Dooks 	dev_set_drvdata(dev, info);
16099f2a8aeSBen Dooks 
16199f2a8aeSBen Dooks 	info->dev = dev;
16299f2a8aeSBen Dooks 	info->pdata = pdata;
16399f2a8aeSBen Dooks 
16499f2a8aeSBen Dooks 	/* get the resource for the memory mapping */
16599f2a8aeSBen Dooks 
16699f2a8aeSBen Dooks 	res = platform_get_resource(pd, IORESOURCE_MEM, 0);
16799f2a8aeSBen Dooks 
16899f2a8aeSBen Dooks 	if (res == NULL) {
16999f2a8aeSBen Dooks 		dev_err(dev, "no memory resource specified\n");
17099f2a8aeSBen Dooks 		err = -ENOENT;
17199f2a8aeSBen Dooks 		goto exit_free;
17299f2a8aeSBen Dooks 	}
17399f2a8aeSBen Dooks 
17499f2a8aeSBen Dooks 	dev_dbg(dev, "got platform resource %p (0x%lx)\n", res, res->start);
17599f2a8aeSBen Dooks 
17699f2a8aeSBen Dooks 	/* setup map parameters */
17799f2a8aeSBen Dooks 
17899f2a8aeSBen Dooks 	info->map.phys = res->start;
17999f2a8aeSBen Dooks 	info->map.size = (res->end - res->start) + 1;
1803e174049SThomas Gleixner 	info->map.name = pdata->mapname != NULL ? pdata->mapname : (char *)pd->name;
18199f2a8aeSBen Dooks 	info->map.bankwidth = pdata->bankwidth;
18299f2a8aeSBen Dooks 
18399f2a8aeSBen Dooks 	/* register our usage of the memory area */
18499f2a8aeSBen Dooks 
18599f2a8aeSBen Dooks 	info->area = request_mem_region(res->start, info->map.size, pd->name);
18699f2a8aeSBen Dooks 	if (info->area == NULL) {
18799f2a8aeSBen Dooks 		dev_err(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);
195fb6bb52dSBen Dooks 	dev_dbg(dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
19699f2a8aeSBen Dooks 
19799f2a8aeSBen Dooks 	if (info->map.virt == NULL) {
19899f2a8aeSBen Dooks 		dev_err(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 
20599f2a8aeSBen Dooks 	dev_dbg(dev, "initialised map, probing for mtd\n");
20699f2a8aeSBen Dooks 
20799f2a8aeSBen Dooks 	/* probe for the right mtd map driver */
20899f2a8aeSBen Dooks 
20999f2a8aeSBen Dooks 	info->mtd = do_map_probe("map_ram" , &info->map);
21099f2a8aeSBen Dooks 	if (info->mtd == NULL) {
21199f2a8aeSBen Dooks 		dev_err(dev, "failed to probe for map_ram\n");
21299f2a8aeSBen Dooks 		err = -ENOMEM;
21399f2a8aeSBen Dooks 		goto exit_free;
21499f2a8aeSBen Dooks 	}
21599f2a8aeSBen Dooks 
21699f2a8aeSBen Dooks 	info->mtd->owner = THIS_MODULE;
21799f2a8aeSBen Dooks 
21899f2a8aeSBen Dooks 	platram_setrw(info, PLATRAM_RW);
21999f2a8aeSBen Dooks 
22099f2a8aeSBen Dooks 	/* check to see if there are any available partitions, or wether
22199f2a8aeSBen Dooks 	 * to add this device whole */
22299f2a8aeSBen Dooks 
22399f2a8aeSBen Dooks #ifdef CONFIG_MTD_PARTITIONS
22499f2a8aeSBen Dooks 	if (pdata->nr_partitions > 0) {
22599f2a8aeSBen Dooks 		const char **probes = { NULL };
22699f2a8aeSBen Dooks 
22799f2a8aeSBen Dooks 		if (pdata->probes)
22899f2a8aeSBen Dooks 			probes = (const char **)pdata->probes;
22999f2a8aeSBen Dooks 
23099f2a8aeSBen Dooks 		err = parse_mtd_partitions(info->mtd, probes,
23199f2a8aeSBen Dooks 					   &info->partitions, 0);
23299f2a8aeSBen Dooks 		if (err > 0) {
23399f2a8aeSBen Dooks 			err = add_mtd_partitions(info->mtd, info->partitions,
23499f2a8aeSBen Dooks 						 err);
23599f2a8aeSBen Dooks 		}
23699f2a8aeSBen Dooks 	}
23799f2a8aeSBen Dooks #endif /* CONFIG_MTD_PARTITIONS */
23899f2a8aeSBen Dooks 
23999f2a8aeSBen Dooks 	if (add_mtd_device(info->mtd)) {
24099f2a8aeSBen Dooks 		dev_err(dev, "add_mtd_device() failed\n");
24199f2a8aeSBen Dooks 		err = -ENOMEM;
24299f2a8aeSBen Dooks 	}
24399f2a8aeSBen Dooks 
24499f2a8aeSBen Dooks 	dev_info(dev, "registered mtd device\n");
24599f2a8aeSBen Dooks 	return err;
24699f2a8aeSBen Dooks 
24799f2a8aeSBen Dooks  exit_free:
24899f2a8aeSBen Dooks 	platram_remove(dev);
24999f2a8aeSBen Dooks  exit_error:
25099f2a8aeSBen Dooks 	return err;
25199f2a8aeSBen Dooks }
25299f2a8aeSBen Dooks 
25399f2a8aeSBen Dooks /* device driver info */
25499f2a8aeSBen Dooks 
25599f2a8aeSBen Dooks static struct device_driver platram_driver = {
25699f2a8aeSBen Dooks 	.name		= "mtd-ram",
257df2e1629SBen Dooks 	.owner		= THIS_MODULE,
25899f2a8aeSBen Dooks 	.bus		= &platform_bus_type,
25999f2a8aeSBen Dooks 	.probe		= platram_probe,
26099f2a8aeSBen Dooks 	.remove		= platram_remove,
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");
26899f2a8aeSBen Dooks 	return driver_register(&platram_driver);
26999f2a8aeSBen Dooks }
27099f2a8aeSBen Dooks 
27199f2a8aeSBen Dooks static void __exit platram_exit(void)
27299f2a8aeSBen Dooks {
27399f2a8aeSBen Dooks 	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