blob: 23eab153ede45f755203385f163d17ea6498dc7d [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
Angel Pons12bd8ab2020-11-13 23:10:52 +01003#include <assert.h>
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01004#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <console/console.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01006#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010010#include <northbridge/intel/sandybridge/chip.h>
11#include <device/pci_def.h>
12#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020013#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020014
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010015#include "raminit_native.h"
16#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010017#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#include "sandybridge.h"
19
Angel Pons7c49cb82020-03-16 23:17:32 +010020/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010021
22static void sfence(void)
23{
24 asm volatile ("sfence");
25}
26
Angel Pons7c49cb82020-03-16 23:17:32 +010027/* Toggle IO reset bit */
28static void toggle_io_reset(void)
29{
Angel Pons88521882020-01-05 20:21:20 +010030 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010031 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010033 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010034 udelay(1);
35}
36
37static u32 get_XOVER_CLK(u8 rankmap)
38{
39 return rankmap << 24;
40}
41
42static u32 get_XOVER_CMD(u8 rankmap)
43{
44 u32 reg;
45
Angel Pons7c49cb82020-03-16 23:17:32 +010046 /* Enable xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010047 reg = 0x4000;
48
Angel Pons7c49cb82020-03-16 23:17:32 +010049 /* Enable xover ctl */
50 if (rankmap & 0x03)
51 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010052
Angel Pons7c49cb82020-03-16 23:17:32 +010053 if (rankmap & 0x0c)
54 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010055
56 return reg;
57}
58
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010059void dram_find_common_params(ramctr_timing *ctrl)
60{
61 size_t valid_dimms;
62 int channel, slot;
63 dimm_info *dimms = &ctrl->info;
64
65 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
66 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010067
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010068 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010069
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010070 const dimm_attr *dimm = &dimms->dimm[channel][slot];
71 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
72 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010073
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010074 valid_dimms++;
75
76 /* Find all possible CAS combinations */
77 ctrl->cas_supported &= dimm->cas_supported;
78
79 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010080 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
81 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
82 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
84 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010085 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010086 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
87 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
88 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
89 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
90 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030091 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
92 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010093 }
94
95 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010096 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
97
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010098 if (!valid_dimms)
99 die("No valid DIMMs found");
100}
101
Angel Pons88521882020-01-05 20:21:20 +0100102void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100103{
104 u32 reg;
105 int channel;
106
107 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100108 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100110 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
111 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100112
Angel Pons7c49cb82020-03-16 23:17:32 +0100113 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100115 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
116 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100117 }
118}
119
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100120static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100121{
Angel Pons89ae6b82020-03-21 13:23:32 +0100122 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100123
124 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 /*
126 * ODT stretch:
127 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
128 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100129 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100130 if (stretch == 2)
131 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100132
Angel Pons88521882020-01-05 20:21:20 +0100133 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100134 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100136 } else {
Angel Pons88521882020-01-05 20:21:20 +0100137 addr = TC_OTHP_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100138 MCHBAR32_AND_OR(addr, ~(0xf << 16), (stretch << 16) | (stretch << 18));
Iru Cai89af71c2018-08-16 16:46:27 +0800139 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100140 }
141}
142
143void dram_timing_regs(ramctr_timing *ctrl)
144{
145 u32 reg, addr, val32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100146 int channel;
147
148 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100149 /* BIN parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100150 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100151 reg |= (ctrl->tRCD << 0);
152 reg |= (ctrl->tRP << 4);
153 reg |= (ctrl->CAS << 8);
154 reg |= (ctrl->CWL << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100155 reg |= (ctrl->tRAS << 16);
Angel Pons88521882020-01-05 20:21:20 +0100156 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), reg);
157 MCHBAR32(TC_DBP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100158
Angel Pons7c49cb82020-03-16 23:17:32 +0100159 /* Regular access parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100160 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100161 reg |= (ctrl->tRRD << 0);
162 reg |= (ctrl->tRTP << 4);
163 reg |= (ctrl->tCKE << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100164 reg |= (ctrl->tWTR << 12);
165 reg |= (ctrl->tFAW << 16);
Angel Pons7c49cb82020-03-16 23:17:32 +0100166 reg |= (ctrl->tWR << 24);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100167 reg |= (3 << 30);
Angel Pons88521882020-01-05 20:21:20 +0100168 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), reg);
169 MCHBAR32(TC_RAP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100170
Angel Pons7c49cb82020-03-16 23:17:32 +0100171 /* Other parameters */
Angel Pons88521882020-01-05 20:21:20 +0100172 addr = TC_OTHP_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100173 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100174 reg |= (ctrl->tXPDLL << 0);
175 reg |= (ctrl->tXP << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100176 reg |= (ctrl->tAONPD << 8);
177 reg |= 0xa0000;
178 printram("OTHP [%x] = %x\n", addr, reg);
179 MCHBAR32(addr) = reg;
180
Angel Ponsca2f68a2020-03-22 13:15:12 +0100181 /* Debug parameters - only applies to Ivy Bridge */
182 if (IS_IVY_CPU(ctrl->cpu)) {
183 reg = 0;
184
185 /*
186 * If tXP and tXPDLL are very high, we need to increase them by one.
187 * This can only happen on Ivy Bridge, and when overclocking the RAM.
188 */
189 if (ctrl->tXP >= 8)
190 reg |= (1 << 12);
191
192 if (ctrl->tXPDLL >= 32)
193 reg |= (1 << 13);
194
195 MCHBAR32(TC_DTP_ch(channel)) = reg;
196 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100197
Felix Held9fe248f2018-07-31 20:59:45 +0200198 MCHBAR32_OR(addr, 0x00020000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100199
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100200 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100201
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100202 /*
Angel Pons7c49cb82020-03-16 23:17:32 +0100203 * TC-Refresh timing parameters:
204 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
205 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100206 */
207 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
208
Angel Pons7c49cb82020-03-16 23:17:32 +0100209 reg = ((ctrl->tREFI & 0xffff) << 0) |
210 ((ctrl->tRFC & 0x01ff) << 16) | (((val32 / 1024) & 0x7f) << 25);
211
Angel Pons88521882020-01-05 20:21:20 +0100212 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), reg);
213 MCHBAR32(TC_RFTP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100214
Angel Ponsdc5539f2020-11-12 12:44:25 +0100215 MCHBAR32_OR(TC_RFP_ch(channel), 0xff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100216
Angel Pons7c49cb82020-03-16 23:17:32 +0100217 /* Self-refresh timing parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100218 reg = 0;
219 val32 = tDLLK;
Angel Pons7c49cb82020-03-16 23:17:32 +0100220 reg = (reg & ~0x00000fff) | (val32 << 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100221 val32 = ctrl->tXSOffset;
Angel Pons7c49cb82020-03-16 23:17:32 +0100222 reg = (reg & ~0x0000f000) | (val32 << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100223 val32 = tDLLK - ctrl->tXSOffset;
Angel Pons7c49cb82020-03-16 23:17:32 +0100224 reg = (reg & ~0x03ff0000) | (val32 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100225 val32 = ctrl->tMOD - 8;
Angel Pons7c49cb82020-03-16 23:17:32 +0100226 reg = (reg & ~0xf0000000) | (val32 << 28);
227 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), reg);
Angel Pons88521882020-01-05 20:21:20 +0100228 MCHBAR32(TC_SRFTP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100229 }
230}
231
232void dram_dimm_mapping(ramctr_timing *ctrl)
233{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100234 int channel;
235 dimm_info *info = &ctrl->info;
236
237 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200238 dimm_attr *dimmA, *dimmB;
239 u32 reg = 0;
240
Angel Pons7c49cb82020-03-16 23:17:32 +0100241 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100242 dimmA = &info->dimm[channel][0];
243 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100244 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100245 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100246 dimmA = &info->dimm[channel][1];
247 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100248 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100249 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100250
Nico Huberac4f2162017-10-01 18:14:43 +0200251 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100252 reg |= (dimmA->size_mb / 256) << 0;
253 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200254 reg |= (dimmA->width / 8 - 1) << 19;
255 }
256
257 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100258 reg |= (dimmB->size_mb / 256) << 8;
259 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200260 reg |= (dimmB->width / 8 - 1) << 20;
261 }
262
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200263 /*
264 * Rank interleave: Bit 16 of the physical address space sets
265 * the rank to use in a dual single rank DIMM configuration.
266 * That results in every 64KiB being interleaved between two ranks.
267 */
268 reg |= 1 << 21;
269 /* Enhanced interleave */
270 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200271
Angel Pons7c49cb82020-03-16 23:17:32 +0100272 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100273 ctrl->mad_dimm[channel] = reg;
274 } else {
275 ctrl->mad_dimm[channel] = 0;
276 }
277 }
278}
279
Patrick Rudolphdd662872017-10-28 18:20:11 +0200280void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100281{
282 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200283 u32 ecc;
284
285 if (ctrl->ecc_enabled)
286 ecc = training ? (1 << 24) : (3 << 24);
287 else
288 ecc = 0;
289
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100290 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200291 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100292 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200293
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200294 if (ctrl->ecc_enabled)
295 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100296}
297
Angel Pons88521882020-01-05 20:21:20 +0100298void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100299{
300 u32 reg, ch0size, ch1size;
301 u8 val;
302 reg = 0;
303 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100304
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100305 if (training) {
306 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
307 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
308 } else {
309 ch0size = ctrl->channel_size_mb[0];
310 ch1size = ctrl->channel_size_mb[1];
311 }
312
313 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100314 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100315 val = ch1size / 256;
316 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100317 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100318 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100319 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100320
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100321 } else {
Angel Pons88521882020-01-05 20:21:20 +0100322 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100323 val = ch0size / 256;
324 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100325 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100326 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100327 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100328 }
329}
330
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100331#define DEFAULT_PCI_MMIO_SIZE 2048
332
333static unsigned int get_mmio_size(void)
334{
335 const struct device *dev;
336 const struct northbridge_intel_sandybridge_config *cfg = NULL;
337
Angel Ponsb31d1d72020-01-10 01:35:09 +0100338 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100339 if (dev)
340 cfg = dev->chip_info;
341
342 /* If this is zero, it just means devicetree.cb didn't set it */
343 if (!cfg || cfg->pci_mmio_size == 0)
344 return DEFAULT_PCI_MMIO_SIZE;
345 else
346 return cfg->pci_mmio_size;
347}
348
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200349/*
350 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
351 * The ME/PCU/.. has the ability to change this.
352 * Return 0: ECC is optional
353 * Return 1: ECC is forced
354 */
355bool get_host_ecc_forced(void)
356{
357 /* read Capabilities A Register */
358 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
359 return !!(reg32 & (1 << 24));
360}
361
362/*
363 * Returns the ECC capability.
364 * The ME/PCU/.. has the ability to change this.
365 * Return 0: ECC is disabled
366 * Return 1: ECC is possible
367 */
368bool get_host_ecc_cap(void)
369{
370 /* read Capabilities A Register */
371 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
372 return !(reg32 & (1 << 25));
373}
374
Angel Pons88521882020-01-05 20:21:20 +0100375void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100376{
Angel Pons7c49cb82020-03-16 23:17:32 +0100377 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
378 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
379 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100380 uint16_t ggc;
381
382 mmiosize = get_mmio_size();
383
Felix Held87ddea22020-01-26 04:55:27 +0100384 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100385 if (!(ggc & 2)) {
386 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100387 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100388 } else {
389 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100390 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100391 }
392
393 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
394
395 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
396
397 mestolenbase = tom - me_uma_size;
398
Angel Pons7c49cb82020-03-16 23:17:32 +0100399 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
400
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100401 gfxstolenbase = toludbase - gfxstolen;
402 gttbase = gfxstolenbase - gttsize;
403
404 tsegbase = gttbase - tsegsize;
405
Angel Pons7c49cb82020-03-16 23:17:32 +0100406 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100407 tsegbasedelta = tsegbase & (tsegsize - 1);
408 tsegbase &= ~(tsegsize - 1);
409
410 gttbase -= tsegbasedelta;
411 gfxstolenbase -= tsegbasedelta;
412 toludbase -= tsegbasedelta;
413
Angel Pons7c49cb82020-03-16 23:17:32 +0100414 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100415 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100416 /* Reclaim is possible */
417 reclaim = 1;
418 remapbase = MAX(4096, tom - me_uma_size);
419 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
420 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100421 } else {
422 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100423 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100424 touudbase = tom - me_uma_size;
425 }
426
Angel Pons7c49cb82020-03-16 23:17:32 +0100427 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100428 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
429
Angel Pons7c49cb82020-03-16 23:17:32 +0100430 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100431 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100432 val = tom & 0xfff;
433 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100434 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100435 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100436
Angel Ponsb31d1d72020-01-10 01:35:09 +0100437 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100438 val = tom & 0xfffff000;
439 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100440 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442
Angel Pons7c49cb82020-03-16 23:17:32 +0100443 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100444 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100445 val = toludbase & 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", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100448 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100449
Angel Pons7c49cb82020-03-16 23:17:32 +0100450 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100451 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100452 val = touudbase & 0xfff;
453 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100454 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100455 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100456
Angel Pons7c49cb82020-03-16 23:17:32 +0100457 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100458 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100459 val = touudbase & 0xfffff000;
460 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100461 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100462 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100463
464 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100465 /* REMAP BASE */
466 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100467 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100468
Angel Pons7c49cb82020-03-16 23:17:32 +0100469 /* REMAP LIMIT */
470 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100471 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100472 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100473 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100474 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100475 val = tsegbase & 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", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100478 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100479
Angel Pons7c49cb82020-03-16 23:17:32 +0100480 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100481 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100482 val = gfxstolenbase & 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", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100485 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100486
Angel Pons7c49cb82020-03-16 23:17:32 +0100487 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100488 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100489 val = gttbase & 0xfff;
490 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100491 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100492 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100493
494 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100495 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100496 val = (0x80000 - me_uma_size) & 0xfffff000;
497 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100498 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100499 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100500
Angel Pons7c49cb82020-03-16 23:17:32 +0100501 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100502 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100503 val = mestolenbase & 0xfff;
504 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100505 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100506 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100507
Angel Ponsb31d1d72020-01-10 01:35:09 +0100508 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100509 val = mestolenbase & 0xfffff000;
510 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100511 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513
Angel Pons7c49cb82020-03-16 23:17:32 +0100514 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100515 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100516 val = (0x80000 - me_uma_size) & 0xfff;
517 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100518 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
519 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100520 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100521 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100522 }
523}
524
Angel Pons88521882020-01-05 20:21:20 +0100525static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526{
527 int channel, slotrank;
528
Angel Pons7c49cb82020-03-16 23:17:32 +0100529 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100530 channel = (ctrl->rankmap[0]) ? 0 : 1;
531
Angel Pons88521882020-01-05 20:21:20 +0100532 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100533
Angel Pons7c49cb82020-03-16 23:17:32 +0100534 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100535 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
536
Angel Ponsffd50152020-11-12 11:03:10 +0100537 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100538
Angel Pons7c49cb82020-03-16 23:17:32 +0100539 /*
540 * Execute command queue - why is bit 22 set here?!
541 *
542 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
543 */
Angel Pons38d901e2020-05-02 23:50:43 +0200544 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200545
Angel Pons88521882020-01-05 20:21:20 +0100546 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100547}
548
Angel Pons88521882020-01-05 20:21:20 +0100549void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100550{
Felix Held9fe248f2018-07-31 20:59:45 +0200551 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100552 int channel;
553
Angel Pons7c49cb82020-03-16 23:17:32 +0100554 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
555 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100556 do {
Angel Pons88521882020-01-05 20:21:20 +0100557 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100558 } while ((reg & 0x14) == 0);
559
Angel Pons7c49cb82020-03-16 23:17:32 +0100560 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100561 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100562 MCHBAR32(MC_INIT_STATE_G) = reg;
563 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100565 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566
Angel Pons7c49cb82020-03-16 23:17:32 +0100567 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100568 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 udelay(200);
572
Angel Pons7c49cb82020-03-16 23:17:32 +0100573 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100574 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100575
Angel Pons7c49cb82020-03-16 23:17:32 +0100576 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100577 udelay(500);
578
Angel Pons7c49cb82020-03-16 23:17:32 +0100579 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100580 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581
Angel Pons7c49cb82020-03-16 23:17:32 +0100582 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100583 udelay(1);
584
585 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200587 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100588 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100589
Angel Pons7c49cb82020-03-16 23:17:32 +0100590 /* Wait 10ns for ranks to settle */
591 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100592
593 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100594 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100595
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100597 write_reset(ctrl);
598 }
599}
600
Angel Pons3d3bf482020-11-14 16:18:15 +0100601/*
602 * DDR3 Rank1 Address mirror swap the following pins:
603 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
604 */
605static void ddr3_mirror_mrreg(int *bank, u32 *addr)
606{
607 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
608 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
609}
610
Angel Pons7c49cb82020-03-16 23:17:32 +0100611static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100612{
Angel Pons88521882020-01-05 20:21:20 +0100613 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100614
Angel Pons3d3bf482020-11-14 16:18:15 +0100615 if (ctrl->rank_mirror[channel][slotrank])
616 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100617
Angel Pons8f0757e2020-11-11 23:03:36 +0100618 const struct iosav_ssq sequence[] = {
619 /* DRAM command MRS */
620 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200621 .sp_cmd_ctrl = {
622 .command = IOSAV_MRS,
623 },
624 .subseq_ctrl = {
625 .cmd_executions = 1,
626 .cmd_delay_gap = 4,
627 .post_ssq_wait = 4,
628 .data_direction = SSQ_NA,
629 },
630 .sp_cmd_addr = {
631 .address = val,
632 .rowbits = 6,
633 .bank = reg,
634 .rank = slotrank,
635 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100636 },
637 /* DRAM command MRS */
638 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200639 .sp_cmd_ctrl = {
640 .command = IOSAV_MRS,
641 .ranksel_ap = 1,
642 },
643 .subseq_ctrl = {
644 .cmd_executions = 1,
645 .cmd_delay_gap = 4,
646 .post_ssq_wait = 4,
647 .data_direction = SSQ_NA,
648 },
649 .sp_cmd_addr = {
650 .address = val,
651 .rowbits = 6,
652 .bank = reg,
653 .rank = slotrank,
654 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100655 },
656 /* DRAM command MRS */
657 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200658 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100659 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200660 },
661 .subseq_ctrl = {
662 .cmd_executions = 1,
663 .cmd_delay_gap = 4,
664 .post_ssq_wait = ctrl->tMOD,
665 .data_direction = SSQ_NA,
666 },
667 .sp_cmd_addr = {
668 .address = val,
669 .rowbits = 6,
670 .bank = reg,
671 .rank = slotrank,
672 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100673 },
674 };
675 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200676
Angel Pons7c49cb82020-03-16 23:17:32 +0100677 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200678 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100679}
680
Angel Pons88521882020-01-05 20:21:20 +0100681static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100682{
683 u16 mr0reg, mch_cas, mch_wr;
684 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Patrick Rudolph74203de2017-11-20 11:57:01 +0100685 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100686
Angel Pons7c49cb82020-03-16 23:17:32 +0100687 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100688 if (ctrl->CAS < 12) {
689 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
690 } else {
691 mch_cas = (u16) (ctrl->CAS - 12);
692 mch_cas = ((mch_cas << 1) | 0x1);
693 }
694
Angel Pons7c49cb82020-03-16 23:17:32 +0100695 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100696 mch_wr = mch_wr_t[ctrl->tWR - 5];
697
Angel Pons2bf28ed2020-11-12 13:49:59 +0100698 /* DLL Reset - self clearing - set after CLK frequency has been changed */
699 mr0reg = 1 << 8;
700
701 mr0reg |= (mch_cas & 0x1) << 2;
702 mr0reg |= (mch_cas & 0xe) << 3;
703 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100704
Angel Pons7c49cb82020-03-16 23:17:32 +0100705 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
Angel Pons2bf28ed2020-11-12 13:49:59 +0100706 mr0reg |= !is_mobile << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100707 return mr0reg;
708}
709
710static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
711{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200712 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100713}
714
Angel Ponsf9997482020-11-12 16:02:52 +0100715static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100716{
717 /* Get ODT based on rankmap */
718 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
719
720 if (dimms_per_ch == 1) {
721 return (const odtmap){60, 60};
722 } else {
723 return (const odtmap){120, 30};
724 }
725}
726
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100727static u32 encode_odt(u32 odt)
728{
729 switch (odt) {
730 case 30:
731 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
732 case 60:
733 return (1 << 2); // RZQ/4
734 case 120:
735 return (1 << 6); // RZQ/2
736 default:
737 case 0:
738 return 0;
739 }
740}
741
742static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
743{
744 odtmap odt;
745 u32 mr1reg;
746
Angel Ponsf9997482020-11-12 16:02:52 +0100747 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100748 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100749
750 mr1reg |= encode_odt(odt.rttnom);
751
752 return mr1reg;
753}
754
755static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
756{
757 u16 mr1reg;
758
759 mr1reg = make_mr1(ctrl, rank, channel);
760
761 write_mrreg(ctrl, channel, rank, 1, mr1reg);
762}
763
764static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
765{
Angel Pons868bca22020-11-13 13:38:04 +0100766 const u16 pasr = 0;
767 const u16 cwl = ctrl->CWL - 5;
768 const odtmap odt = get_ODT(ctrl, channel);
769
Angel Ponsdca3cb52020-11-13 13:42:07 +0100770 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100771 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
772 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100773
Angel Pons868bca22020-11-13 13:38:04 +0100774 u16 mr2reg = 0;
775 mr2reg |= pasr;
776 mr2reg |= cwl << 3;
777 mr2reg |= ctrl->auto_self_refresh << 6;
778 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100779 mr2reg |= (odt.rttwr / 60) << 9;
780
781 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100782
783 /* Program MR2 shadow */
784 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
785
786 reg32 &= 3 << 14 | 3 << 6;
787
788 reg32 |= mr2reg & ~(3 << 6);
789
790 if (rank & 1) {
791 if (srt)
792 reg32 |= 1 << (rank / 2 + 6);
793 } else {
794 if (ctrl->rank_mirror[channel][rank])
795 reg32 |= 1 << (rank / 2 + 14);
796 }
797 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100798}
799
800static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
801{
802 write_mrreg(ctrl, channel, rank, 3, 0);
803}
804
Angel Pons88521882020-01-05 20:21:20 +0100805void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100806{
807 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100808 int channel;
809
810 FOR_ALL_POPULATED_CHANNELS {
811 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100812 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100813 dram_mr2(ctrl, slotrank, channel);
814
Angel Pons7c49cb82020-03-16 23:17:32 +0100815 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100816 dram_mr3(ctrl, slotrank, channel);
817
Angel Pons7c49cb82020-03-16 23:17:32 +0100818 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100819 dram_mr1(ctrl, slotrank, channel);
820
Angel Pons7c49cb82020-03-16 23:17:32 +0100821 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100822 dram_mr0(ctrl, slotrank, channel);
823 }
824 }
825
Angel Pons8f0757e2020-11-11 23:03:36 +0100826 const struct iosav_ssq zqcl_sequence[] = {
827 /* DRAM command NOP (without ODT nor chip selects) */
828 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200829 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100830 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200831 },
832 .subseq_ctrl = {
833 .cmd_executions = 1,
834 .cmd_delay_gap = 4,
835 .post_ssq_wait = 15,
836 .data_direction = SSQ_NA,
837 },
838 .sp_cmd_addr = {
839 .address = 2,
840 .rowbits = 6,
841 .bank = 0,
842 .rank = 0,
843 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100844 },
845 /* DRAM command ZQCL */
846 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200847 .sp_cmd_ctrl = {
848 .command = IOSAV_ZQCS,
849 .ranksel_ap = 1,
850 },
851 .subseq_ctrl = {
852 .cmd_executions = 1,
853 .cmd_delay_gap = 4,
854 .post_ssq_wait = 400,
855 .data_direction = SSQ_NA,
856 },
857 .sp_cmd_addr = {
858 .address = 1024,
859 .rowbits = 6,
860 .bank = 0,
861 .rank = 0,
862 },
863 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100864 .inc_rank = 1,
865 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200866 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100867 },
868 };
869 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100870
Angel Pons7c49cb82020-03-16 23:17:32 +0100871 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200872 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100873
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100874 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100875 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100876 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100877 }
878
Angel Pons7c49cb82020-03-16 23:17:32 +0100879 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100880 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100881
882 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100883 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100884
Angel Pons88521882020-01-05 20:21:20 +0100885 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100886
887 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
888
Angel Pons7c49cb82020-03-16 23:17:32 +0100889 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100890 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100891
Angel Ponsffd50152020-11-12 11:03:10 +0100892 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200893
Angel Pons7c49cb82020-03-16 23:17:32 +0100894 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200895 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100896
Angel Pons7c49cb82020-03-16 23:17:32 +0100897 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100898 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100899 }
900}
901
Felix Held3b906032020-01-14 17:05:43 +0100902static const u32 lane_base[] = {
903 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
904 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
905 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100906};
907
Angel Pons88521882020-01-05 20:21:20 +0100908void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100909{
Angel Pons88521882020-01-05 20:21:20 +0100910 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100911 int lane;
912 int slotrank, slot;
913 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100914 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100915
916 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100917 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
918 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100919 }
920
921 for (slot = 0; slot < NUM_SLOTS; slot++)
922 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
923 case 0:
924 default:
Angel Pons88521882020-01-05 20:21:20 +0100925 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100926 break;
927 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100928 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100929 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100930 break;
931 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100932 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100933 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100934 break;
935 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100936 pi_coding_ctrl[slot] =
937 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100938 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100939 break;
940 }
941
Angel Pons7c49cb82020-03-16 23:17:32 +0100942 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100943 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100944 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
945 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100946 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100947 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
948
Angel Pons88521882020-01-05 20:21:20 +0100949 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100950
Angel Pons7c49cb82020-03-16 23:17:32 +0100951 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100952 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
953 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100954
955 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100956 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100957 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100958 if (shift < 0)
959 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100960
Angel Pons88521882020-01-05 20:21:20 +0100961 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100962
963 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100964 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
965 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100966 }
967
Angel Pons88521882020-01-05 20:21:20 +0100968 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
969 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100970
Angel Pons88521882020-01-05 20:21:20 +0100971 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100972 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100973
Angel Pons88521882020-01-05 20:21:20 +0100974 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100975
976 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100977 int post_timA_min_high = 7, pre_timA_min_high = 7;
978 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100979 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100980 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100981
982 if (shift < 0)
983 shift = 0;
984
985 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200986 post_timA_min_high = MIN(post_timA_min_high,
987 (ctrl->timings[channel][slotrank].lanes[lane].
988 timA + shift) >> 6);
989 pre_timA_min_high = MIN(pre_timA_min_high,
990 ctrl->timings[channel][slotrank].lanes[lane].
991 timA >> 6);
992 post_timA_max_high = MAX(post_timA_max_high,
993 (ctrl->timings[channel][slotrank].lanes[lane].
994 timA + shift) >> 6);
995 pre_timA_max_high = MAX(pre_timA_max_high,
996 ctrl->timings[channel][slotrank].lanes[lane].
997 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100998 }
999
1000 if (pre_timA_max_high - pre_timA_min_high <
1001 post_timA_max_high - post_timA_min_high)
1002 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001003
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001004 else if (pre_timA_max_high - pre_timA_min_high >
1005 post_timA_max_high - post_timA_min_high)
1006 shift_402x = -1;
1007
Felix Helddee167e2019-12-30 17:30:16 +01001008 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001009 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001010 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001011
Angel Pons88521882020-01-05 20:21:20 +01001012 reg_roundtrip_latency |=
1013 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001014 shift_402x) << (8 * slotrank);
1015
1016 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001017 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001018 (((ctrl->timings[channel][slotrank].lanes[lane].
1019 timA + shift) & 0x3f)
1020 |
1021 ((ctrl->timings[channel][slotrank].lanes[lane].
1022 rising + shift) << 8)
1023 |
1024 (((ctrl->timings[channel][slotrank].lanes[lane].
1025 timA + shift -
1026 (post_timA_min_high << 6)) & 0x1c0) << 10)
1027 | ((ctrl->timings[channel][slotrank].lanes[lane].
1028 falling + shift) << 20));
1029
Felix Heldfb19c8a2020-01-14 21:27:59 +01001030 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001031 (((ctrl->timings[channel][slotrank].lanes[lane].
1032 timC + shift) & 0x3f)
1033 |
1034 (((ctrl->timings[channel][slotrank].lanes[lane].
1035 timB + shift) & 0x3f) << 8)
1036 |
1037 (((ctrl->timings[channel][slotrank].lanes[lane].
1038 timB + shift) & 0x1c0) << 9)
1039 |
1040 (((ctrl->timings[channel][slotrank].lanes[lane].
1041 timC + shift) & 0x40) << 13));
1042 }
1043 }
Angel Pons88521882020-01-05 20:21:20 +01001044 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1045 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001046}
1047
Angel Pons88521882020-01-05 20:21:20 +01001048static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001049{
Angel Pons88521882020-01-05 20:21:20 +01001050 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001051
Angel Ponsffd50152020-11-12 11:03:10 +01001052 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053
Angel Pons7c49cb82020-03-16 23:17:32 +01001054 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001055 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001056
Angel Pons88521882020-01-05 20:21:20 +01001057 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001058}
1059
Angel Pons7c49cb82020-03-16 23:17:32 +01001060static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001061{
1062 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001063
1064 return (MCHBAR32(lane_base[lane] +
1065 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001066}
1067
1068struct run {
1069 int middle;
1070 int end;
1071 int start;
1072 int all;
1073 int length;
1074};
1075
1076static struct run get_longest_zero_run(int *seq, int sz)
1077{
1078 int i, ls;
1079 int bl = 0, bs = 0;
1080 struct run ret;
1081
1082 ls = 0;
1083 for (i = 0; i < 2 * sz; i++)
1084 if (seq[i % sz]) {
1085 if (i - ls > bl) {
1086 bl = i - ls;
1087 bs = ls;
1088 }
1089 ls = i + 1;
1090 }
1091 if (bl == 0) {
1092 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001093 ret.start = 0;
1094 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001095 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001096 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001097 return ret;
1098 }
1099
Angel Pons7c49cb82020-03-16 23:17:32 +01001100 ret.start = bs % sz;
1101 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001102 ret.middle = (bs + (bl - 1) / 2) % sz;
1103 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001104 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001105
1106 return ret;
1107}
1108
Angel Ponsf3053392020-11-13 23:31:12 +01001109static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001110{
1111 int timA;
1112 int statistics[NUM_LANES][128];
1113 int lane;
1114
1115 for (timA = 0; timA < 128; timA++) {
1116 FOR_ALL_LANES {
1117 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1118 }
1119 program_timings(ctrl, channel);
1120
1121 test_timA(ctrl, channel, slotrank);
1122
1123 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001124 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001125 }
1126 }
1127 FOR_ALL_LANES {
1128 struct run rn = get_longest_zero_run(statistics[lane], 128);
1129 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1130 upperA[lane] = rn.end;
1131 if (upperA[lane] < rn.middle)
1132 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001133
Patrick Rudolph368b6152016-11-25 16:36:52 +01001134 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001135 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001136 }
1137}
1138
Angel Ponsf3053392020-11-13 23:31:12 +01001139static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001140{
1141 int timA_delta;
1142 int statistics[NUM_LANES][51];
1143 int lane, i;
1144
1145 memset(statistics, 0, sizeof(statistics));
1146
1147 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001148
1149 FOR_ALL_LANES {
1150 ctrl->timings[channel][slotrank].lanes[lane].timA
1151 = upperA[lane] + timA_delta + 0x40;
1152 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001153 program_timings(ctrl, channel);
1154
1155 for (i = 0; i < 100; i++) {
1156 test_timA(ctrl, channel, slotrank);
1157 FOR_ALL_LANES {
1158 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001159 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001160 }
1161 }
1162 }
1163 FOR_ALL_LANES {
1164 int last_zero, first_all;
1165
1166 for (last_zero = -25; last_zero <= 25; last_zero++)
1167 if (statistics[lane][last_zero + 25])
1168 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001169
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001170 last_zero--;
1171 for (first_all = -25; first_all <= 25; first_all++)
1172 if (statistics[lane][first_all + 25] == 100)
1173 break;
1174
Angel Pons7c49cb82020-03-16 23:17:32 +01001175 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001176
1177 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001178 (last_zero + first_all) / 2 + upperA[lane];
1179
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001180 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001181 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001182 }
1183}
1184
Angel Ponsf3053392020-11-13 23:31:12 +01001185static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001186{
1187 int works[NUM_LANES];
1188 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190 while (1) {
1191 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001192
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 program_timings(ctrl, channel);
1194 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001195
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001196 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001197 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1198
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001199 if (works[lane])
1200 some_works = 1;
1201 else
1202 all_works = 0;
1203 }
1204 if (all_works)
1205 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001206
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001207 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001208 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001209 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1210 channel, slotrank);
1211 return MAKE_ERR;
1212 }
Angel Pons88521882020-01-05 20:21:20 +01001213 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 printram("4024 -= 2;\n");
1215 continue;
1216 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001217 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001218 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001219
Felix Heldef4fe3e2019-12-31 14:15:05 +01001220 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001221 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1222 channel, slotrank);
1223 return MAKE_ERR;
1224 }
1225 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001226 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001227 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001228 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 }
1230 }
1231 return 0;
1232}
1233
Angel Pons12bd8ab2020-11-13 23:10:52 +01001234static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001235{
1236 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001237 u16 logic_delay_min = 7;
1238 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001239
1240 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001241 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1242
1243 logic_delay_min = MIN(logic_delay_min, logic_delay);
1244 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001245 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001246
1247 if (logic_delay_max < logic_delay_min) {
1248 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1249 logic_delay_max, logic_delay_min, channel, slotrank);
1250 }
1251
1252 assert(logic_delay_max >= logic_delay_min);
1253
1254 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001255}
1256
Angel Pons12bd8ab2020-11-13 23:10:52 +01001257static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001258{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001259 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001260
Angel Pons7c49cb82020-03-16 23:17:32 +01001261 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001262 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001263
Angel Pons12bd8ab2020-11-13 23:10:52 +01001264 if (prev < post)
1265 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001266
Angel Pons12bd8ab2020-11-13 23:10:52 +01001267 else if (prev > post)
1268 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001269
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001271 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001272
Angel Pons12bd8ab2020-11-13 23:10:52 +01001273 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1274 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1275 printram("4024 += %d;\n", latency_offset);
1276 printram("4028 += %d;\n", latency_offset);
1277
1278 return post;
1279}
1280
1281static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1282{
1283 u16 logic_delay_min = 7;
1284 int lane;
1285
1286 FOR_ALL_LANES {
1287 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1288
1289 logic_delay_min = MIN(logic_delay_min, logic_delay);
1290 }
1291
1292 if (logic_delay_min >= 2) {
1293 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1294 logic_delay_min, channel, slotrank);
1295 }
1296
1297 FOR_ALL_LANES {
1298 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1299 }
1300 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1301 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001302}
1303
Angel Pons7c49cb82020-03-16 23:17:32 +01001304/*
1305 * Compensate the skew between DQS and DQs.
1306 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001307 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1308 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001309 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001310 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001311 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001312 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1313 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1314 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001315 * DQ in phase the data being read is expected to alternate on every byte:
1316 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001317 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001318 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001319 * Once the controller has detected this pattern a bit in the result register is set for the
1320 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001321 */
Angel Pons88521882020-01-05 20:21:20 +01001322int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001323{
1324 int channel, slotrank, lane;
1325 int err;
1326
1327 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1328 int all_high, some_high;
1329 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001330 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001331
Angel Pons88521882020-01-05 20:21:20 +01001332 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001333
Angel Ponsffd50152020-11-12 11:03:10 +01001334 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001335
Angel Pons7c49cb82020-03-16 23:17:32 +01001336 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001337 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001338
Angel Pons88521882020-01-05 20:21:20 +01001339 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001340
Felix Heldef4fe3e2019-12-31 14:15:05 +01001341 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001342 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001343 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001344
Angel Ponsf3053392020-11-13 23:31:12 +01001345 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001346
Felix Held2bb3cdf2018-07-28 00:23:59 +02001347 all_high = 1;
1348 some_high = 0;
1349 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001350 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001351 some_high = 1;
1352 else
1353 all_high = 0;
1354 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001355
1356 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001357 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001358 printram("4028--;\n");
1359 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001360 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001361 upperA[lane] -= 0x40;
1362
1363 }
1364 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001365 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001366 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001367 printram("4024++;\n");
1368 printram("4028++;\n");
1369 }
1370
1371 program_timings(ctrl, channel);
1372
Angel Pons12bd8ab2020-11-13 23:10:52 +01001373 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001374
Angel Ponsf3053392020-11-13 23:31:12 +01001375 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001376 if (err)
1377 return err;
1378
Angel Pons12bd8ab2020-11-13 23:10:52 +01001379 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380
Angel Ponsf3053392020-11-13 23:31:12 +01001381 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
Angel Pons12bd8ab2020-11-13 23:10:52 +01001383 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Pons12bd8ab2020-11-13 23:10:52 +01001385 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386
Angel Pons12bd8ab2020-11-13 23:10:52 +01001387 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388
1389 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001390 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001391 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001392
1393 printram("final results:\n");
1394 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001395 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001396 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001397
Angel Pons88521882020-01-05 20:21:20 +01001398 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001399
1400 toggle_io_reset();
1401 }
1402
1403 FOR_ALL_POPULATED_CHANNELS {
1404 program_timings(ctrl, channel);
1405 }
1406 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001407 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001408 }
1409 return 0;
1410}
1411
Angel Pons011661c2020-11-15 18:21:35 +01001412static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001413{
1414 int lane;
1415
1416 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001417 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1418 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001419 }
1420
Angel Pons88521882020-01-05 20:21:20 +01001421 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001422
Angel Ponsffd50152020-11-12 11:03:10 +01001423 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1424 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001425
Angel Pons7c49cb82020-03-16 23:17:32 +01001426 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001427 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001428
Angel Pons88521882020-01-05 20:21:20 +01001429 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001430
Angel Pons8f0757e2020-11-11 23:03:36 +01001431 const struct iosav_ssq rd_sequence[] = {
1432 /* DRAM command PREA */
1433 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001434 .sp_cmd_ctrl = {
1435 .command = IOSAV_PRE,
1436 .ranksel_ap = 1,
1437 },
1438 .subseq_ctrl = {
1439 .cmd_executions = 1,
1440 .cmd_delay_gap = 3,
1441 .post_ssq_wait = ctrl->tRP,
1442 .data_direction = SSQ_NA,
1443 },
1444 .sp_cmd_addr = {
1445 .address = 1024,
1446 .rowbits = 6,
1447 .bank = 0,
1448 .rank = slotrank,
1449 },
1450 .addr_update = {
1451 .addr_wrap = 18,
1452 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001453 },
1454 /* DRAM command ACT */
1455 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001456 .sp_cmd_ctrl = {
1457 .command = IOSAV_ACT,
1458 .ranksel_ap = 1,
1459 },
1460 .subseq_ctrl = {
1461 .cmd_executions = 8,
1462 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1463 .post_ssq_wait = ctrl->CAS,
1464 .data_direction = SSQ_NA,
1465 },
1466 .sp_cmd_addr = {
1467 .address = 0,
1468 .rowbits = 6,
1469 .bank = 0,
1470 .rank = slotrank,
1471 },
1472 .addr_update = {
1473 .inc_bank = 1,
1474 .addr_wrap = 18,
1475 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001476 },
1477 /* DRAM command RD */
1478 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001479 .sp_cmd_ctrl = {
1480 .command = IOSAV_RD,
1481 .ranksel_ap = 1,
1482 },
1483 .subseq_ctrl = {
1484 .cmd_executions = 500,
1485 .cmd_delay_gap = 4,
1486 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1487 .data_direction = SSQ_RD,
1488 },
1489 .sp_cmd_addr = {
1490 .address = 0,
1491 .rowbits = 0,
1492 .bank = 0,
1493 .rank = slotrank,
1494 },
1495 .addr_update = {
1496 .inc_addr_8 = 1,
1497 .addr_wrap = 18,
1498 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001499 },
1500 /* DRAM command PREA */
1501 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001502 .sp_cmd_ctrl = {
1503 .command = IOSAV_PRE,
1504 .ranksel_ap = 1,
1505 },
1506 .subseq_ctrl = {
1507 .cmd_executions = 1,
1508 .cmd_delay_gap = 3,
1509 .post_ssq_wait = ctrl->tRP,
1510 .data_direction = SSQ_NA,
1511 },
1512 .sp_cmd_addr = {
1513 .address = 1024,
1514 .rowbits = 6,
1515 .bank = 0,
1516 .rank = slotrank,
1517 },
1518 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01001519 .addr_wrap = 18,
Angel Pons3abd2062020-05-03 00:25:02 +02001520 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001521 },
1522 };
1523 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001524
Angel Pons7c49cb82020-03-16 23:17:32 +01001525 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001526 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001527
Angel Pons88521882020-01-05 20:21:20 +01001528 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001529}
1530
Angel Pons011661c2020-11-15 18:21:35 +01001531static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001532{
1533 int min = data[0];
1534 int max = min;
1535 int i;
1536 for (i = 1; i < count; i++) {
1537 if (min > data[i])
1538 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001539
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001540 if (max < data[i])
1541 max = data[i];
1542 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001543 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001544 for (i = 0; i < count; i++)
1545 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001546
Angel Pons891f2bc2020-01-10 01:27:28 +01001547 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001548}
1549
Angel Pons011661c2020-11-15 18:21:35 +01001550static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001551{
Angel Pons011661c2020-11-15 18:21:35 +01001552 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001553 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001554 int lane;
1555
Angel Pons88521882020-01-05 20:21:20 +01001556 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557
Angel Ponsffd50152020-11-12 11:03:10 +01001558 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001559
Angel Pons7c49cb82020-03-16 23:17:32 +01001560 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001561 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001562
Angel Pons011661c2020-11-15 18:21:35 +01001563 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1564 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001565 program_timings(ctrl, channel);
1566
Angel Pons011661c2020-11-15 18:21:35 +01001567 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001568
1569 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001570 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001571 }
1572 }
1573 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001574 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1575
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001576 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001577 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1578 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001579 /*
1580 * With command training not being done yet, the lane can be erroneous.
1581 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001582 */
Angel Pons011661c2020-11-15 18:21:35 +01001583 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001584 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1585
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001586 if (rn.all || rn.length < 8) {
1587 printk(BIOS_EMERG, "timC recovery failed\n");
1588 return MAKE_ERR;
1589 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001590 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001591 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001592 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001593 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001594 }
1595 return 0;
1596}
1597
Angel Pons88521882020-01-05 20:21:20 +01001598static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001599{
1600 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001601
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001602 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1603 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001604
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001605 return ret;
1606}
1607
Angel Pons765d4652020-11-11 14:44:35 +01001608/* Each cacheline is 64 bits long */
1609static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1610{
1611 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1612}
1613
Angel Pons88521882020-01-05 20:21:20 +01001614static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001615{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301616 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001617 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001618
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 for (j = 0; j < 16; j++)
1620 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001621
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001622 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001623
1624 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001625}
1626
Angel Pons88521882020-01-05 20:21:20 +01001627static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001628{
1629 int ret = 0;
1630 int channel;
1631 FOR_ALL_POPULATED_CHANNELS ret++;
1632 return ret;
1633}
1634
Angel Pons88521882020-01-05 20:21:20 +01001635static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001636{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301637 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001638 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301639 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001640
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001641 for (j = 0; j < 16; j++)
1642 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001643
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001644 for (j = 0; j < 16; j++)
1645 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001646
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001647 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001648
1649 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001650}
1651
Angel Pons88521882020-01-05 20:21:20 +01001652static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001653{
1654 int channel, slotrank, lane;
1655
1656 FOR_ALL_POPULATED_CHANNELS {
1657 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001658 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1659 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001660 }
1661
1662 program_timings(ctrl, channel);
1663
1664 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001665 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001666
Angel Ponsffd50152020-11-12 11:03:10 +01001667 iosav_write_read_mpr_sequence(
1668 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001669
Angel Pons7c49cb82020-03-16 23:17:32 +01001670 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001671 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001672
Angel Pons88521882020-01-05 20:21:20 +01001673 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001674 }
1675
1676 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001677 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1678 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001679 }
1680
1681 program_timings(ctrl, channel);
1682
1683 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001684 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001685
Angel Ponsffd50152020-11-12 11:03:10 +01001686 iosav_write_read_mpr_sequence(
1687 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001688
Angel Pons7c49cb82020-03-16 23:17:32 +01001689 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001690 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001691
Angel Pons88521882020-01-05 20:21:20 +01001692 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001693 }
1694 }
1695}
1696
Angel Pons88521882020-01-05 20:21:20 +01001697static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001698{
1699 /* enable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001700 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel) | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001701
Angel Pons88521882020-01-05 20:21:20 +01001702 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001703
1704 const struct iosav_ssq sequence[] = {
1705 /* DRAM command NOP */
1706 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001707 .sp_cmd_ctrl = {
1708 .command = IOSAV_NOP,
1709 .ranksel_ap = 1,
1710 },
1711 .subseq_ctrl = {
1712 .cmd_executions = 1,
1713 .cmd_delay_gap = 3,
1714 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1715 .data_direction = SSQ_WR,
1716 },
1717 .sp_cmd_addr = {
1718 .address = 8,
1719 .rowbits = 0,
1720 .bank = 0,
1721 .rank = slotrank,
1722 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001723 },
1724 /* DRAM command NOP */
1725 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001726 .sp_cmd_ctrl = {
1727 .command = IOSAV_NOP_ALT,
1728 .ranksel_ap = 1,
1729 },
1730 .subseq_ctrl = {
1731 .cmd_executions = 1,
1732 .cmd_delay_gap = 3,
1733 .post_ssq_wait = ctrl->CAS + 38,
1734 .data_direction = SSQ_RD,
1735 },
1736 .sp_cmd_addr = {
1737 .address = 4,
1738 .rowbits = 0,
1739 .bank = 0,
1740 .rank = slotrank,
1741 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001742 },
1743 };
1744 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001745
Angel Pons7c49cb82020-03-16 23:17:32 +01001746 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001747 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001748
Angel Pons88521882020-01-05 20:21:20 +01001749 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001750
1751 /* disable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001752 write_mrreg(ctrl, channel, slotrank, 1,
1753 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001754}
1755
1756static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1757{
1758 int timB;
1759 int statistics[NUM_LANES][128];
1760 int lane;
1761
Angel Pons88521882020-01-05 20:21:20 +01001762 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001763
1764 for (timB = 0; timB < 128; timB++) {
1765 FOR_ALL_LANES {
1766 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1767 }
1768 program_timings(ctrl, channel);
1769
1770 test_timB(ctrl, channel, slotrank);
1771
1772 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001773 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1774 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1775 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001776 }
1777 }
1778 FOR_ALL_LANES {
1779 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001780 /*
1781 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1782 * of timB by a small value, which might cause the 6-bit value to overflow if
1783 * it's close to 0x3f. Increment the value by a small offset if it's likely
1784 * to overflow, to make sure it won't overflow while running tests and bricks
1785 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001787 * TODO: find out why some tests (edge write discovery) increment timB.
1788 */
1789 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001790 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001791 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001792 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001793
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001794 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1795 if (rn.all) {
1796 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1797 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001798
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001799 return MAKE_ERR;
1800 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001801 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1802 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001803 }
1804 return 0;
1805}
1806
1807static int get_timB_high_adjust(u64 val)
1808{
1809 int i;
1810
Angel Ponsbf13ef02020-11-11 18:40:06 +01001811 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001812 if (val == 0xffffffffffffffffLL)
1813 return 0;
1814
1815 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001816 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001817 for (i = 0; i < 8; i++)
1818 if (val << (8 * (7 - i) + 4))
1819 return -i;
1820 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001821 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001822 for (i = 0; i < 8; i++)
1823 if (val >> (8 * (7 - i) + 4))
1824 return i;
1825 }
1826 return 8;
1827}
1828
Angel Ponsbf13ef02020-11-11 18:40:06 +01001829static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001830{
1831 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001832 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001833 FOR_ALL_POPULATED_CHANNELS {
1834 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001835 }
1836 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1837
Angel Pons765d4652020-11-11 14:44:35 +01001838 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001839 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001840
Angel Pons88521882020-01-05 20:21:20 +01001841 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001842
Angel Ponsffd50152020-11-12 11:03:10 +01001843 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001844
Angel Pons7c49cb82020-03-16 23:17:32 +01001845 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001846 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001847
Angel Pons88521882020-01-05 20:21:20 +01001848 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001849
Angel Pons8f0757e2020-11-11 23:03:36 +01001850 const struct iosav_ssq rd_sequence[] = {
1851 /* DRAM command PREA */
1852 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001853 .sp_cmd_ctrl = {
1854 .command = IOSAV_PRE,
1855 .ranksel_ap = 1,
1856 },
1857 .subseq_ctrl = {
1858 .cmd_executions = 1,
1859 .cmd_delay_gap = 3,
1860 .post_ssq_wait = ctrl->tRP,
1861 .data_direction = SSQ_NA,
1862 },
1863 .sp_cmd_addr = {
1864 .address = 1024,
1865 .rowbits = 6,
1866 .bank = 0,
1867 .rank = slotrank,
1868 },
1869 .addr_update = {
1870 .addr_wrap = 18,
1871 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001872 },
1873 /* DRAM command ACT */
1874 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001875 .sp_cmd_ctrl = {
1876 .command = IOSAV_ACT,
1877 .ranksel_ap = 1,
1878 },
1879 .subseq_ctrl = {
1880 .cmd_executions = 1,
1881 .cmd_delay_gap = 3,
1882 .post_ssq_wait = ctrl->tRCD,
1883 .data_direction = SSQ_NA,
1884 },
1885 .sp_cmd_addr = {
1886 .address = 0,
1887 .rowbits = 6,
1888 .bank = 0,
1889 .rank = slotrank,
1890 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001891 },
1892 /* DRAM command RD */
1893 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001894 .sp_cmd_ctrl = {
1895 .command = IOSAV_RD,
1896 .ranksel_ap = 3,
1897 },
1898 .subseq_ctrl = {
1899 .cmd_executions = 1,
1900 .cmd_delay_gap = 3,
1901 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001902 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001903 ctrl->timings[channel][slotrank].io_latency,
1904 .data_direction = SSQ_RD,
1905 },
1906 .sp_cmd_addr = {
1907 .address = 8,
1908 .rowbits = 6,
1909 .bank = 0,
1910 .rank = slotrank,
1911 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001912 },
1913 };
1914 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001915
Angel Pons7c49cb82020-03-16 23:17:32 +01001916 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001917 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001918
Angel Pons88521882020-01-05 20:21:20 +01001919 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001920 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001921 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001922 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001923 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001924 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1925 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1926 get_timB_high_adjust(res) * 64;
1927
1928 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001929 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1930 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001931 }
1932 }
Angel Pons88521882020-01-05 20:21:20 +01001933 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001934}
1935
Angel Pons88521882020-01-05 20:21:20 +01001936static void write_op(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001937{
1938 int slotrank;
1939
Angel Pons88521882020-01-05 20:21:20 +01001940 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001941
1942 /* choose an existing rank. */
1943 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
1944
Angel Ponsffd50152020-11-12 11:03:10 +01001945 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001946
Angel Pons7c49cb82020-03-16 23:17:32 +01001947 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001948 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001949
Angel Pons88521882020-01-05 20:21:20 +01001950 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001951}
1952
Angel Pons7c49cb82020-03-16 23:17:32 +01001953/*
1954 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001955 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001956 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1957 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1958 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1959 * CLK/ADDR/CMD signals have the same routing delay.
1960 *
1961 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1962 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1963 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001964 */
Angel Pons88521882020-01-05 20:21:20 +01001965int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001966{
1967 int channel, slotrank, lane;
1968 int err;
1969
1970 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01001971 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972
1973 FOR_ALL_POPULATED_CHANNELS {
1974 write_op(ctrl, channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01001975 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001976 }
1977
Angel Pons7c49cb82020-03-16 23:17:32 +01001978 /* Refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001979 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001980 FOR_ALL_POPULATED_CHANNELS {
1981 write_op(ctrl, channel);
1982 }
1983
Angel Pons7c49cb82020-03-16 23:17:32 +01001984 /* Enable write leveling on all ranks
1985 Disable all DQ outputs
1986 Only NOP is allowed in this mode */
1987 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1988 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001989 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001990
Angel Pons88521882020-01-05 20:21:20 +01001991 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001992
1993 toggle_io_reset();
1994
Angel Pons7c49cb82020-03-16 23:17:32 +01001995 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001996 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1997 err = discover_timB(ctrl, channel, slotrank);
1998 if (err)
1999 return err;
2000 }
2001
Angel Pons7c49cb82020-03-16 23:17:32 +01002002 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002003 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01002004 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002005
Angel Pons88521882020-01-05 20:21:20 +01002006 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002007
2008 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002009 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002010
Angel Pons7c49cb82020-03-16 23:17:32 +01002011 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002012 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002013
2014 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002015 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01002016 MCHBAR32(IOSAV_STATUS_ch(channel));
2017 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002018
Angel Ponsffd50152020-11-12 11:03:10 +01002019 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002020
Angel Pons7c49cb82020-03-16 23:17:32 +01002021 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002022 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002023
Angel Pons88521882020-01-05 20:21:20 +01002024 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002025 }
2026
2027 toggle_io_reset();
2028
2029 printram("CPE\n");
2030 precharge(ctrl);
2031 printram("CPF\n");
2032
2033 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002034 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002035 }
2036
2037 FOR_ALL_POPULATED_CHANNELS {
2038 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002039 }
2040
2041 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01002042 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002043 if (err)
2044 return err;
2045 }
2046
2047 FOR_ALL_POPULATED_CHANNELS
2048 program_timings(ctrl, channel);
2049
2050 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002051 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002052
2053 FOR_ALL_POPULATED_CHANNELS
2054 program_timings(ctrl, channel);
2055
2056 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002057 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002058 }
2059 return 0;
2060}
2061
Angel Ponsbf13ef02020-11-11 18:40:06 +01002062static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002063{
2064 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2065 int timC_delta;
2066 int lanes_ok = 0;
2067 int ctr = 0;
2068 int lane;
2069
2070 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2071 FOR_ALL_LANES {
2072 ctrl->timings[channel][slotrank].lanes[lane].timC =
2073 saved_rt.lanes[lane].timC + timC_delta;
2074 }
2075 program_timings(ctrl, channel);
2076 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002077 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002078 }
2079
Angel Pons765d4652020-11-11 14:44:35 +01002080 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002081 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002082
Angel Pons88521882020-01-05 20:21:20 +01002083 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002084
Angel Ponsffd50152020-11-12 11:03:10 +01002085 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002086
2087 /* Program LFSR for the RD/WR subsequences */
2088 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2089 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002090
Angel Pons7c49cb82020-03-16 23:17:32 +01002091 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002092 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002093
Angel Pons88521882020-01-05 20:21:20 +01002094 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002095 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002096 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002097
2098 if (r32 == 0)
2099 lanes_ok |= 1 << lane;
2100 }
2101 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002102 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002103 break;
2104 }
2105
2106 ctrl->timings[channel][slotrank] = saved_rt;
2107
Patrick Rudolphdd662872017-10-28 18:20:11 +02002108 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002109}
2110
Angel Pons88521882020-01-05 20:21:20 +01002111static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002112{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302113 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002114 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2115 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002116
2117 if (patno) {
2118 u8 base8 = 0x80 >> ((patno - 1) % 8);
2119 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2120 for (i = 0; i < 32; i++) {
2121 for (j = 0; j < 16; j++) {
2122 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002123
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002124 if (invert[patno - 1][i] & (1 << (j / 2)))
2125 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002126
2127 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002128 }
2129 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002130 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002131 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2132 for (j = 0; j < 16; j++) {
2133 const u32 val = pattern[i][j];
2134 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2135 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002136 }
2137 sfence();
2138 }
Angel Pons765d4652020-11-11 14:44:35 +01002139
2140 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002141}
2142
Angel Pons88521882020-01-05 20:21:20 +01002143static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002144{
2145 int channel, slotrank;
2146
2147 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002148 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149
Angel Pons7c49cb82020-03-16 23:17:32 +01002150 /* Choose an existing rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002151 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2152
Angel Ponsffd50152020-11-12 11:03:10 +01002153 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002154
Angel Pons7c49cb82020-03-16 23:17:32 +01002155 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002156 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002157
Angel Pons88521882020-01-05 20:21:20 +01002158 wait_for_iosav(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002159 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002160 }
2161
2162 /* refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002163 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002164 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002165 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002166
2167 /* choose an existing rank. */
2168 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2169
Angel Ponsffd50152020-11-12 11:03:10 +01002170 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002171
Angel Pons7c49cb82020-03-16 23:17:32 +01002172 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002173 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002174
Angel Pons88521882020-01-05 20:21:20 +01002175 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002176 }
2177
Angel Pons7c49cb82020-03-16 23:17:32 +01002178 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002179 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002180
2181 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002182 dram_mrscommands(ctrl);
2183
2184 toggle_io_reset();
2185}
2186
Angel Ponsbf13ef02020-11-11 18:40:06 +01002187#define CT_MIN_PI -127
2188#define CT_MAX_PI 128
2189#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2190
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002191#define MIN_C320C_LEN 13
2192
2193static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2194{
2195 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2196 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002197 int command_pi;
2198 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002199 int delta = 0;
2200
2201 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2202
2203 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002204 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002205 }
2206
2207 ctrl->cmd_stretch[channel] = cmd_stretch;
2208
Angel Pons88521882020-01-05 20:21:20 +01002209 MCHBAR32(TC_RAP_ch(channel)) =
Angel Pons7c49cb82020-03-16 23:17:32 +01002210 (ctrl->tRRD << 0)
2211 | (ctrl->tRTP << 4)
2212 | (ctrl->tCKE << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002213 | (ctrl->tWTR << 12)
2214 | (ctrl->tFAW << 16)
Angel Pons7c49cb82020-03-16 23:17:32 +01002215 | (ctrl->tWR << 24)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002216 | (ctrl->cmd_stretch[channel] << 30);
2217
2218 if (ctrl->cmd_stretch[channel] == 2)
2219 delta = 2;
2220 else if (ctrl->cmd_stretch[channel] == 0)
2221 delta = 4;
2222
2223 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002224 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002225 }
2226
Angel Ponsbf13ef02020-11-11 18:40:06 +01002227 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002228 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002229 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002230 }
2231 program_timings(ctrl, channel);
2232 reprogram_320c(ctrl);
2233 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002234 stat[slotrank][command_pi - CT_MIN_PI] =
2235 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002236 }
2237 }
2238 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002239 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002240
Angel Ponsbf13ef02020-11-11 18:40:06 +01002241 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002242 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2243 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002244
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002245 if (rn.all || rn.length < MIN_C320C_LEN) {
2246 FOR_ALL_POPULATED_RANKS {
2247 ctrl->timings[channel][slotrank] =
2248 saved_timings[channel][slotrank];
2249 }
2250 return MAKE_ERR;
2251 }
2252 }
2253
2254 return 0;
2255}
2256
Angel Pons7c49cb82020-03-16 23:17:32 +01002257/*
2258 * Adjust CMD phase shift and try multiple command rates.
2259 * A command rate of 2T doubles the time needed for address and command decode.
2260 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002261int command_training(ramctr_timing *ctrl)
2262{
2263 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002264
2265 FOR_ALL_POPULATED_CHANNELS {
2266 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002267 }
2268
2269 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002270 int cmdrate, err;
2271
2272 /*
2273 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002274 * Issue:
2275 * While c320c discovery seems to succeed raminit will fail in write training.
2276 *
2277 * Workaround:
2278 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2279 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002280 *
2281 * Single DIMM per channel:
2282 * Try command rate 1T and 2T
2283 */
2284 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002285 if (ctrl->tCMD)
2286 /* XMP gives the CMD rate in clock ticks, not ns */
2287 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002288
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002289 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002290 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2291
2292 if (!err)
2293 break;
2294 }
2295
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002296 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002297 printk(BIOS_EMERG, "c320c discovery failed\n");
2298 return err;
2299 }
2300
Angel Pons891f2bc2020-01-10 01:27:28 +01002301 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002302 }
2303
2304 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002305 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002306
2307 reprogram_320c(ctrl);
2308 return 0;
2309}
2310
Angel Pons4c79f932020-11-14 01:26:52 +01002311static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002312{
2313 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002314 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002315 int lane;
2316
2317 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2318 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002319 ctrl->timings[channel][slotrank].lanes[lane].rising = edge;
Angel Pons891f2bc2020-01-10 01:27:28 +01002320 ctrl->timings[channel][slotrank].lanes[lane].falling = edge;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002321 }
2322 program_timings(ctrl, channel);
2323
2324 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002325 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2326 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002327 }
2328
Angel Pons88521882020-01-05 20:21:20 +01002329 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002330
Angel Ponsffd50152020-11-12 11:03:10 +01002331 iosav_write_read_mpr_sequence(
2332 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002333
Angel Pons7c49cb82020-03-16 23:17:32 +01002334 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002335 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336
Angel Pons88521882020-01-05 20:21:20 +01002337 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002338
2339 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002340 stats[lane][edge] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002341 }
2342 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002343
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002344 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002345 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002346 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002347
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002348 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002349 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2350 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002351 return MAKE_ERR;
2352 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002353 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002354 }
2355 return 0;
2356}
2357
Angel Pons60971dc2020-11-14 00:49:38 +01002358static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2359{
2360 int slotrank, lane;
2361
2362 fill_pattern0(ctrl, channel, 0, 0);
2363 FOR_ALL_LANES {
2364 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2365 }
2366
2367 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2368 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2369 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2370 }
2371
2372 program_timings(ctrl, channel);
2373
2374 FOR_ALL_POPULATED_RANKS {
2375 wait_for_iosav(channel);
2376
2377 iosav_write_read_mpr_sequence(
2378 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2379
2380 /* Execute command queue */
2381 iosav_run_once(channel);
2382
2383 wait_for_iosav(channel);
2384 }
2385
2386 /* XXX: check any measured value ? */
2387
2388 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2389 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2390 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2391 }
2392
2393 program_timings(ctrl, channel);
2394
2395 FOR_ALL_POPULATED_RANKS {
2396 wait_for_iosav(channel);
2397
2398 iosav_write_read_mpr_sequence(
2399 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2400
2401 /* Execute command queue */
2402 iosav_run_once(channel);
2403
2404 wait_for_iosav(channel);
2405 }
2406
2407 /* XXX: check any measured value ? */
2408
2409 FOR_ALL_LANES {
2410 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2411 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2412 }
2413}
2414
Angel Pons4c79f932020-11-14 01:26:52 +01002415int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002416{
2417 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2418 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2419 int channel, slotrank, lane;
2420 int err;
2421
Angel Pons88521882020-01-05 20:21:20 +01002422 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002423
2424 toggle_io_reset();
2425
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002426 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002427 FOR_ALL_LANES {
Angel Pons60971dc2020-11-14 00:49:38 +01002428 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002429 }
2430
Angel Pons60971dc2020-11-14 00:49:38 +01002431 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002432
2433 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002434 }
2435
Angel Pons0c3936e2020-03-22 12:49:27 +01002436 /*
2437 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2438 * also use a single loop. It would seem that it is a debugging configuration.
2439 */
Angel Pons88521882020-01-05 20:21:20 +01002440 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2441 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002442
2443 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002444 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002445 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002446 if (err)
2447 return err;
2448 }
2449
Angel Pons88521882020-01-05 20:21:20 +01002450 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2451 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002452
2453 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002454 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002455 rising_edges[channel][slotrank]);
2456 if (err)
2457 return err;
2458 }
2459
Angel Pons88521882020-01-05 20:21:20 +01002460 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002461
2462 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2463 ctrl->timings[channel][slotrank].lanes[lane].falling =
2464 falling_edges[channel][slotrank][lane];
2465 ctrl->timings[channel][slotrank].lanes[lane].rising =
2466 rising_edges[channel][slotrank][lane];
2467 }
2468
2469 FOR_ALL_POPULATED_CHANNELS {
2470 program_timings(ctrl, channel);
2471 }
2472
2473 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002474 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002475 }
2476 return 0;
2477}
2478
Angel Pons7c49cb82020-03-16 23:17:32 +01002479static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002480{
2481 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002482 u32 raw_stats[MAX_EDGE_TIMING + 1];
2483 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002484 const int reg3000b24[] = { 0, 0xc, 0x2c };
2485 int lane, i;
2486 int lower[NUM_LANES];
2487 int upper[NUM_LANES];
2488 int pat;
2489
2490 FOR_ALL_LANES {
2491 lower[lane] = 0;
2492 upper[lane] = MAX_EDGE_TIMING;
2493 }
2494
2495 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002496 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002497 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2498
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2500 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002501 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002502
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002503 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2504 FOR_ALL_LANES {
2505 ctrl->timings[channel][slotrank].lanes[lane].
2506 rising = edge;
2507 ctrl->timings[channel][slotrank].lanes[lane].
2508 falling = edge;
2509 }
2510 program_timings(ctrl, channel);
2511
2512 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002513 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2514 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002515 }
Angel Pons88521882020-01-05 20:21:20 +01002516 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002517
Angel Ponsffd50152020-11-12 11:03:10 +01002518 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519
Angel Pons7c49cb82020-03-16 23:17:32 +01002520 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002521 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002522
Angel Pons88521882020-01-05 20:21:20 +01002523 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002524 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002525 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002526 }
2527
Angel Pons7c49cb82020-03-16 23:17:32 +01002528 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002529 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002530 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002531
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002532 FOR_ALL_LANES {
2533 struct run rn;
2534 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002535 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2536
2537 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2538
2539 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2540 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2541 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002542 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002543
2544 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2545 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2546
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002547 edges[lane] = (lower[lane] + upper[lane]) / 2;
2548 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002549 printk(BIOS_EMERG, "edge write discovery failed: "
2550 "%d, %d, %d\n", channel, slotrank, lane);
2551
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002552 return MAKE_ERR;
2553 }
2554 }
2555 }
2556 }
2557
Angel Ponsa93f46e2020-11-17 16:54:01 +01002558 /* Restore nominal Vref after training */
2559 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002560 printram("CPA\n");
2561 return 0;
2562}
2563
2564int discover_edges_write(ramctr_timing *ctrl)
2565{
2566 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002567 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2568 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002569
Angel Pons7c49cb82020-03-16 23:17:32 +01002570 /*
2571 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2572 * also use a single loop. It would seem that it is a debugging configuration.
2573 */
Angel Pons88521882020-01-05 20:21:20 +01002574 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2575 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002576
2577 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2578 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002579 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002580 if (err)
2581 return err;
2582 }
2583
Angel Pons88521882020-01-05 20:21:20 +01002584 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2585 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002586
2587 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2588 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002589 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002590 if (err)
2591 return err;
2592 }
2593
Angel Pons88521882020-01-05 20:21:20 +01002594 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002595
2596 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2597 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002598 falling_edges[channel][slotrank][lane];
2599
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002600 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002601 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002602 }
2603
2604 FOR_ALL_POPULATED_CHANNELS
2605 program_timings(ctrl, channel);
2606
2607 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002608 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002609 }
2610 return 0;
2611}
2612
2613static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2614{
Angel Pons88521882020-01-05 20:21:20 +01002615 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002616
Angel Ponsffd50152020-11-12 11:03:10 +01002617 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002618
Angel Pons7c49cb82020-03-16 23:17:32 +01002619 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002620 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002621
Angel Pons88521882020-01-05 20:21:20 +01002622 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002623}
2624
2625int discover_timC_write(ramctr_timing *ctrl)
2626{
Angel Pons7c49cb82020-03-16 23:17:32 +01002627 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002628 int i, pat;
2629
2630 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2631 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2632 int channel, slotrank, lane;
2633
2634 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2635 lower[channel][slotrank][lane] = 0;
2636 upper[channel][slotrank][lane] = MAX_TIMC;
2637 }
2638
Angel Pons88521882020-01-05 20:21:20 +01002639 /*
2640 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2641 * FIXME: This must only be done on Ivy Bridge.
2642 */
2643 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002644 printram("discover timC write:\n");
2645
2646 for (i = 0; i < 3; i++)
2647 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002648
2649 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2650 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2651 ~0x3f000000, rege3c_b24[i] << 24);
2652
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002653 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002654
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002655 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2656 FOR_ALL_POPULATED_RANKS {
2657 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002658 u32 raw_stats[MAX_TIMC + 1];
2659 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002660
2661 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002662 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002663
2664 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002665
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002666 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002667 FOR_ALL_LANES {
2668 ctrl->timings[channel][slotrank]
2669 .lanes[lane].timC = timC;
2670 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002671 program_timings(ctrl, channel);
2672
2673 test_timC_write (ctrl, channel, slotrank);
2674
Angel Pons7c49cb82020-03-16 23:17:32 +01002675 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002676 raw_stats[timC] = MCHBAR32(
2677 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002678 }
2679 FOR_ALL_LANES {
2680 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002681 for (timC = 0; timC < MAX_TIMC; timC++) {
2682 stats[timC] = !!(raw_stats[timC]
2683 & (1 << lane));
2684 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002685
Angel Pons7c49cb82020-03-16 23:17:32 +01002686 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002687 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002688 printk(BIOS_EMERG,
2689 "timC write discovery failed: "
2690 "%d, %d, %d\n", channel,
2691 slotrank, lane);
2692
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002693 return MAKE_ERR;
2694 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002695 printram("timC: %d, %d, %d: "
2696 "0x%02x-0x%02x-0x%02x, "
2697 "0x%02x-0x%02x\n", channel, slotrank,
2698 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002699 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002700 rn.end - ctrl->timC_offset[i]);
2701
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002702 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002703 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002704 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002705
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002706 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002707 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002708 upper[channel][slotrank][lane]);
2709
2710 }
2711 }
2712 }
2713 }
2714
2715 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002716 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002717 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002718 udelay(2);
2719 }
2720
Angel Pons88521882020-01-05 20:21:20 +01002721 /*
2722 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2723 * FIXME: This must only be done on Ivy Bridge.
2724 */
2725 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002726
2727 printram("CPB\n");
2728
2729 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002730 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002731 (lower[channel][slotrank][lane] +
2732 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002733
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002734 ctrl->timings[channel][slotrank].lanes[lane].timC =
2735 (lower[channel][slotrank][lane] +
2736 upper[channel][slotrank][lane]) / 2;
2737 }
2738 FOR_ALL_POPULATED_CHANNELS {
2739 program_timings(ctrl, channel);
2740 }
2741 return 0;
2742}
2743
Angel Pons88521882020-01-05 20:21:20 +01002744void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002745{
2746 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002747 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002748
2749 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2750 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002751 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002752 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002753 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002754 printram("normalize %d, %d, %d: mat %d\n",
2755 channel, slotrank, lane, mat);
2756
Felix Heldef4fe3e2019-12-31 14:15:05 +01002757 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002758 printram("normalize %d, %d, %d: delta %d\n",
2759 channel, slotrank, lane, delta);
2760
Angel Pons88521882020-01-05 20:21:20 +01002761 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002762 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002763 }
2764
2765 FOR_ALL_POPULATED_CHANNELS {
2766 program_timings(ctrl, channel);
2767 }
2768}
2769
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002770int channel_test(ramctr_timing *ctrl)
2771{
2772 int channel, slotrank, lane;
2773
2774 slotrank = 0;
2775 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002776 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002777 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002778 return MAKE_ERR;
2779 }
2780 FOR_ALL_POPULATED_CHANNELS {
2781 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002782 }
2783
2784 for (slotrank = 0; slotrank < 4; slotrank++)
2785 FOR_ALL_CHANNELS
2786 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2787 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002788 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2789 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002790 }
Angel Pons88521882020-01-05 20:21:20 +01002791 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002792
Angel Ponsffd50152020-11-12 11:03:10 +01002793 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002794
Angel Pons7c49cb82020-03-16 23:17:32 +01002795 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002796 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002797
Angel Pons88521882020-01-05 20:21:20 +01002798 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002799 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002800 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002801 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2802 channel, slotrank, lane);
2803 return MAKE_ERR;
2804 }
2805 }
2806 return 0;
2807}
2808
Patrick Rudolphdd662872017-10-28 18:20:11 +02002809void channel_scrub(ramctr_timing *ctrl)
2810{
2811 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002812 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002813
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002814 FOR_ALL_POPULATED_CHANNELS {
2815 wait_for_iosav(channel);
2816 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002817 }
2818
2819 /*
2820 * During runtime the "scrubber" will periodically scan through the memory in the
2821 * physical address space, to identify and fix CRC errors.
2822 * The following loops writes to every DRAM address, setting the ECC bits to the
2823 * correct value. A read from this location will no longer return a CRC error,
2824 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002825 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002826 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2827 * and firmware running in x86_32.
2828 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002829 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2830 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002831 for (bank = 0; bank < 8; bank++) {
2832 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002833
Angel Pons8f0757e2020-11-11 23:03:36 +01002834 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2835 const struct iosav_ssq sequence[] = {
2836 /*
2837 * DRAM command ACT
2838 * Opens the row for writing.
2839 */
2840 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002841 .sp_cmd_ctrl = {
2842 .command = IOSAV_ACT,
2843 .ranksel_ap = 1,
2844 },
2845 .subseq_ctrl = {
2846 .cmd_executions = 1,
2847 .cmd_delay_gap = gap,
2848 .post_ssq_wait = ctrl->tRCD,
2849 .data_direction = SSQ_NA,
2850 },
2851 .sp_cmd_addr = {
2852 .address = row,
2853 .rowbits = 6,
2854 .bank = bank,
2855 .rank = slotrank,
2856 },
2857 .addr_update = {
2858 .inc_addr_1 = 1,
2859 .addr_wrap = 18,
2860 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002861 },
2862 /*
2863 * DRAM command WR
2864 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2865 * bytes.
2866 */
2867 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002868 .sp_cmd_ctrl = {
2869 .command = IOSAV_WR,
2870 .ranksel_ap = 1,
2871 },
2872 .subseq_ctrl = {
2873 .cmd_executions = 129,
2874 .cmd_delay_gap = 4,
2875 .post_ssq_wait = ctrl->tWTR +
2876 ctrl->CWL + 8,
2877 .data_direction = SSQ_WR,
2878 },
2879 .sp_cmd_addr = {
2880 .address = row,
2881 .rowbits = 0,
2882 .bank = bank,
2883 .rank = slotrank,
2884 },
2885 .addr_update = {
2886 .inc_addr_8 = 1,
2887 .addr_wrap = 9,
2888 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002889 },
2890 /*
2891 * DRAM command PRE
2892 * Closes the row.
2893 */
2894 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002895 .sp_cmd_ctrl = {
2896 .command = IOSAV_PRE,
2897 .ranksel_ap = 1,
2898 },
2899 .subseq_ctrl = {
2900 .cmd_executions = 1,
2901 .cmd_delay_gap = 4,
2902 .post_ssq_wait = ctrl->tRP,
2903 .data_direction = SSQ_NA,
2904 },
2905 .sp_cmd_addr = {
2906 .address = 0,
2907 .rowbits = 6,
2908 .bank = bank,
2909 .rank = slotrank,
2910 },
2911 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002912 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002913 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002914 },
2915 };
2916 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002917
2918 /* Execute command queue */
2919 iosav_run_queue(channel, 16, 0);
2920
2921 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002922 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002923 }
2924 }
2925}
2926
Angel Pons88521882020-01-05 20:21:20 +01002927void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002928{
2929 int channel;
2930
Angel Pons7c49cb82020-03-16 23:17:32 +01002931 /* 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 +01002932 static u32 seeds[NUM_CHANNELS][3] = {
2933 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2934 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2935 };
2936 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002937 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002938 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2939 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2940 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002941 }
2942}
2943
Angel Pons89ae6b82020-03-21 13:23:32 +01002944void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002945{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002946 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002947 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002948 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002949 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002950 }
2951}
2952
Angel Pons88521882020-01-05 20:21:20 +01002953void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002954{
2955 int channel;
2956
2957 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002958 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002959 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002960 }
2961
2962 udelay(1);
2963
2964 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002965 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002966 }
2967}
2968
Angel Pons7c49cb82020-03-16 23:17:32 +01002969void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002970{
2971 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002972
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002973 FOR_ALL_POPULATED_CHANNELS {
2974 u32 b20, b4_8_12;
Angel Pons88521882020-01-05 20:21:20 +01002975 int min_pi = 10000;
2976 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002977
2978 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002979 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2980 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002981 }
2982
Angel Pons7c49cb82020-03-16 23:17:32 +01002983 b20 = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002984
Angel Pons7c49cb82020-03-16 23:17:32 +01002985 b4_8_12 = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 0x3330 : 0x2220;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002986
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002987 dram_odt_stretch(ctrl, channel);
2988
Angel Ponsdc5539f2020-11-12 12:44:25 +01002989 MCHBAR32(TC_RWP_ch(channel)) = (1 << 27) | (2 << 24) | (b20 << 20) |
Felix Held2463aa92018-07-29 21:37:55 +02002990 ((ctrl->ref_card_offset[channel] + 2) << 16) | b4_8_12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002991 }
2992}
2993
Angel Pons88521882020-01-05 20:21:20 +01002994void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002995{
2996 int channel;
2997 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002998 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2999 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003000 }
3001}
3002
Angel Pons7c49cb82020-03-16 23:17:32 +01003003/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
3004static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003005{
Angel Pons88521882020-01-05 20:21:20 +01003006 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003007}
3008
Angel Pons7c49cb82020-03-16 23:17:32 +01003009/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01003010void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003011{
Angel Ponsb50ca572020-11-11 19:07:20 +01003012 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01003013
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003014 int channel;
3015 int t1_cycles = 0, t1_ns = 0, t2_ns;
3016 int t3_ns;
3017 u32 r32;
3018
Angel Pons7c49cb82020-03-16 23:17:32 +01003019 /* FIXME: This register only exists on Ivy Bridge */
3020 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003021
Felix Heldf9b826a2018-07-30 17:56:52 +02003022 FOR_ALL_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003023 MCHBAR32_AND_OR(TC_OTHP_ch(channel), ~(3 << 12), 1 << 12);
Patrick Rudolph652c4912017-10-31 11:36:55 +01003024
Patrick Rudolph74203de2017-11-20 11:57:01 +01003025 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003026 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003027 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003028 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003029 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003030 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003031
Felix Heldf9b826a2018-07-30 17:56:52 +02003032 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003033 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003034
Angel Pons88521882020-01-05 20:21:20 +01003035 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3036 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003037
3038 FOR_ALL_CHANNELS {
3039 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003040 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003041 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003042 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003043 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003044 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003045 case 1:
3046 case 4:
3047 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003048 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003049 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003050 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003051 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003052 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003053 break;
3054 }
3055 }
3056
Felix Held50b7ed22019-12-30 20:41:54 +01003057 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003058 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003059 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003060
3061 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003062 MCHBAR32_AND_OR(TC_RFP_ch(channel), ~(3 << 16), 1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003063
Angel Ponsdc5539f2020-11-12 12:44:25 +01003064 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3065 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003066 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003067
Angel Pons7c49cb82020-03-16 23:17:32 +01003068 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003069 FOR_ALL_POPULATED_CHANNELS
3070 break;
3071
Angel Pons88521882020-01-05 20:21:20 +01003072 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3073 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003074 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003075 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003076 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003077 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003078 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003079 t1_ns += 500;
3080
Angel Pons88521882020-01-05 20:21:20 +01003081 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003082 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003083 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003084 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003085 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003086 t3_ns = 500;
3087 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003088
3089 /* The graphics driver will use these watermark values */
3090 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003091 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003092 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3093 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003094}
3095
Angel Pons88521882020-01-05 20:21:20 +01003096void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003097{
3098 int channel, slotrank, lane;
3099
Angel Pons7c49cb82020-03-16 23:17:32 +01003100 FOR_ALL_POPULATED_CHANNELS {
3101 MCHBAR32(TC_RAP_ch(channel)) =
3102 (ctrl->tRRD << 0)
3103 | (ctrl->tRTP << 4)
3104 | (ctrl->tCKE << 8)
3105 | (ctrl->tWTR << 12)
3106 | (ctrl->tFAW << 16)
3107 | (ctrl->tWR << 24)
3108 | (ctrl->cmd_stretch[channel] << 30);
3109 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003110
3111 udelay(1);
3112
3113 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003114 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003115 }
3116
3117 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003118 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003119 }
3120
3121 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003122 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003123
3124 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003125 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003126 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003127 }
3128
3129 printram("CPE\n");
3130
Angel Pons88521882020-01-05 20:21:20 +01003131 MCHBAR32(GDCRTRAININGMOD) = 0;
3132 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003133
3134 printram("CP5b\n");
3135
3136 FOR_ALL_POPULATED_CHANNELS {
3137 program_timings(ctrl, channel);
3138 }
3139
3140 u32 reg, addr;
3141
Angel Pons7c49cb82020-03-16 23:17:32 +01003142 /* Poll for RCOMP */
3143 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3144 ;
3145
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003146 do {
Angel Pons88521882020-01-05 20:21:20 +01003147 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003148 } while ((reg & 0x14) == 0);
3149
Angel Pons7c49cb82020-03-16 23:17:32 +01003150 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003151 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003152 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003153
Angel Pons7c49cb82020-03-16 23:17:32 +01003154 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003155 udelay(500);
3156
3157 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003158 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003159 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003160 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003161 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003162 MCHBAR32(addr) = reg;
3163
Angel Pons7c49cb82020-03-16 23:17:32 +01003164 /* Wait 10ns for ranks to settle */
3165 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003166
3167 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3168 MCHBAR32(addr) = reg;
3169
Angel Pons7c49cb82020-03-16 23:17:32 +01003170 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003171 write_reset(ctrl);
3172 }
3173
Angel Pons7c49cb82020-03-16 23:17:32 +01003174 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003175 dram_mrscommands(ctrl);
3176
3177 printram("CP5c\n");
3178
Angel Pons88521882020-01-05 20:21:20 +01003179 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003180
3181 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003182 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003183 udelay(2);
3184 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003185}