blob: a48a84f58b2749490371289732afadb4d813bc86 [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{
Angel Pons593f4ca2020-11-11 19:55:51 +0100145 u32 reg, 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 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100172 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100173 reg |= (ctrl->tXPDLL << 0);
174 reg |= (ctrl->tXP << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100175 reg |= (ctrl->tAONPD << 8);
176 reg |= 0xa0000;
Angel Pons593f4ca2020-11-11 19:55:51 +0100177 printram("OTHP [%x] = %x\n", TC_OTHP_ch(channel), reg);
178 MCHBAR32(TC_OTHP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100179
Angel Ponsca2f68a2020-03-22 13:15:12 +0100180 /* Debug parameters - only applies to Ivy Bridge */
181 if (IS_IVY_CPU(ctrl->cpu)) {
182 reg = 0;
183
184 /*
185 * If tXP and tXPDLL are very high, we need to increase them by one.
186 * This can only happen on Ivy Bridge, and when overclocking the RAM.
187 */
188 if (ctrl->tXP >= 8)
189 reg |= (1 << 12);
190
191 if (ctrl->tXPDLL >= 32)
192 reg |= (1 << 13);
193
194 MCHBAR32(TC_DTP_ch(channel)) = reg;
195 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100196
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100197 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100198
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100199 /*
Angel Pons7c49cb82020-03-16 23:17:32 +0100200 * TC-Refresh timing parameters:
201 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
202 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100203 */
204 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
205
Angel Pons7c49cb82020-03-16 23:17:32 +0100206 reg = ((ctrl->tREFI & 0xffff) << 0) |
207 ((ctrl->tRFC & 0x01ff) << 16) | (((val32 / 1024) & 0x7f) << 25);
208
Angel Pons88521882020-01-05 20:21:20 +0100209 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), reg);
210 MCHBAR32(TC_RFTP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100211
Angel Ponsdc5539f2020-11-12 12:44:25 +0100212 MCHBAR32_OR(TC_RFP_ch(channel), 0xff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100213
Angel Pons7c49cb82020-03-16 23:17:32 +0100214 /* Self-refresh timing parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100215 reg = 0;
216 val32 = tDLLK;
Angel Pons7c49cb82020-03-16 23:17:32 +0100217 reg = (reg & ~0x00000fff) | (val32 << 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100218 val32 = ctrl->tXSOffset;
Angel Pons7c49cb82020-03-16 23:17:32 +0100219 reg = (reg & ~0x0000f000) | (val32 << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100220 val32 = tDLLK - ctrl->tXSOffset;
Angel Pons7c49cb82020-03-16 23:17:32 +0100221 reg = (reg & ~0x03ff0000) | (val32 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100222 val32 = ctrl->tMOD - 8;
Angel Pons7c49cb82020-03-16 23:17:32 +0100223 reg = (reg & ~0xf0000000) | (val32 << 28);
224 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), reg);
Angel Pons88521882020-01-05 20:21:20 +0100225 MCHBAR32(TC_SRFTP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100226 }
227}
228
229void dram_dimm_mapping(ramctr_timing *ctrl)
230{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100231 int channel;
232 dimm_info *info = &ctrl->info;
233
234 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200235 dimm_attr *dimmA, *dimmB;
236 u32 reg = 0;
237
Angel Pons7c49cb82020-03-16 23:17:32 +0100238 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100239 dimmA = &info->dimm[channel][0];
240 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100241 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100242 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100243 dimmA = &info->dimm[channel][1];
244 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100245 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100246 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100247
Nico Huberac4f2162017-10-01 18:14:43 +0200248 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100249 reg |= (dimmA->size_mb / 256) << 0;
250 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200251 reg |= (dimmA->width / 8 - 1) << 19;
252 }
253
254 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100255 reg |= (dimmB->size_mb / 256) << 8;
256 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200257 reg |= (dimmB->width / 8 - 1) << 20;
258 }
259
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200260 /*
261 * Rank interleave: Bit 16 of the physical address space sets
262 * the rank to use in a dual single rank DIMM configuration.
263 * That results in every 64KiB being interleaved between two ranks.
264 */
265 reg |= 1 << 21;
266 /* Enhanced interleave */
267 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200268
Angel Pons7c49cb82020-03-16 23:17:32 +0100269 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100270 ctrl->mad_dimm[channel] = reg;
271 } else {
272 ctrl->mad_dimm[channel] = 0;
273 }
274 }
275}
276
Patrick Rudolphdd662872017-10-28 18:20:11 +0200277void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100278{
279 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200280 u32 ecc;
281
282 if (ctrl->ecc_enabled)
283 ecc = training ? (1 << 24) : (3 << 24);
284 else
285 ecc = 0;
286
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100287 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200288 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100289 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200290
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200291 if (ctrl->ecc_enabled)
292 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100293}
294
Angel Pons88521882020-01-05 20:21:20 +0100295void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100296{
297 u32 reg, ch0size, ch1size;
298 u8 val;
299 reg = 0;
300 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100301
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100302 if (training) {
303 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
304 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
305 } else {
306 ch0size = ctrl->channel_size_mb[0];
307 ch1size = ctrl->channel_size_mb[1];
308 }
309
310 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100311 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100312 val = ch1size / 256;
313 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100314 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100315 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100316 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100317
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100318 } else {
Angel Pons88521882020-01-05 20:21:20 +0100319 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100320 val = ch0size / 256;
321 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100322 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100323 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100324 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100325 }
326}
327
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100328#define DEFAULT_PCI_MMIO_SIZE 2048
329
330static unsigned int get_mmio_size(void)
331{
332 const struct device *dev;
333 const struct northbridge_intel_sandybridge_config *cfg = NULL;
334
Angel Ponsb31d1d72020-01-10 01:35:09 +0100335 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100336 if (dev)
337 cfg = dev->chip_info;
338
339 /* If this is zero, it just means devicetree.cb didn't set it */
340 if (!cfg || cfg->pci_mmio_size == 0)
341 return DEFAULT_PCI_MMIO_SIZE;
342 else
343 return cfg->pci_mmio_size;
344}
345
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200346/*
347 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
348 * The ME/PCU/.. has the ability to change this.
349 * Return 0: ECC is optional
350 * Return 1: ECC is forced
351 */
352bool get_host_ecc_forced(void)
353{
354 /* read Capabilities A Register */
355 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
356 return !!(reg32 & (1 << 24));
357}
358
359/*
360 * Returns the ECC capability.
361 * The ME/PCU/.. has the ability to change this.
362 * Return 0: ECC is disabled
363 * Return 1: ECC is possible
364 */
365bool get_host_ecc_cap(void)
366{
367 /* read Capabilities A Register */
368 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
369 return !(reg32 & (1 << 25));
370}
371
Angel Pons88521882020-01-05 20:21:20 +0100372void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100373{
Angel Pons7c49cb82020-03-16 23:17:32 +0100374 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
375 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
376 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100377 uint16_t ggc;
378
379 mmiosize = get_mmio_size();
380
Felix Held87ddea22020-01-26 04:55:27 +0100381 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100382 if (!(ggc & 2)) {
383 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100384 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100385 } else {
386 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100387 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100388 }
389
390 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
391
392 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
393
394 mestolenbase = tom - me_uma_size;
395
Angel Pons7c49cb82020-03-16 23:17:32 +0100396 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
397
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100398 gfxstolenbase = toludbase - gfxstolen;
399 gttbase = gfxstolenbase - gttsize;
400
401 tsegbase = gttbase - tsegsize;
402
Angel Pons7c49cb82020-03-16 23:17:32 +0100403 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100404 tsegbasedelta = tsegbase & (tsegsize - 1);
405 tsegbase &= ~(tsegsize - 1);
406
407 gttbase -= tsegbasedelta;
408 gfxstolenbase -= tsegbasedelta;
409 toludbase -= tsegbasedelta;
410
Angel Pons7c49cb82020-03-16 23:17:32 +0100411 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100412 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100413 /* Reclaim is possible */
414 reclaim = 1;
415 remapbase = MAX(4096, tom - me_uma_size);
416 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
417 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100418 } else {
419 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100420 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100421 touudbase = tom - me_uma_size;
422 }
423
Angel Pons7c49cb82020-03-16 23:17:32 +0100424 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100425 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
426
Angel Pons7c49cb82020-03-16 23:17:32 +0100427 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100428 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100429 val = tom & 0xfff;
430 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100431 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100432 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100433
Angel Ponsb31d1d72020-01-10 01:35:09 +0100434 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100435 val = tom & 0xfffff000;
436 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100437 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100438 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100439
Angel Pons7c49cb82020-03-16 23:17:32 +0100440 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442 val = toludbase & 0xfff;
443 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100444 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100445 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100446
Angel Pons7c49cb82020-03-16 23:17:32 +0100447 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100448 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100449 val = touudbase & 0xfff;
450 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100451 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100452 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100453
Angel Pons7c49cb82020-03-16 23:17:32 +0100454 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100455 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100456 val = touudbase & 0xfffff000;
457 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100458 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100459 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100460
461 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100462 /* REMAP BASE */
463 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100464 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100465
Angel Pons7c49cb82020-03-16 23:17:32 +0100466 /* REMAP LIMIT */
467 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100468 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100469 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100470 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100471 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100472 val = tsegbase & 0xfff;
473 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100474 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100475 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100476
Angel Pons7c49cb82020-03-16 23:17:32 +0100477 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100478 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100479 val = gfxstolenbase & 0xfff;
480 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100481 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100482 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100483
Angel Pons7c49cb82020-03-16 23:17:32 +0100484 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100485 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100486 val = gttbase & 0xfff;
487 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100488 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100489 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100490
491 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100492 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100493 val = (0x80000 - me_uma_size) & 0xfffff000;
494 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100495 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100496 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100497
Angel Pons7c49cb82020-03-16 23:17:32 +0100498 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100499 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100500 val = mestolenbase & 0xfff;
501 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100502 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100503 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100504
Angel Ponsb31d1d72020-01-10 01:35:09 +0100505 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100506 val = mestolenbase & 0xfffff000;
507 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100508 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100509 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100510
Angel Pons7c49cb82020-03-16 23:17:32 +0100511 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513 val = (0x80000 - me_uma_size) & 0xfff;
514 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100515 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
516 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100517 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100518 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100519 }
520}
521
Angel Pons88521882020-01-05 20:21:20 +0100522static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100523{
524 int channel, slotrank;
525
Angel Pons7c49cb82020-03-16 23:17:32 +0100526 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100527 channel = (ctrl->rankmap[0]) ? 0 : 1;
528
Angel Pons88521882020-01-05 20:21:20 +0100529 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100530
Angel Pons7c49cb82020-03-16 23:17:32 +0100531 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100532 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
533
Angel Ponsffd50152020-11-12 11:03:10 +0100534 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100535
Angel Pons7c49cb82020-03-16 23:17:32 +0100536 /*
537 * Execute command queue - why is bit 22 set here?!
538 *
539 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
540 */
Angel Pons38d901e2020-05-02 23:50:43 +0200541 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200542
Angel Pons88521882020-01-05 20:21:20 +0100543 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100544}
545
Angel Pons88521882020-01-05 20:21:20 +0100546void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100547{
Felix Held9fe248f2018-07-31 20:59:45 +0200548 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100549 int channel;
550
Angel Pons7c49cb82020-03-16 23:17:32 +0100551 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
552 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100553 do {
Angel Pons88521882020-01-05 20:21:20 +0100554 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100555 } while ((reg & 0x14) == 0);
556
Angel Pons7c49cb82020-03-16 23:17:32 +0100557 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100558 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100559 MCHBAR32(MC_INIT_STATE_G) = reg;
560 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100561 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100562 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100563
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100565 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566
Angel Pons7c49cb82020-03-16 23:17:32 +0100567 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100568 udelay(200);
569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100571 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100572
Angel Pons7c49cb82020-03-16 23:17:32 +0100573 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100574 udelay(500);
575
Angel Pons7c49cb82020-03-16 23:17:32 +0100576 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100577 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100578
Angel Pons7c49cb82020-03-16 23:17:32 +0100579 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100580 udelay(1);
581
582 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100583 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200584 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100585 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100586
Angel Pons7c49cb82020-03-16 23:17:32 +0100587 /* Wait 10ns for ranks to settle */
588 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100589
590 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100591 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100592
Angel Pons7c49cb82020-03-16 23:17:32 +0100593 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100594 write_reset(ctrl);
595 }
596}
597
Angel Pons3d3bf482020-11-14 16:18:15 +0100598/*
599 * DDR3 Rank1 Address mirror swap the following pins:
600 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
601 */
602static void ddr3_mirror_mrreg(int *bank, u32 *addr)
603{
604 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
605 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
606}
607
Angel Pons7c49cb82020-03-16 23:17:32 +0100608static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100609{
Angel Pons88521882020-01-05 20:21:20 +0100610 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100611
Angel Pons3d3bf482020-11-14 16:18:15 +0100612 if (ctrl->rank_mirror[channel][slotrank])
613 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100614
Angel Pons8f0757e2020-11-11 23:03:36 +0100615 const struct iosav_ssq sequence[] = {
616 /* DRAM command MRS */
617 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200618 .sp_cmd_ctrl = {
619 .command = IOSAV_MRS,
620 },
621 .subseq_ctrl = {
622 .cmd_executions = 1,
623 .cmd_delay_gap = 4,
624 .post_ssq_wait = 4,
625 .data_direction = SSQ_NA,
626 },
627 .sp_cmd_addr = {
628 .address = val,
629 .rowbits = 6,
630 .bank = reg,
631 .rank = slotrank,
632 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100633 },
634 /* DRAM command MRS */
635 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200636 .sp_cmd_ctrl = {
637 .command = IOSAV_MRS,
638 .ranksel_ap = 1,
639 },
640 .subseq_ctrl = {
641 .cmd_executions = 1,
642 .cmd_delay_gap = 4,
643 .post_ssq_wait = 4,
644 .data_direction = SSQ_NA,
645 },
646 .sp_cmd_addr = {
647 .address = val,
648 .rowbits = 6,
649 .bank = reg,
650 .rank = slotrank,
651 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100652 },
653 /* DRAM command MRS */
654 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200655 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100656 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200657 },
658 .subseq_ctrl = {
659 .cmd_executions = 1,
660 .cmd_delay_gap = 4,
661 .post_ssq_wait = ctrl->tMOD,
662 .data_direction = SSQ_NA,
663 },
664 .sp_cmd_addr = {
665 .address = val,
666 .rowbits = 6,
667 .bank = reg,
668 .rank = slotrank,
669 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100670 },
671 };
672 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200673
Angel Pons7c49cb82020-03-16 23:17:32 +0100674 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200675 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100676}
677
Angel Pons88521882020-01-05 20:21:20 +0100678static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100679{
680 u16 mr0reg, mch_cas, mch_wr;
681 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 +0100682 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100683
Angel Pons7c49cb82020-03-16 23:17:32 +0100684 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100685 if (ctrl->CAS < 12) {
686 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
687 } else {
688 mch_cas = (u16) (ctrl->CAS - 12);
689 mch_cas = ((mch_cas << 1) | 0x1);
690 }
691
Angel Pons7c49cb82020-03-16 23:17:32 +0100692 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100693 mch_wr = mch_wr_t[ctrl->tWR - 5];
694
Angel Pons2bf28ed2020-11-12 13:49:59 +0100695 /* DLL Reset - self clearing - set after CLK frequency has been changed */
696 mr0reg = 1 << 8;
697
698 mr0reg |= (mch_cas & 0x1) << 2;
699 mr0reg |= (mch_cas & 0xe) << 3;
700 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100701
Angel Pons7c49cb82020-03-16 23:17:32 +0100702 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
Angel Pons2bf28ed2020-11-12 13:49:59 +0100703 mr0reg |= !is_mobile << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100704 return mr0reg;
705}
706
707static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
708{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200709 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100710}
711
Angel Ponsf9997482020-11-12 16:02:52 +0100712static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100713{
714 /* Get ODT based on rankmap */
715 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
716
717 if (dimms_per_ch == 1) {
718 return (const odtmap){60, 60};
719 } else {
720 return (const odtmap){120, 30};
721 }
722}
723
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100724static u32 encode_odt(u32 odt)
725{
726 switch (odt) {
727 case 30:
728 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
729 case 60:
730 return (1 << 2); // RZQ/4
731 case 120:
732 return (1 << 6); // RZQ/2
733 default:
734 case 0:
735 return 0;
736 }
737}
738
739static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
740{
741 odtmap odt;
742 u32 mr1reg;
743
Angel Ponsf9997482020-11-12 16:02:52 +0100744 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100745 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100746
747 mr1reg |= encode_odt(odt.rttnom);
748
749 return mr1reg;
750}
751
752static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
753{
754 u16 mr1reg;
755
756 mr1reg = make_mr1(ctrl, rank, channel);
757
758 write_mrreg(ctrl, channel, rank, 1, mr1reg);
759}
760
761static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
762{
Angel Pons868bca22020-11-13 13:38:04 +0100763 const u16 pasr = 0;
764 const u16 cwl = ctrl->CWL - 5;
765 const odtmap odt = get_ODT(ctrl, channel);
766
Angel Ponsdca3cb52020-11-13 13:42:07 +0100767 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100768 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
769 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100770
Angel Pons868bca22020-11-13 13:38:04 +0100771 u16 mr2reg = 0;
772 mr2reg |= pasr;
773 mr2reg |= cwl << 3;
774 mr2reg |= ctrl->auto_self_refresh << 6;
775 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100776 mr2reg |= (odt.rttwr / 60) << 9;
777
778 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100779
780 /* Program MR2 shadow */
781 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
782
783 reg32 &= 3 << 14 | 3 << 6;
784
785 reg32 |= mr2reg & ~(3 << 6);
786
787 if (rank & 1) {
788 if (srt)
789 reg32 |= 1 << (rank / 2 + 6);
790 } else {
791 if (ctrl->rank_mirror[channel][rank])
792 reg32 |= 1 << (rank / 2 + 14);
793 }
794 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100795}
796
797static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
798{
799 write_mrreg(ctrl, channel, rank, 3, 0);
800}
801
Angel Pons88521882020-01-05 20:21:20 +0100802void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100803{
804 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100805 int channel;
806
807 FOR_ALL_POPULATED_CHANNELS {
808 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100809 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100810 dram_mr2(ctrl, slotrank, channel);
811
Angel Pons7c49cb82020-03-16 23:17:32 +0100812 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100813 dram_mr3(ctrl, slotrank, channel);
814
Angel Pons7c49cb82020-03-16 23:17:32 +0100815 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100816 dram_mr1(ctrl, slotrank, channel);
817
Angel Pons7c49cb82020-03-16 23:17:32 +0100818 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100819 dram_mr0(ctrl, slotrank, channel);
820 }
821 }
822
Angel Pons8f0757e2020-11-11 23:03:36 +0100823 const struct iosav_ssq zqcl_sequence[] = {
824 /* DRAM command NOP (without ODT nor chip selects) */
825 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200826 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100827 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200828 },
829 .subseq_ctrl = {
830 .cmd_executions = 1,
831 .cmd_delay_gap = 4,
832 .post_ssq_wait = 15,
833 .data_direction = SSQ_NA,
834 },
835 .sp_cmd_addr = {
836 .address = 2,
837 .rowbits = 6,
838 .bank = 0,
839 .rank = 0,
840 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100841 },
842 /* DRAM command ZQCL */
843 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200844 .sp_cmd_ctrl = {
845 .command = IOSAV_ZQCS,
846 .ranksel_ap = 1,
847 },
848 .subseq_ctrl = {
849 .cmd_executions = 1,
850 .cmd_delay_gap = 4,
851 .post_ssq_wait = 400,
852 .data_direction = SSQ_NA,
853 },
854 .sp_cmd_addr = {
855 .address = 1024,
856 .rowbits = 6,
857 .bank = 0,
858 .rank = 0,
859 },
860 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100861 .inc_rank = 1,
862 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200863 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100864 },
865 };
866 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100867
Angel Pons7c49cb82020-03-16 23:17:32 +0100868 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200869 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100870
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100871 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100872 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100873 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100874 }
875
Angel Pons7c49cb82020-03-16 23:17:32 +0100876 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100877 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100878
879 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100880 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100881
Angel Pons88521882020-01-05 20:21:20 +0100882 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100883
884 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
885
Angel Pons7c49cb82020-03-16 23:17:32 +0100886 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100887 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100888
Angel Ponsffd50152020-11-12 11:03:10 +0100889 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200890
Angel Pons7c49cb82020-03-16 23:17:32 +0100891 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200892 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100893
Angel Pons7c49cb82020-03-16 23:17:32 +0100894 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100895 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100896 }
897}
898
Felix Held3b906032020-01-14 17:05:43 +0100899static const u32 lane_base[] = {
900 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
901 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
902 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100903};
904
Angel Pons88521882020-01-05 20:21:20 +0100905void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100906{
Angel Pons88521882020-01-05 20:21:20 +0100907 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100908 int lane;
909 int slotrank, slot;
910 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100911 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100912
913 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100914 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
915 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100916 }
917
918 for (slot = 0; slot < NUM_SLOTS; slot++)
919 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
920 case 0:
921 default:
Angel Pons88521882020-01-05 20:21:20 +0100922 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100923 break;
924 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100925 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100926 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100927 break;
928 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100929 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100930 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100931 break;
932 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100933 pi_coding_ctrl[slot] =
934 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100935 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100936 break;
937 }
938
Angel Pons7c49cb82020-03-16 23:17:32 +0100939 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100941 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
942 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100943 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100944 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
945
Angel Pons88521882020-01-05 20:21:20 +0100946 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100947
Angel Pons7c49cb82020-03-16 23:17:32 +0100948 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100949 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
950 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100951
952 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100953 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100954 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100955 if (shift < 0)
956 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100957
Angel Pons88521882020-01-05 20:21:20 +0100958 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100959
960 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100961 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
962 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100963 }
964
Angel Pons88521882020-01-05 20:21:20 +0100965 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
966 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100967
Angel Pons88521882020-01-05 20:21:20 +0100968 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100969 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100970
Angel Pons88521882020-01-05 20:21:20 +0100971 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100972
973 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100974 int post_timA_min_high = 7, pre_timA_min_high = 7;
975 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100976 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100977 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100978
979 if (shift < 0)
980 shift = 0;
981
982 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200983 post_timA_min_high = MIN(post_timA_min_high,
984 (ctrl->timings[channel][slotrank].lanes[lane].
985 timA + shift) >> 6);
986 pre_timA_min_high = MIN(pre_timA_min_high,
987 ctrl->timings[channel][slotrank].lanes[lane].
988 timA >> 6);
989 post_timA_max_high = MAX(post_timA_max_high,
990 (ctrl->timings[channel][slotrank].lanes[lane].
991 timA + shift) >> 6);
992 pre_timA_max_high = MAX(pre_timA_max_high,
993 ctrl->timings[channel][slotrank].lanes[lane].
994 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100995 }
996
997 if (pre_timA_max_high - pre_timA_min_high <
998 post_timA_max_high - post_timA_min_high)
999 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001000
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001001 else if (pre_timA_max_high - pre_timA_min_high >
1002 post_timA_max_high - post_timA_min_high)
1003 shift_402x = -1;
1004
Felix Helddee167e2019-12-30 17:30:16 +01001005 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001006 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001007 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001008
Angel Pons88521882020-01-05 20:21:20 +01001009 reg_roundtrip_latency |=
1010 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001011 shift_402x) << (8 * slotrank);
1012
1013 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001014 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001015 (((ctrl->timings[channel][slotrank].lanes[lane].
1016 timA + shift) & 0x3f)
1017 |
1018 ((ctrl->timings[channel][slotrank].lanes[lane].
1019 rising + shift) << 8)
1020 |
1021 (((ctrl->timings[channel][slotrank].lanes[lane].
1022 timA + shift -
1023 (post_timA_min_high << 6)) & 0x1c0) << 10)
1024 | ((ctrl->timings[channel][slotrank].lanes[lane].
1025 falling + shift) << 20));
1026
Felix Heldfb19c8a2020-01-14 21:27:59 +01001027 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001028 (((ctrl->timings[channel][slotrank].lanes[lane].
1029 timC + shift) & 0x3f)
1030 |
1031 (((ctrl->timings[channel][slotrank].lanes[lane].
1032 timB + shift) & 0x3f) << 8)
1033 |
1034 (((ctrl->timings[channel][slotrank].lanes[lane].
1035 timB + shift) & 0x1c0) << 9)
1036 |
1037 (((ctrl->timings[channel][slotrank].lanes[lane].
1038 timC + shift) & 0x40) << 13));
1039 }
1040 }
Angel Pons88521882020-01-05 20:21:20 +01001041 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1042 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001043}
1044
Angel Pons88521882020-01-05 20:21:20 +01001045static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001046{
Angel Pons88521882020-01-05 20:21:20 +01001047 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001048
Angel Ponsffd50152020-11-12 11:03:10 +01001049 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001050
Angel Pons7c49cb82020-03-16 23:17:32 +01001051 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001052 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053
Angel Pons88521882020-01-05 20:21:20 +01001054 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001055}
1056
Angel Pons7c49cb82020-03-16 23:17:32 +01001057static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001058{
1059 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001060
1061 return (MCHBAR32(lane_base[lane] +
1062 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001063}
1064
1065struct run {
1066 int middle;
1067 int end;
1068 int start;
1069 int all;
1070 int length;
1071};
1072
1073static struct run get_longest_zero_run(int *seq, int sz)
1074{
1075 int i, ls;
1076 int bl = 0, bs = 0;
1077 struct run ret;
1078
1079 ls = 0;
1080 for (i = 0; i < 2 * sz; i++)
1081 if (seq[i % sz]) {
1082 if (i - ls > bl) {
1083 bl = i - ls;
1084 bs = ls;
1085 }
1086 ls = i + 1;
1087 }
1088 if (bl == 0) {
1089 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001090 ret.start = 0;
1091 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001092 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001093 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001094 return ret;
1095 }
1096
Angel Pons7c49cb82020-03-16 23:17:32 +01001097 ret.start = bs % sz;
1098 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001099 ret.middle = (bs + (bl - 1) / 2) % sz;
1100 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001101 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001102
1103 return ret;
1104}
1105
Angel Ponsf3053392020-11-13 23:31:12 +01001106static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001107{
1108 int timA;
1109 int statistics[NUM_LANES][128];
1110 int lane;
1111
1112 for (timA = 0; timA < 128; timA++) {
1113 FOR_ALL_LANES {
1114 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1115 }
1116 program_timings(ctrl, channel);
1117
1118 test_timA(ctrl, channel, slotrank);
1119
1120 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001121 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001122 }
1123 }
1124 FOR_ALL_LANES {
1125 struct run rn = get_longest_zero_run(statistics[lane], 128);
1126 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1127 upperA[lane] = rn.end;
1128 if (upperA[lane] < rn.middle)
1129 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001130
Patrick Rudolph368b6152016-11-25 16:36:52 +01001131 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001132 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001133 }
1134}
1135
Angel Ponsf3053392020-11-13 23:31:12 +01001136static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001137{
1138 int timA_delta;
1139 int statistics[NUM_LANES][51];
1140 int lane, i;
1141
1142 memset(statistics, 0, sizeof(statistics));
1143
1144 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001145
1146 FOR_ALL_LANES {
1147 ctrl->timings[channel][slotrank].lanes[lane].timA
1148 = upperA[lane] + timA_delta + 0x40;
1149 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001150 program_timings(ctrl, channel);
1151
1152 for (i = 0; i < 100; i++) {
1153 test_timA(ctrl, channel, slotrank);
1154 FOR_ALL_LANES {
1155 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001156 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001157 }
1158 }
1159 }
1160 FOR_ALL_LANES {
1161 int last_zero, first_all;
1162
1163 for (last_zero = -25; last_zero <= 25; last_zero++)
1164 if (statistics[lane][last_zero + 25])
1165 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001166
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001167 last_zero--;
1168 for (first_all = -25; first_all <= 25; first_all++)
1169 if (statistics[lane][first_all + 25] == 100)
1170 break;
1171
Angel Pons7c49cb82020-03-16 23:17:32 +01001172 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001173
1174 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001175 (last_zero + first_all) / 2 + upperA[lane];
1176
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001177 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001178 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001179 }
1180}
1181
Angel Ponsf3053392020-11-13 23:31:12 +01001182static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001183{
1184 int works[NUM_LANES];
1185 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001186
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001187 while (1) {
1188 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190 program_timings(ctrl, channel);
1191 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001192
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001194 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1195
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001196 if (works[lane])
1197 some_works = 1;
1198 else
1199 all_works = 0;
1200 }
1201 if (all_works)
1202 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001203
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001204 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001205 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001206 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1207 channel, slotrank);
1208 return MAKE_ERR;
1209 }
Angel Pons88521882020-01-05 20:21:20 +01001210 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001211 printram("4024 -= 2;\n");
1212 continue;
1213 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001214 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001215 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001216
Felix Heldef4fe3e2019-12-31 14:15:05 +01001217 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001218 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1219 channel, slotrank);
1220 return MAKE_ERR;
1221 }
1222 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001223 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001225 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001226 }
1227 }
1228 return 0;
1229}
1230
Angel Pons12bd8ab2020-11-13 23:10:52 +01001231static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001232{
1233 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001234 u16 logic_delay_min = 7;
1235 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001236
1237 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001238 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1239
1240 logic_delay_min = MIN(logic_delay_min, logic_delay);
1241 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001243
1244 if (logic_delay_max < logic_delay_min) {
1245 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1246 logic_delay_max, logic_delay_min, channel, slotrank);
1247 }
1248
1249 assert(logic_delay_max >= logic_delay_min);
1250
1251 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001252}
1253
Angel Pons12bd8ab2020-11-13 23:10:52 +01001254static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001255{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001256 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001257
Angel Pons7c49cb82020-03-16 23:17:32 +01001258 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001259 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001260
Angel Pons12bd8ab2020-11-13 23:10:52 +01001261 if (prev < post)
1262 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001263
Angel Pons12bd8ab2020-11-13 23:10:52 +01001264 else if (prev > post)
1265 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001266
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001267 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001268 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001269
Angel Pons12bd8ab2020-11-13 23:10:52 +01001270 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1271 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1272 printram("4024 += %d;\n", latency_offset);
1273 printram("4028 += %d;\n", latency_offset);
1274
1275 return post;
1276}
1277
1278static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1279{
1280 u16 logic_delay_min = 7;
1281 int lane;
1282
1283 FOR_ALL_LANES {
1284 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1285
1286 logic_delay_min = MIN(logic_delay_min, logic_delay);
1287 }
1288
1289 if (logic_delay_min >= 2) {
1290 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1291 logic_delay_min, channel, slotrank);
1292 }
1293
1294 FOR_ALL_LANES {
1295 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1296 }
1297 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1298 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001299}
1300
Angel Pons7c49cb82020-03-16 23:17:32 +01001301/*
1302 * Compensate the skew between DQS and DQs.
1303 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001304 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1305 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001306 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001307 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001308 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001309 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1310 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1311 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001312 * DQ in phase the data being read is expected to alternate on every byte:
1313 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001314 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001315 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001316 * Once the controller has detected this pattern a bit in the result register is set for the
1317 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001318 */
Angel Pons88521882020-01-05 20:21:20 +01001319int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001320{
1321 int channel, slotrank, lane;
1322 int err;
1323
1324 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1325 int all_high, some_high;
1326 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001327 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001328
Angel Pons88521882020-01-05 20:21:20 +01001329 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001330
Angel Ponsffd50152020-11-12 11:03:10 +01001331 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001332
Angel Pons7c49cb82020-03-16 23:17:32 +01001333 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001334 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001335
Angel Pons88521882020-01-05 20:21:20 +01001336 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001337
Felix Heldef4fe3e2019-12-31 14:15:05 +01001338 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001339 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001340 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001341
Angel Ponsf3053392020-11-13 23:31:12 +01001342 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001343
Felix Held2bb3cdf2018-07-28 00:23:59 +02001344 all_high = 1;
1345 some_high = 0;
1346 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001347 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001348 some_high = 1;
1349 else
1350 all_high = 0;
1351 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001352
1353 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001354 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001355 printram("4028--;\n");
1356 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001357 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001358 upperA[lane] -= 0x40;
1359
1360 }
1361 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001362 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001363 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001364 printram("4024++;\n");
1365 printram("4028++;\n");
1366 }
1367
1368 program_timings(ctrl, channel);
1369
Angel Pons12bd8ab2020-11-13 23:10:52 +01001370 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001371
Angel Ponsf3053392020-11-13 23:31:12 +01001372 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001373 if (err)
1374 return err;
1375
Angel Pons12bd8ab2020-11-13 23:10:52 +01001376 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001377
Angel Ponsf3053392020-11-13 23:31:12 +01001378 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379
Angel Pons12bd8ab2020-11-13 23:10:52 +01001380 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001381
Angel Pons12bd8ab2020-11-13 23:10:52 +01001382 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001383
Angel Pons12bd8ab2020-11-13 23:10:52 +01001384 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001385
1386 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001387 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001388 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001389
1390 printram("final results:\n");
1391 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001392 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001393 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001394
Angel Pons88521882020-01-05 20:21:20 +01001395 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396
1397 toggle_io_reset();
1398 }
1399
1400 FOR_ALL_POPULATED_CHANNELS {
1401 program_timings(ctrl, channel);
1402 }
Angel Pons50a6fe72020-11-14 01:18:14 +01001403 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001404 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001405 }
1406 return 0;
1407}
1408
Angel Pons011661c2020-11-15 18:21:35 +01001409static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001410{
1411 int lane;
1412
1413 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001414 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1415 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001416 }
1417
Angel Pons88521882020-01-05 20:21:20 +01001418 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001419
Angel Ponsffd50152020-11-12 11:03:10 +01001420 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1421 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001422
Angel Pons7c49cb82020-03-16 23:17:32 +01001423 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001424 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001425
Angel Pons88521882020-01-05 20:21:20 +01001426 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001427
Angel Pons8f0757e2020-11-11 23:03:36 +01001428 const struct iosav_ssq rd_sequence[] = {
1429 /* DRAM command PREA */
1430 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001431 .sp_cmd_ctrl = {
1432 .command = IOSAV_PRE,
1433 .ranksel_ap = 1,
1434 },
1435 .subseq_ctrl = {
1436 .cmd_executions = 1,
1437 .cmd_delay_gap = 3,
1438 .post_ssq_wait = ctrl->tRP,
1439 .data_direction = SSQ_NA,
1440 },
1441 .sp_cmd_addr = {
1442 .address = 1024,
1443 .rowbits = 6,
1444 .bank = 0,
1445 .rank = slotrank,
1446 },
1447 .addr_update = {
1448 .addr_wrap = 18,
1449 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001450 },
1451 /* DRAM command ACT */
1452 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001453 .sp_cmd_ctrl = {
1454 .command = IOSAV_ACT,
1455 .ranksel_ap = 1,
1456 },
1457 .subseq_ctrl = {
1458 .cmd_executions = 8,
1459 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1460 .post_ssq_wait = ctrl->CAS,
1461 .data_direction = SSQ_NA,
1462 },
1463 .sp_cmd_addr = {
1464 .address = 0,
1465 .rowbits = 6,
1466 .bank = 0,
1467 .rank = slotrank,
1468 },
1469 .addr_update = {
1470 .inc_bank = 1,
1471 .addr_wrap = 18,
1472 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001473 },
1474 /* DRAM command RD */
1475 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001476 .sp_cmd_ctrl = {
1477 .command = IOSAV_RD,
1478 .ranksel_ap = 1,
1479 },
1480 .subseq_ctrl = {
1481 .cmd_executions = 500,
1482 .cmd_delay_gap = 4,
1483 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1484 .data_direction = SSQ_RD,
1485 },
1486 .sp_cmd_addr = {
1487 .address = 0,
1488 .rowbits = 0,
1489 .bank = 0,
1490 .rank = slotrank,
1491 },
1492 .addr_update = {
1493 .inc_addr_8 = 1,
1494 .addr_wrap = 18,
1495 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001496 },
1497 /* DRAM command PREA */
1498 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001499 .sp_cmd_ctrl = {
1500 .command = IOSAV_PRE,
1501 .ranksel_ap = 1,
1502 },
1503 .subseq_ctrl = {
1504 .cmd_executions = 1,
1505 .cmd_delay_gap = 3,
1506 .post_ssq_wait = ctrl->tRP,
1507 .data_direction = SSQ_NA,
1508 },
1509 .sp_cmd_addr = {
1510 .address = 1024,
1511 .rowbits = 6,
1512 .bank = 0,
1513 .rank = slotrank,
1514 },
1515 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01001516 .addr_wrap = 18,
Angel Pons3abd2062020-05-03 00:25:02 +02001517 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001518 },
1519 };
1520 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001521
Angel Pons7c49cb82020-03-16 23:17:32 +01001522 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001523 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001524
Angel Pons88521882020-01-05 20:21:20 +01001525 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001526}
1527
Angel Pons011661c2020-11-15 18:21:35 +01001528static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001529{
1530 int min = data[0];
1531 int max = min;
1532 int i;
1533 for (i = 1; i < count; i++) {
1534 if (min > data[i])
1535 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001536
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001537 if (max < data[i])
1538 max = data[i];
1539 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001540 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001541 for (i = 0; i < count; i++)
1542 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001543
Angel Pons891f2bc2020-01-10 01:27:28 +01001544 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001545}
1546
Angel Pons011661c2020-11-15 18:21:35 +01001547static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001548{
Angel Pons011661c2020-11-15 18:21:35 +01001549 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001550 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001551 int lane;
1552
Angel Pons88521882020-01-05 20:21:20 +01001553 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001554
Angel Ponsffd50152020-11-12 11:03:10 +01001555 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001556
Angel Pons7c49cb82020-03-16 23:17:32 +01001557 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001558 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001559
Angel Pons011661c2020-11-15 18:21:35 +01001560 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1561 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001562 program_timings(ctrl, channel);
1563
Angel Pons011661c2020-11-15 18:21:35 +01001564 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001565
1566 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001567 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001568 }
1569 }
1570 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001571 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1572
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001573 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001574 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1575 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001576 /*
1577 * With command training not being done yet, the lane can be erroneous.
1578 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001579 */
Angel Pons011661c2020-11-15 18:21:35 +01001580 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001581 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1582
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001583 if (rn.all || rn.length < 8) {
1584 printk(BIOS_EMERG, "timC recovery failed\n");
1585 return MAKE_ERR;
1586 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001587 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001588 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001589 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001590 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001591 }
1592 return 0;
1593}
1594
Angel Pons88521882020-01-05 20:21:20 +01001595static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001596{
1597 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001598
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001599 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1600 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001601
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001602 return ret;
1603}
1604
Angel Pons765d4652020-11-11 14:44:35 +01001605/* Each cacheline is 64 bits long */
1606static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1607{
1608 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1609}
1610
Angel Pons88521882020-01-05 20:21:20 +01001611static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001612{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301613 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001614 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001615
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001616 for (j = 0; j < 16; j++)
1617 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001618
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001620
1621 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001622}
1623
Angel Pons88521882020-01-05 20:21:20 +01001624static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001625{
1626 int ret = 0;
1627 int channel;
1628 FOR_ALL_POPULATED_CHANNELS ret++;
1629 return ret;
1630}
1631
Angel Pons88521882020-01-05 20:21:20 +01001632static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001633{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301634 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001635 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301636 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001637
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001638 for (j = 0; j < 16; j++)
1639 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001640
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001641 for (j = 0; j < 16; j++)
1642 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001643
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001644 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001645
1646 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001647}
1648
Angel Pons88521882020-01-05 20:21:20 +01001649static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001650{
1651 int channel, slotrank, lane;
1652
1653 FOR_ALL_POPULATED_CHANNELS {
1654 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001655 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1656 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657 }
1658
1659 program_timings(ctrl, channel);
1660
1661 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001662 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663
Angel Ponsffd50152020-11-12 11:03:10 +01001664 iosav_write_read_mpr_sequence(
1665 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001666
Angel Pons7c49cb82020-03-16 23:17:32 +01001667 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001668 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001669
Angel Pons88521882020-01-05 20:21:20 +01001670 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001671 }
1672
1673 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001674 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1675 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001676 }
1677
1678 program_timings(ctrl, channel);
1679
1680 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001681 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001682
Angel Ponsffd50152020-11-12 11:03:10 +01001683 iosav_write_read_mpr_sequence(
1684 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001685
Angel Pons7c49cb82020-03-16 23:17:32 +01001686 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001687 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001688
Angel Pons88521882020-01-05 20:21:20 +01001689 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001690 }
1691 }
1692}
1693
Angel Pons88521882020-01-05 20:21:20 +01001694static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001695{
Angel Pons59996e02020-11-14 16:34:35 +01001696 /* First DQS/DQS# rising edge after write leveling mode is programmed */
1697 const u32 tWLMRD = 40;
1698
1699 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1700 int bank = 1;
1701
1702 if (ctrl->rank_mirror[channel][slotrank])
1703 ddr3_mirror_mrreg(&bank, &mr1reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001704
Angel Pons88521882020-01-05 20:21:20 +01001705 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001706
1707 const struct iosav_ssq sequence[] = {
Angel Pons59996e02020-11-14 16:34:35 +01001708 /* DRAM command MRS: enable DQs on this slotrank */
Angel Pons8f0757e2020-11-11 23:03:36 +01001709 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001710 .sp_cmd_ctrl = {
Angel Pons59996e02020-11-14 16:34:35 +01001711 .command = IOSAV_MRS,
1712 .ranksel_ap = 1,
1713 },
1714 .subseq_ctrl = {
1715 .cmd_executions = 1,
1716 .cmd_delay_gap = 3,
1717 .post_ssq_wait = tWLMRD,
1718 .data_direction = SSQ_NA,
1719 },
1720 .sp_cmd_addr = {
1721 .address = mr1reg,
1722 .rowbits = 6,
1723 .bank = bank,
1724 .rank = slotrank,
1725 },
1726 },
1727 /* DRAM command NOP */
1728 [1] = {
1729 .sp_cmd_ctrl = {
Angel Pons3abd2062020-05-03 00:25:02 +02001730 .command = IOSAV_NOP,
1731 .ranksel_ap = 1,
1732 },
1733 .subseq_ctrl = {
1734 .cmd_executions = 1,
1735 .cmd_delay_gap = 3,
1736 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1737 .data_direction = SSQ_WR,
1738 },
1739 .sp_cmd_addr = {
1740 .address = 8,
1741 .rowbits = 0,
1742 .bank = 0,
1743 .rank = slotrank,
1744 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001745 },
1746 /* DRAM command NOP */
Angel Pons59996e02020-11-14 16:34:35 +01001747 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001748 .sp_cmd_ctrl = {
1749 .command = IOSAV_NOP_ALT,
1750 .ranksel_ap = 1,
1751 },
1752 .subseq_ctrl = {
1753 .cmd_executions = 1,
1754 .cmd_delay_gap = 3,
1755 .post_ssq_wait = ctrl->CAS + 38,
1756 .data_direction = SSQ_RD,
1757 },
1758 .sp_cmd_addr = {
1759 .address = 4,
1760 .rowbits = 0,
1761 .bank = 0,
1762 .rank = slotrank,
1763 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001764 },
Angel Pons59996e02020-11-14 16:34:35 +01001765 /* DRAM command MRS: disable DQs on this slotrank */
1766 [3] = {
1767 .sp_cmd_ctrl = {
1768 .command = IOSAV_MRS,
1769 .ranksel_ap = 1,
1770 },
1771 .subseq_ctrl = {
1772 .cmd_executions = 1,
1773 .cmd_delay_gap = 3,
1774 .post_ssq_wait = ctrl->tMOD,
1775 .data_direction = SSQ_NA,
1776 },
1777 .sp_cmd_addr = {
1778 .address = mr1reg | 1 << 12,
1779 .rowbits = 6,
1780 .bank = bank,
1781 .rank = slotrank,
1782 },
1783 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001784 };
1785 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786
Angel Pons7c49cb82020-03-16 23:17:32 +01001787 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001788 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001789
Angel Pons88521882020-01-05 20:21:20 +01001790 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001791}
1792
1793static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1794{
1795 int timB;
1796 int statistics[NUM_LANES][128];
1797 int lane;
1798
Angel Pons88521882020-01-05 20:21:20 +01001799 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001800
1801 for (timB = 0; timB < 128; timB++) {
1802 FOR_ALL_LANES {
1803 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1804 }
1805 program_timings(ctrl, channel);
1806
1807 test_timB(ctrl, channel, slotrank);
1808
1809 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001810 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1811 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1812 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813 }
1814 }
1815 FOR_ALL_LANES {
1816 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001817 /*
1818 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1819 * of timB by a small value, which might cause the 6-bit value to overflow if
1820 * it's close to 0x3f. Increment the value by a small offset if it's likely
1821 * to overflow, to make sure it won't overflow while running tests and bricks
1822 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001823 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001824 * TODO: find out why some tests (edge write discovery) increment timB.
1825 */
1826 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001827 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001828 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001829 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001830
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001831 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1832 if (rn.all) {
1833 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1834 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001835
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836 return MAKE_ERR;
1837 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001838 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1839 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001840 }
1841 return 0;
1842}
1843
1844static int get_timB_high_adjust(u64 val)
1845{
1846 int i;
1847
Angel Ponsbf13ef02020-11-11 18:40:06 +01001848 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001849 if (val == 0xffffffffffffffffLL)
1850 return 0;
1851
1852 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001853 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001854 for (i = 0; i < 8; i++)
1855 if (val << (8 * (7 - i) + 4))
1856 return -i;
1857 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001858 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001859 for (i = 0; i < 8; i++)
1860 if (val >> (8 * (7 - i) + 4))
1861 return i;
1862 }
1863 return 8;
1864}
1865
Angel Ponsbf13ef02020-11-11 18:40:06 +01001866static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001867{
1868 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001869 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001870 FOR_ALL_POPULATED_CHANNELS {
1871 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001872 }
1873 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1874
Angel Pons765d4652020-11-11 14:44:35 +01001875 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001876 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001877
Angel Pons88521882020-01-05 20:21:20 +01001878 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001879
Angel Ponsffd50152020-11-12 11:03:10 +01001880 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001881
Angel Pons7c49cb82020-03-16 23:17:32 +01001882 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001883 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001884
Angel Pons88521882020-01-05 20:21:20 +01001885 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001886
Angel Pons8f0757e2020-11-11 23:03:36 +01001887 const struct iosav_ssq rd_sequence[] = {
1888 /* DRAM command PREA */
1889 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001890 .sp_cmd_ctrl = {
1891 .command = IOSAV_PRE,
1892 .ranksel_ap = 1,
1893 },
1894 .subseq_ctrl = {
1895 .cmd_executions = 1,
1896 .cmd_delay_gap = 3,
1897 .post_ssq_wait = ctrl->tRP,
1898 .data_direction = SSQ_NA,
1899 },
1900 .sp_cmd_addr = {
1901 .address = 1024,
1902 .rowbits = 6,
1903 .bank = 0,
1904 .rank = slotrank,
1905 },
1906 .addr_update = {
1907 .addr_wrap = 18,
1908 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001909 },
1910 /* DRAM command ACT */
1911 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001912 .sp_cmd_ctrl = {
1913 .command = IOSAV_ACT,
1914 .ranksel_ap = 1,
1915 },
1916 .subseq_ctrl = {
1917 .cmd_executions = 1,
1918 .cmd_delay_gap = 3,
1919 .post_ssq_wait = ctrl->tRCD,
1920 .data_direction = SSQ_NA,
1921 },
1922 .sp_cmd_addr = {
1923 .address = 0,
1924 .rowbits = 6,
1925 .bank = 0,
1926 .rank = slotrank,
1927 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001928 },
1929 /* DRAM command RD */
1930 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001931 .sp_cmd_ctrl = {
1932 .command = IOSAV_RD,
1933 .ranksel_ap = 3,
1934 },
1935 .subseq_ctrl = {
1936 .cmd_executions = 1,
1937 .cmd_delay_gap = 3,
1938 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001939 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001940 ctrl->timings[channel][slotrank].io_latency,
1941 .data_direction = SSQ_RD,
1942 },
1943 .sp_cmd_addr = {
1944 .address = 8,
1945 .rowbits = 6,
1946 .bank = 0,
1947 .rank = slotrank,
1948 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001949 },
1950 };
1951 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001952
Angel Pons7c49cb82020-03-16 23:17:32 +01001953 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001954 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001955
Angel Pons88521882020-01-05 20:21:20 +01001956 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001957 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001958 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001959 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001960 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001961 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1962 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1963 get_timB_high_adjust(res) * 64;
1964
1965 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001966 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1967 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001968 }
1969 }
Angel Pons88521882020-01-05 20:21:20 +01001970 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001971}
1972
Angel Pons7d115132020-11-14 01:44:44 +01001973static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974{
Angel Pons7d115132020-11-14 01:44:44 +01001975 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001976
Angel Pons7d115132020-11-14 01:44:44 +01001977 FOR_ALL_POPULATED_CHANNELS {
1978 /* choose an existing rank */
1979 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001980
Angel Pons7d115132020-11-14 01:44:44 +01001981 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001982
Angel Pons7d115132020-11-14 01:44:44 +01001983 /* Execute command queue */
1984 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985
Angel Pons7d115132020-11-14 01:44:44 +01001986 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001987
Angel Pons7d115132020-11-14 01:44:44 +01001988 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1989 }
1990
1991 /* Refresh disable */
1992 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1993
1994 FOR_ALL_POPULATED_CHANNELS {
1995 /* Execute the same command queue */
1996 iosav_run_once(channel);
1997
1998 wait_for_iosav(channel);
1999 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002000}
2001
Angel Pons7c49cb82020-03-16 23:17:32 +01002002/*
2003 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002004 *
Angel Pons7c49cb82020-03-16 23:17:32 +01002005 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
2006 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
2007 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
2008 * CLK/ADDR/CMD signals have the same routing delay.
2009 *
2010 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
2011 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
2012 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002013 */
Angel Pons88521882020-01-05 20:21:20 +01002014int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002015{
2016 int channel, slotrank, lane;
2017 int err;
2018
2019 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002020 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002021
Angel Pons7d115132020-11-14 01:44:44 +01002022 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002023
Angel Pons7c49cb82020-03-16 23:17:32 +01002024 /* Enable write leveling on all ranks
2025 Disable all DQ outputs
2026 Only NOP is allowed in this mode */
2027 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2028 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01002029 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002030
Angel Pons88521882020-01-05 20:21:20 +01002031 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002032
2033 toggle_io_reset();
2034
Angel Pons7c49cb82020-03-16 23:17:32 +01002035 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002036 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2037 err = discover_timB(ctrl, channel, slotrank);
2038 if (err)
2039 return err;
2040 }
2041
Angel Pons7c49cb82020-03-16 23:17:32 +01002042 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002043 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01002044 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002045
Angel Pons88521882020-01-05 20:21:20 +01002046 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002047
2048 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002049 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002050
Angel Pons7c49cb82020-03-16 23:17:32 +01002051 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002052 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002053
2054 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002055 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01002056 MCHBAR32(IOSAV_STATUS_ch(channel));
2057 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002058
Angel Ponsffd50152020-11-12 11:03:10 +01002059 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002060
Angel Pons7c49cb82020-03-16 23:17:32 +01002061 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002062 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002063
Angel Pons88521882020-01-05 20:21:20 +01002064 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002065 }
2066
2067 toggle_io_reset();
2068
2069 printram("CPE\n");
2070 precharge(ctrl);
2071 printram("CPF\n");
2072
Angel Pons50a6fe72020-11-14 01:18:14 +01002073 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002074 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002075 }
2076
2077 FOR_ALL_POPULATED_CHANNELS {
2078 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002079 }
2080
2081 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01002082 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002083 if (err)
2084 return err;
2085 }
2086
2087 FOR_ALL_POPULATED_CHANNELS
2088 program_timings(ctrl, channel);
2089
2090 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002091 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002092
2093 FOR_ALL_POPULATED_CHANNELS
2094 program_timings(ctrl, channel);
2095
Angel Pons50a6fe72020-11-14 01:18:14 +01002096 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002097 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002098 }
2099 return 0;
2100}
2101
Angel Ponsbf13ef02020-11-11 18:40:06 +01002102static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002103{
2104 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2105 int timC_delta;
2106 int lanes_ok = 0;
2107 int ctr = 0;
2108 int lane;
2109
2110 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2111 FOR_ALL_LANES {
2112 ctrl->timings[channel][slotrank].lanes[lane].timC =
2113 saved_rt.lanes[lane].timC + timC_delta;
2114 }
2115 program_timings(ctrl, channel);
2116 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002117 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002118 }
2119
Angel Pons765d4652020-11-11 14:44:35 +01002120 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002121 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002122
Angel Pons88521882020-01-05 20:21:20 +01002123 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002124
Angel Ponsffd50152020-11-12 11:03:10 +01002125 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002126
2127 /* Program LFSR for the RD/WR subsequences */
2128 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2129 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002130
Angel Pons7c49cb82020-03-16 23:17:32 +01002131 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002132 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002133
Angel Pons88521882020-01-05 20:21:20 +01002134 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002135 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002136 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002137
2138 if (r32 == 0)
2139 lanes_ok |= 1 << lane;
2140 }
2141 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002142 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143 break;
2144 }
2145
2146 ctrl->timings[channel][slotrank] = saved_rt;
2147
Patrick Rudolphdd662872017-10-28 18:20:11 +02002148 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149}
2150
Angel Pons88521882020-01-05 20:21:20 +01002151static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002152{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302153 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002154 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2155 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002156
2157 if (patno) {
2158 u8 base8 = 0x80 >> ((patno - 1) % 8);
2159 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2160 for (i = 0; i < 32; i++) {
2161 for (j = 0; j < 16; j++) {
2162 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002163
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002164 if (invert[patno - 1][i] & (1 << (j / 2)))
2165 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002166
2167 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002168 }
2169 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002170 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002171 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2172 for (j = 0; j < 16; j++) {
2173 const u32 val = pattern[i][j];
2174 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2175 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002176 }
2177 sfence();
2178 }
Angel Pons765d4652020-11-11 14:44:35 +01002179
2180 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002181}
2182
Angel Pons88521882020-01-05 20:21:20 +01002183static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002184{
Angel Pons7d115132020-11-14 01:44:44 +01002185 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002186
Angel Pons7c49cb82020-03-16 23:17:32 +01002187 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002188 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002189
2190 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002191 dram_mrscommands(ctrl);
2192
2193 toggle_io_reset();
2194}
2195
Angel Ponsbf13ef02020-11-11 18:40:06 +01002196#define CT_MIN_PI -127
2197#define CT_MAX_PI 128
2198#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2199
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002200#define MIN_C320C_LEN 13
2201
2202static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2203{
2204 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2205 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002206 int command_pi;
2207 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002208 int delta = 0;
2209
2210 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2211
2212 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002213 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002214 }
2215
2216 ctrl->cmd_stretch[channel] = cmd_stretch;
2217
Angel Pons88521882020-01-05 20:21:20 +01002218 MCHBAR32(TC_RAP_ch(channel)) =
Angel Pons7c49cb82020-03-16 23:17:32 +01002219 (ctrl->tRRD << 0)
2220 | (ctrl->tRTP << 4)
2221 | (ctrl->tCKE << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002222 | (ctrl->tWTR << 12)
2223 | (ctrl->tFAW << 16)
Angel Pons7c49cb82020-03-16 23:17:32 +01002224 | (ctrl->tWR << 24)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002225 | (ctrl->cmd_stretch[channel] << 30);
2226
2227 if (ctrl->cmd_stretch[channel] == 2)
2228 delta = 2;
2229 else if (ctrl->cmd_stretch[channel] == 0)
2230 delta = 4;
2231
2232 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002233 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002234 }
2235
Angel Ponsbf13ef02020-11-11 18:40:06 +01002236 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002237 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002238 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002239 }
2240 program_timings(ctrl, channel);
2241 reprogram_320c(ctrl);
2242 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002243 stat[slotrank][command_pi - CT_MIN_PI] =
2244 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002245 }
2246 }
2247 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002248 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002249
Angel Ponsbf13ef02020-11-11 18:40:06 +01002250 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002251 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2252 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002253
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002254 if (rn.all || rn.length < MIN_C320C_LEN) {
2255 FOR_ALL_POPULATED_RANKS {
2256 ctrl->timings[channel][slotrank] =
2257 saved_timings[channel][slotrank];
2258 }
2259 return MAKE_ERR;
2260 }
2261 }
2262
2263 return 0;
2264}
2265
Angel Pons7c49cb82020-03-16 23:17:32 +01002266/*
2267 * Adjust CMD phase shift and try multiple command rates.
2268 * A command rate of 2T doubles the time needed for address and command decode.
2269 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002270int command_training(ramctr_timing *ctrl)
2271{
2272 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002273
2274 FOR_ALL_POPULATED_CHANNELS {
2275 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002276 }
2277
2278 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002279 int cmdrate, err;
2280
2281 /*
2282 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002283 * Issue:
2284 * While c320c discovery seems to succeed raminit will fail in write training.
2285 *
2286 * Workaround:
2287 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2288 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002289 *
2290 * Single DIMM per channel:
2291 * Try command rate 1T and 2T
2292 */
2293 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002294 if (ctrl->tCMD)
2295 /* XMP gives the CMD rate in clock ticks, not ns */
2296 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002297
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002298 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002299 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2300
2301 if (!err)
2302 break;
2303 }
2304
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002305 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002306 printk(BIOS_EMERG, "c320c discovery failed\n");
2307 return err;
2308 }
2309
Angel Pons891f2bc2020-01-10 01:27:28 +01002310 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002311 }
2312
2313 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002314 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002315
2316 reprogram_320c(ctrl);
2317 return 0;
2318}
2319
Angel Pons4c79f932020-11-14 01:26:52 +01002320static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002321{
Angel Pons96a06dd2020-11-14 00:33:18 +01002322 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002323 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324 int lane;
2325
Angel Pons96a06dd2020-11-14 00:33:18 +01002326 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002327 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002328 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2329 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330 }
2331 program_timings(ctrl, channel);
2332
2333 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002334 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2335 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336 }
2337
Angel Pons88521882020-01-05 20:21:20 +01002338 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002339
Angel Ponsffd50152020-11-12 11:03:10 +01002340 iosav_write_read_mpr_sequence(
2341 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002342
Angel Pons7c49cb82020-03-16 23:17:32 +01002343 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002344 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002345
Angel Pons88521882020-01-05 20:21:20 +01002346 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002347
2348 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002349 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002350 }
2351 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002352
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002353 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002354 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002355 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002356
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002357 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002358 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2359 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002360 return MAKE_ERR;
2361 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002362 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002363 }
2364 return 0;
2365}
2366
Angel Pons60971dc2020-11-14 00:49:38 +01002367static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2368{
2369 int slotrank, lane;
2370
2371 fill_pattern0(ctrl, channel, 0, 0);
2372 FOR_ALL_LANES {
2373 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2374 }
2375
2376 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2377 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2378 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2379 }
2380
2381 program_timings(ctrl, channel);
2382
2383 FOR_ALL_POPULATED_RANKS {
2384 wait_for_iosav(channel);
2385
2386 iosav_write_read_mpr_sequence(
2387 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2388
2389 /* Execute command queue */
2390 iosav_run_once(channel);
2391
2392 wait_for_iosav(channel);
2393 }
2394
2395 /* XXX: check any measured value ? */
2396
2397 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2398 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2399 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2400 }
2401
2402 program_timings(ctrl, channel);
2403
2404 FOR_ALL_POPULATED_RANKS {
2405 wait_for_iosav(channel);
2406
2407 iosav_write_read_mpr_sequence(
2408 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2409
2410 /* Execute command queue */
2411 iosav_run_once(channel);
2412
2413 wait_for_iosav(channel);
2414 }
2415
2416 /* XXX: check any measured value ? */
2417
2418 FOR_ALL_LANES {
2419 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2420 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2421 }
2422}
2423
Angel Pons4c79f932020-11-14 01:26:52 +01002424int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002425{
2426 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2427 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2428 int channel, slotrank, lane;
2429 int err;
2430
Angel Pons88521882020-01-05 20:21:20 +01002431 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002432
2433 toggle_io_reset();
2434
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002435 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002436 FOR_ALL_LANES {
Angel Pons60971dc2020-11-14 00:49:38 +01002437 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002438 }
2439
Angel Pons60971dc2020-11-14 00:49:38 +01002440 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002441
2442 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002443 }
2444
Angel Pons0c3936e2020-03-22 12:49:27 +01002445 /*
2446 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2447 * also use a single loop. It would seem that it is a debugging configuration.
2448 */
Angel Pons88521882020-01-05 20:21:20 +01002449 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2450 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002451
2452 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002453 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002454 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002455 if (err)
2456 return err;
2457 }
2458
Angel Pons88521882020-01-05 20:21:20 +01002459 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2460 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002461
2462 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002463 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002464 rising_edges[channel][slotrank]);
2465 if (err)
2466 return err;
2467 }
2468
Angel Pons88521882020-01-05 20:21:20 +01002469 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002470
2471 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2472 ctrl->timings[channel][slotrank].lanes[lane].falling =
2473 falling_edges[channel][slotrank][lane];
2474 ctrl->timings[channel][slotrank].lanes[lane].rising =
2475 rising_edges[channel][slotrank][lane];
2476 }
2477
2478 FOR_ALL_POPULATED_CHANNELS {
2479 program_timings(ctrl, channel);
2480 }
2481
Angel Pons50a6fe72020-11-14 01:18:14 +01002482 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002483 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002484 }
2485 return 0;
2486}
2487
Angel Pons7c49cb82020-03-16 23:17:32 +01002488static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002489{
2490 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002491 u32 raw_stats[MAX_EDGE_TIMING + 1];
2492 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002493 const int reg3000b24[] = { 0, 0xc, 0x2c };
2494 int lane, i;
2495 int lower[NUM_LANES];
2496 int upper[NUM_LANES];
2497 int pat;
2498
2499 FOR_ALL_LANES {
2500 lower[lane] = 0;
2501 upper[lane] = MAX_EDGE_TIMING;
2502 }
2503
2504 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002505 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002506 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2507
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002508 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2509 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002510 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002511
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002512 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2513 FOR_ALL_LANES {
2514 ctrl->timings[channel][slotrank].lanes[lane].
2515 rising = edge;
2516 ctrl->timings[channel][slotrank].lanes[lane].
2517 falling = edge;
2518 }
2519 program_timings(ctrl, channel);
2520
2521 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002522 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2523 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002524 }
Angel Pons88521882020-01-05 20:21:20 +01002525 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002526
Angel Ponsffd50152020-11-12 11:03:10 +01002527 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002528
Angel Pons7c49cb82020-03-16 23:17:32 +01002529 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002530 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002531
Angel Pons88521882020-01-05 20:21:20 +01002532 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002533 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002534 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002535 }
2536
Angel Pons7c49cb82020-03-16 23:17:32 +01002537 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002538 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002539 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002540
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002541 FOR_ALL_LANES {
2542 struct run rn;
2543 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002544 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2545
2546 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2547
2548 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2549 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2550 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002551 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002552
2553 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2554 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2555
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002556 edges[lane] = (lower[lane] + upper[lane]) / 2;
2557 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002558 printk(BIOS_EMERG, "edge write discovery failed: "
2559 "%d, %d, %d\n", channel, slotrank, lane);
2560
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002561 return MAKE_ERR;
2562 }
2563 }
2564 }
2565 }
2566
Angel Ponsa93f46e2020-11-17 16:54:01 +01002567 /* Restore nominal Vref after training */
2568 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002569 printram("CPA\n");
2570 return 0;
2571}
2572
2573int discover_edges_write(ramctr_timing *ctrl)
2574{
2575 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002576 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2577 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002578
Angel Pons7c49cb82020-03-16 23:17:32 +01002579 /*
2580 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2581 * also use a single loop. It would seem that it is a debugging configuration.
2582 */
Angel Pons88521882020-01-05 20:21:20 +01002583 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2584 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002585
2586 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2587 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002588 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002589 if (err)
2590 return err;
2591 }
2592
Angel Pons88521882020-01-05 20:21:20 +01002593 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2594 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002595
2596 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2597 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002598 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002599 if (err)
2600 return err;
2601 }
2602
Angel Pons88521882020-01-05 20:21:20 +01002603 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002604
2605 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2606 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002607 falling_edges[channel][slotrank][lane];
2608
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002609 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002610 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002611 }
2612
2613 FOR_ALL_POPULATED_CHANNELS
2614 program_timings(ctrl, channel);
2615
Angel Pons50a6fe72020-11-14 01:18:14 +01002616 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002617 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002618 }
2619 return 0;
2620}
2621
2622static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2623{
Angel Pons88521882020-01-05 20:21:20 +01002624 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002625
Angel Ponsffd50152020-11-12 11:03:10 +01002626 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002627
Angel Pons7c49cb82020-03-16 23:17:32 +01002628 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002629 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002630
Angel Pons88521882020-01-05 20:21:20 +01002631 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002632}
2633
2634int discover_timC_write(ramctr_timing *ctrl)
2635{
Angel Pons7c49cb82020-03-16 23:17:32 +01002636 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002637 int i, pat;
2638
2639 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2640 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2641 int channel, slotrank, lane;
2642
2643 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2644 lower[channel][slotrank][lane] = 0;
2645 upper[channel][slotrank][lane] = MAX_TIMC;
2646 }
2647
Angel Pons88521882020-01-05 20:21:20 +01002648 /*
2649 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2650 * FIXME: This must only be done on Ivy Bridge.
2651 */
2652 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002653 printram("discover timC write:\n");
2654
2655 for (i = 0; i < 3; i++)
2656 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002657
2658 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2659 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2660 ~0x3f000000, rege3c_b24[i] << 24);
2661
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002662 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002663
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002664 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2665 FOR_ALL_POPULATED_RANKS {
2666 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002667 u32 raw_stats[MAX_TIMC + 1];
2668 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002669
2670 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002671 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002672
2673 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002674
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002675 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002676 FOR_ALL_LANES {
2677 ctrl->timings[channel][slotrank]
2678 .lanes[lane].timC = timC;
2679 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002680 program_timings(ctrl, channel);
2681
2682 test_timC_write (ctrl, channel, slotrank);
2683
Angel Pons7c49cb82020-03-16 23:17:32 +01002684 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002685 raw_stats[timC] = MCHBAR32(
2686 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002687 }
2688 FOR_ALL_LANES {
2689 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002690 for (timC = 0; timC < MAX_TIMC; timC++) {
2691 stats[timC] = !!(raw_stats[timC]
2692 & (1 << lane));
2693 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002694
Angel Pons7c49cb82020-03-16 23:17:32 +01002695 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002696 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002697 printk(BIOS_EMERG,
2698 "timC write discovery failed: "
2699 "%d, %d, %d\n", channel,
2700 slotrank, lane);
2701
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002702 return MAKE_ERR;
2703 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002704 printram("timC: %d, %d, %d: "
2705 "0x%02x-0x%02x-0x%02x, "
2706 "0x%02x-0x%02x\n", channel, slotrank,
2707 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002708 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002709 rn.end - ctrl->timC_offset[i]);
2710
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002711 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002712 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002713 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002714
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002715 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002716 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002717 upper[channel][slotrank][lane]);
2718
2719 }
2720 }
2721 }
2722 }
2723
2724 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002725 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002726 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002727 udelay(2);
2728 }
2729
Angel Pons88521882020-01-05 20:21:20 +01002730 /*
2731 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2732 * FIXME: This must only be done on Ivy Bridge.
2733 */
2734 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002735
2736 printram("CPB\n");
2737
2738 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002739 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002740 (lower[channel][slotrank][lane] +
2741 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002742
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002743 ctrl->timings[channel][slotrank].lanes[lane].timC =
2744 (lower[channel][slotrank][lane] +
2745 upper[channel][slotrank][lane]) / 2;
2746 }
2747 FOR_ALL_POPULATED_CHANNELS {
2748 program_timings(ctrl, channel);
2749 }
2750 return 0;
2751}
2752
Angel Pons88521882020-01-05 20:21:20 +01002753void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002754{
2755 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002756 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002757
2758 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2759 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002760 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002761 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002762 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002763 printram("normalize %d, %d, %d: mat %d\n",
2764 channel, slotrank, lane, mat);
2765
Felix Heldef4fe3e2019-12-31 14:15:05 +01002766 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002767 printram("normalize %d, %d, %d: delta %d\n",
2768 channel, slotrank, lane, delta);
2769
Angel Pons88521882020-01-05 20:21:20 +01002770 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002771 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002772 }
2773
2774 FOR_ALL_POPULATED_CHANNELS {
2775 program_timings(ctrl, channel);
2776 }
2777}
2778
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002779int channel_test(ramctr_timing *ctrl)
2780{
2781 int channel, slotrank, lane;
2782
2783 slotrank = 0;
2784 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002785 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002786 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002787 return MAKE_ERR;
2788 }
2789 FOR_ALL_POPULATED_CHANNELS {
2790 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002791 }
2792
2793 for (slotrank = 0; slotrank < 4; slotrank++)
2794 FOR_ALL_CHANNELS
2795 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2796 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002797 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2798 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002799 }
Angel Pons88521882020-01-05 20:21:20 +01002800 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002801
Angel Ponsffd50152020-11-12 11:03:10 +01002802 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002803
Angel Pons7c49cb82020-03-16 23:17:32 +01002804 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002805 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002806
Angel Pons88521882020-01-05 20:21:20 +01002807 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002808 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002809 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002810 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2811 channel, slotrank, lane);
2812 return MAKE_ERR;
2813 }
2814 }
2815 return 0;
2816}
2817
Patrick Rudolphdd662872017-10-28 18:20:11 +02002818void channel_scrub(ramctr_timing *ctrl)
2819{
2820 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002821 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002822
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002823 FOR_ALL_POPULATED_CHANNELS {
2824 wait_for_iosav(channel);
2825 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002826 }
2827
2828 /*
2829 * During runtime the "scrubber" will periodically scan through the memory in the
2830 * physical address space, to identify and fix CRC errors.
2831 * The following loops writes to every DRAM address, setting the ECC bits to the
2832 * correct value. A read from this location will no longer return a CRC error,
2833 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002834 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002835 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2836 * and firmware running in x86_32.
2837 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002838 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2839 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002840 for (bank = 0; bank < 8; bank++) {
2841 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002842
Angel Pons8f0757e2020-11-11 23:03:36 +01002843 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2844 const struct iosav_ssq sequence[] = {
2845 /*
2846 * DRAM command ACT
2847 * Opens the row for writing.
2848 */
2849 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002850 .sp_cmd_ctrl = {
2851 .command = IOSAV_ACT,
2852 .ranksel_ap = 1,
2853 },
2854 .subseq_ctrl = {
2855 .cmd_executions = 1,
2856 .cmd_delay_gap = gap,
2857 .post_ssq_wait = ctrl->tRCD,
2858 .data_direction = SSQ_NA,
2859 },
2860 .sp_cmd_addr = {
2861 .address = row,
2862 .rowbits = 6,
2863 .bank = bank,
2864 .rank = slotrank,
2865 },
2866 .addr_update = {
2867 .inc_addr_1 = 1,
2868 .addr_wrap = 18,
2869 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002870 },
2871 /*
2872 * DRAM command WR
2873 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2874 * bytes.
2875 */
2876 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002877 .sp_cmd_ctrl = {
2878 .command = IOSAV_WR,
2879 .ranksel_ap = 1,
2880 },
2881 .subseq_ctrl = {
2882 .cmd_executions = 129,
2883 .cmd_delay_gap = 4,
2884 .post_ssq_wait = ctrl->tWTR +
2885 ctrl->CWL + 8,
2886 .data_direction = SSQ_WR,
2887 },
2888 .sp_cmd_addr = {
2889 .address = row,
2890 .rowbits = 0,
2891 .bank = bank,
2892 .rank = slotrank,
2893 },
2894 .addr_update = {
2895 .inc_addr_8 = 1,
2896 .addr_wrap = 9,
2897 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002898 },
2899 /*
2900 * DRAM command PRE
2901 * Closes the row.
2902 */
2903 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002904 .sp_cmd_ctrl = {
2905 .command = IOSAV_PRE,
2906 .ranksel_ap = 1,
2907 },
2908 .subseq_ctrl = {
2909 .cmd_executions = 1,
2910 .cmd_delay_gap = 4,
2911 .post_ssq_wait = ctrl->tRP,
2912 .data_direction = SSQ_NA,
2913 },
2914 .sp_cmd_addr = {
2915 .address = 0,
2916 .rowbits = 6,
2917 .bank = bank,
2918 .rank = slotrank,
2919 },
2920 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002921 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002922 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002923 },
2924 };
2925 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002926
2927 /* Execute command queue */
2928 iosav_run_queue(channel, 16, 0);
2929
2930 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002931 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002932 }
2933 }
2934}
2935
Angel Pons88521882020-01-05 20:21:20 +01002936void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002937{
2938 int channel;
2939
Angel Pons7c49cb82020-03-16 23:17:32 +01002940 /* 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 +01002941 static u32 seeds[NUM_CHANNELS][3] = {
2942 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2943 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2944 };
2945 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002946 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002947 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2948 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2949 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002950 }
2951}
2952
Angel Pons89ae6b82020-03-21 13:23:32 +01002953void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002954{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002955 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002956 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002957 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002958 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002959 }
2960}
2961
Angel Pons88521882020-01-05 20:21:20 +01002962void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002963{
2964 int channel;
2965
2966 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002967 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002968 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002969 }
2970
2971 udelay(1);
2972
2973 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002974 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002975 }
2976}
2977
Angel Pons7c49cb82020-03-16 23:17:32 +01002978void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002979{
2980 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002981
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002982 FOR_ALL_POPULATED_CHANNELS {
2983 u32 b20, b4_8_12;
Angel Pons88521882020-01-05 20:21:20 +01002984 int min_pi = 10000;
2985 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002986
2987 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002988 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2989 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002990 }
2991
Angel Pons7c49cb82020-03-16 23:17:32 +01002992 b20 = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002993
Angel Pons7c49cb82020-03-16 23:17:32 +01002994 b4_8_12 = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 0x3330 : 0x2220;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002995
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002996 dram_odt_stretch(ctrl, channel);
2997
Angel Ponsdc5539f2020-11-12 12:44:25 +01002998 MCHBAR32(TC_RWP_ch(channel)) = (1 << 27) | (2 << 24) | (b20 << 20) |
Felix Held2463aa92018-07-29 21:37:55 +02002999 ((ctrl->ref_card_offset[channel] + 2) << 16) | b4_8_12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003000 }
3001}
3002
Angel Pons88521882020-01-05 20:21:20 +01003003void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003004{
3005 int channel;
3006 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01003007 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
3008 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003009 }
3010}
3011
Angel Pons7c49cb82020-03-16 23:17:32 +01003012/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
3013static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003014{
Angel Pons88521882020-01-05 20:21:20 +01003015 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003016}
3017
Angel Pons7c49cb82020-03-16 23:17:32 +01003018/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01003019void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003020{
Angel Ponsb50ca572020-11-11 19:07:20 +01003021 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01003022
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003023 int channel;
3024 int t1_cycles = 0, t1_ns = 0, t2_ns;
3025 int t3_ns;
3026 u32 r32;
3027
Angel Pons7c49cb82020-03-16 23:17:32 +01003028 /* FIXME: This register only exists on Ivy Bridge */
3029 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003030
Felix Heldf9b826a2018-07-30 17:56:52 +02003031 FOR_ALL_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003032 MCHBAR32_AND_OR(TC_OTHP_ch(channel), ~(3 << 12), 1 << 12);
Patrick Rudolph652c4912017-10-31 11:36:55 +01003033
Patrick Rudolph74203de2017-11-20 11:57:01 +01003034 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003035 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003036 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003037 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003038 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003039 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003040
Felix Heldf9b826a2018-07-30 17:56:52 +02003041 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003042 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003043
Angel Pons88521882020-01-05 20:21:20 +01003044 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3045 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003046
3047 FOR_ALL_CHANNELS {
3048 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003049 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003050 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003051 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003052 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003053 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003054 case 1:
3055 case 4:
3056 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003057 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003058 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003059 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003060 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003061 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003062 break;
3063 }
3064 }
3065
Felix Held50b7ed22019-12-30 20:41:54 +01003066 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003067 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003068 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003069
3070 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003071 MCHBAR32_AND_OR(TC_RFP_ch(channel), ~(3 << 16), 1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003072
Angel Ponsdc5539f2020-11-12 12:44:25 +01003073 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3074 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003075 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003076
Angel Pons7c49cb82020-03-16 23:17:32 +01003077 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003078 FOR_ALL_POPULATED_CHANNELS
3079 break;
3080
Angel Pons88521882020-01-05 20:21:20 +01003081 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3082 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003083 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003084 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003085 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003086 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003087 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003088 t1_ns += 500;
3089
Angel Pons88521882020-01-05 20:21:20 +01003090 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003091 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003092 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003093 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003094 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003095 t3_ns = 500;
3096 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003097
3098 /* The graphics driver will use these watermark values */
3099 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003100 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003101 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3102 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003103}
3104
Angel Pons88521882020-01-05 20:21:20 +01003105void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003106{
Angel Pons50a6fe72020-11-14 01:18:14 +01003107 int channel, lane;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003108
Angel Pons7c49cb82020-03-16 23:17:32 +01003109 FOR_ALL_POPULATED_CHANNELS {
3110 MCHBAR32(TC_RAP_ch(channel)) =
3111 (ctrl->tRRD << 0)
3112 | (ctrl->tRTP << 4)
3113 | (ctrl->tCKE << 8)
3114 | (ctrl->tWTR << 12)
3115 | (ctrl->tFAW << 16)
3116 | (ctrl->tWR << 24)
3117 | (ctrl->cmd_stretch[channel] << 30);
3118 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003119
3120 udelay(1);
3121
3122 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003123 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003124 }
3125
Angel Pons50a6fe72020-11-14 01:18:14 +01003126 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003127 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003128 }
3129
3130 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003131 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003132
3133 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003134 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003135 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003136 }
3137
3138 printram("CPE\n");
3139
Angel Pons88521882020-01-05 20:21:20 +01003140 MCHBAR32(GDCRTRAININGMOD) = 0;
3141 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003142
3143 printram("CP5b\n");
3144
3145 FOR_ALL_POPULATED_CHANNELS {
3146 program_timings(ctrl, channel);
3147 }
3148
3149 u32 reg, addr;
3150
Angel Pons7c49cb82020-03-16 23:17:32 +01003151 /* Poll for RCOMP */
3152 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3153 ;
3154
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003155 do {
Angel Pons88521882020-01-05 20:21:20 +01003156 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003157 } while ((reg & 0x14) == 0);
3158
Angel Pons7c49cb82020-03-16 23:17:32 +01003159 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003160 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003161 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003162
Angel Pons7c49cb82020-03-16 23:17:32 +01003163 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003164 udelay(500);
3165
3166 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003167 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003168 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003169 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003170 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003171 MCHBAR32(addr) = reg;
3172
Angel Pons7c49cb82020-03-16 23:17:32 +01003173 /* Wait 10ns for ranks to settle */
3174 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003175
3176 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3177 MCHBAR32(addr) = reg;
3178
Angel Pons7c49cb82020-03-16 23:17:32 +01003179 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003180 write_reset(ctrl);
3181 }
3182
Angel Pons7c49cb82020-03-16 23:17:32 +01003183 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003184 dram_mrscommands(ctrl);
3185
3186 printram("CP5c\n");
3187
Angel Pons88521882020-01-05 20:21:20 +01003188 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003189
3190 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003191 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003192 udelay(2);
3193 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003194}