bootm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <u-boot/zlib.h>
  31. #include <bzlib.h>
  32. #include <environment.h>
  33. #include <asm/byteorder.h>
  34. #include <asm/mp.h>
  35. #if defined(CONFIG_OF_LIBFDT)
  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. static void boot_jump_linux(bootm_headers_t *images)
  50. {
  51. void (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
  52. ulong r7, ulong r8, ulong r9);
  53. #ifdef CONFIG_OF_LIBFDT
  54. char *of_flat_tree = images->ft_addr;
  55. #endif
  56. kernel = (void (*)(bd_t *, ulong, ulong, ulong,
  57. ulong, ulong, ulong))images->ep;
  58. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  59. (ulong)kernel);
  60. bootstage_mark(BOOTSTAGE_ID_RUN_OS);
  61. #if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  62. unlock_ram_in_cache();
  63. #endif
  64. #if defined(CONFIG_OF_LIBFDT)
  65. if (of_flat_tree) { /* device tree; boot new style */
  66. /*
  67. * Linux Kernel Parameters (passing device tree):
  68. * r3: pointer to the fdt
  69. * r4: 0
  70. * r5: 0
  71. * r6: epapr magic
  72. * r7: size of IMA in bytes
  73. * r8: 0
  74. * r9: 0
  75. */
  76. debug (" Booting using OF flat tree...\n");
  77. WATCHDOG_RESET ();
  78. (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
  79. getenv_bootm_mapsize(), 0, 0);
  80. /* does not return */
  81. } else
  82. #endif
  83. {
  84. /*
  85. * Linux Kernel Parameters (passing board info data):
  86. * r3: ptr to board info data
  87. * r4: initrd_start or 0 if no initrd
  88. * r5: initrd_end - unused if r4 is 0
  89. * r6: Start of command line string
  90. * r7: End of command line string
  91. * r8: 0
  92. * r9: 0
  93. */
  94. ulong cmd_start = images->cmdline_start;
  95. ulong cmd_end = images->cmdline_end;
  96. ulong initrd_start = images->initrd_start;
  97. ulong initrd_end = images->initrd_end;
  98. bd_t *kbd = images->kbd;
  99. debug (" Booting using board info...\n");
  100. WATCHDOG_RESET ();
  101. (*kernel) (kbd, initrd_start, initrd_end,
  102. cmd_start, cmd_end, 0, 0);
  103. /* does not return */
  104. }
  105. return ;
  106. }
  107. void arch_lmb_reserve(struct lmb *lmb)
  108. {
  109. phys_size_t bootm_size;
  110. ulong size, sp, bootmap_base;
  111. bootmap_base = getenv_bootm_low();
  112. bootm_size = getenv_bootm_size();
  113. #ifdef DEBUG
  114. if (((u64)bootmap_base + bootm_size) >
  115. (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
  116. puts("WARNING: bootm_low + bootm_size exceed total memory\n");
  117. if ((bootmap_base + bootm_size) > get_effective_memsize())
  118. puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
  119. #endif
  120. size = min(bootm_size, get_effective_memsize());
  121. size = min(size, CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
  122. if (size < bootm_size) {
  123. ulong base = bootmap_base + size;
  124. printf("WARNING: adjusting available memory to %lx\n", size);
  125. lmb_reserve(lmb, base, bootm_size - size);
  126. }
  127. /*
  128. * Booting a (Linux) kernel image
  129. *
  130. * Allocate space for command line and board info - the
  131. * address should be as high as possible within the reach of
  132. * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
  133. * memory, which means far enough below the current stack
  134. * pointer.
  135. */
  136. sp = get_sp();
  137. debug ("## Current stack ends at 0x%08lx\n", sp);
  138. /* adjust sp by 4K to be safe */
  139. sp -= 4096;
  140. lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
  141. #ifdef CONFIG_MP
  142. cpu_mp_lmb_reserve(lmb);
  143. #endif
  144. return ;
  145. }
  146. static void boot_prep_linux(bootm_headers_t *images)
  147. {
  148. #ifdef CONFIG_MP
  149. /*
  150. * if we are MP make sure to flush the device tree so any changes are
  151. * made visibile to all other cores. In AMP boot scenarios the cores
  152. * might not be HW cache coherent with each other.
  153. */
  154. flush_cache((unsigned long)images->ft_addr, images->ft_len);
  155. #endif
  156. }
  157. static int boot_cmdline_linux(bootm_headers_t *images)
  158. {
  159. ulong of_size = images->ft_len;
  160. struct lmb *lmb = &images->lmb;
  161. ulong *cmd_start = &images->cmdline_start;
  162. ulong *cmd_end = &images->cmdline_end;
  163. int ret = 0;
  164. if (!of_size) {
  165. /* allocate space and init command line */
  166. ret = boot_get_cmdline (lmb, cmd_start, cmd_end);
  167. if (ret) {
  168. puts("ERROR with allocation of cmdline\n");
  169. return ret;
  170. }
  171. }
  172. return ret;
  173. }
  174. static int boot_bd_t_linux(bootm_headers_t *images)
  175. {
  176. ulong of_size = images->ft_len;
  177. struct lmb *lmb = &images->lmb;
  178. bd_t **kbd = &images->kbd;
  179. int ret = 0;
  180. if (!of_size) {
  181. /* allocate space for kernel copy of board info */
  182. ret = boot_get_kbd (lmb, kbd);
  183. if (ret) {
  184. puts("ERROR with allocation of kernel bd\n");
  185. return ret;
  186. }
  187. set_clocks_in_mhz(*kbd);
  188. }
  189. return ret;
  190. }
  191. static int boot_body_linux(bootm_headers_t *images)
  192. {
  193. int ret;
  194. /* allocate space for kernel copy of board info */
  195. ret = boot_bd_t_linux(images);
  196. if (ret)
  197. return ret;
  198. ret = image_setup_linux(images);
  199. if (ret)
  200. return ret;
  201. return 0;
  202. }
  203. noinline
  204. int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
  205. {
  206. int ret;
  207. if (flag & BOOTM_STATE_OS_CMDLINE) {
  208. boot_cmdline_linux(images);
  209. return 0;
  210. }
  211. if (flag & BOOTM_STATE_OS_BD_T) {
  212. boot_bd_t_linux(images);
  213. return 0;
  214. }
  215. if (flag & BOOTM_STATE_OS_PREP) {
  216. boot_prep_linux(images);
  217. return 0;
  218. }
  219. boot_prep_linux(images);
  220. ret = boot_body_linux(images);
  221. if (ret)
  222. return ret;
  223. boot_jump_linux(images);
  224. return 0;
  225. }
  226. static ulong get_sp (void)
  227. {
  228. ulong sp;
  229. asm( "mr %0,1": "=r"(sp) : );
  230. return sp;
  231. }
  232. static void set_clocks_in_mhz (bd_t *kbd)
  233. {
  234. char *s;
  235. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  236. /* convert all clock information to MHz */
  237. kbd->bi_intfreq /= 1000000L;
  238. kbd->bi_busfreq /= 1000000L;
  239. #if defined(CONFIG_CPM2)
  240. kbd->bi_cpmfreq /= 1000000L;
  241. kbd->bi_brgfreq /= 1000000L;
  242. kbd->bi_sccfreq /= 1000000L;
  243. kbd->bi_vco /= 1000000L;
  244. #endif
  245. #if defined(CONFIG_MPC5xxx)
  246. kbd->bi_ipbfreq /= 1000000L;
  247. kbd->bi_pcifreq /= 1000000L;
  248. #endif /* CONFIG_MPC5xxx */
  249. }
  250. }