tqm8xxl.c 6.6 KB

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