blob: 6b80a43a8c9899709830bf75d4bd1b44f28c5e66 [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
108 if (prog_locate(&dram))
109 return -1;
110
111 if (cbfs_prog_stage_load(&dram))
112 return -2;
113
Hung-Te Linbeeab4e2019-10-15 17:49:24 +0800114 dparam->do_putc = do_putchar;
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800115 dparam->freq_params[0].ddr_geometry = ddr_geometry;
116 printk(BIOS_INFO, "ddr_geometry: %d, config: %#x\n", ddr_geometry, config);
Huayang Duan078332e2019-08-27 13:36:14 +0800117 prog_set_entry(&dram, prog_entry(&dram), dparam);
118 prog_run(&dram);
119
120 if (dparam->header.status != DRAMC_SUCCESS) {
121 printk(BIOS_ERR, "Full calibration failed: status = %d\n",
122 dparam->header.status);
123 return -3;
124 }
125
126 if (!(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
127 printk(BIOS_ERR,
128 "Full calibration executed without saving parameters. "
129 "Please ensure the blob is built properly.\n");
130 return -4;
131 }
132
133 return 0;
134}
135
136static void set_source_to_flash(struct sdram_params *freq_params)
137{
138 for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
139 shuffle++)
140 freq_params[shuffle].source = DRAMC_PARAM_SOURCE_FLASH;
141}
142
143static void init_sdram_params(struct sdram_params *dst,
144 const struct sdram_params *src)
145{
146 for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
147 shuffle++)
148 memcpy(&dst[shuffle], src, sizeof(*dst));
149}
150
Huayang Duane6ac20b2019-12-24 16:35:13 +0800151static void mt_mem_init_run(struct dramc_param_ops *dparam_ops)
Huayang Duan078332e2019-08-27 13:36:14 +0800152{
153 struct dramc_param *dparam = dparam_ops->param;
Huayang Duan078332e2019-08-27 13:36:14 +0800154
155 u16 config = 0;
156 if (CONFIG(MT8183_DRAM_EMCP))
157 config |= DRAMC_CONFIG_EMCP;
158
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800159 const bool recovery_mode = vboot_recovery_mode_enabled();
160
Yu-Ping Wu02d90712019-10-29 16:20:35 +0800161 /* DRAM DVFS is disabled in recovery mode */
162 if (CONFIG(MT8183_DRAM_DVFS) && !recovery_mode)
163 config |= DRAMC_CONFIG_DVFS;
164
Huayang Duan078332e2019-08-27 13:36:14 +0800165 /* Load calibration params from flash and run fast calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800166 if (recovery_mode) {
167 printk(BIOS_WARNING, "Skip loading cached calibration data\n");
Julius Werner00961672020-04-22 22:31:36 +0000168 if (get_recovery_mode_retrain_switch()) {
Yu-Ping Wu46009ea2019-10-17 13:38:32 +0800169 printk(BIOS_WARNING, "Retrain memory in next boot\n");
170 /* Use 0xFF as erased flash data. */
171 memset(dparam, 0xff, sizeof(*dparam));
172 dparam_ops->write_to_flash(dparam);
173 }
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800174 } else if (dparam_ops->read_from_flash(dparam)) {
175 printk(BIOS_INFO, "DRAM-K: Fast Calibration\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800176 if (dram_run_fast_calibration(dparam, config) == 0) {
177 printk(BIOS_INFO,
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800178 "Calibration params loaded from flash\n");
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +0800179 if (mt_set_emi(dparam) == 0 && mt_mem_test() == 0)
180 return;
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800181 } else {
182 printk(BIOS_ERR,
183 "Failed to apply cached calibration data\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800184 }
185 } else {
186 printk(BIOS_WARNING,
187 "Failed to read calibration data from flash\n");
188 }
189
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800190 const struct sdram_params *sdram_cfg = get_sdram_config();
191
Huayang Duan078332e2019-08-27 13:36:14 +0800192 /* Run full calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800193 printk(BIOS_INFO, "DRAM-K: Full Calibration\n");
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800194 int err = dram_run_full_calibration(dparam, sdram_cfg->ddr_geometry, config);
Huayang Duan078332e2019-08-27 13:36:14 +0800195 if (err == 0) {
196 printk(BIOS_INFO, "Successfully loaded DRAM blobs and "
197 "ran DRAM calibration\n");
Huayang Duane6ac20b2019-12-24 16:35:13 +0800198
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800199 /*
200 * In recovery mode the system boots in RO but the flash params
201 * should be calibrated for RW so we can't mix them up.
202 */
203 if (!recovery_mode) {
204 set_source_to_flash(dparam->freq_params);
205 dparam->header.checksum = compute_checksum(dparam);
206 dparam_ops->write_to_flash(dparam);
207 printk(BIOS_DEBUG, "Calibration params saved to flash: "
208 "version=%#x, size=%#x\n",
209 dparam->header.version, dparam->header.size);
210 }
Huayang Duan078332e2019-08-27 13:36:14 +0800211 return;
212 }
213
214 printk(BIOS_ERR, "Failed to do full calibration (%d), "
215 "falling back to load default sdram param\n", err);
216
217 /* Init params from sdram configs and run partial calibration */
Yu-Ping Wu998a3cc2019-10-16 18:29:50 +0800218 printk(BIOS_INFO, "DRAM-K: Partial Calibration\n");
Huayang Duan6e57b1c2020-07-23 13:53:39 +0800219 init_sdram_params(dparam->freq_params, sdram_cfg);
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +0800220 if (mt_set_emi(dparam) != 0)
221 die("Set emi failed with params from sdram config\n");
222 if (mt_mem_test() != 0)
223 die("Memory test failed with params from sdram config\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800224}
Huayang Duane6ac20b2019-12-24 16:35:13 +0800225
226void mt_mem_init(struct dramc_param_ops *dparam_ops)
227{
228 mt_mem_init_run(dparam_ops);
229
230 /* After DRAM calibration, restore vcore voltage to default setting */
231 pmic_set_vcore_vol(800000);
232}