xref: /rk3399_rockchip-uboot/common/init/board_init.c (revision ecc306639e83c9019a5093b77a48685ea40eedc2)
1af6bbd4dSSimon Glass /*
2af6bbd4dSSimon Glass  * Code shared between SPL and U-Boot proper
3af6bbd4dSSimon Glass  *
4af6bbd4dSSimon Glass  * Copyright (c) 2015 Google, Inc
5af6bbd4dSSimon Glass  * Written by Simon Glass <sjg@chromium.org>
6af6bbd4dSSimon Glass  *
7af6bbd4dSSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
8af6bbd4dSSimon Glass  */
9af6bbd4dSSimon Glass 
10af6bbd4dSSimon Glass #include <common.h>
11af6bbd4dSSimon Glass 
12af6bbd4dSSimon Glass DECLARE_GLOBAL_DATA_PTR;
13af6bbd4dSSimon Glass 
1412360982SSimon Glass /*
1512360982SSimon Glass  * It isn't trivial to figure out whether memcpy() exists. The arch-specific
1612360982SSimon Glass  * memcpy() is not normally available in SPL due to code size.
1712360982SSimon Glass  */
1812360982SSimon Glass #if !defined(CONFIG_SPL_BUILD) || \
1912360982SSimon Glass 		(defined(CONFIG_SPL_LIBGENERIC_SUPPORT) && \
2012360982SSimon Glass 		!defined(CONFIG_USE_ARCH_MEMSET))
2112360982SSimon Glass #define _USE_MEMCPY
2212360982SSimon Glass #endif
2312360982SSimon Glass 
24af6bbd4dSSimon Glass /* Unfortunately x86 can't compile this code as gd cannot be assigned */
25af6bbd4dSSimon Glass #ifndef CONFIG_X86
26af6bbd4dSSimon Glass __weak void arch_setup_gd(struct global_data *gd_ptr)
27af6bbd4dSSimon Glass {
28af6bbd4dSSimon Glass 	gd = gd_ptr;
29af6bbd4dSSimon Glass }
30af6bbd4dSSimon Glass #endif /* !CONFIG_X86 */
31af6bbd4dSSimon Glass 
32*ecc30663SAlbert ARIBAUD /*
33*ecc30663SAlbert ARIBAUD  * Allocate reserved space for use as 'globals' from 'top' address and
34*ecc30663SAlbert ARIBAUD  * return 'bottom' address of allocated space
35*ecc30663SAlbert ARIBAUD  *
36*ecc30663SAlbert ARIBAUD  * Notes:
37*ecc30663SAlbert ARIBAUD  *
38*ecc30663SAlbert ARIBAUD  * Actual reservation cannot be done from within this function as
39*ecc30663SAlbert ARIBAUD  * it requires altering the C stack pointer, so this will be done by
40*ecc30663SAlbert ARIBAUD  * the caller upon return from this function.
41*ecc30663SAlbert ARIBAUD  *
42*ecc30663SAlbert ARIBAUD  * IMPORTANT:
43*ecc30663SAlbert ARIBAUD  *
44*ecc30663SAlbert ARIBAUD  * Alignment constraints may differ for each 'chunk' allocated. For now:
45*ecc30663SAlbert ARIBAUD  *
46*ecc30663SAlbert ARIBAUD  * - GD is aligned down on a 16-byte boundary
47*ecc30663SAlbert ARIBAUD  *
48*ecc30663SAlbert ARIBAUD  *  - the early malloc arena is not aligned, therefore it follows the stack
49*ecc30663SAlbert ARIBAUD  *   alignment constraint of the architecture for which we are bulding.
50*ecc30663SAlbert ARIBAUD  *
51*ecc30663SAlbert ARIBAUD  *  - GD is allocated last, so that the return value of this functions is
52*ecc30663SAlbert ARIBAUD  *   both the bottom of the reserved area and the address of GD, should
53*ecc30663SAlbert ARIBAUD  *   the calling context need it.
54*ecc30663SAlbert ARIBAUD  */
55*ecc30663SAlbert ARIBAUD 
56*ecc30663SAlbert ARIBAUD ulong board_init_f_alloc_reserve(ulong top)
57*ecc30663SAlbert ARIBAUD {
58*ecc30663SAlbert ARIBAUD 	/* Reserve early malloc arena */
59*ecc30663SAlbert ARIBAUD #if defined(CONFIG_SYS_MALLOC_F)
60*ecc30663SAlbert ARIBAUD 	top -= CONFIG_SYS_MALLOC_F_LEN;
61*ecc30663SAlbert ARIBAUD #endif
62*ecc30663SAlbert ARIBAUD 	/* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
63*ecc30663SAlbert ARIBAUD 	top = rounddown(top-sizeof(struct global_data), 16);
64*ecc30663SAlbert ARIBAUD 
65*ecc30663SAlbert ARIBAUD 	return top;
66*ecc30663SAlbert ARIBAUD }
67*ecc30663SAlbert ARIBAUD 
68*ecc30663SAlbert ARIBAUD /*
69*ecc30663SAlbert ARIBAUD  * Initialize reserved space (which has been safely allocated on the C
70*ecc30663SAlbert ARIBAUD  * stack from the C runtime environment handling code).
71*ecc30663SAlbert ARIBAUD  *
72*ecc30663SAlbert ARIBAUD  * Notes:
73*ecc30663SAlbert ARIBAUD  *
74*ecc30663SAlbert ARIBAUD  * Actual reservation was done by the caller; the locations from base
75*ecc30663SAlbert ARIBAUD  * to base+size-1 (where 'size' is the value returned by the allocation
76*ecc30663SAlbert ARIBAUD  * function above) can be accessed freely without risk of corrupting the
77*ecc30663SAlbert ARIBAUD  * C runtime environment.
78*ecc30663SAlbert ARIBAUD  *
79*ecc30663SAlbert ARIBAUD  * IMPORTANT:
80*ecc30663SAlbert ARIBAUD  *
81*ecc30663SAlbert ARIBAUD  * Upon return from the allocation function above, on some architectures
82*ecc30663SAlbert ARIBAUD  * the caller will set gd to the lowest reserved location. Therefore, in
83*ecc30663SAlbert ARIBAUD  * this initialization function, the global data MUST be placed at base.
84*ecc30663SAlbert ARIBAUD  *
85*ecc30663SAlbert ARIBAUD  * ALSO IMPORTANT:
86*ecc30663SAlbert ARIBAUD  *
87*ecc30663SAlbert ARIBAUD  * On some architectures, gd will already be good when entering this
88*ecc30663SAlbert ARIBAUD  * function. On others, it will only be good once arch_setup_gd() returns.
89*ecc30663SAlbert ARIBAUD  * Therefore, global data accesses must be done:
90*ecc30663SAlbert ARIBAUD  *
91*ecc30663SAlbert ARIBAUD  * - through gd_ptr if before the call to arch_setup_gd();
92*ecc30663SAlbert ARIBAUD  *
93*ecc30663SAlbert ARIBAUD  * - through gd once arch_setup_gd() has been called.
94*ecc30663SAlbert ARIBAUD  *
95*ecc30663SAlbert ARIBAUD  * Do not use 'gd->' until arch_setup_gd() has been called!
96*ecc30663SAlbert ARIBAUD  *
97*ecc30663SAlbert ARIBAUD  * IMPORTANT TOO:
98*ecc30663SAlbert ARIBAUD  *
99*ecc30663SAlbert ARIBAUD  * Initialization for each "chunk" (GD, early malloc arena...) ends with
100*ecc30663SAlbert ARIBAUD  * an incrementation line of the form 'base += <some size>'. The last of
101*ecc30663SAlbert ARIBAUD  * these incrementations seems useless, as base will not be used any
102*ecc30663SAlbert ARIBAUD  * more after this incrementation; but if/when a new "chunk" is appended,
103*ecc30663SAlbert ARIBAUD  * this increment will be essential as it will give base right value for
104*ecc30663SAlbert ARIBAUD  * this new chunk (which will have to end with its own incrementation
105*ecc30663SAlbert ARIBAUD  * statement). Besides, the compiler's optimizer will silently detect
106*ecc30663SAlbert ARIBAUD  * and remove the last base incrementation, therefore leaving that last
107*ecc30663SAlbert ARIBAUD  * (seemingly useless) incrementation causes no code increase.
108*ecc30663SAlbert ARIBAUD  */
109*ecc30663SAlbert ARIBAUD 
110*ecc30663SAlbert ARIBAUD void board_init_f_init_reserve(ulong base)
111af6bbd4dSSimon Glass {
112af6bbd4dSSimon Glass 	struct global_data *gd_ptr;
11312360982SSimon Glass #ifndef _USE_MEMCPY
11412360982SSimon Glass 	int *ptr;
11512360982SSimon Glass #endif
116af6bbd4dSSimon Glass 
117*ecc30663SAlbert ARIBAUD 	/*
118*ecc30663SAlbert ARIBAUD 	 * clear GD entirely and set it up.
119*ecc30663SAlbert ARIBAUD 	 * Use gd_ptr, as gd may not be properly set yet.
120*ecc30663SAlbert ARIBAUD 	 */
121af6bbd4dSSimon Glass 
122*ecc30663SAlbert ARIBAUD 	gd_ptr = (struct global_data *)base;
123*ecc30663SAlbert ARIBAUD 	/* zero the area */
12412360982SSimon Glass #ifdef _USE_MEMCPY
125af6bbd4dSSimon Glass 	memset(gd_ptr, '\0', sizeof(*gd));
12612360982SSimon Glass #else
12712360982SSimon Glass 	for (ptr = (int *)gd_ptr; ptr < (int *)(gd_ptr + 1); )
12812360982SSimon Glass 		*ptr++ = 0;
12912360982SSimon Glass #endif
130*ecc30663SAlbert ARIBAUD 	/* set GD unless architecture did it already */
131*ecc30663SAlbert ARIBAUD #ifndef CONFIG_X86
132af6bbd4dSSimon Glass 	arch_setup_gd(gd_ptr);
133*ecc30663SAlbert ARIBAUD #endif
134*ecc30663SAlbert ARIBAUD 	/* next alloc will be higher by one GD plus 16-byte alignment */
135*ecc30663SAlbert ARIBAUD 	base += roundup(sizeof(struct global_data), 16);
136*ecc30663SAlbert ARIBAUD 
137*ecc30663SAlbert ARIBAUD 	/*
138*ecc30663SAlbert ARIBAUD 	 * record early malloc arena start.
139*ecc30663SAlbert ARIBAUD 	 * Use gd as it is now properly set for all architectures.
140*ecc30663SAlbert ARIBAUD 	 */
141af6bbd4dSSimon Glass 
1429ac4fc82SFabio Estevam #if defined(CONFIG_SYS_MALLOC_F)
143*ecc30663SAlbert ARIBAUD 	/* go down one 'early malloc arena' */
144*ecc30663SAlbert ARIBAUD 	gd->malloc_base = base;
145*ecc30663SAlbert ARIBAUD 	/* next alloc will be higher by one 'early malloc arena' size */
146*ecc30663SAlbert ARIBAUD 	base += CONFIG_SYS_MALLOC_F_LEN;
147af6bbd4dSSimon Glass #endif
148af6bbd4dSSimon Glass }
149