blob: 3f5e290c57f36c17fc789f39cf4e7db3c309567e [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 */
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100670static enum power_down_mode get_power_down_mode(ramctr_timing *ctrl, int channel)
Angel Pons09fc4b92020-11-19 12:02:07 +0100671{
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100672 int slotrank;
673
Angel Pons09fc4b92020-11-19 12:02:07 +0100674 if (ctrl->tXP > 8)
675 return PDM_NONE;
676
677 if (ctrl->tXPDLL > 32)
678 return PDM_PPD;
679
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100680 FOR_ALL_POPULATED_RANKS
681 if (!ctrl->info.dimm[channel][slotrank >> 1].flags.dll_off_mode)
682 return PDM_APD_PPD;
683
Angel Pons09fc4b92020-11-19 12:02:07 +0100684 if (CONFIG(RAMINIT_ALWAYS_ALLOW_DLL_OFF) || get_platform_type() == PLATFORM_MOBILE)
685 return PDM_DLL_OFF;
686
687 return PDM_APD_PPD;
688}
689
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100690static u32 make_mr0(ramctr_timing *ctrl, int channel, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100691{
692 u16 mr0reg, mch_cas, mch_wr;
693 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 +0100694
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100695 const enum power_down_mode power_down = get_power_down_mode(ctrl, channel);
Angel Pons09fc4b92020-11-19 12:02:07 +0100696
697 const bool slow_exit = power_down == PDM_DLL_OFF || power_down == PDM_APD_DLL_OFF;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100698
Angel Pons7c49cb82020-03-16 23:17:32 +0100699 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100700 if (ctrl->CAS < 12) {
Elyes Haouas3a998072022-11-18 15:11:02 +0100701 mch_cas = (u16)((ctrl->CAS - 4) << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100702 } else {
Elyes Haouas3a998072022-11-18 15:11:02 +0100703 mch_cas = (u16)(ctrl->CAS - 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100704 mch_cas = ((mch_cas << 1) | 0x1);
705 }
706
Angel Pons7c49cb82020-03-16 23:17:32 +0100707 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100708 mch_wr = mch_wr_t[ctrl->tWR - 5];
709
Angel Pons2bf28ed2020-11-12 13:49:59 +0100710 /* DLL Reset - self clearing - set after CLK frequency has been changed */
711 mr0reg = 1 << 8;
712
713 mr0reg |= (mch_cas & 0x1) << 2;
714 mr0reg |= (mch_cas & 0xe) << 3;
715 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100716
Angel Pons09fc4b92020-11-19 12:02:07 +0100717 /* Precharge PD - Use slow exit when DLL-off is used - mostly power-saving feature */
718 mr0reg |= !slow_exit << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100719 return mr0reg;
720}
721
722static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
723{
Patrick Rudolph68642ca2023-12-21 07:08:49 +0100724 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, channel, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100725}
726
Angel Ponsf9997482020-11-12 16:02:52 +0100727static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100728{
729 /* Get ODT based on rankmap */
730 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
731
732 if (dimms_per_ch == 1) {
733 return (const odtmap){60, 60};
734 } else {
735 return (const odtmap){120, 30};
736 }
737}
738
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100739static u32 encode_odt(u32 odt)
740{
741 switch (odt) {
742 case 30:
Angel Ponsc728e252021-01-03 16:47:09 +0100743 return (1 << 9) | (1 << 2); /* RZQ/8, RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100744 case 60:
Angel Ponsc728e252021-01-03 16:47:09 +0100745 return (1 << 2); /* RZQ/4 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100746 case 120:
Angel Ponsc728e252021-01-03 16:47:09 +0100747 return (1 << 6); /* RZQ/2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100748 default:
749 case 0:
750 return 0;
751 }
752}
753
754static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
755{
756 odtmap odt;
757 u32 mr1reg;
758
Angel Ponsf9997482020-11-12 16:02:52 +0100759 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100760 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100761
762 mr1reg |= encode_odt(odt.rttnom);
763
764 return mr1reg;
765}
766
767static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
768{
769 u16 mr1reg;
770
771 mr1reg = make_mr1(ctrl, rank, channel);
772
773 write_mrreg(ctrl, channel, rank, 1, mr1reg);
774}
775
776static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
777{
Angel Pons868bca22020-11-13 13:38:04 +0100778 const u16 pasr = 0;
779 const u16 cwl = ctrl->CWL - 5;
780 const odtmap odt = get_ODT(ctrl, channel);
781
Angel Ponsdca3cb52020-11-13 13:42:07 +0100782 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100783 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
784 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100785
Angel Pons868bca22020-11-13 13:38:04 +0100786 u16 mr2reg = 0;
787 mr2reg |= pasr;
788 mr2reg |= cwl << 3;
789 mr2reg |= ctrl->auto_self_refresh << 6;
790 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100791 mr2reg |= (odt.rttwr / 60) << 9;
792
793 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100794
795 /* Program MR2 shadow */
Angel Pons66780a02021-03-26 13:33:22 +0100796 u32 reg32 = mchbar_read32(TC_MR2_SHADOW_ch(channel));
Angel Pons7f1363d2020-11-13 13:31:58 +0100797
798 reg32 &= 3 << 14 | 3 << 6;
799
800 reg32 |= mr2reg & ~(3 << 6);
801
Angel Pons927b1c02020-12-10 22:11:27 +0100802 if (srt)
803 reg32 |= 1 << (rank / 2 + 6);
804
805 if (ctrl->rank_mirror[channel][rank])
806 reg32 |= 1 << (rank / 2 + 14);
807
Angel Pons66780a02021-03-26 13:33:22 +0100808 mchbar_write32(TC_MR2_SHADOW_ch(channel), reg32);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100809}
810
811static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
812{
813 write_mrreg(ctrl, channel, rank, 3, 0);
814}
815
Angel Pons88521882020-01-05 20:21:20 +0100816void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100817{
818 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100819 int channel;
820
821 FOR_ALL_POPULATED_CHANNELS {
822 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100823 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100824 dram_mr2(ctrl, slotrank, channel);
825
Angel Pons7c49cb82020-03-16 23:17:32 +0100826 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100827 dram_mr3(ctrl, slotrank, channel);
828
Angel Pons7c49cb82020-03-16 23:17:32 +0100829 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100830 dram_mr1(ctrl, slotrank, channel);
831
Angel Pons7c49cb82020-03-16 23:17:32 +0100832 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100833 dram_mr0(ctrl, slotrank, channel);
834 }
835 }
836
Angel Pons8f0757e2020-11-11 23:03:36 +0100837 const struct iosav_ssq zqcl_sequence[] = {
838 /* DRAM command NOP (without ODT nor chip selects) */
839 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200840 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100841 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200842 },
843 .subseq_ctrl = {
844 .cmd_executions = 1,
845 .cmd_delay_gap = 4,
846 .post_ssq_wait = 15,
847 .data_direction = SSQ_NA,
848 },
849 .sp_cmd_addr = {
850 .address = 2,
851 .rowbits = 6,
852 .bank = 0,
853 .rank = 0,
854 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100855 },
856 /* DRAM command ZQCL */
857 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200858 .sp_cmd_ctrl = {
859 .command = IOSAV_ZQCS,
860 .ranksel_ap = 1,
861 },
862 .subseq_ctrl = {
863 .cmd_executions = 1,
864 .cmd_delay_gap = 4,
865 .post_ssq_wait = 400,
866 .data_direction = SSQ_NA,
867 },
868 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +0100869 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +0200870 .rowbits = 6,
871 .bank = 0,
872 .rank = 0,
873 },
874 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100875 .inc_rank = 1,
876 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200877 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100878 },
879 };
880 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100881
Angel Pons38d901e2020-05-02 23:50:43 +0200882 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100883
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100884 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +0100885 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100886 }
887
Angel Pons7c49cb82020-03-16 23:17:32 +0100888 /* Refresh enable */
Angel Pons66780a02021-03-26 13:33:22 +0100889 mchbar_setbits32(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100890
891 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +0100892 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100893
Angel Pons88521882020-01-05 20:21:20 +0100894 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100895
896 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
897
Angel Pons88521882020-01-05 20:21:20 +0100898 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100899
Angel Ponsffd50152020-11-12 11:03:10 +0100900 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200901
Angel Ponsa853e7a2020-12-07 12:28:38 +0100902 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100903 }
904}
905
Felix Held3b906032020-01-14 17:05:43 +0100906static const u32 lane_base[] = {
907 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
908 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
909 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100910};
911
Angel Pons42d033a2021-01-03 15:26:37 +0100912/* Maximum delay for command, control, clock */
913#define CCC_MAX_PI (2 * QCLK_PI - 1)
914
Angel Pons88521882020-01-05 20:21:20 +0100915void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100916{
Angel Pons7584e552020-11-19 21:34:32 +0100917 u32 reg_roundtrip_latency, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100918 int lane;
919 int slotrank, slot;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100920
Angel Pons7584e552020-11-19 21:34:32 +0100921 u32 ctl_delay[NUM_SLOTS] = { 0 };
922 int cmd_delay = 0;
923
924 /* Enable CLK XOVER */
925 u32 clk_pi_coding = get_XOVER_CLK(ctrl->rankmap[channel]);
926 u32 clk_logic_dly = 0;
927
928 /*
Angel Pons7519ca42021-01-12 01:21:24 +0100929 * Compute command timing as abs() of the most negative PI code
930 * across all ranks. Use zero if none of the values is negative.
Angel Pons7584e552020-11-19 21:34:32 +0100931 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100932 FOR_ALL_POPULATED_RANKS {
Angel Pons7519ca42021-01-12 01:21:24 +0100933 cmd_delay = MAX(cmd_delay, -ctrl->timings[channel][slotrank].pi_coding);
Angel Pons7584e552020-11-19 21:34:32 +0100934 }
Angel Pons42d033a2021-01-03 15:26:37 +0100935 if (cmd_delay > CCC_MAX_PI) {
Angel Pons7584e552020-11-19 21:34:32 +0100936 printk(BIOS_ERR, "C%d command delay overflow: %d\n", channel, cmd_delay);
Angel Pons42d033a2021-01-03 15:26:37 +0100937 cmd_delay = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100938 }
939
Angel Pons89200d22021-01-12 01:04:04 +0100940 for (slot = 0; slot < NUM_SLOTS; slot++) {
941 const int pi_coding_0 = ctrl->timings[channel][2 * slot + 0].pi_coding;
942 const int pi_coding_1 = ctrl->timings[channel][2 * slot + 1].pi_coding;
Angel Pons7584e552020-11-19 21:34:32 +0100943
Angel Pons89200d22021-01-12 01:04:04 +0100944 const u8 slot_map = (ctrl->rankmap[channel] >> (2 * slot)) & 3;
Angel Pons7584e552020-11-19 21:34:32 +0100945
Angel Pons89200d22021-01-12 01:04:04 +0100946 if (slot_map & 1)
947 ctl_delay[slot] += pi_coding_0 + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100948
Angel Pons89200d22021-01-12 01:04:04 +0100949 if (slot_map & 2)
950 ctl_delay[slot] += pi_coding_1 + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100951
Angel Pons89200d22021-01-12 01:04:04 +0100952 /* If both ranks in a slot are populated, use the average */
953 if (slot_map == 3)
954 ctl_delay[slot] /= 2;
Angel Pons7584e552020-11-19 21:34:32 +0100955
Angel Pons89200d22021-01-12 01:04:04 +0100956 if (ctl_delay[slot] > CCC_MAX_PI) {
957 printk(BIOS_ERR, "C%dS%d control delay overflow: %d\n",
958 channel, slot, ctl_delay[slot]);
959 ctl_delay[slot] = CCC_MAX_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100960 }
Angel Pons89200d22021-01-12 01:04:04 +0100961 }
962 FOR_ALL_POPULATED_RANKS {
Angel Pons0a7d99c2021-01-12 01:13:08 +0100963 int clk_delay = ctrl->timings[channel][slotrank].pi_coding + cmd_delay;
Angel Pons7584e552020-11-19 21:34:32 +0100964
Angel Pons0a7d99c2021-01-12 01:13:08 +0100965 /*
966 * Clock is a differential signal, whereas command and control are not.
967 * This affects its timing, and it is also why it needs a magic offset.
968 */
969 clk_delay += ctrl->pi_code_offset;
970
971 /* Can never happen with valid values */
972 if (clk_delay < 0) {
973 printk(BIOS_ERR, "C%dR%d clock delay underflow: %d\n",
Angel Pons89200d22021-01-12 01:04:04 +0100974 channel, slotrank, clk_delay);
Angel Pons0a7d99c2021-01-12 01:13:08 +0100975 clk_delay = 0;
Angel Pons7584e552020-11-19 21:34:32 +0100976 }
Angel Pons89200d22021-01-12 01:04:04 +0100977
Angel Pons0a7d99c2021-01-12 01:13:08 +0100978 /* Clock can safely wrap around because it is a periodic signal */
979 clk_delay %= CCC_MAX_PI + 1;
980
Angel Pons89200d22021-01-12 01:04:04 +0100981 clk_pi_coding |= (clk_delay % QCLK_PI) << (6 * slotrank);
982 clk_logic_dly |= (clk_delay / QCLK_PI) << slotrank;
Angel Pons7584e552020-11-19 21:34:32 +0100983 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100984
Angel Pons7c49cb82020-03-16 23:17:32 +0100985 /* Enable CMD XOVER */
Angel Pons737f1112020-11-13 14:07:30 +0100986 union gdcr_cmd_pi_coding_reg cmd_pi_coding = {
987 .raw = get_XOVER_CMD(ctrl->rankmap[channel]),
988 };
Angel Pons42d033a2021-01-03 15:26:37 +0100989 cmd_pi_coding.cmd_pi_code = cmd_delay % QCLK_PI;
990 cmd_pi_coding.cmd_logic_delay = cmd_delay / QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100991
Angel Pons42d033a2021-01-03 15:26:37 +0100992 cmd_pi_coding.ctl_pi_code_d0 = ctl_delay[0] % QCLK_PI;
993 cmd_pi_coding.ctl_pi_code_d1 = ctl_delay[1] % QCLK_PI;
994 cmd_pi_coding.ctl_logic_delay_d0 = ctl_delay[0] / QCLK_PI;
995 cmd_pi_coding.ctl_logic_delay_d1 = ctl_delay[1] / QCLK_PI;
Angel Pons737f1112020-11-13 14:07:30 +0100996
Angel Pons66780a02021-03-26 13:33:22 +0100997 mchbar_write32(GDCRCMDPICODING_ch(channel), cmd_pi_coding.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100998
Angel Pons66780a02021-03-26 13:33:22 +0100999 mchbar_write32(GDCRCKPICODE_ch(channel), clk_pi_coding);
1000 mchbar_write32(GDCRCKLOGICDELAY_ch(channel), clk_logic_dly);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001001
Angel Pons66780a02021-03-26 13:33:22 +01001002 reg_io_latency = mchbar_read32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +01001003 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001004
Angel Pons88521882020-01-05 20:21:20 +01001005 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001006
1007 FOR_ALL_POPULATED_RANKS {
Angel Pons075d1232020-11-19 21:50:33 +01001008 reg_io_latency |= ctrl->timings[channel][slotrank].io_latency << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001009
Angel Pons88521882020-01-05 20:21:20 +01001010 reg_roundtrip_latency |=
Angel Pons075d1232020-11-19 21:50:33 +01001011 ctrl->timings[channel][slotrank].roundtrip_latency << (8 * slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001012
1013 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001014 const u16 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
1015 const u8 dqs_p = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p;
1016 const u8 dqs_n = ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n;
Angel Pons9fcc1102020-11-19 22:23:13 +01001017 const union gdcr_rx_reg gdcr_rx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001018 .rcven_pi_code = rcven % QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001019 .rx_dqs_p_pi_code = dqs_p,
Angel Pons42d033a2021-01-03 15:26:37 +01001020 .rcven_logic_delay = rcven / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001021 .rx_dqs_n_pi_code = dqs_n,
1022 };
Angel Pons66780a02021-03-26 13:33:22 +01001023 mchbar_write32(lane_base[lane] + GDCRRX(channel, slotrank),
1024 gdcr_rx.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001025
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001026 const u16 tx_dqs = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1027 const int tx_dq = ctrl->timings[channel][slotrank].lanes[lane].tx_dq;
Angel Pons9fcc1102020-11-19 22:23:13 +01001028 const union gdcr_tx_reg gdcr_tx = {
Angel Pons42d033a2021-01-03 15:26:37 +01001029 .tx_dq_pi_code = tx_dq % QCLK_PI,
1030 .tx_dqs_pi_code = tx_dqs % QCLK_PI,
1031 .tx_dqs_logic_delay = tx_dqs / QCLK_PI,
1032 .tx_dq_logic_delay = tx_dq / QCLK_PI,
Angel Pons9fcc1102020-11-19 22:23:13 +01001033 };
Angel Pons66780a02021-03-26 13:33:22 +01001034 mchbar_write32(lane_base[lane] + GDCRTX(channel, slotrank),
1035 gdcr_tx.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001036 }
1037 }
Angel Pons66780a02021-03-26 13:33:22 +01001038 mchbar_write32(SC_ROUNDT_LAT_ch(channel), reg_roundtrip_latency);
1039 mchbar_write32(SC_IO_LATENCY_ch(channel), reg_io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001040}
1041
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001042static void test_rcven(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001043{
Angel Pons88521882020-01-05 20:21:20 +01001044 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001045
Angel Pons3aed6ac2020-12-07 02:00:41 +01001046 /* Send a burst of 16 back-to-back read commands (4 DCLK apart) */
Angel Ponsffd50152020-11-12 11:03:10 +01001047 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001048
Angel Ponsa853e7a2020-12-07 12:28:38 +01001049 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001050}
1051
Angel Pons7c49cb82020-03-16 23:17:32 +01001052static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001054 u32 rcven = ctrl->timings[channel][slotrank].lanes[lane].rcven;
Angel Pons7c49cb82020-03-16 23:17:32 +01001055
Angel Pons66780a02021-03-26 13:33:22 +01001056 return (mchbar_read32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001057 GDCRTRAININGRESULT(channel, (rcven / 32) & 1)) >> (rcven % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001058}
1059
1060struct run {
1061 int middle;
1062 int end;
1063 int start;
1064 int all;
1065 int length;
1066};
1067
1068static struct run get_longest_zero_run(int *seq, int sz)
1069{
1070 int i, ls;
1071 int bl = 0, bs = 0;
1072 struct run ret;
1073
1074 ls = 0;
1075 for (i = 0; i < 2 * sz; i++)
1076 if (seq[i % sz]) {
1077 if (i - ls > bl) {
1078 bl = i - ls;
1079 bs = ls;
1080 }
1081 ls = i + 1;
1082 }
1083 if (bl == 0) {
1084 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001085 ret.start = 0;
1086 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001087 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001088 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001089 return ret;
1090 }
1091
Angel Pons7c49cb82020-03-16 23:17:32 +01001092 ret.start = bs % sz;
1093 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001094 ret.middle = (bs + (bl - 1) / 2) % sz;
1095 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001096 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001097
1098 return ret;
1099}
1100
Angel Pons42d033a2021-01-03 15:26:37 +01001101#define RCVEN_COARSE_PI_LENGTH (2 * QCLK_PI)
1102
Angel Ponsf3053392020-11-13 23:31:12 +01001103static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001104{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001105 int rcven;
Angel Pons42d033a2021-01-03 15:26:37 +01001106 int statistics[NUM_LANES][RCVEN_COARSE_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001107 int lane;
1108
Angel Pons42d033a2021-01-03 15:26:37 +01001109 for (rcven = 0; rcven < RCVEN_COARSE_PI_LENGTH; rcven++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001110 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001111 ctrl->timings[channel][slotrank].lanes[lane].rcven = rcven;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001112 }
1113 program_timings(ctrl, channel);
1114
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001115 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001116
1117 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001118 statistics[lane][rcven] =
1119 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001120 }
1121 }
1122 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001123 struct run rn = get_longest_zero_run(statistics[lane], RCVEN_COARSE_PI_LENGTH);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001124 ctrl->timings[channel][slotrank].lanes[lane].rcven = rn.middle;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001125 upperA[lane] = rn.end;
1126 if (upperA[lane] < rn.middle)
Angel Pons42d033a2021-01-03 15:26:37 +01001127 upperA[lane] += 2 * QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001128
Angel Pons7e439c92020-12-07 11:56:01 +01001129 printram("rcven: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001130 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001131 }
1132}
1133
Angel Ponsf3053392020-11-13 23:31:12 +01001134static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001135{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001136 int rcven_delta;
Angel Pons86e3d742021-01-03 14:55:12 +01001137 int statistics[NUM_LANES][51] = {0};
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001138 int lane, i;
1139
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001140 for (rcven_delta = -25; rcven_delta <= 25; rcven_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001141
1142 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001143 ctrl->timings[channel][slotrank].lanes[lane].rcven
Angel Pons42d033a2021-01-03 15:26:37 +01001144 = upperA[lane] + rcven_delta + QCLK_PI;
Angel Pons7c49cb82020-03-16 23:17:32 +01001145 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001146 program_timings(ctrl, channel);
1147
1148 for (i = 0; i < 100; i++) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001149 test_rcven(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001150 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001151 statistics[lane][rcven_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001152 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001153 }
1154 }
1155 }
1156 FOR_ALL_LANES {
1157 int last_zero, first_all;
1158
1159 for (last_zero = -25; last_zero <= 25; last_zero++)
1160 if (statistics[lane][last_zero + 25])
1161 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001162
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001163 last_zero--;
1164 for (first_all = -25; first_all <= 25; first_all++)
1165 if (statistics[lane][first_all + 25] == 100)
1166 break;
1167
Angel Pons7c49cb82020-03-16 23:17:32 +01001168 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001169
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001170 ctrl->timings[channel][slotrank].lanes[lane].rcven =
Angel Pons7c49cb82020-03-16 23:17:32 +01001171 (last_zero + first_all) / 2 + upperA[lane];
1172
Angel Pons7e439c92020-12-07 11:56:01 +01001173 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001174 lane, ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001175 }
1176}
1177
Angel Pons3aed6ac2020-12-07 02:00:41 +01001178/*
1179 * Once the DQS high phase has been found (for each DRAM) the next stage
1180 * is to find out the round trip latency, by locating the preamble cycle.
1181 * This is achieved by trying smaller and smaller roundtrip values until
1182 * the strobe sampling is done on the preamble cycle.
1183 */
Angel Ponsf3053392020-11-13 23:31:12 +01001184static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001185{
1186 int works[NUM_LANES];
1187 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001188
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001189 while (1) {
1190 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001191
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001192 program_timings(ctrl, channel);
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001193 test_rcven(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001194
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001195 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001196 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1197
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001198 if (works[lane])
1199 some_works = 1;
1200 else
1201 all_works = 0;
1202 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001203
1204 /* If every lane is working, exit */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001205 if (all_works)
1206 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001207
Angel Pons3aed6ac2020-12-07 02:00:41 +01001208 /*
1209 * If all bits are one (everyone is failing), decrement
1210 * the roundtrip value by two, and do another iteration.
1211 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001212 if (!some_works) {
Angel Pons3aed6ac2020-12-07 02:00:41 +01001213 /* Guard against roundtrip latency underflow */
Angel Pons88521882020-01-05 20:21:20 +01001214 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Angel Pons30791632020-12-12 12:28:29 +01001215 printk(BIOS_EMERG, "Roundtrip latency underflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001216 channel, slotrank);
1217 return MAKE_ERR;
1218 }
Angel Pons88521882020-01-05 20:21:20 +01001219 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001220 printram("4024 -= 2;\n");
1221 continue;
1222 }
Angel Pons3aed6ac2020-12-07 02:00:41 +01001223
1224 /*
1225 * Else (if some lanes are failing), increase the rank's
1226 * I/O latency by 2, and increase rcven logic delay by 2
1227 * on the working lanes, then perform another iteration.
1228 */
Felix Heldef4fe3e2019-12-31 14:15:05 +01001229 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001230 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001231
Angel Pons3aed6ac2020-12-07 02:00:41 +01001232 /* Guard against I/O latency overflow */
Angel Pons5db1b152020-12-13 16:37:53 +01001233 if (ctrl->timings[channel][slotrank].io_latency >= 16) {
Angel Pons30791632020-12-12 12:28:29 +01001234 printk(BIOS_EMERG, "I/O latency overflow: %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001235 channel, slotrank);
1236 return MAKE_ERR;
1237 }
1238 FOR_ALL_LANES if (works[lane]) {
Angel Pons42d033a2021-01-03 15:26:37 +01001239 ctrl->timings[channel][slotrank].lanes[lane].rcven += 2 * QCLK_PI;
1240 upperA[lane] += 2 * QCLK_PI;
Angel Pons891f2bc2020-01-10 01:27:28 +01001241 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242 }
1243 }
1244 return 0;
1245}
1246
Angel Pons12bd8ab2020-11-13 23:10:52 +01001247static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001248{
1249 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001250 u16 logic_delay_min = 7;
1251 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001252
1253 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001254 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001255
1256 logic_delay_min = MIN(logic_delay_min, logic_delay);
1257 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001258 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001259
1260 if (logic_delay_max < logic_delay_min) {
1261 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1262 logic_delay_max, logic_delay_min, channel, slotrank);
1263 }
1264
1265 assert(logic_delay_max >= logic_delay_min);
1266
1267 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001268}
1269
Angel Pons12bd8ab2020-11-13 23:10:52 +01001270static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001271{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001272 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001273
Angel Pons7c49cb82020-03-16 23:17:32 +01001274 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001275 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001276
Angel Pons12bd8ab2020-11-13 23:10:52 +01001277 if (prev < post)
1278 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001279
Angel Pons12bd8ab2020-11-13 23:10:52 +01001280 else if (prev > post)
1281 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001282
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001283 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001284 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001285
Angel Pons12bd8ab2020-11-13 23:10:52 +01001286 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1287 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1288 printram("4024 += %d;\n", latency_offset);
1289 printram("4028 += %d;\n", latency_offset);
1290
1291 return post;
1292}
1293
1294static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1295{
1296 u16 logic_delay_min = 7;
1297 int lane;
1298
1299 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001300 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].rcven >> 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001301
1302 logic_delay_min = MIN(logic_delay_min, logic_delay);
1303 }
1304
1305 if (logic_delay_min >= 2) {
1306 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1307 logic_delay_min, channel, slotrank);
1308 }
1309
1310 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001311 ctrl->timings[channel][slotrank].lanes[lane].rcven -= logic_delay_min << 6;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001312 }
1313 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1314 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001315}
1316
Angel Pons7f5a97c2020-11-13 16:58:46 +01001317int receive_enable_calibration(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001318{
1319 int channel, slotrank, lane;
1320 int err;
1321
1322 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1323 int all_high, some_high;
1324 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001325 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001326
Angel Pons88521882020-01-05 20:21:20 +01001327 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001328
Angel Ponsffd50152020-11-12 11:03:10 +01001329 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001330
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001331 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001332
Angel Pons58b609b2020-11-13 14:35:29 +01001333 const union gdcr_training_mod_reg training_mod = {
1334 .receive_enable_mode = 1,
1335 .training_rank_sel = slotrank,
1336 .odt_always_on = 1,
1337 };
Angel Pons66780a02021-03-26 13:33:22 +01001338 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001339
Felix Heldef4fe3e2019-12-31 14:15:05 +01001340 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001341 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001342 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001343
Angel Ponsf3053392020-11-13 23:31:12 +01001344 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001345
Felix Held2bb3cdf2018-07-28 00:23:59 +02001346 all_high = 1;
1347 some_high = 0;
1348 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001349 if (ctrl->timings[channel][slotrank].lanes[lane].rcven >= QCLK_PI)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001350 some_high = 1;
1351 else
1352 all_high = 0;
1353 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001354
1355 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001356 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001357 printram("4028--;\n");
1358 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001359 ctrl->timings[channel][slotrank].lanes[lane].rcven -= QCLK_PI;
1360 upperA[lane] -= QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001361
1362 }
1363 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001364 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001365 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001366 printram("4024++;\n");
1367 printram("4028++;\n");
1368 }
1369
1370 program_timings(ctrl, channel);
1371
Angel Pons12bd8ab2020-11-13 23:10:52 +01001372 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001373
Angel Ponsf3053392020-11-13 23:31:12 +01001374 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001375 if (err)
1376 return err;
1377
Angel Pons12bd8ab2020-11-13 23:10:52 +01001378 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379
Angel Ponsf3053392020-11-13 23:31:12 +01001380 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001381
Angel Pons12bd8ab2020-11-13 23:10:52 +01001382 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001383
Angel Pons12bd8ab2020-11-13 23:10:52 +01001384 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001385
Angel Pons12bd8ab2020-11-13 23:10:52 +01001386 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001387
Angel Pons7e439c92020-12-07 11:56:01 +01001388 printram("4/8: %d, %d, % 4d, % 4d\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001389 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001390 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001391
1392 printram("final results:\n");
1393 FOR_ALL_LANES
Angel Pons7e439c92020-12-07 11:56:01 +01001394 printram("Aval: %d, %d, %d: % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001395 ctrl->timings[channel][slotrank].lanes[lane].rcven);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396
Angel Pons66780a02021-03-26 13:33:22 +01001397 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001398
1399 toggle_io_reset();
1400 }
1401
1402 FOR_ALL_POPULATED_CHANNELS {
1403 program_timings(ctrl, channel);
1404 }
Angel Ponsc6742232020-11-15 13:26:21 +01001405
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001406 return 0;
1407}
1408
Angel Pons011661c2020-11-15 18:21:35 +01001409static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001410{
1411 int lane;
1412
1413 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001414 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane), 0);
1415 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001416 }
1417
Angel Pons88521882020-01-05 20:21:20 +01001418 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001419
Angel Ponsffd50152020-11-12 11:03:10 +01001420 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1421 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001422
Angel Ponsa853e7a2020-12-07 12:28:38 +01001423 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001424
Angel Pons801a5cb2020-11-15 15:48:29 +01001425 iosav_write_prea_act_read_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02001426
Angel Ponsa853e7a2020-12-07 12:28:38 +01001427 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001428}
1429
Angel Pons011661c2020-11-15 18:21:35 +01001430static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001431{
1432 int min = data[0];
1433 int max = min;
1434 int i;
1435 for (i = 1; i < count; i++) {
1436 if (min > data[i])
1437 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001438
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001439 if (max < data[i])
1440 max = data[i];
1441 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001442 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001443 for (i = 0; i < count; i++)
1444 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001445
Angel Pons891f2bc2020-01-10 01:27:28 +01001446 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001447}
1448
Angel Pons011661c2020-11-15 18:21:35 +01001449static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001450{
Angel Pons011661c2020-11-15 18:21:35 +01001451 int tx_dq;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001452 int stats[NUM_LANES][MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001453 int lane;
1454
Angel Pons88521882020-01-05 20:21:20 +01001455 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001456
Angel Ponsffd50152020-11-12 11:03:10 +01001457 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001458
Angel Pons9f4ed3b2020-12-07 12:34:36 +01001459 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001460
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001461 for (tx_dq = 0; tx_dq <= MAX_TX_DQ; tx_dq++) {
1462 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].tx_dq = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001463 program_timings(ctrl, channel);
1464
Angel Pons011661c2020-11-15 18:21:35 +01001465 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001466
1467 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001468 stats[lane][tx_dq] = mchbar_read32(
1469 IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001470 }
1471 }
1472 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001473 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1474
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001475 if (rn.all || rn.length < 8) {
Angel Pons30791632020-12-12 12:28:29 +01001476 printk(BIOS_EMERG, "tx_dq write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001477 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001478 /*
1479 * With command training not being done yet, the lane can be erroneous.
1480 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001481 */
Angel Pons011661c2020-11-15 18:21:35 +01001482 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001483 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1484
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001485 if (rn.all || rn.length < 8) {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001486 printk(BIOS_EMERG, "tx_dq recovery failed\n");
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001487 return MAKE_ERR;
1488 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001489 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001490 ctrl->timings[channel][slotrank].lanes[lane].tx_dq = rn.middle;
Angel Pons7e439c92020-12-07 11:56:01 +01001491 printram("tx_dq: %d, %d, %d: % 4d-% 4d-% 4d\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001492 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001493 }
1494 return 0;
1495}
1496
Angel Pons88521882020-01-05 20:21:20 +01001497static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001498{
1499 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001500
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001501 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1502 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001503
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001504 return ret;
1505}
1506
Angel Pons765d4652020-11-11 14:44:35 +01001507/* Each cacheline is 64 bits long */
1508static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1509{
Angel Pons66780a02021-03-26 13:33:22 +01001510 mchbar_write8(IOSAV_DATA_CTL_ch(channel), num_cachelines / 8 - 1);
Angel Pons765d4652020-11-11 14:44:35 +01001511}
1512
Angel Pons88521882020-01-05 20:21:20 +01001513static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001514{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301515 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001516 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001517 uintptr_t addr;
Angel Pons7c49cb82020-03-16 23:17:32 +01001518
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001519 for (j = 0; j < 16; j++) {
1520 addr = 0x04000000 + channel_offset + 4 * j;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001521 write32p(addr, j & 2 ? b : a);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001522 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001523
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001524 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001525
1526 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001527}
1528
Angel Pons88521882020-01-05 20:21:20 +01001529static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001530{
1531 int ret = 0;
1532 int channel;
1533 FOR_ALL_POPULATED_CHANNELS ret++;
1534 return ret;
1535}
1536
Angel Pons88521882020-01-05 20:21:20 +01001537static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001538{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301539 unsigned int j;
Angel Pons5db1b152020-12-13 16:37:53 +01001540 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 64;
1541 unsigned int channel_step = 64 * num_of_channels(ctrl);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001542 uintptr_t addr;
Angel Pons7c49cb82020-03-16 23:17:32 +01001543
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001544 for (j = 0; j < 16; j++) {
1545 addr = 0x04000000 + channel_offset + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001546 write32p(addr, 0xffffffff);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001547 }
1548 for (j = 0; j < 16; j++) {
1549 addr = 0x04000000 + channel_offset + channel_step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001550 write32p(addr, 0);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001551 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001552 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001553
1554 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001555}
1556
Angel Pons42d033a2021-01-03 15:26:37 +01001557#define TX_DQS_PI_LENGTH (2 * QCLK_PI)
1558
Angel Pons820bce72020-11-14 17:02:55 +01001559static int write_level_rank(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001560{
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001561 int tx_dqs;
Angel Pons42d033a2021-01-03 15:26:37 +01001562 int statistics[NUM_LANES][TX_DQS_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001563 int lane;
1564
Angel Pons58b609b2020-11-13 14:35:29 +01001565 const union gdcr_training_mod_reg training_mod = {
1566 .write_leveling_mode = 1,
1567 .training_rank_sel = slotrank,
1568 .enable_dqs_wl = 5,
1569 .odt_always_on = 1,
1570 .force_drive_enable = 1,
1571 };
Angel Pons66780a02021-03-26 13:33:22 +01001572 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001573
Angel Ponsc6d2fea2020-11-14 16:52:33 +01001574 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1575 int bank = 1;
1576
1577 if (ctrl->rank_mirror[channel][slotrank])
1578 ddr3_mirror_mrreg(&bank, &mr1reg);
1579
1580 wait_for_iosav(channel);
1581
1582 iosav_write_jedec_write_leveling_sequence(ctrl, channel, slotrank, bank, mr1reg);
1583
Angel Pons42d033a2021-01-03 15:26:37 +01001584 for (tx_dqs = 0; tx_dqs < TX_DQS_PI_LENGTH; tx_dqs++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001585 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001586 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = tx_dqs;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001587 }
1588 program_timings(ctrl, channel);
1589
Angel Ponsa853e7a2020-12-07 12:28:38 +01001590 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001591
1592 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001593 statistics[lane][tx_dqs] = !((mchbar_read32(lane_base[lane] +
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001594 GDCRTRAININGRESULT(channel, (tx_dqs / 32) & 1)) >>
1595 (tx_dqs % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001596 }
1597 }
1598 FOR_ALL_LANES {
Angel Pons42d033a2021-01-03 15:26:37 +01001599 struct run rn = get_longest_zero_run(statistics[lane], TX_DQS_PI_LENGTH);
Angel Pons7c49cb82020-03-16 23:17:32 +01001600 /*
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001601 * tx_dq is a direct function of tx_dqs's 6 LSBs. Some tests increment the value
1602 * of tx_dqs by a small value, which might cause the 6-bit value to overflow if
Angel Pons7c49cb82020-03-16 23:17:32 +01001603 * it's close to 0x3f. Increment the value by a small offset if it's likely
1604 * to overflow, to make sure it won't overflow while running tests and bricks
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001605 * the system due to a non matching tx_dq.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001606 *
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001607 * TODO: find out why some tests (edge write discovery) increment tx_dqs.
Angel Pons7c49cb82020-03-16 23:17:32 +01001608 */
1609 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001610 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001611 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001612 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001613
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001614 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs = rn.start;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001615 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01001616 printk(BIOS_EMERG, "JEDEC write leveling failed: %d, %d, %d\n",
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001617 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001618
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 return MAKE_ERR;
1620 }
Angel Pons7e439c92020-12-07 11:56:01 +01001621 printram("tx_dqs: %d, %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01001622 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001623 }
1624 return 0;
1625}
1626
Angel Pons820bce72020-11-14 17:02:55 +01001627static int get_dqs_flyby_adjust(u64 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001628{
1629 int i;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001630 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001631 if (val == 0xffffffffffffffffLL)
1632 return 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001633 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001634 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001635 for (i = 0; i < 8; i++)
1636 if (val << (8 * (7 - i) + 4))
1637 return -i;
1638 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001639 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001640 for (i = 0; i < 8; i++)
1641 if (val >> (8 * (7 - i) + 4))
1642 return i;
1643 }
1644 return 8;
1645}
1646
Angel Ponsbf13ef02020-11-11 18:40:06 +01001647static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001648{
1649 int channel, slotrank, lane, old;
Angel Pons58b609b2020-11-13 14:35:29 +01001650
1651 const union gdcr_training_mod_reg training_mod = {
1652 .dq_dqs_training_res = 1,
1653 };
Angel Pons66780a02021-03-26 13:33:22 +01001654 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Angel Pons58b609b2020-11-13 14:35:29 +01001655
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001656 FOR_ALL_POPULATED_CHANNELS {
1657 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001658 }
1659 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1660
Angel Pons765d4652020-11-11 14:44:35 +01001661 /* Reset read and write WDB pointers */
Angel Pons66780a02021-03-26 13:33:22 +01001662 mchbar_write32(IOSAV_DATA_CTL_ch(channel), 0x10001);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663
Angel Pons88521882020-01-05 20:21:20 +01001664 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001665
Angel Ponsffd50152020-11-12 11:03:10 +01001666 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001667
Angel Ponsa853e7a2020-12-07 12:28:38 +01001668 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001669
Angel Pons8f0757e2020-11-11 23:03:36 +01001670 const struct iosav_ssq rd_sequence[] = {
1671 /* DRAM command PREA */
1672 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001673 .sp_cmd_ctrl = {
1674 .command = IOSAV_PRE,
1675 .ranksel_ap = 1,
1676 },
1677 .subseq_ctrl = {
1678 .cmd_executions = 1,
1679 .cmd_delay_gap = 3,
1680 .post_ssq_wait = ctrl->tRP,
1681 .data_direction = SSQ_NA,
1682 },
1683 .sp_cmd_addr = {
Angel Pons5db1b152020-12-13 16:37:53 +01001684 .address = 1 << 10,
Angel Pons3abd2062020-05-03 00:25:02 +02001685 .rowbits = 6,
1686 .bank = 0,
1687 .rank = slotrank,
1688 },
1689 .addr_update = {
1690 .addr_wrap = 18,
1691 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001692 },
1693 /* DRAM command ACT */
1694 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001695 .sp_cmd_ctrl = {
1696 .command = IOSAV_ACT,
1697 .ranksel_ap = 1,
1698 },
1699 .subseq_ctrl = {
1700 .cmd_executions = 1,
1701 .cmd_delay_gap = 3,
1702 .post_ssq_wait = ctrl->tRCD,
1703 .data_direction = SSQ_NA,
1704 },
1705 .sp_cmd_addr = {
1706 .address = 0,
1707 .rowbits = 6,
1708 .bank = 0,
1709 .rank = slotrank,
1710 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001711 },
Angel Ponsf5502312021-02-10 11:08:28 +01001712 /* DRAM command RDA */
Angel Pons8f0757e2020-11-11 23:03:36 +01001713 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001714 .sp_cmd_ctrl = {
1715 .command = IOSAV_RD,
1716 .ranksel_ap = 3,
1717 },
1718 .subseq_ctrl = {
1719 .cmd_executions = 1,
1720 .cmd_delay_gap = 3,
1721 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001722 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001723 ctrl->timings[channel][slotrank].io_latency,
1724 .data_direction = SSQ_RD,
1725 },
1726 .sp_cmd_addr = {
1727 .address = 8,
1728 .rowbits = 6,
1729 .bank = 0,
1730 .rank = slotrank,
1731 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001732 },
1733 };
1734 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001735
Angel Ponsa853e7a2020-12-07 12:28:38 +01001736 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001737
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001738 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001739 u64 res = mchbar_read32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Elyes Haouas3a998072022-11-18 15:11:02 +01001740 res |= ((u64)mchbar_read32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001741 GDCRTRAININGRESULT2(channel))) << 32;
Angel Pons820bce72020-11-14 17:02:55 +01001742
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001743 old = ctrl->timings[channel][slotrank].lanes[lane].tx_dqs;
1744 ctrl->timings[channel][slotrank].lanes[lane].tx_dqs +=
Angel Pons42d033a2021-01-03 15:26:37 +01001745 get_dqs_flyby_adjust(res) * QCLK_PI;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001746
1747 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons7e439c92020-12-07 11:56:01 +01001748 printram("Bval+: %d, %d, %d, % 4d -> % 4d\n", channel, slotrank, lane,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001749 old, ctrl->timings[channel][slotrank].lanes[lane].tx_dqs);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001750 }
1751 }
Angel Pons66780a02021-03-26 13:33:22 +01001752 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001753}
1754
Angel Pons7d115132020-11-14 01:44:44 +01001755static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001756{
Angel Pons7d115132020-11-14 01:44:44 +01001757 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001758
Angel Pons7d115132020-11-14 01:44:44 +01001759 FOR_ALL_POPULATED_CHANNELS {
1760 /* choose an existing rank */
1761 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001762
Angel Pons7d115132020-11-14 01:44:44 +01001763 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001764
Angel Ponsa853e7a2020-12-07 12:28:38 +01001765 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001766
Angel Pons66780a02021-03-26 13:33:22 +01001767 mchbar_setbits32(SCHED_CBIT_ch(channel), 1 << 21);
Angel Pons7d115132020-11-14 01:44:44 +01001768 }
1769
1770 /* Refresh disable */
Angel Pons66780a02021-03-26 13:33:22 +01001771 mchbar_clrbits32(MC_INIT_STATE_G, 1 << 3);
Angel Pons7d115132020-11-14 01:44:44 +01001772
1773 FOR_ALL_POPULATED_CHANNELS {
1774 /* Execute the same command queue */
Angel Ponsa853e7a2020-12-07 12:28:38 +01001775 iosav_run_once_and_wait(channel);
Angel Pons7d115132020-11-14 01:44:44 +01001776 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001777}
1778
Angel Pons7c49cb82020-03-16 23:17:32 +01001779/*
1780 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001781 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001782 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1783 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1784 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1785 * CLK/ADDR/CMD signals have the same routing delay.
1786 *
1787 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1788 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1789 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001790 */
Angel Pons820bce72020-11-14 17:02:55 +01001791static int jedec_write_leveling(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001792{
Angel Pons820bce72020-11-14 17:02:55 +01001793 int channel, slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001794
Angel Pons7d115132020-11-14 01:44:44 +01001795 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001796
Angel Pons7c49cb82020-03-16 23:17:32 +01001797 /* Enable write leveling on all ranks
1798 Disable all DQ outputs
1799 Only NOP is allowed in this mode */
1800 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1801 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001802 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001803
Angel Ponsa1f17142020-11-15 12:50:03 +01001804 /* Needs to be programmed before I/O reset below */
Angel Pons58b609b2020-11-13 14:35:29 +01001805 const union gdcr_training_mod_reg training_mod = {
1806 .write_leveling_mode = 1,
1807 .enable_dqs_wl = 5,
1808 .odt_always_on = 1,
1809 .force_drive_enable = 1,
1810 };
Angel Pons66780a02021-03-26 13:33:22 +01001811 mchbar_write32(GDCRTRAININGMOD, training_mod.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001812
1813 toggle_io_reset();
1814
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001815 /* Set any valid value for tx_dqs, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001816 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons820bce72020-11-14 17:02:55 +01001817 const int err = write_level_rank(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001818 if (err)
1819 return err;
1820 }
1821
Angel Pons7c49cb82020-03-16 23:17:32 +01001822 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001823 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001824 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001825
Angel Pons66780a02021-03-26 13:33:22 +01001826 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001827
1828 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001829 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001830
Angel Pons7c49cb82020-03-16 23:17:32 +01001831 /* Refresh enable */
Angel Pons66780a02021-03-26 13:33:22 +01001832 mchbar_setbits32(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001833
1834 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01001835 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 21);
1836 mchbar_read32(IOSAV_STATUS_ch(channel));
Angel Pons88521882020-01-05 20:21:20 +01001837 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001838
Angel Ponsffd50152020-11-12 11:03:10 +01001839 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001840
Angel Ponsa853e7a2020-12-07 12:28:38 +01001841 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001842 }
1843
1844 toggle_io_reset();
1845
Angel Pons820bce72020-11-14 17:02:55 +01001846 return 0;
1847}
1848
1849int write_training(ramctr_timing *ctrl)
1850{
Angel Ponsc6742232020-11-15 13:26:21 +01001851 int channel, slotrank;
Angel Pons820bce72020-11-14 17:02:55 +01001852 int err;
1853
Angel Pons4d192822020-12-12 13:54:37 +01001854 /*
1855 * Set the DEC_WRD bit, required for the write flyby algorithm.
1856 * Needs to be done before starting the write training procedure.
1857 */
Angel Pons820bce72020-11-14 17:02:55 +01001858 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01001859 mchbar_setbits32(TC_RWP_ch(channel), 1 << 27);
Angel Pons820bce72020-11-14 17:02:55 +01001860
Angel Pons4c76d252020-11-15 13:06:53 +01001861 printram("CPE\n");
1862
Angel Pons820bce72020-11-14 17:02:55 +01001863 err = jedec_write_leveling(ctrl);
1864 if (err)
1865 return err;
1866
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001867 printram("CPF\n");
1868
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001869 FOR_ALL_POPULATED_CHANNELS {
1870 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001871 }
1872
1873 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01001874 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001875 if (err)
1876 return err;
1877 }
1878
1879 FOR_ALL_POPULATED_CHANNELS
1880 program_timings(ctrl, channel);
1881
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001882 /* measure and adjust tx_dqs timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01001883 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001884
1885 FOR_ALL_POPULATED_CHANNELS
1886 program_timings(ctrl, channel);
1887
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001888 return 0;
1889}
1890
Angel Ponsbf13ef02020-11-11 18:40:06 +01001891static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001892{
1893 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001894 int tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001895 int lanes_ok = 0;
1896 int ctr = 0;
1897 int lane;
1898
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001899 for (tx_dq_delta = -5; tx_dq_delta <= 5; tx_dq_delta++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001900 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01001901 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
1902 saved_rt.lanes[lane].tx_dq + tx_dq_delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001903 }
1904 program_timings(ctrl, channel);
1905 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001906 mchbar_write32(IOSAV_By_ERROR_COUNT(lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001907 }
1908
Angel Pons765d4652020-11-11 14:44:35 +01001909 /* Reset read WDB pointer */
Angel Pons66780a02021-03-26 13:33:22 +01001910 mchbar_write32(IOSAV_DATA_CTL_ch(channel), 0x1f);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001911
Angel Pons88521882020-01-05 20:21:20 +01001912 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001913
Angel Ponsffd50152020-11-12 11:03:10 +01001914 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01001915
1916 /* Program LFSR for the RD/WR subsequences */
Angel Pons66780a02021-03-26 13:33:22 +01001917 mchbar_write32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1), 0x389abcd);
1918 mchbar_write32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2), 0x389abcd);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001919
Angel Ponsa853e7a2020-12-07 12:28:38 +01001920 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001921
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001922 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01001923 u32 r32 = mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001924
1925 if (r32 == 0)
1926 lanes_ok |= 1 << lane;
1927 }
1928 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02001929 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001930 break;
1931 }
1932
1933 ctrl->timings[channel][slotrank] = saved_rt;
1934
Patrick Rudolphdd662872017-10-28 18:20:11 +02001935 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001936}
1937
Angel Pons88521882020-01-05 20:21:20 +01001938static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001939{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301940 unsigned int i, j;
Angel Pons5db1b152020-12-13 16:37:53 +01001941 unsigned int offset = get_precedening_channels(ctrl, channel) * 64;
1942 unsigned int step = 64 * num_of_channels(ctrl);
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001943 uintptr_t addr;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001944
1945 if (patno) {
1946 u8 base8 = 0x80 >> ((patno - 1) % 8);
1947 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
1948 for (i = 0; i < 32; i++) {
1949 for (j = 0; j < 16; j++) {
1950 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001951
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001952 if (invert[patno - 1][i] & (1 << (j / 2)))
1953 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01001954
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001955 addr = (1 << 26) + offset + i * step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001956 write32p(addr, val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001957 }
1958 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001959 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01001960 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
1961 for (j = 0; j < 16; j++) {
1962 const u32 val = pattern[i][j];
Patrick Rudolphb50b6a52020-08-20 16:50:01 +02001963 addr = (1 << 26) + offset + i * step + j * 4;
Elyes Haouasee4646e2022-12-04 09:16:07 +01001964 write32p(addr, val);
Angel Pons7c49cb82020-03-16 23:17:32 +01001965 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001966 }
1967 sfence();
1968 }
Angel Pons765d4652020-11-11 14:44:35 +01001969
1970 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001971}
1972
Angel Pons88521882020-01-05 20:21:20 +01001973static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974{
Angel Pons7d115132020-11-14 01:44:44 +01001975 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001976
Angel Pons7c49cb82020-03-16 23:17:32 +01001977 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001978 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001979
1980 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001981 dram_mrscommands(ctrl);
1982
1983 toggle_io_reset();
1984}
1985
Angel Pons42d033a2021-01-03 15:26:37 +01001986#define CT_MIN_PI (-CCC_MAX_PI)
1987#define CT_MAX_PI (+CCC_MAX_PI + 1)
Angel Ponsbf13ef02020-11-11 18:40:06 +01001988#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
1989
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001990#define MIN_C320C_LEN 13
1991
1992static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
1993{
1994 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
1995 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01001996 int command_pi;
1997 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001998 int delta = 0;
1999
2000 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2001
2002 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002003 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002004 }
2005
2006 ctrl->cmd_stretch[channel] = cmd_stretch;
2007
Angel Pons7a612742020-11-12 13:34:03 +01002008 const union tc_rap_reg tc_rap = {
2009 .tRRD = ctrl->tRRD,
2010 .tRTP = ctrl->tRTP,
2011 .tCKE = ctrl->tCKE,
2012 .tWTR = ctrl->tWTR,
2013 .tFAW = ctrl->tFAW,
2014 .tWR = ctrl->tWR,
2015 .tCMD = ctrl->cmd_stretch[channel],
2016 };
Angel Pons66780a02021-03-26 13:33:22 +01002017 mchbar_write32(TC_RAP_ch(channel), tc_rap.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002018
2019 if (ctrl->cmd_stretch[channel] == 2)
2020 delta = 2;
2021 else if (ctrl->cmd_stretch[channel] == 0)
2022 delta = 4;
2023
2024 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002025 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002026 }
2027
Angel Ponsbf13ef02020-11-11 18:40:06 +01002028 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002029 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002030 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002031 }
2032 program_timings(ctrl, channel);
2033 reprogram_320c(ctrl);
2034 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002035 stat[slotrank][command_pi - CT_MIN_PI] =
2036 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002037 }
2038 }
2039 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002040 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002041
Angel Ponsbf13ef02020-11-11 18:40:06 +01002042 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Angel Pons7e439c92020-12-07 11:56:01 +01002043 printram("cmd_stretch: %d, %d: % 4d-% 4d-% 4d\n",
Patrick Rudolph368b6152016-11-25 16:36:52 +01002044 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002045
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002046 if (rn.all || rn.length < MIN_C320C_LEN) {
2047 FOR_ALL_POPULATED_RANKS {
2048 ctrl->timings[channel][slotrank] =
2049 saved_timings[channel][slotrank];
2050 }
2051 return MAKE_ERR;
2052 }
2053 }
2054
2055 return 0;
2056}
2057
Angel Pons7c49cb82020-03-16 23:17:32 +01002058/*
2059 * Adjust CMD phase shift and try multiple command rates.
2060 * A command rate of 2T doubles the time needed for address and command decode.
2061 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002062int command_training(ramctr_timing *ctrl)
2063{
2064 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002065
2066 FOR_ALL_POPULATED_CHANNELS {
2067 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002068 }
2069
2070 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002071 int cmdrate, err;
2072
2073 /*
2074 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002075 * Issue:
Angel Pons30791632020-12-12 12:28:29 +01002076 * While command training seems to succeed, raminit will fail in write training.
Angel Pons7c49cb82020-03-16 23:17:32 +01002077 *
2078 * Workaround:
2079 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2080 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002081 *
2082 * Single DIMM per channel:
2083 * Try command rate 1T and 2T
2084 */
2085 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002086 if (ctrl->tCMD)
2087 /* XMP gives the CMD rate in clock ticks, not ns */
2088 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002089
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002090 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002091 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2092
2093 if (!err)
2094 break;
2095 }
2096
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002097 if (err) {
Angel Pons30791632020-12-12 12:28:29 +01002098 printk(BIOS_EMERG, "Command training failed: %d\n", channel);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002099 return err;
2100 }
2101
Angel Pons891f2bc2020-01-10 01:27:28 +01002102 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002103 }
2104
2105 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002106 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002107
2108 reprogram_320c(ctrl);
2109 return 0;
2110}
2111
Angel Pons4c79f932020-11-14 01:26:52 +01002112static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002113{
Angel Pons96a06dd2020-11-14 00:33:18 +01002114 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002115 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002116 int lane;
2117
Angel Pons96a06dd2020-11-14 00:33:18 +01002118 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002119 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002120 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = dqs_pi;
2121 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002122 }
2123 program_timings(ctrl, channel);
2124
2125 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002126 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane), 0);
2127 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002128 }
2129
Angel Pons88521882020-01-05 20:21:20 +01002130 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002131
Angel Ponsffd50152020-11-12 11:03:10 +01002132 iosav_write_read_mpr_sequence(
2133 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002134
Angel Ponsa853e7a2020-12-07 12:28:38 +01002135 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002136
2137 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002138 stats[lane][dqs_pi] = mchbar_read32(
2139 IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002140 }
2141 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002142
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002144 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002145 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002146
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002147 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002148 printk(BIOS_EMERG, "Read MPR training failed: %d, %d, %d\n", channel,
Angel Pons7c49cb82020-03-16 23:17:32 +01002149 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002150 return MAKE_ERR;
2151 }
Angel Pons7e439c92020-12-07 11:56:01 +01002152 printram("eval %d, %d, %d: % 4d\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002153 }
2154 return 0;
2155}
2156
Angel Pons60971dc2020-11-14 00:49:38 +01002157static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2158{
2159 int slotrank, lane;
2160
2161 fill_pattern0(ctrl, channel, 0, 0);
2162 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002163 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane), 0);
2164 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Angel Pons60971dc2020-11-14 00:49:38 +01002165 }
2166
2167 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002168 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 16;
2169 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 16;
Angel Pons60971dc2020-11-14 00:49:38 +01002170 }
2171
2172 program_timings(ctrl, channel);
2173
2174 FOR_ALL_POPULATED_RANKS {
2175 wait_for_iosav(channel);
2176
2177 iosav_write_read_mpr_sequence(
2178 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2179
Angel Ponsa853e7a2020-12-07 12:28:38 +01002180 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002181 }
2182
2183 /* XXX: check any measured value ? */
2184
2185 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002186 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n = 48;
2187 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p = 48;
Angel Pons60971dc2020-11-14 00:49:38 +01002188 }
2189
2190 program_timings(ctrl, channel);
2191
2192 FOR_ALL_POPULATED_RANKS {
2193 wait_for_iosav(channel);
2194
2195 iosav_write_read_mpr_sequence(
2196 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2197
Angel Ponsa853e7a2020-12-07 12:28:38 +01002198 iosav_run_once_and_wait(channel);
Angel Pons60971dc2020-11-14 00:49:38 +01002199 }
2200
2201 /* XXX: check any measured value ? */
2202
2203 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002204 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane),
2205 ~mchbar_read32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff);
Angel Pons60971dc2020-11-14 00:49:38 +01002206 }
2207}
2208
Angel Pons4c79f932020-11-14 01:26:52 +01002209int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002210{
2211 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2212 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2213 int channel, slotrank, lane;
2214 int err;
2215
Angel Pons66780a02021-03-26 13:33:22 +01002216 mchbar_write32(GDCRTRAININGMOD, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002217
2218 toggle_io_reset();
2219
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002220 FOR_ALL_POPULATED_CHANNELS {
Angel Pons60971dc2020-11-14 00:49:38 +01002221 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002222
2223 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002224 }
2225
Angel Pons0c3936e2020-03-22 12:49:27 +01002226 /*
2227 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2228 * also use a single loop. It would seem that it is a debugging configuration.
2229 */
Angel Pons66780a02021-03-26 13:33:22 +01002230 mchbar_write32(IOSAV_DC_MASK, 3 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002231 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002232
2233 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002234 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002235 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002236 if (err)
2237 return err;
2238 }
2239
Angel Pons66780a02021-03-26 13:33:22 +01002240 mchbar_write32(IOSAV_DC_MASK, 2 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002241 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002242
2243 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002244 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002245 rising_edges[channel][slotrank]);
2246 if (err)
2247 return err;
2248 }
2249
Angel Pons66780a02021-03-26 13:33:22 +01002250 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002251
2252 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002253 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002254 falling_edges[channel][slotrank][lane];
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002255 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002256 rising_edges[channel][slotrank][lane];
2257 }
2258
2259 FOR_ALL_POPULATED_CHANNELS {
2260 program_timings(ctrl, channel);
2261 }
2262
Angel Pons50a6fe72020-11-14 01:18:14 +01002263 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002264 mchbar_write32(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002265 }
2266 return 0;
2267}
2268
Angel Pons08f749d2020-11-17 16:50:56 +01002269static int find_agrsv_read_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002270{
Angel Pons08f749d2020-11-17 16:50:56 +01002271 const int rd_vref_offsets[] = { 0, 0xc, 0x2c };
2272
Angel Pons7c49cb82020-03-16 23:17:32 +01002273 u32 raw_stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002274 int lower[NUM_LANES];
2275 int upper[NUM_LANES];
Angel Pons08f749d2020-11-17 16:50:56 +01002276 int lane, i, read_pi, pat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002277
2278 FOR_ALL_LANES {
2279 lower[lane] = 0;
2280 upper[lane] = MAX_EDGE_TIMING;
2281 }
2282
Angel Pons08f749d2020-11-17 16:50:56 +01002283 for (i = 0; i < ARRAY_SIZE(rd_vref_offsets); i++) {
Angel Pons58b609b2020-11-13 14:35:29 +01002284 const union gdcr_training_mod_reg training_mod = {
Angel Pons08f749d2020-11-17 16:50:56 +01002285 .vref_gen_ctl = rd_vref_offsets[i],
Angel Pons58b609b2020-11-13 14:35:29 +01002286 };
Angel Pons66780a02021-03-26 13:33:22 +01002287 mchbar_write32(GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons58b609b2020-11-13 14:35:29 +01002288 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), training_mod.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002289
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002290 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2291 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002292 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002293
Angel Pons08f749d2020-11-17 16:50:56 +01002294 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002295 FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002296 ctrl->timings[channel][slotrank].lanes[lane]
2297 .rx_dqs_p = read_pi;
2298 ctrl->timings[channel][slotrank].lanes[lane]
2299 .rx_dqs_n = read_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002300 }
2301 program_timings(ctrl, channel);
2302
2303 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002304 mchbar_write32(IOSAV_By_ERROR_COUNT_ch(channel, lane),
2305 0);
2306 mchbar_read32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307 }
Angel Pons88521882020-01-05 20:21:20 +01002308 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002309
Angel Ponsffd50152020-11-12 11:03:10 +01002310 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002311
Angel Ponsa853e7a2020-12-07 12:28:38 +01002312 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002313
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002314 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002315 mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002316 }
2317
Angel Pons7c49cb82020-03-16 23:17:32 +01002318 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons66780a02021-03-26 13:33:22 +01002319 raw_stats[read_pi] = mchbar_read32(
2320 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002321 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002322
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002323 FOR_ALL_LANES {
Angel Pons08f749d2020-11-17 16:50:56 +01002324 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002325 struct run rn;
Angel Pons08f749d2020-11-17 16:50:56 +01002326
2327 for (read_pi = 0; read_pi <= MAX_EDGE_TIMING; read_pi++)
2328 stats[read_pi] = !!(raw_stats[read_pi] & (1 << lane));
Angel Pons7c49cb82020-03-16 23:17:32 +01002329
2330 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2331
Angel Pons7e439c92020-12-07 11:56:01 +01002332 printram("edges: %d, %d, %d: % 4d-% 4d-% 4d, "
2333 "% 4d-% 4d\n", channel, slotrank, i, rn.start,
Angel Pons7c49cb82020-03-16 23:17:32 +01002334 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002335 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002336
2337 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2338 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2339
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002340 edges[lane] = (lower[lane] + upper[lane]) / 2;
2341 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons30791632020-12-12 12:28:29 +01002342 printk(BIOS_EMERG, "Aggressive read training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002343 "%d, %d, %d\n", channel, slotrank, lane);
2344
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002345 return MAKE_ERR;
2346 }
2347 }
2348 }
2349 }
2350
Angel Ponsa93f46e2020-11-17 16:54:01 +01002351 /* Restore nominal Vref after training */
Angel Pons66780a02021-03-26 13:33:22 +01002352 mchbar_write32(GDCRTRAININGMOD_ch(channel), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002353 printram("CPA\n");
2354 return 0;
2355}
2356
Angel Pons08f749d2020-11-17 16:50:56 +01002357int aggressive_read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002358{
2359 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002360 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2361 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002362
Angel Pons7c49cb82020-03-16 23:17:32 +01002363 /*
2364 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2365 * also use a single loop. It would seem that it is a debugging configuration.
2366 */
Angel Pons66780a02021-03-26 13:33:22 +01002367 mchbar_write32(IOSAV_DC_MASK, 3 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002368 printram("discover falling edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 3 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002369
2370 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002371 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002372 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002373 if (err)
2374 return err;
2375 }
2376
Angel Pons66780a02021-03-26 13:33:22 +01002377 mchbar_write32(IOSAV_DC_MASK, 2 << 8);
Angel Pons5db1b152020-12-13 16:37:53 +01002378 printram("discover rising edges aggressive:\n[%x] = %x\n", IOSAV_DC_MASK, 2 << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002379
2380 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons08f749d2020-11-17 16:50:56 +01002381 err = find_agrsv_read_margin(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002382 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002383 if (err)
2384 return err;
2385 }
2386
Angel Pons66780a02021-03-26 13:33:22 +01002387 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002388
2389 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002390 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_n =
Angel Pons7c49cb82020-03-16 23:17:32 +01002391 falling_edges[channel][slotrank][lane];
2392
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002393 ctrl->timings[channel][slotrank].lanes[lane].rx_dqs_p =
Angel Pons7c49cb82020-03-16 23:17:32 +01002394 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002395 }
2396
2397 FOR_ALL_POPULATED_CHANNELS
2398 program_timings(ctrl, channel);
2399
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002400 return 0;
2401}
2402
Angel Pons2a7d7522020-11-19 12:49:07 +01002403static void test_aggressive_write(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002404{
Angel Pons88521882020-01-05 20:21:20 +01002405 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002406
Angel Ponsffd50152020-11-12 11:03:10 +01002407 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002408
Angel Ponsa853e7a2020-12-07 12:28:38 +01002409 iosav_run_once_and_wait(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002410}
2411
Angel Pons2a7d7522020-11-19 12:49:07 +01002412static void set_write_vref(const int channel, const u8 wr_vref)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002413{
Angel Pons66780a02021-03-26 13:33:22 +01002414 mchbar_clrsetbits32(GDCRCMDDEBUGMUXCFG_Cz_S(channel), 0x3f << 24, wr_vref << 24);
Angel Pons2a7d7522020-11-19 12:49:07 +01002415 udelay(2);
2416}
2417
2418int aggressive_write_training(ramctr_timing *ctrl)
2419{
2420 const u8 wr_vref_offsets[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002421 int i, pat;
2422
2423 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2424 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2425 int channel, slotrank, lane;
2426
Angel Pons9fbb1b02020-11-19 12:53:36 +01002427 /* Changing the write Vref is only supported on some Ivy Bridge SKUs */
2428 if (!IS_IVY_CPU(ctrl->cpu))
2429 return 0;
2430
2431 if (!(pci_read_config32(HOST_BRIDGE, CAPID0_A) & CAPID_WRTVREF))
2432 return 0;
2433
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002434 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2435 lower[channel][slotrank][lane] = 0;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002436 upper[channel][slotrank][lane] = MAX_TX_DQ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002437 }
2438
Angel Pons2a7d7522020-11-19 12:49:07 +01002439 /* Only enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization on later steppings */
2440 const bool enable_iosav_opt = IS_IVY_CPU_D(ctrl->cpu) || IS_IVY_CPU_E(ctrl->cpu);
2441
2442 if (enable_iosav_opt)
Angel Pons66780a02021-03-26 13:33:22 +01002443 mchbar_write32(MCMNTS_SPARE, 1);
Angel Pons2a7d7522020-11-19 12:49:07 +01002444
Martin Roth50863da2021-10-01 14:37:30 -06002445 printram("Aggressive write training:\n");
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002446
Angel Pons2a7d7522020-11-19 12:49:07 +01002447 for (i = 0; i < ARRAY_SIZE(wr_vref_offsets); i++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002448 FOR_ALL_POPULATED_CHANNELS {
Angel Pons2a7d7522020-11-19 12:49:07 +01002449 set_write_vref(channel, wr_vref_offsets[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002450
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002451 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2452 FOR_ALL_POPULATED_RANKS {
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002453 int tx_dq;
2454 u32 raw_stats[MAX_TX_DQ + 1];
2455 int stats[MAX_TX_DQ + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002456
2457 /* Make sure rn.start < rn.end */
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002458 stats[MAX_TX_DQ] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002459
2460 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002461
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002462 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002463 FOR_ALL_LANES {
2464 ctrl->timings[channel][slotrank]
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002465 .lanes[lane].tx_dq = tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01002466 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002467 program_timings(ctrl, channel);
2468
Angel Pons2a7d7522020-11-19 12:49:07 +01002469 test_aggressive_write(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470
Angel Pons66780a02021-03-26 13:33:22 +01002471 raw_stats[tx_dq] = mchbar_read32(
Angel Pons098240eb2020-03-22 12:55:32 +01002472 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002473 }
2474 FOR_ALL_LANES {
2475 struct run rn;
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002476 for (tx_dq = 0; tx_dq < MAX_TX_DQ; tx_dq++) {
2477 stats[tx_dq] = !!(raw_stats[tx_dq]
Angel Pons7c49cb82020-03-16 23:17:32 +01002478 & (1 << lane));
2479 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002480
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002481 rn = get_longest_zero_run(stats, MAX_TX_DQ + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002482 if (rn.all) {
Angel Pons30791632020-12-12 12:28:29 +01002483 printk(BIOS_EMERG, "Aggressive "
2484 "write training failed: "
Angel Pons7c49cb82020-03-16 23:17:32 +01002485 "%d, %d, %d\n", channel,
2486 slotrank, lane);
2487
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002488 return MAKE_ERR;
2489 }
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002490 printram("tx_dq: %d, %d, %d: "
Angel Pons7e439c92020-12-07 11:56:01 +01002491 "% 4d-% 4d-% 4d, "
2492 "% 4d-% 4d\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002493 i, rn.start, rn.middle, rn.end,
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002494 rn.start + ctrl->tx_dq_offset[i],
2495 rn.end - ctrl->tx_dq_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002496
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002497 lower[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002498 MAX(rn.start + ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002500
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002501 upper[channel][slotrank][lane] =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002502 MIN(rn.end - ctrl->tx_dq_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002503 upper[channel][slotrank][lane]);
2504
2505 }
2506 }
2507 }
2508 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002509 }
2510
Angel Pons2a7d7522020-11-19 12:49:07 +01002511 FOR_ALL_CHANNELS {
2512 /* Restore nominal write Vref after training */
2513 set_write_vref(channel, 0);
2514 }
2515
2516 /* Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization */
2517 if (enable_iosav_opt)
Angel Pons66780a02021-03-26 13:33:22 +01002518 mchbar_write32(MCMNTS_SPARE, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519
2520 printram("CPB\n");
2521
2522 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7e439c92020-12-07 11:56:01 +01002523 printram("tx_dq %d, %d, %d: % 4d\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002524 (lower[channel][slotrank][lane] +
2525 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002526
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002527 ctrl->timings[channel][slotrank].lanes[lane].tx_dq =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002528 (lower[channel][slotrank][lane] +
2529 upper[channel][slotrank][lane]) / 2;
2530 }
2531 FOR_ALL_POPULATED_CHANNELS {
2532 program_timings(ctrl, channel);
2533 }
2534 return 0;
2535}
2536
Angel Pons88521882020-01-05 20:21:20 +01002537void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002538{
2539 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002540 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002541
2542 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2543 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002544 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002545 FOR_ALL_LANES mat =
Angel Ponsc8ac2cc2020-11-19 22:50:54 +01002546 MAX(ctrl->timings[channel][slotrank].lanes[lane].rcven, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002547 printram("normalize %d, %d, %d: mat %d\n",
2548 channel, slotrank, lane, mat);
2549
Felix Heldef4fe3e2019-12-31 14:15:05 +01002550 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002551 printram("normalize %d, %d, %d: delta %d\n",
2552 channel, slotrank, lane, delta);
2553
Angel Pons88521882020-01-05 20:21:20 +01002554 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002555 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002556 }
2557
2558 FOR_ALL_POPULATED_CHANNELS {
2559 program_timings(ctrl, channel);
2560 }
2561}
2562
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002563int channel_test(ramctr_timing *ctrl)
2564{
2565 int channel, slotrank, lane;
2566
2567 slotrank = 0;
2568 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01002569 if (mchbar_read32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002570 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002571 return MAKE_ERR;
2572 }
2573 FOR_ALL_POPULATED_CHANNELS {
2574 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002575 }
2576
2577 for (slotrank = 0; slotrank < 4; slotrank++)
2578 FOR_ALL_CHANNELS
2579 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2580 FOR_ALL_LANES {
Angel Pons66780a02021-03-26 13:33:22 +01002581 mchbar_write32(IOSAV_By_ERROR_COUNT(lane), 0);
2582 mchbar_write32(IOSAV_By_BW_SERROR_C(lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002583 }
Angel Pons88521882020-01-05 20:21:20 +01002584 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002585
Angel Ponsffd50152020-11-12 11:03:10 +01002586 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002587
Angel Ponsa853e7a2020-12-07 12:28:38 +01002588 iosav_run_once_and_wait(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002589
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002590 FOR_ALL_LANES
Angel Pons66780a02021-03-26 13:33:22 +01002591 if (mchbar_read32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002592 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2593 channel, slotrank, lane);
2594 return MAKE_ERR;
2595 }
2596 }
2597 return 0;
2598}
2599
Patrick Rudolphdd662872017-10-28 18:20:11 +02002600void channel_scrub(ramctr_timing *ctrl)
2601{
2602 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002603 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002604
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002605 FOR_ALL_POPULATED_CHANNELS {
2606 wait_for_iosav(channel);
2607 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002608 }
2609
2610 /*
2611 * During runtime the "scrubber" will periodically scan through the memory in the
2612 * physical address space, to identify and fix CRC errors.
2613 * The following loops writes to every DRAM address, setting the ECC bits to the
2614 * correct value. A read from this location will no longer return a CRC error,
2615 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002616 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002617 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2618 * and firmware running in x86_32.
2619 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002620 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2621 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002622 for (bank = 0; bank < 8; bank++) {
2623 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002624
Angel Pons8f0757e2020-11-11 23:03:36 +01002625 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2626 const struct iosav_ssq sequence[] = {
2627 /*
2628 * DRAM command ACT
2629 * Opens the row for writing.
2630 */
2631 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002632 .sp_cmd_ctrl = {
2633 .command = IOSAV_ACT,
2634 .ranksel_ap = 1,
2635 },
2636 .subseq_ctrl = {
2637 .cmd_executions = 1,
2638 .cmd_delay_gap = gap,
2639 .post_ssq_wait = ctrl->tRCD,
2640 .data_direction = SSQ_NA,
2641 },
2642 .sp_cmd_addr = {
2643 .address = row,
2644 .rowbits = 6,
2645 .bank = bank,
2646 .rank = slotrank,
2647 },
2648 .addr_update = {
2649 .inc_addr_1 = 1,
2650 .addr_wrap = 18,
2651 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002652 },
2653 /*
2654 * DRAM command WR
2655 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2656 * bytes.
2657 */
2658 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002659 .sp_cmd_ctrl = {
2660 .command = IOSAV_WR,
2661 .ranksel_ap = 1,
2662 },
2663 .subseq_ctrl = {
2664 .cmd_executions = 129,
2665 .cmd_delay_gap = 4,
2666 .post_ssq_wait = ctrl->tWTR +
2667 ctrl->CWL + 8,
2668 .data_direction = SSQ_WR,
2669 },
2670 .sp_cmd_addr = {
2671 .address = row,
2672 .rowbits = 0,
2673 .bank = bank,
2674 .rank = slotrank,
2675 },
2676 .addr_update = {
2677 .inc_addr_8 = 1,
2678 .addr_wrap = 9,
2679 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002680 },
2681 /*
2682 * DRAM command PRE
2683 * Closes the row.
2684 */
2685 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002686 .sp_cmd_ctrl = {
2687 .command = IOSAV_PRE,
2688 .ranksel_ap = 1,
2689 },
2690 .subseq_ctrl = {
2691 .cmd_executions = 1,
2692 .cmd_delay_gap = 4,
2693 .post_ssq_wait = ctrl->tRP,
2694 .data_direction = SSQ_NA,
2695 },
2696 .sp_cmd_addr = {
2697 .address = 0,
2698 .rowbits = 6,
2699 .bank = bank,
2700 .rank = slotrank,
2701 },
2702 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002703 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002704 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002705 },
2706 };
2707 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002708
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002709 iosav_run_queue(channel, 16, 0);
2710
2711 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002712 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002713 }
2714 }
2715}
2716
Angel Pons88521882020-01-05 20:21:20 +01002717void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002718{
2719 int channel;
2720
Angel Pons7c49cb82020-03-16 23:17:32 +01002721 /* 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 +01002722 static u32 seeds[NUM_CHANNELS][3] = {
2723 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2724 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2725 };
2726 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002727 mchbar_clrbits32(SCHED_CBIT_ch(channel), 1 << 28);
2728 mchbar_write32(SCRAMBLING_SEED_1_ch(channel), seeds[channel][0]);
2729 mchbar_write32(SCRAMBLING_SEED_2_HI_ch(channel), seeds[channel][1]);
2730 mchbar_write32(SCRAMBLING_SEED_2_LO_ch(channel), seeds[channel][2]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002731 }
2732}
2733
Angel Pons89ae6b82020-03-21 13:23:32 +01002734void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002735{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002736 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons66780a02021-03-26 13:33:22 +01002737 mchbar_write32(SC_WDBWM, 0x141d1519);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002738 } else {
Angel Pons66780a02021-03-26 13:33:22 +01002739 mchbar_write32(SC_WDBWM, 0x551d1519);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002740 }
2741}
2742
Angel Pons88521882020-01-05 20:21:20 +01002743void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002744{
2745 int channel;
2746
2747 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002748 /* Always drive command bus */
Angel Pons66780a02021-03-26 13:33:22 +01002749 mchbar_setbits32(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002750 }
2751
2752 udelay(1);
2753
2754 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002755 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002756 }
2757}
2758
Angel Pons7c49cb82020-03-16 23:17:32 +01002759void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002760{
Angel Pons11463322020-11-19 11:04:28 +01002761 /* Use a larger delay when running fast to improve stability */
2762 const u32 tRWDRDD_inc = ctrl->tCK <= TCK_1066MHZ ? 4 : 2;
2763
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002764 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002765
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002766 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002767 int min_pi = 10000;
2768 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002769
2770 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002771 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2772 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002773 }
2774
Angel Pons7a612742020-11-12 13:34:03 +01002775 const u32 tWRDRDD = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002776
Angel Pons7a612742020-11-12 13:34:03 +01002777 const u32 val = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 3 : 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002778
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002779 dram_odt_stretch(ctrl, channel);
2780
Angel Pons7a612742020-11-12 13:34:03 +01002781 const union tc_rwp_reg tc_rwp = {
2782 .tRRDR = 0,
2783 .tRRDD = val,
2784 .tWWDR = val,
2785 .tWWDD = val,
Angel Pons11463322020-11-19 11:04:28 +01002786 .tRWDRDD = ctrl->ref_card_offset[channel] + tRWDRDD_inc,
Angel Pons7a612742020-11-12 13:34:03 +01002787 .tWRDRDD = tWRDRDD,
2788 .tRWSR = 2,
2789 .dec_wrd = 1,
2790 };
Angel Pons66780a02021-03-26 13:33:22 +01002791 mchbar_write32(TC_RWP_ch(channel), tc_rwp.raw);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002792 }
2793}
2794
Angel Pons88521882020-01-05 20:21:20 +01002795void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002796{
2797 int channel;
2798 FOR_ALL_POPULATED_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002799 mchbar_write32(MC_INIT_STATE_ch(channel), 1 << 12 | ctrl->rankmap[channel]);
2800 mchbar_clrbits32(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002801 }
2802}
2803
Angel Pons7c49cb82020-03-16 23:17:32 +01002804/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2805static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002806{
Angel Pons88521882020-01-05 20:21:20 +01002807 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002808}
2809
Angel Pons7c49cb82020-03-16 23:17:32 +01002810/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002811void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002812{
2813 int channel;
2814 int t1_cycles = 0, t1_ns = 0, t2_ns;
2815 int t3_ns;
2816 u32 r32;
2817
Angel Pons7c49cb82020-03-16 23:17:32 +01002818 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons66780a02021-03-26 13:33:22 +01002819 mchbar_write32(WMM_READ_CONFIG, 0x46);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002820
Angel Pons7a612742020-11-12 13:34:03 +01002821 FOR_ALL_CHANNELS {
2822 union tc_othp_reg tc_othp = {
Angel Pons66780a02021-03-26 13:33:22 +01002823 .raw = mchbar_read32(TC_OTHP_ch(channel)),
Angel Pons7a612742020-11-12 13:34:03 +01002824 };
2825 tc_othp.tCPDED = 1;
Angel Pons66780a02021-03-26 13:33:22 +01002826 mchbar_write32(TC_OTHP_ch(channel), tc_othp.raw);
Patrick Rudolph652c4912017-10-31 11:36:55 +01002827
Patrick Rudolph68642ca2023-12-21 07:08:49 +01002828 /* 64 DCLKs until idle, decision per rank */
2829 r32 = get_power_down_mode(ctrl, channel) << 8 | 64;
2830 mchbar_write32(PM_PDWN_CONFIG_ch(channel), r32);
Patrick Rudolph652c4912017-10-31 11:36:55 +01002831
Angel Pons66780a02021-03-26 13:33:22 +01002832 mchbar_write32(PM_TRML_M_CONFIG_ch(channel), 0x00000aaa);
Patrick Rudolph68642ca2023-12-21 07:08:49 +01002833 }
Felix Heldf9b826a2018-07-30 17:56:52 +02002834
Angel Pons66780a02021-03-26 13:33:22 +01002835 mchbar_write32(PM_BW_LIMIT_CONFIG, 0x5f7003ff);
Patrick Rudolphe27a26b2023-12-21 14:45:27 +01002836 if (IS_SANDY_CPU(ctrl->cpu))
2837 mchbar_write32(PM_DLL_CONFIG, 0x000330f0);
2838 else
2839 mchbar_write32(PM_DLL_CONFIG, 0x00073000 | ctrl->mdll_wake_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002840
2841 FOR_ALL_CHANNELS {
2842 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002843 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002844 case 0:
Angel Pons66780a02021-03-26 13:33:22 +01002845 mchbar_write32(PM_CMD_PWR_ch(channel), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002846 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002847 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002848 case 1:
2849 case 4:
2850 case 5:
Angel Pons66780a02021-03-26 13:33:22 +01002851 mchbar_write32(PM_CMD_PWR_ch(channel), 0x00373131);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002852 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01002853 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002854 default:
Angel Pons66780a02021-03-26 13:33:22 +01002855 mchbar_write32(PM_CMD_PWR_ch(channel), 0x009b6ea1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002856 break;
2857 }
2858 }
2859
Angel Pons66780a02021-03-26 13:33:22 +01002860 mchbar_write32(MEM_TRML_ESTIMATION_CONFIG, 0xca9171e5);
2861 mchbar_clrsetbits32(MEM_TRML_THRESHOLDS_CONFIG, 0x00ffffff, 0x00e4d5d0);
2862 mchbar_clrbits32(MEM_TRML_INTERRUPT, 0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02002863
Angel Pons7a612742020-11-12 13:34:03 +01002864 FOR_ALL_CHANNELS {
2865 union tc_rfp_reg tc_rfp = {
Angel Pons66780a02021-03-26 13:33:22 +01002866 .raw = mchbar_read32(TC_RFP_ch(channel)),
Angel Pons7a612742020-11-12 13:34:03 +01002867 };
2868 tc_rfp.refresh_2x_control = 1;
Angel Pons66780a02021-03-26 13:33:22 +01002869 mchbar_write32(TC_RFP_ch(channel), tc_rfp.raw);
Angel Pons7a612742020-11-12 13:34:03 +01002870 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002871
Angel Pons66780a02021-03-26 13:33:22 +01002872 mchbar_setbits32(MC_INIT_STATE_G, 1 << 0);
2873 mchbar_setbits32(MC_INIT_STATE_G, 1 << 7);
2874 mchbar_write32(BANDTIMERS_SNB, 0xfa);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002875
Angel Pons7c49cb82020-03-16 23:17:32 +01002876 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002877 FOR_ALL_POPULATED_CHANNELS
2878 break;
2879
Angel Pons66780a02021-03-26 13:33:22 +01002880 t1_cycles = (mchbar_read32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
2881 r32 = mchbar_read32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01002882 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002883 t1_cycles += (r32 & 0xfff);
Angel Pons66780a02021-03-26 13:33:22 +01002884 t1_cycles += mchbar_read32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002885 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01002886 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002887 t1_ns += 500;
2888
Angel Pons66780a02021-03-26 13:33:22 +01002889 t2_ns = 10 * ((mchbar_read32(SAPMTIMERS) >> 8) & 0xfff);
2890 if (mchbar_read32(SAPMCTL) & 8) {
2891 t3_ns = 10 * ((mchbar_read32(BANDTIMERS_IVB) >> 8) & 0xfff);
2892 t3_ns += 10 * (mchbar_read32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01002893 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002894 t3_ns = 500;
2895 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002896
2897 /* The graphics driver will use these watermark values */
2898 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Pons66780a02021-03-26 13:33:22 +01002899 mchbar_clrsetbits32(SSKPD, 0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01002900 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
2901 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002902}
2903
Angel Pons88521882020-01-05 20:21:20 +01002904void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002905{
Angel Ponsc6742232020-11-15 13:26:21 +01002906 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002907
Angel Pons7c49cb82020-03-16 23:17:32 +01002908 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7a612742020-11-12 13:34:03 +01002909 const union tc_rap_reg tc_rap = {
2910 .tRRD = ctrl->tRRD,
2911 .tRTP = ctrl->tRTP,
2912 .tCKE = ctrl->tCKE,
2913 .tWTR = ctrl->tWTR,
2914 .tFAW = ctrl->tFAW,
2915 .tWR = ctrl->tWR,
2916 .tCMD = ctrl->cmd_stretch[channel],
2917 };
Angel Pons66780a02021-03-26 13:33:22 +01002918 mchbar_write32(TC_RAP_ch(channel), tc_rap.raw);
Angel Pons7c49cb82020-03-16 23:17:32 +01002919 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920
2921 udelay(1);
2922
2923 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002924 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002925 }
2926
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002927 FOR_ALL_POPULATED_CHANNELS
Angel Pons66780a02021-03-26 13:33:22 +01002928 mchbar_setbits32(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002929
2930 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002931 udelay(1);
Angel Pons66780a02021-03-26 13:33:22 +01002932 mchbar_setbits32(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002933 }
2934
2935 printram("CPE\n");
2936
Angel Pons66780a02021-03-26 13:33:22 +01002937 mchbar_write32(GDCRTRAININGMOD, 0);
2938 mchbar_write32(IOSAV_DC_MASK, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002939
2940 printram("CP5b\n");
2941
2942 FOR_ALL_POPULATED_CHANNELS {
2943 program_timings(ctrl, channel);
2944 }
2945
2946 u32 reg, addr;
2947
Angel Pons7c49cb82020-03-16 23:17:32 +01002948 /* Poll for RCOMP */
Angel Pons66780a02021-03-26 13:33:22 +01002949 while (!(mchbar_read32(RCOMP_TIMER) & (1 << 16)))
Angel Pons7c49cb82020-03-16 23:17:32 +01002950 ;
2951
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002952 do {
Angel Pons66780a02021-03-26 13:33:22 +01002953 reg = mchbar_read32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002954 } while ((reg & 0x14) == 0);
2955
Angel Pons7c49cb82020-03-16 23:17:32 +01002956 /* Set state of memory controller */
Angel Pons66780a02021-03-26 13:33:22 +01002957 mchbar_write32(MC_INIT_STATE_G, 0x116);
2958 mchbar_write32(MC_INIT_STATE, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002959
Angel Pons7c49cb82020-03-16 23:17:32 +01002960 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002961 udelay(500);
2962
2963 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002964 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002965 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002966 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01002967 addr = MC_INIT_STATE_ch(channel);
Angel Pons66780a02021-03-26 13:33:22 +01002968 mchbar_write32(addr, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002969
Angel Pons7c49cb82020-03-16 23:17:32 +01002970 /* Wait 10ns for ranks to settle */
2971 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972
2973 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons66780a02021-03-26 13:33:22 +01002974 mchbar_write32(addr, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002975
Angel Pons7c49cb82020-03-16 23:17:32 +01002976 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002977 write_reset(ctrl);
2978 }
2979
Angel Pons7c49cb82020-03-16 23:17:32 +01002980 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002981 dram_mrscommands(ctrl);
2982
2983 printram("CP5c\n");
2984
Angel Pons66780a02021-03-26 13:33:22 +01002985 mchbar_write32(GDCRTRAININGMOD_ch(0), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002986
2987 FOR_ALL_CHANNELS {
Angel Pons66780a02021-03-26 13:33:22 +01002988 mchbar_clrbits32(GDCRCMDDEBUGMUXCFG_Cz_S(channel), 0x3f << 24);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002989 udelay(2);
2990 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002991}