axisflashmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. * Physical mapping layer for MTD using the Axis partitiontable format
  3. *
  4. * Copyright (c) 2001, 2002, 2003 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. * Copy of os/lx25/arch/cris/arch-v10/drivers/axisflashmap.c 1.5
  14. * with minor changes.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/config.h>
  21. #include <linux/init.h>
  22. #include <linux/slab.h>
  23. #include <linux/mtd/concat.h>
  24. #include <linux/mtd/map.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/mtd/mtdram.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <asm/arch/hwregs/config_defs.h>
  29. #include <asm/axisflashmap.h>
  30. #include <asm/mmu.h>
  31. #define MEM_CSE0_SIZE (0x04000000)
  32. #define MEM_CSE1_SIZE (0x04000000)
  33. #define FLASH_UNCACHED_ADDR KSEG_E
  34. #define FLASH_CACHED_ADDR KSEG_F
  35. #if CONFIG_ETRAX_FLASH_BUSWIDTH==1
  36. #define flash_data __u8
  37. #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
  38. #define flash_data __u16
  39. #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
  40. #define flash_data __u16
  41. #endif
  42. /* From head.S */
  43. extern unsigned long romfs_start, romfs_length, romfs_in_flash;
  44. /* The master mtd for the entire flash. */
  45. struct mtd_info* axisflash_mtd = NULL;
  46. /* Map driver functions. */
  47. static map_word flash_read(struct map_info *map, unsigned long ofs)
  48. {
  49. map_word tmp;
  50. tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);
  51. return tmp;
  52. }
  53. static void flash_copy_from(struct map_info *map, void *to,
  54. unsigned long from, ssize_t len)
  55. {
  56. memcpy(to, (void *)(map->map_priv_1 + from), len);
  57. }
  58. static void flash_write(struct map_info *map, map_word d, unsigned long adr)
  59. {
  60. *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];
  61. }
  62. /*
  63. * The map for chip select e0.
  64. *
  65. * We run into tricky coherence situations if we mix cached with uncached
  66. * accesses to we only use the uncached version here.
  67. *
  68. * The size field is the total size where the flash chips may be mapped on the
  69. * chip select. MTD probes should find all devices there and it does not matter
  70. * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
  71. * probes will ignore them.
  72. *
  73. * The start address in map_priv_1 is in virtual memory so we cannot use
  74. * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
  75. * address of cse0.
  76. */
  77. static struct map_info map_cse0 = {
  78. .name = "cse0",
  79. .size = MEM_CSE0_SIZE,
  80. .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
  81. .read = flash_read,
  82. .copy_from = flash_copy_from,
  83. .write = flash_write,
  84. .map_priv_1 = FLASH_UNCACHED_ADDR
  85. };
  86. /*
  87. * The map for chip select e1.
  88. *
  89. * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
  90. * address, but there isn't.
  91. */
  92. static struct map_info map_cse1 = {
  93. .name = "cse1",
  94. .size = MEM_CSE1_SIZE,
  95. .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
  96. .read = flash_read,
  97. .copy_from = flash_copy_from,
  98. .write = flash_write,
  99. .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE
  100. };
  101. /* If no partition-table was found, we use this default-set. */
  102. #define MAX_PARTITIONS 7
  103. #define NUM_DEFAULT_PARTITIONS 3
  104. /*
  105. * Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the
  106. * size of one flash block and "filesystem"-partition needs 5 blocks to be able
  107. * to use JFFS.
  108. */
  109. static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {
  110. {
  111. .name = "boot firmware",
  112. .size = CONFIG_ETRAX_PTABLE_SECTOR,
  113. .offset = 0
  114. },
  115. {
  116. .name = "kernel",
  117. .size = 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR),
  118. .offset = CONFIG_ETRAX_PTABLE_SECTOR
  119. },
  120. {
  121. .name = "filesystem",
  122. .size = 5 * CONFIG_ETRAX_PTABLE_SECTOR,
  123. .offset = 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR)
  124. }
  125. };
  126. /* Initialize the ones normally used. */
  127. static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {
  128. {
  129. .name = "part0",
  130. .size = CONFIG_ETRAX_PTABLE_SECTOR,
  131. .offset = 0
  132. },
  133. {
  134. .name = "part1",
  135. .size = 0,
  136. .offset = 0
  137. },
  138. {
  139. .name = "part2",
  140. .size = 0,
  141. .offset = 0
  142. },
  143. {
  144. .name = "part3",
  145. .size = 0,
  146. .offset = 0
  147. },
  148. {
  149. .name = "part4",
  150. .size = 0,
  151. .offset = 0
  152. },
  153. {
  154. .name = "part5",
  155. .size = 0,
  156. .offset = 0
  157. },
  158. {
  159. .name = "part6",
  160. .size = 0,
  161. .offset = 0
  162. },
  163. };
  164. /*
  165. * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
  166. * chips in that order (because the amd_flash-driver is faster).
  167. */
  168. static struct mtd_info *probe_cs(struct map_info *map_cs)
  169. {
  170. struct mtd_info *mtd_cs = NULL;
  171. printk(KERN_INFO
  172. "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
  173. map_cs->name, map_cs->size, map_cs->map_priv_1);
  174. #ifdef CONFIG_MTD_AMDSTD
  175. mtd_cs = do_map_probe("amd_flash", map_cs);
  176. #endif
  177. #ifdef CONFIG_MTD_CFI
  178. if (!mtd_cs) {
  179. mtd_cs = do_map_probe("cfi_probe", map_cs);
  180. }
  181. #endif
  182. return mtd_cs;
  183. }
  184. /*
  185. * Probe each chip select individually for flash chips. If there are chips on
  186. * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
  187. * so that MTD partitions can cross chip boundries.
  188. *
  189. * The only known restriction to how you can mount your chips is that each
  190. * chip select must hold similar flash chips. But you need external hardware
  191. * to do that anyway and you can put totally different chips on cse0 and cse1
  192. * so it isn't really much of a restriction.
  193. */
  194. extern struct mtd_info* __init crisv32_nand_flash_probe (void);
  195. static struct mtd_info *flash_probe(void)
  196. {
  197. struct mtd_info *mtd_cse0;
  198. struct mtd_info *mtd_cse1;
  199. struct mtd_info *mtd_nand = NULL;
  200. struct mtd_info *mtd_total;
  201. struct mtd_info *mtds[3];
  202. int count = 0;
  203. if ((mtd_cse0 = probe_cs(&map_cse0)) != NULL)
  204. mtds[count++] = mtd_cse0;
  205. if ((mtd_cse1 = probe_cs(&map_cse1)) != NULL)
  206. mtds[count++] = mtd_cse1;
  207. #ifdef CONFIG_ETRAX_NANDFLASH
  208. if ((mtd_nand = crisv32_nand_flash_probe()) != NULL)
  209. mtds[count++] = mtd_nand;
  210. #endif
  211. if (!mtd_cse0 && !mtd_cse1 && !mtd_nand) {
  212. /* No chip found. */
  213. return NULL;
  214. }
  215. if (count > 1) {
  216. #ifdef CONFIG_MTD_CONCAT
  217. /* Since the concatenation layer adds a small overhead we
  218. * could try to figure out if the chips in cse0 and cse1 are
  219. * identical and reprobe the whole cse0+cse1 window. But since
  220. * flash chips are slow, the overhead is relatively small.
  221. * So we use the MTD concatenation layer instead of further
  222. * complicating the probing procedure.
  223. */
  224. mtd_total = mtd_concat_create(mtds,
  225. count,
  226. "cse0+cse1+nand");
  227. #else
  228. printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel "
  229. "(mis)configuration!\n", map_cse0.name, map_cse1.name);
  230. mtd_toal = NULL;
  231. #endif
  232. if (!mtd_total) {
  233. printk(KERN_ERR "%s and %s: Concatenation failed!\n",
  234. map_cse0.name, map_cse1.name);
  235. /* The best we can do now is to only use what we found
  236. * at cse0.
  237. */
  238. mtd_total = mtd_cse0;
  239. map_destroy(mtd_cse1);
  240. }
  241. } else {
  242. mtd_total = mtd_cse0? mtd_cse0 : mtd_cse1 ? mtd_cse1 : mtd_nand;
  243. }
  244. return mtd_total;
  245. }
  246. extern unsigned long crisv32_nand_boot;
  247. extern unsigned long crisv32_nand_cramfs_offset;
  248. /*
  249. * Probe the flash chip(s) and, if it succeeds, read the partition-table
  250. * and register the partitions with MTD.
  251. */
  252. static int __init init_axis_flash(void)
  253. {
  254. struct mtd_info *mymtd;
  255. int err = 0;
  256. int pidx = 0;
  257. struct partitiontable_head *ptable_head = NULL;
  258. struct partitiontable_entry *ptable;
  259. int use_default_ptable = 1; /* Until proven otherwise. */
  260. const char *pmsg = KERN_INFO " /dev/flash%d at 0x%08x, size 0x%08x\n";
  261. static char page[512];
  262. size_t len;
  263. #ifndef CONFIG_ETRAXFS_SIM
  264. mymtd = flash_probe();
  265. mymtd->read(mymtd, CONFIG_ETRAX_PTABLE_SECTOR, 512, &len, page);
  266. ptable_head = (struct partitiontable_head *)(page + PARTITION_TABLE_OFFSET);
  267. if (!mymtd) {
  268. /* There's no reason to use this module if no flash chip can
  269. * be identified. Make sure that's understood.
  270. */
  271. printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
  272. } else {
  273. printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
  274. mymtd->name, mymtd->size);
  275. axisflash_mtd = mymtd;
  276. }
  277. if (mymtd) {
  278. mymtd->owner = THIS_MODULE;
  279. }
  280. pidx++; /* First partition is always set to the default. */
  281. if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
  282. && (ptable_head->size <
  283. (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
  284. PARTITIONTABLE_END_MARKER_SIZE))
  285. && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
  286. ptable_head->size -
  287. PARTITIONTABLE_END_MARKER_SIZE)
  288. == PARTITIONTABLE_END_MARKER)) {
  289. /* Looks like a start, sane length and end of a
  290. * partition table, lets check csum etc.
  291. */
  292. int ptable_ok = 0;
  293. struct partitiontable_entry *max_addr =
  294. (struct partitiontable_entry *)
  295. ((unsigned long)ptable_head + sizeof(*ptable_head) +
  296. ptable_head->size);
  297. unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
  298. unsigned char *p;
  299. unsigned long csum = 0;
  300. ptable = (struct partitiontable_entry *)
  301. ((unsigned long)ptable_head + sizeof(*ptable_head));
  302. /* Lets be PARANOID, and check the checksum. */
  303. p = (unsigned char*) ptable;
  304. while (p <= (unsigned char*)max_addr) {
  305. csum += *p++;
  306. csum += *p++;
  307. csum += *p++;
  308. csum += *p++;
  309. }
  310. ptable_ok = (csum == ptable_head->checksum);
  311. /* Read the entries and use/show the info. */
  312. printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
  313. (ptable_ok ? " valid" : "n invalid"), ptable_head,
  314. max_addr);
  315. /* We have found a working bootblock. Now read the
  316. * partition table. Scan the table. It ends when
  317. * there is 0xffffffff, that is, empty flash.
  318. */
  319. while (ptable_ok
  320. && ptable->offset != 0xffffffff
  321. && ptable < max_addr
  322. && pidx < MAX_PARTITIONS) {
  323. axis_partitions[pidx].offset = offset + ptable->offset + (crisv32_nand_boot ? 16384 : 0);
  324. axis_partitions[pidx].size = ptable->size;
  325. printk(pmsg, pidx, axis_partitions[pidx].offset,
  326. axis_partitions[pidx].size);
  327. pidx++;
  328. ptable++;
  329. }
  330. use_default_ptable = !ptable_ok;
  331. }
  332. if (romfs_in_flash) {
  333. /* Add an overlapping device for the root partition (romfs). */
  334. axis_partitions[pidx].name = "romfs";
  335. if (crisv32_nand_boot) {
  336. char* data = kmalloc(1024, GFP_KERNEL);
  337. int len;
  338. int offset = crisv32_nand_cramfs_offset & ~(1024-1);
  339. char* tmp;
  340. mymtd->read(mymtd, offset, 1024, &len, data);
  341. tmp = &data[crisv32_nand_cramfs_offset % 512];
  342. axis_partitions[pidx].size = *(unsigned*)(tmp + 4);
  343. axis_partitions[pidx].offset = crisv32_nand_cramfs_offset;
  344. kfree(data);
  345. } else {
  346. axis_partitions[pidx].size = romfs_length;
  347. axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
  348. }
  349. axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
  350. printk(KERN_INFO
  351. " Adding readonly flash partition for romfs image:\n");
  352. printk(pmsg, pidx, axis_partitions[pidx].offset,
  353. axis_partitions[pidx].size);
  354. pidx++;
  355. }
  356. if (mymtd) {
  357. if (use_default_ptable) {
  358. printk(KERN_INFO " Using default partition table.\n");
  359. err = add_mtd_partitions(mymtd, axis_default_partitions,
  360. NUM_DEFAULT_PARTITIONS);
  361. } else {
  362. err = add_mtd_partitions(mymtd, axis_partitions, pidx);
  363. }
  364. if (err) {
  365. panic("axisflashmap could not add MTD partitions!\n");
  366. }
  367. }
  368. /* CONFIG_EXTRAXFS_SIM */
  369. #endif
  370. if (!romfs_in_flash) {
  371. /* Create an RAM device for the root partition (romfs). */
  372. #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
  373. /* No use trying to boot this kernel from RAM. Panic! */
  374. printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
  375. "device due to kernel (mis)configuration!\n");
  376. panic("This kernel cannot boot from RAM!\n");
  377. #else
  378. struct mtd_info *mtd_ram;
  379. mtd_ram = (struct mtd_info *)kmalloc(sizeof(struct mtd_info),
  380. GFP_KERNEL);
  381. if (!mtd_ram) {
  382. panic("axisflashmap couldn't allocate memory for "
  383. "mtd_info!\n");
  384. }
  385. printk(KERN_INFO " Adding RAM partition for romfs image:\n");
  386. printk(pmsg, pidx, romfs_start, romfs_length);
  387. err = mtdram_init_device(mtd_ram, (void*)romfs_start,
  388. romfs_length, "romfs");
  389. if (err) {
  390. panic("axisflashmap could not initialize MTD RAM "
  391. "device!\n");
  392. }
  393. #endif
  394. }
  395. return err;
  396. }
  397. /* This adds the above to the kernels init-call chain. */
  398. module_init(init_axis_flash);
  399. EXPORT_SYMBOL(axisflash_mtd);