axisflashmap.c 12 KB

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