1/*
2 * Ceiva flash memory driver.
3 * Copyright (C) 2002 Rob Scott <rscott@mtrob.fdns.net>
4 *
5 * Note: this driver supports jedec compatible devices. Modification
6 * for CFI compatible devices should be straight forward: change
7 * jedec_probe to cfi_probe.
8 *
9 * Based on: sa1100-flash.c, which has the following copyright:
10 * Flash memory access on SA11x0 based devices
11 *
12 * (C) 2000 Nicolas Pitre <nico@cam.org>
13 *
14 * $Id: ceiva.c,v 1.1.1.1 2007/08/03 18:52:43 Exp $
15 */
16
17#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/ioport.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/slab.h>
23
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/map.h>
26#include <linux/mtd/partitions.h>
27#include <linux/mtd/concat.h>
28
29#include <asm/hardware.h>
30#include <asm/mach-types.h>
31#include <asm/io.h>
32#include <asm/sizes.h>
33
34/*
35 * This isn't complete yet, so...
36 */
37#define CONFIG_MTD_CEIVA_STATICMAP
38
39#ifdef CONFIG_MTD_CEIVA_STATICMAP
40/*
41 * See include/linux/mtd/partitions.h for definition of the mtd_partition
42 * structure.
43 *
44 * Please note:
45 *  1. The flash size given should be the largest flash size that can
46 *     be accomodated.
47 *
48 *  2. The bus width must defined in clps_setup_flash.
49 *
50 * The MTD layer will detect flash chip aliasing and reduce the size of
51 * the map accordingly.
52 *
53 */
54
55#ifdef CONFIG_ARCH_CEIVA
56/* Flash / Partition sizing */
57/* For the 28F8003, we use the block mapping to calcuate the sizes */
58#define MAX_SIZE_KiB                  (16 + 8 + 8 + 96 + (7*128))
59#define BOOT_PARTITION_SIZE_KiB       (16)
60#define PARAMS_PARTITION_SIZE_KiB     (8)
61#define KERNEL_PARTITION_SIZE_KiB     (4*128)
62/* Use both remaing portion of first flash, and all of second flash */
63#define ROOT_PARTITION_SIZE_KiB       (3*128) + (8*128)
64
65static struct mtd_partition ceiva_partitions[] = {
66	{
67		.name = "Ceiva BOOT partition",
68		.size   = BOOT_PARTITION_SIZE_KiB*1024,
69		.offset = 0,
70
71	},{
72		.name = "Ceiva parameters partition",
73		.size   = PARAMS_PARTITION_SIZE_KiB*1024,
74		.offset = (16 + 8) * 1024,
75	},{
76		.name = "Ceiva kernel partition",
77		.size = (KERNEL_PARTITION_SIZE_KiB)*1024,
78		.offset = 0x20000,
79
80	},{
81		.name = "Ceiva root filesystem partition",
82		.offset = MTDPART_OFS_APPEND,
83		.size = (ROOT_PARTITION_SIZE_KiB)*1024,
84	}
85};
86#endif
87
88static int __init clps_static_partitions(struct mtd_partition **parts)
89{
90	int nb_parts = 0;
91
92#ifdef CONFIG_ARCH_CEIVA
93	if (machine_is_ceiva()) {
94		*parts       = ceiva_partitions;
95		nb_parts     = ARRAY_SIZE(ceiva_partitions);
96	}
97#endif
98	return nb_parts;
99}
100#endif
101
102struct clps_info {
103	unsigned long base;
104	unsigned long size;
105	int width;
106	void *vbase;
107	struct map_info *map;
108	struct mtd_info *mtd;
109	struct resource *res;
110};
111
112#define NR_SUBMTD 4
113
114static struct clps_info info[NR_SUBMTD];
115
116static int __init clps_setup_mtd(struct clps_info *clps, int nr, struct mtd_info **rmtd)
117{
118	struct mtd_info *subdev[nr];
119	struct map_info *maps;
120	int i, found = 0, ret = 0;
121
122	/*
123	 * Allocate the map_info structs in one go.
124	 */
125	maps = kzalloc(sizeof(struct map_info) * nr, GFP_KERNEL);
126	if (!maps)
127		return -ENOMEM;
128	/*
129	 * Claim and then map the memory regions.
130	 */
131	for (i = 0; i < nr; i++) {
132		if (clps[i].base == (unsigned long)-1)
133			break;
134
135		clps[i].res = request_mem_region(clps[i].base, clps[i].size, "clps flash");
136		if (!clps[i].res) {
137			ret = -EBUSY;
138			break;
139		}
140
141		clps[i].map = maps + i;
142
143		clps[i].map->name = "clps flash";
144		clps[i].map->phys = clps[i].base;
145
146		clps[i].vbase = ioremap(clps[i].base, clps[i].size);
147		if (!clps[i].vbase) {
148			ret = -ENOMEM;
149			break;
150		}
151
152		clps[i].map->virt = (void __iomem *)clps[i].vbase;
153		clps[i].map->bankwidth = clps[i].width;
154		clps[i].map->size = clps[i].size;
155
156		simple_map_init(&clps[i].map);
157
158		clps[i].mtd = do_map_probe("jedec_probe", clps[i].map);
159		if (clps[i].mtd == NULL) {
160			ret = -ENXIO;
161			break;
162		}
163		clps[i].mtd->owner = THIS_MODULE;
164		subdev[i] = clps[i].mtd;
165
166		printk(KERN_INFO "clps flash: JEDEC device at 0x%08lx, %dMiB, "
167			"%d-bit\n", clps[i].base, clps[i].mtd->size >> 20,
168			clps[i].width * 8);
169		found += 1;
170	}
171
172	/*
173	 * ENXIO is special.  It means we didn't find a chip when
174	 * we probed.  We need to tear down the mapping, free the
175	 * resource and mark it as such.
176	 */
177	if (ret == -ENXIO) {
178		iounmap(clps[i].vbase);
179		clps[i].vbase = NULL;
180		release_resource(clps[i].res);
181		clps[i].res = NULL;
182	}
183
184	/*
185	 * If we found one device, don't bother with concat support.
186	 * If we found multiple devices, use concat if we have it
187	 * available, otherwise fail.
188	 */
189	if (ret == 0 || ret == -ENXIO) {
190		if (found == 1) {
191			*rmtd = subdev[0];
192			ret = 0;
193		} else if (found > 1) {
194			/*
195			 * We detected multiple devices.  Concatenate
196			 * them together.
197			 */
198#ifdef CONFIG_MTD_CONCAT
199			*rmtd = mtd_concat_create(subdev, found,
200						  "clps flash");
201			if (*rmtd == NULL)
202				ret = -ENXIO;
203#else
204			printk(KERN_ERR "clps flash: multiple devices "
205			       "found but MTD concat support disabled.\n");
206			ret = -ENXIO;
207#endif
208		}
209	}
210
211	/*
212	 * If we failed, clean up.
213	 */
214	if (ret) {
215		do {
216			if (clps[i].mtd)
217				map_destroy(clps[i].mtd);
218			if (clps[i].vbase)
219				iounmap(clps[i].vbase);
220			if (clps[i].res)
221				release_resource(clps[i].res);
222		} while (i--);
223
224		kfree(maps);
225	}
226
227	return ret;
228}
229
230static void __exit clps_destroy_mtd(struct clps_info *clps, struct mtd_info *mtd)
231{
232	int i;
233
234	del_mtd_partitions(mtd);
235
236	if (mtd != clps[0].mtd)
237		mtd_concat_destroy(mtd);
238
239	for (i = NR_SUBMTD; i >= 0; i--) {
240		if (clps[i].mtd)
241			map_destroy(clps[i].mtd);
242		if (clps[i].vbase)
243			iounmap(clps[i].vbase);
244		if (clps[i].res)
245			release_resource(clps[i].res);
246	}
247	kfree(clps[0].map);
248}
249
250/*
251 * We define the memory space, size, and width for the flash memory
252 * space here.
253 */
254
255static int __init clps_setup_flash(void)
256{
257	int nr;
258
259#ifdef CONFIG_ARCH_CEIVA
260	if (machine_is_ceiva()) {
261		info[0].base = CS0_PHYS_BASE;
262		info[0].size = SZ_32M;
263		info[0].width = CEIVA_FLASH_WIDTH;
264		info[1].base = CS1_PHYS_BASE;
265		info[1].size = SZ_32M;
266		info[1].width = CEIVA_FLASH_WIDTH;
267		nr = 2;
268	}
269#endif
270	return nr;
271}
272
273static struct mtd_partition *parsed_parts;
274static const char *probes[] = { "cmdlinepart", "RedBoot", NULL };
275
276static void __init clps_locate_partitions(struct mtd_info *mtd)
277{
278	const char *part_type = NULL;
279	int nr_parts = 0;
280	do {
281		/*
282		 * Partition selection stuff.
283		 */
284		nr_parts = parse_mtd_partitions(mtd, probes, &parsed_parts, 0);
285		if (nr_parts > 0) {
286			part_type = "command line";
287			break;
288		}
289#ifdef CONFIG_MTD_CEIVA_STATICMAP
290		nr_parts = clps_static_partitions(&parsed_parts);
291		if (nr_parts > 0) {
292			part_type = "static";
293			break;
294		}
295		printk("found: %d partitions\n", nr_parts);
296#endif
297	} while (0);
298
299	if (nr_parts == 0) {
300		printk(KERN_NOTICE "clps flash: no partition info "
301			"available, registering whole flash\n");
302		add_mtd_device(mtd);
303	} else {
304		printk(KERN_NOTICE "clps flash: using %s partition "
305			"definition\n", part_type);
306		add_mtd_partitions(mtd, parsed_parts, nr_parts);
307	}
308
309	/* Always succeeds. */
310}
311
312static void __exit clps_destroy_partitions(void)
313{
314	kfree(parsed_parts);
315}
316
317static struct mtd_info *mymtd;
318
319static int __init clps_mtd_init(void)
320{
321	int ret;
322	int nr;
323
324	nr = clps_setup_flash();
325	if (nr < 0)
326		return nr;
327
328	ret = clps_setup_mtd(info, nr, &mymtd);
329	if (ret)
330		return ret;
331
332	clps_locate_partitions(mymtd);
333
334	return 0;
335}
336
337static void __exit clps_mtd_cleanup(void)
338{
339	clps_destroy_mtd(info, mymtd);
340	clps_destroy_partitions();
341}
342
343module_init(clps_mtd_init);
344module_exit(clps_mtd_cleanup);
345
346MODULE_AUTHOR("Rob Scott");
347MODULE_DESCRIPTION("Cirrus Logic JEDEC map driver");
348MODULE_LICENSE("GPL");
349