blob: ca6f7a185069e08e0e11026c180ed2f1a9ddd8f6 [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>
Julius Wernerde371092024-01-30 16:51:05 -08006#include <commonlib/bsd/ipchksum.h>
Huayang Duan4d15d2f2018-09-26 21:23:53 +08007#include <console/console.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{
Julius Wernerde371092024-01-30 16:51:05 -080066 return (u32)ipchksum(dparam->freq_params, sizeof(dparam->freq_params));
Yu-Ping Wue67dce02019-10-14 16:56:50 +080067}
68
Huayang Duan078332e2019-08-27 13:36:14 +080069static int dram_run_fast_calibration(const struct dramc_param *dparam,
70 u16 config)
71{
72 if (!is_valid_dramc_param(dparam)) {
73 printk(BIOS_WARNING,
74 "Invalid DRAM calibration data from flash\n");
75 dump_param_header(dparam);
76 return -1;
77 }
78
Yu-Ping Wu31ec0c42019-10-09 16:11:47 +080079 if (dparam->header.config != config) {
Huayang Duan078332e2019-08-27 13:36:14 +080080 printk(BIOS_WARNING,
81 "Incompatible config for calibration data from flash "
82 "(expected: %#x, saved: %#x)\n",
83 config, dparam->header.config);
84 return -1;
85 }
86
Yu-Ping Wue67dce02019-10-14 16:56:50 +080087 const u32 checksum = compute_checksum(dparam);
88 if (dparam->header.checksum != checksum) {
89 printk(BIOS_ERR,
90 "Invalid DRAM calibration checksum from flash "
91 "(expected: %#x, saved: %#x)\n",
92 checksum, dparam->header.checksum);
93 return -1;
94 }
95
Huayang Duan078332e2019-08-27 13:36:14 +080096 return 0;
97}
98
Huayang Duan6e57b1c2020-07-23 13:53:39 +080099static int dram_run_full_calibration(struct dramc_param *dparam,
100 u32 ddr_geometry, u16 config)
Huayang Duan078332e2019-08-27 13:36:14 +0800101{
102 initialize_dramc_param(dparam, config);
103
104 /* Load and run the provided blob for full-calibration if available */
105 struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram");
106
Huayang Duan078332e2019-08-27 13:36:14 +0800107 if (cbfs_prog_stage_load(&dram))
108 return -2;
109
Hung-Te Linbeeab4e2019-10-15 17:49:24 +0800110 dparam->do_putc = do_putchar;
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800111 dparam->freq_params[0].ddr_geometry = ddr_geometry;
112 printk(BIOS_INFO, "ddr_geometry: %d, config: %#x\n", ddr_geometry, config);
Huayang Duan078332e2019-08-27 13:36:14 +0800113 prog_set_entry(&dram, prog_entry(&dram), dparam);
114 prog_run(&dram);
115
116 if (dparam->header.status != DRAMC_SUCCESS) {
117 printk(BIOS_ERR, "Full calibration failed: status = %d\n",
118 dparam->header.status);
119 return -3;
120 }
121
122 if (!(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
123 printk(BIOS_ERR,
124 "Full calibration executed without saving parameters. "
125 "Please ensure the blob is built properly.\n");
126 return -4;
127 }
128
129 return 0;
130}
131
132static void set_source_to_flash(struct sdram_params *freq_params)
133{
134 for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
135 shuffle++)
136 freq_params[shuffle].source = DRAMC_PARAM_SOURCE_FLASH;
137}
138
139static void init_sdram_params(struct sdram_params *dst,
140 const struct sdram_params *src)
141{
142 for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
143 shuffle++)
144 memcpy(&dst[shuffle], src, sizeof(*dst));
145}
146
Huayang Duane6ac20b2019-12-24 16:35:13 +0800147static void mt_mem_init_run(struct dramc_param_ops *dparam_ops)
Huayang Duan078332e2019-08-27 13:36:14 +0800148{
149 struct dramc_param *dparam = dparam_ops->param;
Huayang Duan078332e2019-08-27 13:36:14 +0800150
151 u16 config = 0;
152 if (CONFIG(MT8183_DRAM_EMCP))
153 config |= DRAMC_CONFIG_EMCP;
154
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800155 const bool recovery_mode = vboot_recovery_mode_enabled();
156
Yu-Ping Wu02d90712019-10-29 16:20:35 +0800157 /* DRAM DVFS is disabled in recovery mode */
158 if (CONFIG(MT8183_DRAM_DVFS) && !recovery_mode)
159 config |= DRAMC_CONFIG_DVFS;
160
Huayang Duan078332e2019-08-27 13:36:14 +0800161 /* Load calibration params from flash and run fast calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800162 if (recovery_mode) {
163 printk(BIOS_WARNING, "Skip loading cached calibration data\n");
Julius Werner00961672020-04-22 22:31:36 +0000164 if (get_recovery_mode_retrain_switch()) {
Yu-Ping Wu46009ea2019-10-17 13:38:32 +0800165 printk(BIOS_WARNING, "Retrain memory in next boot\n");
166 /* Use 0xFF as erased flash data. */
167 memset(dparam, 0xff, sizeof(*dparam));
168 dparam_ops->write_to_flash(dparam);
169 }
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800170 } else if (dparam_ops->read_from_flash(dparam)) {
171 printk(BIOS_INFO, "DRAM-K: Fast Calibration\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800172 if (dram_run_fast_calibration(dparam, config) == 0) {
173 printk(BIOS_INFO,
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800174 "Calibration params loaded from flash\n");
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +0800175 if (mt_set_emi(dparam) == 0 && mt_mem_test() == 0)
176 return;
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800177 } else {
178 printk(BIOS_ERR,
179 "Failed to apply cached calibration data\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800180 }
181 } else {
182 printk(BIOS_WARNING,
183 "Failed to read calibration data from flash\n");
184 }
185
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800186 const struct sdram_params *sdram_cfg = get_sdram_config();
187
Huayang Duan078332e2019-08-27 13:36:14 +0800188 /* Run full calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800189 printk(BIOS_INFO, "DRAM-K: Full Calibration\n");
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800190 int err = dram_run_full_calibration(dparam, sdram_cfg->ddr_geometry, config);
Huayang Duan078332e2019-08-27 13:36:14 +0800191 if (err == 0) {
192 printk(BIOS_INFO, "Successfully loaded DRAM blobs and "
193 "ran DRAM calibration\n");
Huayang Duane6ac20b2019-12-24 16:35:13 +0800194
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800195 /*
196 * In recovery mode the system boots in RO but the flash params
197 * should be calibrated for RW so we can't mix them up.
198 */
199 if (!recovery_mode) {
200 set_source_to_flash(dparam->freq_params);
201 dparam->header.checksum = compute_checksum(dparam);
202 dparam_ops->write_to_flash(dparam);
203 printk(BIOS_DEBUG, "Calibration params saved to flash: "
204 "version=%#x, size=%#x\n",
205 dparam->header.version, dparam->header.size);
206 }
Huayang Duan078332e2019-08-27 13:36:14 +0800207 return;
208 }
209
210 printk(BIOS_ERR, "Failed to do full calibration (%d), "
211 "falling back to load default sdram param\n", err);
212
213 /* Init params from sdram configs and run partial calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800214 printk(BIOS_INFO, "DRAM-K: Partial Calibration\n");
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800215 init_sdram_params(dparam->freq_params, sdram_cfg);
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +0800216 if (mt_set_emi(dparam) != 0)
217 die("Set emi failed with params from sdram config\n");
218 if (mt_mem_test() != 0)
219 die("Memory test failed with params from sdram config\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800220}
Huayang Duane6ac20b2019-12-24 16:35:13 +0800221
222void mt_mem_init(struct dramc_param_ops *dparam_ops)
223{
224 mt_mem_init_run(dparam_ops);
225
226 /* After DRAM calibration, restore vcore voltage to default setting */
227 pmic_set_vcore_vol(800000);
228}