blob: eac0b50eedf8eac25cd2c1d66930a9ef3553453a [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
Angel Pons12bd8ab2020-11-13 23:10:52 +01003#include <assert.h>
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01004#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <console/console.h>
Angel Pons47a80a02020-12-07 13:15:23 +01006#include <cpu/intel/model_206ax/model_206ax.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01007#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05308#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02009#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020010#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010011#include <northbridge/intel/sandybridge/chip.h>
12#include <device/pci_def.h>
13#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020014#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020015
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010016#include "raminit_native.h"
17#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010018#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010019#include "sandybridge.h"
20
Angel Pons7c49cb82020-03-16 23:17:32 +010021/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010022
23static void sfence(void)
24{
25 asm volatile ("sfence");
26}
27
Angel Pons7c49cb82020-03-16 23:17:32 +010028/* Toggle IO reset bit */
29static void toggle_io_reset(void)
30{
Angel Pons88521882020-01-05 20:21:20 +010031 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010032 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010033 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010034 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010035 udelay(1);
36}
37
38static u32 get_XOVER_CLK(u8 rankmap)
39{
40 return rankmap << 24;
41}
42
43static u32 get_XOVER_CMD(u8 rankmap)
44{
45 u32 reg;
46
Angel Pons7c49cb82020-03-16 23:17:32 +010047 /* Enable xover cmd */
Angel Pons5db1b152020-12-13 16:37:53 +010048 reg = 1 << 14;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010049
Angel Pons7c49cb82020-03-16 23:17:32 +010050 /* Enable xover ctl */
51 if (rankmap & 0x03)
52 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010053
Angel Pons7c49cb82020-03-16 23:17:32 +010054 if (rankmap & 0x0c)
55 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010056
57 return reg;
58}
59
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010060void dram_find_common_params(ramctr_timing *ctrl)
61{
62 size_t valid_dimms;
63 int channel, slot;
64 dimm_info *dimms = &ctrl->info;
65
66 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
67 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010068
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010069 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010070
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010071 const dimm_attr *dimm = &dimms->dimm[channel][slot];
72 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
73 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010074
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010075 valid_dimms++;
76
77 /* Find all possible CAS combinations */
78 ctrl->cas_supported &= dimm->cas_supported;
79
80 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010081 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
82 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
83 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010084 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
85 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010086 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010087 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
88 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
89 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
90 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
91 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030092 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
93 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010094 }
95
96 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010097 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
98
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010099 if (!valid_dimms)
100 die("No valid DIMMs found");
101}
102
Angel Pons88521882020-01-05 20:21:20 +0100103void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100104{
105 u32 reg;
106 int channel;
107
108 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100109 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100110 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100111 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
112 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100113
Angel Pons7c49cb82020-03-16 23:17:32 +0100114 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100115 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100116 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
117 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100118 }
119}
120
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100121static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100122{
Angel Pons89ae6b82020-03-21 13:23:32 +0100123 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100124
125 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100126 /*
127 * ODT stretch:
128 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
129 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100130 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100131 if (stretch == 2)
132 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100133
Angel Pons88521882020-01-05 20:21:20 +0100134 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100135 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100136 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100137 } else {
Angel Pons88521882020-01-05 20:21:20 +0100138 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100139 union tc_othp_reg tc_othp = {
140 .raw = MCHBAR32(addr),
141 };
142 tc_othp.odt_delay_d0 = stretch;
143 tc_othp.odt_delay_d1 = stretch;
144 MCHBAR32(addr) = tc_othp.raw;
Iru Cai89af71c2018-08-16 16:46:27 +0800145 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100146 }
147}
148
149void dram_timing_regs(ramctr_timing *ctrl)
150{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100151 int channel;
152
Angel Pons81378062020-11-12 13:46:21 +0100153 /* BIN parameters */
154 const union tc_dbp_reg tc_dbp = {
155 .tRCD = ctrl->tRCD,
156 .tRP = ctrl->tRP,
157 .tAA = ctrl->CAS,
158 .tCWL = ctrl->CWL,
159 .tRAS = ctrl->tRAS,
160 };
161
162 /* Regular access parameters */
163 const union tc_rap_reg tc_rap = {
164 .tRRD = ctrl->tRRD,
165 .tRTP = ctrl->tRTP,
166 .tCKE = ctrl->tCKE,
167 .tWTR = ctrl->tWTR,
168 .tFAW = ctrl->tFAW,
169 .tWR = ctrl->tWR,
170 .tCMD = 3,
171 };
172
173 /* Other parameters */
174 const union tc_othp_reg tc_othp = {
175 .tXPDLL = ctrl->tXPDLL,
176 .tXP = ctrl->tXP,
177 .tAONPD = ctrl->tAONPD,
178 .tCPDED = 2,
Angel Pons2ad03a42020-11-19 11:07:27 +0100179 .tPRPDEN = 1,
Angel Pons81378062020-11-12 13:46:21 +0100180 };
181
182 /*
183 * If tXP and tXPDLL are very high, we need to increase them by one.
184 * This can only happen on Ivy Bridge, and when overclocking the RAM.
185 */
186 const union tc_dtp_reg tc_dtp = {
187 .overclock_tXP = ctrl->tXP >= 8,
188 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
189 };
190
191 /*
192 * TC-Refresh timing parameters:
193 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
194 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
195 */
196 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
197
198 const union tc_rftp_reg tc_rftp = {
199 .tREFI = ctrl->tREFI,
200 .tRFC = ctrl->tRFC,
201 .tREFIx9 = val32 / 1024,
202 };
203
204 /* Self-refresh timing parameters */
205 const union tc_srftp_reg tc_srftp = {
206 .tXSDLL = tDLLK,
207 .tXS_offset = ctrl->tXSOffset,
208 .tZQOPER = tDLLK - ctrl->tXSOffset,
209 .tMOD = ctrl->tMOD - 8,
210 };
211
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100212 FOR_ALL_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +0100213 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
214 MCHBAR32(TC_DBP_ch(channel)) = tc_dbp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100215
Angel Pons7a612742020-11-12 13:34:03 +0100216 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
217 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100218
Angel Pons7a612742020-11-12 13:34:03 +0100219 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
220 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100221
Angel Ponsca2f68a2020-03-22 13:15:12 +0100222 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Pons81378062020-11-12 13:46:21 +0100223 /* Debug parameters - only applies to Ivy Bridge */
Angel Pons7a612742020-11-12 13:34:03 +0100224 MCHBAR32(TC_DTP_ch(channel)) = tc_dtp.raw;
Angel Ponsca2f68a2020-03-22 13:15:12 +0100225 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100226
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100227 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100228
Angel Pons7a612742020-11-12 13:34:03 +0100229 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
230 MCHBAR32(TC_RFTP_ch(channel)) = tc_rftp.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +0100231
Angel Pons7a612742020-11-12 13:34:03 +0100232 union tc_rfp_reg tc_rfp = {
233 .raw = MCHBAR32(TC_RFP_ch(channel)),
234 };
235 tc_rfp.oref_ri = 0xff;
236 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100237
Angel Pons7a612742020-11-12 13:34:03 +0100238 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
239 MCHBAR32(TC_SRFTP_ch(channel)) = tc_srftp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100240 }
241}
242
243void dram_dimm_mapping(ramctr_timing *ctrl)
244{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100245 int channel;
246 dimm_info *info = &ctrl->info;
247
248 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200249 dimm_attr *dimmA, *dimmB;
250 u32 reg = 0;
251
Angel Pons7c49cb82020-03-16 23:17:32 +0100252 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100253 dimmA = &info->dimm[channel][0];
254 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100255 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100256 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100257 dimmA = &info->dimm[channel][1];
258 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100259 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100260 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100261
Nico Huberac4f2162017-10-01 18:14:43 +0200262 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100263 reg |= (dimmA->size_mb / 256) << 0;
264 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200265 reg |= (dimmA->width / 8 - 1) << 19;
266 }
267
268 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100269 reg |= (dimmB->size_mb / 256) << 8;
270 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200271 reg |= (dimmB->width / 8 - 1) << 20;
272 }
273
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200274 /*
275 * Rank interleave: Bit 16 of the physical address space sets
276 * the rank to use in a dual single rank DIMM configuration.
277 * That results in every 64KiB being interleaved between two ranks.
278 */
279 reg |= 1 << 21;
280 /* Enhanced interleave */
281 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200282
Angel Pons7c49cb82020-03-16 23:17:32 +0100283 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100284 ctrl->mad_dimm[channel] = reg;
285 } else {
286 ctrl->mad_dimm[channel] = 0;
287 }
288 }
289}
290
Patrick Rudolphdd662872017-10-28 18:20:11 +0200291void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100292{
293 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200294 u32 ecc;
295
296 if (ctrl->ecc_enabled)
297 ecc = training ? (1 << 24) : (3 << 24);
298 else
299 ecc = 0;
300
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100301 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200302 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100303 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200304
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200305 if (ctrl->ecc_enabled)
306 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100307}
308
Angel Pons88521882020-01-05 20:21:20 +0100309void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100310{
311 u32 reg, ch0size, ch1size;
312 u8 val;
313 reg = 0;
314 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100315
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100316 if (training) {
317 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
318 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
319 } else {
320 ch0size = ctrl->channel_size_mb[0];
321 ch1size = ctrl->channel_size_mb[1];
322 }
323
324 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100325 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100326 val = ch1size / 256;
327 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100328 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100329 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100330 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100331
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100332 } else {
Angel Pons88521882020-01-05 20:21:20 +0100333 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100334 val = ch0size / 256;
335 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100336 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100337 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100338 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100339 }
340}
341
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100342#define DEFAULT_PCI_MMIO_SIZE 2048
343
344static unsigned int get_mmio_size(void)
345{
346 const struct device *dev;
347 const struct northbridge_intel_sandybridge_config *cfg = NULL;
348
Angel Ponsb31d1d72020-01-10 01:35:09 +0100349 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100350 if (dev)
351 cfg = dev->chip_info;
352
353 /* If this is zero, it just means devicetree.cb didn't set it */
354 if (!cfg || cfg->pci_mmio_size == 0)
355 return DEFAULT_PCI_MMIO_SIZE;
356 else
357 return cfg->pci_mmio_size;
358}
359
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200360/*
361 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
362 * The ME/PCU/.. has the ability to change this.
363 * Return 0: ECC is optional
364 * Return 1: ECC is forced
365 */
366bool get_host_ecc_forced(void)
367{
368 /* read Capabilities A Register */
369 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
370 return !!(reg32 & (1 << 24));
371}
372
373/*
374 * Returns the ECC capability.
375 * The ME/PCU/.. has the ability to change this.
376 * Return 0: ECC is disabled
377 * Return 1: ECC is possible
378 */
379bool get_host_ecc_cap(void)
380{
381 /* read Capabilities A Register */
382 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
383 return !(reg32 & (1 << 25));
384}
385
Angel Pons88521882020-01-05 20:21:20 +0100386void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100387{
Angel Pons7c49cb82020-03-16 23:17:32 +0100388 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
389 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
390 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100391 uint16_t ggc;
392
393 mmiosize = get_mmio_size();
394
Felix Held87ddea22020-01-26 04:55:27 +0100395 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100396 if (!(ggc & 2)) {
397 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100398 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100399 } else {
400 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100401 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100402 }
403
404 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
405
406 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
407
408 mestolenbase = tom - me_uma_size;
409
Angel Pons7c49cb82020-03-16 23:17:32 +0100410 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
411
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100412 gfxstolenbase = toludbase - gfxstolen;
413 gttbase = gfxstolenbase - gttsize;
414
415 tsegbase = gttbase - tsegsize;
416
Angel Pons7c49cb82020-03-16 23:17:32 +0100417 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100418 tsegbasedelta = tsegbase & (tsegsize - 1);
419 tsegbase &= ~(tsegsize - 1);
420
421 gttbase -= tsegbasedelta;
422 gfxstolenbase -= tsegbasedelta;
423 toludbase -= tsegbasedelta;
424
Angel Pons7c49cb82020-03-16 23:17:32 +0100425 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100426 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100427 /* Reclaim is possible */
428 reclaim = 1;
429 remapbase = MAX(4096, tom - me_uma_size);
430 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
431 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100432 } else {
Angel Ponsc728e252021-01-03 16:47:09 +0100433 /* Reclaim not possible */
Angel Pons7c49cb82020-03-16 23:17:32 +0100434 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100435 touudbase = tom - me_uma_size;
436 }
437
Angel Pons7c49cb82020-03-16 23:17:32 +0100438 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100439 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
440
Angel Pons7c49cb82020-03-16 23:17:32 +0100441 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100442 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100443 val = tom & 0xfff;
444 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100445 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100446 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100447
Angel Ponsb31d1d72020-01-10 01:35:09 +0100448 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100449 val = tom & 0xfffff000;
450 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100451 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100452 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100453
Angel Pons7c49cb82020-03-16 23:17:32 +0100454 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100455 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100456 val = toludbase & 0xfff;
457 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100458 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100459 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100460
Angel Pons7c49cb82020-03-16 23:17:32 +0100461 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100462 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100463 val = touudbase & 0xfff;
464 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100465 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100466 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100467
Angel Pons7c49cb82020-03-16 23:17:32 +0100468 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100469 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100470 val = touudbase & 0xfffff000;
471 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100472 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100473 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100474
475 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100476 /* REMAP BASE */
477 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100478 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100479
Angel Pons7c49cb82020-03-16 23:17:32 +0100480 /* REMAP LIMIT */
481 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100482 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100483 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100484 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100485 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100486 val = tsegbase & 0xfff;
487 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100488 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100489 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100490
Angel Pons7c49cb82020-03-16 23:17:32 +0100491 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100492 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100493 val = gfxstolenbase & 0xfff;
494 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100495 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100496 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100497
Angel Pons7c49cb82020-03-16 23:17:32 +0100498 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100499 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100500 val = gttbase & 0xfff;
501 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100502 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100503 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100504
505 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100506 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100507 val = (0x80000 - me_uma_size) & 0xfffff000;
508 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100509 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100510 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100511
Angel Pons7c49cb82020-03-16 23:17:32 +0100512 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100513 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100514 val = mestolenbase & 0xfff;
515 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100516 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100517 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100518
Angel Ponsb31d1d72020-01-10 01:35:09 +0100519 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100520 val = mestolenbase & 0xfffff000;
521 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100522 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100523 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100524
Angel Pons7c49cb82020-03-16 23:17:32 +0100525 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100526 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100527 val = (0x80000 - me_uma_size) & 0xfff;
528 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100529 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
530 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100531 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100532 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100533 }
534}
535
Angel Pons88521882020-01-05 20:21:20 +0100536static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100537{
538 int channel, slotrank;
539
Angel Pons7c49cb82020-03-16 23:17:32 +0100540 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100541 channel = (ctrl->rankmap[0]) ? 0 : 1;
542
Angel Pons88521882020-01-05 20:21:20 +0100543 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100544
Angel Pons7c49cb82020-03-16 23:17:32 +0100545 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100546 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
547
Angel Ponsffd50152020-11-12 11:03:10 +0100548 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100549
Angel Ponsedd7cb42020-12-07 12:17:17 +0100550 /* This is actually using the IOSAV state machine as a timer */
Angel Pons38d901e2020-05-02 23:50:43 +0200551 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200552
Angel Pons88521882020-01-05 20:21:20 +0100553 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100554}
555
Angel Pons88521882020-01-05 20:21:20 +0100556void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100557{
Felix Held9fe248f2018-07-31 20:59:45 +0200558 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100559 int channel;
560
Angel Pons7c49cb82020-03-16 23:17:32 +0100561 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
562 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100563 do {
Angel Pons88521882020-01-05 20:21:20 +0100564 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100565 } while ((reg & 0x14) == 0);
566
Angel Pons7c49cb82020-03-16 23:17:32 +0100567 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100568 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100569 MCHBAR32(MC_INIT_STATE_G) = reg;
570 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100571 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100572 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100573
Angel Pons7c49cb82020-03-16 23:17:32 +0100574 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100575 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100576
Angel Pons7c49cb82020-03-16 23:17:32 +0100577 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100578 udelay(200);
579
Angel Pons7c49cb82020-03-16 23:17:32 +0100580 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100581 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100582
Angel Pons7c49cb82020-03-16 23:17:32 +0100583 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100584 udelay(500);
585
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100587 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100588
Angel Pons7c49cb82020-03-16 23:17:32 +0100589 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100590 udelay(1);
591
592 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100593 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200594 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100595 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100596
Angel Pons7c49cb82020-03-16 23:17:32 +0100597 /* Wait 10ns for ranks to settle */
598 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100599
600 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100601 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100602
Angel Pons7c49cb82020-03-16 23:17:32 +0100603 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100604 write_reset(ctrl);
605 }
606}
607
Angel Pons3d3bf482020-11-14 16:18:15 +0100608/*
609 * DDR3 Rank1 Address mirror swap the following pins:
610 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
611 */
612static void ddr3_mirror_mrreg(int *bank, u32 *addr)
613{
614 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
615 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
616}
617
Angel Pons7c49cb82020-03-16 23:17:32 +0100618static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100619{
Angel Pons88521882020-01-05 20:21:20 +0100620 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100621
Angel Pons3d3bf482020-11-14 16:18:15 +0100622 if (ctrl->rank_mirror[channel][slotrank])
623 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100624
Angel Pons8f0757e2020-11-11 23:03:36 +0100625 const struct iosav_ssq sequence[] = {
626 /* DRAM command MRS */
627 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200628 .sp_cmd_ctrl = {
629 .command = IOSAV_MRS,
630 },
631 .subseq_ctrl = {
632 .cmd_executions = 1,
633 .cmd_delay_gap = 4,
634 .post_ssq_wait = 4,
635 .data_direction = SSQ_NA,
636 },
637 .sp_cmd_addr = {
638 .address = val,
639 .rowbits = 6,
640 .bank = reg,
641 .rank = slotrank,
642 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100643 },
644 /* DRAM command MRS */
645 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200646 .sp_cmd_ctrl = {
647 .command = IOSAV_MRS,
648 .ranksel_ap = 1,
649 },
650 .subseq_ctrl = {
651 .cmd_executions = 1,
652 .cmd_delay_gap = 4,
653 .post_ssq_wait = 4,
654 .data_direction = SSQ_NA,
655 },
656 .sp_cmd_addr = {
657 .address = val,
658 .rowbits = 6,
659 .bank = reg,
660 .rank = slotrank,
661 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100662 },
663 /* DRAM command MRS */
664 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200665 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100666 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200667 },
668 .subseq_ctrl = {
669 .cmd_executions = 1,
670 .cmd_delay_gap = 4,
671 .post_ssq_wait = ctrl->tMOD,
672 .data_direction = SSQ_NA,
673 },
674 .sp_cmd_addr = {
675 .address = val,
676 .rowbits = 6,
677 .bank = reg,
678 .rank = slotrank,
679 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100680 },
681 };
682 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200683
Angel Pons9f4ed3b2020-12-07 12:34:36 +0100684 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100685}
686
Angel Pons09fc4b92020-11-19 12:02:07 +0100687/* Obtain optimal power down mode for current configuration */
688static enum pdwm_mode get_power_down_mode(ramctr_timing *ctrl)
689{
690 if (ctrl->tXP > 8)
691 return PDM_NONE;
692
693 if (ctrl->tXPDLL > 32)
694 return PDM_PPD;
695
696 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
697 return PDM_DLL_OFF;
698
699 return PDM_APD_PPD;
700}
701
Angel Pons88521882020-01-05 20:21:20 +0100702static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100703{
704 u16 mr0reg, mch_cas, mch_wr;
705 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Angel Pons09fc4b92020-11-19 12:02:07 +0100706
707 const enum pdwm_mode power_down = get_power_down_mode(ctrl);
708
709 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100710
Angel Pons7c49cb82020-03-16 23:17:32 +0100711 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100712 if (ctrl->CAS < 12) {
713 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
714 } else {
715 mch_cas = (u16) (ctrl->CAS - 12);
716 mch_cas = ((mch_cas << 1) | 0x1);
717 }
718
Angel Pons7c49cb82020-03-16 23:17:32 +0100719 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100720 mch_wr = mch_wr_t[ctrl->tWR - 5];
721
Angel Pons2bf28ed2020-11-12 13:49:59 +0100722 /* DLL Reset - self clearing - set after CLK frequency has been changed */
723 mr0reg = 1 << 8;
724
725 mr0reg |= (mch_cas & 0x1) << 2;
726 mr0reg |= (mch_cas & 0xe) << 3;
727 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100728
Angel Pons09fc4b92020-11-19 12:02:07 +0100729 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
730 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100731 return mr0reg;
732}
733
734static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
735{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200736 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100737}
738
Angel Ponsf9997482020-11-12 16:02:52 +0100739static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100740{
741 /* Get ODT based on rankmap */
742 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
743
744 if (dimms_per_ch == 1) {
745 return (const odtmap){60, 60};
746 } else {
747 return (const odtmap){120, 30};
748 }
749}
750
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100751static u32 encode_odt(u32 odt)
752{
753 switch (odt) {
754 case 30:
Angel Ponsc728e252021-01-03 16:47:09 +0100755 return (1 << 9) | (1 << 2); /* RZQ/8, RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100756 case 60:
Angel Ponsc728e252021-01-03 16:47:09 +0100757 return (1 << 2); /* RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100758 case 120:
Angel Ponsc728e252021-01-03 16:47:09 +0100759 return (1 << 6); /* RZQ/2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100760 default:
761 case 0:
762 return 0;
763 }
764}
765
766static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
767{
768 odtmap odt;
769 u32 mr1reg;
770
Angel Ponsf9997482020-11-12 16:02:52 +0100771 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100772 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100773
774 mr1reg |= encode_odt(odt.rttnom);
775
776 return mr1reg;
777}
778
779static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
780{
781 u16 mr1reg;
782
783 mr1reg = make_mr1(ctrl, rank, channel);
784
785 write_mrreg(ctrl, channel, rank, 1, mr1reg);
786}
787
788static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
789{
Angel Pons868bca22020-11-13 13:38:04 +0100790 const u16 pasr = 0;
791 const u16 cwl = ctrl->CWL - 5;
792 const odtmap odt = get_ODT(ctrl, channel);
793
Angel Ponsdca3cb52020-11-13 13:42:07 +0100794 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100795 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
796 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100797
Angel Pons868bca22020-11-13 13:38:04 +0100798 u16 mr2reg = 0;
799 mr2reg |= pasr;
800 mr2reg |= cwl << 3;
801 mr2reg |= ctrl->auto_self_refresh << 6;
802 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100803 mr2reg |= (odt.rttwr / 60) << 9;
804
805 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100806
807 /* Program MR2 shadow */
808 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
809
810 reg32 &= 3 << 14 | 3 << 6;
811
812 reg32 |= mr2reg & ~(3 << 6);
813
Angel Pons927b1c02020-12-10 22:11:27 +0100814 if (srt)
815 reg32 |= 1 << (rank / 2 + 6);
816
817 if (ctrl->rank_mirror[channel][rank])
818 reg32 |= 1 << (rank / 2 + 14);
819
Angel Pons7f1363d2020-11-13 13:31:58 +0100820 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100821}
822
823static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
824{
825 write_mrreg(ctrl, channel, rank, 3, 0);
826}
827
Angel Pons88521882020-01-05 20:21:20 +0100828void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100829{
830 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100831 int channel;
832
833 FOR_ALL_POPULATED_CHANNELS {
834 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100835 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100836 dram_mr2(ctrl, slotrank, channel);
837
Angel Pons7c49cb82020-03-16 23:17:32 +0100838 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100839 dram_mr3(ctrl, slotrank, channel);
840
Angel Pons7c49cb82020-03-16 23:17:32 +0100841 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100842 dram_mr1(ctrl, slotrank, channel);
843
Angel Pons7c49cb82020-03-16 23:17:32 +0100844 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100845 dram_mr0(ctrl, slotrank, channel);
846 }
847 }
848
Angel Pons8f0757e2020-11-11 23:03:36 +0100849 const struct iosav_ssq zqcl_sequence[] = {
850 /* DRAM command NOP (without ODT nor chip selects) */
851 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200852 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100853 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200854 },
855 .subseq_ctrl = {
856 .cmd_executions = 1,
857 .cmd_delay_gap = 4,
858 .post_ssq_wait = 15,
859 .data_direction = SSQ_NA,
860 },
861 .sp_cmd_addr = {
862 .address = 2,
863 .rowbits = 6,
864 .bank = 0,
865 .rank = 0,
866 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100867 },
868 /* DRAM command ZQCL */
869 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200870 .sp_cmd_ctrl = {
871 .command = IOSAV_ZQCS,
872 .ranksel_ap = 1,
873 },
874 .subseq_ctrl = {
875 .cmd_executions = 1,
876 .cmd_delay_gap = 4,
877 .post_ssq_wait = 400,
878 .data_direction = SSQ_NA,
879 },
880 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +0100881 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +0200882 .rowbits = 6,
883 .bank = 0,
884 .rank = 0,
885 },
886 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100887 .inc_rank = 1,
888 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200889 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100890 },
891 };
892 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100893
Angel Pons38d901e2020-05-02 23:50:43 +0200894 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100895
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100896 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +0100897 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100898 }
899
Angel Pons7c49cb82020-03-16 23:17:32 +0100900 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100901 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100902
903 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100904 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100905
Angel Pons88521882020-01-05 20:21:20 +0100906 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100907
908 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
909
Angel Pons88521882020-01-05 20:21:20 +0100910 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100911
Angel Ponsffd50152020-11-12 11:03:10 +0100912 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200913
Angel Ponsa853e7a2020-12-07 12:28:38 +0100914 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100915 }
916}
917
Felix Held3b906032020-01-14 17:05:43 +0100918static const u32 lane_base[] = {
919 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
920 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
921 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100922};
923
Angel Pons42d033a2021-01-03 15:26:37 +0100924/* Maximum delay for command, control, clock */
925#define CCC_MAX_PI (2 * QCLK_PI - 1)
926
Angel Pons88521882020-01-05 20:21:20 +0100927void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100928{
Angel Pons7584e552020-11-19 21:34:32 +0100929 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100930 int lane;
931 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100932
Angel Pons7584e552020-11-19 21:34:32 +0100933 u32 ctl_delay[NUM_SLOTS] = { 0 };
934 int cmd_delay = 0;
935
936 /* Enable CLK XOVER */
937 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
938 u32 clk_logic_dly = 0;
939
940 /*
941 * Apply command delay if desired setting is negative. Find the
942 * most negative value: 'cmd_delay' will be the absolute value.
943 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100944 FOR_ALL_POPULATED_RANKS {
Angel Pons7584e552020-11-19 21:34:32 +0100945 if (cmd_delay < -ctrl->timings[channel][slotrank].pi_coding)
946 cmd_delay = -ctrl->timings[channel][slotrank].pi_coding;
947 }
948 if (cmd_delay < 0) {
949 printk(BIOS_ERR, "C%d command delay underflow: %d\n", channel, cmd_delay);
950 cmd_delay = 0;
951 }
Angel Pons42d033a2021-01-03 15:26:37 +0100952 if (cmd_delay > CCC_MAX_PI) {
Angel Pons7584e552020-11-19 21:34:32 +0100953 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
Angel Pons42d033a2021-01-03 15:26:37 +0100954 cmd_delay = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100955 }
956
Angel Pons7584e552020-11-19 21:34:32 +0100957 /* Apply control and clock delay if desired setting is positive */
958 if (cmd_delay == 0) {
959 for (slot = 0; slot < NUM_SLOTS; slot++) {
960 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
961 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
962
963 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
964
965 if (slot_map & 1)
966 ctl_delay[slot] += pi_coding_0 + cmd_delay;
967
968 if (slot_map & 2)
969 ctl_delay[slot] += pi_coding_1 + cmd_delay;
970
971 /* If both ranks in a slot are populated, use the average */
972 if (slot_map == 3)
973 ctl_delay[slot] /= 2;
974
Angel Pons42d033a2021-01-03 15:26:37 +0100975 if (ctl_delay[slot] > CCC_MAX_PI) {
Angel Pons7584e552020-11-19 21:34:32 +0100976 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
977 channel, slot, ctl_delay[slot]);
Angel Pons42d033a2021-01-03 15:26:37 +0100978 ctl_delay[slot] = CCC_MAX_PI;
Angel Pons7584e552020-11-19 21:34:32 +0100979 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100980 }
Angel Pons7584e552020-11-19 21:34:32 +0100981 FOR_ALL_POPULATED_RANKS {
982 u32 clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
983
Angel Pons42d033a2021-01-03 15:26:37 +0100984 if (clk_delay > CCC_MAX_PI) {
Angel Pons7584e552020-11-19 21:34:32 +0100985 printk(BIOS_ERR, "C%dR%d clock delay overflow: %d\n",
986 channel, slotrank, clk_delay);
Angel Pons42d033a2021-01-03 15:26:37 +0100987 clk_delay = CCC_MAX_PI;
Angel Pons7584e552020-11-19 21:34:32 +0100988 }
989
Angel Pons42d033a2021-01-03 15:26:37 +0100990 clk_pi_coding |= (clk_delay % QCLK_PI) << (6 * slotrank);
991 clk_logic_dly |= (clk_delay / QCLK_PI) << slotrank;
Angel Pons7584e552020-11-19 21:34:32 +0100992 }
993 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100994
Angel Pons7c49cb82020-03-16 23:17:32 +0100995 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100996 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
997 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
998 };
Angel Pons42d033a2021-01-03 15:26:37 +0100999 cmd_pi_coding.cmd_pi_code = cmd_delay % QCLK_PI;
1000 cmd_pi_coding.cmd_logic_delay = cmd_delay / QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001001
Angel Pons42d033a2021-01-03 15:26:37 +01001002 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % QCLK_PI;
1003 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % QCLK_PI;
1004 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / QCLK_PI;
1005 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / QCLK_PI;
Angel Pons737f1112020-11-13 14:07:30 +01001006
1007 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001008
Angel Pons7584e552020-11-19 21:34:32 +01001009 MCHBAR32(GDCRCKPICODE_ch(channel)) = clk_pi_coding;
1010 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = clk_logic_dly;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001011
Angel Pons88521882020-01-05 20:21:20 +01001012 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001013 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001014
Angel Pons88521882020-01-05 20:21:20 +01001015 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001016
1017 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001018 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001019
Angel Pons88521882020-01-05 20:21:20 +01001020 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001021 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001022
1023 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001024 const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
1025 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p;
1026 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n;
Angel Pons9fcc1102020-11-19 22:23:13 +01001027 const union gdcr_rx_reg gdcr_rx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001028 .rcven_pi_code = rcven % QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001029 .rx_dqs_p_pi_code = dqs_p,
Angel Pons42d033a2021-01-03 15:26:37 +01001030 .rcven_logic_delay = rcven / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001031 .rx_dqs_n_pi_code = dqs_n,
1032 };
1033 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) = gdcr_rx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001034
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001035 const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1036 const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq;
Angel Pons9fcc1102020-11-19 22:23:13 +01001037 const union gdcr_tx_reg gdcr_tx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001038 .tx_dq_pi_code = tx_dq % QCLK_PI,
1039 .tx_dqs_pi_code = tx_dqs % QCLK_PI,
1040 .tx_dqs_logic_delay = tx_dqs / QCLK_PI,
1041 .tx_dq_logic_delay = tx_dq / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001042 };
1043 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) = gdcr_tx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001044 }
1045 }
Angel Pons88521882020-01-05 20:21:20 +01001046 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1047 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001048}
1049
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001050static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001051{
Angel Pons88521882020-01-05 20:21:20 +01001052 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053
Angel Pons3aed6ac2020-12-07 02:00:41 +01001054 /* Send a burst of 16 back-to-back read commands (4 DCLK apart) */
Angel Ponsffd50152020-11-12 11:03:10 +01001055 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001056
Angel Ponsa853e7a2020-12-07 12:28:38 +01001057 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001058}
1059
Angel Pons7c49cb82020-03-16 23:17:32 +01001060static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001061{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001062 u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
Angel Pons7c49cb82020-03-16 23:17:32 +01001063
1064 return (MCHBAR32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001065 GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001066}
1067
1068struct run {
1069 int middle;
1070 int end;
1071 int start;
1072 int all;
1073 int length;
1074};
1075
1076static struct run get_longest_zero_run(int *seq, int sz)
1077{
1078 int i, ls;
1079 int bl = 0, bs = 0;
1080 struct run ret;
1081
1082 ls = 0;
1083 for (i = 0; i < 2 * sz; i++)
1084 if (seq[i % sz]) {
1085 if (i - ls > bl) {
1086 bl = i - ls;
1087 bs = ls;
1088 }
1089 ls = i + 1;
1090 }
1091 if (bl == 0) {
1092 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001093 ret.start = 0;
1094 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001095 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001096 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001097 return ret;
1098 }
1099
Angel Pons7c49cb82020-03-16 23:17:32 +01001100 ret.start = bs % sz;
1101 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001102 ret.middle = (bs + (bl - 1) / 2) % sz;
1103 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001104 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001105
1106 return ret;
1107}
1108
Angel Pons42d033a2021-01-03 15:26:37 +01001109#define RCVEN_COARSE_PI_LENGTH (2 * QCLK_PI)
1110
Angel Ponsf3053392020-11-13 23:31:12 +01001111static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001112{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001113 int rcven;
Angel Pons42d033a2021-01-03 15:26:37 +01001114 int statistics[NUM_LANES][RCVEN_COARSE_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001115 int lane;
1116
Angel Pons42d033a2021-01-03 15:26:37 +01001117 for (rcven = 0; rcven < RCVEN_COARSE_PI_LENGTH; rcven++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001118 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001119 ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001120 }
1121 program_timings(ctrl, channel);
1122
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001123 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001124
1125 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001126 statistics[lane][rcven] =
1127 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001128 }
1129 }
1130 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001131 struct run rn = get_longest_zero_run(statistics[lane], RCVEN_COARSE_PI_LENGTH);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001132 ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001133 upperA[lane] = rn.end;
1134 if (upperA[lane] < rn.middle)
Angel Pons42d033a2021-01-03 15:26:37 +01001135 upperA[lane] += 2 * QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001136
Angel Pons7e439c92020-12-07 11:56:01 +01001137 printram("rcven: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001138 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001139 }
1140}
1141
Angel Ponsf3053392020-11-13 23:31:12 +01001142static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001143{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001144 int rcven_delta;
Angel Pons86e3d742021-01-03 14:55:12 +01001145 int statistics[NUM_LANES][51] = {0};
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001146 int lane, i;
1147
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001148 for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001149
1150 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001151 ctrl->timings[channel][slotrank].lanes[lane].rcven
Angel Pons42d033a2021-01-03 15:26:37 +01001152 = upperA[lane] + rcven_delta + QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001153 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001154 program_timings(ctrl, channel);
1155
1156 for (i = 0; i < 100; i++) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001157 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001158 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001159 statistics[lane][rcven_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001160 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001161 }
1162 }
1163 }
1164 FOR_ALL_LANES {
1165 int last_zero, first_all;
1166
1167 for (last_zero = -25; last_zero <= 25; last_zero++)
1168 if (statistics[lane][last_zero + 25])
1169 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001170
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001171 last_zero--;
1172 for (first_all = -25; first_all <= 25; first_all++)
1173 if (statistics[lane][first_all + 25] == 100)
1174 break;
1175
Angel Pons7c49cb82020-03-16 23:17:32 +01001176 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001177
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001178 ctrl->timings[channel][slotrank].lanes[lane].rcven =
Angel Pons7c49cb82020-03-16 23:17:32 +01001179 (last_zero + first_all) / 2 + upperA[lane];
1180
Angel Pons7e439c92020-12-07 11:56:01 +01001181 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001182 lane, ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001183 }
1184}
1185
Angel Pons3aed6ac2020-12-07 02:00:41 +01001186/*
1187 * Once the DQS high phase has been found (for each DRAM) the next stage
1188 * is to find out the round trip latency, by locating the preamble cycle.
1189 * This is achieved by trying smaller and smaller roundtrip values until
1190 * the strobe sampling is done on the preamble cycle.
1191 */
Angel Ponsf3053392020-11-13 23:31:12 +01001192static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193{
1194 int works[NUM_LANES];
1195 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001196
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001197 while (1) {
1198 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001199
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001200 program_timings(ctrl, channel);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001201 test_rcven(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001204 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1205
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001206 if (works[lane])
1207 some_works = 1;
1208 else
1209 all_works = 0;
1210 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001211
1212 /* If every lane is working, exit */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001213 if (all_works)
1214 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001215
Angel Pons3aed6ac2020-12-07 02:00:41 +01001216 /*
1217 * If all bits are one (everyone is failing), decrement
1218 * the roundtrip value by two, and do another iteration.
1219 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001220 if (!some_works) {
Angel Pons3aed6ac2020-12-07 02:00:41 +01001221 /* Guard against roundtrip latency underflow */
Angel Pons88521882020-01-05 20:21:20 +01001222 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Angel Pons30791632020-12-12 12:28:29 +01001223 printk(BIOS_EMERG, "Roundtrip latency underflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 channel, slotrank);
1225 return MAKE_ERR;
1226 }
Angel Pons88521882020-01-05 20:21:20 +01001227 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001228 printram("4024 -= 2;\n");
1229 continue;
1230 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001231
1232 /*
1233 * Else (if some lanes are failing), increase the rank's
1234 * I/O latency by 2, and increase rcven logic delay by 2
1235 * on the working lanes, then perform another iteration.
1236 */
Felix Heldef4fe3e2019-12-31 14:15:05 +01001237 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001238 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001239
Angel Pons3aed6ac2020-12-07 02:00:41 +01001240 /* Guard against I/O latency overflow */
Angel Pons5db1b152020-12-13 16:37:53 +01001241 if (ctrl->timings[channel][slotrank].io_latency >= 16) {
Angel Pons30791632020-12-12 12:28:29 +01001242 printk(BIOS_EMERG, "I/O latency overflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001243 channel, slotrank);
1244 return MAKE_ERR;
1245 }
1246 FOR_ALL_LANES if (works[lane]) {
Angel Pons42d033a2021-01-03 15:26:37 +01001247 ctrl->timings[channel][slotrank].lanes[lane].rcven += 2 * QCLK_PI;
1248 upperA[lane] += 2 * QCLK_PI;
Angel Pons891f2bc2020-01-10 01:27:28 +01001249 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001250 }
1251 }
1252 return 0;
1253}
1254
Angel Pons12bd8ab2020-11-13 23:10:52 +01001255static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001256{
1257 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001258 u16 logic_delay_min = 7;
1259 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001260
1261 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001262 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001263
1264 logic_delay_min = MIN(logic_delay_min, logic_delay);
1265 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001266 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001267
1268 if (logic_delay_max < logic_delay_min) {
1269 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1270 logic_delay_max, logic_delay_min, channel, slotrank);
1271 }
1272
1273 assert(logic_delay_max >= logic_delay_min);
1274
1275 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001276}
1277
Angel Pons12bd8ab2020-11-13 23:10:52 +01001278static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001279{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001280 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001281
Angel Pons7c49cb82020-03-16 23:17:32 +01001282 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001283 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001284
Angel Pons12bd8ab2020-11-13 23:10:52 +01001285 if (prev < post)
1286 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001287
Angel Pons12bd8ab2020-11-13 23:10:52 +01001288 else if (prev > post)
1289 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001290
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001291 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001292 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001293
Angel Pons12bd8ab2020-11-13 23:10:52 +01001294 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1295 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1296 printram("4024 += %d;\n", latency_offset);
1297 printram("4028 += %d;\n", latency_offset);
1298
1299 return post;
1300}
1301
1302static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1303{
1304 u16 logic_delay_min = 7;
1305 int lane;
1306
1307 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001308 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001309
1310 logic_delay_min = MIN(logic_delay_min, logic_delay);
1311 }
1312
1313 if (logic_delay_min >= 2) {
1314 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1315 logic_delay_min, channel, slotrank);
1316 }
1317
1318 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001319 ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001320 }
1321 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1322 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001323}
1324
Angel Pons7f5a97c2020-11-13 16:58:46 +01001325int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001326{
1327 int channel, slotrank, lane;
1328 int err;
1329
1330 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1331 int all_high, some_high;
1332 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001333 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001334
Angel Pons88521882020-01-05 20:21:20 +01001335 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001336
Angel Ponsffd50152020-11-12 11:03:10 +01001337 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001338
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001339 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001340
Angel Pons58b609b2020-11-13 14:35:29 +01001341 const union gdcr_training_mod_reg training_mod = {
1342 .receive_enable_mode = 1,
1343 .training_rank_sel = slotrank,
1344 .odt_always_on = 1,
1345 };
1346 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001347
Felix Heldef4fe3e2019-12-31 14:15:05 +01001348 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001349 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001350 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001351
Angel Ponsf3053392020-11-13 23:31:12 +01001352 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001353
Felix Held2bb3cdf2018-07-28 00:23:59 +02001354 all_high = 1;
1355 some_high = 0;
1356 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001357 if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= QCLK_PI)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001358 some_high = 1;
1359 else
1360 all_high = 0;
1361 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001362
1363 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001364 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001365 printram("4028--;\n");
1366 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001367 ctrl->timings[channel][slotrank].lanes[lane].rcven -= QCLK_PI;
1368 upperA[lane] -= QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001369
1370 }
1371 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001372 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001373 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001374 printram("4024++;\n");
1375 printram("4028++;\n");
1376 }
1377
1378 program_timings(ctrl, channel);
1379
Angel Pons12bd8ab2020-11-13 23:10:52 +01001380 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001381
Angel Ponsf3053392020-11-13 23:31:12 +01001382 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001383 if (err)
1384 return err;
1385
Angel Pons12bd8ab2020-11-13 23:10:52 +01001386 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001387
Angel Ponsf3053392020-11-13 23:31:12 +01001388 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001389
Angel Pons12bd8ab2020-11-13 23:10:52 +01001390 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001391
Angel Pons12bd8ab2020-11-13 23:10:52 +01001392 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001393
Angel Pons12bd8ab2020-11-13 23:10:52 +01001394 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001395
Angel Pons7e439c92020-12-07 11:56:01 +01001396 printram("4/8: %d, %d, % 4d, % 4d\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001397 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001398 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001399
1400 printram("final results:\n");
1401 FOR_ALL_LANES
Angel Pons7e439c92020-12-07 11:56:01 +01001402 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001403 ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001404
Angel Pons88521882020-01-05 20:21:20 +01001405 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001406
1407 toggle_io_reset();
1408 }
1409
1410 FOR_ALL_POPULATED_CHANNELS {
1411 program_timings(ctrl, channel);
1412 }
Angel Ponsc6742232020-11-15 13:26:21 +01001413
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001414 return 0;
1415}
1416
Angel Pons011661c2020-11-15 18:21:35 +01001417static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001418{
1419 int lane;
1420
1421 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001422 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1423 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001424 }
1425
Angel Pons88521882020-01-05 20:21:20 +01001426 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001427
Angel Ponsffd50152020-11-12 11:03:10 +01001428 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1429 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001430
Angel Ponsa853e7a2020-12-07 12:28:38 +01001431 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001432
Angel Pons801a5cb2020-11-15 15:48:29 +01001433 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001434
Angel Ponsa853e7a2020-12-07 12:28:38 +01001435 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001436}
1437
Angel Pons011661c2020-11-15 18:21:35 +01001438static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001439{
1440 int min = data[0];
1441 int max = min;
1442 int i;
1443 for (i = 1; i < count; i++) {
1444 if (min > data[i])
1445 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001446
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001447 if (max < data[i])
1448 max = data[i];
1449 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001450 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001451 for (i = 0; i < count; i++)
1452 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001453
Angel Pons891f2bc2020-01-10 01:27:28 +01001454 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001455}
1456
Angel Pons011661c2020-11-15 18:21:35 +01001457static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001458{
Angel Pons011661c2020-11-15 18:21:35 +01001459 int tx_dq;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001460 int stats[NUM_LANES][MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001461 int lane;
1462
Angel Pons88521882020-01-05 20:21:20 +01001463 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001464
Angel Ponsffd50152020-11-12 11:03:10 +01001465 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001466
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001467 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001468
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001469 for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) {
1470 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001471 program_timings(ctrl, channel);
1472
Angel Pons011661c2020-11-15 18:21:35 +01001473 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001474
1475 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001476 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001477 }
1478 }
1479 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001480 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1481
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001482 if (rn.all || rn.length < 8) {
Angel Pons30791632020-12-12 12:28:29 +01001483 printk(BIOS_EMERG, "tx_dq write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001484 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001485 /*
1486 * With command training not being done yet, the lane can be erroneous.
1487 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001488 */
Angel Pons011661c2020-11-15 18:21:35 +01001489 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001490 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1491
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001492 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001493 printk(BIOS_EMERG, "tx_dq recovery failed\n");
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001494 return MAKE_ERR;
1495 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001496 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001497 ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle;
Angel Pons7e439c92020-12-07 11:56:01 +01001498 printram("tx_dq: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001499 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001500 }
1501 return 0;
1502}
1503
Angel Pons88521882020-01-05 20:21:20 +01001504static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001505{
1506 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001507
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001508 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1509 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001510
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001511 return ret;
1512}
1513
Angel Pons765d4652020-11-11 14:44:35 +01001514/* Each cacheline is 64 bits long */
1515static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1516{
1517 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1518}
1519
Angel Pons88521882020-01-05 20:21:20 +01001520static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001521{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301522 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001523 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
Angel Pons7c49cb82020-03-16 23:17:32 +01001524
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001525 for (j = 0; j < 16; j++)
1526 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001527
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001528 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001529
1530 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001531}
1532
Angel Pons88521882020-01-05 20:21:20 +01001533static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001534{
1535 int ret = 0;
1536 int channel;
1537 FOR_ALL_POPULATED_CHANNELS ret++;
1538 return ret;
1539}
1540
Angel Pons88521882020-01-05 20:21:20 +01001541static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001542{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301543 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001544 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
1545 unsigned int channel_step = 64 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001546
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001547 for (j = 0; j < 16; j++)
1548 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001549
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001550 for (j = 0; j < 16; j++)
1551 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001552
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001553 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001554
1555 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001556}
1557
Angel Pons42d033a2021-01-03 15:26:37 +01001558#define TX_DQS_PI_LENGTH (2 * QCLK_PI)
1559
Angel Pons820bce72020-11-14 17:02:55 +01001560static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001561{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001562 int tx_dqs;
Angel Pons42d033a2021-01-03 15:26:37 +01001563 int statistics[NUM_LANES][TX_DQS_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001564 int lane;
1565
Angel Pons58b609b2020-11-13 14:35:29 +01001566 const union gdcr_training_mod_reg training_mod = {
1567 .write_leveling_mode = 1,
1568 .training_rank_sel = slotrank,
1569 .enable_dqs_wl = 5,
1570 .odt_always_on = 1,
1571 .force_drive_enable = 1,
1572 };
1573 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001574
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001575 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1576 int bank = 1;
1577
1578 if (ctrl->rank_mirror[channel][slotrank])
1579 ddr3_mirror_mrreg(&bank, &mr1reg);
1580
1581 wait_for_iosav(channel);
1582
1583 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1584
Angel Pons42d033a2021-01-03 15:26:37 +01001585 for (tx_dqs = 0; tx_dqs < TX_DQS_PI_LENGTH; tx_dqs++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001586 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001587 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001588 }
1589 program_timings(ctrl, channel);
1590
Angel Ponsa853e7a2020-12-07 12:28:38 +01001591 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001592
1593 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001594 statistics[lane][tx_dqs] = !((MCHBAR32(lane_base[lane] +
1595 GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >>
1596 (tx_dqs % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001597 }
1598 }
1599 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001600 struct run rn = get_longest_zero_run(statistics[lane], TX_DQS_PI_LENGTH);
Angel Pons7c49cb82020-03-16 23:17:32 +01001601 /*
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001602 * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value
1603 * of tx_dqs by a small value, which might cause the 6-bit value to overflow if
Angel Pons7c49cb82020-03-16 23:17:32 +01001604 * it's close to 0x3f. Increment the value by a small offset if it's likely
1605 * to overflow, to make sure it won't overflow while running tests and bricks
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001606 * the system due to a non matching tx_dq.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001607 *
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001608 * TODO: find out why some tests (edge write discovery) increment tx_dqs.
Angel Pons7c49cb82020-03-16 23:17:32 +01001609 */
1610 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001611 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001612 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001613 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001614
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001615 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001616 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01001617 printk(BIOS_EMERG, "JEDEC write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001618 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001619
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001620 return MAKE_ERR;
1621 }
Angel Pons7e439c92020-12-07 11:56:01 +01001622 printram("tx_dqs: %d, %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01001623 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001624 }
1625 return 0;
1626}
1627
Angel Pons820bce72020-11-14 17:02:55 +01001628static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001629{
1630 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001631 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001632 if (val == 0xffffffffffffffffLL)
1633 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001634 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001635 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001636 for (i = 0; i < 8; i++)
1637 if (val << (8 * (7 - i) + 4))
1638 return -i;
1639 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001640 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001641 for (i = 0; i < 8; i++)
1642 if (val >> (8 * (7 - i) + 4))
1643 return i;
1644 }
1645 return 8;
1646}
1647
Angel Ponsbf13ef02020-11-11 18:40:06 +01001648static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001649{
1650 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001651
1652 const union gdcr_training_mod_reg training_mod = {
1653 .dq_dqs_training_res = 1,
1654 };
1655 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1656
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657 FOR_ALL_POPULATED_CHANNELS {
1658 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001659 }
1660 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1661
Angel Pons765d4652020-11-11 14:44:35 +01001662 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001663 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001664
Angel Pons88521882020-01-05 20:21:20 +01001665 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001666
Angel Ponsffd50152020-11-12 11:03:10 +01001667 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001668
Angel Ponsa853e7a2020-12-07 12:28:38 +01001669 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001670
Angel Pons8f0757e2020-11-11 23:03:36 +01001671 const struct iosav_ssq rd_sequence[] = {
1672 /* DRAM command PREA */
1673 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001674 .sp_cmd_ctrl = {
1675 .command = IOSAV_PRE,
1676 .ranksel_ap = 1,
1677 },
1678 .subseq_ctrl = {
1679 .cmd_executions = 1,
1680 .cmd_delay_gap = 3,
1681 .post_ssq_wait = ctrl->tRP,
1682 .data_direction = SSQ_NA,
1683 },
1684 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +01001685 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +02001686 .rowbits = 6,
1687 .bank = 0,
1688 .rank = slotrank,
1689 },
1690 .addr_update = {
1691 .addr_wrap = 18,
1692 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001693 },
1694 /* DRAM command ACT */
1695 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001696 .sp_cmd_ctrl = {
1697 .command = IOSAV_ACT,
1698 .ranksel_ap = 1,
1699 },
1700 .subseq_ctrl = {
1701 .cmd_executions = 1,
1702 .cmd_delay_gap = 3,
1703 .post_ssq_wait = ctrl->tRCD,
1704 .data_direction = SSQ_NA,
1705 },
1706 .sp_cmd_addr = {
1707 .address = 0,
1708 .rowbits = 6,
1709 .bank = 0,
1710 .rank = slotrank,
1711 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001712 },
1713 /* DRAM command RD */
1714 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001715 .sp_cmd_ctrl = {
1716 .command = IOSAV_RD,
1717 .ranksel_ap = 3,
1718 },
1719 .subseq_ctrl = {
1720 .cmd_executions = 1,
1721 .cmd_delay_gap = 3,
1722 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001723 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001724 ctrl->timings[channel][slotrank].io_latency,
1725 .data_direction = SSQ_RD,
1726 },
1727 .sp_cmd_addr = {
1728 .address = 8,
1729 .rowbits = 6,
1730 .bank = 0,
1731 .rank = slotrank,
1732 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001733 },
1734 };
1735 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001736
Angel Ponsa853e7a2020-12-07 12:28:38 +01001737 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001738
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001739 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001740 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001741 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001742 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001743
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001744 old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1745 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs +=
Angel Pons42d033a2021-01-03 15:26:37 +01001746 get_dqs_flyby_adjust(res) * QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001747
1748 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons7e439c92020-12-07 11:56:01 +01001749 printram("Bval+: %d, %d, %d, % 4d -> % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001750 old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001751 }
1752 }
Angel Pons88521882020-01-05 20:21:20 +01001753 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001754}
1755
Angel Pons7d115132020-11-14 01:44:44 +01001756static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001757{
Angel Pons7d115132020-11-14 01:44:44 +01001758 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001759
Angel Pons7d115132020-11-14 01:44:44 +01001760 FOR_ALL_POPULATED_CHANNELS {
1761 /* choose an existing rank */
1762 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001763
Angel Pons7d115132020-11-14 01:44:44 +01001764 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001765
Angel Ponsa853e7a2020-12-07 12:28:38 +01001766 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001767
Angel Pons7d115132020-11-14 01:44:44 +01001768 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1769 }
1770
1771 /* Refresh disable */
1772 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1773
1774 FOR_ALL_POPULATED_CHANNELS {
1775 /* Execute the same command queue */
Angel Ponsa853e7a2020-12-07 12:28:38 +01001776 iosav_run_once_and_wait(channel);
Angel Pons7d115132020-11-14 01:44:44 +01001777 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001778}
1779
Angel Pons7c49cb82020-03-16 23:17:32 +01001780/*
1781 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001782 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001783 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1784 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1785 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1786 * CLK/ADDR/CMD signals have the same routing delay.
1787 *
1788 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1789 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1790 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001791 */
Angel Pons820bce72020-11-14 17:02:55 +01001792static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001793{
Angel Pons820bce72020-11-14 17:02:55 +01001794 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001795
Angel Pons7d115132020-11-14 01:44:44 +01001796 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001797
Angel Pons7c49cb82020-03-16 23:17:32 +01001798 /* Enable write leveling on all ranks
1799 Disable all DQ outputs
1800 Only NOP is allowed in this mode */
1801 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1802 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001803 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001804
Angel Ponsa1f17142020-11-15 12:50:03 +01001805 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001806 const union gdcr_training_mod_reg training_mod = {
1807 .write_leveling_mode = 1,
1808 .enable_dqs_wl = 5,
1809 .odt_always_on = 1,
1810 .force_drive_enable = 1,
1811 };
1812 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813
1814 toggle_io_reset();
1815
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001816 /* Set any valid value for tx_dqs, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001817 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001818 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001819 if (err)
1820 return err;
1821 }
1822
Angel Pons7c49cb82020-03-16 23:17:32 +01001823 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001824 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001825 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826
Angel Pons88521882020-01-05 20:21:20 +01001827 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001828
1829 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001830 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001831
Angel Pons7c49cb82020-03-16 23:17:32 +01001832 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001833 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001834
1835 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001836 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001837 MCHBAR32(IOSAV_STATUS_ch(channel));
1838 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001839
Angel Ponsffd50152020-11-12 11:03:10 +01001840 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001841
Angel Ponsa853e7a2020-12-07 12:28:38 +01001842 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001843 }
1844
1845 toggle_io_reset();
1846
Angel Pons820bce72020-11-14 17:02:55 +01001847 return 0;
1848}
1849
1850int write_training(ramctr_timing *ctrl)
1851{
Angel Ponsc6742232020-11-15 13:26:21 +01001852 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001853 int err;
1854
Angel Pons4d192822020-12-12 13:54:37 +01001855 /*
1856 * Set the DEC_WRD bit, required for the write flyby algorithm.
1857 * Needs to be done before starting the write training procedure.
1858 */
Angel Pons820bce72020-11-14 17:02:55 +01001859 FOR_ALL_POPULATED_CHANNELS
1860 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1861
Angel Pons4c76d252020-11-15 13:06:53 +01001862 printram("CPE\n");
1863
Angel Pons820bce72020-11-14 17:02:55 +01001864 err = jedec_write_leveling(ctrl);
1865 if (err)
1866 return err;
1867
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001868 printram("CPF\n");
1869
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001870 FOR_ALL_POPULATED_CHANNELS {
1871 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001872 }
1873
1874 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001875 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001876 if (err)
1877 return err;
1878 }
1879
1880 FOR_ALL_POPULATED_CHANNELS
1881 program_timings(ctrl, channel);
1882
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001883 /* measure and adjust tx_dqs timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001884 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001885
1886 FOR_ALL_POPULATED_CHANNELS
1887 program_timings(ctrl, channel);
1888
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001889 return 0;
1890}
1891
Angel Ponsbf13ef02020-11-11 18:40:06 +01001892static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001893{
1894 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001895 int tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001896 int lanes_ok = 0;
1897 int ctr = 0;
1898 int lane;
1899
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001900 for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001901 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001902 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
1903 saved_rt.lanes[lane].tx_dq + tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001904 }
1905 program_timings(ctrl, channel);
1906 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001907 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001908 }
1909
Angel Pons765d4652020-11-11 14:44:35 +01001910 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001911 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001912
Angel Pons88521882020-01-05 20:21:20 +01001913 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001914
Angel Ponsffd50152020-11-12 11:03:10 +01001915 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001916
1917 /* Program LFSR for the RD/WR subsequences */
1918 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1919 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001920
Angel Ponsa853e7a2020-12-07 12:28:38 +01001921 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001922
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001923 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001924 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001925
1926 if (r32 == 0)
1927 lanes_ok |= 1 << lane;
1928 }
1929 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001930 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001931 break;
1932 }
1933
1934 ctrl->timings[channel][slotrank] = saved_rt;
1935
Patrick Rudolphdd662872017-10-28 18:20:11 +02001936 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001937}
1938
Angel Pons88521882020-01-05 20:21:20 +01001939static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001940{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301941 unsigned int i, j;
Angel Pons5db1b152020-12-13 16:37:53 +01001942 unsigned int offset = get_precedening_channels(ctrl, channel) * 64;
1943 unsigned int step = 64 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001944
1945 if (patno) {
1946 u8 base8 = 0x80 >> ((patno - 1) % 8);
1947 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1948 for (i = 0; i < 32; i++) {
1949 for (j = 0; j < 16; j++) {
1950 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001951
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001952 if (invert[patno - 1][i] & (1 << (j / 2)))
1953 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001954
1955 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001956 }
1957 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001958 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001959 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1960 for (j = 0; j < 16; j++) {
1961 const u32 val = pattern[i][j];
1962 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1963 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001964 }
1965 sfence();
1966 }
Angel Pons765d4652020-11-11 14:44:35 +01001967
1968 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001969}
1970
Angel Pons88521882020-01-05 20:21:20 +01001971static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972{
Angel Pons7d115132020-11-14 01:44:44 +01001973 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974
Angel Pons7c49cb82020-03-16 23:17:32 +01001975 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001976 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001977
1978 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001979 dram_mrscommands(ctrl);
1980
1981 toggle_io_reset();
1982}
1983
Angel Pons42d033a2021-01-03 15:26:37 +01001984#define CT_MIN_PI (-CCC_MAX_PI)
1985#define CT_MAX_PI (+CCC_MAX_PI + 1)
Angel Ponsbf13ef02020-11-11 18:40:06 +01001986#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1987
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001988#define MIN_C320C_LEN 13
1989
1990static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
1991{
1992 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
1993 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001994 int command_pi;
1995 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001996 int delta = 0;
1997
1998 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
1999
2000 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002001 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002002 }
2003
2004 ctrl->cmd_stretch[channel] = cmd_stretch;
2005
Angel Pons7a612742020-11-12 13:34:03 +01002006 const union tc_rap_reg tc_rap = {
2007 .tRRD = ctrl->tRRD,
2008 .tRTP = ctrl->tRTP,
2009 .tCKE = ctrl->tCKE,
2010 .tWTR = ctrl->tWTR,
2011 .tFAW = ctrl->tFAW,
2012 .tWR = ctrl->tWR,
2013 .tCMD = ctrl->cmd_stretch[channel],
2014 };
2015 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002016
2017 if (ctrl->cmd_stretch[channel] == 2)
2018 delta = 2;
2019 else if (ctrl->cmd_stretch[channel] == 0)
2020 delta = 4;
2021
2022 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002023 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002024 }
2025
Angel Ponsbf13ef02020-11-11 18:40:06 +01002026 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002027 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002028 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002029 }
2030 program_timings(ctrl, channel);
2031 reprogram_320c(ctrl);
2032 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002033 stat[slotrank][command_pi - CT_MIN_PI] =
2034 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002035 }
2036 }
2037 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002038 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002039
Angel Ponsbf13ef02020-11-11 18:40:06 +01002040 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Angel Pons7e439c92020-12-07 11:56:01 +01002041 printram("cmd_stretch: %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01002042 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002043
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002044 if (rn.all || rn.length < MIN_C320C_LEN) {
2045 FOR_ALL_POPULATED_RANKS {
2046 ctrl->timings[channel][slotrank] =
2047 saved_timings[channel][slotrank];
2048 }
2049 return MAKE_ERR;
2050 }
2051 }
2052
2053 return 0;
2054}
2055
Angel Pons7c49cb82020-03-16 23:17:32 +01002056/*
2057 * Adjust CMD phase shift and try multiple command rates.
2058 * A command rate of 2T doubles the time needed for address and command decode.
2059 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002060int command_training(ramctr_timing *ctrl)
2061{
2062 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002063
2064 FOR_ALL_POPULATED_CHANNELS {
2065 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002066 }
2067
2068 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002069 int cmdrate, err;
2070
2071 /*
2072 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002073 * Issue:
Angel Pons30791632020-12-12 12:28:29 +01002074 * While command training seems to succeed, raminit will fail in write training.
Angel Pons7c49cb82020-03-16 23:17:32 +01002075 *
2076 * Workaround:
2077 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2078 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002079 *
2080 * Single DIMM per channel:
2081 * Try command rate 1T and 2T
2082 */
2083 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002084 if (ctrl->tCMD)
2085 /* XMP gives the CMD rate in clock ticks, not ns */
2086 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002087
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002088 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002089 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2090
2091 if (!err)
2092 break;
2093 }
2094
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002095 if (err) {
Angel Pons30791632020-12-12 12:28:29 +01002096 printk(BIOS_EMERG, "Command training failed: %d\n", channel);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002097 return err;
2098 }
2099
Angel Pons891f2bc2020-01-10 01:27:28 +01002100 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002101 }
2102
2103 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002104 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002105
2106 reprogram_320c(ctrl);
2107 return 0;
2108}
2109
Angel Pons4c79f932020-11-14 01:26:52 +01002110static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111{
Angel Pons96a06dd2020-11-14 00:33:18 +01002112 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002113 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002114 int lane;
2115
Angel Pons96a06dd2020-11-14 00:33:18 +01002116 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002117 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002118 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi;
2119 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002120 }
2121 program_timings(ctrl, channel);
2122
2123 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002124 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2125 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002126 }
2127
Angel Pons88521882020-01-05 20:21:20 +01002128 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002129
Angel Ponsffd50152020-11-12 11:03:10 +01002130 iosav_write_read_mpr_sequence(
2131 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002132
Angel Ponsa853e7a2020-12-07 12:28:38 +01002133 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002134
2135 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002136 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002137 }
2138 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002139
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002140 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002141 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002142 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002143
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002144 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002145 printk(BIOS_EMERG, "Read MPR training failed: %d, %d, %d\n", channel,
Angel Pons7c49cb82020-03-16 23:17:32 +01002146 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002147 return MAKE_ERR;
2148 }
Angel Pons7e439c92020-12-07 11:56:01 +01002149 printram("eval %d, %d, %d: % 4d\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002150 }
2151 return 0;
2152}
2153
Angel Pons60971dc2020-11-14 00:49:38 +01002154static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2155{
2156 int slotrank, lane;
2157
2158 fill_pattern0(ctrl, channel, 0, 0);
2159 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002160 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002161 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2162 }
2163
2164 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002165 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16;
2166 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16;
Angel Pons60971dc2020-11-14 00:49:38 +01002167 }
2168
2169 program_timings(ctrl, channel);
2170
2171 FOR_ALL_POPULATED_RANKS {
2172 wait_for_iosav(channel);
2173
2174 iosav_write_read_mpr_sequence(
2175 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2176
Angel Ponsa853e7a2020-12-07 12:28:38 +01002177 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002178 }
2179
2180 /* XXX: check any measured value ? */
2181
2182 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002183 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48;
2184 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48;
Angel Pons60971dc2020-11-14 00:49:38 +01002185 }
2186
2187 program_timings(ctrl, channel);
2188
2189 FOR_ALL_POPULATED_RANKS {
2190 wait_for_iosav(channel);
2191
2192 iosav_write_read_mpr_sequence(
2193 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2194
Angel Ponsa853e7a2020-12-07 12:28:38 +01002195 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002196 }
2197
2198 /* XXX: check any measured value ? */
2199
2200 FOR_ALL_LANES {
2201 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2202 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2203 }
2204}
2205
Angel Pons4c79f932020-11-14 01:26:52 +01002206int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002207{
2208 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2209 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2210 int channel, slotrank, lane;
2211 int err;
2212
Angel Pons88521882020-01-05 20:21:20 +01002213 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002214
2215 toggle_io_reset();
2216
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002217 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002218 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002219
2220 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002221 }
2222
Angel Pons0c3936e2020-03-22 12:49:27 +01002223 /*
2224 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2225 * also use a single loop. It would seem that it is a debugging configuration.
2226 */
Angel Pons5db1b152020-12-13 16:37:53 +01002227 MCHBAR32(IOSAV_DC_MASK) = 3 << 8;
2228 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002229
2230 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002231 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002232 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002233 if (err)
2234 return err;
2235 }
2236
Angel Pons5db1b152020-12-13 16:37:53 +01002237 MCHBAR32(IOSAV_DC_MASK) = 2 << 8;
2238 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002239
2240 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002241 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002242 rising_edges[channel][slotrank]);
2243 if (err)
2244 return err;
2245 }
2246
Angel Pons88521882020-01-05 20:21:20 +01002247 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002248
2249 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002250 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002251 falling_edges[channel][slotrank][lane];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002252 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002253 rising_edges[channel][slotrank][lane];
2254 }
2255
2256 FOR_ALL_POPULATED_CHANNELS {
2257 program_timings(ctrl, channel);
2258 }
2259
Angel Pons50a6fe72020-11-14 01:18:14 +01002260 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002261 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002262 }
2263 return 0;
2264}
2265
Angel Pons08f749d2020-11-17 16:50:56 +01002266static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002267{
Angel Pons08f749d2020-11-17 16:50:56 +01002268 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2269
Angel Pons7c49cb82020-03-16 23:17:32 +01002270 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002271 int lower[NUM_LANES];
2272 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002273 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002274
2275 FOR_ALL_LANES {
2276 lower[lane] = 0;
2277 upper[lane] = MAX_EDGE_TIMING;
2278 }
2279
Angel Pons08f749d2020-11-17 16:50:56 +01002280 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002281 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002282 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002283 };
2284 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2285 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002286
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002287 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2288 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002289 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002290
Angel Pons08f749d2020-11-17 16:50:56 +01002291 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002292 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002293 ctrl->timings[channel][slotrank].lanes[lane]
2294 .rx_dqs_p = read_pi;
2295 ctrl->timings[channel][slotrank].lanes[lane]
2296 .rx_dqs_n = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002297 }
2298 program_timings(ctrl, channel);
2299
2300 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002301 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2302 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002303 }
Angel Pons88521882020-01-05 20:21:20 +01002304 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002305
Angel Ponsffd50152020-11-12 11:03:10 +01002306 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307
Angel Ponsa853e7a2020-12-07 12:28:38 +01002308 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002309
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002310 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002311 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002312 }
2313
Angel Pons7c49cb82020-03-16 23:17:32 +01002314 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002315 raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002316 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002317
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002318 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002319 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002320 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002321
2322 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2323 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002324
2325 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2326
Angel Pons7e439c92020-12-07 11:56:01 +01002327 printram("edges: %d, %d, %d: % 4d-% 4d-% 4d, "
2328 "% 4d-% 4d\n", channel, slotrank, i, rn.start,
Angel Pons7c49cb82020-03-16 23:17:32 +01002329 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002331
2332 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2333 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2334
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002335 edges[lane] = (lower[lane] + upper[lane]) / 2;
2336 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons30791632020-12-12 12:28:29 +01002337 printk(BIOS_EMERG, "Aggressive read training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002338 "%d, %d, %d\n", channel, slotrank, lane);
2339
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002340 return MAKE_ERR;
2341 }
2342 }
2343 }
2344 }
2345
Angel Ponsa93f46e2020-11-17 16:54:01 +01002346 /* Restore nominal Vref after training */
2347 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002348 printram("CPA\n");
2349 return 0;
2350}
2351
Angel Pons08f749d2020-11-17 16:50:56 +01002352int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002353{
2354 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002355 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2356 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002357
Angel Pons7c49cb82020-03-16 23:17:32 +01002358 /*
2359 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2360 * also use a single loop. It would seem that it is a debugging configuration.
2361 */
Angel Pons5db1b152020-12-13 16:37:53 +01002362 MCHBAR32(IOSAV_DC_MASK) = 3 << 8;
2363 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002364
2365 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002366 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002367 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002368 if (err)
2369 return err;
2370 }
2371
Angel Pons5db1b152020-12-13 16:37:53 +01002372 MCHBAR32(IOSAV_DC_MASK) = 2 << 8;
2373 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002374
2375 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002376 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002377 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002378 if (err)
2379 return err;
2380 }
2381
Angel Pons88521882020-01-05 20:21:20 +01002382 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002383
2384 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002385 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Angel Pons7c49cb82020-03-16 23:17:32 +01002386 falling_edges[channel][slotrank][lane];
2387
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002388 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Angel Pons7c49cb82020-03-16 23:17:32 +01002389 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002390 }
2391
2392 FOR_ALL_POPULATED_CHANNELS
2393 program_timings(ctrl, channel);
2394
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002395 return 0;
2396}
2397
Angel Pons2a7d7522020-11-19 12:49:07 +01002398static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002399{
Angel Pons88521882020-01-05 20:21:20 +01002400 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002401
Angel Ponsffd50152020-11-12 11:03:10 +01002402 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002403
Angel Ponsa853e7a2020-12-07 12:28:38 +01002404 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002405}
2406
Angel Pons2a7d7522020-11-19 12:49:07 +01002407static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002408{
Angel Pons2a7d7522020-11-19 12:49:07 +01002409 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24);
2410 udelay(2);
2411}
2412
2413int aggressive_write_training(ramctr_timing *ctrl)
2414{
2415 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002416 int i, pat;
2417
2418 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2419 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2420 int channel, slotrank, lane;
2421
Angel Pons9fbb1b02020-11-19 12:53:36 +01002422 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2423 if (!IS_IVY_CPU(ctrl->cpu))
2424 return 0;
2425
2426 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2427 return 0;
2428
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002429 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2430 lower[channel][slotrank][lane] = 0;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002431 upper[channel][slotrank][lane] = MAX_TX_DQ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002432 }
2433
Angel Pons2a7d7522020-11-19 12:49:07 +01002434 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2435 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2436
2437 if (enable_iosav_opt)
2438 MCHBAR32(MCMNTS_SPARE) = 1;
2439
Angel Pons30791632020-12-12 12:28:29 +01002440 printram("Aggresive write training:\n");
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002441
Angel Pons2a7d7522020-11-19 12:49:07 +01002442 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002443 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002444 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002445
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002446 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2447 FOR_ALL_POPULATED_RANKS {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002448 int tx_dq;
2449 u32 raw_stats[MAX_TX_DQ + 1];
2450 int stats[MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002451
2452 /* Make sure rn.start < rn.end */
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002453 stats[MAX_TX_DQ] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002454
2455 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002456
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002457 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002458 FOR_ALL_LANES {
2459 ctrl->timings[channel][slotrank]
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002460 .lanes[lane].tx_dq = tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01002461 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002462 program_timings(ctrl, channel);
2463
Angel Pons2a7d7522020-11-19 12:49:07 +01002464 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002465
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002466 raw_stats[tx_dq] = MCHBAR32(
Angel Pons098240eb2020-03-22 12:55:32 +01002467 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002468 }
2469 FOR_ALL_LANES {
2470 struct run rn;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002471 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
2472 stats[tx_dq] = !!(raw_stats[tx_dq]
Angel Pons7c49cb82020-03-16 23:17:32 +01002473 & (1 << lane));
2474 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002475
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002476 rn = get_longest_zero_run(stats, MAX_TX_DQ + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002477 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002478 printk(BIOS_EMERG, "Aggressive "
2479 "write training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002480 "%d, %d, %d\n", channel,
2481 slotrank, lane);
2482
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002483 return MAKE_ERR;
2484 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002485 printram("tx_dq: %d, %d, %d: "
Angel Pons7e439c92020-12-07 11:56:01 +01002486 "% 4d-% 4d-% 4d, "
2487 "% 4d-% 4d\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002488 i, rn.start, rn.middle, rn.end,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002489 rn.start + ctrl->tx_dq_offset[i],
2490 rn.end - ctrl->tx_dq_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002491
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002492 lower[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002493 MAX(rn.start + ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002494 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002495
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002496 upper[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002497 MIN(rn.end - ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002498 upper[channel][slotrank][lane]);
2499
2500 }
2501 }
2502 }
2503 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002504 }
2505
Angel Pons2a7d7522020-11-19 12:49:07 +01002506 FOR_ALL_CHANNELS {
2507 /* Restore nominal write Vref after training */
2508 set_write_vref(channel, 0);
2509 }
2510
2511 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2512 if (enable_iosav_opt)
2513 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002514
2515 printram("CPB\n");
2516
2517 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7e439c92020-12-07 11:56:01 +01002518 printram("tx_dq %d, %d, %d: % 4d\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519 (lower[channel][slotrank][lane] +
2520 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002521
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002522 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002523 (lower[channel][slotrank][lane] +
2524 upper[channel][slotrank][lane]) / 2;
2525 }
2526 FOR_ALL_POPULATED_CHANNELS {
2527 program_timings(ctrl, channel);
2528 }
2529 return 0;
2530}
2531
Angel Pons88521882020-01-05 20:21:20 +01002532void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002533{
2534 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002535 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002536
2537 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2538 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002539 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002540 FOR_ALL_LANES mat =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002541 MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002542 printram("normalize %d, %d, %d: mat %d\n",
2543 channel, slotrank, lane, mat);
2544
Felix Heldef4fe3e2019-12-31 14:15:05 +01002545 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002546 printram("normalize %d, %d, %d: delta %d\n",
2547 channel, slotrank, lane, delta);
2548
Angel Pons88521882020-01-05 20:21:20 +01002549 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002550 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002551 }
2552
2553 FOR_ALL_POPULATED_CHANNELS {
2554 program_timings(ctrl, channel);
2555 }
2556}
2557
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002558int channel_test(ramctr_timing *ctrl)
2559{
2560 int channel, slotrank, lane;
2561
2562 slotrank = 0;
2563 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002564 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002565 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002566 return MAKE_ERR;
2567 }
2568 FOR_ALL_POPULATED_CHANNELS {
2569 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002570 }
2571
2572 for (slotrank = 0; slotrank < 4; slotrank++)
2573 FOR_ALL_CHANNELS
2574 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2575 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002576 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2577 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002578 }
Angel Pons88521882020-01-05 20:21:20 +01002579 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002580
Angel Ponsffd50152020-11-12 11:03:10 +01002581 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002582
Angel Ponsa853e7a2020-12-07 12:28:38 +01002583 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002584
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002585 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002586 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002587 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2588 channel, slotrank, lane);
2589 return MAKE_ERR;
2590 }
2591 }
2592 return 0;
2593}
2594
Patrick Rudolphdd662872017-10-28 18:20:11 +02002595void channel_scrub(ramctr_timing *ctrl)
2596{
2597 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002598 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002599
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002600 FOR_ALL_POPULATED_CHANNELS {
2601 wait_for_iosav(channel);
2602 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002603 }
2604
2605 /*
2606 * During runtime the "scrubber" will periodically scan through the memory in the
2607 * physical address space, to identify and fix CRC errors.
2608 * The following loops writes to every DRAM address, setting the ECC bits to the
2609 * correct value. A read from this location will no longer return a CRC error,
2610 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002611 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002612 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2613 * and firmware running in x86_32.
2614 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002615 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2616 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002617 for (bank = 0; bank < 8; bank++) {
2618 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002619
Angel Pons8f0757e2020-11-11 23:03:36 +01002620 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2621 const struct iosav_ssq sequence[] = {
2622 /*
2623 * DRAM command ACT
2624 * Opens the row for writing.
2625 */
2626 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002627 .sp_cmd_ctrl = {
2628 .command = IOSAV_ACT,
2629 .ranksel_ap = 1,
2630 },
2631 .subseq_ctrl = {
2632 .cmd_executions = 1,
2633 .cmd_delay_gap = gap,
2634 .post_ssq_wait = ctrl->tRCD,
2635 .data_direction = SSQ_NA,
2636 },
2637 .sp_cmd_addr = {
2638 .address = row,
2639 .rowbits = 6,
2640 .bank = bank,
2641 .rank = slotrank,
2642 },
2643 .addr_update = {
2644 .inc_addr_1 = 1,
2645 .addr_wrap = 18,
2646 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002647 },
2648 /*
2649 * DRAM command WR
2650 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2651 * bytes.
2652 */
2653 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002654 .sp_cmd_ctrl = {
2655 .command = IOSAV_WR,
2656 .ranksel_ap = 1,
2657 },
2658 .subseq_ctrl = {
2659 .cmd_executions = 129,
2660 .cmd_delay_gap = 4,
2661 .post_ssq_wait = ctrl->tWTR +
2662 ctrl->CWL + 8,
2663 .data_direction = SSQ_WR,
2664 },
2665 .sp_cmd_addr = {
2666 .address = row,
2667 .rowbits = 0,
2668 .bank = bank,
2669 .rank = slotrank,
2670 },
2671 .addr_update = {
2672 .inc_addr_8 = 1,
2673 .addr_wrap = 9,
2674 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002675 },
2676 /*
2677 * DRAM command PRE
2678 * Closes the row.
2679 */
2680 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002681 .sp_cmd_ctrl = {
2682 .command = IOSAV_PRE,
2683 .ranksel_ap = 1,
2684 },
2685 .subseq_ctrl = {
2686 .cmd_executions = 1,
2687 .cmd_delay_gap = 4,
2688 .post_ssq_wait = ctrl->tRP,
2689 .data_direction = SSQ_NA,
2690 },
2691 .sp_cmd_addr = {
2692 .address = 0,
2693 .rowbits = 6,
2694 .bank = bank,
2695 .rank = slotrank,
2696 },
2697 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002698 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002699 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002700 },
2701 };
2702 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002703
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002704 iosav_run_queue(channel, 16, 0);
2705
2706 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002707 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002708 }
2709 }
2710}
2711
Angel Pons88521882020-01-05 20:21:20 +01002712void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002713{
2714 int channel;
2715
Angel Pons7c49cb82020-03-16 23:17:32 +01002716 /* 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 +01002717 static u32 seeds[NUM_CHANNELS][3] = {
2718 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2719 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2720 };
2721 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002722 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002723 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2724 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2725 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002726 }
2727}
2728
Angel Pons89ae6b82020-03-21 13:23:32 +01002729void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002730{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002731 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002732 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002733 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002734 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002735 }
2736}
2737
Angel Pons88521882020-01-05 20:21:20 +01002738void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002739{
2740 int channel;
2741
2742 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002743 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002744 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002745 }
2746
2747 udelay(1);
2748
2749 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002750 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002751 }
2752}
2753
Angel Pons7c49cb82020-03-16 23:17:32 +01002754void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002755{
Angel Pons11463322020-11-19 11:04:28 +01002756 /* Use a larger delay when running fast to improve stability */
2757 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2758
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002759 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002760
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002761 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002762 int min_pi = 10000;
2763 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002764
2765 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002766 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2767 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002768 }
2769
Angel Pons7a612742020-11-12 13:34:03 +01002770 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002771
Angel Pons7a612742020-11-12 13:34:03 +01002772 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002773
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002774 dram_odt_stretch(ctrl, channel);
2775
Angel Pons7a612742020-11-12 13:34:03 +01002776 const union tc_rwp_reg tc_rwp = {
2777 .tRRDR = 0,
2778 .tRRDD = val,
2779 .tWWDR = val,
2780 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002781 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002782 .tWRDRDD = tWRDRDD,
2783 .tRWSR = 2,
2784 .dec_wrd = 1,
2785 };
2786 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002787 }
2788}
2789
Angel Pons88521882020-01-05 20:21:20 +01002790void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002791{
2792 int channel;
2793 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002794 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2795 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002796 }
2797}
2798
Angel Pons7c49cb82020-03-16 23:17:32 +01002799/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2800static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002801{
Angel Pons88521882020-01-05 20:21:20 +01002802 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002803}
2804
Angel Pons7c49cb82020-03-16 23:17:32 +01002805/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002806void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002807{
2808 int channel;
2809 int t1_cycles = 0, t1_ns = 0, t2_ns;
2810 int t3_ns;
2811 u32 r32;
2812
Angel Pons7c49cb82020-03-16 23:17:32 +01002813 /* FIXME: This register only exists on Ivy Bridge */
2814 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002815
Angel Pons7a612742020-11-12 13:34:03 +01002816 FOR_ALL_CHANNELS {
2817 union tc_othp_reg tc_othp = {
2818 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2819 };
2820 tc_othp.tCPDED = 1;
2821 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2822 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002823
Angel Pons09fc4b92020-11-19 12:02:07 +01002824 /* 64 DCLKs until idle, decision per rank */
2825 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002826
Felix Heldf9b826a2018-07-30 17:56:52 +02002827 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002828 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002829
Angel Ponsc728e252021-01-03 16:47:09 +01002830 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff;
2831 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002832
2833 FOR_ALL_CHANNELS {
2834 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002835 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002836 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002837 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002838 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002839 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002840 case 1:
2841 case 4:
2842 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002843 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002844 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002845 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002846 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002847 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002848 break;
2849 }
2850 }
2851
Felix Held50b7ed22019-12-30 20:41:54 +01002852 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002853 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002854 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002855
Angel Pons7a612742020-11-12 13:34:03 +01002856 FOR_ALL_CHANNELS {
2857 union tc_rfp_reg tc_rfp = {
2858 .raw = MCHBAR32(TC_RFP_ch(channel)),
2859 };
2860 tc_rfp.refresh_2x_control = 1;
2861 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2862 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002863
Angel Ponsdc5539f2020-11-12 12:44:25 +01002864 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2865 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002866 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002867
Angel Pons7c49cb82020-03-16 23:17:32 +01002868 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002869 FOR_ALL_POPULATED_CHANNELS
2870 break;
2871
Angel Pons88521882020-01-05 20:21:20 +01002872 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2873 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002874 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002875 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002876 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002877 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002878 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002879 t1_ns += 500;
2880
Angel Pons88521882020-01-05 20:21:20 +01002881 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002882 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002883 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002884 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002885 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002886 t3_ns = 500;
2887 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002888
2889 /* The graphics driver will use these watermark values */
2890 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002891 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002892 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2893 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002894}
2895
Angel Pons88521882020-01-05 20:21:20 +01002896void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002897{
Angel Ponsc6742232020-11-15 13:26:21 +01002898 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002899
Angel Pons7c49cb82020-03-16 23:17:32 +01002900 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002901 const union tc_rap_reg tc_rap = {
2902 .tRRD = ctrl->tRRD,
2903 .tRTP = ctrl->tRTP,
2904 .tCKE = ctrl->tCKE,
2905 .tWTR = ctrl->tWTR,
2906 .tFAW = ctrl->tFAW,
2907 .tWR = ctrl->tWR,
2908 .tCMD = ctrl->cmd_stretch[channel],
2909 };
2910 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002911 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002912
2913 udelay(1);
2914
2915 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002916 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002917 }
2918
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002919 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002920 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002921
2922 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002923 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002924 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002925 }
2926
2927 printram("CPE\n");
2928
Angel Pons88521882020-01-05 20:21:20 +01002929 MCHBAR32(GDCRTRAININGMOD) = 0;
2930 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002931
2932 printram("CP5b\n");
2933
2934 FOR_ALL_POPULATED_CHANNELS {
2935 program_timings(ctrl, channel);
2936 }
2937
2938 u32 reg, addr;
2939
Angel Pons7c49cb82020-03-16 23:17:32 +01002940 /* Poll for RCOMP */
2941 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2942 ;
2943
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002944 do {
Angel Pons88521882020-01-05 20:21:20 +01002945 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002946 } while ((reg & 0x14) == 0);
2947
Angel Pons7c49cb82020-03-16 23:17:32 +01002948 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002949 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002950 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002951
Angel Pons7c49cb82020-03-16 23:17:32 +01002952 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002953 udelay(500);
2954
2955 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002956 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002957 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002958 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002959 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002960 MCHBAR32(addr) = reg;
2961
Angel Pons7c49cb82020-03-16 23:17:32 +01002962 /* Wait 10ns for ranks to settle */
2963 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002964
2965 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
2966 MCHBAR32(addr) = reg;
2967
Angel Pons7c49cb82020-03-16 23:17:32 +01002968 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002969 write_reset(ctrl);
2970 }
2971
Angel Pons7c49cb82020-03-16 23:17:32 +01002972 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002973 dram_mrscommands(ctrl);
2974
2975 printram("CP5c\n");
2976
Angel Pons88521882020-01-05 20:21:20 +01002977 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002978
2979 FOR_ALL_CHANNELS {
Angel Pons5db1b152020-12-13 16:37:53 +01002980 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002981 udelay(2);
2982 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002983}