blob: d06e929384d7e518ed31b8d651cacd7563d5853f [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;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700853 while (reg1 & 0x80000000) {
854 printk(BIOS_DEBUG, " PLL busy...");
Patrick Rudolph069018d2016-11-12 11:43:59 +0100855 reg1 = MCHBAR32(MC_BIOS_REQ);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700856 }
857 printk(BIOS_DEBUG, "done\n");
858
859 /* Step 3 - Verify lock frequency */
Patrick Rudolph069018d2016-11-12 11:43:59 +0100860 reg1 = MCHBAR32(MC_BIOS_DATA);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700861 val2 = (u8) reg1;
862 if (val2 >= FRQ) {
863 printk(BIOS_DEBUG, "MCU frequency is set at : %d MHz\n",
864 (1000 << 8) / ctrl->tCK);
865 return;
866 }
867 printk(BIOS_DEBUG, "PLL didn't lock. Retrying at lower frequency\n");
868 ctrl->tCK++;
869 }
870}
871
872static void dram_xover(ramctr_timing * ctrl)
873{
874 u32 reg;
875 int channel;
876
877 FOR_ALL_CHANNELS {
878 // enable xover clk
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +0100879 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100880 printram("XOVER CLK [%x] = %x\n", channel * 0x100 + 0xc14,
881 reg);
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +0100882 MCHBAR32(channel * 0x100 + 0xc14) = reg;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700883
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +0100884 // enable xover ctl & xover cmd
885 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100886 printram("XOVER CMD [%x] = %x\n", 0x100 * channel + 0x320c,
887 reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700888 MCHBAR32(0x100 * channel + 0x320c) = reg;
889 }
890}
891
892static void dram_timing_regs(ramctr_timing * ctrl)
893{
894 u32 reg, addr, val32, cpu, stretch;
895 struct cpuid_result cpures;
896 int channel;
897
898 FOR_ALL_CHANNELS {
899 // DBP
900 reg = 0;
901 reg |= ctrl->tRCD;
902 reg |= (ctrl->tRP << 4);
903 reg |= (ctrl->CAS << 8);
904 reg |= (ctrl->CWL << 12);
905 reg |= (ctrl->tRAS << 16);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100906 printram("DBP [%x] = %x\n", 0x400 * channel + 0x4000, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700907 MCHBAR32(0x400 * channel + 0x4000) = reg;
908
909 // RAP
910 reg = 0;
911 reg |= ctrl->tRRD;
912 reg |= (ctrl->tRTP << 4);
913 reg |= (ctrl->tCKE << 8);
914 reg |= (ctrl->tWTR << 12);
915 reg |= (ctrl->tFAW << 16);
916 reg |= (ctrl->tWR << 24);
917 reg |= (3 << 30);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100918 printram("RAP [%x] = %x\n", 0x400 * channel + 0x4004, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700919 MCHBAR32(0x400 * channel + 0x4004) = reg;
920
921 // OTHP
922 addr = 0x400 * channel + 0x400c;
923 reg = 0;
924 reg |= ctrl->tXPDLL;
925 reg |= (ctrl->tXP << 5);
926 reg |= (ctrl->tAONPD << 8);
927 reg |= 0xa0000;
Patrick Rudolpha649a542016-01-17 18:32:06 +0100928 printram("OTHP [%x] = %x\n", addr, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700929 MCHBAR32(addr) = reg;
930
931 MCHBAR32(0x400 * channel + 0x4014) = 0;
932
933 MCHBAR32(addr) |= 0x00020000;
934
935 // ODT stretch
936 reg = 0;
937
Ryan Salsamendie4da9aa2016-06-24 12:01:11 -0700938 cpures = cpuid(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700939 cpu = cpures.eax;
940 if (IS_IVY_CPU(cpu)
941 || (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_D2(cpu))) {
942 stretch = 2;
943 addr = 0x400 * channel + 0x400c;
Patrick Rudolpha649a542016-01-17 18:32:06 +0100944 printram("ODT stretch [%x] = %x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700945 0x400 * channel + 0x400c, reg);
946 reg = MCHBAR32(addr);
947
948 if (((ctrl->rankmap[channel] & 3) == 0)
949 || (ctrl->rankmap[channel] & 0xc) == 0) {
950
951 // Rank 0 - operate on rank 2
952 reg = (reg & ~0xc0000) | (stretch << 18);
953
954 // Rank 2 - operate on rank 0
955 reg = (reg & ~0x30000) | (stretch << 16);
956
Patrick Rudolpha649a542016-01-17 18:32:06 +0100957 printram("ODT stretch [%x] = %x\n", addr, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700958 MCHBAR32(addr) = reg;
959 }
960
961 } else if (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_C(cpu)) {
962 stretch = 3;
963 addr = 0x400 * channel + 0x401c;
964 reg = MCHBAR32(addr);
965
966 if (((ctrl->rankmap[channel] & 3) == 0)
967 || (ctrl->rankmap[channel] & 0xc) == 0) {
968
969 // Rank 0 - operate on rank 2
970 reg = (reg & ~0x3000) | (stretch << 12);
971
972 // Rank 2 - operate on rank 0
973 reg = (reg & ~0xc00) | (stretch << 10);
974
Patrick Rudolpha649a542016-01-17 18:32:06 +0100975 printram("ODT stretch [%x] = %x\n", addr, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700976 MCHBAR32(addr) = reg;
977 }
978 } else {
979 stretch = 0;
980 }
981
982 // REFI
983 reg = 0;
984 val32 = ctrl->tREFI;
985 reg = (reg & ~0xffff) | val32;
986 val32 = ctrl->tRFC;
987 reg = (reg & ~0x1ff0000) | (val32 << 16);
988 val32 = (u32) (ctrl->tREFI * 9) / 1024;
989 reg = (reg & ~0xfe000000) | (val32 << 25);
Patrick Rudolpha649a542016-01-17 18:32:06 +0100990 printram("REFI [%x] = %x\n", 0x400 * channel + 0x4298,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -0700991 reg);
992 MCHBAR32(0x400 * channel + 0x4298) = reg;
993
994 MCHBAR32(0x400 * channel + 0x4294) |= 0xff;
995
996 // SRFTP
997 reg = 0;
998 val32 = tDLLK;
999 reg = (reg & ~0xfff) | val32;
1000 val32 = ctrl->tXSOffset;
1001 reg = (reg & ~0xf000) | (val32 << 12);
1002 val32 = tDLLK - ctrl->tXSOffset;
1003 reg = (reg & ~0x3ff0000) | (val32 << 16);
1004 val32 = ctrl->tMOD - 8;
1005 reg = (reg & ~0xf0000000) | (val32 << 28);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001006 printram("SRFTP [%x] = %x\n", 0x400 * channel + 0x42a4,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001007 reg);
1008 MCHBAR32(0x400 * channel + 0x42a4) = reg;
1009 }
1010}
1011
Patrick Rudolph735ecce2016-03-26 10:42:27 +01001012static void dram_dimm_mapping(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001013{
1014 u32 reg, val32;
1015 int channel;
Patrick Rudolph735ecce2016-03-26 10:42:27 +01001016 dimm_info *info = &ctrl->info;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001017
1018 FOR_ALL_CHANNELS {
1019 dimm_attr *dimmA = 0;
1020 dimm_attr *dimmB = 0;
1021 reg = 0;
1022 val32 = 0;
1023 if (info->dimm[channel][0].size_mb >=
1024 info->dimm[channel][1].size_mb) {
1025 // dimm 0 is bigger, set it to dimmA
1026 dimmA = &info->dimm[channel][0];
1027 dimmB = &info->dimm[channel][1];
1028 reg |= (0 << 16);
1029 } else {
1030 // dimm 1 is bigger, set it to dimmA
1031 dimmA = &info->dimm[channel][1];
1032 dimmB = &info->dimm[channel][0];
1033 reg |= (1 << 16);
1034 }
1035 // dimmA
1036 if (dimmA && (dimmA->ranks > 0)) {
1037 val32 = dimmA->size_mb / 256;
1038 reg = (reg & ~0xff) | val32;
1039 val32 = dimmA->ranks - 1;
1040 reg = (reg & ~0x20000) | (val32 << 17);
1041 val32 = (dimmA->width / 8) - 1;
1042 reg = (reg & ~0x80000) | (val32 << 19);
1043 }
1044 // dimmB
1045 if (dimmB && (dimmB->ranks > 0)) {
1046 val32 = dimmB->size_mb / 256;
1047 reg = (reg & ~0xff00) | (val32 << 8);
1048 val32 = dimmB->ranks - 1;
1049 reg = (reg & ~0x40000) | (val32 << 18);
1050 val32 = (dimmB->width / 8) - 1;
1051 reg = (reg & ~0x100000) | (val32 << 20);
1052 }
1053 reg = (reg & ~0x200000) | (1 << 21); // rank interleave
1054 reg = (reg & ~0x400000) | (1 << 22); // enhanced interleave
1055
1056 // Save MAD-DIMM register
1057 if ((dimmA && (dimmA->ranks > 0))
1058 || (dimmB && (dimmB->ranks > 0))) {
1059 ctrl->mad_dimm[channel] = reg;
1060 } else {
1061 ctrl->mad_dimm[channel] = 0;
1062 }
1063 }
1064}
1065
1066static void dram_dimm_set_mapping(ramctr_timing * ctrl)
1067{
1068 int channel;
1069 FOR_ALL_CHANNELS {
1070 MCHBAR32(0x5004 + channel * 4) = ctrl->mad_dimm[channel];
1071 }
1072}
1073
1074static void dram_zones(ramctr_timing * ctrl, int training)
1075{
1076 u32 reg, ch0size, ch1size;
1077 u8 val;
1078 reg = 0;
1079 val = 0;
1080 if (training) {
1081 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
1082 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
1083 } else {
1084 ch0size = ctrl->channel_size_mb[0];
1085 ch1size = ctrl->channel_size_mb[1];
1086 }
1087
1088 if (ch0size >= ch1size) {
1089 reg = MCHBAR32(0x5014);
1090 val = ch1size / 256;
1091 reg = (reg & ~0xff000000) | val << 24;
1092 reg = (reg & ~0xff0000) | (2 * val) << 16;
1093 MCHBAR32(0x5014) = reg;
1094 MCHBAR32(0x5000) = 0x24;
1095 } else {
1096 reg = MCHBAR32(0x5014);
1097 val = ch0size / 256;
1098 reg = (reg & ~0xff000000) | val << 24;
1099 reg = (reg & ~0xff0000) | (2 * val) << 16;
1100 MCHBAR32(0x5014) = reg;
1101 MCHBAR32(0x5000) = 0x21;
1102 }
1103}
1104
1105static void dram_memorymap(ramctr_timing * ctrl, int me_uma_size)
1106{
1107 u32 reg, val, reclaim;
1108 u32 tom, gfxstolen, gttsize;
1109 size_t tsegsize, mmiosize, toludbase, touudbase, gfxstolenbase, gttbase,
1110 tsegbase, mestolenbase;
1111 size_t tsegbasedelta, remapbase, remaplimit;
1112 uint16_t ggc;
1113
Patrick Rudolph266a1f72016-06-09 18:13:34 +02001114 mmiosize = get_mmio_size();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001115
1116 ggc = pci_read_config16(NORTHBRIDGE, GGC);
1117 if (!(ggc & 2)) {
1118 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
1119 gttsize = ((ggc >> 8) & 0x3);
1120 } else {
1121 gfxstolen = 0;
1122 gttsize = 0;
1123 }
1124
1125 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
1126
1127 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
1128
1129 mestolenbase = tom - me_uma_size;
1130
1131 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize,
1132 tom - me_uma_size);
1133 gfxstolenbase = toludbase - gfxstolen;
1134 gttbase = gfxstolenbase - gttsize;
1135
1136 tsegbase = gttbase - tsegsize;
1137
1138 // Round tsegbase down to nearest address aligned to tsegsize
1139 tsegbasedelta = tsegbase & (tsegsize - 1);
1140 tsegbase &= ~(tsegsize - 1);
1141
1142 gttbase -= tsegbasedelta;
1143 gfxstolenbase -= tsegbasedelta;
1144 toludbase -= tsegbasedelta;
1145
Elyes HAOUAS15279a92016-07-28 21:05:26 +02001146 // Test if it is possible to reclaim a hole in the RAM addressing
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001147 if (tom - me_uma_size > toludbase) {
1148 // Reclaim is possible
1149 reclaim = 1;
1150 remapbase = MAX(4096, tom - me_uma_size);
1151 remaplimit =
1152 remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
1153 touudbase = remaplimit + 1;
1154 } else {
1155 // Reclaim not possible
1156 reclaim = 0;
1157 touudbase = tom - me_uma_size;
1158 }
1159
1160 // Update memory map in pci-e configuration space
Patrick Rudolpha649a542016-01-17 18:32:06 +01001161 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001162
1163 // TOM (top of memory)
1164 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa0);
1165 val = tom & 0xfff;
1166 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001167 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa0, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001168 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa0, reg);
1169
1170 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa4);
1171 val = tom & 0xfffff000;
1172 reg = (reg & ~0x000fffff) | (val >> 12);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001173 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa4, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001174 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa4, reg);
1175
1176 // TOLUD (top of low used dram)
1177 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xbc);
1178 val = toludbase & 0xfff;
1179 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001180 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xbc, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001181 pcie_write_config32(PCI_DEV(0, 0, 0), 0xbc, reg);
1182
1183 // TOUUD LSB (top of upper usable dram)
1184 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xa8);
1185 val = touudbase & 0xfff;
1186 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001187 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa8, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001188 pcie_write_config32(PCI_DEV(0, 0, 0), 0xa8, reg);
1189
1190 // TOUUD MSB
1191 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xac);
1192 val = touudbase & 0xfffff000;
1193 reg = (reg & ~0x000fffff) | (val >> 12);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001194 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xac, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001195 pcie_write_config32(PCI_DEV(0, 0, 0), 0xac, reg);
1196
1197 if (reclaim) {
1198 // REMAP BASE
1199 pcie_write_config32(PCI_DEV(0, 0, 0), 0x90, remapbase << 20);
1200 pcie_write_config32(PCI_DEV(0, 0, 0), 0x94, remapbase >> 12);
1201
1202 // REMAP LIMIT
1203 pcie_write_config32(PCI_DEV(0, 0, 0), 0x98, remaplimit << 20);
1204 pcie_write_config32(PCI_DEV(0, 0, 0), 0x9c, remaplimit >> 12);
1205 }
1206 // TSEG
1207 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb8);
1208 val = tsegbase & 0xfff;
1209 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001210 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb8, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001211 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb8, reg);
1212
1213 // GFX stolen memory
1214 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb0);
1215 val = gfxstolenbase & 0xfff;
1216 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001217 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb0, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001218 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb0, reg);
1219
1220 // GTT stolen memory
1221 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0xb4);
1222 val = gttbase & 0xfff;
1223 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001224 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb4, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001225 pcie_write_config32(PCI_DEV(0, 0, 0), 0xb4, reg);
1226
1227 if (me_uma_size) {
1228 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x7c);
1229 val = (0x80000 - me_uma_size) & 0xfffff000;
1230 reg = (reg & ~0x000fffff) | (val >> 12);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001231 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x7c, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001232 pcie_write_config32(PCI_DEV(0, 0, 0), 0x7c, reg);
1233
1234 // ME base
1235 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x70);
1236 val = mestolenbase & 0xfff;
1237 reg = (reg & ~0xfff00000) | (val << 20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001238 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x70, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001239 pcie_write_config32(PCI_DEV(0, 0, 0), 0x70, reg);
1240
1241 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x74);
1242 val = mestolenbase & 0xfffff000;
1243 reg = (reg & ~0x000fffff) | (val >> 12);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001244 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x74, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001245 pcie_write_config32(PCI_DEV(0, 0, 0), 0x74, reg);
1246
1247 // ME mask
1248 reg = pcie_read_config32(PCI_DEV(0, 0, 0), 0x78);
1249 val = (0x80000 - me_uma_size) & 0xfff;
1250 reg = (reg & ~0xfff00000) | (val << 20);
1251 reg = (reg & ~0x400) | (1 << 10); // set lockbit on ME mem
1252
1253 reg = (reg & ~0x800) | (1 << 11); // set ME memory enable
Patrick Rudolpha649a542016-01-17 18:32:06 +01001254 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x78, reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001255 pcie_write_config32(PCI_DEV(0, 0, 0), 0x78, reg);
1256 }
1257}
1258
1259static void dram_ioregs(ramctr_timing * ctrl)
1260{
1261 u32 reg, comp2;
1262
1263 int channel;
1264
1265 // IO clock
1266 FOR_ALL_CHANNELS {
1267 MCHBAR32(0xc00 + 0x100 * channel) = ctrl->rankmap[channel];
1268 }
1269
1270 // IO command
1271 FOR_ALL_CHANNELS {
1272 MCHBAR32(0x3200 + 0x100 * channel) = ctrl->rankmap[channel];
1273 }
1274
1275 // IO control
1276 FOR_ALL_POPULATED_CHANNELS {
1277 program_timings(ctrl, channel);
1278 }
1279
1280 // Rcomp
Patrick Rudolpha649a542016-01-17 18:32:06 +01001281 printram("RCOMP...");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001282 reg = 0;
1283 while (reg == 0) {
1284 reg = MCHBAR32(0x5084) & 0x10000;
1285 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01001286 printram("done\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001287
1288 // Set comp2
1289 comp2 = get_COMP2(ctrl->tCK);
1290 MCHBAR32(0x3714) = comp2;
Patrick Rudolpha649a542016-01-17 18:32:06 +01001291 printram("COMP2 done\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001292
1293 // Set comp1
1294 FOR_ALL_POPULATED_CHANNELS {
1295 reg = MCHBAR32(0x1810 + channel * 0x100); //ch0
1296 reg = (reg & ~0xe00) | (1 << 9); //odt
1297 reg = (reg & ~0xe00000) | (1 << 21); //clk drive up
1298 reg = (reg & ~0x38000000) | (1 << 27); //ctl drive up
1299 MCHBAR32(0x1810 + channel * 0x100) = reg;
1300 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01001301 printram("COMP1 done\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001302
Patrick Rudolpha649a542016-01-17 18:32:06 +01001303 printram("FORCE RCOMP and wait 20us...");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001304 MCHBAR32(0x5f08) |= 0x100;
1305 udelay(20);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001306 printram("done\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001307}
1308
1309static void wait_428c(int channel)
1310{
1311 while (1) {
1312 if (read32(DEFAULT_MCHBAR + 0x428c + (channel << 10)) & 0x50)
1313 return;
1314 }
1315}
1316
1317static void write_reset(ramctr_timing * ctrl)
1318{
1319 int channel, slotrank;
1320
1321 /* choose a populated channel. */
1322 channel = (ctrl->rankmap[0]) ? 0 : 1;
1323
1324 wait_428c(channel);
1325
1326 /* choose a populated rank. */
1327 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
1328
Patrick Rudolph371d2912015-10-09 13:33:25 +02001329 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001330 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1331 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x80c01);
1332
1333 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1334 (slotrank << 24) | 0x60000);
1335
1336 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1337
1338 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x400001);
1339 wait_428c(channel);
1340}
1341
1342static void dram_jedecreset(ramctr_timing * ctrl)
1343{
1344 u32 reg, addr;
1345 int channel;
1346
Elyes HAOUAS7db506c2016-10-02 11:56:39 +02001347 while (!(MCHBAR32(0x5084) & 0x10000));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001348 do {
1349 reg = MCHBAR32(0x428c);
1350 } while ((reg & 0x14) == 0);
1351
1352 // Set state of memory controller
1353 reg = 0x112;
1354 MCHBAR32(0x5030) = reg;
1355 MCHBAR32(0x4ea0) = 0;
1356 reg |= 2; //ddr reset
1357 MCHBAR32(0x5030) = reg;
1358
1359 // Assert dimm reset signal
1360 reg = MCHBAR32(0x5030);
1361 reg &= ~0x2;
1362 MCHBAR32(0x5030) = reg;
1363
1364 // Wait 200us
1365 udelay(200);
1366
1367 // Deassert dimm reset signal
1368 MCHBAR32(0x5030) |= 2;
1369
1370 // Wait 500us
1371 udelay(500);
1372
1373 // Enable DCLK
1374 MCHBAR32(0x5030) |= 4;
1375
1376 // XXX Wait 20ns
1377 udelay(1);
1378
1379 FOR_ALL_CHANNELS {
1380 // Set valid rank CKE
1381 reg = 0;
1382 reg = (reg & ~0xf) | ctrl->rankmap[channel];
1383 addr = 0x400 * channel + 0x42a0;
1384 MCHBAR32(addr) = reg;
1385
1386 // Wait 10ns for ranks to settle
1387 //udelay(0.01);
1388
1389 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
1390 MCHBAR32(addr) = reg;
1391
1392 // Write reset using a NOP
1393 write_reset(ctrl);
1394 }
1395}
1396
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001397static odtmap get_ODT(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001398{
1399 /* Get ODT based on rankmap: */
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001400 int dimms_per_ch = (ctrl->rankmap[channel] & 1)
1401 + ((ctrl->rankmap[channel] >> 2) & 1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001402
1403 if (dimms_per_ch == 1) {
1404 return (const odtmap){60, 60};
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001405 } else {
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001406 return (const odtmap){120, 30};
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001407 }
1408}
1409
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001410static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001411 int reg, u32 val)
1412{
1413 wait_428c(channel);
1414
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001415 if (ctrl->rank_mirror[channel][slotrank]) {
1416 /* DDR3 Rank1 Address mirror
1417 * swap the following pins:
1418 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
1419 reg = ((reg >> 1) & 1) | ((reg << 1) & 2);
1420 val = (val & ~0x1f8) | ((val >> 1) & 0xa8)
1421 | ((val & 0xa8) << 1);
1422 }
1423
Patrick Rudolph371d2912015-10-09 13:33:25 +02001424 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001425 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f000);
1426 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
1427 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1428 (slotrank << 24) | (reg << 20) | val | 0x60000);
1429 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1430
Patrick Rudolph371d2912015-10-09 13:33:25 +02001431 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001432 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f000);
1433 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x41001);
1434 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1435 (slotrank << 24) | (reg << 20) | val | 0x60000);
1436 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1437
Patrick Rudolph371d2912015-10-09 13:33:25 +02001438 /* DRAM command MRS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001439 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x0f000);
1440 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1441 0x1001 | (ctrl->tMOD << 16));
1442 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1443 (slotrank << 24) | (reg << 20) | val | 0x60000);
1444 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1445 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
1446}
1447
1448static u32 make_mr0(ramctr_timing * ctrl, u8 rank)
1449{
1450 u16 mr0reg, mch_cas, mch_wr;
1451 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 +02001452
1453 /* DLL Reset - self clearing - set after CLK frequency has been changed */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001454 mr0reg = 0x100;
1455
1456 // Convert CAS to MCH register friendly
1457 if (ctrl->CAS < 12) {
1458 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
1459 } else {
1460 mch_cas = (u16) (ctrl->CAS - 12);
1461 mch_cas = ((mch_cas << 1) | 0x1);
1462 }
1463
1464 // Convert tWR to MCH register friendly
1465 mch_wr = mch_wr_t[ctrl->tWR - 5];
1466
1467 mr0reg = (mr0reg & ~0x4) | (mch_cas & 0x1);
1468 mr0reg = (mr0reg & ~0x70) | ((mch_cas & 0xe) << 3);
1469 mr0reg = (mr0reg & ~0xe00) | (mch_wr << 9);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001470
1471 // Precharge PD - Fast (desktop) 0x1 or slow (mobile) 0x0 - mostly power-saving feature
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001472 mr0reg = (mr0reg & ~0x1000) | (!ctrl->mobile << 12);
1473 return mr0reg;
1474}
1475
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001476static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001477{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001478 write_mrreg(ctrl, channel, rank, 0,
1479 make_mr0(ctrl, rank));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001480}
1481
1482static u32 encode_odt(u32 odt)
1483{
1484 switch (odt) {
1485 case 30:
1486 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
1487 case 60:
1488 return (1 << 2); // RZQ/4
1489 case 120:
1490 return (1 << 6); // RZQ/2
1491 default:
1492 case 0:
1493 return 0;
1494 }
1495}
1496
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001497static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001498{
1499 odtmap odt;
1500 u32 mr1reg;
1501
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001502 odt = get_ODT(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001503 mr1reg = 0x2;
1504
1505 mr1reg |= encode_odt(odt.rttnom);
1506
1507 return mr1reg;
1508}
1509
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001510static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001511{
1512 u16 mr1reg;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001513
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001514 mr1reg = make_mr1(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001515
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001516 write_mrreg(ctrl, channel, rank, 1, mr1reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001517}
1518
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001519static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001520{
1521 u16 pasr, cwl, mr2reg;
1522 odtmap odt;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001523 int srt;
1524
1525 pasr = 0;
1526 cwl = ctrl->CWL - 5;
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001527 odt = get_ODT(ctrl, rank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001528
1529 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
1530
1531 mr2reg = 0;
1532 mr2reg = (mr2reg & ~0x7) | pasr;
1533 mr2reg = (mr2reg & ~0x38) | (cwl << 3);
1534 mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6);
1535 mr2reg = (mr2reg & ~0x80) | (srt << 7);
1536 mr2reg |= (odt.rttwr / 60) << 9;
1537
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001538 write_mrreg(ctrl, channel, rank, 2, mr2reg);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001539}
1540
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001541static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001542{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001543 write_mrreg(ctrl, channel, rank, 3, 0);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001544}
1545
1546static void dram_mrscommands(ramctr_timing * ctrl)
1547{
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001548 u8 slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001549 u32 reg, addr;
1550 int channel;
1551
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001552 FOR_ALL_POPULATED_CHANNELS {
1553 FOR_ALL_POPULATED_RANKS {
1554 // MR2
1555 dram_mr2(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001556
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001557 // MR3
1558 dram_mr3(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001559
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001560 // MR1
1561 dram_mr1(ctrl, slotrank, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001562
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001563 // MR0
1564 dram_mr0(ctrl, slotrank, channel);
1565 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001566 }
1567
Patrick Rudolph371d2912015-10-09 13:33:25 +02001568 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001569 write32(DEFAULT_MCHBAR + 0x4e20, 0x7);
1570 write32(DEFAULT_MCHBAR + 0x4e30, 0xf1001);
1571 write32(DEFAULT_MCHBAR + 0x4e00, 0x60002);
1572 write32(DEFAULT_MCHBAR + 0x4e10, 0);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001573
1574 /* DRAM command ZQCL */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001575 write32(DEFAULT_MCHBAR + 0x4e24, 0x1f003);
1576 write32(DEFAULT_MCHBAR + 0x4e34, 0x1901001);
1577 write32(DEFAULT_MCHBAR + 0x4e04, 0x60400);
1578 write32(DEFAULT_MCHBAR + 0x4e14, 0x288);
Patrick Rudolph371d2912015-10-09 13:33:25 +02001579
1580 /* execute command queue on all channels ? */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001581 write32(DEFAULT_MCHBAR + 0x4e84, 0x40004);
1582
1583 // Drain
1584 FOR_ALL_CHANNELS {
1585 // Wait for ref drained
1586 wait_428c(channel);
1587 }
1588
1589 // Refresh enable
1590 MCHBAR32(0x5030) |= 8;
1591
1592 FOR_ALL_POPULATED_CHANNELS {
1593 addr = 0x400 * channel + 0x4020;
1594 reg = MCHBAR32(addr);
1595 reg &= ~0x200000;
1596 MCHBAR32(addr) = reg;
1597
1598 wait_428c(channel);
1599
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001600 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001601
1602 // Drain
1603 wait_428c(channel);
1604
Patrick Rudolph371d2912015-10-09 13:33:25 +02001605 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001606 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1607 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
1608 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01001609 (slotrank << 24) | 0x60000);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001610 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
1611 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x1);
1612
1613 // Drain
1614 wait_428c(channel);
1615 }
1616}
1617
1618const u32 lane_registers[] = {
1619 0x0000, 0x0200, 0x0400, 0x0600,
1620 0x1000, 0x1200, 0x1400, 0x1600,
1621 0x0800
1622};
1623
1624static void program_timings(ramctr_timing * ctrl, int channel)
1625{
1626 u32 reg32, reg_4024, reg_c14, reg_c18, reg_4028;
1627 int lane;
1628 int slotrank, slot;
1629 int full_shift = 0;
1630 u16 slot320c[NUM_SLOTS];
1631
1632 FOR_ALL_POPULATED_RANKS {
1633 if (full_shift < -ctrl->timings[channel][slotrank].val_320c)
1634 full_shift = -ctrl->timings[channel][slotrank].val_320c;
1635 }
1636
1637 for (slot = 0; slot < NUM_SLOTS; slot++)
1638 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
1639 case 0:
1640 default:
1641 slot320c[slot] = 0x7f;
1642 break;
1643 case 1:
1644 slot320c[slot] =
1645 ctrl->timings[channel][2 * slot + 0].val_320c +
1646 full_shift;
1647 break;
1648 case 2:
1649 slot320c[slot] =
1650 ctrl->timings[channel][2 * slot + 1].val_320c +
1651 full_shift;
1652 break;
1653 case 3:
1654 slot320c[slot] =
1655 (ctrl->timings[channel][2 * slot].val_320c +
1656 ctrl->timings[channel][2 * slot +
1657 1].val_320c) / 2 +
1658 full_shift;
1659 break;
1660 }
1661
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +01001662 /* enable CMD XOVER */
1663 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001664 reg32 |= ((slot320c[0] & 0x3f) << 6) | ((slot320c[0] & 0x40) << 9);
1665 reg32 |= (slot320c[1] & 0x7f) << 18;
1666 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
1667
1668 MCHBAR32(0x320c + 0x100 * channel) = reg32;
1669
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +01001670 /* enable CLK XOVER */
1671 reg_c14 = get_XOVER_CLK(ctrl->rankmap[channel]);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001672 reg_c18 = 0;
1673
1674 FOR_ALL_POPULATED_RANKS {
1675 int shift =
1676 ctrl->timings[channel][slotrank].val_320c + full_shift;
1677 int offset_val_c14;
1678 if (shift < 0)
1679 shift = 0;
1680 offset_val_c14 = ctrl->reg_c14_offset + shift;
Patrick Rudolpha1c3bed2016-01-24 14:07:15 +01001681 /* set CLK phase shift */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001682 reg_c14 |= (offset_val_c14 & 0x3f) << (6 * slotrank);
1683 reg_c18 |= ((offset_val_c14 >> 6) & 1) << slotrank;
1684 }
1685
1686 MCHBAR32(0xc14 + channel * 0x100) = reg_c14;
1687 MCHBAR32(0xc18 + channel * 0x100) = reg_c18;
1688
1689 reg_4028 = MCHBAR32(0x4028 + 0x400 * channel);
1690 reg_4028 &= 0xffff0000;
1691
1692 reg_4024 = 0;
1693
1694 FOR_ALL_POPULATED_RANKS {
1695 int post_timA_min_high = 7, post_timA_max_high = 0;
1696 int pre_timA_min_high = 7, pre_timA_max_high = 0;
1697 int shift_402x = 0;
1698 int shift =
1699 ctrl->timings[channel][slotrank].val_320c + full_shift;
1700
1701 if (shift < 0)
1702 shift = 0;
1703
1704 FOR_ALL_LANES {
1705 if (post_timA_min_high >
1706 ((ctrl->timings[channel][slotrank].lanes[lane].
1707 timA + shift) >> 6))
1708 post_timA_min_high =
1709 ((ctrl->timings[channel][slotrank].
1710 lanes[lane].timA + shift) >> 6);
1711 if (pre_timA_min_high >
1712 (ctrl->timings[channel][slotrank].lanes[lane].
1713 timA >> 6))
1714 pre_timA_min_high =
1715 (ctrl->timings[channel][slotrank].
1716 lanes[lane].timA >> 6);
1717 if (post_timA_max_high <
1718 ((ctrl->timings[channel][slotrank].lanes[lane].
1719 timA + shift) >> 6))
1720 post_timA_max_high =
1721 ((ctrl->timings[channel][slotrank].
1722 lanes[lane].timA + shift) >> 6);
1723 if (pre_timA_max_high <
1724 (ctrl->timings[channel][slotrank].lanes[lane].
1725 timA >> 6))
1726 pre_timA_max_high =
1727 (ctrl->timings[channel][slotrank].
1728 lanes[lane].timA >> 6);
1729 }
1730
1731 if (pre_timA_max_high - pre_timA_min_high <
1732 post_timA_max_high - post_timA_min_high)
1733 shift_402x = +1;
1734 else if (pre_timA_max_high - pre_timA_min_high >
1735 post_timA_max_high - post_timA_min_high)
1736 shift_402x = -1;
1737
1738 reg_4028 |=
1739 (ctrl->timings[channel][slotrank].val_4028 + shift_402x -
1740 post_timA_min_high) << (4 * slotrank);
1741 reg_4024 |=
1742 (ctrl->timings[channel][slotrank].val_4024 +
1743 shift_402x) << (8 * slotrank);
1744
1745 FOR_ALL_LANES {
1746 MCHBAR32(lane_registers[lane] + 0x10 + 0x100 * channel +
1747 4 * slotrank)
1748 =
1749 (((ctrl->timings[channel][slotrank].lanes[lane].
1750 timA + shift) & 0x3f)
1751 |
1752 ((ctrl->timings[channel][slotrank].lanes[lane].
1753 rising + shift) << 8)
1754 |
1755 (((ctrl->timings[channel][slotrank].lanes[lane].
1756 timA + shift -
1757 (post_timA_min_high << 6)) & 0x1c0) << 10)
Patrick Rudolph0188b132016-02-10 19:16:34 +01001758 | ((ctrl->timings[channel][slotrank].lanes[lane].
1759 falling + shift) << 20));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001760
1761 MCHBAR32(lane_registers[lane] + 0x20 + 0x100 * channel +
1762 4 * slotrank)
1763 =
1764 (((ctrl->timings[channel][slotrank].lanes[lane].
1765 timC + shift) & 0x3f)
1766 |
1767 (((ctrl->timings[channel][slotrank].lanes[lane].
1768 timB + shift) & 0x3f) << 8)
1769 |
1770 (((ctrl->timings[channel][slotrank].lanes[lane].
1771 timB + shift) & 0x1c0) << 9)
1772 |
1773 (((ctrl->timings[channel][slotrank].lanes[lane].
1774 timC + shift) & 0x40) << 13));
1775 }
1776 }
1777 MCHBAR32(0x4024 + 0x400 * channel) = reg_4024;
1778 MCHBAR32(0x4028 + 0x400 * channel) = reg_4028;
1779}
1780
1781static void test_timA(ramctr_timing * ctrl, int channel, int slotrank)
1782{
1783 wait_428c(channel);
1784
Patrick Rudolph371d2912015-10-09 13:33:25 +02001785 /* DRAM command MRS
1786 * write MR3 MPR enable
1787 * in this mode only RD and RDA are allowed
1788 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001789 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
1790 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1791 (0xc01 | (ctrl->tMOD << 16)));
1792 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1793 (slotrank << 24) | 0x360004);
1794 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1795
Patrick Rudolph371d2912015-10-09 13:33:25 +02001796 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001797 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
1798 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x4040c01);
1799 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, (slotrank << 24));
1800 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1801
Patrick Rudolph371d2912015-10-09 13:33:25 +02001802 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001803 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
1804 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1805 0x100f | ((ctrl->CAS + 36) << 16));
1806 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1807 (slotrank << 24) | 0x60000);
1808 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1809
Patrick Rudolph371d2912015-10-09 13:33:25 +02001810 /* DRAM command MRS
1811 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001812 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
1813 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1814 (0xc01 | (ctrl->tMOD << 16)));
1815 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1816 (slotrank << 24) | 0x360000);
1817 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1818
1819 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1820
1821 wait_428c(channel);
1822}
1823
1824static int does_lane_work(ramctr_timing * ctrl, int channel, int slotrank,
1825 int lane)
1826{
1827 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
1828 return ((read32
1829 (DEFAULT_MCHBAR + lane_registers[lane] + channel * 0x100 + 4 +
1830 ((timA / 32) & 1) * 4)
1831 >> (timA % 32)) & 1);
1832}
1833
1834struct run {
1835 int middle;
1836 int end;
1837 int start;
1838 int all;
1839 int length;
1840};
1841
1842static struct run get_longest_zero_run(int *seq, int sz)
1843{
1844 int i, ls;
1845 int bl = 0, bs = 0;
1846 struct run ret;
1847
1848 ls = 0;
1849 for (i = 0; i < 2 * sz; i++)
1850 if (seq[i % sz]) {
1851 if (i - ls > bl) {
1852 bl = i - ls;
1853 bs = ls;
1854 }
1855 ls = i + 1;
1856 }
1857 if (bl == 0) {
1858 ret.middle = sz / 2;
1859 ret.start = 0;
1860 ret.end = sz;
1861 ret.all = 1;
1862 return ret;
1863 }
1864
1865 ret.start = bs % sz;
1866 ret.end = (bs + bl - 1) % sz;
1867 ret.middle = (bs + (bl - 1) / 2) % sz;
1868 ret.length = bl;
1869 ret.all = 0;
1870
1871 return ret;
1872}
1873
1874static void discover_timA_coarse(ramctr_timing * ctrl, int channel,
1875 int slotrank, int *upperA)
1876{
1877 int timA;
1878 int statistics[NUM_LANES][128];
1879 int lane;
1880
1881 for (timA = 0; timA < 128; timA++) {
1882 FOR_ALL_LANES {
1883 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1884 }
1885 program_timings(ctrl, channel);
1886
1887 test_timA(ctrl, channel, slotrank);
1888
1889 FOR_ALL_LANES {
1890 statistics[lane][timA] =
1891 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001892 printram("Astat: %d, %d, %d: %x, %x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001893 channel, slotrank, lane, timA,
1894 statistics[lane][timA]);
1895 }
1896 }
1897 FOR_ALL_LANES {
1898 struct run rn = get_longest_zero_run(statistics[lane], 128);
1899 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1900 upperA[lane] = rn.end;
1901 if (upperA[lane] < rn.middle)
1902 upperA[lane] += 128;
Patrick Rudolpha649a542016-01-17 18:32:06 +01001903 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001904 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
Patrick Rudolpha649a542016-01-17 18:32:06 +01001905 printram("Aend: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001906 lane, upperA[lane]);
1907 }
1908}
1909
1910static void discover_timA_fine(ramctr_timing * ctrl, int channel, int slotrank,
1911 int *upperA)
1912{
1913 int timA_delta;
1914 int statistics[NUM_LANES][51];
1915 int lane, i;
1916
1917 memset(statistics, 0, sizeof(statistics));
1918
1919 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
1920 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
1921 timA = upperA[lane] + timA_delta + 0x40;
1922 program_timings(ctrl, channel);
1923
1924 for (i = 0; i < 100; i++) {
1925 test_timA(ctrl, channel, slotrank);
1926 FOR_ALL_LANES {
1927 statistics[lane][timA_delta + 25] +=
1928 does_lane_work(ctrl, channel, slotrank,
1929 lane);
1930 }
1931 }
1932 }
1933 FOR_ALL_LANES {
1934 int last_zero, first_all;
1935
1936 for (last_zero = -25; last_zero <= 25; last_zero++)
1937 if (statistics[lane][last_zero + 25])
1938 break;
1939 last_zero--;
1940 for (first_all = -25; first_all <= 25; first_all++)
1941 if (statistics[lane][first_all + 25] == 100)
1942 break;
1943
1944 printram("lane %d: %d, %d\n", lane, last_zero,
1945 first_all);
1946
1947 ctrl->timings[channel][slotrank].lanes[lane].timA =
1948 (last_zero + first_all) / 2 + upperA[lane];
Patrick Rudolpha649a542016-01-17 18:32:06 +01001949 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001950 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
1951 }
1952}
1953
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001954static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001955 int *upperA)
1956{
1957 int works[NUM_LANES];
1958 int lane;
1959 while (1) {
1960 int all_works = 1, some_works = 0;
1961 program_timings(ctrl, channel);
1962 test_timA(ctrl, channel, slotrank);
1963 FOR_ALL_LANES {
1964 works[lane] =
1965 !does_lane_work(ctrl, channel, slotrank, lane);
1966 if (works[lane])
1967 some_works = 1;
1968 else
1969 all_works = 0;
1970 }
1971 if (all_works)
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001972 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001973 if (!some_works) {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001974 if (ctrl->timings[channel][slotrank].val_4024 < 2) {
1975 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1976 channel, slotrank);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001977 return MAKE_ERR;
1978 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001979 ctrl->timings[channel][slotrank].val_4024 -= 2;
1980 printram("4024 -= 2;\n");
1981 continue;
1982 }
1983 ctrl->timings[channel][slotrank].val_4028 += 2;
1984 printram("4028 += 2;\n");
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001985 if (ctrl->timings[channel][slotrank].val_4028 >= 0x10) {
1986 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1987 channel, slotrank);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001988 return MAKE_ERR;
1989 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001990 FOR_ALL_LANES if (works[lane]) {
1991 ctrl->timings[channel][slotrank].lanes[lane].timA +=
1992 128;
1993 upperA[lane] += 128;
1994 printram("increment %d, %d, %d\n", channel,
1995 slotrank, lane);
1996 }
1997 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01001998 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07001999}
2000
2001struct timA_minmax {
2002 int timA_min_high, timA_max_high;
2003};
2004
2005static void pre_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
2006 struct timA_minmax *mnmx)
2007{
2008 int lane;
2009 mnmx->timA_min_high = 7;
2010 mnmx->timA_max_high = 0;
2011
2012 FOR_ALL_LANES {
2013 if (mnmx->timA_min_high >
2014 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
2015 mnmx->timA_min_high =
2016 (ctrl->timings[channel][slotrank].lanes[lane].
2017 timA >> 6);
2018 if (mnmx->timA_max_high <
2019 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
2020 mnmx->timA_max_high =
2021 (ctrl->timings[channel][slotrank].lanes[lane].
2022 timA >> 6);
2023 }
2024}
2025
2026static void post_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
2027 struct timA_minmax *mnmx)
2028{
2029 struct timA_minmax post;
2030 int shift_402x = 0;
2031
2032 /* Get changed maxima. */
2033 pre_timA_change(ctrl, channel, slotrank, &post);
2034
2035 if (mnmx->timA_max_high - mnmx->timA_min_high <
2036 post.timA_max_high - post.timA_min_high)
2037 shift_402x = +1;
2038 else if (mnmx->timA_max_high - mnmx->timA_min_high >
2039 post.timA_max_high - post.timA_min_high)
2040 shift_402x = -1;
2041 else
2042 shift_402x = 0;
2043
2044 ctrl->timings[channel][slotrank].val_4028 += shift_402x;
2045 ctrl->timings[channel][slotrank].val_4024 += shift_402x;
2046 printram("4024 += %d;\n", shift_402x);
2047 printram("4028 += %d;\n", shift_402x);
2048}
2049
Patrick Rudolph371d2912015-10-09 13:33:25 +02002050/* Compensate the skew between DQS and DQs.
2051 * To ease PCB design a small skew between Data Strobe signals and
2052 * Data Signals is allowed.
2053 * The controller has to measure and compensate this skew for every byte-lane.
2054 * By delaying either all DQs signals or DQS signal, a full phase
2055 * shift can be introduced.
2056 * It is assumed that one byte-lane's DQs signals have the same routing delay.
2057 *
2058 * To measure the actual skew, the DRAM is placed in "read leveling" mode.
2059 * In read leveling mode the DRAM-chip outputs an alternating periodic pattern.
2060 * The memory controller iterates over all possible values to do a full phase shift
2061 * and issues read commands.
2062 * With DQS and DQs in phase the data read is expected to alternate on every byte:
2063 * 0xFF 0x00 0xFF ...
2064 * Once the controller has detected this pattern a bit in the result register is
2065 * set for the current phase shift.
2066 */
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002067static int read_training(ramctr_timing * ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002068{
2069 int channel, slotrank, lane;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002070 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002071
2072 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002073 int all_high, some_high;
2074 int upperA[NUM_LANES];
2075 struct timA_minmax mnmx;
2076
2077 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002078
2079 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002080 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2081 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2082 0xc01 | (ctrl->tRP << 16));
2083 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2084 (slotrank << 24) | 0x60400);
2085 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2086 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2087
2088 write32(DEFAULT_MCHBAR + 0x3400, (slotrank << 2) | 0x8001);
2089
2090 ctrl->timings[channel][slotrank].val_4028 = 4;
2091 ctrl->timings[channel][slotrank].val_4024 = 55;
2092 program_timings(ctrl, channel);
2093
2094 discover_timA_coarse(ctrl, channel, slotrank, upperA);
2095
2096 all_high = 1;
2097 some_high = 0;
2098 FOR_ALL_LANES {
2099 if (ctrl->timings[channel][slotrank].lanes[lane].
2100 timA >= 0x40)
2101 some_high = 1;
2102 else
2103 all_high = 0;
2104 }
2105
2106 if (all_high) {
2107 ctrl->timings[channel][slotrank].val_4028--;
2108 printram("4028--;\n");
2109 FOR_ALL_LANES {
2110 ctrl->timings[channel][slotrank].lanes[lane].
2111 timA -= 0x40;
2112 upperA[lane] -= 0x40;
2113
2114 }
2115 } else if (some_high) {
2116 ctrl->timings[channel][slotrank].val_4024++;
2117 ctrl->timings[channel][slotrank].val_4028++;
2118 printram("4024++;\n");
2119 printram("4028++;\n");
2120 }
2121
2122 program_timings(ctrl, channel);
2123
2124 pre_timA_change(ctrl, channel, slotrank, &mnmx);
2125
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002126 err = discover_402x(ctrl, channel, slotrank, upperA);
2127 if (err)
2128 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002129
2130 post_timA_change(ctrl, channel, slotrank, &mnmx);
2131 pre_timA_change(ctrl, channel, slotrank, &mnmx);
2132
2133 discover_timA_fine(ctrl, channel, slotrank, upperA);
2134
2135 post_timA_change(ctrl, channel, slotrank, &mnmx);
2136 pre_timA_change(ctrl, channel, slotrank, &mnmx);
2137
2138 FOR_ALL_LANES {
2139 ctrl->timings[channel][slotrank].lanes[lane].timA -= mnmx.timA_min_high * 0x40;
2140 }
2141 ctrl->timings[channel][slotrank].val_4028 -= mnmx.timA_min_high;
2142 printram("4028 -= %d;\n", mnmx.timA_min_high);
2143
2144 post_timA_change(ctrl, channel, slotrank, &mnmx);
2145
2146 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
2147 ctrl->timings[channel][slotrank].val_4024,
2148 ctrl->timings[channel][slotrank].val_4028);
2149
Patrick Rudolpha649a542016-01-17 18:32:06 +01002150 printram("final results:\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002151 FOR_ALL_LANES
Patrick Rudolpha649a542016-01-17 18:32:06 +01002152 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002153 lane,
2154 ctrl->timings[channel][slotrank].lanes[lane].timA);
2155
2156 write32(DEFAULT_MCHBAR + 0x3400, 0);
2157
Patrick Rudolph9b515682015-10-09 13:43:51 +02002158 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002159 }
2160
2161 FOR_ALL_POPULATED_CHANNELS {
2162 program_timings(ctrl, channel);
2163 }
2164 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2165 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
2166 + 4 * lane, 0);
2167 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002168 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002169}
2170
2171static void test_timC(ramctr_timing * ctrl, int channel, int slotrank)
2172{
2173 int lane;
2174
2175 FOR_ALL_LANES {
2176 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + 4 * lane, 0);
2177 read32(DEFAULT_MCHBAR + 0x4140 + 0x400 * channel + 4 * lane);
2178 }
2179
2180 wait_428c(channel);
2181
Patrick Rudolph371d2912015-10-09 13:33:25 +02002182 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002183 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2184 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2185 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD) << 10)
2186 | 4 | (ctrl->tRCD << 16));
2187
2188 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2189 (slotrank << 24) | (6 << 16));
2190
2191 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
2192
Patrick Rudolph371d2912015-10-09 13:33:25 +02002193 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002194 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2195 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8041001);
2196 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2197 (slotrank << 24) | 8);
2198 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2199
Patrick Rudolph371d2912015-10-09 13:33:25 +02002200 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002201 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2202 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x80411f4);
2203 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
2204 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
2205
Patrick Rudolph371d2912015-10-09 13:33:25 +02002206 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002207 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2208 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2209 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2210 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2211 (slotrank << 24) | 8);
2212 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2213
2214 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2215
2216 wait_428c(channel);
2217
Patrick Rudolph371d2912015-10-09 13:33:25 +02002218 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002219 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2220 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2221 0xc01 | (ctrl->tRP << 16));
2222 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2223 (slotrank << 24) | 0x60400);
2224 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2225
Patrick Rudolph371d2912015-10-09 13:33:25 +02002226 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002227 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2228 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2229 (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) << 10)
2230 | 8 | (ctrl->CAS << 16));
2231
2232 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2233 (slotrank << 24) | 0x60000);
2234
2235 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x244);
2236
Patrick Rudolph371d2912015-10-09 13:33:25 +02002237 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002238 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2239 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2240 0x40011f4 | (max(ctrl->tRTP, 8) << 16));
2241 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
2242 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
2243
Patrick Rudolph371d2912015-10-09 13:33:25 +02002244 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002245 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2246 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2247 0xc01 | (ctrl->tRP << 16));
2248 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2249 (slotrank << 24) | 0x60400);
2250 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2251 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2252 wait_428c(channel);
2253}
2254
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002255static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002256{
2257 int timC;
2258 int statistics[NUM_LANES][MAX_TIMC + 1];
2259 int lane;
2260
2261 wait_428c(channel);
2262
Patrick Rudolph371d2912015-10-09 13:33:25 +02002263 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002264 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2265 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2266 0xc01 | (ctrl->tRP << 16));
2267 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2268 (slotrank << 24) | 0x60400);
2269 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2270 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2271
2272 for (timC = 0; timC <= MAX_TIMC; timC++) {
2273 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
2274 timC = timC;
2275 program_timings(ctrl, channel);
2276
2277 test_timC(ctrl, channel, slotrank);
2278
2279 FOR_ALL_LANES {
2280 statistics[lane][timC] =
2281 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2282 0x400 * channel);
2283 printram("Cstat: %d, %d, %d, %x, %x\n",
2284 channel, slotrank, lane, timC,
2285 statistics[lane][timC]);
2286 }
2287 }
2288 FOR_ALL_LANES {
2289 struct run rn =
2290 get_longest_zero_run(statistics[lane], MAX_TIMC + 1);
2291 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002292 if (rn.all) {
2293 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
2294 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002295 return MAKE_ERR;
2296 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01002297 printram("Cval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002298 lane, ctrl->timings[channel][slotrank].lanes[lane].timC);
2299 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002300 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002301}
2302
2303static int get_precedening_channels(ramctr_timing * ctrl, int target_channel)
2304{
2305 int channel, ret = 0;
2306 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
2307 ret++;
2308 return ret;
2309}
2310
2311static void fill_pattern0(ramctr_timing * ctrl, int channel, u32 a, u32 b)
2312{
2313 unsigned j;
2314 unsigned channel_offset =
2315 get_precedening_channels(ctrl, channel) * 0x40;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002316 for (j = 0; j < 16; j++)
2317 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
2318 sfence();
2319}
2320
2321static int num_of_channels(const ramctr_timing * ctrl)
2322{
2323 int ret = 0;
2324 int channel;
2325 FOR_ALL_POPULATED_CHANNELS ret++;
2326 return ret;
2327}
2328
2329static void fill_pattern1(ramctr_timing * ctrl, int channel)
2330{
2331 unsigned j;
2332 unsigned channel_offset =
2333 get_precedening_channels(ctrl, channel) * 0x40;
2334 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2335 for (j = 0; j < 16; j++)
2336 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
2337 for (j = 0; j < 16; j++)
2338 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
2339 sfence();
2340}
2341
2342static void precharge(ramctr_timing * ctrl)
2343{
2344 int channel, slotrank, lane;
2345
2346 FOR_ALL_POPULATED_CHANNELS {
2347 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2348 ctrl->timings[channel][slotrank].lanes[lane].falling =
2349 16;
2350 ctrl->timings[channel][slotrank].lanes[lane].rising =
2351 16;
Patrick Rudolpha649a542016-01-17 18:32:06 +01002352 }
2353
2354 program_timings(ctrl, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002355
2356 FOR_ALL_POPULATED_RANKS {
2357 wait_428c(channel);
2358
Patrick Rudolph371d2912015-10-09 13:33:25 +02002359 /* DRAM command MRS
2360 * write MR3 MPR enable
2361 * in this mode only RD and RDA are allowed
2362 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002363 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2364 0x1f000);
2365 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2366 0xc01 | (ctrl->tMOD << 16));
2367 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2368 (slotrank << 24) | 0x360004);
2369 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2370
Patrick Rudolph371d2912015-10-09 13:33:25 +02002371 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002372 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2373 0x1f105);
2374 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2375 0x4041003);
2376 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2377 (slotrank << 24) | 0);
2378 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2379
Patrick Rudolph371d2912015-10-09 13:33:25 +02002380 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002381 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2382 0x1f105);
2383 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2384 0x1001 | ((ctrl->CAS + 8) << 16));
2385 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2386 (slotrank << 24) | 0x60000);
2387 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2388
Patrick Rudolph371d2912015-10-09 13:33:25 +02002389 /* DRAM command MRS
2390 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002391 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2392 0x1f000);
2393 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2394 0xc01 | (ctrl->tMOD << 16));
2395 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2396 (slotrank << 24) | 0x360000);
2397 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2398 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2399 0xc0001);
2400
2401 wait_428c(channel);
2402 }
2403
2404 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2405 ctrl->timings[channel][slotrank].lanes[lane].falling =
2406 48;
2407 ctrl->timings[channel][slotrank].lanes[lane].rising =
2408 48;
2409 }
2410
2411 program_timings(ctrl, channel);
2412
2413 FOR_ALL_POPULATED_RANKS {
2414 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002415 /* DRAM command MRS
2416 * write MR3 MPR enable
2417 * in this mode only RD and RDA are allowed
2418 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002419 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2420 0x1f000);
2421 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2422 0xc01 | (ctrl->tMOD << 16));
2423 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2424 (slotrank << 24) | 0x360004);
2425 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2426
Patrick Rudolph371d2912015-10-09 13:33:25 +02002427 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002428 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2429 0x1f105);
2430 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2431 0x4041003);
2432 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2433 (slotrank << 24) | 0);
2434 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2435
Patrick Rudolph371d2912015-10-09 13:33:25 +02002436 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002437 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2438 0x1f105);
2439 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2440 0x1001 | ((ctrl->CAS + 8) << 16));
2441 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2442 (slotrank << 24) | 0x60000);
2443 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2444
Patrick Rudolph371d2912015-10-09 13:33:25 +02002445 /* DRAM command MRS
2446 * write MR3 MPR disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002447 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2448 0x1f000);
2449 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2450 0xc01 | (ctrl->tMOD << 16));
2451
2452 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2453 (slotrank << 24) | 0x360000);
2454 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2455
2456 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2457 0xc0001);
2458 wait_428c(channel);
2459 }
2460 }
2461}
2462
2463static void test_timB(ramctr_timing * ctrl, int channel, int slotrank)
2464{
Patrick Rudolph371d2912015-10-09 13:33:25 +02002465 /* enable DQs on this slotrank */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002466 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002467 0x80 | make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002468
2469 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002470 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002471 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f207);
2472 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2473 0x8000c01 | ((ctrl->CWL + ctrl->tWLO) << 16));
2474 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2475 8 | (slotrank << 24));
2476 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2477
Patrick Rudolph371d2912015-10-09 13:33:25 +02002478 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002479 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f107);
2480 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2481 0x4000c01 | ((ctrl->CAS + 38) << 16));
2482 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2483 (slotrank << 24) | 4);
2484 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2485
2486 write32(DEFAULT_MCHBAR + 0x400 * channel + 0x4284, 0x40001);
2487 wait_428c(channel);
2488
Patrick Rudolph371d2912015-10-09 13:33:25 +02002489 /* disable DQs on this slotrank */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002490 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002491 0x1080 | make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002492}
2493
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002494static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002495{
2496 int timB;
2497 int statistics[NUM_LANES][128];
2498 int lane;
2499
2500 write32(DEFAULT_MCHBAR + 0x3400, 0x108052 | (slotrank << 2));
2501
2502 for (timB = 0; timB < 128; timB++) {
2503 FOR_ALL_LANES {
2504 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
2505 }
2506 program_timings(ctrl, channel);
2507
2508 test_timB(ctrl, channel, slotrank);
2509
2510 FOR_ALL_LANES {
2511 statistics[lane][timB] =
2512 !((read32
2513 (DEFAULT_MCHBAR + lane_registers[lane] +
2514 channel * 0x100 + 4 + ((timB / 32) & 1) * 4)
2515 >> (timB % 32)) & 1);
Patrick Rudolpha649a542016-01-17 18:32:06 +01002516 printram("Bstat: %d, %d, %d: %x, %x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002517 channel, slotrank, lane, timB,
2518 statistics[lane][timB]);
2519 }
2520 }
2521 FOR_ALL_LANES {
2522 struct run rn = get_longest_zero_run(statistics[lane], 128);
Patrick Rudolphd912f1d2016-02-15 20:07:42 +01002523 /* timC is a direct function of timB's 6 LSBs.
2524 * Some tests increments the value of timB by a small value,
2525 * which might cause the 6bit value to overflow, if it's close
2526 * to 0x3F. Increment the value by a small offset if it's likely
2527 * to overflow, to make sure it won't overflow while running
2528 * tests and bricks the system due to a non matching timC.
2529 *
2530 * TODO: find out why some tests (edge write discovery)
2531 * increment timB. */
2532 if ((rn.start & 0x3F) == 0x3E)
2533 rn.start += 2;
2534 else if ((rn.start & 0x3F) == 0x3F)
2535 rn.start += 1;
Vladimir Serbinenko3141eac2016-01-29 19:42:02 +01002536 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002537 if (rn.all) {
2538 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
2539 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002540 return MAKE_ERR;
2541 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01002542 printram("Bval: %d, %d, %d: %x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002543 lane, ctrl->timings[channel][slotrank].lanes[lane].timB);
2544 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002545 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002546}
2547
2548static int get_timB_high_adjust(u64 val)
2549{
2550 int i;
2551
2552 /* good */
2553 if (val == 0xffffffffffffffffLL)
2554 return 0;
2555
2556 if (val >= 0xf000000000000000LL) {
2557 /* needs negative adjustment */
2558 for (i = 0; i < 8; i++)
2559 if (val << (8 * (7 - i) + 4))
2560 return -i;
2561 } else {
2562 /* needs positive adjustment */
2563 for (i = 0; i < 8; i++)
2564 if (val >> (8 * (7 - i) + 4))
2565 return i;
2566 }
2567 return 8;
2568}
2569
2570static void adjust_high_timB(ramctr_timing * ctrl)
2571{
2572 int channel, slotrank, lane, old;
2573 write32(DEFAULT_MCHBAR + 0x3400, 0x200);
2574 FOR_ALL_POPULATED_CHANNELS {
2575 fill_pattern1(ctrl, channel);
2576 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 1);
2577 }
2578 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
2579
2580 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x10001);
2581
2582 wait_428c(channel);
2583
Patrick Rudolph371d2912015-10-09 13:33:25 +02002584 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002585 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2586 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2587 0xc01 | (ctrl->tRCD << 16));
2588 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2589 (slotrank << 24) | 0x60000);
2590 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2591
Patrick Rudolph371d2912015-10-09 13:33:25 +02002592 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002593 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
2594 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8040c01);
2595 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2596 (slotrank << 24) | 0x8);
2597 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
2598
Patrick Rudolph371d2912015-10-09 13:33:25 +02002599 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002600 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
2601 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x8041003);
2602 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2603 (slotrank << 24));
2604 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x3e2);
2605
Patrick Rudolph371d2912015-10-09 13:33:25 +02002606 /* DRAM command NOP */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002607 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
2608 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2609 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
2610 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2611 (slotrank << 24) | 0x8);
2612 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
2613
2614 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2615
2616 wait_428c(channel);
2617
Patrick Rudolph371d2912015-10-09 13:33:25 +02002618 /* DRAM command PREA */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002619 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
2620 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2621 0xc01 | ((ctrl->tRP) << 16));
2622 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2623 (slotrank << 24) | 0x60400);
2624 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
2625
Patrick Rudolph371d2912015-10-09 13:33:25 +02002626 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002627 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
2628 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2629 0xc01 | ((ctrl->tRCD) << 16));
2630 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2631 (slotrank << 24) | 0x60000);
2632 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2633
Patrick Rudolph371d2912015-10-09 13:33:25 +02002634 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002635 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x3f105);
2636 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2637 0x4000c01 |
2638 ((ctrl->tRP +
2639 ctrl->timings[channel][slotrank].val_4024 +
2640 ctrl->timings[channel][slotrank].val_4028) << 16));
2641 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2642 (slotrank << 24) | 0x60008);
2643 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2644
2645 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
2646 wait_428c(channel);
2647 FOR_ALL_LANES {
2648 u64 res =
2649 read32(DEFAULT_MCHBAR + lane_registers[lane] +
2650 0x100 * channel + 4);
2651 res |=
2652 ((u64) read32(DEFAULT_MCHBAR + lane_registers[lane] +
2653 0x100 * channel + 8)) << 32;
2654 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
2655 ctrl->timings[channel][slotrank].lanes[lane].timB +=
2656 get_timB_high_adjust(res) * 64;
2657
Patrick Rudolpha649a542016-01-17 18:32:06 +01002658 printram("High adjust %d:%016llx\n", lane, res);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002659 printram("Bval+: %d, %d, %d, %x -> %x\n", channel,
2660 slotrank, lane, old,
2661 ctrl->timings[channel][slotrank].lanes[lane].
2662 timB);
2663 }
2664 }
2665 write32(DEFAULT_MCHBAR + 0x3400, 0);
2666}
2667
2668static void write_op(ramctr_timing * ctrl, int channel)
2669{
2670 int slotrank;
2671
2672 wait_428c(channel);
2673
2674 /* choose an existing rank. */
2675 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2676
Patrick Rudolph371d2912015-10-09 13:33:25 +02002677 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002678 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2679 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2680
2681 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2682 (slotrank << 24) | 0x60000);
2683
2684 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2685
2686 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2687 wait_428c(channel);
2688}
2689
Patrick Rudolph371d2912015-10-09 13:33:25 +02002690/* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
2691 * DDR3 adopted the fly-by topology. The data and strobes signals reach
2692 * the chips at different times with respect to command, address and
2693 * clock signals.
2694 * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase
2695 * shift can be introduced.
2696 * It is assumed that the CLK/ADDR/CMD signals have the same routing delay.
2697 *
2698 * To find the required phase shift the DRAM is placed in "write leveling" mode.
2699 * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the
2700 * sampled value on the data lanes (DQs).
2701 */
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002702static int write_training(ramctr_timing * ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002703{
2704 int channel, slotrank, lane;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002705 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002706
2707 FOR_ALL_POPULATED_CHANNELS
2708 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
2709 read32(DEFAULT_MCHBAR + 0x4008 +
2710 0x400 * channel) | 0x8000000);
2711
2712 FOR_ALL_POPULATED_CHANNELS {
2713 write_op(ctrl, channel);
2714 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2715 read32(DEFAULT_MCHBAR + 0x4020 +
2716 0x400 * channel) | 0x200000);
2717 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002718
2719 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002720 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2721 FOR_ALL_POPULATED_CHANNELS {
2722 write_op(ctrl, channel);
2723 }
2724
Patrick Rudolph371d2912015-10-09 13:33:25 +02002725 /* enable write leveling on all ranks
2726 * disable all DQ outputs
2727 * only NOP is allowed in this mode */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002728 FOR_ALL_CHANNELS
2729 FOR_ALL_POPULATED_RANKS
2730 write_mrreg(ctrl, channel, slotrank, 1,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002731 make_mr1(ctrl, slotrank, channel) | 0x1080);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002732
2733 write32(DEFAULT_MCHBAR + 0x3400, 0x108052);
2734
Patrick Rudolph9b515682015-10-09 13:43:51 +02002735 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002736
Patrick Rudolph371d2912015-10-09 13:33:25 +02002737 /* set any valid value for timB, it gets corrected later */
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002738 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2739 err = discover_timB(ctrl, channel, slotrank);
2740 if (err)
2741 return err;
2742 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002743
Patrick Rudolph371d2912015-10-09 13:33:25 +02002744 /* disable write leveling on all ranks */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002745 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2746 write_mrreg(ctrl, channel,
Patrick Rudolph7e513d12016-01-10 14:22:34 +01002747 slotrank, 1, make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002748
2749 write32(DEFAULT_MCHBAR + 0x3400, 0);
2750
2751 FOR_ALL_POPULATED_CHANNELS
2752 wait_428c(channel);
2753
Patrick Rudolph371d2912015-10-09 13:33:25 +02002754 /* refresh enable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002755 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) | 8);
2756
2757 FOR_ALL_POPULATED_CHANNELS {
2758 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2759 ~0x00200000 & read32(DEFAULT_MCHBAR + 0x4020 +
2760 0x400 * channel));
2761 read32(DEFAULT_MCHBAR + 0x428c + 0x400 * channel);
2762 wait_428c(channel);
2763
Patrick Rudolph371d2912015-10-09 13:33:25 +02002764 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002765 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2766 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
2767 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, 0x60000);
2768 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2769
2770 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2771 wait_428c(channel);
2772 }
2773
Patrick Rudolph9b515682015-10-09 13:43:51 +02002774 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002775
2776 printram("CPE\n");
2777 precharge(ctrl);
2778 printram("CPF\n");
2779
2780 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2781 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2782 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2783 0);
2784 }
2785
2786 FOR_ALL_POPULATED_CHANNELS {
2787 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
2788 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2789 }
2790
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002791 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2792 err = discover_timC(ctrl, channel, slotrank);
2793 if (err)
2794 return err;
2795 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002796
2797 FOR_ALL_POPULATED_CHANNELS
2798 program_timings(ctrl, channel);
2799
Patrick Rudolph371d2912015-10-09 13:33:25 +02002800 /* measure and adjust timB timings */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002801 adjust_high_timB(ctrl);
2802
2803 FOR_ALL_POPULATED_CHANNELS
2804 program_timings(ctrl, channel);
2805
2806 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2807 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2808 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2809 0);
2810 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01002811 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002812}
2813
2814static int test_320c(ramctr_timing * ctrl, int channel, int slotrank)
2815{
2816 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2817 int timC_delta;
2818 int lanes_ok = 0;
2819 int ctr = 0;
2820 int lane;
2821
2822 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2823 FOR_ALL_LANES {
2824 ctrl->timings[channel][slotrank].lanes[lane].timC =
2825 saved_rt.lanes[lane].timC + timC_delta;
2826 }
2827 program_timings(ctrl, channel);
2828 FOR_ALL_LANES {
2829 write32(DEFAULT_MCHBAR + 4 * lane + 0x4f40, 0);
2830 }
2831
2832 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2833
2834 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002835 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002836 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2837 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2838 ((max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1)) << 10)
2839 | 8 | (ctrl->tRCD << 16));
2840
2841 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2842 (slotrank << 24) | ctr | 0x60000);
2843
2844 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02002845 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002846 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2847 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2848 0x8001020 | ((ctrl->CWL + ctrl->tWTR + 8) << 16));
2849 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2850 (slotrank << 24));
2851 write32(DEFAULT_MCHBAR + 0x4244 + 0x400 * channel, 0x389abcd);
2852 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x20e42);
2853
Patrick Rudolph371d2912015-10-09 13:33:25 +02002854 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002855 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2856 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2857 0x4001020 | (max(ctrl->tRTP, 8) << 16));
2858 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2859 (slotrank << 24));
2860 write32(DEFAULT_MCHBAR + 0x4248 + 0x400 * channel, 0x389abcd);
2861 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x20e42);
2862
Patrick Rudolph371d2912015-10-09 13:33:25 +02002863 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002864 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2865 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, 0xf1001);
2866 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2867 (slotrank << 24) | 0x60400);
2868 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2869
2870 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2871 wait_428c(channel);
2872 FOR_ALL_LANES {
2873 u32 r32 =
2874 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2875 0x400 * channel);
2876
2877 if (r32 == 0)
2878 lanes_ok |= 1 << lane;
2879 }
2880 ctr++;
2881 if (lanes_ok == ((1 << NUM_LANES) - 1))
2882 break;
2883 }
2884
2885 ctrl->timings[channel][slotrank] = saved_rt;
2886
2887 printram("3lanes: %x\n", lanes_ok);
2888 return lanes_ok != ((1 << NUM_LANES) - 1);
2889}
2890
2891#include "raminit_patterns.h"
2892
2893static void fill_pattern5(ramctr_timing * ctrl, int channel, int patno)
2894{
2895 unsigned i, j;
2896 unsigned channel_offset =
2897 get_precedening_channels(ctrl, channel) * 0x40;
2898 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2899
2900 if (patno) {
2901 u8 base8 = 0x80 >> ((patno - 1) % 8);
2902 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2903 for (i = 0; i < 32; i++) {
2904 for (j = 0; j < 16; j++) {
2905 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
2906 if (invert[patno - 1][i] & (1 << (j / 2)))
2907 val = ~val;
2908 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2909 j * 4), val);
2910 }
2911 }
2912
2913 } else {
2914 for (i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++) {
2915 for (j = 0; j < 16; j++)
2916 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2917 j * 4), pattern[i][j]);
2918 }
2919 sfence();
2920 }
2921}
2922
2923static void reprogram_320c(ramctr_timing * ctrl)
2924{
2925 int channel, slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002926
2927 FOR_ALL_POPULATED_CHANNELS {
2928 wait_428c(channel);
2929
2930 /* choose an existing rank. */
2931 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2932
Patrick Rudolph371d2912015-10-09 13:33:25 +02002933 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002934 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2935 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2936
2937 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2938 (slotrank << 24) | 0x60000);
2939
2940 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2941
2942 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2943 wait_428c(channel);
2944 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2945 read32(DEFAULT_MCHBAR + 0x4020 +
2946 0x400 * channel) | 0x200000);
2947 }
Patrick Rudolph371d2912015-10-09 13:33:25 +02002948
2949 /* refresh disable */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002950 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2951 FOR_ALL_POPULATED_CHANNELS {
2952 wait_428c(channel);
2953
2954 /* choose an existing rank. */
2955 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2956
Patrick Rudolph371d2912015-10-09 13:33:25 +02002957 /* DRAM command ZQCS */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002958 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2959 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2960
2961 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2962 (slotrank << 24) | 0x60000);
2963
2964 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2965
2966 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2967 wait_428c(channel);
2968 }
2969
2970 /* jedec reset */
2971 dram_jedecreset(ctrl);
2972 /* mrs commands. */
2973 dram_mrscommands(ctrl);
2974
Patrick Rudolph9b515682015-10-09 13:43:51 +02002975 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002976}
2977
2978#define MIN_C320C_LEN 13
2979
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002980static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002981{
2982 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002983 int slotrank;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002984 int c320c;
2985 int stat[NUM_SLOTRANKS][256];
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002986 int delta = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002987
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002988 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2989
2990 FOR_ALL_POPULATED_RANKS {
2991 saved_timings[channel][slotrank] =
2992 ctrl->timings[channel][slotrank];
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002993 }
2994
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002995 ctrl->cmd_stretch[channel] = cmd_stretch;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002996
Patrick Rudolph45d6a552016-11-16 19:21:31 +01002997 MCHBAR32(0x4004 + 0x400 * channel) =
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07002998 ctrl->tRRD
2999 | (ctrl->tRTP << 4)
3000 | (ctrl->tCKE << 8)
3001 | (ctrl->tWTR << 12)
3002 | (ctrl->tFAW << 16)
3003 | (ctrl->tWR << 24)
3004 | (ctrl->cmd_stretch[channel] << 30);
3005
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003006 if (ctrl->cmd_stretch[channel] == 2)
3007 delta = 2;
3008 else if (ctrl->cmd_stretch[channel] == 0)
3009 delta = 4;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003010
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003011 FOR_ALL_POPULATED_RANKS {
3012 ctrl->timings[channel][slotrank].val_4024 -= delta;
3013 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003014
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003015 for (c320c = -127; c320c <= 127; c320c++) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003016 FOR_ALL_POPULATED_RANKS {
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003017 ctrl->timings[channel][slotrank].val_320c = c320c;
3018 }
3019 program_timings(ctrl, channel);
3020 reprogram_320c(ctrl);
3021 FOR_ALL_POPULATED_RANKS {
3022 stat[slotrank][c320c + 127] =
3023 test_320c(ctrl, channel, slotrank);
3024 printram("3stat: %d, %d, %d: %x\n",
3025 channel, slotrank, c320c,
3026 stat[slotrank][c320c + 127]);
3027 }
3028 }
3029 FOR_ALL_POPULATED_RANKS {
3030 struct run rn =
3031 get_longest_zero_run(stat[slotrank], 255);
3032 ctrl->timings[channel][slotrank].val_320c =
3033 rn.middle - 127;
3034 printram("3val: %d, %d: %d\n", channel,
3035 slotrank,
3036 ctrl->timings[channel][slotrank].val_320c);
3037 if (rn.all || rn.length < MIN_C320C_LEN) {
3038 FOR_ALL_POPULATED_RANKS {
3039 ctrl->timings[channel][slotrank] =
3040 saved_timings[channel][slotrank];
3041 }
3042 return MAKE_ERR;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003043 }
3044 }
3045
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003046 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003047}
3048
Patrick Rudolph371d2912015-10-09 13:33:25 +02003049/* Adjust CMD phase shift and try multiple command rates.
3050 * A command rate of 2T doubles the time needed for address and
3051 * command decode. */
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003052static int command_training(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003053{
3054 int channel;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003055 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003056
3057 FOR_ALL_POPULATED_CHANNELS {
3058 fill_pattern5(ctrl, channel, 0);
3059 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
3060 }
3061
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003062 FOR_ALL_POPULATED_CHANNELS {
3063 /* try command rate 1T and 2T */
3064 err = try_cmd_stretch(ctrl, channel, 0);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003065 if (err) {
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003066 err = try_cmd_stretch(ctrl, channel, 2);
3067 if (err) {
3068 printk(BIOS_EMERG, "c320c discovery failed\n");
3069 return err;
3070 }
3071 printram("Using CMD rate 2T on channel %u\n", channel);
3072 } else
3073 printram("Using CMD rate 1T on channel %u\n", channel);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003074 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003075
Patrick Rudolph45d6a552016-11-16 19:21:31 +01003076 FOR_ALL_POPULATED_CHANNELS
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003077 program_timings(ctrl, channel);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003078
3079 reprogram_320c(ctrl);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003080 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003081}
3082
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003083static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003084 int *edges)
3085{
3086 int edge;
3087 int statistics[NUM_LANES][MAX_EDGE_TIMING + 1];
3088 int lane;
3089
3090 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
3091 FOR_ALL_LANES {
3092 ctrl->timings[channel][slotrank].lanes[lane].rising =
3093 edge;
3094 ctrl->timings[channel][slotrank].lanes[lane].falling =
3095 edge;
3096 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003097 program_timings(ctrl, channel);
3098
3099 FOR_ALL_LANES {
3100 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
3101 4 * lane, 0);
3102 read32(DEFAULT_MCHBAR + 0x400 * channel + 4 * lane +
3103 0x4140);
3104 }
3105
3106 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003107 /* DRAM command MRS
3108 * write MR3 MPR enable
3109 * in this mode only RD and RDA are allowed
3110 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003111 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
3112 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3113 (0xc01 | (ctrl->tMOD << 16)));
3114 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3115 (slotrank << 24) | 0x360004);
3116 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3117
Patrick Rudolph371d2912015-10-09 13:33:25 +02003118 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003119 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
3120 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x40411f4);
3121 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3122 (slotrank << 24));
3123 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3124
Patrick Rudolph371d2912015-10-09 13:33:25 +02003125 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003126 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
3127 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3128 0x1001 | ((ctrl->CAS + 8) << 16));
3129 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3130 (slotrank << 24) | 0x60000);
3131 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3132
Patrick Rudolph371d2912015-10-09 13:33:25 +02003133 /* DRAM command MRS
3134 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003135 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
3136 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3137 (0xc01 | (ctrl->tMOD << 16)));
3138 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3139 (slotrank << 24) | 0x360000);
3140 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3141
3142 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
3143
3144 wait_428c(channel);
3145
3146 FOR_ALL_LANES {
3147 statistics[lane][edge] =
3148 read32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
3149 lane * 4);
3150 }
3151 }
3152 FOR_ALL_LANES {
3153 struct run rn =
3154 get_longest_zero_run(statistics[lane], MAX_EDGE_TIMING + 1);
3155 edges[lane] = rn.middle;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003156 if (rn.all) {
3157 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n",
3158 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003159 return MAKE_ERR;
3160 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01003161 printram("eval %d, %d, %d: %02x\n", channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003162 lane, edges[lane]);
3163 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003164 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003165}
3166
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003167static int discover_edges(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003168{
3169 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3170 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3171 int channel, slotrank, lane;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003172 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003173
3174 write32(DEFAULT_MCHBAR + 0x3400, 0);
3175
Patrick Rudolph9b515682015-10-09 13:43:51 +02003176 toggle_io_reset();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003177
3178 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
3179 write32(DEFAULT_MCHBAR + 4 * lane +
3180 0x400 * channel + 0x4080, 0);
3181 }
3182
3183 FOR_ALL_POPULATED_CHANNELS {
3184 fill_pattern0(ctrl, channel, 0, 0);
3185 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3186 FOR_ALL_LANES {
3187 read32(DEFAULT_MCHBAR + 0x400 * channel +
3188 lane * 4 + 0x4140);
3189 }
3190
3191 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3192 ctrl->timings[channel][slotrank].lanes[lane].falling =
3193 16;
3194 ctrl->timings[channel][slotrank].lanes[lane].rising =
3195 16;
3196 }
3197
3198 program_timings(ctrl, channel);
3199
3200 FOR_ALL_POPULATED_RANKS {
3201 wait_428c(channel);
3202
Patrick Rudolph371d2912015-10-09 13:33:25 +02003203 /* DRAM command MRS
3204 * MR3 enable MPR
3205 * write MR3 MPR enable
3206 * in this mode only RD and RDA are allowed
3207 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003208 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3209 0x1f000);
3210 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3211 0xc01 | (ctrl->tMOD << 16));
3212 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3213 (slotrank << 24) | 0x360004);
3214 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3215
Patrick Rudolph371d2912015-10-09 13:33:25 +02003216 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003217 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3218 0x1f105);
3219 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3220 0x4041003);
3221 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3222 (slotrank << 24) | 0);
3223 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3224
Patrick Rudolph371d2912015-10-09 13:33:25 +02003225 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003226 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3227 0x1f105);
3228 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3229 0x1001 | ((ctrl->CAS + 8) << 16));
3230 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3231 (slotrank << 24) | 0x60000);
3232 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3233
Patrick Rudolph371d2912015-10-09 13:33:25 +02003234 /* DRAM command MRS
3235 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003236 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3237 0x1f000);
3238 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3239 0xc01 | (ctrl->tMOD << 16));
3240 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3241 (slotrank << 24) | 0x360000);
3242 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3243 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3244 0xc0001);
3245
3246 wait_428c(channel);
3247 }
3248
Patrick Rudolph371d2912015-10-09 13:33:25 +02003249 /* XXX: check any measured value ? */
3250
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003251 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3252 ctrl->timings[channel][slotrank].lanes[lane].falling =
3253 48;
3254 ctrl->timings[channel][slotrank].lanes[lane].rising =
3255 48;
3256 }
3257
3258 program_timings(ctrl, channel);
3259
3260 FOR_ALL_POPULATED_RANKS {
3261 wait_428c(channel);
3262
Patrick Rudolph371d2912015-10-09 13:33:25 +02003263 /* DRAM command MRS
3264 * MR3 enable MPR
3265 * write MR3 MPR enable
3266 * in this mode only RD and RDA are allowed
3267 * all reads return a predefined pattern */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003268 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3269 0x1f000);
3270 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3271 0xc01 | (ctrl->tMOD << 16));
3272 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3273 (slotrank << 24) | 0x360004);
3274 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
3275
Patrick Rudolph371d2912015-10-09 13:33:25 +02003276 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003277 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3278 0x1f105);
3279 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3280 0x4041003);
3281 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3282 (slotrank << 24) | 0);
3283 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
3284
Patrick Rudolph371d2912015-10-09 13:33:25 +02003285 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003286 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3287 0x1f105);
3288 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3289 0x1001 | ((ctrl->CAS + 8) << 16));
3290 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3291 (slotrank << 24) | 0x60000);
3292 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
3293
Patrick Rudolph371d2912015-10-09 13:33:25 +02003294 /* DRAM command MRS
3295 * MR3 disable MPR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003296 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3297 0x1f000);
3298 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3299 0xc01 | (ctrl->tMOD << 16));
3300 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3301 (slotrank << 24) | 0x360000);
3302 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3303
3304 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3305 0xc0001);
3306 wait_428c(channel);
3307 }
3308
Patrick Rudolph371d2912015-10-09 13:33:25 +02003309 /* XXX: check any measured value ? */
3310
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003311 FOR_ALL_LANES {
3312 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel +
3313 lane * 4,
3314 ~read32(DEFAULT_MCHBAR + 0x4040 +
3315 0x400 * channel + lane * 4) & 0xff);
3316 }
3317
3318 fill_pattern0(ctrl, channel, 0, 0xffffffff);
3319 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3320 }
3321
3322 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3323 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003324 printram("discover falling edges:\n[%x] = %x\n", 0x4eb0, 0x300);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003325
3326 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003327 err = discover_edges_real(ctrl, channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003328 falling_edges[channel][slotrank]);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003329 if (err)
3330 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003331 }
3332
3333 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003334 printram("discover rising edges:\n[%x] = %x\n", 0x4eb0, 0x200);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003335
3336 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003337 err = discover_edges_real(ctrl, channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003338 rising_edges[channel][slotrank]);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003339 if (err)
3340 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003341 }
3342
3343 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3344
3345 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3346 ctrl->timings[channel][slotrank].lanes[lane].falling =
3347 falling_edges[channel][slotrank][lane];
3348 ctrl->timings[channel][slotrank].lanes[lane].rising =
3349 rising_edges[channel][slotrank][lane];
3350 }
3351
3352 FOR_ALL_POPULATED_CHANNELS {
3353 program_timings(ctrl, channel);
3354 }
3355
3356 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3357 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3358 0);
3359 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003360 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003361}
3362
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003363static int discover_edges_write_real(ramctr_timing *ctrl, int channel,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003364 int slotrank, int *edges)
3365{
3366 int edge;
3367 u32 raw_statistics[MAX_EDGE_TIMING + 1];
3368 int statistics[MAX_EDGE_TIMING + 1];
3369 const int reg3000b24[] = { 0, 0xc, 0x2c };
3370 int lane, i;
3371 int lower[NUM_LANES];
3372 int upper[NUM_LANES];
3373 int pat;
3374
3375 FOR_ALL_LANES {
3376 lower[lane] = 0;
3377 upper[lane] = MAX_EDGE_TIMING;
3378 }
3379
3380 for (i = 0; i < 3; i++) {
3381 write32(DEFAULT_MCHBAR + 0x3000 + 0x100 * channel,
3382 reg3000b24[i] << 24);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003383 printram("[%x] = 0x%08x\n",
3384 0x3000 + 0x100 * channel, reg3000b24[i] << 24);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003385 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3386 fill_pattern5(ctrl, channel, pat);
3387 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003388 printram("using pattern %d\n", pat);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003389 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
3390 FOR_ALL_LANES {
3391 ctrl->timings[channel][slotrank].lanes[lane].
3392 rising = edge;
3393 ctrl->timings[channel][slotrank].lanes[lane].
3394 falling = edge;
3395 }
3396 program_timings(ctrl, channel);
3397
3398 FOR_ALL_LANES {
3399 write32(DEFAULT_MCHBAR + 0x4340 +
3400 0x400 * channel + 4 * lane, 0);
3401 read32(DEFAULT_MCHBAR + 0x400 * channel +
3402 4 * lane + 0x4140);
3403 }
3404 wait_428c(channel);
3405
Patrick Rudolph371d2912015-10-09 13:33:25 +02003406 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003407 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
3408 0x1f006);
3409 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3410 0x4 | (ctrl->tRCD << 16)
3411 | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) <<
3412 10));
3413 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3414 (slotrank << 24) | 0x60000);
3415 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel,
3416 0x240);
3417
Patrick Rudolph371d2912015-10-09 13:33:25 +02003418 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003419 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
3420 0x1f201);
3421 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3422 0x8005020 | ((ctrl->tWTR + ctrl->CWL + 8) <<
3423 16));
3424 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
3425 (slotrank << 24));
3426 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel,
3427 0x242);
3428
Patrick Rudolph371d2912015-10-09 13:33:25 +02003429 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003430 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
3431 0x1f105);
3432 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
3433 0x4005020 | (max(ctrl->tRTP, 8) << 16));
3434 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
3435 (slotrank << 24));
3436 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel,
3437 0x242);
3438
Patrick Rudolph371d2912015-10-09 13:33:25 +02003439 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003440 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
3441 0x1f002);
3442 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
3443 0xc01 | (ctrl->tRP << 16));
3444 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
3445 (slotrank << 24) | 0x60400);
3446 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
3447
3448 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
3449 0xc0001);
3450 wait_428c(channel);
3451 FOR_ALL_LANES {
3452 read32(DEFAULT_MCHBAR + 0x4340 +
3453 0x400 * channel + lane * 4);
3454 }
3455
3456 raw_statistics[edge] =
3457 MCHBAR32(0x436c + 0x400 * channel);
3458 }
3459 FOR_ALL_LANES {
3460 struct run rn;
3461 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
3462 statistics[edge] =
3463 ! !(raw_statistics[edge] & (1 << lane));
3464 rn = get_longest_zero_run(statistics,
3465 MAX_EDGE_TIMING + 1);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003466 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003467 channel, slotrank, i, rn.start, rn.middle,
3468 rn.end, rn.start + ctrl->edge_offset[i],
3469 rn.end - ctrl->edge_offset[i]);
3470 lower[lane] =
3471 max(rn.start + ctrl->edge_offset[i], lower[lane]);
3472 upper[lane] =
3473 min(rn.end - ctrl->edge_offset[i], upper[lane]);
3474 edges[lane] = (lower[lane] + upper[lane]) / 2;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003475 if (rn.all || (lower[lane] > upper[lane])) {
3476 printk(BIOS_EMERG, "edge write discovery failed: %d, %d, %d\n",
3477 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003478 return MAKE_ERR;
3479 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003480 }
3481 }
3482 }
3483
3484 write32(DEFAULT_MCHBAR + 0x3000, 0);
3485 printram("CPA\n");
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003486 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003487}
3488
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003489static int discover_edges_write(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003490{
3491 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3492 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3493 int channel, slotrank, lane;
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003494 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003495
3496 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
3497 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003498 printram("discover falling edges write:\n[%x] = %x\n", 0x4eb0, 0x300);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003499
3500 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003501 err = discover_edges_write_real(ctrl, channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003502 falling_edges[channel][slotrank]);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003503 if (err)
3504 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003505 }
3506
3507 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003508 printram("discover rising edges write:\n[%x] = %x\n", 0x4eb0, 0x200);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003509
3510 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003511 err = discover_edges_write_real(ctrl, channel, slotrank,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003512 rising_edges[channel][slotrank]);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003513 if (err)
3514 return err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003515 }
3516
3517 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3518
3519 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3520 ctrl->timings[channel][slotrank].lanes[lane].falling =
3521 falling_edges[channel][slotrank][lane];
3522 ctrl->timings[channel][slotrank].lanes[lane].rising =
3523 rising_edges[channel][slotrank][lane];
3524 }
3525
3526 FOR_ALL_POPULATED_CHANNELS
3527 program_timings(ctrl, channel);
3528
3529 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3530 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
3531 0);
3532 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003533 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003534}
3535
3536static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
3537{
3538 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003539 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003540 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
3541 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
3542 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD)
3543 << 10) | (ctrl->tRCD << 16) | 4);
3544 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
3545 (slotrank << 24) | 0x60000);
3546 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
3547
Patrick Rudolph371d2912015-10-09 13:33:25 +02003548 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003549 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
3550 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
3551 0x80011e0 |
3552 ((ctrl->tWTR + ctrl->CWL + 8) << 16));
3553 write32(DEFAULT_MCHBAR + 0x4204 +
3554 0x400 * channel, (slotrank << 24));
3555 write32(DEFAULT_MCHBAR + 0x4214 +
3556 0x400 * channel, 0x242);
3557
Patrick Rudolph371d2912015-10-09 13:33:25 +02003558 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003559 write32(DEFAULT_MCHBAR + 0x4228 +
3560 0x400 * channel, 0x1f105);
3561 write32(DEFAULT_MCHBAR + 0x4238 +
3562 0x400 * channel,
3563 0x40011e0 | (max(ctrl->tRTP, 8) << 16));
3564 write32(DEFAULT_MCHBAR + 0x4208 +
3565 0x400 * channel, (slotrank << 24));
3566 write32(DEFAULT_MCHBAR + 0x4218 +
3567 0x400 * channel, 0x242);
3568
Patrick Rudolph371d2912015-10-09 13:33:25 +02003569 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003570 write32(DEFAULT_MCHBAR + 0x422c +
3571 0x400 * channel, 0x1f002);
3572 write32(DEFAULT_MCHBAR + 0x423c +
3573 0x400 * channel,
3574 0x1001 | (ctrl->tRP << 16));
3575 write32(DEFAULT_MCHBAR + 0x420c +
3576 0x400 * channel,
3577 (slotrank << 24) | 0x60400);
3578 write32(DEFAULT_MCHBAR + 0x421c +
3579 0x400 * channel, 0);
3580
3581 write32(DEFAULT_MCHBAR + 0x4284 +
3582 0x400 * channel, 0xc0001);
3583 wait_428c(channel);
3584}
3585
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003586static int discover_timC_write(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003587{
3588 const u8 rege3c_b24[3] = { 0, 0xf, 0x2f };
3589 int i, pat;
3590
3591 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3592 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
3593 int channel, slotrank, lane;
3594
3595 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3596 lower[channel][slotrank][lane] = 0;
3597 upper[channel][slotrank][lane] = MAX_TIMC;
3598 }
3599
3600 write32(DEFAULT_MCHBAR + 0x4ea8, 1);
Patrick Rudolpha649a542016-01-17 18:32:06 +01003601 printram("discover timC write:\n");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003602
3603 for (i = 0; i < 3; i++)
3604 FOR_ALL_POPULATED_CHANNELS {
3605 write32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100),
3606 (rege3c_b24[i] << 24)
3607 | (read32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100))
3608 & ~0x3f000000));
3609 udelay(2);
3610 for (pat = 0; pat < NUM_PATTERNS; pat++) {
3611 FOR_ALL_POPULATED_RANKS {
3612 int timC;
3613 u32 raw_statistics[MAX_TIMC + 1];
3614 int statistics[MAX_TIMC + 1];
3615
Patrick Rudolph77e45d32016-02-28 16:14:45 +01003616 /* Make sure rn.start < rn.end */
3617 statistics[MAX_TIMC] = 1;
3618
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003619 fill_pattern5(ctrl, channel, pat);
3620 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
Patrick Rudolph77e45d32016-02-28 16:14:45 +01003621 for (timC = 0; timC < MAX_TIMC; timC++) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003622 FOR_ALL_LANES
3623 ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
3624 program_timings(ctrl, channel);
3625
3626 test_timC_write (ctrl, channel, slotrank);
3627
3628 raw_statistics[timC] =
3629 MCHBAR32(0x436c + 0x400 * channel);
3630 }
3631 FOR_ALL_LANES {
3632 struct run rn;
Patrick Rudolph77e45d32016-02-28 16:14:45 +01003633 for (timC = 0; timC < MAX_TIMC; timC++)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003634 statistics[timC] =
3635 !!(raw_statistics[timC] &
3636 (1 << lane));
Patrick Rudolph77e45d32016-02-28 16:14:45 +01003637
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003638 rn = get_longest_zero_run(statistics,
3639 MAX_TIMC + 1);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003640 if (rn.all) {
3641 printk(BIOS_EMERG, "timC write discovery failed: %d, %d, %d\n",
3642 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003643 return MAKE_ERR;
3644 }
Patrick Rudolpha649a542016-01-17 18:32:06 +01003645 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003646 channel, slotrank, i, rn.start,
3647 rn.middle, rn.end,
3648 rn.start + ctrl->timC_offset[i],
3649 rn.end - ctrl->timC_offset[i]);
3650 lower[channel][slotrank][lane] =
3651 max(rn.start + ctrl->timC_offset[i],
3652 lower[channel][slotrank][lane]);
3653 upper[channel][slotrank][lane] =
3654 min(rn.end - ctrl->timC_offset[i],
3655 upper[channel][slotrank][lane]);
3656
3657 }
3658 }
3659 }
3660 }
3661
3662 FOR_ALL_CHANNELS {
3663 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3664 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3665 ~0x3f000000));
3666 udelay(2);
3667 }
3668
3669 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3670
3671 printram("CPB\n");
3672
3673 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
Patrick Rudolpha649a542016-01-17 18:32:06 +01003674 printram("timC %d, %d, %d: %x\n", channel,
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003675 slotrank, lane,
3676 (lower[channel][slotrank][lane] +
3677 upper[channel][slotrank][lane]) / 2);
3678 ctrl->timings[channel][slotrank].lanes[lane].timC =
3679 (lower[channel][slotrank][lane] +
3680 upper[channel][slotrank][lane]) / 2;
3681 }
3682 FOR_ALL_POPULATED_CHANNELS {
3683 program_timings(ctrl, channel);
3684 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003685 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003686}
3687
3688static void normalize_training(ramctr_timing * ctrl)
3689{
3690 int channel, slotrank, lane;
3691 int mat = 0;
3692
3693 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3694 int delta;
3695 FOR_ALL_LANES mat =
3696 max(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
3697 delta = (mat >> 6) - ctrl->timings[channel][slotrank].val_4028;
3698 ctrl->timings[channel][slotrank].val_4024 += delta;
3699 ctrl->timings[channel][slotrank].val_4028 += delta;
3700 }
3701
3702 FOR_ALL_POPULATED_CHANNELS {
3703 program_timings(ctrl, channel);
3704 }
3705}
3706
3707static void write_controller_mr(ramctr_timing * ctrl)
3708{
3709 int channel, slotrank;
3710
3711 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3712 write32(DEFAULT_MCHBAR + 0x0004 + (channel << 8) +
3713 lane_registers[slotrank], make_mr0(ctrl, slotrank));
3714 write32(DEFAULT_MCHBAR + 0x0008 + (channel << 8) +
Patrick Rudolph7e513d12016-01-10 14:22:34 +01003715 lane_registers[slotrank],
3716 make_mr1(ctrl, slotrank, channel));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003717 }
3718}
3719
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003720static int channel_test(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003721{
3722 int channel, slotrank, lane;
3723
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003724 slotrank = 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003725 FOR_ALL_POPULATED_CHANNELS
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003726 if (read32(DEFAULT_MCHBAR + 0x42a0 + (channel << 10)) & 0xa000) {
3727 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n",
3728 channel);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003729 return MAKE_ERR;
3730 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003731 FOR_ALL_POPULATED_CHANNELS {
3732 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
3733
3734 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3735 }
3736
3737 for (slotrank = 0; slotrank < 4; slotrank++)
3738 FOR_ALL_CHANNELS
3739 if (ctrl->rankmap[channel] & (1 << slotrank)) {
3740 FOR_ALL_LANES {
3741 write32(DEFAULT_MCHBAR + (0x4f40 + 4 * lane), 0);
3742 write32(DEFAULT_MCHBAR + (0x4d40 + 4 * lane), 0);
3743 }
3744 wait_428c(channel);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003745 /* DRAM command ACT */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003746 write32(DEFAULT_MCHBAR + 0x4220 + (channel << 10), 0x0001f006);
3747 write32(DEFAULT_MCHBAR + 0x4230 + (channel << 10), 0x0028a004);
3748 write32(DEFAULT_MCHBAR + 0x4200 + (channel << 10),
3749 0x00060000 | (slotrank << 24));
3750 write32(DEFAULT_MCHBAR + 0x4210 + (channel << 10), 0x00000244);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003751 /* DRAM command WR */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003752 write32(DEFAULT_MCHBAR + 0x4224 + (channel << 10), 0x0001f201);
3753 write32(DEFAULT_MCHBAR + 0x4234 + (channel << 10), 0x08281064);
3754 write32(DEFAULT_MCHBAR + 0x4204 + (channel << 10),
3755 0x00000000 | (slotrank << 24));
3756 write32(DEFAULT_MCHBAR + 0x4214 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003757 /* DRAM command RD */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003758 write32(DEFAULT_MCHBAR + 0x4228 + (channel << 10), 0x0001f105);
3759 write32(DEFAULT_MCHBAR + 0x4238 + (channel << 10), 0x04281064);
3760 write32(DEFAULT_MCHBAR + 0x4208 + (channel << 10),
3761 0x00000000 | (slotrank << 24));
3762 write32(DEFAULT_MCHBAR + 0x4218 + (channel << 10), 0x00000242);
Patrick Rudolph371d2912015-10-09 13:33:25 +02003763 /* DRAM command PRE */
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003764 write32(DEFAULT_MCHBAR + 0x422c + (channel << 10), 0x0001f002);
3765 write32(DEFAULT_MCHBAR + 0x423c + (channel << 10), 0x00280c01);
3766 write32(DEFAULT_MCHBAR + 0x420c + (channel << 10),
3767 0x00060400 | (slotrank << 24));
3768 write32(DEFAULT_MCHBAR + 0x421c + (channel << 10), 0x00000240);
3769 write32(DEFAULT_MCHBAR + 0x4284 + (channel << 10), 0x000c0001);
3770 wait_428c(channel);
3771 FOR_ALL_LANES
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003772 if (read32(DEFAULT_MCHBAR + 0x4340 + (channel << 10) + 4 * lane)) {
3773 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
3774 channel, slotrank, lane);
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003775 return MAKE_ERR;
3776 }
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003777 }
Patrick Rudolph24a845b2016-03-25 18:19:47 +01003778 return 0;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003779}
3780
3781static void set_scrambling_seed(ramctr_timing * ctrl)
3782{
3783 int channel;
3784
3785 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them?
3786 I don't think so. */
3787 static u32 seeds[NUM_CHANNELS][3] = {
3788 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
3789 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
3790 };
3791 FOR_ALL_POPULATED_CHANNELS {
3792 MCHBAR32(0x4020 + 0x400 * channel) &= ~0x10000000;
3793 write32(DEFAULT_MCHBAR + 0x4034, seeds[channel][0]);
3794 write32(DEFAULT_MCHBAR + 0x403c, seeds[channel][1]);
3795 write32(DEFAULT_MCHBAR + 0x4038, seeds[channel][2]);
3796 }
3797}
3798
3799static void set_4f8c(void)
3800{
3801 struct cpuid_result cpures;
3802 u32 cpu;
3803
Ryan Salsamendie4da9aa2016-06-24 12:01:11 -07003804 cpures = cpuid(1);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003805 cpu = (cpures.eax);
3806 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
3807 MCHBAR32(0x4f8c) = 0x141D1519;
3808 } else {
3809 MCHBAR32(0x4f8c) = 0x551D1519;
3810 }
3811}
3812
3813static void prepare_training(ramctr_timing * ctrl)
3814{
3815 int channel;
3816
3817 FOR_ALL_POPULATED_CHANNELS {
3818 // Always drive command bus
3819 MCHBAR32(0x4004 + 0x400 * channel) |= 0x20000000;
3820 }
3821
3822 udelay(1);
3823
3824 FOR_ALL_POPULATED_CHANNELS {
3825 wait_428c(channel);
3826 }
3827}
3828
3829static void set_4008c(ramctr_timing * ctrl)
3830{
3831 int channel, slotrank;
3832 u32 reg;
3833 FOR_ALL_POPULATED_CHANNELS {
3834 u32 b20, b4_8_12;
3835 int min_320c = 10000;
3836 int max_320c = -10000;
3837
3838 FOR_ALL_POPULATED_RANKS {
3839 max_320c = max(ctrl->timings[channel][slotrank].val_320c, max_320c);
3840 min_320c = min(ctrl->timings[channel][slotrank].val_320c, min_320c);
3841 }
3842
3843 if (max_320c - min_320c > 51)
3844 b20 = 0;
3845 else
3846 b20 = ctrl->ref_card_offset[channel];
3847
3848 if (ctrl->reg_320c_range_threshold < max_320c - min_320c)
3849 b4_8_12 = 0x3330;
3850 else
3851 b4_8_12 = 0x2220;
3852
3853 reg = read32(DEFAULT_MCHBAR + 0x400c + (channel << 10));
3854 write32(DEFAULT_MCHBAR + 0x400c + (channel << 10),
3855 (reg & 0xFFF0FFFF)
3856 | (ctrl->ref_card_offset[channel] << 16)
3857 | (ctrl->ref_card_offset[channel] << 18));
3858 write32(DEFAULT_MCHBAR + 0x4008 + (channel << 10),
3859 0x0a000000
3860 | (b20 << 20)
3861 | ((ctrl->ref_card_offset[channel] + 2) << 16)
3862 | b4_8_12);
3863 }
3864}
3865
3866static void set_42a0(ramctr_timing * ctrl)
3867{
3868 int channel;
3869 FOR_ALL_POPULATED_CHANNELS {
3870 write32(DEFAULT_MCHBAR + (0x42a0 + 0x400 * channel),
3871 0x00001000 | ctrl->rankmap[channel]);
3872 MCHBAR32(0x4004 + 0x400 * channel) &= ~0x20000000; // OK
3873 }
3874}
3875
3876static int encode_5d10(int ns)
3877{
3878 return (ns + 499) / 500;
3879}
3880
3881/* FIXME: values in this function should be hardware revision-dependent. */
3882static void final_registers(ramctr_timing * ctrl)
3883{
3884 int channel;
3885 int t1_cycles = 0, t1_ns = 0, t2_ns;
3886 int t3_ns;
3887 u32 r32;
3888
3889 write32(DEFAULT_MCHBAR + 0x4cd4, 0x00000046);
3890
3891 write32(DEFAULT_MCHBAR + 0x400c, (read32(DEFAULT_MCHBAR + 0x400c) & 0xFFFFCFFF) | 0x1000); // OK
3892 write32(DEFAULT_MCHBAR + 0x440c, (read32(DEFAULT_MCHBAR + 0x440c) & 0xFFFFCFFF) | 0x1000); // OK
3893 write32(DEFAULT_MCHBAR + 0x4cb0, 0x00000740);
3894 write32(DEFAULT_MCHBAR + 0x4380, 0x00000aaa); // OK
3895 write32(DEFAULT_MCHBAR + 0x4780, 0x00000aaa); // OK
3896 write32(DEFAULT_MCHBAR + 0x4f88, 0x5f7003ff); // OK
3897 write32(DEFAULT_MCHBAR + 0x5064, 0x00073000 | ctrl->reg_5064b0); // OK
3898
3899 FOR_ALL_CHANNELS {
3900 switch (ctrl->rankmap[channel]) {
3901 /* Unpopulated channel. */
3902 case 0:
3903 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0);
3904 break;
3905 /* Only single-ranked dimms. */
3906 case 1:
3907 case 4:
3908 case 5:
3909 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x373131);
3910 break;
3911 /* Dual-ranked dimms present. */
3912 default:
3913 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x9b6ea1);
3914 break;
3915 }
3916 }
3917
3918 write32 (DEFAULT_MCHBAR + 0x5880, 0xca9171e5);
3919 write32 (DEFAULT_MCHBAR + 0x5888,
3920 (read32 (DEFAULT_MCHBAR + 0x5888) & ~0xffffff) | 0xe4d5d0);
3921 write32 (DEFAULT_MCHBAR + 0x58a8, read32 (DEFAULT_MCHBAR + 0x58a8) & ~0x1f);
3922 write32 (DEFAULT_MCHBAR + 0x4294,
3923 (read32 (DEFAULT_MCHBAR + 0x4294) & ~0x30000)
3924 | (1 << 16));
3925 write32 (DEFAULT_MCHBAR + 0x4694,
3926 (read32 (DEFAULT_MCHBAR + 0x4694) & ~0x30000)
3927 | (1 << 16));
3928
3929 MCHBAR32(0x5030) |= 1; // OK
3930 MCHBAR32(0x5030) |= 0x80; // OK
3931 MCHBAR32(0x5f18) = 0xfa; // OK
3932
3933 /* Find a populated channel. */
3934 FOR_ALL_POPULATED_CHANNELS
3935 break;
3936
3937 t1_cycles = ((read32(DEFAULT_MCHBAR + 0x4290 + channel * 0x400) >> 8) & 0xff);
3938 r32 = read32(DEFAULT_MCHBAR + 0x5064);
3939 if (r32 & 0x20000)
3940 t1_cycles += (r32 & 0xfff);
3941 t1_cycles += (read32(DEFAULT_MCHBAR + channel * 0x400 + 0x42a4) & 0xfff);
3942 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
3943 if (!(r32 & 0x20000))
3944 t1_ns += 500;
3945
3946 t2_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f10) >> 8) & 0xfff);
3947 if ( read32(DEFAULT_MCHBAR + 0x5f00) & 8 )
3948 {
3949 t3_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f20) >> 8) & 0xfff);
3950 t3_ns += 10 * (read32(DEFAULT_MCHBAR + 0x5f18) & 0xff);
3951 }
3952 else
3953 {
3954 t3_ns = 500;
3955 }
3956 printk(BIOS_DEBUG, "t123: %d, %d, %d\n",
3957 t1_ns, t2_ns, t3_ns);
3958 write32 (DEFAULT_MCHBAR + 0x5d10,
3959 ((encode_5d10(t1_ns) + encode_5d10(t2_ns)) << 16)
3960 | (encode_5d10(t1_ns) << 8)
3961 | ((encode_5d10(t3_ns) + encode_5d10(t2_ns) + encode_5d10(t1_ns)) << 24)
3962 | (read32(DEFAULT_MCHBAR + 0x5d10) & 0xC0C0C0C0)
3963 | 0xc);
3964}
3965
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +02003966static void save_timings(ramctr_timing *ctrl)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003967{
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003968 /* Save the MRC S3 restore data to cbmem */
Patrick Rudolphbb9c90a2016-05-29 17:05:06 +02003969 store_current_mrc_cache(ctrl, sizeof(*ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07003970}
3971
3972static void restore_timings(ramctr_timing * ctrl)
3973{
3974 int channel, slotrank, lane;
3975
3976 FOR_ALL_POPULATED_CHANNELS
3977 MCHBAR32(0x4004 + 0x400 * channel) =
3978 ctrl->tRRD
3979 | (ctrl->tRTP << 4)
3980 | (ctrl->tCKE << 8)
3981 | (ctrl->tWTR << 12)
3982 | (ctrl->tFAW << 16)
3983 | (ctrl->tWR << 24)
3984 | (ctrl->cmd_stretch[channel] << 30);
3985
3986 udelay(1);
3987
3988 FOR_ALL_POPULATED_CHANNELS {
3989 wait_428c(channel);
3990 }
3991
3992 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3993 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
3994 + 4 * lane, 0);
3995 }
3996
3997 FOR_ALL_POPULATED_CHANNELS
3998 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
3999 read32(DEFAULT_MCHBAR + 0x4008 +
4000 0x400 * channel) | 0x8000000);
4001
4002 FOR_ALL_POPULATED_CHANNELS {
4003 udelay (1);
4004 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
4005 read32(DEFAULT_MCHBAR + 0x4020 +
4006 0x400 * channel) | 0x200000);
4007 }
4008
4009 printram("CPE\n");
4010
4011 write32(DEFAULT_MCHBAR + 0x3400, 0);
4012 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
4013
4014 printram("CP5b\n");
4015
4016 FOR_ALL_POPULATED_CHANNELS {
4017 program_timings(ctrl, channel);
4018 }
4019
4020 u32 reg, addr;
4021
Elyes HAOUAS7db506c2016-10-02 11:56:39 +02004022 while (!(MCHBAR32(0x5084) & 0x10000));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004023 do {
4024 reg = MCHBAR32(0x428c);
4025 } while ((reg & 0x14) == 0);
4026
4027 // Set state of memory controller
4028 MCHBAR32(0x5030) = 0x116;
4029 MCHBAR32(0x4ea0) = 0;
4030
4031 // Wait 500us
4032 udelay(500);
4033
4034 FOR_ALL_CHANNELS {
4035 // Set valid rank CKE
4036 reg = 0;
4037 reg = (reg & ~0xf) | ctrl->rankmap[channel];
4038 addr = 0x400 * channel + 0x42a0;
4039 MCHBAR32(addr) = reg;
4040
4041 // Wait 10ns for ranks to settle
4042 //udelay(0.01);
4043
4044 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
4045 MCHBAR32(addr) = reg;
4046
4047 // Write reset using a NOP
4048 write_reset(ctrl);
4049 }
4050
4051 /* mrs commands. */
4052 dram_mrscommands(ctrl);
4053
4054 printram("CP5c\n");
4055
4056 write32(DEFAULT_MCHBAR + 0x3000, 0);
4057
4058 FOR_ALL_CHANNELS {
4059 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
4060 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
4061 ~0x3f000000));
4062 udelay(2);
4063 }
4064
4065 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
4066}
4067
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004068static int try_init_dram_ddr3(ramctr_timing *ctrl, int fast_boot,
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004069 int s3_resume, int me_uma_size)
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004070{
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004071 int err;
4072
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004073 printk(BIOS_DEBUG, "Starting RAM training (%d).\n", fast_boot);
4074
4075 if (!fast_boot) {
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004076 /* Find fastest common supported parameters */
4077 dram_find_common_params(ctrl);
4078
4079 dram_dimm_mapping(ctrl);
4080 }
4081
4082 /* Set MCU frequency */
4083 dram_freq(ctrl);
4084
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004085 if (!fast_boot) {
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004086 /* Calculate timings */
4087 dram_timing(ctrl);
4088 }
4089
4090 /* Set version register */
4091 MCHBAR32(0x5034) = 0xC04EB002;
4092
4093 /* Enable crossover */
4094 dram_xover(ctrl);
4095
4096 /* Set timing and refresh registers */
4097 dram_timing_regs(ctrl);
4098
4099 /* Power mode preset */
4100 MCHBAR32(0x4e80) = 0x5500;
4101
4102 /* Set scheduler parameters */
4103 MCHBAR32(0x4c20) = 0x10100005;
4104
Elyes HAOUAS15279a92016-07-28 21:05:26 +02004105 /* Set CPU specific register */
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004106 set_4f8c();
4107
4108 /* Clear IO reset bit */
4109 MCHBAR32(0x5030) &= ~0x20;
4110
4111 /* Set MAD-DIMM registers */
4112 dram_dimm_set_mapping(ctrl);
4113 printk(BIOS_DEBUG, "Done dimm mapping\n");
4114
4115 /* Zone config */
4116 dram_zones(ctrl, 1);
4117
4118 /* Set memory map */
4119 dram_memorymap(ctrl, me_uma_size);
4120 printk(BIOS_DEBUG, "Done memory map\n");
4121
4122 /* Set IO registers */
4123 dram_ioregs(ctrl);
4124 printk(BIOS_DEBUG, "Done io registers\n");
4125
4126 udelay(1);
4127
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004128 if (fast_boot) {
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004129 restore_timings(ctrl);
4130 } else {
4131 /* Do jedec ddr3 reset sequence */
4132 dram_jedecreset(ctrl);
4133 printk(BIOS_DEBUG, "Done jedec reset\n");
4134
4135 /* MRS commands */
4136 dram_mrscommands(ctrl);
4137 printk(BIOS_DEBUG, "Done MRS commands\n");
4138
4139 /* Prepare for memory training */
4140 prepare_training(ctrl);
4141
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004142 err = read_training(ctrl);
4143 if (err)
4144 return err;
4145
4146 err = write_training(ctrl);
4147 if (err)
4148 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004149
4150 printram("CP5a\n");
4151
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004152 err = discover_edges(ctrl);
4153 if (err)
4154 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004155
4156 printram("CP5b\n");
4157
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004158 err = command_training(ctrl);
4159 if (err)
4160 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004161
4162 printram("CP5c\n");
4163
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004164 err = discover_edges_write(ctrl);
4165 if (err)
4166 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004167
Patrick Rudolph24a845b2016-03-25 18:19:47 +01004168 err = discover_timC_write(ctrl);
4169 if (err)
4170 return err;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004171
4172 normalize_training(ctrl);
4173 }
4174
4175 set_4008c(ctrl);
4176
4177 write_controller_mr(ctrl);
4178
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004179 if (!s3_resume) {
4180 err = channel_test(ctrl);
4181 if (err)
4182 return err;
4183 }
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004184
4185 return 0;
4186}
4187
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +02004188static void init_dram_ddr3(int mobile, int min_tck, int s3resume)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004189{
4190 int me_uma_size;
4191 int cbmem_was_inited;
Patrick Rudolph735ecce2016-03-26 10:42:27 +01004192 ramctr_timing ctrl;
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004193 int fast_boot;
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +02004194 spd_raw_data spds[4];
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004195 struct mrc_data_container *mrc_cache;
4196 ramctr_timing *ctrl_cached;
Patrick Rudolph31d19592016-03-26 12:22:34 +01004197 int err;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004198
4199 MCHBAR32(0x5f00) |= 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +02004200
Vadim Bendebury7a3f36a2012-04-18 15:47:32 -07004201 report_platform_info();
4202
Stefan Reinauer00636b02012-04-04 00:08:51 +02004203 /* Wait for ME to be ready */
4204 intel_early_me_init();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004205 me_uma_size = intel_early_me_uma_size();
Stefan Reinauer00636b02012-04-04 00:08:51 +02004206
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004207 printk(BIOS_DEBUG, "Starting native Platform init\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +02004208
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004209 u32 reg_5d10;
Stefan Reinauer00636b02012-04-04 00:08:51 +02004210
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004211 wait_txt_clear();
Stefan Reinauer00636b02012-04-04 00:08:51 +02004212
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004213 wrmsr(0x000002e6, (msr_t) { .lo = 0, .hi = 0 });
Stefan Reinauer00636b02012-04-04 00:08:51 +02004214
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004215 reg_5d10 = read32(DEFAULT_MCHBAR + 0x5d10); // !!! = 0x00000000
4216 if ((pcie_read_config16(SOUTHBRIDGE, 0xa2) & 0xa0) == 0x20 /* 0x0004 */
4217 && reg_5d10 && !s3resume) {
4218 write32(DEFAULT_MCHBAR + 0x5d10, 0);
4219 /* Need reset. */
Stefan Reinauer00636b02012-04-04 00:08:51 +02004220 outb(0x6, 0xcf9);
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004221
Patrick Georgi546953c2014-11-29 10:38:17 +01004222 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02004223 }
Stefan Reinauer00636b02012-04-04 00:08:51 +02004224
Elyes HAOUAS0d4b11a2016-10-03 21:57:21 +02004225 memset(&ctrl, 0, sizeof(ctrl));
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004226
4227 early_pch_init_native();
4228 early_thermal_init();
4229
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004230 /* try to find timings in MRC cache */
4231 mrc_cache = find_current_mrc_cache();
4232 if (!mrc_cache || (mrc_cache->mrc_data_size < sizeof(ctrl))) {
4233 if (s3resume) {
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004234 /* Failed S3 resume, reset to come up cleanly */
4235 outb(0x6, 0xcf9);
4236 halt();
Stefan Reinauer00636b02012-04-04 00:08:51 +02004237 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004238 ctrl_cached = NULL;
Patrick Rudolph27e085a2016-03-26 10:59:02 +01004239 } else {
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004240 ctrl_cached = (ramctr_timing *)mrc_cache->mrc_data;
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004241 }
4242
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004243 /* verify MRC cache for fast boot */
Kyösti Mälkki38cb8222016-11-18 19:25:52 +02004244 if (!s3resume && ctrl_cached) {
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +02004245 /* Load SPD unique information data. */
4246 memset(spds, 0, sizeof(spds));
4247 mainboard_get_spd(spds, 1);
4248
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004249 /* check SPD CRC16 to make sure the DIMMs haven't been replaced */
4250 fast_boot = verify_crc16_spds_ddr3(spds, ctrl_cached);
4251 if (!fast_boot)
4252 printk(BIOS_DEBUG, "Stored timings CRC16 mismatch.\n");
Kyösti Mälkki38cb8222016-11-18 19:25:52 +02004253 } else {
4254 fast_boot = s3resume;
4255 }
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004256
4257 if (fast_boot) {
4258 printk(BIOS_DEBUG, "Trying stored timings.\n");
4259 memcpy(&ctrl, ctrl_cached, sizeof(ctrl));
4260
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004261 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004262 if (err) {
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004263 if (s3resume) {
4264 /* Failed S3 resume, reset to come up cleanly */
4265 outb(0x6, 0xcf9);
4266 halt();
4267 }
4268 /* no need to erase bad mrc cache here, it gets overwritten on
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004269 * successful boot. */
4270 printk(BIOS_ERR, "Stored timings are invalid !\n");
4271 fast_boot = 0;
4272 }
4273 }
4274 if (!fast_boot) {
4275 ctrl.mobile = mobile;
4276 ctrl.tCK = min_tck;
4277
4278 /* Get DDR3 SPD data */
Kyösti Mälkkie258b9a2016-11-18 19:59:23 +02004279 memset(spds, 0, sizeof(spds));
4280 mainboard_get_spd(spds, 0);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004281 dram_find_spds_ddr3(spds, &ctrl);
4282
Patrick Rudolph588ccaa2016-04-20 18:00:27 +02004283 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004284 }
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +01004285
Patrick Rudolph2ccb74b2016-03-26 12:16:29 +01004286 if (err) {
4287 /* fallback: disable failing channel */
4288 printk(BIOS_ERR, "RAM training failed, trying fallback.\n");
4289 printram("Disable failing channel.\n");
4290
4291 /* Reset DDR3 frequency */
4292 dram_find_spds_ddr3(spds, &ctrl);
4293
4294 /* disable failing channel */
4295 disable_channel(&ctrl, GET_ERR_CHANNEL(err));
4296
4297 err = try_init_dram_ddr3(&ctrl, fast_boot, s3resume, me_uma_size);
4298 }
4299
Patrick Rudolph31d19592016-03-26 12:22:34 +01004300 if (err)
4301 die("raminit failed");
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004302
4303 /* FIXME: should be hardware revision-dependent. */
4304 write32(DEFAULT_MCHBAR + 0x5024, 0x00a030ce);
4305
4306 set_scrambling_seed(&ctrl);
4307
4308 set_42a0(&ctrl);
4309
4310 final_registers(&ctrl);
4311
4312 /* Zone config */
4313 dram_zones(&ctrl, 0);
4314
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004315 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004316 quick_ram_check();
4317
4318 intel_early_me_status();
4319 intel_early_me_init_done(ME_INIT_STATUS_SUCCESS);
4320 intel_early_me_status();
4321
Stefan Reinauer00636b02012-04-04 00:08:51 +02004322 report_memory_config();
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004323
4324 cbmem_was_inited = !cbmem_recovery(s3resume);
Patrick Rudolph56abd4d2016-03-13 11:07:45 +01004325 if (!fast_boot)
Alexandru Gagniucecf2eb42015-09-28 21:39:12 -07004326 save_timings(&ctrl);
4327 if (s3resume && !cbmem_was_inited) {
4328 /* Failed S3 resume, reset to come up cleanly */
4329 outb(0x6, 0xcf9);
4330 halt();
4331 }
Patrick Rudolphb97009e2016-02-28 15:24:04 +01004332
Patrick Rudolph735ecce2016-03-26 10:42:27 +01004333 fill_smbios17(&ctrl);
Stefan Reinauer00636b02012-04-04 00:08:51 +02004334}
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004335
4336#define HOST_BRIDGE PCI_DEVFN(0, 0)
4337#define DEFAULT_TCK TCK_800MHZ
4338
4339static unsigned int get_mem_min_tck(void)
4340{
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004341 u32 reg32;
4342 u8 rev;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004343 const struct device *dev;
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004344 const struct northbridge_intel_sandybridge_config *cfg = NULL;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004345
4346 dev = dev_find_slot(0, HOST_BRIDGE);
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004347 if (dev)
4348 cfg = dev->chip_info;
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004349
4350 /* If this is zero, it just means devicetree.cb didn't set it */
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004351 if (!cfg || cfg->max_mem_clock_mhz == 0) {
4352 rev = pci_read_config8(PCI_DEV(0, 0, 0), PCI_DEVICE_ID);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004353
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004354 if ((rev & BASE_REV_MASK) == BASE_REV_SNB) {
4355 /* read Capabilities A Register DMFC bits */
4356 reg32 = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_A);
4357 reg32 &= 0x7;
4358
4359 switch (reg32) {
4360 case 7: return TCK_533MHZ;
4361 case 6: return TCK_666MHZ;
4362 case 5: return TCK_800MHZ;
4363 /* reserved: */
4364 default:
4365 break;
4366 }
4367 } else {
4368 /* read Capabilities B Register DMFC bits */
4369 reg32 = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_B);
4370 reg32 = (reg32 >> 4) & 0x7;
4371
4372 switch (reg32) {
4373 case 7: return TCK_533MHZ;
4374 case 6: return TCK_666MHZ;
4375 case 5: return TCK_800MHZ;
4376 case 4: return TCK_933MHZ;
4377 case 3: return TCK_1066MHZ;
4378 case 2: return TCK_1200MHZ;
4379 case 1: return TCK_1333MHZ;
4380 /* reserved: */
4381 default:
4382 break;
4383 }
4384 }
4385 return DEFAULT_TCK;
4386 } else {
Patrick Rudolph7bddd302016-06-11 18:39:35 +02004387 if (cfg->max_mem_clock_mhz >= 1066)
4388 return TCK_1066MHZ;
4389 else if (cfg->max_mem_clock_mhz >= 933)
4390 return TCK_933MHZ;
4391 else if (cfg->max_mem_clock_mhz >= 800)
Patrick Rudolph9f3f9152016-01-26 20:02:14 +01004392 return TCK_800MHZ;
4393 else if (cfg->max_mem_clock_mhz >= 666)
4394 return TCK_666MHZ;
4395 else if (cfg->max_mem_clock_mhz >= 533)
4396 return TCK_533MHZ;
4397 else
4398 return TCK_400MHZ;
4399 }
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004400}
4401
Patrick Rudolph266a1f72016-06-09 18:13:34 +02004402#define DEFAULT_PCI_MMIO_SIZE 2048
4403
4404static unsigned int get_mmio_size(void)
4405{
4406 const struct device *dev;
4407 const struct northbridge_intel_sandybridge_config *cfg = NULL;
4408
4409 dev = dev_find_slot(0, HOST_BRIDGE);
4410 if (dev)
4411 cfg = dev->chip_info;
4412
4413 /* If this is zero, it just means devicetree.cb didn't set it */
4414 if (!cfg || cfg->pci_mmio_size == 0)
4415 return DEFAULT_PCI_MMIO_SIZE;
4416 else
4417 return cfg->pci_mmio_size;
4418}
4419
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004420void perform_raminit(int s3resume)
4421{
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004422 post_code(0x3a);
4423
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004424 timestamp_add_now(TS_BEFORE_INITRAM);
4425
Kyösti Mälkki4cb44e52016-11-18 19:11:24 +02004426 init_dram_ddr3(1, get_mem_min_tck(), s3resume);
Vladimir Serbinenkoffbb3c02016-02-10 01:36:25 +01004427}