blob: a43cdea102498528f53fa2162cbc7dfd7850e5fc [file] [log] [blame]
Angel Ponse67ab182020-04-04 18:51:11 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Huayang Duanc2ef1022018-09-26 14:24:02 +08002
Huayang Duan4d15d2f2018-09-26 21:23:53 +08003#include <assert.h>
Joel Kitching9a292282020-03-06 13:44:50 +08004#include <bootmode.h>
Huayang Duan078332e2019-08-27 13:36:14 +08005#include <cbfs.h>
Huayang Duan4d15d2f2018-09-26 21:23:53 +08006#include <console/console.h>
Yu-Ping Wue67dce02019-10-14 16:56:50 +08007#include <ip_checksum.h>
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +08008#include <security/vboot/vboot_common.h>
Huayang Duan078332e2019-08-27 13:36:14 +08009#include <soc/dramc_param.h>
Huayang Duan4d15d2f2018-09-26 21:23:53 +080010#include <soc/dramc_pi_api.h>
Huayang Duanc2ef1022018-09-26 14:24:02 +080011#include <soc/emi.h>
Huayang Duane6ac20b2019-12-24 16:35:13 +080012#include <soc/mt6358.h>
Huayang Duan4d15d2f2018-09-26 21:23:53 +080013#include <symbols.h>
Huayang Duanc2ef1022018-09-26 14:24:02 +080014
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +080015static int mt_mem_test(void)
Huayang Duanc2ef1022018-09-26 14:24:02 +080016{
Huayang Duan4d15d2f2018-09-26 21:23:53 +080017 u64 rank_size[RANK_MAX];
18
Julius Wernercd49cce2019-03-05 16:53:33 -080019 if (CONFIG(MEMORY_TEST)) {
Huayang Duan4d15d2f2018-09-26 21:23:53 +080020 size_t r;
21 u8 *addr = _dram;
22
23 dramc_get_rank_size(rank_size);
24
25 for (r = RANK_0; r < RANK_MAX; r++) {
26 int i;
27
28 if (rank_size[r] == 0)
29 break;
30
31 i = complex_mem_test(addr, 0x2000);
32
33 printk(BIOS_DEBUG, "[MEM] complex R/W mem test %s : %d\n",
34 (i == 0) ? "pass" : "fail", i);
35
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +080036 if (i != 0) {
Yu-Ping Wu31ec0c42019-10-09 16:11:47 +080037 printk(BIOS_ERR, "DRAM memory test failed\n");
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +080038 return -1;
39 }
Huayang Duan4d15d2f2018-09-26 21:23:53 +080040
41 addr += rank_size[r];
42 }
43 }
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +080044
45 return 0;
Huayang Duanc2ef1022018-09-26 14:24:02 +080046}
Huayang Duan078332e2019-08-27 13:36:14 +080047
48static void dump_param_header(const struct dramc_param *dparam)
49{
50 const struct dramc_param_header *header = &dparam->header;
51
52 printk(BIOS_DEBUG, "header.status = %#x\n", header->status);
53 printk(BIOS_DEBUG, "header.magic = %#x (expected: %#x)\n",
54 header->magic, DRAMC_PARAM_HEADER_MAGIC);
55 printk(BIOS_DEBUG, "header.version = %#x (expected: %#x)\n",
56 header->version, DRAMC_PARAM_HEADER_VERSION);
57 printk(BIOS_DEBUG, "header.size = %#x (expected: %#lx)\n",
58 header->size, sizeof(*dparam));
59 printk(BIOS_DEBUG, "header.config = %#x\n", header->config);
60 printk(BIOS_DEBUG, "header.flags = %#x\n", header->flags);
61 printk(BIOS_DEBUG, "header.checksum = %#x\n", header->checksum);
62}
63
Yu-Ping Wue67dce02019-10-14 16:56:50 +080064static u32 compute_checksum(const struct dramc_param *dparam)
65{
66 return (u32)compute_ip_checksum(dparam->freq_params,
67 sizeof(dparam->freq_params));
68}
69
Huayang Duan078332e2019-08-27 13:36:14 +080070static int dram_run_fast_calibration(const struct dramc_param *dparam,
71 u16 config)
72{
73 if (!is_valid_dramc_param(dparam)) {
74 printk(BIOS_WARNING,
75 "Invalid DRAM calibration data from flash\n");
76 dump_param_header(dparam);
77 return -1;
78 }
79
Yu-Ping Wu31ec0c42019-10-09 16:11:47 +080080 if (dparam->header.config != config) {
Huayang Duan078332e2019-08-27 13:36:14 +080081 printk(BIOS_WARNING,
82 "Incompatible config for calibration data from flash "
83 "(expected: %#x, saved: %#x)\n",
84 config, dparam->header.config);
85 return -1;
86 }
87
Yu-Ping Wue67dce02019-10-14 16:56:50 +080088 const u32 checksum = compute_checksum(dparam);
89 if (dparam->header.checksum != checksum) {
90 printk(BIOS_ERR,
91 "Invalid DRAM calibration checksum from flash "
92 "(expected: %#x, saved: %#x)\n",
93 checksum, dparam->header.checksum);
94 return -1;
95 }
96
Huayang Duan078332e2019-08-27 13:36:14 +080097 return 0;
98}
99
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800100static int dram_run_full_calibration(struct dramc_param *dparam,
101 u32 ddr_geometry, u16 config)
Huayang Duan078332e2019-08-27 13:36:14 +0800102{
103 initialize_dramc_param(dparam, config);
104
105 /* Load and run the provided blob for full-calibration if available */
106 struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram");
107
Huayang Duan078332e2019-08-27 13:36:14 +0800108 if (cbfs_prog_stage_load(&dram))
109 return -2;
110
Hung-Te Linbeeab4e2019-10-15 17:49:24 +0800111 dparam->do_putc = do_putchar;
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800112 dparam->freq_params[0].ddr_geometry = ddr_geometry;
113 printk(BIOS_INFO, "ddr_geometry: %d, config: %#x\n", ddr_geometry, config);
Huayang Duan078332e2019-08-27 13:36:14 +0800114 prog_set_entry(&dram, prog_entry(&dram), dparam);
115 prog_run(&dram);
116
117 if (dparam->header.status != DRAMC_SUCCESS) {
118 printk(BIOS_ERR, "Full calibration failed: status = %d\n",
119 dparam->header.status);
120 return -3;
121 }
122
123 if (!(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
124 printk(BIOS_ERR,
125 "Full calibration executed without saving parameters. "
126 "Please ensure the blob is built properly.\n");
127 return -4;
128 }
129
130 return 0;
131}
132
133static void set_source_to_flash(struct sdram_params *freq_params)
134{
135 for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
136 shuffle++)
137 freq_params[shuffle].source = DRAMC_PARAM_SOURCE_FLASH;
138}
139
140static void init_sdram_params(struct sdram_params *dst,
141 const struct sdram_params *src)
142{
143 for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
144 shuffle++)
145 memcpy(&dst[shuffle], src, sizeof(*dst));
146}
147
Huayang Duane6ac20b2019-12-24 16:35:13 +0800148static void mt_mem_init_run(struct dramc_param_ops *dparam_ops)
Huayang Duan078332e2019-08-27 13:36:14 +0800149{
150 struct dramc_param *dparam = dparam_ops->param;
Huayang Duan078332e2019-08-27 13:36:14 +0800151
152 u16 config = 0;
153 if (CONFIG(MT8183_DRAM_EMCP))
154 config |= DRAMC_CONFIG_EMCP;
155
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800156 const bool recovery_mode = vboot_recovery_mode_enabled();
157
Yu-Ping Wu02d90712019-10-29 16:20:35 +0800158 /* DRAM DVFS is disabled in recovery mode */
159 if (CONFIG(MT8183_DRAM_DVFS) && !recovery_mode)
160 config |= DRAMC_CONFIG_DVFS;
161
Huayang Duan078332e2019-08-27 13:36:14 +0800162 /* Load calibration params from flash and run fast calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800163 if (recovery_mode) {
164 printk(BIOS_WARNING, "Skip loading cached calibration data\n");
Julius Werner00961672020-04-22 22:31:36 +0000165 if (get_recovery_mode_retrain_switch()) {
Yu-Ping Wu46009ea2019-10-17 13:38:32 +0800166 printk(BIOS_WARNING, "Retrain memory in next boot\n");
167 /* Use 0xFF as erased flash data. */
168 memset(dparam, 0xff, sizeof(*dparam));
169 dparam_ops->write_to_flash(dparam);
170 }
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800171 } else if (dparam_ops->read_from_flash(dparam)) {
172 printk(BIOS_INFO, "DRAM-K: Fast Calibration\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800173 if (dram_run_fast_calibration(dparam, config) == 0) {
174 printk(BIOS_INFO,
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800175 "Calibration params loaded from flash\n");
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +0800176 if (mt_set_emi(dparam) == 0 && mt_mem_test() == 0)
177 return;
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800178 } else {
179 printk(BIOS_ERR,
180 "Failed to apply cached calibration data\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800181 }
182 } else {
183 printk(BIOS_WARNING,
184 "Failed to read calibration data from flash\n");
185 }
186
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800187 const struct sdram_params *sdram_cfg = get_sdram_config();
188
Huayang Duan078332e2019-08-27 13:36:14 +0800189 /* Run full calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800190 printk(BIOS_INFO, "DRAM-K: Full Calibration\n");
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800191 int err = dram_run_full_calibration(dparam, sdram_cfg->ddr_geometry, config);
Huayang Duan078332e2019-08-27 13:36:14 +0800192 if (err == 0) {
193 printk(BIOS_INFO, "Successfully loaded DRAM blobs and "
194 "ran DRAM calibration\n");
Huayang Duane6ac20b2019-12-24 16:35:13 +0800195
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800196 /*
197 * In recovery mode the system boots in RO but the flash params
198 * should be calibrated for RW so we can't mix them up.
199 */
200 if (!recovery_mode) {
201 set_source_to_flash(dparam->freq_params);
202 dparam->header.checksum = compute_checksum(dparam);
203 dparam_ops->write_to_flash(dparam);
204 printk(BIOS_DEBUG, "Calibration params saved to flash: "
205 "version=%#x, size=%#x\n",
206 dparam->header.version, dparam->header.size);
207 }
Huayang Duan078332e2019-08-27 13:36:14 +0800208 return;
209 }
210
211 printk(BIOS_ERR, "Failed to do full calibration (%d), "
212 "falling back to load default sdram param\n", err);
213
214 /* Init params from sdram configs and run partial calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800215 printk(BIOS_INFO, "DRAM-K: Partial Calibration\n");
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800216 init_sdram_params(dparam->freq_params, sdram_cfg);
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +0800217 if (mt_set_emi(dparam) != 0)
218 die("Set emi failed with params from sdram config\n");
219 if (mt_mem_test() != 0)
220 die("Memory test failed with params from sdram config\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800221}
Huayang Duane6ac20b2019-12-24 16:35:13 +0800222
223void mt_mem_init(struct dramc_param_ops *dparam_ops)
224{
225 mt_mem_init_run(dparam_ops);
226
227 /* After DRAM calibration, restore vcore voltage to default setting */
228 pmic_set_vcore_vol(800000);
229}