blob: 9c4fafbf478ba58a08ec93d58719cf1dc579a61c [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01004 */
5
Patrick Delaunayeb653ac2020-11-06 19:01:29 +01006#define LOG_CATEGORY LOGC_ARCH
7
Tom Rini03de3052024-05-20 13:35:03 -06008#include <config.h>
Patrick Delaunaydc7e5f12020-04-30 16:30:21 +02009#include <cpu_func.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010010#include <dm.h>
Simon Glassdb41d652019-12-28 10:45:07 -070011#include <hang.h>
Simon Glass691d7192020-05-10 11:40:02 -060012#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Alexandru Gagniuc85332632021-07-15 14:19:26 -050014#include <ram.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010015#include <spl.h>
Simon Glass90526e92020-05-10 11:39:56 -060016#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Patrick Delaunay11dfd1a2018-03-20 10:54:54 +010018#include <asm/io.h>
Patrick Delaunay006ea182019-02-27 17:01:14 +010019#include <asm/arch/sys_proto.h>
Alexandru Gagniuc85332632021-07-15 14:19:26 -050020#include <mach/tzc.h>
Sughosh Ganu5fe9e0d2024-08-26 17:29:38 +053021#include <mach/stm32mp.h>
Patrick Delaunay006ea182019-02-27 17:01:14 +010022#include <linux/libfdt.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010023
24u32 spl_boot_device(void)
25{
Patrick Delaunay11dfd1a2018-03-20 10:54:54 +010026 u32 boot_mode;
27
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +010028 boot_mode = get_bootmode();
Patrick Delaunay11dfd1a2018-03-20 10:54:54 +010029
30 switch (boot_mode) {
31 case BOOT_FLASH_SD_1:
32 case BOOT_FLASH_EMMC_1:
33 return BOOT_DEVICE_MMC1;
34 case BOOT_FLASH_SD_2:
35 case BOOT_FLASH_EMMC_2:
36 return BOOT_DEVICE_MMC2;
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +010037 case BOOT_SERIAL_UART_1:
38 case BOOT_SERIAL_UART_2:
39 case BOOT_SERIAL_UART_3:
40 case BOOT_SERIAL_UART_4:
41 case BOOT_SERIAL_UART_5:
42 case BOOT_SERIAL_UART_6:
43 case BOOT_SERIAL_UART_7:
44 case BOOT_SERIAL_UART_8:
45 return BOOT_DEVICE_UART;
46 case BOOT_SERIAL_USB_OTG:
Marek Vasut757c8382021-12-06 21:58:08 +010047 return BOOT_DEVICE_DFU;
Patrick Delaunay7f63c1e2019-02-27 17:01:12 +010048 case BOOT_FLASH_NAND_FMC:
49 return BOOT_DEVICE_NAND;
50 case BOOT_FLASH_NOR_QSPI:
51 return BOOT_DEVICE_SPI;
Patrick Delaunayb664a742020-03-18 09:22:52 +010052 case BOOT_FLASH_SPINAND_1:
53 return BOOT_DEVICE_NONE; /* SPINAND not supported in SPL */
Patrick Delaunay11dfd1a2018-03-20 10:54:54 +010054 }
55
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010056 return BOOT_DEVICE_MMC1;
57}
58
Andre Przywara59073572021-07-12 11:06:49 +010059u32 spl_mmc_boot_mode(struct mmc *mmc, const u32 boot_device)
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010060{
61 return MMCSD_MODE_RAW;
62}
63
Richard Genoud40426d62020-10-12 16:11:09 +020064#ifdef CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_USE_PARTITION
Harald Seilerc51b7512020-04-15 11:33:31 +020065int spl_mmc_boot_partition(const u32 boot_device)
Patrick Delaunay11dfd1a2018-03-20 10:54:54 +010066{
67 switch (boot_device) {
68 case BOOT_DEVICE_MMC1:
69 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION;
70 case BOOT_DEVICE_MMC2:
71 return CONFIG_SYS_MMCSD_RAW_MODE_U_BOOT_PARTITION_MMC2;
72 default:
73 return -EINVAL;
74 }
75}
Richard Genoud40426d62020-10-12 16:11:09 +020076#endif
Patrick Delaunay11dfd1a2018-03-20 10:54:54 +010077
Patrick Delaunay006ea182019-02-27 17:01:14 +010078#ifdef CONFIG_SPL_DISPLAY_PRINT
79void spl_display_print(void)
80{
81 DECLARE_GLOBAL_DATA_PTR;
82 const char *model;
83
84 /* same code than show_board_info() but not compiled for SPL
85 * see CONFIG_DISPLAY_BOARDINFO & common/board_info.c
86 */
87 model = fdt_getprop(gd->fdt_blob, 0, "model", NULL);
88 if (model)
Patrick Delaunayeb653ac2020-11-06 19:01:29 +010089 log_info("Model: %s\n", model);
Patrick Delaunay006ea182019-02-27 17:01:14 +010090}
91#endif
92
Marek Vasut65e38e82020-04-22 13:18:10 +020093__weak int board_early_init_f(void)
94{
95 return 0;
96}
97
Alexandru Gagniuc85332632021-07-15 14:19:26 -050098uint32_t stm32mp_get_dram_size(void)
99{
100 struct ram_info ram;
101 struct udevice *dev;
102 int ret;
103
104 if (uclass_get_device(UCLASS_RAM, 0, &dev))
105 return 0;
106
107 ret = ram_get_info(dev, &ram);
108 if (ret)
109 return 0;
110
111 return ram.size;
112}
113
Alexandru Gagniuc85332632021-07-15 14:19:26 -0500114#define CFG_SHMEM_SIZE 0x200000
115#define STM32_TZC_NSID_ALL 0xffff
116#define STM32_TZC_FILTER_ALL 3
117
118void stm32_init_tzc_for_optee(void)
119{
120 const uint32_t dram_size = stm32mp_get_dram_size();
121 const uintptr_t dram_top = STM32_DDR_BASE + (dram_size - 1);
Patrice Chotard9c9eeb62024-06-11 11:52:38 +0200122 u32 optee_base = 0, optee_size = 0, tee_shmem_base;
Alexandru Gagniuc85332632021-07-15 14:19:26 -0500123 const uintptr_t tzc = STM32_TZC_BASE;
124 int ret;
125
126 if (dram_size == 0)
127 panic("Cannot determine DRAM size from devicetree\n");
128
129 ret = optee_get_reserved_memory(&optee_base, &optee_size);
130 if (ret < 0 || optee_size <= CFG_SHMEM_SIZE)
131 panic("Invalid OPTEE reserved memory in devicetree\n");
132
133 tee_shmem_base = optee_base + optee_size - CFG_SHMEM_SIZE;
134
135 const struct tzc_region optee_config[] = {
136 {
137 .base = STM32_DDR_BASE,
138 .top = optee_base - 1,
139 .sec_mode = TZC_ATTR_SEC_NONE,
140 .nsec_id = STM32_TZC_NSID_ALL,
141 .filters_mask = STM32_TZC_FILTER_ALL,
142 }, {
143 .base = optee_base,
144 .top = tee_shmem_base - 1,
145 .sec_mode = TZC_ATTR_SEC_RW,
146 .nsec_id = 0,
147 .filters_mask = STM32_TZC_FILTER_ALL,
148 }, {
149 .base = tee_shmem_base,
150 .top = dram_top,
151 .sec_mode = TZC_ATTR_SEC_NONE,
152 .nsec_id = STM32_TZC_NSID_ALL,
153 .filters_mask = STM32_TZC_FILTER_ALL,
154 }, {
155 .top = 0,
156 }
157 };
158
159 flush_dcache_all();
160
161 tzc_configure(tzc, optee_config);
162 tzc_dump_config(tzc);
163
164 dcache_disable();
165}
166
167void spl_board_prepare_for_optee(void *fdt)
168{
169 stm32_init_tzc_for_optee();
170}
171
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100172void board_init_f(ulong dummy)
173{
174 struct udevice *dev;
175 int ret;
176
177 arch_cpu_init();
Patrick Delaunay6df271a2022-05-20 18:24:42 +0200178 mach_cpu_init();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100179
180 ret = spl_early_init();
181 if (ret) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100182 log_debug("spl_early_init() failed: %d\n", ret);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100183 hang();
184 }
185
186 ret = uclass_get_device(UCLASS_CLK, 0, &dev);
187 if (ret) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100188 log_debug("Clock init failed: %d\n", ret);
Patrick Delaunayeaec1f92020-04-22 14:29:10 +0200189 hang();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100190 }
191
192 ret = uclass_get_device(UCLASS_RESET, 0, &dev);
193 if (ret) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100194 log_debug("Reset init failed: %d\n", ret);
Patrick Delaunayeaec1f92020-04-22 14:29:10 +0200195 hang();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100196 }
197
198 ret = uclass_get_device(UCLASS_PINCTRL, 0, &dev);
199 if (ret) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100200 log_debug("%s: Cannot find pinctrl device\n", __func__);
Patrick Delaunayeaec1f92020-04-22 14:29:10 +0200201 hang();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100202 }
203
204 /* enable console uart printing */
205 preloader_console_init();
206
Marek Vasut65e38e82020-04-22 13:18:10 +0200207 ret = board_early_init_f();
208 if (ret) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100209 log_debug("board_early_init_f() failed: %d\n", ret);
Marek Vasut65e38e82020-04-22 13:18:10 +0200210 hang();
211 }
212
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100213 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
214 if (ret) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +0100215 log_err("DRAM init failed: %d\n", ret);
Patrick Delaunay105a5ad2019-02-27 17:01:17 +0100216 hang();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100217 }
Patrick Delaunaydc7e5f12020-04-30 16:30:21 +0200218
219 /*
220 * activate cache on DDR only when DDR is fully initialized
221 * to avoid speculative access and issue in get_ram_size()
222 */
223 if (!CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Patrick Delaunay67f9f112020-09-04 12:55:19 +0200224 mmu_set_region_dcache_behaviour(STM32_DDR_BASE,
225 CONFIG_DDR_CACHEABLE_SIZE,
Patrick Delaunaydc7e5f12020-04-30 16:30:21 +0200226 DCACHE_DEFAULT_OPTION);
227}
228
229void spl_board_prepare_for_boot(void)
230{
231 dcache_disable();
232}
233
Patrick Delaunay346034a2020-07-07 14:21:53 +0200234void spl_board_prepare_for_linux(void)
Patrick Delaunaydc7e5f12020-04-30 16:30:21 +0200235{
236 dcache_disable();
Patrick Delaunay2514c2d2018-03-12 10:46:10 +0100237}