blob: f3f784e121d7fa8f2d9273fbb011eee4bcd5bb45 [file] [log] [blame]
Huayang Duanc90a9e62020-06-22 19:52:45 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <assert.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +08004#include <cbfs.h>
Xi Chen555c2ae2022-01-21 11:43:53 +08005#include <cbmem.h>
6#include <commonlib/bsd/mem_chip_info.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +08007#include <console/console.h>
Xi Chen5c7a9232022-01-04 19:00:44 +08008#include <soc/dramc_common.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +08009#include <ip_checksum.h>
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080010#include <mrc_cache.h>
11#include <soc/dramc_param.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080012#include <soc/emi.h>
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080013#include <soc/mmu_operations.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080014#include <symbols.h>
Huayang Duan68e597d2020-06-22 19:59:40 +080015#include <timer.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080016
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080017/* This must be defined in chromeos.fmd in same name and size. */
Martin Rothb6a0b262022-09-05 14:51:34 -060018#define CAL_REGION_RW_MRC_CACHE "RW_MRC_CACHE"
19#define CAL_REGION_RW_MRC_CACHE_SIZE 0x2000
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080020
Martin Rothb6a0b262022-09-05 14:51:34 -060021_Static_assert(sizeof(struct dramc_param) <= CAL_REGION_RW_MRC_CACHE_SIZE,
22 "sizeof(struct dramc_param) exceeds " CAL_REGION_RW_MRC_CACHE);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080023
Xi Chene8c681c2021-03-03 17:58:07 +080024const char *get_dram_geometry_str(u32 ddr_geometry);
25const char *get_dram_type_str(u32 ddr_type);
26
Xi Chen555c2ae2022-01-21 11:43:53 +080027static const struct ddr_base_info *curr_ddr_info;
28
Huayang Duanc90a9e62020-06-22 19:52:45 +080029static int mt_mem_test(const struct dramc_data *dparam)
30{
31 if (CONFIG(MEMORY_TEST)) {
32 u8 *addr = _dram;
33 const struct ddr_base_info *ddr_info = &dparam->ddr_info;
34
35 for (u8 rank = RANK_0; rank < ddr_info->support_ranks; rank++) {
Xi Chen3827f562020-10-20 17:55:14 +080036 int result = complex_mem_test(addr, 0x2000);
Huayang Duanc90a9e62020-06-22 19:52:45 +080037
Xi Chen3827f562020-10-20 17:55:14 +080038 if (result != 0) {
39 printk(BIOS_ERR,
40 "[MEM] complex R/W mem test failed: %d\n", result);
Huayang Duanc90a9e62020-06-22 19:52:45 +080041 return -1;
Huayang Duanc90a9e62020-06-22 19:52:45 +080042 }
Xi Chene8c681c2021-03-03 17:58:07 +080043 printk(BIOS_DEBUG, "[MEM] rank %u complex R/W mem test passed\n", rank);
Huayang Duanc90a9e62020-06-22 19:52:45 +080044
45 addr += ddr_info->rank_size[rank];
46 }
47 }
48
49 return 0;
50}
51
Xi Chene8c681c2021-03-03 17:58:07 +080052const char *get_dram_geometry_str(u32 ddr_geometry)
53{
54 const char *s;
55
56 switch (ddr_geometry) {
57 case DDR_TYPE_2CH_2RK_4GB_2_2:
58 s = "2CH_2RK_4GB_2_2";
59 break;
60 case DDR_TYPE_2CH_2RK_6GB_3_3:
61 s = "2CH_2RK_6GB_3_3";
62 break;
63 case DDR_TYPE_2CH_2RK_8GB_4_4:
64 s = "2CH_2RK_8GB_4_4";
65 break;
66 case DDR_TYPE_2CH_2RK_8GB_4_4_BYTE:
67 s = "2CH_2RK_8GB_4_4_BYTE";
68 break;
69 case DDR_TYPE_2CH_1RK_4GB_4_0:
70 s = "2CH_1RK_4GB_4_0";
71 break;
72 case DDR_TYPE_2CH_2RK_6GB_2_4:
73 s = "2CH_2RK_6GB_2_4";
74 break;
75 default:
76 s = "";
77 break;
78 }
79
80 return s;
81}
82
83const char *get_dram_type_str(u32 ddr_type)
84{
85 const char *s;
86
87 switch (ddr_type) {
88 case DDR_TYPE_DISCRETE:
89 s = "DSC";
90 break;
91 case DDR_TYPE_EMCP:
92 s = "EMCP";
93 break;
94 default:
95 s = "";
96 break;
97 }
98
99 return s;
100}
101
Xi Chen555c2ae2022-01-21 11:43:53 +0800102size_t mtk_dram_size(void)
103{
104 size_t size = 0;
105
106 if (!curr_ddr_info)
107 return 0;
108 for (unsigned int i = 0; i < RANK_MAX; ++i)
109 size += curr_ddr_info->mrr_info.mr8_density[i];
110 return size;
111}
112
113static void fill_dram_info(struct mem_chip_info *mc, const struct ddr_base_info *ddr)
114{
115 unsigned int i;
116 size_t size;
117
118 mc->type = MEM_CHIP_LPDDR4X;
119 mc->num_channels = CHANNEL_MAX;
120 size = mtk_dram_size();
121 assert(size);
122
123 for (i = 0; i < mc->num_channels; ++i) {
124 mc->channel[i].density = size / mc->num_channels;
125 mc->channel[i].io_width = DQ_DATA_WIDTH_LP4;
126 mc->channel[i].manufacturer_id = ddr->mrr_info.mr5_vendor_id;
127 mc->channel[i].revision_id[0] = ddr->mrr_info.mr6_revision_id;
128 mc->channel[i].revision_id[1] = ddr->mrr_info.mr7_revision_id;
129 }
130}
131
132static void add_mem_chip_info(int unused)
133{
134 struct mem_chip_info *mc;
135 size_t size;
136
Rex-BC Chenc69ea242022-03-25 15:53:22 +0800137 if (!CONFIG(USE_CBMEM_DRAM_INFO)) {
138 printk(BIOS_DEBUG,
139 "DRAM-K: CBMEM DRAM info is unsupported (USE_CBMEM_DRAM_INFO)\n");
140 return;
141 }
142
Xi Chen555c2ae2022-01-21 11:43:53 +0800143 size = sizeof(*mc) + sizeof(struct mem_chip_channel) * CHANNEL_MAX;
144 mc = cbmem_add(CBMEM_ID_MEM_CHIP_INFO, size);
145 assert(mc);
146
147 fill_dram_info(mc, curr_ddr_info);
148}
Kyösti Mälkkifa3bc042022-03-31 07:40:10 +0300149CBMEM_CREATION_HOOK(add_mem_chip_info);
Xi Chen555c2ae2022-01-21 11:43:53 +0800150
Xi Chen5c7a9232022-01-04 19:00:44 +0800151static int run_dram_blob(struct dramc_param *dparam)
Huayang Duan68e597d2020-06-22 19:59:40 +0800152{
153 /* Load and run the provided blob for full-calibration if available */
154 struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram");
155
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800156 dump_param_header(dparam);
Huayang Duan68e597d2020-06-22 19:59:40 +0800157
Huayang Duan68e597d2020-06-22 19:59:40 +0800158 if (cbfs_prog_stage_load(&dram)) {
159 printk(BIOS_ERR, "DRAM-K: CBFS load program failed\n");
160 return -2;
161 }
162
163 dparam->do_putc = do_putchar;
164
165 prog_set_entry(&dram, prog_entry(&dram), dparam);
166 prog_run(&dram);
167 if (dparam->header.status != DRAMC_SUCCESS) {
Xi Chen5c7a9232022-01-04 19:00:44 +0800168 printk(BIOS_ERR, "DRAM-K: calibration failed: status = %d\n",
Xi Chene8c681c2021-03-03 17:58:07 +0800169 dparam->header.status);
Huayang Duan68e597d2020-06-22 19:59:40 +0800170 return -3;
171 }
172
Xi Chen5c7a9232022-01-04 19:00:44 +0800173 if (!(dparam->header.config & DRAMC_CONFIG_FAST_K)
174 && !(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
Huayang Duan68e597d2020-06-22 19:59:40 +0800175 printk(BIOS_ERR,
176 "DRAM-K: Full calibration executed without saving parameters. "
177 "Please ensure the blob is built properly.\n");
178 return -4;
179 }
180
181 return 0;
182}
183
Xi Chen5c7a9232022-01-04 19:00:44 +0800184static int dram_run_fast_calibration(struct dramc_param *dparam)
185{
186 const u16 config = CONFIG(MEDIATEK_DRAM_DVFS) ? DRAMC_ENABLE_DVFS : DRAMC_DISABLE_DVFS;
187
188 if (dparam->dramc_datas.ddr_info.config_dvfs != config) {
189 printk(BIOS_WARNING,
190 "DRAM-K: Incompatible config for calibration data from flash "
191 "(expected: %#x, saved: %#x)\n",
192 config, dparam->dramc_datas.ddr_info.config_dvfs);
193 return -1;
194 }
195
196 printk(BIOS_INFO, "DRAM-K: DRAM calibration data valid pass\n");
197
198 if (CONFIG(MEDIATEK_BLOB_FAST_INIT)) {
199 printk(BIOS_INFO, "DRAM-K: Run fast calibration run in blob mode\n");
200
201 /*
202 * The loaded config should not contain FAST_K (done in full calibration),
203 * so we have to set that now to indicate the blob taking the config instead
204 * of generating a new config.
205 */
206 dparam->header.config |= DRAMC_CONFIG_FAST_K;
207
208 if (run_dram_blob(dparam) < 0)
209 return -3;
210 } else {
211 init_dram_by_params(dparam);
212 }
213
214 if (mt_mem_test(&dparam->dramc_datas) < 0)
215 return -4;
216
217 return 0;
218}
219
220static int dram_run_full_calibration(struct dramc_param *dparam)
221{
222 initialize_dramc_param(dparam);
223
224 return run_dram_blob(dparam);
225}
226
Huayang Duanc90a9e62020-06-22 19:52:45 +0800227static void mem_init_set_default_config(struct dramc_param *dparam,
Xi Chene8c681c2021-03-03 17:58:07 +0800228 const struct sdram_info *dram_info)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800229{
Xi Chene8c681c2021-03-03 17:58:07 +0800230 u32 type, geometry;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800231 memset(dparam, 0, sizeof(*dparam));
232
Xi Chene8c681c2021-03-03 17:58:07 +0800233 type = dram_info->ddr_type;
234 geometry = dram_info->ddr_geometry;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800235
Yu-Ping Wuc074f612021-04-12 11:03:57 +0800236 dparam->dramc_datas.ddr_info.sdram.ddr_type = type;
Xi Chene8c681c2021-03-03 17:58:07 +0800237
238 if (CONFIG(MEDIATEK_DRAM_DVFS))
Huayang Duanc90a9e62020-06-22 19:52:45 +0800239 dparam->dramc_datas.ddr_info.config_dvfs = DRAMC_ENABLE_DVFS;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800240
Yu-Ping Wuc074f612021-04-12 11:03:57 +0800241 dparam->dramc_datas.ddr_info.sdram.ddr_geometry = geometry;
Xi Chene8c681c2021-03-03 17:58:07 +0800242
243 printk(BIOS_INFO, "DRAM-K: ddr_type: %s, config_dvfs: %d, ddr_geometry: %s\n",
244 get_dram_type_str(type),
245 dparam->dramc_datas.ddr_info.config_dvfs,
246 get_dram_geometry_str(geometry));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800247}
248
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800249static void mt_mem_init_run(struct dramc_param *dparam,
Xi Chene8c681c2021-03-03 17:58:07 +0800250 const struct sdram_info *dram_info)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800251{
Xi Chenf4bb77b2022-01-21 17:18:45 +0800252 const ssize_t mrc_cache_size = sizeof(*dparam);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800253 ssize_t data_size;
Huayang Duan68e597d2020-06-22 19:59:40 +0800254 struct stopwatch sw;
255 int ret;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800256
257 /* Load calibration params from flash and run fast calibration */
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800258 data_size = mrc_cache_load_current(MRC_TRAINING_DATA,
259 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800260 dparam, mrc_cache_size);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800261 if (data_size == mrc_cache_size) {
Huayang Duanc90a9e62020-06-22 19:52:45 +0800262 printk(BIOS_INFO, "DRAM-K: Running fast calibration\n");
Huayang Duan68e597d2020-06-22 19:59:40 +0800263 stopwatch_init(&sw);
264
265 ret = dram_run_fast_calibration(dparam);
266 if (ret != 0) {
267 printk(BIOS_ERR, "DRAM-K: Failed to run fast calibration "
268 "in %ld msecs, error: %d\n",
269 stopwatch_duration_msecs(&sw), ret);
Huayang Duanc90a9e62020-06-22 19:52:45 +0800270
271 /* Erase flash data after fast calibration failed */
Xi Chenf4bb77b2022-01-21 17:18:45 +0800272 memset(dparam, 0xa5, mrc_cache_size);
Yu-Ping Wuba494442021-04-15 10:06:27 +0800273 mrc_cache_stash_data(MRC_TRAINING_DATA,
274 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800275 dparam, mrc_cache_size);
Huayang Duanc90a9e62020-06-22 19:52:45 +0800276 } else {
Huayang Duan68e597d2020-06-22 19:59:40 +0800277 printk(BIOS_INFO, "DRAM-K: Fast calibration passed in %ld msecs\n",
278 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800279 return;
280 }
281 } else {
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800282 printk(BIOS_WARNING, "DRAM-K: Invalid data in flash (size: %#zx, expected: %#zx)\n",
283 data_size, mrc_cache_size);
Huayang Duan68e597d2020-06-22 19:59:40 +0800284 }
285
286 /* Run full calibration */
287 printk(BIOS_INFO, "DRAM-K: Running full calibration\n");
Xi Chene8c681c2021-03-03 17:58:07 +0800288 mem_init_set_default_config(dparam, dram_info);
Huayang Duan68e597d2020-06-22 19:59:40 +0800289
290 stopwatch_init(&sw);
291 int err = dram_run_full_calibration(dparam);
292 if (err == 0) {
293 printk(BIOS_INFO, "DRAM-K: Full calibration passed in %ld msecs\n",
294 stopwatch_duration_msecs(&sw));
Yu-Ping Wuba494442021-04-15 10:06:27 +0800295 mrc_cache_stash_data(MRC_TRAINING_DATA,
296 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800297 dparam, mrc_cache_size);
Huayang Duan68e597d2020-06-22 19:59:40 +0800298 } else {
299 printk(BIOS_ERR, "DRAM-K: Full calibration failed in %ld msecs\n",
300 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800301 }
302}
303
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800304void mt_mem_init(struct dramc_param *dparam)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800305{
306 const struct sdram_info *sdram_param = get_sdram_config();
307
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800308 mt_mem_init_run(dparam, sdram_param);
309}
310
311void mtk_dram_init(void)
312{
313 /* dramc_param is too large to fit in stack. */
314 static struct dramc_param dramc_parameter;
315 mt_mem_init(&dramc_parameter);
Xi Chen555c2ae2022-01-21 11:43:53 +0800316 curr_ddr_info = &dramc_parameter.dramc_datas.ddr_info;
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800317 mtk_mmu_after_dram();
Huayang Duanc90a9e62020-06-22 19:52:45 +0800318}