blob: 8a287c184c645afe08fc5c94b613c3e326f2cc29 [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);
Vladimir Serbinenko3141eac2016-01-29 19:42:02 +01002337 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002338 if (rn.all)
2339 die("timB discovery failed");
2340 printram("Bval: %d, %d, %d, %x\n", channel, slotrank,
2341 lane, ctrl->timings[channel][slotrank].lanes[lane].timB);
2342 }
2343}
2344
2345static int get_timB_high_adjust(u64 val)
2346{
2347 int i;
2348
2349 /* good */
2350 if (val == 0xffffffffffffffffLL)
2351 return 0;
2352
2353 if (val >= 0xf000000000000000LL) {
2354 /* needs negative adjustment */
2355 for (i = 0; i < 8; i++)
2356 if (val << (8 * (7 - i) + 4))
2357 return -i;
2358 } else {
2359 /* needs positive adjustment */
2360 for (i = 0; i < 8; i++)
2361 if (val >> (8 * (7 - i) + 4))
2362 return i;
2363 }
2364 return 8;
2365}
2366
2367static void adjust_high_timB(ramctr_timing * ctrl)
2368{
2369 int channel, slotrank, lane, old;
2370 write32(DEFAULT_MCHBAR + 0x3400, 0x200);
2371 FOR_ALL_POPULATED_CHANNELS {
2372 fill_pattern1(ctrl, channel);
2373 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 1);
2374 }
2375 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2376
2377 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x10001);
2378
2379 wait_428c(channel);
2380
Patrick Rudolph371d2912015-10-09 13:33:25 +02002381 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002382 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2383 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2384 0xc01 | (ctrl->tRCD << 16));
2385 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2386 (slotrank << 24) | 0x60000);
2387 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2388
Patrick Rudolph371d2912015-10-09 13:33:25 +02002389 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002390 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2391 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8040c01);
2392 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2393 (slotrank << 24) | 0x8);
2394 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2395
Patrick Rudolph371d2912015-10-09 13:33:25 +02002396 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002397 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2398 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x8041003);
2399 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2400 (slotrank << 24));
2401 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x3e2);
2402
Patrick Rudolph371d2912015-10-09 13:33:25 +02002403 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002404 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2405 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2406 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2407 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2408 (slotrank << 24) | 0x8);
2409 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2410
2411 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2412
2413 wait_428c(channel);
2414
Patrick Rudolph371d2912015-10-09 13:33:25 +02002415 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002416 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2417 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2418 0xc01 | ((ctrl->tRP) << 16));
2419 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2420 (slotrank << 24) | 0x60400);
2421 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2422
Patrick Rudolph371d2912015-10-09 13:33:25 +02002423 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002424 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2425 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2426 0xc01 | ((ctrl->tRCD) << 16));
2427 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2428 (slotrank << 24) | 0x60000);
2429 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2430
Patrick Rudolph371d2912015-10-09 13:33:25 +02002431 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002432 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x3f105);
2433 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2434 0x4000c01 |
2435 ((ctrl->tRP +
2436 ctrl->timings[channel][slotrank].val_4024 +
2437 ctrl->timings[channel][slotrank].val_4028) << 16));
2438 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2439 (slotrank << 24) | 0x60008);
2440 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2441
2442 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
2443 wait_428c(channel);
2444 FOR_ALL_LANES {
2445 u64 res =
2446 read32(DEFAULT_MCHBAR + lane_registers[lane] +
2447 0x100 * channel + 4);
2448 res |=
2449 ((u64) read32(DEFAULT_MCHBAR + lane_registers[lane] +
2450 0x100 * channel + 8)) << 32;
2451 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
2452 ctrl->timings[channel][slotrank].lanes[lane].timB +=
2453 get_timB_high_adjust(res) * 64;
2454
2455 printk(BIOS_DEBUG, "High adjust %d:%016llx\n", lane, res);
2456 printram("Bval+: %d, %d, %d, %x -> %x\n", channel,
2457 slotrank, lane, old,
2458 ctrl->timings[channel][slotrank].lanes[lane].
2459 timB);
2460 }
2461 }
2462 write32(DEFAULT_MCHBAR + 0x3400, 0);
2463}
2464
2465static void write_op(ramctr_timing * ctrl, int channel)
2466{
2467 int slotrank;
2468
2469 wait_428c(channel);
2470
2471 /* choose an existing rank. */
2472 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2473
Patrick Rudolph371d2912015-10-09 13:33:25 +02002474 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002475 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2476 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2477
2478 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2479 (slotrank << 24) | 0x60000);
2480
2481 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2482
2483 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2484 wait_428c(channel);
2485}
2486
Patrick Rudolph371d2912015-10-09 13:33:25 +02002487/* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
2488 * DDR3 adopted the fly-by topology. The data and strobes signals reach
2489 * the chips at different times with respect to command, address and
2490 * clock signals.
2491 * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase
2492 * shift can be introduced.
2493 * It is assumed that the CLK/ADDR/CMD signals have the same routing delay.
2494 *
2495 * To find the required phase shift the DRAM is placed in "write leveling" mode.
2496 * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the
2497 * sampled value on the data lanes (DQs).
2498 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002499static void write_training(ramctr_timing * ctrl)
2500{
2501 int channel, slotrank, lane;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002502
2503 FOR_ALL_POPULATED_CHANNELS
2504 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
2505 read32(DEFAULT_MCHBAR + 0x4008 +
2506 0x400 * channel) | 0x8000000);
2507
2508 FOR_ALL_POPULATED_CHANNELS {
2509 write_op(ctrl, channel);
2510 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2511 read32(DEFAULT_MCHBAR + 0x4020 +
2512 0x400 * channel) | 0x200000);
2513 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002514
2515 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002516 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2517 FOR_ALL_POPULATED_CHANNELS {
2518 write_op(ctrl, channel);
2519 }
2520
Patrick Rudolph371d2912015-10-09 13:33:25 +02002521 /* enable write leveling on all ranks
2522 * disable all DQ outputs
2523 * only NOP is allowed in this mode */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002524 FOR_ALL_CHANNELS
2525 FOR_ALL_POPULATED_RANKS
2526 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002527 make_mr1(ctrl, slotrank, channel) | 0x1080);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002528
2529 write32(DEFAULT_MCHBAR + 0x3400, 0x108052);
2530
Patrick Rudolph9b515682015-10-09 13:43:51 +02002531 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002532
Patrick Rudolph371d2912015-10-09 13:33:25 +02002533 /* set any valid value for timB, it gets corrected later */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002534 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2535 discover_timB(ctrl, channel, slotrank);
2536
Patrick Rudolph371d2912015-10-09 13:33:25 +02002537 /* disable write leveling on all ranks */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002538 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2539 write_mrreg(ctrl, channel,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002540 slotrank, 1, make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002541
2542 write32(DEFAULT_MCHBAR + 0x3400, 0);
2543
2544 FOR_ALL_POPULATED_CHANNELS
2545 wait_428c(channel);
2546
Patrick Rudolph371d2912015-10-09 13:33:25 +02002547 /* refresh enable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002548 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) | 8);
2549
2550 FOR_ALL_POPULATED_CHANNELS {
2551 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2552 ~0x00200000 & read32(DEFAULT_MCHBAR + 0x4020 +
2553 0x400 * channel));
2554 read32(DEFAULT_MCHBAR + 0x428c + 0x400 * channel);
2555 wait_428c(channel);
2556
Patrick Rudolph371d2912015-10-09 13:33:25 +02002557 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002558 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2559 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
2560 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, 0x60000);
2561 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2562
2563 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2564 wait_428c(channel);
2565 }
2566
Patrick Rudolph9b515682015-10-09 13:43:51 +02002567 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002568
2569 printram("CPE\n");
2570 precharge(ctrl);
2571 printram("CPF\n");
2572
2573 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2574 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2575 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2576 0);
2577 }
2578
2579 FOR_ALL_POPULATED_CHANNELS {
2580 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
2581 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2582 }
2583
2584 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2585 discover_timC(ctrl, channel, slotrank);
2586
2587 FOR_ALL_POPULATED_CHANNELS
2588 program_timings(ctrl, channel);
2589
Patrick Rudolph371d2912015-10-09 13:33:25 +02002590 /* measure and adjust timB timings */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002591 adjust_high_timB(ctrl);
2592
2593 FOR_ALL_POPULATED_CHANNELS
2594 program_timings(ctrl, channel);
2595
2596 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2597 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2598 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2599 0);
2600 }
2601}
2602
2603static int test_320c(ramctr_timing * ctrl, int channel, int slotrank)
2604{
2605 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2606 int timC_delta;
2607 int lanes_ok = 0;
2608 int ctr = 0;
2609 int lane;
2610
2611 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2612 FOR_ALL_LANES {
2613 ctrl->timings[channel][slotrank].lanes[lane].timC =
2614 saved_rt.lanes[lane].timC + timC_delta;
2615 }
2616 program_timings(ctrl, channel);
2617 FOR_ALL_LANES {
2618 write32(DEFAULT_MCHBAR + 4 * lane + 0x4f40, 0);
2619 }
2620
2621 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2622
2623 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002624 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002625 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2626 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2627 ((max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1)) << 10)
2628 | 8 | (ctrl->tRCD << 16));
2629
2630 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2631 (slotrank << 24) | ctr | 0x60000);
2632
2633 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002634 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002635 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2636 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2637 0x8001020 | ((ctrl->CWL + ctrl->tWTR + 8) << 16));
2638 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2639 (slotrank << 24));
2640 write32(DEFAULT_MCHBAR + 0x4244 + 0x400 * channel, 0x389abcd);
2641 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x20e42);
2642
Patrick Rudolph371d2912015-10-09 13:33:25 +02002643 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002644 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2645 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2646 0x4001020 | (max(ctrl->tRTP, 8) << 16));
2647 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2648 (slotrank << 24));
2649 write32(DEFAULT_MCHBAR + 0x4248 + 0x400 * channel, 0x389abcd);
2650 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x20e42);
2651
Patrick Rudolph371d2912015-10-09 13:33:25 +02002652 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002653 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2654 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, 0xf1001);
2655 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2656 (slotrank << 24) | 0x60400);
2657 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2658
2659 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2660 wait_428c(channel);
2661 FOR_ALL_LANES {
2662 u32 r32 =
2663 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2664 0x400 * channel);
2665
2666 if (r32 == 0)
2667 lanes_ok |= 1 << lane;
2668 }
2669 ctr++;
2670 if (lanes_ok == ((1 << NUM_LANES) - 1))
2671 break;
2672 }
2673
2674 ctrl->timings[channel][slotrank] = saved_rt;
2675
2676 printram("3lanes: %x\n", lanes_ok);
2677 return lanes_ok != ((1 << NUM_LANES) - 1);
2678}
2679
2680#include "raminit_patterns.h"
2681
2682static void fill_pattern5(ramctr_timing * ctrl, int channel, int patno)
2683{
2684 unsigned i, j;
2685 unsigned channel_offset =
2686 get_precedening_channels(ctrl, channel) * 0x40;
2687 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2688
2689 if (patno) {
2690 u8 base8 = 0x80 >> ((patno - 1) % 8);
2691 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2692 for (i = 0; i < 32; i++) {
2693 for (j = 0; j < 16; j++) {
2694 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
2695 if (invert[patno - 1][i] & (1 << (j / 2)))
2696 val = ~val;
2697 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2698 j * 4), val);
2699 }
2700 }
2701
2702 } else {
2703 for (i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++) {
2704 for (j = 0; j < 16; j++)
2705 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2706 j * 4), pattern[i][j]);
2707 }
2708 sfence();
2709 }
2710}
2711
2712static void reprogram_320c(ramctr_timing * ctrl)
2713{
2714 int channel, slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002715
2716 FOR_ALL_POPULATED_CHANNELS {
2717 wait_428c(channel);
2718
2719 /* choose an existing rank. */
2720 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2721
Patrick Rudolph371d2912015-10-09 13:33:25 +02002722 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002723 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2724 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2725
2726 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2727 (slotrank << 24) | 0x60000);
2728
2729 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2730
2731 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2732 wait_428c(channel);
2733 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2734 read32(DEFAULT_MCHBAR + 0x4020 +
2735 0x400 * channel) | 0x200000);
2736 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002737
2738 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002739 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2740 FOR_ALL_POPULATED_CHANNELS {
2741 wait_428c(channel);
2742
2743 /* choose an existing rank. */
2744 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2745
Patrick Rudolph371d2912015-10-09 13:33:25 +02002746 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002747 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2748 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2749
2750 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2751 (slotrank << 24) | 0x60000);
2752
2753 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2754
2755 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2756 wait_428c(channel);
2757 }
2758
2759 /* jedec reset */
2760 dram_jedecreset(ctrl);
2761 /* mrs commands. */
2762 dram_mrscommands(ctrl);
2763
Patrick Rudolph9b515682015-10-09 13:43:51 +02002764 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002765}
2766
2767#define MIN_C320C_LEN 13
2768
2769static int try_cmd_stretch(ramctr_timing * ctrl, int cmd_stretch)
2770{
2771 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2772 int channel, slotrank;
2773 int c320c;
2774 int stat[NUM_SLOTRANKS][256];
2775
2776 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2777 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
2778 }
2779
2780 FOR_ALL_POPULATED_CHANNELS {
2781 ctrl->cmd_stretch[channel] = cmd_stretch;
2782 }
2783
2784 FOR_ALL_POPULATED_CHANNELS
2785 MCHBAR32(0x4004 + 0x400 * channel) =
2786 ctrl->tRRD
2787 | (ctrl->tRTP << 4)
2788 | (ctrl->tCKE << 8)
2789 | (ctrl->tWTR << 12)
2790 | (ctrl->tFAW << 16)
2791 | (ctrl->tWR << 24)
2792 | (ctrl->cmd_stretch[channel] << 30);
2793
2794
2795 FOR_ALL_CHANNELS {
2796 int delta = 0;
2797 if (ctrl->cmd_stretch[channel] == 2)
2798 delta = 2;
2799 else if (ctrl->cmd_stretch[channel] == 0)
2800 delta = 4;
2801
2802 FOR_ALL_POPULATED_RANKS {
2803 ctrl->timings[channel][slotrank].val_4024 -= delta;
2804 }
2805 }
2806
2807 FOR_ALL_POPULATED_CHANNELS {
2808 for (c320c = -127; c320c <= 127; c320c++) {
2809 FOR_ALL_POPULATED_RANKS {
2810 ctrl->timings[channel][slotrank].val_320c = c320c;
2811 }
2812 program_timings(ctrl, channel);
2813 reprogram_320c(ctrl);
2814 FOR_ALL_POPULATED_RANKS {
2815 stat[slotrank][c320c + 127] =
2816 test_320c(ctrl, channel, slotrank);
2817 printram("3stat: %d, %d, %d: %d\n",
2818 channel, slotrank, c320c,
2819 stat[slotrank][c320c + 127]);
2820 }
2821 }
2822 FOR_ALL_POPULATED_RANKS {
2823 struct run rn =
2824 get_longest_zero_run(stat[slotrank], 255);
2825 ctrl->timings[channel][slotrank].val_320c =
2826 rn.middle - 127;
2827 printram("3val: %d, %d: %d\n", channel,
2828 slotrank,
2829 ctrl->timings[channel][slotrank].val_320c);
2830 if (rn.all || rn.length < MIN_C320C_LEN) {
2831 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2832 ctrl->timings[channel][slotrank] = saved_timings[channel][slotrank];
2833 }
2834 return 0;
2835 }
2836 }
2837 }
2838 return 1;
2839}
2840
Patrick Rudolph371d2912015-10-09 13:33:25 +02002841/* Adjust CMD phase shift and try multiple command rates.
2842 * A command rate of 2T doubles the time needed for address and
2843 * command decode. */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002844static void command_training(ramctr_timing * ctrl)
2845{
2846 int channel;
2847
2848 FOR_ALL_POPULATED_CHANNELS {
2849 fill_pattern5(ctrl, channel, 0);
2850 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2851 }
2852
2853 /* try command rate 1T and 2T */
2854 if (!try_cmd_stretch(ctrl, 0) && !try_cmd_stretch(ctrl, 2))
2855 die("c320c discovery failed");
2856
2857 FOR_ALL_POPULATED_CHANNELS {
2858 program_timings(ctrl, channel);
2859 }
2860
2861 reprogram_320c(ctrl);
2862}
2863
2864static void discover_edges_real(ramctr_timing * ctrl, int channel, int slotrank,
2865 int *edges)
2866{
2867 int edge;
2868 int statistics[NUM_LANES][MAX_EDGE_TIMING + 1];
2869 int lane;
2870
2871 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2872 FOR_ALL_LANES {
2873 ctrl->timings[channel][slotrank].lanes[lane].rising =
2874 edge;
2875 ctrl->timings[channel][slotrank].lanes[lane].falling =
2876 edge;
2877 }
2878 printram("edge %02x\n", edge);
2879 program_timings(ctrl, channel);
2880
2881 FOR_ALL_LANES {
2882 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2883 4 * lane, 0);
2884 read32(DEFAULT_MCHBAR + 0x400 * channel + 4 * lane +
2885 0x4140);
2886 }
2887
2888 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002889 /* DRAM command MRS
2890 * write MR3 MPR enable
2891 * in this mode only RD and RDA are allowed
2892 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002893 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
2894 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2895 (0xc01 | (ctrl->tMOD << 16)));
2896 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2897 (slotrank << 24) | 0x360004);
2898 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2899
Patrick Rudolph371d2912015-10-09 13:33:25 +02002900 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002901 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
2902 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x40411f4);
2903 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2904 (slotrank << 24));
2905 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2906
Patrick Rudolph371d2912015-10-09 13:33:25 +02002907 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002908 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2909 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2910 0x1001 | ((ctrl->CAS + 8) << 16));
2911 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2912 (slotrank << 24) | 0x60000);
2913 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2914
Patrick Rudolph371d2912015-10-09 13:33:25 +02002915 /* DRAM command MRS
2916 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002917 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
2918 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2919 (0xc01 | (ctrl->tMOD << 16)));
2920 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2921 (slotrank << 24) | 0x360000);
2922 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2923
2924 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2925
2926 wait_428c(channel);
2927
2928 FOR_ALL_LANES {
2929 statistics[lane][edge] =
2930 read32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2931 lane * 4);
2932 }
2933 }
2934 FOR_ALL_LANES {
2935 struct run rn =
2936 get_longest_zero_run(statistics[lane], MAX_EDGE_TIMING + 1);
2937 edges[lane] = rn.middle;
2938 if (rn.all)
2939 die("edge discovery failed");
2940 printram("eval %d, %d, %d, %02x\n", channel, slotrank,
2941 lane, edges[lane]);
2942 }
2943}
2944
2945static void discover_edges(ramctr_timing * ctrl)
2946{
2947 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2948 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2949 int channel, slotrank, lane;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002950
2951 write32(DEFAULT_MCHBAR + 0x3400, 0);
2952
Patrick Rudolph9b515682015-10-09 13:43:51 +02002953 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002954
2955 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
2956 write32(DEFAULT_MCHBAR + 4 * lane +
2957 0x400 * channel + 0x4080, 0);
2958 }
2959
2960 FOR_ALL_POPULATED_CHANNELS {
2961 fill_pattern0(ctrl, channel, 0, 0);
2962 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2963 FOR_ALL_LANES {
2964 read32(DEFAULT_MCHBAR + 0x400 * channel +
2965 lane * 4 + 0x4140);
2966 }
2967
2968 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2969 ctrl->timings[channel][slotrank].lanes[lane].falling =
2970 16;
2971 ctrl->timings[channel][slotrank].lanes[lane].rising =
2972 16;
2973 }
2974
2975 program_timings(ctrl, channel);
2976
2977 FOR_ALL_POPULATED_RANKS {
2978 wait_428c(channel);
2979
Patrick Rudolph371d2912015-10-09 13:33:25 +02002980 /* DRAM command MRS
2981 * MR3 enable MPR
2982 * write MR3 MPR enable
2983 * in this mode only RD and RDA are allowed
2984 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002985 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2986 0x1f000);
2987 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2988 0xc01 | (ctrl->tMOD << 16));
2989 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2990 (slotrank << 24) | 0x360004);
2991 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2992
Patrick Rudolph371d2912015-10-09 13:33:25 +02002993 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002994 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2995 0x1f105);
2996 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2997 0x4041003);
2998 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2999 (slotrank << 24) | 0);
3000 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3001
Patrick Rudolph371d2912015-10-09 13:33:25 +02003002 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003003 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3004 0x1f105);
3005 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3006 0x1001 | ((ctrl->CAS + 8) << 16));
3007 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3008 (slotrank << 24) | 0x60000);
3009 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3010
Patrick Rudolph371d2912015-10-09 13:33:25 +02003011 /* DRAM command MRS
3012 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003013 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3014 0x1f000);
3015 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3016 0xc01 | (ctrl->tMOD << 16));
3017 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3018 (slotrank << 24) | 0x360000);
3019 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3020 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3021 0xc0001);
3022
3023 wait_428c(channel);
3024 }
3025
Patrick Rudolph371d2912015-10-09 13:33:25 +02003026 /* XXX: check any measured value ? */
3027
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003028 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3029 ctrl->timings[channel][slotrank].lanes[lane].falling =
3030 48;
3031 ctrl->timings[channel][slotrank].lanes[lane].rising =
3032 48;
3033 }
3034
3035 program_timings(ctrl, channel);
3036
3037 FOR_ALL_POPULATED_RANKS {
3038 wait_428c(channel);
3039
Patrick Rudolph371d2912015-10-09 13:33:25 +02003040 /* DRAM command MRS
3041 * MR3 enable MPR
3042 * write MR3 MPR enable
3043 * in this mode only RD and RDA are allowed
3044 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003045 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3046 0x1f000);
3047 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3048 0xc01 | (ctrl->tMOD << 16));
3049 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3050 (slotrank << 24) | 0x360004);
3051 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3052
Patrick Rudolph371d2912015-10-09 13:33:25 +02003053 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003054 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3055 0x1f105);
3056 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3057 0x4041003);
3058 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3059 (slotrank << 24) | 0);
3060 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3061
Patrick Rudolph371d2912015-10-09 13:33:25 +02003062 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003063 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3064 0x1f105);
3065 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3066 0x1001 | ((ctrl->CAS + 8) << 16));
3067 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3068 (slotrank << 24) | 0x60000);
3069 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3070
Patrick Rudolph371d2912015-10-09 13:33:25 +02003071 /* DRAM command MRS
3072 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003073 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3074 0x1f000);
3075 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3076 0xc01 | (ctrl->tMOD << 16));
3077 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3078 (slotrank << 24) | 0x360000);
3079 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3080
3081 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3082 0xc0001);
3083 wait_428c(channel);
3084 }
3085
Patrick Rudolph371d2912015-10-09 13:33:25 +02003086 /* XXX: check any measured value ? */
3087
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003088 FOR_ALL_LANES {
3089 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel +
3090 lane * 4,
3091 ~read32(DEFAULT_MCHBAR + 0x4040 +
3092 0x400 * channel + lane * 4) & 0xff);
3093 }
3094
3095 fill_pattern0(ctrl, channel, 0, 0xffffffff);
3096 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3097 }
3098
3099 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3100 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
3101
3102 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3103 discover_edges_real(ctrl, channel, slotrank,
3104 falling_edges[channel][slotrank]);
3105 }
3106
3107 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
3108
3109 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3110 discover_edges_real(ctrl, channel, slotrank,
3111 rising_edges[channel][slotrank]);
3112 }
3113
3114 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3115
3116 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3117 ctrl->timings[channel][slotrank].lanes[lane].falling =
3118 falling_edges[channel][slotrank][lane];
3119 ctrl->timings[channel][slotrank].lanes[lane].rising =
3120 rising_edges[channel][slotrank][lane];
3121 }
3122
3123 FOR_ALL_POPULATED_CHANNELS {
3124 program_timings(ctrl, channel);
3125 }
3126
3127 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3128 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3129 0);
3130 }
3131}
3132
3133static void discover_edges_write_real(ramctr_timing * ctrl, int channel,
3134 int slotrank, int *edges)
3135{
3136 int edge;
3137 u32 raw_statistics[MAX_EDGE_TIMING + 1];
3138 int statistics[MAX_EDGE_TIMING + 1];
3139 const int reg3000b24[] = { 0, 0xc, 0x2c };
3140 int lane, i;
3141 int lower[NUM_LANES];
3142 int upper[NUM_LANES];
3143 int pat;
3144
3145 FOR_ALL_LANES {
3146 lower[lane] = 0;
3147 upper[lane] = MAX_EDGE_TIMING;
3148 }
3149
3150 for (i = 0; i < 3; i++) {
3151 write32(DEFAULT_MCHBAR + 0x3000 + 0x100 * channel,
3152 reg3000b24[i] << 24);
3153 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3154 fill_pattern5(ctrl, channel, pat);
3155 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
3156 printram("patterned\n");
3157 printram("[%x] = 0x%08x\n(%d, %d)\n",
3158 0x3000 + 0x100 * channel, reg3000b24[i] << 24, channel,
3159 slotrank);
3160 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
3161 FOR_ALL_LANES {
3162 ctrl->timings[channel][slotrank].lanes[lane].
3163 rising = edge;
3164 ctrl->timings[channel][slotrank].lanes[lane].
3165 falling = edge;
3166 }
3167 program_timings(ctrl, channel);
3168
3169 FOR_ALL_LANES {
3170 write32(DEFAULT_MCHBAR + 0x4340 +
3171 0x400 * channel + 4 * lane, 0);
3172 read32(DEFAULT_MCHBAR + 0x400 * channel +
3173 4 * lane + 0x4140);
3174 }
3175 wait_428c(channel);
3176
Patrick Rudolph371d2912015-10-09 13:33:25 +02003177 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003178 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3179 0x1f006);
3180 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3181 0x4 | (ctrl->tRCD << 16)
3182 | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) <<
3183 10));
3184 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3185 (slotrank << 24) | 0x60000);
3186 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel,
3187 0x240);
3188
Patrick Rudolph371d2912015-10-09 13:33:25 +02003189 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003190 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3191 0x1f201);
3192 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3193 0x8005020 | ((ctrl->tWTR + ctrl->CWL + 8) <<
3194 16));
3195 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3196 (slotrank << 24));
3197 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel,
3198 0x242);
3199
Patrick Rudolph371d2912015-10-09 13:33:25 +02003200 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003201 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3202 0x1f105);
3203 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3204 0x4005020 | (max(ctrl->tRTP, 8) << 16));
3205 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3206 (slotrank << 24));
3207 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel,
3208 0x242);
3209
Patrick Rudolph371d2912015-10-09 13:33:25 +02003210 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003211 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3212 0x1f002);
3213 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3214 0xc01 | (ctrl->tRP << 16));
3215 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3216 (slotrank << 24) | 0x60400);
3217 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3218
3219 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3220 0xc0001);
3221 wait_428c(channel);
3222 FOR_ALL_LANES {
3223 read32(DEFAULT_MCHBAR + 0x4340 +
3224 0x400 * channel + lane * 4);
3225 }
3226
3227 raw_statistics[edge] =
3228 MCHBAR32(0x436c + 0x400 * channel);
3229 }
3230 FOR_ALL_LANES {
3231 struct run rn;
3232 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
3233 statistics[edge] =
3234 ! !(raw_statistics[edge] & (1 << lane));
3235 rn = get_longest_zero_run(statistics,
3236 MAX_EDGE_TIMING + 1);
3237 printram("edges: %d, %d, %d: 0x%x-0x%x-0x%x, 0x%x-0x%x\n",
3238 channel, slotrank, i, rn.start, rn.middle,
3239 rn.end, rn.start + ctrl->edge_offset[i],
3240 rn.end - ctrl->edge_offset[i]);
3241 lower[lane] =
3242 max(rn.start + ctrl->edge_offset[i], lower[lane]);
3243 upper[lane] =
3244 min(rn.end - ctrl->edge_offset[i], upper[lane]);
3245 edges[lane] = (lower[lane] + upper[lane]) / 2;
Patrick Rudolph9733e282015-08-16 17:06:30 +02003246 if (rn.all || (lower[lane] > upper[lane]))
3247 die("edge write discovery failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003248
3249 }
3250 }
3251 }
3252
3253 write32(DEFAULT_MCHBAR + 0x3000, 0);
3254 printram("CPA\n");
3255}
3256
3257static void discover_edges_write(ramctr_timing * ctrl)
3258{
3259 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3260 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3261 int channel, slotrank, lane;
3262
3263 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3264 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
3265
3266 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3267 discover_edges_write_real(ctrl, channel, slotrank,
3268 falling_edges[channel][slotrank]);
3269 }
3270
3271 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
3272
3273 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3274 discover_edges_write_real(ctrl, channel, slotrank,
3275 rising_edges[channel][slotrank]);
3276 }
3277
3278 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3279
3280 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3281 ctrl->timings[channel][slotrank].lanes[lane].falling =
3282 falling_edges[channel][slotrank][lane];
3283 ctrl->timings[channel][slotrank].lanes[lane].rising =
3284 rising_edges[channel][slotrank][lane];
3285 }
3286
3287 FOR_ALL_POPULATED_CHANNELS
3288 program_timings(ctrl, channel);
3289
3290 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3291 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3292 0);
3293 }
3294}
3295
3296static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
3297{
3298 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003299 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003300 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
3301 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3302 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD)
3303 << 10) | (ctrl->tRCD << 16) | 4);
3304 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3305 (slotrank << 24) | 0x60000);
3306 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
3307
Patrick Rudolph371d2912015-10-09 13:33:25 +02003308 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003309 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
3310 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3311 0x80011e0 |
3312 ((ctrl->tWTR + ctrl->CWL + 8) << 16));
3313 write32(DEFAULT_MCHBAR + 0x4204 +
3314 0x400 * channel, (slotrank << 24));
3315 write32(DEFAULT_MCHBAR + 0x4214 +
3316 0x400 * channel, 0x242);
3317
Patrick Rudolph371d2912015-10-09 13:33:25 +02003318 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003319 write32(DEFAULT_MCHBAR + 0x4228 +
3320 0x400 * channel, 0x1f105);
3321 write32(DEFAULT_MCHBAR + 0x4238 +
3322 0x400 * channel,
3323 0x40011e0 | (max(ctrl->tRTP, 8) << 16));
3324 write32(DEFAULT_MCHBAR + 0x4208 +
3325 0x400 * channel, (slotrank << 24));
3326 write32(DEFAULT_MCHBAR + 0x4218 +
3327 0x400 * channel, 0x242);
3328
Patrick Rudolph371d2912015-10-09 13:33:25 +02003329 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003330 write32(DEFAULT_MCHBAR + 0x422c +
3331 0x400 * channel, 0x1f002);
3332 write32(DEFAULT_MCHBAR + 0x423c +
3333 0x400 * channel,
3334 0x1001 | (ctrl->tRP << 16));
3335 write32(DEFAULT_MCHBAR + 0x420c +
3336 0x400 * channel,
3337 (slotrank << 24) | 0x60400);
3338 write32(DEFAULT_MCHBAR + 0x421c +
3339 0x400 * channel, 0);
3340
3341 write32(DEFAULT_MCHBAR + 0x4284 +
3342 0x400 * channel, 0xc0001);
3343 wait_428c(channel);
3344}
3345
3346static void discover_timC_write(ramctr_timing * ctrl)
3347{
3348 const u8 rege3c_b24[3] = { 0, 0xf, 0x2f };
3349 int i, pat;
3350
3351 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3352 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3353 int channel, slotrank, lane;
3354
3355 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3356 lower[channel][slotrank][lane] = 0;
3357 upper[channel][slotrank][lane] = MAX_TIMC;
3358 }
3359
3360 write32(DEFAULT_MCHBAR + 0x4ea8, 1);
3361
3362 for (i = 0; i < 3; i++)
3363 FOR_ALL_POPULATED_CHANNELS {
3364 write32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100),
3365 (rege3c_b24[i] << 24)
3366 | (read32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100))
3367 & ~0x3f000000));
3368 udelay(2);
3369 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3370 FOR_ALL_POPULATED_RANKS {
3371 int timC;
3372 u32 raw_statistics[MAX_TIMC + 1];
3373 int statistics[MAX_TIMC + 1];
3374
3375 fill_pattern5(ctrl, channel, pat);
3376 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
3377 for (timC = 0; timC < MAX_TIMC + 1; timC++) {
3378 FOR_ALL_LANES
3379 ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
3380 program_timings(ctrl, channel);
3381
3382 test_timC_write (ctrl, channel, slotrank);
3383
3384 raw_statistics[timC] =
3385 MCHBAR32(0x436c + 0x400 * channel);
3386 }
3387 FOR_ALL_LANES {
3388 struct run rn;
3389 for (timC = 0; timC <= MAX_TIMC; timC++)
3390 statistics[timC] =
3391 !!(raw_statistics[timC] &
3392 (1 << lane));
3393 rn = get_longest_zero_run(statistics,
3394 MAX_TIMC + 1);
3395 if (rn.all)
3396 die("timC write discovery failed");
3397 printram("timC: %d, %d, %d: 0x%x-0x%x-0x%x, 0x%x-0x%x\n",
3398 channel, slotrank, i, rn.start,
3399 rn.middle, rn.end,
3400 rn.start + ctrl->timC_offset[i],
3401 rn.end - ctrl->timC_offset[i]);
3402 lower[channel][slotrank][lane] =
3403 max(rn.start + ctrl->timC_offset[i],
3404 lower[channel][slotrank][lane]);
3405 upper[channel][slotrank][lane] =
3406 min(rn.end - ctrl->timC_offset[i],
3407 upper[channel][slotrank][lane]);
3408
3409 }
3410 }
3411 }
3412 }
3413
3414 FOR_ALL_CHANNELS {
3415 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3416 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3417 ~0x3f000000));
3418 udelay(2);
3419 }
3420
3421 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3422
3423 printram("CPB\n");
3424
3425 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3426 printram("timC [%d, %d, %d] = 0x%x\n", channel,
3427 slotrank, lane,
3428 (lower[channel][slotrank][lane] +
3429 upper[channel][slotrank][lane]) / 2);
3430 ctrl->timings[channel][slotrank].lanes[lane].timC =
3431 (lower[channel][slotrank][lane] +
3432 upper[channel][slotrank][lane]) / 2;
3433 }
3434 FOR_ALL_POPULATED_CHANNELS {
3435 program_timings(ctrl, channel);
3436 }
3437}
3438
3439static void normalize_training(ramctr_timing * ctrl)
3440{
3441 int channel, slotrank, lane;
3442 int mat = 0;
3443
3444 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3445 int delta;
3446 FOR_ALL_LANES mat =
3447 max(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
3448 delta = (mat >> 6) - ctrl->timings[channel][slotrank].val_4028;
3449 ctrl->timings[channel][slotrank].val_4024 += delta;
3450 ctrl->timings[channel][slotrank].val_4028 += delta;
3451 }
3452
3453 FOR_ALL_POPULATED_CHANNELS {
3454 program_timings(ctrl, channel);
3455 }
3456}
3457
3458static void write_controller_mr(ramctr_timing * ctrl)
3459{
3460 int channel, slotrank;
3461
3462 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3463 write32(DEFAULT_MCHBAR + 0x0004 + (channel << 8) +
3464 lane_registers[slotrank], make_mr0(ctrl, slotrank));
3465 write32(DEFAULT_MCHBAR + 0x0008 + (channel << 8) +
Patrick Rudolph7e513d12016-01-10 14:22:34 +01003466 lane_registers[slotrank],
3467 make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003468 }
3469}
3470
3471static void channel_test(ramctr_timing * ctrl)
3472{
3473 int channel, slotrank, lane;
3474
3475 FOR_ALL_POPULATED_CHANNELS
3476 if (read32(DEFAULT_MCHBAR + 0x42a0 + (channel << 10)) & 0xa000)
3477 die("Mini channel test failed (1)\n");
3478 FOR_ALL_POPULATED_CHANNELS {
3479 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
3480
3481 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3482 }
3483
3484 for (slotrank = 0; slotrank < 4; slotrank++)
3485 FOR_ALL_CHANNELS
3486 if (ctrl->rankmap[channel] & (1 << slotrank)) {
3487 FOR_ALL_LANES {
3488 write32(DEFAULT_MCHBAR + (0x4f40 + 4 * lane), 0);
3489 write32(DEFAULT_MCHBAR + (0x4d40 + 4 * lane), 0);
3490 }
3491 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003492 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003493 write32(DEFAULT_MCHBAR + 0x4220 + (channel << 10), 0x0001f006);
3494 write32(DEFAULT_MCHBAR + 0x4230 + (channel << 10), 0x0028a004);
3495 write32(DEFAULT_MCHBAR + 0x4200 + (channel << 10),
3496 0x00060000 | (slotrank << 24));
3497 write32(DEFAULT_MCHBAR + 0x4210 + (channel << 10), 0x00000244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003498 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003499 write32(DEFAULT_MCHBAR + 0x4224 + (channel << 10), 0x0001f201);
3500 write32(DEFAULT_MCHBAR + 0x4234 + (channel << 10), 0x08281064);
3501 write32(DEFAULT_MCHBAR + 0x4204 + (channel << 10),
3502 0x00000000 | (slotrank << 24));
3503 write32(DEFAULT_MCHBAR + 0x4214 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003504 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003505 write32(DEFAULT_MCHBAR + 0x4228 + (channel << 10), 0x0001f105);
3506 write32(DEFAULT_MCHBAR + 0x4238 + (channel << 10), 0x04281064);
3507 write32(DEFAULT_MCHBAR + 0x4208 + (channel << 10),
3508 0x00000000 | (slotrank << 24));
3509 write32(DEFAULT_MCHBAR + 0x4218 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003510 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003511 write32(DEFAULT_MCHBAR + 0x422c + (channel << 10), 0x0001f002);
3512 write32(DEFAULT_MCHBAR + 0x423c + (channel << 10), 0x00280c01);
3513 write32(DEFAULT_MCHBAR + 0x420c + (channel << 10),
3514 0x00060400 | (slotrank << 24));
3515 write32(DEFAULT_MCHBAR + 0x421c + (channel << 10), 0x00000240);
3516 write32(DEFAULT_MCHBAR + 0x4284 + (channel << 10), 0x000c0001);
3517 wait_428c(channel);
3518 FOR_ALL_LANES
3519 if (read32(DEFAULT_MCHBAR + 0x4340 + (channel << 10) + 4 * lane))
3520 die("Mini channel test failed (2)\n");
3521 }
3522}
3523
3524static void set_scrambling_seed(ramctr_timing * ctrl)
3525{
3526 int channel;
3527
3528 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them?
3529 I don't think so. */
3530 static u32 seeds[NUM_CHANNELS][3] = {
3531 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
3532 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
3533 };
3534 FOR_ALL_POPULATED_CHANNELS {
3535 MCHBAR32(0x4020 + 0x400 * channel) &= ~0x10000000;
3536 write32(DEFAULT_MCHBAR + 0x4034, seeds[channel][0]);
3537 write32(DEFAULT_MCHBAR + 0x403c, seeds[channel][1]);
3538 write32(DEFAULT_MCHBAR + 0x4038, seeds[channel][2]);
3539 }
3540}
3541
3542static void set_4f8c(void)
3543{
3544 struct cpuid_result cpures;
3545 u32 cpu;
3546
3547 cpures = cpuid(0);
3548 cpu = (cpures.eax);
3549 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
3550 MCHBAR32(0x4f8c) = 0x141D1519;
3551 } else {
3552 MCHBAR32(0x4f8c) = 0x551D1519;
3553 }
3554}
3555
3556static void prepare_training(ramctr_timing * ctrl)
3557{
3558 int channel;
3559
3560 FOR_ALL_POPULATED_CHANNELS {
3561 // Always drive command bus
3562 MCHBAR32(0x4004 + 0x400 * channel) |= 0x20000000;
3563 }
3564
3565 udelay(1);
3566
3567 FOR_ALL_POPULATED_CHANNELS {
3568 wait_428c(channel);
3569 }
3570}
3571
3572static void set_4008c(ramctr_timing * ctrl)
3573{
3574 int channel, slotrank;
3575 u32 reg;
3576 FOR_ALL_POPULATED_CHANNELS {
3577 u32 b20, b4_8_12;
3578 int min_320c = 10000;
3579 int max_320c = -10000;
3580
3581 FOR_ALL_POPULATED_RANKS {
3582 max_320c = max(ctrl->timings[channel][slotrank].val_320c, max_320c);
3583 min_320c = min(ctrl->timings[channel][slotrank].val_320c, min_320c);
3584 }
3585
3586 if (max_320c - min_320c > 51)
3587 b20 = 0;
3588 else
3589 b20 = ctrl->ref_card_offset[channel];
3590
3591 if (ctrl->reg_320c_range_threshold < max_320c - min_320c)
3592 b4_8_12 = 0x3330;
3593 else
3594 b4_8_12 = 0x2220;
3595
3596 reg = read32(DEFAULT_MCHBAR + 0x400c + (channel << 10));
3597 write32(DEFAULT_MCHBAR + 0x400c + (channel << 10),
3598 (reg & 0xFFF0FFFF)
3599 | (ctrl->ref_card_offset[channel] << 16)
3600 | (ctrl->ref_card_offset[channel] << 18));
3601 write32(DEFAULT_MCHBAR + 0x4008 + (channel << 10),
3602 0x0a000000
3603 | (b20 << 20)
3604 | ((ctrl->ref_card_offset[channel] + 2) << 16)
3605 | b4_8_12);
3606 }
3607}
3608
3609static void set_42a0(ramctr_timing * ctrl)
3610{
3611 int channel;
3612 FOR_ALL_POPULATED_CHANNELS {
3613 write32(DEFAULT_MCHBAR + (0x42a0 + 0x400 * channel),
3614 0x00001000 | ctrl->rankmap[channel]);
3615 MCHBAR32(0x4004 + 0x400 * channel) &= ~0x20000000; // OK
3616 }
3617}
3618
3619static int encode_5d10(int ns)
3620{
3621 return (ns + 499) / 500;
3622}
3623
3624/* FIXME: values in this function should be hardware revision-dependent. */
3625static void final_registers(ramctr_timing * ctrl)
3626{
3627 int channel;
3628 int t1_cycles = 0, t1_ns = 0, t2_ns;
3629 int t3_ns;
3630 u32 r32;
3631
3632 write32(DEFAULT_MCHBAR + 0x4cd4, 0x00000046);
3633
3634 write32(DEFAULT_MCHBAR + 0x400c, (read32(DEFAULT_MCHBAR + 0x400c) & 0xFFFFCFFF) | 0x1000); // OK
3635 write32(DEFAULT_MCHBAR + 0x440c, (read32(DEFAULT_MCHBAR + 0x440c) & 0xFFFFCFFF) | 0x1000); // OK
3636 write32(DEFAULT_MCHBAR + 0x4cb0, 0x00000740);
3637 write32(DEFAULT_MCHBAR + 0x4380, 0x00000aaa); // OK
3638 write32(DEFAULT_MCHBAR + 0x4780, 0x00000aaa); // OK
3639 write32(DEFAULT_MCHBAR + 0x4f88, 0x5f7003ff); // OK
3640 write32(DEFAULT_MCHBAR + 0x5064, 0x00073000 | ctrl->reg_5064b0); // OK
3641
3642 FOR_ALL_CHANNELS {
3643 switch (ctrl->rankmap[channel]) {
3644 /* Unpopulated channel. */
3645 case 0:
3646 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0);
3647 break;
3648 /* Only single-ranked dimms. */
3649 case 1:
3650 case 4:
3651 case 5:
3652 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x373131);
3653 break;
3654 /* Dual-ranked dimms present. */
3655 default:
3656 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x9b6ea1);
3657 break;
3658 }
3659 }
3660
3661 write32 (DEFAULT_MCHBAR + 0x5880, 0xca9171e5);
3662 write32 (DEFAULT_MCHBAR + 0x5888,
3663 (read32 (DEFAULT_MCHBAR + 0x5888) & ~0xffffff) | 0xe4d5d0);
3664 write32 (DEFAULT_MCHBAR + 0x58a8, read32 (DEFAULT_MCHBAR + 0x58a8) & ~0x1f);
3665 write32 (DEFAULT_MCHBAR + 0x4294,
3666 (read32 (DEFAULT_MCHBAR + 0x4294) & ~0x30000)
3667 | (1 << 16));
3668 write32 (DEFAULT_MCHBAR + 0x4694,
3669 (read32 (DEFAULT_MCHBAR + 0x4694) & ~0x30000)
3670 | (1 << 16));
3671
3672 MCHBAR32(0x5030) |= 1; // OK
3673 MCHBAR32(0x5030) |= 0x80; // OK
3674 MCHBAR32(0x5f18) = 0xfa; // OK
3675
3676 /* Find a populated channel. */
3677 FOR_ALL_POPULATED_CHANNELS
3678 break;
3679
3680 t1_cycles = ((read32(DEFAULT_MCHBAR + 0x4290 + channel * 0x400) >> 8) & 0xff);
3681 r32 = read32(DEFAULT_MCHBAR + 0x5064);
3682 if (r32 & 0x20000)
3683 t1_cycles += (r32 & 0xfff);
3684 t1_cycles += (read32(DEFAULT_MCHBAR + channel * 0x400 + 0x42a4) & 0xfff);
3685 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
3686 if (!(r32 & 0x20000))
3687 t1_ns += 500;
3688
3689 t2_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f10) >> 8) & 0xfff);
3690 if ( read32(DEFAULT_MCHBAR + 0x5f00) & 8 )
3691 {
3692 t3_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f20) >> 8) & 0xfff);
3693 t3_ns += 10 * (read32(DEFAULT_MCHBAR + 0x5f18) & 0xff);
3694 }
3695 else
3696 {
3697 t3_ns = 500;
3698 }
3699 printk(BIOS_DEBUG, "t123: %d, %d, %d\n",
3700 t1_ns, t2_ns, t3_ns);
3701 write32 (DEFAULT_MCHBAR + 0x5d10,
3702 ((encode_5d10(t1_ns) + encode_5d10(t2_ns)) << 16)
3703 | (encode_5d10(t1_ns) << 8)
3704 | ((encode_5d10(t3_ns) + encode_5d10(t2_ns) + encode_5d10(t1_ns)) << 24)
3705 | (read32(DEFAULT_MCHBAR + 0x5d10) & 0xC0C0C0C0)
3706 | 0xc);
3707}
3708
3709static void save_timings(ramctr_timing * ctrl)
3710{
3711 struct mrc_data_container *mrcdata;
3712 int output_len = ALIGN(sizeof (*ctrl), 16);
3713
3714 /* Save the MRC S3 restore data to cbmem */
3715 mrcdata = cbmem_add
3716 (CBMEM_ID_MRCDATA,
3717 output_len + sizeof(struct mrc_data_container));
3718
3719 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
3720 ctrl, mrcdata, output_len);
3721
3722 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
3723 mrcdata->mrc_data_size = output_len;
3724 mrcdata->reserved = 0;
3725 memcpy(mrcdata->mrc_data, ctrl, sizeof (*ctrl));
3726
3727 /* Zero the unused space in aligned buffer. */
3728 if (output_len > sizeof (*ctrl))
3729 memset(mrcdata->mrc_data+sizeof (*ctrl), 0,
3730 output_len - sizeof (*ctrl));
3731
3732 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
3733 mrcdata->mrc_data_size);
3734}
3735
3736static void restore_timings(ramctr_timing * ctrl)
3737{
3738 int channel, slotrank, lane;
3739
3740 FOR_ALL_POPULATED_CHANNELS
3741 MCHBAR32(0x4004 + 0x400 * channel) =
3742 ctrl->tRRD
3743 | (ctrl->tRTP << 4)
3744 | (ctrl->tCKE << 8)
3745 | (ctrl->tWTR << 12)
3746 | (ctrl->tFAW << 16)
3747 | (ctrl->tWR << 24)
3748 | (ctrl->cmd_stretch[channel] << 30);
3749
3750 udelay(1);
3751
3752 FOR_ALL_POPULATED_CHANNELS {
3753 wait_428c(channel);
3754 }
3755
3756 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3757 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
3758 + 4 * lane, 0);
3759 }
3760
3761 FOR_ALL_POPULATED_CHANNELS
3762 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
3763 read32(DEFAULT_MCHBAR + 0x4008 +
3764 0x400 * channel) | 0x8000000);
3765
3766 FOR_ALL_POPULATED_CHANNELS {
3767 udelay (1);
3768 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
3769 read32(DEFAULT_MCHBAR + 0x4020 +
3770 0x400 * channel) | 0x200000);
3771 }
3772
3773 printram("CPE\n");
3774
3775 write32(DEFAULT_MCHBAR + 0x3400, 0);
3776 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3777
3778 printram("CP5b\n");
3779
3780 FOR_ALL_POPULATED_CHANNELS {
3781 program_timings(ctrl, channel);
3782 }
3783
3784 u32 reg, addr;
3785
3786 while (!(MCHBAR32(0x5084) & 0x10000)) ;
3787 do {
3788 reg = MCHBAR32(0x428c);
3789 } while ((reg & 0x14) == 0);
3790
3791 // Set state of memory controller
3792 MCHBAR32(0x5030) = 0x116;
3793 MCHBAR32(0x4ea0) = 0;
3794
3795 // Wait 500us
3796 udelay(500);
3797
3798 FOR_ALL_CHANNELS {
3799 // Set valid rank CKE
3800 reg = 0;
3801 reg = (reg & ~0xf) | ctrl->rankmap[channel];
3802 addr = 0x400 * channel + 0x42a0;
3803 MCHBAR32(addr) = reg;
3804
3805 // Wait 10ns for ranks to settle
3806 //udelay(0.01);
3807
3808 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3809 MCHBAR32(addr) = reg;
3810
3811 // Write reset using a NOP
3812 write_reset(ctrl);
3813 }
3814
3815 /* mrs commands. */
3816 dram_mrscommands(ctrl);
3817
3818 printram("CP5c\n");
3819
3820 write32(DEFAULT_MCHBAR + 0x3000, 0);
3821
3822 FOR_ALL_CHANNELS {
3823 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3824 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3825 ~0x3f000000));
3826 udelay(2);
3827 }
3828
3829 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3830}
3831
3832void init_dram_ddr3(spd_raw_data * spds, int mobile, int min_tck,
3833 int s3resume)
3834{
3835 int me_uma_size;
3836 int cbmem_was_inited;
3837
3838 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +02003839
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -07003840 report_platform_info();
3841
Stefan Reinauer00636b02012-04-04 00:08:51 +02003842 /* Wait for ME to be ready */
3843 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003844 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003845
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003846 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +02003847
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003848 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +02003849
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003850 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003851
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003852 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +02003853
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003854 reg_5d10 = read32(DEFAULT_MCHBAR + 0x5d10); // !!! = 0x00000000
3855 if ((pcie_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
3856 && reg_5d10 && !s3resume) {
3857 write32(DEFAULT_MCHBAR + 0x5d10, 0);
3858 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +02003859 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003860
Patrick Georgi546953c2014-11-29 10:38:17 +01003861 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003862 }
Stefan Reinauer00636b02012-04-04 00:08:51 +02003863
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003864 ramctr_timing ctrl;
Vadim Bendebury48a4a7f2012-06-07 18:47:13 -07003865
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003866 memset(&ctrl, 0, sizeof (ctrl));
3867
3868 early_pch_init_native();
3869 early_thermal_init();
3870
3871 ctrl.mobile = mobile;
3872 ctrl.tCK = min_tck;
3873
3874 /* FIXME: for non-S3 we should be able to use timing caching with
3875 proper verification. Right now we use timings only for S3 case.
3876 */
3877 if (s3resume) {
3878 struct mrc_data_container *mrc_cache;
3879
3880 mrc_cache = find_current_mrc_cache();
3881 if (!mrc_cache || mrc_cache->mrc_data_size < sizeof (ctrl)) {
3882 /* Failed S3 resume, reset to come up cleanly */
3883 outb(0x6, 0xcf9);
3884 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003885 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003886 memcpy(&ctrl, mrc_cache->mrc_data, sizeof (ctrl));
Stefan Reinauer00636b02012-04-04 00:08:51 +02003887 }
3888
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003889 if (!s3resume) {
3890 dimm_info info;
Sven Schnelled4ee8082012-07-28 09:28:56 +02003891
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003892 /* Get DDR3 SPD data */
3893 dram_find_spds_ddr3(spds, &info, &ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +02003894
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003895 /* Find fastest common supported parameters */
3896 dram_find_common_params(&info, &ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +02003897
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003898 dram_dimm_mapping(&info, &ctrl);
3899 }
3900
3901 /* Set MCU frequency */
3902 dram_freq(&ctrl);
3903
3904 if (!s3resume) {
3905 /* Calculate timings */
3906 dram_timing(&ctrl);
3907 }
3908
3909 /* Set version register */
3910 MCHBAR32(0x5034) = 0xC04EB002;
3911
3912 /* Enable crossover */
3913 dram_xover(&ctrl);
3914
3915 /* Set timing and refresh registers */
3916 dram_timing_regs(&ctrl);
3917
3918 /* Power mode preset */
3919 MCHBAR32(0x4e80) = 0x5500;
3920
3921 /* Set scheduler parameters */
3922 MCHBAR32(0x4c20) = 0x10100005;
3923
3924 /* Set cpu specific register */
3925 set_4f8c();
3926
3927 /* Clear IO reset bit */
3928 MCHBAR32(0x5030) &= ~0x20;
3929
3930 /* Set MAD-DIMM registers */
3931 dram_dimm_set_mapping(&ctrl);
3932 printk(BIOS_DEBUG, "Done dimm mapping\n");
3933
3934 /* Zone config */
3935 dram_zones(&ctrl, 1);
3936
3937 /* Set memory map */
3938 dram_memorymap(&ctrl, me_uma_size);
3939 printk(BIOS_DEBUG, "Done memory map\n");
3940
3941 /* Set IO registers */
3942 dram_ioregs(&ctrl);
3943 printk(BIOS_DEBUG, "Done io registers\n");
3944
3945 udelay(1);
3946
3947 if (s3resume) {
3948 restore_timings(&ctrl);
3949 } else {
3950 /* Do jedec ddr3 reset sequence */
3951 dram_jedecreset(&ctrl);
3952 printk(BIOS_DEBUG, "Done jedec reset\n");
3953
3954 /* MRS commands */
3955 dram_mrscommands(&ctrl);
3956 printk(BIOS_DEBUG, "Done MRS commands\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003957
3958 /* Prepare for memory training */
3959 prepare_training(&ctrl);
3960
3961 read_training(&ctrl);
3962 write_training(&ctrl);
3963
3964 printram("CP5a\n");
3965
3966 discover_edges(&ctrl);
3967
3968 printram("CP5b\n");
3969
3970 command_training(&ctrl);
3971
3972 printram("CP5c\n");
3973
3974 discover_edges_write(&ctrl);
3975
3976 discover_timC_write(&ctrl);
3977
3978 normalize_training(&ctrl);
3979 }
3980
3981 set_4008c(&ctrl);
3982
3983 write_controller_mr(&ctrl);
3984
3985 if (!s3resume) {
3986 channel_test(&ctrl);
3987 }
3988
3989 /* FIXME: should be hardware revision-dependent. */
3990 write32(DEFAULT_MCHBAR + 0x5024, 0x00a030ce);
3991
3992 set_scrambling_seed(&ctrl);
3993
3994 set_42a0(&ctrl);
3995
3996 final_registers(&ctrl);
3997
3998 /* Zone config */
3999 dram_zones(&ctrl, 0);
4000
4001 if (!s3resume)
4002 quick_ram_check();
4003
4004 intel_early_me_status();
4005 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
4006 intel_early_me_status();
4007
Stefan Reinauer00636b02012-04-04 00:08:51 +02004008 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004009
4010 cbmem_was_inited = !cbmem_recovery(s3resume);
4011 if (!s3resume)
4012 save_timings(&ctrl);
4013 if (s3resume && !cbmem_was_inited) {
4014 /* Failed S3 resume, reset to come up cleanly */
4015 outb(0x6, 0xcf9);
4016 halt();
4017 }
Stefan Reinauer00636b02012-04-04 00:08:51 +02004018}