blob: cb35ed60ca193fa11f1f9cddd7a96f2eff89eec9 [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
Patrick Delaunayd1633b32020-03-18 09:22:48 +010055 /* found enough not-reserved memory to relocated U-Boot */
56 lmb_init(&lmb);
Marek Vasut405ca2e2023-01-05 02:22:22 +010057 lmb_add(&lmb, gd->ram_base, get_effective_memsize());
Patrick Delaunayd1633b32020-03-18 09:22:48 +010058 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
Patrick Delaunayf9203e92021-05-07 14:50:34 +020059 /* add 8M for reserved memory for display, fdt, gd,... */
60 size = ALIGN(SZ_8M + CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE),
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010061 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
Patrick Delaunayd1633b32020-03-18 09:22:48 +010062
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010063 if (!reg)
64 reg = gd->ram_top - size;
65
Patrick Delaunayf9203e92021-05-07 14:50:34 +020066 /* before relocation, mark the U-Boot memory as cacheable by default */
67 if (!(gd->flags & GD_FLG_RELOC))
68 mmu_set_region_dcache_behaviour(reg, size, DCACHE_DEFAULT_OPTION);
Patrick Delaunayd1633b32020-03-18 09:22:48 +010069
Patrick Delaunayc9b0dc32021-02-05 13:53:32 +010070 return reg + size;
Patrick Delaunayd1633b32020-03-18 09:22:48 +010071}