bootm.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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 <fdt.h>
  37. #include <libfdt.h>
  38. #include <fdt_support.h>
  39. #endif
  40. #ifdef CONFIG_SYS_INIT_RAM_LOCK
  41. #include <asm/cache.h>
  42. #endif
  43. DECLARE_GLOBAL_DATA_PTR;
  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 CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE
  48. #define CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE (768*1024*1024)
  49. #endif
  50. static void boot_jump_linux(bootm_headers_t *images)
  51. {
  52. void (*kernel)(bd_t *, ulong r4, ulong r5, ulong r6,
  53. ulong r7, ulong r8, ulong r9);
  54. #ifdef CONFIG_OF_LIBFDT
  55. char *of_flat_tree = images->ft_addr;
  56. #endif
  57. kernel = (void (*)(bd_t *, ulong, ulong, ulong,
  58. ulong, ulong, ulong))images->ep;
  59. debug ("## Transferring control to Linux (at address %08lx) ...\n",
  60. (ulong)kernel);
  61. show_boot_progress (15);
  62. #if defined(CONFIG_SYS_INIT_RAM_LOCK) && !defined(CONFIG_E500)
  63. unlock_ram_in_cache();
  64. #endif
  65. #if defined(CONFIG_OF_LIBFDT)
  66. if (of_flat_tree) { /* device tree; boot new style */
  67. /*
  68. * Linux Kernel Parameters (passing device tree):
  69. * r3: pointer to the fdt
  70. * r4: 0
  71. * r5: 0
  72. * r6: epapr magic
  73. * r7: size of IMA in bytes
  74. * r8: 0
  75. * r9: 0
  76. */
  77. #if defined(CONFIG_MPC85xx) || defined(CONFIG_440)
  78. #define EPAPR_MAGIC (0x45504150)
  79. #else
  80. #define EPAPR_MAGIC (0x65504150)
  81. #endif
  82. debug (" Booting using OF flat tree...\n");
  83. WATCHDOG_RESET ();
  84. (*kernel) ((bd_t *)of_flat_tree, 0, 0, EPAPR_MAGIC,
  85. getenv_bootm_mapsize(), 0, 0);
  86. /* does not return */
  87. } else
  88. #endif
  89. {
  90. /*
  91. * Linux Kernel Parameters (passing board info data):
  92. * r3: ptr to board info data
  93. * r4: initrd_start or 0 if no initrd
  94. * r5: initrd_end - unused if r4 is 0
  95. * r6: Start of command line string
  96. * r7: End of command line string
  97. * r8: 0
  98. * r9: 0
  99. */
  100. ulong cmd_start = images->cmdline_start;
  101. ulong cmd_end = images->cmdline_end;
  102. ulong initrd_start = images->initrd_start;
  103. ulong initrd_end = images->initrd_end;
  104. bd_t *kbd = images->kbd;
  105. debug (" Booting using board info...\n");
  106. WATCHDOG_RESET ();
  107. (*kernel) (kbd, initrd_start, initrd_end,
  108. cmd_start, cmd_end, 0, 0);
  109. /* does not return */
  110. }
  111. return ;
  112. }
  113. void arch_lmb_reserve(struct lmb *lmb)
  114. {
  115. phys_size_t bootm_size;
  116. ulong size, sp, bootmap_base;
  117. bootmap_base = getenv_bootm_low();
  118. bootm_size = getenv_bootm_size();
  119. #ifdef DEBUG
  120. if (((u64)bootmap_base + bootm_size) >
  121. (CONFIG_SYS_SDRAM_BASE + (u64)gd->ram_size))
  122. puts("WARNING: bootm_low + bootm_size exceed total memory\n");
  123. if ((bootmap_base + bootm_size) > get_effective_memsize())
  124. puts("WARNING: bootm_low + bootm_size exceed eff. memory\n");
  125. #endif
  126. size = min(bootm_size, get_effective_memsize());
  127. size = min(size, CONFIG_SYS_LINUX_LOWMEM_MAX_SIZE);
  128. if (size < bootm_size) {
  129. ulong base = bootmap_base + size;
  130. printf("WARNING: adjusting available memory to %lx\n", size);
  131. lmb_reserve(lmb, base, bootm_size - size);
  132. }
  133. /*
  134. * Booting a (Linux) kernel image
  135. *
  136. * Allocate space for command line and board info - the
  137. * address should be as high as possible within the reach of
  138. * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
  139. * memory, which means far enough below the current stack
  140. * pointer.
  141. */
  142. sp = get_sp();
  143. debug ("## Current stack ends at 0x%08lx\n", sp);
  144. /* adjust sp by 4K to be safe */
  145. sp -= 4096;
  146. lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - sp));
  147. #ifdef CONFIG_MP
  148. cpu_mp_lmb_reserve(lmb);
  149. #endif
  150. return ;
  151. }
  152. static void boot_prep_linux(bootm_headers_t *images)
  153. {
  154. #ifdef CONFIG_MP
  155. /*
  156. * if we are MP make sure to flush the device tree so any changes are
  157. * made visibile to all other cores. In AMP boot scenarios the cores
  158. * might not be HW cache coherent with each other.
  159. */
  160. flush_cache((unsigned long)images->ft_addr, images->ft_len);
  161. #endif
  162. }
  163. static int boot_cmdline_linux(bootm_headers_t *images)
  164. {
  165. ulong of_size = images->ft_len;
  166. struct lmb *lmb = &images->lmb;
  167. ulong *cmd_start = &images->cmdline_start;
  168. ulong *cmd_end = &images->cmdline_end;
  169. int ret = 0;
  170. if (!of_size) {
  171. /* allocate space and init command line */
  172. ret = boot_get_cmdline (lmb, cmd_start, cmd_end);
  173. if (ret) {
  174. puts("ERROR with allocation of cmdline\n");
  175. return ret;
  176. }
  177. }
  178. return ret;
  179. }
  180. static int boot_bd_t_linux(bootm_headers_t *images)
  181. {
  182. ulong of_size = images->ft_len;
  183. struct lmb *lmb = &images->lmb;
  184. bd_t **kbd = &images->kbd;
  185. int ret = 0;
  186. if (!of_size) {
  187. /* allocate space for kernel copy of board info */
  188. ret = boot_get_kbd (lmb, kbd);
  189. if (ret) {
  190. puts("ERROR with allocation of kernel bd\n");
  191. return ret;
  192. }
  193. set_clocks_in_mhz(*kbd);
  194. }
  195. return ret;
  196. }
  197. /*
  198. * Verify the device tree.
  199. *
  200. * This function is called after all device tree fix-ups have been enacted,
  201. * so that the final device tree can be verified. The definition of "verified"
  202. * is up to the specific implementation. However, it generally means that the
  203. * addresses of some of the devices in the device tree are compared with the
  204. * actual addresses at which U-Boot has placed them.
  205. *
  206. * Returns 1 on success, 0 on failure. If 0 is returned, U-boot will halt the
  207. * boot process.
  208. */
  209. static int __ft_verify_fdt(void *fdt)
  210. {
  211. return 1;
  212. }
  213. __attribute__((weak, alias("__ft_verify_fdt"))) int ft_verify_fdt(void *fdt);
  214. static int boot_body_linux(bootm_headers_t *images)
  215. {
  216. ulong rd_len;
  217. struct lmb *lmb = &images->lmb;
  218. ulong *initrd_start = &images->initrd_start;
  219. ulong *initrd_end = &images->initrd_end;
  220. #if defined(CONFIG_OF_LIBFDT)
  221. ulong of_size = images->ft_len;
  222. char **of_flat_tree = &images->ft_addr;
  223. #endif
  224. int ret;
  225. #if defined(CONFIG_OF_LIBFDT)
  226. boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
  227. #endif
  228. /* allocate space and init command line */
  229. ret = boot_cmdline_linux(images);
  230. if (ret)
  231. return ret;
  232. /* allocate space for kernel copy of board info */
  233. ret = boot_bd_t_linux(images);
  234. if (ret)
  235. return ret;
  236. rd_len = images->rd_end - images->rd_start;
  237. ret = boot_ramdisk_high (lmb, images->rd_start, rd_len, initrd_start, initrd_end);
  238. if (ret)
  239. return ret;
  240. #if defined(CONFIG_OF_LIBFDT)
  241. ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
  242. if (ret)
  243. return ret;
  244. /*
  245. * Add the chosen node if it doesn't exist, add the env and bd_t
  246. * if the user wants it (the logic is in the subroutines).
  247. */
  248. if (of_size) {
  249. if (fdt_chosen(*of_flat_tree, 1) < 0) {
  250. puts ("ERROR: ");
  251. puts ("/chosen node create failed");
  252. puts (" - must RESET the board to recover.\n");
  253. return -1;
  254. }
  255. #ifdef CONFIG_OF_BOARD_SETUP
  256. /* Call the board-specific fixup routine */
  257. ft_board_setup(*of_flat_tree, gd->bd);
  258. #endif
  259. /* Delete the old LMB reservation */
  260. lmb_free(lmb, (phys_addr_t)(u32)*of_flat_tree,
  261. (phys_size_t)fdt_totalsize(*of_flat_tree));
  262. ret = fdt_resize(*of_flat_tree);
  263. if (ret < 0)
  264. return ret;
  265. of_size = ret;
  266. if (*initrd_start && *initrd_end) {
  267. of_size += FDT_RAMDISK_OVERHEAD;
  268. fdt_set_totalsize(*of_flat_tree, of_size);
  269. }
  270. /* Create a new LMB reservation */
  271. lmb_reserve(lmb, (ulong)*of_flat_tree, of_size);
  272. /* fixup the initrd now that we know where it should be */
  273. if (*initrd_start && *initrd_end)
  274. fdt_initrd(*of_flat_tree, *initrd_start, *initrd_end, 1);
  275. if (!ft_verify_fdt(*of_flat_tree))
  276. return -1;
  277. }
  278. #endif /* CONFIG_OF_LIBFDT */
  279. return 0;
  280. }
  281. __attribute__((noinline))
  282. int do_bootm_linux(int flag, int argc, char * const argv[], bootm_headers_t *images)
  283. {
  284. int ret;
  285. if (flag & BOOTM_STATE_OS_CMDLINE) {
  286. boot_cmdline_linux(images);
  287. return 0;
  288. }
  289. if (flag & BOOTM_STATE_OS_BD_T) {
  290. boot_bd_t_linux(images);
  291. return 0;
  292. }
  293. if (flag & BOOTM_STATE_OS_PREP) {
  294. boot_prep_linux(images);
  295. return 0;
  296. }
  297. if (flag & BOOTM_STATE_OS_GO) {
  298. boot_jump_linux(images);
  299. return 0;
  300. }
  301. boot_prep_linux(images);
  302. ret = boot_body_linux(images);
  303. if (ret)
  304. return ret;
  305. boot_jump_linux(images);
  306. return 0;
  307. }
  308. static ulong get_sp (void)
  309. {
  310. ulong sp;
  311. asm( "mr %0,1": "=r"(sp) : );
  312. return sp;
  313. }
  314. static void set_clocks_in_mhz (bd_t *kbd)
  315. {
  316. char *s;
  317. if ((s = getenv ("clocks_in_mhz")) != NULL) {
  318. /* convert all clock information to MHz */
  319. kbd->bi_intfreq /= 1000000L;
  320. kbd->bi_busfreq /= 1000000L;
  321. #if defined(CONFIG_MPC8220)
  322. kbd->bi_inpfreq /= 1000000L;
  323. kbd->bi_pcifreq /= 1000000L;
  324. kbd->bi_pevfreq /= 1000000L;
  325. kbd->bi_flbfreq /= 1000000L;
  326. kbd->bi_vcofreq /= 1000000L;
  327. #endif
  328. #if defined(CONFIG_CPM2)
  329. kbd->bi_cpmfreq /= 1000000L;
  330. kbd->bi_brgfreq /= 1000000L;
  331. kbd->bi_sccfreq /= 1000000L;
  332. kbd->bi_vco /= 1000000L;
  333. #endif
  334. #if defined(CONFIG_MPC5xxx)
  335. kbd->bi_ipbfreq /= 1000000L;
  336. kbd->bi_pcifreq /= 1000000L;
  337. #endif /* CONFIG_MPC5xxx */
  338. }
  339. }