bootm.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * (C) Copyright 2008 Semihalf
  3. *
  4. * (C) Copyright 2000-2006
  5. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  6. *
  7. * See file CREDITS for list of people who contributed to this
  8. * project.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. #include <common.h>
  26. #include <watchdog.h>
  27. #include <command.h>
  28. #include <image.h>
  29. #include <malloc.h>
  30. #include <zlib.h>
  31. #include <bzlib.h>
  32. #include <environment.h>
  33. #include <asm/byteorder.h>
  34. #if defined(CONFIG_OF_LIBFDT)
  35. #include <fdt.h>
  36. #include <libfdt.h>
  37. #include <fdt_support.h>
  38. #endif
  39. #ifdef CFG_INIT_RAM_LOCK
  40. #include <asm/cache.h>
  41. #endif
  42. DECLARE_GLOBAL_DATA_PTR;
  43. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  44. extern ulong get_effective_memsize(void);
  45. static ulong get_sp (void);
  46. static void set_clocks_in_mhz (bd_t *kbd);
  47. #ifndef CFG_LINUX_LOWMEM_MAX_SIZE
  48. #define CFG_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
  49. #endif
  50. void __attribute__((noinline))
  51. do_bootm_linux(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
  52. bootm_headers_t *images)
  53. {
  54. ulong sp;
  55. ulong initrd_start, initrd_end;
  56. ulong rd_len;
  57. ulong size;
  58. phys_size_t bootm_size;
  59. ulong cmd_start, cmd_end, bootmap_base;
  60. bd_t *kbd;
  61. void (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
  62. ulong r7, ulong r8, ulong r9);
  63. int ret;
  64. ulong of_size = images->ft_len;
  65. struct lmb *lmb = &images->lmb;
  66. #if defined(CONFIG_OF_LIBFDT)
  67. char *of_flat_tree = images->ft_addr;
  68. #endif
  69. kernel = (void (*)(bd_t *, ulong, ulong, ulong,
  70. ulong, ulong, ulong))images->ep;
  71. bootmap_base = getenv_bootm_low();
  72. bootm_size = getenv_bootm_size();
  73. #ifdef DEBUG
  74. if (((u64)bootmap_base + bootm_size) >
  75. (CFG_SDRAM_BASE + (u64)gd->ram_size))
  76. puts("WARNING: bootm_low + bootm_size exceed total memory\n");
  77. if ((bootmap_base + bootm_size) > get_effective_memsize())
  78. puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
  79. #endif
  80. size = min(bootm_size, get_effective_memsize());
  81. size = min(size, CFG_LINUX_LOWMEM_MAX_SIZE);
  82. if (size < bootm_size) {
  83. ulong base = bootmap_base + size;
  84. printf("WARNING: adjusting available memory to %lx\n", size);
  85. lmb_reserve(lmb, base, bootm_size - size);
  86. }
  87. /*
  88. * Booting a (Linux) kernel image
  89. *
  90. * Allocate space for command line and board info - the
  91. * address should be as high as possible within the reach of
  92. * the kernel (see CFG_BOOTMAPSZ settings), but in unused
  93. * memory, which means far enough below the current stack
  94. * pointer.
  95. */
  96. sp = get_sp();
  97. debug ("## Current stack ends at 0x%08lx\n", sp);
  98. /* adjust sp by 1K to be safe */
  99. sp -= 1024;
  100. lmb_reserve(lmb, sp, (CFG_SDRAM_BASE + get_effective_memsize() - sp));
  101. if (!of_size) {
  102. /* allocate space and init command line */
  103. ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
  104. if (ret) {
  105. puts("ERROR with allocation of cmdline\n");
  106. goto error;
  107. }
  108. /* allocate space for kernel copy of board info */
  109. ret = boot_get_kbd (lmb, &kbd, bootmap_base);
  110. if (ret) {
  111. puts("ERROR with allocation of kernel bd\n");
  112. goto error;
  113. }
  114. set_clocks_in_mhz(kbd);
  115. }
  116. rd_len = images->rd_end - images->rd_start;
  117. #if defined(CONFIG_OF_LIBFDT)
  118. ret = boot_relocate_fdt(lmb, bootmap_base, &of_flat_tree, &of_size);
  119. if (ret)
  120. goto error;
  121. /*
  122. * Add the chosen node if it doesn't exist, add the env and bd_t
  123. * if the user wants it (the logic is in the subroutines).
  124. */
  125. if (of_size) {
  126. /* pass in dummy initrd info, we'll fix up later */
  127. if (fdt_chosen(of_flat_tree, images->rd_start, images->rd_end, 0) < 0) {
  128. puts ("ERROR: ");
  129. puts ("/chosen node create failed");
  130. puts (" - must RESET the board to recover.\n");
  131. goto error;
  132. }
  133. #ifdef CONFIG_OF_BOARD_SETUP
  134. /* Call the board-specific fixup routine */
  135. ft_board_setup(of_flat_tree, gd->bd);
  136. #endif
  137. }
  138. /* Fixup the fdt memreserve now that we know how big it is */
  139. if (of_flat_tree) {
  140. int j;
  141. uint64_t addr, size;
  142. int total = fdt_num_mem_rsv(of_flat_tree);
  143. uint actualsize;
  144. for (j = 0; j < total; j++) {
  145. fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
  146. if (addr == (uint64_t)(u32)of_flat_tree) {
  147. fdt_del_mem_rsv(of_flat_tree, j);
  148. break;
  149. }
  150. }
  151. /* Delete the old LMB reservation */
  152. lmb_free(lmb, (phys_addr_t)(u32)of_flat_tree,
  153. (phys_size_t)fdt_totalsize(of_flat_tree));
  154. /* Calculate the actual size of the fdt */
  155. actualsize = fdt_off_dt_strings(of_flat_tree) +
  156. fdt_size_dt_strings(of_flat_tree);
  157. /* Make it so the fdt ends on a page boundary */
  158. actualsize = ALIGN(actualsize, 0x1000);
  159. actualsize = actualsize - ((uint)of_flat_tree & 0xfff);
  160. /* Change the fdt header to reflect the correct size */
  161. fdt_set_totalsize(of_flat_tree, actualsize);
  162. of_size = actualsize;
  163. /* Add the new reservation */
  164. ret = fdt_add_mem_rsv(of_flat_tree, (uint)of_flat_tree,
  165. of_size);
  166. /* Create a new LMB reservation */
  167. lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
  168. }
  169. #endif /* CONFIG_OF_LIBFDT */
  170. ret = boot_ramdisk_high (lmb, images->rd_start, rd_len, &initrd_start, &initrd_end);
  171. if (ret)
  172. goto error;
  173. #if defined(CONFIG_OF_LIBFDT)
  174. /* fixup the initrd now that we know where it should be */
  175. if ((of_flat_tree) && (initrd_start && initrd_end)) {
  176. uint64_t addr, size;
  177. int total = fdt_num_mem_rsv(of_flat_tree);
  178. int j;
  179. /* Look for the dummy entry and delete it */
  180. for (j = 0; j < total; j++) {
  181. fdt_get_mem_rsv(of_flat_tree, j, &addr, &size);
  182. if (addr == images->rd_start) {
  183. fdt_del_mem_rsv(of_flat_tree, j);
  184. break;
  185. }
  186. }
  187. ret = fdt_add_mem_rsv(of_flat_tree, initrd_start,
  188. initrd_end - initrd_start + 1);
  189. if (ret < 0) {
  190. printf("fdt_chosen: %s\n", fdt_strerror(ret));
  191. goto error;
  192. }
  193. do_fixup_by_path_u32(of_flat_tree, "/chosen",
  194. "linux,initrd-start", initrd_start, 0);
  195. do_fixup_by_path_u32(of_flat_tree, "/chosen",
  196. "linux,initrd-end", initrd_end, 0);
  197. }
  198. #endif
  199. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  200. (ulong)kernel);
  201. show_boot_progress (15);
  202. #if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  203. unlock_ram_in_cache();
  204. #endif
  205. #if defined(CONFIG_OF_LIBFDT)
  206. if (of_flat_tree) { /* device tree; boot new style */
  207. /*
  208. * Linux Kernel Parameters (passing device tree):
  209. * r3: pointer to the fdt
  210. * r4: 0
  211. * r5: 0
  212. * r6: epapr magic
  213. * r7: size of IMA in bytes
  214. * r8: 0
  215. * r9: 0
  216. */
  217. #if defined(CONFIG_85xx) || defined(CONFIG_440)
  218. #define EPAPR_MAGIC (0x45504150)
  219. #else
  220. #define EPAPR_MAGIC (0x65504150)
  221. #endif
  222. debug (" Booting using OF flat tree...\n");
  223. (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
  224. CFG_BOOTMAPSZ, 0, 0);
  225. /* does not return */
  226. } else
  227. #endif
  228. {
  229. /*
  230. * Linux Kernel Parameters (passing board info data):
  231. * r3: ptr to board info data
  232. * r4: initrd_start or 0 if no initrd
  233. * r5: initrd_end - unused if r4 is 0
  234. * r6: Start of command line string
  235. * r7: End of command line string
  236. * r8: 0
  237. * r9: 0
  238. */
  239. debug (" Booting using board info...\n");
  240. (*kernel) (kbd, initrd_start, initrd_end,
  241. cmd_start, cmd_end, 0, 0);
  242. /* does not return */
  243. }
  244. return ;
  245. error:
  246. do_reset (cmdtp, flag, argc, argv);
  247. return ;
  248. }
  249. static ulong get_sp (void)
  250. {
  251. ulong sp;
  252. asm( "mr %0,1": "=r"(sp) : );
  253. return sp;
  254. }
  255. static void set_clocks_in_mhz (bd_t *kbd)
  256. {
  257. char *s;
  258. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  259. /* convert all clock information to MHz */
  260. kbd->bi_intfreq /= 1000000L;
  261. kbd->bi_busfreq /= 1000000L;
  262. #if defined(CONFIG_MPC8220)
  263. kbd->bi_inpfreq /= 1000000L;
  264. kbd->bi_pcifreq /= 1000000L;
  265. kbd->bi_pevfreq /= 1000000L;
  266. kbd->bi_flbfreq /= 1000000L;
  267. kbd->bi_vcofreq /= 1000000L;
  268. #endif
  269. #if defined(CONFIG_CPM2)
  270. kbd->bi_cpmfreq /= 1000000L;
  271. kbd->bi_brgfreq /= 1000000L;
  272. kbd->bi_sccfreq /= 1000000L;
  273. kbd->bi_vco /= 1000000L;
  274. #endif
  275. #if defined(CONFIG_MPC5xxx)
  276. kbd->bi_ipbfreq /= 1000000L;
  277. kbd->bi_pcifreq /= 1000000L;
  278. #endif /* CONFIG_MPC5xxx */
  279. }
  280. }