1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Google, Inc
13 #include <asm/intel_regs.h>
14 #include <asm/mrc_common.h>
15 #include <asm/pch_common.h>
17 #include <asm/arch/me.h>
18 #include <asm/report_platform.h>
20 static const char *const ecc_decoder[] = {
27 ulong mrc_common_board_get_usable_ram_top(ulong total_size)
29 struct memory_info *info = &gd->arch.meminfo;
30 uintptr_t dest_addr = 0;
31 struct memory_area *largest = NULL;
34 /* Find largest area of memory below 4GB */
36 for (i = 0; i < info->num_areas; i++) {
37 struct memory_area *area = &info->area[i];
39 if (area->start >= 1ULL << 32)
41 if (!largest || area->size > largest->size)
45 /* If no suitable area was found, return an error. */
47 if (!largest || largest->size < (2 << 20))
48 panic("No available memory found for relocation");
50 dest_addr = largest->start + largest->size;
52 return (ulong)dest_addr;
55 void mrc_common_dram_init_banksize(void)
57 struct memory_info *info = &gd->arch.meminfo;
61 for (i = 0, num_banks = 0; i < info->num_areas; i++) {
62 struct memory_area *area = &info->area[i];
64 if (area->start >= 1ULL << 32)
66 gd->bd->bi_dram[num_banks].start = area->start;
67 gd->bd->bi_dram[num_banks].size = area->size;
72 int mrc_add_memory_area(struct memory_info *info, uint64_t start,
75 struct memory_area *ptr;
77 if (info->num_areas == CONFIG_NR_DRAM_BANKS)
80 ptr = &info->area[info->num_areas];
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);
95 * Dump in the log memory controller configuration as read from the memory
96 * controller registers.
98 void report_memory_config(void)
100 u32 addr_decoder_common, addr_decode_ch[2];
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));
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);
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" : "");
135 int mrc_locate_spd(struct udevice *dev, int size, const void **spd_datap)
137 const void *blob = gd->fdt_blob;
139 struct gpio_desc desc[4];
144 ret = gpio_request_list_by_name(dev, "board-id-gpios", desc,
145 ARRAY_SIZE(desc), GPIOD_IS_IN);
147 debug("%s: gpio ret=%d\n", __func__, ret);
150 spd_index = dm_gpio_get_values_as_int(desc, ret);
151 debug("spd index %d\n", spd_index);
153 node = fdt_first_subnode(blob, dev_of_offset(dev));
156 for (spd_node = fdt_first_subnode(blob, node);
158 spd_node = fdt_next_subnode(blob, spd_node)) {
161 if (fdtdec_get_int(blob, spd_node, "reg", -1) != spd_index)
163 *spd_datap = fdt_getprop(blob, spd_node, "data", &len);
165 printf("Missing SPD data\n");
169 debug("Using SDRAM SPD data for '%s'\n",
170 fdt_get_name(blob, spd_node, NULL));
174 printf("No SPD data found for index %d\n", spd_index);
178 asmlinkage void sdram_console_tx_byte(unsigned char byte)
186 * Find the PEI executable in the ROM and execute it.
188 * @me_dev: Management Engine device
189 * @pei_data: configuration data for UEFI PEI reference code
191 static int sdram_initialise(struct udevice *dev, struct udevice *me_dev,
192 void *pei_data, bool use_asm_linkage)
197 report_platform_info(dev);
198 debug("Starting UEFI PEI System Agent\n");
200 debug("PEI data at %p:\n", pei_data);
202 data = (char *)CONFIG_X86_MRC_ADDR;
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 *);
213 func = (asmlinkage int (*)(void *))data;
218 func = (int (*)(void *))data;
225 printf("PEI version mismatch.\n");
228 printf("Invalid memory frequency.\n");
231 printf("MRC returned %x.\n", rv);
233 printf("Nonzero MRC return value.\n");
236 debug("MRC execution time %lu ms\n", get_timer(start));
238 printf("UEFI PEI System Agent not found.\n");
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);
250 int mrc_common_init(struct udevice *dev, void *pei_data, bool use_asm_linkage)
252 struct udevice *me_dev;
255 ret = syscon_get_by_driver_data(X86_SYSCON_ME, &me_dev);
259 ret = sdram_initialise(dev, me_dev, pei_data, use_asm_linkage);
263 post_code(POST_DRAM);
264 report_memory_config();