tqm834x.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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] = kzalloc(sizeof(struct map_info), GFP_KERNEL);
  118. if (map_banks[idx] == NULL) {
  119. ret = -ENOMEM;
  120. goto error_mem;
  121. }
  122. map_banks[idx]->name = kzalloc(16, GFP_KERNEL);
  123. if (map_banks[idx]->name == NULL) {
  124. ret = -ENOMEM;
  125. goto error_mem;
  126. }
  127. sprintf(map_banks[idx]->name, "TQM834x-%d", idx);
  128. map_banks[idx]->size = flash_size;
  129. map_banks[idx]->bankwidth = 4;
  130. simple_map_init(map_banks[idx]);
  131. map_banks[idx]->virt = (void __iomem *)
  132. (start_scan_addr + ((idx > 0) ?
  133. (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0));
  134. map_banks[idx]->phys =
  135. flash_addr + ((idx > 0) ?
  136. (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0);
  137. /* start to probe flash chips */
  138. mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]);
  139. if (mtd_banks[idx]) {
  140. mtd_banks[idx]->owner = THIS_MODULE;
  141. mtd_size += mtd_banks[idx]->size;
  142. num_banks++;
  143. pr_debug("%s: bank %ld, name: %s, size: %d bytes \n",
  144. __FUNCTION__, num_banks,
  145. mtd_banks[idx]->name, mtd_banks[idx]->size);
  146. }
  147. }
  148. /* no supported flash chips found */
  149. if (!num_banks) {
  150. printk("TQM834x: No supported flash chips found!\n");
  151. ret = -ENXIO;
  152. goto error_mem;
  153. }
  154. #ifdef CONFIG_MTD_PARTITIONS
  155. /*
  156. * Select static partition definitions
  157. */
  158. n = ARRAY_SIZE(tqm834x_partitions_bank1);
  159. part_banks[0].mtd_part = tqm834x_partitions_bank1;
  160. part_banks[0].type = "static image bank1";
  161. part_banks[0].nums = n;
  162. /* update last partition size to cover actual remaining space */
  163. tqm834x_partitions_bank1[n - 1].size =
  164. mtd_banks[0]->size -
  165. tqm834x_partitions_bank1[n - 1].offset;
  166. /* check if we have second bank? */
  167. if (num_banks == 2) {
  168. n = ARRAY_SIZE(tqm834x_partitions_bank2);
  169. part_banks[1].mtd_part = tqm834x_partitions_bank2;
  170. part_banks[1].type = "static image bank2";
  171. part_banks[1].nums = n;
  172. /* update last partition size to cover actual remaining space */
  173. tqm834x_partitions_bank2[n - 1].size =
  174. mtd_banks[1]->size -
  175. tqm834x_partitions_bank2[n - 1].offset;
  176. }
  177. for(idx = 0; idx < num_banks ; idx++) {
  178. #ifdef CONFIG_MTD_CMDLINE_PARTS
  179. sprintf(mtdid, "%d", idx);
  180. n = parse_mtd_partitions(mtd_banks[idx],
  181. part_probes,
  182. &part_banks[idx].mtd_part,
  183. 0);
  184. pr_debug("%s: %d command line partitions on bank %s\n",
  185. __FUNCTION__, n, mtdid);
  186. if (n > 0) {
  187. part_banks[idx].type = "command line";
  188. part_banks[idx].nums = n;
  189. }
  190. #endif /* CONFIG_MTD_CMDLINE_PARTS */
  191. if (part_banks[idx].nums == 0) {
  192. printk(KERN_NOTICE
  193. "TQM834x flash bank %d: no partition info "
  194. "available, registering whole device\n", idx);
  195. add_mtd_device(mtd_banks[idx]);
  196. } else {
  197. printk(KERN_NOTICE
  198. "TQM834x flash bank %d: Using %s partition "
  199. "definition\n", idx, part_banks[idx].type);
  200. add_mtd_partitions(mtd_banks[idx],
  201. part_banks[idx].mtd_part,
  202. part_banks[idx].nums);
  203. }
  204. }
  205. #else /* ! CONFIG_MTD_PARTITIONS */
  206. printk(KERN_NOTICE "TQM834x flash: registering %d flash banks "
  207. "at once\n", num_banks);
  208. for(idx = 0 ; idx < num_banks ; idx++)
  209. add_mtd_device(mtd_banks[idx]);
  210. #endif /* CONFIG_MTD_PARTITIONS */
  211. return 0;
  212. error_mem:
  213. for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
  214. if (map_banks[idx] != NULL) {
  215. if (map_banks[idx]->name != NULL) {
  216. kfree(map_banks[idx]->name);
  217. map_banks[idx]->name = NULL;
  218. }
  219. kfree(map_banks[idx]);
  220. map_banks[idx] = NULL;
  221. }
  222. }
  223. iounmap((void *)start_scan_addr);
  224. return ret;
  225. }
  226. static void __exit cleanup_tqm834x_mtd(void)
  227. {
  228. unsigned int idx = 0;
  229. for(idx = 0 ; idx < num_banks ; idx++) {
  230. /* destroy mtd_info previously allocated */
  231. if (mtd_banks[idx]) {
  232. del_mtd_partitions(mtd_banks[idx]);
  233. map_destroy(mtd_banks[idx]);
  234. }
  235. /* release map_info not used anymore */
  236. kfree(map_banks[idx]->name);
  237. kfree(map_banks[idx]);
  238. }
  239. if (start_scan_addr) {
  240. iounmap((void *)start_scan_addr);
  241. start_scan_addr = 0;
  242. }
  243. }
  244. module_init(init_tqm834x_mtd);
  245. module_exit(cleanup_tqm834x_mtd);
  246. MODULE_LICENSE("GPL");
  247. MODULE_AUTHOR("Wolfgang Denk <wd@denx.de>");
  248. MODULE_DESCRIPTION("MTD map driver for TQM834x boards");