1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
2 /*
3  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
4  */
5 
6 #define LOG_CATEGORY LOGC_ARCH
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <image.h>
11 #include <init.h>
12 #include <lmb.h>
13 #include <log.h>
14 #include <ram.h>
15 #include <asm/global_data.h>
16 #include <asm/system.h>
17 
18 DECLARE_GLOBAL_DATA_PTR;
19 
dram_init(void)20 int 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);
27 	if (ret) {
28 		log_debug("RAM init failed: %d\n", ret);
29 		return ret;
30 	}
31 	ret = ram_get_info(dev, &ram);
32 	if (ret) {
33 		log_debug("Cannot get RAM size: %d\n", ret);
34 		return ret;
35 	}
36 	log_debug("RAM init base=%lx, size=%x\n", ram.base, ram.size);
37 
38 	gd->ram_size = ram.size;
39 
40 	return 0;
41 }
42 
board_get_usable_ram_top(ulong total_size)43 ulong board_get_usable_ram_top(ulong total_size)
44 {
45 	phys_size_t size;
46 	phys_addr_t reg;
47 	struct lmb lmb;
48 
49 	/* found enough not-reserved memory to relocated U-Boot */
50 	lmb_init(&lmb);
51 	lmb_add(&lmb, gd->ram_base, gd->ram_size);
52 	boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
53 	size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE),
54 	reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
55 
56 	if (!reg)
57 		reg = gd->ram_top - size;
58 
59 	mmu_set_region_dcache_behaviour(reg, size, DCACHE_DEFAULT_OPTION);
60 
61 	return reg + size;
62 }
63