blob: 453222e59d4e42c3e14f2e8b8b92b5425ca04d25 [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;
Angel Ponsdca3cb52020-11-13 13:42:07 +0100762 int srt = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100763
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
Angel Ponsdca3cb52020-11-13 13:42:07 +0100768 if (IS_IVY_CPU(ctrl->cpu) && ctrl->tCK >= TCK_1066MHZ)
769 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100770
771 mr2reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100772 mr2reg = (mr2reg & ~0x07) | pasr;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100773 mr2reg = (mr2reg & ~0x38) | (cwl << 3);
774 mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6);
775 mr2reg = (mr2reg & ~0x80) | (srt << 7);
776 mr2reg |= (odt.rttwr / 60) << 9;
777
778 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Angel Pons7f1363d2020-11-13 13:31:58 +0100779
780 /* Program MR2 shadow */
781 u32 reg32 = MCHBAR32(TC_MR2_SHADOW_ch(channel));
782
783 reg32 &= 3 << 14 | 3 << 6;
784
785 reg32 |= mr2reg & ~(3 << 6);
786
787 if (rank & 1) {
788 if (srt)
789 reg32 |= 1 << (rank / 2 + 6);
790 } else {
791 if (ctrl->rank_mirror[channel][rank])
792 reg32 |= 1 << (rank / 2 + 14);
793 }
794 MCHBAR32(TC_MR2_SHADOW_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100795}
796
797static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
798{
799 write_mrreg(ctrl, channel, rank, 3, 0);
800}
801
Angel Pons88521882020-01-05 20:21:20 +0100802void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100803{
804 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100805 int channel;
806
807 FOR_ALL_POPULATED_CHANNELS {
808 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100809 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100810 dram_mr2(ctrl, slotrank, channel);
811
Angel Pons7c49cb82020-03-16 23:17:32 +0100812 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100813 dram_mr3(ctrl, slotrank, channel);
814
Angel Pons7c49cb82020-03-16 23:17:32 +0100815 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100816 dram_mr1(ctrl, slotrank, channel);
817
Angel Pons7c49cb82020-03-16 23:17:32 +0100818 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100819 dram_mr0(ctrl, slotrank, channel);
820 }
821 }
822
Angel Pons8f0757e2020-11-11 23:03:36 +0100823 const struct iosav_ssq zqcl_sequence[] = {
824 /* DRAM command NOP (without ODT nor chip selects) */
825 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200826 .sp_cmd_ctrl = {
827 .command = IOSAV_NOP & ~(0xff << 8),
828 },
829 .subseq_ctrl = {
830 .cmd_executions = 1,
831 .cmd_delay_gap = 4,
832 .post_ssq_wait = 15,
833 .data_direction = SSQ_NA,
834 },
835 .sp_cmd_addr = {
836 .address = 2,
837 .rowbits = 6,
838 .bank = 0,
839 .rank = 0,
840 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100841 },
842 /* DRAM command ZQCL */
843 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200844 .sp_cmd_ctrl = {
845 .command = IOSAV_ZQCS,
846 .ranksel_ap = 1,
847 },
848 .subseq_ctrl = {
849 .cmd_executions = 1,
850 .cmd_delay_gap = 4,
851 .post_ssq_wait = 400,
852 .data_direction = SSQ_NA,
853 },
854 .sp_cmd_addr = {
855 .address = 1024,
856 .rowbits = 6,
857 .bank = 0,
858 .rank = 0,
859 },
860 .addr_update = {
861 .inc_rank = 1,
862 .addr_wrap = 20,
863 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100864 },
865 };
866 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100867
Angel Pons7c49cb82020-03-16 23:17:32 +0100868 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200869 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100870
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100871 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100872 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100873 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100874 }
875
Angel Pons7c49cb82020-03-16 23:17:32 +0100876 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +0100877 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100878
879 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +0100880 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100881
Angel Pons88521882020-01-05 20:21:20 +0100882 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100883
884 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
885
Angel Pons7c49cb82020-03-16 23:17:32 +0100886 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100887 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100888
Angel Ponsffd50152020-11-12 11:03:10 +0100889 iosav_write_zqcs_sequence(channel, slotrank, 4, 101, 31);
Felix Held9cf1dd22018-07-31 14:52:40 +0200890
Angel Pons7c49cb82020-03-16 23:17:32 +0100891 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200892 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100893
Angel Pons7c49cb82020-03-16 23:17:32 +0100894 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100895 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100896 }
897}
898
Felix Held3b906032020-01-14 17:05:43 +0100899static const u32 lane_base[] = {
900 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
901 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
902 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100903};
904
Angel Pons88521882020-01-05 20:21:20 +0100905void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100906{
Angel Pons88521882020-01-05 20:21:20 +0100907 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100908 int lane;
909 int slotrank, slot;
910 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100911 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100912
913 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100914 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
915 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100916 }
917
918 for (slot = 0; slot < NUM_SLOTS; slot++)
919 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
920 case 0:
921 default:
Angel Pons88521882020-01-05 20:21:20 +0100922 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100923 break;
924 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100925 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100926 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100927 break;
928 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100929 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100930 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100931 break;
932 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100933 pi_coding_ctrl[slot] =
934 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100935 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100936 break;
937 }
938
Angel Pons7c49cb82020-03-16 23:17:32 +0100939 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100941 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
942 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100943 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100944 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
945
Angel Pons88521882020-01-05 20:21:20 +0100946 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100947
Angel Pons7c49cb82020-03-16 23:17:32 +0100948 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100949 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
950 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100951
952 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100953 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100954 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100955 if (shift < 0)
956 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100957
Angel Pons88521882020-01-05 20:21:20 +0100958 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100959
960 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100961 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
962 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100963 }
964
Angel Pons88521882020-01-05 20:21:20 +0100965 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
966 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100967
Angel Pons88521882020-01-05 20:21:20 +0100968 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Angel Ponsdc5539f2020-11-12 12:44:25 +0100969 reg_io_latency &= ~0xffff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100970
Angel Pons88521882020-01-05 20:21:20 +0100971 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100972
973 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100974 int post_timA_min_high = 7, pre_timA_min_high = 7;
975 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100976 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100977 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100978
979 if (shift < 0)
980 shift = 0;
981
982 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200983 post_timA_min_high = MIN(post_timA_min_high,
984 (ctrl->timings[channel][slotrank].lanes[lane].
985 timA + shift) >> 6);
986 pre_timA_min_high = MIN(pre_timA_min_high,
987 ctrl->timings[channel][slotrank].lanes[lane].
988 timA >> 6);
989 post_timA_max_high = MAX(post_timA_max_high,
990 (ctrl->timings[channel][slotrank].lanes[lane].
991 timA + shift) >> 6);
992 pre_timA_max_high = MAX(pre_timA_max_high,
993 ctrl->timings[channel][slotrank].lanes[lane].
994 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100995 }
996
997 if (pre_timA_max_high - pre_timA_min_high <
998 post_timA_max_high - post_timA_min_high)
999 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001000
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001001 else if (pre_timA_max_high - pre_timA_min_high >
1002 post_timA_max_high - post_timA_min_high)
1003 shift_402x = -1;
1004
Felix Helddee167e2019-12-30 17:30:16 +01001005 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +01001006 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001007 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001008
Angel Pons88521882020-01-05 20:21:20 +01001009 reg_roundtrip_latency |=
1010 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001011 shift_402x) << (8 * slotrank);
1012
1013 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001014 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001015 (((ctrl->timings[channel][slotrank].lanes[lane].
1016 timA + shift) & 0x3f)
1017 |
1018 ((ctrl->timings[channel][slotrank].lanes[lane].
1019 rising + shift) << 8)
1020 |
1021 (((ctrl->timings[channel][slotrank].lanes[lane].
1022 timA + shift -
1023 (post_timA_min_high << 6)) & 0x1c0) << 10)
1024 | ((ctrl->timings[channel][slotrank].lanes[lane].
1025 falling + shift) << 20));
1026
Felix Heldfb19c8a2020-01-14 21:27:59 +01001027 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001028 (((ctrl->timings[channel][slotrank].lanes[lane].
1029 timC + shift) & 0x3f)
1030 |
1031 (((ctrl->timings[channel][slotrank].lanes[lane].
1032 timB + shift) & 0x3f) << 8)
1033 |
1034 (((ctrl->timings[channel][slotrank].lanes[lane].
1035 timB + shift) & 0x1c0) << 9)
1036 |
1037 (((ctrl->timings[channel][slotrank].lanes[lane].
1038 timC + shift) & 0x40) << 13));
1039 }
1040 }
Angel Pons88521882020-01-05 20:21:20 +01001041 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1042 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001043}
1044
Angel Pons88521882020-01-05 20:21:20 +01001045static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001046{
Angel Pons88521882020-01-05 20:21:20 +01001047 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001048
Angel Ponsffd50152020-11-12 11:03:10 +01001049 iosav_write_read_mpr_sequence(channel, slotrank, ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001050
Angel Pons7c49cb82020-03-16 23:17:32 +01001051 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001052 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001053
Angel Pons88521882020-01-05 20:21:20 +01001054 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001055}
1056
Angel Pons7c49cb82020-03-16 23:17:32 +01001057static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001058{
1059 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001060
1061 return (MCHBAR32(lane_base[lane] +
1062 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001063}
1064
1065struct run {
1066 int middle;
1067 int end;
1068 int start;
1069 int all;
1070 int length;
1071};
1072
1073static struct run get_longest_zero_run(int *seq, int sz)
1074{
1075 int i, ls;
1076 int bl = 0, bs = 0;
1077 struct run ret;
1078
1079 ls = 0;
1080 for (i = 0; i < 2 * sz; i++)
1081 if (seq[i % sz]) {
1082 if (i - ls > bl) {
1083 bl = i - ls;
1084 bs = ls;
1085 }
1086 ls = i + 1;
1087 }
1088 if (bl == 0) {
1089 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001090 ret.start = 0;
1091 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001092 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001093 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001094 return ret;
1095 }
1096
Angel Pons7c49cb82020-03-16 23:17:32 +01001097 ret.start = bs % sz;
1098 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001099 ret.middle = (bs + (bl - 1) / 2) % sz;
1100 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001101 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001102
1103 return ret;
1104}
1105
Angel Pons7c49cb82020-03-16 23:17:32 +01001106static void discover_timA_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001107{
1108 int timA;
1109 int statistics[NUM_LANES][128];
1110 int lane;
1111
1112 for (timA = 0; timA < 128; timA++) {
1113 FOR_ALL_LANES {
1114 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1115 }
1116 program_timings(ctrl, channel);
1117
1118 test_timA(ctrl, channel, slotrank);
1119
1120 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001121 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001122 }
1123 }
1124 FOR_ALL_LANES {
1125 struct run rn = get_longest_zero_run(statistics[lane], 128);
1126 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1127 upperA[lane] = rn.end;
1128 if (upperA[lane] < rn.middle)
1129 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001130
Patrick Rudolph368b6152016-11-25 16:36:52 +01001131 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001132 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001133 }
1134}
1135
Angel Pons7c49cb82020-03-16 23:17:32 +01001136static void discover_timA_fine(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001137{
1138 int timA_delta;
1139 int statistics[NUM_LANES][51];
1140 int lane, i;
1141
1142 memset(statistics, 0, sizeof(statistics));
1143
1144 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001145
1146 FOR_ALL_LANES {
1147 ctrl->timings[channel][slotrank].lanes[lane].timA
1148 = upperA[lane] + timA_delta + 0x40;
1149 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001150 program_timings(ctrl, channel);
1151
1152 for (i = 0; i < 100; i++) {
1153 test_timA(ctrl, channel, slotrank);
1154 FOR_ALL_LANES {
1155 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001156 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001157 }
1158 }
1159 }
1160 FOR_ALL_LANES {
1161 int last_zero, first_all;
1162
1163 for (last_zero = -25; last_zero <= 25; last_zero++)
1164 if (statistics[lane][last_zero + 25])
1165 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001166
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001167 last_zero--;
1168 for (first_all = -25; first_all <= 25; first_all++)
1169 if (statistics[lane][first_all + 25] == 100)
1170 break;
1171
Angel Pons7c49cb82020-03-16 23:17:32 +01001172 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001173
1174 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001175 (last_zero + first_all) / 2 + upperA[lane];
1176
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001177 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001178 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001179 }
1180}
1181
Angel Pons891f2bc2020-01-10 01:27:28 +01001182static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001183{
1184 int works[NUM_LANES];
1185 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001186
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001187 while (1) {
1188 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001189
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001190 program_timings(ctrl, channel);
1191 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001192
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001194 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1195
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001196 if (works[lane])
1197 some_works = 1;
1198 else
1199 all_works = 0;
1200 }
1201 if (all_works)
1202 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001203
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001204 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001205 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001206 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1207 channel, slotrank);
1208 return MAKE_ERR;
1209 }
Angel Pons88521882020-01-05 20:21:20 +01001210 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001211 printram("4024 -= 2;\n");
1212 continue;
1213 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001214 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001215 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001216
Felix Heldef4fe3e2019-12-31 14:15:05 +01001217 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001218 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1219 channel, slotrank);
1220 return MAKE_ERR;
1221 }
1222 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001223 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001224 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001225 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001226 }
1227 }
1228 return 0;
1229}
1230
1231struct timA_minmax {
1232 int timA_min_high, timA_max_high;
1233};
1234
Angel Pons88521882020-01-05 20:21:20 +01001235static void pre_timA_change(ramctr_timing *ctrl, int channel, int slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001236 struct timA_minmax *mnmx)
1237{
1238 int lane;
1239 mnmx->timA_min_high = 7;
1240 mnmx->timA_max_high = 0;
1241
1242 FOR_ALL_LANES {
1243 if (mnmx->timA_min_high >
1244 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1245 mnmx->timA_min_high =
Angel Pons891f2bc2020-01-10 01:27:28 +01001246 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001247 if (mnmx->timA_max_high <
1248 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1249 mnmx->timA_max_high =
Angel Pons891f2bc2020-01-10 01:27:28 +01001250 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001251 }
1252}
1253
Angel Pons88521882020-01-05 20:21:20 +01001254static void post_timA_change(ramctr_timing *ctrl, int channel, int slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001255 struct timA_minmax *mnmx)
1256{
1257 struct timA_minmax post;
1258 int shift_402x = 0;
1259
Angel Pons7c49cb82020-03-16 23:17:32 +01001260 /* Get changed maxima */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001261 pre_timA_change(ctrl, channel, slotrank, &post);
1262
1263 if (mnmx->timA_max_high - mnmx->timA_min_high <
1264 post.timA_max_high - post.timA_min_high)
1265 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001266
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001267 else if (mnmx->timA_max_high - mnmx->timA_min_high >
1268 post.timA_max_high - post.timA_min_high)
1269 shift_402x = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001270
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001271 else
1272 shift_402x = 0;
1273
Felix Heldef4fe3e2019-12-31 14:15:05 +01001274 ctrl->timings[channel][slotrank].io_latency += shift_402x;
Angel Pons88521882020-01-05 20:21:20 +01001275 ctrl->timings[channel][slotrank].roundtrip_latency += shift_402x;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001276 printram("4024 += %d;\n", shift_402x);
1277 printram("4028 += %d;\n", shift_402x);
1278}
1279
Angel Pons7c49cb82020-03-16 23:17:32 +01001280/*
1281 * Compensate the skew between DQS and DQs.
1282 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001283 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1284 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001285 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001286 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001287 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001288 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1289 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1290 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001291 * DQ in phase the data being read is expected to alternate on every byte:
1292 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001293 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001294 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001295 * Once the controller has detected this pattern a bit in the result register is set for the
1296 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001297 */
Angel Pons88521882020-01-05 20:21:20 +01001298int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001299{
1300 int channel, slotrank, lane;
1301 int err;
1302
1303 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1304 int all_high, some_high;
1305 int upperA[NUM_LANES];
1306 struct timA_minmax mnmx;
1307
Angel Pons88521882020-01-05 20:21:20 +01001308 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001309
Angel Ponsffd50152020-11-12 11:03:10 +01001310 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 0);
Felix Held9cf1dd22018-07-31 14:52:40 +02001311
Angel Pons7c49cb82020-03-16 23:17:32 +01001312 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001313 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001314
Angel Pons88521882020-01-05 20:21:20 +01001315 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001316
Felix Heldef4fe3e2019-12-31 14:15:05 +01001317 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001318 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001319 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001320
Felix Held2bb3cdf2018-07-28 00:23:59 +02001321 discover_timA_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001322
Felix Held2bb3cdf2018-07-28 00:23:59 +02001323 all_high = 1;
1324 some_high = 0;
1325 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001326 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001327 some_high = 1;
1328 else
1329 all_high = 0;
1330 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001331
1332 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001333 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001334 printram("4028--;\n");
1335 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001336 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001337 upperA[lane] -= 0x40;
1338
1339 }
1340 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001341 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001342 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001343 printram("4024++;\n");
1344 printram("4028++;\n");
1345 }
1346
1347 program_timings(ctrl, channel);
1348
1349 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1350
1351 err = discover_402x(ctrl, channel, slotrank, upperA);
1352 if (err)
1353 return err;
1354
1355 post_timA_change(ctrl, channel, slotrank, &mnmx);
1356 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1357
1358 discover_timA_fine(ctrl, channel, slotrank, upperA);
1359
1360 post_timA_change(ctrl, channel, slotrank, &mnmx);
1361 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1362
1363 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001364 ctrl->timings[channel][slotrank].lanes[lane].timA -=
1365 mnmx.timA_min_high * 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001366 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001367 ctrl->timings[channel][slotrank].io_latency -= mnmx.timA_min_high;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001368 printram("4028 -= %d;\n", mnmx.timA_min_high);
1369
1370 post_timA_change(ctrl, channel, slotrank, &mnmx);
1371
1372 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001373 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001374 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001375
1376 printram("final results:\n");
1377 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001378 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001379 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001380
Angel Pons88521882020-01-05 20:21:20 +01001381 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001382
1383 toggle_io_reset();
1384 }
1385
1386 FOR_ALL_POPULATED_CHANNELS {
1387 program_timings(ctrl, channel);
1388 }
1389 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001390 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001391 }
1392 return 0;
1393}
1394
Angel Pons88521882020-01-05 20:21:20 +01001395static void test_timC(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001396{
1397 int lane;
1398
1399 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001400 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1401 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001402 }
1403
Angel Pons88521882020-01-05 20:21:20 +01001404 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001405
Angel Ponsffd50152020-11-12 11:03:10 +01001406 iosav_write_misc_write_sequence(ctrl, channel, slotrank,
1407 MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001408
Angel Pons7c49cb82020-03-16 23:17:32 +01001409 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001410 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001411
Angel Pons88521882020-01-05 20:21:20 +01001412 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001413
Angel Pons8f0757e2020-11-11 23:03:36 +01001414 const struct iosav_ssq rd_sequence[] = {
1415 /* DRAM command PREA */
1416 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001417 .sp_cmd_ctrl = {
1418 .command = IOSAV_PRE,
1419 .ranksel_ap = 1,
1420 },
1421 .subseq_ctrl = {
1422 .cmd_executions = 1,
1423 .cmd_delay_gap = 3,
1424 .post_ssq_wait = ctrl->tRP,
1425 .data_direction = SSQ_NA,
1426 },
1427 .sp_cmd_addr = {
1428 .address = 1024,
1429 .rowbits = 6,
1430 .bank = 0,
1431 .rank = slotrank,
1432 },
1433 .addr_update = {
1434 .addr_wrap = 18,
1435 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001436 },
1437 /* DRAM command ACT */
1438 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001439 .sp_cmd_ctrl = {
1440 .command = IOSAV_ACT,
1441 .ranksel_ap = 1,
1442 },
1443 .subseq_ctrl = {
1444 .cmd_executions = 8,
1445 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1446 .post_ssq_wait = ctrl->CAS,
1447 .data_direction = SSQ_NA,
1448 },
1449 .sp_cmd_addr = {
1450 .address = 0,
1451 .rowbits = 6,
1452 .bank = 0,
1453 .rank = slotrank,
1454 },
1455 .addr_update = {
1456 .inc_bank = 1,
1457 .addr_wrap = 18,
1458 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001459 },
1460 /* DRAM command RD */
1461 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001462 .sp_cmd_ctrl = {
1463 .command = IOSAV_RD,
1464 .ranksel_ap = 1,
1465 },
1466 .subseq_ctrl = {
1467 .cmd_executions = 500,
1468 .cmd_delay_gap = 4,
1469 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1470 .data_direction = SSQ_RD,
1471 },
1472 .sp_cmd_addr = {
1473 .address = 0,
1474 .rowbits = 0,
1475 .bank = 0,
1476 .rank = slotrank,
1477 },
1478 .addr_update = {
1479 .inc_addr_8 = 1,
1480 .addr_wrap = 18,
1481 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001482 },
1483 /* DRAM command PREA */
1484 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001485 .sp_cmd_ctrl = {
1486 .command = IOSAV_PRE,
1487 .ranksel_ap = 1,
1488 },
1489 .subseq_ctrl = {
1490 .cmd_executions = 1,
1491 .cmd_delay_gap = 3,
1492 .post_ssq_wait = ctrl->tRP,
1493 .data_direction = SSQ_NA,
1494 },
1495 .sp_cmd_addr = {
1496 .address = 1024,
1497 .rowbits = 6,
1498 .bank = 0,
1499 .rank = slotrank,
1500 },
1501 .addr_update = {
1502 .addr_wrap = 18,
1503 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001504 },
1505 };
1506 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001507
Angel Pons7c49cb82020-03-16 23:17:32 +01001508 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001509 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001510
Angel Pons88521882020-01-05 20:21:20 +01001511 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001512}
1513
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001514static void timC_threshold_process(int *data, const int count)
1515{
1516 int min = data[0];
1517 int max = min;
1518 int i;
1519 for (i = 1; i < count; i++) {
1520 if (min > data[i])
1521 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001522
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001523 if (max < data[i])
1524 max = data[i];
1525 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001526 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001527 for (i = 0; i < count; i++)
1528 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001529
Angel Pons891f2bc2020-01-10 01:27:28 +01001530 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001531}
1532
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001533static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
1534{
1535 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01001536 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001537 int lane;
1538
Angel Pons88521882020-01-05 20:21:20 +01001539 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001540
Angel Ponsffd50152020-11-12 11:03:10 +01001541 iosav_write_prea_sequence(channel, slotrank, ctrl->tRP, 18);
Felix Held9cf1dd22018-07-31 14:52:40 +02001542
Angel Pons7c49cb82020-03-16 23:17:32 +01001543 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001544 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001545
1546 for (timC = 0; timC <= MAX_TIMC; timC++) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001547 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001548 program_timings(ctrl, channel);
1549
1550 test_timC(ctrl, channel, slotrank);
1551
1552 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001553 stats[lane][timC] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001554 }
1555 }
1556 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001557 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1558
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001559 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001560 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1561 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001562 /*
1563 * With command training not being done yet, the lane can be erroneous.
1564 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001565 */
Angel Pons7c49cb82020-03-16 23:17:32 +01001566 timC_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
1567 rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1568
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001569 if (rn.all || rn.length < 8) {
1570 printk(BIOS_EMERG, "timC recovery failed\n");
1571 return MAKE_ERR;
1572 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001573 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001574 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001575 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001576 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001577 }
1578 return 0;
1579}
1580
Angel Pons88521882020-01-05 20:21:20 +01001581static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001582{
1583 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001584
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001585 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1586 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001587
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001588 return ret;
1589}
1590
Angel Pons765d4652020-11-11 14:44:35 +01001591/* Each cacheline is 64 bits long */
1592static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1593{
1594 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1595}
1596
Angel Pons88521882020-01-05 20:21:20 +01001597static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001598{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301599 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001600 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001601
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001602 for (j = 0; j < 16; j++)
1603 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001604
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001605 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001606
1607 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001608}
1609
Angel Pons88521882020-01-05 20:21:20 +01001610static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001611{
1612 int ret = 0;
1613 int channel;
1614 FOR_ALL_POPULATED_CHANNELS ret++;
1615 return ret;
1616}
1617
Angel Pons88521882020-01-05 20:21:20 +01001618static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001619{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301620 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001621 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301622 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001623
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001624 for (j = 0; j < 16; j++)
1625 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001626
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001627 for (j = 0; j < 16; j++)
1628 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001629
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001630 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001631
1632 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001633}
1634
Angel Pons88521882020-01-05 20:21:20 +01001635static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001636{
1637 int channel, slotrank, lane;
1638
1639 FOR_ALL_POPULATED_CHANNELS {
1640 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001641 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1642 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001643 }
1644
1645 program_timings(ctrl, channel);
1646
1647 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001648 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001649
Angel Ponsffd50152020-11-12 11:03:10 +01001650 iosav_write_read_mpr_sequence(
1651 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02001652
Angel Pons7c49cb82020-03-16 23:17:32 +01001653 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001654 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001655
Angel Pons88521882020-01-05 20:21:20 +01001656 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001657 }
1658
1659 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001660 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1661 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001662 }
1663
1664 program_timings(ctrl, channel);
1665
1666 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001667 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001668
Angel Ponsffd50152020-11-12 11:03:10 +01001669 iosav_write_read_mpr_sequence(
1670 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001671
Angel Pons7c49cb82020-03-16 23:17:32 +01001672 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001673 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001674
Angel Pons88521882020-01-05 20:21:20 +01001675 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001676 }
1677 }
1678}
1679
Angel Pons88521882020-01-05 20:21:20 +01001680static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001681{
1682 /* enable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001683 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel) | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001684
Angel Pons88521882020-01-05 20:21:20 +01001685 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001686
1687 const struct iosav_ssq sequence[] = {
1688 /* DRAM command NOP */
1689 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001690 .sp_cmd_ctrl = {
1691 .command = IOSAV_NOP,
1692 .ranksel_ap = 1,
1693 },
1694 .subseq_ctrl = {
1695 .cmd_executions = 1,
1696 .cmd_delay_gap = 3,
1697 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1698 .data_direction = SSQ_WR,
1699 },
1700 .sp_cmd_addr = {
1701 .address = 8,
1702 .rowbits = 0,
1703 .bank = 0,
1704 .rank = slotrank,
1705 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001706 },
1707 /* DRAM command NOP */
1708 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001709 .sp_cmd_ctrl = {
1710 .command = IOSAV_NOP_ALT,
1711 .ranksel_ap = 1,
1712 },
1713 .subseq_ctrl = {
1714 .cmd_executions = 1,
1715 .cmd_delay_gap = 3,
1716 .post_ssq_wait = ctrl->CAS + 38,
1717 .data_direction = SSQ_RD,
1718 },
1719 .sp_cmd_addr = {
1720 .address = 4,
1721 .rowbits = 0,
1722 .bank = 0,
1723 .rank = slotrank,
1724 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001725 },
1726 };
1727 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001728
Angel Pons7c49cb82020-03-16 23:17:32 +01001729 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001730 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001731
Angel Pons88521882020-01-05 20:21:20 +01001732 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001733
1734 /* disable DQs on this slotrank */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001735 write_mrreg(ctrl, channel, slotrank, 1,
1736 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001737}
1738
1739static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1740{
1741 int timB;
1742 int statistics[NUM_LANES][128];
1743 int lane;
1744
Angel Pons88521882020-01-05 20:21:20 +01001745 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001746
1747 for (timB = 0; timB < 128; timB++) {
1748 FOR_ALL_LANES {
1749 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1750 }
1751 program_timings(ctrl, channel);
1752
1753 test_timB(ctrl, channel, slotrank);
1754
1755 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001756 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1757 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1758 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001759 }
1760 }
1761 FOR_ALL_LANES {
1762 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001763 /*
1764 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1765 * of timB by a small value, which might cause the 6-bit value to overflow if
1766 * it's close to 0x3f. Increment the value by a small offset if it's likely
1767 * to overflow, to make sure it won't overflow while running tests and bricks
1768 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001769 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001770 * TODO: find out why some tests (edge write discovery) increment timB.
1771 */
1772 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001773 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001774 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001775 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001776
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001777 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1778 if (rn.all) {
1779 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1780 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001781
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001782 return MAKE_ERR;
1783 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001784 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1785 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786 }
1787 return 0;
1788}
1789
1790static int get_timB_high_adjust(u64 val)
1791{
1792 int i;
1793
Angel Ponsbf13ef02020-11-11 18:40:06 +01001794 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001795 if (val == 0xffffffffffffffffLL)
1796 return 0;
1797
1798 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001799 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001800 for (i = 0; i < 8; i++)
1801 if (val << (8 * (7 - i) + 4))
1802 return -i;
1803 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001804 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001805 for (i = 0; i < 8; i++)
1806 if (val >> (8 * (7 - i) + 4))
1807 return i;
1808 }
1809 return 8;
1810}
1811
Angel Ponsbf13ef02020-11-11 18:40:06 +01001812static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001813{
1814 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001815 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001816 FOR_ALL_POPULATED_CHANNELS {
1817 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001818 }
1819 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1820
Angel Pons765d4652020-11-11 14:44:35 +01001821 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001822 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001823
Angel Pons88521882020-01-05 20:21:20 +01001824 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001825
Angel Ponsffd50152020-11-12 11:03:10 +01001826 iosav_write_misc_write_sequence(ctrl, channel, slotrank, 3, 1, 3, 3, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001827
Angel Pons7c49cb82020-03-16 23:17:32 +01001828 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001829 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001830
Angel Pons88521882020-01-05 20:21:20 +01001831 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001832
Angel Pons8f0757e2020-11-11 23:03:36 +01001833 const struct iosav_ssq rd_sequence[] = {
1834 /* DRAM command PREA */
1835 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001836 .sp_cmd_ctrl = {
1837 .command = IOSAV_PRE,
1838 .ranksel_ap = 1,
1839 },
1840 .subseq_ctrl = {
1841 .cmd_executions = 1,
1842 .cmd_delay_gap = 3,
1843 .post_ssq_wait = ctrl->tRP,
1844 .data_direction = SSQ_NA,
1845 },
1846 .sp_cmd_addr = {
1847 .address = 1024,
1848 .rowbits = 6,
1849 .bank = 0,
1850 .rank = slotrank,
1851 },
1852 .addr_update = {
1853 .addr_wrap = 18,
1854 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001855 },
1856 /* DRAM command ACT */
1857 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001858 .sp_cmd_ctrl = {
1859 .command = IOSAV_ACT,
1860 .ranksel_ap = 1,
1861 },
1862 .subseq_ctrl = {
1863 .cmd_executions = 1,
1864 .cmd_delay_gap = 3,
1865 .post_ssq_wait = ctrl->tRCD,
1866 .data_direction = SSQ_NA,
1867 },
1868 .sp_cmd_addr = {
1869 .address = 0,
1870 .rowbits = 6,
1871 .bank = 0,
1872 .rank = slotrank,
1873 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001874 },
1875 /* DRAM command RD */
1876 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001877 .sp_cmd_ctrl = {
1878 .command = IOSAV_RD,
1879 .ranksel_ap = 3,
1880 },
1881 .subseq_ctrl = {
1882 .cmd_executions = 1,
1883 .cmd_delay_gap = 3,
1884 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001885 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001886 ctrl->timings[channel][slotrank].io_latency,
1887 .data_direction = SSQ_RD,
1888 },
1889 .sp_cmd_addr = {
1890 .address = 8,
1891 .rowbits = 6,
1892 .bank = 0,
1893 .rank = slotrank,
1894 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001895 },
1896 };
1897 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001898
Angel Pons7c49cb82020-03-16 23:17:32 +01001899 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001900 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001901
Angel Pons88521882020-01-05 20:21:20 +01001902 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001903 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001904 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001905 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001906 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001907 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1908 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1909 get_timB_high_adjust(res) * 64;
1910
1911 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001912 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1913 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001914 }
1915 }
Angel Pons88521882020-01-05 20:21:20 +01001916 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001917}
1918
Angel Pons88521882020-01-05 20:21:20 +01001919static void write_op(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001920{
1921 int slotrank;
1922
Angel Pons88521882020-01-05 20:21:20 +01001923 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001924
1925 /* choose an existing rank. */
1926 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
1927
Angel Ponsffd50152020-11-12 11:03:10 +01001928 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001929
Angel Pons7c49cb82020-03-16 23:17:32 +01001930 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001931 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001932
Angel Pons88521882020-01-05 20:21:20 +01001933 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001934}
1935
Angel Pons7c49cb82020-03-16 23:17:32 +01001936/*
1937 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001938 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001939 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1940 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1941 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1942 * CLK/ADDR/CMD signals have the same routing delay.
1943 *
1944 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1945 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1946 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001947 */
Angel Pons88521882020-01-05 20:21:20 +01001948int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001949{
1950 int channel, slotrank, lane;
1951 int err;
1952
1953 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01001954 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001955
1956 FOR_ALL_POPULATED_CHANNELS {
1957 write_op(ctrl, channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01001958 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001959 }
1960
Angel Pons7c49cb82020-03-16 23:17:32 +01001961 /* Refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001962 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001963 FOR_ALL_POPULATED_CHANNELS {
1964 write_op(ctrl, channel);
1965 }
1966
Angel Pons7c49cb82020-03-16 23:17:32 +01001967 /* Enable write leveling on all ranks
1968 Disable all DQ outputs
1969 Only NOP is allowed in this mode */
1970 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1971 write_mrreg(ctrl, channel, slotrank, 1,
Angel Ponsdc5539f2020-11-12 12:44:25 +01001972 make_mr1(ctrl, slotrank, channel) | 1 << 12 | 1 << 7);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001973
Angel Pons88521882020-01-05 20:21:20 +01001974 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001975
1976 toggle_io_reset();
1977
Angel Pons7c49cb82020-03-16 23:17:32 +01001978 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001979 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1980 err = discover_timB(ctrl, channel, slotrank);
1981 if (err)
1982 return err;
1983 }
1984
Angel Pons7c49cb82020-03-16 23:17:32 +01001985 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001986 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001987 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001988
Angel Pons88521882020-01-05 20:21:20 +01001989 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001990
1991 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001992 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001993
Angel Pons7c49cb82020-03-16 23:17:32 +01001994 /* Refresh enable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01001995 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 3);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001996
1997 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01001998 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~(1 << 21));
Angel Pons88521882020-01-05 20:21:20 +01001999 MCHBAR32(IOSAV_STATUS_ch(channel));
2000 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002001
Angel Ponsffd50152020-11-12 11:03:10 +01002002 iosav_write_zqcs_sequence(channel, 0, 4, 101, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002003
Angel Pons7c49cb82020-03-16 23:17:32 +01002004 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002005 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002006
Angel Pons88521882020-01-05 20:21:20 +01002007 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002008 }
2009
2010 toggle_io_reset();
2011
2012 printram("CPE\n");
2013 precharge(ctrl);
2014 printram("CPF\n");
2015
2016 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002017 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002018 }
2019
2020 FOR_ALL_POPULATED_CHANNELS {
2021 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002022 }
2023
2024 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2025 err = discover_timC(ctrl, channel, slotrank);
2026 if (err)
2027 return err;
2028 }
2029
2030 FOR_ALL_POPULATED_CHANNELS
2031 program_timings(ctrl, channel);
2032
2033 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002034 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002035
2036 FOR_ALL_POPULATED_CHANNELS
2037 program_timings(ctrl, channel);
2038
2039 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002040 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002041 }
2042 return 0;
2043}
2044
Angel Ponsbf13ef02020-11-11 18:40:06 +01002045static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002046{
2047 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2048 int timC_delta;
2049 int lanes_ok = 0;
2050 int ctr = 0;
2051 int lane;
2052
2053 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2054 FOR_ALL_LANES {
2055 ctrl->timings[channel][slotrank].lanes[lane].timC =
2056 saved_rt.lanes[lane].timC + timC_delta;
2057 }
2058 program_timings(ctrl, channel);
2059 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002060 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002061 }
2062
Angel Pons765d4652020-11-11 14:44:35 +01002063 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002064 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002065
Angel Pons88521882020-01-05 20:21:20 +01002066 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002067
Angel Ponsffd50152020-11-12 11:03:10 +01002068 iosav_write_command_training_sequence(ctrl, channel, slotrank, ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002069
2070 /* Program LFSR for the RD/WR subsequences */
2071 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2072 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002073
Angel Pons7c49cb82020-03-16 23:17:32 +01002074 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002075 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002076
Angel Pons88521882020-01-05 20:21:20 +01002077 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002078 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002079 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002080
2081 if (r32 == 0)
2082 lanes_ok |= 1 << lane;
2083 }
2084 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002085 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002086 break;
2087 }
2088
2089 ctrl->timings[channel][slotrank] = saved_rt;
2090
Patrick Rudolphdd662872017-10-28 18:20:11 +02002091 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002092}
2093
Angel Pons88521882020-01-05 20:21:20 +01002094static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002095{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302096 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002097 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2098 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002099
2100 if (patno) {
2101 u8 base8 = 0x80 >> ((patno - 1) % 8);
2102 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2103 for (i = 0; i < 32; i++) {
2104 for (j = 0; j < 16; j++) {
2105 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002106
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002107 if (invert[patno - 1][i] & (1 << (j / 2)))
2108 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002109
2110 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002111 }
2112 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002113 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002114 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2115 for (j = 0; j < 16; j++) {
2116 const u32 val = pattern[i][j];
2117 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2118 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002119 }
2120 sfence();
2121 }
Angel Pons765d4652020-11-11 14:44:35 +01002122
2123 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002124}
2125
Angel Pons88521882020-01-05 20:21:20 +01002126static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002127{
2128 int channel, slotrank;
2129
2130 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002131 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002132
Angel Pons7c49cb82020-03-16 23:17:32 +01002133 /* Choose an existing rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002134 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2135
Angel Ponsffd50152020-11-12 11:03:10 +01002136 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002137
Angel Pons7c49cb82020-03-16 23:17:32 +01002138 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002139 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002140
Angel Pons88521882020-01-05 20:21:20 +01002141 wait_for_iosav(channel);
Angel Ponsdc5539f2020-11-12 12:44:25 +01002142 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143 }
2144
2145 /* refresh disable */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002146 MCHBAR32_AND(MC_INIT_STATE_G, ~(1 << 3));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002147 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002148 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002149
2150 /* choose an existing rank. */
2151 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2152
Angel Ponsffd50152020-11-12 11:03:10 +01002153 iosav_write_zqcs_sequence(channel, slotrank, 4, 4, 31);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002154
Angel Pons7c49cb82020-03-16 23:17:32 +01002155 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002156 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002157
Angel Pons88521882020-01-05 20:21:20 +01002158 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159 }
2160
Angel Pons7c49cb82020-03-16 23:17:32 +01002161 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002162 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002163
2164 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002165 dram_mrscommands(ctrl);
2166
2167 toggle_io_reset();
2168}
2169
Angel Ponsbf13ef02020-11-11 18:40:06 +01002170#define CT_MIN_PI -127
2171#define CT_MAX_PI 128
2172#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2173
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002174#define MIN_C320C_LEN 13
2175
2176static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2177{
2178 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2179 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002180 int command_pi;
2181 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002182 int delta = 0;
2183
2184 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2185
2186 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002187 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002188 }
2189
2190 ctrl->cmd_stretch[channel] = cmd_stretch;
2191
Angel Pons88521882020-01-05 20:21:20 +01002192 MCHBAR32(TC_RAP_ch(channel)) =
Angel Pons7c49cb82020-03-16 23:17:32 +01002193 (ctrl->tRRD << 0)
2194 | (ctrl->tRTP << 4)
2195 | (ctrl->tCKE << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002196 | (ctrl->tWTR << 12)
2197 | (ctrl->tFAW << 16)
Angel Pons7c49cb82020-03-16 23:17:32 +01002198 | (ctrl->tWR << 24)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002199 | (ctrl->cmd_stretch[channel] << 30);
2200
2201 if (ctrl->cmd_stretch[channel] == 2)
2202 delta = 2;
2203 else if (ctrl->cmd_stretch[channel] == 0)
2204 delta = 4;
2205
2206 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002207 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002208 }
2209
Angel Ponsbf13ef02020-11-11 18:40:06 +01002210 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002211 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002212 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002213 }
2214 program_timings(ctrl, channel);
2215 reprogram_320c(ctrl);
2216 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002217 stat[slotrank][command_pi - CT_MIN_PI] =
2218 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002219 }
2220 }
2221 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002222 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002223
Angel Ponsbf13ef02020-11-11 18:40:06 +01002224 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002225 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2226 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002227
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002228 if (rn.all || rn.length < MIN_C320C_LEN) {
2229 FOR_ALL_POPULATED_RANKS {
2230 ctrl->timings[channel][slotrank] =
2231 saved_timings[channel][slotrank];
2232 }
2233 return MAKE_ERR;
2234 }
2235 }
2236
2237 return 0;
2238}
2239
Angel Pons7c49cb82020-03-16 23:17:32 +01002240/*
2241 * Adjust CMD phase shift and try multiple command rates.
2242 * A command rate of 2T doubles the time needed for address and command decode.
2243 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002244int command_training(ramctr_timing *ctrl)
2245{
2246 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002247
2248 FOR_ALL_POPULATED_CHANNELS {
2249 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002250 }
2251
2252 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002253 int cmdrate, err;
2254
2255 /*
2256 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002257 * Issue:
2258 * While c320c discovery seems to succeed raminit will fail in write training.
2259 *
2260 * Workaround:
2261 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2262 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002263 *
2264 * Single DIMM per channel:
2265 * Try command rate 1T and 2T
2266 */
2267 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002268 if (ctrl->tCMD)
2269 /* XMP gives the CMD rate in clock ticks, not ns */
2270 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002271
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002272 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002273 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2274
2275 if (!err)
2276 break;
2277 }
2278
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002279 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002280 printk(BIOS_EMERG, "c320c discovery failed\n");
2281 return err;
2282 }
2283
Angel Pons891f2bc2020-01-10 01:27:28 +01002284 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002285 }
2286
2287 FOR_ALL_POPULATED_CHANNELS
2288 program_timings(ctrl, channel);
2289
2290 reprogram_320c(ctrl);
2291 return 0;
2292}
2293
Angel Pons891f2bc2020-01-10 01:27:28 +01002294static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002295{
2296 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002297 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002298 int lane;
2299
2300 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2301 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002302 ctrl->timings[channel][slotrank].lanes[lane].rising = edge;
Angel Pons891f2bc2020-01-10 01:27:28 +01002303 ctrl->timings[channel][slotrank].lanes[lane].falling = edge;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002304 }
2305 program_timings(ctrl, channel);
2306
2307 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002308 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2309 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002310 }
2311
Angel Pons88521882020-01-05 20:21:20 +01002312 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002313
Angel Ponsffd50152020-11-12 11:03:10 +01002314 iosav_write_read_mpr_sequence(
2315 channel, slotrank, ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002316
Angel Pons7c49cb82020-03-16 23:17:32 +01002317 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002318 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002319
Angel Pons88521882020-01-05 20:21:20 +01002320 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002321
2322 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002323 stats[lane][edge] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324 }
2325 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002326
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002327 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002328 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002329 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002330
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002331 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002332 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2333 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002334 return MAKE_ERR;
2335 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002336 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002337 }
2338 return 0;
2339}
2340
2341int discover_edges(ramctr_timing *ctrl)
2342{
2343 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2344 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2345 int channel, slotrank, lane;
2346 int err;
2347
Angel Pons88521882020-01-05 20:21:20 +01002348 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002349
2350 toggle_io_reset();
2351
2352 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002353 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002354 }
2355
2356 FOR_ALL_POPULATED_CHANNELS {
2357 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002358 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002359 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002360 }
2361
2362 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01002363 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2364 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002365 }
2366
2367 program_timings(ctrl, channel);
2368
2369 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002370 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002371
Angel Ponsffd50152020-11-12 11:03:10 +01002372 iosav_write_read_mpr_sequence(
2373 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Felix Held9cf1dd22018-07-31 14:52:40 +02002374
Angel Pons7c49cb82020-03-16 23:17:32 +01002375 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002376 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002377
Angel Pons88521882020-01-05 20:21:20 +01002378 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002379 }
2380
2381 /* XXX: check any measured value ? */
2382
2383 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01002384 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
Angel Pons7c49cb82020-03-16 23:17:32 +01002385 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002386 }
2387
2388 program_timings(ctrl, channel);
2389
2390 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002391 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002392
Angel Ponsffd50152020-11-12 11:03:10 +01002393 iosav_write_read_mpr_sequence(
2394 channel, slotrank, ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002395
Angel Pons7c49cb82020-03-16 23:17:32 +01002396 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002397 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002398
Angel Pons88521882020-01-05 20:21:20 +01002399 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002400 }
2401
2402 /* XXX: check any measured value ? */
2403
2404 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002405 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) =
Angel Pons891f2bc2020-01-10 01:27:28 +01002406 ~MCHBAR32(IOSAV_By_BW_SERROR_ch(channel, lane)) & 0xff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002407 }
2408
2409 fill_pattern0(ctrl, channel, 0, 0xffffffff);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002410 }
2411
Angel Pons0c3936e2020-03-22 12:49:27 +01002412 /*
2413 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2414 * also use a single loop. It would seem that it is a debugging configuration.
2415 */
Angel Pons88521882020-01-05 20:21:20 +01002416 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2417 printram("discover falling edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002418
2419 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2420 err = discover_edges_real(ctrl, channel, slotrank,
Felix Held2bb3cdf2018-07-28 00:23:59 +02002421 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002422 if (err)
2423 return err;
2424 }
2425
Angel Pons88521882020-01-05 20:21:20 +01002426 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2427 printram("discover rising edges:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002428
2429 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2430 err = discover_edges_real(ctrl, channel, slotrank,
2431 rising_edges[channel][slotrank]);
2432 if (err)
2433 return err;
2434 }
2435
Angel Pons88521882020-01-05 20:21:20 +01002436 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002437
2438 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2439 ctrl->timings[channel][slotrank].lanes[lane].falling =
2440 falling_edges[channel][slotrank][lane];
2441 ctrl->timings[channel][slotrank].lanes[lane].rising =
2442 rising_edges[channel][slotrank][lane];
2443 }
2444
2445 FOR_ALL_POPULATED_CHANNELS {
2446 program_timings(ctrl, channel);
2447 }
2448
2449 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002450 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002451 }
2452 return 0;
2453}
2454
Angel Pons7c49cb82020-03-16 23:17:32 +01002455static int discover_edges_write_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002456{
2457 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002458 u32 raw_stats[MAX_EDGE_TIMING + 1];
2459 int stats[MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002460 const int reg3000b24[] = { 0, 0xc, 0x2c };
2461 int lane, i;
2462 int lower[NUM_LANES];
2463 int upper[NUM_LANES];
2464 int pat;
2465
2466 FOR_ALL_LANES {
2467 lower[lane] = 0;
2468 upper[lane] = MAX_EDGE_TIMING;
2469 }
2470
2471 for (i = 0; i < 3; i++) {
Angel Pons88521882020-01-05 20:21:20 +01002472 MCHBAR32(GDCRTRAININGMOD_ch(channel)) = reg3000b24[i] << 24;
Angel Pons7c49cb82020-03-16 23:17:32 +01002473 printram("[%x] = 0x%08x\n", GDCRTRAININGMOD_ch(channel), reg3000b24[i] << 24);
2474
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002475 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2476 fill_pattern5(ctrl, channel, pat);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002477 printram("using pattern %d\n", pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002478
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002479 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2480 FOR_ALL_LANES {
2481 ctrl->timings[channel][slotrank].lanes[lane].
2482 rising = edge;
2483 ctrl->timings[channel][slotrank].lanes[lane].
2484 falling = edge;
2485 }
2486 program_timings(ctrl, channel);
2487
2488 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002489 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2490 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002491 }
Angel Pons88521882020-01-05 20:21:20 +01002492 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002493
Angel Ponsffd50152020-11-12 11:03:10 +01002494 iosav_write_data_write_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002495
Angel Pons7c49cb82020-03-16 23:17:32 +01002496 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002497 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002498
Angel Pons88521882020-01-05 20:21:20 +01002499 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002500 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002501 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002502 }
2503
Angel Pons7c49cb82020-03-16 23:17:32 +01002504 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002505 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002506 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002507
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002508 FOR_ALL_LANES {
2509 struct run rn;
2510 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002511 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2512
2513 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2514
2515 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2516 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2517 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002518 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002519
2520 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2521 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2522
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002523 edges[lane] = (lower[lane] + upper[lane]) / 2;
2524 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002525 printk(BIOS_EMERG, "edge write discovery failed: "
2526 "%d, %d, %d\n", channel, slotrank, lane);
2527
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002528 return MAKE_ERR;
2529 }
2530 }
2531 }
2532 }
2533
Angel Pons88521882020-01-05 20:21:20 +01002534 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002535 printram("CPA\n");
2536 return 0;
2537}
2538
2539int discover_edges_write(ramctr_timing *ctrl)
2540{
2541 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002542 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2543 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002544
Angel Pons7c49cb82020-03-16 23:17:32 +01002545 /*
2546 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2547 * also use a single loop. It would seem that it is a debugging configuration.
2548 */
Angel Pons88521882020-01-05 20:21:20 +01002549 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2550 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002551
2552 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2553 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002554 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002555 if (err)
2556 return err;
2557 }
2558
Angel Pons88521882020-01-05 20:21:20 +01002559 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2560 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002561
2562 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2563 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002564 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002565 if (err)
2566 return err;
2567 }
2568
Angel Pons88521882020-01-05 20:21:20 +01002569 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002570
2571 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2572 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002573 falling_edges[channel][slotrank][lane];
2574
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002575 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002576 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002577 }
2578
2579 FOR_ALL_POPULATED_CHANNELS
2580 program_timings(ctrl, channel);
2581
2582 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002583 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002584 }
2585 return 0;
2586}
2587
2588static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2589{
Angel Pons88521882020-01-05 20:21:20 +01002590 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002591
Angel Ponsffd50152020-11-12 11:03:10 +01002592 iosav_write_aggressive_write_read_sequence(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002593
Angel Pons7c49cb82020-03-16 23:17:32 +01002594 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002595 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002596
Angel Pons88521882020-01-05 20:21:20 +01002597 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002598}
2599
2600int discover_timC_write(ramctr_timing *ctrl)
2601{
Angel Pons7c49cb82020-03-16 23:17:32 +01002602 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002603 int i, pat;
2604
2605 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2606 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2607 int channel, slotrank, lane;
2608
2609 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2610 lower[channel][slotrank][lane] = 0;
2611 upper[channel][slotrank][lane] = MAX_TIMC;
2612 }
2613
Angel Pons88521882020-01-05 20:21:20 +01002614 /*
2615 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2616 * FIXME: This must only be done on Ivy Bridge.
2617 */
2618 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002619 printram("discover timC write:\n");
2620
2621 for (i = 0; i < 3; i++)
2622 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002623
2624 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2625 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2626 ~0x3f000000, rege3c_b24[i] << 24);
2627
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002628 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002629
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002630 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2631 FOR_ALL_POPULATED_RANKS {
2632 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002633 u32 raw_stats[MAX_TIMC + 1];
2634 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002635
2636 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002637 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002638
2639 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002640
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002641 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002642 FOR_ALL_LANES {
2643 ctrl->timings[channel][slotrank]
2644 .lanes[lane].timC = timC;
2645 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002646 program_timings(ctrl, channel);
2647
2648 test_timC_write (ctrl, channel, slotrank);
2649
Angel Pons7c49cb82020-03-16 23:17:32 +01002650 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002651 raw_stats[timC] = MCHBAR32(
2652 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002653 }
2654 FOR_ALL_LANES {
2655 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002656 for (timC = 0; timC < MAX_TIMC; timC++) {
2657 stats[timC] = !!(raw_stats[timC]
2658 & (1 << lane));
2659 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002660
Angel Pons7c49cb82020-03-16 23:17:32 +01002661 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002662 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002663 printk(BIOS_EMERG,
2664 "timC write discovery failed: "
2665 "%d, %d, %d\n", channel,
2666 slotrank, lane);
2667
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002668 return MAKE_ERR;
2669 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002670 printram("timC: %d, %d, %d: "
2671 "0x%02x-0x%02x-0x%02x, "
2672 "0x%02x-0x%02x\n", channel, slotrank,
2673 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002674 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002675 rn.end - ctrl->timC_offset[i]);
2676
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002677 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002678 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002679 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002680
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002681 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002682 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002683 upper[channel][slotrank][lane]);
2684
2685 }
2686 }
2687 }
2688 }
2689
2690 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002691 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002692 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002693 udelay(2);
2694 }
2695
Angel Pons88521882020-01-05 20:21:20 +01002696 /*
2697 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2698 * FIXME: This must only be done on Ivy Bridge.
2699 */
2700 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002701
2702 printram("CPB\n");
2703
2704 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002705 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002706 (lower[channel][slotrank][lane] +
2707 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002708
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002709 ctrl->timings[channel][slotrank].lanes[lane].timC =
2710 (lower[channel][slotrank][lane] +
2711 upper[channel][slotrank][lane]) / 2;
2712 }
2713 FOR_ALL_POPULATED_CHANNELS {
2714 program_timings(ctrl, channel);
2715 }
2716 return 0;
2717}
2718
Angel Pons88521882020-01-05 20:21:20 +01002719void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002720{
2721 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002722 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002723
2724 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2725 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002726 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002727 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002728 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002729 printram("normalize %d, %d, %d: mat %d\n",
2730 channel, slotrank, lane, mat);
2731
Felix Heldef4fe3e2019-12-31 14:15:05 +01002732 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002733 printram("normalize %d, %d, %d: delta %d\n",
2734 channel, slotrank, lane, delta);
2735
Angel Pons88521882020-01-05 20:21:20 +01002736 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002737 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002738 }
2739
2740 FOR_ALL_POPULATED_CHANNELS {
2741 program_timings(ctrl, channel);
2742 }
2743}
2744
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002745int channel_test(ramctr_timing *ctrl)
2746{
2747 int channel, slotrank, lane;
2748
2749 slotrank = 0;
2750 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002751 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002752 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002753 return MAKE_ERR;
2754 }
2755 FOR_ALL_POPULATED_CHANNELS {
2756 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002757 }
2758
2759 for (slotrank = 0; slotrank < 4; slotrank++)
2760 FOR_ALL_CHANNELS
2761 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2762 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002763 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2764 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002765 }
Angel Pons88521882020-01-05 20:21:20 +01002766 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002767
Angel Ponsffd50152020-11-12 11:03:10 +01002768 iosav_write_memory_test_sequence(ctrl, channel, slotrank);
Felix Held9cf1dd22018-07-31 14:52:40 +02002769
Angel Pons7c49cb82020-03-16 23:17:32 +01002770 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002771 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002772
Angel Pons88521882020-01-05 20:21:20 +01002773 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002774 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002775 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002776 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2777 channel, slotrank, lane);
2778 return MAKE_ERR;
2779 }
2780 }
2781 return 0;
2782}
2783
Patrick Rudolphdd662872017-10-28 18:20:11 +02002784void channel_scrub(ramctr_timing *ctrl)
2785{
2786 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002787 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002788
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002789 FOR_ALL_POPULATED_CHANNELS {
2790 wait_for_iosav(channel);
2791 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002792 }
2793
2794 /*
2795 * During runtime the "scrubber" will periodically scan through the memory in the
2796 * physical address space, to identify and fix CRC errors.
2797 * The following loops writes to every DRAM address, setting the ECC bits to the
2798 * correct value. A read from this location will no longer return a CRC error,
2799 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002800 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002801 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2802 * and firmware running in x86_32.
2803 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002804 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2805 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002806 for (bank = 0; bank < 8; bank++) {
2807 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002808
Angel Pons8f0757e2020-11-11 23:03:36 +01002809 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2810 const struct iosav_ssq sequence[] = {
2811 /*
2812 * DRAM command ACT
2813 * Opens the row for writing.
2814 */
2815 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002816 .sp_cmd_ctrl = {
2817 .command = IOSAV_ACT,
2818 .ranksel_ap = 1,
2819 },
2820 .subseq_ctrl = {
2821 .cmd_executions = 1,
2822 .cmd_delay_gap = gap,
2823 .post_ssq_wait = ctrl->tRCD,
2824 .data_direction = SSQ_NA,
2825 },
2826 .sp_cmd_addr = {
2827 .address = row,
2828 .rowbits = 6,
2829 .bank = bank,
2830 .rank = slotrank,
2831 },
2832 .addr_update = {
2833 .inc_addr_1 = 1,
2834 .addr_wrap = 18,
2835 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002836 },
2837 /*
2838 * DRAM command WR
2839 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2840 * bytes.
2841 */
2842 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002843 .sp_cmd_ctrl = {
2844 .command = IOSAV_WR,
2845 .ranksel_ap = 1,
2846 },
2847 .subseq_ctrl = {
2848 .cmd_executions = 129,
2849 .cmd_delay_gap = 4,
2850 .post_ssq_wait = ctrl->tWTR +
2851 ctrl->CWL + 8,
2852 .data_direction = SSQ_WR,
2853 },
2854 .sp_cmd_addr = {
2855 .address = row,
2856 .rowbits = 0,
2857 .bank = bank,
2858 .rank = slotrank,
2859 },
2860 .addr_update = {
2861 .inc_addr_8 = 1,
2862 .addr_wrap = 9,
2863 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002864 },
2865 /*
2866 * DRAM command PRE
2867 * Closes the row.
2868 */
2869 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002870 .sp_cmd_ctrl = {
2871 .command = IOSAV_PRE,
2872 .ranksel_ap = 1,
2873 },
2874 .subseq_ctrl = {
2875 .cmd_executions = 1,
2876 .cmd_delay_gap = 4,
2877 .post_ssq_wait = ctrl->tRP,
2878 .data_direction = SSQ_NA,
2879 },
2880 .sp_cmd_addr = {
2881 .address = 0,
2882 .rowbits = 6,
2883 .bank = bank,
2884 .rank = slotrank,
2885 },
2886 .addr_update = {
2887 .addr_wrap = 18,
2888 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002889 },
2890 };
2891 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002892
2893 /* Execute command queue */
2894 iosav_run_queue(channel, 16, 0);
2895
2896 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002897 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002898 }
2899 }
2900}
2901
Angel Pons88521882020-01-05 20:21:20 +01002902void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002903{
2904 int channel;
2905
Angel Pons7c49cb82020-03-16 23:17:32 +01002906 /* 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 +01002907 static u32 seeds[NUM_CHANNELS][3] = {
2908 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2909 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2910 };
2911 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002912 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~(1 << 28);
Angel Pons7c49cb82020-03-16 23:17:32 +01002913 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2914 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2915 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002916 }
2917}
2918
Angel Pons89ae6b82020-03-21 13:23:32 +01002919void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002920{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002921 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002922 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002923 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002924 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002925 }
2926}
2927
Angel Pons88521882020-01-05 20:21:20 +01002928void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002929{
2930 int channel;
2931
2932 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002933 /* Always drive command bus */
Angel Ponsdc5539f2020-11-12 12:44:25 +01002934 MCHBAR32_OR(TC_RAP_ch(channel), 1 << 29);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002935 }
2936
2937 udelay(1);
2938
2939 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002940 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002941 }
2942}
2943
Angel Pons7c49cb82020-03-16 23:17:32 +01002944void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002945{
2946 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002947
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002948 FOR_ALL_POPULATED_CHANNELS {
2949 u32 b20, b4_8_12;
Angel Pons88521882020-01-05 20:21:20 +01002950 int min_pi = 10000;
2951 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002952
2953 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002954 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2955 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002956 }
2957
Angel Pons7c49cb82020-03-16 23:17:32 +01002958 b20 = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002959
Angel Pons7c49cb82020-03-16 23:17:32 +01002960 b4_8_12 = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 0x3330 : 0x2220;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002961
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002962 dram_odt_stretch(ctrl, channel);
2963
Angel Ponsdc5539f2020-11-12 12:44:25 +01002964 MCHBAR32(TC_RWP_ch(channel)) = (1 << 27) | (2 << 24) | (b20 << 20) |
Felix Held2463aa92018-07-29 21:37:55 +02002965 ((ctrl->ref_card_offset[channel] + 2) << 16) | b4_8_12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002966 }
2967}
2968
Angel Pons88521882020-01-05 20:21:20 +01002969void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002970{
2971 int channel;
2972 FOR_ALL_POPULATED_CHANNELS {
Angel Ponsdc5539f2020-11-12 12:44:25 +01002973 MCHBAR32(MC_INIT_STATE_ch(channel)) = (1 << 12) | ctrl->rankmap[channel];
2974 MCHBAR32_AND(TC_RAP_ch(channel), ~(1 << 29));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002975 }
2976}
2977
Angel Pons7c49cb82020-03-16 23:17:32 +01002978/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2979static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002980{
Angel Pons88521882020-01-05 20:21:20 +01002981 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002982}
2983
Angel Pons7c49cb82020-03-16 23:17:32 +01002984/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002985void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002986{
Angel Ponsb50ca572020-11-11 19:07:20 +01002987 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01002988
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002989 int channel;
2990 int t1_cycles = 0, t1_ns = 0, t2_ns;
2991 int t3_ns;
2992 u32 r32;
2993
Angel Pons7c49cb82020-03-16 23:17:32 +01002994 /* FIXME: This register only exists on Ivy Bridge */
2995 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002996
Felix Heldf9b826a2018-07-30 17:56:52 +02002997 FOR_ALL_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01002998 MCHBAR32_AND_OR(TC_OTHP_ch(channel), ~(3 << 12), 1 << 12);
Patrick Rudolph652c4912017-10-31 11:36:55 +01002999
Patrick Rudolph74203de2017-11-20 11:57:01 +01003000 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003001 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003002 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003003 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003004 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003005 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003006
Felix Heldf9b826a2018-07-30 17:56:52 +02003007 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003008 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003009
Angel Pons88521882020-01-05 20:21:20 +01003010 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3011 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003012
3013 FOR_ALL_CHANNELS {
3014 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003015 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003016 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003017 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003018 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003019 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003020 case 1:
3021 case 4:
3022 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003023 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003024 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003025 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003026 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003027 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003028 break;
3029 }
3030 }
3031
Felix Held50b7ed22019-12-30 20:41:54 +01003032 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003033 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003034 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003035
3036 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003037 MCHBAR32_AND_OR(TC_RFP_ch(channel), ~(3 << 16), 1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003038
Angel Ponsdc5539f2020-11-12 12:44:25 +01003039 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 0);
3040 MCHBAR32_OR(MC_INIT_STATE_G, 1 << 7);
Angel Pons88521882020-01-05 20:21:20 +01003041 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003042
Angel Pons7c49cb82020-03-16 23:17:32 +01003043 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003044 FOR_ALL_POPULATED_CHANNELS
3045 break;
3046
Angel Pons88521882020-01-05 20:21:20 +01003047 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3048 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003049 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003050 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003051 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003052 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003053 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003054 t1_ns += 500;
3055
Angel Pons88521882020-01-05 20:21:20 +01003056 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003057 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003058 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003059 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003060 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003061 t3_ns = 500;
3062 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003063
3064 /* The graphics driver will use these watermark values */
3065 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003066 MCHBAR32_AND_OR(SSKPD, ~0x3f3f3f3f,
Angel Pons7c49cb82020-03-16 23:17:32 +01003067 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3068 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003069}
3070
Angel Pons88521882020-01-05 20:21:20 +01003071void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003072{
3073 int channel, slotrank, lane;
3074
Angel Pons7c49cb82020-03-16 23:17:32 +01003075 FOR_ALL_POPULATED_CHANNELS {
3076 MCHBAR32(TC_RAP_ch(channel)) =
3077 (ctrl->tRRD << 0)
3078 | (ctrl->tRTP << 4)
3079 | (ctrl->tCKE << 8)
3080 | (ctrl->tWTR << 12)
3081 | (ctrl->tFAW << 16)
3082 | (ctrl->tWR << 24)
3083 | (ctrl->cmd_stretch[channel] << 30);
3084 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003085
3086 udelay(1);
3087
3088 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003089 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003090 }
3091
3092 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003093 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003094 }
3095
3096 FOR_ALL_POPULATED_CHANNELS
Angel Ponsdc5539f2020-11-12 12:44:25 +01003097 MCHBAR32_OR(TC_RWP_ch(channel), 1 << 27);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003098
3099 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003100 udelay(1);
Angel Ponsdc5539f2020-11-12 12:44:25 +01003101 MCHBAR32_OR(SCHED_CBIT_ch(channel), 1 << 21);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003102 }
3103
3104 printram("CPE\n");
3105
Angel Pons88521882020-01-05 20:21:20 +01003106 MCHBAR32(GDCRTRAININGMOD) = 0;
3107 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003108
3109 printram("CP5b\n");
3110
3111 FOR_ALL_POPULATED_CHANNELS {
3112 program_timings(ctrl, channel);
3113 }
3114
3115 u32 reg, addr;
3116
Angel Pons7c49cb82020-03-16 23:17:32 +01003117 /* Poll for RCOMP */
3118 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3119 ;
3120
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003121 do {
Angel Pons88521882020-01-05 20:21:20 +01003122 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003123 } while ((reg & 0x14) == 0);
3124
Angel Pons7c49cb82020-03-16 23:17:32 +01003125 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003126 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003127 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003128
Angel Pons7c49cb82020-03-16 23:17:32 +01003129 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003130 udelay(500);
3131
3132 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003133 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003134 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003135 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003136 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003137 MCHBAR32(addr) = reg;
3138
Angel Pons7c49cb82020-03-16 23:17:32 +01003139 /* Wait 10ns for ranks to settle */
3140 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003141
3142 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3143 MCHBAR32(addr) = reg;
3144
Angel Pons7c49cb82020-03-16 23:17:32 +01003145 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003146 write_reset(ctrl);
3147 }
3148
Angel Pons7c49cb82020-03-16 23:17:32 +01003149 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003150 dram_mrscommands(ctrl);
3151
3152 printram("CP5c\n");
3153
Angel Pons88521882020-01-05 20:21:20 +01003154 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003155
3156 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003157 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003158 udelay(2);
3159 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003160}