blob: be72023dac8ca4b3cac4c439feb0e621961ec004 [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>
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. */
Martin Rothb6a0b262022-09-05 14:51:34 -060017#define CAL_REGION_RW_MRC_CACHE "RW_MRC_CACHE"
18#define CAL_REGION_RW_MRC_CACHE_SIZE 0x2000
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080019
Martin Rothb6a0b262022-09-05 14:51:34 -060020_Static_assert(sizeof(struct dramc_param) <= CAL_REGION_RW_MRC_CACHE_SIZE,
21 "sizeof(struct dramc_param) exceeds " CAL_REGION_RW_MRC_CACHE);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +080022
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
Xi Chen555c2ae2022-01-21 11:43:53 +080026static const struct ddr_base_info *curr_ddr_info;
27
Huayang Duanc90a9e62020-06-22 19:52:45 +080028static int mt_mem_test(const struct dramc_data *dparam)
29{
30 if (CONFIG(MEMORY_TEST)) {
31 u8 *addr = _dram;
32 const struct ddr_base_info *ddr_info = &dparam->ddr_info;
33
34 for (u8 rank = RANK_0; rank < ddr_info->support_ranks; rank++) {
Xi Chen3827f562020-10-20 17:55:14 +080035 int result = complex_mem_test(addr, 0x2000);
Huayang Duanc90a9e62020-06-22 19:52:45 +080036
Xi Chen3827f562020-10-20 17:55:14 +080037 if (result != 0) {
38 printk(BIOS_ERR,
39 "[MEM] complex R/W mem test failed: %d\n", result);
Huayang Duanc90a9e62020-06-22 19:52:45 +080040 return -1;
Huayang Duanc90a9e62020-06-22 19:52:45 +080041 }
Xi Chene8c681c2021-03-03 17:58:07 +080042 printk(BIOS_DEBUG, "[MEM] rank %u complex R/W mem test passed\n", rank);
Huayang Duanc90a9e62020-06-22 19:52:45 +080043
44 addr += ddr_info->rank_size[rank];
45 }
46 }
47
48 return 0;
49}
50
Xi Chene8c681c2021-03-03 17:58:07 +080051const char *get_dram_geometry_str(u32 ddr_geometry)
52{
53 const char *s;
54
55 switch (ddr_geometry) {
56 case DDR_TYPE_2CH_2RK_4GB_2_2:
57 s = "2CH_2RK_4GB_2_2";
58 break;
59 case DDR_TYPE_2CH_2RK_6GB_3_3:
60 s = "2CH_2RK_6GB_3_3";
61 break;
62 case DDR_TYPE_2CH_2RK_8GB_4_4:
63 s = "2CH_2RK_8GB_4_4";
64 break;
65 case DDR_TYPE_2CH_2RK_8GB_4_4_BYTE:
66 s = "2CH_2RK_8GB_4_4_BYTE";
67 break;
68 case DDR_TYPE_2CH_1RK_4GB_4_0:
69 s = "2CH_1RK_4GB_4_0";
70 break;
71 case DDR_TYPE_2CH_2RK_6GB_2_4:
72 s = "2CH_2RK_6GB_2_4";
73 break;
74 default:
75 s = "";
76 break;
77 }
78
79 return s;
80}
81
82const char *get_dram_type_str(u32 ddr_type)
83{
84 const char *s;
85
86 switch (ddr_type) {
87 case DDR_TYPE_DISCRETE:
88 s = "DSC";
89 break;
90 case DDR_TYPE_EMCP:
91 s = "EMCP";
92 break;
93 default:
94 s = "";
95 break;
96 }
97
98 return s;
99}
100
Xi Chen555c2ae2022-01-21 11:43:53 +0800101size_t mtk_dram_size(void)
102{
103 size_t size = 0;
104
105 if (!curr_ddr_info)
106 return 0;
107 for (unsigned int i = 0; i < RANK_MAX; ++i)
108 size += curr_ddr_info->mrr_info.mr8_density[i];
109 return size;
110}
111
112static void fill_dram_info(struct mem_chip_info *mc, const struct ddr_base_info *ddr)
113{
Julius Werner3460aa32022-10-24 19:06:03 -0700114 unsigned int c, r;
Xi Chen555c2ae2022-01-21 11:43:53 +0800115
Julius Werner3460aa32022-10-24 19:06:03 -0700116 mc->num_entries = CHANNEL_MAX * ddr->mrr_info.rank_nums;
117 mc->struct_version = MEM_CHIP_STRUCT_VERSION;
Xi Chen555c2ae2022-01-21 11:43:53 +0800118
Julius Werner3460aa32022-10-24 19:06:03 -0700119 struct mem_chip_entry *entry = mc->entries;
120 for (c = 0; c < CHANNEL_MAX; c++) {
121 for (r = 0; r < ddr->mrr_info.rank_nums; r++) {
122 entry->channel = c;
123 entry->rank = r;
124 entry->type = MEM_CHIP_LPDDR4X;
125 entry->channel_io_width = DQ_DATA_WIDTH_LP4;
126 entry->density_mbits = ddr->mrr_info.mr8_density[r] / CHANNEL_MAX /
127 (MiB / 8);
128 entry->io_width = DQ_DATA_WIDTH_LP4;
129 entry->manufacturer_id = ddr->mrr_info.mr5_vendor_id;
130 entry->revision_id[0] = ddr->mrr_info.mr6_revision_id;
131 entry->revision_id[1] = ddr->mrr_info.mr7_revision_id;
132 entry++;
133 }
Xi Chen555c2ae2022-01-21 11:43:53 +0800134 }
135}
136
137static void add_mem_chip_info(int unused)
138{
139 struct mem_chip_info *mc;
140 size_t size;
141
Rex-BC Chenc69ea242022-03-25 15:53:22 +0800142 if (!CONFIG(USE_CBMEM_DRAM_INFO)) {
143 printk(BIOS_DEBUG,
144 "DRAM-K: CBMEM DRAM info is unsupported (USE_CBMEM_DRAM_INFO)\n");
145 return;
146 }
147
Julius Werner3460aa32022-10-24 19:06:03 -0700148 size = mem_chip_info_size(CHANNEL_MAX * curr_ddr_info->mrr_info.rank_nums);
Xi Chen555c2ae2022-01-21 11:43:53 +0800149 mc = cbmem_add(CBMEM_ID_MEM_CHIP_INFO, size);
150 assert(mc);
Julius Werner3460aa32022-10-24 19:06:03 -0700151 memset(mc, 0, size);
Xi Chen555c2ae2022-01-21 11:43:53 +0800152
153 fill_dram_info(mc, curr_ddr_info);
154}
Kyösti Mälkkifa3bc042022-03-31 07:40:10 +0300155CBMEM_CREATION_HOOK(add_mem_chip_info);
Xi Chen555c2ae2022-01-21 11:43:53 +0800156
Xi Chen5c7a9232022-01-04 19:00:44 +0800157static int run_dram_blob(struct dramc_param *dparam)
Huayang Duan68e597d2020-06-22 19:59:40 +0800158{
159 /* Load and run the provided blob for full-calibration if available */
160 struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram");
161
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800162 dump_param_header(dparam);
Huayang Duan68e597d2020-06-22 19:59:40 +0800163
Huayang Duan68e597d2020-06-22 19:59:40 +0800164 if (cbfs_prog_stage_load(&dram)) {
165 printk(BIOS_ERR, "DRAM-K: CBFS load program failed\n");
166 return -2;
167 }
168
169 dparam->do_putc = do_putchar;
170
171 prog_set_entry(&dram, prog_entry(&dram), dparam);
172 prog_run(&dram);
173 if (dparam->header.status != DRAMC_SUCCESS) {
Xi Chen5c7a9232022-01-04 19:00:44 +0800174 printk(BIOS_ERR, "DRAM-K: calibration failed: status = %d\n",
Xi Chene8c681c2021-03-03 17:58:07 +0800175 dparam->header.status);
Huayang Duan68e597d2020-06-22 19:59:40 +0800176 return -3;
177 }
178
Xi Chen5c7a9232022-01-04 19:00:44 +0800179 if (!(dparam->header.config & DRAMC_CONFIG_FAST_K)
180 && !(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
Huayang Duan68e597d2020-06-22 19:59:40 +0800181 printk(BIOS_ERR,
182 "DRAM-K: Full calibration executed without saving parameters. "
183 "Please ensure the blob is built properly.\n");
184 return -4;
185 }
186
187 return 0;
188}
189
Xi Chen5c7a9232022-01-04 19:00:44 +0800190static int dram_run_fast_calibration(struct dramc_param *dparam)
191{
192 const u16 config = CONFIG(MEDIATEK_DRAM_DVFS) ? DRAMC_ENABLE_DVFS : DRAMC_DISABLE_DVFS;
193
194 if (dparam->dramc_datas.ddr_info.config_dvfs != config) {
195 printk(BIOS_WARNING,
196 "DRAM-K: Incompatible config for calibration data from flash "
197 "(expected: %#x, saved: %#x)\n",
198 config, dparam->dramc_datas.ddr_info.config_dvfs);
199 return -1;
200 }
201
202 printk(BIOS_INFO, "DRAM-K: DRAM calibration data valid pass\n");
203
Xi Chen35fb55a2023-06-14 13:34:25 +0800204 if (CONFIG(MEDIATEK_DRAM_SCRAMBLE))
205 dparam->header.config |= DRAMC_CONFIG_SCRAMBLE;
Yu-Ping Wuc0716522023-02-21 17:24:54 +0800206 if (CONFIG(MEDIATEK_DRAM_BLOB_FAST_INIT)) {
Xi Chen5c7a9232022-01-04 19:00:44 +0800207 printk(BIOS_INFO, "DRAM-K: Run fast calibration run in blob mode\n");
208
209 /*
210 * The loaded config should not contain FAST_K (done in full calibration),
211 * so we have to set that now to indicate the blob taking the config instead
212 * of generating a new config.
213 */
214 dparam->header.config |= DRAMC_CONFIG_FAST_K;
215
216 if (run_dram_blob(dparam) < 0)
217 return -3;
218 } else {
219 init_dram_by_params(dparam);
220 }
221
222 if (mt_mem_test(&dparam->dramc_datas) < 0)
223 return -4;
224
225 return 0;
226}
227
228static int dram_run_full_calibration(struct dramc_param *dparam)
229{
230 initialize_dramc_param(dparam);
231
232 return run_dram_blob(dparam);
233}
234
Huayang Duanc90a9e62020-06-22 19:52:45 +0800235static void mem_init_set_default_config(struct dramc_param *dparam,
Xi Chene8c681c2021-03-03 17:58:07 +0800236 const struct sdram_info *dram_info)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800237{
Xi Chene8c681c2021-03-03 17:58:07 +0800238 u32 type, geometry;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800239 memset(dparam, 0, sizeof(*dparam));
240
Xi Chene8c681c2021-03-03 17:58:07 +0800241 type = dram_info->ddr_type;
242 geometry = dram_info->ddr_geometry;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800243
Yu-Ping Wuc074f612021-04-12 11:03:57 +0800244 dparam->dramc_datas.ddr_info.sdram.ddr_type = type;
Xi Chene8c681c2021-03-03 17:58:07 +0800245
246 if (CONFIG(MEDIATEK_DRAM_DVFS))
Huayang Duanc90a9e62020-06-22 19:52:45 +0800247 dparam->dramc_datas.ddr_info.config_dvfs = DRAMC_ENABLE_DVFS;
Xi Chena6378732023-02-21 14:31:58 +0800248 if (CONFIG(MEDIATEK_DRAM_SCRAMBLE))
249 dparam->header.config |= DRAMC_CONFIG_SCRAMBLE;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800250
Yu-Ping Wuc074f612021-04-12 11:03:57 +0800251 dparam->dramc_datas.ddr_info.sdram.ddr_geometry = geometry;
Xi Chene8c681c2021-03-03 17:58:07 +0800252
253 printk(BIOS_INFO, "DRAM-K: ddr_type: %s, config_dvfs: %d, ddr_geometry: %s\n",
254 get_dram_type_str(type),
255 dparam->dramc_datas.ddr_info.config_dvfs,
256 get_dram_geometry_str(geometry));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800257}
258
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800259static void mt_mem_init_run(struct dramc_param *dparam,
Xi Chene8c681c2021-03-03 17:58:07 +0800260 const struct sdram_info *dram_info)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800261{
Xi Chenf4bb77b2022-01-21 17:18:45 +0800262 const ssize_t mrc_cache_size = sizeof(*dparam);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800263 ssize_t data_size;
Huayang Duan68e597d2020-06-22 19:59:40 +0800264 struct stopwatch sw;
265 int ret;
Huayang Duanc90a9e62020-06-22 19:52:45 +0800266
267 /* Load calibration params from flash and run fast calibration */
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800268 data_size = mrc_cache_load_current(MRC_TRAINING_DATA,
269 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800270 dparam, mrc_cache_size);
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800271 if (data_size == mrc_cache_size) {
Huayang Duanc90a9e62020-06-22 19:52:45 +0800272 printk(BIOS_INFO, "DRAM-K: Running fast calibration\n");
Huayang Duan68e597d2020-06-22 19:59:40 +0800273 stopwatch_init(&sw);
274
275 ret = dram_run_fast_calibration(dparam);
276 if (ret != 0) {
277 printk(BIOS_ERR, "DRAM-K: Failed to run fast calibration "
Rob Barnesd522f382022-09-12 06:31:47 -0600278 "in %lld msecs, error: %d\n",
Huayang Duan68e597d2020-06-22 19:59:40 +0800279 stopwatch_duration_msecs(&sw), ret);
Huayang Duanc90a9e62020-06-22 19:52:45 +0800280
281 /* Erase flash data after fast calibration failed */
Xi Chenf4bb77b2022-01-21 17:18:45 +0800282 memset(dparam, 0xa5, mrc_cache_size);
Yu-Ping Wuba494442021-04-15 10:06:27 +0800283 mrc_cache_stash_data(MRC_TRAINING_DATA,
284 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800285 dparam, mrc_cache_size);
Huayang Duanc90a9e62020-06-22 19:52:45 +0800286 } else {
Rob Barnesd522f382022-09-12 06:31:47 -0600287 printk(BIOS_INFO, "DRAM-K: Fast calibration passed in %lld msecs\n",
Huayang Duan68e597d2020-06-22 19:59:40 +0800288 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800289 return;
290 }
291 } else {
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800292 printk(BIOS_WARNING, "DRAM-K: Invalid data in flash (size: %#zx, expected: %#zx)\n",
293 data_size, mrc_cache_size);
Huayang Duan68e597d2020-06-22 19:59:40 +0800294 }
295
296 /* Run full calibration */
297 printk(BIOS_INFO, "DRAM-K: Running full calibration\n");
Xi Chene8c681c2021-03-03 17:58:07 +0800298 mem_init_set_default_config(dparam, dram_info);
Huayang Duan68e597d2020-06-22 19:59:40 +0800299
300 stopwatch_init(&sw);
301 int err = dram_run_full_calibration(dparam);
302 if (err == 0) {
Rob Barnesd522f382022-09-12 06:31:47 -0600303 printk(BIOS_INFO, "DRAM-K: Full calibration passed in %lld msecs\n",
Huayang Duan68e597d2020-06-22 19:59:40 +0800304 stopwatch_duration_msecs(&sw));
Yu-Ping Wuba494442021-04-15 10:06:27 +0800305 mrc_cache_stash_data(MRC_TRAINING_DATA,
306 DRAMC_PARAM_HEADER_VERSION,
Xi Chenf4bb77b2022-01-21 17:18:45 +0800307 dparam, mrc_cache_size);
Huayang Duan68e597d2020-06-22 19:59:40 +0800308 } else {
Rob Barnesd522f382022-09-12 06:31:47 -0600309 printk(BIOS_ERR, "DRAM-K: Full calibration failed in %lld msecs\n",
Huayang Duan68e597d2020-06-22 19:59:40 +0800310 stopwatch_duration_msecs(&sw));
Huayang Duanc90a9e62020-06-22 19:52:45 +0800311 }
312}
313
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800314void mt_mem_init(struct dramc_param *dparam)
Huayang Duanc90a9e62020-06-22 19:52:45 +0800315{
316 const struct sdram_info *sdram_param = get_sdram_config();
317
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800318 mt_mem_init_run(dparam, sdram_param);
319}
320
321void mtk_dram_init(void)
322{
323 /* dramc_param is too large to fit in stack. */
324 static struct dramc_param dramc_parameter;
325 mt_mem_init(&dramc_parameter);
Xi Chen555c2ae2022-01-21 11:43:53 +0800326 curr_ddr_info = &dramc_parameter.dramc_datas.ddr_info;
Yu-Ping Wu71c5ca72021-01-13 10:29:18 +0800327 mtk_mmu_after_dram();
Huayang Duanc90a9e62020-06-22 19:52:45 +0800328}