blob: d533ca8a309fa1081eb4bab1f0f74ecd8f1f3bc7 [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
Angel Pons12bd8ab2020-11-13 23:10:52 +01003#include <assert.h>
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01004#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <console/console.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01006#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010010#include <northbridge/intel/sandybridge/chip.h>
11#include <device/pci_def.h>
12#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020013#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020014
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010015#include "raminit_native.h"
16#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010017#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#include "sandybridge.h"
19
Angel Pons7c49cb82020-03-16 23:17:32 +010020/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010021
22static void sfence(void)
23{
24 asm volatile ("sfence");
25}
26
Angel Pons7c49cb82020-03-16 23:17:32 +010027/* Toggle IO reset bit */
28static void toggle_io_reset(void)
29{
Angel Pons88521882020-01-05 20:21:20 +010030 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010031 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010033 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010034 udelay(1);
35}
36
37static u32 get_XOVER_CLK(u8 rankmap)
38{
39 return rankmap << 24;
40}
41
42static u32 get_XOVER_CMD(u8 rankmap)
43{
44 u32 reg;
45
Angel Pons7c49cb82020-03-16 23:17:32 +010046 /* Enable xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010047 reg = 0x4000;
48
Angel Pons7c49cb82020-03-16 23:17:32 +010049 /* Enable xover ctl */
50 if (rankmap & 0x03)
51 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010052
Angel Pons7c49cb82020-03-16 23:17:32 +010053 if (rankmap & 0x0c)
54 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010055
56 return reg;
57}
58
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010059void dram_find_common_params(ramctr_timing *ctrl)
60{
61 size_t valid_dimms;
62 int channel, slot;
63 dimm_info *dimms = &ctrl->info;
64
65 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
66 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010067
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010068 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010069
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010070 const dimm_attr *dimm = &dimms->dimm[channel][slot];
71 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
72 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010073
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010074 valid_dimms++;
75
76 /* Find all possible CAS combinations */
77 ctrl->cas_supported &= dimm->cas_supported;
78
79 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010080 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
81 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
82 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
84 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010085 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010086 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
87 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
88 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
89 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
90 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030091 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
92 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010093 }
94
95 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010096 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
97
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010098 if (!valid_dimms)
99 die("No valid DIMMs found");
100}
101
Angel Pons88521882020-01-05 20:21:20 +0100102void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100103{
104 u32 reg;
105 int channel;
106
107 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100108 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100110 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
111 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100112
Angel Pons7c49cb82020-03-16 23:17:32 +0100113 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100115 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
116 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100117 }
118}
119
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100120static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100121{
Angel Pons89ae6b82020-03-21 13:23:32 +0100122 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100123
124 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 /*
126 * ODT stretch:
127 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
128 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100129 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100130 if (stretch == 2)
131 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100132
Angel Pons88521882020-01-05 20:21:20 +0100133 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100134 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100136 } else {
Angel Pons88521882020-01-05 20:21:20 +0100137 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100138 union tc_othp_reg tc_othp = {
139 .raw = MCHBAR32(addr),
140 };
141 tc_othp.odt_delay_d0 = stretch;
142 tc_othp.odt_delay_d1 = stretch;
143 MCHBAR32(addr) = tc_othp.raw;
Iru Cai89af71c2018-08-16 16:46:27 +0800144 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100145 }
146}
147
148void dram_timing_regs(ramctr_timing *ctrl)
149{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100150 int channel;
151
Angel Pons81378062020-11-12 13:46:21 +0100152 /* BIN parameters */
153 const union tc_dbp_reg tc_dbp = {
154 .tRCD = ctrl->tRCD,
155 .tRP = ctrl->tRP,
156 .tAA = ctrl->CAS,
157 .tCWL = ctrl->CWL,
158 .tRAS = ctrl->tRAS,
159 };
160
161 /* Regular access parameters */
162 const union tc_rap_reg tc_rap = {
163 .tRRD = ctrl->tRRD,
164 .tRTP = ctrl->tRTP,
165 .tCKE = ctrl->tCKE,
166 .tWTR = ctrl->tWTR,
167 .tFAW = ctrl->tFAW,
168 .tWR = ctrl->tWR,
169 .tCMD = 3,
170 };
171
172 /* Other parameters */
173 const union tc_othp_reg tc_othp = {
174 .tXPDLL = ctrl->tXPDLL,
175 .tXP = ctrl->tXP,
176 .tAONPD = ctrl->tAONPD,
177 .tCPDED = 2,
Angel Pons2ad03a42020-11-19 11:07:27 +0100178 .tPRPDEN = 1,
Angel Pons81378062020-11-12 13:46:21 +0100179 };
180
181 /*
182 * If tXP and tXPDLL are very high, we need to increase them by one.
183 * This can only happen on Ivy Bridge, and when overclocking the RAM.
184 */
185 const union tc_dtp_reg tc_dtp = {
186 .overclock_tXP = ctrl->tXP >= 8,
187 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
188 };
189
190 /*
191 * TC-Refresh timing parameters:
192 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
193 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
194 */
195 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
196
197 const union tc_rftp_reg tc_rftp = {
198 .tREFI = ctrl->tREFI,
199 .tRFC = ctrl->tRFC,
200 .tREFIx9 = val32 / 1024,
201 };
202
203 /* Self-refresh timing parameters */
204 const union tc_srftp_reg tc_srftp = {
205 .tXSDLL = tDLLK,
206 .tXS_offset = ctrl->tXSOffset,
207 .tZQOPER = tDLLK - ctrl->tXSOffset,
208 .tMOD = ctrl->tMOD - 8,
209 };
210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100211 FOR_ALL_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +0100212 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
213 MCHBAR32(TC_DBP_ch(channel)) = tc_dbp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100214
Angel Pons7a612742020-11-12 13:34:03 +0100215 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
216 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100217
Angel Pons7a612742020-11-12 13:34:03 +0100218 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
219 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100220
Angel Ponsca2f68a2020-03-22 13:15:12 +0100221 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Pons81378062020-11-12 13:46:21 +0100222 /* Debug parameters - only applies to Ivy Bridge */
Angel Pons7a612742020-11-12 13:34:03 +0100223 MCHBAR32(TC_DTP_ch(channel)) = tc_dtp.raw;
Angel Ponsca2f68a2020-03-22 13:15:12 +0100224 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100225
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100226 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100227
Angel Pons7a612742020-11-12 13:34:03 +0100228 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
229 MCHBAR32(TC_RFTP_ch(channel)) = tc_rftp.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +0100230
Angel Pons7a612742020-11-12 13:34:03 +0100231 union tc_rfp_reg tc_rfp = {
232 .raw = MCHBAR32(TC_RFP_ch(channel)),
233 };
234 tc_rfp.oref_ri = 0xff;
235 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100236
Angel Pons7a612742020-11-12 13:34:03 +0100237 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
238 MCHBAR32(TC_SRFTP_ch(channel)) = tc_srftp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100239 }
240}
241
242void dram_dimm_mapping(ramctr_timing *ctrl)
243{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100244 int channel;
245 dimm_info *info = &ctrl->info;
246
247 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200248 dimm_attr *dimmA, *dimmB;
249 u32 reg = 0;
250
Angel Pons7c49cb82020-03-16 23:17:32 +0100251 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100252 dimmA = &info->dimm[channel][0];
253 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100254 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100255 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100256 dimmA = &info->dimm[channel][1];
257 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100258 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100259 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100260
Nico Huberac4f2162017-10-01 18:14:43 +0200261 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100262 reg |= (dimmA->size_mb / 256) << 0;
263 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200264 reg |= (dimmA->width / 8 - 1) << 19;
265 }
266
267 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100268 reg |= (dimmB->size_mb / 256) << 8;
269 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200270 reg |= (dimmB->width / 8 - 1) << 20;
271 }
272
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200273 /*
274 * Rank interleave: Bit 16 of the physical address space sets
275 * the rank to use in a dual single rank DIMM configuration.
276 * That results in every 64KiB being interleaved between two ranks.
277 */
278 reg |= 1 << 21;
279 /* Enhanced interleave */
280 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200281
Angel Pons7c49cb82020-03-16 23:17:32 +0100282 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100283 ctrl->mad_dimm[channel] = reg;
284 } else {
285 ctrl->mad_dimm[channel] = 0;
286 }
287 }
288}
289
Patrick Rudolphdd662872017-10-28 18:20:11 +0200290void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100291{
292 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200293 u32 ecc;
294
295 if (ctrl->ecc_enabled)
296 ecc = training ? (1 << 24) : (3 << 24);
297 else
298 ecc = 0;
299
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100300 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200301 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100302 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200303
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200304 if (ctrl->ecc_enabled)
305 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100306}
307
Angel Pons88521882020-01-05 20:21:20 +0100308void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100309{
310 u32 reg, ch0size, ch1size;
311 u8 val;
312 reg = 0;
313 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100314
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100315 if (training) {
316 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
317 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
318 } else {
319 ch0size = ctrl->channel_size_mb[0];
320 ch1size = ctrl->channel_size_mb[1];
321 }
322
323 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100324 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100325 val = ch1size / 256;
326 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100327 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100328 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100329 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100330
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100331 } else {
Angel Pons88521882020-01-05 20:21:20 +0100332 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100333 val = ch0size / 256;
334 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100335 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100336 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100337 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100338 }
339}
340
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100341#define DEFAULT_PCI_MMIO_SIZE 2048
342
343static unsigned int get_mmio_size(void)
344{
345 const struct device *dev;
346 const struct northbridge_intel_sandybridge_config *cfg = NULL;
347
Angel Ponsb31d1d72020-01-10 01:35:09 +0100348 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100349 if (dev)
350 cfg = dev->chip_info;
351
352 /* If this is zero, it just means devicetree.cb didn't set it */
353 if (!cfg || cfg->pci_mmio_size == 0)
354 return DEFAULT_PCI_MMIO_SIZE;
355 else
356 return cfg->pci_mmio_size;
357}
358
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200359/*
360 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
361 * The ME/PCU/.. has the ability to change this.
362 * Return 0: ECC is optional
363 * Return 1: ECC is forced
364 */
365bool get_host_ecc_forced(void)
366{
367 /* read Capabilities A Register */
368 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
369 return !!(reg32 & (1 << 24));
370}
371
372/*
373 * Returns the ECC capability.
374 * The ME/PCU/.. has the ability to change this.
375 * Return 0: ECC is disabled
376 * Return 1: ECC is possible
377 */
378bool get_host_ecc_cap(void)
379{
380 /* read Capabilities A Register */
381 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
382 return !(reg32 & (1 << 25));
383}
384
Angel Pons88521882020-01-05 20:21:20 +0100385void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100386{
Angel Pons7c49cb82020-03-16 23:17:32 +0100387 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
388 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
389 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100390 uint16_t ggc;
391
392 mmiosize = get_mmio_size();
393
Felix Held87ddea22020-01-26 04:55:27 +0100394 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100395 if (!(ggc & 2)) {
396 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100397 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100398 } else {
399 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100400 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100401 }
402
403 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
404
405 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
406
407 mestolenbase = tom - me_uma_size;
408
Angel Pons7c49cb82020-03-16 23:17:32 +0100409 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
410
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100411 gfxstolenbase = toludbase - gfxstolen;
412 gttbase = gfxstolenbase - gttsize;
413
414 tsegbase = gttbase - tsegsize;
415
Angel Pons7c49cb82020-03-16 23:17:32 +0100416 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100417 tsegbasedelta = tsegbase & (tsegsize - 1);
418 tsegbase &= ~(tsegsize - 1);
419
420 gttbase -= tsegbasedelta;
421 gfxstolenbase -= tsegbasedelta;
422 toludbase -= tsegbasedelta;
423
Angel Pons7c49cb82020-03-16 23:17:32 +0100424 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100425 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100426 /* Reclaim is possible */
427 reclaim = 1;
428 remapbase = MAX(4096, tom - me_uma_size);
429 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
430 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100431 } else {
432 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100433 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100434 touudbase = tom - me_uma_size;
435 }
436
Angel Pons7c49cb82020-03-16 23:17:32 +0100437 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100438 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
439
Angel Pons7c49cb82020-03-16 23:17:32 +0100440 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442 val = tom & 0xfff;
443 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100444 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100445 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100446
Angel Ponsb31d1d72020-01-10 01:35:09 +0100447 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100448 val = tom & 0xfffff000;
449 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100450 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100451 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100452
Angel Pons7c49cb82020-03-16 23:17:32 +0100453 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100454 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100455 val = toludbase & 0xfff;
456 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100457 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100458 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100459
Angel Pons7c49cb82020-03-16 23:17:32 +0100460 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100461 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100462 val = touudbase & 0xfff;
463 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100464 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100465 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100466
Angel Pons7c49cb82020-03-16 23:17:32 +0100467 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100468 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100469 val = touudbase & 0xfffff000;
470 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100471 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100472 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100473
474 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100475 /* REMAP BASE */
476 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100477 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100478
Angel Pons7c49cb82020-03-16 23:17:32 +0100479 /* REMAP LIMIT */
480 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100481 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100482 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100483 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100484 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100485 val = tsegbase & 0xfff;
486 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100487 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100488 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100489
Angel Pons7c49cb82020-03-16 23:17:32 +0100490 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100491 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100492 val = gfxstolenbase & 0xfff;
493 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100494 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100495 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100496
Angel Pons7c49cb82020-03-16 23:17:32 +0100497 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100498 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100499 val = gttbase & 0xfff;
500 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100501 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100502 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100503
504 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100505 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100506 val = (0x80000 - me_uma_size) & 0xfffff000;
507 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100508 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100509 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100510
Angel Pons7c49cb82020-03-16 23:17:32 +0100511 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513 val = mestolenbase & 0xfff;
514 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100515 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100516 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100517
Angel Ponsb31d1d72020-01-10 01:35:09 +0100518 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100519 val = mestolenbase & 0xfffff000;
520 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100521 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100522 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100523
Angel Pons7c49cb82020-03-16 23:17:32 +0100524 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100525 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526 val = (0x80000 - me_uma_size) & 0xfff;
527 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100528 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
529 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100530 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100531 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100532 }
533}
534
Angel Pons88521882020-01-05 20:21:20 +0100535static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100536{
537 int channel, slotrank;
538
Angel Pons7c49cb82020-03-16 23:17:32 +0100539 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100540 channel = (ctrl->rankmap[0]) ? 0 : 1;
541
Angel Pons88521882020-01-05 20:21:20 +0100542 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100543
Angel Pons7c49cb82020-03-16 23:17:32 +0100544 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100545 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
546
Angel Ponsffd50152020-11-12 11:03:10 +0100547 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100548
Angel Pons7c49cb82020-03-16 23:17:32 +0100549 /*
550 * Execute command queue - why is bit 22 set here?!
551 *
552 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
553 */
Angel Pons38d901e2020-05-02 23:50:43 +0200554 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200555
Angel Pons88521882020-01-05 20:21:20 +0100556 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100557}
558
Angel Pons88521882020-01-05 20:21:20 +0100559void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560{
Felix Held9fe248f2018-07-31 20:59:45 +0200561 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100562 int channel;
563
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
565 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566 do {
Angel Pons88521882020-01-05 20:21:20 +0100567 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100568 } while ((reg & 0x14) == 0);
569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100572 MCHBAR32(MC_INIT_STATE_G) = reg;
573 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100574 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100575 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100576
Angel Pons7c49cb82020-03-16 23:17:32 +0100577 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100578 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100579
Angel Pons7c49cb82020-03-16 23:17:32 +0100580 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581 udelay(200);
582
Angel Pons7c49cb82020-03-16 23:17:32 +0100583 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100584 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100585
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100587 udelay(500);
588
Angel Pons7c49cb82020-03-16 23:17:32 +0100589 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100590 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100591
Angel Pons7c49cb82020-03-16 23:17:32 +0100592 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100593 udelay(1);
594
595 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200597 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100598 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100599
Angel Pons7c49cb82020-03-16 23:17:32 +0100600 /* Wait 10ns for ranks to settle */
601 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100602
603 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100604 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100605
Angel Pons7c49cb82020-03-16 23:17:32 +0100606 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100607 write_reset(ctrl);
608 }
609}
610
Angel Pons3d3bf482020-11-14 16:18:15 +0100611/*
612 * DDR3 Rank1 Address mirror swap the following pins:
613 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
614 */
615static void ddr3_mirror_mrreg(int *bank, u32 *addr)
616{
617 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
618 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
619}
620
Angel Pons7c49cb82020-03-16 23:17:32 +0100621static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100622{
Angel Pons88521882020-01-05 20:21:20 +0100623 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100624
Angel Pons3d3bf482020-11-14 16:18:15 +0100625 if (ctrl->rank_mirror[channel][slotrank])
626 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100627
Angel Pons8f0757e2020-11-11 23:03:36 +0100628 const struct iosav_ssq sequence[] = {
629 /* DRAM command MRS */
630 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200631 .sp_cmd_ctrl = {
632 .command = IOSAV_MRS,
633 },
634 .subseq_ctrl = {
635 .cmd_executions = 1,
636 .cmd_delay_gap = 4,
637 .post_ssq_wait = 4,
638 .data_direction = SSQ_NA,
639 },
640 .sp_cmd_addr = {
641 .address = val,
642 .rowbits = 6,
643 .bank = reg,
644 .rank = slotrank,
645 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100646 },
647 /* DRAM command MRS */
648 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200649 .sp_cmd_ctrl = {
650 .command = IOSAV_MRS,
651 .ranksel_ap = 1,
652 },
653 .subseq_ctrl = {
654 .cmd_executions = 1,
655 .cmd_delay_gap = 4,
656 .post_ssq_wait = 4,
657 .data_direction = SSQ_NA,
658 },
659 .sp_cmd_addr = {
660 .address = val,
661 .rowbits = 6,
662 .bank = reg,
663 .rank = slotrank,
664 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100665 },
666 /* DRAM command MRS */
667 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200668 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100669 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200670 },
671 .subseq_ctrl = {
672 .cmd_executions = 1,
673 .cmd_delay_gap = 4,
674 .post_ssq_wait = ctrl->tMOD,
675 .data_direction = SSQ_NA,
676 },
677 .sp_cmd_addr = {
678 .address = val,
679 .rowbits = 6,
680 .bank = reg,
681 .rank = slotrank,
682 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100683 },
684 };
685 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200686
Angel Pons7c49cb82020-03-16 23:17:32 +0100687 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200688 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100689}
690
Angel Pons09fc4b92020-11-19 12:02:07 +0100691/* Obtain optimal power down mode for current configuration */
692static enum pdwm_mode get_power_down_mode(ramctr_timing *ctrl)
693{
694 if (ctrl->tXP > 8)
695 return PDM_NONE;
696
697 if (ctrl->tXPDLL > 32)
698 return PDM_PPD;
699
700 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
701 return PDM_DLL_OFF;
702
703 return PDM_APD_PPD;
704}
705
Angel Pons88521882020-01-05 20:21:20 +0100706static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100707{
708 u16 mr0reg, mch_cas, mch_wr;
709 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Angel Pons09fc4b92020-11-19 12:02:07 +0100710
711 const enum pdwm_mode power_down = get_power_down_mode(ctrl);
712
713 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100714
Angel Pons7c49cb82020-03-16 23:17:32 +0100715 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100716 if (ctrl->CAS < 12) {
717 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
718 } else {
719 mch_cas = (u16) (ctrl->CAS - 12);
720 mch_cas = ((mch_cas << 1) | 0x1);
721 }
722
Angel Pons7c49cb82020-03-16 23:17:32 +0100723 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100724 mch_wr = mch_wr_t[ctrl->tWR - 5];
725
Angel Pons2bf28ed2020-11-12 13:49:59 +0100726 /* DLL Reset - self clearing - set after CLK frequency has been changed */
727 mr0reg = 1 << 8;
728
729 mr0reg |= (mch_cas & 0x1) << 2;
730 mr0reg |= (mch_cas & 0xe) << 3;
731 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100732
Angel Pons09fc4b92020-11-19 12:02:07 +0100733 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
734 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100735 return mr0reg;
736}
737
738static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
739{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200740 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100741}
742
Angel Ponsf9997482020-11-12 16:02:52 +0100743static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100744{
745 /* Get ODT based on rankmap */
746 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
747
748 if (dimms_per_ch == 1) {
749 return (const odtmap){60, 60};
750 } else {
751 return (const odtmap){120, 30};
752 }
753}
754
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100755static u32 encode_odt(u32 odt)
756{
757 switch (odt) {
758 case 30:
759 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
760 case 60:
761 return (1 << 2); // RZQ/4
762 case 120:
763 return (1 << 6); // RZQ/2
764 default:
765 case 0:
766 return 0;
767 }
768}
769
770static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
771{
772 odtmap odt;
773 u32 mr1reg;
774
Angel Ponsf9997482020-11-12 16:02:52 +0100775 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100776 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100777
778 mr1reg |= encode_odt(odt.rttnom);
779
780 return mr1reg;
781}
782
783static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
784{
785 u16 mr1reg;
786
787 mr1reg = make_mr1(ctrl, rank, channel);
788
789 write_mrreg(ctrl, channel, rank, 1, mr1reg);
790}
791
792static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
793{
Angel Pons868bca22020-11-13 13:38:04 +0100794 const u16 pasr = 0;
795 const u16 cwl = ctrl->CWL - 5;
796 const odtmap odt = get_ODT(ctrl, channel);
797
Angel Ponsdca3cb52020-11-13 13:42:07 +0100798 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100799 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
800 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100801
Angel Pons868bca22020-11-13 13:38:04 +0100802 u16 mr2reg = 0;
803 mr2reg |= pasr;
804 mr2reg |= cwl << 3;
805 mr2reg |= ctrl->auto_self_refresh << 6;
806 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100807 mr2reg |= (odt.rttwr / 60) << 9;
808
809 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100810
811 /* Program MR2 shadow */
812 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
813
814 reg32 &= 3 << 14 | 3 << 6;
815
816 reg32 |= mr2reg & ~(3 << 6);
817
818 if (rank & 1) {
819 if (srt)
820 reg32 |= 1 << (rank / 2 + 6);
821 } else {
822 if (ctrl->rank_mirror[channel][rank])
823 reg32 |= 1 << (rank / 2 + 14);
824 }
825 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100826}
827
828static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
829{
830 write_mrreg(ctrl, channel, rank, 3, 0);
831}
832
Angel Pons88521882020-01-05 20:21:20 +0100833void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100834{
835 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100836 int channel;
837
838 FOR_ALL_POPULATED_CHANNELS {
839 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100840 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100841 dram_mr2(ctrl, slotrank, channel);
842
Angel Pons7c49cb82020-03-16 23:17:32 +0100843 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100844 dram_mr3(ctrl, slotrank, channel);
845
Angel Pons7c49cb82020-03-16 23:17:32 +0100846 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100847 dram_mr1(ctrl, slotrank, channel);
848
Angel Pons7c49cb82020-03-16 23:17:32 +0100849 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100850 dram_mr0(ctrl, slotrank, channel);
851 }
852 }
853
Angel Pons8f0757e2020-11-11 23:03:36 +0100854 const struct iosav_ssq zqcl_sequence[] = {
855 /* DRAM command NOP (without ODT nor chip selects) */
856 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200857 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100858 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200859 },
860 .subseq_ctrl = {
861 .cmd_executions = 1,
862 .cmd_delay_gap = 4,
863 .post_ssq_wait = 15,
864 .data_direction = SSQ_NA,
865 },
866 .sp_cmd_addr = {
867 .address = 2,
868 .rowbits = 6,
869 .bank = 0,
870 .rank = 0,
871 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100872 },
873 /* DRAM command ZQCL */
874 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200875 .sp_cmd_ctrl = {
876 .command = IOSAV_ZQCS,
877 .ranksel_ap = 1,
878 },
879 .subseq_ctrl = {
880 .cmd_executions = 1,
881 .cmd_delay_gap = 4,
882 .post_ssq_wait = 400,
883 .data_direction = SSQ_NA,
884 },
885 .sp_cmd_addr = {
886 .address = 1024,
887 .rowbits = 6,
888 .bank = 0,
889 .rank = 0,
890 },
891 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100892 .inc_rank = 1,
893 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200894 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100895 },
896 };
897 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100898
Angel Pons7c49cb82020-03-16 23:17:32 +0100899 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200900 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100902 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100903 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100904 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100905 }
906
Angel Pons7c49cb82020-03-16 23:17:32 +0100907 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100908 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100909
910 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100911 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100912
Angel Pons88521882020-01-05 20:21:20 +0100913 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100914
915 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
916
Angel Pons7c49cb82020-03-16 23:17:32 +0100917 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100918 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100919
Angel Ponsffd50152020-11-12 11:03:10 +0100920 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200921
Angel Pons7c49cb82020-03-16 23:17:32 +0100922 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200923 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100924
Angel Pons7c49cb82020-03-16 23:17:32 +0100925 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100926 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100927 }
928}
929
Felix Held3b906032020-01-14 17:05:43 +0100930static const u32 lane_base[] = {
931 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
932 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
933 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100934};
935
Angel Pons88521882020-01-05 20:21:20 +0100936void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100937{
Angel Pons737f1112020-11-13 14:07:30 +0100938 u32 reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100939 int lane;
940 int slotrank, slot;
941 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100942 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100943
944 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100945 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
946 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100947 }
948
949 for (slot = 0; slot < NUM_SLOTS; slot++)
950 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
951 case 0:
952 default:
Angel Pons88521882020-01-05 20:21:20 +0100953 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100954 break;
955 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100956 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100957 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100958 break;
959 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100960 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100961 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100962 break;
963 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100964 pi_coding_ctrl[slot] =
965 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100966 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100967 break;
968 }
969
Angel Pons7c49cb82020-03-16 23:17:32 +0100970 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100971 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
972 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
973 };
974 cmd_pi_coding.cmd_pi_code = full_shift & 0x3f;
975 cmd_pi_coding.cmd_logic_delay = !!(full_shift & 0x40);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100976
Angel Pons737f1112020-11-13 14:07:30 +0100977 cmd_pi_coding.ctl_pi_code_d0 = pi_coding_ctrl[0] & 0x3f;
978 cmd_pi_coding.ctl_pi_code_d1 = pi_coding_ctrl[1] & 0x3f;
979 cmd_pi_coding.ctl_logic_delay_d0 = !!(pi_coding_ctrl[0] & 0x40);
980 cmd_pi_coding.ctl_logic_delay_d1 = !!(pi_coding_ctrl[1] & 0x40);
981
982 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100983
Angel Pons7c49cb82020-03-16 23:17:32 +0100984 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100985 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
986 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100987
988 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100989 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100990 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100991 if (shift < 0)
992 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100993
Angel Pons88521882020-01-05 20:21:20 +0100994 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100995
996 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100997 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
998 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100999 }
1000
Angel Pons88521882020-01-05 20:21:20 +01001001 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
1002 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001003
Angel Pons88521882020-01-05 20:21:20 +01001004 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001005 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001006
Angel Pons88521882020-01-05 20:21:20 +01001007 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001008
1009 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +01001010 int post_timA_min_high = 7, pre_timA_min_high = 7;
1011 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001012 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001013 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001014
1015 if (shift < 0)
1016 shift = 0;
1017
1018 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +02001019 post_timA_min_high = MIN(post_timA_min_high,
1020 (ctrl->timings[channel][slotrank].lanes[lane].
1021 timA + shift) >> 6);
1022 pre_timA_min_high = MIN(pre_timA_min_high,
1023 ctrl->timings[channel][slotrank].lanes[lane].
1024 timA >> 6);
1025 post_timA_max_high = MAX(post_timA_max_high,
1026 (ctrl->timings[channel][slotrank].lanes[lane].
1027 timA + shift) >> 6);
1028 pre_timA_max_high = MAX(pre_timA_max_high,
1029 ctrl->timings[channel][slotrank].lanes[lane].
1030 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001031 }
1032
1033 if (pre_timA_max_high - pre_timA_min_high <
1034 post_timA_max_high - post_timA_min_high)
1035 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001036
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001037 else if (pre_timA_max_high - pre_timA_min_high >
1038 post_timA_max_high - post_timA_min_high)
1039 shift_402x = -1;
1040
Felix Helddee167e2019-12-30 17:30:16 +01001041 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001042 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001043 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001044
Angel Pons88521882020-01-05 20:21:20 +01001045 reg_roundtrip_latency |=
1046 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001047 shift_402x) << (8 * slotrank);
1048
1049 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001050 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001051 (((ctrl->timings[channel][slotrank].lanes[lane].
1052 timA + shift) & 0x3f)
1053 |
1054 ((ctrl->timings[channel][slotrank].lanes[lane].
1055 rising + shift) << 8)
1056 |
1057 (((ctrl->timings[channel][slotrank].lanes[lane].
1058 timA + shift -
1059 (post_timA_min_high << 6)) & 0x1c0) << 10)
1060 | ((ctrl->timings[channel][slotrank].lanes[lane].
1061 falling + shift) << 20));
1062
Felix Heldfb19c8a2020-01-14 21:27:59 +01001063 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001064 (((ctrl->timings[channel][slotrank].lanes[lane].
1065 timC + shift) & 0x3f)
1066 |
1067 (((ctrl->timings[channel][slotrank].lanes[lane].
1068 timB + shift) & 0x3f) << 8)
1069 |
1070 (((ctrl->timings[channel][slotrank].lanes[lane].
1071 timB + shift) & 0x1c0) << 9)
1072 |
1073 (((ctrl->timings[channel][slotrank].lanes[lane].
1074 timC + shift) & 0x40) << 13));
1075 }
1076 }
Angel Pons88521882020-01-05 20:21:20 +01001077 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1078 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001079}
1080
Angel Pons88521882020-01-05 20:21:20 +01001081static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001082{
Angel Pons88521882020-01-05 20:21:20 +01001083 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001084
Angel Ponsffd50152020-11-12 11:03:10 +01001085 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001086
Angel Pons7c49cb82020-03-16 23:17:32 +01001087 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001088 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001089
Angel Pons88521882020-01-05 20:21:20 +01001090 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001091}
1092
Angel Pons7c49cb82020-03-16 23:17:32 +01001093static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001094{
1095 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001096
1097 return (MCHBAR32(lane_base[lane] +
1098 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001099}
1100
1101struct run {
1102 int middle;
1103 int end;
1104 int start;
1105 int all;
1106 int length;
1107};
1108
1109static struct run get_longest_zero_run(int *seq, int sz)
1110{
1111 int i, ls;
1112 int bl = 0, bs = 0;
1113 struct run ret;
1114
1115 ls = 0;
1116 for (i = 0; i < 2 * sz; i++)
1117 if (seq[i % sz]) {
1118 if (i - ls > bl) {
1119 bl = i - ls;
1120 bs = ls;
1121 }
1122 ls = i + 1;
1123 }
1124 if (bl == 0) {
1125 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001126 ret.start = 0;
1127 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001128 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001129 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001130 return ret;
1131 }
1132
Angel Pons7c49cb82020-03-16 23:17:32 +01001133 ret.start = bs % sz;
1134 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001135 ret.middle = (bs + (bl - 1) / 2) % sz;
1136 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001137 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001138
1139 return ret;
1140}
1141
Angel Ponsf3053392020-11-13 23:31:12 +01001142static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001143{
1144 int timA;
1145 int statistics[NUM_LANES][128];
1146 int lane;
1147
1148 for (timA = 0; timA < 128; timA++) {
1149 FOR_ALL_LANES {
1150 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1151 }
1152 program_timings(ctrl, channel);
1153
1154 test_timA(ctrl, channel, slotrank);
1155
1156 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001157 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001158 }
1159 }
1160 FOR_ALL_LANES {
1161 struct run rn = get_longest_zero_run(statistics[lane], 128);
1162 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1163 upperA[lane] = rn.end;
1164 if (upperA[lane] < rn.middle)
1165 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001166
Patrick Rudolph368b6152016-11-25 16:36:52 +01001167 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001168 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001169 }
1170}
1171
Angel Ponsf3053392020-11-13 23:31:12 +01001172static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001173{
1174 int timA_delta;
1175 int statistics[NUM_LANES][51];
1176 int lane, i;
1177
1178 memset(statistics, 0, sizeof(statistics));
1179
1180 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001181
1182 FOR_ALL_LANES {
1183 ctrl->timings[channel][slotrank].lanes[lane].timA
1184 = upperA[lane] + timA_delta + 0x40;
1185 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001186 program_timings(ctrl, channel);
1187
1188 for (i = 0; i < 100; i++) {
1189 test_timA(ctrl, channel, slotrank);
1190 FOR_ALL_LANES {
1191 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001192 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 }
1194 }
1195 }
1196 FOR_ALL_LANES {
1197 int last_zero, first_all;
1198
1199 for (last_zero = -25; last_zero <= 25; last_zero++)
1200 if (statistics[lane][last_zero + 25])
1201 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 last_zero--;
1204 for (first_all = -25; first_all <= 25; first_all++)
1205 if (statistics[lane][first_all + 25] == 100)
1206 break;
1207
Angel Pons7c49cb82020-03-16 23:17:32 +01001208 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001209
1210 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001211 (last_zero + first_all) / 2 + upperA[lane];
1212
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001213 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001214 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001215 }
1216}
1217
Angel Ponsf3053392020-11-13 23:31:12 +01001218static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001219{
1220 int works[NUM_LANES];
1221 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001222
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001223 while (1) {
1224 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001225
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001226 program_timings(ctrl, channel);
1227 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001228
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001230 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1231
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001232 if (works[lane])
1233 some_works = 1;
1234 else
1235 all_works = 0;
1236 }
1237 if (all_works)
1238 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001239
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001240 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001241 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1243 channel, slotrank);
1244 return MAKE_ERR;
1245 }
Angel Pons88521882020-01-05 20:21:20 +01001246 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001247 printram("4024 -= 2;\n");
1248 continue;
1249 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001250 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001251 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001252
Felix Heldef4fe3e2019-12-31 14:15:05 +01001253 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001254 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1255 channel, slotrank);
1256 return MAKE_ERR;
1257 }
1258 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001259 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001260 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001261 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001262 }
1263 }
1264 return 0;
1265}
1266
Angel Pons12bd8ab2020-11-13 23:10:52 +01001267static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001268{
1269 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001270 u16 logic_delay_min = 7;
1271 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001272
1273 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001274 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1275
1276 logic_delay_min = MIN(logic_delay_min, logic_delay);
1277 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001278 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001279
1280 if (logic_delay_max < logic_delay_min) {
1281 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1282 logic_delay_max, logic_delay_min, channel, slotrank);
1283 }
1284
1285 assert(logic_delay_max >= logic_delay_min);
1286
1287 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001288}
1289
Angel Pons12bd8ab2020-11-13 23:10:52 +01001290static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001291{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001292 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001293
Angel Pons7c49cb82020-03-16 23:17:32 +01001294 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001295 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001296
Angel Pons12bd8ab2020-11-13 23:10:52 +01001297 if (prev < post)
1298 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001299
Angel Pons12bd8ab2020-11-13 23:10:52 +01001300 else if (prev > post)
1301 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001302
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001303 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001304 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001305
Angel Pons12bd8ab2020-11-13 23:10:52 +01001306 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1307 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1308 printram("4024 += %d;\n", latency_offset);
1309 printram("4028 += %d;\n", latency_offset);
1310
1311 return post;
1312}
1313
1314static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1315{
1316 u16 logic_delay_min = 7;
1317 int lane;
1318
1319 FOR_ALL_LANES {
1320 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1321
1322 logic_delay_min = MIN(logic_delay_min, logic_delay);
1323 }
1324
1325 if (logic_delay_min >= 2) {
1326 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1327 logic_delay_min, channel, slotrank);
1328 }
1329
1330 FOR_ALL_LANES {
1331 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1332 }
1333 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1334 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001335}
1336
Angel Pons7f5a97c2020-11-13 16:58:46 +01001337int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001338{
1339 int channel, slotrank, lane;
1340 int err;
1341
1342 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1343 int all_high, some_high;
1344 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001345 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001346
Angel Pons88521882020-01-05 20:21:20 +01001347 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001348
Angel Ponsffd50152020-11-12 11:03:10 +01001349 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001350
Angel Pons7c49cb82020-03-16 23:17:32 +01001351 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001352 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001353
Angel Pons58b609b2020-11-13 14:35:29 +01001354 const union gdcr_training_mod_reg training_mod = {
1355 .receive_enable_mode = 1,
1356 .training_rank_sel = slotrank,
1357 .odt_always_on = 1,
1358 };
1359 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001360
Felix Heldef4fe3e2019-12-31 14:15:05 +01001361 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001362 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001363 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001364
Angel Ponsf3053392020-11-13 23:31:12 +01001365 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001366
Felix Held2bb3cdf2018-07-28 00:23:59 +02001367 all_high = 1;
1368 some_high = 0;
1369 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001370 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001371 some_high = 1;
1372 else
1373 all_high = 0;
1374 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001375
1376 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001377 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001378 printram("4028--;\n");
1379 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001380 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001381 upperA[lane] -= 0x40;
1382
1383 }
1384 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001385 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001386 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001387 printram("4024++;\n");
1388 printram("4028++;\n");
1389 }
1390
1391 program_timings(ctrl, channel);
1392
Angel Pons12bd8ab2020-11-13 23:10:52 +01001393 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394
Angel Ponsf3053392020-11-13 23:31:12 +01001395 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396 if (err)
1397 return err;
1398
Angel Pons12bd8ab2020-11-13 23:10:52 +01001399 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001400
Angel Ponsf3053392020-11-13 23:31:12 +01001401 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001402
Angel Pons12bd8ab2020-11-13 23:10:52 +01001403 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001404
Angel Pons12bd8ab2020-11-13 23:10:52 +01001405 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001406
Angel Pons12bd8ab2020-11-13 23:10:52 +01001407 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001408
1409 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001410 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001411 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001412
1413 printram("final results:\n");
1414 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001415 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001416 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001417
Angel Pons88521882020-01-05 20:21:20 +01001418 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001419
1420 toggle_io_reset();
1421 }
1422
1423 FOR_ALL_POPULATED_CHANNELS {
1424 program_timings(ctrl, channel);
1425 }
Angel Ponsc6742232020-11-15 13:26:21 +01001426
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001427 return 0;
1428}
1429
Angel Pons011661c2020-11-15 18:21:35 +01001430static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001431{
1432 int lane;
1433
1434 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001435 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1436 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001437 }
1438
Angel Pons88521882020-01-05 20:21:20 +01001439 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001440
Angel Ponsffd50152020-11-12 11:03:10 +01001441 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1442 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001443
Angel Pons7c49cb82020-03-16 23:17:32 +01001444 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001445 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001446
Angel Pons88521882020-01-05 20:21:20 +01001447 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001448
Angel Pons801a5cb2020-11-15 15:48:29 +01001449 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001450
Angel Pons7c49cb82020-03-16 23:17:32 +01001451 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001452 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001453
Angel Pons88521882020-01-05 20:21:20 +01001454 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001455}
1456
Angel Pons011661c2020-11-15 18:21:35 +01001457static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001458{
1459 int min = data[0];
1460 int max = min;
1461 int i;
1462 for (i = 1; i < count; i++) {
1463 if (min > data[i])
1464 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001465
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001466 if (max < data[i])
1467 max = data[i];
1468 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001469 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001470 for (i = 0; i < count; i++)
1471 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001472
Angel Pons891f2bc2020-01-10 01:27:28 +01001473 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001474}
1475
Angel Pons011661c2020-11-15 18:21:35 +01001476static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001477{
Angel Pons011661c2020-11-15 18:21:35 +01001478 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001479 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001480 int lane;
1481
Angel Pons88521882020-01-05 20:21:20 +01001482 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001483
Angel Ponsffd50152020-11-12 11:03:10 +01001484 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001485
Angel Pons7c49cb82020-03-16 23:17:32 +01001486 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001487 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001488
Angel Pons011661c2020-11-15 18:21:35 +01001489 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1490 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001491 program_timings(ctrl, channel);
1492
Angel Pons011661c2020-11-15 18:21:35 +01001493 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001494
1495 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001496 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001497 }
1498 }
1499 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001500 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1501
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001502 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001503 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1504 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001505 /*
1506 * With command training not being done yet, the lane can be erroneous.
1507 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001508 */
Angel Pons011661c2020-11-15 18:21:35 +01001509 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001510 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1511
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001512 if (rn.all || rn.length < 8) {
1513 printk(BIOS_EMERG, "timC recovery failed\n");
1514 return MAKE_ERR;
1515 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001516 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001517 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001518 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001519 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001520 }
1521 return 0;
1522}
1523
Angel Pons88521882020-01-05 20:21:20 +01001524static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001525{
1526 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001527
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001528 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1529 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001530
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001531 return ret;
1532}
1533
Angel Pons765d4652020-11-11 14:44:35 +01001534/* Each cacheline is 64 bits long */
1535static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1536{
1537 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1538}
1539
Angel Pons88521882020-01-05 20:21:20 +01001540static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001541{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301542 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001543 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001544
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001545 for (j = 0; j < 16; j++)
1546 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001547
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001548 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001549
1550 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001551}
1552
Angel Pons88521882020-01-05 20:21:20 +01001553static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001554{
1555 int ret = 0;
1556 int channel;
1557 FOR_ALL_POPULATED_CHANNELS ret++;
1558 return ret;
1559}
1560
Angel Pons88521882020-01-05 20:21:20 +01001561static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001562{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301563 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001564 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301565 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001566
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001567 for (j = 0; j < 16; j++)
1568 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001569
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001570 for (j = 0; j < 16; j++)
1571 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001572
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001573 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001574
1575 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001576}
1577
Angel Pons820bce72020-11-14 17:02:55 +01001578static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001579{
1580 int timB;
1581 int statistics[NUM_LANES][128];
1582 int lane;
1583
Angel Pons58b609b2020-11-13 14:35:29 +01001584 const union gdcr_training_mod_reg training_mod = {
1585 .write_leveling_mode = 1,
1586 .training_rank_sel = slotrank,
1587 .enable_dqs_wl = 5,
1588 .odt_always_on = 1,
1589 .force_drive_enable = 1,
1590 };
1591 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001592
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001593 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1594 int bank = 1;
1595
1596 if (ctrl->rank_mirror[channel][slotrank])
1597 ddr3_mirror_mrreg(&bank, &mr1reg);
1598
1599 wait_for_iosav(channel);
1600
1601 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1602
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001603 for (timB = 0; timB < 128; timB++) {
1604 FOR_ALL_LANES {
1605 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1606 }
1607 program_timings(ctrl, channel);
1608
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001609 /* Execute command queue */
1610 iosav_run_once(channel);
1611
1612 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001613
1614 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001615 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1616 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1617 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001618 }
1619 }
1620 FOR_ALL_LANES {
1621 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001622 /*
1623 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1624 * of timB by a small value, which might cause the 6-bit value to overflow if
1625 * it's close to 0x3f. Increment the value by a small offset if it's likely
1626 * to overflow, to make sure it won't overflow while running tests and bricks
1627 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001628 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001629 * TODO: find out why some tests (edge write discovery) increment timB.
1630 */
1631 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001632 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001633 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001634 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001635
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001636 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1637 if (rn.all) {
1638 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1639 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001640
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001641 return MAKE_ERR;
1642 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001643 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1644 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001645 }
1646 return 0;
1647}
1648
Angel Pons820bce72020-11-14 17:02:55 +01001649static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001650{
1651 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001652 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001653 if (val == 0xffffffffffffffffLL)
1654 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001655 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001656 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657 for (i = 0; i < 8; i++)
1658 if (val << (8 * (7 - i) + 4))
1659 return -i;
1660 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001661 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001662 for (i = 0; i < 8; i++)
1663 if (val >> (8 * (7 - i) + 4))
1664 return i;
1665 }
1666 return 8;
1667}
1668
Angel Ponsbf13ef02020-11-11 18:40:06 +01001669static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001670{
1671 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001672
1673 const union gdcr_training_mod_reg training_mod = {
1674 .dq_dqs_training_res = 1,
1675 };
1676 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1677
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001678 FOR_ALL_POPULATED_CHANNELS {
1679 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001680 }
1681 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1682
Angel Pons765d4652020-11-11 14:44:35 +01001683 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001684 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001685
Angel Pons88521882020-01-05 20:21:20 +01001686 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001687
Angel Ponsffd50152020-11-12 11:03:10 +01001688 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001689
Angel Pons7c49cb82020-03-16 23:17:32 +01001690 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001691 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001692
Angel Pons88521882020-01-05 20:21:20 +01001693 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001694
Angel Pons8f0757e2020-11-11 23:03:36 +01001695 const struct iosav_ssq rd_sequence[] = {
1696 /* DRAM command PREA */
1697 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001698 .sp_cmd_ctrl = {
1699 .command = IOSAV_PRE,
1700 .ranksel_ap = 1,
1701 },
1702 .subseq_ctrl = {
1703 .cmd_executions = 1,
1704 .cmd_delay_gap = 3,
1705 .post_ssq_wait = ctrl->tRP,
1706 .data_direction = SSQ_NA,
1707 },
1708 .sp_cmd_addr = {
1709 .address = 1024,
1710 .rowbits = 6,
1711 .bank = 0,
1712 .rank = slotrank,
1713 },
1714 .addr_update = {
1715 .addr_wrap = 18,
1716 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001717 },
1718 /* DRAM command ACT */
1719 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001720 .sp_cmd_ctrl = {
1721 .command = IOSAV_ACT,
1722 .ranksel_ap = 1,
1723 },
1724 .subseq_ctrl = {
1725 .cmd_executions = 1,
1726 .cmd_delay_gap = 3,
1727 .post_ssq_wait = ctrl->tRCD,
1728 .data_direction = SSQ_NA,
1729 },
1730 .sp_cmd_addr = {
1731 .address = 0,
1732 .rowbits = 6,
1733 .bank = 0,
1734 .rank = slotrank,
1735 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001736 },
1737 /* DRAM command RD */
1738 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001739 .sp_cmd_ctrl = {
1740 .command = IOSAV_RD,
1741 .ranksel_ap = 3,
1742 },
1743 .subseq_ctrl = {
1744 .cmd_executions = 1,
1745 .cmd_delay_gap = 3,
1746 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001747 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001748 ctrl->timings[channel][slotrank].io_latency,
1749 .data_direction = SSQ_RD,
1750 },
1751 .sp_cmd_addr = {
1752 .address = 8,
1753 .rowbits = 6,
1754 .bank = 0,
1755 .rank = slotrank,
1756 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001757 },
1758 };
1759 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001760
Angel Pons7c49cb82020-03-16 23:17:32 +01001761 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001762 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001763
Angel Pons88521882020-01-05 20:21:20 +01001764 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001765 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001766 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001767 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001768 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001769
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001770 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1771 ctrl->timings[channel][slotrank].lanes[lane].timB +=
Angel Pons820bce72020-11-14 17:02:55 +01001772 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001773
1774 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001775 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1776 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001777 }
1778 }
Angel Pons88521882020-01-05 20:21:20 +01001779 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001780}
1781
Angel Pons7d115132020-11-14 01:44:44 +01001782static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001783{
Angel Pons7d115132020-11-14 01:44:44 +01001784 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001785
Angel Pons7d115132020-11-14 01:44:44 +01001786 FOR_ALL_POPULATED_CHANNELS {
1787 /* choose an existing rank */
1788 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001789
Angel Pons7d115132020-11-14 01:44:44 +01001790 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001791
Angel Pons7d115132020-11-14 01:44:44 +01001792 /* Execute command queue */
1793 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001794
Angel Pons7d115132020-11-14 01:44:44 +01001795 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001796
Angel Pons7d115132020-11-14 01:44:44 +01001797 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1798 }
1799
1800 /* Refresh disable */
1801 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1802
1803 FOR_ALL_POPULATED_CHANNELS {
1804 /* Execute the same command queue */
1805 iosav_run_once(channel);
1806
1807 wait_for_iosav(channel);
1808 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001809}
1810
Angel Pons7c49cb82020-03-16 23:17:32 +01001811/*
1812 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001814 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1815 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1816 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1817 * CLK/ADDR/CMD signals have the same routing delay.
1818 *
1819 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1820 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1821 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001822 */
Angel Pons820bce72020-11-14 17:02:55 +01001823static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001824{
Angel Pons820bce72020-11-14 17:02:55 +01001825 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826
Angel Pons7d115132020-11-14 01:44:44 +01001827 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001828
Angel Pons7c49cb82020-03-16 23:17:32 +01001829 /* Enable write leveling on all ranks
1830 Disable all DQ outputs
1831 Only NOP is allowed in this mode */
1832 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1833 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001834 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001835
Angel Ponsa1f17142020-11-15 12:50:03 +01001836 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001837 const union gdcr_training_mod_reg training_mod = {
1838 .write_leveling_mode = 1,
1839 .enable_dqs_wl = 5,
1840 .odt_always_on = 1,
1841 .force_drive_enable = 1,
1842 };
1843 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001844
1845 toggle_io_reset();
1846
Angel Pons7c49cb82020-03-16 23:17:32 +01001847 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001848 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001849 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001850 if (err)
1851 return err;
1852 }
1853
Angel Pons7c49cb82020-03-16 23:17:32 +01001854 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001855 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001856 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001857
Angel Pons88521882020-01-05 20:21:20 +01001858 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001859
1860 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001861 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001862
Angel Pons7c49cb82020-03-16 23:17:32 +01001863 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001864 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001865
1866 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001867 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001868 MCHBAR32(IOSAV_STATUS_ch(channel));
1869 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001870
Angel Ponsffd50152020-11-12 11:03:10 +01001871 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001872
Angel Pons7c49cb82020-03-16 23:17:32 +01001873 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001874 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001875
Angel Pons88521882020-01-05 20:21:20 +01001876 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001877 }
1878
1879 toggle_io_reset();
1880
Angel Pons820bce72020-11-14 17:02:55 +01001881 return 0;
1882}
1883
1884int write_training(ramctr_timing *ctrl)
1885{
Angel Ponsc6742232020-11-15 13:26:21 +01001886 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001887 int err;
1888
1889 FOR_ALL_POPULATED_CHANNELS
1890 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1891
Angel Pons4c76d252020-11-15 13:06:53 +01001892 printram("CPE\n");
1893
Angel Pons820bce72020-11-14 17:02:55 +01001894 err = jedec_write_leveling(ctrl);
1895 if (err)
1896 return err;
1897
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001898 printram("CPF\n");
1899
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001900 FOR_ALL_POPULATED_CHANNELS {
1901 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001902 }
1903
1904 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001905 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001906 if (err)
1907 return err;
1908 }
1909
1910 FOR_ALL_POPULATED_CHANNELS
1911 program_timings(ctrl, channel);
1912
1913 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001914 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001915
1916 FOR_ALL_POPULATED_CHANNELS
1917 program_timings(ctrl, channel);
1918
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001919 return 0;
1920}
1921
Angel Ponsbf13ef02020-11-11 18:40:06 +01001922static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001923{
1924 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
1925 int timC_delta;
1926 int lanes_ok = 0;
1927 int ctr = 0;
1928 int lane;
1929
1930 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
1931 FOR_ALL_LANES {
1932 ctrl->timings[channel][slotrank].lanes[lane].timC =
1933 saved_rt.lanes[lane].timC + timC_delta;
1934 }
1935 program_timings(ctrl, channel);
1936 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001937 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001938 }
1939
Angel Pons765d4652020-11-11 14:44:35 +01001940 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001941 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001942
Angel Pons88521882020-01-05 20:21:20 +01001943 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001944
Angel Ponsffd50152020-11-12 11:03:10 +01001945 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001946
1947 /* Program LFSR for the RD/WR subsequences */
1948 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1949 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001950
Angel Pons7c49cb82020-03-16 23:17:32 +01001951 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001952 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001953
Angel Pons88521882020-01-05 20:21:20 +01001954 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001955 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001956 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001957
1958 if (r32 == 0)
1959 lanes_ok |= 1 << lane;
1960 }
1961 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001962 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001963 break;
1964 }
1965
1966 ctrl->timings[channel][slotrank] = saved_rt;
1967
Patrick Rudolphdd662872017-10-28 18:20:11 +02001968 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001969}
1970
Angel Pons88521882020-01-05 20:21:20 +01001971static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301973 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01001974 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
1975 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001976
1977 if (patno) {
1978 u8 base8 = 0x80 >> ((patno - 1) % 8);
1979 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1980 for (i = 0; i < 32; i++) {
1981 for (j = 0; j < 16; j++) {
1982 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001983
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001984 if (invert[patno - 1][i] & (1 << (j / 2)))
1985 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001986
1987 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001988 }
1989 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001990 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001991 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1992 for (j = 0; j < 16; j++) {
1993 const u32 val = pattern[i][j];
1994 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1995 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001996 }
1997 sfence();
1998 }
Angel Pons765d4652020-11-11 14:44:35 +01001999
2000 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002001}
2002
Angel Pons88521882020-01-05 20:21:20 +01002003static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002004{
Angel Pons7d115132020-11-14 01:44:44 +01002005 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002006
Angel Pons7c49cb82020-03-16 23:17:32 +01002007 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002008 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002009
2010 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002011 dram_mrscommands(ctrl);
2012
2013 toggle_io_reset();
2014}
2015
Angel Ponsbf13ef02020-11-11 18:40:06 +01002016#define CT_MIN_PI -127
2017#define CT_MAX_PI 128
2018#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2019
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002020#define MIN_C320C_LEN 13
2021
2022static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2023{
2024 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2025 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002026 int command_pi;
2027 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002028 int delta = 0;
2029
2030 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2031
2032 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002033 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002034 }
2035
2036 ctrl->cmd_stretch[channel] = cmd_stretch;
2037
Angel Pons7a612742020-11-12 13:34:03 +01002038 const union tc_rap_reg tc_rap = {
2039 .tRRD = ctrl->tRRD,
2040 .tRTP = ctrl->tRTP,
2041 .tCKE = ctrl->tCKE,
2042 .tWTR = ctrl->tWTR,
2043 .tFAW = ctrl->tFAW,
2044 .tWR = ctrl->tWR,
2045 .tCMD = ctrl->cmd_stretch[channel],
2046 };
2047 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002048
2049 if (ctrl->cmd_stretch[channel] == 2)
2050 delta = 2;
2051 else if (ctrl->cmd_stretch[channel] == 0)
2052 delta = 4;
2053
2054 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002055 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002056 }
2057
Angel Ponsbf13ef02020-11-11 18:40:06 +01002058 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002059 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002060 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002061 }
2062 program_timings(ctrl, channel);
2063 reprogram_320c(ctrl);
2064 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002065 stat[slotrank][command_pi - CT_MIN_PI] =
2066 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002067 }
2068 }
2069 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002070 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002071
Angel Ponsbf13ef02020-11-11 18:40:06 +01002072 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002073 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2074 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002075
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002076 if (rn.all || rn.length < MIN_C320C_LEN) {
2077 FOR_ALL_POPULATED_RANKS {
2078 ctrl->timings[channel][slotrank] =
2079 saved_timings[channel][slotrank];
2080 }
2081 return MAKE_ERR;
2082 }
2083 }
2084
2085 return 0;
2086}
2087
Angel Pons7c49cb82020-03-16 23:17:32 +01002088/*
2089 * Adjust CMD phase shift and try multiple command rates.
2090 * A command rate of 2T doubles the time needed for address and command decode.
2091 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002092int command_training(ramctr_timing *ctrl)
2093{
2094 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002095
2096 FOR_ALL_POPULATED_CHANNELS {
2097 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002098 }
2099
2100 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002101 int cmdrate, err;
2102
2103 /*
2104 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002105 * Issue:
2106 * While c320c discovery seems to succeed raminit will fail in write training.
2107 *
2108 * Workaround:
2109 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2110 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002111 *
2112 * Single DIMM per channel:
2113 * Try command rate 1T and 2T
2114 */
2115 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002116 if (ctrl->tCMD)
2117 /* XMP gives the CMD rate in clock ticks, not ns */
2118 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002119
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002120 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002121 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2122
2123 if (!err)
2124 break;
2125 }
2126
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002127 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002128 printk(BIOS_EMERG, "c320c discovery failed\n");
2129 return err;
2130 }
2131
Angel Pons891f2bc2020-01-10 01:27:28 +01002132 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002133 }
2134
2135 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002136 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002137
2138 reprogram_320c(ctrl);
2139 return 0;
2140}
2141
Angel Pons4c79f932020-11-14 01:26:52 +01002142static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143{
Angel Pons96a06dd2020-11-14 00:33:18 +01002144 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002145 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146 int lane;
2147
Angel Pons96a06dd2020-11-14 00:33:18 +01002148 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002150 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2151 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002152 }
2153 program_timings(ctrl, channel);
2154
2155 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002156 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2157 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002158 }
2159
Angel Pons88521882020-01-05 20:21:20 +01002160 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002161
Angel Ponsffd50152020-11-12 11:03:10 +01002162 iosav_write_read_mpr_sequence(
2163 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002164
Angel Pons7c49cb82020-03-16 23:17:32 +01002165 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002166 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002167
Angel Pons88521882020-01-05 20:21:20 +01002168 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002169
2170 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002171 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002172 }
2173 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002174
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002175 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002176 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002177 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002178
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002179 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002180 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2181 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002182 return MAKE_ERR;
2183 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002184 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002185 }
2186 return 0;
2187}
2188
Angel Pons60971dc2020-11-14 00:49:38 +01002189static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2190{
2191 int slotrank, lane;
2192
2193 fill_pattern0(ctrl, channel, 0, 0);
2194 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002195 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002196 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2197 }
2198
2199 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2200 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2201 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2202 }
2203
2204 program_timings(ctrl, channel);
2205
2206 FOR_ALL_POPULATED_RANKS {
2207 wait_for_iosav(channel);
2208
2209 iosav_write_read_mpr_sequence(
2210 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2211
2212 /* Execute command queue */
2213 iosav_run_once(channel);
2214
2215 wait_for_iosav(channel);
2216 }
2217
2218 /* XXX: check any measured value ? */
2219
2220 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2221 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2222 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2223 }
2224
2225 program_timings(ctrl, channel);
2226
2227 FOR_ALL_POPULATED_RANKS {
2228 wait_for_iosav(channel);
2229
2230 iosav_write_read_mpr_sequence(
2231 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2232
2233 /* Execute command queue */
2234 iosav_run_once(channel);
2235
2236 wait_for_iosav(channel);
2237 }
2238
2239 /* XXX: check any measured value ? */
2240
2241 FOR_ALL_LANES {
2242 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2243 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2244 }
2245}
2246
Angel Pons4c79f932020-11-14 01:26:52 +01002247int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002248{
2249 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2250 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2251 int channel, slotrank, lane;
2252 int err;
2253
Angel Pons88521882020-01-05 20:21:20 +01002254 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002255
2256 toggle_io_reset();
2257
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002258 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002259 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002260
2261 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002262 }
2263
Angel Pons0c3936e2020-03-22 12:49:27 +01002264 /*
2265 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2266 * also use a single loop. It would seem that it is a debugging configuration.
2267 */
Angel Pons88521882020-01-05 20:21:20 +01002268 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2269 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002270
2271 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002272 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002273 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002274 if (err)
2275 return err;
2276 }
2277
Angel Pons88521882020-01-05 20:21:20 +01002278 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2279 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002280
2281 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002282 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002283 rising_edges[channel][slotrank]);
2284 if (err)
2285 return err;
2286 }
2287
Angel Pons88521882020-01-05 20:21:20 +01002288 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002289
2290 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2291 ctrl->timings[channel][slotrank].lanes[lane].falling =
2292 falling_edges[channel][slotrank][lane];
2293 ctrl->timings[channel][slotrank].lanes[lane].rising =
2294 rising_edges[channel][slotrank][lane];
2295 }
2296
2297 FOR_ALL_POPULATED_CHANNELS {
2298 program_timings(ctrl, channel);
2299 }
2300
Angel Pons50a6fe72020-11-14 01:18:14 +01002301 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002302 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002303 }
2304 return 0;
2305}
2306
Angel Pons08f749d2020-11-17 16:50:56 +01002307static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002308{
Angel Pons08f749d2020-11-17 16:50:56 +01002309 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2310
Angel Pons7c49cb82020-03-16 23:17:32 +01002311 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002312 int lower[NUM_LANES];
2313 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002314 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002315
2316 FOR_ALL_LANES {
2317 lower[lane] = 0;
2318 upper[lane] = MAX_EDGE_TIMING;
2319 }
2320
Angel Pons08f749d2020-11-17 16:50:56 +01002321 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002322 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002323 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002324 };
2325 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2326 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002327
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002328 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2329 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002331
Angel Pons08f749d2020-11-17 16:50:56 +01002332 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002333 FOR_ALL_LANES {
2334 ctrl->timings[channel][slotrank].lanes[lane].
Angel Pons08f749d2020-11-17 16:50:56 +01002335 rising = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336 ctrl->timings[channel][slotrank].lanes[lane].
Angel Pons08f749d2020-11-17 16:50:56 +01002337 falling = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002338 }
2339 program_timings(ctrl, channel);
2340
2341 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002342 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2343 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002344 }
Angel Pons88521882020-01-05 20:21:20 +01002345 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002346
Angel Ponsffd50152020-11-12 11:03:10 +01002347 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002348
Angel Pons7c49cb82020-03-16 23:17:32 +01002349 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002350 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002351
Angel Pons88521882020-01-05 20:21:20 +01002352 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002353 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002354 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002355 }
2356
Angel Pons7c49cb82020-03-16 23:17:32 +01002357 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002358 raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002359 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002360
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002361 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002362 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002363 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002364
2365 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2366 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002367
2368 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2369
2370 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2371 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2372 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002373 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002374
2375 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2376 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2377
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002378 edges[lane] = (lower[lane] + upper[lane]) / 2;
2379 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002380 printk(BIOS_EMERG, "edge write discovery failed: "
2381 "%d, %d, %d\n", channel, slotrank, lane);
2382
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002383 return MAKE_ERR;
2384 }
2385 }
2386 }
2387 }
2388
Angel Ponsa93f46e2020-11-17 16:54:01 +01002389 /* Restore nominal Vref after training */
2390 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002391 printram("CPA\n");
2392 return 0;
2393}
2394
Angel Pons08f749d2020-11-17 16:50:56 +01002395int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002396{
2397 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002398 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2399 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002400
Angel Pons7c49cb82020-03-16 23:17:32 +01002401 /*
2402 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2403 * also use a single loop. It would seem that it is a debugging configuration.
2404 */
Angel Pons88521882020-01-05 20:21:20 +01002405 MCHBAR32(IOSAV_DC_MASK) = 0x300;
Angel Pons08f749d2020-11-17 16:50:56 +01002406 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002407
2408 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002409 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002410 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002411 if (err)
2412 return err;
2413 }
2414
Angel Pons88521882020-01-05 20:21:20 +01002415 MCHBAR32(IOSAV_DC_MASK) = 0x200;
Angel Pons08f749d2020-11-17 16:50:56 +01002416 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002417
2418 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002419 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002420 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002421 if (err)
2422 return err;
2423 }
2424
Angel Pons88521882020-01-05 20:21:20 +01002425 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002426
2427 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2428 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002429 falling_edges[channel][slotrank][lane];
2430
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002431 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002432 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002433 }
2434
2435 FOR_ALL_POPULATED_CHANNELS
2436 program_timings(ctrl, channel);
2437
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002438 return 0;
2439}
2440
2441static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2442{
Angel Pons88521882020-01-05 20:21:20 +01002443 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002444
Angel Ponsffd50152020-11-12 11:03:10 +01002445 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002446
Angel Pons7c49cb82020-03-16 23:17:32 +01002447 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002448 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002449
Angel Pons88521882020-01-05 20:21:20 +01002450 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002451}
2452
2453int discover_timC_write(ramctr_timing *ctrl)
2454{
Angel Pons7c49cb82020-03-16 23:17:32 +01002455 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002456 int i, pat;
2457
2458 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2459 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2460 int channel, slotrank, lane;
2461
2462 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2463 lower[channel][slotrank][lane] = 0;
2464 upper[channel][slotrank][lane] = MAX_TIMC;
2465 }
2466
Angel Pons88521882020-01-05 20:21:20 +01002467 /*
2468 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2469 * FIXME: This must only be done on Ivy Bridge.
2470 */
2471 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002472 printram("discover timC write:\n");
2473
2474 for (i = 0; i < 3; i++)
2475 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002476
2477 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2478 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2479 ~0x3f000000, rege3c_b24[i] << 24);
2480
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002481 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002482
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002483 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2484 FOR_ALL_POPULATED_RANKS {
2485 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002486 u32 raw_stats[MAX_TIMC + 1];
2487 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002488
2489 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002490 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002491
2492 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002493
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002494 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002495 FOR_ALL_LANES {
2496 ctrl->timings[channel][slotrank]
2497 .lanes[lane].timC = timC;
2498 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499 program_timings(ctrl, channel);
2500
2501 test_timC_write (ctrl, channel, slotrank);
2502
Angel Pons7c49cb82020-03-16 23:17:32 +01002503 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002504 raw_stats[timC] = MCHBAR32(
2505 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002506 }
2507 FOR_ALL_LANES {
2508 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002509 for (timC = 0; timC < MAX_TIMC; timC++) {
2510 stats[timC] = !!(raw_stats[timC]
2511 & (1 << lane));
2512 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002513
Angel Pons7c49cb82020-03-16 23:17:32 +01002514 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002515 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002516 printk(BIOS_EMERG,
2517 "timC write discovery failed: "
2518 "%d, %d, %d\n", channel,
2519 slotrank, lane);
2520
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002521 return MAKE_ERR;
2522 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002523 printram("timC: %d, %d, %d: "
2524 "0x%02x-0x%02x-0x%02x, "
2525 "0x%02x-0x%02x\n", channel, slotrank,
2526 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002527 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002528 rn.end - ctrl->timC_offset[i]);
2529
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002530 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002531 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002532 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002533
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002534 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002535 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002536 upper[channel][slotrank][lane]);
2537
2538 }
2539 }
2540 }
2541 }
2542
2543 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002544 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002545 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002546 udelay(2);
2547 }
2548
Angel Pons88521882020-01-05 20:21:20 +01002549 /*
2550 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2551 * FIXME: This must only be done on Ivy Bridge.
2552 */
2553 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002554
2555 printram("CPB\n");
2556
2557 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002558 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002559 (lower[channel][slotrank][lane] +
2560 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002561
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002562 ctrl->timings[channel][slotrank].lanes[lane].timC =
2563 (lower[channel][slotrank][lane] +
2564 upper[channel][slotrank][lane]) / 2;
2565 }
2566 FOR_ALL_POPULATED_CHANNELS {
2567 program_timings(ctrl, channel);
2568 }
2569 return 0;
2570}
2571
Angel Pons88521882020-01-05 20:21:20 +01002572void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002573{
2574 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002575 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002576
2577 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2578 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002579 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002580 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002581 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002582 printram("normalize %d, %d, %d: mat %d\n",
2583 channel, slotrank, lane, mat);
2584
Felix Heldef4fe3e2019-12-31 14:15:05 +01002585 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002586 printram("normalize %d, %d, %d: delta %d\n",
2587 channel, slotrank, lane, delta);
2588
Angel Pons88521882020-01-05 20:21:20 +01002589 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002590 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002591 }
2592
2593 FOR_ALL_POPULATED_CHANNELS {
2594 program_timings(ctrl, channel);
2595 }
2596}
2597
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002598int channel_test(ramctr_timing *ctrl)
2599{
2600 int channel, slotrank, lane;
2601
2602 slotrank = 0;
2603 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002604 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002605 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002606 return MAKE_ERR;
2607 }
2608 FOR_ALL_POPULATED_CHANNELS {
2609 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002610 }
2611
2612 for (slotrank = 0; slotrank < 4; slotrank++)
2613 FOR_ALL_CHANNELS
2614 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2615 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002616 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2617 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002618 }
Angel Pons88521882020-01-05 20:21:20 +01002619 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002620
Angel Ponsffd50152020-11-12 11:03:10 +01002621 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002622
Angel Pons7c49cb82020-03-16 23:17:32 +01002623 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002624 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002625
Angel Pons88521882020-01-05 20:21:20 +01002626 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002627 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002628 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002629 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2630 channel, slotrank, lane);
2631 return MAKE_ERR;
2632 }
2633 }
2634 return 0;
2635}
2636
Patrick Rudolphdd662872017-10-28 18:20:11 +02002637void channel_scrub(ramctr_timing *ctrl)
2638{
2639 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002640 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002641
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002642 FOR_ALL_POPULATED_CHANNELS {
2643 wait_for_iosav(channel);
2644 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002645 }
2646
2647 /*
2648 * During runtime the "scrubber" will periodically scan through the memory in the
2649 * physical address space, to identify and fix CRC errors.
2650 * The following loops writes to every DRAM address, setting the ECC bits to the
2651 * correct value. A read from this location will no longer return a CRC error,
2652 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002653 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002654 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2655 * and firmware running in x86_32.
2656 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002657 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2658 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002659 for (bank = 0; bank < 8; bank++) {
2660 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002661
Angel Pons8f0757e2020-11-11 23:03:36 +01002662 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2663 const struct iosav_ssq sequence[] = {
2664 /*
2665 * DRAM command ACT
2666 * Opens the row for writing.
2667 */
2668 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002669 .sp_cmd_ctrl = {
2670 .command = IOSAV_ACT,
2671 .ranksel_ap = 1,
2672 },
2673 .subseq_ctrl = {
2674 .cmd_executions = 1,
2675 .cmd_delay_gap = gap,
2676 .post_ssq_wait = ctrl->tRCD,
2677 .data_direction = SSQ_NA,
2678 },
2679 .sp_cmd_addr = {
2680 .address = row,
2681 .rowbits = 6,
2682 .bank = bank,
2683 .rank = slotrank,
2684 },
2685 .addr_update = {
2686 .inc_addr_1 = 1,
2687 .addr_wrap = 18,
2688 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002689 },
2690 /*
2691 * DRAM command WR
2692 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2693 * bytes.
2694 */
2695 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002696 .sp_cmd_ctrl = {
2697 .command = IOSAV_WR,
2698 .ranksel_ap = 1,
2699 },
2700 .subseq_ctrl = {
2701 .cmd_executions = 129,
2702 .cmd_delay_gap = 4,
2703 .post_ssq_wait = ctrl->tWTR +
2704 ctrl->CWL + 8,
2705 .data_direction = SSQ_WR,
2706 },
2707 .sp_cmd_addr = {
2708 .address = row,
2709 .rowbits = 0,
2710 .bank = bank,
2711 .rank = slotrank,
2712 },
2713 .addr_update = {
2714 .inc_addr_8 = 1,
2715 .addr_wrap = 9,
2716 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002717 },
2718 /*
2719 * DRAM command PRE
2720 * Closes the row.
2721 */
2722 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002723 .sp_cmd_ctrl = {
2724 .command = IOSAV_PRE,
2725 .ranksel_ap = 1,
2726 },
2727 .subseq_ctrl = {
2728 .cmd_executions = 1,
2729 .cmd_delay_gap = 4,
2730 .post_ssq_wait = ctrl->tRP,
2731 .data_direction = SSQ_NA,
2732 },
2733 .sp_cmd_addr = {
2734 .address = 0,
2735 .rowbits = 6,
2736 .bank = bank,
2737 .rank = slotrank,
2738 },
2739 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002740 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002741 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002742 },
2743 };
2744 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002745
2746 /* Execute command queue */
2747 iosav_run_queue(channel, 16, 0);
2748
2749 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002750 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002751 }
2752 }
2753}
2754
Angel Pons88521882020-01-05 20:21:20 +01002755void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002756{
2757 int channel;
2758
Angel Pons7c49cb82020-03-16 23:17:32 +01002759 /* 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 +01002760 static u32 seeds[NUM_CHANNELS][3] = {
2761 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2762 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2763 };
2764 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002765 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002766 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2767 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2768 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002769 }
2770}
2771
Angel Pons89ae6b82020-03-21 13:23:32 +01002772void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002773{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002774 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002775 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002776 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002777 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002778 }
2779}
2780
Angel Pons88521882020-01-05 20:21:20 +01002781void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002782{
2783 int channel;
2784
2785 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002786 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002787 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002788 }
2789
2790 udelay(1);
2791
2792 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002793 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002794 }
2795}
2796
Angel Pons7c49cb82020-03-16 23:17:32 +01002797void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002798{
Angel Pons11463322020-11-19 11:04:28 +01002799 /* Use a larger delay when running fast to improve stability */
2800 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2801
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002802 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002803
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002804 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002805 int min_pi = 10000;
2806 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002807
2808 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002809 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2810 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002811 }
2812
Angel Pons7a612742020-11-12 13:34:03 +01002813 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002814
Angel Pons7a612742020-11-12 13:34:03 +01002815 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002816
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002817 dram_odt_stretch(ctrl, channel);
2818
Angel Pons7a612742020-11-12 13:34:03 +01002819 const union tc_rwp_reg tc_rwp = {
2820 .tRRDR = 0,
2821 .tRRDD = val,
2822 .tWWDR = val,
2823 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002824 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002825 .tWRDRDD = tWRDRDD,
2826 .tRWSR = 2,
2827 .dec_wrd = 1,
2828 };
2829 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002830 }
2831}
2832
Angel Pons88521882020-01-05 20:21:20 +01002833void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002834{
2835 int channel;
2836 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002837 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2838 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002839 }
2840}
2841
Angel Pons7c49cb82020-03-16 23:17:32 +01002842/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2843static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002844{
Angel Pons88521882020-01-05 20:21:20 +01002845 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002846}
2847
Angel Pons7c49cb82020-03-16 23:17:32 +01002848/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002849void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002850{
2851 int channel;
2852 int t1_cycles = 0, t1_ns = 0, t2_ns;
2853 int t3_ns;
2854 u32 r32;
2855
Angel Pons7c49cb82020-03-16 23:17:32 +01002856 /* FIXME: This register only exists on Ivy Bridge */
2857 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002858
Angel Pons7a612742020-11-12 13:34:03 +01002859 FOR_ALL_CHANNELS {
2860 union tc_othp_reg tc_othp = {
2861 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2862 };
2863 tc_othp.tCPDED = 1;
2864 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2865 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002866
Angel Pons09fc4b92020-11-19 12:02:07 +01002867 /* 64 DCLKs until idle, decision per rank */
2868 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002869
Felix Heldf9b826a2018-07-30 17:56:52 +02002870 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002871 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002872
Angel Pons88521882020-01-05 20:21:20 +01002873 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
2874 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002875
2876 FOR_ALL_CHANNELS {
2877 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002878 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002879 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002880 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002881 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002882 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002883 case 1:
2884 case 4:
2885 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002886 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002887 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002888 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002889 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002890 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002891 break;
2892 }
2893 }
2894
Felix Held50b7ed22019-12-30 20:41:54 +01002895 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002896 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002897 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002898
Angel Pons7a612742020-11-12 13:34:03 +01002899 FOR_ALL_CHANNELS {
2900 union tc_rfp_reg tc_rfp = {
2901 .raw = MCHBAR32(TC_RFP_ch(channel)),
2902 };
2903 tc_rfp.refresh_2x_control = 1;
2904 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2905 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002906
Angel Ponsdc5539f2020-11-12 12:44:25 +01002907 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2908 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002909 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002910
Angel Pons7c49cb82020-03-16 23:17:32 +01002911 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002912 FOR_ALL_POPULATED_CHANNELS
2913 break;
2914
Angel Pons88521882020-01-05 20:21:20 +01002915 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2916 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002917 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002918 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002919 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002921 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002922 t1_ns += 500;
2923
Angel Pons88521882020-01-05 20:21:20 +01002924 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002925 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002926 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002927 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002928 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002929 t3_ns = 500;
2930 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002931
2932 /* The graphics driver will use these watermark values */
2933 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002934 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002935 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2936 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002937}
2938
Angel Pons88521882020-01-05 20:21:20 +01002939void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002940{
Angel Ponsc6742232020-11-15 13:26:21 +01002941 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002942
Angel Pons7c49cb82020-03-16 23:17:32 +01002943 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002944 const union tc_rap_reg tc_rap = {
2945 .tRRD = ctrl->tRRD,
2946 .tRTP = ctrl->tRTP,
2947 .tCKE = ctrl->tCKE,
2948 .tWTR = ctrl->tWTR,
2949 .tFAW = ctrl->tFAW,
2950 .tWR = ctrl->tWR,
2951 .tCMD = ctrl->cmd_stretch[channel],
2952 };
2953 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002954 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002955
2956 udelay(1);
2957
2958 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002959 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002960 }
2961
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002962 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002963 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002964
2965 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002966 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002967 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002968 }
2969
2970 printram("CPE\n");
2971
Angel Pons88521882020-01-05 20:21:20 +01002972 MCHBAR32(GDCRTRAININGMOD) = 0;
2973 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002974
2975 printram("CP5b\n");
2976
2977 FOR_ALL_POPULATED_CHANNELS {
2978 program_timings(ctrl, channel);
2979 }
2980
2981 u32 reg, addr;
2982
Angel Pons7c49cb82020-03-16 23:17:32 +01002983 /* Poll for RCOMP */
2984 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2985 ;
2986
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002987 do {
Angel Pons88521882020-01-05 20:21:20 +01002988 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002989 } while ((reg & 0x14) == 0);
2990
Angel Pons7c49cb82020-03-16 23:17:32 +01002991 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002992 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002993 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002994
Angel Pons7c49cb82020-03-16 23:17:32 +01002995 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002996 udelay(500);
2997
2998 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002999 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003000 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003001 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003002 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003003 MCHBAR32(addr) = reg;
3004
Angel Pons7c49cb82020-03-16 23:17:32 +01003005 /* Wait 10ns for ranks to settle */
3006 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003007
3008 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3009 MCHBAR32(addr) = reg;
3010
Angel Pons7c49cb82020-03-16 23:17:32 +01003011 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003012 write_reset(ctrl);
3013 }
3014
Angel Pons7c49cb82020-03-16 23:17:32 +01003015 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003016 dram_mrscommands(ctrl);
3017
3018 printram("CP5c\n");
3019
Angel Pons88521882020-01-05 20:21:20 +01003020 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003021
3022 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003023 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003024 udelay(2);
3025 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003026}