blob: 51d6786f34ccb9ccd0c7409180ed6b876a400c9f [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
Angel Pons12bd8ab2020-11-13 23:10:52 +01003#include <assert.h>
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01004#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <console/console.h>
Angel Pons47a80a02020-12-07 13:15:23 +01006#include <cpu/intel/model_206ax/model_206ax.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02007#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02008#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01009#include <northbridge/intel/sandybridge/chip.h>
10#include <device/pci_def.h>
11#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020012#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020013
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010014#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010015#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010016#include "sandybridge.h"
17
Angel Pons7c49cb82020-03-16 23:17:32 +010018/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010019
20static void sfence(void)
21{
22 asm volatile ("sfence");
23}
24
Angel Pons7c49cb82020-03-16 23:17:32 +010025/* Toggle IO reset bit */
26static void toggle_io_reset(void)
27{
Angel Pons66780a02021-03-26 13:33:22 +010028 u32 r32 = mchbar_read32(MC_INIT_STATE_G);
29 mchbar_write32(MC_INIT_STATE_G, r32 | (1 << 5));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010030 udelay(1);
Angel Pons66780a02021-03-26 13:33:22 +010031 mchbar_write32(MC_INIT_STATE_G, r32 & ~(1 << 5));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
33}
34
35static u32 get_XOVER_CLK(u8 rankmap)
36{
37 return rankmap << 24;
38}
39
40static u32 get_XOVER_CMD(u8 rankmap)
41{
42 u32 reg;
43
Angel Pons7c49cb82020-03-16 23:17:32 +010044 /* Enable xover cmd */
Angel Pons5db1b152020-12-13 16:37:53 +010045 reg = 1 << 14;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010046
Angel Pons7c49cb82020-03-16 23:17:32 +010047 /* Enable xover ctl */
48 if (rankmap & 0x03)
49 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010050
Angel Pons7c49cb82020-03-16 23:17:32 +010051 if (rankmap & 0x0c)
52 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010053
54 return reg;
55}
56
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010057void dram_find_common_params(ramctr_timing *ctrl)
58{
59 size_t valid_dimms;
60 int channel, slot;
61 dimm_info *dimms = &ctrl->info;
62
63 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
64 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010065
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010066 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Ponsafb3d7e2021-03-28 13:43:13 +020067 const struct dimm_attr_ddr3_st *dimm = &dimms->dimm[channel][slot];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010068 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
69 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010070
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010071 valid_dimms++;
72
73 /* Find all possible CAS combinations */
74 ctrl->cas_supported &= dimm->cas_supported;
75
76 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010077 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
78 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
79 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010080 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
81 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010082 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
84 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
85 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
86 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
87 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030088 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
89 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010090 }
91
92 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010093 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
94
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010095 if (!valid_dimms)
96 die("No valid DIMMs found");
97}
98
Angel Pons88521882020-01-05 20:21:20 +010099void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100100{
101 u32 reg;
102 int channel;
103
104 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100105 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100106 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100107 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
Angel Pons66780a02021-03-26 13:33:22 +0100108 mchbar_write32(GDCRCKPICODE_ch(channel), reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109
Angel Pons7c49cb82020-03-16 23:17:32 +0100110 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100111 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100112 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
Angel Pons66780a02021-03-26 13:33:22 +0100113 mchbar_write32(GDCRCMDPICODING_ch(channel), reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114 }
115}
116
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100117static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100118{
Angel Pons89ae6b82020-03-21 13:23:32 +0100119 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100120
121 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100122 /*
123 * ODT stretch:
124 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
125 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100126 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100127 if (stretch == 2)
128 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100129
Angel Pons88521882020-01-05 20:21:20 +0100130 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Pons66780a02021-03-26 13:33:22 +0100131 mchbar_clrsetbits32(addr, 0xf << 10, stretch << 12 | stretch << 10);
132 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, mchbar_read32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100133 } else {
Angel Pons88521882020-01-05 20:21:20 +0100134 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100135 union tc_othp_reg tc_othp = {
Angel Pons66780a02021-03-26 13:33:22 +0100136 .raw = mchbar_read32(addr),
Angel Pons7a612742020-11-12 13:34:03 +0100137 };
138 tc_othp.odt_delay_d0 = stretch;
139 tc_othp.odt_delay_d1 = stretch;
Angel Pons66780a02021-03-26 13:33:22 +0100140 mchbar_write32(addr, tc_othp.raw);
141 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, mchbar_read32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100142 }
143}
144
145void dram_timing_regs(ramctr_timing *ctrl)
146{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100147 int channel;
148
Angel Pons81378062020-11-12 13:46:21 +0100149 /* BIN parameters */
150 const union tc_dbp_reg tc_dbp = {
151 .tRCD = ctrl->tRCD,
152 .tRP = ctrl->tRP,
153 .tAA = ctrl->CAS,
154 .tCWL = ctrl->CWL,
155 .tRAS = ctrl->tRAS,
156 };
157
158 /* Regular access parameters */
159 const union tc_rap_reg tc_rap = {
160 .tRRD = ctrl->tRRD,
161 .tRTP = ctrl->tRTP,
162 .tCKE = ctrl->tCKE,
163 .tWTR = ctrl->tWTR,
164 .tFAW = ctrl->tFAW,
165 .tWR = ctrl->tWR,
166 .tCMD = 3,
167 };
168
169 /* Other parameters */
170 const union tc_othp_reg tc_othp = {
Angel Ponsda437372021-01-24 18:34:51 +0100171 .tXPDLL = MIN(ctrl->tXPDLL, 31),
172 .tXP = MIN(ctrl->tXP, 7),
Angel Pons81378062020-11-12 13:46:21 +0100173 .tAONPD = ctrl->tAONPD,
Patrick Rudolphb7341da2023-12-21 14:53:45 +0100174 .tCPDED = 1,
Angel Pons2ad03a42020-11-19 11:07:27 +0100175 .tPRPDEN = 1,
Angel Pons81378062020-11-12 13:46:21 +0100176 };
177
178 /*
Angel Ponsda437372021-01-24 18:34:51 +0100179 * If tXP and tXPDLL are very high, they no longer fit in the bitfields
180 * of the TC_OTHP register. If so, we set bits in TC_DTP to compensate.
Angel Pons81378062020-11-12 13:46:21 +0100181 * This can only happen on Ivy Bridge, and when overclocking the RAM.
182 */
183 const union tc_dtp_reg tc_dtp = {
184 .overclock_tXP = ctrl->tXP >= 8,
185 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
186 };
187
188 /*
189 * TC-Refresh timing parameters:
190 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
191 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
192 */
193 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
194
195 const union tc_rftp_reg tc_rftp = {
196 .tREFI = ctrl->tREFI,
197 .tRFC = ctrl->tRFC,
198 .tREFIx9 = val32 / 1024,
199 };
200
201 /* Self-refresh timing parameters */
202 const union tc_srftp_reg tc_srftp = {
203 .tXSDLL = tDLLK,
204 .tXS_offset = ctrl->tXSOffset,
205 .tZQOPER = tDLLK - ctrl->tXSOffset,
206 .tMOD = ctrl->tMOD - 8,
207 };
208
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100209 FOR_ALL_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +0100210 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100211 mchbar_write32(TC_DBP_ch(channel), tc_dbp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100212
Angel Pons7a612742020-11-12 13:34:03 +0100213 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100214 mchbar_write32(TC_RAP_ch(channel), tc_rap.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100215
Angel Pons7a612742020-11-12 13:34:03 +0100216 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100217 mchbar_write32(TC_OTHP_ch(channel), tc_othp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100218
Angel Ponsca2f68a2020-03-22 13:15:12 +0100219 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Pons81378062020-11-12 13:46:21 +0100220 /* Debug parameters - only applies to Ivy Bridge */
Angel Pons66780a02021-03-26 13:33:22 +0100221 mchbar_write32(TC_DTP_ch(channel), tc_dtp.raw);
Angel Ponsca2f68a2020-03-22 13:15:12 +0100222 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100223
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100224 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100225
Angel Pons7a612742020-11-12 13:34:03 +0100226 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100227 mchbar_write32(TC_RFTP_ch(channel), tc_rftp.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +0100228
Angel Pons7a612742020-11-12 13:34:03 +0100229 union tc_rfp_reg tc_rfp = {
Angel Pons66780a02021-03-26 13:33:22 +0100230 .raw = mchbar_read32(TC_RFP_ch(channel)),
Angel Pons7a612742020-11-12 13:34:03 +0100231 };
232 tc_rfp.oref_ri = 0xff;
Angel Pons66780a02021-03-26 13:33:22 +0100233 mchbar_write32(TC_RFP_ch(channel), tc_rfp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100234
Angel Pons7a612742020-11-12 13:34:03 +0100235 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100236 mchbar_write32(TC_SRFTP_ch(channel), tc_srftp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100237 }
238}
239
240void dram_dimm_mapping(ramctr_timing *ctrl)
241{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100242 int channel;
243 dimm_info *info = &ctrl->info;
244
245 FOR_ALL_CHANNELS {
Angel Ponsafb3d7e2021-03-28 13:43:13 +0200246 struct dimm_attr_ddr3_st *dimmA, *dimmB;
Nico Huberac4f2162017-10-01 18:14:43 +0200247 u32 reg = 0;
248
Angel Pons7c49cb82020-03-16 23:17:32 +0100249 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100250 dimmA = &info->dimm[channel][0];
251 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100252 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100253 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100254 dimmA = &info->dimm[channel][1];
255 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100256 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100257 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100258
Nico Huberac4f2162017-10-01 18:14:43 +0200259 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100260 reg |= (dimmA->size_mb / 256) << 0;
261 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200262 reg |= (dimmA->width / 8 - 1) << 19;
263 }
264
265 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100266 reg |= (dimmB->size_mb / 256) << 8;
267 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200268 reg |= (dimmB->width / 8 - 1) << 20;
269 }
270
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200271 /*
272 * Rank interleave: Bit 16 of the physical address space sets
273 * the rank to use in a dual single rank DIMM configuration.
274 * That results in every 64KiB being interleaved between two ranks.
275 */
276 reg |= 1 << 21;
277 /* Enhanced interleave */
278 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200279
Angel Pons7c49cb82020-03-16 23:17:32 +0100280 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100281 ctrl->mad_dimm[channel] = reg;
282 } else {
283 ctrl->mad_dimm[channel] = 0;
284 }
285 }
286}
287
Patrick Rudolphdd662872017-10-28 18:20:11 +0200288void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100289{
290 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200291 u32 ecc;
292
293 if (ctrl->ecc_enabled)
294 ecc = training ? (1 << 24) : (3 << 24);
295 else
296 ecc = 0;
297
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100298 FOR_ALL_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +0100299 mchbar_write32(MAD_DIMM(channel), ctrl->mad_dimm[channel] | ecc);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100300 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200301
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200302 if (ctrl->ecc_enabled)
303 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100304}
305
Angel Pons88521882020-01-05 20:21:20 +0100306void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100307{
308 u32 reg, ch0size, ch1size;
309 u8 val;
310 reg = 0;
311 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100312
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100313 if (training) {
314 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
315 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
316 } else {
317 ch0size = ctrl->channel_size_mb[0];
318 ch1size = ctrl->channel_size_mb[1];
319 }
320
321 if (ch0size >= ch1size) {
Angel Pons66780a02021-03-26 13:33:22 +0100322 reg = mchbar_read32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100323 val = ch1size / 256;
324 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100325 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons66780a02021-03-26 13:33:22 +0100326 mchbar_write32(MAD_ZR, reg);
327 mchbar_write32(MAD_CHNL, 0x24);
Angel Pons7c49cb82020-03-16 23:17:32 +0100328
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100329 } else {
Angel Pons66780a02021-03-26 13:33:22 +0100330 reg = mchbar_read32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100331 val = ch0size / 256;
332 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100333 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons66780a02021-03-26 13:33:22 +0100334 mchbar_write32(MAD_ZR, reg);
335 mchbar_write32(MAD_CHNL, 0x21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100336 }
337}
338
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200339/*
340 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
341 * The ME/PCU/.. has the ability to change this.
342 * Return 0: ECC is optional
343 * Return 1: ECC is forced
344 */
345bool get_host_ecc_forced(void)
346{
347 /* read Capabilities A Register */
348 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
349 return !!(reg32 & (1 << 24));
350}
351
352/*
353 * Returns the ECC capability.
354 * The ME/PCU/.. has the ability to change this.
355 * Return 0: ECC is disabled
356 * Return 1: ECC is possible
357 */
358bool get_host_ecc_cap(void)
359{
360 /* read Capabilities A Register */
361 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
362 return !(reg32 & (1 << 25));
363}
364
Angel Pons5304ce12021-04-02 22:55:00 +0200365#define DEFAULT_PCI_MMIO_SIZE 2048
366
Angel Pons88521882020-01-05 20:21:20 +0100367void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100368{
Angel Pons7c49cb82020-03-16 23:17:32 +0100369 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
370 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
371 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100372 uint16_t ggc;
373
Angel Pons5304ce12021-04-02 22:55:00 +0200374 mmiosize = DEFAULT_PCI_MMIO_SIZE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100375
Felix Held87ddea22020-01-26 04:55:27 +0100376 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100377 if (!(ggc & 2)) {
378 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100379 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100380 } else {
381 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100382 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100383 }
384
385 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
386
387 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
388
389 mestolenbase = tom - me_uma_size;
390
Angel Pons7c49cb82020-03-16 23:17:32 +0100391 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
392
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100393 gfxstolenbase = toludbase - gfxstolen;
394 gttbase = gfxstolenbase - gttsize;
395
396 tsegbase = gttbase - tsegsize;
397
Angel Pons7c49cb82020-03-16 23:17:32 +0100398 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100399 tsegbasedelta = tsegbase & (tsegsize - 1);
400 tsegbase &= ~(tsegsize - 1);
401
402 gttbase -= tsegbasedelta;
403 gfxstolenbase -= tsegbasedelta;
404 toludbase -= tsegbasedelta;
405
Angel Pons7c49cb82020-03-16 23:17:32 +0100406 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100407 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100408 /* Reclaim is possible */
409 reclaim = 1;
410 remapbase = MAX(4096, tom - me_uma_size);
411 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
412 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100413 } else {
Angel Ponsc728e252021-01-03 16:47:09 +0100414 /* Reclaim not possible */
Angel Pons7c49cb82020-03-16 23:17:32 +0100415 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100416 touudbase = tom - me_uma_size;
417 }
418
Angel Pons7c49cb82020-03-16 23:17:32 +0100419 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100420 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
421
Angel Pons7c49cb82020-03-16 23:17:32 +0100422 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100423 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100424 val = tom & 0xfff;
425 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100426 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100427 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100428
Angel Ponsb31d1d72020-01-10 01:35:09 +0100429 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100430 val = tom & 0xfffff000;
431 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100432 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100433 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100434
Angel Pons7c49cb82020-03-16 23:17:32 +0100435 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100436 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100437 val = toludbase & 0xfff;
438 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100439 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100440 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100441
Angel Pons7c49cb82020-03-16 23:17:32 +0100442 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100443 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100444 val = touudbase & 0xfff;
445 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100446 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100447 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100448
Angel Pons7c49cb82020-03-16 23:17:32 +0100449 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100450 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100451 val = touudbase & 0xfffff000;
452 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100453 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100454 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100455
456 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100457 /* REMAP BASE */
458 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100459 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100460
Angel Pons7c49cb82020-03-16 23:17:32 +0100461 /* REMAP LIMIT */
462 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100463 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100464 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100465 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100466 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100467 val = tsegbase & 0xfff;
468 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100469 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100470 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100471
Angel Pons7c49cb82020-03-16 23:17:32 +0100472 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100473 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100474 val = gfxstolenbase & 0xfff;
475 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100476 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100477 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100478
Angel Pons7c49cb82020-03-16 23:17:32 +0100479 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100480 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100481 val = gttbase & 0xfff;
482 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100483 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100484 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100485
486 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100487 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100488 val = (0x80000 - me_uma_size) & 0xfffff000;
489 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100490 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100491 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100492
Angel Pons7c49cb82020-03-16 23:17:32 +0100493 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100494 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100495 val = mestolenbase & 0xfff;
496 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100497 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100498 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100499
Angel Ponsb31d1d72020-01-10 01:35:09 +0100500 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100501 val = mestolenbase & 0xfffff000;
502 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100503 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100504 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100505
Angel Pons7c49cb82020-03-16 23:17:32 +0100506 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100507 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100508 val = (0x80000 - me_uma_size) & 0xfff;
509 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100510 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
511 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100512 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100513 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100514 }
515}
516
Angel Pons88521882020-01-05 20:21:20 +0100517static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100518{
519 int channel, slotrank;
520
Angel Pons7c49cb82020-03-16 23:17:32 +0100521 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100522 channel = (ctrl->rankmap[0]) ? 0 : 1;
523
Angel Pons88521882020-01-05 20:21:20 +0100524 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100525
Angel Pons7c49cb82020-03-16 23:17:32 +0100526 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100527 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
528
Angel Ponsffd50152020-11-12 11:03:10 +0100529 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100530
Angel Ponsedd7cb42020-12-07 12:17:17 +0100531 /* This is actually using the IOSAV state machine as a timer */
Angel Pons38d901e2020-05-02 23:50:43 +0200532 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200533
Angel Pons88521882020-01-05 20:21:20 +0100534 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100535}
536
Angel Pons88521882020-01-05 20:21:20 +0100537void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100538{
Felix Held9fe248f2018-07-31 20:59:45 +0200539 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100540 int channel;
541
Angel Pons66780a02021-03-26 13:33:22 +0100542 while (!(mchbar_read32(RCOMP_TIMER) & (1 << 16)))
Angel Pons7c49cb82020-03-16 23:17:32 +0100543 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100544 do {
Angel Pons66780a02021-03-26 13:33:22 +0100545 reg = mchbar_read32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100546 } while ((reg & 0x14) == 0);
547
Angel Pons7c49cb82020-03-16 23:17:32 +0100548 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100549 reg = 0x112;
Angel Pons66780a02021-03-26 13:33:22 +0100550 mchbar_write32(MC_INIT_STATE_G, reg);
551 mchbar_write32(MC_INIT_STATE, 0);
Angel Pons7c49cb82020-03-16 23:17:32 +0100552 reg |= 2; /* DDR reset */
Angel Pons66780a02021-03-26 13:33:22 +0100553 mchbar_write32(MC_INIT_STATE_G, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100554
Angel Pons7c49cb82020-03-16 23:17:32 +0100555 /* Assert DIMM reset signal */
Angel Pons66780a02021-03-26 13:33:22 +0100556 mchbar_clrbits32(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100557
Angel Pons7c49cb82020-03-16 23:17:32 +0100558 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100559 udelay(200);
560
Angel Pons7c49cb82020-03-16 23:17:32 +0100561 /* Deassert DIMM reset signal */
Angel Pons66780a02021-03-26 13:33:22 +0100562 mchbar_setbits32(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100563
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100565 udelay(500);
566
Angel Pons7c49cb82020-03-16 23:17:32 +0100567 /* Enable DCLK */
Angel Pons66780a02021-03-26 13:33:22 +0100568 mchbar_setbits32(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 udelay(1);
572
573 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100574 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200575 reg = ctrl->rankmap[channel];
Angel Pons66780a02021-03-26 13:33:22 +0100576 mchbar_write32(MC_INIT_STATE_ch(channel), reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100577
Angel Pons7c49cb82020-03-16 23:17:32 +0100578 /* Wait 10ns for ranks to settle */
579 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100580
581 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons66780a02021-03-26 13:33:22 +0100582 mchbar_write32(MC_INIT_STATE_ch(channel), reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100583
Angel Pons7c49cb82020-03-16 23:17:32 +0100584 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100585 write_reset(ctrl);
586 }
587}
588
Angel Pons3d3bf482020-11-14 16:18:15 +0100589/*
590 * DDR3 Rank1 Address mirror swap the following pins:
591 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
592 */
593static void ddr3_mirror_mrreg(int *bank, u32 *addr)
594{
595 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
596 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
597}
598
Angel Pons7c49cb82020-03-16 23:17:32 +0100599static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100600{
Angel Pons88521882020-01-05 20:21:20 +0100601 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100602
Angel Pons3d3bf482020-11-14 16:18:15 +0100603 if (ctrl->rank_mirror[channel][slotrank])
604 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100605
Angel Pons8f0757e2020-11-11 23:03:36 +0100606 const struct iosav_ssq sequence[] = {
607 /* DRAM command MRS */
608 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200609 .sp_cmd_ctrl = {
610 .command = IOSAV_MRS,
611 },
612 .subseq_ctrl = {
613 .cmd_executions = 1,
614 .cmd_delay_gap = 4,
615 .post_ssq_wait = 4,
616 .data_direction = SSQ_NA,
617 },
618 .sp_cmd_addr = {
619 .address = val,
620 .rowbits = 6,
621 .bank = reg,
622 .rank = slotrank,
623 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100624 },
625 /* DRAM command MRS */
626 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200627 .sp_cmd_ctrl = {
628 .command = IOSAV_MRS,
629 .ranksel_ap = 1,
630 },
631 .subseq_ctrl = {
632 .cmd_executions = 1,
633 .cmd_delay_gap = 4,
634 .post_ssq_wait = 4,
635 .data_direction = SSQ_NA,
636 },
637 .sp_cmd_addr = {
638 .address = val,
639 .rowbits = 6,
640 .bank = reg,
641 .rank = slotrank,
642 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100643 },
644 /* DRAM command MRS */
645 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200646 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100647 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200648 },
649 .subseq_ctrl = {
650 .cmd_executions = 1,
651 .cmd_delay_gap = 4,
652 .post_ssq_wait = ctrl->tMOD,
653 .data_direction = SSQ_NA,
654 },
655 .sp_cmd_addr = {
656 .address = val,
657 .rowbits = 6,
658 .bank = reg,
659 .rank = slotrank,
660 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100661 },
662 };
663 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200664
Angel Pons9f4ed3b2020-12-07 12:34:36 +0100665 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100666}
667
Angel Pons09fc4b92020-11-19 12:02:07 +0100668/* Obtain optimal power down mode for current configuration */
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100669static enum power_down_mode get_power_down_mode(ramctr_timing *ctrl, int channel)
Angel Pons09fc4b92020-11-19 12:02:07 +0100670{
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100671 int slotrank;
672
Angel Pons09fc4b92020-11-19 12:02:07 +0100673 if (ctrl->tXP > 8)
674 return PDM_NONE;
675
676 if (ctrl->tXPDLL > 32)
677 return PDM_PPD;
678
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100679 FOR_ALL_POPULATED_RANKS
680 if (!ctrl->info.dimm[channel][slotrank >> 1].flags.dll_off_mode)
681 return PDM_APD_PPD;
682
Angel Pons09fc4b92020-11-19 12:02:07 +0100683 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
684 return PDM_DLL_OFF;
685
686 return PDM_APD_PPD;
687}
688
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100689static u32 make_mr0(ramctr_timing *ctrl, int channel, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100690{
691 u16 mr0reg, mch_cas, mch_wr;
692 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Angel Pons09fc4b92020-11-19 12:02:07 +0100693
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100694 const enum power_down_mode power_down = get_power_down_mode(ctrl, channel);
Angel Pons09fc4b92020-11-19 12:02:07 +0100695
696 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100697
Angel Pons7c49cb82020-03-16 23:17:32 +0100698 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100699 if (ctrl->CAS < 12) {
Elyes Haouas3a998072022-11-18 15:11:02 +0100700 mch_cas = (u16)((ctrl->CAS - 4) << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100701 } else {
Elyes Haouas3a998072022-11-18 15:11:02 +0100702 mch_cas = (u16)(ctrl->CAS - 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100703 mch_cas = ((mch_cas << 1) | 0x1);
704 }
705
Angel Pons7c49cb82020-03-16 23:17:32 +0100706 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100707 mch_wr = mch_wr_t[ctrl->tWR - 5];
708
Angel Pons2bf28ed2020-11-12 13:49:59 +0100709 /* DLL Reset - self clearing - set after CLK frequency has been changed */
710 mr0reg = 1 << 8;
711
712 mr0reg |= (mch_cas & 0x1) << 2;
713 mr0reg |= (mch_cas & 0xe) << 3;
714 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100715
Angel Pons09fc4b92020-11-19 12:02:07 +0100716 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
717 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100718 return mr0reg;
719}
720
721static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
722{
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100723 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, channel, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100724}
725
Angel Ponsf9997482020-11-12 16:02:52 +0100726static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100727{
728 /* Get ODT based on rankmap */
729 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
730
731 if (dimms_per_ch == 1) {
732 return (const odtmap){60, 60};
733 } else {
734 return (const odtmap){120, 30};
735 }
736}
737
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100738static u32 encode_odt(u32 odt)
739{
740 switch (odt) {
741 case 30:
Angel Ponsc728e252021-01-03 16:47:09 +0100742 return (1 << 9) | (1 << 2); /* RZQ/8, RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100743 case 60:
Angel Ponsc728e252021-01-03 16:47:09 +0100744 return (1 << 2); /* RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100745 case 120:
Angel Ponsc728e252021-01-03 16:47:09 +0100746 return (1 << 6); /* RZQ/2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100747 default:
748 case 0:
749 return 0;
750 }
751}
752
753static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
754{
755 odtmap odt;
756 u32 mr1reg;
757
Angel Ponsf9997482020-11-12 16:02:52 +0100758 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100759 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100760
761 mr1reg |= encode_odt(odt.rttnom);
762
763 return mr1reg;
764}
765
766static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
767{
768 u16 mr1reg;
769
770 mr1reg = make_mr1(ctrl, rank, channel);
771
772 write_mrreg(ctrl, channel, rank, 1, mr1reg);
773}
774
775static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
776{
Angel Pons868bca22020-11-13 13:38:04 +0100777 const u16 pasr = 0;
778 const u16 cwl = ctrl->CWL - 5;
779 const odtmap odt = get_ODT(ctrl, channel);
780
Angel Ponsdca3cb52020-11-13 13:42:07 +0100781 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100782 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
783 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100784
Angel Pons868bca22020-11-13 13:38:04 +0100785 u16 mr2reg = 0;
786 mr2reg |= pasr;
787 mr2reg |= cwl << 3;
788 mr2reg |= ctrl->auto_self_refresh << 6;
789 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100790 mr2reg |= (odt.rttwr / 60) << 9;
791
792 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100793
794 /* Program MR2 shadow */
Angel Pons66780a02021-03-26 13:33:22 +0100795 u32 reg32 = mchbar_read32(TC_MR2_SHADOW_ch(channel));
Angel Pons7f1363d2020-11-13 13:31:58 +0100796
797 reg32 &= 3 << 14 | 3 << 6;
798
799 reg32 |= mr2reg & ~(3 << 6);
800
Angel Pons927b1c02020-12-10 22:11:27 +0100801 if (srt)
802 reg32 |= 1 << (rank / 2 + 6);
803
804 if (ctrl->rank_mirror[channel][rank])
805 reg32 |= 1 << (rank / 2 + 14);
806
Angel Pons66780a02021-03-26 13:33:22 +0100807 mchbar_write32(TC_MR2_SHADOW_ch(channel), reg32);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100808}
809
810static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
811{
812 write_mrreg(ctrl, channel, rank, 3, 0);
813}
814
Angel Pons88521882020-01-05 20:21:20 +0100815void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100816{
817 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100818 int channel;
819
820 FOR_ALL_POPULATED_CHANNELS {
821 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100822 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100823 dram_mr2(ctrl, slotrank, channel);
824
Angel Pons7c49cb82020-03-16 23:17:32 +0100825 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100826 dram_mr3(ctrl, slotrank, channel);
827
Angel Pons7c49cb82020-03-16 23:17:32 +0100828 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100829 dram_mr1(ctrl, slotrank, channel);
830
Angel Pons7c49cb82020-03-16 23:17:32 +0100831 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100832 dram_mr0(ctrl, slotrank, channel);
833 }
834 }
835
Angel Pons8f0757e2020-11-11 23:03:36 +0100836 const struct iosav_ssq zqcl_sequence[] = {
837 /* DRAM command NOP (without ODT nor chip selects) */
838 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200839 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100840 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200841 },
842 .subseq_ctrl = {
843 .cmd_executions = 1,
844 .cmd_delay_gap = 4,
845 .post_ssq_wait = 15,
846 .data_direction = SSQ_NA,
847 },
848 .sp_cmd_addr = {
849 .address = 2,
850 .rowbits = 6,
851 .bank = 0,
852 .rank = 0,
853 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100854 },
855 /* DRAM command ZQCL */
856 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200857 .sp_cmd_ctrl = {
858 .command = IOSAV_ZQCS,
859 .ranksel_ap = 1,
860 },
861 .subseq_ctrl = {
862 .cmd_executions = 1,
863 .cmd_delay_gap = 4,
864 .post_ssq_wait = 400,
865 .data_direction = SSQ_NA,
866 },
867 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +0100868 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +0200869 .rowbits = 6,
870 .bank = 0,
871 .rank = 0,
872 },
873 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100874 .inc_rank = 1,
875 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200876 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100877 },
878 };
879 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100880
Angel Pons38d901e2020-05-02 23:50:43 +0200881 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100882
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100883 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +0100884 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100885 }
886
Angel Pons7c49cb82020-03-16 23:17:32 +0100887 /* Refresh enable */
Angel Pons66780a02021-03-26 13:33:22 +0100888 mchbar_setbits32(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100889
890 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +0100891 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100892
Angel Pons88521882020-01-05 20:21:20 +0100893 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100894
895 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
896
Angel Pons88521882020-01-05 20:21:20 +0100897 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100898
Angel Ponsffd50152020-11-12 11:03:10 +0100899 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200900
Angel Ponsa853e7a2020-12-07 12:28:38 +0100901 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100902 }
903}
904
Felix Held3b906032020-01-14 17:05:43 +0100905static const u32 lane_base[] = {
906 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
907 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
908 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100909};
910
Angel Pons42d033a2021-01-03 15:26:37 +0100911/* Maximum delay for command, control, clock */
912#define CCC_MAX_PI (2 * QCLK_PI - 1)
913
Angel Pons88521882020-01-05 20:21:20 +0100914void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100915{
Angel Pons7584e552020-11-19 21:34:32 +0100916 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100917 int lane;
918 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100919
Angel Pons7584e552020-11-19 21:34:32 +0100920 u32 ctl_delay[NUM_SLOTS] = { 0 };
921 int cmd_delay = 0;
922
923 /* Enable CLK XOVER */
924 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
925 u32 clk_logic_dly = 0;
926
927 /*
Angel Pons7519ca42021-01-12 01:21:24 +0100928 * Compute command timing as abs() of the most negative PI code
929 * across all ranks. Use zero if none of the values is negative.
Angel Pons7584e552020-11-19 21:34:32 +0100930 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100931 FOR_ALL_POPULATED_RANKS {
Angel Pons7519ca42021-01-12 01:21:24 +0100932 cmd_delay = MAX(cmd_delay, -ctrl->timings[channel][slotrank].pi_coding);
Angel Pons7584e552020-11-19 21:34:32 +0100933 }
Angel Pons42d033a2021-01-03 15:26:37 +0100934 if (cmd_delay > CCC_MAX_PI) {
Angel Pons7584e552020-11-19 21:34:32 +0100935 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
Angel Pons42d033a2021-01-03 15:26:37 +0100936 cmd_delay = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100937 }
938
Angel Pons89200d22021-01-12 01:04:04 +0100939 for (slot = 0; slot < NUM_SLOTS; slot++) {
940 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
941 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
Angel Pons7584e552020-11-19 21:34:32 +0100942
Angel Pons89200d22021-01-12 01:04:04 +0100943 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
Angel Pons7584e552020-11-19 21:34:32 +0100944
Angel Pons89200d22021-01-12 01:04:04 +0100945 if (slot_map & 1)
946 ctl_delay[slot] += pi_coding_0 + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100947
Angel Pons89200d22021-01-12 01:04:04 +0100948 if (slot_map & 2)
949 ctl_delay[slot] += pi_coding_1 + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100950
Angel Pons89200d22021-01-12 01:04:04 +0100951 /* If both ranks in a slot are populated, use the average */
952 if (slot_map == 3)
953 ctl_delay[slot] /= 2;
Angel Pons7584e552020-11-19 21:34:32 +0100954
Angel Pons89200d22021-01-12 01:04:04 +0100955 if (ctl_delay[slot] > CCC_MAX_PI) {
956 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
957 channel, slot, ctl_delay[slot]);
958 ctl_delay[slot] = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100959 }
Angel Pons89200d22021-01-12 01:04:04 +0100960 }
961 FOR_ALL_POPULATED_RANKS {
Angel Pons0a7d99c2021-01-12 01:13:08 +0100962 int clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100963
Angel Pons0a7d99c2021-01-12 01:13:08 +0100964 /*
965 * Clock is a differential signal, whereas command and control are not.
966 * This affects its timing, and it is also why it needs a magic offset.
967 */
968 clk_delay += ctrl->pi_code_offset;
969
970 /* Can never happen with valid values */
971 if (clk_delay < 0) {
972 printk(BIOS_ERR, "C%dR%d clock delay underflow: %d\n",
Angel Pons89200d22021-01-12 01:04:04 +0100973 channel, slotrank, clk_delay);
Angel Pons0a7d99c2021-01-12 01:13:08 +0100974 clk_delay = 0;
Angel Pons7584e552020-11-19 21:34:32 +0100975 }
Angel Pons89200d22021-01-12 01:04:04 +0100976
Angel Pons0a7d99c2021-01-12 01:13:08 +0100977 /* Clock can safely wrap around because it is a periodic signal */
978 clk_delay %= CCC_MAX_PI + 1;
979
Angel Pons89200d22021-01-12 01:04:04 +0100980 clk_pi_coding |= (clk_delay % QCLK_PI) << (6 * slotrank);
981 clk_logic_dly |= (clk_delay / QCLK_PI) << slotrank;
Angel Pons7584e552020-11-19 21:34:32 +0100982 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100983
Angel Pons7c49cb82020-03-16 23:17:32 +0100984 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100985 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
986 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
987 };
Angel Pons42d033a2021-01-03 15:26:37 +0100988 cmd_pi_coding.cmd_pi_code = cmd_delay % QCLK_PI;
989 cmd_pi_coding.cmd_logic_delay = cmd_delay / QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100990
Angel Pons42d033a2021-01-03 15:26:37 +0100991 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % QCLK_PI;
992 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % QCLK_PI;
993 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / QCLK_PI;
994 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / QCLK_PI;
Angel Pons737f1112020-11-13 14:07:30 +0100995
Angel Pons66780a02021-03-26 13:33:22 +0100996 mchbar_write32(GDCRCMDPICODING_ch(channel), cmd_pi_coding.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100997
Angel Pons66780a02021-03-26 13:33:22 +0100998 mchbar_write32(GDCRCKPICODE_ch(channel), clk_pi_coding);
999 mchbar_write32(GDCRCKLOGICDELAY_ch(channel), clk_logic_dly);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001000
Angel Pons66780a02021-03-26 13:33:22 +01001001 reg_io_latency = mchbar_read32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001002 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001003
Angel Pons88521882020-01-05 20:21:20 +01001004 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001005
1006 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001007 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001008
Angel Pons88521882020-01-05 20:21:20 +01001009 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001010 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001011
1012 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001013 const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
1014 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p;
1015 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n;
Angel Pons9fcc1102020-11-19 22:23:13 +01001016 const union gdcr_rx_reg gdcr_rx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001017 .rcven_pi_code = rcven % QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001018 .rx_dqs_p_pi_code = dqs_p,
Angel Pons42d033a2021-01-03 15:26:37 +01001019 .rcven_logic_delay = rcven / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001020 .rx_dqs_n_pi_code = dqs_n,
1021 };
Angel Pons66780a02021-03-26 13:33:22 +01001022 mchbar_write32(lane_base[lane] + GDCRRX(channel, slotrank),
1023 gdcr_rx.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001024
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001025 const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1026 const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq;
Angel Pons9fcc1102020-11-19 22:23:13 +01001027 const union gdcr_tx_reg gdcr_tx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001028 .tx_dq_pi_code = tx_dq % QCLK_PI,
1029 .tx_dqs_pi_code = tx_dqs % QCLK_PI,
1030 .tx_dqs_logic_delay = tx_dqs / QCLK_PI,
1031 .tx_dq_logic_delay = tx_dq / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001032 };
Angel Pons66780a02021-03-26 13:33:22 +01001033 mchbar_write32(lane_base[lane] + GDCRTX(channel, slotrank),
1034 gdcr_tx.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001035 }
1036 }
Angel Pons66780a02021-03-26 13:33:22 +01001037 mchbar_write32(SC_ROUNDT_LAT_ch(channel), reg_roundtrip_latency);
1038 mchbar_write32(SC_IO_LATENCY_ch(channel), reg_io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001039}
1040
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001041static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001042{
Angel Pons88521882020-01-05 20:21:20 +01001043 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001044
Angel Pons3aed6ac2020-12-07 02:00:41 +01001045 /* Send a burst of 16 back-to-back read commands (4 DCLK apart) */
Angel Ponsffd50152020-11-12 11:03:10 +01001046 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001047
Angel Ponsa853e7a2020-12-07 12:28:38 +01001048 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001049}
1050
Angel Pons7c49cb82020-03-16 23:17:32 +01001051static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001052{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001053 u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
Angel Pons7c49cb82020-03-16 23:17:32 +01001054
Angel Pons66780a02021-03-26 13:33:22 +01001055 return (mchbar_read32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001056 GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001057}
1058
1059struct run {
1060 int middle;
1061 int end;
1062 int start;
1063 int all;
1064 int length;
1065};
1066
1067static struct run get_longest_zero_run(int *seq, int sz)
1068{
1069 int i, ls;
1070 int bl = 0, bs = 0;
1071 struct run ret;
1072
1073 ls = 0;
1074 for (i = 0; i < 2 * sz; i++)
1075 if (seq[i % sz]) {
1076 if (i - ls > bl) {
1077 bl = i - ls;
1078 bs = ls;
1079 }
1080 ls = i + 1;
1081 }
1082 if (bl == 0) {
1083 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001084 ret.start = 0;
1085 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001086 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001087 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001088 return ret;
1089 }
1090
Angel Pons7c49cb82020-03-16 23:17:32 +01001091 ret.start = bs % sz;
1092 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001093 ret.middle = (bs + (bl - 1) / 2) % sz;
1094 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001095 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001096
1097 return ret;
1098}
1099
Angel Pons42d033a2021-01-03 15:26:37 +01001100#define RCVEN_COARSE_PI_LENGTH (2 * QCLK_PI)
1101
Angel Ponsf3053392020-11-13 23:31:12 +01001102static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001103{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001104 int rcven;
Angel Pons42d033a2021-01-03 15:26:37 +01001105 int statistics[NUM_LANES][RCVEN_COARSE_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001106 int lane;
1107
Angel Pons42d033a2021-01-03 15:26:37 +01001108 for (rcven = 0; rcven < RCVEN_COARSE_PI_LENGTH; rcven++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001109 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001110 ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001111 }
1112 program_timings(ctrl, channel);
1113
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001114 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001115
1116 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001117 statistics[lane][rcven] =
1118 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001119 }
1120 }
1121 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001122 struct run rn = get_longest_zero_run(statistics[lane], RCVEN_COARSE_PI_LENGTH);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001123 ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001124 upperA[lane] = rn.end;
1125 if (upperA[lane] < rn.middle)
Angel Pons42d033a2021-01-03 15:26:37 +01001126 upperA[lane] += 2 * QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001127
Angel Pons7e439c92020-12-07 11:56:01 +01001128 printram("rcven: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001129 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001130 }
1131}
1132
Angel Ponsf3053392020-11-13 23:31:12 +01001133static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001134{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001135 int rcven_delta;
Angel Pons86e3d742021-01-03 14:55:12 +01001136 int statistics[NUM_LANES][51] = {0};
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001137 int lane, i;
1138
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001139 for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001140 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001141 ctrl->timings[channel][slotrank].lanes[lane].rcven
Angel Pons42d033a2021-01-03 15:26:37 +01001142 = upperA[lane] + rcven_delta + QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001143 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001144 program_timings(ctrl, channel);
1145
1146 for (i = 0; i < 100; i++) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001147 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001148 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001149 statistics[lane][rcven_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001150 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001151 }
1152 }
1153 }
1154 FOR_ALL_LANES {
1155 int last_zero, first_all;
1156
1157 for (last_zero = -25; last_zero <= 25; last_zero++)
1158 if (statistics[lane][last_zero + 25])
1159 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001160
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001161 last_zero--;
1162 for (first_all = -25; first_all <= 25; first_all++)
1163 if (statistics[lane][first_all + 25] == 100)
1164 break;
1165
Angel Pons7c49cb82020-03-16 23:17:32 +01001166 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001167
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001168 ctrl->timings[channel][slotrank].lanes[lane].rcven =
Angel Pons7c49cb82020-03-16 23:17:32 +01001169 (last_zero + first_all) / 2 + upperA[lane];
1170
Angel Pons7e439c92020-12-07 11:56:01 +01001171 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001172 lane, ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001173 }
1174}
1175
Angel Pons3aed6ac2020-12-07 02:00:41 +01001176/*
1177 * Once the DQS high phase has been found (for each DRAM) the next stage
1178 * is to find out the round trip latency, by locating the preamble cycle.
1179 * This is achieved by trying smaller and smaller roundtrip values until
1180 * the strobe sampling is done on the preamble cycle.
1181 */
Angel Ponsf3053392020-11-13 23:31:12 +01001182static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001183{
1184 int works[NUM_LANES];
1185 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001186
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001187 while (1) {
1188 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190 program_timings(ctrl, channel);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001191 test_rcven(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001192
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001194 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1195
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001196 if (works[lane])
1197 some_works = 1;
1198 else
1199 all_works = 0;
1200 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001201
1202 /* If every lane is working, exit */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 if (all_works)
1204 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001205
Angel Pons3aed6ac2020-12-07 02:00:41 +01001206 /*
1207 * If all bits are one (everyone is failing), decrement
1208 * the roundtrip value by two, and do another iteration.
1209 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001210 if (!some_works) {
Angel Pons3aed6ac2020-12-07 02:00:41 +01001211 /* Guard against roundtrip latency underflow */
Angel Pons88521882020-01-05 20:21:20 +01001212 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Angel Pons30791632020-12-12 12:28:29 +01001213 printk(BIOS_EMERG, "Roundtrip latency underflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 channel, slotrank);
1215 return MAKE_ERR;
1216 }
Angel Pons88521882020-01-05 20:21:20 +01001217 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001218 printram("4024 -= 2;\n");
1219 continue;
1220 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001221
1222 /*
1223 * Else (if some lanes are failing), increase the rank's
1224 * I/O latency by 2, and increase rcven logic delay by 2
1225 * on the working lanes, then perform another iteration.
1226 */
Felix Heldef4fe3e2019-12-31 14:15:05 +01001227 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001228 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001229
Angel Pons3aed6ac2020-12-07 02:00:41 +01001230 /* Guard against I/O latency overflow */
Angel Pons5db1b152020-12-13 16:37:53 +01001231 if (ctrl->timings[channel][slotrank].io_latency >= 16) {
Angel Pons30791632020-12-12 12:28:29 +01001232 printk(BIOS_EMERG, "I/O latency overflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001233 channel, slotrank);
1234 return MAKE_ERR;
1235 }
1236 FOR_ALL_LANES if (works[lane]) {
Angel Pons42d033a2021-01-03 15:26:37 +01001237 ctrl->timings[channel][slotrank].lanes[lane].rcven += 2 * QCLK_PI;
1238 upperA[lane] += 2 * QCLK_PI;
Angel Pons891f2bc2020-01-10 01:27:28 +01001239 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001240 }
1241 }
1242 return 0;
1243}
1244
Angel Pons12bd8ab2020-11-13 23:10:52 +01001245static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001246{
1247 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001248 u16 logic_delay_min = 7;
1249 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001250
1251 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001252 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001253
1254 logic_delay_min = MIN(logic_delay_min, logic_delay);
1255 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001256 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001257
1258 if (logic_delay_max < logic_delay_min) {
1259 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1260 logic_delay_max, logic_delay_min, channel, slotrank);
1261 }
1262
1263 assert(logic_delay_max >= logic_delay_min);
1264
1265 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001266}
1267
Angel Pons12bd8ab2020-11-13 23:10:52 +01001268static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001269{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001270 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001271
Angel Pons7c49cb82020-03-16 23:17:32 +01001272 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001273 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001274
Angel Pons12bd8ab2020-11-13 23:10:52 +01001275 if (prev < post)
1276 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001277
Angel Pons12bd8ab2020-11-13 23:10:52 +01001278 else if (prev > post)
1279 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001280
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001281 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001282 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001283
Angel Pons12bd8ab2020-11-13 23:10:52 +01001284 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1285 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1286 printram("4024 += %d;\n", latency_offset);
1287 printram("4028 += %d;\n", latency_offset);
1288
1289 return post;
1290}
1291
1292static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1293{
1294 u16 logic_delay_min = 7;
1295 int lane;
1296
1297 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001298 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001299
1300 logic_delay_min = MIN(logic_delay_min, logic_delay);
1301 }
1302
1303 if (logic_delay_min >= 2) {
1304 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1305 logic_delay_min, channel, slotrank);
1306 }
1307
1308 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001309 ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001310 }
1311 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1312 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001313}
1314
Angel Pons7f5a97c2020-11-13 16:58:46 +01001315int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001316{
1317 int channel, slotrank, lane;
1318 int err;
1319
1320 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1321 int all_high, some_high;
1322 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001323 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001324
Angel Pons88521882020-01-05 20:21:20 +01001325 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001326
Angel Ponsffd50152020-11-12 11:03:10 +01001327 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001328
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001329 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001330
Angel Pons58b609b2020-11-13 14:35:29 +01001331 const union gdcr_training_mod_reg training_mod = {
1332 .receive_enable_mode = 1,
1333 .training_rank_sel = slotrank,
1334 .odt_always_on = 1,
1335 };
Angel Pons66780a02021-03-26 13:33:22 +01001336 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001337
Felix Heldef4fe3e2019-12-31 14:15:05 +01001338 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001339 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001340 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001341
Angel Ponsf3053392020-11-13 23:31:12 +01001342 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001343
Felix Held2bb3cdf2018-07-28 00:23:59 +02001344 all_high = 1;
1345 some_high = 0;
1346 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001347 if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= QCLK_PI)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001348 some_high = 1;
1349 else
1350 all_high = 0;
1351 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001352
1353 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001354 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001355 printram("4028--;\n");
1356 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001357 ctrl->timings[channel][slotrank].lanes[lane].rcven -= QCLK_PI;
1358 upperA[lane] -= QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001359 }
1360 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001361 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001362 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001363 printram("4024++;\n");
1364 printram("4028++;\n");
1365 }
1366
1367 program_timings(ctrl, channel);
1368
Angel Pons12bd8ab2020-11-13 23:10:52 +01001369 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001370
Angel Ponsf3053392020-11-13 23:31:12 +01001371 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001372 if (err)
1373 return err;
1374
Angel Pons12bd8ab2020-11-13 23:10:52 +01001375 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001376
Angel Ponsf3053392020-11-13 23:31:12 +01001377 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001378
Angel Pons12bd8ab2020-11-13 23:10:52 +01001379 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380
Angel Pons12bd8ab2020-11-13 23:10:52 +01001381 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
Angel Pons12bd8ab2020-11-13 23:10:52 +01001383 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Pons7e439c92020-12-07 11:56:01 +01001385 printram("4/8: %d, %d, % 4d, % 4d\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001386 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001387 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388
1389 printram("final results:\n");
1390 FOR_ALL_LANES
Angel Pons7e439c92020-12-07 11:56:01 +01001391 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001392 ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001393
Angel Pons66780a02021-03-26 13:33:22 +01001394 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001395
1396 toggle_io_reset();
1397 }
1398
1399 FOR_ALL_POPULATED_CHANNELS {
1400 program_timings(ctrl, channel);
1401 }
Angel Ponsc6742232020-11-15 13:26:21 +01001402
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001403 return 0;
1404}
1405
Angel Pons011661c2020-11-15 18:21:35 +01001406static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001407{
1408 int lane;
1409
1410 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001411 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane), 0);
1412 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001413 }
1414
Angel Pons88521882020-01-05 20:21:20 +01001415 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001416
Angel Ponsffd50152020-11-12 11:03:10 +01001417 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1418 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001419
Angel Ponsa853e7a2020-12-07 12:28:38 +01001420 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001421
Angel Pons801a5cb2020-11-15 15:48:29 +01001422 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001423
Angel Ponsa853e7a2020-12-07 12:28:38 +01001424 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001425}
1426
Angel Pons011661c2020-11-15 18:21:35 +01001427static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001428{
1429 int min = data[0];
1430 int max = min;
1431 int i;
1432 for (i = 1; i < count; i++) {
1433 if (min > data[i])
1434 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001435
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001436 if (max < data[i])
1437 max = data[i];
1438 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001439 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001440 for (i = 0; i < count; i++)
1441 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001442
Angel Pons891f2bc2020-01-10 01:27:28 +01001443 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001444}
1445
Angel Pons011661c2020-11-15 18:21:35 +01001446static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001447{
Angel Pons011661c2020-11-15 18:21:35 +01001448 int tx_dq;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001449 int stats[NUM_LANES][MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001450 int lane;
1451
Angel Pons88521882020-01-05 20:21:20 +01001452 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001453
Angel Ponsffd50152020-11-12 11:03:10 +01001454 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001455
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001456 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001457
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001458 for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) {
1459 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001460 program_timings(ctrl, channel);
1461
Angel Pons011661c2020-11-15 18:21:35 +01001462 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001463
1464 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001465 stats[lane][tx_dq] = mchbar_read32(
1466 IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001467 }
1468 }
1469 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001470 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1471
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001472 if (rn.all || rn.length < 8) {
Angel Pons30791632020-12-12 12:28:29 +01001473 printk(BIOS_EMERG, "tx_dq write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001474 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001475 /*
1476 * With command training not being done yet, the lane can be erroneous.
1477 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001478 */
Angel Pons011661c2020-11-15 18:21:35 +01001479 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001480 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1481
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001482 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001483 printk(BIOS_EMERG, "tx_dq recovery failed\n");
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001484 return MAKE_ERR;
1485 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001486 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001487 ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle;
Angel Pons7e439c92020-12-07 11:56:01 +01001488 printram("tx_dq: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001489 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001490 }
1491 return 0;
1492}
1493
Angel Pons88521882020-01-05 20:21:20 +01001494static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001495{
1496 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001497
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001498 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1499 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001500
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001501 return ret;
1502}
1503
Angel Pons765d4652020-11-11 14:44:35 +01001504/* Each cacheline is 64 bits long */
1505static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1506{
Angel Pons66780a02021-03-26 13:33:22 +01001507 mchbar_write8(IOSAV_DATA_CTL_ch(channel), num_cachelines / 8 - 1);
Angel Pons765d4652020-11-11 14:44:35 +01001508}
1509
Angel Pons88521882020-01-05 20:21:20 +01001510static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001511{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301512 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001513 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001514 uintptr_t addr;
Angel Pons7c49cb82020-03-16 23:17:32 +01001515
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001516 for (j = 0; j < 16; j++) {
1517 addr = 0x04000000 + channel_offset + 4 * j;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001518 write32p(addr, j & 2 ? b : a);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001519 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001520
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001521 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001522
1523 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001524}
1525
Angel Pons88521882020-01-05 20:21:20 +01001526static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001527{
1528 int ret = 0;
1529 int channel;
1530 FOR_ALL_POPULATED_CHANNELS ret++;
1531 return ret;
1532}
1533
Angel Pons88521882020-01-05 20:21:20 +01001534static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001535{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301536 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001537 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
1538 unsigned int channel_step = 64 * num_of_channels(ctrl);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001539 uintptr_t addr;
Angel Pons7c49cb82020-03-16 23:17:32 +01001540
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001541 for (j = 0; j < 16; j++) {
1542 addr = 0x04000000 + channel_offset + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001543 write32p(addr, 0xffffffff);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001544 }
1545 for (j = 0; j < 16; j++) {
1546 addr = 0x04000000 + channel_offset + channel_step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001547 write32p(addr, 0);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001548 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001549 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001550
1551 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001552}
1553
Angel Pons42d033a2021-01-03 15:26:37 +01001554#define TX_DQS_PI_LENGTH (2 * QCLK_PI)
1555
Angel Pons820bce72020-11-14 17:02:55 +01001556static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001558 int tx_dqs;
Angel Pons42d033a2021-01-03 15:26:37 +01001559 int statistics[NUM_LANES][TX_DQS_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001560 int lane;
1561
Angel Pons58b609b2020-11-13 14:35:29 +01001562 const union gdcr_training_mod_reg training_mod = {
1563 .write_leveling_mode = 1,
1564 .training_rank_sel = slotrank,
1565 .enable_dqs_wl = 5,
1566 .odt_always_on = 1,
1567 .force_drive_enable = 1,
1568 };
Angel Pons66780a02021-03-26 13:33:22 +01001569 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001570
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001571 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1572 int bank = 1;
1573
1574 if (ctrl->rank_mirror[channel][slotrank])
1575 ddr3_mirror_mrreg(&bank, &mr1reg);
1576
1577 wait_for_iosav(channel);
1578
1579 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1580
Angel Pons42d033a2021-01-03 15:26:37 +01001581 for (tx_dqs = 0; tx_dqs < TX_DQS_PI_LENGTH; tx_dqs++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001582 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001583 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001584 }
1585 program_timings(ctrl, channel);
1586
Angel Ponsa853e7a2020-12-07 12:28:38 +01001587 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001588
1589 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001590 statistics[lane][tx_dqs] = !((mchbar_read32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001591 GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >>
1592 (tx_dqs % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001593 }
1594 }
1595 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001596 struct run rn = get_longest_zero_run(statistics[lane], TX_DQS_PI_LENGTH);
Angel Pons7c49cb82020-03-16 23:17:32 +01001597 /*
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001598 * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value
1599 * of tx_dqs by a small value, which might cause the 6-bit value to overflow if
Angel Pons7c49cb82020-03-16 23:17:32 +01001600 * it's close to 0x3f. Increment the value by a small offset if it's likely
1601 * to overflow, to make sure it won't overflow while running tests and bricks
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001602 * the system due to a non matching tx_dq.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001603 *
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001604 * TODO: find out why some tests (edge write discovery) increment tx_dqs.
Angel Pons7c49cb82020-03-16 23:17:32 +01001605 */
1606 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001607 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001608 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001609 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001610
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001611 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001612 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01001613 printk(BIOS_EMERG, "JEDEC write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001614 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001615
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001616 return MAKE_ERR;
1617 }
Angel Pons7e439c92020-12-07 11:56:01 +01001618 printram("tx_dqs: %d, %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01001619 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001620 }
1621 return 0;
1622}
1623
Angel Pons820bce72020-11-14 17:02:55 +01001624static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001625{
1626 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001627 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001628 if (val == 0xffffffffffffffffLL)
1629 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001630 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001631 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001632 for (i = 0; i < 8; i++)
1633 if (val << (8 * (7 - i) + 4))
1634 return -i;
1635 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001636 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001637 for (i = 0; i < 8; i++)
1638 if (val >> (8 * (7 - i) + 4))
1639 return i;
1640 }
1641 return 8;
1642}
1643
Angel Ponsbf13ef02020-11-11 18:40:06 +01001644static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001645{
1646 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001647
1648 const union gdcr_training_mod_reg training_mod = {
1649 .dq_dqs_training_res = 1,
1650 };
Angel Pons66780a02021-03-26 13:33:22 +01001651 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Angel Pons58b609b2020-11-13 14:35:29 +01001652
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001653 FOR_ALL_POPULATED_CHANNELS {
1654 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001655 }
1656 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons765d4652020-11-11 14:44:35 +01001657 /* Reset read and write WDB pointers */
Angel Pons66780a02021-03-26 13:33:22 +01001658 mchbar_write32(IOSAV_DATA_CTL_ch(channel), 0x10001);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001659
Angel Pons88521882020-01-05 20:21:20 +01001660 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001661
Angel Ponsffd50152020-11-12 11:03:10 +01001662 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663
Angel Ponsa853e7a2020-12-07 12:28:38 +01001664 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001665
Angel Pons8f0757e2020-11-11 23:03:36 +01001666 const struct iosav_ssq rd_sequence[] = {
1667 /* DRAM command PREA */
1668 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001669 .sp_cmd_ctrl = {
1670 .command = IOSAV_PRE,
1671 .ranksel_ap = 1,
1672 },
1673 .subseq_ctrl = {
1674 .cmd_executions = 1,
1675 .cmd_delay_gap = 3,
1676 .post_ssq_wait = ctrl->tRP,
1677 .data_direction = SSQ_NA,
1678 },
1679 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +01001680 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +02001681 .rowbits = 6,
1682 .bank = 0,
1683 .rank = slotrank,
1684 },
1685 .addr_update = {
1686 .addr_wrap = 18,
1687 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001688 },
1689 /* DRAM command ACT */
1690 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001691 .sp_cmd_ctrl = {
1692 .command = IOSAV_ACT,
1693 .ranksel_ap = 1,
1694 },
1695 .subseq_ctrl = {
1696 .cmd_executions = 1,
1697 .cmd_delay_gap = 3,
1698 .post_ssq_wait = ctrl->tRCD,
1699 .data_direction = SSQ_NA,
1700 },
1701 .sp_cmd_addr = {
1702 .address = 0,
1703 .rowbits = 6,
1704 .bank = 0,
1705 .rank = slotrank,
1706 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001707 },
Angel Ponsf5502312021-02-10 11:08:28 +01001708 /* DRAM command RDA */
Angel Pons8f0757e2020-11-11 23:03:36 +01001709 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001710 .sp_cmd_ctrl = {
1711 .command = IOSAV_RD,
1712 .ranksel_ap = 3,
1713 },
1714 .subseq_ctrl = {
1715 .cmd_executions = 1,
1716 .cmd_delay_gap = 3,
1717 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001718 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001719 ctrl->timings[channel][slotrank].io_latency,
1720 .data_direction = SSQ_RD,
1721 },
1722 .sp_cmd_addr = {
1723 .address = 8,
1724 .rowbits = 6,
1725 .bank = 0,
1726 .rank = slotrank,
1727 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001728 },
1729 };
1730 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001731
Angel Ponsa853e7a2020-12-07 12:28:38 +01001732 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001733
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001734 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001735 u64 res = mchbar_read32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Elyes Haouas3a998072022-11-18 15:11:02 +01001736 res |= ((u64)mchbar_read32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001737 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001738
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001739 old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1740 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs +=
Angel Pons42d033a2021-01-03 15:26:37 +01001741 get_dqs_flyby_adjust(res) * QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001742
1743 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons7e439c92020-12-07 11:56:01 +01001744 printram("Bval+: %d, %d, %d, % 4d -> % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001745 old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001746 }
1747 }
Angel Pons66780a02021-03-26 13:33:22 +01001748 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001749}
1750
Angel Pons7d115132020-11-14 01:44:44 +01001751static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001752{
Angel Pons7d115132020-11-14 01:44:44 +01001753 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001754
Angel Pons7d115132020-11-14 01:44:44 +01001755 FOR_ALL_POPULATED_CHANNELS {
1756 /* choose an existing rank */
1757 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001758
Angel Pons7d115132020-11-14 01:44:44 +01001759 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001760
Angel Ponsa853e7a2020-12-07 12:28:38 +01001761 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001762
Angel Pons66780a02021-03-26 13:33:22 +01001763 mchbar_setbits32(SCHED_CBIT_ch(channel), 1 << 21);
Angel Pons7d115132020-11-14 01:44:44 +01001764 }
1765
1766 /* Refresh disable */
Angel Pons66780a02021-03-26 13:33:22 +01001767 mchbar_clrbits32(MC_INIT_STATE_G, 1 << 3);
Angel Pons7d115132020-11-14 01:44:44 +01001768
1769 FOR_ALL_POPULATED_CHANNELS {
1770 /* Execute the same command queue */
Angel Ponsa853e7a2020-12-07 12:28:38 +01001771 iosav_run_once_and_wait(channel);
Angel Pons7d115132020-11-14 01:44:44 +01001772 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001773}
1774
Angel Pons7c49cb82020-03-16 23:17:32 +01001775/*
1776 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001777 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001778 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1779 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1780 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1781 * CLK/ADDR/CMD signals have the same routing delay.
1782 *
1783 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1784 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1785 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786 */
Angel Pons820bce72020-11-14 17:02:55 +01001787static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001788{
Angel Pons820bce72020-11-14 17:02:55 +01001789 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001790
Angel Pons7d115132020-11-14 01:44:44 +01001791 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001792
Angel Pons7c49cb82020-03-16 23:17:32 +01001793 /* Enable write leveling on all ranks
1794 Disable all DQ outputs
1795 Only NOP is allowed in this mode */
1796 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1797 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001798 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001799
Angel Ponsa1f17142020-11-15 12:50:03 +01001800 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001801 const union gdcr_training_mod_reg training_mod = {
1802 .write_leveling_mode = 1,
1803 .enable_dqs_wl = 5,
1804 .odt_always_on = 1,
1805 .force_drive_enable = 1,
1806 };
Angel Pons66780a02021-03-26 13:33:22 +01001807 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001808
1809 toggle_io_reset();
1810
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001811 /* Set any valid value for tx_dqs, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001812 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001813 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001814 if (err)
1815 return err;
1816 }
1817
Angel Pons7c49cb82020-03-16 23:17:32 +01001818 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001819 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001820 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001821
Angel Pons66780a02021-03-26 13:33:22 +01001822 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001823
1824 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001825 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826
Angel Pons7c49cb82020-03-16 23:17:32 +01001827 /* Refresh enable */
Angel Pons66780a02021-03-26 13:33:22 +01001828 mchbar_setbits32(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001829
1830 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01001831 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 21);
1832 mchbar_read32(IOSAV_STATUS_ch(channel));
Angel Pons88521882020-01-05 20:21:20 +01001833 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001834
Angel Ponsffd50152020-11-12 11:03:10 +01001835 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836
Angel Ponsa853e7a2020-12-07 12:28:38 +01001837 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001838 }
1839
1840 toggle_io_reset();
1841
Angel Pons820bce72020-11-14 17:02:55 +01001842 return 0;
1843}
1844
1845int write_training(ramctr_timing *ctrl)
1846{
Angel Ponsc6742232020-11-15 13:26:21 +01001847 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001848 int err;
1849
Angel Pons4d192822020-12-12 13:54:37 +01001850 /*
1851 * Set the DEC_WRD bit, required for the write flyby algorithm.
1852 * Needs to be done before starting the write training procedure.
1853 */
Angel Pons820bce72020-11-14 17:02:55 +01001854 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01001855 mchbar_setbits32(TC_RWP_ch(channel), 1 << 27);
Angel Pons820bce72020-11-14 17:02:55 +01001856
Angel Pons4c76d252020-11-15 13:06:53 +01001857 printram("CPE\n");
1858
Angel Pons820bce72020-11-14 17:02:55 +01001859 err = jedec_write_leveling(ctrl);
1860 if (err)
1861 return err;
1862
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001863 printram("CPF\n");
1864
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001865 FOR_ALL_POPULATED_CHANNELS {
1866 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001867 }
1868
1869 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001870 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001871 if (err)
1872 return err;
1873 }
1874
1875 FOR_ALL_POPULATED_CHANNELS
1876 program_timings(ctrl, channel);
1877
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001878 /* measure and adjust tx_dqs timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001879 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001880
1881 FOR_ALL_POPULATED_CHANNELS
1882 program_timings(ctrl, channel);
1883
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001884 return 0;
1885}
1886
Angel Ponsbf13ef02020-11-11 18:40:06 +01001887static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001888{
1889 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001890 int tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001891 int lanes_ok = 0;
1892 int ctr = 0;
1893 int lane;
1894
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001895 for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001896 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001897 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
1898 saved_rt.lanes[lane].tx_dq + tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001899 }
1900 program_timings(ctrl, channel);
1901 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001902 mchbar_write32(IOSAV_By_ERROR_COUNT(lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001903 }
1904
Angel Pons765d4652020-11-11 14:44:35 +01001905 /* Reset read WDB pointer */
Angel Pons66780a02021-03-26 13:33:22 +01001906 mchbar_write32(IOSAV_DATA_CTL_ch(channel), 0x1f);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001907
Angel Pons88521882020-01-05 20:21:20 +01001908 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001909
Angel Ponsffd50152020-11-12 11:03:10 +01001910 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001911
1912 /* Program LFSR for the RD/WR subsequences */
Angel Pons66780a02021-03-26 13:33:22 +01001913 mchbar_write32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1), 0x389abcd);
1914 mchbar_write32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2), 0x389abcd);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001915
Angel Ponsa853e7a2020-12-07 12:28:38 +01001916 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001917
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001918 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001919 u32 r32 = mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001920
1921 if (r32 == 0)
1922 lanes_ok |= 1 << lane;
1923 }
1924 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001925 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001926 break;
1927 }
1928
1929 ctrl->timings[channel][slotrank] = saved_rt;
1930
Patrick Rudolphdd662872017-10-28 18:20:11 +02001931 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001932}
1933
Angel Pons88521882020-01-05 20:21:20 +01001934static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001935{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301936 unsigned int i, j;
Angel Pons5db1b152020-12-13 16:37:53 +01001937 unsigned int offset = get_precedening_channels(ctrl, channel) * 64;
1938 unsigned int step = 64 * num_of_channels(ctrl);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001939 uintptr_t addr;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001940
1941 if (patno) {
1942 u8 base8 = 0x80 >> ((patno - 1) % 8);
1943 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1944 for (i = 0; i < 32; i++) {
1945 for (j = 0; j < 16; j++) {
1946 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001947
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001948 if (invert[patno - 1][i] & (1 << (j / 2)))
1949 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001950
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001951 addr = (1 << 26) + offset + i * step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001952 write32p(addr, val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001953 }
1954 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001955 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001956 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1957 for (j = 0; j < 16; j++) {
1958 const u32 val = pattern[i][j];
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001959 addr = (1 << 26) + offset + i * step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001960 write32p(addr, val);
Angel Pons7c49cb82020-03-16 23:17:32 +01001961 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001962 }
1963 sfence();
1964 }
Angel Pons765d4652020-11-11 14:44:35 +01001965
1966 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001967}
1968
Angel Pons88521882020-01-05 20:21:20 +01001969static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970{
Angel Pons7d115132020-11-14 01:44:44 +01001971 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972
Angel Pons7c49cb82020-03-16 23:17:32 +01001973 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001975
1976 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001977 dram_mrscommands(ctrl);
1978
1979 toggle_io_reset();
1980}
1981
Angel Pons42d033a2021-01-03 15:26:37 +01001982#define CT_MIN_PI (-CCC_MAX_PI)
1983#define CT_MAX_PI (+CCC_MAX_PI + 1)
Angel Ponsbf13ef02020-11-11 18:40:06 +01001984#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1985
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001986#define MIN_C320C_LEN 13
1987
1988static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
1989{
1990 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
1991 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001992 int command_pi;
1993 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001994 int delta = 0;
1995
1996 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
1997
1998 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01001999 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002000 }
2001
2002 ctrl->cmd_stretch[channel] = cmd_stretch;
2003
Angel Pons7a612742020-11-12 13:34:03 +01002004 const union tc_rap_reg tc_rap = {
2005 .tRRD = ctrl->tRRD,
2006 .tRTP = ctrl->tRTP,
2007 .tCKE = ctrl->tCKE,
2008 .tWTR = ctrl->tWTR,
2009 .tFAW = ctrl->tFAW,
2010 .tWR = ctrl->tWR,
2011 .tCMD = ctrl->cmd_stretch[channel],
2012 };
Angel Pons66780a02021-03-26 13:33:22 +01002013 mchbar_write32(TC_RAP_ch(channel), tc_rap.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002014
2015 if (ctrl->cmd_stretch[channel] == 2)
2016 delta = 2;
2017 else if (ctrl->cmd_stretch[channel] == 0)
2018 delta = 4;
2019
2020 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002021 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002022 }
2023
Angel Ponsbf13ef02020-11-11 18:40:06 +01002024 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002025 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002026 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002027 }
2028 program_timings(ctrl, channel);
2029 reprogram_320c(ctrl);
2030 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002031 stat[slotrank][command_pi - CT_MIN_PI] =
2032 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002033 }
2034 }
2035 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002036 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002037
Angel Ponsbf13ef02020-11-11 18:40:06 +01002038 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Angel Pons7e439c92020-12-07 11:56:01 +01002039 printram("cmd_stretch: %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01002040 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002041
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002042 if (rn.all || rn.length < MIN_C320C_LEN) {
2043 FOR_ALL_POPULATED_RANKS {
2044 ctrl->timings[channel][slotrank] =
2045 saved_timings[channel][slotrank];
2046 }
2047 return MAKE_ERR;
2048 }
2049 }
2050
2051 return 0;
2052}
2053
Angel Pons7c49cb82020-03-16 23:17:32 +01002054/*
2055 * Adjust CMD phase shift and try multiple command rates.
2056 * A command rate of 2T doubles the time needed for address and command decode.
2057 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002058int command_training(ramctr_timing *ctrl)
2059{
2060 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002061
2062 FOR_ALL_POPULATED_CHANNELS {
2063 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002064 }
2065
2066 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002067 int cmdrate, err;
2068
2069 /*
2070 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002071 * Issue:
Angel Pons30791632020-12-12 12:28:29 +01002072 * While command training seems to succeed, raminit will fail in write training.
Angel Pons7c49cb82020-03-16 23:17:32 +01002073 *
2074 * Workaround:
2075 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2076 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002077 *
2078 * Single DIMM per channel:
2079 * Try command rate 1T and 2T
2080 */
2081 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002082 if (ctrl->tCMD)
2083 /* XMP gives the CMD rate in clock ticks, not ns */
2084 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002085
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002086 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002087 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2088
2089 if (!err)
2090 break;
2091 }
2092
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002093 if (err) {
Angel Pons30791632020-12-12 12:28:29 +01002094 printk(BIOS_EMERG, "Command training failed: %d\n", channel);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002095 return err;
2096 }
2097
Angel Pons891f2bc2020-01-10 01:27:28 +01002098 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002099 }
2100
2101 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002102 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002103
2104 reprogram_320c(ctrl);
2105 return 0;
2106}
2107
Angel Pons4c79f932020-11-14 01:26:52 +01002108static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002109{
Angel Pons96a06dd2020-11-14 00:33:18 +01002110 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002111 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002112 int lane;
2113
Angel Pons96a06dd2020-11-14 00:33:18 +01002114 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002115 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002116 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi;
2117 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002118 }
2119 program_timings(ctrl, channel);
2120
2121 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002122 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane), 0);
2123 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002124 }
2125
Angel Pons88521882020-01-05 20:21:20 +01002126 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002127
Angel Ponsffd50152020-11-12 11:03:10 +01002128 iosav_write_read_mpr_sequence(
2129 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002130
Angel Ponsa853e7a2020-12-07 12:28:38 +01002131 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002132
2133 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002134 stats[lane][dqs_pi] = mchbar_read32(
2135 IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002136 }
2137 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002138
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002139 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002140 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002141 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002142
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002144 printk(BIOS_EMERG, "Read MPR training failed: %d, %d, %d\n", channel,
Angel Pons7c49cb82020-03-16 23:17:32 +01002145 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146 return MAKE_ERR;
2147 }
Angel Pons7e439c92020-12-07 11:56:01 +01002148 printram("eval %d, %d, %d: % 4d\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149 }
2150 return 0;
2151}
2152
Angel Pons60971dc2020-11-14 00:49:38 +01002153static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2154{
2155 int slotrank, lane;
2156
2157 fill_pattern0(ctrl, channel, 0, 0);
2158 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002159 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane), 0);
2160 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Angel Pons60971dc2020-11-14 00:49:38 +01002161 }
2162
2163 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002164 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16;
2165 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16;
Angel Pons60971dc2020-11-14 00:49:38 +01002166 }
2167
2168 program_timings(ctrl, channel);
2169
2170 FOR_ALL_POPULATED_RANKS {
2171 wait_for_iosav(channel);
2172
2173 iosav_write_read_mpr_sequence(
2174 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2175
Angel Ponsa853e7a2020-12-07 12:28:38 +01002176 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002177 }
2178
2179 /* XXX: check any measured value ? */
2180
2181 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002182 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48;
2183 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48;
Angel Pons60971dc2020-11-14 00:49:38 +01002184 }
2185
2186 program_timings(ctrl, channel);
2187
2188 FOR_ALL_POPULATED_RANKS {
2189 wait_for_iosav(channel);
2190
2191 iosav_write_read_mpr_sequence(
2192 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2193
Angel Ponsa853e7a2020-12-07 12:28:38 +01002194 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002195 }
2196
2197 /* XXX: check any measured value ? */
2198
2199 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002200 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane),
2201 ~mchbar_read32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff);
Angel Pons60971dc2020-11-14 00:49:38 +01002202 }
2203}
2204
Angel Pons4c79f932020-11-14 01:26:52 +01002205int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002206{
2207 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2208 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2209 int channel, slotrank, lane;
2210 int err;
2211
Angel Pons66780a02021-03-26 13:33:22 +01002212 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002213
2214 toggle_io_reset();
2215
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002216 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002217 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002218
2219 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002220 }
2221
Angel Pons0c3936e2020-03-22 12:49:27 +01002222 /*
2223 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2224 * also use a single loop. It would seem that it is a debugging configuration.
2225 */
Angel Pons66780a02021-03-26 13:33:22 +01002226 mchbar_write32(IOSAV_DC_MASK, 3 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002227 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002228
2229 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002230 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002231 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002232 if (err)
2233 return err;
2234 }
2235
Angel Pons66780a02021-03-26 13:33:22 +01002236 mchbar_write32(IOSAV_DC_MASK, 2 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002237 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002238
2239 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002240 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002241 rising_edges[channel][slotrank]);
2242 if (err)
2243 return err;
2244 }
2245
Angel Pons66780a02021-03-26 13:33:22 +01002246 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002247
2248 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002249 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002250 falling_edges[channel][slotrank][lane];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002251 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002252 rising_edges[channel][slotrank][lane];
2253 }
2254
2255 FOR_ALL_POPULATED_CHANNELS {
2256 program_timings(ctrl, channel);
2257 }
2258
Angel Pons50a6fe72020-11-14 01:18:14 +01002259 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002260 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002261 }
2262 return 0;
2263}
2264
Angel Pons08f749d2020-11-17 16:50:56 +01002265static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002266{
Angel Pons08f749d2020-11-17 16:50:56 +01002267 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2268
Angel Pons7c49cb82020-03-16 23:17:32 +01002269 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002270 int lower[NUM_LANES];
2271 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002272 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002273
2274 FOR_ALL_LANES {
2275 lower[lane] = 0;
2276 upper[lane] = MAX_EDGE_TIMING;
2277 }
2278
Angel Pons08f749d2020-11-17 16:50:56 +01002279 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002280 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002281 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002282 };
Angel Pons66780a02021-03-26 13:33:22 +01002283 mchbar_write32(GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons58b609b2020-11-13 14:35:29 +01002284 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002285
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002286 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2287 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002288 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002289
Angel Pons08f749d2020-11-17 16:50:56 +01002290 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002291 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002292 ctrl->timings[channel][slotrank].lanes[lane]
2293 .rx_dqs_p = read_pi;
2294 ctrl->timings[channel][slotrank].lanes[lane]
2295 .rx_dqs_n = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002296 }
2297 program_timings(ctrl, channel);
2298
2299 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002300 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane),
2301 0);
2302 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002303 }
Angel Pons88521882020-01-05 20:21:20 +01002304 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002305
Angel Ponsffd50152020-11-12 11:03:10 +01002306 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307
Angel Ponsa853e7a2020-12-07 12:28:38 +01002308 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002309
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002310 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002311 mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002312 }
2313
Angel Pons7c49cb82020-03-16 23:17:32 +01002314 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons66780a02021-03-26 13:33:22 +01002315 raw_stats[read_pi] = mchbar_read32(
2316 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002317 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002318
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002319 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002320 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002321 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002322
2323 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2324 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002325
2326 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2327
Angel Pons7e439c92020-12-07 11:56:01 +01002328 printram("edges: %d, %d, %d: % 4d-% 4d-% 4d, "
2329 "% 4d-% 4d\n", channel, slotrank, i, rn.start,
Angel Pons7c49cb82020-03-16 23:17:32 +01002330 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002331 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002332
2333 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2334 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2335
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336 edges[lane] = (lower[lane] + upper[lane]) / 2;
2337 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons30791632020-12-12 12:28:29 +01002338 printk(BIOS_EMERG, "Aggressive read training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002339 "%d, %d, %d\n", channel, slotrank, lane);
2340
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002341 return MAKE_ERR;
2342 }
2343 }
2344 }
2345 }
2346
Angel Ponsa93f46e2020-11-17 16:54:01 +01002347 /* Restore nominal Vref after training */
Angel Pons66780a02021-03-26 13:33:22 +01002348 mchbar_write32(GDCRTRAININGMOD_ch(channel), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002349 printram("CPA\n");
2350 return 0;
2351}
2352
Angel Pons08f749d2020-11-17 16:50:56 +01002353int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002354{
2355 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002356 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2357 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002358
Angel Pons7c49cb82020-03-16 23:17:32 +01002359 /*
2360 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2361 * also use a single loop. It would seem that it is a debugging configuration.
2362 */
Angel Pons66780a02021-03-26 13:33:22 +01002363 mchbar_write32(IOSAV_DC_MASK, 3 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002364 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002365
2366 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002367 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002368 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002369 if (err)
2370 return err;
2371 }
2372
Angel Pons66780a02021-03-26 13:33:22 +01002373 mchbar_write32(IOSAV_DC_MASK, 2 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002374 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002375
2376 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002377 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002378 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002379 if (err)
2380 return err;
2381 }
2382
Angel Pons66780a02021-03-26 13:33:22 +01002383 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002384
2385 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002386 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Angel Pons7c49cb82020-03-16 23:17:32 +01002387 falling_edges[channel][slotrank][lane];
2388
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002389 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Angel Pons7c49cb82020-03-16 23:17:32 +01002390 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002391 }
2392
2393 FOR_ALL_POPULATED_CHANNELS
2394 program_timings(ctrl, channel);
2395
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002396 return 0;
2397}
2398
Angel Pons2a7d7522020-11-19 12:49:07 +01002399static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002400{
Angel Pons88521882020-01-05 20:21:20 +01002401 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002402
Angel Ponsffd50152020-11-12 11:03:10 +01002403 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002404
Angel Ponsa853e7a2020-12-07 12:28:38 +01002405 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002406}
2407
Angel Pons2a7d7522020-11-19 12:49:07 +01002408static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002409{
Angel Pons66780a02021-03-26 13:33:22 +01002410 mchbar_clrsetbits32(GDCRCMDDEBUGMUXCFG_Cz_S(channel), 0x3f << 24, wr_vref << 24);
Angel Pons2a7d7522020-11-19 12:49:07 +01002411 udelay(2);
2412}
2413
2414int aggressive_write_training(ramctr_timing *ctrl)
2415{
2416 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002417 int i, pat;
2418
2419 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2420 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2421 int channel, slotrank, lane;
2422
Angel Pons9fbb1b02020-11-19 12:53:36 +01002423 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2424 if (!IS_IVY_CPU(ctrl->cpu))
2425 return 0;
2426
2427 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2428 return 0;
2429
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002430 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2431 lower[channel][slotrank][lane] = 0;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002432 upper[channel][slotrank][lane] = MAX_TX_DQ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002433 }
2434
Angel Pons2a7d7522020-11-19 12:49:07 +01002435 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2436 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2437
2438 if (enable_iosav_opt)
Angel Pons66780a02021-03-26 13:33:22 +01002439 mchbar_write32(MCMNTS_SPARE, 1);
Angel Pons2a7d7522020-11-19 12:49:07 +01002440
Martin Roth50863da2021-10-01 14:37:30 -06002441 printram("Aggressive write training:\n");
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002442
Angel Pons2a7d7522020-11-19 12:49:07 +01002443 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002444 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002445 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002446
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002447 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2448 FOR_ALL_POPULATED_RANKS {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002449 int tx_dq;
2450 u32 raw_stats[MAX_TX_DQ + 1];
2451 int stats[MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002452
2453 /* Make sure rn.start < rn.end */
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002454 stats[MAX_TX_DQ] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002455
2456 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002457
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002458 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002459 FOR_ALL_LANES {
2460 ctrl->timings[channel][slotrank]
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002461 .lanes[lane].tx_dq = tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01002462 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002463 program_timings(ctrl, channel);
2464
Angel Pons2a7d7522020-11-19 12:49:07 +01002465 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002466
Angel Pons66780a02021-03-26 13:33:22 +01002467 raw_stats[tx_dq] = mchbar_read32(
Angel Pons098240eb2020-03-22 12:55:32 +01002468 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002469 }
2470 FOR_ALL_LANES {
2471 struct run rn;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002472 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
2473 stats[tx_dq] = !!(raw_stats[tx_dq]
Angel Pons7c49cb82020-03-16 23:17:32 +01002474 & (1 << lane));
2475 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002476
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002477 rn = get_longest_zero_run(stats, MAX_TX_DQ + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002478 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002479 printk(BIOS_EMERG, "Aggressive "
2480 "write training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002481 "%d, %d, %d\n", channel,
2482 slotrank, lane);
2483
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002484 return MAKE_ERR;
2485 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002486 printram("tx_dq: %d, %d, %d: "
Angel Pons7e439c92020-12-07 11:56:01 +01002487 "% 4d-% 4d-% 4d, "
2488 "% 4d-% 4d\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002489 i, rn.start, rn.middle, rn.end,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002490 rn.start + ctrl->tx_dq_offset[i],
2491 rn.end - ctrl->tx_dq_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002492
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002493 lower[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002494 MAX(rn.start + ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002495 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002496
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002497 upper[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002498 MIN(rn.end - ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499 upper[channel][slotrank][lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002500 }
2501 }
2502 }
2503 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002504 }
2505
Angel Pons2a7d7522020-11-19 12:49:07 +01002506 FOR_ALL_CHANNELS {
2507 /* Restore nominal write Vref after training */
2508 set_write_vref(channel, 0);
2509 }
2510
2511 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2512 if (enable_iosav_opt)
Angel Pons66780a02021-03-26 13:33:22 +01002513 mchbar_write32(MCMNTS_SPARE, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002514
2515 printram("CPB\n");
2516
2517 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7e439c92020-12-07 11:56:01 +01002518 printram("tx_dq %d, %d, %d: % 4d\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519 (lower[channel][slotrank][lane] +
2520 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002521
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002522 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002523 (lower[channel][slotrank][lane] +
2524 upper[channel][slotrank][lane]) / 2;
2525 }
2526 FOR_ALL_POPULATED_CHANNELS {
2527 program_timings(ctrl, channel);
2528 }
2529 return 0;
2530}
2531
Angel Pons88521882020-01-05 20:21:20 +01002532void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002533{
2534 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002535 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002536
2537 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2538 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002539 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002540 FOR_ALL_LANES mat =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002541 MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002542 printram("normalize %d, %d, %d: mat %d\n",
2543 channel, slotrank, lane, mat);
2544
Felix Heldef4fe3e2019-12-31 14:15:05 +01002545 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002546 printram("normalize %d, %d, %d: delta %d\n",
2547 channel, slotrank, lane, delta);
2548
Angel Pons88521882020-01-05 20:21:20 +01002549 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002550 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002551 }
2552
2553 FOR_ALL_POPULATED_CHANNELS {
2554 program_timings(ctrl, channel);
2555 }
2556}
2557
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002558int channel_test(ramctr_timing *ctrl)
2559{
2560 int channel, slotrank, lane;
2561
2562 slotrank = 0;
2563 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01002564 if (mchbar_read32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002565 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002566 return MAKE_ERR;
2567 }
2568 FOR_ALL_POPULATED_CHANNELS {
2569 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002570 }
2571
2572 for (slotrank = 0; slotrank < 4; slotrank++)
2573 FOR_ALL_CHANNELS
2574 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2575 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002576 mchbar_write32(IOSAV_By_ERROR_COUNT(lane), 0);
2577 mchbar_write32(IOSAV_By_BW_SERROR_C(lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002578 }
Angel Pons88521882020-01-05 20:21:20 +01002579 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002580
Angel Ponsffd50152020-11-12 11:03:10 +01002581 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002582
Angel Ponsa853e7a2020-12-07 12:28:38 +01002583 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002584
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002585 FOR_ALL_LANES
Angel Pons66780a02021-03-26 13:33:22 +01002586 if (mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002587 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2588 channel, slotrank, lane);
2589 return MAKE_ERR;
2590 }
2591 }
2592 return 0;
2593}
2594
Patrick Rudolphdd662872017-10-28 18:20:11 +02002595void channel_scrub(ramctr_timing *ctrl)
2596{
2597 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002598 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002599
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002600 FOR_ALL_POPULATED_CHANNELS {
2601 wait_for_iosav(channel);
2602 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002603 }
2604
2605 /*
2606 * During runtime the "scrubber" will periodically scan through the memory in the
2607 * physical address space, to identify and fix CRC errors.
2608 * The following loops writes to every DRAM address, setting the ECC bits to the
2609 * correct value. A read from this location will no longer return a CRC error,
2610 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002611 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002612 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2613 * and firmware running in x86_32.
2614 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002615 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2616 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002617 for (bank = 0; bank < 8; bank++) {
2618 for (row = 0; row < rowsize; row += 16) {
Angel Pons8f0757e2020-11-11 23:03:36 +01002619 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2620 const struct iosav_ssq sequence[] = {
2621 /*
2622 * DRAM command ACT
2623 * Opens the row for writing.
2624 */
2625 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002626 .sp_cmd_ctrl = {
2627 .command = IOSAV_ACT,
2628 .ranksel_ap = 1,
2629 },
2630 .subseq_ctrl = {
2631 .cmd_executions = 1,
2632 .cmd_delay_gap = gap,
2633 .post_ssq_wait = ctrl->tRCD,
2634 .data_direction = SSQ_NA,
2635 },
2636 .sp_cmd_addr = {
2637 .address = row,
2638 .rowbits = 6,
2639 .bank = bank,
2640 .rank = slotrank,
2641 },
2642 .addr_update = {
2643 .inc_addr_1 = 1,
2644 .addr_wrap = 18,
2645 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002646 },
2647 /*
2648 * DRAM command WR
2649 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2650 * bytes.
2651 */
2652 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002653 .sp_cmd_ctrl = {
2654 .command = IOSAV_WR,
2655 .ranksel_ap = 1,
2656 },
2657 .subseq_ctrl = {
2658 .cmd_executions = 129,
2659 .cmd_delay_gap = 4,
2660 .post_ssq_wait = ctrl->tWTR +
2661 ctrl->CWL + 8,
2662 .data_direction = SSQ_WR,
2663 },
2664 .sp_cmd_addr = {
2665 .address = row,
2666 .rowbits = 0,
2667 .bank = bank,
2668 .rank = slotrank,
2669 },
2670 .addr_update = {
2671 .inc_addr_8 = 1,
2672 .addr_wrap = 9,
2673 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002674 },
2675 /*
2676 * DRAM command PRE
2677 * Closes the row.
2678 */
2679 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002680 .sp_cmd_ctrl = {
2681 .command = IOSAV_PRE,
2682 .ranksel_ap = 1,
2683 },
2684 .subseq_ctrl = {
2685 .cmd_executions = 1,
2686 .cmd_delay_gap = 4,
2687 .post_ssq_wait = ctrl->tRP,
2688 .data_direction = SSQ_NA,
2689 },
2690 .sp_cmd_addr = {
2691 .address = 0,
2692 .rowbits = 6,
2693 .bank = bank,
2694 .rank = slotrank,
2695 },
2696 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002697 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002698 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002699 },
2700 };
2701 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002702
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002703 iosav_run_queue(channel, 16, 0);
2704
2705 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002706 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002707 }
2708 }
2709}
2710
Angel Pons88521882020-01-05 20:21:20 +01002711void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002712{
2713 int channel;
2714
Angel Pons7c49cb82020-03-16 23:17:32 +01002715 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them? I don't think so. */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002716 static u32 seeds[NUM_CHANNELS][3] = {
2717 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2718 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2719 };
2720 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002721 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 28);
2722 mchbar_write32(SCRAMBLING_SEED_1_ch(channel), seeds[channel][0]);
2723 mchbar_write32(SCRAMBLING_SEED_2_HI_ch(channel), seeds[channel][1]);
2724 mchbar_write32(SCRAMBLING_SEED_2_LO_ch(channel), seeds[channel][2]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002725 }
2726}
2727
Angel Pons89ae6b82020-03-21 13:23:32 +01002728void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002729{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002730 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons66780a02021-03-26 13:33:22 +01002731 mchbar_write32(SC_WDBWM, 0x141d1519);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002732 } else {
Angel Pons66780a02021-03-26 13:33:22 +01002733 mchbar_write32(SC_WDBWM, 0x551d1519);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002734 }
2735}
2736
Angel Pons88521882020-01-05 20:21:20 +01002737void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002738{
2739 int channel;
2740
2741 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002742 /* Always drive command bus */
Angel Pons66780a02021-03-26 13:33:22 +01002743 mchbar_setbits32(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002744 }
2745
2746 udelay(1);
2747
2748 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002749 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002750 }
2751}
2752
Angel Pons7c49cb82020-03-16 23:17:32 +01002753void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002754{
Angel Pons11463322020-11-19 11:04:28 +01002755 /* Use a larger delay when running fast to improve stability */
2756 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2757
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002758 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002759
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002760 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002761 int min_pi = 10000;
2762 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002763
2764 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002765 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2766 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002767 }
2768
Angel Pons7a612742020-11-12 13:34:03 +01002769 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002770
Angel Pons7a612742020-11-12 13:34:03 +01002771 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002772
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002773 dram_odt_stretch(ctrl, channel);
2774
Angel Pons7a612742020-11-12 13:34:03 +01002775 const union tc_rwp_reg tc_rwp = {
2776 .tRRDR = 0,
2777 .tRRDD = val,
2778 .tWWDR = val,
2779 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002780 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002781 .tWRDRDD = tWRDRDD,
2782 .tRWSR = 2,
2783 .dec_wrd = 1,
2784 };
Angel Pons66780a02021-03-26 13:33:22 +01002785 mchbar_write32(TC_RWP_ch(channel), tc_rwp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002786 }
2787}
2788
Angel Pons88521882020-01-05 20:21:20 +01002789void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002790{
2791 int channel;
2792 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002793 mchbar_write32(MC_INIT_STATE_ch(channel), 1 << 12 | ctrl->rankmap[channel]);
2794 mchbar_clrbits32(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002795 }
2796}
2797
Angel Pons7c49cb82020-03-16 23:17:32 +01002798/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2799static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002800{
Angel Pons88521882020-01-05 20:21:20 +01002801 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002802}
2803
Angel Pons7c49cb82020-03-16 23:17:32 +01002804/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002805void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002806{
2807 int channel;
2808 int t1_cycles = 0, t1_ns = 0, t2_ns;
2809 int t3_ns;
2810 u32 r32;
2811
Patrick Rudolph220bd262023-12-21 14:49:36 +01002812 if (IS_IVY_CPU(ctrl->cpu))
2813 mchbar_write32(WMM_READ_CONFIG, 0x46);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002814
Angel Pons7a612742020-11-12 13:34:03 +01002815 FOR_ALL_CHANNELS {
2816 union tc_othp_reg tc_othp = {
Angel Pons66780a02021-03-26 13:33:22 +01002817 .raw = mchbar_read32(TC_OTHP_ch(channel)),
Angel Pons7a612742020-11-12 13:34:03 +01002818 };
Patrick Rudolphb7341da2023-12-21 14:53:45 +01002819 if (IS_SANDY_CPU(ctrl->cpu) && (ctrl->cpu & 0xf) < SNB_STEP_D0)
2820 tc_othp.tCPDED = 2;
2821 else
2822 tc_othp.tCPDED = 1;
Angel Pons66780a02021-03-26 13:33:22 +01002823 mchbar_write32(TC_OTHP_ch(channel), tc_othp.raw);
Patrick Rudolph652c4912017-10-31 11:36:55 +01002824
Patrick Rudolph68642ca2023-12-21 07:08:49 +01002825 /* 64 DCLKs until idle, decision per rank */
2826 r32 = get_power_down_mode(ctrl, channel) << 8 | 64;
2827 mchbar_write32(PM_PDWN_CONFIG_ch(channel), r32);
Patrick Rudolph652c4912017-10-31 11:36:55 +01002828
Angel Pons66780a02021-03-26 13:33:22 +01002829 mchbar_write32(PM_TRML_M_CONFIG_ch(channel), 0x00000aaa);
Patrick Rudolph68642ca2023-12-21 07:08:49 +01002830 }
Felix Heldf9b826a2018-07-30 17:56:52 +02002831
Angel Pons66780a02021-03-26 13:33:22 +01002832 mchbar_write32(PM_BW_LIMIT_CONFIG, 0x5f7003ff);
Patrick Rudolphe27a26b2023-12-21 14:45:27 +01002833 if (IS_SANDY_CPU(ctrl->cpu))
2834 mchbar_write32(PM_DLL_CONFIG, 0x000330f0);
2835 else
2836 mchbar_write32(PM_DLL_CONFIG, 0x00073000 | ctrl->mdll_wake_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002837
2838 FOR_ALL_CHANNELS {
2839 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002840 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002841 case 0:
Angel Pons66780a02021-03-26 13:33:22 +01002842 mchbar_write32(PM_CMD_PWR_ch(channel), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002843 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002844 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002845 case 1:
2846 case 4:
2847 case 5:
Angel Pons66780a02021-03-26 13:33:22 +01002848 mchbar_write32(PM_CMD_PWR_ch(channel), 0x00373131);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002849 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002850 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002851 default:
Angel Pons66780a02021-03-26 13:33:22 +01002852 mchbar_write32(PM_CMD_PWR_ch(channel), 0x009b6ea1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002853 break;
2854 }
2855 }
2856
Angel Pons66780a02021-03-26 13:33:22 +01002857 mchbar_write32(MEM_TRML_ESTIMATION_CONFIG, 0xca9171e5);
2858 mchbar_clrsetbits32(MEM_TRML_THRESHOLDS_CONFIG, 0x00ffffff, 0x00e4d5d0);
2859 mchbar_clrbits32(MEM_TRML_INTERRUPT, 0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002860
Angel Pons7a612742020-11-12 13:34:03 +01002861 FOR_ALL_CHANNELS {
2862 union tc_rfp_reg tc_rfp = {
Angel Pons66780a02021-03-26 13:33:22 +01002863 .raw = mchbar_read32(TC_RFP_ch(channel)),
Angel Pons7a612742020-11-12 13:34:03 +01002864 };
2865 tc_rfp.refresh_2x_control = 1;
Angel Pons66780a02021-03-26 13:33:22 +01002866 mchbar_write32(TC_RFP_ch(channel), tc_rfp.raw);
Angel Pons7a612742020-11-12 13:34:03 +01002867 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002868
Angel Pons66780a02021-03-26 13:33:22 +01002869 mchbar_setbits32(MC_INIT_STATE_G, 1 << 0);
2870 mchbar_setbits32(MC_INIT_STATE_G, 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002871
Angel Pons7c49cb82020-03-16 23:17:32 +01002872 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002873 FOR_ALL_POPULATED_CHANNELS
2874 break;
2875
Angel Pons66780a02021-03-26 13:33:22 +01002876 t1_cycles = (mchbar_read32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2877 r32 = mchbar_read32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002878 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002879 t1_cycles += (r32 & 0xfff);
Angel Pons66780a02021-03-26 13:33:22 +01002880 t1_cycles += mchbar_read32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002881 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002882 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002883 t1_ns += 500;
2884
Angel Pons66780a02021-03-26 13:33:22 +01002885 t2_ns = 10 * ((mchbar_read32(SAPMTIMERS) >> 8) & 0xfff);
2886 if (mchbar_read32(SAPMCTL) & 8) {
2887 t3_ns = 10 * ((mchbar_read32(BANDTIMERS_IVB) >> 8) & 0xfff);
2888 t3_ns += 10 * (mchbar_read32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002889 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002890 t3_ns = 500;
2891 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002892
2893 /* The graphics driver will use these watermark values */
2894 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Pons66780a02021-03-26 13:33:22 +01002895 mchbar_clrsetbits32(SSKPD, 0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002896 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2897 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002898}
2899
Angel Pons88521882020-01-05 20:21:20 +01002900void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002901{
Angel Ponsc6742232020-11-15 13:26:21 +01002902 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002903
Angel Pons7c49cb82020-03-16 23:17:32 +01002904 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002905 const union tc_rap_reg tc_rap = {
2906 .tRRD = ctrl->tRRD,
2907 .tRTP = ctrl->tRTP,
2908 .tCKE = ctrl->tCKE,
2909 .tWTR = ctrl->tWTR,
2910 .tFAW = ctrl->tFAW,
2911 .tWR = ctrl->tWR,
2912 .tCMD = ctrl->cmd_stretch[channel],
2913 };
Angel Pons66780a02021-03-26 13:33:22 +01002914 mchbar_write32(TC_RAP_ch(channel), tc_rap.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002915 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002916
2917 udelay(1);
2918
2919 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002920 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002921 }
2922
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002923 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01002924 mchbar_setbits32(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002925
2926 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002927 udelay(1);
Angel Pons66780a02021-03-26 13:33:22 +01002928 mchbar_setbits32(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002929 }
2930
2931 printram("CPE\n");
2932
Angel Pons66780a02021-03-26 13:33:22 +01002933 mchbar_write32(GDCRTRAININGMOD, 0);
2934 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002935
2936 printram("CP5b\n");
2937
2938 FOR_ALL_POPULATED_CHANNELS {
2939 program_timings(ctrl, channel);
2940 }
2941
2942 u32 reg, addr;
2943
Angel Pons7c49cb82020-03-16 23:17:32 +01002944 /* Poll for RCOMP */
Angel Pons66780a02021-03-26 13:33:22 +01002945 while (!(mchbar_read32(RCOMP_TIMER) & (1 << 16)))
Angel Pons7c49cb82020-03-16 23:17:32 +01002946 ;
2947
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002948 do {
Angel Pons66780a02021-03-26 13:33:22 +01002949 reg = mchbar_read32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002950 } while ((reg & 0x14) == 0);
2951
Angel Pons7c49cb82020-03-16 23:17:32 +01002952 /* Set state of memory controller */
Angel Pons66780a02021-03-26 13:33:22 +01002953 mchbar_write32(MC_INIT_STATE_G, 0x116);
2954 mchbar_write32(MC_INIT_STATE, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002955
Angel Pons7c49cb82020-03-16 23:17:32 +01002956 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002957 udelay(500);
2958
2959 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002960 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002961 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002962 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002963 addr = MC_INIT_STATE_ch(channel);
Angel Pons66780a02021-03-26 13:33:22 +01002964 mchbar_write32(addr, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002965
Angel Pons7c49cb82020-03-16 23:17:32 +01002966 /* Wait 10ns for ranks to settle */
2967 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002968
2969 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons66780a02021-03-26 13:33:22 +01002970 mchbar_write32(addr, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002971
Angel Pons7c49cb82020-03-16 23:17:32 +01002972 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002973 write_reset(ctrl);
2974 }
2975
Angel Pons7c49cb82020-03-16 23:17:32 +01002976 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002977 dram_mrscommands(ctrl);
2978
2979 printram("CP5c\n");
2980
Angel Pons66780a02021-03-26 13:33:22 +01002981 mchbar_write32(GDCRTRAININGMOD_ch(0), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002982
2983 FOR_ALL_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002984 mchbar_clrbits32(GDCRCMDDEBUGMUXCFG_Cz_S(channel), 0x3f << 24);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002985 udelay(2);
2986 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002987}