Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 1 | /* |
| 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 Menzel | a46a712 | 2013-02-23 18:37:27 +0100 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include <console/console.h> |
Kyösti Mälkki | 1d7541f | 2014-02-17 21:34:42 +0200 | [diff] [blame] | 21 | #include <console/usb.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 22 | #include <string.h> |
| 23 | #include <arch/hlt.h> |
| 24 | #include <arch/io.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 25 | #include <cbmem.h> |
| 26 | #include <arch/cbfs.h> |
| 27 | #include <cbfs.h> |
| 28 | #include <ip_checksum.h> |
| 29 | #include <pc80/mc146818rtc.h> |
Duncan Laurie | 7b508dd | 2012-04-09 12:30:43 -0700 | [diff] [blame] | 30 | #include <device/pci_def.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 31 | #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 Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 39 | #else |
| 40 | #define recovery_mode_enabled(x) 0 |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 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 |
Duncan Laurie | cf81b82 | 2012-08-08 13:43:55 -0700 | [diff] [blame] | 53 | #define CMOS_OFFSET_MRC_SEED 152 |
| 54 | #define CMOS_OFFSET_MRC_SEED_S3 156 |
| 55 | #define CMOS_OFFSET_MRC_SEED_CHK 160 |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 56 | #endif |
| 57 | |
Kyösti Mälkki | 7893848 | 2014-01-04 11:02:45 +0200 | [diff] [blame] | 58 | void save_mrc_data(struct pei_data *pei_data) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 59 | { |
| 60 | u16 c1, c2, checksum; |
| 61 | |
Kyösti Mälkki | f8bf5a1 | 2013-10-11 22:08:02 +0300 | [diff] [blame] | 62 | #if CONFIG_EARLY_CBMEM_INIT |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 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 */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 67 | 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älkki | f8bf5a1 | 2013-10-11 22:08:02 +0300 | [diff] [blame] | 87 | #endif |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 88 | |
| 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 Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 109 | static void prepare_mrc_cache(struct pei_data *pei_data) |
| 110 | { |
Stefan Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 111 | struct mrc_data_container *mrc_cache; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 112 | u16 c1, c2, checksum, seed_checksum; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 113 | |
| 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 Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 144 | if ((mrc_cache = find_current_mrc_cache()) == NULL) { |
| 145 | /* error message printed in find_current_mrc_cache */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 146 | 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 Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 152 | printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n", |
| 153 | __func__, pei_data->mrc_input, |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 154 | pei_data->mrc_input_len, mrc_cache->mrc_checksum); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 155 | } |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 156 | |
| 157 | static 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 | */ |
| 168 | static 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 Jones | 313ec9d | 2012-11-02 14:26:44 -0600 | [diff] [blame] | 207 | static 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 Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 214 | /** |
| 215 | * Find PEI executable in coreboot filesystem and execute it. |
| 216 | * |
| 217 | * @param pei_data: configuration data for UEFI PEI reference code |
| 218 | */ |
| 219 | void sdram_initialize(struct pei_data *pei_data) |
| 220 | { |
| 221 | struct sys_info sysinfo; |
Vladimir Serbinenko | c2ee680 | 2014-01-12 14:28:56 +0100 | [diff] [blame] | 222 | int (*entry) (struct pei_data *pei_data) __attribute__ ((regparm(1))); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 223 | |
Vadim Bendebury | 7a3f36a | 2012-04-18 15:47:32 -0700 | [diff] [blame] | 224 | report_platform_info(); |
| 225 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 226 | /* 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 Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 236 | /* |
| 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 Reinauer | 1244f4b | 2012-05-10 11:31:40 -0700 | [diff] [blame] | 245 | printk(BIOS_DEBUG, "Giving up in sdram_initialize: No MRC data\n"); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 246 | outb(0x6, 0xcf9); |
| 247 | hlt(); |
| 248 | } |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 249 | |
Vadim Bendebury | 48a4a7f | 2012-06-07 18:47:13 -0700 | [diff] [blame] | 250 | /* Pass console handler in pei_data */ |
Kyösti Mälkki | 657e0be | 2014-02-04 19:03:57 +0200 | [diff] [blame] | 251 | pei_data->tx_byte = do_putchar; |
Vadim Bendebury | 48a4a7f | 2012-06-07 18:47:13 -0700 | [diff] [blame] | 252 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 253 | /* Locate and call UEFI System Agent binary. */ |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 254 | /* TODO make MRC blob (0xab?) defined in cbfs_core.h. */ |
Vladimir Serbinenko | c2ee680 | 2014-01-12 14:28:56 +0100 | [diff] [blame] | 255 | entry = cbfs_get_file_content( |
Vladimir Serbinenko | 0af61b6 | 2014-01-12 13:45:52 +0100 | [diff] [blame] | 256 | CBFS_DEFAULT_MEDIA, "mrc.bin", 0xab, NULL); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 257 | if (entry) { |
| 258 | int rv; |
Vladimir Serbinenko | c2ee680 | 2014-01-12 14:28:56 +0100 | [diff] [blame] | 259 | rv = entry (pei_data); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 260 | if (rv) { |
Stefan Reinauer | 696262b | 2012-06-18 14:38:40 -0700 | [diff] [blame] | 261 | switch (rv) { |
| 262 | case -1: |
Stefan Reinauer | c6b9f92 | 2012-08-09 11:00:14 -0700 | [diff] [blame] | 263 | printk(BIOS_ERR, "PEI version mismatch.\n"); |
Stefan Reinauer | 696262b | 2012-06-18 14:38:40 -0700 | [diff] [blame] | 264 | 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 Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 272 | } |
| 273 | } else { |
| 274 | die("UEFI PEI System Agent not found.\n"); |
| 275 | } |
| 276 | |
Kyösti Mälkki | 50ecb9c | 2013-12-06 21:50:55 +0200 | [diff] [blame] | 277 | #if CONFIG_USBDEBUG_IN_ROMSTAGE |
Sven Schnelle | d4ee808 | 2012-07-28 09:28:56 +0200 | [diff] [blame] | 278 | /* mrc.bin reconfigures USB, so reinit it to have debug */ |
Kyösti Mälkki | 9e7806a | 2013-07-06 11:56:49 +0300 | [diff] [blame] | 279 | usbdebug_init(); |
Sven Schnelle | d4ee808 | 2012-07-28 09:28:56 +0200 | [diff] [blame] | 280 | #endif |
| 281 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 282 | /* 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 Laurie | 7b508dd | 2012-04-09 12:30:43 -0700 | [diff] [blame] | 290 | /* 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 Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 297 | |
Marc Jones | 313ec9d | 2012-11-02 14:26:44 -0600 | [diff] [blame] | 298 | post_system_agent_init(pei_data); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 299 | report_memory_config(); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 300 | } |