Debian Codegraph @

src/northbridge/intel/haswell/native_raminit/change_margin.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <assert.h>
4 #include <commonlib/bsd/clamp.h>
5 #include <console/console.h>
6 #include <delay.h>
7 #include <northbridge/intel/haswell/haswell.h>
8 #include <timer.h>
9
10 #include "raminit_native.h"
11
12 void update_rxt(
13 struct sysinfo *ctrl,
14 const uint8_t channel,
15 const uint8_t rank,
16 const uint8_t byte,
17 const enum rxt_subfield subfield,
18 const int32_t value)
19 {
20 union ddr_data_rx_train_rank_reg rxt = {
21 .rcven = ctrl->rcven[channel][rank][byte],
22 .dqs_p = ctrl->rxdqsp[channel][rank][byte],
23 .rx_eq = ctrl->rx_eq[channel][rank][byte],
24 .dqs_n = ctrl->rxdqsn[channel][rank][byte],
25 .vref = ctrl->rxvref[channel][rank][byte],
26 };
27 int32_t new_value;
28 switch (subfield) {
29 case RXT_RCVEN:
30 new_value = clamp_s32(0, value, 511);
31 rxt.rcven = new_value;
32 break;
33 case RXT_RXDQS_P:
34 new_value = clamp_s32(0, value, 63);
35 rxt.dqs_p = new_value;
36 break;
37 case RXT_RX_EQ:
38 new_value = clamp_s32(0, value, 31);
39 rxt.rx_eq = new_value;
40 break;
41 case RXT_RXDQS_N:
42 new_value = clamp_s32(0, value, 63);
43 rxt.dqs_n = new_value;
44 break;
45 case RXT_RX_VREF:
46 new_value = clamp_s32(-32, value, 31);
47 rxt.vref = new_value;
48 break;
49 case RXT_RXDQS_BOTH:
50 new_value = clamp_s32(0, value, 63);
51 rxt.dqs_p = new_value;
52 rxt.dqs_n = new_value;
53 break;
54 case RXT_RESTORE:
55 new_value = value;
56 break;
57 default:
58 die("%s: Unhandled subfield index %u\n", __func__, subfield);
59 }
60
61 if (new_value != value) {
62 printk(BIOS_ERR, "%s: Overflow for subfield %u: %d ---> %d\n",
63 __func__, subfield, value, new_value);
64 }
65 mchbar_write32(RX_TRAIN_ch_r_b(channel, rank, byte), rxt.raw);
66 download_regfile(ctrl, channel, false, rank, REG_FILE_USE_RANK, byte, true, false);
67 }
68
69 void update_txt(
70 struct sysinfo *ctrl,
71 const uint8_t channel,
72 const uint8_t rank,
73 const uint8_t byte,
74 const enum txt_subfield subfield,
75 const int32_t value)
76 {
77 union ddr_data_tx_train_rank_reg txt = {
78 .dq_delay = ctrl->tx_dq[channel][rank][byte],
79 .dqs_delay = ctrl->txdqs[channel][rank][byte],
80 .tx_eq = ctrl->tx_eq[channel][rank][byte],
81 };
82 int32_t new_value;
83 switch (subfield) {
84 case TXT_TX_DQ:
85 new_value = clamp_s32(0, value, 511);
86 txt.dq_delay = new_value;
87 break;
88 case TXT_TXDQS:
89 new_value = clamp_s32(0, value, 511);
90 txt.dqs_delay = new_value;
91 break;
92 case TXT_TX_EQ:
93 new_value = clamp_s32(0, value, 63);
94 txt.tx_eq = new_value;
95 break;
96 case TXT_DQDQS_OFF:
97 new_value = value;
98 txt.dqs_delay += new_value;
99 txt.dq_delay += new_value;
100 break;
101 case TXT_RESTORE:
102 new_value = value;
103 break;
104 default:
105 die("%s: Unhandled subfield index %u\n", __func__, subfield);
106 }
107 if (new_value != value) {
108 printk(BIOS_ERR, "%s: Overflow for subfield %u: %d ---> %d\n",
109 __func__, subfield, value, new_value);
110 }
111 mchbar_write32(TX_TRAIN_ch_r_b(channel, rank, byte), txt.raw);
112 download_regfile(ctrl, channel, false, rank, REG_FILE_USE_RANK, byte, false, true);
113 }
114
115 void download_regfile(
116 struct sysinfo *ctrl,
117 const uint8_t channel,
118 const bool multicast,
119 const uint8_t rank,
120 const enum regfile_mode regfile,
121 const uint8_t byte,
122 const bool read_rf_rd,
123 const bool read_rf_wr)
124 {
125 union reut_seq_base_addr_reg reut_seq_base_addr;
126 switch (regfile) {
127 case REG_FILE_USE_START:
128 reut_seq_base_addr.raw = mchbar_read64(REUT_ch_SEQ_ADDR_START(channel));
129 break;
130 case REG_FILE_USE_CURRENT:
131 reut_seq_base_addr.raw = mchbar_read64(REUT_ch_SEQ_ADDR_CURRENT(channel));
132 break;
133 case REG_FILE_USE_RANK:
134 reut_seq_base_addr.raw = 0;
135 if (rank >= NUM_SLOTRANKS)
136 die("%s: bad rank %u\n", __func__, rank);
137 break;
138 default:
139 die("%s: Invalid regfile param %u\n", __func__, regfile);
140 }
141 uint8_t phys_rank = rank;
142 if (reut_seq_base_addr.raw != 0) {
143 /* Map REUT logical rank to physical rank */
144 const uint32_t log_to_phys = mchbar_read32(REUT_ch_RANK_LOG_TO_PHYS(channel));
145 phys_rank = log_to_phys >> (reut_seq_base_addr.rank_addr * 4) & 0x3;
146 }
147 uint32_t reg = multicast ? DDR_DATA_ch_CONTROL_0(channel) : DQ_CONTROL_0(channel, byte);
148 union ddr_data_control_0_reg ddr_data_control_0 = {
149 .raw = mchbar_read32(reg),
150 };
151 ddr_data_control_0.read_rf_rd = read_rf_rd;
152 ddr_data_control_0.read_rf_wr = read_rf_wr;
153 ddr_data_control_0.read_rf_rank = phys_rank;
154 mchbar_write32(reg, ddr_data_control_0.raw);
155 }
156
157 static void update_data_offset_train(
158 struct sysinfo *ctrl,
159 const uint8_t param,
160 const uint8_t en_multicast,
161 const uint8_t channel_in,
162 const uint8_t rank,
163 const uint8_t byte_in,
164 const bool update_ctrl,
165 const enum regfile_mode regfile,
166 const uint32_t value)
167 {
168 bool is_rd = false;
169 bool is_wr = false;
170 switch (param) {
171 case RdT:
172 case RdV:
173 case RcvEna:
174 is_rd = true;
175 break;
176 case WrT:
177 case WrDqsT:
178 is_wr = true;
179 break;
180 default:
181 die("%s: Invalid margin parameter %u\n", __func__, param);
182 }
183 if (en_multicast) {
184 mchbar_write32(DDR_DATA_OFFSET_TRAIN, value);
185 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
186 if (!does_ch_exist(ctrl, channel))
187 continue;
188
189 download_regfile(ctrl, channel, true, rank, regfile, 0, is_rd, is_wr);
190 if (update_ctrl) {
191 for (uint8_t byte = 0; byte < ctrl->lanes; byte++)
192 ctrl->data_offset_train[channel][byte] = value;
193 }
194 }
195 } else {
196 mchbar_write32(DDR_DATA_OFFSET_TRAIN_ch_b(channel_in, byte_in), value);
197 download_regfile(ctrl, channel_in, false, rank, regfile, byte_in, is_rd, is_wr);
198 if (update_ctrl)
199 ctrl->data_offset_train[channel_in][byte_in] = value;
200 }
201 }
202
203 static uint32_t get_max_margin(const enum margin_parameter param)
204 {
205 switch (param) {
206 case RcvEna:
207 case RdT:
208 case WrT:
209 case WrDqsT:
210 return MAX_POSSIBLE_TIME;
211 case RdV:
212 return MAX_POSSIBLE_VREF;
213 default:
214 die("%s: Invalid margin parameter %u\n", __func__, param);
215 }
216 }
217
218 void change_margin(
219 struct sysinfo *ctrl,
220 const enum margin_parameter param,
221 const int32_t value0,
222 const bool en_multicast,
223 const uint8_t channel,
224 const uint8_t rank,
225 const uint8_t byte,
226 const bool update_ctrl,
227 const enum regfile_mode regfile)
228 {
229 /** FIXME: Remove this **/
230 if (rank == 0xff)
231 die("%s: rank is 0xff\n", __func__);
232
233 if (!en_multicast && !does_ch_exist(ctrl, channel))
234 die("%s: Tried to change margin of empty channel %u\n", __func__, channel);
235
236 const uint32_t max_value = get_max_margin(param);
237 const int32_t v0 = clamp_s32(-max_value, value0, max_value);
238
239 union ddr_data_offset_train_reg ddr_data_offset_train = {
240 .raw = en_multicast ? 0 : ctrl->data_offset_train[channel][byte],
241 };
242 bool update_offset_train = false;
243 switch (param) {
244 case RcvEna:
245 ddr_data_offset_train.rcven = v0;
246 update_offset_train = true;
247 break;
248 case RdT:
249 ddr_data_offset_train.rx_dqs = v0;
250 update_offset_train = true;
251 break;
252 case WrT:
253 ddr_data_offset_train.tx_dq = v0;
254 update_offset_train = true;
255 break;
256 case WrDqsT:
257 ddr_data_offset_train.tx_dqs = v0;
258 update_offset_train = true;
259 break;
260 case RdV:
261 ddr_data_offset_train.vref = v0;
262 update_offset_train = true;
263 break;
264 default:
265 die("%s: Invalid margin parameter %u\n", __func__, param);
266 }
267 if (update_offset_train) {
268 update_data_offset_train(
269 ctrl,
270 param,
271 en_multicast,
272 channel,
273 rank,
274 byte,
275 update_ctrl,
276 regfile,
277 ddr_data_offset_train.raw);
278 }
279 }
280
281 void change_1d_margin_multicast(
282 struct sysinfo *ctrl,
283 const enum margin_parameter param,
284 const int32_t value0,
285 const uint8_t rank,
286 const bool update_ctrl,
287 const enum regfile_mode regfile)
288 {
289 change_margin(ctrl, param, value0, true, 0, rank, 0, update_ctrl, regfile);
290 }
291
292 uint32_t update_comp_global_offset(
293 struct sysinfo *ctrl,
294 const enum global_comp_offset param,
295 const int32_t offset,
296 const uint8_t update_ctrl)
297 {
298 union ddr_comp_ctl_0_reg ddr_comp_ctl_0 = ctrl->comp_ctl_0;
299 union ddr_comp_ctl_1_reg ddr_comp_ctl_1 = ctrl->comp_ctl_1;
300 switch (param) {
301 case RdOdt:
302 /* Disable fixed ODT offset before changing this param */
303 ddr_comp_ctl_0.fixed_odt_offset = 0;
304 ddr_comp_ctl_0.dq_odt_vref = offset;
305 break;
306 case WrDS:
307 ddr_comp_ctl_0.dq_drv_vref = offset;
308 break;
309 case WrDSCmd:
310 ddr_comp_ctl_0.cmd_drv_vref = offset;
311 break;
312 case WrDSCtl:
313 ddr_comp_ctl_0.ctl_drv_vref = offset;
314 break;
315 case WrDSClk:
316 ddr_comp_ctl_0.clk_drv_vref = offset;
317 break;
318 case SCompDq:
319 ddr_comp_ctl_1.dq_scomp = offset;
320 break;
321 case SCompCmd:
322 ddr_comp_ctl_1.cmd_scomp = offset;
323 break;
324 case SCompCtl:
325 ddr_comp_ctl_1.ctl_scomp = offset;
326 break;
327 case SCompClk:
328 ddr_comp_ctl_1.clk_scomp = offset;
329 break;
330 case DisOdtStatic:
331 ctrl->comp_ctl_0.disable_odt_static = !!offset;
332 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
333 union ddr_data_control_0_reg data_control_0 = {
334 .raw = ctrl->dq_control_0[channel],
335 };
336 data_control_0.disable_odt_static = !!offset;
337 mchbar_write32(DDR_DATA_ch_CONTROL_0(channel), data_control_0.raw);
338 if (update_ctrl) {
339 ctrl->dq_control_0[channel] = data_control_0.raw;
340 }
341 }
342 break;
343 default:
344 die("%s: Invalid global comp offset %d\n", __func__, param);
345 }
346 mchbar_write32(DDR_COMP_CTL_0, ddr_comp_ctl_0.raw);
347 mchbar_write32(DDR_COMP_CTL_1, ddr_comp_ctl_1.raw);
348 if (update_ctrl) {
349 ctrl->comp_ctl_0 = ddr_comp_ctl_0;
350 ctrl->comp_ctl_1 = ddr_comp_ctl_1;
351 }
352 force_rcomp_and_wait_us(8);
353 const union ddr_data_comp_1_reg ddr_data_comp_1 = {
354 .raw = mchbar_read32(DDR_DATA_COMP_1),
355 };
356 const uint8_t rcomp_odt_up = ddr_data_comp_1.rcomp_odt_up;
357 /* Check if we are close to saturation and try changing the static legs */
358 if (param == RdOdt && (rcomp_odt_up < 16 || rcomp_odt_up > 48)) {
359 const bool dis_odt_static = rcomp_odt_up < 16;
360
361 /* Host always needs to be up-to-date with the static leg state */
362 ddr_comp_ctl_0.disable_odt_static = dis_odt_static;
363 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
364 if (!does_ch_exist(ctrl, channel))
365 continue;
366
367 union ddr_data_control_0_reg data_control_0 = {
368 .raw = ctrl->dq_control_0[channel],
369 };
370 data_control_0.disable_odt_static = dis_odt_static;
371 mchbar_write32(DDR_DATA_ch_CONTROL_0(channel),
372 data_control_0.raw);
373 ctrl->dq_control_0[channel] = data_control_0.raw;
374 }
375 mchbar_write32(DDR_COMP_CTL_0, ddr_comp_ctl_0.raw);
376 ctrl->comp_ctl_0.disable_odt_static = dis_odt_static;
377 force_rcomp_and_wait_us(8);
378 }
379 switch (param) {
380 case RdOdt:
381 case DisOdtStatic: {
382 /* Re-enable fixed ODT offset after changing this param */
383 const uint32_t delta = ddr_data_comp_1.rcomp_odt_down - ddr_data_comp_1.rcomp_odt_up;
384 ddr_comp_ctl_0.odt_up_down_off = delta;
385 ddr_comp_ctl_0.fixed_odt_offset = 1;
386 mchbar_write32(DDR_COMP_CTL_0, ddr_comp_ctl_0.raw);
387 if (update_ctrl)
388 ctrl->comp_ctl_0 = ddr_comp_ctl_0;
389
390 return ddr_data_comp_1.rcomp_odt_up;
391 }
392 case WrDS:
393 case SCompDq: {
394 const union ddr_data_comp_0_reg data_comp_0 = {
395 .raw = mchbar_read32(DDR_DATA_COMP_0),
396 };
397 return param == WrDS ? data_comp_0.rcomp_drv_up : data_comp_0.slew_rate_comp;
398 }
399 case WrDSCmd:
400 case SCompCmd: {
401 const union ddr_comp_cmd_comp_reg cmd_comp = {
402 .raw = mchbar_read32(DDR_COMP_CMD_COMP),
403 };
404 return param == WrDSCmd ? cmd_comp.rcomp_drv_up : cmd_comp.slew_rate_comp;
405 }
406 case WrDSCtl:
407 case SCompCtl: {
408 const union ddr_comp_ctl_comp_reg ctl_comp = {
409 .raw = mchbar_read32(DDR_COMP_CTL_COMP),
410 };
411 return param == WrDSCtl ? ctl_comp.rcomp_drv_up : ctl_comp.slew_rate_comp;
412 }
413 case WrDSClk:
414 case SCompClk: {
415 const union ddr_comp_clk_comp_reg clk_comp = {
416 .raw = mchbar_read32(DDR_COMP_CLK_COMP),
417 };
418 return param == WrDSClk ? clk_comp.rcomp_drv_up : clk_comp.slew_rate_comp;
419 }
420 default:
421 die("%s: Invalid global comp offset %d\n", __func__, param);
422 return 0;
423 }
424 }
425
426 static bool is_cpu_comp(const enum opt_param optparam)
427 {
428 switch (optparam) {
429 case OptWrDS:
430 case OptRdOdt:
431 case OptSComp:
432 case OptTComp:
433 return true;
434 default:
435 return false;
436 }
437 }
438
439 static const uint8_t odt_map[6] = {0, 120, 60, 40, 30, 20};
440
441 void update_opt_param_offset(
442 struct sysinfo *ctrl,
443 const uint8_t channel,
444 const uint8_t ranks_in,
445 const uint8_t byte,
446 const enum opt_param optparam,
447 const int16_t off_in,
448 const bool update_ctrl)
449 {
450 const uint8_t rankmask = ranks_in & ctrl->rankmap[channel];
451 union ddr_data_offset_comp_reg data_offset_comp = ctrl->data_offset_comp[channel][byte];
452 if (optparam == OptDefault) {
453 mchbar_write32(DDR_DATA_OFFSET_COMP_ch_b(channel, byte), data_offset_comp.raw);
454 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
455 if (!rank_in_mask(rank, rankmask)) {
456 continue;
457 }
458 update_rxt(ctrl, channel, rank, byte, RXT_RESTORE, 0);
459 update_txt(ctrl, channel, rank, byte, TXT_RESTORE, 0);
460 }
461 mchbar_write32(DQ_CONTROL_1(channel, byte), ctrl->dq_control_1[channel][byte]);
462 return;
463 }
464 if (is_cpu_comp(optparam)) {
465 int16_t off;
466 if (optparam == OptWrDS) {
467 off = clamp_s16(-32, off_in, 31);
468 } else {
469 off = clamp_s16(-16, off_in, 15);
470 }
471 if (optparam == OptWrDS) {
472 data_offset_comp.drv_up = off;
473 data_offset_comp.drv_down = off;
474 } else if (optparam == OptRdOdt) {
475 data_offset_comp.odt_up = off;
476 data_offset_comp.odt_down = off;
477 } else if (optparam == OptTComp) {
478 data_offset_comp.t_clk_out = off;
479 } else if (optparam == OptSComp) {
480 data_offset_comp.slew_rate = off;
481 }
482 mchbar_write32(DDR_DATA_OFFSET_COMP_ch_b(channel, byte), data_offset_comp.raw);
483 if (update_ctrl) {
484 ctrl->data_offset_comp[channel][byte] = data_offset_comp;
485 }
486 union ddr_scram_misc_control_reg misc_control = ctrl->misc_control_0;
487 misc_control.force_comp_update = 1;
488 mchbar_write32(DDR_SCRAM_MISC_CONTROL, misc_control.raw);
489 }
490 if (optparam == OptTxEq) {
491 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
492 if (!rank_in_mask(rank, rankmask)) {
493 continue;
494 }
495 /*
496 * TxEq[5:4] = Emphasize = [3, 6, 9, 12] legs
497 * TxEq[3:0] = Deemphasize = [0 .. 11, 4 * Rsvd] legs
498 */
499 /* Use 12 emphasis legs (not trained) */
500 const uint8_t off_code = clamp_s16(0, off_in, 11) | TXEQFULLDRV;
501 update_txt(ctrl, channel, rank, byte, TXT_TX_EQ, off_code);
502 if (update_ctrl) {
503 ctrl->tx_eq[channel][rank][byte] = off_code;
504 }
505 }
506 }
507 if (optparam == OptRxEq) {
508 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
509 if (!rank_in_mask(rank, rankmask)) {
510 continue;
511 }
512 /*
513 * RxEQ[4:0] CR Decoding (pF/kOhm)
514 * [2:0]
515 * [4:3] 0 1 2 3 4 5-7
516 * 0 0.5/.02 0.5/1.0 0.5/.50 0.5/.25 0.5/.12 rsvd
517 * 1 1.0/.02 1.0/1.0 1.0/.50 1.0/.25 1.0/.12 rsvd
518 * 2 1.5/.02 1.5/1.0 1.5/.50 1.5/.25 1.5/.12 rsvd
519 * 3 2.0/.02 2.0/1.0 2.0/.50 2.0/.25 2.0/.12 rsvd
520 * Sweep = 0-19 [4:3] = (Sweep / 5) [2:0] = (Sweep % 5)
521 */
522 const int16_t off = clamp_s16(0, off_in, 19);
523 const uint8_t value = ((off / 5) << 3) + (off % 5);
524 update_rxt(ctrl, channel, rank, byte, RXT_RX_EQ, value);
525 if (update_ctrl) {
526 ctrl->rx_eq[channel][rank][byte] = value;
527 }
528 }
529 }
530 if (optparam == OptRxBias) {
531 /*
532 * Mapping: 0: 0.44 1: 0.66 2: 0.88 3: 1.00
533 * 4: 1.33 5: 1.66 6: 2.00 7: 2.33
534 */
535 union ddr_data_control_1_reg data_control_1 = {
536 .raw = ctrl->dq_control_1[channel][byte],
537 };
538 data_control_1.rx_bias_ctl = clamp_s16(0, off_in, 7);
539 mchbar_write32(DQ_CONTROL_1(channel, byte), data_control_1.raw);
540 if (update_ctrl) {
541 ctrl->dq_control_1[channel][byte] = data_control_1.raw;
542 }
543 }
544 if (optparam == OptDimmRon) {
545 if (ctrl->lpddr) {
546 die("%s: LPDDR3 support missing\n", __func__);
547 }
548 /* DIMM Ron: 240/6, 240/7 Ohms */
549 const uint16_t ron = !!off_in << 1;
550 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
551 if (!rank_in_mask(rank, rankmask)) {
552 continue;
553 }
554 const uint16_t mr1reg = (ctrl->mr1[channel][rank] & ~BIT(1)) | ron;
555 reut_issue_mrs(ctrl, channel, BIT(rank), 1, mr1reg);
556 if (update_ctrl) {
557 ctrl->mr1[channel][rank] = mr1reg;
558 }
559 }
560 }
561 if (optparam == OptDimmOdt || optparam == OptDimmOdtWr) {
562 if (ctrl->lpddr) {
563 die("%s: LPDDR3 support missing\n", __func__);
564 }
565 const uint16_t rtt_wr_mrs_encoding[] = { 0, 2, 1 };
566 const uint16_t rtt_wr_mask = BIT(10) | BIT(9);
567 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
568 if (!rank_in_mask(rank, rankmask)) {
569 continue;
570 }
571 uint8_t index = off_in;
572 if (optparam == OptDimmOdt) {
573 index >>= 4;
574 }
575 index = MIN(index, 2);
576 const uint16_t rtt_wr = rtt_wr_mrs_encoding[index] << 9;
577 uint16_t mr2reg = (ctrl->mr2[channel][rank] & ~rtt_wr_mask) | rtt_wr;
578 reut_issue_mrs(ctrl, channel, BIT(rank), 2, mr2reg);
579 if (update_ctrl) {
580 ctrl->mr2[channel][rank] = mr2reg;
581 }
582 if (optparam == OptDimmOdtWr) {
583 continue;
584 }
585 index = off_in;
586 index &= 0xf;
587 if (index >= ARRAY_SIZE(odt_map)) {
588 printk(BIOS_ERR, "C%uR%u RTT NOM idx %u is too large\n",
589 channel, rank, index);
590 index = ARRAY_SIZE(odt_map) - 1;
591 }
592 const uint16_t rtt_nom = encode_ddr3_rttnom(odt_map[index]);
593 uint16_t mr1reg = (ctrl->mr1[channel][rank] & ~RTTNOM_MASK) | rtt_nom;
594 reut_issue_mrs(ctrl, channel, BIT(rank), 1, mr1reg);
595 if (update_ctrl) {
596 ctrl->mr1[channel][rank] = mr1reg;
597 }
598 }
599 }
600 }