blob: bb56bac75947ec76c510bf10f528e4ac4605fc44 [file] [log] [blame]
Yann Gautieree8f5422019-02-14 11:13:25 +01001/*
Yann Gautierd0714c02022-01-05 18:02:46 +01002 * Copyright (c) 2015-2022, ARM Limited and Contributors. All rights reserved.
Yann Gautieree8f5422019-02-14 11:13:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Yann Gautiere97b6632019-04-19 10:48:36 +02008#include <errno.h>
Yann Gautieree8f5422019-02-14 11:13:25 +01009
Yann Gautieree8f5422019-02-14 11:13:25 +010010#include <arch_helpers.h>
11#include <common/debug.h>
Yann Gautiera205a5c2021-08-30 15:06:54 +020012#include <drivers/clk.h>
Yann Gautier7a819122021-10-18 15:26:33 +020013#include <drivers/delay_timer.h>
14#include <drivers/st/stm32_console.h>
Yann Gautier3d78a2e2019-02-14 11:01:20 +010015#include <drivers/st/stm32mp_clkfunc.h>
Yann Gautier7a819122021-10-18 15:26:33 +020016#include <drivers/st/stm32mp_reset.h>
Yann Gautiered6515d2021-03-08 15:03:35 +010017#include <lib/smccc.h>
Yann Gautiera55169b2020-01-10 18:18:59 +010018#include <lib/xlat_tables/xlat_tables_v2.h>
Yann Gautieree8f5422019-02-14 11:13:25 +010019#include <plat/common/platform.h>
Yann Gautiered6515d2021-03-08 15:03:35 +010020#include <services/arm_arch_svc.h>
Yann Gautieree8f5422019-02-14 11:13:25 +010021
Yann Gautier7a819122021-10-18 15:26:33 +020022#include <platform_def.h>
23
Nicolas Le Bayondc4bcba2019-11-18 17:12:27 +010024#define HEADER_VERSION_MAJOR_MASK GENMASK(23, 16)
Yann Gautier7a819122021-10-18 15:26:33 +020025#define RESET_TIMEOUT_US_1MS 1000U
26
27static console_t console;
Nicolas Le Bayondc4bcba2019-11-18 17:12:27 +010028
Yann Gautieree8f5422019-02-14 11:13:25 +010029uintptr_t plat_get_ns_image_entrypoint(void)
30{
31 return BL33_BASE;
32}
33
34unsigned int plat_get_syscnt_freq2(void)
35{
36 return read_cntfrq_el0();
37}
38
39static uintptr_t boot_ctx_address;
Yann Gautiercf1360d2020-08-27 18:28:57 +020040static uint16_t boot_itf_selected;
Yann Gautieree8f5422019-02-14 11:13:25 +010041
Yann Gautiera2e2a302019-02-14 11:13:39 +010042void stm32mp_save_boot_ctx_address(uintptr_t address)
Yann Gautieree8f5422019-02-14 11:13:25 +010043{
Yann Gautiercf1360d2020-08-27 18:28:57 +020044 boot_api_context_t *boot_context = (boot_api_context_t *)address;
45
Yann Gautieree8f5422019-02-14 11:13:25 +010046 boot_ctx_address = address;
Yann Gautiercf1360d2020-08-27 18:28:57 +020047 boot_itf_selected = boot_context->boot_interface_selected;
Yann Gautieree8f5422019-02-14 11:13:25 +010048}
49
Yann Gautiera2e2a302019-02-14 11:13:39 +010050uintptr_t stm32mp_get_boot_ctx_address(void)
Yann Gautieree8f5422019-02-14 11:13:25 +010051{
52 return boot_ctx_address;
53}
54
Yann Gautiercf1360d2020-08-27 18:28:57 +020055uint16_t stm32mp_get_boot_itf_selected(void)
56{
57 return boot_itf_selected;
58}
59
Yann Gautier3d78a2e2019-02-14 11:01:20 +010060uintptr_t stm32mp_ddrctrl_base(void)
61{
Yann Gautiera18f61b2020-05-05 17:58:40 +020062 return DDRCTRL_BASE;
Yann Gautier3d78a2e2019-02-14 11:01:20 +010063}
64
65uintptr_t stm32mp_ddrphyc_base(void)
66{
Yann Gautiera18f61b2020-05-05 17:58:40 +020067 return DDRPHYC_BASE;
Yann Gautier3d78a2e2019-02-14 11:01:20 +010068}
69
70uintptr_t stm32mp_pwr_base(void)
71{
Yann Gautiera18f61b2020-05-05 17:58:40 +020072 return PWR_BASE;
Yann Gautier3d78a2e2019-02-14 11:01:20 +010073}
74
75uintptr_t stm32mp_rcc_base(void)
76{
Yann Gautiera18f61b2020-05-05 17:58:40 +020077 return RCC_BASE;
Yann Gautier3d78a2e2019-02-14 11:01:20 +010078}
79
Yann Gautierf540a592019-05-22 19:13:51 +020080bool stm32mp_lock_available(void)
81{
82 const uint32_t c_m_bits = SCTLR_M_BIT | SCTLR_C_BIT;
83
84 /* The spinlocks are used only when MMU and data cache are enabled */
85 return (read_sctlr() & c_m_bits) == c_m_bits;
86}
87
Yann Gautiera55169b2020-01-10 18:18:59 +010088int stm32mp_map_ddr_non_cacheable(void)
89{
90 return mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
91 STM32MP_DDR_MAX_SIZE,
Yann Gautierf3bd87e2020-09-04 15:55:53 +020092 MT_NON_CACHEABLE | MT_RW | MT_SECURE);
Yann Gautiera55169b2020-01-10 18:18:59 +010093}
94
95int stm32mp_unmap_ddr(void)
96{
97 return mmap_remove_dynamic_region(STM32MP_DDR_BASE,
98 STM32MP_DDR_MAX_SIZE);
99}
Yann Gautiered6515d2021-03-08 15:03:35 +0100100
Lionel Debievebc2d88d2019-11-04 14:31:38 +0100101int stm32_get_otp_index(const char *otp_name, uint32_t *otp_idx,
102 uint32_t *otp_len)
103{
104 assert(otp_name != NULL);
105 assert(otp_idx != NULL);
106
107 return dt_find_otp_name(otp_name, otp_idx, otp_len);
108}
109
110int stm32_get_otp_value(const char *otp_name, uint32_t *otp_val)
111{
112 uint32_t otp_idx;
113
114 assert(otp_name != NULL);
115 assert(otp_val != NULL);
116
117 if (stm32_get_otp_index(otp_name, &otp_idx, NULL) != 0) {
118 return -1;
119 }
120
121 if (stm32_get_otp_value_from_idx(otp_idx, otp_val) != 0) {
122 ERROR("BSEC: %s Read Error\n", otp_name);
123 return -1;
124 }
125
126 return 0;
127}
128
129int stm32_get_otp_value_from_idx(const uint32_t otp_idx, uint32_t *otp_val)
130{
131 uint32_t ret = BSEC_NOT_SUPPORTED;
132
133 assert(otp_val != NULL);
134
135#if defined(IMAGE_BL2)
136 ret = bsec_shadow_read_otp(otp_val, otp_idx);
137#elif defined(IMAGE_BL32)
138 ret = bsec_read_otp(otp_val, otp_idx);
139#else
140#error "Not supported"
141#endif
142 if (ret != BSEC_OK) {
143 ERROR("BSEC: idx=%u Read Error\n", otp_idx);
144 return -1;
145 }
146
147 return 0;
148}
149
Yann Gautier414f17c2021-10-18 15:50:05 +0200150#if defined(IMAGE_BL2)
Yann Gautier7a819122021-10-18 15:26:33 +0200151static void reset_uart(uint32_t reset)
152{
153 int ret;
154
155 ret = stm32mp_reset_assert(reset, RESET_TIMEOUT_US_1MS);
156 if (ret != 0) {
157 panic();
158 }
159
160 udelay(2);
161
162 ret = stm32mp_reset_deassert(reset, RESET_TIMEOUT_US_1MS);
163 if (ret != 0) {
164 panic();
165 }
166
167 mdelay(1);
168}
Yann Gautier414f17c2021-10-18 15:50:05 +0200169#endif
Yann Gautier7a819122021-10-18 15:26:33 +0200170
Yann Gautierd1435742021-10-18 10:55:23 +0200171static void set_console(uintptr_t base, uint32_t clk_rate)
172{
173 unsigned int console_flags;
174
175 if (console_stm32_register(base, clk_rate,
Yann Gautierb02dd492022-03-02 14:31:55 +0100176 (uint32_t)STM32MP_UART_BAUDRATE, &console) == 0) {
Yann Gautierd1435742021-10-18 10:55:23 +0200177 panic();
178 }
179
180 console_flags = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH |
181 CONSOLE_FLAG_TRANSLATE_CRLF;
182#if !defined(IMAGE_BL2) && defined(DEBUG)
183 console_flags |= CONSOLE_FLAG_RUNTIME;
184#endif
185
186 console_set_scope(&console, console_flags);
187}
188
Yann Gautier7a819122021-10-18 15:26:33 +0200189int stm32mp_uart_console_setup(void)
190{
191 struct dt_node_info dt_uart_info;
Yann Gautierd0714c02022-01-05 18:02:46 +0100192 uint32_t clk_rate = 0U;
Yann Gautier7a819122021-10-18 15:26:33 +0200193 int result;
Yann Gautier3d8497c2021-10-18 16:06:22 +0200194 uint32_t boot_itf __unused;
195 uint32_t boot_instance __unused;
Yann Gautier7a819122021-10-18 15:26:33 +0200196
197 result = dt_get_stdout_uart_info(&dt_uart_info);
198
199 if ((result <= 0) ||
Yann Gautierd0714c02022-01-05 18:02:46 +0100200 (dt_uart_info.status == DT_DISABLED)) {
201 return -ENODEV;
202 }
203
204#if defined(IMAGE_BL2)
205 if ((dt_uart_info.clock < 0) ||
Yann Gautier7a819122021-10-18 15:26:33 +0200206 (dt_uart_info.reset < 0)) {
207 return -ENODEV;
208 }
Yann Gautierd0714c02022-01-05 18:02:46 +0100209#endif
Yann Gautier7a819122021-10-18 15:26:33 +0200210
Yann Gautier3d8497c2021-10-18 16:06:22 +0200211#if STM32MP_UART_PROGRAMMER || !defined(IMAGE_BL2)
212 stm32_get_boot_interface(&boot_itf, &boot_instance);
213
214 if ((boot_itf == BOOT_API_CTX_BOOT_INTERFACE_SEL_SERIAL_UART) &&
215 (get_uart_address(boot_instance) == dt_uart_info.base)) {
216 return -EACCES;
217 }
218#endif
219
Yann Gautier414f17c2021-10-18 15:50:05 +0200220#if defined(IMAGE_BL2)
Yann Gautier7a819122021-10-18 15:26:33 +0200221 if (dt_set_stdout_pinctrl() != 0) {
222 return -ENODEV;
223 }
224
Yann Gautiera205a5c2021-08-30 15:06:54 +0200225 clk_enable((unsigned long)dt_uart_info.clock);
Yann Gautier7a819122021-10-18 15:26:33 +0200226
227 reset_uart((uint32_t)dt_uart_info.reset);
228
Yann Gautiera205a5c2021-08-30 15:06:54 +0200229 clk_rate = clk_get_rate((unsigned long)dt_uart_info.clock);
Yann Gautierd0714c02022-01-05 18:02:46 +0100230#endif
Yann Gautier7a819122021-10-18 15:26:33 +0200231
Yann Gautierd1435742021-10-18 10:55:23 +0200232 set_console(dt_uart_info.base, clk_rate);
Yann Gautier7a819122021-10-18 15:26:33 +0200233
234 return 0;
235}
236
Yann Gautierd1435742021-10-18 10:55:23 +0200237#if STM32MP_EARLY_CONSOLE
238void stm32mp_setup_early_console(void)
239{
Yann Gautier6e49b7f2022-09-13 13:59:48 +0200240#if defined(IMAGE_BL2) || STM32MP_RECONFIGURE_CONSOLE
Yann Gautierd1435742021-10-18 10:55:23 +0200241 plat_crash_console_init();
Yann Gautier6e49b7f2022-09-13 13:59:48 +0200242#endif
Yann Gautierd1435742021-10-18 10:55:23 +0200243 set_console(STM32MP_DEBUG_USART_BASE, STM32MP_DEBUG_USART_CLK_FRQ);
Yann Gautier2652ba72022-06-09 17:34:30 +0200244 NOTICE("Early console setup\n");
Yann Gautierd1435742021-10-18 10:55:23 +0200245}
246#endif /* STM32MP_EARLY_CONSOLE */
247
Yann Gautiered6515d2021-03-08 15:03:35 +0100248/*****************************************************************************
249 * plat_is_smccc_feature_available() - This function checks whether SMCCC
250 * feature is availabile for platform.
251 * @fid: SMCCC function id
252 *
253 * Return SMC_ARCH_CALL_SUCCESS if SMCCC feature is available and
254 * SMC_ARCH_CALL_NOT_SUPPORTED otherwise.
255 *****************************************************************************/
256int32_t plat_is_smccc_feature_available(u_register_t fid)
257{
258 switch (fid) {
259 case SMCCC_ARCH_SOC_ID:
260 return SMC_ARCH_CALL_SUCCESS;
261 default:
262 return SMC_ARCH_CALL_NOT_SUPPORTED;
263 }
264}
265
266/* Get SOC version */
267int32_t plat_get_soc_version(void)
268{
269 uint32_t chip_id = stm32mp_get_chip_dev_id();
270 uint32_t manfid = SOC_ID_SET_JEP_106(JEDEC_ST_BKID, JEDEC_ST_MFID);
271
272 return (int32_t)(manfid | (chip_id & SOC_ID_IMPL_DEF_MASK));
273}
274
275/* Get SOC revision */
276int32_t plat_get_soc_revision(void)
277{
278 return (int32_t)(stm32mp_get_chip_version() & SOC_ID_REV_MASK);
279}