bootm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 CONFIG_SYS_INIT_RAM_LOCK
  40. #include <asm/cache.h>
  41. #endif
  42. DECLARE_GLOBAL_DATA_PTR;
  43. extern ulong get_effective_memsize(void);
  44. static ulong get_sp (void);
  45. static void set_clocks_in_mhz (bd_t *kbd);
  46. #ifndef CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE
  47. #define CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
  48. #endif
  49. void arch_lmb_reserve(struct lmb *lmb)
  50. {
  51. phys_size_t bootm_size;
  52. ulong size, sp, bootmap_base;
  53. bootmap_base = getenv_bootm_low();
  54. bootm_size = getenv_bootm_size();
  55. #ifdef DEBUG
  56. if (((u64)bootmap_base + bootm_size) >
  57. (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
  58. puts("WARNING: bootm_low + bootm_size exceed total memory\n");
  59. if ((bootmap_base + bootm_size) > get_effective_memsize())
  60. puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
  61. #endif
  62. size = min(bootm_size, get_effective_memsize());
  63. size = min(size, CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
  64. if (size < bootm_size) {
  65. ulong base = bootmap_base + size;
  66. printf("WARNING: adjusting available memory to %lx\n", size);
  67. lmb_reserve(lmb, base, bootm_size - size);
  68. }
  69. /*
  70. * Booting a (Linux) kernel image
  71. *
  72. * Allocate space for command line and board info - the
  73. * address should be as high as possible within the reach of
  74. * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
  75. * memory, which means far enough below the current stack
  76. * pointer.
  77. */
  78. sp = get_sp();
  79. debug ("## Current stack ends at 0x%08lx\n", sp);
  80. /* adjust sp by 1K to be safe */
  81. sp -= 1024;
  82. lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
  83. return ;
  84. }
  85. __attribute__((noinline))
  86. int do_bootm_linux(int flag, int argc, char *argv[], bootm_headers_t *images)
  87. {
  88. ulong initrd_start, initrd_end;
  89. ulong rd_len;
  90. ulong cmd_start, cmd_end, bootmap_base;
  91. bd_t *kbd;
  92. void (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
  93. ulong r7, ulong r8, ulong r9);
  94. int ret;
  95. ulong of_size = images->ft_len;
  96. struct lmb *lmb = &images->lmb;
  97. #if defined(CONFIG_OF_LIBFDT)
  98. char *of_flat_tree = images->ft_addr;
  99. #endif
  100. if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
  101. return 1;
  102. kernel = (void (*)(bd_t *, ulong, ulong, ulong,
  103. ulong, ulong, ulong))images->ep;
  104. bootmap_base = getenv_bootm_low();
  105. if (!of_size) {
  106. /* allocate space and init command line */
  107. ret = boot_get_cmdline (lmb, &cmd_start, &cmd_end, bootmap_base);
  108. if (ret) {
  109. puts("ERROR with allocation of cmdline\n");
  110. goto error;
  111. }
  112. /* allocate space for kernel copy of board info */
  113. ret = boot_get_kbd (lmb, &kbd, bootmap_base);
  114. if (ret) {
  115. puts("ERROR with allocation of kernel bd\n");
  116. goto error;
  117. }
  118. set_clocks_in_mhz(kbd);
  119. }
  120. rd_len = images->rd_end - images->rd_start;
  121. #if defined(CONFIG_OF_LIBFDT)
  122. ret = boot_relocate_fdt(lmb, bootmap_base, &of_flat_tree, &of_size);
  123. if (ret)
  124. goto error;
  125. /*
  126. * Add the chosen node if it doesn't exist, add the env and bd_t
  127. * if the user wants it (the logic is in the subroutines).
  128. */
  129. if (of_size) {
  130. if (fdt_chosen(of_flat_tree, 1) < 0) {
  131. puts ("ERROR: ");
  132. puts ("/chosen node create failed");
  133. puts (" - must RESET the board to recover.\n");
  134. goto error;
  135. }
  136. #ifdef CONFIG_OF_BOARD_SETUP
  137. /* Call the board-specific fixup routine */
  138. ft_board_setup(of_flat_tree, gd->bd);
  139. #endif
  140. }
  141. /* Fixup the fdt memreserve now that we know how big it is */
  142. if (of_flat_tree) {
  143. /* Delete the old LMB reservation */
  144. lmb_free(lmb, (phys_addr_t)(u32)of_flat_tree,
  145. (phys_size_t)fdt_totalsize(of_flat_tree));
  146. ret = fdt_resize(of_flat_tree);
  147. if (ret < 0)
  148. goto error;
  149. of_size = ret;
  150. if ((of_flat_tree) && (initrd_start && initrd_end))
  151. of_size += FDT_RAMDISK_OVERHEAD;
  152. /* Create a new LMB reservation */
  153. lmb_reserve(lmb, (ulong)of_flat_tree, of_size);
  154. }
  155. #endif /* CONFIG_OF_LIBFDT */
  156. ret = boot_ramdisk_high (lmb, images->rd_start, rd_len, &initrd_start, &initrd_end);
  157. if (ret)
  158. goto error;
  159. #if defined(CONFIG_OF_LIBFDT)
  160. /* fixup the initrd now that we know where it should be */
  161. if ((of_flat_tree) && (initrd_start && initrd_end))
  162. fdt_initrd(of_flat_tree, initrd_start, initrd_end, 1);
  163. #endif
  164. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  165. (ulong)kernel);
  166. show_boot_progress (15);
  167. #if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  168. unlock_ram_in_cache();
  169. #endif
  170. #if defined(CONFIG_OF_LIBFDT)
  171. if (of_flat_tree) { /* device tree; boot new style */
  172. /*
  173. * Linux Kernel Parameters (passing device tree):
  174. * r3: pointer to the fdt
  175. * r4: 0
  176. * r5: 0
  177. * r6: epapr magic
  178. * r7: size of IMA in bytes
  179. * r8: 0
  180. * r9: 0
  181. */
  182. #if defined(CONFIG_85xx) || defined(CONFIG_440)
  183. #define EPAPR_MAGIC (0x45504150)
  184. #else
  185. #define EPAPR_MAGIC (0x65504150)
  186. #endif
  187. debug (" Booting using OF flat tree...\n");
  188. (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
  189. CONFIG_SYS_BOOTMAPSZ, 0, 0);
  190. /* does not return */
  191. } else
  192. #endif
  193. {
  194. /*
  195. * Linux Kernel Parameters (passing board info data):
  196. * r3: ptr to board info data
  197. * r4: initrd_start or 0 if no initrd
  198. * r5: initrd_end - unused if r4 is 0
  199. * r6: Start of command line string
  200. * r7: End of command line string
  201. * r8: 0
  202. * r9: 0
  203. */
  204. debug (" Booting using board info...\n");
  205. (*kernel) (kbd, initrd_start, initrd_end,
  206. cmd_start, cmd_end, 0, 0);
  207. /* does not return */
  208. }
  209. return 1;
  210. error:
  211. return 1;
  212. }
  213. static ulong get_sp (void)
  214. {
  215. ulong sp;
  216. asm( "mr %0,1": "=r"(sp) : );
  217. return sp;
  218. }
  219. static void set_clocks_in_mhz (bd_t *kbd)
  220. {
  221. char *s;
  222. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  223. /* convert all clock information to MHz */
  224. kbd->bi_intfreq /= 1000000L;
  225. kbd->bi_busfreq /= 1000000L;
  226. #if defined(CONFIG_MPC8220)
  227. kbd->bi_inpfreq /= 1000000L;
  228. kbd->bi_pcifreq /= 1000000L;
  229. kbd->bi_pevfreq /= 1000000L;
  230. kbd->bi_flbfreq /= 1000000L;
  231. kbd->bi_vcofreq /= 1000000L;
  232. #endif
  233. #if defined(CONFIG_CPM2)
  234. kbd->bi_cpmfreq /= 1000000L;
  235. kbd->bi_brgfreq /= 1000000L;
  236. kbd->bi_sccfreq /= 1000000L;
  237. kbd->bi_vco /= 1000000L;
  238. #endif
  239. #if defined(CONFIG_MPC5xxx)
  240. kbd->bi_ipbfreq /= 1000000L;
  241. kbd->bi_pcifreq /= 1000000L;
  242. #endif /* CONFIG_MPC5xxx */
  243. }
  244. }