blob: 0eb966d2fddba835be4801773f57872dd9b17eac [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 Pons7c49cb82020-03-16 23:17:32 +0100601static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100602{
Angel Pons88521882020-01-05 20:21:20 +0100603 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100604
605 if (ctrl->rank_mirror[channel][slotrank]) {
606 /* DDR3 Rank1 Address mirror
Angel Pons7c49cb82020-03-16 23:17:32 +0100607 swap the following pins:
608 A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100609 reg = ((reg >> 1) & 1) | ((reg << 1) & 2);
Angel Pons7c49cb82020-03-16 23:17:32 +0100610 val = (val & ~0x1f8) | ((val >> 1) & 0xa8) | ((val & 0xa8) << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100611 }
612
Angel Pons8f0757e2020-11-11 23:03:36 +0100613 const struct iosav_ssq sequence[] = {
614 /* DRAM command MRS */
615 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200616 .sp_cmd_ctrl = {
617 .command = IOSAV_MRS,
618 },
619 .subseq_ctrl = {
620 .cmd_executions = 1,
621 .cmd_delay_gap = 4,
622 .post_ssq_wait = 4,
623 .data_direction = SSQ_NA,
624 },
625 .sp_cmd_addr = {
626 .address = val,
627 .rowbits = 6,
628 .bank = reg,
629 .rank = slotrank,
630 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100631 },
632 /* DRAM command MRS */
633 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200634 .sp_cmd_ctrl = {
635 .command = IOSAV_MRS,
636 .ranksel_ap = 1,
637 },
638 .subseq_ctrl = {
639 .cmd_executions = 1,
640 .cmd_delay_gap = 4,
641 .post_ssq_wait = 4,
642 .data_direction = SSQ_NA,
643 },
644 .sp_cmd_addr = {
645 .address = val,
646 .rowbits = 6,
647 .bank = reg,
648 .rank = slotrank,
649 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100650 },
651 /* DRAM command MRS */
652 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200653 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100654 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200655 },
656 .subseq_ctrl = {
657 .cmd_executions = 1,
658 .cmd_delay_gap = 4,
659 .post_ssq_wait = ctrl->tMOD,
660 .data_direction = SSQ_NA,
661 },
662 .sp_cmd_addr = {
663 .address = val,
664 .rowbits = 6,
665 .bank = reg,
666 .rank = slotrank,
667 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100668 },
669 };
670 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200671
Angel Pons7c49cb82020-03-16 23:17:32 +0100672 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200673 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100674}
675
Angel Pons88521882020-01-05 20:21:20 +0100676static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100677{
678 u16 mr0reg, mch_cas, mch_wr;
679 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 +0100680 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100681
Angel Pons7c49cb82020-03-16 23:17:32 +0100682 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100683 if (ctrl->CAS < 12) {
684 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
685 } else {
686 mch_cas = (u16) (ctrl->CAS - 12);
687 mch_cas = ((mch_cas << 1) | 0x1);
688 }
689
Angel Pons7c49cb82020-03-16 23:17:32 +0100690 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100691 mch_wr = mch_wr_t[ctrl->tWR - 5];
692
Angel Pons2bf28ed2020-11-12 13:49:59 +0100693 /* DLL Reset - self clearing - set after CLK frequency has been changed */
694 mr0reg = 1 << 8;
695
696 mr0reg |= (mch_cas & 0x1) << 2;
697 mr0reg |= (mch_cas & 0xe) << 3;
698 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100699
Angel Pons7c49cb82020-03-16 23:17:32 +0100700 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
Angel Pons2bf28ed2020-11-12 13:49:59 +0100701 mr0reg |= !is_mobile << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100702 return mr0reg;
703}
704
705static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
706{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200707 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100708}
709
Angel Ponsf9997482020-11-12 16:02:52 +0100710static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100711{
712 /* Get ODT based on rankmap */
713 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
714
715 if (dimms_per_ch == 1) {
716 return (const odtmap){60, 60};
717 } else {
718 return (const odtmap){120, 30};
719 }
720}
721
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100722static u32 encode_odt(u32 odt)
723{
724 switch (odt) {
725 case 30:
726 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
727 case 60:
728 return (1 << 2); // RZQ/4
729 case 120:
730 return (1 << 6); // RZQ/2
731 default:
732 case 0:
733 return 0;
734 }
735}
736
737static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
738{
739 odtmap odt;
740 u32 mr1reg;
741
Angel Ponsf9997482020-11-12 16:02:52 +0100742 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100743 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100744
745 mr1reg |= encode_odt(odt.rttnom);
746
747 return mr1reg;
748}
749
750static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
751{
752 u16 mr1reg;
753
754 mr1reg = make_mr1(ctrl, rank, channel);
755
756 write_mrreg(ctrl, channel, rank, 1, mr1reg);
757}
758
759static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
760{
Angel Pons868bca22020-11-13 13:38:04 +0100761 const u16 pasr = 0;
762 const u16 cwl = ctrl->CWL - 5;
763 const odtmap odt = get_ODT(ctrl, channel);
764
Angel Ponsdca3cb52020-11-13 13:42:07 +0100765 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100766 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
767 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100768
Angel Pons868bca22020-11-13 13:38:04 +0100769 u16 mr2reg = 0;
770 mr2reg |= pasr;
771 mr2reg |= cwl << 3;
772 mr2reg |= ctrl->auto_self_refresh << 6;
773 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100774 mr2reg |= (odt.rttwr / 60) << 9;
775
776 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100777
778 /* Program MR2 shadow */
779 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
780
781 reg32 &= 3 << 14 | 3 << 6;
782
783 reg32 |= mr2reg & ~(3 << 6);
784
785 if (rank & 1) {
786 if (srt)
787 reg32 |= 1 << (rank / 2 + 6);
788 } else {
789 if (ctrl->rank_mirror[channel][rank])
790 reg32 |= 1 << (rank / 2 + 14);
791 }
792 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100793}
794
795static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
796{
797 write_mrreg(ctrl, channel, rank, 3, 0);
798}
799
Angel Pons88521882020-01-05 20:21:20 +0100800void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100801{
802 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100803 int channel;
804
805 FOR_ALL_POPULATED_CHANNELS {
806 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100807 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100808 dram_mr2(ctrl, slotrank, channel);
809
Angel Pons7c49cb82020-03-16 23:17:32 +0100810 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100811 dram_mr3(ctrl, slotrank, channel);
812
Angel Pons7c49cb82020-03-16 23:17:32 +0100813 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100814 dram_mr1(ctrl, slotrank, channel);
815
Angel Pons7c49cb82020-03-16 23:17:32 +0100816 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100817 dram_mr0(ctrl, slotrank, channel);
818 }
819 }
820
Angel Pons8f0757e2020-11-11 23:03:36 +0100821 const struct iosav_ssq zqcl_sequence[] = {
822 /* DRAM command NOP (without ODT nor chip selects) */
823 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200824 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100825 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200826 },
827 .subseq_ctrl = {
828 .cmd_executions = 1,
829 .cmd_delay_gap = 4,
830 .post_ssq_wait = 15,
831 .data_direction = SSQ_NA,
832 },
833 .sp_cmd_addr = {
834 .address = 2,
835 .rowbits = 6,
836 .bank = 0,
837 .rank = 0,
838 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100839 },
840 /* DRAM command ZQCL */
841 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200842 .sp_cmd_ctrl = {
843 .command = IOSAV_ZQCS,
844 .ranksel_ap = 1,
845 },
846 .subseq_ctrl = {
847 .cmd_executions = 1,
848 .cmd_delay_gap = 4,
849 .post_ssq_wait = 400,
850 .data_direction = SSQ_NA,
851 },
852 .sp_cmd_addr = {
853 .address = 1024,
854 .rowbits = 6,
855 .bank = 0,
856 .rank = 0,
857 },
858 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100859 .inc_rank = 1,
860 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200861 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100862 },
863 };
864 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100865
Angel Pons7c49cb82020-03-16 23:17:32 +0100866 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200867 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100868
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100869 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100870 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100871 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100872 }
873
Angel Pons7c49cb82020-03-16 23:17:32 +0100874 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100875 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100876
877 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100878 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100879
Angel Pons88521882020-01-05 20:21:20 +0100880 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100881
882 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
883
Angel Pons7c49cb82020-03-16 23:17:32 +0100884 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100885 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100886
Angel Ponsffd50152020-11-12 11:03:10 +0100887 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200888
Angel Pons7c49cb82020-03-16 23:17:32 +0100889 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200890 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100891
Angel Pons7c49cb82020-03-16 23:17:32 +0100892 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100893 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100894 }
895}
896
Felix Held3b906032020-01-14 17:05:43 +0100897static const u32 lane_base[] = {
898 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
899 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
900 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901};
902
Angel Pons88521882020-01-05 20:21:20 +0100903void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100904{
Angel Pons88521882020-01-05 20:21:20 +0100905 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100906 int lane;
907 int slotrank, slot;
908 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100909 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100910
911 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100912 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
913 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100914 }
915
916 for (slot = 0; slot < NUM_SLOTS; slot++)
917 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
918 case 0:
919 default:
Angel Pons88521882020-01-05 20:21:20 +0100920 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100921 break;
922 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100923 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100924 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100925 break;
926 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100927 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100928 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100929 break;
930 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100931 pi_coding_ctrl[slot] =
932 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100933 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100934 break;
935 }
936
Angel Pons7c49cb82020-03-16 23:17:32 +0100937 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100938 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100939 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
940 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100941 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100942 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
943
Angel Pons88521882020-01-05 20:21:20 +0100944 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100945
Angel Pons7c49cb82020-03-16 23:17:32 +0100946 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100947 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
948 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100949
950 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100951 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100952 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100953 if (shift < 0)
954 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100955
Angel Pons88521882020-01-05 20:21:20 +0100956 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100957
958 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100959 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
960 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100961 }
962
Angel Pons88521882020-01-05 20:21:20 +0100963 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
964 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100965
Angel Pons88521882020-01-05 20:21:20 +0100966 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100967 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100968
Angel Pons88521882020-01-05 20:21:20 +0100969 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100970
971 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100972 int post_timA_min_high = 7, pre_timA_min_high = 7;
973 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100974 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100975 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100976
977 if (shift < 0)
978 shift = 0;
979
980 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200981 post_timA_min_high = MIN(post_timA_min_high,
982 (ctrl->timings[channel][slotrank].lanes[lane].
983 timA + shift) >> 6);
984 pre_timA_min_high = MIN(pre_timA_min_high,
985 ctrl->timings[channel][slotrank].lanes[lane].
986 timA >> 6);
987 post_timA_max_high = MAX(post_timA_max_high,
988 (ctrl->timings[channel][slotrank].lanes[lane].
989 timA + shift) >> 6);
990 pre_timA_max_high = MAX(pre_timA_max_high,
991 ctrl->timings[channel][slotrank].lanes[lane].
992 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100993 }
994
995 if (pre_timA_max_high - pre_timA_min_high <
996 post_timA_max_high - post_timA_min_high)
997 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +0100998
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100999 else if (pre_timA_max_high - pre_timA_min_high >
1000 post_timA_max_high - post_timA_min_high)
1001 shift_402x = -1;
1002
Felix Helddee167e2019-12-30 17:30:16 +01001003 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001004 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001005 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001006
Angel Pons88521882020-01-05 20:21:20 +01001007 reg_roundtrip_latency |=
1008 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001009 shift_402x) << (8 * slotrank);
1010
1011 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001012 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001013 (((ctrl->timings[channel][slotrank].lanes[lane].
1014 timA + shift) & 0x3f)
1015 |
1016 ((ctrl->timings[channel][slotrank].lanes[lane].
1017 rising + shift) << 8)
1018 |
1019 (((ctrl->timings[channel][slotrank].lanes[lane].
1020 timA + shift -
1021 (post_timA_min_high << 6)) & 0x1c0) << 10)
1022 | ((ctrl->timings[channel][slotrank].lanes[lane].
1023 falling + shift) << 20));
1024
Felix Heldfb19c8a2020-01-14 21:27:59 +01001025 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001026 (((ctrl->timings[channel][slotrank].lanes[lane].
1027 timC + shift) & 0x3f)
1028 |
1029 (((ctrl->timings[channel][slotrank].lanes[lane].
1030 timB + shift) & 0x3f) << 8)
1031 |
1032 (((ctrl->timings[channel][slotrank].lanes[lane].
1033 timB + shift) & 0x1c0) << 9)
1034 |
1035 (((ctrl->timings[channel][slotrank].lanes[lane].
1036 timC + shift) & 0x40) << 13));
1037 }
1038 }
Angel Pons88521882020-01-05 20:21:20 +01001039 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1040 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001041}
1042
Angel Pons88521882020-01-05 20:21:20 +01001043static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001044{
Angel Pons88521882020-01-05 20:21:20 +01001045 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001046
Angel Ponsffd50152020-11-12 11:03:10 +01001047 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001048
Angel Pons7c49cb82020-03-16 23:17:32 +01001049 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001050 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001051
Angel Pons88521882020-01-05 20:21:20 +01001052 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053}
1054
Angel Pons7c49cb82020-03-16 23:17:32 +01001055static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001056{
1057 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001058
1059 return (MCHBAR32(lane_base[lane] +
1060 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001061}
1062
1063struct run {
1064 int middle;
1065 int end;
1066 int start;
1067 int all;
1068 int length;
1069};
1070
1071static struct run get_longest_zero_run(int *seq, int sz)
1072{
1073 int i, ls;
1074 int bl = 0, bs = 0;
1075 struct run ret;
1076
1077 ls = 0;
1078 for (i = 0; i < 2 * sz; i++)
1079 if (seq[i % sz]) {
1080 if (i - ls > bl) {
1081 bl = i - ls;
1082 bs = ls;
1083 }
1084 ls = i + 1;
1085 }
1086 if (bl == 0) {
1087 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001088 ret.start = 0;
1089 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001090 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001091 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001092 return ret;
1093 }
1094
Angel Pons7c49cb82020-03-16 23:17:32 +01001095 ret.start = bs % sz;
1096 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001097 ret.middle = (bs + (bl - 1) / 2) % sz;
1098 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001099 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001100
1101 return ret;
1102}
1103
Angel Ponsf3053392020-11-13 23:31:12 +01001104static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001105{
1106 int timA;
1107 int statistics[NUM_LANES][128];
1108 int lane;
1109
1110 for (timA = 0; timA < 128; timA++) {
1111 FOR_ALL_LANES {
1112 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1113 }
1114 program_timings(ctrl, channel);
1115
1116 test_timA(ctrl, channel, slotrank);
1117
1118 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001119 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001120 }
1121 }
1122 FOR_ALL_LANES {
1123 struct run rn = get_longest_zero_run(statistics[lane], 128);
1124 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1125 upperA[lane] = rn.end;
1126 if (upperA[lane] < rn.middle)
1127 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001128
Patrick Rudolph368b6152016-11-25 16:36:52 +01001129 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001130 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001131 }
1132}
1133
Angel Ponsf3053392020-11-13 23:31:12 +01001134static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001135{
1136 int timA_delta;
1137 int statistics[NUM_LANES][51];
1138 int lane, i;
1139
1140 memset(statistics, 0, sizeof(statistics));
1141
1142 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001143
1144 FOR_ALL_LANES {
1145 ctrl->timings[channel][slotrank].lanes[lane].timA
1146 = upperA[lane] + timA_delta + 0x40;
1147 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001148 program_timings(ctrl, channel);
1149
1150 for (i = 0; i < 100; i++) {
1151 test_timA(ctrl, channel, slotrank);
1152 FOR_ALL_LANES {
1153 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001154 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001155 }
1156 }
1157 }
1158 FOR_ALL_LANES {
1159 int last_zero, first_all;
1160
1161 for (last_zero = -25; last_zero <= 25; last_zero++)
1162 if (statistics[lane][last_zero + 25])
1163 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001164
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001165 last_zero--;
1166 for (first_all = -25; first_all <= 25; first_all++)
1167 if (statistics[lane][first_all + 25] == 100)
1168 break;
1169
Angel Pons7c49cb82020-03-16 23:17:32 +01001170 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001171
1172 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001173 (last_zero + first_all) / 2 + upperA[lane];
1174
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001175 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001176 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001177 }
1178}
1179
Angel Ponsf3053392020-11-13 23:31:12 +01001180static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001181{
1182 int works[NUM_LANES];
1183 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001184
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001185 while (1) {
1186 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001187
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001188 program_timings(ctrl, channel);
1189 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001190
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001191 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001192 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1193
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001194 if (works[lane])
1195 some_works = 1;
1196 else
1197 all_works = 0;
1198 }
1199 if (all_works)
1200 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001201
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001202 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001203 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001204 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1205 channel, slotrank);
1206 return MAKE_ERR;
1207 }
Angel Pons88521882020-01-05 20:21:20 +01001208 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001209 printram("4024 -= 2;\n");
1210 continue;
1211 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001212 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001213 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001214
Felix Heldef4fe3e2019-12-31 14:15:05 +01001215 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001216 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1217 channel, slotrank);
1218 return MAKE_ERR;
1219 }
1220 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001221 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001222 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001223 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 }
1225 }
1226 return 0;
1227}
1228
Angel Pons12bd8ab2020-11-13 23:10:52 +01001229static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001230{
1231 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001232 u16 logic_delay_min = 7;
1233 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001234
1235 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001236 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1237
1238 logic_delay_min = MIN(logic_delay_min, logic_delay);
1239 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001240 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001241
1242 if (logic_delay_max < logic_delay_min) {
1243 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1244 logic_delay_max, logic_delay_min, channel, slotrank);
1245 }
1246
1247 assert(logic_delay_max >= logic_delay_min);
1248
1249 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001250}
1251
Angel Pons12bd8ab2020-11-13 23:10:52 +01001252static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001253{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001254 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001255
Angel Pons7c49cb82020-03-16 23:17:32 +01001256 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001257 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001258
Angel Pons12bd8ab2020-11-13 23:10:52 +01001259 if (prev < post)
1260 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001261
Angel Pons12bd8ab2020-11-13 23:10:52 +01001262 else if (prev > post)
1263 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001264
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001265 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001266 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001267
Angel Pons12bd8ab2020-11-13 23:10:52 +01001268 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1269 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1270 printram("4024 += %d;\n", latency_offset);
1271 printram("4028 += %d;\n", latency_offset);
1272
1273 return post;
1274}
1275
1276static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1277{
1278 u16 logic_delay_min = 7;
1279 int lane;
1280
1281 FOR_ALL_LANES {
1282 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1283
1284 logic_delay_min = MIN(logic_delay_min, logic_delay);
1285 }
1286
1287 if (logic_delay_min >= 2) {
1288 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1289 logic_delay_min, channel, slotrank);
1290 }
1291
1292 FOR_ALL_LANES {
1293 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1294 }
1295 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1296 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001297}
1298
Angel Pons7c49cb82020-03-16 23:17:32 +01001299/*
1300 * Compensate the skew between DQS and DQs.
1301 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001302 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1303 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001304 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001305 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001306 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001307 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1308 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1309 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001310 * DQ in phase the data being read is expected to alternate on every byte:
1311 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001312 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001313 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001314 * Once the controller has detected this pattern a bit in the result register is set for the
1315 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001316 */
Angel Pons88521882020-01-05 20:21:20 +01001317int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001318{
1319 int channel, slotrank, lane;
1320 int err;
1321
1322 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1323 int all_high, some_high;
1324 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001325 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001326
Angel Pons88521882020-01-05 20:21:20 +01001327 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001328
Angel Ponsffd50152020-11-12 11:03:10 +01001329 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001330
Angel Pons7c49cb82020-03-16 23:17:32 +01001331 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001332 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001333
Angel Pons88521882020-01-05 20:21:20 +01001334 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001335
Felix Heldef4fe3e2019-12-31 14:15:05 +01001336 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001337 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001338 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001339
Angel Ponsf3053392020-11-13 23:31:12 +01001340 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001341
Felix Held2bb3cdf2018-07-28 00:23:59 +02001342 all_high = 1;
1343 some_high = 0;
1344 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001345 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001346 some_high = 1;
1347 else
1348 all_high = 0;
1349 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001350
1351 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001352 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001353 printram("4028--;\n");
1354 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001355 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001356 upperA[lane] -= 0x40;
1357
1358 }
1359 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001360 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001361 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001362 printram("4024++;\n");
1363 printram("4028++;\n");
1364 }
1365
1366 program_timings(ctrl, channel);
1367
Angel Pons12bd8ab2020-11-13 23:10:52 +01001368 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001369
Angel Ponsf3053392020-11-13 23:31:12 +01001370 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001371 if (err)
1372 return err;
1373
Angel Pons12bd8ab2020-11-13 23:10:52 +01001374 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001375
Angel Ponsf3053392020-11-13 23:31:12 +01001376 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001377
Angel Pons12bd8ab2020-11-13 23:10:52 +01001378 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379
Angel Pons12bd8ab2020-11-13 23:10:52 +01001380 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001381
Angel Pons12bd8ab2020-11-13 23:10:52 +01001382 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001383
1384 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001385 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001386 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001387
1388 printram("final results:\n");
1389 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001390 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001391 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001392
Angel Pons88521882020-01-05 20:21:20 +01001393 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394
1395 toggle_io_reset();
1396 }
1397
1398 FOR_ALL_POPULATED_CHANNELS {
1399 program_timings(ctrl, channel);
1400 }
1401 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001402 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001403 }
1404 return 0;
1405}
1406
Angel Pons88521882020-01-05 20:21:20 +01001407static void test_timC(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001408{
1409 int lane;
1410
1411 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001412 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1413 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001414 }
1415
Angel Pons88521882020-01-05 20:21:20 +01001416 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001417
Angel Ponsffd50152020-11-12 11:03:10 +01001418 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1419 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001420
Angel Pons7c49cb82020-03-16 23:17:32 +01001421 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001422 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001423
Angel Pons88521882020-01-05 20:21:20 +01001424 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001425
Angel Pons8f0757e2020-11-11 23:03:36 +01001426 const struct iosav_ssq rd_sequence[] = {
1427 /* DRAM command PREA */
1428 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001429 .sp_cmd_ctrl = {
1430 .command = IOSAV_PRE,
1431 .ranksel_ap = 1,
1432 },
1433 .subseq_ctrl = {
1434 .cmd_executions = 1,
1435 .cmd_delay_gap = 3,
1436 .post_ssq_wait = ctrl->tRP,
1437 .data_direction = SSQ_NA,
1438 },
1439 .sp_cmd_addr = {
1440 .address = 1024,
1441 .rowbits = 6,
1442 .bank = 0,
1443 .rank = slotrank,
1444 },
1445 .addr_update = {
1446 .addr_wrap = 18,
1447 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001448 },
1449 /* DRAM command ACT */
1450 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001451 .sp_cmd_ctrl = {
1452 .command = IOSAV_ACT,
1453 .ranksel_ap = 1,
1454 },
1455 .subseq_ctrl = {
1456 .cmd_executions = 8,
1457 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1458 .post_ssq_wait = ctrl->CAS,
1459 .data_direction = SSQ_NA,
1460 },
1461 .sp_cmd_addr = {
1462 .address = 0,
1463 .rowbits = 6,
1464 .bank = 0,
1465 .rank = slotrank,
1466 },
1467 .addr_update = {
1468 .inc_bank = 1,
1469 .addr_wrap = 18,
1470 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001471 },
1472 /* DRAM command RD */
1473 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001474 .sp_cmd_ctrl = {
1475 .command = IOSAV_RD,
1476 .ranksel_ap = 1,
1477 },
1478 .subseq_ctrl = {
1479 .cmd_executions = 500,
1480 .cmd_delay_gap = 4,
1481 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1482 .data_direction = SSQ_RD,
1483 },
1484 .sp_cmd_addr = {
1485 .address = 0,
1486 .rowbits = 0,
1487 .bank = 0,
1488 .rank = slotrank,
1489 },
1490 .addr_update = {
1491 .inc_addr_8 = 1,
1492 .addr_wrap = 18,
1493 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001494 },
1495 /* DRAM command PREA */
1496 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001497 .sp_cmd_ctrl = {
1498 .command = IOSAV_PRE,
1499 .ranksel_ap = 1,
1500 },
1501 .subseq_ctrl = {
1502 .cmd_executions = 1,
1503 .cmd_delay_gap = 3,
1504 .post_ssq_wait = ctrl->tRP,
1505 .data_direction = SSQ_NA,
1506 },
1507 .sp_cmd_addr = {
1508 .address = 1024,
1509 .rowbits = 6,
1510 .bank = 0,
1511 .rank = slotrank,
1512 },
1513 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01001514 .addr_wrap = 18,
Angel Pons3abd2062020-05-03 00:25:02 +02001515 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001516 },
1517 };
1518 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001519
Angel Pons7c49cb82020-03-16 23:17:32 +01001520 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001521 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001522
Angel Pons88521882020-01-05 20:21:20 +01001523 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001524}
1525
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001526static void timC_threshold_process(int *data, const int count)
1527{
1528 int min = data[0];
1529 int max = min;
1530 int i;
1531 for (i = 1; i < count; i++) {
1532 if (min > data[i])
1533 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001534
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001535 if (max < data[i])
1536 max = data[i];
1537 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001538 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001539 for (i = 0; i < count; i++)
1540 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001541
Angel Pons891f2bc2020-01-10 01:27:28 +01001542 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001543}
1544
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001545static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
1546{
1547 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01001548 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001549 int lane;
1550
Angel Pons88521882020-01-05 20:21:20 +01001551 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001552
Angel Ponsffd50152020-11-12 11:03:10 +01001553 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001554
Angel Pons7c49cb82020-03-16 23:17:32 +01001555 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001556 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557
1558 for (timC = 0; timC <= MAX_TIMC; timC++) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001559 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001560 program_timings(ctrl, channel);
1561
1562 test_timC(ctrl, channel, slotrank);
1563
1564 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001565 stats[lane][timC] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001566 }
1567 }
1568 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001569 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1570
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001571 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001572 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1573 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001574 /*
1575 * With command training not being done yet, the lane can be erroneous.
1576 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001577 */
Angel Pons7c49cb82020-03-16 23:17:32 +01001578 timC_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
1579 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1580
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001581 if (rn.all || rn.length < 8) {
1582 printk(BIOS_EMERG, "timC recovery failed\n");
1583 return MAKE_ERR;
1584 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001585 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001586 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001587 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001588 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001589 }
1590 return 0;
1591}
1592
Angel Pons88521882020-01-05 20:21:20 +01001593static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001594{
1595 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001596
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001597 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1598 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001599
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001600 return ret;
1601}
1602
Angel Pons765d4652020-11-11 14:44:35 +01001603/* Each cacheline is 64 bits long */
1604static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1605{
1606 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1607}
1608
Angel Pons88521882020-01-05 20:21:20 +01001609static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001610{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301611 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001612 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001613
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001614 for (j = 0; j < 16; j++)
1615 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001616
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001617 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001618
1619 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001620}
1621
Angel Pons88521882020-01-05 20:21:20 +01001622static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001623{
1624 int ret = 0;
1625 int channel;
1626 FOR_ALL_POPULATED_CHANNELS ret++;
1627 return ret;
1628}
1629
Angel Pons88521882020-01-05 20:21:20 +01001630static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001631{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301632 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001633 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301634 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001635
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001636 for (j = 0; j < 16; j++)
1637 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001638
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001639 for (j = 0; j < 16; j++)
1640 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001641
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001642 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001643
1644 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001645}
1646
Angel Pons88521882020-01-05 20:21:20 +01001647static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001648{
1649 int channel, slotrank, lane;
1650
1651 FOR_ALL_POPULATED_CHANNELS {
1652 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001653 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1654 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001655 }
1656
1657 program_timings(ctrl, channel);
1658
1659 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001660 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001661
Angel Ponsffd50152020-11-12 11:03:10 +01001662 iosav_write_read_mpr_sequence(
1663 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001664
Angel Pons7c49cb82020-03-16 23:17:32 +01001665 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001666 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001667
Angel Pons88521882020-01-05 20:21:20 +01001668 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001669 }
1670
1671 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001672 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1673 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001674 }
1675
1676 program_timings(ctrl, channel);
1677
1678 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001679 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001680
Angel Ponsffd50152020-11-12 11:03:10 +01001681 iosav_write_read_mpr_sequence(
1682 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001683
Angel Pons7c49cb82020-03-16 23:17:32 +01001684 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001685 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001686
Angel Pons88521882020-01-05 20:21:20 +01001687 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001688 }
1689 }
1690}
1691
Angel Pons88521882020-01-05 20:21:20 +01001692static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001693{
1694 /* enable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001695 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel) | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001696
Angel Pons88521882020-01-05 20:21:20 +01001697 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001698
1699 const struct iosav_ssq sequence[] = {
1700 /* DRAM command NOP */
1701 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001702 .sp_cmd_ctrl = {
1703 .command = IOSAV_NOP,
1704 .ranksel_ap = 1,
1705 },
1706 .subseq_ctrl = {
1707 .cmd_executions = 1,
1708 .cmd_delay_gap = 3,
1709 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1710 .data_direction = SSQ_WR,
1711 },
1712 .sp_cmd_addr = {
1713 .address = 8,
1714 .rowbits = 0,
1715 .bank = 0,
1716 .rank = slotrank,
1717 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001718 },
1719 /* DRAM command NOP */
1720 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001721 .sp_cmd_ctrl = {
1722 .command = IOSAV_NOP_ALT,
1723 .ranksel_ap = 1,
1724 },
1725 .subseq_ctrl = {
1726 .cmd_executions = 1,
1727 .cmd_delay_gap = 3,
1728 .post_ssq_wait = ctrl->CAS + 38,
1729 .data_direction = SSQ_RD,
1730 },
1731 .sp_cmd_addr = {
1732 .address = 4,
1733 .rowbits = 0,
1734 .bank = 0,
1735 .rank = slotrank,
1736 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001737 },
1738 };
1739 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001740
Angel Pons7c49cb82020-03-16 23:17:32 +01001741 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001742 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001743
Angel Pons88521882020-01-05 20:21:20 +01001744 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001745
1746 /* disable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001747 write_mrreg(ctrl, channel, slotrank, 1,
1748 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001749}
1750
1751static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1752{
1753 int timB;
1754 int statistics[NUM_LANES][128];
1755 int lane;
1756
Angel Pons88521882020-01-05 20:21:20 +01001757 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001758
1759 for (timB = 0; timB < 128; timB++) {
1760 FOR_ALL_LANES {
1761 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1762 }
1763 program_timings(ctrl, channel);
1764
1765 test_timB(ctrl, channel, slotrank);
1766
1767 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001768 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1769 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1770 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001771 }
1772 }
1773 FOR_ALL_LANES {
1774 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001775 /*
1776 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1777 * of timB by a small value, which might cause the 6-bit value to overflow if
1778 * it's close to 0x3f. Increment the value by a small offset if it's likely
1779 * to overflow, to make sure it won't overflow while running tests and bricks
1780 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001781 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001782 * TODO: find out why some tests (edge write discovery) increment timB.
1783 */
1784 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001785 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001786 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001787 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001788
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001789 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1790 if (rn.all) {
1791 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1792 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001793
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001794 return MAKE_ERR;
1795 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001796 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1797 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001798 }
1799 return 0;
1800}
1801
1802static int get_timB_high_adjust(u64 val)
1803{
1804 int i;
1805
Angel Ponsbf13ef02020-11-11 18:40:06 +01001806 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001807 if (val == 0xffffffffffffffffLL)
1808 return 0;
1809
1810 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001811 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001812 for (i = 0; i < 8; i++)
1813 if (val << (8 * (7 - i) + 4))
1814 return -i;
1815 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001816 /* DQS is early, needs positive 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 }
1821 return 8;
1822}
1823
Angel Ponsbf13ef02020-11-11 18:40:06 +01001824static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001825{
1826 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001827 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001828 FOR_ALL_POPULATED_CHANNELS {
1829 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001830 }
1831 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1832
Angel Pons765d4652020-11-11 14:44:35 +01001833 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001834 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001835
Angel Pons88521882020-01-05 20:21:20 +01001836 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001837
Angel Ponsffd50152020-11-12 11:03:10 +01001838 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001839
Angel Pons7c49cb82020-03-16 23:17:32 +01001840 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001841 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001842
Angel Pons88521882020-01-05 20:21:20 +01001843 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001844
Angel Pons8f0757e2020-11-11 23:03:36 +01001845 const struct iosav_ssq rd_sequence[] = {
1846 /* DRAM command PREA */
1847 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001848 .sp_cmd_ctrl = {
1849 .command = IOSAV_PRE,
1850 .ranksel_ap = 1,
1851 },
1852 .subseq_ctrl = {
1853 .cmd_executions = 1,
1854 .cmd_delay_gap = 3,
1855 .post_ssq_wait = ctrl->tRP,
1856 .data_direction = SSQ_NA,
1857 },
1858 .sp_cmd_addr = {
1859 .address = 1024,
1860 .rowbits = 6,
1861 .bank = 0,
1862 .rank = slotrank,
1863 },
1864 .addr_update = {
1865 .addr_wrap = 18,
1866 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001867 },
1868 /* DRAM command ACT */
1869 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001870 .sp_cmd_ctrl = {
1871 .command = IOSAV_ACT,
1872 .ranksel_ap = 1,
1873 },
1874 .subseq_ctrl = {
1875 .cmd_executions = 1,
1876 .cmd_delay_gap = 3,
1877 .post_ssq_wait = ctrl->tRCD,
1878 .data_direction = SSQ_NA,
1879 },
1880 .sp_cmd_addr = {
1881 .address = 0,
1882 .rowbits = 6,
1883 .bank = 0,
1884 .rank = slotrank,
1885 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001886 },
1887 /* DRAM command RD */
1888 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001889 .sp_cmd_ctrl = {
1890 .command = IOSAV_RD,
1891 .ranksel_ap = 3,
1892 },
1893 .subseq_ctrl = {
1894 .cmd_executions = 1,
1895 .cmd_delay_gap = 3,
1896 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001897 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001898 ctrl->timings[channel][slotrank].io_latency,
1899 .data_direction = SSQ_RD,
1900 },
1901 .sp_cmd_addr = {
1902 .address = 8,
1903 .rowbits = 6,
1904 .bank = 0,
1905 .rank = slotrank,
1906 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001907 },
1908 };
1909 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001910
Angel Pons7c49cb82020-03-16 23:17:32 +01001911 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001912 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001913
Angel Pons88521882020-01-05 20:21:20 +01001914 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001915 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001916 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001917 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001918 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001919 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1920 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1921 get_timB_high_adjust(res) * 64;
1922
1923 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001924 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1925 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001926 }
1927 }
Angel Pons88521882020-01-05 20:21:20 +01001928 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001929}
1930
Angel Pons88521882020-01-05 20:21:20 +01001931static void write_op(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001932{
1933 int slotrank;
1934
Angel Pons88521882020-01-05 20:21:20 +01001935 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001936
1937 /* choose an existing rank. */
1938 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
1939
Angel Ponsffd50152020-11-12 11:03:10 +01001940 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001941
Angel Pons7c49cb82020-03-16 23:17:32 +01001942 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001943 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001944
Angel Pons88521882020-01-05 20:21:20 +01001945 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001946}
1947
Angel Pons7c49cb82020-03-16 23:17:32 +01001948/*
1949 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001950 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001951 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1952 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1953 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1954 * CLK/ADDR/CMD signals have the same routing delay.
1955 *
1956 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1957 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1958 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001959 */
Angel Pons88521882020-01-05 20:21:20 +01001960int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001961{
1962 int channel, slotrank, lane;
1963 int err;
1964
1965 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01001966 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001967
1968 FOR_ALL_POPULATED_CHANNELS {
1969 write_op(ctrl, channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01001970 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001971 }
1972
Angel Pons7c49cb82020-03-16 23:17:32 +01001973 /* Refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001974 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001975 FOR_ALL_POPULATED_CHANNELS {
1976 write_op(ctrl, channel);
1977 }
1978
Angel Pons7c49cb82020-03-16 23:17:32 +01001979 /* Enable write leveling on all ranks
1980 Disable all DQ outputs
1981 Only NOP is allowed in this mode */
1982 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1983 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001984 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985
Angel Pons88521882020-01-05 20:21:20 +01001986 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001987
1988 toggle_io_reset();
1989
Angel Pons7c49cb82020-03-16 23:17:32 +01001990 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001991 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1992 err = discover_timB(ctrl, channel, slotrank);
1993 if (err)
1994 return err;
1995 }
1996
Angel Pons7c49cb82020-03-16 23:17:32 +01001997 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001998 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001999 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002000
Angel Pons88521882020-01-05 20:21:20 +01002001 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002002
2003 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002004 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002005
Angel Pons7c49cb82020-03-16 23:17:32 +01002006 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002007 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002008
2009 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002010 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01002011 MCHBAR32(IOSAV_STATUS_ch(channel));
2012 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002013
Angel Ponsffd50152020-11-12 11:03:10 +01002014 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002015
Angel Pons7c49cb82020-03-16 23:17:32 +01002016 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002017 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002018
Angel Pons88521882020-01-05 20:21:20 +01002019 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002020 }
2021
2022 toggle_io_reset();
2023
2024 printram("CPE\n");
2025 precharge(ctrl);
2026 printram("CPF\n");
2027
2028 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002029 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002030 }
2031
2032 FOR_ALL_POPULATED_CHANNELS {
2033 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002034 }
2035
2036 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2037 err = discover_timC(ctrl, channel, slotrank);
2038 if (err)
2039 return err;
2040 }
2041
2042 FOR_ALL_POPULATED_CHANNELS
2043 program_timings(ctrl, channel);
2044
2045 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002046 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002047
2048 FOR_ALL_POPULATED_CHANNELS
2049 program_timings(ctrl, channel);
2050
2051 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002052 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002053 }
2054 return 0;
2055}
2056
Angel Ponsbf13ef02020-11-11 18:40:06 +01002057static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002058{
2059 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2060 int timC_delta;
2061 int lanes_ok = 0;
2062 int ctr = 0;
2063 int lane;
2064
2065 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2066 FOR_ALL_LANES {
2067 ctrl->timings[channel][slotrank].lanes[lane].timC =
2068 saved_rt.lanes[lane].timC + timC_delta;
2069 }
2070 program_timings(ctrl, channel);
2071 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002072 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002073 }
2074
Angel Pons765d4652020-11-11 14:44:35 +01002075 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002076 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002077
Angel Pons88521882020-01-05 20:21:20 +01002078 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002079
Angel Ponsffd50152020-11-12 11:03:10 +01002080 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002081
2082 /* Program LFSR for the RD/WR subsequences */
2083 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2084 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002085
Angel Pons7c49cb82020-03-16 23:17:32 +01002086 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002087 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002088
Angel Pons88521882020-01-05 20:21:20 +01002089 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002090 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002091 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002092
2093 if (r32 == 0)
2094 lanes_ok |= 1 << lane;
2095 }
2096 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002097 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002098 break;
2099 }
2100
2101 ctrl->timings[channel][slotrank] = saved_rt;
2102
Patrick Rudolphdd662872017-10-28 18:20:11 +02002103 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002104}
2105
Angel Pons88521882020-01-05 20:21:20 +01002106static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002107{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302108 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002109 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2110 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111
2112 if (patno) {
2113 u8 base8 = 0x80 >> ((patno - 1) % 8);
2114 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2115 for (i = 0; i < 32; i++) {
2116 for (j = 0; j < 16; j++) {
2117 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002118
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002119 if (invert[patno - 1][i] & (1 << (j / 2)))
2120 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002121
2122 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002123 }
2124 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002125 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002126 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2127 for (j = 0; j < 16; j++) {
2128 const u32 val = pattern[i][j];
2129 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2130 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002131 }
2132 sfence();
2133 }
Angel Pons765d4652020-11-11 14:44:35 +01002134
2135 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002136}
2137
Angel Pons88521882020-01-05 20:21:20 +01002138static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002139{
2140 int channel, slotrank;
2141
2142 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002143 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002144
Angel Pons7c49cb82020-03-16 23:17:32 +01002145 /* Choose an existing rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2147
Angel Ponsffd50152020-11-12 11:03:10 +01002148 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149
Angel Pons7c49cb82020-03-16 23:17:32 +01002150 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002151 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002152
Angel Pons88521882020-01-05 20:21:20 +01002153 wait_for_iosav(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002154 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002155 }
2156
2157 /* refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002158 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002160 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002161
2162 /* choose an existing rank. */
2163 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2164
Angel Ponsffd50152020-11-12 11:03:10 +01002165 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002166
Angel Pons7c49cb82020-03-16 23:17:32 +01002167 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002168 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002169
Angel Pons88521882020-01-05 20:21:20 +01002170 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002171 }
2172
Angel Pons7c49cb82020-03-16 23:17:32 +01002173 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002174 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002175
2176 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002177 dram_mrscommands(ctrl);
2178
2179 toggle_io_reset();
2180}
2181
Angel Ponsbf13ef02020-11-11 18:40:06 +01002182#define CT_MIN_PI -127
2183#define CT_MAX_PI 128
2184#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2185
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002186#define MIN_C320C_LEN 13
2187
2188static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2189{
2190 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2191 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002192 int command_pi;
2193 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002194 int delta = 0;
2195
2196 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2197
2198 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002199 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002200 }
2201
2202 ctrl->cmd_stretch[channel] = cmd_stretch;
2203
Angel Pons88521882020-01-05 20:21:20 +01002204 MCHBAR32(TC_RAP_ch(channel)) =
Angel Pons7c49cb82020-03-16 23:17:32 +01002205 (ctrl->tRRD << 0)
2206 | (ctrl->tRTP << 4)
2207 | (ctrl->tCKE << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002208 | (ctrl->tWTR << 12)
2209 | (ctrl->tFAW << 16)
Angel Pons7c49cb82020-03-16 23:17:32 +01002210 | (ctrl->tWR << 24)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002211 | (ctrl->cmd_stretch[channel] << 30);
2212
2213 if (ctrl->cmd_stretch[channel] == 2)
2214 delta = 2;
2215 else if (ctrl->cmd_stretch[channel] == 0)
2216 delta = 4;
2217
2218 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002219 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002220 }
2221
Angel Ponsbf13ef02020-11-11 18:40:06 +01002222 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002223 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002224 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002225 }
2226 program_timings(ctrl, channel);
2227 reprogram_320c(ctrl);
2228 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002229 stat[slotrank][command_pi - CT_MIN_PI] =
2230 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002231 }
2232 }
2233 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002234 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002235
Angel Ponsbf13ef02020-11-11 18:40:06 +01002236 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002237 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2238 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002239
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002240 if (rn.all || rn.length < MIN_C320C_LEN) {
2241 FOR_ALL_POPULATED_RANKS {
2242 ctrl->timings[channel][slotrank] =
2243 saved_timings[channel][slotrank];
2244 }
2245 return MAKE_ERR;
2246 }
2247 }
2248
2249 return 0;
2250}
2251
Angel Pons7c49cb82020-03-16 23:17:32 +01002252/*
2253 * Adjust CMD phase shift and try multiple command rates.
2254 * A command rate of 2T doubles the time needed for address and command decode.
2255 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002256int command_training(ramctr_timing *ctrl)
2257{
2258 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002259
2260 FOR_ALL_POPULATED_CHANNELS {
2261 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002262 }
2263
2264 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002265 int cmdrate, err;
2266
2267 /*
2268 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002269 * Issue:
2270 * While c320c discovery seems to succeed raminit will fail in write training.
2271 *
2272 * Workaround:
2273 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2274 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002275 *
2276 * Single DIMM per channel:
2277 * Try command rate 1T and 2T
2278 */
2279 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002280 if (ctrl->tCMD)
2281 /* XMP gives the CMD rate in clock ticks, not ns */
2282 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002283
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002284 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002285 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2286
2287 if (!err)
2288 break;
2289 }
2290
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002291 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002292 printk(BIOS_EMERG, "c320c discovery failed\n");
2293 return err;
2294 }
2295
Angel Pons891f2bc2020-01-10 01:27:28 +01002296 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002297 }
2298
2299 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002300 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002301
2302 reprogram_320c(ctrl);
2303 return 0;
2304}
2305
Angel Pons891f2bc2020-01-10 01:27:28 +01002306static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307{
2308 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002309 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002310 int lane;
2311
2312 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2313 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002314 ctrl->timings[channel][slotrank].lanes[lane].rising = edge;
Angel Pons891f2bc2020-01-10 01:27:28 +01002315 ctrl->timings[channel][slotrank].lanes[lane].falling = edge;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002316 }
2317 program_timings(ctrl, channel);
2318
2319 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002320 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2321 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002322 }
2323
Angel Pons88521882020-01-05 20:21:20 +01002324 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002325
Angel Ponsffd50152020-11-12 11:03:10 +01002326 iosav_write_read_mpr_sequence(
2327 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002328
Angel Pons7c49cb82020-03-16 23:17:32 +01002329 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002330 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002331
Angel Pons88521882020-01-05 20:21:20 +01002332 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002333
2334 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002335 stats[lane][edge] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336 }
2337 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002338
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002339 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002340 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002341 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002342
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002343 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002344 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2345 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002346 return MAKE_ERR;
2347 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002348 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002349 }
2350 return 0;
2351}
2352
Angel Pons60971dc2020-11-14 00:49:38 +01002353static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2354{
2355 int slotrank, lane;
2356
2357 fill_pattern0(ctrl, channel, 0, 0);
2358 FOR_ALL_LANES {
2359 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2360 }
2361
2362 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2363 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2364 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2365 }
2366
2367 program_timings(ctrl, channel);
2368
2369 FOR_ALL_POPULATED_RANKS {
2370 wait_for_iosav(channel);
2371
2372 iosav_write_read_mpr_sequence(
2373 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2374
2375 /* Execute command queue */
2376 iosav_run_once(channel);
2377
2378 wait_for_iosav(channel);
2379 }
2380
2381 /* XXX: check any measured value ? */
2382
2383 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2384 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2385 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2386 }
2387
2388 program_timings(ctrl, channel);
2389
2390 FOR_ALL_POPULATED_RANKS {
2391 wait_for_iosav(channel);
2392
2393 iosav_write_read_mpr_sequence(
2394 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2395
2396 /* Execute command queue */
2397 iosav_run_once(channel);
2398
2399 wait_for_iosav(channel);
2400 }
2401
2402 /* XXX: check any measured value ? */
2403
2404 FOR_ALL_LANES {
2405 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2406 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2407 }
2408}
2409
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002410int discover_edges(ramctr_timing *ctrl)
2411{
2412 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2413 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2414 int channel, slotrank, lane;
2415 int err;
2416
Angel Pons88521882020-01-05 20:21:20 +01002417 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002418
2419 toggle_io_reset();
2420
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002421 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002422 FOR_ALL_LANES {
Angel Pons60971dc2020-11-14 00:49:38 +01002423 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002424 }
2425
Angel Pons60971dc2020-11-14 00:49:38 +01002426 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002427
2428 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002429 }
2430
Angel Pons0c3936e2020-03-22 12:49:27 +01002431 /*
2432 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2433 * also use a single loop. It would seem that it is a debugging configuration.
2434 */
Angel Pons88521882020-01-05 20:21:20 +01002435 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2436 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002437
2438 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2439 err = discover_edges_real(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002440 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002441 if (err)
2442 return err;
2443 }
2444
Angel Pons88521882020-01-05 20:21:20 +01002445 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2446 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002447
2448 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2449 err = discover_edges_real(ctrl, channel, slotrank,
2450 rising_edges[channel][slotrank]);
2451 if (err)
2452 return err;
2453 }
2454
Angel Pons88521882020-01-05 20:21:20 +01002455 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002456
2457 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2458 ctrl->timings[channel][slotrank].lanes[lane].falling =
2459 falling_edges[channel][slotrank][lane];
2460 ctrl->timings[channel][slotrank].lanes[lane].rising =
2461 rising_edges[channel][slotrank][lane];
2462 }
2463
2464 FOR_ALL_POPULATED_CHANNELS {
2465 program_timings(ctrl, channel);
2466 }
2467
2468 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002469 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470 }
2471 return 0;
2472}
2473
Angel Pons7c49cb82020-03-16 23:17:32 +01002474static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002475{
2476 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002477 u32 raw_stats[MAX_EDGE_TIMING + 1];
2478 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002479 const int reg3000b24[] = { 0, 0xc, 0x2c };
2480 int lane, i;
2481 int lower[NUM_LANES];
2482 int upper[NUM_LANES];
2483 int pat;
2484
2485 FOR_ALL_LANES {
2486 lower[lane] = 0;
2487 upper[lane] = MAX_EDGE_TIMING;
2488 }
2489
2490 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002491 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002492 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2493
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002494 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2495 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002496 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002497
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002498 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2499 FOR_ALL_LANES {
2500 ctrl->timings[channel][slotrank].lanes[lane].
2501 rising = edge;
2502 ctrl->timings[channel][slotrank].lanes[lane].
2503 falling = edge;
2504 }
2505 program_timings(ctrl, channel);
2506
2507 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002508 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2509 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002510 }
Angel Pons88521882020-01-05 20:21:20 +01002511 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002512
Angel Ponsffd50152020-11-12 11:03:10 +01002513 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002514
Angel Pons7c49cb82020-03-16 23:17:32 +01002515 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002516 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002517
Angel Pons88521882020-01-05 20:21:20 +01002518 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002520 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002521 }
2522
Angel Pons7c49cb82020-03-16 23:17:32 +01002523 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002524 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002525 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002526
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002527 FOR_ALL_LANES {
2528 struct run rn;
2529 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002530 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2531
2532 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2533
2534 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2535 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2536 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002537 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002538
2539 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2540 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2541
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002542 edges[lane] = (lower[lane] + upper[lane]) / 2;
2543 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002544 printk(BIOS_EMERG, "edge write discovery failed: "
2545 "%d, %d, %d\n", channel, slotrank, lane);
2546
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002547 return MAKE_ERR;
2548 }
2549 }
2550 }
2551 }
2552
Angel Pons88521882020-01-05 20:21:20 +01002553 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002554 printram("CPA\n");
2555 return 0;
2556}
2557
2558int discover_edges_write(ramctr_timing *ctrl)
2559{
2560 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002561 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2562 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002563
Angel Pons7c49cb82020-03-16 23:17:32 +01002564 /*
2565 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2566 * also use a single loop. It would seem that it is a debugging configuration.
2567 */
Angel Pons88521882020-01-05 20:21:20 +01002568 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2569 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002570
2571 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2572 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002573 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002574 if (err)
2575 return err;
2576 }
2577
Angel Pons88521882020-01-05 20:21:20 +01002578 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2579 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002580
2581 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2582 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002583 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002584 if (err)
2585 return err;
2586 }
2587
Angel Pons88521882020-01-05 20:21:20 +01002588 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002589
2590 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2591 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002592 falling_edges[channel][slotrank][lane];
2593
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002594 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002595 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002596 }
2597
2598 FOR_ALL_POPULATED_CHANNELS
2599 program_timings(ctrl, channel);
2600
2601 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002602 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002603 }
2604 return 0;
2605}
2606
2607static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2608{
Angel Pons88521882020-01-05 20:21:20 +01002609 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002610
Angel Ponsffd50152020-11-12 11:03:10 +01002611 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002612
Angel Pons7c49cb82020-03-16 23:17:32 +01002613 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002614 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002615
Angel Pons88521882020-01-05 20:21:20 +01002616 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002617}
2618
2619int discover_timC_write(ramctr_timing *ctrl)
2620{
Angel Pons7c49cb82020-03-16 23:17:32 +01002621 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002622 int i, pat;
2623
2624 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2625 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2626 int channel, slotrank, lane;
2627
2628 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2629 lower[channel][slotrank][lane] = 0;
2630 upper[channel][slotrank][lane] = MAX_TIMC;
2631 }
2632
Angel Pons88521882020-01-05 20:21:20 +01002633 /*
2634 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2635 * FIXME: This must only be done on Ivy Bridge.
2636 */
2637 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002638 printram("discover timC write:\n");
2639
2640 for (i = 0; i < 3; i++)
2641 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002642
2643 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2644 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2645 ~0x3f000000, rege3c_b24[i] << 24);
2646
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002647 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002648
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002649 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2650 FOR_ALL_POPULATED_RANKS {
2651 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002652 u32 raw_stats[MAX_TIMC + 1];
2653 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002654
2655 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002656 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002657
2658 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002659
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002660 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002661 FOR_ALL_LANES {
2662 ctrl->timings[channel][slotrank]
2663 .lanes[lane].timC = timC;
2664 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002665 program_timings(ctrl, channel);
2666
2667 test_timC_write (ctrl, channel, slotrank);
2668
Angel Pons7c49cb82020-03-16 23:17:32 +01002669 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002670 raw_stats[timC] = MCHBAR32(
2671 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002672 }
2673 FOR_ALL_LANES {
2674 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002675 for (timC = 0; timC < MAX_TIMC; timC++) {
2676 stats[timC] = !!(raw_stats[timC]
2677 & (1 << lane));
2678 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002679
Angel Pons7c49cb82020-03-16 23:17:32 +01002680 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002681 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002682 printk(BIOS_EMERG,
2683 "timC write discovery failed: "
2684 "%d, %d, %d\n", channel,
2685 slotrank, lane);
2686
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002687 return MAKE_ERR;
2688 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002689 printram("timC: %d, %d, %d: "
2690 "0x%02x-0x%02x-0x%02x, "
2691 "0x%02x-0x%02x\n", channel, slotrank,
2692 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002693 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002694 rn.end - ctrl->timC_offset[i]);
2695
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002696 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002697 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002698 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002699
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002700 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002701 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002702 upper[channel][slotrank][lane]);
2703
2704 }
2705 }
2706 }
2707 }
2708
2709 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002710 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002711 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002712 udelay(2);
2713 }
2714
Angel Pons88521882020-01-05 20:21:20 +01002715 /*
2716 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2717 * FIXME: This must only be done on Ivy Bridge.
2718 */
2719 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002720
2721 printram("CPB\n");
2722
2723 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002724 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002725 (lower[channel][slotrank][lane] +
2726 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002727
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002728 ctrl->timings[channel][slotrank].lanes[lane].timC =
2729 (lower[channel][slotrank][lane] +
2730 upper[channel][slotrank][lane]) / 2;
2731 }
2732 FOR_ALL_POPULATED_CHANNELS {
2733 program_timings(ctrl, channel);
2734 }
2735 return 0;
2736}
2737
Angel Pons88521882020-01-05 20:21:20 +01002738void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002739{
2740 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002741 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002742
2743 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2744 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002745 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002746 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002747 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002748 printram("normalize %d, %d, %d: mat %d\n",
2749 channel, slotrank, lane, mat);
2750
Felix Heldef4fe3e2019-12-31 14:15:05 +01002751 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002752 printram("normalize %d, %d, %d: delta %d\n",
2753 channel, slotrank, lane, delta);
2754
Angel Pons88521882020-01-05 20:21:20 +01002755 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002756 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002757 }
2758
2759 FOR_ALL_POPULATED_CHANNELS {
2760 program_timings(ctrl, channel);
2761 }
2762}
2763
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002764int channel_test(ramctr_timing *ctrl)
2765{
2766 int channel, slotrank, lane;
2767
2768 slotrank = 0;
2769 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002770 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002771 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002772 return MAKE_ERR;
2773 }
2774 FOR_ALL_POPULATED_CHANNELS {
2775 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002776 }
2777
2778 for (slotrank = 0; slotrank < 4; slotrank++)
2779 FOR_ALL_CHANNELS
2780 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2781 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002782 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2783 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002784 }
Angel Pons88521882020-01-05 20:21:20 +01002785 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002786
Angel Ponsffd50152020-11-12 11:03:10 +01002787 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002788
Angel Pons7c49cb82020-03-16 23:17:32 +01002789 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002790 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002791
Angel Pons88521882020-01-05 20:21:20 +01002792 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002793 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002794 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002795 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2796 channel, slotrank, lane);
2797 return MAKE_ERR;
2798 }
2799 }
2800 return 0;
2801}
2802
Patrick Rudolphdd662872017-10-28 18:20:11 +02002803void channel_scrub(ramctr_timing *ctrl)
2804{
2805 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002806 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002807
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002808 FOR_ALL_POPULATED_CHANNELS {
2809 wait_for_iosav(channel);
2810 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002811 }
2812
2813 /*
2814 * During runtime the "scrubber" will periodically scan through the memory in the
2815 * physical address space, to identify and fix CRC errors.
2816 * The following loops writes to every DRAM address, setting the ECC bits to the
2817 * correct value. A read from this location will no longer return a CRC error,
2818 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002819 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002820 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2821 * and firmware running in x86_32.
2822 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002823 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2824 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002825 for (bank = 0; bank < 8; bank++) {
2826 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002827
Angel Pons8f0757e2020-11-11 23:03:36 +01002828 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2829 const struct iosav_ssq sequence[] = {
2830 /*
2831 * DRAM command ACT
2832 * Opens the row for writing.
2833 */
2834 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002835 .sp_cmd_ctrl = {
2836 .command = IOSAV_ACT,
2837 .ranksel_ap = 1,
2838 },
2839 .subseq_ctrl = {
2840 .cmd_executions = 1,
2841 .cmd_delay_gap = gap,
2842 .post_ssq_wait = ctrl->tRCD,
2843 .data_direction = SSQ_NA,
2844 },
2845 .sp_cmd_addr = {
2846 .address = row,
2847 .rowbits = 6,
2848 .bank = bank,
2849 .rank = slotrank,
2850 },
2851 .addr_update = {
2852 .inc_addr_1 = 1,
2853 .addr_wrap = 18,
2854 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002855 },
2856 /*
2857 * DRAM command WR
2858 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2859 * bytes.
2860 */
2861 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002862 .sp_cmd_ctrl = {
2863 .command = IOSAV_WR,
2864 .ranksel_ap = 1,
2865 },
2866 .subseq_ctrl = {
2867 .cmd_executions = 129,
2868 .cmd_delay_gap = 4,
2869 .post_ssq_wait = ctrl->tWTR +
2870 ctrl->CWL + 8,
2871 .data_direction = SSQ_WR,
2872 },
2873 .sp_cmd_addr = {
2874 .address = row,
2875 .rowbits = 0,
2876 .bank = bank,
2877 .rank = slotrank,
2878 },
2879 .addr_update = {
2880 .inc_addr_8 = 1,
2881 .addr_wrap = 9,
2882 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002883 },
2884 /*
2885 * DRAM command PRE
2886 * Closes the row.
2887 */
2888 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002889 .sp_cmd_ctrl = {
2890 .command = IOSAV_PRE,
2891 .ranksel_ap = 1,
2892 },
2893 .subseq_ctrl = {
2894 .cmd_executions = 1,
2895 .cmd_delay_gap = 4,
2896 .post_ssq_wait = ctrl->tRP,
2897 .data_direction = SSQ_NA,
2898 },
2899 .sp_cmd_addr = {
2900 .address = 0,
2901 .rowbits = 6,
2902 .bank = bank,
2903 .rank = slotrank,
2904 },
2905 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002906 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002907 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002908 },
2909 };
2910 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002911
2912 /* Execute command queue */
2913 iosav_run_queue(channel, 16, 0);
2914
2915 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002916 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002917 }
2918 }
2919}
2920
Angel Pons88521882020-01-05 20:21:20 +01002921void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002922{
2923 int channel;
2924
Angel Pons7c49cb82020-03-16 23:17:32 +01002925 /* 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 +01002926 static u32 seeds[NUM_CHANNELS][3] = {
2927 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2928 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2929 };
2930 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002931 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002932 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2933 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2934 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002935 }
2936}
2937
Angel Pons89ae6b82020-03-21 13:23:32 +01002938void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002939{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002940 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002941 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002942 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002943 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002944 }
2945}
2946
Angel Pons88521882020-01-05 20:21:20 +01002947void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002948{
2949 int channel;
2950
2951 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002952 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002953 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002954 }
2955
2956 udelay(1);
2957
2958 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002959 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002960 }
2961}
2962
Angel Pons7c49cb82020-03-16 23:17:32 +01002963void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002964{
2965 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002966
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002967 FOR_ALL_POPULATED_CHANNELS {
2968 u32 b20, b4_8_12;
Angel Pons88521882020-01-05 20:21:20 +01002969 int min_pi = 10000;
2970 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002971
2972 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002973 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2974 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002975 }
2976
Angel Pons7c49cb82020-03-16 23:17:32 +01002977 b20 = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002978
Angel Pons7c49cb82020-03-16 23:17:32 +01002979 b4_8_12 = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 0x3330 : 0x2220;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002980
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002981 dram_odt_stretch(ctrl, channel);
2982
Angel Ponsdc5539f2020-11-12 12:44:25 +01002983 MCHBAR32(TC_RWP_ch(channel)) = (1 << 27) | (2 << 24) | (b20 << 20) |
Felix Held2463aa92018-07-29 21:37:55 +02002984 ((ctrl->ref_card_offset[channel] + 2) << 16) | b4_8_12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002985 }
2986}
2987
Angel Pons88521882020-01-05 20:21:20 +01002988void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002989{
2990 int channel;
2991 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002992 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2993 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002994 }
2995}
2996
Angel Pons7c49cb82020-03-16 23:17:32 +01002997/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2998static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002999{
Angel Pons88521882020-01-05 20:21:20 +01003000 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003001}
3002
Angel Pons7c49cb82020-03-16 23:17:32 +01003003/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01003004void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003005{
Angel Ponsb50ca572020-11-11 19:07:20 +01003006 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01003007
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003008 int channel;
3009 int t1_cycles = 0, t1_ns = 0, t2_ns;
3010 int t3_ns;
3011 u32 r32;
3012
Angel Pons7c49cb82020-03-16 23:17:32 +01003013 /* FIXME: This register only exists on Ivy Bridge */
3014 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003015
Felix Heldf9b826a2018-07-30 17:56:52 +02003016 FOR_ALL_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003017 MCHBAR32_AND_OR(TC_OTHP_ch(channel), ~(3 << 12), 1 << 12);
Patrick Rudolph652c4912017-10-31 11:36:55 +01003018
Patrick Rudolph74203de2017-11-20 11:57:01 +01003019 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003020 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003021 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003022 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003023 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003024 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003025
Felix Heldf9b826a2018-07-30 17:56:52 +02003026 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003027 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003028
Angel Pons88521882020-01-05 20:21:20 +01003029 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3030 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003031
3032 FOR_ALL_CHANNELS {
3033 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003034 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003035 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003036 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003037 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003038 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003039 case 1:
3040 case 4:
3041 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003042 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003043 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003044 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003045 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003046 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003047 break;
3048 }
3049 }
3050
Felix Held50b7ed22019-12-30 20:41:54 +01003051 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003052 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003053 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003054
3055 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003056 MCHBAR32_AND_OR(TC_RFP_ch(channel), ~(3 << 16), 1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003057
Angel Ponsdc5539f2020-11-12 12:44:25 +01003058 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3059 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003060 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003061
Angel Pons7c49cb82020-03-16 23:17:32 +01003062 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003063 FOR_ALL_POPULATED_CHANNELS
3064 break;
3065
Angel Pons88521882020-01-05 20:21:20 +01003066 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3067 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003068 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003069 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003070 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003071 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003072 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003073 t1_ns += 500;
3074
Angel Pons88521882020-01-05 20:21:20 +01003075 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003076 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003077 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003078 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003079 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003080 t3_ns = 500;
3081 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003082
3083 /* The graphics driver will use these watermark values */
3084 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003085 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003086 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3087 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003088}
3089
Angel Pons88521882020-01-05 20:21:20 +01003090void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003091{
3092 int channel, slotrank, lane;
3093
Angel Pons7c49cb82020-03-16 23:17:32 +01003094 FOR_ALL_POPULATED_CHANNELS {
3095 MCHBAR32(TC_RAP_ch(channel)) =
3096 (ctrl->tRRD << 0)
3097 | (ctrl->tRTP << 4)
3098 | (ctrl->tCKE << 8)
3099 | (ctrl->tWTR << 12)
3100 | (ctrl->tFAW << 16)
3101 | (ctrl->tWR << 24)
3102 | (ctrl->cmd_stretch[channel] << 30);
3103 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003104
3105 udelay(1);
3106
3107 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003108 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003109 }
3110
3111 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003112 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003113 }
3114
3115 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003116 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003117
3118 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003119 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003120 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003121 }
3122
3123 printram("CPE\n");
3124
Angel Pons88521882020-01-05 20:21:20 +01003125 MCHBAR32(GDCRTRAININGMOD) = 0;
3126 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003127
3128 printram("CP5b\n");
3129
3130 FOR_ALL_POPULATED_CHANNELS {
3131 program_timings(ctrl, channel);
3132 }
3133
3134 u32 reg, addr;
3135
Angel Pons7c49cb82020-03-16 23:17:32 +01003136 /* Poll for RCOMP */
3137 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3138 ;
3139
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003140 do {
Angel Pons88521882020-01-05 20:21:20 +01003141 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003142 } while ((reg & 0x14) == 0);
3143
Angel Pons7c49cb82020-03-16 23:17:32 +01003144 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003145 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003146 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003147
Angel Pons7c49cb82020-03-16 23:17:32 +01003148 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003149 udelay(500);
3150
3151 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003152 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003153 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003154 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003155 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003156 MCHBAR32(addr) = reg;
3157
Angel Pons7c49cb82020-03-16 23:17:32 +01003158 /* Wait 10ns for ranks to settle */
3159 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003160
3161 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3162 MCHBAR32(addr) = reg;
3163
Angel Pons7c49cb82020-03-16 23:17:32 +01003164 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003165 write_reset(ctrl);
3166 }
3167
Angel Pons7c49cb82020-03-16 23:17:32 +01003168 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003169 dram_mrscommands(ctrl);
3170
3171 printram("CP5c\n");
3172
Angel Pons88521882020-01-05 20:21:20 +01003173 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003174
3175 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003176 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003177 udelay(2);
3178 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003179}