blob: fb1208fc5d570bb3e4676c308bf67f56c57f626f [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 <common.h>
9#include <dm.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>
Patrick Delaunay85b53972018-03-12 10:46:10 +010017
18DECLARE_GLOBAL_DATA_PTR;
19
20int dram_init(void)
21{
22 struct ram_info ram;
23 struct udevice *dev;
24 int ret;
25
26 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
Patrice Chotard0f0faea2023-10-27 16:42:57 +020027 /* in case there is no RAM driver, retrieve DDR size from DT */
28 if (ret == -ENODEV) {
29 return fdtdec_setup_mem_size_base();
30 } else if (ret) {
31 log_err("RAM init failed: %d\n", ret);
Patrick Delaunay85b53972018-03-12 10:46:10 +010032 return ret;
33 }
34 ret = ram_get_info(dev, &ram);
35 if (ret) {
Patrick Delaunayba779402020-11-06 19:01:29 +010036 log_debug("Cannot get RAM size: %d\n", ret);
Patrick Delaunay85b53972018-03-12 10:46:10 +010037 return ret;
38 }
Patrick Delaunay4c063772023-10-27 16:42:58 +020039 log_debug("RAM init base=%p, size=%zx\n", (void *)ram.base, ram.size);
Patrick Delaunay85b53972018-03-12 10:46:10 +010040
41 gd->ram_size = ram.size;
42
43 return 0;
44}
Patrick Delaunayd1633b32020-03-18 09:22:48 +010045
Heinrich Schuchardt51a9aac2023-08-12 20:16:58 +020046phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
Patrick Delaunayd1633b32020-03-18 09:22:48 +010047{
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010048 phys_size_t size;
Patrick Delaunayd1633b32020-03-18 09:22:48 +010049 phys_addr_t reg;
50 struct lmb lmb;
51
Patrick Delaunay9e249d82021-07-26 11:55:27 +020052 if (!total_size)
Patrice Chotardf9339b12021-09-01 09:56:02 +020053 return gd->ram_top;
Patrick Delaunay9e249d82021-07-26 11:55:27 +020054
Patrice Chotard5501e382023-10-27 16:42:59 +020055 /*
56 * make sure U-Boot uses address space below 4GB boundaries even
57 * if the effective available memory is bigger
58 */
59 gd->ram_top = clamp_val(gd->ram_top, 0, SZ_4G - 1);
60
Patrick Delaunayd1633b32020-03-18 09:22:48 +010061 /* found enough not-reserved memory to relocated U-Boot */
62 lmb_init(&lmb);
Patrice Chotard5501e382023-10-27 16:42:59 +020063 lmb_add(&lmb, gd->ram_base, gd->ram_top - gd->ram_base);
Patrick Delaunayd1633b32020-03-18 09:22:48 +010064 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
Patrick Delaunayf9203e92021-05-07 14:50:34 +020065 /* add 8M for reserved memory for display, fdt, gd,... */
66 size = ALIGN(SZ_8M + CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE),
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010067 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
Patrick Delaunayd1633b32020-03-18 09:22:48 +010068
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010069 if (!reg)
70 reg = gd->ram_top - size;
71
Patrick Delaunayf9203e92021-05-07 14:50:34 +020072 /* before relocation, mark the U-Boot memory as cacheable by default */
73 if (!(gd->flags & GD_FLG_RELOC))
74 mmu_set_region_dcache_behaviour(reg, size, DCACHE_DEFAULT_OPTION);
Patrick Delaunayd1633b32020-03-18 09:22:48 +010075
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010076 return reg + size;
Patrick Delaunayd1633b32020-03-18 09:22:48 +010077}