blob: d24e5a8883bc6b6472ca69463731b44a4c34f2d7 [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 Pons7c49cb82020-03-16 23:17:32 +0100549 /*
550 * Execute command queue - why is bit 22 set here?!
551 *
552 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
553 */
Angel Pons38d901e2020-05-02 23:50:43 +0200554 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200555
Angel Pons88521882020-01-05 20:21:20 +0100556 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100557}
558
Angel Pons88521882020-01-05 20:21:20 +0100559void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560{
Felix Held9fe248f2018-07-31 20:59:45 +0200561 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100562 int channel;
563
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
565 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566 do {
Angel Pons88521882020-01-05 20:21:20 +0100567 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100568 } while ((reg & 0x14) == 0);
569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100572 MCHBAR32(MC_INIT_STATE_G) = reg;
573 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100574 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100575 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100576
Angel Pons7c49cb82020-03-16 23:17:32 +0100577 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100578 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100579
Angel Pons7c49cb82020-03-16 23:17:32 +0100580 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581 udelay(200);
582
Angel Pons7c49cb82020-03-16 23:17:32 +0100583 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100584 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100585
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100587 udelay(500);
588
Angel Pons7c49cb82020-03-16 23:17:32 +0100589 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100590 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100591
Angel Pons7c49cb82020-03-16 23:17:32 +0100592 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100593 udelay(1);
594
595 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200597 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100598 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100599
Angel Pons7c49cb82020-03-16 23:17:32 +0100600 /* Wait 10ns for ranks to settle */
601 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100602
603 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100604 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100605
Angel Pons7c49cb82020-03-16 23:17:32 +0100606 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100607 write_reset(ctrl);
608 }
609}
610
Angel Pons3d3bf482020-11-14 16:18:15 +0100611/*
612 * DDR3 Rank1 Address mirror swap the following pins:
613 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
614 */
615static void ddr3_mirror_mrreg(int *bank, u32 *addr)
616{
617 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
618 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
619}
620
Angel Pons7c49cb82020-03-16 23:17:32 +0100621static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100622{
Angel Pons88521882020-01-05 20:21:20 +0100623 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100624
Angel Pons3d3bf482020-11-14 16:18:15 +0100625 if (ctrl->rank_mirror[channel][slotrank])
626 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100627
Angel Pons8f0757e2020-11-11 23:03:36 +0100628 const struct iosav_ssq sequence[] = {
629 /* DRAM command MRS */
630 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200631 .sp_cmd_ctrl = {
632 .command = IOSAV_MRS,
633 },
634 .subseq_ctrl = {
635 .cmd_executions = 1,
636 .cmd_delay_gap = 4,
637 .post_ssq_wait = 4,
638 .data_direction = SSQ_NA,
639 },
640 .sp_cmd_addr = {
641 .address = val,
642 .rowbits = 6,
643 .bank = reg,
644 .rank = slotrank,
645 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100646 },
647 /* DRAM command MRS */
648 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200649 .sp_cmd_ctrl = {
650 .command = IOSAV_MRS,
651 .ranksel_ap = 1,
652 },
653 .subseq_ctrl = {
654 .cmd_executions = 1,
655 .cmd_delay_gap = 4,
656 .post_ssq_wait = 4,
657 .data_direction = SSQ_NA,
658 },
659 .sp_cmd_addr = {
660 .address = val,
661 .rowbits = 6,
662 .bank = reg,
663 .rank = slotrank,
664 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100665 },
666 /* DRAM command MRS */
667 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200668 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100669 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200670 },
671 .subseq_ctrl = {
672 .cmd_executions = 1,
673 .cmd_delay_gap = 4,
674 .post_ssq_wait = ctrl->tMOD,
675 .data_direction = SSQ_NA,
676 },
677 .sp_cmd_addr = {
678 .address = val,
679 .rowbits = 6,
680 .bank = reg,
681 .rank = slotrank,
682 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100683 },
684 };
685 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200686
Angel Pons7c49cb82020-03-16 23:17:32 +0100687 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200688 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100689}
690
Angel Pons09fc4b92020-11-19 12:02:07 +0100691/* Obtain optimal power down mode for current configuration */
692static enum pdwm_mode get_power_down_mode(ramctr_timing *ctrl)
693{
694 if (ctrl->tXP > 8)
695 return PDM_NONE;
696
697 if (ctrl->tXPDLL > 32)
698 return PDM_PPD;
699
700 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
701 return PDM_DLL_OFF;
702
703 return PDM_APD_PPD;
704}
705
Angel Pons88521882020-01-05 20:21:20 +0100706static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100707{
708 u16 mr0reg, mch_cas, mch_wr;
709 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 +0100710
711 const enum pdwm_mode power_down = get_power_down_mode(ctrl);
712
713 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100714
Angel Pons7c49cb82020-03-16 23:17:32 +0100715 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100716 if (ctrl->CAS < 12) {
717 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
718 } else {
719 mch_cas = (u16) (ctrl->CAS - 12);
720 mch_cas = ((mch_cas << 1) | 0x1);
721 }
722
Angel Pons7c49cb82020-03-16 23:17:32 +0100723 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100724 mch_wr = mch_wr_t[ctrl->tWR - 5];
725
Angel Pons2bf28ed2020-11-12 13:49:59 +0100726 /* DLL Reset - self clearing - set after CLK frequency has been changed */
727 mr0reg = 1 << 8;
728
729 mr0reg |= (mch_cas & 0x1) << 2;
730 mr0reg |= (mch_cas & 0xe) << 3;
731 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100732
Angel Pons09fc4b92020-11-19 12:02:07 +0100733 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
734 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100735 return mr0reg;
736}
737
738static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
739{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200740 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100741}
742
Angel Ponsf9997482020-11-12 16:02:52 +0100743static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100744{
745 /* Get ODT based on rankmap */
746 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
747
748 if (dimms_per_ch == 1) {
749 return (const odtmap){60, 60};
750 } else {
751 return (const odtmap){120, 30};
752 }
753}
754
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100755static u32 encode_odt(u32 odt)
756{
757 switch (odt) {
758 case 30:
759 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
760 case 60:
761 return (1 << 2); // RZQ/4
762 case 120:
763 return (1 << 6); // RZQ/2
764 default:
765 case 0:
766 return 0;
767 }
768}
769
770static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
771{
772 odtmap odt;
773 u32 mr1reg;
774
Angel Ponsf9997482020-11-12 16:02:52 +0100775 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100776 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100777
778 mr1reg |= encode_odt(odt.rttnom);
779
780 return mr1reg;
781}
782
783static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
784{
785 u16 mr1reg;
786
787 mr1reg = make_mr1(ctrl, rank, channel);
788
789 write_mrreg(ctrl, channel, rank, 1, mr1reg);
790}
791
792static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
793{
Angel Pons868bca22020-11-13 13:38:04 +0100794 const u16 pasr = 0;
795 const u16 cwl = ctrl->CWL - 5;
796 const odtmap odt = get_ODT(ctrl, channel);
797
Angel Ponsdca3cb52020-11-13 13:42:07 +0100798 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100799 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
800 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100801
Angel Pons868bca22020-11-13 13:38:04 +0100802 u16 mr2reg = 0;
803 mr2reg |= pasr;
804 mr2reg |= cwl << 3;
805 mr2reg |= ctrl->auto_self_refresh << 6;
806 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100807 mr2reg |= (odt.rttwr / 60) << 9;
808
809 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100810
811 /* Program MR2 shadow */
812 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
813
814 reg32 &= 3 << 14 | 3 << 6;
815
816 reg32 |= mr2reg & ~(3 << 6);
817
Angel Pons927b1c02020-12-10 22:11:27 +0100818 if (srt)
819 reg32 |= 1 << (rank / 2 + 6);
820
821 if (ctrl->rank_mirror[channel][rank])
822 reg32 |= 1 << (rank / 2 + 14);
823
Angel Pons7f1363d2020-11-13 13:31:58 +0100824 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100825}
826
827static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
828{
829 write_mrreg(ctrl, channel, rank, 3, 0);
830}
831
Angel Pons88521882020-01-05 20:21:20 +0100832void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100833{
834 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100835 int channel;
836
837 FOR_ALL_POPULATED_CHANNELS {
838 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100839 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100840 dram_mr2(ctrl, slotrank, channel);
841
Angel Pons7c49cb82020-03-16 23:17:32 +0100842 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100843 dram_mr3(ctrl, slotrank, channel);
844
Angel Pons7c49cb82020-03-16 23:17:32 +0100845 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100846 dram_mr1(ctrl, slotrank, channel);
847
Angel Pons7c49cb82020-03-16 23:17:32 +0100848 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100849 dram_mr0(ctrl, slotrank, channel);
850 }
851 }
852
Angel Pons8f0757e2020-11-11 23:03:36 +0100853 const struct iosav_ssq zqcl_sequence[] = {
854 /* DRAM command NOP (without ODT nor chip selects) */
855 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200856 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100857 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200858 },
859 .subseq_ctrl = {
860 .cmd_executions = 1,
861 .cmd_delay_gap = 4,
862 .post_ssq_wait = 15,
863 .data_direction = SSQ_NA,
864 },
865 .sp_cmd_addr = {
866 .address = 2,
867 .rowbits = 6,
868 .bank = 0,
869 .rank = 0,
870 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100871 },
872 /* DRAM command ZQCL */
873 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200874 .sp_cmd_ctrl = {
875 .command = IOSAV_ZQCS,
876 .ranksel_ap = 1,
877 },
878 .subseq_ctrl = {
879 .cmd_executions = 1,
880 .cmd_delay_gap = 4,
881 .post_ssq_wait = 400,
882 .data_direction = SSQ_NA,
883 },
884 .sp_cmd_addr = {
885 .address = 1024,
886 .rowbits = 6,
887 .bank = 0,
888 .rank = 0,
889 },
890 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100891 .inc_rank = 1,
892 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200893 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100894 },
895 };
896 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100897
Angel Pons7c49cb82020-03-16 23:17:32 +0100898 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200899 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100900
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100902 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100903 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100904 }
905
Angel Pons7c49cb82020-03-16 23:17:32 +0100906 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100907 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100908
909 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100910 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100911
Angel Pons88521882020-01-05 20:21:20 +0100912 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100913
914 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
915
Angel Pons7c49cb82020-03-16 23:17:32 +0100916 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100917 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100918
Angel Ponsffd50152020-11-12 11:03:10 +0100919 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200920
Angel Pons7c49cb82020-03-16 23:17:32 +0100921 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200922 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100923
Angel Pons7c49cb82020-03-16 23:17:32 +0100924 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100925 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100926 }
927}
928
Felix Held3b906032020-01-14 17:05:43 +0100929static const u32 lane_base[] = {
930 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
931 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
932 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100933};
934
Angel Pons88521882020-01-05 20:21:20 +0100935void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100936{
Angel Pons7584e552020-11-19 21:34:32 +0100937 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100938 int lane;
939 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940
Angel Pons7584e552020-11-19 21:34:32 +0100941 u32 ctl_delay[NUM_SLOTS] = { 0 };
942 int cmd_delay = 0;
943
944 /* Enable CLK XOVER */
945 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
946 u32 clk_logic_dly = 0;
947
948 /*
949 * Apply command delay if desired setting is negative. Find the
950 * most negative value: 'cmd_delay' will be the absolute value.
951 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100952 FOR_ALL_POPULATED_RANKS {
Angel Pons7584e552020-11-19 21:34:32 +0100953 if (cmd_delay < -ctrl->timings[channel][slotrank].pi_coding)
954 cmd_delay = -ctrl->timings[channel][slotrank].pi_coding;
955 }
956 if (cmd_delay < 0) {
957 printk(BIOS_ERR, "C%d command delay underflow: %d\n", channel, cmd_delay);
958 cmd_delay = 0;
959 }
960 if (cmd_delay >= 128) {
961 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
962 cmd_delay = 127;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100963 }
964
Angel Pons7584e552020-11-19 21:34:32 +0100965 /* Apply control and clock delay if desired setting is positive */
966 if (cmd_delay == 0) {
967 for (slot = 0; slot < NUM_SLOTS; slot++) {
968 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
969 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
970
971 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
972
973 if (slot_map & 1)
974 ctl_delay[slot] += pi_coding_0 + cmd_delay;
975
976 if (slot_map & 2)
977 ctl_delay[slot] += pi_coding_1 + cmd_delay;
978
979 /* If both ranks in a slot are populated, use the average */
980 if (slot_map == 3)
981 ctl_delay[slot] /= 2;
982
983 if (ctl_delay[slot] >= 128) {
984 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
985 channel, slot, ctl_delay[slot]);
986 ctl_delay[slot] = 127;
987 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100988 }
Angel Pons7584e552020-11-19 21:34:32 +0100989 FOR_ALL_POPULATED_RANKS {
990 u32 clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
991
992 if (clk_delay >= 128) {
993 printk(BIOS_ERR, "C%dR%d clock delay overflow: %d\n",
994 channel, slotrank, clk_delay);
995 clk_delay = 127;
996 }
997
998 clk_pi_coding |= (clk_delay % 64) << (6 * slotrank);
999 clk_logic_dly |= (clk_delay / 64) << slotrank;
1000 }
1001 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001002
Angel Pons7c49cb82020-03-16 23:17:32 +01001003 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +01001004 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
1005 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
1006 };
Angel Pons7584e552020-11-19 21:34:32 +01001007 cmd_pi_coding.cmd_pi_code = cmd_delay % 64;
1008 cmd_pi_coding.cmd_logic_delay = cmd_delay / 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001009
Angel Pons7584e552020-11-19 21:34:32 +01001010 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % 64;
1011 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % 64;
1012 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / 64;
1013 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / 64;
Angel Pons737f1112020-11-13 14:07:30 +01001014
1015 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001016
Angel Pons7584e552020-11-19 21:34:32 +01001017 MCHBAR32(GDCRCKPICODE_ch(channel)) = clk_pi_coding;
1018 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = clk_logic_dly;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001019
Angel Pons88521882020-01-05 20:21:20 +01001020 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001021 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001022
Angel Pons88521882020-01-05 20:21:20 +01001023 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001024
1025 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001026 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001027
Angel Pons88521882020-01-05 20:21:20 +01001028 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001029 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001030
1031 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001032 const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
1033 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p;
1034 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n;
Angel Pons9fcc1102020-11-19 22:23:13 +01001035 const union gdcr_rx_reg gdcr_rx = {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001036 .rcven_pi_code = rcven % 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001037 .rx_dqs_p_pi_code = dqs_p,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001038 .rcven_logic_delay = rcven / 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001039 .rx_dqs_n_pi_code = dqs_n,
1040 };
1041 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) = gdcr_rx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001042
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001043 const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1044 const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq;
Angel Pons9fcc1102020-11-19 22:23:13 +01001045 const union gdcr_tx_reg gdcr_tx = {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001046 .tx_dq_pi_code = tx_dq % 64,
1047 .tx_dqs_pi_code = tx_dqs % 64,
1048 .tx_dqs_logic_delay = tx_dqs / 64,
1049 .tx_dq_logic_delay = tx_dq / 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001050 };
1051 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) = gdcr_tx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001052 }
1053 }
Angel Pons88521882020-01-05 20:21:20 +01001054 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1055 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001056}
1057
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001058static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001059{
Angel Pons88521882020-01-05 20:21:20 +01001060 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001061
Angel Ponsffd50152020-11-12 11:03:10 +01001062 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001063
Angel Pons7c49cb82020-03-16 23:17:32 +01001064 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001065 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001066
Angel Pons88521882020-01-05 20:21:20 +01001067 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001068}
1069
Angel Pons7c49cb82020-03-16 23:17:32 +01001070static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001071{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001072 u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
Angel Pons7c49cb82020-03-16 23:17:32 +01001073
1074 return (MCHBAR32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001075 GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001076}
1077
1078struct run {
1079 int middle;
1080 int end;
1081 int start;
1082 int all;
1083 int length;
1084};
1085
1086static struct run get_longest_zero_run(int *seq, int sz)
1087{
1088 int i, ls;
1089 int bl = 0, bs = 0;
1090 struct run ret;
1091
1092 ls = 0;
1093 for (i = 0; i < 2 * sz; i++)
1094 if (seq[i % sz]) {
1095 if (i - ls > bl) {
1096 bl = i - ls;
1097 bs = ls;
1098 }
1099 ls = i + 1;
1100 }
1101 if (bl == 0) {
1102 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001103 ret.start = 0;
1104 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001105 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001106 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001107 return ret;
1108 }
1109
Angel Pons7c49cb82020-03-16 23:17:32 +01001110 ret.start = bs % sz;
1111 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001112 ret.middle = (bs + (bl - 1) / 2) % sz;
1113 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001114 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001115
1116 return ret;
1117}
1118
Angel Ponsf3053392020-11-13 23:31:12 +01001119static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001120{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001121 int rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001122 int statistics[NUM_LANES][128];
1123 int lane;
1124
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001125 for (rcven = 0; rcven < 128; rcven++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001126 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001127 ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001128 }
1129 program_timings(ctrl, channel);
1130
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001131 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001132
1133 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001134 statistics[lane][rcven] =
1135 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001136 }
1137 }
1138 FOR_ALL_LANES {
1139 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001140 ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001141 upperA[lane] = rn.end;
1142 if (upperA[lane] < rn.middle)
1143 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001144
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001145 printram("rcven: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001146 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001147 }
1148}
1149
Angel Ponsf3053392020-11-13 23:31:12 +01001150static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001151{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001152 int rcven_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001153 int statistics[NUM_LANES][51];
1154 int lane, i;
1155
1156 memset(statistics, 0, sizeof(statistics));
1157
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001158 for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001159
1160 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001161 ctrl->timings[channel][slotrank].lanes[lane].rcven
1162 = upperA[lane] + rcven_delta + 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001163 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001164 program_timings(ctrl, channel);
1165
1166 for (i = 0; i < 100; i++) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001167 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001168 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001169 statistics[lane][rcven_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001170 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001171 }
1172 }
1173 }
1174 FOR_ALL_LANES {
1175 int last_zero, first_all;
1176
1177 for (last_zero = -25; last_zero <= 25; last_zero++)
1178 if (statistics[lane][last_zero + 25])
1179 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001180
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001181 last_zero--;
1182 for (first_all = -25; first_all <= 25; first_all++)
1183 if (statistics[lane][first_all + 25] == 100)
1184 break;
1185
Angel Pons7c49cb82020-03-16 23:17:32 +01001186 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001187
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001188 ctrl->timings[channel][slotrank].lanes[lane].rcven =
Angel Pons7c49cb82020-03-16 23:17:32 +01001189 (last_zero + first_all) / 2 + upperA[lane];
1190
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001191 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001192 lane, ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 }
1194}
1195
Angel Ponsf3053392020-11-13 23:31:12 +01001196static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001197{
1198 int works[NUM_LANES];
1199 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001200
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001201 while (1) {
1202 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001203
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001204 program_timings(ctrl, channel);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001205 test_rcven(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001206
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001207 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001208 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1209
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001210 if (works[lane])
1211 some_works = 1;
1212 else
1213 all_works = 0;
1214 }
1215 if (all_works)
1216 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001217
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001218 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001219 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001220 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1221 channel, slotrank);
1222 return MAKE_ERR;
1223 }
Angel Pons88521882020-01-05 20:21:20 +01001224 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001225 printram("4024 -= 2;\n");
1226 continue;
1227 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001228 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001230
Felix Heldef4fe3e2019-12-31 14:15:05 +01001231 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001232 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1233 channel, slotrank);
1234 return MAKE_ERR;
1235 }
1236 FOR_ALL_LANES if (works[lane]) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001237 ctrl->timings[channel][slotrank].lanes[lane].rcven += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001238 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001239 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001240 }
1241 }
1242 return 0;
1243}
1244
Angel Pons12bd8ab2020-11-13 23:10:52 +01001245static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001246{
1247 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001248 u16 logic_delay_min = 7;
1249 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001250
1251 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001252 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001253
1254 logic_delay_min = MIN(logic_delay_min, logic_delay);
1255 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001256 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001257
1258 if (logic_delay_max < logic_delay_min) {
1259 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1260 logic_delay_max, logic_delay_min, channel, slotrank);
1261 }
1262
1263 assert(logic_delay_max >= logic_delay_min);
1264
1265 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001266}
1267
Angel Pons12bd8ab2020-11-13 23:10:52 +01001268static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001269{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001270 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001271
Angel Pons7c49cb82020-03-16 23:17:32 +01001272 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001273 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001274
Angel Pons12bd8ab2020-11-13 23:10:52 +01001275 if (prev < post)
1276 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001277
Angel Pons12bd8ab2020-11-13 23:10:52 +01001278 else if (prev > post)
1279 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001280
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001281 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001282 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001283
Angel Pons12bd8ab2020-11-13 23:10:52 +01001284 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1285 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1286 printram("4024 += %d;\n", latency_offset);
1287 printram("4028 += %d;\n", latency_offset);
1288
1289 return post;
1290}
1291
1292static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1293{
1294 u16 logic_delay_min = 7;
1295 int lane;
1296
1297 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001298 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001299
1300 logic_delay_min = MIN(logic_delay_min, logic_delay);
1301 }
1302
1303 if (logic_delay_min >= 2) {
1304 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1305 logic_delay_min, channel, slotrank);
1306 }
1307
1308 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001309 ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001310 }
1311 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1312 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001313}
1314
Angel Pons7f5a97c2020-11-13 16:58:46 +01001315int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001316{
1317 int channel, slotrank, lane;
1318 int err;
1319
1320 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1321 int all_high, some_high;
1322 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001323 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001324
Angel Pons88521882020-01-05 20:21:20 +01001325 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001326
Angel Ponsffd50152020-11-12 11:03:10 +01001327 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001328
Angel Pons7c49cb82020-03-16 23:17:32 +01001329 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001330 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001331
Angel Pons58b609b2020-11-13 14:35:29 +01001332 const union gdcr_training_mod_reg training_mod = {
1333 .receive_enable_mode = 1,
1334 .training_rank_sel = slotrank,
1335 .odt_always_on = 1,
1336 };
1337 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001338
Felix Heldef4fe3e2019-12-31 14:15:05 +01001339 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001340 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001341 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001342
Angel Ponsf3053392020-11-13 23:31:12 +01001343 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001344
Felix Held2bb3cdf2018-07-28 00:23:59 +02001345 all_high = 1;
1346 some_high = 0;
1347 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001348 if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001349 some_high = 1;
1350 else
1351 all_high = 0;
1352 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001353
1354 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001355 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001356 printram("4028--;\n");
1357 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001358 ctrl->timings[channel][slotrank].lanes[lane].rcven -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001359 upperA[lane] -= 0x40;
1360
1361 }
1362 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001363 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001364 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001365 printram("4024++;\n");
1366 printram("4028++;\n");
1367 }
1368
1369 program_timings(ctrl, channel);
1370
Angel Pons12bd8ab2020-11-13 23:10:52 +01001371 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001372
Angel Ponsf3053392020-11-13 23:31:12 +01001373 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001374 if (err)
1375 return err;
1376
Angel Pons12bd8ab2020-11-13 23:10:52 +01001377 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001378
Angel Ponsf3053392020-11-13 23:31:12 +01001379 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380
Angel Pons12bd8ab2020-11-13 23:10:52 +01001381 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
Angel Pons12bd8ab2020-11-13 23:10:52 +01001383 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Pons12bd8ab2020-11-13 23:10:52 +01001385 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386
1387 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001388 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001389 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390
1391 printram("final results:\n");
1392 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001393 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001394 ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001395
Angel Pons88521882020-01-05 20:21:20 +01001396 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001397
1398 toggle_io_reset();
1399 }
1400
1401 FOR_ALL_POPULATED_CHANNELS {
1402 program_timings(ctrl, channel);
1403 }
Angel Ponsc6742232020-11-15 13:26:21 +01001404
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001405 return 0;
1406}
1407
Angel Pons011661c2020-11-15 18:21:35 +01001408static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001409{
1410 int lane;
1411
1412 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001413 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1414 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001415 }
1416
Angel Pons88521882020-01-05 20:21:20 +01001417 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001418
Angel Ponsffd50152020-11-12 11:03:10 +01001419 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1420 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001421
Angel Pons7c49cb82020-03-16 23:17:32 +01001422 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001423 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001424
Angel Pons88521882020-01-05 20:21:20 +01001425 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001426
Angel Pons801a5cb2020-11-15 15:48:29 +01001427 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001428
Angel Pons7c49cb82020-03-16 23:17:32 +01001429 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001430 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001431
Angel Pons88521882020-01-05 20:21:20 +01001432 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001433}
1434
Angel Pons011661c2020-11-15 18:21:35 +01001435static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001436{
1437 int min = data[0];
1438 int max = min;
1439 int i;
1440 for (i = 1; i < count; i++) {
1441 if (min > data[i])
1442 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001443
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001444 if (max < data[i])
1445 max = data[i];
1446 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001447 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001448 for (i = 0; i < count; i++)
1449 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001450
Angel Pons891f2bc2020-01-10 01:27:28 +01001451 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001452}
1453
Angel Pons011661c2020-11-15 18:21:35 +01001454static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001455{
Angel Pons011661c2020-11-15 18:21:35 +01001456 int tx_dq;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001457 int stats[NUM_LANES][MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001458 int lane;
1459
Angel Pons88521882020-01-05 20:21:20 +01001460 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001461
Angel Ponsffd50152020-11-12 11:03:10 +01001462 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001463
Angel Pons7c49cb82020-03-16 23:17:32 +01001464 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001465 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001466
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001467 for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) {
1468 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001469 program_timings(ctrl, channel);
1470
Angel Pons011661c2020-11-15 18:21:35 +01001471 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001472
1473 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001474 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001475 }
1476 }
1477 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001478 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1479
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001480 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001481 printk(BIOS_EMERG, "tx_dq discovery failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001482 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001483 /*
1484 * With command training not being done yet, the lane can be erroneous.
1485 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001486 */
Angel Pons011661c2020-11-15 18:21:35 +01001487 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001488 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1489
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001490 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001491 printk(BIOS_EMERG, "tx_dq recovery failed\n");
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001492 return MAKE_ERR;
1493 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001494 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001495 ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle;
1496 printram("tx_dq: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001497 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001498 }
1499 return 0;
1500}
1501
Angel Pons88521882020-01-05 20:21:20 +01001502static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001503{
1504 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001505
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001506 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1507 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001508
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001509 return ret;
1510}
1511
Angel Pons765d4652020-11-11 14:44:35 +01001512/* Each cacheline is 64 bits long */
1513static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1514{
1515 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1516}
1517
Angel Pons88521882020-01-05 20:21:20 +01001518static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001519{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301520 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001521 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001522
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001523 for (j = 0; j < 16; j++)
1524 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001525
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001526 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001527
1528 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001529}
1530
Angel Pons88521882020-01-05 20:21:20 +01001531static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001532{
1533 int ret = 0;
1534 int channel;
1535 FOR_ALL_POPULATED_CHANNELS ret++;
1536 return ret;
1537}
1538
Angel Pons88521882020-01-05 20:21:20 +01001539static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001540{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301541 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001542 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301543 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001544
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001545 for (j = 0; j < 16; j++)
1546 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001547
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001548 for (j = 0; j < 16; j++)
1549 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001550
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001551 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001552
1553 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001554}
1555
Angel Pons820bce72020-11-14 17:02:55 +01001556static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001558 int tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001559 int statistics[NUM_LANES][128];
1560 int lane;
1561
Angel Pons58b609b2020-11-13 14:35:29 +01001562 const union gdcr_training_mod_reg training_mod = {
1563 .write_leveling_mode = 1,
1564 .training_rank_sel = slotrank,
1565 .enable_dqs_wl = 5,
1566 .odt_always_on = 1,
1567 .force_drive_enable = 1,
1568 };
1569 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001570
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001571 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1572 int bank = 1;
1573
1574 if (ctrl->rank_mirror[channel][slotrank])
1575 ddr3_mirror_mrreg(&bank, &mr1reg);
1576
1577 wait_for_iosav(channel);
1578
1579 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1580
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001581 for (tx_dqs = 0; tx_dqs < 128; tx_dqs++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001582 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001583 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001584 }
1585 program_timings(ctrl, channel);
1586
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001587 /* Execute command queue */
1588 iosav_run_once(channel);
1589
1590 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001591
1592 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001593 statistics[lane][tx_dqs] = !((MCHBAR32(lane_base[lane] +
1594 GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >>
1595 (tx_dqs % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001596 }
1597 }
1598 FOR_ALL_LANES {
1599 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001600 /*
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001601 * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value
1602 * of tx_dqs by a small value, which might cause the 6-bit value to overflow if
Angel Pons7c49cb82020-03-16 23:17:32 +01001603 * it's close to 0x3f. Increment the value by a small offset if it's likely
1604 * to overflow, to make sure it won't overflow while running tests and bricks
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001605 * the system due to a non matching tx_dq.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001606 *
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001607 * TODO: find out why some tests (edge write discovery) increment tx_dqs.
Angel Pons7c49cb82020-03-16 23:17:32 +01001608 */
1609 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001610 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001611 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001612 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001613
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001614 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001615 if (rn.all) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001616 printk(BIOS_EMERG, "tx_dqs discovery failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001617 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001618
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 return MAKE_ERR;
1620 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001621 printram("tx_dqs: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01001622 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001623 }
1624 return 0;
1625}
1626
Angel Pons820bce72020-11-14 17:02:55 +01001627static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001628{
1629 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001630 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001631 if (val == 0xffffffffffffffffLL)
1632 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001633 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001634 /* DQS is late, needs negative 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 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001639 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001640 for (i = 0; i < 8; i++)
1641 if (val >> (8 * (7 - i) + 4))
1642 return i;
1643 }
1644 return 8;
1645}
1646
Angel Ponsbf13ef02020-11-11 18:40:06 +01001647static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001648{
1649 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001650
1651 const union gdcr_training_mod_reg training_mod = {
1652 .dq_dqs_training_res = 1,
1653 };
1654 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1655
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001656 FOR_ALL_POPULATED_CHANNELS {
1657 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001658 }
1659 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1660
Angel Pons765d4652020-11-11 14:44:35 +01001661 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001662 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663
Angel Pons88521882020-01-05 20:21:20 +01001664 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001665
Angel Ponsffd50152020-11-12 11:03:10 +01001666 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001667
Angel Pons7c49cb82020-03-16 23:17:32 +01001668 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001669 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001670
Angel Pons88521882020-01-05 20:21:20 +01001671 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001672
Angel Pons8f0757e2020-11-11 23:03:36 +01001673 const struct iosav_ssq rd_sequence[] = {
1674 /* DRAM command PREA */
1675 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001676 .sp_cmd_ctrl = {
1677 .command = IOSAV_PRE,
1678 .ranksel_ap = 1,
1679 },
1680 .subseq_ctrl = {
1681 .cmd_executions = 1,
1682 .cmd_delay_gap = 3,
1683 .post_ssq_wait = ctrl->tRP,
1684 .data_direction = SSQ_NA,
1685 },
1686 .sp_cmd_addr = {
1687 .address = 1024,
1688 .rowbits = 6,
1689 .bank = 0,
1690 .rank = slotrank,
1691 },
1692 .addr_update = {
1693 .addr_wrap = 18,
1694 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001695 },
1696 /* DRAM command ACT */
1697 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001698 .sp_cmd_ctrl = {
1699 .command = IOSAV_ACT,
1700 .ranksel_ap = 1,
1701 },
1702 .subseq_ctrl = {
1703 .cmd_executions = 1,
1704 .cmd_delay_gap = 3,
1705 .post_ssq_wait = ctrl->tRCD,
1706 .data_direction = SSQ_NA,
1707 },
1708 .sp_cmd_addr = {
1709 .address = 0,
1710 .rowbits = 6,
1711 .bank = 0,
1712 .rank = slotrank,
1713 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001714 },
1715 /* DRAM command RD */
1716 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001717 .sp_cmd_ctrl = {
1718 .command = IOSAV_RD,
1719 .ranksel_ap = 3,
1720 },
1721 .subseq_ctrl = {
1722 .cmd_executions = 1,
1723 .cmd_delay_gap = 3,
1724 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001725 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001726 ctrl->timings[channel][slotrank].io_latency,
1727 .data_direction = SSQ_RD,
1728 },
1729 .sp_cmd_addr = {
1730 .address = 8,
1731 .rowbits = 6,
1732 .bank = 0,
1733 .rank = slotrank,
1734 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001735 },
1736 };
1737 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001738
Angel Pons7c49cb82020-03-16 23:17:32 +01001739 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001740 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001741
Angel Pons88521882020-01-05 20:21:20 +01001742 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001743 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001744 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001745 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001746 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001747
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001748 old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1749 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs +=
Angel Pons820bce72020-11-14 17:02:55 +01001750 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001751
1752 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001753 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001754 old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001755 }
1756 }
Angel Pons88521882020-01-05 20:21:20 +01001757 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001758}
1759
Angel Pons7d115132020-11-14 01:44:44 +01001760static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001761{
Angel Pons7d115132020-11-14 01:44:44 +01001762 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001763
Angel Pons7d115132020-11-14 01:44:44 +01001764 FOR_ALL_POPULATED_CHANNELS {
1765 /* choose an existing rank */
1766 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001767
Angel Pons7d115132020-11-14 01:44:44 +01001768 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001769
Angel Pons7d115132020-11-14 01:44:44 +01001770 /* Execute command queue */
1771 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001772
Angel Pons7d115132020-11-14 01:44:44 +01001773 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001774
Angel Pons7d115132020-11-14 01:44:44 +01001775 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1776 }
1777
1778 /* Refresh disable */
1779 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1780
1781 FOR_ALL_POPULATED_CHANNELS {
1782 /* Execute the same command queue */
1783 iosav_run_once(channel);
1784
1785 wait_for_iosav(channel);
1786 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001787}
1788
Angel Pons7c49cb82020-03-16 23:17:32 +01001789/*
1790 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001791 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001792 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1793 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1794 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1795 * CLK/ADDR/CMD signals have the same routing delay.
1796 *
1797 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1798 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1799 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001800 */
Angel Pons820bce72020-11-14 17:02:55 +01001801static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001802{
Angel Pons820bce72020-11-14 17:02:55 +01001803 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001804
Angel Pons7d115132020-11-14 01:44:44 +01001805 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001806
Angel Pons7c49cb82020-03-16 23:17:32 +01001807 /* Enable write leveling on all ranks
1808 Disable all DQ outputs
1809 Only NOP is allowed in this mode */
1810 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1811 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001812 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813
Angel Ponsa1f17142020-11-15 12:50:03 +01001814 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001815 const union gdcr_training_mod_reg training_mod = {
1816 .write_leveling_mode = 1,
1817 .enable_dqs_wl = 5,
1818 .odt_always_on = 1,
1819 .force_drive_enable = 1,
1820 };
1821 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001822
1823 toggle_io_reset();
1824
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001825 /* Set any valid value for tx_dqs, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001827 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001828 if (err)
1829 return err;
1830 }
1831
Angel Pons7c49cb82020-03-16 23:17:32 +01001832 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001833 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001834 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001835
Angel Pons88521882020-01-05 20:21:20 +01001836 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001837
1838 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001839 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001840
Angel Pons7c49cb82020-03-16 23:17:32 +01001841 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001842 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001843
1844 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001845 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001846 MCHBAR32(IOSAV_STATUS_ch(channel));
1847 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001848
Angel Ponsffd50152020-11-12 11:03:10 +01001849 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001850
Angel Pons7c49cb82020-03-16 23:17:32 +01001851 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001852 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001853
Angel Pons88521882020-01-05 20:21:20 +01001854 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001855 }
1856
1857 toggle_io_reset();
1858
Angel Pons820bce72020-11-14 17:02:55 +01001859 return 0;
1860}
1861
1862int write_training(ramctr_timing *ctrl)
1863{
Angel Ponsc6742232020-11-15 13:26:21 +01001864 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001865 int err;
1866
1867 FOR_ALL_POPULATED_CHANNELS
1868 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1869
Angel Pons4c76d252020-11-15 13:06:53 +01001870 printram("CPE\n");
1871
Angel Pons820bce72020-11-14 17:02:55 +01001872 err = jedec_write_leveling(ctrl);
1873 if (err)
1874 return err;
1875
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001876 printram("CPF\n");
1877
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001878 FOR_ALL_POPULATED_CHANNELS {
1879 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001880 }
1881
1882 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001883 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001884 if (err)
1885 return err;
1886 }
1887
1888 FOR_ALL_POPULATED_CHANNELS
1889 program_timings(ctrl, channel);
1890
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001891 /* measure and adjust tx_dqs timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001892 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001893
1894 FOR_ALL_POPULATED_CHANNELS
1895 program_timings(ctrl, channel);
1896
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001897 return 0;
1898}
1899
Angel Ponsbf13ef02020-11-11 18:40:06 +01001900static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001901{
1902 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001903 int tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001904 int lanes_ok = 0;
1905 int ctr = 0;
1906 int lane;
1907
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001908 for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001909 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001910 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
1911 saved_rt.lanes[lane].tx_dq + tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001912 }
1913 program_timings(ctrl, channel);
1914 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001915 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001916 }
1917
Angel Pons765d4652020-11-11 14:44:35 +01001918 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001919 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001920
Angel Pons88521882020-01-05 20:21:20 +01001921 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001922
Angel Ponsffd50152020-11-12 11:03:10 +01001923 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001924
1925 /* Program LFSR for the RD/WR subsequences */
1926 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1927 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001928
Angel Pons7c49cb82020-03-16 23:17:32 +01001929 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001930 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001931
Angel Pons88521882020-01-05 20:21:20 +01001932 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001933 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001934 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001935
1936 if (r32 == 0)
1937 lanes_ok |= 1 << lane;
1938 }
1939 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001940 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001941 break;
1942 }
1943
1944 ctrl->timings[channel][slotrank] = saved_rt;
1945
Patrick Rudolphdd662872017-10-28 18:20:11 +02001946 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001947}
1948
Angel Pons88521882020-01-05 20:21:20 +01001949static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001950{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301951 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01001952 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
1953 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001954
1955 if (patno) {
1956 u8 base8 = 0x80 >> ((patno - 1) % 8);
1957 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1958 for (i = 0; i < 32; i++) {
1959 for (j = 0; j < 16; j++) {
1960 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001961
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001962 if (invert[patno - 1][i] & (1 << (j / 2)))
1963 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001964
1965 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001966 }
1967 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001968 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001969 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1970 for (j = 0; j < 16; j++) {
1971 const u32 val = pattern[i][j];
1972 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1973 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974 }
1975 sfence();
1976 }
Angel Pons765d4652020-11-11 14:44:35 +01001977
1978 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001979}
1980
Angel Pons88521882020-01-05 20:21:20 +01001981static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001982{
Angel Pons7d115132020-11-14 01:44:44 +01001983 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001984
Angel Pons7c49cb82020-03-16 23:17:32 +01001985 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001986 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001987
1988 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001989 dram_mrscommands(ctrl);
1990
1991 toggle_io_reset();
1992}
1993
Angel Ponsbf13ef02020-11-11 18:40:06 +01001994#define CT_MIN_PI -127
1995#define CT_MAX_PI 128
1996#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1997
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001998#define MIN_C320C_LEN 13
1999
2000static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2001{
2002 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2003 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002004 int command_pi;
2005 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002006 int delta = 0;
2007
2008 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2009
2010 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002011 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002012 }
2013
2014 ctrl->cmd_stretch[channel] = cmd_stretch;
2015
Angel Pons7a612742020-11-12 13:34:03 +01002016 const union tc_rap_reg tc_rap = {
2017 .tRRD = ctrl->tRRD,
2018 .tRTP = ctrl->tRTP,
2019 .tCKE = ctrl->tCKE,
2020 .tWTR = ctrl->tWTR,
2021 .tFAW = ctrl->tFAW,
2022 .tWR = ctrl->tWR,
2023 .tCMD = ctrl->cmd_stretch[channel],
2024 };
2025 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002026
2027 if (ctrl->cmd_stretch[channel] == 2)
2028 delta = 2;
2029 else if (ctrl->cmd_stretch[channel] == 0)
2030 delta = 4;
2031
2032 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002033 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002034 }
2035
Angel Ponsbf13ef02020-11-11 18:40:06 +01002036 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002037 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002038 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002039 }
2040 program_timings(ctrl, channel);
2041 reprogram_320c(ctrl);
2042 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002043 stat[slotrank][command_pi - CT_MIN_PI] =
2044 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002045 }
2046 }
2047 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002048 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002049
Angel Ponsbf13ef02020-11-11 18:40:06 +01002050 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002051 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2052 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002053
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002054 if (rn.all || rn.length < MIN_C320C_LEN) {
2055 FOR_ALL_POPULATED_RANKS {
2056 ctrl->timings[channel][slotrank] =
2057 saved_timings[channel][slotrank];
2058 }
2059 return MAKE_ERR;
2060 }
2061 }
2062
2063 return 0;
2064}
2065
Angel Pons7c49cb82020-03-16 23:17:32 +01002066/*
2067 * Adjust CMD phase shift and try multiple command rates.
2068 * A command rate of 2T doubles the time needed for address and command decode.
2069 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002070int command_training(ramctr_timing *ctrl)
2071{
2072 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002073
2074 FOR_ALL_POPULATED_CHANNELS {
2075 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002076 }
2077
2078 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002079 int cmdrate, err;
2080
2081 /*
2082 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002083 * Issue:
2084 * While c320c discovery seems to succeed raminit will fail in write training.
2085 *
2086 * Workaround:
2087 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2088 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002089 *
2090 * Single DIMM per channel:
2091 * Try command rate 1T and 2T
2092 */
2093 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002094 if (ctrl->tCMD)
2095 /* XMP gives the CMD rate in clock ticks, not ns */
2096 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002097
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002098 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002099 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2100
2101 if (!err)
2102 break;
2103 }
2104
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002105 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002106 printk(BIOS_EMERG, "c320c discovery failed\n");
2107 return err;
2108 }
2109
Angel Pons891f2bc2020-01-10 01:27:28 +01002110 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111 }
2112
2113 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002114 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002115
2116 reprogram_320c(ctrl);
2117 return 0;
2118}
2119
Angel Pons4c79f932020-11-14 01:26:52 +01002120static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002121{
Angel Pons96a06dd2020-11-14 00:33:18 +01002122 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002123 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002124 int lane;
2125
Angel Pons96a06dd2020-11-14 00:33:18 +01002126 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002127 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002128 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi;
2129 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002130 }
2131 program_timings(ctrl, channel);
2132
2133 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002134 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2135 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002136 }
2137
Angel Pons88521882020-01-05 20:21:20 +01002138 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002139
Angel Ponsffd50152020-11-12 11:03:10 +01002140 iosav_write_read_mpr_sequence(
2141 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002142
Angel Pons7c49cb82020-03-16 23:17:32 +01002143 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002144 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002145
Angel Pons88521882020-01-05 20:21:20 +01002146 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002147
2148 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002149 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002150 }
2151 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002152
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002153 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002154 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002155 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002156
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002157 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002158 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2159 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002160 return MAKE_ERR;
2161 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002162 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002163 }
2164 return 0;
2165}
2166
Angel Pons60971dc2020-11-14 00:49:38 +01002167static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2168{
2169 int slotrank, lane;
2170
2171 fill_pattern0(ctrl, channel, 0, 0);
2172 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002173 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002174 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2175 }
2176
2177 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002178 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16;
2179 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16;
Angel Pons60971dc2020-11-14 00:49:38 +01002180 }
2181
2182 program_timings(ctrl, channel);
2183
2184 FOR_ALL_POPULATED_RANKS {
2185 wait_for_iosav(channel);
2186
2187 iosav_write_read_mpr_sequence(
2188 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2189
2190 /* Execute command queue */
2191 iosav_run_once(channel);
2192
2193 wait_for_iosav(channel);
2194 }
2195
2196 /* XXX: check any measured value ? */
2197
2198 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002199 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48;
2200 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48;
Angel Pons60971dc2020-11-14 00:49:38 +01002201 }
2202
2203 program_timings(ctrl, channel);
2204
2205 FOR_ALL_POPULATED_RANKS {
2206 wait_for_iosav(channel);
2207
2208 iosav_write_read_mpr_sequence(
2209 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2210
2211 /* Execute command queue */
2212 iosav_run_once(channel);
2213
2214 wait_for_iosav(channel);
2215 }
2216
2217 /* XXX: check any measured value ? */
2218
2219 FOR_ALL_LANES {
2220 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2221 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2222 }
2223}
2224
Angel Pons4c79f932020-11-14 01:26:52 +01002225int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002226{
2227 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2228 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2229 int channel, slotrank, lane;
2230 int err;
2231
Angel Pons88521882020-01-05 20:21:20 +01002232 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002233
2234 toggle_io_reset();
2235
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002236 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002237 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002238
2239 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002240 }
2241
Angel Pons0c3936e2020-03-22 12:49:27 +01002242 /*
2243 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2244 * also use a single loop. It would seem that it is a debugging configuration.
2245 */
Angel Pons88521882020-01-05 20:21:20 +01002246 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2247 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002248
2249 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002250 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002251 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002252 if (err)
2253 return err;
2254 }
2255
Angel Pons88521882020-01-05 20:21:20 +01002256 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2257 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002258
2259 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002260 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002261 rising_edges[channel][slotrank]);
2262 if (err)
2263 return err;
2264 }
2265
Angel Pons88521882020-01-05 20:21:20 +01002266 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002267
2268 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002269 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002270 falling_edges[channel][slotrank][lane];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002271 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002272 rising_edges[channel][slotrank][lane];
2273 }
2274
2275 FOR_ALL_POPULATED_CHANNELS {
2276 program_timings(ctrl, channel);
2277 }
2278
Angel Pons50a6fe72020-11-14 01:18:14 +01002279 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002280 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002281 }
2282 return 0;
2283}
2284
Angel Pons08f749d2020-11-17 16:50:56 +01002285static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002286{
Angel Pons08f749d2020-11-17 16:50:56 +01002287 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2288
Angel Pons7c49cb82020-03-16 23:17:32 +01002289 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002290 int lower[NUM_LANES];
2291 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002292 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002293
2294 FOR_ALL_LANES {
2295 lower[lane] = 0;
2296 upper[lane] = MAX_EDGE_TIMING;
2297 }
2298
Angel Pons08f749d2020-11-17 16:50:56 +01002299 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002300 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002301 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002302 };
2303 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2304 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002305
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002306 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2307 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002308 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002309
Angel Pons08f749d2020-11-17 16:50:56 +01002310 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002311 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002312 ctrl->timings[channel][slotrank].lanes[lane]
2313 .rx_dqs_p = read_pi;
2314 ctrl->timings[channel][slotrank].lanes[lane]
2315 .rx_dqs_n = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002316 }
2317 program_timings(ctrl, channel);
2318
2319 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002320 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2321 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002322 }
Angel Pons88521882020-01-05 20:21:20 +01002323 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324
Angel Ponsffd50152020-11-12 11:03:10 +01002325 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002326
Angel Pons7c49cb82020-03-16 23:17:32 +01002327 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002328 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002329
Angel Pons88521882020-01-05 20:21:20 +01002330 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002331 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002332 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002333 }
2334
Angel Pons7c49cb82020-03-16 23:17:32 +01002335 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002336 raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002337 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002338
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002339 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002340 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002341 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002342
2343 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2344 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002345
2346 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2347
2348 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2349 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2350 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002351 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002352
2353 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2354 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2355
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002356 edges[lane] = (lower[lane] + upper[lane]) / 2;
2357 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002358 printk(BIOS_EMERG, "edge write discovery failed: "
2359 "%d, %d, %d\n", channel, slotrank, lane);
2360
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002361 return MAKE_ERR;
2362 }
2363 }
2364 }
2365 }
2366
Angel Ponsa93f46e2020-11-17 16:54:01 +01002367 /* Restore nominal Vref after training */
2368 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002369 printram("CPA\n");
2370 return 0;
2371}
2372
Angel Pons08f749d2020-11-17 16:50:56 +01002373int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002374{
2375 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002376 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2377 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002378
Angel Pons7c49cb82020-03-16 23:17:32 +01002379 /*
2380 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2381 * also use a single loop. It would seem that it is a debugging configuration.
2382 */
Angel Pons88521882020-01-05 20:21:20 +01002383 MCHBAR32(IOSAV_DC_MASK) = 0x300;
Angel Pons08f749d2020-11-17 16:50:56 +01002384 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002385
2386 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002387 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002388 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002389 if (err)
2390 return err;
2391 }
2392
Angel Pons88521882020-01-05 20:21:20 +01002393 MCHBAR32(IOSAV_DC_MASK) = 0x200;
Angel Pons08f749d2020-11-17 16:50:56 +01002394 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002395
2396 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002397 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002398 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002399 if (err)
2400 return err;
2401 }
2402
Angel Pons88521882020-01-05 20:21:20 +01002403 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002404
2405 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002406 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Angel Pons7c49cb82020-03-16 23:17:32 +01002407 falling_edges[channel][slotrank][lane];
2408
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002409 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Angel Pons7c49cb82020-03-16 23:17:32 +01002410 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002411 }
2412
2413 FOR_ALL_POPULATED_CHANNELS
2414 program_timings(ctrl, channel);
2415
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002416 return 0;
2417}
2418
Angel Pons2a7d7522020-11-19 12:49:07 +01002419static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002420{
Angel Pons88521882020-01-05 20:21:20 +01002421 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002422
Angel Ponsffd50152020-11-12 11:03:10 +01002423 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002424
Angel Pons7c49cb82020-03-16 23:17:32 +01002425 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002426 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002427
Angel Pons88521882020-01-05 20:21:20 +01002428 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002429}
2430
Angel Pons2a7d7522020-11-19 12:49:07 +01002431static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002432{
Angel Pons2a7d7522020-11-19 12:49:07 +01002433 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24);
2434 udelay(2);
2435}
2436
2437int aggressive_write_training(ramctr_timing *ctrl)
2438{
2439 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002440 int i, pat;
2441
2442 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2443 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2444 int channel, slotrank, lane;
2445
Angel Pons9fbb1b02020-11-19 12:53:36 +01002446 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2447 if (!IS_IVY_CPU(ctrl->cpu))
2448 return 0;
2449
2450 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2451 return 0;
2452
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002453 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2454 lower[channel][slotrank][lane] = 0;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002455 upper[channel][slotrank][lane] = MAX_TX_DQ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002456 }
2457
Angel Pons2a7d7522020-11-19 12:49:07 +01002458 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2459 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2460
2461 if (enable_iosav_opt)
2462 MCHBAR32(MCMNTS_SPARE) = 1;
2463
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002464 printram("discover tx_dq write:\n");
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002465
Angel Pons2a7d7522020-11-19 12:49:07 +01002466 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002467 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002468 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002469
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2471 FOR_ALL_POPULATED_RANKS {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002472 int tx_dq;
2473 u32 raw_stats[MAX_TX_DQ + 1];
2474 int stats[MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002475
2476 /* Make sure rn.start < rn.end */
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002477 stats[MAX_TX_DQ] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002478
2479 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002480
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002481 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002482 FOR_ALL_LANES {
2483 ctrl->timings[channel][slotrank]
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002484 .lanes[lane].tx_dq = tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01002485 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002486 program_timings(ctrl, channel);
2487
Angel Pons2a7d7522020-11-19 12:49:07 +01002488 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002489
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002490 raw_stats[tx_dq] = MCHBAR32(
Angel Pons098240eb2020-03-22 12:55:32 +01002491 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002492 }
2493 FOR_ALL_LANES {
2494 struct run rn;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002495 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
2496 stats[tx_dq] = !!(raw_stats[tx_dq]
Angel Pons7c49cb82020-03-16 23:17:32 +01002497 & (1 << lane));
2498 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002500 rn = get_longest_zero_run(stats, MAX_TX_DQ + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002501 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002502 printk(BIOS_EMERG,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002503 "tx_dq write discovery failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002504 "%d, %d, %d\n", channel,
2505 slotrank, lane);
2506
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002507 return MAKE_ERR;
2508 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002509 printram("tx_dq: %d, %d, %d: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002510 "0x%02x-0x%02x-0x%02x, "
2511 "0x%02x-0x%02x\n", channel, slotrank,
2512 i, rn.start, rn.middle, rn.end,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002513 rn.start + ctrl->tx_dq_offset[i],
2514 rn.end - ctrl->tx_dq_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002515
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002516 lower[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002517 MAX(rn.start + ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002518 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002519
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002520 upper[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002521 MIN(rn.end - ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002522 upper[channel][slotrank][lane]);
2523
2524 }
2525 }
2526 }
2527 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002528 }
2529
Angel Pons2a7d7522020-11-19 12:49:07 +01002530 FOR_ALL_CHANNELS {
2531 /* Restore nominal write Vref after training */
2532 set_write_vref(channel, 0);
2533 }
2534
2535 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2536 if (enable_iosav_opt)
2537 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002538
2539 printram("CPB\n");
2540
2541 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002542 printram("tx_dq %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002543 (lower[channel][slotrank][lane] +
2544 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002545
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002546 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002547 (lower[channel][slotrank][lane] +
2548 upper[channel][slotrank][lane]) / 2;
2549 }
2550 FOR_ALL_POPULATED_CHANNELS {
2551 program_timings(ctrl, channel);
2552 }
2553 return 0;
2554}
2555
Angel Pons88521882020-01-05 20:21:20 +01002556void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002557{
2558 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002559 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002560
2561 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2562 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002563 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002564 FOR_ALL_LANES mat =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002565 MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002566 printram("normalize %d, %d, %d: mat %d\n",
2567 channel, slotrank, lane, mat);
2568
Felix Heldef4fe3e2019-12-31 14:15:05 +01002569 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002570 printram("normalize %d, %d, %d: delta %d\n",
2571 channel, slotrank, lane, delta);
2572
Angel Pons88521882020-01-05 20:21:20 +01002573 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002574 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002575 }
2576
2577 FOR_ALL_POPULATED_CHANNELS {
2578 program_timings(ctrl, channel);
2579 }
2580}
2581
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002582int channel_test(ramctr_timing *ctrl)
2583{
2584 int channel, slotrank, lane;
2585
2586 slotrank = 0;
2587 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002588 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002589 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002590 return MAKE_ERR;
2591 }
2592 FOR_ALL_POPULATED_CHANNELS {
2593 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002594 }
2595
2596 for (slotrank = 0; slotrank < 4; slotrank++)
2597 FOR_ALL_CHANNELS
2598 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2599 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002600 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2601 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002602 }
Angel Pons88521882020-01-05 20:21:20 +01002603 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002604
Angel Ponsffd50152020-11-12 11:03:10 +01002605 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002606
Angel Pons7c49cb82020-03-16 23:17:32 +01002607 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002608 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002609
Angel Pons88521882020-01-05 20:21:20 +01002610 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002611 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002612 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002613 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2614 channel, slotrank, lane);
2615 return MAKE_ERR;
2616 }
2617 }
2618 return 0;
2619}
2620
Patrick Rudolphdd662872017-10-28 18:20:11 +02002621void channel_scrub(ramctr_timing *ctrl)
2622{
2623 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002624 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002625
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002626 FOR_ALL_POPULATED_CHANNELS {
2627 wait_for_iosav(channel);
2628 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002629 }
2630
2631 /*
2632 * During runtime the "scrubber" will periodically scan through the memory in the
2633 * physical address space, to identify and fix CRC errors.
2634 * The following loops writes to every DRAM address, setting the ECC bits to the
2635 * correct value. A read from this location will no longer return a CRC error,
2636 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002637 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002638 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2639 * and firmware running in x86_32.
2640 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002641 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2642 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002643 for (bank = 0; bank < 8; bank++) {
2644 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002645
Angel Pons8f0757e2020-11-11 23:03:36 +01002646 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2647 const struct iosav_ssq sequence[] = {
2648 /*
2649 * DRAM command ACT
2650 * Opens the row for writing.
2651 */
2652 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002653 .sp_cmd_ctrl = {
2654 .command = IOSAV_ACT,
2655 .ranksel_ap = 1,
2656 },
2657 .subseq_ctrl = {
2658 .cmd_executions = 1,
2659 .cmd_delay_gap = gap,
2660 .post_ssq_wait = ctrl->tRCD,
2661 .data_direction = SSQ_NA,
2662 },
2663 .sp_cmd_addr = {
2664 .address = row,
2665 .rowbits = 6,
2666 .bank = bank,
2667 .rank = slotrank,
2668 },
2669 .addr_update = {
2670 .inc_addr_1 = 1,
2671 .addr_wrap = 18,
2672 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002673 },
2674 /*
2675 * DRAM command WR
2676 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2677 * bytes.
2678 */
2679 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002680 .sp_cmd_ctrl = {
2681 .command = IOSAV_WR,
2682 .ranksel_ap = 1,
2683 },
2684 .subseq_ctrl = {
2685 .cmd_executions = 129,
2686 .cmd_delay_gap = 4,
2687 .post_ssq_wait = ctrl->tWTR +
2688 ctrl->CWL + 8,
2689 .data_direction = SSQ_WR,
2690 },
2691 .sp_cmd_addr = {
2692 .address = row,
2693 .rowbits = 0,
2694 .bank = bank,
2695 .rank = slotrank,
2696 },
2697 .addr_update = {
2698 .inc_addr_8 = 1,
2699 .addr_wrap = 9,
2700 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002701 },
2702 /*
2703 * DRAM command PRE
2704 * Closes the row.
2705 */
2706 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002707 .sp_cmd_ctrl = {
2708 .command = IOSAV_PRE,
2709 .ranksel_ap = 1,
2710 },
2711 .subseq_ctrl = {
2712 .cmd_executions = 1,
2713 .cmd_delay_gap = 4,
2714 .post_ssq_wait = ctrl->tRP,
2715 .data_direction = SSQ_NA,
2716 },
2717 .sp_cmd_addr = {
2718 .address = 0,
2719 .rowbits = 6,
2720 .bank = bank,
2721 .rank = slotrank,
2722 },
2723 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002724 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002725 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002726 },
2727 };
2728 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002729
2730 /* Execute command queue */
2731 iosav_run_queue(channel, 16, 0);
2732
2733 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002734 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002735 }
2736 }
2737}
2738
Angel Pons88521882020-01-05 20:21:20 +01002739void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002740{
2741 int channel;
2742
Angel Pons7c49cb82020-03-16 23:17:32 +01002743 /* 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 +01002744 static u32 seeds[NUM_CHANNELS][3] = {
2745 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2746 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2747 };
2748 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002749 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002750 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2751 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2752 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002753 }
2754}
2755
Angel Pons89ae6b82020-03-21 13:23:32 +01002756void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002757{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002758 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002759 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002760 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002761 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002762 }
2763}
2764
Angel Pons88521882020-01-05 20:21:20 +01002765void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002766{
2767 int channel;
2768
2769 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002770 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002771 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002772 }
2773
2774 udelay(1);
2775
2776 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002777 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002778 }
2779}
2780
Angel Pons7c49cb82020-03-16 23:17:32 +01002781void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002782{
Angel Pons11463322020-11-19 11:04:28 +01002783 /* Use a larger delay when running fast to improve stability */
2784 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2785
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002786 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002787
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002788 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002789 int min_pi = 10000;
2790 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002791
2792 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002793 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2794 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002795 }
2796
Angel Pons7a612742020-11-12 13:34:03 +01002797 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002798
Angel Pons7a612742020-11-12 13:34:03 +01002799 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002800
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002801 dram_odt_stretch(ctrl, channel);
2802
Angel Pons7a612742020-11-12 13:34:03 +01002803 const union tc_rwp_reg tc_rwp = {
2804 .tRRDR = 0,
2805 .tRRDD = val,
2806 .tWWDR = val,
2807 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002808 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002809 .tWRDRDD = tWRDRDD,
2810 .tRWSR = 2,
2811 .dec_wrd = 1,
2812 };
2813 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002814 }
2815}
2816
Angel Pons88521882020-01-05 20:21:20 +01002817void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002818{
2819 int channel;
2820 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002821 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2822 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002823 }
2824}
2825
Angel Pons7c49cb82020-03-16 23:17:32 +01002826/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2827static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002828{
Angel Pons88521882020-01-05 20:21:20 +01002829 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002830}
2831
Angel Pons7c49cb82020-03-16 23:17:32 +01002832/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002833void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002834{
2835 int channel;
2836 int t1_cycles = 0, t1_ns = 0, t2_ns;
2837 int t3_ns;
2838 u32 r32;
2839
Angel Pons7c49cb82020-03-16 23:17:32 +01002840 /* FIXME: This register only exists on Ivy Bridge */
2841 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002842
Angel Pons7a612742020-11-12 13:34:03 +01002843 FOR_ALL_CHANNELS {
2844 union tc_othp_reg tc_othp = {
2845 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2846 };
2847 tc_othp.tCPDED = 1;
2848 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2849 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002850
Angel Pons09fc4b92020-11-19 12:02:07 +01002851 /* 64 DCLKs until idle, decision per rank */
2852 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002853
Felix Heldf9b826a2018-07-30 17:56:52 +02002854 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002855 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002856
Angel Pons88521882020-01-05 20:21:20 +01002857 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
2858 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002859
2860 FOR_ALL_CHANNELS {
2861 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002862 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002863 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002864 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002865 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002866 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002867 case 1:
2868 case 4:
2869 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002870 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002871 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002872 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002873 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002874 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002875 break;
2876 }
2877 }
2878
Felix Held50b7ed22019-12-30 20:41:54 +01002879 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002880 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002881 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002882
Angel Pons7a612742020-11-12 13:34:03 +01002883 FOR_ALL_CHANNELS {
2884 union tc_rfp_reg tc_rfp = {
2885 .raw = MCHBAR32(TC_RFP_ch(channel)),
2886 };
2887 tc_rfp.refresh_2x_control = 1;
2888 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2889 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002890
Angel Ponsdc5539f2020-11-12 12:44:25 +01002891 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2892 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002893 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002894
Angel Pons7c49cb82020-03-16 23:17:32 +01002895 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002896 FOR_ALL_POPULATED_CHANNELS
2897 break;
2898
Angel Pons88521882020-01-05 20:21:20 +01002899 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2900 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002901 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002902 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002903 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002904 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002905 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002906 t1_ns += 500;
2907
Angel Pons88521882020-01-05 20:21:20 +01002908 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002909 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002910 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002911 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002912 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002913 t3_ns = 500;
2914 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002915
2916 /* The graphics driver will use these watermark values */
2917 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002918 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002919 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2920 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002921}
2922
Angel Pons88521882020-01-05 20:21:20 +01002923void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002924{
Angel Ponsc6742232020-11-15 13:26:21 +01002925 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002926
Angel Pons7c49cb82020-03-16 23:17:32 +01002927 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002928 const union tc_rap_reg tc_rap = {
2929 .tRRD = ctrl->tRRD,
2930 .tRTP = ctrl->tRTP,
2931 .tCKE = ctrl->tCKE,
2932 .tWTR = ctrl->tWTR,
2933 .tFAW = ctrl->tFAW,
2934 .tWR = ctrl->tWR,
2935 .tCMD = ctrl->cmd_stretch[channel],
2936 };
2937 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002938 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002939
2940 udelay(1);
2941
2942 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002943 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002944 }
2945
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002946 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002947 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002948
2949 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002950 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002951 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002952 }
2953
2954 printram("CPE\n");
2955
Angel Pons88521882020-01-05 20:21:20 +01002956 MCHBAR32(GDCRTRAININGMOD) = 0;
2957 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002958
2959 printram("CP5b\n");
2960
2961 FOR_ALL_POPULATED_CHANNELS {
2962 program_timings(ctrl, channel);
2963 }
2964
2965 u32 reg, addr;
2966
Angel Pons7c49cb82020-03-16 23:17:32 +01002967 /* Poll for RCOMP */
2968 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2969 ;
2970
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002971 do {
Angel Pons88521882020-01-05 20:21:20 +01002972 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002973 } while ((reg & 0x14) == 0);
2974
Angel Pons7c49cb82020-03-16 23:17:32 +01002975 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002976 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002977 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002978
Angel Pons7c49cb82020-03-16 23:17:32 +01002979 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002980 udelay(500);
2981
2982 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002983 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002984 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002985 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002986 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002987 MCHBAR32(addr) = reg;
2988
Angel Pons7c49cb82020-03-16 23:17:32 +01002989 /* Wait 10ns for ranks to settle */
2990 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002991
2992 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
2993 MCHBAR32(addr) = reg;
2994
Angel Pons7c49cb82020-03-16 23:17:32 +01002995 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002996 write_reset(ctrl);
2997 }
2998
Angel Pons7c49cb82020-03-16 23:17:32 +01002999 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003000 dram_mrscommands(ctrl);
3001
3002 printram("CP5c\n");
3003
Angel Pons88521882020-01-05 20:21:20 +01003004 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003005
3006 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003007 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003008 udelay(2);
3009 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003010}