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 <linux/cramfs_fs.h>
29 #include <asm/axisflashmap.h>
32 #define MEM_CSE0_SIZE (0x04000000)
33 #define MEM_CSE1_SIZE (0x04000000)
35 #define FLASH_UNCACHED_ADDR KSEG_E
36 #define FLASH_CACHED_ADDR KSEG_F
38 #define PAGESIZE (512)
40 #if CONFIG_ETRAX_FLASH_BUSWIDTH==1
41 #define flash_data __u8
42 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
43 #define flash_data __u16
44 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
45 #define flash_data __u32
49 extern unsigned long romfs_in_flash
; /* 1 when romfs_start, _length in flash */
50 extern unsigned long romfs_start
, romfs_length
;
51 extern unsigned long nand_boot
; /* 1 when booted from nand flash */
53 struct partition_name
{
57 /* The master mtd for the entire flash. */
58 struct mtd_info
* axisflash_mtd
= NULL
;
60 /* Map driver functions. */
62 static map_word
flash_read(struct map_info
*map
, unsigned long ofs
)
65 tmp
.x
[0] = *(flash_data
*)(map
->map_priv_1
+ ofs
);
69 static void flash_copy_from(struct map_info
*map
, void *to
,
70 unsigned long from
, ssize_t len
)
72 memcpy(to
, (void *)(map
->map_priv_1
+ from
), len
);
75 static void flash_write(struct map_info
*map
, map_word d
, unsigned long adr
)
77 *(flash_data
*)(map
->map_priv_1
+ adr
) = (flash_data
)d
.x
[0];
81 * The map for chip select e0.
83 * We run into tricky coherence situations if we mix cached with uncached
84 * accesses to we only use the uncached version here.
86 * The size field is the total size where the flash chips may be mapped on the
87 * chip select. MTD probes should find all devices there and it does not matter
88 * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
89 * probes will ignore them.
91 * The start address in map_priv_1 is in virtual memory so we cannot use
92 * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
95 static struct map_info map_cse0
= {
97 .size
= MEM_CSE0_SIZE
,
98 .bankwidth
= CONFIG_ETRAX_FLASH_BUSWIDTH
,
100 .copy_from
= flash_copy_from
,
101 .write
= flash_write
,
102 .map_priv_1
= FLASH_UNCACHED_ADDR
106 * The map for chip select e1.
108 * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
109 * address, but there isn't.
111 static struct map_info map_cse1
= {
113 .size
= MEM_CSE1_SIZE
,
114 .bankwidth
= CONFIG_ETRAX_FLASH_BUSWIDTH
,
116 .copy_from
= flash_copy_from
,
117 .write
= flash_write
,
118 .map_priv_1
= FLASH_UNCACHED_ADDR
+ MEM_CSE0_SIZE
121 #define MAX_PARTITIONS 7
122 #ifdef CONFIG_ETRAX_NANDBOOT
123 #define NUM_DEFAULT_PARTITIONS 4
124 #define DEFAULT_ROOTFS_PARTITION_NO 2
125 #define DEFAULT_MEDIA_SIZE 0x2000000 /* 32 megs */
127 #define NUM_DEFAULT_PARTITIONS 3
128 #define DEFAULT_ROOTFS_PARTITION_NO (-1)
129 #define DEFAULT_MEDIA_SIZE 0x800000 /* 8 megs */
132 #if (MAX_PARTITIONS < NUM_DEFAULT_PARTITIONS)
133 #error MAX_PARTITIONS must be >= than NUM_DEFAULT_PARTITIONS
136 /* Initialize the ones normally used. */
137 static struct mtd_partition axis_partitions
[MAX_PARTITIONS
] = {
140 .size
= CONFIG_ETRAX_PTABLE_SECTOR
,
176 /* If no partition-table was found, we use this default-set.
177 * Default flash size is 8MB (NOR). CONFIG_ETRAX_PTABLE_SECTOR is most
178 * likely the size of one flash block and "filesystem"-partition needs
179 * to be >=5 blocks to be able to use JFFS.
181 static struct mtd_partition axis_default_partitions
[NUM_DEFAULT_PARTITIONS
] = {
183 .name
= "boot firmware",
184 .size
= CONFIG_ETRAX_PTABLE_SECTOR
,
189 .size
= 10 * CONFIG_ETRAX_PTABLE_SECTOR
,
190 .offset
= CONFIG_ETRAX_PTABLE_SECTOR
192 #define FILESYSTEM_SECTOR (11 * CONFIG_ETRAX_PTABLE_SECTOR)
193 #ifdef CONFIG_ETRAX_NANDBOOT
196 .size
= 10 * CONFIG_ETRAX_PTABLE_SECTOR
,
197 .offset
= FILESYSTEM_SECTOR
199 #undef FILESYSTEM_SECTOR
200 #define FILESYSTEM_SECTOR (21 * CONFIG_ETRAX_PTABLE_SECTOR)
204 .size
= DEFAULT_MEDIA_SIZE
- FILESYSTEM_SECTOR
,
205 .offset
= FILESYSTEM_SECTOR
209 #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
210 /* Main flash device */
211 static struct mtd_partition main_partition
= {
218 /* Auxiliary partition if we find another flash */
219 static struct mtd_partition aux_partition
= {
226 * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
227 * chips in that order (because the amd_flash-driver is faster).
229 static struct mtd_info
*probe_cs(struct map_info
*map_cs
)
231 struct mtd_info
*mtd_cs
= NULL
;
234 "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
235 map_cs
->name
, map_cs
->size
, map_cs
->map_priv_1
);
237 #ifdef CONFIG_MTD_CFI
238 mtd_cs
= do_map_probe("cfi_probe", map_cs
);
240 #ifdef CONFIG_MTD_JEDECPROBE
242 mtd_cs
= do_map_probe("jedec_probe", map_cs
);
249 * Probe each chip select individually for flash chips. If there are chips on
250 * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
251 * so that MTD partitions can cross chip boundries.
253 * The only known restriction to how you can mount your chips is that each
254 * chip select must hold similar flash chips. But you need external hardware
255 * to do that anyway and you can put totally different chips on cse0 and cse1
256 * so it isn't really much of a restriction.
258 extern struct mtd_info
* __init
crisv32_nand_flash_probe (void);
259 static struct mtd_info
*flash_probe(void)
261 struct mtd_info
*mtd_cse0
;
262 struct mtd_info
*mtd_cse1
;
263 struct mtd_info
*mtd_total
;
264 struct mtd_info
*mtds
[2];
267 if ((mtd_cse0
= probe_cs(&map_cse0
)) != NULL
)
268 mtds
[count
++] = mtd_cse0
;
269 if ((mtd_cse1
= probe_cs(&map_cse1
)) != NULL
)
270 mtds
[count
++] = mtd_cse1
;
272 if (!mtd_cse0
&& !mtd_cse1
) {
278 /* Since the concatenation layer adds a small overhead we
279 * could try to figure out if the chips in cse0 and cse1 are
280 * identical and reprobe the whole cse0+cse1 window. But since
281 * flash chips are slow, the overhead is relatively small.
282 * So we use the MTD concatenation layer instead of further
283 * complicating the probing procedure.
285 mtd_total
= mtd_concat_create(mtds
, count
, "cse0+cse1");
287 printk(KERN_ERR
"%s and %s: Concatenation failed!\n",
288 map_cse0
.name
, map_cse1
.name
);
290 /* The best we can do now is to only use what we found
292 mtd_total
= mtd_cse0
;
293 map_destroy(mtd_cse1
);
296 mtd_total
= mtd_cse0
? mtd_cse0
: mtd_cse1
;
302 * Probe the flash chip(s) and, if it succeeds, read the partition-table
303 * and register the partitions with MTD.
305 static int __init
init_axis_flash(void)
307 struct mtd_info
*main_mtd
;
308 struct mtd_info
*aux_mtd
= NULL
;
311 struct partitiontable_head
*ptable_head
= NULL
;
312 struct partitiontable_entry
*ptable
;
314 static char page
[PAGESIZE
];
316 int ram_rootfs_partition
= -1; /* -1 => no RAM rootfs partition */
319 /* We need a root fs. If it resides in RAM, we need to use an
320 * MTDRAM device, so it must be enabled in the kernel config,
321 * but its size must be configured as 0 so as not to conflict
324 #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
325 if (!romfs_in_flash
&& !nand_boot
) {
326 printk(KERN_EMERG
"axisflashmap: Cannot create an MTD RAM "
327 "device; configure CONFIG_MTD_MTDRAM with size = 0!\n");
328 panic("This kernel cannot boot from RAM!\n");
332 main_mtd
= flash_probe();
334 printk(KERN_INFO
"%s: 0x%08x bytes of NOR flash memory.\n",
335 main_mtd
->name
, main_mtd
->size
);
337 #ifdef CONFIG_ETRAX_NANDFLASH
338 aux_mtd
= crisv32_nand_flash_probe();
340 printk(KERN_INFO
"%s: 0x%08x bytes of NAND flash memory.\n",
341 aux_mtd
->name
, aux_mtd
->size
);
343 #ifdef CONFIG_ETRAX_NANDBOOT
345 struct mtd_info
*tmp_mtd
;
347 printk(KERN_INFO
"axisflashmap: Set to boot from NAND flash, "
348 "making NAND flash primary device.\n");
353 #endif /* CONFIG_ETRAX_NANDBOOT */
354 #endif /* CONFIG_ETRAX_NANDFLASH */
356 if (!main_mtd
&& !aux_mtd
) {
357 /* There's no reason to use this module if no flash chip can
358 * be identified. Make sure that's understood.
360 printk(KERN_INFO
"axisflashmap: Found no flash chip.\n");
363 #if 0 /* Dump flash memory so we can see what is going on */
366 for (sectoraddr
= 0; sectoraddr
< 2*65536+4096;
367 sectoraddr
+= PAGESIZE
) {
368 main_mtd
->read(main_mtd
, sectoraddr
, PAGESIZE
, &len
,
371 "Sector at %d (length %d):\n",
373 for (i
= 0; i
< PAGESIZE
; i
+= 16) {
375 "%02x %02x %02x %02x "
376 "%02x %02x %02x %02x "
377 "%02x %02x %02x %02x "
378 "%02x %02x %02x %02x\n",
379 page
[i
] & 255, page
[i
+1] & 255,
380 page
[i
+2] & 255, page
[i
+3] & 255,
381 page
[i
+4] & 255, page
[i
+5] & 255,
382 page
[i
+6] & 255, page
[i
+7] & 255,
383 page
[i
+8] & 255, page
[i
+9] & 255,
384 page
[i
+10] & 255, page
[i
+11] & 255,
385 page
[i
+12] & 255, page
[i
+13] & 255,
386 page
[i
+14] & 255, page
[i
+15] & 255);
393 main_mtd
->owner
= THIS_MODULE
;
394 axisflash_mtd
= main_mtd
;
396 loff_t ptable_sector
= CONFIG_ETRAX_PTABLE_SECTOR
;
398 /* First partition (rescue) is always set to the default. */
400 #ifdef CONFIG_ETRAX_NANDBOOT
401 /* We know where the partition table should be located,
402 * it will be in first good block after that.
406 blockstat
= mtd_block_isbad(main_mtd
, ptable_sector
);
408 ptable_sector
= 0; /* read error */
410 ptable_sector
+= main_mtd
->erasesize
;
411 } while (blockstat
&& ptable_sector
);
414 mtd_read(main_mtd
, ptable_sector
, PAGESIZE
, &len
,
416 ptable_head
= &((struct partitiontable
*) page
)->head
;
419 #if 0 /* Dump partition table so we can see what is going on */
421 "axisflashmap: flash read %d bytes at 0x%08x, data: "
422 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
423 len
, CONFIG_ETRAX_PTABLE_SECTOR
,
424 page
[0] & 255, page
[1] & 255,
425 page
[2] & 255, page
[3] & 255,
426 page
[4] & 255, page
[5] & 255,
427 page
[6] & 255, page
[7] & 255);
429 "axisflashmap: partition table offset %d, data: "
430 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
431 PARTITION_TABLE_OFFSET
,
432 page
[PARTITION_TABLE_OFFSET
+0] & 255,
433 page
[PARTITION_TABLE_OFFSET
+1] & 255,
434 page
[PARTITION_TABLE_OFFSET
+2] & 255,
435 page
[PARTITION_TABLE_OFFSET
+3] & 255,
436 page
[PARTITION_TABLE_OFFSET
+4] & 255,
437 page
[PARTITION_TABLE_OFFSET
+5] & 255,
438 page
[PARTITION_TABLE_OFFSET
+6] & 255,
439 page
[PARTITION_TABLE_OFFSET
+7] & 255);
443 if (ptable_head
&& (ptable_head
->magic
== PARTITION_TABLE_MAGIC
)
444 && (ptable_head
->size
<
445 (MAX_PARTITIONS
* sizeof(struct partitiontable_entry
) +
446 PARTITIONTABLE_END_MARKER_SIZE
))
447 && (*(unsigned long*)((void*)ptable_head
+ sizeof(*ptable_head
) +
449 PARTITIONTABLE_END_MARKER_SIZE
)
450 == PARTITIONTABLE_END_MARKER
)) {
451 /* Looks like a start, sane length and end of a
452 * partition table, lets check csum etc.
454 struct partitiontable_entry
*max_addr
=
455 (struct partitiontable_entry
*)
456 ((unsigned long)ptable_head
+ sizeof(*ptable_head
) +
458 unsigned long offset
= CONFIG_ETRAX_PTABLE_SECTOR
;
460 unsigned long csum
= 0;
462 ptable
= (struct partitiontable_entry
*)
463 ((unsigned long)ptable_head
+ sizeof(*ptable_head
));
465 /* Lets be PARANOID, and check the checksum. */
466 p
= (unsigned char*) ptable
;
468 while (p
<= (unsigned char*)max_addr
) {
474 ptable_ok
= (csum
== ptable_head
->checksum
);
476 /* Read the entries and use/show the info. */
477 printk(KERN_INFO
"axisflashmap: "
478 "Found a%s partition table at 0x%p-0x%p.\n",
479 (ptable_ok
? " valid" : "n invalid"), ptable_head
,
482 /* We have found a working bootblock. Now read the
483 * partition table. Scan the table. It ends with 0xffffffff.
486 && ptable
->offset
!= PARTITIONTABLE_END_MARKER
488 && pidx
< MAX_PARTITIONS
- 1) {
490 axis_partitions
[pidx
].offset
= offset
+ ptable
->offset
;
491 #ifdef CONFIG_ETRAX_NANDFLASH
492 if (main_mtd
->type
== MTD_NANDFLASH
) {
493 axis_partitions
[pidx
].size
=
494 (((ptable
+1)->offset
==
495 PARTITIONTABLE_END_MARKER
) ?
497 ((ptable
+1)->offset
+ offset
)) -
498 (ptable
->offset
+ offset
);
501 #endif /* CONFIG_ETRAX_NANDFLASH */
502 axis_partitions
[pidx
].size
= ptable
->size
;
503 #ifdef CONFIG_ETRAX_NANDBOOT
504 /* Save partition number of jffs2 ro partition.
505 * Needed if RAM booting or root file system in RAM.
508 ram_rootfs_partition
< 0 && /* not already set */
509 ptable
->type
== PARTITION_TYPE_JFFS2
&&
510 (ptable
->flags
& PARTITION_FLAGS_READONLY_MASK
) ==
511 PARTITION_FLAGS_READONLY
)
512 ram_rootfs_partition
= pidx
;
513 #endif /* CONFIG_ETRAX_NANDBOOT */
519 /* Decide whether to use default partition table. */
520 /* Only use default table if we actually have a device (main_mtd) */
522 struct mtd_partition
*partition
= &axis_partitions
[0];
523 if (main_mtd
&& !ptable_ok
) {
524 memcpy(axis_partitions
, axis_default_partitions
,
525 sizeof(axis_default_partitions
));
526 pidx
= NUM_DEFAULT_PARTITIONS
;
527 ram_rootfs_partition
= DEFAULT_ROOTFS_PARTITION_NO
;
530 /* Add artificial partitions for rootfs if necessary */
531 if (romfs_in_flash
) {
532 /* rootfs is in directly accessible flash memory = NOR flash.
533 Add an overlapping device for the rootfs partition. */
534 printk(KERN_INFO
"axisflashmap: Adding partition for "
535 "overlapping root file system image\n");
536 axis_partitions
[pidx
].size
= romfs_length
;
537 axis_partitions
[pidx
].offset
= romfs_start
- FLASH_CACHED_ADDR
;
538 axis_partitions
[pidx
].name
= "romfs";
539 axis_partitions
[pidx
].mask_flags
|= MTD_WRITEABLE
;
540 ram_rootfs_partition
= -1;
542 } else if (romfs_length
&& !nand_boot
) {
543 /* romfs exists in memory, but not in flash, so must be in RAM.
544 * Configure an MTDRAM partition. */
545 if (ram_rootfs_partition
< 0) {
546 /* None set yet, put it at the end */
547 ram_rootfs_partition
= pidx
;
550 printk(KERN_INFO
"axisflashmap: Adding partition for "
551 "root file system image in RAM\n");
552 axis_partitions
[ram_rootfs_partition
].size
= romfs_length
;
553 axis_partitions
[ram_rootfs_partition
].offset
= romfs_start
;
554 axis_partitions
[ram_rootfs_partition
].name
= "romfs";
555 axis_partitions
[ram_rootfs_partition
].mask_flags
|=
559 #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
561 main_partition
.size
= main_mtd
->size
;
562 err
= mtd_device_register(main_mtd
, &main_partition
, 1);
564 panic("axisflashmap: Could not initialize "
565 "partition for whole main mtd device!\n");
569 /* Now, register all partitions with mtd.
570 * We do this one at a time so we can slip in an MTDRAM device
571 * in the proper place if required. */
573 for (part
= 0; part
< pidx
; part
++) {
574 if (part
== ram_rootfs_partition
) {
575 /* add MTDRAM partition here */
576 struct mtd_info
*mtd_ram
;
578 mtd_ram
= kmalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
580 panic("axisflashmap: Couldn't allocate memory "
582 printk(KERN_INFO
"axisflashmap: Adding RAM partition "
583 "for rootfs image.\n");
584 err
= mtdram_init_device(mtd_ram
,
585 (void *)partition
[part
].offset
,
586 partition
[part
].size
,
587 partition
[part
].name
);
589 panic("axisflashmap: Could not initialize "
590 "MTD RAM device!\n");
591 /* JFFS2 likes to have an erasesize. Keep potential
592 * JFFS2 rootfs happy by providing one. Since image
593 * was most likely created for main mtd, use that
594 * erasesize, if available. Otherwise, make a guess. */
595 mtd_ram
->erasesize
= (main_mtd
? main_mtd
->erasesize
:
596 CONFIG_ETRAX_PTABLE_SECTOR
);
598 err
= mtd_device_register(main_mtd
, &partition
[part
],
601 panic("axisflashmap: Could not add mtd "
602 "partition %d\n", part
);
607 aux_partition
.size
= aux_mtd
->size
;
608 err
= mtd_device_register(aux_mtd
, &aux_partition
, 1);
610 panic("axisflashmap: Could not initialize "
611 "aux mtd device!\n");
618 /* This adds the above to the kernels init-call chain. */
619 module_init(init_axis_flash
);
621 EXPORT_SYMBOL(axisflash_mtd
);