xref: /rk3399_rockchip-uboot/include/spl.h (revision b6bda7d5f40f8df92d3195bfab2b7ecbabd3e740)
1 /*
2  * (C) Copyright 2012
3  * Texas Instruments, <www.ti.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 #ifndef	_SPL_H_
8 #define	_SPL_H_
9 
10 /* Platform-specific defines */
11 #include <mmc.h>
12 #include <linux/compiler.h>
13 #include <asm/spl.h>
14 
15 /* Value in r0 indicates we booted from U-Boot */
16 #define UBOOT_NOT_LOADED_FROM_SPL	0x13578642
17 
18 /* Boot type */
19 #define MMCSD_MODE_UNDEFINED	0
20 #define MMCSD_MODE_RAW		1
21 #define MMCSD_MODE_FS		2
22 #define MMCSD_MODE_EMMCBOOT	3
23 
24 #define SPL_NEXT_STAGE_UNDEFINED	0
25 #define SPL_NEXT_STAGE_UBOOT		1
26 #define SPL_NEXT_STAGE_KERNEL		2
27 
28 struct spl_image_info {
29 	const char *name;
30 	u8 os;
31 	uintptr_t load_addr;
32 	uintptr_t entry_point;		/* Next stage entry point */
33 #if CONFIG_IS_ENABLED(ATF)
34 	uintptr_t entry_point_bl32;
35 	uintptr_t entry_point_bl33;
36 #endif
37 #if CONFIG_IS_ENABLED(OPTEE) || CONFIG_IS_ENABLED(KERNEL_BOOT)
38 	uintptr_t entry_point_os;	/* point to uboot or kernel */
39 #endif
40 	void *fdt_addr;
41 	u32 boot_device;
42 	u32 next_stage;
43 	u32 size;
44 	u32 flags;
45 	void *arg;
46 };
47 
48 /*
49  * Information required to load data from a device
50  *
51  * @dev: Pointer to the device, e.g. struct mmc *
52  * @priv: Private data for the device
53  * @bl_len: Block length for reading in bytes
54  * @filename: Name of the fit image file.
55  * @read: Function to call to read from the device
56  */
57 struct spl_load_info {
58 	void *dev;
59 	void *priv;
60 	int bl_len;
61 	const char *filename;
62 	ulong (*read)(struct spl_load_info *load, ulong sector, ulong count,
63 		      void *buf);
64 };
65 
66 /**
67  * spl_load_simple_fit() - Loads a fit image from a device.
68  * @spl_image:	Image description to set up
69  * @info:	Structure containing the information required to load data.
70  * @sector:	Sector number where FIT image is located in the device
71  * @fdt:	Pointer to the copied FIT header.
72  *
73  * Reads the FIT image @sector in the device. Loads u-boot image to
74  * specified load address and copies the dtb to end of u-boot image.
75  * Returns 0 on success.
76  */
77 int spl_load_simple_fit(struct spl_image_info *spl_image,
78 			struct spl_load_info *info, ulong sector, void *fdt);
79 
80 #define SPL_COPY_PAYLOAD_ONLY	1
81 #define SPL_ATF_AARCH32_BL33	BIT(31)
82 
83 /* SPL common functions */
84 void preloader_console_init(void);
85 u32 spl_boot_device(void);
86 u32 spl_boot_mode(const u32 boot_device);
87 void spl_next_stage(struct spl_image_info *spl);
88 void spl_set_bd(void);
89 
90 /**
91  * spl_set_header_raw_uboot() - Set up a standard SPL image structure
92  *
93  * This sets up the given spl_image which the standard values obtained from
94  * config options: CONFIG_SYS_MONITOR_LEN, CONFIG_SYS_UBOOT_START,
95  * CONFIG_SYS_TEXT_BASE.
96  *
97  * @spl_image: Image description to set up
98  */
99 void spl_set_header_raw_uboot(struct spl_image_info *spl_image);
100 
101 /**
102  * spl_parse_image_header() - parse the image header and set up info
103  *
104  * This parses the legacy image header information at @header and sets up
105  * @spl_image according to what is found. If no image header is found, then
106  * a raw image or bootz is assumed. If CONFIG_SPL_PANIC_ON_RAW_IMAGE is
107  * enabled, then this causes a panic. If CONFIG_SPL_RAW_IMAGE_SUPPORT is not
108  * enabled then U-Boot gives up. Otherwise U-Boot sets up the image using
109  * spl_set_header_raw_uboot(), or possibly the bootz header.
110  *
111  * @spl_image: Image description to set up
112  * @header image header to parse
113  * @return 0 if a header was correctly parsed, -ve on error
114  */
115 int spl_parse_image_header(struct spl_image_info *spl_image,
116 			   const struct image_header *header);
117 
118 void spl_board_prepare_for_linux(void);
119 void spl_board_prepare_for_boot(void);
120 int spl_board_ubi_load_image(u32 boot_device);
121 
122 /**
123  * jump_to_image_linux() - Jump to a Linux kernel from SPL
124  *
125  * This jumps into a Linux kernel using the information in @spl_image.
126  *
127  * @spl_image: Image description to set up
128  */
129 void __noreturn jump_to_image_linux(struct spl_image_info *spl_image);
130 
131 /**
132  * spl_start_uboot() - Check if SPL should start the kernel or U-Boot
133  *
134  * This is called by the various SPL loaders to determine whether the board
135  * wants to load the kernel or U-Boot. This function should be provided by
136  * the board.
137  *
138  * @return 0 if SPL should start the kernel, 1 if U-Boot must be started
139  */
140 int spl_start_uboot(void);
141 
142 /**
143  * spl_display_print() - Display a board-specific message in SPL
144  *
145  * If CONFIG_SPL_DISPLAY_PRINT is enabled, U-Boot will call this function
146  * immediately after displaying the SPL console banner ("U-Boot SPL ...").
147  * This function should be provided by the board.
148  */
149 void spl_display_print(void);
150 
151 /**
152  * struct spl_boot_device - Describes a boot device used by SPL
153  *
154  * @boot_device: A number indicating the BOOT_DEVICE type. There are various
155  * BOOT_DEVICE... #defines and enums in U-Boot and they are not consistently
156  * numbered.
157  * @boot_device_name: Named boot device, or NULL if none.
158  *
159  * Note: Additional fields can be added here, bearing in mind that SPL is
160  * size-sensitive and common fields will be present on all boards. This
161  * struct can also be used to return additional information about the load
162  * process if that becomes useful.
163  */
164 struct spl_boot_device {
165 	uint boot_device;
166 	const char *boot_device_name;
167 };
168 
169 /**
170  * Holds information about a way of loading an SPL image
171  *
172  * @name: User-friendly name for this method (e.g. "MMC")
173  * @boot_device: Boot device that this loader supports
174  * @load_image: Function to call to load image
175  */
176 struct spl_image_loader {
177 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
178 	const char *name;
179 #endif
180 	uint boot_device;
181 	/**
182 	 * load_image() - Load an SPL image
183 	 *
184 	 * @spl_image: place to put image information
185 	 * @bootdev: describes the boot device to load from
186 	 */
187 	int (*load_image)(struct spl_image_info *spl_image,
188 			  struct spl_boot_device *bootdev);
189 };
190 
191 /* Declare an SPL image loader */
192 #define SPL_LOAD_IMAGE(__name)					\
193 	ll_entry_declare(struct spl_image_loader, __name, spl_image_loader)
194 
195 /*
196  * _priority is the priority of this method, 0 meaning it will be the top
197  * choice for this device, 9 meaning it is the bottom choice.
198  * _boot_device is the BOOT_DEVICE_... value
199  * _method is the load_image function to call
200  */
201 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
202 #define SPL_LOAD_IMAGE_METHOD(_name, _priority, _boot_device, _method) \
203 	SPL_LOAD_IMAGE(_method ## _priority ## _boot_device) = { \
204 		.name = _name, \
205 		.boot_device = _boot_device, \
206 		.load_image = _method, \
207 	}
208 #else
209 #define SPL_LOAD_IMAGE_METHOD(_name, _priority, _boot_device, _method) \
210 	SPL_LOAD_IMAGE(_method ## _priority ## _boot_device) = { \
211 		.boot_device = _boot_device, \
212 		.load_image = _method, \
213 	}
214 #endif
215 
216 /* SPL FAT image functions */
217 int spl_load_image_fat(struct spl_image_info *spl_image,
218 		       struct blk_desc *block_dev, int partition,
219 		       const char *filename);
220 int spl_load_image_fat_os(struct spl_image_info *spl_image,
221 			  struct blk_desc *block_dev, int partition);
222 
223 void __noreturn jump_to_image_no_args(struct spl_image_info *spl_image);
224 
225 /* SPL EXT image functions */
226 int spl_load_image_ext(struct spl_image_info *spl_image,
227 		       struct blk_desc *block_dev, int partition,
228 		       const char *filename);
229 int spl_load_image_ext_os(struct spl_image_info *spl_image,
230 			  struct blk_desc *block_dev, int partition);
231 
232 /**
233  * spl_early_init() - Set up device tree and driver model in SPL if enabled
234  *
235  * Call this function in board_init_f() if you want to use device tree and
236  * driver model early, before board_init_r() is called.
237  *
238  * If this is not called, then driver model will be inactive in SPL's
239  * board_init_f(), and no device tree will be available.
240  */
241 int spl_early_init(void);
242 
243 /**
244  * spl_init() - Set up device tree and driver model in SPL if enabled
245  *
246  * You can optionally call spl_early_init(), then optionally call spl_init().
247  * This function will be called from board_init_r() if not called earlier.
248  *
249  * Both spl_early_init() and spl_init() perform a similar function except that
250  * the latter will not set up the malloc() area if
251  * CONFIG_SPL_STACK_R_MALLOC_SIMPLE_LEN is enabled, since it is assumed to
252  * already be done by a calll to spl_relocate_stack_gd() before board_init_r()
253  * is reached.
254  *
255  * This function will be called from board_init_r() if not called earlier.
256  *
257  * If this is not called, then driver model will be inactive in SPL's
258  * board_init_f(), and no device tree will be available.
259  */
260 int spl_init(void);
261 
262 #ifdef CONFIG_SPL_BOARD_INIT
263 void spl_board_init(void);
264 #endif
265 
266 /**
267  * spl_was_boot_source() - check if U-Boot booted from SPL
268  *
269  * This will normally be true, but if U-Boot jumps to second U-Boot, it will
270  * be false. This should be implemented by board-specific code.
271  *
272  * @return true if U-Boot booted from SPL, else false
273  */
274 bool spl_was_boot_source(void);
275 
276 /**
277  * spl_dfu_cmd- run dfu command with chosen mmc device interface
278  * @param usb_index - usb controller number
279  * @param mmc_dev -  mmc device nubmer
280  *
281  * @return 0 on success, otherwise error code
282  */
283 int spl_dfu_cmd(int usbctrl, char *dfu_alt_info, char *interface, char *devstr);
284 
285 int spl_mmc_find_device(struct mmc **mmcp, u32 boot_device);
286 int spl_mmc_load_image(struct spl_image_info *spl_image,
287 		       struct spl_boot_device *bootdev);
288 
289 /**
290  * spl_invoke_atf - boot using an ARM trusted firmware image
291  */
292 void spl_invoke_atf(struct spl_image_info *spl_image);
293 
294 /**
295  * bl31_entry - Fill bl31_params structure, and jump to bl31
296  */
297 void bl31_entry(struct spl_image_info *spl_image,
298 		uintptr_t bl31_entry, uintptr_t bl32_entry,
299 		uintptr_t bl33_entry, uintptr_t fdt_addr);
300 
301 /**
302  * spl_optee_entry - entry function for optee
303  *
304  * args defind in op-tee project
305  * https://github.com/OP-TEE/optee_os/
306  * core/arch/arm/kernel/generic_entry_a32.S
307  * @arg0: pagestore
308  * @arg1: (ARMv7 standard bootarg #1)
309  * @arg2: device tree address, (ARMv7 standard bootarg #2)
310  * @arg3: non-secure entry address (ARMv7 bootarg #0)
311  */
312 void spl_optee_entry(void *arg0, void *arg1, void *arg2, void *arg3);
313 
314 /**
315  * board_return_to_bootrom - allow for boards to continue with the boot ROM
316  *
317  * If a board (e.g. the Rockchip RK3368 boards) provide some
318  * supporting functionality for SPL in their boot ROM and the SPL
319  * stage wants to return to the ROM code to continue booting, boards
320  * can implement 'board_return_to_bootrom'.
321  */
322 void board_return_to_bootrom(void);
323 
324 /**
325  * spl_cleanup_before_jump() - cleanup cache/mmu/interrupt, etc before jump
326  *			       to next stage.
327  */
328 void spl_cleanup_before_jump(struct spl_image_info *spl_image);
329 
330 /**
331  * spl_perform_fixups() - arch/board-specific callback before processing
332  *                        the boot-payload
333  */
334 void spl_perform_fixups(struct spl_image_info *spl_image);
335 
336 /**
337  * spl_board_prepare_for_jump() - arch/board-specific callback exactly before
338  *				  jumping to next stage
339  */
340 int spl_board_prepare_for_jump(struct spl_image_info *spl_image);
341 
342 #ifdef CONFIG_SPL_KERNEL_BOOT
343 /**
344  * spl_kernel_partition() - arch/board-specific callback to get kernel partition
345  */
346 const char *spl_kernel_partition(struct spl_image_info *spl,
347 				 struct spl_load_info *info);
348 /**
349  * spl_fdt_fixup_memory() - arch/board-specific fixup kernel dtb memory node.
350  */
351 void spl_fdt_fixup_memory(struct spl_image_info *spl_image);
352 /**
353  * spl_find_hwid_dtb() - Support select kernel dtb based on HW-ID
354  */
355 int spl_find_hwid_dtb(const char *fdt_name);
356 /**
357  * spl_fdt_chosen_bootargs() - Support append bootargs into kernel fdt chosen node
358  */
359 int spl_fdt_chosen_bootargs(struct spl_load_info *info, void *fdt);
360 #endif
361 
362 #endif
363