xref: /openbmc/u-boot/common/init/board_init.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2af6bbd4dSSimon Glass /*
3af6bbd4dSSimon Glass  * Code shared between SPL and U-Boot proper
4af6bbd4dSSimon Glass  *
5af6bbd4dSSimon Glass  * Copyright (c) 2015 Google, Inc
6af6bbd4dSSimon Glass  * Written by Simon Glass <sjg@chromium.org>
7af6bbd4dSSimon Glass  */
8af6bbd4dSSimon Glass 
9af6bbd4dSSimon Glass #include <common.h>
10af6bbd4dSSimon Glass 
11af6bbd4dSSimon Glass DECLARE_GLOBAL_DATA_PTR;
12af6bbd4dSSimon Glass 
13adc421e4SAlbert ARIBAUD /* Unfortunately x86 or ARM can't compile this code as gd cannot be assigned */
14adc421e4SAlbert ARIBAUD #if !defined(CONFIG_X86) && !defined(CONFIG_ARM)
arch_setup_gd(struct global_data * gd_ptr)15af6bbd4dSSimon Glass __weak void arch_setup_gd(struct global_data *gd_ptr)
16af6bbd4dSSimon Glass {
17af6bbd4dSSimon Glass 	gd = gd_ptr;
18af6bbd4dSSimon Glass }
19adc421e4SAlbert ARIBAUD #endif /* !CONFIG_X86 && !CONFIG_ARM */
20af6bbd4dSSimon Glass 
21ecc30663SAlbert ARIBAUD /*
22ecc30663SAlbert ARIBAUD  * Allocate reserved space for use as 'globals' from 'top' address and
23ecc30663SAlbert ARIBAUD  * return 'bottom' address of allocated space
24ecc30663SAlbert ARIBAUD  *
25ecc30663SAlbert ARIBAUD  * Notes:
26ecc30663SAlbert ARIBAUD  *
27ecc30663SAlbert ARIBAUD  * Actual reservation cannot be done from within this function as
28ecc30663SAlbert ARIBAUD  * it requires altering the C stack pointer, so this will be done by
29ecc30663SAlbert ARIBAUD  * the caller upon return from this function.
30ecc30663SAlbert ARIBAUD  *
31ecc30663SAlbert ARIBAUD  * IMPORTANT:
32ecc30663SAlbert ARIBAUD  *
33ecc30663SAlbert ARIBAUD  * Alignment constraints may differ for each 'chunk' allocated. For now:
34ecc30663SAlbert ARIBAUD  *
35ecc30663SAlbert ARIBAUD  * - GD is aligned down on a 16-byte boundary
36ecc30663SAlbert ARIBAUD  *
37ecc30663SAlbert ARIBAUD  *  - the early malloc arena is not aligned, therefore it follows the stack
38ecc30663SAlbert ARIBAUD  *   alignment constraint of the architecture for which we are bulding.
39ecc30663SAlbert ARIBAUD  *
40ecc30663SAlbert ARIBAUD  *  - GD is allocated last, so that the return value of this functions is
41ecc30663SAlbert ARIBAUD  *   both the bottom of the reserved area and the address of GD, should
42ecc30663SAlbert ARIBAUD  *   the calling context need it.
43ecc30663SAlbert ARIBAUD  */
44ecc30663SAlbert ARIBAUD 
board_init_f_alloc_reserve(ulong top)45ecc30663SAlbert ARIBAUD ulong board_init_f_alloc_reserve(ulong top)
46ecc30663SAlbert ARIBAUD {
47ecc30663SAlbert ARIBAUD 	/* Reserve early malloc arena */
48f1896c45SAndy Yan #if CONFIG_VAL(SYS_MALLOC_F_LEN)
49f1896c45SAndy Yan 	top -= CONFIG_VAL(SYS_MALLOC_F_LEN);
50ecc30663SAlbert ARIBAUD #endif
51ecc30663SAlbert ARIBAUD 	/* LAST : reserve GD (rounded up to a multiple of 16 bytes) */
52ecc30663SAlbert ARIBAUD 	top = rounddown(top-sizeof(struct global_data), 16);
53ecc30663SAlbert ARIBAUD 
54ecc30663SAlbert ARIBAUD 	return top;
55ecc30663SAlbert ARIBAUD }
56ecc30663SAlbert ARIBAUD 
57ecc30663SAlbert ARIBAUD /*
58ecc30663SAlbert ARIBAUD  * Initialize reserved space (which has been safely allocated on the C
59ecc30663SAlbert ARIBAUD  * stack from the C runtime environment handling code).
60ecc30663SAlbert ARIBAUD  *
61ecc30663SAlbert ARIBAUD  * Notes:
62ecc30663SAlbert ARIBAUD  *
63ecc30663SAlbert ARIBAUD  * Actual reservation was done by the caller; the locations from base
64ecc30663SAlbert ARIBAUD  * to base+size-1 (where 'size' is the value returned by the allocation
65ecc30663SAlbert ARIBAUD  * function above) can be accessed freely without risk of corrupting the
66ecc30663SAlbert ARIBAUD  * C runtime environment.
67ecc30663SAlbert ARIBAUD  *
68ecc30663SAlbert ARIBAUD  * IMPORTANT:
69ecc30663SAlbert ARIBAUD  *
70ecc30663SAlbert ARIBAUD  * Upon return from the allocation function above, on some architectures
71ecc30663SAlbert ARIBAUD  * the caller will set gd to the lowest reserved location. Therefore, in
72ecc30663SAlbert ARIBAUD  * this initialization function, the global data MUST be placed at base.
73ecc30663SAlbert ARIBAUD  *
74ecc30663SAlbert ARIBAUD  * ALSO IMPORTANT:
75ecc30663SAlbert ARIBAUD  *
76ecc30663SAlbert ARIBAUD  * On some architectures, gd will already be good when entering this
77ecc30663SAlbert ARIBAUD  * function. On others, it will only be good once arch_setup_gd() returns.
78ecc30663SAlbert ARIBAUD  * Therefore, global data accesses must be done:
79ecc30663SAlbert ARIBAUD  *
80ecc30663SAlbert ARIBAUD  * - through gd_ptr if before the call to arch_setup_gd();
81ecc30663SAlbert ARIBAUD  *
82ecc30663SAlbert ARIBAUD  * - through gd once arch_setup_gd() has been called.
83ecc30663SAlbert ARIBAUD  *
84ecc30663SAlbert ARIBAUD  * Do not use 'gd->' until arch_setup_gd() has been called!
85ecc30663SAlbert ARIBAUD  *
86ecc30663SAlbert ARIBAUD  * IMPORTANT TOO:
87ecc30663SAlbert ARIBAUD  *
88ecc30663SAlbert ARIBAUD  * Initialization for each "chunk" (GD, early malloc arena...) ends with
89ecc30663SAlbert ARIBAUD  * an incrementation line of the form 'base += <some size>'. The last of
90ecc30663SAlbert ARIBAUD  * these incrementations seems useless, as base will not be used any
91ecc30663SAlbert ARIBAUD  * more after this incrementation; but if/when a new "chunk" is appended,
92ecc30663SAlbert ARIBAUD  * this increment will be essential as it will give base right value for
93ecc30663SAlbert ARIBAUD  * this new chunk (which will have to end with its own incrementation
94ecc30663SAlbert ARIBAUD  * statement). Besides, the compiler's optimizer will silently detect
95ecc30663SAlbert ARIBAUD  * and remove the last base incrementation, therefore leaving that last
96ecc30663SAlbert ARIBAUD  * (seemingly useless) incrementation causes no code increase.
97ecc30663SAlbert ARIBAUD  */
98ecc30663SAlbert ARIBAUD 
board_init_f_init_reserve(ulong base)99ecc30663SAlbert ARIBAUD void board_init_f_init_reserve(ulong base)
100af6bbd4dSSimon Glass {
101af6bbd4dSSimon Glass 	struct global_data *gd_ptr;
102af6bbd4dSSimon Glass 
103ecc30663SAlbert ARIBAUD 	/*
104ecc30663SAlbert ARIBAUD 	 * clear GD entirely and set it up.
105ecc30663SAlbert ARIBAUD 	 * Use gd_ptr, as gd may not be properly set yet.
106ecc30663SAlbert ARIBAUD 	 */
107af6bbd4dSSimon Glass 
108ecc30663SAlbert ARIBAUD 	gd_ptr = (struct global_data *)base;
109ecc30663SAlbert ARIBAUD 	/* zero the area */
110af6bbd4dSSimon Glass 	memset(gd_ptr, '\0', sizeof(*gd));
111ecc30663SAlbert ARIBAUD 	/* set GD unless architecture did it already */
112af7a5551SSimon Glass #if !defined(CONFIG_ARM)
113af6bbd4dSSimon Glass 	arch_setup_gd(gd_ptr);
114ecc30663SAlbert ARIBAUD #endif
115ecc30663SAlbert ARIBAUD 	/* next alloc will be higher by one GD plus 16-byte alignment */
116ecc30663SAlbert ARIBAUD 	base += roundup(sizeof(struct global_data), 16);
117ecc30663SAlbert ARIBAUD 
118ecc30663SAlbert ARIBAUD 	/*
119ecc30663SAlbert ARIBAUD 	 * record early malloc arena start.
120ecc30663SAlbert ARIBAUD 	 * Use gd as it is now properly set for all architectures.
121ecc30663SAlbert ARIBAUD 	 */
122af6bbd4dSSimon Glass 
123f1896c45SAndy Yan #if CONFIG_VAL(SYS_MALLOC_F_LEN)
124ecc30663SAlbert ARIBAUD 	/* go down one 'early malloc arena' */
125ecc30663SAlbert ARIBAUD 	gd->malloc_base = base;
126ecc30663SAlbert ARIBAUD 	/* next alloc will be higher by one 'early malloc arena' size */
127f1896c45SAndy Yan 	base += CONFIG_VAL(SYS_MALLOC_F_LEN);
128af6bbd4dSSimon Glass #endif
129af6bbd4dSSimon Glass }
130496c5483SHeiko Schocher 
131496c5483SHeiko Schocher /*
132496c5483SHeiko Schocher  * Board-specific Platform code can reimplement show_boot_progress () if needed
133496c5483SHeiko Schocher  */
show_boot_progress(int val)134496c5483SHeiko Schocher __weak void show_boot_progress(int val) {}
135