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