blob: 7252574ad6c100404f29e01b997dbfaf09c1a331 [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 Pons7c49cb82020-03-16 23:17:32 +01001026 int post_timA_min_high = 7, pre_timA_min_high = 7;
1027 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001028 int shift_402x = 0;
Angel Pons7584e552020-11-19 21:34:32 +01001029 int shift = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001030
1031 if (shift < 0)
1032 shift = 0;
1033
1034 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +02001035 post_timA_min_high = MIN(post_timA_min_high,
1036 (ctrl->timings[channel][slotrank].lanes[lane].
1037 timA + shift) >> 6);
1038 pre_timA_min_high = MIN(pre_timA_min_high,
1039 ctrl->timings[channel][slotrank].lanes[lane].
1040 timA >> 6);
1041 post_timA_max_high = MAX(post_timA_max_high,
1042 (ctrl->timings[channel][slotrank].lanes[lane].
1043 timA + shift) >> 6);
1044 pre_timA_max_high = MAX(pre_timA_max_high,
1045 ctrl->timings[channel][slotrank].lanes[lane].
1046 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001047 }
1048
1049 if (pre_timA_max_high - pre_timA_min_high <
1050 post_timA_max_high - post_timA_min_high)
1051 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001052
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053 else if (pre_timA_max_high - pre_timA_min_high >
1054 post_timA_max_high - post_timA_min_high)
1055 shift_402x = -1;
1056
Felix Helddee167e2019-12-30 17:30:16 +01001057 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001058 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001059 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001060
Angel Pons88521882020-01-05 20:21:20 +01001061 reg_roundtrip_latency |=
1062 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001063 shift_402x) << (8 * slotrank);
1064
1065 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001066 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001067 (((ctrl->timings[channel][slotrank].lanes[lane].
1068 timA + shift) & 0x3f)
1069 |
1070 ((ctrl->timings[channel][slotrank].lanes[lane].
1071 rising + shift) << 8)
1072 |
1073 (((ctrl->timings[channel][slotrank].lanes[lane].
1074 timA + shift -
1075 (post_timA_min_high << 6)) & 0x1c0) << 10)
1076 | ((ctrl->timings[channel][slotrank].lanes[lane].
1077 falling + shift) << 20));
1078
Felix Heldfb19c8a2020-01-14 21:27:59 +01001079 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001080 (((ctrl->timings[channel][slotrank].lanes[lane].
1081 timC + shift) & 0x3f)
1082 |
1083 (((ctrl->timings[channel][slotrank].lanes[lane].
1084 timB + shift) & 0x3f) << 8)
1085 |
1086 (((ctrl->timings[channel][slotrank].lanes[lane].
1087 timB + shift) & 0x1c0) << 9)
1088 |
1089 (((ctrl->timings[channel][slotrank].lanes[lane].
1090 timC + shift) & 0x40) << 13));
1091 }
1092 }
Angel Pons88521882020-01-05 20:21:20 +01001093 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1094 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001095}
1096
Angel Pons88521882020-01-05 20:21:20 +01001097static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001098{
Angel Pons88521882020-01-05 20:21:20 +01001099 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001100
Angel Ponsffd50152020-11-12 11:03:10 +01001101 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001102
Angel Pons7c49cb82020-03-16 23:17:32 +01001103 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001104 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001105
Angel Pons88521882020-01-05 20:21:20 +01001106 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001107}
1108
Angel Pons7c49cb82020-03-16 23:17:32 +01001109static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001110{
1111 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001112
1113 return (MCHBAR32(lane_base[lane] +
1114 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001115}
1116
1117struct run {
1118 int middle;
1119 int end;
1120 int start;
1121 int all;
1122 int length;
1123};
1124
1125static struct run get_longest_zero_run(int *seq, int sz)
1126{
1127 int i, ls;
1128 int bl = 0, bs = 0;
1129 struct run ret;
1130
1131 ls = 0;
1132 for (i = 0; i < 2 * sz; i++)
1133 if (seq[i % sz]) {
1134 if (i - ls > bl) {
1135 bl = i - ls;
1136 bs = ls;
1137 }
1138 ls = i + 1;
1139 }
1140 if (bl == 0) {
1141 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001142 ret.start = 0;
1143 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001144 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001145 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001146 return ret;
1147 }
1148
Angel Pons7c49cb82020-03-16 23:17:32 +01001149 ret.start = bs % sz;
1150 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001151 ret.middle = (bs + (bl - 1) / 2) % sz;
1152 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001153 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001154
1155 return ret;
1156}
1157
Angel Ponsf3053392020-11-13 23:31:12 +01001158static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001159{
1160 int timA;
1161 int statistics[NUM_LANES][128];
1162 int lane;
1163
1164 for (timA = 0; timA < 128; timA++) {
1165 FOR_ALL_LANES {
1166 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1167 }
1168 program_timings(ctrl, channel);
1169
1170 test_timA(ctrl, channel, slotrank);
1171
1172 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001173 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001174 }
1175 }
1176 FOR_ALL_LANES {
1177 struct run rn = get_longest_zero_run(statistics[lane], 128);
1178 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1179 upperA[lane] = rn.end;
1180 if (upperA[lane] < rn.middle)
1181 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001182
Patrick Rudolph368b6152016-11-25 16:36:52 +01001183 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001184 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001185 }
1186}
1187
Angel Ponsf3053392020-11-13 23:31:12 +01001188static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001189{
1190 int timA_delta;
1191 int statistics[NUM_LANES][51];
1192 int lane, i;
1193
1194 memset(statistics, 0, sizeof(statistics));
1195
1196 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001197
1198 FOR_ALL_LANES {
1199 ctrl->timings[channel][slotrank].lanes[lane].timA
1200 = upperA[lane] + timA_delta + 0x40;
1201 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001202 program_timings(ctrl, channel);
1203
1204 for (i = 0; i < 100; i++) {
1205 test_timA(ctrl, channel, slotrank);
1206 FOR_ALL_LANES {
1207 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001208 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001209 }
1210 }
1211 }
1212 FOR_ALL_LANES {
1213 int last_zero, first_all;
1214
1215 for (last_zero = -25; last_zero <= 25; last_zero++)
1216 if (statistics[lane][last_zero + 25])
1217 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001218
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001219 last_zero--;
1220 for (first_all = -25; first_all <= 25; first_all++)
1221 if (statistics[lane][first_all + 25] == 100)
1222 break;
1223
Angel Pons7c49cb82020-03-16 23:17:32 +01001224 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001225
1226 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001227 (last_zero + first_all) / 2 + upperA[lane];
1228
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001230 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001231 }
1232}
1233
Angel Ponsf3053392020-11-13 23:31:12 +01001234static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001235{
1236 int works[NUM_LANES];
1237 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001238
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001239 while (1) {
1240 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001241
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242 program_timings(ctrl, channel);
1243 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001244
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001245 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001246 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1247
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001248 if (works[lane])
1249 some_works = 1;
1250 else
1251 all_works = 0;
1252 }
1253 if (all_works)
1254 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001255
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001256 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001257 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001258 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1259 channel, slotrank);
1260 return MAKE_ERR;
1261 }
Angel Pons88521882020-01-05 20:21:20 +01001262 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001263 printram("4024 -= 2;\n");
1264 continue;
1265 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001266 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001267 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001268
Felix Heldef4fe3e2019-12-31 14:15:05 +01001269 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1271 channel, slotrank);
1272 return MAKE_ERR;
1273 }
1274 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001275 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001276 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001277 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001278 }
1279 }
1280 return 0;
1281}
1282
Angel Pons12bd8ab2020-11-13 23:10:52 +01001283static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001284{
1285 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001286 u16 logic_delay_min = 7;
1287 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001288
1289 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001290 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1291
1292 logic_delay_min = MIN(logic_delay_min, logic_delay);
1293 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001294 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001295
1296 if (logic_delay_max < logic_delay_min) {
1297 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1298 logic_delay_max, logic_delay_min, channel, slotrank);
1299 }
1300
1301 assert(logic_delay_max >= logic_delay_min);
1302
1303 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001304}
1305
Angel Pons12bd8ab2020-11-13 23:10:52 +01001306static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001307{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001308 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001309
Angel Pons7c49cb82020-03-16 23:17:32 +01001310 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001311 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001312
Angel Pons12bd8ab2020-11-13 23:10:52 +01001313 if (prev < post)
1314 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001315
Angel Pons12bd8ab2020-11-13 23:10:52 +01001316 else if (prev > post)
1317 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001318
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001319 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001320 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001321
Angel Pons12bd8ab2020-11-13 23:10:52 +01001322 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1323 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1324 printram("4024 += %d;\n", latency_offset);
1325 printram("4028 += %d;\n", latency_offset);
1326
1327 return post;
1328}
1329
1330static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1331{
1332 u16 logic_delay_min = 7;
1333 int lane;
1334
1335 FOR_ALL_LANES {
1336 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1337
1338 logic_delay_min = MIN(logic_delay_min, logic_delay);
1339 }
1340
1341 if (logic_delay_min >= 2) {
1342 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1343 logic_delay_min, channel, slotrank);
1344 }
1345
1346 FOR_ALL_LANES {
1347 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1348 }
1349 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1350 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001351}
1352
Angel Pons7f5a97c2020-11-13 16:58:46 +01001353int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001354{
1355 int channel, slotrank, lane;
1356 int err;
1357
1358 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1359 int all_high, some_high;
1360 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001361 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001362
Angel Pons88521882020-01-05 20:21:20 +01001363 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001364
Angel Ponsffd50152020-11-12 11:03:10 +01001365 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001366
Angel Pons7c49cb82020-03-16 23:17:32 +01001367 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001368 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001369
Angel Pons58b609b2020-11-13 14:35:29 +01001370 const union gdcr_training_mod_reg training_mod = {
1371 .receive_enable_mode = 1,
1372 .training_rank_sel = slotrank,
1373 .odt_always_on = 1,
1374 };
1375 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001376
Felix Heldef4fe3e2019-12-31 14:15:05 +01001377 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001378 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001379 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380
Angel Ponsf3053392020-11-13 23:31:12 +01001381 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
Felix Held2bb3cdf2018-07-28 00:23:59 +02001383 all_high = 1;
1384 some_high = 0;
1385 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001386 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001387 some_high = 1;
1388 else
1389 all_high = 0;
1390 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001391
1392 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001393 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394 printram("4028--;\n");
1395 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001396 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001397 upperA[lane] -= 0x40;
1398
1399 }
1400 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001401 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001402 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001403 printram("4024++;\n");
1404 printram("4028++;\n");
1405 }
1406
1407 program_timings(ctrl, channel);
1408
Angel Pons12bd8ab2020-11-13 23:10:52 +01001409 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001410
Angel Ponsf3053392020-11-13 23:31:12 +01001411 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001412 if (err)
1413 return err;
1414
Angel Pons12bd8ab2020-11-13 23:10:52 +01001415 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001416
Angel Ponsf3053392020-11-13 23:31:12 +01001417 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001418
Angel Pons12bd8ab2020-11-13 23:10:52 +01001419 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001420
Angel Pons12bd8ab2020-11-13 23:10:52 +01001421 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001422
Angel Pons12bd8ab2020-11-13 23:10:52 +01001423 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001424
1425 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001426 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001427 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001428
1429 printram("final results:\n");
1430 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001431 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001432 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001433
Angel Pons88521882020-01-05 20:21:20 +01001434 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001435
1436 toggle_io_reset();
1437 }
1438
1439 FOR_ALL_POPULATED_CHANNELS {
1440 program_timings(ctrl, channel);
1441 }
Angel Ponsc6742232020-11-15 13:26:21 +01001442
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001443 return 0;
1444}
1445
Angel Pons011661c2020-11-15 18:21:35 +01001446static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001447{
1448 int lane;
1449
1450 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001451 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1452 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001453 }
1454
Angel Pons88521882020-01-05 20:21:20 +01001455 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001456
Angel Ponsffd50152020-11-12 11:03:10 +01001457 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1458 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001459
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 Pons88521882020-01-05 20:21:20 +01001463 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001464
Angel Pons801a5cb2020-11-15 15:48:29 +01001465 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001466
Angel Pons7c49cb82020-03-16 23:17:32 +01001467 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001468 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001469
Angel Pons88521882020-01-05 20:21:20 +01001470 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001471}
1472
Angel Pons011661c2020-11-15 18:21:35 +01001473static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001474{
1475 int min = data[0];
1476 int max = min;
1477 int i;
1478 for (i = 1; i < count; i++) {
1479 if (min > data[i])
1480 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001481
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001482 if (max < data[i])
1483 max = data[i];
1484 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001485 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001486 for (i = 0; i < count; i++)
1487 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001488
Angel Pons891f2bc2020-01-10 01:27:28 +01001489 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001490}
1491
Angel Pons011661c2020-11-15 18:21:35 +01001492static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001493{
Angel Pons011661c2020-11-15 18:21:35 +01001494 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001495 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001496 int lane;
1497
Angel Pons88521882020-01-05 20:21:20 +01001498 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001499
Angel Ponsffd50152020-11-12 11:03:10 +01001500 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001501
Angel Pons7c49cb82020-03-16 23:17:32 +01001502 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001503 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001504
Angel Pons011661c2020-11-15 18:21:35 +01001505 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1506 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001507 program_timings(ctrl, channel);
1508
Angel Pons011661c2020-11-15 18:21:35 +01001509 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001510
1511 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001512 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001513 }
1514 }
1515 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001516 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1517
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001518 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001519 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1520 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001521 /*
1522 * With command training not being done yet, the lane can be erroneous.
1523 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001524 */
Angel Pons011661c2020-11-15 18:21:35 +01001525 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001526 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1527
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001528 if (rn.all || rn.length < 8) {
1529 printk(BIOS_EMERG, "timC recovery failed\n");
1530 return MAKE_ERR;
1531 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001532 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001533 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001534 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001535 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001536 }
1537 return 0;
1538}
1539
Angel Pons88521882020-01-05 20:21:20 +01001540static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001541{
1542 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001543
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001544 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1545 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001546
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001547 return ret;
1548}
1549
Angel Pons765d4652020-11-11 14:44:35 +01001550/* Each cacheline is 64 bits long */
1551static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1552{
1553 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1554}
1555
Angel Pons88521882020-01-05 20:21:20 +01001556static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301558 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001559 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001560
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001561 for (j = 0; j < 16; j++)
1562 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001563
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001564 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001565
1566 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001567}
1568
Angel Pons88521882020-01-05 20:21:20 +01001569static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001570{
1571 int ret = 0;
1572 int channel;
1573 FOR_ALL_POPULATED_CHANNELS ret++;
1574 return ret;
1575}
1576
Angel Pons88521882020-01-05 20:21:20 +01001577static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001578{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301579 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001580 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301581 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001582
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001583 for (j = 0; j < 16; j++)
1584 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001585
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001586 for (j = 0; j < 16; j++)
1587 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001588
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001589 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001590
1591 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001592}
1593
Angel Pons820bce72020-11-14 17:02:55 +01001594static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001595{
1596 int timB;
1597 int statistics[NUM_LANES][128];
1598 int lane;
1599
Angel Pons58b609b2020-11-13 14:35:29 +01001600 const union gdcr_training_mod_reg training_mod = {
1601 .write_leveling_mode = 1,
1602 .training_rank_sel = slotrank,
1603 .enable_dqs_wl = 5,
1604 .odt_always_on = 1,
1605 .force_drive_enable = 1,
1606 };
1607 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001608
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001609 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1610 int bank = 1;
1611
1612 if (ctrl->rank_mirror[channel][slotrank])
1613 ddr3_mirror_mrreg(&bank, &mr1reg);
1614
1615 wait_for_iosav(channel);
1616
1617 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1618
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 for (timB = 0; timB < 128; timB++) {
1620 FOR_ALL_LANES {
1621 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1622 }
1623 program_timings(ctrl, channel);
1624
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001625 /* Execute command queue */
1626 iosav_run_once(channel);
1627
1628 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001629
1630 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001631 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1632 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1633 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001634 }
1635 }
1636 FOR_ALL_LANES {
1637 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001638 /*
1639 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1640 * of timB by a small value, which might cause the 6-bit value to overflow if
1641 * it's close to 0x3f. Increment the value by a small offset if it's likely
1642 * to overflow, to make sure it won't overflow while running tests and bricks
1643 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001644 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001645 * TODO: find out why some tests (edge write discovery) increment timB.
1646 */
1647 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001648 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001649 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001650 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001651
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001652 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1653 if (rn.all) {
1654 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1655 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001656
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657 return MAKE_ERR;
1658 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001659 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1660 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001661 }
1662 return 0;
1663}
1664
Angel Pons820bce72020-11-14 17:02:55 +01001665static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001666{
1667 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001668 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001669 if (val == 0xffffffffffffffffLL)
1670 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001671 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001672 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001673 for (i = 0; i < 8; i++)
1674 if (val << (8 * (7 - i) + 4))
1675 return -i;
1676 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001677 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001678 for (i = 0; i < 8; i++)
1679 if (val >> (8 * (7 - i) + 4))
1680 return i;
1681 }
1682 return 8;
1683}
1684
Angel Ponsbf13ef02020-11-11 18:40:06 +01001685static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001686{
1687 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001688
1689 const union gdcr_training_mod_reg training_mod = {
1690 .dq_dqs_training_res = 1,
1691 };
1692 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
1693
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001694 FOR_ALL_POPULATED_CHANNELS {
1695 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001696 }
1697 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1698
Angel Pons765d4652020-11-11 14:44:35 +01001699 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001700 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001701
Angel Pons88521882020-01-05 20:21:20 +01001702 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001703
Angel Ponsffd50152020-11-12 11:03:10 +01001704 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001705
Angel Pons7c49cb82020-03-16 23:17:32 +01001706 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001707 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001708
Angel Pons88521882020-01-05 20:21:20 +01001709 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001710
Angel Pons8f0757e2020-11-11 23:03:36 +01001711 const struct iosav_ssq rd_sequence[] = {
1712 /* DRAM command PREA */
1713 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001714 .sp_cmd_ctrl = {
1715 .command = IOSAV_PRE,
1716 .ranksel_ap = 1,
1717 },
1718 .subseq_ctrl = {
1719 .cmd_executions = 1,
1720 .cmd_delay_gap = 3,
1721 .post_ssq_wait = ctrl->tRP,
1722 .data_direction = SSQ_NA,
1723 },
1724 .sp_cmd_addr = {
1725 .address = 1024,
1726 .rowbits = 6,
1727 .bank = 0,
1728 .rank = slotrank,
1729 },
1730 .addr_update = {
1731 .addr_wrap = 18,
1732 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001733 },
1734 /* DRAM command ACT */
1735 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001736 .sp_cmd_ctrl = {
1737 .command = IOSAV_ACT,
1738 .ranksel_ap = 1,
1739 },
1740 .subseq_ctrl = {
1741 .cmd_executions = 1,
1742 .cmd_delay_gap = 3,
1743 .post_ssq_wait = ctrl->tRCD,
1744 .data_direction = SSQ_NA,
1745 },
1746 .sp_cmd_addr = {
1747 .address = 0,
1748 .rowbits = 6,
1749 .bank = 0,
1750 .rank = slotrank,
1751 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001752 },
1753 /* DRAM command RD */
1754 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001755 .sp_cmd_ctrl = {
1756 .command = IOSAV_RD,
1757 .ranksel_ap = 3,
1758 },
1759 .subseq_ctrl = {
1760 .cmd_executions = 1,
1761 .cmd_delay_gap = 3,
1762 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001763 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001764 ctrl->timings[channel][slotrank].io_latency,
1765 .data_direction = SSQ_RD,
1766 },
1767 .sp_cmd_addr = {
1768 .address = 8,
1769 .rowbits = 6,
1770 .bank = 0,
1771 .rank = slotrank,
1772 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001773 },
1774 };
1775 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001776
Angel Pons7c49cb82020-03-16 23:17:32 +01001777 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001778 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001779
Angel Pons88521882020-01-05 20:21:20 +01001780 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001781 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001782 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001783 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001784 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001785
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1787 ctrl->timings[channel][slotrank].lanes[lane].timB +=
Angel Pons820bce72020-11-14 17:02:55 +01001788 get_dqs_flyby_adjust(res) * 64;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001789
1790 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001791 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1792 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001793 }
1794 }
Angel Pons88521882020-01-05 20:21:20 +01001795 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001796}
1797
Angel Pons7d115132020-11-14 01:44:44 +01001798static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001799{
Angel Pons7d115132020-11-14 01:44:44 +01001800 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001801
Angel Pons7d115132020-11-14 01:44:44 +01001802 FOR_ALL_POPULATED_CHANNELS {
1803 /* choose an existing rank */
1804 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001805
Angel Pons7d115132020-11-14 01:44:44 +01001806 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001807
Angel Pons7d115132020-11-14 01:44:44 +01001808 /* Execute command queue */
1809 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001810
Angel Pons7d115132020-11-14 01:44:44 +01001811 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001812
Angel Pons7d115132020-11-14 01:44:44 +01001813 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1814 }
1815
1816 /* Refresh disable */
1817 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1818
1819 FOR_ALL_POPULATED_CHANNELS {
1820 /* Execute the same command queue */
1821 iosav_run_once(channel);
1822
1823 wait_for_iosav(channel);
1824 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001825}
1826
Angel Pons7c49cb82020-03-16 23:17:32 +01001827/*
1828 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001829 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001830 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1831 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1832 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1833 * CLK/ADDR/CMD signals have the same routing delay.
1834 *
1835 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1836 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1837 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001838 */
Angel Pons820bce72020-11-14 17:02:55 +01001839static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001840{
Angel Pons820bce72020-11-14 17:02:55 +01001841 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001842
Angel Pons7d115132020-11-14 01:44:44 +01001843 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001844
Angel Pons7c49cb82020-03-16 23:17:32 +01001845 /* Enable write leveling on all ranks
1846 Disable all DQ outputs
1847 Only NOP is allowed in this mode */
1848 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1849 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001850 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001851
Angel Ponsa1f17142020-11-15 12:50:03 +01001852 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001853 const union gdcr_training_mod_reg training_mod = {
1854 .write_leveling_mode = 1,
1855 .enable_dqs_wl = 5,
1856 .odt_always_on = 1,
1857 .force_drive_enable = 1,
1858 };
1859 MCHBAR32(GDCRTRAININGMOD) = training_mod.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001860
1861 toggle_io_reset();
1862
Angel Pons7c49cb82020-03-16 23:17:32 +01001863 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001864 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001865 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001866 if (err)
1867 return err;
1868 }
1869
Angel Pons7c49cb82020-03-16 23:17:32 +01001870 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001871 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001872 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001873
Angel Pons88521882020-01-05 20:21:20 +01001874 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001875
1876 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001877 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001878
Angel Pons7c49cb82020-03-16 23:17:32 +01001879 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001880 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001881
1882 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001883 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001884 MCHBAR32(IOSAV_STATUS_ch(channel));
1885 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001886
Angel Ponsffd50152020-11-12 11:03:10 +01001887 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001888
Angel Pons7c49cb82020-03-16 23:17:32 +01001889 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001890 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001891
Angel Pons88521882020-01-05 20:21:20 +01001892 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001893 }
1894
1895 toggle_io_reset();
1896
Angel Pons820bce72020-11-14 17:02:55 +01001897 return 0;
1898}
1899
1900int write_training(ramctr_timing *ctrl)
1901{
Angel Ponsc6742232020-11-15 13:26:21 +01001902 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001903 int err;
1904
1905 FOR_ALL_POPULATED_CHANNELS
1906 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
1907
Angel Pons4c76d252020-11-15 13:06:53 +01001908 printram("CPE\n");
1909
Angel Pons820bce72020-11-14 17:02:55 +01001910 err = jedec_write_leveling(ctrl);
1911 if (err)
1912 return err;
1913
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001914 printram("CPF\n");
1915
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001916 FOR_ALL_POPULATED_CHANNELS {
1917 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001918 }
1919
1920 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001921 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001922 if (err)
1923 return err;
1924 }
1925
1926 FOR_ALL_POPULATED_CHANNELS
1927 program_timings(ctrl, channel);
1928
1929 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001930 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001931
1932 FOR_ALL_POPULATED_CHANNELS
1933 program_timings(ctrl, channel);
1934
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001935 return 0;
1936}
1937
Angel Ponsbf13ef02020-11-11 18:40:06 +01001938static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001939{
1940 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
1941 int timC_delta;
1942 int lanes_ok = 0;
1943 int ctr = 0;
1944 int lane;
1945
1946 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
1947 FOR_ALL_LANES {
1948 ctrl->timings[channel][slotrank].lanes[lane].timC =
1949 saved_rt.lanes[lane].timC + timC_delta;
1950 }
1951 program_timings(ctrl, channel);
1952 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001953 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001954 }
1955
Angel Pons765d4652020-11-11 14:44:35 +01001956 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01001957 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001958
Angel Pons88521882020-01-05 20:21:20 +01001959 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001960
Angel Ponsffd50152020-11-12 11:03:10 +01001961 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001962
1963 /* Program LFSR for the RD/WR subsequences */
1964 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
1965 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001966
Angel Pons7c49cb82020-03-16 23:17:32 +01001967 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001968 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001969
Angel Pons88521882020-01-05 20:21:20 +01001970 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001971 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001972 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001973
1974 if (r32 == 0)
1975 lanes_ok |= 1 << lane;
1976 }
1977 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001978 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001979 break;
1980 }
1981
1982 ctrl->timings[channel][slotrank] = saved_rt;
1983
Patrick Rudolphdd662872017-10-28 18:20:11 +02001984 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985}
1986
Angel Pons88521882020-01-05 20:21:20 +01001987static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001988{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301989 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01001990 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
1991 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001992
1993 if (patno) {
1994 u8 base8 = 0x80 >> ((patno - 1) % 8);
1995 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1996 for (i = 0; i < 32; i++) {
1997 for (j = 0; j < 16; j++) {
1998 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001999
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002000 if (invert[patno - 1][i] & (1 << (j / 2)))
2001 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002002
2003 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002004 }
2005 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002006 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002007 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2008 for (j = 0; j < 16; j++) {
2009 const u32 val = pattern[i][j];
2010 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2011 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002012 }
2013 sfence();
2014 }
Angel Pons765d4652020-11-11 14:44:35 +01002015
2016 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002017}
2018
Angel Pons88521882020-01-05 20:21:20 +01002019static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002020{
Angel Pons7d115132020-11-14 01:44:44 +01002021 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002022
Angel Pons7c49cb82020-03-16 23:17:32 +01002023 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002024 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002025
2026 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002027 dram_mrscommands(ctrl);
2028
2029 toggle_io_reset();
2030}
2031
Angel Ponsbf13ef02020-11-11 18:40:06 +01002032#define CT_MIN_PI -127
2033#define CT_MAX_PI 128
2034#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2035
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002036#define MIN_C320C_LEN 13
2037
2038static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2039{
2040 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2041 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002042 int command_pi;
2043 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002044 int delta = 0;
2045
2046 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2047
2048 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002049 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002050 }
2051
2052 ctrl->cmd_stretch[channel] = cmd_stretch;
2053
Angel Pons7a612742020-11-12 13:34:03 +01002054 const union tc_rap_reg tc_rap = {
2055 .tRRD = ctrl->tRRD,
2056 .tRTP = ctrl->tRTP,
2057 .tCKE = ctrl->tCKE,
2058 .tWTR = ctrl->tWTR,
2059 .tFAW = ctrl->tFAW,
2060 .tWR = ctrl->tWR,
2061 .tCMD = ctrl->cmd_stretch[channel],
2062 };
2063 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002064
2065 if (ctrl->cmd_stretch[channel] == 2)
2066 delta = 2;
2067 else if (ctrl->cmd_stretch[channel] == 0)
2068 delta = 4;
2069
2070 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002071 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002072 }
2073
Angel Ponsbf13ef02020-11-11 18:40:06 +01002074 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002075 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002076 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002077 }
2078 program_timings(ctrl, channel);
2079 reprogram_320c(ctrl);
2080 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002081 stat[slotrank][command_pi - CT_MIN_PI] =
2082 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002083 }
2084 }
2085 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002086 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002087
Angel Ponsbf13ef02020-11-11 18:40:06 +01002088 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002089 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2090 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002091
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002092 if (rn.all || rn.length < MIN_C320C_LEN) {
2093 FOR_ALL_POPULATED_RANKS {
2094 ctrl->timings[channel][slotrank] =
2095 saved_timings[channel][slotrank];
2096 }
2097 return MAKE_ERR;
2098 }
2099 }
2100
2101 return 0;
2102}
2103
Angel Pons7c49cb82020-03-16 23:17:32 +01002104/*
2105 * Adjust CMD phase shift and try multiple command rates.
2106 * A command rate of 2T doubles the time needed for address and command decode.
2107 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002108int command_training(ramctr_timing *ctrl)
2109{
2110 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111
2112 FOR_ALL_POPULATED_CHANNELS {
2113 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002114 }
2115
2116 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002117 int cmdrate, err;
2118
2119 /*
2120 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002121 * Issue:
2122 * While c320c discovery seems to succeed raminit will fail in write training.
2123 *
2124 * Workaround:
2125 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2126 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002127 *
2128 * Single DIMM per channel:
2129 * Try command rate 1T and 2T
2130 */
2131 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002132 if (ctrl->tCMD)
2133 /* XMP gives the CMD rate in clock ticks, not ns */
2134 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002135
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002136 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002137 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2138
2139 if (!err)
2140 break;
2141 }
2142
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002144 printk(BIOS_EMERG, "c320c discovery failed\n");
2145 return err;
2146 }
2147
Angel Pons891f2bc2020-01-10 01:27:28 +01002148 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149 }
2150
2151 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002152 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002153
2154 reprogram_320c(ctrl);
2155 return 0;
2156}
2157
Angel Pons4c79f932020-11-14 01:26:52 +01002158static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159{
Angel Pons96a06dd2020-11-14 00:33:18 +01002160 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002161 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002162 int lane;
2163
Angel Pons96a06dd2020-11-14 00:33:18 +01002164 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002165 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002166 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2167 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002168 }
2169 program_timings(ctrl, channel);
2170
2171 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002172 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2173 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002174 }
2175
Angel Pons88521882020-01-05 20:21:20 +01002176 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002177
Angel Ponsffd50152020-11-12 11:03:10 +01002178 iosav_write_read_mpr_sequence(
2179 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002180
Angel Pons7c49cb82020-03-16 23:17:32 +01002181 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002182 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002183
Angel Pons88521882020-01-05 20:21:20 +01002184 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002185
2186 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002187 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002188 }
2189 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002190
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002191 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002192 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002193 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002194
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002195 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002196 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2197 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002198 return MAKE_ERR;
2199 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002200 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002201 }
2202 return 0;
2203}
2204
Angel Pons60971dc2020-11-14 00:49:38 +01002205static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2206{
2207 int slotrank, lane;
2208
2209 fill_pattern0(ctrl, channel, 0, 0);
2210 FOR_ALL_LANES {
Angel Ponsc6742232020-11-15 13:26:21 +01002211 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Angel Pons60971dc2020-11-14 00:49:38 +01002212 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2213 }
2214
2215 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2216 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2217 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2218 }
2219
2220 program_timings(ctrl, channel);
2221
2222 FOR_ALL_POPULATED_RANKS {
2223 wait_for_iosav(channel);
2224
2225 iosav_write_read_mpr_sequence(
2226 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2227
2228 /* Execute command queue */
2229 iosav_run_once(channel);
2230
2231 wait_for_iosav(channel);
2232 }
2233
2234 /* XXX: check any measured value ? */
2235
2236 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2237 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2238 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2239 }
2240
2241 program_timings(ctrl, channel);
2242
2243 FOR_ALL_POPULATED_RANKS {
2244 wait_for_iosav(channel);
2245
2246 iosav_write_read_mpr_sequence(
2247 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2248
2249 /* Execute command queue */
2250 iosav_run_once(channel);
2251
2252 wait_for_iosav(channel);
2253 }
2254
2255 /* XXX: check any measured value ? */
2256
2257 FOR_ALL_LANES {
2258 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2259 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2260 }
2261}
2262
Angel Pons4c79f932020-11-14 01:26:52 +01002263int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002264{
2265 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2266 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2267 int channel, slotrank, lane;
2268 int err;
2269
Angel Pons88521882020-01-05 20:21:20 +01002270 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002271
2272 toggle_io_reset();
2273
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002274 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002275 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002276
2277 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002278 }
2279
Angel Pons0c3936e2020-03-22 12:49:27 +01002280 /*
2281 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2282 * also use a single loop. It would seem that it is a debugging configuration.
2283 */
Angel Pons88521882020-01-05 20:21:20 +01002284 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2285 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002286
2287 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002288 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002289 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002290 if (err)
2291 return err;
2292 }
2293
Angel Pons88521882020-01-05 20:21:20 +01002294 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2295 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002296
2297 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002298 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002299 rising_edges[channel][slotrank]);
2300 if (err)
2301 return err;
2302 }
2303
Angel Pons88521882020-01-05 20:21:20 +01002304 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002305
2306 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2307 ctrl->timings[channel][slotrank].lanes[lane].falling =
2308 falling_edges[channel][slotrank][lane];
2309 ctrl->timings[channel][slotrank].lanes[lane].rising =
2310 rising_edges[channel][slotrank][lane];
2311 }
2312
2313 FOR_ALL_POPULATED_CHANNELS {
2314 program_timings(ctrl, channel);
2315 }
2316
Angel Pons50a6fe72020-11-14 01:18:14 +01002317 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002318 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002319 }
2320 return 0;
2321}
2322
Angel Pons08f749d2020-11-17 16:50:56 +01002323static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324{
Angel Pons08f749d2020-11-17 16:50:56 +01002325 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2326
Angel Pons7c49cb82020-03-16 23:17:32 +01002327 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002328 int lower[NUM_LANES];
2329 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002330 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002331
2332 FOR_ALL_LANES {
2333 lower[lane] = 0;
2334 upper[lane] = MAX_EDGE_TIMING;
2335 }
2336
Angel Pons08f749d2020-11-17 16:50:56 +01002337 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002338 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002339 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002340 };
2341 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = training_mod.raw;
2342 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002343
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002344 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2345 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002346 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002347
Angel Pons08f749d2020-11-17 16:50:56 +01002348 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002349 FOR_ALL_LANES {
2350 ctrl->timings[channel][slotrank].lanes[lane].
Angel Pons08f749d2020-11-17 16:50:56 +01002351 rising = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002352 ctrl->timings[channel][slotrank].lanes[lane].
Angel Pons08f749d2020-11-17 16:50:56 +01002353 falling = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002354 }
2355 program_timings(ctrl, channel);
2356
2357 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002358 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2359 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002360 }
Angel Pons88521882020-01-05 20:21:20 +01002361 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002362
Angel Ponsffd50152020-11-12 11:03:10 +01002363 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002364
Angel Pons7c49cb82020-03-16 23:17:32 +01002365 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002366 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002367
Angel Pons88521882020-01-05 20:21:20 +01002368 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002369 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002370 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002371 }
2372
Angel Pons7c49cb82020-03-16 23:17:32 +01002373 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons08f749d2020-11-17 16:50:56 +01002374 raw_stats[read_pi] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002375 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002376
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002377 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002378 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002379 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002380
2381 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2382 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002383
2384 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2385
2386 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2387 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2388 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002389 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002390
2391 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2392 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2393
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002394 edges[lane] = (lower[lane] + upper[lane]) / 2;
2395 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002396 printk(BIOS_EMERG, "edge write discovery failed: "
2397 "%d, %d, %d\n", channel, slotrank, lane);
2398
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002399 return MAKE_ERR;
2400 }
2401 }
2402 }
2403 }
2404
Angel Ponsa93f46e2020-11-17 16:54:01 +01002405 /* Restore nominal Vref after training */
2406 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002407 printram("CPA\n");
2408 return 0;
2409}
2410
Angel Pons08f749d2020-11-17 16:50:56 +01002411int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002412{
2413 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002414 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2415 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002416
Angel Pons7c49cb82020-03-16 23:17:32 +01002417 /*
2418 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2419 * also use a single loop. It would seem that it is a debugging configuration.
2420 */
Angel Pons88521882020-01-05 20:21:20 +01002421 MCHBAR32(IOSAV_DC_MASK) = 0x300;
Angel Pons08f749d2020-11-17 16:50:56 +01002422 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002423
2424 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002425 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002426 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002427 if (err)
2428 return err;
2429 }
2430
Angel Pons88521882020-01-05 20:21:20 +01002431 MCHBAR32(IOSAV_DC_MASK) = 0x200;
Angel Pons08f749d2020-11-17 16:50:56 +01002432 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002433
2434 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002435 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002436 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002437 if (err)
2438 return err;
2439 }
2440
Angel Pons88521882020-01-05 20:21:20 +01002441 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002442
2443 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2444 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002445 falling_edges[channel][slotrank][lane];
2446
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002447 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002448 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002449 }
2450
2451 FOR_ALL_POPULATED_CHANNELS
2452 program_timings(ctrl, channel);
2453
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002454 return 0;
2455}
2456
Angel Pons2a7d7522020-11-19 12:49:07 +01002457static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002458{
Angel Pons88521882020-01-05 20:21:20 +01002459 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002460
Angel Ponsffd50152020-11-12 11:03:10 +01002461 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002462
Angel Pons7c49cb82020-03-16 23:17:32 +01002463 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002464 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002465
Angel Pons88521882020-01-05 20:21:20 +01002466 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002467}
2468
Angel Pons2a7d7522020-11-19 12:49:07 +01002469static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470{
Angel Pons2a7d7522020-11-19 12:49:07 +01002471 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~(0x3f << 24), wr_vref << 24);
2472 udelay(2);
2473}
2474
2475int aggressive_write_training(ramctr_timing *ctrl)
2476{
2477 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002478 int i, pat;
2479
2480 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2481 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2482 int channel, slotrank, lane;
2483
Angel Pons9fbb1b02020-11-19 12:53:36 +01002484 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2485 if (!IS_IVY_CPU(ctrl->cpu))
2486 return 0;
2487
2488 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2489 return 0;
2490
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002491 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2492 lower[channel][slotrank][lane] = 0;
2493 upper[channel][slotrank][lane] = MAX_TIMC;
2494 }
2495
Angel Pons2a7d7522020-11-19 12:49:07 +01002496 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2497 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2498
2499 if (enable_iosav_opt)
2500 MCHBAR32(MCMNTS_SPARE) = 1;
2501
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002502 printram("discover timC write:\n");
2503
Angel Pons2a7d7522020-11-19 12:49:07 +01002504 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002505 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002506 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002507
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002508 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2509 FOR_ALL_POPULATED_RANKS {
2510 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002511 u32 raw_stats[MAX_TIMC + 1];
2512 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002513
2514 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002515 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002516
2517 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002518
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002520 FOR_ALL_LANES {
2521 ctrl->timings[channel][slotrank]
2522 .lanes[lane].timC = timC;
2523 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002524 program_timings(ctrl, channel);
2525
Angel Pons2a7d7522020-11-19 12:49:07 +01002526 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002527
Angel Pons098240eb2020-03-22 12:55:32 +01002528 raw_stats[timC] = MCHBAR32(
2529 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002530 }
2531 FOR_ALL_LANES {
2532 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002533 for (timC = 0; timC < MAX_TIMC; timC++) {
2534 stats[timC] = !!(raw_stats[timC]
2535 & (1 << lane));
2536 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002537
Angel Pons7c49cb82020-03-16 23:17:32 +01002538 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002539 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002540 printk(BIOS_EMERG,
2541 "timC write discovery failed: "
2542 "%d, %d, %d\n", channel,
2543 slotrank, lane);
2544
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002545 return MAKE_ERR;
2546 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002547 printram("timC: %d, %d, %d: "
2548 "0x%02x-0x%02x-0x%02x, "
2549 "0x%02x-0x%02x\n", channel, slotrank,
2550 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002551 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002552 rn.end - ctrl->timC_offset[i]);
2553
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002554 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002555 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002556 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002557
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002558 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002559 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002560 upper[channel][slotrank][lane]);
2561
2562 }
2563 }
2564 }
2565 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002566 }
2567
Angel Pons2a7d7522020-11-19 12:49:07 +01002568 FOR_ALL_CHANNELS {
2569 /* Restore nominal write Vref after training */
2570 set_write_vref(channel, 0);
2571 }
2572
2573 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2574 if (enable_iosav_opt)
2575 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002576
2577 printram("CPB\n");
2578
2579 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002580 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002581 (lower[channel][slotrank][lane] +
2582 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002583
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002584 ctrl->timings[channel][slotrank].lanes[lane].timC =
2585 (lower[channel][slotrank][lane] +
2586 upper[channel][slotrank][lane]) / 2;
2587 }
2588 FOR_ALL_POPULATED_CHANNELS {
2589 program_timings(ctrl, channel);
2590 }
2591 return 0;
2592}
2593
Angel Pons88521882020-01-05 20:21:20 +01002594void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002595{
2596 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002597 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002598
2599 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2600 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002601 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002602 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002603 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002604 printram("normalize %d, %d, %d: mat %d\n",
2605 channel, slotrank, lane, mat);
2606
Felix Heldef4fe3e2019-12-31 14:15:05 +01002607 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002608 printram("normalize %d, %d, %d: delta %d\n",
2609 channel, slotrank, lane, delta);
2610
Angel Pons88521882020-01-05 20:21:20 +01002611 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002612 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002613 }
2614
2615 FOR_ALL_POPULATED_CHANNELS {
2616 program_timings(ctrl, channel);
2617 }
2618}
2619
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002620int channel_test(ramctr_timing *ctrl)
2621{
2622 int channel, slotrank, lane;
2623
2624 slotrank = 0;
2625 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002626 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002627 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002628 return MAKE_ERR;
2629 }
2630 FOR_ALL_POPULATED_CHANNELS {
2631 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002632 }
2633
2634 for (slotrank = 0; slotrank < 4; slotrank++)
2635 FOR_ALL_CHANNELS
2636 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2637 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002638 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2639 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002640 }
Angel Pons88521882020-01-05 20:21:20 +01002641 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002642
Angel Ponsffd50152020-11-12 11:03:10 +01002643 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002644
Angel Pons7c49cb82020-03-16 23:17:32 +01002645 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002646 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002647
Angel Pons88521882020-01-05 20:21:20 +01002648 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002649 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002650 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002651 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2652 channel, slotrank, lane);
2653 return MAKE_ERR;
2654 }
2655 }
2656 return 0;
2657}
2658
Patrick Rudolphdd662872017-10-28 18:20:11 +02002659void channel_scrub(ramctr_timing *ctrl)
2660{
2661 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002662 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002663
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002664 FOR_ALL_POPULATED_CHANNELS {
2665 wait_for_iosav(channel);
2666 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002667 }
2668
2669 /*
2670 * During runtime the "scrubber" will periodically scan through the memory in the
2671 * physical address space, to identify and fix CRC errors.
2672 * The following loops writes to every DRAM address, setting the ECC bits to the
2673 * correct value. A read from this location will no longer return a CRC error,
2674 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002675 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002676 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2677 * and firmware running in x86_32.
2678 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002679 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2680 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002681 for (bank = 0; bank < 8; bank++) {
2682 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002683
Angel Pons8f0757e2020-11-11 23:03:36 +01002684 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2685 const struct iosav_ssq sequence[] = {
2686 /*
2687 * DRAM command ACT
2688 * Opens the row for writing.
2689 */
2690 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002691 .sp_cmd_ctrl = {
2692 .command = IOSAV_ACT,
2693 .ranksel_ap = 1,
2694 },
2695 .subseq_ctrl = {
2696 .cmd_executions = 1,
2697 .cmd_delay_gap = gap,
2698 .post_ssq_wait = ctrl->tRCD,
2699 .data_direction = SSQ_NA,
2700 },
2701 .sp_cmd_addr = {
2702 .address = row,
2703 .rowbits = 6,
2704 .bank = bank,
2705 .rank = slotrank,
2706 },
2707 .addr_update = {
2708 .inc_addr_1 = 1,
2709 .addr_wrap = 18,
2710 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002711 },
2712 /*
2713 * DRAM command WR
2714 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2715 * bytes.
2716 */
2717 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002718 .sp_cmd_ctrl = {
2719 .command = IOSAV_WR,
2720 .ranksel_ap = 1,
2721 },
2722 .subseq_ctrl = {
2723 .cmd_executions = 129,
2724 .cmd_delay_gap = 4,
2725 .post_ssq_wait = ctrl->tWTR +
2726 ctrl->CWL + 8,
2727 .data_direction = SSQ_WR,
2728 },
2729 .sp_cmd_addr = {
2730 .address = row,
2731 .rowbits = 0,
2732 .bank = bank,
2733 .rank = slotrank,
2734 },
2735 .addr_update = {
2736 .inc_addr_8 = 1,
2737 .addr_wrap = 9,
2738 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002739 },
2740 /*
2741 * DRAM command PRE
2742 * Closes the row.
2743 */
2744 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002745 .sp_cmd_ctrl = {
2746 .command = IOSAV_PRE,
2747 .ranksel_ap = 1,
2748 },
2749 .subseq_ctrl = {
2750 .cmd_executions = 1,
2751 .cmd_delay_gap = 4,
2752 .post_ssq_wait = ctrl->tRP,
2753 .data_direction = SSQ_NA,
2754 },
2755 .sp_cmd_addr = {
2756 .address = 0,
2757 .rowbits = 6,
2758 .bank = bank,
2759 .rank = slotrank,
2760 },
2761 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002762 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002763 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002764 },
2765 };
2766 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002767
2768 /* Execute command queue */
2769 iosav_run_queue(channel, 16, 0);
2770
2771 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002772 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002773 }
2774 }
2775}
2776
Angel Pons88521882020-01-05 20:21:20 +01002777void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002778{
2779 int channel;
2780
Angel Pons7c49cb82020-03-16 23:17:32 +01002781 /* 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 +01002782 static u32 seeds[NUM_CHANNELS][3] = {
2783 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2784 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2785 };
2786 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002787 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002788 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2789 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2790 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002791 }
2792}
2793
Angel Pons89ae6b82020-03-21 13:23:32 +01002794void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002795{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002796 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002797 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002798 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002799 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002800 }
2801}
2802
Angel Pons88521882020-01-05 20:21:20 +01002803void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002804{
2805 int channel;
2806
2807 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002808 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002809 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002810 }
2811
2812 udelay(1);
2813
2814 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002815 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002816 }
2817}
2818
Angel Pons7c49cb82020-03-16 23:17:32 +01002819void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002820{
Angel Pons11463322020-11-19 11:04:28 +01002821 /* Use a larger delay when running fast to improve stability */
2822 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2823
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002824 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002825
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002826 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002827 int min_pi = 10000;
2828 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002829
2830 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002831 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2832 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002833 }
2834
Angel Pons7a612742020-11-12 13:34:03 +01002835 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002836
Angel Pons7a612742020-11-12 13:34:03 +01002837 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002838
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002839 dram_odt_stretch(ctrl, channel);
2840
Angel Pons7a612742020-11-12 13:34:03 +01002841 const union tc_rwp_reg tc_rwp = {
2842 .tRRDR = 0,
2843 .tRRDD = val,
2844 .tWWDR = val,
2845 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002846 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002847 .tWRDRDD = tWRDRDD,
2848 .tRWSR = 2,
2849 .dec_wrd = 1,
2850 };
2851 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002852 }
2853}
2854
Angel Pons88521882020-01-05 20:21:20 +01002855void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002856{
2857 int channel;
2858 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002859 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2860 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002861 }
2862}
2863
Angel Pons7c49cb82020-03-16 23:17:32 +01002864/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2865static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002866{
Angel Pons88521882020-01-05 20:21:20 +01002867 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002868}
2869
Angel Pons7c49cb82020-03-16 23:17:32 +01002870/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002871void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002872{
2873 int channel;
2874 int t1_cycles = 0, t1_ns = 0, t2_ns;
2875 int t3_ns;
2876 u32 r32;
2877
Angel Pons7c49cb82020-03-16 23:17:32 +01002878 /* FIXME: This register only exists on Ivy Bridge */
2879 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002880
Angel Pons7a612742020-11-12 13:34:03 +01002881 FOR_ALL_CHANNELS {
2882 union tc_othp_reg tc_othp = {
2883 .raw = MCHBAR32(TC_OTHP_ch(channel)),
2884 };
2885 tc_othp.tCPDED = 1;
2886 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
2887 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002888
Angel Pons09fc4b92020-11-19 12:02:07 +01002889 /* 64 DCLKs until idle, decision per rank */
2890 MCHBAR32(PM_PDWN_CONFIG) = get_power_down_mode(ctrl) << 8 | 64;
Patrick Rudolph652c4912017-10-31 11:36:55 +01002891
Felix Heldf9b826a2018-07-30 17:56:52 +02002892 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002893 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02002894
Angel Pons88521882020-01-05 20:21:20 +01002895 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
2896 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002897
2898 FOR_ALL_CHANNELS {
2899 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002900 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002901 case 0:
Angel Pons88521882020-01-05 20:21:20 +01002902 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002903 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002904 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002905 case 1:
2906 case 4:
2907 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01002908 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002909 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002910 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002911 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01002912 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002913 break;
2914 }
2915 }
2916
Felix Held50b7ed22019-12-30 20:41:54 +01002917 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01002918 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01002919 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002920
Angel Pons7a612742020-11-12 13:34:03 +01002921 FOR_ALL_CHANNELS {
2922 union tc_rfp_reg tc_rfp = {
2923 .raw = MCHBAR32(TC_RFP_ch(channel)),
2924 };
2925 tc_rfp.refresh_2x_control = 1;
2926 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
2927 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002928
Angel Ponsdc5539f2020-11-12 12:44:25 +01002929 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
2930 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01002931 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002932
Angel Pons7c49cb82020-03-16 23:17:32 +01002933 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002934 FOR_ALL_POPULATED_CHANNELS
2935 break;
2936
Angel Pons88521882020-01-05 20:21:20 +01002937 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2938 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002939 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002940 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002941 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002942 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002943 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002944 t1_ns += 500;
2945
Angel Pons88521882020-01-05 20:21:20 +01002946 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002947 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002948 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01002949 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002950 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002951 t3_ns = 500;
2952 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002953
2954 /* The graphics driver will use these watermark values */
2955 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002956 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002957 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2958 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002959}
2960
Angel Pons88521882020-01-05 20:21:20 +01002961void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002962{
Angel Ponsc6742232020-11-15 13:26:21 +01002963 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002964
Angel Pons7c49cb82020-03-16 23:17:32 +01002965 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002966 const union tc_rap_reg tc_rap = {
2967 .tRRD = ctrl->tRRD,
2968 .tRTP = ctrl->tRTP,
2969 .tCKE = ctrl->tCKE,
2970 .tWTR = ctrl->tWTR,
2971 .tFAW = ctrl->tFAW,
2972 .tWR = ctrl->tWR,
2973 .tCMD = ctrl->cmd_stretch[channel],
2974 };
2975 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01002976 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002977
2978 udelay(1);
2979
2980 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002981 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002982 }
2983
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002984 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002985 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002986
2987 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002988 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002989 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002990 }
2991
2992 printram("CPE\n");
2993
Angel Pons88521882020-01-05 20:21:20 +01002994 MCHBAR32(GDCRTRAININGMOD) = 0;
2995 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002996
2997 printram("CP5b\n");
2998
2999 FOR_ALL_POPULATED_CHANNELS {
3000 program_timings(ctrl, channel);
3001 }
3002
3003 u32 reg, addr;
3004
Angel Pons7c49cb82020-03-16 23:17:32 +01003005 /* Poll for RCOMP */
3006 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3007 ;
3008
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003009 do {
Angel Pons88521882020-01-05 20:21:20 +01003010 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003011 } while ((reg & 0x14) == 0);
3012
Angel Pons7c49cb82020-03-16 23:17:32 +01003013 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003014 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003015 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003016
Angel Pons7c49cb82020-03-16 23:17:32 +01003017 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003018 udelay(500);
3019
3020 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003021 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003022 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003023 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003024 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003025 MCHBAR32(addr) = reg;
3026
Angel Pons7c49cb82020-03-16 23:17:32 +01003027 /* Wait 10ns for ranks to settle */
3028 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003029
3030 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3031 MCHBAR32(addr) = reg;
3032
Angel Pons7c49cb82020-03-16 23:17:32 +01003033 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003034 write_reset(ctrl);
3035 }
3036
Angel Pons7c49cb82020-03-16 23:17:32 +01003037 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003038 dram_mrscommands(ctrl);
3039
3040 printram("CP5c\n");
3041
Angel Pons88521882020-01-05 20:21:20 +01003042 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003043
3044 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003045 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003046 udelay(2);
3047 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003048}