blob: 2e460ae1bb17e4dbc42ef1748351c29d4467367f [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>
Xi Chen555c2ae2022-01-21 11:43:53 +08006#include <cbmem.h>
7#include <commonlib/bsd/mem_chip_info.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +08008#include <console/console.h>
Xi Chen5c7a9232022-01-04 19:00:44 +08009#include <soc/dramc_common.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080010#include <ip_checksum.h>
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080011#include <mrc_cache.h>
12#include <soc/dramc_param.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080013#include <soc/emi.h>
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080014#include <soc/mmu_operations.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080015#include <symbols.h>
Huayang Duan68e597d2020-06-22 19:59:40 +080016#include <timer.h>
Huayang Duanc90a9e62020-06-22 19:52:45 +080017
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080018/* This must be defined in chromeos.fmd in same name and size. */
19#define CALIBRATION_REGION "RW_MRC_CACHE"
20#define CALIBRATION_REGION_SIZE 0x2000
21
22_Static_assert(sizeof(struct dramc_param) <= CALIBRATION_REGION_SIZE,
23 "sizeof(struct dramc_param) exceeds " CALIBRATION_REGION);
24
Xi Chene8c681c2021-03-03 17:58:07 +080025const char *get_dram_geometry_str(u32 ddr_geometry);
26const char *get_dram_type_str(u32 ddr_type);
27
Xi Chen555c2ae2022-01-21 11:43:53 +080028static const struct ddr_base_info *curr_ddr_info;
29
Huayang Duanc90a9e62020-06-22 19:52:45 +080030static int mt_mem_test(const struct dramc_data *dparam)
31{
32 if (CONFIG(MEMORY_TEST)) {
33 u8 *addr = _dram;
34 const struct ddr_base_info *ddr_info = &dparam->ddr_info;
35
36 for (u8 rank = RANK_0; rank < ddr_info->support_ranks; rank++) {
Xi Chen3827f562020-10-20 17:55:14 +080037 int result = complex_mem_test(addr, 0x2000);
Huayang Duanc90a9e62020-06-22 19:52:45 +080038
Xi Chen3827f562020-10-20 17:55:14 +080039 if (result != 0) {
40 printk(BIOS_ERR,
41 "[MEM] complex R/W mem test failed: %d\n", result);
Huayang Duanc90a9e62020-06-22 19:52:45 +080042 return -1;
Huayang Duanc90a9e62020-06-22 19:52:45 +080043 }
Xi Chene8c681c2021-03-03 17:58:07 +080044 printk(BIOS_DEBUG, "[MEM] rank %u complex R/W mem test passed\n", rank);
Huayang Duanc90a9e62020-06-22 19:52:45 +080045
46 addr += ddr_info->rank_size[rank];
47 }
48 }
49
50 return 0;
51}
52
Xi Chene8c681c2021-03-03 17:58:07 +080053const char *get_dram_geometry_str(u32 ddr_geometry)
54{
55 const char *s;
56
57 switch (ddr_geometry) {
58 case DDR_TYPE_2CH_2RK_4GB_2_2:
59 s = "2CH_2RK_4GB_2_2";
60 break;
61 case DDR_TYPE_2CH_2RK_6GB_3_3:
62 s = "2CH_2RK_6GB_3_3";
63 break;
64 case DDR_TYPE_2CH_2RK_8GB_4_4:
65 s = "2CH_2RK_8GB_4_4";
66 break;
67 case DDR_TYPE_2CH_2RK_8GB_4_4_BYTE:
68 s = "2CH_2RK_8GB_4_4_BYTE";
69 break;
70 case DDR_TYPE_2CH_1RK_4GB_4_0:
71 s = "2CH_1RK_4GB_4_0";
72 break;
73 case DDR_TYPE_2CH_2RK_6GB_2_4:
74 s = "2CH_2RK_6GB_2_4";
75 break;
76 default:
77 s = "";
78 break;
79 }
80
81 return s;
82}
83
84const char *get_dram_type_str(u32 ddr_type)
85{
86 const char *s;
87
88 switch (ddr_type) {
89 case DDR_TYPE_DISCRETE:
90 s = "DSC";
91 break;
92 case DDR_TYPE_EMCP:
93 s = "EMCP";
94 break;
95 default:
96 s = "";
97 break;
98 }
99
100 return s;
101}
102
Xi Chen555c2ae2022-01-21 11:43:53 +0800103size_t mtk_dram_size(void)
104{
105 size_t size = 0;
106
107 if (!curr_ddr_info)
108 return 0;
109 for (unsigned int i = 0; i < RANK_MAX; ++i)
110 size += curr_ddr_info->mrr_info.mr8_density[i];
111 return size;
112}
113
114static void fill_dram_info(struct mem_chip_info *mc, const struct ddr_base_info *ddr)
115{
116 unsigned int i;
117 size_t size;
118
119 mc->type = MEM_CHIP_LPDDR4X;
120 mc->num_channels = CHANNEL_MAX;
121 size = mtk_dram_size();
122 assert(size);
123
124 for (i = 0; i < mc->num_channels; ++i) {
125 mc->channel[i].density = size / mc->num_channels;
126 mc->channel[i].io_width = DQ_DATA_WIDTH_LP4;
127 mc->channel[i].manufacturer_id = ddr->mrr_info.mr5_vendor_id;
128 mc->channel[i].revision_id[0] = ddr->mrr_info.mr6_revision_id;
129 mc->channel[i].revision_id[1] = ddr->mrr_info.mr7_revision_id;
130 }
131}
132
133static void add_mem_chip_info(int unused)
134{
135 struct mem_chip_info *mc;
136 size_t size;
137
138 size = sizeof(*mc) + sizeof(struct mem_chip_channel) * CHANNEL_MAX;
139 mc = cbmem_add(CBMEM_ID_MEM_CHIP_INFO, size);
140 assert(mc);
141
142 fill_dram_info(mc, curr_ddr_info);
143}
144ROMSTAGE_CBMEM_INIT_HOOK(add_mem_chip_info);
145
Xi Chen5c7a9232022-01-04 19:00:44 +0800146static int run_dram_blob(struct dramc_param *dparam)
Huayang Duan68e597d2020-06-22 19:59:40 +0800147{
148 /* Load and run the provided blob for full-calibration if available */
149 struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram");
150
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800151 dump_param_header(dparam);
Huayang Duan68e597d2020-06-22 19:59:40 +0800152
Huayang Duan68e597d2020-06-22 19:59:40 +0800153 if (cbfs_prog_stage_load(&dram)) {
154 printk(BIOS_ERR, "DRAM-K: CBFS load program failed\n");
155 return -2;
156 }
157
158 dparam->do_putc = do_putchar;
159
160 prog_set_entry(&dram, prog_entry(&dram), dparam);
161 prog_run(&dram);
162 if (dparam->header.status != DRAMC_SUCCESS) {
Xi Chen5c7a9232022-01-04 19:00:44 +0800163 printk(BIOS_ERR, "DRAM-K: calibration failed: status = %d\n",
Xi Chene8c681c2021-03-03 17:58:07 +0800164 dparam->header.status);
Huayang Duan68e597d2020-06-22 19:59:40 +0800165 return -3;
166 }
167
Xi Chen5c7a9232022-01-04 19:00:44 +0800168 if (!(dparam->header.config & DRAMC_CONFIG_FAST_K)
169 && !(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
Huayang Duan68e597d2020-06-22 19:59:40 +0800170 printk(BIOS_ERR,
171 "DRAM-K: Full calibration executed without saving parameters. "
172 "Please ensure the blob is built properly.\n");
173 return -4;
174 }
175
176 return 0;
177}
178
Xi Chen5c7a9232022-01-04 19:00:44 +0800179static int dram_run_fast_calibration(struct dramc_param *dparam)
180{
181 const u16 config = CONFIG(MEDIATEK_DRAM_DVFS) ? DRAMC_ENABLE_DVFS : DRAMC_DISABLE_DVFS;
182
183 if (dparam->dramc_datas.ddr_info.config_dvfs != config) {
184 printk(BIOS_WARNING,
185 "DRAM-K: Incompatible config for calibration data from flash "
186 "(expected: %#x, saved: %#x)\n",
187 config, dparam->dramc_datas.ddr_info.config_dvfs);
188 return -1;
189 }
190
191 printk(BIOS_INFO, "DRAM-K: DRAM calibration data valid pass\n");
192
193 if (CONFIG(MEDIATEK_BLOB_FAST_INIT)) {
194 printk(BIOS_INFO, "DRAM-K: Run fast calibration run in blob mode\n");
195
196 /*
197 * The loaded config should not contain FAST_K (done in full calibration),
198 * so we have to set that now to indicate the blob taking the config instead
199 * of generating a new config.
200 */
201 dparam->header.config |= DRAMC_CONFIG_FAST_K;
202
203 if (run_dram_blob(dparam) < 0)
204 return -3;
205 } else {
206 init_dram_by_params(dparam);
207 }
208
209 if (mt_mem_test(&dparam->dramc_datas) < 0)
210 return -4;
211
212 return 0;
213}
214
215static int dram_run_full_calibration(struct dramc_param *dparam)
216{
217 initialize_dramc_param(dparam);
218
219 return run_dram_blob(dparam);
220}
221
Huayang Duanc90a9e62020-06-22 19:52:45 +0800222static void mem_init_set_default_config(struct dramc_param *dparam,
Xi Chene8c681c2021-03-03 17:58:07 +0800223 const struct sdram_info *dram_info)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800224{
Xi Chene8c681c2021-03-03 17:58:07 +0800225 u32 type, geometry;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800226 memset(dparam, 0, sizeof(*dparam));
227
Xi Chene8c681c2021-03-03 17:58:07 +0800228 type = dram_info->ddr_type;
229 geometry = dram_info->ddr_geometry;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800230
Yu-Ping Wuc074f612021-04-12 11:03:57 +0800231 dparam->dramc_datas.ddr_info.sdram.ddr_type = type;
Xi Chene8c681c2021-03-03 17:58:07 +0800232
233 if (CONFIG(MEDIATEK_DRAM_DVFS))
Huayang Duanc90a9e62020-06-22 19:52:45 +0800234 dparam->dramc_datas.ddr_info.config_dvfs = DRAMC_ENABLE_DVFS;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800235
Yu-Ping Wuc074f612021-04-12 11:03:57 +0800236 dparam->dramc_datas.ddr_info.sdram.ddr_geometry = geometry;
Xi Chene8c681c2021-03-03 17:58:07 +0800237
238 printk(BIOS_INFO, "DRAM-K: ddr_type: %s, config_dvfs: %d, ddr_geometry: %s\n",
239 get_dram_type_str(type),
240 dparam->dramc_datas.ddr_info.config_dvfs,
241 get_dram_geometry_str(geometry));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800242}
243
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800244static void mt_mem_init_run(struct dramc_param *dparam,
Xi Chene8c681c2021-03-03 17:58:07 +0800245 const struct sdram_info *dram_info)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800246{
Xi Chenf4bb77b2022-01-21 17:18:45 +0800247 const ssize_t mrc_cache_size = sizeof(*dparam);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800248 ssize_t data_size;
Huayang Duan68e597d2020-06-22 19:59:40 +0800249 struct stopwatch sw;
250 int ret;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800251
252 /* Load calibration params from flash and run fast calibration */
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800253 data_size = mrc_cache_load_current(MRC_TRAINING_DATA,
254 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800255 dparam, mrc_cache_size);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800256 if (data_size == mrc_cache_size) {
Huayang Duanc90a9e62020-06-22 19:52:45 +0800257 printk(BIOS_INFO, "DRAM-K: Running fast calibration\n");
Huayang Duan68e597d2020-06-22 19:59:40 +0800258 stopwatch_init(&sw);
259
260 ret = dram_run_fast_calibration(dparam);
261 if (ret != 0) {
262 printk(BIOS_ERR, "DRAM-K: Failed to run fast calibration "
263 "in %ld msecs, error: %d\n",
264 stopwatch_duration_msecs(&sw), ret);
Huayang Duanc90a9e62020-06-22 19:52:45 +0800265
266 /* Erase flash data after fast calibration failed */
Xi Chenf4bb77b2022-01-21 17:18:45 +0800267 memset(dparam, 0xa5, mrc_cache_size);
Yu-Ping Wuba494442021-04-15 10:06:27 +0800268 mrc_cache_stash_data(MRC_TRAINING_DATA,
269 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800270 dparam, mrc_cache_size);
Huayang Duanc90a9e62020-06-22 19:52:45 +0800271 } else {
Huayang Duan68e597d2020-06-22 19:59:40 +0800272 printk(BIOS_INFO, "DRAM-K: Fast calibration passed in %ld msecs\n",
273 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800274 return;
275 }
276 } else {
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800277 printk(BIOS_WARNING, "DRAM-K: Invalid data in flash (size: %#zx, expected: %#zx)\n",
278 data_size, mrc_cache_size);
Huayang Duan68e597d2020-06-22 19:59:40 +0800279 }
280
281 /* Run full calibration */
282 printk(BIOS_INFO, "DRAM-K: Running full calibration\n");
Xi Chene8c681c2021-03-03 17:58:07 +0800283 mem_init_set_default_config(dparam, dram_info);
Huayang Duan68e597d2020-06-22 19:59:40 +0800284
285 stopwatch_init(&sw);
286 int err = dram_run_full_calibration(dparam);
287 if (err == 0) {
288 printk(BIOS_INFO, "DRAM-K: Full calibration passed in %ld msecs\n",
289 stopwatch_duration_msecs(&sw));
Yu-Ping Wuba494442021-04-15 10:06:27 +0800290 mrc_cache_stash_data(MRC_TRAINING_DATA,
291 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800292 dparam, mrc_cache_size);
Huayang Duan68e597d2020-06-22 19:59:40 +0800293 } else {
294 printk(BIOS_ERR, "DRAM-K: Full calibration failed in %ld msecs\n",
295 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800296 }
297}
298
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800299void mt_mem_init(struct dramc_param *dparam)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800300{
301 const struct sdram_info *sdram_param = get_sdram_config();
302
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800303 mt_mem_init_run(dparam, sdram_param);
304}
305
306void mtk_dram_init(void)
307{
308 /* dramc_param is too large to fit in stack. */
309 static struct dramc_param dramc_parameter;
310 mt_mem_init(&dramc_parameter);
Xi Chen555c2ae2022-01-21 11:43:53 +0800311 curr_ddr_info = &dramc_parameter.dramc_datas.ddr_info;
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800312 mtk_mmu_after_dram();
Huayang Duanc90a9e62020-06-22 19:52:45 +0800313}