axisflashmap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 <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. struct mtd_info *mtds[] = { mtd_cse0, mtd_cse1 };
  213. /* Since the concatenation layer adds a small overhead we
  214. * could try to figure out if the chips in cse0 and cse1 are
  215. * identical and reprobe the whole cse0+cse1 window. But since
  216. * flash chips are slow, the overhead is relatively small.
  217. * So we use the MTD concatenation layer instead of further
  218. * complicating the probing procedure.
  219. */
  220. mtd_cse = mtd_concat_create(mtds, ARRAY_SIZE(mtds),
  221. "cse0+cse1");
  222. if (!mtd_cse) {
  223. printk(KERN_ERR "%s and %s: Concatenation failed!\n",
  224. map_cse0.name, map_cse1.name);
  225. /* The best we can do now is to only use what we found
  226. * at cse0.
  227. */
  228. mtd_cse = mtd_cse0;
  229. map_destroy(mtd_cse1);
  230. }
  231. } else {
  232. mtd_cse = mtd_cse0? mtd_cse0 : mtd_cse1;
  233. }
  234. return mtd_cse;
  235. }
  236. /*
  237. * Probe the flash chip(s) and, if it succeeds, read the partition-table
  238. * and register the partitions with MTD.
  239. */
  240. static int __init init_axis_flash(void)
  241. {
  242. struct mtd_info *mymtd;
  243. int err = 0;
  244. int pidx = 0;
  245. struct partitiontable_head *ptable_head = NULL;
  246. struct partitiontable_entry *ptable;
  247. int use_default_ptable = 1; /* Until proven otherwise. */
  248. const char pmsg[] = " /dev/flash%d at 0x%08x, size 0x%08x\n";
  249. if (!(mymtd = flash_probe())) {
  250. /* There's no reason to use this module if no flash chip can
  251. * be identified. Make sure that's understood.
  252. */
  253. printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
  254. } else {
  255. printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
  256. mymtd->name, mymtd->size);
  257. axisflash_mtd = mymtd;
  258. }
  259. if (mymtd) {
  260. mymtd->owner = THIS_MODULE;
  261. ptable_head = (struct partitiontable_head *)(FLASH_CACHED_ADDR +
  262. CONFIG_ETRAX_PTABLE_SECTOR +
  263. PARTITION_TABLE_OFFSET);
  264. }
  265. pidx++; /* First partition is always set to the default. */
  266. if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
  267. && (ptable_head->size <
  268. (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
  269. PARTITIONTABLE_END_MARKER_SIZE))
  270. && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
  271. ptable_head->size -
  272. PARTITIONTABLE_END_MARKER_SIZE)
  273. == PARTITIONTABLE_END_MARKER)) {
  274. /* Looks like a start, sane length and end of a
  275. * partition table, lets check csum etc.
  276. */
  277. int ptable_ok = 0;
  278. struct partitiontable_entry *max_addr =
  279. (struct partitiontable_entry *)
  280. ((unsigned long)ptable_head + sizeof(*ptable_head) +
  281. ptable_head->size);
  282. unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
  283. unsigned char *p;
  284. unsigned long csum = 0;
  285. ptable = (struct partitiontable_entry *)
  286. ((unsigned long)ptable_head + sizeof(*ptable_head));
  287. /* Lets be PARANOID, and check the checksum. */
  288. p = (unsigned char*) ptable;
  289. while (p <= (unsigned char*)max_addr) {
  290. csum += *p++;
  291. csum += *p++;
  292. csum += *p++;
  293. csum += *p++;
  294. }
  295. ptable_ok = (csum == ptable_head->checksum);
  296. /* Read the entries and use/show the info. */
  297. printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
  298. (ptable_ok ? " valid" : "n invalid"), ptable_head,
  299. max_addr);
  300. /* We have found a working bootblock. Now read the
  301. * partition table. Scan the table. It ends when
  302. * there is 0xffffffff, that is, empty flash.
  303. */
  304. while (ptable_ok
  305. && ptable->offset != 0xffffffff
  306. && ptable < max_addr
  307. && pidx < MAX_PARTITIONS) {
  308. axis_partitions[pidx].offset = offset + ptable->offset;
  309. axis_partitions[pidx].size = ptable->size;
  310. printk(pmsg, pidx, axis_partitions[pidx].offset,
  311. axis_partitions[pidx].size);
  312. pidx++;
  313. ptable++;
  314. }
  315. use_default_ptable = !ptable_ok;
  316. }
  317. if (romfs_in_flash) {
  318. /* Add an overlapping device for the root partition (romfs). */
  319. axis_partitions[pidx].name = "romfs";
  320. axis_partitions[pidx].size = romfs_length;
  321. axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
  322. axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
  323. printk(KERN_INFO
  324. " Adding readonly flash partition for romfs image:\n");
  325. printk(pmsg, pidx, axis_partitions[pidx].offset,
  326. axis_partitions[pidx].size);
  327. pidx++;
  328. }
  329. #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
  330. if (mymtd) {
  331. main_partition.size = mymtd->size;
  332. err = mtd_device_register(mymtd, &main_partition, 1);
  333. if (err)
  334. panic("axisflashmap: Could not initialize "
  335. "partition for whole main mtd device!\n");
  336. }
  337. #endif
  338. if (mymtd) {
  339. if (use_default_ptable) {
  340. printk(KERN_INFO " Using default partition table.\n");
  341. err = mtd_device_register(mymtd,
  342. axis_default_partitions,
  343. NUM_DEFAULT_PARTITIONS);
  344. } else {
  345. err = mtd_device_register(mymtd, axis_partitions,
  346. pidx);
  347. }
  348. if (err)
  349. panic("axisflashmap could not add MTD partitions!\n");
  350. }
  351. if (!romfs_in_flash) {
  352. /* Create an RAM device for the root partition (romfs). */
  353. #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
  354. /* No use trying to boot this kernel from RAM. Panic! */
  355. printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
  356. "device due to kernel (mis)configuration!\n");
  357. panic("This kernel cannot boot from RAM!\n");
  358. #else
  359. struct mtd_info *mtd_ram;
  360. mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
  361. if (!mtd_ram)
  362. panic("axisflashmap couldn't allocate memory for "
  363. "mtd_info!\n");
  364. printk(KERN_INFO " Adding RAM partition for romfs image:\n");
  365. printk(pmsg, pidx, (unsigned)romfs_start,
  366. (unsigned)romfs_length);
  367. err = mtdram_init_device(mtd_ram,
  368. (void *)romfs_start,
  369. romfs_length,
  370. "romfs");
  371. if (err)
  372. panic("axisflashmap could not initialize MTD RAM "
  373. "device!\n");
  374. #endif
  375. }
  376. return err;
  377. }
  378. /* This adds the above to the kernels init-call chain. */
  379. module_init(init_axis_flash);
  380. EXPORT_SYMBOL(axisflash_mtd);