axisflashmap.c 12 KB

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