blob: b45682596e9ed50930a7df63f593c993cde84f02 [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <console/console.h>
21#include <string.h>
22#include <arch/hlt.h>
23#include <arch/io.h>
24#include <arch/romcc_io.h>
25#include <cbmem.h>
26#include <arch/cbfs.h>
27#include <cbfs.h>
28#include <ip_checksum.h>
29#include <pc80/mc146818rtc.h>
30#include <device/pci_def.h>
31#include "raminit.h"
32#include "pei_data.h"
33#include "haswell.h"
34
35/* Management Engine is in the southbridge */
36#include "southbridge/intel/lynxpoint/me.h"
37#if CONFIG_CHROMEOS
38#include <vendorcode/google/chromeos/chromeos.h>
39#else
40#define recovery_mode_enabled(x) 0
41#endif
42
43/*
44 * MRC scrambler seed offsets should be reserved in
45 * mainboard cmos.layout and not covered by checksum.
46 */
47#if CONFIG_USE_OPTION_TABLE
48#include "option_table.h"
49#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
50#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
51#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
52#else
53#define CMOS_OFFSET_MRC_SEED 152
54#define CMOS_OFFSET_MRC_SEED_S3 156
55#define CMOS_OFFSET_MRC_SEED_CHK 160
56#endif
57
58static void save_mrc_data(struct pei_data *pei_data)
59{
60 u16 c1, c2, checksum;
61
62#if CONFIG_EARLY_CBMEM_INIT
63 struct mrc_data_container *mrcdata;
64 int output_len = ALIGN(pei_data->mrc_output_len, 16);
65
66 /* Save the MRC S3 restore data to cbmem */
67 cbmem_initialize();
68 mrcdata = cbmem_add
69 (CBMEM_ID_MRCDATA,
70 output_len + sizeof(struct mrc_data_container));
71
72 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
73 pei_data->mrc_output, mrcdata, output_len);
74
75 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
76 mrcdata->mrc_data_size = output_len;
77 mrcdata->reserved = 0;
78 memcpy(mrcdata->mrc_data, pei_data->mrc_output,
79 pei_data->mrc_output_len);
80
81 /* Zero the unused space in aligned buffer. */
82 if (output_len > pei_data->mrc_output_len)
83 memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0,
84 output_len - pei_data->mrc_output_len);
85
86 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
87 mrcdata->mrc_data_size);
88#endif
89
90 /* Save the MRC seed values to CMOS */
91 cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
92 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
93 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
94
95 cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
96 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
97 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
98
99 /* Save a simple checksum of the seed values */
100 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
101 sizeof(u32));
102 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
103 sizeof(u32));
104 checksum = add_ip_checksums(sizeof(u32), c1, c2);
105
106 cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
107 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1);
108}
109
110static void prepare_mrc_cache(struct pei_data *pei_data)
111{
112 struct mrc_data_container *mrc_cache;
113 u16 c1, c2, checksum, seed_checksum;
114
115 // preset just in case there is an error
116 pei_data->mrc_input = NULL;
117 pei_data->mrc_input_len = 0;
118
119 /* Read scrambler seeds from CMOS */
120 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
121 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
122 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
123
124 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
125 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
126 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
127
128 /* Compute seed checksum and compare */
129 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
130 sizeof(u32));
131 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
132 sizeof(u32));
133 checksum = add_ip_checksums(sizeof(u32), c1, c2);
134
135 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
136 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8;
137
138 if (checksum != seed_checksum) {
139 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
140 pei_data->scrambler_seed = 0;
141 pei_data->scrambler_seed_s3 = 0;
142 return;
143 }
144
145 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
146 /* error message printed in find_current_mrc_cache */
147 return;
148 }
149
150 pei_data->mrc_input = mrc_cache->mrc_data;
151 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
152
153 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
154 __func__, pei_data->mrc_input,
155 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
156}
157
158static const char* ecc_decoder[] = {
159 "inactive",
160 "active on IO",
161 "disabled on IO",
162 "active"
163};
164
165/*
166 * Dump in the log memory controller configuration as read from the memory
167 * controller registers.
168 */
169static void report_memory_config(void)
170{
171 u32 addr_decoder_common, addr_decode_ch[2];
172 int i;
173
174 addr_decoder_common = MCHBAR32(0x5000);
175 addr_decode_ch[0] = MCHBAR32(0x5004);
176 addr_decode_ch[1] = MCHBAR32(0x5008);
177
178 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
179 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
180 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
181 addr_decoder_common & 3,
182 (addr_decoder_common >> 2) & 3,
183 (addr_decoder_common >> 4) & 3);
184
185 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
186 u32 ch_conf = addr_decode_ch[i];
187 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
188 i, ch_conf);
189 printk(BIOS_DEBUG, " ECC %s\n",
190 ecc_decoder[(ch_conf >> 24) & 3]);
191 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
192 ((ch_conf >> 22) & 1) ? "on" : "off");
193 printk(BIOS_DEBUG, " rank interleave %s\n",
194 ((ch_conf >> 21) & 1) ? "on" : "off");
195 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
196 ((ch_conf >> 0) & 0xff) * 256,
197 ((ch_conf >> 19) & 1) ? 16 : 8,
198 ((ch_conf >> 17) & 1) ? "dual" : "single",
199 ((ch_conf >> 16) & 1) ? "" : ", selected");
200 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
201 ((ch_conf >> 8) & 0xff) * 256,
202 ((ch_conf >> 20) & 1) ? 16 : 8,
203 ((ch_conf >> 18) & 1) ? "dual" : "single",
204 ((ch_conf >> 16) & 1) ? ", selected" : "");
205 }
206}
207
208/**
209 * Find PEI executable in coreboot filesystem and execute it.
210 *
211 * @param pei_data: configuration data for UEFI PEI reference code
212 */
213void sdram_initialize(struct pei_data *pei_data)
214{
215 struct sys_info sysinfo;
216 unsigned long entry;
217
218 report_platform_info();
219
220 /* Wait for ME to be ready */
221 intel_early_me_init();
222 intel_early_me_uma_size();
223
224 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
225
226 memset(&sysinfo, 0, sizeof(sysinfo));
227
228 sysinfo.boot_path = pei_data->boot_mode;
229
230 /*
231 * Do not pass MRC data in for recovery mode boot,
232 * Always pass it in for S3 resume.
233 */
234 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
235 prepare_mrc_cache(pei_data);
236
237 /* If MRC data is not found we cannot continue S3 resume. */
238 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
239 printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n");
240 outb(0x6, 0xcf9);
241 while(1) {
242 hlt();
243 }
244 }
245
246 /* Pass console handler in pei_data */
247 pei_data->tx_byte = console_tx_byte;
248
249 /* Locate and call UEFI System Agent binary. */
250 entry = (unsigned long)cbfs_get_file_content(
251 CBFS_DEFAULT_MEDIA, "mrc.bin", 0xab);
252 if (entry) {
253 int rv;
254 asm volatile (
255 "call *%%ecx\n\t"
256 :"=a" (rv) : "c" (entry), "a" (pei_data));
257 if (rv) {
258 switch (rv) {
259 case -1:
260 printk(BIOS_ERR, "PEI version mismatch.\n");
261 break;
262 case -2:
263 printk(BIOS_ERR, "Invalid memory frequency.\n");
264 break;
265 default:
266 printk(BIOS_ERR, "MRC returned %x.\n", rv);
267 }
268 die("Nonzero MRC return value.\n");
269 }
270 } else {
271 die("UEFI PEI System Agent not found.\n");
272 }
273
274 /* For reference print the System Agent version
275 * after executing the UEFI PEI stage.
276 */
277 u32 version = MCHBAR32(0x5034);
278 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
279 version >> 24 , (version >> 16) & 0xff,
280 (version >> 8) & 0xff, version & 0xff);
281
282 /* Send ME init done for SandyBridge here. This is done
283 * inside the SystemAgent binary on IvyBridge. */
284 if (BASE_REV_SNB ==
285 (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
286 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
287 else
288 intel_early_me_status();
289
290 report_memory_config();
291
292 /* S3 resume: don't save scrambler seed or MRC data */
293 if (pei_data->boot_mode != 2)
294 save_mrc_data(pei_data);
295}
296
297struct cbmem_entry *get_cbmem_toc(void)
298{
299 return (struct cbmem_entry *)(get_top_of_ram() - HIGH_MEMORY_SIZE);
300}
301
302unsigned long get_top_of_ram(void)
303{
304 /* Base of TSEG is top of usable DRAM */
305 u32 tom = pci_read_config32(PCI_DEV(0,0,0), TSEG);
306 return (unsigned long) tom;
307}