blob: 49168a9adc9f8b2f8eb09bed90b45f3d19c88e4a [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
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01003#include <commonlib/helpers.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01004#include <console/console.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01005#include <string.h>
Subrata Banik53b08c32018-12-10 14:11:35 +05306#include <arch/cpu.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02007#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02008#include <device/pci_ops.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01009#include <northbridge/intel/sandybridge/chip.h>
10#include <device/pci_def.h>
11#include <delay.h>
Elyes HAOUAS1d6484a2020-07-10 11:18:11 +020012#include <types.h>
Elyes HAOUAS1d3b3c32019-05-04 08:12:42 +020013
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010014#include "raminit_native.h"
15#include "raminit_common.h"
Angel Pons7f6586f2020-03-21 12:45:12 +010016#include "raminit_tables.h"
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010017#include "sandybridge.h"
18
Angel Pons7c49cb82020-03-16 23:17:32 +010019/* FIXME: no support for 3-channel chipsets */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010020
21static void sfence(void)
22{
23 asm volatile ("sfence");
24}
25
Angel Pons7c49cb82020-03-16 23:17:32 +010026/* Toggle IO reset bit */
27static void toggle_io_reset(void)
28{
Angel Pons88521882020-01-05 20:21:20 +010029 u32 r32 = MCHBAR32(MC_INIT_STATE_G);
Angel Ponsdc5539f2020-11-12 12:44:25 +010030 MCHBAR32(MC_INIT_STATE_G) = r32 | (1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010031 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +010032 MCHBAR32(MC_INIT_STATE_G) = r32 & ~(1 << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010033 udelay(1);
34}
35
36static u32 get_XOVER_CLK(u8 rankmap)
37{
38 return rankmap << 24;
39}
40
41static u32 get_XOVER_CMD(u8 rankmap)
42{
43 u32 reg;
44
Angel Pons7c49cb82020-03-16 23:17:32 +010045 /* Enable xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010046 reg = 0x4000;
47
Angel Pons7c49cb82020-03-16 23:17:32 +010048 /* Enable xover ctl */
49 if (rankmap & 0x03)
50 reg |= (1 << 17);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010051
Angel Pons7c49cb82020-03-16 23:17:32 +010052 if (rankmap & 0x0c)
53 reg |= (1 << 26);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010054
55 return reg;
56}
57
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010058void dram_find_common_params(ramctr_timing *ctrl)
59{
60 size_t valid_dimms;
61 int channel, slot;
62 dimm_info *dimms = &ctrl->info;
63
64 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
65 valid_dimms = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +010066
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010067 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
Angel Pons7c49cb82020-03-16 23:17:32 +010068
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010069 const dimm_attr *dimm = &dimms->dimm[channel][slot];
70 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
71 continue;
Angel Pons7c49cb82020-03-16 23:17:32 +010072
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010073 valid_dimms++;
74
75 /* Find all possible CAS combinations */
76 ctrl->cas_supported &= dimm->cas_supported;
77
78 /* Find the smallest common latencies supported by all DIMMs */
Angel Pons7c49cb82020-03-16 23:17:32 +010079 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
80 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
81 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010082 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
83 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
Angel Pons7c49cb82020-03-16 23:17:32 +010084 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010085 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
86 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
87 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
88 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
89 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +030090 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
91 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010092 }
93
94 if (!ctrl->cas_supported)
Angel Pons7c49cb82020-03-16 23:17:32 +010095 die("Unsupported DIMM combination. DIMMS do not support common CAS latency");
96
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010097 if (!valid_dimms)
98 die("No valid DIMMs found");
99}
100
Angel Pons88521882020-01-05 20:21:20 +0100101void dram_xover(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100102{
103 u32 reg;
104 int channel;
105
106 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100107 /* Enable xover clk */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100108 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100109 printram("XOVER CLK [%x] = %x\n", GDCRCKPICODE_ch(channel), reg);
110 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100111
Angel Pons7c49cb82020-03-16 23:17:32 +0100112 /* Enable xover ctl & xover cmd */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100113 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons88521882020-01-05 20:21:20 +0100114 printram("XOVER CMD [%x] = %x\n", GDCRCMDPICODING_ch(channel), reg);
115 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100116 }
117}
118
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100119static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100120{
Angel Pons89ae6b82020-03-21 13:23:32 +0100121 u32 addr, stretch;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100122
123 stretch = ctrl->ref_card_offset[channel];
Angel Pons7c49cb82020-03-16 23:17:32 +0100124 /*
125 * ODT stretch:
126 * Delay ODT signal by stretch value. Useful for multi DIMM setups on the same channel.
127 */
Angel Pons89ae6b82020-03-21 13:23:32 +0100128 if (IS_SANDY_CPU(ctrl->cpu) && IS_SANDY_CPU_C(ctrl->cpu)) {
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100129 if (stretch == 2)
130 stretch = 3;
Angel Pons7c49cb82020-03-16 23:17:32 +0100131
Angel Pons88521882020-01-05 20:21:20 +0100132 addr = SCHED_SECOND_CBIT_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100133 MCHBAR32_AND_OR(addr, ~(0xf << 10), (stretch << 12) | (stretch << 10));
Angel Pons7c49cb82020-03-16 23:17:32 +0100134 printk(RAM_DEBUG, "OTHP Workaround [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100135 } else {
Angel Pons88521882020-01-05 20:21:20 +0100136 addr = TC_OTHP_ch(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +0100137 MCHBAR32_AND_OR(addr, ~(0xf << 16), (stretch << 16) | (stretch << 18));
Iru Cai89af71c2018-08-16 16:46:27 +0800138 printk(RAM_DEBUG, "OTHP [%x] = %x\n", addr, MCHBAR32(addr));
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100139 }
140}
141
142void dram_timing_regs(ramctr_timing *ctrl)
143{
144 u32 reg, addr, val32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100145 int channel;
146
147 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100148 /* BIN parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100149 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100150 reg |= (ctrl->tRCD << 0);
151 reg |= (ctrl->tRP << 4);
152 reg |= (ctrl->CAS << 8);
153 reg |= (ctrl->CWL << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100154 reg |= (ctrl->tRAS << 16);
Angel Pons88521882020-01-05 20:21:20 +0100155 printram("DBP [%x] = %x\n", TC_DBP_ch(channel), reg);
156 MCHBAR32(TC_DBP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100157
Angel Pons7c49cb82020-03-16 23:17:32 +0100158 /* Regular access parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100159 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100160 reg |= (ctrl->tRRD << 0);
161 reg |= (ctrl->tRTP << 4);
162 reg |= (ctrl->tCKE << 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100163 reg |= (ctrl->tWTR << 12);
164 reg |= (ctrl->tFAW << 16);
Angel Pons7c49cb82020-03-16 23:17:32 +0100165 reg |= (ctrl->tWR << 24);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100166 reg |= (3 << 30);
Angel Pons88521882020-01-05 20:21:20 +0100167 printram("RAP [%x] = %x\n", TC_RAP_ch(channel), reg);
168 MCHBAR32(TC_RAP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100169
Angel Pons7c49cb82020-03-16 23:17:32 +0100170 /* Other parameters */
Angel Pons88521882020-01-05 20:21:20 +0100171 addr = TC_OTHP_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100172 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100173 reg |= (ctrl->tXPDLL << 0);
174 reg |= (ctrl->tXP << 5);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100175 reg |= (ctrl->tAONPD << 8);
176 reg |= 0xa0000;
177 printram("OTHP [%x] = %x\n", addr, reg);
178 MCHBAR32(addr) = reg;
179
Angel Ponsca2f68a2020-03-22 13:15:12 +0100180 /* Debug parameters - only applies to Ivy Bridge */
181 if (IS_IVY_CPU(ctrl->cpu)) {
182 reg = 0;
183
184 /*
185 * If tXP and tXPDLL are very high, we need to increase them by one.
186 * This can only happen on Ivy Bridge, and when overclocking the RAM.
187 */
188 if (ctrl->tXP >= 8)
189 reg |= (1 << 12);
190
191 if (ctrl->tXPDLL >= 32)
192 reg |= (1 << 13);
193
194 MCHBAR32(TC_DTP_ch(channel)) = reg;
195 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100196
Felix Held9fe248f2018-07-31 20:59:45 +0200197 MCHBAR32_OR(addr, 0x00020000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100198
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100199 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100200
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100201 /*
Angel Pons7c49cb82020-03-16 23:17:32 +0100202 * TC-Refresh timing parameters:
203 * The tREFIx9 field should be programmed to minimum of 8.9 * tREFI (to allow
204 * for possible delays from ZQ or isoc) and tRASmax (70us) divided by 1024.
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100205 */
206 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
207
Angel Pons7c49cb82020-03-16 23:17:32 +0100208 reg = ((ctrl->tREFI & 0xffff) << 0) |
209 ((ctrl->tRFC & 0x01ff) << 16) | (((val32 / 1024) & 0x7f) << 25);
210
Angel Pons88521882020-01-05 20:21:20 +0100211 printram("REFI [%x] = %x\n", TC_RFTP_ch(channel), reg);
212 MCHBAR32(TC_RFTP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100213
Angel Ponsdc5539f2020-11-12 12:44:25 +0100214 MCHBAR32_OR(TC_RFP_ch(channel), 0xff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100215
Angel Pons7c49cb82020-03-16 23:17:32 +0100216 /* Self-refresh timing parameters */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100217 reg = 0;
218 val32 = tDLLK;
Angel Pons7c49cb82020-03-16 23:17:32 +0100219 reg = (reg & ~0x00000fff) | (val32 << 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100220 val32 = ctrl->tXSOffset;
Angel Pons7c49cb82020-03-16 23:17:32 +0100221 reg = (reg & ~0x0000f000) | (val32 << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100222 val32 = tDLLK - ctrl->tXSOffset;
Angel Pons7c49cb82020-03-16 23:17:32 +0100223 reg = (reg & ~0x03ff0000) | (val32 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100224 val32 = ctrl->tMOD - 8;
Angel Pons7c49cb82020-03-16 23:17:32 +0100225 reg = (reg & ~0xf0000000) | (val32 << 28);
226 printram("SRFTP [%x] = %x\n", TC_SRFTP_ch(channel), reg);
Angel Pons88521882020-01-05 20:21:20 +0100227 MCHBAR32(TC_SRFTP_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100228 }
229}
230
231void dram_dimm_mapping(ramctr_timing *ctrl)
232{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100233 int channel;
234 dimm_info *info = &ctrl->info;
235
236 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200237 dimm_attr *dimmA, *dimmB;
238 u32 reg = 0;
239
Angel Pons7c49cb82020-03-16 23:17:32 +0100240 if (info->dimm[channel][0].size_mb >= info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100241 dimmA = &info->dimm[channel][0];
242 dimmB = &info->dimm[channel][1];
Angel Pons7c49cb82020-03-16 23:17:32 +0100243 reg |= (0 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100244 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100245 dimmA = &info->dimm[channel][1];
246 dimmB = &info->dimm[channel][0];
Angel Pons7c49cb82020-03-16 23:17:32 +0100247 reg |= (1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100248 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100249
Nico Huberac4f2162017-10-01 18:14:43 +0200250 if (dimmA && (dimmA->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100251 reg |= (dimmA->size_mb / 256) << 0;
252 reg |= (dimmA->ranks - 1) << 17;
Nico Huberac4f2162017-10-01 18:14:43 +0200253 reg |= (dimmA->width / 8 - 1) << 19;
254 }
255
256 if (dimmB && (dimmB->ranks > 0)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100257 reg |= (dimmB->size_mb / 256) << 8;
258 reg |= (dimmB->ranks - 1) << 18;
Nico Huberac4f2162017-10-01 18:14:43 +0200259 reg |= (dimmB->width / 8 - 1) << 20;
260 }
261
Patrick Rudolph4e0cd822020-05-01 18:35:36 +0200262 /*
263 * Rank interleave: Bit 16 of the physical address space sets
264 * the rank to use in a dual single rank DIMM configuration.
265 * That results in every 64KiB being interleaved between two ranks.
266 */
267 reg |= 1 << 21;
268 /* Enhanced interleave */
269 reg |= 1 << 22;
Nico Huberac4f2162017-10-01 18:14:43 +0200270
Angel Pons7c49cb82020-03-16 23:17:32 +0100271 if ((dimmA && (dimmA->ranks > 0)) || (dimmB && (dimmB->ranks > 0))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100272 ctrl->mad_dimm[channel] = reg;
273 } else {
274 ctrl->mad_dimm[channel] = 0;
275 }
276 }
277}
278
Patrick Rudolphdd662872017-10-28 18:20:11 +0200279void dram_dimm_set_mapping(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100280{
281 int channel;
Patrick Rudolphdd662872017-10-28 18:20:11 +0200282 u32 ecc;
283
284 if (ctrl->ecc_enabled)
285 ecc = training ? (1 << 24) : (3 << 24);
286 else
287 ecc = 0;
288
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100289 FOR_ALL_CHANNELS {
Patrick Rudolphdd662872017-10-28 18:20:11 +0200290 MCHBAR32(MAD_DIMM(channel)) = ctrl->mad_dimm[channel] | ecc;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100291 }
Patrick Rudolphdd662872017-10-28 18:20:11 +0200292
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +0200293 if (ctrl->ecc_enabled)
294 udelay(10);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100295}
296
Angel Pons88521882020-01-05 20:21:20 +0100297void dram_zones(ramctr_timing *ctrl, int training)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100298{
299 u32 reg, ch0size, ch1size;
300 u8 val;
301 reg = 0;
302 val = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100303
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100304 if (training) {
305 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
306 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
307 } else {
308 ch0size = ctrl->channel_size_mb[0];
309 ch1size = ctrl->channel_size_mb[1];
310 }
311
312 if (ch0size >= ch1size) {
Angel Pons88521882020-01-05 20:21:20 +0100313 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100314 val = ch1size / 256;
315 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100316 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100317 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100318 MCHBAR32(MAD_CHNL) = 0x24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100319
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100320 } else {
Angel Pons88521882020-01-05 20:21:20 +0100321 reg = MCHBAR32(MAD_ZR);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100322 val = ch0size / 256;
323 reg = (reg & ~0xff000000) | val << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +0100324 reg = (reg & ~0x00ff0000) | (2 * val) << 16;
Angel Pons88521882020-01-05 20:21:20 +0100325 MCHBAR32(MAD_ZR) = reg;
Felix Helddee167e2019-12-30 17:30:16 +0100326 MCHBAR32(MAD_CHNL) = 0x21;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100327 }
328}
329
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100330#define DEFAULT_PCI_MMIO_SIZE 2048
331
332static unsigned int get_mmio_size(void)
333{
334 const struct device *dev;
335 const struct northbridge_intel_sandybridge_config *cfg = NULL;
336
Angel Ponsb31d1d72020-01-10 01:35:09 +0100337 dev = pcidev_path_on_root(PCI_DEVFN(0, 0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100338 if (dev)
339 cfg = dev->chip_info;
340
341 /* If this is zero, it just means devicetree.cb didn't set it */
342 if (!cfg || cfg->pci_mmio_size == 0)
343 return DEFAULT_PCI_MMIO_SIZE;
344 else
345 return cfg->pci_mmio_size;
346}
347
Patrick Rudolph05d4bf7e2017-10-28 16:36:09 +0200348/*
349 * Returns the ECC mode the NB is running at. It takes precedence over ECC capability.
350 * The ME/PCU/.. has the ability to change this.
351 * Return 0: ECC is optional
352 * Return 1: ECC is forced
353 */
354bool get_host_ecc_forced(void)
355{
356 /* read Capabilities A Register */
357 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
358 return !!(reg32 & (1 << 24));
359}
360
361/*
362 * Returns the ECC capability.
363 * The ME/PCU/.. has the ability to change this.
364 * Return 0: ECC is disabled
365 * Return 1: ECC is possible
366 */
367bool get_host_ecc_cap(void)
368{
369 /* read Capabilities A Register */
370 const u32 reg32 = pci_read_config32(HOST_BRIDGE, CAPID0_A);
371 return !(reg32 & (1 << 25));
372}
373
Angel Pons88521882020-01-05 20:21:20 +0100374void dram_memorymap(ramctr_timing *ctrl, int me_uma_size)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100375{
Angel Pons7c49cb82020-03-16 23:17:32 +0100376 u32 reg, val, reclaim, tom, gfxstolen, gttsize;
377 size_t tsegbase, toludbase, remapbase, gfxstolenbase, mmiosize, gttbase;
378 size_t tsegsize, touudbase, remaplimit, mestolenbase, tsegbasedelta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100379 uint16_t ggc;
380
381 mmiosize = get_mmio_size();
382
Felix Held87ddea22020-01-26 04:55:27 +0100383 ggc = pci_read_config16(HOST_BRIDGE, GGC);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100384 if (!(ggc & 2)) {
385 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
Angel Pons7c49cb82020-03-16 23:17:32 +0100386 gttsize = ((ggc >> 8) & 0x3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100387 } else {
388 gfxstolen = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100389 gttsize = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100390 }
391
392 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
393
394 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
395
396 mestolenbase = tom - me_uma_size;
397
Angel Pons7c49cb82020-03-16 23:17:32 +0100398 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize, tom - me_uma_size);
399
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100400 gfxstolenbase = toludbase - gfxstolen;
401 gttbase = gfxstolenbase - gttsize;
402
403 tsegbase = gttbase - tsegsize;
404
Angel Pons7c49cb82020-03-16 23:17:32 +0100405 /* Round tsegbase down to nearest address aligned to tsegsize */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100406 tsegbasedelta = tsegbase & (tsegsize - 1);
407 tsegbase &= ~(tsegsize - 1);
408
409 gttbase -= tsegbasedelta;
410 gfxstolenbase -= tsegbasedelta;
411 toludbase -= tsegbasedelta;
412
Angel Pons7c49cb82020-03-16 23:17:32 +0100413 /* Test if it is possible to reclaim a hole in the RAM addressing */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100414 if (tom - me_uma_size > toludbase) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100415 /* Reclaim is possible */
416 reclaim = 1;
417 remapbase = MAX(4096, tom - me_uma_size);
418 remaplimit = remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
419 touudbase = remaplimit + 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100420 } else {
421 // Reclaim not possible
Angel Pons7c49cb82020-03-16 23:17:32 +0100422 reclaim = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100423 touudbase = tom - me_uma_size;
424 }
425
Angel Pons7c49cb82020-03-16 23:17:32 +0100426 /* Update memory map in PCIe configuration space */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100427 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
428
Angel Pons7c49cb82020-03-16 23:17:32 +0100429 /* TOM (top of memory) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100430 reg = pci_read_config32(HOST_BRIDGE, TOM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100431 val = tom & 0xfff;
432 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100433 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100434 pci_write_config32(HOST_BRIDGE, TOM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100435
Angel Ponsb31d1d72020-01-10 01:35:09 +0100436 reg = pci_read_config32(HOST_BRIDGE, TOM + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100437 val = tom & 0xfffff000;
438 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100439 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOM + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100440 pci_write_config32(HOST_BRIDGE, TOM + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100441
Angel Pons7c49cb82020-03-16 23:17:32 +0100442 /* TOLUD (Top Of Low Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100443 reg = pci_read_config32(HOST_BRIDGE, TOLUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100444 val = toludbase & 0xfff;
445 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100446 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOLUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100447 pci_write_config32(HOST_BRIDGE, TOLUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100448
Angel Pons7c49cb82020-03-16 23:17:32 +0100449 /* TOUUD LSB (Top Of Upper Usable DRAM) */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100450 reg = pci_read_config32(HOST_BRIDGE, TOUUD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100451 val = touudbase & 0xfff;
452 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100453 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100454 pci_write_config32(HOST_BRIDGE, TOUUD, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100455
Angel Pons7c49cb82020-03-16 23:17:32 +0100456 /* TOUUD MSB */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100457 reg = pci_read_config32(HOST_BRIDGE, TOUUD + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100458 val = touudbase & 0xfffff000;
459 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held4902fee2019-12-28 18:09:47 +0100460 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TOUUD + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100461 pci_write_config32(HOST_BRIDGE, TOUUD + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100462
463 if (reclaim) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100464 /* REMAP BASE */
465 pci_write_config32(HOST_BRIDGE, REMAPBASE, remapbase << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100466 pci_write_config32(HOST_BRIDGE, REMAPBASE + 4, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100467
Angel Pons7c49cb82020-03-16 23:17:32 +0100468 /* REMAP LIMIT */
469 pci_write_config32(HOST_BRIDGE, REMAPLIMIT, remaplimit << 20);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100470 pci_write_config32(HOST_BRIDGE, REMAPLIMIT + 4, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100471 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100472 /* TSEG */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100473 reg = pci_read_config32(HOST_BRIDGE, TSEGMB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100474 val = tsegbase & 0xfff;
475 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100476 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", TSEGMB, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100477 pci_write_config32(HOST_BRIDGE, TSEGMB, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100478
Angel Pons7c49cb82020-03-16 23:17:32 +0100479 /* GFX stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100480 reg = pci_read_config32(HOST_BRIDGE, BDSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100481 val = gfxstolenbase & 0xfff;
482 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100483 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BDSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100484 pci_write_config32(HOST_BRIDGE, BDSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100485
Angel Pons7c49cb82020-03-16 23:17:32 +0100486 /* GTT stolen memory */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100487 reg = pci_read_config32(HOST_BRIDGE, BGSM);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100488 val = gttbase & 0xfff;
489 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held4902fee2019-12-28 18:09:47 +0100490 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", BGSM, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100491 pci_write_config32(HOST_BRIDGE, BGSM, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100492
493 if (me_uma_size) {
Angel Ponsb31d1d72020-01-10 01:35:09 +0100494 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100495 val = (0x80000 - me_uma_size) & 0xfffff000;
496 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100497 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100498 pci_write_config32(HOST_BRIDGE, MESEG_MASK + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100499
Angel Pons7c49cb82020-03-16 23:17:32 +0100500 /* ME base */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100501 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100502 val = mestolenbase & 0xfff;
503 reg = (reg & ~0xfff00000) | (val << 20);
Felix Held651f99f2019-12-30 16:28:48 +0100504 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100505 pci_write_config32(HOST_BRIDGE, MESEG_BASE, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100506
Angel Ponsb31d1d72020-01-10 01:35:09 +0100507 reg = pci_read_config32(HOST_BRIDGE, MESEG_BASE + 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100508 val = mestolenbase & 0xfffff000;
509 reg = (reg & ~0x000fffff) | (val >> 12);
Felix Held651f99f2019-12-30 16:28:48 +0100510 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_BASE + 4, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100511 pci_write_config32(HOST_BRIDGE, MESEG_BASE + 4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100512
Angel Pons7c49cb82020-03-16 23:17:32 +0100513 /* ME mask */
Angel Ponsb31d1d72020-01-10 01:35:09 +0100514 reg = pci_read_config32(HOST_BRIDGE, MESEG_MASK);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100515 val = (0x80000 - me_uma_size) & 0xfff;
516 reg = (reg & ~0xfff00000) | (val << 20);
Angel Pons7c49cb82020-03-16 23:17:32 +0100517 reg = reg | ME_STLEN_EN; /* Set ME memory enable */
518 reg = reg | MELCK; /* Set lock bit on ME mem */
Felix Held651f99f2019-12-30 16:28:48 +0100519 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", MESEG_MASK, reg);
Angel Ponsb31d1d72020-01-10 01:35:09 +0100520 pci_write_config32(HOST_BRIDGE, MESEG_MASK, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100521 }
522}
523
Angel Pons88521882020-01-05 20:21:20 +0100524static void write_reset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100525{
526 int channel, slotrank;
527
Angel Pons7c49cb82020-03-16 23:17:32 +0100528 /* Choose a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100529 channel = (ctrl->rankmap[0]) ? 0 : 1;
530
Angel Pons88521882020-01-05 20:21:20 +0100531 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100532
Angel Pons7c49cb82020-03-16 23:17:32 +0100533 /* Choose a populated rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100534 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
535
Angel Ponsffd50152020-11-12 11:03:10 +0100536 iosav_write_zqcs_sequence(channel, slotrank, 3, 8, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100537
Angel Pons7c49cb82020-03-16 23:17:32 +0100538 /*
539 * Execute command queue - why is bit 22 set here?!
540 *
541 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
542 */
Angel Pons38d901e2020-05-02 23:50:43 +0200543 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200544
Angel Pons88521882020-01-05 20:21:20 +0100545 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100546}
547
Angel Pons88521882020-01-05 20:21:20 +0100548void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100549{
Felix Held9fe248f2018-07-31 20:59:45 +0200550 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100551 int channel;
552
Angel Pons7c49cb82020-03-16 23:17:32 +0100553 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
554 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100555 do {
Angel Pons88521882020-01-05 20:21:20 +0100556 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100557 } while ((reg & 0x14) == 0);
558
Angel Pons7c49cb82020-03-16 23:17:32 +0100559 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100561 MCHBAR32(MC_INIT_STATE_G) = reg;
562 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100563 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100564 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100565
Angel Pons7c49cb82020-03-16 23:17:32 +0100566 /* Assert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100567 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 1));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100568
Angel Pons7c49cb82020-03-16 23:17:32 +0100569 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100570 udelay(200);
571
Angel Pons7c49cb82020-03-16 23:17:32 +0100572 /* Deassert DIMM reset signal */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100573 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100574
Angel Pons7c49cb82020-03-16 23:17:32 +0100575 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100576 udelay(500);
577
Angel Pons7c49cb82020-03-16 23:17:32 +0100578 /* Enable DCLK */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100579 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100580
Angel Pons7c49cb82020-03-16 23:17:32 +0100581 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100582 udelay(1);
583
584 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100585 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200586 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100587 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100588
Angel Pons7c49cb82020-03-16 23:17:32 +0100589 /* Wait 10ns for ranks to settle */
590 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100591
592 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100593 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100594
Angel Pons7c49cb82020-03-16 23:17:32 +0100595 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100596 write_reset(ctrl);
597 }
598}
599
Angel Pons7c49cb82020-03-16 23:17:32 +0100600static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100601{
Angel Pons88521882020-01-05 20:21:20 +0100602 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100603
604 if (ctrl->rank_mirror[channel][slotrank]) {
605 /* DDR3 Rank1 Address mirror
Angel Pons7c49cb82020-03-16 23:17:32 +0100606 swap the following pins:
607 A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100608 reg = ((reg >> 1) & 1) | ((reg << 1) & 2);
Angel Pons7c49cb82020-03-16 23:17:32 +0100609 val = (val & ~0x1f8) | ((val >> 1) & 0xa8) | ((val & 0xa8) << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100610 }
611
Angel Pons8f0757e2020-11-11 23:03:36 +0100612 const struct iosav_ssq sequence[] = {
613 /* DRAM command MRS */
614 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200615 .sp_cmd_ctrl = {
616 .command = IOSAV_MRS,
617 },
618 .subseq_ctrl = {
619 .cmd_executions = 1,
620 .cmd_delay_gap = 4,
621 .post_ssq_wait = 4,
622 .data_direction = SSQ_NA,
623 },
624 .sp_cmd_addr = {
625 .address = val,
626 .rowbits = 6,
627 .bank = reg,
628 .rank = slotrank,
629 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100630 },
631 /* DRAM command MRS */
632 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200633 .sp_cmd_ctrl = {
634 .command = IOSAV_MRS,
635 .ranksel_ap = 1,
636 },
637 .subseq_ctrl = {
638 .cmd_executions = 1,
639 .cmd_delay_gap = 4,
640 .post_ssq_wait = 4,
641 .data_direction = SSQ_NA,
642 },
643 .sp_cmd_addr = {
644 .address = val,
645 .rowbits = 6,
646 .bank = reg,
647 .rank = slotrank,
648 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100649 },
650 /* DRAM command MRS */
651 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200652 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100653 .command = IOSAV_MRS,
Angel Pons3abd2062020-05-03 00:25:02 +0200654 },
655 .subseq_ctrl = {
656 .cmd_executions = 1,
657 .cmd_delay_gap = 4,
658 .post_ssq_wait = ctrl->tMOD,
659 .data_direction = SSQ_NA,
660 },
661 .sp_cmd_addr = {
662 .address = val,
663 .rowbits = 6,
664 .bank = reg,
665 .rank = slotrank,
666 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100667 },
668 };
669 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200670
Angel Pons7c49cb82020-03-16 23:17:32 +0100671 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200672 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100673}
674
Angel Pons88521882020-01-05 20:21:20 +0100675static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100676{
677 u16 mr0reg, mch_cas, mch_wr;
678 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 +0100679 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100680
Angel Pons7c49cb82020-03-16 23:17:32 +0100681 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100682 if (ctrl->CAS < 12) {
683 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
684 } else {
685 mch_cas = (u16) (ctrl->CAS - 12);
686 mch_cas = ((mch_cas << 1) | 0x1);
687 }
688
Angel Pons7c49cb82020-03-16 23:17:32 +0100689 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100690 mch_wr = mch_wr_t[ctrl->tWR - 5];
691
Angel Pons2bf28ed2020-11-12 13:49:59 +0100692 /* DLL Reset - self clearing - set after CLK frequency has been changed */
693 mr0reg = 1 << 8;
694
695 mr0reg |= (mch_cas & 0x1) << 2;
696 mr0reg |= (mch_cas & 0xe) << 3;
697 mr0reg |= mch_wr << 9;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100698
Angel Pons7c49cb82020-03-16 23:17:32 +0100699 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
Angel Pons2bf28ed2020-11-12 13:49:59 +0100700 mr0reg |= !is_mobile << 12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100701 return mr0reg;
702}
703
704static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
705{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200706 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100707}
708
Angel Ponsf9997482020-11-12 16:02:52 +0100709static odtmap get_ODT(ramctr_timing *ctrl, int channel)
Angel Pons1a9b5aa2020-11-12 13:51:46 +0100710{
711 /* Get ODT based on rankmap */
712 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
713
714 if (dimms_per_ch == 1) {
715 return (const odtmap){60, 60};
716 } else {
717 return (const odtmap){120, 30};
718 }
719}
720
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100721static u32 encode_odt(u32 odt)
722{
723 switch (odt) {
724 case 30:
725 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
726 case 60:
727 return (1 << 2); // RZQ/4
728 case 120:
729 return (1 << 6); // RZQ/2
730 default:
731 case 0:
732 return 0;
733 }
734}
735
736static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
737{
738 odtmap odt;
739 u32 mr1reg;
740
Angel Ponsf9997482020-11-12 16:02:52 +0100741 odt = get_ODT(ctrl, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100742 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100743
744 mr1reg |= encode_odt(odt.rttnom);
745
746 return mr1reg;
747}
748
749static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
750{
751 u16 mr1reg;
752
753 mr1reg = make_mr1(ctrl, rank, channel);
754
755 write_mrreg(ctrl, channel, rank, 1, mr1reg);
756}
757
758static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
759{
Angel Pons868bca22020-11-13 13:38:04 +0100760 const u16 pasr = 0;
761 const u16 cwl = ctrl->CWL - 5;
762 const odtmap odt = get_ODT(ctrl, channel);
763
Angel Ponsdca3cb52020-11-13 13:42:07 +0100764 int srt = 0;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100765 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
766 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100767
Angel Pons868bca22020-11-13 13:38:04 +0100768 u16 mr2reg = 0;
769 mr2reg |= pasr;
770 mr2reg |= cwl << 3;
771 mr2reg |= ctrl->auto_self_refresh << 6;
772 mr2reg |= srt << 7;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100773 mr2reg |= (odt.rttwr / 60) << 9;
774
775 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100776
777 /* Program MR2 shadow */
778 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
779
780 reg32 &= 3 << 14 | 3 << 6;
781
782 reg32 |= mr2reg & ~(3 << 6);
783
784 if (rank & 1) {
785 if (srt)
786 reg32 |= 1 << (rank / 2 + 6);
787 } else {
788 if (ctrl->rank_mirror[channel][rank])
789 reg32 |= 1 << (rank / 2 + 14);
790 }
791 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100792}
793
794static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
795{
796 write_mrreg(ctrl, channel, rank, 3, 0);
797}
798
Angel Pons88521882020-01-05 20:21:20 +0100799void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100800{
801 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100802 int channel;
803
804 FOR_ALL_POPULATED_CHANNELS {
805 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100806 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100807 dram_mr2(ctrl, slotrank, channel);
808
Angel Pons7c49cb82020-03-16 23:17:32 +0100809 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100810 dram_mr3(ctrl, slotrank, channel);
811
Angel Pons7c49cb82020-03-16 23:17:32 +0100812 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100813 dram_mr1(ctrl, slotrank, channel);
814
Angel Pons7c49cb82020-03-16 23:17:32 +0100815 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100816 dram_mr0(ctrl, slotrank, channel);
817 }
818 }
819
Angel Pons8f0757e2020-11-11 23:03:36 +0100820 const struct iosav_ssq zqcl_sequence[] = {
821 /* DRAM command NOP (without ODT nor chip selects) */
822 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200823 .sp_cmd_ctrl = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100824 .command = IOSAV_NOP & ~(0xff << 8),
Angel Pons3abd2062020-05-03 00:25:02 +0200825 },
826 .subseq_ctrl = {
827 .cmd_executions = 1,
828 .cmd_delay_gap = 4,
829 .post_ssq_wait = 15,
830 .data_direction = SSQ_NA,
831 },
832 .sp_cmd_addr = {
833 .address = 2,
834 .rowbits = 6,
835 .bank = 0,
836 .rank = 0,
837 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100838 },
839 /* DRAM command ZQCL */
840 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200841 .sp_cmd_ctrl = {
842 .command = IOSAV_ZQCS,
843 .ranksel_ap = 1,
844 },
845 .subseq_ctrl = {
846 .cmd_executions = 1,
847 .cmd_delay_gap = 4,
848 .post_ssq_wait = 400,
849 .data_direction = SSQ_NA,
850 },
851 .sp_cmd_addr = {
852 .address = 1024,
853 .rowbits = 6,
854 .bank = 0,
855 .rank = 0,
856 },
857 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +0100858 .inc_rank = 1,
859 .addr_wrap = 20,
Angel Pons3abd2062020-05-03 00:25:02 +0200860 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100861 },
862 };
863 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100864
Angel Pons7c49cb82020-03-16 23:17:32 +0100865 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200866 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100867
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100868 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100869 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100870 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100871 }
872
Angel Pons7c49cb82020-03-16 23:17:32 +0100873 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100874 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100875
876 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100877 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100878
Angel Pons88521882020-01-05 20:21:20 +0100879 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100880
881 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
882
Angel Pons7c49cb82020-03-16 23:17:32 +0100883 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100884 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100885
Angel Ponsffd50152020-11-12 11:03:10 +0100886 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200887
Angel Pons7c49cb82020-03-16 23:17:32 +0100888 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200889 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100890
Angel Pons7c49cb82020-03-16 23:17:32 +0100891 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100892 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100893 }
894}
895
Felix Held3b906032020-01-14 17:05:43 +0100896static const u32 lane_base[] = {
897 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
898 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
899 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100900};
901
Angel Pons88521882020-01-05 20:21:20 +0100902void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100903{
Angel Pons88521882020-01-05 20:21:20 +0100904 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100905 int lane;
906 int slotrank, slot;
907 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100908 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100909
910 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100911 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
912 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100913 }
914
915 for (slot = 0; slot < NUM_SLOTS; slot++)
916 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
917 case 0:
918 default:
Angel Pons88521882020-01-05 20:21:20 +0100919 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100920 break;
921 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100922 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100923 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100924 break;
925 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100926 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100927 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100928 break;
929 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100930 pi_coding_ctrl[slot] =
931 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100932 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100933 break;
934 }
935
Angel Pons7c49cb82020-03-16 23:17:32 +0100936 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100937 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100938 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
939 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100940 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100941 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
942
Angel Pons88521882020-01-05 20:21:20 +0100943 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100944
Angel Pons7c49cb82020-03-16 23:17:32 +0100945 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100946 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
947 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100948
949 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100950 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100951 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100952 if (shift < 0)
953 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100954
Angel Pons88521882020-01-05 20:21:20 +0100955 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100956
957 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100958 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
959 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100960 }
961
Angel Pons88521882020-01-05 20:21:20 +0100962 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
963 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100964
Angel Pons88521882020-01-05 20:21:20 +0100965 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100966 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100967
Angel Pons88521882020-01-05 20:21:20 +0100968 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100969
970 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100971 int post_timA_min_high = 7, pre_timA_min_high = 7;
972 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100973 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100974 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100975
976 if (shift < 0)
977 shift = 0;
978
979 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200980 post_timA_min_high = MIN(post_timA_min_high,
981 (ctrl->timings[channel][slotrank].lanes[lane].
982 timA + shift) >> 6);
983 pre_timA_min_high = MIN(pre_timA_min_high,
984 ctrl->timings[channel][slotrank].lanes[lane].
985 timA >> 6);
986 post_timA_max_high = MAX(post_timA_max_high,
987 (ctrl->timings[channel][slotrank].lanes[lane].
988 timA + shift) >> 6);
989 pre_timA_max_high = MAX(pre_timA_max_high,
990 ctrl->timings[channel][slotrank].lanes[lane].
991 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100992 }
993
994 if (pre_timA_max_high - pre_timA_min_high <
995 post_timA_max_high - post_timA_min_high)
996 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +0100997
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100998 else if (pre_timA_max_high - pre_timA_min_high >
999 post_timA_max_high - post_timA_min_high)
1000 shift_402x = -1;
1001
Felix Helddee167e2019-12-30 17:30:16 +01001002 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001003 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001004 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001005
Angel Pons88521882020-01-05 20:21:20 +01001006 reg_roundtrip_latency |=
1007 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001008 shift_402x) << (8 * slotrank);
1009
1010 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001011 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001012 (((ctrl->timings[channel][slotrank].lanes[lane].
1013 timA + shift) & 0x3f)
1014 |
1015 ((ctrl->timings[channel][slotrank].lanes[lane].
1016 rising + shift) << 8)
1017 |
1018 (((ctrl->timings[channel][slotrank].lanes[lane].
1019 timA + shift -
1020 (post_timA_min_high << 6)) & 0x1c0) << 10)
1021 | ((ctrl->timings[channel][slotrank].lanes[lane].
1022 falling + shift) << 20));
1023
Felix Heldfb19c8a2020-01-14 21:27:59 +01001024 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001025 (((ctrl->timings[channel][slotrank].lanes[lane].
1026 timC + shift) & 0x3f)
1027 |
1028 (((ctrl->timings[channel][slotrank].lanes[lane].
1029 timB + shift) & 0x3f) << 8)
1030 |
1031 (((ctrl->timings[channel][slotrank].lanes[lane].
1032 timB + shift) & 0x1c0) << 9)
1033 |
1034 (((ctrl->timings[channel][slotrank].lanes[lane].
1035 timC + shift) & 0x40) << 13));
1036 }
1037 }
Angel Pons88521882020-01-05 20:21:20 +01001038 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1039 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001040}
1041
Angel Pons88521882020-01-05 20:21:20 +01001042static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001043{
Angel Pons88521882020-01-05 20:21:20 +01001044 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001045
Angel Ponsffd50152020-11-12 11:03:10 +01001046 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001047
Angel Pons7c49cb82020-03-16 23:17:32 +01001048 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001049 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001050
Angel Pons88521882020-01-05 20:21:20 +01001051 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001052}
1053
Angel Pons7c49cb82020-03-16 23:17:32 +01001054static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001055{
1056 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001057
1058 return (MCHBAR32(lane_base[lane] +
1059 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001060}
1061
1062struct run {
1063 int middle;
1064 int end;
1065 int start;
1066 int all;
1067 int length;
1068};
1069
1070static struct run get_longest_zero_run(int *seq, int sz)
1071{
1072 int i, ls;
1073 int bl = 0, bs = 0;
1074 struct run ret;
1075
1076 ls = 0;
1077 for (i = 0; i < 2 * sz; i++)
1078 if (seq[i % sz]) {
1079 if (i - ls > bl) {
1080 bl = i - ls;
1081 bs = ls;
1082 }
1083 ls = i + 1;
1084 }
1085 if (bl == 0) {
1086 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001087 ret.start = 0;
1088 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001089 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001090 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001091 return ret;
1092 }
1093
Angel Pons7c49cb82020-03-16 23:17:32 +01001094 ret.start = bs % sz;
1095 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001096 ret.middle = (bs + (bl - 1) / 2) % sz;
1097 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001098 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001099
1100 return ret;
1101}
1102
Angel Pons7c49cb82020-03-16 23:17:32 +01001103static void discover_timA_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001104{
1105 int timA;
1106 int statistics[NUM_LANES][128];
1107 int lane;
1108
1109 for (timA = 0; timA < 128; timA++) {
1110 FOR_ALL_LANES {
1111 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1112 }
1113 program_timings(ctrl, channel);
1114
1115 test_timA(ctrl, channel, slotrank);
1116
1117 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001118 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001119 }
1120 }
1121 FOR_ALL_LANES {
1122 struct run rn = get_longest_zero_run(statistics[lane], 128);
1123 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1124 upperA[lane] = rn.end;
1125 if (upperA[lane] < rn.middle)
1126 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001127
Patrick Rudolph368b6152016-11-25 16:36:52 +01001128 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001129 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001130 }
1131}
1132
Angel Pons7c49cb82020-03-16 23:17:32 +01001133static void discover_timA_fine(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001134{
1135 int timA_delta;
1136 int statistics[NUM_LANES][51];
1137 int lane, i;
1138
1139 memset(statistics, 0, sizeof(statistics));
1140
1141 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001142
1143 FOR_ALL_LANES {
1144 ctrl->timings[channel][slotrank].lanes[lane].timA
1145 = upperA[lane] + timA_delta + 0x40;
1146 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001147 program_timings(ctrl, channel);
1148
1149 for (i = 0; i < 100; i++) {
1150 test_timA(ctrl, channel, slotrank);
1151 FOR_ALL_LANES {
1152 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001153 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001154 }
1155 }
1156 }
1157 FOR_ALL_LANES {
1158 int last_zero, first_all;
1159
1160 for (last_zero = -25; last_zero <= 25; last_zero++)
1161 if (statistics[lane][last_zero + 25])
1162 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001163
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001164 last_zero--;
1165 for (first_all = -25; first_all <= 25; first_all++)
1166 if (statistics[lane][first_all + 25] == 100)
1167 break;
1168
Angel Pons7c49cb82020-03-16 23:17:32 +01001169 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001170
1171 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001172 (last_zero + first_all) / 2 + upperA[lane];
1173
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001174 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001175 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001176 }
1177}
1178
Angel Pons891f2bc2020-01-10 01:27:28 +01001179static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001180{
1181 int works[NUM_LANES];
1182 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001183
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001184 while (1) {
1185 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001186
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001187 program_timings(ctrl, channel);
1188 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001191 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1192
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 if (works[lane])
1194 some_works = 1;
1195 else
1196 all_works = 0;
1197 }
1198 if (all_works)
1199 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001200
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001201 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001202 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1204 channel, slotrank);
1205 return MAKE_ERR;
1206 }
Angel Pons88521882020-01-05 20:21:20 +01001207 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001208 printram("4024 -= 2;\n");
1209 continue;
1210 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001211 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001212 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001213
Felix Heldef4fe3e2019-12-31 14:15:05 +01001214 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001215 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1216 channel, slotrank);
1217 return MAKE_ERR;
1218 }
1219 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001220 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001221 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001222 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001223 }
1224 }
1225 return 0;
1226}
1227
1228struct timA_minmax {
1229 int timA_min_high, timA_max_high;
1230};
1231
Angel Pons88521882020-01-05 20:21:20 +01001232static void pre_timA_change(ramctr_timing *ctrl, int channel, int slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001233 struct timA_minmax *mnmx)
1234{
1235 int lane;
1236 mnmx->timA_min_high = 7;
1237 mnmx->timA_max_high = 0;
1238
1239 FOR_ALL_LANES {
1240 if (mnmx->timA_min_high >
1241 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1242 mnmx->timA_min_high =
Angel Pons891f2bc2020-01-10 01:27:28 +01001243 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001244 if (mnmx->timA_max_high <
1245 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1246 mnmx->timA_max_high =
Angel Pons891f2bc2020-01-10 01:27:28 +01001247 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001248 }
1249}
1250
Angel Pons88521882020-01-05 20:21:20 +01001251static void post_timA_change(ramctr_timing *ctrl, int channel, int slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001252 struct timA_minmax *mnmx)
1253{
1254 struct timA_minmax post;
1255 int shift_402x = 0;
1256
Angel Pons7c49cb82020-03-16 23:17:32 +01001257 /* Get changed maxima */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001258 pre_timA_change(ctrl, channel, slotrank, &post);
1259
1260 if (mnmx->timA_max_high - mnmx->timA_min_high <
1261 post.timA_max_high - post.timA_min_high)
1262 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001263
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001264 else if (mnmx->timA_max_high - mnmx->timA_min_high >
1265 post.timA_max_high - post.timA_min_high)
1266 shift_402x = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001267
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001268 else
1269 shift_402x = 0;
1270
Felix Heldef4fe3e2019-12-31 14:15:05 +01001271 ctrl->timings[channel][slotrank].io_latency += shift_402x;
Angel Pons88521882020-01-05 20:21:20 +01001272 ctrl->timings[channel][slotrank].roundtrip_latency += shift_402x;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001273 printram("4024 += %d;\n", shift_402x);
1274 printram("4028 += %d;\n", shift_402x);
1275}
1276
Angel Pons7c49cb82020-03-16 23:17:32 +01001277/*
1278 * Compensate the skew between DQS and DQs.
1279 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001280 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1281 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001282 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001283 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001284 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001285 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1286 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1287 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001288 * DQ in phase the data being read is expected to alternate on every byte:
1289 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001290 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001291 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001292 * Once the controller has detected this pattern a bit in the result register is set for the
1293 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001294 */
Angel Pons88521882020-01-05 20:21:20 +01001295int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001296{
1297 int channel, slotrank, lane;
1298 int err;
1299
1300 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1301 int all_high, some_high;
1302 int upperA[NUM_LANES];
1303 struct timA_minmax mnmx;
1304
Angel Pons88521882020-01-05 20:21:20 +01001305 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001306
Angel Ponsffd50152020-11-12 11:03:10 +01001307 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001308
Angel Pons7c49cb82020-03-16 23:17:32 +01001309 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001310 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001311
Angel Pons88521882020-01-05 20:21:20 +01001312 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001313
Felix Heldef4fe3e2019-12-31 14:15:05 +01001314 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001315 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001316 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001317
Felix Held2bb3cdf2018-07-28 00:23:59 +02001318 discover_timA_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001319
Felix Held2bb3cdf2018-07-28 00:23:59 +02001320 all_high = 1;
1321 some_high = 0;
1322 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001323 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001324 some_high = 1;
1325 else
1326 all_high = 0;
1327 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001328
1329 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001330 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001331 printram("4028--;\n");
1332 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001333 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001334 upperA[lane] -= 0x40;
1335
1336 }
1337 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001338 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001339 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001340 printram("4024++;\n");
1341 printram("4028++;\n");
1342 }
1343
1344 program_timings(ctrl, channel);
1345
1346 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1347
1348 err = discover_402x(ctrl, channel, slotrank, upperA);
1349 if (err)
1350 return err;
1351
1352 post_timA_change(ctrl, channel, slotrank, &mnmx);
1353 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1354
1355 discover_timA_fine(ctrl, channel, slotrank, upperA);
1356
1357 post_timA_change(ctrl, channel, slotrank, &mnmx);
1358 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1359
1360 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001361 ctrl->timings[channel][slotrank].lanes[lane].timA -=
1362 mnmx.timA_min_high * 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001363 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001364 ctrl->timings[channel][slotrank].io_latency -= mnmx.timA_min_high;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001365 printram("4028 -= %d;\n", mnmx.timA_min_high);
1366
1367 post_timA_change(ctrl, channel, slotrank, &mnmx);
1368
1369 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001370 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001371 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001372
1373 printram("final results:\n");
1374 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001375 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001376 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001377
Angel Pons88521882020-01-05 20:21:20 +01001378 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379
1380 toggle_io_reset();
1381 }
1382
1383 FOR_ALL_POPULATED_CHANNELS {
1384 program_timings(ctrl, channel);
1385 }
1386 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001387 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001388 }
1389 return 0;
1390}
1391
Angel Pons88521882020-01-05 20:21:20 +01001392static void test_timC(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001393{
1394 int lane;
1395
1396 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001397 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1398 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001399 }
1400
Angel Pons88521882020-01-05 20:21:20 +01001401 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001402
Angel Ponsffd50152020-11-12 11:03:10 +01001403 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1404 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001405
Angel Pons7c49cb82020-03-16 23:17:32 +01001406 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001407 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001408
Angel Pons88521882020-01-05 20:21:20 +01001409 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001410
Angel Pons8f0757e2020-11-11 23:03:36 +01001411 const struct iosav_ssq rd_sequence[] = {
1412 /* DRAM command PREA */
1413 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001414 .sp_cmd_ctrl = {
1415 .command = IOSAV_PRE,
1416 .ranksel_ap = 1,
1417 },
1418 .subseq_ctrl = {
1419 .cmd_executions = 1,
1420 .cmd_delay_gap = 3,
1421 .post_ssq_wait = ctrl->tRP,
1422 .data_direction = SSQ_NA,
1423 },
1424 .sp_cmd_addr = {
1425 .address = 1024,
1426 .rowbits = 6,
1427 .bank = 0,
1428 .rank = slotrank,
1429 },
1430 .addr_update = {
1431 .addr_wrap = 18,
1432 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001433 },
1434 /* DRAM command ACT */
1435 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001436 .sp_cmd_ctrl = {
1437 .command = IOSAV_ACT,
1438 .ranksel_ap = 1,
1439 },
1440 .subseq_ctrl = {
1441 .cmd_executions = 8,
1442 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1443 .post_ssq_wait = ctrl->CAS,
1444 .data_direction = SSQ_NA,
1445 },
1446 .sp_cmd_addr = {
1447 .address = 0,
1448 .rowbits = 6,
1449 .bank = 0,
1450 .rank = slotrank,
1451 },
1452 .addr_update = {
1453 .inc_bank = 1,
1454 .addr_wrap = 18,
1455 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001456 },
1457 /* DRAM command RD */
1458 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001459 .sp_cmd_ctrl = {
1460 .command = IOSAV_RD,
1461 .ranksel_ap = 1,
1462 },
1463 .subseq_ctrl = {
1464 .cmd_executions = 500,
1465 .cmd_delay_gap = 4,
1466 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1467 .data_direction = SSQ_RD,
1468 },
1469 .sp_cmd_addr = {
1470 .address = 0,
1471 .rowbits = 0,
1472 .bank = 0,
1473 .rank = slotrank,
1474 },
1475 .addr_update = {
1476 .inc_addr_8 = 1,
1477 .addr_wrap = 18,
1478 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001479 },
1480 /* DRAM command PREA */
1481 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001482 .sp_cmd_ctrl = {
1483 .command = IOSAV_PRE,
1484 .ranksel_ap = 1,
1485 },
1486 .subseq_ctrl = {
1487 .cmd_executions = 1,
1488 .cmd_delay_gap = 3,
1489 .post_ssq_wait = ctrl->tRP,
1490 .data_direction = SSQ_NA,
1491 },
1492 .sp_cmd_addr = {
1493 .address = 1024,
1494 .rowbits = 6,
1495 .bank = 0,
1496 .rank = slotrank,
1497 },
1498 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01001499 .addr_wrap = 18,
Angel Pons3abd2062020-05-03 00:25:02 +02001500 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001501 },
1502 };
1503 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001504
Angel Pons7c49cb82020-03-16 23:17:32 +01001505 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001506 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001507
Angel Pons88521882020-01-05 20:21:20 +01001508 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001509}
1510
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001511static void timC_threshold_process(int *data, const int count)
1512{
1513 int min = data[0];
1514 int max = min;
1515 int i;
1516 for (i = 1; i < count; i++) {
1517 if (min > data[i])
1518 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001519
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001520 if (max < data[i])
1521 max = data[i];
1522 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001523 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001524 for (i = 0; i < count; i++)
1525 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001526
Angel Pons891f2bc2020-01-10 01:27:28 +01001527 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001528}
1529
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001530static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
1531{
1532 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01001533 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001534 int lane;
1535
Angel Pons88521882020-01-05 20:21:20 +01001536 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001537
Angel Ponsffd50152020-11-12 11:03:10 +01001538 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001539
Angel Pons7c49cb82020-03-16 23:17:32 +01001540 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001541 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001542
1543 for (timC = 0; timC <= MAX_TIMC; timC++) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001544 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001545 program_timings(ctrl, channel);
1546
1547 test_timC(ctrl, channel, slotrank);
1548
1549 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001550 stats[lane][timC] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001551 }
1552 }
1553 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001554 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1555
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001556 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001557 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1558 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001559 /*
1560 * With command training not being done yet, the lane can be erroneous.
1561 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001562 */
Angel Pons7c49cb82020-03-16 23:17:32 +01001563 timC_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
1564 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1565
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001566 if (rn.all || rn.length < 8) {
1567 printk(BIOS_EMERG, "timC recovery failed\n");
1568 return MAKE_ERR;
1569 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001570 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001571 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001572 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001573 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001574 }
1575 return 0;
1576}
1577
Angel Pons88521882020-01-05 20:21:20 +01001578static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001579{
1580 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001581
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001582 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1583 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001584
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001585 return ret;
1586}
1587
Angel Pons765d4652020-11-11 14:44:35 +01001588/* Each cacheline is 64 bits long */
1589static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1590{
1591 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1592}
1593
Angel Pons88521882020-01-05 20:21:20 +01001594static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001595{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301596 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001597 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001598
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001599 for (j = 0; j < 16; j++)
1600 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001601
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001602 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001603
1604 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001605}
1606
Angel Pons88521882020-01-05 20:21:20 +01001607static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001608{
1609 int ret = 0;
1610 int channel;
1611 FOR_ALL_POPULATED_CHANNELS ret++;
1612 return ret;
1613}
1614
Angel Pons88521882020-01-05 20:21:20 +01001615static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001616{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301617 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001618 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301619 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001620
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001621 for (j = 0; j < 16; j++)
1622 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001623
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001624 for (j = 0; j < 16; j++)
1625 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001626
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001627 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001628
1629 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001630}
1631
Angel Pons88521882020-01-05 20:21:20 +01001632static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001633{
1634 int channel, slotrank, lane;
1635
1636 FOR_ALL_POPULATED_CHANNELS {
1637 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001638 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1639 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001640 }
1641
1642 program_timings(ctrl, channel);
1643
1644 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001645 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001646
Angel Ponsffd50152020-11-12 11:03:10 +01001647 iosav_write_read_mpr_sequence(
1648 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001649
Angel Pons7c49cb82020-03-16 23:17:32 +01001650 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001651 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001652
Angel Pons88521882020-01-05 20:21:20 +01001653 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001654 }
1655
1656 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001657 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1658 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001659 }
1660
1661 program_timings(ctrl, channel);
1662
1663 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001664 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001665
Angel Ponsffd50152020-11-12 11:03:10 +01001666 iosav_write_read_mpr_sequence(
1667 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001668
Angel Pons7c49cb82020-03-16 23:17:32 +01001669 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001670 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001671
Angel Pons88521882020-01-05 20:21:20 +01001672 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001673 }
1674 }
1675}
1676
Angel Pons88521882020-01-05 20:21:20 +01001677static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001678{
1679 /* enable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001680 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel) | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001681
Angel Pons88521882020-01-05 20:21:20 +01001682 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001683
1684 const struct iosav_ssq sequence[] = {
1685 /* DRAM command NOP */
1686 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001687 .sp_cmd_ctrl = {
1688 .command = IOSAV_NOP,
1689 .ranksel_ap = 1,
1690 },
1691 .subseq_ctrl = {
1692 .cmd_executions = 1,
1693 .cmd_delay_gap = 3,
1694 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1695 .data_direction = SSQ_WR,
1696 },
1697 .sp_cmd_addr = {
1698 .address = 8,
1699 .rowbits = 0,
1700 .bank = 0,
1701 .rank = slotrank,
1702 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001703 },
1704 /* DRAM command NOP */
1705 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001706 .sp_cmd_ctrl = {
1707 .command = IOSAV_NOP_ALT,
1708 .ranksel_ap = 1,
1709 },
1710 .subseq_ctrl = {
1711 .cmd_executions = 1,
1712 .cmd_delay_gap = 3,
1713 .post_ssq_wait = ctrl->CAS + 38,
1714 .data_direction = SSQ_RD,
1715 },
1716 .sp_cmd_addr = {
1717 .address = 4,
1718 .rowbits = 0,
1719 .bank = 0,
1720 .rank = slotrank,
1721 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001722 },
1723 };
1724 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001725
Angel Pons7c49cb82020-03-16 23:17:32 +01001726 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001727 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001728
Angel Pons88521882020-01-05 20:21:20 +01001729 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001730
1731 /* disable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001732 write_mrreg(ctrl, channel, slotrank, 1,
1733 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001734}
1735
1736static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1737{
1738 int timB;
1739 int statistics[NUM_LANES][128];
1740 int lane;
1741
Angel Pons88521882020-01-05 20:21:20 +01001742 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001743
1744 for (timB = 0; timB < 128; timB++) {
1745 FOR_ALL_LANES {
1746 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1747 }
1748 program_timings(ctrl, channel);
1749
1750 test_timB(ctrl, channel, slotrank);
1751
1752 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001753 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1754 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1755 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001756 }
1757 }
1758 FOR_ALL_LANES {
1759 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001760 /*
1761 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1762 * of timB by a small value, which might cause the 6-bit value to overflow if
1763 * it's close to 0x3f. Increment the value by a small offset if it's likely
1764 * to overflow, to make sure it won't overflow while running tests and bricks
1765 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001766 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001767 * TODO: find out why some tests (edge write discovery) increment timB.
1768 */
1769 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001770 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001771 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001772 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001773
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001774 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1775 if (rn.all) {
1776 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1777 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001778
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001779 return MAKE_ERR;
1780 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001781 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1782 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001783 }
1784 return 0;
1785}
1786
1787static int get_timB_high_adjust(u64 val)
1788{
1789 int i;
1790
Angel Ponsbf13ef02020-11-11 18:40:06 +01001791 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001792 if (val == 0xffffffffffffffffLL)
1793 return 0;
1794
1795 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001796 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001797 for (i = 0; i < 8; i++)
1798 if (val << (8 * (7 - i) + 4))
1799 return -i;
1800 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001801 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001802 for (i = 0; i < 8; i++)
1803 if (val >> (8 * (7 - i) + 4))
1804 return i;
1805 }
1806 return 8;
1807}
1808
Angel Ponsbf13ef02020-11-11 18:40:06 +01001809static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001810{
1811 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001812 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813 FOR_ALL_POPULATED_CHANNELS {
1814 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001815 }
1816 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1817
Angel Pons765d4652020-11-11 14:44:35 +01001818 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001819 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001820
Angel Pons88521882020-01-05 20:21:20 +01001821 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001822
Angel Ponsffd50152020-11-12 11:03:10 +01001823 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001824
Angel Pons7c49cb82020-03-16 23:17:32 +01001825 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001826 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001827
Angel Pons88521882020-01-05 20:21:20 +01001828 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001829
Angel Pons8f0757e2020-11-11 23:03:36 +01001830 const struct iosav_ssq rd_sequence[] = {
1831 /* DRAM command PREA */
1832 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001833 .sp_cmd_ctrl = {
1834 .command = IOSAV_PRE,
1835 .ranksel_ap = 1,
1836 },
1837 .subseq_ctrl = {
1838 .cmd_executions = 1,
1839 .cmd_delay_gap = 3,
1840 .post_ssq_wait = ctrl->tRP,
1841 .data_direction = SSQ_NA,
1842 },
1843 .sp_cmd_addr = {
1844 .address = 1024,
1845 .rowbits = 6,
1846 .bank = 0,
1847 .rank = slotrank,
1848 },
1849 .addr_update = {
1850 .addr_wrap = 18,
1851 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001852 },
1853 /* DRAM command ACT */
1854 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001855 .sp_cmd_ctrl = {
1856 .command = IOSAV_ACT,
1857 .ranksel_ap = 1,
1858 },
1859 .subseq_ctrl = {
1860 .cmd_executions = 1,
1861 .cmd_delay_gap = 3,
1862 .post_ssq_wait = ctrl->tRCD,
1863 .data_direction = SSQ_NA,
1864 },
1865 .sp_cmd_addr = {
1866 .address = 0,
1867 .rowbits = 6,
1868 .bank = 0,
1869 .rank = slotrank,
1870 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001871 },
1872 /* DRAM command RD */
1873 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001874 .sp_cmd_ctrl = {
1875 .command = IOSAV_RD,
1876 .ranksel_ap = 3,
1877 },
1878 .subseq_ctrl = {
1879 .cmd_executions = 1,
1880 .cmd_delay_gap = 3,
1881 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001882 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001883 ctrl->timings[channel][slotrank].io_latency,
1884 .data_direction = SSQ_RD,
1885 },
1886 .sp_cmd_addr = {
1887 .address = 8,
1888 .rowbits = 6,
1889 .bank = 0,
1890 .rank = slotrank,
1891 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001892 },
1893 };
1894 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001895
Angel Pons7c49cb82020-03-16 23:17:32 +01001896 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001897 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001898
Angel Pons88521882020-01-05 20:21:20 +01001899 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001900 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001901 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001902 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001903 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001904 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1905 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1906 get_timB_high_adjust(res) * 64;
1907
1908 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001909 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1910 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001911 }
1912 }
Angel Pons88521882020-01-05 20:21:20 +01001913 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001914}
1915
Angel Pons88521882020-01-05 20:21:20 +01001916static void write_op(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001917{
1918 int slotrank;
1919
Angel Pons88521882020-01-05 20:21:20 +01001920 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001921
1922 /* choose an existing rank. */
1923 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
1924
Angel Ponsffd50152020-11-12 11:03:10 +01001925 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001926
Angel Pons7c49cb82020-03-16 23:17:32 +01001927 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001928 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001929
Angel Pons88521882020-01-05 20:21:20 +01001930 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001931}
1932
Angel Pons7c49cb82020-03-16 23:17:32 +01001933/*
1934 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001935 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001936 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1937 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1938 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1939 * CLK/ADDR/CMD signals have the same routing delay.
1940 *
1941 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1942 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1943 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001944 */
Angel Pons88521882020-01-05 20:21:20 +01001945int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001946{
1947 int channel, slotrank, lane;
1948 int err;
1949
1950 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01001951 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001952
1953 FOR_ALL_POPULATED_CHANNELS {
1954 write_op(ctrl, channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01001955 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001956 }
1957
Angel Pons7c49cb82020-03-16 23:17:32 +01001958 /* Refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001959 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001960 FOR_ALL_POPULATED_CHANNELS {
1961 write_op(ctrl, channel);
1962 }
1963
Angel Pons7c49cb82020-03-16 23:17:32 +01001964 /* Enable write leveling on all ranks
1965 Disable all DQ outputs
1966 Only NOP is allowed in this mode */
1967 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1968 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001969 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001970
Angel Pons88521882020-01-05 20:21:20 +01001971 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972
1973 toggle_io_reset();
1974
Angel Pons7c49cb82020-03-16 23:17:32 +01001975 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001976 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1977 err = discover_timB(ctrl, channel, slotrank);
1978 if (err)
1979 return err;
1980 }
1981
Angel Pons7c49cb82020-03-16 23:17:32 +01001982 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001983 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001984 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985
Angel Pons88521882020-01-05 20:21:20 +01001986 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001987
1988 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001989 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001990
Angel Pons7c49cb82020-03-16 23:17:32 +01001991 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001992 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001993
1994 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001995 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001996 MCHBAR32(IOSAV_STATUS_ch(channel));
1997 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001998
Angel Ponsffd50152020-11-12 11:03:10 +01001999 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002000
Angel Pons7c49cb82020-03-16 23:17:32 +01002001 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002002 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002003
Angel Pons88521882020-01-05 20:21:20 +01002004 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002005 }
2006
2007 toggle_io_reset();
2008
2009 printram("CPE\n");
2010 precharge(ctrl);
2011 printram("CPF\n");
2012
2013 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002014 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002015 }
2016
2017 FOR_ALL_POPULATED_CHANNELS {
2018 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002019 }
2020
2021 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2022 err = discover_timC(ctrl, channel, slotrank);
2023 if (err)
2024 return err;
2025 }
2026
2027 FOR_ALL_POPULATED_CHANNELS
2028 program_timings(ctrl, channel);
2029
2030 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002031 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002032
2033 FOR_ALL_POPULATED_CHANNELS
2034 program_timings(ctrl, channel);
2035
2036 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002037 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002038 }
2039 return 0;
2040}
2041
Angel Ponsbf13ef02020-11-11 18:40:06 +01002042static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002043{
2044 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2045 int timC_delta;
2046 int lanes_ok = 0;
2047 int ctr = 0;
2048 int lane;
2049
2050 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2051 FOR_ALL_LANES {
2052 ctrl->timings[channel][slotrank].lanes[lane].timC =
2053 saved_rt.lanes[lane].timC + timC_delta;
2054 }
2055 program_timings(ctrl, channel);
2056 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002057 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002058 }
2059
Angel Pons765d4652020-11-11 14:44:35 +01002060 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002061 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002062
Angel Pons88521882020-01-05 20:21:20 +01002063 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002064
Angel Ponsffd50152020-11-12 11:03:10 +01002065 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002066
2067 /* Program LFSR for the RD/WR subsequences */
2068 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2069 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002070
Angel Pons7c49cb82020-03-16 23:17:32 +01002071 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002072 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002073
Angel Pons88521882020-01-05 20:21:20 +01002074 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002075 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002076 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002077
2078 if (r32 == 0)
2079 lanes_ok |= 1 << lane;
2080 }
2081 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002082 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002083 break;
2084 }
2085
2086 ctrl->timings[channel][slotrank] = saved_rt;
2087
Patrick Rudolphdd662872017-10-28 18:20:11 +02002088 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002089}
2090
Angel Pons88521882020-01-05 20:21:20 +01002091static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002092{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302093 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002094 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2095 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002096
2097 if (patno) {
2098 u8 base8 = 0x80 >> ((patno - 1) % 8);
2099 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2100 for (i = 0; i < 32; i++) {
2101 for (j = 0; j < 16; j++) {
2102 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002103
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002104 if (invert[patno - 1][i] & (1 << (j / 2)))
2105 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002106
2107 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002108 }
2109 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002110 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002111 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2112 for (j = 0; j < 16; j++) {
2113 const u32 val = pattern[i][j];
2114 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2115 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002116 }
2117 sfence();
2118 }
Angel Pons765d4652020-11-11 14:44:35 +01002119
2120 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002121}
2122
Angel Pons88521882020-01-05 20:21:20 +01002123static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002124{
2125 int channel, slotrank;
2126
2127 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002128 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002129
Angel Pons7c49cb82020-03-16 23:17:32 +01002130 /* Choose an existing rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002131 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2132
Angel Ponsffd50152020-11-12 11:03:10 +01002133 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002134
Angel Pons7c49cb82020-03-16 23:17:32 +01002135 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002136 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002137
Angel Pons88521882020-01-05 20:21:20 +01002138 wait_for_iosav(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002139 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002140 }
2141
2142 /* refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002143 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002144 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002145 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146
2147 /* choose an existing rank. */
2148 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2149
Angel Ponsffd50152020-11-12 11:03:10 +01002150 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002151
Angel Pons7c49cb82020-03-16 23:17:32 +01002152 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002153 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002154
Angel Pons88521882020-01-05 20:21:20 +01002155 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002156 }
2157
Angel Pons7c49cb82020-03-16 23:17:32 +01002158 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002160
2161 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002162 dram_mrscommands(ctrl);
2163
2164 toggle_io_reset();
2165}
2166
Angel Ponsbf13ef02020-11-11 18:40:06 +01002167#define CT_MIN_PI -127
2168#define CT_MAX_PI 128
2169#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2170
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002171#define MIN_C320C_LEN 13
2172
2173static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2174{
2175 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2176 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002177 int command_pi;
2178 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002179 int delta = 0;
2180
2181 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2182
2183 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002184 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002185 }
2186
2187 ctrl->cmd_stretch[channel] = cmd_stretch;
2188
Angel Pons88521882020-01-05 20:21:20 +01002189 MCHBAR32(TC_RAP_ch(channel)) =
Angel Pons7c49cb82020-03-16 23:17:32 +01002190 (ctrl->tRRD << 0)
2191 | (ctrl->tRTP << 4)
2192 | (ctrl->tCKE << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002193 | (ctrl->tWTR << 12)
2194 | (ctrl->tFAW << 16)
Angel Pons7c49cb82020-03-16 23:17:32 +01002195 | (ctrl->tWR << 24)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002196 | (ctrl->cmd_stretch[channel] << 30);
2197
2198 if (ctrl->cmd_stretch[channel] == 2)
2199 delta = 2;
2200 else if (ctrl->cmd_stretch[channel] == 0)
2201 delta = 4;
2202
2203 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002204 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002205 }
2206
Angel Ponsbf13ef02020-11-11 18:40:06 +01002207 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002208 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002209 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002210 }
2211 program_timings(ctrl, channel);
2212 reprogram_320c(ctrl);
2213 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002214 stat[slotrank][command_pi - CT_MIN_PI] =
2215 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002216 }
2217 }
2218 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002219 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002220
Angel Ponsbf13ef02020-11-11 18:40:06 +01002221 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002222 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2223 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002224
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002225 if (rn.all || rn.length < MIN_C320C_LEN) {
2226 FOR_ALL_POPULATED_RANKS {
2227 ctrl->timings[channel][slotrank] =
2228 saved_timings[channel][slotrank];
2229 }
2230 return MAKE_ERR;
2231 }
2232 }
2233
2234 return 0;
2235}
2236
Angel Pons7c49cb82020-03-16 23:17:32 +01002237/*
2238 * Adjust CMD phase shift and try multiple command rates.
2239 * A command rate of 2T doubles the time needed for address and command decode.
2240 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002241int command_training(ramctr_timing *ctrl)
2242{
2243 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002244
2245 FOR_ALL_POPULATED_CHANNELS {
2246 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002247 }
2248
2249 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002250 int cmdrate, err;
2251
2252 /*
2253 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002254 * Issue:
2255 * While c320c discovery seems to succeed raminit will fail in write training.
2256 *
2257 * Workaround:
2258 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2259 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002260 *
2261 * Single DIMM per channel:
2262 * Try command rate 1T and 2T
2263 */
2264 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002265 if (ctrl->tCMD)
2266 /* XMP gives the CMD rate in clock ticks, not ns */
2267 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002268
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002269 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002270 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2271
2272 if (!err)
2273 break;
2274 }
2275
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002276 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002277 printk(BIOS_EMERG, "c320c discovery failed\n");
2278 return err;
2279 }
2280
Angel Pons891f2bc2020-01-10 01:27:28 +01002281 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002282 }
2283
2284 FOR_ALL_POPULATED_CHANNELS
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002285 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002286
2287 reprogram_320c(ctrl);
2288 return 0;
2289}
2290
Angel Pons891f2bc2020-01-10 01:27:28 +01002291static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002292{
2293 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002294 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002295 int lane;
2296
2297 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2298 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002299 ctrl->timings[channel][slotrank].lanes[lane].rising = edge;
Angel Pons891f2bc2020-01-10 01:27:28 +01002300 ctrl->timings[channel][slotrank].lanes[lane].falling = edge;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002301 }
2302 program_timings(ctrl, channel);
2303
2304 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002305 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2306 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307 }
2308
Angel Pons88521882020-01-05 20:21:20 +01002309 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002310
Angel Ponsffd50152020-11-12 11:03:10 +01002311 iosav_write_read_mpr_sequence(
2312 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002313
Angel Pons7c49cb82020-03-16 23:17:32 +01002314 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002315 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002316
Angel Pons88521882020-01-05 20:21:20 +01002317 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002318
2319 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002320 stats[lane][edge] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002321 }
2322 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002323
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002325 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002326 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002327
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002328 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002329 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2330 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002331 return MAKE_ERR;
2332 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002333 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002334 }
2335 return 0;
2336}
2337
2338int discover_edges(ramctr_timing *ctrl)
2339{
2340 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2341 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2342 int channel, slotrank, lane;
2343 int err;
2344
Angel Pons88521882020-01-05 20:21:20 +01002345 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002346
2347 toggle_io_reset();
2348
2349 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002350 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002351 }
2352
2353 FOR_ALL_POPULATED_CHANNELS {
2354 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002355 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002356 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002357 }
2358
2359 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01002360 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2361 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002362 }
2363
2364 program_timings(ctrl, channel);
2365
2366 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002367 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002368
Angel Ponsffd50152020-11-12 11:03:10 +01002369 iosav_write_read_mpr_sequence(
2370 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02002371
Angel Pons7c49cb82020-03-16 23:17:32 +01002372 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002373 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002374
Angel Pons88521882020-01-05 20:21:20 +01002375 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002376 }
2377
2378 /* XXX: check any measured value ? */
2379
2380 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01002381 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
Angel Pons7c49cb82020-03-16 23:17:32 +01002382 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002383 }
2384
2385 program_timings(ctrl, channel);
2386
2387 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002388 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002389
Angel Ponsffd50152020-11-12 11:03:10 +01002390 iosav_write_read_mpr_sequence(
2391 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002392
Angel Pons7c49cb82020-03-16 23:17:32 +01002393 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002394 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002395
Angel Pons88521882020-01-05 20:21:20 +01002396 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002397 }
2398
2399 /* XXX: check any measured value ? */
2400
2401 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002402 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
Angel Pons891f2bc2020-01-10 01:27:28 +01002403 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002404 }
2405
2406 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002407 }
2408
Angel Pons0c3936e2020-03-22 12:49:27 +01002409 /*
2410 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2411 * also use a single loop. It would seem that it is a debugging configuration.
2412 */
Angel Pons88521882020-01-05 20:21:20 +01002413 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2414 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002415
2416 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2417 err = discover_edges_real(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002418 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002419 if (err)
2420 return err;
2421 }
2422
Angel Pons88521882020-01-05 20:21:20 +01002423 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2424 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002425
2426 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2427 err = discover_edges_real(ctrl, channel, slotrank,
2428 rising_edges[channel][slotrank]);
2429 if (err)
2430 return err;
2431 }
2432
Angel Pons88521882020-01-05 20:21:20 +01002433 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002434
2435 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2436 ctrl->timings[channel][slotrank].lanes[lane].falling =
2437 falling_edges[channel][slotrank][lane];
2438 ctrl->timings[channel][slotrank].lanes[lane].rising =
2439 rising_edges[channel][slotrank][lane];
2440 }
2441
2442 FOR_ALL_POPULATED_CHANNELS {
2443 program_timings(ctrl, channel);
2444 }
2445
2446 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002447 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002448 }
2449 return 0;
2450}
2451
Angel Pons7c49cb82020-03-16 23:17:32 +01002452static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002453{
2454 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002455 u32 raw_stats[MAX_EDGE_TIMING + 1];
2456 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002457 const int reg3000b24[] = { 0, 0xc, 0x2c };
2458 int lane, i;
2459 int lower[NUM_LANES];
2460 int upper[NUM_LANES];
2461 int pat;
2462
2463 FOR_ALL_LANES {
2464 lower[lane] = 0;
2465 upper[lane] = MAX_EDGE_TIMING;
2466 }
2467
2468 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002469 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002470 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2471
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002472 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2473 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002474 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002475
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002476 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2477 FOR_ALL_LANES {
2478 ctrl->timings[channel][slotrank].lanes[lane].
2479 rising = edge;
2480 ctrl->timings[channel][slotrank].lanes[lane].
2481 falling = edge;
2482 }
2483 program_timings(ctrl, channel);
2484
2485 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002486 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2487 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002488 }
Angel Pons88521882020-01-05 20:21:20 +01002489 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002490
Angel Ponsffd50152020-11-12 11:03:10 +01002491 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002492
Angel Pons7c49cb82020-03-16 23:17:32 +01002493 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002494 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002495
Angel Pons88521882020-01-05 20:21:20 +01002496 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002497 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002498 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499 }
2500
Angel Pons7c49cb82020-03-16 23:17:32 +01002501 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002502 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002503 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002504
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002505 FOR_ALL_LANES {
2506 struct run rn;
2507 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002508 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2509
2510 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2511
2512 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2513 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2514 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002515 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002516
2517 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2518 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2519
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002520 edges[lane] = (lower[lane] + upper[lane]) / 2;
2521 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002522 printk(BIOS_EMERG, "edge write discovery failed: "
2523 "%d, %d, %d\n", channel, slotrank, lane);
2524
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002525 return MAKE_ERR;
2526 }
2527 }
2528 }
2529 }
2530
Angel Pons88521882020-01-05 20:21:20 +01002531 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002532 printram("CPA\n");
2533 return 0;
2534}
2535
2536int discover_edges_write(ramctr_timing *ctrl)
2537{
2538 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002539 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2540 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002541
Angel Pons7c49cb82020-03-16 23:17:32 +01002542 /*
2543 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2544 * also use a single loop. It would seem that it is a debugging configuration.
2545 */
Angel Pons88521882020-01-05 20:21:20 +01002546 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2547 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002548
2549 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2550 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002551 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002552 if (err)
2553 return err;
2554 }
2555
Angel Pons88521882020-01-05 20:21:20 +01002556 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2557 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002558
2559 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2560 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002561 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002562 if (err)
2563 return err;
2564 }
2565
Angel Pons88521882020-01-05 20:21:20 +01002566 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002567
2568 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2569 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002570 falling_edges[channel][slotrank][lane];
2571
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002572 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002573 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002574 }
2575
2576 FOR_ALL_POPULATED_CHANNELS
2577 program_timings(ctrl, channel);
2578
2579 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002580 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002581 }
2582 return 0;
2583}
2584
2585static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2586{
Angel Pons88521882020-01-05 20:21:20 +01002587 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002588
Angel Ponsffd50152020-11-12 11:03:10 +01002589 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002590
Angel Pons7c49cb82020-03-16 23:17:32 +01002591 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002592 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002593
Angel Pons88521882020-01-05 20:21:20 +01002594 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002595}
2596
2597int discover_timC_write(ramctr_timing *ctrl)
2598{
Angel Pons7c49cb82020-03-16 23:17:32 +01002599 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002600 int i, pat;
2601
2602 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2603 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2604 int channel, slotrank, lane;
2605
2606 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2607 lower[channel][slotrank][lane] = 0;
2608 upper[channel][slotrank][lane] = MAX_TIMC;
2609 }
2610
Angel Pons88521882020-01-05 20:21:20 +01002611 /*
2612 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2613 * FIXME: This must only be done on Ivy Bridge.
2614 */
2615 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002616 printram("discover timC write:\n");
2617
2618 for (i = 0; i < 3; i++)
2619 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002620
2621 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2622 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2623 ~0x3f000000, rege3c_b24[i] << 24);
2624
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002625 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002626
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002627 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2628 FOR_ALL_POPULATED_RANKS {
2629 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002630 u32 raw_stats[MAX_TIMC + 1];
2631 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002632
2633 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002634 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002635
2636 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002637
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002638 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002639 FOR_ALL_LANES {
2640 ctrl->timings[channel][slotrank]
2641 .lanes[lane].timC = timC;
2642 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002643 program_timings(ctrl, channel);
2644
2645 test_timC_write (ctrl, channel, slotrank);
2646
Angel Pons7c49cb82020-03-16 23:17:32 +01002647 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002648 raw_stats[timC] = MCHBAR32(
2649 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002650 }
2651 FOR_ALL_LANES {
2652 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002653 for (timC = 0; timC < MAX_TIMC; timC++) {
2654 stats[timC] = !!(raw_stats[timC]
2655 & (1 << lane));
2656 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002657
Angel Pons7c49cb82020-03-16 23:17:32 +01002658 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002659 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002660 printk(BIOS_EMERG,
2661 "timC write discovery failed: "
2662 "%d, %d, %d\n", channel,
2663 slotrank, lane);
2664
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002665 return MAKE_ERR;
2666 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002667 printram("timC: %d, %d, %d: "
2668 "0x%02x-0x%02x-0x%02x, "
2669 "0x%02x-0x%02x\n", channel, slotrank,
2670 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002671 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002672 rn.end - ctrl->timC_offset[i]);
2673
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002674 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002675 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002676 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002677
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002678 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002679 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002680 upper[channel][slotrank][lane]);
2681
2682 }
2683 }
2684 }
2685 }
2686
2687 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002688 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002689 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002690 udelay(2);
2691 }
2692
Angel Pons88521882020-01-05 20:21:20 +01002693 /*
2694 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2695 * FIXME: This must only be done on Ivy Bridge.
2696 */
2697 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002698
2699 printram("CPB\n");
2700
2701 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002702 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002703 (lower[channel][slotrank][lane] +
2704 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002705
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002706 ctrl->timings[channel][slotrank].lanes[lane].timC =
2707 (lower[channel][slotrank][lane] +
2708 upper[channel][slotrank][lane]) / 2;
2709 }
2710 FOR_ALL_POPULATED_CHANNELS {
2711 program_timings(ctrl, channel);
2712 }
2713 return 0;
2714}
2715
Angel Pons88521882020-01-05 20:21:20 +01002716void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002717{
2718 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002719 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002720
2721 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2722 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002723 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002724 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002725 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002726 printram("normalize %d, %d, %d: mat %d\n",
2727 channel, slotrank, lane, mat);
2728
Felix Heldef4fe3e2019-12-31 14:15:05 +01002729 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002730 printram("normalize %d, %d, %d: delta %d\n",
2731 channel, slotrank, lane, delta);
2732
Angel Pons88521882020-01-05 20:21:20 +01002733 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002734 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002735 }
2736
2737 FOR_ALL_POPULATED_CHANNELS {
2738 program_timings(ctrl, channel);
2739 }
2740}
2741
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002742int channel_test(ramctr_timing *ctrl)
2743{
2744 int channel, slotrank, lane;
2745
2746 slotrank = 0;
2747 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002748 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002749 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002750 return MAKE_ERR;
2751 }
2752 FOR_ALL_POPULATED_CHANNELS {
2753 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002754 }
2755
2756 for (slotrank = 0; slotrank < 4; slotrank++)
2757 FOR_ALL_CHANNELS
2758 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2759 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002760 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2761 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002762 }
Angel Pons88521882020-01-05 20:21:20 +01002763 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002764
Angel Ponsffd50152020-11-12 11:03:10 +01002765 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002766
Angel Pons7c49cb82020-03-16 23:17:32 +01002767 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002768 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002769
Angel Pons88521882020-01-05 20:21:20 +01002770 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002771 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002772 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002773 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2774 channel, slotrank, lane);
2775 return MAKE_ERR;
2776 }
2777 }
2778 return 0;
2779}
2780
Patrick Rudolphdd662872017-10-28 18:20:11 +02002781void channel_scrub(ramctr_timing *ctrl)
2782{
2783 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002784 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002785
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002786 FOR_ALL_POPULATED_CHANNELS {
2787 wait_for_iosav(channel);
2788 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002789 }
2790
2791 /*
2792 * During runtime the "scrubber" will periodically scan through the memory in the
2793 * physical address space, to identify and fix CRC errors.
2794 * The following loops writes to every DRAM address, setting the ECC bits to the
2795 * correct value. A read from this location will no longer return a CRC error,
2796 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002797 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002798 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2799 * and firmware running in x86_32.
2800 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002801 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2802 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002803 for (bank = 0; bank < 8; bank++) {
2804 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002805
Angel Pons8f0757e2020-11-11 23:03:36 +01002806 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2807 const struct iosav_ssq sequence[] = {
2808 /*
2809 * DRAM command ACT
2810 * Opens the row for writing.
2811 */
2812 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002813 .sp_cmd_ctrl = {
2814 .command = IOSAV_ACT,
2815 .ranksel_ap = 1,
2816 },
2817 .subseq_ctrl = {
2818 .cmd_executions = 1,
2819 .cmd_delay_gap = gap,
2820 .post_ssq_wait = ctrl->tRCD,
2821 .data_direction = SSQ_NA,
2822 },
2823 .sp_cmd_addr = {
2824 .address = row,
2825 .rowbits = 6,
2826 .bank = bank,
2827 .rank = slotrank,
2828 },
2829 .addr_update = {
2830 .inc_addr_1 = 1,
2831 .addr_wrap = 18,
2832 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002833 },
2834 /*
2835 * DRAM command WR
2836 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2837 * bytes.
2838 */
2839 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002840 .sp_cmd_ctrl = {
2841 .command = IOSAV_WR,
2842 .ranksel_ap = 1,
2843 },
2844 .subseq_ctrl = {
2845 .cmd_executions = 129,
2846 .cmd_delay_gap = 4,
2847 .post_ssq_wait = ctrl->tWTR +
2848 ctrl->CWL + 8,
2849 .data_direction = SSQ_WR,
2850 },
2851 .sp_cmd_addr = {
2852 .address = row,
2853 .rowbits = 0,
2854 .bank = bank,
2855 .rank = slotrank,
2856 },
2857 .addr_update = {
2858 .inc_addr_8 = 1,
2859 .addr_wrap = 9,
2860 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002861 },
2862 /*
2863 * DRAM command PRE
2864 * Closes the row.
2865 */
2866 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002867 .sp_cmd_ctrl = {
2868 .command = IOSAV_PRE,
2869 .ranksel_ap = 1,
2870 },
2871 .subseq_ctrl = {
2872 .cmd_executions = 1,
2873 .cmd_delay_gap = 4,
2874 .post_ssq_wait = ctrl->tRP,
2875 .data_direction = SSQ_NA,
2876 },
2877 .sp_cmd_addr = {
2878 .address = 0,
2879 .rowbits = 6,
2880 .bank = bank,
2881 .rank = slotrank,
2882 },
2883 .addr_update = {
Angel Ponsfd9a8b62020-11-13 13:56:30 +01002884 .addr_wrap = 18,
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002885 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002886 },
2887 };
2888 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002889
2890 /* Execute command queue */
2891 iosav_run_queue(channel, 16, 0);
2892
2893 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002894 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002895 }
2896 }
2897}
2898
Angel Pons88521882020-01-05 20:21:20 +01002899void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002900{
2901 int channel;
2902
Angel Pons7c49cb82020-03-16 23:17:32 +01002903 /* 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 +01002904 static u32 seeds[NUM_CHANNELS][3] = {
2905 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2906 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2907 };
2908 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002909 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002910 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2911 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2912 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002913 }
2914}
2915
Angel Pons89ae6b82020-03-21 13:23:32 +01002916void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002917{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002918 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002919 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002921 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002922 }
2923}
2924
Angel Pons88521882020-01-05 20:21:20 +01002925void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002926{
2927 int channel;
2928
2929 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002930 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002931 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002932 }
2933
2934 udelay(1);
2935
2936 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002937 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002938 }
2939}
2940
Angel Pons7c49cb82020-03-16 23:17:32 +01002941void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002942{
2943 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002944
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002945 FOR_ALL_POPULATED_CHANNELS {
2946 u32 b20, b4_8_12;
Angel Pons88521882020-01-05 20:21:20 +01002947 int min_pi = 10000;
2948 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002949
2950 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002951 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2952 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002953 }
2954
Angel Pons7c49cb82020-03-16 23:17:32 +01002955 b20 = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002956
Angel Pons7c49cb82020-03-16 23:17:32 +01002957 b4_8_12 = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 0x3330 : 0x2220;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002958
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002959 dram_odt_stretch(ctrl, channel);
2960
Angel Ponsdc5539f2020-11-12 12:44:25 +01002961 MCHBAR32(TC_RWP_ch(channel)) = (1 << 27) | (2 << 24) | (b20 << 20) |
Felix Held2463aa92018-07-29 21:37:55 +02002962 ((ctrl->ref_card_offset[channel] + 2) << 16) | b4_8_12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002963 }
2964}
2965
Angel Pons88521882020-01-05 20:21:20 +01002966void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002967{
2968 int channel;
2969 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002970 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2971 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002972 }
2973}
2974
Angel Pons7c49cb82020-03-16 23:17:32 +01002975/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2976static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002977{
Angel Pons88521882020-01-05 20:21:20 +01002978 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002979}
2980
Angel Pons7c49cb82020-03-16 23:17:32 +01002981/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002982void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002983{
Angel Ponsb50ca572020-11-11 19:07:20 +01002984 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01002985
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002986 int channel;
2987 int t1_cycles = 0, t1_ns = 0, t2_ns;
2988 int t3_ns;
2989 u32 r32;
2990
Angel Pons7c49cb82020-03-16 23:17:32 +01002991 /* FIXME: This register only exists on Ivy Bridge */
2992 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002993
Felix Heldf9b826a2018-07-30 17:56:52 +02002994 FOR_ALL_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002995 MCHBAR32_AND_OR(TC_OTHP_ch(channel), ~(3 << 12), 1 << 12);
Patrick Rudolph652c4912017-10-31 11:36:55 +01002996
Patrick Rudolph74203de2017-11-20 11:57:01 +01002997 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01002998 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01002999 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003000 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003001 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003002 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003003
Felix Heldf9b826a2018-07-30 17:56:52 +02003004 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003005 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003006
Angel Pons88521882020-01-05 20:21:20 +01003007 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3008 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003009
3010 FOR_ALL_CHANNELS {
3011 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003012 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003013 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003014 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003015 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003016 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003017 case 1:
3018 case 4:
3019 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003020 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003021 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003022 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003023 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003024 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003025 break;
3026 }
3027 }
3028
Felix Held50b7ed22019-12-30 20:41:54 +01003029 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003030 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003031 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003032
3033 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003034 MCHBAR32_AND_OR(TC_RFP_ch(channel), ~(3 << 16), 1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003035
Angel Ponsdc5539f2020-11-12 12:44:25 +01003036 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3037 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003038 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003039
Angel Pons7c49cb82020-03-16 23:17:32 +01003040 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003041 FOR_ALL_POPULATED_CHANNELS
3042 break;
3043
Angel Pons88521882020-01-05 20:21:20 +01003044 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3045 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003046 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003047 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003048 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003049 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003050 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003051 t1_ns += 500;
3052
Angel Pons88521882020-01-05 20:21:20 +01003053 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003054 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003055 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003056 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003057 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003058 t3_ns = 500;
3059 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003060
3061 /* The graphics driver will use these watermark values */
3062 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003063 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003064 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3065 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003066}
3067
Angel Pons88521882020-01-05 20:21:20 +01003068void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003069{
3070 int channel, slotrank, lane;
3071
Angel Pons7c49cb82020-03-16 23:17:32 +01003072 FOR_ALL_POPULATED_CHANNELS {
3073 MCHBAR32(TC_RAP_ch(channel)) =
3074 (ctrl->tRRD << 0)
3075 | (ctrl->tRTP << 4)
3076 | (ctrl->tCKE << 8)
3077 | (ctrl->tWTR << 12)
3078 | (ctrl->tFAW << 16)
3079 | (ctrl->tWR << 24)
3080 | (ctrl->cmd_stretch[channel] << 30);
3081 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003082
3083 udelay(1);
3084
3085 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003086 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003087 }
3088
3089 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003090 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003091 }
3092
3093 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003094 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003095
3096 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003097 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003098 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003099 }
3100
3101 printram("CPE\n");
3102
Angel Pons88521882020-01-05 20:21:20 +01003103 MCHBAR32(GDCRTRAININGMOD) = 0;
3104 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003105
3106 printram("CP5b\n");
3107
3108 FOR_ALL_POPULATED_CHANNELS {
3109 program_timings(ctrl, channel);
3110 }
3111
3112 u32 reg, addr;
3113
Angel Pons7c49cb82020-03-16 23:17:32 +01003114 /* Poll for RCOMP */
3115 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3116 ;
3117
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003118 do {
Angel Pons88521882020-01-05 20:21:20 +01003119 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003120 } while ((reg & 0x14) == 0);
3121
Angel Pons7c49cb82020-03-16 23:17:32 +01003122 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003123 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003124 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003125
Angel Pons7c49cb82020-03-16 23:17:32 +01003126 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003127 udelay(500);
3128
3129 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003130 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003131 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003132 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003133 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003134 MCHBAR32(addr) = reg;
3135
Angel Pons7c49cb82020-03-16 23:17:32 +01003136 /* Wait 10ns for ranks to settle */
3137 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003138
3139 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3140 MCHBAR32(addr) = reg;
3141
Angel Pons7c49cb82020-03-16 23:17:32 +01003142 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003143 write_reset(ctrl);
3144 }
3145
Angel Pons7c49cb82020-03-16 23:17:32 +01003146 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003147 dram_mrscommands(ctrl);
3148
3149 printram("CP5c\n");
3150
Angel Pons88521882020-01-05 20:21:20 +01003151 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003152
3153 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003154 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003155 udelay(2);
3156 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003157}