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