blob: e8ad4169ccfaf1826c888490b2236dda96cf54d2 [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>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010027#include <timestamp.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020028#include <pc80/mc146818rtc.h>
Alexander Couzens81c5c762016-03-09 03:13:45 +010029#include <northbridge/intel/common/mrc_cache.h>
Duncan Laurie7b508dd2012-04-09 12:30:43 -070030#include <device/pci_def.h>
Patrick Rudolphb97009e2016-02-28 15:24:04 +010031#include <memory_info.h>
32#include <smbios.h>
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070033#include "raminit_native.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020034#include "sandybridge.h"
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070035#include <delay.h>
36#include <lib.h>
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010037#include <device/device.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020038
39/* Management Engine is in the southbridge */
40#include "southbridge/intel/bd82x6x/me.h"
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070041/* For SPD. */
42#include "southbridge/intel/bd82x6x/smbus.h"
43#include "arch/cpu.h"
44#include "cpu/x86/msr.h"
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +010045#include <northbridge/intel/sandybridge/chip.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020046
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070047/* FIXME: no ECC support. */
48/* FIXME: no support for 3-channel chipsets. */
Stefan Reinauer00636b02012-04-04 00:08:51 +020049
Patrick Rudolph371d2912015-10-09 13:33:25 +020050/*
51 * Register description:
52 * Intel provides a command queue of depth four.
53 * Every command is configured by using multiple registers.
54 * On executing the command queue you have to provide the depth used.
55 *
56 * Known registers:
57 * Channel X = [0, 1]
58 * Command queue index Y = [0, 1, 2, 3]
59 *
60 * DEFAULT_MCHBAR + 0x4220 + 0x400 * X + 4 * Y: command io register
61 * Controls the DRAM command signals
62 * Bit 0: !RAS
63 * Bit 1: !CAS
64 * Bit 2: !WE
65 *
66 * DEFAULT_MCHBAR + 0x4200 + 0x400 * X + 4 * Y: addr bankslot io register
67 * Controls the address, bank address and slotrank signals
68 * Bit 0-15 : Address
69 * Bit 20-22: Bank Address
70 * Bit 24-25: slotrank
71 *
72 * DEFAULT_MCHBAR + 0x4230 + 0x400 * X + 4 * Y: idle register
73 * Controls the idle time after issuing this DRAM command
Martin Roth128c1042016-11-18 09:29:03 -070074 * Bit 16-32: number of clock-cycles to idle
Patrick Rudolph371d2912015-10-09 13:33:25 +020075 *
76 * DEFAULT_MCHBAR + 0x4284 + 0x400 * channel: execute command queue
77 * Starts to execute all queued commands
78 * Bit 0 : start DRAM command execution
79 * Bit 16-20: (number of queued commands - 1) * 4
80 */
81
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070082#define BASEFREQ 133
83#define tDLLK 512
Stefan Reinauer00636b02012-04-04 00:08:51 +020084
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070085#define IS_SANDY_CPU(x) ((x & 0xffff0) == 0x206a0)
86#define IS_SANDY_CPU_C(x) ((x & 0xf) == 4)
87#define IS_SANDY_CPU_D0(x) ((x & 0xf) == 5)
88#define IS_SANDY_CPU_D1(x) ((x & 0xf) == 6)
89#define IS_SANDY_CPU_D2(x) ((x & 0xf) == 7)
Stefan Reinauer00636b02012-04-04 00:08:51 +020090
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070091#define IS_IVY_CPU(x) ((x & 0xffff0) == 0x306a0)
92#define IS_IVY_CPU_C(x) ((x & 0xf) == 4)
93#define IS_IVY_CPU_K(x) ((x & 0xf) == 5)
94#define IS_IVY_CPU_D(x) ((x & 0xf) == 6)
95#define IS_IVY_CPU_E(x) ((x & 0xf) >= 8)
Stefan Reinauer00636b02012-04-04 00:08:51 +020096
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -070097#define NUM_CHANNELS 2
98#define NUM_SLOTRANKS 4
99#define NUM_SLOTS 2
100#define NUM_LANES 8
Stefan Reinauer00636b02012-04-04 00:08:51 +0200101
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700102/* FIXME: Vendor BIOS uses 64 but our algorithms are less
103 performant and even 1 seems to be enough in practice. */
104#define NUM_PATTERNS 4
Stefan Reinauer00636b02012-04-04 00:08:51 +0200105
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700106typedef struct odtmap_st {
107 u16 rttwr;
108 u16 rttnom;
109} odtmap;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200110
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700111typedef struct dimm_info_st {
112 dimm_attr dimm[NUM_CHANNELS][NUM_SLOTS];
113} dimm_info;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200114
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700115struct ram_rank_timings {
116 /* Register 4024. One byte per slotrank. */
117 u8 val_4024;
118 /* Register 4028. One nibble per slotrank. */
119 u8 val_4028;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200120
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700121 int val_320c;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200122
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700123 struct ram_lane_timings {
124 /* lane register offset 0x10. */
125 u16 timA; /* bits 0 - 5, bits 16 - 18 */
126 u8 rising; /* bits 8 - 14 */
127 u8 falling; /* bits 20 - 26. */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200128
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700129 /* lane register offset 0x20. */
130 int timC; /* bit 0 - 5, 19. */
131 u16 timB; /* bits 8 - 13, 15 - 17. */
132 } lanes[NUM_LANES];
133};
Stefan Reinauer00636b02012-04-04 00:08:51 +0200134
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700135struct ramctr_timing_st;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200136
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700137typedef struct ramctr_timing_st {
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100138 u16 spd_crc[NUM_CHANNELS][NUM_SLOTS];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700139 int mobile;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200140
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700141 u16 cas_supported;
142 /* tLatencies are in units of ns, scaled by x256 */
143 u32 tCK;
144 u32 tAA;
145 u32 tWR;
146 u32 tRCD;
147 u32 tRRD;
148 u32 tRP;
149 u32 tRAS;
150 u32 tRFC;
151 u32 tWTR;
152 u32 tRTP;
153 u32 tFAW;
154 /* Latencies in terms of clock cycles
155 * They are saved separately as they are needed for DRAM MRS commands*/
156 u8 CAS; /* CAS read latency */
157 u8 CWL; /* CAS write latency */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200158
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700159 u32 tREFI;
160 u32 tMOD;
161 u32 tXSOffset;
162 u32 tWLO;
163 u32 tCKE;
164 u32 tXPDLL;
165 u32 tXP;
166 u32 tAONPD;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200167
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700168 u16 reg_5064b0; /* bits 0-11. */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200169
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700170 u8 rankmap[NUM_CHANNELS];
171 int ref_card_offset[NUM_CHANNELS];
172 u32 mad_dimm[NUM_CHANNELS];
173 int channel_size_mb[NUM_CHANNELS];
174 u32 cmd_stretch[NUM_CHANNELS];
Stefan Reinauer00636b02012-04-04 00:08:51 +0200175
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700176 int reg_c14_offset;
177 int reg_320c_range_threshold;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200178
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700179 int edge_offset[3];
180 int timC_offset[3];
Stefan Reinauer00636b02012-04-04 00:08:51 +0200181
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700182 int extended_temperature_range;
183 int auto_self_refresh;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200184
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700185 int rank_mirror[NUM_CHANNELS][NUM_SLOTRANKS];
186
187 struct ram_rank_timings timings[NUM_CHANNELS][NUM_SLOTRANKS];
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100188
189 dimm_info info;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700190} ramctr_timing;
191
192#define SOUTHBRIDGE PCI_DEV(0, 0x1f, 0)
193#define NORTHBRIDGE PCI_DEV(0, 0x0, 0)
194#define FOR_ALL_LANES for (lane = 0; lane < NUM_LANES; lane++)
195#define FOR_ALL_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++)
196#define FOR_ALL_POPULATED_RANKS for (slotrank = 0; slotrank < NUM_SLOTRANKS; slotrank++) if (ctrl->rankmap[channel] & (1 << slotrank))
197#define FOR_ALL_POPULATED_CHANNELS for (channel = 0; channel < NUM_CHANNELS; channel++) if (ctrl->rankmap[channel])
198#define MAX_EDGE_TIMING 71
199#define MAX_TIMC 127
200#define MAX_TIMB 511
201#define MAX_TIMA 127
202
Patrick Rudolph24a845b2016-03-25 18:19:47 +0100203#define MAKE_ERR ((channel<<16)|(slotrank<<8)|1)
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100204#define GET_ERR_CHANNEL(x) (x>>16)
Patrick Rudolph24a845b2016-03-25 18:19:47 +0100205
Patrick Rudolph069018d2016-11-12 11:43:59 +0100206#define MC_BIOS_REQ 0x5e00
207#define MC_BIOS_DATA 0x5e04
208
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700209static void program_timings(ramctr_timing * ctrl, int channel);
Patrick Rudolph266a1f72016-06-09 18:13:34 +0200210static unsigned int get_mmio_size(void);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700211
212static const char *ecc_decoder[] = {
Stefan Reinauer00636b02012-04-04 00:08:51 +0200213 "inactive",
214 "active on IO",
215 "disabled on IO",
216 "active"
217};
218
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700219static void wait_txt_clear(void)
220{
221 struct cpuid_result cp;
222
223 cp = cpuid_ext(0x1, 0x0);
224 /* Check if TXT is supported? */
225 if (!(cp.ecx & 0x40))
226 return;
227 /* Some TXT public bit. */
228 if (!(read32((void *)0xfed30010) & 1))
229 return;
230 /* Wait for TXT clear. */
Elyes HAOUAS7db506c2016-10-02 11:56:39 +0200231 while (!(read8((void *)0xfed40000) & (1 << 7)));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700232}
233
234static void sfence(void)
235{
236 asm volatile ("sfence");
237}
238
Patrick Rudolph9b515682015-10-09 13:43:51 +0200239static void toggle_io_reset(void) {
240 /* toggle IO reset bit */
241 u32 r32 = read32(DEFAULT_MCHBAR + 0x5030);
242 write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20);
243 udelay(1);
244 write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20);
245 udelay(1);
246}
247
Stefan Reinauer00636b02012-04-04 00:08:51 +0200248/*
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100249 * Disable a channel in ramctr_timing.
250 */
251static void disable_channel(ramctr_timing *ctrl, int channel) {
252 ctrl->rankmap[channel] = 0;
253 memset(&ctrl->rank_mirror[channel][0], 0, sizeof(ctrl->rank_mirror[0]));
254 ctrl->channel_size_mb[channel] = 0;
255 ctrl->cmd_stretch[channel] = 0;
256 ctrl->mad_dimm[channel] = 0;
257 memset(&ctrl->timings[channel][0], 0, sizeof(ctrl->timings[0]));
Patrick Rudolph74163d62016-11-17 20:02:43 +0100258 memset(&ctrl->info.dimm[channel][0], 0, sizeof(ctrl->info.dimm[0]));
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +0100259}
260
261/*
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100262 * Fill cbmem with information for SMBIOS type 17.
263 */
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100264static void fill_smbios17(ramctr_timing *ctrl)
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100265{
266 struct memory_info *mem_info;
267 int channel, slot;
268 struct dimm_info *dimm;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100269 uint16_t ddr_freq;
270 dimm_info *info = &ctrl->info;
271
272 ddr_freq = (1000 << 8) / ctrl->tCK;
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100273
274 /*
275 * Allocate CBMEM area for DIMM information used to populate SMBIOS
276 * table 17
277 */
278 mem_info = cbmem_add(CBMEM_ID_MEMINFO, sizeof(*mem_info));
279 printk(BIOS_DEBUG, "CBMEM entry for DIMM info: 0x%p\n", mem_info);
280 if (!mem_info)
281 return;
282
283 memset(mem_info, 0, sizeof(*mem_info));
284
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200285 FOR_ALL_CHANNELS for (slot = 0; slot < NUM_SLOTS; slot++) {
Patrick Rudolphb97009e2016-02-28 15:24:04 +0100286 dimm = &mem_info->dimm[mem_info->dimm_cnt];
287 if (info->dimm[channel][slot].size_mb) {
288 dimm->ddr_type = MEMORY_TYPE_DDR3;
289 dimm->ddr_frequency = ddr_freq;
290 dimm->dimm_size = info->dimm[channel][slot].size_mb;
291 dimm->channel_num = channel;
292 dimm->rank_per_dimm = info->dimm[channel][slot].ranks;
293 dimm->dimm_num = slot;
294 memcpy(dimm->module_part_number,
295 info->dimm[channel][slot].part_number, 16);
296 dimm->mod_id = info->dimm[channel][slot].manufacturer_id;
297 dimm->mod_type = info->dimm[channel][slot].dimm_type;
298 dimm->bus_width = info->dimm[channel][slot].width;
299 mem_info->dimm_cnt++;
300 }
301 }
302}
303
304/*
Stefan Reinauer00636b02012-04-04 00:08:51 +0200305 * Dump in the log memory controller configuration as read from the memory
306 * controller registers.
307 */
308static void report_memory_config(void)
309{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700310 u32 addr_decoder_common, addr_decode_ch[NUM_CHANNELS];
Stefan Reinauer00636b02012-04-04 00:08:51 +0200311 int i;
312
313 addr_decoder_common = MCHBAR32(0x5000);
314 addr_decode_ch[0] = MCHBAR32(0x5004);
315 addr_decode_ch[1] = MCHBAR32(0x5008);
316
317 printk(BIOS_DEBUG, "memcfg DDR3 clock %d MHz\n",
Patrick Rudolph069018d2016-11-12 11:43:59 +0100318 (MCHBAR32(MC_BIOS_DATA) * 13333 * 2 + 50) / 100);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200319 printk(BIOS_DEBUG, "memcfg channel assignment: A: %d, B % d, C % d\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700320 addr_decoder_common & 3, (addr_decoder_common >> 2) & 3,
Stefan Reinauer00636b02012-04-04 00:08:51 +0200321 (addr_decoder_common >> 4) & 3);
322
323 for (i = 0; i < ARRAY_SIZE(addr_decode_ch); i++) {
324 u32 ch_conf = addr_decode_ch[i];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700325 printk(BIOS_DEBUG, "memcfg channel[%d] config (%8.8x):\n", i,
326 ch_conf);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200327 printk(BIOS_DEBUG, " ECC %s\n",
328 ecc_decoder[(ch_conf >> 24) & 3]);
329 printk(BIOS_DEBUG, " enhanced interleave mode %s\n",
330 ((ch_conf >> 22) & 1) ? "on" : "off");
331 printk(BIOS_DEBUG, " rank interleave %s\n",
332 ((ch_conf >> 21) & 1) ? "on" : "off");
333 printk(BIOS_DEBUG, " DIMMA %d MB width x%d %s rank%s\n",
334 ((ch_conf >> 0) & 0xff) * 256,
335 ((ch_conf >> 19) & 1) ? 16 : 8,
336 ((ch_conf >> 17) & 1) ? "dual" : "single",
337 ((ch_conf >> 16) & 1) ? "" : ", selected");
338 printk(BIOS_DEBUG, " DIMMB %d MB width x%d %s rank%s\n",
339 ((ch_conf >> 8) & 0xff) * 256,
340 ((ch_conf >> 20) & 1) ? 16 : 8,
341 ((ch_conf >> 18) & 1) ? "dual" : "single",
342 ((ch_conf >> 16) & 1) ? ", selected" : "");
343 }
344}
345
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100346/*
347 * Return CRC16 match for all SPDs.
348 */
349static int verify_crc16_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
350{
351 int channel, slot, spd_slot;
352 int match = 1;
353
354 FOR_ALL_CHANNELS {
355 for (slot = 0; slot < NUM_SLOTS; slot++) {
356 spd_slot = 2 * channel + slot;
357 match &= ctrl->spd_crc[channel][slot] ==
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200358 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100359 }
360 }
361 return match;
362}
363
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200364void read_spd(spd_raw_data * spd, u8 addr, bool id_only)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200365{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700366 int j;
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +0200367 if (id_only) {
368 for (j = 117; j < 128; j++)
369 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
370 } else {
371 for (j = 0; j < 256; j++)
372 (*spd)[j] = do_smbus_read_byte(SMBUS_IO_BASE, addr, j);
373 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700374}
375
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100376static void dram_find_spds_ddr3(spd_raw_data *spd, ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700377{
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100378 int dimms = 0, dimms_on_channel;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700379 int channel, slot, spd_slot;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100380 dimm_info *dimm = &ctrl->info;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700381
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +0200382 memset (ctrl->rankmap, 0, sizeof(ctrl->rankmap));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700383
384 ctrl->extended_temperature_range = 1;
385 ctrl->auto_self_refresh = 1;
386
387 FOR_ALL_CHANNELS {
388 ctrl->channel_size_mb[channel] = 0;
389
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100390 dimms_on_channel = 0;
391 /* count dimms on channel */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700392 for (slot = 0; slot < NUM_SLOTS; slot++) {
393 spd_slot = 2 * channel + slot;
394 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
Patrick Rudolphbd1fdc62016-01-26 08:45:21 +0100395 if (dimm->dimm[channel][slot].dram_type == SPD_MEMORY_TYPE_SDRAM_DDR3)
396 dimms_on_channel++;
397 }
398
399 for (slot = 0; slot < NUM_SLOTS; slot++) {
400 spd_slot = 2 * channel + slot;
401 /* search for XMP profile */
402 spd_xmp_decode_ddr3(&dimm->dimm[channel][slot],
403 spd[spd_slot],
404 DDR3_XMP_PROFILE_1);
405
406 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
407 printram("No valid XMP profile found.\n");
408 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
409 } else if (dimms_on_channel > dimm->dimm[channel][slot].dimms_per_channel) {
410 printram("XMP profile supports %u DIMMs, but %u DIMMs are installed.\n",
411 dimm->dimm[channel][slot].dimms_per_channel,
412 dimms_on_channel);
413 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
414 } else if (dimm->dimm[channel][slot].voltage != 1500) {
415 /* TODO: support other DDR3 voltage than 1500mV */
416 printram("XMP profile's requested %u mV is unsupported.\n",
417 dimm->dimm[channel][slot].voltage);
418 spd_decode_ddr3(&dimm->dimm[channel][slot], spd[spd_slot]);
419 }
420
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100421 /* fill in CRC16 for MRC cache */
422 ctrl->spd_crc[channel][slot] =
Kyösti Mälkkifc5d85c2016-11-18 18:52:04 +0200423 spd_ddr3_calc_unique_crc(spd[spd_slot], sizeof(spd_raw_data));
Patrick Rudolph56abd4d2016-03-13 11:07:45 +0100424
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700425 if (dimm->dimm[channel][slot].dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3) {
426 // set dimm invalid
427 dimm->dimm[channel][slot].ranks = 0;
428 dimm->dimm[channel][slot].size_mb = 0;
429 continue;
430 }
431
432 dram_print_spd_ddr3(&dimm->dimm[channel][slot]);
433 dimms++;
434 ctrl->rank_mirror[channel][slot * 2] = 0;
435 ctrl->rank_mirror[channel][slot * 2 + 1] = dimm->dimm[channel][slot].flags.pins_mirrored;
436 ctrl->channel_size_mb[channel] += dimm->dimm[channel][slot].size_mb;
437
438 ctrl->auto_self_refresh &= dimm->dimm[channel][slot].flags.asr;
439 ctrl->extended_temperature_range &= dimm->dimm[channel][slot].flags.ext_temp_refresh;
440
441 ctrl->rankmap[channel] |= ((1 << dimm->dimm[channel][slot].ranks) - 1) << (2 * slot);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100442 printk(BIOS_DEBUG, "channel[%d] rankmap = 0x%x\n",
443 channel, ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700444 }
445 if ((ctrl->rankmap[channel] & 3) && (ctrl->rankmap[channel] & 0xc)
446 && dimm->dimm[channel][0].reference_card <= 5 && dimm->dimm[channel][1].reference_card <= 5) {
447 const int ref_card_offset_table[6][6] = {
448 { 0, 0, 0, 0, 2, 2, },
449 { 0, 0, 0, 0, 2, 2, },
450 { 0, 0, 0, 0, 2, 2, },
451 { 0, 0, 0, 0, 1, 1, },
452 { 2, 2, 2, 1, 0, 0, },
453 { 2, 2, 2, 1, 0, 0, },
454 };
455 ctrl->ref_card_offset[channel] = ref_card_offset_table[dimm->dimm[channel][0].reference_card]
456 [dimm->dimm[channel][1].reference_card];
457 } else
458 ctrl->ref_card_offset[channel] = 0;
459 }
460
461 if (!dimms)
462 die("No DIMMs were found");
463}
464
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100465static void dram_find_common_params(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700466{
467 size_t valid_dimms;
468 int channel, slot;
Patrick Rudolph735ecce2016-03-26 10:42:27 +0100469 dimm_info *dimms = &ctrl->info;
470
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700471 ctrl->cas_supported = 0xff;
472 valid_dimms = 0;
473 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
474 const dimm_attr *dimm = &dimms->dimm[channel][slot];
475 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
476 continue;
477 valid_dimms++;
478
479 /* Find all possible CAS combinations */
480 ctrl->cas_supported &= dimm->cas_supported;
481
482 /* Find the smallest common latencies supported by all DIMMs */
483 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
484 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
485 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
486 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
487 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
488 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
489 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
490 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
491 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
492 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
493 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
494 }
495
496 if (!ctrl->cas_supported)
497 die("Unsupported DIMM combination. "
498 "DIMMS do not support common CAS latency");
499 if (!valid_dimms)
500 die("No valid DIMMs found");
501}
502
Patrick Rudolphbec66962016-11-11 19:17:56 +0100503/* CAS write latency. To be programmed in MR2.
504 * See DDR3 SPEC for MR2 documentation. */
505static u8 get_CWL(u32 tCK)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700506{
Patrick Rudolphbec66962016-11-11 19:17:56 +0100507 /* Get CWL based on tCK using the following rule: */
508 switch (tCK) {
509 case TCK_1333MHZ:
510 return 12;
511 case TCK_1200MHZ:
512 case TCK_1100MHZ:
513 return 11;
514 case TCK_1066MHZ:
515 case TCK_1000MHZ:
516 return 10;
517 case TCK_933MHZ:
518 case TCK_900MHZ:
519 return 9;
520 case TCK_800MHZ:
521 case TCK_700MHZ:
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700522 return 8;
Patrick Rudolphbec66962016-11-11 19:17:56 +0100523 case TCK_666MHZ:
524 return 7;
525 case TCK_533MHZ:
526 return 6;
527 default:
528 return 5;
529 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700530}
531
532/* Frequency multiplier. */
533static u32 get_FRQ(u32 tCK)
534{
535 u32 FRQ;
536 FRQ = 256000 / (tCK * BASEFREQ);
537 if (FRQ > 8)
538 return 8;
539 if (FRQ < 3)
540 return 3;
541 return FRQ;
542}
543
544static u32 get_REFI(u32 tCK)
545{
546 /* Get REFI based on MCU frequency using the following rule:
547 * _________________________________________
548 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
549 * REFI: | 3120 | 4160 | 5200 | 6240 | 7280 | 8320 |
550 */
551 static const u32 frq_refi_map[] =
552 { 3120, 4160, 5200, 6240, 7280, 8320 };
553 return frq_refi_map[get_FRQ(tCK) - 3];
554}
555
556static u8 get_XSOffset(u32 tCK)
557{
558 /* Get XSOffset based on MCU frequency using the following rule:
559 * _________________________
560 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
561 * XSOffset : | 4 | 6 | 7 | 8 | 10 | 11 |
562 */
563 static const u8 frq_xs_map[] = { 4, 6, 7, 8, 10, 11 };
564 return frq_xs_map[get_FRQ(tCK) - 3];
565}
566
567static u8 get_MOD(u32 tCK)
568{
569 /* Get MOD based on MCU frequency using the following rule:
570 * _____________________________
571 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
572 * MOD : | 12 | 12 | 12 | 12 | 15 | 16 |
573 */
574 static const u8 frq_mod_map[] = { 12, 12, 12, 12, 15, 16 };
575 return frq_mod_map[get_FRQ(tCK) - 3];
576}
577
578static u8 get_WLO(u32 tCK)
579{
580 /* Get WLO based on MCU frequency using the following rule:
581 * _______________________
582 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
583 * WLO : | 4 | 5 | 6 | 6 | 8 | 8 |
584 */
585 static const u8 frq_wlo_map[] = { 4, 5, 6, 6, 8, 8 };
586 return frq_wlo_map[get_FRQ(tCK) - 3];
587}
588
589static u8 get_CKE(u32 tCK)
590{
591 /* Get CKE based on MCU frequency using the following rule:
592 * _______________________
593 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
594 * CKE : | 3 | 3 | 4 | 4 | 5 | 6 |
595 */
596 static const u8 frq_cke_map[] = { 3, 3, 4, 4, 5, 6 };
597 return frq_cke_map[get_FRQ(tCK) - 3];
598}
599
600static u8 get_XPDLL(u32 tCK)
601{
602 /* Get XPDLL based on MCU frequency using the following rule:
603 * _____________________________
604 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
605 * XPDLL : | 10 | 13 | 16 | 20 | 23 | 26 |
606 */
607 static const u8 frq_xpdll_map[] = { 10, 13, 16, 20, 23, 26 };
608 return frq_xpdll_map[get_FRQ(tCK) - 3];
609}
610
611static u8 get_XP(u32 tCK)
612{
613 /* Get XP based on MCU frequency using the following rule:
614 * _______________________
615 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
616 * XP : | 3 | 4 | 4 | 5 | 6 | 7 |
617 */
618 static const u8 frq_xp_map[] = { 3, 4, 4, 5, 6, 7 };
619 return frq_xp_map[get_FRQ(tCK) - 3];
620}
621
622static u8 get_AONPD(u32 tCK)
623{
624 /* Get AONPD based on MCU frequency using the following rule:
625 * ________________________
626 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
627 * AONPD : | 4 | 5 | 6 | 8 | 8 | 10 |
628 */
629 static const u8 frq_aonpd_map[] = { 4, 5, 6, 8, 8, 10 };
630 return frq_aonpd_map[get_FRQ(tCK) - 3];
631}
632
633static u32 get_COMP2(u32 tCK)
634{
635 /* Get COMP2 based on MCU frequency using the following rule:
636 * ___________________________________________________________
637 * FRQ : | 3 | 4 | 5 | 6 | 7 | 8 |
638 * COMP : | D6BEDCC | CE7C34C | CA57A4C | C6369CC | C42514C | C21410C |
639 */
640 static const u32 frq_comp2_map[] = { 0xD6BEDCC, 0xCE7C34C, 0xCA57A4C,
641 0xC6369CC, 0xC42514C, 0xC21410C
642 };
643 return frq_comp2_map[get_FRQ(tCK) - 3];
644}
645
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +0100646static u32 get_XOVER_CLK(u8 rankmap)
647{
648 return rankmap << 24;
649}
650
651static u32 get_XOVER_CMD(u8 rankmap)
652{
653 u32 reg;
654
655 // enable xover cmd
656 reg = 0x4000;
657
658 // enable xover ctl
659 if (rankmap & 0x3)
660 reg |= 0x20000;
661
662 if (rankmap & 0xc)
663 reg |= 0x4000000;
664
665 return reg;
666}
667
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700668static void dram_timing(ramctr_timing * ctrl)
669{
670 u8 val;
671 u32 val32;
672
673 /* Maximum supported DDR3 frequency is 1066MHz (DDR3 2133) so make sure
674 * we cap it if we have faster DIMMs.
675 * Then, align it to the closest JEDEC standard frequency */
676 if (ctrl->tCK <= TCK_1066MHZ) {
677 ctrl->tCK = TCK_1066MHZ;
678 ctrl->edge_offset[0] = 16;
679 ctrl->edge_offset[1] = 7;
680 ctrl->edge_offset[2] = 7;
681 ctrl->timC_offset[0] = 18;
682 ctrl->timC_offset[1] = 7;
683 ctrl->timC_offset[2] = 7;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700684 ctrl->reg_320c_range_threshold = 13;
685 } else if (ctrl->tCK <= TCK_933MHZ) {
686 ctrl->tCK = TCK_933MHZ;
687 ctrl->edge_offset[0] = 14;
688 ctrl->edge_offset[1] = 6;
689 ctrl->edge_offset[2] = 6;
690 ctrl->timC_offset[0] = 15;
691 ctrl->timC_offset[1] = 6;
692 ctrl->timC_offset[2] = 6;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700693 ctrl->reg_320c_range_threshold = 15;
694 } else if (ctrl->tCK <= TCK_800MHZ) {
695 ctrl->tCK = TCK_800MHZ;
696 ctrl->edge_offset[0] = 13;
697 ctrl->edge_offset[1] = 5;
698 ctrl->edge_offset[2] = 5;
699 ctrl->timC_offset[0] = 14;
700 ctrl->timC_offset[1] = 5;
701 ctrl->timC_offset[2] = 5;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700702 ctrl->reg_320c_range_threshold = 15;
703 } else if (ctrl->tCK <= TCK_666MHZ) {
704 ctrl->tCK = TCK_666MHZ;
705 ctrl->edge_offset[0] = 10;
706 ctrl->edge_offset[1] = 4;
707 ctrl->edge_offset[2] = 4;
708 ctrl->timC_offset[0] = 11;
709 ctrl->timC_offset[1] = 4;
710 ctrl->timC_offset[2] = 4;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700711 ctrl->reg_320c_range_threshold = 16;
712 } else if (ctrl->tCK <= TCK_533MHZ) {
713 ctrl->tCK = TCK_533MHZ;
714 ctrl->edge_offset[0] = 8;
715 ctrl->edge_offset[1] = 3;
716 ctrl->edge_offset[2] = 3;
717 ctrl->timC_offset[0] = 9;
718 ctrl->timC_offset[1] = 3;
719 ctrl->timC_offset[2] = 3;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700720 ctrl->reg_320c_range_threshold = 17;
721 } else {
722 ctrl->tCK = TCK_400MHZ;
723 ctrl->edge_offset[0] = 6;
724 ctrl->edge_offset[1] = 2;
725 ctrl->edge_offset[2] = 2;
726 ctrl->timC_offset[0] = 6;
727 ctrl->timC_offset[1] = 2;
728 ctrl->timC_offset[2] = 2;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700729 ctrl->reg_320c_range_threshold = 17;
730 }
731
Patrick Rudolphd4c53e32016-06-14 20:07:32 +0200732 /* Initial phase between CLK/CMD pins */
733 ctrl->reg_c14_offset = (256000 / ctrl->tCK) / 66;
734
Patrick Rudolphb7b1b282016-06-14 18:44:28 +0200735 /* DLL_CONFIG_MDLL_W_TIMER */
736 ctrl->reg_5064b0 = (128000 / ctrl->tCK) + 3;
737
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700738 val32 = (1000 << 8) / ctrl->tCK;
739 printk(BIOS_DEBUG, "Selected DRAM frequency: %u MHz\n", val32);
740
Patrick Rudolphbec66962016-11-11 19:17:56 +0100741 /* Find CAS latency */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700742 val = (ctrl->tAA + ctrl->tCK - 1) / ctrl->tCK;
743 printk(BIOS_DEBUG, "Minimum CAS latency : %uT\n", val);
744 /* Find lowest supported CAS latency that satisfies the minimum value */
745 while (!((ctrl->cas_supported >> (val - 4)) & 1)
746 && (ctrl->cas_supported >> (val - 4))) {
747 val++;
748 }
749 /* Is CAS supported */
Patrick Rudolph55409eb2016-06-15 20:28:32 +0200750 if (!(ctrl->cas_supported & (1 << (val - 4)))) {
751 printk(BIOS_ERR, "CAS %uT not supported. ", val);
752 val = 18;
753 /* Find highest supported CAS latency */
754 while (!((ctrl->cas_supported >> (val - 4)) & 1))
755 val--;
756
757 printk(BIOS_ERR, "Using CAS %uT instead.\n", val);
758 }
759
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700760 printk(BIOS_DEBUG, "Selected CAS latency : %uT\n", val);
761 ctrl->CAS = val;
Patrick Rudolphbec66962016-11-11 19:17:56 +0100762 ctrl->CWL = get_CWL(ctrl->tCK);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700763 printk(BIOS_DEBUG, "Selected CWL latency : %uT\n", ctrl->CWL);
764
765 /* Find tRCD */
766 ctrl->tRCD = (ctrl->tRCD + ctrl->tCK - 1) / ctrl->tCK;
767 printk(BIOS_DEBUG, "Selected tRCD : %uT\n", ctrl->tRCD);
768
769 ctrl->tRP = (ctrl->tRP + ctrl->tCK - 1) / ctrl->tCK;
770 printk(BIOS_DEBUG, "Selected tRP : %uT\n", ctrl->tRP);
771
772 /* Find tRAS */
773 ctrl->tRAS = (ctrl->tRAS + ctrl->tCK - 1) / ctrl->tCK;
774 printk(BIOS_DEBUG, "Selected tRAS : %uT\n", ctrl->tRAS);
775
776 /* Find tWR */
777 ctrl->tWR = (ctrl->tWR + ctrl->tCK - 1) / ctrl->tCK;
778 printk(BIOS_DEBUG, "Selected tWR : %uT\n", ctrl->tWR);
779
780 /* Find tFAW */
781 ctrl->tFAW = (ctrl->tFAW + ctrl->tCK - 1) / ctrl->tCK;
782 printk(BIOS_DEBUG, "Selected tFAW : %uT\n", ctrl->tFAW);
783
784 /* Find tRRD */
785 ctrl->tRRD = (ctrl->tRRD + ctrl->tCK - 1) / ctrl->tCK;
786 printk(BIOS_DEBUG, "Selected tRRD : %uT\n", ctrl->tRRD);
787
788 /* Find tRTP */
789 ctrl->tRTP = (ctrl->tRTP + ctrl->tCK - 1) / ctrl->tCK;
790 printk(BIOS_DEBUG, "Selected tRTP : %uT\n", ctrl->tRTP);
791
792 /* Find tWTR */
793 ctrl->tWTR = (ctrl->tWTR + ctrl->tCK - 1) / ctrl->tCK;
794 printk(BIOS_DEBUG, "Selected tWTR : %uT\n", ctrl->tWTR);
795
796 /* Refresh-to-Active or Refresh-to-Refresh (tRFC) */
797 ctrl->tRFC = (ctrl->tRFC + ctrl->tCK - 1) / ctrl->tCK;
798 printk(BIOS_DEBUG, "Selected tRFC : %uT\n", ctrl->tRFC);
799
800 ctrl->tREFI = get_REFI(ctrl->tCK);
801 ctrl->tMOD = get_MOD(ctrl->tCK);
802 ctrl->tXSOffset = get_XSOffset(ctrl->tCK);
803 ctrl->tWLO = get_WLO(ctrl->tCK);
804 ctrl->tCKE = get_CKE(ctrl->tCK);
805 ctrl->tXPDLL = get_XPDLL(ctrl->tCK);
806 ctrl->tXP = get_XP(ctrl->tCK);
807 ctrl->tAONPD = get_AONPD(ctrl->tCK);
808}
809
810static void dram_freq(ramctr_timing * ctrl)
811{
812 if (ctrl->tCK > TCK_400MHZ) {
813 printk (BIOS_ERR, "DRAM frequency is under lowest supported frequency (400 MHz). Increasing to 400 MHz as last resort");
814 ctrl->tCK = TCK_400MHZ;
815 }
816 while (1) {
817 u8 val2;
818 u32 reg1 = 0;
819
820 /* Step 1 - Set target PCU frequency */
821
822 if (ctrl->tCK <= TCK_1066MHZ) {
823 ctrl->tCK = TCK_1066MHZ;
824 } else if (ctrl->tCK <= TCK_933MHZ) {
825 ctrl->tCK = TCK_933MHZ;
826 } else if (ctrl->tCK <= TCK_800MHZ) {
827 ctrl->tCK = TCK_800MHZ;
828 } else if (ctrl->tCK <= TCK_666MHZ) {
829 ctrl->tCK = TCK_666MHZ;
830 } else if (ctrl->tCK <= TCK_533MHZ) {
831 ctrl->tCK = TCK_533MHZ;
832 } else if (ctrl->tCK <= TCK_400MHZ) {
833 ctrl->tCK = TCK_400MHZ;
834 } else {
835 die ("No lock frequency found");
836 }
837
Martin Roth128c1042016-11-18 09:29:03 -0700838 /* Frequency multiplier. */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700839 u32 FRQ = get_FRQ(ctrl->tCK);
840
Patrick Rudolph9c9bde32016-03-26 17:20:02 +0100841 /* The PLL will never lock if the required frequency is
842 * already set. Exit early to prevent a system hang.
843 */
Patrick Rudolph069018d2016-11-12 11:43:59 +0100844 reg1 = MCHBAR32(MC_BIOS_DATA);
Patrick Rudolph9c9bde32016-03-26 17:20:02 +0100845 val2 = (u8) reg1;
Patrick Rudolphf7047542016-11-12 11:39:57 +0100846 if (val2)
Patrick Rudolph9c9bde32016-03-26 17:20:02 +0100847 return;
Patrick Rudolph9c9bde32016-03-26 17:20:02 +0100848
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700849 /* Step 2 - Select frequency in the MCU */
850 reg1 = FRQ;
851 reg1 |= 0x80000000; // set running bit
Patrick Rudolph069018d2016-11-12 11:43:59 +0100852 MCHBAR32(MC_BIOS_REQ) = reg1;
Kyösti Mälkki809b5882016-11-19 17:12:39 +0200853 int i=0;
854 printk(BIOS_DEBUG, "PLL busy... ");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700855 while (reg1 & 0x80000000) {
Kyösti Mälkki809b5882016-11-19 17:12:39 +0200856 udelay(10);
857 i++;
Patrick Rudolph069018d2016-11-12 11:43:59 +0100858 reg1 = MCHBAR32(MC_BIOS_REQ);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700859 }
Kyösti Mälkki809b5882016-11-19 17:12:39 +0200860 printk(BIOS_DEBUG, "done in %d us\n", i * 10);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700861
862 /* Step 3 - Verify lock frequency */
Patrick Rudolph069018d2016-11-12 11:43:59 +0100863 reg1 = MCHBAR32(MC_BIOS_DATA);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700864 val2 = (u8) reg1;
865 if (val2 >= FRQ) {
866 printk(BIOS_DEBUG, "MCU frequency is set at : %d MHz\n",
867 (1000 << 8) / ctrl->tCK);
868 return;
869 }
870 printk(BIOS_DEBUG, "PLL didn't lock. Retrying at lower frequency\n");
871 ctrl->tCK++;
872 }
873}
874
875static void dram_xover(ramctr_timing * ctrl)
876{
877 u32 reg;
878 int channel;
879
880 FOR_ALL_CHANNELS {
881 // enable xover clk
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +0100882 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100883 printram("XOVER CLK [%x] = %x\n", channel * 0x100 + 0xc14,
884 reg);
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +0100885 MCHBAR32(channel * 0x100 + 0xc14) = reg;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700886
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +0100887 // enable xover ctl & xover cmd
888 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100889 printram("XOVER CMD [%x] = %x\n", 0x100 * channel + 0x320c,
890 reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700891 MCHBAR32(0x100 * channel + 0x320c) = reg;
892 }
893}
894
895static void dram_timing_regs(ramctr_timing * ctrl)
896{
897 u32 reg, addr, val32, cpu, stretch;
898 struct cpuid_result cpures;
899 int channel;
900
901 FOR_ALL_CHANNELS {
902 // DBP
903 reg = 0;
904 reg |= ctrl->tRCD;
905 reg |= (ctrl->tRP << 4);
906 reg |= (ctrl->CAS << 8);
907 reg |= (ctrl->CWL << 12);
908 reg |= (ctrl->tRAS << 16);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100909 printram("DBP [%x] = %x\n", 0x400 * channel + 0x4000, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700910 MCHBAR32(0x400 * channel + 0x4000) = reg;
911
912 // RAP
913 reg = 0;
914 reg |= ctrl->tRRD;
915 reg |= (ctrl->tRTP << 4);
916 reg |= (ctrl->tCKE << 8);
917 reg |= (ctrl->tWTR << 12);
918 reg |= (ctrl->tFAW << 16);
919 reg |= (ctrl->tWR << 24);
920 reg |= (3 << 30);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100921 printram("RAP [%x] = %x\n", 0x400 * channel + 0x4004, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700922 MCHBAR32(0x400 * channel + 0x4004) = reg;
923
924 // OTHP
925 addr = 0x400 * channel + 0x400c;
926 reg = 0;
927 reg |= ctrl->tXPDLL;
928 reg |= (ctrl->tXP << 5);
929 reg |= (ctrl->tAONPD << 8);
930 reg |= 0xa0000;
Patrick Rudolpha649a542016-01-17 18:32:06 +0100931 printram("OTHP [%x] = %x\n", addr, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700932 MCHBAR32(addr) = reg;
933
934 MCHBAR32(0x400 * channel + 0x4014) = 0;
935
936 MCHBAR32(addr) |= 0x00020000;
937
938 // ODT stretch
939 reg = 0;
940
Ryan Salsamendie4da9aa2016-06-24 12:01:11 -0700941 cpures = cpuid(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700942 cpu = cpures.eax;
943 if (IS_IVY_CPU(cpu)
944 || (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_D2(cpu))) {
945 stretch = 2;
946 addr = 0x400 * channel + 0x400c;
Patrick Rudolpha649a542016-01-17 18:32:06 +0100947 printram("ODT stretch [%x] = %x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700948 0x400 * channel + 0x400c, reg);
949 reg = MCHBAR32(addr);
950
951 if (((ctrl->rankmap[channel] & 3) == 0)
952 || (ctrl->rankmap[channel] & 0xc) == 0) {
953
954 // Rank 0 - operate on rank 2
955 reg = (reg & ~0xc0000) | (stretch << 18);
956
957 // Rank 2 - operate on rank 0
958 reg = (reg & ~0x30000) | (stretch << 16);
959
Patrick Rudolpha649a542016-01-17 18:32:06 +0100960 printram("ODT stretch [%x] = %x\n", addr, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700961 MCHBAR32(addr) = reg;
962 }
963
964 } else if (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_C(cpu)) {
965 stretch = 3;
966 addr = 0x400 * channel + 0x401c;
967 reg = MCHBAR32(addr);
968
969 if (((ctrl->rankmap[channel] & 3) == 0)
970 || (ctrl->rankmap[channel] & 0xc) == 0) {
971
972 // Rank 0 - operate on rank 2
973 reg = (reg & ~0x3000) | (stretch << 12);
974
975 // Rank 2 - operate on rank 0
976 reg = (reg & ~0xc00) | (stretch << 10);
977
Patrick Rudolpha649a542016-01-17 18:32:06 +0100978 printram("ODT stretch [%x] = %x\n", addr, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700979 MCHBAR32(addr) = reg;
980 }
981 } else {
982 stretch = 0;
983 }
984
985 // REFI
986 reg = 0;
987 val32 = ctrl->tREFI;
988 reg = (reg & ~0xffff) | val32;
989 val32 = ctrl->tRFC;
990 reg = (reg & ~0x1ff0000) | (val32 << 16);
991 val32 = (u32) (ctrl->tREFI * 9) / 1024;
992 reg = (reg & ~0xfe000000) | (val32 << 25);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100993 printram("REFI [%x] = %x\n", 0x400 * channel + 0x4298,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700994 reg);
995 MCHBAR32(0x400 * channel + 0x4298) = reg;
996
997 MCHBAR32(0x400 * channel + 0x4294) |= 0xff;
998
999 // SRFTP
1000 reg = 0;
1001 val32 = tDLLK;
1002 reg = (reg & ~0xfff) | val32;
1003 val32 = ctrl->tXSOffset;
1004 reg = (reg & ~0xf000) | (val32 << 12);
1005 val32 = tDLLK - ctrl->tXSOffset;
1006 reg = (reg & ~0x3ff0000) | (val32 << 16);
1007 val32 = ctrl->tMOD - 8;
1008 reg = (reg & ~0xf0000000) | (val32 << 28);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001009 printram("SRFTP [%x] = %x\n", 0x400 * channel + 0x42a4,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001010 reg);
1011 MCHBAR32(0x400 * channel + 0x42a4) = reg;
1012 }
1013}
1014
Patrick Rudolph735ecce2016-03-26 10:42:27 +01001015static void dram_dimm_mapping(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001016{
1017 u32 reg, val32;
1018 int channel;
Patrick Rudolph735ecce2016-03-26 10:42:27 +01001019 dimm_info *info = &ctrl->info;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001020
1021 FOR_ALL_CHANNELS {
1022 dimm_attr *dimmA = 0;
1023 dimm_attr *dimmB = 0;
1024 reg = 0;
1025 val32 = 0;
1026 if (info->dimm[channel][0].size_mb >=
1027 info->dimm[channel][1].size_mb) {
1028 // dimm 0 is bigger, set it to dimmA
1029 dimmA = &info->dimm[channel][0];
1030 dimmB = &info->dimm[channel][1];
1031 reg |= (0 << 16);
1032 } else {
1033 // dimm 1 is bigger, set it to dimmA
1034 dimmA = &info->dimm[channel][1];
1035 dimmB = &info->dimm[channel][0];
1036 reg |= (1 << 16);
1037 }
1038 // dimmA
1039 if (dimmA && (dimmA->ranks > 0)) {
1040 val32 = dimmA->size_mb / 256;
1041 reg = (reg & ~0xff) | val32;
1042 val32 = dimmA->ranks - 1;
1043 reg = (reg & ~0x20000) | (val32 << 17);
1044 val32 = (dimmA->width / 8) - 1;
1045 reg = (reg & ~0x80000) | (val32 << 19);
1046 }
1047 // dimmB
1048 if (dimmB && (dimmB->ranks > 0)) {
1049 val32 = dimmB->size_mb / 256;
1050 reg = (reg & ~0xff00) | (val32 << 8);
1051 val32 = dimmB->ranks - 1;
1052 reg = (reg & ~0x40000) | (val32 << 18);
1053 val32 = (dimmB->width / 8) - 1;
1054 reg = (reg & ~0x100000) | (val32 << 20);
1055 }
1056 reg = (reg & ~0x200000) | (1 << 21); // rank interleave
1057 reg = (reg & ~0x400000) | (1 << 22); // enhanced interleave
1058
1059 // Save MAD-DIMM register
1060 if ((dimmA && (dimmA->ranks > 0))
1061 || (dimmB && (dimmB->ranks > 0))) {
1062 ctrl->mad_dimm[channel] = reg;
1063 } else {
1064 ctrl->mad_dimm[channel] = 0;
1065 }
1066 }
1067}
1068
1069static void dram_dimm_set_mapping(ramctr_timing * ctrl)
1070{
1071 int channel;
1072 FOR_ALL_CHANNELS {
1073 MCHBAR32(0x5004 + channel * 4) = ctrl->mad_dimm[channel];
1074 }
1075}
1076
1077static void dram_zones(ramctr_timing * ctrl, int training)
1078{
1079 u32 reg, ch0size, ch1size;
1080 u8 val;
1081 reg = 0;
1082 val = 0;
1083 if (training) {
1084 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
1085 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
1086 } else {
1087 ch0size = ctrl->channel_size_mb[0];
1088 ch1size = ctrl->channel_size_mb[1];
1089 }
1090
1091 if (ch0size >= ch1size) {
1092 reg = MCHBAR32(0x5014);
1093 val = ch1size / 256;
1094 reg = (reg & ~0xff000000) | val << 24;
1095 reg = (reg & ~0xff0000) | (2 * val) << 16;
1096 MCHBAR32(0x5014) = reg;
1097 MCHBAR32(0x5000) = 0x24;
1098 } else {
1099 reg = MCHBAR32(0x5014);
1100 val = ch0size / 256;
1101 reg = (reg & ~0xff000000) | val << 24;
1102 reg = (reg & ~0xff0000) | (2 * val) << 16;
1103 MCHBAR32(0x5014) = reg;
1104 MCHBAR32(0x5000) = 0x21;
1105 }
1106}
1107
1108static void dram_memorymap(ramctr_timing * ctrl, int me_uma_size)
1109{
1110 u32 reg, val, reclaim;
1111 u32 tom, gfxstolen, gttsize;
1112 size_t tsegsize, mmiosize, toludbase, touudbase, gfxstolenbase, gttbase,
1113 tsegbase, mestolenbase;
1114 size_t tsegbasedelta, remapbase, remaplimit;
1115 uint16_t ggc;
1116
Patrick Rudolph266a1f72016-06-09 18:13:34 +02001117 mmiosize = get_mmio_size();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001118
1119 ggc = pci_read_config16(NORTHBRIDGE, GGC);
1120 if (!(ggc & 2)) {
1121 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
1122 gttsize = ((ggc >> 8) & 0x3);
1123 } else {
1124 gfxstolen = 0;
1125 gttsize = 0;
1126 }
1127
1128 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
1129
1130 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
1131
1132 mestolenbase = tom - me_uma_size;
1133
1134 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize,
1135 tom - me_uma_size);
1136 gfxstolenbase = toludbase - gfxstolen;
1137 gttbase = gfxstolenbase - gttsize;
1138
1139 tsegbase = gttbase - tsegsize;
1140
1141 // Round tsegbase down to nearest address aligned to tsegsize
1142 tsegbasedelta = tsegbase & (tsegsize - 1);
1143 tsegbase &= ~(tsegsize - 1);
1144
1145 gttbase -= tsegbasedelta;
1146 gfxstolenbase -= tsegbasedelta;
1147 toludbase -= tsegbasedelta;
1148
Elyes HAOUAS15279a92016-07-28 21:05:26 +02001149 // Test if it is possible to reclaim a hole in the RAM addressing
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001150 if (tom - me_uma_size > toludbase) {
1151 // Reclaim is possible
1152 reclaim = 1;
1153 remapbase = MAX(4096, tom - me_uma_size);
1154 remaplimit =
1155 remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
1156 touudbase = remaplimit + 1;
1157 } else {
1158 // Reclaim not possible
1159 reclaim = 0;
1160 touudbase = tom - me_uma_size;
1161 }
1162
1163 // Update memory map in pci-e configuration space
Patrick Rudolpha649a542016-01-17 18:32:06 +01001164 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001165
1166 // TOM (top of memory)
1167 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa0);
1168 val = tom & 0xfff;
1169 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001170 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa0, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001171 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa0, reg);
1172
1173 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa4);
1174 val = tom & 0xfffff000;
1175 reg = (reg & ~0x000fffff) | (val >> 12);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001176 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa4, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001177 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa4, reg);
1178
1179 // TOLUD (top of low used dram)
1180 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xbc);
1181 val = toludbase & 0xfff;
1182 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001183 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xbc, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001184 pcie_write_config32(PCI_DEV(0, 0, 0), 0xbc, reg);
1185
1186 // TOUUD LSB (top of upper usable dram)
1187 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa8);
1188 val = touudbase & 0xfff;
1189 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001190 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa8, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001191 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa8, reg);
1192
1193 // TOUUD MSB
1194 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xac);
1195 val = touudbase & 0xfffff000;
1196 reg = (reg & ~0x000fffff) | (val >> 12);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001197 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xac, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001198 pcie_write_config32(PCI_DEV(0, 0, 0), 0xac, reg);
1199
1200 if (reclaim) {
1201 // REMAP BASE
1202 pcie_write_config32(PCI_DEV(0, 0, 0), 0x90, remapbase << 20);
1203 pcie_write_config32(PCI_DEV(0, 0, 0), 0x94, remapbase >> 12);
1204
1205 // REMAP LIMIT
1206 pcie_write_config32(PCI_DEV(0, 0, 0), 0x98, remaplimit << 20);
1207 pcie_write_config32(PCI_DEV(0, 0, 0), 0x9c, remaplimit >> 12);
1208 }
1209 // TSEG
1210 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb8);
1211 val = tsegbase & 0xfff;
1212 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001213 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb8, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001214 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb8, reg);
1215
1216 // GFX stolen memory
1217 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb0);
1218 val = gfxstolenbase & 0xfff;
1219 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001220 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb0, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001221 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb0, reg);
1222
1223 // GTT stolen memory
1224 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb4);
1225 val = gttbase & 0xfff;
1226 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001227 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb4, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001228 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb4, reg);
1229
1230 if (me_uma_size) {
1231 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x7c);
1232 val = (0x80000 - me_uma_size) & 0xfffff000;
1233 reg = (reg & ~0x000fffff) | (val >> 12);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001234 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x7c, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001235 pcie_write_config32(PCI_DEV(0, 0, 0), 0x7c, reg);
1236
1237 // ME base
1238 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x70);
1239 val = mestolenbase & 0xfff;
1240 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001241 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x70, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001242 pcie_write_config32(PCI_DEV(0, 0, 0), 0x70, reg);
1243
1244 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x74);
1245 val = mestolenbase & 0xfffff000;
1246 reg = (reg & ~0x000fffff) | (val >> 12);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001247 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x74, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001248 pcie_write_config32(PCI_DEV(0, 0, 0), 0x74, reg);
1249
1250 // ME mask
1251 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x78);
1252 val = (0x80000 - me_uma_size) & 0xfff;
1253 reg = (reg & ~0xfff00000) | (val << 20);
1254 reg = (reg & ~0x400) | (1 << 10); // set lockbit on ME mem
1255
1256 reg = (reg & ~0x800) | (1 << 11); // set ME memory enable
Patrick Rudolpha649a542016-01-17 18:32:06 +01001257 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x78, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001258 pcie_write_config32(PCI_DEV(0, 0, 0), 0x78, reg);
1259 }
1260}
1261
1262static void dram_ioregs(ramctr_timing * ctrl)
1263{
1264 u32 reg, comp2;
1265
1266 int channel;
1267
1268 // IO clock
1269 FOR_ALL_CHANNELS {
1270 MCHBAR32(0xc00 + 0x100 * channel) = ctrl->rankmap[channel];
1271 }
1272
1273 // IO command
1274 FOR_ALL_CHANNELS {
1275 MCHBAR32(0x3200 + 0x100 * channel) = ctrl->rankmap[channel];
1276 }
1277
1278 // IO control
1279 FOR_ALL_POPULATED_CHANNELS {
1280 program_timings(ctrl, channel);
1281 }
1282
1283 // Rcomp
Patrick Rudolpha649a542016-01-17 18:32:06 +01001284 printram("RCOMP...");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001285 reg = 0;
1286 while (reg == 0) {
1287 reg = MCHBAR32(0x5084) & 0x10000;
1288 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01001289 printram("done\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001290
1291 // Set comp2
1292 comp2 = get_COMP2(ctrl->tCK);
1293 MCHBAR32(0x3714) = comp2;
Patrick Rudolpha649a542016-01-17 18:32:06 +01001294 printram("COMP2 done\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001295
1296 // Set comp1
1297 FOR_ALL_POPULATED_CHANNELS {
1298 reg = MCHBAR32(0x1810 + channel * 0x100); //ch0
1299 reg = (reg & ~0xe00) | (1 << 9); //odt
1300 reg = (reg & ~0xe00000) | (1 << 21); //clk drive up
1301 reg = (reg & ~0x38000000) | (1 << 27); //ctl drive up
1302 MCHBAR32(0x1810 + channel * 0x100) = reg;
1303 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01001304 printram("COMP1 done\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001305
Patrick Rudolpha649a542016-01-17 18:32:06 +01001306 printram("FORCE RCOMP and wait 20us...");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001307 MCHBAR32(0x5f08) |= 0x100;
1308 udelay(20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001309 printram("done\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001310}
1311
1312static void wait_428c(int channel)
1313{
1314 while (1) {
1315 if (read32(DEFAULT_MCHBAR + 0x428c + (channel << 10)) & 0x50)
1316 return;
1317 }
1318}
1319
1320static void write_reset(ramctr_timing * ctrl)
1321{
1322 int channel, slotrank;
1323
1324 /* choose a populated channel. */
1325 channel = (ctrl->rankmap[0]) ? 0 : 1;
1326
1327 wait_428c(channel);
1328
1329 /* choose a populated rank. */
1330 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
1331
Patrick Rudolph371d2912015-10-09 13:33:25 +02001332 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001333 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1334 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x80c01);
1335
1336 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1337 (slotrank << 24) | 0x60000);
1338
1339 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1340
1341 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x400001);
1342 wait_428c(channel);
1343}
1344
1345static void dram_jedecreset(ramctr_timing * ctrl)
1346{
1347 u32 reg, addr;
1348 int channel;
1349
Elyes HAOUAS7db506c2016-10-02 11:56:39 +02001350 while (!(MCHBAR32(0x5084) & 0x10000));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001351 do {
1352 reg = MCHBAR32(0x428c);
1353 } while ((reg & 0x14) == 0);
1354
1355 // Set state of memory controller
1356 reg = 0x112;
1357 MCHBAR32(0x5030) = reg;
1358 MCHBAR32(0x4ea0) = 0;
1359 reg |= 2; //ddr reset
1360 MCHBAR32(0x5030) = reg;
1361
1362 // Assert dimm reset signal
1363 reg = MCHBAR32(0x5030);
1364 reg &= ~0x2;
1365 MCHBAR32(0x5030) = reg;
1366
1367 // Wait 200us
1368 udelay(200);
1369
1370 // Deassert dimm reset signal
1371 MCHBAR32(0x5030) |= 2;
1372
1373 // Wait 500us
1374 udelay(500);
1375
1376 // Enable DCLK
1377 MCHBAR32(0x5030) |= 4;
1378
1379 // XXX Wait 20ns
1380 udelay(1);
1381
1382 FOR_ALL_CHANNELS {
1383 // Set valid rank CKE
1384 reg = 0;
1385 reg = (reg & ~0xf) | ctrl->rankmap[channel];
1386 addr = 0x400 * channel + 0x42a0;
1387 MCHBAR32(addr) = reg;
1388
1389 // Wait 10ns for ranks to settle
1390 //udelay(0.01);
1391
1392 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
1393 MCHBAR32(addr) = reg;
1394
1395 // Write reset using a NOP
1396 write_reset(ctrl);
1397 }
1398}
1399
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001400static odtmap get_ODT(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001401{
1402 /* Get ODT based on rankmap: */
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001403 int dimms_per_ch = (ctrl->rankmap[channel] & 1)
1404 + ((ctrl->rankmap[channel] >> 2) & 1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001405
1406 if (dimms_per_ch == 1) {
1407 return (const odtmap){60, 60};
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001408 } else {
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001409 return (const odtmap){120, 30};
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001410 }
1411}
1412
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001413static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001414 int reg, u32 val)
1415{
1416 wait_428c(channel);
1417
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001418 if (ctrl->rank_mirror[channel][slotrank]) {
1419 /* DDR3 Rank1 Address mirror
1420 * swap the following pins:
1421 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
1422 reg = ((reg >> 1) & 1) | ((reg << 1) & 2);
1423 val = (val & ~0x1f8) | ((val >> 1) & 0xa8)
1424 | ((val & 0xa8) << 1);
1425 }
1426
Patrick Rudolph371d2912015-10-09 13:33:25 +02001427 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001428 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f000);
1429 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
1430 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1431 (slotrank << 24) | (reg << 20) | val | 0x60000);
1432 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1433
Patrick Rudolph371d2912015-10-09 13:33:25 +02001434 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001435 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f000);
1436 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x41001);
1437 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1438 (slotrank << 24) | (reg << 20) | val | 0x60000);
1439 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1440
Patrick Rudolph371d2912015-10-09 13:33:25 +02001441 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001442 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x0f000);
1443 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1444 0x1001 | (ctrl->tMOD << 16));
1445 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1446 (slotrank << 24) | (reg << 20) | val | 0x60000);
1447 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1448 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
1449}
1450
1451static u32 make_mr0(ramctr_timing * ctrl, u8 rank)
1452{
1453 u16 mr0reg, mch_cas, mch_wr;
1454 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 +02001455
1456 /* DLL Reset - self clearing - set after CLK frequency has been changed */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001457 mr0reg = 0x100;
1458
1459 // Convert CAS to MCH register friendly
1460 if (ctrl->CAS < 12) {
1461 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
1462 } else {
1463 mch_cas = (u16) (ctrl->CAS - 12);
1464 mch_cas = ((mch_cas << 1) | 0x1);
1465 }
1466
1467 // Convert tWR to MCH register friendly
1468 mch_wr = mch_wr_t[ctrl->tWR - 5];
1469
1470 mr0reg = (mr0reg & ~0x4) | (mch_cas & 0x1);
1471 mr0reg = (mr0reg & ~0x70) | ((mch_cas & 0xe) << 3);
1472 mr0reg = (mr0reg & ~0xe00) | (mch_wr << 9);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001473
1474 // Precharge PD - Fast (desktop) 0x1 or slow (mobile) 0x0 - mostly power-saving feature
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001475 mr0reg = (mr0reg & ~0x1000) | (!ctrl->mobile << 12);
1476 return mr0reg;
1477}
1478
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001479static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001480{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001481 write_mrreg(ctrl, channel, rank, 0,
1482 make_mr0(ctrl, rank));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001483}
1484
1485static u32 encode_odt(u32 odt)
1486{
1487 switch (odt) {
1488 case 30:
1489 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
1490 case 60:
1491 return (1 << 2); // RZQ/4
1492 case 120:
1493 return (1 << 6); // RZQ/2
1494 default:
1495 case 0:
1496 return 0;
1497 }
1498}
1499
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001500static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001501{
1502 odtmap odt;
1503 u32 mr1reg;
1504
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001505 odt = get_ODT(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001506 mr1reg = 0x2;
1507
1508 mr1reg |= encode_odt(odt.rttnom);
1509
1510 return mr1reg;
1511}
1512
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001513static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001514{
1515 u16 mr1reg;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001516
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001517 mr1reg = make_mr1(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001518
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001519 write_mrreg(ctrl, channel, rank, 1, mr1reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001520}
1521
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001522static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001523{
1524 u16 pasr, cwl, mr2reg;
1525 odtmap odt;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001526 int srt;
1527
1528 pasr = 0;
1529 cwl = ctrl->CWL - 5;
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001530 odt = get_ODT(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001531
1532 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
1533
1534 mr2reg = 0;
1535 mr2reg = (mr2reg & ~0x7) | pasr;
1536 mr2reg = (mr2reg & ~0x38) | (cwl << 3);
1537 mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6);
1538 mr2reg = (mr2reg & ~0x80) | (srt << 7);
1539 mr2reg |= (odt.rttwr / 60) << 9;
1540
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001541 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001542}
1543
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001544static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001545{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001546 write_mrreg(ctrl, channel, rank, 3, 0);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001547}
1548
1549static void dram_mrscommands(ramctr_timing * ctrl)
1550{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001551 u8 slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001552 u32 reg, addr;
1553 int channel;
1554
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001555 FOR_ALL_POPULATED_CHANNELS {
1556 FOR_ALL_POPULATED_RANKS {
1557 // MR2
1558 dram_mr2(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001559
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001560 // MR3
1561 dram_mr3(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001562
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001563 // MR1
1564 dram_mr1(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001565
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001566 // MR0
1567 dram_mr0(ctrl, slotrank, channel);
1568 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001569 }
1570
Patrick Rudolph371d2912015-10-09 13:33:25 +02001571 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001572 write32(DEFAULT_MCHBAR + 0x4e20, 0x7);
1573 write32(DEFAULT_MCHBAR + 0x4e30, 0xf1001);
1574 write32(DEFAULT_MCHBAR + 0x4e00, 0x60002);
1575 write32(DEFAULT_MCHBAR + 0x4e10, 0);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001576
1577 /* DRAM command ZQCL */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001578 write32(DEFAULT_MCHBAR + 0x4e24, 0x1f003);
1579 write32(DEFAULT_MCHBAR + 0x4e34, 0x1901001);
1580 write32(DEFAULT_MCHBAR + 0x4e04, 0x60400);
1581 write32(DEFAULT_MCHBAR + 0x4e14, 0x288);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001582
1583 /* execute command queue on all channels ? */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001584 write32(DEFAULT_MCHBAR + 0x4e84, 0x40004);
1585
1586 // Drain
1587 FOR_ALL_CHANNELS {
1588 // Wait for ref drained
1589 wait_428c(channel);
1590 }
1591
1592 // Refresh enable
1593 MCHBAR32(0x5030) |= 8;
1594
1595 FOR_ALL_POPULATED_CHANNELS {
1596 addr = 0x400 * channel + 0x4020;
1597 reg = MCHBAR32(addr);
1598 reg &= ~0x200000;
1599 MCHBAR32(addr) = reg;
1600
1601 wait_428c(channel);
1602
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001603 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001604
1605 // Drain
1606 wait_428c(channel);
1607
Patrick Rudolph371d2912015-10-09 13:33:25 +02001608 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001609 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1610 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
1611 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001612 (slotrank << 24) | 0x60000);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001613 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
1614 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x1);
1615
1616 // Drain
1617 wait_428c(channel);
1618 }
1619}
1620
1621const u32 lane_registers[] = {
1622 0x0000, 0x0200, 0x0400, 0x0600,
1623 0x1000, 0x1200, 0x1400, 0x1600,
1624 0x0800
1625};
1626
1627static void program_timings(ramctr_timing * ctrl, int channel)
1628{
1629 u32 reg32, reg_4024, reg_c14, reg_c18, reg_4028;
1630 int lane;
1631 int slotrank, slot;
1632 int full_shift = 0;
1633 u16 slot320c[NUM_SLOTS];
1634
1635 FOR_ALL_POPULATED_RANKS {
1636 if (full_shift < -ctrl->timings[channel][slotrank].val_320c)
1637 full_shift = -ctrl->timings[channel][slotrank].val_320c;
1638 }
1639
1640 for (slot = 0; slot < NUM_SLOTS; slot++)
1641 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
1642 case 0:
1643 default:
1644 slot320c[slot] = 0x7f;
1645 break;
1646 case 1:
1647 slot320c[slot] =
1648 ctrl->timings[channel][2 * slot + 0].val_320c +
1649 full_shift;
1650 break;
1651 case 2:
1652 slot320c[slot] =
1653 ctrl->timings[channel][2 * slot + 1].val_320c +
1654 full_shift;
1655 break;
1656 case 3:
1657 slot320c[slot] =
1658 (ctrl->timings[channel][2 * slot].val_320c +
1659 ctrl->timings[channel][2 * slot +
1660 1].val_320c) / 2 +
1661 full_shift;
1662 break;
1663 }
1664
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +01001665 /* enable CMD XOVER */
1666 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001667 reg32 |= ((slot320c[0] & 0x3f) << 6) | ((slot320c[0] & 0x40) << 9);
1668 reg32 |= (slot320c[1] & 0x7f) << 18;
1669 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
1670
1671 MCHBAR32(0x320c + 0x100 * channel) = reg32;
1672
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +01001673 /* enable CLK XOVER */
1674 reg_c14 = get_XOVER_CLK(ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001675 reg_c18 = 0;
1676
1677 FOR_ALL_POPULATED_RANKS {
1678 int shift =
1679 ctrl->timings[channel][slotrank].val_320c + full_shift;
1680 int offset_val_c14;
1681 if (shift < 0)
1682 shift = 0;
1683 offset_val_c14 = ctrl->reg_c14_offset + shift;
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +01001684 /* set CLK phase shift */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001685 reg_c14 |= (offset_val_c14 & 0x3f) << (6 * slotrank);
1686 reg_c18 |= ((offset_val_c14 >> 6) & 1) << slotrank;
1687 }
1688
1689 MCHBAR32(0xc14 + channel * 0x100) = reg_c14;
1690 MCHBAR32(0xc18 + channel * 0x100) = reg_c18;
1691
1692 reg_4028 = MCHBAR32(0x4028 + 0x400 * channel);
1693 reg_4028 &= 0xffff0000;
1694
1695 reg_4024 = 0;
1696
1697 FOR_ALL_POPULATED_RANKS {
1698 int post_timA_min_high = 7, post_timA_max_high = 0;
1699 int pre_timA_min_high = 7, pre_timA_max_high = 0;
1700 int shift_402x = 0;
1701 int shift =
1702 ctrl->timings[channel][slotrank].val_320c + full_shift;
1703
1704 if (shift < 0)
1705 shift = 0;
1706
1707 FOR_ALL_LANES {
1708 if (post_timA_min_high >
1709 ((ctrl->timings[channel][slotrank].lanes[lane].
1710 timA + shift) >> 6))
1711 post_timA_min_high =
1712 ((ctrl->timings[channel][slotrank].
1713 lanes[lane].timA + shift) >> 6);
1714 if (pre_timA_min_high >
1715 (ctrl->timings[channel][slotrank].lanes[lane].
1716 timA >> 6))
1717 pre_timA_min_high =
1718 (ctrl->timings[channel][slotrank].
1719 lanes[lane].timA >> 6);
1720 if (post_timA_max_high <
1721 ((ctrl->timings[channel][slotrank].lanes[lane].
1722 timA + shift) >> 6))
1723 post_timA_max_high =
1724 ((ctrl->timings[channel][slotrank].
1725 lanes[lane].timA + shift) >> 6);
1726 if (pre_timA_max_high <
1727 (ctrl->timings[channel][slotrank].lanes[lane].
1728 timA >> 6))
1729 pre_timA_max_high =
1730 (ctrl->timings[channel][slotrank].
1731 lanes[lane].timA >> 6);
1732 }
1733
1734 if (pre_timA_max_high - pre_timA_min_high <
1735 post_timA_max_high - post_timA_min_high)
1736 shift_402x = +1;
1737 else if (pre_timA_max_high - pre_timA_min_high >
1738 post_timA_max_high - post_timA_min_high)
1739 shift_402x = -1;
1740
1741 reg_4028 |=
1742 (ctrl->timings[channel][slotrank].val_4028 + shift_402x -
1743 post_timA_min_high) << (4 * slotrank);
1744 reg_4024 |=
1745 (ctrl->timings[channel][slotrank].val_4024 +
1746 shift_402x) << (8 * slotrank);
1747
1748 FOR_ALL_LANES {
1749 MCHBAR32(lane_registers[lane] + 0x10 + 0x100 * channel +
1750 4 * slotrank)
1751 =
1752 (((ctrl->timings[channel][slotrank].lanes[lane].
1753 timA + shift) & 0x3f)
1754 |
1755 ((ctrl->timings[channel][slotrank].lanes[lane].
1756 rising + shift) << 8)
1757 |
1758 (((ctrl->timings[channel][slotrank].lanes[lane].
1759 timA + shift -
1760 (post_timA_min_high << 6)) & 0x1c0) << 10)
Patrick Rudolph0188b132016-02-10 19:16:34 +01001761 | ((ctrl->timings[channel][slotrank].lanes[lane].
1762 falling + shift) << 20));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001763
1764 MCHBAR32(lane_registers[lane] + 0x20 + 0x100 * channel +
1765 4 * slotrank)
1766 =
1767 (((ctrl->timings[channel][slotrank].lanes[lane].
1768 timC + shift) & 0x3f)
1769 |
1770 (((ctrl->timings[channel][slotrank].lanes[lane].
1771 timB + shift) & 0x3f) << 8)
1772 |
1773 (((ctrl->timings[channel][slotrank].lanes[lane].
1774 timB + shift) & 0x1c0) << 9)
1775 |
1776 (((ctrl->timings[channel][slotrank].lanes[lane].
1777 timC + shift) & 0x40) << 13));
1778 }
1779 }
1780 MCHBAR32(0x4024 + 0x400 * channel) = reg_4024;
1781 MCHBAR32(0x4028 + 0x400 * channel) = reg_4028;
1782}
1783
1784static void test_timA(ramctr_timing * ctrl, int channel, int slotrank)
1785{
1786 wait_428c(channel);
1787
Patrick Rudolph371d2912015-10-09 13:33:25 +02001788 /* DRAM command MRS
1789 * write MR3 MPR enable
1790 * in this mode only RD and RDA are allowed
1791 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001792 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
1793 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1794 (0xc01 | (ctrl->tMOD << 16)));
1795 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1796 (slotrank << 24) | 0x360004);
1797 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1798
Patrick Rudolph371d2912015-10-09 13:33:25 +02001799 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001800 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
1801 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x4040c01);
1802 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, (slotrank << 24));
1803 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1804
Patrick Rudolph371d2912015-10-09 13:33:25 +02001805 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001806 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
1807 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1808 0x100f | ((ctrl->CAS + 36) << 16));
1809 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1810 (slotrank << 24) | 0x60000);
1811 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1812
Patrick Rudolph371d2912015-10-09 13:33:25 +02001813 /* DRAM command MRS
1814 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001815 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
1816 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1817 (0xc01 | (ctrl->tMOD << 16)));
1818 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1819 (slotrank << 24) | 0x360000);
1820 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1821
1822 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1823
1824 wait_428c(channel);
1825}
1826
1827static int does_lane_work(ramctr_timing * ctrl, int channel, int slotrank,
1828 int lane)
1829{
1830 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
1831 return ((read32
1832 (DEFAULT_MCHBAR + lane_registers[lane] + channel * 0x100 + 4 +
1833 ((timA / 32) & 1) * 4)
1834 >> (timA % 32)) & 1);
1835}
1836
1837struct run {
1838 int middle;
1839 int end;
1840 int start;
1841 int all;
1842 int length;
1843};
1844
1845static struct run get_longest_zero_run(int *seq, int sz)
1846{
1847 int i, ls;
1848 int bl = 0, bs = 0;
1849 struct run ret;
1850
1851 ls = 0;
1852 for (i = 0; i < 2 * sz; i++)
1853 if (seq[i % sz]) {
1854 if (i - ls > bl) {
1855 bl = i - ls;
1856 bs = ls;
1857 }
1858 ls = i + 1;
1859 }
1860 if (bl == 0) {
1861 ret.middle = sz / 2;
1862 ret.start = 0;
1863 ret.end = sz;
1864 ret.all = 1;
1865 return ret;
1866 }
1867
1868 ret.start = bs % sz;
1869 ret.end = (bs + bl - 1) % sz;
1870 ret.middle = (bs + (bl - 1) / 2) % sz;
1871 ret.length = bl;
1872 ret.all = 0;
1873
1874 return ret;
1875}
1876
1877static void discover_timA_coarse(ramctr_timing * ctrl, int channel,
1878 int slotrank, int *upperA)
1879{
1880 int timA;
1881 int statistics[NUM_LANES][128];
1882 int lane;
1883
1884 for (timA = 0; timA < 128; timA++) {
1885 FOR_ALL_LANES {
1886 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1887 }
1888 program_timings(ctrl, channel);
1889
1890 test_timA(ctrl, channel, slotrank);
1891
1892 FOR_ALL_LANES {
1893 statistics[lane][timA] =
1894 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001895 printram("Astat: %d, %d, %d: %x, %x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001896 channel, slotrank, lane, timA,
1897 statistics[lane][timA]);
1898 }
1899 }
1900 FOR_ALL_LANES {
1901 struct run rn = get_longest_zero_run(statistics[lane], 128);
1902 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1903 upperA[lane] = rn.end;
1904 if (upperA[lane] < rn.middle)
1905 upperA[lane] += 128;
Patrick Rudolpha649a542016-01-17 18:32:06 +01001906 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001907 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001908 printram("Aend: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001909 lane, upperA[lane]);
1910 }
1911}
1912
1913static void discover_timA_fine(ramctr_timing * ctrl, int channel, int slotrank,
1914 int *upperA)
1915{
1916 int timA_delta;
1917 int statistics[NUM_LANES][51];
1918 int lane, i;
1919
1920 memset(statistics, 0, sizeof(statistics));
1921
1922 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
1923 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
1924 timA = upperA[lane] + timA_delta + 0x40;
1925 program_timings(ctrl, channel);
1926
1927 for (i = 0; i < 100; i++) {
1928 test_timA(ctrl, channel, slotrank);
1929 FOR_ALL_LANES {
1930 statistics[lane][timA_delta + 25] +=
1931 does_lane_work(ctrl, channel, slotrank,
1932 lane);
1933 }
1934 }
1935 }
1936 FOR_ALL_LANES {
1937 int last_zero, first_all;
1938
1939 for (last_zero = -25; last_zero <= 25; last_zero++)
1940 if (statistics[lane][last_zero + 25])
1941 break;
1942 last_zero--;
1943 for (first_all = -25; first_all <= 25; first_all++)
1944 if (statistics[lane][first_all + 25] == 100)
1945 break;
1946
1947 printram("lane %d: %d, %d\n", lane, last_zero,
1948 first_all);
1949
1950 ctrl->timings[channel][slotrank].lanes[lane].timA =
1951 (last_zero + first_all) / 2 + upperA[lane];
Patrick Rudolpha649a542016-01-17 18:32:06 +01001952 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001953 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
1954 }
1955}
1956
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001957static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001958 int *upperA)
1959{
1960 int works[NUM_LANES];
1961 int lane;
1962 while (1) {
1963 int all_works = 1, some_works = 0;
1964 program_timings(ctrl, channel);
1965 test_timA(ctrl, channel, slotrank);
1966 FOR_ALL_LANES {
1967 works[lane] =
1968 !does_lane_work(ctrl, channel, slotrank, lane);
1969 if (works[lane])
1970 some_works = 1;
1971 else
1972 all_works = 0;
1973 }
1974 if (all_works)
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001975 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001976 if (!some_works) {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001977 if (ctrl->timings[channel][slotrank].val_4024 < 2) {
1978 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1979 channel, slotrank);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001980 return MAKE_ERR;
1981 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001982 ctrl->timings[channel][slotrank].val_4024 -= 2;
1983 printram("4024 -= 2;\n");
1984 continue;
1985 }
1986 ctrl->timings[channel][slotrank].val_4028 += 2;
1987 printram("4028 += 2;\n");
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001988 if (ctrl->timings[channel][slotrank].val_4028 >= 0x10) {
1989 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1990 channel, slotrank);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001991 return MAKE_ERR;
1992 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001993 FOR_ALL_LANES if (works[lane]) {
1994 ctrl->timings[channel][slotrank].lanes[lane].timA +=
1995 128;
1996 upperA[lane] += 128;
1997 printram("increment %d, %d, %d\n", channel,
1998 slotrank, lane);
1999 }
2000 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002001 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002002}
2003
2004struct timA_minmax {
2005 int timA_min_high, timA_max_high;
2006};
2007
2008static void pre_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
2009 struct timA_minmax *mnmx)
2010{
2011 int lane;
2012 mnmx->timA_min_high = 7;
2013 mnmx->timA_max_high = 0;
2014
2015 FOR_ALL_LANES {
2016 if (mnmx->timA_min_high >
2017 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
2018 mnmx->timA_min_high =
2019 (ctrl->timings[channel][slotrank].lanes[lane].
2020 timA >> 6);
2021 if (mnmx->timA_max_high <
2022 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
2023 mnmx->timA_max_high =
2024 (ctrl->timings[channel][slotrank].lanes[lane].
2025 timA >> 6);
2026 }
2027}
2028
2029static void post_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
2030 struct timA_minmax *mnmx)
2031{
2032 struct timA_minmax post;
2033 int shift_402x = 0;
2034
2035 /* Get changed maxima. */
2036 pre_timA_change(ctrl, channel, slotrank, &post);
2037
2038 if (mnmx->timA_max_high - mnmx->timA_min_high <
2039 post.timA_max_high - post.timA_min_high)
2040 shift_402x = +1;
2041 else if (mnmx->timA_max_high - mnmx->timA_min_high >
2042 post.timA_max_high - post.timA_min_high)
2043 shift_402x = -1;
2044 else
2045 shift_402x = 0;
2046
2047 ctrl->timings[channel][slotrank].val_4028 += shift_402x;
2048 ctrl->timings[channel][slotrank].val_4024 += shift_402x;
2049 printram("4024 += %d;\n", shift_402x);
2050 printram("4028 += %d;\n", shift_402x);
2051}
2052
Patrick Rudolph371d2912015-10-09 13:33:25 +02002053/* Compensate the skew between DQS and DQs.
2054 * To ease PCB design a small skew between Data Strobe signals and
2055 * Data Signals is allowed.
2056 * The controller has to measure and compensate this skew for every byte-lane.
2057 * By delaying either all DQs signals or DQS signal, a full phase
2058 * shift can be introduced.
2059 * It is assumed that one byte-lane's DQs signals have the same routing delay.
2060 *
2061 * To measure the actual skew, the DRAM is placed in "read leveling" mode.
2062 * In read leveling mode the DRAM-chip outputs an alternating periodic pattern.
2063 * The memory controller iterates over all possible values to do a full phase shift
2064 * and issues read commands.
2065 * With DQS and DQs in phase the data read is expected to alternate on every byte:
2066 * 0xFF 0x00 0xFF ...
2067 * Once the controller has detected this pattern a bit in the result register is
2068 * set for the current phase shift.
2069 */
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002070static int read_training(ramctr_timing * ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002071{
2072 int channel, slotrank, lane;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002073 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002074
2075 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002076 int all_high, some_high;
2077 int upperA[NUM_LANES];
2078 struct timA_minmax mnmx;
2079
2080 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002081
2082 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002083 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2084 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2085 0xc01 | (ctrl->tRP << 16));
2086 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2087 (slotrank << 24) | 0x60400);
2088 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2089 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2090
2091 write32(DEFAULT_MCHBAR + 0x3400, (slotrank << 2) | 0x8001);
2092
2093 ctrl->timings[channel][slotrank].val_4028 = 4;
2094 ctrl->timings[channel][slotrank].val_4024 = 55;
2095 program_timings(ctrl, channel);
2096
2097 discover_timA_coarse(ctrl, channel, slotrank, upperA);
2098
2099 all_high = 1;
2100 some_high = 0;
2101 FOR_ALL_LANES {
2102 if (ctrl->timings[channel][slotrank].lanes[lane].
2103 timA >= 0x40)
2104 some_high = 1;
2105 else
2106 all_high = 0;
2107 }
2108
2109 if (all_high) {
2110 ctrl->timings[channel][slotrank].val_4028--;
2111 printram("4028--;\n");
2112 FOR_ALL_LANES {
2113 ctrl->timings[channel][slotrank].lanes[lane].
2114 timA -= 0x40;
2115 upperA[lane] -= 0x40;
2116
2117 }
2118 } else if (some_high) {
2119 ctrl->timings[channel][slotrank].val_4024++;
2120 ctrl->timings[channel][slotrank].val_4028++;
2121 printram("4024++;\n");
2122 printram("4028++;\n");
2123 }
2124
2125 program_timings(ctrl, channel);
2126
2127 pre_timA_change(ctrl, channel, slotrank, &mnmx);
2128
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002129 err = discover_402x(ctrl, channel, slotrank, upperA);
2130 if (err)
2131 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002132
2133 post_timA_change(ctrl, channel, slotrank, &mnmx);
2134 pre_timA_change(ctrl, channel, slotrank, &mnmx);
2135
2136 discover_timA_fine(ctrl, channel, slotrank, upperA);
2137
2138 post_timA_change(ctrl, channel, slotrank, &mnmx);
2139 pre_timA_change(ctrl, channel, slotrank, &mnmx);
2140
2141 FOR_ALL_LANES {
2142 ctrl->timings[channel][slotrank].lanes[lane].timA -= mnmx.timA_min_high * 0x40;
2143 }
2144 ctrl->timings[channel][slotrank].val_4028 -= mnmx.timA_min_high;
2145 printram("4028 -= %d;\n", mnmx.timA_min_high);
2146
2147 post_timA_change(ctrl, channel, slotrank, &mnmx);
2148
2149 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
2150 ctrl->timings[channel][slotrank].val_4024,
2151 ctrl->timings[channel][slotrank].val_4028);
2152
Patrick Rudolpha649a542016-01-17 18:32:06 +01002153 printram("final results:\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002154 FOR_ALL_LANES
Patrick Rudolpha649a542016-01-17 18:32:06 +01002155 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002156 lane,
2157 ctrl->timings[channel][slotrank].lanes[lane].timA);
2158
2159 write32(DEFAULT_MCHBAR + 0x3400, 0);
2160
Patrick Rudolph9b515682015-10-09 13:43:51 +02002161 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002162 }
2163
2164 FOR_ALL_POPULATED_CHANNELS {
2165 program_timings(ctrl, channel);
2166 }
2167 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2168 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
2169 + 4 * lane, 0);
2170 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002171 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002172}
2173
2174static void test_timC(ramctr_timing * ctrl, int channel, int slotrank)
2175{
2176 int lane;
2177
2178 FOR_ALL_LANES {
2179 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + 4 * lane, 0);
2180 read32(DEFAULT_MCHBAR + 0x4140 + 0x400 * channel + 4 * lane);
2181 }
2182
2183 wait_428c(channel);
2184
Patrick Rudolph371d2912015-10-09 13:33:25 +02002185 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002186 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2187 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2188 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD) << 10)
2189 | 4 | (ctrl->tRCD << 16));
2190
2191 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2192 (slotrank << 24) | (6 << 16));
2193
2194 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
2195
Patrick Rudolph371d2912015-10-09 13:33:25 +02002196 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002197 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2198 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8041001);
2199 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2200 (slotrank << 24) | 8);
2201 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2202
Patrick Rudolph371d2912015-10-09 13:33:25 +02002203 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002204 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2205 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x80411f4);
2206 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
2207 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
2208
Patrick Rudolph371d2912015-10-09 13:33:25 +02002209 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002210 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2211 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2212 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2213 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2214 (slotrank << 24) | 8);
2215 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2216
2217 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2218
2219 wait_428c(channel);
2220
Patrick Rudolph371d2912015-10-09 13:33:25 +02002221 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002222 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2223 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2224 0xc01 | (ctrl->tRP << 16));
2225 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2226 (slotrank << 24) | 0x60400);
2227 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2228
Patrick Rudolph371d2912015-10-09 13:33:25 +02002229 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002230 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2231 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2232 (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) << 10)
2233 | 8 | (ctrl->CAS << 16));
2234
2235 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2236 (slotrank << 24) | 0x60000);
2237
2238 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x244);
2239
Patrick Rudolph371d2912015-10-09 13:33:25 +02002240 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002241 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2242 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2243 0x40011f4 | (max(ctrl->tRTP, 8) << 16));
2244 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
2245 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
2246
Patrick Rudolph371d2912015-10-09 13:33:25 +02002247 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002248 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2249 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2250 0xc01 | (ctrl->tRP << 16));
2251 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2252 (slotrank << 24) | 0x60400);
2253 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2254 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2255 wait_428c(channel);
2256}
2257
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002258static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002259{
2260 int timC;
2261 int statistics[NUM_LANES][MAX_TIMC + 1];
2262 int lane;
2263
2264 wait_428c(channel);
2265
Patrick Rudolph371d2912015-10-09 13:33:25 +02002266 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002267 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2268 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2269 0xc01 | (ctrl->tRP << 16));
2270 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2271 (slotrank << 24) | 0x60400);
2272 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2273 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2274
2275 for (timC = 0; timC <= MAX_TIMC; timC++) {
2276 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
2277 timC = timC;
2278 program_timings(ctrl, channel);
2279
2280 test_timC(ctrl, channel, slotrank);
2281
2282 FOR_ALL_LANES {
2283 statistics[lane][timC] =
2284 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2285 0x400 * channel);
2286 printram("Cstat: %d, %d, %d, %x, %x\n",
2287 channel, slotrank, lane, timC,
2288 statistics[lane][timC]);
2289 }
2290 }
2291 FOR_ALL_LANES {
2292 struct run rn =
2293 get_longest_zero_run(statistics[lane], MAX_TIMC + 1);
2294 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002295 if (rn.all) {
2296 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
2297 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002298 return MAKE_ERR;
2299 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01002300 printram("Cval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002301 lane, ctrl->timings[channel][slotrank].lanes[lane].timC);
2302 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002303 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002304}
2305
2306static int get_precedening_channels(ramctr_timing * ctrl, int target_channel)
2307{
2308 int channel, ret = 0;
2309 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
2310 ret++;
2311 return ret;
2312}
2313
2314static void fill_pattern0(ramctr_timing * ctrl, int channel, u32 a, u32 b)
2315{
2316 unsigned j;
2317 unsigned channel_offset =
2318 get_precedening_channels(ctrl, channel) * 0x40;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002319 for (j = 0; j < 16; j++)
2320 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
2321 sfence();
2322}
2323
2324static int num_of_channels(const ramctr_timing * ctrl)
2325{
2326 int ret = 0;
2327 int channel;
2328 FOR_ALL_POPULATED_CHANNELS ret++;
2329 return ret;
2330}
2331
2332static void fill_pattern1(ramctr_timing * ctrl, int channel)
2333{
2334 unsigned j;
2335 unsigned channel_offset =
2336 get_precedening_channels(ctrl, channel) * 0x40;
2337 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2338 for (j = 0; j < 16; j++)
2339 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
2340 for (j = 0; j < 16; j++)
2341 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
2342 sfence();
2343}
2344
2345static void precharge(ramctr_timing * ctrl)
2346{
2347 int channel, slotrank, lane;
2348
2349 FOR_ALL_POPULATED_CHANNELS {
2350 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2351 ctrl->timings[channel][slotrank].lanes[lane].falling =
2352 16;
2353 ctrl->timings[channel][slotrank].lanes[lane].rising =
2354 16;
Patrick Rudolpha649a542016-01-17 18:32:06 +01002355 }
2356
2357 program_timings(ctrl, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002358
2359 FOR_ALL_POPULATED_RANKS {
2360 wait_428c(channel);
2361
Patrick Rudolph371d2912015-10-09 13:33:25 +02002362 /* DRAM command MRS
2363 * write MR3 MPR enable
2364 * in this mode only RD and RDA are allowed
2365 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002366 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2367 0x1f000);
2368 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2369 0xc01 | (ctrl->tMOD << 16));
2370 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2371 (slotrank << 24) | 0x360004);
2372 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2373
Patrick Rudolph371d2912015-10-09 13:33:25 +02002374 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002375 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2376 0x1f105);
2377 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2378 0x4041003);
2379 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2380 (slotrank << 24) | 0);
2381 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2382
Patrick Rudolph371d2912015-10-09 13:33:25 +02002383 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002384 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2385 0x1f105);
2386 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2387 0x1001 | ((ctrl->CAS + 8) << 16));
2388 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2389 (slotrank << 24) | 0x60000);
2390 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2391
Patrick Rudolph371d2912015-10-09 13:33:25 +02002392 /* DRAM command MRS
2393 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002394 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2395 0x1f000);
2396 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2397 0xc01 | (ctrl->tMOD << 16));
2398 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2399 (slotrank << 24) | 0x360000);
2400 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2401 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2402 0xc0001);
2403
2404 wait_428c(channel);
2405 }
2406
2407 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2408 ctrl->timings[channel][slotrank].lanes[lane].falling =
2409 48;
2410 ctrl->timings[channel][slotrank].lanes[lane].rising =
2411 48;
2412 }
2413
2414 program_timings(ctrl, channel);
2415
2416 FOR_ALL_POPULATED_RANKS {
2417 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002418 /* DRAM command MRS
2419 * write MR3 MPR enable
2420 * in this mode only RD and RDA are allowed
2421 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002422 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2423 0x1f000);
2424 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2425 0xc01 | (ctrl->tMOD << 16));
2426 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2427 (slotrank << 24) | 0x360004);
2428 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2429
Patrick Rudolph371d2912015-10-09 13:33:25 +02002430 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002431 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2432 0x1f105);
2433 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2434 0x4041003);
2435 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2436 (slotrank << 24) | 0);
2437 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2438
Patrick Rudolph371d2912015-10-09 13:33:25 +02002439 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002440 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2441 0x1f105);
2442 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2443 0x1001 | ((ctrl->CAS + 8) << 16));
2444 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2445 (slotrank << 24) | 0x60000);
2446 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2447
Patrick Rudolph371d2912015-10-09 13:33:25 +02002448 /* DRAM command MRS
2449 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002450 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2451 0x1f000);
2452 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2453 0xc01 | (ctrl->tMOD << 16));
2454
2455 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2456 (slotrank << 24) | 0x360000);
2457 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2458
2459 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2460 0xc0001);
2461 wait_428c(channel);
2462 }
2463 }
2464}
2465
2466static void test_timB(ramctr_timing * ctrl, int channel, int slotrank)
2467{
Patrick Rudolph371d2912015-10-09 13:33:25 +02002468 /* enable DQs on this slotrank */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002469 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002470 0x80 | make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002471
2472 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002473 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002474 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f207);
2475 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2476 0x8000c01 | ((ctrl->CWL + ctrl->tWLO) << 16));
2477 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2478 8 | (slotrank << 24));
2479 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2480
Patrick Rudolph371d2912015-10-09 13:33:25 +02002481 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002482 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f107);
2483 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2484 0x4000c01 | ((ctrl->CAS + 38) << 16));
2485 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2486 (slotrank << 24) | 4);
2487 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2488
2489 write32(DEFAULT_MCHBAR + 0x400 * channel + 0x4284, 0x40001);
2490 wait_428c(channel);
2491
Patrick Rudolph371d2912015-10-09 13:33:25 +02002492 /* disable DQs on this slotrank */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002493 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002494 0x1080 | make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002495}
2496
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002497static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002498{
2499 int timB;
2500 int statistics[NUM_LANES][128];
2501 int lane;
2502
2503 write32(DEFAULT_MCHBAR + 0x3400, 0x108052 | (slotrank << 2));
2504
2505 for (timB = 0; timB < 128; timB++) {
2506 FOR_ALL_LANES {
2507 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
2508 }
2509 program_timings(ctrl, channel);
2510
2511 test_timB(ctrl, channel, slotrank);
2512
2513 FOR_ALL_LANES {
2514 statistics[lane][timB] =
2515 !((read32
2516 (DEFAULT_MCHBAR + lane_registers[lane] +
2517 channel * 0x100 + 4 + ((timB / 32) & 1) * 4)
2518 >> (timB % 32)) & 1);
Patrick Rudolpha649a542016-01-17 18:32:06 +01002519 printram("Bstat: %d, %d, %d: %x, %x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002520 channel, slotrank, lane, timB,
2521 statistics[lane][timB]);
2522 }
2523 }
2524 FOR_ALL_LANES {
2525 struct run rn = get_longest_zero_run(statistics[lane], 128);
Patrick Rudolphd912f1d2016-02-15 20:07:42 +01002526 /* timC is a direct function of timB's 6 LSBs.
2527 * Some tests increments the value of timB by a small value,
2528 * which might cause the 6bit value to overflow, if it's close
2529 * to 0x3F. Increment the value by a small offset if it's likely
2530 * to overflow, to make sure it won't overflow while running
2531 * tests and bricks the system due to a non matching timC.
2532 *
2533 * TODO: find out why some tests (edge write discovery)
2534 * increment timB. */
2535 if ((rn.start & 0x3F) == 0x3E)
2536 rn.start += 2;
2537 else if ((rn.start & 0x3F) == 0x3F)
2538 rn.start += 1;
Vladimir Serbinenko3141eac2016-01-29 19:42:02 +01002539 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002540 if (rn.all) {
2541 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
2542 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002543 return MAKE_ERR;
2544 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01002545 printram("Bval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002546 lane, ctrl->timings[channel][slotrank].lanes[lane].timB);
2547 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002548 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002549}
2550
2551static int get_timB_high_adjust(u64 val)
2552{
2553 int i;
2554
2555 /* good */
2556 if (val == 0xffffffffffffffffLL)
2557 return 0;
2558
2559 if (val >= 0xf000000000000000LL) {
2560 /* needs negative adjustment */
2561 for (i = 0; i < 8; i++)
2562 if (val << (8 * (7 - i) + 4))
2563 return -i;
2564 } else {
2565 /* needs positive adjustment */
2566 for (i = 0; i < 8; i++)
2567 if (val >> (8 * (7 - i) + 4))
2568 return i;
2569 }
2570 return 8;
2571}
2572
2573static void adjust_high_timB(ramctr_timing * ctrl)
2574{
2575 int channel, slotrank, lane, old;
2576 write32(DEFAULT_MCHBAR + 0x3400, 0x200);
2577 FOR_ALL_POPULATED_CHANNELS {
2578 fill_pattern1(ctrl, channel);
2579 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 1);
2580 }
2581 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2582
2583 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x10001);
2584
2585 wait_428c(channel);
2586
Patrick Rudolph371d2912015-10-09 13:33:25 +02002587 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002588 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2589 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2590 0xc01 | (ctrl->tRCD << 16));
2591 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2592 (slotrank << 24) | 0x60000);
2593 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2594
Patrick Rudolph371d2912015-10-09 13:33:25 +02002595 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002596 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2597 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8040c01);
2598 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2599 (slotrank << 24) | 0x8);
2600 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2601
Patrick Rudolph371d2912015-10-09 13:33:25 +02002602 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002603 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2604 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x8041003);
2605 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2606 (slotrank << 24));
2607 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x3e2);
2608
Patrick Rudolph371d2912015-10-09 13:33:25 +02002609 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002610 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2611 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2612 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2613 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2614 (slotrank << 24) | 0x8);
2615 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2616
2617 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2618
2619 wait_428c(channel);
2620
Patrick Rudolph371d2912015-10-09 13:33:25 +02002621 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002622 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2623 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2624 0xc01 | ((ctrl->tRP) << 16));
2625 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2626 (slotrank << 24) | 0x60400);
2627 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2628
Patrick Rudolph371d2912015-10-09 13:33:25 +02002629 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002630 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2631 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2632 0xc01 | ((ctrl->tRCD) << 16));
2633 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2634 (slotrank << 24) | 0x60000);
2635 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2636
Patrick Rudolph371d2912015-10-09 13:33:25 +02002637 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002638 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x3f105);
2639 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2640 0x4000c01 |
2641 ((ctrl->tRP +
2642 ctrl->timings[channel][slotrank].val_4024 +
2643 ctrl->timings[channel][slotrank].val_4028) << 16));
2644 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2645 (slotrank << 24) | 0x60008);
2646 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2647
2648 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
2649 wait_428c(channel);
2650 FOR_ALL_LANES {
2651 u64 res =
2652 read32(DEFAULT_MCHBAR + lane_registers[lane] +
2653 0x100 * channel + 4);
2654 res |=
2655 ((u64) read32(DEFAULT_MCHBAR + lane_registers[lane] +
2656 0x100 * channel + 8)) << 32;
2657 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
2658 ctrl->timings[channel][slotrank].lanes[lane].timB +=
2659 get_timB_high_adjust(res) * 64;
2660
Patrick Rudolpha649a542016-01-17 18:32:06 +01002661 printram("High adjust %d:%016llx\n", lane, res);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002662 printram("Bval+: %d, %d, %d, %x -> %x\n", channel,
2663 slotrank, lane, old,
2664 ctrl->timings[channel][slotrank].lanes[lane].
2665 timB);
2666 }
2667 }
2668 write32(DEFAULT_MCHBAR + 0x3400, 0);
2669}
2670
2671static void write_op(ramctr_timing * ctrl, int channel)
2672{
2673 int slotrank;
2674
2675 wait_428c(channel);
2676
2677 /* choose an existing rank. */
2678 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2679
Patrick Rudolph371d2912015-10-09 13:33:25 +02002680 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002681 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2682 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2683
2684 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2685 (slotrank << 24) | 0x60000);
2686
2687 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2688
2689 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2690 wait_428c(channel);
2691}
2692
Patrick Rudolph371d2912015-10-09 13:33:25 +02002693/* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
2694 * DDR3 adopted the fly-by topology. The data and strobes signals reach
2695 * the chips at different times with respect to command, address and
2696 * clock signals.
2697 * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase
2698 * shift can be introduced.
2699 * It is assumed that the CLK/ADDR/CMD signals have the same routing delay.
2700 *
2701 * To find the required phase shift the DRAM is placed in "write leveling" mode.
2702 * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the
2703 * sampled value on the data lanes (DQs).
2704 */
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002705static int write_training(ramctr_timing * ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002706{
2707 int channel, slotrank, lane;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002708 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002709
2710 FOR_ALL_POPULATED_CHANNELS
2711 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
2712 read32(DEFAULT_MCHBAR + 0x4008 +
2713 0x400 * channel) | 0x8000000);
2714
2715 FOR_ALL_POPULATED_CHANNELS {
2716 write_op(ctrl, channel);
2717 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2718 read32(DEFAULT_MCHBAR + 0x4020 +
2719 0x400 * channel) | 0x200000);
2720 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002721
2722 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002723 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2724 FOR_ALL_POPULATED_CHANNELS {
2725 write_op(ctrl, channel);
2726 }
2727
Patrick Rudolph371d2912015-10-09 13:33:25 +02002728 /* enable write leveling on all ranks
2729 * disable all DQ outputs
2730 * only NOP is allowed in this mode */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002731 FOR_ALL_CHANNELS
2732 FOR_ALL_POPULATED_RANKS
2733 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002734 make_mr1(ctrl, slotrank, channel) | 0x1080);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002735
2736 write32(DEFAULT_MCHBAR + 0x3400, 0x108052);
2737
Patrick Rudolph9b515682015-10-09 13:43:51 +02002738 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002739
Patrick Rudolph371d2912015-10-09 13:33:25 +02002740 /* set any valid value for timB, it gets corrected later */
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002741 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2742 err = discover_timB(ctrl, channel, slotrank);
2743 if (err)
2744 return err;
2745 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002746
Patrick Rudolph371d2912015-10-09 13:33:25 +02002747 /* disable write leveling on all ranks */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002748 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2749 write_mrreg(ctrl, channel,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002750 slotrank, 1, make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002751
2752 write32(DEFAULT_MCHBAR + 0x3400, 0);
2753
2754 FOR_ALL_POPULATED_CHANNELS
2755 wait_428c(channel);
2756
Patrick Rudolph371d2912015-10-09 13:33:25 +02002757 /* refresh enable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002758 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) | 8);
2759
2760 FOR_ALL_POPULATED_CHANNELS {
2761 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2762 ~0x00200000 & read32(DEFAULT_MCHBAR + 0x4020 +
2763 0x400 * channel));
2764 read32(DEFAULT_MCHBAR + 0x428c + 0x400 * channel);
2765 wait_428c(channel);
2766
Patrick Rudolph371d2912015-10-09 13:33:25 +02002767 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002768 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2769 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
2770 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, 0x60000);
2771 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2772
2773 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2774 wait_428c(channel);
2775 }
2776
Patrick Rudolph9b515682015-10-09 13:43:51 +02002777 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002778
2779 printram("CPE\n");
2780 precharge(ctrl);
2781 printram("CPF\n");
2782
2783 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2784 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2785 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2786 0);
2787 }
2788
2789 FOR_ALL_POPULATED_CHANNELS {
2790 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
2791 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2792 }
2793
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002794 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2795 err = discover_timC(ctrl, channel, slotrank);
2796 if (err)
2797 return err;
2798 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002799
2800 FOR_ALL_POPULATED_CHANNELS
2801 program_timings(ctrl, channel);
2802
Patrick Rudolph371d2912015-10-09 13:33:25 +02002803 /* measure and adjust timB timings */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002804 adjust_high_timB(ctrl);
2805
2806 FOR_ALL_POPULATED_CHANNELS
2807 program_timings(ctrl, channel);
2808
2809 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2810 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2811 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2812 0);
2813 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002814 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002815}
2816
2817static int test_320c(ramctr_timing * ctrl, int channel, int slotrank)
2818{
2819 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2820 int timC_delta;
2821 int lanes_ok = 0;
2822 int ctr = 0;
2823 int lane;
2824
2825 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2826 FOR_ALL_LANES {
2827 ctrl->timings[channel][slotrank].lanes[lane].timC =
2828 saved_rt.lanes[lane].timC + timC_delta;
2829 }
2830 program_timings(ctrl, channel);
2831 FOR_ALL_LANES {
2832 write32(DEFAULT_MCHBAR + 4 * lane + 0x4f40, 0);
2833 }
2834
2835 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2836
2837 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002838 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002839 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2840 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2841 ((max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1)) << 10)
2842 | 8 | (ctrl->tRCD << 16));
2843
2844 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2845 (slotrank << 24) | ctr | 0x60000);
2846
2847 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002848 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002849 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2850 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2851 0x8001020 | ((ctrl->CWL + ctrl->tWTR + 8) << 16));
2852 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2853 (slotrank << 24));
2854 write32(DEFAULT_MCHBAR + 0x4244 + 0x400 * channel, 0x389abcd);
2855 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x20e42);
2856
Patrick Rudolph371d2912015-10-09 13:33:25 +02002857 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002858 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2859 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2860 0x4001020 | (max(ctrl->tRTP, 8) << 16));
2861 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2862 (slotrank << 24));
2863 write32(DEFAULT_MCHBAR + 0x4248 + 0x400 * channel, 0x389abcd);
2864 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x20e42);
2865
Patrick Rudolph371d2912015-10-09 13:33:25 +02002866 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002867 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2868 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, 0xf1001);
2869 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2870 (slotrank << 24) | 0x60400);
2871 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2872
2873 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2874 wait_428c(channel);
2875 FOR_ALL_LANES {
2876 u32 r32 =
2877 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2878 0x400 * channel);
2879
2880 if (r32 == 0)
2881 lanes_ok |= 1 << lane;
2882 }
2883 ctr++;
2884 if (lanes_ok == ((1 << NUM_LANES) - 1))
2885 break;
2886 }
2887
2888 ctrl->timings[channel][slotrank] = saved_rt;
2889
2890 printram("3lanes: %x\n", lanes_ok);
2891 return lanes_ok != ((1 << NUM_LANES) - 1);
2892}
2893
2894#include "raminit_patterns.h"
2895
2896static void fill_pattern5(ramctr_timing * ctrl, int channel, int patno)
2897{
2898 unsigned i, j;
2899 unsigned channel_offset =
2900 get_precedening_channels(ctrl, channel) * 0x40;
2901 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2902
2903 if (patno) {
2904 u8 base8 = 0x80 >> ((patno - 1) % 8);
2905 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2906 for (i = 0; i < 32; i++) {
2907 for (j = 0; j < 16; j++) {
2908 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
2909 if (invert[patno - 1][i] & (1 << (j / 2)))
2910 val = ~val;
2911 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2912 j * 4), val);
2913 }
2914 }
2915
2916 } else {
2917 for (i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++) {
2918 for (j = 0; j < 16; j++)
2919 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2920 j * 4), pattern[i][j]);
2921 }
2922 sfence();
2923 }
2924}
2925
2926static void reprogram_320c(ramctr_timing * ctrl)
2927{
2928 int channel, slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002929
2930 FOR_ALL_POPULATED_CHANNELS {
2931 wait_428c(channel);
2932
2933 /* choose an existing rank. */
2934 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2935
Patrick Rudolph371d2912015-10-09 13:33:25 +02002936 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002937 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2938 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2939
2940 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2941 (slotrank << 24) | 0x60000);
2942
2943 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2944
2945 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2946 wait_428c(channel);
2947 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2948 read32(DEFAULT_MCHBAR + 0x4020 +
2949 0x400 * channel) | 0x200000);
2950 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002951
2952 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002953 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2954 FOR_ALL_POPULATED_CHANNELS {
2955 wait_428c(channel);
2956
2957 /* choose an existing rank. */
2958 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2959
Patrick Rudolph371d2912015-10-09 13:33:25 +02002960 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002961 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2962 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2963
2964 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2965 (slotrank << 24) | 0x60000);
2966
2967 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2968
2969 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2970 wait_428c(channel);
2971 }
2972
2973 /* jedec reset */
2974 dram_jedecreset(ctrl);
2975 /* mrs commands. */
2976 dram_mrscommands(ctrl);
2977
Patrick Rudolph9b515682015-10-09 13:43:51 +02002978 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002979}
2980
2981#define MIN_C320C_LEN 13
2982
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002983static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002984{
2985 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002986 int slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002987 int c320c;
2988 int stat[NUM_SLOTRANKS][256];
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002989 int delta = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002990
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002991 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2992
2993 FOR_ALL_POPULATED_RANKS {
2994 saved_timings[channel][slotrank] =
2995 ctrl->timings[channel][slotrank];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002996 }
2997
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002998 ctrl->cmd_stretch[channel] = cmd_stretch;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002999
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003000 MCHBAR32(0x4004 + 0x400 * channel) =
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003001 ctrl->tRRD
3002 | (ctrl->tRTP << 4)
3003 | (ctrl->tCKE << 8)
3004 | (ctrl->tWTR << 12)
3005 | (ctrl->tFAW << 16)
3006 | (ctrl->tWR << 24)
3007 | (ctrl->cmd_stretch[channel] << 30);
3008
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003009 if (ctrl->cmd_stretch[channel] == 2)
3010 delta = 2;
3011 else if (ctrl->cmd_stretch[channel] == 0)
3012 delta = 4;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003013
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003014 FOR_ALL_POPULATED_RANKS {
3015 ctrl->timings[channel][slotrank].val_4024 -= delta;
3016 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003017
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003018 for (c320c = -127; c320c <= 127; c320c++) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003019 FOR_ALL_POPULATED_RANKS {
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003020 ctrl->timings[channel][slotrank].val_320c = c320c;
3021 }
3022 program_timings(ctrl, channel);
3023 reprogram_320c(ctrl);
3024 FOR_ALL_POPULATED_RANKS {
3025 stat[slotrank][c320c + 127] =
3026 test_320c(ctrl, channel, slotrank);
3027 printram("3stat: %d, %d, %d: %x\n",
3028 channel, slotrank, c320c,
3029 stat[slotrank][c320c + 127]);
3030 }
3031 }
3032 FOR_ALL_POPULATED_RANKS {
3033 struct run rn =
3034 get_longest_zero_run(stat[slotrank], 255);
3035 ctrl->timings[channel][slotrank].val_320c =
3036 rn.middle - 127;
3037 printram("3val: %d, %d: %d\n", channel,
3038 slotrank,
3039 ctrl->timings[channel][slotrank].val_320c);
3040 if (rn.all || rn.length < MIN_C320C_LEN) {
3041 FOR_ALL_POPULATED_RANKS {
3042 ctrl->timings[channel][slotrank] =
3043 saved_timings[channel][slotrank];
3044 }
3045 return MAKE_ERR;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003046 }
3047 }
3048
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003049 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003050}
3051
Patrick Rudolph371d2912015-10-09 13:33:25 +02003052/* Adjust CMD phase shift and try multiple command rates.
3053 * A command rate of 2T doubles the time needed for address and
3054 * command decode. */
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003055static int command_training(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003056{
3057 int channel;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003058 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003059
3060 FOR_ALL_POPULATED_CHANNELS {
3061 fill_pattern5(ctrl, channel, 0);
3062 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
3063 }
3064
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003065 FOR_ALL_POPULATED_CHANNELS {
3066 /* try command rate 1T and 2T */
3067 err = try_cmd_stretch(ctrl, channel, 0);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003068 if (err) {
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003069 err = try_cmd_stretch(ctrl, channel, 2);
3070 if (err) {
3071 printk(BIOS_EMERG, "c320c discovery failed\n");
3072 return err;
3073 }
3074 printram("Using CMD rate 2T on channel %u\n", channel);
3075 } else
3076 printram("Using CMD rate 1T on channel %u\n", channel);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003077 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003078
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003079 FOR_ALL_POPULATED_CHANNELS
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003080 program_timings(ctrl, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003081
3082 reprogram_320c(ctrl);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003083 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003084}
3085
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003086static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003087 int *edges)
3088{
3089 int edge;
3090 int statistics[NUM_LANES][MAX_EDGE_TIMING + 1];
3091 int lane;
3092
3093 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
3094 FOR_ALL_LANES {
3095 ctrl->timings[channel][slotrank].lanes[lane].rising =
3096 edge;
3097 ctrl->timings[channel][slotrank].lanes[lane].falling =
3098 edge;
3099 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003100 program_timings(ctrl, channel);
3101
3102 FOR_ALL_LANES {
3103 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
3104 4 * lane, 0);
3105 read32(DEFAULT_MCHBAR + 0x400 * channel + 4 * lane +
3106 0x4140);
3107 }
3108
3109 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003110 /* DRAM command MRS
3111 * write MR3 MPR enable
3112 * in this mode only RD and RDA are allowed
3113 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003114 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
3115 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3116 (0xc01 | (ctrl->tMOD << 16)));
3117 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3118 (slotrank << 24) | 0x360004);
3119 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3120
Patrick Rudolph371d2912015-10-09 13:33:25 +02003121 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003122 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
3123 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x40411f4);
3124 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3125 (slotrank << 24));
3126 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3127
Patrick Rudolph371d2912015-10-09 13:33:25 +02003128 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003129 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
3130 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3131 0x1001 | ((ctrl->CAS + 8) << 16));
3132 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3133 (slotrank << 24) | 0x60000);
3134 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3135
Patrick Rudolph371d2912015-10-09 13:33:25 +02003136 /* DRAM command MRS
3137 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003138 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
3139 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3140 (0xc01 | (ctrl->tMOD << 16)));
3141 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3142 (slotrank << 24) | 0x360000);
3143 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3144
3145 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
3146
3147 wait_428c(channel);
3148
3149 FOR_ALL_LANES {
3150 statistics[lane][edge] =
3151 read32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
3152 lane * 4);
3153 }
3154 }
3155 FOR_ALL_LANES {
3156 struct run rn =
3157 get_longest_zero_run(statistics[lane], MAX_EDGE_TIMING + 1);
3158 edges[lane] = rn.middle;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003159 if (rn.all) {
3160 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n",
3161 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003162 return MAKE_ERR;
3163 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01003164 printram("eval %d, %d, %d: %02x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003165 lane, edges[lane]);
3166 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003167 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003168}
3169
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003170static int discover_edges(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003171{
3172 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3173 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3174 int channel, slotrank, lane;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003175 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003176
3177 write32(DEFAULT_MCHBAR + 0x3400, 0);
3178
Patrick Rudolph9b515682015-10-09 13:43:51 +02003179 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003180
3181 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
3182 write32(DEFAULT_MCHBAR + 4 * lane +
3183 0x400 * channel + 0x4080, 0);
3184 }
3185
3186 FOR_ALL_POPULATED_CHANNELS {
3187 fill_pattern0(ctrl, channel, 0, 0);
3188 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3189 FOR_ALL_LANES {
3190 read32(DEFAULT_MCHBAR + 0x400 * channel +
3191 lane * 4 + 0x4140);
3192 }
3193
3194 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3195 ctrl->timings[channel][slotrank].lanes[lane].falling =
3196 16;
3197 ctrl->timings[channel][slotrank].lanes[lane].rising =
3198 16;
3199 }
3200
3201 program_timings(ctrl, channel);
3202
3203 FOR_ALL_POPULATED_RANKS {
3204 wait_428c(channel);
3205
Patrick Rudolph371d2912015-10-09 13:33:25 +02003206 /* DRAM command MRS
3207 * MR3 enable MPR
3208 * write MR3 MPR enable
3209 * in this mode only RD and RDA are allowed
3210 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003211 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3212 0x1f000);
3213 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3214 0xc01 | (ctrl->tMOD << 16));
3215 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3216 (slotrank << 24) | 0x360004);
3217 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3218
Patrick Rudolph371d2912015-10-09 13:33:25 +02003219 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003220 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3221 0x1f105);
3222 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3223 0x4041003);
3224 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3225 (slotrank << 24) | 0);
3226 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3227
Patrick Rudolph371d2912015-10-09 13:33:25 +02003228 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003229 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3230 0x1f105);
3231 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3232 0x1001 | ((ctrl->CAS + 8) << 16));
3233 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3234 (slotrank << 24) | 0x60000);
3235 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3236
Patrick Rudolph371d2912015-10-09 13:33:25 +02003237 /* DRAM command MRS
3238 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003239 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3240 0x1f000);
3241 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3242 0xc01 | (ctrl->tMOD << 16));
3243 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3244 (slotrank << 24) | 0x360000);
3245 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3246 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3247 0xc0001);
3248
3249 wait_428c(channel);
3250 }
3251
Patrick Rudolph371d2912015-10-09 13:33:25 +02003252 /* XXX: check any measured value ? */
3253
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003254 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3255 ctrl->timings[channel][slotrank].lanes[lane].falling =
3256 48;
3257 ctrl->timings[channel][slotrank].lanes[lane].rising =
3258 48;
3259 }
3260
3261 program_timings(ctrl, channel);
3262
3263 FOR_ALL_POPULATED_RANKS {
3264 wait_428c(channel);
3265
Patrick Rudolph371d2912015-10-09 13:33:25 +02003266 /* DRAM command MRS
3267 * MR3 enable MPR
3268 * write MR3 MPR enable
3269 * in this mode only RD and RDA are allowed
3270 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003271 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3272 0x1f000);
3273 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3274 0xc01 | (ctrl->tMOD << 16));
3275 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3276 (slotrank << 24) | 0x360004);
3277 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3278
Patrick Rudolph371d2912015-10-09 13:33:25 +02003279 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003280 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3281 0x1f105);
3282 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3283 0x4041003);
3284 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3285 (slotrank << 24) | 0);
3286 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3287
Patrick Rudolph371d2912015-10-09 13:33:25 +02003288 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003289 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3290 0x1f105);
3291 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3292 0x1001 | ((ctrl->CAS + 8) << 16));
3293 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3294 (slotrank << 24) | 0x60000);
3295 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3296
Patrick Rudolph371d2912015-10-09 13:33:25 +02003297 /* DRAM command MRS
3298 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003299 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3300 0x1f000);
3301 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3302 0xc01 | (ctrl->tMOD << 16));
3303 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3304 (slotrank << 24) | 0x360000);
3305 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3306
3307 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3308 0xc0001);
3309 wait_428c(channel);
3310 }
3311
Patrick Rudolph371d2912015-10-09 13:33:25 +02003312 /* XXX: check any measured value ? */
3313
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003314 FOR_ALL_LANES {
3315 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel +
3316 lane * 4,
3317 ~read32(DEFAULT_MCHBAR + 0x4040 +
3318 0x400 * channel + lane * 4) & 0xff);
3319 }
3320
3321 fill_pattern0(ctrl, channel, 0, 0xffffffff);
3322 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3323 }
3324
3325 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3326 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003327 printram("discover falling edges:\n[%x] = %x\n", 0x4eb0, 0x300);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003328
3329 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003330 err = discover_edges_real(ctrl, channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003331 falling_edges[channel][slotrank]);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003332 if (err)
3333 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003334 }
3335
3336 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003337 printram("discover rising edges:\n[%x] = %x\n", 0x4eb0, 0x200);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003338
3339 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003340 err = discover_edges_real(ctrl, channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003341 rising_edges[channel][slotrank]);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003342 if (err)
3343 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003344 }
3345
3346 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3347
3348 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3349 ctrl->timings[channel][slotrank].lanes[lane].falling =
3350 falling_edges[channel][slotrank][lane];
3351 ctrl->timings[channel][slotrank].lanes[lane].rising =
3352 rising_edges[channel][slotrank][lane];
3353 }
3354
3355 FOR_ALL_POPULATED_CHANNELS {
3356 program_timings(ctrl, channel);
3357 }
3358
3359 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3360 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3361 0);
3362 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003363 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003364}
3365
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003366static int discover_edges_write_real(ramctr_timing *ctrl, int channel,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003367 int slotrank, int *edges)
3368{
3369 int edge;
3370 u32 raw_statistics[MAX_EDGE_TIMING + 1];
3371 int statistics[MAX_EDGE_TIMING + 1];
3372 const int reg3000b24[] = { 0, 0xc, 0x2c };
3373 int lane, i;
3374 int lower[NUM_LANES];
3375 int upper[NUM_LANES];
3376 int pat;
3377
3378 FOR_ALL_LANES {
3379 lower[lane] = 0;
3380 upper[lane] = MAX_EDGE_TIMING;
3381 }
3382
3383 for (i = 0; i < 3; i++) {
3384 write32(DEFAULT_MCHBAR + 0x3000 + 0x100 * channel,
3385 reg3000b24[i] << 24);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003386 printram("[%x] = 0x%08x\n",
3387 0x3000 + 0x100 * channel, reg3000b24[i] << 24);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003388 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3389 fill_pattern5(ctrl, channel, pat);
3390 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003391 printram("using pattern %d\n", pat);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003392 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
3393 FOR_ALL_LANES {
3394 ctrl->timings[channel][slotrank].lanes[lane].
3395 rising = edge;
3396 ctrl->timings[channel][slotrank].lanes[lane].
3397 falling = edge;
3398 }
3399 program_timings(ctrl, channel);
3400
3401 FOR_ALL_LANES {
3402 write32(DEFAULT_MCHBAR + 0x4340 +
3403 0x400 * channel + 4 * lane, 0);
3404 read32(DEFAULT_MCHBAR + 0x400 * channel +
3405 4 * lane + 0x4140);
3406 }
3407 wait_428c(channel);
3408
Patrick Rudolph371d2912015-10-09 13:33:25 +02003409 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003410 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3411 0x1f006);
3412 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3413 0x4 | (ctrl->tRCD << 16)
3414 | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) <<
3415 10));
3416 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3417 (slotrank << 24) | 0x60000);
3418 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel,
3419 0x240);
3420
Patrick Rudolph371d2912015-10-09 13:33:25 +02003421 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003422 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3423 0x1f201);
3424 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3425 0x8005020 | ((ctrl->tWTR + ctrl->CWL + 8) <<
3426 16));
3427 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3428 (slotrank << 24));
3429 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel,
3430 0x242);
3431
Patrick Rudolph371d2912015-10-09 13:33:25 +02003432 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003433 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3434 0x1f105);
3435 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3436 0x4005020 | (max(ctrl->tRTP, 8) << 16));
3437 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3438 (slotrank << 24));
3439 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel,
3440 0x242);
3441
Patrick Rudolph371d2912015-10-09 13:33:25 +02003442 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003443 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3444 0x1f002);
3445 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3446 0xc01 | (ctrl->tRP << 16));
3447 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3448 (slotrank << 24) | 0x60400);
3449 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3450
3451 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3452 0xc0001);
3453 wait_428c(channel);
3454 FOR_ALL_LANES {
3455 read32(DEFAULT_MCHBAR + 0x4340 +
3456 0x400 * channel + lane * 4);
3457 }
3458
3459 raw_statistics[edge] =
3460 MCHBAR32(0x436c + 0x400 * channel);
3461 }
3462 FOR_ALL_LANES {
3463 struct run rn;
3464 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
3465 statistics[edge] =
3466 ! !(raw_statistics[edge] & (1 << lane));
3467 rn = get_longest_zero_run(statistics,
3468 MAX_EDGE_TIMING + 1);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003469 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003470 channel, slotrank, i, rn.start, rn.middle,
3471 rn.end, rn.start + ctrl->edge_offset[i],
3472 rn.end - ctrl->edge_offset[i]);
3473 lower[lane] =
3474 max(rn.start + ctrl->edge_offset[i], lower[lane]);
3475 upper[lane] =
3476 min(rn.end - ctrl->edge_offset[i], upper[lane]);
3477 edges[lane] = (lower[lane] + upper[lane]) / 2;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003478 if (rn.all || (lower[lane] > upper[lane])) {
3479 printk(BIOS_EMERG, "edge write discovery failed: %d, %d, %d\n",
3480 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003481 return MAKE_ERR;
3482 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003483 }
3484 }
3485 }
3486
3487 write32(DEFAULT_MCHBAR + 0x3000, 0);
3488 printram("CPA\n");
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003489 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003490}
3491
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003492static int discover_edges_write(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003493{
3494 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3495 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3496 int channel, slotrank, lane;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003497 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003498
3499 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3500 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003501 printram("discover falling edges write:\n[%x] = %x\n", 0x4eb0, 0x300);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003502
3503 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003504 err = discover_edges_write_real(ctrl, channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003505 falling_edges[channel][slotrank]);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003506 if (err)
3507 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003508 }
3509
3510 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003511 printram("discover rising edges write:\n[%x] = %x\n", 0x4eb0, 0x200);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003512
3513 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003514 err = discover_edges_write_real(ctrl, channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003515 rising_edges[channel][slotrank]);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003516 if (err)
3517 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003518 }
3519
3520 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3521
3522 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3523 ctrl->timings[channel][slotrank].lanes[lane].falling =
3524 falling_edges[channel][slotrank][lane];
3525 ctrl->timings[channel][slotrank].lanes[lane].rising =
3526 rising_edges[channel][slotrank][lane];
3527 }
3528
3529 FOR_ALL_POPULATED_CHANNELS
3530 program_timings(ctrl, channel);
3531
3532 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3533 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3534 0);
3535 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003536 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003537}
3538
3539static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
3540{
3541 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003542 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003543 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
3544 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3545 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD)
3546 << 10) | (ctrl->tRCD << 16) | 4);
3547 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3548 (slotrank << 24) | 0x60000);
3549 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
3550
Patrick Rudolph371d2912015-10-09 13:33:25 +02003551 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003552 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
3553 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3554 0x80011e0 |
3555 ((ctrl->tWTR + ctrl->CWL + 8) << 16));
3556 write32(DEFAULT_MCHBAR + 0x4204 +
3557 0x400 * channel, (slotrank << 24));
3558 write32(DEFAULT_MCHBAR + 0x4214 +
3559 0x400 * channel, 0x242);
3560
Patrick Rudolph371d2912015-10-09 13:33:25 +02003561 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003562 write32(DEFAULT_MCHBAR + 0x4228 +
3563 0x400 * channel, 0x1f105);
3564 write32(DEFAULT_MCHBAR + 0x4238 +
3565 0x400 * channel,
3566 0x40011e0 | (max(ctrl->tRTP, 8) << 16));
3567 write32(DEFAULT_MCHBAR + 0x4208 +
3568 0x400 * channel, (slotrank << 24));
3569 write32(DEFAULT_MCHBAR + 0x4218 +
3570 0x400 * channel, 0x242);
3571
Patrick Rudolph371d2912015-10-09 13:33:25 +02003572 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003573 write32(DEFAULT_MCHBAR + 0x422c +
3574 0x400 * channel, 0x1f002);
3575 write32(DEFAULT_MCHBAR + 0x423c +
3576 0x400 * channel,
3577 0x1001 | (ctrl->tRP << 16));
3578 write32(DEFAULT_MCHBAR + 0x420c +
3579 0x400 * channel,
3580 (slotrank << 24) | 0x60400);
3581 write32(DEFAULT_MCHBAR + 0x421c +
3582 0x400 * channel, 0);
3583
3584 write32(DEFAULT_MCHBAR + 0x4284 +
3585 0x400 * channel, 0xc0001);
3586 wait_428c(channel);
3587}
3588
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003589static int discover_timC_write(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003590{
3591 const u8 rege3c_b24[3] = { 0, 0xf, 0x2f };
3592 int i, pat;
3593
3594 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3595 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3596 int channel, slotrank, lane;
3597
3598 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3599 lower[channel][slotrank][lane] = 0;
3600 upper[channel][slotrank][lane] = MAX_TIMC;
3601 }
3602
3603 write32(DEFAULT_MCHBAR + 0x4ea8, 1);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003604 printram("discover timC write:\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003605
3606 for (i = 0; i < 3; i++)
3607 FOR_ALL_POPULATED_CHANNELS {
3608 write32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100),
3609 (rege3c_b24[i] << 24)
3610 | (read32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100))
3611 & ~0x3f000000));
3612 udelay(2);
3613 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3614 FOR_ALL_POPULATED_RANKS {
3615 int timC;
3616 u32 raw_statistics[MAX_TIMC + 1];
3617 int statistics[MAX_TIMC + 1];
3618
Patrick Rudolph77e45d32016-02-28 16:14:45 +01003619 /* Make sure rn.start < rn.end */
3620 statistics[MAX_TIMC] = 1;
3621
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003622 fill_pattern5(ctrl, channel, pat);
3623 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
Patrick Rudolph77e45d32016-02-28 16:14:45 +01003624 for (timC = 0; timC < MAX_TIMC; timC++) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003625 FOR_ALL_LANES
3626 ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
3627 program_timings(ctrl, channel);
3628
3629 test_timC_write (ctrl, channel, slotrank);
3630
3631 raw_statistics[timC] =
3632 MCHBAR32(0x436c + 0x400 * channel);
3633 }
3634 FOR_ALL_LANES {
3635 struct run rn;
Patrick Rudolph77e45d32016-02-28 16:14:45 +01003636 for (timC = 0; timC < MAX_TIMC; timC++)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003637 statistics[timC] =
3638 !!(raw_statistics[timC] &
3639 (1 << lane));
Patrick Rudolph77e45d32016-02-28 16:14:45 +01003640
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003641 rn = get_longest_zero_run(statistics,
3642 MAX_TIMC + 1);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003643 if (rn.all) {
3644 printk(BIOS_EMERG, "timC write discovery failed: %d, %d, %d\n",
3645 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003646 return MAKE_ERR;
3647 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01003648 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003649 channel, slotrank, i, rn.start,
3650 rn.middle, rn.end,
3651 rn.start + ctrl->timC_offset[i],
3652 rn.end - ctrl->timC_offset[i]);
3653 lower[channel][slotrank][lane] =
3654 max(rn.start + ctrl->timC_offset[i],
3655 lower[channel][slotrank][lane]);
3656 upper[channel][slotrank][lane] =
3657 min(rn.end - ctrl->timC_offset[i],
3658 upper[channel][slotrank][lane]);
3659
3660 }
3661 }
3662 }
3663 }
3664
3665 FOR_ALL_CHANNELS {
3666 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3667 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3668 ~0x3f000000));
3669 udelay(2);
3670 }
3671
3672 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3673
3674 printram("CPB\n");
3675
3676 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Patrick Rudolpha649a542016-01-17 18:32:06 +01003677 printram("timC %d, %d, %d: %x\n", channel,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003678 slotrank, lane,
3679 (lower[channel][slotrank][lane] +
3680 upper[channel][slotrank][lane]) / 2);
3681 ctrl->timings[channel][slotrank].lanes[lane].timC =
3682 (lower[channel][slotrank][lane] +
3683 upper[channel][slotrank][lane]) / 2;
3684 }
3685 FOR_ALL_POPULATED_CHANNELS {
3686 program_timings(ctrl, channel);
3687 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003688 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003689}
3690
3691static void normalize_training(ramctr_timing * ctrl)
3692{
3693 int channel, slotrank, lane;
3694 int mat = 0;
3695
3696 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3697 int delta;
3698 FOR_ALL_LANES mat =
3699 max(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
3700 delta = (mat >> 6) - ctrl->timings[channel][slotrank].val_4028;
3701 ctrl->timings[channel][slotrank].val_4024 += delta;
3702 ctrl->timings[channel][slotrank].val_4028 += delta;
3703 }
3704
3705 FOR_ALL_POPULATED_CHANNELS {
3706 program_timings(ctrl, channel);
3707 }
3708}
3709
3710static void write_controller_mr(ramctr_timing * ctrl)
3711{
3712 int channel, slotrank;
3713
3714 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3715 write32(DEFAULT_MCHBAR + 0x0004 + (channel << 8) +
3716 lane_registers[slotrank], make_mr0(ctrl, slotrank));
3717 write32(DEFAULT_MCHBAR + 0x0008 + (channel << 8) +
Patrick Rudolph7e513d12016-01-10 14:22:34 +01003718 lane_registers[slotrank],
3719 make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003720 }
3721}
3722
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003723static int channel_test(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003724{
3725 int channel, slotrank, lane;
3726
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003727 slotrank = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003728 FOR_ALL_POPULATED_CHANNELS
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003729 if (read32(DEFAULT_MCHBAR + 0x42a0 + (channel << 10)) & 0xa000) {
3730 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n",
3731 channel);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003732 return MAKE_ERR;
3733 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003734 FOR_ALL_POPULATED_CHANNELS {
3735 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
3736
3737 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3738 }
3739
3740 for (slotrank = 0; slotrank < 4; slotrank++)
3741 FOR_ALL_CHANNELS
3742 if (ctrl->rankmap[channel] & (1 << slotrank)) {
3743 FOR_ALL_LANES {
3744 write32(DEFAULT_MCHBAR + (0x4f40 + 4 * lane), 0);
3745 write32(DEFAULT_MCHBAR + (0x4d40 + 4 * lane), 0);
3746 }
3747 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003748 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003749 write32(DEFAULT_MCHBAR + 0x4220 + (channel << 10), 0x0001f006);
3750 write32(DEFAULT_MCHBAR + 0x4230 + (channel << 10), 0x0028a004);
3751 write32(DEFAULT_MCHBAR + 0x4200 + (channel << 10),
3752 0x00060000 | (slotrank << 24));
3753 write32(DEFAULT_MCHBAR + 0x4210 + (channel << 10), 0x00000244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003754 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003755 write32(DEFAULT_MCHBAR + 0x4224 + (channel << 10), 0x0001f201);
3756 write32(DEFAULT_MCHBAR + 0x4234 + (channel << 10), 0x08281064);
3757 write32(DEFAULT_MCHBAR + 0x4204 + (channel << 10),
3758 0x00000000 | (slotrank << 24));
3759 write32(DEFAULT_MCHBAR + 0x4214 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003760 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003761 write32(DEFAULT_MCHBAR + 0x4228 + (channel << 10), 0x0001f105);
3762 write32(DEFAULT_MCHBAR + 0x4238 + (channel << 10), 0x04281064);
3763 write32(DEFAULT_MCHBAR + 0x4208 + (channel << 10),
3764 0x00000000 | (slotrank << 24));
3765 write32(DEFAULT_MCHBAR + 0x4218 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003766 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003767 write32(DEFAULT_MCHBAR + 0x422c + (channel << 10), 0x0001f002);
3768 write32(DEFAULT_MCHBAR + 0x423c + (channel << 10), 0x00280c01);
3769 write32(DEFAULT_MCHBAR + 0x420c + (channel << 10),
3770 0x00060400 | (slotrank << 24));
3771 write32(DEFAULT_MCHBAR + 0x421c + (channel << 10), 0x00000240);
3772 write32(DEFAULT_MCHBAR + 0x4284 + (channel << 10), 0x000c0001);
3773 wait_428c(channel);
3774 FOR_ALL_LANES
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003775 if (read32(DEFAULT_MCHBAR + 0x4340 + (channel << 10) + 4 * lane)) {
3776 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
3777 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003778 return MAKE_ERR;
3779 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003780 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003781 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003782}
3783
3784static void set_scrambling_seed(ramctr_timing * ctrl)
3785{
3786 int channel;
3787
3788 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them?
3789 I don't think so. */
3790 static u32 seeds[NUM_CHANNELS][3] = {
3791 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
3792 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
3793 };
3794 FOR_ALL_POPULATED_CHANNELS {
3795 MCHBAR32(0x4020 + 0x400 * channel) &= ~0x10000000;
3796 write32(DEFAULT_MCHBAR + 0x4034, seeds[channel][0]);
3797 write32(DEFAULT_MCHBAR + 0x403c, seeds[channel][1]);
3798 write32(DEFAULT_MCHBAR + 0x4038, seeds[channel][2]);
3799 }
3800}
3801
3802static void set_4f8c(void)
3803{
3804 struct cpuid_result cpures;
3805 u32 cpu;
3806
Ryan Salsamendie4da9aa2016-06-24 12:01:11 -07003807 cpures = cpuid(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003808 cpu = (cpures.eax);
3809 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
3810 MCHBAR32(0x4f8c) = 0x141D1519;
3811 } else {
3812 MCHBAR32(0x4f8c) = 0x551D1519;
3813 }
3814}
3815
3816static void prepare_training(ramctr_timing * ctrl)
3817{
3818 int channel;
3819
3820 FOR_ALL_POPULATED_CHANNELS {
3821 // Always drive command bus
3822 MCHBAR32(0x4004 + 0x400 * channel) |= 0x20000000;
3823 }
3824
3825 udelay(1);
3826
3827 FOR_ALL_POPULATED_CHANNELS {
3828 wait_428c(channel);
3829 }
3830}
3831
3832static void set_4008c(ramctr_timing * ctrl)
3833{
3834 int channel, slotrank;
3835 u32 reg;
3836 FOR_ALL_POPULATED_CHANNELS {
3837 u32 b20, b4_8_12;
3838 int min_320c = 10000;
3839 int max_320c = -10000;
3840
3841 FOR_ALL_POPULATED_RANKS {
3842 max_320c = max(ctrl->timings[channel][slotrank].val_320c, max_320c);
3843 min_320c = min(ctrl->timings[channel][slotrank].val_320c, min_320c);
3844 }
3845
3846 if (max_320c - min_320c > 51)
3847 b20 = 0;
3848 else
3849 b20 = ctrl->ref_card_offset[channel];
3850
3851 if (ctrl->reg_320c_range_threshold < max_320c - min_320c)
3852 b4_8_12 = 0x3330;
3853 else
3854 b4_8_12 = 0x2220;
3855
3856 reg = read32(DEFAULT_MCHBAR + 0x400c + (channel << 10));
3857 write32(DEFAULT_MCHBAR + 0x400c + (channel << 10),
3858 (reg & 0xFFF0FFFF)
3859 | (ctrl->ref_card_offset[channel] << 16)
3860 | (ctrl->ref_card_offset[channel] << 18));
3861 write32(DEFAULT_MCHBAR + 0x4008 + (channel << 10),
3862 0x0a000000
3863 | (b20 << 20)
3864 | ((ctrl->ref_card_offset[channel] + 2) << 16)
3865 | b4_8_12);
3866 }
3867}
3868
3869static void set_42a0(ramctr_timing * ctrl)
3870{
3871 int channel;
3872 FOR_ALL_POPULATED_CHANNELS {
3873 write32(DEFAULT_MCHBAR + (0x42a0 + 0x400 * channel),
3874 0x00001000 | ctrl->rankmap[channel]);
3875 MCHBAR32(0x4004 + 0x400 * channel) &= ~0x20000000; // OK
3876 }
3877}
3878
3879static int encode_5d10(int ns)
3880{
3881 return (ns + 499) / 500;
3882}
3883
3884/* FIXME: values in this function should be hardware revision-dependent. */
3885static void final_registers(ramctr_timing * ctrl)
3886{
3887 int channel;
3888 int t1_cycles = 0, t1_ns = 0, t2_ns;
3889 int t3_ns;
3890 u32 r32;
3891
3892 write32(DEFAULT_MCHBAR + 0x4cd4, 0x00000046);
3893
3894 write32(DEFAULT_MCHBAR + 0x400c, (read32(DEFAULT_MCHBAR + 0x400c) & 0xFFFFCFFF) | 0x1000); // OK
3895 write32(DEFAULT_MCHBAR + 0x440c, (read32(DEFAULT_MCHBAR + 0x440c) & 0xFFFFCFFF) | 0x1000); // OK
3896 write32(DEFAULT_MCHBAR + 0x4cb0, 0x00000740);
3897 write32(DEFAULT_MCHBAR + 0x4380, 0x00000aaa); // OK
3898 write32(DEFAULT_MCHBAR + 0x4780, 0x00000aaa); // OK
3899 write32(DEFAULT_MCHBAR + 0x4f88, 0x5f7003ff); // OK
3900 write32(DEFAULT_MCHBAR + 0x5064, 0x00073000 | ctrl->reg_5064b0); // OK
3901
3902 FOR_ALL_CHANNELS {
3903 switch (ctrl->rankmap[channel]) {
3904 /* Unpopulated channel. */
3905 case 0:
3906 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0);
3907 break;
3908 /* Only single-ranked dimms. */
3909 case 1:
3910 case 4:
3911 case 5:
3912 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x373131);
3913 break;
3914 /* Dual-ranked dimms present. */
3915 default:
3916 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x9b6ea1);
3917 break;
3918 }
3919 }
3920
3921 write32 (DEFAULT_MCHBAR + 0x5880, 0xca9171e5);
3922 write32 (DEFAULT_MCHBAR + 0x5888,
3923 (read32 (DEFAULT_MCHBAR + 0x5888) & ~0xffffff) | 0xe4d5d0);
3924 write32 (DEFAULT_MCHBAR + 0x58a8, read32 (DEFAULT_MCHBAR + 0x58a8) & ~0x1f);
3925 write32 (DEFAULT_MCHBAR + 0x4294,
3926 (read32 (DEFAULT_MCHBAR + 0x4294) & ~0x30000)
3927 | (1 << 16));
3928 write32 (DEFAULT_MCHBAR + 0x4694,
3929 (read32 (DEFAULT_MCHBAR + 0x4694) & ~0x30000)
3930 | (1 << 16));
3931
3932 MCHBAR32(0x5030) |= 1; // OK
3933 MCHBAR32(0x5030) |= 0x80; // OK
3934 MCHBAR32(0x5f18) = 0xfa; // OK
3935
3936 /* Find a populated channel. */
3937 FOR_ALL_POPULATED_CHANNELS
3938 break;
3939
3940 t1_cycles = ((read32(DEFAULT_MCHBAR + 0x4290 + channel * 0x400) >> 8) & 0xff);
3941 r32 = read32(DEFAULT_MCHBAR + 0x5064);
3942 if (r32 & 0x20000)
3943 t1_cycles += (r32 & 0xfff);
3944 t1_cycles += (read32(DEFAULT_MCHBAR + channel * 0x400 + 0x42a4) & 0xfff);
3945 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
3946 if (!(r32 & 0x20000))
3947 t1_ns += 500;
3948
3949 t2_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f10) >> 8) & 0xfff);
3950 if ( read32(DEFAULT_MCHBAR + 0x5f00) & 8 )
3951 {
3952 t3_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f20) >> 8) & 0xfff);
3953 t3_ns += 10 * (read32(DEFAULT_MCHBAR + 0x5f18) & 0xff);
3954 }
3955 else
3956 {
3957 t3_ns = 500;
3958 }
3959 printk(BIOS_DEBUG, "t123: %d, %d, %d\n",
3960 t1_ns, t2_ns, t3_ns);
3961 write32 (DEFAULT_MCHBAR + 0x5d10,
3962 ((encode_5d10(t1_ns) + encode_5d10(t2_ns)) << 16)
3963 | (encode_5d10(t1_ns) << 8)
3964 | ((encode_5d10(t3_ns) + encode_5d10(t2_ns) + encode_5d10(t1_ns)) << 24)
3965 | (read32(DEFAULT_MCHBAR + 0x5d10) & 0xC0C0C0C0)
3966 | 0xc);
3967}
3968
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +02003969static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003970{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003971 /* Save the MRC S3 restore data to cbmem */
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +02003972 store_current_mrc_cache(ctrl, sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003973}
3974
3975static void restore_timings(ramctr_timing * ctrl)
3976{
3977 int channel, slotrank, lane;
3978
3979 FOR_ALL_POPULATED_CHANNELS
3980 MCHBAR32(0x4004 + 0x400 * channel) =
3981 ctrl->tRRD
3982 | (ctrl->tRTP << 4)
3983 | (ctrl->tCKE << 8)
3984 | (ctrl->tWTR << 12)
3985 | (ctrl->tFAW << 16)
3986 | (ctrl->tWR << 24)
3987 | (ctrl->cmd_stretch[channel] << 30);
3988
3989 udelay(1);
3990
3991 FOR_ALL_POPULATED_CHANNELS {
3992 wait_428c(channel);
3993 }
3994
3995 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3996 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
3997 + 4 * lane, 0);
3998 }
3999
4000 FOR_ALL_POPULATED_CHANNELS
4001 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
4002 read32(DEFAULT_MCHBAR + 0x4008 +
4003 0x400 * channel) | 0x8000000);
4004
4005 FOR_ALL_POPULATED_CHANNELS {
4006 udelay (1);
4007 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
4008 read32(DEFAULT_MCHBAR + 0x4020 +
4009 0x400 * channel) | 0x200000);
4010 }
4011
4012 printram("CPE\n");
4013
4014 write32(DEFAULT_MCHBAR + 0x3400, 0);
4015 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
4016
4017 printram("CP5b\n");
4018
4019 FOR_ALL_POPULATED_CHANNELS {
4020 program_timings(ctrl, channel);
4021 }
4022
4023 u32 reg, addr;
4024
Elyes HAOUAS7db506c2016-10-02 11:56:39 +02004025 while (!(MCHBAR32(0x5084) & 0x10000));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004026 do {
4027 reg = MCHBAR32(0x428c);
4028 } while ((reg & 0x14) == 0);
4029
4030 // Set state of memory controller
4031 MCHBAR32(0x5030) = 0x116;
4032 MCHBAR32(0x4ea0) = 0;
4033
4034 // Wait 500us
4035 udelay(500);
4036
4037 FOR_ALL_CHANNELS {
4038 // Set valid rank CKE
4039 reg = 0;
4040 reg = (reg & ~0xf) | ctrl->rankmap[channel];
4041 addr = 0x400 * channel + 0x42a0;
4042 MCHBAR32(addr) = reg;
4043
4044 // Wait 10ns for ranks to settle
4045 //udelay(0.01);
4046
4047 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
4048 MCHBAR32(addr) = reg;
4049
4050 // Write reset using a NOP
4051 write_reset(ctrl);
4052 }
4053
4054 /* mrs commands. */
4055 dram_mrscommands(ctrl);
4056
4057 printram("CP5c\n");
4058
4059 write32(DEFAULT_MCHBAR + 0x3000, 0);
4060
4061 FOR_ALL_CHANNELS {
4062 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
4063 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
4064 ~0x3f000000));
4065 udelay(2);
4066 }
4067
4068 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
4069}
4070
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004071static int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot,
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004072 int s3_resume, int me_uma_size)
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004073{
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004074 int err;
4075
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004076 printk(BIOS_DEBUG, "Starting RAM training (%d).\n", fast_boot);
4077
4078 if (!fast_boot) {
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004079 /* Find fastest common supported parameters */
4080 dram_find_common_params(ctrl);
4081
4082 dram_dimm_mapping(ctrl);
4083 }
4084
4085 /* Set MCU frequency */
4086 dram_freq(ctrl);
4087
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004088 if (!fast_boot) {
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004089 /* Calculate timings */
4090 dram_timing(ctrl);
4091 }
4092
4093 /* Set version register */
4094 MCHBAR32(0x5034) = 0xC04EB002;
4095
4096 /* Enable crossover */
4097 dram_xover(ctrl);
4098
4099 /* Set timing and refresh registers */
4100 dram_timing_regs(ctrl);
4101
4102 /* Power mode preset */
4103 MCHBAR32(0x4e80) = 0x5500;
4104
4105 /* Set scheduler parameters */
4106 MCHBAR32(0x4c20) = 0x10100005;
4107
Elyes HAOUAS15279a92016-07-28 21:05:26 +02004108 /* Set CPU specific register */
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004109 set_4f8c();
4110
4111 /* Clear IO reset bit */
4112 MCHBAR32(0x5030) &= ~0x20;
4113
4114 /* Set MAD-DIMM registers */
4115 dram_dimm_set_mapping(ctrl);
4116 printk(BIOS_DEBUG, "Done dimm mapping\n");
4117
4118 /* Zone config */
4119 dram_zones(ctrl, 1);
4120
4121 /* Set memory map */
4122 dram_memorymap(ctrl, me_uma_size);
4123 printk(BIOS_DEBUG, "Done memory map\n");
4124
4125 /* Set IO registers */
4126 dram_ioregs(ctrl);
4127 printk(BIOS_DEBUG, "Done io registers\n");
4128
4129 udelay(1);
4130
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004131 if (fast_boot) {
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004132 restore_timings(ctrl);
4133 } else {
4134 /* Do jedec ddr3 reset sequence */
4135 dram_jedecreset(ctrl);
4136 printk(BIOS_DEBUG, "Done jedec reset\n");
4137
4138 /* MRS commands */
4139 dram_mrscommands(ctrl);
4140 printk(BIOS_DEBUG, "Done MRS commands\n");
4141
4142 /* Prepare for memory training */
4143 prepare_training(ctrl);
4144
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004145 err = read_training(ctrl);
4146 if (err)
4147 return err;
4148
4149 err = write_training(ctrl);
4150 if (err)
4151 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004152
4153 printram("CP5a\n");
4154
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004155 err = discover_edges(ctrl);
4156 if (err)
4157 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004158
4159 printram("CP5b\n");
4160
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004161 err = command_training(ctrl);
4162 if (err)
4163 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004164
4165 printram("CP5c\n");
4166
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004167 err = discover_edges_write(ctrl);
4168 if (err)
4169 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004170
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004171 err = discover_timC_write(ctrl);
4172 if (err)
4173 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004174
4175 normalize_training(ctrl);
4176 }
4177
4178 set_4008c(ctrl);
4179
4180 write_controller_mr(ctrl);
4181
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004182 if (!s3_resume) {
4183 err = channel_test(ctrl);
4184 if (err)
4185 return err;
4186 }
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004187
4188 return 0;
4189}
4190
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +02004191static void init_dram_ddr3(int mobile, int min_tck, int s3resume)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004192{
4193 int me_uma_size;
4194 int cbmem_was_inited;
Patrick Rudolph735ecce2016-03-26 10:42:27 +01004195 ramctr_timing ctrl;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004196 int fast_boot;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +02004197 spd_raw_data spds[4];
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004198 struct mrc_data_container *mrc_cache;
4199 ramctr_timing *ctrl_cached;
Patrick Rudolph31d19592016-03-26 12:22:34 +01004200 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004201
4202 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +02004203
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -07004204 report_platform_info();
4205
Stefan Reinauer00636b02012-04-04 00:08:51 +02004206 /* Wait for ME to be ready */
4207 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004208 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +02004209
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004210 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +02004211
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004212 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +02004213
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004214 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +02004215
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004216 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +02004217
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004218 reg_5d10 = read32(DEFAULT_MCHBAR + 0x5d10); // !!! = 0x00000000
4219 if ((pcie_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
4220 && reg_5d10 && !s3resume) {
4221 write32(DEFAULT_MCHBAR + 0x5d10, 0);
4222 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +02004223 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004224
Patrick Georgi546953c2014-11-29 10:38:17 +01004225 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02004226 }
Stefan Reinauer00636b02012-04-04 00:08:51 +02004227
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +02004228 memset(&ctrl, 0, sizeof(ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004229
4230 early_pch_init_native();
4231 early_thermal_init();
4232
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004233 /* try to find timings in MRC cache */
4234 mrc_cache = find_current_mrc_cache();
4235 if (!mrc_cache || (mrc_cache->mrc_data_size < sizeof(ctrl))) {
4236 if (s3resume) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004237 /* Failed S3 resume, reset to come up cleanly */
4238 outb(0x6, 0xcf9);
4239 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02004240 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004241 ctrl_cached = NULL;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004242 } else {
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004243 ctrl_cached = (ramctr_timing *)mrc_cache->mrc_data;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004244 }
4245
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004246 /* verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +02004247 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +02004248 /* Load SPD unique information data. */
4249 memset(spds, 0, sizeof(spds));
4250 mainboard_get_spd(spds, 1);
4251
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004252 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
4253 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
4254 if (!fast_boot)
4255 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +02004256 } else {
4257 fast_boot = s3resume;
4258 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004259
4260 if (fast_boot) {
4261 printk(BIOS_DEBUG, "Trying stored timings.\n");
4262 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
4263
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004264 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004265 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004266 if (s3resume) {
4267 /* Failed S3 resume, reset to come up cleanly */
4268 outb(0x6, 0xcf9);
4269 halt();
4270 }
4271 /* no need to erase bad mrc cache here, it gets overwritten on
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004272 * successful boot. */
4273 printk(BIOS_ERR, "Stored timings are invalid !\n");
4274 fast_boot = 0;
4275 }
4276 }
4277 if (!fast_boot) {
4278 ctrl.mobile = mobile;
4279 ctrl.tCK = min_tck;
4280
4281 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +02004282 memset(spds, 0, sizeof(spds));
4283 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004284 dram_find_spds_ddr3(spds, &ctrl);
4285
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004286 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004287 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +01004288
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +01004289 if (err) {
4290 /* fallback: disable failing channel */
4291 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
4292 printram("Disable failing channel.\n");
4293
4294 /* Reset DDR3 frequency */
4295 dram_find_spds_ddr3(spds, &ctrl);
4296
4297 /* disable failing channel */
4298 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
4299
4300 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
4301 }
4302
Patrick Rudolph31d19592016-03-26 12:22:34 +01004303 if (err)
4304 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004305
4306 /* FIXME: should be hardware revision-dependent. */
4307 write32(DEFAULT_MCHBAR + 0x5024, 0x00a030ce);
4308
4309 set_scrambling_seed(&ctrl);
4310
4311 set_42a0(&ctrl);
4312
4313 final_registers(&ctrl);
4314
4315 /* Zone config */
4316 dram_zones(&ctrl, 0);
4317
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004318 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004319 quick_ram_check();
4320
4321 intel_early_me_status();
4322 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
4323 intel_early_me_status();
4324
Stefan Reinauer00636b02012-04-04 00:08:51 +02004325 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004326
4327 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004328 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004329 save_timings(&ctrl);
4330 if (s3resume && !cbmem_was_inited) {
4331 /* Failed S3 resume, reset to come up cleanly */
4332 outb(0x6, 0xcf9);
4333 halt();
4334 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +01004335
Patrick Rudolph735ecce2016-03-26 10:42:27 +01004336 fill_smbios17(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +02004337}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004338
4339#define HOST_BRIDGE PCI_DEVFN(0, 0)
4340#define DEFAULT_TCK TCK_800MHZ
4341
4342static unsigned int get_mem_min_tck(void)
4343{
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004344 u32 reg32;
4345 u8 rev;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004346 const struct device *dev;
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004347 const struct northbridge_intel_sandybridge_config *cfg = NULL;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004348
4349 dev = dev_find_slot(0, HOST_BRIDGE);
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004350 if (dev)
4351 cfg = dev->chip_info;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004352
4353 /* If this is zero, it just means devicetree.cb didn't set it */
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004354 if (!cfg || cfg->max_mem_clock_mhz == 0) {
4355 rev = pci_read_config8(PCI_DEV(0, 0, 0), PCI_DEVICE_ID);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004356
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004357 if ((rev & BASE_REV_MASK) == BASE_REV_SNB) {
4358 /* read Capabilities A Register DMFC bits */
4359 reg32 = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_A);
4360 reg32 &= 0x7;
4361
4362 switch (reg32) {
4363 case 7: return TCK_533MHZ;
4364 case 6: return TCK_666MHZ;
4365 case 5: return TCK_800MHZ;
4366 /* reserved: */
4367 default:
4368 break;
4369 }
4370 } else {
4371 /* read Capabilities B Register DMFC bits */
4372 reg32 = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_B);
4373 reg32 = (reg32 >> 4) & 0x7;
4374
4375 switch (reg32) {
4376 case 7: return TCK_533MHZ;
4377 case 6: return TCK_666MHZ;
4378 case 5: return TCK_800MHZ;
4379 case 4: return TCK_933MHZ;
4380 case 3: return TCK_1066MHZ;
4381 case 2: return TCK_1200MHZ;
4382 case 1: return TCK_1333MHZ;
4383 /* reserved: */
4384 default:
4385 break;
4386 }
4387 }
4388 return DEFAULT_TCK;
4389 } else {
Patrick Rudolph7bddd302016-06-11 18:39:35 +02004390 if (cfg->max_mem_clock_mhz >= 1066)
4391 return TCK_1066MHZ;
4392 else if (cfg->max_mem_clock_mhz >= 933)
4393 return TCK_933MHZ;
4394 else if (cfg->max_mem_clock_mhz >= 800)
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004395 return TCK_800MHZ;
4396 else if (cfg->max_mem_clock_mhz >= 666)
4397 return TCK_666MHZ;
4398 else if (cfg->max_mem_clock_mhz >= 533)
4399 return TCK_533MHZ;
4400 else
4401 return TCK_400MHZ;
4402 }
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004403}
4404
Patrick Rudolph266a1f72016-06-09 18:13:34 +02004405#define DEFAULT_PCI_MMIO_SIZE 2048
4406
4407static unsigned int get_mmio_size(void)
4408{
4409 const struct device *dev;
4410 const struct northbridge_intel_sandybridge_config *cfg = NULL;
4411
4412 dev = dev_find_slot(0, HOST_BRIDGE);
4413 if (dev)
4414 cfg = dev->chip_info;
4415
4416 /* If this is zero, it just means devicetree.cb didn't set it */
4417 if (!cfg || cfg->pci_mmio_size == 0)
4418 return DEFAULT_PCI_MMIO_SIZE;
4419 else
4420 return cfg->pci_mmio_size;
4421}
4422
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004423void perform_raminit(int s3resume)
4424{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004425 post_code(0x3a);
4426
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004427 timestamp_add_now(TS_BEFORE_INITRAM);
4428
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +02004429 init_dram_ddr3(1, get_mem_min_tck(), s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004430}