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/init.h>
22 #include <linux/slab.h>
24 #include <linux/mtd/concat.h>
25 #include <linux/mtd/map.h>
26 #include <linux/mtd/mtd.h>
27 #include <linux/mtd/mtdram.h>
28 #include <linux/mtd/partitions.h>
30 #include <asm/arch/hwregs/config_defs.h>
31 #include <asm/axisflashmap.h>
34 #define MEM_CSE0_SIZE (0x04000000)
35 #define MEM_CSE1_SIZE (0x04000000)
37 #define FLASH_UNCACHED_ADDR KSEG_E
38 #define FLASH_CACHED_ADDR KSEG_F
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 __u16
49 extern unsigned long romfs_start, romfs_length, romfs_in_flash;
51 /* The master mtd for the entire flash. */
52 struct mtd_info* axisflash_mtd = NULL;
54 /* Map driver functions. */
56 static map_word flash_read(struct map_info *map, unsigned long ofs)
59 tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);
63 static void flash_copy_from(struct map_info *map, void *to,
64 unsigned long from, ssize_t len)
66 memcpy(to, (void *)(map->map_priv_1 + from), len);
69 static void flash_write(struct map_info *map, map_word d, unsigned long adr)
71 *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];
75 * The map for chip select e0.
77 * We run into tricky coherence situations if we mix cached with uncached
78 * accesses to we only use the uncached version here.
80 * The size field is the total size where the flash chips may be mapped on the
81 * chip select. MTD probes should find all devices there and it does not matter
82 * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
83 * probes will ignore them.
85 * The start address in map_priv_1 is in virtual memory so we cannot use
86 * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
89 static struct map_info map_cse0 = {
91 .size = MEM_CSE0_SIZE,
92 .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
94 .copy_from = flash_copy_from,
96 .map_priv_1 = FLASH_UNCACHED_ADDR
100 * The map for chip select e1.
102 * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
103 * address, but there isn't.
105 static struct map_info map_cse1 = {
107 .size = MEM_CSE1_SIZE,
108 .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
110 .copy_from = flash_copy_from,
111 .write = flash_write,
112 .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE
115 /* If no partition-table was found, we use this default-set. */
116 #define MAX_PARTITIONS 7
117 #define NUM_DEFAULT_PARTITIONS 3
120 * Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the
121 * size of one flash block and "filesystem"-partition needs 5 blocks to be able
124 static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {
126 .name = "boot firmware",
127 .size = CONFIG_ETRAX_PTABLE_SECTOR,
132 .size = 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR),
133 .offset = CONFIG_ETRAX_PTABLE_SECTOR
136 .name = "filesystem",
137 .size = 5 * CONFIG_ETRAX_PTABLE_SECTOR,
138 .offset = 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR)
142 /* Initialize the ones normally used. */
143 static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {
146 .size = CONFIG_ETRAX_PTABLE_SECTOR,
182 * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
183 * chips in that order (because the amd_flash-driver is faster).
185 static struct mtd_info *probe_cs(struct map_info *map_cs)
187 struct mtd_info *mtd_cs = NULL;
190 "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
191 map_cs->name, map_cs->size, map_cs->map_priv_1);
193 #ifdef CONFIG_MTD_CFI
194 mtd_cs = do_map_probe("cfi_probe", map_cs);
196 #ifdef CONFIG_MTD_JEDECPROBE
198 mtd_cs = do_map_probe("jedec_probe", map_cs);
205 * Probe each chip select individually for flash chips. If there are chips on
206 * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
207 * so that MTD partitions can cross chip boundaries.
209 * The only known restriction to how you can mount your chips is that each
210 * chip select must hold similar flash chips. But you need external hardware
211 * to do that anyway and you can put totally different chips on cse0 and cse1
212 * so it isn't really much of a restriction.
214 extern struct mtd_info* __init crisv32_nand_flash_probe (void);
215 static struct mtd_info *flash_probe(void)
217 struct mtd_info *mtd_cse0;
218 struct mtd_info *mtd_cse1;
219 struct mtd_info *mtd_nand = NULL;
220 struct mtd_info *mtd_total;
221 struct mtd_info *mtds[3];
224 if ((mtd_cse0 = probe_cs(&map_cse0)) != NULL)
225 mtds[count++] = mtd_cse0;
226 if ((mtd_cse1 = probe_cs(&map_cse1)) != NULL)
227 mtds[count++] = mtd_cse1;
229 #ifdef CONFIG_ETRAX_NANDFLASH
230 if ((mtd_nand = crisv32_nand_flash_probe()) != NULL)
231 mtds[count++] = mtd_nand;
234 if (!mtd_cse0 && !mtd_cse1 && !mtd_nand) {
240 #ifdef CONFIG_MTD_CONCAT
241 /* Since the concatenation layer adds a small overhead we
242 * could try to figure out if the chips in cse0 and cse1 are
243 * identical and reprobe the whole cse0+cse1 window. But since
244 * flash chips are slow, the overhead is relatively small.
245 * So we use the MTD concatenation layer instead of further
246 * complicating the probing procedure.
248 mtd_total = mtd_concat_create(mtds,
252 printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel "
253 "(mis)configuration!\n", map_cse0.name, map_cse1.name);
257 printk(KERN_ERR "%s and %s: Concatenation failed!\n",
258 map_cse0.name, map_cse1.name);
260 /* The best we can do now is to only use what we found
263 mtd_total = mtd_cse0;
264 map_destroy(mtd_cse1);
267 mtd_total = mtd_cse0? mtd_cse0 : mtd_cse1 ? mtd_cse1 : mtd_nand;
274 extern unsigned long crisv32_nand_boot;
275 extern unsigned long crisv32_nand_cramfs_offset;
278 * Probe the flash chip(s) and, if it succeeds, read the partition-table
279 * and register the partitions with MTD.
281 static int __init init_axis_flash(void)
283 struct mtd_info *mymtd;
286 struct partitiontable_head *ptable_head = NULL;
287 struct partitiontable_entry *ptable;
288 int use_default_ptable = 1; /* Until proven otherwise. */
289 const char *pmsg = KERN_INFO " /dev/flash%d at 0x%08x, size 0x%08x\n";
290 static char page[512];
293 #ifndef CONFIG_ETRAXFS_SIM
294 mymtd = flash_probe();
295 mymtd->read(mymtd, CONFIG_ETRAX_PTABLE_SECTOR, 512, &len, page);
296 ptable_head = (struct partitiontable_head *)(page + PARTITION_TABLE_OFFSET);
299 /* There's no reason to use this module if no flash chip can
300 * be identified. Make sure that's understood.
302 printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
304 printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
305 mymtd->name, mymtd->size);
306 axisflash_mtd = mymtd;
310 mymtd->owner = THIS_MODULE;
312 pidx++; /* First partition is always set to the default. */
314 if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
315 && (ptable_head->size <
316 (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
317 PARTITIONTABLE_END_MARKER_SIZE))
318 && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
320 PARTITIONTABLE_END_MARKER_SIZE)
321 == PARTITIONTABLE_END_MARKER)) {
322 /* Looks like a start, sane length and end of a
323 * partition table, lets check csum etc.
326 struct partitiontable_entry *max_addr =
327 (struct partitiontable_entry *)
328 ((unsigned long)ptable_head + sizeof(*ptable_head) +
330 unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
332 unsigned long csum = 0;
334 ptable = (struct partitiontable_entry *)
335 ((unsigned long)ptable_head + sizeof(*ptable_head));
337 /* Lets be PARANOID, and check the checksum. */
338 p = (unsigned char*) ptable;
340 while (p <= (unsigned char*)max_addr) {
346 ptable_ok = (csum == ptable_head->checksum);
348 /* Read the entries and use/show the info. */
349 printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
350 (ptable_ok ? " valid" : "n invalid"), ptable_head,
353 /* We have found a working bootblock. Now read the
354 * partition table. Scan the table. It ends when
355 * there is 0xffffffff, that is, empty flash.
358 && ptable->offset != 0xffffffff
360 && pidx < MAX_PARTITIONS) {
362 axis_partitions[pidx].offset = offset + ptable->offset + (crisv32_nand_boot ? 16384 : 0);
363 axis_partitions[pidx].size = ptable->size;
365 printk(pmsg, pidx, axis_partitions[pidx].offset,
366 axis_partitions[pidx].size);
370 use_default_ptable = !ptable_ok;
373 if (romfs_in_flash) {
374 /* Add an overlapping device for the root partition (romfs). */
376 axis_partitions[pidx].name = "romfs";
377 if (crisv32_nand_boot) {
378 char* data = kmalloc(1024, GFP_KERNEL);
380 int offset = crisv32_nand_cramfs_offset & ~(1024-1);
383 mymtd->read(mymtd, offset, 1024, &len, data);
384 tmp = &data[crisv32_nand_cramfs_offset % 512];
385 axis_partitions[pidx].size = *(unsigned*)(tmp + 4);
386 axis_partitions[pidx].offset = crisv32_nand_cramfs_offset;
389 axis_partitions[pidx].size = romfs_length;
390 axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
393 axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
396 " Adding readonly flash partition for romfs image:\n");
397 printk(pmsg, pidx, axis_partitions[pidx].offset,
398 axis_partitions[pidx].size);
403 if (use_default_ptable) {
404 printk(KERN_INFO " Using default partition table.\n");
405 err = add_mtd_partitions(mymtd, axis_default_partitions,
406 NUM_DEFAULT_PARTITIONS);
408 err = add_mtd_partitions(mymtd, axis_partitions, pidx);
412 panic("axisflashmap could not add MTD partitions!\n");
415 /* CONFIG_EXTRAXFS_SIM */
418 if (!romfs_in_flash) {
419 /* Create an RAM device for the root partition (romfs). */
421 #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
422 /* No use trying to boot this kernel from RAM. Panic! */
423 printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
424 "device due to kernel (mis)configuration!\n");
425 panic("This kernel cannot boot from RAM!\n");
427 struct mtd_info *mtd_ram;
429 mtd_ram = kmalloc(sizeof(struct mtd_info),
432 panic("axisflashmap couldn't allocate memory for "
436 printk(KERN_INFO " Adding RAM partition for romfs image:\n");
437 printk(pmsg, pidx, romfs_start, romfs_length);
439 err = mtdram_init_device(mtd_ram, (void*)romfs_start,
440 romfs_length, "romfs");
442 panic("axisflashmap could not initialize MTD RAM "
451 /* This adds the above to the kernels init-call chain. */
452 module_init(init_axis_flash);
454 EXPORT_SYMBOL(axisflash_mtd);