blob: 3e769ec5a4e32e0631d2d4b08920d9cc060ce4ba [file] [log] [blame]
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 Damien Zammit <damien@zamaudio.com>
5 * Copyright (C) 2014 Vladimir Serbinenko <phcoder@gmail.com>
6 * Copyright (C) 2016 Patrick Rudolph <siro@das-labor.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <console/console.h>
19#include <console/usb.h>
20#include <string.h>
21#include <arch/io.h>
22#include <cbmem.h>
23#include <arch/cbfs.h>
24#include <cbfs.h>
25#include <northbridge/intel/sandybridge/chip.h>
26#include <device/pci_def.h>
27#include <delay.h>
28#include <arch/cpu.h>
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +010029#include "raminit_native.h"
30#include "raminit_common.h"
31#include "sandybridge.h"
32
33/* FIXME: no ECC support. */
34/* FIXME: no support for 3-channel chipsets. */
35
36/*
37 * Register description:
38 * Intel provides a command queue of depth four.
39 * Every command is configured by using multiple registers.
40 * On executing the command queue you have to provide the depth used.
41 *
42 * Known registers:
43 * Channel X = [0, 1]
44 * Command queue index Y = [0, 1, 2, 3]
45 *
46 * DEFAULT_MCHBAR + 0x4220 + 0x400 * X + 4 * Y: command io register
47 * Controls the DRAM command signals
48 * Bit 0: !RAS
49 * Bit 1: !CAS
50 * Bit 2: !WE
51 *
52 * DEFAULT_MCHBAR + 0x4200 + 0x400 * X + 4 * Y: addr bankslot io register
53 * Controls the address, bank address and slotrank signals
54 * Bit 0-15 : Address
55 * Bit 20-22: Bank Address
56 * Bit 24-25: slotrank
57 *
58 * DEFAULT_MCHBAR + 0x4230 + 0x400 * X + 4 * Y: idle register
59 * Controls the idle time after issuing this DRAM command
60 * Bit 16-32: number of clock-cylces to idle
61 *
62 * DEFAULT_MCHBAR + 0x4284 + 0x400 * channel: execute command queue
63 * Starts to execute all queued commands
64 * Bit 0 : start DRAM command execution
65 * Bit 16-20: (number of queued commands - 1) * 4
66 */
67
68static void sfence(void)
69{
70 asm volatile ("sfence");
71}
72
73static void toggle_io_reset(void) {
74 /* toggle IO reset bit */
75 u32 r32 = read32(DEFAULT_MCHBAR + 0x5030);
76 write32(DEFAULT_MCHBAR + 0x5030, r32 | 0x20);
77 udelay(1);
78 write32(DEFAULT_MCHBAR + 0x5030, r32 & ~0x20);
79 udelay(1);
80}
81
82static u32 get_XOVER_CLK(u8 rankmap)
83{
84 return rankmap << 24;
85}
86
87static u32 get_XOVER_CMD(u8 rankmap)
88{
89 u32 reg;
90
91 // enable xover cmd
92 reg = 0x4000;
93
94 // enable xover ctl
95 if (rankmap & 0x3)
96 reg |= 0x20000;
97
98 if (rankmap & 0xc)
99 reg |= 0x4000000;
100
101 return reg;
102}
103
104/* CAS write latency. To be programmed in MR2.
105 * See DDR3 SPEC for MR2 documentation. */
106u8 get_CWL(u32 tCK)
107{
108 /* Get CWL based on tCK using the following rule: */
109 switch (tCK) {
110 case TCK_1333MHZ:
111 return 12;
112 case TCK_1200MHZ:
113 case TCK_1100MHZ:
114 return 11;
115 case TCK_1066MHZ:
116 case TCK_1000MHZ:
117 return 10;
118 case TCK_933MHZ:
119 case TCK_900MHZ:
120 return 9;
121 case TCK_800MHZ:
122 case TCK_700MHZ:
123 return 8;
124 case TCK_666MHZ:
125 return 7;
126 case TCK_533MHZ:
127 return 6;
128 default:
129 return 5;
130 }
131}
132
133void dram_find_common_params(ramctr_timing *ctrl)
134{
135 size_t valid_dimms;
136 int channel, slot;
137 dimm_info *dimms = &ctrl->info;
138
139 ctrl->cas_supported = (1 << (MAX_CAS - MIN_CAS + 1)) - 1;
140 valid_dimms = 0;
141 FOR_ALL_CHANNELS for (slot = 0; slot < 2; slot++) {
142 const dimm_attr *dimm = &dimms->dimm[channel][slot];
143 if (dimm->dram_type != SPD_MEMORY_TYPE_SDRAM_DDR3)
144 continue;
145 valid_dimms++;
146
147 /* Find all possible CAS combinations */
148 ctrl->cas_supported &= dimm->cas_supported;
149
150 /* Find the smallest common latencies supported by all DIMMs */
151 ctrl->tCK = MAX(ctrl->tCK, dimm->tCK);
152 ctrl->tAA = MAX(ctrl->tAA, dimm->tAA);
153 ctrl->tWR = MAX(ctrl->tWR, dimm->tWR);
154 ctrl->tRCD = MAX(ctrl->tRCD, dimm->tRCD);
155 ctrl->tRRD = MAX(ctrl->tRRD, dimm->tRRD);
156 ctrl->tRP = MAX(ctrl->tRP, dimm->tRP);
157 ctrl->tRAS = MAX(ctrl->tRAS, dimm->tRAS);
158 ctrl->tRFC = MAX(ctrl->tRFC, dimm->tRFC);
159 ctrl->tWTR = MAX(ctrl->tWTR, dimm->tWTR);
160 ctrl->tRTP = MAX(ctrl->tRTP, dimm->tRTP);
161 ctrl->tFAW = MAX(ctrl->tFAW, dimm->tFAW);
Dan Elkoubydabebc32018-04-13 18:47:10 +0300162 ctrl->tCWL = MAX(ctrl->tCWL, dimm->tCWL);
163 ctrl->tCMD = MAX(ctrl->tCMD, dimm->tCMD);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100164 }
165
166 if (!ctrl->cas_supported)
167 die("Unsupported DIMM combination. "
168 "DIMMS do not support common CAS latency");
169 if (!valid_dimms)
170 die("No valid DIMMs found");
171}
172
173void dram_xover(ramctr_timing * ctrl)
174{
175 u32 reg;
176 int channel;
177
178 FOR_ALL_CHANNELS {
179 // enable xover clk
180 reg = get_XOVER_CLK(ctrl->rankmap[channel]);
181 printram("XOVER CLK [%x] = %x\n", channel * 0x100 + 0xc14,
182 reg);
183 MCHBAR32(channel * 0x100 + 0xc14) = reg;
184
185 // enable xover ctl & xover cmd
186 reg = get_XOVER_CMD(ctrl->rankmap[channel]);
187 printram("XOVER CMD [%x] = %x\n", 0x100 * channel + 0x320c,
188 reg);
189 MCHBAR32(0x100 * channel + 0x320c) = reg;
190 }
191}
192
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100193static void dram_odt_stretch(ramctr_timing *ctrl, int channel)
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100194{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100195 struct cpuid_result cpures;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100196 u32 reg, addr, cpu, stretch;
197
198 stretch = ctrl->ref_card_offset[channel];
199 /* ODT stretch: Delay ODT signal by stretch value.
200 * Useful for multi DIMM setups on the same channel. */
201 cpures = cpuid(1);
202 cpu = cpures.eax;
203 if (IS_SANDY_CPU(cpu) && IS_SANDY_CPU_C(cpu)) {
204 if (stretch == 2)
205 stretch = 3;
206 addr = 0x400 * channel + 0x401c;
207 reg = MCHBAR32(addr) & 0xffffc3ff;
208 reg |= (stretch << 12);
209 reg |= (stretch << 10);
210 MCHBAR32(addr) = reg;
211 printram("OTHP Workaround [%x] = %x\n", addr, reg);
212 } else {
213 // OTHP
214 addr = 0x400 * channel + 0x400c;
215 reg = MCHBAR32(addr) & 0xfff0ffff;
216 reg |= (stretch << 16);
217 reg |= (stretch << 18);
218 MCHBAR32(addr) = reg;
219 printram("OTHP [%x] = %x\n", addr, reg);
220 }
221}
222
223void dram_timing_regs(ramctr_timing *ctrl)
224{
225 u32 reg, addr, val32;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100226 int channel;
227
228 FOR_ALL_CHANNELS {
229 // DBP
230 reg = 0;
231 reg |= ctrl->tRCD;
232 reg |= (ctrl->tRP << 4);
233 reg |= (ctrl->CAS << 8);
234 reg |= (ctrl->CWL << 12);
235 reg |= (ctrl->tRAS << 16);
236 printram("DBP [%x] = %x\n", 0x400 * channel + 0x4000, reg);
237 MCHBAR32(0x400 * channel + 0x4000) = reg;
238
239 // RAP
240 reg = 0;
241 reg |= ctrl->tRRD;
242 reg |= (ctrl->tRTP << 4);
243 reg |= (ctrl->tCKE << 8);
244 reg |= (ctrl->tWTR << 12);
245 reg |= (ctrl->tFAW << 16);
246 reg |= (ctrl->tWR << 24);
247 reg |= (3 << 30);
248 printram("RAP [%x] = %x\n", 0x400 * channel + 0x4004, reg);
249 MCHBAR32(0x400 * channel + 0x4004) = reg;
250
251 // OTHP
252 addr = 0x400 * channel + 0x400c;
253 reg = 0;
254 reg |= ctrl->tXPDLL;
255 reg |= (ctrl->tXP << 5);
256 reg |= (ctrl->tAONPD << 8);
257 reg |= 0xa0000;
258 printram("OTHP [%x] = %x\n", addr, reg);
259 MCHBAR32(addr) = reg;
260
261 MCHBAR32(0x400 * channel + 0x4014) = 0;
262
263 MCHBAR32(addr) |= 0x00020000;
264
Patrick Rudolph19c3dad2016-11-26 11:37:45 +0100265 dram_odt_stretch(ctrl, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100266
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100267 /*
Patrick Rudolphb009ac42018-07-25 15:27:50 +0200268 * TC-Refresh timing parameters
Patrick Rudolph5ee9bc12017-10-31 10:49:52 +0100269 * The tREFIx9 field should be programmed to minimum of
270 * 8.9*tREFI (to allow for possible delays from ZQ or
271 * isoc) and tRASmax (70us) divided by 1024.
272 */
273 val32 = MIN((ctrl->tREFI * 89) / 10, (70000 << 8) / ctrl->tCK);
274
275 reg = ((ctrl->tREFI & 0xffff) << 0) |
276 ((ctrl->tRFC & 0x1ff) << 16) |
277 (((val32 / 1024) & 0x7f) << 25);
278 printram("REFI [%x] = %x\n", 0x400 * channel + 0x4298, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100279 MCHBAR32(0x400 * channel + 0x4298) = reg;
280
281 MCHBAR32(0x400 * channel + 0x4294) |= 0xff;
282
283 // SRFTP
284 reg = 0;
285 val32 = tDLLK;
286 reg = (reg & ~0xfff) | val32;
287 val32 = ctrl->tXSOffset;
288 reg = (reg & ~0xf000) | (val32 << 12);
289 val32 = tDLLK - ctrl->tXSOffset;
290 reg = (reg & ~0x3ff0000) | (val32 << 16);
291 val32 = ctrl->tMOD - 8;
292 reg = (reg & ~0xf0000000) | (val32 << 28);
293 printram("SRFTP [%x] = %x\n", 0x400 * channel + 0x42a4,
294 reg);
295 MCHBAR32(0x400 * channel + 0x42a4) = reg;
296 }
297}
298
299void dram_dimm_mapping(ramctr_timing *ctrl)
300{
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100301 int channel;
302 dimm_info *info = &ctrl->info;
303
304 FOR_ALL_CHANNELS {
Nico Huberac4f2162017-10-01 18:14:43 +0200305 dimm_attr *dimmA, *dimmB;
306 u32 reg = 0;
307
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100308 if (info->dimm[channel][0].size_mb >=
309 info->dimm[channel][1].size_mb) {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100310 dimmA = &info->dimm[channel][0];
311 dimmB = &info->dimm[channel][1];
Nico Huberac4f2162017-10-01 18:14:43 +0200312 reg |= 0 << 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100313 } else {
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100314 dimmA = &info->dimm[channel][1];
315 dimmB = &info->dimm[channel][0];
Nico Huberac4f2162017-10-01 18:14:43 +0200316 reg |= 1 << 16;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100317 }
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100318
Nico Huberac4f2162017-10-01 18:14:43 +0200319 if (dimmA && (dimmA->ranks > 0)) {
320 reg |= dimmA->size_mb / 256;
321 reg |= (dimmA->ranks - 1) << 17;
322 reg |= (dimmA->width / 8 - 1) << 19;
323 }
324
325 if (dimmB && (dimmB->ranks > 0)) {
326 reg |= (dimmB->size_mb / 256) << 8;
327 reg |= (dimmB->ranks - 1) << 18;
328 reg |= (dimmB->width / 8 - 1) << 20;
329 }
330
331 reg |= 1 << 21; /* rank interleave */
332 reg |= 1 << 22; /* enhanced interleave */
333
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100334 if ((dimmA && (dimmA->ranks > 0))
335 || (dimmB && (dimmB->ranks > 0))) {
336 ctrl->mad_dimm[channel] = reg;
337 } else {
338 ctrl->mad_dimm[channel] = 0;
339 }
340 }
341}
342
343void dram_dimm_set_mapping(ramctr_timing * ctrl)
344{
345 int channel;
346 FOR_ALL_CHANNELS {
347 MCHBAR32(0x5004 + channel * 4) = ctrl->mad_dimm[channel];
348 }
349}
350
351void dram_zones(ramctr_timing * ctrl, int training)
352{
353 u32 reg, ch0size, ch1size;
354 u8 val;
355 reg = 0;
356 val = 0;
357 if (training) {
358 ch0size = ctrl->channel_size_mb[0] ? 256 : 0;
359 ch1size = ctrl->channel_size_mb[1] ? 256 : 0;
360 } else {
361 ch0size = ctrl->channel_size_mb[0];
362 ch1size = ctrl->channel_size_mb[1];
363 }
364
365 if (ch0size >= ch1size) {
366 reg = MCHBAR32(0x5014);
367 val = ch1size / 256;
368 reg = (reg & ~0xff000000) | val << 24;
369 reg = (reg & ~0xff0000) | (2 * val) << 16;
370 MCHBAR32(0x5014) = reg;
371 MCHBAR32(0x5000) = 0x24;
372 } else {
373 reg = MCHBAR32(0x5014);
374 val = ch0size / 256;
375 reg = (reg & ~0xff000000) | val << 24;
376 reg = (reg & ~0xff0000) | (2 * val) << 16;
377 MCHBAR32(0x5014) = reg;
378 MCHBAR32(0x5000) = 0x21;
379 }
380}
381
382#define HOST_BRIDGE PCI_DEVFN(0, 0)
383#define DEFAULT_TCK TCK_800MHZ
384
385unsigned int get_mem_min_tck(void)
386{
387 u32 reg32;
388 u8 rev;
389 const struct device *dev;
390 const struct northbridge_intel_sandybridge_config *cfg = NULL;
391
392 dev = dev_find_slot(0, HOST_BRIDGE);
393 if (dev)
394 cfg = dev->chip_info;
395
396 /* If this is zero, it just means devicetree.cb didn't set it */
397 if (!cfg || cfg->max_mem_clock_mhz == 0) {
Patrick Rudolphb794a692017-08-08 13:13:51 +0200398 if (IS_ENABLED(CONFIG_NATIVE_RAMINIT_IGNORE_MAX_MEM_FUSES))
399 return TCK_1333MHZ;
400
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100401 rev = pci_read_config8(PCI_DEV(0, 0, 0), PCI_DEVICE_ID);
402
403 if ((rev & BASE_REV_MASK) == BASE_REV_SNB) {
404 /* read Capabilities A Register DMFC bits */
405 reg32 = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_A);
406 reg32 &= 0x7;
407
408 switch (reg32) {
409 case 7: return TCK_533MHZ;
410 case 6: return TCK_666MHZ;
411 case 5: return TCK_800MHZ;
412 /* reserved: */
413 default:
414 break;
415 }
416 } else {
417 /* read Capabilities B Register DMFC bits */
418 reg32 = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_B);
419 reg32 = (reg32 >> 4) & 0x7;
420
421 switch (reg32) {
422 case 7: return TCK_533MHZ;
423 case 6: return TCK_666MHZ;
424 case 5: return TCK_800MHZ;
425 case 4: return TCK_933MHZ;
426 case 3: return TCK_1066MHZ;
427 case 2: return TCK_1200MHZ;
428 case 1: return TCK_1333MHZ;
429 /* reserved: */
430 default:
431 break;
432 }
433 }
434 return DEFAULT_TCK;
435 } else {
436 if (cfg->max_mem_clock_mhz >= 1066)
437 return TCK_1066MHZ;
438 else if (cfg->max_mem_clock_mhz >= 933)
439 return TCK_933MHZ;
440 else if (cfg->max_mem_clock_mhz >= 800)
441 return TCK_800MHZ;
442 else if (cfg->max_mem_clock_mhz >= 666)
443 return TCK_666MHZ;
444 else if (cfg->max_mem_clock_mhz >= 533)
445 return TCK_533MHZ;
446 else
447 return TCK_400MHZ;
448 }
449}
450
451#define DEFAULT_PCI_MMIO_SIZE 2048
452
453static unsigned int get_mmio_size(void)
454{
455 const struct device *dev;
456 const struct northbridge_intel_sandybridge_config *cfg = NULL;
457
458 dev = dev_find_slot(0, HOST_BRIDGE);
459 if (dev)
460 cfg = dev->chip_info;
461
462 /* If this is zero, it just means devicetree.cb didn't set it */
463 if (!cfg || cfg->pci_mmio_size == 0)
464 return DEFAULT_PCI_MMIO_SIZE;
465 else
466 return cfg->pci_mmio_size;
467}
468
469void dram_memorymap(ramctr_timing * ctrl, int me_uma_size)
470{
471 u32 reg, val, reclaim;
472 u32 tom, gfxstolen, gttsize;
473 size_t tsegsize, mmiosize, toludbase, touudbase, gfxstolenbase, gttbase,
474 tsegbase, mestolenbase;
475 size_t tsegbasedelta, remapbase, remaplimit;
476 uint16_t ggc;
477
478 mmiosize = get_mmio_size();
479
480 ggc = pci_read_config16(NORTHBRIDGE, GGC);
481 if (!(ggc & 2)) {
482 gfxstolen = ((ggc >> 3) & 0x1f) * 32;
483 gttsize = ((ggc >> 8) & 0x3);
484 } else {
485 gfxstolen = 0;
486 gttsize = 0;
487 }
488
489 tsegsize = CONFIG_SMM_TSEG_SIZE >> 20;
490
491 tom = ctrl->channel_size_mb[0] + ctrl->channel_size_mb[1];
492
493 mestolenbase = tom - me_uma_size;
494
495 toludbase = MIN(4096 - mmiosize + gfxstolen + gttsize + tsegsize,
496 tom - me_uma_size);
497 gfxstolenbase = toludbase - gfxstolen;
498 gttbase = gfxstolenbase - gttsize;
499
500 tsegbase = gttbase - tsegsize;
501
502 // Round tsegbase down to nearest address aligned to tsegsize
503 tsegbasedelta = tsegbase & (tsegsize - 1);
504 tsegbase &= ~(tsegsize - 1);
505
506 gttbase -= tsegbasedelta;
507 gfxstolenbase -= tsegbasedelta;
508 toludbase -= tsegbasedelta;
509
510 // Test if it is possible to reclaim a hole in the RAM addressing
511 if (tom - me_uma_size > toludbase) {
512 // Reclaim is possible
513 reclaim = 1;
514 remapbase = MAX(4096, tom - me_uma_size);
515 remaplimit =
516 remapbase + MIN(4096, tom - me_uma_size) - toludbase - 1;
517 touudbase = remaplimit + 1;
518 } else {
519 // Reclaim not possible
520 reclaim = 0;
521 touudbase = tom - me_uma_size;
522 }
523
524 // Update memory map in pci-e configuration space
525 printk(BIOS_DEBUG, "Update PCI-E configuration space:\n");
526
527 // TOM (top of memory)
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300528 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0xa0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100529 val = tom & 0xfff;
530 reg = (reg & ~0xfff00000) | (val << 20);
531 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa0, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300532 pci_write_config32(PCI_DEV(0, 0, 0), 0xa0, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100533
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300534 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0xa4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100535 val = tom & 0xfffff000;
536 reg = (reg & ~0x000fffff) | (val >> 12);
537 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa4, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300538 pci_write_config32(PCI_DEV(0, 0, 0), 0xa4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100539
540 // TOLUD (top of low used dram)
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300541 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0xbc);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100542 val = toludbase & 0xfff;
543 reg = (reg & ~0xfff00000) | (val << 20);
544 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xbc, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300545 pci_write_config32(PCI_DEV(0, 0, 0), 0xbc, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100546
547 // TOUUD LSB (top of upper usable dram)
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300548 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0xa8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100549 val = touudbase & 0xfff;
550 reg = (reg & ~0xfff00000) | (val << 20);
551 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xa8, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300552 pci_write_config32(PCI_DEV(0, 0, 0), 0xa8, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100553
554 // TOUUD MSB
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300555 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0xac);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100556 val = touudbase & 0xfffff000;
557 reg = (reg & ~0x000fffff) | (val >> 12);
558 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xac, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300559 pci_write_config32(PCI_DEV(0, 0, 0), 0xac, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100560
561 if (reclaim) {
562 // REMAP BASE
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300563 pci_write_config32(PCI_DEV(0, 0, 0), 0x90, remapbase << 20);
564 pci_write_config32(PCI_DEV(0, 0, 0), 0x94, remapbase >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100565
566 // REMAP LIMIT
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300567 pci_write_config32(PCI_DEV(0, 0, 0), 0x98, remaplimit << 20);
568 pci_write_config32(PCI_DEV(0, 0, 0), 0x9c, remaplimit >> 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100569 }
570 // TSEG
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300571 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0xb8);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100572 val = tsegbase & 0xfff;
573 reg = (reg & ~0xfff00000) | (val << 20);
574 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb8, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300575 pci_write_config32(PCI_DEV(0, 0, 0), 0xb8, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100576
577 // GFX stolen memory
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300578 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0xb0);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100579 val = gfxstolenbase & 0xfff;
580 reg = (reg & ~0xfff00000) | (val << 20);
581 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb0, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300582 pci_write_config32(PCI_DEV(0, 0, 0), 0xb0, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100583
584 // GTT stolen memory
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300585 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0xb4);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100586 val = gttbase & 0xfff;
587 reg = (reg & ~0xfff00000) | (val << 20);
588 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0xb4, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300589 pci_write_config32(PCI_DEV(0, 0, 0), 0xb4, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100590
591 if (me_uma_size) {
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300592 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0x7c);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100593 val = (0x80000 - me_uma_size) & 0xfffff000;
594 reg = (reg & ~0x000fffff) | (val >> 12);
595 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x7c, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300596 pci_write_config32(PCI_DEV(0, 0, 0), 0x7c, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100597
598 // ME base
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300599 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0x70);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100600 val = mestolenbase & 0xfff;
601 reg = (reg & ~0xfff00000) | (val << 20);
602 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x70, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300603 pci_write_config32(PCI_DEV(0, 0, 0), 0x70, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100604
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300605 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0x74);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100606 val = mestolenbase & 0xfffff000;
607 reg = (reg & ~0x000fffff) | (val >> 12);
608 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x74, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300609 pci_write_config32(PCI_DEV(0, 0, 0), 0x74, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100610
611 // ME mask
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300612 reg = pci_read_config32(PCI_DEV(0, 0, 0), 0x78);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100613 val = (0x80000 - me_uma_size) & 0xfff;
614 reg = (reg & ~0xfff00000) | (val << 20);
615 reg = (reg & ~0x400) | (1 << 10); // set lockbit on ME mem
616
617 reg = (reg & ~0x800) | (1 << 11); // set ME memory enable
618 printk(BIOS_DEBUG, "PCI(0, 0, 0)[%x] = %x\n", 0x78, reg);
Kyösti Mälkkid45114f2013-07-26 08:53:59 +0300619 pci_write_config32(PCI_DEV(0, 0, 0), 0x78, reg);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100620 }
621}
622
623static void wait_428c(int channel)
624{
625 while (1) {
626 if (read32(DEFAULT_MCHBAR + 0x428c + (channel << 10)) & 0x50)
627 return;
628 }
629}
630
631static void write_reset(ramctr_timing * ctrl)
632{
633 int channel, slotrank;
634
635 /* choose a populated channel. */
636 channel = (ctrl->rankmap[0]) ? 0 : 1;
637
638 wait_428c(channel);
639
640 /* choose a populated rank. */
641 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
642
643 /* DRAM command ZQCS */
644 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
645 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x80c01);
646
647 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
648 (slotrank << 24) | 0x60000);
649
650 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
651
652 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x400001);
653 wait_428c(channel);
654}
655
656void dram_jedecreset(ramctr_timing * ctrl)
657{
658 u32 reg, addr;
659 int channel;
660
661 while (!(MCHBAR32(0x5084) & 0x10000));
662 do {
663 reg = MCHBAR32(0x428c);
664 } while ((reg & 0x14) == 0);
665
666 // Set state of memory controller
667 reg = 0x112;
668 MCHBAR32(0x5030) = reg;
669 MCHBAR32(0x4ea0) = 0;
670 reg |= 2; //ddr reset
671 MCHBAR32(0x5030) = reg;
672
673 // Assert dimm reset signal
674 reg = MCHBAR32(0x5030);
675 reg &= ~0x2;
676 MCHBAR32(0x5030) = reg;
677
678 // Wait 200us
679 udelay(200);
680
681 // Deassert dimm reset signal
682 MCHBAR32(0x5030) |= 2;
683
684 // Wait 500us
685 udelay(500);
686
687 // Enable DCLK
688 MCHBAR32(0x5030) |= 4;
689
690 // XXX Wait 20ns
691 udelay(1);
692
693 FOR_ALL_CHANNELS {
694 // Set valid rank CKE
695 reg = 0;
696 reg = (reg & ~0xf) | ctrl->rankmap[channel];
697 addr = 0x400 * channel + 0x42a0;
698 MCHBAR32(addr) = reg;
699
700 // Wait 10ns for ranks to settle
701 //udelay(0.01);
702
703 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
704 MCHBAR32(addr) = reg;
705
706 // Write reset using a NOP
707 write_reset(ctrl);
708 }
709}
710
711static odtmap get_ODT(ramctr_timing *ctrl, u8 rank, int channel)
712{
713 /* Get ODT based on rankmap: */
714 int dimms_per_ch = (ctrl->rankmap[channel] & 1)
715 + ((ctrl->rankmap[channel] >> 2) & 1);
716
717 if (dimms_per_ch == 1) {
718 return (const odtmap){60, 60};
719 } else {
720 return (const odtmap){120, 30};
721 }
722}
723
724static void write_mrreg(ramctr_timing *ctrl, int channel, int slotrank,
725 int reg, u32 val)
726{
727 wait_428c(channel);
728
729 if (ctrl->rank_mirror[channel][slotrank]) {
730 /* DDR3 Rank1 Address mirror
731 * swap the following pins:
732 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
733 reg = ((reg >> 1) & 1) | ((reg << 1) & 2);
734 val = (val & ~0x1f8) | ((val >> 1) & 0xa8)
735 | ((val & 0xa8) << 1);
736 }
737
738 /* DRAM command MRS */
739 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f000);
740 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
741 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
742 (slotrank << 24) | (reg << 20) | val | 0x60000);
743 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
744
745 /* DRAM command MRS */
746 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f000);
747 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x41001);
748 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
749 (slotrank << 24) | (reg << 20) | val | 0x60000);
750 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
751
752 /* DRAM command MRS */
753 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x0f000);
754 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
755 0x1001 | (ctrl->tMOD << 16));
756 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
757 (slotrank << 24) | (reg << 20) | val | 0x60000);
758 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
759 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
760}
761
762static u32 make_mr0(ramctr_timing * ctrl, u8 rank)
763{
764 u16 mr0reg, mch_cas, mch_wr;
765 static const u8 mch_wr_t[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
766
767 /* DLL Reset - self clearing - set after CLK frequency has been changed */
768 mr0reg = 0x100;
769
770 // Convert CAS to MCH register friendly
771 if (ctrl->CAS < 12) {
772 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
773 } else {
774 mch_cas = (u16) (ctrl->CAS - 12);
775 mch_cas = ((mch_cas << 1) | 0x1);
776 }
777
778 // Convert tWR to MCH register friendly
779 mch_wr = mch_wr_t[ctrl->tWR - 5];
780
781 mr0reg = (mr0reg & ~0x4) | ((mch_cas & 0x1) << 2);
782 mr0reg = (mr0reg & ~0x70) | ((mch_cas & 0xe) << 3);
783 mr0reg = (mr0reg & ~0xe00) | (mch_wr << 9);
784
785 // Precharge PD - Fast (desktop) 0x1 or slow (mobile) 0x0 - mostly power-saving feature
786 mr0reg = (mr0reg & ~0x1000) | (!ctrl->mobile << 12);
787 return mr0reg;
788}
789
790static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
791{
792 write_mrreg(ctrl, channel, rank, 0,
793 make_mr0(ctrl, rank));
794}
795
796static u32 encode_odt(u32 odt)
797{
798 switch (odt) {
799 case 30:
800 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
801 case 60:
802 return (1 << 2); // RZQ/4
803 case 120:
804 return (1 << 6); // RZQ/2
805 default:
806 case 0:
807 return 0;
808 }
809}
810
811static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
812{
813 odtmap odt;
814 u32 mr1reg;
815
816 odt = get_ODT(ctrl, rank, channel);
817 mr1reg = 0x2;
818
819 mr1reg |= encode_odt(odt.rttnom);
820
821 return mr1reg;
822}
823
824static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
825{
826 u16 mr1reg;
827
828 mr1reg = make_mr1(ctrl, rank, channel);
829
830 write_mrreg(ctrl, channel, rank, 1, mr1reg);
831}
832
833static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
834{
835 u16 pasr, cwl, mr2reg;
836 odtmap odt;
837 int srt;
838
839 pasr = 0;
840 cwl = ctrl->CWL - 5;
841 odt = get_ODT(ctrl, rank, channel);
842
843 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
844
845 mr2reg = 0;
846 mr2reg = (mr2reg & ~0x7) | pasr;
847 mr2reg = (mr2reg & ~0x38) | (cwl << 3);
848 mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6);
849 mr2reg = (mr2reg & ~0x80) | (srt << 7);
850 mr2reg |= (odt.rttwr / 60) << 9;
851
852 write_mrreg(ctrl, channel, rank, 2, mr2reg);
853}
854
855static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
856{
857 write_mrreg(ctrl, channel, rank, 3, 0);
858}
859
860void dram_mrscommands(ramctr_timing * ctrl)
861{
862 u8 slotrank;
863 u32 reg, addr;
864 int channel;
865
866 FOR_ALL_POPULATED_CHANNELS {
867 FOR_ALL_POPULATED_RANKS {
868 // MR2
869 dram_mr2(ctrl, slotrank, channel);
870
871 // MR3
872 dram_mr3(ctrl, slotrank, channel);
873
874 // MR1
875 dram_mr1(ctrl, slotrank, channel);
876
877 // MR0
878 dram_mr0(ctrl, slotrank, channel);
879 }
880 }
881
882 /* DRAM command NOP */
883 write32(DEFAULT_MCHBAR + 0x4e20, 0x7);
884 write32(DEFAULT_MCHBAR + 0x4e30, 0xf1001);
885 write32(DEFAULT_MCHBAR + 0x4e00, 0x60002);
886 write32(DEFAULT_MCHBAR + 0x4e10, 0);
887
888 /* DRAM command ZQCL */
889 write32(DEFAULT_MCHBAR + 0x4e24, 0x1f003);
890 write32(DEFAULT_MCHBAR + 0x4e34, 0x1901001);
891 write32(DEFAULT_MCHBAR + 0x4e04, 0x60400);
892 write32(DEFAULT_MCHBAR + 0x4e14, 0x288);
893
894 /* execute command queue on all channels ? */
895 write32(DEFAULT_MCHBAR + 0x4e84, 0x40004);
896
897 // Drain
898 FOR_ALL_CHANNELS {
899 // Wait for ref drained
900 wait_428c(channel);
901 }
902
903 // Refresh enable
904 MCHBAR32(0x5030) |= 8;
905
906 FOR_ALL_POPULATED_CHANNELS {
907 addr = 0x400 * channel + 0x4020;
908 reg = MCHBAR32(addr);
909 reg &= ~0x200000;
910 MCHBAR32(addr) = reg;
911
912 wait_428c(channel);
913
914 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
915
916 // Drain
917 wait_428c(channel);
918
919 /* DRAM command ZQCS */
920 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
921 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
922 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
923 (slotrank << 24) | 0x60000);
924 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
925 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x1);
926
927 // Drain
928 wait_428c(channel);
929 }
930}
931
932static const u32 lane_registers[] = {
933 0x0000, 0x0200, 0x0400, 0x0600,
934 0x1000, 0x1200, 0x1400, 0x1600,
935 0x0800
936};
937
938void program_timings(ramctr_timing * ctrl, int channel)
939{
940 u32 reg32, reg_4024, reg_c14, reg_c18, reg_4028;
941 int lane;
942 int slotrank, slot;
943 int full_shift = 0;
944 u16 slot320c[NUM_SLOTS];
945
946 FOR_ALL_POPULATED_RANKS {
947 if (full_shift < -ctrl->timings[channel][slotrank].val_320c)
948 full_shift = -ctrl->timings[channel][slotrank].val_320c;
949 }
950
951 for (slot = 0; slot < NUM_SLOTS; slot++)
952 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
953 case 0:
954 default:
955 slot320c[slot] = 0x7f;
956 break;
957 case 1:
958 slot320c[slot] =
959 ctrl->timings[channel][2 * slot + 0].val_320c +
960 full_shift;
961 break;
962 case 2:
963 slot320c[slot] =
964 ctrl->timings[channel][2 * slot + 1].val_320c +
965 full_shift;
966 break;
967 case 3:
968 slot320c[slot] =
969 (ctrl->timings[channel][2 * slot].val_320c +
970 ctrl->timings[channel][2 * slot +
971 1].val_320c) / 2 +
972 full_shift;
973 break;
974 }
975
976 /* enable CMD XOVER */
977 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
978 reg32 |= ((slot320c[0] & 0x3f) << 6) | ((slot320c[0] & 0x40) << 9);
979 reg32 |= (slot320c[1] & 0x7f) << 18;
980 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
981
982 MCHBAR32(0x320c + 0x100 * channel) = reg32;
983
984 /* enable CLK XOVER */
985 reg_c14 = get_XOVER_CLK(ctrl->rankmap[channel]);
986 reg_c18 = 0;
987
988 FOR_ALL_POPULATED_RANKS {
989 int shift =
990 ctrl->timings[channel][slotrank].val_320c + full_shift;
991 int offset_val_c14;
992 if (shift < 0)
993 shift = 0;
994 offset_val_c14 = ctrl->reg_c14_offset + shift;
995 /* set CLK phase shift */
996 reg_c14 |= (offset_val_c14 & 0x3f) << (6 * slotrank);
997 reg_c18 |= ((offset_val_c14 >> 6) & 1) << slotrank;
998 }
999
1000 MCHBAR32(0xc14 + channel * 0x100) = reg_c14;
1001 MCHBAR32(0xc18 + channel * 0x100) = reg_c18;
1002
1003 reg_4028 = MCHBAR32(0x4028 + 0x400 * channel);
1004 reg_4028 &= 0xffff0000;
1005
1006 reg_4024 = 0;
1007
1008 FOR_ALL_POPULATED_RANKS {
1009 int post_timA_min_high = 7, post_timA_max_high = 0;
1010 int pre_timA_min_high = 7, pre_timA_max_high = 0;
1011 int shift_402x = 0;
1012 int shift =
1013 ctrl->timings[channel][slotrank].val_320c + full_shift;
1014
1015 if (shift < 0)
1016 shift = 0;
1017
1018 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +02001019 post_timA_min_high = MIN(post_timA_min_high,
1020 (ctrl->timings[channel][slotrank].lanes[lane].
1021 timA + shift) >> 6);
1022 pre_timA_min_high = MIN(pre_timA_min_high,
1023 ctrl->timings[channel][slotrank].lanes[lane].
1024 timA >> 6);
1025 post_timA_max_high = MAX(post_timA_max_high,
1026 (ctrl->timings[channel][slotrank].lanes[lane].
1027 timA + shift) >> 6);
1028 pre_timA_max_high = MAX(pre_timA_max_high,
1029 ctrl->timings[channel][slotrank].lanes[lane].
1030 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001031 }
1032
1033 if (pre_timA_max_high - pre_timA_min_high <
1034 post_timA_max_high - post_timA_min_high)
1035 shift_402x = +1;
1036 else if (pre_timA_max_high - pre_timA_min_high >
1037 post_timA_max_high - post_timA_min_high)
1038 shift_402x = -1;
1039
1040 reg_4028 |=
1041 (ctrl->timings[channel][slotrank].val_4028 + shift_402x -
1042 post_timA_min_high) << (4 * slotrank);
1043 reg_4024 |=
1044 (ctrl->timings[channel][slotrank].val_4024 +
1045 shift_402x) << (8 * slotrank);
1046
1047 FOR_ALL_LANES {
1048 MCHBAR32(lane_registers[lane] + 0x10 + 0x100 * channel +
1049 4 * slotrank)
1050 =
1051 (((ctrl->timings[channel][slotrank].lanes[lane].
1052 timA + shift) & 0x3f)
1053 |
1054 ((ctrl->timings[channel][slotrank].lanes[lane].
1055 rising + shift) << 8)
1056 |
1057 (((ctrl->timings[channel][slotrank].lanes[lane].
1058 timA + shift -
1059 (post_timA_min_high << 6)) & 0x1c0) << 10)
1060 | ((ctrl->timings[channel][slotrank].lanes[lane].
1061 falling + shift) << 20));
1062
1063 MCHBAR32(lane_registers[lane] + 0x20 + 0x100 * channel +
1064 4 * slotrank)
1065 =
1066 (((ctrl->timings[channel][slotrank].lanes[lane].
1067 timC + shift) & 0x3f)
1068 |
1069 (((ctrl->timings[channel][slotrank].lanes[lane].
1070 timB + shift) & 0x3f) << 8)
1071 |
1072 (((ctrl->timings[channel][slotrank].lanes[lane].
1073 timB + shift) & 0x1c0) << 9)
1074 |
1075 (((ctrl->timings[channel][slotrank].lanes[lane].
1076 timC + shift) & 0x40) << 13));
1077 }
1078 }
1079 MCHBAR32(0x4024 + 0x400 * channel) = reg_4024;
1080 MCHBAR32(0x4028 + 0x400 * channel) = reg_4028;
1081}
1082
1083static void test_timA(ramctr_timing * ctrl, int channel, int slotrank)
1084{
1085 wait_428c(channel);
1086
1087 /* DRAM command MRS
1088 * write MR3 MPR enable
1089 * in this mode only RD and RDA are allowed
1090 * all reads return a predefined pattern */
1091 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
1092 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1093 (0xc01 | (ctrl->tMOD << 16)));
1094 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1095 (slotrank << 24) | 0x360004);
1096 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1097
1098 /* DRAM command RD */
1099 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
1100 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x4040c01);
1101 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, (slotrank << 24));
1102 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1103
1104 /* DRAM command RD */
1105 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
1106 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1107 0x100f | ((ctrl->CAS + 36) << 16));
1108 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1109 (slotrank << 24) | 0x60000);
1110 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1111
1112 /* DRAM command MRS
1113 * write MR3 MPR disable */
1114 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
1115 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1116 (0xc01 | (ctrl->tMOD << 16)));
1117 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1118 (slotrank << 24) | 0x360000);
1119 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1120
1121 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1122
1123 wait_428c(channel);
1124}
1125
1126static int does_lane_work(ramctr_timing * ctrl, int channel, int slotrank,
1127 int lane)
1128{
1129 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
1130 return ((read32
1131 (DEFAULT_MCHBAR + lane_registers[lane] + channel * 0x100 + 4 +
1132 ((timA / 32) & 1) * 4)
1133 >> (timA % 32)) & 1);
1134}
1135
1136struct run {
1137 int middle;
1138 int end;
1139 int start;
1140 int all;
1141 int length;
1142};
1143
1144static struct run get_longest_zero_run(int *seq, int sz)
1145{
1146 int i, ls;
1147 int bl = 0, bs = 0;
1148 struct run ret;
1149
1150 ls = 0;
1151 for (i = 0; i < 2 * sz; i++)
1152 if (seq[i % sz]) {
1153 if (i - ls > bl) {
1154 bl = i - ls;
1155 bs = ls;
1156 }
1157 ls = i + 1;
1158 }
1159 if (bl == 0) {
1160 ret.middle = sz / 2;
1161 ret.start = 0;
1162 ret.end = sz;
1163 ret.all = 1;
1164 return ret;
1165 }
1166
1167 ret.start = bs % sz;
1168 ret.end = (bs + bl - 1) % sz;
1169 ret.middle = (bs + (bl - 1) / 2) % sz;
1170 ret.length = bl;
1171 ret.all = 0;
1172
1173 return ret;
1174}
1175
1176static void discover_timA_coarse(ramctr_timing * ctrl, int channel,
1177 int slotrank, int *upperA)
1178{
1179 int timA;
1180 int statistics[NUM_LANES][128];
1181 int lane;
1182
1183 for (timA = 0; timA < 128; timA++) {
1184 FOR_ALL_LANES {
1185 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1186 }
1187 program_timings(ctrl, channel);
1188
1189 test_timA(ctrl, channel, slotrank);
1190
1191 FOR_ALL_LANES {
1192 statistics[lane][timA] =
1193 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001194 }
1195 }
1196 FOR_ALL_LANES {
1197 struct run rn = get_longest_zero_run(statistics[lane], 128);
1198 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1199 upperA[lane] = rn.end;
1200 if (upperA[lane] < rn.middle)
1201 upperA[lane] += 128;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001202 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1203 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001204 }
1205}
1206
1207static void discover_timA_fine(ramctr_timing * ctrl, int channel, int slotrank,
1208 int *upperA)
1209{
1210 int timA_delta;
1211 int statistics[NUM_LANES][51];
1212 int lane, i;
1213
1214 memset(statistics, 0, sizeof(statistics));
1215
1216 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
1217 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
1218 timA = upperA[lane] + timA_delta + 0x40;
1219 program_timings(ctrl, channel);
1220
1221 for (i = 0; i < 100; i++) {
1222 test_timA(ctrl, channel, slotrank);
1223 FOR_ALL_LANES {
1224 statistics[lane][timA_delta + 25] +=
1225 does_lane_work(ctrl, channel, slotrank,
1226 lane);
1227 }
1228 }
1229 }
1230 FOR_ALL_LANES {
1231 int last_zero, first_all;
1232
1233 for (last_zero = -25; last_zero <= 25; last_zero++)
1234 if (statistics[lane][last_zero + 25])
1235 break;
1236 last_zero--;
1237 for (first_all = -25; first_all <= 25; first_all++)
1238 if (statistics[lane][first_all + 25] == 100)
1239 break;
1240
1241 printram("lane %d: %d, %d\n", lane, last_zero,
1242 first_all);
1243
1244 ctrl->timings[channel][slotrank].lanes[lane].timA =
1245 (last_zero + first_all) / 2 + upperA[lane];
1246 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
1247 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
1248 }
1249}
1250
1251static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank,
1252 int *upperA)
1253{
1254 int works[NUM_LANES];
1255 int lane;
1256 while (1) {
1257 int all_works = 1, some_works = 0;
1258 program_timings(ctrl, channel);
1259 test_timA(ctrl, channel, slotrank);
1260 FOR_ALL_LANES {
1261 works[lane] =
1262 !does_lane_work(ctrl, channel, slotrank, lane);
1263 if (works[lane])
1264 some_works = 1;
1265 else
1266 all_works = 0;
1267 }
1268 if (all_works)
1269 return 0;
1270 if (!some_works) {
1271 if (ctrl->timings[channel][slotrank].val_4024 < 2) {
1272 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1273 channel, slotrank);
1274 return MAKE_ERR;
1275 }
1276 ctrl->timings[channel][slotrank].val_4024 -= 2;
1277 printram("4024 -= 2;\n");
1278 continue;
1279 }
1280 ctrl->timings[channel][slotrank].val_4028 += 2;
1281 printram("4028 += 2;\n");
1282 if (ctrl->timings[channel][slotrank].val_4028 >= 0x10) {
1283 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1284 channel, slotrank);
1285 return MAKE_ERR;
1286 }
1287 FOR_ALL_LANES if (works[lane]) {
1288 ctrl->timings[channel][slotrank].lanes[lane].timA +=
1289 128;
1290 upperA[lane] += 128;
1291 printram("increment %d, %d, %d\n", channel,
1292 slotrank, lane);
1293 }
1294 }
1295 return 0;
1296}
1297
1298struct timA_minmax {
1299 int timA_min_high, timA_max_high;
1300};
1301
1302static void pre_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
1303 struct timA_minmax *mnmx)
1304{
1305 int lane;
1306 mnmx->timA_min_high = 7;
1307 mnmx->timA_max_high = 0;
1308
1309 FOR_ALL_LANES {
1310 if (mnmx->timA_min_high >
1311 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1312 mnmx->timA_min_high =
1313 (ctrl->timings[channel][slotrank].lanes[lane].
1314 timA >> 6);
1315 if (mnmx->timA_max_high <
1316 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1317 mnmx->timA_max_high =
1318 (ctrl->timings[channel][slotrank].lanes[lane].
1319 timA >> 6);
1320 }
1321}
1322
1323static void post_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
1324 struct timA_minmax *mnmx)
1325{
1326 struct timA_minmax post;
1327 int shift_402x = 0;
1328
1329 /* Get changed maxima. */
1330 pre_timA_change(ctrl, channel, slotrank, &post);
1331
1332 if (mnmx->timA_max_high - mnmx->timA_min_high <
1333 post.timA_max_high - post.timA_min_high)
1334 shift_402x = +1;
1335 else if (mnmx->timA_max_high - mnmx->timA_min_high >
1336 post.timA_max_high - post.timA_min_high)
1337 shift_402x = -1;
1338 else
1339 shift_402x = 0;
1340
1341 ctrl->timings[channel][slotrank].val_4028 += shift_402x;
1342 ctrl->timings[channel][slotrank].val_4024 += shift_402x;
1343 printram("4024 += %d;\n", shift_402x);
1344 printram("4028 += %d;\n", shift_402x);
1345}
1346
1347/* Compensate the skew between DQS and DQs.
1348 * To ease PCB design a small skew between Data Strobe signals and
1349 * Data Signals is allowed.
1350 * The controller has to measure and compensate this skew for every byte-lane.
1351 * By delaying either all DQs signals or DQS signal, a full phase
1352 * shift can be introduced.
1353 * It is assumed that one byte-lane's DQs signals have the same routing delay.
1354 *
1355 * To measure the actual skew, the DRAM is placed in "read leveling" mode.
1356 * In read leveling mode the DRAM-chip outputs an alternating periodic pattern.
1357 * The memory controller iterates over all possible values to do a full phase shift
1358 * and issues read commands.
1359 * With DQS and DQs in phase the data read is expected to alternate on every byte:
1360 * 0xFF 0x00 0xFF ...
1361 * Once the controller has detected this pattern a bit in the result register is
1362 * set for the current phase shift.
1363 */
1364int read_training(ramctr_timing * ctrl)
1365{
1366 int channel, slotrank, lane;
1367 int err;
1368
1369 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1370 int all_high, some_high;
1371 int upperA[NUM_LANES];
1372 struct timA_minmax mnmx;
1373
1374 wait_428c(channel);
1375
1376 /* DRAM command PREA */
1377 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1378 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1379 0xc01 | (ctrl->tRP << 16));
1380 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1381 (slotrank << 24) | 0x60400);
1382 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1383 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
1384
1385 write32(DEFAULT_MCHBAR + 0x3400, (slotrank << 2) | 0x8001);
1386
1387 ctrl->timings[channel][slotrank].val_4028 = 4;
1388 ctrl->timings[channel][slotrank].val_4024 = 55;
1389 program_timings(ctrl, channel);
1390
1391 discover_timA_coarse(ctrl, channel, slotrank, upperA);
1392
1393 all_high = 1;
1394 some_high = 0;
1395 FOR_ALL_LANES {
1396 if (ctrl->timings[channel][slotrank].lanes[lane].
1397 timA >= 0x40)
1398 some_high = 1;
1399 else
1400 all_high = 0;
1401 }
1402
1403 if (all_high) {
1404 ctrl->timings[channel][slotrank].val_4028--;
1405 printram("4028--;\n");
1406 FOR_ALL_LANES {
1407 ctrl->timings[channel][slotrank].lanes[lane].
1408 timA -= 0x40;
1409 upperA[lane] -= 0x40;
1410
1411 }
1412 } else if (some_high) {
1413 ctrl->timings[channel][slotrank].val_4024++;
1414 ctrl->timings[channel][slotrank].val_4028++;
1415 printram("4024++;\n");
1416 printram("4028++;\n");
1417 }
1418
1419 program_timings(ctrl, channel);
1420
1421 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1422
1423 err = discover_402x(ctrl, channel, slotrank, upperA);
1424 if (err)
1425 return err;
1426
1427 post_timA_change(ctrl, channel, slotrank, &mnmx);
1428 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1429
1430 discover_timA_fine(ctrl, channel, slotrank, upperA);
1431
1432 post_timA_change(ctrl, channel, slotrank, &mnmx);
1433 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1434
1435 FOR_ALL_LANES {
1436 ctrl->timings[channel][slotrank].lanes[lane].timA -= mnmx.timA_min_high * 0x40;
1437 }
1438 ctrl->timings[channel][slotrank].val_4028 -= mnmx.timA_min_high;
1439 printram("4028 -= %d;\n", mnmx.timA_min_high);
1440
1441 post_timA_change(ctrl, channel, slotrank, &mnmx);
1442
1443 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
1444 ctrl->timings[channel][slotrank].val_4024,
1445 ctrl->timings[channel][slotrank].val_4028);
1446
1447 printram("final results:\n");
1448 FOR_ALL_LANES
1449 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
1450 lane,
1451 ctrl->timings[channel][slotrank].lanes[lane].timA);
1452
1453 write32(DEFAULT_MCHBAR + 0x3400, 0);
1454
1455 toggle_io_reset();
1456 }
1457
1458 FOR_ALL_POPULATED_CHANNELS {
1459 program_timings(ctrl, channel);
1460 }
1461 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
1462 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
1463 + 4 * lane, 0);
1464 }
1465 return 0;
1466}
1467
1468static void test_timC(ramctr_timing * ctrl, int channel, int slotrank)
1469{
1470 int lane;
1471
1472 FOR_ALL_LANES {
1473 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + 4 * lane, 0);
1474 read32(DEFAULT_MCHBAR + 0x4140 + 0x400 * channel + 4 * lane);
1475 }
1476
1477 wait_428c(channel);
1478
1479 /* DRAM command ACT */
1480 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
1481 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1482 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD) << 10)
1483 | 4 | (ctrl->tRCD << 16));
1484
1485 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1486 (slotrank << 24) | (6 << 16));
1487
1488 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
1489
1490 /* DRAM command NOP */
1491 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
1492 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8041001);
1493 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1494 (slotrank << 24) | 8);
1495 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
1496
1497 /* DRAM command WR */
1498 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
1499 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x80411f4);
1500 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
1501 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
1502
1503 /* DRAM command NOP */
1504 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
1505 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1506 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
1507 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1508 (slotrank << 24) | 8);
1509 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
1510
1511 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1512
1513 wait_428c(channel);
1514
1515 /* DRAM command PREA */
1516 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1517 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1518 0xc01 | (ctrl->tRP << 16));
1519 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1520 (slotrank << 24) | 0x60400);
1521 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
1522
1523 /* DRAM command ACT */
1524 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
1525 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1526 (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) << 10)
1527 | 8 | (ctrl->CAS << 16));
1528
1529 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1530 (slotrank << 24) | 0x60000);
1531
1532 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x244);
1533
1534 /* DRAM command RD */
1535 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
1536 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1537 0x40011f4 | (max(ctrl->tRTP, 8) << 16));
1538 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
1539 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
1540
1541 /* DRAM command PREA */
1542 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
1543 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1544 0xc01 | (ctrl->tRP << 16));
1545 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1546 (slotrank << 24) | 0x60400);
1547 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
1548 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1549 wait_428c(channel);
1550}
1551
1552static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
1553{
1554 int timC;
1555 int statistics[NUM_LANES][MAX_TIMC + 1];
1556 int lane;
1557
1558 wait_428c(channel);
1559
1560 /* DRAM command PREA */
1561 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1562 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1563 0xc01 | (ctrl->tRP << 16));
1564 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1565 (slotrank << 24) | 0x60400);
1566 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
1567 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
1568
1569 for (timC = 0; timC <= MAX_TIMC; timC++) {
1570 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
1571 timC = timC;
1572 program_timings(ctrl, channel);
1573
1574 test_timC(ctrl, channel, slotrank);
1575
1576 FOR_ALL_LANES {
1577 statistics[lane][timC] =
1578 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
1579 0x400 * channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001580 }
1581 }
1582 FOR_ALL_LANES {
1583 struct run rn =
1584 get_longest_zero_run(statistics[lane], MAX_TIMC + 1);
1585 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
1586 if (rn.all) {
1587 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1588 channel, slotrank, lane);
1589 return MAKE_ERR;
1590 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001591 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1592 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001593 }
1594 return 0;
1595}
1596
1597static int get_precedening_channels(ramctr_timing * ctrl, int target_channel)
1598{
1599 int channel, ret = 0;
1600 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1601 ret++;
1602 return ret;
1603}
1604
1605static void fill_pattern0(ramctr_timing * ctrl, int channel, u32 a, u32 b)
1606{
1607 unsigned j;
1608 unsigned channel_offset =
1609 get_precedening_channels(ctrl, channel) * 0x40;
1610 for (j = 0; j < 16; j++)
1611 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
1612 sfence();
1613}
1614
1615static int num_of_channels(const ramctr_timing * ctrl)
1616{
1617 int ret = 0;
1618 int channel;
1619 FOR_ALL_POPULATED_CHANNELS ret++;
1620 return ret;
1621}
1622
1623static void fill_pattern1(ramctr_timing * ctrl, int channel)
1624{
1625 unsigned j;
1626 unsigned channel_offset =
1627 get_precedening_channels(ctrl, channel) * 0x40;
1628 unsigned channel_step = 0x40 * num_of_channels(ctrl);
1629 for (j = 0; j < 16; j++)
1630 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
1631 for (j = 0; j < 16; j++)
1632 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
1633 sfence();
1634}
1635
1636static void precharge(ramctr_timing * ctrl)
1637{
1638 int channel, slotrank, lane;
1639
1640 FOR_ALL_POPULATED_CHANNELS {
1641 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
1642 ctrl->timings[channel][slotrank].lanes[lane].falling =
1643 16;
1644 ctrl->timings[channel][slotrank].lanes[lane].rising =
1645 16;
1646 }
1647
1648 program_timings(ctrl, channel);
1649
1650 FOR_ALL_POPULATED_RANKS {
1651 wait_428c(channel);
1652
1653 /* DRAM command MRS
1654 * write MR3 MPR enable
1655 * in this mode only RD and RDA are allowed
1656 * all reads return a predefined pattern */
1657 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
1658 0x1f000);
1659 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1660 0xc01 | (ctrl->tMOD << 16));
1661 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1662 (slotrank << 24) | 0x360004);
1663 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1664
1665 /* DRAM command RD */
1666 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
1667 0x1f105);
1668 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1669 0x4041003);
1670 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1671 (slotrank << 24) | 0);
1672 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1673
1674 /* DRAM command RD */
1675 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
1676 0x1f105);
1677 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1678 0x1001 | ((ctrl->CAS + 8) << 16));
1679 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1680 (slotrank << 24) | 0x60000);
1681 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1682
1683 /* DRAM command MRS
1684 * write MR3 MPR disable */
1685 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
1686 0x1f000);
1687 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1688 0xc01 | (ctrl->tMOD << 16));
1689 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1690 (slotrank << 24) | 0x360000);
1691 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1692 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
1693 0xc0001);
1694
1695 wait_428c(channel);
1696 }
1697
1698 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
1699 ctrl->timings[channel][slotrank].lanes[lane].falling =
1700 48;
1701 ctrl->timings[channel][slotrank].lanes[lane].rising =
1702 48;
1703 }
1704
1705 program_timings(ctrl, channel);
1706
1707 FOR_ALL_POPULATED_RANKS {
1708 wait_428c(channel);
1709 /* DRAM command MRS
1710 * write MR3 MPR enable
1711 * in this mode only RD and RDA are allowed
1712 * all reads return a predefined pattern */
1713 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
1714 0x1f000);
1715 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1716 0xc01 | (ctrl->tMOD << 16));
1717 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1718 (slotrank << 24) | 0x360004);
1719 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1720
1721 /* DRAM command RD */
1722 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
1723 0x1f105);
1724 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1725 0x4041003);
1726 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1727 (slotrank << 24) | 0);
1728 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1729
1730 /* DRAM command RD */
1731 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
1732 0x1f105);
1733 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1734 0x1001 | ((ctrl->CAS + 8) << 16));
1735 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1736 (slotrank << 24) | 0x60000);
1737 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1738
1739 /* DRAM command MRS
1740 * write MR3 MPR disable */
1741 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
1742 0x1f000);
1743 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1744 0xc01 | (ctrl->tMOD << 16));
1745
1746 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1747 (slotrank << 24) | 0x360000);
1748 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1749
1750 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
1751 0xc0001);
1752 wait_428c(channel);
1753 }
1754 }
1755}
1756
1757static void test_timB(ramctr_timing * ctrl, int channel, int slotrank)
1758{
1759 /* enable DQs on this slotrank */
1760 write_mrreg(ctrl, channel, slotrank, 1,
1761 0x80 | make_mr1(ctrl, slotrank, channel));
1762
1763 wait_428c(channel);
1764 /* DRAM command NOP */
1765 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f207);
1766 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1767 0x8000c01 | ((ctrl->CWL + ctrl->tWLO) << 16));
1768 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1769 8 | (slotrank << 24));
1770 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1771
1772 /* DRAM command NOP */
1773 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f107);
1774 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1775 0x4000c01 | ((ctrl->CAS + 38) << 16));
1776 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1777 (slotrank << 24) | 4);
1778 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1779
1780 write32(DEFAULT_MCHBAR + 0x400 * channel + 0x4284, 0x40001);
1781 wait_428c(channel);
1782
1783 /* disable DQs on this slotrank */
1784 write_mrreg(ctrl, channel, slotrank, 1,
1785 0x1080 | make_mr1(ctrl, slotrank, channel));
1786}
1787
1788static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1789{
1790 int timB;
1791 int statistics[NUM_LANES][128];
1792 int lane;
1793
1794 write32(DEFAULT_MCHBAR + 0x3400, 0x108052 | (slotrank << 2));
1795
1796 for (timB = 0; timB < 128; timB++) {
1797 FOR_ALL_LANES {
1798 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1799 }
1800 program_timings(ctrl, channel);
1801
1802 test_timB(ctrl, channel, slotrank);
1803
1804 FOR_ALL_LANES {
1805 statistics[lane][timB] =
1806 !((read32
1807 (DEFAULT_MCHBAR + lane_registers[lane] +
1808 channel * 0x100 + 4 + ((timB / 32) & 1) * 4)
1809 >> (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001810 }
1811 }
1812 FOR_ALL_LANES {
1813 struct run rn = get_longest_zero_run(statistics[lane], 128);
1814 /* timC is a direct function of timB's 6 LSBs.
1815 * Some tests increments the value of timB by a small value,
1816 * which might cause the 6bit value to overflow, if it's close
1817 * to 0x3F. Increment the value by a small offset if it's likely
1818 * to overflow, to make sure it won't overflow while running
1819 * tests and bricks the system due to a non matching timC.
1820 *
1821 * TODO: find out why some tests (edge write discovery)
1822 * increment timB. */
1823 if ((rn.start & 0x3F) == 0x3E)
1824 rn.start += 2;
1825 else if ((rn.start & 0x3F) == 0x3F)
1826 rn.start += 1;
1827 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1828 if (rn.all) {
1829 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1830 channel, slotrank, lane);
1831 return MAKE_ERR;
1832 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001833 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1834 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001835 }
1836 return 0;
1837}
1838
1839static int get_timB_high_adjust(u64 val)
1840{
1841 int i;
1842
1843 /* good */
1844 if (val == 0xffffffffffffffffLL)
1845 return 0;
1846
1847 if (val >= 0xf000000000000000LL) {
1848 /* needs negative adjustment */
1849 for (i = 0; i < 8; i++)
1850 if (val << (8 * (7 - i) + 4))
1851 return -i;
1852 } else {
1853 /* needs positive adjustment */
1854 for (i = 0; i < 8; i++)
1855 if (val >> (8 * (7 - i) + 4))
1856 return i;
1857 }
1858 return 8;
1859}
1860
1861static void adjust_high_timB(ramctr_timing * ctrl)
1862{
1863 int channel, slotrank, lane, old;
1864 write32(DEFAULT_MCHBAR + 0x3400, 0x200);
1865 FOR_ALL_POPULATED_CHANNELS {
1866 fill_pattern1(ctrl, channel);
1867 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 1);
1868 }
1869 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1870
1871 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x10001);
1872
1873 wait_428c(channel);
1874
1875 /* DRAM command ACT */
1876 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
1877 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1878 0xc01 | (ctrl->tRCD << 16));
1879 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1880 (slotrank << 24) | 0x60000);
1881 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1882
1883 /* DRAM command NOP */
1884 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
1885 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8040c01);
1886 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1887 (slotrank << 24) | 0x8);
1888 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
1889
1890 /* DRAM command WR */
1891 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
1892 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x8041003);
1893 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1894 (slotrank << 24));
1895 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x3e2);
1896
1897 /* DRAM command NOP */
1898 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
1899 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1900 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
1901 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1902 (slotrank << 24) | 0x8);
1903 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
1904
1905 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1906
1907 wait_428c(channel);
1908
1909 /* DRAM command PREA */
1910 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1911 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1912 0xc01 | ((ctrl->tRP) << 16));
1913 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1914 (slotrank << 24) | 0x60400);
1915 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
1916
1917 /* DRAM command ACT */
1918 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
1919 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1920 0xc01 | ((ctrl->tRCD) << 16));
1921 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1922 (slotrank << 24) | 0x60000);
1923 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1924
1925 /* DRAM command RD */
1926 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x3f105);
1927 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1928 0x4000c01 |
1929 ((ctrl->tRP +
1930 ctrl->timings[channel][slotrank].val_4024 +
1931 ctrl->timings[channel][slotrank].val_4028) << 16));
1932 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1933 (slotrank << 24) | 0x60008);
1934 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1935
1936 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
1937 wait_428c(channel);
1938 FOR_ALL_LANES {
1939 u64 res =
1940 read32(DEFAULT_MCHBAR + lane_registers[lane] +
1941 0x100 * channel + 4);
1942 res |=
1943 ((u64) read32(DEFAULT_MCHBAR + lane_registers[lane] +
1944 0x100 * channel + 8)) << 32;
1945 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1946 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1947 get_timB_high_adjust(res) * 64;
1948
1949 printram("High adjust %d:%016llx\n", lane, res);
1950 printram("Bval+: %d, %d, %d, %x -> %x\n", channel,
1951 slotrank, lane, old,
1952 ctrl->timings[channel][slotrank].lanes[lane].
1953 timB);
1954 }
1955 }
1956 write32(DEFAULT_MCHBAR + 0x3400, 0);
1957}
1958
1959static void write_op(ramctr_timing * ctrl, int channel)
1960{
1961 int slotrank;
1962
1963 wait_428c(channel);
1964
1965 /* choose an existing rank. */
1966 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
1967
1968 /* DRAM command ACT */
1969 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1970 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
1971
1972 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1973 (slotrank << 24) | 0x60000);
1974
1975 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
1976
1977 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
1978 wait_428c(channel);
1979}
1980
1981/* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
1982 * DDR3 adopted the fly-by topology. The data and strobes signals reach
1983 * the chips at different times with respect to command, address and
1984 * clock signals.
1985 * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase
1986 * shift can be introduced.
1987 * It is assumed that the CLK/ADDR/CMD signals have the same routing delay.
1988 *
1989 * To find the required phase shift the DRAM is placed in "write leveling" mode.
1990 * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the
1991 * sampled value on the data lanes (DQs).
1992 */
1993int write_training(ramctr_timing * ctrl)
1994{
1995 int channel, slotrank, lane;
1996 int err;
1997
1998 FOR_ALL_POPULATED_CHANNELS
1999 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
2000 read32(DEFAULT_MCHBAR + 0x4008 +
2001 0x400 * channel) | 0x8000000);
2002
2003 FOR_ALL_POPULATED_CHANNELS {
2004 write_op(ctrl, channel);
2005 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2006 read32(DEFAULT_MCHBAR + 0x4020 +
2007 0x400 * channel) | 0x200000);
2008 }
2009
2010 /* refresh disable */
2011 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2012 FOR_ALL_POPULATED_CHANNELS {
2013 write_op(ctrl, channel);
2014 }
2015
2016 /* enable write leveling on all ranks
2017 * disable all DQ outputs
2018 * only NOP is allowed in this mode */
2019 FOR_ALL_CHANNELS
2020 FOR_ALL_POPULATED_RANKS
2021 write_mrreg(ctrl, channel, slotrank, 1,
2022 make_mr1(ctrl, slotrank, channel) | 0x1080);
2023
2024 write32(DEFAULT_MCHBAR + 0x3400, 0x108052);
2025
2026 toggle_io_reset();
2027
2028 /* set any valid value for timB, it gets corrected later */
2029 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2030 err = discover_timB(ctrl, channel, slotrank);
2031 if (err)
2032 return err;
2033 }
2034
2035 /* disable write leveling on all ranks */
2036 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2037 write_mrreg(ctrl, channel,
2038 slotrank, 1, make_mr1(ctrl, slotrank, channel));
2039
2040 write32(DEFAULT_MCHBAR + 0x3400, 0);
2041
2042 FOR_ALL_POPULATED_CHANNELS
2043 wait_428c(channel);
2044
2045 /* refresh enable */
2046 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) | 8);
2047
2048 FOR_ALL_POPULATED_CHANNELS {
2049 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2050 ~0x00200000 & read32(DEFAULT_MCHBAR + 0x4020 +
2051 0x400 * channel));
2052 read32(DEFAULT_MCHBAR + 0x428c + 0x400 * channel);
2053 wait_428c(channel);
2054
2055 /* DRAM command ZQCS */
2056 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2057 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
2058 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, 0x60000);
2059 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2060
2061 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2062 wait_428c(channel);
2063 }
2064
2065 toggle_io_reset();
2066
2067 printram("CPE\n");
2068 precharge(ctrl);
2069 printram("CPF\n");
2070
2071 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2072 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2073 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2074 0);
2075 }
2076
2077 FOR_ALL_POPULATED_CHANNELS {
2078 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
2079 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2080 }
2081
2082 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2083 err = discover_timC(ctrl, channel, slotrank);
2084 if (err)
2085 return err;
2086 }
2087
2088 FOR_ALL_POPULATED_CHANNELS
2089 program_timings(ctrl, channel);
2090
2091 /* measure and adjust timB timings */
2092 adjust_high_timB(ctrl);
2093
2094 FOR_ALL_POPULATED_CHANNELS
2095 program_timings(ctrl, channel);
2096
2097 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2098 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2099 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2100 0);
2101 }
2102 return 0;
2103}
2104
2105static int test_320c(ramctr_timing * ctrl, int channel, int slotrank)
2106{
2107 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2108 int timC_delta;
2109 int lanes_ok = 0;
2110 int ctr = 0;
2111 int lane;
2112
2113 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2114 FOR_ALL_LANES {
2115 ctrl->timings[channel][slotrank].lanes[lane].timC =
2116 saved_rt.lanes[lane].timC + timC_delta;
2117 }
2118 program_timings(ctrl, channel);
2119 FOR_ALL_LANES {
2120 write32(DEFAULT_MCHBAR + 4 * lane + 0x4f40, 0);
2121 }
2122
2123 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2124
2125 wait_428c(channel);
2126 /* DRAM command ACT */
2127 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2128 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2129 ((max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1)) << 10)
2130 | 8 | (ctrl->tRCD << 16));
2131
2132 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2133 (slotrank << 24) | ctr | 0x60000);
2134
2135 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
2136 /* DRAM command WR */
2137 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2138 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2139 0x8001020 | ((ctrl->CWL + ctrl->tWTR + 8) << 16));
2140 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2141 (slotrank << 24));
2142 write32(DEFAULT_MCHBAR + 0x4244 + 0x400 * channel, 0x389abcd);
2143 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x20e42);
2144
2145 /* DRAM command RD */
2146 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2147 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2148 0x4001020 | (max(ctrl->tRTP, 8) << 16));
2149 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2150 (slotrank << 24));
2151 write32(DEFAULT_MCHBAR + 0x4248 + 0x400 * channel, 0x389abcd);
2152 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x20e42);
2153
2154 /* DRAM command PRE */
2155 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2156 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, 0xf1001);
2157 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2158 (slotrank << 24) | 0x60400);
2159 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2160
2161 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2162 wait_428c(channel);
2163 FOR_ALL_LANES {
2164 u32 r32 =
2165 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2166 0x400 * channel);
2167
2168 if (r32 == 0)
2169 lanes_ok |= 1 << lane;
2170 }
2171 ctr++;
2172 if (lanes_ok == ((1 << NUM_LANES) - 1))
2173 break;
2174 }
2175
2176 ctrl->timings[channel][slotrank] = saved_rt;
2177
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002178 return lanes_ok != ((1 << NUM_LANES) - 1);
2179}
2180
2181#include "raminit_patterns.h"
2182
2183static void fill_pattern5(ramctr_timing * ctrl, int channel, int patno)
2184{
2185 unsigned i, j;
2186 unsigned channel_offset =
2187 get_precedening_channels(ctrl, channel) * 0x40;
2188 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2189
2190 if (patno) {
2191 u8 base8 = 0x80 >> ((patno - 1) % 8);
2192 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2193 for (i = 0; i < 32; i++) {
2194 for (j = 0; j < 16; j++) {
2195 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
2196 if (invert[patno - 1][i] & (1 << (j / 2)))
2197 val = ~val;
2198 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2199 j * 4), val);
2200 }
2201 }
2202
2203 } else {
2204 for (i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++) {
2205 for (j = 0; j < 16; j++)
2206 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2207 j * 4), pattern[i][j]);
2208 }
2209 sfence();
2210 }
2211}
2212
2213static void reprogram_320c(ramctr_timing * ctrl)
2214{
2215 int channel, slotrank;
2216
2217 FOR_ALL_POPULATED_CHANNELS {
2218 wait_428c(channel);
2219
2220 /* choose an existing rank. */
2221 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2222
2223 /* DRAM command ZQCS */
2224 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2225 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2226
2227 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2228 (slotrank << 24) | 0x60000);
2229
2230 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2231
2232 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2233 wait_428c(channel);
2234 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2235 read32(DEFAULT_MCHBAR + 0x4020 +
2236 0x400 * channel) | 0x200000);
2237 }
2238
2239 /* refresh disable */
2240 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2241 FOR_ALL_POPULATED_CHANNELS {
2242 wait_428c(channel);
2243
2244 /* choose an existing rank. */
2245 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2246
2247 /* DRAM command ZQCS */
2248 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2249 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2250
2251 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2252 (slotrank << 24) | 0x60000);
2253
2254 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2255
2256 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2257 wait_428c(channel);
2258 }
2259
2260 /* jedec reset */
2261 dram_jedecreset(ctrl);
2262 /* mrs commands. */
2263 dram_mrscommands(ctrl);
2264
2265 toggle_io_reset();
2266}
2267
2268#define MIN_C320C_LEN 13
2269
2270static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2271{
2272 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2273 int slotrank;
2274 int c320c;
2275 int stat[NUM_SLOTRANKS][256];
2276 int delta = 0;
2277
2278 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2279
2280 FOR_ALL_POPULATED_RANKS {
2281 saved_timings[channel][slotrank] =
2282 ctrl->timings[channel][slotrank];
2283 }
2284
2285 ctrl->cmd_stretch[channel] = cmd_stretch;
2286
2287 MCHBAR32(0x4004 + 0x400 * channel) =
2288 ctrl->tRRD
2289 | (ctrl->tRTP << 4)
2290 | (ctrl->tCKE << 8)
2291 | (ctrl->tWTR << 12)
2292 | (ctrl->tFAW << 16)
2293 | (ctrl->tWR << 24)
2294 | (ctrl->cmd_stretch[channel] << 30);
2295
2296 if (ctrl->cmd_stretch[channel] == 2)
2297 delta = 2;
2298 else if (ctrl->cmd_stretch[channel] == 0)
2299 delta = 4;
2300
2301 FOR_ALL_POPULATED_RANKS {
2302 ctrl->timings[channel][slotrank].val_4024 -= delta;
2303 }
2304
2305 for (c320c = -127; c320c <= 127; c320c++) {
2306 FOR_ALL_POPULATED_RANKS {
2307 ctrl->timings[channel][slotrank].val_320c = c320c;
2308 }
2309 program_timings(ctrl, channel);
2310 reprogram_320c(ctrl);
2311 FOR_ALL_POPULATED_RANKS {
2312 stat[slotrank][c320c + 127] =
2313 test_320c(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002314 }
2315 }
2316 FOR_ALL_POPULATED_RANKS {
2317 struct run rn =
2318 get_longest_zero_run(stat[slotrank], 255);
2319 ctrl->timings[channel][slotrank].val_320c =
2320 rn.middle - 127;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002321 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2322 channel, slotrank, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002323 if (rn.all || rn.length < MIN_C320C_LEN) {
2324 FOR_ALL_POPULATED_RANKS {
2325 ctrl->timings[channel][slotrank] =
2326 saved_timings[channel][slotrank];
2327 }
2328 return MAKE_ERR;
2329 }
2330 }
2331
2332 return 0;
2333}
2334
2335/* Adjust CMD phase shift and try multiple command rates.
2336 * A command rate of 2T doubles the time needed for address and
2337 * command decode. */
2338int command_training(ramctr_timing *ctrl)
2339{
2340 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002341
2342 FOR_ALL_POPULATED_CHANNELS {
2343 fill_pattern5(ctrl, channel, 0);
2344 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2345 }
2346
2347 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002348 int cmdrate, err;
2349
2350 /*
2351 * Dual DIMM per channel:
2352 * Issue: While c320c discovery seems to succeed raminit
2353 * will fail in write training.
2354 * Workaround: Skip 1T in dual DIMM mode, that's only
2355 * supported by a few DIMMs.
Dan Elkoubydabebc32018-04-13 18:47:10 +03002356 * Only try 1T mode for XMP DIMMs that request it in dual DIMM
2357 * mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002358 *
2359 * Single DIMM per channel:
2360 * Try command rate 1T and 2T
2361 */
2362 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002363 if (ctrl->tCMD)
2364 /* XMP gives the CMD rate in clock ticks, not ns */
2365 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002366
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002367 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002368 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2369
2370 if (!err)
2371 break;
2372 }
2373
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002374 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002375 printk(BIOS_EMERG, "c320c discovery failed\n");
2376 return err;
2377 }
2378
2379 printram("Using CMD rate %uT on channel %u\n",
2380 cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002381 }
2382
2383 FOR_ALL_POPULATED_CHANNELS
2384 program_timings(ctrl, channel);
2385
2386 reprogram_320c(ctrl);
2387 return 0;
2388}
2389
2390
2391static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank,
2392 int *edges)
2393{
2394 int edge;
2395 int statistics[NUM_LANES][MAX_EDGE_TIMING + 1];
2396 int lane;
2397
2398 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2399 FOR_ALL_LANES {
2400 ctrl->timings[channel][slotrank].lanes[lane].rising =
2401 edge;
2402 ctrl->timings[channel][slotrank].lanes[lane].falling =
2403 edge;
2404 }
2405 program_timings(ctrl, channel);
2406
2407 FOR_ALL_LANES {
2408 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2409 4 * lane, 0);
2410 read32(DEFAULT_MCHBAR + 0x400 * channel + 4 * lane +
2411 0x4140);
2412 }
2413
2414 wait_428c(channel);
2415 /* 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 */
2419 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
2420 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2421 (0xc01 | (ctrl->tMOD << 16)));
2422 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2423 (slotrank << 24) | 0x360004);
2424 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2425
2426 /* DRAM command RD */
2427 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
2428 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x40411f4);
2429 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2430 (slotrank << 24));
2431 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2432
2433 /* DRAM command RD */
2434 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2435 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2436 0x1001 | ((ctrl->CAS + 8) << 16));
2437 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2438 (slotrank << 24) | 0x60000);
2439 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2440
2441 /* DRAM command MRS
2442 * MR3 disable MPR */
2443 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
2444 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2445 (0xc01 | (ctrl->tMOD << 16)));
2446 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2447 (slotrank << 24) | 0x360000);
2448 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2449
2450 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2451
2452 wait_428c(channel);
2453
2454 FOR_ALL_LANES {
2455 statistics[lane][edge] =
2456 read32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2457 lane * 4);
2458 }
2459 }
2460 FOR_ALL_LANES {
2461 struct run rn =
2462 get_longest_zero_run(statistics[lane], MAX_EDGE_TIMING + 1);
2463 edges[lane] = rn.middle;
2464 if (rn.all) {
2465 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n",
2466 channel, slotrank, lane);
2467 return MAKE_ERR;
2468 }
2469 printram("eval %d, %d, %d: %02x\n", channel, slotrank,
2470 lane, edges[lane]);
2471 }
2472 return 0;
2473}
2474
2475int discover_edges(ramctr_timing *ctrl)
2476{
2477 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2478 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2479 int channel, slotrank, lane;
2480 int err;
2481
2482 write32(DEFAULT_MCHBAR + 0x3400, 0);
2483
2484 toggle_io_reset();
2485
2486 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
2487 write32(DEFAULT_MCHBAR + 4 * lane +
2488 0x400 * channel + 0x4080, 0);
2489 }
2490
2491 FOR_ALL_POPULATED_CHANNELS {
2492 fill_pattern0(ctrl, channel, 0, 0);
2493 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2494 FOR_ALL_LANES {
2495 read32(DEFAULT_MCHBAR + 0x400 * channel +
2496 lane * 4 + 0x4140);
2497 }
2498
2499 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2500 ctrl->timings[channel][slotrank].lanes[lane].falling =
2501 16;
2502 ctrl->timings[channel][slotrank].lanes[lane].rising =
2503 16;
2504 }
2505
2506 program_timings(ctrl, channel);
2507
2508 FOR_ALL_POPULATED_RANKS {
2509 wait_428c(channel);
2510
2511 /* DRAM command MRS
2512 * MR3 enable MPR
2513 * write MR3 MPR enable
2514 * in this mode only RD and RDA are allowed
2515 * all reads return a predefined pattern */
2516 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2517 0x1f000);
2518 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2519 0xc01 | (ctrl->tMOD << 16));
2520 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2521 (slotrank << 24) | 0x360004);
2522 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2523
2524 /* DRAM command RD */
2525 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2526 0x1f105);
2527 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2528 0x4041003);
2529 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2530 (slotrank << 24) | 0);
2531 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2532
2533 /* DRAM command RD */
2534 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2535 0x1f105);
2536 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2537 0x1001 | ((ctrl->CAS + 8) << 16));
2538 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2539 (slotrank << 24) | 0x60000);
2540 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2541
2542 /* DRAM command MRS
2543 * MR3 disable MPR */
2544 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2545 0x1f000);
2546 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2547 0xc01 | (ctrl->tMOD << 16));
2548 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2549 (slotrank << 24) | 0x360000);
2550 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2551 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2552 0xc0001);
2553
2554 wait_428c(channel);
2555 }
2556
2557 /* XXX: check any measured value ? */
2558
2559 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2560 ctrl->timings[channel][slotrank].lanes[lane].falling =
2561 48;
2562 ctrl->timings[channel][slotrank].lanes[lane].rising =
2563 48;
2564 }
2565
2566 program_timings(ctrl, channel);
2567
2568 FOR_ALL_POPULATED_RANKS {
2569 wait_428c(channel);
2570
2571 /* DRAM command MRS
2572 * MR3 enable MPR
2573 * write MR3 MPR enable
2574 * in this mode only RD and RDA are allowed
2575 * all reads return a predefined pattern */
2576 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2577 0x1f000);
2578 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2579 0xc01 | (ctrl->tMOD << 16));
2580 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2581 (slotrank << 24) | 0x360004);
2582 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2583
2584 /* DRAM command RD */
2585 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2586 0x1f105);
2587 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2588 0x4041003);
2589 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2590 (slotrank << 24) | 0);
2591 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2592
2593 /* DRAM command RD */
2594 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2595 0x1f105);
2596 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2597 0x1001 | ((ctrl->CAS + 8) << 16));
2598 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2599 (slotrank << 24) | 0x60000);
2600 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2601
2602 /* DRAM command MRS
2603 * MR3 disable MPR */
2604 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2605 0x1f000);
2606 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2607 0xc01 | (ctrl->tMOD << 16));
2608 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2609 (slotrank << 24) | 0x360000);
2610 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2611
2612 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2613 0xc0001);
2614 wait_428c(channel);
2615 }
2616
2617 /* XXX: check any measured value ? */
2618
2619 FOR_ALL_LANES {
2620 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel +
2621 lane * 4,
2622 ~read32(DEFAULT_MCHBAR + 0x4040 +
2623 0x400 * channel + lane * 4) & 0xff);
2624 }
2625
2626 fill_pattern0(ctrl, channel, 0, 0xffffffff);
2627 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2628 }
2629
2630 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
2631 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
2632 printram("discover falling edges:\n[%x] = %x\n", 0x4eb0, 0x300);
2633
2634 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2635 err = discover_edges_real(ctrl, channel, slotrank,
2636 falling_edges[channel][slotrank]);
2637 if (err)
2638 return err;
2639 }
2640
2641 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
2642 printram("discover rising edges:\n[%x] = %x\n", 0x4eb0, 0x200);
2643
2644 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2645 err = discover_edges_real(ctrl, channel, slotrank,
2646 rising_edges[channel][slotrank]);
2647 if (err)
2648 return err;
2649 }
2650
2651 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
2652
2653 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2654 ctrl->timings[channel][slotrank].lanes[lane].falling =
2655 falling_edges[channel][slotrank][lane];
2656 ctrl->timings[channel][slotrank].lanes[lane].rising =
2657 rising_edges[channel][slotrank][lane];
2658 }
2659
2660 FOR_ALL_POPULATED_CHANNELS {
2661 program_timings(ctrl, channel);
2662 }
2663
2664 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2665 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2666 0);
2667 }
2668 return 0;
2669}
2670
2671static int discover_edges_write_real(ramctr_timing *ctrl, int channel,
2672 int slotrank, int *edges)
2673{
2674 int edge;
2675 u32 raw_statistics[MAX_EDGE_TIMING + 1];
2676 int statistics[MAX_EDGE_TIMING + 1];
2677 const int reg3000b24[] = { 0, 0xc, 0x2c };
2678 int lane, i;
2679 int lower[NUM_LANES];
2680 int upper[NUM_LANES];
2681 int pat;
2682
2683 FOR_ALL_LANES {
2684 lower[lane] = 0;
2685 upper[lane] = MAX_EDGE_TIMING;
2686 }
2687
2688 for (i = 0; i < 3; i++) {
2689 write32(DEFAULT_MCHBAR + 0x3000 + 0x100 * channel,
2690 reg3000b24[i] << 24);
2691 printram("[%x] = 0x%08x\n",
2692 0x3000 + 0x100 * channel, reg3000b24[i] << 24);
2693 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2694 fill_pattern5(ctrl, channel, pat);
2695 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2696 printram("using pattern %d\n", pat);
2697 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2698 FOR_ALL_LANES {
2699 ctrl->timings[channel][slotrank].lanes[lane].
2700 rising = edge;
2701 ctrl->timings[channel][slotrank].lanes[lane].
2702 falling = edge;
2703 }
2704 program_timings(ctrl, channel);
2705
2706 FOR_ALL_LANES {
2707 write32(DEFAULT_MCHBAR + 0x4340 +
2708 0x400 * channel + 4 * lane, 0);
2709 read32(DEFAULT_MCHBAR + 0x400 * channel +
2710 4 * lane + 0x4140);
2711 }
2712 wait_428c(channel);
2713
2714 /* DRAM command ACT */
2715 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2716 0x1f006);
2717 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2718 0x4 | (ctrl->tRCD << 16)
2719 | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) <<
2720 10));
2721 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2722 (slotrank << 24) | 0x60000);
2723 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel,
2724 0x240);
2725
2726 /* DRAM command WR */
2727 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2728 0x1f201);
2729 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2730 0x8005020 | ((ctrl->tWTR + ctrl->CWL + 8) <<
2731 16));
2732 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2733 (slotrank << 24));
2734 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel,
2735 0x242);
2736
2737 /* DRAM command RD */
2738 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2739 0x1f105);
2740 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2741 0x4005020 | (max(ctrl->tRTP, 8) << 16));
2742 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2743 (slotrank << 24));
2744 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel,
2745 0x242);
2746
2747 /* DRAM command PRE */
2748 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2749 0x1f002);
2750 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2751 0xc01 | (ctrl->tRP << 16));
2752 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2753 (slotrank << 24) | 0x60400);
2754 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2755
2756 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2757 0xc0001);
2758 wait_428c(channel);
2759 FOR_ALL_LANES {
2760 read32(DEFAULT_MCHBAR + 0x4340 +
2761 0x400 * channel + lane * 4);
2762 }
2763
2764 raw_statistics[edge] =
2765 MCHBAR32(0x436c + 0x400 * channel);
2766 }
2767 FOR_ALL_LANES {
2768 struct run rn;
2769 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
2770 statistics[edge] =
2771 ! !(raw_statistics[edge] & (1 << lane));
2772 rn = get_longest_zero_run(statistics,
2773 MAX_EDGE_TIMING + 1);
2774 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
2775 channel, slotrank, i, rn.start, rn.middle,
2776 rn.end, rn.start + ctrl->edge_offset[i],
2777 rn.end - ctrl->edge_offset[i]);
2778 lower[lane] =
2779 max(rn.start + ctrl->edge_offset[i], lower[lane]);
2780 upper[lane] =
2781 min(rn.end - ctrl->edge_offset[i], upper[lane]);
2782 edges[lane] = (lower[lane] + upper[lane]) / 2;
2783 if (rn.all || (lower[lane] > upper[lane])) {
2784 printk(BIOS_EMERG, "edge write discovery failed: %d, %d, %d\n",
2785 channel, slotrank, lane);
2786 return MAKE_ERR;
2787 }
2788 }
2789 }
2790 }
2791
2792 write32(DEFAULT_MCHBAR + 0x3000, 0);
2793 printram("CPA\n");
2794 return 0;
2795}
2796
2797int discover_edges_write(ramctr_timing *ctrl)
2798{
2799 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2800 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2801 int channel, slotrank, lane;
2802 int err;
2803
2804 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
2805 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
2806 printram("discover falling edges write:\n[%x] = %x\n", 0x4eb0, 0x300);
2807
2808 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2809 err = discover_edges_write_real(ctrl, channel, slotrank,
2810 falling_edges[channel][slotrank]);
2811 if (err)
2812 return err;
2813 }
2814
2815 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
2816 printram("discover rising edges write:\n[%x] = %x\n", 0x4eb0, 0x200);
2817
2818 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2819 err = discover_edges_write_real(ctrl, channel, slotrank,
2820 rising_edges[channel][slotrank]);
2821 if (err)
2822 return err;
2823 }
2824
2825 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
2826
2827 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2828 ctrl->timings[channel][slotrank].lanes[lane].falling =
2829 falling_edges[channel][slotrank][lane];
2830 ctrl->timings[channel][slotrank].lanes[lane].rising =
2831 rising_edges[channel][slotrank][lane];
2832 }
2833
2834 FOR_ALL_POPULATED_CHANNELS
2835 program_timings(ctrl, channel);
2836
2837 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2838 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2839 0);
2840 }
2841 return 0;
2842}
2843
2844static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2845{
2846 wait_428c(channel);
2847 /* DRAM command ACT */
2848 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2849 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2850 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD)
2851 << 10) | (ctrl->tRCD << 16) | 4);
2852 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2853 (slotrank << 24) | 0x60000);
2854 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
2855
2856 /* DRAM command WR */
2857 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2858 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2859 0x80011e0 |
2860 ((ctrl->tWTR + ctrl->CWL + 8) << 16));
2861 write32(DEFAULT_MCHBAR + 0x4204 +
2862 0x400 * channel, (slotrank << 24));
2863 write32(DEFAULT_MCHBAR + 0x4214 +
2864 0x400 * channel, 0x242);
2865
2866 /* DRAM command RD */
2867 write32(DEFAULT_MCHBAR + 0x4228 +
2868 0x400 * channel, 0x1f105);
2869 write32(DEFAULT_MCHBAR + 0x4238 +
2870 0x400 * channel,
2871 0x40011e0 | (max(ctrl->tRTP, 8) << 16));
2872 write32(DEFAULT_MCHBAR + 0x4208 +
2873 0x400 * channel, (slotrank << 24));
2874 write32(DEFAULT_MCHBAR + 0x4218 +
2875 0x400 * channel, 0x242);
2876
2877 /* DRAM command PRE */
2878 write32(DEFAULT_MCHBAR + 0x422c +
2879 0x400 * channel, 0x1f002);
2880 write32(DEFAULT_MCHBAR + 0x423c +
2881 0x400 * channel,
2882 0x1001 | (ctrl->tRP << 16));
2883 write32(DEFAULT_MCHBAR + 0x420c +
2884 0x400 * channel,
2885 (slotrank << 24) | 0x60400);
2886 write32(DEFAULT_MCHBAR + 0x421c +
2887 0x400 * channel, 0);
2888
2889 write32(DEFAULT_MCHBAR + 0x4284 +
2890 0x400 * channel, 0xc0001);
2891 wait_428c(channel);
2892}
2893
2894int discover_timC_write(ramctr_timing *ctrl)
2895{
2896 const u8 rege3c_b24[3] = { 0, 0xf, 0x2f };
2897 int i, pat;
2898
2899 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2900 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2901 int channel, slotrank, lane;
2902
2903 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2904 lower[channel][slotrank][lane] = 0;
2905 upper[channel][slotrank][lane] = MAX_TIMC;
2906 }
2907
2908 write32(DEFAULT_MCHBAR + 0x4ea8, 1);
2909 printram("discover timC write:\n");
2910
2911 for (i = 0; i < 3; i++)
2912 FOR_ALL_POPULATED_CHANNELS {
2913 write32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100),
2914 (rege3c_b24[i] << 24)
2915 | (read32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100))
2916 & ~0x3f000000));
2917 udelay(2);
2918 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2919 FOR_ALL_POPULATED_RANKS {
2920 int timC;
2921 u32 raw_statistics[MAX_TIMC + 1];
2922 int statistics[MAX_TIMC + 1];
2923
2924 /* Make sure rn.start < rn.end */
2925 statistics[MAX_TIMC] = 1;
2926
2927 fill_pattern5(ctrl, channel, pat);
2928 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2929 for (timC = 0; timC < MAX_TIMC; timC++) {
2930 FOR_ALL_LANES
2931 ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
2932 program_timings(ctrl, channel);
2933
2934 test_timC_write (ctrl, channel, slotrank);
2935
2936 raw_statistics[timC] =
2937 MCHBAR32(0x436c + 0x400 * channel);
2938 }
2939 FOR_ALL_LANES {
2940 struct run rn;
2941 for (timC = 0; timC < MAX_TIMC; timC++)
2942 statistics[timC] =
2943 !!(raw_statistics[timC] &
2944 (1 << lane));
2945
2946 rn = get_longest_zero_run(statistics,
2947 MAX_TIMC + 1);
2948 if (rn.all) {
2949 printk(BIOS_EMERG, "timC write discovery failed: %d, %d, %d\n",
2950 channel, slotrank, lane);
2951 return MAKE_ERR;
2952 }
2953 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
2954 channel, slotrank, i, rn.start,
2955 rn.middle, rn.end,
2956 rn.start + ctrl->timC_offset[i],
2957 rn.end - ctrl->timC_offset[i]);
2958 lower[channel][slotrank][lane] =
2959 max(rn.start + ctrl->timC_offset[i],
2960 lower[channel][slotrank][lane]);
2961 upper[channel][slotrank][lane] =
2962 min(rn.end - ctrl->timC_offset[i],
2963 upper[channel][slotrank][lane]);
2964
2965 }
2966 }
2967 }
2968 }
2969
2970 FOR_ALL_CHANNELS {
2971 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
2972 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
2973 ~0x3f000000));
2974 udelay(2);
2975 }
2976
2977 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
2978
2979 printram("CPB\n");
2980
2981 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2982 printram("timC %d, %d, %d: %x\n", channel,
2983 slotrank, lane,
2984 (lower[channel][slotrank][lane] +
2985 upper[channel][slotrank][lane]) / 2);
2986 ctrl->timings[channel][slotrank].lanes[lane].timC =
2987 (lower[channel][slotrank][lane] +
2988 upper[channel][slotrank][lane]) / 2;
2989 }
2990 FOR_ALL_POPULATED_CHANNELS {
2991 program_timings(ctrl, channel);
2992 }
2993 return 0;
2994}
2995
2996void normalize_training(ramctr_timing * ctrl)
2997{
2998 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01002999 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003000
3001 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3002 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01003003 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003004 FOR_ALL_LANES mat =
3005 max(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01003006 printram("normalize %d, %d, %d: mat %d\n",
3007 channel, slotrank, lane, mat);
3008
3009 delta = (mat >> 6) - ctrl->timings[channel][slotrank].val_4028;
3010 printram("normalize %d, %d, %d: delta %d\n",
3011 channel, slotrank, lane, delta);
3012
3013 ctrl->timings[channel][slotrank].val_4024 += delta;
3014 ctrl->timings[channel][slotrank].val_4028 += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003015 }
3016
3017 FOR_ALL_POPULATED_CHANNELS {
3018 program_timings(ctrl, channel);
3019 }
3020}
3021
3022void write_controller_mr(ramctr_timing * ctrl)
3023{
3024 int channel, slotrank;
3025
3026 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3027 write32(DEFAULT_MCHBAR + 0x0004 + (channel << 8) +
3028 lane_registers[slotrank], make_mr0(ctrl, slotrank));
3029 write32(DEFAULT_MCHBAR + 0x0008 + (channel << 8) +
3030 lane_registers[slotrank],
3031 make_mr1(ctrl, slotrank, channel));
3032 }
3033}
3034
3035int channel_test(ramctr_timing *ctrl)
3036{
3037 int channel, slotrank, lane;
3038
3039 slotrank = 0;
3040 FOR_ALL_POPULATED_CHANNELS
3041 if (read32(DEFAULT_MCHBAR + 0x42a0 + (channel << 10)) & 0xa000) {
3042 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n",
3043 channel);
3044 return MAKE_ERR;
3045 }
3046 FOR_ALL_POPULATED_CHANNELS {
3047 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
3048
3049 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3050 }
3051
3052 for (slotrank = 0; slotrank < 4; slotrank++)
3053 FOR_ALL_CHANNELS
3054 if (ctrl->rankmap[channel] & (1 << slotrank)) {
3055 FOR_ALL_LANES {
3056 write32(DEFAULT_MCHBAR + (0x4f40 + 4 * lane), 0);
3057 write32(DEFAULT_MCHBAR + (0x4d40 + 4 * lane), 0);
3058 }
3059 wait_428c(channel);
3060 /* DRAM command ACT */
3061 write32(DEFAULT_MCHBAR + 0x4220 + (channel << 10), 0x0001f006);
3062 write32(DEFAULT_MCHBAR + 0x4230 + (channel << 10), 0x0028a004);
3063 write32(DEFAULT_MCHBAR + 0x4200 + (channel << 10),
3064 0x00060000 | (slotrank << 24));
3065 write32(DEFAULT_MCHBAR + 0x4210 + (channel << 10), 0x00000244);
3066 /* DRAM command WR */
3067 write32(DEFAULT_MCHBAR + 0x4224 + (channel << 10), 0x0001f201);
3068 write32(DEFAULT_MCHBAR + 0x4234 + (channel << 10), 0x08281064);
3069 write32(DEFAULT_MCHBAR + 0x4204 + (channel << 10),
3070 0x00000000 | (slotrank << 24));
3071 write32(DEFAULT_MCHBAR + 0x4214 + (channel << 10), 0x00000242);
3072 /* DRAM command RD */
3073 write32(DEFAULT_MCHBAR + 0x4228 + (channel << 10), 0x0001f105);
3074 write32(DEFAULT_MCHBAR + 0x4238 + (channel << 10), 0x04281064);
3075 write32(DEFAULT_MCHBAR + 0x4208 + (channel << 10),
3076 0x00000000 | (slotrank << 24));
3077 write32(DEFAULT_MCHBAR + 0x4218 + (channel << 10), 0x00000242);
3078 /* DRAM command PRE */
3079 write32(DEFAULT_MCHBAR + 0x422c + (channel << 10), 0x0001f002);
3080 write32(DEFAULT_MCHBAR + 0x423c + (channel << 10), 0x00280c01);
3081 write32(DEFAULT_MCHBAR + 0x420c + (channel << 10),
3082 0x00060400 | (slotrank << 24));
3083 write32(DEFAULT_MCHBAR + 0x421c + (channel << 10), 0x00000240);
3084 write32(DEFAULT_MCHBAR + 0x4284 + (channel << 10), 0x000c0001);
3085 wait_428c(channel);
3086 FOR_ALL_LANES
3087 if (read32(DEFAULT_MCHBAR + 0x4340 + (channel << 10) + 4 * lane)) {
3088 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
3089 channel, slotrank, lane);
3090 return MAKE_ERR;
3091 }
3092 }
3093 return 0;
3094}
3095
3096void set_scrambling_seed(ramctr_timing * ctrl)
3097{
3098 int channel;
3099
3100 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them?
3101 I don't think so. */
3102 static u32 seeds[NUM_CHANNELS][3] = {
3103 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
3104 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
3105 };
3106 FOR_ALL_POPULATED_CHANNELS {
3107 MCHBAR32(0x4020 + 0x400 * channel) &= ~0x10000000;
Arthur Heymans6af8aab2017-09-26 23:18:14 +02003108 MCHBAR32(0x4034 + 0x400 * channel) = seeds[channel][0];
3109 MCHBAR32(0x403c + 0x400 * channel) = seeds[channel][1];
3110 MCHBAR32(0x4038 + 0x400 * channel) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003111 }
3112}
3113
3114void set_4f8c(void)
3115{
3116 struct cpuid_result cpures;
3117 u32 cpu;
3118
3119 cpures = cpuid(1);
3120 cpu = (cpures.eax);
3121 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
3122 MCHBAR32(0x4f8c) = 0x141D1519;
3123 } else {
3124 MCHBAR32(0x4f8c) = 0x551D1519;
3125 }
3126}
3127
3128void prepare_training(ramctr_timing * ctrl)
3129{
3130 int channel;
3131
3132 FOR_ALL_POPULATED_CHANNELS {
3133 // Always drive command bus
3134 MCHBAR32(0x4004 + 0x400 * channel) |= 0x20000000;
3135 }
3136
3137 udelay(1);
3138
3139 FOR_ALL_POPULATED_CHANNELS {
3140 wait_428c(channel);
3141 }
3142}
3143
3144void set_4008c(ramctr_timing * ctrl)
3145{
3146 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01003147
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003148 FOR_ALL_POPULATED_CHANNELS {
3149 u32 b20, b4_8_12;
3150 int min_320c = 10000;
3151 int max_320c = -10000;
3152
3153 FOR_ALL_POPULATED_RANKS {
3154 max_320c = max(ctrl->timings[channel][slotrank].val_320c, max_320c);
3155 min_320c = min(ctrl->timings[channel][slotrank].val_320c, min_320c);
3156 }
3157
3158 if (max_320c - min_320c > 51)
3159 b20 = 0;
3160 else
3161 b20 = ctrl->ref_card_offset[channel];
3162
3163 if (ctrl->reg_320c_range_threshold < max_320c - min_320c)
3164 b4_8_12 = 0x3330;
3165 else
3166 b4_8_12 = 0x2220;
3167
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01003168 dram_odt_stretch(ctrl, channel);
3169
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003170 write32(DEFAULT_MCHBAR + 0x4008 + (channel << 10),
3171 0x0a000000
3172 | (b20 << 20)
3173 | ((ctrl->ref_card_offset[channel] + 2) << 16)
3174 | b4_8_12);
3175 }
3176}
3177
3178void set_42a0(ramctr_timing * ctrl)
3179{
3180 int channel;
3181 FOR_ALL_POPULATED_CHANNELS {
3182 write32(DEFAULT_MCHBAR + (0x42a0 + 0x400 * channel),
3183 0x00001000 | ctrl->rankmap[channel]);
3184 MCHBAR32(0x4004 + 0x400 * channel) &= ~0x20000000; // OK
3185 }
3186}
3187
3188static int encode_5d10(int ns)
3189{
3190 return (ns + 499) / 500;
3191}
3192
3193/* FIXME: values in this function should be hardware revision-dependent. */
3194void final_registers(ramctr_timing * ctrl)
3195{
3196 int channel;
3197 int t1_cycles = 0, t1_ns = 0, t2_ns;
3198 int t3_ns;
3199 u32 r32;
3200
3201 write32(DEFAULT_MCHBAR + 0x4cd4, 0x00000046);
3202
3203 write32(DEFAULT_MCHBAR + 0x400c, (read32(DEFAULT_MCHBAR + 0x400c) & 0xFFFFCFFF) | 0x1000); // OK
3204 write32(DEFAULT_MCHBAR + 0x440c, (read32(DEFAULT_MCHBAR + 0x440c) & 0xFFFFCFFF) | 0x1000); // OK
3205 write32(DEFAULT_MCHBAR + 0x4cb0, 0x00000740);
3206 write32(DEFAULT_MCHBAR + 0x4380, 0x00000aaa); // OK
3207 write32(DEFAULT_MCHBAR + 0x4780, 0x00000aaa); // OK
3208 write32(DEFAULT_MCHBAR + 0x4f88, 0x5f7003ff); // OK
3209 write32(DEFAULT_MCHBAR + 0x5064, 0x00073000 | ctrl->reg_5064b0); // OK
3210
3211 FOR_ALL_CHANNELS {
3212 switch (ctrl->rankmap[channel]) {
3213 /* Unpopulated channel. */
3214 case 0:
3215 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0);
3216 break;
3217 /* Only single-ranked dimms. */
3218 case 1:
3219 case 4:
3220 case 5:
3221 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x373131);
3222 break;
3223 /* Dual-ranked dimms present. */
3224 default:
3225 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x9b6ea1);
3226 break;
3227 }
3228 }
3229
3230 write32 (DEFAULT_MCHBAR + 0x5880, 0xca9171e5);
3231 write32 (DEFAULT_MCHBAR + 0x5888,
3232 (read32 (DEFAULT_MCHBAR + 0x5888) & ~0xffffff) | 0xe4d5d0);
3233 write32 (DEFAULT_MCHBAR + 0x58a8, read32 (DEFAULT_MCHBAR + 0x58a8) & ~0x1f);
3234 write32 (DEFAULT_MCHBAR + 0x4294,
3235 (read32 (DEFAULT_MCHBAR + 0x4294) & ~0x30000)
3236 | (1 << 16));
3237 write32 (DEFAULT_MCHBAR + 0x4694,
3238 (read32 (DEFAULT_MCHBAR + 0x4694) & ~0x30000)
3239 | (1 << 16));
3240
3241 MCHBAR32(0x5030) |= 1; // OK
3242 MCHBAR32(0x5030) |= 0x80; // OK
3243 MCHBAR32(0x5f18) = 0xfa; // OK
3244
3245 /* Find a populated channel. */
3246 FOR_ALL_POPULATED_CHANNELS
3247 break;
3248
3249 t1_cycles = ((read32(DEFAULT_MCHBAR + 0x4290 + channel * 0x400) >> 8) & 0xff);
3250 r32 = read32(DEFAULT_MCHBAR + 0x5064);
3251 if (r32 & 0x20000)
3252 t1_cycles += (r32 & 0xfff);
3253 t1_cycles += (read32(DEFAULT_MCHBAR + channel * 0x400 + 0x42a4) & 0xfff);
3254 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
3255 if (!(r32 & 0x20000))
3256 t1_ns += 500;
3257
3258 t2_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f10) >> 8) & 0xfff);
3259 if ( read32(DEFAULT_MCHBAR + 0x5f00) & 8 )
3260 {
3261 t3_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f20) >> 8) & 0xfff);
3262 t3_ns += 10 * (read32(DEFAULT_MCHBAR + 0x5f18) & 0xff);
3263 }
3264 else
3265 {
3266 t3_ns = 500;
3267 }
3268 printk(BIOS_DEBUG, "t123: %d, %d, %d\n",
3269 t1_ns, t2_ns, t3_ns);
3270 write32 (DEFAULT_MCHBAR + 0x5d10,
3271 ((encode_5d10(t1_ns) + encode_5d10(t2_ns)) << 16)
3272 | (encode_5d10(t1_ns) << 8)
3273 | ((encode_5d10(t3_ns) + encode_5d10(t2_ns) + encode_5d10(t1_ns)) << 24)
3274 | (read32(DEFAULT_MCHBAR + 0x5d10) & 0xC0C0C0C0)
3275 | 0xc);
3276}
3277
3278void restore_timings(ramctr_timing * ctrl)
3279{
3280 int channel, slotrank, lane;
3281
3282 FOR_ALL_POPULATED_CHANNELS
3283 MCHBAR32(0x4004 + 0x400 * channel) =
3284 ctrl->tRRD
3285 | (ctrl->tRTP << 4)
3286 | (ctrl->tCKE << 8)
3287 | (ctrl->tWTR << 12)
3288 | (ctrl->tFAW << 16)
3289 | (ctrl->tWR << 24)
3290 | (ctrl->cmd_stretch[channel] << 30);
3291
3292 udelay(1);
3293
3294 FOR_ALL_POPULATED_CHANNELS {
3295 wait_428c(channel);
3296 }
3297
3298 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3299 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
3300 + 4 * lane, 0);
3301 }
3302
3303 FOR_ALL_POPULATED_CHANNELS
3304 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
3305 read32(DEFAULT_MCHBAR + 0x4008 +
3306 0x400 * channel) | 0x8000000);
3307
3308 FOR_ALL_POPULATED_CHANNELS {
3309 udelay (1);
3310 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
3311 read32(DEFAULT_MCHBAR + 0x4020 +
3312 0x400 * channel) | 0x200000);
3313 }
3314
3315 printram("CPE\n");
3316
3317 write32(DEFAULT_MCHBAR + 0x3400, 0);
3318 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3319
3320 printram("CP5b\n");
3321
3322 FOR_ALL_POPULATED_CHANNELS {
3323 program_timings(ctrl, channel);
3324 }
3325
3326 u32 reg, addr;
3327
3328 while (!(MCHBAR32(0x5084) & 0x10000));
3329 do {
3330 reg = MCHBAR32(0x428c);
3331 } while ((reg & 0x14) == 0);
3332
3333 // Set state of memory controller
3334 MCHBAR32(0x5030) = 0x116;
3335 MCHBAR32(0x4ea0) = 0;
3336
3337 // Wait 500us
3338 udelay(500);
3339
3340 FOR_ALL_CHANNELS {
3341 // Set valid rank CKE
3342 reg = 0;
3343 reg = (reg & ~0xf) | ctrl->rankmap[channel];
3344 addr = 0x400 * channel + 0x42a0;
3345 MCHBAR32(addr) = reg;
3346
3347 // Wait 10ns for ranks to settle
3348 //udelay(0.01);
3349
3350 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3351 MCHBAR32(addr) = reg;
3352
3353 // Write reset using a NOP
3354 write_reset(ctrl);
3355 }
3356
3357 /* mrs commands. */
3358 dram_mrscommands(ctrl);
3359
3360 printram("CP5c\n");
3361
3362 write32(DEFAULT_MCHBAR + 0x3000, 0);
3363
3364 FOR_ALL_CHANNELS {
3365 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3366 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3367 ~0x3f000000));
3368 udelay(2);
3369 }
3370
3371 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3372}