blob: 73bfd123321fa2c6b4667349b0537b9b66f38dbe [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Simon Glassdbc6e142016-03-16 07:44:37 -06002/*
3 * Copyright (c) 2016 Google, Inc
Simon Glassdbc6e142016-03-16 07:44:37 -06004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glass6980b6b2019-11-14 12:57:45 -07008#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassdbc6e142016-03-16 07:44:37 -060010#include <syscon.h>
11#include <asm/cpu.h>
12#include <asm/gpio.h>
13#include <asm/intel_regs.h>
14#include <asm/mrc_common.h>
15#include <asm/pch_common.h>
16#include <asm/post.h>
17#include <asm/arch/me.h>
18#include <asm/report_platform.h>
19
20static const char *const ecc_decoder[] = {
21 "inactive",
22 "active on IO",
23 "disabled on IO",
24 "active"
25};
26
27ulong mrc_common_board_get_usable_ram_top(ulong total_size)
28{
29 struct memory_info *info = &gd->arch.meminfo;
30 uintptr_t dest_addr = 0;
31 struct memory_area *largest = NULL;
32 int i;
33
34 /* Find largest area of memory below 4GB */
35
36 for (i = 0; i < info->num_areas; i++) {
37 struct memory_area *area = &info->area[i];
38
39 if (area->start >= 1ULL << 32)
40 continue;
41 if (!largest || area->size > largest->size)
42 largest = area;
43 }
44
45 /* If no suitable area was found, return an error. */
46 assert(largest);
47 if (!largest || largest->size < (2 << 20))
48 panic("No available memory found for relocation");
49
50 dest_addr = largest->start + largest->size;
51
52 return (ulong)dest_addr;
53}
54
55void mrc_common_dram_init_banksize(void)
56{
57 struct memory_info *info = &gd->arch.meminfo;
58 int num_banks;
59 int i;
60
61 for (i = 0, num_banks = 0; i < info->num_areas; i++) {
62 struct memory_area *area = &info->area[i];
63
64 if (area->start >= 1ULL << 32)
65 continue;
66 gd->bd->bi_dram[num_banks].start = area->start;
67 gd->bd->bi_dram[num_banks].size = area->size;
68 num_banks++;
69 }
70}
71
72int mrc_add_memory_area(struct memory_info *info, uint64_t start,
73 uint64_t end)
74{
75 struct memory_area *ptr;
76
77 if (info->num_areas == CONFIG_NR_DRAM_BANKS)
78 return -ENOSPC;
79
80 ptr = &info->area[info->num_areas];
81 ptr->start = start;
82 ptr->size = end - start;
83 info->total_memory += ptr->size;
84 if (ptr->start < (1ULL << 32))
85 info->total_32bit_memory += ptr->size;
86 debug("%d: memory %llx size %llx, total now %llx / %llx\n",
87 info->num_areas, ptr->start, ptr->size,
88 info->total_32bit_memory, info->total_memory);
89 info->num_areas++;
90
91 return 0;
92}
93
94/*
95 * Dump in the log memory controller configuration as read from the memory
96 * controller registers.
97 */
98void report_memory_config(void)
99{
100 u32 addr_decoder_common, addr_decode_ch[2];
101 int i;
102
103 addr_decoder_common = readl(MCHBAR_REG(0x5000));
104 addr_decode_ch[0] = readl(MCHBAR_REG(0x5004));
105 addr_decode_ch[1] = readl(MCHBAR_REG(0x5008));
106
107 debug("memcfg DDR3 clock %d MHz\n",
108 (readl(MCHBAR_REG(0x5e04)) * 13333 * 2 + 50) / 100);
109 debug("memcfg channel assignment: A: %d, B % d, C % d\n",
110 addr_decoder_common & 3,
111 (addr_decoder_common >> 2) & 3,
112 (addr_decoder_common >> 4) & 3);
113
114 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
115 u32 ch_conf = addr_decode_ch[i];
116 debug("memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
117 debug(" ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
118 debug(" enhanced interleave mode %s\n",
119 ((ch_conf >> 22) & 1) ? "on" : "off");
120 debug(" rank interleave %s\n",
121 ((ch_conf >> 21) & 1) ? "on" : "off");
122 debug(" DIMMA %d MB width x%d %s rank%s\n",
123 ((ch_conf >> 0) & 0xff) * 256,
124 ((ch_conf >> 19) & 1) ? 16 : 8,
125 ((ch_conf >> 17) & 1) ? "dual" : "single",
126 ((ch_conf >> 16) & 1) ? "" : ", selected");
127 debug(" DIMMB %d MB width x%d %s rank%s\n",
128 ((ch_conf >> 8) & 0xff) * 256,
129 ((ch_conf >> 20) & 1) ? 16 : 8,
130 ((ch_conf >> 18) & 1) ? "dual" : "single",
131 ((ch_conf >> 16) & 1) ? ", selected" : "");
132 }
133}
134
135int mrc_locate_spd(struct udevice *dev, int size, const void **spd_datap)
136{
137 const void *blob = gd->fdt_blob;
138 int spd_index;
139 struct gpio_desc desc[4];
140 int spd_node;
141 int node;
142 int ret;
143
144 ret = gpio_request_list_by_name(dev, "board-id-gpios", desc,
145 ARRAY_SIZE(desc), GPIOD_IS_IN);
146 if (ret < 0) {
147 debug("%s: gpio ret=%d\n", __func__, ret);
148 return ret;
149 }
150 spd_index = dm_gpio_get_values_as_int(desc, ret);
151 debug("spd index %d\n", spd_index);
152
Simon Glassdd79d6e2017-01-17 16:52:55 -0700153 node = fdt_first_subnode(blob, dev_of_offset(dev));
Simon Glassdbc6e142016-03-16 07:44:37 -0600154 if (node < 0)
155 return -EINVAL;
156 for (spd_node = fdt_first_subnode(blob, node);
157 spd_node > 0;
158 spd_node = fdt_next_subnode(blob, spd_node)) {
159 int len;
160
161 if (fdtdec_get_int(blob, spd_node, "reg", -1) != spd_index)
162 continue;
163 *spd_datap = fdt_getprop(blob, spd_node, "data", &len);
164 if (len < size) {
165 printf("Missing SPD data\n");
166 return -EINVAL;
167 }
168
169 debug("Using SDRAM SPD data for '%s'\n",
170 fdt_get_name(blob, spd_node, NULL));
171 return 0;
172 }
173
174 printf("No SPD data found for index %d\n", spd_index);
175 return -ENOENT;
176}
177
178asmlinkage void sdram_console_tx_byte(unsigned char byte)
179{
180#ifdef DEBUG
181 putc(byte);
182#endif
183}
184
185/**
186 * Find the PEI executable in the ROM and execute it.
187 *
188 * @me_dev: Management Engine device
189 * @pei_data: configuration data for UEFI PEI reference code
190 */
191static int sdram_initialise(struct udevice *dev, struct udevice *me_dev,
192 void *pei_data, bool use_asm_linkage)
193{
194 unsigned version;
195 const char *data;
196
197 report_platform_info(dev);
198 debug("Starting UEFI PEI System Agent\n");
199
200 debug("PEI data at %p:\n", pei_data);
201
202 data = (char *)CONFIG_X86_MRC_ADDR;
203 if (data) {
204 int rv;
205 ulong start;
206
207 debug("Calling MRC at %p\n", data);
208 post_code(POST_PRE_MRC);
209 start = get_timer(0);
210 if (use_asm_linkage) {
211 asmlinkage int (*func)(void *);
212
213 func = (asmlinkage int (*)(void *))data;
214 rv = func(pei_data);
215 } else {
216 int (*func)(void *);
217
218 func = (int (*)(void *))data;
219 rv = func(pei_data);
220 }
221 post_code(POST_MRC);
222 if (rv) {
223 switch (rv) {
224 case -1:
225 printf("PEI version mismatch.\n");
226 break;
227 case -2:
228 printf("Invalid memory frequency.\n");
229 break;
230 default:
231 printf("MRC returned %x.\n", rv);
232 }
233 printf("Nonzero MRC return value.\n");
234 return -EFAULT;
235 }
236 debug("MRC execution time %lu ms\n", get_timer(start));
237 } else {
238 printf("UEFI PEI System Agent not found.\n");
239 return -ENOSYS;
240 }
241
242 version = readl(MCHBAR_REG(MCHBAR_PEI_VERSION));
243 debug("System Agent Version %d.%d.%d Build %d\n",
244 version >> 24 , (version >> 16) & 0xff,
245 (version >> 8) & 0xff, version & 0xff);
246
Simon Glassdbc6e142016-03-16 07:44:37 -0600247 return 0;
248}
249
250int mrc_common_init(struct udevice *dev, void *pei_data, bool use_asm_linkage)
251{
252 struct udevice *me_dev;
253 int ret;
254
255 ret = syscon_get_by_driver_data(X86_SYSCON_ME, &me_dev);
256 if (ret)
257 return ret;
258
259 ret = sdram_initialise(dev, me_dev, pei_data, use_asm_linkage);
260 if (ret)
261 return ret;
262 quick_ram_check();
263 post_code(POST_DRAM);
264 report_memory_config();
265
266 return 0;
267}