axisflashmap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. * $Log: axisflashmap.c,v $
  14. * Revision 1.11 2004/11/15 10:27:14 starvik
  15. * Corrected typo (Thanks to Milton Miller <miltonm@bga.com>).
  16. *
  17. * Revision 1.10 2004/08/16 12:37:22 starvik
  18. * Merge of Linux 2.6.8
  19. *
  20. * Revision 1.8 2004/05/14 07:58:03 starvik
  21. * Merge of changes from 2.4
  22. *
  23. * Revision 1.6 2003/07/04 08:27:37 starvik
  24. * Merge of Linux 2.5.74
  25. *
  26. * Revision 1.5 2002/12/11 13:13:57 starvik
  27. * Added arch/ to v10 specific includes
  28. * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
  29. *
  30. * Revision 1.4 2002/11/20 11:56:10 starvik
  31. * Merge of Linux 2.5.48
  32. *
  33. * Revision 1.3 2002/11/13 14:54:13 starvik
  34. * Copied from linux 2.4
  35. *
  36. * Revision 1.28 2002/10/01 08:08:43 jonashg
  37. * The first partition ends at the start of the partition table.
  38. *
  39. * Revision 1.27 2002/08/21 09:23:13 jonashg
  40. * Speling.
  41. *
  42. * Revision 1.26 2002/08/21 08:35:20 jonashg
  43. * Cosmetic change to printouts.
  44. *
  45. * Revision 1.25 2002/08/21 08:15:42 jonashg
  46. * Made it compile even without CONFIG_MTD_CONCAT defined.
  47. *
  48. * Revision 1.24 2002/08/20 13:12:35 jonashg
  49. * * New approach to probing. Probe cse0 and cse1 separately and (mtd)concat
  50. * the results.
  51. * * Removed compile time tests concerning how the mtdram driver has been
  52. * configured. The user will know about the misconfiguration at runtime
  53. * instead. (The old approach made it impossible to use mtdram for anything
  54. * else than RAM boot).
  55. *
  56. * Revision 1.23 2002/05/13 12:12:28 johana
  57. * Allow compile without CONFIG_MTD_MTDRAM but warn at compiletime and
  58. * be informative at runtime.
  59. *
  60. * Revision 1.22 2002/05/13 10:24:44 johana
  61. * Added #if checks on MTDRAM CONFIG
  62. *
  63. * Revision 1.21 2002/05/06 16:05:20 johana
  64. * Removed debug printout.
  65. *
  66. * Revision 1.20 2002/05/06 16:03:00 johana
  67. * No more cramfs as root hack in generic code.
  68. * It's handled by axisflashmap using mtdram.
  69. *
  70. * Revision 1.19 2002/03/15 17:10:28 bjornw
  71. * Changed comment about cached access since we changed this before
  72. *
  73. * Revision 1.18 2002/03/05 17:06:15 jonashg
  74. * Try amd_flash probe before cfi_probe since amd_flash driver can handle two
  75. * (or more) flash chips of different model and the cfi driver cannot.
  76. *
  77. * Revision 1.17 2001/11/12 19:42:38 pkj
  78. * Fixed compiler warnings.
  79. *
  80. * Revision 1.16 2001/11/08 11:18:58 jonashg
  81. * Always read from uncached address to avoid problems with flushing
  82. * cachelines after write and MTD-erase. No performance loss have been
  83. * seen yet.
  84. *
  85. * Revision 1.15 2001/10/19 12:41:04 jonashg
  86. * Name of probe has changed in MTD.
  87. *
  88. * Revision 1.14 2001/09/21 07:14:10 jonashg
  89. * Made root filesystem (cramfs) use mtdblock driver when booting from flash.
  90. *
  91. * Revision 1.13 2001/08/15 13:57:35 jonashg
  92. * Entire MTD updated to the linux 2.4.7 version.
  93. *
  94. * Revision 1.12 2001/06/11 09:50:30 jonashg
  95. * Oops, 2MB is 0x200000 bytes.
  96. *
  97. * Revision 1.11 2001/06/08 11:39:44 jonashg
  98. * Changed sizes and offsets in axis_default_partitions to use
  99. * CONFIG_ETRAX_PTABLE_SECTOR.
  100. *
  101. * Revision 1.10 2001/05/29 09:42:03 jonashg
  102. * Use macro for end marker length instead of sizeof.
  103. *
  104. * Revision 1.9 2001/05/29 08:52:52 jonashg
  105. * Gave names to the magic fours (size of the ptable end marker).
  106. *
  107. * Revision 1.8 2001/05/28 15:36:20 jonashg
  108. * * Removed old comment about ptable location in flash (it's a CONFIG_ option).
  109. * * Variable ptable was initialized twice to the same value.
  110. *
  111. * Revision 1.7 2001/04/05 13:41:46 markusl
  112. * Updated according to review remarks
  113. *
  114. * Revision 1.6 2001/03/07 09:21:21 bjornw
  115. * No need to waste .data
  116. *
  117. * Revision 1.5 2001/03/06 16:27:01 jonashg
  118. * Probe the entire flash area for flash devices.
  119. *
  120. * Revision 1.4 2001/02/23 12:47:15 bjornw
  121. * Uncached flash in LOW_MAP moved from 0xe to 0x8
  122. *
  123. * Revision 1.3 2001/02/16 12:11:45 jonashg
  124. * MTD driver amd_flash is now included in MTD CVS repository.
  125. * (It's now in drivers/mtd).
  126. *
  127. * Revision 1.2 2001/02/09 11:12:22 jonashg
  128. * Support for AMD compatible non-CFI flash chips.
  129. * Only tested with Toshiba TC58FVT160 so far.
  130. *
  131. * Revision 1.1 2001/01/12 17:01:18 bjornw
  132. * * Added axisflashmap.c, a physical mapping for MTD that reads and understands
  133. * Axis partition-table format.
  134. *
  135. *
  136. */
  137. #include <linux/module.h>
  138. #include <linux/types.h>
  139. #include <linux/kernel.h>
  140. #include <linux/config.h>
  141. #include <linux/init.h>
  142. #include <linux/mtd/concat.h>
  143. #include <linux/mtd/map.h>
  144. #include <linux/mtd/mtd.h>
  145. #include <linux/mtd/mtdram.h>
  146. #include <linux/mtd/partitions.h>
  147. #include <asm/axisflashmap.h>
  148. #include <asm/mmu.h>
  149. #include <asm/arch/sv_addr_ag.h>
  150. #ifdef CONFIG_CRIS_LOW_MAP
  151. #define FLASH_UNCACHED_ADDR KSEG_8
  152. #define FLASH_CACHED_ADDR KSEG_5
  153. #else
  154. #define FLASH_UNCACHED_ADDR KSEG_E
  155. #define FLASH_CACHED_ADDR KSEG_F
  156. #endif
  157. #if CONFIG_ETRAX_FLASH_BUSWIDTH==1
  158. #define flash_data __u8
  159. #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
  160. #define flash_data __u16
  161. #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
  162. #define flash_data __u32
  163. #endif
  164. /* From head.S */
  165. extern unsigned long romfs_start, romfs_length, romfs_in_flash;
  166. /* The master mtd for the entire flash. */
  167. struct mtd_info* axisflash_mtd = NULL;
  168. /* Map driver functions. */
  169. static map_word flash_read(struct map_info *map, unsigned long ofs)
  170. {
  171. map_word tmp;
  172. tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);
  173. return tmp;
  174. }
  175. static void flash_copy_from(struct map_info *map, void *to,
  176. unsigned long from, ssize_t len)
  177. {
  178. memcpy(to, (void *)(map->map_priv_1 + from), len);
  179. }
  180. static void flash_write(struct map_info *map, map_word d, unsigned long adr)
  181. {
  182. *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];
  183. }
  184. /*
  185. * The map for chip select e0.
  186. *
  187. * We run into tricky coherence situations if we mix cached with uncached
  188. * accesses to we only use the uncached version here.
  189. *
  190. * The size field is the total size where the flash chips may be mapped on the
  191. * chip select. MTD probes should find all devices there and it does not matter
  192. * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD
  193. * probes will ignore them.
  194. *
  195. * The start address in map_priv_1 is in virtual memory so we cannot use
  196. * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start
  197. * address of cse0.
  198. */
  199. static struct map_info map_cse0 = {
  200. .name = "cse0",
  201. .size = MEM_CSE0_SIZE,
  202. .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
  203. .read = flash_read,
  204. .copy_from = flash_copy_from,
  205. .write = flash_write,
  206. .map_priv_1 = FLASH_UNCACHED_ADDR
  207. };
  208. /*
  209. * The map for chip select e1.
  210. *
  211. * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong
  212. * address, but there isn't.
  213. */
  214. static struct map_info map_cse1 = {
  215. .name = "cse1",
  216. .size = MEM_CSE1_SIZE,
  217. .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
  218. .read = flash_read,
  219. .copy_from = flash_copy_from,
  220. .write = flash_write,
  221. .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE
  222. };
  223. /* If no partition-table was found, we use this default-set. */
  224. #define MAX_PARTITIONS 7
  225. #define NUM_DEFAULT_PARTITIONS 3
  226. /*
  227. * Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the
  228. * size of one flash block and "filesystem"-partition needs 5 blocks to be able
  229. * to use JFFS.
  230. */
  231. static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {
  232. {
  233. .name = "boot firmware",
  234. .size = CONFIG_ETRAX_PTABLE_SECTOR,
  235. .offset = 0
  236. },
  237. {
  238. .name = "kernel",
  239. .size = 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR),
  240. .offset = CONFIG_ETRAX_PTABLE_SECTOR
  241. },
  242. {
  243. .name = "filesystem",
  244. .size = 5 * CONFIG_ETRAX_PTABLE_SECTOR,
  245. .offset = 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR)
  246. }
  247. };
  248. /* Initialize the ones normally used. */
  249. static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {
  250. {
  251. .name = "part0",
  252. .size = CONFIG_ETRAX_PTABLE_SECTOR,
  253. .offset = 0
  254. },
  255. {
  256. .name = "part1",
  257. .size = 0,
  258. .offset = 0
  259. },
  260. {
  261. .name = "part2",
  262. .size = 0,
  263. .offset = 0
  264. },
  265. {
  266. .name = "part3",
  267. .size = 0,
  268. .offset = 0
  269. },
  270. {
  271. .name = "part4",
  272. .size = 0,
  273. .offset = 0
  274. },
  275. {
  276. .name = "part5",
  277. .size = 0,
  278. .offset = 0
  279. },
  280. {
  281. .name = "part6",
  282. .size = 0,
  283. .offset = 0
  284. },
  285. };
  286. /*
  287. * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash
  288. * chips in that order (because the amd_flash-driver is faster).
  289. */
  290. static struct mtd_info *probe_cs(struct map_info *map_cs)
  291. {
  292. struct mtd_info *mtd_cs = NULL;
  293. printk(KERN_INFO
  294. "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
  295. map_cs->name, map_cs->size, map_cs->map_priv_1);
  296. #ifdef CONFIG_MTD_AMDSTD
  297. mtd_cs = do_map_probe("amd_flash", map_cs);
  298. #endif
  299. #ifdef CONFIG_MTD_CFI
  300. if (!mtd_cs) {
  301. mtd_cs = do_map_probe("cfi_probe", map_cs);
  302. }
  303. #endif
  304. return mtd_cs;
  305. }
  306. /*
  307. * Probe each chip select individually for flash chips. If there are chips on
  308. * both cse0 and cse1, the mtd_info structs will be concatenated to one struct
  309. * so that MTD partitions can cross chip boundries.
  310. *
  311. * The only known restriction to how you can mount your chips is that each
  312. * chip select must hold similar flash chips. But you need external hardware
  313. * to do that anyway and you can put totally different chips on cse0 and cse1
  314. * so it isn't really much of a restriction.
  315. */
  316. static struct mtd_info *flash_probe(void)
  317. {
  318. struct mtd_info *mtd_cse0;
  319. struct mtd_info *mtd_cse1;
  320. struct mtd_info *mtd_cse;
  321. mtd_cse0 = probe_cs(&map_cse0);
  322. mtd_cse1 = probe_cs(&map_cse1);
  323. if (!mtd_cse0 && !mtd_cse1) {
  324. /* No chip found. */
  325. return NULL;
  326. }
  327. if (mtd_cse0 && mtd_cse1) {
  328. #ifdef CONFIG_MTD_CONCAT
  329. struct mtd_info *mtds[] = { mtd_cse0, mtd_cse1 };
  330. /* Since the concatenation layer adds a small overhead we
  331. * could try to figure out if the chips in cse0 and cse1 are
  332. * identical and reprobe the whole cse0+cse1 window. But since
  333. * flash chips are slow, the overhead is relatively small.
  334. * So we use the MTD concatenation layer instead of further
  335. * complicating the probing procedure.
  336. */
  337. mtd_cse = mtd_concat_create(mtds,
  338. sizeof(mtds) / sizeof(mtds[0]),
  339. "cse0+cse1");
  340. #else
  341. printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel "
  342. "(mis)configuration!\n", map_cse0.name, map_cse1.name);
  343. mtd_cse = NULL;
  344. #endif
  345. if (!mtd_cse) {
  346. printk(KERN_ERR "%s and %s: Concatenation failed!\n",
  347. map_cse0.name, map_cse1.name);
  348. /* The best we can do now is to only use what we found
  349. * at cse0.
  350. */
  351. mtd_cse = mtd_cse0;
  352. map_destroy(mtd_cse1);
  353. }
  354. } else {
  355. mtd_cse = mtd_cse0? mtd_cse0 : mtd_cse1;
  356. }
  357. return mtd_cse;
  358. }
  359. /*
  360. * Probe the flash chip(s) and, if it succeeds, read the partition-table
  361. * and register the partitions with MTD.
  362. */
  363. static int __init init_axis_flash(void)
  364. {
  365. struct mtd_info *mymtd;
  366. int err = 0;
  367. int pidx = 0;
  368. struct partitiontable_head *ptable_head = NULL;
  369. struct partitiontable_entry *ptable;
  370. int use_default_ptable = 1; /* Until proven otherwise. */
  371. const char *pmsg = " /dev/flash%d at 0x%08x, size 0x%08x\n";
  372. if (!(mymtd = flash_probe())) {
  373. /* There's no reason to use this module if no flash chip can
  374. * be identified. Make sure that's understood.
  375. */
  376. printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
  377. } else {
  378. printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
  379. mymtd->name, mymtd->size);
  380. axisflash_mtd = mymtd;
  381. }
  382. if (mymtd) {
  383. mymtd->owner = THIS_MODULE;
  384. ptable_head = (struct partitiontable_head *)(FLASH_CACHED_ADDR +
  385. CONFIG_ETRAX_PTABLE_SECTOR +
  386. PARTITION_TABLE_OFFSET);
  387. }
  388. pidx++; /* First partition is always set to the default. */
  389. if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
  390. && (ptable_head->size <
  391. (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
  392. PARTITIONTABLE_END_MARKER_SIZE))
  393. && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
  394. ptable_head->size -
  395. PARTITIONTABLE_END_MARKER_SIZE)
  396. == PARTITIONTABLE_END_MARKER)) {
  397. /* Looks like a start, sane length and end of a
  398. * partition table, lets check csum etc.
  399. */
  400. int ptable_ok = 0;
  401. struct partitiontable_entry *max_addr =
  402. (struct partitiontable_entry *)
  403. ((unsigned long)ptable_head + sizeof(*ptable_head) +
  404. ptable_head->size);
  405. unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
  406. unsigned char *p;
  407. unsigned long csum = 0;
  408. ptable = (struct partitiontable_entry *)
  409. ((unsigned long)ptable_head + sizeof(*ptable_head));
  410. /* Lets be PARANOID, and check the checksum. */
  411. p = (unsigned char*) ptable;
  412. while (p <= (unsigned char*)max_addr) {
  413. csum += *p++;
  414. csum += *p++;
  415. csum += *p++;
  416. csum += *p++;
  417. }
  418. ptable_ok = (csum == ptable_head->checksum);
  419. /* Read the entries and use/show the info. */
  420. printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
  421. (ptable_ok ? " valid" : "n invalid"), ptable_head,
  422. max_addr);
  423. /* We have found a working bootblock. Now read the
  424. * partition table. Scan the table. It ends when
  425. * there is 0xffffffff, that is, empty flash.
  426. */
  427. while (ptable_ok
  428. && ptable->offset != 0xffffffff
  429. && ptable < max_addr
  430. && pidx < MAX_PARTITIONS) {
  431. axis_partitions[pidx].offset = offset + ptable->offset;
  432. axis_partitions[pidx].size = ptable->size;
  433. printk(pmsg, pidx, axis_partitions[pidx].offset,
  434. axis_partitions[pidx].size);
  435. pidx++;
  436. ptable++;
  437. }
  438. use_default_ptable = !ptable_ok;
  439. }
  440. if (romfs_in_flash) {
  441. /* Add an overlapping device for the root partition (romfs). */
  442. axis_partitions[pidx].name = "romfs";
  443. axis_partitions[pidx].size = romfs_length;
  444. axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
  445. axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
  446. printk(KERN_INFO
  447. " Adding readonly flash partition for romfs image:\n");
  448. printk(pmsg, pidx, axis_partitions[pidx].offset,
  449. axis_partitions[pidx].size);
  450. pidx++;
  451. }
  452. if (mymtd) {
  453. if (use_default_ptable) {
  454. printk(KERN_INFO " Using default partition table.\n");
  455. err = add_mtd_partitions(mymtd, axis_default_partitions,
  456. NUM_DEFAULT_PARTITIONS);
  457. } else {
  458. err = add_mtd_partitions(mymtd, axis_partitions, pidx);
  459. }
  460. if (err) {
  461. panic("axisflashmap could not add MTD partitions!\n");
  462. }
  463. }
  464. if (!romfs_in_flash) {
  465. /* Create an RAM device for the root partition (romfs). */
  466. #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
  467. /* No use trying to boot this kernel from RAM. Panic! */
  468. printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
  469. "device due to kernel (mis)configuration!\n");
  470. panic("This kernel cannot boot from RAM!\n");
  471. #else
  472. struct mtd_info *mtd_ram;
  473. mtd_ram = (struct mtd_info *)kmalloc(sizeof(struct mtd_info),
  474. GFP_KERNEL);
  475. if (!mtd_ram) {
  476. panic("axisflashmap couldn't allocate memory for "
  477. "mtd_info!\n");
  478. }
  479. printk(KERN_INFO " Adding RAM partition for romfs image:\n");
  480. printk(pmsg, pidx, romfs_start, romfs_length);
  481. err = mtdram_init_device(mtd_ram, (void*)romfs_start,
  482. romfs_length, "romfs");
  483. if (err) {
  484. panic("axisflashmap could not initialize MTD RAM "
  485. "device!\n");
  486. }
  487. #endif
  488. }
  489. return err;
  490. }
  491. /* This adds the above to the kernels init-call chain. */
  492. module_init(init_axis_flash);
  493. EXPORT_SYMBOL(axisflash_mtd);