blob: bfb403317a141ee285eec55b591f6e58e342002c [file] [log] [blame]
Stefan Reinauer00636b02012-04-04 00:08:51 +02001/*
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
Paul Menzela46a7122013-02-23 18:37:27 +010017 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Stefan Reinauer00636b02012-04-04 00:08:51 +020018 */
19
20#include <console/console.h>
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020021#include <console/usb.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020022#include <string.h>
23#include <arch/hlt.h>
24#include <arch/io.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020025#include <cbmem.h>
26#include <arch/cbfs.h>
27#include <cbfs.h>
28#include <ip_checksum.h>
29#include <pc80/mc146818rtc.h>
Duncan Laurie7b508dd2012-04-09 12:30:43 -070030#include <device/pci_def.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020031#include "raminit.h"
32#include "pei_data.h"
33#include "sandybridge.h"
34
35/* Management Engine is in the southbridge */
36#include "southbridge/intel/bd82x6x/me.h"
37#if CONFIG_CHROMEOS
38#include <vendorcode/google/chromeos/chromeos.h>
Stefan Reinauer1244f4b2012-05-10 11:31:40 -070039#else
40#define recovery_mode_enabled(x) 0
Stefan Reinauer00636b02012-04-04 00:08:51 +020041#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
Duncan Lauriecf81b822012-08-08 13:43:55 -070053#define CMOS_OFFSET_MRC_SEED 152
54#define CMOS_OFFSET_MRC_SEED_S3 156
55#define CMOS_OFFSET_MRC_SEED_CHK 160
Stefan Reinauer00636b02012-04-04 00:08:51 +020056#endif
57
Kyösti Mälkki78938482014-01-04 11:02:45 +020058void save_mrc_data(struct pei_data *pei_data)
Stefan Reinauer00636b02012-04-04 00:08:51 +020059{
60 u16 c1, c2, checksum;
61
Kyösti Mälkkif8bf5a12013-10-11 22:08:02 +030062#if CONFIG_EARLY_CBMEM_INIT
Stefan Reinauer00636b02012-04-04 00:08:51 +020063 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 */
Stefan Reinauer00636b02012-04-04 00:08:51 +020067 mrcdata = cbmem_add
68 (CBMEM_ID_MRCDATA,
69 output_len + sizeof(struct mrc_data_container));
70
71 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
72 pei_data->mrc_output, mrcdata, output_len);
73
74 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
75 mrcdata->mrc_data_size = output_len;
76 mrcdata->reserved = 0;
77 memcpy(mrcdata->mrc_data, pei_data->mrc_output,
78 pei_data->mrc_output_len);
79
80 /* Zero the unused space in aligned buffer. */
81 if (output_len > pei_data->mrc_output_len)
82 memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0,
83 output_len - pei_data->mrc_output_len);
84
85 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
86 mrcdata->mrc_data_size);
Kyösti Mälkkif8bf5a12013-10-11 22:08:02 +030087#endif
Stefan Reinauer00636b02012-04-04 00:08:51 +020088
89 /* Save the MRC seed values to CMOS */
90 cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
91 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
92 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
93
94 cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
95 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
96 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
97
98 /* Save a simple checksum of the seed values */
99 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
100 sizeof(u32));
101 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
102 sizeof(u32));
103 checksum = add_ip_checksums(sizeof(u32), c1, c2);
104
105 cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
106 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1);
107}
108
Stefan Reinauer00636b02012-04-04 00:08:51 +0200109static void prepare_mrc_cache(struct pei_data *pei_data)
110{
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700111 struct mrc_data_container *mrc_cache;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200112 u16 c1, c2, checksum, seed_checksum;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200113
114 // preset just in case there is an error
115 pei_data->mrc_input = NULL;
116 pei_data->mrc_input_len = 0;
117
118 /* Read scrambler seeds from CMOS */
119 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
120 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
121 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
122
123 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
124 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
125 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
126
127 /* Compute seed checksum and compare */
128 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
129 sizeof(u32));
130 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
131 sizeof(u32));
132 checksum = add_ip_checksums(sizeof(u32), c1, c2);
133
134 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
135 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8;
136
137 if (checksum != seed_checksum) {
138 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
139 pei_data->scrambler_seed = 0;
140 pei_data->scrambler_seed_s3 = 0;
141 return;
142 }
143
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700144 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
145 /* error message printed in find_current_mrc_cache */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200146 return;
147 }
148
149 pei_data->mrc_input = mrc_cache->mrc_data;
150 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
151
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700152 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
153 __func__, pei_data->mrc_input,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200154 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200155}
Stefan Reinauer00636b02012-04-04 00:08:51 +0200156
157static const char* ecc_decoder[] = {
158 "inactive",
159 "active on IO",
160 "disabled on IO",
161 "active"
162};
163
164/*
165 * Dump in the log memory controller configuration as read from the memory
166 * controller registers.
167 */
168static void report_memory_config(void)
169{
170 u32 addr_decoder_common, addr_decode_ch[2];
171 int i;
172
173 addr_decoder_common = MCHBAR32(0x5000);
174 addr_decode_ch[0] = MCHBAR32(0x5004);
175 addr_decode_ch[1] = MCHBAR32(0x5008);
176
177 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
178 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
179 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
180 addr_decoder_common & 3,
181 (addr_decoder_common >> 2) & 3,
182 (addr_decoder_common >> 4) & 3);
183
184 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
185 u32 ch_conf = addr_decode_ch[i];
186 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
187 i, ch_conf);
188 printk(BIOS_DEBUG, " ECC %s\n",
189 ecc_decoder[(ch_conf >> 24) & 3]);
190 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
191 ((ch_conf >> 22) & 1) ? "on" : "off");
192 printk(BIOS_DEBUG, " rank interleave %s\n",
193 ((ch_conf >> 21) & 1) ? "on" : "off");
194 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
195 ((ch_conf >> 0) & 0xff) * 256,
196 ((ch_conf >> 19) & 1) ? 16 : 8,
197 ((ch_conf >> 17) & 1) ? "dual" : "single",
198 ((ch_conf >> 16) & 1) ? "" : ", selected");
199 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
200 ((ch_conf >> 8) & 0xff) * 256,
201 ((ch_conf >> 20) & 1) ? 16 : 8,
202 ((ch_conf >> 18) & 1) ? "dual" : "single",
203 ((ch_conf >> 16) & 1) ? ", selected" : "");
204 }
205}
206
Marc Jones313ec9d2012-11-02 14:26:44 -0600207static void post_system_agent_init(struct pei_data *pei_data)
208{
209 /* If PCIe init is skipped, set the PEG clock gating */
210 if (!pei_data->pcie_init)
211 MCHBAR32(0x7010) = MCHBAR32(0x7010) | 0x01;
212}
213
Stefan Reinauer00636b02012-04-04 00:08:51 +0200214/**
215 * Find PEI executable in coreboot filesystem and execute it.
216 *
217 * @param pei_data: configuration data for UEFI PEI reference code
218 */
219void sdram_initialize(struct pei_data *pei_data)
220{
221 struct sys_info sysinfo;
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100222 int (*entry) (struct pei_data *pei_data) __attribute__ ((regparm(1)));
Stefan Reinauer00636b02012-04-04 00:08:51 +0200223
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -0700224 report_platform_info();
225
Stefan Reinauer00636b02012-04-04 00:08:51 +0200226 /* Wait for ME to be ready */
227 intel_early_me_init();
228 intel_early_me_uma_size();
229
230 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
231
232 memset(&sysinfo, 0, sizeof(sysinfo));
233
234 sysinfo.boot_path = pei_data->boot_mode;
235
Stefan Reinauer00636b02012-04-04 00:08:51 +0200236 /*
237 * Do not pass MRC data in for recovery mode boot,
238 * Always pass it in for S3 resume.
239 */
240 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
241 prepare_mrc_cache(pei_data);
242
243 /* If MRC data is not found we cannot continue S3 resume. */
244 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700245 printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200246 outb(0x6, 0xcf9);
247 hlt();
248 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200249
Vadim Bendebury48a4a7f2012-06-07 18:47:13 -0700250 /* Pass console handler in pei_data */
251 pei_data->tx_byte = console_tx_byte;
252
Stefan Reinauer00636b02012-04-04 00:08:51 +0200253 /* Locate and call UEFI System Agent binary. */
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800254 /* TODO make MRC blob (0xab?) defined in cbfs_core.h. */
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100255 entry = cbfs_get_file_content(
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100256 CBFS_DEFAULT_MEDIA, "mrc.bin", 0xab, NULL);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200257 if (entry) {
258 int rv;
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100259 rv = entry (pei_data);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200260 if (rv) {
Stefan Reinauer696262b2012-06-18 14:38:40 -0700261 switch (rv) {
262 case -1:
Stefan Reinauerc6b9f922012-08-09 11:00:14 -0700263 printk(BIOS_ERR, "PEI version mismatch.\n");
Stefan Reinauer696262b2012-06-18 14:38:40 -0700264 break;
265 case -2:
266 printk(BIOS_ERR, "Invalid memory frequency.\n");
267 break;
268 default:
269 printk(BIOS_ERR, "MRC returned %x.\n", rv);
270 }
271 die("Nonzero MRC return value.\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200272 }
273 } else {
274 die("UEFI PEI System Agent not found.\n");
275 }
276
Kyösti Mälkki50ecb9c2013-12-06 21:50:55 +0200277#if CONFIG_USBDEBUG_IN_ROMSTAGE
Sven Schnelled4ee8082012-07-28 09:28:56 +0200278 /* mrc.bin reconfigures USB, so reinit it to have debug */
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300279 usbdebug_init();
Sven Schnelled4ee8082012-07-28 09:28:56 +0200280#endif
281
Stefan Reinauer00636b02012-04-04 00:08:51 +0200282 /* For reference print the System Agent version
283 * after executing the UEFI PEI stage.
284 */
285 u32 version = MCHBAR32(0x5034);
286 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
287 version >> 24 , (version >> 16) & 0xff,
288 (version >> 8) & 0xff, version & 0xff);
289
Duncan Laurie7b508dd2012-04-09 12:30:43 -0700290 /* Send ME init done for SandyBridge here. This is done
291 * inside the SystemAgent binary on IvyBridge. */
292 if (BASE_REV_SNB ==
293 (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
294 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
295 else
296 intel_early_me_status();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200297
Marc Jones313ec9d2012-11-02 14:26:44 -0600298 post_system_agent_init(pei_data);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200299 report_memory_config();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200300}