tqm8xxl.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Handle mapping of the flash memory access routines
  3. * on TQM8xxL based devices.
  4. *
  5. * $Id: tqm8xxl.c,v 1.15 2005/11/07 11:14:28 gleixner 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/module.h>
  25. #include <linux/types.h>
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/map.h>
  31. #include <linux/mtd/partitions.h>
  32. #include <asm/io.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] = kzalloc(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. map_banks[idx]->name = (char *)kmalloc(16, GFP_KERNEL);
  129. if (!map_banks[idx]->name) {
  130. ret = -ENOMEM;
  131. /* FIXME: What if some MTD devices were probed already? */
  132. goto error_mem;
  133. }
  134. sprintf(map_banks[idx]->name, "TQM8xxL%d", idx);
  135. map_banks[idx]->size = flash_size;
  136. map_banks[idx]->bankwidth = 4;
  137. simple_map_init(map_banks[idx]);
  138. map_banks[idx]->virt = start_scan_addr;
  139. map_banks[idx]->phys = flash_addr;
  140. /* FIXME: This looks utterly bogus, but I'm trying to
  141. preserve the behaviour of the original (shown here)...
  142. map_banks[idx]->map_priv_1 =
  143. start_scan_addr + ((idx > 0) ?
  144. (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0);
  145. */
  146. if (idx && mtd_banks[idx-1]) {
  147. map_banks[idx]->virt += mtd_banks[idx-1]->size;
  148. map_banks[idx]->phys += mtd_banks[idx-1]->size;
  149. }
  150. //start to probe flash chips
  151. mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]);
  152. if (mtd_banks[idx]) {
  153. mtd_banks[idx]->owner = THIS_MODULE;
  154. mtd_size += mtd_banks[idx]->size;
  155. num_banks++;
  156. printk(KERN_INFO "%s: bank%d, name:%s, size:%dbytes \n", __FUNCTION__, num_banks,
  157. mtd_banks[idx]->name, mtd_banks[idx]->size);
  158. }
  159. }
  160. /* no supported flash chips found */
  161. if (!num_banks) {
  162. printk(KERN_NOTICE "TQM8xxL: No support flash chips found!\n");
  163. ret = -ENXIO;
  164. goto error_mem;
  165. }
  166. #ifdef CONFIG_MTD_PARTITIONS
  167. /*
  168. * Select Static partition definitions
  169. */
  170. part_banks[0].mtd_part = tqm8xxl_partitions;
  171. part_banks[0].type = "Static image";
  172. part_banks[0].nums = ARRAY_SIZE(tqm8xxl_partitions);
  173. part_banks[1].mtd_part = tqm8xxl_fs_partitions;
  174. part_banks[1].type = "Static file system";
  175. part_banks[1].nums = ARRAY_SIZE(tqm8xxl_fs_partitions);
  176. for(idx = 0; idx < num_banks ; idx++) {
  177. if (part_banks[idx].nums == 0) {
  178. printk(KERN_NOTICE "TQM flash%d: no partition info available, registering whole flash at once\n", idx);
  179. add_mtd_device(mtd_banks[idx]);
  180. } else {
  181. printk(KERN_NOTICE "TQM flash%d: Using %s partition definition\n",
  182. idx, part_banks[idx].type);
  183. add_mtd_partitions(mtd_banks[idx], part_banks[idx].mtd_part,
  184. part_banks[idx].nums);
  185. }
  186. }
  187. #else
  188. printk(KERN_NOTICE "TQM flash: registering %d whole flash banks at once\n", num_banks);
  189. for(idx = 0 ; idx < num_banks ; idx++)
  190. add_mtd_device(mtd_banks[idx]);
  191. #endif
  192. return 0;
  193. error_mem:
  194. for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
  195. if(map_banks[idx] != NULL) {
  196. kfree(map_banks[idx]->name);
  197. map_banks[idx]->name = NULL;
  198. kfree(map_banks[idx]);
  199. map_banks[idx] = NULL;
  200. }
  201. }
  202. error:
  203. iounmap(start_scan_addr);
  204. return ret;
  205. }
  206. static void __exit cleanup_tqm_mtd(void)
  207. {
  208. unsigned int idx = 0;
  209. for(idx = 0 ; idx < num_banks ; idx++) {
  210. /* destroy mtd_info previously allocated */
  211. if (mtd_banks[idx]) {
  212. del_mtd_partitions(mtd_banks[idx]);
  213. map_destroy(mtd_banks[idx]);
  214. }
  215. /* release map_info not used anymore */
  216. kfree(map_banks[idx]->name);
  217. kfree(map_banks[idx]);
  218. }
  219. if (start_scan_addr) {
  220. iounmap(start_scan_addr);
  221. start_scan_addr = 0;
  222. }
  223. }
  224. module_init(init_tqm_mtd);
  225. module_exit(cleanup_tqm_mtd);
  226. MODULE_LICENSE("GPL");
  227. MODULE_AUTHOR("Kirk Lee <kirk@hpc.ee.ntu.edu.tw>");
  228. MODULE_DESCRIPTION("MTD map driver for TQM8xxL boards");