tqm834x.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * drivers/mtd/maps/tqm834x.c
  3. *
  4. * MTD mapping driver for TQM834x boards
  5. *
  6. * Copyright 2005 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>.
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. *
  12. */
  13. #include <linux/config.h>
  14. #include <linux/init.h>
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <asm/io.h>
  20. #include <asm/ppcboot.h>
  21. #include <linux/mtd/mtd.h>
  22. #include <linux/mtd/map.h>
  23. #include <linux/mtd/partitions.h>
  24. #define FLASH_BANK_MAX 2
  25. extern unsigned char __res[];
  26. /* trivial struct to describe partition information */
  27. struct mtd_part_def
  28. {
  29. int nums;
  30. unsigned char *type;
  31. struct mtd_partition* mtd_part;
  32. };
  33. static struct mtd_info* mtd_banks[FLASH_BANK_MAX];
  34. static struct map_info* map_banks[FLASH_BANK_MAX];
  35. static struct mtd_part_def part_banks[FLASH_BANK_MAX];
  36. static unsigned long num_banks;
  37. static unsigned long start_scan_addr;
  38. #ifdef CONFIG_MTD_PARTITIONS
  39. /*
  40. * The following defines the partition layout of TQM834x boards.
  41. *
  42. * See include/linux/mtd/partitions.h for definition of the
  43. * mtd_partition structure.
  44. *
  45. * Assume minimal initial size of 4 MiB per bank, will be updated
  46. * later in init_tqm834x_mtd() routine.
  47. */
  48. /* Partition definition for the first flash bank which is always present. */
  49. static struct mtd_partition tqm834x_partitions_bank1[] = {
  50. {
  51. .name = "u-boot", /* u-boot firmware */
  52. .offset = 0x00000000,
  53. .size = 0x00040000, /* 256 KiB */
  54. /*mask_flags: MTD_WRITEABLE, * force read-only */
  55. },
  56. {
  57. .name = "env", /* u-boot environment */
  58. .offset = 0x00040000,
  59. .size = 0x00020000, /* 128 KiB */
  60. /*mask_flags: MTD_WRITEABLE, * force read-only */
  61. },
  62. {
  63. .name = "kernel", /* linux kernel image */
  64. .offset = 0x00060000,
  65. .size = 0x00100000, /* 1 MiB */
  66. /*mask_flags: MTD_WRITEABLE, * force read-only */
  67. },
  68. {
  69. .name = "initrd", /* ramdisk image */
  70. .offset = 0x00160000,
  71. .size = 0x00200000, /* 2 MiB */
  72. },
  73. {
  74. .name = "user", /* user data */
  75. .offset = 0x00360000,
  76. .size = 0x000a0000, /* remaining space */
  77. /* NOTE: this parttion size is re-calcated in */
  78. /* init_tqm834x_mtd() to cover actual remaining space. */
  79. },
  80. };
  81. /* Partition definition for the second flash bank which may be present on some
  82. * TQM834x boards.
  83. */
  84. static struct mtd_partition tqm834x_partitions_bank2[] = {
  85. {
  86. .name = "jffs2", /* jffs2 filesystem */
  87. .offset = 0x00000000,
  88. .size = 0x00400000, /* whole device */
  89. /* NOTE: this parttion size is re-calcated in */
  90. /* init_tqm834x_mtd() to cover actual device size. */
  91. },
  92. };
  93. #endif /* CONFIG_MTD_PARTITIONS */
  94. static int __init init_tqm834x_mtd(void)
  95. {
  96. int idx = 0, ret = 0;
  97. unsigned long flash_addr, flash_size, mtd_size = 0;
  98. /* pointer to TQM834x board info data */
  99. bd_t *bd = (bd_t *)__res;
  100. #ifdef CONFIG_MTD_CMDLINE_PARTS
  101. int n;
  102. char mtdid[4];
  103. const char *part_probes[] = { "cmdlinepart", NULL };
  104. #endif
  105. flash_addr = bd->bi_flashstart;
  106. flash_size = bd->bi_flashsize;
  107. /* request maximum flash size address space */
  108. start_scan_addr = (unsigned long)ioremap(flash_addr, flash_size);
  109. if (!start_scan_addr) {
  110. printk("%s: Failed to ioremap address: 0x%lx\n",
  111. __FUNCTION__, flash_addr);
  112. return -EIO;
  113. }
  114. for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
  115. if (mtd_size >= flash_size)
  116. break;
  117. pr_debug("%s: chip probing count %d\n", __FUNCTION__, idx);
  118. map_banks[idx] =
  119. (struct map_info *)kmalloc(sizeof(struct map_info),
  120. GFP_KERNEL);
  121. if (map_banks[idx] == NULL) {
  122. ret = -ENOMEM;
  123. goto error_mem;
  124. }
  125. memset((void *)map_banks[idx], 0, sizeof(struct map_info));
  126. map_banks[idx]->name = (char *)kmalloc(16, GFP_KERNEL);
  127. if (map_banks[idx]->name == NULL) {
  128. ret = -ENOMEM;
  129. goto error_mem;
  130. }
  131. memset((void *)map_banks[idx]->name, 0, 16);
  132. sprintf(map_banks[idx]->name, "TQM834x-%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 = (void __iomem *)
  137. (start_scan_addr + ((idx > 0) ?
  138. (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0));
  139. map_banks[idx]->phys =
  140. flash_addr + ((idx > 0) ?
  141. (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0);
  142. /* start to probe flash chips */
  143. mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]);
  144. if (mtd_banks[idx]) {
  145. mtd_banks[idx]->owner = THIS_MODULE;
  146. mtd_size += mtd_banks[idx]->size;
  147. num_banks++;
  148. pr_debug("%s: bank %ld, name: %s, size: %d bytes \n",
  149. __FUNCTION__, num_banks,
  150. mtd_banks[idx]->name, mtd_banks[idx]->size);
  151. }
  152. }
  153. /* no supported flash chips found */
  154. if (!num_banks) {
  155. printk("TQM834x: No supported flash chips found!\n");
  156. ret = -ENXIO;
  157. goto error_mem;
  158. }
  159. #ifdef CONFIG_MTD_PARTITIONS
  160. /*
  161. * Select static partition definitions
  162. */
  163. n = ARRAY_SIZE(tqm834x_partitions_bank1);
  164. part_banks[0].mtd_part = tqm834x_partitions_bank1;
  165. part_banks[0].type = "static image bank1";
  166. part_banks[0].nums = n;
  167. /* update last partition size to cover actual remaining space */
  168. tqm834x_partitions_bank1[n - 1].size =
  169. mtd_banks[0]->size -
  170. tqm834x_partitions_bank1[n - 1].offset;
  171. /* check if we have second bank? */
  172. if (num_banks == 2) {
  173. n = ARRAY_SIZE(tqm834x_partitions_bank2);
  174. part_banks[1].mtd_part = tqm834x_partitions_bank2;
  175. part_banks[1].type = "static image bank2";
  176. part_banks[1].nums = n;
  177. /* update last partition size to cover actual remaining space */
  178. tqm834x_partitions_bank2[n - 1].size =
  179. mtd_banks[1]->size -
  180. tqm834x_partitions_bank2[n - 1].offset;
  181. }
  182. for(idx = 0; idx < num_banks ; idx++) {
  183. #ifdef CONFIG_MTD_CMDLINE_PARTS
  184. sprintf(mtdid, "%d", idx);
  185. n = parse_mtd_partitions(mtd_banks[idx],
  186. part_probes,
  187. &part_banks[idx].mtd_part,
  188. 0);
  189. pr_debug("%s: %d command line partitions on bank %s\n",
  190. __FUNCTION__, n, mtdid);
  191. if (n > 0) {
  192. part_banks[idx].type = "command line";
  193. part_banks[idx].nums = n;
  194. }
  195. #endif /* CONFIG_MTD_CMDLINE_PARTS */
  196. if (part_banks[idx].nums == 0) {
  197. printk(KERN_NOTICE
  198. "TQM834x flash bank %d: no partition info "
  199. "available, registering whole device\n", idx);
  200. add_mtd_device(mtd_banks[idx]);
  201. } else {
  202. printk(KERN_NOTICE
  203. "TQM834x flash bank %d: Using %s partition "
  204. "definition\n", idx, part_banks[idx].type);
  205. add_mtd_partitions(mtd_banks[idx],
  206. part_banks[idx].mtd_part,
  207. part_banks[idx].nums);
  208. }
  209. }
  210. #else /* ! CONFIG_MTD_PARTITIONS */
  211. printk(KERN_NOTICE "TQM834x flash: registering %d flash banks "
  212. "at once\n", num_banks);
  213. for(idx = 0 ; idx < num_banks ; idx++)
  214. add_mtd_device(mtd_banks[idx]);
  215. #endif /* CONFIG_MTD_PARTITIONS */
  216. return 0;
  217. error_mem:
  218. for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
  219. if (map_banks[idx] != NULL) {
  220. if (map_banks[idx]->name != NULL) {
  221. kfree(map_banks[idx]->name);
  222. map_banks[idx]->name = NULL;
  223. }
  224. kfree(map_banks[idx]);
  225. map_banks[idx] = NULL;
  226. }
  227. }
  228. iounmap((void *)start_scan_addr);
  229. return ret;
  230. }
  231. static void __exit cleanup_tqm834x_mtd(void)
  232. {
  233. unsigned int idx = 0;
  234. for(idx = 0 ; idx < num_banks ; idx++) {
  235. /* destroy mtd_info previously allocated */
  236. if (mtd_banks[idx]) {
  237. del_mtd_partitions(mtd_banks[idx]);
  238. map_destroy(mtd_banks[idx]);
  239. }
  240. /* release map_info not used anymore */
  241. kfree(map_banks[idx]->name);
  242. kfree(map_banks[idx]);
  243. }
  244. if (start_scan_addr) {
  245. iounmap((void *)start_scan_addr);
  246. start_scan_addr = 0;
  247. }
  248. }
  249. module_init(init_tqm834x_mtd);
  250. module_exit(cleanup_tqm834x_mtd);
  251. MODULE_LICENSE("GPL");
  252. MODULE_AUTHOR("Wolfgang Denk <wd@denx.de>");
  253. MODULE_DESCRIPTION("MTD map driver for TQM834x boards");