blob: d18e302de0347440848b2651f4b8f5a34954c8fe [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,
178 .tPRPDEN = 2,
179 };
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 Pons88521882020-01-05 20:21:20 +0100691static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100692{
693 u16 mr0reg, mch_cas, mch_wr;
694 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Patrick Rudolph74203de2017-11-20 11:57:01 +0100695 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100696
Angel Pons7c49cb82020-03-16 23:17:32 +0100697 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100698 if (ctrl->CAS < 12) {
699 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
700 } else {
701 mch_cas = (u16) (ctrl->CAS - 12);
702 mch_cas = ((mch_cas << 1) | 0x1);
703 }
704
Angel Pons7c49cb82020-03-16 23:17:32 +0100705 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100706 mch_wr = mch_wr_t[ctrl->tWR - 5];
707
Angel Pons2bf28ed2020-11-12 13:49:59 +0100708 /* DLL Reset - self clearing - set after CLK frequency has been changed */
709 mr0reg = 1 << 8;
710
711 mr0reg |= (mch_cas & 0x1) << 2;
712 mr0reg |= (mch_cas & 0xe) << 3;
713 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100714
Angel Pons7c49cb82020-03-16 23:17:32 +0100715 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
Angel Pons2bf28ed2020-11-12 13:49:59 +0100716 mr0reg |= !is_mobile << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100717 return mr0reg;
718}
719
720static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
721{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200722 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100723}
724
Angel Ponsf9997482020-11-12 16:02:52 +0100725static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100726{
727 /* Get ODT based on rankmap */
728 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
729
730 if (dimms_per_ch == 1) {
731 return (const odtmap){60, 60};
732 } else {
733 return (const odtmap){120, 30};
734 }
735}
736
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100737static u32 encode_odt(u32 odt)
738{
739 switch (odt) {
740 case 30:
741 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
742 case 60:
743 return (1 << 2); // RZQ/4
744 case 120:
745 return (1 << 6); // RZQ/2
746 default:
747 case 0:
748 return 0;
749 }
750}
751
752static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
753{
754 odtmap odt;
755 u32 mr1reg;
756
Angel Ponsf9997482020-11-12 16:02:52 +0100757 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100758 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100759
760 mr1reg |= encode_odt(odt.rttnom);
761
762 return mr1reg;
763}
764
765static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
766{
767 u16 mr1reg;
768
769 mr1reg = make_mr1(ctrl, rank, channel);
770
771 write_mrreg(ctrl, channel, rank, 1, mr1reg);
772}
773
774static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
775{
Angel Pons868bca22020-11-13 13:38:04 +0100776 const u16 pasr = 0;
777 const u16 cwl = ctrl->CWL - 5;
778 const odtmap odt = get_ODT(ctrl, channel);
779
Angel Ponsdca3cb52020-11-13 13:42:07 +0100780 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100781 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
782 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100783
Angel Pons868bca22020-11-13 13:38:04 +0100784 u16 mr2reg = 0;
785 mr2reg |= pasr;
786 mr2reg |= cwl << 3;
787 mr2reg |= ctrl->auto_self_refresh << 6;
788 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100789 mr2reg |= (odt.rttwr / 60) << 9;
790
791 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100792
793 /* Program MR2 shadow */
794 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
795
796 reg32 &= 3 << 14 | 3 << 6;
797
798 reg32 |= mr2reg & ~(3 << 6);
799
800 if (rank & 1) {
801 if (srt)
802 reg32 |= 1 << (rank / 2 + 6);
803 } else {
804 if (ctrl->rank_mirror[channel][rank])
805 reg32 |= 1 << (rank / 2 + 14);
806 }
807 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100808}
809
810static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
811{
812 write_mrreg(ctrl, channel, rank, 3, 0);
813}
814
Angel Pons88521882020-01-05 20:21:20 +0100815void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100816{
817 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100818 int channel;
819
820 FOR_ALL_POPULATED_CHANNELS {
821 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100822 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100823 dram_mr2(ctrl, slotrank, channel);
824
Angel Pons7c49cb82020-03-16 23:17:32 +0100825 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100826 dram_mr3(ctrl, slotrank, channel);
827
Angel Pons7c49cb82020-03-16 23:17:32 +0100828 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100829 dram_mr1(ctrl, slotrank, channel);
830
Angel Pons7c49cb82020-03-16 23:17:32 +0100831 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100832 dram_mr0(ctrl, slotrank, channel);
833 }
834 }
835
Angel Pons8f0757e2020-11-11 23:03:36 +0100836 const struct iosav_ssq zqcl_sequence[] = {
837 /* DRAM command NOP (without ODT nor chip selects) */
838 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200839 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100840 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200841 },
842 .subseq_ctrl = {
843 .cmd_executions = 1,
844 .cmd_delay_gap = 4,
845 .post_ssq_wait = 15,
846 .data_direction = SSQ_NA,
847 },
848 .sp_cmd_addr = {
849 .address = 2,
850 .rowbits = 6,
851 .bank = 0,
852 .rank = 0,
853 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100854 },
855 /* DRAM command ZQCL */
856 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200857 .sp_cmd_ctrl = {
858 .command = IOSAV_ZQCS,
859 .ranksel_ap = 1,
860 },
861 .subseq_ctrl = {
862 .cmd_executions = 1,
863 .cmd_delay_gap = 4,
864 .post_ssq_wait = 400,
865 .data_direction = SSQ_NA,
866 },
867 .sp_cmd_addr = {
868 .address = 1024,
869 .rowbits = 6,
870 .bank = 0,
871 .rank = 0,
872 },
873 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100874 .inc_rank = 1,
875 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200876 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100877 },
878 };
879 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100880
Angel Pons7c49cb82020-03-16 23:17:32 +0100881 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200882 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100883
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100884 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100885 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100886 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100887 }
888
Angel Pons7c49cb82020-03-16 23:17:32 +0100889 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100890 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100891
892 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100893 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100894
Angel Pons88521882020-01-05 20:21:20 +0100895 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100896
897 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
898
Angel Pons7c49cb82020-03-16 23:17:32 +0100899 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100900 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901
Angel Ponsffd50152020-11-12 11:03:10 +0100902 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200903
Angel Pons7c49cb82020-03-16 23:17:32 +0100904 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200905 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100906
Angel Pons7c49cb82020-03-16 23:17:32 +0100907 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100908 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100909 }
910}
911
Felix Held3b906032020-01-14 17:05:43 +0100912static const u32 lane_base[] = {
913 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
914 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
915 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100916};
917
Angel Pons88521882020-01-05 20:21:20 +0100918void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100919{
Angel Pons88521882020-01-05 20:21:20 +0100920 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100921 int lane;
922 int slotrank, slot;
923 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100924 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100925
926 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100927 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
928 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100929 }
930
931 for (slot = 0; slot < NUM_SLOTS; slot++)
932 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
933 case 0:
934 default:
Angel Pons88521882020-01-05 20:21:20 +0100935 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100936 break;
937 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100938 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100939 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940 break;
941 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100942 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100943 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100944 break;
945 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100946 pi_coding_ctrl[slot] =
947 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100948 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100949 break;
950 }
951
Angel Pons7c49cb82020-03-16 23:17:32 +0100952 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100953 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100954 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
955 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100956 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100957 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
958
Angel Pons88521882020-01-05 20:21:20 +0100959 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100960
Angel Pons7c49cb82020-03-16 23:17:32 +0100961 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100962 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
963 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100964
965 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100966 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100967 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100968 if (shift < 0)
969 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100970
Angel Pons88521882020-01-05 20:21:20 +0100971 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100972
973 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100974 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
975 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100976 }
977
Angel Pons88521882020-01-05 20:21:20 +0100978 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
979 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100980
Angel Pons88521882020-01-05 20:21:20 +0100981 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100982 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100983
Angel Pons88521882020-01-05 20:21:20 +0100984 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100985
986 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100987 int post_timA_min_high = 7, pre_timA_min_high = 7;
988 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100989 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100990 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100991
992 if (shift < 0)
993 shift = 0;
994
995 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200996 post_timA_min_high = MIN(post_timA_min_high,
997 (ctrl->timings[channel][slotrank].lanes[lane].
998 timA + shift) >> 6);
999 pre_timA_min_high = MIN(pre_timA_min_high,
1000 ctrl->timings[channel][slotrank].lanes[lane].
1001 timA >> 6);
1002 post_timA_max_high = MAX(post_timA_max_high,
1003 (ctrl->timings[channel][slotrank].lanes[lane].
1004 timA + shift) >> 6);
1005 pre_timA_max_high = MAX(pre_timA_max_high,
1006 ctrl->timings[channel][slotrank].lanes[lane].
1007 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001008 }
1009
1010 if (pre_timA_max_high - pre_timA_min_high <
1011 post_timA_max_high - post_timA_min_high)
1012 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001013
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001014 else if (pre_timA_max_high - pre_timA_min_high >
1015 post_timA_max_high - post_timA_min_high)
1016 shift_402x = -1;
1017
Felix Helddee167e2019-12-30 17:30:16 +01001018 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001019 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001020 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001021
Angel Pons88521882020-01-05 20:21:20 +01001022 reg_roundtrip_latency |=
1023 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001024 shift_402x) << (8 * slotrank);
1025
1026 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001027 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001028 (((ctrl->timings[channel][slotrank].lanes[lane].
1029 timA + shift) & 0x3f)
1030 |
1031 ((ctrl->timings[channel][slotrank].lanes[lane].
1032 rising + shift) << 8)
1033 |
1034 (((ctrl->timings[channel][slotrank].lanes[lane].
1035 timA + shift -
1036 (post_timA_min_high << 6)) & 0x1c0) << 10)
1037 | ((ctrl->timings[channel][slotrank].lanes[lane].
1038 falling + shift) << 20));
1039
Felix Heldfb19c8a2020-01-14 21:27:59 +01001040 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001041 (((ctrl->timings[channel][slotrank].lanes[lane].
1042 timC + shift) & 0x3f)
1043 |
1044 (((ctrl->timings[channel][slotrank].lanes[lane].
1045 timB + shift) & 0x3f) << 8)
1046 |
1047 (((ctrl->timings[channel][slotrank].lanes[lane].
1048 timB + shift) & 0x1c0) << 9)
1049 |
1050 (((ctrl->timings[channel][slotrank].lanes[lane].
1051 timC + shift) & 0x40) << 13));
1052 }
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 Pons88521882020-01-05 20:21:20 +01001058static void test_timA(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{
1072 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001073
1074 return (MCHBAR32(lane_base[lane] +
1075 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 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{
1121 int timA;
1122 int statistics[NUM_LANES][128];
1123 int lane;
1124
1125 for (timA = 0; timA < 128; timA++) {
1126 FOR_ALL_LANES {
1127 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1128 }
1129 program_timings(ctrl, channel);
1130
1131 test_timA(ctrl, channel, slotrank);
1132
1133 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001134 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001135 }
1136 }
1137 FOR_ALL_LANES {
1138 struct run rn = get_longest_zero_run(statistics[lane], 128);
1139 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1140 upperA[lane] = rn.end;
1141 if (upperA[lane] < rn.middle)
1142 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001143
Patrick Rudolph368b6152016-11-25 16:36:52 +01001144 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001145 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001146 }
1147}
1148
Angel Ponsf3053392020-11-13 23:31:12 +01001149static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001150{
1151 int timA_delta;
1152 int statistics[NUM_LANES][51];
1153 int lane, i;
1154
1155 memset(statistics, 0, sizeof(statistics));
1156
1157 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001158
1159 FOR_ALL_LANES {
1160 ctrl->timings[channel][slotrank].lanes[lane].timA
1161 = upperA[lane] + timA_delta + 0x40;
1162 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001163 program_timings(ctrl, channel);
1164
1165 for (i = 0; i < 100; i++) {
1166 test_timA(ctrl, channel, slotrank);
1167 FOR_ALL_LANES {
1168 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001169 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001170 }
1171 }
1172 }
1173 FOR_ALL_LANES {
1174 int last_zero, first_all;
1175
1176 for (last_zero = -25; last_zero <= 25; last_zero++)
1177 if (statistics[lane][last_zero + 25])
1178 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001179
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001180 last_zero--;
1181 for (first_all = -25; first_all <= 25; first_all++)
1182 if (statistics[lane][first_all + 25] == 100)
1183 break;
1184
Angel Pons7c49cb82020-03-16 23:17:32 +01001185 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001186
1187 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001188 (last_zero + first_all) / 2 + upperA[lane];
1189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001191 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001192 }
1193}
1194
Angel Ponsf3053392020-11-13 23:31:12 +01001195static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001196{
1197 int works[NUM_LANES];
1198 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001199
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001200 while (1) {
1201 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 program_timings(ctrl, channel);
1204 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001205
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001206 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001207 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1208
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001209 if (works[lane])
1210 some_works = 1;
1211 else
1212 all_works = 0;
1213 }
1214 if (all_works)
1215 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001216
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001217 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001218 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001219 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1220 channel, slotrank);
1221 return MAKE_ERR;
1222 }
Angel Pons88521882020-01-05 20:21:20 +01001223 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 printram("4024 -= 2;\n");
1225 continue;
1226 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001227 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001228 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001229
Felix Heldef4fe3e2019-12-31 14:15:05 +01001230 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001231 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1232 channel, slotrank);
1233 return MAKE_ERR;
1234 }
1235 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001236 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001237 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001238 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001239 }
1240 }
1241 return 0;
1242}
1243
Angel Pons12bd8ab2020-11-13 23:10:52 +01001244static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001245{
1246 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001247 u16 logic_delay_min = 7;
1248 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001249
1250 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001251 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1252
1253 logic_delay_min = MIN(logic_delay_min, logic_delay);
1254 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001255 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001256
1257 if (logic_delay_max < logic_delay_min) {
1258 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1259 logic_delay_max, logic_delay_min, channel, slotrank);
1260 }
1261
1262 assert(logic_delay_max >= logic_delay_min);
1263
1264 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001265}
1266
Angel Pons12bd8ab2020-11-13 23:10:52 +01001267static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001268{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001269 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270
Angel Pons7c49cb82020-03-16 23:17:32 +01001271 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001272 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001273
Angel Pons12bd8ab2020-11-13 23:10:52 +01001274 if (prev < post)
1275 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001276
Angel Pons12bd8ab2020-11-13 23:10:52 +01001277 else if (prev > post)
1278 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001279
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001280 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001281 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001282
Angel Pons12bd8ab2020-11-13 23:10:52 +01001283 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1284 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1285 printram("4024 += %d;\n", latency_offset);
1286 printram("4028 += %d;\n", latency_offset);
1287
1288 return post;
1289}
1290
1291static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1292{
1293 u16 logic_delay_min = 7;
1294 int lane;
1295
1296 FOR_ALL_LANES {
1297 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1298
1299 logic_delay_min = MIN(logic_delay_min, logic_delay);
1300 }
1301
1302 if (logic_delay_min >= 2) {
1303 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1304 logic_delay_min, channel, slotrank);
1305 }
1306
1307 FOR_ALL_LANES {
1308 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1309 }
1310 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1311 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001312}
1313
Angel Pons7c49cb82020-03-16 23:17:32 +01001314/*
1315 * Compensate the skew between DQS and DQs.
1316 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001317 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1318 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001319 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001320 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001321 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001322 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1323 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1324 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001325 * DQ in phase the data being read is expected to alternate on every byte:
1326 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001327 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001328 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001329 * Once the controller has detected this pattern a bit in the result register is set for the
1330 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001331 */
Angel Pons88521882020-01-05 20:21:20 +01001332int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001333{
1334 int channel, slotrank, lane;
1335 int err;
1336
1337 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1338 int all_high, some_high;
1339 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001340 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001341
Angel Pons88521882020-01-05 20:21:20 +01001342 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001343
Angel Ponsffd50152020-11-12 11:03:10 +01001344 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001345
Angel Pons7c49cb82020-03-16 23:17:32 +01001346 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001347 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001348
Angel Pons88521882020-01-05 20:21:20 +01001349 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001350
Felix Heldef4fe3e2019-12-31 14:15:05 +01001351 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001352 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001353 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001354
Angel Ponsf3053392020-11-13 23:31:12 +01001355 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001356
Felix Held2bb3cdf2018-07-28 00:23:59 +02001357 all_high = 1;
1358 some_high = 0;
1359 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001360 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001361 some_high = 1;
1362 else
1363 all_high = 0;
1364 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001365
1366 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001367 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001368 printram("4028--;\n");
1369 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001370 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001371 upperA[lane] -= 0x40;
1372
1373 }
1374 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001375 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001376 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001377 printram("4024++;\n");
1378 printram("4028++;\n");
1379 }
1380
1381 program_timings(ctrl, channel);
1382
Angel Pons12bd8ab2020-11-13 23:10:52 +01001383 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Ponsf3053392020-11-13 23:31:12 +01001385 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386 if (err)
1387 return err;
1388
Angel Pons12bd8ab2020-11-13 23:10:52 +01001389 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390
Angel Ponsf3053392020-11-13 23:31:12 +01001391 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001392
Angel Pons12bd8ab2020-11-13 23:10:52 +01001393 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394
Angel Pons12bd8ab2020-11-13 23:10:52 +01001395 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396
Angel Pons12bd8ab2020-11-13 23:10:52 +01001397 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001398
1399 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001400 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001401 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001402
1403 printram("final results:\n");
1404 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001405 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001406 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001407
Angel Pons88521882020-01-05 20:21:20 +01001408 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001409
1410 toggle_io_reset();
1411 }
1412
1413 FOR_ALL_POPULATED_CHANNELS {
1414 program_timings(ctrl, channel);
1415 }
Angel Pons50a6fe72020-11-14 01:18:14 +01001416 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001417 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001418 }
1419 return 0;
1420}
1421
Angel Pons011661c2020-11-15 18:21:35 +01001422static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001423{
1424 int lane;
1425
1426 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001427 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1428 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001429 }
1430
Angel Pons88521882020-01-05 20:21:20 +01001431 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001432
Angel Ponsffd50152020-11-12 11:03:10 +01001433 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1434 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001435
Angel Pons7c49cb82020-03-16 23:17:32 +01001436 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001437 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001438
Angel Pons88521882020-01-05 20:21:20 +01001439 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001440
Angel Pons8f0757e2020-11-11 23:03:36 +01001441 const struct iosav_ssq rd_sequence[] = {
1442 /* DRAM command PREA */
1443 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001444 .sp_cmd_ctrl = {
1445 .command = IOSAV_PRE,
1446 .ranksel_ap = 1,
1447 },
1448 .subseq_ctrl = {
1449 .cmd_executions = 1,
1450 .cmd_delay_gap = 3,
1451 .post_ssq_wait = ctrl->tRP,
1452 .data_direction = SSQ_NA,
1453 },
1454 .sp_cmd_addr = {
1455 .address = 1024,
1456 .rowbits = 6,
1457 .bank = 0,
1458 .rank = slotrank,
1459 },
1460 .addr_update = {
1461 .addr_wrap = 18,
1462 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001463 },
1464 /* DRAM command ACT */
1465 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001466 .sp_cmd_ctrl = {
1467 .command = IOSAV_ACT,
1468 .ranksel_ap = 1,
1469 },
1470 .subseq_ctrl = {
1471 .cmd_executions = 8,
1472 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1473 .post_ssq_wait = ctrl->CAS,
1474 .data_direction = SSQ_NA,
1475 },
1476 .sp_cmd_addr = {
1477 .address = 0,
1478 .rowbits = 6,
1479 .bank = 0,
1480 .rank = slotrank,
1481 },
1482 .addr_update = {
1483 .inc_bank = 1,
1484 .addr_wrap = 18,
1485 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001486 },
1487 /* DRAM command RD */
1488 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001489 .sp_cmd_ctrl = {
1490 .command = IOSAV_RD,
1491 .ranksel_ap = 1,
1492 },
1493 .subseq_ctrl = {
1494 .cmd_executions = 500,
1495 .cmd_delay_gap = 4,
1496 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1497 .data_direction = SSQ_RD,
1498 },
1499 .sp_cmd_addr = {
1500 .address = 0,
1501 .rowbits = 0,
1502 .bank = 0,
1503 .rank = slotrank,
1504 },
1505 .addr_update = {
1506 .inc_addr_8 = 1,
1507 .addr_wrap = 18,
1508 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001509 },
1510 /* DRAM command PREA */
1511 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001512 .sp_cmd_ctrl = {
1513 .command = IOSAV_PRE,
1514 .ranksel_ap = 1,
1515 },
1516 .subseq_ctrl = {
1517 .cmd_executions = 1,
1518 .cmd_delay_gap = 3,
1519 .post_ssq_wait = ctrl->tRP,
1520 .data_direction = SSQ_NA,
1521 },
1522 .sp_cmd_addr = {
1523 .address = 1024,
1524 .rowbits = 6,
1525 .bank = 0,
1526 .rank = slotrank,
1527 },
1528 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01001529 .addr_wrap = 18,
Angel Pons3abd2062020-05-03 00:25:02 +02001530 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001531 },
1532 };
1533 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001534
Angel Pons7c49cb82020-03-16 23:17:32 +01001535 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001536 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001537
Angel Pons88521882020-01-05 20:21:20 +01001538 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001539}
1540
Angel Pons011661c2020-11-15 18:21:35 +01001541static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001542{
1543 int min = data[0];
1544 int max = min;
1545 int i;
1546 for (i = 1; i < count; i++) {
1547 if (min > data[i])
1548 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001549
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001550 if (max < data[i])
1551 max = data[i];
1552 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001553 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001554 for (i = 0; i < count; i++)
1555 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001556
Angel Pons891f2bc2020-01-10 01:27:28 +01001557 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001558}
1559
Angel Pons011661c2020-11-15 18:21:35 +01001560static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001561{
Angel Pons011661c2020-11-15 18:21:35 +01001562 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001563 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001564 int lane;
1565
Angel Pons88521882020-01-05 20:21:20 +01001566 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001567
Angel Ponsffd50152020-11-12 11:03:10 +01001568 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001569
Angel Pons7c49cb82020-03-16 23:17:32 +01001570 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001571 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001572
Angel Pons011661c2020-11-15 18:21:35 +01001573 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1574 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001575 program_timings(ctrl, channel);
1576
Angel Pons011661c2020-11-15 18:21:35 +01001577 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001578
1579 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001580 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001581 }
1582 }
1583 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001584 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1585
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001586 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001587 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1588 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001589 /*
1590 * With command training not being done yet, the lane can be erroneous.
1591 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001592 */
Angel Pons011661c2020-11-15 18:21:35 +01001593 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001594 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1595
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001596 if (rn.all || rn.length < 8) {
1597 printk(BIOS_EMERG, "timC recovery failed\n");
1598 return MAKE_ERR;
1599 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001600 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001601 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001602 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001603 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001604 }
1605 return 0;
1606}
1607
Angel Pons88521882020-01-05 20:21:20 +01001608static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001609{
1610 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001611
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001612 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1613 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001614
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001615 return ret;
1616}
1617
Angel Pons765d4652020-11-11 14:44:35 +01001618/* Each cacheline is 64 bits long */
1619static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1620{
1621 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1622}
1623
Angel Pons88521882020-01-05 20:21:20 +01001624static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001625{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301626 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001627 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001628
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001629 for (j = 0; j < 16; j++)
1630 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001631
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001632 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001633
1634 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001635}
1636
Angel Pons88521882020-01-05 20:21:20 +01001637static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001638{
1639 int ret = 0;
1640 int channel;
1641 FOR_ALL_POPULATED_CHANNELS ret++;
1642 return ret;
1643}
1644
Angel Pons88521882020-01-05 20:21:20 +01001645static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001646{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301647 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001648 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301649 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001650
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001651 for (j = 0; j < 16; j++)
1652 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001653
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001654 for (j = 0; j < 16; j++)
1655 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001656
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001658
1659 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001660}
1661
Angel Pons88521882020-01-05 20:21:20 +01001662static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663{
1664 int channel, slotrank, lane;
1665
1666 FOR_ALL_POPULATED_CHANNELS {
1667 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001668 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1669 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001670 }
1671
1672 program_timings(ctrl, channel);
1673
1674 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001675 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001676
Angel Ponsffd50152020-11-12 11:03:10 +01001677 iosav_write_read_mpr_sequence(
1678 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001679
Angel Pons7c49cb82020-03-16 23:17:32 +01001680 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001681 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001682
Angel Pons88521882020-01-05 20:21:20 +01001683 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001684 }
1685
1686 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001687 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1688 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001689 }
1690
1691 program_timings(ctrl, channel);
1692
1693 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001694 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001695
Angel Ponsffd50152020-11-12 11:03:10 +01001696 iosav_write_read_mpr_sequence(
1697 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001698
Angel Pons7c49cb82020-03-16 23:17:32 +01001699 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001700 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001701
Angel Pons88521882020-01-05 20:21:20 +01001702 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001703 }
1704 }
1705}
1706
Angel Pons88521882020-01-05 20:21:20 +01001707static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001708{
Angel Pons59996e02020-11-14 16:34:35 +01001709 /* First DQS/DQS# rising edge after write leveling mode is programmed */
1710 const u32 tWLMRD = 40;
1711
1712 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1713 int bank = 1;
1714
1715 if (ctrl->rank_mirror[channel][slotrank])
1716 ddr3_mirror_mrreg(&bank, &mr1reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001717
Angel Pons88521882020-01-05 20:21:20 +01001718 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001719
1720 const struct iosav_ssq sequence[] = {
Angel Pons59996e02020-11-14 16:34:35 +01001721 /* DRAM command MRS: enable DQs on this slotrank */
Angel Pons8f0757e2020-11-11 23:03:36 +01001722 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001723 .sp_cmd_ctrl = {
Angel Pons59996e02020-11-14 16:34:35 +01001724 .command = IOSAV_MRS,
1725 .ranksel_ap = 1,
1726 },
1727 .subseq_ctrl = {
1728 .cmd_executions = 1,
1729 .cmd_delay_gap = 3,
1730 .post_ssq_wait = tWLMRD,
1731 .data_direction = SSQ_NA,
1732 },
1733 .sp_cmd_addr = {
1734 .address = mr1reg,
1735 .rowbits = 6,
1736 .bank = bank,
1737 .rank = slotrank,
1738 },
1739 },
1740 /* DRAM command NOP */
1741 [1] = {
1742 .sp_cmd_ctrl = {
Angel Pons3abd2062020-05-03 00:25:02 +02001743 .command = IOSAV_NOP,
1744 .ranksel_ap = 1,
1745 },
1746 .subseq_ctrl = {
1747 .cmd_executions = 1,
1748 .cmd_delay_gap = 3,
1749 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1750 .data_direction = SSQ_WR,
1751 },
1752 .sp_cmd_addr = {
1753 .address = 8,
1754 .rowbits = 0,
1755 .bank = 0,
1756 .rank = slotrank,
1757 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001758 },
1759 /* DRAM command NOP */
Angel Pons59996e02020-11-14 16:34:35 +01001760 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001761 .sp_cmd_ctrl = {
1762 .command = IOSAV_NOP_ALT,
1763 .ranksel_ap = 1,
1764 },
1765 .subseq_ctrl = {
1766 .cmd_executions = 1,
1767 .cmd_delay_gap = 3,
1768 .post_ssq_wait = ctrl->CAS + 38,
1769 .data_direction = SSQ_RD,
1770 },
1771 .sp_cmd_addr = {
1772 .address = 4,
1773 .rowbits = 0,
1774 .bank = 0,
1775 .rank = slotrank,
1776 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001777 },
Angel Pons59996e02020-11-14 16:34:35 +01001778 /* DRAM command MRS: disable DQs on this slotrank */
1779 [3] = {
1780 .sp_cmd_ctrl = {
1781 .command = IOSAV_MRS,
1782 .ranksel_ap = 1,
1783 },
1784 .subseq_ctrl = {
1785 .cmd_executions = 1,
1786 .cmd_delay_gap = 3,
1787 .post_ssq_wait = ctrl->tMOD,
1788 .data_direction = SSQ_NA,
1789 },
1790 .sp_cmd_addr = {
1791 .address = mr1reg | 1 << 12,
1792 .rowbits = 6,
1793 .bank = bank,
1794 .rank = slotrank,
1795 },
1796 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001797 };
1798 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001799
Angel Pons7c49cb82020-03-16 23:17:32 +01001800 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001801 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001802
Angel Pons88521882020-01-05 20:21:20 +01001803 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001804}
1805
1806static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1807{
1808 int timB;
1809 int statistics[NUM_LANES][128];
1810 int lane;
1811
Angel Pons88521882020-01-05 20:21:20 +01001812 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813
1814 for (timB = 0; timB < 128; timB++) {
1815 FOR_ALL_LANES {
1816 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1817 }
1818 program_timings(ctrl, channel);
1819
1820 test_timB(ctrl, channel, slotrank);
1821
1822 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001823 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1824 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1825 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826 }
1827 }
1828 FOR_ALL_LANES {
1829 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001830 /*
1831 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1832 * of timB by a small value, which might cause the 6-bit value to overflow if
1833 * it's close to 0x3f. Increment the value by a small offset if it's likely
1834 * to overflow, to make sure it won't overflow while running tests and bricks
1835 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001837 * TODO: find out why some tests (edge write discovery) increment timB.
1838 */
1839 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001840 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001841 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001842 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001843
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001844 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1845 if (rn.all) {
1846 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1847 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001848
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001849 return MAKE_ERR;
1850 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001851 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1852 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001853 }
1854 return 0;
1855}
1856
1857static int get_timB_high_adjust(u64 val)
1858{
1859 int i;
1860
Angel Ponsbf13ef02020-11-11 18:40:06 +01001861 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001862 if (val == 0xffffffffffffffffLL)
1863 return 0;
1864
1865 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001866 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001867 for (i = 0; i < 8; i++)
1868 if (val << (8 * (7 - i) + 4))
1869 return -i;
1870 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001871 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001872 for (i = 0; i < 8; i++)
1873 if (val >> (8 * (7 - i) + 4))
1874 return i;
1875 }
1876 return 8;
1877}
1878
Angel Ponsbf13ef02020-11-11 18:40:06 +01001879static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001880{
1881 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001882 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001883 FOR_ALL_POPULATED_CHANNELS {
1884 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001885 }
1886 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1887
Angel Pons765d4652020-11-11 14:44:35 +01001888 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001889 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001890
Angel Pons88521882020-01-05 20:21:20 +01001891 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001892
Angel Ponsffd50152020-11-12 11:03:10 +01001893 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001894
Angel Pons7c49cb82020-03-16 23:17:32 +01001895 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001896 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001897
Angel Pons88521882020-01-05 20:21:20 +01001898 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001899
Angel Pons8f0757e2020-11-11 23:03:36 +01001900 const struct iosav_ssq rd_sequence[] = {
1901 /* DRAM command PREA */
1902 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001903 .sp_cmd_ctrl = {
1904 .command = IOSAV_PRE,
1905 .ranksel_ap = 1,
1906 },
1907 .subseq_ctrl = {
1908 .cmd_executions = 1,
1909 .cmd_delay_gap = 3,
1910 .post_ssq_wait = ctrl->tRP,
1911 .data_direction = SSQ_NA,
1912 },
1913 .sp_cmd_addr = {
1914 .address = 1024,
1915 .rowbits = 6,
1916 .bank = 0,
1917 .rank = slotrank,
1918 },
1919 .addr_update = {
1920 .addr_wrap = 18,
1921 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001922 },
1923 /* DRAM command ACT */
1924 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001925 .sp_cmd_ctrl = {
1926 .command = IOSAV_ACT,
1927 .ranksel_ap = 1,
1928 },
1929 .subseq_ctrl = {
1930 .cmd_executions = 1,
1931 .cmd_delay_gap = 3,
1932 .post_ssq_wait = ctrl->tRCD,
1933 .data_direction = SSQ_NA,
1934 },
1935 .sp_cmd_addr = {
1936 .address = 0,
1937 .rowbits = 6,
1938 .bank = 0,
1939 .rank = slotrank,
1940 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001941 },
1942 /* DRAM command RD */
1943 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001944 .sp_cmd_ctrl = {
1945 .command = IOSAV_RD,
1946 .ranksel_ap = 3,
1947 },
1948 .subseq_ctrl = {
1949 .cmd_executions = 1,
1950 .cmd_delay_gap = 3,
1951 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001952 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001953 ctrl->timings[channel][slotrank].io_latency,
1954 .data_direction = SSQ_RD,
1955 },
1956 .sp_cmd_addr = {
1957 .address = 8,
1958 .rowbits = 6,
1959 .bank = 0,
1960 .rank = slotrank,
1961 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001962 },
1963 };
1964 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001965
Angel Pons7c49cb82020-03-16 23:17:32 +01001966 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001967 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001968
Angel Pons88521882020-01-05 20:21:20 +01001969 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001971 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001972 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001973 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1975 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1976 get_timB_high_adjust(res) * 64;
1977
1978 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001979 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1980 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001981 }
1982 }
Angel Pons88521882020-01-05 20:21:20 +01001983 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001984}
1985
Angel Pons7d115132020-11-14 01:44:44 +01001986static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001987{
Angel Pons7d115132020-11-14 01:44:44 +01001988 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001989
Angel Pons7d115132020-11-14 01:44:44 +01001990 FOR_ALL_POPULATED_CHANNELS {
1991 /* choose an existing rank */
1992 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001993
Angel Pons7d115132020-11-14 01:44:44 +01001994 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001995
Angel Pons7d115132020-11-14 01:44:44 +01001996 /* Execute command queue */
1997 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001998
Angel Pons7d115132020-11-14 01:44:44 +01001999 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002000
Angel Pons7d115132020-11-14 01:44:44 +01002001 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
2002 }
2003
2004 /* Refresh disable */
2005 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
2006
2007 FOR_ALL_POPULATED_CHANNELS {
2008 /* Execute the same command queue */
2009 iosav_run_once(channel);
2010
2011 wait_for_iosav(channel);
2012 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002013}
2014
Angel Pons7c49cb82020-03-16 23:17:32 +01002015/*
2016 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002017 *
Angel Pons7c49cb82020-03-16 23:17:32 +01002018 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
2019 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
2020 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
2021 * CLK/ADDR/CMD signals have the same routing delay.
2022 *
2023 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
2024 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
2025 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002026 */
Angel Pons88521882020-01-05 20:21:20 +01002027int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002028{
2029 int channel, slotrank, lane;
2030 int err;
2031
2032 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002033 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002034
Angel Pons7d115132020-11-14 01:44:44 +01002035 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002036
Angel Pons7c49cb82020-03-16 23:17:32 +01002037 /* Enable write leveling on all ranks
2038 Disable all DQ outputs
2039 Only NOP is allowed in this mode */
2040 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2041 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01002042 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002043
Angel Pons88521882020-01-05 20:21:20 +01002044 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002045
2046 toggle_io_reset();
2047
Angel Pons7c49cb82020-03-16 23:17:32 +01002048 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002049 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2050 err = discover_timB(ctrl, channel, slotrank);
2051 if (err)
2052 return err;
2053 }
2054
Angel Pons7c49cb82020-03-16 23:17:32 +01002055 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002056 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01002057 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002058
Angel Pons88521882020-01-05 20:21:20 +01002059 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002060
2061 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002062 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002063
Angel Pons7c49cb82020-03-16 23:17:32 +01002064 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002065 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002066
2067 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002068 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01002069 MCHBAR32(IOSAV_STATUS_ch(channel));
2070 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002071
Angel Ponsffd50152020-11-12 11:03:10 +01002072 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002073
Angel Pons7c49cb82020-03-16 23:17:32 +01002074 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002075 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002076
Angel Pons88521882020-01-05 20:21:20 +01002077 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002078 }
2079
2080 toggle_io_reset();
2081
2082 printram("CPE\n");
2083 precharge(ctrl);
2084 printram("CPF\n");
2085
Angel Pons50a6fe72020-11-14 01:18:14 +01002086 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002087 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002088 }
2089
2090 FOR_ALL_POPULATED_CHANNELS {
2091 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002092 }
2093
2094 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01002095 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002096 if (err)
2097 return err;
2098 }
2099
2100 FOR_ALL_POPULATED_CHANNELS
2101 program_timings(ctrl, channel);
2102
2103 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002104 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002105
2106 FOR_ALL_POPULATED_CHANNELS
2107 program_timings(ctrl, channel);
2108
Angel Pons50a6fe72020-11-14 01:18:14 +01002109 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002110 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111 }
2112 return 0;
2113}
2114
Angel Ponsbf13ef02020-11-11 18:40:06 +01002115static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002116{
2117 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2118 int timC_delta;
2119 int lanes_ok = 0;
2120 int ctr = 0;
2121 int lane;
2122
2123 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2124 FOR_ALL_LANES {
2125 ctrl->timings[channel][slotrank].lanes[lane].timC =
2126 saved_rt.lanes[lane].timC + timC_delta;
2127 }
2128 program_timings(ctrl, channel);
2129 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002130 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002131 }
2132
Angel Pons765d4652020-11-11 14:44:35 +01002133 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002134 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002135
Angel Pons88521882020-01-05 20:21:20 +01002136 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002137
Angel Ponsffd50152020-11-12 11:03:10 +01002138 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002139
2140 /* Program LFSR for the RD/WR subsequences */
2141 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2142 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143
Angel Pons7c49cb82020-03-16 23:17:32 +01002144 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002145 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002146
Angel Pons88521882020-01-05 20:21:20 +01002147 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002148 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002149 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002150
2151 if (r32 == 0)
2152 lanes_ok |= 1 << lane;
2153 }
2154 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002155 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002156 break;
2157 }
2158
2159 ctrl->timings[channel][slotrank] = saved_rt;
2160
Patrick Rudolphdd662872017-10-28 18:20:11 +02002161 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002162}
2163
Angel Pons88521882020-01-05 20:21:20 +01002164static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002165{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302166 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002167 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2168 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002169
2170 if (patno) {
2171 u8 base8 = 0x80 >> ((patno - 1) % 8);
2172 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2173 for (i = 0; i < 32; i++) {
2174 for (j = 0; j < 16; j++) {
2175 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002176
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002177 if (invert[patno - 1][i] & (1 << (j / 2)))
2178 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002179
2180 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002181 }
2182 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002183 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002184 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2185 for (j = 0; j < 16; j++) {
2186 const u32 val = pattern[i][j];
2187 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2188 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002189 }
2190 sfence();
2191 }
Angel Pons765d4652020-11-11 14:44:35 +01002192
2193 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002194}
2195
Angel Pons88521882020-01-05 20:21:20 +01002196static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002197{
Angel Pons7d115132020-11-14 01:44:44 +01002198 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002199
Angel Pons7c49cb82020-03-16 23:17:32 +01002200 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002201 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002202
2203 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002204 dram_mrscommands(ctrl);
2205
2206 toggle_io_reset();
2207}
2208
Angel Ponsbf13ef02020-11-11 18:40:06 +01002209#define CT_MIN_PI -127
2210#define CT_MAX_PI 128
2211#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2212
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002213#define MIN_C320C_LEN 13
2214
2215static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2216{
2217 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2218 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002219 int command_pi;
2220 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002221 int delta = 0;
2222
2223 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2224
2225 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002226 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002227 }
2228
2229 ctrl->cmd_stretch[channel] = cmd_stretch;
2230
Angel Pons7a612742020-11-12 13:34:03 +01002231 const union tc_rap_reg tc_rap = {
2232 .tRRD = ctrl->tRRD,
2233 .tRTP = ctrl->tRTP,
2234 .tCKE = ctrl->tCKE,
2235 .tWTR = ctrl->tWTR,
2236 .tFAW = ctrl->tFAW,
2237 .tWR = ctrl->tWR,
2238 .tCMD = ctrl->cmd_stretch[channel],
2239 };
2240 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002241
2242 if (ctrl->cmd_stretch[channel] == 2)
2243 delta = 2;
2244 else if (ctrl->cmd_stretch[channel] == 0)
2245 delta = 4;
2246
2247 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002248 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002249 }
2250
Angel Ponsbf13ef02020-11-11 18:40:06 +01002251 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002252 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002253 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002254 }
2255 program_timings(ctrl, channel);
2256 reprogram_320c(ctrl);
2257 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002258 stat[slotrank][command_pi - CT_MIN_PI] =
2259 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002260 }
2261 }
2262 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002263 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002264
Angel Ponsbf13ef02020-11-11 18:40:06 +01002265 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002266 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2267 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002268
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002269 if (rn.all || rn.length < MIN_C320C_LEN) {
2270 FOR_ALL_POPULATED_RANKS {
2271 ctrl->timings[channel][slotrank] =
2272 saved_timings[channel][slotrank];
2273 }
2274 return MAKE_ERR;
2275 }
2276 }
2277
2278 return 0;
2279}
2280
Angel Pons7c49cb82020-03-16 23:17:32 +01002281/*
2282 * Adjust CMD phase shift and try multiple command rates.
2283 * A command rate of 2T doubles the time needed for address and command decode.
2284 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002285int command_training(ramctr_timing *ctrl)
2286{
2287 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002288
2289 FOR_ALL_POPULATED_CHANNELS {
2290 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002291 }
2292
2293 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002294 int cmdrate, err;
2295
2296 /*
2297 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002298 * Issue:
2299 * While c320c discovery seems to succeed raminit will fail in write training.
2300 *
2301 * Workaround:
2302 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2303 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002304 *
2305 * Single DIMM per channel:
2306 * Try command rate 1T and 2T
2307 */
2308 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002309 if (ctrl->tCMD)
2310 /* XMP gives the CMD rate in clock ticks, not ns */
2311 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002312
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002313 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002314 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2315
2316 if (!err)
2317 break;
2318 }
2319
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002320 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002321 printk(BIOS_EMERG, "c320c discovery failed\n");
2322 return err;
2323 }
2324
Angel Pons891f2bc2020-01-10 01:27:28 +01002325 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002326 }
2327
2328 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002329 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330
2331 reprogram_320c(ctrl);
2332 return 0;
2333}
2334
Angel Pons4c79f932020-11-14 01:26:52 +01002335static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336{
Angel Pons96a06dd2020-11-14 00:33:18 +01002337 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002338 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002339 int lane;
2340
Angel Pons96a06dd2020-11-14 00:33:18 +01002341 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002342 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002343 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2344 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002345 }
2346 program_timings(ctrl, channel);
2347
2348 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002349 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2350 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002351 }
2352
Angel Pons88521882020-01-05 20:21:20 +01002353 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002354
Angel Ponsffd50152020-11-12 11:03:10 +01002355 iosav_write_read_mpr_sequence(
2356 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002357
Angel Pons7c49cb82020-03-16 23:17:32 +01002358 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002359 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002360
Angel Pons88521882020-01-05 20:21:20 +01002361 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002362
2363 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002364 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002365 }
2366 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002367
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002368 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002369 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002370 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002371
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002372 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002373 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2374 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002375 return MAKE_ERR;
2376 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002377 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002378 }
2379 return 0;
2380}
2381
Angel Pons60971dc2020-11-14 00:49:38 +01002382static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2383{
2384 int slotrank, lane;
2385
2386 fill_pattern0(ctrl, channel, 0, 0);
2387 FOR_ALL_LANES {
2388 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2389 }
2390
2391 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2392 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2393 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2394 }
2395
2396 program_timings(ctrl, channel);
2397
2398 FOR_ALL_POPULATED_RANKS {
2399 wait_for_iosav(channel);
2400
2401 iosav_write_read_mpr_sequence(
2402 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2403
2404 /* Execute command queue */
2405 iosav_run_once(channel);
2406
2407 wait_for_iosav(channel);
2408 }
2409
2410 /* XXX: check any measured value ? */
2411
2412 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2413 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2414 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2415 }
2416
2417 program_timings(ctrl, channel);
2418
2419 FOR_ALL_POPULATED_RANKS {
2420 wait_for_iosav(channel);
2421
2422 iosav_write_read_mpr_sequence(
2423 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2424
2425 /* Execute command queue */
2426 iosav_run_once(channel);
2427
2428 wait_for_iosav(channel);
2429 }
2430
2431 /* XXX: check any measured value ? */
2432
2433 FOR_ALL_LANES {
2434 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2435 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2436 }
2437}
2438
Angel Pons4c79f932020-11-14 01:26:52 +01002439int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002440{
2441 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2442 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2443 int channel, slotrank, lane;
2444 int err;
2445
Angel Pons88521882020-01-05 20:21:20 +01002446 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002447
2448 toggle_io_reset();
2449
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002450 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002451 FOR_ALL_LANES {
Angel Pons60971dc2020-11-14 00:49:38 +01002452 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002453 }
2454
Angel Pons60971dc2020-11-14 00:49:38 +01002455 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002456
2457 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002458 }
2459
Angel Pons0c3936e2020-03-22 12:49:27 +01002460 /*
2461 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2462 * also use a single loop. It would seem that it is a debugging configuration.
2463 */
Angel Pons88521882020-01-05 20:21:20 +01002464 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2465 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002466
2467 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002468 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002469 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470 if (err)
2471 return err;
2472 }
2473
Angel Pons88521882020-01-05 20:21:20 +01002474 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2475 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002476
2477 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002478 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002479 rising_edges[channel][slotrank]);
2480 if (err)
2481 return err;
2482 }
2483
Angel Pons88521882020-01-05 20:21:20 +01002484 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002485
2486 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2487 ctrl->timings[channel][slotrank].lanes[lane].falling =
2488 falling_edges[channel][slotrank][lane];
2489 ctrl->timings[channel][slotrank].lanes[lane].rising =
2490 rising_edges[channel][slotrank][lane];
2491 }
2492
2493 FOR_ALL_POPULATED_CHANNELS {
2494 program_timings(ctrl, channel);
2495 }
2496
Angel Pons50a6fe72020-11-14 01:18:14 +01002497 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002498 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499 }
2500 return 0;
2501}
2502
Angel Pons7c49cb82020-03-16 23:17:32 +01002503static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002504{
2505 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002506 u32 raw_stats[MAX_EDGE_TIMING + 1];
2507 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002508 const int reg3000b24[] = { 0, 0xc, 0x2c };
2509 int lane, i;
2510 int lower[NUM_LANES];
2511 int upper[NUM_LANES];
2512 int pat;
2513
2514 FOR_ALL_LANES {
2515 lower[lane] = 0;
2516 upper[lane] = MAX_EDGE_TIMING;
2517 }
2518
2519 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002520 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002521 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2522
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002523 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2524 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002525 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002526
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002527 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2528 FOR_ALL_LANES {
2529 ctrl->timings[channel][slotrank].lanes[lane].
2530 rising = edge;
2531 ctrl->timings[channel][slotrank].lanes[lane].
2532 falling = edge;
2533 }
2534 program_timings(ctrl, channel);
2535
2536 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002537 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2538 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002539 }
Angel Pons88521882020-01-05 20:21:20 +01002540 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002541
Angel Ponsffd50152020-11-12 11:03:10 +01002542 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002543
Angel Pons7c49cb82020-03-16 23:17:32 +01002544 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002545 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002546
Angel Pons88521882020-01-05 20:21:20 +01002547 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002548 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002549 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002550 }
2551
Angel Pons7c49cb82020-03-16 23:17:32 +01002552 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002553 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002554 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002555
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002556 FOR_ALL_LANES {
2557 struct run rn;
2558 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002559 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2560
2561 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2562
2563 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2564 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2565 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002566 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002567
2568 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2569 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2570
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002571 edges[lane] = (lower[lane] + upper[lane]) / 2;
2572 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002573 printk(BIOS_EMERG, "edge write discovery failed: "
2574 "%d, %d, %d\n", channel, slotrank, lane);
2575
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002576 return MAKE_ERR;
2577 }
2578 }
2579 }
2580 }
2581
Angel Ponsa93f46e2020-11-17 16:54:01 +01002582 /* Restore nominal Vref after training */
2583 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002584 printram("CPA\n");
2585 return 0;
2586}
2587
2588int discover_edges_write(ramctr_timing *ctrl)
2589{
2590 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002591 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2592 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002593
Angel Pons7c49cb82020-03-16 23:17:32 +01002594 /*
2595 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2596 * also use a single loop. It would seem that it is a debugging configuration.
2597 */
Angel Pons88521882020-01-05 20:21:20 +01002598 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2599 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002600
2601 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2602 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002603 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002604 if (err)
2605 return err;
2606 }
2607
Angel Pons88521882020-01-05 20:21:20 +01002608 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2609 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002610
2611 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2612 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002613 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002614 if (err)
2615 return err;
2616 }
2617
Angel Pons88521882020-01-05 20:21:20 +01002618 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002619
2620 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2621 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002622 falling_edges[channel][slotrank][lane];
2623
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002624 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002625 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002626 }
2627
2628 FOR_ALL_POPULATED_CHANNELS
2629 program_timings(ctrl, channel);
2630
Angel Pons50a6fe72020-11-14 01:18:14 +01002631 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002632 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002633 }
2634 return 0;
2635}
2636
2637static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2638{
Angel Pons88521882020-01-05 20:21:20 +01002639 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002640
Angel Ponsffd50152020-11-12 11:03:10 +01002641 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002642
Angel Pons7c49cb82020-03-16 23:17:32 +01002643 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002644 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002645
Angel Pons88521882020-01-05 20:21:20 +01002646 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002647}
2648
2649int discover_timC_write(ramctr_timing *ctrl)
2650{
Angel Pons7c49cb82020-03-16 23:17:32 +01002651 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002652 int i, pat;
2653
2654 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2655 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2656 int channel, slotrank, lane;
2657
2658 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2659 lower[channel][slotrank][lane] = 0;
2660 upper[channel][slotrank][lane] = MAX_TIMC;
2661 }
2662
Angel Pons88521882020-01-05 20:21:20 +01002663 /*
2664 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2665 * FIXME: This must only be done on Ivy Bridge.
2666 */
2667 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002668 printram("discover timC write:\n");
2669
2670 for (i = 0; i < 3; i++)
2671 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002672
2673 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2674 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2675 ~0x3f000000, rege3c_b24[i] << 24);
2676
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002677 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002678
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002679 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2680 FOR_ALL_POPULATED_RANKS {
2681 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002682 u32 raw_stats[MAX_TIMC + 1];
2683 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002684
2685 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002686 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002687
2688 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002689
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002690 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002691 FOR_ALL_LANES {
2692 ctrl->timings[channel][slotrank]
2693 .lanes[lane].timC = timC;
2694 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002695 program_timings(ctrl, channel);
2696
2697 test_timC_write (ctrl, channel, slotrank);
2698
Angel Pons7c49cb82020-03-16 23:17:32 +01002699 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002700 raw_stats[timC] = MCHBAR32(
2701 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002702 }
2703 FOR_ALL_LANES {
2704 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002705 for (timC = 0; timC < MAX_TIMC; timC++) {
2706 stats[timC] = !!(raw_stats[timC]
2707 & (1 << lane));
2708 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002709
Angel Pons7c49cb82020-03-16 23:17:32 +01002710 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002711 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002712 printk(BIOS_EMERG,
2713 "timC write discovery failed: "
2714 "%d, %d, %d\n", channel,
2715 slotrank, lane);
2716
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002717 return MAKE_ERR;
2718 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002719 printram("timC: %d, %d, %d: "
2720 "0x%02x-0x%02x-0x%02x, "
2721 "0x%02x-0x%02x\n", channel, slotrank,
2722 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002723 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002724 rn.end - ctrl->timC_offset[i]);
2725
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002726 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002727 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002728 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002729
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002730 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002731 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002732 upper[channel][slotrank][lane]);
2733
2734 }
2735 }
2736 }
2737 }
2738
2739 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002740 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002741 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002742 udelay(2);
2743 }
2744
Angel Pons88521882020-01-05 20:21:20 +01002745 /*
2746 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2747 * FIXME: This must only be done on Ivy Bridge.
2748 */
2749 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002750
2751 printram("CPB\n");
2752
2753 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002754 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002755 (lower[channel][slotrank][lane] +
2756 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002757
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002758 ctrl->timings[channel][slotrank].lanes[lane].timC =
2759 (lower[channel][slotrank][lane] +
2760 upper[channel][slotrank][lane]) / 2;
2761 }
2762 FOR_ALL_POPULATED_CHANNELS {
2763 program_timings(ctrl, channel);
2764 }
2765 return 0;
2766}
2767
Angel Pons88521882020-01-05 20:21:20 +01002768void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002769{
2770 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002771 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002772
2773 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2774 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002775 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002776 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002777 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002778 printram("normalize %d, %d, %d: mat %d\n",
2779 channel, slotrank, lane, mat);
2780
Felix Heldef4fe3e2019-12-31 14:15:05 +01002781 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002782 printram("normalize %d, %d, %d: delta %d\n",
2783 channel, slotrank, lane, delta);
2784
Angel Pons88521882020-01-05 20:21:20 +01002785 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002786 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002787 }
2788
2789 FOR_ALL_POPULATED_CHANNELS {
2790 program_timings(ctrl, channel);
2791 }
2792}
2793
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002794int channel_test(ramctr_timing *ctrl)
2795{
2796 int channel, slotrank, lane;
2797
2798 slotrank = 0;
2799 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002800 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002801 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002802 return MAKE_ERR;
2803 }
2804 FOR_ALL_POPULATED_CHANNELS {
2805 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002806 }
2807
2808 for (slotrank = 0; slotrank < 4; slotrank++)
2809 FOR_ALL_CHANNELS
2810 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2811 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002812 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2813 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002814 }
Angel Pons88521882020-01-05 20:21:20 +01002815 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002816
Angel Ponsffd50152020-11-12 11:03:10 +01002817 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002818
Angel Pons7c49cb82020-03-16 23:17:32 +01002819 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002820 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002821
Angel Pons88521882020-01-05 20:21:20 +01002822 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002823 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002824 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002825 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2826 channel, slotrank, lane);
2827 return MAKE_ERR;
2828 }
2829 }
2830 return 0;
2831}
2832
Patrick Rudolphdd662872017-10-28 18:20:11 +02002833void channel_scrub(ramctr_timing *ctrl)
2834{
2835 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002836 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002837
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002838 FOR_ALL_POPULATED_CHANNELS {
2839 wait_for_iosav(channel);
2840 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002841 }
2842
2843 /*
2844 * During runtime the "scrubber" will periodically scan through the memory in the
2845 * physical address space, to identify and fix CRC errors.
2846 * The following loops writes to every DRAM address, setting the ECC bits to the
2847 * correct value. A read from this location will no longer return a CRC error,
2848 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002849 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002850 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2851 * and firmware running in x86_32.
2852 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002853 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2854 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002855 for (bank = 0; bank < 8; bank++) {
2856 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002857
Angel Pons8f0757e2020-11-11 23:03:36 +01002858 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2859 const struct iosav_ssq sequence[] = {
2860 /*
2861 * DRAM command ACT
2862 * Opens the row for writing.
2863 */
2864 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002865 .sp_cmd_ctrl = {
2866 .command = IOSAV_ACT,
2867 .ranksel_ap = 1,
2868 },
2869 .subseq_ctrl = {
2870 .cmd_executions = 1,
2871 .cmd_delay_gap = gap,
2872 .post_ssq_wait = ctrl->tRCD,
2873 .data_direction = SSQ_NA,
2874 },
2875 .sp_cmd_addr = {
2876 .address = row,
2877 .rowbits = 6,
2878 .bank = bank,
2879 .rank = slotrank,
2880 },
2881 .addr_update = {
2882 .inc_addr_1 = 1,
2883 .addr_wrap = 18,
2884 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002885 },
2886 /*
2887 * DRAM command WR
2888 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2889 * bytes.
2890 */
2891 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002892 .sp_cmd_ctrl = {
2893 .command = IOSAV_WR,
2894 .ranksel_ap = 1,
2895 },
2896 .subseq_ctrl = {
2897 .cmd_executions = 129,
2898 .cmd_delay_gap = 4,
2899 .post_ssq_wait = ctrl->tWTR +
2900 ctrl->CWL + 8,
2901 .data_direction = SSQ_WR,
2902 },
2903 .sp_cmd_addr = {
2904 .address = row,
2905 .rowbits = 0,
2906 .bank = bank,
2907 .rank = slotrank,
2908 },
2909 .addr_update = {
2910 .inc_addr_8 = 1,
2911 .addr_wrap = 9,
2912 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002913 },
2914 /*
2915 * DRAM command PRE
2916 * Closes the row.
2917 */
2918 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002919 .sp_cmd_ctrl = {
2920 .command = IOSAV_PRE,
2921 .ranksel_ap = 1,
2922 },
2923 .subseq_ctrl = {
2924 .cmd_executions = 1,
2925 .cmd_delay_gap = 4,
2926 .post_ssq_wait = ctrl->tRP,
2927 .data_direction = SSQ_NA,
2928 },
2929 .sp_cmd_addr = {
2930 .address = 0,
2931 .rowbits = 6,
2932 .bank = bank,
2933 .rank = slotrank,
2934 },
2935 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002936 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002937 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002938 },
2939 };
2940 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002941
2942 /* Execute command queue */
2943 iosav_run_queue(channel, 16, 0);
2944
2945 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002946 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002947 }
2948 }
2949}
2950
Angel Pons88521882020-01-05 20:21:20 +01002951void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002952{
2953 int channel;
2954
Angel Pons7c49cb82020-03-16 23:17:32 +01002955 /* 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 +01002956 static u32 seeds[NUM_CHANNELS][3] = {
2957 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2958 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2959 };
2960 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002961 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002962 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2963 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2964 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002965 }
2966}
2967
Angel Pons89ae6b82020-03-21 13:23:32 +01002968void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002969{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002970 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002971 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002973 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002974 }
2975}
2976
Angel Pons88521882020-01-05 20:21:20 +01002977void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002978{
2979 int channel;
2980
2981 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002982 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002983 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002984 }
2985
2986 udelay(1);
2987
2988 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002989 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002990 }
2991}
2992
Angel Pons7c49cb82020-03-16 23:17:32 +01002993void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002994{
2995 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002996
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002997 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002998 int min_pi = 10000;
2999 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003000
3001 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01003002 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
3003 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003004 }
3005
Angel Pons7a612742020-11-12 13:34:03 +01003006 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003007
Angel Pons7a612742020-11-12 13:34:03 +01003008 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003009
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01003010 dram_odt_stretch(ctrl, channel);
3011
Angel Pons7a612742020-11-12 13:34:03 +01003012 const union tc_rwp_reg tc_rwp = {
3013 .tRRDR = 0,
3014 .tRRDD = val,
3015 .tWWDR = val,
3016 .tWWDD = val,
3017 .tRWDRDD = ctrl->ref_card_offset[channel] + 2,
3018 .tWRDRDD = tWRDRDD,
3019 .tRWSR = 2,
3020 .dec_wrd = 1,
3021 };
3022 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003023 }
3024}
3025
Angel Pons88521882020-01-05 20:21:20 +01003026void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003027{
3028 int channel;
3029 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01003030 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
3031 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003032 }
3033}
3034
Angel Pons7c49cb82020-03-16 23:17:32 +01003035/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
3036static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003037{
Angel Pons88521882020-01-05 20:21:20 +01003038 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003039}
3040
Angel Pons7c49cb82020-03-16 23:17:32 +01003041/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01003042void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003043{
Angel Ponsb50ca572020-11-11 19:07:20 +01003044 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01003045
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003046 int channel;
3047 int t1_cycles = 0, t1_ns = 0, t2_ns;
3048 int t3_ns;
3049 u32 r32;
3050
Angel Pons7c49cb82020-03-16 23:17:32 +01003051 /* FIXME: This register only exists on Ivy Bridge */
3052 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003053
Angel Pons7a612742020-11-12 13:34:03 +01003054 FOR_ALL_CHANNELS {
3055 union tc_othp_reg tc_othp = {
3056 .raw = MCHBAR32(TC_OTHP_ch(channel)),
3057 };
3058 tc_othp.tCPDED = 1;
3059 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
3060 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01003061
Patrick Rudolph74203de2017-11-20 11:57:01 +01003062 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003063 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003064 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003065 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003066 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003067 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003068
Felix Heldf9b826a2018-07-30 17:56:52 +02003069 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003070 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003071
Angel Pons88521882020-01-05 20:21:20 +01003072 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3073 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003074
3075 FOR_ALL_CHANNELS {
3076 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003077 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003078 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003079 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003080 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003081 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003082 case 1:
3083 case 4:
3084 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003085 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003086 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003087 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003088 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003089 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003090 break;
3091 }
3092 }
3093
Felix Held50b7ed22019-12-30 20:41:54 +01003094 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003095 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003096 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003097
Angel Pons7a612742020-11-12 13:34:03 +01003098 FOR_ALL_CHANNELS {
3099 union tc_rfp_reg tc_rfp = {
3100 .raw = MCHBAR32(TC_RFP_ch(channel)),
3101 };
3102 tc_rfp.refresh_2x_control = 1;
3103 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
3104 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003105
Angel Ponsdc5539f2020-11-12 12:44:25 +01003106 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3107 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003108 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003109
Angel Pons7c49cb82020-03-16 23:17:32 +01003110 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003111 FOR_ALL_POPULATED_CHANNELS
3112 break;
3113
Angel Pons88521882020-01-05 20:21:20 +01003114 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3115 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003116 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003117 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003118 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003119 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003120 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003121 t1_ns += 500;
3122
Angel Pons88521882020-01-05 20:21:20 +01003123 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003124 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003125 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003126 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003127 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003128 t3_ns = 500;
3129 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003130
3131 /* The graphics driver will use these watermark values */
3132 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003133 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003134 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3135 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003136}
3137
Angel Pons88521882020-01-05 20:21:20 +01003138void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003139{
Angel Pons50a6fe72020-11-14 01:18:14 +01003140 int channel, lane;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003141
Angel Pons7c49cb82020-03-16 23:17:32 +01003142 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01003143 const union tc_rap_reg tc_rap = {
3144 .tRRD = ctrl->tRRD,
3145 .tRTP = ctrl->tRTP,
3146 .tCKE = ctrl->tCKE,
3147 .tWTR = ctrl->tWTR,
3148 .tFAW = ctrl->tFAW,
3149 .tWR = ctrl->tWR,
3150 .tCMD = ctrl->cmd_stretch[channel],
3151 };
3152 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01003153 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003154
3155 udelay(1);
3156
3157 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003158 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003159 }
3160
Angel Pons50a6fe72020-11-14 01:18:14 +01003161 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003162 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003163 }
3164
3165 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003166 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003167
3168 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003169 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003170 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003171 }
3172
3173 printram("CPE\n");
3174
Angel Pons88521882020-01-05 20:21:20 +01003175 MCHBAR32(GDCRTRAININGMOD) = 0;
3176 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003177
3178 printram("CP5b\n");
3179
3180 FOR_ALL_POPULATED_CHANNELS {
3181 program_timings(ctrl, channel);
3182 }
3183
3184 u32 reg, addr;
3185
Angel Pons7c49cb82020-03-16 23:17:32 +01003186 /* Poll for RCOMP */
3187 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3188 ;
3189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003190 do {
Angel Pons88521882020-01-05 20:21:20 +01003191 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003192 } while ((reg & 0x14) == 0);
3193
Angel Pons7c49cb82020-03-16 23:17:32 +01003194 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003195 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003196 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003197
Angel Pons7c49cb82020-03-16 23:17:32 +01003198 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003199 udelay(500);
3200
3201 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003202 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003203 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003204 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003205 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003206 MCHBAR32(addr) = reg;
3207
Angel Pons7c49cb82020-03-16 23:17:32 +01003208 /* Wait 10ns for ranks to settle */
3209 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003210
3211 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3212 MCHBAR32(addr) = reg;
3213
Angel Pons7c49cb82020-03-16 23:17:32 +01003214 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003215 write_reset(ctrl);
3216 }
3217
Angel Pons7c49cb82020-03-16 23:17:32 +01003218 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003219 dram_mrscommands(ctrl);
3220
3221 printram("CP5c\n");
3222
Angel Pons88521882020-01-05 20:21:20 +01003223 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003224
3225 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003226 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003227 udelay(2);
3228 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003229}