blob: c2a30515fb3a043ca8a8161a77412698d7489231 [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>
Angel Pons47a80a02020-12-07 13:15:23 +01006#include <cpu/intel/model_206ax/model_206ax.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02007#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02008#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01009#include <northbridge/intel/sandybridge/chip.h>
10#include <device/pci_def.h>
11#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020012#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020013
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010014#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010015#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010016#include "sandybridge.h"
17
Angel Pons7c49cb82020-03-16 23:17:32 +010018/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010019
20static void sfence(void)
21{
22 asm volatile ("sfence");
23}
24
Angel Pons7c49cb82020-03-16 23:17:32 +010025/* Toggle IO reset bit */
26static void toggle_io_reset(void)
27{
Angel Pons66780a02021-03-26 13:33:22 +010028 u32 r32 = mchbar_read32(MC_INIT_STATE_G);
29 mchbar_write32(MC_INIT_STATE_G, r32 | (1 << 5));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010030 udelay(1);
Angel Pons66780a02021-03-26 13:33:22 +010031 mchbar_write32(MC_INIT_STATE_G, r32 & ~(1 << 5));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
33}
34
35static u32 get_XOVER_CLK(u8 rankmap)
36{
37 return rankmap << 24;
38}
39
40static u32 get_XOVER_CMD(u8 rankmap)
41{
42 u32 reg;
43
Angel Pons7c49cb82020-03-16 23:17:32 +010044 /* Enable xover cmd */
Angel Pons5db1b152020-12-13 16:37:53 +010045 reg = 1 << 14;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010046
Angel Pons7c49cb82020-03-16 23:17:32 +010047 /* Enable xover ctl */
48 if (rankmap & 0x03)
49 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010050
Angel Pons7c49cb82020-03-16 23:17:32 +010051 if (rankmap & 0x0c)
52 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010053
54 return reg;
55}
56
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010057void dram_find_common_params(ramctr_timing *ctrl)
58{
59 size_t valid_dimms;
60 int channel, slot;
61 dimm_info *dimms = &ctrl->info;
62
63 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
64 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010065
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010066 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010067
Angel Ponsafb3d7e2021-03-28 13:43:13 +020068 const struct dimm_attr_ddr3_st *dimm = &dimms->dimm[channel][slot];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010069 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
70 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010071
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010072 valid_dimms++;
73
74 /* Find all possible CAS combinations */
75 ctrl->cas_supported &= dimm->cas_supported;
76
77 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010078 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
79 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
80 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010081 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
82 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010083 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010084 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
85 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
86 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
87 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
88 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030089 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
90 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010091 }
92
93 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010094 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
95
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010096 if (!valid_dimms)
97 die("No valid DIMMs found");
98}
99
Angel Pons88521882020-01-05 20:21:20 +0100100void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100101{
102 u32 reg;
103 int channel;
104
105 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100106 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100107 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100108 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
Angel Pons66780a02021-03-26 13:33:22 +0100109 mchbar_write32(GDCRCKPICODE_ch(channel), reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100110
Angel Pons7c49cb82020-03-16 23:17:32 +0100111 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100112 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100113 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
Angel Pons66780a02021-03-26 13:33:22 +0100114 mchbar_write32(GDCRCMDPICODING_ch(channel), reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100115 }
116}
117
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100118static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100119{
Angel Pons89ae6b82020-03-21 13:23:32 +0100120 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100121
122 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100123 /*
124 * ODT stretch:
125 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
126 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100127 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100128 if (stretch == 2)
129 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100130
Angel Pons88521882020-01-05 20:21:20 +0100131 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Pons66780a02021-03-26 13:33:22 +0100132 mchbar_clrsetbits32(addr, 0xf << 10, stretch << 12 | stretch << 10);
133 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, mchbar_read32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100134 } else {
Angel Pons88521882020-01-05 20:21:20 +0100135 addr = TC_OTHP_ch(channel);
Angel Pons7a612742020-11-12 13:34:03 +0100136 union tc_othp_reg tc_othp = {
Angel Pons66780a02021-03-26 13:33:22 +0100137 .raw = mchbar_read32(addr),
Angel Pons7a612742020-11-12 13:34:03 +0100138 };
139 tc_othp.odt_delay_d0 = stretch;
140 tc_othp.odt_delay_d1 = stretch;
Angel Pons66780a02021-03-26 13:33:22 +0100141 mchbar_write32(addr, tc_othp.raw);
142 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, mchbar_read32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100143 }
144}
145
146void dram_timing_regs(ramctr_timing *ctrl)
147{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100148 int channel;
149
Angel Pons81378062020-11-12 13:46:21 +0100150 /* BIN parameters */
151 const union tc_dbp_reg tc_dbp = {
152 .tRCD = ctrl->tRCD,
153 .tRP = ctrl->tRP,
154 .tAA = ctrl->CAS,
155 .tCWL = ctrl->CWL,
156 .tRAS = ctrl->tRAS,
157 };
158
159 /* Regular access parameters */
160 const union tc_rap_reg tc_rap = {
161 .tRRD = ctrl->tRRD,
162 .tRTP = ctrl->tRTP,
163 .tCKE = ctrl->tCKE,
164 .tWTR = ctrl->tWTR,
165 .tFAW = ctrl->tFAW,
166 .tWR = ctrl->tWR,
167 .tCMD = 3,
168 };
169
170 /* Other parameters */
171 const union tc_othp_reg tc_othp = {
Angel Ponsda437372021-01-24 18:34:51 +0100172 .tXPDLL = MIN(ctrl->tXPDLL, 31),
173 .tXP = MIN(ctrl->tXP, 7),
Angel Pons81378062020-11-12 13:46:21 +0100174 .tAONPD = ctrl->tAONPD,
175 .tCPDED = 2,
Angel Pons2ad03a42020-11-19 11:07:27 +0100176 .tPRPDEN = 1,
Angel Pons81378062020-11-12 13:46:21 +0100177 };
178
179 /*
Angel Ponsda437372021-01-24 18:34:51 +0100180 * If tXP and tXPDLL are very high, they no longer fit in the bitfields
181 * of the TC_OTHP register. If so, we set bits in TC_DTP to compensate.
Angel Pons81378062020-11-12 13:46:21 +0100182 * This can only happen on Ivy Bridge, and when overclocking the RAM.
183 */
184 const union tc_dtp_reg tc_dtp = {
185 .overclock_tXP = ctrl->tXP >= 8,
186 .overclock_tXPDLL = ctrl->tXPDLL >= 32,
187 };
188
189 /*
190 * TC-Refresh timing parameters:
191 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
192 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
193 */
194 const u32 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
195
196 const union tc_rftp_reg tc_rftp = {
197 .tREFI = ctrl->tREFI,
198 .tRFC = ctrl->tRFC,
199 .tREFIx9 = val32 / 1024,
200 };
201
202 /* Self-refresh timing parameters */
203 const union tc_srftp_reg tc_srftp = {
204 .tXSDLL = tDLLK,
205 .tXS_offset = ctrl->tXSOffset,
206 .tZQOPER = tDLLK - ctrl->tXSOffset,
207 .tMOD = ctrl->tMOD - 8,
208 };
209
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100210 FOR_ALL_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +0100211 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), tc_dbp.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100212 mchbar_write32(TC_DBP_ch(channel), tc_dbp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100213
Angel Pons7a612742020-11-12 13:34:03 +0100214 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), tc_rap.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100215 mchbar_write32(TC_RAP_ch(channel), tc_rap.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100216
Angel Pons7a612742020-11-12 13:34:03 +0100217 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), tc_othp.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100218 mchbar_write32(TC_OTHP_ch(channel), tc_othp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100219
Angel Ponsca2f68a2020-03-22 13:15:12 +0100220 if (IS_IVY_CPU(ctrl->cpu)) {
Angel Pons81378062020-11-12 13:46:21 +0100221 /* Debug parameters - only applies to Ivy Bridge */
Angel Pons66780a02021-03-26 13:33:22 +0100222 mchbar_write32(TC_DTP_ch(channel), tc_dtp.raw);
Angel Ponsca2f68a2020-03-22 13:15:12 +0100223 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100224
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100225 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100226
Angel Pons7a612742020-11-12 13:34:03 +0100227 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), tc_rftp.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100228 mchbar_write32(TC_RFTP_ch(channel), tc_rftp.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +0100229
Angel Pons7a612742020-11-12 13:34:03 +0100230 union tc_rfp_reg tc_rfp = {
Angel Pons66780a02021-03-26 13:33:22 +0100231 .raw = mchbar_read32(TC_RFP_ch(channel)),
Angel Pons7a612742020-11-12 13:34:03 +0100232 };
233 tc_rfp.oref_ri = 0xff;
Angel Pons66780a02021-03-26 13:33:22 +0100234 mchbar_write32(TC_RFP_ch(channel), tc_rfp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100235
Angel Pons7a612742020-11-12 13:34:03 +0100236 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), tc_srftp.raw);
Angel Pons66780a02021-03-26 13:33:22 +0100237 mchbar_write32(TC_SRFTP_ch(channel), tc_srftp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100238 }
239}
240
241void dram_dimm_mapping(ramctr_timing *ctrl)
242{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100243 int channel;
244 dimm_info *info = &ctrl->info;
245
246 FOR_ALL_CHANNELS {
Angel Ponsafb3d7e2021-03-28 13:43:13 +0200247 struct dimm_attr_ddr3_st *dimmA, *dimmB;
Nico Huberac4f2162017-10-01 18:14:43 +0200248 u32 reg = 0;
249
Angel Pons7c49cb82020-03-16 23:17:32 +0100250 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100251 dimmA = &info->dimm[channel][0];
252 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100253 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100254 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100255 dimmA = &info->dimm[channel][1];
256 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100257 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100258 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100259
Nico Huberac4f2162017-10-01 18:14:43 +0200260 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100261 reg |= (dimmA->size_mb / 256) << 0;
262 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200263 reg |= (dimmA->width / 8 - 1) << 19;
264 }
265
266 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100267 reg |= (dimmB->size_mb / 256) << 8;
268 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200269 reg |= (dimmB->width / 8 - 1) << 20;
270 }
271
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200272 /*
273 * Rank interleave: Bit 16 of the physical address space sets
274 * the rank to use in a dual single rank DIMM configuration.
275 * That results in every 64KiB being interleaved between two ranks.
276 */
277 reg |= 1 << 21;
278 /* Enhanced interleave */
279 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200280
Angel Pons7c49cb82020-03-16 23:17:32 +0100281 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100282 ctrl->mad_dimm[channel] = reg;
283 } else {
284 ctrl->mad_dimm[channel] = 0;
285 }
286 }
287}
288
Patrick Rudolphdd662872017-10-28 18:20:11 +0200289void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100290{
291 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200292 u32 ecc;
293
294 if (ctrl->ecc_enabled)
295 ecc = training ? (1 << 24) : (3 << 24);
296 else
297 ecc = 0;
298
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100299 FOR_ALL_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +0100300 mchbar_write32(MAD_DIMM(channel), ctrl->mad_dimm[channel] | ecc);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100301 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200302
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200303 if (ctrl->ecc_enabled)
304 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100305}
306
Angel Pons88521882020-01-05 20:21:20 +0100307void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100308{
309 u32 reg, ch0size, ch1size;
310 u8 val;
311 reg = 0;
312 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100313
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100314 if (training) {
315 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
316 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
317 } else {
318 ch0size = ctrl->channel_size_mb[0];
319 ch1size = ctrl->channel_size_mb[1];
320 }
321
322 if (ch0size >= ch1size) {
Angel Pons66780a02021-03-26 13:33:22 +0100323 reg = mchbar_read32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100324 val = ch1size / 256;
325 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100326 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons66780a02021-03-26 13:33:22 +0100327 mchbar_write32(MAD_ZR, reg);
328 mchbar_write32(MAD_CHNL, 0x24);
Angel Pons7c49cb82020-03-16 23:17:32 +0100329
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100330 } else {
Angel Pons66780a02021-03-26 13:33:22 +0100331 reg = mchbar_read32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100332 val = ch0size / 256;
333 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100334 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons66780a02021-03-26 13:33:22 +0100335 mchbar_write32(MAD_ZR, reg);
336 mchbar_write32(MAD_CHNL, 0x21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100337 }
338}
339
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200340/*
341 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
342 * The ME/PCU/.. has the ability to change this.
343 * Return 0: ECC is optional
344 * Return 1: ECC is forced
345 */
346bool get_host_ecc_forced(void)
347{
348 /* read Capabilities A Register */
349 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
350 return !!(reg32 & (1 << 24));
351}
352
353/*
354 * Returns the ECC capability.
355 * The ME/PCU/.. has the ability to change this.
356 * Return 0: ECC is disabled
357 * Return 1: ECC is possible
358 */
359bool get_host_ecc_cap(void)
360{
361 /* read Capabilities A Register */
362 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
363 return !(reg32 & (1 << 25));
364}
365
Angel Pons5304ce12021-04-02 22:55:00 +0200366#define DEFAULT_PCI_MMIO_SIZE 2048
367
Angel Pons88521882020-01-05 20:21:20 +0100368void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100369{
Angel Pons7c49cb82020-03-16 23:17:32 +0100370 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
371 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
372 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100373 uint16_t ggc;
374
Angel Pons5304ce12021-04-02 22:55:00 +0200375 mmiosize = DEFAULT_PCI_MMIO_SIZE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100376
Felix Held87ddea22020-01-26 04:55:27 +0100377 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100378 if (!(ggc & 2)) {
379 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100380 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100381 } else {
382 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100383 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100384 }
385
386 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
387
388 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
389
390 mestolenbase = tom - me_uma_size;
391
Angel Pons7c49cb82020-03-16 23:17:32 +0100392 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
393
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100394 gfxstolenbase = toludbase - gfxstolen;
395 gttbase = gfxstolenbase - gttsize;
396
397 tsegbase = gttbase - tsegsize;
398
Angel Pons7c49cb82020-03-16 23:17:32 +0100399 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100400 tsegbasedelta = tsegbase & (tsegsize - 1);
401 tsegbase &= ~(tsegsize - 1);
402
403 gttbase -= tsegbasedelta;
404 gfxstolenbase -= tsegbasedelta;
405 toludbase -= tsegbasedelta;
406
Angel Pons7c49cb82020-03-16 23:17:32 +0100407 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100408 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100409 /* Reclaim is possible */
410 reclaim = 1;
411 remapbase = MAX(4096, tom - me_uma_size);
412 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
413 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100414 } else {
Angel Ponsc728e252021-01-03 16:47:09 +0100415 /* Reclaim not possible */
Angel Pons7c49cb82020-03-16 23:17:32 +0100416 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100417 touudbase = tom - me_uma_size;
418 }
419
Angel Pons7c49cb82020-03-16 23:17:32 +0100420 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100421 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
422
Angel Pons7c49cb82020-03-16 23:17:32 +0100423 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100424 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100425 val = tom & 0xfff;
426 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100427 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100428 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100429
Angel Ponsb31d1d72020-01-10 01:35:09 +0100430 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100431 val = tom & 0xfffff000;
432 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100433 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100434 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100435
Angel Pons7c49cb82020-03-16 23:17:32 +0100436 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100437 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100438 val = toludbase & 0xfff;
439 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100440 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442
Angel Pons7c49cb82020-03-16 23:17:32 +0100443 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100444 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100445 val = touudbase & 0xfff;
446 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100447 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100448 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100449
Angel Pons7c49cb82020-03-16 23:17:32 +0100450 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100451 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100452 val = touudbase & 0xfffff000;
453 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100454 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100455 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100456
457 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100458 /* REMAP BASE */
459 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100460 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100461
Angel Pons7c49cb82020-03-16 23:17:32 +0100462 /* REMAP LIMIT */
463 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100464 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100465 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100466 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100467 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100468 val = tsegbase & 0xfff;
469 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100470 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100471 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100472
Angel Pons7c49cb82020-03-16 23:17:32 +0100473 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100474 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100475 val = gfxstolenbase & 0xfff;
476 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100477 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100478 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100479
Angel Pons7c49cb82020-03-16 23:17:32 +0100480 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100481 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100482 val = gttbase & 0xfff;
483 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100484 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100485 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100486
487 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100488 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100489 val = (0x80000 - me_uma_size) & 0xfffff000;
490 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100491 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100492 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100493
Angel Pons7c49cb82020-03-16 23:17:32 +0100494 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100495 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100496 val = mestolenbase & 0xfff;
497 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100498 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100499 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100500
Angel Ponsb31d1d72020-01-10 01:35:09 +0100501 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100502 val = mestolenbase & 0xfffff000;
503 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100504 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100505 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100506
Angel Pons7c49cb82020-03-16 23:17:32 +0100507 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100508 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100509 val = (0x80000 - me_uma_size) & 0xfff;
510 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100511 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
512 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100513 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100514 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100515 }
516}
517
Angel Pons88521882020-01-05 20:21:20 +0100518static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100519{
520 int channel, slotrank;
521
Angel Pons7c49cb82020-03-16 23:17:32 +0100522 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100523 channel = (ctrl->rankmap[0]) ? 0 : 1;
524
Angel Pons88521882020-01-05 20:21:20 +0100525 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526
Angel Pons7c49cb82020-03-16 23:17:32 +0100527 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100528 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
529
Angel Ponsffd50152020-11-12 11:03:10 +0100530 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100531
Angel Ponsedd7cb42020-12-07 12:17:17 +0100532 /* This is actually using the IOSAV state machine as a timer */
Angel Pons38d901e2020-05-02 23:50:43 +0200533 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200534
Angel Pons88521882020-01-05 20:21:20 +0100535 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100536}
537
Angel Pons88521882020-01-05 20:21:20 +0100538void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100539{
Felix Held9fe248f2018-07-31 20:59:45 +0200540 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100541 int channel;
542
Angel Pons66780a02021-03-26 13:33:22 +0100543 while (!(mchbar_read32(RCOMP_TIMER) & (1 << 16)))
Angel Pons7c49cb82020-03-16 23:17:32 +0100544 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100545 do {
Angel Pons66780a02021-03-26 13:33:22 +0100546 reg = mchbar_read32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100547 } while ((reg & 0x14) == 0);
548
Angel Pons7c49cb82020-03-16 23:17:32 +0100549 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100550 reg = 0x112;
Angel Pons66780a02021-03-26 13:33:22 +0100551 mchbar_write32(MC_INIT_STATE_G, reg);
552 mchbar_write32(MC_INIT_STATE, 0);
Angel Pons7c49cb82020-03-16 23:17:32 +0100553 reg |= 2; /* DDR reset */
Angel Pons66780a02021-03-26 13:33:22 +0100554 mchbar_write32(MC_INIT_STATE_G, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100555
Angel Pons7c49cb82020-03-16 23:17:32 +0100556 /* Assert DIMM reset signal */
Angel Pons66780a02021-03-26 13:33:22 +0100557 mchbar_clrbits32(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100558
Angel Pons7c49cb82020-03-16 23:17:32 +0100559 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560 udelay(200);
561
Angel Pons7c49cb82020-03-16 23:17:32 +0100562 /* Deassert DIMM reset signal */
Angel Pons66780a02021-03-26 13:33:22 +0100563 mchbar_setbits32(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100564
Angel Pons7c49cb82020-03-16 23:17:32 +0100565 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566 udelay(500);
567
Angel Pons7c49cb82020-03-16 23:17:32 +0100568 /* Enable DCLK */
Angel Pons66780a02021-03-26 13:33:22 +0100569 mchbar_setbits32(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100570
Angel Pons7c49cb82020-03-16 23:17:32 +0100571 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100572 udelay(1);
573
574 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100575 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200576 reg = ctrl->rankmap[channel];
Angel Pons66780a02021-03-26 13:33:22 +0100577 mchbar_write32(MC_INIT_STATE_ch(channel), reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100578
Angel Pons7c49cb82020-03-16 23:17:32 +0100579 /* Wait 10ns for ranks to settle */
580 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581
582 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons66780a02021-03-26 13:33:22 +0100583 mchbar_write32(MC_INIT_STATE_ch(channel), reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100584
Angel Pons7c49cb82020-03-16 23:17:32 +0100585 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100586 write_reset(ctrl);
587 }
588}
589
Angel Pons3d3bf482020-11-14 16:18:15 +0100590/*
591 * DDR3 Rank1 Address mirror swap the following pins:
592 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
593 */
594static void ddr3_mirror_mrreg(int *bank, u32 *addr)
595{
596 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
597 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
598}
599
Angel Pons7c49cb82020-03-16 23:17:32 +0100600static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100601{
Angel Pons88521882020-01-05 20:21:20 +0100602 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100603
Angel Pons3d3bf482020-11-14 16:18:15 +0100604 if (ctrl->rank_mirror[channel][slotrank])
605 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100606
Angel Pons8f0757e2020-11-11 23:03:36 +0100607 const struct iosav_ssq sequence[] = {
608 /* DRAM command MRS */
609 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200610 .sp_cmd_ctrl = {
611 .command = IOSAV_MRS,
612 },
613 .subseq_ctrl = {
614 .cmd_executions = 1,
615 .cmd_delay_gap = 4,
616 .post_ssq_wait = 4,
617 .data_direction = SSQ_NA,
618 },
619 .sp_cmd_addr = {
620 .address = val,
621 .rowbits = 6,
622 .bank = reg,
623 .rank = slotrank,
624 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100625 },
626 /* DRAM command MRS */
627 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200628 .sp_cmd_ctrl = {
629 .command = IOSAV_MRS,
630 .ranksel_ap = 1,
631 },
632 .subseq_ctrl = {
633 .cmd_executions = 1,
634 .cmd_delay_gap = 4,
635 .post_ssq_wait = 4,
636 .data_direction = SSQ_NA,
637 },
638 .sp_cmd_addr = {
639 .address = val,
640 .rowbits = 6,
641 .bank = reg,
642 .rank = slotrank,
643 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100644 },
645 /* DRAM command MRS */
646 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200647 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100648 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200649 },
650 .subseq_ctrl = {
651 .cmd_executions = 1,
652 .cmd_delay_gap = 4,
653 .post_ssq_wait = ctrl->tMOD,
654 .data_direction = SSQ_NA,
655 },
656 .sp_cmd_addr = {
657 .address = val,
658 .rowbits = 6,
659 .bank = reg,
660 .rank = slotrank,
661 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100662 },
663 };
664 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200665
Angel Pons9f4ed3b2020-12-07 12:34:36 +0100666 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100667}
668
Angel Pons09fc4b92020-11-19 12:02:07 +0100669/* Obtain optimal power down mode for current configuration */
Angel Ponsae5e6362021-03-28 14:52:38 +0200670static enum power_down_mode get_power_down_mode(ramctr_timing *ctrl)
Angel Pons09fc4b92020-11-19 12:02:07 +0100671{
672 if (ctrl->tXP > 8)
673 return PDM_NONE;
674
675 if (ctrl->tXPDLL > 32)
676 return PDM_PPD;
677
678 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
679 return PDM_DLL_OFF;
680
681 return PDM_APD_PPD;
682}
683
Angel Pons88521882020-01-05 20:21:20 +0100684static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100685{
686 u16 mr0reg, mch_cas, mch_wr;
687 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Angel Pons09fc4b92020-11-19 12:02:07 +0100688
Angel Ponsae5e6362021-03-28 14:52:38 +0200689 const enum power_down_mode power_down = get_power_down_mode(ctrl);
Angel Pons09fc4b92020-11-19 12:02:07 +0100690
691 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100692
Angel Pons7c49cb82020-03-16 23:17:32 +0100693 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100694 if (ctrl->CAS < 12) {
Elyes Haouas3a998072022-11-18 15:11:02 +0100695 mch_cas = (u16)((ctrl->CAS - 4) << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100696 } else {
Elyes Haouas3a998072022-11-18 15:11:02 +0100697 mch_cas = (u16)(ctrl->CAS - 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100698 mch_cas = ((mch_cas << 1) | 0x1);
699 }
700
Angel Pons7c49cb82020-03-16 23:17:32 +0100701 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100702 mch_wr = mch_wr_t[ctrl->tWR - 5];
703
Angel Pons2bf28ed2020-11-12 13:49:59 +0100704 /* DLL Reset - self clearing - set after CLK frequency has been changed */
705 mr0reg = 1 << 8;
706
707 mr0reg |= (mch_cas & 0x1) << 2;
708 mr0reg |= (mch_cas & 0xe) << 3;
709 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100710
Angel Pons09fc4b92020-11-19 12:02:07 +0100711 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
712 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100713 return mr0reg;
714}
715
716static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
717{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200718 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100719}
720
Angel Ponsf9997482020-11-12 16:02:52 +0100721static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100722{
723 /* Get ODT based on rankmap */
724 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
725
726 if (dimms_per_ch == 1) {
727 return (const odtmap){60, 60};
728 } else {
729 return (const odtmap){120, 30};
730 }
731}
732
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100733static u32 encode_odt(u32 odt)
734{
735 switch (odt) {
736 case 30:
Angel Ponsc728e252021-01-03 16:47:09 +0100737 return (1 << 9) | (1 << 2); /* RZQ/8, RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100738 case 60:
Angel Ponsc728e252021-01-03 16:47:09 +0100739 return (1 << 2); /* RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100740 case 120:
Angel Ponsc728e252021-01-03 16:47:09 +0100741 return (1 << 6); /* RZQ/2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100742 default:
743 case 0:
744 return 0;
745 }
746}
747
748static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
749{
750 odtmap odt;
751 u32 mr1reg;
752
Angel Ponsf9997482020-11-12 16:02:52 +0100753 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100754 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100755
756 mr1reg |= encode_odt(odt.rttnom);
757
758 return mr1reg;
759}
760
761static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
762{
763 u16 mr1reg;
764
765 mr1reg = make_mr1(ctrl, rank, channel);
766
767 write_mrreg(ctrl, channel, rank, 1, mr1reg);
768}
769
770static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
771{
Angel Pons868bca22020-11-13 13:38:04 +0100772 const u16 pasr = 0;
773 const u16 cwl = ctrl->CWL - 5;
774 const odtmap odt = get_ODT(ctrl, channel);
775
Angel Ponsdca3cb52020-11-13 13:42:07 +0100776 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100777 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
778 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100779
Angel Pons868bca22020-11-13 13:38:04 +0100780 u16 mr2reg = 0;
781 mr2reg |= pasr;
782 mr2reg |= cwl << 3;
783 mr2reg |= ctrl->auto_self_refresh << 6;
784 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100785 mr2reg |= (odt.rttwr / 60) << 9;
786
787 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100788
789 /* Program MR2 shadow */
Angel Pons66780a02021-03-26 13:33:22 +0100790 u32 reg32 = mchbar_read32(TC_MR2_SHADOW_ch(channel));
Angel Pons7f1363d2020-11-13 13:31:58 +0100791
792 reg32 &= 3 << 14 | 3 << 6;
793
794 reg32 |= mr2reg & ~(3 << 6);
795
Angel Pons927b1c02020-12-10 22:11:27 +0100796 if (srt)
797 reg32 |= 1 << (rank / 2 + 6);
798
799 if (ctrl->rank_mirror[channel][rank])
800 reg32 |= 1 << (rank / 2 + 14);
801
Angel Pons66780a02021-03-26 13:33:22 +0100802 mchbar_write32(TC_MR2_SHADOW_ch(channel), reg32);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100803}
804
805static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
806{
807 write_mrreg(ctrl, channel, rank, 3, 0);
808}
809
Angel Pons88521882020-01-05 20:21:20 +0100810void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100811{
812 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100813 int channel;
814
815 FOR_ALL_POPULATED_CHANNELS {
816 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100817 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100818 dram_mr2(ctrl, slotrank, channel);
819
Angel Pons7c49cb82020-03-16 23:17:32 +0100820 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100821 dram_mr3(ctrl, slotrank, channel);
822
Angel Pons7c49cb82020-03-16 23:17:32 +0100823 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100824 dram_mr1(ctrl, slotrank, channel);
825
Angel Pons7c49cb82020-03-16 23:17:32 +0100826 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100827 dram_mr0(ctrl, slotrank, channel);
828 }
829 }
830
Angel Pons8f0757e2020-11-11 23:03:36 +0100831 const struct iosav_ssq zqcl_sequence[] = {
832 /* DRAM command NOP (without ODT nor chip selects) */
833 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200834 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100835 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200836 },
837 .subseq_ctrl = {
838 .cmd_executions = 1,
839 .cmd_delay_gap = 4,
840 .post_ssq_wait = 15,
841 .data_direction = SSQ_NA,
842 },
843 .sp_cmd_addr = {
844 .address = 2,
845 .rowbits = 6,
846 .bank = 0,
847 .rank = 0,
848 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100849 },
850 /* DRAM command ZQCL */
851 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200852 .sp_cmd_ctrl = {
853 .command = IOSAV_ZQCS,
854 .ranksel_ap = 1,
855 },
856 .subseq_ctrl = {
857 .cmd_executions = 1,
858 .cmd_delay_gap = 4,
859 .post_ssq_wait = 400,
860 .data_direction = SSQ_NA,
861 },
862 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +0100863 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +0200864 .rowbits = 6,
865 .bank = 0,
866 .rank = 0,
867 },
868 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100869 .inc_rank = 1,
870 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200871 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100872 },
873 };
874 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100875
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 Pons88521882020-01-05 20:21:20 +0100879 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100880 }
881
Angel Pons7c49cb82020-03-16 23:17:32 +0100882 /* Refresh enable */
Angel Pons66780a02021-03-26 13:33:22 +0100883 mchbar_setbits32(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100884
885 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +0100886 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100887
Angel Pons88521882020-01-05 20:21:20 +0100888 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100889
890 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
891
Angel Pons88521882020-01-05 20:21:20 +0100892 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100893
Angel Ponsffd50152020-11-12 11:03:10 +0100894 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200895
Angel Ponsa853e7a2020-12-07 12:28:38 +0100896 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100897 }
898}
899
Felix Held3b906032020-01-14 17:05:43 +0100900static const u32 lane_base[] = {
901 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
902 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
903 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100904};
905
Angel Pons42d033a2021-01-03 15:26:37 +0100906/* Maximum delay for command, control, clock */
907#define CCC_MAX_PI (2 * QCLK_PI - 1)
908
Angel Pons88521882020-01-05 20:21:20 +0100909void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100910{
Angel Pons7584e552020-11-19 21:34:32 +0100911 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100912 int lane;
913 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100914
Angel Pons7584e552020-11-19 21:34:32 +0100915 u32 ctl_delay[NUM_SLOTS] = { 0 };
916 int cmd_delay = 0;
917
918 /* Enable CLK XOVER */
919 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
920 u32 clk_logic_dly = 0;
921
922 /*
Angel Pons7519ca42021-01-12 01:21:24 +0100923 * Compute command timing as abs() of the most negative PI code
924 * across all ranks. Use zero if none of the values is negative.
Angel Pons7584e552020-11-19 21:34:32 +0100925 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100926 FOR_ALL_POPULATED_RANKS {
Angel Pons7519ca42021-01-12 01:21:24 +0100927 cmd_delay = MAX(cmd_delay, -ctrl->timings[channel][slotrank].pi_coding);
Angel Pons7584e552020-11-19 21:34:32 +0100928 }
Angel Pons42d033a2021-01-03 15:26:37 +0100929 if (cmd_delay > CCC_MAX_PI) {
Angel Pons7584e552020-11-19 21:34:32 +0100930 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
Angel Pons42d033a2021-01-03 15:26:37 +0100931 cmd_delay = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100932 }
933
Angel Pons89200d22021-01-12 01:04:04 +0100934 for (slot = 0; slot < NUM_SLOTS; slot++) {
935 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
936 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
Angel Pons7584e552020-11-19 21:34:32 +0100937
Angel Pons89200d22021-01-12 01:04:04 +0100938 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
Angel Pons7584e552020-11-19 21:34:32 +0100939
Angel Pons89200d22021-01-12 01:04:04 +0100940 if (slot_map & 1)
941 ctl_delay[slot] += pi_coding_0 + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100942
Angel Pons89200d22021-01-12 01:04:04 +0100943 if (slot_map & 2)
944 ctl_delay[slot] += pi_coding_1 + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100945
Angel Pons89200d22021-01-12 01:04:04 +0100946 /* If both ranks in a slot are populated, use the average */
947 if (slot_map == 3)
948 ctl_delay[slot] /= 2;
Angel Pons7584e552020-11-19 21:34:32 +0100949
Angel Pons89200d22021-01-12 01:04:04 +0100950 if (ctl_delay[slot] > CCC_MAX_PI) {
951 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
952 channel, slot, ctl_delay[slot]);
953 ctl_delay[slot] = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100954 }
Angel Pons89200d22021-01-12 01:04:04 +0100955 }
956 FOR_ALL_POPULATED_RANKS {
Angel Pons0a7d99c2021-01-12 01:13:08 +0100957 int clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100958
Angel Pons0a7d99c2021-01-12 01:13:08 +0100959 /*
960 * Clock is a differential signal, whereas command and control are not.
961 * This affects its timing, and it is also why it needs a magic offset.
962 */
963 clk_delay += ctrl->pi_code_offset;
964
965 /* Can never happen with valid values */
966 if (clk_delay < 0) {
967 printk(BIOS_ERR, "C%dR%d clock delay underflow: %d\n",
Angel Pons89200d22021-01-12 01:04:04 +0100968 channel, slotrank, clk_delay);
Angel Pons0a7d99c2021-01-12 01:13:08 +0100969 clk_delay = 0;
Angel Pons7584e552020-11-19 21:34:32 +0100970 }
Angel Pons89200d22021-01-12 01:04:04 +0100971
Angel Pons0a7d99c2021-01-12 01:13:08 +0100972 /* Clock can safely wrap around because it is a periodic signal */
973 clk_delay %= CCC_MAX_PI + 1;
974
Angel Pons89200d22021-01-12 01:04:04 +0100975 clk_pi_coding |= (clk_delay % QCLK_PI) << (6 * slotrank);
976 clk_logic_dly |= (clk_delay / QCLK_PI) << slotrank;
Angel Pons7584e552020-11-19 21:34:32 +0100977 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100978
Angel Pons7c49cb82020-03-16 23:17:32 +0100979 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100980 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
981 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
982 };
Angel Pons42d033a2021-01-03 15:26:37 +0100983 cmd_pi_coding.cmd_pi_code = cmd_delay % QCLK_PI;
984 cmd_pi_coding.cmd_logic_delay = cmd_delay / QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100985
Angel Pons42d033a2021-01-03 15:26:37 +0100986 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % QCLK_PI;
987 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % QCLK_PI;
988 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / QCLK_PI;
989 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / QCLK_PI;
Angel Pons737f1112020-11-13 14:07:30 +0100990
Angel Pons66780a02021-03-26 13:33:22 +0100991 mchbar_write32(GDCRCMDPICODING_ch(channel), cmd_pi_coding.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100992
Angel Pons66780a02021-03-26 13:33:22 +0100993 mchbar_write32(GDCRCKPICODE_ch(channel), clk_pi_coding);
994 mchbar_write32(GDCRCKLOGICDELAY_ch(channel), clk_logic_dly);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100995
Angel Pons66780a02021-03-26 13:33:22 +0100996 reg_io_latency = mchbar_read32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100997 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100998
Angel Pons88521882020-01-05 20:21:20 +0100999 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001000
1001 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001002 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001003
Angel Pons88521882020-01-05 20:21:20 +01001004 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001005 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001006
1007 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001008 const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
1009 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p;
1010 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n;
Angel Pons9fcc1102020-11-19 22:23:13 +01001011 const union gdcr_rx_reg gdcr_rx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001012 .rcven_pi_code = rcven % QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001013 .rx_dqs_p_pi_code = dqs_p,
Angel Pons42d033a2021-01-03 15:26:37 +01001014 .rcven_logic_delay = rcven / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001015 .rx_dqs_n_pi_code = dqs_n,
1016 };
Angel Pons66780a02021-03-26 13:33:22 +01001017 mchbar_write32(lane_base[lane] + GDCRRX(channel, slotrank),
1018 gdcr_rx.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001019
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001020 const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1021 const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq;
Angel Pons9fcc1102020-11-19 22:23:13 +01001022 const union gdcr_tx_reg gdcr_tx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001023 .tx_dq_pi_code = tx_dq % QCLK_PI,
1024 .tx_dqs_pi_code = tx_dqs % QCLK_PI,
1025 .tx_dqs_logic_delay = tx_dqs / QCLK_PI,
1026 .tx_dq_logic_delay = tx_dq / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001027 };
Angel Pons66780a02021-03-26 13:33:22 +01001028 mchbar_write32(lane_base[lane] + GDCRTX(channel, slotrank),
1029 gdcr_tx.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001030 }
1031 }
Angel Pons66780a02021-03-26 13:33:22 +01001032 mchbar_write32(SC_ROUNDT_LAT_ch(channel), reg_roundtrip_latency);
1033 mchbar_write32(SC_IO_LATENCY_ch(channel), reg_io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001034}
1035
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001036static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001037{
Angel Pons88521882020-01-05 20:21:20 +01001038 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001039
Angel Pons3aed6ac2020-12-07 02:00:41 +01001040 /* Send a burst of 16 back-to-back read commands (4 DCLK apart) */
Angel Ponsffd50152020-11-12 11:03:10 +01001041 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001042
Angel Ponsa853e7a2020-12-07 12:28:38 +01001043 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001044}
1045
Angel Pons7c49cb82020-03-16 23:17:32 +01001046static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001047{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001048 u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
Angel Pons7c49cb82020-03-16 23:17:32 +01001049
Angel Pons66780a02021-03-26 13:33:22 +01001050 return (mchbar_read32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001051 GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001052}
1053
1054struct run {
1055 int middle;
1056 int end;
1057 int start;
1058 int all;
1059 int length;
1060};
1061
1062static struct run get_longest_zero_run(int *seq, int sz)
1063{
1064 int i, ls;
1065 int bl = 0, bs = 0;
1066 struct run ret;
1067
1068 ls = 0;
1069 for (i = 0; i < 2 * sz; i++)
1070 if (seq[i % sz]) {
1071 if (i - ls > bl) {
1072 bl = i - ls;
1073 bs = ls;
1074 }
1075 ls = i + 1;
1076 }
1077 if (bl == 0) {
1078 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001079 ret.start = 0;
1080 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001081 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001082 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001083 return ret;
1084 }
1085
Angel Pons7c49cb82020-03-16 23:17:32 +01001086 ret.start = bs % sz;
1087 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001088 ret.middle = (bs + (bl - 1) / 2) % sz;
1089 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001090 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001091
1092 return ret;
1093}
1094
Angel Pons42d033a2021-01-03 15:26:37 +01001095#define RCVEN_COARSE_PI_LENGTH (2 * QCLK_PI)
1096
Angel Ponsf3053392020-11-13 23:31:12 +01001097static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001098{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001099 int rcven;
Angel Pons42d033a2021-01-03 15:26:37 +01001100 int statistics[NUM_LANES][RCVEN_COARSE_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001101 int lane;
1102
Angel Pons42d033a2021-01-03 15:26:37 +01001103 for (rcven = 0; rcven < RCVEN_COARSE_PI_LENGTH; rcven++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001104 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001105 ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001106 }
1107 program_timings(ctrl, channel);
1108
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001109 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001110
1111 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001112 statistics[lane][rcven] =
1113 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001114 }
1115 }
1116 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001117 struct run rn = get_longest_zero_run(statistics[lane], RCVEN_COARSE_PI_LENGTH);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001118 ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001119 upperA[lane] = rn.end;
1120 if (upperA[lane] < rn.middle)
Angel Pons42d033a2021-01-03 15:26:37 +01001121 upperA[lane] += 2 * QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001122
Angel Pons7e439c92020-12-07 11:56:01 +01001123 printram("rcven: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001124 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001125 }
1126}
1127
Angel Ponsf3053392020-11-13 23:31:12 +01001128static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001129{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001130 int rcven_delta;
Angel Pons86e3d742021-01-03 14:55:12 +01001131 int statistics[NUM_LANES][51] = {0};
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001132 int lane, i;
1133
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001134 for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001135
1136 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001137 ctrl->timings[channel][slotrank].lanes[lane].rcven
Angel Pons42d033a2021-01-03 15:26:37 +01001138 = upperA[lane] + rcven_delta + QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001139 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001140 program_timings(ctrl, channel);
1141
1142 for (i = 0; i < 100; i++) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001143 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001144 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001145 statistics[lane][rcven_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001146 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001147 }
1148 }
1149 }
1150 FOR_ALL_LANES {
1151 int last_zero, first_all;
1152
1153 for (last_zero = -25; last_zero <= 25; last_zero++)
1154 if (statistics[lane][last_zero + 25])
1155 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001156
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001157 last_zero--;
1158 for (first_all = -25; first_all <= 25; first_all++)
1159 if (statistics[lane][first_all + 25] == 100)
1160 break;
1161
Angel Pons7c49cb82020-03-16 23:17:32 +01001162 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001163
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001164 ctrl->timings[channel][slotrank].lanes[lane].rcven =
Angel Pons7c49cb82020-03-16 23:17:32 +01001165 (last_zero + first_all) / 2 + upperA[lane];
1166
Angel Pons7e439c92020-12-07 11:56:01 +01001167 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001168 lane, ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001169 }
1170}
1171
Angel Pons3aed6ac2020-12-07 02:00:41 +01001172/*
1173 * Once the DQS high phase has been found (for each DRAM) the next stage
1174 * is to find out the round trip latency, by locating the preamble cycle.
1175 * This is achieved by trying smaller and smaller roundtrip values until
1176 * the strobe sampling is done on the preamble cycle.
1177 */
Angel Ponsf3053392020-11-13 23:31:12 +01001178static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001179{
1180 int works[NUM_LANES];
1181 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001182
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001183 while (1) {
1184 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001185
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001186 program_timings(ctrl, channel);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001187 test_rcven(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001188
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001189 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001190 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1191
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001192 if (works[lane])
1193 some_works = 1;
1194 else
1195 all_works = 0;
1196 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001197
1198 /* If every lane is working, exit */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001199 if (all_works)
1200 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001201
Angel Pons3aed6ac2020-12-07 02:00:41 +01001202 /*
1203 * If all bits are one (everyone is failing), decrement
1204 * the roundtrip value by two, and do another iteration.
1205 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001206 if (!some_works) {
Angel Pons3aed6ac2020-12-07 02:00:41 +01001207 /* Guard against roundtrip latency underflow */
Angel Pons88521882020-01-05 20:21:20 +01001208 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Angel Pons30791632020-12-12 12:28:29 +01001209 printk(BIOS_EMERG, "Roundtrip latency underflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001210 channel, slotrank);
1211 return MAKE_ERR;
1212 }
Angel Pons88521882020-01-05 20:21:20 +01001213 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 printram("4024 -= 2;\n");
1215 continue;
1216 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001217
1218 /*
1219 * Else (if some lanes are failing), increase the rank's
1220 * I/O latency by 2, and increase rcven logic delay by 2
1221 * on the working lanes, then perform another iteration.
1222 */
Felix Heldef4fe3e2019-12-31 14:15:05 +01001223 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001225
Angel Pons3aed6ac2020-12-07 02:00:41 +01001226 /* Guard against I/O latency overflow */
Angel Pons5db1b152020-12-13 16:37:53 +01001227 if (ctrl->timings[channel][slotrank].io_latency >= 16) {
Angel Pons30791632020-12-12 12:28:29 +01001228 printk(BIOS_EMERG, "I/O latency overflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 channel, slotrank);
1230 return MAKE_ERR;
1231 }
1232 FOR_ALL_LANES if (works[lane]) {
Angel Pons42d033a2021-01-03 15:26:37 +01001233 ctrl->timings[channel][slotrank].lanes[lane].rcven += 2 * QCLK_PI;
1234 upperA[lane] += 2 * QCLK_PI;
Angel Pons891f2bc2020-01-10 01:27:28 +01001235 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001236 }
1237 }
1238 return 0;
1239}
1240
Angel Pons12bd8ab2020-11-13 23:10:52 +01001241static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242{
1243 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001244 u16 logic_delay_min = 7;
1245 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001246
1247 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001248 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001249
1250 logic_delay_min = MIN(logic_delay_min, logic_delay);
1251 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001252 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001253
1254 if (logic_delay_max < logic_delay_min) {
1255 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1256 logic_delay_max, logic_delay_min, channel, slotrank);
1257 }
1258
1259 assert(logic_delay_max >= logic_delay_min);
1260
1261 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001262}
1263
Angel Pons12bd8ab2020-11-13 23:10:52 +01001264static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001265{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001266 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001267
Angel Pons7c49cb82020-03-16 23:17:32 +01001268 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001269 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270
Angel Pons12bd8ab2020-11-13 23:10:52 +01001271 if (prev < post)
1272 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001273
Angel Pons12bd8ab2020-11-13 23:10:52 +01001274 else if (prev > post)
1275 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001276
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001277 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001278 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001279
Angel Pons12bd8ab2020-11-13 23:10:52 +01001280 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1281 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1282 printram("4024 += %d;\n", latency_offset);
1283 printram("4028 += %d;\n", latency_offset);
1284
1285 return post;
1286}
1287
1288static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1289{
1290 u16 logic_delay_min = 7;
1291 int lane;
1292
1293 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001294 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001295
1296 logic_delay_min = MIN(logic_delay_min, logic_delay);
1297 }
1298
1299 if (logic_delay_min >= 2) {
1300 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1301 logic_delay_min, channel, slotrank);
1302 }
1303
1304 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001305 ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001306 }
1307 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1308 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001309}
1310
Angel Pons7f5a97c2020-11-13 16:58:46 +01001311int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001312{
1313 int channel, slotrank, lane;
1314 int err;
1315
1316 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1317 int all_high, some_high;
1318 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001319 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001320
Angel Pons88521882020-01-05 20:21:20 +01001321 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001322
Angel Ponsffd50152020-11-12 11:03:10 +01001323 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001324
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001325 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001326
Angel Pons58b609b2020-11-13 14:35:29 +01001327 const union gdcr_training_mod_reg training_mod = {
1328 .receive_enable_mode = 1,
1329 .training_rank_sel = slotrank,
1330 .odt_always_on = 1,
1331 };
Angel Pons66780a02021-03-26 13:33:22 +01001332 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001333
Felix Heldef4fe3e2019-12-31 14:15:05 +01001334 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001335 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001336 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001337
Angel Ponsf3053392020-11-13 23:31:12 +01001338 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001339
Felix Held2bb3cdf2018-07-28 00:23:59 +02001340 all_high = 1;
1341 some_high = 0;
1342 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001343 if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= QCLK_PI)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001344 some_high = 1;
1345 else
1346 all_high = 0;
1347 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001348
1349 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001350 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001351 printram("4028--;\n");
1352 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001353 ctrl->timings[channel][slotrank].lanes[lane].rcven -= QCLK_PI;
1354 upperA[lane] -= QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001355
1356 }
1357 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001358 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001359 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001360 printram("4024++;\n");
1361 printram("4028++;\n");
1362 }
1363
1364 program_timings(ctrl, channel);
1365
Angel Pons12bd8ab2020-11-13 23:10:52 +01001366 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001367
Angel Ponsf3053392020-11-13 23:31:12 +01001368 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001369 if (err)
1370 return err;
1371
Angel Pons12bd8ab2020-11-13 23:10:52 +01001372 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001373
Angel Ponsf3053392020-11-13 23:31:12 +01001374 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001375
Angel Pons12bd8ab2020-11-13 23:10:52 +01001376 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001377
Angel Pons12bd8ab2020-11-13 23:10:52 +01001378 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379
Angel Pons12bd8ab2020-11-13 23:10:52 +01001380 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001381
Angel Pons7e439c92020-12-07 11:56:01 +01001382 printram("4/8: %d, %d, % 4d, % 4d\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001383 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001384 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001385
1386 printram("final results:\n");
1387 FOR_ALL_LANES
Angel Pons7e439c92020-12-07 11:56:01 +01001388 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001389 ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390
Angel Pons66780a02021-03-26 13:33:22 +01001391 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001392
1393 toggle_io_reset();
1394 }
1395
1396 FOR_ALL_POPULATED_CHANNELS {
1397 program_timings(ctrl, channel);
1398 }
Angel Ponsc6742232020-11-15 13:26:21 +01001399
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001400 return 0;
1401}
1402
Angel Pons011661c2020-11-15 18:21:35 +01001403static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001404{
1405 int lane;
1406
1407 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001408 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane), 0);
1409 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001410 }
1411
Angel Pons88521882020-01-05 20:21:20 +01001412 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001413
Angel Ponsffd50152020-11-12 11:03:10 +01001414 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1415 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001416
Angel Ponsa853e7a2020-12-07 12:28:38 +01001417 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001418
Angel Pons801a5cb2020-11-15 15:48:29 +01001419 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001420
Angel Ponsa853e7a2020-12-07 12:28:38 +01001421 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001422}
1423
Angel Pons011661c2020-11-15 18:21:35 +01001424static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001425{
1426 int min = data[0];
1427 int max = min;
1428 int i;
1429 for (i = 1; i < count; i++) {
1430 if (min > data[i])
1431 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001432
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001433 if (max < data[i])
1434 max = data[i];
1435 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001436 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001437 for (i = 0; i < count; i++)
1438 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001439
Angel Pons891f2bc2020-01-10 01:27:28 +01001440 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001441}
1442
Angel Pons011661c2020-11-15 18:21:35 +01001443static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001444{
Angel Pons011661c2020-11-15 18:21:35 +01001445 int tx_dq;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001446 int stats[NUM_LANES][MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001447 int lane;
1448
Angel Pons88521882020-01-05 20:21:20 +01001449 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001450
Angel Ponsffd50152020-11-12 11:03:10 +01001451 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001452
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001453 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001454
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001455 for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) {
1456 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001457 program_timings(ctrl, channel);
1458
Angel Pons011661c2020-11-15 18:21:35 +01001459 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001460
1461 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001462 stats[lane][tx_dq] = mchbar_read32(
1463 IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001464 }
1465 }
1466 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001467 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1468
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001469 if (rn.all || rn.length < 8) {
Angel Pons30791632020-12-12 12:28:29 +01001470 printk(BIOS_EMERG, "tx_dq write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001471 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001472 /*
1473 * With command training not being done yet, the lane can be erroneous.
1474 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001475 */
Angel Pons011661c2020-11-15 18:21:35 +01001476 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001477 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1478
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001479 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001480 printk(BIOS_EMERG, "tx_dq recovery failed\n");
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001481 return MAKE_ERR;
1482 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001483 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001484 ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle;
Angel Pons7e439c92020-12-07 11:56:01 +01001485 printram("tx_dq: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001486 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001487 }
1488 return 0;
1489}
1490
Angel Pons88521882020-01-05 20:21:20 +01001491static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001492{
1493 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001494
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001495 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1496 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001497
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001498 return ret;
1499}
1500
Angel Pons765d4652020-11-11 14:44:35 +01001501/* Each cacheline is 64 bits long */
1502static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1503{
Angel Pons66780a02021-03-26 13:33:22 +01001504 mchbar_write8(IOSAV_DATA_CTL_ch(channel), num_cachelines / 8 - 1);
Angel Pons765d4652020-11-11 14:44:35 +01001505}
1506
Angel Pons88521882020-01-05 20:21:20 +01001507static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001508{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301509 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001510 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001511 uintptr_t addr;
Angel Pons7c49cb82020-03-16 23:17:32 +01001512
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001513 for (j = 0; j < 16; j++) {
1514 addr = 0x04000000 + channel_offset + 4 * j;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001515 write32p(addr, j & 2 ? b : a);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001516 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001517
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001518 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001519
1520 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001521}
1522
Angel Pons88521882020-01-05 20:21:20 +01001523static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001524{
1525 int ret = 0;
1526 int channel;
1527 FOR_ALL_POPULATED_CHANNELS ret++;
1528 return ret;
1529}
1530
Angel Pons88521882020-01-05 20:21:20 +01001531static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001532{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301533 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001534 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
1535 unsigned int channel_step = 64 * num_of_channels(ctrl);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001536 uintptr_t addr;
Angel Pons7c49cb82020-03-16 23:17:32 +01001537
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001538 for (j = 0; j < 16; j++) {
1539 addr = 0x04000000 + channel_offset + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001540 write32p(addr, 0xffffffff);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001541 }
1542 for (j = 0; j < 16; j++) {
1543 addr = 0x04000000 + channel_offset + channel_step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001544 write32p(addr, 0);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001545 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001546 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001547
1548 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001549}
1550
Angel Pons42d033a2021-01-03 15:26:37 +01001551#define TX_DQS_PI_LENGTH (2 * QCLK_PI)
1552
Angel Pons820bce72020-11-14 17:02:55 +01001553static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001554{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001555 int tx_dqs;
Angel Pons42d033a2021-01-03 15:26:37 +01001556 int statistics[NUM_LANES][TX_DQS_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557 int lane;
1558
Angel Pons58b609b2020-11-13 14:35:29 +01001559 const union gdcr_training_mod_reg training_mod = {
1560 .write_leveling_mode = 1,
1561 .training_rank_sel = slotrank,
1562 .enable_dqs_wl = 5,
1563 .odt_always_on = 1,
1564 .force_drive_enable = 1,
1565 };
Angel Pons66780a02021-03-26 13:33:22 +01001566 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001567
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001568 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1569 int bank = 1;
1570
1571 if (ctrl->rank_mirror[channel][slotrank])
1572 ddr3_mirror_mrreg(&bank, &mr1reg);
1573
1574 wait_for_iosav(channel);
1575
1576 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1577
Angel Pons42d033a2021-01-03 15:26:37 +01001578 for (tx_dqs = 0; tx_dqs < TX_DQS_PI_LENGTH; tx_dqs++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001579 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001580 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001581 }
1582 program_timings(ctrl, channel);
1583
Angel Ponsa853e7a2020-12-07 12:28:38 +01001584 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001585
1586 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001587 statistics[lane][tx_dqs] = !((mchbar_read32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001588 GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >>
1589 (tx_dqs % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001590 }
1591 }
1592 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001593 struct run rn = get_longest_zero_run(statistics[lane], TX_DQS_PI_LENGTH);
Angel Pons7c49cb82020-03-16 23:17:32 +01001594 /*
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001595 * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value
1596 * of tx_dqs by a small value, which might cause the 6-bit value to overflow if
Angel Pons7c49cb82020-03-16 23:17:32 +01001597 * it's close to 0x3f. Increment the value by a small offset if it's likely
1598 * to overflow, to make sure it won't overflow while running tests and bricks
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001599 * the system due to a non matching tx_dq.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001600 *
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001601 * TODO: find out why some tests (edge write discovery) increment tx_dqs.
Angel Pons7c49cb82020-03-16 23:17:32 +01001602 */
1603 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001604 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001605 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001606 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001607
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001608 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001609 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01001610 printk(BIOS_EMERG, "JEDEC write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001611 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001612
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001613 return MAKE_ERR;
1614 }
Angel Pons7e439c92020-12-07 11:56:01 +01001615 printram("tx_dqs: %d, %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01001616 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001617 }
1618 return 0;
1619}
1620
Angel Pons820bce72020-11-14 17:02:55 +01001621static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001622{
1623 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001624 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001625 if (val == 0xffffffffffffffffLL)
1626 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001627 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001628 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001629 for (i = 0; i < 8; i++)
1630 if (val << (8 * (7 - i) + 4))
1631 return -i;
1632 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001633 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001634 for (i = 0; i < 8; i++)
1635 if (val >> (8 * (7 - i) + 4))
1636 return i;
1637 }
1638 return 8;
1639}
1640
Angel Ponsbf13ef02020-11-11 18:40:06 +01001641static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001642{
1643 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001644
1645 const union gdcr_training_mod_reg training_mod = {
1646 .dq_dqs_training_res = 1,
1647 };
Angel Pons66780a02021-03-26 13:33:22 +01001648 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Angel Pons58b609b2020-11-13 14:35:29 +01001649
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001650 FOR_ALL_POPULATED_CHANNELS {
1651 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001652 }
1653 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1654
Angel Pons765d4652020-11-11 14:44:35 +01001655 /* Reset read and write WDB pointers */
Angel Pons66780a02021-03-26 13:33:22 +01001656 mchbar_write32(IOSAV_DATA_CTL_ch(channel), 0x10001);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657
Angel Pons88521882020-01-05 20:21:20 +01001658 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001659
Angel Ponsffd50152020-11-12 11:03:10 +01001660 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001661
Angel Ponsa853e7a2020-12-07 12:28:38 +01001662 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663
Angel Pons8f0757e2020-11-11 23:03:36 +01001664 const struct iosav_ssq rd_sequence[] = {
1665 /* DRAM command PREA */
1666 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001667 .sp_cmd_ctrl = {
1668 .command = IOSAV_PRE,
1669 .ranksel_ap = 1,
1670 },
1671 .subseq_ctrl = {
1672 .cmd_executions = 1,
1673 .cmd_delay_gap = 3,
1674 .post_ssq_wait = ctrl->tRP,
1675 .data_direction = SSQ_NA,
1676 },
1677 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +01001678 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +02001679 .rowbits = 6,
1680 .bank = 0,
1681 .rank = slotrank,
1682 },
1683 .addr_update = {
1684 .addr_wrap = 18,
1685 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001686 },
1687 /* DRAM command ACT */
1688 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001689 .sp_cmd_ctrl = {
1690 .command = IOSAV_ACT,
1691 .ranksel_ap = 1,
1692 },
1693 .subseq_ctrl = {
1694 .cmd_executions = 1,
1695 .cmd_delay_gap = 3,
1696 .post_ssq_wait = ctrl->tRCD,
1697 .data_direction = SSQ_NA,
1698 },
1699 .sp_cmd_addr = {
1700 .address = 0,
1701 .rowbits = 6,
1702 .bank = 0,
1703 .rank = slotrank,
1704 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001705 },
Angel Ponsf5502312021-02-10 11:08:28 +01001706 /* DRAM command RDA */
Angel Pons8f0757e2020-11-11 23:03:36 +01001707 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001708 .sp_cmd_ctrl = {
1709 .command = IOSAV_RD,
1710 .ranksel_ap = 3,
1711 },
1712 .subseq_ctrl = {
1713 .cmd_executions = 1,
1714 .cmd_delay_gap = 3,
1715 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001716 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001717 ctrl->timings[channel][slotrank].io_latency,
1718 .data_direction = SSQ_RD,
1719 },
1720 .sp_cmd_addr = {
1721 .address = 8,
1722 .rowbits = 6,
1723 .bank = 0,
1724 .rank = slotrank,
1725 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001726 },
1727 };
1728 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001729
Angel Ponsa853e7a2020-12-07 12:28:38 +01001730 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001731
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001732 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001733 u64 res = mchbar_read32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Elyes Haouas3a998072022-11-18 15:11:02 +01001734 res |= ((u64)mchbar_read32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001735 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001736
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001737 old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1738 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs +=
Angel Pons42d033a2021-01-03 15:26:37 +01001739 get_dqs_flyby_adjust(res) * QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001740
1741 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons7e439c92020-12-07 11:56:01 +01001742 printram("Bval+: %d, %d, %d, % 4d -> % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001743 old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001744 }
1745 }
Angel Pons66780a02021-03-26 13:33:22 +01001746 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001747}
1748
Angel Pons7d115132020-11-14 01:44:44 +01001749static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001750{
Angel Pons7d115132020-11-14 01:44:44 +01001751 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001752
Angel Pons7d115132020-11-14 01:44:44 +01001753 FOR_ALL_POPULATED_CHANNELS {
1754 /* choose an existing rank */
1755 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001756
Angel Pons7d115132020-11-14 01:44:44 +01001757 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001758
Angel Ponsa853e7a2020-12-07 12:28:38 +01001759 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001760
Angel Pons66780a02021-03-26 13:33:22 +01001761 mchbar_setbits32(SCHED_CBIT_ch(channel), 1 << 21);
Angel Pons7d115132020-11-14 01:44:44 +01001762 }
1763
1764 /* Refresh disable */
Angel Pons66780a02021-03-26 13:33:22 +01001765 mchbar_clrbits32(MC_INIT_STATE_G, 1 << 3);
Angel Pons7d115132020-11-14 01:44:44 +01001766
1767 FOR_ALL_POPULATED_CHANNELS {
1768 /* Execute the same command queue */
Angel Ponsa853e7a2020-12-07 12:28:38 +01001769 iosav_run_once_and_wait(channel);
Angel Pons7d115132020-11-14 01:44:44 +01001770 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001771}
1772
Angel Pons7c49cb82020-03-16 23:17:32 +01001773/*
1774 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001775 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001776 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1777 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1778 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1779 * CLK/ADDR/CMD signals have the same routing delay.
1780 *
1781 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1782 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1783 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001784 */
Angel Pons820bce72020-11-14 17:02:55 +01001785static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786{
Angel Pons820bce72020-11-14 17:02:55 +01001787 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001788
Angel Pons7d115132020-11-14 01:44:44 +01001789 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001790
Angel Pons7c49cb82020-03-16 23:17:32 +01001791 /* Enable write leveling on all ranks
1792 Disable all DQ outputs
1793 Only NOP is allowed in this mode */
1794 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1795 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001796 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001797
Angel Ponsa1f17142020-11-15 12:50:03 +01001798 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001799 const union gdcr_training_mod_reg training_mod = {
1800 .write_leveling_mode = 1,
1801 .enable_dqs_wl = 5,
1802 .odt_always_on = 1,
1803 .force_drive_enable = 1,
1804 };
Angel Pons66780a02021-03-26 13:33:22 +01001805 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001806
1807 toggle_io_reset();
1808
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001809 /* Set any valid value for tx_dqs, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001810 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001811 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001812 if (err)
1813 return err;
1814 }
1815
Angel Pons7c49cb82020-03-16 23:17:32 +01001816 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001817 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001818 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001819
Angel Pons66780a02021-03-26 13:33:22 +01001820 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001821
1822 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001823 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001824
Angel Pons7c49cb82020-03-16 23:17:32 +01001825 /* Refresh enable */
Angel Pons66780a02021-03-26 13:33:22 +01001826 mchbar_setbits32(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001827
1828 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01001829 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 21);
1830 mchbar_read32(IOSAV_STATUS_ch(channel));
Angel Pons88521882020-01-05 20:21:20 +01001831 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001832
Angel Ponsffd50152020-11-12 11:03:10 +01001833 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001834
Angel Ponsa853e7a2020-12-07 12:28:38 +01001835 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836 }
1837
1838 toggle_io_reset();
1839
Angel Pons820bce72020-11-14 17:02:55 +01001840 return 0;
1841}
1842
1843int write_training(ramctr_timing *ctrl)
1844{
Angel Ponsc6742232020-11-15 13:26:21 +01001845 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001846 int err;
1847
Angel Pons4d192822020-12-12 13:54:37 +01001848 /*
1849 * Set the DEC_WRD bit, required for the write flyby algorithm.
1850 * Needs to be done before starting the write training procedure.
1851 */
Angel Pons820bce72020-11-14 17:02:55 +01001852 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01001853 mchbar_setbits32(TC_RWP_ch(channel), 1 << 27);
Angel Pons820bce72020-11-14 17:02:55 +01001854
Angel Pons4c76d252020-11-15 13:06:53 +01001855 printram("CPE\n");
1856
Angel Pons820bce72020-11-14 17:02:55 +01001857 err = jedec_write_leveling(ctrl);
1858 if (err)
1859 return err;
1860
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001861 printram("CPF\n");
1862
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001863 FOR_ALL_POPULATED_CHANNELS {
1864 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001865 }
1866
1867 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001868 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001869 if (err)
1870 return err;
1871 }
1872
1873 FOR_ALL_POPULATED_CHANNELS
1874 program_timings(ctrl, channel);
1875
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001876 /* measure and adjust tx_dqs timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001877 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001878
1879 FOR_ALL_POPULATED_CHANNELS
1880 program_timings(ctrl, channel);
1881
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001882 return 0;
1883}
1884
Angel Ponsbf13ef02020-11-11 18:40:06 +01001885static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001886{
1887 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001888 int tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001889 int lanes_ok = 0;
1890 int ctr = 0;
1891 int lane;
1892
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001893 for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001894 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001895 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
1896 saved_rt.lanes[lane].tx_dq + tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001897 }
1898 program_timings(ctrl, channel);
1899 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001900 mchbar_write32(IOSAV_By_ERROR_COUNT(lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001901 }
1902
Angel Pons765d4652020-11-11 14:44:35 +01001903 /* Reset read WDB pointer */
Angel Pons66780a02021-03-26 13:33:22 +01001904 mchbar_write32(IOSAV_DATA_CTL_ch(channel), 0x1f);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001905
Angel Pons88521882020-01-05 20:21:20 +01001906 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001907
Angel Ponsffd50152020-11-12 11:03:10 +01001908 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001909
1910 /* Program LFSR for the RD/WR subsequences */
Angel Pons66780a02021-03-26 13:33:22 +01001911 mchbar_write32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1), 0x389abcd);
1912 mchbar_write32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2), 0x389abcd);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001913
Angel Ponsa853e7a2020-12-07 12:28:38 +01001914 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001915
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001916 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001917 u32 r32 = mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001918
1919 if (r32 == 0)
1920 lanes_ok |= 1 << lane;
1921 }
1922 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001923 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001924 break;
1925 }
1926
1927 ctrl->timings[channel][slotrank] = saved_rt;
1928
Patrick Rudolphdd662872017-10-28 18:20:11 +02001929 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001930}
1931
Angel Pons88521882020-01-05 20:21:20 +01001932static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001933{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301934 unsigned int i, j;
Angel Pons5db1b152020-12-13 16:37:53 +01001935 unsigned int offset = get_precedening_channels(ctrl, channel) * 64;
1936 unsigned int step = 64 * num_of_channels(ctrl);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001937 uintptr_t addr;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001938
1939 if (patno) {
1940 u8 base8 = 0x80 >> ((patno - 1) % 8);
1941 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1942 for (i = 0; i < 32; i++) {
1943 for (j = 0; j < 16; j++) {
1944 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001945
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001946 if (invert[patno - 1][i] & (1 << (j / 2)))
1947 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001948
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001949 addr = (1 << 26) + offset + i * step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001950 write32p(addr, val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001951 }
1952 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001953 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001954 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1955 for (j = 0; j < 16; j++) {
1956 const u32 val = pattern[i][j];
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001957 addr = (1 << 26) + offset + i * step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001958 write32p(addr, val);
Angel Pons7c49cb82020-03-16 23:17:32 +01001959 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001960 }
1961 sfence();
1962 }
Angel Pons765d4652020-11-11 14:44:35 +01001963
1964 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001965}
1966
Angel Pons88521882020-01-05 20:21:20 +01001967static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001968{
Angel Pons7d115132020-11-14 01:44:44 +01001969 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970
Angel Pons7c49cb82020-03-16 23:17:32 +01001971 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001973
1974 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001975 dram_mrscommands(ctrl);
1976
1977 toggle_io_reset();
1978}
1979
Angel Pons42d033a2021-01-03 15:26:37 +01001980#define CT_MIN_PI (-CCC_MAX_PI)
1981#define CT_MAX_PI (+CCC_MAX_PI + 1)
Angel Ponsbf13ef02020-11-11 18:40:06 +01001982#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1983
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001984#define MIN_C320C_LEN 13
1985
1986static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
1987{
1988 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
1989 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001990 int command_pi;
1991 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001992 int delta = 0;
1993
1994 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
1995
1996 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01001997 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001998 }
1999
2000 ctrl->cmd_stretch[channel] = cmd_stretch;
2001
Angel Pons7a612742020-11-12 13:34:03 +01002002 const union tc_rap_reg tc_rap = {
2003 .tRRD = ctrl->tRRD,
2004 .tRTP = ctrl->tRTP,
2005 .tCKE = ctrl->tCKE,
2006 .tWTR = ctrl->tWTR,
2007 .tFAW = ctrl->tFAW,
2008 .tWR = ctrl->tWR,
2009 .tCMD = ctrl->cmd_stretch[channel],
2010 };
Angel Pons66780a02021-03-26 13:33:22 +01002011 mchbar_write32(TC_RAP_ch(channel), tc_rap.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002012
2013 if (ctrl->cmd_stretch[channel] == 2)
2014 delta = 2;
2015 else if (ctrl->cmd_stretch[channel] == 0)
2016 delta = 4;
2017
2018 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002019 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002020 }
2021
Angel Ponsbf13ef02020-11-11 18:40:06 +01002022 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002023 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002024 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002025 }
2026 program_timings(ctrl, channel);
2027 reprogram_320c(ctrl);
2028 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002029 stat[slotrank][command_pi - CT_MIN_PI] =
2030 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002031 }
2032 }
2033 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002034 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002035
Angel Ponsbf13ef02020-11-11 18:40:06 +01002036 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Angel Pons7e439c92020-12-07 11:56:01 +01002037 printram("cmd_stretch: %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01002038 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002039
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002040 if (rn.all || rn.length < MIN_C320C_LEN) {
2041 FOR_ALL_POPULATED_RANKS {
2042 ctrl->timings[channel][slotrank] =
2043 saved_timings[channel][slotrank];
2044 }
2045 return MAKE_ERR;
2046 }
2047 }
2048
2049 return 0;
2050}
2051
Angel Pons7c49cb82020-03-16 23:17:32 +01002052/*
2053 * Adjust CMD phase shift and try multiple command rates.
2054 * A command rate of 2T doubles the time needed for address and command decode.
2055 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002056int command_training(ramctr_timing *ctrl)
2057{
2058 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002059
2060 FOR_ALL_POPULATED_CHANNELS {
2061 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002062 }
2063
2064 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002065 int cmdrate, err;
2066
2067 /*
2068 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002069 * Issue:
Angel Pons30791632020-12-12 12:28:29 +01002070 * While command training seems to succeed, raminit will fail in write training.
Angel Pons7c49cb82020-03-16 23:17:32 +01002071 *
2072 * Workaround:
2073 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2074 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002075 *
2076 * Single DIMM per channel:
2077 * Try command rate 1T and 2T
2078 */
2079 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002080 if (ctrl->tCMD)
2081 /* XMP gives the CMD rate in clock ticks, not ns */
2082 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002083
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002084 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002085 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2086
2087 if (!err)
2088 break;
2089 }
2090
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002091 if (err) {
Angel Pons30791632020-12-12 12:28:29 +01002092 printk(BIOS_EMERG, "Command training failed: %d\n", channel);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002093 return err;
2094 }
2095
Angel Pons891f2bc2020-01-10 01:27:28 +01002096 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002097 }
2098
2099 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002100 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002101
2102 reprogram_320c(ctrl);
2103 return 0;
2104}
2105
Angel Pons4c79f932020-11-14 01:26:52 +01002106static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002107{
Angel Pons96a06dd2020-11-14 00:33:18 +01002108 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002109 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002110 int lane;
2111
Angel Pons96a06dd2020-11-14 00:33:18 +01002112 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002113 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002114 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi;
2115 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002116 }
2117 program_timings(ctrl, channel);
2118
2119 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002120 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane), 0);
2121 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002122 }
2123
Angel Pons88521882020-01-05 20:21:20 +01002124 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002125
Angel Ponsffd50152020-11-12 11:03:10 +01002126 iosav_write_read_mpr_sequence(
2127 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002128
Angel Ponsa853e7a2020-12-07 12:28:38 +01002129 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002130
2131 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002132 stats[lane][dqs_pi] = mchbar_read32(
2133 IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002134 }
2135 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002136
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002137 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002138 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002139 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002140
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002141 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002142 printk(BIOS_EMERG, "Read MPR training failed: %d, %d, %d\n", channel,
Angel Pons7c49cb82020-03-16 23:17:32 +01002143 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002144 return MAKE_ERR;
2145 }
Angel Pons7e439c92020-12-07 11:56:01 +01002146 printram("eval %d, %d, %d: % 4d\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002147 }
2148 return 0;
2149}
2150
Angel Pons60971dc2020-11-14 00:49:38 +01002151static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2152{
2153 int slotrank, lane;
2154
2155 fill_pattern0(ctrl, channel, 0, 0);
2156 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002157 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane), 0);
2158 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Angel Pons60971dc2020-11-14 00:49:38 +01002159 }
2160
2161 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002162 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16;
2163 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16;
Angel Pons60971dc2020-11-14 00:49:38 +01002164 }
2165
2166 program_timings(ctrl, channel);
2167
2168 FOR_ALL_POPULATED_RANKS {
2169 wait_for_iosav(channel);
2170
2171 iosav_write_read_mpr_sequence(
2172 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2173
Angel Ponsa853e7a2020-12-07 12:28:38 +01002174 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002175 }
2176
2177 /* XXX: check any measured value ? */
2178
2179 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002180 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48;
2181 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48;
Angel Pons60971dc2020-11-14 00:49:38 +01002182 }
2183
2184 program_timings(ctrl, channel);
2185
2186 FOR_ALL_POPULATED_RANKS {
2187 wait_for_iosav(channel);
2188
2189 iosav_write_read_mpr_sequence(
2190 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2191
Angel Ponsa853e7a2020-12-07 12:28:38 +01002192 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002193 }
2194
2195 /* XXX: check any measured value ? */
2196
2197 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002198 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane),
2199 ~mchbar_read32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff);
Angel Pons60971dc2020-11-14 00:49:38 +01002200 }
2201}
2202
Angel Pons4c79f932020-11-14 01:26:52 +01002203int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002204{
2205 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2206 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2207 int channel, slotrank, lane;
2208 int err;
2209
Angel Pons66780a02021-03-26 13:33:22 +01002210 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002211
2212 toggle_io_reset();
2213
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002214 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002215 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002216
2217 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002218 }
2219
Angel Pons0c3936e2020-03-22 12:49:27 +01002220 /*
2221 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2222 * also use a single loop. It would seem that it is a debugging configuration.
2223 */
Angel Pons66780a02021-03-26 13:33:22 +01002224 mchbar_write32(IOSAV_DC_MASK, 3 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002225 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002226
2227 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002228 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002229 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002230 if (err)
2231 return err;
2232 }
2233
Angel Pons66780a02021-03-26 13:33:22 +01002234 mchbar_write32(IOSAV_DC_MASK, 2 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002235 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002236
2237 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002238 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002239 rising_edges[channel][slotrank]);
2240 if (err)
2241 return err;
2242 }
2243
Angel Pons66780a02021-03-26 13:33:22 +01002244 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002245
2246 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002247 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002248 falling_edges[channel][slotrank][lane];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002249 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002250 rising_edges[channel][slotrank][lane];
2251 }
2252
2253 FOR_ALL_POPULATED_CHANNELS {
2254 program_timings(ctrl, channel);
2255 }
2256
Angel Pons50a6fe72020-11-14 01:18:14 +01002257 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002258 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002259 }
2260 return 0;
2261}
2262
Angel Pons08f749d2020-11-17 16:50:56 +01002263static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002264{
Angel Pons08f749d2020-11-17 16:50:56 +01002265 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2266
Angel Pons7c49cb82020-03-16 23:17:32 +01002267 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002268 int lower[NUM_LANES];
2269 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002270 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002271
2272 FOR_ALL_LANES {
2273 lower[lane] = 0;
2274 upper[lane] = MAX_EDGE_TIMING;
2275 }
2276
Angel Pons08f749d2020-11-17 16:50:56 +01002277 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002278 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002279 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002280 };
Angel Pons66780a02021-03-26 13:33:22 +01002281 mchbar_write32(GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons58b609b2020-11-13 14:35:29 +01002282 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002283
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002284 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2285 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002286 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002287
Angel Pons08f749d2020-11-17 16:50:56 +01002288 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002289 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002290 ctrl->timings[channel][slotrank].lanes[lane]
2291 .rx_dqs_p = read_pi;
2292 ctrl->timings[channel][slotrank].lanes[lane]
2293 .rx_dqs_n = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002294 }
2295 program_timings(ctrl, channel);
2296
2297 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002298 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane),
2299 0);
2300 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002301 }
Angel Pons88521882020-01-05 20:21:20 +01002302 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002303
Angel Ponsffd50152020-11-12 11:03:10 +01002304 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002305
Angel Ponsa853e7a2020-12-07 12:28:38 +01002306 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002307
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002308 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002309 mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002310 }
2311
Angel Pons7c49cb82020-03-16 23:17:32 +01002312 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons66780a02021-03-26 13:33:22 +01002313 raw_stats[read_pi] = mchbar_read32(
2314 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002315 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002316
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002317 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002318 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002319 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002320
2321 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2322 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002323
2324 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2325
Angel Pons7e439c92020-12-07 11:56:01 +01002326 printram("edges: %d, %d, %d: % 4d-% 4d-% 4d, "
2327 "% 4d-% 4d\n", channel, slotrank, i, rn.start,
Angel Pons7c49cb82020-03-16 23:17:32 +01002328 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002329 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002330
2331 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2332 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2333
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002334 edges[lane] = (lower[lane] + upper[lane]) / 2;
2335 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons30791632020-12-12 12:28:29 +01002336 printk(BIOS_EMERG, "Aggressive read training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002337 "%d, %d, %d\n", channel, slotrank, lane);
2338
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002339 return MAKE_ERR;
2340 }
2341 }
2342 }
2343 }
2344
Angel Ponsa93f46e2020-11-17 16:54:01 +01002345 /* Restore nominal Vref after training */
Angel Pons66780a02021-03-26 13:33:22 +01002346 mchbar_write32(GDCRTRAININGMOD_ch(channel), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002347 printram("CPA\n");
2348 return 0;
2349}
2350
Angel Pons08f749d2020-11-17 16:50:56 +01002351int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002352{
2353 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002354 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2355 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002356
Angel Pons7c49cb82020-03-16 23:17:32 +01002357 /*
2358 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2359 * also use a single loop. It would seem that it is a debugging configuration.
2360 */
Angel Pons66780a02021-03-26 13:33:22 +01002361 mchbar_write32(IOSAV_DC_MASK, 3 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002362 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002363
2364 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002365 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002366 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002367 if (err)
2368 return err;
2369 }
2370
Angel Pons66780a02021-03-26 13:33:22 +01002371 mchbar_write32(IOSAV_DC_MASK, 2 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002372 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002373
2374 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002375 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002376 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002377 if (err)
2378 return err;
2379 }
2380
Angel Pons66780a02021-03-26 13:33:22 +01002381 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002382
2383 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002384 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Angel Pons7c49cb82020-03-16 23:17:32 +01002385 falling_edges[channel][slotrank][lane];
2386
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002387 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Angel Pons7c49cb82020-03-16 23:17:32 +01002388 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002389 }
2390
2391 FOR_ALL_POPULATED_CHANNELS
2392 program_timings(ctrl, channel);
2393
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002394 return 0;
2395}
2396
Angel Pons2a7d7522020-11-19 12:49:07 +01002397static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002398{
Angel Pons88521882020-01-05 20:21:20 +01002399 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002400
Angel Ponsffd50152020-11-12 11:03:10 +01002401 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002402
Angel Ponsa853e7a2020-12-07 12:28:38 +01002403 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002404}
2405
Angel Pons2a7d7522020-11-19 12:49:07 +01002406static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002407{
Angel Pons66780a02021-03-26 13:33:22 +01002408 mchbar_clrsetbits32(GDCRCMDDEBUGMUXCFG_Cz_S(channel), 0x3f << 24, wr_vref << 24);
Angel Pons2a7d7522020-11-19 12:49:07 +01002409 udelay(2);
2410}
2411
2412int aggressive_write_training(ramctr_timing *ctrl)
2413{
2414 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002415 int i, pat;
2416
2417 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2418 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2419 int channel, slotrank, lane;
2420
Angel Pons9fbb1b02020-11-19 12:53:36 +01002421 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2422 if (!IS_IVY_CPU(ctrl->cpu))
2423 return 0;
2424
2425 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2426 return 0;
2427
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002428 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2429 lower[channel][slotrank][lane] = 0;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002430 upper[channel][slotrank][lane] = MAX_TX_DQ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002431 }
2432
Angel Pons2a7d7522020-11-19 12:49:07 +01002433 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2434 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2435
2436 if (enable_iosav_opt)
Angel Pons66780a02021-03-26 13:33:22 +01002437 mchbar_write32(MCMNTS_SPARE, 1);
Angel Pons2a7d7522020-11-19 12:49:07 +01002438
Martin Roth50863da2021-10-01 14:37:30 -06002439 printram("Aggressive write training:\n");
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002440
Angel Pons2a7d7522020-11-19 12:49:07 +01002441 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002442 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002443 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002444
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002445 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2446 FOR_ALL_POPULATED_RANKS {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002447 int tx_dq;
2448 u32 raw_stats[MAX_TX_DQ + 1];
2449 int stats[MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002450
2451 /* Make sure rn.start < rn.end */
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002452 stats[MAX_TX_DQ] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002453
2454 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002455
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002456 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002457 FOR_ALL_LANES {
2458 ctrl->timings[channel][slotrank]
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002459 .lanes[lane].tx_dq = tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01002460 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002461 program_timings(ctrl, channel);
2462
Angel Pons2a7d7522020-11-19 12:49:07 +01002463 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002464
Angel Pons66780a02021-03-26 13:33:22 +01002465 raw_stats[tx_dq] = mchbar_read32(
Angel Pons098240eb2020-03-22 12:55:32 +01002466 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002467 }
2468 FOR_ALL_LANES {
2469 struct run rn;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002470 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
2471 stats[tx_dq] = !!(raw_stats[tx_dq]
Angel Pons7c49cb82020-03-16 23:17:32 +01002472 & (1 << lane));
2473 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002474
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002475 rn = get_longest_zero_run(stats, MAX_TX_DQ + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002476 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002477 printk(BIOS_EMERG, "Aggressive "
2478 "write training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002479 "%d, %d, %d\n", channel,
2480 slotrank, lane);
2481
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002482 return MAKE_ERR;
2483 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002484 printram("tx_dq: %d, %d, %d: "
Angel Pons7e439c92020-12-07 11:56:01 +01002485 "% 4d-% 4d-% 4d, "
2486 "% 4d-% 4d\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002487 i, rn.start, rn.middle, rn.end,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002488 rn.start + ctrl->tx_dq_offset[i],
2489 rn.end - ctrl->tx_dq_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002490
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002491 lower[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002492 MAX(rn.start + ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002493 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002494
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002495 upper[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002496 MIN(rn.end - ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002497 upper[channel][slotrank][lane]);
2498
2499 }
2500 }
2501 }
2502 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002503 }
2504
Angel Pons2a7d7522020-11-19 12:49:07 +01002505 FOR_ALL_CHANNELS {
2506 /* Restore nominal write Vref after training */
2507 set_write_vref(channel, 0);
2508 }
2509
2510 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2511 if (enable_iosav_opt)
Angel Pons66780a02021-03-26 13:33:22 +01002512 mchbar_write32(MCMNTS_SPARE, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002513
2514 printram("CPB\n");
2515
2516 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7e439c92020-12-07 11:56:01 +01002517 printram("tx_dq %d, %d, %d: % 4d\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002518 (lower[channel][slotrank][lane] +
2519 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002520
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002521 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002522 (lower[channel][slotrank][lane] +
2523 upper[channel][slotrank][lane]) / 2;
2524 }
2525 FOR_ALL_POPULATED_CHANNELS {
2526 program_timings(ctrl, channel);
2527 }
2528 return 0;
2529}
2530
Angel Pons88521882020-01-05 20:21:20 +01002531void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002532{
2533 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002534 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002535
2536 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2537 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002538 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002539 FOR_ALL_LANES mat =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002540 MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002541 printram("normalize %d, %d, %d: mat %d\n",
2542 channel, slotrank, lane, mat);
2543
Felix Heldef4fe3e2019-12-31 14:15:05 +01002544 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002545 printram("normalize %d, %d, %d: delta %d\n",
2546 channel, slotrank, lane, delta);
2547
Angel Pons88521882020-01-05 20:21:20 +01002548 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002549 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002550 }
2551
2552 FOR_ALL_POPULATED_CHANNELS {
2553 program_timings(ctrl, channel);
2554 }
2555}
2556
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002557int channel_test(ramctr_timing *ctrl)
2558{
2559 int channel, slotrank, lane;
2560
2561 slotrank = 0;
2562 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01002563 if (mchbar_read32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002564 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002565 return MAKE_ERR;
2566 }
2567 FOR_ALL_POPULATED_CHANNELS {
2568 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002569 }
2570
2571 for (slotrank = 0; slotrank < 4; slotrank++)
2572 FOR_ALL_CHANNELS
2573 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2574 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002575 mchbar_write32(IOSAV_By_ERROR_COUNT(lane), 0);
2576 mchbar_write32(IOSAV_By_BW_SERROR_C(lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002577 }
Angel Pons88521882020-01-05 20:21:20 +01002578 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002579
Angel Ponsffd50152020-11-12 11:03:10 +01002580 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002581
Angel Ponsa853e7a2020-12-07 12:28:38 +01002582 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002583
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002584 FOR_ALL_LANES
Angel Pons66780a02021-03-26 13:33:22 +01002585 if (mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002586 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2587 channel, slotrank, lane);
2588 return MAKE_ERR;
2589 }
2590 }
2591 return 0;
2592}
2593
Patrick Rudolphdd662872017-10-28 18:20:11 +02002594void channel_scrub(ramctr_timing *ctrl)
2595{
2596 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002597 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002598
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002599 FOR_ALL_POPULATED_CHANNELS {
2600 wait_for_iosav(channel);
2601 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002602 }
2603
2604 /*
2605 * During runtime the "scrubber" will periodically scan through the memory in the
2606 * physical address space, to identify and fix CRC errors.
2607 * The following loops writes to every DRAM address, setting the ECC bits to the
2608 * correct value. A read from this location will no longer return a CRC error,
2609 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002610 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002611 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2612 * and firmware running in x86_32.
2613 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002614 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2615 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002616 for (bank = 0; bank < 8; bank++) {
2617 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002618
Angel Pons8f0757e2020-11-11 23:03:36 +01002619 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2620 const struct iosav_ssq sequence[] = {
2621 /*
2622 * DRAM command ACT
2623 * Opens the row for writing.
2624 */
2625 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002626 .sp_cmd_ctrl = {
2627 .command = IOSAV_ACT,
2628 .ranksel_ap = 1,
2629 },
2630 .subseq_ctrl = {
2631 .cmd_executions = 1,
2632 .cmd_delay_gap = gap,
2633 .post_ssq_wait = ctrl->tRCD,
2634 .data_direction = SSQ_NA,
2635 },
2636 .sp_cmd_addr = {
2637 .address = row,
2638 .rowbits = 6,
2639 .bank = bank,
2640 .rank = slotrank,
2641 },
2642 .addr_update = {
2643 .inc_addr_1 = 1,
2644 .addr_wrap = 18,
2645 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002646 },
2647 /*
2648 * DRAM command WR
2649 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2650 * bytes.
2651 */
2652 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002653 .sp_cmd_ctrl = {
2654 .command = IOSAV_WR,
2655 .ranksel_ap = 1,
2656 },
2657 .subseq_ctrl = {
2658 .cmd_executions = 129,
2659 .cmd_delay_gap = 4,
2660 .post_ssq_wait = ctrl->tWTR +
2661 ctrl->CWL + 8,
2662 .data_direction = SSQ_WR,
2663 },
2664 .sp_cmd_addr = {
2665 .address = row,
2666 .rowbits = 0,
2667 .bank = bank,
2668 .rank = slotrank,
2669 },
2670 .addr_update = {
2671 .inc_addr_8 = 1,
2672 .addr_wrap = 9,
2673 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002674 },
2675 /*
2676 * DRAM command PRE
2677 * Closes the row.
2678 */
2679 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002680 .sp_cmd_ctrl = {
2681 .command = IOSAV_PRE,
2682 .ranksel_ap = 1,
2683 },
2684 .subseq_ctrl = {
2685 .cmd_executions = 1,
2686 .cmd_delay_gap = 4,
2687 .post_ssq_wait = ctrl->tRP,
2688 .data_direction = SSQ_NA,
2689 },
2690 .sp_cmd_addr = {
2691 .address = 0,
2692 .rowbits = 6,
2693 .bank = bank,
2694 .rank = slotrank,
2695 },
2696 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002697 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002698 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002699 },
2700 };
2701 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002702
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002703 iosav_run_queue(channel, 16, 0);
2704
2705 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002706 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002707 }
2708 }
2709}
2710
Angel Pons88521882020-01-05 20:21:20 +01002711void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002712{
2713 int channel;
2714
Angel Pons7c49cb82020-03-16 23:17:32 +01002715 /* 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 +01002716 static u32 seeds[NUM_CHANNELS][3] = {
2717 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2718 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2719 };
2720 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002721 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 28);
2722 mchbar_write32(SCRAMBLING_SEED_1_ch(channel), seeds[channel][0]);
2723 mchbar_write32(SCRAMBLING_SEED_2_HI_ch(channel), seeds[channel][1]);
2724 mchbar_write32(SCRAMBLING_SEED_2_LO_ch(channel), seeds[channel][2]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002725 }
2726}
2727
Angel Pons89ae6b82020-03-21 13:23:32 +01002728void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002729{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002730 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons66780a02021-03-26 13:33:22 +01002731 mchbar_write32(SC_WDBWM, 0x141d1519);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002732 } else {
Angel Pons66780a02021-03-26 13:33:22 +01002733 mchbar_write32(SC_WDBWM, 0x551d1519);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002734 }
2735}
2736
Angel Pons88521882020-01-05 20:21:20 +01002737void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002738{
2739 int channel;
2740
2741 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002742 /* Always drive command bus */
Angel Pons66780a02021-03-26 13:33:22 +01002743 mchbar_setbits32(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002744 }
2745
2746 udelay(1);
2747
2748 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002749 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002750 }
2751}
2752
Angel Pons7c49cb82020-03-16 23:17:32 +01002753void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002754{
Angel Pons11463322020-11-19 11:04:28 +01002755 /* Use a larger delay when running fast to improve stability */
2756 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2757
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002758 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002759
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002760 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002761 int min_pi = 10000;
2762 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002763
2764 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002765 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2766 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002767 }
2768
Angel Pons7a612742020-11-12 13:34:03 +01002769 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002770
Angel Pons7a612742020-11-12 13:34:03 +01002771 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002772
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002773 dram_odt_stretch(ctrl, channel);
2774
Angel Pons7a612742020-11-12 13:34:03 +01002775 const union tc_rwp_reg tc_rwp = {
2776 .tRRDR = 0,
2777 .tRRDD = val,
2778 .tWWDR = val,
2779 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002780 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002781 .tWRDRDD = tWRDRDD,
2782 .tRWSR = 2,
2783 .dec_wrd = 1,
2784 };
Angel Pons66780a02021-03-26 13:33:22 +01002785 mchbar_write32(TC_RWP_ch(channel), tc_rwp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002786 }
2787}
2788
Angel Pons88521882020-01-05 20:21:20 +01002789void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002790{
2791 int channel;
2792 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002793 mchbar_write32(MC_INIT_STATE_ch(channel), 1 << 12 | ctrl->rankmap[channel]);
2794 mchbar_clrbits32(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002795 }
2796}
2797
Angel Pons7c49cb82020-03-16 23:17:32 +01002798/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2799static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002800{
Angel Pons88521882020-01-05 20:21:20 +01002801 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002802}
2803
Angel Pons7c49cb82020-03-16 23:17:32 +01002804/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002805void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002806{
2807 int channel;
2808 int t1_cycles = 0, t1_ns = 0, t2_ns;
2809 int t3_ns;
2810 u32 r32;
2811
Angel Pons7c49cb82020-03-16 23:17:32 +01002812 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons66780a02021-03-26 13:33:22 +01002813 mchbar_write32(WMM_READ_CONFIG, 0x46);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002814
Angel Pons7a612742020-11-12 13:34:03 +01002815 FOR_ALL_CHANNELS {
2816 union tc_othp_reg tc_othp = {
Angel Pons66780a02021-03-26 13:33:22 +01002817 .raw = mchbar_read32(TC_OTHP_ch(channel)),
Angel Pons7a612742020-11-12 13:34:03 +01002818 };
2819 tc_othp.tCPDED = 1;
Angel Pons66780a02021-03-26 13:33:22 +01002820 mchbar_write32(TC_OTHP_ch(channel), tc_othp.raw);
Angel Pons7a612742020-11-12 13:34:03 +01002821 }
Patrick Rudolph652c4912017-10-31 11:36:55 +01002822
Angel Pons09fc4b92020-11-19 12:02:07 +01002823 /* 64 DCLKs until idle, decision per rank */
Angel Pons66780a02021-03-26 13:33:22 +01002824 mchbar_write32(PM_PDWN_CONFIG, get_power_down_mode(ctrl) << 8 | 64);
Patrick Rudolph652c4912017-10-31 11:36:55 +01002825
Felix Heldf9b826a2018-07-30 17:56:52 +02002826 FOR_ALL_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01002827 mchbar_write32(PM_TRML_M_CONFIG_ch(channel), 0x00000aaa);
Felix Heldf9b826a2018-07-30 17:56:52 +02002828
Angel Pons66780a02021-03-26 13:33:22 +01002829 mchbar_write32(PM_BW_LIMIT_CONFIG, 0x5f7003ff);
2830 mchbar_write32(PM_DLL_CONFIG, 0x00073000 | ctrl->mdll_wake_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002831
2832 FOR_ALL_CHANNELS {
2833 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002834 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002835 case 0:
Angel Pons66780a02021-03-26 13:33:22 +01002836 mchbar_write32(PM_CMD_PWR_ch(channel), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002837 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002838 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002839 case 1:
2840 case 4:
2841 case 5:
Angel Pons66780a02021-03-26 13:33:22 +01002842 mchbar_write32(PM_CMD_PWR_ch(channel), 0x00373131);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002843 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002844 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002845 default:
Angel Pons66780a02021-03-26 13:33:22 +01002846 mchbar_write32(PM_CMD_PWR_ch(channel), 0x009b6ea1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002847 break;
2848 }
2849 }
2850
Angel Pons66780a02021-03-26 13:33:22 +01002851 mchbar_write32(MEM_TRML_ESTIMATION_CONFIG, 0xca9171e5);
2852 mchbar_clrsetbits32(MEM_TRML_THRESHOLDS_CONFIG, 0x00ffffff, 0x00e4d5d0);
2853 mchbar_clrbits32(MEM_TRML_INTERRUPT, 0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002854
Angel Pons7a612742020-11-12 13:34:03 +01002855 FOR_ALL_CHANNELS {
2856 union tc_rfp_reg tc_rfp = {
Angel Pons66780a02021-03-26 13:33:22 +01002857 .raw = mchbar_read32(TC_RFP_ch(channel)),
Angel Pons7a612742020-11-12 13:34:03 +01002858 };
2859 tc_rfp.refresh_2x_control = 1;
Angel Pons66780a02021-03-26 13:33:22 +01002860 mchbar_write32(TC_RFP_ch(channel), tc_rfp.raw);
Angel Pons7a612742020-11-12 13:34:03 +01002861 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002862
Angel Pons66780a02021-03-26 13:33:22 +01002863 mchbar_setbits32(MC_INIT_STATE_G, 1 << 0);
2864 mchbar_setbits32(MC_INIT_STATE_G, 1 << 7);
2865 mchbar_write32(BANDTIMERS_SNB, 0xfa);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002866
Angel Pons7c49cb82020-03-16 23:17:32 +01002867 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002868 FOR_ALL_POPULATED_CHANNELS
2869 break;
2870
Angel Pons66780a02021-03-26 13:33:22 +01002871 t1_cycles = (mchbar_read32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2872 r32 = mchbar_read32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002873 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002874 t1_cycles += (r32 & 0xfff);
Angel Pons66780a02021-03-26 13:33:22 +01002875 t1_cycles += mchbar_read32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002876 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002877 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002878 t1_ns += 500;
2879
Angel Pons66780a02021-03-26 13:33:22 +01002880 t2_ns = 10 * ((mchbar_read32(SAPMTIMERS) >> 8) & 0xfff);
2881 if (mchbar_read32(SAPMCTL) & 8) {
2882 t3_ns = 10 * ((mchbar_read32(BANDTIMERS_IVB) >> 8) & 0xfff);
2883 t3_ns += 10 * (mchbar_read32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002884 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002885 t3_ns = 500;
2886 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002887
2888 /* The graphics driver will use these watermark values */
2889 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Pons66780a02021-03-26 13:33:22 +01002890 mchbar_clrsetbits32(SSKPD, 0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002891 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2892 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002893}
2894
Angel Pons88521882020-01-05 20:21:20 +01002895void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002896{
Angel Ponsc6742232020-11-15 13:26:21 +01002897 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002898
Angel Pons7c49cb82020-03-16 23:17:32 +01002899 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002900 const union tc_rap_reg tc_rap = {
2901 .tRRD = ctrl->tRRD,
2902 .tRTP = ctrl->tRTP,
2903 .tCKE = ctrl->tCKE,
2904 .tWTR = ctrl->tWTR,
2905 .tFAW = ctrl->tFAW,
2906 .tWR = ctrl->tWR,
2907 .tCMD = ctrl->cmd_stretch[channel],
2908 };
Angel Pons66780a02021-03-26 13:33:22 +01002909 mchbar_write32(TC_RAP_ch(channel), tc_rap.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002910 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002911
2912 udelay(1);
2913
2914 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002915 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002916 }
2917
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002918 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01002919 mchbar_setbits32(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920
2921 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002922 udelay(1);
Angel Pons66780a02021-03-26 13:33:22 +01002923 mchbar_setbits32(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002924 }
2925
2926 printram("CPE\n");
2927
Angel Pons66780a02021-03-26 13:33:22 +01002928 mchbar_write32(GDCRTRAININGMOD, 0);
2929 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002930
2931 printram("CP5b\n");
2932
2933 FOR_ALL_POPULATED_CHANNELS {
2934 program_timings(ctrl, channel);
2935 }
2936
2937 u32 reg, addr;
2938
Angel Pons7c49cb82020-03-16 23:17:32 +01002939 /* Poll for RCOMP */
Angel Pons66780a02021-03-26 13:33:22 +01002940 while (!(mchbar_read32(RCOMP_TIMER) & (1 << 16)))
Angel Pons7c49cb82020-03-16 23:17:32 +01002941 ;
2942
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002943 do {
Angel Pons66780a02021-03-26 13:33:22 +01002944 reg = mchbar_read32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002945 } while ((reg & 0x14) == 0);
2946
Angel Pons7c49cb82020-03-16 23:17:32 +01002947 /* Set state of memory controller */
Angel Pons66780a02021-03-26 13:33:22 +01002948 mchbar_write32(MC_INIT_STATE_G, 0x116);
2949 mchbar_write32(MC_INIT_STATE, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002950
Angel Pons7c49cb82020-03-16 23:17:32 +01002951 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002952 udelay(500);
2953
2954 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002955 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002956 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002957 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002958 addr = MC_INIT_STATE_ch(channel);
Angel Pons66780a02021-03-26 13:33:22 +01002959 mchbar_write32(addr, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002960
Angel Pons7c49cb82020-03-16 23:17:32 +01002961 /* Wait 10ns for ranks to settle */
2962 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002963
2964 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons66780a02021-03-26 13:33:22 +01002965 mchbar_write32(addr, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002966
Angel Pons7c49cb82020-03-16 23:17:32 +01002967 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002968 write_reset(ctrl);
2969 }
2970
Angel Pons7c49cb82020-03-16 23:17:32 +01002971 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972 dram_mrscommands(ctrl);
2973
2974 printram("CP5c\n");
2975
Angel Pons66780a02021-03-26 13:33:22 +01002976 mchbar_write32(GDCRTRAININGMOD_ch(0), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002977
2978 FOR_ALL_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002979 mchbar_clrbits32(GDCRCMDDEBUGMUXCFG_Cz_S(channel), 0x3f << 24);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002980 udelay(2);
2981 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002982}