blob: 92d0c4f47d746dd535e4b4e7e89a1c89b6d98f1a [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002
Angel Pons12bd8ab2020-11-13 23:10:52 +01003#include <assert.h>
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01004#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <console/console.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01006#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05307#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02008#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02009#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010010#include <northbridge/intel/sandybridge/chip.h>
11#include <device/pci_def.h>
12#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020013#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020014
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010015#include "raminit_native.h"
16#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010017#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010018#include "sandybridge.h"
19
Angel Pons7c49cb82020-03-16 23:17:32 +010020/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010021
22static void sfence(void)
23{
24 asm volatile ("sfence");
25}
26
Angel Pons7c49cb82020-03-16 23:17:32 +010027/* Toggle IO reset bit */
28static void toggle_io_reset(void)
29{
Angel Pons88521882020-01-05 20:21:20 +010030 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010031 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010032 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010033 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010034 udelay(1);
35}
36
37static u32 get_XOVER_CLK(u8 rankmap)
38{
39 return rankmap << 24;
40}
41
42static u32 get_XOVER_CMD(u8 rankmap)
43{
44 u32 reg;
45
Angel Pons7c49cb82020-03-16 23:17:32 +010046 /* Enable xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010047 reg = 0x4000;
48
Angel Pons7c49cb82020-03-16 23:17:32 +010049 /* Enable xover ctl */
50 if (rankmap & 0x03)
51 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010052
Angel Pons7c49cb82020-03-16 23:17:32 +010053 if (rankmap & 0x0c)
54 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010055
56 return reg;
57}
58
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010059void dram_find_common_params(ramctr_timing *ctrl)
60{
61 size_t valid_dimms;
62 int channel, slot;
63 dimm_info *dimms = &ctrl->info;
64
65 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
66 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010067
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010068 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010069
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010070 const dimm_attr *dimm = &dimms->dimm[channel][slot];
71 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
72 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010073
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010074 valid_dimms++;
75
76 /* Find all possible CAS combinations */
77 ctrl->cas_supported &= dimm->cas_supported;
78
79 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010080 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
81 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
82 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010083 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
84 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010085 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010086 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
87 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
88 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
89 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
90 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030091 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
92 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010093 }
94
95 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010096 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
97
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010098 if (!valid_dimms)
99 die("No valid DIMMs found");
100}
101
Angel Pons88521882020-01-05 20:21:20 +0100102void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100103{
104 u32 reg;
105 int channel;
106
107 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100108 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100109 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100110 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
111 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100112
Angel Pons7c49cb82020-03-16 23:17:32 +0100113 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100114 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100115 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
116 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100117 }
118}
119
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100120static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100121{
Angel Pons89ae6b82020-03-21 13:23:32 +0100122 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100123
124 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 /*
126 * ODT stretch:
127 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
128 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100129 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100130 if (stretch == 2)
131 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100132
Angel Pons88521882020-01-05 20:21:20 +0100133 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100134 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100136 } else {
Angel Pons88521882020-01-05 20:21:20 +0100137 addr = TC_OTHP_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100138 MCHBAR32_AND_OR(addr, ~(0xf << 16), (stretch << 16) | (stretch << 18));
Iru Cai89af71c2018-08-16 16:46:27 +0800139 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100140 }
141}
142
143void dram_timing_regs(ramctr_timing *ctrl)
144{
145 u32 reg, addr, val32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100146 int channel;
147
148 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100149 /* BIN parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100150 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100151 reg |= (ctrl->tRCD << 0);
152 reg |= (ctrl->tRP << 4);
153 reg |= (ctrl->CAS << 8);
154 reg |= (ctrl->CWL << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100155 reg |= (ctrl->tRAS << 16);
Angel Pons88521882020-01-05 20:21:20 +0100156 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), reg);
157 MCHBAR32(TC_DBP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100158
Angel Pons7c49cb82020-03-16 23:17:32 +0100159 /* Regular access parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100160 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100161 reg |= (ctrl->tRRD << 0);
162 reg |= (ctrl->tRTP << 4);
163 reg |= (ctrl->tCKE << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100164 reg |= (ctrl->tWTR << 12);
165 reg |= (ctrl->tFAW << 16);
Angel Pons7c49cb82020-03-16 23:17:32 +0100166 reg |= (ctrl->tWR << 24);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100167 reg |= (3 << 30);
Angel Pons88521882020-01-05 20:21:20 +0100168 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), reg);
169 MCHBAR32(TC_RAP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100170
Angel Pons7c49cb82020-03-16 23:17:32 +0100171 /* Other parameters */
Angel Pons88521882020-01-05 20:21:20 +0100172 addr = TC_OTHP_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100173 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100174 reg |= (ctrl->tXPDLL << 0);
175 reg |= (ctrl->tXP << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100176 reg |= (ctrl->tAONPD << 8);
177 reg |= 0xa0000;
178 printram("OTHP [%x] = %x\n", addr, reg);
179 MCHBAR32(addr) = reg;
180
Angel Ponsca2f68a2020-03-22 13:15:12 +0100181 /* Debug parameters - only applies to Ivy Bridge */
182 if (IS_IVY_CPU(ctrl->cpu)) {
183 reg = 0;
184
185 /*
186 * If tXP and tXPDLL are very high, we need to increase them by one.
187 * This can only happen on Ivy Bridge, and when overclocking the RAM.
188 */
189 if (ctrl->tXP >= 8)
190 reg |= (1 << 12);
191
192 if (ctrl->tXPDLL >= 32)
193 reg |= (1 << 13);
194
195 MCHBAR32(TC_DTP_ch(channel)) = reg;
196 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100197
Felix Held9fe248f2018-07-31 20:59:45 +0200198 MCHBAR32_OR(addr, 0x00020000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100199
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100200 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100201
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100202 /*
Angel Pons7c49cb82020-03-16 23:17:32 +0100203 * TC-Refresh timing parameters:
204 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
205 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100206 */
207 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
208
Angel Pons7c49cb82020-03-16 23:17:32 +0100209 reg = ((ctrl->tREFI & 0xffff) << 0) |
210 ((ctrl->tRFC & 0x01ff) << 16) | (((val32 / 1024) & 0x7f) << 25);
211
Angel Pons88521882020-01-05 20:21:20 +0100212 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), reg);
213 MCHBAR32(TC_RFTP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100214
Angel Ponsdc5539f2020-11-12 12:44:25 +0100215 MCHBAR32_OR(TC_RFP_ch(channel), 0xff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100216
Angel Pons7c49cb82020-03-16 23:17:32 +0100217 /* Self-refresh timing parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100218 reg = 0;
219 val32 = tDLLK;
Angel Pons7c49cb82020-03-16 23:17:32 +0100220 reg = (reg & ~0x00000fff) | (val32 << 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100221 val32 = ctrl->tXSOffset;
Angel Pons7c49cb82020-03-16 23:17:32 +0100222 reg = (reg & ~0x0000f000) | (val32 << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100223 val32 = tDLLK - ctrl->tXSOffset;
Angel Pons7c49cb82020-03-16 23:17:32 +0100224 reg = (reg & ~0x03ff0000) | (val32 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100225 val32 = ctrl->tMOD - 8;
Angel Pons7c49cb82020-03-16 23:17:32 +0100226 reg = (reg & ~0xf0000000) | (val32 << 28);
227 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), reg);
Angel Pons88521882020-01-05 20:21:20 +0100228 MCHBAR32(TC_SRFTP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100229 }
230}
231
232void dram_dimm_mapping(ramctr_timing *ctrl)
233{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100234 int channel;
235 dimm_info *info = &ctrl->info;
236
237 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200238 dimm_attr *dimmA, *dimmB;
239 u32 reg = 0;
240
Angel Pons7c49cb82020-03-16 23:17:32 +0100241 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100242 dimmA = &info->dimm[channel][0];
243 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100244 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100245 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100246 dimmA = &info->dimm[channel][1];
247 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100248 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100249 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100250
Nico Huberac4f2162017-10-01 18:14:43 +0200251 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100252 reg |= (dimmA->size_mb / 256) << 0;
253 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200254 reg |= (dimmA->width / 8 - 1) << 19;
255 }
256
257 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100258 reg |= (dimmB->size_mb / 256) << 8;
259 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200260 reg |= (dimmB->width / 8 - 1) << 20;
261 }
262
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200263 /*
264 * Rank interleave: Bit 16 of the physical address space sets
265 * the rank to use in a dual single rank DIMM configuration.
266 * That results in every 64KiB being interleaved between two ranks.
267 */
268 reg |= 1 << 21;
269 /* Enhanced interleave */
270 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200271
Angel Pons7c49cb82020-03-16 23:17:32 +0100272 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100273 ctrl->mad_dimm[channel] = reg;
274 } else {
275 ctrl->mad_dimm[channel] = 0;
276 }
277 }
278}
279
Patrick Rudolphdd662872017-10-28 18:20:11 +0200280void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100281{
282 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200283 u32 ecc;
284
285 if (ctrl->ecc_enabled)
286 ecc = training ? (1 << 24) : (3 << 24);
287 else
288 ecc = 0;
289
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100290 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200291 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100292 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200293
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200294 if (ctrl->ecc_enabled)
295 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100296}
297
Angel Pons88521882020-01-05 20:21:20 +0100298void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100299{
300 u32 reg, ch0size, ch1size;
301 u8 val;
302 reg = 0;
303 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100304
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100305 if (training) {
306 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
307 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
308 } else {
309 ch0size = ctrl->channel_size_mb[0];
310 ch1size = ctrl->channel_size_mb[1];
311 }
312
313 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100314 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100315 val = ch1size / 256;
316 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100317 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100318 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100319 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100320
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100321 } else {
Angel Pons88521882020-01-05 20:21:20 +0100322 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100323 val = ch0size / 256;
324 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100325 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100326 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100327 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100328 }
329}
330
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100331#define DEFAULT_PCI_MMIO_SIZE 2048
332
333static unsigned int get_mmio_size(void)
334{
335 const struct device *dev;
336 const struct northbridge_intel_sandybridge_config *cfg = NULL;
337
Angel Ponsb31d1d72020-01-10 01:35:09 +0100338 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100339 if (dev)
340 cfg = dev->chip_info;
341
342 /* If this is zero, it just means devicetree.cb didn't set it */
343 if (!cfg || cfg->pci_mmio_size == 0)
344 return DEFAULT_PCI_MMIO_SIZE;
345 else
346 return cfg->pci_mmio_size;
347}
348
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200349/*
350 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
351 * The ME/PCU/.. has the ability to change this.
352 * Return 0: ECC is optional
353 * Return 1: ECC is forced
354 */
355bool get_host_ecc_forced(void)
356{
357 /* read Capabilities A Register */
358 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
359 return !!(reg32 & (1 << 24));
360}
361
362/*
363 * Returns the ECC capability.
364 * The ME/PCU/.. has the ability to change this.
365 * Return 0: ECC is disabled
366 * Return 1: ECC is possible
367 */
368bool get_host_ecc_cap(void)
369{
370 /* read Capabilities A Register */
371 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
372 return !(reg32 & (1 << 25));
373}
374
Angel Pons88521882020-01-05 20:21:20 +0100375void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100376{
Angel Pons7c49cb82020-03-16 23:17:32 +0100377 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
378 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
379 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100380 uint16_t ggc;
381
382 mmiosize = get_mmio_size();
383
Felix Held87ddea22020-01-26 04:55:27 +0100384 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100385 if (!(ggc & 2)) {
386 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100387 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100388 } else {
389 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100390 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100391 }
392
393 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
394
395 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
396
397 mestolenbase = tom - me_uma_size;
398
Angel Pons7c49cb82020-03-16 23:17:32 +0100399 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
400
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100401 gfxstolenbase = toludbase - gfxstolen;
402 gttbase = gfxstolenbase - gttsize;
403
404 tsegbase = gttbase - tsegsize;
405
Angel Pons7c49cb82020-03-16 23:17:32 +0100406 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100407 tsegbasedelta = tsegbase & (tsegsize - 1);
408 tsegbase &= ~(tsegsize - 1);
409
410 gttbase -= tsegbasedelta;
411 gfxstolenbase -= tsegbasedelta;
412 toludbase -= tsegbasedelta;
413
Angel Pons7c49cb82020-03-16 23:17:32 +0100414 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100415 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100416 /* Reclaim is possible */
417 reclaim = 1;
418 remapbase = MAX(4096, tom - me_uma_size);
419 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
420 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100421 } else {
422 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100423 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100424 touudbase = tom - me_uma_size;
425 }
426
Angel Pons7c49cb82020-03-16 23:17:32 +0100427 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100428 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
429
Angel Pons7c49cb82020-03-16 23:17:32 +0100430 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100431 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100432 val = tom & 0xfff;
433 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100434 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100435 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100436
Angel Ponsb31d1d72020-01-10 01:35:09 +0100437 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100438 val = tom & 0xfffff000;
439 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100440 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100441 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100442
Angel Pons7c49cb82020-03-16 23:17:32 +0100443 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100444 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100445 val = toludbase & 0xfff;
446 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100447 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100448 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100449
Angel Pons7c49cb82020-03-16 23:17:32 +0100450 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100451 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100452 val = touudbase & 0xfff;
453 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100454 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100455 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100456
Angel Pons7c49cb82020-03-16 23:17:32 +0100457 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100458 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100459 val = touudbase & 0xfffff000;
460 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100461 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100462 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100463
464 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100465 /* REMAP BASE */
466 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100467 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100468
Angel Pons7c49cb82020-03-16 23:17:32 +0100469 /* REMAP LIMIT */
470 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100471 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100472 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100473 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100474 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100475 val = tsegbase & 0xfff;
476 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100477 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100478 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100479
Angel Pons7c49cb82020-03-16 23:17:32 +0100480 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100481 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100482 val = gfxstolenbase & 0xfff;
483 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100484 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100485 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100486
Angel Pons7c49cb82020-03-16 23:17:32 +0100487 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100488 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100489 val = gttbase & 0xfff;
490 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100491 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100492 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100493
494 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100495 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100496 val = (0x80000 - me_uma_size) & 0xfffff000;
497 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100498 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100499 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100500
Angel Pons7c49cb82020-03-16 23:17:32 +0100501 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100502 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100503 val = mestolenbase & 0xfff;
504 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100505 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100506 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100507
Angel Ponsb31d1d72020-01-10 01:35:09 +0100508 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100509 val = mestolenbase & 0xfffff000;
510 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100511 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100512 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100513
Angel Pons7c49cb82020-03-16 23:17:32 +0100514 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100515 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100516 val = (0x80000 - me_uma_size) & 0xfff;
517 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100518 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
519 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100520 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100521 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100522 }
523}
524
Angel Pons88521882020-01-05 20:21:20 +0100525static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100526{
527 int channel, slotrank;
528
Angel Pons7c49cb82020-03-16 23:17:32 +0100529 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100530 channel = (ctrl->rankmap[0]) ? 0 : 1;
531
Angel Pons88521882020-01-05 20:21:20 +0100532 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100533
Angel Pons7c49cb82020-03-16 23:17:32 +0100534 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100535 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
536
Angel Ponsffd50152020-11-12 11:03:10 +0100537 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100538
Angel Pons7c49cb82020-03-16 23:17:32 +0100539 /*
540 * Execute command queue - why is bit 22 set here?!
541 *
542 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
543 */
Angel Pons38d901e2020-05-02 23:50:43 +0200544 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200545
Angel Pons88521882020-01-05 20:21:20 +0100546 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100547}
548
Angel Pons88521882020-01-05 20:21:20 +0100549void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100550{
Felix Held9fe248f2018-07-31 20:59:45 +0200551 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100552 int channel;
553
Angel Pons7c49cb82020-03-16 23:17:32 +0100554 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
555 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100556 do {
Angel Pons88521882020-01-05 20:21:20 +0100557 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100558 } while ((reg & 0x14) == 0);
559
Angel Pons7c49cb82020-03-16 23:17:32 +0100560 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100561 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100562 MCHBAR32(MC_INIT_STATE_G) = reg;
563 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100565 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566
Angel Pons7c49cb82020-03-16 23:17:32 +0100567 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100568 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 udelay(200);
572
Angel Pons7c49cb82020-03-16 23:17:32 +0100573 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100574 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100575
Angel Pons7c49cb82020-03-16 23:17:32 +0100576 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100577 udelay(500);
578
Angel Pons7c49cb82020-03-16 23:17:32 +0100579 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100580 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581
Angel Pons7c49cb82020-03-16 23:17:32 +0100582 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100583 udelay(1);
584
585 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200587 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100588 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100589
Angel Pons7c49cb82020-03-16 23:17:32 +0100590 /* Wait 10ns for ranks to settle */
591 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100592
593 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100594 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100595
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100597 write_reset(ctrl);
598 }
599}
600
Angel Pons3d3bf482020-11-14 16:18:15 +0100601/*
602 * DDR3 Rank1 Address mirror swap the following pins:
603 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1
604 */
605static void ddr3_mirror_mrreg(int *bank, u32 *addr)
606{
607 *bank = ((*bank >> 1) & 1) | ((*bank << 1) & 2);
608 *addr = (*addr & ~0x1f8) | ((*addr >> 1) & 0xa8) | ((*addr & 0xa8) << 1);
609}
610
Angel Pons7c49cb82020-03-16 23:17:32 +0100611static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100612{
Angel Pons88521882020-01-05 20:21:20 +0100613 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100614
Angel Pons3d3bf482020-11-14 16:18:15 +0100615 if (ctrl->rank_mirror[channel][slotrank])
616 ddr3_mirror_mrreg(&reg, &val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100617
Angel Pons8f0757e2020-11-11 23:03:36 +0100618 const struct iosav_ssq sequence[] = {
619 /* DRAM command MRS */
620 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200621 .sp_cmd_ctrl = {
622 .command = IOSAV_MRS,
623 },
624 .subseq_ctrl = {
625 .cmd_executions = 1,
626 .cmd_delay_gap = 4,
627 .post_ssq_wait = 4,
628 .data_direction = SSQ_NA,
629 },
630 .sp_cmd_addr = {
631 .address = val,
632 .rowbits = 6,
633 .bank = reg,
634 .rank = slotrank,
635 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100636 },
637 /* DRAM command MRS */
638 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200639 .sp_cmd_ctrl = {
640 .command = IOSAV_MRS,
641 .ranksel_ap = 1,
642 },
643 .subseq_ctrl = {
644 .cmd_executions = 1,
645 .cmd_delay_gap = 4,
646 .post_ssq_wait = 4,
647 .data_direction = SSQ_NA,
648 },
649 .sp_cmd_addr = {
650 .address = val,
651 .rowbits = 6,
652 .bank = reg,
653 .rank = slotrank,
654 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100655 },
656 /* DRAM command MRS */
657 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200658 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100659 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200660 },
661 .subseq_ctrl = {
662 .cmd_executions = 1,
663 .cmd_delay_gap = 4,
664 .post_ssq_wait = ctrl->tMOD,
665 .data_direction = SSQ_NA,
666 },
667 .sp_cmd_addr = {
668 .address = val,
669 .rowbits = 6,
670 .bank = reg,
671 .rank = slotrank,
672 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100673 },
674 };
675 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200676
Angel Pons7c49cb82020-03-16 23:17:32 +0100677 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200678 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100679}
680
Angel Pons88521882020-01-05 20:21:20 +0100681static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100682{
683 u16 mr0reg, mch_cas, mch_wr;
684 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Patrick Rudolph74203de2017-11-20 11:57:01 +0100685 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100686
Angel Pons7c49cb82020-03-16 23:17:32 +0100687 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100688 if (ctrl->CAS < 12) {
689 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
690 } else {
691 mch_cas = (u16) (ctrl->CAS - 12);
692 mch_cas = ((mch_cas << 1) | 0x1);
693 }
694
Angel Pons7c49cb82020-03-16 23:17:32 +0100695 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100696 mch_wr = mch_wr_t[ctrl->tWR - 5];
697
Angel Pons2bf28ed2020-11-12 13:49:59 +0100698 /* DLL Reset - self clearing - set after CLK frequency has been changed */
699 mr0reg = 1 << 8;
700
701 mr0reg |= (mch_cas & 0x1) << 2;
702 mr0reg |= (mch_cas & 0xe) << 3;
703 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100704
Angel Pons7c49cb82020-03-16 23:17:32 +0100705 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
Angel Pons2bf28ed2020-11-12 13:49:59 +0100706 mr0reg |= !is_mobile << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100707 return mr0reg;
708}
709
710static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
711{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200712 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100713}
714
Angel Ponsf9997482020-11-12 16:02:52 +0100715static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100716{
717 /* Get ODT based on rankmap */
718 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
719
720 if (dimms_per_ch == 1) {
721 return (const odtmap){60, 60};
722 } else {
723 return (const odtmap){120, 30};
724 }
725}
726
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100727static u32 encode_odt(u32 odt)
728{
729 switch (odt) {
730 case 30:
731 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
732 case 60:
733 return (1 << 2); // RZQ/4
734 case 120:
735 return (1 << 6); // RZQ/2
736 default:
737 case 0:
738 return 0;
739 }
740}
741
742static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
743{
744 odtmap odt;
745 u32 mr1reg;
746
Angel Ponsf9997482020-11-12 16:02:52 +0100747 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100748 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100749
750 mr1reg |= encode_odt(odt.rttnom);
751
752 return mr1reg;
753}
754
755static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
756{
757 u16 mr1reg;
758
759 mr1reg = make_mr1(ctrl, rank, channel);
760
761 write_mrreg(ctrl, channel, rank, 1, mr1reg);
762}
763
764static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
765{
Angel Pons868bca22020-11-13 13:38:04 +0100766 const u16 pasr = 0;
767 const u16 cwl = ctrl->CWL - 5;
768 const odtmap odt = get_ODT(ctrl, channel);
769
Angel Ponsdca3cb52020-11-13 13:42:07 +0100770 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100771 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
772 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100773
Angel Pons868bca22020-11-13 13:38:04 +0100774 u16 mr2reg = 0;
775 mr2reg |= pasr;
776 mr2reg |= cwl << 3;
777 mr2reg |= ctrl->auto_self_refresh << 6;
778 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100779 mr2reg |= (odt.rttwr / 60) << 9;
780
781 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100782
783 /* Program MR2 shadow */
784 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
785
786 reg32 &= 3 << 14 | 3 << 6;
787
788 reg32 |= mr2reg & ~(3 << 6);
789
790 if (rank & 1) {
791 if (srt)
792 reg32 |= 1 << (rank / 2 + 6);
793 } else {
794 if (ctrl->rank_mirror[channel][rank])
795 reg32 |= 1 << (rank / 2 + 14);
796 }
797 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100798}
799
800static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
801{
802 write_mrreg(ctrl, channel, rank, 3, 0);
803}
804
Angel Pons88521882020-01-05 20:21:20 +0100805void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100806{
807 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100808 int channel;
809
810 FOR_ALL_POPULATED_CHANNELS {
811 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100812 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100813 dram_mr2(ctrl, slotrank, channel);
814
Angel Pons7c49cb82020-03-16 23:17:32 +0100815 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100816 dram_mr3(ctrl, slotrank, channel);
817
Angel Pons7c49cb82020-03-16 23:17:32 +0100818 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100819 dram_mr1(ctrl, slotrank, channel);
820
Angel Pons7c49cb82020-03-16 23:17:32 +0100821 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100822 dram_mr0(ctrl, slotrank, channel);
823 }
824 }
825
Angel Pons8f0757e2020-11-11 23:03:36 +0100826 const struct iosav_ssq zqcl_sequence[] = {
827 /* DRAM command NOP (without ODT nor chip selects) */
828 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200829 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100830 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200831 },
832 .subseq_ctrl = {
833 .cmd_executions = 1,
834 .cmd_delay_gap = 4,
835 .post_ssq_wait = 15,
836 .data_direction = SSQ_NA,
837 },
838 .sp_cmd_addr = {
839 .address = 2,
840 .rowbits = 6,
841 .bank = 0,
842 .rank = 0,
843 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100844 },
845 /* DRAM command ZQCL */
846 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200847 .sp_cmd_ctrl = {
848 .command = IOSAV_ZQCS,
849 .ranksel_ap = 1,
850 },
851 .subseq_ctrl = {
852 .cmd_executions = 1,
853 .cmd_delay_gap = 4,
854 .post_ssq_wait = 400,
855 .data_direction = SSQ_NA,
856 },
857 .sp_cmd_addr = {
858 .address = 1024,
859 .rowbits = 6,
860 .bank = 0,
861 .rank = 0,
862 },
863 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100864 .inc_rank = 1,
865 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200866 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100867 },
868 };
869 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100870
Angel Pons7c49cb82020-03-16 23:17:32 +0100871 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200872 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100873
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100874 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100875 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100876 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100877 }
878
Angel Pons7c49cb82020-03-16 23:17:32 +0100879 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100880 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100881
882 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100883 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100884
Angel Pons88521882020-01-05 20:21:20 +0100885 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100886
887 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
888
Angel Pons7c49cb82020-03-16 23:17:32 +0100889 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100890 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100891
Angel Ponsffd50152020-11-12 11:03:10 +0100892 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200893
Angel Pons7c49cb82020-03-16 23:17:32 +0100894 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200895 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100896
Angel Pons7c49cb82020-03-16 23:17:32 +0100897 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100898 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100899 }
900}
901
Felix Held3b906032020-01-14 17:05:43 +0100902static const u32 lane_base[] = {
903 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
904 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
905 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100906};
907
Angel Pons88521882020-01-05 20:21:20 +0100908void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100909{
Angel Pons88521882020-01-05 20:21:20 +0100910 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100911 int lane;
912 int slotrank, slot;
913 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100914 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100915
916 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100917 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
918 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100919 }
920
921 for (slot = 0; slot < NUM_SLOTS; slot++)
922 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
923 case 0:
924 default:
Angel Pons88521882020-01-05 20:21:20 +0100925 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100926 break;
927 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100928 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100929 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100930 break;
931 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100932 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100933 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100934 break;
935 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100936 pi_coding_ctrl[slot] =
937 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100938 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100939 break;
940 }
941
Angel Pons7c49cb82020-03-16 23:17:32 +0100942 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100943 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100944 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
945 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100946 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100947 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
948
Angel Pons88521882020-01-05 20:21:20 +0100949 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100950
Angel Pons7c49cb82020-03-16 23:17:32 +0100951 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100952 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
953 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100954
955 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100956 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100957 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100958 if (shift < 0)
959 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100960
Angel Pons88521882020-01-05 20:21:20 +0100961 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100962
963 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100964 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
965 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100966 }
967
Angel Pons88521882020-01-05 20:21:20 +0100968 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
969 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100970
Angel Pons88521882020-01-05 20:21:20 +0100971 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100972 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100973
Angel Pons88521882020-01-05 20:21:20 +0100974 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100975
976 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100977 int post_timA_min_high = 7, pre_timA_min_high = 7;
978 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100979 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100980 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100981
982 if (shift < 0)
983 shift = 0;
984
985 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200986 post_timA_min_high = MIN(post_timA_min_high,
987 (ctrl->timings[channel][slotrank].lanes[lane].
988 timA + shift) >> 6);
989 pre_timA_min_high = MIN(pre_timA_min_high,
990 ctrl->timings[channel][slotrank].lanes[lane].
991 timA >> 6);
992 post_timA_max_high = MAX(post_timA_max_high,
993 (ctrl->timings[channel][slotrank].lanes[lane].
994 timA + shift) >> 6);
995 pre_timA_max_high = MAX(pre_timA_max_high,
996 ctrl->timings[channel][slotrank].lanes[lane].
997 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100998 }
999
1000 if (pre_timA_max_high - pre_timA_min_high <
1001 post_timA_max_high - post_timA_min_high)
1002 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001003
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001004 else if (pre_timA_max_high - pre_timA_min_high >
1005 post_timA_max_high - post_timA_min_high)
1006 shift_402x = -1;
1007
Felix Helddee167e2019-12-30 17:30:16 +01001008 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001009 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001010 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001011
Angel Pons88521882020-01-05 20:21:20 +01001012 reg_roundtrip_latency |=
1013 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001014 shift_402x) << (8 * slotrank);
1015
1016 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001017 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001018 (((ctrl->timings[channel][slotrank].lanes[lane].
1019 timA + shift) & 0x3f)
1020 |
1021 ((ctrl->timings[channel][slotrank].lanes[lane].
1022 rising + shift) << 8)
1023 |
1024 (((ctrl->timings[channel][slotrank].lanes[lane].
1025 timA + shift -
1026 (post_timA_min_high << 6)) & 0x1c0) << 10)
1027 | ((ctrl->timings[channel][slotrank].lanes[lane].
1028 falling + shift) << 20));
1029
Felix Heldfb19c8a2020-01-14 21:27:59 +01001030 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001031 (((ctrl->timings[channel][slotrank].lanes[lane].
1032 timC + shift) & 0x3f)
1033 |
1034 (((ctrl->timings[channel][slotrank].lanes[lane].
1035 timB + shift) & 0x3f) << 8)
1036 |
1037 (((ctrl->timings[channel][slotrank].lanes[lane].
1038 timB + shift) & 0x1c0) << 9)
1039 |
1040 (((ctrl->timings[channel][slotrank].lanes[lane].
1041 timC + shift) & 0x40) << 13));
1042 }
1043 }
Angel Pons88521882020-01-05 20:21:20 +01001044 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1045 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001046}
1047
Angel Pons88521882020-01-05 20:21:20 +01001048static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001049{
Angel Pons88521882020-01-05 20:21:20 +01001050 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001051
Angel Ponsffd50152020-11-12 11:03:10 +01001052 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053
Angel Pons7c49cb82020-03-16 23:17:32 +01001054 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001055 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001056
Angel Pons88521882020-01-05 20:21:20 +01001057 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001058}
1059
Angel Pons7c49cb82020-03-16 23:17:32 +01001060static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001061{
1062 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001063
1064 return (MCHBAR32(lane_base[lane] +
1065 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001066}
1067
1068struct run {
1069 int middle;
1070 int end;
1071 int start;
1072 int all;
1073 int length;
1074};
1075
1076static struct run get_longest_zero_run(int *seq, int sz)
1077{
1078 int i, ls;
1079 int bl = 0, bs = 0;
1080 struct run ret;
1081
1082 ls = 0;
1083 for (i = 0; i < 2 * sz; i++)
1084 if (seq[i % sz]) {
1085 if (i - ls > bl) {
1086 bl = i - ls;
1087 bs = ls;
1088 }
1089 ls = i + 1;
1090 }
1091 if (bl == 0) {
1092 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001093 ret.start = 0;
1094 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001095 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001096 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001097 return ret;
1098 }
1099
Angel Pons7c49cb82020-03-16 23:17:32 +01001100 ret.start = bs % sz;
1101 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001102 ret.middle = (bs + (bl - 1) / 2) % sz;
1103 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001104 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001105
1106 return ret;
1107}
1108
Angel Ponsf3053392020-11-13 23:31:12 +01001109static void find_rcven_pi_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001110{
1111 int timA;
1112 int statistics[NUM_LANES][128];
1113 int lane;
1114
1115 for (timA = 0; timA < 128; timA++) {
1116 FOR_ALL_LANES {
1117 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1118 }
1119 program_timings(ctrl, channel);
1120
1121 test_timA(ctrl, channel, slotrank);
1122
1123 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001124 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001125 }
1126 }
1127 FOR_ALL_LANES {
1128 struct run rn = get_longest_zero_run(statistics[lane], 128);
1129 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1130 upperA[lane] = rn.end;
1131 if (upperA[lane] < rn.middle)
1132 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001133
Patrick Rudolph368b6152016-11-25 16:36:52 +01001134 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001135 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001136 }
1137}
1138
Angel Ponsf3053392020-11-13 23:31:12 +01001139static void fine_tune_rcven_pi(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001140{
1141 int timA_delta;
1142 int statistics[NUM_LANES][51];
1143 int lane, i;
1144
1145 memset(statistics, 0, sizeof(statistics));
1146
1147 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001148
1149 FOR_ALL_LANES {
1150 ctrl->timings[channel][slotrank].lanes[lane].timA
1151 = upperA[lane] + timA_delta + 0x40;
1152 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001153 program_timings(ctrl, channel);
1154
1155 for (i = 0; i < 100; i++) {
1156 test_timA(ctrl, channel, slotrank);
1157 FOR_ALL_LANES {
1158 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001159 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001160 }
1161 }
1162 }
1163 FOR_ALL_LANES {
1164 int last_zero, first_all;
1165
1166 for (last_zero = -25; last_zero <= 25; last_zero++)
1167 if (statistics[lane][last_zero + 25])
1168 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001169
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001170 last_zero--;
1171 for (first_all = -25; first_all <= 25; first_all++)
1172 if (statistics[lane][first_all + 25] == 100)
1173 break;
1174
Angel Pons7c49cb82020-03-16 23:17:32 +01001175 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001176
1177 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001178 (last_zero + first_all) / 2 + upperA[lane];
1179
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001180 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001181 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001182 }
1183}
1184
Angel Ponsf3053392020-11-13 23:31:12 +01001185static int find_roundtrip_latency(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001186{
1187 int works[NUM_LANES];
1188 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190 while (1) {
1191 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001192
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 program_timings(ctrl, channel);
1194 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001195
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001196 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001197 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1198
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001199 if (works[lane])
1200 some_works = 1;
1201 else
1202 all_works = 0;
1203 }
1204 if (all_works)
1205 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001206
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001207 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001208 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001209 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1210 channel, slotrank);
1211 return MAKE_ERR;
1212 }
Angel Pons88521882020-01-05 20:21:20 +01001213 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 printram("4024 -= 2;\n");
1215 continue;
1216 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001217 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001218 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001219
Felix Heldef4fe3e2019-12-31 14:15:05 +01001220 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001221 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1222 channel, slotrank);
1223 return MAKE_ERR;
1224 }
1225 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001226 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001227 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001228 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001229 }
1230 }
1231 return 0;
1232}
1233
Angel Pons12bd8ab2020-11-13 23:10:52 +01001234static int get_logic_delay_delta(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001235{
1236 int lane;
Angel Pons12bd8ab2020-11-13 23:10:52 +01001237 u16 logic_delay_min = 7;
1238 u16 logic_delay_max = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001239
1240 FOR_ALL_LANES {
Angel Pons12bd8ab2020-11-13 23:10:52 +01001241 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1242
1243 logic_delay_min = MIN(logic_delay_min, logic_delay);
1244 logic_delay_max = MAX(logic_delay_max, logic_delay);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001245 }
Angel Pons12bd8ab2020-11-13 23:10:52 +01001246
1247 if (logic_delay_max < logic_delay_min) {
1248 printk(BIOS_EMERG, "Logic delay max < min (%u < %u): %d, %d\n",
1249 logic_delay_max, logic_delay_min, channel, slotrank);
1250 }
1251
1252 assert(logic_delay_max >= logic_delay_min);
1253
1254 return logic_delay_max - logic_delay_min;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001255}
1256
Angel Pons12bd8ab2020-11-13 23:10:52 +01001257static int align_rt_io_latency(ramctr_timing *ctrl, int channel, int slotrank, int prev)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001258{
Angel Pons12bd8ab2020-11-13 23:10:52 +01001259 int latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001260
Angel Pons7c49cb82020-03-16 23:17:32 +01001261 /* Get changed maxima */
Angel Pons12bd8ab2020-11-13 23:10:52 +01001262 const int post = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001263
Angel Pons12bd8ab2020-11-13 23:10:52 +01001264 if (prev < post)
1265 latency_offset = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001266
Angel Pons12bd8ab2020-11-13 23:10:52 +01001267 else if (prev > post)
1268 latency_offset = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001269
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270 else
Angel Pons12bd8ab2020-11-13 23:10:52 +01001271 latency_offset = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001272
Angel Pons12bd8ab2020-11-13 23:10:52 +01001273 ctrl->timings[channel][slotrank].io_latency += latency_offset;
1274 ctrl->timings[channel][slotrank].roundtrip_latency += latency_offset;
1275 printram("4024 += %d;\n", latency_offset);
1276 printram("4028 += %d;\n", latency_offset);
1277
1278 return post;
1279}
1280
1281static void compute_final_logic_delay(ramctr_timing *ctrl, int channel, int slotrank)
1282{
1283 u16 logic_delay_min = 7;
1284 int lane;
1285
1286 FOR_ALL_LANES {
1287 const u16 logic_delay = ctrl->timings[channel][slotrank].lanes[lane].timA >> 6;
1288
1289 logic_delay_min = MIN(logic_delay_min, logic_delay);
1290 }
1291
1292 if (logic_delay_min >= 2) {
1293 printk(BIOS_WARNING, "Logic delay %u greater than 1: %d %d\n",
1294 logic_delay_min, channel, slotrank);
1295 }
1296
1297 FOR_ALL_LANES {
1298 ctrl->timings[channel][slotrank].lanes[lane].timA -= logic_delay_min << 6;
1299 }
1300 ctrl->timings[channel][slotrank].io_latency -= logic_delay_min;
1301 printram("4028 -= %d;\n", logic_delay_min);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001302}
1303
Angel Pons7c49cb82020-03-16 23:17:32 +01001304/*
1305 * Compensate the skew between DQS and DQs.
1306 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001307 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1308 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001309 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001310 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001311 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001312 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1313 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1314 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001315 * DQ in phase the data being read is expected to alternate on every byte:
1316 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001317 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001318 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001319 * Once the controller has detected this pattern a bit in the result register is set for the
1320 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001321 */
Angel Pons88521882020-01-05 20:21:20 +01001322int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001323{
1324 int channel, slotrank, lane;
1325 int err;
1326
1327 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1328 int all_high, some_high;
1329 int upperA[NUM_LANES];
Angel Pons12bd8ab2020-11-13 23:10:52 +01001330 int prev;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001331
Angel Pons88521882020-01-05 20:21:20 +01001332 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001333
Angel Ponsffd50152020-11-12 11:03:10 +01001334 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001335
Angel Pons7c49cb82020-03-16 23:17:32 +01001336 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001337 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001338
Angel Pons88521882020-01-05 20:21:20 +01001339 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001340
Felix Heldef4fe3e2019-12-31 14:15:05 +01001341 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001342 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001343 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001344
Angel Ponsf3053392020-11-13 23:31:12 +01001345 find_rcven_pi_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001346
Felix Held2bb3cdf2018-07-28 00:23:59 +02001347 all_high = 1;
1348 some_high = 0;
1349 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001350 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001351 some_high = 1;
1352 else
1353 all_high = 0;
1354 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001355
1356 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001357 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001358 printram("4028--;\n");
1359 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001360 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001361 upperA[lane] -= 0x40;
1362
1363 }
1364 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001365 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001366 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001367 printram("4024++;\n");
1368 printram("4028++;\n");
1369 }
1370
1371 program_timings(ctrl, channel);
1372
Angel Pons12bd8ab2020-11-13 23:10:52 +01001373 prev = get_logic_delay_delta(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001374
Angel Ponsf3053392020-11-13 23:31:12 +01001375 err = find_roundtrip_latency(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001376 if (err)
1377 return err;
1378
Angel Pons12bd8ab2020-11-13 23:10:52 +01001379 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380
Angel Ponsf3053392020-11-13 23:31:12 +01001381 fine_tune_rcven_pi(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
Angel Pons12bd8ab2020-11-13 23:10:52 +01001383 prev = align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384
Angel Pons12bd8ab2020-11-13 23:10:52 +01001385 compute_final_logic_delay(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001386
Angel Pons12bd8ab2020-11-13 23:10:52 +01001387 align_rt_io_latency(ctrl, channel, slotrank, prev);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388
1389 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001390 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001391 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001392
1393 printram("final results:\n");
1394 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001395 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001396 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001397
Angel Pons88521882020-01-05 20:21:20 +01001398 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001399
1400 toggle_io_reset();
1401 }
1402
1403 FOR_ALL_POPULATED_CHANNELS {
1404 program_timings(ctrl, channel);
1405 }
Angel Pons50a6fe72020-11-14 01:18:14 +01001406 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001407 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001408 }
1409 return 0;
1410}
1411
Angel Pons011661c2020-11-15 18:21:35 +01001412static void test_tx_dq(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001413{
1414 int lane;
1415
1416 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001417 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1418 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001419 }
1420
Angel Pons88521882020-01-05 20:21:20 +01001421 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001422
Angel Ponsffd50152020-11-12 11:03:10 +01001423 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1424 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001425
Angel Pons7c49cb82020-03-16 23:17:32 +01001426 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001427 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001428
Angel Pons88521882020-01-05 20:21:20 +01001429 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001430
Angel Pons8f0757e2020-11-11 23:03:36 +01001431 const struct iosav_ssq rd_sequence[] = {
1432 /* DRAM command PREA */
1433 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001434 .sp_cmd_ctrl = {
1435 .command = IOSAV_PRE,
1436 .ranksel_ap = 1,
1437 },
1438 .subseq_ctrl = {
1439 .cmd_executions = 1,
1440 .cmd_delay_gap = 3,
1441 .post_ssq_wait = ctrl->tRP,
1442 .data_direction = SSQ_NA,
1443 },
1444 .sp_cmd_addr = {
1445 .address = 1024,
1446 .rowbits = 6,
1447 .bank = 0,
1448 .rank = slotrank,
1449 },
1450 .addr_update = {
1451 .addr_wrap = 18,
1452 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001453 },
1454 /* DRAM command ACT */
1455 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001456 .sp_cmd_ctrl = {
1457 .command = IOSAV_ACT,
1458 .ranksel_ap = 1,
1459 },
1460 .subseq_ctrl = {
1461 .cmd_executions = 8,
1462 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1463 .post_ssq_wait = ctrl->CAS,
1464 .data_direction = SSQ_NA,
1465 },
1466 .sp_cmd_addr = {
1467 .address = 0,
1468 .rowbits = 6,
1469 .bank = 0,
1470 .rank = slotrank,
1471 },
1472 .addr_update = {
1473 .inc_bank = 1,
1474 .addr_wrap = 18,
1475 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001476 },
1477 /* DRAM command RD */
1478 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001479 .sp_cmd_ctrl = {
1480 .command = IOSAV_RD,
1481 .ranksel_ap = 1,
1482 },
1483 .subseq_ctrl = {
1484 .cmd_executions = 500,
1485 .cmd_delay_gap = 4,
1486 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1487 .data_direction = SSQ_RD,
1488 },
1489 .sp_cmd_addr = {
1490 .address = 0,
1491 .rowbits = 0,
1492 .bank = 0,
1493 .rank = slotrank,
1494 },
1495 .addr_update = {
1496 .inc_addr_8 = 1,
1497 .addr_wrap = 18,
1498 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001499 },
1500 /* DRAM command PREA */
1501 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001502 .sp_cmd_ctrl = {
1503 .command = IOSAV_PRE,
1504 .ranksel_ap = 1,
1505 },
1506 .subseq_ctrl = {
1507 .cmd_executions = 1,
1508 .cmd_delay_gap = 3,
1509 .post_ssq_wait = ctrl->tRP,
1510 .data_direction = SSQ_NA,
1511 },
1512 .sp_cmd_addr = {
1513 .address = 1024,
1514 .rowbits = 6,
1515 .bank = 0,
1516 .rank = slotrank,
1517 },
1518 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01001519 .addr_wrap = 18,
Angel Pons3abd2062020-05-03 00:25:02 +02001520 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001521 },
1522 };
1523 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001524
Angel Pons7c49cb82020-03-16 23:17:32 +01001525 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001526 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001527
Angel Pons88521882020-01-05 20:21:20 +01001528 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001529}
1530
Angel Pons011661c2020-11-15 18:21:35 +01001531static void tx_dq_threshold_process(int *data, const int count)
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001532{
1533 int min = data[0];
1534 int max = min;
1535 int i;
1536 for (i = 1; i < count; i++) {
1537 if (min > data[i])
1538 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001539
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001540 if (max < data[i])
1541 max = data[i];
1542 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001543 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001544 for (i = 0; i < count; i++)
1545 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001546
Angel Pons891f2bc2020-01-10 01:27:28 +01001547 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001548}
1549
Angel Pons011661c2020-11-15 18:21:35 +01001550static int tx_dq_write_leveling(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001551{
Angel Pons011661c2020-11-15 18:21:35 +01001552 int tx_dq;
Angel Pons7c49cb82020-03-16 23:17:32 +01001553 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001554 int lane;
1555
Angel Pons88521882020-01-05 20:21:20 +01001556 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557
Angel Ponsffd50152020-11-12 11:03:10 +01001558 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001559
Angel Pons7c49cb82020-03-16 23:17:32 +01001560 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001561 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001562
Angel Pons011661c2020-11-15 18:21:35 +01001563 for (tx_dq = 0; tx_dq <= MAX_TIMC; tx_dq++) {
1564 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = tx_dq;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001565 program_timings(ctrl, channel);
1566
Angel Pons011661c2020-11-15 18:21:35 +01001567 test_tx_dq(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001568
1569 FOR_ALL_LANES {
Angel Pons011661c2020-11-15 18:21:35 +01001570 stats[lane][tx_dq] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001571 }
1572 }
1573 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001574 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1575
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001576 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001577 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1578 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001579 /*
1580 * With command training not being done yet, the lane can be erroneous.
1581 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001582 */
Angel Pons011661c2020-11-15 18:21:35 +01001583 tx_dq_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
Angel Pons7c49cb82020-03-16 23:17:32 +01001584 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1585
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001586 if (rn.all || rn.length < 8) {
1587 printk(BIOS_EMERG, "timC recovery failed\n");
1588 return MAKE_ERR;
1589 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001590 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001591 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001592 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001593 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001594 }
1595 return 0;
1596}
1597
Angel Pons88521882020-01-05 20:21:20 +01001598static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001599{
1600 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001601
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001602 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1603 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001604
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001605 return ret;
1606}
1607
Angel Pons765d4652020-11-11 14:44:35 +01001608/* Each cacheline is 64 bits long */
1609static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1610{
1611 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1612}
1613
Angel Pons88521882020-01-05 20:21:20 +01001614static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001615{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301616 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001617 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001618
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619 for (j = 0; j < 16; j++)
1620 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001621
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001622 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001623
1624 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001625}
1626
Angel Pons88521882020-01-05 20:21:20 +01001627static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001628{
1629 int ret = 0;
1630 int channel;
1631 FOR_ALL_POPULATED_CHANNELS ret++;
1632 return ret;
1633}
1634
Angel Pons88521882020-01-05 20:21:20 +01001635static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001636{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301637 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001638 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301639 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001640
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001641 for (j = 0; j < 16; j++)
1642 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001643
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001644 for (j = 0; j < 16; j++)
1645 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001646
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001647 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001648
1649 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001650}
1651
Angel Pons88521882020-01-05 20:21:20 +01001652static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001653{
1654 int channel, slotrank, lane;
1655
1656 FOR_ALL_POPULATED_CHANNELS {
1657 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001658 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1659 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001660 }
1661
1662 program_timings(ctrl, channel);
1663
1664 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001665 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001666
Angel Ponsffd50152020-11-12 11:03:10 +01001667 iosav_write_read_mpr_sequence(
1668 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001669
Angel Pons7c49cb82020-03-16 23:17:32 +01001670 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001671 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001672
Angel Pons88521882020-01-05 20:21:20 +01001673 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001674 }
1675
1676 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001677 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1678 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001679 }
1680
1681 program_timings(ctrl, channel);
1682
1683 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001684 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001685
Angel Ponsffd50152020-11-12 11:03:10 +01001686 iosav_write_read_mpr_sequence(
1687 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001688
Angel Pons7c49cb82020-03-16 23:17:32 +01001689 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001690 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001691
Angel Pons88521882020-01-05 20:21:20 +01001692 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001693 }
1694 }
1695}
1696
Angel Pons88521882020-01-05 20:21:20 +01001697static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001698{
Angel Pons59996e02020-11-14 16:34:35 +01001699 /* First DQS/DQS# rising edge after write leveling mode is programmed */
1700 const u32 tWLMRD = 40;
1701
1702 u32 mr1reg = make_mr1(ctrl, slotrank, channel) | 1 << 7;
1703 int bank = 1;
1704
1705 if (ctrl->rank_mirror[channel][slotrank])
1706 ddr3_mirror_mrreg(&bank, &mr1reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001707
Angel Pons88521882020-01-05 20:21:20 +01001708 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001709
1710 const struct iosav_ssq sequence[] = {
Angel Pons59996e02020-11-14 16:34:35 +01001711 /* DRAM command MRS: enable DQs on this slotrank */
Angel Pons8f0757e2020-11-11 23:03:36 +01001712 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001713 .sp_cmd_ctrl = {
Angel Pons59996e02020-11-14 16:34:35 +01001714 .command = IOSAV_MRS,
1715 .ranksel_ap = 1,
1716 },
1717 .subseq_ctrl = {
1718 .cmd_executions = 1,
1719 .cmd_delay_gap = 3,
1720 .post_ssq_wait = tWLMRD,
1721 .data_direction = SSQ_NA,
1722 },
1723 .sp_cmd_addr = {
1724 .address = mr1reg,
1725 .rowbits = 6,
1726 .bank = bank,
1727 .rank = slotrank,
1728 },
1729 },
1730 /* DRAM command NOP */
1731 [1] = {
1732 .sp_cmd_ctrl = {
Angel Pons3abd2062020-05-03 00:25:02 +02001733 .command = IOSAV_NOP,
1734 .ranksel_ap = 1,
1735 },
1736 .subseq_ctrl = {
1737 .cmd_executions = 1,
1738 .cmd_delay_gap = 3,
1739 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1740 .data_direction = SSQ_WR,
1741 },
1742 .sp_cmd_addr = {
1743 .address = 8,
1744 .rowbits = 0,
1745 .bank = 0,
1746 .rank = slotrank,
1747 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001748 },
1749 /* DRAM command NOP */
Angel Pons59996e02020-11-14 16:34:35 +01001750 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001751 .sp_cmd_ctrl = {
1752 .command = IOSAV_NOP_ALT,
1753 .ranksel_ap = 1,
1754 },
1755 .subseq_ctrl = {
1756 .cmd_executions = 1,
1757 .cmd_delay_gap = 3,
1758 .post_ssq_wait = ctrl->CAS + 38,
1759 .data_direction = SSQ_RD,
1760 },
1761 .sp_cmd_addr = {
1762 .address = 4,
1763 .rowbits = 0,
1764 .bank = 0,
1765 .rank = slotrank,
1766 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001767 },
Angel Pons59996e02020-11-14 16:34:35 +01001768 /* DRAM command MRS: disable DQs on this slotrank */
1769 [3] = {
1770 .sp_cmd_ctrl = {
1771 .command = IOSAV_MRS,
1772 .ranksel_ap = 1,
1773 },
1774 .subseq_ctrl = {
1775 .cmd_executions = 1,
1776 .cmd_delay_gap = 3,
1777 .post_ssq_wait = ctrl->tMOD,
1778 .data_direction = SSQ_NA,
1779 },
1780 .sp_cmd_addr = {
1781 .address = mr1reg | 1 << 12,
1782 .rowbits = 6,
1783 .bank = bank,
1784 .rank = slotrank,
1785 },
1786 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001787 };
1788 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001789
Angel Pons7c49cb82020-03-16 23:17:32 +01001790 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001791 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001792
Angel Pons88521882020-01-05 20:21:20 +01001793 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001794}
1795
1796static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1797{
1798 int timB;
1799 int statistics[NUM_LANES][128];
1800 int lane;
1801
Angel Pons88521882020-01-05 20:21:20 +01001802 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001803
1804 for (timB = 0; timB < 128; timB++) {
1805 FOR_ALL_LANES {
1806 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1807 }
1808 program_timings(ctrl, channel);
1809
1810 test_timB(ctrl, channel, slotrank);
1811
1812 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001813 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1814 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1815 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001816 }
1817 }
1818 FOR_ALL_LANES {
1819 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001820 /*
1821 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1822 * of timB by a small value, which might cause the 6-bit value to overflow if
1823 * it's close to 0x3f. Increment the value by a small offset if it's likely
1824 * to overflow, to make sure it won't overflow while running tests and bricks
1825 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001827 * TODO: find out why some tests (edge write discovery) increment timB.
1828 */
1829 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001830 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001831 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001832 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001833
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001834 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1835 if (rn.all) {
1836 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1837 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001838
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001839 return MAKE_ERR;
1840 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001841 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1842 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001843 }
1844 return 0;
1845}
1846
1847static int get_timB_high_adjust(u64 val)
1848{
1849 int i;
1850
Angel Ponsbf13ef02020-11-11 18:40:06 +01001851 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001852 if (val == 0xffffffffffffffffLL)
1853 return 0;
1854
1855 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001856 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001857 for (i = 0; i < 8; i++)
1858 if (val << (8 * (7 - i) + 4))
1859 return -i;
1860 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001861 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001862 for (i = 0; i < 8; i++)
1863 if (val >> (8 * (7 - i) + 4))
1864 return i;
1865 }
1866 return 8;
1867}
1868
Angel Ponsbf13ef02020-11-11 18:40:06 +01001869static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001870{
1871 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001872 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001873 FOR_ALL_POPULATED_CHANNELS {
1874 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001875 }
1876 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1877
Angel Pons765d4652020-11-11 14:44:35 +01001878 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001879 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001880
Angel Pons88521882020-01-05 20:21:20 +01001881 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001882
Angel Ponsffd50152020-11-12 11:03:10 +01001883 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001884
Angel Pons7c49cb82020-03-16 23:17:32 +01001885 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001886 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001887
Angel Pons88521882020-01-05 20:21:20 +01001888 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001889
Angel Pons8f0757e2020-11-11 23:03:36 +01001890 const struct iosav_ssq rd_sequence[] = {
1891 /* DRAM command PREA */
1892 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001893 .sp_cmd_ctrl = {
1894 .command = IOSAV_PRE,
1895 .ranksel_ap = 1,
1896 },
1897 .subseq_ctrl = {
1898 .cmd_executions = 1,
1899 .cmd_delay_gap = 3,
1900 .post_ssq_wait = ctrl->tRP,
1901 .data_direction = SSQ_NA,
1902 },
1903 .sp_cmd_addr = {
1904 .address = 1024,
1905 .rowbits = 6,
1906 .bank = 0,
1907 .rank = slotrank,
1908 },
1909 .addr_update = {
1910 .addr_wrap = 18,
1911 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001912 },
1913 /* DRAM command ACT */
1914 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001915 .sp_cmd_ctrl = {
1916 .command = IOSAV_ACT,
1917 .ranksel_ap = 1,
1918 },
1919 .subseq_ctrl = {
1920 .cmd_executions = 1,
1921 .cmd_delay_gap = 3,
1922 .post_ssq_wait = ctrl->tRCD,
1923 .data_direction = SSQ_NA,
1924 },
1925 .sp_cmd_addr = {
1926 .address = 0,
1927 .rowbits = 6,
1928 .bank = 0,
1929 .rank = slotrank,
1930 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001931 },
1932 /* DRAM command RD */
1933 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001934 .sp_cmd_ctrl = {
1935 .command = IOSAV_RD,
1936 .ranksel_ap = 3,
1937 },
1938 .subseq_ctrl = {
1939 .cmd_executions = 1,
1940 .cmd_delay_gap = 3,
1941 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001942 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001943 ctrl->timings[channel][slotrank].io_latency,
1944 .data_direction = SSQ_RD,
1945 },
1946 .sp_cmd_addr = {
1947 .address = 8,
1948 .rowbits = 6,
1949 .bank = 0,
1950 .rank = slotrank,
1951 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001952 },
1953 };
1954 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001955
Angel Pons7c49cb82020-03-16 23:17:32 +01001956 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001957 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001958
Angel Pons88521882020-01-05 20:21:20 +01001959 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001960 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001961 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001962 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001963 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001964 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1965 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1966 get_timB_high_adjust(res) * 64;
1967
1968 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001969 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1970 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001971 }
1972 }
Angel Pons88521882020-01-05 20:21:20 +01001973 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974}
1975
Angel Pons7d115132020-11-14 01:44:44 +01001976static void disable_refresh_machine(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001977{
Angel Pons7d115132020-11-14 01:44:44 +01001978 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001979
Angel Pons7d115132020-11-14 01:44:44 +01001980 FOR_ALL_POPULATED_CHANNELS {
1981 /* choose an existing rank */
1982 const int slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001983
Angel Pons7d115132020-11-14 01:44:44 +01001984 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985
Angel Pons7d115132020-11-14 01:44:44 +01001986 /* Execute command queue */
1987 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001988
Angel Pons7d115132020-11-14 01:44:44 +01001989 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001990
Angel Pons7d115132020-11-14 01:44:44 +01001991 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
1992 }
1993
1994 /* Refresh disable */
1995 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
1996
1997 FOR_ALL_POPULATED_CHANNELS {
1998 /* Execute the same command queue */
1999 iosav_run_once(channel);
2000
2001 wait_for_iosav(channel);
2002 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002003}
2004
Angel Pons7c49cb82020-03-16 23:17:32 +01002005/*
2006 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002007 *
Angel Pons7c49cb82020-03-16 23:17:32 +01002008 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
2009 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
2010 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
2011 * CLK/ADDR/CMD signals have the same routing delay.
2012 *
2013 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
2014 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
2015 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002016 */
Angel Pons88521882020-01-05 20:21:20 +01002017int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002018{
2019 int channel, slotrank, lane;
2020 int err;
2021
2022 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002023 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002024
Angel Pons7d115132020-11-14 01:44:44 +01002025 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002026
Angel Pons7c49cb82020-03-16 23:17:32 +01002027 /* Enable write leveling on all ranks
2028 Disable all DQ outputs
2029 Only NOP is allowed in this mode */
2030 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2031 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01002032 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002033
Angel Pons88521882020-01-05 20:21:20 +01002034 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002035
2036 toggle_io_reset();
2037
Angel Pons7c49cb82020-03-16 23:17:32 +01002038 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002039 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2040 err = discover_timB(ctrl, channel, slotrank);
2041 if (err)
2042 return err;
2043 }
2044
Angel Pons7c49cb82020-03-16 23:17:32 +01002045 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002046 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01002047 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002048
Angel Pons88521882020-01-05 20:21:20 +01002049 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002050
2051 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002052 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002053
Angel Pons7c49cb82020-03-16 23:17:32 +01002054 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002055 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002056
2057 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002058 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01002059 MCHBAR32(IOSAV_STATUS_ch(channel));
2060 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002061
Angel Ponsffd50152020-11-12 11:03:10 +01002062 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002063
Angel Pons7c49cb82020-03-16 23:17:32 +01002064 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002065 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002066
Angel Pons88521882020-01-05 20:21:20 +01002067 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002068 }
2069
2070 toggle_io_reset();
2071
2072 printram("CPE\n");
2073 precharge(ctrl);
2074 printram("CPF\n");
2075
Angel Pons50a6fe72020-11-14 01:18:14 +01002076 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002077 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002078 }
2079
2080 FOR_ALL_POPULATED_CHANNELS {
2081 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002082 }
2083
2084 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons011661c2020-11-15 18:21:35 +01002085 err = tx_dq_write_leveling(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002086 if (err)
2087 return err;
2088 }
2089
2090 FOR_ALL_POPULATED_CHANNELS
2091 program_timings(ctrl, channel);
2092
2093 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002094 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002095
2096 FOR_ALL_POPULATED_CHANNELS
2097 program_timings(ctrl, channel);
2098
Angel Pons50a6fe72020-11-14 01:18:14 +01002099 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Ponscf5dd492020-11-14 01:12:24 +01002100 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002101 }
2102 return 0;
2103}
2104
Angel Ponsbf13ef02020-11-11 18:40:06 +01002105static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002106{
2107 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2108 int timC_delta;
2109 int lanes_ok = 0;
2110 int ctr = 0;
2111 int lane;
2112
2113 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2114 FOR_ALL_LANES {
2115 ctrl->timings[channel][slotrank].lanes[lane].timC =
2116 saved_rt.lanes[lane].timC + timC_delta;
2117 }
2118 program_timings(ctrl, channel);
2119 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002120 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002121 }
2122
Angel Pons765d4652020-11-11 14:44:35 +01002123 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002124 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002125
Angel Pons88521882020-01-05 20:21:20 +01002126 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002127
Angel Ponsffd50152020-11-12 11:03:10 +01002128 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002129
2130 /* Program LFSR for the RD/WR subsequences */
2131 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2132 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002133
Angel Pons7c49cb82020-03-16 23:17:32 +01002134 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002135 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002136
Angel Pons88521882020-01-05 20:21:20 +01002137 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002138 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002139 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002140
2141 if (r32 == 0)
2142 lanes_ok |= 1 << lane;
2143 }
2144 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002145 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146 break;
2147 }
2148
2149 ctrl->timings[channel][slotrank] = saved_rt;
2150
Patrick Rudolphdd662872017-10-28 18:20:11 +02002151 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002152}
2153
Angel Pons88521882020-01-05 20:21:20 +01002154static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002155{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302156 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002157 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2158 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159
2160 if (patno) {
2161 u8 base8 = 0x80 >> ((patno - 1) % 8);
2162 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2163 for (i = 0; i < 32; i++) {
2164 for (j = 0; j < 16; j++) {
2165 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002166
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002167 if (invert[patno - 1][i] & (1 << (j / 2)))
2168 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002169
2170 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002171 }
2172 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002173 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002174 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2175 for (j = 0; j < 16; j++) {
2176 const u32 val = pattern[i][j];
2177 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2178 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002179 }
2180 sfence();
2181 }
Angel Pons765d4652020-11-11 14:44:35 +01002182
2183 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002184}
2185
Angel Pons88521882020-01-05 20:21:20 +01002186static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002187{
Angel Pons7d115132020-11-14 01:44:44 +01002188 disable_refresh_machine(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002189
Angel Pons7c49cb82020-03-16 23:17:32 +01002190 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002191 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002192
2193 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002194 dram_mrscommands(ctrl);
2195
2196 toggle_io_reset();
2197}
2198
Angel Ponsbf13ef02020-11-11 18:40:06 +01002199#define CT_MIN_PI -127
2200#define CT_MAX_PI 128
2201#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002203#define MIN_C320C_LEN 13
2204
2205static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2206{
2207 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2208 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002209 int command_pi;
2210 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002211 int delta = 0;
2212
2213 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2214
2215 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002216 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002217 }
2218
2219 ctrl->cmd_stretch[channel] = cmd_stretch;
2220
Angel Pons88521882020-01-05 20:21:20 +01002221 MCHBAR32(TC_RAP_ch(channel)) =
Angel Pons7c49cb82020-03-16 23:17:32 +01002222 (ctrl->tRRD << 0)
2223 | (ctrl->tRTP << 4)
2224 | (ctrl->tCKE << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002225 | (ctrl->tWTR << 12)
2226 | (ctrl->tFAW << 16)
Angel Pons7c49cb82020-03-16 23:17:32 +01002227 | (ctrl->tWR << 24)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002228 | (ctrl->cmd_stretch[channel] << 30);
2229
2230 if (ctrl->cmd_stretch[channel] == 2)
2231 delta = 2;
2232 else if (ctrl->cmd_stretch[channel] == 0)
2233 delta = 4;
2234
2235 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002236 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002237 }
2238
Angel Ponsbf13ef02020-11-11 18:40:06 +01002239 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002240 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002241 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002242 }
2243 program_timings(ctrl, channel);
2244 reprogram_320c(ctrl);
2245 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002246 stat[slotrank][command_pi - CT_MIN_PI] =
2247 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002248 }
2249 }
2250 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002251 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002252
Angel Ponsbf13ef02020-11-11 18:40:06 +01002253 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002254 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2255 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002256
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002257 if (rn.all || rn.length < MIN_C320C_LEN) {
2258 FOR_ALL_POPULATED_RANKS {
2259 ctrl->timings[channel][slotrank] =
2260 saved_timings[channel][slotrank];
2261 }
2262 return MAKE_ERR;
2263 }
2264 }
2265
2266 return 0;
2267}
2268
Angel Pons7c49cb82020-03-16 23:17:32 +01002269/*
2270 * Adjust CMD phase shift and try multiple command rates.
2271 * A command rate of 2T doubles the time needed for address and command decode.
2272 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002273int command_training(ramctr_timing *ctrl)
2274{
2275 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002276
2277 FOR_ALL_POPULATED_CHANNELS {
2278 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002279 }
2280
2281 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002282 int cmdrate, err;
2283
2284 /*
2285 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002286 * Issue:
2287 * While c320c discovery seems to succeed raminit will fail in write training.
2288 *
2289 * Workaround:
2290 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2291 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002292 *
2293 * Single DIMM per channel:
2294 * Try command rate 1T and 2T
2295 */
2296 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002297 if (ctrl->tCMD)
2298 /* XMP gives the CMD rate in clock ticks, not ns */
2299 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002300
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002301 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002302 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2303
2304 if (!err)
2305 break;
2306 }
2307
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002308 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002309 printk(BIOS_EMERG, "c320c discovery failed\n");
2310 return err;
2311 }
2312
Angel Pons891f2bc2020-01-10 01:27:28 +01002313 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002314 }
2315
2316 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002317 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002318
2319 reprogram_320c(ctrl);
2320 return 0;
2321}
2322
Angel Pons4c79f932020-11-14 01:26:52 +01002323static int find_read_mpr_margin(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324{
Angel Pons96a06dd2020-11-14 00:33:18 +01002325 int dqs_pi;
Angel Pons7c49cb82020-03-16 23:17:32 +01002326 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002327 int lane;
2328
Angel Pons96a06dd2020-11-14 00:33:18 +01002329 for (dqs_pi = 0; dqs_pi <= MAX_EDGE_TIMING; dqs_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002331 ctrl->timings[channel][slotrank].lanes[lane].rising = dqs_pi;
2332 ctrl->timings[channel][slotrank].lanes[lane].falling = dqs_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002333 }
2334 program_timings(ctrl, channel);
2335
2336 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002337 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2338 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002339 }
2340
Angel Pons88521882020-01-05 20:21:20 +01002341 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002342
Angel Ponsffd50152020-11-12 11:03:10 +01002343 iosav_write_read_mpr_sequence(
2344 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002345
Angel Pons7c49cb82020-03-16 23:17:32 +01002346 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002347 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002348
Angel Pons88521882020-01-05 20:21:20 +01002349 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002350
2351 FOR_ALL_LANES {
Angel Pons96a06dd2020-11-14 00:33:18 +01002352 stats[lane][dqs_pi] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002353 }
2354 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002355
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002356 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002357 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002358 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002359
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002360 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002361 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2362 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002363 return MAKE_ERR;
2364 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002365 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002366 }
2367 return 0;
2368}
2369
Angel Pons60971dc2020-11-14 00:49:38 +01002370static void find_predefined_pattern(ramctr_timing *ctrl, const int channel)
2371{
2372 int slotrank, lane;
2373
2374 fill_pattern0(ctrl, channel, 0, 0);
2375 FOR_ALL_LANES {
2376 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
2377 }
2378
2379 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2380 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2381 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
2382 }
2383
2384 program_timings(ctrl, channel);
2385
2386 FOR_ALL_POPULATED_RANKS {
2387 wait_for_iosav(channel);
2388
2389 iosav_write_read_mpr_sequence(
2390 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2391
2392 /* Execute command queue */
2393 iosav_run_once(channel);
2394
2395 wait_for_iosav(channel);
2396 }
2397
2398 /* XXX: check any measured value ? */
2399
2400 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2401 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
2402 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
2403 }
2404
2405 program_timings(ctrl, channel);
2406
2407 FOR_ALL_POPULATED_RANKS {
2408 wait_for_iosav(channel);
2409
2410 iosav_write_read_mpr_sequence(
2411 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
2412
2413 /* Execute command queue */
2414 iosav_run_once(channel);
2415
2416 wait_for_iosav(channel);
2417 }
2418
2419 /* XXX: check any measured value ? */
2420
2421 FOR_ALL_LANES {
2422 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
2423 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
2424 }
2425}
2426
Angel Pons4c79f932020-11-14 01:26:52 +01002427int read_mpr_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002428{
2429 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2430 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2431 int channel, slotrank, lane;
2432 int err;
2433
Angel Pons88521882020-01-05 20:21:20 +01002434 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002435
2436 toggle_io_reset();
2437
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002438 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002439 FOR_ALL_LANES {
Angel Pons60971dc2020-11-14 00:49:38 +01002440 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002441 }
2442
Angel Pons60971dc2020-11-14 00:49:38 +01002443 find_predefined_pattern(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002444
2445 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002446 }
2447
Angel Pons0c3936e2020-03-22 12:49:27 +01002448 /*
2449 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2450 * also use a single loop. It would seem that it is a debugging configuration.
2451 */
Angel Pons88521882020-01-05 20:21:20 +01002452 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2453 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002454
2455 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002456 err = find_read_mpr_margin(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002457 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002458 if (err)
2459 return err;
2460 }
2461
Angel Pons88521882020-01-05 20:21:20 +01002462 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2463 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002464
2465 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Angel Pons4c79f932020-11-14 01:26:52 +01002466 err = find_read_mpr_margin(ctrl, channel, slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002467 rising_edges[channel][slotrank]);
2468 if (err)
2469 return err;
2470 }
2471
Angel Pons88521882020-01-05 20:21:20 +01002472 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002473
2474 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2475 ctrl->timings[channel][slotrank].lanes[lane].falling =
2476 falling_edges[channel][slotrank][lane];
2477 ctrl->timings[channel][slotrank].lanes[lane].rising =
2478 rising_edges[channel][slotrank][lane];
2479 }
2480
2481 FOR_ALL_POPULATED_CHANNELS {
2482 program_timings(ctrl, channel);
2483 }
2484
Angel Pons50a6fe72020-11-14 01:18:14 +01002485 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002486 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002487 }
2488 return 0;
2489}
2490
Angel Pons7c49cb82020-03-16 23:17:32 +01002491static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002492{
2493 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002494 u32 raw_stats[MAX_EDGE_TIMING + 1];
2495 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002496 const int reg3000b24[] = { 0, 0xc, 0x2c };
2497 int lane, i;
2498 int lower[NUM_LANES];
2499 int upper[NUM_LANES];
2500 int pat;
2501
2502 FOR_ALL_LANES {
2503 lower[lane] = 0;
2504 upper[lane] = MAX_EDGE_TIMING;
2505 }
2506
2507 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002508 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002509 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2510
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002511 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2512 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002513 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002514
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002515 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2516 FOR_ALL_LANES {
2517 ctrl->timings[channel][slotrank].lanes[lane].
2518 rising = edge;
2519 ctrl->timings[channel][slotrank].lanes[lane].
2520 falling = edge;
2521 }
2522 program_timings(ctrl, channel);
2523
2524 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002525 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2526 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002527 }
Angel Pons88521882020-01-05 20:21:20 +01002528 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002529
Angel Ponsffd50152020-11-12 11:03:10 +01002530 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002531
Angel Pons7c49cb82020-03-16 23:17:32 +01002532 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002533 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002534
Angel Pons88521882020-01-05 20:21:20 +01002535 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002536 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002537 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002538 }
2539
Angel Pons7c49cb82020-03-16 23:17:32 +01002540 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002541 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002542 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002543
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002544 FOR_ALL_LANES {
2545 struct run rn;
2546 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002547 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2548
2549 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2550
2551 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2552 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2553 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002554 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002555
2556 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2557 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2558
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002559 edges[lane] = (lower[lane] + upper[lane]) / 2;
2560 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002561 printk(BIOS_EMERG, "edge write discovery failed: "
2562 "%d, %d, %d\n", channel, slotrank, lane);
2563
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002564 return MAKE_ERR;
2565 }
2566 }
2567 }
2568 }
2569
Angel Ponsa93f46e2020-11-17 16:54:01 +01002570 /* Restore nominal Vref after training */
2571 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002572 printram("CPA\n");
2573 return 0;
2574}
2575
2576int discover_edges_write(ramctr_timing *ctrl)
2577{
2578 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002579 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2580 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002581
Angel Pons7c49cb82020-03-16 23:17:32 +01002582 /*
2583 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2584 * also use a single loop. It would seem that it is a debugging configuration.
2585 */
Angel Pons88521882020-01-05 20:21:20 +01002586 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2587 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002588
2589 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2590 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002591 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002592 if (err)
2593 return err;
2594 }
2595
Angel Pons88521882020-01-05 20:21:20 +01002596 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2597 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002598
2599 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2600 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002601 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002602 if (err)
2603 return err;
2604 }
2605
Angel Pons88521882020-01-05 20:21:20 +01002606 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002607
2608 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2609 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002610 falling_edges[channel][slotrank][lane];
2611
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002612 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002613 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002614 }
2615
2616 FOR_ALL_POPULATED_CHANNELS
2617 program_timings(ctrl, channel);
2618
Angel Pons50a6fe72020-11-14 01:18:14 +01002619 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002620 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002621 }
2622 return 0;
2623}
2624
2625static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2626{
Angel Pons88521882020-01-05 20:21:20 +01002627 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002628
Angel Ponsffd50152020-11-12 11:03:10 +01002629 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002630
Angel Pons7c49cb82020-03-16 23:17:32 +01002631 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002632 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002633
Angel Pons88521882020-01-05 20:21:20 +01002634 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002635}
2636
2637int discover_timC_write(ramctr_timing *ctrl)
2638{
Angel Pons7c49cb82020-03-16 23:17:32 +01002639 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002640 int i, pat;
2641
2642 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2643 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2644 int channel, slotrank, lane;
2645
2646 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2647 lower[channel][slotrank][lane] = 0;
2648 upper[channel][slotrank][lane] = MAX_TIMC;
2649 }
2650
Angel Pons88521882020-01-05 20:21:20 +01002651 /*
2652 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2653 * FIXME: This must only be done on Ivy Bridge.
2654 */
2655 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002656 printram("discover timC write:\n");
2657
2658 for (i = 0; i < 3; i++)
2659 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002660
2661 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2662 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2663 ~0x3f000000, rege3c_b24[i] << 24);
2664
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002665 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002666
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002667 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2668 FOR_ALL_POPULATED_RANKS {
2669 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002670 u32 raw_stats[MAX_TIMC + 1];
2671 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002672
2673 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002674 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002675
2676 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002677
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002678 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002679 FOR_ALL_LANES {
2680 ctrl->timings[channel][slotrank]
2681 .lanes[lane].timC = timC;
2682 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002683 program_timings(ctrl, channel);
2684
2685 test_timC_write (ctrl, channel, slotrank);
2686
Angel Pons7c49cb82020-03-16 23:17:32 +01002687 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002688 raw_stats[timC] = MCHBAR32(
2689 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002690 }
2691 FOR_ALL_LANES {
2692 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002693 for (timC = 0; timC < MAX_TIMC; timC++) {
2694 stats[timC] = !!(raw_stats[timC]
2695 & (1 << lane));
2696 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002697
Angel Pons7c49cb82020-03-16 23:17:32 +01002698 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002699 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002700 printk(BIOS_EMERG,
2701 "timC write discovery failed: "
2702 "%d, %d, %d\n", channel,
2703 slotrank, lane);
2704
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002705 return MAKE_ERR;
2706 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002707 printram("timC: %d, %d, %d: "
2708 "0x%02x-0x%02x-0x%02x, "
2709 "0x%02x-0x%02x\n", channel, slotrank,
2710 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002711 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002712 rn.end - ctrl->timC_offset[i]);
2713
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002714 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002715 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002716 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002717
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002718 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002719 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002720 upper[channel][slotrank][lane]);
2721
2722 }
2723 }
2724 }
2725 }
2726
2727 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002728 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002729 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002730 udelay(2);
2731 }
2732
Angel Pons88521882020-01-05 20:21:20 +01002733 /*
2734 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2735 * FIXME: This must only be done on Ivy Bridge.
2736 */
2737 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002738
2739 printram("CPB\n");
2740
2741 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002742 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002743 (lower[channel][slotrank][lane] +
2744 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002745
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002746 ctrl->timings[channel][slotrank].lanes[lane].timC =
2747 (lower[channel][slotrank][lane] +
2748 upper[channel][slotrank][lane]) / 2;
2749 }
2750 FOR_ALL_POPULATED_CHANNELS {
2751 program_timings(ctrl, channel);
2752 }
2753 return 0;
2754}
2755
Angel Pons88521882020-01-05 20:21:20 +01002756void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002757{
2758 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002759 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002760
2761 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2762 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002763 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002764 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002765 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002766 printram("normalize %d, %d, %d: mat %d\n",
2767 channel, slotrank, lane, mat);
2768
Felix Heldef4fe3e2019-12-31 14:15:05 +01002769 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002770 printram("normalize %d, %d, %d: delta %d\n",
2771 channel, slotrank, lane, delta);
2772
Angel Pons88521882020-01-05 20:21:20 +01002773 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002774 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002775 }
2776
2777 FOR_ALL_POPULATED_CHANNELS {
2778 program_timings(ctrl, channel);
2779 }
2780}
2781
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002782int channel_test(ramctr_timing *ctrl)
2783{
2784 int channel, slotrank, lane;
2785
2786 slotrank = 0;
2787 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002788 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002789 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002790 return MAKE_ERR;
2791 }
2792 FOR_ALL_POPULATED_CHANNELS {
2793 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002794 }
2795
2796 for (slotrank = 0; slotrank < 4; slotrank++)
2797 FOR_ALL_CHANNELS
2798 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2799 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002800 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2801 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002802 }
Angel Pons88521882020-01-05 20:21:20 +01002803 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002804
Angel Ponsffd50152020-11-12 11:03:10 +01002805 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002806
Angel Pons7c49cb82020-03-16 23:17:32 +01002807 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002808 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002809
Angel Pons88521882020-01-05 20:21:20 +01002810 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002811 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002812 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002813 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2814 channel, slotrank, lane);
2815 return MAKE_ERR;
2816 }
2817 }
2818 return 0;
2819}
2820
Patrick Rudolphdd662872017-10-28 18:20:11 +02002821void channel_scrub(ramctr_timing *ctrl)
2822{
2823 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002824 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002825
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002826 FOR_ALL_POPULATED_CHANNELS {
2827 wait_for_iosav(channel);
2828 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002829 }
2830
2831 /*
2832 * During runtime the "scrubber" will periodically scan through the memory in the
2833 * physical address space, to identify and fix CRC errors.
2834 * The following loops writes to every DRAM address, setting the ECC bits to the
2835 * correct value. A read from this location will no longer return a CRC error,
2836 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002837 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002838 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2839 * and firmware running in x86_32.
2840 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002841 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2842 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002843 for (bank = 0; bank < 8; bank++) {
2844 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002845
Angel Pons8f0757e2020-11-11 23:03:36 +01002846 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2847 const struct iosav_ssq sequence[] = {
2848 /*
2849 * DRAM command ACT
2850 * Opens the row for writing.
2851 */
2852 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002853 .sp_cmd_ctrl = {
2854 .command = IOSAV_ACT,
2855 .ranksel_ap = 1,
2856 },
2857 .subseq_ctrl = {
2858 .cmd_executions = 1,
2859 .cmd_delay_gap = gap,
2860 .post_ssq_wait = ctrl->tRCD,
2861 .data_direction = SSQ_NA,
2862 },
2863 .sp_cmd_addr = {
2864 .address = row,
2865 .rowbits = 6,
2866 .bank = bank,
2867 .rank = slotrank,
2868 },
2869 .addr_update = {
2870 .inc_addr_1 = 1,
2871 .addr_wrap = 18,
2872 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002873 },
2874 /*
2875 * DRAM command WR
2876 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2877 * bytes.
2878 */
2879 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002880 .sp_cmd_ctrl = {
2881 .command = IOSAV_WR,
2882 .ranksel_ap = 1,
2883 },
2884 .subseq_ctrl = {
2885 .cmd_executions = 129,
2886 .cmd_delay_gap = 4,
2887 .post_ssq_wait = ctrl->tWTR +
2888 ctrl->CWL + 8,
2889 .data_direction = SSQ_WR,
2890 },
2891 .sp_cmd_addr = {
2892 .address = row,
2893 .rowbits = 0,
2894 .bank = bank,
2895 .rank = slotrank,
2896 },
2897 .addr_update = {
2898 .inc_addr_8 = 1,
2899 .addr_wrap = 9,
2900 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002901 },
2902 /*
2903 * DRAM command PRE
2904 * Closes the row.
2905 */
2906 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002907 .sp_cmd_ctrl = {
2908 .command = IOSAV_PRE,
2909 .ranksel_ap = 1,
2910 },
2911 .subseq_ctrl = {
2912 .cmd_executions = 1,
2913 .cmd_delay_gap = 4,
2914 .post_ssq_wait = ctrl->tRP,
2915 .data_direction = SSQ_NA,
2916 },
2917 .sp_cmd_addr = {
2918 .address = 0,
2919 .rowbits = 6,
2920 .bank = bank,
2921 .rank = slotrank,
2922 },
2923 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002924 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002925 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002926 },
2927 };
2928 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002929
2930 /* Execute command queue */
2931 iosav_run_queue(channel, 16, 0);
2932
2933 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002934 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002935 }
2936 }
2937}
2938
Angel Pons88521882020-01-05 20:21:20 +01002939void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002940{
2941 int channel;
2942
Angel Pons7c49cb82020-03-16 23:17:32 +01002943 /* 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 +01002944 static u32 seeds[NUM_CHANNELS][3] = {
2945 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2946 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2947 };
2948 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002949 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002950 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2951 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2952 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002953 }
2954}
2955
Angel Pons89ae6b82020-03-21 13:23:32 +01002956void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002957{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002958 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002959 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002960 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002961 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002962 }
2963}
2964
Angel Pons88521882020-01-05 20:21:20 +01002965void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002966{
2967 int channel;
2968
2969 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002970 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002971 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972 }
2973
2974 udelay(1);
2975
2976 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002977 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002978 }
2979}
2980
Angel Pons7c49cb82020-03-16 23:17:32 +01002981void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002982{
2983 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002984
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002985 FOR_ALL_POPULATED_CHANNELS {
2986 u32 b20, b4_8_12;
Angel Pons88521882020-01-05 20:21:20 +01002987 int min_pi = 10000;
2988 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002989
2990 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002991 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2992 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002993 }
2994
Angel Pons7c49cb82020-03-16 23:17:32 +01002995 b20 = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002996
Angel Pons7c49cb82020-03-16 23:17:32 +01002997 b4_8_12 = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 0x3330 : 0x2220;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002998
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002999 dram_odt_stretch(ctrl, channel);
3000
Angel Ponsdc5539f2020-11-12 12:44:25 +01003001 MCHBAR32(TC_RWP_ch(channel)) = (1 << 27) | (2 << 24) | (b20 << 20) |
Felix Held2463aa92018-07-29 21:37:55 +02003002 ((ctrl->ref_card_offset[channel] + 2) << 16) | b4_8_12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003003 }
3004}
3005
Angel Pons88521882020-01-05 20:21:20 +01003006void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003007{
3008 int channel;
3009 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01003010 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
3011 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003012 }
3013}
3014
Angel Pons7c49cb82020-03-16 23:17:32 +01003015/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
3016static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003017{
Angel Pons88521882020-01-05 20:21:20 +01003018 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003019}
3020
Angel Pons7c49cb82020-03-16 23:17:32 +01003021/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01003022void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003023{
Angel Ponsb50ca572020-11-11 19:07:20 +01003024 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01003025
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003026 int channel;
3027 int t1_cycles = 0, t1_ns = 0, t2_ns;
3028 int t3_ns;
3029 u32 r32;
3030
Angel Pons7c49cb82020-03-16 23:17:32 +01003031 /* FIXME: This register only exists on Ivy Bridge */
3032 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003033
Felix Heldf9b826a2018-07-30 17:56:52 +02003034 FOR_ALL_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003035 MCHBAR32_AND_OR(TC_OTHP_ch(channel), ~(3 << 12), 1 << 12);
Patrick Rudolph652c4912017-10-31 11:36:55 +01003036
Patrick Rudolph74203de2017-11-20 11:57:01 +01003037 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003038 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003039 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003040 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003041 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003042 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003043
Felix Heldf9b826a2018-07-30 17:56:52 +02003044 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003045 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003046
Angel Pons88521882020-01-05 20:21:20 +01003047 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3048 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003049
3050 FOR_ALL_CHANNELS {
3051 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003052 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003053 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003054 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003055 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003056 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003057 case 1:
3058 case 4:
3059 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003060 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003061 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003062 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003063 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003064 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003065 break;
3066 }
3067 }
3068
Felix Held50b7ed22019-12-30 20:41:54 +01003069 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003070 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003071 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003072
3073 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003074 MCHBAR32_AND_OR(TC_RFP_ch(channel), ~(3 << 16), 1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003075
Angel Ponsdc5539f2020-11-12 12:44:25 +01003076 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3077 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003078 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003079
Angel Pons7c49cb82020-03-16 23:17:32 +01003080 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003081 FOR_ALL_POPULATED_CHANNELS
3082 break;
3083
Angel Pons88521882020-01-05 20:21:20 +01003084 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3085 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003086 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003087 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003088 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003089 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003090 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003091 t1_ns += 500;
3092
Angel Pons88521882020-01-05 20:21:20 +01003093 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003094 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003095 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003096 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003097 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003098 t3_ns = 500;
3099 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003100
3101 /* The graphics driver will use these watermark values */
3102 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003103 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003104 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3105 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003106}
3107
Angel Pons88521882020-01-05 20:21:20 +01003108void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003109{
Angel Pons50a6fe72020-11-14 01:18:14 +01003110 int channel, lane;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003111
Angel Pons7c49cb82020-03-16 23:17:32 +01003112 FOR_ALL_POPULATED_CHANNELS {
3113 MCHBAR32(TC_RAP_ch(channel)) =
3114 (ctrl->tRRD << 0)
3115 | (ctrl->tRTP << 4)
3116 | (ctrl->tCKE << 8)
3117 | (ctrl->tWTR << 12)
3118 | (ctrl->tFAW << 16)
3119 | (ctrl->tWR << 24)
3120 | (ctrl->cmd_stretch[channel] << 30);
3121 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003122
3123 udelay(1);
3124
3125 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003126 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003127 }
3128
Angel Pons50a6fe72020-11-14 01:18:14 +01003129 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003130 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003131 }
3132
3133 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003134 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003135
3136 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003137 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003138 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003139 }
3140
3141 printram("CPE\n");
3142
Angel Pons88521882020-01-05 20:21:20 +01003143 MCHBAR32(GDCRTRAININGMOD) = 0;
3144 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003145
3146 printram("CP5b\n");
3147
3148 FOR_ALL_POPULATED_CHANNELS {
3149 program_timings(ctrl, channel);
3150 }
3151
3152 u32 reg, addr;
3153
Angel Pons7c49cb82020-03-16 23:17:32 +01003154 /* Poll for RCOMP */
3155 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3156 ;
3157
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003158 do {
Angel Pons88521882020-01-05 20:21:20 +01003159 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003160 } while ((reg & 0x14) == 0);
3161
Angel Pons7c49cb82020-03-16 23:17:32 +01003162 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003163 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003164 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003165
Angel Pons7c49cb82020-03-16 23:17:32 +01003166 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003167 udelay(500);
3168
3169 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003170 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003171 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003172 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003173 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003174 MCHBAR32(addr) = reg;
3175
Angel Pons7c49cb82020-03-16 23:17:32 +01003176 /* Wait 10ns for ranks to settle */
3177 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003178
3179 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3180 MCHBAR32(addr) = reg;
3181
Angel Pons7c49cb82020-03-16 23:17:32 +01003182 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003183 write_reset(ctrl);
3184 }
3185
Angel Pons7c49cb82020-03-16 23:17:32 +01003186 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003187 dram_mrscommands(ctrl);
3188
3189 printram("CP5c\n");
3190
Angel Pons88521882020-01-05 20:21:20 +01003191 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003192
3193 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003194 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003195 udelay(2);
3196 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003197}