1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  * (C) Copyright 2010,2011
5  * Graeme Russ, <graeme.russ@gmail.com>
6  */
7 
8 #include <common.h>
9 #include <init.h>
10 #include <asm/e820.h>
11 #include <asm/arch/sysinfo.h>
12 #include <asm/global_data.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
install_e820_map(unsigned int max_entries,struct e820_entry * entries)16 unsigned int install_e820_map(unsigned int max_entries,
17 			      struct e820_entry *entries)
18 {
19 	unsigned int num_entries;
20 	int i;
21 
22 	num_entries = min((unsigned int)lib_sysinfo.n_memranges, max_entries);
23 	if (num_entries < lib_sysinfo.n_memranges) {
24 		printf("Warning: Limiting e820 map to %d entries.\n",
25 			num_entries);
26 	}
27 	for (i = 0; i < num_entries; i++) {
28 		struct memrange *memrange = &lib_sysinfo.memrange[i];
29 
30 		entries[i].addr = memrange->base;
31 		entries[i].size = memrange->size;
32 
33 		/*
34 		 * coreboot has some extensions (type 6 & 16) to the E820 types.
35 		 * When we detect this, mark it as E820_RESERVED.
36 		 */
37 		if (memrange->type == CB_MEM_VENDOR_RSVD ||
38 		    memrange->type == CB_MEM_TABLE)
39 			entries[i].type = E820_RESERVED;
40 		else
41 			entries[i].type = memrange->type;
42 	}
43 
44 	return num_entries;
45 }
46 
47 /*
48  * This function looks for the highest region of memory lower than 4GB which
49  * has enough space for U-Boot where U-Boot is aligned on a page boundary. It
50  * overrides the default implementation found elsewhere which simply picks the
51  * end of ram, wherever that may be. The location of the stack, the relocation
52  * address, and how far U-Boot is moved by relocation are set in the global
53  * data structure.
54  */
board_get_usable_ram_top(ulong total_size)55 ulong board_get_usable_ram_top(ulong total_size)
56 {
57 	uintptr_t dest_addr = 0;
58 	int i;
59 
60 	for (i = 0; i < lib_sysinfo.n_memranges; i++) {
61 		struct memrange *memrange = &lib_sysinfo.memrange[i];
62 		/* Force U-Boot to relocate to a page aligned address. */
63 		uint64_t start = roundup(memrange->base, 1 << 12);
64 		uint64_t end = memrange->base + memrange->size;
65 
66 		/* Ignore non-memory regions. */
67 		if (memrange->type != CB_MEM_RAM)
68 			continue;
69 
70 		/* Filter memory over 4GB. */
71 		if (end > 0xffffffffULL)
72 			end = 0x100000000ULL;
73 		/* Skip this region if it's too small. */
74 		if (end - start < total_size)
75 			continue;
76 
77 		/* Use this address if it's the largest so far. */
78 		if (end > dest_addr)
79 			dest_addr = end;
80 	}
81 
82 	/* If no suitable area was found, return an error. */
83 	if (!dest_addr)
84 		panic("No available memory found for relocation");
85 
86 	return (ulong)dest_addr;
87 }
88 
dram_init(void)89 int dram_init(void)
90 {
91 	int i;
92 	phys_size_t ram_size = 0;
93 
94 	for (i = 0; i < lib_sysinfo.n_memranges; i++) {
95 		struct memrange *memrange = &lib_sysinfo.memrange[i];
96 		unsigned long long end = memrange->base + memrange->size;
97 
98 		if (memrange->type == CB_MEM_RAM && end > ram_size)
99 			ram_size += memrange->size;
100 	}
101 
102 	gd->ram_size = ram_size;
103 	if (ram_size == 0)
104 		return -1;
105 
106 	return 0;
107 }
108 
dram_init_banksize(void)109 int dram_init_banksize(void)
110 {
111 	int i, j;
112 
113 	if (CONFIG_NR_DRAM_BANKS) {
114 		for (i = 0, j = 0; i < lib_sysinfo.n_memranges; i++) {
115 			struct memrange *memrange = &lib_sysinfo.memrange[i];
116 
117 			if (memrange->type == CB_MEM_RAM) {
118 				gd->bd->bi_dram[j].start = memrange->base;
119 				gd->bd->bi_dram[j].size = memrange->size;
120 				j++;
121 				if (j >= CONFIG_NR_DRAM_BANKS)
122 					break;
123 			}
124 		}
125 	}
126 
127 	return 0;
128 }
129