blob: 1e020f95b53402a2ec75fc4678c93813dd066e4e [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>
Aaron Durbin76c37002012-10-30 09:03:43 -050024#include <cbmem.h>
25#include <arch/cbfs.h>
26#include <cbfs.h>
27#include <ip_checksum.h>
28#include <pc80/mc146818rtc.h>
29#include <device/pci_def.h>
30#include "raminit.h"
31#include "pei_data.h"
32#include "haswell.h"
33
Aaron Durbin76c37002012-10-30 09:03:43 -050034#if CONFIG_CHROMEOS
35#include <vendorcode/google/chromeos/chromeos.h>
36#else
37#define recovery_mode_enabled(x) 0
38#endif
39
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060040void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050041{
Aaron Durbin76c37002012-10-30 09:03:43 -050042 struct mrc_data_container *mrcdata;
43 int output_len = ALIGN(pei_data->mrc_output_len, 16);
44
45 /* Save the MRC S3 restore data to cbmem */
Aaron Durbin76c37002012-10-30 09:03:43 -050046 mrcdata = cbmem_add
47 (CBMEM_ID_MRCDATA,
48 output_len + sizeof(struct mrc_data_container));
49
50 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
51 pei_data->mrc_output, mrcdata, output_len);
52
53 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
54 mrcdata->mrc_data_size = output_len;
55 mrcdata->reserved = 0;
56 memcpy(mrcdata->mrc_data, pei_data->mrc_output,
57 pei_data->mrc_output_len);
58
59 /* Zero the unused space in aligned buffer. */
60 if (output_len > pei_data->mrc_output_len)
61 memset(mrcdata->mrc_data+pei_data->mrc_output_len, 0,
62 output_len - pei_data->mrc_output_len);
63
64 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
65 mrcdata->mrc_data_size);
Aaron Durbin76c37002012-10-30 09:03:43 -050066}
67
68static void prepare_mrc_cache(struct pei_data *pei_data)
69{
70 struct mrc_data_container *mrc_cache;
Aaron Durbin76c37002012-10-30 09:03:43 -050071
72 // preset just in case there is an error
73 pei_data->mrc_input = NULL;
74 pei_data->mrc_input_len = 0;
75
Aaron Durbin76c37002012-10-30 09:03:43 -050076 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
77 /* error message printed in find_current_mrc_cache */
78 return;
79 }
80
81 pei_data->mrc_input = mrc_cache->mrc_data;
82 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
83
84 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
85 __func__, pei_data->mrc_input,
86 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
87}
88
89static const char* ecc_decoder[] = {
90 "inactive",
91 "active on IO",
92 "disabled on IO",
93 "active"
94};
95
96/*
97 * Dump in the log memory controller configuration as read from the memory
98 * controller registers.
99 */
100static void report_memory_config(void)
101{
102 u32 addr_decoder_common, addr_decode_ch[2];
103 int i;
104
105 addr_decoder_common = MCHBAR32(0x5000);
106 addr_decode_ch[0] = MCHBAR32(0x5004);
107 addr_decode_ch[1] = MCHBAR32(0x5008);
108
109 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
110 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
111 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
112 addr_decoder_common & 3,
113 (addr_decoder_common >> 2) & 3,
114 (addr_decoder_common >> 4) & 3);
115
116 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
117 u32 ch_conf = addr_decode_ch[i];
118 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
119 i, ch_conf);
120 printk(BIOS_DEBUG, " ECC %s\n",
121 ecc_decoder[(ch_conf >> 24) & 3]);
122 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
123 ((ch_conf >> 22) & 1) ? "on" : "off");
124 printk(BIOS_DEBUG, " rank interleave %s\n",
125 ((ch_conf >> 21) & 1) ? "on" : "off");
126 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
127 ((ch_conf >> 0) & 0xff) * 256,
128 ((ch_conf >> 19) & 1) ? 16 : 8,
129 ((ch_conf >> 17) & 1) ? "dual" : "single",
130 ((ch_conf >> 16) & 1) ? "" : ", selected");
131 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
132 ((ch_conf >> 8) & 0xff) * 256,
133 ((ch_conf >> 20) & 1) ? 16 : 8,
134 ((ch_conf >> 18) & 1) ? "dual" : "single",
135 ((ch_conf >> 16) & 1) ? ", selected" : "");
136 }
137}
138
139/**
140 * Find PEI executable in coreboot filesystem and execute it.
141 *
142 * @param pei_data: configuration data for UEFI PEI reference code
143 */
144void sdram_initialize(struct pei_data *pei_data)
145{
Aaron Durbin76c37002012-10-30 09:03:43 -0500146 unsigned long entry;
147
Aaron Durbin76c37002012-10-30 09:03:43 -0500148 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
149
Aaron Durbin76c37002012-10-30 09:03:43 -0500150 /*
151 * Do not pass MRC data in for recovery mode boot,
152 * Always pass it in for S3 resume.
153 */
154 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
155 prepare_mrc_cache(pei_data);
156
157 /* If MRC data is not found we cannot continue S3 resume. */
158 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700159 post_code(POST_RESUME_FAILURE);
160 printk(BIOS_DEBUG, "Giving up in sdram_initialize: "
161 "No MRC data\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500162 outb(0x6, 0xcf9);
163 while(1) {
164 hlt();
165 }
166 }
167
168 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200169 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500170
171 /* Locate and call UEFI System Agent binary. */
172 entry = (unsigned long)cbfs_get_file_content(
Vladimir Serbinenko0af61b62014-01-12 13:45:52 +0100173 CBFS_DEFAULT_MEDIA, "mrc.bin", 0xab, NULL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500174 if (entry) {
175 int rv;
176 asm volatile (
177 "call *%%ecx\n\t"
178 :"=a" (rv) : "c" (entry), "a" (pei_data));
179 if (rv) {
180 switch (rv) {
181 case -1:
182 printk(BIOS_ERR, "PEI version mismatch.\n");
183 break;
184 case -2:
185 printk(BIOS_ERR, "Invalid memory frequency.\n");
186 break;
187 default:
188 printk(BIOS_ERR, "MRC returned %x.\n", rv);
189 }
190 die("Nonzero MRC return value.\n");
191 }
192 } else {
193 die("UEFI PEI System Agent not found.\n");
194 }
195
196 /* For reference print the System Agent version
197 * after executing the UEFI PEI stage.
198 */
199 u32 version = MCHBAR32(0x5034);
200 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
201 version >> 24 , (version >> 16) & 0xff,
202 (version >> 8) & 0xff, version & 0xff);
203
Aaron Durbin76c37002012-10-30 09:03:43 -0500204 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500205}