blob: f1a3c1798b23812f653226b2d70949dcfb44958d [file] [log] [blame]
Angel Ponsc3f58f62020-04-05 15:46:41 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05003
4#include <stddef.h>
Aaron Durbinf5cfaa32016-07-13 23:20:07 -05005#include <arch/acpi.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -06006#include <assert.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05007#include <cbfs.h>
8#include <cbmem.h>
Patrick Rudolph45022ae2018-10-01 19:17:11 +02009#include <cf9_reset.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050010#include <console/console.h>
11#include <device/pci_def.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020012#include <device/pci_ops.h>
Kyösti Mälkkif555a582020-01-06 19:41:42 +020013#include <device/smbus_host.h>
Aaron Durbindecd0622017-12-15 12:26:40 -070014#include <mrc_cache.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070015#include <soc/gpio.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070016#include <soc/iomap.h>
17#include <soc/iosf.h>
18#include <soc/pci_devs.h>
Julius Werner18ea2d32014-10-07 16:42:17 -070019#include <soc/romstage.h>
Aaron Durbin107b71c2014-01-09 14:35:41 -060020#include <ec/google/chromeec/ec.h>
21#include <ec/google/chromeec/ec_commands.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020022#include <security/vboot/vboot_common.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050023
Kyösti Mälkkif555a582020-01-06 19:41:42 +020024uintptr_t smbus_base(void)
25{
26 return SMBUS_BASE_ADDRESS;
27}
28
29int smbus_enable_iobar(uintptr_t base)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050030{
31 uint32_t reg;
32 const uint32_t smbus_dev = PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC);
33
34 /* SMBus I/O BAR */
Kyösti Mälkkif555a582020-01-06 19:41:42 +020035 reg = base | 2;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050036 pci_write_config32(smbus_dev, PCI_BASE_ADDRESS_4, reg);
37 /* Enable decode of I/O space. */
38 reg = pci_read_config16(smbus_dev, PCI_COMMAND);
39 reg |= 0x1;
40 pci_write_config16(smbus_dev, PCI_COMMAND, reg);
41 /* Enable Host Controller */
42 reg = pci_read_config8(smbus_dev, 0x40);
43 reg |= 1;
44 pci_write_config8(smbus_dev, 0x40, reg);
45
46 /* Configure pads to be used for SMBus */
47 score_select_func(PCU_SMB_CLK_PAD, 1);
48 score_select_func(PCU_SMB_DATA_PAD, 1);
Kyösti Mälkkif555a582020-01-06 19:41:42 +020049
50 return 0;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050051}
52
Aaron Durbin833ff352013-10-02 11:06:31 -050053static void ABI_X86 send_to_console(unsigned char b)
54{
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020055 do_putchar(b);
Aaron Durbin833ff352013-10-02 11:06:31 -050056}
57
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -050058static void print_dram_info(void)
59{
60 const int mrc_ver_reg = 0xf0;
61 const uint32_t soc_dev = PCI_DEV(0, SOC_DEV, SOC_FUNC);
62 uint32_t reg;
63 int num_channels;
64 int speed;
65 uint32_t ch0;
66 uint32_t ch1;
67
68 reg = pci_read_config32(soc_dev, mrc_ver_reg);
69
70 printk(BIOS_INFO, "MRC v%d.%02d\n", (reg >> 8) & 0xff, reg & 0xff);
71
72 /* Number of channels enabled and DDR3 type. Determine number of
73 * channels by keying of the rank enable bits [3:0]. * */
74 ch0 = iosf_dunit_ch0_read(DRP);
75 ch1 = iosf_dunit_ch1_read(DRP);
76 num_channels = 0;
77 if (ch0 & DRP_RANK_MASK)
78 num_channels++;
79 if (ch1 & DRP_RANK_MASK)
80 num_channels++;
81
82 printk(BIOS_INFO, "%d channels of %sDDR3 @ ", num_channels,
83 (reg & (1 << 22)) ? "LP" : "");
84
85 /* DRAM frequency -- all channels run at same frequency. */
86 reg = iosf_dunit_read(DTR0);
87 switch (reg & 0x3) {
88 case 0:
89 speed = 800; break;
90 case 1:
91 speed = 1066; break;
92 case 2:
93 speed = 1333; break;
94 case 3:
95 speed = 1600; break;
96 }
97 printk(BIOS_INFO, "%dMHz\n", speed);
98}
99
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500100void raminit(struct mrc_params *mp, int prev_sleep_state)
101{
102 int ret;
103 mrc_wrapper_entry_t mrc_entry;
Aaron Durbin31be2c92016-12-03 22:08:20 -0600104 struct region_device rdev;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500105
106 /* Fill in default entries. */
107 mp->version = MRC_PARAMS_VER;
Aaron Durbin833ff352013-10-02 11:06:31 -0500108 mp->console_out = &send_to_console;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500109 mp->prev_sleep_state = prev_sleep_state;
Julius Wernercd49cce2019-03-05 16:53:33 -0800110 mp->rmt_enabled = CONFIG(MRC_RMT);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800111
112 /* Default to 2GiB IO hole. */
113 if (!mp->io_hole_mb)
114 mp->io_hole_mb = 2048;
115
Furquan Shaikh0325dc62016-07-25 13:02:36 -0700116 if (vboot_recovery_mode_enabled()) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600117 printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n");
Aaron Durbin31be2c92016-12-03 22:08:20 -0600118 } else if (!mrc_cache_get_current(MRC_TRAINING_DATA, 0, &rdev)) {
119 mp->saved_data_size = region_device_sz(&rdev);
120 mp->saved_data = rdev_mmap_full(&rdev);
121 /* Assume boot device is memory mapped. */
Julius Wernercd49cce2019-03-05 16:53:33 -0800122 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500123 } else if (prev_sleep_state == ACPI_S3) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600124 /* If waking from S3 and no cache then. */
125 printk(BIOS_DEBUG, "No MRC cache found in S3 resume path.\n");
126 post_code(POST_RESUME_FAILURE);
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200127 system_reset();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500128 } else {
129 printk(BIOS_DEBUG, "No MRC cache found.\n");
130 }
131
Aaron Durbin11318892014-04-02 20:46:13 -0500132 /* Determine if mrc.bin is in the cbfs. */
Aaron Durbin899d13d2015-05-15 23:39:23 -0500133 if (cbfs_boot_map_with_leak("mrc.bin", CBFS_TYPE_MRC, NULL) == NULL) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500134 printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
135 return;
136 }
Aaron Durbin11318892014-04-02 20:46:13 -0500137
138 /*
139 * The entry point is currently the first instruction. Handle the
140 * case of an ELF file being put in the cbfs by setting the entry
141 * to the CONFIG_MRC_BIN_ADDRESS.
142 */
143 mrc_entry = (void *)(uintptr_t)CONFIG_MRC_BIN_ADDRESS;
144
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500145 if (mp->mainboard.dram_info_location == DRAM_INFO_SPD_SMBUS)
146 enable_smbus();
147
148 ret = mrc_entry(mp);
149
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -0500150 print_dram_info();
151
Aaron Durbinf5cfaa32016-07-13 23:20:07 -0500152 if (prev_sleep_state != ACPI_S3) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600153 cbmem_initialize_empty();
Aaron Durbin42e68562015-06-09 13:55:51 -0500154 } else if (cbmem_initialize()) {
Julius Wernercd49cce2019-03-05 16:53:33 -0800155 #if CONFIG(HAVE_ACPI_RESUME)
Aaron Durbin42e68562015-06-09 13:55:51 -0500156 printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n");
157 /* Failed S3 resume, reset to come up cleanly */
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200158 system_reset();
Aaron Durbin42e68562015-06-09 13:55:51 -0500159 #endif
Aaron Durbin6e328932013-11-06 12:04:50 -0600160 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500161
162 printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret);
163 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save,
164 mp->data_to_save_size);
165
166 if (mp->data_to_save != NULL && mp->data_to_save_size > 0)
Aaron Durbin31be2c92016-12-03 22:08:20 -0600167 mrc_cache_stash_data(MRC_TRAINING_DATA, 0, mp->data_to_save,
168 mp->data_to_save_size);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500169}