1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Google, Inc
11 #include <asm/intel_regs.h>
12 #include <asm/mrc_common.h>
13 #include <asm/pch_common.h>
15 #include <asm/arch/me.h>
16 #include <asm/report_platform.h>
18 static const char *const ecc_decoder[] = {
25 ulong mrc_common_board_get_usable_ram_top(ulong total_size)
27 struct memory_info *info = &gd->arch.meminfo;
28 uintptr_t dest_addr = 0;
29 struct memory_area *largest = NULL;
32 /* Find largest area of memory below 4GB */
34 for (i = 0; i < info->num_areas; i++) {
35 struct memory_area *area = &info->area[i];
37 if (area->start >= 1ULL << 32)
39 if (!largest || area->size > largest->size)
43 /* If no suitable area was found, return an error. */
45 if (!largest || largest->size < (2 << 20))
46 panic("No available memory found for relocation");
48 dest_addr = largest->start + largest->size;
50 return (ulong)dest_addr;
53 void mrc_common_dram_init_banksize(void)
55 struct memory_info *info = &gd->arch.meminfo;
59 for (i = 0, num_banks = 0; i < info->num_areas; i++) {
60 struct memory_area *area = &info->area[i];
62 if (area->start >= 1ULL << 32)
64 gd->bd->bi_dram[num_banks].start = area->start;
65 gd->bd->bi_dram[num_banks].size = area->size;
70 int mrc_add_memory_area(struct memory_info *info, uint64_t start,
73 struct memory_area *ptr;
75 if (info->num_areas == CONFIG_NR_DRAM_BANKS)
78 ptr = &info->area[info->num_areas];
80 ptr->size = end - start;
81 info->total_memory += ptr->size;
82 if (ptr->start < (1ULL << 32))
83 info->total_32bit_memory += ptr->size;
84 debug("%d: memory %llx size %llx, total now %llx / %llx\n",
85 info->num_areas, ptr->start, ptr->size,
86 info->total_32bit_memory, info->total_memory);
93 * Dump in the log memory controller configuration as read from the memory
94 * controller registers.
96 void report_memory_config(void)
98 u32 addr_decoder_common, addr_decode_ch[2];
101 addr_decoder_common = readl(MCHBAR_REG(0x5000));
102 addr_decode_ch[0] = readl(MCHBAR_REG(0x5004));
103 addr_decode_ch[1] = readl(MCHBAR_REG(0x5008));
105 debug("memcfg DDR3 clock %d MHz\n",
106 (readl(MCHBAR_REG(0x5e04)) * 13333 * 2 + 50) / 100);
107 debug("memcfg channel assignment: A: %d, B % d, C % d\n",
108 addr_decoder_common & 3,
109 (addr_decoder_common >> 2) & 3,
110 (addr_decoder_common >> 4) & 3);
112 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
113 u32 ch_conf = addr_decode_ch[i];
114 debug("memcfg channel[%d] config (%8.8x):\n", i, ch_conf);
115 debug(" ECC %s\n", ecc_decoder[(ch_conf >> 24) & 3]);
116 debug(" enhanced interleave mode %s\n",
117 ((ch_conf >> 22) & 1) ? "on" : "off");
118 debug(" rank interleave %s\n",
119 ((ch_conf >> 21) & 1) ? "on" : "off");
120 debug(" DIMMA %d MB width x%d %s rank%s\n",
121 ((ch_conf >> 0) & 0xff) * 256,
122 ((ch_conf >> 19) & 1) ? 16 : 8,
123 ((ch_conf >> 17) & 1) ? "dual" : "single",
124 ((ch_conf >> 16) & 1) ? "" : ", selected");
125 debug(" DIMMB %d MB width x%d %s rank%s\n",
126 ((ch_conf >> 8) & 0xff) * 256,
127 ((ch_conf >> 20) & 1) ? 16 : 8,
128 ((ch_conf >> 18) & 1) ? "dual" : "single",
129 ((ch_conf >> 16) & 1) ? ", selected" : "");
133 int mrc_locate_spd(struct udevice *dev, int size, const void **spd_datap)
135 const void *blob = gd->fdt_blob;
137 struct gpio_desc desc[4];
142 ret = gpio_request_list_by_name(dev, "board-id-gpios", desc,
143 ARRAY_SIZE(desc), GPIOD_IS_IN);
145 debug("%s: gpio ret=%d\n", __func__, ret);
148 spd_index = dm_gpio_get_values_as_int(desc, ret);
149 debug("spd index %d\n", spd_index);
151 node = fdt_first_subnode(blob, dev_of_offset(dev));
154 for (spd_node = fdt_first_subnode(blob, node);
156 spd_node = fdt_next_subnode(blob, spd_node)) {
159 if (fdtdec_get_int(blob, spd_node, "reg", -1) != spd_index)
161 *spd_datap = fdt_getprop(blob, spd_node, "data", &len);
163 printf("Missing SPD data\n");
167 debug("Using SDRAM SPD data for '%s'\n",
168 fdt_get_name(blob, spd_node, NULL));
172 printf("No SPD data found for index %d\n", spd_index);
176 asmlinkage void sdram_console_tx_byte(unsigned char byte)
184 * Find the PEI executable in the ROM and execute it.
186 * @me_dev: Management Engine device
187 * @pei_data: configuration data for UEFI PEI reference code
189 static int sdram_initialise(struct udevice *dev, struct udevice *me_dev,
190 void *pei_data, bool use_asm_linkage)
195 report_platform_info(dev);
196 debug("Starting UEFI PEI System Agent\n");
198 debug("PEI data at %p:\n", pei_data);
200 data = (char *)CONFIG_X86_MRC_ADDR;
205 debug("Calling MRC at %p\n", data);
206 post_code(POST_PRE_MRC);
207 start = get_timer(0);
208 if (use_asm_linkage) {
209 asmlinkage int (*func)(void *);
211 func = (asmlinkage int (*)(void *))data;
216 func = (int (*)(void *))data;
223 printf("PEI version mismatch.\n");
226 printf("Invalid memory frequency.\n");
229 printf("MRC returned %x.\n", rv);
231 printf("Nonzero MRC return value.\n");
234 debug("MRC execution time %lu ms\n", get_timer(start));
236 printf("UEFI PEI System Agent not found.\n");
240 version = readl(MCHBAR_REG(MCHBAR_PEI_VERSION));
241 debug("System Agent Version %d.%d.%d Build %d\n",
242 version >> 24 , (version >> 16) & 0xff,
243 (version >> 8) & 0xff, version & 0xff);
248 int mrc_common_init(struct udevice *dev, void *pei_data, bool use_asm_linkage)
250 struct udevice *me_dev;
253 ret = syscon_get_by_driver_data(X86_SYSCON_ME, &me_dev);
257 ret = sdram_initialise(dev, me_dev, pei_data, use_asm_linkage);
261 post_code(POST_DRAM);
262 report_memory_config();