coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
memory.c
Go to the documentation of this file.
1 /* 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>
7 #include <ip_checksum.h>
9 #include <soc/dramc_param.h>
10 #include <soc/dramc_pi_api.h>
11 #include <soc/emi.h>
12 #include <soc/mt6358.h>
13 #include <symbols.h>
14 
15 static int mt_mem_test(void)
16 {
17  u64 rank_size[RANK_MAX];
18 
19  if (CONFIG(MEMORY_TEST)) {
20  size_t r;
21  u8 *addr = _dram;
22 
23  dramc_get_rank_size(rank_size);
24 
25  for (r = RANK_0; r < RANK_MAX; r++) {
26  int i;
27 
28  if (rank_size[r] == 0)
29  break;
30 
31  i = complex_mem_test(addr, 0x2000);
32 
33  printk(BIOS_DEBUG, "[MEM] complex R/W mem test %s : %d\n",
34  (i == 0) ? "pass" : "fail", i);
35 
36  if (i != 0) {
37  printk(BIOS_ERR, "DRAM memory test failed\n");
38  return -1;
39  }
40 
41  addr += rank_size[r];
42  }
43  }
44 
45  return 0;
46 }
47 
48 static void dump_param_header(const struct dramc_param *dparam)
49 {
50  const struct dramc_param_header *header = &dparam->header;
51 
52  printk(BIOS_DEBUG, "header.status = %#x\n", header->status);
53  printk(BIOS_DEBUG, "header.magic = %#x (expected: %#x)\n",
55  printk(BIOS_DEBUG, "header.version = %#x (expected: %#x)\n",
57  printk(BIOS_DEBUG, "header.size = %#x (expected: %#lx)\n",
58  header->size, sizeof(*dparam));
59  printk(BIOS_DEBUG, "header.config = %#x\n", header->config);
60  printk(BIOS_DEBUG, "header.flags = %#x\n", header->flags);
61  printk(BIOS_DEBUG, "header.checksum = %#x\n", header->checksum);
62 }
63 
64 static u32 compute_checksum(const struct dramc_param *dparam)
65 {
66  return (u32)compute_ip_checksum(dparam->freq_params,
67  sizeof(dparam->freq_params));
68 }
69 
70 static int dram_run_fast_calibration(const struct dramc_param *dparam,
71  u16 config)
72 {
73  if (!is_valid_dramc_param(dparam)) {
75  "Invalid DRAM calibration data from flash\n");
76  dump_param_header(dparam);
77  return -1;
78  }
79 
80  if (dparam->header.config != config) {
82  "Incompatible config for calibration data from flash "
83  "(expected: %#x, saved: %#x)\n",
84  config, dparam->header.config);
85  return -1;
86  }
87 
88  const u32 checksum = compute_checksum(dparam);
89  if (dparam->header.checksum != checksum) {
91  "Invalid DRAM calibration checksum from flash "
92  "(expected: %#x, saved: %#x)\n",
93  checksum, dparam->header.checksum);
94  return -1;
95  }
96 
97  return 0;
98 }
99 
100 static int dram_run_full_calibration(struct dramc_param *dparam,
101  u32 ddr_geometry, u16 config)
102 {
104 
105  /* Load and run the provided blob for full-calibration if available */
106  struct prog dram = PROG_INIT(PROG_REFCODE, CONFIG_CBFS_PREFIX "/dram");
107 
108  if (cbfs_prog_stage_load(&dram))
109  return -2;
110 
111  dparam->do_putc = do_putchar;
112  dparam->freq_params[0].ddr_geometry = ddr_geometry;
113  printk(BIOS_INFO, "ddr_geometry: %d, config: %#x\n", ddr_geometry, config);
114  prog_set_entry(&dram, prog_entry(&dram), dparam);
115  prog_run(&dram);
116 
117  if (dparam->header.status != DRAMC_SUCCESS) {
118  printk(BIOS_ERR, "Full calibration failed: status = %d\n",
119  dparam->header.status);
120  return -3;
121  }
122 
123  if (!(dparam->header.flags & DRAMC_FLAG_HAS_SAVED_DATA)) {
125  "Full calibration executed without saving parameters. "
126  "Please ensure the blob is built properly.\n");
127  return -4;
128  }
129 
130  return 0;
131 }
132 
133 static void set_source_to_flash(struct sdram_params *freq_params)
134 {
135  for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
136  shuffle++)
137  freq_params[shuffle].source = DRAMC_PARAM_SOURCE_FLASH;
138 }
139 
140 static void init_sdram_params(struct sdram_params *dst,
141  const struct sdram_params *src)
142 {
143  for (u8 shuffle = DRAM_DFS_SHUFFLE_1; shuffle < DRAM_DFS_SHUFFLE_MAX;
144  shuffle++)
145  memcpy(&dst[shuffle], src, sizeof(*dst));
146 }
147 
149 {
150  struct dramc_param *dparam = dparam_ops->param;
151 
152  u16 config = 0;
153  if (CONFIG(MT8183_DRAM_EMCP))
155 
156  const bool recovery_mode = vboot_recovery_mode_enabled();
157 
158  /* DRAM DVFS is disabled in recovery mode */
159  if (CONFIG(MT8183_DRAM_DVFS) && !recovery_mode)
161 
162  /* Load calibration params from flash and run fast calibration */
163  if (recovery_mode) {
164  printk(BIOS_WARNING, "Skip loading cached calibration data\n");
166  printk(BIOS_WARNING, "Retrain memory in next boot\n");
167  /* Use 0xFF as erased flash data. */
168  memset(dparam, 0xff, sizeof(*dparam));
169  dparam_ops->write_to_flash(dparam);
170  }
171  } else if (dparam_ops->read_from_flash(dparam)) {
172  printk(BIOS_INFO, "DRAM-K: Fast Calibration\n");
173  if (dram_run_fast_calibration(dparam, config) == 0) {
175  "Calibration params loaded from flash\n");
176  if (mt_set_emi(dparam) == 0 && mt_mem_test() == 0)
177  return;
178  } else {
180  "Failed to apply cached calibration data\n");
181  }
182  } else {
184  "Failed to read calibration data from flash\n");
185  }
186 
187  const struct sdram_params *sdram_cfg = get_sdram_config();
188 
189  /* Run full calibration */
190  printk(BIOS_INFO, "DRAM-K: Full Calibration\n");
191  int err = dram_run_full_calibration(dparam, sdram_cfg->ddr_geometry, config);
192  if (err == 0) {
193  printk(BIOS_INFO, "Successfully loaded DRAM blobs and "
194  "ran DRAM calibration\n");
195 
196  /*
197  * In recovery mode the system boots in RO but the flash params
198  * should be calibrated for RW so we can't mix them up.
199  */
200  if (!recovery_mode) {
202  dparam->header.checksum = compute_checksum(dparam);
203  dparam_ops->write_to_flash(dparam);
204  printk(BIOS_DEBUG, "Calibration params saved to flash: "
205  "version=%#x, size=%#x\n",
206  dparam->header.version, dparam->header.size);
207  }
208  return;
209  }
210 
211  printk(BIOS_ERR, "Failed to do full calibration (%d), "
212  "falling back to load default sdram param\n", err);
213 
214  /* Init params from sdram configs and run partial calibration */
215  printk(BIOS_INFO, "DRAM-K: Partial Calibration\n");
216  init_sdram_params(dparam->freq_params, sdram_cfg);
217  if (mt_set_emi(dparam) != 0)
218  die("Set emi failed with params from sdram config\n");
219  if (mt_mem_test() != 0)
220  die("Memory test failed with params from sdram config\n");
221 }
222 
224 {
226 
227  /* After DRAM calibration, restore vcore voltage to default setting */
228  pmic_set_vcore_vol(800000);
229 }
struct arm64_kernel_header header
Definition: fit_payload.c:30
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
void * memset(void *dstpp, int c, size_t len)
Definition: memset.c:12
const struct sdram_info * get_sdram_config(void)
Definition: sdram_configs.c:85
enum cb_err cbfs_prog_stage_load(struct prog *prog)
Definition: cbfs.c:523
static u32 addr
Definition: cirrus.c:14
int initialize_dramc_param(void *blob)
Definition: dramc_param.c:27
void mt_set_emi(struct dramc_param *dparam)
Definition: emi.c:34
int complex_mem_test(u8 *start, unsigned int len)
Definition: memory_test.c:25
#define printk(level,...)
Definition: stdlib.h:16
void __noreturn die(const char *fmt,...)
Definition: die.c:17
void do_putchar(unsigned char byte)
Definition: printk.c:55
@ RANK_0
@ RANK_MAX
@ DRAM_DFS_SHUFFLE_MAX
@ DRAM_DFS_SHUFFLE_1
@ DRAMC_SUCCESS
@ DRAMC_CONFIG_EMCP
@ DRAMC_CONFIG_DVFS
@ DRAMC_FLAG_HAS_SAVED_DATA
@ CONFIG
Definition: dsi_common.h:201
u8 _dram[]
unsigned long compute_ip_checksum(const void *addr, unsigned long length)
static uint8_t checksum(uint8_t *data, int offset)
Definition: ipmi_fru.c:70
#define BIOS_INFO
BIOS_INFO - Expected events.
Definition: loglevel.h:113
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
enum board_config config
Definition: memory.c:448
static struct dramc_param_ops dparam_ops
Definition: romstage.c:45
void pmic_set_vcore_vol(unsigned int vcore_uv)
Definition: mt6358.c:853
int is_valid_dramc_param(const void *blob)
Definition: dramc_param.c:28
void dramc_get_rank_size(u64 *dram_rank_size)
Definition: emi.c:93
@ DRAMC_PARAM_HEADER_VERSION
Definition: dramc_param.h:13
@ DRAMC_PARAM_HEADER_MAGIC
Definition: dramc_param.h:12
@ DRAMC_PARAM_SOURCE_FLASH
Definition: emi.h:12
static void * prog_entry(const struct prog *prog)
#define PROG_INIT(type_, name_)
static void prog_set_entry(struct prog *prog, void *e, void *arg)
void prog_run(struct prog *prog)
Definition: prog_ops.c:24
@ PROG_REFCODE
int vboot_recovery_mode_enabled(void)
Definition: bootmode.c:21
void mt_mem_init(struct dramc_param *dparam)
Definition: memory.c:304
static void mt_mem_init_run(struct dramc_param_ops *dparam_ops)
Definition: memory.c:148
static int mt_mem_test(void)
Definition: memory.c:15
static int dram_run_fast_calibration(const struct dramc_param *dparam, u16 config)
Definition: memory.c:70
static void dump_param_header(const struct dramc_param *dparam)
Definition: memory.c:48
static u32 compute_checksum(const struct dramc_param *dparam)
Definition: memory.c:64
static int dram_run_full_calibration(struct dramc_param *dparam, u32 ddr_geometry, u16 config)
Definition: memory.c:100
static void set_source_to_flash(struct sdram_params *freq_params)
Definition: memory.c:133
static void init_sdram_params(struct sdram_params *dst, const struct sdram_params *src)
Definition: memory.c:140
uint64_t u64
Definition: stdint.h:54
uint32_t u32
Definition: stdint.h:51
uint16_t u16
Definition: stdint.h:48
uint8_t u8
Definition: stdint.h:45
bool(* read_from_flash)(struct dramc_param *dparam)
Definition: dramc_param.h:68
bool(* write_to_flash)(const struct dramc_param *dparam)
Definition: dramc_param.h:69
struct dramc_param * param
Definition: dramc_param.h:67
struct dramc_param_header header
Definition: dramc_param.h:61
struct sdram_params freq_params[DRAM_DFS_SHUFFLE_MAX]
Definition: dramc_param.h:63
void(* do_putc)(unsigned char c)
Definition: dramc_param.h:62
Defines the SDRAM parameter structure.
Definition: emi.h:15
u32 ddr_geometry
Definition: emi.h:19
int get_recovery_mode_retrain_switch(void)
Definition: switches.c:29