blob: ef4ae455f8180611551f8bbbecf00871d2fa1dc7 [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 Pons737f1112020-11-13 14:07:30 +0100920 u32 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 */
Angel Pons737f1112020-11-13 14:07:30 +0100953 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
954 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
955 };
956 cmd_pi_coding.cmd_pi_code = full_shift & 0x3f;
957 cmd_pi_coding.cmd_logic_delay = !!(full_shift & 0x40);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100958
Angel Pons737f1112020-11-13 14:07:30 +0100959 cmd_pi_coding.ctl_pi_code_d0 = pi_coding_ctrl[0] & 0x3f;
960 cmd_pi_coding.ctl_pi_code_d1 = pi_coding_ctrl[1] & 0x3f;
961 cmd_pi_coding.ctl_logic_delay_d0 = !!(pi_coding_ctrl[0] & 0x40);
962 cmd_pi_coding.ctl_logic_delay_d1 = !!(pi_coding_ctrl[1] & 0x40);
963
964 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100965
Angel Pons7c49cb82020-03-16 23:17:32 +0100966 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100967 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
968 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100969
970 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100971 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100972 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100973 if (shift < 0)
974 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100975
Angel Pons88521882020-01-05 20:21:20 +0100976 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100977
978 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100979 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
980 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100981 }
982
Angel Pons88521882020-01-05 20:21:20 +0100983 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
984 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100985
Angel Pons88521882020-01-05 20:21:20 +0100986 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100987 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100988
Angel Pons88521882020-01-05 20:21:20 +0100989 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100990
991 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100992 int post_timA_min_high = 7, pre_timA_min_high = 7;
993 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100994 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100995 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100996
997 if (shift < 0)
998 shift = 0;
999
1000 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +02001001 post_timA_min_high = MIN(post_timA_min_high,
1002 (ctrl->timings[channel][slotrank].lanes[lane].
1003 timA + shift) >> 6);
1004 pre_timA_min_high = MIN(pre_timA_min_high,
1005 ctrl->timings[channel][slotrank].lanes[lane].
1006 timA >> 6);
1007 post_timA_max_high = MAX(post_timA_max_high,
1008 (ctrl->timings[channel][slotrank].lanes[lane].
1009 timA + shift) >> 6);
1010 pre_timA_max_high = MAX(pre_timA_max_high,
1011 ctrl->timings[channel][slotrank].lanes[lane].
1012 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001013 }
1014
1015 if (pre_timA_max_high - pre_timA_min_high <
1016 post_timA_max_high - post_timA_min_high)
1017 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001018
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001019 else if (pre_timA_max_high - pre_timA_min_high >
1020 post_timA_max_high - post_timA_min_high)
1021 shift_402x = -1;
1022
Felix Helddee167e2019-12-30 17:30:16 +01001023 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001024 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001025 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001026
Angel Pons88521882020-01-05 20:21:20 +01001027 reg_roundtrip_latency |=
1028 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001029 shift_402x) << (8 * slotrank);
1030
1031 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001032 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001033 (((ctrl->timings[channel][slotrank].lanes[lane].
1034 timA + shift) & 0x3f)
1035 |
1036 ((ctrl->timings[channel][slotrank].lanes[lane].
1037 rising + shift) << 8)
1038 |
1039 (((ctrl->timings[channel][slotrank].lanes[lane].
1040 timA + shift -
1041 (post_timA_min_high << 6)) & 0x1c0) << 10)
1042 | ((ctrl->timings[channel][slotrank].lanes[lane].
1043 falling + shift) << 20));
1044
Felix Heldfb19c8a2020-01-14 21:27:59 +01001045 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001046 (((ctrl->timings[channel][slotrank].lanes[lane].
1047 timC + shift) & 0x3f)
1048 |
1049 (((ctrl->timings[channel][slotrank].lanes[lane].
1050 timB + shift) & 0x3f) << 8)
1051 |
1052 (((ctrl->timings[channel][slotrank].lanes[lane].
1053 timB + shift) & 0x1c0) << 9)
1054 |
1055 (((ctrl->timings[channel][slotrank].lanes[lane].
1056 timC + shift) & 0x40) << 13));
1057 }
1058 }
Angel Pons88521882020-01-05 20:21:20 +01001059 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1060 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001061}
1062
Angel Pons88521882020-01-05 20:21:20 +01001063static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001064{
Angel Pons88521882020-01-05 20:21:20 +01001065 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001066
Angel Ponsffd50152020-11-12 11:03:10 +01001067 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001068
Angel Pons7c49cb82020-03-16 23:17:32 +01001069 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001070 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001071
Angel Pons88521882020-01-05 20:21:20 +01001072 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001073}
1074
Angel Pons7c49cb82020-03-16 23:17:32 +01001075static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001076{
1077 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001078
1079 return (MCHBAR32(lane_base[lane] +
1080 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001081}
1082
1083struct run {
1084 int middle;
1085 int end;
1086 int start;
1087 int all;
1088 int length;
1089};
1090
1091static struct run get_longest_zero_run(int *seq, int sz)
1092{
1093 int i, ls;
1094 int bl = 0, bs = 0;
1095 struct run ret;
1096
1097 ls = 0;
1098 for (i = 0; i < 2 * sz; i++)
1099 if (seq[i % sz]) {
1100 if (i - ls > bl) {
1101 bl = i - ls;
1102 bs = ls;
1103 }
1104 ls = i + 1;
1105 }
1106 if (bl == 0) {
1107 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001108 ret.start = 0;
1109 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001110 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001111 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001112 return ret;
1113 }
1114
Angel Pons7c49cb82020-03-16 23:17:32 +01001115 ret.start = bs % sz;
1116 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001117 ret.middle = (bs + (bl - 1) / 2) % sz;
1118 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001119 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001120
1121 return ret;
1122}
1123
Angel Ponsf3053392020-11-13 23:31:12 +01001124static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001125{
1126 int timA;
1127 int statistics[NUM_LANES][128];
1128 int lane;
1129
1130 for (timA = 0; timA < 128; timA++) {
1131 FOR_ALL_LANES {
1132 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1133 }
1134 program_timings(ctrl, channel);
1135
1136 test_timA(ctrl, channel, slotrank);
1137
1138 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001139 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001140 }
1141 }
1142 FOR_ALL_LANES {
1143 struct run rn = get_longest_zero_run(statistics[lane], 128);
1144 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1145 upperA[lane] = rn.end;
1146 if (upperA[lane] < rn.middle)
1147 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001148
Patrick Rudolph368b6152016-11-25 16:36:52 +01001149 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001150 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001151 }
1152}
1153
Angel Ponsf3053392020-11-13 23:31:12 +01001154static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001155{
1156 int timA_delta;
1157 int statistics[NUM_LANES][51];
1158 int lane, i;
1159
1160 memset(statistics, 0, sizeof(statistics));
1161
1162 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001163
1164 FOR_ALL_LANES {
1165 ctrl->timings[channel][slotrank].lanes[lane].timA
1166 = upperA[lane] + timA_delta + 0x40;
1167 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001168 program_timings(ctrl, channel);
1169
1170 for (i = 0; i < 100; i++) {
1171 test_timA(ctrl, channel, slotrank);
1172 FOR_ALL_LANES {
1173 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001174 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001175 }
1176 }
1177 }
1178 FOR_ALL_LANES {
1179 int last_zero, first_all;
1180
1181 for (last_zero = -25; last_zero <= 25; last_zero++)
1182 if (statistics[lane][last_zero + 25])
1183 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001184
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001185 last_zero--;
1186 for (first_all = -25; first_all <= 25; first_all++)
1187 if (statistics[lane][first_all + 25] == 100)
1188 break;
1189
Angel Pons7c49cb82020-03-16 23:17:32 +01001190 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001191
1192 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001193 (last_zero + first_all) / 2 + upperA[lane];
1194
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001195 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001196 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001197 }
1198}
1199
Angel Ponsf3053392020-11-13 23:31:12 +01001200static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001201{
1202 int works[NUM_LANES];
1203 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001204
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001205 while (1) {
1206 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001207
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001208 program_timings(ctrl, channel);
1209 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001211 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001212 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1213
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 if (works[lane])
1215 some_works = 1;
1216 else
1217 all_works = 0;
1218 }
1219 if (all_works)
1220 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001221
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001222 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001223 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1225 channel, slotrank);
1226 return MAKE_ERR;
1227 }
Angel Pons88521882020-01-05 20:21:20 +01001228 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 printram("4024 -= 2;\n");
1230 continue;
1231 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001232 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001233 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001234
Felix Heldef4fe3e2019-12-31 14:15:05 +01001235 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001236 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1237 channel, slotrank);
1238 return MAKE_ERR;
1239 }
1240 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001241 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001243 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001244 }
1245 }
1246 return 0;
1247}
1248
Angel Pons12bd8ab2020-11-13 23:10:52 +01001249static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001250{
1251 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001252 u16 logic_delay_min = 7;
1253 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001254
1255 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001256 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1257
1258 logic_delay_min = MIN(logic_delay_min, logic_delay);
1259 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001260 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001261
1262 if (logic_delay_max < logic_delay_min) {
1263 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1264 logic_delay_max, logic_delay_min, channel, slotrank);
1265 }
1266
1267 assert(logic_delay_max >= logic_delay_min);
1268
1269 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270}
1271
Angel Pons12bd8ab2020-11-13 23:10:52 +01001272static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001273{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001274 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001275
Angel Pons7c49cb82020-03-16 23:17:32 +01001276 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001277 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001278
Angel Pons12bd8ab2020-11-13 23:10:52 +01001279 if (prev < post)
1280 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001281
Angel Pons12bd8ab2020-11-13 23:10:52 +01001282 else if (prev > post)
1283 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001284
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001285 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001286 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001287
Angel Pons12bd8ab2020-11-13 23:10:52 +01001288 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1289 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1290 printram("4024 += %d;\n", latency_offset);
1291 printram("4028 += %d;\n", latency_offset);
1292
1293 return post;
1294}
1295
1296static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1297{
1298 u16 logic_delay_min = 7;
1299 int lane;
1300
1301 FOR_ALL_LANES {
1302 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1303
1304 logic_delay_min = MIN(logic_delay_min, logic_delay);
1305 }
1306
1307 if (logic_delay_min >= 2) {
1308 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1309 logic_delay_min, channel, slotrank);
1310 }
1311
1312 FOR_ALL_LANES {
1313 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1314 }
1315 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1316 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001317}
1318
Angel Pons7f5a97c2020-11-13 16:58:46 +01001319int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001320{
1321 int channel, slotrank, lane;
1322 int err;
1323
1324 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1325 int all_high, some_high;
1326 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001327 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001328
Angel Pons88521882020-01-05 20:21:20 +01001329 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001330
Angel Ponsffd50152020-11-12 11:03:10 +01001331 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001332
Angel Pons7c49cb82020-03-16 23:17:32 +01001333 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001334 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001335
Angel Pons58b609b2020-11-13 14:35:29 +01001336 const union gdcr_training_mod_reg training_mod = {
1337 .receive_enable_mode = 1,
1338 .training_rank_sel = slotrank,
1339 .odt_always_on = 1,
1340 };
1341 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001342
Felix Heldef4fe3e2019-12-31 14:15:05 +01001343 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001344 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001345 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001346
Angel Ponsf3053392020-11-13 23:31:12 +01001347 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001348
Felix Held2bb3cdf2018-07-28 00:23:59 +02001349 all_high = 1;
1350 some_high = 0;
1351 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001352 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001353 some_high = 1;
1354 else
1355 all_high = 0;
1356 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001357
1358 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001359 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001360 printram("4028--;\n");
1361 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001362 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001363 upperA[lane] -= 0x40;
1364
1365 }
1366 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001367 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001368 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001369 printram("4024++;\n");
1370 printram("4028++;\n");
1371 }
1372
1373 program_timings(ctrl, channel);
1374
Angel Pons12bd8ab2020-11-13 23:10:52 +01001375 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001376
Angel Ponsf3053392020-11-13 23:31:12 +01001377 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001378 if (err)
1379 return err;
1380
Angel Pons12bd8ab2020-11-13 23:10:52 +01001381 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
Angel Ponsf3053392020-11-13 23:31:12 +01001383 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Pons12bd8ab2020-11-13 23:10:52 +01001385 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386
Angel Pons12bd8ab2020-11-13 23:10:52 +01001387 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388
Angel Pons12bd8ab2020-11-13 23:10:52 +01001389 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390
1391 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001392 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001393 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394
1395 printram("final results:\n");
1396 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001397 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001398 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001399
Angel Pons88521882020-01-05 20:21:20 +01001400 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001401
1402 toggle_io_reset();
1403 }
1404
1405 FOR_ALL_POPULATED_CHANNELS {
1406 program_timings(ctrl, channel);
1407 }
Angel Pons50a6fe72020-11-14 01:18:14 +01001408 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001409 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001410 }
1411 return 0;
1412}
1413
Angel Pons011661c2020-11-15 18:21:35 +01001414static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001415{
1416 int lane;
1417
1418 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001419 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1420 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001421 }
1422
Angel Pons88521882020-01-05 20:21:20 +01001423 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001424
Angel Ponsffd50152020-11-12 11:03:10 +01001425 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1426 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001427
Angel Pons7c49cb82020-03-16 23:17:32 +01001428 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001429 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001430
Angel Pons88521882020-01-05 20:21:20 +01001431 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001432
Angel Pons8f0757e2020-11-11 23:03:36 +01001433 const struct iosav_ssq rd_sequence[] = {
1434 /* DRAM command PREA */
1435 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001436 .sp_cmd_ctrl = {
1437 .command = IOSAV_PRE,
1438 .ranksel_ap = 1,
1439 },
1440 .subseq_ctrl = {
1441 .cmd_executions = 1,
1442 .cmd_delay_gap = 3,
1443 .post_ssq_wait = ctrl->tRP,
1444 .data_direction = SSQ_NA,
1445 },
1446 .sp_cmd_addr = {
1447 .address = 1024,
1448 .rowbits = 6,
1449 .bank = 0,
1450 .rank = slotrank,
1451 },
1452 .addr_update = {
1453 .addr_wrap = 18,
1454 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001455 },
1456 /* DRAM command ACT */
1457 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001458 .sp_cmd_ctrl = {
1459 .command = IOSAV_ACT,
1460 .ranksel_ap = 1,
1461 },
1462 .subseq_ctrl = {
1463 .cmd_executions = 8,
1464 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1465 .post_ssq_wait = ctrl->CAS,
1466 .data_direction = SSQ_NA,
1467 },
1468 .sp_cmd_addr = {
1469 .address = 0,
1470 .rowbits = 6,
1471 .bank = 0,
1472 .rank = slotrank,
1473 },
1474 .addr_update = {
1475 .inc_bank = 1,
1476 .addr_wrap = 18,
1477 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001478 },
1479 /* DRAM command RD */
1480 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001481 .sp_cmd_ctrl = {
1482 .command = IOSAV_RD,
1483 .ranksel_ap = 1,
1484 },
1485 .subseq_ctrl = {
1486 .cmd_executions = 500,
1487 .cmd_delay_gap = 4,
1488 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1489 .data_direction = SSQ_RD,
1490 },
1491 .sp_cmd_addr = {
1492 .address = 0,
1493 .rowbits = 0,
1494 .bank = 0,
1495 .rank = slotrank,
1496 },
1497 .addr_update = {
1498 .inc_addr_8 = 1,
1499 .addr_wrap = 18,
1500 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001501 },
1502 /* DRAM command PREA */
1503 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001504 .sp_cmd_ctrl = {
1505 .command = IOSAV_PRE,
1506 .ranksel_ap = 1,
1507 },
1508 .subseq_ctrl = {
1509 .cmd_executions = 1,
1510 .cmd_delay_gap = 3,
1511 .post_ssq_wait = ctrl->tRP,
1512 .data_direction = SSQ_NA,
1513 },
1514 .sp_cmd_addr = {
1515 .address = 1024,
1516 .rowbits = 6,
1517 .bank = 0,
1518 .rank = slotrank,
1519 },
1520 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01001521 .addr_wrap = 18,
Angel Pons3abd2062020-05-03 00:25:02 +02001522 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001523 },
1524 };
1525 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001526
Angel Pons7c49cb82020-03-16 23:17:32 +01001527 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001528 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001529
Angel Pons88521882020-01-05 20:21:20 +01001530 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001531}
1532
Angel Pons011661c2020-11-15 18:21:35 +01001533static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001534{
1535 int min = data[0];
1536 int max = min;
1537 int i;
1538 for (i = 1; i < count; i++) {
1539 if (min > data[i])
1540 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001541
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001542 if (max < data[i])
1543 max = data[i];
1544 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001545 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001546 for (i = 0; i < count; i++)
1547 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001548
Angel Pons891f2bc2020-01-10 01:27:28 +01001549 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001550}
1551
Angel Pons011661c2020-11-15 18:21:35 +01001552static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001553{
Angel Pons011661c2020-11-15 18:21:35 +01001554 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001555 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001556 int lane;
1557
Angel Pons88521882020-01-05 20:21:20 +01001558 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001559
Angel Ponsffd50152020-11-12 11:03:10 +01001560 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001561
Angel Pons7c49cb82020-03-16 23:17:32 +01001562 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001563 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001564
Angel Pons011661c2020-11-15 18:21:35 +01001565 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1566 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001567 program_timings(ctrl, channel);
1568
Angel Pons011661c2020-11-15 18:21:35 +01001569 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001570
1571 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001572 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001573 }
1574 }
1575 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001576 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1577
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001578 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001579 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1580 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001581 /*
1582 * With command training not being done yet, the lane can be erroneous.
1583 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001584 */
Angel Pons011661c2020-11-15 18:21:35 +01001585 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001586 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1587
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001588 if (rn.all || rn.length < 8) {
1589 printk(BIOS_EMERG, "timC recovery failed\n");
1590 return MAKE_ERR;
1591 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001592 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001593 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001594 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001595 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001596 }
1597 return 0;
1598}
1599
Angel Pons88521882020-01-05 20:21:20 +01001600static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001601{
1602 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001603
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001604 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1605 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001606
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001607 return ret;
1608}
1609
Angel Pons765d4652020-11-11 14:44:35 +01001610/* Each cacheline is 64 bits long */
1611static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1612{
1613 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1614}
1615
Angel Pons88521882020-01-05 20:21:20 +01001616static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001617{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301618 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001619 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001620
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001621 for (j = 0; j < 16; j++)
1622 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001623
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001624 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001625
1626 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001627}
1628
Angel Pons88521882020-01-05 20:21:20 +01001629static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001630{
1631 int ret = 0;
1632 int channel;
1633 FOR_ALL_POPULATED_CHANNELS ret++;
1634 return ret;
1635}
1636
Angel Pons88521882020-01-05 20:21:20 +01001637static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001638{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301639 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001640 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301641 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001642
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001643 for (j = 0; j < 16; j++)
1644 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001645
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001646 for (j = 0; j < 16; j++)
1647 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001648
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001649 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001650
1651 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001652}
1653
Angel Pons88521882020-01-05 20:21:20 +01001654static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001655{
1656 int channel, slotrank, lane;
1657
1658 FOR_ALL_POPULATED_CHANNELS {
1659 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001660 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1661 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001662 }
1663
1664 program_timings(ctrl, channel);
1665
1666 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001667 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001668
Angel Ponsffd50152020-11-12 11:03:10 +01001669 iosav_write_read_mpr_sequence(
1670 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001671
Angel Pons7c49cb82020-03-16 23:17:32 +01001672 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001673 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001674
Angel Pons88521882020-01-05 20:21:20 +01001675 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001676 }
1677
1678 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001679 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1680 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001681 }
1682
1683 program_timings(ctrl, channel);
1684
1685 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001686 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001687
Angel Ponsffd50152020-11-12 11:03:10 +01001688 iosav_write_read_mpr_sequence(
1689 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001690
Angel Pons7c49cb82020-03-16 23:17:32 +01001691 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001692 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001693
Angel Pons88521882020-01-05 20:21:20 +01001694 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001695 }
1696 }
1697}
1698
Angel Pons820bce72020-11-14 17:02:55 +01001699static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001700{
1701 int timB;
1702 int statistics[NUM_LANES][128];
1703 int lane;
1704
Angel Pons58b609b2020-11-13 14:35:29 +01001705 const union gdcr_training_mod_reg training_mod = {
1706 .write_leveling_mode = 1,
1707 .training_rank_sel = slotrank,
1708 .enable_dqs_wl = 5,
1709 .odt_always_on = 1,
1710 .force_drive_enable = 1,
1711 };
1712 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001713
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001714 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1715 int bank = 1;
1716
1717 if (ctrl->rank_mirror[channel][slotrank])
1718 ddr3_mirror_mrreg(&bank, &mr1reg);
1719
1720 wait_for_iosav(channel);
1721
1722 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1723
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001724 for (timB = 0; timB < 128; timB++) {
1725 FOR_ALL_LANES {
1726 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1727 }
1728 program_timings(ctrl, channel);
1729
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001730 /* Execute command queue */
1731 iosav_run_once(channel);
1732
1733 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001734
1735 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001736 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1737 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1738 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001739 }
1740 }
1741 FOR_ALL_LANES {
1742 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001743 /*
1744 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1745 * of timB by a small value, which might cause the 6-bit value to overflow if
1746 * it's close to 0x3f. Increment the value by a small offset if it's likely
1747 * to overflow, to make sure it won't overflow while running tests and bricks
1748 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001749 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001750 * TODO: find out why some tests (edge write discovery) increment timB.
1751 */
1752 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001753 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001754 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001755 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001756
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001757 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1758 if (rn.all) {
1759 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1760 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001761
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001762 return MAKE_ERR;
1763 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001764 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1765 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001766 }
1767 return 0;
1768}
1769
Angel Pons820bce72020-11-14 17:02:55 +01001770static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001771{
1772 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001773 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001774 if (val == 0xffffffffffffffffLL)
1775 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001776 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001777 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001778 for (i = 0; i < 8; i++)
1779 if (val << (8 * (7 - i) + 4))
1780 return -i;
1781 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001782 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001783 for (i = 0; i < 8; i++)
1784 if (val >> (8 * (7 - i) + 4))
1785 return i;
1786 }
1787 return 8;
1788}
1789
Angel Ponsbf13ef02020-11-11 18:40:06 +01001790static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001791{
1792 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001793
1794 const union gdcr_training_mod_reg training_mod = {
1795 .dq_dqs_training_res = 1,
1796 };
1797 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1798
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001799 FOR_ALL_POPULATED_CHANNELS {
1800 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001801 }
1802 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1803
Angel Pons765d4652020-11-11 14:44:35 +01001804 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001805 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001806
Angel Pons88521882020-01-05 20:21:20 +01001807 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001808
Angel Ponsffd50152020-11-12 11:03:10 +01001809 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001810
Angel Pons7c49cb82020-03-16 23:17:32 +01001811 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001812 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813
Angel Pons88521882020-01-05 20:21:20 +01001814 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001815
Angel Pons8f0757e2020-11-11 23:03:36 +01001816 const struct iosav_ssq rd_sequence[] = {
1817 /* DRAM command PREA */
1818 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001819 .sp_cmd_ctrl = {
1820 .command = IOSAV_PRE,
1821 .ranksel_ap = 1,
1822 },
1823 .subseq_ctrl = {
1824 .cmd_executions = 1,
1825 .cmd_delay_gap = 3,
1826 .post_ssq_wait = ctrl->tRP,
1827 .data_direction = SSQ_NA,
1828 },
1829 .sp_cmd_addr = {
1830 .address = 1024,
1831 .rowbits = 6,
1832 .bank = 0,
1833 .rank = slotrank,
1834 },
1835 .addr_update = {
1836 .addr_wrap = 18,
1837 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001838 },
1839 /* DRAM command ACT */
1840 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001841 .sp_cmd_ctrl = {
1842 .command = IOSAV_ACT,
1843 .ranksel_ap = 1,
1844 },
1845 .subseq_ctrl = {
1846 .cmd_executions = 1,
1847 .cmd_delay_gap = 3,
1848 .post_ssq_wait = ctrl->tRCD,
1849 .data_direction = SSQ_NA,
1850 },
1851 .sp_cmd_addr = {
1852 .address = 0,
1853 .rowbits = 6,
1854 .bank = 0,
1855 .rank = slotrank,
1856 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001857 },
1858 /* DRAM command RD */
1859 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001860 .sp_cmd_ctrl = {
1861 .command = IOSAV_RD,
1862 .ranksel_ap = 3,
1863 },
1864 .subseq_ctrl = {
1865 .cmd_executions = 1,
1866 .cmd_delay_gap = 3,
1867 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001868 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001869 ctrl->timings[channel][slotrank].io_latency,
1870 .data_direction = SSQ_RD,
1871 },
1872 .sp_cmd_addr = {
1873 .address = 8,
1874 .rowbits = 6,
1875 .bank = 0,
1876 .rank = slotrank,
1877 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001878 },
1879 };
1880 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001881
Angel Pons7c49cb82020-03-16 23:17:32 +01001882 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001883 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001884
Angel Pons88521882020-01-05 20:21:20 +01001885 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001886 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001887 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001888 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001889 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001890
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001891 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1892 ctrl->timings[channel][slotrank].lanes[lane].timB +=
Angel Pons820bce72020-11-14 17:02:55 +01001893 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001894
1895 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001896 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1897 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001898 }
1899 }
Angel Pons88521882020-01-05 20:21:20 +01001900 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001901}
1902
Angel Pons7d115132020-11-14 01:44:44 +01001903static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001904{
Angel Pons7d115132020-11-14 01:44:44 +01001905 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001906
Angel Pons7d115132020-11-14 01:44:44 +01001907 FOR_ALL_POPULATED_CHANNELS {
1908 /* choose an existing rank */
1909 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001910
Angel Pons7d115132020-11-14 01:44:44 +01001911 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001912
Angel Pons7d115132020-11-14 01:44:44 +01001913 /* Execute command queue */
1914 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001915
Angel Pons7d115132020-11-14 01:44:44 +01001916 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001917
Angel Pons7d115132020-11-14 01:44:44 +01001918 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1919 }
1920
1921 /* Refresh disable */
1922 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1923
1924 FOR_ALL_POPULATED_CHANNELS {
1925 /* Execute the same command queue */
1926 iosav_run_once(channel);
1927
1928 wait_for_iosav(channel);
1929 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001930}
1931
Angel Pons7c49cb82020-03-16 23:17:32 +01001932/*
1933 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001934 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001935 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1936 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1937 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1938 * CLK/ADDR/CMD signals have the same routing delay.
1939 *
1940 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1941 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1942 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001943 */
Angel Pons820bce72020-11-14 17:02:55 +01001944static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001945{
Angel Pons820bce72020-11-14 17:02:55 +01001946 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001947
Angel Pons7d115132020-11-14 01:44:44 +01001948 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001949
Angel Pons7c49cb82020-03-16 23:17:32 +01001950 /* Enable write leveling on all ranks
1951 Disable all DQ outputs
1952 Only NOP is allowed in this mode */
1953 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1954 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001955 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001956
Angel Pons58b609b2020-11-13 14:35:29 +01001957 const union gdcr_training_mod_reg training_mod = {
1958 .write_leveling_mode = 1,
1959 .enable_dqs_wl = 5,
1960 .odt_always_on = 1,
1961 .force_drive_enable = 1,
1962 };
1963 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001964
1965 toggle_io_reset();
1966
Angel Pons7c49cb82020-03-16 23:17:32 +01001967 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001968 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001969 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970 if (err)
1971 return err;
1972 }
1973
Angel Pons7c49cb82020-03-16 23:17:32 +01001974 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001975 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001976 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001977
Angel Pons88521882020-01-05 20:21:20 +01001978 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001979
1980 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001981 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001982
Angel Pons7c49cb82020-03-16 23:17:32 +01001983 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001984 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985
1986 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001987 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001988 MCHBAR32(IOSAV_STATUS_ch(channel));
1989 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001990
Angel Ponsffd50152020-11-12 11:03:10 +01001991 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001992
Angel Pons7c49cb82020-03-16 23:17:32 +01001993 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001994 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001995
Angel Pons88521882020-01-05 20:21:20 +01001996 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001997 }
1998
1999 toggle_io_reset();
2000
Angel Pons820bce72020-11-14 17:02:55 +01002001 return 0;
2002}
2003
2004int write_training(ramctr_timing *ctrl)
2005{
2006 int channel, slotrank, lane;
2007 int err;
2008
2009 FOR_ALL_POPULATED_CHANNELS
2010 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
2011
2012 err = jedec_write_leveling(ctrl);
2013 if (err)
2014 return err;
2015
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002016 printram("CPE\n");
2017 precharge(ctrl);
2018 printram("CPF\n");
2019
Angel Pons50a6fe72020-11-14 01:18:14 +01002020 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002021 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002022 }
2023
2024 FOR_ALL_POPULATED_CHANNELS {
2025 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002026 }
2027
2028 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01002029 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002030 if (err)
2031 return err;
2032 }
2033
2034 FOR_ALL_POPULATED_CHANNELS
2035 program_timings(ctrl, channel);
2036
2037 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002038 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002039
2040 FOR_ALL_POPULATED_CHANNELS
2041 program_timings(ctrl, channel);
2042
Angel Pons50a6fe72020-11-14 01:18:14 +01002043 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002044 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002045 }
2046 return 0;
2047}
2048
Angel Ponsbf13ef02020-11-11 18:40:06 +01002049static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002050{
2051 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2052 int timC_delta;
2053 int lanes_ok = 0;
2054 int ctr = 0;
2055 int lane;
2056
2057 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2058 FOR_ALL_LANES {
2059 ctrl->timings[channel][slotrank].lanes[lane].timC =
2060 saved_rt.lanes[lane].timC + timC_delta;
2061 }
2062 program_timings(ctrl, channel);
2063 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002064 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002065 }
2066
Angel Pons765d4652020-11-11 14:44:35 +01002067 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002068 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002069
Angel Pons88521882020-01-05 20:21:20 +01002070 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002071
Angel Ponsffd50152020-11-12 11:03:10 +01002072 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002073
2074 /* Program LFSR for the RD/WR subsequences */
2075 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2076 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002077
Angel Pons7c49cb82020-03-16 23:17:32 +01002078 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002079 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002080
Angel Pons88521882020-01-05 20:21:20 +01002081 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002082 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002083 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002084
2085 if (r32 == 0)
2086 lanes_ok |= 1 << lane;
2087 }
2088 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002089 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002090 break;
2091 }
2092
2093 ctrl->timings[channel][slotrank] = saved_rt;
2094
Patrick Rudolphdd662872017-10-28 18:20:11 +02002095 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002096}
2097
Angel Pons88521882020-01-05 20:21:20 +01002098static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002099{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302100 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002101 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2102 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002103
2104 if (patno) {
2105 u8 base8 = 0x80 >> ((patno - 1) % 8);
2106 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2107 for (i = 0; i < 32; i++) {
2108 for (j = 0; j < 16; j++) {
2109 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002110
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111 if (invert[patno - 1][i] & (1 << (j / 2)))
2112 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002113
2114 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002115 }
2116 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002117 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002118 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2119 for (j = 0; j < 16; j++) {
2120 const u32 val = pattern[i][j];
2121 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2122 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002123 }
2124 sfence();
2125 }
Angel Pons765d4652020-11-11 14:44:35 +01002126
2127 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002128}
2129
Angel Pons88521882020-01-05 20:21:20 +01002130static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002131{
Angel Pons7d115132020-11-14 01:44:44 +01002132 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002133
Angel Pons7c49cb82020-03-16 23:17:32 +01002134 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002135 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002136
2137 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002138 dram_mrscommands(ctrl);
2139
2140 toggle_io_reset();
2141}
2142
Angel Ponsbf13ef02020-11-11 18:40:06 +01002143#define CT_MIN_PI -127
2144#define CT_MAX_PI 128
2145#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2146
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002147#define MIN_C320C_LEN 13
2148
2149static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2150{
2151 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2152 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002153 int command_pi;
2154 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002155 int delta = 0;
2156
2157 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2158
2159 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002160 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002161 }
2162
2163 ctrl->cmd_stretch[channel] = cmd_stretch;
2164
Angel Pons7a612742020-11-12 13:34:03 +01002165 const union tc_rap_reg tc_rap = {
2166 .tRRD = ctrl->tRRD,
2167 .tRTP = ctrl->tRTP,
2168 .tCKE = ctrl->tCKE,
2169 .tWTR = ctrl->tWTR,
2170 .tFAW = ctrl->tFAW,
2171 .tWR = ctrl->tWR,
2172 .tCMD = ctrl->cmd_stretch[channel],
2173 };
2174 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002175
2176 if (ctrl->cmd_stretch[channel] == 2)
2177 delta = 2;
2178 else if (ctrl->cmd_stretch[channel] == 0)
2179 delta = 4;
2180
2181 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002182 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002183 }
2184
Angel Ponsbf13ef02020-11-11 18:40:06 +01002185 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002186 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002187 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002188 }
2189 program_timings(ctrl, channel);
2190 reprogram_320c(ctrl);
2191 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002192 stat[slotrank][command_pi - CT_MIN_PI] =
2193 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002194 }
2195 }
2196 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002197 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002198
Angel Ponsbf13ef02020-11-11 18:40:06 +01002199 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002200 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2201 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002203 if (rn.all || rn.length < MIN_C320C_LEN) {
2204 FOR_ALL_POPULATED_RANKS {
2205 ctrl->timings[channel][slotrank] =
2206 saved_timings[channel][slotrank];
2207 }
2208 return MAKE_ERR;
2209 }
2210 }
2211
2212 return 0;
2213}
2214
Angel Pons7c49cb82020-03-16 23:17:32 +01002215/*
2216 * Adjust CMD phase shift and try multiple command rates.
2217 * A command rate of 2T doubles the time needed for address and command decode.
2218 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002219int command_training(ramctr_timing *ctrl)
2220{
2221 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002222
2223 FOR_ALL_POPULATED_CHANNELS {
2224 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002225 }
2226
2227 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002228 int cmdrate, err;
2229
2230 /*
2231 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002232 * Issue:
2233 * While c320c discovery seems to succeed raminit will fail in write training.
2234 *
2235 * Workaround:
2236 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2237 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002238 *
2239 * Single DIMM per channel:
2240 * Try command rate 1T and 2T
2241 */
2242 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002243 if (ctrl->tCMD)
2244 /* XMP gives the CMD rate in clock ticks, not ns */
2245 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002246
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002247 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002248 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2249
2250 if (!err)
2251 break;
2252 }
2253
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002254 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002255 printk(BIOS_EMERG, "c320c discovery failed\n");
2256 return err;
2257 }
2258
Angel Pons891f2bc2020-01-10 01:27:28 +01002259 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002260 }
2261
2262 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002263 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002264
2265 reprogram_320c(ctrl);
2266 return 0;
2267}
2268
Angel Pons4c79f932020-11-14 01:26:52 +01002269static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002270{
Angel Pons96a06dd2020-11-14 00:33:18 +01002271 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002272 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002273 int lane;
2274
Angel Pons96a06dd2020-11-14 00:33:18 +01002275 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002276 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002277 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2278 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002279 }
2280 program_timings(ctrl, channel);
2281
2282 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002283 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2284 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002285 }
2286
Angel Pons88521882020-01-05 20:21:20 +01002287 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002288
Angel Ponsffd50152020-11-12 11:03:10 +01002289 iosav_write_read_mpr_sequence(
2290 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002291
Angel Pons7c49cb82020-03-16 23:17:32 +01002292 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002293 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002294
Angel Pons88521882020-01-05 20:21:20 +01002295 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002296
2297 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002298 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002299 }
2300 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002301
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002302 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002303 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002304 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002305
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002306 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002307 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2308 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002309 return MAKE_ERR;
2310 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002311 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002312 }
2313 return 0;
2314}
2315
Angel Pons60971dc2020-11-14 00:49:38 +01002316static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2317{
2318 int slotrank, lane;
2319
2320 fill_pattern0(ctrl, channel, 0, 0);
2321 FOR_ALL_LANES {
2322 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2323 }
2324
2325 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2326 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2327 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2328 }
2329
2330 program_timings(ctrl, channel);
2331
2332 FOR_ALL_POPULATED_RANKS {
2333 wait_for_iosav(channel);
2334
2335 iosav_write_read_mpr_sequence(
2336 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2337
2338 /* Execute command queue */
2339 iosav_run_once(channel);
2340
2341 wait_for_iosav(channel);
2342 }
2343
2344 /* XXX: check any measured value ? */
2345
2346 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2347 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2348 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2349 }
2350
2351 program_timings(ctrl, channel);
2352
2353 FOR_ALL_POPULATED_RANKS {
2354 wait_for_iosav(channel);
2355
2356 iosav_write_read_mpr_sequence(
2357 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2358
2359 /* Execute command queue */
2360 iosav_run_once(channel);
2361
2362 wait_for_iosav(channel);
2363 }
2364
2365 /* XXX: check any measured value ? */
2366
2367 FOR_ALL_LANES {
2368 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2369 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2370 }
2371}
2372
Angel Pons4c79f932020-11-14 01:26:52 +01002373int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002374{
2375 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2376 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2377 int channel, slotrank, lane;
2378 int err;
2379
Angel Pons88521882020-01-05 20:21:20 +01002380 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002381
2382 toggle_io_reset();
2383
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002384 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002385 FOR_ALL_LANES {
Angel Pons60971dc2020-11-14 00:49:38 +01002386 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002387 }
2388
Angel Pons60971dc2020-11-14 00:49:38 +01002389 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002390
2391 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002392 }
2393
Angel Pons0c3936e2020-03-22 12:49:27 +01002394 /*
2395 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2396 * also use a single loop. It would seem that it is a debugging configuration.
2397 */
Angel Pons88521882020-01-05 20:21:20 +01002398 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2399 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002400
2401 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002402 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002403 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002404 if (err)
2405 return err;
2406 }
2407
Angel Pons88521882020-01-05 20:21:20 +01002408 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2409 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002410
2411 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002412 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002413 rising_edges[channel][slotrank]);
2414 if (err)
2415 return err;
2416 }
2417
Angel Pons88521882020-01-05 20:21:20 +01002418 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002419
2420 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2421 ctrl->timings[channel][slotrank].lanes[lane].falling =
2422 falling_edges[channel][slotrank][lane];
2423 ctrl->timings[channel][slotrank].lanes[lane].rising =
2424 rising_edges[channel][slotrank][lane];
2425 }
2426
2427 FOR_ALL_POPULATED_CHANNELS {
2428 program_timings(ctrl, channel);
2429 }
2430
Angel Pons50a6fe72020-11-14 01:18:14 +01002431 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002432 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002433 }
2434 return 0;
2435}
2436
Angel Pons7c49cb82020-03-16 23:17:32 +01002437static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002438{
2439 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002440 u32 raw_stats[MAX_EDGE_TIMING + 1];
2441 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002442 const int reg3000b24[] = { 0, 0xc, 0x2c };
2443 int lane, i;
2444 int lower[NUM_LANES];
2445 int upper[NUM_LANES];
2446 int pat;
2447
2448 FOR_ALL_LANES {
2449 lower[lane] = 0;
2450 upper[lane] = MAX_EDGE_TIMING;
2451 }
2452
2453 for (i = 0; i < 3; i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002454 const union gdcr_training_mod_reg training_mod = {
2455 .vref_gen_ctl = reg3000b24[i],
2456 };
2457 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2458 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002459
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002460 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2461 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002462 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002463
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002464 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2465 FOR_ALL_LANES {
2466 ctrl->timings[channel][slotrank].lanes[lane].
2467 rising = edge;
2468 ctrl->timings[channel][slotrank].lanes[lane].
2469 falling = edge;
2470 }
2471 program_timings(ctrl, channel);
2472
2473 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002474 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2475 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002476 }
Angel Pons88521882020-01-05 20:21:20 +01002477 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002478
Angel Ponsffd50152020-11-12 11:03:10 +01002479 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002480
Angel Pons7c49cb82020-03-16 23:17:32 +01002481 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002482 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002483
Angel Pons88521882020-01-05 20:21:20 +01002484 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002485 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002486 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002487 }
2488
Angel Pons7c49cb82020-03-16 23:17:32 +01002489 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002490 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002491 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002492
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002493 FOR_ALL_LANES {
2494 struct run rn;
2495 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002496 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2497
2498 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2499
2500 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2501 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2502 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002503 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002504
2505 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2506 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2507
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002508 edges[lane] = (lower[lane] + upper[lane]) / 2;
2509 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002510 printk(BIOS_EMERG, "edge write discovery failed: "
2511 "%d, %d, %d\n", channel, slotrank, lane);
2512
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002513 return MAKE_ERR;
2514 }
2515 }
2516 }
2517 }
2518
Angel Ponsa93f46e2020-11-17 16:54:01 +01002519 /* Restore nominal Vref after training */
2520 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002521 printram("CPA\n");
2522 return 0;
2523}
2524
2525int discover_edges_write(ramctr_timing *ctrl)
2526{
2527 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002528 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2529 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002530
Angel Pons7c49cb82020-03-16 23:17:32 +01002531 /*
2532 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2533 * also use a single loop. It would seem that it is a debugging configuration.
2534 */
Angel Pons88521882020-01-05 20:21:20 +01002535 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2536 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002537
2538 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2539 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002540 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002541 if (err)
2542 return err;
2543 }
2544
Angel Pons88521882020-01-05 20:21:20 +01002545 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2546 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002547
2548 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2549 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002550 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002551 if (err)
2552 return err;
2553 }
2554
Angel Pons88521882020-01-05 20:21:20 +01002555 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002556
2557 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2558 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002559 falling_edges[channel][slotrank][lane];
2560
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002561 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002562 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002563 }
2564
2565 FOR_ALL_POPULATED_CHANNELS
2566 program_timings(ctrl, channel);
2567
Angel Pons50a6fe72020-11-14 01:18:14 +01002568 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002569 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002570 }
2571 return 0;
2572}
2573
2574static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2575{
Angel Pons88521882020-01-05 20:21:20 +01002576 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002577
Angel Ponsffd50152020-11-12 11:03:10 +01002578 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002579
Angel Pons7c49cb82020-03-16 23:17:32 +01002580 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002581 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002582
Angel Pons88521882020-01-05 20:21:20 +01002583 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002584}
2585
2586int discover_timC_write(ramctr_timing *ctrl)
2587{
Angel Pons7c49cb82020-03-16 23:17:32 +01002588 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002589 int i, pat;
2590
2591 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2592 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2593 int channel, slotrank, lane;
2594
2595 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2596 lower[channel][slotrank][lane] = 0;
2597 upper[channel][slotrank][lane] = MAX_TIMC;
2598 }
2599
Angel Pons88521882020-01-05 20:21:20 +01002600 /*
2601 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2602 * FIXME: This must only be done on Ivy Bridge.
2603 */
2604 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002605 printram("discover timC write:\n");
2606
2607 for (i = 0; i < 3; i++)
2608 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002609
2610 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2611 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2612 ~0x3f000000, rege3c_b24[i] << 24);
2613
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002614 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002615
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002616 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2617 FOR_ALL_POPULATED_RANKS {
2618 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002619 u32 raw_stats[MAX_TIMC + 1];
2620 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002621
2622 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002623 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002624
2625 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002626
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002627 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002628 FOR_ALL_LANES {
2629 ctrl->timings[channel][slotrank]
2630 .lanes[lane].timC = timC;
2631 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002632 program_timings(ctrl, channel);
2633
2634 test_timC_write (ctrl, channel, slotrank);
2635
Angel Pons7c49cb82020-03-16 23:17:32 +01002636 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002637 raw_stats[timC] = MCHBAR32(
2638 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002639 }
2640 FOR_ALL_LANES {
2641 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002642 for (timC = 0; timC < MAX_TIMC; timC++) {
2643 stats[timC] = !!(raw_stats[timC]
2644 & (1 << lane));
2645 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002646
Angel Pons7c49cb82020-03-16 23:17:32 +01002647 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002648 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002649 printk(BIOS_EMERG,
2650 "timC write discovery failed: "
2651 "%d, %d, %d\n", channel,
2652 slotrank, lane);
2653
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002654 return MAKE_ERR;
2655 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002656 printram("timC: %d, %d, %d: "
2657 "0x%02x-0x%02x-0x%02x, "
2658 "0x%02x-0x%02x\n", channel, slotrank,
2659 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002660 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002661 rn.end - ctrl->timC_offset[i]);
2662
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002663 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002664 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002665 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002666
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002667 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002668 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002669 upper[channel][slotrank][lane]);
2670
2671 }
2672 }
2673 }
2674 }
2675
2676 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002677 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002678 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002679 udelay(2);
2680 }
2681
Angel Pons88521882020-01-05 20:21:20 +01002682 /*
2683 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2684 * FIXME: This must only be done on Ivy Bridge.
2685 */
2686 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002687
2688 printram("CPB\n");
2689
2690 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002691 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002692 (lower[channel][slotrank][lane] +
2693 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002694
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002695 ctrl->timings[channel][slotrank].lanes[lane].timC =
2696 (lower[channel][slotrank][lane] +
2697 upper[channel][slotrank][lane]) / 2;
2698 }
2699 FOR_ALL_POPULATED_CHANNELS {
2700 program_timings(ctrl, channel);
2701 }
2702 return 0;
2703}
2704
Angel Pons88521882020-01-05 20:21:20 +01002705void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002706{
2707 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002708 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002709
2710 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2711 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002712 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002713 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002714 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002715 printram("normalize %d, %d, %d: mat %d\n",
2716 channel, slotrank, lane, mat);
2717
Felix Heldef4fe3e2019-12-31 14:15:05 +01002718 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002719 printram("normalize %d, %d, %d: delta %d\n",
2720 channel, slotrank, lane, delta);
2721
Angel Pons88521882020-01-05 20:21:20 +01002722 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002723 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002724 }
2725
2726 FOR_ALL_POPULATED_CHANNELS {
2727 program_timings(ctrl, channel);
2728 }
2729}
2730
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002731int channel_test(ramctr_timing *ctrl)
2732{
2733 int channel, slotrank, lane;
2734
2735 slotrank = 0;
2736 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002737 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002738 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002739 return MAKE_ERR;
2740 }
2741 FOR_ALL_POPULATED_CHANNELS {
2742 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002743 }
2744
2745 for (slotrank = 0; slotrank < 4; slotrank++)
2746 FOR_ALL_CHANNELS
2747 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2748 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002749 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2750 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002751 }
Angel Pons88521882020-01-05 20:21:20 +01002752 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002753
Angel Ponsffd50152020-11-12 11:03:10 +01002754 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002755
Angel Pons7c49cb82020-03-16 23:17:32 +01002756 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002757 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002758
Angel Pons88521882020-01-05 20:21:20 +01002759 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002760 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002761 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002762 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2763 channel, slotrank, lane);
2764 return MAKE_ERR;
2765 }
2766 }
2767 return 0;
2768}
2769
Patrick Rudolphdd662872017-10-28 18:20:11 +02002770void channel_scrub(ramctr_timing *ctrl)
2771{
2772 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002773 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002774
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002775 FOR_ALL_POPULATED_CHANNELS {
2776 wait_for_iosav(channel);
2777 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002778 }
2779
2780 /*
2781 * During runtime the "scrubber" will periodically scan through the memory in the
2782 * physical address space, to identify and fix CRC errors.
2783 * The following loops writes to every DRAM address, setting the ECC bits to the
2784 * correct value. A read from this location will no longer return a CRC error,
2785 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002786 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002787 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2788 * and firmware running in x86_32.
2789 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002790 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2791 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002792 for (bank = 0; bank < 8; bank++) {
2793 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002794
Angel Pons8f0757e2020-11-11 23:03:36 +01002795 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2796 const struct iosav_ssq sequence[] = {
2797 /*
2798 * DRAM command ACT
2799 * Opens the row for writing.
2800 */
2801 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002802 .sp_cmd_ctrl = {
2803 .command = IOSAV_ACT,
2804 .ranksel_ap = 1,
2805 },
2806 .subseq_ctrl = {
2807 .cmd_executions = 1,
2808 .cmd_delay_gap = gap,
2809 .post_ssq_wait = ctrl->tRCD,
2810 .data_direction = SSQ_NA,
2811 },
2812 .sp_cmd_addr = {
2813 .address = row,
2814 .rowbits = 6,
2815 .bank = bank,
2816 .rank = slotrank,
2817 },
2818 .addr_update = {
2819 .inc_addr_1 = 1,
2820 .addr_wrap = 18,
2821 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002822 },
2823 /*
2824 * DRAM command WR
2825 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2826 * bytes.
2827 */
2828 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002829 .sp_cmd_ctrl = {
2830 .command = IOSAV_WR,
2831 .ranksel_ap = 1,
2832 },
2833 .subseq_ctrl = {
2834 .cmd_executions = 129,
2835 .cmd_delay_gap = 4,
2836 .post_ssq_wait = ctrl->tWTR +
2837 ctrl->CWL + 8,
2838 .data_direction = SSQ_WR,
2839 },
2840 .sp_cmd_addr = {
2841 .address = row,
2842 .rowbits = 0,
2843 .bank = bank,
2844 .rank = slotrank,
2845 },
2846 .addr_update = {
2847 .inc_addr_8 = 1,
2848 .addr_wrap = 9,
2849 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002850 },
2851 /*
2852 * DRAM command PRE
2853 * Closes the row.
2854 */
2855 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002856 .sp_cmd_ctrl = {
2857 .command = IOSAV_PRE,
2858 .ranksel_ap = 1,
2859 },
2860 .subseq_ctrl = {
2861 .cmd_executions = 1,
2862 .cmd_delay_gap = 4,
2863 .post_ssq_wait = ctrl->tRP,
2864 .data_direction = SSQ_NA,
2865 },
2866 .sp_cmd_addr = {
2867 .address = 0,
2868 .rowbits = 6,
2869 .bank = bank,
2870 .rank = slotrank,
2871 },
2872 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002873 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002874 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002875 },
2876 };
2877 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002878
2879 /* Execute command queue */
2880 iosav_run_queue(channel, 16, 0);
2881
2882 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002883 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002884 }
2885 }
2886}
2887
Angel Pons88521882020-01-05 20:21:20 +01002888void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002889{
2890 int channel;
2891
Angel Pons7c49cb82020-03-16 23:17:32 +01002892 /* 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 +01002893 static u32 seeds[NUM_CHANNELS][3] = {
2894 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2895 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2896 };
2897 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002898 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002899 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2900 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2901 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002902 }
2903}
2904
Angel Pons89ae6b82020-03-21 13:23:32 +01002905void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002906{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002907 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002908 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002909 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002910 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002911 }
2912}
2913
Angel Pons88521882020-01-05 20:21:20 +01002914void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002915{
2916 int channel;
2917
2918 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002919 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002920 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002921 }
2922
2923 udelay(1);
2924
2925 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002926 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002927 }
2928}
2929
Angel Pons7c49cb82020-03-16 23:17:32 +01002930void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002931{
2932 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002933
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002934 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002935 int min_pi = 10000;
2936 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002937
2938 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002939 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2940 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002941 }
2942
Angel Pons7a612742020-11-12 13:34:03 +01002943 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002944
Angel Pons7a612742020-11-12 13:34:03 +01002945 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002946
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002947 dram_odt_stretch(ctrl, channel);
2948
Angel Pons7a612742020-11-12 13:34:03 +01002949 const union tc_rwp_reg tc_rwp = {
2950 .tRRDR = 0,
2951 .tRRDD = val,
2952 .tWWDR = val,
2953 .tWWDD = val,
2954 .tRWDRDD = ctrl->ref_card_offset[channel] + 2,
2955 .tWRDRDD = tWRDRDD,
2956 .tRWSR = 2,
2957 .dec_wrd = 1,
2958 };
2959 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002960 }
2961}
2962
Angel Pons88521882020-01-05 20:21:20 +01002963void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002964{
2965 int channel;
2966 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002967 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2968 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002969 }
2970}
2971
Angel Pons7c49cb82020-03-16 23:17:32 +01002972/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2973static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002974{
Angel Pons88521882020-01-05 20:21:20 +01002975 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002976}
2977
Angel Pons7c49cb82020-03-16 23:17:32 +01002978/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002979void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002980{
Angel Ponsb50ca572020-11-11 19:07:20 +01002981 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01002982
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002983 int channel;
2984 int t1_cycles = 0, t1_ns = 0, t2_ns;
2985 int t3_ns;
2986 u32 r32;
2987
Angel Pons7c49cb82020-03-16 23:17:32 +01002988 /* FIXME: This register only exists on Ivy Bridge */
2989 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002990
Angel Pons7a612742020-11-12 13:34:03 +01002991 FOR_ALL_CHANNELS {
2992 union tc_othp_reg tc_othp = {
2993 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2994 };
2995 tc_othp.tCPDED = 1;
2996 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2997 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002998
Patrick Rudolph74203de2017-11-20 11:57:01 +01002999 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003000 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003001 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003002 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003003 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003004 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003005
Felix Heldf9b826a2018-07-30 17:56:52 +02003006 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003007 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003008
Angel Pons88521882020-01-05 20:21:20 +01003009 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3010 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003011
3012 FOR_ALL_CHANNELS {
3013 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003014 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003015 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003016 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003017 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003018 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003019 case 1:
3020 case 4:
3021 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003022 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003023 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003024 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003025 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003026 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003027 break;
3028 }
3029 }
3030
Felix Held50b7ed22019-12-30 20:41:54 +01003031 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003032 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003033 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003034
Angel Pons7a612742020-11-12 13:34:03 +01003035 FOR_ALL_CHANNELS {
3036 union tc_rfp_reg tc_rfp = {
3037 .raw = MCHBAR32(TC_RFP_ch(channel)),
3038 };
3039 tc_rfp.refresh_2x_control = 1;
3040 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
3041 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003042
Angel Ponsdc5539f2020-11-12 12:44:25 +01003043 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3044 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003045 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003046
Angel Pons7c49cb82020-03-16 23:17:32 +01003047 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003048 FOR_ALL_POPULATED_CHANNELS
3049 break;
3050
Angel Pons88521882020-01-05 20:21:20 +01003051 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3052 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003053 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003054 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003055 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003056 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003057 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003058 t1_ns += 500;
3059
Angel Pons88521882020-01-05 20:21:20 +01003060 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003061 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003062 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003063 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003064 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003065 t3_ns = 500;
3066 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003067
3068 /* The graphics driver will use these watermark values */
3069 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003070 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003071 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3072 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003073}
3074
Angel Pons88521882020-01-05 20:21:20 +01003075void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003076{
Angel Pons50a6fe72020-11-14 01:18:14 +01003077 int channel, lane;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003078
Angel Pons7c49cb82020-03-16 23:17:32 +01003079 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01003080 const union tc_rap_reg tc_rap = {
3081 .tRRD = ctrl->tRRD,
3082 .tRTP = ctrl->tRTP,
3083 .tCKE = ctrl->tCKE,
3084 .tWTR = ctrl->tWTR,
3085 .tFAW = ctrl->tFAW,
3086 .tWR = ctrl->tWR,
3087 .tCMD = ctrl->cmd_stretch[channel],
3088 };
3089 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01003090 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003091
3092 udelay(1);
3093
3094 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003095 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003096 }
3097
Angel Pons50a6fe72020-11-14 01:18:14 +01003098 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003099 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003100 }
3101
3102 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003103 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003104
3105 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003106 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003107 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003108 }
3109
3110 printram("CPE\n");
3111
Angel Pons88521882020-01-05 20:21:20 +01003112 MCHBAR32(GDCRTRAININGMOD) = 0;
3113 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003114
3115 printram("CP5b\n");
3116
3117 FOR_ALL_POPULATED_CHANNELS {
3118 program_timings(ctrl, channel);
3119 }
3120
3121 u32 reg, addr;
3122
Angel Pons7c49cb82020-03-16 23:17:32 +01003123 /* Poll for RCOMP */
3124 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3125 ;
3126
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003127 do {
Angel Pons88521882020-01-05 20:21:20 +01003128 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003129 } while ((reg & 0x14) == 0);
3130
Angel Pons7c49cb82020-03-16 23:17:32 +01003131 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003132 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003133 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003134
Angel Pons7c49cb82020-03-16 23:17:32 +01003135 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003136 udelay(500);
3137
3138 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003139 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003140 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003141 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003142 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003143 MCHBAR32(addr) = reg;
3144
Angel Pons7c49cb82020-03-16 23:17:32 +01003145 /* Wait 10ns for ranks to settle */
3146 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003147
3148 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3149 MCHBAR32(addr) = reg;
3150
Angel Pons7c49cb82020-03-16 23:17:32 +01003151 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003152 write_reset(ctrl);
3153 }
3154
Angel Pons7c49cb82020-03-16 23:17:32 +01003155 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003156 dram_mrscommands(ctrl);
3157
3158 printram("CP5c\n");
3159
Angel Pons88521882020-01-05 20:21:20 +01003160 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003161
3162 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003163 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003164 udelay(2);
3165 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003166}