2 * Physical mapping layer for MTD using the Axis partitiontable format
4 * Copyright (c) 2001, 2002, 2003 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 * Copy of os/lx25/arch/cris/arch-v10/drivers/axisflashmap.c 1.5
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/config.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
25 #include <linux/mtd/concat.h>
26 #include <linux/mtd/map.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/mtdram.h>
29 #include <linux/mtd/partitions.h>
31 #include <asm/arch/hwregs/config_defs.h>
32 #include <asm/axisflashmap.h>
35 #define MEM_CSE0_SIZE (0x04000000)
36 #define MEM_CSE1_SIZE (0x04000000)
38 #define FLASH_UNCACHED_ADDR KSEG_E
39 #define FLASH_CACHED_ADDR KSEG_F
41 #if CONFIG_ETRAX_FLASH_BUSWIDTH==1
42 #define flash_data __u8
43 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
44 #define flash_data __u16
45 #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
46 #define flash_data __u16
50 extern unsigned long romfs_start
, romfs_length
, romfs_in_flash
;
52 /* The master mtd for the entire flash. */
53 struct mtd_info
* axisflash_mtd
= NULL
;
55 /* Map driver functions. */
57 static map_word
flash_read(struct map_info
*map
, unsigned long ofs
)
60 tmp
.x
[0] = *(flash_data
*)(map
->map_priv_1
+ ofs
);
64 static void flash_copy_from(struct map_info
*map
, void *to
,
65 unsigned long from
, ssize_t len
)
67 memcpy(to
, (void *)(map
->map_priv_1
+ from
), len
);
70 static void flash_write(struct map_info
*map
, map_word d
, unsigned long adr
)
72 *(flash_data
*)(map
->map_priv_1
+ adr
) = (flash_data
)d
.x
[0];
76 * The map for chip select e0.
78 * We run into tricky coherence situations if we mix cached with uncached
79 * accesses to we only use the uncached version here.
81 * The size field is the total size where the flash chips may be mapped on the
82 * chip select. MTD probes should find all devices there and it does not matter
83 * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
84 * probes will ignore them.
86 * The start address in map_priv_1 is in virtual memory so we cannot use
87 * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
90 static struct map_info map_cse0
= {
92 .size
= MEM_CSE0_SIZE
,
93 .bankwidth
= CONFIG_ETRAX_FLASH_BUSWIDTH
,
95 .copy_from
= flash_copy_from
,
97 .map_priv_1
= FLASH_UNCACHED_ADDR
101 * The map for chip select e1.
103 * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
104 * address, but there isn't.
106 static struct map_info map_cse1
= {
108 .size
= MEM_CSE1_SIZE
,
109 .bankwidth
= CONFIG_ETRAX_FLASH_BUSWIDTH
,
111 .copy_from
= flash_copy_from
,
112 .write
= flash_write
,
113 .map_priv_1
= FLASH_UNCACHED_ADDR
+ MEM_CSE0_SIZE
116 /* If no partition-table was found, we use this default-set. */
117 #define MAX_PARTITIONS 7
118 #define NUM_DEFAULT_PARTITIONS 3
121 * Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the
122 * size of one flash block and "filesystem"-partition needs 5 blocks to be able
125 static struct mtd_partition axis_default_partitions
[NUM_DEFAULT_PARTITIONS
] = {
127 .name
= "boot firmware",
128 .size
= CONFIG_ETRAX_PTABLE_SECTOR
,
133 .size
= 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR
),
134 .offset
= CONFIG_ETRAX_PTABLE_SECTOR
137 .name
= "filesystem",
138 .size
= 5 * CONFIG_ETRAX_PTABLE_SECTOR
,
139 .offset
= 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR
)
143 /* Initialize the ones normally used. */
144 static struct mtd_partition axis_partitions
[MAX_PARTITIONS
] = {
147 .size
= CONFIG_ETRAX_PTABLE_SECTOR
,
183 * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
184 * chips in that order (because the amd_flash-driver is faster).
186 static struct mtd_info
*probe_cs(struct map_info
*map_cs
)
188 struct mtd_info
*mtd_cs
= NULL
;
191 "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
192 map_cs
->name
, map_cs
->size
, map_cs
->map_priv_1
);
194 #ifdef CONFIG_MTD_AMDSTD
195 mtd_cs
= do_map_probe("amd_flash", map_cs
);
197 #ifdef CONFIG_MTD_CFI
199 mtd_cs
= do_map_probe("cfi_probe", map_cs
);
207 * Probe each chip select individually for flash chips. If there are chips on
208 * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
209 * so that MTD partitions can cross chip boundries.
211 * The only known restriction to how you can mount your chips is that each
212 * chip select must hold similar flash chips. But you need external hardware
213 * to do that anyway and you can put totally different chips on cse0 and cse1
214 * so it isn't really much of a restriction.
216 extern struct mtd_info
* __init
crisv32_nand_flash_probe (void);
217 static struct mtd_info
*flash_probe(void)
219 struct mtd_info
*mtd_cse0
;
220 struct mtd_info
*mtd_cse1
;
221 struct mtd_info
*mtd_nand
= NULL
;
222 struct mtd_info
*mtd_total
;
223 struct mtd_info
*mtds
[3];
226 if ((mtd_cse0
= probe_cs(&map_cse0
)) != NULL
)
227 mtds
[count
++] = mtd_cse0
;
228 if ((mtd_cse1
= probe_cs(&map_cse1
)) != NULL
)
229 mtds
[count
++] = mtd_cse1
;
231 #ifdef CONFIG_ETRAX_NANDFLASH
232 if ((mtd_nand
= crisv32_nand_flash_probe()) != NULL
)
233 mtds
[count
++] = mtd_nand
;
236 if (!mtd_cse0
&& !mtd_cse1
&& !mtd_nand
) {
242 #ifdef CONFIG_MTD_CONCAT
243 /* Since the concatenation layer adds a small overhead we
244 * could try to figure out if the chips in cse0 and cse1 are
245 * identical and reprobe the whole cse0+cse1 window. But since
246 * flash chips are slow, the overhead is relatively small.
247 * So we use the MTD concatenation layer instead of further
248 * complicating the probing procedure.
250 mtd_total
= mtd_concat_create(mtds
,
254 printk(KERN_ERR
"%s and %s: Cannot concatenate due to kernel "
255 "(mis)configuration!\n", map_cse0
.name
, map_cse1
.name
);
259 printk(KERN_ERR
"%s and %s: Concatenation failed!\n",
260 map_cse0
.name
, map_cse1
.name
);
262 /* The best we can do now is to only use what we found
265 mtd_total
= mtd_cse0
;
266 map_destroy(mtd_cse1
);
269 mtd_total
= mtd_cse0
? mtd_cse0
: mtd_cse1
? mtd_cse1
: mtd_nand
;
276 extern unsigned long crisv32_nand_boot
;
277 extern unsigned long crisv32_nand_cramfs_offset
;
280 * Probe the flash chip(s) and, if it succeeds, read the partition-table
281 * and register the partitions with MTD.
283 static int __init
init_axis_flash(void)
285 struct mtd_info
*mymtd
;
288 struct partitiontable_head
*ptable_head
= NULL
;
289 struct partitiontable_entry
*ptable
;
290 int use_default_ptable
= 1; /* Until proven otherwise. */
291 const char *pmsg
= KERN_INFO
" /dev/flash%d at 0x%08x, size 0x%08x\n";
292 static char page
[512];
295 #ifndef CONFIG_ETRAXFS_SIM
296 mymtd
= flash_probe();
297 mymtd
->read(mymtd
, CONFIG_ETRAX_PTABLE_SECTOR
, 512, &len
, page
);
298 ptable_head
= (struct partitiontable_head
*)(page
+ PARTITION_TABLE_OFFSET
);
301 /* There's no reason to use this module if no flash chip can
302 * be identified. Make sure that's understood.
304 printk(KERN_INFO
"axisflashmap: Found no flash chip.\n");
306 printk(KERN_INFO
"%s: 0x%08x bytes of flash memory.\n",
307 mymtd
->name
, mymtd
->size
);
308 axisflash_mtd
= mymtd
;
312 mymtd
->owner
= THIS_MODULE
;
314 pidx
++; /* First partition is always set to the default. */
316 if (ptable_head
&& (ptable_head
->magic
== PARTITION_TABLE_MAGIC
)
317 && (ptable_head
->size
<
318 (MAX_PARTITIONS
* sizeof(struct partitiontable_entry
) +
319 PARTITIONTABLE_END_MARKER_SIZE
))
320 && (*(unsigned long*)((void*)ptable_head
+ sizeof(*ptable_head
) +
322 PARTITIONTABLE_END_MARKER_SIZE
)
323 == PARTITIONTABLE_END_MARKER
)) {
324 /* Looks like a start, sane length and end of a
325 * partition table, lets check csum etc.
328 struct partitiontable_entry
*max_addr
=
329 (struct partitiontable_entry
*)
330 ((unsigned long)ptable_head
+ sizeof(*ptable_head
) +
332 unsigned long offset
= CONFIG_ETRAX_PTABLE_SECTOR
;
334 unsigned long csum
= 0;
336 ptable
= (struct partitiontable_entry
*)
337 ((unsigned long)ptable_head
+ sizeof(*ptable_head
));
339 /* Lets be PARANOID, and check the checksum. */
340 p
= (unsigned char*) ptable
;
342 while (p
<= (unsigned char*)max_addr
) {
348 ptable_ok
= (csum
== ptable_head
->checksum
);
350 /* Read the entries and use/show the info. */
351 printk(KERN_INFO
" Found a%s partition table at 0x%p-0x%p.\n",
352 (ptable_ok
? " valid" : "n invalid"), ptable_head
,
355 /* We have found a working bootblock. Now read the
356 * partition table. Scan the table. It ends when
357 * there is 0xffffffff, that is, empty flash.
360 && ptable
->offset
!= 0xffffffff
362 && pidx
< MAX_PARTITIONS
) {
364 axis_partitions
[pidx
].offset
= offset
+ ptable
->offset
+ (crisv32_nand_boot
? 16384 : 0);
365 axis_partitions
[pidx
].size
= ptable
->size
;
367 printk(pmsg
, pidx
, axis_partitions
[pidx
].offset
,
368 axis_partitions
[pidx
].size
);
372 use_default_ptable
= !ptable_ok
;
375 if (romfs_in_flash
) {
376 /* Add an overlapping device for the root partition (romfs). */
378 axis_partitions
[pidx
].name
= "romfs";
379 if (crisv32_nand_boot
) {
380 char* data
= kmalloc(1024, GFP_KERNEL
);
382 int offset
= crisv32_nand_cramfs_offset
& ~(1024-1);
385 mymtd
->read(mymtd
, offset
, 1024, &len
, data
);
386 tmp
= &data
[crisv32_nand_cramfs_offset
% 512];
387 axis_partitions
[pidx
].size
= *(unsigned*)(tmp
+ 4);
388 axis_partitions
[pidx
].offset
= crisv32_nand_cramfs_offset
;
391 axis_partitions
[pidx
].size
= romfs_length
;
392 axis_partitions
[pidx
].offset
= romfs_start
- FLASH_CACHED_ADDR
;
395 axis_partitions
[pidx
].mask_flags
|= MTD_WRITEABLE
;
398 " Adding readonly flash partition for romfs image:\n");
399 printk(pmsg
, pidx
, axis_partitions
[pidx
].offset
,
400 axis_partitions
[pidx
].size
);
405 if (use_default_ptable
) {
406 printk(KERN_INFO
" Using default partition table.\n");
407 err
= add_mtd_partitions(mymtd
, axis_default_partitions
,
408 NUM_DEFAULT_PARTITIONS
);
410 err
= add_mtd_partitions(mymtd
, axis_partitions
, pidx
);
414 panic("axisflashmap could not add MTD partitions!\n");
417 /* CONFIG_EXTRAXFS_SIM */
420 if (!romfs_in_flash
) {
421 /* Create an RAM device for the root partition (romfs). */
423 #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
424 /* No use trying to boot this kernel from RAM. Panic! */
425 printk(KERN_EMERG
"axisflashmap: Cannot create an MTD RAM "
426 "device due to kernel (mis)configuration!\n");
427 panic("This kernel cannot boot from RAM!\n");
429 struct mtd_info
*mtd_ram
;
431 mtd_ram
= (struct mtd_info
*)kmalloc(sizeof(struct mtd_info
),
434 panic("axisflashmap couldn't allocate memory for "
438 printk(KERN_INFO
" Adding RAM partition for romfs image:\n");
439 printk(pmsg
, pidx
, romfs_start
, romfs_length
);
441 err
= mtdram_init_device(mtd_ram
, (void*)romfs_start
,
442 romfs_length
, "romfs");
444 panic("axisflashmap could not initialize MTD RAM "
453 /* This adds the above to the kernels init-call chain. */
454 module_init(init_axis_flash
);
456 EXPORT_SYMBOL(axisflash_mtd
);