blob: 4f0a8da3f7ef47ccbe899785dad6bb6f4cbef74c [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 Pons7c49cb82020-03-16 23:17:32 +010030 MCHBAR32(MC_INIT_STATE_G) = r32 | 0x20;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010031 udelay(1);
Angel Pons88521882020-01-05 20:21:20 +010032 MCHBAR32(MC_INIT_STATE_G) = r32 & ~0x20;
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 Pons7c49cb82020-03-16 23:17:32 +0100133 MCHBAR32_AND_OR(addr, 0xffffc3ff, (stretch << 12) | (stretch << 10));
134 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 Pons7c49cb82020-03-16 23:17:32 +0100137 MCHBAR32_AND_OR(addr, 0xfff0ffff, (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 Pons88521882020-01-05 20:21:20 +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 Pons6a8ddc72020-11-12 01:44:05 +0100536 const struct iosav_ssq sequence[] = ZQCS_SEQUENCE(slotrank, 3, 8, 0);
Angel Pons8f0757e2020-11-11 23:03:36 +0100537 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100538
Angel Pons7c49cb82020-03-16 23:17:32 +0100539 /*
540 * Execute command queue - why is bit 22 set here?!
541 *
542 * This is actually using the IOSAV state machine as a timer, so refresh is allowed.
543 */
Angel Pons38d901e2020-05-02 23:50:43 +0200544 iosav_run_queue(channel, 1, 1);
Felix Held9cf1dd22018-07-31 14:52:40 +0200545
Angel Pons88521882020-01-05 20:21:20 +0100546 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100547}
548
Angel Pons88521882020-01-05 20:21:20 +0100549void dram_jedecreset(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100550{
Felix Held9fe248f2018-07-31 20:59:45 +0200551 u32 reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100552 int channel;
553
Angel Pons7c49cb82020-03-16 23:17:32 +0100554 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
555 ;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100556 do {
Angel Pons88521882020-01-05 20:21:20 +0100557 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100558 } while ((reg & 0x14) == 0);
559
Angel Pons7c49cb82020-03-16 23:17:32 +0100560 /* Set state of memory controller */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100561 reg = 0x112;
Angel Pons88521882020-01-05 20:21:20 +0100562 MCHBAR32(MC_INIT_STATE_G) = reg;
563 MCHBAR32(MC_INIT_STATE) = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100564 reg |= 2; /* DDR reset */
Angel Pons88521882020-01-05 20:21:20 +0100565 MCHBAR32(MC_INIT_STATE_G) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100566
Angel Pons7c49cb82020-03-16 23:17:32 +0100567 /* Assert DIMM reset signal */
568 MCHBAR32_AND(MC_INIT_STATE_G, ~2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100569
Angel Pons7c49cb82020-03-16 23:17:32 +0100570 /* Wait 200us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100571 udelay(200);
572
Angel Pons7c49cb82020-03-16 23:17:32 +0100573 /* Deassert DIMM reset signal */
Angel Pons88521882020-01-05 20:21:20 +0100574 MCHBAR32_OR(MC_INIT_STATE_G, 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100575
Angel Pons7c49cb82020-03-16 23:17:32 +0100576 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100577 udelay(500);
578
Angel Pons7c49cb82020-03-16 23:17:32 +0100579 /* Enable DCLK */
Angel Pons88521882020-01-05 20:21:20 +0100580 MCHBAR32_OR(MC_INIT_STATE_G, 4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100581
Angel Pons7c49cb82020-03-16 23:17:32 +0100582 /* XXX Wait 20ns */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100583 udelay(1);
584
585 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100586 /* Set valid rank CKE */
Felix Held9fe248f2018-07-31 20:59:45 +0200587 reg = ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +0100588 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100589
Angel Pons7c49cb82020-03-16 23:17:32 +0100590 /* Wait 10ns for ranks to settle */
591 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100592
593 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
Angel Pons88521882020-01-05 20:21:20 +0100594 MCHBAR32(MC_INIT_STATE_ch(channel)) = reg;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100595
Angel Pons7c49cb82020-03-16 23:17:32 +0100596 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100597 write_reset(ctrl);
598 }
599}
600
601static odtmap get_ODT(ramctr_timing *ctrl, u8 rank, int channel)
602{
Angel Pons7c49cb82020-03-16 23:17:32 +0100603 /* Get ODT based on rankmap */
604 int dimms_per_ch = (ctrl->rankmap[channel] & 1) + ((ctrl->rankmap[channel] >> 2) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100605
606 if (dimms_per_ch == 1) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100607 return (const odtmap){60, 60};
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100608 } else {
609 return (const odtmap){120, 30};
610 }
611}
612
Angel Pons7c49cb82020-03-16 23:17:32 +0100613static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank, int reg, u32 val)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100614{
Angel Pons88521882020-01-05 20:21:20 +0100615 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100616
617 if (ctrl->rank_mirror[channel][slotrank]) {
618 /* DDR3 Rank1 Address mirror
Angel Pons7c49cb82020-03-16 23:17:32 +0100619 swap the following pins:
620 A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100621 reg = ((reg >> 1) & 1) | ((reg << 1) & 2);
Angel Pons7c49cb82020-03-16 23:17:32 +0100622 val = (val & ~0x1f8) | ((val >> 1) & 0xa8) | ((val & 0xa8) << 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100623 }
624
Angel Pons8f0757e2020-11-11 23:03:36 +0100625 const struct iosav_ssq sequence[] = {
626 /* DRAM command MRS */
627 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200628 .sp_cmd_ctrl = {
629 .command = IOSAV_MRS,
630 },
631 .subseq_ctrl = {
632 .cmd_executions = 1,
633 .cmd_delay_gap = 4,
634 .post_ssq_wait = 4,
635 .data_direction = SSQ_NA,
636 },
637 .sp_cmd_addr = {
638 .address = val,
639 .rowbits = 6,
640 .bank = reg,
641 .rank = slotrank,
642 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100643 },
644 /* DRAM command MRS */
645 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200646 .sp_cmd_ctrl = {
647 .command = IOSAV_MRS,
648 .ranksel_ap = 1,
649 },
650 .subseq_ctrl = {
651 .cmd_executions = 1,
652 .cmd_delay_gap = 4,
653 .post_ssq_wait = 4,
654 .data_direction = SSQ_NA,
655 },
656 .sp_cmd_addr = {
657 .address = val,
658 .rowbits = 6,
659 .bank = reg,
660 .rank = slotrank,
661 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100662 },
663 /* DRAM command MRS */
664 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200665 .sp_cmd_ctrl = {
666 .command = IOSAV_MRS,
667 },
668 .subseq_ctrl = {
669 .cmd_executions = 1,
670 .cmd_delay_gap = 4,
671 .post_ssq_wait = ctrl->tMOD,
672 .data_direction = SSQ_NA,
673 },
674 .sp_cmd_addr = {
675 .address = val,
676 .rowbits = 6,
677 .bank = reg,
678 .rank = slotrank,
679 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100680 },
681 };
682 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200683
Angel Pons7c49cb82020-03-16 23:17:32 +0100684 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200685 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100686}
687
Angel Pons88521882020-01-05 20:21:20 +0100688static u32 make_mr0(ramctr_timing *ctrl, u8 rank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100689{
690 u16 mr0reg, mch_cas, mch_wr;
691 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 +0100692 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100693
694 /* DLL Reset - self clearing - set after CLK frequency has been changed */
695 mr0reg = 0x100;
696
Angel Pons7c49cb82020-03-16 23:17:32 +0100697 /* Convert CAS to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100698 if (ctrl->CAS < 12) {
699 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
700 } else {
701 mch_cas = (u16) (ctrl->CAS - 12);
702 mch_cas = ((mch_cas << 1) | 0x1);
703 }
704
Angel Pons7c49cb82020-03-16 23:17:32 +0100705 /* Convert tWR to MCH register friendly */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100706 mch_wr = mch_wr_t[ctrl->tWR - 5];
707
Angel Pons7c49cb82020-03-16 23:17:32 +0100708 mr0reg = (mr0reg & ~0x0004) | ((mch_cas & 0x1) << 2);
709 mr0reg = (mr0reg & ~0x0070) | ((mch_cas & 0xe) << 3);
710 mr0reg = (mr0reg & ~0x0e00) | (mch_wr << 9);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100711
Angel Pons7c49cb82020-03-16 23:17:32 +0100712 /* Precharge PD - Fast (desktop) 1 or slow (mobile) 0 - mostly power-saving feature */
713 mr0reg = (mr0reg & ~(1 << 12)) | (!is_mobile << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100714 return mr0reg;
715}
716
717static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
718{
Felix Held2bb3cdf2018-07-28 00:23:59 +0200719 write_mrreg(ctrl, channel, rank, 0, make_mr0(ctrl, rank));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100720}
721
722static u32 encode_odt(u32 odt)
723{
724 switch (odt) {
725 case 30:
726 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
727 case 60:
728 return (1 << 2); // RZQ/4
729 case 120:
730 return (1 << 6); // RZQ/2
731 default:
732 case 0:
733 return 0;
734 }
735}
736
737static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
738{
739 odtmap odt;
740 u32 mr1reg;
741
742 odt = get_ODT(ctrl, rank, channel);
Angel Pons7c49cb82020-03-16 23:17:32 +0100743 mr1reg = 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100744
745 mr1reg |= encode_odt(odt.rttnom);
746
747 return mr1reg;
748}
749
750static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
751{
752 u16 mr1reg;
753
754 mr1reg = make_mr1(ctrl, rank, channel);
755
756 write_mrreg(ctrl, channel, rank, 1, mr1reg);
757}
758
759static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
760{
761 u16 pasr, cwl, mr2reg;
762 odtmap odt;
763 int srt;
764
765 pasr = 0;
766 cwl = ctrl->CWL - 5;
767 odt = get_ODT(ctrl, rank, channel);
768
769 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
770
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);
779}
780
781static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
782{
783 write_mrreg(ctrl, channel, rank, 3, 0);
784}
785
Angel Pons88521882020-01-05 20:21:20 +0100786void dram_mrscommands(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100787{
788 u8 slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100789 int channel;
790
791 FOR_ALL_POPULATED_CHANNELS {
792 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100793 /* MR2 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100794 dram_mr2(ctrl, slotrank, channel);
795
Angel Pons7c49cb82020-03-16 23:17:32 +0100796 /* MR3 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100797 dram_mr3(ctrl, slotrank, channel);
798
Angel Pons7c49cb82020-03-16 23:17:32 +0100799 /* MR1 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100800 dram_mr1(ctrl, slotrank, channel);
801
Angel Pons7c49cb82020-03-16 23:17:32 +0100802 /* MR0 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100803 dram_mr0(ctrl, slotrank, channel);
804 }
805 }
806
Angel Pons8f0757e2020-11-11 23:03:36 +0100807 const struct iosav_ssq zqcl_sequence[] = {
808 /* DRAM command NOP (without ODT nor chip selects) */
809 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200810 .sp_cmd_ctrl = {
811 .command = IOSAV_NOP & ~(0xff << 8),
812 },
813 .subseq_ctrl = {
814 .cmd_executions = 1,
815 .cmd_delay_gap = 4,
816 .post_ssq_wait = 15,
817 .data_direction = SSQ_NA,
818 },
819 .sp_cmd_addr = {
820 .address = 2,
821 .rowbits = 6,
822 .bank = 0,
823 .rank = 0,
824 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100825 },
826 /* DRAM command ZQCL */
827 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +0200828 .sp_cmd_ctrl = {
829 .command = IOSAV_ZQCS,
830 .ranksel_ap = 1,
831 },
832 .subseq_ctrl = {
833 .cmd_executions = 1,
834 .cmd_delay_gap = 4,
835 .post_ssq_wait = 400,
836 .data_direction = SSQ_NA,
837 },
838 .sp_cmd_addr = {
839 .address = 1024,
840 .rowbits = 6,
841 .bank = 0,
842 .rank = 0,
843 },
844 .addr_update = {
845 .inc_rank = 1,
846 .addr_wrap = 20,
847 },
Angel Pons8f0757e2020-11-11 23:03:36 +0100848 },
849 };
850 iosav_write_sequence(BROADCAST_CH, zqcl_sequence, ARRAY_SIZE(zqcl_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100851
Angel Pons7c49cb82020-03-16 23:17:32 +0100852 /* Execute command queue on all channels. Do it four times. */
Angel Pons38d901e2020-05-02 23:50:43 +0200853 iosav_run_queue(BROADCAST_CH, 4, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100854
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100855 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100856 /* Wait for ref drained */
Angel Pons88521882020-01-05 20:21:20 +0100857 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100858 }
859
Angel Pons7c49cb82020-03-16 23:17:32 +0100860 /* Refresh enable */
Angel Pons88521882020-01-05 20:21:20 +0100861 MCHBAR32_OR(MC_INIT_STATE_G, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100862
863 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +0100864 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~0x200000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100865
Angel Pons88521882020-01-05 20:21:20 +0100866 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100867
868 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
869
Angel Pons7c49cb82020-03-16 23:17:32 +0100870 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100871 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100872
Angel Pons6a8ddc72020-11-12 01:44:05 +0100873 const struct iosav_ssq zqcs_sequence[] = ZQCS_SEQUENCE(slotrank, 4, 101, 31);
Angel Pons8f0757e2020-11-11 23:03:36 +0100874 iosav_write_sequence(channel, zqcs_sequence, ARRAY_SIZE(zqcs_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +0200875
Angel Pons7c49cb82020-03-16 23:17:32 +0100876 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +0200877 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100878
Angel Pons7c49cb82020-03-16 23:17:32 +0100879 /* Drain */
Angel Pons88521882020-01-05 20:21:20 +0100880 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100881 }
882}
883
Felix Held3b906032020-01-14 17:05:43 +0100884static const u32 lane_base[] = {
885 LANEBASE_B0, LANEBASE_B1, LANEBASE_B2, LANEBASE_B3,
886 LANEBASE_B4, LANEBASE_B5, LANEBASE_B6, LANEBASE_B7,
887 LANEBASE_ECC
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100888};
889
Angel Pons88521882020-01-05 20:21:20 +0100890void program_timings(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100891{
Angel Pons88521882020-01-05 20:21:20 +0100892 u32 reg32, reg_roundtrip_latency, reg_pi_code, reg_logic_delay, reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100893 int lane;
894 int slotrank, slot;
895 int full_shift = 0;
Angel Pons88521882020-01-05 20:21:20 +0100896 u16 pi_coding_ctrl[NUM_SLOTS];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100897
898 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +0100899 if (full_shift < -ctrl->timings[channel][slotrank].pi_coding)
900 full_shift = -ctrl->timings[channel][slotrank].pi_coding;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100901 }
902
903 for (slot = 0; slot < NUM_SLOTS; slot++)
904 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
905 case 0:
906 default:
Angel Pons88521882020-01-05 20:21:20 +0100907 pi_coding_ctrl[slot] = 0x7f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100908 break;
909 case 1:
Angel Pons88521882020-01-05 20:21:20 +0100910 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100911 ctrl->timings[channel][2 * slot + 0].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100912 break;
913 case 2:
Angel Pons88521882020-01-05 20:21:20 +0100914 pi_coding_ctrl[slot] =
Angel Pons7c49cb82020-03-16 23:17:32 +0100915 ctrl->timings[channel][2 * slot + 1].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100916 break;
917 case 3:
Angel Pons88521882020-01-05 20:21:20 +0100918 pi_coding_ctrl[slot] =
919 (ctrl->timings[channel][2 * slot].pi_coding +
Angel Pons7c49cb82020-03-16 23:17:32 +0100920 ctrl->timings[channel][2 * slot + 1].pi_coding) / 2 + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100921 break;
922 }
923
Angel Pons7c49cb82020-03-16 23:17:32 +0100924 /* Enable CMD XOVER */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100925 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Angel Pons7c49cb82020-03-16 23:17:32 +0100926 reg32 |= (pi_coding_ctrl[0] & 0x3f) << 6;
927 reg32 |= (pi_coding_ctrl[0] & 0x40) << 9;
Angel Pons88521882020-01-05 20:21:20 +0100928 reg32 |= (pi_coding_ctrl[1] & 0x7f) << 18;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100929 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
930
Angel Pons88521882020-01-05 20:21:20 +0100931 MCHBAR32(GDCRCMDPICODING_ch(channel)) = reg32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100932
Angel Pons7c49cb82020-03-16 23:17:32 +0100933 /* Enable CLK XOVER */
Angel Pons88521882020-01-05 20:21:20 +0100934 reg_pi_code = get_XOVER_CLK(ctrl->rankmap[channel]);
935 reg_logic_delay = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100936
937 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100938 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Angel Pons88521882020-01-05 20:21:20 +0100939 int offset_pi_code;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100940 if (shift < 0)
941 shift = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100942
Angel Pons88521882020-01-05 20:21:20 +0100943 offset_pi_code = ctrl->pi_code_offset + shift;
Angel Pons7c49cb82020-03-16 23:17:32 +0100944
945 /* Set CLK phase shift */
Angel Pons88521882020-01-05 20:21:20 +0100946 reg_pi_code |= (offset_pi_code & 0x3f) << (6 * slotrank);
947 reg_logic_delay |= ((offset_pi_code >> 6) & 1) << slotrank;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100948 }
949
Angel Pons88521882020-01-05 20:21:20 +0100950 MCHBAR32(GDCRCKPICODE_ch(channel)) = reg_pi_code;
951 MCHBAR32(GDCRCKLOGICDELAY_ch(channel)) = reg_logic_delay;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100952
Angel Pons88521882020-01-05 20:21:20 +0100953 reg_io_latency = MCHBAR32(SC_IO_LATENCY_ch(channel));
Felix Helddee167e2019-12-30 17:30:16 +0100954 reg_io_latency &= 0xffff0000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100955
Angel Pons88521882020-01-05 20:21:20 +0100956 reg_roundtrip_latency = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100957
958 FOR_ALL_POPULATED_RANKS {
Angel Pons7c49cb82020-03-16 23:17:32 +0100959 int post_timA_min_high = 7, pre_timA_min_high = 7;
960 int post_timA_max_high = 0, pre_timA_max_high = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100961 int shift_402x = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +0100962 int shift = ctrl->timings[channel][slotrank].pi_coding + full_shift;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100963
964 if (shift < 0)
965 shift = 0;
966
967 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +0200968 post_timA_min_high = MIN(post_timA_min_high,
969 (ctrl->timings[channel][slotrank].lanes[lane].
970 timA + shift) >> 6);
971 pre_timA_min_high = MIN(pre_timA_min_high,
972 ctrl->timings[channel][slotrank].lanes[lane].
973 timA >> 6);
974 post_timA_max_high = MAX(post_timA_max_high,
975 (ctrl->timings[channel][slotrank].lanes[lane].
976 timA + shift) >> 6);
977 pre_timA_max_high = MAX(pre_timA_max_high,
978 ctrl->timings[channel][slotrank].lanes[lane].
979 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100980 }
981
982 if (pre_timA_max_high - pre_timA_min_high <
983 post_timA_max_high - post_timA_min_high)
984 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +0100985
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100986 else if (pre_timA_max_high - pre_timA_min_high >
987 post_timA_max_high - post_timA_min_high)
988 shift_402x = -1;
989
Felix Helddee167e2019-12-30 17:30:16 +0100990 reg_io_latency |=
Felix Heldef4fe3e2019-12-31 14:15:05 +0100991 (ctrl->timings[channel][slotrank].io_latency + shift_402x -
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100992 post_timA_min_high) << (4 * slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +0100993
Angel Pons88521882020-01-05 20:21:20 +0100994 reg_roundtrip_latency |=
995 (ctrl->timings[channel][slotrank].roundtrip_latency +
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100996 shift_402x) << (8 * slotrank);
997
998 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +0100999 MCHBAR32(lane_base[lane] + GDCRRX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001000 (((ctrl->timings[channel][slotrank].lanes[lane].
1001 timA + shift) & 0x3f)
1002 |
1003 ((ctrl->timings[channel][slotrank].lanes[lane].
1004 rising + shift) << 8)
1005 |
1006 (((ctrl->timings[channel][slotrank].lanes[lane].
1007 timA + shift -
1008 (post_timA_min_high << 6)) & 0x1c0) << 10)
1009 | ((ctrl->timings[channel][slotrank].lanes[lane].
1010 falling + shift) << 20));
1011
Felix Heldfb19c8a2020-01-14 21:27:59 +01001012 MCHBAR32(lane_base[lane] + GDCRTX(channel, slotrank)) =
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001013 (((ctrl->timings[channel][slotrank].lanes[lane].
1014 timC + shift) & 0x3f)
1015 |
1016 (((ctrl->timings[channel][slotrank].lanes[lane].
1017 timB + shift) & 0x3f) << 8)
1018 |
1019 (((ctrl->timings[channel][slotrank].lanes[lane].
1020 timB + shift) & 0x1c0) << 9)
1021 |
1022 (((ctrl->timings[channel][slotrank].lanes[lane].
1023 timC + shift) & 0x40) << 13));
1024 }
1025 }
Angel Pons88521882020-01-05 20:21:20 +01001026 MCHBAR32(SC_ROUNDT_LAT_ch(channel)) = reg_roundtrip_latency;
1027 MCHBAR32(SC_IO_LATENCY_ch(channel)) = reg_io_latency;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001028}
1029
Angel Pons88521882020-01-05 20:21:20 +01001030static void test_timA(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001031{
Angel Pons88521882020-01-05 20:21:20 +01001032 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001033
Angel Pons6a8ddc72020-11-12 01:44:05 +01001034 const struct iosav_ssq sequence[] =
1035 READ_MPR_SEQUENCE(ctrl->tMOD, 1, 3, 15, ctrl->CAS + 36);
Angel Pons8f0757e2020-11-11 23:03:36 +01001036 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001037
Angel Pons7c49cb82020-03-16 23:17:32 +01001038 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001039 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001040
Angel Pons88521882020-01-05 20:21:20 +01001041 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001042}
1043
Angel Pons7c49cb82020-03-16 23:17:32 +01001044static int does_lane_work(ramctr_timing *ctrl, int channel, int slotrank, int lane)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001045{
1046 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
Angel Pons7c49cb82020-03-16 23:17:32 +01001047
1048 return (MCHBAR32(lane_base[lane] +
1049 GDCRTRAININGRESULT(channel, (timA / 32) & 1)) >> (timA % 32)) & 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001050}
1051
1052struct run {
1053 int middle;
1054 int end;
1055 int start;
1056 int all;
1057 int length;
1058};
1059
1060static struct run get_longest_zero_run(int *seq, int sz)
1061{
1062 int i, ls;
1063 int bl = 0, bs = 0;
1064 struct run ret;
1065
1066 ls = 0;
1067 for (i = 0; i < 2 * sz; i++)
1068 if (seq[i % sz]) {
1069 if (i - ls > bl) {
1070 bl = i - ls;
1071 bs = ls;
1072 }
1073 ls = i + 1;
1074 }
1075 if (bl == 0) {
1076 ret.middle = sz / 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001077 ret.start = 0;
1078 ret.end = sz;
Jacob Garbere0c181d2019-04-08 22:21:43 -06001079 ret.length = sz;
Angel Pons7c49cb82020-03-16 23:17:32 +01001080 ret.all = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001081 return ret;
1082 }
1083
Angel Pons7c49cb82020-03-16 23:17:32 +01001084 ret.start = bs % sz;
1085 ret.end = (bs + bl - 1) % sz;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001086 ret.middle = (bs + (bl - 1) / 2) % sz;
1087 ret.length = bl;
Angel Pons7c49cb82020-03-16 23:17:32 +01001088 ret.all = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001089
1090 return ret;
1091}
1092
Angel Pons7c49cb82020-03-16 23:17:32 +01001093static void discover_timA_coarse(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001094{
1095 int timA;
1096 int statistics[NUM_LANES][128];
1097 int lane;
1098
1099 for (timA = 0; timA < 128; timA++) {
1100 FOR_ALL_LANES {
1101 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1102 }
1103 program_timings(ctrl, channel);
1104
1105 test_timA(ctrl, channel, slotrank);
1106
1107 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001108 statistics[lane][timA] = !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001109 }
1110 }
1111 FOR_ALL_LANES {
1112 struct run rn = get_longest_zero_run(statistics[lane], 128);
1113 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1114 upperA[lane] = rn.end;
1115 if (upperA[lane] < rn.middle)
1116 upperA[lane] += 128;
Angel Pons7c49cb82020-03-16 23:17:32 +01001117
Patrick Rudolph368b6152016-11-25 16:36:52 +01001118 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001119 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001120 }
1121}
1122
Angel Pons7c49cb82020-03-16 23:17:32 +01001123static void discover_timA_fine(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001124{
1125 int timA_delta;
1126 int statistics[NUM_LANES][51];
1127 int lane, i;
1128
1129 memset(statistics, 0, sizeof(statistics));
1130
1131 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01001132
1133 FOR_ALL_LANES {
1134 ctrl->timings[channel][slotrank].lanes[lane].timA
1135 = upperA[lane] + timA_delta + 0x40;
1136 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001137 program_timings(ctrl, channel);
1138
1139 for (i = 0; i < 100; i++) {
1140 test_timA(ctrl, channel, slotrank);
1141 FOR_ALL_LANES {
1142 statistics[lane][timA_delta + 25] +=
Angel Pons7c49cb82020-03-16 23:17:32 +01001143 does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001144 }
1145 }
1146 }
1147 FOR_ALL_LANES {
1148 int last_zero, first_all;
1149
1150 for (last_zero = -25; last_zero <= 25; last_zero++)
1151 if (statistics[lane][last_zero + 25])
1152 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01001153
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001154 last_zero--;
1155 for (first_all = -25; first_all <= 25; first_all++)
1156 if (statistics[lane][first_all + 25] == 100)
1157 break;
1158
Angel Pons7c49cb82020-03-16 23:17:32 +01001159 printram("lane %d: %d, %d\n", lane, last_zero, first_all);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001160
1161 ctrl->timings[channel][slotrank].lanes[lane].timA =
Angel Pons7c49cb82020-03-16 23:17:32 +01001162 (last_zero + first_all) / 2 + upperA[lane];
1163
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001164 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01001165 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001166 }
1167}
1168
Angel Pons891f2bc2020-01-10 01:27:28 +01001169static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank, int *upperA)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001170{
1171 int works[NUM_LANES];
1172 int lane;
Angel Pons7c49cb82020-03-16 23:17:32 +01001173
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001174 while (1) {
1175 int all_works = 1, some_works = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001176
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001177 program_timings(ctrl, channel);
1178 test_timA(ctrl, channel, slotrank);
Angel Pons7c49cb82020-03-16 23:17:32 +01001179
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001180 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001181 works[lane] = !does_lane_work(ctrl, channel, slotrank, lane);
1182
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001183 if (works[lane])
1184 some_works = 1;
1185 else
1186 all_works = 0;
1187 }
1188 if (all_works)
1189 return 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001190
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001191 if (!some_works) {
Angel Pons88521882020-01-05 20:21:20 +01001192 if (ctrl->timings[channel][slotrank].roundtrip_latency < 2) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001193 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1194 channel, slotrank);
1195 return MAKE_ERR;
1196 }
Angel Pons88521882020-01-05 20:21:20 +01001197 ctrl->timings[channel][slotrank].roundtrip_latency -= 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001198 printram("4024 -= 2;\n");
1199 continue;
1200 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001201 ctrl->timings[channel][slotrank].io_latency += 2;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001202 printram("4028 += 2;\n");
Angel Pons7c49cb82020-03-16 23:17:32 +01001203
Felix Heldef4fe3e2019-12-31 14:15:05 +01001204 if (ctrl->timings[channel][slotrank].io_latency >= 0x10) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001205 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1206 channel, slotrank);
1207 return MAKE_ERR;
1208 }
1209 FOR_ALL_LANES if (works[lane]) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001210 ctrl->timings[channel][slotrank].lanes[lane].timA += 128;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001211 upperA[lane] += 128;
Angel Pons891f2bc2020-01-10 01:27:28 +01001212 printram("increment %d, %d, %d\n", channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001213 }
1214 }
1215 return 0;
1216}
1217
1218struct timA_minmax {
1219 int timA_min_high, timA_max_high;
1220};
1221
Angel Pons88521882020-01-05 20:21:20 +01001222static void pre_timA_change(ramctr_timing *ctrl, int channel, int slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001223 struct timA_minmax *mnmx)
1224{
1225 int lane;
1226 mnmx->timA_min_high = 7;
1227 mnmx->timA_max_high = 0;
1228
1229 FOR_ALL_LANES {
1230 if (mnmx->timA_min_high >
1231 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1232 mnmx->timA_min_high =
Angel Pons891f2bc2020-01-10 01:27:28 +01001233 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001234 if (mnmx->timA_max_high <
1235 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1236 mnmx->timA_max_high =
Angel Pons891f2bc2020-01-10 01:27:28 +01001237 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001238 }
1239}
1240
Angel Pons88521882020-01-05 20:21:20 +01001241static void post_timA_change(ramctr_timing *ctrl, int channel, int slotrank,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001242 struct timA_minmax *mnmx)
1243{
1244 struct timA_minmax post;
1245 int shift_402x = 0;
1246
Angel Pons7c49cb82020-03-16 23:17:32 +01001247 /* Get changed maxima */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001248 pre_timA_change(ctrl, channel, slotrank, &post);
1249
1250 if (mnmx->timA_max_high - mnmx->timA_min_high <
1251 post.timA_max_high - post.timA_min_high)
1252 shift_402x = +1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001253
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001254 else if (mnmx->timA_max_high - mnmx->timA_min_high >
1255 post.timA_max_high - post.timA_min_high)
1256 shift_402x = -1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001257
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001258 else
1259 shift_402x = 0;
1260
Felix Heldef4fe3e2019-12-31 14:15:05 +01001261 ctrl->timings[channel][slotrank].io_latency += shift_402x;
Angel Pons88521882020-01-05 20:21:20 +01001262 ctrl->timings[channel][slotrank].roundtrip_latency += shift_402x;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001263 printram("4024 += %d;\n", shift_402x);
1264 printram("4028 += %d;\n", shift_402x);
1265}
1266
Angel Pons7c49cb82020-03-16 23:17:32 +01001267/*
1268 * Compensate the skew between DQS and DQs.
1269 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001270 * To ease PCB design, a small skew between Data Strobe signals and Data Signals is allowed.
1271 * The controller has to measure and compensate this skew for every byte-lane. By delaying
Angel Pons7c49cb82020-03-16 23:17:32 +01001272 * either all DQ signals or DQS signal, a full phase shift can be introduced. It is assumed
Angel Pons891f2bc2020-01-10 01:27:28 +01001273 * that one byte-lane's DQs signals have the same routing delay.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001274 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001275 * To measure the actual skew, the DRAM is placed in "read leveling" mode. In read leveling
1276 * mode the DRAM-chip outputs an alternating periodic pattern. The memory controller iterates
1277 * over all possible values to do a full phase shift and issues read commands. With DQS and
Angel Pons7c49cb82020-03-16 23:17:32 +01001278 * DQ in phase the data being read is expected to alternate on every byte:
1279 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001280 * 0xFF 0x00 0xFF ...
Angel Pons7c49cb82020-03-16 23:17:32 +01001281 *
Angel Pons891f2bc2020-01-10 01:27:28 +01001282 * Once the controller has detected this pattern a bit in the result register is set for the
1283 * current phase shift.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001284 */
Angel Pons88521882020-01-05 20:21:20 +01001285int read_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001286{
1287 int channel, slotrank, lane;
1288 int err;
1289
1290 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1291 int all_high, some_high;
1292 int upperA[NUM_LANES];
1293 struct timA_minmax mnmx;
1294
Angel Pons88521882020-01-05 20:21:20 +01001295 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001296
Angel Pons6a8ddc72020-11-12 01:44:05 +01001297 const struct iosav_ssq sequence[] = PREA_SEQUENCE(ctrl->tRP, 0);
Angel Pons8f0757e2020-11-11 23:03:36 +01001298 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001299
Angel Pons7c49cb82020-03-16 23:17:32 +01001300 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001301 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001302
Angel Pons88521882020-01-05 20:21:20 +01001303 MCHBAR32(GDCRTRAININGMOD) = (slotrank << 2) | 0x8001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001304
Felix Heldef4fe3e2019-12-31 14:15:05 +01001305 ctrl->timings[channel][slotrank].io_latency = 4;
Angel Pons88521882020-01-05 20:21:20 +01001306 ctrl->timings[channel][slotrank].roundtrip_latency = 55;
Felix Held2bb3cdf2018-07-28 00:23:59 +02001307 program_timings(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001308
Felix Held2bb3cdf2018-07-28 00:23:59 +02001309 discover_timA_coarse(ctrl, channel, slotrank, upperA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001310
Felix Held2bb3cdf2018-07-28 00:23:59 +02001311 all_high = 1;
1312 some_high = 0;
1313 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001314 if (ctrl->timings[channel][slotrank].lanes[lane].timA >= 0x40)
Felix Held2bb3cdf2018-07-28 00:23:59 +02001315 some_high = 1;
1316 else
1317 all_high = 0;
1318 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001319
1320 if (all_high) {
Felix Heldef4fe3e2019-12-31 14:15:05 +01001321 ctrl->timings[channel][slotrank].io_latency--;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001322 printram("4028--;\n");
1323 FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001324 ctrl->timings[channel][slotrank].lanes[lane].timA -= 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001325 upperA[lane] -= 0x40;
1326
1327 }
1328 } else if (some_high) {
Angel Pons88521882020-01-05 20:21:20 +01001329 ctrl->timings[channel][slotrank].roundtrip_latency++;
Felix Heldef4fe3e2019-12-31 14:15:05 +01001330 ctrl->timings[channel][slotrank].io_latency++;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001331 printram("4024++;\n");
1332 printram("4028++;\n");
1333 }
1334
1335 program_timings(ctrl, channel);
1336
1337 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1338
1339 err = discover_402x(ctrl, channel, slotrank, upperA);
1340 if (err)
1341 return err;
1342
1343 post_timA_change(ctrl, channel, slotrank, &mnmx);
1344 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1345
1346 discover_timA_fine(ctrl, channel, slotrank, upperA);
1347
1348 post_timA_change(ctrl, channel, slotrank, &mnmx);
1349 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1350
1351 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001352 ctrl->timings[channel][slotrank].lanes[lane].timA -=
1353 mnmx.timA_min_high * 0x40;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001354 }
Felix Heldef4fe3e2019-12-31 14:15:05 +01001355 ctrl->timings[channel][slotrank].io_latency -= mnmx.timA_min_high;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001356 printram("4028 -= %d;\n", mnmx.timA_min_high);
1357
1358 post_timA_change(ctrl, channel, slotrank, &mnmx);
1359
1360 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
Angel Pons88521882020-01-05 20:21:20 +01001361 ctrl->timings[channel][slotrank].roundtrip_latency,
Felix Heldef4fe3e2019-12-31 14:15:05 +01001362 ctrl->timings[channel][slotrank].io_latency);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001363
1364 printram("final results:\n");
1365 FOR_ALL_LANES
Angel Pons7c49cb82020-03-16 23:17:32 +01001366 printram("Aval: %d, %d, %d: %x\n", channel, slotrank, lane,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001367 ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001368
Angel Pons88521882020-01-05 20:21:20 +01001369 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001370
1371 toggle_io_reset();
1372 }
1373
1374 FOR_ALL_POPULATED_CHANNELS {
1375 program_timings(ctrl, channel);
1376 }
1377 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001378 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001379 }
1380 return 0;
1381}
1382
Angel Pons88521882020-01-05 20:21:20 +01001383static void test_timC(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001384{
1385 int lane;
1386
1387 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01001388 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
1389 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001390 }
1391
Angel Pons88521882020-01-05 20:21:20 +01001392 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001393
Angel Pons6a8ddc72020-11-12 01:44:05 +01001394 const struct iosav_ssq wr_sequence[] =
1395 MISC_WRITE_SEQUENCE(MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1), 4, 4, 500, 18);
Angel Pons8f0757e2020-11-11 23:03:36 +01001396 iosav_write_sequence(channel, wr_sequence, ARRAY_SIZE(wr_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001397
Angel Pons7c49cb82020-03-16 23:17:32 +01001398 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001399 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001400
Angel Pons88521882020-01-05 20:21:20 +01001401 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001402
Angel Pons8f0757e2020-11-11 23:03:36 +01001403 const struct iosav_ssq rd_sequence[] = {
1404 /* DRAM command PREA */
1405 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001406 .sp_cmd_ctrl = {
1407 .command = IOSAV_PRE,
1408 .ranksel_ap = 1,
1409 },
1410 .subseq_ctrl = {
1411 .cmd_executions = 1,
1412 .cmd_delay_gap = 3,
1413 .post_ssq_wait = ctrl->tRP,
1414 .data_direction = SSQ_NA,
1415 },
1416 .sp_cmd_addr = {
1417 .address = 1024,
1418 .rowbits = 6,
1419 .bank = 0,
1420 .rank = slotrank,
1421 },
1422 .addr_update = {
1423 .addr_wrap = 18,
1424 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001425 },
1426 /* DRAM command ACT */
1427 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001428 .sp_cmd_ctrl = {
1429 .command = IOSAV_ACT,
1430 .ranksel_ap = 1,
1431 },
1432 .subseq_ctrl = {
1433 .cmd_executions = 8,
1434 .cmd_delay_gap = MAX(ctrl->tRRD, (ctrl->tFAW >> 2) + 1),
1435 .post_ssq_wait = ctrl->CAS,
1436 .data_direction = SSQ_NA,
1437 },
1438 .sp_cmd_addr = {
1439 .address = 0,
1440 .rowbits = 6,
1441 .bank = 0,
1442 .rank = slotrank,
1443 },
1444 .addr_update = {
1445 .inc_bank = 1,
1446 .addr_wrap = 18,
1447 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001448 },
1449 /* DRAM command RD */
1450 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001451 .sp_cmd_ctrl = {
1452 .command = IOSAV_RD,
1453 .ranksel_ap = 1,
1454 },
1455 .subseq_ctrl = {
1456 .cmd_executions = 500,
1457 .cmd_delay_gap = 4,
1458 .post_ssq_wait = MAX(ctrl->tRTP, 8),
1459 .data_direction = SSQ_RD,
1460 },
1461 .sp_cmd_addr = {
1462 .address = 0,
1463 .rowbits = 0,
1464 .bank = 0,
1465 .rank = slotrank,
1466 },
1467 .addr_update = {
1468 .inc_addr_8 = 1,
1469 .addr_wrap = 18,
1470 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001471 },
1472 /* DRAM command PREA */
1473 [3] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001474 .sp_cmd_ctrl = {
1475 .command = IOSAV_PRE,
1476 .ranksel_ap = 1,
1477 },
1478 .subseq_ctrl = {
1479 .cmd_executions = 1,
1480 .cmd_delay_gap = 3,
1481 .post_ssq_wait = ctrl->tRP,
1482 .data_direction = SSQ_NA,
1483 },
1484 .sp_cmd_addr = {
1485 .address = 1024,
1486 .rowbits = 6,
1487 .bank = 0,
1488 .rank = slotrank,
1489 },
1490 .addr_update = {
1491 .addr_wrap = 18,
1492 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001493 },
1494 };
1495 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001496
Angel Pons7c49cb82020-03-16 23:17:32 +01001497 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001498 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001499
Angel Pons88521882020-01-05 20:21:20 +01001500 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001501}
1502
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001503static void timC_threshold_process(int *data, const int count)
1504{
1505 int min = data[0];
1506 int max = min;
1507 int i;
1508 for (i = 1; i < count; i++) {
1509 if (min > data[i])
1510 min = data[i];
Angel Pons7c49cb82020-03-16 23:17:32 +01001511
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001512 if (max < data[i])
1513 max = data[i];
1514 }
Angel Pons7c49cb82020-03-16 23:17:32 +01001515 int threshold = min / 2 + max / 2;
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001516 for (i = 0; i < count; i++)
1517 data[i] = data[i] > threshold;
Angel Pons7c49cb82020-03-16 23:17:32 +01001518
Angel Pons891f2bc2020-01-10 01:27:28 +01001519 printram("threshold=%d min=%d max=%d\n", threshold, min, max);
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001520}
1521
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001522static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
1523{
1524 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01001525 int stats[NUM_LANES][MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001526 int lane;
1527
Angel Pons88521882020-01-05 20:21:20 +01001528 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001529
Angel Pons6a8ddc72020-11-12 01:44:05 +01001530 const struct iosav_ssq sequence[] = PREA_SEQUENCE(ctrl->tRP, 18);
Angel Pons8f0757e2020-11-11 23:03:36 +01001531 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001532
Angel Pons7c49cb82020-03-16 23:17:32 +01001533 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001534 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001535
1536 for (timC = 0; timC <= MAX_TIMC; timC++) {
Angel Pons891f2bc2020-01-10 01:27:28 +01001537 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001538 program_timings(ctrl, channel);
1539
1540 test_timC(ctrl, channel, slotrank);
1541
1542 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001543 stats[lane][timC] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001544 }
1545 }
1546 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01001547 struct run rn = get_longest_zero_run(stats[lane], ARRAY_SIZE(stats[lane]));
1548
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001549 if (rn.all || rn.length < 8) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001550 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1551 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001552 /*
1553 * With command training not being done yet, the lane can be erroneous.
1554 * Take the average as reference and try again to find a run.
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001555 */
Angel Pons7c49cb82020-03-16 23:17:32 +01001556 timC_threshold_process(stats[lane], ARRAY_SIZE(stats[lane]));
1557 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) {
1560 printk(BIOS_EMERG, "timC recovery failed\n");
1561 return MAKE_ERR;
1562 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001563 }
Tobias Diedrich87c4f112017-12-07 22:40:20 +01001564 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001565 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
Felix Held2bb3cdf2018-07-28 00:23:59 +02001566 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001567 }
1568 return 0;
1569}
1570
Angel Pons88521882020-01-05 20:21:20 +01001571static int get_precedening_channels(ramctr_timing *ctrl, int target_channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001572{
1573 int channel, ret = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01001574
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001575 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1576 ret++;
Angel Pons7c49cb82020-03-16 23:17:32 +01001577
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001578 return ret;
1579}
1580
Angel Pons765d4652020-11-11 14:44:35 +01001581/* Each cacheline is 64 bits long */
1582static void program_wdb_pattern_length(int channel, const unsigned int num_cachelines)
1583{
1584 MCHBAR8(IOSAV_DATA_CTL_ch(channel)) = num_cachelines / 8 - 1;
1585}
1586
Angel Pons88521882020-01-05 20:21:20 +01001587static void fill_pattern0(ramctr_timing *ctrl, int channel, u32 a, u32 b)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001588{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301589 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001590 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Angel Pons7c49cb82020-03-16 23:17:32 +01001591
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001592 for (j = 0; j < 16; j++)
1593 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
Angel Pons7c49cb82020-03-16 23:17:32 +01001594
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001595 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001596
1597 program_wdb_pattern_length(channel, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001598}
1599
Angel Pons88521882020-01-05 20:21:20 +01001600static int num_of_channels(const ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001601{
1602 int ret = 0;
1603 int channel;
1604 FOR_ALL_POPULATED_CHANNELS ret++;
1605 return ret;
1606}
1607
Angel Pons88521882020-01-05 20:21:20 +01001608static void fill_pattern1(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001609{
Subrata Banikb1434fc2019-03-15 22:20:41 +05301610 unsigned int j;
Angel Pons891f2bc2020-01-10 01:27:28 +01001611 unsigned int channel_offset = get_precedening_channels(ctrl, channel) * 0x40;
Subrata Banikb1434fc2019-03-15 22:20:41 +05301612 unsigned int channel_step = 0x40 * num_of_channels(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01001613
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001614 for (j = 0; j < 16; j++)
1615 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
Angel Pons7c49cb82020-03-16 23:17:32 +01001616
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001617 for (j = 0; j < 16; j++)
1618 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
Angel Pons7c49cb82020-03-16 23:17:32 +01001619
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001620 sfence();
Angel Pons765d4652020-11-11 14:44:35 +01001621
1622 program_wdb_pattern_length(channel, 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001623}
1624
Angel Pons88521882020-01-05 20:21:20 +01001625static void precharge(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001626{
1627 int channel, slotrank, lane;
1628
1629 FOR_ALL_POPULATED_CHANNELS {
1630 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001631 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
1632 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001633 }
1634
1635 program_timings(ctrl, channel);
1636
1637 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001638 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001639
Angel Pons6a8ddc72020-11-12 01:44:05 +01001640 const struct iosav_ssq sequence[] =
1641 READ_MPR_SEQUENCE(ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Angel Pons8f0757e2020-11-11 23:03:36 +01001642 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02001643
Angel Pons7c49cb82020-03-16 23:17:32 +01001644 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001645 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001646
Angel Pons88521882020-01-05 20:21:20 +01001647 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001648 }
1649
1650 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01001651 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
1652 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001653 }
1654
1655 program_timings(ctrl, channel);
1656
1657 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01001658 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02001659
Angel Pons6a8ddc72020-11-12 01:44:05 +01001660 const struct iosav_ssq sequence[] =
1661 READ_MPR_SEQUENCE(ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Angel Pons8f0757e2020-11-11 23:03:36 +01001662 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001663
Angel Pons7c49cb82020-03-16 23:17:32 +01001664 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001665 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001666
Angel Pons88521882020-01-05 20:21:20 +01001667 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001668 }
1669 }
1670}
1671
Angel Pons88521882020-01-05 20:21:20 +01001672static void test_timB(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001673{
1674 /* enable DQs on this slotrank */
Angel Pons891f2bc2020-01-10 01:27:28 +01001675 write_mrreg(ctrl, channel, slotrank, 1, 0x80 | make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001676
Angel Pons88521882020-01-05 20:21:20 +01001677 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01001678
1679 const struct iosav_ssq sequence[] = {
1680 /* DRAM command NOP */
1681 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001682 .sp_cmd_ctrl = {
1683 .command = IOSAV_NOP,
1684 .ranksel_ap = 1,
1685 },
1686 .subseq_ctrl = {
1687 .cmd_executions = 1,
1688 .cmd_delay_gap = 3,
1689 .post_ssq_wait = ctrl->CWL + ctrl->tWLO,
1690 .data_direction = SSQ_WR,
1691 },
1692 .sp_cmd_addr = {
1693 .address = 8,
1694 .rowbits = 0,
1695 .bank = 0,
1696 .rank = slotrank,
1697 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001698 },
1699 /* DRAM command NOP */
1700 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001701 .sp_cmd_ctrl = {
1702 .command = IOSAV_NOP_ALT,
1703 .ranksel_ap = 1,
1704 },
1705 .subseq_ctrl = {
1706 .cmd_executions = 1,
1707 .cmd_delay_gap = 3,
1708 .post_ssq_wait = ctrl->CAS + 38,
1709 .data_direction = SSQ_RD,
1710 },
1711 .sp_cmd_addr = {
1712 .address = 4,
1713 .rowbits = 0,
1714 .bank = 0,
1715 .rank = slotrank,
1716 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001717 },
1718 };
1719 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001720
Angel Pons7c49cb82020-03-16 23:17:32 +01001721 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001722 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001723
Angel Pons88521882020-01-05 20:21:20 +01001724 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001725
1726 /* disable DQs on this slotrank */
Angel Pons891f2bc2020-01-10 01:27:28 +01001727 write_mrreg(ctrl, channel, slotrank, 1, 0x1080 | make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001728}
1729
1730static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1731{
1732 int timB;
1733 int statistics[NUM_LANES][128];
1734 int lane;
1735
Angel Pons88521882020-01-05 20:21:20 +01001736 MCHBAR32(GDCRTRAININGMOD) = 0x108052 | (slotrank << 2);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001737
1738 for (timB = 0; timB < 128; timB++) {
1739 FOR_ALL_LANES {
1740 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1741 }
1742 program_timings(ctrl, channel);
1743
1744 test_timB(ctrl, channel, slotrank);
1745
1746 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001747 statistics[lane][timB] = !((MCHBAR32(lane_base[lane] +
1748 GDCRTRAININGRESULT(channel, (timB / 32) & 1)) >>
1749 (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001750 }
1751 }
1752 FOR_ALL_LANES {
1753 struct run rn = get_longest_zero_run(statistics[lane], 128);
Angel Pons7c49cb82020-03-16 23:17:32 +01001754 /*
1755 * timC is a direct function of timB's 6 LSBs. Some tests increments the value
1756 * of timB by a small value, which might cause the 6-bit value to overflow if
1757 * it's close to 0x3f. Increment the value by a small offset if it's likely
1758 * to overflow, to make sure it won't overflow while running tests and bricks
1759 * the system due to a non matching timC.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001760 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001761 * TODO: find out why some tests (edge write discovery) increment timB.
1762 */
1763 if ((rn.start & 0x3f) == 0x3e)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001764 rn.start += 2;
Angel Pons7c49cb82020-03-16 23:17:32 +01001765 else if ((rn.start & 0x3f) == 0x3f)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001766 rn.start += 1;
Angel Pons7c49cb82020-03-16 23:17:32 +01001767
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001768 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1769 if (rn.all) {
1770 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1771 channel, slotrank, lane);
Angel Pons7c49cb82020-03-16 23:17:32 +01001772
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001773 return MAKE_ERR;
1774 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001775 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1776 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001777 }
1778 return 0;
1779}
1780
1781static int get_timB_high_adjust(u64 val)
1782{
1783 int i;
1784
Angel Ponsbf13ef02020-11-11 18:40:06 +01001785 /* DQS is good enough */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001786 if (val == 0xffffffffffffffffLL)
1787 return 0;
1788
1789 if (val >= 0xf000000000000000LL) {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001790 /* DQS is late, needs negative adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001791 for (i = 0; i < 8; i++)
1792 if (val << (8 * (7 - i) + 4))
1793 return -i;
1794 } else {
Angel Ponsbf13ef02020-11-11 18:40:06 +01001795 /* DQS is early, needs positive adjustment */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001796 for (i = 0; i < 8; i++)
1797 if (val >> (8 * (7 - i) + 4))
1798 return i;
1799 }
1800 return 8;
1801}
1802
Angel Ponsbf13ef02020-11-11 18:40:06 +01001803static void train_write_flyby(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001804{
1805 int channel, slotrank, lane, old;
Angel Pons88521882020-01-05 20:21:20 +01001806 MCHBAR32(GDCRTRAININGMOD) = 0x200;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001807 FOR_ALL_POPULATED_CHANNELS {
1808 fill_pattern1(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001809 }
1810 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1811
Angel Pons765d4652020-11-11 14:44:35 +01001812 /* Reset read and write WDB pointers */
Angel Pons88521882020-01-05 20:21:20 +01001813 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x10001;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001814
Angel Pons88521882020-01-05 20:21:20 +01001815 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001816
Angel Pons6a8ddc72020-11-12 01:44:05 +01001817 const struct iosav_ssq wr_sequence[] = MISC_WRITE_SEQUENCE(3, 1, 3, 3, 31);
Angel Pons8f0757e2020-11-11 23:03:36 +01001818 iosav_write_sequence(channel, wr_sequence, ARRAY_SIZE(wr_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001819
Angel Pons7c49cb82020-03-16 23:17:32 +01001820 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001821 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001822
Angel Pons88521882020-01-05 20:21:20 +01001823 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001824
Angel Pons8f0757e2020-11-11 23:03:36 +01001825 const struct iosav_ssq rd_sequence[] = {
1826 /* DRAM command PREA */
1827 [0] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001828 .sp_cmd_ctrl = {
1829 .command = IOSAV_PRE,
1830 .ranksel_ap = 1,
1831 },
1832 .subseq_ctrl = {
1833 .cmd_executions = 1,
1834 .cmd_delay_gap = 3,
1835 .post_ssq_wait = ctrl->tRP,
1836 .data_direction = SSQ_NA,
1837 },
1838 .sp_cmd_addr = {
1839 .address = 1024,
1840 .rowbits = 6,
1841 .bank = 0,
1842 .rank = slotrank,
1843 },
1844 .addr_update = {
1845 .addr_wrap = 18,
1846 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001847 },
1848 /* DRAM command ACT */
1849 [1] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001850 .sp_cmd_ctrl = {
1851 .command = IOSAV_ACT,
1852 .ranksel_ap = 1,
1853 },
1854 .subseq_ctrl = {
1855 .cmd_executions = 1,
1856 .cmd_delay_gap = 3,
1857 .post_ssq_wait = ctrl->tRCD,
1858 .data_direction = SSQ_NA,
1859 },
1860 .sp_cmd_addr = {
1861 .address = 0,
1862 .rowbits = 6,
1863 .bank = 0,
1864 .rank = slotrank,
1865 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001866 },
1867 /* DRAM command RD */
1868 [2] = {
Angel Pons3abd2062020-05-03 00:25:02 +02001869 .sp_cmd_ctrl = {
1870 .command = IOSAV_RD,
1871 .ranksel_ap = 3,
1872 },
1873 .subseq_ctrl = {
1874 .cmd_executions = 1,
1875 .cmd_delay_gap = 3,
1876 .post_ssq_wait = ctrl->tRP +
Angel Ponsca00dec2020-05-02 15:04:00 +02001877 ctrl->timings[channel][slotrank].roundtrip_latency +
Angel Pons3abd2062020-05-03 00:25:02 +02001878 ctrl->timings[channel][slotrank].io_latency,
1879 .data_direction = SSQ_RD,
1880 },
1881 .sp_cmd_addr = {
1882 .address = 8,
1883 .rowbits = 6,
1884 .bank = 0,
1885 .rank = slotrank,
1886 },
Angel Pons8f0757e2020-11-11 23:03:36 +01001887 },
1888 };
1889 iosav_write_sequence(channel, rd_sequence, ARRAY_SIZE(rd_sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001890
Angel Pons7c49cb82020-03-16 23:17:32 +01001891 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001892 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001893
Angel Pons88521882020-01-05 20:21:20 +01001894 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001895 FOR_ALL_LANES {
Felix Heldfb19c8a2020-01-14 21:27:59 +01001896 u64 res = MCHBAR32(lane_base[lane] + GDCRTRAININGRESULT1(channel));
Felix Held283b44662020-01-14 21:14:42 +01001897 res |= ((u64) MCHBAR32(lane_base[lane] +
Felix Heldfb19c8a2020-01-14 21:27:59 +01001898 GDCRTRAININGRESULT2(channel))) << 32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001899 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1900 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1901 get_timB_high_adjust(res) * 64;
1902
1903 printram("High adjust %d:%016llx\n", lane, res);
Angel Pons891f2bc2020-01-10 01:27:28 +01001904 printram("Bval+: %d, %d, %d, %x -> %x\n", channel, slotrank, lane,
1905 old, ctrl->timings[channel][slotrank].lanes[lane].timB);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001906 }
1907 }
Angel Pons88521882020-01-05 20:21:20 +01001908 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001909}
1910
Angel Pons88521882020-01-05 20:21:20 +01001911static void write_op(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001912{
1913 int slotrank;
1914
Angel Pons88521882020-01-05 20:21:20 +01001915 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001916
1917 /* choose an existing rank. */
1918 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
1919
Angel Pons6a8ddc72020-11-12 01:44:05 +01001920 const struct iosav_ssq sequence[] = ZQCS_SEQUENCE(slotrank, 4, 4, 31);
Angel Pons8f0757e2020-11-11 23:03:36 +01001921 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001922
Angel Pons7c49cb82020-03-16 23:17:32 +01001923 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001924 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02001925
Angel Pons88521882020-01-05 20:21:20 +01001926 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001927}
1928
Angel Pons7c49cb82020-03-16 23:17:32 +01001929/*
1930 * Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001931 *
Angel Pons7c49cb82020-03-16 23:17:32 +01001932 * Since DDR3 uses a fly-by topology, the data and strobes signals reach the chips at different
1933 * times with respect to command, address and clock signals. By delaying either all DQ/DQS or
1934 * all CMD/ADDR/CLK signals, a full phase shift can be introduced. It is assumed that the
1935 * CLK/ADDR/CMD signals have the same routing delay.
1936 *
1937 * To find the required phase shift the DRAM is placed in "write leveling" mode. In this mode,
1938 * the DRAM-chip samples the CLK on every DQS edge and feeds back the sampled value on the data
1939 * lanes (DQ).
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001940 */
Angel Pons88521882020-01-05 20:21:20 +01001941int write_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001942{
1943 int channel, slotrank, lane;
1944 int err;
1945
1946 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001947 MCHBAR32_OR(TC_RWP_ch(channel), 0x8000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001948
1949 FOR_ALL_POPULATED_CHANNELS {
1950 write_op(ctrl, channel);
Angel Pons88521882020-01-05 20:21:20 +01001951 MCHBAR32_OR(SCHED_CBIT_ch(channel), 0x200000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001952 }
1953
Angel Pons7c49cb82020-03-16 23:17:32 +01001954 /* Refresh disable */
Angel Pons88521882020-01-05 20:21:20 +01001955 MCHBAR32_AND(MC_INIT_STATE_G, ~8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001956 FOR_ALL_POPULATED_CHANNELS {
1957 write_op(ctrl, channel);
1958 }
1959
Angel Pons7c49cb82020-03-16 23:17:32 +01001960 /* Enable write leveling on all ranks
1961 Disable all DQ outputs
1962 Only NOP is allowed in this mode */
1963 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
1964 write_mrreg(ctrl, channel, slotrank, 1,
Felix Held2bb3cdf2018-07-28 00:23:59 +02001965 make_mr1(ctrl, slotrank, channel) | 0x1080);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001966
Angel Pons88521882020-01-05 20:21:20 +01001967 MCHBAR32(GDCRTRAININGMOD) = 0x108052;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001968
1969 toggle_io_reset();
1970
Angel Pons7c49cb82020-03-16 23:17:32 +01001971 /* Set any valid value for timB, it gets corrected later */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001972 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1973 err = discover_timB(ctrl, channel, slotrank);
1974 if (err)
1975 return err;
1976 }
1977
Angel Pons7c49cb82020-03-16 23:17:32 +01001978 /* Disable write leveling on all ranks */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001979 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
Angel Pons7c49cb82020-03-16 23:17:32 +01001980 write_mrreg(ctrl, channel, slotrank, 1, make_mr1(ctrl, slotrank, channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001981
Angel Pons88521882020-01-05 20:21:20 +01001982 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001983
1984 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01001985 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001986
Angel Pons7c49cb82020-03-16 23:17:32 +01001987 /* Refresh enable */
Angel Pons88521882020-01-05 20:21:20 +01001988 MCHBAR32_OR(MC_INIT_STATE_G, 8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001989
1990 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01001991 MCHBAR32_AND(SCHED_CBIT_ch(channel), ~0x00200000);
1992 MCHBAR32(IOSAV_STATUS_ch(channel));
1993 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001994
Angel Pons6a8ddc72020-11-12 01:44:05 +01001995 const struct iosav_ssq sequence[] = ZQCS_SEQUENCE(0, 4, 101, 31);
Angel Pons8f0757e2020-11-11 23:03:36 +01001996 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001997
Angel Pons7c49cb82020-03-16 23:17:32 +01001998 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02001999 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002000
Angel Pons88521882020-01-05 20:21:20 +01002001 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002002 }
2003
2004 toggle_io_reset();
2005
2006 printram("CPE\n");
2007 precharge(ctrl);
2008 printram("CPF\n");
2009
2010 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002011 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002012 }
2013
2014 FOR_ALL_POPULATED_CHANNELS {
2015 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002016 }
2017
2018 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2019 err = discover_timC(ctrl, channel, slotrank);
2020 if (err)
2021 return err;
2022 }
2023
2024 FOR_ALL_POPULATED_CHANNELS
2025 program_timings(ctrl, channel);
2026
2027 /* measure and adjust timB timings */
Angel Ponsbf13ef02020-11-11 18:40:06 +01002028 train_write_flyby(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002029
2030 FOR_ALL_POPULATED_CHANNELS
2031 program_timings(ctrl, channel);
2032
2033 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002034 MCHBAR32_AND(IOSAV_By_BW_MASK_ch(channel, lane), 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002035 }
2036 return 0;
2037}
2038
Angel Ponsbf13ef02020-11-11 18:40:06 +01002039static int test_command_training(ramctr_timing *ctrl, int channel, int slotrank)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002040{
2041 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2042 int timC_delta;
2043 int lanes_ok = 0;
2044 int ctr = 0;
2045 int lane;
2046
2047 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2048 FOR_ALL_LANES {
2049 ctrl->timings[channel][slotrank].lanes[lane].timC =
2050 saved_rt.lanes[lane].timC + timC_delta;
2051 }
2052 program_timings(ctrl, channel);
2053 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002054 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002055 }
2056
Angel Pons765d4652020-11-11 14:44:35 +01002057 /* Reset read WDB pointer */
Angel Pons88521882020-01-05 20:21:20 +01002058 MCHBAR32(IOSAV_DATA_CTL_ch(channel)) = 0x1f;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002059
Angel Pons88521882020-01-05 20:21:20 +01002060 wait_for_iosav(channel);
Angel Pons8f0757e2020-11-11 23:03:36 +01002061
Angel Pons6a8ddc72020-11-12 01:44:05 +01002062 const struct iosav_ssq sequence[] = COMMAND_TRAINING_SEQUENCE(ctr);
Angel Pons8f0757e2020-11-11 23:03:36 +01002063 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
2064
2065 /* Program LFSR for the RD/WR subsequences */
2066 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 1)) = 0x389abcd;
2067 MCHBAR32(IOSAV_n_ADDRESS_LFSR_ch(channel, 2)) = 0x389abcd;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002068
Angel Pons7c49cb82020-03-16 23:17:32 +01002069 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002070 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002071
Angel Pons88521882020-01-05 20:21:20 +01002072 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002073 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002074 u32 r32 = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002075
2076 if (r32 == 0)
2077 lanes_ok |= 1 << lane;
2078 }
2079 ctr++;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002080 if (lanes_ok == ((1 << ctrl->lanes) - 1))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002081 break;
2082 }
2083
2084 ctrl->timings[channel][slotrank] = saved_rt;
2085
Patrick Rudolphdd662872017-10-28 18:20:11 +02002086 return lanes_ok != ((1 << ctrl->lanes) - 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002087}
2088
Angel Pons88521882020-01-05 20:21:20 +01002089static void fill_pattern5(ramctr_timing *ctrl, int channel, int patno)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002090{
Subrata Banikb1434fc2019-03-15 22:20:41 +05302091 unsigned int i, j;
Angel Pons7c49cb82020-03-16 23:17:32 +01002092 unsigned int offset = get_precedening_channels(ctrl, channel) * 0x40;
2093 unsigned int step = 0x40 * num_of_channels(ctrl);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002094
2095 if (patno) {
2096 u8 base8 = 0x80 >> ((patno - 1) % 8);
2097 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2098 for (i = 0; i < 32; i++) {
2099 for (j = 0; j < 16; j++) {
2100 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01002101
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002102 if (invert[patno - 1][i] & (1 << (j / 2)))
2103 val = ~val;
Angel Pons7c49cb82020-03-16 23:17:32 +01002104
2105 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002106 }
2107 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002108 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002109 for (i = 0; i < ARRAY_SIZE(pattern); i++) {
2110 for (j = 0; j < 16; j++) {
2111 const u32 val = pattern[i][j];
2112 write32((void *)((1 << 26) + offset + i * step + j * 4), val);
2113 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002114 }
2115 sfence();
2116 }
Angel Pons765d4652020-11-11 14:44:35 +01002117
2118 program_wdb_pattern_length(channel, 256);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002119}
2120
Angel Pons88521882020-01-05 20:21:20 +01002121static void reprogram_320c(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002122{
2123 int channel, slotrank;
2124
2125 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002126 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002127
Angel Pons7c49cb82020-03-16 23:17:32 +01002128 /* Choose an existing rank */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002129 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2130
Angel Pons6a8ddc72020-11-12 01:44:05 +01002131 const struct iosav_ssq sequence[] = ZQCS_SEQUENCE(slotrank, 4, 4, 31);
Angel Pons8f0757e2020-11-11 23:03:36 +01002132 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002133
Angel Pons7c49cb82020-03-16 23:17:32 +01002134 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002135 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002136
Angel Pons88521882020-01-05 20:21:20 +01002137 wait_for_iosav(channel);
2138 MCHBAR32_OR(SCHED_CBIT_ch(channel), 0x200000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002139 }
2140
2141 /* refresh disable */
Angel Pons88521882020-01-05 20:21:20 +01002142 MCHBAR32_AND(MC_INIT_STATE_G, ~8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002143 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002144 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002145
2146 /* choose an existing rank. */
2147 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2148
Angel Pons6a8ddc72020-11-12 01:44:05 +01002149 const struct iosav_ssq sequence[] = ZQCS_SEQUENCE(slotrank, 4, 4, 31);
Angel Pons8f0757e2020-11-11 23:03:36 +01002150 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002151
Angel Pons7c49cb82020-03-16 23:17:32 +01002152 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002153 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002154
Angel Pons88521882020-01-05 20:21:20 +01002155 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002156 }
2157
Angel Pons7c49cb82020-03-16 23:17:32 +01002158 /* JEDEC reset */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002159 dram_jedecreset(ctrl);
Angel Pons7c49cb82020-03-16 23:17:32 +01002160
2161 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002162 dram_mrscommands(ctrl);
2163
2164 toggle_io_reset();
2165}
2166
Angel Ponsbf13ef02020-11-11 18:40:06 +01002167#define CT_MIN_PI -127
2168#define CT_MAX_PI 128
2169#define CT_PI_LENGTH (CT_MAX_PI - CT_MIN_PI + 1)
2170
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002171#define MIN_C320C_LEN 13
2172
2173static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2174{
2175 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2176 int slotrank;
Angel Ponsbf13ef02020-11-11 18:40:06 +01002177 int command_pi;
2178 int stat[NUM_SLOTRANKS][CT_PI_LENGTH];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002179 int delta = 0;
2180
2181 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2182
2183 FOR_ALL_POPULATED_RANKS {
Angel Pons891f2bc2020-01-10 01:27:28 +01002184 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002185 }
2186
2187 ctrl->cmd_stretch[channel] = cmd_stretch;
2188
Angel Pons88521882020-01-05 20:21:20 +01002189 MCHBAR32(TC_RAP_ch(channel)) =
Angel Pons7c49cb82020-03-16 23:17:32 +01002190 (ctrl->tRRD << 0)
2191 | (ctrl->tRTP << 4)
2192 | (ctrl->tCKE << 8)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002193 | (ctrl->tWTR << 12)
2194 | (ctrl->tFAW << 16)
Angel Pons7c49cb82020-03-16 23:17:32 +01002195 | (ctrl->tWR << 24)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002196 | (ctrl->cmd_stretch[channel] << 30);
2197
2198 if (ctrl->cmd_stretch[channel] == 2)
2199 delta = 2;
2200 else if (ctrl->cmd_stretch[channel] == 0)
2201 delta = 4;
2202
2203 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002204 ctrl->timings[channel][slotrank].roundtrip_latency -= delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002205 }
2206
Angel Ponsbf13ef02020-11-11 18:40:06 +01002207 for (command_pi = CT_MIN_PI; command_pi < CT_MAX_PI; command_pi++) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002208 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002209 ctrl->timings[channel][slotrank].pi_coding = command_pi;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002210 }
2211 program_timings(ctrl, channel);
2212 reprogram_320c(ctrl);
2213 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002214 stat[slotrank][command_pi - CT_MIN_PI] =
2215 test_command_training(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002216 }
2217 }
2218 FOR_ALL_POPULATED_RANKS {
Angel Ponsbf13ef02020-11-11 18:40:06 +01002219 struct run rn = get_longest_zero_run(stat[slotrank], CT_PI_LENGTH - 1);
Angel Pons7c49cb82020-03-16 23:17:32 +01002220
Angel Ponsbf13ef02020-11-11 18:40:06 +01002221 ctrl->timings[channel][slotrank].pi_coding = rn.middle + CT_MIN_PI;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002222 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2223 channel, slotrank, rn.start, rn.middle, rn.end);
Angel Pons7c49cb82020-03-16 23:17:32 +01002224
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002225 if (rn.all || rn.length < MIN_C320C_LEN) {
2226 FOR_ALL_POPULATED_RANKS {
2227 ctrl->timings[channel][slotrank] =
2228 saved_timings[channel][slotrank];
2229 }
2230 return MAKE_ERR;
2231 }
2232 }
2233
2234 return 0;
2235}
2236
Angel Pons7c49cb82020-03-16 23:17:32 +01002237/*
2238 * Adjust CMD phase shift and try multiple command rates.
2239 * A command rate of 2T doubles the time needed for address and command decode.
2240 */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002241int command_training(ramctr_timing *ctrl)
2242{
2243 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002244
2245 FOR_ALL_POPULATED_CHANNELS {
2246 fill_pattern5(ctrl, channel, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002247 }
2248
2249 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002250 int cmdrate, err;
2251
2252 /*
2253 * Dual DIMM per channel:
Angel Pons7c49cb82020-03-16 23:17:32 +01002254 * Issue:
2255 * While c320c discovery seems to succeed raminit will fail in write training.
2256 *
2257 * Workaround:
2258 * Skip 1T in dual DIMM mode, that's only supported by a few DIMMs.
2259 * Only try 1T mode for XMP DIMMs that request it in dual DIMM mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002260 *
2261 * Single DIMM per channel:
2262 * Try command rate 1T and 2T
2263 */
2264 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002265 if (ctrl->tCMD)
2266 /* XMP gives the CMD rate in clock ticks, not ns */
2267 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002268
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002269 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002270 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2271
2272 if (!err)
2273 break;
2274 }
2275
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002276 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002277 printk(BIOS_EMERG, "c320c discovery failed\n");
2278 return err;
2279 }
2280
Angel Pons891f2bc2020-01-10 01:27:28 +01002281 printram("Using CMD rate %uT on channel %u\n", cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002282 }
2283
2284 FOR_ALL_POPULATED_CHANNELS
2285 program_timings(ctrl, channel);
2286
2287 reprogram_320c(ctrl);
2288 return 0;
2289}
2290
Angel Pons891f2bc2020-01-10 01:27:28 +01002291static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank, int *edges)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002292{
2293 int edge;
Angel Pons7c49cb82020-03-16 23:17:32 +01002294 int stats[NUM_LANES][MAX_EDGE_TIMING + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002295 int lane;
2296
2297 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2298 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002299 ctrl->timings[channel][slotrank].lanes[lane].rising = edge;
Angel Pons891f2bc2020-01-10 01:27:28 +01002300 ctrl->timings[channel][slotrank].lanes[lane].falling = edge;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002301 }
2302 program_timings(ctrl, channel);
2303
2304 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002305 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane)) = 0;
2306 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002307 }
2308
Angel Pons88521882020-01-05 20:21:20 +01002309 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002310
Angel Pons6a8ddc72020-11-12 01:44:05 +01002311 const struct iosav_ssq sequence[] =
2312 READ_MPR_SEQUENCE(ctrl->tMOD, 500, 4, 1, ctrl->CAS + 8);
Angel Pons8f0757e2020-11-11 23:03:36 +01002313 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002314
Angel Pons7c49cb82020-03-16 23:17:32 +01002315 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002316 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002317
Angel Pons88521882020-01-05 20:21:20 +01002318 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002319
2320 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002321 stats[lane][edge] = MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002322 }
2323 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002324
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002325 FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002326 struct run rn = get_longest_zero_run(stats[lane], MAX_EDGE_TIMING + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002327 edges[lane] = rn.middle;
Angel Pons7c49cb82020-03-16 23:17:32 +01002328
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002329 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002330 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n", channel,
2331 slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002332 return MAKE_ERR;
2333 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002334 printram("eval %d, %d, %d: %02x\n", channel, slotrank, lane, edges[lane]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002335 }
2336 return 0;
2337}
2338
2339int discover_edges(ramctr_timing *ctrl)
2340{
2341 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2342 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2343 int channel, slotrank, lane;
2344 int err;
2345
Angel Pons88521882020-01-05 20:21:20 +01002346 MCHBAR32(GDCRTRAININGMOD) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002347
2348 toggle_io_reset();
2349
2350 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002351 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002352 }
2353
2354 FOR_ALL_POPULATED_CHANNELS {
2355 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002356 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002357 MCHBAR32(IOSAV_By_BW_SERROR_C_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002358 }
2359
2360 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01002361 ctrl->timings[channel][slotrank].lanes[lane].falling = 16;
2362 ctrl->timings[channel][slotrank].lanes[lane].rising = 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002363 }
2364
2365 program_timings(ctrl, channel);
2366
2367 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002368 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002369
Angel Pons6a8ddc72020-11-12 01:44:05 +01002370 const struct iosav_ssq sequence[] =
2371 READ_MPR_SEQUENCE(ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Angel Pons8f0757e2020-11-11 23:03:36 +01002372 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02002373
Angel Pons7c49cb82020-03-16 23:17:32 +01002374 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002375 iosav_run_once(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002376
Angel Pons88521882020-01-05 20:21:20 +01002377 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002378 }
2379
2380 /* XXX: check any measured value ? */
2381
2382 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons891f2bc2020-01-10 01:27:28 +01002383 ctrl->timings[channel][slotrank].lanes[lane].falling = 48;
Angel Pons7c49cb82020-03-16 23:17:32 +01002384 ctrl->timings[channel][slotrank].lanes[lane].rising = 48;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002385 }
2386
2387 program_timings(ctrl, channel);
2388
2389 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002390 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002391
Angel Pons6a8ddc72020-11-12 01:44:05 +01002392 const struct iosav_ssq sequence[] =
2393 READ_MPR_SEQUENCE(ctrl->tMOD, 3, 4, 1, ctrl->CAS + 8);
Angel Pons8f0757e2020-11-11 23:03:36 +01002394 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
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 Pons6a8ddc72020-11-12 01:44:05 +01002494 const struct iosav_ssq sequence[] = WRITE_DATA_SEQUENCE;
Angel Pons8f0757e2020-11-11 23:03:36 +01002495 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002496
Angel Pons7c49cb82020-03-16 23:17:32 +01002497 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002498 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002499
Angel Pons88521882020-01-05 20:21:20 +01002500 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002501 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002502 MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002503 }
2504
Angel Pons7c49cb82020-03-16 23:17:32 +01002505 /* FIXME: This register only exists on Ivy Bridge */
Angel Pons098240eb2020-03-22 12:55:32 +01002506 raw_stats[edge] = MCHBAR32(IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002507 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002508
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002509 FOR_ALL_LANES {
2510 struct run rn;
2511 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
Angel Pons7c49cb82020-03-16 23:17:32 +01002512 stats[edge] = !!(raw_stats[edge] & (1 << lane));
2513
2514 rn = get_longest_zero_run(stats, MAX_EDGE_TIMING + 1);
2515
2516 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, "
2517 "0x%02x-0x%02x\n", channel, slotrank, i, rn.start,
2518 rn.middle, rn.end, rn.start + ctrl->edge_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002519 rn.end - ctrl->edge_offset[i]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002520
2521 lower[lane] = MAX(rn.start + ctrl->edge_offset[i], lower[lane]);
2522 upper[lane] = MIN(rn.end - ctrl->edge_offset[i], upper[lane]);
2523
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002524 edges[lane] = (lower[lane] + upper[lane]) / 2;
2525 if (rn.all || (lower[lane] > upper[lane])) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002526 printk(BIOS_EMERG, "edge write discovery failed: "
2527 "%d, %d, %d\n", channel, slotrank, lane);
2528
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002529 return MAKE_ERR;
2530 }
2531 }
2532 }
2533 }
2534
Angel Pons88521882020-01-05 20:21:20 +01002535 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002536 printram("CPA\n");
2537 return 0;
2538}
2539
2540int discover_edges_write(ramctr_timing *ctrl)
2541{
2542 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
Angel Pons7c49cb82020-03-16 23:17:32 +01002543 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2544 int channel, slotrank, lane, err;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002545
Angel Pons7c49cb82020-03-16 23:17:32 +01002546 /*
2547 * FIXME: Under some conditions, vendor BIOS sets both edges to the same value. It will
2548 * also use a single loop. It would seem that it is a debugging configuration.
2549 */
Angel Pons88521882020-01-05 20:21:20 +01002550 MCHBAR32(IOSAV_DC_MASK) = 0x300;
2551 printram("discover falling edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x300);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002552
2553 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2554 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002555 falling_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002556 if (err)
2557 return err;
2558 }
2559
Angel Pons88521882020-01-05 20:21:20 +01002560 MCHBAR32(IOSAV_DC_MASK) = 0x200;
2561 printram("discover rising edges write:\n[%x] = %x\n", IOSAV_DC_MASK, 0x200);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002562
2563 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2564 err = discover_edges_write_real(ctrl, channel, slotrank,
Angel Pons7c49cb82020-03-16 23:17:32 +01002565 rising_edges[channel][slotrank]);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002566 if (err)
2567 return err;
2568 }
2569
Angel Pons88521882020-01-05 20:21:20 +01002570 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002571
2572 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2573 ctrl->timings[channel][slotrank].lanes[lane].falling =
Angel Pons7c49cb82020-03-16 23:17:32 +01002574 falling_edges[channel][slotrank][lane];
2575
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002576 ctrl->timings[channel][slotrank].lanes[lane].rising =
Angel Pons7c49cb82020-03-16 23:17:32 +01002577 rising_edges[channel][slotrank][lane];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002578 }
2579
2580 FOR_ALL_POPULATED_CHANNELS
2581 program_timings(ctrl, channel);
2582
2583 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002584 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002585 }
2586 return 0;
2587}
2588
2589static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2590{
Angel Pons88521882020-01-05 20:21:20 +01002591 wait_for_iosav(channel);
Angel Pons7c49cb82020-03-16 23:17:32 +01002592
Angel Pons6a8ddc72020-11-12 01:44:05 +01002593 const struct iosav_ssq sequence[] = AGGRESSIVE_WRITE_READ_SEQUENCE;
Angel Pons8f0757e2020-11-11 23:03:36 +01002594 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002595
Angel Pons7c49cb82020-03-16 23:17:32 +01002596 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002597 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002598
Angel Pons88521882020-01-05 20:21:20 +01002599 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002600}
2601
2602int discover_timC_write(ramctr_timing *ctrl)
2603{
Angel Pons7c49cb82020-03-16 23:17:32 +01002604 const u8 rege3c_b24[3] = { 0, 0x0f, 0x2f };
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002605 int i, pat;
2606
2607 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2608 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2609 int channel, slotrank, lane;
2610
2611 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2612 lower[channel][slotrank][lane] = 0;
2613 upper[channel][slotrank][lane] = MAX_TIMC;
2614 }
2615
Angel Pons88521882020-01-05 20:21:20 +01002616 /*
2617 * Enable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2618 * FIXME: This must only be done on Ivy Bridge.
2619 */
2620 MCHBAR32(MCMNTS_SPARE) = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002621 printram("discover timC write:\n");
2622
2623 for (i = 0; i < 3; i++)
2624 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002625
2626 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
2627 MCHBAR32_AND_OR(GDCRCMDDEBUGMUXCFG_Cz_S(channel),
2628 ~0x3f000000, rege3c_b24[i] << 24);
2629
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002630 udelay(2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002631
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002632 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2633 FOR_ALL_POPULATED_RANKS {
2634 int timC;
Angel Pons7c49cb82020-03-16 23:17:32 +01002635 u32 raw_stats[MAX_TIMC + 1];
2636 int stats[MAX_TIMC + 1];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002637
2638 /* Make sure rn.start < rn.end */
Angel Pons7c49cb82020-03-16 23:17:32 +01002639 stats[MAX_TIMC] = 1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002640
2641 fill_pattern5(ctrl, channel, pat);
Angel Pons7c49cb82020-03-16 23:17:32 +01002642
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002643 for (timC = 0; timC < MAX_TIMC; timC++) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002644 FOR_ALL_LANES {
2645 ctrl->timings[channel][slotrank]
2646 .lanes[lane].timC = timC;
2647 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002648 program_timings(ctrl, channel);
2649
2650 test_timC_write (ctrl, channel, slotrank);
2651
Angel Pons7c49cb82020-03-16 23:17:32 +01002652 /* FIXME: Another IVB-only register! */
Angel Pons098240eb2020-03-22 12:55:32 +01002653 raw_stats[timC] = MCHBAR32(
2654 IOSAV_BYTE_SERROR_C_ch(channel));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002655 }
2656 FOR_ALL_LANES {
2657 struct run rn;
Angel Pons7c49cb82020-03-16 23:17:32 +01002658 for (timC = 0; timC < MAX_TIMC; timC++) {
2659 stats[timC] = !!(raw_stats[timC]
2660 & (1 << lane));
2661 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002662
Angel Pons7c49cb82020-03-16 23:17:32 +01002663 rn = get_longest_zero_run(stats, MAX_TIMC + 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002664 if (rn.all) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002665 printk(BIOS_EMERG,
2666 "timC write discovery failed: "
2667 "%d, %d, %d\n", channel,
2668 slotrank, lane);
2669
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002670 return MAKE_ERR;
2671 }
Angel Pons7c49cb82020-03-16 23:17:32 +01002672 printram("timC: %d, %d, %d: "
2673 "0x%02x-0x%02x-0x%02x, "
2674 "0x%02x-0x%02x\n", channel, slotrank,
2675 i, rn.start, rn.middle, rn.end,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002676 rn.start + ctrl->timC_offset[i],
Angel Pons7c49cb82020-03-16 23:17:32 +01002677 rn.end - ctrl->timC_offset[i]);
2678
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002679 lower[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002680 MAX(rn.start + ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002681 lower[channel][slotrank][lane]);
Angel Pons7c49cb82020-03-16 23:17:32 +01002682
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002683 upper[channel][slotrank][lane] =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002684 MIN(rn.end - ctrl->timC_offset[i],
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002685 upper[channel][slotrank][lane]);
2686
2687 }
2688 }
2689 }
2690 }
2691
2692 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002693 /* FIXME: Setting the Write VREF must only be done on Ivy Bridge */
Angel Pons88521882020-01-05 20:21:20 +01002694 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002695 udelay(2);
2696 }
2697
Angel Pons88521882020-01-05 20:21:20 +01002698 /*
2699 * Disable IOSAV_n_SPECIAL_COMMAND_ADDR optimization.
2700 * FIXME: This must only be done on Ivy Bridge.
2701 */
2702 MCHBAR32(MCMNTS_SPARE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002703
2704 printram("CPB\n");
2705
2706 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons7c49cb82020-03-16 23:17:32 +01002707 printram("timC %d, %d, %d: %x\n", channel, slotrank, lane,
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002708 (lower[channel][slotrank][lane] +
2709 upper[channel][slotrank][lane]) / 2);
Angel Pons7c49cb82020-03-16 23:17:32 +01002710
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002711 ctrl->timings[channel][slotrank].lanes[lane].timC =
2712 (lower[channel][slotrank][lane] +
2713 upper[channel][slotrank][lane]) / 2;
2714 }
2715 FOR_ALL_POPULATED_CHANNELS {
2716 program_timings(ctrl, channel);
2717 }
2718 return 0;
2719}
2720
Angel Pons88521882020-01-05 20:21:20 +01002721void normalize_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002722{
2723 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002724 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002725
2726 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2727 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002728 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002729 FOR_ALL_LANES mat =
Elyes HAOUASf97c1c92019-12-03 18:22:06 +01002730 MAX(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01002731 printram("normalize %d, %d, %d: mat %d\n",
2732 channel, slotrank, lane, mat);
2733
Felix Heldef4fe3e2019-12-31 14:15:05 +01002734 delta = (mat >> 6) - ctrl->timings[channel][slotrank].io_latency;
Patrick Rudolph413edc82016-11-25 15:40:07 +01002735 printram("normalize %d, %d, %d: delta %d\n",
2736 channel, slotrank, lane, delta);
2737
Angel Pons88521882020-01-05 20:21:20 +01002738 ctrl->timings[channel][slotrank].roundtrip_latency += delta;
Felix Heldef4fe3e2019-12-31 14:15:05 +01002739 ctrl->timings[channel][slotrank].io_latency += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002740 }
2741
2742 FOR_ALL_POPULATED_CHANNELS {
2743 program_timings(ctrl, channel);
2744 }
2745}
2746
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002747int channel_test(ramctr_timing *ctrl)
2748{
2749 int channel, slotrank, lane;
2750
2751 slotrank = 0;
2752 FOR_ALL_POPULATED_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01002753 if (MCHBAR32(MC_INIT_STATE_ch(channel)) & 0xa000) {
Angel Pons891f2bc2020-01-10 01:27:28 +01002754 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n", channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002755 return MAKE_ERR;
2756 }
2757 FOR_ALL_POPULATED_CHANNELS {
2758 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002759 }
2760
2761 for (slotrank = 0; slotrank < 4; slotrank++)
2762 FOR_ALL_CHANNELS
2763 if (ctrl->rankmap[channel] & (1 << slotrank)) {
2764 FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01002765 MCHBAR32(IOSAV_By_ERROR_COUNT(lane)) = 0;
2766 MCHBAR32(IOSAV_By_BW_SERROR_C(lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002767 }
Angel Pons88521882020-01-05 20:21:20 +01002768 wait_for_iosav(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002769
Angel Pons6a8ddc72020-11-12 01:44:05 +01002770 const struct iosav_ssq sequence[] = MEMORY_TEST_SEQUENCE;
Angel Pons8f0757e2020-11-11 23:03:36 +01002771 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Felix Held9cf1dd22018-07-31 14:52:40 +02002772
Angel Pons7c49cb82020-03-16 23:17:32 +01002773 /* Execute command queue */
Angel Pons38d901e2020-05-02 23:50:43 +02002774 iosav_run_once(channel);
Felix Held9cf1dd22018-07-31 14:52:40 +02002775
Angel Pons88521882020-01-05 20:21:20 +01002776 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002777 FOR_ALL_LANES
Angel Pons88521882020-01-05 20:21:20 +01002778 if (MCHBAR32(IOSAV_By_ERROR_COUNT_ch(channel, lane))) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002779 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
2780 channel, slotrank, lane);
2781 return MAKE_ERR;
2782 }
2783 }
2784 return 0;
2785}
2786
Patrick Rudolphdd662872017-10-28 18:20:11 +02002787void channel_scrub(ramctr_timing *ctrl)
2788{
2789 int channel, slotrank, row, rowsize;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002790 u8 bank;
Patrick Rudolphdd662872017-10-28 18:20:11 +02002791
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002792 FOR_ALL_POPULATED_CHANNELS {
2793 wait_for_iosav(channel);
2794 fill_pattern0(ctrl, channel, 0, 0);
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002795 }
2796
2797 /*
2798 * During runtime the "scrubber" will periodically scan through the memory in the
2799 * physical address space, to identify and fix CRC errors.
2800 * The following loops writes to every DRAM address, setting the ECC bits to the
2801 * correct value. A read from this location will no longer return a CRC error,
2802 * except when a bit has toggled due to external events.
Angel Pons3b9d3e92020-11-11 19:10:39 +01002803 * The same could be achieved by writing to the physical memory map, but it's
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002804 * much more difficult due to SMM remapping, ME stolen memory, GFX stolen memory,
2805 * and firmware running in x86_32.
2806 */
Patrick Rudolphdd662872017-10-28 18:20:11 +02002807 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2808 rowsize = 1 << ctrl->info.dimm[channel][slotrank >> 1].row_bits;
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002809 for (bank = 0; bank < 8; bank++) {
2810 for (row = 0; row < rowsize; row += 16) {
Patrick Rudolphdd662872017-10-28 18:20:11 +02002811
Angel Pons8f0757e2020-11-11 23:03:36 +01002812 u8 gap = MAX((ctrl->tFAW >> 2) + 1, ctrl->tRRD);
2813 const struct iosav_ssq sequence[] = {
2814 /*
2815 * DRAM command ACT
2816 * Opens the row for writing.
2817 */
2818 [0] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002819 .sp_cmd_ctrl = {
2820 .command = IOSAV_ACT,
2821 .ranksel_ap = 1,
2822 },
2823 .subseq_ctrl = {
2824 .cmd_executions = 1,
2825 .cmd_delay_gap = gap,
2826 .post_ssq_wait = ctrl->tRCD,
2827 .data_direction = SSQ_NA,
2828 },
2829 .sp_cmd_addr = {
2830 .address = row,
2831 .rowbits = 6,
2832 .bank = bank,
2833 .rank = slotrank,
2834 },
2835 .addr_update = {
2836 .inc_addr_1 = 1,
2837 .addr_wrap = 18,
2838 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002839 },
2840 /*
2841 * DRAM command WR
2842 * Writes (128 + 1) * 8 (burst length) * 8 (bus width)
2843 * bytes.
2844 */
2845 [1] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002846 .sp_cmd_ctrl = {
2847 .command = IOSAV_WR,
2848 .ranksel_ap = 1,
2849 },
2850 .subseq_ctrl = {
2851 .cmd_executions = 129,
2852 .cmd_delay_gap = 4,
2853 .post_ssq_wait = ctrl->tWTR +
2854 ctrl->CWL + 8,
2855 .data_direction = SSQ_WR,
2856 },
2857 .sp_cmd_addr = {
2858 .address = row,
2859 .rowbits = 0,
2860 .bank = bank,
2861 .rank = slotrank,
2862 },
2863 .addr_update = {
2864 .inc_addr_8 = 1,
2865 .addr_wrap = 9,
2866 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002867 },
2868 /*
2869 * DRAM command PRE
2870 * Closes the row.
2871 */
2872 [2] = {
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002873 .sp_cmd_ctrl = {
2874 .command = IOSAV_PRE,
2875 .ranksel_ap = 1,
2876 },
2877 .subseq_ctrl = {
2878 .cmd_executions = 1,
2879 .cmd_delay_gap = 4,
2880 .post_ssq_wait = ctrl->tRP,
2881 .data_direction = SSQ_NA,
2882 },
2883 .sp_cmd_addr = {
2884 .address = 0,
2885 .rowbits = 6,
2886 .bank = bank,
2887 .rank = slotrank,
2888 },
2889 .addr_update = {
2890 .addr_wrap = 18,
2891 },
Angel Pons8f0757e2020-11-11 23:03:36 +01002892 },
2893 };
2894 iosav_write_sequence(channel, sequence, ARRAY_SIZE(sequence));
Patrick Rudolphb5fa9c82020-05-01 18:35:05 +02002895
2896 /* Execute command queue */
2897 iosav_run_queue(channel, 16, 0);
2898
2899 wait_for_iosav(channel);
Angel Pons3abd2062020-05-03 00:25:02 +02002900 }
Patrick Rudolphdd662872017-10-28 18:20:11 +02002901 }
2902 }
2903}
2904
Angel Pons88521882020-01-05 20:21:20 +01002905void set_scrambling_seed(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002906{
2907 int channel;
2908
Angel Pons7c49cb82020-03-16 23:17:32 +01002909 /* 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 +01002910 static u32 seeds[NUM_CHANNELS][3] = {
2911 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
2912 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
2913 };
2914 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002915 MCHBAR32(SCHED_CBIT_ch(channel)) &= ~0x10000000;
Angel Pons7c49cb82020-03-16 23:17:32 +01002916 MCHBAR32(SCRAMBLING_SEED_1_ch(channel)) = seeds[channel][0];
2917 MCHBAR32(SCRAMBLING_SEED_2_HI_ch(channel)) = seeds[channel][1];
2918 MCHBAR32(SCRAMBLING_SEED_2_LO_ch(channel)) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002919 }
2920}
2921
Angel Pons89ae6b82020-03-21 13:23:32 +01002922void set_wmm_behavior(const u32 cpu)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002923{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002924 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
Angel Pons7c49cb82020-03-16 23:17:32 +01002925 MCHBAR32(SC_WDBWM) = 0x141d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002926 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +01002927 MCHBAR32(SC_WDBWM) = 0x551d1519;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002928 }
2929}
2930
Angel Pons88521882020-01-05 20:21:20 +01002931void prepare_training(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002932{
2933 int channel;
2934
2935 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01002936 /* Always drive command bus */
Angel Pons88521882020-01-05 20:21:20 +01002937 MCHBAR32_OR(TC_RAP_ch(channel), 0x20000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002938 }
2939
2940 udelay(1);
2941
2942 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002943 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002944 }
2945}
2946
Angel Pons7c49cb82020-03-16 23:17:32 +01002947void set_read_write_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002948{
2949 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002950
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002951 FOR_ALL_POPULATED_CHANNELS {
2952 u32 b20, b4_8_12;
Angel Pons88521882020-01-05 20:21:20 +01002953 int min_pi = 10000;
2954 int max_pi = -10000;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002955
2956 FOR_ALL_POPULATED_RANKS {
Angel Pons88521882020-01-05 20:21:20 +01002957 max_pi = MAX(ctrl->timings[channel][slotrank].pi_coding, max_pi);
2958 min_pi = MIN(ctrl->timings[channel][slotrank].pi_coding, min_pi);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002959 }
2960
Angel Pons7c49cb82020-03-16 23:17:32 +01002961 b20 = (max_pi - min_pi > 51) ? 0 : ctrl->ref_card_offset[channel];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002962
Angel Pons7c49cb82020-03-16 23:17:32 +01002963 b4_8_12 = (ctrl->pi_coding_threshold < max_pi - min_pi) ? 0x3330 : 0x2220;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002964
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01002965 dram_odt_stretch(ctrl, channel);
2966
Angel Pons7c49cb82020-03-16 23:17:32 +01002967 MCHBAR32(TC_RWP_ch(channel)) = 0x0a000000 | (b20 << 20) |
Felix Held2463aa92018-07-29 21:37:55 +02002968 ((ctrl->ref_card_offset[channel] + 2) << 16) | b4_8_12;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002969 }
2970}
2971
Angel Pons88521882020-01-05 20:21:20 +01002972void set_normal_operation(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002973{
2974 int channel;
2975 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01002976 MCHBAR32(MC_INIT_STATE_ch(channel)) = 0x00001000 | ctrl->rankmap[channel];
2977 MCHBAR32_AND(TC_RAP_ch(channel), ~0x20000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002978 }
2979}
2980
Angel Pons7c49cb82020-03-16 23:17:32 +01002981/* Encode the watermark latencies in a suitable format for graphics drivers consumption */
2982static int encode_wm(int ns)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002983{
Angel Pons88521882020-01-05 20:21:20 +01002984 return (ns + 499) / 500;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002985}
2986
Angel Pons7c49cb82020-03-16 23:17:32 +01002987/* FIXME: values in this function should be hardware revision-dependent */
Angel Pons88521882020-01-05 20:21:20 +01002988void final_registers(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002989{
Angel Ponsb50ca572020-11-11 19:07:20 +01002990 const bool is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolph74203de2017-11-20 11:57:01 +01002991
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002992 int channel;
2993 int t1_cycles = 0, t1_ns = 0, t2_ns;
2994 int t3_ns;
2995 u32 r32;
2996
Angel Pons7c49cb82020-03-16 23:17:32 +01002997 /* FIXME: This register only exists on Ivy Bridge */
2998 MCHBAR32(WMM_READ_CONFIG) = 0x46;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002999
Felix Heldf9b826a2018-07-30 17:56:52 +02003000 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003001 MCHBAR32_AND_OR(TC_OTHP_ch(channel), 0xffffcfff, 0x1000);
Patrick Rudolph652c4912017-10-31 11:36:55 +01003002
Patrick Rudolph74203de2017-11-20 11:57:01 +01003003 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003004 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003005 MCHBAR32(PM_PDWN_CONFIG) = 0x00000740;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003006 else
Angel Pons7c49cb82020-03-16 23:17:32 +01003007 /* APD - PPD, 64 DCLKs until idle, decision per rank */
Angel Pons2a9a49b2019-12-31 14:24:12 +01003008 MCHBAR32(PM_PDWN_CONFIG) = 0x00000340;
Patrick Rudolph652c4912017-10-31 11:36:55 +01003009
Felix Heldf9b826a2018-07-30 17:56:52 +02003010 FOR_ALL_CHANNELS
Angel Pons88521882020-01-05 20:21:20 +01003011 MCHBAR32(PM_TRML_M_CONFIG_ch(channel)) = 0x00000aaa;
Felix Heldf9b826a2018-07-30 17:56:52 +02003012
Angel Pons88521882020-01-05 20:21:20 +01003013 MCHBAR32(PM_BW_LIMIT_CONFIG) = 0x5f7003ff; // OK
3014 MCHBAR32(PM_DLL_CONFIG) = 0x00073000 | ctrl->mdll_wake_delay; // OK
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003015
3016 FOR_ALL_CHANNELS {
3017 switch (ctrl->rankmap[channel]) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003018 /* Unpopulated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003019 case 0:
Angel Pons88521882020-01-05 20:21:20 +01003020 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003021 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003022 /* Only single-ranked dimms */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003023 case 1:
3024 case 4:
3025 case 5:
Angel Pons7c49cb82020-03-16 23:17:32 +01003026 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x00373131;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003027 break;
Angel Pons7c49cb82020-03-16 23:17:32 +01003028 /* Dual-ranked dimms present */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003029 default:
Angel Pons7c49cb82020-03-16 23:17:32 +01003030 MCHBAR32(PM_CMD_PWR_ch(channel)) = 0x009b6ea1;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003031 break;
3032 }
3033 }
3034
Felix Held50b7ed22019-12-30 20:41:54 +01003035 MCHBAR32(MEM_TRML_ESTIMATION_CONFIG) = 0xca9171e5;
Angel Pons7c49cb82020-03-16 23:17:32 +01003036 MCHBAR32_AND_OR(MEM_TRML_THRESHOLDS_CONFIG, ~0x00ffffff, 0x00e4d5d0);
Felix Held50b7ed22019-12-30 20:41:54 +01003037 MCHBAR32_AND(MEM_TRML_INTERRUPT, ~0x1f);
Felix Heldf9b826a2018-07-30 17:56:52 +02003038
3039 FOR_ALL_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003040 MCHBAR32_AND_OR(TC_RFP_ch(channel), ~(3 << 16), 1 << 16);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003041
Angel Pons88521882020-01-05 20:21:20 +01003042 MCHBAR32_OR(MC_INIT_STATE_G, 1);
3043 MCHBAR32_OR(MC_INIT_STATE_G, 0x80);
3044 MCHBAR32(BANDTIMERS_SNB) = 0xfa;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003045
Angel Pons7c49cb82020-03-16 23:17:32 +01003046 /* Find a populated channel */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003047 FOR_ALL_POPULATED_CHANNELS
3048 break;
3049
Angel Pons88521882020-01-05 20:21:20 +01003050 t1_cycles = (MCHBAR32(TC_ZQCAL_ch(channel)) >> 8) & 0xff;
3051 r32 = MCHBAR32(PM_DLL_CONFIG);
Angel Pons7c49cb82020-03-16 23:17:32 +01003052 if (r32 & (1 << 17))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003053 t1_cycles += (r32 & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003054 t1_cycles += MCHBAR32(TC_SRFTP_ch(channel)) & 0xfff;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003055 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
Angel Pons7c49cb82020-03-16 23:17:32 +01003056 if (!(r32 & (1 << 17)))
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003057 t1_ns += 500;
3058
Angel Pons88521882020-01-05 20:21:20 +01003059 t2_ns = 10 * ((MCHBAR32(SAPMTIMERS) >> 8) & 0xfff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003060 if (MCHBAR32(SAPMCTL) & 8) {
Angel Pons7c49cb82020-03-16 23:17:32 +01003061 t3_ns = 10 * ((MCHBAR32(BANDTIMERS_IVB) >> 8) & 0xfff);
Angel Pons88521882020-01-05 20:21:20 +01003062 t3_ns += 10 * (MCHBAR32(SAPMTIMERS2_IVB) & 0xff);
Angel Pons891f2bc2020-01-10 01:27:28 +01003063 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003064 t3_ns = 500;
3065 }
Angel Pons7c49cb82020-03-16 23:17:32 +01003066
3067 /* The graphics driver will use these watermark values */
3068 printk(BIOS_DEBUG, "t123: %d, %d, %d\n", t1_ns, t2_ns, t3_ns);
3069 MCHBAR32_AND_OR(SSKPD, 0xC0C0C0C0,
3070 ((encode_wm(t1_ns) + encode_wm(t2_ns)) << 16) | (encode_wm(t1_ns) << 8) |
3071 ((encode_wm(t3_ns) + encode_wm(t2_ns) + encode_wm(t1_ns)) << 24) | 0x0c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003072}
3073
Angel Pons88521882020-01-05 20:21:20 +01003074void restore_timings(ramctr_timing *ctrl)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003075{
3076 int channel, slotrank, lane;
3077
Angel Pons7c49cb82020-03-16 23:17:32 +01003078 FOR_ALL_POPULATED_CHANNELS {
3079 MCHBAR32(TC_RAP_ch(channel)) =
3080 (ctrl->tRRD << 0)
3081 | (ctrl->tRTP << 4)
3082 | (ctrl->tCKE << 8)
3083 | (ctrl->tWTR << 12)
3084 | (ctrl->tFAW << 16)
3085 | (ctrl->tWR << 24)
3086 | (ctrl->cmd_stretch[channel] << 30);
3087 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003088
3089 udelay(1);
3090
3091 FOR_ALL_POPULATED_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003092 wait_for_iosav(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003093 }
3094
3095 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Angel Pons88521882020-01-05 20:21:20 +01003096 MCHBAR32(IOSAV_By_BW_MASK_ch(channel, lane)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003097 }
3098
3099 FOR_ALL_POPULATED_CHANNELS
Angel Pons7c49cb82020-03-16 23:17:32 +01003100 MCHBAR32_OR(TC_RWP_ch(channel), 0x08000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003101
3102 FOR_ALL_POPULATED_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003103 udelay(1);
3104 MCHBAR32_OR(SCHED_CBIT_ch(channel), 0x00200000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003105 }
3106
3107 printram("CPE\n");
3108
Angel Pons88521882020-01-05 20:21:20 +01003109 MCHBAR32(GDCRTRAININGMOD) = 0;
3110 MCHBAR32(IOSAV_DC_MASK) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003111
3112 printram("CP5b\n");
3113
3114 FOR_ALL_POPULATED_CHANNELS {
3115 program_timings(ctrl, channel);
3116 }
3117
3118 u32 reg, addr;
3119
Angel Pons7c49cb82020-03-16 23:17:32 +01003120 /* Poll for RCOMP */
3121 while (!(MCHBAR32(RCOMP_TIMER) & (1 << 16)))
3122 ;
3123
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003124 do {
Angel Pons88521882020-01-05 20:21:20 +01003125 reg = MCHBAR32(IOSAV_STATUS_ch(0));
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003126 } while ((reg & 0x14) == 0);
3127
Angel Pons7c49cb82020-03-16 23:17:32 +01003128 /* Set state of memory controller */
Angel Pons88521882020-01-05 20:21:20 +01003129 MCHBAR32(MC_INIT_STATE_G) = 0x116;
Angel Pons7c49cb82020-03-16 23:17:32 +01003130 MCHBAR32(MC_INIT_STATE) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003131
Angel Pons7c49cb82020-03-16 23:17:32 +01003132 /* Wait 500us */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003133 udelay(500);
3134
3135 FOR_ALL_CHANNELS {
Angel Pons7c49cb82020-03-16 23:17:32 +01003136 /* Set valid rank CKE */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003137 reg = 0;
Angel Pons7c49cb82020-03-16 23:17:32 +01003138 reg = (reg & ~0x0f) | ctrl->rankmap[channel];
Angel Pons88521882020-01-05 20:21:20 +01003139 addr = MC_INIT_STATE_ch(channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003140 MCHBAR32(addr) = reg;
3141
Angel Pons7c49cb82020-03-16 23:17:32 +01003142 /* Wait 10ns for ranks to settle */
3143 // udelay(0.01);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003144
3145 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3146 MCHBAR32(addr) = reg;
3147
Angel Pons7c49cb82020-03-16 23:17:32 +01003148 /* Write reset using a NOP */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003149 write_reset(ctrl);
3150 }
3151
Angel Pons7c49cb82020-03-16 23:17:32 +01003152 /* MRS commands */
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003153 dram_mrscommands(ctrl);
3154
3155 printram("CP5c\n");
3156
Angel Pons88521882020-01-05 20:21:20 +01003157 MCHBAR32(GDCRTRAININGMOD_ch(0)) = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003158
3159 FOR_ALL_CHANNELS {
Angel Pons88521882020-01-05 20:21:20 +01003160 MCHBAR32_AND(GDCRCMDDEBUGMUXCFG_Cz_S(channel), ~0x3f000000);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003161 udelay(2);
3162 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003163}