blob: 198785353f13d968f8a8b92ed84ef9b472fea073 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay85b53972018-03-12 10:46:10 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay85b53972018-03-12 10:46:10 +01004 */
5
Patrick Delaunayba779402020-11-06 19:01:29 +01006#define LOG_CATEGORY LOGC_ARCH
7
Patrick Delaunay85b53972018-03-12 10:46:10 +01008#include <dm.h>
Patrice Chotard04534432024-04-22 17:06:45 +02009#include <efi_loader.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060010#include <image.h>
11#include <init.h>
Patrick Delaunayd1633b32020-03-18 09:22:48 +010012#include <lmb.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Patrick Delaunay85b53972018-03-12 10:46:10 +010014#include <ram.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010016#include <asm/system.h>
Sughosh Ganu40886bf2024-08-26 17:29:38 +053017#include <mach/stm32mp.h>
Patrick Delaunay85b53972018-03-12 10:46:10 +010018
19DECLARE_GLOBAL_DATA_PTR;
20
Sughosh Ganu40886bf2024-08-26 17:29:38 +053021int optee_get_reserved_memory(u32 *start, u32 *size)
22{
23 fdt_addr_t fdt_mem_size;
24 fdt_addr_t fdt_start;
25 ofnode node;
26
27 node = ofnode_path("/reserved-memory/optee");
28 if (!ofnode_valid(node))
29 return -ENOENT;
30
31 fdt_start = ofnode_get_addr_size(node, "reg", &fdt_mem_size);
32 *start = fdt_start;
33 *size = fdt_mem_size;
34 return (fdt_start < 0) ? fdt_start : 0;
35}
36
Patrick Delaunay85b53972018-03-12 10:46:10 +010037int dram_init(void)
38{
39 struct ram_info ram;
40 struct udevice *dev;
41 int ret;
42
43 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
Patrice Chotard0f0faea2023-10-27 16:42:57 +020044 /* in case there is no RAM driver, retrieve DDR size from DT */
45 if (ret == -ENODEV) {
46 return fdtdec_setup_mem_size_base();
47 } else if (ret) {
48 log_err("RAM init failed: %d\n", ret);
Patrick Delaunay85b53972018-03-12 10:46:10 +010049 return ret;
50 }
51 ret = ram_get_info(dev, &ram);
52 if (ret) {
Patrick Delaunayba779402020-11-06 19:01:29 +010053 log_debug("Cannot get RAM size: %d\n", ret);
Patrick Delaunay85b53972018-03-12 10:46:10 +010054 return ret;
55 }
Patrick Delaunay4c063772023-10-27 16:42:58 +020056 log_debug("RAM init base=%p, size=%zx\n", (void *)ram.base, ram.size);
Patrick Delaunay85b53972018-03-12 10:46:10 +010057
58 gd->ram_size = ram.size;
59
60 return 0;
61}
Patrick Delaunayd1633b32020-03-18 09:22:48 +010062
Heinrich Schuchardt51a9aac2023-08-12 20:16:58 +020063phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Patrick Delaunayd1633b32020-03-18 09:22:48 +010064{
Sughosh Ganu61d72c12024-08-26 17:29:39 +053065 int ret;
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010066 phys_size_t size;
Patrick Delaunayd1633b32020-03-18 09:22:48 +010067 phys_addr_t reg;
Sughosh Ganu61d72c12024-08-26 17:29:39 +053068 u32 optee_start, optee_size;
Patrick Delaunayd1633b32020-03-18 09:22:48 +010069
Patrick Delaunay9e249d82021-07-26 11:55:27 +020070 if (!total_size)
Patrice Chotardf9339b12021-09-01 09:56:02 +020071 return gd->ram_top;
Patrick Delaunay9e249d82021-07-26 11:55:27 +020072
Patrice Chotard5501e382023-10-27 16:42:59 +020073 /*
74 * make sure U-Boot uses address space below 4GB boundaries even
75 * if the effective available memory is bigger
76 */
77 gd->ram_top = clamp_val(gd->ram_top, 0, SZ_4G - 1);
Sughosh Ganu61d72c12024-08-26 17:29:39 +053078 size = ALIGN(SZ_8M + CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE);
Patrick Delaunayd1633b32020-03-18 09:22:48 +010079
Sughosh Ganu61d72c12024-08-26 17:29:39 +053080 ret = optee_get_reserved_memory(&optee_start, &optee_size);
81 reg = (!ret ? optee_start : gd->ram_top) - size;
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010082
Patrick Delaunayf9203e92021-05-07 14:50:34 +020083 /* before relocation, mark the U-Boot memory as cacheable by default */
84 if (!(gd->flags & GD_FLG_RELOC))
85 mmu_set_region_dcache_behaviour(reg, size, DCACHE_DEFAULT_OPTION);
Patrick Delaunayd1633b32020-03-18 09:22:48 +010086
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010087 return reg + size;
Patrick Delaunayd1633b32020-03-18 09:22:48 +010088}
Patrice Chotard04534432024-04-22 17:06:45 +020089
90void efi_add_known_memory(void)
91{
92 if (IS_ENABLED(CONFIG_EFI_LOADER))
93 /*
94 * Memory over ram_top is reserved to OPTEE.
95 * Declare to EFI only memory area below ram_top
96 */
97 efi_add_memory_map(gd->ram_base, gd->ram_top - gd->ram_base,
98 EFI_CONVENTIONAL_MEMORY);
99}