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