2 * Physical mapping layer for MTD using the Axis partitiontable format
4 * Copyright (c) 2001, 2002 Axis Communications AB
6 * This file is under the GPL.
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.
13 * $Log: axisflashmap.c,v $
14 * Revision 1.8 2004/05/14 07:58:03 starvik
15 * Merge of changes from 2.4
17 * Revision 1.6 2003/07/04 08:27:37 starvik
18 * Merge of Linux 2.5.74
20 * Revision 1.5 2002/12/11 13:13:57 starvik
21 * Added arch/ to v10 specific includes
22 * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
24 * Revision 1.4 2002/11/20 11:56:10 starvik
25 * Merge of Linux 2.5.48
27 * Revision 1.3 2002/11/13 14:54:13 starvik
28 * Copied from linux 2.4
30 * Revision 1.28 2002/10/01 08:08:43 jonashg
31 * The first partition ends at the start of the partition table.
33 * Revision 1.27 2002/08/21 09:23:13 jonashg
36 * Revision 1.26 2002/08/21 08:35:20 jonashg
37 * Cosmetic change to printouts.
39 * Revision 1.25 2002/08/21 08:15:42 jonashg
40 * Made it compile even without CONFIG_MTD_CONCAT defined.
42 * Revision 1.24 2002/08/20 13:12:35 jonashg
43 * * New approach to probing. Probe cse0 and cse1 separately and (mtd)concat
45 * * Removed compile time tests concerning how the mtdram driver has been
46 * configured. The user will know about the misconfiguration at runtime
47 * instead. (The old approach made it impossible to use mtdram for anything
48 * else than RAM boot).
50 * Revision 1.23 2002/05/13 12:12:28 johana
51 * Allow compile without CONFIG_MTD_MTDRAM but warn at compiletime and
52 * be informative at runtime.
54 * Revision 1.22 2002/05/13 10:24:44 johana
55 * Added #if checks on MTDRAM CONFIG
57 * Revision 1.21 2002/05/06 16:05:20 johana
58 * Removed debug printout.
60 * Revision 1.20 2002/05/06 16:03:00 johana
61 * No more cramfs as root hack in generic code.
62 * It's handled by axisflashmap using mtdram.
64 * Revision 1.19 2002/03/15 17:10:28 bjornw
65 * Changed comment about cached access since we changed this before
67 * Revision 1.18 2002/03/05 17:06:15 jonashg
68 * Try amd_flash probe before cfi_probe since amd_flash driver can handle two
69 * (or more) flash chips of different model and the cfi driver cannot.
71 * Revision 1.17 2001/11/12 19:42:38 pkj
72 * Fixed compiler warnings.
74 * Revision 1.16 2001/11/08 11:18:58 jonashg
75 * Always read from uncached address to avoid problems with flushing
76 * cachelines after write and MTD-erase. No performance loss have been
79 * Revision 1.15 2001/10/19 12:41:04 jonashg
80 * Name of probe has changed in MTD.
82 * Revision 1.14 2001/09/21 07:14:10 jonashg
83 * Made root filesystem (cramfs) use mtdblock driver when booting from flash.
85 * Revision 1.13 2001/08/15 13:57:35 jonashg
86 * Entire MTD updated to the linux 2.4.7 version.
88 * Revision 1.12 2001/06/11 09:50:30 jonashg
89 * Oops, 2MB is 0x200000 bytes.
91 * Revision 1.11 2001/06/08 11:39:44 jonashg
92 * Changed sizes and offsets in axis_default_partitions to use
93 * CONFIG_ETRAX_PTABLE_SECTOR.
95 * Revision 1.10 2001/05/29 09:42:03 jonashg
96 * Use macro for end marker length instead of sizeof.
98 * Revision 1.9 2001/05/29 08:52:52 jonashg
99 * Gave names to the magic fours (size of the ptable end marker).
101 * Revision 1.8 2001/05/28 15:36:20 jonashg
102 * * Removed old comment about ptable location in flash (it's a CONFIG_ option).
103 * * Variable ptable was initialized twice to the same value.
105 * Revision 1.7 2001/04/05 13:41:46 markusl
106 * Updated according to review remarks
108 * Revision 1.6 2001/03/07 09:21:21 bjornw
109 * No need to waste .data
111 * Revision 1.5 2001/03/06 16:27:01 jonashg
112 * Probe the entire flash area for flash devices.
114 * Revision 1.4 2001/02/23 12:47:15 bjornw
115 * Uncached flash in LOW_MAP moved from 0xe to 0x8
117 * Revision 1.3 2001/02/16 12:11:45 jonashg
118 * MTD driver amd_flash is now included in MTD CVS repository.
119 * (It's now in drivers/mtd).
121 * Revision 1.2 2001/02/09 11:12:22 jonashg
122 * Support for AMD compatible non-CFI flash chips.
123 * Only tested with Toshiba TC58FVT160 so far.
125 * Revision 1.1 2001/01/12 17:01:18 bjornw
126 * * Added axisflashmap.c, a physical mapping for MTD that reads and understands
127 * Axis partition-table format.
132 #include <linux/module.h>
133 #include <linux/types.h>
134 #include <linux/kernel.h>
135 #include <linux/config.h>
136 #include <linux/init.h>
138 #include <linux/mtd/concat.h>
139 #include <linux/mtd/map.h>
140 #include <linux/mtd/mtd.h>
141 #include <linux/mtd/mtdram.h>
142 #include <linux/mtd/partitions.h>
144 #include <asm/axisflashmap.h>
146 #include <asm/arch/sv_addr_ag.h>
148 #ifdef CONFIG_CRIS_LOW_MAP
149 #define FLASH_UNCACHED_ADDR KSEG_8
150 #define FLASH_CACHED_ADDR KSEG_5
152 #define FLASH_UNCACHED_ADDR KSEG_E
153 #define FLASH_CACHED_ADDR KSEG_F
157 extern unsigned long romfs_start, romfs_length, romfs_in_flash;
159 /* The master mtd for the entire flash. */
160 struct mtd_info* axisflash_mtd = NULL;
162 /* Map driver functions. */
164 static __u8 flash_read8(struct map_info *map, unsigned long ofs)
166 return *(__u8 *)(map->map_priv_1 + ofs);
169 static __u16 flash_read16(struct map_info *map, unsigned long ofs)
171 return *(__u16 *)(map->map_priv_1 + ofs);
174 static __u32 flash_read32(struct map_info *map, unsigned long ofs)
176 return *(volatile unsigned int *)(map->map_priv_1 + ofs);
179 static void flash_copy_from(struct map_info *map, void *to,
180 unsigned long from, ssize_t len)
182 memcpy(to, (void *)(map->map_priv_1 + from), len);
185 static void flash_write8(struct map_info *map, __u8 d, unsigned long adr)
187 *(__u8 *)(map->map_priv_1 + adr) = d;
190 static void flash_write16(struct map_info *map, __u16 d, unsigned long adr)
192 *(__u16 *)(map->map_priv_1 + adr) = d;
195 static void flash_write32(struct map_info *map, __u32 d, unsigned long adr)
197 *(__u32 *)(map->map_priv_1 + adr) = d;
201 * The map for chip select e0.
203 * We run into tricky coherence situations if we mix cached with uncached
204 * accesses to we only use the uncached version here.
206 * The size field is the total size where the flash chips may be mapped on the
207 * chip select. MTD probes should find all devices there and it does not matter
208 * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
209 * probes will ignore them.
211 * The start address in map_priv_1 is in virtual memory so we cannot use
212 * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
215 static struct map_info map_cse0 = {
217 .size = MEM_CSE0_SIZE,
218 .buswidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
219 .read8 = flash_read8,
220 .read16 = flash_read16,
221 .read32 = flash_read32,
222 .copy_from = flash_copy_from,
223 .write8 = flash_write8,
224 .write16 = flash_write16,
225 .write32 = flash_write32,
226 .map_priv_1 = FLASH_UNCACHED_ADDR
230 * The map for chip select e1.
232 * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
233 * address, but there isn't.
235 static struct map_info map_cse1 = {
237 .size = MEM_CSE1_SIZE,
238 .buswidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
239 .read8 = flash_read8,
240 .read16 = flash_read16,
241 .read32 = flash_read32,
242 .copy_from = flash_copy_from,
243 .write8 = flash_write8,
244 .write16 = flash_write16,
245 .write32 = flash_write32,
246 .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE
249 /* If no partition-table was found, we use this default-set. */
250 #define MAX_PARTITIONS 7
251 #define NUM_DEFAULT_PARTITIONS 3
254 * Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the
255 * size of one flash block and "filesystem"-partition needs 5 blocks to be able
258 static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {
260 .name = "boot firmware",
261 .size = CONFIG_ETRAX_PTABLE_SECTOR,
266 .size = 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR),
267 .offset = CONFIG_ETRAX_PTABLE_SECTOR
270 .name = "filesystem",
271 .size = 5 * CONFIG_ETRAX_PTABLE_SECTOR,
272 .offset = 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR)
276 /* Initialize the ones normally used. */
277 static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {
280 .size = CONFIG_ETRAX_PTABLE_SECTOR,
316 * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
317 * chips in that order (because the amd_flash-driver is faster).
319 static struct mtd_info *probe_cs(struct map_info *map_cs)
321 struct mtd_info *mtd_cs = NULL;
324 "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
325 map_cs->name, map_cs->size, map_cs->map_priv_1);
327 #ifdef CONFIG_MTD_AMDSTD
328 mtd_cs = do_map_probe("amd_flash", map_cs);
330 #ifdef CONFIG_MTD_CFI
332 mtd_cs = do_map_probe("cfi_probe", map_cs);
340 * Probe each chip select individually for flash chips. If there are chips on
341 * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
342 * so that MTD partitions can cross chip boundries.
344 * The only known restriction to how you can mount your chips is that each
345 * chip select must hold similar flash chips. But you need external hardware
346 * to do that anyway and you can put totally different chips on cse0 and cse1
347 * so it isn't really much of a restriction.
349 static struct mtd_info *flash_probe(void)
351 struct mtd_info *mtd_cse0;
352 struct mtd_info *mtd_cse1;
353 struct mtd_info *mtd_cse;
355 mtd_cse0 = probe_cs(&map_cse0);
356 mtd_cse1 = probe_cs(&map_cse1);
358 if (!mtd_cse0 && !mtd_cse1) {
363 if (mtd_cse0 && mtd_cse1) {
364 #ifdef CONFIG_MTD_CONCAT
365 struct mtd_info *mtds[] = { mtd_cse0, mtd_cse1 };
367 /* Since the concatenation layer adds a small overhead we
368 * could try to figure out if the chips in cse0 and cse1 are
369 * identical and reprobe the whole cse0+cse1 window. But since
370 * flash chips are slow, the overhead is relatively small.
371 * So we use the MTD concatenation layer instead of further
372 * complicating the probing procedure.
374 mtd_cse = mtd_concat_create(mtds,
375 sizeof(mtds) / sizeof(mtds[0]),
378 printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel "
379 "(mis)configuration!\n", map_cse0.name, map_cse1.name);
383 printk(KERN_ERR "%s and %s: Concatenation failed!\n",
384 map_cse0.name, map_cse1.name);
386 /* The best we can do now is to only use what we found
390 map_destroy(mtd_cse1);
393 mtd_cse = mtd_cse0? mtd_cse0 : mtd_cse1;
400 * Probe the flash chip(s) and, if it succeeds, read the partition-table
401 * and register the partitions with MTD.
403 static int __init init_axis_flash(void)
405 struct mtd_info *mymtd;
408 struct partitiontable_head *ptable_head = NULL;
409 struct partitiontable_entry *ptable;
410 int use_default_ptable = 1; /* Until proven otherwise. */
411 const char *pmsg = " /dev/flash%d at 0x%08x, size 0x%08x\n";
413 if (!(mymtd = flash_probe())) {
414 /* There's no reason to use this module if no flash chip can
415 * be identified. Make sure that's understood.
417 printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
419 printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
420 mymtd->name, mymtd->size);
421 axisflash_mtd = mymtd;
425 mymtd->owner = THIS_MODULE;
426 ptable_head = (struct partitiontable_head *)(FLASH_CACHED_ADDR +
427 CONFIG_ETRAX_PTABLE_SECTOR +
428 PARTITION_TABLE_OFFSET);
430 pidx++; /* First partition is always set to the default. */
432 if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
433 && (ptable_head->size <
434 (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
435 PARTITIONTABLE_END_MARKER_SIZE))
436 && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
438 PARTITIONTABLE_END_MARKER_SIZE)
439 == PARTITIONTABLE_END_MARKER)) {
440 /* Looks like a start, sane length and end of a
441 * partition table, lets check csum etc.
444 struct partitiontable_entry *max_addr =
445 (struct partitiontable_entry *)
446 ((unsigned long)ptable_head + sizeof(*ptable_head) +
448 unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
450 unsigned long csum = 0;
452 ptable = (struct partitiontable_entry *)
453 ((unsigned long)ptable_head + sizeof(*ptable_head));
455 /* Lets be PARANOID, and check the checksum. */
456 p = (unsigned char*) ptable;
458 while (p <= (unsigned char*)max_addr) {
464 ptable_ok = (csum == ptable_head->checksum);
466 /* Read the entries and use/show the info. */
467 printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
468 (ptable_ok ? " valid" : "n invalid"), ptable_head,
471 /* We have found a working bootblock. Now read the
472 * partition table. Scan the table. It ends when
473 * there is 0xffffffff, that is, empty flash.
476 && ptable->offset != 0xffffffff
478 && pidx < MAX_PARTITIONS) {
480 axis_partitions[pidx].offset = offset + ptable->offset;
481 axis_partitions[pidx].size = ptable->size;
483 printk(pmsg, pidx, axis_partitions[pidx].offset,
484 axis_partitions[pidx].size);
488 use_default_ptable = !ptable_ok;
491 if (romfs_in_flash) {
492 /* Add an overlapping device for the root partition (romfs). */
494 axis_partitions[pidx].name = "romfs";
495 axis_partitions[pidx].size = romfs_length;
496 axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
497 axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
500 " Adding readonly flash partition for romfs image:\n");
501 printk(pmsg, pidx, axis_partitions[pidx].offset,
502 axis_partitions[pidx].size);
507 if (use_default_ptable) {
508 printk(KERN_INFO " Using default partition table.\n");
509 err = add_mtd_partitions(mymtd, axis_default_partitions,
510 NUM_DEFAULT_PARTITIONS);
512 err = add_mtd_partitions(mymtd, axis_partitions, pidx);
516 panic("axisflashmap could not add MTD partitions!\n");
520 if (!romfs_in_flash) {
521 /* Create an RAM device for the root partition (romfs). */
523 #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
524 /* No use trying to boot this kernel from RAM. Panic! */
525 printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
526 "device due to kernel (mis)configuration!\n");
527 panic("This kernel cannot boot from RAM!\n");
529 struct mtd_info *mtd_ram;
531 mtd_ram = (struct mtd_info *)kmalloc(sizeof(struct mtd_info),
534 panic("axisflashmap couldn't allocate memory for "
538 printk(KERN_INFO " Adding RAM partition for romfs image:\n");
539 printk(pmsg, pidx, romfs_start, romfs_length);
541 err = mtdram_init_device(mtd_ram, (void*)romfs_start,
542 romfs_length, "romfs");
544 panic("axisflashmap could not initialize MTD RAM "
553 /* This adds the above to the kernels init-call chain. */
554 module_init(init_axis_flash);
556 EXPORT_SYMBOL(axisflash_mtd);