plat-ram.c 6.3 KB
Newer Older
B
Ben Dooks 已提交
1 2 3 4 5 6
/* drivers/mtd/maps/plat-ram.c
 *
 * (c) 2004-2005 Simtec Electronics
 *	http://www.simtec.co.uk/products/SWLINUX/
 *	Ben Dooks <ben@simtec.co.uk>
 *
7
 * Generic platform device based RAM map
B
Ben Dooks 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <linux/module.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/device.h>
T
Tim Schmielau 已提交
31
#include <linux/slab.h>
32
#include <linux/platform_device.h>
B
Ben Dooks 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/plat-ram.h>

#include <asm/io.h>

/* private structure for each mtd platform ram device created */

struct platram_info {
	struct device		*dev;
	struct mtd_info		*mtd;
	struct map_info		 map;
	struct resource		*area;
	struct platdata_mtd_ram	*pdata;
};

/* to_platram_info()
 *
 * device private data to struct platram_info conversion
*/

56
static inline struct platram_info *to_platram_info(struct platform_device *dev)
B
Ben Dooks 已提交
57
{
58
	return (struct platram_info *)platform_get_drvdata(dev);
B
Ben Dooks 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
}

/* platram_setrw
 *
 * call the platform device's set rw/ro control
 *
 * to = 0 => read-only
 *    = 1 => read-write
*/

static inline void platram_setrw(struct platram_info *info, int to)
{
	if (info->pdata == NULL)
		return;

	if (info->pdata->set_rw != NULL)
		(info->pdata->set_rw)(info->dev, to);
}

/* platram_remove
 *
 * called to remove the device from the driver's control
*/

83
static int platram_remove(struct platform_device *pdev)
B
Ben Dooks 已提交
84
{
85
	struct platram_info *info = to_platram_info(pdev);
B
Ben Dooks 已提交
86

87
	platform_set_drvdata(pdev, NULL);
B
Ben Dooks 已提交
88

89
	dev_dbg(&pdev->dev, "removing device\n");
B
Ben Dooks 已提交
90

91
	if (info == NULL)
B
Ben Dooks 已提交
92 93 94
		return 0;

	if (info->mtd) {
95
		mtd_device_unregister(info->mtd);
B
Ben Dooks 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		map_destroy(info->mtd);
	}

	/* ensure ram is left read-only */

	platram_setrw(info, PLATRAM_RO);

	/* release resources */

	if (info->area) {
		release_resource(info->area);
		kfree(info->area);
	}

	if (info->map.virt != NULL)
		iounmap(info->map.virt);
112

B
Ben Dooks 已提交
113 114 115 116 117 118 119 120 121 122 123
	kfree(info);

	return 0;
}

/* platram_probe
 *
 * called from device drive system when a device matching our
 * driver is found.
*/

124
static int platram_probe(struct platform_device *pdev)
B
Ben Dooks 已提交
125 126 127 128 129 130
{
	struct platdata_mtd_ram	*pdata;
	struct platram_info *info;
	struct resource *res;
	int err = 0;

131
	dev_dbg(&pdev->dev, "probe entered\n");
132

133 134
	if (pdev->dev.platform_data == NULL) {
		dev_err(&pdev->dev, "no platform data supplied\n");
B
Ben Dooks 已提交
135 136 137 138
		err = -ENOENT;
		goto exit_error;
	}

139
	pdata = pdev->dev.platform_data;
B
Ben Dooks 已提交
140

141
	info = kzalloc(sizeof(*info), GFP_KERNEL);
B
Ben Dooks 已提交
142
	if (info == NULL) {
143
		dev_err(&pdev->dev, "no memory for flash info\n");
B
Ben Dooks 已提交
144 145 146 147
		err = -ENOMEM;
		goto exit_error;
	}

148
	platform_set_drvdata(pdev, info);
B
Ben Dooks 已提交
149

150
	info->dev = &pdev->dev;
B
Ben Dooks 已提交
151 152 153 154
	info->pdata = pdata;

	/* get the resource for the memory mapping */

155
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
B
Ben Dooks 已提交
156 157

	if (res == NULL) {
158
		dev_err(&pdev->dev, "no memory resource specified\n");
B
Ben Dooks 已提交
159 160 161 162
		err = -ENOENT;
		goto exit_free;
	}

163 164
	dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
		(unsigned long long)res->start);
B
Ben Dooks 已提交
165 166 167 168

	/* setup map parameters */

	info->map.phys = res->start;
W
Wolfram Sang 已提交
169
	info->map.size = resource_size(res);
170 171
	info->map.name = pdata->mapname != NULL ?
			(char *)pdata->mapname : (char *)pdev->name;
B
Ben Dooks 已提交
172 173 174 175
	info->map.bankwidth = pdata->bankwidth;

	/* register our usage of the memory area */

176
	info->area = request_mem_region(res->start, info->map.size, pdev->name);
B
Ben Dooks 已提交
177
	if (info->area == NULL) {
178
		dev_err(&pdev->dev, "failed to request memory region\n");
B
Ben Dooks 已提交
179 180 181 182 183 184 185
		err = -EIO;
		goto exit_free;
	}

	/* remap the memory area */

	info->map.virt = ioremap(res->start, info->map.size);
186
	dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
B
Ben Dooks 已提交
187 188

	if (info->map.virt == NULL) {
189
		dev_err(&pdev->dev, "failed to ioremap() region\n");
B
Ben Dooks 已提交
190 191 192 193 194 195
		err = -EIO;
		goto exit_free;
	}

	simple_map_init(&info->map);

196
	dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
B
Ben Dooks 已提交
197

198 199 200
	/* probe for the right mtd map driver
	 * supplied by the platform_data struct */

201
	if (pdata->map_probes) {
202 203 204 205 206 207 208 209
		const char **map_probes = pdata->map_probes;

		for ( ; !info->mtd && *map_probes; map_probes++)
			info->mtd = do_map_probe(*map_probes , &info->map);
	}
	/* fallback to map_ram */
	else
		info->mtd = do_map_probe("map_ram", &info->map);
B
Ben Dooks 已提交
210 211

	if (info->mtd == NULL) {
212
		dev_err(&pdev->dev, "failed to probe for map_ram\n");
B
Ben Dooks 已提交
213 214 215 216 217
		err = -ENOMEM;
		goto exit_free;
	}

	info->mtd->owner = THIS_MODULE;
218
	info->mtd->dev.parent = &pdev->dev;
B
Ben Dooks 已提交
219 220 221 222 223 224

	platram_setrw(info, PLATRAM_RW);

	/* check to see if there are any available partitions, or wether
	 * to add this device whole */

225 226
	err = mtd_device_parse_register(info->mtd, pdata->probes, 0,
			pdata->partitions, pdata->nr_partitions);
227 228 229
	if (!err)
		dev_info(&pdev->dev, "registered mtd device\n");

230 231 232 233 234 235 236 237
	if (pdata->nr_partitions) {
		/* add the whole device. */
		err = mtd_device_register(info->mtd, NULL, 0);
		if (err) {
			dev_err(&pdev->dev,
				"failed to register the entire device\n");
		}
	}
238

B
Ben Dooks 已提交
239 240 241
	return err;

 exit_free:
242
	platram_remove(pdev);
B
Ben Dooks 已提交
243 244 245 246 247 248
 exit_error:
	return err;
}

/* device driver info */

249 250 251
/* work with hotplug and coldplug */
MODULE_ALIAS("platform:mtd-ram");

252
static struct platform_driver platram_driver = {
B
Ben Dooks 已提交
253 254
	.probe		= platram_probe,
	.remove		= platram_remove,
255 256 257 258
	.driver		= {
		.name	= "mtd-ram",
		.owner	= THIS_MODULE,
	},
B
Ben Dooks 已提交
259 260 261 262 263 264 265
};

/* module init/exit */

static int __init platram_init(void)
{
	printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
266
	return platform_driver_register(&platram_driver);
B
Ben Dooks 已提交
267 268 269 270
}

static void __exit platram_exit(void)
{
271
	platform_driver_unregister(&platram_driver);
B
Ben Dooks 已提交
272 273 274 275 276 277 278 279
}

module_init(platram_init);
module_exit(platram_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
MODULE_DESCRIPTION("MTD platform RAM map driver");