1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) 2016-2018 Texas Instruments Incorporated - http://www.ti.com/
4 * Andrew F. Davis <afd@ti.com>
5 */
6
7 #include <console.h>
8 #include <drivers/gic.h>
9 #include <drivers/sec_proxy.h>
10 #include <drivers/serial8250_uart.h>
11 #include <drivers/ti_sci.h>
12 #include <kernel/boot.h>
13 #include <kernel/panic.h>
14 #include <kernel/tee_common_otp.h>
15 #include <mm/core_memprot.h>
16 #include <mm/tee_pager.h>
17 #include <platform_config.h>
18 #include <stdint.h>
19 #include <string_ext.h>
20
21 static struct serial8250_uart_data console_data;
22
23 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GICC_BASE, GICC_SIZE);
24 register_phys_mem_pgdir(MEM_AREA_IO_SEC, GICD_BASE, GICD_SIZE);
25 register_phys_mem_pgdir(MEM_AREA_IO_NSEC, CONSOLE_UART_BASE,
26 SERIAL8250_UART_REG_SIZE);
27 register_phys_mem_pgdir(MEM_AREA_IO_SEC, SEC_PROXY_DATA_BASE,
28 SEC_PROXY_DATA_SIZE);
29 register_phys_mem_pgdir(MEM_AREA_IO_SEC, SEC_PROXY_SCFG_BASE,
30 SEC_PROXY_SCFG_SIZE);
31 register_phys_mem_pgdir(MEM_AREA_IO_SEC, SEC_PROXY_RT_BASE, SEC_PROXY_RT_SIZE);
32 register_ddr(DRAM0_BASE, DRAM0_SIZE);
33 register_ddr(DRAM1_BASE, DRAM1_SIZE);
34
boot_primary_init_intc(void)35 void boot_primary_init_intc(void)
36 {
37 gic_init(GICC_BASE, GICD_BASE);
38 }
39
boot_secondary_init_intc(void)40 void boot_secondary_init_intc(void)
41 {
42 gic_init_per_cpu();
43 }
44
plat_console_init(void)45 void plat_console_init(void)
46 {
47 serial8250_uart_init(&console_data, CONSOLE_UART_BASE,
48 CONSOLE_UART_CLK_IN_HZ, CONSOLE_BAUDRATE);
49 register_serial_console(&console_data.chip);
50 }
51
52 #ifndef PLATFORM_FLAVOR_am62lx
init_ti_sci(void)53 static TEE_Result init_ti_sci(void)
54 {
55 TEE_Result ret = TEE_SUCCESS;
56
57 ret = k3_sec_proxy_init();
58 if (ret != TEE_SUCCESS)
59 return ret;
60
61 ret = ti_sci_init();
62 if (ret)
63 return TEE_ERROR_GENERIC;
64
65 return TEE_SUCCESS;
66 }
67
68 /*
69 * TISCI services are required for initialization of TRNG service that gets
70 * initialized during service_init_crypto.
71 *
72 * Initialize TISCI service just before service_init_crypto.
73 */
74 early_init_late(init_ti_sci);
75
secure_boot_information(void)76 static TEE_Result secure_boot_information(void)
77 {
78 uint32_t keycnt = 0;
79 uint32_t keyrev = 0;
80 uint32_t swrev = 0;
81
82 if (!ti_sci_get_swrev(&swrev))
83 IMSG("Secure Board Configuration Software: Rev %"PRIu32,
84 swrev);
85
86 if (!ti_sci_get_keycnt_keyrev(&keycnt, &keyrev))
87 IMSG("Secure Boot Keys: Count %"PRIu32 ", Rev %"PRIu32,
88 keycnt, keyrev);
89
90 return TEE_SUCCESS;
91 }
92
93 service_init_late(secure_boot_information);
94
tee_otp_get_hw_unique_key(struct tee_hw_unique_key * hwkey)95 TEE_Result tee_otp_get_hw_unique_key(struct tee_hw_unique_key *hwkey)
96 {
97 uint8_t dkek[SA2UL_DKEK_KEY_LEN] = { };
98 int ret = 0;
99
100 assert(SA2UL_DKEK_KEY_LEN >= HW_UNIQUE_KEY_LENGTH);
101
102 ret = ti_sci_get_dkek(0, "OP-TEE", "DKEK", dkek);
103 if (ret) {
104 EMSG("Could not get HUK");
105 return TEE_ERROR_SECURITY;
106 }
107
108 memcpy(&hwkey->data[0], dkek, sizeof(hwkey->data));
109 memzero_explicit(&dkek, sizeof(dkek));
110
111 IMSG("HUK Initialized");
112
113 return TEE_SUCCESS;
114 }
115 #endif /* PLATFORM_FLAVOR_am62lx */
116