1ef5a5b00SGabe Black /*
2ef5a5b00SGabe Black * Copyright (c) 2011 The Chromium OS Authors.
3ef5a5b00SGabe Black * (C) Copyright 2010,2011
4ef5a5b00SGabe Black * Graeme Russ, <graeme.russ@gmail.com>
5ef5a5b00SGabe Black *
61a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+
7ef5a5b00SGabe Black */
8ef5a5b00SGabe Black
9ef5a5b00SGabe Black #include <common.h>
10f08fa7a2SGabe Black #include <asm/e820.h>
11112a575eSGabe Black #include <asm/arch/sysinfo.h>
12ef5a5b00SGabe Black
13ef5a5b00SGabe Black DECLARE_GLOBAL_DATA_PTR;
14ef5a5b00SGabe Black
install_e820_map(unsigned max_entries,struct e820entry * entries)15f08fa7a2SGabe Black unsigned install_e820_map(unsigned max_entries, struct e820entry *entries)
16f08fa7a2SGabe Black {
1752b77860SBin Meng unsigned num_entries;
18f08fa7a2SGabe Black int i;
19f08fa7a2SGabe Black
2052b77860SBin Meng num_entries = min((unsigned)lib_sysinfo.n_memranges, max_entries);
21f08fa7a2SGabe Black if (num_entries < lib_sysinfo.n_memranges) {
22f08fa7a2SGabe Black printf("Warning: Limiting e820 map to %d entries.\n",
23f08fa7a2SGabe Black num_entries);
24f08fa7a2SGabe Black }
25f08fa7a2SGabe Black for (i = 0; i < num_entries; i++) {
26f08fa7a2SGabe Black struct memrange *memrange = &lib_sysinfo.memrange[i];
27f08fa7a2SGabe Black
28f08fa7a2SGabe Black entries[i].addr = memrange->base;
29f08fa7a2SGabe Black entries[i].size = memrange->size;
3052b77860SBin Meng
3152b77860SBin Meng /*
3252b77860SBin Meng * coreboot has some extensions (type 6 & 16) to the E820 types.
3352b77860SBin Meng * When we detect this, mark it as E820_RESERVED.
3452b77860SBin Meng */
3552b77860SBin Meng if (memrange->type == CB_MEM_VENDOR_RSVD ||
3652b77860SBin Meng memrange->type == CB_MEM_TABLE)
3752b77860SBin Meng entries[i].type = E820_RESERVED;
3852b77860SBin Meng else
39f08fa7a2SGabe Black entries[i].type = memrange->type;
40f08fa7a2SGabe Black }
4152b77860SBin Meng
42f08fa7a2SGabe Black return num_entries;
43f08fa7a2SGabe Black }
44f08fa7a2SGabe Black
45112a575eSGabe Black /*
46112a575eSGabe Black * This function looks for the highest region of memory lower than 4GB which
47112a575eSGabe Black * has enough space for U-Boot where U-Boot is aligned on a page boundary. It
48112a575eSGabe Black * overrides the default implementation found elsewhere which simply picks the
49112a575eSGabe Black * end of ram, wherever that may be. The location of the stack, the relocation
50112a575eSGabe Black * address, and how far U-Boot is moved by relocation are set in the global
51112a575eSGabe Black * data structure.
52112a575eSGabe Black */
board_get_usable_ram_top(ulong total_size)535e98947fSSimon Glass ulong board_get_usable_ram_top(ulong total_size)
54112a575eSGabe Black {
55112a575eSGabe Black uintptr_t dest_addr = 0;
56112a575eSGabe Black int i;
57112a575eSGabe Black
58112a575eSGabe Black for (i = 0; i < lib_sysinfo.n_memranges; i++) {
59112a575eSGabe Black struct memrange *memrange = &lib_sysinfo.memrange[i];
60112a575eSGabe Black /* Force U-Boot to relocate to a page aligned address. */
61112a575eSGabe Black uint64_t start = roundup(memrange->base, 1 << 12);
62112a575eSGabe Black uint64_t end = memrange->base + memrange->size;
63112a575eSGabe Black
64112a575eSGabe Black /* Ignore non-memory regions. */
65112a575eSGabe Black if (memrange->type != CB_MEM_RAM)
66112a575eSGabe Black continue;
67112a575eSGabe Black
68112a575eSGabe Black /* Filter memory over 4GB. */
69112a575eSGabe Black if (end > 0xffffffffULL)
70112a575eSGabe Black end = 0x100000000ULL;
71112a575eSGabe Black /* Skip this region if it's too small. */
72112a575eSGabe Black if (end - start < total_size)
73112a575eSGabe Black continue;
74112a575eSGabe Black
75112a575eSGabe Black /* Use this address if it's the largest so far. */
765e98947fSSimon Glass if (end > dest_addr)
77112a575eSGabe Black dest_addr = end;
78112a575eSGabe Black }
79112a575eSGabe Black
80112a575eSGabe Black /* If no suitable area was found, return an error. */
81112a575eSGabe Black if (!dest_addr)
825e98947fSSimon Glass panic("No available memory found for relocation");
83112a575eSGabe Black
845e98947fSSimon Glass return (ulong)dest_addr;
85112a575eSGabe Black }
86112a575eSGabe Black
dram_init(void)8707387d17SSimon Glass int dram_init(void)
88ef5a5b00SGabe Black {
89f08fa7a2SGabe Black int i;
90f08fa7a2SGabe Black phys_size_t ram_size = 0;
91f08fa7a2SGabe Black
92f08fa7a2SGabe Black for (i = 0; i < lib_sysinfo.n_memranges; i++) {
93f08fa7a2SGabe Black struct memrange *memrange = &lib_sysinfo.memrange[i];
94f08fa7a2SGabe Black unsigned long long end = memrange->base + memrange->size;
95f08fa7a2SGabe Black
96a25bc78eSBin Meng if (memrange->type == CB_MEM_RAM && end > ram_size)
97a25bc78eSBin Meng ram_size += memrange->size;
98f08fa7a2SGabe Black }
99a25bc78eSBin Meng
100f08fa7a2SGabe Black gd->ram_size = ram_size;
101f08fa7a2SGabe Black if (ram_size == 0)
102f08fa7a2SGabe Black return -1;
10307387d17SSimon Glass
104c17ca6b5SBin Meng return 0;
105ef5a5b00SGabe Black }
106ef5a5b00SGabe Black
dram_init_banksize(void)107*76b00acaSSimon Glass int dram_init_banksize(void)
108ef5a5b00SGabe Black {
1099a7da182SGabe Black int i, j;
1109a7da182SGabe Black
1119a7da182SGabe Black if (CONFIG_NR_DRAM_BANKS) {
1129a7da182SGabe Black for (i = 0, j = 0; i < lib_sysinfo.n_memranges; i++) {
1139a7da182SGabe Black struct memrange *memrange = &lib_sysinfo.memrange[i];
1149a7da182SGabe Black
115a25bc78eSBin Meng if (memrange->type == CB_MEM_RAM) {
1169a7da182SGabe Black gd->bd->bi_dram[j].start = memrange->base;
1179a7da182SGabe Black gd->bd->bi_dram[j].size = memrange->size;
1189a7da182SGabe Black j++;
1199a7da182SGabe Black if (j >= CONFIG_NR_DRAM_BANKS)
1209a7da182SGabe Black break;
1219a7da182SGabe Black }
1229a7da182SGabe Black }
1239a7da182SGabe Black }
124*76b00acaSSimon Glass
125*76b00acaSSimon Glass return 0;
1268b42dfc3SSimon Glass }
127