blob: b86572804f2b0c86cb4b81f2e00177faea9e3c44 [file] [log] [blame]
Yann Gautierc9d75b32019-02-14 11:13:25 +01001/*
Yann Gautier3d201782021-03-08 15:03:35 +01002 * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved.
Yann Gautierc9d75b32019-02-14 11:13:25 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Yann Gautier1e919522019-04-19 10:48:36 +02008#include <errno.h>
Yann Gautierc9d75b32019-02-14 11:13:25 +01009
Yann Gautierc9d75b32019-02-14 11:13:25 +010010#include <arch_helpers.h>
11#include <common/debug.h>
Yann Gautier53612f72021-10-18 15:26:33 +020012#include <drivers/delay_timer.h>
13#include <drivers/st/stm32_console.h>
Yann Gautier7ae58c62019-02-14 11:01:20 +010014#include <drivers/st/stm32mp_clkfunc.h>
Yann Gautier53612f72021-10-18 15:26:33 +020015#include <drivers/st/stm32mp_reset.h>
Yann Gautier3d201782021-03-08 15:03:35 +010016#include <lib/smccc.h>
Yann Gautier84686ba2020-01-10 18:18:59 +010017#include <lib/xlat_tables/xlat_tables_v2.h>
Yann Gautierc9d75b32019-02-14 11:13:25 +010018#include <plat/common/platform.h>
Yann Gautier3d201782021-03-08 15:03:35 +010019#include <services/arm_arch_svc.h>
Yann Gautierc9d75b32019-02-14 11:13:25 +010020
Yann Gautier53612f72021-10-18 15:26:33 +020021#include <platform_def.h>
22
Nicolas Le Bayon8ce89182019-11-18 17:12:27 +010023#define HEADER_VERSION_MAJOR_MASK GENMASK(23, 16)
Yann Gautier53612f72021-10-18 15:26:33 +020024#define RESET_TIMEOUT_US_1MS 1000U
25
26static console_t console;
Nicolas Le Bayon8ce89182019-11-18 17:12:27 +010027
Yann Gautierc9d75b32019-02-14 11:13:25 +010028uintptr_t plat_get_ns_image_entrypoint(void)
29{
30 return BL33_BASE;
31}
32
33unsigned int plat_get_syscnt_freq2(void)
34{
35 return read_cntfrq_el0();
36}
37
38static uintptr_t boot_ctx_address;
Yann Gautier7e87ba22020-08-27 18:28:57 +020039static uint16_t boot_itf_selected;
Yann Gautierc9d75b32019-02-14 11:13:25 +010040
Yann Gautier3f9c9782019-02-14 11:13:39 +010041void stm32mp_save_boot_ctx_address(uintptr_t address)
Yann Gautierc9d75b32019-02-14 11:13:25 +010042{
Yann Gautier7e87ba22020-08-27 18:28:57 +020043 boot_api_context_t *boot_context = (boot_api_context_t *)address;
44
Yann Gautierc9d75b32019-02-14 11:13:25 +010045 boot_ctx_address = address;
Yann Gautier7e87ba22020-08-27 18:28:57 +020046 boot_itf_selected = boot_context->boot_interface_selected;
Yann Gautierc9d75b32019-02-14 11:13:25 +010047}
48
Yann Gautier3f9c9782019-02-14 11:13:39 +010049uintptr_t stm32mp_get_boot_ctx_address(void)
Yann Gautierc9d75b32019-02-14 11:13:25 +010050{
51 return boot_ctx_address;
52}
53
Yann Gautier7e87ba22020-08-27 18:28:57 +020054uint16_t stm32mp_get_boot_itf_selected(void)
55{
56 return boot_itf_selected;
57}
58
Yann Gautier7ae58c62019-02-14 11:01:20 +010059uintptr_t stm32mp_ddrctrl_base(void)
60{
Yann Gautierade9ce02020-05-05 17:58:40 +020061 return DDRCTRL_BASE;
Yann Gautier7ae58c62019-02-14 11:01:20 +010062}
63
64uintptr_t stm32mp_ddrphyc_base(void)
65{
Yann Gautierade9ce02020-05-05 17:58:40 +020066 return DDRPHYC_BASE;
Yann Gautier7ae58c62019-02-14 11:01:20 +010067}
68
69uintptr_t stm32mp_pwr_base(void)
70{
Yann Gautierade9ce02020-05-05 17:58:40 +020071 return PWR_BASE;
Yann Gautier7ae58c62019-02-14 11:01:20 +010072}
73
74uintptr_t stm32mp_rcc_base(void)
75{
Yann Gautierade9ce02020-05-05 17:58:40 +020076 return RCC_BASE;
Yann Gautier7ae58c62019-02-14 11:01:20 +010077}
78
Yann Gautiere463d3f2019-05-22 19:13:51 +020079bool stm32mp_lock_available(void)
80{
81 const uint32_t c_m_bits = SCTLR_M_BIT | SCTLR_C_BIT;
82
83 /* The spinlocks are used only when MMU and data cache are enabled */
84 return (read_sctlr() & c_m_bits) == c_m_bits;
85}
86
Yann Gautier1d204ee2021-05-19 18:48:16 +020087#if STM32MP_USE_STM32IMAGE
Yann Gautier1e919522019-04-19 10:48:36 +020088int stm32mp_check_header(boot_api_image_header_t *header, uintptr_t buffer)
89{
90 uint32_t i;
91 uint32_t img_checksum = 0U;
92
93 /*
94 * Check header/payload validity:
95 * - Header magic
96 * - Header version
97 * - Payload checksum
98 */
99 if (header->magic != BOOT_API_IMAGE_HEADER_MAGIC_NB) {
100 ERROR("Header magic\n");
101 return -EINVAL;
102 }
103
Nicolas Le Bayon8ce89182019-11-18 17:12:27 +0100104 if ((header->header_version & HEADER_VERSION_MAJOR_MASK) !=
105 (BOOT_API_HEADER_VERSION & HEADER_VERSION_MAJOR_MASK)) {
Yann Gautier1e919522019-04-19 10:48:36 +0200106 ERROR("Header version\n");
107 return -EINVAL;
108 }
109
110 for (i = 0U; i < header->image_length; i++) {
111 img_checksum += *(uint8_t *)(buffer + i);
112 }
113
114 if (header->payload_checksum != img_checksum) {
115 ERROR("Checksum: 0x%x (awaited: 0x%x)\n", img_checksum,
116 header->payload_checksum);
117 return -EINVAL;
118 }
119
120 return 0;
121}
Yann Gautier1d204ee2021-05-19 18:48:16 +0200122#endif /* STM32MP_USE_STM32IMAGE */
Yann Gautier84686ba2020-01-10 18:18:59 +0100123
124int stm32mp_map_ddr_non_cacheable(void)
125{
126 return mmap_add_dynamic_region(STM32MP_DDR_BASE, STM32MP_DDR_BASE,
127 STM32MP_DDR_MAX_SIZE,
Yann Gautierc1ad41f2020-09-04 15:55:53 +0200128 MT_NON_CACHEABLE | MT_RW | MT_SECURE);
Yann Gautier84686ba2020-01-10 18:18:59 +0100129}
130
131int stm32mp_unmap_ddr(void)
132{
133 return mmap_remove_dynamic_region(STM32MP_DDR_BASE,
134 STM32MP_DDR_MAX_SIZE);
135}
Yann Gautier3d201782021-03-08 15:03:35 +0100136
Yann Gautier53612f72021-10-18 15:26:33 +0200137static void reset_uart(uint32_t reset)
138{
139 int ret;
140
141 ret = stm32mp_reset_assert(reset, RESET_TIMEOUT_US_1MS);
142 if (ret != 0) {
143 panic();
144 }
145
146 udelay(2);
147
148 ret = stm32mp_reset_deassert(reset, RESET_TIMEOUT_US_1MS);
149 if (ret != 0) {
150 panic();
151 }
152
153 mdelay(1);
154}
155
156int stm32mp_uart_console_setup(void)
157{
158 struct dt_node_info dt_uart_info;
159 unsigned int console_flags;
160 uint32_t clk_rate;
161 int result;
162
163 result = dt_get_stdout_uart_info(&dt_uart_info);
164
165 if ((result <= 0) ||
166 (dt_uart_info.status == DT_DISABLED) ||
167 (dt_uart_info.clock < 0) ||
168 (dt_uart_info.reset < 0)) {
169 return -ENODEV;
170 }
171
172 if (dt_set_stdout_pinctrl() != 0) {
173 return -ENODEV;
174 }
175
176 stm32mp_clk_enable((unsigned long)dt_uart_info.clock);
177
178 reset_uart((uint32_t)dt_uart_info.reset);
179
180 clk_rate = stm32mp_clk_get_rate((unsigned long)dt_uart_info.clock);
181
182 if (console_stm32_register(dt_uart_info.base, clk_rate,
183 STM32MP_UART_BAUDRATE, &console) == 0) {
184 panic();
185 }
186
187 console_flags = CONSOLE_FLAG_BOOT | CONSOLE_FLAG_CRASH |
188 CONSOLE_FLAG_TRANSLATE_CRLF;
189 console_set_scope(&console, console_flags);
190
191 return 0;
192}
193
Yann Gautier3d201782021-03-08 15:03:35 +0100194/*****************************************************************************
195 * plat_is_smccc_feature_available() - This function checks whether SMCCC
196 * feature is availabile for platform.
197 * @fid: SMCCC function id
198 *
199 * Return SMC_ARCH_CALL_SUCCESS if SMCCC feature is available and
200 * SMC_ARCH_CALL_NOT_SUPPORTED otherwise.
201 *****************************************************************************/
202int32_t plat_is_smccc_feature_available(u_register_t fid)
203{
204 switch (fid) {
205 case SMCCC_ARCH_SOC_ID:
206 return SMC_ARCH_CALL_SUCCESS;
207 default:
208 return SMC_ARCH_CALL_NOT_SUPPORTED;
209 }
210}
211
212/* Get SOC version */
213int32_t plat_get_soc_version(void)
214{
215 uint32_t chip_id = stm32mp_get_chip_dev_id();
216 uint32_t manfid = SOC_ID_SET_JEP_106(JEDEC_ST_BKID, JEDEC_ST_MFID);
217
218 return (int32_t)(manfid | (chip_id & SOC_ID_IMPL_DEF_MASK));
219}
220
221/* Get SOC revision */
222int32_t plat_get_soc_revision(void)
223{
224 return (int32_t)(stm32mp_get_chip_version() & SOC_ID_REV_MASK);
225}