Debian Codegraph @

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

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <console/console.h>
4 #include <delay.h>
5 #include <device/pci_ops.h>
6 #include <northbridge/intel/haswell/haswell.h>
7 #include <timer.h>
8 #include <types.h>
9
10 #include "raminit_native.h"
11
12 static void update_internal_clocks_on(struct sysinfo *ctrl)
13 {
14 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
15 if (!does_ch_exist(ctrl, channel))
16 continue;
17
18 bool clocks_on = false;
19 for (uint8_t byte = 0; byte < ctrl->lanes; byte++) {
20 const union ddr_data_control_1_reg data_control_1 = {
21 .raw = ctrl->dq_control_1[channel][byte],
22 };
23 const int8_t o_on = data_control_1.odt_delay;
24 const int8_t s_on = data_control_1.sense_amp_delay;
25 const int8_t o_off = data_control_1.odt_duration;
26 const int8_t s_off = data_control_1.sense_amp_duration;
27 if (o_on + o_off >= 7 || s_on + s_off >= 7) {
28 clocks_on = true;
29 break;
30 }
31 }
32 union ddr_data_control_0_reg data_control_0 = {
33 .raw = ctrl->dq_control_0[channel],
34 };
35 data_control_0.internal_clocks_on = clocks_on;
36 ctrl->dq_control_0[channel] = data_control_0.raw;
37 mchbar_write32(DDR_DATA_ch_CONTROL_0(channel), data_control_0.raw);
38 }
39 }
40
41 /* Switch off unused segments of the SDLL to save power */
42 static void update_sdll_length(struct sysinfo *ctrl)
43 {
44 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
45 if (!does_ch_exist(ctrl, channel))
46 continue;
47
48 for (uint8_t byte = 0; byte < ctrl->lanes; byte++) {
49 uint8_t max_pi = 0;
50 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
51 if (!rank_in_ch(ctrl, rank, channel))
52 continue;
53
54 const uint8_t rx_dqs_p = ctrl->rxdqsp[channel][rank][byte];
55 const uint8_t rx_dqs_n = ctrl->rxdqsn[channel][rank][byte];
56 max_pi = MAX(max_pi, MAX(rx_dqs_p, rx_dqs_n));
57 }
58 /* Update SDLL length for power savings */
59 union ddr_data_control_1_reg data_control_1 = {
60 .raw = ctrl->dq_control_1[channel][byte],
61 };
62 /* Calculate which segments to turn off */
63 data_control_1.sdll_segment_disable = (7 - (max_pi >> 3)) & ~1;
64 ctrl->dq_control_1[channel][byte] = data_control_1.raw;
65 mchbar_write32(DQ_CONTROL_1(channel, byte), data_control_1.raw);
66 }
67 }
68 }
69
70 static void set_rx_clk_stg_num(struct sysinfo *ctrl, const uint8_t channel)
71 {
72 const uint8_t rcven_drift = ctrl->lpddr ? DIV_ROUND_UP(tDQSCK_DRIFT, ctrl->qclkps) : 1;
73 uint8_t max_rcven = 0;
74 for (uint8_t rank = 0; rank < NUM_SLOTRANKS; rank++) {
75 if (!rank_in_ch(ctrl, rank, channel))
76 continue;
77
78 for (uint8_t byte = 0; byte < ctrl->lanes; byte++)
79 max_rcven = MAX(max_rcven, ctrl->rcven[channel][rank][byte] / 64);
80 }
81 const union ddr_data_control_1_reg ddr_data_control_1 = {
82 .raw = ctrl->dq_control_1[channel][0],
83 };
84 const bool lpddr_long_odt = ddr_data_control_1.lpddr_long_odt_en;
85 const uint8_t rcven_turnoff = max_rcven + 18 + 2 * rcven_drift + lpddr_long_odt;
86 const union ddr_data_control_0_reg ddr_data_control_0 = {
87 .raw = ctrl->dq_control_0[channel],
88 };
89 for (uint8_t byte = 0; byte < ctrl->lanes; byte++) {
90 union ddr_data_control_2_reg ddr_data_control_2 = {
91 .raw = ctrl->dq_control_2[channel][byte],
92 };
93 if (ddr_data_control_0.odt_samp_extend_en) {
94 if (ddr_data_control_2.rx_clk_stg_num < rcven_turnoff)
95 ddr_data_control_2.rx_clk_stg_num = rcven_turnoff;
96 } else {
97 const int8_t o_on = ddr_data_control_1.odt_delay;
98 const int8_t o_off = ddr_data_control_1.odt_duration;
99 ddr_data_control_2.rx_clk_stg_num = MAX(17, o_on + o_off + 14);
100 }
101 ctrl->dq_control_2[channel][byte] = ddr_data_control_2.raw;
102 mchbar_write32(DQ_CONTROL_2(channel, byte), ddr_data_control_2.raw);
103 }
104 }
105
106 #define SELF_REFRESH_IDLE_COUNT 0x200
107
108 static void enter_sr(void)
109 {
110 mchbar_write32(PM_SREF_CONFIG, SELF_REFRESH_IDLE_COUNT | BIT(16));
111 udelay(1);
112 }
113
114 enum power_down_mode {
115 PDM_NO_PD = 0,
116 PDM_APD = 1,
117 PDM_PPD = 2,
118 PDM_PPD_DLL_OFF = 6,
119 };
120
121 static void power_down_config(struct sysinfo *ctrl)
122 {
123 const enum power_down_mode pd_mode = ctrl->lpddr ? PDM_PPD : PDM_PPD_DLL_OFF;
124 mchbar_write32(PM_PDWN_CONFIG, pd_mode << 12 | 0x40);
125 }
126
127 static void train_power_modes_post(struct sysinfo *ctrl)
128 {
129 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
130 if (!does_ch_exist(ctrl, channel))
131 continue;
132
133 /* Adjust tCPDED and tPRPDEN */
134 if (ctrl->mem_clock_mhz >= 933)
135 ctrl->tc_bankrank_d[channel].tCPDED = 2;
136
137 if (ctrl->mem_clock_mhz >= 1066)
138 ctrl->tc_bankrank_d[channel].tPRPDEN = 2;
139
140 mchbar_write32(TC_BANK_RANK_D_ch(channel), ctrl->tc_bankrank_d[channel].raw);
141 }
142 power_down_config(ctrl);
143 mchbar_write32(MCDECS_CBIT, BIT(30)); /* dis_msg_clk_gate */
144 }
145
146 static uint8_t compute_burst_end_odt_delay(const struct sysinfo *const ctrl)
147 {
148 /* Must be disabled for LPDDR */
149 if (ctrl->lpddr)
150 return 0;
151
152 const uint8_t beod = MIN(7, DIV_ROUND_CLOSEST(14300 * 20 / 100, ctrl->qclkps));
153 if (beod < 3)
154 return 0;
155
156 if (beod < 4)
157 return 4;
158
159 return beod;
160 }
161
162 static void program_burst_end_odt_delay(struct sysinfo *ctrl)
163 {
164 /* Program burst_end_odt_delay - it should be zero during training steps */
165 const uint8_t beod = compute_burst_end_odt_delay(ctrl);
166 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
167 if (!does_ch_exist(ctrl, channel))
168 continue;
169
170 for (uint8_t byte = 0; byte < ctrl->lanes; byte++) {
171 union ddr_data_control_1_reg ddr_data_control_1 = {
172 .raw = ctrl->dq_control_1[channel][byte],
173 };
174 ddr_data_control_1.burst_end_odt_delay = beod;
175 ctrl->dq_control_1[channel][byte] = ddr_data_control_1.raw;
176 mchbar_write32(DQ_CONTROL_1(channel, byte), ddr_data_control_1.raw);
177 }
178 }
179 }
180
181 /*
182 * Return a random value to use for scrambler seeds. Try to use RDRAND
183 * first and fall back to hardcoded values if RDRAND does not succeed.
184 */
185 static uint16_t get_random_number(const uint8_t channel)
186 {
187 /* The RDRAND instruction is only available 100k cycles after reset */
188 for (size_t i = 0; i < 100000; i++) {
189 uint32_t status;
190 uint32_t random;
191 /** TODO: Clean up asm **/
192 __asm__ __volatile__(
193 "\n\t .byte 0x0F, 0xC7, 0xF0"
194 "\n\t movl %%eax, %0"
195 "\n\t pushf"
196 "\n\t pop %%eax"
197 "\n\t movl %%eax, %1"
198 : "=m"(random),
199 "=m"(status)
200 : /* No inputs */
201 : "eax", "cc");
202
203 /* Only consider non-zero random values as valid */
204 if (status & 1 && random)
205 return random;
206 }
207
208 /* https://xkcd.com/221 */
209 if (channel)
210 return 0x28f4;
211 else
212 return 0x893e;
213 }
214
215 /* Work around "error: 'typeof' applied to a bit-field" */
216 static inline uint32_t max(const uint32_t a, const uint32_t b)
217 {
218 return MAX(a, b);
219 }
220
221 enum raminit_status activate_mc(struct sysinfo *ctrl)
222 {
223 const bool enable_scrambling = true;
224 const bool enable_cmd_tristate = true;
225 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
226 if (!does_ch_exist(ctrl, channel))
227 continue;
228
229 if (enable_scrambling && ctrl->stepping < STEPPING_C0) {
230 /* Make sure tRDRD_(sr, dr, dd) are at least 6 for scrambler W/A */
231 union tc_bank_rank_a_reg tc_bank_rank_a = {
232 .raw = mchbar_read32(TC_BANK_RANK_A_ch(channel)),
233 };
234 tc_bank_rank_a.tRDRD_sr = max(tc_bank_rank_a.tRDRD_sr, 6);
235 tc_bank_rank_a.tRDRD_dr = max(tc_bank_rank_a.tRDRD_dr, 6);
236 tc_bank_rank_a.tRDRD_dd = max(tc_bank_rank_a.tRDRD_dd, 6);
237 mchbar_write32(TC_BANK_RANK_A_ch(channel), tc_bank_rank_a.raw);
238 }
239 if (enable_scrambling) {
240 const union ddr_scramble_reg ddr_scramble = {
241 .scram_key = get_random_number(channel),
242 .scram_en = 1,
243 };
244 mchbar_write32(DDR_SCRAMBLE_ch(channel), ddr_scramble.raw);
245 }
246 if (ctrl->tCMD == 1) {
247 /* If we are in 1N mode, enable and set command rate limit to 3 */
248 union mcmain_command_rate_limit_reg cmd_rate_limit = {
249 .raw = mchbar_read32(COMMAND_RATE_LIMIT_ch(channel)),
250 };
251 cmd_rate_limit.enable_cmd_limit = 1;
252 cmd_rate_limit.cmd_rate_limit = 3;
253 mchbar_write32(COMMAND_RATE_LIMIT_ch(channel), cmd_rate_limit.raw);
254 }
255 if (enable_cmd_tristate) {
256 /* Enable command tri-state at the end of training */
257 union tc_bank_rank_a_reg tc_bank_rank_a = {
258 .raw = mchbar_read32(TC_BANK_RANK_A_ch(channel)),
259 };
260 tc_bank_rank_a.cmd_3st_dis = 0;
261 mchbar_write32(TC_BANK_RANK_A_ch(channel), tc_bank_rank_a.raw);
262 }
263 /* Set MC to normal mode and clean the ODT and CKE */
264 mchbar_write32(REUT_ch_SEQ_CFG(channel), REUT_MODE_NOP << 12);
265 /* Set again the rank occupancy */
266 mchbar_write8(MC_INIT_STATE_ch(channel), ctrl->rankmap[channel]);
267 if (ctrl->is_ecc) {
268 /* Enable ECC I/O and logic */
269 union mad_dimm_reg mad_dimm = {
270 .raw = mchbar_read32(MAD_DIMM(channel)),
271 };
272 mad_dimm.ecc_mode = 3;
273 mchbar_write32(MAD_DIMM(channel), mad_dimm.raw);
274 }
275 }
276
277 if (!is_hsw_ult())
278 update_internal_clocks_on(ctrl);
279
280 update_sdll_length(ctrl);
281
282 program_burst_end_odt_delay(ctrl);
283
284 if (is_hsw_ult()) {
285 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
286 if (!does_ch_exist(ctrl, channel))
287 continue;
288
289 set_rx_clk_stg_num(ctrl, channel);
290 }
291 /** TODO: Program DDRPL_CR_DDR_TX_DELAY if Memory Trace is enabled **/
292 }
293
294 /* Enable periodic COMP */
295 mchbar_write32(M_COMP, (union pcu_comp_reg) {
296 .comp_interval = COMP_INT,
297 }.raw);
298
299 /* Enable the power mode before PCU starts working */
300 train_power_modes_post(ctrl);
301
302 /* Set idle timer and self refresh enable bits */
303 enter_sr();
304
305 /** FIXME: Do not hardcode power weights and RAPL settings **/
306 mchbar_write32(0x5888, 0x00000d0d);
307 mchbar_write32(0x5884, 0x00000004); /* 58.2 pJ */
308
309 mchbar_write32(0x58e0, 0);
310 mchbar_write32(0x58e4, 0);
311
312 mchbar_write32(0x5890, 0xffff);
313 mchbar_write32(0x5894, 0xffff);
314 mchbar_write32(0x5898, 0xffff);
315 mchbar_write32(0x589c, 0xffff);
316 mchbar_write32(0x58d0, 0xffff);
317 mchbar_write32(0x58d4, 0xffff);
318 mchbar_write32(0x58d8, 0xffff);
319 mchbar_write32(0x58dc, 0xffff);
320
321 /* Overwrite thermal parameters */
322 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
323 mchbar_write32(_MCMAIN_C(0x42ec, channel), 0x0000000f);
324 mchbar_write32(_MCMAIN_C(0x42f0, channel), 0x00000009);
325 mchbar_write32(_MCMAIN_C(0x42f4, channel), 0x00000093);
326 mchbar_write32(_MCMAIN_C(0x42f8, channel), 0x00000087);
327 mchbar_write32(_MCMAIN_C(0x42fc, channel), 0x000000de);
328
329 /** TODO: Differs for LPDDR **/
330 mchbar_write32(PM_THRT_CKE_MIN_ch(channel), 0x30);
331 }
332 mchbar_write32(PCU_DDR_PTM_CTL, 0x40);
333 return RAMINIT_STATUS_SUCCESS;
334 }
335
336 enum raminit_status normal_state(struct sysinfo *ctrl)
337 {
338 /* Enable periodic COMP */
339 mchbar_write32(M_COMP, (union pcu_comp_reg) {
340 .comp_interval = COMP_INT,
341 }.raw);
342 for (uint8_t channel = 0; channel < NUM_CHANNELS; channel++) {
343 if (!does_ch_exist(ctrl, channel))
344 continue;
345
346 /* Set MC to normal mode and clean the ODT and CKE */
347 mchbar_write32(REUT_ch_SEQ_CFG(channel), REUT_MODE_NOP << 12);
348 }
349 power_down_config(ctrl);
350 return RAMINIT_STATUS_SUCCESS;
351 }
352
353 static void mc_lockdown(void)
354 {
355 /* Lock memory controller registers */
356 mchbar_write32(MC_LOCK, 0x8f);
357
358 /* MPCOHTRK_GDXC_OCLA_ADDRESS_HI_LOCK is set when programming the memory map */
359
360 /* Lock memory map registers */
361 pci_or_config16(HOST_BRIDGE, GGC, 1 << 0);
362 pci_or_config32(HOST_BRIDGE, DPR, 1 << 0);
363 pci_or_config32(HOST_BRIDGE, MESEG_LIMIT, 1 << 10);
364 pci_or_config32(HOST_BRIDGE, REMAPBASE, 1 << 0);
365 pci_or_config32(HOST_BRIDGE, REMAPLIMIT, 1 << 0);
366 pci_or_config32(HOST_BRIDGE, TOM, 1 << 0);
367 pci_or_config32(HOST_BRIDGE, TOUUD, 1 << 0);
368 pci_or_config32(HOST_BRIDGE, BDSM, 1 << 0);
369 pci_or_config32(HOST_BRIDGE, BGSM, 1 << 0);
370 pci_or_config32(HOST_BRIDGE, TOLUD, 1 << 0);
371 }
372
373 enum raminit_status raminit_done(struct sysinfo *ctrl)
374 {
375 union mc_init_state_g_reg mc_init_state_g = {
376 .raw = mchbar_read32(MC_INIT_STATE_G),
377 };
378 mc_init_state_g.refresh_enable = 1;
379 mc_init_state_g.pu_mrc_done = 1;
380 mc_init_state_g.mrc_done = 1;
381 mchbar_write32(MC_INIT_STATE_G, mc_init_state_g.raw);
382
383 /* Lock the memory controller to enable normal operation */
384 mc_lockdown();
385
386 /* Poll for mc_init_done_ack to make sure memory initialization is complete */
387 printk(BIOS_DEBUG, "Waiting for mc_init_done acknowledgement... ");
388
389 struct stopwatch timer;
390 stopwatch_init_msecs_expire(&timer, 2000);
391 do {
392 mc_init_state_g.raw = mchbar_read32(MC_INIT_STATE_G);
393
394 /* DRAM will NOT work without the acknowledgement. There is no hope. */
395 if (stopwatch_expired(&timer))
396 die("\nTimed out waiting for mc_init_done acknowledgement\n");
397
398 } while (mc_init_state_g.mc_init_done_ack == 0);
399 printk(BIOS_DEBUG, "DONE!\n");
400
401 /* Provide some data for the graphics driver. Yes, it's hardcoded. */
402 mchbar_write32(SSKPD + 0, 0x05a2404f);
403 mchbar_write32(SSKPD + 4, 0x140000a0);
404 return RAMINIT_STATUS_SUCCESS;
405 }