blob: b64364bed81c6db8c57cb2c00dfaa63853c6710e [file] [log] [blame]
Angel Ponsc3f58f62020-04-05 15:46:41 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05002
Elyes HAOUAS8ebad6d2020-07-10 10:46:47 +02003#include <stdint.h>
Furquan Shaikh76cedd22020-05-02 10:24:23 -07004#include <acpi/acpi.h>
Aaron Durbin31be2c92016-12-03 22:08:20 -06005#include <assert.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05006#include <cbfs.h>
7#include <cbmem.h>
Patrick Rudolph45022ae2018-10-01 19:17:11 +02008#include <cf9_reset.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05009#include <console/console.h>
Matt DeVillier853607232020-04-23 00:46:56 -050010#include <device/dram/ddr3.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050011#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>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020020#include <security/vboot/vboot_common.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050021
Kyösti Mälkkif555a582020-01-06 19:41:42 +020022uintptr_t smbus_base(void)
23{
24 return SMBUS_BASE_ADDRESS;
25}
26
27int smbus_enable_iobar(uintptr_t base)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050028{
29 uint32_t reg;
30 const uint32_t smbus_dev = PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC);
31
32 /* SMBus I/O BAR */
Kyösti Mälkkif555a582020-01-06 19:41:42 +020033 reg = base | 2;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050034 pci_write_config32(smbus_dev, PCI_BASE_ADDRESS_4, reg);
35 /* Enable decode of I/O space. */
36 reg = pci_read_config16(smbus_dev, PCI_COMMAND);
Angel Pons89739ba2020-07-25 02:46:39 +020037 reg |= PCI_COMMAND_IO;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050038 pci_write_config16(smbus_dev, PCI_COMMAND, reg);
39 /* Enable Host Controller */
40 reg = pci_read_config8(smbus_dev, 0x40);
41 reg |= 1;
42 pci_write_config8(smbus_dev, 0x40, reg);
43
44 /* Configure pads to be used for SMBus */
45 score_select_func(PCU_SMB_CLK_PAD, 1);
46 score_select_func(PCU_SMB_DATA_PAD, 1);
Kyösti Mälkkif555a582020-01-06 19:41:42 +020047
48 return 0;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050049}
50
Aaron Durbin833ff352013-10-02 11:06:31 -050051static void ABI_X86 send_to_console(unsigned char b)
52{
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020053 do_putchar(b);
Aaron Durbin833ff352013-10-02 11:06:31 -050054}
55
Matt DeVillier853607232020-04-23 00:46:56 -050056static void populate_smbios_tables(void *dram_data, int speed, int num_channels)
57{
Angel Ponsafb3d7e2021-03-28 13:43:13 +020058 struct dimm_attr_ddr3_st dimm;
Matt DeVillier853607232020-04-23 00:46:56 -050059 enum spd_status status;
60
61 /* Decode into dimm_attr struct */
62 status = spd_decode_ddr3(&dimm, *(spd_raw_data *)dram_data);
63
64 /* Some SPDs have bad CRCs, nothing we can do about it */
65 if (status == SPD_STATUS_OK || status == SPD_STATUS_CRC_ERROR) {
66 /* Add table 17 entry for each channel */
67 for (int i = 0; i < num_channels; i++)
68 spd_add_smbios17(i, 0, speed, &dimm);
69 }
70}
71
72static void print_dram_info(void *dram_data)
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -050073{
74 const int mrc_ver_reg = 0xf0;
75 const uint32_t soc_dev = PCI_DEV(0, SOC_DEV, SOC_FUNC);
76 uint32_t reg;
77 int num_channels;
78 int speed;
79 uint32_t ch0;
80 uint32_t ch1;
81
82 reg = pci_read_config32(soc_dev, mrc_ver_reg);
83
84 printk(BIOS_INFO, "MRC v%d.%02d\n", (reg >> 8) & 0xff, reg & 0xff);
85
86 /* Number of channels enabled and DDR3 type. Determine number of
87 * channels by keying of the rank enable bits [3:0]. * */
88 ch0 = iosf_dunit_ch0_read(DRP);
89 ch1 = iosf_dunit_ch1_read(DRP);
90 num_channels = 0;
91 if (ch0 & DRP_RANK_MASK)
92 num_channels++;
93 if (ch1 & DRP_RANK_MASK)
94 num_channels++;
95
96 printk(BIOS_INFO, "%d channels of %sDDR3 @ ", num_channels,
97 (reg & (1 << 22)) ? "LP" : "");
98
99 /* DRAM frequency -- all channels run at same frequency. */
100 reg = iosf_dunit_read(DTR0);
101 switch (reg & 0x3) {
102 case 0:
103 speed = 800; break;
104 case 1:
105 speed = 1066; break;
106 case 2:
107 speed = 1333; break;
108 case 3:
109 speed = 1600; break;
110 }
111 printk(BIOS_INFO, "%dMHz\n", speed);
Matt DeVillier853607232020-04-23 00:46:56 -0500112
113 populate_smbios_tables(dram_data, speed, num_channels);
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -0500114}
115
Mate Kukrie0534932020-08-01 11:45:13 +0200116#define SPD_SIZE 256
117static u8 spd_buf[NUM_CHANNELS][SPD_SIZE];
118
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500119void raminit(struct mrc_params *mp, int prev_sleep_state)
120{
121 int ret;
122 mrc_wrapper_entry_t mrc_entry;
Mate Kukrie0534932020-08-01 11:45:13 +0200123 size_t i;
Shelley Chenad9cd682020-07-23 16:10:52 -0700124 size_t mrc_size;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500125
126 /* Fill in default entries. */
127 mp->version = MRC_PARAMS_VER;
Aaron Durbin833ff352013-10-02 11:06:31 -0500128 mp->console_out = &send_to_console;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500129 mp->prev_sleep_state = prev_sleep_state;
Julius Wernercd49cce2019-03-05 16:53:33 -0800130 mp->rmt_enabled = CONFIG(MRC_RMT);
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800131
Kyösti Mälkkic5c3e3c2021-02-17 17:33:17 +0200132 int s3resume = prev_sleep_state == ACPI_S3;
133
Shawn Nematbakhsh51d787a2014-01-16 17:52:21 -0800134 /* Default to 2GiB IO hole. */
135 if (!mp->io_hole_mb)
136 mp->io_hole_mb = 2048;
137
Shelley Chenad9cd682020-07-23 16:10:52 -0700138 /* Assume boot device is memory mapped. */
139 assert(CONFIG(BOOT_DEVICE_MEMORY_MAPPED));
140
141 mp->saved_data = mrc_cache_current_mmap_leak(MRC_TRAINING_DATA,
142 0,
143 &mrc_size);
144 if (mp->saved_data) {
145 mp->saved_data_size = mrc_size;
Kyösti Mälkkic5c3e3c2021-02-17 17:33:17 +0200146 } else if (s3resume) {
Aaron Durbin6e328932013-11-06 12:04:50 -0600147 /* If waking from S3 and no cache then. */
148 printk(BIOS_DEBUG, "No MRC cache found in S3 resume path.\n");
lilacious40cb3fe2023-06-21 23:24:14 +0200149 post_code(POSTCODE_RESUME_FAILURE);
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200150 system_reset();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500151 } else {
152 printk(BIOS_DEBUG, "No MRC cache found.\n");
153 }
154
Aaron Durbin11318892014-04-02 20:46:13 -0500155 /* Determine if mrc.bin is in the cbfs. */
Julius Werner834b3ec2020-03-04 16:52:08 -0800156 if (cbfs_map("mrc.bin", NULL) == NULL) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500157 printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
158 return;
159 }
Aaron Durbin11318892014-04-02 20:46:13 -0500160
161 /*
162 * The entry point is currently the first instruction. Handle the
163 * case of an ELF file being put in the cbfs by setting the entry
164 * to the CONFIG_MRC_BIN_ADDRESS.
165 */
166 mrc_entry = (void *)(uintptr_t)CONFIG_MRC_BIN_ADDRESS;
167
Mate Kukrie0534932020-08-01 11:45:13 +0200168 if (mp->mainboard.dram_info_location == DRAM_INFO_SPD_SMBUS) {
169 /* Workaround for broken SMBus support in the MRC */
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500170 enable_smbus();
Mate Kukrie0534932020-08-01 11:45:13 +0200171 mp->mainboard.dram_info_location = DRAM_INFO_SPD_MEM;
172 for (i = 0; i < NUM_CHANNELS; ++i) {
173 if (mp->mainboard.spd_addrs[i]) {
174 i2c_eeprom_read(mp->mainboard.spd_addrs[i],
175 0, SPD_SIZE, spd_buf[i]);
176 /* NOTE: MRC looks for Channel 1 SPD at array
177 index 1 */
178 mp->mainboard.dram_data[i] = spd_buf;
179 }
180 }
181 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500182
183 ret = mrc_entry(mp);
184
Kyösti Mälkkic5c3e3c2021-02-17 17:33:17 +0200185 int cbmem_was_initted = !cbmem_recovery(s3resume);
186 if (s3resume && !cbmem_was_initted) {
Aaron Durbin42e68562015-06-09 13:55:51 -0500187 /* Failed S3 resume, reset to come up cleanly */
Kyösti Mälkkic5c3e3c2021-02-17 17:33:17 +0200188 printk(BIOS_CRIT, "Failed to recover CBMEM in S3 resume.\n");
Patrick Rudolph45022ae2018-10-01 19:17:11 +0200189 system_reset();
Aaron Durbin6e328932013-11-06 12:04:50 -0600190 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500191
Matt DeVillier853607232020-04-23 00:46:56 -0500192 print_dram_info(mp->mainboard.dram_data[0]);
193
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500194 printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret);
195 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save,
196 mp->data_to_save_size);
197
198 if (mp->data_to_save != NULL && mp->data_to_save_size > 0)
Aaron Durbin31be2c92016-12-03 22:08:20 -0600199 mrc_cache_stash_data(MRC_TRAINING_DATA, 0, mp->data_to_save,
200 mp->data_to_save_size);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500201}