blob: 151a7ec4d464322c7a5338736e50752282df60a2 [file] [log] [blame]
Stefan Reinauer00636b02012-04-04 00:08:51 +02001/*
2 * This file is part of the coreboot project.
3 *
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004 * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
5 * Copyright (C) 2014 Vladimir Serbinenko <phcoder@gmail.com>
Stefan Reinauer00636b02012-04-04 00:08:51 +02006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Stefan Reinauer00636b02012-04-04 00:08:51 +020015 */
16
17#include <console/console.h>
Kyösti Mälkki1d7541f2014-02-17 21:34:42 +020018#include <console/usb.h>
Kyösti Mälkki5687fc92013-11-28 18:11:49 +020019#include <bootmode.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020020#include <string.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020021#include <arch/io.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020022#include <cbmem.h>
23#include <arch/cbfs.h>
24#include <cbfs.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070025#include <halt.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020026#include <ip_checksum.h>
27#include <pc80/mc146818rtc.h>
Duncan Laurie7b508dd2012-04-09 12:30:43 -070028#include <device/pci_def.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070029#include "raminit_native.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020030#include "sandybridge.h"
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070031#include <delay.h>
32#include <lib.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020033
34/* Management Engine is in the southbridge */
35#include "southbridge/intel/bd82x6x/me.h"
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070036/* For SPD. */
37#include "southbridge/intel/bd82x6x/smbus.h"
38#include "arch/cpu.h"
39#include "cpu/x86/msr.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020040
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070041/* FIXME: no ECC support. */
42/* FIXME: no support for 3-channel chipsets. */
Stefan Reinauer00636b02012-04-04 00:08:51 +020043
Patrick Rudolph371d2912015-10-09 13:33:25 +020044/*
45 * Register description:
46 * Intel provides a command queue of depth four.
47 * Every command is configured by using multiple registers.
48 * On executing the command queue you have to provide the depth used.
49 *
50 * Known registers:
51 * Channel X = [0, 1]
52 * Command queue index Y = [0, 1, 2, 3]
53 *
54 * DEFAULT_MCHBAR + 0x4220 + 0x400 * X + 4 * Y: command io register
55 * Controls the DRAM command signals
56 * Bit 0: !RAS
57 * Bit 1: !CAS
58 * Bit 2: !WE
59 *
60 * DEFAULT_MCHBAR + 0x4200 + 0x400 * X + 4 * Y: addr bankslot io register
61 * Controls the address, bank address and slotrank signals
62 * Bit 0-15 : Address
63 * Bit 20-22: Bank Address
64 * Bit 24-25: slotrank
65 *
66 * DEFAULT_MCHBAR + 0x4230 + 0x400 * X + 4 * Y: idle register
67 * Controls the idle time after issuing this DRAM command
68 * Bit 16-32: number of clock-cylces to idle
69 *
70 * DEFAULT_MCHBAR + 0x4284 + 0x400 * channel: execute command queue
71 * Starts to execute all queued commands
72 * Bit 0 : start DRAM command execution
73 * Bit 16-20: (number of queued commands - 1) * 4
74 */
75
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070076#define BASEFREQ 133
77#define tDLLK 512
Stefan Reinauer00636b02012-04-04 00:08:51 +020078
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070079#define IS_SANDY_CPU(x) ((x & 0xffff0) == 0x206a0)
80#define IS_SANDY_CPU_C(x) ((x & 0xf) == 4)
81#define IS_SANDY_CPU_D0(x) ((x & 0xf) == 5)
82#define IS_SANDY_CPU_D1(x) ((x & 0xf) == 6)
83#define IS_SANDY_CPU_D2(x) ((x & 0xf) == 7)
Stefan Reinauer00636b02012-04-04 00:08:51 +020084
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070085#define IS_IVY_CPU(x) ((x & 0xffff0) == 0x306a0)
86#define IS_IVY_CPU_C(x) ((x & 0xf) == 4)
87#define IS_IVY_CPU_K(x) ((x & 0xf) == 5)
88#define IS_IVY_CPU_D(x) ((x & 0xf) == 6)
89#define IS_IVY_CPU_E(x) ((x & 0xf) >= 8)
Stefan Reinauer00636b02012-04-04 00:08:51 +020090
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070091#define NUM_CHANNELS 2
92#define NUM_SLOTRANKS 4
93#define NUM_SLOTS 2
94#define NUM_LANES 8
Stefan Reinauer00636b02012-04-04 00:08:51 +020095
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070096/* FIXME: Vendor BIOS uses 64 but our algorithms are less
97 performant and even 1 seems to be enough in practice. */
98#define NUM_PATTERNS 4
Stefan Reinauer00636b02012-04-04 00:08:51 +020099
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700100typedef struct odtmap_st {
101 u16 rttwr;
102 u16 rttnom;
103} odtmap;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200104
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700105typedef struct dimm_info_st {
106 dimm_attr dimm[NUM_CHANNELS][NUM_SLOTS];
107} dimm_info;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200108
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700109struct ram_rank_timings {
110 /* Register 4024. One byte per slotrank. */
111 u8 val_4024;
112 /* Register 4028. One nibble per slotrank. */
113 u8 val_4028;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200114
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700115 int val_320c;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200116
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700117 struct ram_lane_timings {
118 /* lane register offset 0x10. */
119 u16 timA; /* bits 0 - 5, bits 16 - 18 */
120 u8 rising; /* bits 8 - 14 */
121 u8 falling; /* bits 20 - 26. */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200122
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700123 /* lane register offset 0x20. */
124 int timC; /* bit 0 - 5, 19. */
125 u16 timB; /* bits 8 - 13, 15 - 17. */
126 } lanes[NUM_LANES];
127};
Stefan Reinauer00636b02012-04-04 00:08:51 +0200128
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700129struct ramctr_timing_st;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200130
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700131typedef struct ramctr_timing_st {
132 int mobile;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200133
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700134 u16 cas_supported;
135 /* tLatencies are in units of ns, scaled by x256 */
136 u32 tCK;
137 u32 tAA;
138 u32 tWR;
139 u32 tRCD;
140 u32 tRRD;
141 u32 tRP;
142 u32 tRAS;
143 u32 tRFC;
144 u32 tWTR;
145 u32 tRTP;
146 u32 tFAW;
147 /* Latencies in terms of clock cycles
148 * They are saved separately as they are needed for DRAM MRS commands*/
149 u8 CAS; /* CAS read latency */
150 u8 CWL; /* CAS write latency */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200151
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700152 u32 tREFI;
153 u32 tMOD;
154 u32 tXSOffset;
155 u32 tWLO;
156 u32 tCKE;
157 u32 tXPDLL;
158 u32 tXP;
159 u32 tAONPD;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200160
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700161 u16 reg_5064b0; /* bits 0-11. */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200162
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700163 u8 rankmap[NUM_CHANNELS];
164 int ref_card_offset[NUM_CHANNELS];
165 u32 mad_dimm[NUM_CHANNELS];
166 int channel_size_mb[NUM_CHANNELS];
167 u32 cmd_stretch[NUM_CHANNELS];
Stefan Reinauer00636b02012-04-04 00:08:51 +0200168
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700169 int reg_c14_offset;
170 int reg_320c_range_threshold;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200171
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700172 int edge_offset[3];
173 int timC_offset[3];
Stefan Reinauer00636b02012-04-04 00:08:51 +0200174
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700175 int extended_temperature_range;
176 int auto_self_refresh;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200177
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700178 int rank_mirror[NUM_CHANNELS][NUM_SLOTRANKS];
179
180 struct ram_rank_timings timings[NUM_CHANNELS][NUM_SLOTRANKS];
181} ramctr_timing;
182
183#define SOUTHBRIDGE PCI_DEV(0, 0x1f, 0)
184#define NORTHBRIDGE PCI_DEV(0, 0x0, 0)
185#define FOR_ALL_LANES for (lane = 0; lane < NUM_LANES; lane++)
186#define FOR_ALL_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++)
187#define FOR_ALL_POPULATED_RANKS for (slotrank = 0; slotrank < NUM_SLOTRANKS; slotrank++) if (ctrl->rankmap[channel] & (1 << slotrank))
188#define FOR_ALL_POPULATED_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) if (ctrl->rankmap[channel])
189#define MAX_EDGE_TIMING 71
190#define MAX_TIMC 127
191#define MAX_TIMB 511
192#define MAX_TIMA 127
193
194static void program_timings(ramctr_timing * ctrl, int channel);
195
196static const char *ecc_decoder[] = {
Stefan Reinauer00636b02012-04-04 00:08:51 +0200197 "inactive",
198 "active on IO",
199 "disabled on IO",
200 "active"
201};
202
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700203static void wait_txt_clear(void)
204{
205 struct cpuid_result cp;
206
207 cp = cpuid_ext(0x1, 0x0);
208 /* Check if TXT is supported? */
209 if (!(cp.ecx & 0x40))
210 return;
211 /* Some TXT public bit. */
212 if (!(read32((void *)0xfed30010) & 1))
213 return;
214 /* Wait for TXT clear. */
215 while (!(read8((void *)0xfed40000) & (1 << 7))) ;
216}
217
218static void sfence(void)
219{
220 asm volatile ("sfence");
221}
222
Patrick Rudolph9b515682015-10-09 13:43:51 +0200223static void toggle_io_reset(void) {
224 /* toggle IO reset bit */
225 u32 r32 = read32(DEFAULT_MCHBAR + 0x5030);
226 write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20);
227 udelay(1);
228 write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20);
229 udelay(1);
230}
231
Stefan Reinauer00636b02012-04-04 00:08:51 +0200232/*
233 * Dump in the log memory controller configuration as read from the memory
234 * controller registers.
235 */
236static void report_memory_config(void)
237{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700238 u32 addr_decoder_common, addr_decode_ch[NUM_CHANNELS];
Stefan Reinauer00636b02012-04-04 00:08:51 +0200239 int i;
240
241 addr_decoder_common = MCHBAR32(0x5000);
242 addr_decode_ch[0] = MCHBAR32(0x5004);
243 addr_decode_ch[1] = MCHBAR32(0x5008);
244
245 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700246 (MCHBAR32(0x5e04) * 13333 * 2 + 50) / 100);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200247 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700248 addr_decoder_common & 3, (addr_decoder_common >> 2) & 3,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200249 (addr_decoder_common >> 4) & 3);
250
251 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
252 u32 ch_conf = addr_decode_ch[i];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700253 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i,
254 ch_conf);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200255 printk(BIOS_DEBUG, " ECC %s\n",
256 ecc_decoder[(ch_conf >> 24) & 3]);
257 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
258 ((ch_conf >> 22) & 1) ? "on" : "off");
259 printk(BIOS_DEBUG, " rank interleave %s\n",
260 ((ch_conf >> 21) & 1) ? "on" : "off");
261 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
262 ((ch_conf >> 0) & 0xff) * 256,
263 ((ch_conf >> 19) & 1) ? 16 : 8,
264 ((ch_conf >> 17) & 1) ? "dual" : "single",
265 ((ch_conf >> 16) & 1) ? "" : ", selected");
266 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
267 ((ch_conf >> 8) & 0xff) * 256,
268 ((ch_conf >> 20) & 1) ? 16 : 8,
269 ((ch_conf >> 18) & 1) ? "dual" : "single",
270 ((ch_conf >> 16) & 1) ? ", selected" : "");
271 }
272}
273
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700274void read_spd(spd_raw_data * spd, u8 addr)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200275{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700276 int j;
277 for (j = 0; j < 256; j++)
278 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
279}
280
281static void dram_find_spds_ddr3(spd_raw_data * spd, dimm_info * dimm,
282 ramctr_timing * ctrl)
283{
284 int dimms = 0;
285 int channel, slot, spd_slot;
286
287 memset (ctrl->rankmap, 0, sizeof (ctrl->rankmap));
288
289 ctrl->extended_temperature_range = 1;
290 ctrl->auto_self_refresh = 1;
291
292 FOR_ALL_CHANNELS {
293 ctrl->channel_size_mb[channel] = 0;
294
295 for (slot = 0; slot < NUM_SLOTS; slot++) {
296 spd_slot = 2 * channel + slot;
297 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
298 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
299 // set dimm invalid
300 dimm->dimm[channel][slot].ranks = 0;
301 dimm->dimm[channel][slot].size_mb = 0;
302 continue;
303 }
304
305 dram_print_spd_ddr3(&dimm->dimm[channel][slot]);
306 dimms++;
307 ctrl->rank_mirror[channel][slot * 2] = 0;
308 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->dimm[channel][slot].flags.pins_mirrored;
309 ctrl->channel_size_mb[channel] += dimm->dimm[channel][slot].size_mb;
310
311 ctrl->auto_self_refresh &= dimm->dimm[channel][slot].flags.asr;
312 ctrl->extended_temperature_range &= dimm->dimm[channel][slot].flags.ext_temp_refresh;
313
314 ctrl->rankmap[channel] |= ((1 << dimm->dimm[channel][slot].ranks) - 1) << (2 * slot);
315 printk(BIOS_DEBUG, "rankmap[%d] = 0x%x\n", channel, ctrl->rankmap[channel]);
316 }
317 if ((ctrl->rankmap[channel] & 3) && (ctrl->rankmap[channel] & 0xc)
318 && dimm->dimm[channel][0].reference_card <= 5 && dimm->dimm[channel][1].reference_card <= 5) {
319 const int ref_card_offset_table[6][6] = {
320 { 0, 0, 0, 0, 2, 2, },
321 { 0, 0, 0, 0, 2, 2, },
322 { 0, 0, 0, 0, 2, 2, },
323 { 0, 0, 0, 0, 1, 1, },
324 { 2, 2, 2, 1, 0, 0, },
325 { 2, 2, 2, 1, 0, 0, },
326 };
327 ctrl->ref_card_offset[channel] = ref_card_offset_table[dimm->dimm[channel][0].reference_card]
328 [dimm->dimm[channel][1].reference_card];
329 } else
330 ctrl->ref_card_offset[channel] = 0;
331 }
332
333 if (!dimms)
334 die("No DIMMs were found");
335}
336
337static void dram_find_common_params(const dimm_info * dimms,
338 ramctr_timing * ctrl)
339{
340 size_t valid_dimms;
341 int channel, slot;
342 ctrl->cas_supported = 0xff;
343 valid_dimms = 0;
344 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
345 const dimm_attr *dimm = &dimms->dimm[channel][slot];
346 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
347 continue;
348 valid_dimms++;
349
350 /* Find all possible CAS combinations */
351 ctrl->cas_supported &= dimm->cas_supported;
352
353 /* Find the smallest common latencies supported by all DIMMs */
354 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
355 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
356 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
357 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
358 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
359 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
360 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
361 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
362 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
363 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
364 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
365 }
366
367 if (!ctrl->cas_supported)
368 die("Unsupported DIMM combination. "
369 "DIMMS do not support common CAS latency");
370 if (!valid_dimms)
371 die("No valid DIMMs found");
372}
373
374static u8 get_CWL(u8 CAS)
375{
376 /* Get CWL based on CAS using the following rule:
377 * _________________________________________
378 * CAS: | 4T | 5T | 6T | 7T | 8T | 9T | 10T | 11T |
379 * CWL: | 5T | 5T | 5T | 6T | 6T | 7T | 7T | 8T |
380 */
381 static const u8 cas_cwl_map[] = { 5, 5, 5, 6, 6, 7, 7, 8 };
382 if (CAS > 11)
383 return 8;
384 return cas_cwl_map[CAS - 4];
385}
386
387/* Frequency multiplier. */
388static u32 get_FRQ(u32 tCK)
389{
390 u32 FRQ;
391 FRQ = 256000 / (tCK * BASEFREQ);
392 if (FRQ > 8)
393 return 8;
394 if (FRQ < 3)
395 return 3;
396 return FRQ;
397}
398
399static u32 get_REFI(u32 tCK)
400{
401 /* Get REFI based on MCU frequency using the following rule:
402 * _________________________________________
403 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
404 * REFI: | 3120 | 4160 | 5200 | 6240 | 7280 | 8320 |
405 */
406 static const u32 frq_refi_map[] =
407 { 3120, 4160, 5200, 6240, 7280, 8320 };
408 return frq_refi_map[get_FRQ(tCK) - 3];
409}
410
411static u8 get_XSOffset(u32 tCK)
412{
413 /* Get XSOffset based on MCU frequency using the following rule:
414 * _________________________
415 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
416 * XSOffset : | 4 | 6 | 7 | 8 | 10 | 11 |
417 */
418 static const u8 frq_xs_map[] = { 4, 6, 7, 8, 10, 11 };
419 return frq_xs_map[get_FRQ(tCK) - 3];
420}
421
422static u8 get_MOD(u32 tCK)
423{
424 /* Get MOD based on MCU frequency using the following rule:
425 * _____________________________
426 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
427 * MOD : | 12 | 12 | 12 | 12 | 15 | 16 |
428 */
429 static const u8 frq_mod_map[] = { 12, 12, 12, 12, 15, 16 };
430 return frq_mod_map[get_FRQ(tCK) - 3];
431}
432
433static u8 get_WLO(u32 tCK)
434{
435 /* Get WLO based on MCU frequency using the following rule:
436 * _______________________
437 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
438 * WLO : | 4 | 5 | 6 | 6 | 8 | 8 |
439 */
440 static const u8 frq_wlo_map[] = { 4, 5, 6, 6, 8, 8 };
441 return frq_wlo_map[get_FRQ(tCK) - 3];
442}
443
444static u8 get_CKE(u32 tCK)
445{
446 /* Get CKE based on MCU frequency using the following rule:
447 * _______________________
448 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
449 * CKE : | 3 | 3 | 4 | 4 | 5 | 6 |
450 */
451 static const u8 frq_cke_map[] = { 3, 3, 4, 4, 5, 6 };
452 return frq_cke_map[get_FRQ(tCK) - 3];
453}
454
455static u8 get_XPDLL(u32 tCK)
456{
457 /* Get XPDLL based on MCU frequency using the following rule:
458 * _____________________________
459 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
460 * XPDLL : | 10 | 13 | 16 | 20 | 23 | 26 |
461 */
462 static const u8 frq_xpdll_map[] = { 10, 13, 16, 20, 23, 26 };
463 return frq_xpdll_map[get_FRQ(tCK) - 3];
464}
465
466static u8 get_XP(u32 tCK)
467{
468 /* Get XP based on MCU frequency using the following rule:
469 * _______________________
470 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
471 * XP : | 3 | 4 | 4 | 5 | 6 | 7 |
472 */
473 static const u8 frq_xp_map[] = { 3, 4, 4, 5, 6, 7 };
474 return frq_xp_map[get_FRQ(tCK) - 3];
475}
476
477static u8 get_AONPD(u32 tCK)
478{
479 /* Get AONPD based on MCU frequency using the following rule:
480 * ________________________
481 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
482 * AONPD : | 4 | 5 | 6 | 8 | 8 | 10 |
483 */
484 static const u8 frq_aonpd_map[] = { 4, 5, 6, 8, 8, 10 };
485 return frq_aonpd_map[get_FRQ(tCK) - 3];
486}
487
488static u32 get_COMP2(u32 tCK)
489{
490 /* Get COMP2 based on MCU frequency using the following rule:
491 * ___________________________________________________________
492 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
493 * COMP : | D6BEDCC | CE7C34C | CA57A4C | C6369CC | C42514C | C21410C |
494 */
495 static const u32 frq_comp2_map[] = { 0xD6BEDCC, 0xCE7C34C, 0xCA57A4C,
496 0xC6369CC, 0xC42514C, 0xC21410C
497 };
498 return frq_comp2_map[get_FRQ(tCK) - 3];
499}
500
501static void dram_timing(ramctr_timing * ctrl)
502{
503 u8 val;
504 u32 val32;
505
506 /* Maximum supported DDR3 frequency is 1066MHz (DDR3 2133) so make sure
507 * we cap it if we have faster DIMMs.
508 * Then, align it to the closest JEDEC standard frequency */
509 if (ctrl->tCK <= TCK_1066MHZ) {
510 ctrl->tCK = TCK_1066MHZ;
511 ctrl->edge_offset[0] = 16;
512 ctrl->edge_offset[1] = 7;
513 ctrl->edge_offset[2] = 7;
514 ctrl->timC_offset[0] = 18;
515 ctrl->timC_offset[1] = 7;
516 ctrl->timC_offset[2] = 7;
517 ctrl->reg_c14_offset = 16;
518 ctrl->reg_5064b0 = 0x218;
519 ctrl->reg_320c_range_threshold = 13;
520 } else if (ctrl->tCK <= TCK_933MHZ) {
521 ctrl->tCK = TCK_933MHZ;
522 ctrl->edge_offset[0] = 14;
523 ctrl->edge_offset[1] = 6;
524 ctrl->edge_offset[2] = 6;
525 ctrl->timC_offset[0] = 15;
526 ctrl->timC_offset[1] = 6;
527 ctrl->timC_offset[2] = 6;
528 ctrl->reg_c14_offset = 14;
529 ctrl->reg_5064b0 = 0x1d5;
530 ctrl->reg_320c_range_threshold = 15;
531 } else if (ctrl->tCK <= TCK_800MHZ) {
532 ctrl->tCK = TCK_800MHZ;
533 ctrl->edge_offset[0] = 13;
534 ctrl->edge_offset[1] = 5;
535 ctrl->edge_offset[2] = 5;
536 ctrl->timC_offset[0] = 14;
537 ctrl->timC_offset[1] = 5;
538 ctrl->timC_offset[2] = 5;
539 ctrl->reg_c14_offset = 12;
540 ctrl->reg_5064b0 = 0x193;
541 ctrl->reg_320c_range_threshold = 15;
542 } else if (ctrl->tCK <= TCK_666MHZ) {
543 ctrl->tCK = TCK_666MHZ;
544 ctrl->edge_offset[0] = 10;
545 ctrl->edge_offset[1] = 4;
546 ctrl->edge_offset[2] = 4;
547 ctrl->timC_offset[0] = 11;
548 ctrl->timC_offset[1] = 4;
549 ctrl->timC_offset[2] = 4;
550 ctrl->reg_c14_offset = 10;
551 ctrl->reg_5064b0 = 0x150;
552 ctrl->reg_320c_range_threshold = 16;
553 } else if (ctrl->tCK <= TCK_533MHZ) {
554 ctrl->tCK = TCK_533MHZ;
555 ctrl->edge_offset[0] = 8;
556 ctrl->edge_offset[1] = 3;
557 ctrl->edge_offset[2] = 3;
558 ctrl->timC_offset[0] = 9;
559 ctrl->timC_offset[1] = 3;
560 ctrl->timC_offset[2] = 3;
561 ctrl->reg_c14_offset = 8;
562 ctrl->reg_5064b0 = 0x10d;
563 ctrl->reg_320c_range_threshold = 17;
564 } else {
565 ctrl->tCK = TCK_400MHZ;
566 ctrl->edge_offset[0] = 6;
567 ctrl->edge_offset[1] = 2;
568 ctrl->edge_offset[2] = 2;
569 ctrl->timC_offset[0] = 6;
570 ctrl->timC_offset[1] = 2;
571 ctrl->timC_offset[2] = 2;
572 ctrl->reg_c14_offset = 8;
573 ctrl->reg_5064b0 = 0xcd;
574 ctrl->reg_320c_range_threshold = 17;
575 }
576
577 val32 = (1000 << 8) / ctrl->tCK;
578 printk(BIOS_DEBUG, "Selected DRAM frequency: %u MHz\n", val32);
579
580 /* Find CAS and CWL latencies */
581 val = (ctrl->tAA + ctrl->tCK - 1) / ctrl->tCK;
582 printk(BIOS_DEBUG, "Minimum CAS latency : %uT\n", val);
583 /* Find lowest supported CAS latency that satisfies the minimum value */
584 while (!((ctrl->cas_supported >> (val - 4)) & 1)
585 && (ctrl->cas_supported >> (val - 4))) {
586 val++;
587 }
588 /* Is CAS supported */
589 if (!(ctrl->cas_supported & (1 << (val - 4))))
590 printk(BIOS_DEBUG, "CAS not supported\n");
591 printk(BIOS_DEBUG, "Selected CAS latency : %uT\n", val);
592 ctrl->CAS = val;
593 ctrl->CWL = get_CWL(ctrl->CAS);
594 printk(BIOS_DEBUG, "Selected CWL latency : %uT\n", ctrl->CWL);
595
596 /* Find tRCD */
597 ctrl->tRCD = (ctrl->tRCD + ctrl->tCK - 1) / ctrl->tCK;
598 printk(BIOS_DEBUG, "Selected tRCD : %uT\n", ctrl->tRCD);
599
600 ctrl->tRP = (ctrl->tRP + ctrl->tCK - 1) / ctrl->tCK;
601 printk(BIOS_DEBUG, "Selected tRP : %uT\n", ctrl->tRP);
602
603 /* Find tRAS */
604 ctrl->tRAS = (ctrl->tRAS + ctrl->tCK - 1) / ctrl->tCK;
605 printk(BIOS_DEBUG, "Selected tRAS : %uT\n", ctrl->tRAS);
606
607 /* Find tWR */
608 ctrl->tWR = (ctrl->tWR + ctrl->tCK - 1) / ctrl->tCK;
609 printk(BIOS_DEBUG, "Selected tWR : %uT\n", ctrl->tWR);
610
611 /* Find tFAW */
612 ctrl->tFAW = (ctrl->tFAW + ctrl->tCK - 1) / ctrl->tCK;
613 printk(BIOS_DEBUG, "Selected tFAW : %uT\n", ctrl->tFAW);
614
615 /* Find tRRD */
616 ctrl->tRRD = (ctrl->tRRD + ctrl->tCK - 1) / ctrl->tCK;
617 printk(BIOS_DEBUG, "Selected tRRD : %uT\n", ctrl->tRRD);
618
619 /* Find tRTP */
620 ctrl->tRTP = (ctrl->tRTP + ctrl->tCK - 1) / ctrl->tCK;
621 printk(BIOS_DEBUG, "Selected tRTP : %uT\n", ctrl->tRTP);
622
623 /* Find tWTR */
624 ctrl->tWTR = (ctrl->tWTR + ctrl->tCK - 1) / ctrl->tCK;
625 printk(BIOS_DEBUG, "Selected tWTR : %uT\n", ctrl->tWTR);
626
627 /* Refresh-to-Active or Refresh-to-Refresh (tRFC) */
628 ctrl->tRFC = (ctrl->tRFC + ctrl->tCK - 1) / ctrl->tCK;
629 printk(BIOS_DEBUG, "Selected tRFC : %uT\n", ctrl->tRFC);
630
631 ctrl->tREFI = get_REFI(ctrl->tCK);
632 ctrl->tMOD = get_MOD(ctrl->tCK);
633 ctrl->tXSOffset = get_XSOffset(ctrl->tCK);
634 ctrl->tWLO = get_WLO(ctrl->tCK);
635 ctrl->tCKE = get_CKE(ctrl->tCK);
636 ctrl->tXPDLL = get_XPDLL(ctrl->tCK);
637 ctrl->tXP = get_XP(ctrl->tCK);
638 ctrl->tAONPD = get_AONPD(ctrl->tCK);
639}
640
641static void dram_freq(ramctr_timing * ctrl)
642{
643 if (ctrl->tCK > TCK_400MHZ) {
644 printk (BIOS_ERR, "DRAM frequency is under lowest supported frequency (400 MHz). Increasing to 400 MHz as last resort");
645 ctrl->tCK = TCK_400MHZ;
646 }
647 while (1) {
648 u8 val2;
649 u32 reg1 = 0;
650
651 /* Step 1 - Set target PCU frequency */
652
653 if (ctrl->tCK <= TCK_1066MHZ) {
654 ctrl->tCK = TCK_1066MHZ;
655 } else if (ctrl->tCK <= TCK_933MHZ) {
656 ctrl->tCK = TCK_933MHZ;
657 } else if (ctrl->tCK <= TCK_800MHZ) {
658 ctrl->tCK = TCK_800MHZ;
659 } else if (ctrl->tCK <= TCK_666MHZ) {
660 ctrl->tCK = TCK_666MHZ;
661 } else if (ctrl->tCK <= TCK_533MHZ) {
662 ctrl->tCK = TCK_533MHZ;
663 } else if (ctrl->tCK <= TCK_400MHZ) {
664 ctrl->tCK = TCK_400MHZ;
665 } else {
666 die ("No lock frequency found");
667 }
668
669 /* Frequency mulitplier. */
670 u32 FRQ = get_FRQ(ctrl->tCK);
671
672 /* Step 2 - Select frequency in the MCU */
673 reg1 = FRQ;
674 reg1 |= 0x80000000; // set running bit
675 MCHBAR32(0x5e00) = reg1;
676 while (reg1 & 0x80000000) {
677 printk(BIOS_DEBUG, " PLL busy...");
678 reg1 = MCHBAR32(0x5e00);
679 }
680 printk(BIOS_DEBUG, "done\n");
681
682 /* Step 3 - Verify lock frequency */
683 reg1 = MCHBAR32(0x5e04);
684 val2 = (u8) reg1;
685 if (val2 >= FRQ) {
686 printk(BIOS_DEBUG, "MCU frequency is set at : %d MHz\n",
687 (1000 << 8) / ctrl->tCK);
688 return;
689 }
690 printk(BIOS_DEBUG, "PLL didn't lock. Retrying at lower frequency\n");
691 ctrl->tCK++;
692 }
693}
694
695static void dram_xover(ramctr_timing * ctrl)
696{
697 u32 reg;
698 int channel;
699
700 FOR_ALL_CHANNELS {
701 // enable xover clk
702 printk(BIOS_DEBUG, "[%x] = %x\n", channel * 0x100 + 0xc14,
703 (ctrl->rankmap[channel] << 24));
704 MCHBAR32(channel * 0x100 + 0xc14) = (ctrl->rankmap[channel] << 24);
705
706 // enable xover ctl
707 reg = 0;
708 if (ctrl->rankmap[channel] & 0x5) {
709 reg |= 0x20000;
710 }
711 if (ctrl->rankmap[channel] & 0xa) {
712 reg |= 0x4000000;
713 }
714 // enable xover cmd
715 reg |= 0x4000;
716 printk(BIOS_DEBUG, "[%x] = %x\n", 0x100 * channel + 0x320c,
717 reg);
718 MCHBAR32(0x100 * channel + 0x320c) = reg;
719 }
720}
721
722static void dram_timing_regs(ramctr_timing * ctrl)
723{
724 u32 reg, addr, val32, cpu, stretch;
725 struct cpuid_result cpures;
726 int channel;
727
728 FOR_ALL_CHANNELS {
729 // DBP
730 reg = 0;
731 reg |= ctrl->tRCD;
732 reg |= (ctrl->tRP << 4);
733 reg |= (ctrl->CAS << 8);
734 reg |= (ctrl->CWL << 12);
735 reg |= (ctrl->tRAS << 16);
736 printk(BIOS_DEBUG, "[%x] = %x\n", 0x400 * channel + 0x4000,
737 reg);
738 MCHBAR32(0x400 * channel + 0x4000) = reg;
739
740 // RAP
741 reg = 0;
742 reg |= ctrl->tRRD;
743 reg |= (ctrl->tRTP << 4);
744 reg |= (ctrl->tCKE << 8);
745 reg |= (ctrl->tWTR << 12);
746 reg |= (ctrl->tFAW << 16);
747 reg |= (ctrl->tWR << 24);
748 reg |= (3 << 30);
749 printk(BIOS_DEBUG, "[%x] = %x\n", 0x400 * channel + 0x4004,
750 reg);
751 MCHBAR32(0x400 * channel + 0x4004) = reg;
752
753 // OTHP
754 addr = 0x400 * channel + 0x400c;
755 reg = 0;
756 reg |= ctrl->tXPDLL;
757 reg |= (ctrl->tXP << 5);
758 reg |= (ctrl->tAONPD << 8);
759 reg |= 0xa0000;
760 printk(BIOS_DEBUG, "[%x] = %x\n", addr, reg);
761 MCHBAR32(addr) = reg;
762
763 MCHBAR32(0x400 * channel + 0x4014) = 0;
764
765 MCHBAR32(addr) |= 0x00020000;
766
767 // ODT stretch
768 reg = 0;
769
770 cpures = cpuid(0);
771 cpu = cpures.eax;
772 if (IS_IVY_CPU(cpu)
773 || (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_D2(cpu))) {
774 stretch = 2;
775 addr = 0x400 * channel + 0x400c;
776 printk(BIOS_DEBUG, "[%x] = %x\n",
777 0x400 * channel + 0x400c, reg);
778 reg = MCHBAR32(addr);
779
780 if (((ctrl->rankmap[channel] & 3) == 0)
781 || (ctrl->rankmap[channel] & 0xc) == 0) {
782
783 // Rank 0 - operate on rank 2
784 reg = (reg & ~0xc0000) | (stretch << 18);
785
786 // Rank 2 - operate on rank 0
787 reg = (reg & ~0x30000) | (stretch << 16);
788
789 printk(BIOS_DEBUG, "[%x] = %x\n", addr, reg);
790 MCHBAR32(addr) = reg;
791 }
792
793 } else if (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_C(cpu)) {
794 stretch = 3;
795 addr = 0x400 * channel + 0x401c;
796 reg = MCHBAR32(addr);
797
798 if (((ctrl->rankmap[channel] & 3) == 0)
799 || (ctrl->rankmap[channel] & 0xc) == 0) {
800
801 // Rank 0 - operate on rank 2
802 reg = (reg & ~0x3000) | (stretch << 12);
803
804 // Rank 2 - operate on rank 0
805 reg = (reg & ~0xc00) | (stretch << 10);
806
807 printk(BIOS_DEBUG, "[%x] = %x\n", addr, reg);
808 MCHBAR32(addr) = reg;
809 }
810 } else {
811 stretch = 0;
812 }
813
814 // REFI
815 reg = 0;
816 val32 = ctrl->tREFI;
817 reg = (reg & ~0xffff) | val32;
818 val32 = ctrl->tRFC;
819 reg = (reg & ~0x1ff0000) | (val32 << 16);
820 val32 = (u32) (ctrl->tREFI * 9) / 1024;
821 reg = (reg & ~0xfe000000) | (val32 << 25);
822 printk(BIOS_DEBUG, "[%x] = %x\n", 0x400 * channel + 0x4298,
823 reg);
824 MCHBAR32(0x400 * channel + 0x4298) = reg;
825
826 MCHBAR32(0x400 * channel + 0x4294) |= 0xff;
827
828 // SRFTP
829 reg = 0;
830 val32 = tDLLK;
831 reg = (reg & ~0xfff) | val32;
832 val32 = ctrl->tXSOffset;
833 reg = (reg & ~0xf000) | (val32 << 12);
834 val32 = tDLLK - ctrl->tXSOffset;
835 reg = (reg & ~0x3ff0000) | (val32 << 16);
836 val32 = ctrl->tMOD - 8;
837 reg = (reg & ~0xf0000000) | (val32 << 28);
838 printk(BIOS_DEBUG, "[%x] = %x\n", 0x400 * channel + 0x42a4,
839 reg);
840 MCHBAR32(0x400 * channel + 0x42a4) = reg;
841 }
842}
843
844static void dram_dimm_mapping(dimm_info * info, ramctr_timing * ctrl)
845{
846 u32 reg, val32;
847 int channel;
848
849 FOR_ALL_CHANNELS {
850 dimm_attr *dimmA = 0;
851 dimm_attr *dimmB = 0;
852 reg = 0;
853 val32 = 0;
854 if (info->dimm[channel][0].size_mb >=
855 info->dimm[channel][1].size_mb) {
856 // dimm 0 is bigger, set it to dimmA
857 dimmA = &info->dimm[channel][0];
858 dimmB = &info->dimm[channel][1];
859 reg |= (0 << 16);
860 } else {
861 // dimm 1 is bigger, set it to dimmA
862 dimmA = &info->dimm[channel][1];
863 dimmB = &info->dimm[channel][0];
864 reg |= (1 << 16);
865 }
866 // dimmA
867 if (dimmA && (dimmA->ranks > 0)) {
868 val32 = dimmA->size_mb / 256;
869 reg = (reg & ~0xff) | val32;
870 val32 = dimmA->ranks - 1;
871 reg = (reg & ~0x20000) | (val32 << 17);
872 val32 = (dimmA->width / 8) - 1;
873 reg = (reg & ~0x80000) | (val32 << 19);
874 }
875 // dimmB
876 if (dimmB && (dimmB->ranks > 0)) {
877 val32 = dimmB->size_mb / 256;
878 reg = (reg & ~0xff00) | (val32 << 8);
879 val32 = dimmB->ranks - 1;
880 reg = (reg & ~0x40000) | (val32 << 18);
881 val32 = (dimmB->width / 8) - 1;
882 reg = (reg & ~0x100000) | (val32 << 20);
883 }
884 reg = (reg & ~0x200000) | (1 << 21); // rank interleave
885 reg = (reg & ~0x400000) | (1 << 22); // enhanced interleave
886
887 // Save MAD-DIMM register
888 if ((dimmA && (dimmA->ranks > 0))
889 || (dimmB && (dimmB->ranks > 0))) {
890 ctrl->mad_dimm[channel] = reg;
891 } else {
892 ctrl->mad_dimm[channel] = 0;
893 }
894 }
895}
896
897static void dram_dimm_set_mapping(ramctr_timing * ctrl)
898{
899 int channel;
900 FOR_ALL_CHANNELS {
901 MCHBAR32(0x5004 + channel * 4) = ctrl->mad_dimm[channel];
902 }
903}
904
905static void dram_zones(ramctr_timing * ctrl, int training)
906{
907 u32 reg, ch0size, ch1size;
908 u8 val;
909 reg = 0;
910 val = 0;
911 if (training) {
912 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
913 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
914 } else {
915 ch0size = ctrl->channel_size_mb[0];
916 ch1size = ctrl->channel_size_mb[1];
917 }
918
919 if (ch0size >= ch1size) {
920 reg = MCHBAR32(0x5014);
921 val = ch1size / 256;
922 reg = (reg & ~0xff000000) | val << 24;
923 reg = (reg & ~0xff0000) | (2 * val) << 16;
924 MCHBAR32(0x5014) = reg;
925 MCHBAR32(0x5000) = 0x24;
926 } else {
927 reg = MCHBAR32(0x5014);
928 val = ch0size / 256;
929 reg = (reg & ~0xff000000) | val << 24;
930 reg = (reg & ~0xff0000) | (2 * val) << 16;
931 MCHBAR32(0x5014) = reg;
932 MCHBAR32(0x5000) = 0x21;
933 }
934}
935
936static void dram_memorymap(ramctr_timing * ctrl, int me_uma_size)
937{
938 u32 reg, val, reclaim;
939 u32 tom, gfxstolen, gttsize;
940 size_t tsegsize, mmiosize, toludbase, touudbase, gfxstolenbase, gttbase,
941 tsegbase, mestolenbase;
942 size_t tsegbasedelta, remapbase, remaplimit;
943 uint16_t ggc;
944
945 mmiosize = 0x400;
946
947 ggc = pci_read_config16(NORTHBRIDGE, GGC);
948 if (!(ggc & 2)) {
949 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
950 gttsize = ((ggc >> 8) & 0x3);
951 } else {
952 gfxstolen = 0;
953 gttsize = 0;
954 }
955
956 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
957
958 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
959
960 mestolenbase = tom - me_uma_size;
961
962 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize,
963 tom - me_uma_size);
964 gfxstolenbase = toludbase - gfxstolen;
965 gttbase = gfxstolenbase - gttsize;
966
967 tsegbase = gttbase - tsegsize;
968
969 // Round tsegbase down to nearest address aligned to tsegsize
970 tsegbasedelta = tsegbase & (tsegsize - 1);
971 tsegbase &= ~(tsegsize - 1);
972
973 gttbase -= tsegbasedelta;
974 gfxstolenbase -= tsegbasedelta;
975 toludbase -= tsegbasedelta;
976
977 // Test if it is possible to reclaim a hole in the ram addressing
978 if (tom - me_uma_size > toludbase) {
979 // Reclaim is possible
980 reclaim = 1;
981 remapbase = MAX(4096, tom - me_uma_size);
982 remaplimit =
983 remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
984 touudbase = remaplimit + 1;
985 } else {
986 // Reclaim not possible
987 reclaim = 0;
988 touudbase = tom - me_uma_size;
989 }
990
991 // Update memory map in pci-e configuration space
992
993 // TOM (top of memory)
994 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa0);
995 val = tom & 0xfff;
996 reg = (reg & ~0xfff00000) | (val << 20);
997 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xa0, reg);
998 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa0, reg);
999
1000 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa4);
1001 val = tom & 0xfffff000;
1002 reg = (reg & ~0x000fffff) | (val >> 12);
1003 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xa4, reg);
1004 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa4, reg);
1005
1006 // TOLUD (top of low used dram)
1007 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xbc);
1008 val = toludbase & 0xfff;
1009 reg = (reg & ~0xfff00000) | (val << 20);
1010 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xbc, reg);
1011 pcie_write_config32(PCI_DEV(0, 0, 0), 0xbc, reg);
1012
1013 // TOUUD LSB (top of upper usable dram)
1014 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa8);
1015 val = touudbase & 0xfff;
1016 reg = (reg & ~0xfff00000) | (val << 20);
1017 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xa8, reg);
1018 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa8, reg);
1019
1020 // TOUUD MSB
1021 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xac);
1022 val = touudbase & 0xfffff000;
1023 reg = (reg & ~0x000fffff) | (val >> 12);
1024 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xac, reg);
1025 pcie_write_config32(PCI_DEV(0, 0, 0), 0xac, reg);
1026
1027 if (reclaim) {
1028 // REMAP BASE
1029 pcie_write_config32(PCI_DEV(0, 0, 0), 0x90, remapbase << 20);
1030 pcie_write_config32(PCI_DEV(0, 0, 0), 0x94, remapbase >> 12);
1031
1032 // REMAP LIMIT
1033 pcie_write_config32(PCI_DEV(0, 0, 0), 0x98, remaplimit << 20);
1034 pcie_write_config32(PCI_DEV(0, 0, 0), 0x9c, remaplimit >> 12);
1035 }
1036 // TSEG
1037 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb8);
1038 val = tsegbase & 0xfff;
1039 reg = (reg & ~0xfff00000) | (val << 20);
1040 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xb8, reg);
1041 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb8, reg);
1042
1043 // GFX stolen memory
1044 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb0);
1045 val = gfxstolenbase & 0xfff;
1046 reg = (reg & ~0xfff00000) | (val << 20);
1047 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xb0, reg);
1048 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb0, reg);
1049
1050 // GTT stolen memory
1051 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb4);
1052 val = gttbase & 0xfff;
1053 reg = (reg & ~0xfff00000) | (val << 20);
1054 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xb4, reg);
1055 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb4, reg);
1056
1057 if (me_uma_size) {
1058 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x7c);
1059 val = (0x80000 - me_uma_size) & 0xfffff000;
1060 reg = (reg & ~0x000fffff) | (val >> 12);
1061 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0x7c, reg);
1062 pcie_write_config32(PCI_DEV(0, 0, 0), 0x7c, reg);
1063
1064 // ME base
1065 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x70);
1066 val = mestolenbase & 0xfff;
1067 reg = (reg & ~0xfff00000) | (val << 20);
1068 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0x70, reg);
1069 pcie_write_config32(PCI_DEV(0, 0, 0), 0x70, reg);
1070
1071 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x74);
1072 val = mestolenbase & 0xfffff000;
1073 reg = (reg & ~0x000fffff) | (val >> 12);
1074 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0x74, reg);
1075 pcie_write_config32(PCI_DEV(0, 0, 0), 0x74, reg);
1076
1077 // ME mask
1078 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x78);
1079 val = (0x80000 - me_uma_size) & 0xfff;
1080 reg = (reg & ~0xfff00000) | (val << 20);
1081 reg = (reg & ~0x400) | (1 << 10); // set lockbit on ME mem
1082
1083 reg = (reg & ~0x800) | (1 << 11); // set ME memory enable
1084 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0x78, reg);
1085 pcie_write_config32(PCI_DEV(0, 0, 0), 0x78, reg);
1086 }
1087}
1088
1089static void dram_ioregs(ramctr_timing * ctrl)
1090{
1091 u32 reg, comp2;
1092
1093 int channel;
1094
1095 // IO clock
1096 FOR_ALL_CHANNELS {
1097 MCHBAR32(0xc00 + 0x100 * channel) = ctrl->rankmap[channel];
1098 }
1099
1100 // IO command
1101 FOR_ALL_CHANNELS {
1102 MCHBAR32(0x3200 + 0x100 * channel) = ctrl->rankmap[channel];
1103 }
1104
1105 // IO control
1106 FOR_ALL_POPULATED_CHANNELS {
1107 program_timings(ctrl, channel);
1108 }
1109
1110 // Rcomp
1111 printk(BIOS_DEBUG, "RCOMP...");
1112 reg = 0;
1113 while (reg == 0) {
1114 reg = MCHBAR32(0x5084) & 0x10000;
1115 }
1116 printk(BIOS_DEBUG, "done\n");
1117
1118 // Set comp2
1119 comp2 = get_COMP2(ctrl->tCK);
1120 MCHBAR32(0x3714) = comp2;
1121 printk(BIOS_DEBUG, "COMP2 done\n");
1122
1123 // Set comp1
1124 FOR_ALL_POPULATED_CHANNELS {
1125 reg = MCHBAR32(0x1810 + channel * 0x100); //ch0
1126 reg = (reg & ~0xe00) | (1 << 9); //odt
1127 reg = (reg & ~0xe00000) | (1 << 21); //clk drive up
1128 reg = (reg & ~0x38000000) | (1 << 27); //ctl drive up
1129 MCHBAR32(0x1810 + channel * 0x100) = reg;
1130 }
1131 printk(BIOS_DEBUG, "COMP1 done\n");
1132
1133 printk(BIOS_DEBUG, "FORCE RCOMP and wait 20us...");
1134 MCHBAR32(0x5f08) |= 0x100;
1135 udelay(20);
1136 printk(BIOS_DEBUG, "done\n");
1137}
1138
1139static void wait_428c(int channel)
1140{
1141 while (1) {
1142 if (read32(DEFAULT_MCHBAR + 0x428c + (channel << 10)) & 0x50)
1143 return;
1144 }
1145}
1146
1147static void write_reset(ramctr_timing * ctrl)
1148{
1149 int channel, slotrank;
1150
1151 /* choose a populated channel. */
1152 channel = (ctrl->rankmap[0]) ? 0 : 1;
1153
1154 wait_428c(channel);
1155
1156 /* choose a populated rank. */
1157 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
1158
Patrick Rudolph371d2912015-10-09 13:33:25 +02001159 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001160 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1161 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x80c01);
1162
1163 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1164 (slotrank << 24) | 0x60000);
1165
1166 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1167
1168 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x400001);
1169 wait_428c(channel);
1170}
1171
1172static void dram_jedecreset(ramctr_timing * ctrl)
1173{
1174 u32 reg, addr;
1175 int channel;
1176
1177 while (!(MCHBAR32(0x5084) & 0x10000)) ;
1178 do {
1179 reg = MCHBAR32(0x428c);
1180 } while ((reg & 0x14) == 0);
1181
1182 // Set state of memory controller
1183 reg = 0x112;
1184 MCHBAR32(0x5030) = reg;
1185 MCHBAR32(0x4ea0) = 0;
1186 reg |= 2; //ddr reset
1187 MCHBAR32(0x5030) = reg;
1188
1189 // Assert dimm reset signal
1190 reg = MCHBAR32(0x5030);
1191 reg &= ~0x2;
1192 MCHBAR32(0x5030) = reg;
1193
1194 // Wait 200us
1195 udelay(200);
1196
1197 // Deassert dimm reset signal
1198 MCHBAR32(0x5030) |= 2;
1199
1200 // Wait 500us
1201 udelay(500);
1202
1203 // Enable DCLK
1204 MCHBAR32(0x5030) |= 4;
1205
1206 // XXX Wait 20ns
1207 udelay(1);
1208
1209 FOR_ALL_CHANNELS {
1210 // Set valid rank CKE
1211 reg = 0;
1212 reg = (reg & ~0xf) | ctrl->rankmap[channel];
1213 addr = 0x400 * channel + 0x42a0;
1214 MCHBAR32(addr) = reg;
1215
1216 // Wait 10ns for ranks to settle
1217 //udelay(0.01);
1218
1219 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
1220 MCHBAR32(addr) = reg;
1221
1222 // Write reset using a NOP
1223 write_reset(ctrl);
1224 }
1225}
1226
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001227static odtmap get_ODT(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001228{
1229 /* Get ODT based on rankmap: */
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001230 int dimms_per_ch = (ctrl->rankmap[channel] & 1)
1231 + ((ctrl->rankmap[channel] >> 2) & 1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001232
1233 if (dimms_per_ch == 1) {
1234 return (const odtmap){60, 60};
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001235 } else {
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001236 return (const odtmap){120, 30};
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001237 }
1238}
1239
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001240static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001241 int reg, u32 val)
1242{
1243 wait_428c(channel);
1244
1245 printram("MRd: %x <= %x\n", reg, val);
1246
1247 if (ctrl->rank_mirror[channel][slotrank]) {
1248 /* DDR3 Rank1 Address mirror
1249 * swap the following pins:
1250 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
1251 reg = ((reg >> 1) & 1) | ((reg << 1) & 2);
1252 val = (val & ~0x1f8) | ((val >> 1) & 0xa8)
1253 | ((val & 0xa8) << 1);
1254 }
1255
1256 printram("MRd: %x <= %x\n", reg, val);
1257
Patrick Rudolph371d2912015-10-09 13:33:25 +02001258 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001259 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f000);
1260 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
1261 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1262 (slotrank << 24) | (reg << 20) | val | 0x60000);
1263 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1264
Patrick Rudolph371d2912015-10-09 13:33:25 +02001265 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001266 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f000);
1267 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x41001);
1268 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1269 (slotrank << 24) | (reg << 20) | val | 0x60000);
1270 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1271
Patrick Rudolph371d2912015-10-09 13:33:25 +02001272 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001273 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x0f000);
1274 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1275 0x1001 | (ctrl->tMOD << 16));
1276 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1277 (slotrank << 24) | (reg << 20) | val | 0x60000);
1278 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1279 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
1280}
1281
1282static u32 make_mr0(ramctr_timing * ctrl, u8 rank)
1283{
1284 u16 mr0reg, mch_cas, mch_wr;
1285 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
Patrick Rudolph371d2912015-10-09 13:33:25 +02001286
1287 /* DLL Reset - self clearing - set after CLK frequency has been changed */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001288 mr0reg = 0x100;
1289
1290 // Convert CAS to MCH register friendly
1291 if (ctrl->CAS < 12) {
1292 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
1293 } else {
1294 mch_cas = (u16) (ctrl->CAS - 12);
1295 mch_cas = ((mch_cas << 1) | 0x1);
1296 }
1297
1298 // Convert tWR to MCH register friendly
1299 mch_wr = mch_wr_t[ctrl->tWR - 5];
1300
1301 mr0reg = (mr0reg & ~0x4) | (mch_cas & 0x1);
1302 mr0reg = (mr0reg & ~0x70) | ((mch_cas & 0xe) << 3);
1303 mr0reg = (mr0reg & ~0xe00) | (mch_wr << 9);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001304
1305 // Precharge PD - Fast (desktop) 0x1 or slow (mobile) 0x0 - mostly power-saving feature
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001306 mr0reg = (mr0reg & ~0x1000) | (!ctrl->mobile << 12);
1307 return mr0reg;
1308}
1309
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001310static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001311{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001312 write_mrreg(ctrl, channel, rank, 0,
1313 make_mr0(ctrl, rank));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001314}
1315
1316static u32 encode_odt(u32 odt)
1317{
1318 switch (odt) {
1319 case 30:
1320 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
1321 case 60:
1322 return (1 << 2); // RZQ/4
1323 case 120:
1324 return (1 << 6); // RZQ/2
1325 default:
1326 case 0:
1327 return 0;
1328 }
1329}
1330
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001331static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001332{
1333 odtmap odt;
1334 u32 mr1reg;
1335
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001336 odt = get_ODT(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001337 mr1reg = 0x2;
1338
1339 mr1reg |= encode_odt(odt.rttnom);
1340
1341 return mr1reg;
1342}
1343
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001344static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001345{
1346 u16 mr1reg;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001347
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001348 mr1reg = make_mr1(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001349
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001350 write_mrreg(ctrl, channel, rank, 1, mr1reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001351}
1352
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001353static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001354{
1355 u16 pasr, cwl, mr2reg;
1356 odtmap odt;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001357 int srt;
1358
1359 pasr = 0;
1360 cwl = ctrl->CWL - 5;
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001361 odt = get_ODT(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001362
1363 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
1364
1365 mr2reg = 0;
1366 mr2reg = (mr2reg & ~0x7) | pasr;
1367 mr2reg = (mr2reg & ~0x38) | (cwl << 3);
1368 mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6);
1369 mr2reg = (mr2reg & ~0x80) | (srt << 7);
1370 mr2reg |= (odt.rttwr / 60) << 9;
1371
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001372 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001373}
1374
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001375static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001376{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001377 write_mrreg(ctrl, channel, rank, 3, 0);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001378}
1379
1380static void dram_mrscommands(ramctr_timing * ctrl)
1381{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001382 u8 slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001383 u32 reg, addr;
1384 int channel;
1385
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001386 FOR_ALL_POPULATED_CHANNELS {
1387 FOR_ALL_POPULATED_RANKS {
1388 // MR2
1389 dram_mr2(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001390
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001391 // MR3
1392 dram_mr3(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001393
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001394 // MR1
1395 dram_mr1(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001396
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001397 // MR0
1398 dram_mr0(ctrl, slotrank, channel);
1399 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001400 }
1401
Patrick Rudolph371d2912015-10-09 13:33:25 +02001402 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001403 write32(DEFAULT_MCHBAR + 0x4e20, 0x7);
1404 write32(DEFAULT_MCHBAR + 0x4e30, 0xf1001);
1405 write32(DEFAULT_MCHBAR + 0x4e00, 0x60002);
1406 write32(DEFAULT_MCHBAR + 0x4e10, 0);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001407
1408 /* DRAM command ZQCL */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001409 write32(DEFAULT_MCHBAR + 0x4e24, 0x1f003);
1410 write32(DEFAULT_MCHBAR + 0x4e34, 0x1901001);
1411 write32(DEFAULT_MCHBAR + 0x4e04, 0x60400);
1412 write32(DEFAULT_MCHBAR + 0x4e14, 0x288);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001413
1414 /* execute command queue on all channels ? */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001415 write32(DEFAULT_MCHBAR + 0x4e84, 0x40004);
1416
1417 // Drain
1418 FOR_ALL_CHANNELS {
1419 // Wait for ref drained
1420 wait_428c(channel);
1421 }
1422
1423 // Refresh enable
1424 MCHBAR32(0x5030) |= 8;
1425
1426 FOR_ALL_POPULATED_CHANNELS {
1427 addr = 0x400 * channel + 0x4020;
1428 reg = MCHBAR32(addr);
1429 reg &= ~0x200000;
1430 MCHBAR32(addr) = reg;
1431
1432 wait_428c(channel);
1433
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001434 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001435
1436 // Drain
1437 wait_428c(channel);
1438
Patrick Rudolph371d2912015-10-09 13:33:25 +02001439 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001440 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1441 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
1442 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001443 (slotrank << 24) | 0x60000);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001444 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
1445 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x1);
1446
1447 // Drain
1448 wait_428c(channel);
1449 }
1450}
1451
1452const u32 lane_registers[] = {
1453 0x0000, 0x0200, 0x0400, 0x0600,
1454 0x1000, 0x1200, 0x1400, 0x1600,
1455 0x0800
1456};
1457
1458static void program_timings(ramctr_timing * ctrl, int channel)
1459{
1460 u32 reg32, reg_4024, reg_c14, reg_c18, reg_4028;
1461 int lane;
1462 int slotrank, slot;
1463 int full_shift = 0;
1464 u16 slot320c[NUM_SLOTS];
1465
1466 FOR_ALL_POPULATED_RANKS {
1467 if (full_shift < -ctrl->timings[channel][slotrank].val_320c)
1468 full_shift = -ctrl->timings[channel][slotrank].val_320c;
1469 }
1470
1471 for (slot = 0; slot < NUM_SLOTS; slot++)
1472 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
1473 case 0:
1474 default:
1475 slot320c[slot] = 0x7f;
1476 break;
1477 case 1:
1478 slot320c[slot] =
1479 ctrl->timings[channel][2 * slot + 0].val_320c +
1480 full_shift;
1481 break;
1482 case 2:
1483 slot320c[slot] =
1484 ctrl->timings[channel][2 * slot + 1].val_320c +
1485 full_shift;
1486 break;
1487 case 3:
1488 slot320c[slot] =
1489 (ctrl->timings[channel][2 * slot].val_320c +
1490 ctrl->timings[channel][2 * slot +
1491 1].val_320c) / 2 +
1492 full_shift;
1493 break;
1494 }
1495
1496 reg32 = (1 << 17) | (1 << 14);
1497 reg32 |= ((slot320c[0] & 0x3f) << 6) | ((slot320c[0] & 0x40) << 9);
1498 reg32 |= (slot320c[1] & 0x7f) << 18;
1499 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
1500
1501 MCHBAR32(0x320c + 0x100 * channel) = reg32;
1502
1503 reg_c14 = ctrl->rankmap[channel] << 24;
1504 reg_c18 = 0;
1505
1506 FOR_ALL_POPULATED_RANKS {
1507 int shift =
1508 ctrl->timings[channel][slotrank].val_320c + full_shift;
1509 int offset_val_c14;
1510 if (shift < 0)
1511 shift = 0;
1512 offset_val_c14 = ctrl->reg_c14_offset + shift;
1513 reg_c14 |= (offset_val_c14 & 0x3f) << (6 * slotrank);
1514 reg_c18 |= ((offset_val_c14 >> 6) & 1) << slotrank;
1515 }
1516
1517 MCHBAR32(0xc14 + channel * 0x100) = reg_c14;
1518 MCHBAR32(0xc18 + channel * 0x100) = reg_c18;
1519
1520 reg_4028 = MCHBAR32(0x4028 + 0x400 * channel);
1521 reg_4028 &= 0xffff0000;
1522
1523 reg_4024 = 0;
1524
1525 FOR_ALL_POPULATED_RANKS {
1526 int post_timA_min_high = 7, post_timA_max_high = 0;
1527 int pre_timA_min_high = 7, pre_timA_max_high = 0;
1528 int shift_402x = 0;
1529 int shift =
1530 ctrl->timings[channel][slotrank].val_320c + full_shift;
1531
1532 if (shift < 0)
1533 shift = 0;
1534
1535 FOR_ALL_LANES {
1536 if (post_timA_min_high >
1537 ((ctrl->timings[channel][slotrank].lanes[lane].
1538 timA + shift) >> 6))
1539 post_timA_min_high =
1540 ((ctrl->timings[channel][slotrank].
1541 lanes[lane].timA + shift) >> 6);
1542 if (pre_timA_min_high >
1543 (ctrl->timings[channel][slotrank].lanes[lane].
1544 timA >> 6))
1545 pre_timA_min_high =
1546 (ctrl->timings[channel][slotrank].
1547 lanes[lane].timA >> 6);
1548 if (post_timA_max_high <
1549 ((ctrl->timings[channel][slotrank].lanes[lane].
1550 timA + shift) >> 6))
1551 post_timA_max_high =
1552 ((ctrl->timings[channel][slotrank].
1553 lanes[lane].timA + shift) >> 6);
1554 if (pre_timA_max_high <
1555 (ctrl->timings[channel][slotrank].lanes[lane].
1556 timA >> 6))
1557 pre_timA_max_high =
1558 (ctrl->timings[channel][slotrank].
1559 lanes[lane].timA >> 6);
1560 }
1561
1562 if (pre_timA_max_high - pre_timA_min_high <
1563 post_timA_max_high - post_timA_min_high)
1564 shift_402x = +1;
1565 else if (pre_timA_max_high - pre_timA_min_high >
1566 post_timA_max_high - post_timA_min_high)
1567 shift_402x = -1;
1568
1569 reg_4028 |=
1570 (ctrl->timings[channel][slotrank].val_4028 + shift_402x -
1571 post_timA_min_high) << (4 * slotrank);
1572 reg_4024 |=
1573 (ctrl->timings[channel][slotrank].val_4024 +
1574 shift_402x) << (8 * slotrank);
1575
1576 FOR_ALL_LANES {
1577 MCHBAR32(lane_registers[lane] + 0x10 + 0x100 * channel +
1578 4 * slotrank)
1579 =
1580 (((ctrl->timings[channel][slotrank].lanes[lane].
1581 timA + shift) & 0x3f)
1582 |
1583 ((ctrl->timings[channel][slotrank].lanes[lane].
1584 rising + shift) << 8)
1585 |
1586 (((ctrl->timings[channel][slotrank].lanes[lane].
1587 timA + shift -
1588 (post_timA_min_high << 6)) & 0x1c0) << 10)
1589 | (ctrl->timings[channel][slotrank].lanes[lane].
1590 falling << 20));
1591
1592 MCHBAR32(lane_registers[lane] + 0x20 + 0x100 * channel +
1593 4 * slotrank)
1594 =
1595 (((ctrl->timings[channel][slotrank].lanes[lane].
1596 timC + shift) & 0x3f)
1597 |
1598 (((ctrl->timings[channel][slotrank].lanes[lane].
1599 timB + shift) & 0x3f) << 8)
1600 |
1601 (((ctrl->timings[channel][slotrank].lanes[lane].
1602 timB + shift) & 0x1c0) << 9)
1603 |
1604 (((ctrl->timings[channel][slotrank].lanes[lane].
1605 timC + shift) & 0x40) << 13));
1606 }
1607 }
1608 MCHBAR32(0x4024 + 0x400 * channel) = reg_4024;
1609 MCHBAR32(0x4028 + 0x400 * channel) = reg_4028;
1610}
1611
1612static void test_timA(ramctr_timing * ctrl, int channel, int slotrank)
1613{
1614 wait_428c(channel);
1615
Patrick Rudolph371d2912015-10-09 13:33:25 +02001616 /* DRAM command MRS
1617 * write MR3 MPR enable
1618 * in this mode only RD and RDA are allowed
1619 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001620 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
1621 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1622 (0xc01 | (ctrl->tMOD << 16)));
1623 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1624 (slotrank << 24) | 0x360004);
1625 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1626
Patrick Rudolph371d2912015-10-09 13:33:25 +02001627 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001628 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
1629 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x4040c01);
1630 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, (slotrank << 24));
1631 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1632
Patrick Rudolph371d2912015-10-09 13:33:25 +02001633 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001634 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
1635 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1636 0x100f | ((ctrl->CAS + 36) << 16));
1637 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1638 (slotrank << 24) | 0x60000);
1639 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1640
Patrick Rudolph371d2912015-10-09 13:33:25 +02001641 /* DRAM command MRS
1642 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001643 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
1644 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1645 (0xc01 | (ctrl->tMOD << 16)));
1646 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1647 (slotrank << 24) | 0x360000);
1648 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1649
1650 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1651
1652 wait_428c(channel);
1653}
1654
1655static int does_lane_work(ramctr_timing * ctrl, int channel, int slotrank,
1656 int lane)
1657{
1658 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
1659 return ((read32
1660 (DEFAULT_MCHBAR + lane_registers[lane] + channel * 0x100 + 4 +
1661 ((timA / 32) & 1) * 4)
1662 >> (timA % 32)) & 1);
1663}
1664
1665struct run {
1666 int middle;
1667 int end;
1668 int start;
1669 int all;
1670 int length;
1671};
1672
1673static struct run get_longest_zero_run(int *seq, int sz)
1674{
1675 int i, ls;
1676 int bl = 0, bs = 0;
1677 struct run ret;
1678
1679 ls = 0;
1680 for (i = 0; i < 2 * sz; i++)
1681 if (seq[i % sz]) {
1682 if (i - ls > bl) {
1683 bl = i - ls;
1684 bs = ls;
1685 }
1686 ls = i + 1;
1687 }
1688 if (bl == 0) {
1689 ret.middle = sz / 2;
1690 ret.start = 0;
1691 ret.end = sz;
1692 ret.all = 1;
1693 return ret;
1694 }
1695
1696 ret.start = bs % sz;
1697 ret.end = (bs + bl - 1) % sz;
1698 ret.middle = (bs + (bl - 1) / 2) % sz;
1699 ret.length = bl;
1700 ret.all = 0;
1701
1702 return ret;
1703}
1704
1705static void discover_timA_coarse(ramctr_timing * ctrl, int channel,
1706 int slotrank, int *upperA)
1707{
1708 int timA;
1709 int statistics[NUM_LANES][128];
1710 int lane;
1711
1712 for (timA = 0; timA < 128; timA++) {
1713 FOR_ALL_LANES {
1714 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1715 }
1716 program_timings(ctrl, channel);
1717
1718 test_timA(ctrl, channel, slotrank);
1719
1720 FOR_ALL_LANES {
1721 statistics[lane][timA] =
1722 !does_lane_work(ctrl, channel, slotrank, lane);
1723 printram("Astat: %d, %d, %d, %x, %x\n",
1724 channel, slotrank, lane, timA,
1725 statistics[lane][timA]);
1726 }
1727 }
1728 FOR_ALL_LANES {
1729 struct run rn = get_longest_zero_run(statistics[lane], 128);
1730 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1731 upperA[lane] = rn.end;
1732 if (upperA[lane] < rn.middle)
1733 upperA[lane] += 128;
1734 printram("Aval: %d, %d, %d, %x\n", channel, slotrank,
1735 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
1736 printram("Aend: %d, %d, %d, %x\n", channel, slotrank,
1737 lane, upperA[lane]);
1738 }
1739}
1740
1741static void discover_timA_fine(ramctr_timing * ctrl, int channel, int slotrank,
1742 int *upperA)
1743{
1744 int timA_delta;
1745 int statistics[NUM_LANES][51];
1746 int lane, i;
1747
1748 memset(statistics, 0, sizeof(statistics));
1749
1750 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
1751 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
1752 timA = upperA[lane] + timA_delta + 0x40;
1753 program_timings(ctrl, channel);
1754
1755 for (i = 0; i < 100; i++) {
1756 test_timA(ctrl, channel, slotrank);
1757 FOR_ALL_LANES {
1758 statistics[lane][timA_delta + 25] +=
1759 does_lane_work(ctrl, channel, slotrank,
1760 lane);
1761 }
1762 }
1763 }
1764 FOR_ALL_LANES {
1765 int last_zero, first_all;
1766
1767 for (last_zero = -25; last_zero <= 25; last_zero++)
1768 if (statistics[lane][last_zero + 25])
1769 break;
1770 last_zero--;
1771 for (first_all = -25; first_all <= 25; first_all++)
1772 if (statistics[lane][first_all + 25] == 100)
1773 break;
1774
1775 printram("lane %d: %d, %d\n", lane, last_zero,
1776 first_all);
1777
1778 ctrl->timings[channel][slotrank].lanes[lane].timA =
1779 (last_zero + first_all) / 2 + upperA[lane];
1780 printram("Aval: %d, %d, %d, %x\n", channel, slotrank,
1781 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
1782 }
1783}
1784
1785static void discover_402x(ramctr_timing * ctrl, int channel, int slotrank,
1786 int *upperA)
1787{
1788 int works[NUM_LANES];
1789 int lane;
1790 while (1) {
1791 int all_works = 1, some_works = 0;
1792 program_timings(ctrl, channel);
1793 test_timA(ctrl, channel, slotrank);
1794 FOR_ALL_LANES {
1795 works[lane] =
1796 !does_lane_work(ctrl, channel, slotrank, lane);
1797 if (works[lane])
1798 some_works = 1;
1799 else
1800 all_works = 0;
1801 }
1802 if (all_works)
1803 return;
1804 if (!some_works) {
1805 if (ctrl->timings[channel][slotrank].val_4024 < 2)
1806 die("402x discovery failed");
1807 ctrl->timings[channel][slotrank].val_4024 -= 2;
1808 printram("4024 -= 2;\n");
1809 continue;
1810 }
1811 ctrl->timings[channel][slotrank].val_4028 += 2;
1812 printram("4028 += 2;\n");
1813 if (ctrl->timings[channel][slotrank].val_4028 >= 0x10)
1814 die("402x discovery failed");
1815 FOR_ALL_LANES if (works[lane]) {
1816 ctrl->timings[channel][slotrank].lanes[lane].timA +=
1817 128;
1818 upperA[lane] += 128;
1819 printram("increment %d, %d, %d\n", channel,
1820 slotrank, lane);
1821 }
1822 }
1823}
1824
1825struct timA_minmax {
1826 int timA_min_high, timA_max_high;
1827};
1828
1829static void pre_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
1830 struct timA_minmax *mnmx)
1831{
1832 int lane;
1833 mnmx->timA_min_high = 7;
1834 mnmx->timA_max_high = 0;
1835
1836 FOR_ALL_LANES {
1837 if (mnmx->timA_min_high >
1838 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1839 mnmx->timA_min_high =
1840 (ctrl->timings[channel][slotrank].lanes[lane].
1841 timA >> 6);
1842 if (mnmx->timA_max_high <
1843 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1844 mnmx->timA_max_high =
1845 (ctrl->timings[channel][slotrank].lanes[lane].
1846 timA >> 6);
1847 }
1848}
1849
1850static void post_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
1851 struct timA_minmax *mnmx)
1852{
1853 struct timA_minmax post;
1854 int shift_402x = 0;
1855
1856 /* Get changed maxima. */
1857 pre_timA_change(ctrl, channel, slotrank, &post);
1858
1859 if (mnmx->timA_max_high - mnmx->timA_min_high <
1860 post.timA_max_high - post.timA_min_high)
1861 shift_402x = +1;
1862 else if (mnmx->timA_max_high - mnmx->timA_min_high >
1863 post.timA_max_high - post.timA_min_high)
1864 shift_402x = -1;
1865 else
1866 shift_402x = 0;
1867
1868 ctrl->timings[channel][slotrank].val_4028 += shift_402x;
1869 ctrl->timings[channel][slotrank].val_4024 += shift_402x;
1870 printram("4024 += %d;\n", shift_402x);
1871 printram("4028 += %d;\n", shift_402x);
1872}
1873
Patrick Rudolph371d2912015-10-09 13:33:25 +02001874/* Compensate the skew between DQS and DQs.
1875 * To ease PCB design a small skew between Data Strobe signals and
1876 * Data Signals is allowed.
1877 * The controller has to measure and compensate this skew for every byte-lane.
1878 * By delaying either all DQs signals or DQS signal, a full phase
1879 * shift can be introduced.
1880 * It is assumed that one byte-lane's DQs signals have the same routing delay.
1881 *
1882 * To measure the actual skew, the DRAM is placed in "read leveling" mode.
1883 * In read leveling mode the DRAM-chip outputs an alternating periodic pattern.
1884 * The memory controller iterates over all possible values to do a full phase shift
1885 * and issues read commands.
1886 * With DQS and DQs in phase the data read is expected to alternate on every byte:
1887 * 0xFF 0x00 0xFF ...
1888 * Once the controller has detected this pattern a bit in the result register is
1889 * set for the current phase shift.
1890 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001891static void read_training(ramctr_timing * ctrl)
1892{
1893 int channel, slotrank, lane;
1894
1895 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001896 int all_high, some_high;
1897 int upperA[NUM_LANES];
1898 struct timA_minmax mnmx;
1899
1900 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001901
1902 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001903 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1904 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1905 0xc01 | (ctrl->tRP << 16));
1906 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1907 (slotrank << 24) | 0x60400);
1908 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1909 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
1910
1911 write32(DEFAULT_MCHBAR + 0x3400, (slotrank << 2) | 0x8001);
1912
1913 ctrl->timings[channel][slotrank].val_4028 = 4;
1914 ctrl->timings[channel][slotrank].val_4024 = 55;
1915 program_timings(ctrl, channel);
1916
1917 discover_timA_coarse(ctrl, channel, slotrank, upperA);
1918
1919 all_high = 1;
1920 some_high = 0;
1921 FOR_ALL_LANES {
1922 if (ctrl->timings[channel][slotrank].lanes[lane].
1923 timA >= 0x40)
1924 some_high = 1;
1925 else
1926 all_high = 0;
1927 }
1928
1929 if (all_high) {
1930 ctrl->timings[channel][slotrank].val_4028--;
1931 printram("4028--;\n");
1932 FOR_ALL_LANES {
1933 ctrl->timings[channel][slotrank].lanes[lane].
1934 timA -= 0x40;
1935 upperA[lane] -= 0x40;
1936
1937 }
1938 } else if (some_high) {
1939 ctrl->timings[channel][slotrank].val_4024++;
1940 ctrl->timings[channel][slotrank].val_4028++;
1941 printram("4024++;\n");
1942 printram("4028++;\n");
1943 }
1944
1945 program_timings(ctrl, channel);
1946
1947 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1948
1949 discover_402x(ctrl, channel, slotrank, upperA);
1950
1951 post_timA_change(ctrl, channel, slotrank, &mnmx);
1952 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1953
1954 discover_timA_fine(ctrl, channel, slotrank, upperA);
1955
1956 post_timA_change(ctrl, channel, slotrank, &mnmx);
1957 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1958
1959 FOR_ALL_LANES {
1960 ctrl->timings[channel][slotrank].lanes[lane].timA -= mnmx.timA_min_high * 0x40;
1961 }
1962 ctrl->timings[channel][slotrank].val_4028 -= mnmx.timA_min_high;
1963 printram("4028 -= %d;\n", mnmx.timA_min_high);
1964
1965 post_timA_change(ctrl, channel, slotrank, &mnmx);
1966
1967 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
1968 ctrl->timings[channel][slotrank].val_4024,
1969 ctrl->timings[channel][slotrank].val_4028);
1970
1971 FOR_ALL_LANES
1972 printram("%d, %d, %d, %x\n", channel, slotrank,
1973 lane,
1974 ctrl->timings[channel][slotrank].lanes[lane].timA);
1975
1976 write32(DEFAULT_MCHBAR + 0x3400, 0);
1977
Patrick Rudolph9b515682015-10-09 13:43:51 +02001978 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001979 }
1980
1981 FOR_ALL_POPULATED_CHANNELS {
1982 program_timings(ctrl, channel);
1983 }
1984 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
1985 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
1986 + 4 * lane, 0);
1987 }
1988}
1989
1990static void test_timC(ramctr_timing * ctrl, int channel, int slotrank)
1991{
1992 int lane;
1993
1994 FOR_ALL_LANES {
1995 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + 4 * lane, 0);
1996 read32(DEFAULT_MCHBAR + 0x4140 + 0x400 * channel + 4 * lane);
1997 }
1998
1999 wait_428c(channel);
2000
Patrick Rudolph371d2912015-10-09 13:33:25 +02002001 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002002 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2003 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2004 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD) << 10)
2005 | 4 | (ctrl->tRCD << 16));
2006
2007 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2008 (slotrank << 24) | (6 << 16));
2009
2010 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
2011
Patrick Rudolph371d2912015-10-09 13:33:25 +02002012 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002013 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2014 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8041001);
2015 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2016 (slotrank << 24) | 8);
2017 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2018
Patrick Rudolph371d2912015-10-09 13:33:25 +02002019 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002020 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2021 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x80411f4);
2022 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
2023 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
2024
Patrick Rudolph371d2912015-10-09 13:33:25 +02002025 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002026 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2027 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2028 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2029 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2030 (slotrank << 24) | 8);
2031 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2032
2033 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2034
2035 wait_428c(channel);
2036
Patrick Rudolph371d2912015-10-09 13:33:25 +02002037 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002038 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2039 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2040 0xc01 | (ctrl->tRP << 16));
2041 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2042 (slotrank << 24) | 0x60400);
2043 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2044
Patrick Rudolph371d2912015-10-09 13:33:25 +02002045 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002046 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2047 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2048 (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) << 10)
2049 | 8 | (ctrl->CAS << 16));
2050
2051 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2052 (slotrank << 24) | 0x60000);
2053
2054 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x244);
2055
Patrick Rudolph371d2912015-10-09 13:33:25 +02002056 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002057 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2058 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2059 0x40011f4 | (max(ctrl->tRTP, 8) << 16));
2060 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
2061 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
2062
Patrick Rudolph371d2912015-10-09 13:33:25 +02002063 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002064 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2065 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2066 0xc01 | (ctrl->tRP << 16));
2067 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2068 (slotrank << 24) | 0x60400);
2069 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2070 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2071 wait_428c(channel);
2072}
2073
2074static void discover_timC(ramctr_timing * ctrl, int channel, int slotrank)
2075{
2076 int timC;
2077 int statistics[NUM_LANES][MAX_TIMC + 1];
2078 int lane;
2079
2080 wait_428c(channel);
2081
Patrick Rudolph371d2912015-10-09 13:33:25 +02002082 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002083 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2084 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2085 0xc01 | (ctrl->tRP << 16));
2086 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2087 (slotrank << 24) | 0x60400);
2088 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2089 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2090
2091 for (timC = 0; timC <= MAX_TIMC; timC++) {
2092 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
2093 timC = timC;
2094 program_timings(ctrl, channel);
2095
2096 test_timC(ctrl, channel, slotrank);
2097
2098 FOR_ALL_LANES {
2099 statistics[lane][timC] =
2100 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2101 0x400 * channel);
2102 printram("Cstat: %d, %d, %d, %x, %x\n",
2103 channel, slotrank, lane, timC,
2104 statistics[lane][timC]);
2105 }
2106 }
2107 FOR_ALL_LANES {
2108 struct run rn =
2109 get_longest_zero_run(statistics[lane], MAX_TIMC + 1);
2110 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
2111 if (rn.all)
2112 printk(BIOS_CRIT, "timC discovery failed");
2113 printram("Cval: %d, %d, %d, %x\n", channel, slotrank,
2114 lane, ctrl->timings[channel][slotrank].lanes[lane].timC);
2115 }
2116}
2117
2118static int get_precedening_channels(ramctr_timing * ctrl, int target_channel)
2119{
2120 int channel, ret = 0;
2121 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
2122 ret++;
2123 return ret;
2124}
2125
2126static void fill_pattern0(ramctr_timing * ctrl, int channel, u32 a, u32 b)
2127{
2128 unsigned j;
2129 unsigned channel_offset =
2130 get_precedening_channels(ctrl, channel) * 0x40;
2131 printram("channel_offset=%x\n", channel_offset);
2132 for (j = 0; j < 16; j++)
2133 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
2134 sfence();
2135}
2136
2137static int num_of_channels(const ramctr_timing * ctrl)
2138{
2139 int ret = 0;
2140 int channel;
2141 FOR_ALL_POPULATED_CHANNELS ret++;
2142 return ret;
2143}
2144
2145static void fill_pattern1(ramctr_timing * ctrl, int channel)
2146{
2147 unsigned j;
2148 unsigned channel_offset =
2149 get_precedening_channels(ctrl, channel) * 0x40;
2150 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2151 for (j = 0; j < 16; j++)
2152 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
2153 for (j = 0; j < 16; j++)
2154 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
2155 sfence();
2156}
2157
2158static void precharge(ramctr_timing * ctrl)
2159{
2160 int channel, slotrank, lane;
2161
2162 FOR_ALL_POPULATED_CHANNELS {
2163 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2164 ctrl->timings[channel][slotrank].lanes[lane].falling =
2165 16;
2166 ctrl->timings[channel][slotrank].lanes[lane].rising =
2167 16;
2168 } program_timings(ctrl, channel);
2169
2170 FOR_ALL_POPULATED_RANKS {
2171 wait_428c(channel);
2172
Patrick Rudolph371d2912015-10-09 13:33:25 +02002173 /* DRAM command MRS
2174 * write MR3 MPR enable
2175 * in this mode only RD and RDA are allowed
2176 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002177 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2178 0x1f000);
2179 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2180 0xc01 | (ctrl->tMOD << 16));
2181 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2182 (slotrank << 24) | 0x360004);
2183 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2184
Patrick Rudolph371d2912015-10-09 13:33:25 +02002185 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002186 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2187 0x1f105);
2188 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2189 0x4041003);
2190 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2191 (slotrank << 24) | 0);
2192 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2193
Patrick Rudolph371d2912015-10-09 13:33:25 +02002194 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002195 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2196 0x1f105);
2197 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2198 0x1001 | ((ctrl->CAS + 8) << 16));
2199 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2200 (slotrank << 24) | 0x60000);
2201 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2202
Patrick Rudolph371d2912015-10-09 13:33:25 +02002203 /* DRAM command MRS
2204 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002205 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2206 0x1f000);
2207 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2208 0xc01 | (ctrl->tMOD << 16));
2209 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2210 (slotrank << 24) | 0x360000);
2211 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2212 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2213 0xc0001);
2214
2215 wait_428c(channel);
2216 }
2217
2218 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2219 ctrl->timings[channel][slotrank].lanes[lane].falling =
2220 48;
2221 ctrl->timings[channel][slotrank].lanes[lane].rising =
2222 48;
2223 }
2224
2225 program_timings(ctrl, channel);
2226
2227 FOR_ALL_POPULATED_RANKS {
2228 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002229 /* DRAM command MRS
2230 * write MR3 MPR enable
2231 * in this mode only RD and RDA are allowed
2232 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002233 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2234 0x1f000);
2235 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2236 0xc01 | (ctrl->tMOD << 16));
2237 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2238 (slotrank << 24) | 0x360004);
2239 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2240
Patrick Rudolph371d2912015-10-09 13:33:25 +02002241 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002242 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2243 0x1f105);
2244 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2245 0x4041003);
2246 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2247 (slotrank << 24) | 0);
2248 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2249
Patrick Rudolph371d2912015-10-09 13:33:25 +02002250 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002251 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2252 0x1f105);
2253 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2254 0x1001 | ((ctrl->CAS + 8) << 16));
2255 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2256 (slotrank << 24) | 0x60000);
2257 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2258
Patrick Rudolph371d2912015-10-09 13:33:25 +02002259 /* DRAM command MRS
2260 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002261 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2262 0x1f000);
2263 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2264 0xc01 | (ctrl->tMOD << 16));
2265
2266 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2267 (slotrank << 24) | 0x360000);
2268 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2269
2270 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2271 0xc0001);
2272 wait_428c(channel);
2273 }
2274 }
2275}
2276
2277static void test_timB(ramctr_timing * ctrl, int channel, int slotrank)
2278{
Patrick Rudolph371d2912015-10-09 13:33:25 +02002279 /* enable DQs on this slotrank */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002280 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002281 0x80 | make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002282
2283 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002284 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002285 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f207);
2286 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2287 0x8000c01 | ((ctrl->CWL + ctrl->tWLO) << 16));
2288 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2289 8 | (slotrank << 24));
2290 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2291
Patrick Rudolph371d2912015-10-09 13:33:25 +02002292 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002293 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f107);
2294 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2295 0x4000c01 | ((ctrl->CAS + 38) << 16));
2296 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2297 (slotrank << 24) | 4);
2298 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2299
2300 write32(DEFAULT_MCHBAR + 0x400 * channel + 0x4284, 0x40001);
2301 wait_428c(channel);
2302
Patrick Rudolph371d2912015-10-09 13:33:25 +02002303 /* disable DQs on this slotrank */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002304 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002305 0x1080 | make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002306}
2307
2308static void discover_timB(ramctr_timing * ctrl, int channel, int slotrank)
2309{
2310 int timB;
2311 int statistics[NUM_LANES][128];
2312 int lane;
2313
2314 write32(DEFAULT_MCHBAR + 0x3400, 0x108052 | (slotrank << 2));
2315
2316 for (timB = 0; timB < 128; timB++) {
2317 FOR_ALL_LANES {
2318 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
2319 }
2320 program_timings(ctrl, channel);
2321
2322 test_timB(ctrl, channel, slotrank);
2323
2324 FOR_ALL_LANES {
2325 statistics[lane][timB] =
2326 !((read32
2327 (DEFAULT_MCHBAR + lane_registers[lane] +
2328 channel * 0x100 + 4 + ((timB / 32) & 1) * 4)
2329 >> (timB % 32)) & 1);
2330 printram("Bstat: %d, %d, %d, %x, %x\n",
2331 channel, slotrank, lane, timB,
2332 statistics[lane][timB]);
2333 }
2334 }
2335 FOR_ALL_LANES {
2336 struct run rn = get_longest_zero_run(statistics[lane], 128);
Patrick Rudolph9f1fbb92015-08-17 19:24:12 +02002337 if (rn.start < rn.middle) {
2338 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
2339 } else {
2340 /* In this case statistics[lane][7f] and statistics[lane][0] are
2341 * both zero.
2342 * Prefer a smaller value over rn.start to prevent failures in
2343 * the following write tests.
2344 */
2345 ctrl->timings[channel][slotrank].lanes[lane].timB = 0;
2346 }
2347
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002348 if (rn.all)
2349 die("timB discovery failed");
2350 printram("Bval: %d, %d, %d, %x\n", channel, slotrank,
2351 lane, ctrl->timings[channel][slotrank].lanes[lane].timB);
2352 }
2353}
2354
2355static int get_timB_high_adjust(u64 val)
2356{
2357 int i;
2358
2359 /* good */
2360 if (val == 0xffffffffffffffffLL)
2361 return 0;
2362
2363 if (val >= 0xf000000000000000LL) {
2364 /* needs negative adjustment */
2365 for (i = 0; i < 8; i++)
2366 if (val << (8 * (7 - i) + 4))
2367 return -i;
2368 } else {
2369 /* needs positive adjustment */
2370 for (i = 0; i < 8; i++)
2371 if (val >> (8 * (7 - i) + 4))
2372 return i;
2373 }
2374 return 8;
2375}
2376
2377static void adjust_high_timB(ramctr_timing * ctrl)
2378{
2379 int channel, slotrank, lane, old;
2380 write32(DEFAULT_MCHBAR + 0x3400, 0x200);
2381 FOR_ALL_POPULATED_CHANNELS {
2382 fill_pattern1(ctrl, channel);
2383 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 1);
2384 }
2385 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2386
2387 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x10001);
2388
2389 wait_428c(channel);
2390
Patrick Rudolph371d2912015-10-09 13:33:25 +02002391 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002392 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2393 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2394 0xc01 | (ctrl->tRCD << 16));
2395 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2396 (slotrank << 24) | 0x60000);
2397 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2398
Patrick Rudolph371d2912015-10-09 13:33:25 +02002399 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002400 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2401 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8040c01);
2402 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2403 (slotrank << 24) | 0x8);
2404 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2405
Patrick Rudolph371d2912015-10-09 13:33:25 +02002406 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002407 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2408 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x8041003);
2409 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2410 (slotrank << 24));
2411 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x3e2);
2412
Patrick Rudolph371d2912015-10-09 13:33:25 +02002413 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002414 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2415 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2416 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2417 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2418 (slotrank << 24) | 0x8);
2419 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2420
2421 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2422
2423 wait_428c(channel);
2424
Patrick Rudolph371d2912015-10-09 13:33:25 +02002425 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002426 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2427 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2428 0xc01 | ((ctrl->tRP) << 16));
2429 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2430 (slotrank << 24) | 0x60400);
2431 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2432
Patrick Rudolph371d2912015-10-09 13:33:25 +02002433 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002434 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2435 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2436 0xc01 | ((ctrl->tRCD) << 16));
2437 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2438 (slotrank << 24) | 0x60000);
2439 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2440
Patrick Rudolph371d2912015-10-09 13:33:25 +02002441 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002442 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x3f105);
2443 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2444 0x4000c01 |
2445 ((ctrl->tRP +
2446 ctrl->timings[channel][slotrank].val_4024 +
2447 ctrl->timings[channel][slotrank].val_4028) << 16));
2448 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2449 (slotrank << 24) | 0x60008);
2450 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2451
2452 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
2453 wait_428c(channel);
2454 FOR_ALL_LANES {
2455 u64 res =
2456 read32(DEFAULT_MCHBAR + lane_registers[lane] +
2457 0x100 * channel + 4);
2458 res |=
2459 ((u64) read32(DEFAULT_MCHBAR + lane_registers[lane] +
2460 0x100 * channel + 8)) << 32;
2461 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
2462 ctrl->timings[channel][slotrank].lanes[lane].timB +=
2463 get_timB_high_adjust(res) * 64;
2464
2465 printk(BIOS_DEBUG, "High adjust %d:%016llx\n", lane, res);
2466 printram("Bval+: %d, %d, %d, %x -> %x\n", channel,
2467 slotrank, lane, old,
2468 ctrl->timings[channel][slotrank].lanes[lane].
2469 timB);
2470 }
2471 }
2472 write32(DEFAULT_MCHBAR + 0x3400, 0);
2473}
2474
2475static void write_op(ramctr_timing * ctrl, int channel)
2476{
2477 int slotrank;
2478
2479 wait_428c(channel);
2480
2481 /* choose an existing rank. */
2482 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2483
Patrick Rudolph371d2912015-10-09 13:33:25 +02002484 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002485 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2486 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2487
2488 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2489 (slotrank << 24) | 0x60000);
2490
2491 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2492
2493 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2494 wait_428c(channel);
2495}
2496
Patrick Rudolph371d2912015-10-09 13:33:25 +02002497/* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
2498 * DDR3 adopted the fly-by topology. The data and strobes signals reach
2499 * the chips at different times with respect to command, address and
2500 * clock signals.
2501 * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase
2502 * shift can be introduced.
2503 * It is assumed that the CLK/ADDR/CMD signals have the same routing delay.
2504 *
2505 * To find the required phase shift the DRAM is placed in "write leveling" mode.
2506 * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the
2507 * sampled value on the data lanes (DQs).
2508 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002509static void write_training(ramctr_timing * ctrl)
2510{
2511 int channel, slotrank, lane;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002512
2513 FOR_ALL_POPULATED_CHANNELS
2514 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
2515 read32(DEFAULT_MCHBAR + 0x4008 +
2516 0x400 * channel) | 0x8000000);
2517
2518 FOR_ALL_POPULATED_CHANNELS {
2519 write_op(ctrl, channel);
2520 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2521 read32(DEFAULT_MCHBAR + 0x4020 +
2522 0x400 * channel) | 0x200000);
2523 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002524
2525 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002526 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2527 FOR_ALL_POPULATED_CHANNELS {
2528 write_op(ctrl, channel);
2529 }
2530
Patrick Rudolph371d2912015-10-09 13:33:25 +02002531 /* enable write leveling on all ranks
2532 * disable all DQ outputs
2533 * only NOP is allowed in this mode */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002534 FOR_ALL_CHANNELS
2535 FOR_ALL_POPULATED_RANKS
2536 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002537 make_mr1(ctrl, slotrank, channel) | 0x1080);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002538
2539 write32(DEFAULT_MCHBAR + 0x3400, 0x108052);
2540
Patrick Rudolph9b515682015-10-09 13:43:51 +02002541 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002542
Patrick Rudolph371d2912015-10-09 13:33:25 +02002543 /* set any valid value for timB, it gets corrected later */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002544 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2545 discover_timB(ctrl, channel, slotrank);
2546
Patrick Rudolph371d2912015-10-09 13:33:25 +02002547 /* disable write leveling on all ranks */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002548 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2549 write_mrreg(ctrl, channel,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002550 slotrank, 1, make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002551
2552 write32(DEFAULT_MCHBAR + 0x3400, 0);
2553
2554 FOR_ALL_POPULATED_CHANNELS
2555 wait_428c(channel);
2556
Patrick Rudolph371d2912015-10-09 13:33:25 +02002557 /* refresh enable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002558 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) | 8);
2559
2560 FOR_ALL_POPULATED_CHANNELS {
2561 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2562 ~0x00200000 & read32(DEFAULT_MCHBAR + 0x4020 +
2563 0x400 * channel));
2564 read32(DEFAULT_MCHBAR + 0x428c + 0x400 * channel);
2565 wait_428c(channel);
2566
Patrick Rudolph371d2912015-10-09 13:33:25 +02002567 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002568 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2569 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
2570 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, 0x60000);
2571 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2572
2573 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2574 wait_428c(channel);
2575 }
2576
Patrick Rudolph9b515682015-10-09 13:43:51 +02002577 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002578
2579 printram("CPE\n");
2580 precharge(ctrl);
2581 printram("CPF\n");
2582
2583 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2584 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2585 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2586 0);
2587 }
2588
2589 FOR_ALL_POPULATED_CHANNELS {
2590 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
2591 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2592 }
2593
2594 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2595 discover_timC(ctrl, channel, slotrank);
2596
2597 FOR_ALL_POPULATED_CHANNELS
2598 program_timings(ctrl, channel);
2599
Patrick Rudolph371d2912015-10-09 13:33:25 +02002600 /* measure and adjust timB timings */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002601 adjust_high_timB(ctrl);
2602
2603 FOR_ALL_POPULATED_CHANNELS
2604 program_timings(ctrl, channel);
2605
2606 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2607 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2608 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2609 0);
2610 }
2611}
2612
2613static int test_320c(ramctr_timing * ctrl, int channel, int slotrank)
2614{
2615 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2616 int timC_delta;
2617 int lanes_ok = 0;
2618 int ctr = 0;
2619 int lane;
2620
2621 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2622 FOR_ALL_LANES {
2623 ctrl->timings[channel][slotrank].lanes[lane].timC =
2624 saved_rt.lanes[lane].timC + timC_delta;
2625 }
2626 program_timings(ctrl, channel);
2627 FOR_ALL_LANES {
2628 write32(DEFAULT_MCHBAR + 4 * lane + 0x4f40, 0);
2629 }
2630
2631 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2632
2633 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002634 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002635 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2636 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2637 ((max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1)) << 10)
2638 | 8 | (ctrl->tRCD << 16));
2639
2640 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2641 (slotrank << 24) | ctr | 0x60000);
2642
2643 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002644 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002645 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2646 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2647 0x8001020 | ((ctrl->CWL + ctrl->tWTR + 8) << 16));
2648 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2649 (slotrank << 24));
2650 write32(DEFAULT_MCHBAR + 0x4244 + 0x400 * channel, 0x389abcd);
2651 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x20e42);
2652
Patrick Rudolph371d2912015-10-09 13:33:25 +02002653 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002654 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2655 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2656 0x4001020 | (max(ctrl->tRTP, 8) << 16));
2657 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2658 (slotrank << 24));
2659 write32(DEFAULT_MCHBAR + 0x4248 + 0x400 * channel, 0x389abcd);
2660 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x20e42);
2661
Patrick Rudolph371d2912015-10-09 13:33:25 +02002662 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002663 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2664 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, 0xf1001);
2665 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2666 (slotrank << 24) | 0x60400);
2667 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2668
2669 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2670 wait_428c(channel);
2671 FOR_ALL_LANES {
2672 u32 r32 =
2673 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2674 0x400 * channel);
2675
2676 if (r32 == 0)
2677 lanes_ok |= 1 << lane;
2678 }
2679 ctr++;
2680 if (lanes_ok == ((1 << NUM_LANES) - 1))
2681 break;
2682 }
2683
2684 ctrl->timings[channel][slotrank] = saved_rt;
2685
2686 printram("3lanes: %x\n", lanes_ok);
2687 return lanes_ok != ((1 << NUM_LANES) - 1);
2688}
2689
2690#include "raminit_patterns.h"
2691
2692static void fill_pattern5(ramctr_timing * ctrl, int channel, int patno)
2693{
2694 unsigned i, j;
2695 unsigned channel_offset =
2696 get_precedening_channels(ctrl, channel) * 0x40;
2697 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2698
2699 if (patno) {
2700 u8 base8 = 0x80 >> ((patno - 1) % 8);
2701 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2702 for (i = 0; i < 32; i++) {
2703 for (j = 0; j < 16; j++) {
2704 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
2705 if (invert[patno - 1][i] & (1 << (j / 2)))
2706 val = ~val;
2707 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2708 j * 4), val);
2709 }
2710 }
2711
2712 } else {
2713 for (i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++) {
2714 for (j = 0; j < 16; j++)
2715 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2716 j * 4), pattern[i][j]);
2717 }
2718 sfence();
2719 }
2720}
2721
2722static void reprogram_320c(ramctr_timing * ctrl)
2723{
2724 int channel, slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002725
2726 FOR_ALL_POPULATED_CHANNELS {
2727 wait_428c(channel);
2728
2729 /* choose an existing rank. */
2730 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2731
Patrick Rudolph371d2912015-10-09 13:33:25 +02002732 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002733 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2734 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2735
2736 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2737 (slotrank << 24) | 0x60000);
2738
2739 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2740
2741 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2742 wait_428c(channel);
2743 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2744 read32(DEFAULT_MCHBAR + 0x4020 +
2745 0x400 * channel) | 0x200000);
2746 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002747
2748 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002749 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2750 FOR_ALL_POPULATED_CHANNELS {
2751 wait_428c(channel);
2752
2753 /* choose an existing rank. */
2754 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2755
Patrick Rudolph371d2912015-10-09 13:33:25 +02002756 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002757 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2758 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2759
2760 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2761 (slotrank << 24) | 0x60000);
2762
2763 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2764
2765 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2766 wait_428c(channel);
2767 }
2768
2769 /* jedec reset */
2770 dram_jedecreset(ctrl);
2771 /* mrs commands. */
2772 dram_mrscommands(ctrl);
2773
Patrick Rudolph9b515682015-10-09 13:43:51 +02002774 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002775}
2776
2777#define MIN_C320C_LEN 13
2778
2779static int try_cmd_stretch(ramctr_timing * ctrl, int cmd_stretch)
2780{
2781 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2782 int channel, slotrank;
2783 int c320c;
2784 int stat[NUM_SLOTRANKS][256];
2785
2786 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2787 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
2788 }
2789
2790 FOR_ALL_POPULATED_CHANNELS {
2791 ctrl->cmd_stretch[channel] = cmd_stretch;
2792 }
2793
2794 FOR_ALL_POPULATED_CHANNELS
2795 MCHBAR32(0x4004 + 0x400 * channel) =
2796 ctrl->tRRD
2797 | (ctrl->tRTP << 4)
2798 | (ctrl->tCKE << 8)
2799 | (ctrl->tWTR << 12)
2800 | (ctrl->tFAW << 16)
2801 | (ctrl->tWR << 24)
2802 | (ctrl->cmd_stretch[channel] << 30);
2803
2804
2805 FOR_ALL_CHANNELS {
2806 int delta = 0;
2807 if (ctrl->cmd_stretch[channel] == 2)
2808 delta = 2;
2809 else if (ctrl->cmd_stretch[channel] == 0)
2810 delta = 4;
2811
2812 FOR_ALL_POPULATED_RANKS {
2813 ctrl->timings[channel][slotrank].val_4024 -= delta;
2814 }
2815 }
2816
2817 FOR_ALL_POPULATED_CHANNELS {
2818 for (c320c = -127; c320c <= 127; c320c++) {
2819 FOR_ALL_POPULATED_RANKS {
2820 ctrl->timings[channel][slotrank].val_320c = c320c;
2821 }
2822 program_timings(ctrl, channel);
2823 reprogram_320c(ctrl);
2824 FOR_ALL_POPULATED_RANKS {
2825 stat[slotrank][c320c + 127] =
2826 test_320c(ctrl, channel, slotrank);
2827 printram("3stat: %d, %d, %d: %d\n",
2828 channel, slotrank, c320c,
2829 stat[slotrank][c320c + 127]);
2830 }
2831 }
2832 FOR_ALL_POPULATED_RANKS {
2833 struct run rn =
2834 get_longest_zero_run(stat[slotrank], 255);
2835 ctrl->timings[channel][slotrank].val_320c =
2836 rn.middle - 127;
2837 printram("3val: %d, %d: %d\n", channel,
2838 slotrank,
2839 ctrl->timings[channel][slotrank].val_320c);
2840 if (rn.all || rn.length < MIN_C320C_LEN) {
2841 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2842 ctrl->timings[channel][slotrank] = saved_timings[channel][slotrank];
2843 }
2844 return 0;
2845 }
2846 }
2847 }
2848 return 1;
2849}
2850
Patrick Rudolph371d2912015-10-09 13:33:25 +02002851/* Adjust CMD phase shift and try multiple command rates.
2852 * A command rate of 2T doubles the time needed for address and
2853 * command decode. */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002854static void command_training(ramctr_timing * ctrl)
2855{
2856 int channel;
2857
2858 FOR_ALL_POPULATED_CHANNELS {
2859 fill_pattern5(ctrl, channel, 0);
2860 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2861 }
2862
2863 /* try command rate 1T and 2T */
2864 if (!try_cmd_stretch(ctrl, 0) && !try_cmd_stretch(ctrl, 2))
2865 die("c320c discovery failed");
2866
2867 FOR_ALL_POPULATED_CHANNELS {
2868 program_timings(ctrl, channel);
2869 }
2870
2871 reprogram_320c(ctrl);
2872}
2873
2874static void discover_edges_real(ramctr_timing * ctrl, int channel, int slotrank,
2875 int *edges)
2876{
2877 int edge;
2878 int statistics[NUM_LANES][MAX_EDGE_TIMING + 1];
2879 int lane;
2880
2881 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2882 FOR_ALL_LANES {
2883 ctrl->timings[channel][slotrank].lanes[lane].rising =
2884 edge;
2885 ctrl->timings[channel][slotrank].lanes[lane].falling =
2886 edge;
2887 }
2888 printram("edge %02x\n", edge);
2889 program_timings(ctrl, channel);
2890
2891 FOR_ALL_LANES {
2892 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2893 4 * lane, 0);
2894 read32(DEFAULT_MCHBAR + 0x400 * channel + 4 * lane +
2895 0x4140);
2896 }
2897
2898 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002899 /* DRAM command MRS
2900 * write MR3 MPR enable
2901 * in this mode only RD and RDA are allowed
2902 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002903 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
2904 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2905 (0xc01 | (ctrl->tMOD << 16)));
2906 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2907 (slotrank << 24) | 0x360004);
2908 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2909
Patrick Rudolph371d2912015-10-09 13:33:25 +02002910 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002911 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
2912 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x40411f4);
2913 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2914 (slotrank << 24));
2915 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2916
Patrick Rudolph371d2912015-10-09 13:33:25 +02002917 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002918 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2919 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2920 0x1001 | ((ctrl->CAS + 8) << 16));
2921 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2922 (slotrank << 24) | 0x60000);
2923 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2924
Patrick Rudolph371d2912015-10-09 13:33:25 +02002925 /* DRAM command MRS
2926 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002927 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
2928 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2929 (0xc01 | (ctrl->tMOD << 16)));
2930 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2931 (slotrank << 24) | 0x360000);
2932 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2933
2934 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2935
2936 wait_428c(channel);
2937
2938 FOR_ALL_LANES {
2939 statistics[lane][edge] =
2940 read32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2941 lane * 4);
2942 }
2943 }
2944 FOR_ALL_LANES {
2945 struct run rn =
2946 get_longest_zero_run(statistics[lane], MAX_EDGE_TIMING + 1);
2947 edges[lane] = rn.middle;
2948 if (rn.all)
2949 die("edge discovery failed");
2950 printram("eval %d, %d, %d, %02x\n", channel, slotrank,
2951 lane, edges[lane]);
2952 }
2953}
2954
2955static void discover_edges(ramctr_timing * ctrl)
2956{
2957 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2958 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2959 int channel, slotrank, lane;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002960
2961 write32(DEFAULT_MCHBAR + 0x3400, 0);
2962
Patrick Rudolph9b515682015-10-09 13:43:51 +02002963 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002964
2965 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
2966 write32(DEFAULT_MCHBAR + 4 * lane +
2967 0x400 * channel + 0x4080, 0);
2968 }
2969
2970 FOR_ALL_POPULATED_CHANNELS {
2971 fill_pattern0(ctrl, channel, 0, 0);
2972 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2973 FOR_ALL_LANES {
2974 read32(DEFAULT_MCHBAR + 0x400 * channel +
2975 lane * 4 + 0x4140);
2976 }
2977
2978 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2979 ctrl->timings[channel][slotrank].lanes[lane].falling =
2980 16;
2981 ctrl->timings[channel][slotrank].lanes[lane].rising =
2982 16;
2983 }
2984
2985 program_timings(ctrl, channel);
2986
2987 FOR_ALL_POPULATED_RANKS {
2988 wait_428c(channel);
2989
Patrick Rudolph371d2912015-10-09 13:33:25 +02002990 /* DRAM command MRS
2991 * MR3 enable MPR
2992 * write MR3 MPR enable
2993 * in this mode only RD and RDA are allowed
2994 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002995 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2996 0x1f000);
2997 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2998 0xc01 | (ctrl->tMOD << 16));
2999 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3000 (slotrank << 24) | 0x360004);
3001 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3002
Patrick Rudolph371d2912015-10-09 13:33:25 +02003003 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003004 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3005 0x1f105);
3006 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3007 0x4041003);
3008 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3009 (slotrank << 24) | 0);
3010 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3011
Patrick Rudolph371d2912015-10-09 13:33:25 +02003012 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003013 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3014 0x1f105);
3015 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3016 0x1001 | ((ctrl->CAS + 8) << 16));
3017 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3018 (slotrank << 24) | 0x60000);
3019 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3020
Patrick Rudolph371d2912015-10-09 13:33:25 +02003021 /* DRAM command MRS
3022 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003023 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3024 0x1f000);
3025 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3026 0xc01 | (ctrl->tMOD << 16));
3027 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3028 (slotrank << 24) | 0x360000);
3029 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3030 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3031 0xc0001);
3032
3033 wait_428c(channel);
3034 }
3035
Patrick Rudolph371d2912015-10-09 13:33:25 +02003036 /* XXX: check any measured value ? */
3037
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003038 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3039 ctrl->timings[channel][slotrank].lanes[lane].falling =
3040 48;
3041 ctrl->timings[channel][slotrank].lanes[lane].rising =
3042 48;
3043 }
3044
3045 program_timings(ctrl, channel);
3046
3047 FOR_ALL_POPULATED_RANKS {
3048 wait_428c(channel);
3049
Patrick Rudolph371d2912015-10-09 13:33:25 +02003050 /* DRAM command MRS
3051 * MR3 enable MPR
3052 * write MR3 MPR enable
3053 * in this mode only RD and RDA are allowed
3054 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003055 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3056 0x1f000);
3057 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3058 0xc01 | (ctrl->tMOD << 16));
3059 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3060 (slotrank << 24) | 0x360004);
3061 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3062
Patrick Rudolph371d2912015-10-09 13:33:25 +02003063 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003064 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3065 0x1f105);
3066 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3067 0x4041003);
3068 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3069 (slotrank << 24) | 0);
3070 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3071
Patrick Rudolph371d2912015-10-09 13:33:25 +02003072 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003073 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3074 0x1f105);
3075 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3076 0x1001 | ((ctrl->CAS + 8) << 16));
3077 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3078 (slotrank << 24) | 0x60000);
3079 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3080
Patrick Rudolph371d2912015-10-09 13:33:25 +02003081 /* DRAM command MRS
3082 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003083 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3084 0x1f000);
3085 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3086 0xc01 | (ctrl->tMOD << 16));
3087 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3088 (slotrank << 24) | 0x360000);
3089 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3090
3091 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3092 0xc0001);
3093 wait_428c(channel);
3094 }
3095
Patrick Rudolph371d2912015-10-09 13:33:25 +02003096 /* XXX: check any measured value ? */
3097
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003098 FOR_ALL_LANES {
3099 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel +
3100 lane * 4,
3101 ~read32(DEFAULT_MCHBAR + 0x4040 +
3102 0x400 * channel + lane * 4) & 0xff);
3103 }
3104
3105 fill_pattern0(ctrl, channel, 0, 0xffffffff);
3106 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3107 }
3108
3109 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3110 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
3111
3112 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3113 discover_edges_real(ctrl, channel, slotrank,
3114 falling_edges[channel][slotrank]);
3115 }
3116
3117 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
3118
3119 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3120 discover_edges_real(ctrl, channel, slotrank,
3121 rising_edges[channel][slotrank]);
3122 }
3123
3124 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3125
3126 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3127 ctrl->timings[channel][slotrank].lanes[lane].falling =
3128 falling_edges[channel][slotrank][lane];
3129 ctrl->timings[channel][slotrank].lanes[lane].rising =
3130 rising_edges[channel][slotrank][lane];
3131 }
3132
3133 FOR_ALL_POPULATED_CHANNELS {
3134 program_timings(ctrl, channel);
3135 }
3136
3137 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3138 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3139 0);
3140 }
3141}
3142
3143static void discover_edges_write_real(ramctr_timing * ctrl, int channel,
3144 int slotrank, int *edges)
3145{
3146 int edge;
3147 u32 raw_statistics[MAX_EDGE_TIMING + 1];
3148 int statistics[MAX_EDGE_TIMING + 1];
3149 const int reg3000b24[] = { 0, 0xc, 0x2c };
3150 int lane, i;
3151 int lower[NUM_LANES];
3152 int upper[NUM_LANES];
3153 int pat;
3154
3155 FOR_ALL_LANES {
3156 lower[lane] = 0;
3157 upper[lane] = MAX_EDGE_TIMING;
3158 }
3159
3160 for (i = 0; i < 3; i++) {
3161 write32(DEFAULT_MCHBAR + 0x3000 + 0x100 * channel,
3162 reg3000b24[i] << 24);
3163 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3164 fill_pattern5(ctrl, channel, pat);
3165 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
3166 printram("patterned\n");
3167 printram("[%x] = 0x%08x\n(%d, %d)\n",
3168 0x3000 + 0x100 * channel, reg3000b24[i] << 24, channel,
3169 slotrank);
3170 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
3171 FOR_ALL_LANES {
3172 ctrl->timings[channel][slotrank].lanes[lane].
3173 rising = edge;
3174 ctrl->timings[channel][slotrank].lanes[lane].
3175 falling = edge;
3176 }
3177 program_timings(ctrl, channel);
3178
3179 FOR_ALL_LANES {
3180 write32(DEFAULT_MCHBAR + 0x4340 +
3181 0x400 * channel + 4 * lane, 0);
3182 read32(DEFAULT_MCHBAR + 0x400 * channel +
3183 4 * lane + 0x4140);
3184 }
3185 wait_428c(channel);
3186
Patrick Rudolph371d2912015-10-09 13:33:25 +02003187 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003188 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3189 0x1f006);
3190 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3191 0x4 | (ctrl->tRCD << 16)
3192 | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) <<
3193 10));
3194 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3195 (slotrank << 24) | 0x60000);
3196 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel,
3197 0x240);
3198
Patrick Rudolph371d2912015-10-09 13:33:25 +02003199 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003200 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3201 0x1f201);
3202 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3203 0x8005020 | ((ctrl->tWTR + ctrl->CWL + 8) <<
3204 16));
3205 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3206 (slotrank << 24));
3207 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel,
3208 0x242);
3209
Patrick Rudolph371d2912015-10-09 13:33:25 +02003210 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003211 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3212 0x1f105);
3213 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3214 0x4005020 | (max(ctrl->tRTP, 8) << 16));
3215 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3216 (slotrank << 24));
3217 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel,
3218 0x242);
3219
Patrick Rudolph371d2912015-10-09 13:33:25 +02003220 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003221 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3222 0x1f002);
3223 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3224 0xc01 | (ctrl->tRP << 16));
3225 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3226 (slotrank << 24) | 0x60400);
3227 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3228
3229 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3230 0xc0001);
3231 wait_428c(channel);
3232 FOR_ALL_LANES {
3233 read32(DEFAULT_MCHBAR + 0x4340 +
3234 0x400 * channel + lane * 4);
3235 }
3236
3237 raw_statistics[edge] =
3238 MCHBAR32(0x436c + 0x400 * channel);
3239 }
3240 FOR_ALL_LANES {
3241 struct run rn;
3242 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
3243 statistics[edge] =
3244 ! !(raw_statistics[edge] & (1 << lane));
3245 rn = get_longest_zero_run(statistics,
3246 MAX_EDGE_TIMING + 1);
3247 printram("edges: %d, %d, %d: 0x%x-0x%x-0x%x, 0x%x-0x%x\n",
3248 channel, slotrank, i, rn.start, rn.middle,
3249 rn.end, rn.start + ctrl->edge_offset[i],
3250 rn.end - ctrl->edge_offset[i]);
3251 lower[lane] =
3252 max(rn.start + ctrl->edge_offset[i], lower[lane]);
3253 upper[lane] =
3254 min(rn.end - ctrl->edge_offset[i], upper[lane]);
3255 edges[lane] = (lower[lane] + upper[lane]) / 2;
Patrick Rudolph9733e282015-08-16 17:06:30 +02003256 if (rn.all || (lower[lane] > upper[lane]))
3257 die("edge write discovery failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003258
3259 }
3260 }
3261 }
3262
3263 write32(DEFAULT_MCHBAR + 0x3000, 0);
3264 printram("CPA\n");
3265}
3266
3267static void discover_edges_write(ramctr_timing * ctrl)
3268{
3269 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3270 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3271 int channel, slotrank, lane;
3272
3273 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3274 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
3275
3276 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3277 discover_edges_write_real(ctrl, channel, slotrank,
3278 falling_edges[channel][slotrank]);
3279 }
3280
3281 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
3282
3283 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3284 discover_edges_write_real(ctrl, channel, slotrank,
3285 rising_edges[channel][slotrank]);
3286 }
3287
3288 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3289
3290 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3291 ctrl->timings[channel][slotrank].lanes[lane].falling =
3292 falling_edges[channel][slotrank][lane];
3293 ctrl->timings[channel][slotrank].lanes[lane].rising =
3294 rising_edges[channel][slotrank][lane];
3295 }
3296
3297 FOR_ALL_POPULATED_CHANNELS
3298 program_timings(ctrl, channel);
3299
3300 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3301 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3302 0);
3303 }
3304}
3305
3306static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
3307{
3308 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003309 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003310 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
3311 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3312 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD)
3313 << 10) | (ctrl->tRCD << 16) | 4);
3314 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3315 (slotrank << 24) | 0x60000);
3316 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
3317
Patrick Rudolph371d2912015-10-09 13:33:25 +02003318 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003319 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
3320 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3321 0x80011e0 |
3322 ((ctrl->tWTR + ctrl->CWL + 8) << 16));
3323 write32(DEFAULT_MCHBAR + 0x4204 +
3324 0x400 * channel, (slotrank << 24));
3325 write32(DEFAULT_MCHBAR + 0x4214 +
3326 0x400 * channel, 0x242);
3327
Patrick Rudolph371d2912015-10-09 13:33:25 +02003328 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003329 write32(DEFAULT_MCHBAR + 0x4228 +
3330 0x400 * channel, 0x1f105);
3331 write32(DEFAULT_MCHBAR + 0x4238 +
3332 0x400 * channel,
3333 0x40011e0 | (max(ctrl->tRTP, 8) << 16));
3334 write32(DEFAULT_MCHBAR + 0x4208 +
3335 0x400 * channel, (slotrank << 24));
3336 write32(DEFAULT_MCHBAR + 0x4218 +
3337 0x400 * channel, 0x242);
3338
Patrick Rudolph371d2912015-10-09 13:33:25 +02003339 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003340 write32(DEFAULT_MCHBAR + 0x422c +
3341 0x400 * channel, 0x1f002);
3342 write32(DEFAULT_MCHBAR + 0x423c +
3343 0x400 * channel,
3344 0x1001 | (ctrl->tRP << 16));
3345 write32(DEFAULT_MCHBAR + 0x420c +
3346 0x400 * channel,
3347 (slotrank << 24) | 0x60400);
3348 write32(DEFAULT_MCHBAR + 0x421c +
3349 0x400 * channel, 0);
3350
3351 write32(DEFAULT_MCHBAR + 0x4284 +
3352 0x400 * channel, 0xc0001);
3353 wait_428c(channel);
3354}
3355
3356static void discover_timC_write(ramctr_timing * ctrl)
3357{
3358 const u8 rege3c_b24[3] = { 0, 0xf, 0x2f };
3359 int i, pat;
3360
3361 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3362 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3363 int channel, slotrank, lane;
3364
3365 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3366 lower[channel][slotrank][lane] = 0;
3367 upper[channel][slotrank][lane] = MAX_TIMC;
3368 }
3369
3370 write32(DEFAULT_MCHBAR + 0x4ea8, 1);
3371
3372 for (i = 0; i < 3; i++)
3373 FOR_ALL_POPULATED_CHANNELS {
3374 write32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100),
3375 (rege3c_b24[i] << 24)
3376 | (read32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100))
3377 & ~0x3f000000));
3378 udelay(2);
3379 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3380 FOR_ALL_POPULATED_RANKS {
3381 int timC;
3382 u32 raw_statistics[MAX_TIMC + 1];
3383 int statistics[MAX_TIMC + 1];
3384
3385 fill_pattern5(ctrl, channel, pat);
3386 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
3387 for (timC = 0; timC < MAX_TIMC + 1; timC++) {
3388 FOR_ALL_LANES
3389 ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
3390 program_timings(ctrl, channel);
3391
3392 test_timC_write (ctrl, channel, slotrank);
3393
3394 raw_statistics[timC] =
3395 MCHBAR32(0x436c + 0x400 * channel);
3396 }
3397 FOR_ALL_LANES {
3398 struct run rn;
3399 for (timC = 0; timC <= MAX_TIMC; timC++)
3400 statistics[timC] =
3401 !!(raw_statistics[timC] &
3402 (1 << lane));
3403 rn = get_longest_zero_run(statistics,
3404 MAX_TIMC + 1);
3405 if (rn.all)
3406 die("timC write discovery failed");
3407 printram("timC: %d, %d, %d: 0x%x-0x%x-0x%x, 0x%x-0x%x\n",
3408 channel, slotrank, i, rn.start,
3409 rn.middle, rn.end,
3410 rn.start + ctrl->timC_offset[i],
3411 rn.end - ctrl->timC_offset[i]);
3412 lower[channel][slotrank][lane] =
3413 max(rn.start + ctrl->timC_offset[i],
3414 lower[channel][slotrank][lane]);
3415 upper[channel][slotrank][lane] =
3416 min(rn.end - ctrl->timC_offset[i],
3417 upper[channel][slotrank][lane]);
3418
3419 }
3420 }
3421 }
3422 }
3423
3424 FOR_ALL_CHANNELS {
3425 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3426 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3427 ~0x3f000000));
3428 udelay(2);
3429 }
3430
3431 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3432
3433 printram("CPB\n");
3434
3435 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3436 printram("timC [%d, %d, %d] = 0x%x\n", channel,
3437 slotrank, lane,
3438 (lower[channel][slotrank][lane] +
3439 upper[channel][slotrank][lane]) / 2);
3440 ctrl->timings[channel][slotrank].lanes[lane].timC =
3441 (lower[channel][slotrank][lane] +
3442 upper[channel][slotrank][lane]) / 2;
3443 }
3444 FOR_ALL_POPULATED_CHANNELS {
3445 program_timings(ctrl, channel);
3446 }
3447}
3448
3449static void normalize_training(ramctr_timing * ctrl)
3450{
3451 int channel, slotrank, lane;
3452 int mat = 0;
3453
3454 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3455 int delta;
3456 FOR_ALL_LANES mat =
3457 max(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
3458 delta = (mat >> 6) - ctrl->timings[channel][slotrank].val_4028;
3459 ctrl->timings[channel][slotrank].val_4024 += delta;
3460 ctrl->timings[channel][slotrank].val_4028 += delta;
3461 }
3462
3463 FOR_ALL_POPULATED_CHANNELS {
3464 program_timings(ctrl, channel);
3465 }
3466}
3467
3468static void write_controller_mr(ramctr_timing * ctrl)
3469{
3470 int channel, slotrank;
3471
3472 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3473 write32(DEFAULT_MCHBAR + 0x0004 + (channel << 8) +
3474 lane_registers[slotrank], make_mr0(ctrl, slotrank));
3475 write32(DEFAULT_MCHBAR + 0x0008 + (channel << 8) +
Patrick Rudolph7e513d12016-01-10 14:22:34 +01003476 lane_registers[slotrank],
3477 make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003478 }
3479}
3480
3481static void channel_test(ramctr_timing * ctrl)
3482{
3483 int channel, slotrank, lane;
3484
3485 FOR_ALL_POPULATED_CHANNELS
3486 if (read32(DEFAULT_MCHBAR + 0x42a0 + (channel << 10)) & 0xa000)
3487 die("Mini channel test failed (1)\n");
3488 FOR_ALL_POPULATED_CHANNELS {
3489 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
3490
3491 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3492 }
3493
3494 for (slotrank = 0; slotrank < 4; slotrank++)
3495 FOR_ALL_CHANNELS
3496 if (ctrl->rankmap[channel] & (1 << slotrank)) {
3497 FOR_ALL_LANES {
3498 write32(DEFAULT_MCHBAR + (0x4f40 + 4 * lane), 0);
3499 write32(DEFAULT_MCHBAR + (0x4d40 + 4 * lane), 0);
3500 }
3501 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003502 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003503 write32(DEFAULT_MCHBAR + 0x4220 + (channel << 10), 0x0001f006);
3504 write32(DEFAULT_MCHBAR + 0x4230 + (channel << 10), 0x0028a004);
3505 write32(DEFAULT_MCHBAR + 0x4200 + (channel << 10),
3506 0x00060000 | (slotrank << 24));
3507 write32(DEFAULT_MCHBAR + 0x4210 + (channel << 10), 0x00000244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003508 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003509 write32(DEFAULT_MCHBAR + 0x4224 + (channel << 10), 0x0001f201);
3510 write32(DEFAULT_MCHBAR + 0x4234 + (channel << 10), 0x08281064);
3511 write32(DEFAULT_MCHBAR + 0x4204 + (channel << 10),
3512 0x00000000 | (slotrank << 24));
3513 write32(DEFAULT_MCHBAR + 0x4214 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003514 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003515 write32(DEFAULT_MCHBAR + 0x4228 + (channel << 10), 0x0001f105);
3516 write32(DEFAULT_MCHBAR + 0x4238 + (channel << 10), 0x04281064);
3517 write32(DEFAULT_MCHBAR + 0x4208 + (channel << 10),
3518 0x00000000 | (slotrank << 24));
3519 write32(DEFAULT_MCHBAR + 0x4218 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003520 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003521 write32(DEFAULT_MCHBAR + 0x422c + (channel << 10), 0x0001f002);
3522 write32(DEFAULT_MCHBAR + 0x423c + (channel << 10), 0x00280c01);
3523 write32(DEFAULT_MCHBAR + 0x420c + (channel << 10),
3524 0x00060400 | (slotrank << 24));
3525 write32(DEFAULT_MCHBAR + 0x421c + (channel << 10), 0x00000240);
3526 write32(DEFAULT_MCHBAR + 0x4284 + (channel << 10), 0x000c0001);
3527 wait_428c(channel);
3528 FOR_ALL_LANES
3529 if (read32(DEFAULT_MCHBAR + 0x4340 + (channel << 10) + 4 * lane))
3530 die("Mini channel test failed (2)\n");
3531 }
3532}
3533
3534static void set_scrambling_seed(ramctr_timing * ctrl)
3535{
3536 int channel;
3537
3538 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them?
3539 I don't think so. */
3540 static u32 seeds[NUM_CHANNELS][3] = {
3541 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
3542 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
3543 };
3544 FOR_ALL_POPULATED_CHANNELS {
3545 MCHBAR32(0x4020 + 0x400 * channel) &= ~0x10000000;
3546 write32(DEFAULT_MCHBAR + 0x4034, seeds[channel][0]);
3547 write32(DEFAULT_MCHBAR + 0x403c, seeds[channel][1]);
3548 write32(DEFAULT_MCHBAR + 0x4038, seeds[channel][2]);
3549 }
3550}
3551
3552static void set_4f8c(void)
3553{
3554 struct cpuid_result cpures;
3555 u32 cpu;
3556
3557 cpures = cpuid(0);
3558 cpu = (cpures.eax);
3559 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
3560 MCHBAR32(0x4f8c) = 0x141D1519;
3561 } else {
3562 MCHBAR32(0x4f8c) = 0x551D1519;
3563 }
3564}
3565
3566static void prepare_training(ramctr_timing * ctrl)
3567{
3568 int channel;
3569
3570 FOR_ALL_POPULATED_CHANNELS {
3571 // Always drive command bus
3572 MCHBAR32(0x4004 + 0x400 * channel) |= 0x20000000;
3573 }
3574
3575 udelay(1);
3576
3577 FOR_ALL_POPULATED_CHANNELS {
3578 wait_428c(channel);
3579 }
3580}
3581
3582static void set_4008c(ramctr_timing * ctrl)
3583{
3584 int channel, slotrank;
3585 u32 reg;
3586 FOR_ALL_POPULATED_CHANNELS {
3587 u32 b20, b4_8_12;
3588 int min_320c = 10000;
3589 int max_320c = -10000;
3590
3591 FOR_ALL_POPULATED_RANKS {
3592 max_320c = max(ctrl->timings[channel][slotrank].val_320c, max_320c);
3593 min_320c = min(ctrl->timings[channel][slotrank].val_320c, min_320c);
3594 }
3595
3596 if (max_320c - min_320c > 51)
3597 b20 = 0;
3598 else
3599 b20 = ctrl->ref_card_offset[channel];
3600
3601 if (ctrl->reg_320c_range_threshold < max_320c - min_320c)
3602 b4_8_12 = 0x3330;
3603 else
3604 b4_8_12 = 0x2220;
3605
3606 reg = read32(DEFAULT_MCHBAR + 0x400c + (channel << 10));
3607 write32(DEFAULT_MCHBAR + 0x400c + (channel << 10),
3608 (reg & 0xFFF0FFFF)
3609 | (ctrl->ref_card_offset[channel] << 16)
3610 | (ctrl->ref_card_offset[channel] << 18));
3611 write32(DEFAULT_MCHBAR + 0x4008 + (channel << 10),
3612 0x0a000000
3613 | (b20 << 20)
3614 | ((ctrl->ref_card_offset[channel] + 2) << 16)
3615 | b4_8_12);
3616 }
3617}
3618
3619static void set_42a0(ramctr_timing * ctrl)
3620{
3621 int channel;
3622 FOR_ALL_POPULATED_CHANNELS {
3623 write32(DEFAULT_MCHBAR + (0x42a0 + 0x400 * channel),
3624 0x00001000 | ctrl->rankmap[channel]);
3625 MCHBAR32(0x4004 + 0x400 * channel) &= ~0x20000000; // OK
3626 }
3627}
3628
3629static int encode_5d10(int ns)
3630{
3631 return (ns + 499) / 500;
3632}
3633
3634/* FIXME: values in this function should be hardware revision-dependent. */
3635static void final_registers(ramctr_timing * ctrl)
3636{
3637 int channel;
3638 int t1_cycles = 0, t1_ns = 0, t2_ns;
3639 int t3_ns;
3640 u32 r32;
3641
3642 write32(DEFAULT_MCHBAR + 0x4cd4, 0x00000046);
3643
3644 write32(DEFAULT_MCHBAR + 0x400c, (read32(DEFAULT_MCHBAR + 0x400c) & 0xFFFFCFFF) | 0x1000); // OK
3645 write32(DEFAULT_MCHBAR + 0x440c, (read32(DEFAULT_MCHBAR + 0x440c) & 0xFFFFCFFF) | 0x1000); // OK
3646 write32(DEFAULT_MCHBAR + 0x4cb0, 0x00000740);
3647 write32(DEFAULT_MCHBAR + 0x4380, 0x00000aaa); // OK
3648 write32(DEFAULT_MCHBAR + 0x4780, 0x00000aaa); // OK
3649 write32(DEFAULT_MCHBAR + 0x4f88, 0x5f7003ff); // OK
3650 write32(DEFAULT_MCHBAR + 0x5064, 0x00073000 | ctrl->reg_5064b0); // OK
3651
3652 FOR_ALL_CHANNELS {
3653 switch (ctrl->rankmap[channel]) {
3654 /* Unpopulated channel. */
3655 case 0:
3656 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0);
3657 break;
3658 /* Only single-ranked dimms. */
3659 case 1:
3660 case 4:
3661 case 5:
3662 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x373131);
3663 break;
3664 /* Dual-ranked dimms present. */
3665 default:
3666 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x9b6ea1);
3667 break;
3668 }
3669 }
3670
3671 write32 (DEFAULT_MCHBAR + 0x5880, 0xca9171e5);
3672 write32 (DEFAULT_MCHBAR + 0x5888,
3673 (read32 (DEFAULT_MCHBAR + 0x5888) & ~0xffffff) | 0xe4d5d0);
3674 write32 (DEFAULT_MCHBAR + 0x58a8, read32 (DEFAULT_MCHBAR + 0x58a8) & ~0x1f);
3675 write32 (DEFAULT_MCHBAR + 0x4294,
3676 (read32 (DEFAULT_MCHBAR + 0x4294) & ~0x30000)
3677 | (1 << 16));
3678 write32 (DEFAULT_MCHBAR + 0x4694,
3679 (read32 (DEFAULT_MCHBAR + 0x4694) & ~0x30000)
3680 | (1 << 16));
3681
3682 MCHBAR32(0x5030) |= 1; // OK
3683 MCHBAR32(0x5030) |= 0x80; // OK
3684 MCHBAR32(0x5f18) = 0xfa; // OK
3685
3686 /* Find a populated channel. */
3687 FOR_ALL_POPULATED_CHANNELS
3688 break;
3689
3690 t1_cycles = ((read32(DEFAULT_MCHBAR + 0x4290 + channel * 0x400) >> 8) & 0xff);
3691 r32 = read32(DEFAULT_MCHBAR + 0x5064);
3692 if (r32 & 0x20000)
3693 t1_cycles += (r32 & 0xfff);
3694 t1_cycles += (read32(DEFAULT_MCHBAR + channel * 0x400 + 0x42a4) & 0xfff);
3695 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
3696 if (!(r32 & 0x20000))
3697 t1_ns += 500;
3698
3699 t2_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f10) >> 8) & 0xfff);
3700 if ( read32(DEFAULT_MCHBAR + 0x5f00) & 8 )
3701 {
3702 t3_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f20) >> 8) & 0xfff);
3703 t3_ns += 10 * (read32(DEFAULT_MCHBAR + 0x5f18) & 0xff);
3704 }
3705 else
3706 {
3707 t3_ns = 500;
3708 }
3709 printk(BIOS_DEBUG, "t123: %d, %d, %d\n",
3710 t1_ns, t2_ns, t3_ns);
3711 write32 (DEFAULT_MCHBAR + 0x5d10,
3712 ((encode_5d10(t1_ns) + encode_5d10(t2_ns)) << 16)
3713 | (encode_5d10(t1_ns) << 8)
3714 | ((encode_5d10(t3_ns) + encode_5d10(t2_ns) + encode_5d10(t1_ns)) << 24)
3715 | (read32(DEFAULT_MCHBAR + 0x5d10) & 0xC0C0C0C0)
3716 | 0xc);
3717}
3718
3719static void save_timings(ramctr_timing * ctrl)
3720{
3721 struct mrc_data_container *mrcdata;
3722 int output_len = ALIGN(sizeof (*ctrl), 16);
3723
3724 /* Save the MRC S3 restore data to cbmem */
3725 mrcdata = cbmem_add
3726 (CBMEM_ID_MRCDATA,
3727 output_len + sizeof(struct mrc_data_container));
3728
3729 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
3730 ctrl, mrcdata, output_len);
3731
3732 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
3733 mrcdata->mrc_data_size = output_len;
3734 mrcdata->reserved = 0;
3735 memcpy(mrcdata->mrc_data, ctrl, sizeof (*ctrl));
3736
3737 /* Zero the unused space in aligned buffer. */
3738 if (output_len > sizeof (*ctrl))
3739 memset(mrcdata->mrc_data+sizeof (*ctrl), 0,
3740 output_len - sizeof (*ctrl));
3741
3742 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
3743 mrcdata->mrc_data_size);
3744}
3745
3746static void restore_timings(ramctr_timing * ctrl)
3747{
3748 int channel, slotrank, lane;
3749
3750 FOR_ALL_POPULATED_CHANNELS
3751 MCHBAR32(0x4004 + 0x400 * channel) =
3752 ctrl->tRRD
3753 | (ctrl->tRTP << 4)
3754 | (ctrl->tCKE << 8)
3755 | (ctrl->tWTR << 12)
3756 | (ctrl->tFAW << 16)
3757 | (ctrl->tWR << 24)
3758 | (ctrl->cmd_stretch[channel] << 30);
3759
3760 udelay(1);
3761
3762 FOR_ALL_POPULATED_CHANNELS {
3763 wait_428c(channel);
3764 }
3765
3766 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3767 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
3768 + 4 * lane, 0);
3769 }
3770
3771 FOR_ALL_POPULATED_CHANNELS
3772 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
3773 read32(DEFAULT_MCHBAR + 0x4008 +
3774 0x400 * channel) | 0x8000000);
3775
3776 FOR_ALL_POPULATED_CHANNELS {
3777 udelay (1);
3778 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
3779 read32(DEFAULT_MCHBAR + 0x4020 +
3780 0x400 * channel) | 0x200000);
3781 }
3782
3783 printram("CPE\n");
3784
3785 write32(DEFAULT_MCHBAR + 0x3400, 0);
3786 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3787
3788 printram("CP5b\n");
3789
3790 FOR_ALL_POPULATED_CHANNELS {
3791 program_timings(ctrl, channel);
3792 }
3793
3794 u32 reg, addr;
3795
3796 while (!(MCHBAR32(0x5084) & 0x10000)) ;
3797 do {
3798 reg = MCHBAR32(0x428c);
3799 } while ((reg & 0x14) == 0);
3800
3801 // Set state of memory controller
3802 MCHBAR32(0x5030) = 0x116;
3803 MCHBAR32(0x4ea0) = 0;
3804
3805 // Wait 500us
3806 udelay(500);
3807
3808 FOR_ALL_CHANNELS {
3809 // Set valid rank CKE
3810 reg = 0;
3811 reg = (reg & ~0xf) | ctrl->rankmap[channel];
3812 addr = 0x400 * channel + 0x42a0;
3813 MCHBAR32(addr) = reg;
3814
3815 // Wait 10ns for ranks to settle
3816 //udelay(0.01);
3817
3818 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3819 MCHBAR32(addr) = reg;
3820
3821 // Write reset using a NOP
3822 write_reset(ctrl);
3823 }
3824
3825 /* mrs commands. */
3826 dram_mrscommands(ctrl);
3827
3828 printram("CP5c\n");
3829
3830 write32(DEFAULT_MCHBAR + 0x3000, 0);
3831
3832 FOR_ALL_CHANNELS {
3833 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3834 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3835 ~0x3f000000));
3836 udelay(2);
3837 }
3838
3839 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3840}
3841
3842void init_dram_ddr3(spd_raw_data * spds, int mobile, int min_tck,
3843 int s3resume)
3844{
3845 int me_uma_size;
3846 int cbmem_was_inited;
3847
3848 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +02003849
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -07003850 report_platform_info();
3851
Stefan Reinauer00636b02012-04-04 00:08:51 +02003852 /* Wait for ME to be ready */
3853 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003854 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003855
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003856 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +02003857
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003858 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +02003859
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003860 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003861
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003862 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +02003863
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003864 reg_5d10 = read32(DEFAULT_MCHBAR + 0x5d10); // !!! = 0x00000000
3865 if ((pcie_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
3866 && reg_5d10 && !s3resume) {
3867 write32(DEFAULT_MCHBAR + 0x5d10, 0);
3868 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +02003869 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003870
Patrick Georgi546953c2014-11-29 10:38:17 +01003871 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003872 }
Stefan Reinauer00636b02012-04-04 00:08:51 +02003873
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003874 ramctr_timing ctrl;
Vadim Bendebury48a4a7f2012-06-07 18:47:13 -07003875
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003876 memset(&ctrl, 0, sizeof (ctrl));
3877
3878 early_pch_init_native();
3879 early_thermal_init();
3880
3881 ctrl.mobile = mobile;
3882 ctrl.tCK = min_tck;
3883
3884 /* FIXME: for non-S3 we should be able to use timing caching with
3885 proper verification. Right now we use timings only for S3 case.
3886 */
3887 if (s3resume) {
3888 struct mrc_data_container *mrc_cache;
3889
3890 mrc_cache = find_current_mrc_cache();
3891 if (!mrc_cache || mrc_cache->mrc_data_size < sizeof (ctrl)) {
3892 /* Failed S3 resume, reset to come up cleanly */
3893 outb(0x6, 0xcf9);
3894 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003895 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003896 memcpy(&ctrl, mrc_cache->mrc_data, sizeof (ctrl));
Stefan Reinauer00636b02012-04-04 00:08:51 +02003897 }
3898
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003899 if (!s3resume) {
3900 dimm_info info;
Sven Schnelled4ee8082012-07-28 09:28:56 +02003901
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003902 /* Get DDR3 SPD data */
3903 dram_find_spds_ddr3(spds, &info, &ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +02003904
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003905 /* Find fastest common supported parameters */
3906 dram_find_common_params(&info, &ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +02003907
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003908 dram_dimm_mapping(&info, &ctrl);
3909 }
3910
3911 /* Set MCU frequency */
3912 dram_freq(&ctrl);
3913
3914 if (!s3resume) {
3915 /* Calculate timings */
3916 dram_timing(&ctrl);
3917 }
3918
3919 /* Set version register */
3920 MCHBAR32(0x5034) = 0xC04EB002;
3921
3922 /* Enable crossover */
3923 dram_xover(&ctrl);
3924
3925 /* Set timing and refresh registers */
3926 dram_timing_regs(&ctrl);
3927
3928 /* Power mode preset */
3929 MCHBAR32(0x4e80) = 0x5500;
3930
3931 /* Set scheduler parameters */
3932 MCHBAR32(0x4c20) = 0x10100005;
3933
3934 /* Set cpu specific register */
3935 set_4f8c();
3936
3937 /* Clear IO reset bit */
3938 MCHBAR32(0x5030) &= ~0x20;
3939
3940 /* Set MAD-DIMM registers */
3941 dram_dimm_set_mapping(&ctrl);
3942 printk(BIOS_DEBUG, "Done dimm mapping\n");
3943
3944 /* Zone config */
3945 dram_zones(&ctrl, 1);
3946
3947 /* Set memory map */
3948 dram_memorymap(&ctrl, me_uma_size);
3949 printk(BIOS_DEBUG, "Done memory map\n");
3950
3951 /* Set IO registers */
3952 dram_ioregs(&ctrl);
3953 printk(BIOS_DEBUG, "Done io registers\n");
3954
3955 udelay(1);
3956
3957 if (s3resume) {
3958 restore_timings(&ctrl);
3959 } else {
3960 /* Do jedec ddr3 reset sequence */
3961 dram_jedecreset(&ctrl);
3962 printk(BIOS_DEBUG, "Done jedec reset\n");
3963
3964 /* MRS commands */
3965 dram_mrscommands(&ctrl);
3966 printk(BIOS_DEBUG, "Done MRS commands\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003967
3968 /* Prepare for memory training */
3969 prepare_training(&ctrl);
3970
3971 read_training(&ctrl);
3972 write_training(&ctrl);
3973
3974 printram("CP5a\n");
3975
3976 discover_edges(&ctrl);
3977
3978 printram("CP5b\n");
3979
3980 command_training(&ctrl);
3981
3982 printram("CP5c\n");
3983
3984 discover_edges_write(&ctrl);
3985
3986 discover_timC_write(&ctrl);
3987
3988 normalize_training(&ctrl);
3989 }
3990
3991 set_4008c(&ctrl);
3992
3993 write_controller_mr(&ctrl);
3994
3995 if (!s3resume) {
3996 channel_test(&ctrl);
3997 }
3998
3999 /* FIXME: should be hardware revision-dependent. */
4000 write32(DEFAULT_MCHBAR + 0x5024, 0x00a030ce);
4001
4002 set_scrambling_seed(&ctrl);
4003
4004 set_42a0(&ctrl);
4005
4006 final_registers(&ctrl);
4007
4008 /* Zone config */
4009 dram_zones(&ctrl, 0);
4010
4011 if (!s3resume)
4012 quick_ram_check();
4013
4014 intel_early_me_status();
4015 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
4016 intel_early_me_status();
4017
Stefan Reinauer00636b02012-04-04 00:08:51 +02004018 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004019
4020 cbmem_was_inited = !cbmem_recovery(s3resume);
4021 if (!s3resume)
4022 save_timings(&ctrl);
4023 if (s3resume && !cbmem_was_inited) {
4024 /* Failed S3 resume, reset to come up cleanly */
4025 outb(0x6, 0xcf9);
4026 halt();
4027 }
Stefan Reinauer00636b02012-04-04 00:08:51 +02004028}