blob: 352b86ffa2cc9599dc4125f59b88db12fae2446c [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.
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 <stddef.h>
Aaron Durbin6e328932013-11-06 12:04:50 -060021#include <arch/hlt.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050022#include <arch/io.h>
Aaron Durbin6e328932013-11-06 12:04:50 -060023#include <bootmode.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050024#include <cbfs.h>
25#include <cbmem.h>
26#include <console/console.h>
27#include <device/pci_def.h>
28#include <baytrail/gpio.h>
29#include <baytrail/mrc_cache.h>
30#include <baytrail/iomap.h>
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -050031#include <baytrail/iosf.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050032#include <baytrail/pci_devs.h>
Aaron Durbin6e328932013-11-06 12:04:50 -060033#include <baytrail/reset.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050034#include <baytrail/romstage.h>
35
Aaron Durbin6e328932013-11-06 12:04:50 -060036static void reset_system(void)
37{
38 warm_reset();
39 while(1) { hlt(); }
40}
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050041
42static void enable_smbus(void)
43{
44 uint32_t reg;
45 const uint32_t smbus_dev = PCI_DEV(0, SMBUS_DEV, SMBUS_FUNC);
46
47 /* SMBus I/O BAR */
48 reg = SMBUS_BASE_ADDRESS | 2;
49 pci_write_config32(smbus_dev, PCI_BASE_ADDRESS_4, reg);
50 /* Enable decode of I/O space. */
51 reg = pci_read_config16(smbus_dev, PCI_COMMAND);
52 reg |= 0x1;
53 pci_write_config16(smbus_dev, PCI_COMMAND, reg);
54 /* Enable Host Controller */
55 reg = pci_read_config8(smbus_dev, 0x40);
56 reg |= 1;
57 pci_write_config8(smbus_dev, 0x40, reg);
58
59 /* Configure pads to be used for SMBus */
60 score_select_func(PCU_SMB_CLK_PAD, 1);
61 score_select_func(PCU_SMB_DATA_PAD, 1);
62}
63
Aaron Durbin833ff352013-10-02 11:06:31 -050064static void ABI_X86 send_to_console(unsigned char b)
65{
Kyösti Mälkki657e0be2014-02-04 19:03:57 +020066 do_putchar(b);
Aaron Durbin833ff352013-10-02 11:06:31 -050067}
68
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -050069static void print_dram_info(void)
70{
71 const int mrc_ver_reg = 0xf0;
72 const uint32_t soc_dev = PCI_DEV(0, SOC_DEV, SOC_FUNC);
73 uint32_t reg;
74 int num_channels;
75 int speed;
76 uint32_t ch0;
77 uint32_t ch1;
78
79 reg = pci_read_config32(soc_dev, mrc_ver_reg);
80
81 printk(BIOS_INFO, "MRC v%d.%02d\n", (reg >> 8) & 0xff, reg & 0xff);
82
83 /* Number of channels enabled and DDR3 type. Determine number of
84 * channels by keying of the rank enable bits [3:0]. * */
85 ch0 = iosf_dunit_ch0_read(DRP);
86 ch1 = iosf_dunit_ch1_read(DRP);
87 num_channels = 0;
88 if (ch0 & DRP_RANK_MASK)
89 num_channels++;
90 if (ch1 & DRP_RANK_MASK)
91 num_channels++;
92
93 printk(BIOS_INFO, "%d channels of %sDDR3 @ ", num_channels,
94 (reg & (1 << 22)) ? "LP" : "");
95
96 /* DRAM frequency -- all channels run at same frequency. */
97 reg = iosf_dunit_read(DTR0);
98 switch (reg & 0x3) {
99 case 0:
100 speed = 800; break;
101 case 1:
102 speed = 1066; break;
103 case 2:
104 speed = 1333; break;
105 case 3:
106 speed = 1600; break;
107 }
108 printk(BIOS_INFO, "%dMHz\n", speed);
109}
110
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500111void raminit(struct mrc_params *mp, int prev_sleep_state)
112{
113 int ret;
114 mrc_wrapper_entry_t mrc_entry;
115 const struct mrc_saved_data *cache;
116
117 /* Fill in default entries. */
118 mp->version = MRC_PARAMS_VER;
Aaron Durbin833ff352013-10-02 11:06:31 -0500119 mp->console_out = &send_to_console;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500120 mp->prev_sleep_state = prev_sleep_state;
Patrick Georgi5b33dc12014-05-07 20:20:10 +0200121 mp->rmt_enabled = IS_ENABLED(CONFIG_MRC_RMT);
Aaron Durbin6e328932013-11-06 12:04:50 -0600122 if (recovery_mode_enabled()) {
123 printk(BIOS_DEBUG, "Recovery mode: not using MRC cache.\n");
124 } else if (!mrc_cache_get_current(&cache)) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500125 mp->saved_data_size = cache->size;
126 mp->saved_data = &cache->data[0];
Aaron Durbin6e328932013-11-06 12:04:50 -0600127 } else if (prev_sleep_state == 3) {
128 /* 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);
131 reset_system();
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500132 } else {
133 printk(BIOS_DEBUG, "No MRC cache found.\n");
134 }
135
136 mrc_entry = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, "mrc.bin", 0xab,
137 NULL);
138
139 if (mrc_entry == NULL) {
140 printk(BIOS_DEBUG, "Couldn't find mrc.bin\n");
141 return;
142 }
143 if (mp->mainboard.dram_info_location == DRAM_INFO_SPD_SMBUS)
144 enable_smbus();
145
146 ret = mrc_entry(mp);
147
Aaron Durbin3ccb3ce2013-10-11 00:26:04 -0500148 print_dram_info();
149
Aaron Durbin6e328932013-11-06 12:04:50 -0600150 if (prev_sleep_state != 3) {
151 cbmem_initialize_empty();
152 } else if (cbmem_initialize()) {
153 #if CONFIG_HAVE_ACPI_RESUME
154 printk(BIOS_DEBUG, "Failed to recover CBMEM in S3 resume.\n");
155 /* Failed S3 resume, reset to come up cleanly */
156 reset_system();
157 #endif
158 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500159
160 printk(BIOS_DEBUG, "MRC Wrapper returned %d\n", ret);
161 printk(BIOS_DEBUG, "MRC data at %p %d bytes\n", mp->data_to_save,
162 mp->data_to_save_size);
163
164 if (mp->data_to_save != NULL && mp->data_to_save_size > 0)
165 mrc_cache_stash_data(mp->data_to_save, mp->data_to_save_size);
166}