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