blob: d2049c1adc585642536a14b4de200fb115dff674 [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
Angel Pons12bd8ab2020-11-13 23:10:52 +01003#include <assert.h>
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01004#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <console/console.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01006#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010010#include <northbridge/intel/sandybridge/chip.h>
11#include <device/pci_def.h>
12#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020013#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020014
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010015#include "raminit_native.h"
16#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010017#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#include "sandybridge.h"
19
Angel Pons7c49cb82020-03-16 23:17:32 +010020/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010021
22static void sfence(void)
23{
24 asm volatile ("sfence");
25}
26
Angel Pons7c49cb82020-03-16 23:17:32 +010027/* Toggle IO reset bit */
28static void toggle_io_reset(void)
29{
Angel Pons88521882020-01-05 20:21:20 +010030 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010031 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010033 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010034 udelay(1);
35}
36
37static u32 get_XOVER_CLK(u8 rankmap)
38{
39 return rankmap << 24;
40}
41
42static u32 get_XOVER_CMD(u8 rankmap)
43{
44 u32 reg;
45
Angel Pons7c49cb82020-03-16 23:17:32 +010046 /* Enable xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010047 reg = 0x4000;
48
Angel Pons7c49cb82020-03-16 23:17:32 +010049 /* Enable xover ctl */
50 if (rankmap & 0x03)
51 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010052
Angel Pons7c49cb82020-03-16 23:17:32 +010053 if (rankmap & 0x0c)
54 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010055
56 return reg;
57}
58
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010059void dram_find_common_params(ramctr_timing *ctrl)
60{
61 size_t valid_dimms;
62 int channel, slot;
63 dimm_info *dimms = &ctrl->info;
64
65 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
66 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010067
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010068 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010069
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010070 const dimm_attr *dimm = &dimms->dimm[channel][slot];
71 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
72 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010073
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010074 valid_dimms++;
75
76 /* Find all possible CAS combinations */
77 ctrl->cas_supported &= dimm->cas_supported;
78
79 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010080 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
81 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
82 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
84 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010085 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010086 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
87 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
88 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
89 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
90 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030091 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
92 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010093 }
94
95 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010096 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
97
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010098 if (!valid_dimms)
99 die("No valid DIMMs found");
100}
101
Angel Pons88521882020-01-05 20:21:20 +0100102void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100103{
104 u32 reg;
105 int channel;
106
107 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100108 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100110 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
111 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100112
Angel Pons7c49cb82020-03-16 23:17:32 +0100113 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100115 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
116 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100117 }
118}
119
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100120static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100121{
Angel Pons89ae6b82020-03-21 13:23:32 +0100122 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100123
124 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 /*
126 * ODT stretch:
127 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
128 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100129 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100130 if (stretch == 2)
131 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100132
Angel Pons88521882020-01-05 20:21:20 +0100133 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100134 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100136 } else {
Angel Pons88521882020-01-05 20:21:20 +0100137 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100138 union tc_othp_reg tc_othp = {
139 .raw = MCHBAR32(addr),
140 };
141 tc_othp.odt_delay_d0 = stretch;
142 tc_othp.odt_delay_d1 = stretch;
143 MCHBAR32(addr) = tc_othp.raw;
Iru Cai89af71c2018-08-16 16:46:27 +0800144 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100145 }
146}
147
148void dram_timing_regs(ramctr_timing *ctrl)
149{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100150 int channel;
151
Angel Pons81378062020-11-12 13:46:21 +0100152 /* BIN parameters */
153 const union tc_dbp_reg tc_dbp = {
154 .tRCD = ctrl->tRCD,
155 .tRP = ctrl->tRP,
156 .tAA = ctrl->CAS,
157 .tCWL = ctrl->CWL,
158 .tRAS = ctrl->tRAS,
159 };
160
161 /* Regular access parameters */
162 const union tc_rap_reg tc_rap = {
163 .tRRD = ctrl->tRRD,
164 .tRTP = ctrl->tRTP,
165 .tCKE = ctrl->tCKE,
166 .tWTR = ctrl->tWTR,
167 .tFAW = ctrl->tFAW,
168 .tWR = ctrl->tWR,
169 .tCMD = 3,
170 };
171
172 /* Other parameters */
173 const union tc_othp_reg tc_othp = {
174 .tXPDLL = ctrl->tXPDLL,
175 .tXP = ctrl->tXP,
176 .tAONPD = ctrl->tAONPD,
177 .tCPDED = 2,
Angel Pons2ad03a42020-11-19 11:07:27 +0100178 .tPRPDEN = 1,
Angel Pons81378062020-11-12 13:46:21 +0100179 };
180
181 /*
182 * If tXP and tXPDLL are very high, we need to increase them by one.
183 * This can only happen on Ivy Bridge, and when overclocking the RAM.
184 */
185 const union tc_dtp_reg tc_dtp = {
186 .overclock_tXP = ctrl->tXP >= 8,
187 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
188 };
189
190 /*
191 * TC-Refresh timing parameters:
192 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
193 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
194 */
195 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
196
197 const union tc_rftp_reg tc_rftp = {
198 .tREFI = ctrl->tREFI,
199 .tRFC = ctrl->tRFC,
200 .tREFIx9 = val32 / 1024,
201 };
202
203 /* Self-refresh timing parameters */
204 const union tc_srftp_reg tc_srftp = {
205 .tXSDLL = tDLLK,
206 .tXS_offset = ctrl->tXSOffset,
207 .tZQOPER = tDLLK - ctrl->tXSOffset,
208 .tMOD = ctrl->tMOD - 8,
209 };
210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100211 FOR_ALL_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +0100212 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
213 MCHBAR32(TC_DBP_ch(channel)) = tc_dbp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100214
Angel Pons7a612742020-11-12 13:34:03 +0100215 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
216 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100217
Angel Pons7a612742020-11-12 13:34:03 +0100218 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
219 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100220
Angel Ponsca2f68a2020-03-22 13:15:12 +0100221 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Pons81378062020-11-12 13:46:21 +0100222 /* Debug parameters - only applies to Ivy Bridge */
Angel Pons7a612742020-11-12 13:34:03 +0100223 MCHBAR32(TC_DTP_ch(channel)) = tc_dtp.raw;
Angel Ponsca2f68a2020-03-22 13:15:12 +0100224 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100225
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100226 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100227
Angel Pons7a612742020-11-12 13:34:03 +0100228 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
229 MCHBAR32(TC_RFTP_ch(channel)) = tc_rftp.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +0100230
Angel Pons7a612742020-11-12 13:34:03 +0100231 union tc_rfp_reg tc_rfp = {
232 .raw = MCHBAR32(TC_RFP_ch(channel)),
233 };
234 tc_rfp.oref_ri = 0xff;
235 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100236
Angel Pons7a612742020-11-12 13:34:03 +0100237 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
238 MCHBAR32(TC_SRFTP_ch(channel)) = tc_srftp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100239 }
240}
241
242void dram_dimm_mapping(ramctr_timing *ctrl)
243{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100244 int channel;
245 dimm_info *info = &ctrl->info;
246
247 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200248 dimm_attr *dimmA, *dimmB;
249 u32 reg = 0;
250
Angel Pons7c49cb82020-03-16 23:17:32 +0100251 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100252 dimmA = &info->dimm[channel][0];
253 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100254 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100255 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100256 dimmA = &info->dimm[channel][1];
257 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100258 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100259 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100260
Nico Huberac4f2162017-10-01 18:14:43 +0200261 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100262 reg |= (dimmA->size_mb / 256) << 0;
263 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200264 reg |= (dimmA->width / 8 - 1) << 19;
265 }
266
267 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100268 reg |= (dimmB->size_mb / 256) << 8;
269 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200270 reg |= (dimmB->width / 8 - 1) << 20;
271 }
272
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200273 /*
274 * Rank interleave: Bit 16 of the physical address space sets
275 * the rank to use in a dual single rank DIMM configuration.
276 * That results in every 64KiB being interleaved between two ranks.
277 */
278 reg |= 1 << 21;
279 /* Enhanced interleave */
280 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200281
Angel Pons7c49cb82020-03-16 23:17:32 +0100282 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100283 ctrl->mad_dimm[channel] = reg;
284 } else {
285 ctrl->mad_dimm[channel] = 0;
286 }
287 }
288}
289
Patrick Rudolphdd662872017-10-28 18:20:11 +0200290void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100291{
292 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200293 u32 ecc;
294
295 if (ctrl->ecc_enabled)
296 ecc = training ? (1 << 24) : (3 << 24);
297 else
298 ecc = 0;
299
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100300 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200301 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100302 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200303
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200304 if (ctrl->ecc_enabled)
305 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100306}
307
Angel Pons88521882020-01-05 20:21:20 +0100308void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100309{
310 u32 reg, ch0size, ch1size;
311 u8 val;
312 reg = 0;
313 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100314
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100315 if (training) {
316 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
317 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
318 } else {
319 ch0size = ctrl->channel_size_mb[0];
320 ch1size = ctrl->channel_size_mb[1];
321 }
322
323 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100324 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100325 val = ch1size / 256;
326 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100327 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100328 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100329 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100330
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100331 } else {
Angel Pons88521882020-01-05 20:21:20 +0100332 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100333 val = ch0size / 256;
334 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100335 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100336 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100337 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100338 }
339}
340
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100341#define DEFAULT_PCI_MMIO_SIZE 2048
342
343static unsigned int get_mmio_size(void)
344{
345 const struct device *dev;
346 const struct northbridge_intel_sandybridge_config *cfg = NULL;
347
Angel Ponsb31d1d72020-01-10 01:35:09 +0100348 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100349 if (dev)
350 cfg = dev->chip_info;
351
352 /* If this is zero, it just means devicetree.cb didn't set it */
353 if (!cfg || cfg->pci_mmio_size == 0)
354 return DEFAULT_PCI_MMIO_SIZE;
355 else
356 return cfg->pci_mmio_size;
357}
358
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200359/*
360 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
361 * The ME/PCU/.. has the ability to change this.
362 * Return 0: ECC is optional
363 * Return 1: ECC is forced
364 */
365bool get_host_ecc_forced(void)
366{
367 /* read Capabilities A Register */
368 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
369 return !!(reg32 & (1 << 24));
370}
371
372/*
373 * Returns the ECC capability.
374 * The ME/PCU/.. has the ability to change this.
375 * Return 0: ECC is disabled
376 * Return 1: ECC is possible
377 */
378bool get_host_ecc_cap(void)
379{
380 /* read Capabilities A Register */
381 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
382 return !(reg32 & (1 << 25));
383}
384
Angel Pons88521882020-01-05 20:21:20 +0100385void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100386{
Angel Pons7c49cb82020-03-16 23:17:32 +0100387 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
388 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
389 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100390 uint16_t ggc;
391
392 mmiosize = get_mmio_size();
393
Felix Held87ddea22020-01-26 04:55:27 +0100394 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100395 if (!(ggc & 2)) {
396 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100397 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100398 } else {
399 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100400 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100401 }
402
403 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
404
405 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
406
407 mestolenbase = tom - me_uma_size;
408
Angel Pons7c49cb82020-03-16 23:17:32 +0100409 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
410
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100411 gfxstolenbase = toludbase - gfxstolen;
412 gttbase = gfxstolenbase - gttsize;
413
414 tsegbase = gttbase - tsegsize;
415
Angel Pons7c49cb82020-03-16 23:17:32 +0100416 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100417 tsegbasedelta = tsegbase & (tsegsize - 1);
418 tsegbase &= ~(tsegsize - 1);
419
420 gttbase -= tsegbasedelta;
421 gfxstolenbase -= tsegbasedelta;
422 toludbase -= tsegbasedelta;
423
Angel Pons7c49cb82020-03-16 23:17:32 +0100424 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100425 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100426 /* Reclaim is possible */
427 reclaim = 1;
428 remapbase = MAX(4096, tom - me_uma_size);
429 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
430 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100431 } else {
432 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100433 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100434 touudbase = tom - me_uma_size;
435 }
436
Angel Pons7c49cb82020-03-16 23:17:32 +0100437 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100438 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
439
Angel Pons7c49cb82020-03-16 23:17:32 +0100440 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442 val = tom & 0xfff;
443 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100444 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100445 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100446
Angel Ponsb31d1d72020-01-10 01:35:09 +0100447 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100448 val = tom & 0xfffff000;
449 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100450 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100451 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100452
Angel Pons7c49cb82020-03-16 23:17:32 +0100453 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100454 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100455 val = toludbase & 0xfff;
456 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100457 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100458 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100459
Angel Pons7c49cb82020-03-16 23:17:32 +0100460 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100461 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100462 val = touudbase & 0xfff;
463 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100464 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100465 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100466
Angel Pons7c49cb82020-03-16 23:17:32 +0100467 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100468 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100469 val = touudbase & 0xfffff000;
470 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100471 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100472 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100473
474 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100475 /* REMAP BASE */
476 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100477 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100478
Angel Pons7c49cb82020-03-16 23:17:32 +0100479 /* REMAP LIMIT */
480 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100481 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100482 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100483 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100484 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100485 val = tsegbase & 0xfff;
486 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100487 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100488 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100489
Angel Pons7c49cb82020-03-16 23:17:32 +0100490 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100491 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100492 val = gfxstolenbase & 0xfff;
493 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100494 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100495 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100496
Angel Pons7c49cb82020-03-16 23:17:32 +0100497 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100498 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100499 val = gttbase & 0xfff;
500 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100501 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100502 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100503
504 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100505 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100506 val = (0x80000 - me_uma_size) & 0xfffff000;
507 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100508 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100509 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100510
Angel Pons7c49cb82020-03-16 23:17:32 +0100511 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513 val = mestolenbase & 0xfff;
514 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100515 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100516 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100517
Angel Ponsb31d1d72020-01-10 01:35:09 +0100518 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100519 val = mestolenbase & 0xfffff000;
520 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100521 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100522 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100523
Angel Pons7c49cb82020-03-16 23:17:32 +0100524 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100525 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526 val = (0x80000 - me_uma_size) & 0xfff;
527 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100528 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
529 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100530 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100531 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100532 }
533}
534
Angel Pons88521882020-01-05 20:21:20 +0100535static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100536{
537 int channel, slotrank;
538
Angel Pons7c49cb82020-03-16 23:17:32 +0100539 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100540 channel = (ctrl->rankmap[0]) ? 0 : 1;
541
Angel Pons88521882020-01-05 20:21:20 +0100542 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100543
Angel Pons7c49cb82020-03-16 23:17:32 +0100544 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100545 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
546
Angel Ponsffd50152020-11-12 11:03:10 +0100547 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100548
Angel Pons7c49cb82020-03-16 23:17:32 +0100549 /*
550 * Execute command queue - why is bit 22 set here?!
551 *
552 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
553 */
Angel Pons38d901e2020-05-02 23:50:43 +0200554 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200555
Angel Pons88521882020-01-05 20:21:20 +0100556 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100557}
558
Angel Pons88521882020-01-05 20:21:20 +0100559void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560{
Felix Held9fe248f2018-07-31 20:59:45 +0200561 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100562 int channel;
563
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
565 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566 do {
Angel Pons88521882020-01-05 20:21:20 +0100567 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100568 } while ((reg & 0x14) == 0);
569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100572 MCHBAR32(MC_INIT_STATE_G) = reg;
573 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100574 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100575 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100576
Angel Pons7c49cb82020-03-16 23:17:32 +0100577 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100578 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100579
Angel Pons7c49cb82020-03-16 23:17:32 +0100580 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581 udelay(200);
582
Angel Pons7c49cb82020-03-16 23:17:32 +0100583 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100584 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100585
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100587 udelay(500);
588
Angel Pons7c49cb82020-03-16 23:17:32 +0100589 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100590 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100591
Angel Pons7c49cb82020-03-16 23:17:32 +0100592 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100593 udelay(1);
594
595 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200597 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100598 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100599
Angel Pons7c49cb82020-03-16 23:17:32 +0100600 /* Wait 10ns for ranks to settle */
601 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100602
603 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100604 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100605
Angel Pons7c49cb82020-03-16 23:17:32 +0100606 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100607 write_reset(ctrl);
608 }
609}
610
Angel Pons3d3bf482020-11-14 16:18:15 +0100611/*
612 * DDR3 Rank1 Address mirror swap the following pins:
613 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
614 */
615static void ddr3_mirror_mrreg(int *bank, u32 *addr)
616{
617 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
618 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
619}
620
Angel Pons7c49cb82020-03-16 23:17:32 +0100621static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100622{
Angel Pons88521882020-01-05 20:21:20 +0100623 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100624
Angel Pons3d3bf482020-11-14 16:18:15 +0100625 if (ctrl->rank_mirror[channel][slotrank])
626 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100627
Angel Pons8f0757e2020-11-11 23:03:36 +0100628 const struct iosav_ssq sequence[] = {
629 /* DRAM command MRS */
630 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200631 .sp_cmd_ctrl = {
632 .command = IOSAV_MRS,
633 },
634 .subseq_ctrl = {
635 .cmd_executions = 1,
636 .cmd_delay_gap = 4,
637 .post_ssq_wait = 4,
638 .data_direction = SSQ_NA,
639 },
640 .sp_cmd_addr = {
641 .address = val,
642 .rowbits = 6,
643 .bank = reg,
644 .rank = slotrank,
645 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100646 },
647 /* DRAM command MRS */
648 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200649 .sp_cmd_ctrl = {
650 .command = IOSAV_MRS,
651 .ranksel_ap = 1,
652 },
653 .subseq_ctrl = {
654 .cmd_executions = 1,
655 .cmd_delay_gap = 4,
656 .post_ssq_wait = 4,
657 .data_direction = SSQ_NA,
658 },
659 .sp_cmd_addr = {
660 .address = val,
661 .rowbits = 6,
662 .bank = reg,
663 .rank = slotrank,
664 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100665 },
666 /* DRAM command MRS */
667 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200668 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100669 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200670 },
671 .subseq_ctrl = {
672 .cmd_executions = 1,
673 .cmd_delay_gap = 4,
674 .post_ssq_wait = ctrl->tMOD,
675 .data_direction = SSQ_NA,
676 },
677 .sp_cmd_addr = {
678 .address = val,
679 .rowbits = 6,
680 .bank = reg,
681 .rank = slotrank,
682 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100683 },
684 };
685 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200686
Angel Pons7c49cb82020-03-16 23:17:32 +0100687 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200688 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100689}
690
Angel Pons09fc4b92020-11-19 12:02:07 +0100691/* Obtain optimal power down mode for current configuration */
692static enum pdwm_mode get_power_down_mode(ramctr_timing *ctrl)
693{
694 if (ctrl->tXP > 8)
695 return PDM_NONE;
696
697 if (ctrl->tXPDLL > 32)
698 return PDM_PPD;
699
700 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
701 return PDM_DLL_OFF;
702
703 return PDM_APD_PPD;
704}
705
Angel Pons88521882020-01-05 20:21:20 +0100706static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100707{
708 u16 mr0reg, mch_cas, mch_wr;
709 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Angel Pons09fc4b92020-11-19 12:02:07 +0100710
711 const enum pdwm_mode power_down = get_power_down_mode(ctrl);
712
713 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100714
Angel Pons7c49cb82020-03-16 23:17:32 +0100715 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100716 if (ctrl->CAS < 12) {
717 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
718 } else {
719 mch_cas = (u16) (ctrl->CAS - 12);
720 mch_cas = ((mch_cas << 1) | 0x1);
721 }
722
Angel Pons7c49cb82020-03-16 23:17:32 +0100723 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100724 mch_wr = mch_wr_t[ctrl->tWR - 5];
725
Angel Pons2bf28ed2020-11-12 13:49:59 +0100726 /* DLL Reset - self clearing - set after CLK frequency has been changed */
727 mr0reg = 1 << 8;
728
729 mr0reg |= (mch_cas & 0x1) << 2;
730 mr0reg |= (mch_cas & 0xe) << 3;
731 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100732
Angel Pons09fc4b92020-11-19 12:02:07 +0100733 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
734 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100735 return mr0reg;
736}
737
738static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
739{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200740 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100741}
742
Angel Ponsf9997482020-11-12 16:02:52 +0100743static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100744{
745 /* Get ODT based on rankmap */
746 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
747
748 if (dimms_per_ch == 1) {
749 return (const odtmap){60, 60};
750 } else {
751 return (const odtmap){120, 30};
752 }
753}
754
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100755static u32 encode_odt(u32 odt)
756{
757 switch (odt) {
758 case 30:
759 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
760 case 60:
761 return (1 << 2); // RZQ/4
762 case 120:
763 return (1 << 6); // RZQ/2
764 default:
765 case 0:
766 return 0;
767 }
768}
769
770static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
771{
772 odtmap odt;
773 u32 mr1reg;
774
Angel Ponsf9997482020-11-12 16:02:52 +0100775 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100776 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100777
778 mr1reg |= encode_odt(odt.rttnom);
779
780 return mr1reg;
781}
782
783static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
784{
785 u16 mr1reg;
786
787 mr1reg = make_mr1(ctrl, rank, channel);
788
789 write_mrreg(ctrl, channel, rank, 1, mr1reg);
790}
791
792static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
793{
Angel Pons868bca22020-11-13 13:38:04 +0100794 const u16 pasr = 0;
795 const u16 cwl = ctrl->CWL - 5;
796 const odtmap odt = get_ODT(ctrl, channel);
797
Angel Ponsdca3cb52020-11-13 13:42:07 +0100798 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100799 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
800 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100801
Angel Pons868bca22020-11-13 13:38:04 +0100802 u16 mr2reg = 0;
803 mr2reg |= pasr;
804 mr2reg |= cwl << 3;
805 mr2reg |= ctrl->auto_self_refresh << 6;
806 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100807 mr2reg |= (odt.rttwr / 60) << 9;
808
809 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100810
811 /* Program MR2 shadow */
812 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
813
814 reg32 &= 3 << 14 | 3 << 6;
815
816 reg32 |= mr2reg & ~(3 << 6);
817
Angel Pons927b1c02020-12-10 22:11:27 +0100818 if (srt)
819 reg32 |= 1 << (rank / 2 + 6);
820
821 if (ctrl->rank_mirror[channel][rank])
822 reg32 |= 1 << (rank / 2 + 14);
823
Angel Pons7f1363d2020-11-13 13:31:58 +0100824 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100825}
826
827static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
828{
829 write_mrreg(ctrl, channel, rank, 3, 0);
830}
831
Angel Pons88521882020-01-05 20:21:20 +0100832void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100833{
834 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100835 int channel;
836
837 FOR_ALL_POPULATED_CHANNELS {
838 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100839 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100840 dram_mr2(ctrl, slotrank, channel);
841
Angel Pons7c49cb82020-03-16 23:17:32 +0100842 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100843 dram_mr3(ctrl, slotrank, channel);
844
Angel Pons7c49cb82020-03-16 23:17:32 +0100845 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100846 dram_mr1(ctrl, slotrank, channel);
847
Angel Pons7c49cb82020-03-16 23:17:32 +0100848 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100849 dram_mr0(ctrl, slotrank, channel);
850 }
851 }
852
Angel Pons8f0757e2020-11-11 23:03:36 +0100853 const struct iosav_ssq zqcl_sequence[] = {
854 /* DRAM command NOP (without ODT nor chip selects) */
855 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200856 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100857 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200858 },
859 .subseq_ctrl = {
860 .cmd_executions = 1,
861 .cmd_delay_gap = 4,
862 .post_ssq_wait = 15,
863 .data_direction = SSQ_NA,
864 },
865 .sp_cmd_addr = {
866 .address = 2,
867 .rowbits = 6,
868 .bank = 0,
869 .rank = 0,
870 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100871 },
872 /* DRAM command ZQCL */
873 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200874 .sp_cmd_ctrl = {
875 .command = IOSAV_ZQCS,
876 .ranksel_ap = 1,
877 },
878 .subseq_ctrl = {
879 .cmd_executions = 1,
880 .cmd_delay_gap = 4,
881 .post_ssq_wait = 400,
882 .data_direction = SSQ_NA,
883 },
884 .sp_cmd_addr = {
885 .address = 1024,
886 .rowbits = 6,
887 .bank = 0,
888 .rank = 0,
889 },
890 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100891 .inc_rank = 1,
892 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200893 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100894 },
895 };
896 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100897
Angel Pons7c49cb82020-03-16 23:17:32 +0100898 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200899 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100900
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100902 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100903 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100904 }
905
Angel Pons7c49cb82020-03-16 23:17:32 +0100906 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100907 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100908
909 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100910 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100911
Angel Pons88521882020-01-05 20:21:20 +0100912 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100913
914 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
915
Angel Pons7c49cb82020-03-16 23:17:32 +0100916 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100917 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100918
Angel Ponsffd50152020-11-12 11:03:10 +0100919 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200920
Angel Pons7c49cb82020-03-16 23:17:32 +0100921 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200922 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100923
Angel Pons7c49cb82020-03-16 23:17:32 +0100924 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100925 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100926 }
927}
928
Felix Held3b906032020-01-14 17:05:43 +0100929static const u32 lane_base[] = {
930 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
931 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
932 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100933};
934
Angel Pons88521882020-01-05 20:21:20 +0100935void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100936{
Angel Pons7584e552020-11-19 21:34:32 +0100937 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100938 int lane;
939 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940
Angel Pons7584e552020-11-19 21:34:32 +0100941 u32 ctl_delay[NUM_SLOTS] = { 0 };
942 int cmd_delay = 0;
943
944 /* Enable CLK XOVER */
945 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
946 u32 clk_logic_dly = 0;
947
948 /*
949 * Apply command delay if desired setting is negative. Find the
950 * most negative value: 'cmd_delay' will be the absolute value.
951 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100952 FOR_ALL_POPULATED_RANKS {
Angel Pons7584e552020-11-19 21:34:32 +0100953 if (cmd_delay < -ctrl->timings[channel][slotrank].pi_coding)
954 cmd_delay = -ctrl->timings[channel][slotrank].pi_coding;
955 }
956 if (cmd_delay < 0) {
957 printk(BIOS_ERR, "C%d command delay underflow: %d\n", channel, cmd_delay);
958 cmd_delay = 0;
959 }
960 if (cmd_delay >= 128) {
961 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
962 cmd_delay = 127;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100963 }
964
Angel Pons7584e552020-11-19 21:34:32 +0100965 /* Apply control and clock delay if desired setting is positive */
966 if (cmd_delay == 0) {
967 for (slot = 0; slot < NUM_SLOTS; slot++) {
968 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
969 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
970
971 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
972
973 if (slot_map & 1)
974 ctl_delay[slot] += pi_coding_0 + cmd_delay;
975
976 if (slot_map & 2)
977 ctl_delay[slot] += pi_coding_1 + cmd_delay;
978
979 /* If both ranks in a slot are populated, use the average */
980 if (slot_map == 3)
981 ctl_delay[slot] /= 2;
982
983 if (ctl_delay[slot] >= 128) {
984 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
985 channel, slot, ctl_delay[slot]);
986 ctl_delay[slot] = 127;
987 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100988 }
Angel Pons7584e552020-11-19 21:34:32 +0100989 FOR_ALL_POPULATED_RANKS {
990 u32 clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
991
992 if (clk_delay >= 128) {
993 printk(BIOS_ERR, "C%dR%d clock delay overflow: %d\n",
994 channel, slotrank, clk_delay);
995 clk_delay = 127;
996 }
997
998 clk_pi_coding |= (clk_delay % 64) << (6 * slotrank);
999 clk_logic_dly |= (clk_delay / 64) << slotrank;
1000 }
1001 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001002
Angel Pons7c49cb82020-03-16 23:17:32 +01001003 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +01001004 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
1005 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
1006 };
Angel Pons7584e552020-11-19 21:34:32 +01001007 cmd_pi_coding.cmd_pi_code = cmd_delay % 64;
1008 cmd_pi_coding.cmd_logic_delay = cmd_delay / 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001009
Angel Pons7584e552020-11-19 21:34:32 +01001010 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % 64;
1011 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % 64;
1012 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / 64;
1013 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / 64;
Angel Pons737f1112020-11-13 14:07:30 +01001014
1015 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001016
Angel Pons7584e552020-11-19 21:34:32 +01001017 MCHBAR32(GDCRCKPICODE_ch(channel)) = clk_pi_coding;
1018 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = clk_logic_dly;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001019
Angel Pons88521882020-01-05 20:21:20 +01001020 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001021 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001022
Angel Pons88521882020-01-05 20:21:20 +01001023 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001024
1025 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001026 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001027
Angel Pons88521882020-01-05 20:21:20 +01001028 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001029 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001030
1031 FOR_ALL_LANES {
Angel Pons9fcc1102020-11-19 22:23:13 +01001032 const u16 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
1033 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rising;
1034 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].falling;
1035 const union gdcr_rx_reg gdcr_rx = {
1036 .rcven_pi_code = timA % 64,
1037 .rx_dqs_p_pi_code = dqs_p,
1038 .rcven_logic_delay = timA / 64,
1039 .rx_dqs_n_pi_code = dqs_n,
1040 };
1041 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) = gdcr_rx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001042
Angel Pons9fcc1102020-11-19 22:23:13 +01001043 const u16 timB = ctrl->timings[channel][slotrank].lanes[lane].timB;
1044 const int timC = ctrl->timings[channel][slotrank].lanes[lane].timC;
1045 const union gdcr_tx_reg gdcr_tx = {
1046 .tx_dq_pi_code = timC % 64,
1047 .tx_dqs_pi_code = timB % 64,
1048 .tx_dqs_logic_delay = timB / 64,
1049 .tx_dq_logic_delay = timC / 64,
1050 };
1051 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) = gdcr_tx.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001052 }
1053 }
Angel Pons88521882020-01-05 20:21:20 +01001054 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1055 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001056}
1057
Angel Pons88521882020-01-05 20:21:20 +01001058static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001059{
Angel Pons88521882020-01-05 20:21:20 +01001060 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001061
Angel Ponsffd50152020-11-12 11:03:10 +01001062 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001063
Angel Pons7c49cb82020-03-16 23:17:32 +01001064 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001065 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001066
Angel Pons88521882020-01-05 20:21:20 +01001067 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001068}
1069
Angel Pons7c49cb82020-03-16 23:17:32 +01001070static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001071{
1072 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001073
1074 return (MCHBAR32(lane_base[lane] +
1075 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001076}
1077
1078struct run {
1079 int middle;
1080 int end;
1081 int start;
1082 int all;
1083 int length;
1084};
1085
1086static struct run get_longest_zero_run(int *seq, int sz)
1087{
1088 int i, ls;
1089 int bl = 0, bs = 0;
1090 struct run ret;
1091
1092 ls = 0;
1093 for (i = 0; i < 2 * sz; i++)
1094 if (seq[i % sz]) {
1095 if (i - ls > bl) {
1096 bl = i - ls;
1097 bs = ls;
1098 }
1099 ls = i + 1;
1100 }
1101 if (bl == 0) {
1102 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001103 ret.start = 0;
1104 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001105 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001106 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001107 return ret;
1108 }
1109
Angel Pons7c49cb82020-03-16 23:17:32 +01001110 ret.start = bs % sz;
1111 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001112 ret.middle = (bs + (bl - 1) / 2) % sz;
1113 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001114 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001115
1116 return ret;
1117}
1118
Angel Ponsf3053392020-11-13 23:31:12 +01001119static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001120{
1121 int timA;
1122 int statistics[NUM_LANES][128];
1123 int lane;
1124
1125 for (timA = 0; timA < 128; timA++) {
1126 FOR_ALL_LANES {
1127 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1128 }
1129 program_timings(ctrl, channel);
1130
1131 test_timA(ctrl, channel, slotrank);
1132
1133 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001134 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001135 }
1136 }
1137 FOR_ALL_LANES {
1138 struct run rn = get_longest_zero_run(statistics[lane], 128);
1139 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1140 upperA[lane] = rn.end;
1141 if (upperA[lane] < rn.middle)
1142 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001143
Patrick Rudolph368b6152016-11-25 16:36:52 +01001144 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001145 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001146 }
1147}
1148
Angel Ponsf3053392020-11-13 23:31:12 +01001149static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001150{
1151 int timA_delta;
1152 int statistics[NUM_LANES][51];
1153 int lane, i;
1154
1155 memset(statistics, 0, sizeof(statistics));
1156
1157 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001158
1159 FOR_ALL_LANES {
1160 ctrl->timings[channel][slotrank].lanes[lane].timA
1161 = upperA[lane] + timA_delta + 0x40;
1162 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001163 program_timings(ctrl, channel);
1164
1165 for (i = 0; i < 100; i++) {
1166 test_timA(ctrl, channel, slotrank);
1167 FOR_ALL_LANES {
1168 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001169 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001170 }
1171 }
1172 }
1173 FOR_ALL_LANES {
1174 int last_zero, first_all;
1175
1176 for (last_zero = -25; last_zero <= 25; last_zero++)
1177 if (statistics[lane][last_zero + 25])
1178 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001179
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001180 last_zero--;
1181 for (first_all = -25; first_all <= 25; first_all++)
1182 if (statistics[lane][first_all + 25] == 100)
1183 break;
1184
Angel Pons7c49cb82020-03-16 23:17:32 +01001185 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001186
1187 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001188 (last_zero + first_all) / 2 + upperA[lane];
1189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001191 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001192 }
1193}
1194
Angel Ponsf3053392020-11-13 23:31:12 +01001195static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001196{
1197 int works[NUM_LANES];
1198 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001199
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001200 while (1) {
1201 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 program_timings(ctrl, channel);
1204 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001205
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001206 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001207 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1208
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001209 if (works[lane])
1210 some_works = 1;
1211 else
1212 all_works = 0;
1213 }
1214 if (all_works)
1215 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001216
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001217 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001218 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001219 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1220 channel, slotrank);
1221 return MAKE_ERR;
1222 }
Angel Pons88521882020-01-05 20:21:20 +01001223 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 printram("4024 -= 2;\n");
1225 continue;
1226 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001227 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001228 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001229
Felix Heldef4fe3e2019-12-31 14:15:05 +01001230 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001231 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1232 channel, slotrank);
1233 return MAKE_ERR;
1234 }
1235 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001236 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001237 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001238 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001239 }
1240 }
1241 return 0;
1242}
1243
Angel Pons12bd8ab2020-11-13 23:10:52 +01001244static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001245{
1246 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001247 u16 logic_delay_min = 7;
1248 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001249
1250 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001251 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1252
1253 logic_delay_min = MIN(logic_delay_min, logic_delay);
1254 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001255 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001256
1257 if (logic_delay_max < logic_delay_min) {
1258 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1259 logic_delay_max, logic_delay_min, channel, slotrank);
1260 }
1261
1262 assert(logic_delay_max >= logic_delay_min);
1263
1264 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001265}
1266
Angel Pons12bd8ab2020-11-13 23:10:52 +01001267static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001268{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001269 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270
Angel Pons7c49cb82020-03-16 23:17:32 +01001271 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001272 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001273
Angel Pons12bd8ab2020-11-13 23:10:52 +01001274 if (prev < post)
1275 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001276
Angel Pons12bd8ab2020-11-13 23:10:52 +01001277 else if (prev > post)
1278 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001279
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001280 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001281 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001282
Angel Pons12bd8ab2020-11-13 23:10:52 +01001283 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1284 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1285 printram("4024 += %d;\n", latency_offset);
1286 printram("4028 += %d;\n", latency_offset);
1287
1288 return post;
1289}
1290
1291static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1292{
1293 u16 logic_delay_min = 7;
1294 int lane;
1295
1296 FOR_ALL_LANES {
1297 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1298
1299 logic_delay_min = MIN(logic_delay_min, logic_delay);
1300 }
1301
1302 if (logic_delay_min >= 2) {
1303 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1304 logic_delay_min, channel, slotrank);
1305 }
1306
1307 FOR_ALL_LANES {
1308 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1309 }
1310 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1311 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001312}
1313
Angel Pons7f5a97c2020-11-13 16:58:46 +01001314int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001315{
1316 int channel, slotrank, lane;
1317 int err;
1318
1319 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1320 int all_high, some_high;
1321 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001322 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001323
Angel Pons88521882020-01-05 20:21:20 +01001324 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001325
Angel Ponsffd50152020-11-12 11:03:10 +01001326 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001327
Angel Pons7c49cb82020-03-16 23:17:32 +01001328 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001329 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001330
Angel Pons58b609b2020-11-13 14:35:29 +01001331 const union gdcr_training_mod_reg training_mod = {
1332 .receive_enable_mode = 1,
1333 .training_rank_sel = slotrank,
1334 .odt_always_on = 1,
1335 };
1336 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001337
Felix Heldef4fe3e2019-12-31 14:15:05 +01001338 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001339 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001340 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001341
Angel Ponsf3053392020-11-13 23:31:12 +01001342 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001343
Felix Held2bb3cdf2018-07-28 00:23:59 +02001344 all_high = 1;
1345 some_high = 0;
1346 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001347 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001348 some_high = 1;
1349 else
1350 all_high = 0;
1351 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001352
1353 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001354 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001355 printram("4028--;\n");
1356 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001357 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001358 upperA[lane] -= 0x40;
1359
1360 }
1361 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001362 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001363 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001364 printram("4024++;\n");
1365 printram("4028++;\n");
1366 }
1367
1368 program_timings(ctrl, channel);
1369
Angel Pons12bd8ab2020-11-13 23:10:52 +01001370 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001371
Angel Ponsf3053392020-11-13 23:31:12 +01001372 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001373 if (err)
1374 return err;
1375
Angel Pons12bd8ab2020-11-13 23:10:52 +01001376 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001377
Angel Ponsf3053392020-11-13 23:31:12 +01001378 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379
Angel Pons12bd8ab2020-11-13 23:10:52 +01001380 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001381
Angel Pons12bd8ab2020-11-13 23:10:52 +01001382 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001383
Angel Pons12bd8ab2020-11-13 23:10:52 +01001384 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001385
1386 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001387 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001388 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001389
1390 printram("final results:\n");
1391 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001392 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001393 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394
Angel Pons88521882020-01-05 20:21:20 +01001395 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396
1397 toggle_io_reset();
1398 }
1399
1400 FOR_ALL_POPULATED_CHANNELS {
1401 program_timings(ctrl, channel);
1402 }
Angel Ponsc6742232020-11-15 13:26:21 +01001403
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001404 return 0;
1405}
1406
Angel Pons011661c2020-11-15 18:21:35 +01001407static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001408{
1409 int lane;
1410
1411 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001412 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1413 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001414 }
1415
Angel Pons88521882020-01-05 20:21:20 +01001416 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001417
Angel Ponsffd50152020-11-12 11:03:10 +01001418 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1419 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001420
Angel Pons7c49cb82020-03-16 23:17:32 +01001421 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001422 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001423
Angel Pons88521882020-01-05 20:21:20 +01001424 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001425
Angel Pons801a5cb2020-11-15 15:48:29 +01001426 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001427
Angel Pons7c49cb82020-03-16 23:17:32 +01001428 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001429 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001430
Angel Pons88521882020-01-05 20:21:20 +01001431 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001432}
1433
Angel Pons011661c2020-11-15 18:21:35 +01001434static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001435{
1436 int min = data[0];
1437 int max = min;
1438 int i;
1439 for (i = 1; i < count; i++) {
1440 if (min > data[i])
1441 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001442
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001443 if (max < data[i])
1444 max = data[i];
1445 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001446 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001447 for (i = 0; i < count; i++)
1448 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001449
Angel Pons891f2bc2020-01-10 01:27:28 +01001450 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001451}
1452
Angel Pons011661c2020-11-15 18:21:35 +01001453static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001454{
Angel Pons011661c2020-11-15 18:21:35 +01001455 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001456 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001457 int lane;
1458
Angel Pons88521882020-01-05 20:21:20 +01001459 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001460
Angel Ponsffd50152020-11-12 11:03:10 +01001461 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001462
Angel Pons7c49cb82020-03-16 23:17:32 +01001463 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001464 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001465
Angel Pons011661c2020-11-15 18:21:35 +01001466 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1467 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = 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) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001480 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1481 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) {
1490 printk(BIOS_EMERG, "timC recovery failed\n");
1491 return MAKE_ERR;
1492 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001493 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001494 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001495 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\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 Pons891f2bc2020-01-10 01:27:28 +01001520 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
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 Pons891f2bc2020-01-10 01:27:28 +01001541 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301542 unsigned int channel_step = 0x40 * 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{
1557 int timB;
1558 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
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001580 for (timB = 0; timB < 128; timB++) {
1581 FOR_ALL_LANES {
1582 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1583 }
1584 program_timings(ctrl, channel);
1585
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001586 /* Execute command queue */
1587 iosav_run_once(channel);
1588
1589 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001590
1591 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001592 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1593 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1594 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001595 }
1596 }
1597 FOR_ALL_LANES {
1598 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001599 /*
1600 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1601 * of timB by a small value, which might cause the 6-bit value to overflow if
1602 * it's close to 0x3f. Increment the value by a small offset if it's likely
1603 * to overflow, to make sure it won't overflow while running tests and bricks
1604 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001605 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001606 * TODO: find out why some tests (edge write discovery) increment timB.
1607 */
1608 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001609 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001610 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001611 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001612
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001613 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1614 if (rn.all) {
1615 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1616 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001617
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001618 return MAKE_ERR;
1619 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001620 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1621 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001622 }
1623 return 0;
1624}
1625
Angel Pons820bce72020-11-14 17:02:55 +01001626static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001627{
1628 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001629 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001630 if (val == 0xffffffffffffffffLL)
1631 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001632 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001633 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001634 for (i = 0; i < 8; i++)
1635 if (val << (8 * (7 - i) + 4))
1636 return -i;
1637 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001638 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001639 for (i = 0; i < 8; i++)
1640 if (val >> (8 * (7 - i) + 4))
1641 return i;
1642 }
1643 return 8;
1644}
1645
Angel Ponsbf13ef02020-11-11 18:40:06 +01001646static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001647{
1648 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001649
1650 const union gdcr_training_mod_reg training_mod = {
1651 .dq_dqs_training_res = 1,
1652 };
1653 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1654
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001655 FOR_ALL_POPULATED_CHANNELS {
1656 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657 }
1658 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1659
Angel Pons765d4652020-11-11 14:44:35 +01001660 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001661 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001662
Angel Pons88521882020-01-05 20:21:20 +01001663 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001664
Angel Ponsffd50152020-11-12 11:03:10 +01001665 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001666
Angel Pons7c49cb82020-03-16 23:17:32 +01001667 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001668 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001669
Angel Pons88521882020-01-05 20:21:20 +01001670 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001671
Angel Pons8f0757e2020-11-11 23:03:36 +01001672 const struct iosav_ssq rd_sequence[] = {
1673 /* DRAM command PREA */
1674 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001675 .sp_cmd_ctrl = {
1676 .command = IOSAV_PRE,
1677 .ranksel_ap = 1,
1678 },
1679 .subseq_ctrl = {
1680 .cmd_executions = 1,
1681 .cmd_delay_gap = 3,
1682 .post_ssq_wait = ctrl->tRP,
1683 .data_direction = SSQ_NA,
1684 },
1685 .sp_cmd_addr = {
1686 .address = 1024,
1687 .rowbits = 6,
1688 .bank = 0,
1689 .rank = slotrank,
1690 },
1691 .addr_update = {
1692 .addr_wrap = 18,
1693 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001694 },
1695 /* DRAM command ACT */
1696 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001697 .sp_cmd_ctrl = {
1698 .command = IOSAV_ACT,
1699 .ranksel_ap = 1,
1700 },
1701 .subseq_ctrl = {
1702 .cmd_executions = 1,
1703 .cmd_delay_gap = 3,
1704 .post_ssq_wait = ctrl->tRCD,
1705 .data_direction = SSQ_NA,
1706 },
1707 .sp_cmd_addr = {
1708 .address = 0,
1709 .rowbits = 6,
1710 .bank = 0,
1711 .rank = slotrank,
1712 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001713 },
1714 /* DRAM command RD */
1715 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001716 .sp_cmd_ctrl = {
1717 .command = IOSAV_RD,
1718 .ranksel_ap = 3,
1719 },
1720 .subseq_ctrl = {
1721 .cmd_executions = 1,
1722 .cmd_delay_gap = 3,
1723 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001724 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001725 ctrl->timings[channel][slotrank].io_latency,
1726 .data_direction = SSQ_RD,
1727 },
1728 .sp_cmd_addr = {
1729 .address = 8,
1730 .rowbits = 6,
1731 .bank = 0,
1732 .rank = slotrank,
1733 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001734 },
1735 };
1736 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001737
Angel Pons7c49cb82020-03-16 23:17:32 +01001738 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001739 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001740
Angel Pons88521882020-01-05 20:21:20 +01001741 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001742 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001743 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001744 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001745 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001746
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001747 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1748 ctrl->timings[channel][slotrank].lanes[lane].timB +=
Angel Pons820bce72020-11-14 17:02:55 +01001749 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001750
1751 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001752 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1753 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001754 }
1755 }
Angel Pons88521882020-01-05 20:21:20 +01001756 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001757}
1758
Angel Pons7d115132020-11-14 01:44:44 +01001759static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001760{
Angel Pons7d115132020-11-14 01:44:44 +01001761 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001762
Angel Pons7d115132020-11-14 01:44:44 +01001763 FOR_ALL_POPULATED_CHANNELS {
1764 /* choose an existing rank */
1765 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001766
Angel Pons7d115132020-11-14 01:44:44 +01001767 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001768
Angel Pons7d115132020-11-14 01:44:44 +01001769 /* Execute command queue */
1770 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001771
Angel Pons7d115132020-11-14 01:44:44 +01001772 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001773
Angel Pons7d115132020-11-14 01:44:44 +01001774 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1775 }
1776
1777 /* Refresh disable */
1778 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1779
1780 FOR_ALL_POPULATED_CHANNELS {
1781 /* Execute the same command queue */
1782 iosav_run_once(channel);
1783
1784 wait_for_iosav(channel);
1785 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786}
1787
Angel Pons7c49cb82020-03-16 23:17:32 +01001788/*
1789 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001790 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001791 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1792 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1793 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1794 * CLK/ADDR/CMD signals have the same routing delay.
1795 *
1796 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1797 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1798 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001799 */
Angel Pons820bce72020-11-14 17:02:55 +01001800static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001801{
Angel Pons820bce72020-11-14 17:02:55 +01001802 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001803
Angel Pons7d115132020-11-14 01:44:44 +01001804 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001805
Angel Pons7c49cb82020-03-16 23:17:32 +01001806 /* Enable write leveling on all ranks
1807 Disable all DQ outputs
1808 Only NOP is allowed in this mode */
1809 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1810 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001811 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001812
Angel Ponsa1f17142020-11-15 12:50:03 +01001813 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001814 const union gdcr_training_mod_reg training_mod = {
1815 .write_leveling_mode = 1,
1816 .enable_dqs_wl = 5,
1817 .odt_always_on = 1,
1818 .force_drive_enable = 1,
1819 };
1820 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001821
1822 toggle_io_reset();
1823
Angel Pons7c49cb82020-03-16 23:17:32 +01001824 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001825 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001826 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001827 if (err)
1828 return err;
1829 }
1830
Angel Pons7c49cb82020-03-16 23:17:32 +01001831 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001832 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001833 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001834
Angel Pons88521882020-01-05 20:21:20 +01001835 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836
1837 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001838 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001839
Angel Pons7c49cb82020-03-16 23:17:32 +01001840 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001841 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001842
1843 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001844 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001845 MCHBAR32(IOSAV_STATUS_ch(channel));
1846 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001847
Angel Ponsffd50152020-11-12 11:03:10 +01001848 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001849
Angel Pons7c49cb82020-03-16 23:17:32 +01001850 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001851 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001852
Angel Pons88521882020-01-05 20:21:20 +01001853 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001854 }
1855
1856 toggle_io_reset();
1857
Angel Pons820bce72020-11-14 17:02:55 +01001858 return 0;
1859}
1860
1861int write_training(ramctr_timing *ctrl)
1862{
Angel Ponsc6742232020-11-15 13:26:21 +01001863 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001864 int err;
1865
1866 FOR_ALL_POPULATED_CHANNELS
1867 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1868
Angel Pons4c76d252020-11-15 13:06:53 +01001869 printram("CPE\n");
1870
Angel Pons820bce72020-11-14 17:02:55 +01001871 err = jedec_write_leveling(ctrl);
1872 if (err)
1873 return err;
1874
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001875 printram("CPF\n");
1876
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001877 FOR_ALL_POPULATED_CHANNELS {
1878 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001879 }
1880
1881 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001882 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001883 if (err)
1884 return err;
1885 }
1886
1887 FOR_ALL_POPULATED_CHANNELS
1888 program_timings(ctrl, channel);
1889
1890 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001891 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001892
1893 FOR_ALL_POPULATED_CHANNELS
1894 program_timings(ctrl, channel);
1895
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001896 return 0;
1897}
1898
Angel Ponsbf13ef02020-11-11 18:40:06 +01001899static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001900{
1901 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
1902 int timC_delta;
1903 int lanes_ok = 0;
1904 int ctr = 0;
1905 int lane;
1906
1907 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
1908 FOR_ALL_LANES {
1909 ctrl->timings[channel][slotrank].lanes[lane].timC =
1910 saved_rt.lanes[lane].timC + timC_delta;
1911 }
1912 program_timings(ctrl, channel);
1913 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001914 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001915 }
1916
Angel Pons765d4652020-11-11 14:44:35 +01001917 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001918 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001919
Angel Pons88521882020-01-05 20:21:20 +01001920 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001921
Angel Ponsffd50152020-11-12 11:03:10 +01001922 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001923
1924 /* Program LFSR for the RD/WR subsequences */
1925 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1926 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001927
Angel Pons7c49cb82020-03-16 23:17:32 +01001928 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001929 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001930
Angel Pons88521882020-01-05 20:21:20 +01001931 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001932 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001933 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001934
1935 if (r32 == 0)
1936 lanes_ok |= 1 << lane;
1937 }
1938 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001939 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001940 break;
1941 }
1942
1943 ctrl->timings[channel][slotrank] = saved_rt;
1944
Patrick Rudolphdd662872017-10-28 18:20:11 +02001945 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001946}
1947
Angel Pons88521882020-01-05 20:21:20 +01001948static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001949{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301950 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01001951 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
1952 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001953
1954 if (patno) {
1955 u8 base8 = 0x80 >> ((patno - 1) % 8);
1956 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1957 for (i = 0; i < 32; i++) {
1958 for (j = 0; j < 16; j++) {
1959 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001960
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001961 if (invert[patno - 1][i] & (1 << (j / 2)))
1962 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001963
1964 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001965 }
1966 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001967 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001968 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1969 for (j = 0; j < 16; j++) {
1970 const u32 val = pattern[i][j];
1971 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1972 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001973 }
1974 sfence();
1975 }
Angel Pons765d4652020-11-11 14:44:35 +01001976
1977 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001978}
1979
Angel Pons88521882020-01-05 20:21:20 +01001980static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001981{
Angel Pons7d115132020-11-14 01:44:44 +01001982 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001983
Angel Pons7c49cb82020-03-16 23:17:32 +01001984 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001986
1987 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001988 dram_mrscommands(ctrl);
1989
1990 toggle_io_reset();
1991}
1992
Angel Ponsbf13ef02020-11-11 18:40:06 +01001993#define CT_MIN_PI -127
1994#define CT_MAX_PI 128
1995#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1996
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001997#define MIN_C320C_LEN 13
1998
1999static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2000{
2001 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2002 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002003 int command_pi;
2004 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002005 int delta = 0;
2006
2007 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2008
2009 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002010 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002011 }
2012
2013 ctrl->cmd_stretch[channel] = cmd_stretch;
2014
Angel Pons7a612742020-11-12 13:34:03 +01002015 const union tc_rap_reg tc_rap = {
2016 .tRRD = ctrl->tRRD,
2017 .tRTP = ctrl->tRTP,
2018 .tCKE = ctrl->tCKE,
2019 .tWTR = ctrl->tWTR,
2020 .tFAW = ctrl->tFAW,
2021 .tWR = ctrl->tWR,
2022 .tCMD = ctrl->cmd_stretch[channel],
2023 };
2024 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002025
2026 if (ctrl->cmd_stretch[channel] == 2)
2027 delta = 2;
2028 else if (ctrl->cmd_stretch[channel] == 0)
2029 delta = 4;
2030
2031 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002032 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002033 }
2034
Angel Ponsbf13ef02020-11-11 18:40:06 +01002035 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002036 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002037 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002038 }
2039 program_timings(ctrl, channel);
2040 reprogram_320c(ctrl);
2041 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002042 stat[slotrank][command_pi - CT_MIN_PI] =
2043 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002044 }
2045 }
2046 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002047 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002048
Angel Ponsbf13ef02020-11-11 18:40:06 +01002049 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002050 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2051 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002052
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002053 if (rn.all || rn.length < MIN_C320C_LEN) {
2054 FOR_ALL_POPULATED_RANKS {
2055 ctrl->timings[channel][slotrank] =
2056 saved_timings[channel][slotrank];
2057 }
2058 return MAKE_ERR;
2059 }
2060 }
2061
2062 return 0;
2063}
2064
Angel Pons7c49cb82020-03-16 23:17:32 +01002065/*
2066 * Adjust CMD phase shift and try multiple command rates.
2067 * A command rate of 2T doubles the time needed for address and command decode.
2068 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002069int command_training(ramctr_timing *ctrl)
2070{
2071 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002072
2073 FOR_ALL_POPULATED_CHANNELS {
2074 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002075 }
2076
2077 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002078 int cmdrate, err;
2079
2080 /*
2081 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002082 * Issue:
2083 * While c320c discovery seems to succeed raminit will fail in write training.
2084 *
2085 * Workaround:
2086 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2087 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002088 *
2089 * Single DIMM per channel:
2090 * Try command rate 1T and 2T
2091 */
2092 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002093 if (ctrl->tCMD)
2094 /* XMP gives the CMD rate in clock ticks, not ns */
2095 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002096
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002097 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002098 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2099
2100 if (!err)
2101 break;
2102 }
2103
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002104 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002105 printk(BIOS_EMERG, "c320c discovery failed\n");
2106 return err;
2107 }
2108
Angel Pons891f2bc2020-01-10 01:27:28 +01002109 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002110 }
2111
2112 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002113 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002114
2115 reprogram_320c(ctrl);
2116 return 0;
2117}
2118
Angel Pons4c79f932020-11-14 01:26:52 +01002119static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002120{
Angel Pons96a06dd2020-11-14 00:33:18 +01002121 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002122 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002123 int lane;
2124
Angel Pons96a06dd2020-11-14 00:33:18 +01002125 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002126 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002127 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2128 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002129 }
2130 program_timings(ctrl, channel);
2131
2132 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002133 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2134 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002135 }
2136
Angel Pons88521882020-01-05 20:21:20 +01002137 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002138
Angel Ponsffd50152020-11-12 11:03:10 +01002139 iosav_write_read_mpr_sequence(
2140 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002141
Angel Pons7c49cb82020-03-16 23:17:32 +01002142 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002143 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002144
Angel Pons88521882020-01-05 20:21:20 +01002145 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146
2147 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002148 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149 }
2150 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002151
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002152 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002153 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002154 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002155
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002156 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002157 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2158 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159 return MAKE_ERR;
2160 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002161 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002162 }
2163 return 0;
2164}
2165
Angel Pons60971dc2020-11-14 00:49:38 +01002166static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2167{
2168 int slotrank, lane;
2169
2170 fill_pattern0(ctrl, channel, 0, 0);
2171 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002172 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002173 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2174 }
2175
2176 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2177 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2178 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2179 }
2180
2181 program_timings(ctrl, channel);
2182
2183 FOR_ALL_POPULATED_RANKS {
2184 wait_for_iosav(channel);
2185
2186 iosav_write_read_mpr_sequence(
2187 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2188
2189 /* Execute command queue */
2190 iosav_run_once(channel);
2191
2192 wait_for_iosav(channel);
2193 }
2194
2195 /* XXX: check any measured value ? */
2196
2197 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2198 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2199 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2200 }
2201
2202 program_timings(ctrl, channel);
2203
2204 FOR_ALL_POPULATED_RANKS {
2205 wait_for_iosav(channel);
2206
2207 iosav_write_read_mpr_sequence(
2208 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2209
2210 /* Execute command queue */
2211 iosav_run_once(channel);
2212
2213 wait_for_iosav(channel);
2214 }
2215
2216 /* XXX: check any measured value ? */
2217
2218 FOR_ALL_LANES {
2219 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2220 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2221 }
2222}
2223
Angel Pons4c79f932020-11-14 01:26:52 +01002224int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002225{
2226 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2227 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2228 int channel, slotrank, lane;
2229 int err;
2230
Angel Pons88521882020-01-05 20:21:20 +01002231 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002232
2233 toggle_io_reset();
2234
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002235 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002236 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002237
2238 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002239 }
2240
Angel Pons0c3936e2020-03-22 12:49:27 +01002241 /*
2242 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2243 * also use a single loop. It would seem that it is a debugging configuration.
2244 */
Angel Pons88521882020-01-05 20:21:20 +01002245 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2246 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002247
2248 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002249 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002250 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002251 if (err)
2252 return err;
2253 }
2254
Angel Pons88521882020-01-05 20:21:20 +01002255 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2256 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002257
2258 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002259 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002260 rising_edges[channel][slotrank]);
2261 if (err)
2262 return err;
2263 }
2264
Angel Pons88521882020-01-05 20:21:20 +01002265 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002266
2267 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2268 ctrl->timings[channel][slotrank].lanes[lane].falling =
2269 falling_edges[channel][slotrank][lane];
2270 ctrl->timings[channel][slotrank].lanes[lane].rising =
2271 rising_edges[channel][slotrank][lane];
2272 }
2273
2274 FOR_ALL_POPULATED_CHANNELS {
2275 program_timings(ctrl, channel);
2276 }
2277
Angel Pons50a6fe72020-11-14 01:18:14 +01002278 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002279 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002280 }
2281 return 0;
2282}
2283
Angel Pons08f749d2020-11-17 16:50:56 +01002284static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002285{
Angel Pons08f749d2020-11-17 16:50:56 +01002286 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2287
Angel Pons7c49cb82020-03-16 23:17:32 +01002288 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002289 int lower[NUM_LANES];
2290 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002291 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002292
2293 FOR_ALL_LANES {
2294 lower[lane] = 0;
2295 upper[lane] = MAX_EDGE_TIMING;
2296 }
2297
Angel Pons08f749d2020-11-17 16:50:56 +01002298 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002299 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002300 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002301 };
2302 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2303 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002304
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002305 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2306 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002308
Angel Pons08f749d2020-11-17 16:50:56 +01002309 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002310 FOR_ALL_LANES {
2311 ctrl->timings[channel][slotrank].lanes[lane].
Angel Pons08f749d2020-11-17 16:50:56 +01002312 rising = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002313 ctrl->timings[channel][slotrank].lanes[lane].
Angel Pons08f749d2020-11-17 16:50:56 +01002314 falling = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002315 }
2316 program_timings(ctrl, channel);
2317
2318 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002319 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2320 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002321 }
Angel Pons88521882020-01-05 20:21:20 +01002322 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002323
Angel Ponsffd50152020-11-12 11:03:10 +01002324 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002325
Angel Pons7c49cb82020-03-16 23:17:32 +01002326 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002327 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002328
Angel Pons88521882020-01-05 20:21:20 +01002329 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002331 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002332 }
2333
Angel Pons7c49cb82020-03-16 23:17:32 +01002334 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002335 raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002337
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002338 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002339 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002340 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002341
2342 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2343 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002344
2345 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2346
2347 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2348 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2349 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002350 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002351
2352 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2353 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2354
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002355 edges[lane] = (lower[lane] + upper[lane]) / 2;
2356 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002357 printk(BIOS_EMERG, "edge write discovery failed: "
2358 "%d, %d, %d\n", channel, slotrank, lane);
2359
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002360 return MAKE_ERR;
2361 }
2362 }
2363 }
2364 }
2365
Angel Ponsa93f46e2020-11-17 16:54:01 +01002366 /* Restore nominal Vref after training */
2367 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002368 printram("CPA\n");
2369 return 0;
2370}
2371
Angel Pons08f749d2020-11-17 16:50:56 +01002372int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002373{
2374 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002375 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2376 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002377
Angel Pons7c49cb82020-03-16 23:17:32 +01002378 /*
2379 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2380 * also use a single loop. It would seem that it is a debugging configuration.
2381 */
Angel Pons88521882020-01-05 20:21:20 +01002382 MCHBAR32(IOSAV_DC_MASK) = 0x300;
Angel Pons08f749d2020-11-17 16:50:56 +01002383 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002384
2385 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002386 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002387 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002388 if (err)
2389 return err;
2390 }
2391
Angel Pons88521882020-01-05 20:21:20 +01002392 MCHBAR32(IOSAV_DC_MASK) = 0x200;
Angel Pons08f749d2020-11-17 16:50:56 +01002393 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002394
2395 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002396 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002397 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002398 if (err)
2399 return err;
2400 }
2401
Angel Pons88521882020-01-05 20:21:20 +01002402 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002403
2404 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2405 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002406 falling_edges[channel][slotrank][lane];
2407
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002408 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002409 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002410 }
2411
2412 FOR_ALL_POPULATED_CHANNELS
2413 program_timings(ctrl, channel);
2414
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002415 return 0;
2416}
2417
Angel Pons2a7d7522020-11-19 12:49:07 +01002418static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002419{
Angel Pons88521882020-01-05 20:21:20 +01002420 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002421
Angel Ponsffd50152020-11-12 11:03:10 +01002422 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002423
Angel Pons7c49cb82020-03-16 23:17:32 +01002424 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002425 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002426
Angel Pons88521882020-01-05 20:21:20 +01002427 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002428}
2429
Angel Pons2a7d7522020-11-19 12:49:07 +01002430static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002431{
Angel Pons2a7d7522020-11-19 12:49:07 +01002432 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24);
2433 udelay(2);
2434}
2435
2436int aggressive_write_training(ramctr_timing *ctrl)
2437{
2438 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002439 int i, pat;
2440
2441 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2442 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2443 int channel, slotrank, lane;
2444
Angel Pons9fbb1b02020-11-19 12:53:36 +01002445 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2446 if (!IS_IVY_CPU(ctrl->cpu))
2447 return 0;
2448
2449 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2450 return 0;
2451
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002452 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2453 lower[channel][slotrank][lane] = 0;
2454 upper[channel][slotrank][lane] = MAX_TIMC;
2455 }
2456
Angel Pons2a7d7522020-11-19 12:49:07 +01002457 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2458 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2459
2460 if (enable_iosav_opt)
2461 MCHBAR32(MCMNTS_SPARE) = 1;
2462
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002463 printram("discover timC write:\n");
2464
Angel Pons2a7d7522020-11-19 12:49:07 +01002465 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002466 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002467 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002468
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002469 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2470 FOR_ALL_POPULATED_RANKS {
2471 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002472 u32 raw_stats[MAX_TIMC + 1];
2473 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002474
2475 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002476 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002477
2478 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002479
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002480 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002481 FOR_ALL_LANES {
2482 ctrl->timings[channel][slotrank]
2483 .lanes[lane].timC = timC;
2484 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002485 program_timings(ctrl, channel);
2486
Angel Pons2a7d7522020-11-19 12:49:07 +01002487 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002488
Angel Pons098240eb2020-03-22 12:55:32 +01002489 raw_stats[timC] = MCHBAR32(
2490 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002491 }
2492 FOR_ALL_LANES {
2493 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002494 for (timC = 0; timC < MAX_TIMC; timC++) {
2495 stats[timC] = !!(raw_stats[timC]
2496 & (1 << lane));
2497 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002498
Angel Pons7c49cb82020-03-16 23:17:32 +01002499 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002500 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002501 printk(BIOS_EMERG,
2502 "timC write discovery failed: "
2503 "%d, %d, %d\n", channel,
2504 slotrank, lane);
2505
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002506 return MAKE_ERR;
2507 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002508 printram("timC: %d, %d, %d: "
2509 "0x%02x-0x%02x-0x%02x, "
2510 "0x%02x-0x%02x\n", channel, slotrank,
2511 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002512 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002513 rn.end - ctrl->timC_offset[i]);
2514
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002515 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002516 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002517 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002518
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002520 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002521 upper[channel][slotrank][lane]);
2522
2523 }
2524 }
2525 }
2526 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002527 }
2528
Angel Pons2a7d7522020-11-19 12:49:07 +01002529 FOR_ALL_CHANNELS {
2530 /* Restore nominal write Vref after training */
2531 set_write_vref(channel, 0);
2532 }
2533
2534 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2535 if (enable_iosav_opt)
2536 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002537
2538 printram("CPB\n");
2539
2540 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002541 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002542 (lower[channel][slotrank][lane] +
2543 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002544
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002545 ctrl->timings[channel][slotrank].lanes[lane].timC =
2546 (lower[channel][slotrank][lane] +
2547 upper[channel][slotrank][lane]) / 2;
2548 }
2549 FOR_ALL_POPULATED_CHANNELS {
2550 program_timings(ctrl, channel);
2551 }
2552 return 0;
2553}
2554
Angel Pons88521882020-01-05 20:21:20 +01002555void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002556{
2557 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002558 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002559
2560 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2561 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002562 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002563 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002564 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002565 printram("normalize %d, %d, %d: mat %d\n",
2566 channel, slotrank, lane, mat);
2567
Felix Heldef4fe3e2019-12-31 14:15:05 +01002568 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002569 printram("normalize %d, %d, %d: delta %d\n",
2570 channel, slotrank, lane, delta);
2571
Angel Pons88521882020-01-05 20:21:20 +01002572 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002573 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002574 }
2575
2576 FOR_ALL_POPULATED_CHANNELS {
2577 program_timings(ctrl, channel);
2578 }
2579}
2580
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002581int channel_test(ramctr_timing *ctrl)
2582{
2583 int channel, slotrank, lane;
2584
2585 slotrank = 0;
2586 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002587 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002588 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002589 return MAKE_ERR;
2590 }
2591 FOR_ALL_POPULATED_CHANNELS {
2592 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002593 }
2594
2595 for (slotrank = 0; slotrank < 4; slotrank++)
2596 FOR_ALL_CHANNELS
2597 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2598 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002599 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2600 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002601 }
Angel Pons88521882020-01-05 20:21:20 +01002602 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002603
Angel Ponsffd50152020-11-12 11:03:10 +01002604 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002605
Angel Pons7c49cb82020-03-16 23:17:32 +01002606 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002607 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002608
Angel Pons88521882020-01-05 20:21:20 +01002609 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002610 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002611 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002612 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2613 channel, slotrank, lane);
2614 return MAKE_ERR;
2615 }
2616 }
2617 return 0;
2618}
2619
Patrick Rudolphdd662872017-10-28 18:20:11 +02002620void channel_scrub(ramctr_timing *ctrl)
2621{
2622 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002623 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002624
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002625 FOR_ALL_POPULATED_CHANNELS {
2626 wait_for_iosav(channel);
2627 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002628 }
2629
2630 /*
2631 * During runtime the "scrubber" will periodically scan through the memory in the
2632 * physical address space, to identify and fix CRC errors.
2633 * The following loops writes to every DRAM address, setting the ECC bits to the
2634 * correct value. A read from this location will no longer return a CRC error,
2635 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002636 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002637 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2638 * and firmware running in x86_32.
2639 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002640 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2641 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002642 for (bank = 0; bank < 8; bank++) {
2643 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002644
Angel Pons8f0757e2020-11-11 23:03:36 +01002645 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2646 const struct iosav_ssq sequence[] = {
2647 /*
2648 * DRAM command ACT
2649 * Opens the row for writing.
2650 */
2651 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002652 .sp_cmd_ctrl = {
2653 .command = IOSAV_ACT,
2654 .ranksel_ap = 1,
2655 },
2656 .subseq_ctrl = {
2657 .cmd_executions = 1,
2658 .cmd_delay_gap = gap,
2659 .post_ssq_wait = ctrl->tRCD,
2660 .data_direction = SSQ_NA,
2661 },
2662 .sp_cmd_addr = {
2663 .address = row,
2664 .rowbits = 6,
2665 .bank = bank,
2666 .rank = slotrank,
2667 },
2668 .addr_update = {
2669 .inc_addr_1 = 1,
2670 .addr_wrap = 18,
2671 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002672 },
2673 /*
2674 * DRAM command WR
2675 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2676 * bytes.
2677 */
2678 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002679 .sp_cmd_ctrl = {
2680 .command = IOSAV_WR,
2681 .ranksel_ap = 1,
2682 },
2683 .subseq_ctrl = {
2684 .cmd_executions = 129,
2685 .cmd_delay_gap = 4,
2686 .post_ssq_wait = ctrl->tWTR +
2687 ctrl->CWL + 8,
2688 .data_direction = SSQ_WR,
2689 },
2690 .sp_cmd_addr = {
2691 .address = row,
2692 .rowbits = 0,
2693 .bank = bank,
2694 .rank = slotrank,
2695 },
2696 .addr_update = {
2697 .inc_addr_8 = 1,
2698 .addr_wrap = 9,
2699 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002700 },
2701 /*
2702 * DRAM command PRE
2703 * Closes the row.
2704 */
2705 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002706 .sp_cmd_ctrl = {
2707 .command = IOSAV_PRE,
2708 .ranksel_ap = 1,
2709 },
2710 .subseq_ctrl = {
2711 .cmd_executions = 1,
2712 .cmd_delay_gap = 4,
2713 .post_ssq_wait = ctrl->tRP,
2714 .data_direction = SSQ_NA,
2715 },
2716 .sp_cmd_addr = {
2717 .address = 0,
2718 .rowbits = 6,
2719 .bank = bank,
2720 .rank = slotrank,
2721 },
2722 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002723 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002724 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002725 },
2726 };
2727 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002728
2729 /* Execute command queue */
2730 iosav_run_queue(channel, 16, 0);
2731
2732 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002733 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002734 }
2735 }
2736}
2737
Angel Pons88521882020-01-05 20:21:20 +01002738void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002739{
2740 int channel;
2741
Angel Pons7c49cb82020-03-16 23:17:32 +01002742 /* 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 +01002743 static u32 seeds[NUM_CHANNELS][3] = {
2744 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2745 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2746 };
2747 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002748 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002749 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2750 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2751 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002752 }
2753}
2754
Angel Pons89ae6b82020-03-21 13:23:32 +01002755void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002756{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002757 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002758 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002759 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002760 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002761 }
2762}
2763
Angel Pons88521882020-01-05 20:21:20 +01002764void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002765{
2766 int channel;
2767
2768 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002769 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002770 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002771 }
2772
2773 udelay(1);
2774
2775 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002776 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002777 }
2778}
2779
Angel Pons7c49cb82020-03-16 23:17:32 +01002780void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002781{
Angel Pons11463322020-11-19 11:04:28 +01002782 /* Use a larger delay when running fast to improve stability */
2783 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2784
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002785 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002786
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002787 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002788 int min_pi = 10000;
2789 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002790
2791 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002792 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2793 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002794 }
2795
Angel Pons7a612742020-11-12 13:34:03 +01002796 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002797
Angel Pons7a612742020-11-12 13:34:03 +01002798 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002799
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002800 dram_odt_stretch(ctrl, channel);
2801
Angel Pons7a612742020-11-12 13:34:03 +01002802 const union tc_rwp_reg tc_rwp = {
2803 .tRRDR = 0,
2804 .tRRDD = val,
2805 .tWWDR = val,
2806 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002807 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002808 .tWRDRDD = tWRDRDD,
2809 .tRWSR = 2,
2810 .dec_wrd = 1,
2811 };
2812 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002813 }
2814}
2815
Angel Pons88521882020-01-05 20:21:20 +01002816void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002817{
2818 int channel;
2819 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002820 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2821 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002822 }
2823}
2824
Angel Pons7c49cb82020-03-16 23:17:32 +01002825/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2826static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002827{
Angel Pons88521882020-01-05 20:21:20 +01002828 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002829}
2830
Angel Pons7c49cb82020-03-16 23:17:32 +01002831/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002832void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002833{
2834 int channel;
2835 int t1_cycles = 0, t1_ns = 0, t2_ns;
2836 int t3_ns;
2837 u32 r32;
2838
Angel Pons7c49cb82020-03-16 23:17:32 +01002839 /* FIXME: This register only exists on Ivy Bridge */
2840 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002841
Angel Pons7a612742020-11-12 13:34:03 +01002842 FOR_ALL_CHANNELS {
2843 union tc_othp_reg tc_othp = {
2844 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2845 };
2846 tc_othp.tCPDED = 1;
2847 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2848 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002849
Angel Pons09fc4b92020-11-19 12:02:07 +01002850 /* 64 DCLKs until idle, decision per rank */
2851 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002852
Felix Heldf9b826a2018-07-30 17:56:52 +02002853 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002854 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002855
Angel Pons88521882020-01-05 20:21:20 +01002856 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
2857 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002858
2859 FOR_ALL_CHANNELS {
2860 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002861 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002862 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002863 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002864 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002865 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002866 case 1:
2867 case 4:
2868 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002869 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002870 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002871 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002872 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002873 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002874 break;
2875 }
2876 }
2877
Felix Held50b7ed22019-12-30 20:41:54 +01002878 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002879 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002880 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002881
Angel Pons7a612742020-11-12 13:34:03 +01002882 FOR_ALL_CHANNELS {
2883 union tc_rfp_reg tc_rfp = {
2884 .raw = MCHBAR32(TC_RFP_ch(channel)),
2885 };
2886 tc_rfp.refresh_2x_control = 1;
2887 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2888 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002889
Angel Ponsdc5539f2020-11-12 12:44:25 +01002890 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2891 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002892 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002893
Angel Pons7c49cb82020-03-16 23:17:32 +01002894 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002895 FOR_ALL_POPULATED_CHANNELS
2896 break;
2897
Angel Pons88521882020-01-05 20:21:20 +01002898 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2899 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002900 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002901 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002902 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002903 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002904 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002905 t1_ns += 500;
2906
Angel Pons88521882020-01-05 20:21:20 +01002907 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002908 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002909 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002910 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002911 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002912 t3_ns = 500;
2913 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002914
2915 /* The graphics driver will use these watermark values */
2916 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002917 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002918 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2919 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920}
2921
Angel Pons88521882020-01-05 20:21:20 +01002922void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002923{
Angel Ponsc6742232020-11-15 13:26:21 +01002924 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002925
Angel Pons7c49cb82020-03-16 23:17:32 +01002926 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002927 const union tc_rap_reg tc_rap = {
2928 .tRRD = ctrl->tRRD,
2929 .tRTP = ctrl->tRTP,
2930 .tCKE = ctrl->tCKE,
2931 .tWTR = ctrl->tWTR,
2932 .tFAW = ctrl->tFAW,
2933 .tWR = ctrl->tWR,
2934 .tCMD = ctrl->cmd_stretch[channel],
2935 };
2936 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002937 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002938
2939 udelay(1);
2940
2941 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002942 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002943 }
2944
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002945 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002946 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002947
2948 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002949 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002950 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002951 }
2952
2953 printram("CPE\n");
2954
Angel Pons88521882020-01-05 20:21:20 +01002955 MCHBAR32(GDCRTRAININGMOD) = 0;
2956 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002957
2958 printram("CP5b\n");
2959
2960 FOR_ALL_POPULATED_CHANNELS {
2961 program_timings(ctrl, channel);
2962 }
2963
2964 u32 reg, addr;
2965
Angel Pons7c49cb82020-03-16 23:17:32 +01002966 /* Poll for RCOMP */
2967 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2968 ;
2969
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002970 do {
Angel Pons88521882020-01-05 20:21:20 +01002971 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972 } while ((reg & 0x14) == 0);
2973
Angel Pons7c49cb82020-03-16 23:17:32 +01002974 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002975 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002976 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002977
Angel Pons7c49cb82020-03-16 23:17:32 +01002978 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002979 udelay(500);
2980
2981 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002982 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002983 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002984 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002985 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002986 MCHBAR32(addr) = reg;
2987
Angel Pons7c49cb82020-03-16 23:17:32 +01002988 /* Wait 10ns for ranks to settle */
2989 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002990
2991 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
2992 MCHBAR32(addr) = reg;
2993
Angel Pons7c49cb82020-03-16 23:17:32 +01002994 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002995 write_reset(ctrl);
2996 }
2997
Angel Pons7c49cb82020-03-16 23:17:32 +01002998 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002999 dram_mrscommands(ctrl);
3000
3001 printram("CP5c\n");
3002
Angel Pons88521882020-01-05 20:21:20 +01003003 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003004
3005 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003006 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003007 udelay(2);
3008 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003009}