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