axisflashmap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/mtd/concat.h>
  23. #include <linux/mtd/map.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/mtdram.h>
  26. #include <linux/mtd/partitions.h>
  27. #include <asm/arch/hwregs/config_defs.h>
  28. #include <asm/axisflashmap.h>
  29. #include <asm/mmu.h>
  30. #define MEM_CSE0_SIZE (0x04000000)
  31. #define MEM_CSE1_SIZE (0x04000000)
  32. #define FLASH_UNCACHED_ADDR KSEG_E
  33. #define FLASH_CACHED_ADDR KSEG_F
  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 __u16
  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. /*
  164. * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
  165. * chips in that order (because the amd_flash-driver is faster).
  166. */
  167. static struct mtd_info *probe_cs(struct map_info *map_cs)
  168. {
  169. struct mtd_info *mtd_cs = NULL;
  170. printk(KERN_INFO
  171. "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
  172. map_cs->name, map_cs->size, map_cs->map_priv_1);
  173. #ifdef CONFIG_MTD_AMDSTD
  174. mtd_cs = do_map_probe("amd_flash", map_cs);
  175. #endif
  176. #ifdef CONFIG_MTD_CFI
  177. if (!mtd_cs) {
  178. mtd_cs = do_map_probe("cfi_probe", map_cs);
  179. }
  180. #endif
  181. return mtd_cs;
  182. }
  183. /*
  184. * Probe each chip select individually for flash chips. If there are chips on
  185. * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
  186. * so that MTD partitions can cross chip boundries.
  187. *
  188. * The only known restriction to how you can mount your chips is that each
  189. * chip select must hold similar flash chips. But you need external hardware
  190. * to do that anyway and you can put totally different chips on cse0 and cse1
  191. * so it isn't really much of a restriction.
  192. */
  193. extern struct mtd_info* __init crisv32_nand_flash_probe (void);
  194. static struct mtd_info *flash_probe(void)
  195. {
  196. struct mtd_info *mtd_cse0;
  197. struct mtd_info *mtd_cse1;
  198. struct mtd_info *mtd_nand = NULL;
  199. struct mtd_info *mtd_total;
  200. struct mtd_info *mtds[3];
  201. int count = 0;
  202. if ((mtd_cse0 = probe_cs(&map_cse0)) != NULL)
  203. mtds[count++] = mtd_cse0;
  204. if ((mtd_cse1 = probe_cs(&map_cse1)) != NULL)
  205. mtds[count++] = mtd_cse1;
  206. #ifdef CONFIG_ETRAX_NANDFLASH
  207. if ((mtd_nand = crisv32_nand_flash_probe()) != NULL)
  208. mtds[count++] = mtd_nand;
  209. #endif
  210. if (!mtd_cse0 && !mtd_cse1 && !mtd_nand) {
  211. /* No chip found. */
  212. return NULL;
  213. }
  214. if (count > 1) {
  215. #ifdef CONFIG_MTD_CONCAT
  216. /* Since the concatenation layer adds a small overhead we
  217. * could try to figure out if the chips in cse0 and cse1 are
  218. * identical and reprobe the whole cse0+cse1 window. But since
  219. * flash chips are slow, the overhead is relatively small.
  220. * So we use the MTD concatenation layer instead of further
  221. * complicating the probing procedure.
  222. */
  223. mtd_total = mtd_concat_create(mtds,
  224. count,
  225. "cse0+cse1+nand");
  226. #else
  227. printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel "
  228. "(mis)configuration!\n", map_cse0.name, map_cse1.name);
  229. mtd_toal = NULL;
  230. #endif
  231. if (!mtd_total) {
  232. printk(KERN_ERR "%s and %s: Concatenation failed!\n",
  233. map_cse0.name, map_cse1.name);
  234. /* The best we can do now is to only use what we found
  235. * at cse0.
  236. */
  237. mtd_total = mtd_cse0;
  238. map_destroy(mtd_cse1);
  239. }
  240. } else {
  241. mtd_total = mtd_cse0? mtd_cse0 : mtd_cse1 ? mtd_cse1 : mtd_nand;
  242. }
  243. return mtd_total;
  244. }
  245. extern unsigned long crisv32_nand_boot;
  246. extern unsigned long crisv32_nand_cramfs_offset;
  247. /*
  248. * Probe the flash chip(s) and, if it succeeds, read the partition-table
  249. * and register the partitions with MTD.
  250. */
  251. static int __init init_axis_flash(void)
  252. {
  253. struct mtd_info *mymtd;
  254. int err = 0;
  255. int pidx = 0;
  256. struct partitiontable_head *ptable_head = NULL;
  257. struct partitiontable_entry *ptable;
  258. int use_default_ptable = 1; /* Until proven otherwise. */
  259. const char *pmsg = KERN_INFO " /dev/flash%d at 0x%08x, size 0x%08x\n";
  260. static char page[512];
  261. size_t len;
  262. #ifndef CONFIG_ETRAXFS_SIM
  263. mymtd = flash_probe();
  264. mymtd->read(mymtd, CONFIG_ETRAX_PTABLE_SECTOR, 512, &len, page);
  265. ptable_head = (struct partitiontable_head *)(page + PARTITION_TABLE_OFFSET);
  266. if (!mymtd) {
  267. /* There's no reason to use this module if no flash chip can
  268. * be identified. Make sure that's understood.
  269. */
  270. printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
  271. } else {
  272. printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
  273. mymtd->name, mymtd->size);
  274. axisflash_mtd = mymtd;
  275. }
  276. if (mymtd) {
  277. mymtd->owner = THIS_MODULE;
  278. }
  279. pidx++; /* First partition is always set to the default. */
  280. if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
  281. && (ptable_head->size <
  282. (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
  283. PARTITIONTABLE_END_MARKER_SIZE))
  284. && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
  285. ptable_head->size -
  286. PARTITIONTABLE_END_MARKER_SIZE)
  287. == PARTITIONTABLE_END_MARKER)) {
  288. /* Looks like a start, sane length and end of a
  289. * partition table, lets check csum etc.
  290. */
  291. int ptable_ok = 0;
  292. struct partitiontable_entry *max_addr =
  293. (struct partitiontable_entry *)
  294. ((unsigned long)ptable_head + sizeof(*ptable_head) +
  295. ptable_head->size);
  296. unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
  297. unsigned char *p;
  298. unsigned long csum = 0;
  299. ptable = (struct partitiontable_entry *)
  300. ((unsigned long)ptable_head + sizeof(*ptable_head));
  301. /* Lets be PARANOID, and check the checksum. */
  302. p = (unsigned char*) ptable;
  303. while (p <= (unsigned char*)max_addr) {
  304. csum += *p++;
  305. csum += *p++;
  306. csum += *p++;
  307. csum += *p++;
  308. }
  309. ptable_ok = (csum == ptable_head->checksum);
  310. /* Read the entries and use/show the info. */
  311. printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
  312. (ptable_ok ? " valid" : "n invalid"), ptable_head,
  313. max_addr);
  314. /* We have found a working bootblock. Now read the
  315. * partition table. Scan the table. It ends when
  316. * there is 0xffffffff, that is, empty flash.
  317. */
  318. while (ptable_ok
  319. && ptable->offset != 0xffffffff
  320. && ptable < max_addr
  321. && pidx < MAX_PARTITIONS) {
  322. axis_partitions[pidx].offset = offset + ptable->offset + (crisv32_nand_boot ? 16384 : 0);
  323. axis_partitions[pidx].size = ptable->size;
  324. printk(pmsg, pidx, axis_partitions[pidx].offset,
  325. axis_partitions[pidx].size);
  326. pidx++;
  327. ptable++;
  328. }
  329. use_default_ptable = !ptable_ok;
  330. }
  331. if (romfs_in_flash) {
  332. /* Add an overlapping device for the root partition (romfs). */
  333. axis_partitions[pidx].name = "romfs";
  334. if (crisv32_nand_boot) {
  335. char* data = kmalloc(1024, GFP_KERNEL);
  336. int len;
  337. int offset = crisv32_nand_cramfs_offset & ~(1024-1);
  338. char* tmp;
  339. mymtd->read(mymtd, offset, 1024, &len, data);
  340. tmp = &data[crisv32_nand_cramfs_offset % 512];
  341. axis_partitions[pidx].size = *(unsigned*)(tmp + 4);
  342. axis_partitions[pidx].offset = crisv32_nand_cramfs_offset;
  343. kfree(data);
  344. } else {
  345. axis_partitions[pidx].size = romfs_length;
  346. axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
  347. }
  348. axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
  349. printk(KERN_INFO
  350. " Adding readonly flash partition for romfs image:\n");
  351. printk(pmsg, pidx, axis_partitions[pidx].offset,
  352. axis_partitions[pidx].size);
  353. pidx++;
  354. }
  355. if (mymtd) {
  356. if (use_default_ptable) {
  357. printk(KERN_INFO " Using default partition table.\n");
  358. err = add_mtd_partitions(mymtd, axis_default_partitions,
  359. NUM_DEFAULT_PARTITIONS);
  360. } else {
  361. err = add_mtd_partitions(mymtd, axis_partitions, pidx);
  362. }
  363. if (err) {
  364. panic("axisflashmap could not add MTD partitions!\n");
  365. }
  366. }
  367. /* CONFIG_EXTRAXFS_SIM */
  368. #endif
  369. if (!romfs_in_flash) {
  370. /* Create an RAM device for the root partition (romfs). */
  371. #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
  372. /* No use trying to boot this kernel from RAM. Panic! */
  373. printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
  374. "device due to kernel (mis)configuration!\n");
  375. panic("This kernel cannot boot from RAM!\n");
  376. #else
  377. struct mtd_info *mtd_ram;
  378. mtd_ram = (struct mtd_info *)kmalloc(sizeof(struct mtd_info),
  379. GFP_KERNEL);
  380. if (!mtd_ram) {
  381. panic("axisflashmap couldn't allocate memory for "
  382. "mtd_info!\n");
  383. }
  384. printk(KERN_INFO " Adding RAM partition for romfs image:\n");
  385. printk(pmsg, pidx, romfs_start, romfs_length);
  386. err = mtdram_init_device(mtd_ram, (void*)romfs_start,
  387. romfs_length, "romfs");
  388. if (err) {
  389. panic("axisflashmap could not initialize MTD RAM "
  390. "device!\n");
  391. }
  392. #endif
  393. }
  394. return err;
  395. }
  396. /* This adds the above to the kernels init-call chain. */
  397. module_init(init_axis_flash);
  398. EXPORT_SYMBOL(axisflash_mtd);