2 * Physical mapping layer for MTD using the Axis partitiontable format
4 * Copyright (c) 2001-2007 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.
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/slab.h>
21 #include <linux/mtd/concat.h>
22 #include <linux/mtd/map.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/mtdram.h>
25 #include <linux/mtd/partitions.h>
27 #include <asm/axisflashmap.h>
30 #define MEM_CSE0_SIZE (0x04000000)
31 #define MEM_CSE1_SIZE (0x04000000)
33 #define FLASH_UNCACHED_ADDR KSEG_E
34 #define FLASH_CACHED_ADDR KSEG_F
36 #define PAGESIZE (512)
38 #if CONFIG_ETRAX_FLASH_BUSWIDTH==1
39 #define flash_data __u8
40 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
41 #define flash_data __u16
42 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
43 #define flash_data __u32
47 extern unsigned long romfs_in_flash
; /* 1 when romfs_start, _length in flash */
48 extern unsigned long romfs_start
, romfs_length
;
49 extern unsigned long nand_boot
; /* 1 when booted from nand flash */
51 struct partition_name
{
55 /* The master mtd for the entire flash. */
56 struct mtd_info
* axisflash_mtd
= NULL
;
58 /* Map driver functions. */
60 static map_word
flash_read(struct map_info
*map
, unsigned long ofs
)
63 tmp
.x
[0] = *(flash_data
*)(map
->map_priv_1
+ ofs
);
67 static void flash_copy_from(struct map_info
*map
, void *to
,
68 unsigned long from
, ssize_t len
)
70 memcpy(to
, (void *)(map
->map_priv_1
+ from
), len
);
73 static void flash_write(struct map_info
*map
, map_word d
, unsigned long adr
)
75 *(flash_data
*)(map
->map_priv_1
+ adr
) = (flash_data
)d
.x
[0];
79 * The map for chip select e0.
81 * We run into tricky coherence situations if we mix cached with uncached
82 * accesses to we only use the uncached version here.
84 * The size field is the total size where the flash chips may be mapped on the
85 * chip select. MTD probes should find all devices there and it does not matter
86 * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
87 * probes will ignore them.
89 * The start address in map_priv_1 is in virtual memory so we cannot use
90 * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
93 static struct map_info map_cse0
= {
95 .size
= MEM_CSE0_SIZE
,
96 .bankwidth
= CONFIG_ETRAX_FLASH_BUSWIDTH
,
98 .copy_from
= flash_copy_from
,
100 .map_priv_1
= FLASH_UNCACHED_ADDR
104 * The map for chip select e1.
106 * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
107 * address, but there isn't.
109 static struct map_info map_cse1
= {
111 .size
= MEM_CSE1_SIZE
,
112 .bankwidth
= CONFIG_ETRAX_FLASH_BUSWIDTH
,
114 .copy_from
= flash_copy_from
,
115 .write
= flash_write
,
116 .map_priv_1
= FLASH_UNCACHED_ADDR
+ MEM_CSE0_SIZE
119 #define MAX_PARTITIONS 7
120 #ifdef CONFIG_ETRAX_NANDBOOT
121 #define NUM_DEFAULT_PARTITIONS 4
122 #define DEFAULT_ROOTFS_PARTITION_NO 2
123 #define DEFAULT_MEDIA_SIZE 0x2000000 /* 32 megs */
125 #define NUM_DEFAULT_PARTITIONS 3
126 #define DEFAULT_ROOTFS_PARTITION_NO (-1)
127 #define DEFAULT_MEDIA_SIZE 0x800000 /* 8 megs */
130 #if (MAX_PARTITIONS < NUM_DEFAULT_PARTITIONS)
131 #error MAX_PARTITIONS must be >= than NUM_DEFAULT_PARTITIONS
134 /* Initialize the ones normally used. */
135 static struct mtd_partition axis_partitions
[MAX_PARTITIONS
] = {
138 .size
= CONFIG_ETRAX_PTABLE_SECTOR
,
174 /* If no partition-table was found, we use this default-set.
175 * Default flash size is 8MB (NOR). CONFIG_ETRAX_PTABLE_SECTOR is most
176 * likely the size of one flash block and "filesystem"-partition needs
177 * to be >=5 blocks to be able to use JFFS.
179 static struct mtd_partition axis_default_partitions
[NUM_DEFAULT_PARTITIONS
] = {
181 .name
= "boot firmware",
182 .size
= CONFIG_ETRAX_PTABLE_SECTOR
,
187 .size
= 10 * CONFIG_ETRAX_PTABLE_SECTOR
,
188 .offset
= CONFIG_ETRAX_PTABLE_SECTOR
190 #define FILESYSTEM_SECTOR (11 * CONFIG_ETRAX_PTABLE_SECTOR)
191 #ifdef CONFIG_ETRAX_NANDBOOT
194 .size
= 10 * CONFIG_ETRAX_PTABLE_SECTOR
,
195 .offset
= FILESYSTEM_SECTOR
197 #undef FILESYSTEM_SECTOR
198 #define FILESYSTEM_SECTOR (21 * CONFIG_ETRAX_PTABLE_SECTOR)
202 .size
= DEFAULT_MEDIA_SIZE
- FILESYSTEM_SECTOR
,
203 .offset
= FILESYSTEM_SECTOR
207 #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
208 /* Main flash device */
209 static struct mtd_partition main_partition
= {
216 /* Auxiliary partition if we find another flash */
217 static struct mtd_partition aux_partition
= {
224 * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
225 * chips in that order (because the amd_flash-driver is faster).
227 static struct mtd_info
*probe_cs(struct map_info
*map_cs
)
229 struct mtd_info
*mtd_cs
= NULL
;
232 "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
233 map_cs
->name
, map_cs
->size
, map_cs
->map_priv_1
);
235 #ifdef CONFIG_MTD_CFI
236 mtd_cs
= do_map_probe("cfi_probe", map_cs
);
238 #ifdef CONFIG_MTD_JEDECPROBE
240 mtd_cs
= do_map_probe("jedec_probe", map_cs
);
247 * Probe each chip select individually for flash chips. If there are chips on
248 * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
249 * so that MTD partitions can cross chip boundaries.
251 * The only known restriction to how you can mount your chips is that each
252 * chip select must hold similar flash chips. But you need external hardware
253 * to do that anyway and you can put totally different chips on cse0 and cse1
254 * so it isn't really much of a restriction.
256 extern struct mtd_info
* __init
crisv32_nand_flash_probe (void);
257 static struct mtd_info
*flash_probe(void)
259 struct mtd_info
*mtd_cse0
;
260 struct mtd_info
*mtd_cse1
;
261 struct mtd_info
*mtd_total
;
262 struct mtd_info
*mtds
[2];
265 if ((mtd_cse0
= probe_cs(&map_cse0
)) != NULL
)
266 mtds
[count
++] = mtd_cse0
;
267 if ((mtd_cse1
= probe_cs(&map_cse1
)) != NULL
)
268 mtds
[count
++] = mtd_cse1
;
270 if (!mtd_cse0
&& !mtd_cse1
) {
276 /* Since the concatenation layer adds a small overhead we
277 * could try to figure out if the chips in cse0 and cse1 are
278 * identical and reprobe the whole cse0+cse1 window. But since
279 * flash chips are slow, the overhead is relatively small.
280 * So we use the MTD concatenation layer instead of further
281 * complicating the probing procedure.
283 mtd_total
= mtd_concat_create(mtds
, count
, "cse0+cse1");
285 printk(KERN_ERR
"%s and %s: Concatenation failed!\n",
286 map_cse0
.name
, map_cse1
.name
);
288 /* The best we can do now is to only use what we found
290 mtd_total
= mtd_cse0
;
291 map_destroy(mtd_cse1
);
294 mtd_total
= mtd_cse0
? mtd_cse0
: mtd_cse1
;
300 * Probe the flash chip(s) and, if it succeeds, read the partition-table
301 * and register the partitions with MTD.
303 static int __init
init_axis_flash(void)
305 struct mtd_info
*main_mtd
;
306 struct mtd_info
*aux_mtd
= NULL
;
309 struct partitiontable_head
*ptable_head
= NULL
;
310 struct partitiontable_entry
*ptable
;
312 static char page
[PAGESIZE
];
314 int ram_rootfs_partition
= -1; /* -1 => no RAM rootfs partition */
316 struct mtd_partition
*partition
;
318 /* We need a root fs. If it resides in RAM, we need to use an
319 * MTDRAM device, so it must be enabled in the kernel config,
320 * but its size must be configured as 0 so as not to conflict
323 #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0)
324 if (!romfs_in_flash
&& !nand_boot
) {
325 printk(KERN_EMERG
"axisflashmap: Cannot create an MTD RAM "
326 "device; configure CONFIG_MTD_MTDRAM with size = 0!\n");
327 panic("This kernel cannot boot from RAM!\n");
331 main_mtd
= flash_probe();
333 printk(KERN_INFO
"%s: 0x%08llx bytes of NOR flash memory.\n",
334 main_mtd
->name
, main_mtd
->size
);
336 #ifdef CONFIG_ETRAX_NANDFLASH
337 aux_mtd
= crisv32_nand_flash_probe();
339 printk(KERN_INFO
"%s: 0x%08x bytes of NAND flash memory.\n",
340 aux_mtd
->name
, aux_mtd
->size
);
342 #ifdef CONFIG_ETRAX_NANDBOOT
344 struct mtd_info
*tmp_mtd
;
346 printk(KERN_INFO
"axisflashmap: Set to boot from NAND flash, "
347 "making NAND flash primary device.\n");
352 #endif /* CONFIG_ETRAX_NANDBOOT */
353 #endif /* CONFIG_ETRAX_NANDFLASH */
355 if (!main_mtd
&& !aux_mtd
) {
356 /* There's no reason to use this module if no flash chip can
357 * be identified. Make sure that's understood.
359 printk(KERN_INFO
"axisflashmap: Found no flash chip.\n");
362 #if 0 /* Dump flash memory so we can see what is going on */
365 for (sectoraddr
= 0; sectoraddr
< 2*65536+4096;
366 sectoraddr
+= PAGESIZE
) {
367 main_mtd
->read(main_mtd
, sectoraddr
, PAGESIZE
, &len
,
370 "Sector at %d (length %d):\n",
372 print_hex_dump(KERN_INFO
, "", DUMP_PREFIX_NONE
, 16, 1, page
, PAGESIZE
, false);
378 loff_t ptable_sector
= CONFIG_ETRAX_PTABLE_SECTOR
;
379 main_mtd
->owner
= THIS_MODULE
;
380 axisflash_mtd
= main_mtd
;
383 /* First partition (rescue) is always set to the default. */
385 #ifdef CONFIG_ETRAX_NANDBOOT
386 /* We know where the partition table should be located,
387 * it will be in first good block after that.
391 blockstat
= mtd_block_isbad(main_mtd
, ptable_sector
);
393 ptable_sector
= 0; /* read error */
395 ptable_sector
+= main_mtd
->erasesize
;
396 } while (blockstat
&& ptable_sector
);
399 mtd_read(main_mtd
, ptable_sector
, PAGESIZE
, &len
,
401 ptable_head
= &((struct partitiontable
*) page
)->head
;
404 #if 0 /* Dump partition table so we can see what is going on */
406 "axisflashmap: flash read %d bytes at 0x%08x, data: %8ph\n",
407 len
, CONFIG_ETRAX_PTABLE_SECTOR
, page
);
409 "axisflashmap: partition table offset %d, data: %8ph\n",
410 PARTITION_TABLE_OFFSET
, page
+ PARTITION_TABLE_OFFSET
);
414 if (ptable_head
&& (ptable_head
->magic
== PARTITION_TABLE_MAGIC
)
415 && (ptable_head
->size
<
416 (MAX_PARTITIONS
* sizeof(struct partitiontable_entry
) +
417 PARTITIONTABLE_END_MARKER_SIZE
))
418 && (*(unsigned long*)((void*)ptable_head
+ sizeof(*ptable_head
) +
420 PARTITIONTABLE_END_MARKER_SIZE
)
421 == PARTITIONTABLE_END_MARKER
)) {
422 /* Looks like a start, sane length and end of a
423 * partition table, lets check csum etc.
425 struct partitiontable_entry
*max_addr
=
426 (struct partitiontable_entry
*)
427 ((unsigned long)ptable_head
+ sizeof(*ptable_head
) +
429 unsigned long offset
= CONFIG_ETRAX_PTABLE_SECTOR
;
431 unsigned long csum
= 0;
433 ptable
= (struct partitiontable_entry
*)
434 ((unsigned long)ptable_head
+ sizeof(*ptable_head
));
436 /* Lets be PARANOID, and check the checksum. */
437 p
= (unsigned char*) ptable
;
439 while (p
<= (unsigned char*)max_addr
) {
445 ptable_ok
= (csum
== ptable_head
->checksum
);
447 /* Read the entries and use/show the info. */
448 printk(KERN_INFO
"axisflashmap: "
449 "Found a%s partition table at 0x%p-0x%p.\n",
450 (ptable_ok
? " valid" : "n invalid"), ptable_head
,
453 /* We have found a working bootblock. Now read the
454 * partition table. Scan the table. It ends with 0xffffffff.
457 && ptable
->offset
!= PARTITIONTABLE_END_MARKER
459 && pidx
< MAX_PARTITIONS
- 1) {
461 axis_partitions
[pidx
].offset
= offset
+ ptable
->offset
;
462 #ifdef CONFIG_ETRAX_NANDFLASH
463 if (main_mtd
->type
== MTD_NANDFLASH
) {
464 axis_partitions
[pidx
].size
=
465 (((ptable
+1)->offset
==
466 PARTITIONTABLE_END_MARKER
) ?
468 ((ptable
+1)->offset
+ offset
)) -
469 (ptable
->offset
+ offset
);
472 #endif /* CONFIG_ETRAX_NANDFLASH */
473 axis_partitions
[pidx
].size
= ptable
->size
;
474 #ifdef CONFIG_ETRAX_NANDBOOT
475 /* Save partition number of jffs2 ro partition.
476 * Needed if RAM booting or root file system in RAM.
479 ram_rootfs_partition
< 0 && /* not already set */
480 ptable
->type
== PARTITION_TYPE_JFFS2
&&
481 (ptable
->flags
& PARTITION_FLAGS_READONLY_MASK
) ==
482 PARTITION_FLAGS_READONLY
)
483 ram_rootfs_partition
= pidx
;
484 #endif /* CONFIG_ETRAX_NANDBOOT */
490 /* Decide whether to use default partition table. */
491 /* Only use default table if we actually have a device (main_mtd) */
493 partition
= &axis_partitions
[0];
494 if (main_mtd
&& !ptable_ok
) {
495 memcpy(axis_partitions
, axis_default_partitions
,
496 sizeof(axis_default_partitions
));
497 pidx
= NUM_DEFAULT_PARTITIONS
;
498 ram_rootfs_partition
= DEFAULT_ROOTFS_PARTITION_NO
;
501 /* Add artificial partitions for rootfs if necessary */
502 if (romfs_in_flash
) {
503 /* rootfs is in directly accessible flash memory = NOR flash.
504 Add an overlapping device for the rootfs partition. */
505 printk(KERN_INFO
"axisflashmap: Adding partition for "
506 "overlapping root file system image\n");
507 axis_partitions
[pidx
].size
= romfs_length
;
508 axis_partitions
[pidx
].offset
= romfs_start
- FLASH_CACHED_ADDR
;
509 axis_partitions
[pidx
].name
= "romfs";
510 axis_partitions
[pidx
].mask_flags
|= MTD_WRITEABLE
;
511 ram_rootfs_partition
= -1;
513 } else if (romfs_length
&& !nand_boot
) {
514 /* romfs exists in memory, but not in flash, so must be in RAM.
515 * Configure an MTDRAM partition. */
516 if (ram_rootfs_partition
< 0) {
517 /* None set yet, put it at the end */
518 ram_rootfs_partition
= pidx
;
521 printk(KERN_INFO
"axisflashmap: Adding partition for "
522 "root file system image in RAM\n");
523 axis_partitions
[ram_rootfs_partition
].size
= romfs_length
;
524 axis_partitions
[ram_rootfs_partition
].offset
= romfs_start
;
525 axis_partitions
[ram_rootfs_partition
].name
= "romfs";
526 axis_partitions
[ram_rootfs_partition
].mask_flags
|=
530 #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
532 main_partition
.size
= main_mtd
->size
;
533 err
= mtd_device_register(main_mtd
, &main_partition
, 1);
535 panic("axisflashmap: Could not initialize "
536 "partition for whole main mtd device!\n");
540 /* Now, register all partitions with mtd.
541 * We do this one at a time so we can slip in an MTDRAM device
542 * in the proper place if required. */
544 for (part
= 0; part
< pidx
; part
++) {
545 if (part
== ram_rootfs_partition
) {
546 /* add MTDRAM partition here */
547 struct mtd_info
*mtd_ram
;
549 mtd_ram
= kmalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
551 panic("axisflashmap: Couldn't allocate memory "
553 printk(KERN_INFO
"axisflashmap: Adding RAM partition "
554 "for rootfs image.\n");
555 err
= mtdram_init_device(mtd_ram
,
556 (void *)(u_int32_t
)partition
[part
].offset
,
557 partition
[part
].size
,
558 partition
[part
].name
);
560 panic("axisflashmap: Could not initialize "
561 "MTD RAM device!\n");
562 /* JFFS2 likes to have an erasesize. Keep potential
563 * JFFS2 rootfs happy by providing one. Since image
564 * was most likely created for main mtd, use that
565 * erasesize, if available. Otherwise, make a guess. */
566 mtd_ram
->erasesize
= (main_mtd
? main_mtd
->erasesize
:
567 CONFIG_ETRAX_PTABLE_SECTOR
);
569 err
= mtd_device_register(main_mtd
, &partition
[part
],
572 panic("axisflashmap: Could not add mtd "
573 "partition %d\n", part
);
578 aux_partition
.size
= aux_mtd
->size
;
579 err
= mtd_device_register(aux_mtd
, &aux_partition
, 1);
581 panic("axisflashmap: Could not initialize "
582 "aux mtd device!\n");
589 /* This adds the above to the kernels init-call chain. */
590 module_init(init_axis_flash
);
592 EXPORT_SYMBOL(axisflash_mtd
);