blob: 3c60a9f5d09c6e6889b7f29cf9d55ae6ade3ea88 [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 {
433 // 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:
755 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
756 case 60:
757 return (1 << 2); // RZQ/4
758 case 120:
759 return (1 << 6); // RZQ/2
760 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 Pons88521882020-01-05 20:21:20 +0100924void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100925{
Angel Pons7584e552020-11-19 21:34:32 +0100926 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100927 int lane;
928 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100929
Angel Pons7584e552020-11-19 21:34:32 +0100930 u32 ctl_delay[NUM_SLOTS] = { 0 };
931 int cmd_delay = 0;
932
933 /* Enable CLK XOVER */
934 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
935 u32 clk_logic_dly = 0;
936
937 /*
938 * Apply command delay if desired setting is negative. Find the
939 * most negative value: 'cmd_delay' will be the absolute value.
940 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100941 FOR_ALL_POPULATED_RANKS {
Angel Pons7584e552020-11-19 21:34:32 +0100942 if (cmd_delay < -ctrl->timings[channel][slotrank].pi_coding)
943 cmd_delay = -ctrl->timings[channel][slotrank].pi_coding;
944 }
945 if (cmd_delay < 0) {
946 printk(BIOS_ERR, "C%d command delay underflow: %d\n", channel, cmd_delay);
947 cmd_delay = 0;
948 }
949 if (cmd_delay >= 128) {
950 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
951 cmd_delay = 127;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100952 }
953
Angel Pons7584e552020-11-19 21:34:32 +0100954 /* Apply control and clock delay if desired setting is positive */
955 if (cmd_delay == 0) {
956 for (slot = 0; slot < NUM_SLOTS; slot++) {
957 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
958 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
959
960 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
961
962 if (slot_map & 1)
963 ctl_delay[slot] += pi_coding_0 + cmd_delay;
964
965 if (slot_map & 2)
966 ctl_delay[slot] += pi_coding_1 + cmd_delay;
967
968 /* If both ranks in a slot are populated, use the average */
969 if (slot_map == 3)
970 ctl_delay[slot] /= 2;
971
972 if (ctl_delay[slot] >= 128) {
973 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
974 channel, slot, ctl_delay[slot]);
975 ctl_delay[slot] = 127;
976 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100977 }
Angel Pons7584e552020-11-19 21:34:32 +0100978 FOR_ALL_POPULATED_RANKS {
979 u32 clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
980
981 if (clk_delay >= 128) {
982 printk(BIOS_ERR, "C%dR%d clock delay overflow: %d\n",
983 channel, slotrank, clk_delay);
984 clk_delay = 127;
985 }
986
987 clk_pi_coding |= (clk_delay % 64) << (6 * slotrank);
988 clk_logic_dly |= (clk_delay / 64) << slotrank;
989 }
990 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100991
Angel Pons7c49cb82020-03-16 23:17:32 +0100992 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100993 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
994 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
995 };
Angel Pons7584e552020-11-19 21:34:32 +0100996 cmd_pi_coding.cmd_pi_code = cmd_delay % 64;
997 cmd_pi_coding.cmd_logic_delay = cmd_delay / 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100998
Angel Pons7584e552020-11-19 21:34:32 +0100999 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % 64;
1000 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % 64;
1001 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / 64;
1002 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / 64;
Angel Pons737f1112020-11-13 14:07:30 +01001003
1004 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001005
Angel Pons7584e552020-11-19 21:34:32 +01001006 MCHBAR32(GDCRCKPICODE_ch(channel)) = clk_pi_coding;
1007 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = clk_logic_dly;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001008
Angel Pons88521882020-01-05 20:21:20 +01001009 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001010 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001011
Angel Pons88521882020-01-05 20:21:20 +01001012 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001013
1014 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001015 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001016
Angel Pons88521882020-01-05 20:21:20 +01001017 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001018 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001019
1020 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001021 const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
1022 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p;
1023 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n;
Angel Pons9fcc1102020-11-19 22:23:13 +01001024 const union gdcr_rx_reg gdcr_rx = {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001025 .rcven_pi_code = rcven % 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001026 .rx_dqs_p_pi_code = dqs_p,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001027 .rcven_logic_delay = rcven / 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001028 .rx_dqs_n_pi_code = dqs_n,
1029 };
1030 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) = gdcr_rx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001031
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001032 const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1033 const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq;
Angel Pons9fcc1102020-11-19 22:23:13 +01001034 const union gdcr_tx_reg gdcr_tx = {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001035 .tx_dq_pi_code = tx_dq % 64,
1036 .tx_dqs_pi_code = tx_dqs % 64,
1037 .tx_dqs_logic_delay = tx_dqs / 64,
1038 .tx_dq_logic_delay = tx_dq / 64,
Angel Pons9fcc1102020-11-19 22:23:13 +01001039 };
1040 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) = gdcr_tx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001041 }
1042 }
Angel Pons88521882020-01-05 20:21:20 +01001043 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1044 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001045}
1046
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001047static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001048{
Angel Pons88521882020-01-05 20:21:20 +01001049 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001050
Angel Pons3aed6ac2020-12-07 02:00:41 +01001051 /* Send a burst of 16 back-to-back read commands (4 DCLK apart) */
Angel Ponsffd50152020-11-12 11:03:10 +01001052 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053
Angel Ponsa853e7a2020-12-07 12:28:38 +01001054 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001055}
1056
Angel Pons7c49cb82020-03-16 23:17:32 +01001057static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001058{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001059 u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
Angel Pons7c49cb82020-03-16 23:17:32 +01001060
1061 return (MCHBAR32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001062 GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001063}
1064
1065struct run {
1066 int middle;
1067 int end;
1068 int start;
1069 int all;
1070 int length;
1071};
1072
1073static struct run get_longest_zero_run(int *seq, int sz)
1074{
1075 int i, ls;
1076 int bl = 0, bs = 0;
1077 struct run ret;
1078
1079 ls = 0;
1080 for (i = 0; i < 2 * sz; i++)
1081 if (seq[i % sz]) {
1082 if (i - ls > bl) {
1083 bl = i - ls;
1084 bs = ls;
1085 }
1086 ls = i + 1;
1087 }
1088 if (bl == 0) {
1089 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001090 ret.start = 0;
1091 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001092 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001093 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001094 return ret;
1095 }
1096
Angel Pons7c49cb82020-03-16 23:17:32 +01001097 ret.start = bs % sz;
1098 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001099 ret.middle = (bs + (bl - 1) / 2) % sz;
1100 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001101 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001102
1103 return ret;
1104}
1105
Angel Ponsf3053392020-11-13 23:31:12 +01001106static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001107{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001108 int rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001109 int statistics[NUM_LANES][128];
1110 int lane;
1111
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001112 for (rcven = 0; rcven < 128; rcven++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001113 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001114 ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001115 }
1116 program_timings(ctrl, channel);
1117
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001118 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001119
1120 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001121 statistics[lane][rcven] =
1122 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001123 }
1124 }
1125 FOR_ALL_LANES {
1126 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001127 ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001128 upperA[lane] = rn.end;
1129 if (upperA[lane] < rn.middle)
1130 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001131
Angel Pons7e439c92020-12-07 11:56:01 +01001132 printram("rcven: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001133 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001134 }
1135}
1136
Angel Ponsf3053392020-11-13 23:31:12 +01001137static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001138{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001139 int rcven_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001140 int statistics[NUM_LANES][51];
1141 int lane, i;
1142
1143 memset(statistics, 0, sizeof(statistics));
1144
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001145 for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001146
1147 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001148 ctrl->timings[channel][slotrank].lanes[lane].rcven
Angel Pons5db1b152020-12-13 16:37:53 +01001149 = upperA[lane] + rcven_delta + 64;
Angel Pons7c49cb82020-03-16 23:17:32 +01001150 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001151 program_timings(ctrl, channel);
1152
1153 for (i = 0; i < 100; i++) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001154 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001155 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001156 statistics[lane][rcven_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001157 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001158 }
1159 }
1160 }
1161 FOR_ALL_LANES {
1162 int last_zero, first_all;
1163
1164 for (last_zero = -25; last_zero <= 25; last_zero++)
1165 if (statistics[lane][last_zero + 25])
1166 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001167
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001168 last_zero--;
1169 for (first_all = -25; first_all <= 25; first_all++)
1170 if (statistics[lane][first_all + 25] == 100)
1171 break;
1172
Angel Pons7c49cb82020-03-16 23:17:32 +01001173 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001174
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001175 ctrl->timings[channel][slotrank].lanes[lane].rcven =
Angel Pons7c49cb82020-03-16 23:17:32 +01001176 (last_zero + first_all) / 2 + upperA[lane];
1177
Angel Pons7e439c92020-12-07 11:56:01 +01001178 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001179 lane, ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001180 }
1181}
1182
Angel Pons3aed6ac2020-12-07 02:00:41 +01001183/*
1184 * Once the DQS high phase has been found (for each DRAM) the next stage
1185 * is to find out the round trip latency, by locating the preamble cycle.
1186 * This is achieved by trying smaller and smaller roundtrip values until
1187 * the strobe sampling is done on the preamble cycle.
1188 */
Angel Ponsf3053392020-11-13 23:31:12 +01001189static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190{
1191 int works[NUM_LANES];
1192 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001193
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001194 while (1) {
1195 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001196
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001197 program_timings(ctrl, channel);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001198 test_rcven(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001199
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001200 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001201 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 if (works[lane])
1204 some_works = 1;
1205 else
1206 all_works = 0;
1207 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001208
1209 /* If every lane is working, exit */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001210 if (all_works)
1211 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001212
Angel Pons3aed6ac2020-12-07 02:00:41 +01001213 /*
1214 * If all bits are one (everyone is failing), decrement
1215 * the roundtrip value by two, and do another iteration.
1216 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001217 if (!some_works) {
Angel Pons3aed6ac2020-12-07 02:00:41 +01001218 /* Guard against roundtrip latency underflow */
Angel Pons88521882020-01-05 20:21:20 +01001219 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Angel Pons30791632020-12-12 12:28:29 +01001220 printk(BIOS_EMERG, "Roundtrip latency underflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001221 channel, slotrank);
1222 return MAKE_ERR;
1223 }
Angel Pons88521882020-01-05 20:21:20 +01001224 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001225 printram("4024 -= 2;\n");
1226 continue;
1227 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001228
1229 /*
1230 * Else (if some lanes are failing), increase the rank's
1231 * I/O latency by 2, and increase rcven logic delay by 2
1232 * on the working lanes, then perform another iteration.
1233 */
Felix Heldef4fe3e2019-12-31 14:15:05 +01001234 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001235 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001236
Angel Pons3aed6ac2020-12-07 02:00:41 +01001237 /* Guard against I/O latency overflow */
Angel Pons5db1b152020-12-13 16:37:53 +01001238 if (ctrl->timings[channel][slotrank].io_latency >= 16) {
Angel Pons30791632020-12-12 12:28:29 +01001239 printk(BIOS_EMERG, "I/O latency overflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001240 channel, slotrank);
1241 return MAKE_ERR;
1242 }
1243 FOR_ALL_LANES if (works[lane]) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001244 ctrl->timings[channel][slotrank].lanes[lane].rcven += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001245 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001246 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001247 }
1248 }
1249 return 0;
1250}
1251
Angel Pons12bd8ab2020-11-13 23:10:52 +01001252static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001253{
1254 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001255 u16 logic_delay_min = 7;
1256 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001257
1258 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001259 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001260
1261 logic_delay_min = MIN(logic_delay_min, logic_delay);
1262 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001263 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001264
1265 if (logic_delay_max < logic_delay_min) {
1266 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1267 logic_delay_max, logic_delay_min, channel, slotrank);
1268 }
1269
1270 assert(logic_delay_max >= logic_delay_min);
1271
1272 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001273}
1274
Angel Pons12bd8ab2020-11-13 23:10:52 +01001275static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001276{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001277 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001278
Angel Pons7c49cb82020-03-16 23:17:32 +01001279 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001280 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001281
Angel Pons12bd8ab2020-11-13 23:10:52 +01001282 if (prev < post)
1283 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001284
Angel Pons12bd8ab2020-11-13 23:10:52 +01001285 else if (prev > post)
1286 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001287
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001288 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001289 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001290
Angel Pons12bd8ab2020-11-13 23:10:52 +01001291 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1292 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1293 printram("4024 += %d;\n", latency_offset);
1294 printram("4028 += %d;\n", latency_offset);
1295
1296 return post;
1297}
1298
1299static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1300{
1301 u16 logic_delay_min = 7;
1302 int lane;
1303
1304 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001305 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001306
1307 logic_delay_min = MIN(logic_delay_min, logic_delay);
1308 }
1309
1310 if (logic_delay_min >= 2) {
1311 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1312 logic_delay_min, channel, slotrank);
1313 }
1314
1315 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001316 ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001317 }
1318 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1319 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001320}
1321
Angel Pons7f5a97c2020-11-13 16:58:46 +01001322int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001323{
1324 int channel, slotrank, lane;
1325 int err;
1326
1327 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1328 int all_high, some_high;
1329 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001330 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001331
Angel Pons88521882020-01-05 20:21:20 +01001332 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001333
Angel Ponsffd50152020-11-12 11:03:10 +01001334 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001335
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001336 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001337
Angel Pons58b609b2020-11-13 14:35:29 +01001338 const union gdcr_training_mod_reg training_mod = {
1339 .receive_enable_mode = 1,
1340 .training_rank_sel = slotrank,
1341 .odt_always_on = 1,
1342 };
1343 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001344
Felix Heldef4fe3e2019-12-31 14:15:05 +01001345 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001346 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001347 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001348
Angel Ponsf3053392020-11-13 23:31:12 +01001349 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001350
Felix Held2bb3cdf2018-07-28 00:23:59 +02001351 all_high = 1;
1352 some_high = 0;
1353 FOR_ALL_LANES {
Angel Pons5db1b152020-12-13 16:37:53 +01001354 if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= 64)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001355 some_high = 1;
1356 else
1357 all_high = 0;
1358 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001359
1360 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001361 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001362 printram("4028--;\n");
1363 FOR_ALL_LANES {
Angel Pons5db1b152020-12-13 16:37:53 +01001364 ctrl->timings[channel][slotrank].lanes[lane].rcven -= 64;
1365 upperA[lane] -= 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001366
1367 }
1368 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001369 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001370 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001371 printram("4024++;\n");
1372 printram("4028++;\n");
1373 }
1374
1375 program_timings(ctrl, channel);
1376
Angel Pons12bd8ab2020-11-13 23:10:52 +01001377 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001378
Angel Ponsf3053392020-11-13 23:31:12 +01001379 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380 if (err)
1381 return err;
1382
Angel Pons12bd8ab2020-11-13 23:10:52 +01001383 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Ponsf3053392020-11-13 23:31:12 +01001385 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386
Angel Pons12bd8ab2020-11-13 23:10:52 +01001387 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388
Angel Pons12bd8ab2020-11-13 23:10:52 +01001389 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390
Angel Pons12bd8ab2020-11-13 23:10:52 +01001391 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001392
Angel Pons7e439c92020-12-07 11:56:01 +01001393 printram("4/8: %d, %d, % 4d, % 4d\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001394 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001395 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396
1397 printram("final results:\n");
1398 FOR_ALL_LANES
Angel Pons7e439c92020-12-07 11:56:01 +01001399 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001400 ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001401
Angel Pons88521882020-01-05 20:21:20 +01001402 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001403
1404 toggle_io_reset();
1405 }
1406
1407 FOR_ALL_POPULATED_CHANNELS {
1408 program_timings(ctrl, channel);
1409 }
Angel Ponsc6742232020-11-15 13:26:21 +01001410
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001411 return 0;
1412}
1413
Angel Pons011661c2020-11-15 18:21:35 +01001414static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001415{
1416 int lane;
1417
1418 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001419 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1420 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001421 }
1422
Angel Pons88521882020-01-05 20:21:20 +01001423 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001424
Angel Ponsffd50152020-11-12 11:03:10 +01001425 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1426 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001427
Angel Ponsa853e7a2020-12-07 12:28:38 +01001428 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001429
Angel Pons801a5cb2020-11-15 15:48:29 +01001430 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001431
Angel Ponsa853e7a2020-12-07 12:28:38 +01001432 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001433}
1434
Angel Pons011661c2020-11-15 18:21:35 +01001435static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001436{
1437 int min = data[0];
1438 int max = min;
1439 int i;
1440 for (i = 1; i < count; i++) {
1441 if (min > data[i])
1442 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001443
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001444 if (max < data[i])
1445 max = data[i];
1446 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001447 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001448 for (i = 0; i < count; i++)
1449 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001450
Angel Pons891f2bc2020-01-10 01:27:28 +01001451 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001452}
1453
Angel Pons011661c2020-11-15 18:21:35 +01001454static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001455{
Angel Pons011661c2020-11-15 18:21:35 +01001456 int tx_dq;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001457 int stats[NUM_LANES][MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001458 int lane;
1459
Angel Pons88521882020-01-05 20:21:20 +01001460 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001461
Angel Ponsffd50152020-11-12 11:03:10 +01001462 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001463
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001464 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001465
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001466 for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) {
1467 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001468 program_timings(ctrl, channel);
1469
Angel Pons011661c2020-11-15 18:21:35 +01001470 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001471
1472 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001473 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001474 }
1475 }
1476 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001477 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1478
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001479 if (rn.all || rn.length < 8) {
Angel Pons30791632020-12-12 12:28:29 +01001480 printk(BIOS_EMERG, "tx_dq write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001481 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001482 /*
1483 * With command training not being done yet, the lane can be erroneous.
1484 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001485 */
Angel Pons011661c2020-11-15 18:21:35 +01001486 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001487 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1488
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001489 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001490 printk(BIOS_EMERG, "tx_dq recovery failed\n");
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001491 return MAKE_ERR;
1492 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001493 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001494 ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle;
Angel Pons7e439c92020-12-07 11:56:01 +01001495 printram("tx_dq: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001496 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001497 }
1498 return 0;
1499}
1500
Angel Pons88521882020-01-05 20:21:20 +01001501static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001502{
1503 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001504
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001505 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1506 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001507
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001508 return ret;
1509}
1510
Angel Pons765d4652020-11-11 14:44:35 +01001511/* Each cacheline is 64 bits long */
1512static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1513{
1514 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1515}
1516
Angel Pons88521882020-01-05 20:21:20 +01001517static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001518{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301519 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001520 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
Angel Pons7c49cb82020-03-16 23:17:32 +01001521
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001522 for (j = 0; j < 16; j++)
1523 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001524
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001525 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001526
1527 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001528}
1529
Angel Pons88521882020-01-05 20:21:20 +01001530static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001531{
1532 int ret = 0;
1533 int channel;
1534 FOR_ALL_POPULATED_CHANNELS ret++;
1535 return ret;
1536}
1537
Angel Pons88521882020-01-05 20:21:20 +01001538static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001539{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301540 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001541 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
1542 unsigned int channel_step = 64 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001543
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001544 for (j = 0; j < 16; j++)
1545 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
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 + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001549
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001550 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001551
1552 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001553}
1554
Angel Pons820bce72020-11-14 17:02:55 +01001555static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001556{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001557 int tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001558 int statistics[NUM_LANES][128];
1559 int lane;
1560
Angel Pons58b609b2020-11-13 14:35:29 +01001561 const union gdcr_training_mod_reg training_mod = {
1562 .write_leveling_mode = 1,
1563 .training_rank_sel = slotrank,
1564 .enable_dqs_wl = 5,
1565 .odt_always_on = 1,
1566 .force_drive_enable = 1,
1567 };
1568 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001569
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001570 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1571 int bank = 1;
1572
1573 if (ctrl->rank_mirror[channel][slotrank])
1574 ddr3_mirror_mrreg(&bank, &mr1reg);
1575
1576 wait_for_iosav(channel);
1577
1578 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1579
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001580 for (tx_dqs = 0; tx_dqs < 128; tx_dqs++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001581 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001582 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001583 }
1584 program_timings(ctrl, channel);
1585
Angel Ponsa853e7a2020-12-07 12:28:38 +01001586 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001587
1588 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001589 statistics[lane][tx_dqs] = !((MCHBAR32(lane_base[lane] +
1590 GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >>
1591 (tx_dqs % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001592 }
1593 }
1594 FOR_ALL_LANES {
1595 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001596 /*
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001597 * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value
1598 * of tx_dqs by a small value, which might cause the 6-bit value to overflow if
Angel Pons7c49cb82020-03-16 23:17:32 +01001599 * it's close to 0x3f. Increment the value by a small offset if it's likely
1600 * to overflow, to make sure it won't overflow while running tests and bricks
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001601 * the system due to a non matching tx_dq.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001602 *
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001603 * TODO: find out why some tests (edge write discovery) increment tx_dqs.
Angel Pons7c49cb82020-03-16 23:17:32 +01001604 */
1605 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001606 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001607 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001608 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001609
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001610 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001611 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01001612 printk(BIOS_EMERG, "JEDEC write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001613 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001614
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001615 return MAKE_ERR;
1616 }
Angel Pons7e439c92020-12-07 11:56:01 +01001617 printram("tx_dqs: %d, %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01001618 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 }
1620 return 0;
1621}
1622
Angel Pons820bce72020-11-14 17:02:55 +01001623static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001624{
1625 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001626 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001627 if (val == 0xffffffffffffffffLL)
1628 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001629 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001630 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001631 for (i = 0; i < 8; i++)
1632 if (val << (8 * (7 - i) + 4))
1633 return -i;
1634 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001635 /* DQS is early, needs positive 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 }
1640 return 8;
1641}
1642
Angel Ponsbf13ef02020-11-11 18:40:06 +01001643static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001644{
1645 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001646
1647 const union gdcr_training_mod_reg training_mod = {
1648 .dq_dqs_training_res = 1,
1649 };
1650 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1651
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001652 FOR_ALL_POPULATED_CHANNELS {
1653 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001654 }
1655 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1656
Angel Pons765d4652020-11-11 14:44:35 +01001657 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001658 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001659
Angel Pons88521882020-01-05 20:21:20 +01001660 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001661
Angel Ponsffd50152020-11-12 11:03:10 +01001662 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663
Angel Ponsa853e7a2020-12-07 12:28:38 +01001664 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001665
Angel Pons8f0757e2020-11-11 23:03:36 +01001666 const struct iosav_ssq rd_sequence[] = {
1667 /* DRAM command PREA */
1668 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001669 .sp_cmd_ctrl = {
1670 .command = IOSAV_PRE,
1671 .ranksel_ap = 1,
1672 },
1673 .subseq_ctrl = {
1674 .cmd_executions = 1,
1675 .cmd_delay_gap = 3,
1676 .post_ssq_wait = ctrl->tRP,
1677 .data_direction = SSQ_NA,
1678 },
1679 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +01001680 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +02001681 .rowbits = 6,
1682 .bank = 0,
1683 .rank = slotrank,
1684 },
1685 .addr_update = {
1686 .addr_wrap = 18,
1687 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001688 },
1689 /* DRAM command ACT */
1690 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001691 .sp_cmd_ctrl = {
1692 .command = IOSAV_ACT,
1693 .ranksel_ap = 1,
1694 },
1695 .subseq_ctrl = {
1696 .cmd_executions = 1,
1697 .cmd_delay_gap = 3,
1698 .post_ssq_wait = ctrl->tRCD,
1699 .data_direction = SSQ_NA,
1700 },
1701 .sp_cmd_addr = {
1702 .address = 0,
1703 .rowbits = 6,
1704 .bank = 0,
1705 .rank = slotrank,
1706 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001707 },
1708 /* DRAM command RD */
1709 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001710 .sp_cmd_ctrl = {
1711 .command = IOSAV_RD,
1712 .ranksel_ap = 3,
1713 },
1714 .subseq_ctrl = {
1715 .cmd_executions = 1,
1716 .cmd_delay_gap = 3,
1717 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001718 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001719 ctrl->timings[channel][slotrank].io_latency,
1720 .data_direction = SSQ_RD,
1721 },
1722 .sp_cmd_addr = {
1723 .address = 8,
1724 .rowbits = 6,
1725 .bank = 0,
1726 .rank = slotrank,
1727 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001728 },
1729 };
1730 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001731
Angel Ponsa853e7a2020-12-07 12:28:38 +01001732 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001733
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001734 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001735 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001736 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001737 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001738
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001739 old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1740 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs +=
Angel Pons820bce72020-11-14 17:02:55 +01001741 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001742
1743 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons7e439c92020-12-07 11:56:01 +01001744 printram("Bval+: %d, %d, %d, % 4d -> % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001745 old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001746 }
1747 }
Angel Pons88521882020-01-05 20:21:20 +01001748 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001749}
1750
Angel Pons7d115132020-11-14 01:44:44 +01001751static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001752{
Angel Pons7d115132020-11-14 01:44:44 +01001753 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001754
Angel Pons7d115132020-11-14 01:44:44 +01001755 FOR_ALL_POPULATED_CHANNELS {
1756 /* choose an existing rank */
1757 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001758
Angel Pons7d115132020-11-14 01:44:44 +01001759 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001760
Angel Ponsa853e7a2020-12-07 12:28:38 +01001761 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001762
Angel Pons7d115132020-11-14 01:44:44 +01001763 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1764 }
1765
1766 /* Refresh disable */
1767 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1768
1769 FOR_ALL_POPULATED_CHANNELS {
1770 /* Execute the same command queue */
Angel Ponsa853e7a2020-12-07 12:28:38 +01001771 iosav_run_once_and_wait(channel);
Angel Pons7d115132020-11-14 01:44:44 +01001772 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001773}
1774
Angel Pons7c49cb82020-03-16 23:17:32 +01001775/*
1776 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001777 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001778 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1779 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1780 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1781 * CLK/ADDR/CMD signals have the same routing delay.
1782 *
1783 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1784 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1785 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786 */
Angel Pons820bce72020-11-14 17:02:55 +01001787static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001788{
Angel Pons820bce72020-11-14 17:02:55 +01001789 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001790
Angel Pons7d115132020-11-14 01:44:44 +01001791 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001792
Angel Pons7c49cb82020-03-16 23:17:32 +01001793 /* Enable write leveling on all ranks
1794 Disable all DQ outputs
1795 Only NOP is allowed in this mode */
1796 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1797 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001798 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001799
Angel Ponsa1f17142020-11-15 12:50:03 +01001800 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001801 const union gdcr_training_mod_reg training_mod = {
1802 .write_leveling_mode = 1,
1803 .enable_dqs_wl = 5,
1804 .odt_always_on = 1,
1805 .force_drive_enable = 1,
1806 };
1807 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001808
1809 toggle_io_reset();
1810
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001811 /* Set any valid value for tx_dqs, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001812 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001813 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001814 if (err)
1815 return err;
1816 }
1817
Angel Pons7c49cb82020-03-16 23:17:32 +01001818 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001819 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001820 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001821
Angel Pons88521882020-01-05 20:21:20 +01001822 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001823
1824 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001825 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826
Angel Pons7c49cb82020-03-16 23:17:32 +01001827 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001828 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001829
1830 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001831 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001832 MCHBAR32(IOSAV_STATUS_ch(channel));
1833 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001834
Angel Ponsffd50152020-11-12 11:03:10 +01001835 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836
Angel Ponsa853e7a2020-12-07 12:28:38 +01001837 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001838 }
1839
1840 toggle_io_reset();
1841
Angel Pons820bce72020-11-14 17:02:55 +01001842 return 0;
1843}
1844
1845int write_training(ramctr_timing *ctrl)
1846{
Angel Ponsc6742232020-11-15 13:26:21 +01001847 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001848 int err;
1849
Angel Pons4d192822020-12-12 13:54:37 +01001850 /*
1851 * Set the DEC_WRD bit, required for the write flyby algorithm.
1852 * Needs to be done before starting the write training procedure.
1853 */
Angel Pons820bce72020-11-14 17:02:55 +01001854 FOR_ALL_POPULATED_CHANNELS
1855 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1856
Angel Pons4c76d252020-11-15 13:06:53 +01001857 printram("CPE\n");
1858
Angel Pons820bce72020-11-14 17:02:55 +01001859 err = jedec_write_leveling(ctrl);
1860 if (err)
1861 return err;
1862
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001863 printram("CPF\n");
1864
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001865 FOR_ALL_POPULATED_CHANNELS {
1866 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001867 }
1868
1869 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001870 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001871 if (err)
1872 return err;
1873 }
1874
1875 FOR_ALL_POPULATED_CHANNELS
1876 program_timings(ctrl, channel);
1877
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001878 /* measure and adjust tx_dqs timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001879 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001880
1881 FOR_ALL_POPULATED_CHANNELS
1882 program_timings(ctrl, channel);
1883
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001884 return 0;
1885}
1886
Angel Ponsbf13ef02020-11-11 18:40:06 +01001887static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001888{
1889 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001890 int tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001891 int lanes_ok = 0;
1892 int ctr = 0;
1893 int lane;
1894
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001895 for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001896 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001897 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
1898 saved_rt.lanes[lane].tx_dq + tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001899 }
1900 program_timings(ctrl, channel);
1901 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001902 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001903 }
1904
Angel Pons765d4652020-11-11 14:44:35 +01001905 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001906 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001907
Angel Pons88521882020-01-05 20:21:20 +01001908 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001909
Angel Ponsffd50152020-11-12 11:03:10 +01001910 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001911
1912 /* Program LFSR for the RD/WR subsequences */
1913 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1914 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001915
Angel Ponsa853e7a2020-12-07 12:28:38 +01001916 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001917
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001918 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001919 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001920
1921 if (r32 == 0)
1922 lanes_ok |= 1 << lane;
1923 }
1924 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001925 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001926 break;
1927 }
1928
1929 ctrl->timings[channel][slotrank] = saved_rt;
1930
Patrick Rudolphdd662872017-10-28 18:20:11 +02001931 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001932}
1933
Angel Pons88521882020-01-05 20:21:20 +01001934static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001935{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301936 unsigned int i, j;
Angel Pons5db1b152020-12-13 16:37:53 +01001937 unsigned int offset = get_precedening_channels(ctrl, channel) * 64;
1938 unsigned int step = 64 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001939
1940 if (patno) {
1941 u8 base8 = 0x80 >> ((patno - 1) % 8);
1942 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1943 for (i = 0; i < 32; i++) {
1944 for (j = 0; j < 16; j++) {
1945 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001946
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001947 if (invert[patno - 1][i] & (1 << (j / 2)))
1948 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001949
1950 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001951 }
1952 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001953 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001954 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1955 for (j = 0; j < 16; j++) {
1956 const u32 val = pattern[i][j];
1957 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1958 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001959 }
1960 sfence();
1961 }
Angel Pons765d4652020-11-11 14:44:35 +01001962
1963 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001964}
1965
Angel Pons88521882020-01-05 20:21:20 +01001966static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001967{
Angel Pons7d115132020-11-14 01:44:44 +01001968 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001969
Angel Pons7c49cb82020-03-16 23:17:32 +01001970 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001971 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001972
1973 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974 dram_mrscommands(ctrl);
1975
1976 toggle_io_reset();
1977}
1978
Angel Ponsbf13ef02020-11-11 18:40:06 +01001979#define CT_MIN_PI -127
1980#define CT_MAX_PI 128
1981#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1982
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001983#define MIN_C320C_LEN 13
1984
1985static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
1986{
1987 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
1988 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001989 int command_pi;
1990 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001991 int delta = 0;
1992
1993 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
1994
1995 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01001996 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001997 }
1998
1999 ctrl->cmd_stretch[channel] = cmd_stretch;
2000
Angel Pons7a612742020-11-12 13:34:03 +01002001 const union tc_rap_reg tc_rap = {
2002 .tRRD = ctrl->tRRD,
2003 .tRTP = ctrl->tRTP,
2004 .tCKE = ctrl->tCKE,
2005 .tWTR = ctrl->tWTR,
2006 .tFAW = ctrl->tFAW,
2007 .tWR = ctrl->tWR,
2008 .tCMD = ctrl->cmd_stretch[channel],
2009 };
2010 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002011
2012 if (ctrl->cmd_stretch[channel] == 2)
2013 delta = 2;
2014 else if (ctrl->cmd_stretch[channel] == 0)
2015 delta = 4;
2016
2017 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002018 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002019 }
2020
Angel Ponsbf13ef02020-11-11 18:40:06 +01002021 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002022 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002023 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002024 }
2025 program_timings(ctrl, channel);
2026 reprogram_320c(ctrl);
2027 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002028 stat[slotrank][command_pi - CT_MIN_PI] =
2029 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002030 }
2031 }
2032 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002033 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002034
Angel Ponsbf13ef02020-11-11 18:40:06 +01002035 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Angel Pons7e439c92020-12-07 11:56:01 +01002036 printram("cmd_stretch: %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01002037 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002038
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002039 if (rn.all || rn.length < MIN_C320C_LEN) {
2040 FOR_ALL_POPULATED_RANKS {
2041 ctrl->timings[channel][slotrank] =
2042 saved_timings[channel][slotrank];
2043 }
2044 return MAKE_ERR;
2045 }
2046 }
2047
2048 return 0;
2049}
2050
Angel Pons7c49cb82020-03-16 23:17:32 +01002051/*
2052 * Adjust CMD phase shift and try multiple command rates.
2053 * A command rate of 2T doubles the time needed for address and command decode.
2054 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002055int command_training(ramctr_timing *ctrl)
2056{
2057 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002058
2059 FOR_ALL_POPULATED_CHANNELS {
2060 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002061 }
2062
2063 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002064 int cmdrate, err;
2065
2066 /*
2067 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002068 * Issue:
Angel Pons30791632020-12-12 12:28:29 +01002069 * While command training seems to succeed, raminit will fail in write training.
Angel Pons7c49cb82020-03-16 23:17:32 +01002070 *
2071 * Workaround:
2072 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2073 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002074 *
2075 * Single DIMM per channel:
2076 * Try command rate 1T and 2T
2077 */
2078 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002079 if (ctrl->tCMD)
2080 /* XMP gives the CMD rate in clock ticks, not ns */
2081 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002082
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002083 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002084 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2085
2086 if (!err)
2087 break;
2088 }
2089
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002090 if (err) {
Angel Pons30791632020-12-12 12:28:29 +01002091 printk(BIOS_EMERG, "Command training failed: %d\n", channel);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002092 return err;
2093 }
2094
Angel Pons891f2bc2020-01-10 01:27:28 +01002095 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002096 }
2097
2098 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002099 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002100
2101 reprogram_320c(ctrl);
2102 return 0;
2103}
2104
Angel Pons4c79f932020-11-14 01:26:52 +01002105static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002106{
Angel Pons96a06dd2020-11-14 00:33:18 +01002107 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002108 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002109 int lane;
2110
Angel Pons96a06dd2020-11-14 00:33:18 +01002111 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002112 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002113 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi;
2114 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002115 }
2116 program_timings(ctrl, channel);
2117
2118 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002119 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2120 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002121 }
2122
Angel Pons88521882020-01-05 20:21:20 +01002123 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002124
Angel Ponsffd50152020-11-12 11:03:10 +01002125 iosav_write_read_mpr_sequence(
2126 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002127
Angel Ponsa853e7a2020-12-07 12:28:38 +01002128 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002129
2130 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002131 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002132 }
2133 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002134
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002135 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002136 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002137 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002138
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002139 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002140 printk(BIOS_EMERG, "Read MPR training failed: %d, %d, %d\n", channel,
Angel Pons7c49cb82020-03-16 23:17:32 +01002141 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002142 return MAKE_ERR;
2143 }
Angel Pons7e439c92020-12-07 11:56:01 +01002144 printram("eval %d, %d, %d: % 4d\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002145 }
2146 return 0;
2147}
2148
Angel Pons60971dc2020-11-14 00:49:38 +01002149static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2150{
2151 int slotrank, lane;
2152
2153 fill_pattern0(ctrl, channel, 0, 0);
2154 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002155 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002156 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2157 }
2158
2159 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002160 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16;
2161 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16;
Angel Pons60971dc2020-11-14 00:49:38 +01002162 }
2163
2164 program_timings(ctrl, channel);
2165
2166 FOR_ALL_POPULATED_RANKS {
2167 wait_for_iosav(channel);
2168
2169 iosav_write_read_mpr_sequence(
2170 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2171
Angel Ponsa853e7a2020-12-07 12:28:38 +01002172 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002173 }
2174
2175 /* XXX: check any measured value ? */
2176
2177 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002178 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48;
2179 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48;
Angel Pons60971dc2020-11-14 00:49:38 +01002180 }
2181
2182 program_timings(ctrl, channel);
2183
2184 FOR_ALL_POPULATED_RANKS {
2185 wait_for_iosav(channel);
2186
2187 iosav_write_read_mpr_sequence(
2188 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2189
Angel Ponsa853e7a2020-12-07 12:28:38 +01002190 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002191 }
2192
2193 /* XXX: check any measured value ? */
2194
2195 FOR_ALL_LANES {
2196 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2197 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2198 }
2199}
2200
Angel Pons4c79f932020-11-14 01:26:52 +01002201int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002202{
2203 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2204 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2205 int channel, slotrank, lane;
2206 int err;
2207
Angel Pons88521882020-01-05 20:21:20 +01002208 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002209
2210 toggle_io_reset();
2211
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002212 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002213 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002214
2215 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002216 }
2217
Angel Pons0c3936e2020-03-22 12:49:27 +01002218 /*
2219 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2220 * also use a single loop. It would seem that it is a debugging configuration.
2221 */
Angel Pons5db1b152020-12-13 16:37:53 +01002222 MCHBAR32(IOSAV_DC_MASK) = 3 << 8;
2223 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002224
2225 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002226 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002227 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002228 if (err)
2229 return err;
2230 }
2231
Angel Pons5db1b152020-12-13 16:37:53 +01002232 MCHBAR32(IOSAV_DC_MASK) = 2 << 8;
2233 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002234
2235 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002236 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002237 rising_edges[channel][slotrank]);
2238 if (err)
2239 return err;
2240 }
2241
Angel Pons88521882020-01-05 20:21:20 +01002242 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002243
2244 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002245 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002246 falling_edges[channel][slotrank][lane];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002247 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002248 rising_edges[channel][slotrank][lane];
2249 }
2250
2251 FOR_ALL_POPULATED_CHANNELS {
2252 program_timings(ctrl, channel);
2253 }
2254
Angel Pons50a6fe72020-11-14 01:18:14 +01002255 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002256 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002257 }
2258 return 0;
2259}
2260
Angel Pons08f749d2020-11-17 16:50:56 +01002261static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002262{
Angel Pons08f749d2020-11-17 16:50:56 +01002263 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2264
Angel Pons7c49cb82020-03-16 23:17:32 +01002265 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002266 int lower[NUM_LANES];
2267 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002268 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002269
2270 FOR_ALL_LANES {
2271 lower[lane] = 0;
2272 upper[lane] = MAX_EDGE_TIMING;
2273 }
2274
Angel Pons08f749d2020-11-17 16:50:56 +01002275 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002276 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002277 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002278 };
2279 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2280 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002281
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002282 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2283 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002284 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002285
Angel Pons08f749d2020-11-17 16:50:56 +01002286 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002287 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002288 ctrl->timings[channel][slotrank].lanes[lane]
2289 .rx_dqs_p = read_pi;
2290 ctrl->timings[channel][slotrank].lanes[lane]
2291 .rx_dqs_n = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002292 }
2293 program_timings(ctrl, channel);
2294
2295 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002296 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2297 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002298 }
Angel Pons88521882020-01-05 20:21:20 +01002299 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002300
Angel Ponsffd50152020-11-12 11:03:10 +01002301 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002302
Angel Ponsa853e7a2020-12-07 12:28:38 +01002303 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002304
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002305 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002306 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307 }
2308
Angel Pons7c49cb82020-03-16 23:17:32 +01002309 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002310 raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002311 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002312
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002313 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002314 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002315 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002316
2317 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2318 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002319
2320 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2321
Angel Pons7e439c92020-12-07 11:56:01 +01002322 printram("edges: %d, %d, %d: % 4d-% 4d-% 4d, "
2323 "% 4d-% 4d\n", channel, slotrank, i, rn.start,
Angel Pons7c49cb82020-03-16 23:17:32 +01002324 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002325 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002326
2327 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2328 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2329
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330 edges[lane] = (lower[lane] + upper[lane]) / 2;
2331 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons30791632020-12-12 12:28:29 +01002332 printk(BIOS_EMERG, "Aggressive read training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002333 "%d, %d, %d\n", channel, slotrank, lane);
2334
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002335 return MAKE_ERR;
2336 }
2337 }
2338 }
2339 }
2340
Angel Ponsa93f46e2020-11-17 16:54:01 +01002341 /* Restore nominal Vref after training */
2342 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002343 printram("CPA\n");
2344 return 0;
2345}
2346
Angel Pons08f749d2020-11-17 16:50:56 +01002347int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002348{
2349 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002350 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2351 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002352
Angel Pons7c49cb82020-03-16 23:17:32 +01002353 /*
2354 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2355 * also use a single loop. It would seem that it is a debugging configuration.
2356 */
Angel Pons5db1b152020-12-13 16:37:53 +01002357 MCHBAR32(IOSAV_DC_MASK) = 3 << 8;
2358 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002359
2360 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002361 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002362 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002363 if (err)
2364 return err;
2365 }
2366
Angel Pons5db1b152020-12-13 16:37:53 +01002367 MCHBAR32(IOSAV_DC_MASK) = 2 << 8;
2368 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002369
2370 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002371 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002372 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002373 if (err)
2374 return err;
2375 }
2376
Angel Pons88521882020-01-05 20:21:20 +01002377 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002378
2379 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002380 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Angel Pons7c49cb82020-03-16 23:17:32 +01002381 falling_edges[channel][slotrank][lane];
2382
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002383 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Angel Pons7c49cb82020-03-16 23:17:32 +01002384 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002385 }
2386
2387 FOR_ALL_POPULATED_CHANNELS
2388 program_timings(ctrl, channel);
2389
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002390 return 0;
2391}
2392
Angel Pons2a7d7522020-11-19 12:49:07 +01002393static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002394{
Angel Pons88521882020-01-05 20:21:20 +01002395 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002396
Angel Ponsffd50152020-11-12 11:03:10 +01002397 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002398
Angel Ponsa853e7a2020-12-07 12:28:38 +01002399 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002400}
2401
Angel Pons2a7d7522020-11-19 12:49:07 +01002402static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002403{
Angel Pons2a7d7522020-11-19 12:49:07 +01002404 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24);
2405 udelay(2);
2406}
2407
2408int aggressive_write_training(ramctr_timing *ctrl)
2409{
2410 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002411 int i, pat;
2412
2413 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2414 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2415 int channel, slotrank, lane;
2416
Angel Pons9fbb1b02020-11-19 12:53:36 +01002417 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2418 if (!IS_IVY_CPU(ctrl->cpu))
2419 return 0;
2420
2421 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2422 return 0;
2423
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002424 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2425 lower[channel][slotrank][lane] = 0;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002426 upper[channel][slotrank][lane] = MAX_TX_DQ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002427 }
2428
Angel Pons2a7d7522020-11-19 12:49:07 +01002429 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2430 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2431
2432 if (enable_iosav_opt)
2433 MCHBAR32(MCMNTS_SPARE) = 1;
2434
Angel Pons30791632020-12-12 12:28:29 +01002435 printram("Aggresive write training:\n");
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002436
Angel Pons2a7d7522020-11-19 12:49:07 +01002437 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002438 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002439 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002440
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002441 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2442 FOR_ALL_POPULATED_RANKS {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002443 int tx_dq;
2444 u32 raw_stats[MAX_TX_DQ + 1];
2445 int stats[MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002446
2447 /* Make sure rn.start < rn.end */
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002448 stats[MAX_TX_DQ] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002449
2450 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002451
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002452 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002453 FOR_ALL_LANES {
2454 ctrl->timings[channel][slotrank]
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002455 .lanes[lane].tx_dq = tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01002456 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002457 program_timings(ctrl, channel);
2458
Angel Pons2a7d7522020-11-19 12:49:07 +01002459 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002460
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002461 raw_stats[tx_dq] = MCHBAR32(
Angel Pons098240eb2020-03-22 12:55:32 +01002462 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002463 }
2464 FOR_ALL_LANES {
2465 struct run rn;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002466 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
2467 stats[tx_dq] = !!(raw_stats[tx_dq]
Angel Pons7c49cb82020-03-16 23:17:32 +01002468 & (1 << lane));
2469 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002471 rn = get_longest_zero_run(stats, MAX_TX_DQ + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002472 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002473 printk(BIOS_EMERG, "Aggressive "
2474 "write training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002475 "%d, %d, %d\n", channel,
2476 slotrank, lane);
2477
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002478 return MAKE_ERR;
2479 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002480 printram("tx_dq: %d, %d, %d: "
Angel Pons7e439c92020-12-07 11:56:01 +01002481 "% 4d-% 4d-% 4d, "
2482 "% 4d-% 4d\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002483 i, rn.start, rn.middle, rn.end,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002484 rn.start + ctrl->tx_dq_offset[i],
2485 rn.end - ctrl->tx_dq_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002486
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002487 lower[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002488 MAX(rn.start + ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002489 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002490
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002491 upper[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002492 MIN(rn.end - ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002493 upper[channel][slotrank][lane]);
2494
2495 }
2496 }
2497 }
2498 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499 }
2500
Angel Pons2a7d7522020-11-19 12:49:07 +01002501 FOR_ALL_CHANNELS {
2502 /* Restore nominal write Vref after training */
2503 set_write_vref(channel, 0);
2504 }
2505
2506 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2507 if (enable_iosav_opt)
2508 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002509
2510 printram("CPB\n");
2511
2512 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7e439c92020-12-07 11:56:01 +01002513 printram("tx_dq %d, %d, %d: % 4d\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002514 (lower[channel][slotrank][lane] +
2515 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002516
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002517 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002518 (lower[channel][slotrank][lane] +
2519 upper[channel][slotrank][lane]) / 2;
2520 }
2521 FOR_ALL_POPULATED_CHANNELS {
2522 program_timings(ctrl, channel);
2523 }
2524 return 0;
2525}
2526
Angel Pons88521882020-01-05 20:21:20 +01002527void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002528{
2529 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002530 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002531
2532 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2533 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002534 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002535 FOR_ALL_LANES mat =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002536 MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002537 printram("normalize %d, %d, %d: mat %d\n",
2538 channel, slotrank, lane, mat);
2539
Felix Heldef4fe3e2019-12-31 14:15:05 +01002540 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002541 printram("normalize %d, %d, %d: delta %d\n",
2542 channel, slotrank, lane, delta);
2543
Angel Pons88521882020-01-05 20:21:20 +01002544 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002545 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002546 }
2547
2548 FOR_ALL_POPULATED_CHANNELS {
2549 program_timings(ctrl, channel);
2550 }
2551}
2552
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002553int channel_test(ramctr_timing *ctrl)
2554{
2555 int channel, slotrank, lane;
2556
2557 slotrank = 0;
2558 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002559 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002560 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002561 return MAKE_ERR;
2562 }
2563 FOR_ALL_POPULATED_CHANNELS {
2564 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002565 }
2566
2567 for (slotrank = 0; slotrank < 4; slotrank++)
2568 FOR_ALL_CHANNELS
2569 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2570 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002571 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2572 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002573 }
Angel Pons88521882020-01-05 20:21:20 +01002574 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002575
Angel Ponsffd50152020-11-12 11:03:10 +01002576 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002577
Angel Ponsa853e7a2020-12-07 12:28:38 +01002578 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002579
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002580 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002581 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002582 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2583 channel, slotrank, lane);
2584 return MAKE_ERR;
2585 }
2586 }
2587 return 0;
2588}
2589
Patrick Rudolphdd662872017-10-28 18:20:11 +02002590void channel_scrub(ramctr_timing *ctrl)
2591{
2592 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002593 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002594
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002595 FOR_ALL_POPULATED_CHANNELS {
2596 wait_for_iosav(channel);
2597 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002598 }
2599
2600 /*
2601 * During runtime the "scrubber" will periodically scan through the memory in the
2602 * physical address space, to identify and fix CRC errors.
2603 * The following loops writes to every DRAM address, setting the ECC bits to the
2604 * correct value. A read from this location will no longer return a CRC error,
2605 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002606 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002607 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2608 * and firmware running in x86_32.
2609 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002610 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2611 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002612 for (bank = 0; bank < 8; bank++) {
2613 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002614
Angel Pons8f0757e2020-11-11 23:03:36 +01002615 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2616 const struct iosav_ssq sequence[] = {
2617 /*
2618 * DRAM command ACT
2619 * Opens the row for writing.
2620 */
2621 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002622 .sp_cmd_ctrl = {
2623 .command = IOSAV_ACT,
2624 .ranksel_ap = 1,
2625 },
2626 .subseq_ctrl = {
2627 .cmd_executions = 1,
2628 .cmd_delay_gap = gap,
2629 .post_ssq_wait = ctrl->tRCD,
2630 .data_direction = SSQ_NA,
2631 },
2632 .sp_cmd_addr = {
2633 .address = row,
2634 .rowbits = 6,
2635 .bank = bank,
2636 .rank = slotrank,
2637 },
2638 .addr_update = {
2639 .inc_addr_1 = 1,
2640 .addr_wrap = 18,
2641 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002642 },
2643 /*
2644 * DRAM command WR
2645 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2646 * bytes.
2647 */
2648 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002649 .sp_cmd_ctrl = {
2650 .command = IOSAV_WR,
2651 .ranksel_ap = 1,
2652 },
2653 .subseq_ctrl = {
2654 .cmd_executions = 129,
2655 .cmd_delay_gap = 4,
2656 .post_ssq_wait = ctrl->tWTR +
2657 ctrl->CWL + 8,
2658 .data_direction = SSQ_WR,
2659 },
2660 .sp_cmd_addr = {
2661 .address = row,
2662 .rowbits = 0,
2663 .bank = bank,
2664 .rank = slotrank,
2665 },
2666 .addr_update = {
2667 .inc_addr_8 = 1,
2668 .addr_wrap = 9,
2669 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002670 },
2671 /*
2672 * DRAM command PRE
2673 * Closes the row.
2674 */
2675 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002676 .sp_cmd_ctrl = {
2677 .command = IOSAV_PRE,
2678 .ranksel_ap = 1,
2679 },
2680 .subseq_ctrl = {
2681 .cmd_executions = 1,
2682 .cmd_delay_gap = 4,
2683 .post_ssq_wait = ctrl->tRP,
2684 .data_direction = SSQ_NA,
2685 },
2686 .sp_cmd_addr = {
2687 .address = 0,
2688 .rowbits = 6,
2689 .bank = bank,
2690 .rank = slotrank,
2691 },
2692 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002693 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002694 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002695 },
2696 };
2697 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002698
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002699 iosav_run_queue(channel, 16, 0);
2700
2701 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002702 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002703 }
2704 }
2705}
2706
Angel Pons88521882020-01-05 20:21:20 +01002707void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002708{
2709 int channel;
2710
Angel Pons7c49cb82020-03-16 23:17:32 +01002711 /* 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 +01002712 static u32 seeds[NUM_CHANNELS][3] = {
2713 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2714 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2715 };
2716 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002717 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002718 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2719 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2720 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002721 }
2722}
2723
Angel Pons89ae6b82020-03-21 13:23:32 +01002724void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002725{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002726 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002727 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002728 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002729 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002730 }
2731}
2732
Angel Pons88521882020-01-05 20:21:20 +01002733void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002734{
2735 int channel;
2736
2737 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002738 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002739 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002740 }
2741
2742 udelay(1);
2743
2744 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002745 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002746 }
2747}
2748
Angel Pons7c49cb82020-03-16 23:17:32 +01002749void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002750{
Angel Pons11463322020-11-19 11:04:28 +01002751 /* Use a larger delay when running fast to improve stability */
2752 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2753
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002754 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002755
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002756 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002757 int min_pi = 10000;
2758 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002759
2760 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002761 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2762 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002763 }
2764
Angel Pons7a612742020-11-12 13:34:03 +01002765 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002766
Angel Pons7a612742020-11-12 13:34:03 +01002767 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002768
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002769 dram_odt_stretch(ctrl, channel);
2770
Angel Pons7a612742020-11-12 13:34:03 +01002771 const union tc_rwp_reg tc_rwp = {
2772 .tRRDR = 0,
2773 .tRRDD = val,
2774 .tWWDR = val,
2775 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002776 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002777 .tWRDRDD = tWRDRDD,
2778 .tRWSR = 2,
2779 .dec_wrd = 1,
2780 };
2781 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002782 }
2783}
2784
Angel Pons88521882020-01-05 20:21:20 +01002785void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002786{
2787 int channel;
2788 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002789 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2790 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002791 }
2792}
2793
Angel Pons7c49cb82020-03-16 23:17:32 +01002794/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2795static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002796{
Angel Pons88521882020-01-05 20:21:20 +01002797 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002798}
2799
Angel Pons7c49cb82020-03-16 23:17:32 +01002800/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002801void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002802{
2803 int channel;
2804 int t1_cycles = 0, t1_ns = 0, t2_ns;
2805 int t3_ns;
2806 u32 r32;
2807
Angel Pons7c49cb82020-03-16 23:17:32 +01002808 /* FIXME: This register only exists on Ivy Bridge */
2809 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002810
Angel Pons7a612742020-11-12 13:34:03 +01002811 FOR_ALL_CHANNELS {
2812 union tc_othp_reg tc_othp = {
2813 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2814 };
2815 tc_othp.tCPDED = 1;
2816 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2817 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002818
Angel Pons09fc4b92020-11-19 12:02:07 +01002819 /* 64 DCLKs until idle, decision per rank */
2820 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002821
Felix Heldf9b826a2018-07-30 17:56:52 +02002822 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002823 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002824
Angel Pons88521882020-01-05 20:21:20 +01002825 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
2826 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002827
2828 FOR_ALL_CHANNELS {
2829 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002830 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002831 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002832 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002833 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002834 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002835 case 1:
2836 case 4:
2837 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002838 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002839 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002840 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002841 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002842 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002843 break;
2844 }
2845 }
2846
Felix Held50b7ed22019-12-30 20:41:54 +01002847 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002848 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002849 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002850
Angel Pons7a612742020-11-12 13:34:03 +01002851 FOR_ALL_CHANNELS {
2852 union tc_rfp_reg tc_rfp = {
2853 .raw = MCHBAR32(TC_RFP_ch(channel)),
2854 };
2855 tc_rfp.refresh_2x_control = 1;
2856 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2857 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002858
Angel Ponsdc5539f2020-11-12 12:44:25 +01002859 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2860 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002861 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002862
Angel Pons7c49cb82020-03-16 23:17:32 +01002863 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002864 FOR_ALL_POPULATED_CHANNELS
2865 break;
2866
Angel Pons88521882020-01-05 20:21:20 +01002867 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2868 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002869 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002870 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002871 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002872 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002873 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002874 t1_ns += 500;
2875
Angel Pons88521882020-01-05 20:21:20 +01002876 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002877 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002878 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002879 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002880 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002881 t3_ns = 500;
2882 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002883
2884 /* The graphics driver will use these watermark values */
2885 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002886 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002887 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2888 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002889}
2890
Angel Pons88521882020-01-05 20:21:20 +01002891void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002892{
Angel Ponsc6742232020-11-15 13:26:21 +01002893 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002894
Angel Pons7c49cb82020-03-16 23:17:32 +01002895 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002896 const union tc_rap_reg tc_rap = {
2897 .tRRD = ctrl->tRRD,
2898 .tRTP = ctrl->tRTP,
2899 .tCKE = ctrl->tCKE,
2900 .tWTR = ctrl->tWTR,
2901 .tFAW = ctrl->tFAW,
2902 .tWR = ctrl->tWR,
2903 .tCMD = ctrl->cmd_stretch[channel],
2904 };
2905 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002906 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002907
2908 udelay(1);
2909
2910 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002911 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002912 }
2913
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002914 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002915 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002916
2917 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002918 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002919 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920 }
2921
2922 printram("CPE\n");
2923
Angel Pons88521882020-01-05 20:21:20 +01002924 MCHBAR32(GDCRTRAININGMOD) = 0;
2925 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002926
2927 printram("CP5b\n");
2928
2929 FOR_ALL_POPULATED_CHANNELS {
2930 program_timings(ctrl, channel);
2931 }
2932
2933 u32 reg, addr;
2934
Angel Pons7c49cb82020-03-16 23:17:32 +01002935 /* Poll for RCOMP */
2936 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2937 ;
2938
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002939 do {
Angel Pons88521882020-01-05 20:21:20 +01002940 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002941 } while ((reg & 0x14) == 0);
2942
Angel Pons7c49cb82020-03-16 23:17:32 +01002943 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002944 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002945 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002946
Angel Pons7c49cb82020-03-16 23:17:32 +01002947 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002948 udelay(500);
2949
2950 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002951 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002952 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002953 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002954 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002955 MCHBAR32(addr) = reg;
2956
Angel Pons7c49cb82020-03-16 23:17:32 +01002957 /* Wait 10ns for ranks to settle */
2958 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002959
2960 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
2961 MCHBAR32(addr) = reg;
2962
Angel Pons7c49cb82020-03-16 23:17:32 +01002963 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002964 write_reset(ctrl);
2965 }
2966
Angel Pons7c49cb82020-03-16 23:17:32 +01002967 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002968 dram_mrscommands(ctrl);
2969
2970 printram("CP5c\n");
2971
Angel Pons88521882020-01-05 20:21:20 +01002972 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002973
2974 FOR_ALL_CHANNELS {
Angel Pons5db1b152020-12-13 16:37:53 +01002975 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002976 udelay(2);
2977 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002978}