blob: 4ba2f0de82b761bd653f77c1af219ca8e1df48f1 [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
Angel Pons12bd8ab2020-11-13 23:10:52 +01003#include <assert.h>
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01004#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <console/console.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01006#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010010#include <northbridge/intel/sandybridge/chip.h>
11#include <device/pci_def.h>
12#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020013#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020014
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010015#include "raminit_native.h"
16#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010017#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#include "sandybridge.h"
19
Angel Pons7c49cb82020-03-16 23:17:32 +010020/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010021
22static void sfence(void)
23{
24 asm volatile ("sfence");
25}
26
Angel Pons7c49cb82020-03-16 23:17:32 +010027/* Toggle IO reset bit */
28static void toggle_io_reset(void)
29{
Angel Pons88521882020-01-05 20:21:20 +010030 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010031 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010033 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010034 udelay(1);
35}
36
37static u32 get_XOVER_CLK(u8 rankmap)
38{
39 return rankmap << 24;
40}
41
42static u32 get_XOVER_CMD(u8 rankmap)
43{
44 u32 reg;
45
Angel Pons7c49cb82020-03-16 23:17:32 +010046 /* Enable xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010047 reg = 0x4000;
48
Angel Pons7c49cb82020-03-16 23:17:32 +010049 /* Enable xover ctl */
50 if (rankmap & 0x03)
51 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010052
Angel Pons7c49cb82020-03-16 23:17:32 +010053 if (rankmap & 0x0c)
54 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010055
56 return reg;
57}
58
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010059void dram_find_common_params(ramctr_timing *ctrl)
60{
61 size_t valid_dimms;
62 int channel, slot;
63 dimm_info *dimms = &ctrl->info;
64
65 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
66 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010067
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010068 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010069
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010070 const dimm_attr *dimm = &dimms->dimm[channel][slot];
71 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
72 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010073
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010074 valid_dimms++;
75
76 /* Find all possible CAS combinations */
77 ctrl->cas_supported &= dimm->cas_supported;
78
79 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010080 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
81 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
82 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
84 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010085 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010086 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
87 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
88 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
89 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
90 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030091 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
92 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010093 }
94
95 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010096 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
97
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010098 if (!valid_dimms)
99 die("No valid DIMMs found");
100}
101
Angel Pons88521882020-01-05 20:21:20 +0100102void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100103{
104 u32 reg;
105 int channel;
106
107 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100108 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100110 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
111 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100112
Angel Pons7c49cb82020-03-16 23:17:32 +0100113 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100115 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
116 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100117 }
118}
119
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100120static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100121{
Angel Pons89ae6b82020-03-21 13:23:32 +0100122 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100123
124 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 /*
126 * ODT stretch:
127 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
128 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100129 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100130 if (stretch == 2)
131 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100132
Angel Pons88521882020-01-05 20:21:20 +0100133 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100134 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100136 } else {
Angel Pons88521882020-01-05 20:21:20 +0100137 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100138 union tc_othp_reg tc_othp = {
139 .raw = MCHBAR32(addr),
140 };
141 tc_othp.odt_delay_d0 = stretch;
142 tc_othp.odt_delay_d1 = stretch;
143 MCHBAR32(addr) = tc_othp.raw;
Iru Cai89af71c2018-08-16 16:46:27 +0800144 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100145 }
146}
147
148void dram_timing_regs(ramctr_timing *ctrl)
149{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100150 int channel;
151
Angel Pons81378062020-11-12 13:46:21 +0100152 /* BIN parameters */
153 const union tc_dbp_reg tc_dbp = {
154 .tRCD = ctrl->tRCD,
155 .tRP = ctrl->tRP,
156 .tAA = ctrl->CAS,
157 .tCWL = ctrl->CWL,
158 .tRAS = ctrl->tRAS,
159 };
160
161 /* Regular access parameters */
162 const union tc_rap_reg tc_rap = {
163 .tRRD = ctrl->tRRD,
164 .tRTP = ctrl->tRTP,
165 .tCKE = ctrl->tCKE,
166 .tWTR = ctrl->tWTR,
167 .tFAW = ctrl->tFAW,
168 .tWR = ctrl->tWR,
169 .tCMD = 3,
170 };
171
172 /* Other parameters */
173 const union tc_othp_reg tc_othp = {
174 .tXPDLL = ctrl->tXPDLL,
175 .tXP = ctrl->tXP,
176 .tAONPD = ctrl->tAONPD,
177 .tCPDED = 2,
178 .tPRPDEN = 2,
179 };
180
181 /*
182 * If tXP and tXPDLL are very high, we need to increase them by one.
183 * This can only happen on Ivy Bridge, and when overclocking the RAM.
184 */
185 const union tc_dtp_reg tc_dtp = {
186 .overclock_tXP = ctrl->tXP >= 8,
187 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
188 };
189
190 /*
191 * TC-Refresh timing parameters:
192 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
193 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
194 */
195 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
196
197 const union tc_rftp_reg tc_rftp = {
198 .tREFI = ctrl->tREFI,
199 .tRFC = ctrl->tRFC,
200 .tREFIx9 = val32 / 1024,
201 };
202
203 /* Self-refresh timing parameters */
204 const union tc_srftp_reg tc_srftp = {
205 .tXSDLL = tDLLK,
206 .tXS_offset = ctrl->tXSOffset,
207 .tZQOPER = tDLLK - ctrl->tXSOffset,
208 .tMOD = ctrl->tMOD - 8,
209 };
210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100211 FOR_ALL_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +0100212 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
213 MCHBAR32(TC_DBP_ch(channel)) = tc_dbp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100214
Angel Pons7a612742020-11-12 13:34:03 +0100215 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
216 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100217
Angel Pons7a612742020-11-12 13:34:03 +0100218 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
219 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100220
Angel Ponsca2f68a2020-03-22 13:15:12 +0100221 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Pons81378062020-11-12 13:46:21 +0100222 /* Debug parameters - only applies to Ivy Bridge */
Angel Pons7a612742020-11-12 13:34:03 +0100223 MCHBAR32(TC_DTP_ch(channel)) = tc_dtp.raw;
Angel Ponsca2f68a2020-03-22 13:15:12 +0100224 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100225
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100226 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100227
Angel Pons7a612742020-11-12 13:34:03 +0100228 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
229 MCHBAR32(TC_RFTP_ch(channel)) = tc_rftp.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +0100230
Angel Pons7a612742020-11-12 13:34:03 +0100231 union tc_rfp_reg tc_rfp = {
232 .raw = MCHBAR32(TC_RFP_ch(channel)),
233 };
234 tc_rfp.oref_ri = 0xff;
235 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100236
Angel Pons7a612742020-11-12 13:34:03 +0100237 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
238 MCHBAR32(TC_SRFTP_ch(channel)) = tc_srftp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100239 }
240}
241
242void dram_dimm_mapping(ramctr_timing *ctrl)
243{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100244 int channel;
245 dimm_info *info = &ctrl->info;
246
247 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200248 dimm_attr *dimmA, *dimmB;
249 u32 reg = 0;
250
Angel Pons7c49cb82020-03-16 23:17:32 +0100251 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100252 dimmA = &info->dimm[channel][0];
253 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100254 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100255 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100256 dimmA = &info->dimm[channel][1];
257 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100258 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100259 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100260
Nico Huberac4f2162017-10-01 18:14:43 +0200261 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100262 reg |= (dimmA->size_mb / 256) << 0;
263 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200264 reg |= (dimmA->width / 8 - 1) << 19;
265 }
266
267 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100268 reg |= (dimmB->size_mb / 256) << 8;
269 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200270 reg |= (dimmB->width / 8 - 1) << 20;
271 }
272
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200273 /*
274 * Rank interleave: Bit 16 of the physical address space sets
275 * the rank to use in a dual single rank DIMM configuration.
276 * That results in every 64KiB being interleaved between two ranks.
277 */
278 reg |= 1 << 21;
279 /* Enhanced interleave */
280 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200281
Angel Pons7c49cb82020-03-16 23:17:32 +0100282 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100283 ctrl->mad_dimm[channel] = reg;
284 } else {
285 ctrl->mad_dimm[channel] = 0;
286 }
287 }
288}
289
Patrick Rudolphdd662872017-10-28 18:20:11 +0200290void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100291{
292 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200293 u32 ecc;
294
295 if (ctrl->ecc_enabled)
296 ecc = training ? (1 << 24) : (3 << 24);
297 else
298 ecc = 0;
299
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100300 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200301 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100302 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200303
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200304 if (ctrl->ecc_enabled)
305 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100306}
307
Angel Pons88521882020-01-05 20:21:20 +0100308void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100309{
310 u32 reg, ch0size, ch1size;
311 u8 val;
312 reg = 0;
313 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100314
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100315 if (training) {
316 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
317 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
318 } else {
319 ch0size = ctrl->channel_size_mb[0];
320 ch1size = ctrl->channel_size_mb[1];
321 }
322
323 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100324 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100325 val = ch1size / 256;
326 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100327 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100328 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100329 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100330
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100331 } else {
Angel Pons88521882020-01-05 20:21:20 +0100332 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100333 val = ch0size / 256;
334 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100335 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100336 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100337 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100338 }
339}
340
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100341#define DEFAULT_PCI_MMIO_SIZE 2048
342
343static unsigned int get_mmio_size(void)
344{
345 const struct device *dev;
346 const struct northbridge_intel_sandybridge_config *cfg = NULL;
347
Angel Ponsb31d1d72020-01-10 01:35:09 +0100348 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100349 if (dev)
350 cfg = dev->chip_info;
351
352 /* If this is zero, it just means devicetree.cb didn't set it */
353 if (!cfg || cfg->pci_mmio_size == 0)
354 return DEFAULT_PCI_MMIO_SIZE;
355 else
356 return cfg->pci_mmio_size;
357}
358
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200359/*
360 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
361 * The ME/PCU/.. has the ability to change this.
362 * Return 0: ECC is optional
363 * Return 1: ECC is forced
364 */
365bool get_host_ecc_forced(void)
366{
367 /* read Capabilities A Register */
368 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
369 return !!(reg32 & (1 << 24));
370}
371
372/*
373 * Returns the ECC capability.
374 * The ME/PCU/.. has the ability to change this.
375 * Return 0: ECC is disabled
376 * Return 1: ECC is possible
377 */
378bool get_host_ecc_cap(void)
379{
380 /* read Capabilities A Register */
381 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
382 return !(reg32 & (1 << 25));
383}
384
Angel Pons88521882020-01-05 20:21:20 +0100385void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100386{
Angel Pons7c49cb82020-03-16 23:17:32 +0100387 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
388 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
389 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100390 uint16_t ggc;
391
392 mmiosize = get_mmio_size();
393
Felix Held87ddea22020-01-26 04:55:27 +0100394 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100395 if (!(ggc & 2)) {
396 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100397 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100398 } else {
399 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100400 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100401 }
402
403 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
404
405 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
406
407 mestolenbase = tom - me_uma_size;
408
Angel Pons7c49cb82020-03-16 23:17:32 +0100409 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
410
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100411 gfxstolenbase = toludbase - gfxstolen;
412 gttbase = gfxstolenbase - gttsize;
413
414 tsegbase = gttbase - tsegsize;
415
Angel Pons7c49cb82020-03-16 23:17:32 +0100416 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100417 tsegbasedelta = tsegbase & (tsegsize - 1);
418 tsegbase &= ~(tsegsize - 1);
419
420 gttbase -= tsegbasedelta;
421 gfxstolenbase -= tsegbasedelta;
422 toludbase -= tsegbasedelta;
423
Angel Pons7c49cb82020-03-16 23:17:32 +0100424 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100425 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100426 /* Reclaim is possible */
427 reclaim = 1;
428 remapbase = MAX(4096, tom - me_uma_size);
429 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
430 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100431 } else {
432 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100433 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100434 touudbase = tom - me_uma_size;
435 }
436
Angel Pons7c49cb82020-03-16 23:17:32 +0100437 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100438 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
439
Angel Pons7c49cb82020-03-16 23:17:32 +0100440 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442 val = tom & 0xfff;
443 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100444 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100445 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100446
Angel Ponsb31d1d72020-01-10 01:35:09 +0100447 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100448 val = tom & 0xfffff000;
449 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100450 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100451 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100452
Angel Pons7c49cb82020-03-16 23:17:32 +0100453 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100454 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100455 val = toludbase & 0xfff;
456 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100457 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100458 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100459
Angel Pons7c49cb82020-03-16 23:17:32 +0100460 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100461 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100462 val = touudbase & 0xfff;
463 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100464 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100465 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100466
Angel Pons7c49cb82020-03-16 23:17:32 +0100467 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100468 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100469 val = touudbase & 0xfffff000;
470 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100471 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100472 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100473
474 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100475 /* REMAP BASE */
476 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100477 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100478
Angel Pons7c49cb82020-03-16 23:17:32 +0100479 /* REMAP LIMIT */
480 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100481 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100482 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100483 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100484 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100485 val = tsegbase & 0xfff;
486 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100487 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100488 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100489
Angel Pons7c49cb82020-03-16 23:17:32 +0100490 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100491 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100492 val = gfxstolenbase & 0xfff;
493 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100494 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100495 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100496
Angel Pons7c49cb82020-03-16 23:17:32 +0100497 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100498 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100499 val = gttbase & 0xfff;
500 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100501 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100502 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100503
504 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100505 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100506 val = (0x80000 - me_uma_size) & 0xfffff000;
507 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100508 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100509 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100510
Angel Pons7c49cb82020-03-16 23:17:32 +0100511 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513 val = mestolenbase & 0xfff;
514 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100515 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100516 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100517
Angel Ponsb31d1d72020-01-10 01:35:09 +0100518 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100519 val = mestolenbase & 0xfffff000;
520 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100521 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100522 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100523
Angel Pons7c49cb82020-03-16 23:17:32 +0100524 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100525 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526 val = (0x80000 - me_uma_size) & 0xfff;
527 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100528 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
529 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100530 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100531 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100532 }
533}
534
Angel Pons88521882020-01-05 20:21:20 +0100535static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100536{
537 int channel, slotrank;
538
Angel Pons7c49cb82020-03-16 23:17:32 +0100539 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100540 channel = (ctrl->rankmap[0]) ? 0 : 1;
541
Angel Pons88521882020-01-05 20:21:20 +0100542 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100543
Angel Pons7c49cb82020-03-16 23:17:32 +0100544 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100545 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
546
Angel Ponsffd50152020-11-12 11:03:10 +0100547 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100548
Angel Pons7c49cb82020-03-16 23:17:32 +0100549 /*
550 * Execute command queue - why is bit 22 set here?!
551 *
552 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
553 */
Angel Pons38d901e2020-05-02 23:50:43 +0200554 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200555
Angel Pons88521882020-01-05 20:21:20 +0100556 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100557}
558
Angel Pons88521882020-01-05 20:21:20 +0100559void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560{
Felix Held9fe248f2018-07-31 20:59:45 +0200561 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100562 int channel;
563
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
565 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566 do {
Angel Pons88521882020-01-05 20:21:20 +0100567 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100568 } while ((reg & 0x14) == 0);
569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100572 MCHBAR32(MC_INIT_STATE_G) = reg;
573 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100574 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100575 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100576
Angel Pons7c49cb82020-03-16 23:17:32 +0100577 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100578 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100579
Angel Pons7c49cb82020-03-16 23:17:32 +0100580 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581 udelay(200);
582
Angel Pons7c49cb82020-03-16 23:17:32 +0100583 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100584 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100585
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100587 udelay(500);
588
Angel Pons7c49cb82020-03-16 23:17:32 +0100589 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100590 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100591
Angel Pons7c49cb82020-03-16 23:17:32 +0100592 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100593 udelay(1);
594
595 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200597 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100598 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100599
Angel Pons7c49cb82020-03-16 23:17:32 +0100600 /* Wait 10ns for ranks to settle */
601 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100602
603 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100604 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100605
Angel Pons7c49cb82020-03-16 23:17:32 +0100606 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100607 write_reset(ctrl);
608 }
609}
610
Angel Pons3d3bf482020-11-14 16:18:15 +0100611/*
612 * DDR3 Rank1 Address mirror swap the following pins:
613 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
614 */
615static void ddr3_mirror_mrreg(int *bank, u32 *addr)
616{
617 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
618 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
619}
620
Angel Pons7c49cb82020-03-16 23:17:32 +0100621static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100622{
Angel Pons88521882020-01-05 20:21:20 +0100623 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100624
Angel Pons3d3bf482020-11-14 16:18:15 +0100625 if (ctrl->rank_mirror[channel][slotrank])
626 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100627
Angel Pons8f0757e2020-11-11 23:03:36 +0100628 const struct iosav_ssq sequence[] = {
629 /* DRAM command MRS */
630 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200631 .sp_cmd_ctrl = {
632 .command = IOSAV_MRS,
633 },
634 .subseq_ctrl = {
635 .cmd_executions = 1,
636 .cmd_delay_gap = 4,
637 .post_ssq_wait = 4,
638 .data_direction = SSQ_NA,
639 },
640 .sp_cmd_addr = {
641 .address = val,
642 .rowbits = 6,
643 .bank = reg,
644 .rank = slotrank,
645 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100646 },
647 /* DRAM command MRS */
648 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200649 .sp_cmd_ctrl = {
650 .command = IOSAV_MRS,
651 .ranksel_ap = 1,
652 },
653 .subseq_ctrl = {
654 .cmd_executions = 1,
655 .cmd_delay_gap = 4,
656 .post_ssq_wait = 4,
657 .data_direction = SSQ_NA,
658 },
659 .sp_cmd_addr = {
660 .address = val,
661 .rowbits = 6,
662 .bank = reg,
663 .rank = slotrank,
664 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100665 },
666 /* DRAM command MRS */
667 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200668 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100669 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200670 },
671 .subseq_ctrl = {
672 .cmd_executions = 1,
673 .cmd_delay_gap = 4,
674 .post_ssq_wait = ctrl->tMOD,
675 .data_direction = SSQ_NA,
676 },
677 .sp_cmd_addr = {
678 .address = val,
679 .rowbits = 6,
680 .bank = reg,
681 .rank = slotrank,
682 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100683 },
684 };
685 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200686
Angel Pons7c49cb82020-03-16 23:17:32 +0100687 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200688 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100689}
690
Angel Pons88521882020-01-05 20:21:20 +0100691static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100692{
693 u16 mr0reg, mch_cas, mch_wr;
694 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Patrick Rudolph74203de2017-11-20 11:57:01 +0100695 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100696
Angel Pons7c49cb82020-03-16 23:17:32 +0100697 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100698 if (ctrl->CAS < 12) {
699 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
700 } else {
701 mch_cas = (u16) (ctrl->CAS - 12);
702 mch_cas = ((mch_cas << 1) | 0x1);
703 }
704
Angel Pons7c49cb82020-03-16 23:17:32 +0100705 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100706 mch_wr = mch_wr_t[ctrl->tWR - 5];
707
Angel Pons2bf28ed2020-11-12 13:49:59 +0100708 /* DLL Reset - self clearing - set after CLK frequency has been changed */
709 mr0reg = 1 << 8;
710
711 mr0reg |= (mch_cas & 0x1) << 2;
712 mr0reg |= (mch_cas & 0xe) << 3;
713 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100714
Angel Pons7c49cb82020-03-16 23:17:32 +0100715 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
Angel Pons2bf28ed2020-11-12 13:49:59 +0100716 mr0reg |= !is_mobile << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100717 return mr0reg;
718}
719
720static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
721{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200722 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100723}
724
Angel Ponsf9997482020-11-12 16:02:52 +0100725static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100726{
727 /* Get ODT based on rankmap */
728 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
729
730 if (dimms_per_ch == 1) {
731 return (const odtmap){60, 60};
732 } else {
733 return (const odtmap){120, 30};
734 }
735}
736
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100737static u32 encode_odt(u32 odt)
738{
739 switch (odt) {
740 case 30:
741 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
742 case 60:
743 return (1 << 2); // RZQ/4
744 case 120:
745 return (1 << 6); // RZQ/2
746 default:
747 case 0:
748 return 0;
749 }
750}
751
752static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
753{
754 odtmap odt;
755 u32 mr1reg;
756
Angel Ponsf9997482020-11-12 16:02:52 +0100757 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100758 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100759
760 mr1reg |= encode_odt(odt.rttnom);
761
762 return mr1reg;
763}
764
765static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
766{
767 u16 mr1reg;
768
769 mr1reg = make_mr1(ctrl, rank, channel);
770
771 write_mrreg(ctrl, channel, rank, 1, mr1reg);
772}
773
774static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
775{
Angel Pons868bca22020-11-13 13:38:04 +0100776 const u16 pasr = 0;
777 const u16 cwl = ctrl->CWL - 5;
778 const odtmap odt = get_ODT(ctrl, channel);
779
Angel Ponsdca3cb52020-11-13 13:42:07 +0100780 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100781 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
782 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100783
Angel Pons868bca22020-11-13 13:38:04 +0100784 u16 mr2reg = 0;
785 mr2reg |= pasr;
786 mr2reg |= cwl << 3;
787 mr2reg |= ctrl->auto_self_refresh << 6;
788 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100789 mr2reg |= (odt.rttwr / 60) << 9;
790
791 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100792
793 /* Program MR2 shadow */
794 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
795
796 reg32 &= 3 << 14 | 3 << 6;
797
798 reg32 |= mr2reg & ~(3 << 6);
799
800 if (rank & 1) {
801 if (srt)
802 reg32 |= 1 << (rank / 2 + 6);
803 } else {
804 if (ctrl->rank_mirror[channel][rank])
805 reg32 |= 1 << (rank / 2 + 14);
806 }
807 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100808}
809
810static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
811{
812 write_mrreg(ctrl, channel, rank, 3, 0);
813}
814
Angel Pons88521882020-01-05 20:21:20 +0100815void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100816{
817 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100818 int channel;
819
820 FOR_ALL_POPULATED_CHANNELS {
821 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100822 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100823 dram_mr2(ctrl, slotrank, channel);
824
Angel Pons7c49cb82020-03-16 23:17:32 +0100825 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100826 dram_mr3(ctrl, slotrank, channel);
827
Angel Pons7c49cb82020-03-16 23:17:32 +0100828 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100829 dram_mr1(ctrl, slotrank, channel);
830
Angel Pons7c49cb82020-03-16 23:17:32 +0100831 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100832 dram_mr0(ctrl, slotrank, channel);
833 }
834 }
835
Angel Pons8f0757e2020-11-11 23:03:36 +0100836 const struct iosav_ssq zqcl_sequence[] = {
837 /* DRAM command NOP (without ODT nor chip selects) */
838 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200839 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100840 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200841 },
842 .subseq_ctrl = {
843 .cmd_executions = 1,
844 .cmd_delay_gap = 4,
845 .post_ssq_wait = 15,
846 .data_direction = SSQ_NA,
847 },
848 .sp_cmd_addr = {
849 .address = 2,
850 .rowbits = 6,
851 .bank = 0,
852 .rank = 0,
853 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100854 },
855 /* DRAM command ZQCL */
856 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200857 .sp_cmd_ctrl = {
858 .command = IOSAV_ZQCS,
859 .ranksel_ap = 1,
860 },
861 .subseq_ctrl = {
862 .cmd_executions = 1,
863 .cmd_delay_gap = 4,
864 .post_ssq_wait = 400,
865 .data_direction = SSQ_NA,
866 },
867 .sp_cmd_addr = {
868 .address = 1024,
869 .rowbits = 6,
870 .bank = 0,
871 .rank = 0,
872 },
873 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100874 .inc_rank = 1,
875 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200876 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100877 },
878 };
879 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100880
Angel Pons7c49cb82020-03-16 23:17:32 +0100881 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200882 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100883
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100884 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100885 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100886 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100887 }
888
Angel Pons7c49cb82020-03-16 23:17:32 +0100889 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100890 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100891
892 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100893 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100894
Angel Pons88521882020-01-05 20:21:20 +0100895 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100896
897 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
898
Angel Pons7c49cb82020-03-16 23:17:32 +0100899 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100900 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901
Angel Ponsffd50152020-11-12 11:03:10 +0100902 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200903
Angel Pons7c49cb82020-03-16 23:17:32 +0100904 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200905 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100906
Angel Pons7c49cb82020-03-16 23:17:32 +0100907 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100908 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100909 }
910}
911
Felix Held3b906032020-01-14 17:05:43 +0100912static const u32 lane_base[] = {
913 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
914 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
915 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100916};
917
Angel Pons88521882020-01-05 20:21:20 +0100918void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100919{
Angel Pons737f1112020-11-13 14:07:30 +0100920 u32 reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100921 int lane;
922 int slotrank, slot;
923 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100924 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100925
926 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100927 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
928 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100929 }
930
931 for (slot = 0; slot < NUM_SLOTS; slot++)
932 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
933 case 0:
934 default:
Angel Pons88521882020-01-05 20:21:20 +0100935 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100936 break;
937 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100938 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100939 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940 break;
941 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100942 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100943 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100944 break;
945 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100946 pi_coding_ctrl[slot] =
947 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100948 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100949 break;
950 }
951
Angel Pons7c49cb82020-03-16 23:17:32 +0100952 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100953 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
954 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
955 };
956 cmd_pi_coding.cmd_pi_code = full_shift & 0x3f;
957 cmd_pi_coding.cmd_logic_delay = !!(full_shift & 0x40);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100958
Angel Pons737f1112020-11-13 14:07:30 +0100959 cmd_pi_coding.ctl_pi_code_d0 = pi_coding_ctrl[0] & 0x3f;
960 cmd_pi_coding.ctl_pi_code_d1 = pi_coding_ctrl[1] & 0x3f;
961 cmd_pi_coding.ctl_logic_delay_d0 = !!(pi_coding_ctrl[0] & 0x40);
962 cmd_pi_coding.ctl_logic_delay_d1 = !!(pi_coding_ctrl[1] & 0x40);
963
964 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100965
Angel Pons7c49cb82020-03-16 23:17:32 +0100966 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100967 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
968 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100969
970 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100971 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100972 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100973 if (shift < 0)
974 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100975
Angel Pons88521882020-01-05 20:21:20 +0100976 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100977
978 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100979 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
980 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100981 }
982
Angel Pons88521882020-01-05 20:21:20 +0100983 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
984 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100985
Angel Pons88521882020-01-05 20:21:20 +0100986 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100987 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100988
Angel Pons88521882020-01-05 20:21:20 +0100989 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100990
991 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100992 int post_timA_min_high = 7, pre_timA_min_high = 7;
993 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100994 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100995 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100996
997 if (shift < 0)
998 shift = 0;
999
1000 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +02001001 post_timA_min_high = MIN(post_timA_min_high,
1002 (ctrl->timings[channel][slotrank].lanes[lane].
1003 timA + shift) >> 6);
1004 pre_timA_min_high = MIN(pre_timA_min_high,
1005 ctrl->timings[channel][slotrank].lanes[lane].
1006 timA >> 6);
1007 post_timA_max_high = MAX(post_timA_max_high,
1008 (ctrl->timings[channel][slotrank].lanes[lane].
1009 timA + shift) >> 6);
1010 pre_timA_max_high = MAX(pre_timA_max_high,
1011 ctrl->timings[channel][slotrank].lanes[lane].
1012 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001013 }
1014
1015 if (pre_timA_max_high - pre_timA_min_high <
1016 post_timA_max_high - post_timA_min_high)
1017 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001018
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001019 else if (pre_timA_max_high - pre_timA_min_high >
1020 post_timA_max_high - post_timA_min_high)
1021 shift_402x = -1;
1022
Felix Helddee167e2019-12-30 17:30:16 +01001023 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001024 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001025 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001026
Angel Pons88521882020-01-05 20:21:20 +01001027 reg_roundtrip_latency |=
1028 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001029 shift_402x) << (8 * slotrank);
1030
1031 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001032 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001033 (((ctrl->timings[channel][slotrank].lanes[lane].
1034 timA + shift) & 0x3f)
1035 |
1036 ((ctrl->timings[channel][slotrank].lanes[lane].
1037 rising + shift) << 8)
1038 |
1039 (((ctrl->timings[channel][slotrank].lanes[lane].
1040 timA + shift -
1041 (post_timA_min_high << 6)) & 0x1c0) << 10)
1042 | ((ctrl->timings[channel][slotrank].lanes[lane].
1043 falling + shift) << 20));
1044
Felix Heldfb19c8a2020-01-14 21:27:59 +01001045 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001046 (((ctrl->timings[channel][slotrank].lanes[lane].
1047 timC + shift) & 0x3f)
1048 |
1049 (((ctrl->timings[channel][slotrank].lanes[lane].
1050 timB + shift) & 0x3f) << 8)
1051 |
1052 (((ctrl->timings[channel][slotrank].lanes[lane].
1053 timB + shift) & 0x1c0) << 9)
1054 |
1055 (((ctrl->timings[channel][slotrank].lanes[lane].
1056 timC + shift) & 0x40) << 13));
1057 }
1058 }
Angel Pons88521882020-01-05 20:21:20 +01001059 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1060 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001061}
1062
Angel Pons88521882020-01-05 20:21:20 +01001063static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001064{
Angel Pons88521882020-01-05 20:21:20 +01001065 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001066
Angel Ponsffd50152020-11-12 11:03:10 +01001067 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001068
Angel Pons7c49cb82020-03-16 23:17:32 +01001069 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001070 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001071
Angel Pons88521882020-01-05 20:21:20 +01001072 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001073}
1074
Angel Pons7c49cb82020-03-16 23:17:32 +01001075static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001076{
1077 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001078
1079 return (MCHBAR32(lane_base[lane] +
1080 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001081}
1082
1083struct run {
1084 int middle;
1085 int end;
1086 int start;
1087 int all;
1088 int length;
1089};
1090
1091static struct run get_longest_zero_run(int *seq, int sz)
1092{
1093 int i, ls;
1094 int bl = 0, bs = 0;
1095 struct run ret;
1096
1097 ls = 0;
1098 for (i = 0; i < 2 * sz; i++)
1099 if (seq[i % sz]) {
1100 if (i - ls > bl) {
1101 bl = i - ls;
1102 bs = ls;
1103 }
1104 ls = i + 1;
1105 }
1106 if (bl == 0) {
1107 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001108 ret.start = 0;
1109 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001110 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001111 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001112 return ret;
1113 }
1114
Angel Pons7c49cb82020-03-16 23:17:32 +01001115 ret.start = bs % sz;
1116 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001117 ret.middle = (bs + (bl - 1) / 2) % sz;
1118 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001119 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001120
1121 return ret;
1122}
1123
Angel Ponsf3053392020-11-13 23:31:12 +01001124static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001125{
1126 int timA;
1127 int statistics[NUM_LANES][128];
1128 int lane;
1129
1130 for (timA = 0; timA < 128; timA++) {
1131 FOR_ALL_LANES {
1132 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1133 }
1134 program_timings(ctrl, channel);
1135
1136 test_timA(ctrl, channel, slotrank);
1137
1138 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001139 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001140 }
1141 }
1142 FOR_ALL_LANES {
1143 struct run rn = get_longest_zero_run(statistics[lane], 128);
1144 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1145 upperA[lane] = rn.end;
1146 if (upperA[lane] < rn.middle)
1147 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001148
Patrick Rudolph368b6152016-11-25 16:36:52 +01001149 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001150 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001151 }
1152}
1153
Angel Ponsf3053392020-11-13 23:31:12 +01001154static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001155{
1156 int timA_delta;
1157 int statistics[NUM_LANES][51];
1158 int lane, i;
1159
1160 memset(statistics, 0, sizeof(statistics));
1161
1162 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001163
1164 FOR_ALL_LANES {
1165 ctrl->timings[channel][slotrank].lanes[lane].timA
1166 = upperA[lane] + timA_delta + 0x40;
1167 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001168 program_timings(ctrl, channel);
1169
1170 for (i = 0; i < 100; i++) {
1171 test_timA(ctrl, channel, slotrank);
1172 FOR_ALL_LANES {
1173 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001174 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001175 }
1176 }
1177 }
1178 FOR_ALL_LANES {
1179 int last_zero, first_all;
1180
1181 for (last_zero = -25; last_zero <= 25; last_zero++)
1182 if (statistics[lane][last_zero + 25])
1183 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001184
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001185 last_zero--;
1186 for (first_all = -25; first_all <= 25; first_all++)
1187 if (statistics[lane][first_all + 25] == 100)
1188 break;
1189
Angel Pons7c49cb82020-03-16 23:17:32 +01001190 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001191
1192 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001193 (last_zero + first_all) / 2 + upperA[lane];
1194
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001195 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001196 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001197 }
1198}
1199
Angel Ponsf3053392020-11-13 23:31:12 +01001200static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001201{
1202 int works[NUM_LANES];
1203 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001204
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001205 while (1) {
1206 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001207
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001208 program_timings(ctrl, channel);
1209 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001211 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001212 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1213
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 if (works[lane])
1215 some_works = 1;
1216 else
1217 all_works = 0;
1218 }
1219 if (all_works)
1220 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001221
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001222 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001223 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1225 channel, slotrank);
1226 return MAKE_ERR;
1227 }
Angel Pons88521882020-01-05 20:21:20 +01001228 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 printram("4024 -= 2;\n");
1230 continue;
1231 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001232 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001233 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001234
Felix Heldef4fe3e2019-12-31 14:15:05 +01001235 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001236 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1237 channel, slotrank);
1238 return MAKE_ERR;
1239 }
1240 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001241 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001243 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001244 }
1245 }
1246 return 0;
1247}
1248
Angel Pons12bd8ab2020-11-13 23:10:52 +01001249static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001250{
1251 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001252 u16 logic_delay_min = 7;
1253 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001254
1255 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001256 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1257
1258 logic_delay_min = MIN(logic_delay_min, logic_delay);
1259 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001260 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001261
1262 if (logic_delay_max < logic_delay_min) {
1263 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1264 logic_delay_max, logic_delay_min, channel, slotrank);
1265 }
1266
1267 assert(logic_delay_max >= logic_delay_min);
1268
1269 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270}
1271
Angel Pons12bd8ab2020-11-13 23:10:52 +01001272static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001273{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001274 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001275
Angel Pons7c49cb82020-03-16 23:17:32 +01001276 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001277 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001278
Angel Pons12bd8ab2020-11-13 23:10:52 +01001279 if (prev < post)
1280 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001281
Angel Pons12bd8ab2020-11-13 23:10:52 +01001282 else if (prev > post)
1283 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001284
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001285 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001286 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001287
Angel Pons12bd8ab2020-11-13 23:10:52 +01001288 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1289 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1290 printram("4024 += %d;\n", latency_offset);
1291 printram("4028 += %d;\n", latency_offset);
1292
1293 return post;
1294}
1295
1296static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1297{
1298 u16 logic_delay_min = 7;
1299 int lane;
1300
1301 FOR_ALL_LANES {
1302 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1303
1304 logic_delay_min = MIN(logic_delay_min, logic_delay);
1305 }
1306
1307 if (logic_delay_min >= 2) {
1308 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1309 logic_delay_min, channel, slotrank);
1310 }
1311
1312 FOR_ALL_LANES {
1313 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1314 }
1315 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1316 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001317}
1318
Angel Pons7f5a97c2020-11-13 16:58:46 +01001319int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001320{
1321 int channel, slotrank, lane;
1322 int err;
1323
1324 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1325 int all_high, some_high;
1326 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001327 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001328
Angel Pons88521882020-01-05 20:21:20 +01001329 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001330
Angel Ponsffd50152020-11-12 11:03:10 +01001331 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001332
Angel Pons7c49cb82020-03-16 23:17:32 +01001333 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001334 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001335
Angel Pons58b609b2020-11-13 14:35:29 +01001336 const union gdcr_training_mod_reg training_mod = {
1337 .receive_enable_mode = 1,
1338 .training_rank_sel = slotrank,
1339 .odt_always_on = 1,
1340 };
1341 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001342
Felix Heldef4fe3e2019-12-31 14:15:05 +01001343 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001344 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001345 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001346
Angel Ponsf3053392020-11-13 23:31:12 +01001347 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001348
Felix Held2bb3cdf2018-07-28 00:23:59 +02001349 all_high = 1;
1350 some_high = 0;
1351 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001352 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001353 some_high = 1;
1354 else
1355 all_high = 0;
1356 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001357
1358 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001359 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001360 printram("4028--;\n");
1361 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001362 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001363 upperA[lane] -= 0x40;
1364
1365 }
1366 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001367 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001368 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001369 printram("4024++;\n");
1370 printram("4028++;\n");
1371 }
1372
1373 program_timings(ctrl, channel);
1374
Angel Pons12bd8ab2020-11-13 23:10:52 +01001375 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001376
Angel Ponsf3053392020-11-13 23:31:12 +01001377 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001378 if (err)
1379 return err;
1380
Angel Pons12bd8ab2020-11-13 23:10:52 +01001381 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
Angel Ponsf3053392020-11-13 23:31:12 +01001383 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Pons12bd8ab2020-11-13 23:10:52 +01001385 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386
Angel Pons12bd8ab2020-11-13 23:10:52 +01001387 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388
Angel Pons12bd8ab2020-11-13 23:10:52 +01001389 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390
1391 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001392 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001393 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394
1395 printram("final results:\n");
1396 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001397 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001398 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001399
Angel Pons88521882020-01-05 20:21:20 +01001400 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001401
1402 toggle_io_reset();
1403 }
1404
1405 FOR_ALL_POPULATED_CHANNELS {
1406 program_timings(ctrl, channel);
1407 }
Angel Ponsc6742232020-11-15 13:26:21 +01001408
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001409 return 0;
1410}
1411
Angel Pons011661c2020-11-15 18:21:35 +01001412static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001413{
1414 int lane;
1415
1416 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001417 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1418 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001419 }
1420
Angel Pons88521882020-01-05 20:21:20 +01001421 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001422
Angel Ponsffd50152020-11-12 11:03:10 +01001423 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1424 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001425
Angel Pons7c49cb82020-03-16 23:17:32 +01001426 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001427 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001428
Angel Pons88521882020-01-05 20:21:20 +01001429 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001430
Angel Pons801a5cb2020-11-15 15:48:29 +01001431 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001432
Angel Pons7c49cb82020-03-16 23:17:32 +01001433 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001434 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001435
Angel Pons88521882020-01-05 20:21:20 +01001436 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001437}
1438
Angel Pons011661c2020-11-15 18:21:35 +01001439static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001440{
1441 int min = data[0];
1442 int max = min;
1443 int i;
1444 for (i = 1; i < count; i++) {
1445 if (min > data[i])
1446 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001447
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001448 if (max < data[i])
1449 max = data[i];
1450 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001451 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001452 for (i = 0; i < count; i++)
1453 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001454
Angel Pons891f2bc2020-01-10 01:27:28 +01001455 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001456}
1457
Angel Pons011661c2020-11-15 18:21:35 +01001458static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001459{
Angel Pons011661c2020-11-15 18:21:35 +01001460 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001461 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001462 int lane;
1463
Angel Pons88521882020-01-05 20:21:20 +01001464 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001465
Angel Ponsffd50152020-11-12 11:03:10 +01001466 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001467
Angel Pons7c49cb82020-03-16 23:17:32 +01001468 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001469 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001470
Angel Pons011661c2020-11-15 18:21:35 +01001471 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1472 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001473 program_timings(ctrl, channel);
1474
Angel Pons011661c2020-11-15 18:21:35 +01001475 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001476
1477 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001478 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001479 }
1480 }
1481 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001482 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1483
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001484 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001485 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1486 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001487 /*
1488 * With command training not being done yet, the lane can be erroneous.
1489 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001490 */
Angel Pons011661c2020-11-15 18:21:35 +01001491 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001492 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1493
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001494 if (rn.all || rn.length < 8) {
1495 printk(BIOS_EMERG, "timC recovery failed\n");
1496 return MAKE_ERR;
1497 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001498 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001499 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001500 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001501 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001502 }
1503 return 0;
1504}
1505
Angel Pons88521882020-01-05 20:21:20 +01001506static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001507{
1508 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001509
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001510 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1511 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001512
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001513 return ret;
1514}
1515
Angel Pons765d4652020-11-11 14:44:35 +01001516/* Each cacheline is 64 bits long */
1517static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1518{
1519 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1520}
1521
Angel Pons88521882020-01-05 20:21:20 +01001522static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001523{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301524 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001525 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001526
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001527 for (j = 0; j < 16; j++)
1528 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001529
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001530 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001531
1532 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001533}
1534
Angel Pons88521882020-01-05 20:21:20 +01001535static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001536{
1537 int ret = 0;
1538 int channel;
1539 FOR_ALL_POPULATED_CHANNELS ret++;
1540 return ret;
1541}
1542
Angel Pons88521882020-01-05 20:21:20 +01001543static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001544{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301545 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001546 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301547 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001548
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001549 for (j = 0; j < 16; j++)
1550 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001551
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001552 for (j = 0; j < 16; j++)
1553 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001554
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001555 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001556
1557 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001558}
1559
Angel Pons820bce72020-11-14 17:02:55 +01001560static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001561{
1562 int timB;
1563 int statistics[NUM_LANES][128];
1564 int lane;
1565
Angel Pons58b609b2020-11-13 14:35:29 +01001566 const union gdcr_training_mod_reg training_mod = {
1567 .write_leveling_mode = 1,
1568 .training_rank_sel = slotrank,
1569 .enable_dqs_wl = 5,
1570 .odt_always_on = 1,
1571 .force_drive_enable = 1,
1572 };
1573 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001574
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001575 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1576 int bank = 1;
1577
1578 if (ctrl->rank_mirror[channel][slotrank])
1579 ddr3_mirror_mrreg(&bank, &mr1reg);
1580
1581 wait_for_iosav(channel);
1582
1583 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1584
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001585 for (timB = 0; timB < 128; timB++) {
1586 FOR_ALL_LANES {
1587 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1588 }
1589 program_timings(ctrl, channel);
1590
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001591 /* Execute command queue */
1592 iosav_run_once(channel);
1593
1594 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001595
1596 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001597 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1598 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1599 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001600 }
1601 }
1602 FOR_ALL_LANES {
1603 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001604 /*
1605 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1606 * of timB by a small value, which might cause the 6-bit value to overflow if
1607 * it's close to 0x3f. Increment the value by a small offset if it's likely
1608 * to overflow, to make sure it won't overflow while running tests and bricks
1609 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001610 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001611 * TODO: find out why some tests (edge write discovery) increment timB.
1612 */
1613 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001614 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001615 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001616 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001617
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001618 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1619 if (rn.all) {
1620 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1621 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001622
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001623 return MAKE_ERR;
1624 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001625 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1626 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001627 }
1628 return 0;
1629}
1630
Angel Pons820bce72020-11-14 17:02:55 +01001631static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001632{
1633 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001634 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001635 if (val == 0xffffffffffffffffLL)
1636 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001637 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001638 /* DQS is late, needs negative 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 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001643 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001644 for (i = 0; i < 8; i++)
1645 if (val >> (8 * (7 - i) + 4))
1646 return i;
1647 }
1648 return 8;
1649}
1650
Angel Ponsbf13ef02020-11-11 18:40:06 +01001651static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001652{
1653 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001654
1655 const union gdcr_training_mod_reg training_mod = {
1656 .dq_dqs_training_res = 1,
1657 };
1658 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1659
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001660 FOR_ALL_POPULATED_CHANNELS {
1661 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001662 }
1663 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1664
Angel Pons765d4652020-11-11 14:44:35 +01001665 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001666 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001667
Angel Pons88521882020-01-05 20:21:20 +01001668 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001669
Angel Ponsffd50152020-11-12 11:03:10 +01001670 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001671
Angel Pons7c49cb82020-03-16 23:17:32 +01001672 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001673 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001674
Angel Pons88521882020-01-05 20:21:20 +01001675 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001676
Angel Pons8f0757e2020-11-11 23:03:36 +01001677 const struct iosav_ssq rd_sequence[] = {
1678 /* DRAM command PREA */
1679 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001680 .sp_cmd_ctrl = {
1681 .command = IOSAV_PRE,
1682 .ranksel_ap = 1,
1683 },
1684 .subseq_ctrl = {
1685 .cmd_executions = 1,
1686 .cmd_delay_gap = 3,
1687 .post_ssq_wait = ctrl->tRP,
1688 .data_direction = SSQ_NA,
1689 },
1690 .sp_cmd_addr = {
1691 .address = 1024,
1692 .rowbits = 6,
1693 .bank = 0,
1694 .rank = slotrank,
1695 },
1696 .addr_update = {
1697 .addr_wrap = 18,
1698 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001699 },
1700 /* DRAM command ACT */
1701 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001702 .sp_cmd_ctrl = {
1703 .command = IOSAV_ACT,
1704 .ranksel_ap = 1,
1705 },
1706 .subseq_ctrl = {
1707 .cmd_executions = 1,
1708 .cmd_delay_gap = 3,
1709 .post_ssq_wait = ctrl->tRCD,
1710 .data_direction = SSQ_NA,
1711 },
1712 .sp_cmd_addr = {
1713 .address = 0,
1714 .rowbits = 6,
1715 .bank = 0,
1716 .rank = slotrank,
1717 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001718 },
1719 /* DRAM command RD */
1720 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001721 .sp_cmd_ctrl = {
1722 .command = IOSAV_RD,
1723 .ranksel_ap = 3,
1724 },
1725 .subseq_ctrl = {
1726 .cmd_executions = 1,
1727 .cmd_delay_gap = 3,
1728 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001729 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001730 ctrl->timings[channel][slotrank].io_latency,
1731 .data_direction = SSQ_RD,
1732 },
1733 .sp_cmd_addr = {
1734 .address = 8,
1735 .rowbits = 6,
1736 .bank = 0,
1737 .rank = slotrank,
1738 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001739 },
1740 };
1741 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001742
Angel Pons7c49cb82020-03-16 23:17:32 +01001743 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001744 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001745
Angel Pons88521882020-01-05 20:21:20 +01001746 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001747 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001748 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001749 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001750 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001751
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001752 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1753 ctrl->timings[channel][slotrank].lanes[lane].timB +=
Angel Pons820bce72020-11-14 17:02:55 +01001754 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001755
1756 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001757 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1758 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001759 }
1760 }
Angel Pons88521882020-01-05 20:21:20 +01001761 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001762}
1763
Angel Pons7d115132020-11-14 01:44:44 +01001764static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001765{
Angel Pons7d115132020-11-14 01:44:44 +01001766 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001767
Angel Pons7d115132020-11-14 01:44:44 +01001768 FOR_ALL_POPULATED_CHANNELS {
1769 /* choose an existing rank */
1770 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001771
Angel Pons7d115132020-11-14 01:44:44 +01001772 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001773
Angel Pons7d115132020-11-14 01:44:44 +01001774 /* Execute command queue */
1775 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001776
Angel Pons7d115132020-11-14 01:44:44 +01001777 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001778
Angel Pons7d115132020-11-14 01:44:44 +01001779 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1780 }
1781
1782 /* Refresh disable */
1783 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1784
1785 FOR_ALL_POPULATED_CHANNELS {
1786 /* Execute the same command queue */
1787 iosav_run_once(channel);
1788
1789 wait_for_iosav(channel);
1790 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001791}
1792
Angel Pons7c49cb82020-03-16 23:17:32 +01001793/*
1794 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001795 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001796 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1797 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1798 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1799 * CLK/ADDR/CMD signals have the same routing delay.
1800 *
1801 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1802 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1803 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001804 */
Angel Pons820bce72020-11-14 17:02:55 +01001805static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001806{
Angel Pons820bce72020-11-14 17:02:55 +01001807 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001808
Angel Pons7d115132020-11-14 01:44:44 +01001809 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001810
Angel Pons7c49cb82020-03-16 23:17:32 +01001811 /* Enable write leveling on all ranks
1812 Disable all DQ outputs
1813 Only NOP is allowed in this mode */
1814 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1815 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001816 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001817
Angel Ponsa1f17142020-11-15 12:50:03 +01001818 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001819 const union gdcr_training_mod_reg training_mod = {
1820 .write_leveling_mode = 1,
1821 .enable_dqs_wl = 5,
1822 .odt_always_on = 1,
1823 .force_drive_enable = 1,
1824 };
1825 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826
1827 toggle_io_reset();
1828
Angel Pons7c49cb82020-03-16 23:17:32 +01001829 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001830 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001831 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001832 if (err)
1833 return err;
1834 }
1835
Angel Pons7c49cb82020-03-16 23:17:32 +01001836 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001837 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001838 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001839
Angel Pons88521882020-01-05 20:21:20 +01001840 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001841
1842 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001843 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001844
Angel Pons7c49cb82020-03-16 23:17:32 +01001845 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001846 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001847
1848 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001849 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001850 MCHBAR32(IOSAV_STATUS_ch(channel));
1851 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001852
Angel Ponsffd50152020-11-12 11:03:10 +01001853 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001854
Angel Pons7c49cb82020-03-16 23:17:32 +01001855 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001856 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001857
Angel Pons88521882020-01-05 20:21:20 +01001858 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001859 }
1860
1861 toggle_io_reset();
1862
Angel Pons820bce72020-11-14 17:02:55 +01001863 return 0;
1864}
1865
1866int write_training(ramctr_timing *ctrl)
1867{
Angel Ponsc6742232020-11-15 13:26:21 +01001868 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001869 int err;
1870
1871 FOR_ALL_POPULATED_CHANNELS
1872 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1873
Angel Pons4c76d252020-11-15 13:06:53 +01001874 printram("CPE\n");
1875
Angel Pons820bce72020-11-14 17:02:55 +01001876 err = jedec_write_leveling(ctrl);
1877 if (err)
1878 return err;
1879
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001880 printram("CPF\n");
1881
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001882 FOR_ALL_POPULATED_CHANNELS {
1883 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001884 }
1885
1886 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001887 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001888 if (err)
1889 return err;
1890 }
1891
1892 FOR_ALL_POPULATED_CHANNELS
1893 program_timings(ctrl, channel);
1894
1895 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001896 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001897
1898 FOR_ALL_POPULATED_CHANNELS
1899 program_timings(ctrl, channel);
1900
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001901 return 0;
1902}
1903
Angel Ponsbf13ef02020-11-11 18:40:06 +01001904static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001905{
1906 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
1907 int timC_delta;
1908 int lanes_ok = 0;
1909 int ctr = 0;
1910 int lane;
1911
1912 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
1913 FOR_ALL_LANES {
1914 ctrl->timings[channel][slotrank].lanes[lane].timC =
1915 saved_rt.lanes[lane].timC + timC_delta;
1916 }
1917 program_timings(ctrl, channel);
1918 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001919 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001920 }
1921
Angel Pons765d4652020-11-11 14:44:35 +01001922 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001923 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001924
Angel Pons88521882020-01-05 20:21:20 +01001925 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001926
Angel Ponsffd50152020-11-12 11:03:10 +01001927 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001928
1929 /* Program LFSR for the RD/WR subsequences */
1930 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1931 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001932
Angel Pons7c49cb82020-03-16 23:17:32 +01001933 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001934 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001935
Angel Pons88521882020-01-05 20:21:20 +01001936 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001937 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001938 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001939
1940 if (r32 == 0)
1941 lanes_ok |= 1 << lane;
1942 }
1943 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001944 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001945 break;
1946 }
1947
1948 ctrl->timings[channel][slotrank] = saved_rt;
1949
Patrick Rudolphdd662872017-10-28 18:20:11 +02001950 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001951}
1952
Angel Pons88521882020-01-05 20:21:20 +01001953static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001954{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301955 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01001956 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
1957 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001958
1959 if (patno) {
1960 u8 base8 = 0x80 >> ((patno - 1) % 8);
1961 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1962 for (i = 0; i < 32; i++) {
1963 for (j = 0; j < 16; j++) {
1964 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001965
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001966 if (invert[patno - 1][i] & (1 << (j / 2)))
1967 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001968
1969 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970 }
1971 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001973 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1974 for (j = 0; j < 16; j++) {
1975 const u32 val = pattern[i][j];
1976 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1977 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001978 }
1979 sfence();
1980 }
Angel Pons765d4652020-11-11 14:44:35 +01001981
1982 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001983}
1984
Angel Pons88521882020-01-05 20:21:20 +01001985static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001986{
Angel Pons7d115132020-11-14 01:44:44 +01001987 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001988
Angel Pons7c49cb82020-03-16 23:17:32 +01001989 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001990 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001991
1992 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001993 dram_mrscommands(ctrl);
1994
1995 toggle_io_reset();
1996}
1997
Angel Ponsbf13ef02020-11-11 18:40:06 +01001998#define CT_MIN_PI -127
1999#define CT_MAX_PI 128
2000#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2001
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002002#define MIN_C320C_LEN 13
2003
2004static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2005{
2006 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2007 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002008 int command_pi;
2009 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002010 int delta = 0;
2011
2012 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2013
2014 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002015 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002016 }
2017
2018 ctrl->cmd_stretch[channel] = cmd_stretch;
2019
Angel Pons7a612742020-11-12 13:34:03 +01002020 const union tc_rap_reg tc_rap = {
2021 .tRRD = ctrl->tRRD,
2022 .tRTP = ctrl->tRTP,
2023 .tCKE = ctrl->tCKE,
2024 .tWTR = ctrl->tWTR,
2025 .tFAW = ctrl->tFAW,
2026 .tWR = ctrl->tWR,
2027 .tCMD = ctrl->cmd_stretch[channel],
2028 };
2029 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002030
2031 if (ctrl->cmd_stretch[channel] == 2)
2032 delta = 2;
2033 else if (ctrl->cmd_stretch[channel] == 0)
2034 delta = 4;
2035
2036 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002037 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002038 }
2039
Angel Ponsbf13ef02020-11-11 18:40:06 +01002040 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002041 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002042 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002043 }
2044 program_timings(ctrl, channel);
2045 reprogram_320c(ctrl);
2046 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002047 stat[slotrank][command_pi - CT_MIN_PI] =
2048 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002049 }
2050 }
2051 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002052 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002053
Angel Ponsbf13ef02020-11-11 18:40:06 +01002054 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002055 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2056 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002057
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002058 if (rn.all || rn.length < MIN_C320C_LEN) {
2059 FOR_ALL_POPULATED_RANKS {
2060 ctrl->timings[channel][slotrank] =
2061 saved_timings[channel][slotrank];
2062 }
2063 return MAKE_ERR;
2064 }
2065 }
2066
2067 return 0;
2068}
2069
Angel Pons7c49cb82020-03-16 23:17:32 +01002070/*
2071 * Adjust CMD phase shift and try multiple command rates.
2072 * A command rate of 2T doubles the time needed for address and command decode.
2073 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002074int command_training(ramctr_timing *ctrl)
2075{
2076 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002077
2078 FOR_ALL_POPULATED_CHANNELS {
2079 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002080 }
2081
2082 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002083 int cmdrate, err;
2084
2085 /*
2086 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002087 * Issue:
2088 * While c320c discovery seems to succeed raminit will fail in write training.
2089 *
2090 * Workaround:
2091 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2092 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002093 *
2094 * Single DIMM per channel:
2095 * Try command rate 1T and 2T
2096 */
2097 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002098 if (ctrl->tCMD)
2099 /* XMP gives the CMD rate in clock ticks, not ns */
2100 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002101
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002102 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002103 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2104
2105 if (!err)
2106 break;
2107 }
2108
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002109 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002110 printk(BIOS_EMERG, "c320c discovery failed\n");
2111 return err;
2112 }
2113
Angel Pons891f2bc2020-01-10 01:27:28 +01002114 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002115 }
2116
2117 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002118 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002119
2120 reprogram_320c(ctrl);
2121 return 0;
2122}
2123
Angel Pons4c79f932020-11-14 01:26:52 +01002124static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002125{
Angel Pons96a06dd2020-11-14 00:33:18 +01002126 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002127 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002128 int lane;
2129
Angel Pons96a06dd2020-11-14 00:33:18 +01002130 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002131 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002132 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2133 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002134 }
2135 program_timings(ctrl, channel);
2136
2137 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002138 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2139 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002140 }
2141
Angel Pons88521882020-01-05 20:21:20 +01002142 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002143
Angel Ponsffd50152020-11-12 11:03:10 +01002144 iosav_write_read_mpr_sequence(
2145 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146
Angel Pons7c49cb82020-03-16 23:17:32 +01002147 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002148 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149
Angel Pons88521882020-01-05 20:21:20 +01002150 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002151
2152 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002153 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002154 }
2155 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002156
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002157 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002158 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002160
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002161 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002162 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2163 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002164 return MAKE_ERR;
2165 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002166 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002167 }
2168 return 0;
2169}
2170
Angel Pons60971dc2020-11-14 00:49:38 +01002171static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2172{
2173 int slotrank, lane;
2174
2175 fill_pattern0(ctrl, channel, 0, 0);
2176 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002177 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002178 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2179 }
2180
2181 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2182 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2183 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2184 }
2185
2186 program_timings(ctrl, channel);
2187
2188 FOR_ALL_POPULATED_RANKS {
2189 wait_for_iosav(channel);
2190
2191 iosav_write_read_mpr_sequence(
2192 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2193
2194 /* Execute command queue */
2195 iosav_run_once(channel);
2196
2197 wait_for_iosav(channel);
2198 }
2199
2200 /* XXX: check any measured value ? */
2201
2202 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2203 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2204 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2205 }
2206
2207 program_timings(ctrl, channel);
2208
2209 FOR_ALL_POPULATED_RANKS {
2210 wait_for_iosav(channel);
2211
2212 iosav_write_read_mpr_sequence(
2213 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2214
2215 /* Execute command queue */
2216 iosav_run_once(channel);
2217
2218 wait_for_iosav(channel);
2219 }
2220
2221 /* XXX: check any measured value ? */
2222
2223 FOR_ALL_LANES {
2224 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2225 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2226 }
2227}
2228
Angel Pons4c79f932020-11-14 01:26:52 +01002229int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002230{
2231 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2232 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2233 int channel, slotrank, lane;
2234 int err;
2235
Angel Pons88521882020-01-05 20:21:20 +01002236 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002237
2238 toggle_io_reset();
2239
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002240 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002241 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002242
2243 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002244 }
2245
Angel Pons0c3936e2020-03-22 12:49:27 +01002246 /*
2247 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2248 * also use a single loop. It would seem that it is a debugging configuration.
2249 */
Angel Pons88521882020-01-05 20:21:20 +01002250 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2251 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002252
2253 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002254 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002255 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002256 if (err)
2257 return err;
2258 }
2259
Angel Pons88521882020-01-05 20:21:20 +01002260 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2261 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002262
2263 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002264 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002265 rising_edges[channel][slotrank]);
2266 if (err)
2267 return err;
2268 }
2269
Angel Pons88521882020-01-05 20:21:20 +01002270 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002271
2272 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2273 ctrl->timings[channel][slotrank].lanes[lane].falling =
2274 falling_edges[channel][slotrank][lane];
2275 ctrl->timings[channel][slotrank].lanes[lane].rising =
2276 rising_edges[channel][slotrank][lane];
2277 }
2278
2279 FOR_ALL_POPULATED_CHANNELS {
2280 program_timings(ctrl, channel);
2281 }
2282
Angel Pons50a6fe72020-11-14 01:18:14 +01002283 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002284 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002285 }
2286 return 0;
2287}
2288
Angel Pons7c49cb82020-03-16 23:17:32 +01002289static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002290{
2291 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002292 u32 raw_stats[MAX_EDGE_TIMING + 1];
2293 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002294 const int reg3000b24[] = { 0, 0xc, 0x2c };
2295 int lane, i;
2296 int lower[NUM_LANES];
2297 int upper[NUM_LANES];
2298 int pat;
2299
2300 FOR_ALL_LANES {
2301 lower[lane] = 0;
2302 upper[lane] = MAX_EDGE_TIMING;
2303 }
2304
2305 for (i = 0; i < 3; i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002306 const union gdcr_training_mod_reg training_mod = {
2307 .vref_gen_ctl = reg3000b24[i],
2308 };
2309 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2310 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002311
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002312 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2313 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002314 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002315
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002316 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2317 FOR_ALL_LANES {
2318 ctrl->timings[channel][slotrank].lanes[lane].
2319 rising = edge;
2320 ctrl->timings[channel][slotrank].lanes[lane].
2321 falling = edge;
2322 }
2323 program_timings(ctrl, channel);
2324
2325 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002326 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2327 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002328 }
Angel Pons88521882020-01-05 20:21:20 +01002329 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330
Angel Ponsffd50152020-11-12 11:03:10 +01002331 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002332
Angel Pons7c49cb82020-03-16 23:17:32 +01002333 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002334 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002335
Angel Pons88521882020-01-05 20:21:20 +01002336 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002337 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002338 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002339 }
2340
Angel Pons7c49cb82020-03-16 23:17:32 +01002341 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002342 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002343 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002344
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002345 FOR_ALL_LANES {
2346 struct run rn;
2347 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002348 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2349
2350 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2351
2352 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2353 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2354 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002355 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002356
2357 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2358 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2359
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002360 edges[lane] = (lower[lane] + upper[lane]) / 2;
2361 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002362 printk(BIOS_EMERG, "edge write discovery failed: "
2363 "%d, %d, %d\n", channel, slotrank, lane);
2364
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002365 return MAKE_ERR;
2366 }
2367 }
2368 }
2369 }
2370
Angel Ponsa93f46e2020-11-17 16:54:01 +01002371 /* Restore nominal Vref after training */
2372 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002373 printram("CPA\n");
2374 return 0;
2375}
2376
2377int discover_edges_write(ramctr_timing *ctrl)
2378{
2379 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002380 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2381 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002382
Angel Pons7c49cb82020-03-16 23:17:32 +01002383 /*
2384 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2385 * also use a single loop. It would seem that it is a debugging configuration.
2386 */
Angel Pons88521882020-01-05 20:21:20 +01002387 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2388 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002389
2390 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2391 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002392 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002393 if (err)
2394 return err;
2395 }
2396
Angel Pons88521882020-01-05 20:21:20 +01002397 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2398 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002399
2400 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2401 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002402 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002403 if (err)
2404 return err;
2405 }
2406
Angel Pons88521882020-01-05 20:21:20 +01002407 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002408
2409 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2410 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002411 falling_edges[channel][slotrank][lane];
2412
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002413 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002414 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002415 }
2416
2417 FOR_ALL_POPULATED_CHANNELS
2418 program_timings(ctrl, channel);
2419
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002420 return 0;
2421}
2422
2423static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2424{
Angel Pons88521882020-01-05 20:21:20 +01002425 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002426
Angel Ponsffd50152020-11-12 11:03:10 +01002427 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002428
Angel Pons7c49cb82020-03-16 23:17:32 +01002429 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002430 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002431
Angel Pons88521882020-01-05 20:21:20 +01002432 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002433}
2434
2435int discover_timC_write(ramctr_timing *ctrl)
2436{
Angel Pons7c49cb82020-03-16 23:17:32 +01002437 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002438 int i, pat;
2439
2440 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2441 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2442 int channel, slotrank, lane;
2443
2444 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2445 lower[channel][slotrank][lane] = 0;
2446 upper[channel][slotrank][lane] = MAX_TIMC;
2447 }
2448
Angel Pons88521882020-01-05 20:21:20 +01002449 /*
2450 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2451 * FIXME: This must only be done on Ivy Bridge.
2452 */
2453 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002454 printram("discover timC write:\n");
2455
2456 for (i = 0; i < 3; i++)
2457 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002458
2459 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2460 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2461 ~0x3f000000, rege3c_b24[i] << 24);
2462
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002463 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002464
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002465 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2466 FOR_ALL_POPULATED_RANKS {
2467 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002468 u32 raw_stats[MAX_TIMC + 1];
2469 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470
2471 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002472 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002473
2474 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002475
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002476 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002477 FOR_ALL_LANES {
2478 ctrl->timings[channel][slotrank]
2479 .lanes[lane].timC = timC;
2480 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002481 program_timings(ctrl, channel);
2482
2483 test_timC_write (ctrl, channel, slotrank);
2484
Angel Pons7c49cb82020-03-16 23:17:32 +01002485 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002486 raw_stats[timC] = MCHBAR32(
2487 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002488 }
2489 FOR_ALL_LANES {
2490 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002491 for (timC = 0; timC < MAX_TIMC; timC++) {
2492 stats[timC] = !!(raw_stats[timC]
2493 & (1 << lane));
2494 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002495
Angel Pons7c49cb82020-03-16 23:17:32 +01002496 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002497 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002498 printk(BIOS_EMERG,
2499 "timC write discovery failed: "
2500 "%d, %d, %d\n", channel,
2501 slotrank, lane);
2502
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002503 return MAKE_ERR;
2504 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002505 printram("timC: %d, %d, %d: "
2506 "0x%02x-0x%02x-0x%02x, "
2507 "0x%02x-0x%02x\n", channel, slotrank,
2508 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002509 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002510 rn.end - ctrl->timC_offset[i]);
2511
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002512 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002513 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002514 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002515
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002516 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002517 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002518 upper[channel][slotrank][lane]);
2519
2520 }
2521 }
2522 }
2523 }
2524
2525 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002526 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002527 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002528 udelay(2);
2529 }
2530
Angel Pons88521882020-01-05 20:21:20 +01002531 /*
2532 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2533 * FIXME: This must only be done on Ivy Bridge.
2534 */
2535 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002536
2537 printram("CPB\n");
2538
2539 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002540 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002541 (lower[channel][slotrank][lane] +
2542 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002543
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002544 ctrl->timings[channel][slotrank].lanes[lane].timC =
2545 (lower[channel][slotrank][lane] +
2546 upper[channel][slotrank][lane]) / 2;
2547 }
2548 FOR_ALL_POPULATED_CHANNELS {
2549 program_timings(ctrl, channel);
2550 }
2551 return 0;
2552}
2553
Angel Pons88521882020-01-05 20:21:20 +01002554void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002555{
2556 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002557 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002558
2559 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2560 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002561 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002562 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002563 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002564 printram("normalize %d, %d, %d: mat %d\n",
2565 channel, slotrank, lane, mat);
2566
Felix Heldef4fe3e2019-12-31 14:15:05 +01002567 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002568 printram("normalize %d, %d, %d: delta %d\n",
2569 channel, slotrank, lane, delta);
2570
Angel Pons88521882020-01-05 20:21:20 +01002571 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002572 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002573 }
2574
2575 FOR_ALL_POPULATED_CHANNELS {
2576 program_timings(ctrl, channel);
2577 }
2578}
2579
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002580int channel_test(ramctr_timing *ctrl)
2581{
2582 int channel, slotrank, lane;
2583
2584 slotrank = 0;
2585 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002586 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002587 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002588 return MAKE_ERR;
2589 }
2590 FOR_ALL_POPULATED_CHANNELS {
2591 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002592 }
2593
2594 for (slotrank = 0; slotrank < 4; slotrank++)
2595 FOR_ALL_CHANNELS
2596 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2597 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002598 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2599 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002600 }
Angel Pons88521882020-01-05 20:21:20 +01002601 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002602
Angel Ponsffd50152020-11-12 11:03:10 +01002603 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002604
Angel Pons7c49cb82020-03-16 23:17:32 +01002605 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002606 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002607
Angel Pons88521882020-01-05 20:21:20 +01002608 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002609 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002610 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002611 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2612 channel, slotrank, lane);
2613 return MAKE_ERR;
2614 }
2615 }
2616 return 0;
2617}
2618
Patrick Rudolphdd662872017-10-28 18:20:11 +02002619void channel_scrub(ramctr_timing *ctrl)
2620{
2621 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002622 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002623
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002624 FOR_ALL_POPULATED_CHANNELS {
2625 wait_for_iosav(channel);
2626 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002627 }
2628
2629 /*
2630 * During runtime the "scrubber" will periodically scan through the memory in the
2631 * physical address space, to identify and fix CRC errors.
2632 * The following loops writes to every DRAM address, setting the ECC bits to the
2633 * correct value. A read from this location will no longer return a CRC error,
2634 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002635 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002636 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2637 * and firmware running in x86_32.
2638 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002639 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2640 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002641 for (bank = 0; bank < 8; bank++) {
2642 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002643
Angel Pons8f0757e2020-11-11 23:03:36 +01002644 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2645 const struct iosav_ssq sequence[] = {
2646 /*
2647 * DRAM command ACT
2648 * Opens the row for writing.
2649 */
2650 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002651 .sp_cmd_ctrl = {
2652 .command = IOSAV_ACT,
2653 .ranksel_ap = 1,
2654 },
2655 .subseq_ctrl = {
2656 .cmd_executions = 1,
2657 .cmd_delay_gap = gap,
2658 .post_ssq_wait = ctrl->tRCD,
2659 .data_direction = SSQ_NA,
2660 },
2661 .sp_cmd_addr = {
2662 .address = row,
2663 .rowbits = 6,
2664 .bank = bank,
2665 .rank = slotrank,
2666 },
2667 .addr_update = {
2668 .inc_addr_1 = 1,
2669 .addr_wrap = 18,
2670 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002671 },
2672 /*
2673 * DRAM command WR
2674 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2675 * bytes.
2676 */
2677 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002678 .sp_cmd_ctrl = {
2679 .command = IOSAV_WR,
2680 .ranksel_ap = 1,
2681 },
2682 .subseq_ctrl = {
2683 .cmd_executions = 129,
2684 .cmd_delay_gap = 4,
2685 .post_ssq_wait = ctrl->tWTR +
2686 ctrl->CWL + 8,
2687 .data_direction = SSQ_WR,
2688 },
2689 .sp_cmd_addr = {
2690 .address = row,
2691 .rowbits = 0,
2692 .bank = bank,
2693 .rank = slotrank,
2694 },
2695 .addr_update = {
2696 .inc_addr_8 = 1,
2697 .addr_wrap = 9,
2698 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002699 },
2700 /*
2701 * DRAM command PRE
2702 * Closes the row.
2703 */
2704 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002705 .sp_cmd_ctrl = {
2706 .command = IOSAV_PRE,
2707 .ranksel_ap = 1,
2708 },
2709 .subseq_ctrl = {
2710 .cmd_executions = 1,
2711 .cmd_delay_gap = 4,
2712 .post_ssq_wait = ctrl->tRP,
2713 .data_direction = SSQ_NA,
2714 },
2715 .sp_cmd_addr = {
2716 .address = 0,
2717 .rowbits = 6,
2718 .bank = bank,
2719 .rank = slotrank,
2720 },
2721 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002722 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002723 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002724 },
2725 };
2726 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002727
2728 /* Execute command queue */
2729 iosav_run_queue(channel, 16, 0);
2730
2731 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002732 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002733 }
2734 }
2735}
2736
Angel Pons88521882020-01-05 20:21:20 +01002737void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002738{
2739 int channel;
2740
Angel Pons7c49cb82020-03-16 23:17:32 +01002741 /* 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 +01002742 static u32 seeds[NUM_CHANNELS][3] = {
2743 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2744 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2745 };
2746 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002747 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002748 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2749 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2750 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002751 }
2752}
2753
Angel Pons89ae6b82020-03-21 13:23:32 +01002754void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002755{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002756 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002757 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002758 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002759 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002760 }
2761}
2762
Angel Pons88521882020-01-05 20:21:20 +01002763void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002764{
2765 int channel;
2766
2767 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002768 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002769 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002770 }
2771
2772 udelay(1);
2773
2774 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002775 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002776 }
2777}
2778
Angel Pons7c49cb82020-03-16 23:17:32 +01002779void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002780{
2781 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002782
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002783 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002784 int min_pi = 10000;
2785 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002786
2787 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002788 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2789 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002790 }
2791
Angel Pons7a612742020-11-12 13:34:03 +01002792 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002793
Angel Pons7a612742020-11-12 13:34:03 +01002794 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002795
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002796 dram_odt_stretch(ctrl, channel);
2797
Angel Pons7a612742020-11-12 13:34:03 +01002798 const union tc_rwp_reg tc_rwp = {
2799 .tRRDR = 0,
2800 .tRRDD = val,
2801 .tWWDR = val,
2802 .tWWDD = val,
2803 .tRWDRDD = ctrl->ref_card_offset[channel] + 2,
2804 .tWRDRDD = tWRDRDD,
2805 .tRWSR = 2,
2806 .dec_wrd = 1,
2807 };
2808 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002809 }
2810}
2811
Angel Pons88521882020-01-05 20:21:20 +01002812void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002813{
2814 int channel;
2815 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002816 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2817 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002818 }
2819}
2820
Angel Pons7c49cb82020-03-16 23:17:32 +01002821/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2822static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002823{
Angel Pons88521882020-01-05 20:21:20 +01002824 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002825}
2826
Angel Pons7c49cb82020-03-16 23:17:32 +01002827/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002828void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002829{
Angel Ponsb50ca572020-11-11 19:07:20 +01002830 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01002831
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002832 int channel;
2833 int t1_cycles = 0, t1_ns = 0, t2_ns;
2834 int t3_ns;
2835 u32 r32;
2836
Angel Pons7c49cb82020-03-16 23:17:32 +01002837 /* FIXME: This register only exists on Ivy Bridge */
2838 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002839
Angel Pons7a612742020-11-12 13:34:03 +01002840 FOR_ALL_CHANNELS {
2841 union tc_othp_reg tc_othp = {
2842 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2843 };
2844 tc_othp.tCPDED = 1;
2845 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2846 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002847
Patrick Rudolph74203de2017-11-20 11:57:01 +01002848 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01002849 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01002850 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002851 else
Angel Pons7c49cb82020-03-16 23:17:32 +01002852 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01002853 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002854
Felix Heldf9b826a2018-07-30 17:56:52 +02002855 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002856 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002857
Angel Pons88521882020-01-05 20:21:20 +01002858 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
2859 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002860
2861 FOR_ALL_CHANNELS {
2862 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002863 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002864 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002865 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002866 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002867 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002868 case 1:
2869 case 4:
2870 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002871 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002872 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002873 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002874 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002875 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002876 break;
2877 }
2878 }
2879
Felix Held50b7ed22019-12-30 20:41:54 +01002880 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002881 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002882 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002883
Angel Pons7a612742020-11-12 13:34:03 +01002884 FOR_ALL_CHANNELS {
2885 union tc_rfp_reg tc_rfp = {
2886 .raw = MCHBAR32(TC_RFP_ch(channel)),
2887 };
2888 tc_rfp.refresh_2x_control = 1;
2889 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2890 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002891
Angel Ponsdc5539f2020-11-12 12:44:25 +01002892 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2893 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002894 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002895
Angel Pons7c49cb82020-03-16 23:17:32 +01002896 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002897 FOR_ALL_POPULATED_CHANNELS
2898 break;
2899
Angel Pons88521882020-01-05 20:21:20 +01002900 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2901 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002902 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002903 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002904 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002905 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002906 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002907 t1_ns += 500;
2908
Angel Pons88521882020-01-05 20:21:20 +01002909 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002910 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002911 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002912 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002913 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002914 t3_ns = 500;
2915 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002916
2917 /* The graphics driver will use these watermark values */
2918 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002919 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002920 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2921 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002922}
2923
Angel Pons88521882020-01-05 20:21:20 +01002924void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002925{
Angel Ponsc6742232020-11-15 13:26:21 +01002926 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002927
Angel Pons7c49cb82020-03-16 23:17:32 +01002928 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002929 const union tc_rap_reg tc_rap = {
2930 .tRRD = ctrl->tRRD,
2931 .tRTP = ctrl->tRTP,
2932 .tCKE = ctrl->tCKE,
2933 .tWTR = ctrl->tWTR,
2934 .tFAW = ctrl->tFAW,
2935 .tWR = ctrl->tWR,
2936 .tCMD = ctrl->cmd_stretch[channel],
2937 };
2938 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002939 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002940
2941 udelay(1);
2942
2943 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002944 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002945 }
2946
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002947 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002948 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002949
2950 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002951 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002952 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002953 }
2954
2955 printram("CPE\n");
2956
Angel Pons88521882020-01-05 20:21:20 +01002957 MCHBAR32(GDCRTRAININGMOD) = 0;
2958 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002959
2960 printram("CP5b\n");
2961
2962 FOR_ALL_POPULATED_CHANNELS {
2963 program_timings(ctrl, channel);
2964 }
2965
2966 u32 reg, addr;
2967
Angel Pons7c49cb82020-03-16 23:17:32 +01002968 /* Poll for RCOMP */
2969 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2970 ;
2971
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972 do {
Angel Pons88521882020-01-05 20:21:20 +01002973 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002974 } while ((reg & 0x14) == 0);
2975
Angel Pons7c49cb82020-03-16 23:17:32 +01002976 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002977 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002978 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002979
Angel Pons7c49cb82020-03-16 23:17:32 +01002980 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002981 udelay(500);
2982
2983 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002984 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002985 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002986 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002987 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002988 MCHBAR32(addr) = reg;
2989
Angel Pons7c49cb82020-03-16 23:17:32 +01002990 /* Wait 10ns for ranks to settle */
2991 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002992
2993 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
2994 MCHBAR32(addr) = reg;
2995
Angel Pons7c49cb82020-03-16 23:17:32 +01002996 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002997 write_reset(ctrl);
2998 }
2999
Angel Pons7c49cb82020-03-16 23:17:32 +01003000 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003001 dram_mrscommands(ctrl);
3002
3003 printram("CP5c\n");
3004
Angel Pons88521882020-01-05 20:21:20 +01003005 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003006
3007 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003008 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003009 udelay(2);
3010 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003011}