tqm8xxl.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Handle mapping of the flash memory access routines
  3. * on TQM8xxL based devices.
  4. *
  5. * $Id: tqm8xxl.c,v 1.13 2004/10/20 22:21:53 dwmw2 Exp $
  6. *
  7. * based on rpxlite.c
  8. *
  9. * Copyright(C) 2001 Kirk Lee <kirk@hpc.ee.ntu.edu.tw>
  10. *
  11. * This code is GPLed
  12. *
  13. */
  14. /*
  15. * According to TQM8xxL hardware manual, TQM8xxL series have
  16. * following flash memory organisations:
  17. * | capacity | | chip type | | bank0 | | bank1 |
  18. * 2MiB 512Kx16 2MiB 0
  19. * 4MiB 1Mx16 4MiB 0
  20. * 8MiB 1Mx16 4MiB 4MiB
  21. * Thus, we choose CONFIG_MTD_CFI_I2 & CONFIG_MTD_CFI_B4 at
  22. * kernel configuration.
  23. */
  24. #include <linux/config.h>
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/init.h>
  29. #include <asm/io.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/map.h>
  32. #include <linux/mtd/partitions.h>
  33. #define FLASH_ADDR 0x40000000
  34. #define FLASH_SIZE 0x00800000
  35. #define FLASH_BANK_MAX 4
  36. // trivial struct to describe partition information
  37. struct mtd_part_def
  38. {
  39. int nums;
  40. unsigned char *type;
  41. struct mtd_partition* mtd_part;
  42. };
  43. //static struct mtd_info *mymtd;
  44. static struct mtd_info* mtd_banks[FLASH_BANK_MAX];
  45. static struct map_info* map_banks[FLASH_BANK_MAX];
  46. static struct mtd_part_def part_banks[FLASH_BANK_MAX];
  47. static unsigned long num_banks;
  48. static void __iomem *start_scan_addr;
  49. /*
  50. * Here are partition information for all known TQM8xxL series devices.
  51. * See include/linux/mtd/partitions.h for definition of the mtd_partition
  52. * structure.
  53. *
  54. * The *_max_flash_size is the maximum possible mapped flash size which
  55. * is not necessarily the actual flash size. It must correspond to the
  56. * value specified in the mapping definition defined by the
  57. * "struct map_desc *_io_desc" for the corresponding machine.
  58. */
  59. #ifdef CONFIG_MTD_PARTITIONS
  60. /* Currently, TQM8xxL has upto 8MiB flash */
  61. static unsigned long tqm8xxl_max_flash_size = 0x00800000;
  62. /* partition definition for first flash bank
  63. * (cf. "drivers/char/flash_config.c")
  64. */
  65. static struct mtd_partition tqm8xxl_partitions[] = {
  66. {
  67. .name = "ppcboot",
  68. .offset = 0x00000000,
  69. .size = 0x00020000, /* 128KB */
  70. .mask_flags = MTD_WRITEABLE, /* force read-only */
  71. },
  72. {
  73. .name = "kernel", /* default kernel image */
  74. .offset = 0x00020000,
  75. .size = 0x000e0000,
  76. .mask_flags = MTD_WRITEABLE, /* force read-only */
  77. },
  78. {
  79. .name = "user",
  80. .offset = 0x00100000,
  81. .size = 0x00100000,
  82. },
  83. {
  84. .name = "initrd",
  85. .offset = 0x00200000,
  86. .size = 0x00200000,
  87. }
  88. };
  89. /* partition definition for second flash bank */
  90. static struct mtd_partition tqm8xxl_fs_partitions[] = {
  91. {
  92. .name = "cramfs",
  93. .offset = 0x00000000,
  94. .size = 0x00200000,
  95. },
  96. {
  97. .name = "jffs",
  98. .offset = 0x00200000,
  99. .size = 0x00200000,
  100. //.size = MTDPART_SIZ_FULL,
  101. }
  102. };
  103. #endif
  104. int __init init_tqm_mtd(void)
  105. {
  106. int idx = 0, ret = 0;
  107. unsigned long flash_addr, flash_size, mtd_size = 0;
  108. /* pointer to TQM8xxL board info data */
  109. bd_t *bd = (bd_t *)__res;
  110. flash_addr = bd->bi_flashstart;
  111. flash_size = bd->bi_flashsize;
  112. //request maximum flash size address space
  113. start_scan_addr = ioremap(flash_addr, flash_size);
  114. if (!start_scan_addr) {
  115. printk(KERN_WARNING "%s:Failed to ioremap address:0x%x\n", __FUNCTION__, flash_addr);
  116. return -EIO;
  117. }
  118. for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
  119. if(mtd_size >= flash_size)
  120. break;
  121. printk(KERN_INFO "%s: chip probing count %d\n", __FUNCTION__, idx);
  122. map_banks[idx] = (struct map_info *)kmalloc(sizeof(struct map_info), GFP_KERNEL);
  123. if(map_banks[idx] == NULL) {
  124. ret = -ENOMEM;
  125. /* FIXME: What if some MTD devices were probed already? */
  126. goto error_mem;
  127. }
  128. memset((void *)map_banks[idx], 0, sizeof(struct map_info));
  129. map_banks[idx]->name = (char *)kmalloc(16, GFP_KERNEL);
  130. if (!map_banks[idx]->name) {
  131. ret = -ENOMEM;
  132. /* FIXME: What if some MTD devices were probed already? */
  133. goto error_mem;
  134. }
  135. sprintf(map_banks[idx]->name, "TQM8xxL%d", idx);
  136. map_banks[idx]->size = flash_size;
  137. map_banks[idx]->bankwidth = 4;
  138. simple_map_init(map_banks[idx]);
  139. map_banks[idx]->virt = start_scan_addr;
  140. map_banks[idx]->phys = flash_addr;
  141. /* FIXME: This looks utterly bogus, but I'm trying to
  142. preserve the behaviour of the original (shown here)...
  143. map_banks[idx]->map_priv_1 =
  144. start_scan_addr + ((idx > 0) ?
  145. (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0);
  146. */
  147. if (idx && mtd_banks[idx-1]) {
  148. map_banks[idx]->virt += mtd_banks[idx-1]->size;
  149. map_banks[idx]->phys += mtd_banks[idx-1]->size;
  150. }
  151. //start to probe flash chips
  152. mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]);
  153. if (mtd_banks[idx]) {
  154. mtd_banks[idx]->owner = THIS_MODULE;
  155. mtd_size += mtd_banks[idx]->size;
  156. num_banks++;
  157. printk(KERN_INFO "%s: bank%d, name:%s, size:%dbytes \n", __FUNCTION__, num_banks,
  158. mtd_banks[idx]->name, mtd_banks[idx]->size);
  159. }
  160. }
  161. /* no supported flash chips found */
  162. if (!num_banks) {
  163. printk(KERN_NOTICE "TQM8xxL: No support flash chips found!\n");
  164. ret = -ENXIO;
  165. goto error_mem;
  166. }
  167. #ifdef CONFIG_MTD_PARTITIONS
  168. /*
  169. * Select Static partition definitions
  170. */
  171. part_banks[0].mtd_part = tqm8xxl_partitions;
  172. part_banks[0].type = "Static image";
  173. part_banks[0].nums = ARRAY_SIZE(tqm8xxl_partitions);
  174. part_banks[1].mtd_part = tqm8xxl_fs_partitions;
  175. part_banks[1].type = "Static file system";
  176. part_banks[1].nums = ARRAY_SIZE(tqm8xxl_fs_partitions);
  177. for(idx = 0; idx < num_banks ; idx++) {
  178. if (part_banks[idx].nums == 0) {
  179. printk(KERN_NOTICE "TQM flash%d: no partition info available, registering whole flash at once\n", idx);
  180. add_mtd_device(mtd_banks[idx]);
  181. } else {
  182. printk(KERN_NOTICE "TQM flash%d: Using %s partition definition\n",
  183. idx, part_banks[idx].type);
  184. add_mtd_partitions(mtd_banks[idx], part_banks[idx].mtd_part,
  185. part_banks[idx].nums);
  186. }
  187. }
  188. #else
  189. printk(KERN_NOTICE "TQM flash: registering %d whole flash banks at once\n", num_banks);
  190. for(idx = 0 ; idx < num_banks ; idx++)
  191. add_mtd_device(mtd_banks[idx]);
  192. #endif
  193. return 0;
  194. error_mem:
  195. for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
  196. if(map_banks[idx] != NULL) {
  197. if(map_banks[idx]->name != NULL) {
  198. kfree(map_banks[idx]->name);
  199. map_banks[idx]->name = NULL;
  200. }
  201. kfree(map_banks[idx]);
  202. map_banks[idx] = NULL;
  203. }
  204. }
  205. error:
  206. iounmap(start_scan_addr);
  207. return ret;
  208. }
  209. static void __exit cleanup_tqm_mtd(void)
  210. {
  211. unsigned int idx = 0;
  212. for(idx = 0 ; idx < num_banks ; idx++) {
  213. /* destroy mtd_info previously allocated */
  214. if (mtd_banks[idx]) {
  215. del_mtd_partitions(mtd_banks[idx]);
  216. map_destroy(mtd_banks[idx]);
  217. }
  218. /* release map_info not used anymore */
  219. kfree(map_banks[idx]->name);
  220. kfree(map_banks[idx]);
  221. }
  222. if (start_scan_addr) {
  223. iounmap(start_scan_addr);
  224. start_scan_addr = 0;
  225. }
  226. }
  227. module_init(init_tqm_mtd);
  228. module_exit(cleanup_tqm_mtd);
  229. MODULE_LICENSE("GPL");
  230. MODULE_AUTHOR("Kirk Lee <kirk@hpc.ee.ntu.edu.tw>");
  231. MODULE_DESCRIPTION("MTD map driver for TQM8xxL boards");