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