Tom Rini | 4549e78 | 2018-05-06 18:27:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | eb653ac | 2020-11-06 19:01:29 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY LOGC_ARCH |
| 7 | |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 10 | #include <image.h> |
| 11 | #include <init.h> |
Patrick Delaunay | 4a1b975 | 2020-03-18 09:22:48 +0100 | [diff] [blame] | 12 | #include <lmb.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 14 | #include <ram.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Patrick Delaunay | 1419e5b | 2021-02-05 13:53:32 +0100 | [diff] [blame] | 16 | #include <asm/system.h> |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
| 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) { |
Patrick Delaunay | eb653ac | 2020-11-06 19:01:29 +0100 | [diff] [blame] | 28 | log_debug("RAM init failed: %d\n", ret); |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 29 | return ret; |
| 30 | } |
| 31 | ret = ram_get_info(dev, &ram); |
| 32 | if (ret) { |
Patrick Delaunay | eb653ac | 2020-11-06 19:01:29 +0100 | [diff] [blame] | 33 | log_debug("Cannot get RAM size: %d\n", ret); |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 34 | return ret; |
| 35 | } |
Patrick Delaunay | eb653ac | 2020-11-06 19:01:29 +0100 | [diff] [blame] | 36 | log_debug("RAM init base=%lx, size=%x\n", ram.base, ram.size); |
Patrick Delaunay | 2514c2d | 2018-03-12 10:46:10 +0100 | [diff] [blame] | 37 | |
| 38 | gd->ram_size = ram.size; |
| 39 | |
| 40 | return 0; |
| 41 | } |
Patrick Delaunay | 4a1b975 | 2020-03-18 09:22:48 +0100 | [diff] [blame] | 42 | |
| 43 | ulong board_get_usable_ram_top(ulong total_size) |
| 44 | { |
Patrick Delaunay | 1419e5b | 2021-02-05 13:53:32 +0100 | [diff] [blame] | 45 | phys_size_t size; |
Patrick Delaunay | 4a1b975 | 2020-03-18 09:22:48 +0100 | [diff] [blame] | 46 | phys_addr_t reg; |
| 47 | struct lmb lmb; |
| 48 | |
Patrick Delaunay | 92b611e | 2021-07-26 11:55:27 +0200 | [diff] [blame] | 49 | if (!total_size) |
Patrice Chotard | c8510e3 | 2021-09-01 09:56:02 +0200 | [diff] [blame] | 50 | return gd->ram_top; |
Patrick Delaunay | 92b611e | 2021-07-26 11:55:27 +0200 | [diff] [blame] | 51 | |
Patrick Delaunay | 4a1b975 | 2020-03-18 09:22:48 +0100 | [diff] [blame] | 52 | /* found enough not-reserved memory to relocated U-Boot */ |
| 53 | lmb_init(&lmb); |
| 54 | lmb_add(&lmb, gd->ram_base, gd->ram_size); |
| 55 | boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob); |
Patrick Delaunay | 7dc6068 | 2021-05-07 14:50:34 +0200 | [diff] [blame] | 56 | /* add 8M for reserved memory for display, fdt, gd,... */ |
| 57 | size = ALIGN(SZ_8M + CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE), |
Patrick Delaunay | 1419e5b | 2021-02-05 13:53:32 +0100 | [diff] [blame] | 58 | reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE); |
Patrick Delaunay | 4a1b975 | 2020-03-18 09:22:48 +0100 | [diff] [blame] | 59 | |
Patrick Delaunay | 1419e5b | 2021-02-05 13:53:32 +0100 | [diff] [blame] | 60 | if (!reg) |
| 61 | reg = gd->ram_top - size; |
Patrick Delaunay | 4a1b975 | 2020-03-18 09:22:48 +0100 | [diff] [blame] | 62 | |
Patrick Delaunay | 7dc6068 | 2021-05-07 14:50:34 +0200 | [diff] [blame] | 63 | /* before relocation, mark the U-Boot memory as cacheable by default */ |
| 64 | if (!(gd->flags & GD_FLG_RELOC)) |
| 65 | mmu_set_region_dcache_behaviour(reg, size, DCACHE_DEFAULT_OPTION); |
Patrick Delaunay | 1419e5b | 2021-02-05 13:53:32 +0100 | [diff] [blame] | 66 | |
| 67 | return reg + size; |
Patrick Delaunay | 4a1b975 | 2020-03-18 09:22:48 +0100 | [diff] [blame] | 68 | } |