blob: cc055c0dbe28d5f4241d7c2043ca3495bfd94bb2 [file] [log] [blame]
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2013 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 Durbin9a7d7bc2013-09-07 00:41:48 -050014 */
15
16#include <stddef.h>
Aaron Durbinf5cfaa32016-07-13 23:20:07 -050017#include <arch/acpi.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -060018#include <assert.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050019#include <cbfs.h>
20#include <cbmem.h>
Patrick Rudolph45022ae2018-10-01 19:17:11 +020021#include <cf9_reset.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050022#include <console/console.h>
23#include <device/pci_def.h>
Patrick Georgibd79c5e2014-11-28 22:35:36 +010024#include <halt.h>
Aaron Durbindecd0622017-12-15 12:26:40 -070025#include <mrc_cache.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070026#include <soc/gpio.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070027#include <soc/iomap.h>
28#include <soc/iosf.h>
29#include <soc/pci_devs.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070030#include <soc/romstage.h>
Aaron Durbin107b71c2014-01-09 14:35:41 -060031#include <ec/google/chromeec/ec.h>
32#include <ec/google/chromeec/ec_commands.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020033#include <security/vboot/vboot_common.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050034
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050035static void enable_smbus(void)
36{
37 uint32_t reg;
38 const uint32_t smbus_dev = PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC);
39
40 /* SMBus I/O BAR */
41 reg = SMBUS_BASE_ADDRESS | 2;
42 pci_write_config32(smbus_dev, PCI_BASE_ADDRESS_4, reg);
43 /* Enable decode of I/O space. */
44 reg = pci_read_config16(smbus_dev, PCI_COMMAND);
45 reg |= 0x1;
46 pci_write_config16(smbus_dev, PCI_COMMAND, reg);
47 /* Enable Host Controller */
48 reg = pci_read_config8(smbus_dev, 0x40);
49 reg |= 1;
50 pci_write_config8(smbus_dev, 0x40, reg);
51
52 /* Configure pads to be used for SMBus */
53 score_select_func(PCU_SMB_CLK_PAD, 1);
54 score_select_func(PCU_SMB_DATA_PAD, 1);
55}
56
Aaron Durbin833ff352013-10-02 11:06:31 -050057static void ABI_X86 send_to_console(unsigned char b)
58{
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020059 do_putchar(b);
Aaron Durbin833ff352013-10-02 11:06:31 -050060}
61
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -050062static void print_dram_info(void)
63{
64 const int mrc_ver_reg = 0xf0;
65 const uint32_t soc_dev = PCI_DEV(0, SOC_DEV, SOC_FUNC);
66 uint32_t reg;
67 int num_channels;
68 int speed;
69 uint32_t ch0;
70 uint32_t ch1;
71
72 reg = pci_read_config32(soc_dev, mrc_ver_reg);
73
74 printk(BIOS_INFO, "MRC v%d.%02d\n", (reg >> 8) & 0xff, reg & 0xff);
75
76 /* Number of channels enabled and DDR3 type. Determine number of
77 * channels by keying of the rank enable bits [3:0]. * */
78 ch0 = iosf_dunit_ch0_read(DRP);
79 ch1 = iosf_dunit_ch1_read(DRP);
80 num_channels = 0;
81 if (ch0 & DRP_RANK_MASK)
82 num_channels++;
83 if (ch1 & DRP_RANK_MASK)
84 num_channels++;
85
86 printk(BIOS_INFO, "%d channels of %sDDR3 @ ", num_channels,
87 (reg & (1 << 22)) ? "LP" : "");
88
89 /* DRAM frequency -- all channels run at same frequency. */
90 reg = iosf_dunit_read(DTR0);
91 switch (reg & 0x3) {
92 case 0:
93 speed = 800; break;
94 case 1:
95 speed = 1066; break;
96 case 2:
97 speed = 1333; break;
98 case 3:
99 speed = 1600; break;
100 }
101 printk(BIOS_INFO, "%dMHz\n", speed);
102}
103
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500104void raminit(struct mrc_params *mp, int prev_sleep_state)
105{
106 int ret;
107 mrc_wrapper_entry_t mrc_entry;
Aaron Durbin31be2c92016-12-03 22:08:20 -0600108 struct region_device rdev;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500109
110 /* Fill in default entries. */
111 mp->version = MRC_PARAMS_VER;
Aaron Durbin833ff352013-10-02 11:06:31 -0500112 mp->console_out = &send_to_console;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500113 mp->prev_sleep_state = prev_sleep_state;
Patrick Georgi5b33dc12014-05-07 20:20:10 +0200114 mp->rmt_enabled = IS_ENABLED(CONFIG_MRC_RMT);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800115
116 /* Default to 2GiB IO hole. */
117 if (!mp->io_hole_mb)
118 mp->io_hole_mb = 2048;
119
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700120 if (vboot_recovery_mode_enabled()) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600121 printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n");
Aaron Durbin31be2c92016-12-03 22:08:20 -0600122 } else if (!mrc_cache_get_current(MRC_TRAINING_DATA, 0, &rdev)) {
123 mp->saved_data_size = region_device_sz(&rdev);
124 mp->saved_data = rdev_mmap_full(&rdev);
125 /* Assume boot device is memory mapped. */
126 assert(IS_ENABLED(CONFIG_BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500127 } else if (prev_sleep_state == ACPI_S3) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600128 /* If waking from S3 and no cache then. */
129 printk(BIOS_DEBUG, "No MRC cache found in S3 resume path.\n");
130 post_code(POST_RESUME_FAILURE);
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200131 system_reset();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500132 } else {
133 printk(BIOS_DEBUG, "No MRC cache found.\n");
134 }
135
Aaron Durbin11318892014-04-02 20:46:13 -0500136 /* Determine if mrc.bin is in the cbfs. */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500137 if (cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL) == NULL) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500138 printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
139 return;
140 }
Aaron Durbin11318892014-04-02 20:46:13 -0500141
142 /*
143 * The entry point is currently the first instruction. Handle the
144 * case of an ELF file being put in the cbfs by setting the entry
145 * to the CONFIG_MRC_BIN_ADDRESS.
146 */
147 mrc_entry = (void *)(uintptr_t)CONFIG_MRC_BIN_ADDRESS;
148
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500149 if (mp->mainboard.dram_info_location == DRAM_INFO_SPD_SMBUS)
150 enable_smbus();
151
152 ret = mrc_entry(mp);
153
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -0500154 print_dram_info();
155
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500156 if (prev_sleep_state != ACPI_S3) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600157 cbmem_initialize_empty();
Aaron Durbin42e68562015-06-09 13:55:51 -0500158 } else if (cbmem_initialize()) {
Martin Rothe6ff1592017-06-24 21:34:29 -0600159 #if IS_ENABLED(CONFIG_HAVE_ACPI_RESUME)
Aaron Durbin42e68562015-06-09 13:55:51 -0500160 printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n");
161 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200162 system_reset();
Aaron Durbin42e68562015-06-09 13:55:51 -0500163 #endif
Aaron Durbin6e328932013-11-06 12:04:50 -0600164 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500165
166 printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret);
167 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save,
168 mp->data_to_save_size);
169
170 if (mp->data_to_save != NULL && mp->data_to_save_size > 0)
Aaron Durbin31be2c92016-12-03 22:08:20 -0600171 mrc_cache_stash_data(MRC_TRAINING_DATA, 0, mp->data_to_save,
172 mp->data_to_save_size);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500173}