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