blob: 464f7c8694caacc62bb76b8470b16be733db6753 [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.
Aaron Durbin76c37002012-10-30 09:03:43 -050014 */
15
16#include <console/console.h>
Kyösti Mälkki5687fc92013-11-28 18:11:49 +020017#include <bootmode.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050018#include <string.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050019#include <arch/io.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050020#include <cbmem.h>
21#include <arch/cbfs.h>
22#include <cbfs.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010023#include <halt.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050024#include <ip_checksum.h>
Alexander Couzens81c5c762016-03-09 03:13:45 +010025#include <northbridge/intel/common/mrc_cache.h>
Aaron Durbin76c37002012-10-30 09:03:43 -050026#include <pc80/mc146818rtc.h>
27#include <device/pci_def.h>
28#include "raminit.h"
29#include "pei_data.h"
30#include "haswell.h"
31
Aaron Durbin2ad1dba2013-02-07 00:51:18 -060032void save_mrc_data(struct pei_data *pei_data)
Aaron Durbin76c37002012-10-30 09:03:43 -050033{
Aaron Durbin76c37002012-10-30 09:03:43 -050034 /* Save the MRC S3 restore data to cbmem */
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +020035 store_current_mrc_cache(pei_data->mrc_output, pei_data->mrc_output_len);
Aaron Durbin76c37002012-10-30 09:03:43 -050036}
37
38static void prepare_mrc_cache(struct pei_data *pei_data)
39{
40 struct mrc_data_container *mrc_cache;
Aaron Durbin76c37002012-10-30 09:03:43 -050041
42 // preset just in case there is an error
43 pei_data->mrc_input = NULL;
44 pei_data->mrc_input_len = 0;
45
Aaron Durbin76c37002012-10-30 09:03:43 -050046 if ((mrc_cache = find_current_mrc_cache()) == NULL) {
47 /* error message printed in find_current_mrc_cache */
48 return;
49 }
50
51 pei_data->mrc_input = mrc_cache->mrc_data;
52 pei_data->mrc_input_len = mrc_cache->mrc_data_size;
53
54 printk(BIOS_DEBUG, "%s: at %p, size %x checksum %04x\n",
55 __func__, pei_data->mrc_input,
56 pei_data->mrc_input_len, mrc_cache->mrc_checksum);
57}
58
59static const char* ecc_decoder[] = {
60 "inactive",
61 "active on IO",
62 "disabled on IO",
63 "active"
64};
65
66/*
67 * Dump in the log memory controller configuration as read from the memory
68 * controller registers.
69 */
70static void report_memory_config(void)
71{
72 u32 addr_decoder_common, addr_decode_ch[2];
73 int i;
74
75 addr_decoder_common = MCHBAR32(0x5000);
76 addr_decode_ch[0] = MCHBAR32(0x5004);
77 addr_decode_ch[1] = MCHBAR32(0x5008);
78
79 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
80 (MCHBAR32(0x5e04) * 13333 * 2 + 50)/100);
81 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
82 addr_decoder_common & 3,
83 (addr_decoder_common >> 2) & 3,
84 (addr_decoder_common >> 4) & 3);
85
86 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
87 u32 ch_conf = addr_decode_ch[i];
88 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n",
89 i, ch_conf);
90 printk(BIOS_DEBUG, " ECC %s\n",
91 ecc_decoder[(ch_conf >> 24) & 3]);
92 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
93 ((ch_conf >> 22) & 1) ? "on" : "off");
94 printk(BIOS_DEBUG, " rank interleave %s\n",
95 ((ch_conf >> 21) & 1) ? "on" : "off");
Duncan Laurie8d774022013-10-22 16:32:49 -070096 printk(BIOS_DEBUG, " DIMMA %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -050097 ((ch_conf >> 0) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -070098 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -050099 ((ch_conf >> 17) & 1) ? "dual" : "single",
100 ((ch_conf >> 16) & 1) ? "" : ", selected");
Duncan Laurie8d774022013-10-22 16:32:49 -0700101 printk(BIOS_DEBUG, " DIMMB %d MB width %s %s rank%s\n",
Aaron Durbin76c37002012-10-30 09:03:43 -0500102 ((ch_conf >> 8) & 0xff) * 256,
Duncan Laurie8d774022013-10-22 16:32:49 -0700103 ((ch_conf >> 19) & 1) ? "x16" : "x8 or x32",
Aaron Durbin76c37002012-10-30 09:03:43 -0500104 ((ch_conf >> 18) & 1) ? "dual" : "single",
105 ((ch_conf >> 16) & 1) ? ", selected" : "");
106 }
107}
108
109/**
110 * Find PEI executable in coreboot filesystem and execute it.
111 *
112 * @param pei_data: configuration data for UEFI PEI reference code
113 */
114void sdram_initialize(struct pei_data *pei_data)
115{
Aaron Durbin76c37002012-10-30 09:03:43 -0500116 unsigned long entry;
117
Aaron Durbin76c37002012-10-30 09:03:43 -0500118 printk(BIOS_DEBUG, "Starting UEFI PEI System Agent\n");
119
Aaron Durbin76c37002012-10-30 09:03:43 -0500120 /*
121 * Do not pass MRC data in for recovery mode boot,
122 * Always pass it in for S3 resume.
123 */
124 if (!recovery_mode_enabled() || pei_data->boot_mode == 2)
125 prepare_mrc_cache(pei_data);
126
127 /* If MRC data is not found we cannot continue S3 resume. */
128 if (pei_data->boot_mode == 2 && !pei_data->mrc_input) {
Duncan Laurie727b5452013-08-08 16:28:41 -0700129 post_code(POST_RESUME_FAILURE);
130 printk(BIOS_DEBUG, "Giving up in sdram_initialize: "
131 "No MRC data\n");
Aaron Durbin76c37002012-10-30 09:03:43 -0500132 outb(0x6, 0xcf9);
Patrick Georgibd79c5e2014-11-28 22:35:36 +0100133 halt();
Aaron Durbin76c37002012-10-30 09:03:43 -0500134 }
135
136 /* Pass console handler in pei_data */
Kyösti Mälkki657e0be2014-02-04 19:03:57 +0200137 pei_data->tx_byte = do_putchar;
Aaron Durbin76c37002012-10-30 09:03:43 -0500138
139 /* Locate and call UEFI System Agent binary. */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500140 entry = (unsigned long)cbfs_boot_map_with_leak("mrc.bin",
141 CBFS_TYPE_MRC, NULL);
Aaron Durbin76c37002012-10-30 09:03:43 -0500142 if (entry) {
143 int rv;
144 asm volatile (
145 "call *%%ecx\n\t"
146 :"=a" (rv) : "c" (entry), "a" (pei_data));
147 if (rv) {
148 switch (rv) {
149 case -1:
150 printk(BIOS_ERR, "PEI version mismatch.\n");
151 break;
152 case -2:
153 printk(BIOS_ERR, "Invalid memory frequency.\n");
154 break;
155 default:
156 printk(BIOS_ERR, "MRC returned %x.\n", rv);
157 }
158 die("Nonzero MRC return value.\n");
159 }
160 } else {
161 die("UEFI PEI System Agent not found.\n");
162 }
163
164 /* For reference print the System Agent version
165 * after executing the UEFI PEI stage.
166 */
167 u32 version = MCHBAR32(0x5034);
168 printk(BIOS_DEBUG, "System Agent Version %d.%d.%d Build %d\n",
169 version >> 24 , (version >> 16) & 0xff,
170 (version >> 8) & 0xff, version & 0xff);
171
Aaron Durbin76c37002012-10-30 09:03:43 -0500172 report_memory_config();
Aaron Durbin76c37002012-10-30 09:03:43 -0500173}