blob: 3d7668d70266caccfb23f64bae9085110ec3ee50 [file] [log] [blame]
Huayang Duanc2ef1022018-09-26 14:24:02 +08001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright 2018 MediaTek Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
Huayang Duan4d15d2f2018-09-26 21:23:53 +080016#include <assert.h>
Huayang Duan078332e2019-08-27 13:36:14 +080017#include <cbfs.h>
Huayang Duan4d15d2f2018-09-26 21:23:53 +080018#include <console/console.h>
Huayang Duan078332e2019-08-27 13:36:14 +080019#include <soc/dramc_param.h>
Huayang Duan4d15d2f2018-09-26 21:23:53 +080020#include <soc/dramc_pi_api.h>
Huayang Duanc2ef1022018-09-26 14:24:02 +080021#include <soc/emi.h>
Huayang Duan4d15d2f2018-09-26 21:23:53 +080022#include <symbols.h>
Huayang Duanc2ef1022018-09-26 14:24:02 +080023
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +080024static int mt_mem_test(void)
Huayang Duanc2ef1022018-09-26 14:24:02 +080025{
Huayang Duan4d15d2f2018-09-26 21:23:53 +080026 u64 rank_size[RANK_MAX];
27
Julius Wernercd49cce2019-03-05 16:53:33 -080028 if (CONFIG(MEMORY_TEST)) {
Huayang Duan4d15d2f2018-09-26 21:23:53 +080029 size_t r;
30 u8 *addr = _dram;
31
32 dramc_get_rank_size(rank_size);
33
34 for (r = RANK_0; r < RANK_MAX; r++) {
35 int i;
36
37 if (rank_size[r] == 0)
38 break;
39
40 i = complex_mem_test(addr, 0x2000);
41
42 printk(BIOS_DEBUG, "[MEM] complex R/W mem test %s : %d\n",
43 (i == 0) ? "pass" : "fail", i);
44
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +080045 if (i != 0) {
Yu-Ping Wu31ec0c42019-10-09 16:11:47 +080046 printk(BIOS_ERR, "DRAM memory test failed\n");
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +080047 return -1;
48 }
Huayang Duan4d15d2f2018-09-26 21:23:53 +080049
50 addr += rank_size[r];
51 }
52 }
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +080053
54 return 0;
Huayang Duanc2ef1022018-09-26 14:24:02 +080055}
Huayang Duan078332e2019-08-27 13:36:14 +080056
57static void dump_param_header(const struct dramc_param *dparam)
58{
59 const struct dramc_param_header *header = &dparam->header;
60
61 printk(BIOS_DEBUG, "header.status = %#x\n", header->status);
62 printk(BIOS_DEBUG, "header.magic = %#x (expected: %#x)\n",
63 header->magic, DRAMC_PARAM_HEADER_MAGIC);
64 printk(BIOS_DEBUG, "header.version = %#x (expected: %#x)\n",
65 header->version, DRAMC_PARAM_HEADER_VERSION);
66 printk(BIOS_DEBUG, "header.size = %#x (expected: %#lx)\n",
67 header->size, sizeof(*dparam));
68 printk(BIOS_DEBUG, "header.config = %#x\n", header->config);
69 printk(BIOS_DEBUG, "header.flags = %#x\n", header->flags);
70 printk(BIOS_DEBUG, "header.checksum = %#x\n", header->checksum);
71}
72
73static int dram_run_fast_calibration(const struct dramc_param *dparam,
74 u16 config)
75{
76 if (!is_valid_dramc_param(dparam)) {
77 printk(BIOS_WARNING,
78 "Invalid DRAM calibration data from flash\n");
79 dump_param_header(dparam);
80 return -1;
81 }
82
Yu-Ping Wu31ec0c42019-10-09 16:11:47 +080083 if (dparam->header.config != config) {
Huayang Duan078332e2019-08-27 13:36:14 +080084 printk(BIOS_WARNING,
85 "Incompatible config for calibration data from flash "
86 "(expected: %#x, saved: %#x)\n",
87 config, dparam->header.config);
88 return -1;
89 }
90
91 return 0;
92}
93
94static int dram_run_full_calibration(struct dramc_param *dparam, u16 config)
95{
96 initialize_dramc_param(dparam, config);
97
98 /* Load and run the provided blob for full-calibration if available */
99 struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram");
100
101 if (prog_locate(&dram))
102 return -1;
103
104 if (cbfs_prog_stage_load(&dram))
105 return -2;
106
107 prog_set_entry(&dram, prog_entry(&dram), dparam);
108 prog_run(&dram);
109
110 if (dparam->header.status != DRAMC_SUCCESS) {
111 printk(BIOS_ERR, "Full calibration failed: status = %d\n",
112 dparam->header.status);
113 return -3;
114 }
115
116 if (!(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
117 printk(BIOS_ERR,
118 "Full calibration executed without saving parameters. "
119 "Please ensure the blob is built properly.\n");
120 return -4;
121 }
122
123 return 0;
124}
125
126static void set_source_to_flash(struct sdram_params *freq_params)
127{
128 for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
129 shuffle++)
130 freq_params[shuffle].source = DRAMC_PARAM_SOURCE_FLASH;
131}
132
133static void init_sdram_params(struct sdram_params *dst,
134 const struct sdram_params *src)
135{
136 for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
137 shuffle++)
138 memcpy(&dst[shuffle], src, sizeof(*dst));
139}
140
141void mt_mem_init(struct dramc_param_ops *dparam_ops)
142{
143 struct dramc_param *dparam = dparam_ops->param;
Huayang Duan078332e2019-08-27 13:36:14 +0800144
145 u16 config = 0;
146 if (CONFIG(MT8183_DRAM_EMCP))
147 config |= DRAMC_CONFIG_EMCP;
148
149 /* Load calibration params from flash and run fast calibration */
150 if (dparam_ops->read_from_flash(dparam)) {
151 if (dram_run_fast_calibration(dparam, config) == 0) {
152 printk(BIOS_INFO,
153 "DRAM calibraion params loaded from flash\n");
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +0800154 if (mt_set_emi(dparam) == 0 && mt_mem_test() == 0)
155 return;
Huayang Duan078332e2019-08-27 13:36:14 +0800156 }
157 } else {
158 printk(BIOS_WARNING,
159 "Failed to read calibration data from flash\n");
160 }
161
162 /* Run full calibration */
163 int err = dram_run_full_calibration(dparam, config);
164 if (err == 0) {
165 printk(BIOS_INFO, "Successfully loaded DRAM blobs and "
166 "ran DRAM calibration\n");
167 set_source_to_flash(dparam->freq_params);
168 dparam_ops->write_to_flash(dparam);
169 printk(BIOS_DEBUG, "Calibration params saved to flash: "
Yu-Ping Wu31ec0c42019-10-09 16:11:47 +0800170 "version=%#x, size=%#x\n",
Huayang Duan078332e2019-08-27 13:36:14 +0800171 dparam->header.version, dparam->header.size);
172 return;
173 }
174
175 printk(BIOS_ERR, "Failed to do full calibration (%d), "
176 "falling back to load default sdram param\n", err);
177
178 /* Init params from sdram configs and run partial calibration */
Yu-Ping Wu0e5b1962019-10-07 16:57:24 +0800179 init_sdram_params(dparam->freq_params, get_sdram_config());
Yu-Ping Wuffb5ea32019-10-07 15:55:57 +0800180 if (mt_set_emi(dparam) != 0)
181 die("Set emi failed with params from sdram config\n");
182 if (mt_mem_test() != 0)
183 die("Memory test failed with params from sdram config\n");
Huayang Duan078332e2019-08-27 13:36:14 +0800184}