| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <commonlib/bsd/clamp.h> |
| 5 |
#include <console/console.h> |
| 6 |
#include <cpu/intel/haswell/haswell.h> |
| 7 |
#include <delay.h> |
| 8 |
#include <lib.h> |
| 9 |
#include <northbridge/intel/haswell/haswell.h> |
| 10 |
#include <string.h> |
| 11 |
#include <types.h> |
| 12 |
|
| 13 |
#include "raminit_native.h" |
| 14 |
|
| 15 |
static void program_misc_control(struct sysinfo *ctrl) |
| 16 |
{ |
| 17 |
if (!is_hsw_ult()) |
| 18 |
return; |
| 19 |
|
| 20 |
const union ddr_scram_misc_control_reg ddr_scram_misc_ctrl = { |
| 21 |
.ddr_no_ch_interleave = !ctrl->dq_pins_interleaved, |
| 22 |
.lpddr_mode = ctrl->lpddr, |
| 23 |
.cke_mapping_ch0 = ctrl->lpddr ? ctrl->lpddr_cke_rank_map[0] : 0, |
| 24 |
.cke_mapping_ch1 = ctrl->lpddr ? ctrl->lpddr_cke_rank_map[1] : 0, |
| 25 |
}; |
| 26 |
mchbar_write32(DDR_SCRAM_MISC_CONTROL, ddr_scram_misc_ctrl.raw); |
| 27 |
} |
| 28 |
|
| 29 |
static void program_mrc_revision(void) |
| 30 |
{ |
| 31 |
mchbar_write32(MRC_REVISION, 0x01090000); |
| 32 |
} |
| 33 |
|
| 34 |
static void program_ranks_used(struct sysinfo *ctrl) |
| 35 |
{ |
| 36 |
for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) { |
| 37 |
mchbar_write8(MC_INIT_STATE_ch(channel), ctrl->rankmap[channel]); |
| 38 |
if (!does_ch_exist(ctrl, channel)) { |
| 39 |
mchbar_write32(DDR_CLK_ch_RANKS_USED(channel), 0); |
| 40 |
mchbar_write32(DDR_CTL_ch_CTL_RANKS_USED(channel), 0); |
| 41 |
mchbar_write32(DDR_CKE_ch_CTL_RANKS_USED(channel), 0); |
| 42 |
continue; |
| 43 |
} |
| 44 |
uint32_t clk_ranks_used = ctrl->rankmap[channel]; |
| 45 |
if (ctrl->lpddr) { |
| 46 |
|
| 47 |
clk_ranks_used = 0; |
| 48 |
for (uint8_t group = 0; group < NUM_GROUPS; group++) { |
| 49 |
if (ctrl->dq_byte_map[channel][CT_ITERATION_CLOCK][group]) |
| 50 |
clk_ranks_used |= BIT(group); |
| 51 |
} |
| 52 |
} |
| 53 |
mchbar_write32(DDR_CLK_ch_RANKS_USED(channel), clk_ranks_used); |
| 54 |
|
| 55 |
uint32_t ctl_ranks_used = ctrl->rankmap[channel]; |
| 56 |
if (is_hsw_ult()) { |
| 57 |
|
| 58 |
|
| 59 |
if (ctrl->lpddr && ctrl->lpddr_dram_odt) |
| 60 |
ctl_ranks_used |= 2 << 4; |
| 61 |
else |
| 62 |
ctl_ranks_used |= 3 << 4; |
| 63 |
} |
| 64 |
mchbar_write32(DDR_CTL_ch_CTL_RANKS_USED(channel), ctl_ranks_used); |
| 65 |
|
| 66 |
uint32_t cke_ranks_used = ctrl->rankmap[channel]; |
| 67 |
if (ctrl->lpddr) { |
| 68 |
|
| 69 |
const uint8_t cke_rank_map = ctrl->lpddr_cke_rank_map[channel]; |
| 70 |
cke_ranks_used = 0; |
| 71 |
for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) { |
| 72 |
|
| 73 |
if (rank >= 2) |
| 74 |
break; |
| 75 |
|
| 76 |
if (!rank_in_ch(ctrl, rank, channel)) |
| 77 |
continue; |
| 78 |
|
| 79 |
for (uint8_t cke = 0; cke < 4; cke++) { |
| 80 |
if (rank == ((cke_rank_map >> cke) & 1)) |
| 81 |
cke_ranks_used |= BIT(cke); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
mchbar_write32(DDR_CKE_ch_CTL_RANKS_USED(channel), cke_ranks_used); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
static const uint8_t rxb_trad[2][5][4] = { |
| 90 |
{ |
| 91 |
|
| 92 |
{4, 3, 3, 2}, {4, 4, 3, 2}, {5, 4, 3, 3}, {5, 4, 4, 3}, {5, 4, 4, 3}, |
| 93 |
}, |
| 94 |
{ |
| 95 |
|
| 96 |
{4, 3, 3, 2}, {4, 4, 3, 2}, {5, 4, 3, 3}, {5, 4, 4, 3}, {4, 4, 3, 3}, |
| 97 |
}, |
| 98 |
}; |
| 99 |
|
| 100 |
static const uint8_t rxb_ultx[2][3][4] = { |
| 101 |
{ |
| 102 |
|
| 103 |
{5, 6, 6, 5}, {5, 6, 6, 5}, {4, 6, 6, 6}, |
| 104 |
}, |
| 105 |
{ |
| 106 |
|
| 107 |
{7, 6, 6, 5}, {7, 6, 6, 5}, {7, 6, 6, 6}, |
| 108 |
}, |
| 109 |
}; |
| 110 |
|
| 111 |
uint8_t get_rx_bias(const struct sysinfo *ctrl) |
| 112 |
{ |
| 113 |
const bool is_ult = is_hsw_ult(); |
| 114 |
const bool vddhi = ctrl->vdd_mv > 1350; |
| 115 |
const uint8_t max_rxf = is_ult ? ARRAY_SIZE(rxb_ultx[0]) : ARRAY_SIZE(rxb_trad[0]); |
| 116 |
const uint8_t ref_clk = ctrl->base_freq == 133 ? 4 : 6; |
| 117 |
const uint8_t rx_f = clamp_s8(0, ctrl->multiplier - ref_clk, max_rxf - 1); |
| 118 |
const uint8_t rx_cb = mchbar_read32(DDR_CLK_CB_STATUS) & 0x3; |
| 119 |
if (is_ult) |
| 120 |
return rxb_ultx[vddhi][rx_f][rx_cb]; |
| 121 |
else |
| 122 |
return rxb_trad[vddhi][rx_f][rx_cb]; |
| 123 |
} |
| 124 |
|
| 125 |
static void program_ddr_data(struct sysinfo *ctrl, const bool dis_odt_static, const bool vddhi) |
| 126 |
{ |
| 127 |
const bool is_ult = is_hsw_ult(); |
| 128 |
|
| 129 |
for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) { |
| 130 |
if (!does_rank_exist(ctrl, rank)) |
| 131 |
continue; |
| 132 |
|
| 133 |
const union ddr_data_rx_train_rank_reg rx_train = { |
| 134 |
.rcven = 64, |
| 135 |
.dqs_p = 32, |
| 136 |
.dqs_n = 32, |
| 137 |
}; |
| 138 |
mchbar_write32(DDR_DATA_RX_TRAIN_RANK(rank), rx_train.raw); |
| 139 |
mchbar_write32(DDR_DATA_RX_PER_BIT_RANK(rank), 0x88888888); |
| 140 |
|
| 141 |
const union ddr_data_tx_train_rank_reg tx_train = { |
| 142 |
.tx_eq = TXEQFULLDRV | 11, |
| 143 |
.dq_delay = 96, |
| 144 |
.dqs_delay = 64, |
| 145 |
}; |
| 146 |
mchbar_write32(DDR_DATA_TX_TRAIN_RANK(rank), tx_train.raw); |
| 147 |
mchbar_write32(DDR_DATA_TX_PER_BIT_RANK(rank), 0x88888888); |
| 148 |
|
| 149 |
for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) { |
| 150 |
for (uint8_t byte = 0; byte < ctrl->lanes; byte++) { |
| 151 |
ctrl->tx_dq[channel][rank][byte] = tx_train.dq_delay; |
| 152 |
ctrl->txdqs[channel][rank][byte] = tx_train.dqs_delay; |
| 153 |
ctrl->tx_eq[channel][rank][byte] = tx_train.tx_eq; |
| 154 |
|
| 155 |
ctrl->rcven[channel][rank][byte] = rx_train.rcven; |
| 156 |
ctrl->rxdqsp[channel][rank][byte] = rx_train.dqs_p; |
| 157 |
ctrl->rxdqsn[channel][rank][byte] = rx_train.dqs_n; |
| 158 |
ctrl->rx_eq[channel][rank][byte] = rx_train.rx_eq; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
mchbar_write32(DDR_DATA_TX_XTALK, 0); |
| 163 |
mchbar_write32(DDR_DATA_RX_OFFSET_VDQ, 0x88888888); |
| 164 |
mchbar_write32(DDR_DATA_OFFSET_TRAIN, 0); |
| 165 |
mchbar_write32(DDR_DATA_OFFSET_COMP, 0); |
| 166 |
|
| 167 |
const union ddr_data_control_0_reg data_control_0 = { |
| 168 |
.internal_clocks_on = !is_ult, |
| 169 |
.data_vccddq_hi = vddhi, |
| 170 |
.disable_odt_static = dis_odt_static, |
| 171 |
.lpddr_mode = ctrl->lpddr, |
| 172 |
.odt_samp_extend_en = ctrl->lpddr, |
| 173 |
.early_rleak_en = ctrl->lpddr && ctrl->stepping >= STEPPING_C0, |
| 174 |
}; |
| 175 |
mchbar_write32(DDR_DATA_CONTROL_0, data_control_0.raw); |
| 176 |
|
| 177 |
const union ddr_data_control_1_reg data_control_1 = { |
| 178 |
.dll_mask = 1, |
| 179 |
.rx_bias_ctl = get_rx_bias(ctrl), |
| 180 |
.odt_delay = -2, |
| 181 |
.odt_duration = 7, |
| 182 |
.sense_amp_delay = -2, |
| 183 |
.sense_amp_duration = 7, |
| 184 |
}; |
| 185 |
mchbar_write32(DDR_DATA_CONTROL_1, data_control_1.raw); |
| 186 |
|
| 187 |
clear_data_offset_train_all(ctrl); |
| 188 |
|
| 189 |
|
| 190 |
const uint8_t byte_stagger[] = { 0, 4, 1, 5, 2, 6, 3, 7, 8 }; |
| 191 |
const uint8_t latency = 2 * ctrl->tAA - 6; |
| 192 |
for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) { |
| 193 |
if (!does_ch_exist(ctrl, channel)) |
| 194 |
continue; |
| 195 |
|
| 196 |
union ddr_data_control_2_reg data_control_2 = { |
| 197 |
.raw = 0, |
| 198 |
}; |
| 199 |
if (is_ult) { |
| 200 |
data_control_2.rx_dqs_amp_offset = 8; |
| 201 |
data_control_2.rx_clk_stg_num = 0x1f; |
| 202 |
data_control_2.leaker_comp = ctrl->lpddr ? 3 : 0; |
| 203 |
} |
| 204 |
for (uint8_t byte = 0; byte < ctrl->lanes; byte++) { |
| 205 |
const uint8_t stg = latency * byte_stagger[byte] / ctrl->lanes; |
| 206 |
data_control_2.rx_stagger_ctl = stg & 0x1f; |
| 207 |
mchbar_write32(DQ_CONTROL_2(channel, byte), data_control_2.raw); |
| 208 |
ctrl->data_offset_comp[channel][byte].raw = 0; |
| 209 |
ctrl->dq_control_1[channel][byte] = data_control_1.raw; |
| 210 |
ctrl->dq_control_2[channel][byte] = data_control_2.raw; |
| 211 |
} |
| 212 |
ctrl->dq_control_0[channel] = data_control_0.raw; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
static void program_vsshi_control(struct sysinfo *ctrl, const uint16_t vsshi_mv) |
| 217 |
{ |
| 218 |
const uint32_t vsshi_control_reg = is_hsw_ult() ? 0x366c : 0x306c; |
| 219 |
const union ddr_comp_vsshi_control_reg ddr_vsshi_control = { |
| 220 |
.vsshi_target = (vsshi_mv * 192) / ctrl->vdd_mv - 20, |
| 221 |
.hi_bw_divider = 1, |
| 222 |
.lo_bw_divider = 1, |
| 223 |
.bw_error = 2, |
| 224 |
.panic_driver_en = 1, |
| 225 |
.panic_voltage = 24 / 8, |
| 226 |
.gain_boost = 1, |
| 227 |
}; |
| 228 |
mchbar_write32(vsshi_control_reg, ddr_vsshi_control.raw); |
| 229 |
mchbar_write32(DDR_COMP_VSSHI_CONTROL, ddr_vsshi_control.raw); |
| 230 |
} |
| 231 |
|
| 232 |
static void calc_vt_slope_code(const uint16_t slope, uint8_t *best_a, uint8_t *best_b) |
| 233 |
{ |
| 234 |
const int16_t coding[] = {0, -125, -62, -31, 250, 125, 62, 31}; |
| 235 |
*best_a = 0; |
| 236 |
*best_b = 0; |
| 237 |
int16_t best_err = slope; |
| 238 |
for (uint8_t b = 0; b < ARRAY_SIZE(coding); b++) { |
| 239 |
for (uint8_t a = b; a < ARRAY_SIZE(coding); a++) { |
| 240 |
int16_t error = slope - (coding[a] + coding[b]); |
| 241 |
if (error < 0) |
| 242 |
error = -error; |
| 243 |
|
| 244 |
if (error < best_err) { |
| 245 |
best_err = error; |
| 246 |
*best_a = a; |
| 247 |
*best_b = b; |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
static void program_dimm_vref(struct sysinfo *ctrl, const uint16_t vccio_mv, const bool vddhi) |
| 254 |
{ |
| 255 |
const bool is_ult = is_hsw_ult(); |
| 256 |
|
| 257 |
|
| 258 |
uint8_t vt_slope_a = 4; |
| 259 |
uint8_t vt_slope_b = 0; |
| 260 |
if (!is_ult) { |
| 261 |
|
| 262 |
const uint16_t vt_slope = 1500 * vccio_mv / ctrl->vdd_mv - 1000; |
| 263 |
calc_vt_slope_code(vt_slope, &vt_slope_a, &vt_slope_b); |
| 264 |
} |
| 265 |
const union ddr_data_vref_control_reg ddr_vref_control = { |
| 266 |
.hi_bw_divider = is_ult ? 0 : 3, |
| 267 |
.lo_bw_divider = 3, |
| 268 |
.sample_divider = is_ult ? 1 : 3, |
| 269 |
.slow_bw_error = 1, |
| 270 |
.hi_bw_enable = 1, |
| 271 |
.vt_slope_b = vt_slope_b, |
| 272 |
.vt_slope_a = vt_slope_a, |
| 273 |
.vt_offset = 0, |
| 274 |
}; |
| 275 |
mchbar_write32(is_ult ? 0xf68 : 0xf6c, ddr_vref_control.raw); |
| 276 |
|
| 277 |
const union ddr_data_vref_adjust_reg ddr_vref_adjust = { |
| 278 |
.en_dimm_vref_ca = 1, |
| 279 |
.en_dimm_vref_ch0 = 1, |
| 280 |
.en_dimm_vref_ch1 = 1, |
| 281 |
.vccddq_hi_qnnn_h = vddhi, |
| 282 |
.hi_z_timer_ctrl = 3, |
| 283 |
}; |
| 284 |
ctrl->dimm_vref = ddr_vref_adjust; |
| 285 |
mchbar_write32(DDR_DATA_VREF_ADJUST, ddr_vref_adjust.raw); |
| 286 |
} |
| 287 |
|
| 288 |
static uint32_t pi_code(const uint32_t code) |
| 289 |
{ |
| 290 |
return code << 21 | code << 14 | code << 7 | code << 0; |
| 291 |
} |
| 292 |
|
| 293 |
static void program_ddr_ca(struct sysinfo *ctrl, const bool vddhi) |
| 294 |
{ |
| 295 |
for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) { |
| 296 |
if (!does_ch_exist(ctrl, channel)) |
| 297 |
continue; |
| 298 |
|
| 299 |
const union ddr_clk_controls_reg ddr_clk_controls = { |
| 300 |
.dll_mask = 1, |
| 301 |
.vccddq_hi = vddhi, |
| 302 |
.lpddr_mode = ctrl->lpddr, |
| 303 |
}; |
| 304 |
mchbar_write32(DDR_CLK_ch_CONTROLS(channel), ddr_clk_controls.raw); |
| 305 |
|
| 306 |
const union ddr_cmd_controls_reg ddr_cmd_controls = { |
| 307 |
.dll_mask = 1, |
| 308 |
.vccddq_hi = vddhi, |
| 309 |
.lpddr_mode = ctrl->lpddr, |
| 310 |
.early_weak_drive = 3, |
| 311 |
.cmd_tx_eq = 1, |
| 312 |
}; |
| 313 |
mchbar_write32(DDR_CMD_ch_CONTROLS(channel), ddr_cmd_controls.raw); |
| 314 |
|
| 315 |
const union ddr_cke_ctl_controls_reg ddr_cke_controls = { |
| 316 |
.dll_mask = 1, |
| 317 |
.vccddq_hi = vddhi, |
| 318 |
.lpddr_mode = ctrl->lpddr, |
| 319 |
.early_weak_drive = 3, |
| 320 |
.cmd_tx_eq = 1, |
| 321 |
.ctl_tx_eq = 1, |
| 322 |
.ctl_sr_drv = 2, |
| 323 |
}; |
| 324 |
mchbar_write32(DDR_CKE_ch_CTL_CONTROLS(channel), ddr_cke_controls.raw); |
| 325 |
|
| 326 |
const union ddr_cke_ctl_controls_reg ddr_ctl_controls = { |
| 327 |
.dll_mask = 1, |
| 328 |
.vccddq_hi = vddhi, |
| 329 |
.lpddr_mode = ctrl->lpddr, |
| 330 |
.ctl_tx_eq = 1, |
| 331 |
.ctl_sr_drv = 2, |
| 332 |
.la_drv_en_ovrd = 1, |
| 333 |
}; |
| 334 |
mchbar_write32(DDR_CTL_ch_CTL_CONTROLS(channel), ddr_ctl_controls.raw); |
| 335 |
|
| 336 |
const uint8_t cmd_pi = ctrl->lpddr ? 96 : 64; |
| 337 |
mchbar_write32(DDR_CMD_ch_PI_CODING(channel), pi_code(cmd_pi)); |
| 338 |
mchbar_write32(DDR_CKE_ch_CMD_PI_CODING(channel), pi_code(cmd_pi)); |
| 339 |
mchbar_write32(DDR_CKE_CTL_ch_CTL_PI_CODING(channel), pi_code(64)); |
| 340 |
mchbar_write32(DDR_CLK_ch_PI_CODING(channel), pi_code(64)); |
| 341 |
|
| 342 |
mchbar_write32(DDR_CMD_ch_COMP_OFFSET(channel), 0); |
| 343 |
mchbar_write32(DDR_CLK_ch_COMP_OFFSET(channel), 0); |
| 344 |
mchbar_write32(DDR_CKE_CTL_ch_CTL_COMP_OFFSET(channel), 0); |
| 345 |
|
| 346 |
for (uint8_t group = 0; group < NUM_GROUPS; group++) { |
| 347 |
ctrl->cke_cmd_pi_code[channel][group] = cmd_pi; |
| 348 |
ctrl->cmd_north_pi_code[channel][group] = cmd_pi; |
| 349 |
ctrl->cmd_south_pi_code[channel][group] = cmd_pi; |
| 350 |
} |
| 351 |
for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) { |
| 352 |
ctrl->clk_pi_code[channel][rank] = 64; |
| 353 |
ctrl->ctl_pi_code[channel][rank] = 64; |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
enum { |
| 359 |
RCOMP_RD_ODT = 0, |
| 360 |
RCOMP_WR_DS_DQ, |
| 361 |
RCOMP_WR_DS_CMD, |
| 362 |
RCOMP_WR_DS_CTL, |
| 363 |
RCOMP_WR_DS_CLK, |
| 364 |
RCOMP_MAX_CODES, |
| 365 |
}; |
| 366 |
|
| 367 |
struct rcomp_info { |
| 368 |
uint8_t resistor; |
| 369 |
uint8_t sz_steps; |
| 370 |
uint8_t target_r; |
| 371 |
int8_t result; |
| 372 |
}; |
| 373 |
|
| 374 |
static void program_rcomp_vref(struct sysinfo *ctrl, const bool dis_odt_static) |
| 375 |
{ |
| 376 |
const bool is_ult = is_hsw_ult(); |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
struct rcomp_info rcomp_cfg[RCOMP_MAX_CODES] = { |
| 389 |
[RCOMP_RD_ODT] = { |
| 390 |
.resistor = 50, |
| 391 |
.sz_steps = 96, |
| 392 |
.target_r = 50, |
| 393 |
}, |
| 394 |
[RCOMP_WR_DS_DQ] = { |
| 395 |
.resistor = 25, |
| 396 |
.sz_steps = 64, |
| 397 |
.target_r = 33, |
| 398 |
}, |
| 399 |
[RCOMP_WR_DS_CMD] = { |
| 400 |
.resistor = 20, |
| 401 |
.sz_steps = 64, |
| 402 |
.target_r = 20, |
| 403 |
}, |
| 404 |
[RCOMP_WR_DS_CTL] = { |
| 405 |
.resistor = 20, |
| 406 |
.sz_steps = 64, |
| 407 |
.target_r = 20, |
| 408 |
}, |
| 409 |
[RCOMP_WR_DS_CLK] = { |
| 410 |
.resistor = 25, |
| 411 |
.sz_steps = 64, |
| 412 |
.target_r = 29, |
| 413 |
}, |
| 414 |
}; |
| 415 |
if (is_ult) { |
| 416 |
rcomp_cfg[RCOMP_WR_DS_DQ].resistor = 40; |
| 417 |
rcomp_cfg[RCOMP_WR_DS_DQ].target_r = 40; |
| 418 |
rcomp_cfg[RCOMP_WR_DS_CLK].resistor = 40; |
| 419 |
} else if (ctrl->dpc[0] == 2 || ctrl->dpc[1] == 2) { |
| 420 |
rcomp_cfg[RCOMP_RD_ODT].target_r = 60; |
| 421 |
} |
| 422 |
for (uint8_t i = 0; i < RCOMP_MAX_CODES; i++) { |
| 423 |
struct rcomp_info *const r = &rcomp_cfg[i]; |
| 424 |
const int32_t div = 2 * (r->resistor + r->target_r); |
| 425 |
assert(div); |
| 426 |
const int32_t vref = (r->sz_steps * (r->resistor - r->target_r)) / div; |
| 427 |
|
| 428 |
|
| 429 |
const int8_t comp_limit = i == RCOMP_RD_ODT ? 16 : 8; |
| 430 |
r->result = clamp_s32(-comp_limit, vref, comp_limit - 1); |
| 431 |
} |
| 432 |
const union ddr_comp_ctl_0_reg ddr_comp_ctl_0 = { |
| 433 |
.disable_odt_static = dis_odt_static, |
| 434 |
.dq_drv_vref = rcomp_cfg[RCOMP_WR_DS_DQ].result, |
| 435 |
.dq_odt_vref = rcomp_cfg[RCOMP_RD_ODT].result, |
| 436 |
.cmd_drv_vref = rcomp_cfg[RCOMP_WR_DS_CMD].result, |
| 437 |
.ctl_drv_vref = rcomp_cfg[RCOMP_WR_DS_CTL].result, |
| 438 |
.clk_drv_vref = rcomp_cfg[RCOMP_WR_DS_CLK].result, |
| 439 |
}; |
| 440 |
ctrl->comp_ctl_0 = ddr_comp_ctl_0; |
| 441 |
mchbar_write32(DDR_COMP_CTL_0, ctrl->comp_ctl_0.raw); |
| 442 |
} |
| 443 |
|
| 444 |
enum { |
| 445 |
SCOMP_DQ = 0, |
| 446 |
SCOMP_CMD, |
| 447 |
SCOMP_CTL, |
| 448 |
SCOMP_CLK, |
| 449 |
SCOMP_MAX_CODES, |
| 450 |
}; |
| 451 |
|
| 452 |
static void program_slew_rates(struct sysinfo *ctrl, const bool vddhi) |
| 453 |
{ |
| 454 |
const uint8_t min_cycle_delay[SCOMP_MAX_CODES] = { 46, 70, 70, 46 }; |
| 455 |
uint8_t buffer_stage_delay_ps[SCOMP_MAX_CODES] = { 59, 53, 53, 53 }; |
| 456 |
uint16_t comp_slew_rate_codes[SCOMP_MAX_CODES]; |
| 457 |
|
| 458 |
|
| 459 |
if (ctrl->tCMD == 2) |
| 460 |
buffer_stage_delay_ps[SCOMP_CMD] = 89; |
| 461 |
|
| 462 |
|
| 463 |
if (ctrl->lpddr) |
| 464 |
buffer_stage_delay_ps[SCOMP_CMD] = 63; |
| 465 |
|
| 466 |
for (uint8_t i = 0; i < SCOMP_MAX_CODES; i++) { |
| 467 |
uint16_t stages = DIV_ROUND_CLOSEST(ctrl->qclkps, buffer_stage_delay_ps[i]); |
| 468 |
if (stages < 5) |
| 469 |
stages = 5; |
| 470 |
|
| 471 |
bool dll_pc = buffer_stage_delay_ps[i] < min_cycle_delay[i] || stages > 16; |
| 472 |
|
| 473 |
|
| 474 |
if (dll_pc) |
| 475 |
comp_slew_rate_codes[i] = stages / 2 - 1; |
| 476 |
else |
| 477 |
comp_slew_rate_codes[i] = (stages - 1) | BIT(4); |
| 478 |
} |
| 479 |
union ddr_comp_ctl_1_reg ddr_comp_ctl_1 = { |
| 480 |
.dq_scomp = comp_slew_rate_codes[SCOMP_DQ], |
| 481 |
.cmd_scomp = comp_slew_rate_codes[SCOMP_CMD], |
| 482 |
.ctl_scomp = comp_slew_rate_codes[SCOMP_CTL], |
| 483 |
.clk_scomp = comp_slew_rate_codes[SCOMP_CLK], |
| 484 |
.vccddq_hi = vddhi, |
| 485 |
}; |
| 486 |
ctrl->comp_ctl_1 = ddr_comp_ctl_1; |
| 487 |
mchbar_write32(DDR_COMP_CTL_1, ctrl->comp_ctl_1.raw); |
| 488 |
} |
| 489 |
|
| 490 |
static uint32_t ln_x100(const uint32_t input_x100) |
| 491 |
{ |
| 492 |
uint32_t val = input_x100; |
| 493 |
uint32_t ret = 0; |
| 494 |
while (val > 271) { |
| 495 |
val = (val * 1000) / 2718; |
| 496 |
ret += 100; |
| 497 |
} |
| 498 |
return ret + (-16 * val * val + 11578 * val - 978860) / 10000; |
| 499 |
} |
| 500 |
|
| 501 |
static uint32_t compute_vsshi_vref(struct sysinfo *ctrl, const uint32_t vsshi_tgt, bool up) |
| 502 |
{ |
| 503 |
const uint32_t delta = 15; |
| 504 |
const uint32_t c_die_vsshi = 2000; |
| 505 |
const uint32_t r_cmd_ref = 100 * 10; |
| 506 |
const uint32_t offset = up ? 64 : 0; |
| 507 |
const uint32_t ln_vsshi = ln_x100((100 * vsshi_tgt) / (vsshi_tgt - delta)); |
| 508 |
const uint32_t r_target = (ctrl->qclkps * 2000) / (c_die_vsshi * ln_vsshi); |
| 509 |
const uint32_t r_dividend = 128 * (up ? r_cmd_ref : r_target); |
| 510 |
return r_dividend / (r_cmd_ref + r_target) - offset; |
| 511 |
} |
| 512 |
|
| 513 |
static void program_vsshi(struct sysinfo *ctrl, const uint16_t vccio_mv, const uint16_t vsshi) |
| 514 |
{ |
| 515 |
const uint16_t vsshi_down = vsshi + 24; |
| 516 |
const uint16_t vsshi_up = vccio_mv - vsshi_down; |
| 517 |
const union ddr_comp_vsshi_reg ddr_comp_vsshi = { |
| 518 |
.panic_drv_down_vref = compute_vsshi_vref(ctrl, vsshi_down, false), |
| 519 |
.panic_drv_up_vref = compute_vsshi_vref(ctrl, vsshi_up, true), |
| 520 |
.vt_offset = 128 * 450 / vccio_mv / 2, |
| 521 |
.vt_slope_a = 4, |
| 522 |
}; |
| 523 |
mchbar_write32(DDR_COMP_VSSHI, ddr_comp_vsshi.raw); |
| 524 |
} |
| 525 |
|
| 526 |
static void program_misc(struct sysinfo *ctrl) |
| 527 |
{ |
| 528 |
ctrl->misc_control_0.raw = mchbar_read32(DDR_SCRAM_MISC_CONTROL); |
| 529 |
ctrl->misc_control_0.weaklock_latency = 12; |
| 530 |
ctrl->misc_control_0.wl_sleep_cycles = 5; |
| 531 |
ctrl->misc_control_0.wl_wake_cycles = 2; |
| 532 |
mchbar_write32(DDR_SCRAM_MISC_CONTROL, ctrl->misc_control_0.raw); |
| 533 |
for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) { |
| 534 |
|
| 535 |
mchbar_write32(DDR_SCRAMBLE_ch(channel), 0); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
static void override_comp(uint32_t value, uint32_t width, uint32_t shift, uint32_t offset) |
| 541 |
{ |
| 542 |
const uint32_t mask = (1 << width) - 1; |
| 543 |
uint32_t reg32 = mchbar_read32(offset); |
| 544 |
reg32 &= ~(mask << shift); |
| 545 |
reg32 |= (value << shift); |
| 546 |
mchbar_write32(offset, reg32); |
| 547 |
} |
| 548 |
|
| 549 |
static void program_ls_comp(struct sysinfo *ctrl) |
| 550 |
{ |
| 551 |
force_rcomp_and_wait_us(10); |
| 552 |
|
| 553 |
|
| 554 |
const uint32_t ls_comp = 2; |
| 555 |
override_comp(ls_comp, 3, 28, DDR_DATA_RCOMP_DATA_1); |
| 556 |
override_comp(ls_comp, 3, 24, DDR_CMD_COMP); |
| 557 |
override_comp(ls_comp, 3, 24, DDR_CKE_CTL_COMP); |
| 558 |
override_comp(ls_comp, 3, 23, DDR_CLK_COMP); |
| 559 |
override_comp(ls_comp, 3, 28, DDR_DATA_COMP_1); |
| 560 |
override_comp(ls_comp, 3, 24, DDR_COMP_CMD_COMP); |
| 561 |
override_comp(ls_comp, 4, 24, DDR_COMP_CTL_COMP); |
| 562 |
override_comp(ls_comp, 4, 23, DDR_COMP_CLK_COMP); |
| 563 |
override_comp(ls_comp, 3, 24, DDR_COMP_OVERRIDE); |
| 564 |
|
| 565 |
|
| 566 |
union ddr_scram_misc_control_reg ddr_scram_misc_ctrl = ctrl->misc_control_0; |
| 567 |
ddr_scram_misc_ctrl.force_comp_update = 1; |
| 568 |
mchbar_write32(DDR_SCRAM_MISC_CONTROL, ddr_scram_misc_ctrl.raw); |
| 569 |
|
| 570 |
|
| 571 |
const union ddr_data_comp_1_reg data_comp_1 = { |
| 572 |
.raw = mchbar_read32(DDR_DATA_COMP_1), |
| 573 |
}; |
| 574 |
const uint32_t odt_offset = data_comp_1.rcomp_odt_down - data_comp_1.rcomp_odt_up; |
| 575 |
ctrl->comp_ctl_0.odt_up_down_off = odt_offset; |
| 576 |
ctrl->comp_ctl_0.fixed_odt_offset = 1; |
| 577 |
mchbar_write32(DDR_COMP_CTL_0, ctrl->comp_ctl_0.raw); |
| 578 |
} |
| 579 |
|
| 580 |
static void enable_2x_refresh(struct sysinfo *ctrl) |
| 581 |
{ |
| 582 |
if (!CONFIG(ENABLE_DDR_2X_REFRESH)) { |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
printk(BIOS_DEBUG, "Enabling 2x Refresh\n"); |
| 587 |
const bool asr = ctrl->flags.asr; |
| 588 |
const bool lpddr = ctrl->lpddr; |
| 589 |
|
| 590 |
|
| 591 |
assert(!asr || !lpddr); |
| 592 |
if (!asr) { |
| 593 |
uint32_t desired_cfg = 0; |
| 594 |
desired_cfg |= BIT(0); |
| 595 |
desired_cfg |= BIT(31); |
| 596 |
if (lpddr) { |
| 597 |
desired_cfg |= 4 << 1; |
| 598 |
} |
| 599 |
uint32_t reg32 = pcode_mailbox_read(MAILBOX_BIOS_CMD_READ_DDR_2X_REFRESH); |
| 600 |
if (reg32 & BIT(31)) { |
| 601 |
|
| 602 |
const uint32_t current_cfg = reg32; |
| 603 |
if (current_cfg != desired_cfg) { |
| 604 |
printk(BIOS_ERR, |
| 605 |
"Mailbox 2x Refresh locked with wrong values: %x != %x", |
| 606 |
current_cfg, desired_cfg); |
| 607 |
} |
| 608 |
} else { |
| 609 |
|
| 610 |
reg32 |= desired_cfg; |
| 611 |
if (pcode_mailbox_write(MAILBOX_BIOS_CMD_WRITE_DDR_2X_REFRESH, reg32)) { |
| 612 |
printk(BIOS_ERR, "Could not enable Mailbox 2x Refresh\n"); |
| 613 |
} |
| 614 |
} |
| 615 |
if (!lpddr) { |
| 616 |
return; |
| 617 |
} |
| 618 |
} |
| 619 |
assert(asr || lpddr); |
| 620 |
uint16_t refi_reduction = 50; |
| 621 |
if (lpddr) { |
| 622 |
refi_reduction = 97; |
| 623 |
mchbar_clrbits32(PCU_DDR_PTM_CTL, 1 << 7); |
| 624 |
} |
| 625 |
|
| 626 |
if (ctrl->bootmode == BOOTMODE_COLD) { |
| 627 |
ctrl->tREFI *= refi_reduction; |
| 628 |
ctrl->tREFI /= 100; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
static void set_pcu_ddr_voltage(const uint16_t vdd_mv) |
| 633 |
{ |
| 634 |
|
| 635 |
uint32_t pcu_ddr_voltage; |
| 636 |
switch (vdd_mv) { |
| 637 |
case 1200: |
| 638 |
pcu_ddr_voltage = 3; |
| 639 |
break; |
| 640 |
case 1350: |
| 641 |
pcu_ddr_voltage = 1; |
| 642 |
break; |
| 643 |
default: |
| 644 |
case 1500: |
| 645 |
pcu_ddr_voltage = 0; |
| 646 |
break; |
| 647 |
} |
| 648 |
|
| 649 |
mchbar_write32(PCU_DDR_VOLTAGE, pcu_ddr_voltage); |
| 650 |
} |
| 651 |
|
| 652 |
static void program_scheduler(struct sysinfo *ctrl) |
| 653 |
{ |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
const union mcscheds_cbit_reg mcscheds_cbit = { |
| 664 |
.dis_write_gap = 1, |
| 665 |
.dis_odt = is_hsw_ult() && !(ctrl->lpddr && ctrl->lpddr_dram_odt), |
| 666 |
.serialize_zq = ctrl->lpddr, |
| 667 |
}; |
| 668 |
mchbar_write32(MCSCHEDS_CBIT, mcscheds_cbit.raw); |
| 669 |
mchbar_write32(MCMNTS_SC_WDBWM, 0x553c3038); |
| 670 |
if (ctrl->lpddr) { |
| 671 |
for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) { |
| 672 |
if (!does_ch_exist(ctrl, channel)) |
| 673 |
continue; |
| 674 |
|
| 675 |
union mcmain_command_rate_limit_reg cmd_rate_limit = { |
| 676 |
.raw = mchbar_read32(COMMAND_RATE_LIMIT_ch(channel)), |
| 677 |
}; |
| 678 |
cmd_rate_limit.enable_cmd_limit = 1; |
| 679 |
cmd_rate_limit.cmd_rate_limit = 3; |
| 680 |
mchbar_write32(COMMAND_RATE_LIMIT_ch(channel), cmd_rate_limit.raw); |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
static uint8_t biggest_channel(const struct sysinfo *const ctrl) |
| 686 |
{ |
| 687 |
_Static_assert(NUM_CHANNELS == 2, "Code assumes exactly two channels"); |
| 688 |
return !!(ctrl->channel_size_mb[0] < ctrl->channel_size_mb[1]); |
| 689 |
} |
| 690 |
|
| 691 |
static void dram_zones(struct sysinfo *ctrl) |
| 692 |
{ |
| 693 |
|
| 694 |
const uint8_t biggest = biggest_channel(ctrl); |
| 695 |
const uint8_t smaller = !biggest; |
| 696 |
|
| 697 |
|
| 698 |
const union mad_chnl_reg mad_channel = { |
| 699 |
.ch_a = biggest, |
| 700 |
.ch_b = smaller, |
| 701 |
.ch_c = 2, |
| 702 |
.lpddr_mode = ctrl->lpddr, |
| 703 |
}; |
| 704 |
mchbar_write32(MAD_CHNL, mad_channel.raw); |
| 705 |
|
| 706 |
const uint8_t channel_b_zone_size = ctrl->channel_size_mb[smaller] / 256; |
| 707 |
const union mad_zr_reg mad_zr = { |
| 708 |
.ch_b_double = channel_b_zone_size * 2, |
| 709 |
.ch_b_single = channel_b_zone_size, |
| 710 |
}; |
| 711 |
mchbar_write32(MAD_ZR, mad_zr.raw); |
| 712 |
} |
| 713 |
|
| 714 |
static uint8_t biggest_dimm(const struct raminit_dimm_info *dimms) |
| 715 |
{ |
| 716 |
_Static_assert(NUM_SLOTS <= 2, "Code assumes at most two DIMMs per channel."); |
| 717 |
if (NUM_SLOTS == 1) |
| 718 |
return 0; |
| 719 |
|
| 720 |
return !!(dimms[0].data.size_mb < dimms[1].data.size_mb); |
| 721 |
} |
| 722 |
|
| 723 |
static void dram_dimm_mapping(struct sysinfo *ctrl) |
| 724 |
{ |
| 725 |
for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) { |
| 726 |
if (!does_ch_exist(ctrl, channel)) { |
| 727 |
const union mad_dimm_reg mad_dimm = { |
| 728 |
.rank_interleave = 1, |
| 729 |
.enh_interleave = 1, |
| 730 |
}; |
| 731 |
mchbar_write32(MAD_DIMM(channel), mad_dimm.raw); |
| 732 |
continue; |
| 733 |
} |
| 734 |
const uint8_t biggest = biggest_dimm(ctrl->dimms[channel]); |
| 735 |
const uint8_t smaller = !biggest; |
| 736 |
const struct dimm_attr_ddr3_st *dimm_a = &ctrl->dimms[channel][biggest].data; |
| 737 |
const struct dimm_attr_ddr3_st *dimm_b = &ctrl->dimms[channel][smaller].data; |
| 738 |
union mad_dimm_reg mad_dimm = { |
| 739 |
.dimm_a_size = dimm_a->size_mb / 256, |
| 740 |
.dimm_b_size = dimm_b->size_mb / 256, |
| 741 |
.dimm_a_sel = biggest, |
| 742 |
.dimm_a_ranks = dimm_a->ranks == 2, |
| 743 |
.dimm_b_ranks = dimm_b->ranks == 2, |
| 744 |
.dimm_a_width = dimm_a->width == 16, |
| 745 |
.dimm_b_width = dimm_b->width == 16, |
| 746 |
.rank_interleave = 1, |
| 747 |
.enh_interleave = 1, |
| 748 |
.ecc_mode = 0, |
| 749 |
}; |
| 750 |
if (is_hsw_ult()) |
| 751 |
mad_dimm.dimm_b_width = mad_dimm.dimm_a_width; |
| 752 |
|
| 753 |
mchbar_write32(MAD_DIMM(channel), mad_dimm.raw); |
| 754 |
if (ctrl->lpddr) |
| 755 |
die("%s: Missing LPDDR support (LPDDR_MR_PARAMS)\n", __func__); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
enum raminit_status configure_mc(struct sysinfo *ctrl) |
| 760 |
{ |
| 761 |
const uint16_t vccio_mv = 1000; |
| 762 |
const uint16_t vsshi_mv = ctrl->vdd_mv - 950; |
| 763 |
const bool dis_odt_static = is_hsw_ult(); |
| 764 |
const bool vddhi = ctrl->vdd_mv > 1350; |
| 765 |
|
| 766 |
program_misc_control(ctrl); |
| 767 |
program_mrc_revision(); |
| 768 |
program_ranks_used(ctrl); |
| 769 |
program_ddr_data(ctrl, dis_odt_static, vddhi); |
| 770 |
program_vsshi_control(ctrl, vsshi_mv); |
| 771 |
program_dimm_vref(ctrl, vccio_mv, vddhi); |
| 772 |
program_ddr_ca(ctrl, vddhi); |
| 773 |
program_rcomp_vref(ctrl, dis_odt_static); |
| 774 |
program_slew_rates(ctrl, vddhi); |
| 775 |
program_vsshi(ctrl, vccio_mv, vsshi_mv); |
| 776 |
program_misc(ctrl); |
| 777 |
program_ls_comp(ctrl); |
| 778 |
enable_2x_refresh(ctrl); |
| 779 |
set_pcu_ddr_voltage(ctrl->vdd_mv); |
| 780 |
configure_timings(ctrl); |
| 781 |
configure_refresh(ctrl); |
| 782 |
program_scheduler(ctrl); |
| 783 |
dram_zones(ctrl); |
| 784 |
dram_dimm_mapping(ctrl); |
| 785 |
|
| 786 |
return RAMINIT_STATUS_SUCCESS; |
| 787 |
} |