blob: 5c51cae8ab0b126fb8c391919a51e880e031e08e [file] [log] [blame]
David Feng85fd5f12013-12-14 11:47:35 +08001/*
2 * relocate - common relocation function for AArch64 U-Boot
3 *
4 * (C) Copyright 2013
5 * Albert ARIBAUD <albert.u.boot@aribaud.net>
6 * David Feng <fenghua@phytium.com.cn>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11#include <asm-offsets.h>
12#include <config.h>
13#include <linux/linkage.h>
York Sun11b84ee2014-02-26 13:26:03 -080014#include <asm/macro.h>
David Feng85fd5f12013-12-14 11:47:35 +080015
16/*
17 * void relocate_code (addr_moni)
18 *
19 * This function relocates the monitor code.
20 * x0 holds the destination address.
21 */
22ENTRY(relocate_code)
York Sun11b84ee2014-02-26 13:26:03 -080023 stp x29, x30, [sp, #-32]! /* create a stack frame */
24 mov x29, sp
25 str x0, [sp, #16]
David Feng85fd5f12013-12-14 11:47:35 +080026 /*
27 * Copy u-boot from flash to RAM
28 */
29 ldr x1, =__image_copy_start /* x1 <- SRC &__image_copy_start */
30 subs x9, x0, x1 /* x9 <- relocation offset */
31 b.eq relocate_done /* skip relocation */
32 ldr x2, =__image_copy_end /* x2 <- SRC &__image_copy_end */
33
34copy_loop:
35 ldp x10, x11, [x1], #16 /* copy from source address [x1] */
36 stp x10, x11, [x0], #16 /* copy to target address [x0] */
37 cmp x1, x2 /* until source end address [x2] */
38 b.lo copy_loop
York Sun11b84ee2014-02-26 13:26:03 -080039 str x0, [sp, #24]
David Feng85fd5f12013-12-14 11:47:35 +080040
41 /*
42 * Fix .rela.dyn relocations
43 */
44 ldr x2, =__rel_dyn_start /* x2 <- SRC &__rel_dyn_start */
45 ldr x3, =__rel_dyn_end /* x3 <- SRC &__rel_dyn_end */
46fixloop:
47 ldp x0, x1, [x2], #16 /* (x0,x1) <- (SRC location, fixup) */
48 ldr x4, [x2], #8 /* x4 <- addend */
49 and x1, x1, #0xffffffff
50 cmp x1, #1027 /* relative fixup? */
51 bne fixnext
52
53 /* relative fix: store addend plus offset at dest location */
54 add x0, x0, x9
55 add x4, x4, x9
56 str x4, [x0]
57fixnext:
58 cmp x2, x3
59 b.lo fixloop
60
61relocate_done:
York Sun11b84ee2014-02-26 13:26:03 -080062 switch_el x1, 3f, 2f, 1f
63 bl hang
643: mrs x0, sctlr_el3
65 b 0f
662: mrs x0, sctlr_el2
67 b 0f
681: mrs x0, sctlr_el1
690: tbz w0, #2, 5f /* skip flushing cache if disabled */
70 tbz w0, #12, 4f /* invalide i-cache is enabled */
71 ic iallu /* i-cache invalidate all */
72 isb sy
734: ldp x0, x1, [sp, #16]
74 bl __asm_flush_dcache_range
755: ldp x29, x30, [sp],#16
David Feng85fd5f12013-12-14 11:47:35 +080076 ret
77ENDPROC(relocate_code)