blob: d6a8aa550ae3937f95cf0091160e894d2bc9359e [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_native.h"
15#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010016#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010017#include "sandybridge.h"
18
Angel Pons7c49cb82020-03-16 23:17:32 +010019/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010020
21static void sfence(void)
22{
23 asm volatile ("sfence");
24}
25
Angel Pons7c49cb82020-03-16 23:17:32 +010026/* Toggle IO reset bit */
27static void toggle_io_reset(void)
28{
Angel Pons88521882020-01-05 20:21:20 +010029 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010030 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010031 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010032 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010033 udelay(1);
34}
35
36static u32 get_XOVER_CLK(u8 rankmap)
37{
38 return rankmap << 24;
39}
40
41static u32 get_XOVER_CMD(u8 rankmap)
42{
43 u32 reg;
44
Angel Pons7c49cb82020-03-16 23:17:32 +010045 /* Enable xover cmd */
Angel Pons5db1b152020-12-13 16:37:53 +010046 reg = 1 << 14;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010047
Angel Pons7c49cb82020-03-16 23:17:32 +010048 /* Enable xover ctl */
49 if (rankmap & 0x03)
50 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010051
Angel Pons7c49cb82020-03-16 23:17:32 +010052 if (rankmap & 0x0c)
53 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010054
55 return reg;
56}
57
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010058void dram_find_common_params(ramctr_timing *ctrl)
59{
60 size_t valid_dimms;
61 int channel, slot;
62 dimm_info *dimms = &ctrl->info;
63
64 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
65 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010066
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010067 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010068
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010069 const dimm_attr *dimm = &dimms->dimm[channel][slot];
70 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
71 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010072
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010073 valid_dimms++;
74
75 /* Find all possible CAS combinations */
76 ctrl->cas_supported &= dimm->cas_supported;
77
78 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010079 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
80 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
81 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010082 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
83 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010084 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010085 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
86 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
87 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
88 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
89 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030090 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
91 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010092 }
93
94 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010095 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
96
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010097 if (!valid_dimms)
98 die("No valid DIMMs found");
99}
100
Angel Pons88521882020-01-05 20:21:20 +0100101void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100102{
103 u32 reg;
104 int channel;
105
106 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100107 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100108 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100109 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
110 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100111
Angel Pons7c49cb82020-03-16 23:17:32 +0100112 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100113 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100114 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
115 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100116 }
117}
118
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100119static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100120{
Angel Pons89ae6b82020-03-21 13:23:32 +0100121 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100122
123 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100124 /*
125 * ODT stretch:
126 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
127 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100128 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100129 if (stretch == 2)
130 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100131
Angel Pons88521882020-01-05 20:21:20 +0100132 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100133 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100134 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100135 } else {
Angel Pons88521882020-01-05 20:21:20 +0100136 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100137 union tc_othp_reg tc_othp = {
138 .raw = MCHBAR32(addr),
139 };
140 tc_othp.odt_delay_d0 = stretch;
141 tc_othp.odt_delay_d1 = stretch;
142 MCHBAR32(addr) = tc_othp.raw;
Iru Cai89af71c2018-08-16 16:46:27 +0800143 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100144 }
145}
146
147void dram_timing_regs(ramctr_timing *ctrl)
148{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100149 int channel;
150
Angel Pons81378062020-11-12 13:46:21 +0100151 /* BIN parameters */
152 const union tc_dbp_reg tc_dbp = {
153 .tRCD = ctrl->tRCD,
154 .tRP = ctrl->tRP,
155 .tAA = ctrl->CAS,
156 .tCWL = ctrl->CWL,
157 .tRAS = ctrl->tRAS,
158 };
159
160 /* Regular access parameters */
161 const union tc_rap_reg tc_rap = {
162 .tRRD = ctrl->tRRD,
163 .tRTP = ctrl->tRTP,
164 .tCKE = ctrl->tCKE,
165 .tWTR = ctrl->tWTR,
166 .tFAW = ctrl->tFAW,
167 .tWR = ctrl->tWR,
168 .tCMD = 3,
169 };
170
171 /* Other parameters */
172 const union tc_othp_reg tc_othp = {
Angel Ponsda437372021-01-24 18:34:51 +0100173 .tXPDLL = MIN(ctrl->tXPDLL, 31),
174 .tXP = MIN(ctrl->tXP, 7),
Angel Pons81378062020-11-12 13:46:21 +0100175 .tAONPD = ctrl->tAONPD,
176 .tCPDED = 2,
Angel Pons2ad03a42020-11-19 11:07:27 +0100177 .tPRPDEN = 1,
Angel Pons81378062020-11-12 13:46:21 +0100178 };
179
180 /*
Angel Ponsda437372021-01-24 18:34:51 +0100181 * If tXP and tXPDLL are very high, they no longer fit in the bitfields
182 * of the TC_OTHP register. If so, we set bits in TC_DTP to compensate.
Angel Pons81378062020-11-12 13:46:21 +0100183 * This can only happen on Ivy Bridge, and when overclocking the RAM.
184 */
185 const union tc_dtp_reg tc_dtp = {
186 .overclock_tXP = ctrl->tXP >= 8,
187 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
188 };
189
190 /*
191 * TC-Refresh timing parameters:
192 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
193 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
194 */
195 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
196
197 const union tc_rftp_reg tc_rftp = {
198 .tREFI = ctrl->tREFI,
199 .tRFC = ctrl->tRFC,
200 .tREFIx9 = val32 / 1024,
201 };
202
203 /* Self-refresh timing parameters */
204 const union tc_srftp_reg tc_srftp = {
205 .tXSDLL = tDLLK,
206 .tXS_offset = ctrl->tXSOffset,
207 .tZQOPER = tDLLK - ctrl->tXSOffset,
208 .tMOD = ctrl->tMOD - 8,
209 };
210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100211 FOR_ALL_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +0100212 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
213 MCHBAR32(TC_DBP_ch(channel)) = tc_dbp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100214
Angel Pons7a612742020-11-12 13:34:03 +0100215 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
216 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100217
Angel Pons7a612742020-11-12 13:34:03 +0100218 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
219 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100220
Angel Ponsca2f68a2020-03-22 13:15:12 +0100221 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Pons81378062020-11-12 13:46:21 +0100222 /* Debug parameters - only applies to Ivy Bridge */
Angel Pons7a612742020-11-12 13:34:03 +0100223 MCHBAR32(TC_DTP_ch(channel)) = tc_dtp.raw;
Angel Ponsca2f68a2020-03-22 13:15:12 +0100224 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100225
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100226 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100227
Angel Pons7a612742020-11-12 13:34:03 +0100228 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
229 MCHBAR32(TC_RFTP_ch(channel)) = tc_rftp.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +0100230
Angel Pons7a612742020-11-12 13:34:03 +0100231 union tc_rfp_reg tc_rfp = {
232 .raw = MCHBAR32(TC_RFP_ch(channel)),
233 };
234 tc_rfp.oref_ri = 0xff;
235 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100236
Angel Pons7a612742020-11-12 13:34:03 +0100237 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
238 MCHBAR32(TC_SRFTP_ch(channel)) = tc_srftp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100239 }
240}
241
242void dram_dimm_mapping(ramctr_timing *ctrl)
243{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100244 int channel;
245 dimm_info *info = &ctrl->info;
246
247 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200248 dimm_attr *dimmA, *dimmB;
249 u32 reg = 0;
250
Angel Pons7c49cb82020-03-16 23:17:32 +0100251 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100252 dimmA = &info->dimm[channel][0];
253 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100254 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100255 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100256 dimmA = &info->dimm[channel][1];
257 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100258 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100259 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100260
Nico Huberac4f2162017-10-01 18:14:43 +0200261 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100262 reg |= (dimmA->size_mb / 256) << 0;
263 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200264 reg |= (dimmA->width / 8 - 1) << 19;
265 }
266
267 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100268 reg |= (dimmB->size_mb / 256) << 8;
269 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200270 reg |= (dimmB->width / 8 - 1) << 20;
271 }
272
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200273 /*
274 * Rank interleave: Bit 16 of the physical address space sets
275 * the rank to use in a dual single rank DIMM configuration.
276 * That results in every 64KiB being interleaved between two ranks.
277 */
278 reg |= 1 << 21;
279 /* Enhanced interleave */
280 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200281
Angel Pons7c49cb82020-03-16 23:17:32 +0100282 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100283 ctrl->mad_dimm[channel] = reg;
284 } else {
285 ctrl->mad_dimm[channel] = 0;
286 }
287 }
288}
289
Patrick Rudolphdd662872017-10-28 18:20:11 +0200290void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100291{
292 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200293 u32 ecc;
294
295 if (ctrl->ecc_enabled)
296 ecc = training ? (1 << 24) : (3 << 24);
297 else
298 ecc = 0;
299
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100300 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200301 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100302 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200303
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200304 if (ctrl->ecc_enabled)
305 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100306}
307
Angel Pons88521882020-01-05 20:21:20 +0100308void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100309{
310 u32 reg, ch0size, ch1size;
311 u8 val;
312 reg = 0;
313 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100314
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100315 if (training) {
316 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
317 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
318 } else {
319 ch0size = ctrl->channel_size_mb[0];
320 ch1size = ctrl->channel_size_mb[1];
321 }
322
323 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100324 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100325 val = ch1size / 256;
326 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100327 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100328 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100329 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100330
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100331 } else {
Angel Pons88521882020-01-05 20:21:20 +0100332 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100333 val = ch0size / 256;
334 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100335 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100336 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100337 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100338 }
339}
340
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100341#define DEFAULT_PCI_MMIO_SIZE 2048
342
343static unsigned int get_mmio_size(void)
344{
345 const struct device *dev;
346 const struct northbridge_intel_sandybridge_config *cfg = NULL;
347
Angel Ponsb31d1d72020-01-10 01:35:09 +0100348 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100349 if (dev)
350 cfg = dev->chip_info;
351
352 /* If this is zero, it just means devicetree.cb didn't set it */
353 if (!cfg || cfg->pci_mmio_size == 0)
354 return DEFAULT_PCI_MMIO_SIZE;
355 else
356 return cfg->pci_mmio_size;
357}
358
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200359/*
360 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
361 * The ME/PCU/.. has the ability to change this.
362 * Return 0: ECC is optional
363 * Return 1: ECC is forced
364 */
365bool get_host_ecc_forced(void)
366{
367 /* read Capabilities A Register */
368 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
369 return !!(reg32 & (1 << 24));
370}
371
372/*
373 * Returns the ECC capability.
374 * The ME/PCU/.. has the ability to change this.
375 * Return 0: ECC is disabled
376 * Return 1: ECC is possible
377 */
378bool get_host_ecc_cap(void)
379{
380 /* read Capabilities A Register */
381 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
382 return !(reg32 & (1 << 25));
383}
384
Angel Pons88521882020-01-05 20:21:20 +0100385void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100386{
Angel Pons7c49cb82020-03-16 23:17:32 +0100387 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
388 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
389 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100390 uint16_t ggc;
391
392 mmiosize = get_mmio_size();
393
Felix Held87ddea22020-01-26 04:55:27 +0100394 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100395 if (!(ggc & 2)) {
396 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100397 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100398 } else {
399 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100400 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100401 }
402
403 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
404
405 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
406
407 mestolenbase = tom - me_uma_size;
408
Angel Pons7c49cb82020-03-16 23:17:32 +0100409 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
410
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100411 gfxstolenbase = toludbase - gfxstolen;
412 gttbase = gfxstolenbase - gttsize;
413
414 tsegbase = gttbase - tsegsize;
415
Angel Pons7c49cb82020-03-16 23:17:32 +0100416 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100417 tsegbasedelta = tsegbase & (tsegsize - 1);
418 tsegbase &= ~(tsegsize - 1);
419
420 gttbase -= tsegbasedelta;
421 gfxstolenbase -= tsegbasedelta;
422 toludbase -= tsegbasedelta;
423
Angel Pons7c49cb82020-03-16 23:17:32 +0100424 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100425 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100426 /* Reclaim is possible */
427 reclaim = 1;
428 remapbase = MAX(4096, tom - me_uma_size);
429 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
430 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100431 } else {
Angel Ponsc728e252021-01-03 16:47:09 +0100432 /* Reclaim not possible */
Angel Pons7c49cb82020-03-16 23:17:32 +0100433 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100434 touudbase = tom - me_uma_size;
435 }
436
Angel Pons7c49cb82020-03-16 23:17:32 +0100437 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100438 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
439
Angel Pons7c49cb82020-03-16 23:17:32 +0100440 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442 val = tom & 0xfff;
443 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100444 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100445 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100446
Angel Ponsb31d1d72020-01-10 01:35:09 +0100447 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100448 val = tom & 0xfffff000;
449 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100450 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100451 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100452
Angel Pons7c49cb82020-03-16 23:17:32 +0100453 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100454 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100455 val = toludbase & 0xfff;
456 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100457 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100458 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100459
Angel Pons7c49cb82020-03-16 23:17:32 +0100460 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100461 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100462 val = touudbase & 0xfff;
463 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100464 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100465 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100466
Angel Pons7c49cb82020-03-16 23:17:32 +0100467 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100468 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100469 val = touudbase & 0xfffff000;
470 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100471 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100472 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100473
474 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100475 /* REMAP BASE */
476 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100477 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100478
Angel Pons7c49cb82020-03-16 23:17:32 +0100479 /* REMAP LIMIT */
480 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100481 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100482 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100483 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100484 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100485 val = tsegbase & 0xfff;
486 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100487 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100488 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100489
Angel Pons7c49cb82020-03-16 23:17:32 +0100490 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100491 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100492 val = gfxstolenbase & 0xfff;
493 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100494 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100495 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100496
Angel Pons7c49cb82020-03-16 23:17:32 +0100497 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100498 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100499 val = gttbase & 0xfff;
500 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100501 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100502 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100503
504 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100505 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100506 val = (0x80000 - me_uma_size) & 0xfffff000;
507 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100508 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100509 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100510
Angel Pons7c49cb82020-03-16 23:17:32 +0100511 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513 val = mestolenbase & 0xfff;
514 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100515 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100516 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100517
Angel Ponsb31d1d72020-01-10 01:35:09 +0100518 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100519 val = mestolenbase & 0xfffff000;
520 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100521 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100522 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100523
Angel Pons7c49cb82020-03-16 23:17:32 +0100524 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100525 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526 val = (0x80000 - me_uma_size) & 0xfff;
527 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100528 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
529 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100530 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100531 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100532 }
533}
534
Angel Pons88521882020-01-05 20:21:20 +0100535static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100536{
537 int channel, slotrank;
538
Angel Pons7c49cb82020-03-16 23:17:32 +0100539 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100540 channel = (ctrl->rankmap[0]) ? 0 : 1;
541
Angel Pons88521882020-01-05 20:21:20 +0100542 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100543
Angel Pons7c49cb82020-03-16 23:17:32 +0100544 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100545 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
546
Angel Ponsffd50152020-11-12 11:03:10 +0100547 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100548
Angel Ponsedd7cb42020-12-07 12:17:17 +0100549 /* This is actually using the IOSAV state machine as a timer */
Angel Pons38d901e2020-05-02 23:50:43 +0200550 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200551
Angel Pons88521882020-01-05 20:21:20 +0100552 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100553}
554
Angel Pons88521882020-01-05 20:21:20 +0100555void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100556{
Felix Held9fe248f2018-07-31 20:59:45 +0200557 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100558 int channel;
559
Angel Pons7c49cb82020-03-16 23:17:32 +0100560 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
561 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100562 do {
Angel Pons88521882020-01-05 20:21:20 +0100563 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100564 } while ((reg & 0x14) == 0);
565
Angel Pons7c49cb82020-03-16 23:17:32 +0100566 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100567 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100568 MCHBAR32(MC_INIT_STATE_G) = reg;
569 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100571 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100572
Angel Pons7c49cb82020-03-16 23:17:32 +0100573 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100574 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100575
Angel Pons7c49cb82020-03-16 23:17:32 +0100576 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100577 udelay(200);
578
Angel Pons7c49cb82020-03-16 23:17:32 +0100579 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100580 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581
Angel Pons7c49cb82020-03-16 23:17:32 +0100582 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100583 udelay(500);
584
Angel Pons7c49cb82020-03-16 23:17:32 +0100585 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100586 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100587
Angel Pons7c49cb82020-03-16 23:17:32 +0100588 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100589 udelay(1);
590
591 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100592 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200593 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100594 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100595
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Wait 10ns for ranks to settle */
597 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100598
599 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100600 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100601
Angel Pons7c49cb82020-03-16 23:17:32 +0100602 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100603 write_reset(ctrl);
604 }
605}
606
Angel Pons3d3bf482020-11-14 16:18:15 +0100607/*
608 * DDR3 Rank1 Address mirror swap the following pins:
609 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
610 */
611static void ddr3_mirror_mrreg(int *bank, u32 *addr)
612{
613 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
614 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
615}
616
Angel Pons7c49cb82020-03-16 23:17:32 +0100617static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100618{
Angel Pons88521882020-01-05 20:21:20 +0100619 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100620
Angel Pons3d3bf482020-11-14 16:18:15 +0100621 if (ctrl->rank_mirror[channel][slotrank])
622 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100623
Angel Pons8f0757e2020-11-11 23:03:36 +0100624 const struct iosav_ssq sequence[] = {
625 /* DRAM command MRS */
626 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200627 .sp_cmd_ctrl = {
628 .command = IOSAV_MRS,
629 },
630 .subseq_ctrl = {
631 .cmd_executions = 1,
632 .cmd_delay_gap = 4,
633 .post_ssq_wait = 4,
634 .data_direction = SSQ_NA,
635 },
636 .sp_cmd_addr = {
637 .address = val,
638 .rowbits = 6,
639 .bank = reg,
640 .rank = slotrank,
641 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100642 },
643 /* DRAM command MRS */
644 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200645 .sp_cmd_ctrl = {
646 .command = IOSAV_MRS,
647 .ranksel_ap = 1,
648 },
649 .subseq_ctrl = {
650 .cmd_executions = 1,
651 .cmd_delay_gap = 4,
652 .post_ssq_wait = 4,
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 /* DRAM command MRS */
663 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200664 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100665 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200666 },
667 .subseq_ctrl = {
668 .cmd_executions = 1,
669 .cmd_delay_gap = 4,
670 .post_ssq_wait = ctrl->tMOD,
671 .data_direction = SSQ_NA,
672 },
673 .sp_cmd_addr = {
674 .address = val,
675 .rowbits = 6,
676 .bank = reg,
677 .rank = slotrank,
678 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100679 },
680 };
681 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200682
Angel Pons9f4ed3b2020-12-07 12:34:36 +0100683 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100684}
685
Angel Pons09fc4b92020-11-19 12:02:07 +0100686/* Obtain optimal power down mode for current configuration */
687static enum pdwm_mode get_power_down_mode(ramctr_timing *ctrl)
688{
689 if (ctrl->tXP > 8)
690 return PDM_NONE;
691
692 if (ctrl->tXPDLL > 32)
693 return PDM_PPD;
694
695 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
696 return PDM_DLL_OFF;
697
698 return PDM_APD_PPD;
699}
700
Angel Pons88521882020-01-05 20:21:20 +0100701static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100702{
703 u16 mr0reg, mch_cas, mch_wr;
704 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 +0100705
706 const enum pdwm_mode power_down = get_power_down_mode(ctrl);
707
708 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100709
Angel Pons7c49cb82020-03-16 23:17:32 +0100710 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100711 if (ctrl->CAS < 12) {
712 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
713 } else {
714 mch_cas = (u16) (ctrl->CAS - 12);
715 mch_cas = ((mch_cas << 1) | 0x1);
716 }
717
Angel Pons7c49cb82020-03-16 23:17:32 +0100718 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100719 mch_wr = mch_wr_t[ctrl->tWR - 5];
720
Angel Pons2bf28ed2020-11-12 13:49:59 +0100721 /* DLL Reset - self clearing - set after CLK frequency has been changed */
722 mr0reg = 1 << 8;
723
724 mr0reg |= (mch_cas & 0x1) << 2;
725 mr0reg |= (mch_cas & 0xe) << 3;
726 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100727
Angel Pons09fc4b92020-11-19 12:02:07 +0100728 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
729 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100730 return mr0reg;
731}
732
733static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
734{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200735 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100736}
737
Angel Ponsf9997482020-11-12 16:02:52 +0100738static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100739{
740 /* Get ODT based on rankmap */
741 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
742
743 if (dimms_per_ch == 1) {
744 return (const odtmap){60, 60};
745 } else {
746 return (const odtmap){120, 30};
747 }
748}
749
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100750static u32 encode_odt(u32 odt)
751{
752 switch (odt) {
753 case 30:
Angel Ponsc728e252021-01-03 16:47:09 +0100754 return (1 << 9) | (1 << 2); /* RZQ/8, RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100755 case 60:
Angel Ponsc728e252021-01-03 16:47:09 +0100756 return (1 << 2); /* RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100757 case 120:
Angel Ponsc728e252021-01-03 16:47:09 +0100758 return (1 << 6); /* RZQ/2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100759 default:
760 case 0:
761 return 0;
762 }
763}
764
765static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
766{
767 odtmap odt;
768 u32 mr1reg;
769
Angel Ponsf9997482020-11-12 16:02:52 +0100770 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100771 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100772
773 mr1reg |= encode_odt(odt.rttnom);
774
775 return mr1reg;
776}
777
778static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
779{
780 u16 mr1reg;
781
782 mr1reg = make_mr1(ctrl, rank, channel);
783
784 write_mrreg(ctrl, channel, rank, 1, mr1reg);
785}
786
787static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
788{
Angel Pons868bca22020-11-13 13:38:04 +0100789 const u16 pasr = 0;
790 const u16 cwl = ctrl->CWL - 5;
791 const odtmap odt = get_ODT(ctrl, channel);
792
Angel Ponsdca3cb52020-11-13 13:42:07 +0100793 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100794 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
795 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100796
Angel Pons868bca22020-11-13 13:38:04 +0100797 u16 mr2reg = 0;
798 mr2reg |= pasr;
799 mr2reg |= cwl << 3;
800 mr2reg |= ctrl->auto_self_refresh << 6;
801 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100802 mr2reg |= (odt.rttwr / 60) << 9;
803
804 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100805
806 /* Program MR2 shadow */
807 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
808
809 reg32 &= 3 << 14 | 3 << 6;
810
811 reg32 |= mr2reg & ~(3 << 6);
812
Angel Pons927b1c02020-12-10 22:11:27 +0100813 if (srt)
814 reg32 |= 1 << (rank / 2 + 6);
815
816 if (ctrl->rank_mirror[channel][rank])
817 reg32 |= 1 << (rank / 2 + 14);
818
Angel Pons7f1363d2020-11-13 13:31:58 +0100819 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100820}
821
822static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
823{
824 write_mrreg(ctrl, channel, rank, 3, 0);
825}
826
Angel Pons88521882020-01-05 20:21:20 +0100827void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100828{
829 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100830 int channel;
831
832 FOR_ALL_POPULATED_CHANNELS {
833 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100834 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100835 dram_mr2(ctrl, slotrank, channel);
836
Angel Pons7c49cb82020-03-16 23:17:32 +0100837 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100838 dram_mr3(ctrl, slotrank, channel);
839
Angel Pons7c49cb82020-03-16 23:17:32 +0100840 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100841 dram_mr1(ctrl, slotrank, channel);
842
Angel Pons7c49cb82020-03-16 23:17:32 +0100843 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100844 dram_mr0(ctrl, slotrank, channel);
845 }
846 }
847
Angel Pons8f0757e2020-11-11 23:03:36 +0100848 const struct iosav_ssq zqcl_sequence[] = {
849 /* DRAM command NOP (without ODT nor chip selects) */
850 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200851 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100852 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200853 },
854 .subseq_ctrl = {
855 .cmd_executions = 1,
856 .cmd_delay_gap = 4,
857 .post_ssq_wait = 15,
858 .data_direction = SSQ_NA,
859 },
860 .sp_cmd_addr = {
861 .address = 2,
862 .rowbits = 6,
863 .bank = 0,
864 .rank = 0,
865 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100866 },
867 /* DRAM command ZQCL */
868 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200869 .sp_cmd_ctrl = {
870 .command = IOSAV_ZQCS,
871 .ranksel_ap = 1,
872 },
873 .subseq_ctrl = {
874 .cmd_executions = 1,
875 .cmd_delay_gap = 4,
876 .post_ssq_wait = 400,
877 .data_direction = SSQ_NA,
878 },
879 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +0100880 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +0200881 .rowbits = 6,
882 .bank = 0,
883 .rank = 0,
884 },
885 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100886 .inc_rank = 1,
887 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200888 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100889 },
890 };
891 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100892
Angel Pons38d901e2020-05-02 23:50:43 +0200893 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100894
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100895 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +0100896 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100897 }
898
Angel Pons7c49cb82020-03-16 23:17:32 +0100899 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100900 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901
902 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100903 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100904
Angel Pons88521882020-01-05 20:21:20 +0100905 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100906
907 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
908
Angel Pons88521882020-01-05 20:21:20 +0100909 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100910
Angel Ponsffd50152020-11-12 11:03:10 +0100911 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200912
Angel Ponsa853e7a2020-12-07 12:28:38 +0100913 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100914 }
915}
916
Felix Held3b906032020-01-14 17:05:43 +0100917static const u32 lane_base[] = {
918 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
919 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
920 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100921};
922
Angel Pons42d033a2021-01-03 15:26:37 +0100923/* Maximum delay for command, control, clock */
924#define CCC_MAX_PI (2 * QCLK_PI - 1)
925
Angel Pons88521882020-01-05 20:21:20 +0100926void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100927{
Angel Pons7584e552020-11-19 21:34:32 +0100928 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100929 int lane;
930 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100931
Angel Pons7584e552020-11-19 21:34:32 +0100932 u32 ctl_delay[NUM_SLOTS] = { 0 };
933 int cmd_delay = 0;
934
935 /* Enable CLK XOVER */
936 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
937 u32 clk_logic_dly = 0;
938
939 /*
Angel Pons7519ca42021-01-12 01:21:24 +0100940 * Compute command timing as abs() of the most negative PI code
941 * across all ranks. Use zero if none of the values is negative.
Angel Pons7584e552020-11-19 21:34:32 +0100942 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100943 FOR_ALL_POPULATED_RANKS {
Angel Pons7519ca42021-01-12 01:21:24 +0100944 cmd_delay = MAX(cmd_delay, -ctrl->timings[channel][slotrank].pi_coding);
Angel Pons7584e552020-11-19 21:34:32 +0100945 }
Angel Pons42d033a2021-01-03 15:26:37 +0100946 if (cmd_delay > CCC_MAX_PI) {
Angel Pons7584e552020-11-19 21:34:32 +0100947 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
Angel Pons42d033a2021-01-03 15:26:37 +0100948 cmd_delay = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100949 }
950
Angel Pons89200d22021-01-12 01:04:04 +0100951 for (slot = 0; slot < NUM_SLOTS; slot++) {
952 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
953 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
Angel Pons7584e552020-11-19 21:34:32 +0100954
Angel Pons89200d22021-01-12 01:04:04 +0100955 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
Angel Pons7584e552020-11-19 21:34:32 +0100956
Angel Pons89200d22021-01-12 01:04:04 +0100957 if (slot_map & 1)
958 ctl_delay[slot] += pi_coding_0 + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100959
Angel Pons89200d22021-01-12 01:04:04 +0100960 if (slot_map & 2)
961 ctl_delay[slot] += pi_coding_1 + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100962
Angel Pons89200d22021-01-12 01:04:04 +0100963 /* If both ranks in a slot are populated, use the average */
964 if (slot_map == 3)
965 ctl_delay[slot] /= 2;
Angel Pons7584e552020-11-19 21:34:32 +0100966
Angel Pons89200d22021-01-12 01:04:04 +0100967 if (ctl_delay[slot] > CCC_MAX_PI) {
968 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
969 channel, slot, ctl_delay[slot]);
970 ctl_delay[slot] = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100971 }
Angel Pons89200d22021-01-12 01:04:04 +0100972 }
973 FOR_ALL_POPULATED_RANKS {
Angel Pons0a7d99c2021-01-12 01:13:08 +0100974 int clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100975
Angel Pons0a7d99c2021-01-12 01:13:08 +0100976 /*
977 * Clock is a differential signal, whereas command and control are not.
978 * This affects its timing, and it is also why it needs a magic offset.
979 */
980 clk_delay += ctrl->pi_code_offset;
981
982 /* Can never happen with valid values */
983 if (clk_delay < 0) {
984 printk(BIOS_ERR, "C%dR%d clock delay underflow: %d\n",
Angel Pons89200d22021-01-12 01:04:04 +0100985 channel, slotrank, clk_delay);
Angel Pons0a7d99c2021-01-12 01:13:08 +0100986 clk_delay = 0;
Angel Pons7584e552020-11-19 21:34:32 +0100987 }
Angel Pons89200d22021-01-12 01:04:04 +0100988
Angel Pons0a7d99c2021-01-12 01:13:08 +0100989 /* Clock can safely wrap around because it is a periodic signal */
990 clk_delay %= CCC_MAX_PI + 1;
991
Angel Pons89200d22021-01-12 01:04:04 +0100992 clk_pi_coding |= (clk_delay % QCLK_PI) << (6 * slotrank);
993 clk_logic_dly |= (clk_delay / QCLK_PI) << slotrank;
Angel Pons7584e552020-11-19 21:34:32 +0100994 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100995
Angel Pons7c49cb82020-03-16 23:17:32 +0100996 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100997 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
998 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
999 };
Angel Pons42d033a2021-01-03 15:26:37 +01001000 cmd_pi_coding.cmd_pi_code = cmd_delay % QCLK_PI;
1001 cmd_pi_coding.cmd_logic_delay = cmd_delay / QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001002
Angel Pons42d033a2021-01-03 15:26:37 +01001003 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % QCLK_PI;
1004 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % QCLK_PI;
1005 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / QCLK_PI;
1006 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / QCLK_PI;
Angel Pons737f1112020-11-13 14:07:30 +01001007
1008 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001009
Angel Pons7584e552020-11-19 21:34:32 +01001010 MCHBAR32(GDCRCKPICODE_ch(channel)) = clk_pi_coding;
1011 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = clk_logic_dly;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001012
Angel Pons88521882020-01-05 20:21:20 +01001013 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001014 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001015
Angel Pons88521882020-01-05 20:21:20 +01001016 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001017
1018 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001019 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001020
Angel Pons88521882020-01-05 20:21:20 +01001021 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001022 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001023
1024 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001025 const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
1026 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p;
1027 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n;
Angel Pons9fcc1102020-11-19 22:23:13 +01001028 const union gdcr_rx_reg gdcr_rx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001029 .rcven_pi_code = rcven % QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001030 .rx_dqs_p_pi_code = dqs_p,
Angel Pons42d033a2021-01-03 15:26:37 +01001031 .rcven_logic_delay = rcven / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001032 .rx_dqs_n_pi_code = dqs_n,
1033 };
1034 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) = gdcr_rx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001035
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001036 const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1037 const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq;
Angel Pons9fcc1102020-11-19 22:23:13 +01001038 const union gdcr_tx_reg gdcr_tx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001039 .tx_dq_pi_code = tx_dq % QCLK_PI,
1040 .tx_dqs_pi_code = tx_dqs % QCLK_PI,
1041 .tx_dqs_logic_delay = tx_dqs / QCLK_PI,
1042 .tx_dq_logic_delay = tx_dq / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001043 };
1044 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) = gdcr_tx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001045 }
1046 }
Angel Pons88521882020-01-05 20:21:20 +01001047 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1048 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001049}
1050
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001051static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001052{
Angel Pons88521882020-01-05 20:21:20 +01001053 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001054
Angel Pons3aed6ac2020-12-07 02:00:41 +01001055 /* Send a burst of 16 back-to-back read commands (4 DCLK apart) */
Angel Ponsffd50152020-11-12 11:03:10 +01001056 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001057
Angel Ponsa853e7a2020-12-07 12:28:38 +01001058 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001059}
1060
Angel Pons7c49cb82020-03-16 23:17:32 +01001061static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001062{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001063 u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
Angel Pons7c49cb82020-03-16 23:17:32 +01001064
1065 return (MCHBAR32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001066 GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001067}
1068
1069struct run {
1070 int middle;
1071 int end;
1072 int start;
1073 int all;
1074 int length;
1075};
1076
1077static struct run get_longest_zero_run(int *seq, int sz)
1078{
1079 int i, ls;
1080 int bl = 0, bs = 0;
1081 struct run ret;
1082
1083 ls = 0;
1084 for (i = 0; i < 2 * sz; i++)
1085 if (seq[i % sz]) {
1086 if (i - ls > bl) {
1087 bl = i - ls;
1088 bs = ls;
1089 }
1090 ls = i + 1;
1091 }
1092 if (bl == 0) {
1093 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001094 ret.start = 0;
1095 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001096 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001097 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001098 return ret;
1099 }
1100
Angel Pons7c49cb82020-03-16 23:17:32 +01001101 ret.start = bs % sz;
1102 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001103 ret.middle = (bs + (bl - 1) / 2) % sz;
1104 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001105 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001106
1107 return ret;
1108}
1109
Angel Pons42d033a2021-01-03 15:26:37 +01001110#define RCVEN_COARSE_PI_LENGTH (2 * QCLK_PI)
1111
Angel Ponsf3053392020-11-13 23:31:12 +01001112static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001113{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001114 int rcven;
Angel Pons42d033a2021-01-03 15:26:37 +01001115 int statistics[NUM_LANES][RCVEN_COARSE_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001116 int lane;
1117
Angel Pons42d033a2021-01-03 15:26:37 +01001118 for (rcven = 0; rcven < RCVEN_COARSE_PI_LENGTH; rcven++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001119 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001120 ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001121 }
1122 program_timings(ctrl, channel);
1123
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001124 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001125
1126 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001127 statistics[lane][rcven] =
1128 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001129 }
1130 }
1131 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001132 struct run rn = get_longest_zero_run(statistics[lane], RCVEN_COARSE_PI_LENGTH);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001133 ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001134 upperA[lane] = rn.end;
1135 if (upperA[lane] < rn.middle)
Angel Pons42d033a2021-01-03 15:26:37 +01001136 upperA[lane] += 2 * QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001137
Angel Pons7e439c92020-12-07 11:56:01 +01001138 printram("rcven: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001139 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001140 }
1141}
1142
Angel Ponsf3053392020-11-13 23:31:12 +01001143static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001144{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001145 int rcven_delta;
Angel Pons86e3d742021-01-03 14:55:12 +01001146 int statistics[NUM_LANES][51] = {0};
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001147 int lane, i;
1148
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001149 for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001150
1151 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001152 ctrl->timings[channel][slotrank].lanes[lane].rcven
Angel Pons42d033a2021-01-03 15:26:37 +01001153 = upperA[lane] + rcven_delta + QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001154 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001155 program_timings(ctrl, channel);
1156
1157 for (i = 0; i < 100; i++) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001158 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001159 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001160 statistics[lane][rcven_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001161 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001162 }
1163 }
1164 }
1165 FOR_ALL_LANES {
1166 int last_zero, first_all;
1167
1168 for (last_zero = -25; last_zero <= 25; last_zero++)
1169 if (statistics[lane][last_zero + 25])
1170 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001171
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001172 last_zero--;
1173 for (first_all = -25; first_all <= 25; first_all++)
1174 if (statistics[lane][first_all + 25] == 100)
1175 break;
1176
Angel Pons7c49cb82020-03-16 23:17:32 +01001177 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001178
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001179 ctrl->timings[channel][slotrank].lanes[lane].rcven =
Angel Pons7c49cb82020-03-16 23:17:32 +01001180 (last_zero + first_all) / 2 + upperA[lane];
1181
Angel Pons7e439c92020-12-07 11:56:01 +01001182 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001183 lane, ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001184 }
1185}
1186
Angel Pons3aed6ac2020-12-07 02:00:41 +01001187/*
1188 * Once the DQS high phase has been found (for each DRAM) the next stage
1189 * is to find out the round trip latency, by locating the preamble cycle.
1190 * This is achieved by trying smaller and smaller roundtrip values until
1191 * the strobe sampling is done on the preamble cycle.
1192 */
Angel Ponsf3053392020-11-13 23:31:12 +01001193static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001194{
1195 int works[NUM_LANES];
1196 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001197
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001198 while (1) {
1199 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001200
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001201 program_timings(ctrl, channel);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001202 test_rcven(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001203
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001204 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001205 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1206
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001207 if (works[lane])
1208 some_works = 1;
1209 else
1210 all_works = 0;
1211 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001212
1213 /* If every lane is working, exit */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 if (all_works)
1215 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001216
Angel Pons3aed6ac2020-12-07 02:00:41 +01001217 /*
1218 * If all bits are one (everyone is failing), decrement
1219 * the roundtrip value by two, and do another iteration.
1220 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001221 if (!some_works) {
Angel Pons3aed6ac2020-12-07 02:00:41 +01001222 /* Guard against roundtrip latency underflow */
Angel Pons88521882020-01-05 20:21:20 +01001223 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Angel Pons30791632020-12-12 12:28:29 +01001224 printk(BIOS_EMERG, "Roundtrip latency underflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001225 channel, slotrank);
1226 return MAKE_ERR;
1227 }
Angel Pons88521882020-01-05 20:21:20 +01001228 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 printram("4024 -= 2;\n");
1230 continue;
1231 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001232
1233 /*
1234 * Else (if some lanes are failing), increase the rank's
1235 * I/O latency by 2, and increase rcven logic delay by 2
1236 * on the working lanes, then perform another iteration.
1237 */
Felix Heldef4fe3e2019-12-31 14:15:05 +01001238 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001239 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001240
Angel Pons3aed6ac2020-12-07 02:00:41 +01001241 /* Guard against I/O latency overflow */
Angel Pons5db1b152020-12-13 16:37:53 +01001242 if (ctrl->timings[channel][slotrank].io_latency >= 16) {
Angel Pons30791632020-12-12 12:28:29 +01001243 printk(BIOS_EMERG, "I/O latency overflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001244 channel, slotrank);
1245 return MAKE_ERR;
1246 }
1247 FOR_ALL_LANES if (works[lane]) {
Angel Pons42d033a2021-01-03 15:26:37 +01001248 ctrl->timings[channel][slotrank].lanes[lane].rcven += 2 * QCLK_PI;
1249 upperA[lane] += 2 * QCLK_PI;
Angel Pons891f2bc2020-01-10 01:27:28 +01001250 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001251 }
1252 }
1253 return 0;
1254}
1255
Angel Pons12bd8ab2020-11-13 23:10:52 +01001256static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001257{
1258 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001259 u16 logic_delay_min = 7;
1260 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001261
1262 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001263 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001264
1265 logic_delay_min = MIN(logic_delay_min, logic_delay);
1266 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001267 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001268
1269 if (logic_delay_max < logic_delay_min) {
1270 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1271 logic_delay_max, logic_delay_min, channel, slotrank);
1272 }
1273
1274 assert(logic_delay_max >= logic_delay_min);
1275
1276 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001277}
1278
Angel Pons12bd8ab2020-11-13 23:10:52 +01001279static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001280{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001281 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001282
Angel Pons7c49cb82020-03-16 23:17:32 +01001283 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001284 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001285
Angel Pons12bd8ab2020-11-13 23:10:52 +01001286 if (prev < post)
1287 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001288
Angel Pons12bd8ab2020-11-13 23:10:52 +01001289 else if (prev > post)
1290 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001291
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001292 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001293 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001294
Angel Pons12bd8ab2020-11-13 23:10:52 +01001295 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1296 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1297 printram("4024 += %d;\n", latency_offset);
1298 printram("4028 += %d;\n", latency_offset);
1299
1300 return post;
1301}
1302
1303static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1304{
1305 u16 logic_delay_min = 7;
1306 int lane;
1307
1308 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001309 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001310
1311 logic_delay_min = MIN(logic_delay_min, logic_delay);
1312 }
1313
1314 if (logic_delay_min >= 2) {
1315 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1316 logic_delay_min, channel, slotrank);
1317 }
1318
1319 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001320 ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001321 }
1322 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1323 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001324}
1325
Angel Pons7f5a97c2020-11-13 16:58:46 +01001326int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001327{
1328 int channel, slotrank, lane;
1329 int err;
1330
1331 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1332 int all_high, some_high;
1333 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001334 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001335
Angel Pons88521882020-01-05 20:21:20 +01001336 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001337
Angel Ponsffd50152020-11-12 11:03:10 +01001338 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001339
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001340 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001341
Angel Pons58b609b2020-11-13 14:35:29 +01001342 const union gdcr_training_mod_reg training_mod = {
1343 .receive_enable_mode = 1,
1344 .training_rank_sel = slotrank,
1345 .odt_always_on = 1,
1346 };
1347 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001348
Felix Heldef4fe3e2019-12-31 14:15:05 +01001349 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001350 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001351 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001352
Angel Ponsf3053392020-11-13 23:31:12 +01001353 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001354
Felix Held2bb3cdf2018-07-28 00:23:59 +02001355 all_high = 1;
1356 some_high = 0;
1357 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001358 if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= QCLK_PI)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001359 some_high = 1;
1360 else
1361 all_high = 0;
1362 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001363
1364 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001365 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001366 printram("4028--;\n");
1367 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001368 ctrl->timings[channel][slotrank].lanes[lane].rcven -= QCLK_PI;
1369 upperA[lane] -= QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001370
1371 }
1372 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001373 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001374 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001375 printram("4024++;\n");
1376 printram("4028++;\n");
1377 }
1378
1379 program_timings(ctrl, channel);
1380
Angel Pons12bd8ab2020-11-13 23:10:52 +01001381 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
Angel Ponsf3053392020-11-13 23:31:12 +01001383 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384 if (err)
1385 return err;
1386
Angel Pons12bd8ab2020-11-13 23:10:52 +01001387 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388
Angel Ponsf3053392020-11-13 23:31:12 +01001389 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390
Angel Pons12bd8ab2020-11-13 23:10:52 +01001391 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001392
Angel Pons12bd8ab2020-11-13 23:10:52 +01001393 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394
Angel Pons12bd8ab2020-11-13 23:10:52 +01001395 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396
Angel Pons7e439c92020-12-07 11:56:01 +01001397 printram("4/8: %d, %d, % 4d, % 4d\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001398 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001399 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001400
1401 printram("final results:\n");
1402 FOR_ALL_LANES
Angel Pons7e439c92020-12-07 11:56:01 +01001403 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001404 ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001405
Angel Pons88521882020-01-05 20:21:20 +01001406 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001407
1408 toggle_io_reset();
1409 }
1410
1411 FOR_ALL_POPULATED_CHANNELS {
1412 program_timings(ctrl, channel);
1413 }
Angel Ponsc6742232020-11-15 13:26:21 +01001414
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001415 return 0;
1416}
1417
Angel Pons011661c2020-11-15 18:21:35 +01001418static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001419{
1420 int lane;
1421
1422 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001423 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1424 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001425 }
1426
Angel Pons88521882020-01-05 20:21:20 +01001427 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001428
Angel Ponsffd50152020-11-12 11:03:10 +01001429 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1430 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001431
Angel Ponsa853e7a2020-12-07 12:28:38 +01001432 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001433
Angel Pons801a5cb2020-11-15 15:48:29 +01001434 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001435
Angel Ponsa853e7a2020-12-07 12:28:38 +01001436 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001437}
1438
Angel Pons011661c2020-11-15 18:21:35 +01001439static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001440{
1441 int min = data[0];
1442 int max = min;
1443 int i;
1444 for (i = 1; i < count; i++) {
1445 if (min > data[i])
1446 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001447
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001448 if (max < data[i])
1449 max = data[i];
1450 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001451 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001452 for (i = 0; i < count; i++)
1453 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001454
Angel Pons891f2bc2020-01-10 01:27:28 +01001455 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001456}
1457
Angel Pons011661c2020-11-15 18:21:35 +01001458static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001459{
Angel Pons011661c2020-11-15 18:21:35 +01001460 int tx_dq;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001461 int stats[NUM_LANES][MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001462 int lane;
1463
Angel Pons88521882020-01-05 20:21:20 +01001464 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001465
Angel Ponsffd50152020-11-12 11:03:10 +01001466 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001467
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001468 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001469
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001470 for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) {
1471 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001472 program_timings(ctrl, channel);
1473
Angel Pons011661c2020-11-15 18:21:35 +01001474 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001475
1476 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001477 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001478 }
1479 }
1480 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001481 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1482
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001483 if (rn.all || rn.length < 8) {
Angel Pons30791632020-12-12 12:28:29 +01001484 printk(BIOS_EMERG, "tx_dq write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001485 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001486 /*
1487 * With command training not being done yet, the lane can be erroneous.
1488 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001489 */
Angel Pons011661c2020-11-15 18:21:35 +01001490 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001491 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1492
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001493 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001494 printk(BIOS_EMERG, "tx_dq recovery failed\n");
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001495 return MAKE_ERR;
1496 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001497 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001498 ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle;
Angel Pons7e439c92020-12-07 11:56:01 +01001499 printram("tx_dq: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001500 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001501 }
1502 return 0;
1503}
1504
Angel Pons88521882020-01-05 20:21:20 +01001505static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001506{
1507 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001508
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001509 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1510 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001511
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001512 return ret;
1513}
1514
Angel Pons765d4652020-11-11 14:44:35 +01001515/* Each cacheline is 64 bits long */
1516static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1517{
1518 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1519}
1520
Angel Pons88521882020-01-05 20:21:20 +01001521static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001522{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301523 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001524 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
Angel Pons7c49cb82020-03-16 23:17:32 +01001525
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001526 for (j = 0; j < 16; j++)
1527 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001528
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001529 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001530
1531 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001532}
1533
Angel Pons88521882020-01-05 20:21:20 +01001534static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001535{
1536 int ret = 0;
1537 int channel;
1538 FOR_ALL_POPULATED_CHANNELS ret++;
1539 return ret;
1540}
1541
Angel Pons88521882020-01-05 20:21:20 +01001542static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001543{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301544 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001545 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
1546 unsigned int channel_step = 64 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001547
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001548 for (j = 0; j < 16; j++)
1549 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001550
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001551 for (j = 0; j < 16; j++)
1552 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001553
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001554 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001555
1556 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557}
1558
Angel Pons42d033a2021-01-03 15:26:37 +01001559#define TX_DQS_PI_LENGTH (2 * QCLK_PI)
1560
Angel Pons820bce72020-11-14 17:02:55 +01001561static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001562{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001563 int tx_dqs;
Angel Pons42d033a2021-01-03 15:26:37 +01001564 int statistics[NUM_LANES][TX_DQS_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001565 int lane;
1566
Angel Pons58b609b2020-11-13 14:35:29 +01001567 const union gdcr_training_mod_reg training_mod = {
1568 .write_leveling_mode = 1,
1569 .training_rank_sel = slotrank,
1570 .enable_dqs_wl = 5,
1571 .odt_always_on = 1,
1572 .force_drive_enable = 1,
1573 };
1574 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001575
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001576 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1577 int bank = 1;
1578
1579 if (ctrl->rank_mirror[channel][slotrank])
1580 ddr3_mirror_mrreg(&bank, &mr1reg);
1581
1582 wait_for_iosav(channel);
1583
1584 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1585
Angel Pons42d033a2021-01-03 15:26:37 +01001586 for (tx_dqs = 0; tx_dqs < TX_DQS_PI_LENGTH; tx_dqs++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001587 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001588 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001589 }
1590 program_timings(ctrl, channel);
1591
Angel Ponsa853e7a2020-12-07 12:28:38 +01001592 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001593
1594 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001595 statistics[lane][tx_dqs] = !((MCHBAR32(lane_base[lane] +
1596 GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >>
1597 (tx_dqs % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001598 }
1599 }
1600 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001601 struct run rn = get_longest_zero_run(statistics[lane], TX_DQS_PI_LENGTH);
Angel Pons7c49cb82020-03-16 23:17:32 +01001602 /*
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001603 * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value
1604 * of tx_dqs by a small value, which might cause the 6-bit value to overflow if
Angel Pons7c49cb82020-03-16 23:17:32 +01001605 * it's close to 0x3f. Increment the value by a small offset if it's likely
1606 * to overflow, to make sure it won't overflow while running tests and bricks
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001607 * the system due to a non matching tx_dq.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001608 *
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001609 * TODO: find out why some tests (edge write discovery) increment tx_dqs.
Angel Pons7c49cb82020-03-16 23:17:32 +01001610 */
1611 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001612 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001613 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001614 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001615
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001616 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001617 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01001618 printk(BIOS_EMERG, "JEDEC write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001620
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001621 return MAKE_ERR;
1622 }
Angel Pons7e439c92020-12-07 11:56:01 +01001623 printram("tx_dqs: %d, %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01001624 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001625 }
1626 return 0;
1627}
1628
Angel Pons820bce72020-11-14 17:02:55 +01001629static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001630{
1631 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001632 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001633 if (val == 0xffffffffffffffffLL)
1634 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001635 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001636 /* DQS is late, needs negative 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 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001641 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001642 for (i = 0; i < 8; i++)
1643 if (val >> (8 * (7 - i) + 4))
1644 return i;
1645 }
1646 return 8;
1647}
1648
Angel Ponsbf13ef02020-11-11 18:40:06 +01001649static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001650{
1651 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001652
1653 const union gdcr_training_mod_reg training_mod = {
1654 .dq_dqs_training_res = 1,
1655 };
1656 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1657
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001658 FOR_ALL_POPULATED_CHANNELS {
1659 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001660 }
1661 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1662
Angel Pons765d4652020-11-11 14:44:35 +01001663 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001664 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001665
Angel Pons88521882020-01-05 20:21:20 +01001666 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001667
Angel Ponsffd50152020-11-12 11:03:10 +01001668 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001669
Angel Ponsa853e7a2020-12-07 12:28:38 +01001670 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001671
Angel Pons8f0757e2020-11-11 23:03:36 +01001672 const struct iosav_ssq rd_sequence[] = {
1673 /* DRAM command PREA */
1674 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001675 .sp_cmd_ctrl = {
1676 .command = IOSAV_PRE,
1677 .ranksel_ap = 1,
1678 },
1679 .subseq_ctrl = {
1680 .cmd_executions = 1,
1681 .cmd_delay_gap = 3,
1682 .post_ssq_wait = ctrl->tRP,
1683 .data_direction = SSQ_NA,
1684 },
1685 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +01001686 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +02001687 .rowbits = 6,
1688 .bank = 0,
1689 .rank = slotrank,
1690 },
1691 .addr_update = {
1692 .addr_wrap = 18,
1693 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001694 },
1695 /* DRAM command ACT */
1696 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001697 .sp_cmd_ctrl = {
1698 .command = IOSAV_ACT,
1699 .ranksel_ap = 1,
1700 },
1701 .subseq_ctrl = {
1702 .cmd_executions = 1,
1703 .cmd_delay_gap = 3,
1704 .post_ssq_wait = ctrl->tRCD,
1705 .data_direction = SSQ_NA,
1706 },
1707 .sp_cmd_addr = {
1708 .address = 0,
1709 .rowbits = 6,
1710 .bank = 0,
1711 .rank = slotrank,
1712 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001713 },
Angel Ponsf5502312021-02-10 11:08:28 +01001714 /* DRAM command RDA */
Angel Pons8f0757e2020-11-11 23:03:36 +01001715 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001716 .sp_cmd_ctrl = {
1717 .command = IOSAV_RD,
1718 .ranksel_ap = 3,
1719 },
1720 .subseq_ctrl = {
1721 .cmd_executions = 1,
1722 .cmd_delay_gap = 3,
1723 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001724 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001725 ctrl->timings[channel][slotrank].io_latency,
1726 .data_direction = SSQ_RD,
1727 },
1728 .sp_cmd_addr = {
1729 .address = 8,
1730 .rowbits = 6,
1731 .bank = 0,
1732 .rank = slotrank,
1733 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001734 },
1735 };
1736 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001737
Angel Ponsa853e7a2020-12-07 12:28:38 +01001738 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001739
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001740 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001741 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001742 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001743 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001744
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001745 old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1746 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs +=
Angel Pons42d033a2021-01-03 15:26:37 +01001747 get_dqs_flyby_adjust(res) * QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001748
1749 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons7e439c92020-12-07 11:56:01 +01001750 printram("Bval+: %d, %d, %d, % 4d -> % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001751 old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001752 }
1753 }
Angel Pons88521882020-01-05 20:21:20 +01001754 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001755}
1756
Angel Pons7d115132020-11-14 01:44:44 +01001757static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001758{
Angel Pons7d115132020-11-14 01:44:44 +01001759 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001760
Angel Pons7d115132020-11-14 01:44:44 +01001761 FOR_ALL_POPULATED_CHANNELS {
1762 /* choose an existing rank */
1763 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001764
Angel Pons7d115132020-11-14 01:44:44 +01001765 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001766
Angel Ponsa853e7a2020-12-07 12:28:38 +01001767 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001768
Angel Pons7d115132020-11-14 01:44:44 +01001769 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1770 }
1771
1772 /* Refresh disable */
1773 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1774
1775 FOR_ALL_POPULATED_CHANNELS {
1776 /* Execute the same command queue */
Angel Ponsa853e7a2020-12-07 12:28:38 +01001777 iosav_run_once_and_wait(channel);
Angel Pons7d115132020-11-14 01:44:44 +01001778 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001779}
1780
Angel Pons7c49cb82020-03-16 23:17:32 +01001781/*
1782 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001783 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001784 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1785 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1786 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1787 * CLK/ADDR/CMD signals have the same routing delay.
1788 *
1789 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1790 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1791 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001792 */
Angel Pons820bce72020-11-14 17:02:55 +01001793static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001794{
Angel Pons820bce72020-11-14 17:02:55 +01001795 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001796
Angel Pons7d115132020-11-14 01:44:44 +01001797 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001798
Angel Pons7c49cb82020-03-16 23:17:32 +01001799 /* Enable write leveling on all ranks
1800 Disable all DQ outputs
1801 Only NOP is allowed in this mode */
1802 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1803 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001804 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001805
Angel Ponsa1f17142020-11-15 12:50:03 +01001806 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001807 const union gdcr_training_mod_reg training_mod = {
1808 .write_leveling_mode = 1,
1809 .enable_dqs_wl = 5,
1810 .odt_always_on = 1,
1811 .force_drive_enable = 1,
1812 };
1813 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001814
1815 toggle_io_reset();
1816
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001817 /* Set any valid value for tx_dqs, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001818 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001819 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001820 if (err)
1821 return err;
1822 }
1823
Angel Pons7c49cb82020-03-16 23:17:32 +01001824 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001825 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001826 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001827
Angel Pons88521882020-01-05 20:21:20 +01001828 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001829
1830 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001831 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001832
Angel Pons7c49cb82020-03-16 23:17:32 +01001833 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001834 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001835
1836 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001837 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001838 MCHBAR32(IOSAV_STATUS_ch(channel));
1839 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001840
Angel Ponsffd50152020-11-12 11:03:10 +01001841 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001842
Angel Ponsa853e7a2020-12-07 12:28:38 +01001843 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001844 }
1845
1846 toggle_io_reset();
1847
Angel Pons820bce72020-11-14 17:02:55 +01001848 return 0;
1849}
1850
1851int write_training(ramctr_timing *ctrl)
1852{
Angel Ponsc6742232020-11-15 13:26:21 +01001853 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001854 int err;
1855
Angel Pons4d192822020-12-12 13:54:37 +01001856 /*
1857 * Set the DEC_WRD bit, required for the write flyby algorithm.
1858 * Needs to be done before starting the write training procedure.
1859 */
Angel Pons820bce72020-11-14 17:02:55 +01001860 FOR_ALL_POPULATED_CHANNELS
1861 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1862
Angel Pons4c76d252020-11-15 13:06:53 +01001863 printram("CPE\n");
1864
Angel Pons820bce72020-11-14 17:02:55 +01001865 err = jedec_write_leveling(ctrl);
1866 if (err)
1867 return err;
1868
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001869 printram("CPF\n");
1870
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001871 FOR_ALL_POPULATED_CHANNELS {
1872 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001873 }
1874
1875 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001876 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001877 if (err)
1878 return err;
1879 }
1880
1881 FOR_ALL_POPULATED_CHANNELS
1882 program_timings(ctrl, channel);
1883
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001884 /* measure and adjust tx_dqs timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001885 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001886
1887 FOR_ALL_POPULATED_CHANNELS
1888 program_timings(ctrl, channel);
1889
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001890 return 0;
1891}
1892
Angel Ponsbf13ef02020-11-11 18:40:06 +01001893static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001894{
1895 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001896 int tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001897 int lanes_ok = 0;
1898 int ctr = 0;
1899 int lane;
1900
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001901 for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001902 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001903 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
1904 saved_rt.lanes[lane].tx_dq + tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001905 }
1906 program_timings(ctrl, channel);
1907 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001908 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001909 }
1910
Angel Pons765d4652020-11-11 14:44:35 +01001911 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001912 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001913
Angel Pons88521882020-01-05 20:21:20 +01001914 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001915
Angel Ponsffd50152020-11-12 11:03:10 +01001916 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001917
1918 /* Program LFSR for the RD/WR subsequences */
1919 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1920 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001921
Angel Ponsa853e7a2020-12-07 12:28:38 +01001922 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001923
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001924 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001925 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001926
1927 if (r32 == 0)
1928 lanes_ok |= 1 << lane;
1929 }
1930 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001931 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001932 break;
1933 }
1934
1935 ctrl->timings[channel][slotrank] = saved_rt;
1936
Patrick Rudolphdd662872017-10-28 18:20:11 +02001937 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001938}
1939
Angel Pons88521882020-01-05 20:21:20 +01001940static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001941{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301942 unsigned int i, j;
Angel Pons5db1b152020-12-13 16:37:53 +01001943 unsigned int offset = get_precedening_channels(ctrl, channel) * 64;
1944 unsigned int step = 64 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001945
1946 if (patno) {
1947 u8 base8 = 0x80 >> ((patno - 1) % 8);
1948 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1949 for (i = 0; i < 32; i++) {
1950 for (j = 0; j < 16; j++) {
1951 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001952
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001953 if (invert[patno - 1][i] & (1 << (j / 2)))
1954 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001955
1956 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001957 }
1958 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001959 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001960 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1961 for (j = 0; j < 16; j++) {
1962 const u32 val = pattern[i][j];
1963 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1964 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001965 }
1966 sfence();
1967 }
Angel Pons765d4652020-11-11 14:44:35 +01001968
1969 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970}
1971
Angel Pons88521882020-01-05 20:21:20 +01001972static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001973{
Angel Pons7d115132020-11-14 01:44:44 +01001974 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001975
Angel Pons7c49cb82020-03-16 23:17:32 +01001976 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001977 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001978
1979 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001980 dram_mrscommands(ctrl);
1981
1982 toggle_io_reset();
1983}
1984
Angel Pons42d033a2021-01-03 15:26:37 +01001985#define CT_MIN_PI (-CCC_MAX_PI)
1986#define CT_MAX_PI (+CCC_MAX_PI + 1)
Angel Ponsbf13ef02020-11-11 18:40:06 +01001987#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1988
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001989#define MIN_C320C_LEN 13
1990
1991static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
1992{
1993 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
1994 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001995 int command_pi;
1996 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001997 int delta = 0;
1998
1999 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2000
2001 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002002 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002003 }
2004
2005 ctrl->cmd_stretch[channel] = cmd_stretch;
2006
Angel Pons7a612742020-11-12 13:34:03 +01002007 const union tc_rap_reg tc_rap = {
2008 .tRRD = ctrl->tRRD,
2009 .tRTP = ctrl->tRTP,
2010 .tCKE = ctrl->tCKE,
2011 .tWTR = ctrl->tWTR,
2012 .tFAW = ctrl->tFAW,
2013 .tWR = ctrl->tWR,
2014 .tCMD = ctrl->cmd_stretch[channel],
2015 };
2016 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002017
2018 if (ctrl->cmd_stretch[channel] == 2)
2019 delta = 2;
2020 else if (ctrl->cmd_stretch[channel] == 0)
2021 delta = 4;
2022
2023 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002024 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002025 }
2026
Angel Ponsbf13ef02020-11-11 18:40:06 +01002027 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002028 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002029 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002030 }
2031 program_timings(ctrl, channel);
2032 reprogram_320c(ctrl);
2033 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002034 stat[slotrank][command_pi - CT_MIN_PI] =
2035 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002036 }
2037 }
2038 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002039 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002040
Angel Ponsbf13ef02020-11-11 18:40:06 +01002041 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Angel Pons7e439c92020-12-07 11:56:01 +01002042 printram("cmd_stretch: %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01002043 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002044
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002045 if (rn.all || rn.length < MIN_C320C_LEN) {
2046 FOR_ALL_POPULATED_RANKS {
2047 ctrl->timings[channel][slotrank] =
2048 saved_timings[channel][slotrank];
2049 }
2050 return MAKE_ERR;
2051 }
2052 }
2053
2054 return 0;
2055}
2056
Angel Pons7c49cb82020-03-16 23:17:32 +01002057/*
2058 * Adjust CMD phase shift and try multiple command rates.
2059 * A command rate of 2T doubles the time needed for address and command decode.
2060 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002061int command_training(ramctr_timing *ctrl)
2062{
2063 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002064
2065 FOR_ALL_POPULATED_CHANNELS {
2066 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002067 }
2068
2069 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002070 int cmdrate, err;
2071
2072 /*
2073 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002074 * Issue:
Angel Pons30791632020-12-12 12:28:29 +01002075 * While command training seems to succeed, raminit will fail in write training.
Angel Pons7c49cb82020-03-16 23:17:32 +01002076 *
2077 * Workaround:
2078 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2079 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002080 *
2081 * Single DIMM per channel:
2082 * Try command rate 1T and 2T
2083 */
2084 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002085 if (ctrl->tCMD)
2086 /* XMP gives the CMD rate in clock ticks, not ns */
2087 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002088
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002089 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002090 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2091
2092 if (!err)
2093 break;
2094 }
2095
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002096 if (err) {
Angel Pons30791632020-12-12 12:28:29 +01002097 printk(BIOS_EMERG, "Command training failed: %d\n", channel);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002098 return err;
2099 }
2100
Angel Pons891f2bc2020-01-10 01:27:28 +01002101 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002102 }
2103
2104 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002105 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002106
2107 reprogram_320c(ctrl);
2108 return 0;
2109}
2110
Angel Pons4c79f932020-11-14 01:26:52 +01002111static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002112{
Angel Pons96a06dd2020-11-14 00:33:18 +01002113 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002114 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002115 int lane;
2116
Angel Pons96a06dd2020-11-14 00:33:18 +01002117 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002118 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002119 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi;
2120 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002121 }
2122 program_timings(ctrl, channel);
2123
2124 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002125 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2126 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002127 }
2128
Angel Pons88521882020-01-05 20:21:20 +01002129 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002130
Angel Ponsffd50152020-11-12 11:03:10 +01002131 iosav_write_read_mpr_sequence(
2132 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002133
Angel Ponsa853e7a2020-12-07 12:28:38 +01002134 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002135
2136 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002137 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002138 }
2139 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002140
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002141 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002142 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002144
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002145 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002146 printk(BIOS_EMERG, "Read MPR training failed: %d, %d, %d\n", channel,
Angel Pons7c49cb82020-03-16 23:17:32 +01002147 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002148 return MAKE_ERR;
2149 }
Angel Pons7e439c92020-12-07 11:56:01 +01002150 printram("eval %d, %d, %d: % 4d\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002151 }
2152 return 0;
2153}
2154
Angel Pons60971dc2020-11-14 00:49:38 +01002155static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2156{
2157 int slotrank, lane;
2158
2159 fill_pattern0(ctrl, channel, 0, 0);
2160 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002161 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002162 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2163 }
2164
2165 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002166 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16;
2167 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16;
Angel Pons60971dc2020-11-14 00:49:38 +01002168 }
2169
2170 program_timings(ctrl, channel);
2171
2172 FOR_ALL_POPULATED_RANKS {
2173 wait_for_iosav(channel);
2174
2175 iosav_write_read_mpr_sequence(
2176 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2177
Angel Ponsa853e7a2020-12-07 12:28:38 +01002178 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002179 }
2180
2181 /* XXX: check any measured value ? */
2182
2183 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002184 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48;
2185 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48;
Angel Pons60971dc2020-11-14 00:49:38 +01002186 }
2187
2188 program_timings(ctrl, channel);
2189
2190 FOR_ALL_POPULATED_RANKS {
2191 wait_for_iosav(channel);
2192
2193 iosav_write_read_mpr_sequence(
2194 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2195
Angel Ponsa853e7a2020-12-07 12:28:38 +01002196 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002197 }
2198
2199 /* XXX: check any measured value ? */
2200
2201 FOR_ALL_LANES {
2202 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2203 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2204 }
2205}
2206
Angel Pons4c79f932020-11-14 01:26:52 +01002207int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002208{
2209 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2210 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2211 int channel, slotrank, lane;
2212 int err;
2213
Angel Pons88521882020-01-05 20:21:20 +01002214 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002215
2216 toggle_io_reset();
2217
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002218 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002219 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002220
2221 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002222 }
2223
Angel Pons0c3936e2020-03-22 12:49:27 +01002224 /*
2225 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2226 * also use a single loop. It would seem that it is a debugging configuration.
2227 */
Angel Pons5db1b152020-12-13 16:37:53 +01002228 MCHBAR32(IOSAV_DC_MASK) = 3 << 8;
2229 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002230
2231 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002232 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002233 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002234 if (err)
2235 return err;
2236 }
2237
Angel Pons5db1b152020-12-13 16:37:53 +01002238 MCHBAR32(IOSAV_DC_MASK) = 2 << 8;
2239 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002240
2241 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002242 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002243 rising_edges[channel][slotrank]);
2244 if (err)
2245 return err;
2246 }
2247
Angel Pons88521882020-01-05 20:21:20 +01002248 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002249
2250 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002251 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002252 falling_edges[channel][slotrank][lane];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002253 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002254 rising_edges[channel][slotrank][lane];
2255 }
2256
2257 FOR_ALL_POPULATED_CHANNELS {
2258 program_timings(ctrl, channel);
2259 }
2260
Angel Pons50a6fe72020-11-14 01:18:14 +01002261 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002262 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002263 }
2264 return 0;
2265}
2266
Angel Pons08f749d2020-11-17 16:50:56 +01002267static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002268{
Angel Pons08f749d2020-11-17 16:50:56 +01002269 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2270
Angel Pons7c49cb82020-03-16 23:17:32 +01002271 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002272 int lower[NUM_LANES];
2273 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002274 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002275
2276 FOR_ALL_LANES {
2277 lower[lane] = 0;
2278 upper[lane] = MAX_EDGE_TIMING;
2279 }
2280
Angel Pons08f749d2020-11-17 16:50:56 +01002281 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002282 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002283 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002284 };
2285 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2286 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002287
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002288 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2289 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002290 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002291
Angel Pons08f749d2020-11-17 16:50:56 +01002292 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002293 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002294 ctrl->timings[channel][slotrank].lanes[lane]
2295 .rx_dqs_p = read_pi;
2296 ctrl->timings[channel][slotrank].lanes[lane]
2297 .rx_dqs_n = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002298 }
2299 program_timings(ctrl, channel);
2300
2301 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002302 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2303 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002304 }
Angel Pons88521882020-01-05 20:21:20 +01002305 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002306
Angel Ponsffd50152020-11-12 11:03:10 +01002307 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002308
Angel Ponsa853e7a2020-12-07 12:28:38 +01002309 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002310
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002311 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002312 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002313 }
2314
Angel Pons7c49cb82020-03-16 23:17:32 +01002315 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002316 raw_stats[read_pi] = MCHBAR32(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 */
2348 MCHBAR32(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 Pons5db1b152020-12-13 16:37:53 +01002363 MCHBAR32(IOSAV_DC_MASK) = 3 << 8;
2364 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 Pons5db1b152020-12-13 16:37:53 +01002373 MCHBAR32(IOSAV_DC_MASK) = 2 << 8;
2374 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 Pons88521882020-01-05 20:21:20 +01002383 MCHBAR32(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 Pons2a7d7522020-11-19 12:49:07 +01002410 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24);
2411 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)
2439 MCHBAR32(MCMNTS_SPARE) = 1;
2440
Angel Pons30791632020-12-12 12:28:29 +01002441 printram("Aggresive 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 Ponsc8ac2cc2020-11-19 22:50:54 +01002467 raw_stats[tx_dq] = MCHBAR32(
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]);
2500
2501 }
2502 }
2503 }
2504 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002505 }
2506
Angel Pons2a7d7522020-11-19 12:49:07 +01002507 FOR_ALL_CHANNELS {
2508 /* Restore nominal write Vref after training */
2509 set_write_vref(channel, 0);
2510 }
2511
2512 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2513 if (enable_iosav_opt)
2514 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002515
2516 printram("CPB\n");
2517
2518 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7e439c92020-12-07 11:56:01 +01002519 printram("tx_dq %d, %d, %d: % 4d\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002520 (lower[channel][slotrank][lane] +
2521 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002522
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002523 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002524 (lower[channel][slotrank][lane] +
2525 upper[channel][slotrank][lane]) / 2;
2526 }
2527 FOR_ALL_POPULATED_CHANNELS {
2528 program_timings(ctrl, channel);
2529 }
2530 return 0;
2531}
2532
Angel Pons88521882020-01-05 20:21:20 +01002533void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002534{
2535 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002536 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002537
2538 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2539 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002540 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002541 FOR_ALL_LANES mat =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002542 MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002543 printram("normalize %d, %d, %d: mat %d\n",
2544 channel, slotrank, lane, mat);
2545
Felix Heldef4fe3e2019-12-31 14:15:05 +01002546 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002547 printram("normalize %d, %d, %d: delta %d\n",
2548 channel, slotrank, lane, delta);
2549
Angel Pons88521882020-01-05 20:21:20 +01002550 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002551 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002552 }
2553
2554 FOR_ALL_POPULATED_CHANNELS {
2555 program_timings(ctrl, channel);
2556 }
2557}
2558
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002559int channel_test(ramctr_timing *ctrl)
2560{
2561 int channel, slotrank, lane;
2562
2563 slotrank = 0;
2564 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002565 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002566 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002567 return MAKE_ERR;
2568 }
2569 FOR_ALL_POPULATED_CHANNELS {
2570 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002571 }
2572
2573 for (slotrank = 0; slotrank < 4; slotrank++)
2574 FOR_ALL_CHANNELS
2575 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2576 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002577 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2578 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002579 }
Angel Pons88521882020-01-05 20:21:20 +01002580 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002581
Angel Ponsffd50152020-11-12 11:03:10 +01002582 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002583
Angel Ponsa853e7a2020-12-07 12:28:38 +01002584 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002585
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002586 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002587 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002588 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2589 channel, slotrank, lane);
2590 return MAKE_ERR;
2591 }
2592 }
2593 return 0;
2594}
2595
Patrick Rudolphdd662872017-10-28 18:20:11 +02002596void channel_scrub(ramctr_timing *ctrl)
2597{
2598 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002599 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002600
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002601 FOR_ALL_POPULATED_CHANNELS {
2602 wait_for_iosav(channel);
2603 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002604 }
2605
2606 /*
2607 * During runtime the "scrubber" will periodically scan through the memory in the
2608 * physical address space, to identify and fix CRC errors.
2609 * The following loops writes to every DRAM address, setting the ECC bits to the
2610 * correct value. A read from this location will no longer return a CRC error,
2611 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002612 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002613 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2614 * and firmware running in x86_32.
2615 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002616 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2617 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002618 for (bank = 0; bank < 8; bank++) {
2619 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002620
Angel Pons8f0757e2020-11-11 23:03:36 +01002621 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2622 const struct iosav_ssq sequence[] = {
2623 /*
2624 * DRAM command ACT
2625 * Opens the row for writing.
2626 */
2627 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002628 .sp_cmd_ctrl = {
2629 .command = IOSAV_ACT,
2630 .ranksel_ap = 1,
2631 },
2632 .subseq_ctrl = {
2633 .cmd_executions = 1,
2634 .cmd_delay_gap = gap,
2635 .post_ssq_wait = ctrl->tRCD,
2636 .data_direction = SSQ_NA,
2637 },
2638 .sp_cmd_addr = {
2639 .address = row,
2640 .rowbits = 6,
2641 .bank = bank,
2642 .rank = slotrank,
2643 },
2644 .addr_update = {
2645 .inc_addr_1 = 1,
2646 .addr_wrap = 18,
2647 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002648 },
2649 /*
2650 * DRAM command WR
2651 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2652 * bytes.
2653 */
2654 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002655 .sp_cmd_ctrl = {
2656 .command = IOSAV_WR,
2657 .ranksel_ap = 1,
2658 },
2659 .subseq_ctrl = {
2660 .cmd_executions = 129,
2661 .cmd_delay_gap = 4,
2662 .post_ssq_wait = ctrl->tWTR +
2663 ctrl->CWL + 8,
2664 .data_direction = SSQ_WR,
2665 },
2666 .sp_cmd_addr = {
2667 .address = row,
2668 .rowbits = 0,
2669 .bank = bank,
2670 .rank = slotrank,
2671 },
2672 .addr_update = {
2673 .inc_addr_8 = 1,
2674 .addr_wrap = 9,
2675 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002676 },
2677 /*
2678 * DRAM command PRE
2679 * Closes the row.
2680 */
2681 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002682 .sp_cmd_ctrl = {
2683 .command = IOSAV_PRE,
2684 .ranksel_ap = 1,
2685 },
2686 .subseq_ctrl = {
2687 .cmd_executions = 1,
2688 .cmd_delay_gap = 4,
2689 .post_ssq_wait = ctrl->tRP,
2690 .data_direction = SSQ_NA,
2691 },
2692 .sp_cmd_addr = {
2693 .address = 0,
2694 .rowbits = 6,
2695 .bank = bank,
2696 .rank = slotrank,
2697 },
2698 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002699 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002700 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002701 },
2702 };
2703 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002704
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002705 iosav_run_queue(channel, 16, 0);
2706
2707 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002708 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002709 }
2710 }
2711}
2712
Angel Pons88521882020-01-05 20:21:20 +01002713void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002714{
2715 int channel;
2716
Angel Pons7c49cb82020-03-16 23:17:32 +01002717 /* 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 +01002718 static u32 seeds[NUM_CHANNELS][3] = {
2719 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2720 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2721 };
2722 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002723 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002724 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2725 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2726 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002727 }
2728}
2729
Angel Pons89ae6b82020-03-21 13:23:32 +01002730void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002731{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002732 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002733 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002734 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002735 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002736 }
2737}
2738
Angel Pons88521882020-01-05 20:21:20 +01002739void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002740{
2741 int channel;
2742
2743 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002744 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002745 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002746 }
2747
2748 udelay(1);
2749
2750 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002751 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002752 }
2753}
2754
Angel Pons7c49cb82020-03-16 23:17:32 +01002755void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002756{
Angel Pons11463322020-11-19 11:04:28 +01002757 /* Use a larger delay when running fast to improve stability */
2758 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2759
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002760 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002761
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002762 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002763 int min_pi = 10000;
2764 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002765
2766 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002767 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2768 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002769 }
2770
Angel Pons7a612742020-11-12 13:34:03 +01002771 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002772
Angel Pons7a612742020-11-12 13:34:03 +01002773 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002774
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002775 dram_odt_stretch(ctrl, channel);
2776
Angel Pons7a612742020-11-12 13:34:03 +01002777 const union tc_rwp_reg tc_rwp = {
2778 .tRRDR = 0,
2779 .tRRDD = val,
2780 .tWWDR = val,
2781 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002782 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002783 .tWRDRDD = tWRDRDD,
2784 .tRWSR = 2,
2785 .dec_wrd = 1,
2786 };
2787 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002788 }
2789}
2790
Angel Pons88521882020-01-05 20:21:20 +01002791void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002792{
2793 int channel;
2794 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002795 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2796 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002797 }
2798}
2799
Angel Pons7c49cb82020-03-16 23:17:32 +01002800/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2801static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002802{
Angel Pons88521882020-01-05 20:21:20 +01002803 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002804}
2805
Angel Pons7c49cb82020-03-16 23:17:32 +01002806/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002807void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002808{
2809 int channel;
2810 int t1_cycles = 0, t1_ns = 0, t2_ns;
2811 int t3_ns;
2812 u32 r32;
2813
Angel Pons7c49cb82020-03-16 23:17:32 +01002814 /* FIXME: This register only exists on Ivy Bridge */
2815 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002816
Angel Pons7a612742020-11-12 13:34:03 +01002817 FOR_ALL_CHANNELS {
2818 union tc_othp_reg tc_othp = {
2819 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2820 };
2821 tc_othp.tCPDED = 1;
2822 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2823 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002824
Angel Pons09fc4b92020-11-19 12:02:07 +01002825 /* 64 DCLKs until idle, decision per rank */
2826 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002827
Felix Heldf9b826a2018-07-30 17:56:52 +02002828 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002829 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002830
Angel Ponsc728e252021-01-03 16:47:09 +01002831 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff;
2832 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002833
2834 FOR_ALL_CHANNELS {
2835 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002836 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002837 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002838 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002839 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002840 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002841 case 1:
2842 case 4:
2843 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002844 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002845 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002846 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002847 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002848 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002849 break;
2850 }
2851 }
2852
Felix Held50b7ed22019-12-30 20:41:54 +01002853 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002854 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002855 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002856
Angel Pons7a612742020-11-12 13:34:03 +01002857 FOR_ALL_CHANNELS {
2858 union tc_rfp_reg tc_rfp = {
2859 .raw = MCHBAR32(TC_RFP_ch(channel)),
2860 };
2861 tc_rfp.refresh_2x_control = 1;
2862 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2863 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002864
Angel Ponsdc5539f2020-11-12 12:44:25 +01002865 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2866 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002867 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002868
Angel Pons7c49cb82020-03-16 23:17:32 +01002869 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002870 FOR_ALL_POPULATED_CHANNELS
2871 break;
2872
Angel Pons88521882020-01-05 20:21:20 +01002873 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2874 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002875 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002876 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002877 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002878 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002879 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002880 t1_ns += 500;
2881
Angel Pons88521882020-01-05 20:21:20 +01002882 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002883 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002884 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002885 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002886 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002887 t3_ns = 500;
2888 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002889
2890 /* The graphics driver will use these watermark values */
2891 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002892 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002893 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2894 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002895}
2896
Angel Pons88521882020-01-05 20:21:20 +01002897void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002898{
Angel Ponsc6742232020-11-15 13:26:21 +01002899 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002900
Angel Pons7c49cb82020-03-16 23:17:32 +01002901 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002902 const union tc_rap_reg tc_rap = {
2903 .tRRD = ctrl->tRRD,
2904 .tRTP = ctrl->tRTP,
2905 .tCKE = ctrl->tCKE,
2906 .tWTR = ctrl->tWTR,
2907 .tFAW = ctrl->tFAW,
2908 .tWR = ctrl->tWR,
2909 .tCMD = ctrl->cmd_stretch[channel],
2910 };
2911 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002912 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002913
2914 udelay(1);
2915
2916 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002917 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002918 }
2919
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002921 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002922
2923 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002924 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002925 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002926 }
2927
2928 printram("CPE\n");
2929
Angel Pons88521882020-01-05 20:21:20 +01002930 MCHBAR32(GDCRTRAININGMOD) = 0;
2931 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002932
2933 printram("CP5b\n");
2934
2935 FOR_ALL_POPULATED_CHANNELS {
2936 program_timings(ctrl, channel);
2937 }
2938
2939 u32 reg, addr;
2940
Angel Pons7c49cb82020-03-16 23:17:32 +01002941 /* Poll for RCOMP */
2942 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2943 ;
2944
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002945 do {
Angel Pons88521882020-01-05 20:21:20 +01002946 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002947 } while ((reg & 0x14) == 0);
2948
Angel Pons7c49cb82020-03-16 23:17:32 +01002949 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002950 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002951 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002952
Angel Pons7c49cb82020-03-16 23:17:32 +01002953 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002954 udelay(500);
2955
2956 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002957 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002958 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002959 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002960 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002961 MCHBAR32(addr) = reg;
2962
Angel Pons7c49cb82020-03-16 23:17:32 +01002963 /* Wait 10ns for ranks to settle */
2964 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002965
2966 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
2967 MCHBAR32(addr) = reg;
2968
Angel Pons7c49cb82020-03-16 23:17:32 +01002969 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002970 write_reset(ctrl);
2971 }
2972
Angel Pons7c49cb82020-03-16 23:17:32 +01002973 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002974 dram_mrscommands(ctrl);
2975
2976 printram("CP5c\n");
2977
Angel Pons88521882020-01-05 20:21:20 +01002978 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002979
2980 FOR_ALL_CHANNELS {
Angel Pons5db1b152020-12-13 16:37:53 +01002981 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002982 udelay(2);
2983 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002984}