blob: ad6977fd4454dc4d89071d81e8f6d4b1d523115d [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 Delaunay85b53972018-03-12 10:46:10 +010016
17DECLARE_GLOBAL_DATA_PTR;
18
19int dram_init(void)
20{
21 struct ram_info ram;
22 struct udevice *dev;
23 int ret;
24
25 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
26 if (ret) {
Patrick Delaunayba779402020-11-06 19:01:29 +010027 log_debug("RAM init failed: %d\n", ret);
Patrick Delaunay85b53972018-03-12 10:46:10 +010028 return ret;
29 }
30 ret = ram_get_info(dev, &ram);
31 if (ret) {
Patrick Delaunayba779402020-11-06 19:01:29 +010032 log_debug("Cannot get RAM size: %d\n", ret);
Patrick Delaunay85b53972018-03-12 10:46:10 +010033 return ret;
34 }
Patrick Delaunayba779402020-11-06 19:01:29 +010035 log_debug("RAM init base=%lx, size=%x\n", ram.base, ram.size);
Patrick Delaunay85b53972018-03-12 10:46:10 +010036
37 gd->ram_size = ram.size;
38
39 return 0;
40}
Patrick Delaunayd1633b32020-03-18 09:22:48 +010041
42ulong board_get_usable_ram_top(ulong total_size)
43{
44 phys_addr_t reg;
45 struct lmb lmb;
46
47 /* found enough not-reserved memory to relocated U-Boot */
48 lmb_init(&lmb);
49 lmb_add(&lmb, gd->ram_base, gd->ram_size);
50 boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob);
51 reg = lmb_alloc(&lmb, CONFIG_SYS_MALLOC_LEN + total_size, SZ_4K);
52
53 if (reg)
54 return ALIGN(reg + CONFIG_SYS_MALLOC_LEN + total_size, SZ_4K);
55
56 return gd->ram_top;
57}