tqm834x.c 7.6 KB

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