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.
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>
29 #include <arch/sv_addr_ag.h>
31 #ifdef CONFIG_CRIS_LOW_MAP
32 #define FLASH_UNCACHED_ADDR KSEG_8
33 #define FLASH_CACHED_ADDR KSEG_5
35 #define FLASH_UNCACHED_ADDR KSEG_E
36 #define FLASH_CACHED_ADDR KSEG_F
39 #if CONFIG_ETRAX_FLASH_BUSWIDTH==1
40 #define flash_data __u8
41 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
42 #define flash_data __u16
43 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
44 #define flash_data __u32
48 extern unsigned long romfs_start
, romfs_length
, romfs_in_flash
;
50 /* The master mtd for the entire flash. */
51 struct mtd_info
* axisflash_mtd
= NULL
;
53 /* Map driver functions. */
55 static map_word
flash_read(struct map_info
*map
, unsigned long ofs
)
58 tmp
.x
[0] = *(flash_data
*)(map
->map_priv_1
+ ofs
);
62 static void flash_copy_from(struct map_info
*map
, void *to
,
63 unsigned long from
, ssize_t len
)
65 memcpy(to
, (void *)(map
->map_priv_1
+ from
), len
);
68 static void flash_write(struct map_info
*map
, map_word d
, unsigned long adr
)
70 *(flash_data
*)(map
->map_priv_1
+ adr
) = (flash_data
)d
.x
[0];
74 * The map for chip select e0.
76 * We run into tricky coherence situations if we mix cached with uncached
77 * accesses to we only use the uncached version here.
79 * The size field is the total size where the flash chips may be mapped on the
80 * chip select. MTD probes should find all devices there and it does not matter
81 * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
82 * probes will ignore them.
84 * The start address in map_priv_1 is in virtual memory so we cannot use
85 * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
88 static struct map_info map_cse0
= {
90 .size
= MEM_CSE0_SIZE
,
91 .bankwidth
= CONFIG_ETRAX_FLASH_BUSWIDTH
,
93 .copy_from
= flash_copy_from
,
95 .map_priv_1
= FLASH_UNCACHED_ADDR
99 * The map for chip select e1.
101 * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
102 * address, but there isn't.
104 static struct map_info map_cse1
= {
106 .size
= MEM_CSE1_SIZE
,
107 .bankwidth
= CONFIG_ETRAX_FLASH_BUSWIDTH
,
109 .copy_from
= flash_copy_from
,
110 .write
= flash_write
,
111 .map_priv_1
= FLASH_UNCACHED_ADDR
+ MEM_CSE0_SIZE
114 /* If no partition-table was found, we use this default-set. */
115 #define MAX_PARTITIONS 7
116 #define NUM_DEFAULT_PARTITIONS 3
119 * Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the
120 * size of one flash block and "filesystem"-partition needs 5 blocks to be able
123 static struct mtd_partition axis_default_partitions
[NUM_DEFAULT_PARTITIONS
] = {
125 .name
= "boot firmware",
126 .size
= CONFIG_ETRAX_PTABLE_SECTOR
,
131 .size
= 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR
),
132 .offset
= CONFIG_ETRAX_PTABLE_SECTOR
135 .name
= "filesystem",
136 .size
= 5 * CONFIG_ETRAX_PTABLE_SECTOR
,
137 .offset
= 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR
)
141 /* Initialize the ones normally used. */
142 static struct mtd_partition axis_partitions
[MAX_PARTITIONS
] = {
145 .size
= CONFIG_ETRAX_PTABLE_SECTOR
,
180 #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
181 /* Main flash device */
182 static struct mtd_partition main_partition
= {
190 * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
191 * chips in that order (because the amd_flash-driver is faster).
193 static struct mtd_info
*probe_cs(struct map_info
*map_cs
)
195 struct mtd_info
*mtd_cs
= NULL
;
198 "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
199 map_cs
->name
, map_cs
->size
, map_cs
->map_priv_1
);
201 #ifdef CONFIG_MTD_CFI
202 mtd_cs
= do_map_probe("cfi_probe", map_cs
);
204 #ifdef CONFIG_MTD_JEDECPROBE
206 mtd_cs
= do_map_probe("jedec_probe", map_cs
);
213 * Probe each chip select individually for flash chips. If there are chips on
214 * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
215 * so that MTD partitions can cross chip boundries.
217 * The only known restriction to how you can mount your chips is that each
218 * chip select must hold similar flash chips. But you need external hardware
219 * to do that anyway and you can put totally different chips on cse0 and cse1
220 * so it isn't really much of a restriction.
222 static struct mtd_info
*flash_probe(void)
224 struct mtd_info
*mtd_cse0
;
225 struct mtd_info
*mtd_cse1
;
226 struct mtd_info
*mtd_cse
;
228 mtd_cse0
= probe_cs(&map_cse0
);
229 mtd_cse1
= probe_cs(&map_cse1
);
231 if (!mtd_cse0
&& !mtd_cse1
) {
236 if (mtd_cse0
&& mtd_cse1
) {
237 struct mtd_info
*mtds
[] = { mtd_cse0
, mtd_cse1
};
239 /* Since the concatenation layer adds a small overhead we
240 * could try to figure out if the chips in cse0 and cse1 are
241 * identical and reprobe the whole cse0+cse1 window. But since
242 * flash chips are slow, the overhead is relatively small.
243 * So we use the MTD concatenation layer instead of further
244 * complicating the probing procedure.
246 mtd_cse
= mtd_concat_create(mtds
, ARRAY_SIZE(mtds
),
249 printk(KERN_ERR
"%s and %s: Concatenation failed!\n",
250 map_cse0
.name
, map_cse1
.name
);
252 /* The best we can do now is to only use what we found
256 map_destroy(mtd_cse1
);
259 mtd_cse
= mtd_cse0
? mtd_cse0
: mtd_cse1
;
266 * Probe the flash chip(s) and, if it succeeds, read the partition-table
267 * and register the partitions with MTD.
269 static int __init
init_axis_flash(void)
271 struct mtd_info
*mymtd
;
274 struct partitiontable_head
*ptable_head
= NULL
;
275 struct partitiontable_entry
*ptable
;
276 int use_default_ptable
= 1; /* Until proven otherwise. */
277 const char pmsg
[] = " /dev/flash%d at 0x%08x, size 0x%08x\n";
279 if (!(mymtd
= flash_probe())) {
280 /* There's no reason to use this module if no flash chip can
281 * be identified. Make sure that's understood.
283 printk(KERN_INFO
"axisflashmap: Found no flash chip.\n");
285 printk(KERN_INFO
"%s: 0x%08x bytes of flash memory.\n",
286 mymtd
->name
, mymtd
->size
);
287 axisflash_mtd
= mymtd
;
291 mymtd
->owner
= THIS_MODULE
;
292 ptable_head
= (struct partitiontable_head
*)(FLASH_CACHED_ADDR
+
293 CONFIG_ETRAX_PTABLE_SECTOR
+
294 PARTITION_TABLE_OFFSET
);
296 pidx
++; /* First partition is always set to the default. */
298 if (ptable_head
&& (ptable_head
->magic
== PARTITION_TABLE_MAGIC
)
299 && (ptable_head
->size
<
300 (MAX_PARTITIONS
* sizeof(struct partitiontable_entry
) +
301 PARTITIONTABLE_END_MARKER_SIZE
))
302 && (*(unsigned long*)((void*)ptable_head
+ sizeof(*ptable_head
) +
304 PARTITIONTABLE_END_MARKER_SIZE
)
305 == PARTITIONTABLE_END_MARKER
)) {
306 /* Looks like a start, sane length and end of a
307 * partition table, lets check csum etc.
310 struct partitiontable_entry
*max_addr
=
311 (struct partitiontable_entry
*)
312 ((unsigned long)ptable_head
+ sizeof(*ptable_head
) +
314 unsigned long offset
= CONFIG_ETRAX_PTABLE_SECTOR
;
316 unsigned long csum
= 0;
318 ptable
= (struct partitiontable_entry
*)
319 ((unsigned long)ptable_head
+ sizeof(*ptable_head
));
321 /* Lets be PARANOID, and check the checksum. */
322 p
= (unsigned char*) ptable
;
324 while (p
<= (unsigned char*)max_addr
) {
330 ptable_ok
= (csum
== ptable_head
->checksum
);
332 /* Read the entries and use/show the info. */
333 printk(KERN_INFO
" Found a%s partition table at 0x%p-0x%p.\n",
334 (ptable_ok
? " valid" : "n invalid"), ptable_head
,
337 /* We have found a working bootblock. Now read the
338 * partition table. Scan the table. It ends when
339 * there is 0xffffffff, that is, empty flash.
342 && ptable
->offset
!= 0xffffffff
344 && pidx
< MAX_PARTITIONS
) {
346 axis_partitions
[pidx
].offset
= offset
+ ptable
->offset
;
347 axis_partitions
[pidx
].size
= ptable
->size
;
349 printk(pmsg
, pidx
, axis_partitions
[pidx
].offset
,
350 axis_partitions
[pidx
].size
);
354 use_default_ptable
= !ptable_ok
;
357 if (romfs_in_flash
) {
358 /* Add an overlapping device for the root partition (romfs). */
360 axis_partitions
[pidx
].name
= "romfs";
361 axis_partitions
[pidx
].size
= romfs_length
;
362 axis_partitions
[pidx
].offset
= romfs_start
- FLASH_CACHED_ADDR
;
363 axis_partitions
[pidx
].mask_flags
|= MTD_WRITEABLE
;
366 " Adding readonly flash partition for romfs image:\n");
367 printk(pmsg
, pidx
, axis_partitions
[pidx
].offset
,
368 axis_partitions
[pidx
].size
);
372 #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
374 main_partition
.size
= mymtd
->size
;
375 err
= mtd_device_register(mymtd
, &main_partition
, 1);
377 panic("axisflashmap: Could not initialize "
378 "partition for whole main mtd device!\n");
383 if (use_default_ptable
) {
384 printk(KERN_INFO
" Using default partition table.\n");
385 err
= mtd_device_register(mymtd
,
386 axis_default_partitions
,
387 NUM_DEFAULT_PARTITIONS
);
389 err
= mtd_device_register(mymtd
, axis_partitions
,
394 panic("axisflashmap could not add MTD partitions!\n");
397 if (!romfs_in_flash
) {
398 /* Create an RAM device for the root partition (romfs). */
400 #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
401 /* No use trying to boot this kernel from RAM. Panic! */
402 printk(KERN_EMERG
"axisflashmap: Cannot create an MTD RAM "
403 "device due to kernel (mis)configuration!\n");
404 panic("This kernel cannot boot from RAM!\n");
406 struct mtd_info
*mtd_ram
;
408 mtd_ram
= kmalloc(sizeof(struct mtd_info
), GFP_KERNEL
);
410 panic("axisflashmap couldn't allocate memory for "
413 printk(KERN_INFO
" Adding RAM partition for romfs image:\n");
414 printk(pmsg
, pidx
, (unsigned)romfs_start
,
415 (unsigned)romfs_length
);
417 err
= mtdram_init_device(mtd_ram
,
422 panic("axisflashmap could not initialize MTD RAM "
429 /* This adds the above to the kernels init-call chain. */
430 module_init(init_axis_flash
);
432 EXPORT_SYMBOL(axisflash_mtd
);