blob: 4d478a06abaa51999a33077281d090b46a9ac32a [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 = {
653 .command = IOSAV_MRS,
654 },
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{
760 u16 pasr, cwl, mr2reg;
761 odtmap odt;
762 int srt;
763
764 pasr = 0;
765 cwl = ctrl->CWL - 5;
Angel Ponsf9997482020-11-12 16:02:52 +0100766 odt = get_ODT(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100767
768 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
769
770 mr2reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100771 mr2reg = (mr2reg & ~0x07) | pasr;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100772 mr2reg = (mr2reg & ~0x38) | (cwl << 3);
773 mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6);
774 mr2reg = (mr2reg & ~0x80) | (srt << 7);
775 mr2reg |= (odt.rttwr / 60) << 9;
776
777 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100778
779 /* Program MR2 shadow */
780 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
781
782 reg32 &= 3 << 14 | 3 << 6;
783
784 reg32 |= mr2reg & ~(3 << 6);
785
786 if (rank & 1) {
787 if (srt)
788 reg32 |= 1 << (rank / 2 + 6);
789 } else {
790 if (ctrl->rank_mirror[channel][rank])
791 reg32 |= 1 << (rank / 2 + 14);
792 }
793 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100794}
795
796static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
797{
798 write_mrreg(ctrl, channel, rank, 3, 0);
799}
800
Angel Pons88521882020-01-05 20:21:20 +0100801void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100802{
803 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100804 int channel;
805
806 FOR_ALL_POPULATED_CHANNELS {
807 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100808 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100809 dram_mr2(ctrl, slotrank, channel);
810
Angel Pons7c49cb82020-03-16 23:17:32 +0100811 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100812 dram_mr3(ctrl, slotrank, channel);
813
Angel Pons7c49cb82020-03-16 23:17:32 +0100814 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100815 dram_mr1(ctrl, slotrank, channel);
816
Angel Pons7c49cb82020-03-16 23:17:32 +0100817 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100818 dram_mr0(ctrl, slotrank, channel);
819 }
820 }
821
Angel Pons8f0757e2020-11-11 23:03:36 +0100822 const struct iosav_ssq zqcl_sequence[] = {
823 /* DRAM command NOP (without ODT nor chip selects) */
824 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200825 .sp_cmd_ctrl = {
826 .command = IOSAV_NOP & ~(0xff << 8),
827 },
828 .subseq_ctrl = {
829 .cmd_executions = 1,
830 .cmd_delay_gap = 4,
831 .post_ssq_wait = 15,
832 .data_direction = SSQ_NA,
833 },
834 .sp_cmd_addr = {
835 .address = 2,
836 .rowbits = 6,
837 .bank = 0,
838 .rank = 0,
839 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100840 },
841 /* DRAM command ZQCL */
842 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200843 .sp_cmd_ctrl = {
844 .command = IOSAV_ZQCS,
845 .ranksel_ap = 1,
846 },
847 .subseq_ctrl = {
848 .cmd_executions = 1,
849 .cmd_delay_gap = 4,
850 .post_ssq_wait = 400,
851 .data_direction = SSQ_NA,
852 },
853 .sp_cmd_addr = {
854 .address = 1024,
855 .rowbits = 6,
856 .bank = 0,
857 .rank = 0,
858 },
859 .addr_update = {
860 .inc_rank = 1,
861 .addr_wrap = 20,
862 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100863 },
864 };
865 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100866
Angel Pons7c49cb82020-03-16 23:17:32 +0100867 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200868 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100869
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100870 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100871 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100872 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100873 }
874
Angel Pons7c49cb82020-03-16 23:17:32 +0100875 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100876 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100877
878 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100879 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100880
Angel Pons88521882020-01-05 20:21:20 +0100881 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100882
883 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
884
Angel Pons7c49cb82020-03-16 23:17:32 +0100885 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100886 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100887
Angel Ponsffd50152020-11-12 11:03:10 +0100888 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200889
Angel Pons7c49cb82020-03-16 23:17:32 +0100890 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200891 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100892
Angel Pons7c49cb82020-03-16 23:17:32 +0100893 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100894 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100895 }
896}
897
Felix Held3b906032020-01-14 17:05:43 +0100898static const u32 lane_base[] = {
899 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
900 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
901 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100902};
903
Angel Pons88521882020-01-05 20:21:20 +0100904void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100905{
Angel Pons88521882020-01-05 20:21:20 +0100906 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100907 int lane;
908 int slotrank, slot;
909 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100910 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100911
912 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100913 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
914 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100915 }
916
917 for (slot = 0; slot < NUM_SLOTS; slot++)
918 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
919 case 0:
920 default:
Angel Pons88521882020-01-05 20:21:20 +0100921 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100922 break;
923 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100924 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100925 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100926 break;
927 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100928 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100929 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100930 break;
931 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100932 pi_coding_ctrl[slot] =
933 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100934 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100935 break;
936 }
937
Angel Pons7c49cb82020-03-16 23:17:32 +0100938 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100939 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100940 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
941 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100942 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100943 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
944
Angel Pons88521882020-01-05 20:21:20 +0100945 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100946
Angel Pons7c49cb82020-03-16 23:17:32 +0100947 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100948 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
949 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100950
951 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100952 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100953 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100954 if (shift < 0)
955 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100956
Angel Pons88521882020-01-05 20:21:20 +0100957 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100958
959 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100960 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
961 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100962 }
963
Angel Pons88521882020-01-05 20:21:20 +0100964 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
965 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100966
Angel Pons88521882020-01-05 20:21:20 +0100967 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100968 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100969
Angel Pons88521882020-01-05 20:21:20 +0100970 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100971
972 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100973 int post_timA_min_high = 7, pre_timA_min_high = 7;
974 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100975 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100976 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100977
978 if (shift < 0)
979 shift = 0;
980
981 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200982 post_timA_min_high = MIN(post_timA_min_high,
983 (ctrl->timings[channel][slotrank].lanes[lane].
984 timA + shift) >> 6);
985 pre_timA_min_high = MIN(pre_timA_min_high,
986 ctrl->timings[channel][slotrank].lanes[lane].
987 timA >> 6);
988 post_timA_max_high = MAX(post_timA_max_high,
989 (ctrl->timings[channel][slotrank].lanes[lane].
990 timA + shift) >> 6);
991 pre_timA_max_high = MAX(pre_timA_max_high,
992 ctrl->timings[channel][slotrank].lanes[lane].
993 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100994 }
995
996 if (pre_timA_max_high - pre_timA_min_high <
997 post_timA_max_high - post_timA_min_high)
998 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +0100999
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001000 else if (pre_timA_max_high - pre_timA_min_high >
1001 post_timA_max_high - post_timA_min_high)
1002 shift_402x = -1;
1003
Felix Helddee167e2019-12-30 17:30:16 +01001004 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001005 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001006 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001007
Angel Pons88521882020-01-05 20:21:20 +01001008 reg_roundtrip_latency |=
1009 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001010 shift_402x) << (8 * slotrank);
1011
1012 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001013 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001014 (((ctrl->timings[channel][slotrank].lanes[lane].
1015 timA + shift) & 0x3f)
1016 |
1017 ((ctrl->timings[channel][slotrank].lanes[lane].
1018 rising + shift) << 8)
1019 |
1020 (((ctrl->timings[channel][slotrank].lanes[lane].
1021 timA + shift -
1022 (post_timA_min_high << 6)) & 0x1c0) << 10)
1023 | ((ctrl->timings[channel][slotrank].lanes[lane].
1024 falling + shift) << 20));
1025
Felix Heldfb19c8a2020-01-14 21:27:59 +01001026 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001027 (((ctrl->timings[channel][slotrank].lanes[lane].
1028 timC + shift) & 0x3f)
1029 |
1030 (((ctrl->timings[channel][slotrank].lanes[lane].
1031 timB + shift) & 0x3f) << 8)
1032 |
1033 (((ctrl->timings[channel][slotrank].lanes[lane].
1034 timB + shift) & 0x1c0) << 9)
1035 |
1036 (((ctrl->timings[channel][slotrank].lanes[lane].
1037 timC + shift) & 0x40) << 13));
1038 }
1039 }
Angel Pons88521882020-01-05 20:21:20 +01001040 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1041 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001042}
1043
Angel Pons88521882020-01-05 20:21:20 +01001044static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001045{
Angel Pons88521882020-01-05 20:21:20 +01001046 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001047
Angel Ponsffd50152020-11-12 11:03:10 +01001048 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001049
Angel Pons7c49cb82020-03-16 23:17:32 +01001050 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001051 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001052
Angel Pons88521882020-01-05 20:21:20 +01001053 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001054}
1055
Angel Pons7c49cb82020-03-16 23:17:32 +01001056static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001057{
1058 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001059
1060 return (MCHBAR32(lane_base[lane] +
1061 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001062}
1063
1064struct run {
1065 int middle;
1066 int end;
1067 int start;
1068 int all;
1069 int length;
1070};
1071
1072static struct run get_longest_zero_run(int *seq, int sz)
1073{
1074 int i, ls;
1075 int bl = 0, bs = 0;
1076 struct run ret;
1077
1078 ls = 0;
1079 for (i = 0; i < 2 * sz; i++)
1080 if (seq[i % sz]) {
1081 if (i - ls > bl) {
1082 bl = i - ls;
1083 bs = ls;
1084 }
1085 ls = i + 1;
1086 }
1087 if (bl == 0) {
1088 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001089 ret.start = 0;
1090 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001091 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001092 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001093 return ret;
1094 }
1095
Angel Pons7c49cb82020-03-16 23:17:32 +01001096 ret.start = bs % sz;
1097 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001098 ret.middle = (bs + (bl - 1) / 2) % sz;
1099 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001100 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001101
1102 return ret;
1103}
1104
Angel Pons7c49cb82020-03-16 23:17:32 +01001105static void discover_timA_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001106{
1107 int timA;
1108 int statistics[NUM_LANES][128];
1109 int lane;
1110
1111 for (timA = 0; timA < 128; timA++) {
1112 FOR_ALL_LANES {
1113 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1114 }
1115 program_timings(ctrl, channel);
1116
1117 test_timA(ctrl, channel, slotrank);
1118
1119 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001120 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001121 }
1122 }
1123 FOR_ALL_LANES {
1124 struct run rn = get_longest_zero_run(statistics[lane], 128);
1125 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1126 upperA[lane] = rn.end;
1127 if (upperA[lane] < rn.middle)
1128 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001129
Patrick Rudolph368b6152016-11-25 16:36:52 +01001130 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001131 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001132 }
1133}
1134
Angel Pons7c49cb82020-03-16 23:17:32 +01001135static void discover_timA_fine(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001136{
1137 int timA_delta;
1138 int statistics[NUM_LANES][51];
1139 int lane, i;
1140
1141 memset(statistics, 0, sizeof(statistics));
1142
1143 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001144
1145 FOR_ALL_LANES {
1146 ctrl->timings[channel][slotrank].lanes[lane].timA
1147 = upperA[lane] + timA_delta + 0x40;
1148 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001149 program_timings(ctrl, channel);
1150
1151 for (i = 0; i < 100; i++) {
1152 test_timA(ctrl, channel, slotrank);
1153 FOR_ALL_LANES {
1154 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001155 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001156 }
1157 }
1158 }
1159 FOR_ALL_LANES {
1160 int last_zero, first_all;
1161
1162 for (last_zero = -25; last_zero <= 25; last_zero++)
1163 if (statistics[lane][last_zero + 25])
1164 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001165
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001166 last_zero--;
1167 for (first_all = -25; first_all <= 25; first_all++)
1168 if (statistics[lane][first_all + 25] == 100)
1169 break;
1170
Angel Pons7c49cb82020-03-16 23:17:32 +01001171 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001172
1173 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001174 (last_zero + first_all) / 2 + upperA[lane];
1175
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001176 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001177 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001178 }
1179}
1180
Angel Pons891f2bc2020-01-10 01:27:28 +01001181static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001182{
1183 int works[NUM_LANES];
1184 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001185
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001186 while (1) {
1187 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001188
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001189 program_timings(ctrl, channel);
1190 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001191
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001192 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001193 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1194
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001195 if (works[lane])
1196 some_works = 1;
1197 else
1198 all_works = 0;
1199 }
1200 if (all_works)
1201 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001202
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001203 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001204 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001205 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1206 channel, slotrank);
1207 return MAKE_ERR;
1208 }
Angel Pons88521882020-01-05 20:21:20 +01001209 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001210 printram("4024 -= 2;\n");
1211 continue;
1212 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001213 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001214 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001215
Felix Heldef4fe3e2019-12-31 14:15:05 +01001216 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001217 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1218 channel, slotrank);
1219 return MAKE_ERR;
1220 }
1221 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001222 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001223 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001224 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001225 }
1226 }
1227 return 0;
1228}
1229
1230struct timA_minmax {
1231 int timA_min_high, timA_max_high;
1232};
1233
Angel Pons88521882020-01-05 20:21:20 +01001234static void pre_timA_change(ramctr_timing *ctrl, int channel, int slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001235 struct timA_minmax *mnmx)
1236{
1237 int lane;
1238 mnmx->timA_min_high = 7;
1239 mnmx->timA_max_high = 0;
1240
1241 FOR_ALL_LANES {
1242 if (mnmx->timA_min_high >
1243 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1244 mnmx->timA_min_high =
Angel Pons891f2bc2020-01-10 01:27:28 +01001245 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001246 if (mnmx->timA_max_high <
1247 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1248 mnmx->timA_max_high =
Angel Pons891f2bc2020-01-10 01:27:28 +01001249 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001250 }
1251}
1252
Angel Pons88521882020-01-05 20:21:20 +01001253static void post_timA_change(ramctr_timing *ctrl, int channel, int slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001254 struct timA_minmax *mnmx)
1255{
1256 struct timA_minmax post;
1257 int shift_402x = 0;
1258
Angel Pons7c49cb82020-03-16 23:17:32 +01001259 /* Get changed maxima */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001260 pre_timA_change(ctrl, channel, slotrank, &post);
1261
1262 if (mnmx->timA_max_high - mnmx->timA_min_high <
1263 post.timA_max_high - post.timA_min_high)
1264 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001265
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001266 else if (mnmx->timA_max_high - mnmx->timA_min_high >
1267 post.timA_max_high - post.timA_min_high)
1268 shift_402x = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001269
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001270 else
1271 shift_402x = 0;
1272
Felix Heldef4fe3e2019-12-31 14:15:05 +01001273 ctrl->timings[channel][slotrank].io_latency += shift_402x;
Angel Pons88521882020-01-05 20:21:20 +01001274 ctrl->timings[channel][slotrank].roundtrip_latency += shift_402x;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001275 printram("4024 += %d;\n", shift_402x);
1276 printram("4028 += %d;\n", shift_402x);
1277}
1278
Angel Pons7c49cb82020-03-16 23:17:32 +01001279/*
1280 * Compensate the skew between DQS and DQs.
1281 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001282 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1283 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001284 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001285 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001286 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001287 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1288 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1289 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001290 * DQ in phase the data being read is expected to alternate on every byte:
1291 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001292 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001293 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001294 * Once the controller has detected this pattern a bit in the result register is set for the
1295 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001296 */
Angel Pons88521882020-01-05 20:21:20 +01001297int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001298{
1299 int channel, slotrank, lane;
1300 int err;
1301
1302 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1303 int all_high, some_high;
1304 int upperA[NUM_LANES];
1305 struct timA_minmax mnmx;
1306
Angel Pons88521882020-01-05 20:21:20 +01001307 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001308
Angel Ponsffd50152020-11-12 11:03:10 +01001309 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001310
Angel Pons7c49cb82020-03-16 23:17:32 +01001311 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001312 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001313
Angel Pons88521882020-01-05 20:21:20 +01001314 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001315
Felix Heldef4fe3e2019-12-31 14:15:05 +01001316 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001317 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001318 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001319
Felix Held2bb3cdf2018-07-28 00:23:59 +02001320 discover_timA_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001321
Felix Held2bb3cdf2018-07-28 00:23:59 +02001322 all_high = 1;
1323 some_high = 0;
1324 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001325 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001326 some_high = 1;
1327 else
1328 all_high = 0;
1329 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001330
1331 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001332 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001333 printram("4028--;\n");
1334 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001335 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001336 upperA[lane] -= 0x40;
1337
1338 }
1339 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001340 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001341 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001342 printram("4024++;\n");
1343 printram("4028++;\n");
1344 }
1345
1346 program_timings(ctrl, channel);
1347
1348 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1349
1350 err = discover_402x(ctrl, channel, slotrank, upperA);
1351 if (err)
1352 return err;
1353
1354 post_timA_change(ctrl, channel, slotrank, &mnmx);
1355 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1356
1357 discover_timA_fine(ctrl, channel, slotrank, upperA);
1358
1359 post_timA_change(ctrl, channel, slotrank, &mnmx);
1360 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1361
1362 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001363 ctrl->timings[channel][slotrank].lanes[lane].timA -=
1364 mnmx.timA_min_high * 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001365 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001366 ctrl->timings[channel][slotrank].io_latency -= mnmx.timA_min_high;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001367 printram("4028 -= %d;\n", mnmx.timA_min_high);
1368
1369 post_timA_change(ctrl, channel, slotrank, &mnmx);
1370
1371 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001372 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001373 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001374
1375 printram("final results:\n");
1376 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001377 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001378 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379
Angel Pons88521882020-01-05 20:21:20 +01001380 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001381
1382 toggle_io_reset();
1383 }
1384
1385 FOR_ALL_POPULATED_CHANNELS {
1386 program_timings(ctrl, channel);
1387 }
1388 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001389 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390 }
1391 return 0;
1392}
1393
Angel Pons88521882020-01-05 20:21:20 +01001394static void test_timC(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001395{
1396 int lane;
1397
1398 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001399 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1400 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001401 }
1402
Angel Pons88521882020-01-05 20:21:20 +01001403 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001404
Angel Ponsffd50152020-11-12 11:03:10 +01001405 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1406 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001407
Angel Pons7c49cb82020-03-16 23:17:32 +01001408 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001409 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001410
Angel Pons88521882020-01-05 20:21:20 +01001411 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001412
Angel Pons8f0757e2020-11-11 23:03:36 +01001413 const struct iosav_ssq rd_sequence[] = {
1414 /* DRAM command PREA */
1415 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001416 .sp_cmd_ctrl = {
1417 .command = IOSAV_PRE,
1418 .ranksel_ap = 1,
1419 },
1420 .subseq_ctrl = {
1421 .cmd_executions = 1,
1422 .cmd_delay_gap = 3,
1423 .post_ssq_wait = ctrl->tRP,
1424 .data_direction = SSQ_NA,
1425 },
1426 .sp_cmd_addr = {
1427 .address = 1024,
1428 .rowbits = 6,
1429 .bank = 0,
1430 .rank = slotrank,
1431 },
1432 .addr_update = {
1433 .addr_wrap = 18,
1434 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001435 },
1436 /* DRAM command ACT */
1437 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001438 .sp_cmd_ctrl = {
1439 .command = IOSAV_ACT,
1440 .ranksel_ap = 1,
1441 },
1442 .subseq_ctrl = {
1443 .cmd_executions = 8,
1444 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1445 .post_ssq_wait = ctrl->CAS,
1446 .data_direction = SSQ_NA,
1447 },
1448 .sp_cmd_addr = {
1449 .address = 0,
1450 .rowbits = 6,
1451 .bank = 0,
1452 .rank = slotrank,
1453 },
1454 .addr_update = {
1455 .inc_bank = 1,
1456 .addr_wrap = 18,
1457 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001458 },
1459 /* DRAM command RD */
1460 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001461 .sp_cmd_ctrl = {
1462 .command = IOSAV_RD,
1463 .ranksel_ap = 1,
1464 },
1465 .subseq_ctrl = {
1466 .cmd_executions = 500,
1467 .cmd_delay_gap = 4,
1468 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1469 .data_direction = SSQ_RD,
1470 },
1471 .sp_cmd_addr = {
1472 .address = 0,
1473 .rowbits = 0,
1474 .bank = 0,
1475 .rank = slotrank,
1476 },
1477 .addr_update = {
1478 .inc_addr_8 = 1,
1479 .addr_wrap = 18,
1480 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001481 },
1482 /* DRAM command PREA */
1483 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001484 .sp_cmd_ctrl = {
1485 .command = IOSAV_PRE,
1486 .ranksel_ap = 1,
1487 },
1488 .subseq_ctrl = {
1489 .cmd_executions = 1,
1490 .cmd_delay_gap = 3,
1491 .post_ssq_wait = ctrl->tRP,
1492 .data_direction = SSQ_NA,
1493 },
1494 .sp_cmd_addr = {
1495 .address = 1024,
1496 .rowbits = 6,
1497 .bank = 0,
1498 .rank = slotrank,
1499 },
1500 .addr_update = {
1501 .addr_wrap = 18,
1502 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001503 },
1504 };
1505 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001506
Angel Pons7c49cb82020-03-16 23:17:32 +01001507 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001508 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001509
Angel Pons88521882020-01-05 20:21:20 +01001510 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001511}
1512
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001513static void timC_threshold_process(int *data, const int count)
1514{
1515 int min = data[0];
1516 int max = min;
1517 int i;
1518 for (i = 1; i < count; i++) {
1519 if (min > data[i])
1520 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001521
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001522 if (max < data[i])
1523 max = data[i];
1524 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001525 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001526 for (i = 0; i < count; i++)
1527 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001528
Angel Pons891f2bc2020-01-10 01:27:28 +01001529 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001530}
1531
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001532static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
1533{
1534 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01001535 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001536 int lane;
1537
Angel Pons88521882020-01-05 20:21:20 +01001538 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001539
Angel Ponsffd50152020-11-12 11:03:10 +01001540 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001541
Angel Pons7c49cb82020-03-16 23:17:32 +01001542 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001543 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001544
1545 for (timC = 0; timC <= MAX_TIMC; timC++) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001546 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001547 program_timings(ctrl, channel);
1548
1549 test_timC(ctrl, channel, slotrank);
1550
1551 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001552 stats[lane][timC] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001553 }
1554 }
1555 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001556 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1557
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001558 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001559 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1560 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001561 /*
1562 * With command training not being done yet, the lane can be erroneous.
1563 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001564 */
Angel Pons7c49cb82020-03-16 23:17:32 +01001565 timC_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
1566 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1567
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001568 if (rn.all || rn.length < 8) {
1569 printk(BIOS_EMERG, "timC recovery failed\n");
1570 return MAKE_ERR;
1571 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001572 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001573 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001574 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001575 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001576 }
1577 return 0;
1578}
1579
Angel Pons88521882020-01-05 20:21:20 +01001580static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001581{
1582 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001583
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001584 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1585 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001586
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001587 return ret;
1588}
1589
Angel Pons765d4652020-11-11 14:44:35 +01001590/* Each cacheline is 64 bits long */
1591static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1592{
1593 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1594}
1595
Angel Pons88521882020-01-05 20:21:20 +01001596static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001597{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301598 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001599 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001600
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001601 for (j = 0; j < 16; j++)
1602 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001603
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001604 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001605
1606 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001607}
1608
Angel Pons88521882020-01-05 20:21:20 +01001609static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001610{
1611 int ret = 0;
1612 int channel;
1613 FOR_ALL_POPULATED_CHANNELS ret++;
1614 return ret;
1615}
1616
Angel Pons88521882020-01-05 20:21:20 +01001617static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001618{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301619 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001620 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301621 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001622
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001623 for (j = 0; j < 16; j++)
1624 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001625
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001626 for (j = 0; j < 16; j++)
1627 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001628
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001629 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001630
1631 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001632}
1633
Angel Pons88521882020-01-05 20:21:20 +01001634static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001635{
1636 int channel, slotrank, lane;
1637
1638 FOR_ALL_POPULATED_CHANNELS {
1639 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001640 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1641 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001642 }
1643
1644 program_timings(ctrl, channel);
1645
1646 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001647 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001648
Angel Ponsffd50152020-11-12 11:03:10 +01001649 iosav_write_read_mpr_sequence(
1650 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001651
Angel Pons7c49cb82020-03-16 23:17:32 +01001652 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001653 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001654
Angel Pons88521882020-01-05 20:21:20 +01001655 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001656 }
1657
1658 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001659 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1660 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001661 }
1662
1663 program_timings(ctrl, channel);
1664
1665 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001666 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001667
Angel Ponsffd50152020-11-12 11:03:10 +01001668 iosav_write_read_mpr_sequence(
1669 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001670
Angel Pons7c49cb82020-03-16 23:17:32 +01001671 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001672 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001673
Angel Pons88521882020-01-05 20:21:20 +01001674 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001675 }
1676 }
1677}
1678
Angel Pons88521882020-01-05 20:21:20 +01001679static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001680{
1681 /* enable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001682 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel) | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001683
Angel Pons88521882020-01-05 20:21:20 +01001684 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001685
1686 const struct iosav_ssq sequence[] = {
1687 /* DRAM command NOP */
1688 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001689 .sp_cmd_ctrl = {
1690 .command = IOSAV_NOP,
1691 .ranksel_ap = 1,
1692 },
1693 .subseq_ctrl = {
1694 .cmd_executions = 1,
1695 .cmd_delay_gap = 3,
1696 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1697 .data_direction = SSQ_WR,
1698 },
1699 .sp_cmd_addr = {
1700 .address = 8,
1701 .rowbits = 0,
1702 .bank = 0,
1703 .rank = slotrank,
1704 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001705 },
1706 /* DRAM command NOP */
1707 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001708 .sp_cmd_ctrl = {
1709 .command = IOSAV_NOP_ALT,
1710 .ranksel_ap = 1,
1711 },
1712 .subseq_ctrl = {
1713 .cmd_executions = 1,
1714 .cmd_delay_gap = 3,
1715 .post_ssq_wait = ctrl->CAS + 38,
1716 .data_direction = SSQ_RD,
1717 },
1718 .sp_cmd_addr = {
1719 .address = 4,
1720 .rowbits = 0,
1721 .bank = 0,
1722 .rank = slotrank,
1723 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001724 },
1725 };
1726 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001727
Angel Pons7c49cb82020-03-16 23:17:32 +01001728 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001729 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001730
Angel Pons88521882020-01-05 20:21:20 +01001731 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001732
1733 /* disable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001734 write_mrreg(ctrl, channel, slotrank, 1,
1735 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001736}
1737
1738static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1739{
1740 int timB;
1741 int statistics[NUM_LANES][128];
1742 int lane;
1743
Angel Pons88521882020-01-05 20:21:20 +01001744 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001745
1746 for (timB = 0; timB < 128; timB++) {
1747 FOR_ALL_LANES {
1748 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1749 }
1750 program_timings(ctrl, channel);
1751
1752 test_timB(ctrl, channel, slotrank);
1753
1754 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001755 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1756 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1757 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001758 }
1759 }
1760 FOR_ALL_LANES {
1761 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001762 /*
1763 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1764 * of timB by a small value, which might cause the 6-bit value to overflow if
1765 * it's close to 0x3f. Increment the value by a small offset if it's likely
1766 * to overflow, to make sure it won't overflow while running tests and bricks
1767 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001768 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001769 * TODO: find out why some tests (edge write discovery) increment timB.
1770 */
1771 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001772 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001773 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001774 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001775
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001776 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1777 if (rn.all) {
1778 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1779 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001780
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001781 return MAKE_ERR;
1782 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001783 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1784 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001785 }
1786 return 0;
1787}
1788
1789static int get_timB_high_adjust(u64 val)
1790{
1791 int i;
1792
Angel Ponsbf13ef02020-11-11 18:40:06 +01001793 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001794 if (val == 0xffffffffffffffffLL)
1795 return 0;
1796
1797 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001798 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001799 for (i = 0; i < 8; i++)
1800 if (val << (8 * (7 - i) + 4))
1801 return -i;
1802 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001803 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001804 for (i = 0; i < 8; i++)
1805 if (val >> (8 * (7 - i) + 4))
1806 return i;
1807 }
1808 return 8;
1809}
1810
Angel Ponsbf13ef02020-11-11 18:40:06 +01001811static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001812{
1813 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001814 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001815 FOR_ALL_POPULATED_CHANNELS {
1816 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001817 }
1818 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1819
Angel Pons765d4652020-11-11 14:44:35 +01001820 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001821 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001822
Angel Pons88521882020-01-05 20:21:20 +01001823 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001824
Angel Ponsffd50152020-11-12 11:03:10 +01001825 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001826
Angel Pons7c49cb82020-03-16 23:17:32 +01001827 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001828 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001829
Angel Pons88521882020-01-05 20:21:20 +01001830 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001831
Angel Pons8f0757e2020-11-11 23:03:36 +01001832 const struct iosav_ssq rd_sequence[] = {
1833 /* DRAM command PREA */
1834 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001835 .sp_cmd_ctrl = {
1836 .command = IOSAV_PRE,
1837 .ranksel_ap = 1,
1838 },
1839 .subseq_ctrl = {
1840 .cmd_executions = 1,
1841 .cmd_delay_gap = 3,
1842 .post_ssq_wait = ctrl->tRP,
1843 .data_direction = SSQ_NA,
1844 },
1845 .sp_cmd_addr = {
1846 .address = 1024,
1847 .rowbits = 6,
1848 .bank = 0,
1849 .rank = slotrank,
1850 },
1851 .addr_update = {
1852 .addr_wrap = 18,
1853 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001854 },
1855 /* DRAM command ACT */
1856 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001857 .sp_cmd_ctrl = {
1858 .command = IOSAV_ACT,
1859 .ranksel_ap = 1,
1860 },
1861 .subseq_ctrl = {
1862 .cmd_executions = 1,
1863 .cmd_delay_gap = 3,
1864 .post_ssq_wait = ctrl->tRCD,
1865 .data_direction = SSQ_NA,
1866 },
1867 .sp_cmd_addr = {
1868 .address = 0,
1869 .rowbits = 6,
1870 .bank = 0,
1871 .rank = slotrank,
1872 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001873 },
1874 /* DRAM command RD */
1875 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001876 .sp_cmd_ctrl = {
1877 .command = IOSAV_RD,
1878 .ranksel_ap = 3,
1879 },
1880 .subseq_ctrl = {
1881 .cmd_executions = 1,
1882 .cmd_delay_gap = 3,
1883 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001884 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001885 ctrl->timings[channel][slotrank].io_latency,
1886 .data_direction = SSQ_RD,
1887 },
1888 .sp_cmd_addr = {
1889 .address = 8,
1890 .rowbits = 6,
1891 .bank = 0,
1892 .rank = slotrank,
1893 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001894 },
1895 };
1896 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001897
Angel Pons7c49cb82020-03-16 23:17:32 +01001898 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001899 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001900
Angel Pons88521882020-01-05 20:21:20 +01001901 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001902 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001903 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001904 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001905 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001906 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1907 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1908 get_timB_high_adjust(res) * 64;
1909
1910 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001911 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1912 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001913 }
1914 }
Angel Pons88521882020-01-05 20:21:20 +01001915 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001916}
1917
Angel Pons88521882020-01-05 20:21:20 +01001918static void write_op(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001919{
1920 int slotrank;
1921
Angel Pons88521882020-01-05 20:21:20 +01001922 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001923
1924 /* choose an existing rank. */
1925 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
1926
Angel Ponsffd50152020-11-12 11:03:10 +01001927 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001928
Angel Pons7c49cb82020-03-16 23:17:32 +01001929 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001930 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001931
Angel Pons88521882020-01-05 20:21:20 +01001932 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001933}
1934
Angel Pons7c49cb82020-03-16 23:17:32 +01001935/*
1936 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001937 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001938 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1939 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1940 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1941 * CLK/ADDR/CMD signals have the same routing delay.
1942 *
1943 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1944 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1945 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001946 */
Angel Pons88521882020-01-05 20:21:20 +01001947int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001948{
1949 int channel, slotrank, lane;
1950 int err;
1951
1952 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01001953 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001954
1955 FOR_ALL_POPULATED_CHANNELS {
1956 write_op(ctrl, channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01001957 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001958 }
1959
Angel Pons7c49cb82020-03-16 23:17:32 +01001960 /* Refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001961 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001962 FOR_ALL_POPULATED_CHANNELS {
1963 write_op(ctrl, channel);
1964 }
1965
Angel Pons7c49cb82020-03-16 23:17:32 +01001966 /* Enable write leveling on all ranks
1967 Disable all DQ outputs
1968 Only NOP is allowed in this mode */
1969 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1970 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001971 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972
Angel Pons88521882020-01-05 20:21:20 +01001973 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001974
1975 toggle_io_reset();
1976
Angel Pons7c49cb82020-03-16 23:17:32 +01001977 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001978 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1979 err = discover_timB(ctrl, channel, slotrank);
1980 if (err)
1981 return err;
1982 }
1983
Angel Pons7c49cb82020-03-16 23:17:32 +01001984 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001985 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001986 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001987
Angel Pons88521882020-01-05 20:21:20 +01001988 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001989
1990 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001991 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001992
Angel Pons7c49cb82020-03-16 23:17:32 +01001993 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001994 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001995
1996 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001997 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001998 MCHBAR32(IOSAV_STATUS_ch(channel));
1999 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002000
Angel Ponsffd50152020-11-12 11:03:10 +01002001 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002002
Angel Pons7c49cb82020-03-16 23:17:32 +01002003 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002004 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002005
Angel Pons88521882020-01-05 20:21:20 +01002006 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002007 }
2008
2009 toggle_io_reset();
2010
2011 printram("CPE\n");
2012 precharge(ctrl);
2013 printram("CPF\n");
2014
2015 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002016 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002017 }
2018
2019 FOR_ALL_POPULATED_CHANNELS {
2020 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002021 }
2022
2023 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2024 err = discover_timC(ctrl, channel, slotrank);
2025 if (err)
2026 return err;
2027 }
2028
2029 FOR_ALL_POPULATED_CHANNELS
2030 program_timings(ctrl, channel);
2031
2032 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002033 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002034
2035 FOR_ALL_POPULATED_CHANNELS
2036 program_timings(ctrl, channel);
2037
2038 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002039 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002040 }
2041 return 0;
2042}
2043
Angel Ponsbf13ef02020-11-11 18:40:06 +01002044static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002045{
2046 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2047 int timC_delta;
2048 int lanes_ok = 0;
2049 int ctr = 0;
2050 int lane;
2051
2052 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2053 FOR_ALL_LANES {
2054 ctrl->timings[channel][slotrank].lanes[lane].timC =
2055 saved_rt.lanes[lane].timC + timC_delta;
2056 }
2057 program_timings(ctrl, channel);
2058 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002059 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002060 }
2061
Angel Pons765d4652020-11-11 14:44:35 +01002062 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002063 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002064
Angel Pons88521882020-01-05 20:21:20 +01002065 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002066
Angel Ponsffd50152020-11-12 11:03:10 +01002067 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002068
2069 /* Program LFSR for the RD/WR subsequences */
2070 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2071 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002072
Angel Pons7c49cb82020-03-16 23:17:32 +01002073 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002074 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002075
Angel Pons88521882020-01-05 20:21:20 +01002076 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002077 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002078 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002079
2080 if (r32 == 0)
2081 lanes_ok |= 1 << lane;
2082 }
2083 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002084 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002085 break;
2086 }
2087
2088 ctrl->timings[channel][slotrank] = saved_rt;
2089
Patrick Rudolphdd662872017-10-28 18:20:11 +02002090 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002091}
2092
Angel Pons88521882020-01-05 20:21:20 +01002093static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002094{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302095 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002096 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2097 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002098
2099 if (patno) {
2100 u8 base8 = 0x80 >> ((patno - 1) % 8);
2101 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2102 for (i = 0; i < 32; i++) {
2103 for (j = 0; j < 16; j++) {
2104 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002105
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002106 if (invert[patno - 1][i] & (1 << (j / 2)))
2107 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002108
2109 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002110 }
2111 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002112 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002113 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2114 for (j = 0; j < 16; j++) {
2115 const u32 val = pattern[i][j];
2116 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2117 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002118 }
2119 sfence();
2120 }
Angel Pons765d4652020-11-11 14:44:35 +01002121
2122 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002123}
2124
Angel Pons88521882020-01-05 20:21:20 +01002125static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002126{
2127 int channel, slotrank;
2128
2129 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002130 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002131
Angel Pons7c49cb82020-03-16 23:17:32 +01002132 /* Choose an existing rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002133 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2134
Angel Ponsffd50152020-11-12 11:03:10 +01002135 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002136
Angel Pons7c49cb82020-03-16 23:17:32 +01002137 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002138 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002139
Angel Pons88521882020-01-05 20:21:20 +01002140 wait_for_iosav(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002141 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002142 }
2143
2144 /* refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002145 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002146 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002147 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002148
2149 /* choose an existing rank. */
2150 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2151
Angel Ponsffd50152020-11-12 11:03:10 +01002152 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002153
Angel Pons7c49cb82020-03-16 23:17:32 +01002154 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002155 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002156
Angel Pons88521882020-01-05 20:21:20 +01002157 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002158 }
2159
Angel Pons7c49cb82020-03-16 23:17:32 +01002160 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002161 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002162
2163 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002164 dram_mrscommands(ctrl);
2165
2166 toggle_io_reset();
2167}
2168
Angel Ponsbf13ef02020-11-11 18:40:06 +01002169#define CT_MIN_PI -127
2170#define CT_MAX_PI 128
2171#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2172
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002173#define MIN_C320C_LEN 13
2174
2175static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2176{
2177 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2178 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002179 int command_pi;
2180 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002181 int delta = 0;
2182
2183 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2184
2185 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002186 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002187 }
2188
2189 ctrl->cmd_stretch[channel] = cmd_stretch;
2190
Angel Pons88521882020-01-05 20:21:20 +01002191 MCHBAR32(TC_RAP_ch(channel)) =
Angel Pons7c49cb82020-03-16 23:17:32 +01002192 (ctrl->tRRD << 0)
2193 | (ctrl->tRTP << 4)
2194 | (ctrl->tCKE << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002195 | (ctrl->tWTR << 12)
2196 | (ctrl->tFAW << 16)
Angel Pons7c49cb82020-03-16 23:17:32 +01002197 | (ctrl->tWR << 24)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002198 | (ctrl->cmd_stretch[channel] << 30);
2199
2200 if (ctrl->cmd_stretch[channel] == 2)
2201 delta = 2;
2202 else if (ctrl->cmd_stretch[channel] == 0)
2203 delta = 4;
2204
2205 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002206 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002207 }
2208
Angel Ponsbf13ef02020-11-11 18:40:06 +01002209 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002210 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002211 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002212 }
2213 program_timings(ctrl, channel);
2214 reprogram_320c(ctrl);
2215 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002216 stat[slotrank][command_pi - CT_MIN_PI] =
2217 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002218 }
2219 }
2220 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002221 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002222
Angel Ponsbf13ef02020-11-11 18:40:06 +01002223 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002224 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2225 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002226
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002227 if (rn.all || rn.length < MIN_C320C_LEN) {
2228 FOR_ALL_POPULATED_RANKS {
2229 ctrl->timings[channel][slotrank] =
2230 saved_timings[channel][slotrank];
2231 }
2232 return MAKE_ERR;
2233 }
2234 }
2235
2236 return 0;
2237}
2238
Angel Pons7c49cb82020-03-16 23:17:32 +01002239/*
2240 * Adjust CMD phase shift and try multiple command rates.
2241 * A command rate of 2T doubles the time needed for address and command decode.
2242 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002243int command_training(ramctr_timing *ctrl)
2244{
2245 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002246
2247 FOR_ALL_POPULATED_CHANNELS {
2248 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002249 }
2250
2251 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002252 int cmdrate, err;
2253
2254 /*
2255 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002256 * Issue:
2257 * While c320c discovery seems to succeed raminit will fail in write training.
2258 *
2259 * Workaround:
2260 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2261 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002262 *
2263 * Single DIMM per channel:
2264 * Try command rate 1T and 2T
2265 */
2266 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002267 if (ctrl->tCMD)
2268 /* XMP gives the CMD rate in clock ticks, not ns */
2269 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002270
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002271 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002272 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2273
2274 if (!err)
2275 break;
2276 }
2277
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002278 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002279 printk(BIOS_EMERG, "c320c discovery failed\n");
2280 return err;
2281 }
2282
Angel Pons891f2bc2020-01-10 01:27:28 +01002283 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002284 }
2285
2286 FOR_ALL_POPULATED_CHANNELS
2287 program_timings(ctrl, channel);
2288
2289 reprogram_320c(ctrl);
2290 return 0;
2291}
2292
Angel Pons891f2bc2020-01-10 01:27:28 +01002293static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002294{
2295 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002296 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002297 int lane;
2298
2299 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2300 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002301 ctrl->timings[channel][slotrank].lanes[lane].rising = edge;
Angel Pons891f2bc2020-01-10 01:27:28 +01002302 ctrl->timings[channel][slotrank].lanes[lane].falling = edge;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002303 }
2304 program_timings(ctrl, channel);
2305
2306 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002307 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2308 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002309 }
2310
Angel Pons88521882020-01-05 20:21:20 +01002311 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002312
Angel Ponsffd50152020-11-12 11:03:10 +01002313 iosav_write_read_mpr_sequence(
2314 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002315
Angel Pons7c49cb82020-03-16 23:17:32 +01002316 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002317 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002318
Angel Pons88521882020-01-05 20:21:20 +01002319 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002320
2321 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002322 stats[lane][edge] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002323 }
2324 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002325
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002326 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002327 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002328 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002329
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002330 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002331 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2332 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002333 return MAKE_ERR;
2334 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002335 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002336 }
2337 return 0;
2338}
2339
2340int discover_edges(ramctr_timing *ctrl)
2341{
2342 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2343 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2344 int channel, slotrank, lane;
2345 int err;
2346
Angel Pons88521882020-01-05 20:21:20 +01002347 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002348
2349 toggle_io_reset();
2350
2351 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002352 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002353 }
2354
2355 FOR_ALL_POPULATED_CHANNELS {
2356 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002357 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002358 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002359 }
2360
2361 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01002362 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2363 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002364 }
2365
2366 program_timings(ctrl, channel);
2367
2368 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002369 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002370
Angel Ponsffd50152020-11-12 11:03:10 +01002371 iosav_write_read_mpr_sequence(
2372 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02002373
Angel Pons7c49cb82020-03-16 23:17:32 +01002374 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002375 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002376
Angel Pons88521882020-01-05 20:21:20 +01002377 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002378 }
2379
2380 /* XXX: check any measured value ? */
2381
2382 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01002383 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
Angel Pons7c49cb82020-03-16 23:17:32 +01002384 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002385 }
2386
2387 program_timings(ctrl, channel);
2388
2389 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002390 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002391
Angel Ponsffd50152020-11-12 11:03:10 +01002392 iosav_write_read_mpr_sequence(
2393 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002394
Angel Pons7c49cb82020-03-16 23:17:32 +01002395 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002396 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002397
Angel Pons88521882020-01-05 20:21:20 +01002398 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002399 }
2400
2401 /* XXX: check any measured value ? */
2402
2403 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002404 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
Angel Pons891f2bc2020-01-10 01:27:28 +01002405 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002406 }
2407
2408 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002409 }
2410
Angel Pons0c3936e2020-03-22 12:49:27 +01002411 /*
2412 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2413 * also use a single loop. It would seem that it is a debugging configuration.
2414 */
Angel Pons88521882020-01-05 20:21:20 +01002415 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2416 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002417
2418 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2419 err = discover_edges_real(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002420 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002421 if (err)
2422 return err;
2423 }
2424
Angel Pons88521882020-01-05 20:21:20 +01002425 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2426 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002427
2428 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2429 err = discover_edges_real(ctrl, channel, slotrank,
2430 rising_edges[channel][slotrank]);
2431 if (err)
2432 return err;
2433 }
2434
Angel Pons88521882020-01-05 20:21:20 +01002435 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002436
2437 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2438 ctrl->timings[channel][slotrank].lanes[lane].falling =
2439 falling_edges[channel][slotrank][lane];
2440 ctrl->timings[channel][slotrank].lanes[lane].rising =
2441 rising_edges[channel][slotrank][lane];
2442 }
2443
2444 FOR_ALL_POPULATED_CHANNELS {
2445 program_timings(ctrl, channel);
2446 }
2447
2448 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002449 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002450 }
2451 return 0;
2452}
2453
Angel Pons7c49cb82020-03-16 23:17:32 +01002454static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002455{
2456 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002457 u32 raw_stats[MAX_EDGE_TIMING + 1];
2458 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002459 const int reg3000b24[] = { 0, 0xc, 0x2c };
2460 int lane, i;
2461 int lower[NUM_LANES];
2462 int upper[NUM_LANES];
2463 int pat;
2464
2465 FOR_ALL_LANES {
2466 lower[lane] = 0;
2467 upper[lane] = MAX_EDGE_TIMING;
2468 }
2469
2470 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002471 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002472 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2473
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002474 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2475 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002476 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002477
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002478 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2479 FOR_ALL_LANES {
2480 ctrl->timings[channel][slotrank].lanes[lane].
2481 rising = edge;
2482 ctrl->timings[channel][slotrank].lanes[lane].
2483 falling = edge;
2484 }
2485 program_timings(ctrl, channel);
2486
2487 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002488 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2489 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002490 }
Angel Pons88521882020-01-05 20:21:20 +01002491 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002492
Angel Ponsffd50152020-11-12 11:03:10 +01002493 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002494
Angel Pons7c49cb82020-03-16 23:17:32 +01002495 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002496 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002497
Angel Pons88521882020-01-05 20:21:20 +01002498 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002499 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002500 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002501 }
2502
Angel Pons7c49cb82020-03-16 23:17:32 +01002503 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002504 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002505 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002506
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002507 FOR_ALL_LANES {
2508 struct run rn;
2509 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002510 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2511
2512 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2513
2514 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2515 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2516 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002517 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002518
2519 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2520 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2521
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002522 edges[lane] = (lower[lane] + upper[lane]) / 2;
2523 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002524 printk(BIOS_EMERG, "edge write discovery failed: "
2525 "%d, %d, %d\n", channel, slotrank, lane);
2526
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002527 return MAKE_ERR;
2528 }
2529 }
2530 }
2531 }
2532
Angel Pons88521882020-01-05 20:21:20 +01002533 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002534 printram("CPA\n");
2535 return 0;
2536}
2537
2538int discover_edges_write(ramctr_timing *ctrl)
2539{
2540 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002541 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2542 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002543
Angel Pons7c49cb82020-03-16 23:17:32 +01002544 /*
2545 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2546 * also use a single loop. It would seem that it is a debugging configuration.
2547 */
Angel Pons88521882020-01-05 20:21:20 +01002548 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2549 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002550
2551 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2552 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002553 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002554 if (err)
2555 return err;
2556 }
2557
Angel Pons88521882020-01-05 20:21:20 +01002558 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2559 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002560
2561 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2562 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002563 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002564 if (err)
2565 return err;
2566 }
2567
Angel Pons88521882020-01-05 20:21:20 +01002568 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002569
2570 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2571 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002572 falling_edges[channel][slotrank][lane];
2573
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002574 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002575 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002576 }
2577
2578 FOR_ALL_POPULATED_CHANNELS
2579 program_timings(ctrl, channel);
2580
2581 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002582 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002583 }
2584 return 0;
2585}
2586
2587static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2588{
Angel Pons88521882020-01-05 20:21:20 +01002589 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002590
Angel Ponsffd50152020-11-12 11:03:10 +01002591 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002592
Angel Pons7c49cb82020-03-16 23:17:32 +01002593 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002594 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002595
Angel Pons88521882020-01-05 20:21:20 +01002596 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002597}
2598
2599int discover_timC_write(ramctr_timing *ctrl)
2600{
Angel Pons7c49cb82020-03-16 23:17:32 +01002601 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002602 int i, pat;
2603
2604 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2605 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2606 int channel, slotrank, lane;
2607
2608 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2609 lower[channel][slotrank][lane] = 0;
2610 upper[channel][slotrank][lane] = MAX_TIMC;
2611 }
2612
Angel Pons88521882020-01-05 20:21:20 +01002613 /*
2614 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2615 * FIXME: This must only be done on Ivy Bridge.
2616 */
2617 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002618 printram("discover timC write:\n");
2619
2620 for (i = 0; i < 3; i++)
2621 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002622
2623 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2624 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2625 ~0x3f000000, rege3c_b24[i] << 24);
2626
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002627 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002628
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002629 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2630 FOR_ALL_POPULATED_RANKS {
2631 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002632 u32 raw_stats[MAX_TIMC + 1];
2633 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002634
2635 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002636 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002637
2638 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002639
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002640 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002641 FOR_ALL_LANES {
2642 ctrl->timings[channel][slotrank]
2643 .lanes[lane].timC = timC;
2644 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002645 program_timings(ctrl, channel);
2646
2647 test_timC_write (ctrl, channel, slotrank);
2648
Angel Pons7c49cb82020-03-16 23:17:32 +01002649 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002650 raw_stats[timC] = MCHBAR32(
2651 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002652 }
2653 FOR_ALL_LANES {
2654 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002655 for (timC = 0; timC < MAX_TIMC; timC++) {
2656 stats[timC] = !!(raw_stats[timC]
2657 & (1 << lane));
2658 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002659
Angel Pons7c49cb82020-03-16 23:17:32 +01002660 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002661 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002662 printk(BIOS_EMERG,
2663 "timC write discovery failed: "
2664 "%d, %d, %d\n", channel,
2665 slotrank, lane);
2666
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002667 return MAKE_ERR;
2668 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002669 printram("timC: %d, %d, %d: "
2670 "0x%02x-0x%02x-0x%02x, "
2671 "0x%02x-0x%02x\n", channel, slotrank,
2672 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002673 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002674 rn.end - ctrl->timC_offset[i]);
2675
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002676 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002677 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002678 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002679
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002680 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002681 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002682 upper[channel][slotrank][lane]);
2683
2684 }
2685 }
2686 }
2687 }
2688
2689 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002690 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002691 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002692 udelay(2);
2693 }
2694
Angel Pons88521882020-01-05 20:21:20 +01002695 /*
2696 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2697 * FIXME: This must only be done on Ivy Bridge.
2698 */
2699 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002700
2701 printram("CPB\n");
2702
2703 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002704 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002705 (lower[channel][slotrank][lane] +
2706 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002707
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002708 ctrl->timings[channel][slotrank].lanes[lane].timC =
2709 (lower[channel][slotrank][lane] +
2710 upper[channel][slotrank][lane]) / 2;
2711 }
2712 FOR_ALL_POPULATED_CHANNELS {
2713 program_timings(ctrl, channel);
2714 }
2715 return 0;
2716}
2717
Angel Pons88521882020-01-05 20:21:20 +01002718void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002719{
2720 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002721 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002722
2723 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2724 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002725 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002726 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002727 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002728 printram("normalize %d, %d, %d: mat %d\n",
2729 channel, slotrank, lane, mat);
2730
Felix Heldef4fe3e2019-12-31 14:15:05 +01002731 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002732 printram("normalize %d, %d, %d: delta %d\n",
2733 channel, slotrank, lane, delta);
2734
Angel Pons88521882020-01-05 20:21:20 +01002735 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002736 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002737 }
2738
2739 FOR_ALL_POPULATED_CHANNELS {
2740 program_timings(ctrl, channel);
2741 }
2742}
2743
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002744int channel_test(ramctr_timing *ctrl)
2745{
2746 int channel, slotrank, lane;
2747
2748 slotrank = 0;
2749 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002750 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002751 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002752 return MAKE_ERR;
2753 }
2754 FOR_ALL_POPULATED_CHANNELS {
2755 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002756 }
2757
2758 for (slotrank = 0; slotrank < 4; slotrank++)
2759 FOR_ALL_CHANNELS
2760 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2761 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002762 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2763 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002764 }
Angel Pons88521882020-01-05 20:21:20 +01002765 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002766
Angel Ponsffd50152020-11-12 11:03:10 +01002767 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002768
Angel Pons7c49cb82020-03-16 23:17:32 +01002769 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002770 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002771
Angel Pons88521882020-01-05 20:21:20 +01002772 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002773 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002774 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002775 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2776 channel, slotrank, lane);
2777 return MAKE_ERR;
2778 }
2779 }
2780 return 0;
2781}
2782
Patrick Rudolphdd662872017-10-28 18:20:11 +02002783void channel_scrub(ramctr_timing *ctrl)
2784{
2785 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002786 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002787
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002788 FOR_ALL_POPULATED_CHANNELS {
2789 wait_for_iosav(channel);
2790 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002791 }
2792
2793 /*
2794 * During runtime the "scrubber" will periodically scan through the memory in the
2795 * physical address space, to identify and fix CRC errors.
2796 * The following loops writes to every DRAM address, setting the ECC bits to the
2797 * correct value. A read from this location will no longer return a CRC error,
2798 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002799 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002800 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2801 * and firmware running in x86_32.
2802 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002803 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2804 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002805 for (bank = 0; bank < 8; bank++) {
2806 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002807
Angel Pons8f0757e2020-11-11 23:03:36 +01002808 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2809 const struct iosav_ssq sequence[] = {
2810 /*
2811 * DRAM command ACT
2812 * Opens the row for writing.
2813 */
2814 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002815 .sp_cmd_ctrl = {
2816 .command = IOSAV_ACT,
2817 .ranksel_ap = 1,
2818 },
2819 .subseq_ctrl = {
2820 .cmd_executions = 1,
2821 .cmd_delay_gap = gap,
2822 .post_ssq_wait = ctrl->tRCD,
2823 .data_direction = SSQ_NA,
2824 },
2825 .sp_cmd_addr = {
2826 .address = row,
2827 .rowbits = 6,
2828 .bank = bank,
2829 .rank = slotrank,
2830 },
2831 .addr_update = {
2832 .inc_addr_1 = 1,
2833 .addr_wrap = 18,
2834 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002835 },
2836 /*
2837 * DRAM command WR
2838 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2839 * bytes.
2840 */
2841 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002842 .sp_cmd_ctrl = {
2843 .command = IOSAV_WR,
2844 .ranksel_ap = 1,
2845 },
2846 .subseq_ctrl = {
2847 .cmd_executions = 129,
2848 .cmd_delay_gap = 4,
2849 .post_ssq_wait = ctrl->tWTR +
2850 ctrl->CWL + 8,
2851 .data_direction = SSQ_WR,
2852 },
2853 .sp_cmd_addr = {
2854 .address = row,
2855 .rowbits = 0,
2856 .bank = bank,
2857 .rank = slotrank,
2858 },
2859 .addr_update = {
2860 .inc_addr_8 = 1,
2861 .addr_wrap = 9,
2862 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002863 },
2864 /*
2865 * DRAM command PRE
2866 * Closes the row.
2867 */
2868 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002869 .sp_cmd_ctrl = {
2870 .command = IOSAV_PRE,
2871 .ranksel_ap = 1,
2872 },
2873 .subseq_ctrl = {
2874 .cmd_executions = 1,
2875 .cmd_delay_gap = 4,
2876 .post_ssq_wait = ctrl->tRP,
2877 .data_direction = SSQ_NA,
2878 },
2879 .sp_cmd_addr = {
2880 .address = 0,
2881 .rowbits = 6,
2882 .bank = bank,
2883 .rank = slotrank,
2884 },
2885 .addr_update = {
2886 .addr_wrap = 18,
2887 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002888 },
2889 };
2890 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002891
2892 /* Execute command queue */
2893 iosav_run_queue(channel, 16, 0);
2894
2895 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002896 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002897 }
2898 }
2899}
2900
Angel Pons88521882020-01-05 20:21:20 +01002901void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002902{
2903 int channel;
2904
Angel Pons7c49cb82020-03-16 23:17:32 +01002905 /* 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 +01002906 static u32 seeds[NUM_CHANNELS][3] = {
2907 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2908 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2909 };
2910 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002911 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002912 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2913 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2914 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002915 }
2916}
2917
Angel Pons89ae6b82020-03-21 13:23:32 +01002918void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002919{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002921 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002922 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002923 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002924 }
2925}
2926
Angel Pons88521882020-01-05 20:21:20 +01002927void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002928{
2929 int channel;
2930
2931 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002932 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002933 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002934 }
2935
2936 udelay(1);
2937
2938 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002939 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002940 }
2941}
2942
Angel Pons7c49cb82020-03-16 23:17:32 +01002943void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002944{
2945 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002946
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002947 FOR_ALL_POPULATED_CHANNELS {
2948 u32 b20, b4_8_12;
Angel Pons88521882020-01-05 20:21:20 +01002949 int min_pi = 10000;
2950 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002951
2952 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002953 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2954 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002955 }
2956
Angel Pons7c49cb82020-03-16 23:17:32 +01002957 b20 = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002958
Angel Pons7c49cb82020-03-16 23:17:32 +01002959 b4_8_12 = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 0x3330 : 0x2220;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002960
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002961 dram_odt_stretch(ctrl, channel);
2962
Angel Ponsdc5539f2020-11-12 12:44:25 +01002963 MCHBAR32(TC_RWP_ch(channel)) = (1 << 27) | (2 << 24) | (b20 << 20) |
Felix Held2463aa92018-07-29 21:37:55 +02002964 ((ctrl->ref_card_offset[channel] + 2) << 16) | b4_8_12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002965 }
2966}
2967
Angel Pons88521882020-01-05 20:21:20 +01002968void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002969{
2970 int channel;
2971 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002972 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2973 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002974 }
2975}
2976
Angel Pons7c49cb82020-03-16 23:17:32 +01002977/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2978static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002979{
Angel Pons88521882020-01-05 20:21:20 +01002980 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002981}
2982
Angel Pons7c49cb82020-03-16 23:17:32 +01002983/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002984void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002985{
Angel Ponsb50ca572020-11-11 19:07:20 +01002986 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01002987
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002988 int channel;
2989 int t1_cycles = 0, t1_ns = 0, t2_ns;
2990 int t3_ns;
2991 u32 r32;
2992
Angel Pons7c49cb82020-03-16 23:17:32 +01002993 /* FIXME: This register only exists on Ivy Bridge */
2994 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002995
Felix Heldf9b826a2018-07-30 17:56:52 +02002996 FOR_ALL_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002997 MCHBAR32_AND_OR(TC_OTHP_ch(channel), ~(3 << 12), 1 << 12);
Patrick Rudolph652c4912017-10-31 11:36:55 +01002998
Patrick Rudolph74203de2017-11-20 11:57:01 +01002999 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003000 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003001 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003002 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003003 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003004 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003005
Felix Heldf9b826a2018-07-30 17:56:52 +02003006 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003007 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003008
Angel Pons88521882020-01-05 20:21:20 +01003009 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3010 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003011
3012 FOR_ALL_CHANNELS {
3013 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003014 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003015 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003016 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003017 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003018 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003019 case 1:
3020 case 4:
3021 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003022 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003023 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003024 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003025 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003026 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003027 break;
3028 }
3029 }
3030
Felix Held50b7ed22019-12-30 20:41:54 +01003031 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003032 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003033 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003034
3035 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003036 MCHBAR32_AND_OR(TC_RFP_ch(channel), ~(3 << 16), 1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003037
Angel Ponsdc5539f2020-11-12 12:44:25 +01003038 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3039 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003040 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003041
Angel Pons7c49cb82020-03-16 23:17:32 +01003042 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003043 FOR_ALL_POPULATED_CHANNELS
3044 break;
3045
Angel Pons88521882020-01-05 20:21:20 +01003046 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3047 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003048 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003049 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003050 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003051 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003052 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003053 t1_ns += 500;
3054
Angel Pons88521882020-01-05 20:21:20 +01003055 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003056 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003057 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003058 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003059 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003060 t3_ns = 500;
3061 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003062
3063 /* The graphics driver will use these watermark values */
3064 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003065 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003066 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3067 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003068}
3069
Angel Pons88521882020-01-05 20:21:20 +01003070void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003071{
3072 int channel, slotrank, lane;
3073
Angel Pons7c49cb82020-03-16 23:17:32 +01003074 FOR_ALL_POPULATED_CHANNELS {
3075 MCHBAR32(TC_RAP_ch(channel)) =
3076 (ctrl->tRRD << 0)
3077 | (ctrl->tRTP << 4)
3078 | (ctrl->tCKE << 8)
3079 | (ctrl->tWTR << 12)
3080 | (ctrl->tFAW << 16)
3081 | (ctrl->tWR << 24)
3082 | (ctrl->cmd_stretch[channel] << 30);
3083 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003084
3085 udelay(1);
3086
3087 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003088 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003089 }
3090
3091 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003092 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003093 }
3094
3095 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003096 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003097
3098 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003099 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003100 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003101 }
3102
3103 printram("CPE\n");
3104
Angel Pons88521882020-01-05 20:21:20 +01003105 MCHBAR32(GDCRTRAININGMOD) = 0;
3106 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003107
3108 printram("CP5b\n");
3109
3110 FOR_ALL_POPULATED_CHANNELS {
3111 program_timings(ctrl, channel);
3112 }
3113
3114 u32 reg, addr;
3115
Angel Pons7c49cb82020-03-16 23:17:32 +01003116 /* Poll for RCOMP */
3117 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3118 ;
3119
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003120 do {
Angel Pons88521882020-01-05 20:21:20 +01003121 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003122 } while ((reg & 0x14) == 0);
3123
Angel Pons7c49cb82020-03-16 23:17:32 +01003124 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003125 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003126 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003127
Angel Pons7c49cb82020-03-16 23:17:32 +01003128 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003129 udelay(500);
3130
3131 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003132 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003133 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003134 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003135 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003136 MCHBAR32(addr) = reg;
3137
Angel Pons7c49cb82020-03-16 23:17:32 +01003138 /* Wait 10ns for ranks to settle */
3139 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003140
3141 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3142 MCHBAR32(addr) = reg;
3143
Angel Pons7c49cb82020-03-16 23:17:32 +01003144 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003145 write_reset(ctrl);
3146 }
3147
Angel Pons7c49cb82020-03-16 23:17:32 +01003148 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003149 dram_mrscommands(ctrl);
3150
3151 printram("CP5c\n");
3152
Angel Pons88521882020-01-05 20:21:20 +01003153 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003154
3155 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003156 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003157 udelay(2);
3158 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003159}