crt0.S 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * crt0 - C-runtime startup Code for ARM U-Boot
  3. *
  4. * Copyright (c) 2012 Albert ARIBAUD <albert.u.boot@aribaud.net>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <config.h>
  9. #include <asm-offsets.h>
  10. #include <linux/linkage.h>
  11. /*
  12. * This file handles the target-independent stages of the U-Boot
  13. * start-up where a C runtime environment is needed. Its entry point
  14. * is _main and is branched into from the target's start.S file.
  15. *
  16. * _main execution sequence is:
  17. *
  18. * 1. Set up initial environment for calling board_init_f().
  19. * This environment only provides a stack and a place to store
  20. * the GD ('global data') structure, both located in some readily
  21. * available RAM (SRAM, locked cache...). In this context, VARIABLE
  22. * global data, initialized or not (BSS), are UNAVAILABLE; only
  23. * CONSTANT initialized data are available.
  24. *
  25. * 2. Call board_init_f(). This function prepares the hardware for
  26. * execution from system RAM (DRAM, DDR...) As system RAM may not
  27. * be available yet, , board_init_f() must use the current GD to
  28. * store any data which must be passed on to later stages. These
  29. * data include the relocation destination, the future stack, and
  30. * the future GD location.
  31. *
  32. * (the following applies only to non-SPL builds)
  33. *
  34. * 3. Set up intermediate environment where the stack and GD are the
  35. * ones allocated by board_init_f() in system RAM, but BSS and
  36. * initialized non-const data are still not available.
  37. *
  38. * 4. Call relocate_code(). This function relocates U-Boot from its
  39. * current location into the relocation destination computed by
  40. * board_init_f().
  41. *
  42. * 5. Set up final environment for calling board_init_r(). This
  43. * environment has BSS (initialized to 0), initialized non-const
  44. * data (initialized to their intended value), and stack in system
  45. * RAM. GD has retained values set by board_init_f(). Some CPUs
  46. * have some work left to do at this point regarding memory, so
  47. * call c_runtime_cpu_setup.
  48. *
  49. * 6. Branch to board_init_r().
  50. */
  51. /*
  52. * entry point of crt0 sequence
  53. */
  54. ENTRY(_main)
  55. /*
  56. * Set up initial C runtime environment and call board_init_f(0).
  57. */
  58. #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_SPL_STACK)
  59. ldr sp, =(CONFIG_SPL_STACK)
  60. #else
  61. ldr sp, =(CONFIG_SYS_INIT_SP_ADDR)
  62. #endif
  63. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  64. mov r2, sp
  65. sub sp, sp, #GD_SIZE /* allocate one GD above SP */
  66. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  67. mov r9, sp /* GD is above SP */
  68. mov r1, sp
  69. mov r0, #0
  70. clr_gd:
  71. cmp r1, r2 /* while not at end of GD */
  72. strlo r0, [r1] /* clear 32-bit GD word */
  73. addlo r1, r1, #4 /* move to next */
  74. blo clr_gd
  75. #if defined(CONFIG_SYS_MALLOC_F_LEN) && !defined(CONFIG_SPL_BUILD)
  76. sub sp, sp, #CONFIG_SYS_MALLOC_F_LEN
  77. str sp, [r9, #GD_MALLOC_BASE]
  78. #endif
  79. /* mov r0, #0 not needed due to above code */
  80. bl board_init_f
  81. #if ! defined(CONFIG_SPL_BUILD)
  82. /*
  83. * Set up intermediate environment (new sp and gd) and call
  84. * relocate_code(addr_moni). Trick here is that we'll return
  85. * 'here' but relocated.
  86. */
  87. ldr sp, [r9, #GD_START_ADDR_SP] /* sp = gd->start_addr_sp */
  88. bic sp, sp, #7 /* 8-byte alignment for ABI compliance */
  89. ldr r9, [r9, #GD_BD] /* r9 = gd->bd */
  90. sub r9, r9, #GD_SIZE /* new GD is below bd */
  91. adr lr, here
  92. ldr r0, [r9, #GD_RELOC_OFF] /* r0 = gd->reloc_off */
  93. add lr, lr, r0
  94. ldr r0, [r9, #GD_RELOCADDR] /* r0 = gd->relocaddr */
  95. b relocate_code
  96. here:
  97. /*
  98. * now relocate vectors
  99. */
  100. bl relocate_vectors
  101. /* Set up final (full) environment */
  102. bl c_runtime_cpu_setup /* we still call old routine here */
  103. ldr r0, =__bss_start /* this is auto-relocated! */
  104. ldr r1, =__bss_end /* this is auto-relocated! */
  105. mov r2, #0x00000000 /* prepare zero to clear BSS */
  106. clbss_l:cmp r0, r1 /* while not at end of BSS */
  107. strlo r2, [r0] /* clear 32-bit BSS word */
  108. addlo r0, r0, #4 /* move to next */
  109. blo clbss_l
  110. bl coloured_LED_init
  111. bl red_led_on
  112. /* call board_init_r(gd_t *id, ulong dest_addr) */
  113. mov r0, r9 /* gd_t */
  114. ldr r1, [r9, #GD_RELOCADDR] /* dest_addr */
  115. /* call board_init_r */
  116. ldr pc, =board_init_r /* this is auto-relocated! */
  117. /* we should not return here. */
  118. #endif
  119. ENDPROC(_main)