blob: e5e2b93cab5407c7a8d22eb8750ff5e4951d899a [file] [log] [blame]
Aaron Durbin76c37002012-10-30 09:03:43 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Aaron Durbin76c37002012-10-30 09:03:43 -050014 */
15
16#include <console/console.h>
17#include <string.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include <arch/io.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include <cbmem.h>
20#include <arch/cbfs.h>
21#include <cbfs.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010022#include <halt.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050023#include <ip_checksum.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050024#include <memory_info.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010025#include <mrc_cache.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050026#include <pc80/mc146818rtc.h>
27#include <device/pci_def.h>
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -050028#include <device/dram/ddr3.h>
29#include <smbios.h>
30#include <spd.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020031#include <security/vboot/vboot_common.h>
Arthur Heymansf300f362018-01-27 13:39:12 +010032#include <commonlib/region.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050033#include "raminit.h"
34#include "pei_data.h"
35#include "haswell.h"
36
Arthur Heymansf300f362018-01-27 13:39:12 +010037#define MRC_CACHE_VERSION 1
38
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060039void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050040{
Aaron Durbin76c37002012-10-30 09:03:43 -050041 /* Save the MRC S3 restore data to cbmem */
Arthur Heymansf300f362018-01-27 13:39:12 +010042 mrc_cache_stash_data(MRC_TRAINING_DATA, MRC_CACHE_VERSION,
43 pei_data->mrc_output, pei_data->mrc_output_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050044}
45
46static void prepare_mrc_cache(struct pei_data *pei_data)
47{
Arthur Heymansf300f362018-01-27 13:39:12 +010048 struct region_device rdev;
Aaron Durbin76c37002012-10-30 09:03:43 -050049
50 // preset just in case there is an error
51 pei_data->mrc_input = NULL;
52 pei_data->mrc_input_len = 0;
53
Arthur Heymansf300f362018-01-27 13:39:12 +010054 if (mrc_cache_get_current(MRC_TRAINING_DATA, MRC_CACHE_VERSION, &rdev))
Aaron Durbin76c37002012-10-30 09:03:43 -050055 /* error message printed in find_current_mrc_cache */
56 return;
Aaron Durbin76c37002012-10-30 09:03:43 -050057
Arthur Heymansf300f362018-01-27 13:39:12 +010058 pei_data->mrc_input = rdev_mmap_full(&rdev);
59 pei_data->mrc_input_len = region_device_sz(&rdev);
Aaron Durbin76c37002012-10-30 09:03:43 -050060
Arthur Heymansf300f362018-01-27 13:39:12 +010061 printk(BIOS_DEBUG, "%s: at %p, size %x\n",
62 __func__, pei_data->mrc_input, pei_data->mrc_input_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050063}
64
65static const char* ecc_decoder[] = {
66 "inactive",
67 "active on IO",
68 "disabled on IO",
69 "active"
70};
71
72/*
73 * Dump in the log memory controller configuration as read from the memory
74 * controller registers.
75 */
76static void report_memory_config(void)
77{
78 u32 addr_decoder_common, addr_decode_ch[2];
79 int i;
80
81 addr_decoder_common = MCHBAR32(0x5000);
82 addr_decode_ch[0] = MCHBAR32(0x5004);
83 addr_decode_ch[1] = MCHBAR32(0x5008);
84
85 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
86 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
87 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
88 addr_decoder_common & 3,
89 (addr_decoder_common >> 2) & 3,
90 (addr_decoder_common >> 4) & 3);
91
92 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
93 u32 ch_conf = addr_decode_ch[i];
94 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
95 i, ch_conf);
96 printk(BIOS_DEBUG, " ECC %s\n",
97 ecc_decoder[(ch_conf >> 24) & 3]);
98 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
99 ((ch_conf >> 22) & 1) ? "on" : "off");
100 printk(BIOS_DEBUG, " rank interleave %s\n",
101 ((ch_conf >> 21) & 1) ? "on" : "off");
Duncan Laurie8d774022013-10-22 16:32:49 -0700102 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500103 ((ch_conf >> 0) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -0700104 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500105 ((ch_conf >> 17) & 1) ? "dual" : "single",
106 ((ch_conf >> 16) & 1) ? "" : ", selected");
Duncan Laurie8d774022013-10-22 16:32:49 -0700107 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500108 ((ch_conf >> 8) & 0xff) * 256,
Ryan Salsamendidab81a42017-06-30 17:36:41 -0700109 ((ch_conf >> 20) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500110 ((ch_conf >> 18) & 1) ? "dual" : "single",
111 ((ch_conf >> 16) & 1) ? ", selected" : "");
112 }
113}
114
115/**
116 * Find PEI executable in coreboot filesystem and execute it.
117 *
118 * @param pei_data: configuration data for UEFI PEI reference code
119 */
120void sdram_initialize(struct pei_data *pei_data)
121{
Aaron Durbin76c37002012-10-30 09:03:43 -0500122 unsigned long entry;
123
Aaron Durbin76c37002012-10-30 09:03:43 -0500124 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
125
Aaron Durbin76c37002012-10-30 09:03:43 -0500126 /*
127 * Do not pass MRC data in for recovery mode boot,
128 * Always pass it in for S3 resume.
129 */
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700130 if (!vboot_recovery_mode_enabled() || pei_data->boot_mode == 2)
Aaron Durbin76c37002012-10-30 09:03:43 -0500131 prepare_mrc_cache(pei_data);
132
133 /* If MRC data is not found we cannot continue S3 resume. */
134 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700135 post_code(POST_RESUME_FAILURE);
136 printk(BIOS_DEBUG, "Giving up in sdram_initialize: "
137 "No MRC data\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500138 outb(0x6, 0xcf9);
Patrick Georgibd79c5e2014-11-28 22:35:36 +0100139 halt();
Aaron Durbin76c37002012-10-30 09:03:43 -0500140 }
141
142 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200143 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500144
145 /* Locate and call UEFI System Agent binary. */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500146 entry = (unsigned long)cbfs_boot_map_with_leak("mrc.bin",
147 CBFS_TYPE_MRC, NULL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500148 if (entry) {
149 int rv;
150 asm volatile (
151 "call *%%ecx\n\t"
152 :"=a" (rv) : "c" (entry), "a" (pei_data));
153 if (rv) {
154 switch (rv) {
155 case -1:
156 printk(BIOS_ERR, "PEI version mismatch.\n");
157 break;
158 case -2:
159 printk(BIOS_ERR, "Invalid memory frequency.\n");
160 break;
161 default:
162 printk(BIOS_ERR, "MRC returned %x.\n", rv);
163 }
164 die("Nonzero MRC return value.\n");
165 }
166 } else {
167 die("UEFI PEI System Agent not found.\n");
168 }
169
170 /* For reference print the System Agent version
171 * after executing the UEFI PEI stage.
172 */
173 u32 version = MCHBAR32(0x5034);
174 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
175 version >> 24 , (version >> 16) & 0xff,
176 (version >> 8) & 0xff, version & 0xff);
177
Aaron Durbin76c37002012-10-30 09:03:43 -0500178 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500179}
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500180
181void setup_sdram_meminfo(struct pei_data *pei_data)
182{
183 u32 addr_decoder_common, addr_decode_ch[2];
184 struct memory_info* mem_info;
185 struct dimm_info *dimm;
186 int ddr_frequency;
187 int dimm_size;
188 int ch, d_num;
189 int dimm_cnt = 0;
190
191 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(struct memory_info));
Nico Huberacac02d2017-06-20 14:49:04 +0200192 if (!mem_info)
193 die("Failed to add memory info to CBMEM.\n");
Matt DeVillier5aaa8ce2016-09-02 13:29:17 -0500194 memset(mem_info, 0, sizeof(struct memory_info));
195
196 addr_decoder_common = MCHBAR32(0x5000);
197 addr_decode_ch[0] = MCHBAR32(0x5004);
198 addr_decode_ch[1] = MCHBAR32(0x5008);
199
200 ddr_frequency = (MCHBAR32(0x5e04) * 13333 * 2 + 50) / 100;
201
202 for (ch = 0; ch < ARRAY_SIZE(addr_decode_ch); ch++) {
203 u32 ch_conf = addr_decode_ch[ch];
204 /* DIMMs A/B */
205 for (d_num = 0; d_num < 2; d_num++) {
206 dimm_size = ((ch_conf >> (d_num * 8)) & 0xff) * 256;
207 if (dimm_size) {
208 dimm = &mem_info->dimm[dimm_cnt];
209 dimm->dimm_size = dimm_size;
210 dimm->ddr_type = MEMORY_TYPE_DDR3;
211 dimm->ddr_frequency = ddr_frequency;
212 dimm->rank_per_dimm = 1 + ((ch_conf >> (17 + d_num)) & 1);
213 dimm->channel_num = ch;
214 dimm->dimm_num = d_num;
215 dimm->bank_locator = ch * 2;
216 memcpy(dimm->serial,
217 &pei_data->spd_data[dimm_cnt][SPD_DIMM_SERIAL_NUM],
218 SPD_DIMM_SERIAL_LEN);
219 memcpy(dimm->module_part_number,
220 &pei_data->spd_data[dimm_cnt][SPD_DIMM_PART_NUM],
221 SPD_DIMM_PART_LEN);
222 dimm->mod_id =
223 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID2] << 8) |
224 (pei_data->spd_data[dimm_cnt][SPD_DIMM_MOD_ID1] & 0xFF);
225 dimm->mod_type = SPD_SODIMM;
226 dimm->bus_width = 0x3; /* 64-bit */
227 dimm_cnt++;
228 }
229 }
230 }
231 mem_info->dimm_cnt = dimm_cnt;
232}