blob: b9c3839e1589904946f9959f77a901c37fc6d5bf [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>
21#include <string.h>
22#include <arch/hlt.h>
23#include <arch/io.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020024#include <cbmem.h>
25#include <arch/cbfs.h>
26#include <cbfs.h>
27#include <ip_checksum.h>
28#include <pc80/mc146818rtc.h>
Duncan Laurie7b508dd2012-04-09 12:30:43 -070029#include <device/pci_def.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020030#include "raminit.h"
31#include "pei_data.h"
32#include "sandybridge.h"
33
34/* Management Engine is in the southbridge */
35#include "southbridge/intel/bd82x6x/me.h"
36#if CONFIG_CHROMEOS
37#include <vendorcode/google/chromeos/chromeos.h>
Stefan Reinauer1244f4b2012-05-10 11:31:40 -070038#else
39#define recovery_mode_enabled(x) 0
Stefan Reinauer00636b02012-04-04 00:08:51 +020040#endif
41
42/*
43 * MRC scrambler seed offsets should be reserved in
44 * mainboard cmos.layout and not covered by checksum.
45 */
46#if CONFIG_USE_OPTION_TABLE
47#include "option_table.h"
48#define CMOS_OFFSET_MRC_SEED (CMOS_VSTART_mrc_scrambler_seed >> 3)
49#define CMOS_OFFSET_MRC_SEED_S3 (CMOS_VSTART_mrc_scrambler_seed_s3 >> 3)
50#define CMOS_OFFSET_MRC_SEED_CHK (CMOS_VSTART_mrc_scrambler_seed_chk >> 3)
51#else
Duncan Lauriecf81b822012-08-08 13:43:55 -070052#define CMOS_OFFSET_MRC_SEED 152
53#define CMOS_OFFSET_MRC_SEED_S3 156
54#define CMOS_OFFSET_MRC_SEED_CHK 160
Stefan Reinauer00636b02012-04-04 00:08:51 +020055#endif
56
Kyösti Mälkki78938482014-01-04 11:02:45 +020057void save_mrc_data(struct pei_data *pei_data)
Stefan Reinauer00636b02012-04-04 00:08:51 +020058{
59 u16 c1, c2, checksum;
60
Kyösti Mälkkif8bf5a12013-10-11 22:08:02 +030061#if CONFIG_EARLY_CBMEM_INIT
Stefan Reinauer00636b02012-04-04 00:08:51 +020062 struct mrc_data_container *mrcdata;
63 int output_len = ALIGN(pei_data->mrc_output_len, 16);
64
65 /* Save the MRC S3 restore data to cbmem */
Stefan Reinauer00636b02012-04-04 00:08:51 +020066 mrcdata = cbmem_add
67 (CBMEM_ID_MRCDATA,
68 output_len + sizeof(struct mrc_data_container));
69
70 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
71 pei_data->mrc_output, mrcdata, output_len);
72
73 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
74 mrcdata->mrc_data_size = output_len;
75 mrcdata->reserved = 0;
76 memcpy(mrcdata->mrc_data, pei_data->mrc_output,
77 pei_data->mrc_output_len);
78
79 /* Zero the unused space in aligned buffer. */
80 if (output_len > pei_data->mrc_output_len)
81 memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0,
82 output_len - pei_data->mrc_output_len);
83
84 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
85 mrcdata->mrc_data_size);
Kyösti Mälkkif8bf5a12013-10-11 22:08:02 +030086#endif
Stefan Reinauer00636b02012-04-04 00:08:51 +020087
88 /* Save the MRC seed values to CMOS */
89 cmos_write32(CMOS_OFFSET_MRC_SEED, pei_data->scrambler_seed);
90 printk(BIOS_DEBUG, "Save scrambler seed 0x%08x to CMOS 0x%02x\n",
91 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
92
93 cmos_write32(CMOS_OFFSET_MRC_SEED_S3, pei_data->scrambler_seed_s3);
94 printk(BIOS_DEBUG, "Save s3 scrambler seed 0x%08x to CMOS 0x%02x\n",
95 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
96
97 /* Save a simple checksum of the seed values */
98 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
99 sizeof(u32));
100 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
101 sizeof(u32));
102 checksum = add_ip_checksums(sizeof(u32), c1, c2);
103
104 cmos_write(checksum & 0xff, CMOS_OFFSET_MRC_SEED_CHK);
105 cmos_write((checksum >> 8) & 0xff, CMOS_OFFSET_MRC_SEED_CHK+1);
106}
107
Stefan Reinauer00636b02012-04-04 00:08:51 +0200108static void prepare_mrc_cache(struct pei_data *pei_data)
109{
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700110 struct mrc_data_container *mrc_cache;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200111 u16 c1, c2, checksum, seed_checksum;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200112
113 // preset just in case there is an error
114 pei_data->mrc_input = NULL;
115 pei_data->mrc_input_len = 0;
116
117 /* Read scrambler seeds from CMOS */
118 pei_data->scrambler_seed = cmos_read32(CMOS_OFFSET_MRC_SEED);
119 printk(BIOS_DEBUG, "Read scrambler seed 0x%08x from CMOS 0x%02x\n",
120 pei_data->scrambler_seed, CMOS_OFFSET_MRC_SEED);
121
122 pei_data->scrambler_seed_s3 = cmos_read32(CMOS_OFFSET_MRC_SEED_S3);
123 printk(BIOS_DEBUG, "Read S3 scrambler seed 0x%08x from CMOS 0x%02x\n",
124 pei_data->scrambler_seed_s3, CMOS_OFFSET_MRC_SEED_S3);
125
126 /* Compute seed checksum and compare */
127 c1 = compute_ip_checksum((u8*)&pei_data->scrambler_seed,
128 sizeof(u32));
129 c2 = compute_ip_checksum((u8*)&pei_data->scrambler_seed_s3,
130 sizeof(u32));
131 checksum = add_ip_checksums(sizeof(u32), c1, c2);
132
133 seed_checksum = cmos_read(CMOS_OFFSET_MRC_SEED_CHK);
134 seed_checksum |= cmos_read(CMOS_OFFSET_MRC_SEED_CHK+1) << 8;
135
136 if (checksum != seed_checksum) {
137 printk(BIOS_ERR, "%s: invalid seed checksum\n", __func__);
138 pei_data->scrambler_seed = 0;
139 pei_data->scrambler_seed_s3 = 0;
140 return;
141 }
142
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700143 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
144 /* error message printed in find_current_mrc_cache */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200145 return;
146 }
147
148 pei_data->mrc_input = mrc_cache->mrc_data;
149 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
150
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700151 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
152 __func__, pei_data->mrc_input,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200153 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200154}
Stefan Reinauer00636b02012-04-04 00:08:51 +0200155
156static const char* ecc_decoder[] = {
157 "inactive",
158 "active on IO",
159 "disabled on IO",
160 "active"
161};
162
163/*
164 * Dump in the log memory controller configuration as read from the memory
165 * controller registers.
166 */
167static void report_memory_config(void)
168{
169 u32 addr_decoder_common, addr_decode_ch[2];
170 int i;
171
172 addr_decoder_common = MCHBAR32(0x5000);
173 addr_decode_ch[0] = MCHBAR32(0x5004);
174 addr_decode_ch[1] = MCHBAR32(0x5008);
175
176 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
177 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
178 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
179 addr_decoder_common & 3,
180 (addr_decoder_common >> 2) & 3,
181 (addr_decoder_common >> 4) & 3);
182
183 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
184 u32 ch_conf = addr_decode_ch[i];
185 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
186 i, ch_conf);
187 printk(BIOS_DEBUG, " ECC %s\n",
188 ecc_decoder[(ch_conf >> 24) & 3]);
189 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
190 ((ch_conf >> 22) & 1) ? "on" : "off");
191 printk(BIOS_DEBUG, " rank interleave %s\n",
192 ((ch_conf >> 21) & 1) ? "on" : "off");
193 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
194 ((ch_conf >> 0) & 0xff) * 256,
195 ((ch_conf >> 19) & 1) ? 16 : 8,
196 ((ch_conf >> 17) & 1) ? "dual" : "single",
197 ((ch_conf >> 16) & 1) ? "" : ", selected");
198 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
199 ((ch_conf >> 8) & 0xff) * 256,
200 ((ch_conf >> 20) & 1) ? 16 : 8,
201 ((ch_conf >> 18) & 1) ? "dual" : "single",
202 ((ch_conf >> 16) & 1) ? ", selected" : "");
203 }
204}
205
Marc Jones313ec9d2012-11-02 14:26:44 -0600206static void post_system_agent_init(struct pei_data *pei_data)
207{
208 /* If PCIe init is skipped, set the PEG clock gating */
209 if (!pei_data->pcie_init)
210 MCHBAR32(0x7010) = MCHBAR32(0x7010) | 0x01;
211}
212
Stefan Reinauer00636b02012-04-04 00:08:51 +0200213/**
214 * Find PEI executable in coreboot filesystem and execute it.
215 *
216 * @param pei_data: configuration data for UEFI PEI reference code
217 */
218void sdram_initialize(struct pei_data *pei_data)
219{
220 struct sys_info sysinfo;
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100221 int (*entry) (struct pei_data *pei_data) __attribute__ ((regparm(1)));
Stefan Reinauer00636b02012-04-04 00:08:51 +0200222
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -0700223 report_platform_info();
224
Stefan Reinauer00636b02012-04-04 00:08:51 +0200225 /* Wait for ME to be ready */
226 intel_early_me_init();
227 intel_early_me_uma_size();
228
229 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
230
231 memset(&sysinfo, 0, sizeof(sysinfo));
232
233 sysinfo.boot_path = pei_data->boot_mode;
234
Stefan Reinauer00636b02012-04-04 00:08:51 +0200235 /*
236 * Do not pass MRC data in for recovery mode boot,
237 * Always pass it in for S3 resume.
238 */
239 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
240 prepare_mrc_cache(pei_data);
241
242 /* If MRC data is not found we cannot continue S3 resume. */
243 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Stefan Reinauer1244f4b2012-05-10 11:31:40 -0700244 printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200245 outb(0x6, 0xcf9);
246 hlt();
247 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200248
Vadim Bendebury48a4a7f2012-06-07 18:47:13 -0700249 /* Pass console handler in pei_data */
250 pei_data->tx_byte = console_tx_byte;
251
Stefan Reinauer00636b02012-04-04 00:08:51 +0200252 /* Locate and call UEFI System Agent binary. */
Hung-Te Lin6fe0cab2013-01-22 18:57:56 +0800253 /* TODO make MRC blob (0xab?) defined in cbfs_core.h. */
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100254 entry = cbfs_get_file_content(
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100255 CBFS_DEFAULT_MEDIA, "mrc.bin", 0xab, NULL);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200256 if (entry) {
257 int rv;
Vladimir Serbinenkoc2ee6802014-01-12 14:28:56 +0100258 rv = entry (pei_data);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200259 if (rv) {
Stefan Reinauer696262b2012-06-18 14:38:40 -0700260 switch (rv) {
261 case -1:
Stefan Reinauerc6b9f922012-08-09 11:00:14 -0700262 printk(BIOS_ERR, "PEI version mismatch.\n");
Stefan Reinauer696262b2012-06-18 14:38:40 -0700263 break;
264 case -2:
265 printk(BIOS_ERR, "Invalid memory frequency.\n");
266 break;
267 default:
268 printk(BIOS_ERR, "MRC returned %x.\n", rv);
269 }
270 die("Nonzero MRC return value.\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +0200271 }
272 } else {
273 die("UEFI PEI System Agent not found.\n");
274 }
275
Kyösti Mälkki50ecb9c2013-12-06 21:50:55 +0200276#if CONFIG_USBDEBUG_IN_ROMSTAGE
Sven Schnelled4ee8082012-07-28 09:28:56 +0200277 /* mrc.bin reconfigures USB, so reinit it to have debug */
Kyösti Mälkki9e7806a2013-07-06 11:56:49 +0300278 usbdebug_init();
Sven Schnelled4ee8082012-07-28 09:28:56 +0200279#endif
280
Stefan Reinauer00636b02012-04-04 00:08:51 +0200281 /* For reference print the System Agent version
282 * after executing the UEFI PEI stage.
283 */
284 u32 version = MCHBAR32(0x5034);
285 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
286 version >> 24 , (version >> 16) & 0xff,
287 (version >> 8) & 0xff, version & 0xff);
288
Duncan Laurie7b508dd2012-04-09 12:30:43 -0700289 /* Send ME init done for SandyBridge here. This is done
290 * inside the SystemAgent binary on IvyBridge. */
291 if (BASE_REV_SNB ==
292 (pci_read_config16(PCI_CPU_DEVICE, PCI_DEVICE_ID) & BASE_REV_MASK))
293 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
294 else
295 intel_early_me_status();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200296
Marc Jones313ec9d2012-11-02 14:26:44 -0600297 post_system_agent_init(pei_data);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200298 report_memory_config();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200299}