summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/maps/integrator-flash.c
blob: 2aac41bde8b314feda6bf213472c76277881c6ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*======================================================================

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

    Copyright (C) 2000 ARM Limited
    Copyright (C) 2003 Deep Blue Solutions Ltd.

   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

   This is access code for flashes using ARM's flash partitioning
   standards.

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

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/io.h>

#include <linux/mtd/mtd.h>
#include <linux/mtd/map.h>
#include <linux/mtd/partitions.h>
#include <linux/mtd/concat.h>

#include <asm/mach/flash.h>
#include <mach/hardware.h>
#include <asm/system.h>

struct armflash_subdev_info {
	char			*name;
	struct mtd_info		*mtd;
	struct map_info		map;
	struct flash_platform_data *plat;
};

struct armflash_info {
	struct resource		*res;
	struct mtd_partition	*parts;
	struct mtd_info		*mtd;
	int			nr_subdev;
	struct armflash_subdev_info subdev[0];
};

static void armflash_set_vpp(struct map_info *map, int on)
{
	struct armflash_subdev_info *info =
		container_of(map, struct armflash_subdev_info, map);

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

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

static int armflash_subdev_probe(struct armflash_subdev_info *subdev,
				 struct resource *res)
{
	struct flash_platform_data *plat = subdev->plat;
	resource_size_t size = res->end - res->start + 1;
	void __iomem *base;
	int err = 0;

	if (!request_mem_region(res->start, size, subdev->name)) {
		err = -EBUSY;
		goto out;
	}

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

	/*
	 * look for CFI based flash parts fitted to this board
	 */
	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;

	simple_map_init(&subdev->map);

	/*
	 * Also, the CFI layer automatically works out what size
	 * of chips we have, and does the necessary identification
	 * for us automatically.
	 */
	subdev->mtd = do_map_probe(plat->map_name, &subdev->map);
	if (!subdev->mtd) {
		err = -ENXIO;
		goto no_device;
	}

	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);
	kfree(subdev->name);
	subdev->name = NULL;
	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 */
			subdev->name = kstrdup(dev_name(&dev->dev), GFP_KERNEL);
		else
			subdev->name = kasprintf(GFP_KERNEL, "%s-%d",
						 dev_name(&dev->dev), i);
		if (!subdev->name) {
			err = -ENOMEM;
			break;
		}
		subdev->plat = plat;

		err = armflash_subdev_probe(subdev, res);
		if (err) {
			kfree(subdev->name);
			subdev->name = NULL;
			break;
		}
	}
	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;

	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);
	}

	if (err == 0) {
		platform_set_drvdata(dev, info);
		return err;
	}

	/*
	 * We got an error, free all resources.
	 */
 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
	}
	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);
 out:
	return err;
}

static int armflash_remove(struct platform_device *dev)
{
	struct armflash_info *info = platform_get_drvdata(dev);
	struct flash_platform_data *plat = dev->dev.platform_data;
	int i;

	platform_set_drvdata(dev, NULL);

	if (info) {
		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
		}
		kfree(info->parts);

		for (i = info->nr_subdev - 1; i >= 0; i--)
			armflash_subdev_remove(&info->subdev[i]);

		if (plat && plat->exit)
			plat->exit();

		kfree(info);
	}

	return 0;
}

static struct platform_driver armflash_driver = {
	.probe		= armflash_probe,
	.remove		= armflash_remove,
	.driver		= {
		.name	= "armflash",
		.owner	= THIS_MODULE,
	},
};

static int __init armflash_init(void)
{
	return platform_driver_register(&armflash_driver);
}

static void __exit armflash_exit(void)
{
	platform_driver_unregister(&armflash_driver);
}

module_init(armflash_init);
module_exit(armflash_exit);

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