xref: /OK3568_Linux_fs/u-boot/lib/initcall.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (c) 2013 The Chromium OS Authors.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <initcall.h>
9 #include <efi.h>
10 
11 DECLARE_GLOBAL_DATA_PTR;
12 
13 #define TICKS_TO_US(ticks)	((ticks) / (COUNTER_FREQUENCY / 1000000))
14 #define US_TO_MS(ticks)		((ticks) / 1000)
15 #define US_TO_US(ticks)		((ticks) % 1000)
16 
17 #ifdef DEBUG
call_get_ticks(ulong * ticks)18 static inline void call_get_ticks(ulong *ticks) { *ticks = get_ticks(); }
19 #else
call_get_ticks(ulong * ticks)20 static inline void call_get_ticks(ulong *ticks) { }
21 #endif
22 
initcall_run_list(const init_fnc_t init_sequence[])23 int initcall_run_list(const init_fnc_t init_sequence[])
24 {
25 	const init_fnc_t *init_fnc_ptr;
26 	ulong start = 0, end = 0, sum = 0;
27 
28 	if (!gd->sys_start_tick)
29 		gd->sys_start_tick = get_ticks();
30 
31 	for (init_fnc_ptr = init_sequence; *init_fnc_ptr; ++init_fnc_ptr) {
32 		unsigned long reloc_ofs = 0;
33 		int ret;
34 
35 		if (gd->flags & GD_FLG_RELOC)
36 			reloc_ofs = gd->reloc_off;
37 #ifdef CONFIG_EFI_APP
38 		reloc_ofs = (unsigned long)image_base;
39 #endif
40 		debug("initcall: %p", (char *)*init_fnc_ptr - reloc_ofs);
41 		if (gd->flags & GD_FLG_RELOC)
42 			debug(" (relocated to %p)\n", (char *)*init_fnc_ptr);
43 		else
44 			debug("\n");
45 		call_get_ticks(&start);
46 		ret = (*init_fnc_ptr)();
47 		call_get_ticks(&end);
48 
49 		if (start != end) {
50 			sum = TICKS_TO_US(end - gd->sys_start_tick);
51 			debug("\t\t\t\t\t\t\t\t#%8ld us #%4ld.%3ld ms\n",
52 			      TICKS_TO_US(end - start), US_TO_MS(sum), US_TO_US(sum));
53 		}
54 		if (ret) {
55 			printf("initcall sequence %p failed at call %p (err=%d)\n",
56 			       init_sequence,
57 			       (char *)*init_fnc_ptr - reloc_ofs, ret);
58 			return -1;
59 		}
60 	}
61 	return 0;
62 }
63