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 <init.h>
9 #include <asm/e820.h>
10 #include <asm/cb_sysinfo.h>
11 #include <asm/global_data.h>
12 
13 DECLARE_GLOBAL_DATA_PTR;
14 
install_e820_map(unsigned int max_entries,struct e820_entry * entries)15 unsigned int install_e820_map(unsigned int max_entries,
16 			      struct e820_entry *entries)
17 {
18 	return cb_install_e820_map(max_entries, entries);
19 }
20 
21 /*
22  * This function looks for the highest region of memory lower than 4GB which
23  * has enough space for U-Boot where U-Boot is aligned on a page boundary. It
24  * overrides the default implementation found elsewhere which simply picks the
25  * end of ram, wherever that may be. The location of the stack, the relocation
26  * address, and how far U-Boot is moved by relocation are set in the global
27  * data structure.
28  */
board_get_usable_ram_top(phys_size_t total_size)29 phys_addr_t board_get_usable_ram_top(phys_size_t total_size)
30 {
31 	uintptr_t dest_addr = 0;
32 	int i;
33 
34 	for (i = 0; i < lib_sysinfo.n_memranges; i++) {
35 		struct memrange *memrange = &lib_sysinfo.memrange[i];
36 		/* Force U-Boot to relocate to a page aligned address. */
37 		uint64_t start = roundup(memrange->base, 1 << 12);
38 		uint64_t end = memrange->base + memrange->size;
39 
40 		/* Ignore non-memory regions. */
41 		if (memrange->type != CB_MEM_RAM)
42 			continue;
43 
44 		/* Filter memory over 4GB. */
45 		if (end > 0xffffffffULL)
46 			end = 0x100000000ULL;
47 		/* Skip this region if it's too small. */
48 		if (end - start < total_size)
49 			continue;
50 
51 		/* Use this address if it's the largest so far. */
52 		if (end > dest_addr)
53 			dest_addr = end;
54 	}
55 
56 	/* If no suitable area was found, return an error. */
57 	if (!dest_addr)
58 		panic("No available memory found for relocation");
59 
60 	return (ulong)dest_addr;
61 }
62 
dram_init(void)63 int dram_init(void)
64 {
65 	int i;
66 	phys_size_t ram_size = 0;
67 
68 	for (i = 0; i < lib_sysinfo.n_memranges; i++) {
69 		struct memrange *memrange = &lib_sysinfo.memrange[i];
70 		unsigned long long end = memrange->base + memrange->size;
71 
72 		if (memrange->type == CB_MEM_RAM && end > ram_size)
73 			ram_size += memrange->size;
74 	}
75 
76 	gd->ram_size = ram_size;
77 	if (ram_size == 0)
78 		return -1;
79 
80 	return 0;
81 }
82 
dram_init_banksize(void)83 int dram_init_banksize(void)
84 {
85 	int i, j;
86 
87 	if (CONFIG_NR_DRAM_BANKS) {
88 		for (i = 0, j = 0; i < lib_sysinfo.n_memranges; i++) {
89 			struct memrange *memrange = &lib_sysinfo.memrange[i];
90 
91 			if (memrange->type == CB_MEM_RAM) {
92 				gd->bd->bi_dram[j].start = memrange->base;
93 				gd->bd->bi_dram[j].size = memrange->size;
94 				j++;
95 				if (j >= CONFIG_NR_DRAM_BANKS)
96 					break;
97 			}
98 		}
99 	}
100 
101 	return 0;
102 }
103