blob: 3c097029bda0be128112df0cadce4e7753bccf5d [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01004 */
5
Patrick Delaunayeb653ac2020-11-06 19:01:29 +01006#define LOG_CATEGORY LOGC_ARCH
7
Patrick Delaunay2514c2d2018-03-12 10:46:10 +01008#include <common.h>
9#include <dm.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060010#include <image.h>
11#include <init.h>
Patrick Delaunay4a1b9752020-03-18 09:22:48 +010012#include <lmb.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010014#include <ram.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Patrick Delaunay1419e5b2021-02-05 13:53:32 +010016#include <asm/system.h>
Patrick Delaunay2514c2d2018-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);
27 if (ret) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +010028 log_debug("RAM init failed: %d\n", ret);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010029 return ret;
30 }
31 ret = ram_get_info(dev, &ram);
32 if (ret) {
Patrick Delaunayeb653ac2020-11-06 19:01:29 +010033 log_debug("Cannot get RAM size: %d\n", ret);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010034 return ret;
35 }
Patrick Delaunayeb653ac2020-11-06 19:01:29 +010036 log_debug("RAM init base=%lx, size=%x\n", ram.base, ram.size);
Patrick Delaunay2514c2d2018-03-12 10:46:10 +010037
38 gd->ram_size = ram.size;
39
40 return 0;
41}
Patrick Delaunay4a1b9752020-03-18 09:22:48 +010042
43ulong board_get_usable_ram_top(ulong total_size)
44{
Patrick Delaunay1419e5b2021-02-05 13:53:32 +010045 phys_size_t size;
Patrick Delaunay4a1b9752020-03-18 09:22:48 +010046 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);
Patrick Delaunay7dc60682021-05-07 14:50:34 +020053 /* add 8M for reserved memory for display, fdt, gd,... */
54 size = ALIGN(SZ_8M + CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE),
Patrick Delaunay1419e5b2021-02-05 13:53:32 +010055 reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE);
Patrick Delaunay4a1b9752020-03-18 09:22:48 +010056
Patrick Delaunay1419e5b2021-02-05 13:53:32 +010057 if (!reg)
58 reg = gd->ram_top - size;
Patrick Delaunay4a1b9752020-03-18 09:22:48 +010059
Patrick Delaunay7dc60682021-05-07 14:50:34 +020060 /* before relocation, mark the U-Boot memory as cacheable by default */
61 if (!(gd->flags & GD_FLG_RELOC))
62 mmu_set_region_dcache_behaviour(reg, size, DCACHE_DEFAULT_OPTION);
Patrick Delaunay1419e5b2021-02-05 13:53:32 +010063
64 return reg + size;
Patrick Delaunay4a1b9752020-03-18 09:22:48 +010065}