blob: 8418ccd97f84e5e3936888e26031a7b39ca1beb4 [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
Angel Pons12bd8ab2020-11-13 23:10:52 +01003#include <assert.h>
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01004#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <console/console.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01006#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010010#include <northbridge/intel/sandybridge/chip.h>
11#include <device/pci_def.h>
12#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020013#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020014
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010015#include "raminit_native.h"
16#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010017#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#include "sandybridge.h"
19
Angel Pons7c49cb82020-03-16 23:17:32 +010020/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010021
22static void sfence(void)
23{
24 asm volatile ("sfence");
25}
26
Angel Pons7c49cb82020-03-16 23:17:32 +010027/* Toggle IO reset bit */
28static void toggle_io_reset(void)
29{
Angel Pons88521882020-01-05 20:21:20 +010030 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010031 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010033 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010034 udelay(1);
35}
36
37static u32 get_XOVER_CLK(u8 rankmap)
38{
39 return rankmap << 24;
40}
41
42static u32 get_XOVER_CMD(u8 rankmap)
43{
44 u32 reg;
45
Angel Pons7c49cb82020-03-16 23:17:32 +010046 /* Enable xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010047 reg = 0x4000;
48
Angel Pons7c49cb82020-03-16 23:17:32 +010049 /* Enable xover ctl */
50 if (rankmap & 0x03)
51 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010052
Angel Pons7c49cb82020-03-16 23:17:32 +010053 if (rankmap & 0x0c)
54 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010055
56 return reg;
57}
58
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010059void dram_find_common_params(ramctr_timing *ctrl)
60{
61 size_t valid_dimms;
62 int channel, slot;
63 dimm_info *dimms = &ctrl->info;
64
65 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
66 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010067
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010068 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010069
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010070 const dimm_attr *dimm = &dimms->dimm[channel][slot];
71 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
72 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010073
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010074 valid_dimms++;
75
76 /* Find all possible CAS combinations */
77 ctrl->cas_supported &= dimm->cas_supported;
78
79 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010080 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
81 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
82 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
84 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010085 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010086 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
87 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
88 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
89 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
90 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030091 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
92 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010093 }
94
95 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010096 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
97
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010098 if (!valid_dimms)
99 die("No valid DIMMs found");
100}
101
Angel Pons88521882020-01-05 20:21:20 +0100102void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100103{
104 u32 reg;
105 int channel;
106
107 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100108 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100110 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
111 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100112
Angel Pons7c49cb82020-03-16 23:17:32 +0100113 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100115 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
116 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100117 }
118}
119
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100120static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100121{
Angel Pons89ae6b82020-03-21 13:23:32 +0100122 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100123
124 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 /*
126 * ODT stretch:
127 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
128 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100129 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100130 if (stretch == 2)
131 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100132
Angel Pons88521882020-01-05 20:21:20 +0100133 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100134 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100136 } else {
Angel Pons88521882020-01-05 20:21:20 +0100137 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100138 union tc_othp_reg tc_othp = {
139 .raw = MCHBAR32(addr),
140 };
141 tc_othp.odt_delay_d0 = stretch;
142 tc_othp.odt_delay_d1 = stretch;
143 MCHBAR32(addr) = tc_othp.raw;
Iru Cai89af71c2018-08-16 16:46:27 +0800144 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100145 }
146}
147
148void dram_timing_regs(ramctr_timing *ctrl)
149{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100150 int channel;
151
Angel Pons81378062020-11-12 13:46:21 +0100152 /* BIN parameters */
153 const union tc_dbp_reg tc_dbp = {
154 .tRCD = ctrl->tRCD,
155 .tRP = ctrl->tRP,
156 .tAA = ctrl->CAS,
157 .tCWL = ctrl->CWL,
158 .tRAS = ctrl->tRAS,
159 };
160
161 /* Regular access parameters */
162 const union tc_rap_reg tc_rap = {
163 .tRRD = ctrl->tRRD,
164 .tRTP = ctrl->tRTP,
165 .tCKE = ctrl->tCKE,
166 .tWTR = ctrl->tWTR,
167 .tFAW = ctrl->tFAW,
168 .tWR = ctrl->tWR,
169 .tCMD = 3,
170 };
171
172 /* Other parameters */
173 const union tc_othp_reg tc_othp = {
174 .tXPDLL = ctrl->tXPDLL,
175 .tXP = ctrl->tXP,
176 .tAONPD = ctrl->tAONPD,
177 .tCPDED = 2,
Angel Pons2ad03a42020-11-19 11:07:27 +0100178 .tPRPDEN = 1,
Angel Pons81378062020-11-12 13:46:21 +0100179 };
180
181 /*
182 * If tXP and tXPDLL are very high, we need to increase them by one.
183 * This can only happen on Ivy Bridge, and when overclocking the RAM.
184 */
185 const union tc_dtp_reg tc_dtp = {
186 .overclock_tXP = ctrl->tXP >= 8,
187 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
188 };
189
190 /*
191 * TC-Refresh timing parameters:
192 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
193 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
194 */
195 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
196
197 const union tc_rftp_reg tc_rftp = {
198 .tREFI = ctrl->tREFI,
199 .tRFC = ctrl->tRFC,
200 .tREFIx9 = val32 / 1024,
201 };
202
203 /* Self-refresh timing parameters */
204 const union tc_srftp_reg tc_srftp = {
205 .tXSDLL = tDLLK,
206 .tXS_offset = ctrl->tXSOffset,
207 .tZQOPER = tDLLK - ctrl->tXSOffset,
208 .tMOD = ctrl->tMOD - 8,
209 };
210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100211 FOR_ALL_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +0100212 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
213 MCHBAR32(TC_DBP_ch(channel)) = tc_dbp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100214
Angel Pons7a612742020-11-12 13:34:03 +0100215 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
216 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100217
Angel Pons7a612742020-11-12 13:34:03 +0100218 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
219 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100220
Angel Ponsca2f68a2020-03-22 13:15:12 +0100221 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Pons81378062020-11-12 13:46:21 +0100222 /* Debug parameters - only applies to Ivy Bridge */
Angel Pons7a612742020-11-12 13:34:03 +0100223 MCHBAR32(TC_DTP_ch(channel)) = tc_dtp.raw;
Angel Ponsca2f68a2020-03-22 13:15:12 +0100224 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100225
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100226 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100227
Angel Pons7a612742020-11-12 13:34:03 +0100228 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
229 MCHBAR32(TC_RFTP_ch(channel)) = tc_rftp.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +0100230
Angel Pons7a612742020-11-12 13:34:03 +0100231 union tc_rfp_reg tc_rfp = {
232 .raw = MCHBAR32(TC_RFP_ch(channel)),
233 };
234 tc_rfp.oref_ri = 0xff;
235 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100236
Angel Pons7a612742020-11-12 13:34:03 +0100237 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
238 MCHBAR32(TC_SRFTP_ch(channel)) = tc_srftp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100239 }
240}
241
242void dram_dimm_mapping(ramctr_timing *ctrl)
243{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100244 int channel;
245 dimm_info *info = &ctrl->info;
246
247 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200248 dimm_attr *dimmA, *dimmB;
249 u32 reg = 0;
250
Angel Pons7c49cb82020-03-16 23:17:32 +0100251 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100252 dimmA = &info->dimm[channel][0];
253 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100254 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100255 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100256 dimmA = &info->dimm[channel][1];
257 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100258 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100259 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100260
Nico Huberac4f2162017-10-01 18:14:43 +0200261 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100262 reg |= (dimmA->size_mb / 256) << 0;
263 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200264 reg |= (dimmA->width / 8 - 1) << 19;
265 }
266
267 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100268 reg |= (dimmB->size_mb / 256) << 8;
269 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200270 reg |= (dimmB->width / 8 - 1) << 20;
271 }
272
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200273 /*
274 * Rank interleave: Bit 16 of the physical address space sets
275 * the rank to use in a dual single rank DIMM configuration.
276 * That results in every 64KiB being interleaved between two ranks.
277 */
278 reg |= 1 << 21;
279 /* Enhanced interleave */
280 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200281
Angel Pons7c49cb82020-03-16 23:17:32 +0100282 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100283 ctrl->mad_dimm[channel] = reg;
284 } else {
285 ctrl->mad_dimm[channel] = 0;
286 }
287 }
288}
289
Patrick Rudolphdd662872017-10-28 18:20:11 +0200290void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100291{
292 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200293 u32 ecc;
294
295 if (ctrl->ecc_enabled)
296 ecc = training ? (1 << 24) : (3 << 24);
297 else
298 ecc = 0;
299
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100300 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200301 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100302 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200303
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200304 if (ctrl->ecc_enabled)
305 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100306}
307
Angel Pons88521882020-01-05 20:21:20 +0100308void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100309{
310 u32 reg, ch0size, ch1size;
311 u8 val;
312 reg = 0;
313 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100314
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100315 if (training) {
316 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
317 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
318 } else {
319 ch0size = ctrl->channel_size_mb[0];
320 ch1size = ctrl->channel_size_mb[1];
321 }
322
323 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100324 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100325 val = ch1size / 256;
326 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100327 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100328 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100329 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100330
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100331 } else {
Angel Pons88521882020-01-05 20:21:20 +0100332 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100333 val = ch0size / 256;
334 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100335 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100336 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100337 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100338 }
339}
340
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100341#define DEFAULT_PCI_MMIO_SIZE 2048
342
343static unsigned int get_mmio_size(void)
344{
345 const struct device *dev;
346 const struct northbridge_intel_sandybridge_config *cfg = NULL;
347
Angel Ponsb31d1d72020-01-10 01:35:09 +0100348 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100349 if (dev)
350 cfg = dev->chip_info;
351
352 /* If this is zero, it just means devicetree.cb didn't set it */
353 if (!cfg || cfg->pci_mmio_size == 0)
354 return DEFAULT_PCI_MMIO_SIZE;
355 else
356 return cfg->pci_mmio_size;
357}
358
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200359/*
360 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
361 * The ME/PCU/.. has the ability to change this.
362 * Return 0: ECC is optional
363 * Return 1: ECC is forced
364 */
365bool get_host_ecc_forced(void)
366{
367 /* read Capabilities A Register */
368 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
369 return !!(reg32 & (1 << 24));
370}
371
372/*
373 * Returns the ECC capability.
374 * The ME/PCU/.. has the ability to change this.
375 * Return 0: ECC is disabled
376 * Return 1: ECC is possible
377 */
378bool get_host_ecc_cap(void)
379{
380 /* read Capabilities A Register */
381 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
382 return !(reg32 & (1 << 25));
383}
384
Angel Pons88521882020-01-05 20:21:20 +0100385void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100386{
Angel Pons7c49cb82020-03-16 23:17:32 +0100387 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
388 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
389 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100390 uint16_t ggc;
391
392 mmiosize = get_mmio_size();
393
Felix Held87ddea22020-01-26 04:55:27 +0100394 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100395 if (!(ggc & 2)) {
396 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100397 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100398 } else {
399 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100400 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100401 }
402
403 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
404
405 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
406
407 mestolenbase = tom - me_uma_size;
408
Angel Pons7c49cb82020-03-16 23:17:32 +0100409 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
410
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100411 gfxstolenbase = toludbase - gfxstolen;
412 gttbase = gfxstolenbase - gttsize;
413
414 tsegbase = gttbase - tsegsize;
415
Angel Pons7c49cb82020-03-16 23:17:32 +0100416 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100417 tsegbasedelta = tsegbase & (tsegsize - 1);
418 tsegbase &= ~(tsegsize - 1);
419
420 gttbase -= tsegbasedelta;
421 gfxstolenbase -= tsegbasedelta;
422 toludbase -= tsegbasedelta;
423
Angel Pons7c49cb82020-03-16 23:17:32 +0100424 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100425 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100426 /* Reclaim is possible */
427 reclaim = 1;
428 remapbase = MAX(4096, tom - me_uma_size);
429 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
430 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100431 } else {
432 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100433 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100434 touudbase = tom - me_uma_size;
435 }
436
Angel Pons7c49cb82020-03-16 23:17:32 +0100437 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100438 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
439
Angel Pons7c49cb82020-03-16 23:17:32 +0100440 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442 val = tom & 0xfff;
443 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100444 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100445 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100446
Angel Ponsb31d1d72020-01-10 01:35:09 +0100447 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100448 val = tom & 0xfffff000;
449 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100450 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100451 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100452
Angel Pons7c49cb82020-03-16 23:17:32 +0100453 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100454 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100455 val = toludbase & 0xfff;
456 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100457 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100458 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100459
Angel Pons7c49cb82020-03-16 23:17:32 +0100460 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100461 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100462 val = touudbase & 0xfff;
463 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100464 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100465 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100466
Angel Pons7c49cb82020-03-16 23:17:32 +0100467 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100468 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100469 val = touudbase & 0xfffff000;
470 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100471 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100472 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100473
474 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100475 /* REMAP BASE */
476 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100477 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100478
Angel Pons7c49cb82020-03-16 23:17:32 +0100479 /* REMAP LIMIT */
480 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100481 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100482 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100483 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100484 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100485 val = tsegbase & 0xfff;
486 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100487 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100488 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100489
Angel Pons7c49cb82020-03-16 23:17:32 +0100490 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100491 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100492 val = gfxstolenbase & 0xfff;
493 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100494 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100495 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100496
Angel Pons7c49cb82020-03-16 23:17:32 +0100497 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100498 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100499 val = gttbase & 0xfff;
500 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100501 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100502 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100503
504 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100505 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100506 val = (0x80000 - me_uma_size) & 0xfffff000;
507 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100508 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100509 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100510
Angel Pons7c49cb82020-03-16 23:17:32 +0100511 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513 val = mestolenbase & 0xfff;
514 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100515 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100516 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100517
Angel Ponsb31d1d72020-01-10 01:35:09 +0100518 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100519 val = mestolenbase & 0xfffff000;
520 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100521 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100522 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100523
Angel Pons7c49cb82020-03-16 23:17:32 +0100524 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100525 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526 val = (0x80000 - me_uma_size) & 0xfff;
527 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100528 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
529 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100530 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100531 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100532 }
533}
534
Angel Pons88521882020-01-05 20:21:20 +0100535static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100536{
537 int channel, slotrank;
538
Angel Pons7c49cb82020-03-16 23:17:32 +0100539 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100540 channel = (ctrl->rankmap[0]) ? 0 : 1;
541
Angel Pons88521882020-01-05 20:21:20 +0100542 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100543
Angel Pons7c49cb82020-03-16 23:17:32 +0100544 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100545 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
546
Angel Ponsffd50152020-11-12 11:03:10 +0100547 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100548
Angel Pons7c49cb82020-03-16 23:17:32 +0100549 /*
550 * Execute command queue - why is bit 22 set here?!
551 *
552 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
553 */
Angel Pons38d901e2020-05-02 23:50:43 +0200554 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200555
Angel Pons88521882020-01-05 20:21:20 +0100556 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100557}
558
Angel Pons88521882020-01-05 20:21:20 +0100559void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560{
Felix Held9fe248f2018-07-31 20:59:45 +0200561 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100562 int channel;
563
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
565 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566 do {
Angel Pons88521882020-01-05 20:21:20 +0100567 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100568 } while ((reg & 0x14) == 0);
569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100572 MCHBAR32(MC_INIT_STATE_G) = reg;
573 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100574 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100575 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100576
Angel Pons7c49cb82020-03-16 23:17:32 +0100577 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100578 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100579
Angel Pons7c49cb82020-03-16 23:17:32 +0100580 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581 udelay(200);
582
Angel Pons7c49cb82020-03-16 23:17:32 +0100583 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100584 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100585
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100587 udelay(500);
588
Angel Pons7c49cb82020-03-16 23:17:32 +0100589 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100590 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100591
Angel Pons7c49cb82020-03-16 23:17:32 +0100592 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100593 udelay(1);
594
595 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200597 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100598 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100599
Angel Pons7c49cb82020-03-16 23:17:32 +0100600 /* Wait 10ns for ranks to settle */
601 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100602
603 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100604 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100605
Angel Pons7c49cb82020-03-16 23:17:32 +0100606 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100607 write_reset(ctrl);
608 }
609}
610
Angel Pons3d3bf482020-11-14 16:18:15 +0100611/*
612 * DDR3 Rank1 Address mirror swap the following pins:
613 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
614 */
615static void ddr3_mirror_mrreg(int *bank, u32 *addr)
616{
617 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
618 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
619}
620
Angel Pons7c49cb82020-03-16 23:17:32 +0100621static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100622{
Angel Pons88521882020-01-05 20:21:20 +0100623 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100624
Angel Pons3d3bf482020-11-14 16:18:15 +0100625 if (ctrl->rank_mirror[channel][slotrank])
626 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100627
Angel Pons8f0757e2020-11-11 23:03:36 +0100628 const struct iosav_ssq sequence[] = {
629 /* DRAM command MRS */
630 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200631 .sp_cmd_ctrl = {
632 .command = IOSAV_MRS,
633 },
634 .subseq_ctrl = {
635 .cmd_executions = 1,
636 .cmd_delay_gap = 4,
637 .post_ssq_wait = 4,
638 .data_direction = SSQ_NA,
639 },
640 .sp_cmd_addr = {
641 .address = val,
642 .rowbits = 6,
643 .bank = reg,
644 .rank = slotrank,
645 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100646 },
647 /* DRAM command MRS */
648 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200649 .sp_cmd_ctrl = {
650 .command = IOSAV_MRS,
651 .ranksel_ap = 1,
652 },
653 .subseq_ctrl = {
654 .cmd_executions = 1,
655 .cmd_delay_gap = 4,
656 .post_ssq_wait = 4,
657 .data_direction = SSQ_NA,
658 },
659 .sp_cmd_addr = {
660 .address = val,
661 .rowbits = 6,
662 .bank = reg,
663 .rank = slotrank,
664 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100665 },
666 /* DRAM command MRS */
667 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200668 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100669 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200670 },
671 .subseq_ctrl = {
672 .cmd_executions = 1,
673 .cmd_delay_gap = 4,
674 .post_ssq_wait = ctrl->tMOD,
675 .data_direction = SSQ_NA,
676 },
677 .sp_cmd_addr = {
678 .address = val,
679 .rowbits = 6,
680 .bank = reg,
681 .rank = slotrank,
682 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100683 },
684 };
685 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200686
Angel Pons7c49cb82020-03-16 23:17:32 +0100687 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200688 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100689}
690
Angel Pons09fc4b92020-11-19 12:02:07 +0100691/* Obtain optimal power down mode for current configuration */
692static enum pdwm_mode get_power_down_mode(ramctr_timing *ctrl)
693{
694 if (ctrl->tXP > 8)
695 return PDM_NONE;
696
697 if (ctrl->tXPDLL > 32)
698 return PDM_PPD;
699
700 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
701 return PDM_DLL_OFF;
702
703 return PDM_APD_PPD;
704}
705
Angel Pons88521882020-01-05 20:21:20 +0100706static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100707{
708 u16 mr0reg, mch_cas, mch_wr;
709 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Angel Pons09fc4b92020-11-19 12:02:07 +0100710
711 const enum pdwm_mode power_down = get_power_down_mode(ctrl);
712
713 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100714
Angel Pons7c49cb82020-03-16 23:17:32 +0100715 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100716 if (ctrl->CAS < 12) {
717 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
718 } else {
719 mch_cas = (u16) (ctrl->CAS - 12);
720 mch_cas = ((mch_cas << 1) | 0x1);
721 }
722
Angel Pons7c49cb82020-03-16 23:17:32 +0100723 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100724 mch_wr = mch_wr_t[ctrl->tWR - 5];
725
Angel Pons2bf28ed2020-11-12 13:49:59 +0100726 /* DLL Reset - self clearing - set after CLK frequency has been changed */
727 mr0reg = 1 << 8;
728
729 mr0reg |= (mch_cas & 0x1) << 2;
730 mr0reg |= (mch_cas & 0xe) << 3;
731 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100732
Angel Pons09fc4b92020-11-19 12:02:07 +0100733 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
734 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100735 return mr0reg;
736}
737
738static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
739{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200740 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100741}
742
Angel Ponsf9997482020-11-12 16:02:52 +0100743static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100744{
745 /* Get ODT based on rankmap */
746 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
747
748 if (dimms_per_ch == 1) {
749 return (const odtmap){60, 60};
750 } else {
751 return (const odtmap){120, 30};
752 }
753}
754
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100755static u32 encode_odt(u32 odt)
756{
757 switch (odt) {
758 case 30:
759 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
760 case 60:
761 return (1 << 2); // RZQ/4
762 case 120:
763 return (1 << 6); // RZQ/2
764 default:
765 case 0:
766 return 0;
767 }
768}
769
770static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
771{
772 odtmap odt;
773 u32 mr1reg;
774
Angel Ponsf9997482020-11-12 16:02:52 +0100775 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100776 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100777
778 mr1reg |= encode_odt(odt.rttnom);
779
780 return mr1reg;
781}
782
783static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
784{
785 u16 mr1reg;
786
787 mr1reg = make_mr1(ctrl, rank, channel);
788
789 write_mrreg(ctrl, channel, rank, 1, mr1reg);
790}
791
792static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
793{
Angel Pons868bca22020-11-13 13:38:04 +0100794 const u16 pasr = 0;
795 const u16 cwl = ctrl->CWL - 5;
796 const odtmap odt = get_ODT(ctrl, channel);
797
Angel Ponsdca3cb52020-11-13 13:42:07 +0100798 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100799 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
800 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100801
Angel Pons868bca22020-11-13 13:38:04 +0100802 u16 mr2reg = 0;
803 mr2reg |= pasr;
804 mr2reg |= cwl << 3;
805 mr2reg |= ctrl->auto_self_refresh << 6;
806 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100807 mr2reg |= (odt.rttwr / 60) << 9;
808
809 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100810
811 /* Program MR2 shadow */
812 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
813
814 reg32 &= 3 << 14 | 3 << 6;
815
816 reg32 |= mr2reg & ~(3 << 6);
817
Angel Pons927b1c02020-12-10 22:11:27 +0100818 if (srt)
819 reg32 |= 1 << (rank / 2 + 6);
820
821 if (ctrl->rank_mirror[channel][rank])
822 reg32 |= 1 << (rank / 2 + 14);
823
Angel Pons7f1363d2020-11-13 13:31:58 +0100824 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100825}
826
827static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
828{
829 write_mrreg(ctrl, channel, rank, 3, 0);
830}
831
Angel Pons88521882020-01-05 20:21:20 +0100832void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100833{
834 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100835 int channel;
836
837 FOR_ALL_POPULATED_CHANNELS {
838 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100839 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100840 dram_mr2(ctrl, slotrank, channel);
841
Angel Pons7c49cb82020-03-16 23:17:32 +0100842 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100843 dram_mr3(ctrl, slotrank, channel);
844
Angel Pons7c49cb82020-03-16 23:17:32 +0100845 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100846 dram_mr1(ctrl, slotrank, channel);
847
Angel Pons7c49cb82020-03-16 23:17:32 +0100848 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100849 dram_mr0(ctrl, slotrank, channel);
850 }
851 }
852
Angel Pons8f0757e2020-11-11 23:03:36 +0100853 const struct iosav_ssq zqcl_sequence[] = {
854 /* DRAM command NOP (without ODT nor chip selects) */
855 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200856 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100857 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200858 },
859 .subseq_ctrl = {
860 .cmd_executions = 1,
861 .cmd_delay_gap = 4,
862 .post_ssq_wait = 15,
863 .data_direction = SSQ_NA,
864 },
865 .sp_cmd_addr = {
866 .address = 2,
867 .rowbits = 6,
868 .bank = 0,
869 .rank = 0,
870 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100871 },
872 /* DRAM command ZQCL */
873 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200874 .sp_cmd_ctrl = {
875 .command = IOSAV_ZQCS,
876 .ranksel_ap = 1,
877 },
878 .subseq_ctrl = {
879 .cmd_executions = 1,
880 .cmd_delay_gap = 4,
881 .post_ssq_wait = 400,
882 .data_direction = SSQ_NA,
883 },
884 .sp_cmd_addr = {
885 .address = 1024,
886 .rowbits = 6,
887 .bank = 0,
888 .rank = 0,
889 },
890 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100891 .inc_rank = 1,
892 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200893 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100894 },
895 };
896 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100897
Angel Pons7c49cb82020-03-16 23:17:32 +0100898 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200899 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100900
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100902 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100903 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100904 }
905
Angel Pons7c49cb82020-03-16 23:17:32 +0100906 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100907 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100908
909 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100910 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100911
Angel Pons88521882020-01-05 20:21:20 +0100912 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100913
914 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
915
Angel Pons7c49cb82020-03-16 23:17:32 +0100916 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100917 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100918
Angel Ponsffd50152020-11-12 11:03:10 +0100919 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200920
Angel Pons7c49cb82020-03-16 23:17:32 +0100921 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200922 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100923
Angel Pons7c49cb82020-03-16 23:17:32 +0100924 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100925 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100926 }
927}
928
Felix Held3b906032020-01-14 17:05:43 +0100929static const u32 lane_base[] = {
930 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
931 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
932 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100933};
934
Angel Pons88521882020-01-05 20:21:20 +0100935void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100936{
Angel Pons7584e552020-11-19 21:34:32 +0100937 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100938 int lane;
939 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940
Angel Pons7584e552020-11-19 21:34:32 +0100941 u32 ctl_delay[NUM_SLOTS] = { 0 };
942 int cmd_delay = 0;
943
944 /* Enable CLK XOVER */
945 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
946 u32 clk_logic_dly = 0;
947
948 /*
949 * Apply command delay if desired setting is negative. Find the
950 * most negative value: 'cmd_delay' will be the absolute value.
951 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100952 FOR_ALL_POPULATED_RANKS {
Angel Pons7584e552020-11-19 21:34:32 +0100953 if (cmd_delay < -ctrl->timings[channel][slotrank].pi_coding)
954 cmd_delay = -ctrl->timings[channel][slotrank].pi_coding;
955 }
956 if (cmd_delay < 0) {
957 printk(BIOS_ERR, "C%d command delay underflow: %d\n", channel, cmd_delay);
958 cmd_delay = 0;
959 }
960 if (cmd_delay >= 128) {
961 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
962 cmd_delay = 127;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100963 }
964
Angel Pons7584e552020-11-19 21:34:32 +0100965 /* Apply control and clock delay if desired setting is positive */
966 if (cmd_delay == 0) {
967 for (slot = 0; slot < NUM_SLOTS; slot++) {
968 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
969 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
970
971 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
972
973 if (slot_map & 1)
974 ctl_delay[slot] += pi_coding_0 + cmd_delay;
975
976 if (slot_map & 2)
977 ctl_delay[slot] += pi_coding_1 + cmd_delay;
978
979 /* If both ranks in a slot are populated, use the average */
980 if (slot_map == 3)
981 ctl_delay[slot] /= 2;
982
983 if (ctl_delay[slot] >= 128) {
984 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
985 channel, slot, ctl_delay[slot]);
986 ctl_delay[slot] = 127;
987 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100988 }
Angel Pons7584e552020-11-19 21:34:32 +0100989 FOR_ALL_POPULATED_RANKS {
990 u32 clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
991
992 if (clk_delay >= 128) {
993 printk(BIOS_ERR, "C%dR%d clock delay overflow: %d\n",
994 channel, slotrank, clk_delay);
995 clk_delay = 127;
996 }
997
998 clk_pi_coding |= (clk_delay % 64) << (6 * slotrank);
999 clk_logic_dly |= (clk_delay / 64) << slotrank;
1000 }
1001 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001002
Angel Pons7c49cb82020-03-16 23:17:32 +01001003 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +01001004 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
1005 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
1006 };
Angel Pons7584e552020-11-19 21:34:32 +01001007 cmd_pi_coding.cmd_pi_code = cmd_delay % 64;
1008 cmd_pi_coding.cmd_logic_delay = cmd_delay / 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001009
Angel Pons7584e552020-11-19 21:34:32 +01001010 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % 64;
1011 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % 64;
1012 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / 64;
1013 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / 64;
Angel Pons737f1112020-11-13 14:07:30 +01001014
1015 MCHBAR32(GDCRCMDPICODING_ch(channel)) = cmd_pi_coding.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001016
Angel Pons7584e552020-11-19 21:34:32 +01001017 MCHBAR32(GDCRCKPICODE_ch(channel)) = clk_pi_coding;
1018 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = clk_logic_dly;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001019
Angel Pons88521882020-01-05 20:21:20 +01001020 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001021 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001022
Angel Pons88521882020-01-05 20:21:20 +01001023 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001024
1025 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001026 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001027
Angel Pons88521882020-01-05 20:21:20 +01001028 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001029 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001030
1031 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001032 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Angel Pons075d1232020-11-19 21:50:33 +01001033 ((ctrl->timings[channel][slotrank].lanes[lane].timA & 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001034 |
Angel Pons075d1232020-11-19 21:50:33 +01001035 (ctrl->timings[channel][slotrank].lanes[lane].rising << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001036 |
Angel Pons075d1232020-11-19 21:50:33 +01001037 ((ctrl->timings[channel][slotrank].lanes[lane].timA & 0x1c0) << 10)
1038 |
1039 (ctrl->timings[channel][slotrank].lanes[lane].falling << 20));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001040
Felix Heldfb19c8a2020-01-14 21:27:59 +01001041 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Angel Pons075d1232020-11-19 21:50:33 +01001042 ((ctrl->timings[channel][slotrank].lanes[lane].timC & 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001043 |
Angel Pons075d1232020-11-19 21:50:33 +01001044 ((ctrl->timings[channel][slotrank].lanes[lane].timB & 0x3f) << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001045 |
Angel Pons075d1232020-11-19 21:50:33 +01001046 ((ctrl->timings[channel][slotrank].lanes[lane].timB & 0x1c0) << 9)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001047 |
Angel Pons075d1232020-11-19 21:50:33 +01001048 ((ctrl->timings[channel][slotrank].lanes[lane].timC & 0x40) << 13));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001049 }
1050 }
Angel Pons88521882020-01-05 20:21:20 +01001051 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1052 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053}
1054
Angel Pons88521882020-01-05 20:21:20 +01001055static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001056{
Angel Pons88521882020-01-05 20:21:20 +01001057 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001058
Angel Ponsffd50152020-11-12 11:03:10 +01001059 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001060
Angel Pons7c49cb82020-03-16 23:17:32 +01001061 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001062 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001063
Angel Pons88521882020-01-05 20:21:20 +01001064 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001065}
1066
Angel Pons7c49cb82020-03-16 23:17:32 +01001067static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001068{
1069 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001070
1071 return (MCHBAR32(lane_base[lane] +
1072 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001073}
1074
1075struct run {
1076 int middle;
1077 int end;
1078 int start;
1079 int all;
1080 int length;
1081};
1082
1083static struct run get_longest_zero_run(int *seq, int sz)
1084{
1085 int i, ls;
1086 int bl = 0, bs = 0;
1087 struct run ret;
1088
1089 ls = 0;
1090 for (i = 0; i < 2 * sz; i++)
1091 if (seq[i % sz]) {
1092 if (i - ls > bl) {
1093 bl = i - ls;
1094 bs = ls;
1095 }
1096 ls = i + 1;
1097 }
1098 if (bl == 0) {
1099 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001100 ret.start = 0;
1101 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001102 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001103 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001104 return ret;
1105 }
1106
Angel Pons7c49cb82020-03-16 23:17:32 +01001107 ret.start = bs % sz;
1108 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001109 ret.middle = (bs + (bl - 1) / 2) % sz;
1110 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001111 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001112
1113 return ret;
1114}
1115
Angel Ponsf3053392020-11-13 23:31:12 +01001116static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001117{
1118 int timA;
1119 int statistics[NUM_LANES][128];
1120 int lane;
1121
1122 for (timA = 0; timA < 128; timA++) {
1123 FOR_ALL_LANES {
1124 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1125 }
1126 program_timings(ctrl, channel);
1127
1128 test_timA(ctrl, channel, slotrank);
1129
1130 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001131 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001132 }
1133 }
1134 FOR_ALL_LANES {
1135 struct run rn = get_longest_zero_run(statistics[lane], 128);
1136 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1137 upperA[lane] = rn.end;
1138 if (upperA[lane] < rn.middle)
1139 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001140
Patrick Rudolph368b6152016-11-25 16:36:52 +01001141 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001142 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001143 }
1144}
1145
Angel Ponsf3053392020-11-13 23:31:12 +01001146static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001147{
1148 int timA_delta;
1149 int statistics[NUM_LANES][51];
1150 int lane, i;
1151
1152 memset(statistics, 0, sizeof(statistics));
1153
1154 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001155
1156 FOR_ALL_LANES {
1157 ctrl->timings[channel][slotrank].lanes[lane].timA
1158 = upperA[lane] + timA_delta + 0x40;
1159 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001160 program_timings(ctrl, channel);
1161
1162 for (i = 0; i < 100; i++) {
1163 test_timA(ctrl, channel, slotrank);
1164 FOR_ALL_LANES {
1165 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001166 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001167 }
1168 }
1169 }
1170 FOR_ALL_LANES {
1171 int last_zero, first_all;
1172
1173 for (last_zero = -25; last_zero <= 25; last_zero++)
1174 if (statistics[lane][last_zero + 25])
1175 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001176
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001177 last_zero--;
1178 for (first_all = -25; first_all <= 25; first_all++)
1179 if (statistics[lane][first_all + 25] == 100)
1180 break;
1181
Angel Pons7c49cb82020-03-16 23:17:32 +01001182 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001183
1184 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001185 (last_zero + first_all) / 2 + upperA[lane];
1186
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001187 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001188 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001189 }
1190}
1191
Angel Ponsf3053392020-11-13 23:31:12 +01001192static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193{
1194 int works[NUM_LANES];
1195 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001196
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001197 while (1) {
1198 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001199
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001200 program_timings(ctrl, channel);
1201 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001204 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1205
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001206 if (works[lane])
1207 some_works = 1;
1208 else
1209 all_works = 0;
1210 }
1211 if (all_works)
1212 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001213
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001215 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001216 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1217 channel, slotrank);
1218 return MAKE_ERR;
1219 }
Angel Pons88521882020-01-05 20:21:20 +01001220 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001221 printram("4024 -= 2;\n");
1222 continue;
1223 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001224 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001225 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001226
Felix Heldef4fe3e2019-12-31 14:15:05 +01001227 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001228 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1229 channel, slotrank);
1230 return MAKE_ERR;
1231 }
1232 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001233 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001234 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001235 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001236 }
1237 }
1238 return 0;
1239}
1240
Angel Pons12bd8ab2020-11-13 23:10:52 +01001241static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242{
1243 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001244 u16 logic_delay_min = 7;
1245 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001246
1247 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001248 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1249
1250 logic_delay_min = MIN(logic_delay_min, logic_delay);
1251 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001252 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001253
1254 if (logic_delay_max < logic_delay_min) {
1255 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1256 logic_delay_max, logic_delay_min, channel, slotrank);
1257 }
1258
1259 assert(logic_delay_max >= logic_delay_min);
1260
1261 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001262}
1263
Angel Pons12bd8ab2020-11-13 23:10:52 +01001264static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001265{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001266 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001267
Angel Pons7c49cb82020-03-16 23:17:32 +01001268 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001269 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270
Angel Pons12bd8ab2020-11-13 23:10:52 +01001271 if (prev < post)
1272 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001273
Angel Pons12bd8ab2020-11-13 23:10:52 +01001274 else if (prev > post)
1275 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001276
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001277 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001278 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001279
Angel Pons12bd8ab2020-11-13 23:10:52 +01001280 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1281 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1282 printram("4024 += %d;\n", latency_offset);
1283 printram("4028 += %d;\n", latency_offset);
1284
1285 return post;
1286}
1287
1288static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1289{
1290 u16 logic_delay_min = 7;
1291 int lane;
1292
1293 FOR_ALL_LANES {
1294 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1295
1296 logic_delay_min = MIN(logic_delay_min, logic_delay);
1297 }
1298
1299 if (logic_delay_min >= 2) {
1300 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1301 logic_delay_min, channel, slotrank);
1302 }
1303
1304 FOR_ALL_LANES {
1305 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1306 }
1307 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1308 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001309}
1310
Angel Pons7f5a97c2020-11-13 16:58:46 +01001311int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001312{
1313 int channel, slotrank, lane;
1314 int err;
1315
1316 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1317 int all_high, some_high;
1318 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001319 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001320
Angel Pons88521882020-01-05 20:21:20 +01001321 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001322
Angel Ponsffd50152020-11-12 11:03:10 +01001323 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001324
Angel Pons7c49cb82020-03-16 23:17:32 +01001325 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001326 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001327
Angel Pons58b609b2020-11-13 14:35:29 +01001328 const union gdcr_training_mod_reg training_mod = {
1329 .receive_enable_mode = 1,
1330 .training_rank_sel = slotrank,
1331 .odt_always_on = 1,
1332 };
1333 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001334
Felix Heldef4fe3e2019-12-31 14:15:05 +01001335 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001336 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001337 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001338
Angel Ponsf3053392020-11-13 23:31:12 +01001339 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001340
Felix Held2bb3cdf2018-07-28 00:23:59 +02001341 all_high = 1;
1342 some_high = 0;
1343 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001344 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001345 some_high = 1;
1346 else
1347 all_high = 0;
1348 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001349
1350 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001351 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001352 printram("4028--;\n");
1353 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001354 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001355 upperA[lane] -= 0x40;
1356
1357 }
1358 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001359 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001360 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001361 printram("4024++;\n");
1362 printram("4028++;\n");
1363 }
1364
1365 program_timings(ctrl, channel);
1366
Angel Pons12bd8ab2020-11-13 23:10:52 +01001367 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001368
Angel Ponsf3053392020-11-13 23:31:12 +01001369 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001370 if (err)
1371 return err;
1372
Angel Pons12bd8ab2020-11-13 23:10:52 +01001373 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001374
Angel Ponsf3053392020-11-13 23:31:12 +01001375 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001376
Angel Pons12bd8ab2020-11-13 23:10:52 +01001377 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001378
Angel Pons12bd8ab2020-11-13 23:10:52 +01001379 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380
Angel Pons12bd8ab2020-11-13 23:10:52 +01001381 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
1383 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001384 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001385 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386
1387 printram("final results:\n");
1388 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001389 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001390 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001391
Angel Pons88521882020-01-05 20:21:20 +01001392 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001393
1394 toggle_io_reset();
1395 }
1396
1397 FOR_ALL_POPULATED_CHANNELS {
1398 program_timings(ctrl, channel);
1399 }
Angel Ponsc6742232020-11-15 13:26:21 +01001400
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001401 return 0;
1402}
1403
Angel Pons011661c2020-11-15 18:21:35 +01001404static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001405{
1406 int lane;
1407
1408 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001409 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1410 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001411 }
1412
Angel Pons88521882020-01-05 20:21:20 +01001413 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001414
Angel Ponsffd50152020-11-12 11:03:10 +01001415 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1416 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001417
Angel Pons7c49cb82020-03-16 23:17:32 +01001418 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001419 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001420
Angel Pons88521882020-01-05 20:21:20 +01001421 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001422
Angel Pons801a5cb2020-11-15 15:48:29 +01001423 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001424
Angel Pons7c49cb82020-03-16 23:17:32 +01001425 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001426 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001427
Angel Pons88521882020-01-05 20:21:20 +01001428 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001429}
1430
Angel Pons011661c2020-11-15 18:21:35 +01001431static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001432{
1433 int min = data[0];
1434 int max = min;
1435 int i;
1436 for (i = 1; i < count; i++) {
1437 if (min > data[i])
1438 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001439
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001440 if (max < data[i])
1441 max = data[i];
1442 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001443 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001444 for (i = 0; i < count; i++)
1445 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001446
Angel Pons891f2bc2020-01-10 01:27:28 +01001447 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001448}
1449
Angel Pons011661c2020-11-15 18:21:35 +01001450static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001451{
Angel Pons011661c2020-11-15 18:21:35 +01001452 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001453 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001454 int lane;
1455
Angel Pons88521882020-01-05 20:21:20 +01001456 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001457
Angel Ponsffd50152020-11-12 11:03:10 +01001458 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001459
Angel Pons7c49cb82020-03-16 23:17:32 +01001460 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001461 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001462
Angel Pons011661c2020-11-15 18:21:35 +01001463 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1464 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001465 program_timings(ctrl, channel);
1466
Angel Pons011661c2020-11-15 18:21:35 +01001467 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001468
1469 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001470 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001471 }
1472 }
1473 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001474 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1475
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001476 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001477 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1478 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001479 /*
1480 * With command training not being done yet, the lane can be erroneous.
1481 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001482 */
Angel Pons011661c2020-11-15 18:21:35 +01001483 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001484 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1485
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001486 if (rn.all || rn.length < 8) {
1487 printk(BIOS_EMERG, "timC recovery failed\n");
1488 return MAKE_ERR;
1489 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001490 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001491 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001492 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001493 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001494 }
1495 return 0;
1496}
1497
Angel Pons88521882020-01-05 20:21:20 +01001498static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001499{
1500 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001501
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001502 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1503 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001504
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001505 return ret;
1506}
1507
Angel Pons765d4652020-11-11 14:44:35 +01001508/* Each cacheline is 64 bits long */
1509static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1510{
1511 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1512}
1513
Angel Pons88521882020-01-05 20:21:20 +01001514static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001515{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301516 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001517 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001518
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001519 for (j = 0; j < 16; j++)
1520 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001521
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001522 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001523
1524 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001525}
1526
Angel Pons88521882020-01-05 20:21:20 +01001527static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001528{
1529 int ret = 0;
1530 int channel;
1531 FOR_ALL_POPULATED_CHANNELS ret++;
1532 return ret;
1533}
1534
Angel Pons88521882020-01-05 20:21:20 +01001535static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001536{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301537 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001538 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301539 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001540
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001541 for (j = 0; j < 16; j++)
1542 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001543
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001544 for (j = 0; j < 16; j++)
1545 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001546
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001547 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001548
1549 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001550}
1551
Angel Pons820bce72020-11-14 17:02:55 +01001552static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001553{
1554 int timB;
1555 int statistics[NUM_LANES][128];
1556 int lane;
1557
Angel Pons58b609b2020-11-13 14:35:29 +01001558 const union gdcr_training_mod_reg training_mod = {
1559 .write_leveling_mode = 1,
1560 .training_rank_sel = slotrank,
1561 .enable_dqs_wl = 5,
1562 .odt_always_on = 1,
1563 .force_drive_enable = 1,
1564 };
1565 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001566
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001567 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1568 int bank = 1;
1569
1570 if (ctrl->rank_mirror[channel][slotrank])
1571 ddr3_mirror_mrreg(&bank, &mr1reg);
1572
1573 wait_for_iosav(channel);
1574
1575 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1576
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001577 for (timB = 0; timB < 128; timB++) {
1578 FOR_ALL_LANES {
1579 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1580 }
1581 program_timings(ctrl, channel);
1582
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001583 /* Execute command queue */
1584 iosav_run_once(channel);
1585
1586 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001587
1588 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001589 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1590 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1591 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001592 }
1593 }
1594 FOR_ALL_LANES {
1595 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001596 /*
1597 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1598 * of timB by a small value, which might cause the 6-bit value to overflow if
1599 * it's close to 0x3f. Increment the value by a small offset if it's likely
1600 * to overflow, to make sure it won't overflow while running tests and bricks
1601 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001602 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001603 * TODO: find out why some tests (edge write discovery) increment timB.
1604 */
1605 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001606 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001607 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001608 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001609
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001610 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1611 if (rn.all) {
1612 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1613 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001614
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001615 return MAKE_ERR;
1616 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001617 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1618 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 }
1620 return 0;
1621}
1622
Angel Pons820bce72020-11-14 17:02:55 +01001623static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001624{
1625 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001626 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001627 if (val == 0xffffffffffffffffLL)
1628 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001629 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001630 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001631 for (i = 0; i < 8; i++)
1632 if (val << (8 * (7 - i) + 4))
1633 return -i;
1634 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001635 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001636 for (i = 0; i < 8; i++)
1637 if (val >> (8 * (7 - i) + 4))
1638 return i;
1639 }
1640 return 8;
1641}
1642
Angel Ponsbf13ef02020-11-11 18:40:06 +01001643static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001644{
1645 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001646
1647 const union gdcr_training_mod_reg training_mod = {
1648 .dq_dqs_training_res = 1,
1649 };
1650 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1651
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001652 FOR_ALL_POPULATED_CHANNELS {
1653 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001654 }
1655 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1656
Angel Pons765d4652020-11-11 14:44:35 +01001657 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001658 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001659
Angel Pons88521882020-01-05 20:21:20 +01001660 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001661
Angel Ponsffd50152020-11-12 11:03:10 +01001662 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663
Angel Pons7c49cb82020-03-16 23:17:32 +01001664 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001665 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001666
Angel Pons88521882020-01-05 20:21:20 +01001667 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001668
Angel Pons8f0757e2020-11-11 23:03:36 +01001669 const struct iosav_ssq rd_sequence[] = {
1670 /* DRAM command PREA */
1671 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001672 .sp_cmd_ctrl = {
1673 .command = IOSAV_PRE,
1674 .ranksel_ap = 1,
1675 },
1676 .subseq_ctrl = {
1677 .cmd_executions = 1,
1678 .cmd_delay_gap = 3,
1679 .post_ssq_wait = ctrl->tRP,
1680 .data_direction = SSQ_NA,
1681 },
1682 .sp_cmd_addr = {
1683 .address = 1024,
1684 .rowbits = 6,
1685 .bank = 0,
1686 .rank = slotrank,
1687 },
1688 .addr_update = {
1689 .addr_wrap = 18,
1690 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001691 },
1692 /* DRAM command ACT */
1693 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001694 .sp_cmd_ctrl = {
1695 .command = IOSAV_ACT,
1696 .ranksel_ap = 1,
1697 },
1698 .subseq_ctrl = {
1699 .cmd_executions = 1,
1700 .cmd_delay_gap = 3,
1701 .post_ssq_wait = ctrl->tRCD,
1702 .data_direction = SSQ_NA,
1703 },
1704 .sp_cmd_addr = {
1705 .address = 0,
1706 .rowbits = 6,
1707 .bank = 0,
1708 .rank = slotrank,
1709 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001710 },
1711 /* DRAM command RD */
1712 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001713 .sp_cmd_ctrl = {
1714 .command = IOSAV_RD,
1715 .ranksel_ap = 3,
1716 },
1717 .subseq_ctrl = {
1718 .cmd_executions = 1,
1719 .cmd_delay_gap = 3,
1720 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001721 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001722 ctrl->timings[channel][slotrank].io_latency,
1723 .data_direction = SSQ_RD,
1724 },
1725 .sp_cmd_addr = {
1726 .address = 8,
1727 .rowbits = 6,
1728 .bank = 0,
1729 .rank = slotrank,
1730 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001731 },
1732 };
1733 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001734
Angel Pons7c49cb82020-03-16 23:17:32 +01001735 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001736 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001737
Angel Pons88521882020-01-05 20:21:20 +01001738 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001739 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001740 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001741 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001742 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001743
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001744 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1745 ctrl->timings[channel][slotrank].lanes[lane].timB +=
Angel Pons820bce72020-11-14 17:02:55 +01001746 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001747
1748 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001749 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1750 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001751 }
1752 }
Angel Pons88521882020-01-05 20:21:20 +01001753 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001754}
1755
Angel Pons7d115132020-11-14 01:44:44 +01001756static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001757{
Angel Pons7d115132020-11-14 01:44:44 +01001758 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001759
Angel Pons7d115132020-11-14 01:44:44 +01001760 FOR_ALL_POPULATED_CHANNELS {
1761 /* choose an existing rank */
1762 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001763
Angel Pons7d115132020-11-14 01:44:44 +01001764 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001765
Angel Pons7d115132020-11-14 01:44:44 +01001766 /* Execute command queue */
1767 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001768
Angel Pons7d115132020-11-14 01:44:44 +01001769 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001770
Angel Pons7d115132020-11-14 01:44:44 +01001771 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1772 }
1773
1774 /* Refresh disable */
1775 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1776
1777 FOR_ALL_POPULATED_CHANNELS {
1778 /* Execute the same command queue */
1779 iosav_run_once(channel);
1780
1781 wait_for_iosav(channel);
1782 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001783}
1784
Angel Pons7c49cb82020-03-16 23:17:32 +01001785/*
1786 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001787 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001788 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1789 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1790 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1791 * CLK/ADDR/CMD signals have the same routing delay.
1792 *
1793 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1794 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1795 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001796 */
Angel Pons820bce72020-11-14 17:02:55 +01001797static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001798{
Angel Pons820bce72020-11-14 17:02:55 +01001799 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001800
Angel Pons7d115132020-11-14 01:44:44 +01001801 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001802
Angel Pons7c49cb82020-03-16 23:17:32 +01001803 /* Enable write leveling on all ranks
1804 Disable all DQ outputs
1805 Only NOP is allowed in this mode */
1806 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1807 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001808 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001809
Angel Ponsa1f17142020-11-15 12:50:03 +01001810 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001811 const union gdcr_training_mod_reg training_mod = {
1812 .write_leveling_mode = 1,
1813 .enable_dqs_wl = 5,
1814 .odt_always_on = 1,
1815 .force_drive_enable = 1,
1816 };
1817 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001818
1819 toggle_io_reset();
1820
Angel Pons7c49cb82020-03-16 23:17:32 +01001821 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001822 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001823 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001824 if (err)
1825 return err;
1826 }
1827
Angel Pons7c49cb82020-03-16 23:17:32 +01001828 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001829 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001830 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001831
Angel Pons88521882020-01-05 20:21:20 +01001832 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001833
1834 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001835 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836
Angel Pons7c49cb82020-03-16 23:17:32 +01001837 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001838 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001839
1840 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001841 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001842 MCHBAR32(IOSAV_STATUS_ch(channel));
1843 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001844
Angel Ponsffd50152020-11-12 11:03:10 +01001845 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001846
Angel Pons7c49cb82020-03-16 23:17:32 +01001847 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001848 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001849
Angel Pons88521882020-01-05 20:21:20 +01001850 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001851 }
1852
1853 toggle_io_reset();
1854
Angel Pons820bce72020-11-14 17:02:55 +01001855 return 0;
1856}
1857
1858int write_training(ramctr_timing *ctrl)
1859{
Angel Ponsc6742232020-11-15 13:26:21 +01001860 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001861 int err;
1862
1863 FOR_ALL_POPULATED_CHANNELS
1864 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1865
Angel Pons4c76d252020-11-15 13:06:53 +01001866 printram("CPE\n");
1867
Angel Pons820bce72020-11-14 17:02:55 +01001868 err = jedec_write_leveling(ctrl);
1869 if (err)
1870 return err;
1871
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001872 printram("CPF\n");
1873
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001874 FOR_ALL_POPULATED_CHANNELS {
1875 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001876 }
1877
1878 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001879 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001880 if (err)
1881 return err;
1882 }
1883
1884 FOR_ALL_POPULATED_CHANNELS
1885 program_timings(ctrl, channel);
1886
1887 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001888 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001889
1890 FOR_ALL_POPULATED_CHANNELS
1891 program_timings(ctrl, channel);
1892
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001893 return 0;
1894}
1895
Angel Ponsbf13ef02020-11-11 18:40:06 +01001896static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001897{
1898 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
1899 int timC_delta;
1900 int lanes_ok = 0;
1901 int ctr = 0;
1902 int lane;
1903
1904 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
1905 FOR_ALL_LANES {
1906 ctrl->timings[channel][slotrank].lanes[lane].timC =
1907 saved_rt.lanes[lane].timC + timC_delta;
1908 }
1909 program_timings(ctrl, channel);
1910 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001911 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001912 }
1913
Angel Pons765d4652020-11-11 14:44:35 +01001914 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001915 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001916
Angel Pons88521882020-01-05 20:21:20 +01001917 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001918
Angel Ponsffd50152020-11-12 11:03:10 +01001919 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001920
1921 /* Program LFSR for the RD/WR subsequences */
1922 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1923 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001924
Angel Pons7c49cb82020-03-16 23:17:32 +01001925 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001926 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001927
Angel Pons88521882020-01-05 20:21:20 +01001928 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001929 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001930 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001931
1932 if (r32 == 0)
1933 lanes_ok |= 1 << lane;
1934 }
1935 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001936 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001937 break;
1938 }
1939
1940 ctrl->timings[channel][slotrank] = saved_rt;
1941
Patrick Rudolphdd662872017-10-28 18:20:11 +02001942 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001943}
1944
Angel Pons88521882020-01-05 20:21:20 +01001945static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001946{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301947 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01001948 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
1949 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001950
1951 if (patno) {
1952 u8 base8 = 0x80 >> ((patno - 1) % 8);
1953 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1954 for (i = 0; i < 32; i++) {
1955 for (j = 0; j < 16; j++) {
1956 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001957
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001958 if (invert[patno - 1][i] & (1 << (j / 2)))
1959 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001960
1961 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001962 }
1963 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001964 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001965 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1966 for (j = 0; j < 16; j++) {
1967 const u32 val = pattern[i][j];
1968 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
1969 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970 }
1971 sfence();
1972 }
Angel Pons765d4652020-11-11 14:44:35 +01001973
1974 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001975}
1976
Angel Pons88521882020-01-05 20:21:20 +01001977static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001978{
Angel Pons7d115132020-11-14 01:44:44 +01001979 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001980
Angel Pons7c49cb82020-03-16 23:17:32 +01001981 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001982 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001983
1984 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985 dram_mrscommands(ctrl);
1986
1987 toggle_io_reset();
1988}
1989
Angel Ponsbf13ef02020-11-11 18:40:06 +01001990#define CT_MIN_PI -127
1991#define CT_MAX_PI 128
1992#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1993
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001994#define MIN_C320C_LEN 13
1995
1996static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
1997{
1998 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
1999 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002000 int command_pi;
2001 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002002 int delta = 0;
2003
2004 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2005
2006 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002007 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002008 }
2009
2010 ctrl->cmd_stretch[channel] = cmd_stretch;
2011
Angel Pons7a612742020-11-12 13:34:03 +01002012 const union tc_rap_reg tc_rap = {
2013 .tRRD = ctrl->tRRD,
2014 .tRTP = ctrl->tRTP,
2015 .tCKE = ctrl->tCKE,
2016 .tWTR = ctrl->tWTR,
2017 .tFAW = ctrl->tFAW,
2018 .tWR = ctrl->tWR,
2019 .tCMD = ctrl->cmd_stretch[channel],
2020 };
2021 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002022
2023 if (ctrl->cmd_stretch[channel] == 2)
2024 delta = 2;
2025 else if (ctrl->cmd_stretch[channel] == 0)
2026 delta = 4;
2027
2028 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002029 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002030 }
2031
Angel Ponsbf13ef02020-11-11 18:40:06 +01002032 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002033 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002034 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002035 }
2036 program_timings(ctrl, channel);
2037 reprogram_320c(ctrl);
2038 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002039 stat[slotrank][command_pi - CT_MIN_PI] =
2040 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002041 }
2042 }
2043 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002044 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002045
Angel Ponsbf13ef02020-11-11 18:40:06 +01002046 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002047 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2048 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002049
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002050 if (rn.all || rn.length < MIN_C320C_LEN) {
2051 FOR_ALL_POPULATED_RANKS {
2052 ctrl->timings[channel][slotrank] =
2053 saved_timings[channel][slotrank];
2054 }
2055 return MAKE_ERR;
2056 }
2057 }
2058
2059 return 0;
2060}
2061
Angel Pons7c49cb82020-03-16 23:17:32 +01002062/*
2063 * Adjust CMD phase shift and try multiple command rates.
2064 * A command rate of 2T doubles the time needed for address and command decode.
2065 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002066int command_training(ramctr_timing *ctrl)
2067{
2068 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002069
2070 FOR_ALL_POPULATED_CHANNELS {
2071 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002072 }
2073
2074 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002075 int cmdrate, err;
2076
2077 /*
2078 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002079 * Issue:
2080 * While c320c discovery seems to succeed raminit will fail in write training.
2081 *
2082 * Workaround:
2083 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2084 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002085 *
2086 * Single DIMM per channel:
2087 * Try command rate 1T and 2T
2088 */
2089 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002090 if (ctrl->tCMD)
2091 /* XMP gives the CMD rate in clock ticks, not ns */
2092 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002093
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002094 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002095 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2096
2097 if (!err)
2098 break;
2099 }
2100
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002101 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002102 printk(BIOS_EMERG, "c320c discovery failed\n");
2103 return err;
2104 }
2105
Angel Pons891f2bc2020-01-10 01:27:28 +01002106 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002107 }
2108
2109 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002110 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111
2112 reprogram_320c(ctrl);
2113 return 0;
2114}
2115
Angel Pons4c79f932020-11-14 01:26:52 +01002116static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002117{
Angel Pons96a06dd2020-11-14 00:33:18 +01002118 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002119 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002120 int lane;
2121
Angel Pons96a06dd2020-11-14 00:33:18 +01002122 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002123 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002124 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2125 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002126 }
2127 program_timings(ctrl, channel);
2128
2129 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002130 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2131 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002132 }
2133
Angel Pons88521882020-01-05 20:21:20 +01002134 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002135
Angel Ponsffd50152020-11-12 11:03:10 +01002136 iosav_write_read_mpr_sequence(
2137 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002138
Angel Pons7c49cb82020-03-16 23:17:32 +01002139 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002140 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002141
Angel Pons88521882020-01-05 20:21:20 +01002142 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143
2144 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002145 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146 }
2147 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002148
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002150 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002151 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002152
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002153 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002154 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2155 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002156 return MAKE_ERR;
2157 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002158 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159 }
2160 return 0;
2161}
2162
Angel Pons60971dc2020-11-14 00:49:38 +01002163static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2164{
2165 int slotrank, lane;
2166
2167 fill_pattern0(ctrl, channel, 0, 0);
2168 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002169 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002170 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2171 }
2172
2173 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2174 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2175 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2176 }
2177
2178 program_timings(ctrl, channel);
2179
2180 FOR_ALL_POPULATED_RANKS {
2181 wait_for_iosav(channel);
2182
2183 iosav_write_read_mpr_sequence(
2184 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2185
2186 /* Execute command queue */
2187 iosav_run_once(channel);
2188
2189 wait_for_iosav(channel);
2190 }
2191
2192 /* XXX: check any measured value ? */
2193
2194 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2195 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2196 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2197 }
2198
2199 program_timings(ctrl, channel);
2200
2201 FOR_ALL_POPULATED_RANKS {
2202 wait_for_iosav(channel);
2203
2204 iosav_write_read_mpr_sequence(
2205 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2206
2207 /* Execute command queue */
2208 iosav_run_once(channel);
2209
2210 wait_for_iosav(channel);
2211 }
2212
2213 /* XXX: check any measured value ? */
2214
2215 FOR_ALL_LANES {
2216 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2217 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2218 }
2219}
2220
Angel Pons4c79f932020-11-14 01:26:52 +01002221int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002222{
2223 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2224 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2225 int channel, slotrank, lane;
2226 int err;
2227
Angel Pons88521882020-01-05 20:21:20 +01002228 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002229
2230 toggle_io_reset();
2231
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002232 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002233 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002234
2235 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002236 }
2237
Angel Pons0c3936e2020-03-22 12:49:27 +01002238 /*
2239 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2240 * also use a single loop. It would seem that it is a debugging configuration.
2241 */
Angel Pons88521882020-01-05 20:21:20 +01002242 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2243 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002244
2245 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002246 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002247 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002248 if (err)
2249 return err;
2250 }
2251
Angel Pons88521882020-01-05 20:21:20 +01002252 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2253 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002254
2255 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002256 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002257 rising_edges[channel][slotrank]);
2258 if (err)
2259 return err;
2260 }
2261
Angel Pons88521882020-01-05 20:21:20 +01002262 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002263
2264 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2265 ctrl->timings[channel][slotrank].lanes[lane].falling =
2266 falling_edges[channel][slotrank][lane];
2267 ctrl->timings[channel][slotrank].lanes[lane].rising =
2268 rising_edges[channel][slotrank][lane];
2269 }
2270
2271 FOR_ALL_POPULATED_CHANNELS {
2272 program_timings(ctrl, channel);
2273 }
2274
Angel Pons50a6fe72020-11-14 01:18:14 +01002275 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002276 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002277 }
2278 return 0;
2279}
2280
Angel Pons08f749d2020-11-17 16:50:56 +01002281static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002282{
Angel Pons08f749d2020-11-17 16:50:56 +01002283 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2284
Angel Pons7c49cb82020-03-16 23:17:32 +01002285 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002286 int lower[NUM_LANES];
2287 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002288 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002289
2290 FOR_ALL_LANES {
2291 lower[lane] = 0;
2292 upper[lane] = MAX_EDGE_TIMING;
2293 }
2294
Angel Pons08f749d2020-11-17 16:50:56 +01002295 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002296 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002297 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002298 };
2299 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2300 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002301
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002302 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2303 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002304 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002305
Angel Pons08f749d2020-11-17 16:50:56 +01002306 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307 FOR_ALL_LANES {
2308 ctrl->timings[channel][slotrank].lanes[lane].
Angel Pons08f749d2020-11-17 16:50:56 +01002309 rising = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002310 ctrl->timings[channel][slotrank].lanes[lane].
Angel Pons08f749d2020-11-17 16:50:56 +01002311 falling = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002312 }
2313 program_timings(ctrl, channel);
2314
2315 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002316 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2317 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002318 }
Angel Pons88521882020-01-05 20:21:20 +01002319 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002320
Angel Ponsffd50152020-11-12 11:03:10 +01002321 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002322
Angel Pons7c49cb82020-03-16 23:17:32 +01002323 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002324 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002325
Angel Pons88521882020-01-05 20:21:20 +01002326 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002327 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002328 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002329 }
2330
Angel Pons7c49cb82020-03-16 23:17:32 +01002331 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002332 raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002333 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002334
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002335 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002336 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002337 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002338
2339 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2340 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002341
2342 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2343
2344 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2345 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2346 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002347 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002348
2349 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2350 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2351
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002352 edges[lane] = (lower[lane] + upper[lane]) / 2;
2353 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002354 printk(BIOS_EMERG, "edge write discovery failed: "
2355 "%d, %d, %d\n", channel, slotrank, lane);
2356
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002357 return MAKE_ERR;
2358 }
2359 }
2360 }
2361 }
2362
Angel Ponsa93f46e2020-11-17 16:54:01 +01002363 /* Restore nominal Vref after training */
2364 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002365 printram("CPA\n");
2366 return 0;
2367}
2368
Angel Pons08f749d2020-11-17 16:50:56 +01002369int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002370{
2371 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002372 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2373 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002374
Angel Pons7c49cb82020-03-16 23:17:32 +01002375 /*
2376 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2377 * also use a single loop. It would seem that it is a debugging configuration.
2378 */
Angel Pons88521882020-01-05 20:21:20 +01002379 MCHBAR32(IOSAV_DC_MASK) = 0x300;
Angel Pons08f749d2020-11-17 16:50:56 +01002380 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002381
2382 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002383 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002384 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002385 if (err)
2386 return err;
2387 }
2388
Angel Pons88521882020-01-05 20:21:20 +01002389 MCHBAR32(IOSAV_DC_MASK) = 0x200;
Angel Pons08f749d2020-11-17 16:50:56 +01002390 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002391
2392 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002393 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002394 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002395 if (err)
2396 return err;
2397 }
2398
Angel Pons88521882020-01-05 20:21:20 +01002399 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002400
2401 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2402 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002403 falling_edges[channel][slotrank][lane];
2404
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002405 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002406 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002407 }
2408
2409 FOR_ALL_POPULATED_CHANNELS
2410 program_timings(ctrl, channel);
2411
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002412 return 0;
2413}
2414
Angel Pons2a7d7522020-11-19 12:49:07 +01002415static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002416{
Angel Pons88521882020-01-05 20:21:20 +01002417 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002418
Angel Ponsffd50152020-11-12 11:03:10 +01002419 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002420
Angel Pons7c49cb82020-03-16 23:17:32 +01002421 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002422 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002423
Angel Pons88521882020-01-05 20:21:20 +01002424 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002425}
2426
Angel Pons2a7d7522020-11-19 12:49:07 +01002427static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002428{
Angel Pons2a7d7522020-11-19 12:49:07 +01002429 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24);
2430 udelay(2);
2431}
2432
2433int aggressive_write_training(ramctr_timing *ctrl)
2434{
2435 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002436 int i, pat;
2437
2438 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2439 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2440 int channel, slotrank, lane;
2441
Angel Pons9fbb1b02020-11-19 12:53:36 +01002442 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2443 if (!IS_IVY_CPU(ctrl->cpu))
2444 return 0;
2445
2446 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2447 return 0;
2448
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002449 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2450 lower[channel][slotrank][lane] = 0;
2451 upper[channel][slotrank][lane] = MAX_TIMC;
2452 }
2453
Angel Pons2a7d7522020-11-19 12:49:07 +01002454 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2455 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2456
2457 if (enable_iosav_opt)
2458 MCHBAR32(MCMNTS_SPARE) = 1;
2459
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002460 printram("discover timC write:\n");
2461
Angel Pons2a7d7522020-11-19 12:49:07 +01002462 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002463 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002464 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002465
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002466 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2467 FOR_ALL_POPULATED_RANKS {
2468 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002469 u32 raw_stats[MAX_TIMC + 1];
2470 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002471
2472 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002473 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002474
2475 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002476
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002477 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002478 FOR_ALL_LANES {
2479 ctrl->timings[channel][slotrank]
2480 .lanes[lane].timC = timC;
2481 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002482 program_timings(ctrl, channel);
2483
Angel Pons2a7d7522020-11-19 12:49:07 +01002484 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002485
Angel Pons098240eb2020-03-22 12:55:32 +01002486 raw_stats[timC] = MCHBAR32(
2487 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002488 }
2489 FOR_ALL_LANES {
2490 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002491 for (timC = 0; timC < MAX_TIMC; timC++) {
2492 stats[timC] = !!(raw_stats[timC]
2493 & (1 << lane));
2494 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002495
Angel Pons7c49cb82020-03-16 23:17:32 +01002496 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002497 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002498 printk(BIOS_EMERG,
2499 "timC write discovery failed: "
2500 "%d, %d, %d\n", channel,
2501 slotrank, lane);
2502
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002503 return MAKE_ERR;
2504 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002505 printram("timC: %d, %d, %d: "
2506 "0x%02x-0x%02x-0x%02x, "
2507 "0x%02x-0x%02x\n", channel, slotrank,
2508 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002509 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002510 rn.end - ctrl->timC_offset[i]);
2511
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002512 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002513 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002514 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002515
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002516 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002517 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002518 upper[channel][slotrank][lane]);
2519
2520 }
2521 }
2522 }
2523 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002524 }
2525
Angel Pons2a7d7522020-11-19 12:49:07 +01002526 FOR_ALL_CHANNELS {
2527 /* Restore nominal write Vref after training */
2528 set_write_vref(channel, 0);
2529 }
2530
2531 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2532 if (enable_iosav_opt)
2533 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002534
2535 printram("CPB\n");
2536
2537 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002538 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002539 (lower[channel][slotrank][lane] +
2540 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002541
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002542 ctrl->timings[channel][slotrank].lanes[lane].timC =
2543 (lower[channel][slotrank][lane] +
2544 upper[channel][slotrank][lane]) / 2;
2545 }
2546 FOR_ALL_POPULATED_CHANNELS {
2547 program_timings(ctrl, channel);
2548 }
2549 return 0;
2550}
2551
Angel Pons88521882020-01-05 20:21:20 +01002552void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002553{
2554 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002555 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002556
2557 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2558 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002559 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002560 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002561 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002562 printram("normalize %d, %d, %d: mat %d\n",
2563 channel, slotrank, lane, mat);
2564
Felix Heldef4fe3e2019-12-31 14:15:05 +01002565 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002566 printram("normalize %d, %d, %d: delta %d\n",
2567 channel, slotrank, lane, delta);
2568
Angel Pons88521882020-01-05 20:21:20 +01002569 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002570 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002571 }
2572
2573 FOR_ALL_POPULATED_CHANNELS {
2574 program_timings(ctrl, channel);
2575 }
2576}
2577
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002578int channel_test(ramctr_timing *ctrl)
2579{
2580 int channel, slotrank, lane;
2581
2582 slotrank = 0;
2583 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002584 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002585 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002586 return MAKE_ERR;
2587 }
2588 FOR_ALL_POPULATED_CHANNELS {
2589 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002590 }
2591
2592 for (slotrank = 0; slotrank < 4; slotrank++)
2593 FOR_ALL_CHANNELS
2594 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2595 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002596 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2597 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002598 }
Angel Pons88521882020-01-05 20:21:20 +01002599 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002600
Angel Ponsffd50152020-11-12 11:03:10 +01002601 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002602
Angel Pons7c49cb82020-03-16 23:17:32 +01002603 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002604 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002605
Angel Pons88521882020-01-05 20:21:20 +01002606 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002607 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002608 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002609 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2610 channel, slotrank, lane);
2611 return MAKE_ERR;
2612 }
2613 }
2614 return 0;
2615}
2616
Patrick Rudolphdd662872017-10-28 18:20:11 +02002617void channel_scrub(ramctr_timing *ctrl)
2618{
2619 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002620 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002621
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002622 FOR_ALL_POPULATED_CHANNELS {
2623 wait_for_iosav(channel);
2624 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002625 }
2626
2627 /*
2628 * During runtime the "scrubber" will periodically scan through the memory in the
2629 * physical address space, to identify and fix CRC errors.
2630 * The following loops writes to every DRAM address, setting the ECC bits to the
2631 * correct value. A read from this location will no longer return a CRC error,
2632 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002633 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002634 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2635 * and firmware running in x86_32.
2636 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002637 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2638 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002639 for (bank = 0; bank < 8; bank++) {
2640 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002641
Angel Pons8f0757e2020-11-11 23:03:36 +01002642 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2643 const struct iosav_ssq sequence[] = {
2644 /*
2645 * DRAM command ACT
2646 * Opens the row for writing.
2647 */
2648 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002649 .sp_cmd_ctrl = {
2650 .command = IOSAV_ACT,
2651 .ranksel_ap = 1,
2652 },
2653 .subseq_ctrl = {
2654 .cmd_executions = 1,
2655 .cmd_delay_gap = gap,
2656 .post_ssq_wait = ctrl->tRCD,
2657 .data_direction = SSQ_NA,
2658 },
2659 .sp_cmd_addr = {
2660 .address = row,
2661 .rowbits = 6,
2662 .bank = bank,
2663 .rank = slotrank,
2664 },
2665 .addr_update = {
2666 .inc_addr_1 = 1,
2667 .addr_wrap = 18,
2668 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002669 },
2670 /*
2671 * DRAM command WR
2672 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2673 * bytes.
2674 */
2675 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002676 .sp_cmd_ctrl = {
2677 .command = IOSAV_WR,
2678 .ranksel_ap = 1,
2679 },
2680 .subseq_ctrl = {
2681 .cmd_executions = 129,
2682 .cmd_delay_gap = 4,
2683 .post_ssq_wait = ctrl->tWTR +
2684 ctrl->CWL + 8,
2685 .data_direction = SSQ_WR,
2686 },
2687 .sp_cmd_addr = {
2688 .address = row,
2689 .rowbits = 0,
2690 .bank = bank,
2691 .rank = slotrank,
2692 },
2693 .addr_update = {
2694 .inc_addr_8 = 1,
2695 .addr_wrap = 9,
2696 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002697 },
2698 /*
2699 * DRAM command PRE
2700 * Closes the row.
2701 */
2702 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002703 .sp_cmd_ctrl = {
2704 .command = IOSAV_PRE,
2705 .ranksel_ap = 1,
2706 },
2707 .subseq_ctrl = {
2708 .cmd_executions = 1,
2709 .cmd_delay_gap = 4,
2710 .post_ssq_wait = ctrl->tRP,
2711 .data_direction = SSQ_NA,
2712 },
2713 .sp_cmd_addr = {
2714 .address = 0,
2715 .rowbits = 6,
2716 .bank = bank,
2717 .rank = slotrank,
2718 },
2719 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002720 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002721 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002722 },
2723 };
2724 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002725
2726 /* Execute command queue */
2727 iosav_run_queue(channel, 16, 0);
2728
2729 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002730 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002731 }
2732 }
2733}
2734
Angel Pons88521882020-01-05 20:21:20 +01002735void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002736{
2737 int channel;
2738
Angel Pons7c49cb82020-03-16 23:17:32 +01002739 /* 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 +01002740 static u32 seeds[NUM_CHANNELS][3] = {
2741 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2742 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2743 };
2744 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002745 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002746 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2747 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2748 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002749 }
2750}
2751
Angel Pons89ae6b82020-03-21 13:23:32 +01002752void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002753{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002754 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002755 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002756 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002757 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002758 }
2759}
2760
Angel Pons88521882020-01-05 20:21:20 +01002761void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002762{
2763 int channel;
2764
2765 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002766 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002767 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002768 }
2769
2770 udelay(1);
2771
2772 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002773 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002774 }
2775}
2776
Angel Pons7c49cb82020-03-16 23:17:32 +01002777void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002778{
Angel Pons11463322020-11-19 11:04:28 +01002779 /* Use a larger delay when running fast to improve stability */
2780 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2781
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002782 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002783
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002784 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002785 int min_pi = 10000;
2786 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002787
2788 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002789 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2790 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002791 }
2792
Angel Pons7a612742020-11-12 13:34:03 +01002793 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002794
Angel Pons7a612742020-11-12 13:34:03 +01002795 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002796
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002797 dram_odt_stretch(ctrl, channel);
2798
Angel Pons7a612742020-11-12 13:34:03 +01002799 const union tc_rwp_reg tc_rwp = {
2800 .tRRDR = 0,
2801 .tRRDD = val,
2802 .tWWDR = val,
2803 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002804 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002805 .tWRDRDD = tWRDRDD,
2806 .tRWSR = 2,
2807 .dec_wrd = 1,
2808 };
2809 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002810 }
2811}
2812
Angel Pons88521882020-01-05 20:21:20 +01002813void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002814{
2815 int channel;
2816 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002817 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2818 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002819 }
2820}
2821
Angel Pons7c49cb82020-03-16 23:17:32 +01002822/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2823static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002824{
Angel Pons88521882020-01-05 20:21:20 +01002825 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002826}
2827
Angel Pons7c49cb82020-03-16 23:17:32 +01002828/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002829void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002830{
2831 int channel;
2832 int t1_cycles = 0, t1_ns = 0, t2_ns;
2833 int t3_ns;
2834 u32 r32;
2835
Angel Pons7c49cb82020-03-16 23:17:32 +01002836 /* FIXME: This register only exists on Ivy Bridge */
2837 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002838
Angel Pons7a612742020-11-12 13:34:03 +01002839 FOR_ALL_CHANNELS {
2840 union tc_othp_reg tc_othp = {
2841 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2842 };
2843 tc_othp.tCPDED = 1;
2844 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2845 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002846
Angel Pons09fc4b92020-11-19 12:02:07 +01002847 /* 64 DCLKs until idle, decision per rank */
2848 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002849
Felix Heldf9b826a2018-07-30 17:56:52 +02002850 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002851 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002852
Angel Pons88521882020-01-05 20:21:20 +01002853 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
2854 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002855
2856 FOR_ALL_CHANNELS {
2857 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002858 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002859 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002860 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002861 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002862 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002863 case 1:
2864 case 4:
2865 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002866 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002867 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002868 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002869 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002870 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002871 break;
2872 }
2873 }
2874
Felix Held50b7ed22019-12-30 20:41:54 +01002875 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002876 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002877 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002878
Angel Pons7a612742020-11-12 13:34:03 +01002879 FOR_ALL_CHANNELS {
2880 union tc_rfp_reg tc_rfp = {
2881 .raw = MCHBAR32(TC_RFP_ch(channel)),
2882 };
2883 tc_rfp.refresh_2x_control = 1;
2884 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2885 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002886
Angel Ponsdc5539f2020-11-12 12:44:25 +01002887 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2888 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002889 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002890
Angel Pons7c49cb82020-03-16 23:17:32 +01002891 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002892 FOR_ALL_POPULATED_CHANNELS
2893 break;
2894
Angel Pons88521882020-01-05 20:21:20 +01002895 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2896 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002897 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002898 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002899 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002900 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002901 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002902 t1_ns += 500;
2903
Angel Pons88521882020-01-05 20:21:20 +01002904 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002905 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002906 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002907 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002908 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002909 t3_ns = 500;
2910 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002911
2912 /* The graphics driver will use these watermark values */
2913 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002914 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002915 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2916 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002917}
2918
Angel Pons88521882020-01-05 20:21:20 +01002919void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920{
Angel Ponsc6742232020-11-15 13:26:21 +01002921 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002922
Angel Pons7c49cb82020-03-16 23:17:32 +01002923 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002924 const union tc_rap_reg tc_rap = {
2925 .tRRD = ctrl->tRRD,
2926 .tRTP = ctrl->tRTP,
2927 .tCKE = ctrl->tCKE,
2928 .tWTR = ctrl->tWTR,
2929 .tFAW = ctrl->tFAW,
2930 .tWR = ctrl->tWR,
2931 .tCMD = ctrl->cmd_stretch[channel],
2932 };
2933 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002934 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002935
2936 udelay(1);
2937
2938 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002939 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002940 }
2941
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002942 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002943 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002944
2945 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002946 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002947 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002948 }
2949
2950 printram("CPE\n");
2951
Angel Pons88521882020-01-05 20:21:20 +01002952 MCHBAR32(GDCRTRAININGMOD) = 0;
2953 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002954
2955 printram("CP5b\n");
2956
2957 FOR_ALL_POPULATED_CHANNELS {
2958 program_timings(ctrl, channel);
2959 }
2960
2961 u32 reg, addr;
2962
Angel Pons7c49cb82020-03-16 23:17:32 +01002963 /* Poll for RCOMP */
2964 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
2965 ;
2966
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002967 do {
Angel Pons88521882020-01-05 20:21:20 +01002968 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002969 } while ((reg & 0x14) == 0);
2970
Angel Pons7c49cb82020-03-16 23:17:32 +01002971 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01002972 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01002973 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002974
Angel Pons7c49cb82020-03-16 23:17:32 +01002975 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002976 udelay(500);
2977
2978 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002979 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002980 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002981 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002982 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002983 MCHBAR32(addr) = reg;
2984
Angel Pons7c49cb82020-03-16 23:17:32 +01002985 /* Wait 10ns for ranks to settle */
2986 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002987
2988 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
2989 MCHBAR32(addr) = reg;
2990
Angel Pons7c49cb82020-03-16 23:17:32 +01002991 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002992 write_reset(ctrl);
2993 }
2994
Angel Pons7c49cb82020-03-16 23:17:32 +01002995 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002996 dram_mrscommands(ctrl);
2997
2998 printram("CP5c\n");
2999
Angel Pons88521882020-01-05 20:21:20 +01003000 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003001
3002 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003003 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003004 udelay(2);
3005 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003006}