blob: ed8e9d1a7f514c71c2e886233badd8e3b93a7bef [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
Stefan Reinauer00636b02012-04-04 00:08:51 +0200223/*
224 * Dump in the log memory controller configuration as read from the memory
225 * controller registers.
226 */
227static void report_memory_config(void)
228{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700229 u32 addr_decoder_common, addr_decode_ch[NUM_CHANNELS];
Stefan Reinauer00636b02012-04-04 00:08:51 +0200230 int i;
231
232 addr_decoder_common = MCHBAR32(0x5000);
233 addr_decode_ch[0] = MCHBAR32(0x5004);
234 addr_decode_ch[1] = MCHBAR32(0x5008);
235
236 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700237 (MCHBAR32(0x5e04) * 13333 * 2 + 50) / 100);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200238 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700239 addr_decoder_common & 3, (addr_decoder_common >> 2) & 3,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200240 (addr_decoder_common >> 4) & 3);
241
242 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
243 u32 ch_conf = addr_decode_ch[i];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700244 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i,
245 ch_conf);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200246 printk(BIOS_DEBUG, " ECC %s\n",
247 ecc_decoder[(ch_conf >> 24) & 3]);
248 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
249 ((ch_conf >> 22) & 1) ? "on" : "off");
250 printk(BIOS_DEBUG, " rank interleave %s\n",
251 ((ch_conf >> 21) & 1) ? "on" : "off");
252 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
253 ((ch_conf >> 0) & 0xff) * 256,
254 ((ch_conf >> 19) & 1) ? 16 : 8,
255 ((ch_conf >> 17) & 1) ? "dual" : "single",
256 ((ch_conf >> 16) & 1) ? "" : ", selected");
257 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
258 ((ch_conf >> 8) & 0xff) * 256,
259 ((ch_conf >> 20) & 1) ? 16 : 8,
260 ((ch_conf >> 18) & 1) ? "dual" : "single",
261 ((ch_conf >> 16) & 1) ? ", selected" : "");
262 }
263}
264
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700265void read_spd(spd_raw_data * spd, u8 addr)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200266{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700267 int j;
268 for (j = 0; j < 256; j++)
269 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
270}
271
272static void dram_find_spds_ddr3(spd_raw_data * spd, dimm_info * dimm,
273 ramctr_timing * ctrl)
274{
275 int dimms = 0;
276 int channel, slot, spd_slot;
277
278 memset (ctrl->rankmap, 0, sizeof (ctrl->rankmap));
279
280 ctrl->extended_temperature_range = 1;
281 ctrl->auto_self_refresh = 1;
282
283 FOR_ALL_CHANNELS {
284 ctrl->channel_size_mb[channel] = 0;
285
286 for (slot = 0; slot < NUM_SLOTS; slot++) {
287 spd_slot = 2 * channel + slot;
288 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
289 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
290 // set dimm invalid
291 dimm->dimm[channel][slot].ranks = 0;
292 dimm->dimm[channel][slot].size_mb = 0;
293 continue;
294 }
295
296 dram_print_spd_ddr3(&dimm->dimm[channel][slot]);
297 dimms++;
298 ctrl->rank_mirror[channel][slot * 2] = 0;
299 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->dimm[channel][slot].flags.pins_mirrored;
300 ctrl->channel_size_mb[channel] += dimm->dimm[channel][slot].size_mb;
301
302 ctrl->auto_self_refresh &= dimm->dimm[channel][slot].flags.asr;
303 ctrl->extended_temperature_range &= dimm->dimm[channel][slot].flags.ext_temp_refresh;
304
305 ctrl->rankmap[channel] |= ((1 << dimm->dimm[channel][slot].ranks) - 1) << (2 * slot);
306 printk(BIOS_DEBUG, "rankmap[%d] = 0x%x\n", channel, ctrl->rankmap[channel]);
307 }
308 if ((ctrl->rankmap[channel] & 3) && (ctrl->rankmap[channel] & 0xc)
309 && dimm->dimm[channel][0].reference_card <= 5 && dimm->dimm[channel][1].reference_card <= 5) {
310 const int ref_card_offset_table[6][6] = {
311 { 0, 0, 0, 0, 2, 2, },
312 { 0, 0, 0, 0, 2, 2, },
313 { 0, 0, 0, 0, 2, 2, },
314 { 0, 0, 0, 0, 1, 1, },
315 { 2, 2, 2, 1, 0, 0, },
316 { 2, 2, 2, 1, 0, 0, },
317 };
318 ctrl->ref_card_offset[channel] = ref_card_offset_table[dimm->dimm[channel][0].reference_card]
319 [dimm->dimm[channel][1].reference_card];
320 } else
321 ctrl->ref_card_offset[channel] = 0;
322 }
323
324 if (!dimms)
325 die("No DIMMs were found");
326}
327
328static void dram_find_common_params(const dimm_info * dimms,
329 ramctr_timing * ctrl)
330{
331 size_t valid_dimms;
332 int channel, slot;
333 ctrl->cas_supported = 0xff;
334 valid_dimms = 0;
335 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
336 const dimm_attr *dimm = &dimms->dimm[channel][slot];
337 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
338 continue;
339 valid_dimms++;
340
341 /* Find all possible CAS combinations */
342 ctrl->cas_supported &= dimm->cas_supported;
343
344 /* Find the smallest common latencies supported by all DIMMs */
345 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
346 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
347 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
348 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
349 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
350 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
351 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
352 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
353 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
354 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
355 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
356 }
357
358 if (!ctrl->cas_supported)
359 die("Unsupported DIMM combination. "
360 "DIMMS do not support common CAS latency");
361 if (!valid_dimms)
362 die("No valid DIMMs found");
363}
364
365static u8 get_CWL(u8 CAS)
366{
367 /* Get CWL based on CAS using the following rule:
368 * _________________________________________
369 * CAS: | 4T | 5T | 6T | 7T | 8T | 9T | 10T | 11T |
370 * CWL: | 5T | 5T | 5T | 6T | 6T | 7T | 7T | 8T |
371 */
372 static const u8 cas_cwl_map[] = { 5, 5, 5, 6, 6, 7, 7, 8 };
373 if (CAS > 11)
374 return 8;
375 return cas_cwl_map[CAS - 4];
376}
377
378/* Frequency multiplier. */
379static u32 get_FRQ(u32 tCK)
380{
381 u32 FRQ;
382 FRQ = 256000 / (tCK * BASEFREQ);
383 if (FRQ > 8)
384 return 8;
385 if (FRQ < 3)
386 return 3;
387 return FRQ;
388}
389
390static u32 get_REFI(u32 tCK)
391{
392 /* Get REFI based on MCU frequency using the following rule:
393 * _________________________________________
394 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
395 * REFI: | 3120 | 4160 | 5200 | 6240 | 7280 | 8320 |
396 */
397 static const u32 frq_refi_map[] =
398 { 3120, 4160, 5200, 6240, 7280, 8320 };
399 return frq_refi_map[get_FRQ(tCK) - 3];
400}
401
402static u8 get_XSOffset(u32 tCK)
403{
404 /* Get XSOffset based on MCU frequency using the following rule:
405 * _________________________
406 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
407 * XSOffset : | 4 | 6 | 7 | 8 | 10 | 11 |
408 */
409 static const u8 frq_xs_map[] = { 4, 6, 7, 8, 10, 11 };
410 return frq_xs_map[get_FRQ(tCK) - 3];
411}
412
413static u8 get_MOD(u32 tCK)
414{
415 /* Get MOD based on MCU frequency using the following rule:
416 * _____________________________
417 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
418 * MOD : | 12 | 12 | 12 | 12 | 15 | 16 |
419 */
420 static const u8 frq_mod_map[] = { 12, 12, 12, 12, 15, 16 };
421 return frq_mod_map[get_FRQ(tCK) - 3];
422}
423
424static u8 get_WLO(u32 tCK)
425{
426 /* Get WLO based on MCU frequency using the following rule:
427 * _______________________
428 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
429 * WLO : | 4 | 5 | 6 | 6 | 8 | 8 |
430 */
431 static const u8 frq_wlo_map[] = { 4, 5, 6, 6, 8, 8 };
432 return frq_wlo_map[get_FRQ(tCK) - 3];
433}
434
435static u8 get_CKE(u32 tCK)
436{
437 /* Get CKE based on MCU frequency using the following rule:
438 * _______________________
439 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
440 * CKE : | 3 | 3 | 4 | 4 | 5 | 6 |
441 */
442 static const u8 frq_cke_map[] = { 3, 3, 4, 4, 5, 6 };
443 return frq_cke_map[get_FRQ(tCK) - 3];
444}
445
446static u8 get_XPDLL(u32 tCK)
447{
448 /* Get XPDLL based on MCU frequency using the following rule:
449 * _____________________________
450 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
451 * XPDLL : | 10 | 13 | 16 | 20 | 23 | 26 |
452 */
453 static const u8 frq_xpdll_map[] = { 10, 13, 16, 20, 23, 26 };
454 return frq_xpdll_map[get_FRQ(tCK) - 3];
455}
456
457static u8 get_XP(u32 tCK)
458{
459 /* Get XP based on MCU frequency using the following rule:
460 * _______________________
461 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
462 * XP : | 3 | 4 | 4 | 5 | 6 | 7 |
463 */
464 static const u8 frq_xp_map[] = { 3, 4, 4, 5, 6, 7 };
465 return frq_xp_map[get_FRQ(tCK) - 3];
466}
467
468static u8 get_AONPD(u32 tCK)
469{
470 /* Get AONPD based on MCU frequency using the following rule:
471 * ________________________
472 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
473 * AONPD : | 4 | 5 | 6 | 8 | 8 | 10 |
474 */
475 static const u8 frq_aonpd_map[] = { 4, 5, 6, 8, 8, 10 };
476 return frq_aonpd_map[get_FRQ(tCK) - 3];
477}
478
479static u32 get_COMP2(u32 tCK)
480{
481 /* Get COMP2 based on MCU frequency using the following rule:
482 * ___________________________________________________________
483 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
484 * COMP : | D6BEDCC | CE7C34C | CA57A4C | C6369CC | C42514C | C21410C |
485 */
486 static const u32 frq_comp2_map[] = { 0xD6BEDCC, 0xCE7C34C, 0xCA57A4C,
487 0xC6369CC, 0xC42514C, 0xC21410C
488 };
489 return frq_comp2_map[get_FRQ(tCK) - 3];
490}
491
492static void dram_timing(ramctr_timing * ctrl)
493{
494 u8 val;
495 u32 val32;
496
497 /* Maximum supported DDR3 frequency is 1066MHz (DDR3 2133) so make sure
498 * we cap it if we have faster DIMMs.
499 * Then, align it to the closest JEDEC standard frequency */
500 if (ctrl->tCK <= TCK_1066MHZ) {
501 ctrl->tCK = TCK_1066MHZ;
502 ctrl->edge_offset[0] = 16;
503 ctrl->edge_offset[1] = 7;
504 ctrl->edge_offset[2] = 7;
505 ctrl->timC_offset[0] = 18;
506 ctrl->timC_offset[1] = 7;
507 ctrl->timC_offset[2] = 7;
508 ctrl->reg_c14_offset = 16;
509 ctrl->reg_5064b0 = 0x218;
510 ctrl->reg_320c_range_threshold = 13;
511 } else if (ctrl->tCK <= TCK_933MHZ) {
512 ctrl->tCK = TCK_933MHZ;
513 ctrl->edge_offset[0] = 14;
514 ctrl->edge_offset[1] = 6;
515 ctrl->edge_offset[2] = 6;
516 ctrl->timC_offset[0] = 15;
517 ctrl->timC_offset[1] = 6;
518 ctrl->timC_offset[2] = 6;
519 ctrl->reg_c14_offset = 14;
520 ctrl->reg_5064b0 = 0x1d5;
521 ctrl->reg_320c_range_threshold = 15;
522 } else if (ctrl->tCK <= TCK_800MHZ) {
523 ctrl->tCK = TCK_800MHZ;
524 ctrl->edge_offset[0] = 13;
525 ctrl->edge_offset[1] = 5;
526 ctrl->edge_offset[2] = 5;
527 ctrl->timC_offset[0] = 14;
528 ctrl->timC_offset[1] = 5;
529 ctrl->timC_offset[2] = 5;
530 ctrl->reg_c14_offset = 12;
531 ctrl->reg_5064b0 = 0x193;
532 ctrl->reg_320c_range_threshold = 15;
533 } else if (ctrl->tCK <= TCK_666MHZ) {
534 ctrl->tCK = TCK_666MHZ;
535 ctrl->edge_offset[0] = 10;
536 ctrl->edge_offset[1] = 4;
537 ctrl->edge_offset[2] = 4;
538 ctrl->timC_offset[0] = 11;
539 ctrl->timC_offset[1] = 4;
540 ctrl->timC_offset[2] = 4;
541 ctrl->reg_c14_offset = 10;
542 ctrl->reg_5064b0 = 0x150;
543 ctrl->reg_320c_range_threshold = 16;
544 } else if (ctrl->tCK <= TCK_533MHZ) {
545 ctrl->tCK = TCK_533MHZ;
546 ctrl->edge_offset[0] = 8;
547 ctrl->edge_offset[1] = 3;
548 ctrl->edge_offset[2] = 3;
549 ctrl->timC_offset[0] = 9;
550 ctrl->timC_offset[1] = 3;
551 ctrl->timC_offset[2] = 3;
552 ctrl->reg_c14_offset = 8;
553 ctrl->reg_5064b0 = 0x10d;
554 ctrl->reg_320c_range_threshold = 17;
555 } else {
556 ctrl->tCK = TCK_400MHZ;
557 ctrl->edge_offset[0] = 6;
558 ctrl->edge_offset[1] = 2;
559 ctrl->edge_offset[2] = 2;
560 ctrl->timC_offset[0] = 6;
561 ctrl->timC_offset[1] = 2;
562 ctrl->timC_offset[2] = 2;
563 ctrl->reg_c14_offset = 8;
564 ctrl->reg_5064b0 = 0xcd;
565 ctrl->reg_320c_range_threshold = 17;
566 }
567
568 val32 = (1000 << 8) / ctrl->tCK;
569 printk(BIOS_DEBUG, "Selected DRAM frequency: %u MHz\n", val32);
570
571 /* Find CAS and CWL latencies */
572 val = (ctrl->tAA + ctrl->tCK - 1) / ctrl->tCK;
573 printk(BIOS_DEBUG, "Minimum CAS latency : %uT\n", val);
574 /* Find lowest supported CAS latency that satisfies the minimum value */
575 while (!((ctrl->cas_supported >> (val - 4)) & 1)
576 && (ctrl->cas_supported >> (val - 4))) {
577 val++;
578 }
579 /* Is CAS supported */
580 if (!(ctrl->cas_supported & (1 << (val - 4))))
581 printk(BIOS_DEBUG, "CAS not supported\n");
582 printk(BIOS_DEBUG, "Selected CAS latency : %uT\n", val);
583 ctrl->CAS = val;
584 ctrl->CWL = get_CWL(ctrl->CAS);
585 printk(BIOS_DEBUG, "Selected CWL latency : %uT\n", ctrl->CWL);
586
587 /* Find tRCD */
588 ctrl->tRCD = (ctrl->tRCD + ctrl->tCK - 1) / ctrl->tCK;
589 printk(BIOS_DEBUG, "Selected tRCD : %uT\n", ctrl->tRCD);
590
591 ctrl->tRP = (ctrl->tRP + ctrl->tCK - 1) / ctrl->tCK;
592 printk(BIOS_DEBUG, "Selected tRP : %uT\n", ctrl->tRP);
593
594 /* Find tRAS */
595 ctrl->tRAS = (ctrl->tRAS + ctrl->tCK - 1) / ctrl->tCK;
596 printk(BIOS_DEBUG, "Selected tRAS : %uT\n", ctrl->tRAS);
597
598 /* Find tWR */
599 ctrl->tWR = (ctrl->tWR + ctrl->tCK - 1) / ctrl->tCK;
600 printk(BIOS_DEBUG, "Selected tWR : %uT\n", ctrl->tWR);
601
602 /* Find tFAW */
603 ctrl->tFAW = (ctrl->tFAW + ctrl->tCK - 1) / ctrl->tCK;
604 printk(BIOS_DEBUG, "Selected tFAW : %uT\n", ctrl->tFAW);
605
606 /* Find tRRD */
607 ctrl->tRRD = (ctrl->tRRD + ctrl->tCK - 1) / ctrl->tCK;
608 printk(BIOS_DEBUG, "Selected tRRD : %uT\n", ctrl->tRRD);
609
610 /* Find tRTP */
611 ctrl->tRTP = (ctrl->tRTP + ctrl->tCK - 1) / ctrl->tCK;
612 printk(BIOS_DEBUG, "Selected tRTP : %uT\n", ctrl->tRTP);
613
614 /* Find tWTR */
615 ctrl->tWTR = (ctrl->tWTR + ctrl->tCK - 1) / ctrl->tCK;
616 printk(BIOS_DEBUG, "Selected tWTR : %uT\n", ctrl->tWTR);
617
618 /* Refresh-to-Active or Refresh-to-Refresh (tRFC) */
619 ctrl->tRFC = (ctrl->tRFC + ctrl->tCK - 1) / ctrl->tCK;
620 printk(BIOS_DEBUG, "Selected tRFC : %uT\n", ctrl->tRFC);
621
622 ctrl->tREFI = get_REFI(ctrl->tCK);
623 ctrl->tMOD = get_MOD(ctrl->tCK);
624 ctrl->tXSOffset = get_XSOffset(ctrl->tCK);
625 ctrl->tWLO = get_WLO(ctrl->tCK);
626 ctrl->tCKE = get_CKE(ctrl->tCK);
627 ctrl->tXPDLL = get_XPDLL(ctrl->tCK);
628 ctrl->tXP = get_XP(ctrl->tCK);
629 ctrl->tAONPD = get_AONPD(ctrl->tCK);
630}
631
632static void dram_freq(ramctr_timing * ctrl)
633{
634 if (ctrl->tCK > TCK_400MHZ) {
635 printk (BIOS_ERR, "DRAM frequency is under lowest supported frequency (400 MHz). Increasing to 400 MHz as last resort");
636 ctrl->tCK = TCK_400MHZ;
637 }
638 while (1) {
639 u8 val2;
640 u32 reg1 = 0;
641
642 /* Step 1 - Set target PCU frequency */
643
644 if (ctrl->tCK <= TCK_1066MHZ) {
645 ctrl->tCK = TCK_1066MHZ;
646 } else if (ctrl->tCK <= TCK_933MHZ) {
647 ctrl->tCK = TCK_933MHZ;
648 } else if (ctrl->tCK <= TCK_800MHZ) {
649 ctrl->tCK = TCK_800MHZ;
650 } else if (ctrl->tCK <= TCK_666MHZ) {
651 ctrl->tCK = TCK_666MHZ;
652 } else if (ctrl->tCK <= TCK_533MHZ) {
653 ctrl->tCK = TCK_533MHZ;
654 } else if (ctrl->tCK <= TCK_400MHZ) {
655 ctrl->tCK = TCK_400MHZ;
656 } else {
657 die ("No lock frequency found");
658 }
659
660 /* Frequency mulitplier. */
661 u32 FRQ = get_FRQ(ctrl->tCK);
662
663 /* Step 2 - Select frequency in the MCU */
664 reg1 = FRQ;
665 reg1 |= 0x80000000; // set running bit
666 MCHBAR32(0x5e00) = reg1;
667 while (reg1 & 0x80000000) {
668 printk(BIOS_DEBUG, " PLL busy...");
669 reg1 = MCHBAR32(0x5e00);
670 }
671 printk(BIOS_DEBUG, "done\n");
672
673 /* Step 3 - Verify lock frequency */
674 reg1 = MCHBAR32(0x5e04);
675 val2 = (u8) reg1;
676 if (val2 >= FRQ) {
677 printk(BIOS_DEBUG, "MCU frequency is set at : %d MHz\n",
678 (1000 << 8) / ctrl->tCK);
679 return;
680 }
681 printk(BIOS_DEBUG, "PLL didn't lock. Retrying at lower frequency\n");
682 ctrl->tCK++;
683 }
684}
685
686static void dram_xover(ramctr_timing * ctrl)
687{
688 u32 reg;
689 int channel;
690
691 FOR_ALL_CHANNELS {
692 // enable xover clk
693 printk(BIOS_DEBUG, "[%x] = %x\n", channel * 0x100 + 0xc14,
694 (ctrl->rankmap[channel] << 24));
695 MCHBAR32(channel * 0x100 + 0xc14) = (ctrl->rankmap[channel] << 24);
696
697 // enable xover ctl
698 reg = 0;
699 if (ctrl->rankmap[channel] & 0x5) {
700 reg |= 0x20000;
701 }
702 if (ctrl->rankmap[channel] & 0xa) {
703 reg |= 0x4000000;
704 }
705 // enable xover cmd
706 reg |= 0x4000;
707 printk(BIOS_DEBUG, "[%x] = %x\n", 0x100 * channel + 0x320c,
708 reg);
709 MCHBAR32(0x100 * channel + 0x320c) = reg;
710 }
711}
712
713static void dram_timing_regs(ramctr_timing * ctrl)
714{
715 u32 reg, addr, val32, cpu, stretch;
716 struct cpuid_result cpures;
717 int channel;
718
719 FOR_ALL_CHANNELS {
720 // DBP
721 reg = 0;
722 reg |= ctrl->tRCD;
723 reg |= (ctrl->tRP << 4);
724 reg |= (ctrl->CAS << 8);
725 reg |= (ctrl->CWL << 12);
726 reg |= (ctrl->tRAS << 16);
727 printk(BIOS_DEBUG, "[%x] = %x\n", 0x400 * channel + 0x4000,
728 reg);
729 MCHBAR32(0x400 * channel + 0x4000) = reg;
730
731 // RAP
732 reg = 0;
733 reg |= ctrl->tRRD;
734 reg |= (ctrl->tRTP << 4);
735 reg |= (ctrl->tCKE << 8);
736 reg |= (ctrl->tWTR << 12);
737 reg |= (ctrl->tFAW << 16);
738 reg |= (ctrl->tWR << 24);
739 reg |= (3 << 30);
740 printk(BIOS_DEBUG, "[%x] = %x\n", 0x400 * channel + 0x4004,
741 reg);
742 MCHBAR32(0x400 * channel + 0x4004) = reg;
743
744 // OTHP
745 addr = 0x400 * channel + 0x400c;
746 reg = 0;
747 reg |= ctrl->tXPDLL;
748 reg |= (ctrl->tXP << 5);
749 reg |= (ctrl->tAONPD << 8);
750 reg |= 0xa0000;
751 printk(BIOS_DEBUG, "[%x] = %x\n", addr, reg);
752 MCHBAR32(addr) = reg;
753
754 MCHBAR32(0x400 * channel + 0x4014) = 0;
755
756 MCHBAR32(addr) |= 0x00020000;
757
758 // ODT stretch
759 reg = 0;
760
761 cpures = cpuid(0);
762 cpu = cpures.eax;
763 if (IS_IVY_CPU(cpu)
764 || (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_D2(cpu))) {
765 stretch = 2;
766 addr = 0x400 * channel + 0x400c;
767 printk(BIOS_DEBUG, "[%x] = %x\n",
768 0x400 * channel + 0x400c, reg);
769 reg = MCHBAR32(addr);
770
771 if (((ctrl->rankmap[channel] & 3) == 0)
772 || (ctrl->rankmap[channel] & 0xc) == 0) {
773
774 // Rank 0 - operate on rank 2
775 reg = (reg & ~0xc0000) | (stretch << 18);
776
777 // Rank 2 - operate on rank 0
778 reg = (reg & ~0x30000) | (stretch << 16);
779
780 printk(BIOS_DEBUG, "[%x] = %x\n", addr, reg);
781 MCHBAR32(addr) = reg;
782 }
783
784 } else if (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_C(cpu)) {
785 stretch = 3;
786 addr = 0x400 * channel + 0x401c;
787 reg = MCHBAR32(addr);
788
789 if (((ctrl->rankmap[channel] & 3) == 0)
790 || (ctrl->rankmap[channel] & 0xc) == 0) {
791
792 // Rank 0 - operate on rank 2
793 reg = (reg & ~0x3000) | (stretch << 12);
794
795 // Rank 2 - operate on rank 0
796 reg = (reg & ~0xc00) | (stretch << 10);
797
798 printk(BIOS_DEBUG, "[%x] = %x\n", addr, reg);
799 MCHBAR32(addr) = reg;
800 }
801 } else {
802 stretch = 0;
803 }
804
805 // REFI
806 reg = 0;
807 val32 = ctrl->tREFI;
808 reg = (reg & ~0xffff) | val32;
809 val32 = ctrl->tRFC;
810 reg = (reg & ~0x1ff0000) | (val32 << 16);
811 val32 = (u32) (ctrl->tREFI * 9) / 1024;
812 reg = (reg & ~0xfe000000) | (val32 << 25);
813 printk(BIOS_DEBUG, "[%x] = %x\n", 0x400 * channel + 0x4298,
814 reg);
815 MCHBAR32(0x400 * channel + 0x4298) = reg;
816
817 MCHBAR32(0x400 * channel + 0x4294) |= 0xff;
818
819 // SRFTP
820 reg = 0;
821 val32 = tDLLK;
822 reg = (reg & ~0xfff) | val32;
823 val32 = ctrl->tXSOffset;
824 reg = (reg & ~0xf000) | (val32 << 12);
825 val32 = tDLLK - ctrl->tXSOffset;
826 reg = (reg & ~0x3ff0000) | (val32 << 16);
827 val32 = ctrl->tMOD - 8;
828 reg = (reg & ~0xf0000000) | (val32 << 28);
829 printk(BIOS_DEBUG, "[%x] = %x\n", 0x400 * channel + 0x42a4,
830 reg);
831 MCHBAR32(0x400 * channel + 0x42a4) = reg;
832 }
833}
834
835static void dram_dimm_mapping(dimm_info * info, ramctr_timing * ctrl)
836{
837 u32 reg, val32;
838 int channel;
839
840 FOR_ALL_CHANNELS {
841 dimm_attr *dimmA = 0;
842 dimm_attr *dimmB = 0;
843 reg = 0;
844 val32 = 0;
845 if (info->dimm[channel][0].size_mb >=
846 info->dimm[channel][1].size_mb) {
847 // dimm 0 is bigger, set it to dimmA
848 dimmA = &info->dimm[channel][0];
849 dimmB = &info->dimm[channel][1];
850 reg |= (0 << 16);
851 } else {
852 // dimm 1 is bigger, set it to dimmA
853 dimmA = &info->dimm[channel][1];
854 dimmB = &info->dimm[channel][0];
855 reg |= (1 << 16);
856 }
857 // dimmA
858 if (dimmA && (dimmA->ranks > 0)) {
859 val32 = dimmA->size_mb / 256;
860 reg = (reg & ~0xff) | val32;
861 val32 = dimmA->ranks - 1;
862 reg = (reg & ~0x20000) | (val32 << 17);
863 val32 = (dimmA->width / 8) - 1;
864 reg = (reg & ~0x80000) | (val32 << 19);
865 }
866 // dimmB
867 if (dimmB && (dimmB->ranks > 0)) {
868 val32 = dimmB->size_mb / 256;
869 reg = (reg & ~0xff00) | (val32 << 8);
870 val32 = dimmB->ranks - 1;
871 reg = (reg & ~0x40000) | (val32 << 18);
872 val32 = (dimmB->width / 8) - 1;
873 reg = (reg & ~0x100000) | (val32 << 20);
874 }
875 reg = (reg & ~0x200000) | (1 << 21); // rank interleave
876 reg = (reg & ~0x400000) | (1 << 22); // enhanced interleave
877
878 // Save MAD-DIMM register
879 if ((dimmA && (dimmA->ranks > 0))
880 || (dimmB && (dimmB->ranks > 0))) {
881 ctrl->mad_dimm[channel] = reg;
882 } else {
883 ctrl->mad_dimm[channel] = 0;
884 }
885 }
886}
887
888static void dram_dimm_set_mapping(ramctr_timing * ctrl)
889{
890 int channel;
891 FOR_ALL_CHANNELS {
892 MCHBAR32(0x5004 + channel * 4) = ctrl->mad_dimm[channel];
893 }
894}
895
896static void dram_zones(ramctr_timing * ctrl, int training)
897{
898 u32 reg, ch0size, ch1size;
899 u8 val;
900 reg = 0;
901 val = 0;
902 if (training) {
903 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
904 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
905 } else {
906 ch0size = ctrl->channel_size_mb[0];
907 ch1size = ctrl->channel_size_mb[1];
908 }
909
910 if (ch0size >= ch1size) {
911 reg = MCHBAR32(0x5014);
912 val = ch1size / 256;
913 reg = (reg & ~0xff000000) | val << 24;
914 reg = (reg & ~0xff0000) | (2 * val) << 16;
915 MCHBAR32(0x5014) = reg;
916 MCHBAR32(0x5000) = 0x24;
917 } else {
918 reg = MCHBAR32(0x5014);
919 val = ch0size / 256;
920 reg = (reg & ~0xff000000) | val << 24;
921 reg = (reg & ~0xff0000) | (2 * val) << 16;
922 MCHBAR32(0x5014) = reg;
923 MCHBAR32(0x5000) = 0x21;
924 }
925}
926
927static void dram_memorymap(ramctr_timing * ctrl, int me_uma_size)
928{
929 u32 reg, val, reclaim;
930 u32 tom, gfxstolen, gttsize;
931 size_t tsegsize, mmiosize, toludbase, touudbase, gfxstolenbase, gttbase,
932 tsegbase, mestolenbase;
933 size_t tsegbasedelta, remapbase, remaplimit;
934 uint16_t ggc;
935
936 mmiosize = 0x400;
937
938 ggc = pci_read_config16(NORTHBRIDGE, GGC);
939 if (!(ggc & 2)) {
940 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
941 gttsize = ((ggc >> 8) & 0x3);
942 } else {
943 gfxstolen = 0;
944 gttsize = 0;
945 }
946
947 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
948
949 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
950
951 mestolenbase = tom - me_uma_size;
952
953 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize,
954 tom - me_uma_size);
955 gfxstolenbase = toludbase - gfxstolen;
956 gttbase = gfxstolenbase - gttsize;
957
958 tsegbase = gttbase - tsegsize;
959
960 // Round tsegbase down to nearest address aligned to tsegsize
961 tsegbasedelta = tsegbase & (tsegsize - 1);
962 tsegbase &= ~(tsegsize - 1);
963
964 gttbase -= tsegbasedelta;
965 gfxstolenbase -= tsegbasedelta;
966 toludbase -= tsegbasedelta;
967
968 // Test if it is possible to reclaim a hole in the ram addressing
969 if (tom - me_uma_size > toludbase) {
970 // Reclaim is possible
971 reclaim = 1;
972 remapbase = MAX(4096, tom - me_uma_size);
973 remaplimit =
974 remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
975 touudbase = remaplimit + 1;
976 } else {
977 // Reclaim not possible
978 reclaim = 0;
979 touudbase = tom - me_uma_size;
980 }
981
982 // Update memory map in pci-e configuration space
983
984 // TOM (top of memory)
985 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa0);
986 val = tom & 0xfff;
987 reg = (reg & ~0xfff00000) | (val << 20);
988 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xa0, reg);
989 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa0, reg);
990
991 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa4);
992 val = tom & 0xfffff000;
993 reg = (reg & ~0x000fffff) | (val >> 12);
994 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xa4, reg);
995 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa4, reg);
996
997 // TOLUD (top of low used dram)
998 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xbc);
999 val = toludbase & 0xfff;
1000 reg = (reg & ~0xfff00000) | (val << 20);
1001 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xbc, reg);
1002 pcie_write_config32(PCI_DEV(0, 0, 0), 0xbc, reg);
1003
1004 // TOUUD LSB (top of upper usable dram)
1005 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa8);
1006 val = touudbase & 0xfff;
1007 reg = (reg & ~0xfff00000) | (val << 20);
1008 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xa8, reg);
1009 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa8, reg);
1010
1011 // TOUUD MSB
1012 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xac);
1013 val = touudbase & 0xfffff000;
1014 reg = (reg & ~0x000fffff) | (val >> 12);
1015 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xac, reg);
1016 pcie_write_config32(PCI_DEV(0, 0, 0), 0xac, reg);
1017
1018 if (reclaim) {
1019 // REMAP BASE
1020 pcie_write_config32(PCI_DEV(0, 0, 0), 0x90, remapbase << 20);
1021 pcie_write_config32(PCI_DEV(0, 0, 0), 0x94, remapbase >> 12);
1022
1023 // REMAP LIMIT
1024 pcie_write_config32(PCI_DEV(0, 0, 0), 0x98, remaplimit << 20);
1025 pcie_write_config32(PCI_DEV(0, 0, 0), 0x9c, remaplimit >> 12);
1026 }
1027 // TSEG
1028 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb8);
1029 val = tsegbase & 0xfff;
1030 reg = (reg & ~0xfff00000) | (val << 20);
1031 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xb8, reg);
1032 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb8, reg);
1033
1034 // GFX stolen memory
1035 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb0);
1036 val = gfxstolenbase & 0xfff;
1037 reg = (reg & ~0xfff00000) | (val << 20);
1038 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xb0, reg);
1039 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb0, reg);
1040
1041 // GTT stolen memory
1042 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb4);
1043 val = gttbase & 0xfff;
1044 reg = (reg & ~0xfff00000) | (val << 20);
1045 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0xb4, reg);
1046 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb4, reg);
1047
1048 if (me_uma_size) {
1049 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x7c);
1050 val = (0x80000 - me_uma_size) & 0xfffff000;
1051 reg = (reg & ~0x000fffff) | (val >> 12);
1052 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0x7c, reg);
1053 pcie_write_config32(PCI_DEV(0, 0, 0), 0x7c, reg);
1054
1055 // ME base
1056 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x70);
1057 val = mestolenbase & 0xfff;
1058 reg = (reg & ~0xfff00000) | (val << 20);
1059 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0x70, reg);
1060 pcie_write_config32(PCI_DEV(0, 0, 0), 0x70, reg);
1061
1062 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x74);
1063 val = mestolenbase & 0xfffff000;
1064 reg = (reg & ~0x000fffff) | (val >> 12);
1065 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0x74, reg);
1066 pcie_write_config32(PCI_DEV(0, 0, 0), 0x74, reg);
1067
1068 // ME mask
1069 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x78);
1070 val = (0x80000 - me_uma_size) & 0xfff;
1071 reg = (reg & ~0xfff00000) | (val << 20);
1072 reg = (reg & ~0x400) | (1 << 10); // set lockbit on ME mem
1073
1074 reg = (reg & ~0x800) | (1 << 11); // set ME memory enable
1075 printk(BIOS_DEBUG, "PCI:[%x] = %x\n", 0x78, reg);
1076 pcie_write_config32(PCI_DEV(0, 0, 0), 0x78, reg);
1077 }
1078}
1079
1080static void dram_ioregs(ramctr_timing * ctrl)
1081{
1082 u32 reg, comp2;
1083
1084 int channel;
1085
1086 // IO clock
1087 FOR_ALL_CHANNELS {
1088 MCHBAR32(0xc00 + 0x100 * channel) = ctrl->rankmap[channel];
1089 }
1090
1091 // IO command
1092 FOR_ALL_CHANNELS {
1093 MCHBAR32(0x3200 + 0x100 * channel) = ctrl->rankmap[channel];
1094 }
1095
1096 // IO control
1097 FOR_ALL_POPULATED_CHANNELS {
1098 program_timings(ctrl, channel);
1099 }
1100
1101 // Rcomp
1102 printk(BIOS_DEBUG, "RCOMP...");
1103 reg = 0;
1104 while (reg == 0) {
1105 reg = MCHBAR32(0x5084) & 0x10000;
1106 }
1107 printk(BIOS_DEBUG, "done\n");
1108
1109 // Set comp2
1110 comp2 = get_COMP2(ctrl->tCK);
1111 MCHBAR32(0x3714) = comp2;
1112 printk(BIOS_DEBUG, "COMP2 done\n");
1113
1114 // Set comp1
1115 FOR_ALL_POPULATED_CHANNELS {
1116 reg = MCHBAR32(0x1810 + channel * 0x100); //ch0
1117 reg = (reg & ~0xe00) | (1 << 9); //odt
1118 reg = (reg & ~0xe00000) | (1 << 21); //clk drive up
1119 reg = (reg & ~0x38000000) | (1 << 27); //ctl drive up
1120 MCHBAR32(0x1810 + channel * 0x100) = reg;
1121 }
1122 printk(BIOS_DEBUG, "COMP1 done\n");
1123
1124 printk(BIOS_DEBUG, "FORCE RCOMP and wait 20us...");
1125 MCHBAR32(0x5f08) |= 0x100;
1126 udelay(20);
1127 printk(BIOS_DEBUG, "done\n");
1128}
1129
1130static void wait_428c(int channel)
1131{
1132 while (1) {
1133 if (read32(DEFAULT_MCHBAR + 0x428c + (channel << 10)) & 0x50)
1134 return;
1135 }
1136}
1137
1138static void write_reset(ramctr_timing * ctrl)
1139{
1140 int channel, slotrank;
1141
1142 /* choose a populated channel. */
1143 channel = (ctrl->rankmap[0]) ? 0 : 1;
1144
1145 wait_428c(channel);
1146
1147 /* choose a populated rank. */
1148 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
1149
Patrick Rudolph371d2912015-10-09 13:33:25 +02001150 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001151 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1152 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x80c01);
1153
1154 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1155 (slotrank << 24) | 0x60000);
1156
1157 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1158
1159 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x400001);
1160 wait_428c(channel);
1161}
1162
1163static void dram_jedecreset(ramctr_timing * ctrl)
1164{
1165 u32 reg, addr;
1166 int channel;
1167
1168 while (!(MCHBAR32(0x5084) & 0x10000)) ;
1169 do {
1170 reg = MCHBAR32(0x428c);
1171 } while ((reg & 0x14) == 0);
1172
1173 // Set state of memory controller
1174 reg = 0x112;
1175 MCHBAR32(0x5030) = reg;
1176 MCHBAR32(0x4ea0) = 0;
1177 reg |= 2; //ddr reset
1178 MCHBAR32(0x5030) = reg;
1179
1180 // Assert dimm reset signal
1181 reg = MCHBAR32(0x5030);
1182 reg &= ~0x2;
1183 MCHBAR32(0x5030) = reg;
1184
1185 // Wait 200us
1186 udelay(200);
1187
1188 // Deassert dimm reset signal
1189 MCHBAR32(0x5030) |= 2;
1190
1191 // Wait 500us
1192 udelay(500);
1193
1194 // Enable DCLK
1195 MCHBAR32(0x5030) |= 4;
1196
1197 // XXX Wait 20ns
1198 udelay(1);
1199
1200 FOR_ALL_CHANNELS {
1201 // Set valid rank CKE
1202 reg = 0;
1203 reg = (reg & ~0xf) | ctrl->rankmap[channel];
1204 addr = 0x400 * channel + 0x42a0;
1205 MCHBAR32(addr) = reg;
1206
1207 // Wait 10ns for ranks to settle
1208 //udelay(0.01);
1209
1210 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
1211 MCHBAR32(addr) = reg;
1212
1213 // Write reset using a NOP
1214 write_reset(ctrl);
1215 }
1216}
1217
1218static odtmap get_ODT(ramctr_timing * ctrl, u8 rank)
1219{
1220 /* Get ODT based on rankmap: */
1221 int dimms_per_ch = 0;
1222 int channel;
1223
1224 FOR_ALL_CHANNELS {
1225 dimms_per_ch = max ((ctrl->rankmap[channel] & 1)
1226 + ((ctrl->rankmap[channel] >> 2) & 1),
1227 dimms_per_ch);
1228 }
1229
1230 if (dimms_per_ch == 1) {
1231 return (const odtmap){60, 60};
1232 } else if (dimms_per_ch == 2) {
1233 return (const odtmap){120, 30};
1234 } else {
1235 printk(BIOS_DEBUG,
1236 "Huh, no dimms? m0 = %d m1 = %d dpc = %d\n",
1237 ctrl->rankmap[0],
1238 ctrl->rankmap[1], dimms_per_ch);
1239 die("");
1240 }
1241}
1242
1243static void write_mrreg(ramctr_timing * ctrl, int channel, int slotrank,
1244 int reg, u32 val)
1245{
1246 wait_428c(channel);
1247
1248 printram("MRd: %x <= %x\n", reg, val);
1249
1250 if (ctrl->rank_mirror[channel][slotrank]) {
1251 /* DDR3 Rank1 Address mirror
1252 * swap the following pins:
1253 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
1254 reg = ((reg >> 1) & 1) | ((reg << 1) & 2);
1255 val = (val & ~0x1f8) | ((val >> 1) & 0xa8)
1256 | ((val & 0xa8) << 1);
1257 }
1258
1259 printram("MRd: %x <= %x\n", reg, val);
1260
Patrick Rudolph371d2912015-10-09 13:33:25 +02001261 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001262 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f000);
1263 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
1264 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1265 (slotrank << 24) | (reg << 20) | val | 0x60000);
1266 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1267
Patrick Rudolph371d2912015-10-09 13:33:25 +02001268 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001269 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f000);
1270 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x41001);
1271 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1272 (slotrank << 24) | (reg << 20) | val | 0x60000);
1273 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1274
Patrick Rudolph371d2912015-10-09 13:33:25 +02001275 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001276 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x0f000);
1277 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1278 0x1001 | (ctrl->tMOD << 16));
1279 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1280 (slotrank << 24) | (reg << 20) | val | 0x60000);
1281 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1282 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
1283}
1284
1285static u32 make_mr0(ramctr_timing * ctrl, u8 rank)
1286{
1287 u16 mr0reg, mch_cas, mch_wr;
1288 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 +02001289
1290 /* DLL Reset - self clearing - set after CLK frequency has been changed */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001291 mr0reg = 0x100;
1292
1293 // Convert CAS to MCH register friendly
1294 if (ctrl->CAS < 12) {
1295 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
1296 } else {
1297 mch_cas = (u16) (ctrl->CAS - 12);
1298 mch_cas = ((mch_cas << 1) | 0x1);
1299 }
1300
1301 // Convert tWR to MCH register friendly
1302 mch_wr = mch_wr_t[ctrl->tWR - 5];
1303
1304 mr0reg = (mr0reg & ~0x4) | (mch_cas & 0x1);
1305 mr0reg = (mr0reg & ~0x70) | ((mch_cas & 0xe) << 3);
1306 mr0reg = (mr0reg & ~0xe00) | (mch_wr << 9);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001307
1308 // Precharge PD - Fast (desktop) 0x1 or slow (mobile) 0x0 - mostly power-saving feature
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001309 mr0reg = (mr0reg & ~0x1000) | (!ctrl->mobile << 12);
1310 return mr0reg;
1311}
1312
1313static void dram_mr0(ramctr_timing * ctrl, u8 rank)
1314{
1315 int channel;
1316
1317 FOR_ALL_POPULATED_CHANNELS write_mrreg(ctrl, channel, rank, 0,
1318 make_mr0(ctrl, rank));
1319}
1320
1321static u32 encode_odt(u32 odt)
1322{
1323 switch (odt) {
1324 case 30:
1325 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
1326 case 60:
1327 return (1 << 2); // RZQ/4
1328 case 120:
1329 return (1 << 6); // RZQ/2
1330 default:
1331 case 0:
1332 return 0;
1333 }
1334}
1335
1336static u32 make_mr1(ramctr_timing * ctrl, u8 rank)
1337{
1338 odtmap odt;
1339 u32 mr1reg;
1340
1341 odt = get_ODT(ctrl, rank);
1342 mr1reg = 0x2;
1343
1344 mr1reg |= encode_odt(odt.rttnom);
1345
1346 return mr1reg;
1347}
1348
1349static void dram_mr1(ramctr_timing * ctrl, u8 rank)
1350{
1351 u16 mr1reg;
1352 int channel;
1353
1354 mr1reg = make_mr1(ctrl, rank);
1355
1356 FOR_ALL_CHANNELS {
1357 write_mrreg(ctrl, channel, rank, 1, mr1reg);
1358 }
1359}
1360
1361static void dram_mr2(ramctr_timing * ctrl, u8 rank)
1362{
1363 u16 pasr, cwl, mr2reg;
1364 odtmap odt;
1365 int channel;
1366 int srt;
1367
1368 pasr = 0;
1369 cwl = ctrl->CWL - 5;
1370 odt = get_ODT(ctrl, rank);
1371
1372 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
1373
1374 mr2reg = 0;
1375 mr2reg = (mr2reg & ~0x7) | pasr;
1376 mr2reg = (mr2reg & ~0x38) | (cwl << 3);
1377 mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6);
1378 mr2reg = (mr2reg & ~0x80) | (srt << 7);
1379 mr2reg |= (odt.rttwr / 60) << 9;
1380
1381 FOR_ALL_CHANNELS {
1382 write_mrreg(ctrl, channel, rank, 2, mr2reg);
1383 }
1384}
1385
1386static void dram_mr3(ramctr_timing * ctrl, u8 rank)
1387{
1388 int channel;
1389
1390 FOR_ALL_CHANNELS {
1391 write_mrreg(ctrl, channel, rank, 3, 0);
1392 }
1393}
1394
1395static void dram_mrscommands(ramctr_timing * ctrl)
1396{
1397 u8 rank;
1398 u32 reg, addr;
1399 int channel;
1400
1401 for (rank = 0; rank < 4; rank++) {
1402 // MR2
1403 printram("MR2 rank %d...", rank);
1404 dram_mr2(ctrl, rank);
1405 printram("done\n");
1406
1407 // MR3
1408 printram("MR3 rank %d...", rank);
1409 dram_mr3(ctrl, rank);
1410 printram("done\n");
1411
1412 // MR1
1413 printram("MR1 rank %d...", rank);
1414 dram_mr1(ctrl, rank);
1415 printram("done\n");
1416
1417 // MR0
1418 printram("MR0 rank %d...", rank);
1419 dram_mr0(ctrl, rank);
1420 printram("done\n");
1421 }
1422
Patrick Rudolph371d2912015-10-09 13:33:25 +02001423 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001424 write32(DEFAULT_MCHBAR + 0x4e20, 0x7);
1425 write32(DEFAULT_MCHBAR + 0x4e30, 0xf1001);
1426 write32(DEFAULT_MCHBAR + 0x4e00, 0x60002);
1427 write32(DEFAULT_MCHBAR + 0x4e10, 0);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001428
1429 /* DRAM command ZQCL */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001430 write32(DEFAULT_MCHBAR + 0x4e24, 0x1f003);
1431 write32(DEFAULT_MCHBAR + 0x4e34, 0x1901001);
1432 write32(DEFAULT_MCHBAR + 0x4e04, 0x60400);
1433 write32(DEFAULT_MCHBAR + 0x4e14, 0x288);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001434
1435 /* execute command queue on all channels ? */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001436 write32(DEFAULT_MCHBAR + 0x4e84, 0x40004);
1437
1438 // Drain
1439 FOR_ALL_CHANNELS {
1440 // Wait for ref drained
1441 wait_428c(channel);
1442 }
1443
1444 // Refresh enable
1445 MCHBAR32(0x5030) |= 8;
1446
1447 FOR_ALL_POPULATED_CHANNELS {
1448 addr = 0x400 * channel + 0x4020;
1449 reg = MCHBAR32(addr);
1450 reg &= ~0x200000;
1451 MCHBAR32(addr) = reg;
1452
1453 wait_428c(channel);
1454
1455 rank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
1456
1457 // Drain
1458 wait_428c(channel);
1459
Patrick Rudolph371d2912015-10-09 13:33:25 +02001460 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001461 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1462 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
1463 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1464 (rank << 24) | 0x60000);
1465 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
1466 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x1);
1467
1468 // Drain
1469 wait_428c(channel);
1470 }
1471}
1472
1473const u32 lane_registers[] = {
1474 0x0000, 0x0200, 0x0400, 0x0600,
1475 0x1000, 0x1200, 0x1400, 0x1600,
1476 0x0800
1477};
1478
1479static void program_timings(ramctr_timing * ctrl, int channel)
1480{
1481 u32 reg32, reg_4024, reg_c14, reg_c18, reg_4028;
1482 int lane;
1483 int slotrank, slot;
1484 int full_shift = 0;
1485 u16 slot320c[NUM_SLOTS];
1486
1487 FOR_ALL_POPULATED_RANKS {
1488 if (full_shift < -ctrl->timings[channel][slotrank].val_320c)
1489 full_shift = -ctrl->timings[channel][slotrank].val_320c;
1490 }
1491
1492 for (slot = 0; slot < NUM_SLOTS; slot++)
1493 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
1494 case 0:
1495 default:
1496 slot320c[slot] = 0x7f;
1497 break;
1498 case 1:
1499 slot320c[slot] =
1500 ctrl->timings[channel][2 * slot + 0].val_320c +
1501 full_shift;
1502 break;
1503 case 2:
1504 slot320c[slot] =
1505 ctrl->timings[channel][2 * slot + 1].val_320c +
1506 full_shift;
1507 break;
1508 case 3:
1509 slot320c[slot] =
1510 (ctrl->timings[channel][2 * slot].val_320c +
1511 ctrl->timings[channel][2 * slot +
1512 1].val_320c) / 2 +
1513 full_shift;
1514 break;
1515 }
1516
1517 reg32 = (1 << 17) | (1 << 14);
1518 reg32 |= ((slot320c[0] & 0x3f) << 6) | ((slot320c[0] & 0x40) << 9);
1519 reg32 |= (slot320c[1] & 0x7f) << 18;
1520 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
1521
1522 MCHBAR32(0x320c + 0x100 * channel) = reg32;
1523
1524 reg_c14 = ctrl->rankmap[channel] << 24;
1525 reg_c18 = 0;
1526
1527 FOR_ALL_POPULATED_RANKS {
1528 int shift =
1529 ctrl->timings[channel][slotrank].val_320c + full_shift;
1530 int offset_val_c14;
1531 if (shift < 0)
1532 shift = 0;
1533 offset_val_c14 = ctrl->reg_c14_offset + shift;
1534 reg_c14 |= (offset_val_c14 & 0x3f) << (6 * slotrank);
1535 reg_c18 |= ((offset_val_c14 >> 6) & 1) << slotrank;
1536 }
1537
1538 MCHBAR32(0xc14 + channel * 0x100) = reg_c14;
1539 MCHBAR32(0xc18 + channel * 0x100) = reg_c18;
1540
1541 reg_4028 = MCHBAR32(0x4028 + 0x400 * channel);
1542 reg_4028 &= 0xffff0000;
1543
1544 reg_4024 = 0;
1545
1546 FOR_ALL_POPULATED_RANKS {
1547 int post_timA_min_high = 7, post_timA_max_high = 0;
1548 int pre_timA_min_high = 7, pre_timA_max_high = 0;
1549 int shift_402x = 0;
1550 int shift =
1551 ctrl->timings[channel][slotrank].val_320c + full_shift;
1552
1553 if (shift < 0)
1554 shift = 0;
1555
1556 FOR_ALL_LANES {
1557 if (post_timA_min_high >
1558 ((ctrl->timings[channel][slotrank].lanes[lane].
1559 timA + shift) >> 6))
1560 post_timA_min_high =
1561 ((ctrl->timings[channel][slotrank].
1562 lanes[lane].timA + shift) >> 6);
1563 if (pre_timA_min_high >
1564 (ctrl->timings[channel][slotrank].lanes[lane].
1565 timA >> 6))
1566 pre_timA_min_high =
1567 (ctrl->timings[channel][slotrank].
1568 lanes[lane].timA >> 6);
1569 if (post_timA_max_high <
1570 ((ctrl->timings[channel][slotrank].lanes[lane].
1571 timA + shift) >> 6))
1572 post_timA_max_high =
1573 ((ctrl->timings[channel][slotrank].
1574 lanes[lane].timA + shift) >> 6);
1575 if (pre_timA_max_high <
1576 (ctrl->timings[channel][slotrank].lanes[lane].
1577 timA >> 6))
1578 pre_timA_max_high =
1579 (ctrl->timings[channel][slotrank].
1580 lanes[lane].timA >> 6);
1581 }
1582
1583 if (pre_timA_max_high - pre_timA_min_high <
1584 post_timA_max_high - post_timA_min_high)
1585 shift_402x = +1;
1586 else if (pre_timA_max_high - pre_timA_min_high >
1587 post_timA_max_high - post_timA_min_high)
1588 shift_402x = -1;
1589
1590 reg_4028 |=
1591 (ctrl->timings[channel][slotrank].val_4028 + shift_402x -
1592 post_timA_min_high) << (4 * slotrank);
1593 reg_4024 |=
1594 (ctrl->timings[channel][slotrank].val_4024 +
1595 shift_402x) << (8 * slotrank);
1596
1597 FOR_ALL_LANES {
1598 MCHBAR32(lane_registers[lane] + 0x10 + 0x100 * channel +
1599 4 * slotrank)
1600 =
1601 (((ctrl->timings[channel][slotrank].lanes[lane].
1602 timA + shift) & 0x3f)
1603 |
1604 ((ctrl->timings[channel][slotrank].lanes[lane].
1605 rising + shift) << 8)
1606 |
1607 (((ctrl->timings[channel][slotrank].lanes[lane].
1608 timA + shift -
1609 (post_timA_min_high << 6)) & 0x1c0) << 10)
1610 | (ctrl->timings[channel][slotrank].lanes[lane].
1611 falling << 20));
1612
1613 MCHBAR32(lane_registers[lane] + 0x20 + 0x100 * channel +
1614 4 * slotrank)
1615 =
1616 (((ctrl->timings[channel][slotrank].lanes[lane].
1617 timC + shift) & 0x3f)
1618 |
1619 (((ctrl->timings[channel][slotrank].lanes[lane].
1620 timB + shift) & 0x3f) << 8)
1621 |
1622 (((ctrl->timings[channel][slotrank].lanes[lane].
1623 timB + shift) & 0x1c0) << 9)
1624 |
1625 (((ctrl->timings[channel][slotrank].lanes[lane].
1626 timC + shift) & 0x40) << 13));
1627 }
1628 }
1629 MCHBAR32(0x4024 + 0x400 * channel) = reg_4024;
1630 MCHBAR32(0x4028 + 0x400 * channel) = reg_4028;
1631}
1632
1633static void test_timA(ramctr_timing * ctrl, int channel, int slotrank)
1634{
1635 wait_428c(channel);
1636
Patrick Rudolph371d2912015-10-09 13:33:25 +02001637 /* DRAM command MRS
1638 * write MR3 MPR enable
1639 * in this mode only RD and RDA are allowed
1640 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001641 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
1642 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1643 (0xc01 | (ctrl->tMOD << 16)));
1644 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1645 (slotrank << 24) | 0x360004);
1646 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1647
Patrick Rudolph371d2912015-10-09 13:33:25 +02001648 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001649 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
1650 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x4040c01);
1651 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, (slotrank << 24));
1652 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1653
Patrick Rudolph371d2912015-10-09 13:33:25 +02001654 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001655 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
1656 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1657 0x100f | ((ctrl->CAS + 36) << 16));
1658 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1659 (slotrank << 24) | 0x60000);
1660 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1661
Patrick Rudolph371d2912015-10-09 13:33:25 +02001662 /* DRAM command MRS
1663 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001664 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
1665 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1666 (0xc01 | (ctrl->tMOD << 16)));
1667 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1668 (slotrank << 24) | 0x360000);
1669 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1670
1671 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1672
1673 wait_428c(channel);
1674}
1675
1676static int does_lane_work(ramctr_timing * ctrl, int channel, int slotrank,
1677 int lane)
1678{
1679 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
1680 return ((read32
1681 (DEFAULT_MCHBAR + lane_registers[lane] + channel * 0x100 + 4 +
1682 ((timA / 32) & 1) * 4)
1683 >> (timA % 32)) & 1);
1684}
1685
1686struct run {
1687 int middle;
1688 int end;
1689 int start;
1690 int all;
1691 int length;
1692};
1693
1694static struct run get_longest_zero_run(int *seq, int sz)
1695{
1696 int i, ls;
1697 int bl = 0, bs = 0;
1698 struct run ret;
1699
1700 ls = 0;
1701 for (i = 0; i < 2 * sz; i++)
1702 if (seq[i % sz]) {
1703 if (i - ls > bl) {
1704 bl = i - ls;
1705 bs = ls;
1706 }
1707 ls = i + 1;
1708 }
1709 if (bl == 0) {
1710 ret.middle = sz / 2;
1711 ret.start = 0;
1712 ret.end = sz;
1713 ret.all = 1;
1714 return ret;
1715 }
1716
1717 ret.start = bs % sz;
1718 ret.end = (bs + bl - 1) % sz;
1719 ret.middle = (bs + (bl - 1) / 2) % sz;
1720 ret.length = bl;
1721 ret.all = 0;
1722
1723 return ret;
1724}
1725
1726static void discover_timA_coarse(ramctr_timing * ctrl, int channel,
1727 int slotrank, int *upperA)
1728{
1729 int timA;
1730 int statistics[NUM_LANES][128];
1731 int lane;
1732
1733 for (timA = 0; timA < 128; timA++) {
1734 FOR_ALL_LANES {
1735 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1736 }
1737 program_timings(ctrl, channel);
1738
1739 test_timA(ctrl, channel, slotrank);
1740
1741 FOR_ALL_LANES {
1742 statistics[lane][timA] =
1743 !does_lane_work(ctrl, channel, slotrank, lane);
1744 printram("Astat: %d, %d, %d, %x, %x\n",
1745 channel, slotrank, lane, timA,
1746 statistics[lane][timA]);
1747 }
1748 }
1749 FOR_ALL_LANES {
1750 struct run rn = get_longest_zero_run(statistics[lane], 128);
1751 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1752 upperA[lane] = rn.end;
1753 if (upperA[lane] < rn.middle)
1754 upperA[lane] += 128;
1755 printram("Aval: %d, %d, %d, %x\n", channel, slotrank,
1756 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
1757 printram("Aend: %d, %d, %d, %x\n", channel, slotrank,
1758 lane, upperA[lane]);
1759 }
1760}
1761
1762static void discover_timA_fine(ramctr_timing * ctrl, int channel, int slotrank,
1763 int *upperA)
1764{
1765 int timA_delta;
1766 int statistics[NUM_LANES][51];
1767 int lane, i;
1768
1769 memset(statistics, 0, sizeof(statistics));
1770
1771 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
1772 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
1773 timA = upperA[lane] + timA_delta + 0x40;
1774 program_timings(ctrl, channel);
1775
1776 for (i = 0; i < 100; i++) {
1777 test_timA(ctrl, channel, slotrank);
1778 FOR_ALL_LANES {
1779 statistics[lane][timA_delta + 25] +=
1780 does_lane_work(ctrl, channel, slotrank,
1781 lane);
1782 }
1783 }
1784 }
1785 FOR_ALL_LANES {
1786 int last_zero, first_all;
1787
1788 for (last_zero = -25; last_zero <= 25; last_zero++)
1789 if (statistics[lane][last_zero + 25])
1790 break;
1791 last_zero--;
1792 for (first_all = -25; first_all <= 25; first_all++)
1793 if (statistics[lane][first_all + 25] == 100)
1794 break;
1795
1796 printram("lane %d: %d, %d\n", lane, last_zero,
1797 first_all);
1798
1799 ctrl->timings[channel][slotrank].lanes[lane].timA =
1800 (last_zero + first_all) / 2 + upperA[lane];
1801 printram("Aval: %d, %d, %d, %x\n", channel, slotrank,
1802 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
1803 }
1804}
1805
1806static void discover_402x(ramctr_timing * ctrl, int channel, int slotrank,
1807 int *upperA)
1808{
1809 int works[NUM_LANES];
1810 int lane;
1811 while (1) {
1812 int all_works = 1, some_works = 0;
1813 program_timings(ctrl, channel);
1814 test_timA(ctrl, channel, slotrank);
1815 FOR_ALL_LANES {
1816 works[lane] =
1817 !does_lane_work(ctrl, channel, slotrank, lane);
1818 if (works[lane])
1819 some_works = 1;
1820 else
1821 all_works = 0;
1822 }
1823 if (all_works)
1824 return;
1825 if (!some_works) {
1826 if (ctrl->timings[channel][slotrank].val_4024 < 2)
1827 die("402x discovery failed");
1828 ctrl->timings[channel][slotrank].val_4024 -= 2;
1829 printram("4024 -= 2;\n");
1830 continue;
1831 }
1832 ctrl->timings[channel][slotrank].val_4028 += 2;
1833 printram("4028 += 2;\n");
1834 if (ctrl->timings[channel][slotrank].val_4028 >= 0x10)
1835 die("402x discovery failed");
1836 FOR_ALL_LANES if (works[lane]) {
1837 ctrl->timings[channel][slotrank].lanes[lane].timA +=
1838 128;
1839 upperA[lane] += 128;
1840 printram("increment %d, %d, %d\n", channel,
1841 slotrank, lane);
1842 }
1843 }
1844}
1845
1846struct timA_minmax {
1847 int timA_min_high, timA_max_high;
1848};
1849
1850static void pre_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
1851 struct timA_minmax *mnmx)
1852{
1853 int lane;
1854 mnmx->timA_min_high = 7;
1855 mnmx->timA_max_high = 0;
1856
1857 FOR_ALL_LANES {
1858 if (mnmx->timA_min_high >
1859 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1860 mnmx->timA_min_high =
1861 (ctrl->timings[channel][slotrank].lanes[lane].
1862 timA >> 6);
1863 if (mnmx->timA_max_high <
1864 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1865 mnmx->timA_max_high =
1866 (ctrl->timings[channel][slotrank].lanes[lane].
1867 timA >> 6);
1868 }
1869}
1870
1871static void post_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
1872 struct timA_minmax *mnmx)
1873{
1874 struct timA_minmax post;
1875 int shift_402x = 0;
1876
1877 /* Get changed maxima. */
1878 pre_timA_change(ctrl, channel, slotrank, &post);
1879
1880 if (mnmx->timA_max_high - mnmx->timA_min_high <
1881 post.timA_max_high - post.timA_min_high)
1882 shift_402x = +1;
1883 else if (mnmx->timA_max_high - mnmx->timA_min_high >
1884 post.timA_max_high - post.timA_min_high)
1885 shift_402x = -1;
1886 else
1887 shift_402x = 0;
1888
1889 ctrl->timings[channel][slotrank].val_4028 += shift_402x;
1890 ctrl->timings[channel][slotrank].val_4024 += shift_402x;
1891 printram("4024 += %d;\n", shift_402x);
1892 printram("4028 += %d;\n", shift_402x);
1893}
1894
Patrick Rudolph371d2912015-10-09 13:33:25 +02001895/* Compensate the skew between DQS and DQs.
1896 * To ease PCB design a small skew between Data Strobe signals and
1897 * Data Signals is allowed.
1898 * The controller has to measure and compensate this skew for every byte-lane.
1899 * By delaying either all DQs signals or DQS signal, a full phase
1900 * shift can be introduced.
1901 * It is assumed that one byte-lane's DQs signals have the same routing delay.
1902 *
1903 * To measure the actual skew, the DRAM is placed in "read leveling" mode.
1904 * In read leveling mode the DRAM-chip outputs an alternating periodic pattern.
1905 * The memory controller iterates over all possible values to do a full phase shift
1906 * and issues read commands.
1907 * With DQS and DQs in phase the data read is expected to alternate on every byte:
1908 * 0xFF 0x00 0xFF ...
1909 * Once the controller has detected this pattern a bit in the result register is
1910 * set for the current phase shift.
1911 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001912static void read_training(ramctr_timing * ctrl)
1913{
1914 int channel, slotrank, lane;
1915
1916 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1917 u32 r32;
1918 int all_high, some_high;
1919 int upperA[NUM_LANES];
1920 struct timA_minmax mnmx;
1921
1922 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001923
1924 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001925 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1926 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1927 0xc01 | (ctrl->tRP << 16));
1928 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1929 (slotrank << 24) | 0x60400);
1930 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1931 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
1932
1933 write32(DEFAULT_MCHBAR + 0x3400, (slotrank << 2) | 0x8001);
1934
1935 ctrl->timings[channel][slotrank].val_4028 = 4;
1936 ctrl->timings[channel][slotrank].val_4024 = 55;
1937 program_timings(ctrl, channel);
1938
1939 discover_timA_coarse(ctrl, channel, slotrank, upperA);
1940
1941 all_high = 1;
1942 some_high = 0;
1943 FOR_ALL_LANES {
1944 if (ctrl->timings[channel][slotrank].lanes[lane].
1945 timA >= 0x40)
1946 some_high = 1;
1947 else
1948 all_high = 0;
1949 }
1950
1951 if (all_high) {
1952 ctrl->timings[channel][slotrank].val_4028--;
1953 printram("4028--;\n");
1954 FOR_ALL_LANES {
1955 ctrl->timings[channel][slotrank].lanes[lane].
1956 timA -= 0x40;
1957 upperA[lane] -= 0x40;
1958
1959 }
1960 } else if (some_high) {
1961 ctrl->timings[channel][slotrank].val_4024++;
1962 ctrl->timings[channel][slotrank].val_4028++;
1963 printram("4024++;\n");
1964 printram("4028++;\n");
1965 }
1966
1967 program_timings(ctrl, channel);
1968
1969 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1970
1971 discover_402x(ctrl, channel, slotrank, upperA);
1972
1973 post_timA_change(ctrl, channel, slotrank, &mnmx);
1974 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1975
1976 discover_timA_fine(ctrl, channel, slotrank, upperA);
1977
1978 post_timA_change(ctrl, channel, slotrank, &mnmx);
1979 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1980
1981 FOR_ALL_LANES {
1982 ctrl->timings[channel][slotrank].lanes[lane].timA -= mnmx.timA_min_high * 0x40;
1983 }
1984 ctrl->timings[channel][slotrank].val_4028 -= mnmx.timA_min_high;
1985 printram("4028 -= %d;\n", mnmx.timA_min_high);
1986
1987 post_timA_change(ctrl, channel, slotrank, &mnmx);
1988
1989 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
1990 ctrl->timings[channel][slotrank].val_4024,
1991 ctrl->timings[channel][slotrank].val_4028);
1992
1993 FOR_ALL_LANES
1994 printram("%d, %d, %d, %x\n", channel, slotrank,
1995 lane,
1996 ctrl->timings[channel][slotrank].lanes[lane].timA);
1997
1998 write32(DEFAULT_MCHBAR + 0x3400, 0);
1999
Patrick Rudolph371d2912015-10-09 13:33:25 +02002000 /* toggle IO reset bit */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002001 r32 = read32(DEFAULT_MCHBAR + 0x5030);
2002 write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20);
2003 udelay(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002004 write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002005 udelay(1);
2006 }
2007
2008 FOR_ALL_POPULATED_CHANNELS {
2009 program_timings(ctrl, channel);
2010 }
2011 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2012 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
2013 + 4 * lane, 0);
2014 }
2015}
2016
2017static void test_timC(ramctr_timing * ctrl, int channel, int slotrank)
2018{
2019 int lane;
2020
2021 FOR_ALL_LANES {
2022 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + 4 * lane, 0);
2023 read32(DEFAULT_MCHBAR + 0x4140 + 0x400 * channel + 4 * lane);
2024 }
2025
2026 wait_428c(channel);
2027
Patrick Rudolph371d2912015-10-09 13:33:25 +02002028 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002029 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2030 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2031 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD) << 10)
2032 | 4 | (ctrl->tRCD << 16));
2033
2034 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2035 (slotrank << 24) | (6 << 16));
2036
2037 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
2038
Patrick Rudolph371d2912015-10-09 13:33:25 +02002039 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002040 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2041 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8041001);
2042 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2043 (slotrank << 24) | 8);
2044 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2045
Patrick Rudolph371d2912015-10-09 13:33:25 +02002046 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002047 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2048 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x80411f4);
2049 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
2050 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
2051
Patrick Rudolph371d2912015-10-09 13:33:25 +02002052 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002053 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2054 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2055 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2056 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2057 (slotrank << 24) | 8);
2058 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2059
2060 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2061
2062 wait_428c(channel);
2063
Patrick Rudolph371d2912015-10-09 13:33:25 +02002064 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002065 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2066 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2067 0xc01 | (ctrl->tRP << 16));
2068 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2069 (slotrank << 24) | 0x60400);
2070 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2071
Patrick Rudolph371d2912015-10-09 13:33:25 +02002072 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002073 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2074 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2075 (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) << 10)
2076 | 8 | (ctrl->CAS << 16));
2077
2078 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2079 (slotrank << 24) | 0x60000);
2080
2081 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x244);
2082
Patrick Rudolph371d2912015-10-09 13:33:25 +02002083 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002084 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2085 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2086 0x40011f4 | (max(ctrl->tRTP, 8) << 16));
2087 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
2088 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
2089
Patrick Rudolph371d2912015-10-09 13:33:25 +02002090 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002091 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2092 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2093 0xc01 | (ctrl->tRP << 16));
2094 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2095 (slotrank << 24) | 0x60400);
2096 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2097 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2098 wait_428c(channel);
2099}
2100
2101static void discover_timC(ramctr_timing * ctrl, int channel, int slotrank)
2102{
2103 int timC;
2104 int statistics[NUM_LANES][MAX_TIMC + 1];
2105 int lane;
2106
2107 wait_428c(channel);
2108
Patrick Rudolph371d2912015-10-09 13:33:25 +02002109 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002110 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2111 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2112 0xc01 | (ctrl->tRP << 16));
2113 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2114 (slotrank << 24) | 0x60400);
2115 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2116 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2117
2118 for (timC = 0; timC <= MAX_TIMC; timC++) {
2119 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
2120 timC = timC;
2121 program_timings(ctrl, channel);
2122
2123 test_timC(ctrl, channel, slotrank);
2124
2125 FOR_ALL_LANES {
2126 statistics[lane][timC] =
2127 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2128 0x400 * channel);
2129 printram("Cstat: %d, %d, %d, %x, %x\n",
2130 channel, slotrank, lane, timC,
2131 statistics[lane][timC]);
2132 }
2133 }
2134 FOR_ALL_LANES {
2135 struct run rn =
2136 get_longest_zero_run(statistics[lane], MAX_TIMC + 1);
2137 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
2138 if (rn.all)
2139 printk(BIOS_CRIT, "timC discovery failed");
2140 printram("Cval: %d, %d, %d, %x\n", channel, slotrank,
2141 lane, ctrl->timings[channel][slotrank].lanes[lane].timC);
2142 }
2143}
2144
2145static int get_precedening_channels(ramctr_timing * ctrl, int target_channel)
2146{
2147 int channel, ret = 0;
2148 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
2149 ret++;
2150 return ret;
2151}
2152
2153static void fill_pattern0(ramctr_timing * ctrl, int channel, u32 a, u32 b)
2154{
2155 unsigned j;
2156 unsigned channel_offset =
2157 get_precedening_channels(ctrl, channel) * 0x40;
2158 printram("channel_offset=%x\n", channel_offset);
2159 for (j = 0; j < 16; j++)
2160 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
2161 sfence();
2162}
2163
2164static int num_of_channels(const ramctr_timing * ctrl)
2165{
2166 int ret = 0;
2167 int channel;
2168 FOR_ALL_POPULATED_CHANNELS ret++;
2169 return ret;
2170}
2171
2172static void fill_pattern1(ramctr_timing * ctrl, int channel)
2173{
2174 unsigned j;
2175 unsigned channel_offset =
2176 get_precedening_channels(ctrl, channel) * 0x40;
2177 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2178 for (j = 0; j < 16; j++)
2179 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
2180 for (j = 0; j < 16; j++)
2181 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
2182 sfence();
2183}
2184
2185static void precharge(ramctr_timing * ctrl)
2186{
2187 int channel, slotrank, lane;
2188
2189 FOR_ALL_POPULATED_CHANNELS {
2190 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2191 ctrl->timings[channel][slotrank].lanes[lane].falling =
2192 16;
2193 ctrl->timings[channel][slotrank].lanes[lane].rising =
2194 16;
2195 } program_timings(ctrl, channel);
2196
2197 FOR_ALL_POPULATED_RANKS {
2198 wait_428c(channel);
2199
Patrick Rudolph371d2912015-10-09 13:33:25 +02002200 /* DRAM command MRS
2201 * write MR3 MPR enable
2202 * in this mode only RD and RDA are allowed
2203 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002204 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2205 0x1f000);
2206 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2207 0xc01 | (ctrl->tMOD << 16));
2208 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2209 (slotrank << 24) | 0x360004);
2210 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2211
Patrick Rudolph371d2912015-10-09 13:33:25 +02002212 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002213 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2214 0x1f105);
2215 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2216 0x4041003);
2217 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2218 (slotrank << 24) | 0);
2219 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2220
Patrick Rudolph371d2912015-10-09 13:33:25 +02002221 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002222 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2223 0x1f105);
2224 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2225 0x1001 | ((ctrl->CAS + 8) << 16));
2226 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2227 (slotrank << 24) | 0x60000);
2228 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2229
Patrick Rudolph371d2912015-10-09 13:33:25 +02002230 /* DRAM command MRS
2231 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002232 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2233 0x1f000);
2234 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2235 0xc01 | (ctrl->tMOD << 16));
2236 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2237 (slotrank << 24) | 0x360000);
2238 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2239 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2240 0xc0001);
2241
2242 wait_428c(channel);
2243 }
2244
2245 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2246 ctrl->timings[channel][slotrank].lanes[lane].falling =
2247 48;
2248 ctrl->timings[channel][slotrank].lanes[lane].rising =
2249 48;
2250 }
2251
2252 program_timings(ctrl, channel);
2253
2254 FOR_ALL_POPULATED_RANKS {
2255 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002256 /* DRAM command MRS
2257 * write MR3 MPR enable
2258 * in this mode only RD and RDA are allowed
2259 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002260 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2261 0x1f000);
2262 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2263 0xc01 | (ctrl->tMOD << 16));
2264 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2265 (slotrank << 24) | 0x360004);
2266 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2267
Patrick Rudolph371d2912015-10-09 13:33:25 +02002268 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002269 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2270 0x1f105);
2271 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2272 0x4041003);
2273 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2274 (slotrank << 24) | 0);
2275 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2276
Patrick Rudolph371d2912015-10-09 13:33:25 +02002277 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002278 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2279 0x1f105);
2280 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2281 0x1001 | ((ctrl->CAS + 8) << 16));
2282 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2283 (slotrank << 24) | 0x60000);
2284 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2285
Patrick Rudolph371d2912015-10-09 13:33:25 +02002286 /* DRAM command MRS
2287 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002288 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2289 0x1f000);
2290 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2291 0xc01 | (ctrl->tMOD << 16));
2292
2293 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2294 (slotrank << 24) | 0x360000);
2295 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2296
2297 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2298 0xc0001);
2299 wait_428c(channel);
2300 }
2301 }
2302}
2303
2304static void test_timB(ramctr_timing * ctrl, int channel, int slotrank)
2305{
Patrick Rudolph371d2912015-10-09 13:33:25 +02002306 /* enable DQs on this slotrank */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002307 write_mrreg(ctrl, channel, slotrank, 1,
2308 0x80 | make_mr1(ctrl, slotrank));
2309
2310 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002311 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002312 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f207);
2313 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2314 0x8000c01 | ((ctrl->CWL + ctrl->tWLO) << 16));
2315 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2316 8 | (slotrank << 24));
2317 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2318
Patrick Rudolph371d2912015-10-09 13:33:25 +02002319 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002320 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f107);
2321 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2322 0x4000c01 | ((ctrl->CAS + 38) << 16));
2323 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2324 (slotrank << 24) | 4);
2325 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2326
2327 write32(DEFAULT_MCHBAR + 0x400 * channel + 0x4284, 0x40001);
2328 wait_428c(channel);
2329
Patrick Rudolph371d2912015-10-09 13:33:25 +02002330 /* disable DQs on this slotrank */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002331 write_mrreg(ctrl, channel, slotrank, 1,
2332 0x1080 | make_mr1(ctrl, slotrank));
2333}
2334
2335static void discover_timB(ramctr_timing * ctrl, int channel, int slotrank)
2336{
2337 int timB;
2338 int statistics[NUM_LANES][128];
2339 int lane;
2340
2341 write32(DEFAULT_MCHBAR + 0x3400, 0x108052 | (slotrank << 2));
2342
2343 for (timB = 0; timB < 128; timB++) {
2344 FOR_ALL_LANES {
2345 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
2346 }
2347 program_timings(ctrl, channel);
2348
2349 test_timB(ctrl, channel, slotrank);
2350
2351 FOR_ALL_LANES {
2352 statistics[lane][timB] =
2353 !((read32
2354 (DEFAULT_MCHBAR + lane_registers[lane] +
2355 channel * 0x100 + 4 + ((timB / 32) & 1) * 4)
2356 >> (timB % 32)) & 1);
2357 printram("Bstat: %d, %d, %d, %x, %x\n",
2358 channel, slotrank, lane, timB,
2359 statistics[lane][timB]);
2360 }
2361 }
2362 FOR_ALL_LANES {
2363 struct run rn = get_longest_zero_run(statistics[lane], 128);
Patrick Rudolph9f1fbb92015-08-17 19:24:12 +02002364 if (rn.start < rn.middle) {
2365 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
2366 } else {
2367 /* In this case statistics[lane][7f] and statistics[lane][0] are
2368 * both zero.
2369 * Prefer a smaller value over rn.start to prevent failures in
2370 * the following write tests.
2371 */
2372 ctrl->timings[channel][slotrank].lanes[lane].timB = 0;
2373 }
2374
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002375 if (rn.all)
2376 die("timB discovery failed");
2377 printram("Bval: %d, %d, %d, %x\n", channel, slotrank,
2378 lane, ctrl->timings[channel][slotrank].lanes[lane].timB);
2379 }
2380}
2381
2382static int get_timB_high_adjust(u64 val)
2383{
2384 int i;
2385
2386 /* good */
2387 if (val == 0xffffffffffffffffLL)
2388 return 0;
2389
2390 if (val >= 0xf000000000000000LL) {
2391 /* needs negative adjustment */
2392 for (i = 0; i < 8; i++)
2393 if (val << (8 * (7 - i) + 4))
2394 return -i;
2395 } else {
2396 /* needs positive adjustment */
2397 for (i = 0; i < 8; i++)
2398 if (val >> (8 * (7 - i) + 4))
2399 return i;
2400 }
2401 return 8;
2402}
2403
2404static void adjust_high_timB(ramctr_timing * ctrl)
2405{
2406 int channel, slotrank, lane, old;
2407 write32(DEFAULT_MCHBAR + 0x3400, 0x200);
2408 FOR_ALL_POPULATED_CHANNELS {
2409 fill_pattern1(ctrl, channel);
2410 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 1);
2411 }
2412 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2413
2414 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x10001);
2415
2416 wait_428c(channel);
2417
Patrick Rudolph371d2912015-10-09 13:33:25 +02002418 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002419 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2420 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2421 0xc01 | (ctrl->tRCD << 16));
2422 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2423 (slotrank << 24) | 0x60000);
2424 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2425
Patrick Rudolph371d2912015-10-09 13:33:25 +02002426 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002427 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2428 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8040c01);
2429 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2430 (slotrank << 24) | 0x8);
2431 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2432
Patrick Rudolph371d2912015-10-09 13:33:25 +02002433 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002434 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2435 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x8041003);
2436 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2437 (slotrank << 24));
2438 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x3e2);
2439
Patrick Rudolph371d2912015-10-09 13:33:25 +02002440 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002441 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2442 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2443 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2444 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2445 (slotrank << 24) | 0x8);
2446 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2447
2448 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2449
2450 wait_428c(channel);
2451
Patrick Rudolph371d2912015-10-09 13:33:25 +02002452 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002453 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2454 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2455 0xc01 | ((ctrl->tRP) << 16));
2456 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2457 (slotrank << 24) | 0x60400);
2458 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2459
Patrick Rudolph371d2912015-10-09 13:33:25 +02002460 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002461 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2462 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2463 0xc01 | ((ctrl->tRCD) << 16));
2464 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2465 (slotrank << 24) | 0x60000);
2466 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2467
Patrick Rudolph371d2912015-10-09 13:33:25 +02002468 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002469 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x3f105);
2470 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2471 0x4000c01 |
2472 ((ctrl->tRP +
2473 ctrl->timings[channel][slotrank].val_4024 +
2474 ctrl->timings[channel][slotrank].val_4028) << 16));
2475 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2476 (slotrank << 24) | 0x60008);
2477 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2478
2479 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
2480 wait_428c(channel);
2481 FOR_ALL_LANES {
2482 u64 res =
2483 read32(DEFAULT_MCHBAR + lane_registers[lane] +
2484 0x100 * channel + 4);
2485 res |=
2486 ((u64) read32(DEFAULT_MCHBAR + lane_registers[lane] +
2487 0x100 * channel + 8)) << 32;
2488 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
2489 ctrl->timings[channel][slotrank].lanes[lane].timB +=
2490 get_timB_high_adjust(res) * 64;
2491
2492 printk(BIOS_DEBUG, "High adjust %d:%016llx\n", lane, res);
2493 printram("Bval+: %d, %d, %d, %x -> %x\n", channel,
2494 slotrank, lane, old,
2495 ctrl->timings[channel][slotrank].lanes[lane].
2496 timB);
2497 }
2498 }
2499 write32(DEFAULT_MCHBAR + 0x3400, 0);
2500}
2501
2502static void write_op(ramctr_timing * ctrl, int channel)
2503{
2504 int slotrank;
2505
2506 wait_428c(channel);
2507
2508 /* choose an existing rank. */
2509 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2510
Patrick Rudolph371d2912015-10-09 13:33:25 +02002511 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002512 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2513 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2514
2515 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2516 (slotrank << 24) | 0x60000);
2517
2518 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2519
2520 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2521 wait_428c(channel);
2522}
2523
Patrick Rudolph371d2912015-10-09 13:33:25 +02002524/* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
2525 * DDR3 adopted the fly-by topology. The data and strobes signals reach
2526 * the chips at different times with respect to command, address and
2527 * clock signals.
2528 * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase
2529 * shift can be introduced.
2530 * It is assumed that the CLK/ADDR/CMD signals have the same routing delay.
2531 *
2532 * To find the required phase shift the DRAM is placed in "write leveling" mode.
2533 * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the
2534 * sampled value on the data lanes (DQs).
2535 */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002536static void write_training(ramctr_timing * ctrl)
2537{
2538 int channel, slotrank, lane;
2539 u32 r32;
2540
2541 FOR_ALL_POPULATED_CHANNELS
2542 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
2543 read32(DEFAULT_MCHBAR + 0x4008 +
2544 0x400 * channel) | 0x8000000);
2545
2546 FOR_ALL_POPULATED_CHANNELS {
2547 write_op(ctrl, channel);
2548 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2549 read32(DEFAULT_MCHBAR + 0x4020 +
2550 0x400 * channel) | 0x200000);
2551 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002552
2553 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002554 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2555 FOR_ALL_POPULATED_CHANNELS {
2556 write_op(ctrl, channel);
2557 }
2558
Patrick Rudolph371d2912015-10-09 13:33:25 +02002559 /* enable write leveling on all ranks
2560 * disable all DQ outputs
2561 * only NOP is allowed in this mode */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002562 FOR_ALL_CHANNELS
2563 FOR_ALL_POPULATED_RANKS
2564 write_mrreg(ctrl, channel, slotrank, 1,
2565 make_mr1(ctrl, slotrank) | 0x1080);
2566
2567 write32(DEFAULT_MCHBAR + 0x3400, 0x108052);
2568
Patrick Rudolph371d2912015-10-09 13:33:25 +02002569 /* toggle IO reset bit */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002570 r32 = read32(DEFAULT_MCHBAR + 0x5030);
2571 write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20);
2572 udelay(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002573 write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002574 udelay(1);
2575
Patrick Rudolph371d2912015-10-09 13:33:25 +02002576 /* set any valid value for timB, it gets corrected later */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002577 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2578 discover_timB(ctrl, channel, slotrank);
2579
Patrick Rudolph371d2912015-10-09 13:33:25 +02002580 /* disable write leveling on all ranks */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002581 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2582 write_mrreg(ctrl, channel,
2583 slotrank, 1, make_mr1(ctrl, slotrank));
2584
2585 write32(DEFAULT_MCHBAR + 0x3400, 0);
2586
2587 FOR_ALL_POPULATED_CHANNELS
2588 wait_428c(channel);
2589
Patrick Rudolph371d2912015-10-09 13:33:25 +02002590 /* refresh enable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002591 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) | 8);
2592
2593 FOR_ALL_POPULATED_CHANNELS {
2594 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2595 ~0x00200000 & read32(DEFAULT_MCHBAR + 0x4020 +
2596 0x400 * channel));
2597 read32(DEFAULT_MCHBAR + 0x428c + 0x400 * channel);
2598 wait_428c(channel);
2599
Patrick Rudolph371d2912015-10-09 13:33:25 +02002600 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002601 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2602 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
2603 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, 0x60000);
2604 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2605
2606 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2607 wait_428c(channel);
2608 }
2609
Patrick Rudolph371d2912015-10-09 13:33:25 +02002610 /* toggle IO reset bit */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002611 r32 = read32(DEFAULT_MCHBAR + 0x5030);
2612 write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20);
2613 udelay(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002614 write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002615 udelay(1);
2616
2617 printram("CPE\n");
2618 precharge(ctrl);
2619 printram("CPF\n");
2620
2621 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2622 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2623 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2624 0);
2625 }
2626
2627 FOR_ALL_POPULATED_CHANNELS {
2628 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
2629 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2630 }
2631
2632 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2633 discover_timC(ctrl, channel, slotrank);
2634
2635 FOR_ALL_POPULATED_CHANNELS
2636 program_timings(ctrl, channel);
2637
Patrick Rudolph371d2912015-10-09 13:33:25 +02002638 /* measure and adjust timB timings */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002639 adjust_high_timB(ctrl);
2640
2641 FOR_ALL_POPULATED_CHANNELS
2642 program_timings(ctrl, channel);
2643
2644 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2645 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2646 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2647 0);
2648 }
2649}
2650
2651static int test_320c(ramctr_timing * ctrl, int channel, int slotrank)
2652{
2653 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2654 int timC_delta;
2655 int lanes_ok = 0;
2656 int ctr = 0;
2657 int lane;
2658
2659 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2660 FOR_ALL_LANES {
2661 ctrl->timings[channel][slotrank].lanes[lane].timC =
2662 saved_rt.lanes[lane].timC + timC_delta;
2663 }
2664 program_timings(ctrl, channel);
2665 FOR_ALL_LANES {
2666 write32(DEFAULT_MCHBAR + 4 * lane + 0x4f40, 0);
2667 }
2668
2669 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2670
2671 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002672 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002673 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2674 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2675 ((max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1)) << 10)
2676 | 8 | (ctrl->tRCD << 16));
2677
2678 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2679 (slotrank << 24) | ctr | 0x60000);
2680
2681 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002682 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002683 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2684 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2685 0x8001020 | ((ctrl->CWL + ctrl->tWTR + 8) << 16));
2686 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2687 (slotrank << 24));
2688 write32(DEFAULT_MCHBAR + 0x4244 + 0x400 * channel, 0x389abcd);
2689 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x20e42);
2690
Patrick Rudolph371d2912015-10-09 13:33:25 +02002691 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002692 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2693 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2694 0x4001020 | (max(ctrl->tRTP, 8) << 16));
2695 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2696 (slotrank << 24));
2697 write32(DEFAULT_MCHBAR + 0x4248 + 0x400 * channel, 0x389abcd);
2698 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x20e42);
2699
Patrick Rudolph371d2912015-10-09 13:33:25 +02002700 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002701 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2702 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, 0xf1001);
2703 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2704 (slotrank << 24) | 0x60400);
2705 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2706
2707 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2708 wait_428c(channel);
2709 FOR_ALL_LANES {
2710 u32 r32 =
2711 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2712 0x400 * channel);
2713
2714 if (r32 == 0)
2715 lanes_ok |= 1 << lane;
2716 }
2717 ctr++;
2718 if (lanes_ok == ((1 << NUM_LANES) - 1))
2719 break;
2720 }
2721
2722 ctrl->timings[channel][slotrank] = saved_rt;
2723
2724 printram("3lanes: %x\n", lanes_ok);
2725 return lanes_ok != ((1 << NUM_LANES) - 1);
2726}
2727
2728#include "raminit_patterns.h"
2729
2730static void fill_pattern5(ramctr_timing * ctrl, int channel, int patno)
2731{
2732 unsigned i, j;
2733 unsigned channel_offset =
2734 get_precedening_channels(ctrl, channel) * 0x40;
2735 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2736
2737 if (patno) {
2738 u8 base8 = 0x80 >> ((patno - 1) % 8);
2739 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2740 for (i = 0; i < 32; i++) {
2741 for (j = 0; j < 16; j++) {
2742 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
2743 if (invert[patno - 1][i] & (1 << (j / 2)))
2744 val = ~val;
2745 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2746 j * 4), val);
2747 }
2748 }
2749
2750 } else {
2751 for (i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++) {
2752 for (j = 0; j < 16; j++)
2753 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2754 j * 4), pattern[i][j]);
2755 }
2756 sfence();
2757 }
2758}
2759
2760static void reprogram_320c(ramctr_timing * ctrl)
2761{
2762 int channel, slotrank;
2763 u32 r32;
2764
2765 FOR_ALL_POPULATED_CHANNELS {
2766 wait_428c(channel);
2767
2768 /* choose an existing rank. */
2769 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2770
Patrick Rudolph371d2912015-10-09 13:33:25 +02002771 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002772 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2773 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2774
2775 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2776 (slotrank << 24) | 0x60000);
2777
2778 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2779
2780 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2781 wait_428c(channel);
2782 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2783 read32(DEFAULT_MCHBAR + 0x4020 +
2784 0x400 * channel) | 0x200000);
2785 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002786
2787 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002788 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2789 FOR_ALL_POPULATED_CHANNELS {
2790 wait_428c(channel);
2791
2792 /* choose an existing rank. */
2793 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2794
Patrick Rudolph371d2912015-10-09 13:33:25 +02002795 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002796 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2797 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2798
2799 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2800 (slotrank << 24) | 0x60000);
2801
2802 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2803
2804 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2805 wait_428c(channel);
2806 }
2807
2808 /* jedec reset */
2809 dram_jedecreset(ctrl);
2810 /* mrs commands. */
2811 dram_mrscommands(ctrl);
2812
Patrick Rudolph371d2912015-10-09 13:33:25 +02002813 /* toggle IO reset bit */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002814 r32 = read32(DEFAULT_MCHBAR + 0x5030);
2815 write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20);
2816 udelay(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002817 write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002818 udelay(1);
2819}
2820
2821#define MIN_C320C_LEN 13
2822
2823static int try_cmd_stretch(ramctr_timing * ctrl, int cmd_stretch)
2824{
2825 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2826 int channel, slotrank;
2827 int c320c;
2828 int stat[NUM_SLOTRANKS][256];
2829
2830 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2831 saved_timings[channel][slotrank] = ctrl->timings[channel][slotrank];
2832 }
2833
2834 FOR_ALL_POPULATED_CHANNELS {
2835 ctrl->cmd_stretch[channel] = cmd_stretch;
2836 }
2837
2838 FOR_ALL_POPULATED_CHANNELS
2839 MCHBAR32(0x4004 + 0x400 * channel) =
2840 ctrl->tRRD
2841 | (ctrl->tRTP << 4)
2842 | (ctrl->tCKE << 8)
2843 | (ctrl->tWTR << 12)
2844 | (ctrl->tFAW << 16)
2845 | (ctrl->tWR << 24)
2846 | (ctrl->cmd_stretch[channel] << 30);
2847
2848
2849 FOR_ALL_CHANNELS {
2850 int delta = 0;
2851 if (ctrl->cmd_stretch[channel] == 2)
2852 delta = 2;
2853 else if (ctrl->cmd_stretch[channel] == 0)
2854 delta = 4;
2855
2856 FOR_ALL_POPULATED_RANKS {
2857 ctrl->timings[channel][slotrank].val_4024 -= delta;
2858 }
2859 }
2860
2861 FOR_ALL_POPULATED_CHANNELS {
2862 for (c320c = -127; c320c <= 127; c320c++) {
2863 FOR_ALL_POPULATED_RANKS {
2864 ctrl->timings[channel][slotrank].val_320c = c320c;
2865 }
2866 program_timings(ctrl, channel);
2867 reprogram_320c(ctrl);
2868 FOR_ALL_POPULATED_RANKS {
2869 stat[slotrank][c320c + 127] =
2870 test_320c(ctrl, channel, slotrank);
2871 printram("3stat: %d, %d, %d: %d\n",
2872 channel, slotrank, c320c,
2873 stat[slotrank][c320c + 127]);
2874 }
2875 }
2876 FOR_ALL_POPULATED_RANKS {
2877 struct run rn =
2878 get_longest_zero_run(stat[slotrank], 255);
2879 ctrl->timings[channel][slotrank].val_320c =
2880 rn.middle - 127;
2881 printram("3val: %d, %d: %d\n", channel,
2882 slotrank,
2883 ctrl->timings[channel][slotrank].val_320c);
2884 if (rn.all || rn.length < MIN_C320C_LEN) {
2885 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2886 ctrl->timings[channel][slotrank] = saved_timings[channel][slotrank];
2887 }
2888 return 0;
2889 }
2890 }
2891 }
2892 return 1;
2893}
2894
Patrick Rudolph371d2912015-10-09 13:33:25 +02002895/* Adjust CMD phase shift and try multiple command rates.
2896 * A command rate of 2T doubles the time needed for address and
2897 * command decode. */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002898static void command_training(ramctr_timing * ctrl)
2899{
2900 int channel;
2901
2902 FOR_ALL_POPULATED_CHANNELS {
2903 fill_pattern5(ctrl, channel, 0);
2904 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2905 }
2906
2907 /* try command rate 1T and 2T */
2908 if (!try_cmd_stretch(ctrl, 0) && !try_cmd_stretch(ctrl, 2))
2909 die("c320c discovery failed");
2910
2911 FOR_ALL_POPULATED_CHANNELS {
2912 program_timings(ctrl, channel);
2913 }
2914
2915 reprogram_320c(ctrl);
2916}
2917
2918static void discover_edges_real(ramctr_timing * ctrl, int channel, int slotrank,
2919 int *edges)
2920{
2921 int edge;
2922 int statistics[NUM_LANES][MAX_EDGE_TIMING + 1];
2923 int lane;
2924
2925 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2926 FOR_ALL_LANES {
2927 ctrl->timings[channel][slotrank].lanes[lane].rising =
2928 edge;
2929 ctrl->timings[channel][slotrank].lanes[lane].falling =
2930 edge;
2931 }
2932 printram("edge %02x\n", edge);
2933 program_timings(ctrl, channel);
2934
2935 FOR_ALL_LANES {
2936 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2937 4 * lane, 0);
2938 read32(DEFAULT_MCHBAR + 0x400 * channel + 4 * lane +
2939 0x4140);
2940 }
2941
2942 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002943 /* DRAM command MRS
2944 * write MR3 MPR enable
2945 * in this mode only RD and RDA are allowed
2946 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002947 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
2948 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2949 (0xc01 | (ctrl->tMOD << 16)));
2950 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2951 (slotrank << 24) | 0x360004);
2952 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2953
Patrick Rudolph371d2912015-10-09 13:33:25 +02002954 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002955 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
2956 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x40411f4);
2957 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2958 (slotrank << 24));
2959 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2960
Patrick Rudolph371d2912015-10-09 13:33:25 +02002961 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002962 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2963 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2964 0x1001 | ((ctrl->CAS + 8) << 16));
2965 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2966 (slotrank << 24) | 0x60000);
2967 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2968
Patrick Rudolph371d2912015-10-09 13:33:25 +02002969 /* DRAM command MRS
2970 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002971 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
2972 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2973 (0xc01 | (ctrl->tMOD << 16)));
2974 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2975 (slotrank << 24) | 0x360000);
2976 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2977
2978 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2979
2980 wait_428c(channel);
2981
2982 FOR_ALL_LANES {
2983 statistics[lane][edge] =
2984 read32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2985 lane * 4);
2986 }
2987 }
2988 FOR_ALL_LANES {
2989 struct run rn =
2990 get_longest_zero_run(statistics[lane], MAX_EDGE_TIMING + 1);
2991 edges[lane] = rn.middle;
2992 if (rn.all)
2993 die("edge discovery failed");
2994 printram("eval %d, %d, %d, %02x\n", channel, slotrank,
2995 lane, edges[lane]);
2996 }
2997}
2998
2999static void discover_edges(ramctr_timing * ctrl)
3000{
3001 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3002 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3003 int channel, slotrank, lane;
3004 u32 r32;
3005
3006 write32(DEFAULT_MCHBAR + 0x3400, 0);
3007
Patrick Rudolph371d2912015-10-09 13:33:25 +02003008 /* toggle IO reset bit */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003009 r32 = read32(DEFAULT_MCHBAR + 0x5030);
3010 write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20);
3011 udelay(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003012 write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003013 udelay(1);
3014
3015 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
3016 write32(DEFAULT_MCHBAR + 4 * lane +
3017 0x400 * channel + 0x4080, 0);
3018 }
3019
3020 FOR_ALL_POPULATED_CHANNELS {
3021 fill_pattern0(ctrl, channel, 0, 0);
3022 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3023 FOR_ALL_LANES {
3024 read32(DEFAULT_MCHBAR + 0x400 * channel +
3025 lane * 4 + 0x4140);
3026 }
3027
3028 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3029 ctrl->timings[channel][slotrank].lanes[lane].falling =
3030 16;
3031 ctrl->timings[channel][slotrank].lanes[lane].rising =
3032 16;
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 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3081 0xc0001);
3082
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_POPULATED_RANKS FOR_ALL_LANES {
3089 ctrl->timings[channel][slotrank].lanes[lane].falling =
3090 48;
3091 ctrl->timings[channel][slotrank].lanes[lane].rising =
3092 48;
3093 }
3094
3095 program_timings(ctrl, channel);
3096
3097 FOR_ALL_POPULATED_RANKS {
3098 wait_428c(channel);
3099
Patrick Rudolph371d2912015-10-09 13:33:25 +02003100 /* DRAM command MRS
3101 * MR3 enable MPR
3102 * write MR3 MPR enable
3103 * in this mode only RD and RDA are allowed
3104 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003105 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3106 0x1f000);
3107 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3108 0xc01 | (ctrl->tMOD << 16));
3109 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3110 (slotrank << 24) | 0x360004);
3111 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3112
Patrick Rudolph371d2912015-10-09 13:33:25 +02003113 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003114 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3115 0x1f105);
3116 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3117 0x4041003);
3118 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3119 (slotrank << 24) | 0);
3120 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3121
Patrick Rudolph371d2912015-10-09 13:33:25 +02003122 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003123 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3124 0x1f105);
3125 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3126 0x1001 | ((ctrl->CAS + 8) << 16));
3127 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3128 (slotrank << 24) | 0x60000);
3129 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3130
Patrick Rudolph371d2912015-10-09 13:33:25 +02003131 /* DRAM command MRS
3132 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003133 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3134 0x1f000);
3135 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3136 0xc01 | (ctrl->tMOD << 16));
3137 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3138 (slotrank << 24) | 0x360000);
3139 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3140
3141 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3142 0xc0001);
3143 wait_428c(channel);
3144 }
3145
Patrick Rudolph371d2912015-10-09 13:33:25 +02003146 /* XXX: check any measured value ? */
3147
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003148 FOR_ALL_LANES {
3149 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel +
3150 lane * 4,
3151 ~read32(DEFAULT_MCHBAR + 0x4040 +
3152 0x400 * channel + lane * 4) & 0xff);
3153 }
3154
3155 fill_pattern0(ctrl, channel, 0, 0xffffffff);
3156 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3157 }
3158
3159 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3160 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
3161
3162 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3163 discover_edges_real(ctrl, channel, slotrank,
3164 falling_edges[channel][slotrank]);
3165 }
3166
3167 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
3168
3169 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3170 discover_edges_real(ctrl, channel, slotrank,
3171 rising_edges[channel][slotrank]);
3172 }
3173
3174 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3175
3176 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3177 ctrl->timings[channel][slotrank].lanes[lane].falling =
3178 falling_edges[channel][slotrank][lane];
3179 ctrl->timings[channel][slotrank].lanes[lane].rising =
3180 rising_edges[channel][slotrank][lane];
3181 }
3182
3183 FOR_ALL_POPULATED_CHANNELS {
3184 program_timings(ctrl, channel);
3185 }
3186
3187 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3188 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3189 0);
3190 }
3191}
3192
3193static void discover_edges_write_real(ramctr_timing * ctrl, int channel,
3194 int slotrank, int *edges)
3195{
3196 int edge;
3197 u32 raw_statistics[MAX_EDGE_TIMING + 1];
3198 int statistics[MAX_EDGE_TIMING + 1];
3199 const int reg3000b24[] = { 0, 0xc, 0x2c };
3200 int lane, i;
3201 int lower[NUM_LANES];
3202 int upper[NUM_LANES];
3203 int pat;
3204
3205 FOR_ALL_LANES {
3206 lower[lane] = 0;
3207 upper[lane] = MAX_EDGE_TIMING;
3208 }
3209
3210 for (i = 0; i < 3; i++) {
3211 write32(DEFAULT_MCHBAR + 0x3000 + 0x100 * channel,
3212 reg3000b24[i] << 24);
3213 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3214 fill_pattern5(ctrl, channel, pat);
3215 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
3216 printram("patterned\n");
3217 printram("[%x] = 0x%08x\n(%d, %d)\n",
3218 0x3000 + 0x100 * channel, reg3000b24[i] << 24, channel,
3219 slotrank);
3220 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
3221 FOR_ALL_LANES {
3222 ctrl->timings[channel][slotrank].lanes[lane].
3223 rising = edge;
3224 ctrl->timings[channel][slotrank].lanes[lane].
3225 falling = edge;
3226 }
3227 program_timings(ctrl, channel);
3228
3229 FOR_ALL_LANES {
3230 write32(DEFAULT_MCHBAR + 0x4340 +
3231 0x400 * channel + 4 * lane, 0);
3232 read32(DEFAULT_MCHBAR + 0x400 * channel +
3233 4 * lane + 0x4140);
3234 }
3235 wait_428c(channel);
3236
Patrick Rudolph371d2912015-10-09 13:33:25 +02003237 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003238 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3239 0x1f006);
3240 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3241 0x4 | (ctrl->tRCD << 16)
3242 | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) <<
3243 10));
3244 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3245 (slotrank << 24) | 0x60000);
3246 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel,
3247 0x240);
3248
Patrick Rudolph371d2912015-10-09 13:33:25 +02003249 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003250 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3251 0x1f201);
3252 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3253 0x8005020 | ((ctrl->tWTR + ctrl->CWL + 8) <<
3254 16));
3255 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3256 (slotrank << 24));
3257 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel,
3258 0x242);
3259
Patrick Rudolph371d2912015-10-09 13:33:25 +02003260 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003261 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3262 0x1f105);
3263 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3264 0x4005020 | (max(ctrl->tRTP, 8) << 16));
3265 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3266 (slotrank << 24));
3267 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel,
3268 0x242);
3269
Patrick Rudolph371d2912015-10-09 13:33:25 +02003270 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003271 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3272 0x1f002);
3273 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3274 0xc01 | (ctrl->tRP << 16));
3275 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3276 (slotrank << 24) | 0x60400);
3277 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3278
3279 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3280 0xc0001);
3281 wait_428c(channel);
3282 FOR_ALL_LANES {
3283 read32(DEFAULT_MCHBAR + 0x4340 +
3284 0x400 * channel + lane * 4);
3285 }
3286
3287 raw_statistics[edge] =
3288 MCHBAR32(0x436c + 0x400 * channel);
3289 }
3290 FOR_ALL_LANES {
3291 struct run rn;
3292 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
3293 statistics[edge] =
3294 ! !(raw_statistics[edge] & (1 << lane));
3295 rn = get_longest_zero_run(statistics,
3296 MAX_EDGE_TIMING + 1);
3297 printram("edges: %d, %d, %d: 0x%x-0x%x-0x%x, 0x%x-0x%x\n",
3298 channel, slotrank, i, rn.start, rn.middle,
3299 rn.end, rn.start + ctrl->edge_offset[i],
3300 rn.end - ctrl->edge_offset[i]);
3301 lower[lane] =
3302 max(rn.start + ctrl->edge_offset[i], lower[lane]);
3303 upper[lane] =
3304 min(rn.end - ctrl->edge_offset[i], upper[lane]);
3305 edges[lane] = (lower[lane] + upper[lane]) / 2;
Patrick Rudolph9733e282015-08-16 17:06:30 +02003306 if (rn.all || (lower[lane] > upper[lane]))
3307 die("edge write discovery failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003308
3309 }
3310 }
3311 }
3312
3313 write32(DEFAULT_MCHBAR + 0x3000, 0);
3314 printram("CPA\n");
3315}
3316
3317static void discover_edges_write(ramctr_timing * ctrl)
3318{
3319 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3320 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3321 int channel, slotrank, lane;
3322
3323 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3324 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
3325
3326 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3327 discover_edges_write_real(ctrl, channel, slotrank,
3328 falling_edges[channel][slotrank]);
3329 }
3330
3331 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
3332
3333 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3334 discover_edges_write_real(ctrl, channel, slotrank,
3335 rising_edges[channel][slotrank]);
3336 }
3337
3338 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3339
3340 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3341 ctrl->timings[channel][slotrank].lanes[lane].falling =
3342 falling_edges[channel][slotrank][lane];
3343 ctrl->timings[channel][slotrank].lanes[lane].rising =
3344 rising_edges[channel][slotrank][lane];
3345 }
3346
3347 FOR_ALL_POPULATED_CHANNELS
3348 program_timings(ctrl, channel);
3349
3350 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3351 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3352 0);
3353 }
3354}
3355
3356static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
3357{
3358 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003359 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003360 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
3361 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3362 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD)
3363 << 10) | (ctrl->tRCD << 16) | 4);
3364 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3365 (slotrank << 24) | 0x60000);
3366 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
3367
Patrick Rudolph371d2912015-10-09 13:33:25 +02003368 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003369 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
3370 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3371 0x80011e0 |
3372 ((ctrl->tWTR + ctrl->CWL + 8) << 16));
3373 write32(DEFAULT_MCHBAR + 0x4204 +
3374 0x400 * channel, (slotrank << 24));
3375 write32(DEFAULT_MCHBAR + 0x4214 +
3376 0x400 * channel, 0x242);
3377
Patrick Rudolph371d2912015-10-09 13:33:25 +02003378 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003379 write32(DEFAULT_MCHBAR + 0x4228 +
3380 0x400 * channel, 0x1f105);
3381 write32(DEFAULT_MCHBAR + 0x4238 +
3382 0x400 * channel,
3383 0x40011e0 | (max(ctrl->tRTP, 8) << 16));
3384 write32(DEFAULT_MCHBAR + 0x4208 +
3385 0x400 * channel, (slotrank << 24));
3386 write32(DEFAULT_MCHBAR + 0x4218 +
3387 0x400 * channel, 0x242);
3388
Patrick Rudolph371d2912015-10-09 13:33:25 +02003389 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003390 write32(DEFAULT_MCHBAR + 0x422c +
3391 0x400 * channel, 0x1f002);
3392 write32(DEFAULT_MCHBAR + 0x423c +
3393 0x400 * channel,
3394 0x1001 | (ctrl->tRP << 16));
3395 write32(DEFAULT_MCHBAR + 0x420c +
3396 0x400 * channel,
3397 (slotrank << 24) | 0x60400);
3398 write32(DEFAULT_MCHBAR + 0x421c +
3399 0x400 * channel, 0);
3400
3401 write32(DEFAULT_MCHBAR + 0x4284 +
3402 0x400 * channel, 0xc0001);
3403 wait_428c(channel);
3404}
3405
3406static void discover_timC_write(ramctr_timing * ctrl)
3407{
3408 const u8 rege3c_b24[3] = { 0, 0xf, 0x2f };
3409 int i, pat;
3410
3411 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3412 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3413 int channel, slotrank, lane;
3414
3415 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3416 lower[channel][slotrank][lane] = 0;
3417 upper[channel][slotrank][lane] = MAX_TIMC;
3418 }
3419
3420 write32(DEFAULT_MCHBAR + 0x4ea8, 1);
3421
3422 for (i = 0; i < 3; i++)
3423 FOR_ALL_POPULATED_CHANNELS {
3424 write32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100),
3425 (rege3c_b24[i] << 24)
3426 | (read32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100))
3427 & ~0x3f000000));
3428 udelay(2);
3429 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3430 FOR_ALL_POPULATED_RANKS {
3431 int timC;
3432 u32 raw_statistics[MAX_TIMC + 1];
3433 int statistics[MAX_TIMC + 1];
3434
3435 fill_pattern5(ctrl, channel, pat);
3436 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
3437 for (timC = 0; timC < MAX_TIMC + 1; timC++) {
3438 FOR_ALL_LANES
3439 ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
3440 program_timings(ctrl, channel);
3441
3442 test_timC_write (ctrl, channel, slotrank);
3443
3444 raw_statistics[timC] =
3445 MCHBAR32(0x436c + 0x400 * channel);
3446 }
3447 FOR_ALL_LANES {
3448 struct run rn;
3449 for (timC = 0; timC <= MAX_TIMC; timC++)
3450 statistics[timC] =
3451 !!(raw_statistics[timC] &
3452 (1 << lane));
3453 rn = get_longest_zero_run(statistics,
3454 MAX_TIMC + 1);
3455 if (rn.all)
3456 die("timC write discovery failed");
3457 printram("timC: %d, %d, %d: 0x%x-0x%x-0x%x, 0x%x-0x%x\n",
3458 channel, slotrank, i, rn.start,
3459 rn.middle, rn.end,
3460 rn.start + ctrl->timC_offset[i],
3461 rn.end - ctrl->timC_offset[i]);
3462 lower[channel][slotrank][lane] =
3463 max(rn.start + ctrl->timC_offset[i],
3464 lower[channel][slotrank][lane]);
3465 upper[channel][slotrank][lane] =
3466 min(rn.end - ctrl->timC_offset[i],
3467 upper[channel][slotrank][lane]);
3468
3469 }
3470 }
3471 }
3472 }
3473
3474 FOR_ALL_CHANNELS {
3475 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3476 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3477 ~0x3f000000));
3478 udelay(2);
3479 }
3480
3481 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3482
3483 printram("CPB\n");
3484
3485 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3486 printram("timC [%d, %d, %d] = 0x%x\n", channel,
3487 slotrank, lane,
3488 (lower[channel][slotrank][lane] +
3489 upper[channel][slotrank][lane]) / 2);
3490 ctrl->timings[channel][slotrank].lanes[lane].timC =
3491 (lower[channel][slotrank][lane] +
3492 upper[channel][slotrank][lane]) / 2;
3493 }
3494 FOR_ALL_POPULATED_CHANNELS {
3495 program_timings(ctrl, channel);
3496 }
3497}
3498
3499static void normalize_training(ramctr_timing * ctrl)
3500{
3501 int channel, slotrank, lane;
3502 int mat = 0;
3503
3504 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3505 int delta;
3506 FOR_ALL_LANES mat =
3507 max(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
3508 delta = (mat >> 6) - ctrl->timings[channel][slotrank].val_4028;
3509 ctrl->timings[channel][slotrank].val_4024 += delta;
3510 ctrl->timings[channel][slotrank].val_4028 += delta;
3511 }
3512
3513 FOR_ALL_POPULATED_CHANNELS {
3514 program_timings(ctrl, channel);
3515 }
3516}
3517
3518static void write_controller_mr(ramctr_timing * ctrl)
3519{
3520 int channel, slotrank;
3521
3522 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3523 write32(DEFAULT_MCHBAR + 0x0004 + (channel << 8) +
3524 lane_registers[slotrank], make_mr0(ctrl, slotrank));
3525 write32(DEFAULT_MCHBAR + 0x0008 + (channel << 8) +
3526 lane_registers[slotrank], make_mr1(ctrl, slotrank));
3527 }
3528}
3529
3530static void channel_test(ramctr_timing * ctrl)
3531{
3532 int channel, slotrank, lane;
3533
3534 FOR_ALL_POPULATED_CHANNELS
3535 if (read32(DEFAULT_MCHBAR + 0x42a0 + (channel << 10)) & 0xa000)
3536 die("Mini channel test failed (1)\n");
3537 FOR_ALL_POPULATED_CHANNELS {
3538 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
3539
3540 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3541 }
3542
3543 for (slotrank = 0; slotrank < 4; slotrank++)
3544 FOR_ALL_CHANNELS
3545 if (ctrl->rankmap[channel] & (1 << slotrank)) {
3546 FOR_ALL_LANES {
3547 write32(DEFAULT_MCHBAR + (0x4f40 + 4 * lane), 0);
3548 write32(DEFAULT_MCHBAR + (0x4d40 + 4 * lane), 0);
3549 }
3550 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003551 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003552 write32(DEFAULT_MCHBAR + 0x4220 + (channel << 10), 0x0001f006);
3553 write32(DEFAULT_MCHBAR + 0x4230 + (channel << 10), 0x0028a004);
3554 write32(DEFAULT_MCHBAR + 0x4200 + (channel << 10),
3555 0x00060000 | (slotrank << 24));
3556 write32(DEFAULT_MCHBAR + 0x4210 + (channel << 10), 0x00000244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003557 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003558 write32(DEFAULT_MCHBAR + 0x4224 + (channel << 10), 0x0001f201);
3559 write32(DEFAULT_MCHBAR + 0x4234 + (channel << 10), 0x08281064);
3560 write32(DEFAULT_MCHBAR + 0x4204 + (channel << 10),
3561 0x00000000 | (slotrank << 24));
3562 write32(DEFAULT_MCHBAR + 0x4214 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003563 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003564 write32(DEFAULT_MCHBAR + 0x4228 + (channel << 10), 0x0001f105);
3565 write32(DEFAULT_MCHBAR + 0x4238 + (channel << 10), 0x04281064);
3566 write32(DEFAULT_MCHBAR + 0x4208 + (channel << 10),
3567 0x00000000 | (slotrank << 24));
3568 write32(DEFAULT_MCHBAR + 0x4218 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003569 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003570 write32(DEFAULT_MCHBAR + 0x422c + (channel << 10), 0x0001f002);
3571 write32(DEFAULT_MCHBAR + 0x423c + (channel << 10), 0x00280c01);
3572 write32(DEFAULT_MCHBAR + 0x420c + (channel << 10),
3573 0x00060400 | (slotrank << 24));
3574 write32(DEFAULT_MCHBAR + 0x421c + (channel << 10), 0x00000240);
3575 write32(DEFAULT_MCHBAR + 0x4284 + (channel << 10), 0x000c0001);
3576 wait_428c(channel);
3577 FOR_ALL_LANES
3578 if (read32(DEFAULT_MCHBAR + 0x4340 + (channel << 10) + 4 * lane))
3579 die("Mini channel test failed (2)\n");
3580 }
3581}
3582
3583static void set_scrambling_seed(ramctr_timing * ctrl)
3584{
3585 int channel;
3586
3587 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them?
3588 I don't think so. */
3589 static u32 seeds[NUM_CHANNELS][3] = {
3590 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
3591 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
3592 };
3593 FOR_ALL_POPULATED_CHANNELS {
3594 MCHBAR32(0x4020 + 0x400 * channel) &= ~0x10000000;
3595 write32(DEFAULT_MCHBAR + 0x4034, seeds[channel][0]);
3596 write32(DEFAULT_MCHBAR + 0x403c, seeds[channel][1]);
3597 write32(DEFAULT_MCHBAR + 0x4038, seeds[channel][2]);
3598 }
3599}
3600
3601static void set_4f8c(void)
3602{
3603 struct cpuid_result cpures;
3604 u32 cpu;
3605
3606 cpures = cpuid(0);
3607 cpu = (cpures.eax);
3608 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
3609 MCHBAR32(0x4f8c) = 0x141D1519;
3610 } else {
3611 MCHBAR32(0x4f8c) = 0x551D1519;
3612 }
3613}
3614
3615static void prepare_training(ramctr_timing * ctrl)
3616{
3617 int channel;
3618
3619 FOR_ALL_POPULATED_CHANNELS {
3620 // Always drive command bus
3621 MCHBAR32(0x4004 + 0x400 * channel) |= 0x20000000;
3622 }
3623
3624 udelay(1);
3625
3626 FOR_ALL_POPULATED_CHANNELS {
3627 wait_428c(channel);
3628 }
3629}
3630
3631static void set_4008c(ramctr_timing * ctrl)
3632{
3633 int channel, slotrank;
3634 u32 reg;
3635 FOR_ALL_POPULATED_CHANNELS {
3636 u32 b20, b4_8_12;
3637 int min_320c = 10000;
3638 int max_320c = -10000;
3639
3640 FOR_ALL_POPULATED_RANKS {
3641 max_320c = max(ctrl->timings[channel][slotrank].val_320c, max_320c);
3642 min_320c = min(ctrl->timings[channel][slotrank].val_320c, min_320c);
3643 }
3644
3645 if (max_320c - min_320c > 51)
3646 b20 = 0;
3647 else
3648 b20 = ctrl->ref_card_offset[channel];
3649
3650 if (ctrl->reg_320c_range_threshold < max_320c - min_320c)
3651 b4_8_12 = 0x3330;
3652 else
3653 b4_8_12 = 0x2220;
3654
3655 reg = read32(DEFAULT_MCHBAR + 0x400c + (channel << 10));
3656 write32(DEFAULT_MCHBAR + 0x400c + (channel << 10),
3657 (reg & 0xFFF0FFFF)
3658 | (ctrl->ref_card_offset[channel] << 16)
3659 | (ctrl->ref_card_offset[channel] << 18));
3660 write32(DEFAULT_MCHBAR + 0x4008 + (channel << 10),
3661 0x0a000000
3662 | (b20 << 20)
3663 | ((ctrl->ref_card_offset[channel] + 2) << 16)
3664 | b4_8_12);
3665 }
3666}
3667
3668static void set_42a0(ramctr_timing * ctrl)
3669{
3670 int channel;
3671 FOR_ALL_POPULATED_CHANNELS {
3672 write32(DEFAULT_MCHBAR + (0x42a0 + 0x400 * channel),
3673 0x00001000 | ctrl->rankmap[channel]);
3674 MCHBAR32(0x4004 + 0x400 * channel) &= ~0x20000000; // OK
3675 }
3676}
3677
3678static int encode_5d10(int ns)
3679{
3680 return (ns + 499) / 500;
3681}
3682
3683/* FIXME: values in this function should be hardware revision-dependent. */
3684static void final_registers(ramctr_timing * ctrl)
3685{
3686 int channel;
3687 int t1_cycles = 0, t1_ns = 0, t2_ns;
3688 int t3_ns;
3689 u32 r32;
3690
3691 write32(DEFAULT_MCHBAR + 0x4cd4, 0x00000046);
3692
3693 write32(DEFAULT_MCHBAR + 0x400c, (read32(DEFAULT_MCHBAR + 0x400c) & 0xFFFFCFFF) | 0x1000); // OK
3694 write32(DEFAULT_MCHBAR + 0x440c, (read32(DEFAULT_MCHBAR + 0x440c) & 0xFFFFCFFF) | 0x1000); // OK
3695 write32(DEFAULT_MCHBAR + 0x4cb0, 0x00000740);
3696 write32(DEFAULT_MCHBAR + 0x4380, 0x00000aaa); // OK
3697 write32(DEFAULT_MCHBAR + 0x4780, 0x00000aaa); // OK
3698 write32(DEFAULT_MCHBAR + 0x4f88, 0x5f7003ff); // OK
3699 write32(DEFAULT_MCHBAR + 0x5064, 0x00073000 | ctrl->reg_5064b0); // OK
3700
3701 FOR_ALL_CHANNELS {
3702 switch (ctrl->rankmap[channel]) {
3703 /* Unpopulated channel. */
3704 case 0:
3705 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0);
3706 break;
3707 /* Only single-ranked dimms. */
3708 case 1:
3709 case 4:
3710 case 5:
3711 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x373131);
3712 break;
3713 /* Dual-ranked dimms present. */
3714 default:
3715 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x9b6ea1);
3716 break;
3717 }
3718 }
3719
3720 write32 (DEFAULT_MCHBAR + 0x5880, 0xca9171e5);
3721 write32 (DEFAULT_MCHBAR + 0x5888,
3722 (read32 (DEFAULT_MCHBAR + 0x5888) & ~0xffffff) | 0xe4d5d0);
3723 write32 (DEFAULT_MCHBAR + 0x58a8, read32 (DEFAULT_MCHBAR + 0x58a8) & ~0x1f);
3724 write32 (DEFAULT_MCHBAR + 0x4294,
3725 (read32 (DEFAULT_MCHBAR + 0x4294) & ~0x30000)
3726 | (1 << 16));
3727 write32 (DEFAULT_MCHBAR + 0x4694,
3728 (read32 (DEFAULT_MCHBAR + 0x4694) & ~0x30000)
3729 | (1 << 16));
3730
3731 MCHBAR32(0x5030) |= 1; // OK
3732 MCHBAR32(0x5030) |= 0x80; // OK
3733 MCHBAR32(0x5f18) = 0xfa; // OK
3734
3735 /* Find a populated channel. */
3736 FOR_ALL_POPULATED_CHANNELS
3737 break;
3738
3739 t1_cycles = ((read32(DEFAULT_MCHBAR + 0x4290 + channel * 0x400) >> 8) & 0xff);
3740 r32 = read32(DEFAULT_MCHBAR + 0x5064);
3741 if (r32 & 0x20000)
3742 t1_cycles += (r32 & 0xfff);
3743 t1_cycles += (read32(DEFAULT_MCHBAR + channel * 0x400 + 0x42a4) & 0xfff);
3744 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
3745 if (!(r32 & 0x20000))
3746 t1_ns += 500;
3747
3748 t2_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f10) >> 8) & 0xfff);
3749 if ( read32(DEFAULT_MCHBAR + 0x5f00) & 8 )
3750 {
3751 t3_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f20) >> 8) & 0xfff);
3752 t3_ns += 10 * (read32(DEFAULT_MCHBAR + 0x5f18) & 0xff);
3753 }
3754 else
3755 {
3756 t3_ns = 500;
3757 }
3758 printk(BIOS_DEBUG, "t123: %d, %d, %d\n",
3759 t1_ns, t2_ns, t3_ns);
3760 write32 (DEFAULT_MCHBAR + 0x5d10,
3761 ((encode_5d10(t1_ns) + encode_5d10(t2_ns)) << 16)
3762 | (encode_5d10(t1_ns) << 8)
3763 | ((encode_5d10(t3_ns) + encode_5d10(t2_ns) + encode_5d10(t1_ns)) << 24)
3764 | (read32(DEFAULT_MCHBAR + 0x5d10) & 0xC0C0C0C0)
3765 | 0xc);
3766}
3767
3768static void save_timings(ramctr_timing * ctrl)
3769{
3770 struct mrc_data_container *mrcdata;
3771 int output_len = ALIGN(sizeof (*ctrl), 16);
3772
3773 /* Save the MRC S3 restore data to cbmem */
3774 mrcdata = cbmem_add
3775 (CBMEM_ID_MRCDATA,
3776 output_len + sizeof(struct mrc_data_container));
3777
3778 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%u bytes)\n",
3779 ctrl, mrcdata, output_len);
3780
3781 mrcdata->mrc_signature = MRC_DATA_SIGNATURE;
3782 mrcdata->mrc_data_size = output_len;
3783 mrcdata->reserved = 0;
3784 memcpy(mrcdata->mrc_data, ctrl, sizeof (*ctrl));
3785
3786 /* Zero the unused space in aligned buffer. */
3787 if (output_len > sizeof (*ctrl))
3788 memset(mrcdata->mrc_data+sizeof (*ctrl), 0,
3789 output_len - sizeof (*ctrl));
3790
3791 mrcdata->mrc_checksum = compute_ip_checksum(mrcdata->mrc_data,
3792 mrcdata->mrc_data_size);
3793}
3794
3795static void restore_timings(ramctr_timing * ctrl)
3796{
3797 int channel, slotrank, lane;
3798
3799 FOR_ALL_POPULATED_CHANNELS
3800 MCHBAR32(0x4004 + 0x400 * channel) =
3801 ctrl->tRRD
3802 | (ctrl->tRTP << 4)
3803 | (ctrl->tCKE << 8)
3804 | (ctrl->tWTR << 12)
3805 | (ctrl->tFAW << 16)
3806 | (ctrl->tWR << 24)
3807 | (ctrl->cmd_stretch[channel] << 30);
3808
3809 udelay(1);
3810
3811 FOR_ALL_POPULATED_CHANNELS {
3812 wait_428c(channel);
3813 }
3814
3815 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3816 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
3817 + 4 * lane, 0);
3818 }
3819
3820 FOR_ALL_POPULATED_CHANNELS
3821 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
3822 read32(DEFAULT_MCHBAR + 0x4008 +
3823 0x400 * channel) | 0x8000000);
3824
3825 FOR_ALL_POPULATED_CHANNELS {
3826 udelay (1);
3827 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
3828 read32(DEFAULT_MCHBAR + 0x4020 +
3829 0x400 * channel) | 0x200000);
3830 }
3831
3832 printram("CPE\n");
3833
3834 write32(DEFAULT_MCHBAR + 0x3400, 0);
3835 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3836
3837 printram("CP5b\n");
3838
3839 FOR_ALL_POPULATED_CHANNELS {
3840 program_timings(ctrl, channel);
3841 }
3842
3843 u32 reg, addr;
3844
3845 while (!(MCHBAR32(0x5084) & 0x10000)) ;
3846 do {
3847 reg = MCHBAR32(0x428c);
3848 } while ((reg & 0x14) == 0);
3849
3850 // Set state of memory controller
3851 MCHBAR32(0x5030) = 0x116;
3852 MCHBAR32(0x4ea0) = 0;
3853
3854 // Wait 500us
3855 udelay(500);
3856
3857 FOR_ALL_CHANNELS {
3858 // Set valid rank CKE
3859 reg = 0;
3860 reg = (reg & ~0xf) | ctrl->rankmap[channel];
3861 addr = 0x400 * channel + 0x42a0;
3862 MCHBAR32(addr) = reg;
3863
3864 // Wait 10ns for ranks to settle
3865 //udelay(0.01);
3866
3867 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3868 MCHBAR32(addr) = reg;
3869
3870 // Write reset using a NOP
3871 write_reset(ctrl);
3872 }
3873
3874 /* mrs commands. */
3875 dram_mrscommands(ctrl);
3876
3877 printram("CP5c\n");
3878
3879 write32(DEFAULT_MCHBAR + 0x3000, 0);
3880
3881 FOR_ALL_CHANNELS {
3882 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3883 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3884 ~0x3f000000));
3885 udelay(2);
3886 }
3887
3888 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3889}
3890
3891void init_dram_ddr3(spd_raw_data * spds, int mobile, int min_tck,
3892 int s3resume)
3893{
3894 int me_uma_size;
3895 int cbmem_was_inited;
3896
3897 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +02003898
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -07003899 report_platform_info();
3900
Stefan Reinauer00636b02012-04-04 00:08:51 +02003901 /* Wait for ME to be ready */
3902 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003903 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003904
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003905 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +02003906
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003907 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +02003908
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003909 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003910
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003911 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +02003912
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003913 reg_5d10 = read32(DEFAULT_MCHBAR + 0x5d10); // !!! = 0x00000000
3914 if ((pcie_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
3915 && reg_5d10 && !s3resume) {
3916 write32(DEFAULT_MCHBAR + 0x5d10, 0);
3917 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +02003918 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003919
Patrick Georgi546953c2014-11-29 10:38:17 +01003920 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003921 }
Stefan Reinauer00636b02012-04-04 00:08:51 +02003922
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003923 ramctr_timing ctrl;
Vadim Bendebury48a4a7f2012-06-07 18:47:13 -07003924
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003925 memset(&ctrl, 0, sizeof (ctrl));
3926
3927 early_pch_init_native();
3928 early_thermal_init();
3929
3930 ctrl.mobile = mobile;
3931 ctrl.tCK = min_tck;
3932
3933 /* FIXME: for non-S3 we should be able to use timing caching with
3934 proper verification. Right now we use timings only for S3 case.
3935 */
3936 if (s3resume) {
3937 struct mrc_data_container *mrc_cache;
3938
3939 mrc_cache = find_current_mrc_cache();
3940 if (!mrc_cache || mrc_cache->mrc_data_size < sizeof (ctrl)) {
3941 /* Failed S3 resume, reset to come up cleanly */
3942 outb(0x6, 0xcf9);
3943 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02003944 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003945 memcpy(&ctrl, mrc_cache->mrc_data, sizeof (ctrl));
Stefan Reinauer00636b02012-04-04 00:08:51 +02003946 }
3947
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003948 if (!s3resume) {
3949 dimm_info info;
Sven Schnelled4ee8082012-07-28 09:28:56 +02003950
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003951 /* Get DDR3 SPD data */
3952 dram_find_spds_ddr3(spds, &info, &ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +02003953
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003954 /* Find fastest common supported parameters */
3955 dram_find_common_params(&info, &ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +02003956
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003957 dram_dimm_mapping(&info, &ctrl);
3958 }
3959
3960 /* Set MCU frequency */
3961 dram_freq(&ctrl);
3962
3963 if (!s3resume) {
3964 /* Calculate timings */
3965 dram_timing(&ctrl);
3966 }
3967
3968 /* Set version register */
3969 MCHBAR32(0x5034) = 0xC04EB002;
3970
3971 /* Enable crossover */
3972 dram_xover(&ctrl);
3973
3974 /* Set timing and refresh registers */
3975 dram_timing_regs(&ctrl);
3976
3977 /* Power mode preset */
3978 MCHBAR32(0x4e80) = 0x5500;
3979
3980 /* Set scheduler parameters */
3981 MCHBAR32(0x4c20) = 0x10100005;
3982
3983 /* Set cpu specific register */
3984 set_4f8c();
3985
3986 /* Clear IO reset bit */
3987 MCHBAR32(0x5030) &= ~0x20;
3988
3989 /* Set MAD-DIMM registers */
3990 dram_dimm_set_mapping(&ctrl);
3991 printk(BIOS_DEBUG, "Done dimm mapping\n");
3992
3993 /* Zone config */
3994 dram_zones(&ctrl, 1);
3995
3996 /* Set memory map */
3997 dram_memorymap(&ctrl, me_uma_size);
3998 printk(BIOS_DEBUG, "Done memory map\n");
3999
4000 /* Set IO registers */
4001 dram_ioregs(&ctrl);
4002 printk(BIOS_DEBUG, "Done io registers\n");
4003
4004 udelay(1);
4005
4006 if (s3resume) {
4007 restore_timings(&ctrl);
4008 } else {
4009 /* Do jedec ddr3 reset sequence */
4010 dram_jedecreset(&ctrl);
4011 printk(BIOS_DEBUG, "Done jedec reset\n");
4012
4013 /* MRS commands */
4014 dram_mrscommands(&ctrl);
4015 printk(BIOS_DEBUG, "Done MRS commands\n");
4016 dram_mrscommands(&ctrl);
4017
4018 /* Prepare for memory training */
4019 prepare_training(&ctrl);
4020
4021 read_training(&ctrl);
4022 write_training(&ctrl);
4023
4024 printram("CP5a\n");
4025
4026 discover_edges(&ctrl);
4027
4028 printram("CP5b\n");
4029
4030 command_training(&ctrl);
4031
4032 printram("CP5c\n");
4033
4034 discover_edges_write(&ctrl);
4035
4036 discover_timC_write(&ctrl);
4037
4038 normalize_training(&ctrl);
4039 }
4040
4041 set_4008c(&ctrl);
4042
4043 write_controller_mr(&ctrl);
4044
4045 if (!s3resume) {
4046 channel_test(&ctrl);
4047 }
4048
4049 /* FIXME: should be hardware revision-dependent. */
4050 write32(DEFAULT_MCHBAR + 0x5024, 0x00a030ce);
4051
4052 set_scrambling_seed(&ctrl);
4053
4054 set_42a0(&ctrl);
4055
4056 final_registers(&ctrl);
4057
4058 /* Zone config */
4059 dram_zones(&ctrl, 0);
4060
4061 if (!s3resume)
4062 quick_ram_check();
4063
4064 intel_early_me_status();
4065 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
4066 intel_early_me_status();
4067
Stefan Reinauer00636b02012-04-04 00:08:51 +02004068 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004069
4070 cbmem_was_inited = !cbmem_recovery(s3resume);
4071 if (!s3resume)
4072 save_timings(&ctrl);
4073 if (s3resume && !cbmem_was_inited) {
4074 /* Failed S3 resume, reset to come up cleanly */
4075 outb(0x6, 0xcf9);
4076 halt();
4077 }
Stefan Reinauer00636b02012-04-04 00:08:51 +02004078}