blob: c8757e4e0557ea39437e6c9236510951b0b5f0d8 [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
152 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100153 /* BIN parameters */
Angel Pons7a612742020-11-12 13:34:03 +0100154 const union tc_dbp_reg tc_dbp = {
155 .tRCD = ctrl->tRCD,
156 .tRP = ctrl->tRP,
157 .tAA = ctrl->CAS,
158 .tCWL = ctrl->CWL,
159 .tRAS = ctrl->tRAS,
160 };
161 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
162 MCHBAR32(TC_DBP_ch(channel)) = tc_dbp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100163
Angel Pons7c49cb82020-03-16 23:17:32 +0100164 /* Regular access parameters */
Angel Pons7a612742020-11-12 13:34:03 +0100165 const union tc_rap_reg tc_rap = {
166 .tRRD = ctrl->tRRD,
167 .tRTP = ctrl->tRTP,
168 .tCKE = ctrl->tCKE,
169 .tWTR = ctrl->tWTR,
170 .tFAW = ctrl->tFAW,
171 .tWR = ctrl->tWR,
172 .tCMD = 3,
173 };
174 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
175 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100176
Angel Pons7c49cb82020-03-16 23:17:32 +0100177 /* Other parameters */
Angel Pons7a612742020-11-12 13:34:03 +0100178 const union tc_othp_reg tc_othp = {
179 .tXPDLL = ctrl->tXPDLL,
180 .tXP = ctrl->tXP,
181 .tAONPD = ctrl->tAONPD,
182 .tCPDED = 2,
183 .tPRPDEN = 2,
184 };
185 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
186 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100187
Angel Ponsca2f68a2020-03-22 13:15:12 +0100188 /* Debug parameters - only applies to Ivy Bridge */
189 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Ponsca2f68a2020-03-22 13:15:12 +0100190 /*
191 * If tXP and tXPDLL are very high, we need to increase them by one.
192 * This can only happen on Ivy Bridge, and when overclocking the RAM.
193 */
Angel Pons7a612742020-11-12 13:34:03 +0100194 const union tc_dtp_reg tc_dtp = {
195 .overclock_tXP = ctrl->tXP >= 8,
196 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
197 };
198 MCHBAR32(TC_DTP_ch(channel)) = tc_dtp.raw;
Angel Ponsca2f68a2020-03-22 13:15:12 +0100199 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100200
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100201 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100202
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100203 /*
Angel Pons7c49cb82020-03-16 23:17:32 +0100204 * TC-Refresh timing parameters:
205 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
206 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100207 */
Angel Pons7a612742020-11-12 13:34:03 +0100208 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100209
Angel Pons7a612742020-11-12 13:34:03 +0100210 const union tc_rftp_reg tc_rftp = {
211 .tREFI = ctrl->tREFI,
212 .tRFC = ctrl->tRFC,
213 .tREFIx9 = val32 / 1024,
214 };
215 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
216 MCHBAR32(TC_RFTP_ch(channel)) = tc_rftp.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +0100217
Angel Pons7a612742020-11-12 13:34:03 +0100218 union tc_rfp_reg tc_rfp = {
219 .raw = MCHBAR32(TC_RFP_ch(channel)),
220 };
221 tc_rfp.oref_ri = 0xff;
222 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100223
Angel Pons7c49cb82020-03-16 23:17:32 +0100224 /* Self-refresh timing parameters */
Angel Pons7a612742020-11-12 13:34:03 +0100225 const union tc_srftp_reg tc_srftp = {
226 .tXSDLL = tDLLK,
227 .tXS_offset = ctrl->tXSOffset,
228 .tZQOPER = tDLLK - ctrl->tXSOffset,
229 .tMOD = ctrl->tMOD - 8,
230 };
231 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
232 MCHBAR32(TC_SRFTP_ch(channel)) = tc_srftp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100233 }
234}
235
236void dram_dimm_mapping(ramctr_timing *ctrl)
237{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100238 int channel;
239 dimm_info *info = &ctrl->info;
240
241 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200242 dimm_attr *dimmA, *dimmB;
243 u32 reg = 0;
244
Angel Pons7c49cb82020-03-16 23:17:32 +0100245 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100246 dimmA = &info->dimm[channel][0];
247 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100248 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100249 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100250 dimmA = &info->dimm[channel][1];
251 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100252 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100253 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100254
Nico Huberac4f2162017-10-01 18:14:43 +0200255 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100256 reg |= (dimmA->size_mb / 256) << 0;
257 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200258 reg |= (dimmA->width / 8 - 1) << 19;
259 }
260
261 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100262 reg |= (dimmB->size_mb / 256) << 8;
263 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200264 reg |= (dimmB->width / 8 - 1) << 20;
265 }
266
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200267 /*
268 * Rank interleave: Bit 16 of the physical address space sets
269 * the rank to use in a dual single rank DIMM configuration.
270 * That results in every 64KiB being interleaved between two ranks.
271 */
272 reg |= 1 << 21;
273 /* Enhanced interleave */
274 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200275
Angel Pons7c49cb82020-03-16 23:17:32 +0100276 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100277 ctrl->mad_dimm[channel] = reg;
278 } else {
279 ctrl->mad_dimm[channel] = 0;
280 }
281 }
282}
283
Patrick Rudolphdd662872017-10-28 18:20:11 +0200284void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100285{
286 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200287 u32 ecc;
288
289 if (ctrl->ecc_enabled)
290 ecc = training ? (1 << 24) : (3 << 24);
291 else
292 ecc = 0;
293
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100294 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200295 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100296 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200297
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200298 if (ctrl->ecc_enabled)
299 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100300}
301
Angel Pons88521882020-01-05 20:21:20 +0100302void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100303{
304 u32 reg, ch0size, ch1size;
305 u8 val;
306 reg = 0;
307 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100308
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100309 if (training) {
310 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
311 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
312 } else {
313 ch0size = ctrl->channel_size_mb[0];
314 ch1size = ctrl->channel_size_mb[1];
315 }
316
317 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100318 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100319 val = ch1size / 256;
320 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100321 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100322 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100323 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100324
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100325 } else {
Angel Pons88521882020-01-05 20:21:20 +0100326 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100327 val = ch0size / 256;
328 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100329 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100330 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100331 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100332 }
333}
334
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100335#define DEFAULT_PCI_MMIO_SIZE 2048
336
337static unsigned int get_mmio_size(void)
338{
339 const struct device *dev;
340 const struct northbridge_intel_sandybridge_config *cfg = NULL;
341
Angel Ponsb31d1d72020-01-10 01:35:09 +0100342 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100343 if (dev)
344 cfg = dev->chip_info;
345
346 /* If this is zero, it just means devicetree.cb didn't set it */
347 if (!cfg || cfg->pci_mmio_size == 0)
348 return DEFAULT_PCI_MMIO_SIZE;
349 else
350 return cfg->pci_mmio_size;
351}
352
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200353/*
354 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
355 * The ME/PCU/.. has the ability to change this.
356 * Return 0: ECC is optional
357 * Return 1: ECC is forced
358 */
359bool get_host_ecc_forced(void)
360{
361 /* read Capabilities A Register */
362 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
363 return !!(reg32 & (1 << 24));
364}
365
366/*
367 * Returns the ECC capability.
368 * The ME/PCU/.. has the ability to change this.
369 * Return 0: ECC is disabled
370 * Return 1: ECC is possible
371 */
372bool get_host_ecc_cap(void)
373{
374 /* read Capabilities A Register */
375 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
376 return !(reg32 & (1 << 25));
377}
378
Angel Pons88521882020-01-05 20:21:20 +0100379void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100380{
Angel Pons7c49cb82020-03-16 23:17:32 +0100381 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
382 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
383 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100384 uint16_t ggc;
385
386 mmiosize = get_mmio_size();
387
Felix Held87ddea22020-01-26 04:55:27 +0100388 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100389 if (!(ggc & 2)) {
390 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100391 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100392 } else {
393 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100394 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100395 }
396
397 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
398
399 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
400
401 mestolenbase = tom - me_uma_size;
402
Angel Pons7c49cb82020-03-16 23:17:32 +0100403 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
404
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100405 gfxstolenbase = toludbase - gfxstolen;
406 gttbase = gfxstolenbase - gttsize;
407
408 tsegbase = gttbase - tsegsize;
409
Angel Pons7c49cb82020-03-16 23:17:32 +0100410 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100411 tsegbasedelta = tsegbase & (tsegsize - 1);
412 tsegbase &= ~(tsegsize - 1);
413
414 gttbase -= tsegbasedelta;
415 gfxstolenbase -= tsegbasedelta;
416 toludbase -= tsegbasedelta;
417
Angel Pons7c49cb82020-03-16 23:17:32 +0100418 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100419 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100420 /* Reclaim is possible */
421 reclaim = 1;
422 remapbase = MAX(4096, tom - me_uma_size);
423 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
424 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100425 } else {
426 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100427 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100428 touudbase = tom - me_uma_size;
429 }
430
Angel Pons7c49cb82020-03-16 23:17:32 +0100431 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100432 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
433
Angel Pons7c49cb82020-03-16 23:17:32 +0100434 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100435 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100436 val = tom & 0xfff;
437 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100438 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100439 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100440
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442 val = tom & 0xfffff000;
443 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100444 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100445 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100446
Angel Pons7c49cb82020-03-16 23:17:32 +0100447 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100448 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100449 val = toludbase & 0xfff;
450 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100451 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100452 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100453
Angel Pons7c49cb82020-03-16 23:17:32 +0100454 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100455 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100456 val = touudbase & 0xfff;
457 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100458 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100459 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100460
Angel Pons7c49cb82020-03-16 23:17:32 +0100461 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100462 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100463 val = touudbase & 0xfffff000;
464 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100465 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100466 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100467
468 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100469 /* REMAP BASE */
470 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100471 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100472
Angel Pons7c49cb82020-03-16 23:17:32 +0100473 /* REMAP LIMIT */
474 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100475 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100476 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100477 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100478 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100479 val = tsegbase & 0xfff;
480 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100481 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100482 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100483
Angel Pons7c49cb82020-03-16 23:17:32 +0100484 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100485 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100486 val = gfxstolenbase & 0xfff;
487 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100488 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100489 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100490
Angel Pons7c49cb82020-03-16 23:17:32 +0100491 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100492 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100493 val = gttbase & 0xfff;
494 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100495 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100496 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100497
498 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100499 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100500 val = (0x80000 - me_uma_size) & 0xfffff000;
501 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100502 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100503 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100504
Angel Pons7c49cb82020-03-16 23:17:32 +0100505 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100506 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100507 val = mestolenbase & 0xfff;
508 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100509 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100510 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100511
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513 val = mestolenbase & 0xfffff000;
514 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100515 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100516 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100517
Angel Pons7c49cb82020-03-16 23:17:32 +0100518 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100519 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100520 val = (0x80000 - me_uma_size) & 0xfff;
521 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100522 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
523 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100524 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100525 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526 }
527}
528
Angel Pons88521882020-01-05 20:21:20 +0100529static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100530{
531 int channel, slotrank;
532
Angel Pons7c49cb82020-03-16 23:17:32 +0100533 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100534 channel = (ctrl->rankmap[0]) ? 0 : 1;
535
Angel Pons88521882020-01-05 20:21:20 +0100536 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100537
Angel Pons7c49cb82020-03-16 23:17:32 +0100538 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100539 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
540
Angel Ponsffd50152020-11-12 11:03:10 +0100541 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100542
Angel Pons7c49cb82020-03-16 23:17:32 +0100543 /*
544 * Execute command queue - why is bit 22 set here?!
545 *
546 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
547 */
Angel Pons38d901e2020-05-02 23:50:43 +0200548 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200549
Angel Pons88521882020-01-05 20:21:20 +0100550 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100551}
552
Angel Pons88521882020-01-05 20:21:20 +0100553void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100554{
Felix Held9fe248f2018-07-31 20:59:45 +0200555 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100556 int channel;
557
Angel Pons7c49cb82020-03-16 23:17:32 +0100558 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
559 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560 do {
Angel Pons88521882020-01-05 20:21:20 +0100561 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100562 } while ((reg & 0x14) == 0);
563
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100565 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100566 MCHBAR32(MC_INIT_STATE_G) = reg;
567 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100568 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100569 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100570
Angel Pons7c49cb82020-03-16 23:17:32 +0100571 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100572 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100573
Angel Pons7c49cb82020-03-16 23:17:32 +0100574 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100575 udelay(200);
576
Angel Pons7c49cb82020-03-16 23:17:32 +0100577 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100578 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100579
Angel Pons7c49cb82020-03-16 23:17:32 +0100580 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581 udelay(500);
582
Angel Pons7c49cb82020-03-16 23:17:32 +0100583 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100584 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100585
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100587 udelay(1);
588
589 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100590 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200591 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100592 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100593
Angel Pons7c49cb82020-03-16 23:17:32 +0100594 /* Wait 10ns for ranks to settle */
595 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100596
597 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
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 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100601 write_reset(ctrl);
602 }
603}
604
Angel Pons3d3bf482020-11-14 16:18:15 +0100605/*
606 * DDR3 Rank1 Address mirror swap the following pins:
607 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
608 */
609static void ddr3_mirror_mrreg(int *bank, u32 *addr)
610{
611 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
612 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
613}
614
Angel Pons7c49cb82020-03-16 23:17:32 +0100615static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100616{
Angel Pons88521882020-01-05 20:21:20 +0100617 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100618
Angel Pons3d3bf482020-11-14 16:18:15 +0100619 if (ctrl->rank_mirror[channel][slotrank])
620 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100621
Angel Pons8f0757e2020-11-11 23:03:36 +0100622 const struct iosav_ssq sequence[] = {
623 /* DRAM command MRS */
624 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200625 .sp_cmd_ctrl = {
626 .command = IOSAV_MRS,
627 },
628 .subseq_ctrl = {
629 .cmd_executions = 1,
630 .cmd_delay_gap = 4,
631 .post_ssq_wait = 4,
632 .data_direction = SSQ_NA,
633 },
634 .sp_cmd_addr = {
635 .address = val,
636 .rowbits = 6,
637 .bank = reg,
638 .rank = slotrank,
639 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100640 },
641 /* DRAM command MRS */
642 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200643 .sp_cmd_ctrl = {
644 .command = IOSAV_MRS,
645 .ranksel_ap = 1,
646 },
647 .subseq_ctrl = {
648 .cmd_executions = 1,
649 .cmd_delay_gap = 4,
650 .post_ssq_wait = 4,
651 .data_direction = SSQ_NA,
652 },
653 .sp_cmd_addr = {
654 .address = val,
655 .rowbits = 6,
656 .bank = reg,
657 .rank = slotrank,
658 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100659 },
660 /* DRAM command MRS */
661 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200662 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100663 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200664 },
665 .subseq_ctrl = {
666 .cmd_executions = 1,
667 .cmd_delay_gap = 4,
668 .post_ssq_wait = ctrl->tMOD,
669 .data_direction = SSQ_NA,
670 },
671 .sp_cmd_addr = {
672 .address = val,
673 .rowbits = 6,
674 .bank = reg,
675 .rank = slotrank,
676 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100677 },
678 };
679 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200680
Angel Pons7c49cb82020-03-16 23:17:32 +0100681 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200682 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100683}
684
Angel Pons88521882020-01-05 20:21:20 +0100685static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100686{
687 u16 mr0reg, mch_cas, mch_wr;
688 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Patrick Rudolph74203de2017-11-20 11:57:01 +0100689 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100690
Angel Pons7c49cb82020-03-16 23:17:32 +0100691 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100692 if (ctrl->CAS < 12) {
693 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
694 } else {
695 mch_cas = (u16) (ctrl->CAS - 12);
696 mch_cas = ((mch_cas << 1) | 0x1);
697 }
698
Angel Pons7c49cb82020-03-16 23:17:32 +0100699 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100700 mch_wr = mch_wr_t[ctrl->tWR - 5];
701
Angel Pons2bf28ed2020-11-12 13:49:59 +0100702 /* DLL Reset - self clearing - set after CLK frequency has been changed */
703 mr0reg = 1 << 8;
704
705 mr0reg |= (mch_cas & 0x1) << 2;
706 mr0reg |= (mch_cas & 0xe) << 3;
707 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100708
Angel Pons7c49cb82020-03-16 23:17:32 +0100709 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
Angel Pons2bf28ed2020-11-12 13:49:59 +0100710 mr0reg |= !is_mobile << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100711 return mr0reg;
712}
713
714static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
715{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200716 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100717}
718
Angel Ponsf9997482020-11-12 16:02:52 +0100719static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100720{
721 /* Get ODT based on rankmap */
722 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
723
724 if (dimms_per_ch == 1) {
725 return (const odtmap){60, 60};
726 } else {
727 return (const odtmap){120, 30};
728 }
729}
730
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100731static u32 encode_odt(u32 odt)
732{
733 switch (odt) {
734 case 30:
735 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
736 case 60:
737 return (1 << 2); // RZQ/4
738 case 120:
739 return (1 << 6); // RZQ/2
740 default:
741 case 0:
742 return 0;
743 }
744}
745
746static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
747{
748 odtmap odt;
749 u32 mr1reg;
750
Angel Ponsf9997482020-11-12 16:02:52 +0100751 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100752 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100753
754 mr1reg |= encode_odt(odt.rttnom);
755
756 return mr1reg;
757}
758
759static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
760{
761 u16 mr1reg;
762
763 mr1reg = make_mr1(ctrl, rank, channel);
764
765 write_mrreg(ctrl, channel, rank, 1, mr1reg);
766}
767
768static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
769{
Angel Pons868bca22020-11-13 13:38:04 +0100770 const u16 pasr = 0;
771 const u16 cwl = ctrl->CWL - 5;
772 const odtmap odt = get_ODT(ctrl, channel);
773
Angel Ponsdca3cb52020-11-13 13:42:07 +0100774 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100775 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
776 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100777
Angel Pons868bca22020-11-13 13:38:04 +0100778 u16 mr2reg = 0;
779 mr2reg |= pasr;
780 mr2reg |= cwl << 3;
781 mr2reg |= ctrl->auto_self_refresh << 6;
782 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100783 mr2reg |= (odt.rttwr / 60) << 9;
784
785 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100786
787 /* Program MR2 shadow */
788 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
789
790 reg32 &= 3 << 14 | 3 << 6;
791
792 reg32 |= mr2reg & ~(3 << 6);
793
794 if (rank & 1) {
795 if (srt)
796 reg32 |= 1 << (rank / 2 + 6);
797 } else {
798 if (ctrl->rank_mirror[channel][rank])
799 reg32 |= 1 << (rank / 2 + 14);
800 }
801 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100802}
803
804static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
805{
806 write_mrreg(ctrl, channel, rank, 3, 0);
807}
808
Angel Pons88521882020-01-05 20:21:20 +0100809void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100810{
811 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100812 int channel;
813
814 FOR_ALL_POPULATED_CHANNELS {
815 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100816 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100817 dram_mr2(ctrl, slotrank, channel);
818
Angel Pons7c49cb82020-03-16 23:17:32 +0100819 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100820 dram_mr3(ctrl, slotrank, channel);
821
Angel Pons7c49cb82020-03-16 23:17:32 +0100822 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100823 dram_mr1(ctrl, slotrank, channel);
824
Angel Pons7c49cb82020-03-16 23:17:32 +0100825 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100826 dram_mr0(ctrl, slotrank, channel);
827 }
828 }
829
Angel Pons8f0757e2020-11-11 23:03:36 +0100830 const struct iosav_ssq zqcl_sequence[] = {
831 /* DRAM command NOP (without ODT nor chip selects) */
832 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200833 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100834 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200835 },
836 .subseq_ctrl = {
837 .cmd_executions = 1,
838 .cmd_delay_gap = 4,
839 .post_ssq_wait = 15,
840 .data_direction = SSQ_NA,
841 },
842 .sp_cmd_addr = {
843 .address = 2,
844 .rowbits = 6,
845 .bank = 0,
846 .rank = 0,
847 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100848 },
849 /* DRAM command ZQCL */
850 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200851 .sp_cmd_ctrl = {
852 .command = IOSAV_ZQCS,
853 .ranksel_ap = 1,
854 },
855 .subseq_ctrl = {
856 .cmd_executions = 1,
857 .cmd_delay_gap = 4,
858 .post_ssq_wait = 400,
859 .data_direction = SSQ_NA,
860 },
861 .sp_cmd_addr = {
862 .address = 1024,
863 .rowbits = 6,
864 .bank = 0,
865 .rank = 0,
866 },
867 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100868 .inc_rank = 1,
869 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200870 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100871 },
872 };
873 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100874
Angel Pons7c49cb82020-03-16 23:17:32 +0100875 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200876 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100877
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100878 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100879 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100880 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100881 }
882
Angel Pons7c49cb82020-03-16 23:17:32 +0100883 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100884 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100885
886 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100887 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100888
Angel Pons88521882020-01-05 20:21:20 +0100889 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100890
891 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
892
Angel Pons7c49cb82020-03-16 23:17:32 +0100893 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100894 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100895
Angel Ponsffd50152020-11-12 11:03:10 +0100896 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200897
Angel Pons7c49cb82020-03-16 23:17:32 +0100898 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200899 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100900
Angel Pons7c49cb82020-03-16 23:17:32 +0100901 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100902 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100903 }
904}
905
Felix Held3b906032020-01-14 17:05:43 +0100906static const u32 lane_base[] = {
907 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
908 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
909 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100910};
911
Angel Pons88521882020-01-05 20:21:20 +0100912void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100913{
Angel Pons88521882020-01-05 20:21:20 +0100914 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100915 int lane;
916 int slotrank, slot;
917 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100918 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100919
920 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100921 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
922 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100923 }
924
925 for (slot = 0; slot < NUM_SLOTS; slot++)
926 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
927 case 0:
928 default:
Angel Pons88521882020-01-05 20:21:20 +0100929 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100930 break;
931 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100932 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100933 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100934 break;
935 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100936 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100937 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100938 break;
939 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100940 pi_coding_ctrl[slot] =
941 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100942 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100943 break;
944 }
945
Angel Pons7c49cb82020-03-16 23:17:32 +0100946 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100947 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100948 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
949 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100950 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100951 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
952
Angel Pons88521882020-01-05 20:21:20 +0100953 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100954
Angel Pons7c49cb82020-03-16 23:17:32 +0100955 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100956 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
957 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100958
959 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100960 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100961 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100962 if (shift < 0)
963 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100964
Angel Pons88521882020-01-05 20:21:20 +0100965 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100966
967 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100968 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
969 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100970 }
971
Angel Pons88521882020-01-05 20:21:20 +0100972 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
973 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100974
Angel Pons88521882020-01-05 20:21:20 +0100975 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100976 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100977
Angel Pons88521882020-01-05 20:21:20 +0100978 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100979
980 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100981 int post_timA_min_high = 7, pre_timA_min_high = 7;
982 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100983 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100984 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100985
986 if (shift < 0)
987 shift = 0;
988
989 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200990 post_timA_min_high = MIN(post_timA_min_high,
991 (ctrl->timings[channel][slotrank].lanes[lane].
992 timA + shift) >> 6);
993 pre_timA_min_high = MIN(pre_timA_min_high,
994 ctrl->timings[channel][slotrank].lanes[lane].
995 timA >> 6);
996 post_timA_max_high = MAX(post_timA_max_high,
997 (ctrl->timings[channel][slotrank].lanes[lane].
998 timA + shift) >> 6);
999 pre_timA_max_high = MAX(pre_timA_max_high,
1000 ctrl->timings[channel][slotrank].lanes[lane].
1001 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001002 }
1003
1004 if (pre_timA_max_high - pre_timA_min_high <
1005 post_timA_max_high - post_timA_min_high)
1006 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001007
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001008 else if (pre_timA_max_high - pre_timA_min_high >
1009 post_timA_max_high - post_timA_min_high)
1010 shift_402x = -1;
1011
Felix Helddee167e2019-12-30 17:30:16 +01001012 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001013 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001014 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001015
Angel Pons88521882020-01-05 20:21:20 +01001016 reg_roundtrip_latency |=
1017 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001018 shift_402x) << (8 * slotrank);
1019
1020 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001021 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001022 (((ctrl->timings[channel][slotrank].lanes[lane].
1023 timA + shift) & 0x3f)
1024 |
1025 ((ctrl->timings[channel][slotrank].lanes[lane].
1026 rising + shift) << 8)
1027 |
1028 (((ctrl->timings[channel][slotrank].lanes[lane].
1029 timA + shift -
1030 (post_timA_min_high << 6)) & 0x1c0) << 10)
1031 | ((ctrl->timings[channel][slotrank].lanes[lane].
1032 falling + shift) << 20));
1033
Felix Heldfb19c8a2020-01-14 21:27:59 +01001034 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001035 (((ctrl->timings[channel][slotrank].lanes[lane].
1036 timC + shift) & 0x3f)
1037 |
1038 (((ctrl->timings[channel][slotrank].lanes[lane].
1039 timB + shift) & 0x3f) << 8)
1040 |
1041 (((ctrl->timings[channel][slotrank].lanes[lane].
1042 timB + shift) & 0x1c0) << 9)
1043 |
1044 (((ctrl->timings[channel][slotrank].lanes[lane].
1045 timC + shift) & 0x40) << 13));
1046 }
1047 }
Angel Pons88521882020-01-05 20:21:20 +01001048 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1049 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001050}
1051
Angel Pons88521882020-01-05 20:21:20 +01001052static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053{
Angel Pons88521882020-01-05 20:21:20 +01001054 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001055
Angel Ponsffd50152020-11-12 11:03:10 +01001056 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001057
Angel Pons7c49cb82020-03-16 23:17:32 +01001058 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001059 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001060
Angel Pons88521882020-01-05 20:21:20 +01001061 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001062}
1063
Angel Pons7c49cb82020-03-16 23:17:32 +01001064static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001065{
1066 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001067
1068 return (MCHBAR32(lane_base[lane] +
1069 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001070}
1071
1072struct run {
1073 int middle;
1074 int end;
1075 int start;
1076 int all;
1077 int length;
1078};
1079
1080static struct run get_longest_zero_run(int *seq, int sz)
1081{
1082 int i, ls;
1083 int bl = 0, bs = 0;
1084 struct run ret;
1085
1086 ls = 0;
1087 for (i = 0; i < 2 * sz; i++)
1088 if (seq[i % sz]) {
1089 if (i - ls > bl) {
1090 bl = i - ls;
1091 bs = ls;
1092 }
1093 ls = i + 1;
1094 }
1095 if (bl == 0) {
1096 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001097 ret.start = 0;
1098 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001099 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001100 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001101 return ret;
1102 }
1103
Angel Pons7c49cb82020-03-16 23:17:32 +01001104 ret.start = bs % sz;
1105 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001106 ret.middle = (bs + (bl - 1) / 2) % sz;
1107 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001108 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001109
1110 return ret;
1111}
1112
Angel Ponsf3053392020-11-13 23:31:12 +01001113static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001114{
1115 int timA;
1116 int statistics[NUM_LANES][128];
1117 int lane;
1118
1119 for (timA = 0; timA < 128; timA++) {
1120 FOR_ALL_LANES {
1121 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1122 }
1123 program_timings(ctrl, channel);
1124
1125 test_timA(ctrl, channel, slotrank);
1126
1127 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001128 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001129 }
1130 }
1131 FOR_ALL_LANES {
1132 struct run rn = get_longest_zero_run(statistics[lane], 128);
1133 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1134 upperA[lane] = rn.end;
1135 if (upperA[lane] < rn.middle)
1136 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001137
Patrick Rudolph368b6152016-11-25 16:36:52 +01001138 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001139 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001140 }
1141}
1142
Angel Ponsf3053392020-11-13 23:31:12 +01001143static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001144{
1145 int timA_delta;
1146 int statistics[NUM_LANES][51];
1147 int lane, i;
1148
1149 memset(statistics, 0, sizeof(statistics));
1150
1151 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001152
1153 FOR_ALL_LANES {
1154 ctrl->timings[channel][slotrank].lanes[lane].timA
1155 = upperA[lane] + timA_delta + 0x40;
1156 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001157 program_timings(ctrl, channel);
1158
1159 for (i = 0; i < 100; i++) {
1160 test_timA(ctrl, channel, slotrank);
1161 FOR_ALL_LANES {
1162 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001163 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001164 }
1165 }
1166 }
1167 FOR_ALL_LANES {
1168 int last_zero, first_all;
1169
1170 for (last_zero = -25; last_zero <= 25; last_zero++)
1171 if (statistics[lane][last_zero + 25])
1172 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001173
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001174 last_zero--;
1175 for (first_all = -25; first_all <= 25; first_all++)
1176 if (statistics[lane][first_all + 25] == 100)
1177 break;
1178
Angel Pons7c49cb82020-03-16 23:17:32 +01001179 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001180
1181 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001182 (last_zero + first_all) / 2 + upperA[lane];
1183
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001184 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001185 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001186 }
1187}
1188
Angel Ponsf3053392020-11-13 23:31:12 +01001189static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190{
1191 int works[NUM_LANES];
1192 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001193
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001194 while (1) {
1195 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001196
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001197 program_timings(ctrl, channel);
1198 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001199
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001200 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001201 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 if (works[lane])
1204 some_works = 1;
1205 else
1206 all_works = 0;
1207 }
1208 if (all_works)
1209 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001210
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001211 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001212 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001213 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1214 channel, slotrank);
1215 return MAKE_ERR;
1216 }
Angel Pons88521882020-01-05 20:21:20 +01001217 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001218 printram("4024 -= 2;\n");
1219 continue;
1220 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001221 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001222 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001223
Felix Heldef4fe3e2019-12-31 14:15:05 +01001224 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001225 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1226 channel, slotrank);
1227 return MAKE_ERR;
1228 }
1229 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001230 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001231 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001232 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001233 }
1234 }
1235 return 0;
1236}
1237
Angel Pons12bd8ab2020-11-13 23:10:52 +01001238static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001239{
1240 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001241 u16 logic_delay_min = 7;
1242 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001243
1244 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001245 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1246
1247 logic_delay_min = MIN(logic_delay_min, logic_delay);
1248 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001249 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001250
1251 if (logic_delay_max < logic_delay_min) {
1252 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1253 logic_delay_max, logic_delay_min, channel, slotrank);
1254 }
1255
1256 assert(logic_delay_max >= logic_delay_min);
1257
1258 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001259}
1260
Angel Pons12bd8ab2020-11-13 23:10:52 +01001261static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001262{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001263 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001264
Angel Pons7c49cb82020-03-16 23:17:32 +01001265 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001266 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001267
Angel Pons12bd8ab2020-11-13 23:10:52 +01001268 if (prev < post)
1269 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001270
Angel Pons12bd8ab2020-11-13 23:10:52 +01001271 else if (prev > post)
1272 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001273
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001274 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001275 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001276
Angel Pons12bd8ab2020-11-13 23:10:52 +01001277 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1278 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1279 printram("4024 += %d;\n", latency_offset);
1280 printram("4028 += %d;\n", latency_offset);
1281
1282 return post;
1283}
1284
1285static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1286{
1287 u16 logic_delay_min = 7;
1288 int lane;
1289
1290 FOR_ALL_LANES {
1291 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1292
1293 logic_delay_min = MIN(logic_delay_min, logic_delay);
1294 }
1295
1296 if (logic_delay_min >= 2) {
1297 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1298 logic_delay_min, channel, slotrank);
1299 }
1300
1301 FOR_ALL_LANES {
1302 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1303 }
1304 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1305 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001306}
1307
Angel Pons7c49cb82020-03-16 23:17:32 +01001308/*
1309 * Compensate the skew between DQS and DQs.
1310 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001311 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1312 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001313 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001314 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001315 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001316 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1317 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1318 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001319 * DQ in phase the data being read is expected to alternate on every byte:
1320 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001321 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001322 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001323 * Once the controller has detected this pattern a bit in the result register is set for the
1324 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001325 */
Angel Pons88521882020-01-05 20:21:20 +01001326int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001327{
1328 int channel, slotrank, lane;
1329 int err;
1330
1331 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1332 int all_high, some_high;
1333 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001334 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001335
Angel Pons88521882020-01-05 20:21:20 +01001336 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001337
Angel Ponsffd50152020-11-12 11:03:10 +01001338 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001339
Angel Pons7c49cb82020-03-16 23:17:32 +01001340 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001341 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001342
Angel Pons88521882020-01-05 20:21:20 +01001343 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001344
Felix Heldef4fe3e2019-12-31 14:15:05 +01001345 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001346 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001347 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001348
Angel Ponsf3053392020-11-13 23:31:12 +01001349 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001350
Felix Held2bb3cdf2018-07-28 00:23:59 +02001351 all_high = 1;
1352 some_high = 0;
1353 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001354 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001355 some_high = 1;
1356 else
1357 all_high = 0;
1358 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001359
1360 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001361 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001362 printram("4028--;\n");
1363 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001364 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001365 upperA[lane] -= 0x40;
1366
1367 }
1368 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001369 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001370 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001371 printram("4024++;\n");
1372 printram("4028++;\n");
1373 }
1374
1375 program_timings(ctrl, channel);
1376
Angel Pons12bd8ab2020-11-13 23:10:52 +01001377 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001378
Angel Ponsf3053392020-11-13 23:31:12 +01001379 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380 if (err)
1381 return err;
1382
Angel Pons12bd8ab2020-11-13 23:10:52 +01001383 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Ponsf3053392020-11-13 23:31:12 +01001385 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386
Angel Pons12bd8ab2020-11-13 23:10:52 +01001387 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388
Angel Pons12bd8ab2020-11-13 23:10:52 +01001389 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390
Angel Pons12bd8ab2020-11-13 23:10:52 +01001391 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001392
1393 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001394 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001395 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396
1397 printram("final results:\n");
1398 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001399 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001400 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001401
Angel Pons88521882020-01-05 20:21:20 +01001402 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001403
1404 toggle_io_reset();
1405 }
1406
1407 FOR_ALL_POPULATED_CHANNELS {
1408 program_timings(ctrl, channel);
1409 }
Angel Pons50a6fe72020-11-14 01:18:14 +01001410 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001411 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001412 }
1413 return 0;
1414}
1415
Angel Pons011661c2020-11-15 18:21:35 +01001416static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001417{
1418 int lane;
1419
1420 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001421 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1422 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001423 }
1424
Angel Pons88521882020-01-05 20:21:20 +01001425 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001426
Angel Ponsffd50152020-11-12 11:03:10 +01001427 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1428 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001429
Angel Pons7c49cb82020-03-16 23:17:32 +01001430 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001431 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001432
Angel Pons88521882020-01-05 20:21:20 +01001433 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001434
Angel Pons8f0757e2020-11-11 23:03:36 +01001435 const struct iosav_ssq rd_sequence[] = {
1436 /* DRAM command PREA */
1437 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001438 .sp_cmd_ctrl = {
1439 .command = IOSAV_PRE,
1440 .ranksel_ap = 1,
1441 },
1442 .subseq_ctrl = {
1443 .cmd_executions = 1,
1444 .cmd_delay_gap = 3,
1445 .post_ssq_wait = ctrl->tRP,
1446 .data_direction = SSQ_NA,
1447 },
1448 .sp_cmd_addr = {
1449 .address = 1024,
1450 .rowbits = 6,
1451 .bank = 0,
1452 .rank = slotrank,
1453 },
1454 .addr_update = {
1455 .addr_wrap = 18,
1456 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001457 },
1458 /* DRAM command ACT */
1459 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001460 .sp_cmd_ctrl = {
1461 .command = IOSAV_ACT,
1462 .ranksel_ap = 1,
1463 },
1464 .subseq_ctrl = {
1465 .cmd_executions = 8,
1466 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1467 .post_ssq_wait = ctrl->CAS,
1468 .data_direction = SSQ_NA,
1469 },
1470 .sp_cmd_addr = {
1471 .address = 0,
1472 .rowbits = 6,
1473 .bank = 0,
1474 .rank = slotrank,
1475 },
1476 .addr_update = {
1477 .inc_bank = 1,
1478 .addr_wrap = 18,
1479 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001480 },
1481 /* DRAM command RD */
1482 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001483 .sp_cmd_ctrl = {
1484 .command = IOSAV_RD,
1485 .ranksel_ap = 1,
1486 },
1487 .subseq_ctrl = {
1488 .cmd_executions = 500,
1489 .cmd_delay_gap = 4,
1490 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1491 .data_direction = SSQ_RD,
1492 },
1493 .sp_cmd_addr = {
1494 .address = 0,
1495 .rowbits = 0,
1496 .bank = 0,
1497 .rank = slotrank,
1498 },
1499 .addr_update = {
1500 .inc_addr_8 = 1,
1501 .addr_wrap = 18,
1502 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001503 },
1504 /* DRAM command PREA */
1505 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001506 .sp_cmd_ctrl = {
1507 .command = IOSAV_PRE,
1508 .ranksel_ap = 1,
1509 },
1510 .subseq_ctrl = {
1511 .cmd_executions = 1,
1512 .cmd_delay_gap = 3,
1513 .post_ssq_wait = ctrl->tRP,
1514 .data_direction = SSQ_NA,
1515 },
1516 .sp_cmd_addr = {
1517 .address = 1024,
1518 .rowbits = 6,
1519 .bank = 0,
1520 .rank = slotrank,
1521 },
1522 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01001523 .addr_wrap = 18,
Angel Pons3abd2062020-05-03 00:25:02 +02001524 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001525 },
1526 };
1527 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001528
Angel Pons7c49cb82020-03-16 23:17:32 +01001529 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001530 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001531
Angel Pons88521882020-01-05 20:21:20 +01001532 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001533}
1534
Angel Pons011661c2020-11-15 18:21:35 +01001535static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001536{
1537 int min = data[0];
1538 int max = min;
1539 int i;
1540 for (i = 1; i < count; i++) {
1541 if (min > data[i])
1542 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001543
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001544 if (max < data[i])
1545 max = data[i];
1546 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001547 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001548 for (i = 0; i < count; i++)
1549 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001550
Angel Pons891f2bc2020-01-10 01:27:28 +01001551 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001552}
1553
Angel Pons011661c2020-11-15 18:21:35 +01001554static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001555{
Angel Pons011661c2020-11-15 18:21:35 +01001556 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001557 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001558 int lane;
1559
Angel Pons88521882020-01-05 20:21:20 +01001560 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001561
Angel Ponsffd50152020-11-12 11:03:10 +01001562 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001563
Angel Pons7c49cb82020-03-16 23:17:32 +01001564 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001565 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001566
Angel Pons011661c2020-11-15 18:21:35 +01001567 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1568 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001569 program_timings(ctrl, channel);
1570
Angel Pons011661c2020-11-15 18:21:35 +01001571 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001572
1573 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001574 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001575 }
1576 }
1577 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001578 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1579
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001580 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001581 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1582 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001583 /*
1584 * With command training not being done yet, the lane can be erroneous.
1585 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001586 */
Angel Pons011661c2020-11-15 18:21:35 +01001587 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001588 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1589
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001590 if (rn.all || rn.length < 8) {
1591 printk(BIOS_EMERG, "timC recovery failed\n");
1592 return MAKE_ERR;
1593 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001594 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001595 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001596 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001597 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001598 }
1599 return 0;
1600}
1601
Angel Pons88521882020-01-05 20:21:20 +01001602static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001603{
1604 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001605
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001606 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1607 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001608
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001609 return ret;
1610}
1611
Angel Pons765d4652020-11-11 14:44:35 +01001612/* Each cacheline is 64 bits long */
1613static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1614{
1615 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1616}
1617
Angel Pons88521882020-01-05 20:21:20 +01001618static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301620 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001621 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001622
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001623 for (j = 0; j < 16; j++)
1624 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001625
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001626 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001627
1628 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001629}
1630
Angel Pons88521882020-01-05 20:21:20 +01001631static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001632{
1633 int ret = 0;
1634 int channel;
1635 FOR_ALL_POPULATED_CHANNELS ret++;
1636 return ret;
1637}
1638
Angel Pons88521882020-01-05 20:21:20 +01001639static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001640{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301641 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001642 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301643 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001644
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001645 for (j = 0; j < 16; j++)
1646 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001647
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001648 for (j = 0; j < 16; j++)
1649 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001650
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001651 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001652
1653 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001654}
1655
Angel Pons88521882020-01-05 20:21:20 +01001656static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657{
1658 int channel, slotrank, lane;
1659
1660 FOR_ALL_POPULATED_CHANNELS {
1661 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001662 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1663 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001664 }
1665
1666 program_timings(ctrl, channel);
1667
1668 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001669 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001670
Angel Ponsffd50152020-11-12 11:03:10 +01001671 iosav_write_read_mpr_sequence(
1672 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001673
Angel Pons7c49cb82020-03-16 23:17:32 +01001674 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001675 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001676
Angel Pons88521882020-01-05 20:21:20 +01001677 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001678 }
1679
1680 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001681 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1682 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001683 }
1684
1685 program_timings(ctrl, channel);
1686
1687 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001688 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001689
Angel Ponsffd50152020-11-12 11:03:10 +01001690 iosav_write_read_mpr_sequence(
1691 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001692
Angel Pons7c49cb82020-03-16 23:17:32 +01001693 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001694 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001695
Angel Pons88521882020-01-05 20:21:20 +01001696 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001697 }
1698 }
1699}
1700
Angel Pons88521882020-01-05 20:21:20 +01001701static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001702{
Angel Pons59996e02020-11-14 16:34:35 +01001703 /* First DQS/DQS# rising edge after write leveling mode is programmed */
1704 const u32 tWLMRD = 40;
1705
1706 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1707 int bank = 1;
1708
1709 if (ctrl->rank_mirror[channel][slotrank])
1710 ddr3_mirror_mrreg(&bank, &mr1reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001711
Angel Pons88521882020-01-05 20:21:20 +01001712 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001713
1714 const struct iosav_ssq sequence[] = {
Angel Pons59996e02020-11-14 16:34:35 +01001715 /* DRAM command MRS: enable DQs on this slotrank */
Angel Pons8f0757e2020-11-11 23:03:36 +01001716 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001717 .sp_cmd_ctrl = {
Angel Pons59996e02020-11-14 16:34:35 +01001718 .command = IOSAV_MRS,
1719 .ranksel_ap = 1,
1720 },
1721 .subseq_ctrl = {
1722 .cmd_executions = 1,
1723 .cmd_delay_gap = 3,
1724 .post_ssq_wait = tWLMRD,
1725 .data_direction = SSQ_NA,
1726 },
1727 .sp_cmd_addr = {
1728 .address = mr1reg,
1729 .rowbits = 6,
1730 .bank = bank,
1731 .rank = slotrank,
1732 },
1733 },
1734 /* DRAM command NOP */
1735 [1] = {
1736 .sp_cmd_ctrl = {
Angel Pons3abd2062020-05-03 00:25:02 +02001737 .command = IOSAV_NOP,
1738 .ranksel_ap = 1,
1739 },
1740 .subseq_ctrl = {
1741 .cmd_executions = 1,
1742 .cmd_delay_gap = 3,
1743 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1744 .data_direction = SSQ_WR,
1745 },
1746 .sp_cmd_addr = {
1747 .address = 8,
1748 .rowbits = 0,
1749 .bank = 0,
1750 .rank = slotrank,
1751 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001752 },
1753 /* DRAM command NOP */
Angel Pons59996e02020-11-14 16:34:35 +01001754 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001755 .sp_cmd_ctrl = {
1756 .command = IOSAV_NOP_ALT,
1757 .ranksel_ap = 1,
1758 },
1759 .subseq_ctrl = {
1760 .cmd_executions = 1,
1761 .cmd_delay_gap = 3,
1762 .post_ssq_wait = ctrl->CAS + 38,
1763 .data_direction = SSQ_RD,
1764 },
1765 .sp_cmd_addr = {
1766 .address = 4,
1767 .rowbits = 0,
1768 .bank = 0,
1769 .rank = slotrank,
1770 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001771 },
Angel Pons59996e02020-11-14 16:34:35 +01001772 /* DRAM command MRS: disable DQs on this slotrank */
1773 [3] = {
1774 .sp_cmd_ctrl = {
1775 .command = IOSAV_MRS,
1776 .ranksel_ap = 1,
1777 },
1778 .subseq_ctrl = {
1779 .cmd_executions = 1,
1780 .cmd_delay_gap = 3,
1781 .post_ssq_wait = ctrl->tMOD,
1782 .data_direction = SSQ_NA,
1783 },
1784 .sp_cmd_addr = {
1785 .address = mr1reg | 1 << 12,
1786 .rowbits = 6,
1787 .bank = bank,
1788 .rank = slotrank,
1789 },
1790 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001791 };
1792 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001793
Angel Pons7c49cb82020-03-16 23:17:32 +01001794 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001795 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001796
Angel Pons88521882020-01-05 20:21:20 +01001797 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001798}
1799
1800static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1801{
1802 int timB;
1803 int statistics[NUM_LANES][128];
1804 int lane;
1805
Angel Pons88521882020-01-05 20:21:20 +01001806 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001807
1808 for (timB = 0; timB < 128; timB++) {
1809 FOR_ALL_LANES {
1810 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1811 }
1812 program_timings(ctrl, channel);
1813
1814 test_timB(ctrl, channel, slotrank);
1815
1816 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001817 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1818 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1819 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001820 }
1821 }
1822 FOR_ALL_LANES {
1823 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001824 /*
1825 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1826 * of timB by a small value, which might cause the 6-bit value to overflow if
1827 * it's close to 0x3f. Increment the value by a small offset if it's likely
1828 * to overflow, to make sure it won't overflow while running tests and bricks
1829 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001830 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001831 * TODO: find out why some tests (edge write discovery) increment timB.
1832 */
1833 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001834 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001835 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001837
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001838 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1839 if (rn.all) {
1840 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1841 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001842
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001843 return MAKE_ERR;
1844 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001845 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1846 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001847 }
1848 return 0;
1849}
1850
1851static int get_timB_high_adjust(u64 val)
1852{
1853 int i;
1854
Angel Ponsbf13ef02020-11-11 18:40:06 +01001855 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001856 if (val == 0xffffffffffffffffLL)
1857 return 0;
1858
1859 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001860 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001861 for (i = 0; i < 8; i++)
1862 if (val << (8 * (7 - i) + 4))
1863 return -i;
1864 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001865 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001866 for (i = 0; i < 8; i++)
1867 if (val >> (8 * (7 - i) + 4))
1868 return i;
1869 }
1870 return 8;
1871}
1872
Angel Ponsbf13ef02020-11-11 18:40:06 +01001873static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001874{
1875 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001876 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001877 FOR_ALL_POPULATED_CHANNELS {
1878 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001879 }
1880 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1881
Angel Pons765d4652020-11-11 14:44:35 +01001882 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001883 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001884
Angel Pons88521882020-01-05 20:21:20 +01001885 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001886
Angel Ponsffd50152020-11-12 11:03:10 +01001887 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 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);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001891
Angel Pons88521882020-01-05 20:21:20 +01001892 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001893
Angel Pons8f0757e2020-11-11 23:03:36 +01001894 const struct iosav_ssq rd_sequence[] = {
1895 /* DRAM command PREA */
1896 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001897 .sp_cmd_ctrl = {
1898 .command = IOSAV_PRE,
1899 .ranksel_ap = 1,
1900 },
1901 .subseq_ctrl = {
1902 .cmd_executions = 1,
1903 .cmd_delay_gap = 3,
1904 .post_ssq_wait = ctrl->tRP,
1905 .data_direction = SSQ_NA,
1906 },
1907 .sp_cmd_addr = {
1908 .address = 1024,
1909 .rowbits = 6,
1910 .bank = 0,
1911 .rank = slotrank,
1912 },
1913 .addr_update = {
1914 .addr_wrap = 18,
1915 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001916 },
1917 /* DRAM command ACT */
1918 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001919 .sp_cmd_ctrl = {
1920 .command = IOSAV_ACT,
1921 .ranksel_ap = 1,
1922 },
1923 .subseq_ctrl = {
1924 .cmd_executions = 1,
1925 .cmd_delay_gap = 3,
1926 .post_ssq_wait = ctrl->tRCD,
1927 .data_direction = SSQ_NA,
1928 },
1929 .sp_cmd_addr = {
1930 .address = 0,
1931 .rowbits = 6,
1932 .bank = 0,
1933 .rank = slotrank,
1934 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001935 },
1936 /* DRAM command RD */
1937 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001938 .sp_cmd_ctrl = {
1939 .command = IOSAV_RD,
1940 .ranksel_ap = 3,
1941 },
1942 .subseq_ctrl = {
1943 .cmd_executions = 1,
1944 .cmd_delay_gap = 3,
1945 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001946 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001947 ctrl->timings[channel][slotrank].io_latency,
1948 .data_direction = SSQ_RD,
1949 },
1950 .sp_cmd_addr = {
1951 .address = 8,
1952 .rowbits = 6,
1953 .bank = 0,
1954 .rank = slotrank,
1955 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001956 },
1957 };
1958 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001959
Angel Pons7c49cb82020-03-16 23:17:32 +01001960 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001961 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001962
Angel Pons88521882020-01-05 20:21:20 +01001963 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001964 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001965 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001966 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001967 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001968 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1969 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1970 get_timB_high_adjust(res) * 64;
1971
1972 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001973 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1974 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001975 }
1976 }
Angel Pons88521882020-01-05 20:21:20 +01001977 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001978}
1979
Angel Pons7d115132020-11-14 01:44:44 +01001980static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001981{
Angel Pons7d115132020-11-14 01:44:44 +01001982 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001983
Angel Pons7d115132020-11-14 01:44:44 +01001984 FOR_ALL_POPULATED_CHANNELS {
1985 /* choose an existing rank */
1986 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001987
Angel Pons7d115132020-11-14 01:44:44 +01001988 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001989
Angel Pons7d115132020-11-14 01:44:44 +01001990 /* Execute command queue */
1991 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001992
Angel Pons7d115132020-11-14 01:44:44 +01001993 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001994
Angel Pons7d115132020-11-14 01:44:44 +01001995 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1996 }
1997
1998 /* Refresh disable */
1999 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
2000
2001 FOR_ALL_POPULATED_CHANNELS {
2002 /* Execute the same command queue */
2003 iosav_run_once(channel);
2004
2005 wait_for_iosav(channel);
2006 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002007}
2008
Angel Pons7c49cb82020-03-16 23:17:32 +01002009/*
2010 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002011 *
Angel Pons7c49cb82020-03-16 23:17:32 +01002012 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
2013 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
2014 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
2015 * CLK/ADDR/CMD signals have the same routing delay.
2016 *
2017 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
2018 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
2019 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002020 */
Angel Pons88521882020-01-05 20:21:20 +01002021int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002022{
2023 int channel, slotrank, lane;
2024 int err;
2025
2026 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002027 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002028
Angel Pons7d115132020-11-14 01:44:44 +01002029 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002030
Angel Pons7c49cb82020-03-16 23:17:32 +01002031 /* Enable write leveling on all ranks
2032 Disable all DQ outputs
2033 Only NOP is allowed in this mode */
2034 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2035 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01002036 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002037
Angel Pons88521882020-01-05 20:21:20 +01002038 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002039
2040 toggle_io_reset();
2041
Angel Pons7c49cb82020-03-16 23:17:32 +01002042 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002043 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2044 err = discover_timB(ctrl, channel, slotrank);
2045 if (err)
2046 return err;
2047 }
2048
Angel Pons7c49cb82020-03-16 23:17:32 +01002049 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002050 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01002051 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002052
Angel Pons88521882020-01-05 20:21:20 +01002053 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002054
2055 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002056 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002057
Angel Pons7c49cb82020-03-16 23:17:32 +01002058 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002059 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002060
2061 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002062 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01002063 MCHBAR32(IOSAV_STATUS_ch(channel));
2064 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002065
Angel Ponsffd50152020-11-12 11:03:10 +01002066 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002067
Angel Pons7c49cb82020-03-16 23:17:32 +01002068 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002069 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002070
Angel Pons88521882020-01-05 20:21:20 +01002071 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002072 }
2073
2074 toggle_io_reset();
2075
2076 printram("CPE\n");
2077 precharge(ctrl);
2078 printram("CPF\n");
2079
Angel Pons50a6fe72020-11-14 01:18:14 +01002080 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002081 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002082 }
2083
2084 FOR_ALL_POPULATED_CHANNELS {
2085 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002086 }
2087
2088 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01002089 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002090 if (err)
2091 return err;
2092 }
2093
2094 FOR_ALL_POPULATED_CHANNELS
2095 program_timings(ctrl, channel);
2096
2097 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002098 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002099
2100 FOR_ALL_POPULATED_CHANNELS
2101 program_timings(ctrl, channel);
2102
Angel Pons50a6fe72020-11-14 01:18:14 +01002103 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002104 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002105 }
2106 return 0;
2107}
2108
Angel Ponsbf13ef02020-11-11 18:40:06 +01002109static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002110{
2111 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2112 int timC_delta;
2113 int lanes_ok = 0;
2114 int ctr = 0;
2115 int lane;
2116
2117 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2118 FOR_ALL_LANES {
2119 ctrl->timings[channel][slotrank].lanes[lane].timC =
2120 saved_rt.lanes[lane].timC + timC_delta;
2121 }
2122 program_timings(ctrl, channel);
2123 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002124 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002125 }
2126
Angel Pons765d4652020-11-11 14:44:35 +01002127 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002128 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002129
Angel Pons88521882020-01-05 20:21:20 +01002130 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002131
Angel Ponsffd50152020-11-12 11:03:10 +01002132 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002133
2134 /* Program LFSR for the RD/WR subsequences */
2135 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2136 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002137
Angel Pons7c49cb82020-03-16 23:17:32 +01002138 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002139 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002140
Angel Pons88521882020-01-05 20:21:20 +01002141 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002142 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002143 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002144
2145 if (r32 == 0)
2146 lanes_ok |= 1 << lane;
2147 }
2148 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002149 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002150 break;
2151 }
2152
2153 ctrl->timings[channel][slotrank] = saved_rt;
2154
Patrick Rudolphdd662872017-10-28 18:20:11 +02002155 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002156}
2157
Angel Pons88521882020-01-05 20:21:20 +01002158static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302160 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002161 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2162 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002163
2164 if (patno) {
2165 u8 base8 = 0x80 >> ((patno - 1) % 8);
2166 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2167 for (i = 0; i < 32; i++) {
2168 for (j = 0; j < 16; j++) {
2169 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002170
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002171 if (invert[patno - 1][i] & (1 << (j / 2)))
2172 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002173
2174 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002175 }
2176 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002177 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002178 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2179 for (j = 0; j < 16; j++) {
2180 const u32 val = pattern[i][j];
2181 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2182 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002183 }
2184 sfence();
2185 }
Angel Pons765d4652020-11-11 14:44:35 +01002186
2187 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002188}
2189
Angel Pons88521882020-01-05 20:21:20 +01002190static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002191{
Angel Pons7d115132020-11-14 01:44:44 +01002192 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002193
Angel Pons7c49cb82020-03-16 23:17:32 +01002194 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002195 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002196
2197 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002198 dram_mrscommands(ctrl);
2199
2200 toggle_io_reset();
2201}
2202
Angel Ponsbf13ef02020-11-11 18:40:06 +01002203#define CT_MIN_PI -127
2204#define CT_MAX_PI 128
2205#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2206
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002207#define MIN_C320C_LEN 13
2208
2209static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2210{
2211 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2212 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002213 int command_pi;
2214 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002215 int delta = 0;
2216
2217 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2218
2219 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002220 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002221 }
2222
2223 ctrl->cmd_stretch[channel] = cmd_stretch;
2224
Angel Pons7a612742020-11-12 13:34:03 +01002225 const union tc_rap_reg tc_rap = {
2226 .tRRD = ctrl->tRRD,
2227 .tRTP = ctrl->tRTP,
2228 .tCKE = ctrl->tCKE,
2229 .tWTR = ctrl->tWTR,
2230 .tFAW = ctrl->tFAW,
2231 .tWR = ctrl->tWR,
2232 .tCMD = ctrl->cmd_stretch[channel],
2233 };
2234 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002235
2236 if (ctrl->cmd_stretch[channel] == 2)
2237 delta = 2;
2238 else if (ctrl->cmd_stretch[channel] == 0)
2239 delta = 4;
2240
2241 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002242 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002243 }
2244
Angel Ponsbf13ef02020-11-11 18:40:06 +01002245 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002246 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002247 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002248 }
2249 program_timings(ctrl, channel);
2250 reprogram_320c(ctrl);
2251 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002252 stat[slotrank][command_pi - CT_MIN_PI] =
2253 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002254 }
2255 }
2256 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002257 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002258
Angel Ponsbf13ef02020-11-11 18:40:06 +01002259 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002260 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2261 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002262
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002263 if (rn.all || rn.length < MIN_C320C_LEN) {
2264 FOR_ALL_POPULATED_RANKS {
2265 ctrl->timings[channel][slotrank] =
2266 saved_timings[channel][slotrank];
2267 }
2268 return MAKE_ERR;
2269 }
2270 }
2271
2272 return 0;
2273}
2274
Angel Pons7c49cb82020-03-16 23:17:32 +01002275/*
2276 * Adjust CMD phase shift and try multiple command rates.
2277 * A command rate of 2T doubles the time needed for address and command decode.
2278 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002279int command_training(ramctr_timing *ctrl)
2280{
2281 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002282
2283 FOR_ALL_POPULATED_CHANNELS {
2284 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002285 }
2286
2287 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002288 int cmdrate, err;
2289
2290 /*
2291 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002292 * Issue:
2293 * While c320c discovery seems to succeed raminit will fail in write training.
2294 *
2295 * Workaround:
2296 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2297 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002298 *
2299 * Single DIMM per channel:
2300 * Try command rate 1T and 2T
2301 */
2302 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002303 if (ctrl->tCMD)
2304 /* XMP gives the CMD rate in clock ticks, not ns */
2305 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002306
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002307 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002308 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2309
2310 if (!err)
2311 break;
2312 }
2313
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002314 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002315 printk(BIOS_EMERG, "c320c discovery failed\n");
2316 return err;
2317 }
2318
Angel Pons891f2bc2020-01-10 01:27:28 +01002319 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002320 }
2321
2322 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002323 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324
2325 reprogram_320c(ctrl);
2326 return 0;
2327}
2328
Angel Pons4c79f932020-11-14 01:26:52 +01002329static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330{
Angel Pons96a06dd2020-11-14 00:33:18 +01002331 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002332 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002333 int lane;
2334
Angel Pons96a06dd2020-11-14 00:33:18 +01002335 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002337 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2338 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002339 }
2340 program_timings(ctrl, channel);
2341
2342 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002343 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2344 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002345 }
2346
Angel Pons88521882020-01-05 20:21:20 +01002347 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002348
Angel Ponsffd50152020-11-12 11:03:10 +01002349 iosav_write_read_mpr_sequence(
2350 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002351
Angel Pons7c49cb82020-03-16 23:17:32 +01002352 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002353 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002354
Angel Pons88521882020-01-05 20:21:20 +01002355 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002356
2357 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002358 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002359 }
2360 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002361
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002362 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002363 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002364 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002365
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002366 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002367 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2368 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002369 return MAKE_ERR;
2370 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002371 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002372 }
2373 return 0;
2374}
2375
Angel Pons60971dc2020-11-14 00:49:38 +01002376static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2377{
2378 int slotrank, lane;
2379
2380 fill_pattern0(ctrl, channel, 0, 0);
2381 FOR_ALL_LANES {
2382 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2383 }
2384
2385 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2386 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2387 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2388 }
2389
2390 program_timings(ctrl, channel);
2391
2392 FOR_ALL_POPULATED_RANKS {
2393 wait_for_iosav(channel);
2394
2395 iosav_write_read_mpr_sequence(
2396 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2397
2398 /* Execute command queue */
2399 iosav_run_once(channel);
2400
2401 wait_for_iosav(channel);
2402 }
2403
2404 /* XXX: check any measured value ? */
2405
2406 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2407 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2408 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2409 }
2410
2411 program_timings(ctrl, channel);
2412
2413 FOR_ALL_POPULATED_RANKS {
2414 wait_for_iosav(channel);
2415
2416 iosav_write_read_mpr_sequence(
2417 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2418
2419 /* Execute command queue */
2420 iosav_run_once(channel);
2421
2422 wait_for_iosav(channel);
2423 }
2424
2425 /* XXX: check any measured value ? */
2426
2427 FOR_ALL_LANES {
2428 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2429 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2430 }
2431}
2432
Angel Pons4c79f932020-11-14 01:26:52 +01002433int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002434{
2435 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2436 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2437 int channel, slotrank, lane;
2438 int err;
2439
Angel Pons88521882020-01-05 20:21:20 +01002440 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002441
2442 toggle_io_reset();
2443
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002444 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002445 FOR_ALL_LANES {
Angel Pons60971dc2020-11-14 00:49:38 +01002446 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002447 }
2448
Angel Pons60971dc2020-11-14 00:49:38 +01002449 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002450
2451 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002452 }
2453
Angel Pons0c3936e2020-03-22 12:49:27 +01002454 /*
2455 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2456 * also use a single loop. It would seem that it is a debugging configuration.
2457 */
Angel Pons88521882020-01-05 20:21:20 +01002458 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2459 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002460
2461 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002462 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002463 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002464 if (err)
2465 return err;
2466 }
2467
Angel Pons88521882020-01-05 20:21:20 +01002468 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2469 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470
2471 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002472 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002473 rising_edges[channel][slotrank]);
2474 if (err)
2475 return err;
2476 }
2477
Angel Pons88521882020-01-05 20:21:20 +01002478 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002479
2480 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2481 ctrl->timings[channel][slotrank].lanes[lane].falling =
2482 falling_edges[channel][slotrank][lane];
2483 ctrl->timings[channel][slotrank].lanes[lane].rising =
2484 rising_edges[channel][slotrank][lane];
2485 }
2486
2487 FOR_ALL_POPULATED_CHANNELS {
2488 program_timings(ctrl, channel);
2489 }
2490
Angel Pons50a6fe72020-11-14 01:18:14 +01002491 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002492 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002493 }
2494 return 0;
2495}
2496
Angel Pons7c49cb82020-03-16 23:17:32 +01002497static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002498{
2499 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002500 u32 raw_stats[MAX_EDGE_TIMING + 1];
2501 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002502 const int reg3000b24[] = { 0, 0xc, 0x2c };
2503 int lane, i;
2504 int lower[NUM_LANES];
2505 int upper[NUM_LANES];
2506 int pat;
2507
2508 FOR_ALL_LANES {
2509 lower[lane] = 0;
2510 upper[lane] = MAX_EDGE_TIMING;
2511 }
2512
2513 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002514 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002515 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2516
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002517 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2518 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002520
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002521 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2522 FOR_ALL_LANES {
2523 ctrl->timings[channel][slotrank].lanes[lane].
2524 rising = edge;
2525 ctrl->timings[channel][slotrank].lanes[lane].
2526 falling = edge;
2527 }
2528 program_timings(ctrl, channel);
2529
2530 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002531 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2532 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002533 }
Angel Pons88521882020-01-05 20:21:20 +01002534 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002535
Angel Ponsffd50152020-11-12 11:03:10 +01002536 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002537
Angel Pons7c49cb82020-03-16 23:17:32 +01002538 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002539 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002540
Angel Pons88521882020-01-05 20:21:20 +01002541 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002542 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002543 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002544 }
2545
Angel Pons7c49cb82020-03-16 23:17:32 +01002546 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002547 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002548 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002549
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002550 FOR_ALL_LANES {
2551 struct run rn;
2552 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002553 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2554
2555 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2556
2557 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2558 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2559 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002560 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002561
2562 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2563 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2564
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002565 edges[lane] = (lower[lane] + upper[lane]) / 2;
2566 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002567 printk(BIOS_EMERG, "edge write discovery failed: "
2568 "%d, %d, %d\n", channel, slotrank, lane);
2569
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002570 return MAKE_ERR;
2571 }
2572 }
2573 }
2574 }
2575
Angel Ponsa93f46e2020-11-17 16:54:01 +01002576 /* Restore nominal Vref after training */
2577 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002578 printram("CPA\n");
2579 return 0;
2580}
2581
2582int discover_edges_write(ramctr_timing *ctrl)
2583{
2584 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002585 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2586 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002587
Angel Pons7c49cb82020-03-16 23:17:32 +01002588 /*
2589 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2590 * also use a single loop. It would seem that it is a debugging configuration.
2591 */
Angel Pons88521882020-01-05 20:21:20 +01002592 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2593 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002594
2595 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2596 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002597 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002598 if (err)
2599 return err;
2600 }
2601
Angel Pons88521882020-01-05 20:21:20 +01002602 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2603 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002604
2605 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2606 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002607 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002608 if (err)
2609 return err;
2610 }
2611
Angel Pons88521882020-01-05 20:21:20 +01002612 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002613
2614 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2615 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002616 falling_edges[channel][slotrank][lane];
2617
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002618 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002619 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002620 }
2621
2622 FOR_ALL_POPULATED_CHANNELS
2623 program_timings(ctrl, channel);
2624
Angel Pons50a6fe72020-11-14 01:18:14 +01002625 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002626 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002627 }
2628 return 0;
2629}
2630
2631static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2632{
Angel Pons88521882020-01-05 20:21:20 +01002633 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002634
Angel Ponsffd50152020-11-12 11:03:10 +01002635 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002636
Angel Pons7c49cb82020-03-16 23:17:32 +01002637 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002638 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002639
Angel Pons88521882020-01-05 20:21:20 +01002640 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002641}
2642
2643int discover_timC_write(ramctr_timing *ctrl)
2644{
Angel Pons7c49cb82020-03-16 23:17:32 +01002645 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002646 int i, pat;
2647
2648 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2649 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2650 int channel, slotrank, lane;
2651
2652 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2653 lower[channel][slotrank][lane] = 0;
2654 upper[channel][slotrank][lane] = MAX_TIMC;
2655 }
2656
Angel Pons88521882020-01-05 20:21:20 +01002657 /*
2658 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2659 * FIXME: This must only be done on Ivy Bridge.
2660 */
2661 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002662 printram("discover timC write:\n");
2663
2664 for (i = 0; i < 3; i++)
2665 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002666
2667 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2668 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2669 ~0x3f000000, rege3c_b24[i] << 24);
2670
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002671 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002672
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002673 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2674 FOR_ALL_POPULATED_RANKS {
2675 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002676 u32 raw_stats[MAX_TIMC + 1];
2677 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002678
2679 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002680 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002681
2682 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002683
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002684 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002685 FOR_ALL_LANES {
2686 ctrl->timings[channel][slotrank]
2687 .lanes[lane].timC = timC;
2688 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002689 program_timings(ctrl, channel);
2690
2691 test_timC_write (ctrl, channel, slotrank);
2692
Angel Pons7c49cb82020-03-16 23:17:32 +01002693 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002694 raw_stats[timC] = MCHBAR32(
2695 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002696 }
2697 FOR_ALL_LANES {
2698 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002699 for (timC = 0; timC < MAX_TIMC; timC++) {
2700 stats[timC] = !!(raw_stats[timC]
2701 & (1 << lane));
2702 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002703
Angel Pons7c49cb82020-03-16 23:17:32 +01002704 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002705 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002706 printk(BIOS_EMERG,
2707 "timC write discovery failed: "
2708 "%d, %d, %d\n", channel,
2709 slotrank, lane);
2710
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002711 return MAKE_ERR;
2712 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002713 printram("timC: %d, %d, %d: "
2714 "0x%02x-0x%02x-0x%02x, "
2715 "0x%02x-0x%02x\n", channel, slotrank,
2716 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002717 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002718 rn.end - ctrl->timC_offset[i]);
2719
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002720 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002721 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002722 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002723
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002724 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002725 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002726 upper[channel][slotrank][lane]);
2727
2728 }
2729 }
2730 }
2731 }
2732
2733 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002734 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002735 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002736 udelay(2);
2737 }
2738
Angel Pons88521882020-01-05 20:21:20 +01002739 /*
2740 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2741 * FIXME: This must only be done on Ivy Bridge.
2742 */
2743 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002744
2745 printram("CPB\n");
2746
2747 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002748 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002749 (lower[channel][slotrank][lane] +
2750 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002751
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002752 ctrl->timings[channel][slotrank].lanes[lane].timC =
2753 (lower[channel][slotrank][lane] +
2754 upper[channel][slotrank][lane]) / 2;
2755 }
2756 FOR_ALL_POPULATED_CHANNELS {
2757 program_timings(ctrl, channel);
2758 }
2759 return 0;
2760}
2761
Angel Pons88521882020-01-05 20:21:20 +01002762void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002763{
2764 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002765 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002766
2767 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2768 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002769 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002770 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002771 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002772 printram("normalize %d, %d, %d: mat %d\n",
2773 channel, slotrank, lane, mat);
2774
Felix Heldef4fe3e2019-12-31 14:15:05 +01002775 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002776 printram("normalize %d, %d, %d: delta %d\n",
2777 channel, slotrank, lane, delta);
2778
Angel Pons88521882020-01-05 20:21:20 +01002779 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002780 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002781 }
2782
2783 FOR_ALL_POPULATED_CHANNELS {
2784 program_timings(ctrl, channel);
2785 }
2786}
2787
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002788int channel_test(ramctr_timing *ctrl)
2789{
2790 int channel, slotrank, lane;
2791
2792 slotrank = 0;
2793 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002794 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002795 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002796 return MAKE_ERR;
2797 }
2798 FOR_ALL_POPULATED_CHANNELS {
2799 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002800 }
2801
2802 for (slotrank = 0; slotrank < 4; slotrank++)
2803 FOR_ALL_CHANNELS
2804 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2805 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002806 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2807 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002808 }
Angel Pons88521882020-01-05 20:21:20 +01002809 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002810
Angel Ponsffd50152020-11-12 11:03:10 +01002811 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002812
Angel Pons7c49cb82020-03-16 23:17:32 +01002813 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002814 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002815
Angel Pons88521882020-01-05 20:21:20 +01002816 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002817 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002818 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002819 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2820 channel, slotrank, lane);
2821 return MAKE_ERR;
2822 }
2823 }
2824 return 0;
2825}
2826
Patrick Rudolphdd662872017-10-28 18:20:11 +02002827void channel_scrub(ramctr_timing *ctrl)
2828{
2829 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002830 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002831
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002832 FOR_ALL_POPULATED_CHANNELS {
2833 wait_for_iosav(channel);
2834 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002835 }
2836
2837 /*
2838 * During runtime the "scrubber" will periodically scan through the memory in the
2839 * physical address space, to identify and fix CRC errors.
2840 * The following loops writes to every DRAM address, setting the ECC bits to the
2841 * correct value. A read from this location will no longer return a CRC error,
2842 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002843 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002844 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2845 * and firmware running in x86_32.
2846 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002847 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2848 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002849 for (bank = 0; bank < 8; bank++) {
2850 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002851
Angel Pons8f0757e2020-11-11 23:03:36 +01002852 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2853 const struct iosav_ssq sequence[] = {
2854 /*
2855 * DRAM command ACT
2856 * Opens the row for writing.
2857 */
2858 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002859 .sp_cmd_ctrl = {
2860 .command = IOSAV_ACT,
2861 .ranksel_ap = 1,
2862 },
2863 .subseq_ctrl = {
2864 .cmd_executions = 1,
2865 .cmd_delay_gap = gap,
2866 .post_ssq_wait = ctrl->tRCD,
2867 .data_direction = SSQ_NA,
2868 },
2869 .sp_cmd_addr = {
2870 .address = row,
2871 .rowbits = 6,
2872 .bank = bank,
2873 .rank = slotrank,
2874 },
2875 .addr_update = {
2876 .inc_addr_1 = 1,
2877 .addr_wrap = 18,
2878 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002879 },
2880 /*
2881 * DRAM command WR
2882 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2883 * bytes.
2884 */
2885 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002886 .sp_cmd_ctrl = {
2887 .command = IOSAV_WR,
2888 .ranksel_ap = 1,
2889 },
2890 .subseq_ctrl = {
2891 .cmd_executions = 129,
2892 .cmd_delay_gap = 4,
2893 .post_ssq_wait = ctrl->tWTR +
2894 ctrl->CWL + 8,
2895 .data_direction = SSQ_WR,
2896 },
2897 .sp_cmd_addr = {
2898 .address = row,
2899 .rowbits = 0,
2900 .bank = bank,
2901 .rank = slotrank,
2902 },
2903 .addr_update = {
2904 .inc_addr_8 = 1,
2905 .addr_wrap = 9,
2906 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002907 },
2908 /*
2909 * DRAM command PRE
2910 * Closes the row.
2911 */
2912 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002913 .sp_cmd_ctrl = {
2914 .command = IOSAV_PRE,
2915 .ranksel_ap = 1,
2916 },
2917 .subseq_ctrl = {
2918 .cmd_executions = 1,
2919 .cmd_delay_gap = 4,
2920 .post_ssq_wait = ctrl->tRP,
2921 .data_direction = SSQ_NA,
2922 },
2923 .sp_cmd_addr = {
2924 .address = 0,
2925 .rowbits = 6,
2926 .bank = bank,
2927 .rank = slotrank,
2928 },
2929 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002930 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002931 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002932 },
2933 };
2934 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002935
2936 /* Execute command queue */
2937 iosav_run_queue(channel, 16, 0);
2938
2939 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002940 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002941 }
2942 }
2943}
2944
Angel Pons88521882020-01-05 20:21:20 +01002945void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002946{
2947 int channel;
2948
Angel Pons7c49cb82020-03-16 23:17:32 +01002949 /* 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 +01002950 static u32 seeds[NUM_CHANNELS][3] = {
2951 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2952 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2953 };
2954 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002955 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002956 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2957 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2958 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002959 }
2960}
2961
Angel Pons89ae6b82020-03-21 13:23:32 +01002962void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002963{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002964 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002965 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002966 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002967 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002968 }
2969}
2970
Angel Pons88521882020-01-05 20:21:20 +01002971void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972{
2973 int channel;
2974
2975 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002976 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002977 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002978 }
2979
2980 udelay(1);
2981
2982 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002983 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002984 }
2985}
2986
Angel Pons7c49cb82020-03-16 23:17:32 +01002987void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002988{
2989 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002990
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002991 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002992 int min_pi = 10000;
2993 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002994
2995 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002996 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2997 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002998 }
2999
Angel Pons7a612742020-11-12 13:34:03 +01003000 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003001
Angel Pons7a612742020-11-12 13:34:03 +01003002 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003003
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01003004 dram_odt_stretch(ctrl, channel);
3005
Angel Pons7a612742020-11-12 13:34:03 +01003006 const union tc_rwp_reg tc_rwp = {
3007 .tRRDR = 0,
3008 .tRRDD = val,
3009 .tWWDR = val,
3010 .tWWDD = val,
3011 .tRWDRDD = ctrl->ref_card_offset[channel] + 2,
3012 .tWRDRDD = tWRDRDD,
3013 .tRWSR = 2,
3014 .dec_wrd = 1,
3015 };
3016 MCHBAR32(TC_RWP_ch(channel)) = tc_rwp.raw;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003017 }
3018}
3019
Angel Pons88521882020-01-05 20:21:20 +01003020void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003021{
3022 int channel;
3023 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01003024 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
3025 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003026 }
3027}
3028
Angel Pons7c49cb82020-03-16 23:17:32 +01003029/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
3030static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003031{
Angel Pons88521882020-01-05 20:21:20 +01003032 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003033}
3034
Angel Pons7c49cb82020-03-16 23:17:32 +01003035/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01003036void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003037{
Angel Ponsb50ca572020-11-11 19:07:20 +01003038 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01003039
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003040 int channel;
3041 int t1_cycles = 0, t1_ns = 0, t2_ns;
3042 int t3_ns;
3043 u32 r32;
3044
Angel Pons7c49cb82020-03-16 23:17:32 +01003045 /* FIXME: This register only exists on Ivy Bridge */
3046 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003047
Angel Pons7a612742020-11-12 13:34:03 +01003048 FOR_ALL_CHANNELS {
3049 union tc_othp_reg tc_othp = {
3050 .raw = MCHBAR32(TC_OTHP_ch(channel)),
3051 };
3052 tc_othp.tCPDED = 1;
3053 MCHBAR32(TC_OTHP_ch(channel)) = tc_othp.raw;
3054 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01003055
Patrick Rudolph74203de2017-11-20 11:57:01 +01003056 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003057 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003058 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003059 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003060 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003061 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003062
Felix Heldf9b826a2018-07-30 17:56:52 +02003063 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003064 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003065
Angel Pons88521882020-01-05 20:21:20 +01003066 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3067 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003068
3069 FOR_ALL_CHANNELS {
3070 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003071 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003072 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003073 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003074 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003075 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003076 case 1:
3077 case 4:
3078 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003079 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003080 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003081 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003082 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003083 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003084 break;
3085 }
3086 }
3087
Felix Held50b7ed22019-12-30 20:41:54 +01003088 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003089 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003090 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003091
Angel Pons7a612742020-11-12 13:34:03 +01003092 FOR_ALL_CHANNELS {
3093 union tc_rfp_reg tc_rfp = {
3094 .raw = MCHBAR32(TC_RFP_ch(channel)),
3095 };
3096 tc_rfp.refresh_2x_control = 1;
3097 MCHBAR32(TC_RFP_ch(channel)) = tc_rfp.raw;
3098 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003099
Angel Ponsdc5539f2020-11-12 12:44:25 +01003100 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3101 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003102 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003103
Angel Pons7c49cb82020-03-16 23:17:32 +01003104 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003105 FOR_ALL_POPULATED_CHANNELS
3106 break;
3107
Angel Pons88521882020-01-05 20:21:20 +01003108 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3109 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003110 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003111 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003112 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003113 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003114 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003115 t1_ns += 500;
3116
Angel Pons88521882020-01-05 20:21:20 +01003117 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003118 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003119 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003120 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003121 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003122 t3_ns = 500;
3123 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003124
3125 /* The graphics driver will use these watermark values */
3126 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003127 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003128 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3129 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003130}
3131
Angel Pons88521882020-01-05 20:21:20 +01003132void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003133{
Angel Pons50a6fe72020-11-14 01:18:14 +01003134 int channel, lane;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003135
Angel Pons7c49cb82020-03-16 23:17:32 +01003136 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01003137 const union tc_rap_reg tc_rap = {
3138 .tRRD = ctrl->tRRD,
3139 .tRTP = ctrl->tRTP,
3140 .tCKE = ctrl->tCKE,
3141 .tWTR = ctrl->tWTR,
3142 .tFAW = ctrl->tFAW,
3143 .tWR = ctrl->tWR,
3144 .tCMD = ctrl->cmd_stretch[channel],
3145 };
3146 MCHBAR32(TC_RAP_ch(channel)) = tc_rap.raw;
Angel Pons7c49cb82020-03-16 23:17:32 +01003147 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003148
3149 udelay(1);
3150
3151 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003152 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003153 }
3154
Angel Pons50a6fe72020-11-14 01:18:14 +01003155 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003156 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003157 }
3158
3159 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003160 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003161
3162 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003163 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003164 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003165 }
3166
3167 printram("CPE\n");
3168
Angel Pons88521882020-01-05 20:21:20 +01003169 MCHBAR32(GDCRTRAININGMOD) = 0;
3170 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003171
3172 printram("CP5b\n");
3173
3174 FOR_ALL_POPULATED_CHANNELS {
3175 program_timings(ctrl, channel);
3176 }
3177
3178 u32 reg, addr;
3179
Angel Pons7c49cb82020-03-16 23:17:32 +01003180 /* Poll for RCOMP */
3181 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3182 ;
3183
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003184 do {
Angel Pons88521882020-01-05 20:21:20 +01003185 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003186 } while ((reg & 0x14) == 0);
3187
Angel Pons7c49cb82020-03-16 23:17:32 +01003188 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003189 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003190 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003191
Angel Pons7c49cb82020-03-16 23:17:32 +01003192 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003193 udelay(500);
3194
3195 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003196 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003197 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003198 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003199 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003200 MCHBAR32(addr) = reg;
3201
Angel Pons7c49cb82020-03-16 23:17:32 +01003202 /* Wait 10ns for ranks to settle */
3203 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003204
3205 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3206 MCHBAR32(addr) = reg;
3207
Angel Pons7c49cb82020-03-16 23:17:32 +01003208 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003209 write_reset(ctrl);
3210 }
3211
Angel Pons7c49cb82020-03-16 23:17:32 +01003212 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003213 dram_mrscommands(ctrl);
3214
3215 printram("CP5c\n");
3216
Angel Pons88521882020-01-05 20:21:20 +01003217 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003218
3219 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003220 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003221 udelay(2);
3222 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003223}