blob: 3ca36117e9eed20145377e98c11107694a9e3807 [file] [log] [blame]
Huayang Duanc90a9e62020-06-22 19:52:45 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <assert.h>
4#include <bootmode.h>
5#include <cbfs.h>
6#include <console/console.h>
Xi Chen5c7a9232022-01-04 19:00:44 +08007#include <soc/dramc_common.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +08008#include <ip_checksum.h>
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +08009#include <mrc_cache.h>
10#include <soc/dramc_param.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080011#include <soc/emi.h>
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080012#include <soc/mmu_operations.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080013#include <symbols.h>
Huayang Duan68e597d2020-06-22 19:59:40 +080014#include <timer.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080015
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080016/* This must be defined in chromeos.fmd in same name and size. */
17#define CALIBRATION_REGION "RW_MRC_CACHE"
18#define CALIBRATION_REGION_SIZE 0x2000
19
20_Static_assert(sizeof(struct dramc_param) <= CALIBRATION_REGION_SIZE,
21 "sizeof(struct dramc_param) exceeds " CALIBRATION_REGION);
22
Xi Chene8c681c2021-03-03 17:58:07 +080023const char *get_dram_geometry_str(u32 ddr_geometry);
24const char *get_dram_type_str(u32 ddr_type);
25
Huayang Duanc90a9e62020-06-22 19:52:45 +080026static int mt_mem_test(const struct dramc_data *dparam)
27{
28 if (CONFIG(MEMORY_TEST)) {
29 u8 *addr = _dram;
30 const struct ddr_base_info *ddr_info = &dparam->ddr_info;
31
32 for (u8 rank = RANK_0; rank < ddr_info->support_ranks; rank++) {
Xi Chen3827f562020-10-20 17:55:14 +080033 int result = complex_mem_test(addr, 0x2000);
Huayang Duanc90a9e62020-06-22 19:52:45 +080034
Xi Chen3827f562020-10-20 17:55:14 +080035 if (result != 0) {
36 printk(BIOS_ERR,
37 "[MEM] complex R/W mem test failed: %d\n", result);
Huayang Duanc90a9e62020-06-22 19:52:45 +080038 return -1;
Huayang Duanc90a9e62020-06-22 19:52:45 +080039 }
Xi Chene8c681c2021-03-03 17:58:07 +080040 printk(BIOS_DEBUG, "[MEM] rank %u complex R/W mem test passed\n", rank);
Huayang Duanc90a9e62020-06-22 19:52:45 +080041
42 addr += ddr_info->rank_size[rank];
43 }
44 }
45
46 return 0;
47}
48
Xi Chene8c681c2021-03-03 17:58:07 +080049const char *get_dram_geometry_str(u32 ddr_geometry)
50{
51 const char *s;
52
53 switch (ddr_geometry) {
54 case DDR_TYPE_2CH_2RK_4GB_2_2:
55 s = "2CH_2RK_4GB_2_2";
56 break;
57 case DDR_TYPE_2CH_2RK_6GB_3_3:
58 s = "2CH_2RK_6GB_3_3";
59 break;
60 case DDR_TYPE_2CH_2RK_8GB_4_4:
61 s = "2CH_2RK_8GB_4_4";
62 break;
63 case DDR_TYPE_2CH_2RK_8GB_4_4_BYTE:
64 s = "2CH_2RK_8GB_4_4_BYTE";
65 break;
66 case DDR_TYPE_2CH_1RK_4GB_4_0:
67 s = "2CH_1RK_4GB_4_0";
68 break;
69 case DDR_TYPE_2CH_2RK_6GB_2_4:
70 s = "2CH_2RK_6GB_2_4";
71 break;
72 default:
73 s = "";
74 break;
75 }
76
77 return s;
78}
79
80const char *get_dram_type_str(u32 ddr_type)
81{
82 const char *s;
83
84 switch (ddr_type) {
85 case DDR_TYPE_DISCRETE:
86 s = "DSC";
87 break;
88 case DDR_TYPE_EMCP:
89 s = "EMCP";
90 break;
91 default:
92 s = "";
93 break;
94 }
95
96 return s;
97}
98
Xi Chen5c7a9232022-01-04 19:00:44 +080099static int run_dram_blob(struct dramc_param *dparam)
Huayang Duan68e597d2020-06-22 19:59:40 +0800100{
101 /* Load and run the provided blob for full-calibration if available */
102 struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram");
103
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800104 dump_param_header(dparam);
Huayang Duan68e597d2020-06-22 19:59:40 +0800105
Huayang Duan68e597d2020-06-22 19:59:40 +0800106 if (cbfs_prog_stage_load(&dram)) {
107 printk(BIOS_ERR, "DRAM-K: CBFS load program failed\n");
108 return -2;
109 }
110
111 dparam->do_putc = do_putchar;
112
113 prog_set_entry(&dram, prog_entry(&dram), dparam);
114 prog_run(&dram);
115 if (dparam->header.status != DRAMC_SUCCESS) {
Xi Chen5c7a9232022-01-04 19:00:44 +0800116 printk(BIOS_ERR, "DRAM-K: calibration failed: status = %d\n",
Xi Chene8c681c2021-03-03 17:58:07 +0800117 dparam->header.status);
Huayang Duan68e597d2020-06-22 19:59:40 +0800118 return -3;
119 }
120
Xi Chen5c7a9232022-01-04 19:00:44 +0800121 if (!(dparam->header.config & DRAMC_CONFIG_FAST_K)
122 && !(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
Huayang Duan68e597d2020-06-22 19:59:40 +0800123 printk(BIOS_ERR,
124 "DRAM-K: 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
Xi Chen5c7a9232022-01-04 19:00:44 +0800132static int dram_run_fast_calibration(struct dramc_param *dparam)
133{
134 const u16 config = CONFIG(MEDIATEK_DRAM_DVFS) ? DRAMC_ENABLE_DVFS : DRAMC_DISABLE_DVFS;
135
136 if (dparam->dramc_datas.ddr_info.config_dvfs != config) {
137 printk(BIOS_WARNING,
138 "DRAM-K: Incompatible config for calibration data from flash "
139 "(expected: %#x, saved: %#x)\n",
140 config, dparam->dramc_datas.ddr_info.config_dvfs);
141 return -1;
142 }
143
144 printk(BIOS_INFO, "DRAM-K: DRAM calibration data valid pass\n");
145
146 if (CONFIG(MEDIATEK_BLOB_FAST_INIT)) {
147 printk(BIOS_INFO, "DRAM-K: Run fast calibration run in blob mode\n");
148
149 /*
150 * The loaded config should not contain FAST_K (done in full calibration),
151 * so we have to set that now to indicate the blob taking the config instead
152 * of generating a new config.
153 */
154 dparam->header.config |= DRAMC_CONFIG_FAST_K;
155
156 if (run_dram_blob(dparam) < 0)
157 return -3;
158 } else {
159 init_dram_by_params(dparam);
160 }
161
162 if (mt_mem_test(&dparam->dramc_datas) < 0)
163 return -4;
164
165 return 0;
166}
167
168static int dram_run_full_calibration(struct dramc_param *dparam)
169{
170 initialize_dramc_param(dparam);
171
172 return run_dram_blob(dparam);
173}
174
Huayang Duanc90a9e62020-06-22 19:52:45 +0800175static void mem_init_set_default_config(struct dramc_param *dparam,
Xi Chene8c681c2021-03-03 17:58:07 +0800176 const struct sdram_info *dram_info)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800177{
Xi Chene8c681c2021-03-03 17:58:07 +0800178 u32 type, geometry;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800179 memset(dparam, 0, sizeof(*dparam));
180
Xi Chene8c681c2021-03-03 17:58:07 +0800181 type = dram_info->ddr_type;
182 geometry = dram_info->ddr_geometry;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800183
Yu-Ping Wuc074f612021-04-12 11:03:57 +0800184 dparam->dramc_datas.ddr_info.sdram.ddr_type = type;
Xi Chene8c681c2021-03-03 17:58:07 +0800185
186 if (CONFIG(MEDIATEK_DRAM_DVFS))
Huayang Duanc90a9e62020-06-22 19:52:45 +0800187 dparam->dramc_datas.ddr_info.config_dvfs = DRAMC_ENABLE_DVFS;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800188
Yu-Ping Wuc074f612021-04-12 11:03:57 +0800189 dparam->dramc_datas.ddr_info.sdram.ddr_geometry = geometry;
Xi Chene8c681c2021-03-03 17:58:07 +0800190
191 printk(BIOS_INFO, "DRAM-K: ddr_type: %s, config_dvfs: %d, ddr_geometry: %s\n",
192 get_dram_type_str(type),
193 dparam->dramc_datas.ddr_info.config_dvfs,
194 get_dram_geometry_str(geometry));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800195}
196
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800197static void mt_mem_init_run(struct dramc_param *dparam,
Xi Chene8c681c2021-03-03 17:58:07 +0800198 const struct sdram_info *dram_info)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800199{
Xi Chenf4bb77b2022-01-21 17:18:45 +0800200 const ssize_t mrc_cache_size = sizeof(*dparam);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800201 ssize_t data_size;
Huayang Duan68e597d2020-06-22 19:59:40 +0800202 struct stopwatch sw;
203 int ret;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800204
205 /* Load calibration params from flash and run fast calibration */
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800206 data_size = mrc_cache_load_current(MRC_TRAINING_DATA,
207 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800208 dparam, mrc_cache_size);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800209 if (data_size == mrc_cache_size) {
Huayang Duanc90a9e62020-06-22 19:52:45 +0800210 printk(BIOS_INFO, "DRAM-K: Running fast calibration\n");
Huayang Duan68e597d2020-06-22 19:59:40 +0800211 stopwatch_init(&sw);
212
213 ret = dram_run_fast_calibration(dparam);
214 if (ret != 0) {
215 printk(BIOS_ERR, "DRAM-K: Failed to run fast calibration "
216 "in %ld msecs, error: %d\n",
217 stopwatch_duration_msecs(&sw), ret);
Huayang Duanc90a9e62020-06-22 19:52:45 +0800218
219 /* Erase flash data after fast calibration failed */
Xi Chenf4bb77b2022-01-21 17:18:45 +0800220 memset(dparam, 0xa5, mrc_cache_size);
Yu-Ping Wuba494442021-04-15 10:06:27 +0800221 mrc_cache_stash_data(MRC_TRAINING_DATA,
222 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800223 dparam, mrc_cache_size);
Huayang Duanc90a9e62020-06-22 19:52:45 +0800224 } else {
Huayang Duan68e597d2020-06-22 19:59:40 +0800225 printk(BIOS_INFO, "DRAM-K: Fast calibration passed in %ld msecs\n",
226 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800227 return;
228 }
229 } else {
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800230 printk(BIOS_WARNING, "DRAM-K: Invalid data in flash (size: %#zx, expected: %#zx)\n",
231 data_size, mrc_cache_size);
Huayang Duan68e597d2020-06-22 19:59:40 +0800232 }
233
234 /* Run full calibration */
235 printk(BIOS_INFO, "DRAM-K: Running full calibration\n");
Xi Chene8c681c2021-03-03 17:58:07 +0800236 mem_init_set_default_config(dparam, dram_info);
Huayang Duan68e597d2020-06-22 19:59:40 +0800237
238 stopwatch_init(&sw);
239 int err = dram_run_full_calibration(dparam);
240 if (err == 0) {
241 printk(BIOS_INFO, "DRAM-K: Full calibration passed in %ld msecs\n",
242 stopwatch_duration_msecs(&sw));
Yu-Ping Wuba494442021-04-15 10:06:27 +0800243 mrc_cache_stash_data(MRC_TRAINING_DATA,
244 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800245 dparam, mrc_cache_size);
Huayang Duan68e597d2020-06-22 19:59:40 +0800246 } else {
247 printk(BIOS_ERR, "DRAM-K: Full calibration failed in %ld msecs\n",
248 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800249 }
250}
251
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800252void mt_mem_init(struct dramc_param *dparam)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800253{
254 const struct sdram_info *sdram_param = get_sdram_config();
255
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800256 mt_mem_init_run(dparam, sdram_param);
257}
258
259void mtk_dram_init(void)
260{
261 /* dramc_param is too large to fit in stack. */
262 static struct dramc_param dramc_parameter;
263 mt_mem_init(&dramc_parameter);
264 mtk_mmu_after_dram();
Huayang Duanc90a9e62020-06-22 19:52:45 +0800265}