blob: 9308ed521b7bea8dbfa3bdffd587dfb822690a6b [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>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01006#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010010#include <northbridge/intel/sandybridge/chip.h>
11#include <device/pci_def.h>
12#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020013#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020014
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010015#include "raminit_native.h"
16#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010017#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#include "sandybridge.h"
19
Angel Pons7c49cb82020-03-16 23:17:32 +010020/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010021
22static void sfence(void)
23{
24 asm volatile ("sfence");
25}
26
Angel Pons7c49cb82020-03-16 23:17:32 +010027/* Toggle IO reset bit */
28static void toggle_io_reset(void)
29{
Angel Pons88521882020-01-05 20:21:20 +010030 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010031 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010033 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010034 udelay(1);
35}
36
37static u32 get_XOVER_CLK(u8 rankmap)
38{
39 return rankmap << 24;
40}
41
42static u32 get_XOVER_CMD(u8 rankmap)
43{
44 u32 reg;
45
Angel Pons7c49cb82020-03-16 23:17:32 +010046 /* Enable xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010047 reg = 0x4000;
48
Angel Pons7c49cb82020-03-16 23:17:32 +010049 /* Enable xover ctl */
50 if (rankmap & 0x03)
51 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010052
Angel Pons7c49cb82020-03-16 23:17:32 +010053 if (rankmap & 0x0c)
54 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010055
56 return reg;
57}
58
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010059void dram_find_common_params(ramctr_timing *ctrl)
60{
61 size_t valid_dimms;
62 int channel, slot;
63 dimm_info *dimms = &ctrl->info;
64
65 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
66 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010067
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010068 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010069
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010070 const dimm_attr *dimm = &dimms->dimm[channel][slot];
71 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
72 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010073
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010074 valid_dimms++;
75
76 /* Find all possible CAS combinations */
77 ctrl->cas_supported &= dimm->cas_supported;
78
79 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010080 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
81 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
82 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
84 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010085 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010086 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
87 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
88 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
89 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
90 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030091 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
92 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010093 }
94
95 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010096 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
97
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010098 if (!valid_dimms)
99 die("No valid DIMMs found");
100}
101
Angel Pons88521882020-01-05 20:21:20 +0100102void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100103{
104 u32 reg;
105 int channel;
106
107 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100108 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100110 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
111 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100112
Angel Pons7c49cb82020-03-16 23:17:32 +0100113 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100115 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
116 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100117 }
118}
119
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100120static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100121{
Angel Pons89ae6b82020-03-21 13:23:32 +0100122 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100123
124 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 /*
126 * ODT stretch:
127 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
128 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100129 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100130 if (stretch == 2)
131 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100132
Angel Pons88521882020-01-05 20:21:20 +0100133 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100134 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100136 } else {
Angel Pons88521882020-01-05 20:21:20 +0100137 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100138 union tc_othp_reg tc_othp = {
139 .raw = MCHBAR32(addr),
140 };
141 tc_othp.odt_delay_d0 = stretch;
142 tc_othp.odt_delay_d1 = stretch;
143 MCHBAR32(addr) = tc_othp.raw;
Iru Cai89af71c2018-08-16 16:46:27 +0800144 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100145 }
146}
147
148void dram_timing_regs(ramctr_timing *ctrl)
149{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100150 int channel;
151
Angel Pons81378062020-11-12 13:46:21 +0100152 /* BIN parameters */
153 const union tc_dbp_reg tc_dbp = {
154 .tRCD = ctrl->tRCD,
155 .tRP = ctrl->tRP,
156 .tAA = ctrl->CAS,
157 .tCWL = ctrl->CWL,
158 .tRAS = ctrl->tRAS,
159 };
160
161 /* Regular access parameters */
162 const union tc_rap_reg tc_rap = {
163 .tRRD = ctrl->tRRD,
164 .tRTP = ctrl->tRTP,
165 .tCKE = ctrl->tCKE,
166 .tWTR = ctrl->tWTR,
167 .tFAW = ctrl->tFAW,
168 .tWR = ctrl->tWR,
169 .tCMD = 3,
170 };
171
172 /* Other parameters */
173 const union tc_othp_reg tc_othp = {
174 .tXPDLL = ctrl->tXPDLL,
175 .tXP = ctrl->tXP,
176 .tAONPD = ctrl->tAONPD,
177 .tCPDED = 2,
Angel Pons2ad03a42020-11-19 11:07:27 +0100178 .tPRPDEN = 1,
Angel Pons81378062020-11-12 13:46:21 +0100179 };
180
181 /*
182 * If tXP and tXPDLL are very high, we need to increase them by one.
183 * 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 {
432 // 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 Pons38d901e2020-05-02 23:50:43 +0200683 iosav_run_once(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:
754 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
755 case 60:
756 return (1 << 2); // RZQ/4
757 case 120:
758 return (1 << 6); // RZQ/2
759 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 = {
880 .address = 1024,
881 .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 Pons88521882020-01-05 20:21:20 +0100923void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100924{
Angel Pons7584e552020-11-19 21:34:32 +0100925 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100926 int lane;
927 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100928
Angel Pons7584e552020-11-19 21:34:32 +0100929 u32 ctl_delay[NUM_SLOTS] = { 0 };
930 int cmd_delay = 0;
931
932 /* Enable CLK XOVER */
933 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
934 u32 clk_logic_dly = 0;
935
936 /*
937 * Apply command delay if desired setting is negative. Find the
938 * most negative value: 'cmd_delay' will be the absolute value.
939 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940 FOR_ALL_POPULATED_RANKS {
Angel Pons7584e552020-11-19 21:34:32 +0100941 if (cmd_delay < -ctrl->timings[channel][slotrank].pi_coding)
942 cmd_delay = -ctrl->timings[channel][slotrank].pi_coding;
943 }
944 if (cmd_delay < 0) {
945 printk(BIOS_ERR, "C%d command delay underflow: %d\n", channel, cmd_delay);
946 cmd_delay = 0;
947 }
948 if (cmd_delay >= 128) {
949 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
950 cmd_delay = 127;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100951 }
952
Angel Pons7584e552020-11-19 21:34:32 +0100953 /* Apply control and clock delay if desired setting is positive */
954 if (cmd_delay == 0) {
955 for (slot = 0; slot < NUM_SLOTS; slot++) {
956 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
957 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
958
959 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
960
961 if (slot_map & 1)
962 ctl_delay[slot] += pi_coding_0 + cmd_delay;
963
964 if (slot_map & 2)
965 ctl_delay[slot] += pi_coding_1 + cmd_delay;
966
967 /* If both ranks in a slot are populated, use the average */
968 if (slot_map == 3)
969 ctl_delay[slot] /= 2;
970
971 if (ctl_delay[slot] >= 128) {
972 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
973 channel, slot, ctl_delay[slot]);
974 ctl_delay[slot] = 127;
975 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100976 }
Angel Pons7584e552020-11-19 21:34:32 +0100977 FOR_ALL_POPULATED_RANKS {
978 u32 clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
979
980 if (clk_delay >= 128) {
981 printk(BIOS_ERR, "C%dR%d clock delay overflow: %d\n",
982 channel, slotrank, clk_delay);
983 clk_delay = 127;
984 }
985
986 clk_pi_coding |= (clk_delay % 64) << (6 * slotrank);
987 clk_logic_dly |= (clk_delay / 64) << slotrank;
988 }
989 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100990
Angel Pons7c49cb82020-03-16 23:17:32 +0100991 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100992 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
993 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
994 };
Angel Pons7584e552020-11-19 21:34:32 +0100995 cmd_pi_coding.cmd_pi_code = cmd_delay % 64;
996 cmd_pi_coding.cmd_logic_delay = cmd_delay / 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100997
Angel Pons7584e552020-11-19 21:34:32 +0100998 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % 64;
999 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % 64;
1000 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / 64;
1001 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / 64;
Angel Pons737f1112020-11-13 14:07:30 +01001002
1003 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001004
Angel Pons7584e552020-11-19 21:34:32 +01001005 MCHBAR32(GDCRCKPICODE_ch(channel)) = clk_pi_coding;
1006 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = clk_logic_dly;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001007
Angel Pons88521882020-01-05 20:21:20 +01001008 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001009 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001010
Angel Pons88521882020-01-05 20:21:20 +01001011 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001012
1013 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001014 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001015
Angel Pons88521882020-01-05 20:21:20 +01001016 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001017 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001018
1019 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001020 const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
1021 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p;
1022 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n;
Angel Pons9fcc1102020-11-19 22:23:13 +01001023 const union gdcr_rx_reg gdcr_rx = {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001024 .rcven_pi_code = rcven % 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001025 .rx_dqs_p_pi_code = dqs_p,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001026 .rcven_logic_delay = rcven / 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001027 .rx_dqs_n_pi_code = dqs_n,
1028 };
1029 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) = gdcr_rx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001030
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001031 const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1032 const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq;
Angel Pons9fcc1102020-11-19 22:23:13 +01001033 const union gdcr_tx_reg gdcr_tx = {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001034 .tx_dq_pi_code = tx_dq % 64,
1035 .tx_dqs_pi_code = tx_dqs % 64,
1036 .tx_dqs_logic_delay = tx_dqs / 64,
1037 .tx_dq_logic_delay = tx_dq / 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001038 };
1039 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) = gdcr_tx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001040 }
1041 }
Angel Pons88521882020-01-05 20:21:20 +01001042 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1043 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001044}
1045
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001046static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001047{
Angel Pons88521882020-01-05 20:21:20 +01001048 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001049
Angel Pons3aed6ac2020-12-07 02:00:41 +01001050 /* Send a burst of 16 back-to-back read commands (4 DCLK apart) */
Angel Ponsffd50152020-11-12 11:03:10 +01001051 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001052
Angel Ponsa853e7a2020-12-07 12:28:38 +01001053 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001054}
1055
Angel Pons7c49cb82020-03-16 23:17:32 +01001056static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001057{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001058 u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
Angel Pons7c49cb82020-03-16 23:17:32 +01001059
1060 return (MCHBAR32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001061 GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001062}
1063
1064struct run {
1065 int middle;
1066 int end;
1067 int start;
1068 int all;
1069 int length;
1070};
1071
1072static struct run get_longest_zero_run(int *seq, int sz)
1073{
1074 int i, ls;
1075 int bl = 0, bs = 0;
1076 struct run ret;
1077
1078 ls = 0;
1079 for (i = 0; i < 2 * sz; i++)
1080 if (seq[i % sz]) {
1081 if (i - ls > bl) {
1082 bl = i - ls;
1083 bs = ls;
1084 }
1085 ls = i + 1;
1086 }
1087 if (bl == 0) {
1088 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001089 ret.start = 0;
1090 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001091 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001092 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001093 return ret;
1094 }
1095
Angel Pons7c49cb82020-03-16 23:17:32 +01001096 ret.start = bs % sz;
1097 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001098 ret.middle = (bs + (bl - 1) / 2) % sz;
1099 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001100 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001101
1102 return ret;
1103}
1104
Angel Ponsf3053392020-11-13 23:31:12 +01001105static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001106{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001107 int rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001108 int statistics[NUM_LANES][128];
1109 int lane;
1110
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001111 for (rcven = 0; rcven < 128; rcven++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001112 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001113 ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001114 }
1115 program_timings(ctrl, channel);
1116
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001117 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001118
1119 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001120 statistics[lane][rcven] =
1121 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001122 }
1123 }
1124 FOR_ALL_LANES {
1125 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001126 ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001127 upperA[lane] = rn.end;
1128 if (upperA[lane] < rn.middle)
1129 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001130
Angel Pons7e439c92020-12-07 11:56:01 +01001131 printram("rcven: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001132 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001133 }
1134}
1135
Angel Ponsf3053392020-11-13 23:31:12 +01001136static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001137{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001138 int rcven_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001139 int statistics[NUM_LANES][51];
1140 int lane, i;
1141
1142 memset(statistics, 0, sizeof(statistics));
1143
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001144 for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001145
1146 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001147 ctrl->timings[channel][slotrank].lanes[lane].rcven
1148 = upperA[lane] + rcven_delta + 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001149 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001150 program_timings(ctrl, channel);
1151
1152 for (i = 0; i < 100; i++) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001153 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001154 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001155 statistics[lane][rcven_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001156 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001157 }
1158 }
1159 }
1160 FOR_ALL_LANES {
1161 int last_zero, first_all;
1162
1163 for (last_zero = -25; last_zero <= 25; last_zero++)
1164 if (statistics[lane][last_zero + 25])
1165 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001166
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001167 last_zero--;
1168 for (first_all = -25; first_all <= 25; first_all++)
1169 if (statistics[lane][first_all + 25] == 100)
1170 break;
1171
Angel Pons7c49cb82020-03-16 23:17:32 +01001172 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001173
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001174 ctrl->timings[channel][slotrank].lanes[lane].rcven =
Angel Pons7c49cb82020-03-16 23:17:32 +01001175 (last_zero + first_all) / 2 + upperA[lane];
1176
Angel Pons7e439c92020-12-07 11:56:01 +01001177 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001178 lane, ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001179 }
1180}
1181
Angel Pons3aed6ac2020-12-07 02:00:41 +01001182/*
1183 * Once the DQS high phase has been found (for each DRAM) the next stage
1184 * is to find out the round trip latency, by locating the preamble cycle.
1185 * This is achieved by trying smaller and smaller roundtrip values until
1186 * the strobe sampling is done on the preamble cycle.
1187 */
Angel Ponsf3053392020-11-13 23:31:12 +01001188static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001189{
1190 int works[NUM_LANES];
1191 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001192
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 while (1) {
1194 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001195
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001196 program_timings(ctrl, channel);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001197 test_rcven(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001198
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001199 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001200 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1201
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001202 if (works[lane])
1203 some_works = 1;
1204 else
1205 all_works = 0;
1206 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001207
1208 /* If every lane is working, exit */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001209 if (all_works)
1210 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001211
Angel Pons3aed6ac2020-12-07 02:00:41 +01001212 /*
1213 * If all bits are one (everyone is failing), decrement
1214 * the roundtrip value by two, and do another iteration.
1215 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001216 if (!some_works) {
Angel Pons3aed6ac2020-12-07 02:00:41 +01001217 /* Guard against roundtrip latency underflow */
Angel Pons88521882020-01-05 20:21:20 +01001218 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Angel Pons30791632020-12-12 12:28:29 +01001219 printk(BIOS_EMERG, "Roundtrip latency underflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001220 channel, slotrank);
1221 return MAKE_ERR;
1222 }
Angel Pons88521882020-01-05 20:21:20 +01001223 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 printram("4024 -= 2;\n");
1225 continue;
1226 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001227
1228 /*
1229 * Else (if some lanes are failing), increase the rank's
1230 * I/O latency by 2, and increase rcven logic delay by 2
1231 * on the working lanes, then perform another iteration.
1232 */
Felix Heldef4fe3e2019-12-31 14:15:05 +01001233 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001234 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001235
Angel Pons3aed6ac2020-12-07 02:00:41 +01001236 /* Guard against I/O latency overflow */
Felix Heldef4fe3e2019-12-31 14:15:05 +01001237 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Angel Pons30791632020-12-12 12:28:29 +01001238 printk(BIOS_EMERG, "I/O latency overflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001239 channel, slotrank);
1240 return MAKE_ERR;
1241 }
1242 FOR_ALL_LANES if (works[lane]) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001243 ctrl->timings[channel][slotrank].lanes[lane].rcven += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001244 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001245 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001246 }
1247 }
1248 return 0;
1249}
1250
Angel Pons12bd8ab2020-11-13 23:10:52 +01001251static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001252{
1253 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001254 u16 logic_delay_min = 7;
1255 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001256
1257 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001258 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001259
1260 logic_delay_min = MIN(logic_delay_min, logic_delay);
1261 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001262 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001263
1264 if (logic_delay_max < logic_delay_min) {
1265 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1266 logic_delay_max, logic_delay_min, channel, slotrank);
1267 }
1268
1269 assert(logic_delay_max >= logic_delay_min);
1270
1271 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001272}
1273
Angel Pons12bd8ab2020-11-13 23:10:52 +01001274static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001275{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001276 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001277
Angel Pons7c49cb82020-03-16 23:17:32 +01001278 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001279 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001280
Angel Pons12bd8ab2020-11-13 23:10:52 +01001281 if (prev < post)
1282 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001283
Angel Pons12bd8ab2020-11-13 23:10:52 +01001284 else if (prev > post)
1285 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001286
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001287 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001288 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001289
Angel Pons12bd8ab2020-11-13 23:10:52 +01001290 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1291 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1292 printram("4024 += %d;\n", latency_offset);
1293 printram("4028 += %d;\n", latency_offset);
1294
1295 return post;
1296}
1297
1298static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1299{
1300 u16 logic_delay_min = 7;
1301 int lane;
1302
1303 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001304 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001305
1306 logic_delay_min = MIN(logic_delay_min, logic_delay);
1307 }
1308
1309 if (logic_delay_min >= 2) {
1310 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1311 logic_delay_min, channel, slotrank);
1312 }
1313
1314 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001315 ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001316 }
1317 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1318 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001319}
1320
Angel Pons7f5a97c2020-11-13 16:58:46 +01001321int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001322{
1323 int channel, slotrank, lane;
1324 int err;
1325
1326 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1327 int all_high, some_high;
1328 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001329 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001330
Angel Pons88521882020-01-05 20:21:20 +01001331 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001332
Angel Ponsffd50152020-11-12 11:03:10 +01001333 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001334
Angel Pons38d901e2020-05-02 23:50:43 +02001335 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001336
Angel Pons58b609b2020-11-13 14:35:29 +01001337 const union gdcr_training_mod_reg training_mod = {
1338 .receive_enable_mode = 1,
1339 .training_rank_sel = slotrank,
1340 .odt_always_on = 1,
1341 };
1342 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001343
Felix Heldef4fe3e2019-12-31 14:15:05 +01001344 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001345 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001346 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001347
Angel Ponsf3053392020-11-13 23:31:12 +01001348 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001349
Felix Held2bb3cdf2018-07-28 00:23:59 +02001350 all_high = 1;
1351 some_high = 0;
1352 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001353 if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001354 some_high = 1;
1355 else
1356 all_high = 0;
1357 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001358
1359 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001360 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001361 printram("4028--;\n");
1362 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001363 ctrl->timings[channel][slotrank].lanes[lane].rcven -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001364 upperA[lane] -= 0x40;
1365
1366 }
1367 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001368 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001369 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001370 printram("4024++;\n");
1371 printram("4028++;\n");
1372 }
1373
1374 program_timings(ctrl, channel);
1375
Angel Pons12bd8ab2020-11-13 23:10:52 +01001376 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001377
Angel Ponsf3053392020-11-13 23:31:12 +01001378 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379 if (err)
1380 return err;
1381
Angel Pons12bd8ab2020-11-13 23:10:52 +01001382 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001383
Angel Ponsf3053392020-11-13 23:31:12 +01001384 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001385
Angel Pons12bd8ab2020-11-13 23:10:52 +01001386 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001387
Angel Pons12bd8ab2020-11-13 23:10:52 +01001388 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001389
Angel Pons12bd8ab2020-11-13 23:10:52 +01001390 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001391
Angel Pons7e439c92020-12-07 11:56:01 +01001392 printram("4/8: %d, %d, % 4d, % 4d\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001393 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001394 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001395
1396 printram("final results:\n");
1397 FOR_ALL_LANES
Angel Pons7e439c92020-12-07 11:56:01 +01001398 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001399 ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001400
Angel Pons88521882020-01-05 20:21:20 +01001401 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001402
1403 toggle_io_reset();
1404 }
1405
1406 FOR_ALL_POPULATED_CHANNELS {
1407 program_timings(ctrl, channel);
1408 }
Angel Ponsc6742232020-11-15 13:26:21 +01001409
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001410 return 0;
1411}
1412
Angel Pons011661c2020-11-15 18:21:35 +01001413static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001414{
1415 int lane;
1416
1417 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001418 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1419 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001420 }
1421
Angel Pons88521882020-01-05 20:21:20 +01001422 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001423
Angel Ponsffd50152020-11-12 11:03:10 +01001424 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1425 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001426
Angel Ponsa853e7a2020-12-07 12:28:38 +01001427 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001428
Angel Pons801a5cb2020-11-15 15:48:29 +01001429 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001430
Angel Ponsa853e7a2020-12-07 12:28:38 +01001431 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001432}
1433
Angel Pons011661c2020-11-15 18:21:35 +01001434static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001435{
1436 int min = data[0];
1437 int max = min;
1438 int i;
1439 for (i = 1; i < count; i++) {
1440 if (min > data[i])
1441 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001442
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001443 if (max < data[i])
1444 max = data[i];
1445 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001446 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001447 for (i = 0; i < count; i++)
1448 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001449
Angel Pons891f2bc2020-01-10 01:27:28 +01001450 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001451}
1452
Angel Pons011661c2020-11-15 18:21:35 +01001453static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001454{
Angel Pons011661c2020-11-15 18:21:35 +01001455 int tx_dq;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001456 int stats[NUM_LANES][MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001457 int lane;
1458
Angel Pons88521882020-01-05 20:21:20 +01001459 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001460
Angel Ponsffd50152020-11-12 11:03:10 +01001461 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001462
Angel Pons38d901e2020-05-02 23:50:43 +02001463 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001464
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001465 for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) {
1466 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001467 program_timings(ctrl, channel);
1468
Angel Pons011661c2020-11-15 18:21:35 +01001469 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001470
1471 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001472 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001473 }
1474 }
1475 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001476 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1477
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001478 if (rn.all || rn.length < 8) {
Angel Pons30791632020-12-12 12:28:29 +01001479 printk(BIOS_EMERG, "tx_dq write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001480 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001481 /*
1482 * With command training not being done yet, the lane can be erroneous.
1483 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001484 */
Angel Pons011661c2020-11-15 18:21:35 +01001485 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001486 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1487
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001488 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001489 printk(BIOS_EMERG, "tx_dq recovery failed\n");
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001490 return MAKE_ERR;
1491 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001492 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001493 ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle;
Angel Pons7e439c92020-12-07 11:56:01 +01001494 printram("tx_dq: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001495 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001496 }
1497 return 0;
1498}
1499
Angel Pons88521882020-01-05 20:21:20 +01001500static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001501{
1502 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001503
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001504 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1505 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001506
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001507 return ret;
1508}
1509
Angel Pons765d4652020-11-11 14:44:35 +01001510/* Each cacheline is 64 bits long */
1511static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1512{
1513 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1514}
1515
Angel Pons88521882020-01-05 20:21:20 +01001516static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001517{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301518 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001519 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001520
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001521 for (j = 0; j < 16; j++)
1522 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001523
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001524 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001525
1526 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001527}
1528
Angel Pons88521882020-01-05 20:21:20 +01001529static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001530{
1531 int ret = 0;
1532 int channel;
1533 FOR_ALL_POPULATED_CHANNELS ret++;
1534 return ret;
1535}
1536
Angel Pons88521882020-01-05 20:21:20 +01001537static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001538{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301539 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001540 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301541 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001542
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001543 for (j = 0; j < 16; j++)
1544 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001545
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001546 for (j = 0; j < 16; j++)
1547 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001548
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001549 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001550
1551 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001552}
1553
Angel Pons820bce72020-11-14 17:02:55 +01001554static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001555{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001556 int tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557 int statistics[NUM_LANES][128];
1558 int lane;
1559
Angel Pons58b609b2020-11-13 14:35:29 +01001560 const union gdcr_training_mod_reg training_mod = {
1561 .write_leveling_mode = 1,
1562 .training_rank_sel = slotrank,
1563 .enable_dqs_wl = 5,
1564 .odt_always_on = 1,
1565 .force_drive_enable = 1,
1566 };
1567 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001568
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001569 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1570 int bank = 1;
1571
1572 if (ctrl->rank_mirror[channel][slotrank])
1573 ddr3_mirror_mrreg(&bank, &mr1reg);
1574
1575 wait_for_iosav(channel);
1576
1577 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1578
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001579 for (tx_dqs = 0; tx_dqs < 128; tx_dqs++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001580 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001581 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001582 }
1583 program_timings(ctrl, channel);
1584
Angel Ponsa853e7a2020-12-07 12:28:38 +01001585 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001586
1587 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001588 statistics[lane][tx_dqs] = !((MCHBAR32(lane_base[lane] +
1589 GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >>
1590 (tx_dqs % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001591 }
1592 }
1593 FOR_ALL_LANES {
1594 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001595 /*
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001596 * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value
1597 * of tx_dqs by a small value, which might cause the 6-bit value to overflow if
Angel Pons7c49cb82020-03-16 23:17:32 +01001598 * it's close to 0x3f. Increment the value by a small offset if it's likely
1599 * to overflow, to make sure it won't overflow while running tests and bricks
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001600 * the system due to a non matching tx_dq.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001601 *
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001602 * TODO: find out why some tests (edge write discovery) increment tx_dqs.
Angel Pons7c49cb82020-03-16 23:17:32 +01001603 */
1604 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001605 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001606 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001607 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001608
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001609 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001610 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01001611 printk(BIOS_EMERG, "JEDEC write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001612 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001613
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001614 return MAKE_ERR;
1615 }
Angel Pons7e439c92020-12-07 11:56:01 +01001616 printram("tx_dqs: %d, %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01001617 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001618 }
1619 return 0;
1620}
1621
Angel Pons820bce72020-11-14 17:02:55 +01001622static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001623{
1624 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001625 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001626 if (val == 0xffffffffffffffffLL)
1627 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001628 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001629 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001630 for (i = 0; i < 8; i++)
1631 if (val << (8 * (7 - i) + 4))
1632 return -i;
1633 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001634 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001635 for (i = 0; i < 8; i++)
1636 if (val >> (8 * (7 - i) + 4))
1637 return i;
1638 }
1639 return 8;
1640}
1641
Angel Ponsbf13ef02020-11-11 18:40:06 +01001642static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001643{
1644 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001645
1646 const union gdcr_training_mod_reg training_mod = {
1647 .dq_dqs_training_res = 1,
1648 };
1649 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1650
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001651 FOR_ALL_POPULATED_CHANNELS {
1652 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001653 }
1654 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1655
Angel Pons765d4652020-11-11 14:44:35 +01001656 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001657 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001658
Angel Pons88521882020-01-05 20:21:20 +01001659 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001660
Angel Ponsffd50152020-11-12 11:03:10 +01001661 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001662
Angel Ponsa853e7a2020-12-07 12:28:38 +01001663 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001664
Angel Pons8f0757e2020-11-11 23:03:36 +01001665 const struct iosav_ssq rd_sequence[] = {
1666 /* DRAM command PREA */
1667 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001668 .sp_cmd_ctrl = {
1669 .command = IOSAV_PRE,
1670 .ranksel_ap = 1,
1671 },
1672 .subseq_ctrl = {
1673 .cmd_executions = 1,
1674 .cmd_delay_gap = 3,
1675 .post_ssq_wait = ctrl->tRP,
1676 .data_direction = SSQ_NA,
1677 },
1678 .sp_cmd_addr = {
1679 .address = 1024,
1680 .rowbits = 6,
1681 .bank = 0,
1682 .rank = slotrank,
1683 },
1684 .addr_update = {
1685 .addr_wrap = 18,
1686 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001687 },
1688 /* DRAM command ACT */
1689 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001690 .sp_cmd_ctrl = {
1691 .command = IOSAV_ACT,
1692 .ranksel_ap = 1,
1693 },
1694 .subseq_ctrl = {
1695 .cmd_executions = 1,
1696 .cmd_delay_gap = 3,
1697 .post_ssq_wait = ctrl->tRCD,
1698 .data_direction = SSQ_NA,
1699 },
1700 .sp_cmd_addr = {
1701 .address = 0,
1702 .rowbits = 6,
1703 .bank = 0,
1704 .rank = slotrank,
1705 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001706 },
1707 /* DRAM command RD */
1708 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001709 .sp_cmd_ctrl = {
1710 .command = IOSAV_RD,
1711 .ranksel_ap = 3,
1712 },
1713 .subseq_ctrl = {
1714 .cmd_executions = 1,
1715 .cmd_delay_gap = 3,
1716 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001717 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001718 ctrl->timings[channel][slotrank].io_latency,
1719 .data_direction = SSQ_RD,
1720 },
1721 .sp_cmd_addr = {
1722 .address = 8,
1723 .rowbits = 6,
1724 .bank = 0,
1725 .rank = slotrank,
1726 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001727 },
1728 };
1729 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001730
Angel Ponsa853e7a2020-12-07 12:28:38 +01001731 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001732
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001733 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001734 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001735 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001736 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001737
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001738 old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1739 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs +=
Angel Pons820bce72020-11-14 17:02:55 +01001740 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001741
1742 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons7e439c92020-12-07 11:56:01 +01001743 printram("Bval+: %d, %d, %d, % 4d -> % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001744 old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001745 }
1746 }
Angel Pons88521882020-01-05 20:21:20 +01001747 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001748}
1749
Angel Pons7d115132020-11-14 01:44:44 +01001750static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001751{
Angel Pons7d115132020-11-14 01:44:44 +01001752 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001753
Angel Pons7d115132020-11-14 01:44:44 +01001754 FOR_ALL_POPULATED_CHANNELS {
1755 /* choose an existing rank */
1756 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001757
Angel Pons7d115132020-11-14 01:44:44 +01001758 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001759
Angel Ponsa853e7a2020-12-07 12:28:38 +01001760 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001761
Angel Pons7d115132020-11-14 01:44:44 +01001762 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1763 }
1764
1765 /* Refresh disable */
1766 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1767
1768 FOR_ALL_POPULATED_CHANNELS {
1769 /* Execute the same command queue */
Angel Ponsa853e7a2020-12-07 12:28:38 +01001770 iosav_run_once_and_wait(channel);
Angel Pons7d115132020-11-14 01:44:44 +01001771 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001772}
1773
Angel Pons7c49cb82020-03-16 23:17:32 +01001774/*
1775 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001776 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001777 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1778 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1779 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1780 * CLK/ADDR/CMD signals have the same routing delay.
1781 *
1782 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1783 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1784 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001785 */
Angel Pons820bce72020-11-14 17:02:55 +01001786static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001787{
Angel Pons820bce72020-11-14 17:02:55 +01001788 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001789
Angel Pons7d115132020-11-14 01:44:44 +01001790 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001791
Angel Pons7c49cb82020-03-16 23:17:32 +01001792 /* Enable write leveling on all ranks
1793 Disable all DQ outputs
1794 Only NOP is allowed in this mode */
1795 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1796 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001797 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001798
Angel Ponsa1f17142020-11-15 12:50:03 +01001799 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001800 const union gdcr_training_mod_reg training_mod = {
1801 .write_leveling_mode = 1,
1802 .enable_dqs_wl = 5,
1803 .odt_always_on = 1,
1804 .force_drive_enable = 1,
1805 };
1806 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001807
1808 toggle_io_reset();
1809
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001810 /* Set any valid value for tx_dqs, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001811 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001812 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813 if (err)
1814 return err;
1815 }
1816
Angel Pons7c49cb82020-03-16 23:17:32 +01001817 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001818 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001819 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001820
Angel Pons88521882020-01-05 20:21:20 +01001821 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001822
1823 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001824 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001825
Angel Pons7c49cb82020-03-16 23:17:32 +01001826 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001827 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001828
1829 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001830 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001831 MCHBAR32(IOSAV_STATUS_ch(channel));
1832 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001833
Angel Ponsffd50152020-11-12 11:03:10 +01001834 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001835
Angel Ponsa853e7a2020-12-07 12:28:38 +01001836 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001837 }
1838
1839 toggle_io_reset();
1840
Angel Pons820bce72020-11-14 17:02:55 +01001841 return 0;
1842}
1843
1844int write_training(ramctr_timing *ctrl)
1845{
Angel Ponsc6742232020-11-15 13:26:21 +01001846 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001847 int err;
1848
Angel Pons4d192822020-12-12 13:54:37 +01001849 /*
1850 * Set the DEC_WRD bit, required for the write flyby algorithm.
1851 * Needs to be done before starting the write training procedure.
1852 */
Angel Pons820bce72020-11-14 17:02:55 +01001853 FOR_ALL_POPULATED_CHANNELS
1854 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1855
Angel Pons4c76d252020-11-15 13:06:53 +01001856 printram("CPE\n");
1857
Angel Pons820bce72020-11-14 17:02:55 +01001858 err = jedec_write_leveling(ctrl);
1859 if (err)
1860 return err;
1861
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001862 printram("CPF\n");
1863
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001864 FOR_ALL_POPULATED_CHANNELS {
1865 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001866 }
1867
1868 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001869 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001870 if (err)
1871 return err;
1872 }
1873
1874 FOR_ALL_POPULATED_CHANNELS
1875 program_timings(ctrl, channel);
1876
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001877 /* measure and adjust tx_dqs timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001878 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001879
1880 FOR_ALL_POPULATED_CHANNELS
1881 program_timings(ctrl, channel);
1882
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001883 return 0;
1884}
1885
Angel Ponsbf13ef02020-11-11 18:40:06 +01001886static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001887{
1888 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001889 int tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001890 int lanes_ok = 0;
1891 int ctr = 0;
1892 int lane;
1893
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001894 for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001895 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001896 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
1897 saved_rt.lanes[lane].tx_dq + tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001898 }
1899 program_timings(ctrl, channel);
1900 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001901 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001902 }
1903
Angel Pons765d4652020-11-11 14:44:35 +01001904 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001905 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001906
Angel Pons88521882020-01-05 20:21:20 +01001907 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001908
Angel Ponsffd50152020-11-12 11:03:10 +01001909 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001910
1911 /* Program LFSR for the RD/WR subsequences */
1912 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1913 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001914
Angel Ponsa853e7a2020-12-07 12:28:38 +01001915 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001916
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001917 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001918 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001919
1920 if (r32 == 0)
1921 lanes_ok |= 1 << lane;
1922 }
1923 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001924 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001925 break;
1926 }
1927
1928 ctrl->timings[channel][slotrank] = saved_rt;
1929
Patrick Rudolphdd662872017-10-28 18:20:11 +02001930 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001931}
1932
Angel Pons88521882020-01-05 20:21:20 +01001933static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001934{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301935 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01001936 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
1937 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001938
1939 if (patno) {
1940 u8 base8 = 0x80 >> ((patno - 1) % 8);
1941 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1942 for (i = 0; i < 32; i++) {
1943 for (j = 0; j < 16; j++) {
1944 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001945
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001946 if (invert[patno - 1][i] & (1 << (j / 2)))
1947 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001948
1949 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001950 }
1951 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001952 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001953 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1954 for (j = 0; j < 16; j++) {
1955 const u32 val = pattern[i][j];
1956 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1957 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001958 }
1959 sfence();
1960 }
Angel Pons765d4652020-11-11 14:44:35 +01001961
1962 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001963}
1964
Angel Pons88521882020-01-05 20:21:20 +01001965static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001966{
Angel Pons7d115132020-11-14 01:44:44 +01001967 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001968
Angel Pons7c49cb82020-03-16 23:17:32 +01001969 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001971
1972 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001973 dram_mrscommands(ctrl);
1974
1975 toggle_io_reset();
1976}
1977
Angel Ponsbf13ef02020-11-11 18:40:06 +01001978#define CT_MIN_PI -127
1979#define CT_MAX_PI 128
1980#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1981
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001982#define MIN_C320C_LEN 13
1983
1984static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
1985{
1986 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
1987 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001988 int command_pi;
1989 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001990 int delta = 0;
1991
1992 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
1993
1994 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01001995 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001996 }
1997
1998 ctrl->cmd_stretch[channel] = cmd_stretch;
1999
Angel Pons7a612742020-11-12 13:34:03 +01002000 const union tc_rap_reg tc_rap = {
2001 .tRRD = ctrl->tRRD,
2002 .tRTP = ctrl->tRTP,
2003 .tCKE = ctrl->tCKE,
2004 .tWTR = ctrl->tWTR,
2005 .tFAW = ctrl->tFAW,
2006 .tWR = ctrl->tWR,
2007 .tCMD = ctrl->cmd_stretch[channel],
2008 };
2009 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002010
2011 if (ctrl->cmd_stretch[channel] == 2)
2012 delta = 2;
2013 else if (ctrl->cmd_stretch[channel] == 0)
2014 delta = 4;
2015
2016 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002017 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002018 }
2019
Angel Ponsbf13ef02020-11-11 18:40:06 +01002020 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002021 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002022 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002023 }
2024 program_timings(ctrl, channel);
2025 reprogram_320c(ctrl);
2026 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002027 stat[slotrank][command_pi - CT_MIN_PI] =
2028 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002029 }
2030 }
2031 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002032 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002033
Angel Ponsbf13ef02020-11-11 18:40:06 +01002034 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Angel Pons7e439c92020-12-07 11:56:01 +01002035 printram("cmd_stretch: %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01002036 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002037
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002038 if (rn.all || rn.length < MIN_C320C_LEN) {
2039 FOR_ALL_POPULATED_RANKS {
2040 ctrl->timings[channel][slotrank] =
2041 saved_timings[channel][slotrank];
2042 }
2043 return MAKE_ERR;
2044 }
2045 }
2046
2047 return 0;
2048}
2049
Angel Pons7c49cb82020-03-16 23:17:32 +01002050/*
2051 * Adjust CMD phase shift and try multiple command rates.
2052 * A command rate of 2T doubles the time needed for address and command decode.
2053 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002054int command_training(ramctr_timing *ctrl)
2055{
2056 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002057
2058 FOR_ALL_POPULATED_CHANNELS {
2059 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002060 }
2061
2062 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002063 int cmdrate, err;
2064
2065 /*
2066 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002067 * Issue:
Angel Pons30791632020-12-12 12:28:29 +01002068 * While command training seems to succeed, raminit will fail in write training.
Angel Pons7c49cb82020-03-16 23:17:32 +01002069 *
2070 * Workaround:
2071 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2072 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002073 *
2074 * Single DIMM per channel:
2075 * Try command rate 1T and 2T
2076 */
2077 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002078 if (ctrl->tCMD)
2079 /* XMP gives the CMD rate in clock ticks, not ns */
2080 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002081
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002082 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002083 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2084
2085 if (!err)
2086 break;
2087 }
2088
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002089 if (err) {
Angel Pons30791632020-12-12 12:28:29 +01002090 printk(BIOS_EMERG, "Command training failed: %d\n", channel);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002091 return err;
2092 }
2093
Angel Pons891f2bc2020-01-10 01:27:28 +01002094 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002095 }
2096
2097 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002098 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002099
2100 reprogram_320c(ctrl);
2101 return 0;
2102}
2103
Angel Pons4c79f932020-11-14 01:26:52 +01002104static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002105{
Angel Pons96a06dd2020-11-14 00:33:18 +01002106 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002107 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002108 int lane;
2109
Angel Pons96a06dd2020-11-14 00:33:18 +01002110 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002112 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi;
2113 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002114 }
2115 program_timings(ctrl, channel);
2116
2117 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002118 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2119 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002120 }
2121
Angel Pons88521882020-01-05 20:21:20 +01002122 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002123
Angel Ponsffd50152020-11-12 11:03:10 +01002124 iosav_write_read_mpr_sequence(
2125 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002126
Angel Ponsa853e7a2020-12-07 12:28:38 +01002127 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002128
2129 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002130 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002131 }
2132 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002133
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002134 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002135 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002136 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002137
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002138 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002139 printk(BIOS_EMERG, "Read MPR training failed: %d, %d, %d\n", channel,
Angel Pons7c49cb82020-03-16 23:17:32 +01002140 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002141 return MAKE_ERR;
2142 }
Angel Pons7e439c92020-12-07 11:56:01 +01002143 printram("eval %d, %d, %d: % 4d\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002144 }
2145 return 0;
2146}
2147
Angel Pons60971dc2020-11-14 00:49:38 +01002148static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2149{
2150 int slotrank, lane;
2151
2152 fill_pattern0(ctrl, channel, 0, 0);
2153 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002154 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002155 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2156 }
2157
2158 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002159 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16;
2160 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16;
Angel Pons60971dc2020-11-14 00:49:38 +01002161 }
2162
2163 program_timings(ctrl, channel);
2164
2165 FOR_ALL_POPULATED_RANKS {
2166 wait_for_iosav(channel);
2167
2168 iosav_write_read_mpr_sequence(
2169 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2170
Angel Ponsa853e7a2020-12-07 12:28:38 +01002171 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002172 }
2173
2174 /* XXX: check any measured value ? */
2175
2176 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002177 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48;
2178 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48;
Angel Pons60971dc2020-11-14 00:49:38 +01002179 }
2180
2181 program_timings(ctrl, channel);
2182
2183 FOR_ALL_POPULATED_RANKS {
2184 wait_for_iosav(channel);
2185
2186 iosav_write_read_mpr_sequence(
2187 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2188
Angel Ponsa853e7a2020-12-07 12:28:38 +01002189 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002190 }
2191
2192 /* XXX: check any measured value ? */
2193
2194 FOR_ALL_LANES {
2195 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2196 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2197 }
2198}
2199
Angel Pons4c79f932020-11-14 01:26:52 +01002200int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002201{
2202 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2203 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2204 int channel, slotrank, lane;
2205 int err;
2206
Angel Pons88521882020-01-05 20:21:20 +01002207 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002208
2209 toggle_io_reset();
2210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002211 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002212 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002213
2214 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002215 }
2216
Angel Pons0c3936e2020-03-22 12:49:27 +01002217 /*
2218 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2219 * also use a single loop. It would seem that it is a debugging configuration.
2220 */
Angel Pons88521882020-01-05 20:21:20 +01002221 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2222 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002223
2224 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002225 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002226 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002227 if (err)
2228 return err;
2229 }
2230
Angel Pons88521882020-01-05 20:21:20 +01002231 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2232 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002233
2234 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002235 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002236 rising_edges[channel][slotrank]);
2237 if (err)
2238 return err;
2239 }
2240
Angel Pons88521882020-01-05 20:21:20 +01002241 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002242
2243 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002244 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002245 falling_edges[channel][slotrank][lane];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002246 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002247 rising_edges[channel][slotrank][lane];
2248 }
2249
2250 FOR_ALL_POPULATED_CHANNELS {
2251 program_timings(ctrl, channel);
2252 }
2253
Angel Pons50a6fe72020-11-14 01:18:14 +01002254 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002255 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002256 }
2257 return 0;
2258}
2259
Angel Pons08f749d2020-11-17 16:50:56 +01002260static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002261{
Angel Pons08f749d2020-11-17 16:50:56 +01002262 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2263
Angel Pons7c49cb82020-03-16 23:17:32 +01002264 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002265 int lower[NUM_LANES];
2266 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002267 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002268
2269 FOR_ALL_LANES {
2270 lower[lane] = 0;
2271 upper[lane] = MAX_EDGE_TIMING;
2272 }
2273
Angel Pons08f749d2020-11-17 16:50:56 +01002274 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002275 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002276 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002277 };
2278 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2279 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002280
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002281 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2282 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002283 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002284
Angel Pons08f749d2020-11-17 16:50:56 +01002285 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002286 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002287 ctrl->timings[channel][slotrank].lanes[lane]
2288 .rx_dqs_p = read_pi;
2289 ctrl->timings[channel][slotrank].lanes[lane]
2290 .rx_dqs_n = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002291 }
2292 program_timings(ctrl, channel);
2293
2294 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002295 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2296 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002297 }
Angel Pons88521882020-01-05 20:21:20 +01002298 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002299
Angel Ponsffd50152020-11-12 11:03:10 +01002300 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002301
Angel Ponsa853e7a2020-12-07 12:28:38 +01002302 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002303
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002304 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002305 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002306 }
2307
Angel Pons7c49cb82020-03-16 23:17:32 +01002308 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002309 raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002310 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002311
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002312 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002313 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002314 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002315
2316 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2317 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002318
2319 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2320
Angel Pons7e439c92020-12-07 11:56:01 +01002321 printram("edges: %d, %d, %d: % 4d-% 4d-% 4d, "
2322 "% 4d-% 4d\n", channel, slotrank, i, rn.start,
Angel Pons7c49cb82020-03-16 23:17:32 +01002323 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002325
2326 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2327 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2328
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002329 edges[lane] = (lower[lane] + upper[lane]) / 2;
2330 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons30791632020-12-12 12:28:29 +01002331 printk(BIOS_EMERG, "Aggressive read training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002332 "%d, %d, %d\n", channel, slotrank, lane);
2333
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002334 return MAKE_ERR;
2335 }
2336 }
2337 }
2338 }
2339
Angel Ponsa93f46e2020-11-17 16:54:01 +01002340 /* Restore nominal Vref after training */
2341 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002342 printram("CPA\n");
2343 return 0;
2344}
2345
Angel Pons08f749d2020-11-17 16:50:56 +01002346int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002347{
2348 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002349 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2350 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002351
Angel Pons7c49cb82020-03-16 23:17:32 +01002352 /*
2353 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2354 * also use a single loop. It would seem that it is a debugging configuration.
2355 */
Angel Pons88521882020-01-05 20:21:20 +01002356 MCHBAR32(IOSAV_DC_MASK) = 0x300;
Angel Pons08f749d2020-11-17 16:50:56 +01002357 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002358
2359 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002360 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002361 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002362 if (err)
2363 return err;
2364 }
2365
Angel Pons88521882020-01-05 20:21:20 +01002366 MCHBAR32(IOSAV_DC_MASK) = 0x200;
Angel Pons08f749d2020-11-17 16:50:56 +01002367 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002368
2369 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002370 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002371 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002372 if (err)
2373 return err;
2374 }
2375
Angel Pons88521882020-01-05 20:21:20 +01002376 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002377
2378 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002379 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Angel Pons7c49cb82020-03-16 23:17:32 +01002380 falling_edges[channel][slotrank][lane];
2381
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002382 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Angel Pons7c49cb82020-03-16 23:17:32 +01002383 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002384 }
2385
2386 FOR_ALL_POPULATED_CHANNELS
2387 program_timings(ctrl, channel);
2388
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002389 return 0;
2390}
2391
Angel Pons2a7d7522020-11-19 12:49:07 +01002392static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002393{
Angel Pons88521882020-01-05 20:21:20 +01002394 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002395
Angel Ponsffd50152020-11-12 11:03:10 +01002396 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002397
Angel Ponsa853e7a2020-12-07 12:28:38 +01002398 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002399}
2400
Angel Pons2a7d7522020-11-19 12:49:07 +01002401static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002402{
Angel Pons2a7d7522020-11-19 12:49:07 +01002403 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24);
2404 udelay(2);
2405}
2406
2407int aggressive_write_training(ramctr_timing *ctrl)
2408{
2409 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002410 int i, pat;
2411
2412 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2413 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2414 int channel, slotrank, lane;
2415
Angel Pons9fbb1b02020-11-19 12:53:36 +01002416 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2417 if (!IS_IVY_CPU(ctrl->cpu))
2418 return 0;
2419
2420 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2421 return 0;
2422
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002423 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2424 lower[channel][slotrank][lane] = 0;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002425 upper[channel][slotrank][lane] = MAX_TX_DQ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002426 }
2427
Angel Pons2a7d7522020-11-19 12:49:07 +01002428 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2429 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2430
2431 if (enable_iosav_opt)
2432 MCHBAR32(MCMNTS_SPARE) = 1;
2433
Angel Pons30791632020-12-12 12:28:29 +01002434 printram("Aggresive write training:\n");
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002435
Angel Pons2a7d7522020-11-19 12:49:07 +01002436 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002437 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002438 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002439
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002440 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2441 FOR_ALL_POPULATED_RANKS {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002442 int tx_dq;
2443 u32 raw_stats[MAX_TX_DQ + 1];
2444 int stats[MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002445
2446 /* Make sure rn.start < rn.end */
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002447 stats[MAX_TX_DQ] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002448
2449 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002450
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002451 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002452 FOR_ALL_LANES {
2453 ctrl->timings[channel][slotrank]
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002454 .lanes[lane].tx_dq = tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01002455 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002456 program_timings(ctrl, channel);
2457
Angel Pons2a7d7522020-11-19 12:49:07 +01002458 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002459
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002460 raw_stats[tx_dq] = MCHBAR32(
Angel Pons098240eb2020-03-22 12:55:32 +01002461 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002462 }
2463 FOR_ALL_LANES {
2464 struct run rn;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002465 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
2466 stats[tx_dq] = !!(raw_stats[tx_dq]
Angel Pons7c49cb82020-03-16 23:17:32 +01002467 & (1 << lane));
2468 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002469
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002470 rn = get_longest_zero_run(stats, MAX_TX_DQ + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002471 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002472 printk(BIOS_EMERG, "Aggressive "
2473 "write training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002474 "%d, %d, %d\n", channel,
2475 slotrank, lane);
2476
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002477 return MAKE_ERR;
2478 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002479 printram("tx_dq: %d, %d, %d: "
Angel Pons7e439c92020-12-07 11:56:01 +01002480 "% 4d-% 4d-% 4d, "
2481 "% 4d-% 4d\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002482 i, rn.start, rn.middle, rn.end,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002483 rn.start + ctrl->tx_dq_offset[i],
2484 rn.end - ctrl->tx_dq_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002485
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002486 lower[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002487 MAX(rn.start + ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002488 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002489
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002490 upper[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002491 MIN(rn.end - ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002492 upper[channel][slotrank][lane]);
2493
2494 }
2495 }
2496 }
2497 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002498 }
2499
Angel Pons2a7d7522020-11-19 12:49:07 +01002500 FOR_ALL_CHANNELS {
2501 /* Restore nominal write Vref after training */
2502 set_write_vref(channel, 0);
2503 }
2504
2505 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2506 if (enable_iosav_opt)
2507 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002508
2509 printram("CPB\n");
2510
2511 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7e439c92020-12-07 11:56:01 +01002512 printram("tx_dq %d, %d, %d: % 4d\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002513 (lower[channel][slotrank][lane] +
2514 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002515
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002516 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002517 (lower[channel][slotrank][lane] +
2518 upper[channel][slotrank][lane]) / 2;
2519 }
2520 FOR_ALL_POPULATED_CHANNELS {
2521 program_timings(ctrl, channel);
2522 }
2523 return 0;
2524}
2525
Angel Pons88521882020-01-05 20:21:20 +01002526void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002527{
2528 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002529 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002530
2531 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2532 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002533 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002534 FOR_ALL_LANES mat =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002535 MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002536 printram("normalize %d, %d, %d: mat %d\n",
2537 channel, slotrank, lane, mat);
2538
Felix Heldef4fe3e2019-12-31 14:15:05 +01002539 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002540 printram("normalize %d, %d, %d: delta %d\n",
2541 channel, slotrank, lane, delta);
2542
Angel Pons88521882020-01-05 20:21:20 +01002543 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002544 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002545 }
2546
2547 FOR_ALL_POPULATED_CHANNELS {
2548 program_timings(ctrl, channel);
2549 }
2550}
2551
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002552int channel_test(ramctr_timing *ctrl)
2553{
2554 int channel, slotrank, lane;
2555
2556 slotrank = 0;
2557 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002558 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002559 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002560 return MAKE_ERR;
2561 }
2562 FOR_ALL_POPULATED_CHANNELS {
2563 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002564 }
2565
2566 for (slotrank = 0; slotrank < 4; slotrank++)
2567 FOR_ALL_CHANNELS
2568 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2569 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002570 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2571 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002572 }
Angel Pons88521882020-01-05 20:21:20 +01002573 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002574
Angel Ponsffd50152020-11-12 11:03:10 +01002575 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002576
Angel Ponsa853e7a2020-12-07 12:28:38 +01002577 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002578
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002579 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002580 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002581 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2582 channel, slotrank, lane);
2583 return MAKE_ERR;
2584 }
2585 }
2586 return 0;
2587}
2588
Patrick Rudolphdd662872017-10-28 18:20:11 +02002589void channel_scrub(ramctr_timing *ctrl)
2590{
2591 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002592 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002593
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002594 FOR_ALL_POPULATED_CHANNELS {
2595 wait_for_iosav(channel);
2596 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002597 }
2598
2599 /*
2600 * During runtime the "scrubber" will periodically scan through the memory in the
2601 * physical address space, to identify and fix CRC errors.
2602 * The following loops writes to every DRAM address, setting the ECC bits to the
2603 * correct value. A read from this location will no longer return a CRC error,
2604 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002605 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002606 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2607 * and firmware running in x86_32.
2608 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002609 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2610 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002611 for (bank = 0; bank < 8; bank++) {
2612 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002613
Angel Pons8f0757e2020-11-11 23:03:36 +01002614 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2615 const struct iosav_ssq sequence[] = {
2616 /*
2617 * DRAM command ACT
2618 * Opens the row for writing.
2619 */
2620 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002621 .sp_cmd_ctrl = {
2622 .command = IOSAV_ACT,
2623 .ranksel_ap = 1,
2624 },
2625 .subseq_ctrl = {
2626 .cmd_executions = 1,
2627 .cmd_delay_gap = gap,
2628 .post_ssq_wait = ctrl->tRCD,
2629 .data_direction = SSQ_NA,
2630 },
2631 .sp_cmd_addr = {
2632 .address = row,
2633 .rowbits = 6,
2634 .bank = bank,
2635 .rank = slotrank,
2636 },
2637 .addr_update = {
2638 .inc_addr_1 = 1,
2639 .addr_wrap = 18,
2640 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002641 },
2642 /*
2643 * DRAM command WR
2644 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2645 * bytes.
2646 */
2647 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002648 .sp_cmd_ctrl = {
2649 .command = IOSAV_WR,
2650 .ranksel_ap = 1,
2651 },
2652 .subseq_ctrl = {
2653 .cmd_executions = 129,
2654 .cmd_delay_gap = 4,
2655 .post_ssq_wait = ctrl->tWTR +
2656 ctrl->CWL + 8,
2657 .data_direction = SSQ_WR,
2658 },
2659 .sp_cmd_addr = {
2660 .address = row,
2661 .rowbits = 0,
2662 .bank = bank,
2663 .rank = slotrank,
2664 },
2665 .addr_update = {
2666 .inc_addr_8 = 1,
2667 .addr_wrap = 9,
2668 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002669 },
2670 /*
2671 * DRAM command PRE
2672 * Closes the row.
2673 */
2674 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002675 .sp_cmd_ctrl = {
2676 .command = IOSAV_PRE,
2677 .ranksel_ap = 1,
2678 },
2679 .subseq_ctrl = {
2680 .cmd_executions = 1,
2681 .cmd_delay_gap = 4,
2682 .post_ssq_wait = ctrl->tRP,
2683 .data_direction = SSQ_NA,
2684 },
2685 .sp_cmd_addr = {
2686 .address = 0,
2687 .rowbits = 6,
2688 .bank = bank,
2689 .rank = slotrank,
2690 },
2691 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002692 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002693 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002694 },
2695 };
2696 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002697
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002698 iosav_run_queue(channel, 16, 0);
2699
2700 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002701 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002702 }
2703 }
2704}
2705
Angel Pons88521882020-01-05 20:21:20 +01002706void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002707{
2708 int channel;
2709
Angel Pons7c49cb82020-03-16 23:17:32 +01002710 /* 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 +01002711 static u32 seeds[NUM_CHANNELS][3] = {
2712 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2713 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2714 };
2715 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002716 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002717 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2718 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2719 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002720 }
2721}
2722
Angel Pons89ae6b82020-03-21 13:23:32 +01002723void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002724{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002725 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002726 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002727 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002728 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002729 }
2730}
2731
Angel Pons88521882020-01-05 20:21:20 +01002732void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002733{
2734 int channel;
2735
2736 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002737 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002738 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002739 }
2740
2741 udelay(1);
2742
2743 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002744 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002745 }
2746}
2747
Angel Pons7c49cb82020-03-16 23:17:32 +01002748void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002749{
Angel Pons11463322020-11-19 11:04:28 +01002750 /* Use a larger delay when running fast to improve stability */
2751 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2752
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002753 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002754
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002755 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002756 int min_pi = 10000;
2757 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002758
2759 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002760 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2761 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002762 }
2763
Angel Pons7a612742020-11-12 13:34:03 +01002764 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002765
Angel Pons7a612742020-11-12 13:34:03 +01002766 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002767
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002768 dram_odt_stretch(ctrl, channel);
2769
Angel Pons7a612742020-11-12 13:34:03 +01002770 const union tc_rwp_reg tc_rwp = {
2771 .tRRDR = 0,
2772 .tRRDD = val,
2773 .tWWDR = val,
2774 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002775 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002776 .tWRDRDD = tWRDRDD,
2777 .tRWSR = 2,
2778 .dec_wrd = 1,
2779 };
2780 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002781 }
2782}
2783
Angel Pons88521882020-01-05 20:21:20 +01002784void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002785{
2786 int channel;
2787 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002788 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2789 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002790 }
2791}
2792
Angel Pons7c49cb82020-03-16 23:17:32 +01002793/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2794static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002795{
Angel Pons88521882020-01-05 20:21:20 +01002796 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002797}
2798
Angel Pons7c49cb82020-03-16 23:17:32 +01002799/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002800void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002801{
2802 int channel;
2803 int t1_cycles = 0, t1_ns = 0, t2_ns;
2804 int t3_ns;
2805 u32 r32;
2806
Angel Pons7c49cb82020-03-16 23:17:32 +01002807 /* FIXME: This register only exists on Ivy Bridge */
2808 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002809
Angel Pons7a612742020-11-12 13:34:03 +01002810 FOR_ALL_CHANNELS {
2811 union tc_othp_reg tc_othp = {
2812 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2813 };
2814 tc_othp.tCPDED = 1;
2815 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2816 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002817
Angel Pons09fc4b92020-11-19 12:02:07 +01002818 /* 64 DCLKs until idle, decision per rank */
2819 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002820
Felix Heldf9b826a2018-07-30 17:56:52 +02002821 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002822 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002823
Angel Pons88521882020-01-05 20:21:20 +01002824 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
2825 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002826
2827 FOR_ALL_CHANNELS {
2828 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002829 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002830 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002831 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002832 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002833 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002834 case 1:
2835 case 4:
2836 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002837 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002838 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002839 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002840 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002841 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002842 break;
2843 }
2844 }
2845
Felix Held50b7ed22019-12-30 20:41:54 +01002846 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002847 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002848 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002849
Angel Pons7a612742020-11-12 13:34:03 +01002850 FOR_ALL_CHANNELS {
2851 union tc_rfp_reg tc_rfp = {
2852 .raw = MCHBAR32(TC_RFP_ch(channel)),
2853 };
2854 tc_rfp.refresh_2x_control = 1;
2855 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2856 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002857
Angel Ponsdc5539f2020-11-12 12:44:25 +01002858 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2859 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002860 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002861
Angel Pons7c49cb82020-03-16 23:17:32 +01002862 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002863 FOR_ALL_POPULATED_CHANNELS
2864 break;
2865
Angel Pons88521882020-01-05 20:21:20 +01002866 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2867 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002868 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002869 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002870 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002871 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002872 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002873 t1_ns += 500;
2874
Angel Pons88521882020-01-05 20:21:20 +01002875 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002876 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002877 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002878 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002879 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002880 t3_ns = 500;
2881 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002882
2883 /* The graphics driver will use these watermark values */
2884 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002885 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002886 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2887 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002888}
2889
Angel Pons88521882020-01-05 20:21:20 +01002890void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002891{
Angel Ponsc6742232020-11-15 13:26:21 +01002892 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002893
Angel Pons7c49cb82020-03-16 23:17:32 +01002894 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002895 const union tc_rap_reg tc_rap = {
2896 .tRRD = ctrl->tRRD,
2897 .tRTP = ctrl->tRTP,
2898 .tCKE = ctrl->tCKE,
2899 .tWTR = ctrl->tWTR,
2900 .tFAW = ctrl->tFAW,
2901 .tWR = ctrl->tWR,
2902 .tCMD = ctrl->cmd_stretch[channel],
2903 };
2904 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002905 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002906
2907 udelay(1);
2908
2909 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002910 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002911 }
2912
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002913 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002914 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002915
2916 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002917 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002918 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002919 }
2920
2921 printram("CPE\n");
2922
Angel Pons88521882020-01-05 20:21:20 +01002923 MCHBAR32(GDCRTRAININGMOD) = 0;
2924 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002925
2926 printram("CP5b\n");
2927
2928 FOR_ALL_POPULATED_CHANNELS {
2929 program_timings(ctrl, channel);
2930 }
2931
2932 u32 reg, addr;
2933
Angel Pons7c49cb82020-03-16 23:17:32 +01002934 /* Poll for RCOMP */
2935 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2936 ;
2937
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002938 do {
Angel Pons88521882020-01-05 20:21:20 +01002939 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002940 } while ((reg & 0x14) == 0);
2941
Angel Pons7c49cb82020-03-16 23:17:32 +01002942 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002943 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002944 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002945
Angel Pons7c49cb82020-03-16 23:17:32 +01002946 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002947 udelay(500);
2948
2949 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002950 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002951 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002952 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002953 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002954 MCHBAR32(addr) = reg;
2955
Angel Pons7c49cb82020-03-16 23:17:32 +01002956 /* Wait 10ns for ranks to settle */
2957 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002958
2959 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
2960 MCHBAR32(addr) = reg;
2961
Angel Pons7c49cb82020-03-16 23:17:32 +01002962 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002963 write_reset(ctrl);
2964 }
2965
Angel Pons7c49cb82020-03-16 23:17:32 +01002966 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002967 dram_mrscommands(ctrl);
2968
2969 printram("CP5c\n");
2970
Angel Pons88521882020-01-05 20:21:20 +01002971 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972
2973 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002974 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002975 udelay(2);
2976 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002977}