1/* 2 * Physical mapping layer for MTD using the Axis partitiontable format 3 * 4 * Copyright (c) 2001-2007 Axis Communications AB 5 * 6 * This file is under the GPL. 7 * 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. 12 * 13 */ 14 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> 20 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> 26 27#include <linux/cramfs_fs.h> 28 29#include <asm/axisflashmap.h> 30#include <asm/mmu.h> 31 32#define MEM_CSE0_SIZE (0x04000000) 33#define MEM_CSE1_SIZE (0x04000000) 34 35#define FLASH_UNCACHED_ADDR KSEG_E 36#define FLASH_CACHED_ADDR KSEG_F 37 38#define PAGESIZE (512) 39 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 46#endif 47 48/* From head.S */ 49extern unsigned long romfs_in_flash; /* 1 when romfs_start, _length in flash */ 50extern unsigned long romfs_start, romfs_length; 51extern unsigned long nand_boot; /* 1 when booted from nand flash */ 52 53struct partition_name { 54 char name[6]; 55}; 56 57/* The master mtd for the entire flash. */ 58struct mtd_info* axisflash_mtd = NULL; 59 60/* Map driver functions. */ 61 62static map_word flash_read(struct map_info *map, unsigned long ofs) 63{ 64 map_word tmp; 65 tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs); 66 return tmp; 67} 68 69static void flash_copy_from(struct map_info *map, void *to, 70 unsigned long from, ssize_t len) 71{ 72 memcpy(to, (void *)(map->map_priv_1 + from), len); 73} 74 75static void flash_write(struct map_info *map, map_word d, unsigned long adr) 76{ 77 *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0]; 78} 79 80/* 81 * The map for chip select e0. 82 * 83 * We run into tricky coherence situations if we mix cached with uncached 84 * accesses to we only use the uncached version here. 85 * 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. 90 * 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 93 * address of cse0. 94 */ 95static struct map_info map_cse0 = { 96 .name = "cse0", 97 .size = MEM_CSE0_SIZE, 98 .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH, 99 .read = flash_read, 100 .copy_from = flash_copy_from, 101 .write = flash_write, 102 .map_priv_1 = FLASH_UNCACHED_ADDR 103}; 104 105/* 106 * The map for chip select e1. 107 * 108 * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong 109 * address, but there isn't. 110 */ 111static struct map_info map_cse1 = { 112 .name = "cse1", 113 .size = MEM_CSE1_SIZE, 114 .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH, 115 .read = flash_read, 116 .copy_from = flash_copy_from, 117 .write = flash_write, 118 .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE 119}; 120 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 */ 126#else 127#define NUM_DEFAULT_PARTITIONS 3 128#define DEFAULT_ROOTFS_PARTITION_NO (-1) 129#define DEFAULT_MEDIA_SIZE 0x800000 /* 8 megs */ 130#endif 131 132#if (MAX_PARTITIONS < NUM_DEFAULT_PARTITIONS) 133#error MAX_PARTITIONS must be >= than NUM_DEFAULT_PARTITIONS 134#endif 135 136/* Initialize the ones normally used. */ 137static struct mtd_partition axis_partitions[MAX_PARTITIONS] = { 138 { 139 .name = "part0", 140 .size = CONFIG_ETRAX_PTABLE_SECTOR, 141 .offset = 0 142 }, 143 { 144 .name = "part1", 145 .size = 0, 146 .offset = 0 147 }, 148 { 149 .name = "part2", 150 .size = 0, 151 .offset = 0 152 }, 153 { 154 .name = "part3", 155 .size = 0, 156 .offset = 0 157 }, 158 { 159 .name = "part4", 160 .size = 0, 161 .offset = 0 162 }, 163 { 164 .name = "part5", 165 .size = 0, 166 .offset = 0 167 }, 168 { 169 .name = "part6", 170 .size = 0, 171 .offset = 0 172 }, 173}; 174 175 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. 180 */ 181static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = { 182 { 183 .name = "boot firmware", 184 .size = CONFIG_ETRAX_PTABLE_SECTOR, 185 .offset = 0 186 }, 187 { 188 .name = "kernel", 189 .size = 10 * CONFIG_ETRAX_PTABLE_SECTOR, 190 .offset = CONFIG_ETRAX_PTABLE_SECTOR 191 }, 192#define FILESYSTEM_SECTOR (11 * CONFIG_ETRAX_PTABLE_SECTOR) 193#ifdef CONFIG_ETRAX_NANDBOOT 194 { 195 .name = "rootfs", 196 .size = 10 * CONFIG_ETRAX_PTABLE_SECTOR, 197 .offset = FILESYSTEM_SECTOR 198 }, 199#undef FILESYSTEM_SECTOR 200#define FILESYSTEM_SECTOR (21 * CONFIG_ETRAX_PTABLE_SECTOR) 201#endif 202 { 203 .name = "rwfs", 204 .size = DEFAULT_MEDIA_SIZE - FILESYSTEM_SECTOR, 205 .offset = FILESYSTEM_SECTOR 206 } 207}; 208 209#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE 210/* Main flash device */ 211static struct mtd_partition main_partition = { 212 .name = "main", 213 .size = 0, 214 .offset = 0 215}; 216#endif 217 218/* Auxilliary partition if we find another flash */ 219static struct mtd_partition aux_partition = { 220 .name = "aux", 221 .size = 0, 222 .offset = 0 223}; 224 225/* 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). 228 */ 229static struct mtd_info *probe_cs(struct map_info *map_cs) 230{ 231 struct mtd_info *mtd_cs = NULL; 232 233 printk(KERN_INFO 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); 236 237#ifdef CONFIG_MTD_CFI 238 mtd_cs = do_map_probe("cfi_probe", map_cs); 239#endif 240#ifdef CONFIG_MTD_JEDECPROBE 241 if (!mtd_cs) 242 mtd_cs = do_map_probe("jedec_probe", map_cs); 243#endif 244 245 return mtd_cs; 246} 247 248/* 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. 252 * 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. 257 */ 258extern struct mtd_info* __init crisv32_nand_flash_probe (void); 259static struct mtd_info *flash_probe(void) 260{ 261 struct mtd_info *mtd_cse0; 262 struct mtd_info *mtd_cse1; 263 struct mtd_info *mtd_total; 264 struct mtd_info *mtds[2]; 265 int count = 0; 266 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; 271 272 if (!mtd_cse0 && !mtd_cse1) { 273 /* No chip found. */ 274 return NULL; 275 } 276 277 if (count > 1) { 278#ifdef CONFIG_MTD_CONCAT 279 /* Since the concatenation layer adds a small overhead we 280 * could try to figure out if the chips in cse0 and cse1 are 281 * identical and reprobe the whole cse0+cse1 window. But since 282 * flash chips are slow, the overhead is relatively small. 283 * So we use the MTD concatenation layer instead of further 284 * complicating the probing procedure. 285 */ 286 mtd_total = mtd_concat_create(mtds, count, "cse0+cse1"); 287#else 288 printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel " 289 "(mis)configuration!\n", map_cse0.name, map_cse1.name); 290 mtd_toal = NULL; 291#endif 292 if (!mtd_total) { 293 printk(KERN_ERR "%s and %s: Concatenation failed!\n", 294 map_cse0.name, map_cse1.name); 295 296 /* The best we can do now is to only use what we found 297 * at cse0. */ 298 mtd_total = mtd_cse0; 299 map_destroy(mtd_cse1); 300 } 301 } else 302 mtd_total = mtd_cse0 ? mtd_cse0 : mtd_cse1; 303 304 return mtd_total; 305} 306 307/* 308 * Probe the flash chip(s) and, if it succeeds, read the partition-table 309 * and register the partitions with MTD. 310 */ 311static int __init init_axis_flash(void) 312{ 313 struct mtd_info *main_mtd; 314 struct mtd_info *aux_mtd = NULL; 315 int err = 0; 316 int pidx = 0; 317 struct partitiontable_head *ptable_head = NULL; 318 struct partitiontable_entry *ptable; 319 int ptable_ok = 0; 320 static char page[PAGESIZE]; 321 size_t len; 322 int ram_rootfs_partition = -1; /* -1 => no RAM rootfs partition */ 323 int part; 324 325 /* We need a root fs. If it resides in RAM, we need to use an 326 * MTDRAM device, so it must be enabled in the kernel config, 327 * but its size must be configured as 0 so as not to conflict 328 * with our usage. 329 */ 330#if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || \ 331 (CONFIG_MTDRAM_ABS_POS != 0) 332 if (!romfs_in_flash && !nand_boot) { 333 printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM " 334 "device; configure CONFIG_MTD_MTDRAM with size = 0!\n"); 335 panic("This kernel cannot boot from RAM!\n"); 336 } 337#endif 338 339#ifndef CONFIG_ETRAX_VCS_SIM 340 main_mtd = flash_probe(); 341 if (main_mtd) 342 printk(KERN_INFO "%s: 0x%08x bytes of NOR flash memory.\n", 343 main_mtd->name, main_mtd->size); 344 345#ifdef CONFIG_ETRAX_NANDFLASH 346 aux_mtd = crisv32_nand_flash_probe(); 347 if (aux_mtd) 348 printk(KERN_INFO "%s: 0x%08x bytes of NAND flash memory.\n", 349 aux_mtd->name, aux_mtd->size); 350 351#ifdef CONFIG_ETRAX_NANDBOOT 352 { 353 struct mtd_info *tmp_mtd; 354 355 printk(KERN_INFO "axisflashmap: Set to boot from NAND flash, " 356 "making NAND flash primary device.\n"); 357 tmp_mtd = main_mtd; 358 main_mtd = aux_mtd; 359 aux_mtd = tmp_mtd; 360 } 361#endif /* CONFIG_ETRAX_NANDBOOT */ 362#endif /* CONFIG_ETRAX_NANDFLASH */ 363 364 if (!main_mtd && !aux_mtd) { 365 /* There's no reason to use this module if no flash chip can 366 * be identified. Make sure that's understood. 367 */ 368 printk(KERN_INFO "axisflashmap: Found no flash chip.\n"); 369 } 370 371 372 if (main_mtd) { 373 main_mtd->owner = THIS_MODULE; 374 axisflash_mtd = main_mtd; 375 376 loff_t ptable_sector = CONFIG_ETRAX_PTABLE_SECTOR; 377 378 /* First partition (rescue) is always set to the default. */ 379 pidx++; 380#ifdef CONFIG_ETRAX_NANDBOOT 381 /* We know where the partition table should be located, 382 * it will be in first good block after that. 383 */ 384 int blockstat; 385 do { 386 blockstat = main_mtd->block_isbad(main_mtd, 387 ptable_sector); 388 if (blockstat < 0) 389 ptable_sector = 0; /* read error */ 390 else if (blockstat) 391 ptable_sector += main_mtd->erasesize; 392 } while (blockstat && ptable_sector); 393#endif 394 if (ptable_sector) { 395 main_mtd->read(main_mtd, ptable_sector, PAGESIZE, 396 &len, page); 397 ptable_head = &((struct partitiontable *) page)->head; 398 } 399 400 } 401 402 if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC) 403 && (ptable_head->size < 404 (MAX_PARTITIONS * sizeof(struct partitiontable_entry) + 405 PARTITIONTABLE_END_MARKER_SIZE)) 406 && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) + 407 ptable_head->size - 408 PARTITIONTABLE_END_MARKER_SIZE) 409 == PARTITIONTABLE_END_MARKER)) { 410 /* Looks like a start, sane length and end of a 411 * partition table, lets check csum etc. 412 */ 413 struct partitiontable_entry *max_addr = 414 (struct partitiontable_entry *) 415 ((unsigned long)ptable_head + sizeof(*ptable_head) + 416 ptable_head->size); 417 unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR; 418 unsigned char *p; 419 unsigned long csum = 0; 420 421 ptable = (struct partitiontable_entry *) 422 ((unsigned long)ptable_head + sizeof(*ptable_head)); 423 424 /* Lets be PARANOID, and check the checksum. */ 425 p = (unsigned char*) ptable; 426 427 while (p <= (unsigned char*)max_addr) { 428 csum += *p++; 429 csum += *p++; 430 csum += *p++; 431 csum += *p++; 432 } 433 ptable_ok = (csum == ptable_head->checksum); 434 435 /* Read the entries and use/show the info. */ 436 printk(KERN_INFO "axisflashmap: " 437 "Found a%s partition table at 0x%p-0x%p.\n", 438 (ptable_ok ? " valid" : "n invalid"), ptable_head, 439 max_addr); 440 441 /* We have found a working bootblock. Now read the 442 * partition table. Scan the table. It ends with 0xffffffff. 443 */ 444 while (ptable_ok 445 && ptable->offset != PARTITIONTABLE_END_MARKER 446 && ptable < max_addr 447 && pidx < MAX_PARTITIONS - 1) { 448 449 axis_partitions[pidx].offset = offset + ptable->offset; 450#ifdef CONFIG_ETRAX_NANDFLASH 451 if (main_mtd->type == MTD_NANDFLASH) { 452 axis_partitions[pidx].size = 453 (((ptable+1)->offset == 454 PARTITIONTABLE_END_MARKER) ? 455 main_mtd->size : 456 ((ptable+1)->offset + offset)) - 457 (ptable->offset + offset); 458 459 } else 460#endif /* CONFIG_ETRAX_NANDFLASH */ 461 axis_partitions[pidx].size = ptable->size; 462#ifdef CONFIG_ETRAX_NANDBOOT 463 /* Save partition number of jffs2 ro partition. 464 * Needed if RAM booting or root file system in RAM. 465 */ 466 if (!nand_boot && 467 ram_rootfs_partition < 0 && /* not already set */ 468 ptable->type == PARTITION_TYPE_JFFS2 && 469 (ptable->flags & PARTITION_FLAGS_READONLY_MASK) == 470 PARTITION_FLAGS_READONLY) 471 ram_rootfs_partition = pidx; 472#endif /* CONFIG_ETRAX_NANDBOOT */ 473 pidx++; 474 ptable++; 475 } 476 } 477 478 /* Decide whether to use default partition table. */ 479 /* Only use default table if we actually have a device (main_mtd) */ 480 481 struct mtd_partition *partition = &axis_partitions[0]; 482 if (main_mtd && !ptable_ok) { 483 memcpy(axis_partitions, axis_default_partitions, 484 sizeof(axis_default_partitions)); 485 pidx = NUM_DEFAULT_PARTITIONS; 486 ram_rootfs_partition = DEFAULT_ROOTFS_PARTITION_NO; 487 } 488 489 /* Add artificial partitions for rootfs if necessary */ 490 if (romfs_in_flash) { 491 /* rootfs is in directly accessible flash memory = NOR flash. 492 Add an overlapping device for the rootfs partition. */ 493 printk(KERN_INFO "axisflashmap: Adding partition for " 494 "overlapping root file system image\n"); 495 axis_partitions[pidx].size = romfs_length; 496 axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR; 497 axis_partitions[pidx].name = "romfs"; 498 axis_partitions[pidx].mask_flags |= MTD_WRITEABLE; 499 ram_rootfs_partition = -1; 500 pidx++; 501 } else if (romfs_length && !nand_boot) { 502 /* romfs exists in memory, but not in flash, so must be in RAM. 503 * Configure an MTDRAM partition. */ 504 if (ram_rootfs_partition < 0) { 505 /* None set yet, put it at the end */ 506 ram_rootfs_partition = pidx; 507 pidx++; 508 } 509 printk(KERN_INFO "axisflashmap: Adding partition for " 510 "root file system image in RAM\n"); 511 axis_partitions[ram_rootfs_partition].size = romfs_length; 512 axis_partitions[ram_rootfs_partition].offset = romfs_start; 513 axis_partitions[ram_rootfs_partition].name = "romfs"; 514 axis_partitions[ram_rootfs_partition].mask_flags |= 515 MTD_WRITEABLE; 516 } 517 518#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE 519 if (main_mtd) { 520 main_partition.size = main_mtd->size; 521 err = add_mtd_partitions(main_mtd, &main_partition, 1); 522 if (err) 523 panic("axisflashmap: Could not initialize " 524 "partition for whole main mtd device!\n"); 525 } 526#endif 527 528 /* Now, register all partitions with mtd. 529 * We do this one at a time so we can slip in an MTDRAM device 530 * in the proper place if required. */ 531 532 for (part = 0; part < pidx; part++) { 533 if (part == ram_rootfs_partition) { 534 /* add MTDRAM partition here */ 535 struct mtd_info *mtd_ram; 536 537 mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL); 538 if (!mtd_ram) 539 panic("axisflashmap: Couldn't allocate memory " 540 "for mtd_info!\n"); 541 printk(KERN_INFO "axisflashmap: Adding RAM partition " 542 "for rootfs image.\n"); 543 err = mtdram_init_device(mtd_ram, 544 (void *)partition[part].offset, 545 partition[part].size, 546 partition[part].name); 547 if (err) 548 panic("axisflashmap: Could not initialize " 549 "MTD RAM device!\n"); 550 /* JFFS2 likes to have an erasesize. Keep potential 551 * JFFS2 rootfs happy by providing one. Since image 552 * was most likely created for main mtd, use that 553 * erasesize, if available. Otherwise, make a guess. */ 554 mtd_ram->erasesize = (main_mtd ? main_mtd->erasesize : 555 CONFIG_ETRAX_PTABLE_SECTOR); 556 } else { 557 err = add_mtd_partitions(main_mtd, &partition[part], 1); 558 if (err) 559 panic("axisflashmap: Could not add mtd " 560 "partition %d\n", part); 561 } 562 } 563#endif /* CONFIG_EXTRAX_VCS_SIM */ 564 565#ifdef CONFIG_ETRAX_VCS_SIM 566 /* For simulator, always use a RAM partition. 567 * The rootfs will be found after the kernel in RAM, 568 * with romfs_start and romfs_end indicating location and size. 569 */ 570 struct mtd_info *mtd_ram; 571 572 mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL); 573 if (!mtd_ram) { 574 panic("axisflashmap: Couldn't allocate memory for " 575 "mtd_info!\n"); 576 } 577 578 printk(KERN_INFO "axisflashmap: Adding RAM partition for romfs, " 579 "at %u, size %u\n", 580 (unsigned) romfs_start, (unsigned) romfs_length); 581 582 err = mtdram_init_device(mtd_ram, (void *)romfs_start, 583 romfs_length, "romfs"); 584 if (err) { 585 panic("axisflashmap: Could not initialize MTD RAM " 586 "device!\n"); 587 } 588#endif /* CONFIG_EXTRAX_VCS_SIM */ 589 590#ifndef CONFIG_ETRAX_VCS_SIM 591 if (aux_mtd) { 592 aux_partition.size = aux_mtd->size; 593 err = add_mtd_partitions(aux_mtd, &aux_partition, 1); 594 if (err) 595 panic("axisflashmap: Could not initialize " 596 "aux mtd device!\n"); 597 598 } 599#endif /* CONFIG_EXTRAX_VCS_SIM */ 600 601 return err; 602} 603 604/* This adds the above to the kernels init-call chain. */ 605module_init(init_axis_flash); 606 607EXPORT_SYMBOL(axisflash_mtd); 608