Linux-2.6.12-rc2
[safe/jmp/linux-2.6] / arch / cris / arch-v10 / drivers / axisflashmap.c
1 /*
2  * Physical mapping layer for MTD using the Axis partitiontable format
3  *
4  * Copyright (c) 2001, 2002 Axis Communications AB
5  *
6  * This file is under the GPL.
7  *
8  * First partition is always sector 0 regardless of if we find a partitiontable
9  * or not. In the start of the next sector, there can be a partitiontable that
10  * tells us what other partitions to define. If there isn't, we use a default
11  * partition split defined below.
12  *
13  * $Log: axisflashmap.c,v $
14  * Revision 1.10  2004/08/16 12:37:22  starvik
15  * Merge of Linux 2.6.8
16  *
17  * Revision 1.8  2004/05/14 07:58:03  starvik
18  * Merge of changes from 2.4
19  *
20  * Revision 1.6  2003/07/04 08:27:37  starvik
21  * Merge of Linux 2.5.74
22  *
23  * Revision 1.5  2002/12/11 13:13:57  starvik
24  * Added arch/ to v10 specific includes
25  * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
26  *
27  * Revision 1.4  2002/11/20 11:56:10  starvik
28  * Merge of Linux 2.5.48
29  *
30  * Revision 1.3  2002/11/13 14:54:13  starvik
31  * Copied from linux 2.4
32  *
33  * Revision 1.28  2002/10/01 08:08:43  jonashg
34  * The first partition ends at the start of the partition table.
35  *
36  * Revision 1.27  2002/08/21 09:23:13  jonashg
37  * Speling.
38  *
39  * Revision 1.26  2002/08/21 08:35:20  jonashg
40  * Cosmetic change to printouts.
41  *
42  * Revision 1.25  2002/08/21 08:15:42  jonashg
43  * Made it compile even without CONFIG_MTD_CONCAT defined.
44  *
45  * Revision 1.24  2002/08/20 13:12:35  jonashg
46  * * New approach to probing. Probe cse0 and cse1 separately and (mtd)concat
47  *   the results.
48  * * Removed compile time tests concerning how the mtdram driver has been
49  *   configured. The user will know about the misconfiguration at runtime
50  *   instead. (The old approach made it impossible to use mtdram for anything
51  *   else than RAM boot).
52  *
53  * Revision 1.23  2002/05/13 12:12:28  johana
54  * Allow compile without CONFIG_MTD_MTDRAM but warn at compiletime and
55  * be informative at runtime.
56  *
57  * Revision 1.22  2002/05/13 10:24:44  johana
58  * Added #if checks on MTDRAM CONFIG
59  *
60  * Revision 1.21  2002/05/06 16:05:20  johana
61  * Removed debug printout.
62  *
63  * Revision 1.20  2002/05/06 16:03:00  johana
64  * No more cramfs as root hack in generic code.
65  * It's handled by axisflashmap using mtdram.
66  *
67  * Revision 1.19  2002/03/15 17:10:28  bjornw
68  * Changed comment about cached access since we changed this before
69  *
70  * Revision 1.18  2002/03/05 17:06:15  jonashg
71  * Try amd_flash probe before cfi_probe since amd_flash driver can handle two
72  * (or more) flash chips of different model and the cfi driver cannot.
73  *
74  * Revision 1.17  2001/11/12 19:42:38  pkj
75  * Fixed compiler warnings.
76  *
77  * Revision 1.16  2001/11/08 11:18:58  jonashg
78  * Always read from uncached address to avoid problems with flushing
79  * cachelines after write and MTD-erase. No performance loss have been
80  * seen yet.
81  *
82  * Revision 1.15  2001/10/19 12:41:04  jonashg
83  * Name of probe has changed in MTD.
84  *
85  * Revision 1.14  2001/09/21 07:14:10  jonashg
86  * Made root filesystem (cramfs) use mtdblock driver when booting from flash.
87  *
88  * Revision 1.13  2001/08/15 13:57:35  jonashg
89  * Entire MTD updated to the linux 2.4.7 version.
90  *
91  * Revision 1.12  2001/06/11 09:50:30  jonashg
92  * Oops, 2MB is 0x200000 bytes.
93  *
94  * Revision 1.11  2001/06/08 11:39:44  jonashg
95  * Changed sizes and offsets in axis_default_partitions to use
96  * CONFIG_ETRAX_PTABLE_SECTOR.
97  *
98  * Revision 1.10  2001/05/29 09:42:03  jonashg
99  * Use macro for end marker length instead of sizeof.
100  *
101  * Revision 1.9  2001/05/29 08:52:52  jonashg
102  * Gave names to the magic fours (size of the ptable end marker).
103  *
104  * Revision 1.8  2001/05/28 15:36:20  jonashg
105  * * Removed old comment about ptable location in flash (it's a CONFIG_ option).
106  * * Variable ptable was initialized twice to the same value.
107  *
108  * Revision 1.7  2001/04/05 13:41:46  markusl
109  * Updated according to review remarks
110  *
111  * Revision 1.6  2001/03/07 09:21:21  bjornw
112  * No need to waste .data
113  *
114  * Revision 1.5  2001/03/06 16:27:01  jonashg
115  * Probe the entire flash area for flash devices.
116  *
117  * Revision 1.4  2001/02/23 12:47:15  bjornw
118  * Uncached flash in LOW_MAP moved from 0xe to 0x8
119  *
120  * Revision 1.3  2001/02/16 12:11:45  jonashg
121  * MTD driver amd_flash is now included in MTD CVS repository.
122  * (It's now in drivers/mtd).
123  *
124  * Revision 1.2  2001/02/09 11:12:22  jonashg
125  * Support for AMD compatible non-CFI flash chips.
126  * Only tested with Toshiba TC58FVT160 so far.
127  *
128  * Revision 1.1  2001/01/12 17:01:18  bjornw
129  * * Added axisflashmap.c, a physical mapping for MTD that reads and understands
130  *   Axis partition-table format.
131  *
132  *
133  */
134
135 #include <linux/module.h>
136 #include <linux/types.h>
137 #include <linux/kernel.h>
138 #include <linux/config.h>
139 #include <linux/init.h>
140
141 #include <linux/mtd/concat.h>
142 #include <linux/mtd/map.h>
143 #include <linux/mtd/mtd.h>
144 #include <linux/mtd/mtdram.h>
145 #include <linux/mtd/partitions.h>
146
147 #include <asm/axisflashmap.h>
148 #include <asm/mmu.h>
149 #include <asm/arch/sv_addr_ag.h>
150
151 #ifdef CONFIG_CRIS_LOW_MAP
152 #define FLASH_UNCACHED_ADDR  KSEG_8
153 #define FLASH_CACHED_ADDR    KSEG_5
154 #else
155 #define FLASH_UNCACHED_ADDR  KSEG_E
156 #define FLASH_CACHED_ADDR    KSEG_F
157 #endif
158
159 #if CONFIG_ETRAX_FLASH_BUSWIDTH==1
160 #define flash_data __u8
161 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
162 #define flash_data __u16
163 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
164 #define flash_data __u16
165 #endif
166
167 /* From head.S */
168 extern unsigned long romfs_start, romfs_length, romfs_in_flash;
169
170 /* The master mtd for the entire flash. */
171 struct mtd_info* axisflash_mtd = NULL;
172
173 /* Map driver functions. */
174
175 static map_word flash_read(struct map_info *map, unsigned long ofs)
176 {
177         map_word tmp;
178         tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);
179         return tmp;
180 }
181
182 static void flash_copy_from(struct map_info *map, void *to,
183                             unsigned long from, ssize_t len)
184 {
185         memcpy(to, (void *)(map->map_priv_1 + from), len);
186 }
187
188 static void flash_write(struct map_info *map, map_word d, unsigned long adr)
189 {
190         *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];
191 }
192
193 /*
194  * The map for chip select e0.
195  *
196  * We run into tricky coherence situations if we mix cached with uncached
197  * accesses to we only use the uncached version here.
198  *
199  * The size field is the total size where the flash chips may be mapped on the
200  * chip select. MTD probes should find all devices there and it does not matter
201  * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
202  * probes will ignore them.
203  *
204  * The start address in map_priv_1 is in virtual memory so we cannot use
205  * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
206  * address of cse0.
207  */
208 static struct map_info map_cse0 = {
209         .name = "cse0",
210         .size = MEM_CSE0_SIZE,
211         .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
212         .read = flash_read,
213         .copy_from = flash_copy_from,
214         .write = flash_write,
215         .map_priv_1 = FLASH_UNCACHED_ADDR
216 };
217
218 /*
219  * The map for chip select e1.
220  *
221  * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
222  * address, but there isn't.
223  */
224 static struct map_info map_cse1 = {
225         .name = "cse1",
226         .size = MEM_CSE1_SIZE,
227         .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
228         .read = flash_read,
229         .copy_from = flash_copy_from,
230         .write = flash_write,
231         .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE
232 };
233
234 /* If no partition-table was found, we use this default-set. */
235 #define MAX_PARTITIONS         7  
236 #define NUM_DEFAULT_PARTITIONS 3
237
238 /*
239  * Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the
240  * size of one flash block and "filesystem"-partition needs 5 blocks to be able
241  * to use JFFS.
242  */
243 static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {
244         {
245                 .name = "boot firmware",
246                 .size = CONFIG_ETRAX_PTABLE_SECTOR,
247                 .offset = 0
248         },
249         {
250                 .name = "kernel",
251                 .size = 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR),
252                 .offset = CONFIG_ETRAX_PTABLE_SECTOR
253         },
254         {
255                 .name = "filesystem",
256                 .size = 5 * CONFIG_ETRAX_PTABLE_SECTOR,
257                 .offset = 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR)
258         }
259 };
260
261 /* Initialize the ones normally used. */
262 static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {
263         {
264                 .name = "part0",
265                 .size = CONFIG_ETRAX_PTABLE_SECTOR,
266                 .offset = 0
267         },
268         {
269                 .name = "part1",
270                 .size = 0,
271                 .offset = 0
272         },
273         {
274                 .name = "part2",
275                 .size = 0,
276                 .offset = 0
277         },
278         {
279                 .name = "part3",
280                 .size = 0,
281                 .offset = 0
282         },
283         {
284                 .name = "part4",
285                 .size = 0,
286                 .offset = 0
287         },
288         {
289                 .name = "part5",
290                 .size = 0,
291                 .offset = 0
292         },
293         {
294                 .name = "part6",
295                 .size = 0,
296                 .offset = 0
297         },
298 };
299
300 /*
301  * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
302  * chips in that order (because the amd_flash-driver is faster).
303  */
304 static struct mtd_info *probe_cs(struct map_info *map_cs)
305 {
306         struct mtd_info *mtd_cs = NULL;
307
308         printk(KERN_INFO
309                "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
310                map_cs->name, map_cs->size, map_cs->map_priv_1);
311
312 #ifdef CONFIG_MTD_AMDSTD
313         mtd_cs = do_map_probe("amd_flash", map_cs);
314 #endif
315 #ifdef CONFIG_MTD_CFI
316         if (!mtd_cs) {
317                 mtd_cs = do_map_probe("cfi_probe", map_cs);
318         }
319 #endif
320
321         return mtd_cs;
322 }
323
324 /* 
325  * Probe each chip select individually for flash chips. If there are chips on
326  * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
327  * so that MTD partitions can cross chip boundries.
328  *
329  * The only known restriction to how you can mount your chips is that each
330  * chip select must hold similar flash chips. But you need external hardware
331  * to do that anyway and you can put totally different chips on cse0 and cse1
332  * so it isn't really much of a restriction.
333  */
334 static struct mtd_info *flash_probe(void)
335 {
336         struct mtd_info *mtd_cse0;
337         struct mtd_info *mtd_cse1;
338         struct mtd_info *mtd_cse;
339
340         mtd_cse0 = probe_cs(&map_cse0);
341         mtd_cse1 = probe_cs(&map_cse1);
342
343         if (!mtd_cse0 && !mtd_cse1) {
344                 /* No chip found. */
345                 return NULL;
346         }
347
348         if (mtd_cse0 && mtd_cse1) {
349 #ifdef CONFIG_MTD_CONCAT
350                 struct mtd_info *mtds[] = { mtd_cse0, mtd_cse1 };
351                 
352                 /* Since the concatenation layer adds a small overhead we
353                  * could try to figure out if the chips in cse0 and cse1 are
354                  * identical and reprobe the whole cse0+cse1 window. But since
355                  * flash chips are slow, the overhead is relatively small.
356                  * So we use the MTD concatenation layer instead of further
357                  * complicating the probing procedure.
358                  */
359                 mtd_cse = mtd_concat_create(mtds,
360                                             sizeof(mtds) / sizeof(mtds[0]),
361                                             "cse0+cse1");
362 #else
363                 printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel "
364                        "(mis)configuration!\n", map_cse0.name, map_cse1.name);
365                 mtd_cse = NULL;
366 #endif
367                 if (!mtd_cse) {
368                         printk(KERN_ERR "%s and %s: Concatenation failed!\n",
369                                map_cse0.name, map_cse1.name);
370
371                         /* The best we can do now is to only use what we found
372                          * at cse0.
373                          */ 
374                         mtd_cse = mtd_cse0;
375                         map_destroy(mtd_cse1);
376                 }
377         } else {
378                 mtd_cse = mtd_cse0? mtd_cse0 : mtd_cse1;
379         }
380
381         return mtd_cse;
382 }
383
384 /*
385  * Probe the flash chip(s) and, if it succeeds, read the partition-table
386  * and register the partitions with MTD.
387  */
388 static int __init init_axis_flash(void)
389 {
390         struct mtd_info *mymtd;
391         int err = 0;
392         int pidx = 0;
393         struct partitiontable_head *ptable_head = NULL;
394         struct partitiontable_entry *ptable;
395         int use_default_ptable = 1; /* Until proven otherwise. */
396         const char *pmsg = "  /dev/flash%d at 0x%08x, size 0x%08x\n";
397
398         if (!(mymtd = flash_probe())) {
399                 /* There's no reason to use this module if no flash chip can
400                  * be identified. Make sure that's understood.
401                  */
402                 printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
403         } else {
404                 printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
405                        mymtd->name, mymtd->size);
406                 axisflash_mtd = mymtd;
407         }
408
409         if (mymtd) {
410                 mymtd->owner = THIS_MODULE;
411                 ptable_head = (struct partitiontable_head *)(FLASH_CACHED_ADDR +
412                               CONFIG_ETRAX_PTABLE_SECTOR +
413                               PARTITION_TABLE_OFFSET);
414         }
415         pidx++;  /* First partition is always set to the default. */
416
417         if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
418             && (ptable_head->size <
419                 (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
420                 PARTITIONTABLE_END_MARKER_SIZE))
421             && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
422                                   ptable_head->size -
423                                   PARTITIONTABLE_END_MARKER_SIZE)
424                 == PARTITIONTABLE_END_MARKER)) {
425                 /* Looks like a start, sane length and end of a
426                  * partition table, lets check csum etc.
427                  */
428                 int ptable_ok = 0;
429                 struct partitiontable_entry *max_addr =
430                         (struct partitiontable_entry *)
431                         ((unsigned long)ptable_head + sizeof(*ptable_head) +
432                          ptable_head->size);
433                 unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
434                 unsigned char *p;
435                 unsigned long csum = 0;
436                 
437                 ptable = (struct partitiontable_entry *)
438                         ((unsigned long)ptable_head + sizeof(*ptable_head));
439
440                 /* Lets be PARANOID, and check the checksum. */
441                 p = (unsigned char*) ptable;
442
443                 while (p <= (unsigned char*)max_addr) {
444                         csum += *p++;
445                         csum += *p++;
446                         csum += *p++;
447                         csum += *p++;
448                 }
449                 ptable_ok = (csum == ptable_head->checksum);
450
451                 /* Read the entries and use/show the info.  */
452                 printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
453                        (ptable_ok ? " valid" : "n invalid"), ptable_head,
454                        max_addr);
455
456                 /* We have found a working bootblock.  Now read the
457                  * partition table.  Scan the table.  It ends when
458                  * there is 0xffffffff, that is, empty flash.
459                  */
460                 while (ptable_ok
461                        && ptable->offset != 0xffffffff
462                        && ptable < max_addr
463                        && pidx < MAX_PARTITIONS) {
464
465                         axis_partitions[pidx].offset = offset + ptable->offset;
466                         axis_partitions[pidx].size = ptable->size;
467
468                         printk(pmsg, pidx, axis_partitions[pidx].offset,
469                                axis_partitions[pidx].size);
470                         pidx++;
471                         ptable++;
472                 }
473                 use_default_ptable = !ptable_ok;
474         }
475
476         if (romfs_in_flash) {
477                 /* Add an overlapping device for the root partition (romfs). */
478
479                 axis_partitions[pidx].name = "romfs";
480                 axis_partitions[pidx].size = romfs_length;
481                 axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
482                 axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
483
484                 printk(KERN_INFO
485                        " Adding readonly flash partition for romfs image:\n");
486                 printk(pmsg, pidx, axis_partitions[pidx].offset,
487                        axis_partitions[pidx].size);
488                 pidx++;
489         }
490
491         if (mymtd) {
492                 if (use_default_ptable) {
493                         printk(KERN_INFO " Using default partition table.\n");
494                         err = add_mtd_partitions(mymtd, axis_default_partitions,
495                                                  NUM_DEFAULT_PARTITIONS);
496                 } else {
497                         err = add_mtd_partitions(mymtd, axis_partitions, pidx);
498                 }
499
500                 if (err) {
501                         panic("axisflashmap could not add MTD partitions!\n");
502                 }
503         }
504
505         if (!romfs_in_flash) {
506                 /* Create an RAM device for the root partition (romfs). */
507
508 #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
509                 /* No use trying to boot this kernel from RAM. Panic! */
510                 printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
511                        "device due to kernel (mis)configuration!\n");
512                 panic("This kernel cannot boot from RAM!\n");
513 #else
514                 struct mtd_info *mtd_ram;
515
516                 mtd_ram = (struct mtd_info *)kmalloc(sizeof(struct mtd_info),
517                                                      GFP_KERNEL);
518                 if (!mtd_ram) {
519                         panic("axisflashmap couldn't allocate memory for "
520                               "mtd_info!\n");
521                 }
522
523                 printk(KERN_INFO " Adding RAM partition for romfs image:\n");
524                 printk(pmsg, pidx, romfs_start, romfs_length);
525
526                 err = mtdram_init_device(mtd_ram, (void*)romfs_start, 
527                                          romfs_length, "romfs");
528                 if (err) {
529                         panic("axisflashmap could not initialize MTD RAM "
530                               "device!\n");
531                 }
532 #endif
533         }
534
535         return err;
536 }
537
538 /* This adds the above to the kernels init-call chain. */
539 module_init(init_axis_flash);
540
541 EXPORT_SYMBOL(axisflash_mtd);