integrator-flash.c 7.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*======================================================================

    drivers/mtd/maps/integrator-flash.c: ARM Integrator flash map driver
4

L
Linus Torvalds 已提交
5 6
    Copyright (C) 2000 ARM Limited
    Copyright (C) 2003 Deep Blue Solutions Ltd.
7

L
Linus Torvalds 已提交
8 9 10 11
   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.
12

L
Linus Torvalds 已提交
13 14 15 16
   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.
17

L
Linus Torvalds 已提交
18 19 20
   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
21 22

   This is access code for flashes using ARM's flash partitioning
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31
   standards.

======================================================================*/

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/ioport.h>
32
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
33
#include <linux/init.h>
34
#include <linux/io.h>
L
Linus Torvalds 已提交
35 36 37 38

#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
39
#include <linux/mtd/concat.h>
L
Linus Torvalds 已提交
40 41

#include <asm/mach/flash.h>
42
#include <mach/hardware.h>
L
Linus Torvalds 已提交
43 44
#include <asm/system.h>

45
struct armflash_subdev_info {
46
	char			*name;
47 48
	struct mtd_info		*mtd;
	struct map_info		map;
L
Linus Torvalds 已提交
49
	struct flash_platform_data *plat;
50 51 52
};

struct armflash_info {
L
Linus Torvalds 已提交
53 54 55
	struct resource		*res;
	struct mtd_partition	*parts;
	struct mtd_info		*mtd;
56 57
	int			nr_subdev;
	struct armflash_subdev_info subdev[0];
L
Linus Torvalds 已提交
58 59 60 61
};

static void armflash_set_vpp(struct map_info *map, int on)
{
62 63
	struct armflash_subdev_info *info =
		container_of(map, struct armflash_subdev_info, map);
L
Linus Torvalds 已提交
64 65 66 67 68 69 70

	if (info->plat && info->plat->set_vpp)
		info->plat->set_vpp(on);
}

static const char *probes[] = { "cmdlinepart", "RedBoot", "afs", NULL };

71 72
static int armflash_subdev_probe(struct armflash_subdev_info *subdev,
				 struct resource *res)
L
Linus Torvalds 已提交
73
{
74 75
	struct flash_platform_data *plat = subdev->plat;
	resource_size_t size = res->end - res->start + 1;
L
Linus Torvalds 已提交
76
	void __iomem *base;
77
	int err = 0;
L
Linus Torvalds 已提交
78

79
	if (!request_mem_region(res->start, size, subdev->name)) {
L
Linus Torvalds 已提交
80
		err = -EBUSY;
81
		goto out;
L
Linus Torvalds 已提交
82 83 84 85 86 87 88 89 90 91 92
	}

	base = ioremap(res->start, size);
	if (!base) {
		err = -ENOMEM;
		goto no_mem;
	}

	/*
	 * look for CFI based flash parts fitted to this board
	 */
93 94 95 96 97 98
	subdev->map.size	= size;
	subdev->map.bankwidth	= plat->width;
	subdev->map.phys	= res->start;
	subdev->map.virt	= base;
	subdev->map.name	= subdev->name;
	subdev->map.set_vpp	= armflash_set_vpp;
L
Linus Torvalds 已提交
99

100
	simple_map_init(&subdev->map);
L
Linus Torvalds 已提交
101 102 103 104 105 106

	/*
	 * Also, the CFI layer automatically works out what size
	 * of chips we have, and does the necessary identification
	 * for us automatically.
	 */
107 108
	subdev->mtd = do_map_probe(plat->map_name, &subdev->map);
	if (!subdev->mtd) {
L
Linus Torvalds 已提交
109 110 111 112
		err = -ENXIO;
		goto no_device;
	}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	subdev->mtd->owner = THIS_MODULE;

	/* Successful? */
	if (err == 0)
		return err;

	if (subdev->mtd)
		map_destroy(subdev->mtd);
 no_device:
	iounmap(base);
 no_mem:
	release_mem_region(res->start, size);
 out:
	return err;
}

static void armflash_subdev_remove(struct armflash_subdev_info *subdev)
{
	if (subdev->mtd)
		map_destroy(subdev->mtd);
	if (subdev->map.virt)
		iounmap(subdev->map.virt);
135 136
	kfree(subdev->name);
	subdev->name = NULL;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	release_mem_region(subdev->map.phys, subdev->map.size);
}

static int armflash_probe(struct platform_device *dev)
{
	struct flash_platform_data *plat = dev->dev.platform_data;
	unsigned int size;
	struct armflash_info *info;
	int i, nr, err;

	/* Count the number of devices */
	for (nr = 0; ; nr++)
		if (!platform_get_resource(dev, IORESOURCE_MEM, nr))
			break;
	if (nr == 0) {
		err = -ENODEV;
		goto out;
	}

	size = sizeof(struct armflash_info) +
		sizeof(struct armflash_subdev_info) * nr;
	info = kzalloc(size, GFP_KERNEL);
	if (!info) {
		err = -ENOMEM;
		goto out;
	}

	if (plat && plat->init) {
		err = plat->init();
		if (err)
			goto no_resource;
	}

	for (i = 0; i < nr; i++) {
		struct armflash_subdev_info *subdev = &info->subdev[i];
		struct resource *res;

		res = platform_get_resource(dev, IORESOURCE_MEM, i);
		if (!res)
			break;

		if (nr == 1)
			/* No MTD concatenation, just use the default name */
180
			subdev->name = kstrdup(dev_name(&dev->dev), GFP_KERNEL);
181
		else
182 183 184 185 186 187
			subdev->name = kasprintf(GFP_KERNEL, "%s-%d",
						 dev_name(&dev->dev), i);
		if (!subdev->name) {
			err = -ENOMEM;
			break;
		}
188 189 190
		subdev->plat = plat;

		err = armflash_subdev_probe(subdev, res);
191 192 193
		if (err) {
			kfree(subdev->name);
			subdev->name = NULL;
194
			break;
195
		}
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	}
	info->nr_subdev = i;

	if (err)
		goto subdev_err;

	if (info->nr_subdev == 1)
		info->mtd = info->subdev[0].mtd;
	else if (info->nr_subdev > 1) {
#ifdef CONFIG_MTD_CONCAT
		struct mtd_info *cdev[info->nr_subdev];

		/*
		 * We detected multiple devices.  Concatenate them together.
		 */
		for (i = 0; i < info->nr_subdev; i++)
			cdev[i] = info->subdev[i].mtd;

		info->mtd = mtd_concat_create(cdev, info->nr_subdev,
					      dev_name(&dev->dev));
		if (info->mtd == NULL)
			err = -ENXIO;
#else
		printk(KERN_ERR "armflash: multiple devices found but "
		       "MTD concat support disabled.\n");
		err = -ENXIO;
#endif
	}

	if (err < 0)
		goto cleanup;
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235

	err = parse_mtd_partitions(info->mtd, probes, &info->parts, 0);
	if (err > 0) {
		err = add_mtd_partitions(info->mtd, info->parts, err);
		if (err)
			printk(KERN_ERR
			       "mtd partition registration failed: %d\n", err);
	}

236
	if (err == 0) {
237
		platform_set_drvdata(dev, info);
238 239
		return err;
	}
L
Linus Torvalds 已提交
240 241

	/*
242
	 * We got an error, free all resources.
L
Linus Torvalds 已提交
243
	 */
244 245 246 247 248 249 250
 cleanup:
	if (info->mtd) {
		del_mtd_partitions(info->mtd);
#ifdef CONFIG_MTD_CONCAT
		if (info->mtd != info->subdev[0].mtd)
			mtd_concat_destroy(info->mtd);
#endif
L
Linus Torvalds 已提交
251
	}
252 253 254 255 256 257 258 259
	kfree(info->parts);
 subdev_err:
	for (i = info->nr_subdev - 1; i >= 0; i--)
		armflash_subdev_remove(&info->subdev[i]);
 no_resource:
	if (plat && plat->exit)
		plat->exit();
	kfree(info);
L
Linus Torvalds 已提交
260 261 262 263
 out:
	return err;
}

264
static int armflash_remove(struct platform_device *dev)
L
Linus Torvalds 已提交
265
{
266
	struct armflash_info *info = platform_get_drvdata(dev);
267 268
	struct flash_platform_data *plat = dev->dev.platform_data;
	int i;
L
Linus Torvalds 已提交
269

270
	platform_set_drvdata(dev, NULL);
L
Linus Torvalds 已提交
271 272 273 274

	if (info) {
		if (info->mtd) {
			del_mtd_partitions(info->mtd);
275 276 277 278
#ifdef CONFIG_MTD_CONCAT
			if (info->mtd != info->subdev[0].mtd)
				mtd_concat_destroy(info->mtd);
#endif
L
Linus Torvalds 已提交
279
		}
J
Jesper Juhl 已提交
280
		kfree(info->parts);
L
Linus Torvalds 已提交
281

282 283
		for (i = info->nr_subdev - 1; i >= 0; i--)
			armflash_subdev_remove(&info->subdev[i]);
L
Linus Torvalds 已提交
284

285 286
		if (plat && plat->exit)
			plat->exit();
L
Linus Torvalds 已提交
287 288 289 290 291 292 293

		kfree(info);
	}

	return 0;
}

294
static struct platform_driver armflash_driver = {
L
Linus Torvalds 已提交
295 296
	.probe		= armflash_probe,
	.remove		= armflash_remove,
297 298
	.driver		= {
		.name	= "armflash",
299
		.owner	= THIS_MODULE,
300
	},
L
Linus Torvalds 已提交
301 302 303 304
};

static int __init armflash_init(void)
{
305
	return platform_driver_register(&armflash_driver);
L
Linus Torvalds 已提交
306 307 308 309
}

static void __exit armflash_exit(void)
{
310
	platform_driver_unregister(&armflash_driver);
L
Linus Torvalds 已提交
311 312 313 314 315 316 317 318
}

module_init(armflash_init);
module_exit(armflash_exit);

MODULE_AUTHOR("ARM Ltd");
MODULE_DESCRIPTION("ARM Integrator CFI map driver");
MODULE_LICENSE("GPL");
319
MODULE_ALIAS("platform:armflash");