tqm8xxl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. /* Currently, TQM8xxL has up to 8MiB flash */
  58. static unsigned long tqm8xxl_max_flash_size = 0x00800000;
  59. /* partition definition for first flash bank
  60. * (cf. "drivers/char/flash_config.c")
  61. */
  62. static struct mtd_partition tqm8xxl_partitions[] = {
  63. {
  64. .name = "ppcboot",
  65. .offset = 0x00000000,
  66. .size = 0x00020000, /* 128KB */
  67. .mask_flags = MTD_WRITEABLE, /* force read-only */
  68. },
  69. {
  70. .name = "kernel", /* default kernel image */
  71. .offset = 0x00020000,
  72. .size = 0x000e0000,
  73. .mask_flags = MTD_WRITEABLE, /* force read-only */
  74. },
  75. {
  76. .name = "user",
  77. .offset = 0x00100000,
  78. .size = 0x00100000,
  79. },
  80. {
  81. .name = "initrd",
  82. .offset = 0x00200000,
  83. .size = 0x00200000,
  84. }
  85. };
  86. /* partition definition for second flash bank */
  87. static struct mtd_partition tqm8xxl_fs_partitions[] = {
  88. {
  89. .name = "cramfs",
  90. .offset = 0x00000000,
  91. .size = 0x00200000,
  92. },
  93. {
  94. .name = "jffs",
  95. .offset = 0x00200000,
  96. .size = 0x00200000,
  97. //.size = MTDPART_SIZ_FULL,
  98. }
  99. };
  100. static int __init init_tqm_mtd(void)
  101. {
  102. int idx = 0, ret = 0;
  103. unsigned long flash_addr, flash_size, mtd_size = 0;
  104. /* pointer to TQM8xxL board info data */
  105. bd_t *bd = (bd_t *)__res;
  106. flash_addr = bd->bi_flashstart;
  107. flash_size = bd->bi_flashsize;
  108. //request maximum flash size address space
  109. start_scan_addr = ioremap(flash_addr, flash_size);
  110. if (!start_scan_addr) {
  111. printk(KERN_WARNING "%s:Failed to ioremap address:0x%x\n", __func__, flash_addr);
  112. return -EIO;
  113. }
  114. for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
  115. if(mtd_size >= flash_size)
  116. break;
  117. printk(KERN_INFO "%s: chip probing count %d\n", __func__, idx);
  118. map_banks[idx] = kzalloc(sizeof(struct map_info), GFP_KERNEL);
  119. if(map_banks[idx] == NULL) {
  120. ret = -ENOMEM;
  121. /* FIXME: What if some MTD devices were probed already? */
  122. goto error_mem;
  123. }
  124. map_banks[idx]->name = kmalloc(16, GFP_KERNEL);
  125. if (!map_banks[idx]->name) {
  126. ret = -ENOMEM;
  127. /* FIXME: What if some MTD devices were probed already? */
  128. goto error_mem;
  129. }
  130. sprintf(map_banks[idx]->name, "TQM8xxL%d", idx);
  131. map_banks[idx]->size = flash_size;
  132. map_banks[idx]->bankwidth = 4;
  133. simple_map_init(map_banks[idx]);
  134. map_banks[idx]->virt = start_scan_addr;
  135. map_banks[idx]->phys = flash_addr;
  136. /* FIXME: This looks utterly bogus, but I'm trying to
  137. preserve the behaviour of the original (shown here)...
  138. map_banks[idx]->map_priv_1 =
  139. start_scan_addr + ((idx > 0) ?
  140. (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0);
  141. */
  142. if (idx && mtd_banks[idx-1]) {
  143. map_banks[idx]->virt += mtd_banks[idx-1]->size;
  144. map_banks[idx]->phys += mtd_banks[idx-1]->size;
  145. }
  146. //start to probe flash chips
  147. mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]);
  148. if (mtd_banks[idx]) {
  149. mtd_banks[idx]->owner = THIS_MODULE;
  150. mtd_size += mtd_banks[idx]->size;
  151. num_banks++;
  152. printk(KERN_INFO "%s: bank%d, name:%s, size:%dbytes \n", __func__, num_banks,
  153. mtd_banks[idx]->name, mtd_banks[idx]->size);
  154. }
  155. }
  156. /* no supported flash chips found */
  157. if (!num_banks) {
  158. printk(KERN_NOTICE "TQM8xxL: No support flash chips found!\n");
  159. ret = -ENXIO;
  160. goto error_mem;
  161. }
  162. /*
  163. * Select Static partition definitions
  164. */
  165. part_banks[0].mtd_part = tqm8xxl_partitions;
  166. part_banks[0].type = "Static image";
  167. part_banks[0].nums = ARRAY_SIZE(tqm8xxl_partitions);
  168. part_banks[1].mtd_part = tqm8xxl_fs_partitions;
  169. part_banks[1].type = "Static file system";
  170. part_banks[1].nums = ARRAY_SIZE(tqm8xxl_fs_partitions);
  171. for(idx = 0; idx < num_banks ; idx++) {
  172. if (part_banks[idx].nums == 0)
  173. printk(KERN_NOTICE "TQM flash%d: no partition info available, registering whole flash at once\n", idx);
  174. else
  175. printk(KERN_NOTICE "TQM flash%d: Using %s partition definition\n",
  176. idx, part_banks[idx].type);
  177. mtd_device_register(mtd_banks[idx], part_banks[idx].mtd_part,
  178. part_banks[idx].nums);
  179. }
  180. return 0;
  181. error_mem:
  182. for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
  183. if(map_banks[idx] != NULL) {
  184. kfree(map_banks[idx]->name);
  185. map_banks[idx]->name = NULL;
  186. kfree(map_banks[idx]);
  187. map_banks[idx] = NULL;
  188. }
  189. }
  190. error:
  191. iounmap(start_scan_addr);
  192. return ret;
  193. }
  194. static void __exit cleanup_tqm_mtd(void)
  195. {
  196. unsigned int idx = 0;
  197. for(idx = 0 ; idx < num_banks ; idx++) {
  198. /* destroy mtd_info previously allocated */
  199. if (mtd_banks[idx]) {
  200. mtd_device_unregister(mtd_banks[idx]);
  201. map_destroy(mtd_banks[idx]);
  202. }
  203. /* release map_info not used anymore */
  204. kfree(map_banks[idx]->name);
  205. kfree(map_banks[idx]);
  206. }
  207. if (start_scan_addr) {
  208. iounmap(start_scan_addr);
  209. start_scan_addr = 0;
  210. }
  211. }
  212. module_init(init_tqm_mtd);
  213. module_exit(cleanup_tqm_mtd);
  214. MODULE_LICENSE("GPL");
  215. MODULE_AUTHOR("Kirk Lee <kirk@hpc.ee.ntu.edu.tw>");
  216. MODULE_DESCRIPTION("MTD map driver for TQM8xxL boards");