blob: 8964e3fcd254f5f1571293c6228bb79f35b5d92b [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 };
Patrick Rudolph74203de2017-11-20 11:57:01 +0100766 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100767
768 /* DLL Reset - self clearing - set after CLK frequency has been changed */
769 mr0reg = 0x100;
770
771 // Convert CAS to MCH register friendly
772 if (ctrl->CAS < 12) {
773 mch_cas = (u16) ((ctrl->CAS - 4) << 1);
774 } else {
775 mch_cas = (u16) (ctrl->CAS - 12);
776 mch_cas = ((mch_cas << 1) | 0x1);
777 }
778
779 // Convert tWR to MCH register friendly
780 mch_wr = mch_wr_t[ctrl->tWR - 5];
781
782 mr0reg = (mr0reg & ~0x4) | ((mch_cas & 0x1) << 2);
783 mr0reg = (mr0reg & ~0x70) | ((mch_cas & 0xe) << 3);
784 mr0reg = (mr0reg & ~0xe00) | (mch_wr << 9);
785
786 // Precharge PD - Fast (desktop) 0x1 or slow (mobile) 0x0 - mostly power-saving feature
Patrick Rudolph74203de2017-11-20 11:57:01 +0100787 mr0reg = (mr0reg & ~0x1000) | (!is_mobile << 12);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +0100788 return mr0reg;
789}
790
791static void dram_mr0(ramctr_timing *ctrl, u8 rank, int channel)
792{
793 write_mrreg(ctrl, channel, rank, 0,
794 make_mr0(ctrl, rank));
795}
796
797static u32 encode_odt(u32 odt)
798{
799 switch (odt) {
800 case 30:
801 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
802 case 60:
803 return (1 << 2); // RZQ/4
804 case 120:
805 return (1 << 6); // RZQ/2
806 default:
807 case 0:
808 return 0;
809 }
810}
811
812static u32 make_mr1(ramctr_timing *ctrl, u8 rank, int channel)
813{
814 odtmap odt;
815 u32 mr1reg;
816
817 odt = get_ODT(ctrl, rank, channel);
818 mr1reg = 0x2;
819
820 mr1reg |= encode_odt(odt.rttnom);
821
822 return mr1reg;
823}
824
825static void dram_mr1(ramctr_timing *ctrl, u8 rank, int channel)
826{
827 u16 mr1reg;
828
829 mr1reg = make_mr1(ctrl, rank, channel);
830
831 write_mrreg(ctrl, channel, rank, 1, mr1reg);
832}
833
834static void dram_mr2(ramctr_timing *ctrl, u8 rank, int channel)
835{
836 u16 pasr, cwl, mr2reg;
837 odtmap odt;
838 int srt;
839
840 pasr = 0;
841 cwl = ctrl->CWL - 5;
842 odt = get_ODT(ctrl, rank, channel);
843
844 srt = ctrl->extended_temperature_range && !ctrl->auto_self_refresh;
845
846 mr2reg = 0;
847 mr2reg = (mr2reg & ~0x7) | pasr;
848 mr2reg = (mr2reg & ~0x38) | (cwl << 3);
849 mr2reg = (mr2reg & ~0x40) | (ctrl->auto_self_refresh << 6);
850 mr2reg = (mr2reg & ~0x80) | (srt << 7);
851 mr2reg |= (odt.rttwr / 60) << 9;
852
853 write_mrreg(ctrl, channel, rank, 2, mr2reg);
854}
855
856static void dram_mr3(ramctr_timing *ctrl, u8 rank, int channel)
857{
858 write_mrreg(ctrl, channel, rank, 3, 0);
859}
860
861void dram_mrscommands(ramctr_timing * ctrl)
862{
863 u8 slotrank;
864 u32 reg, addr;
865 int channel;
866
867 FOR_ALL_POPULATED_CHANNELS {
868 FOR_ALL_POPULATED_RANKS {
869 // MR2
870 dram_mr2(ctrl, slotrank, channel);
871
872 // MR3
873 dram_mr3(ctrl, slotrank, channel);
874
875 // MR1
876 dram_mr1(ctrl, slotrank, channel);
877
878 // MR0
879 dram_mr0(ctrl, slotrank, channel);
880 }
881 }
882
883 /* DRAM command NOP */
884 write32(DEFAULT_MCHBAR + 0x4e20, 0x7);
885 write32(DEFAULT_MCHBAR + 0x4e30, 0xf1001);
886 write32(DEFAULT_MCHBAR + 0x4e00, 0x60002);
887 write32(DEFAULT_MCHBAR + 0x4e10, 0);
888
889 /* DRAM command ZQCL */
890 write32(DEFAULT_MCHBAR + 0x4e24, 0x1f003);
891 write32(DEFAULT_MCHBAR + 0x4e34, 0x1901001);
892 write32(DEFAULT_MCHBAR + 0x4e04, 0x60400);
893 write32(DEFAULT_MCHBAR + 0x4e14, 0x288);
894
895 /* execute command queue on all channels ? */
896 write32(DEFAULT_MCHBAR + 0x4e84, 0x40004);
897
898 // Drain
899 FOR_ALL_CHANNELS {
900 // Wait for ref drained
901 wait_428c(channel);
902 }
903
904 // Refresh enable
905 MCHBAR32(0x5030) |= 8;
906
907 FOR_ALL_POPULATED_CHANNELS {
908 addr = 0x400 * channel + 0x4020;
909 reg = MCHBAR32(addr);
910 reg &= ~0x200000;
911 MCHBAR32(addr) = reg;
912
913 wait_428c(channel);
914
915 slotrank = (ctrl->rankmap[channel] & 1) ? 0 : 2;
916
917 // Drain
918 wait_428c(channel);
919
920 /* DRAM command ZQCS */
921 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
922 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
923 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
924 (slotrank << 24) | 0x60000);
925 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
926 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x1);
927
928 // Drain
929 wait_428c(channel);
930 }
931}
932
933static const u32 lane_registers[] = {
934 0x0000, 0x0200, 0x0400, 0x0600,
935 0x1000, 0x1200, 0x1400, 0x1600,
936 0x0800
937};
938
939void program_timings(ramctr_timing * ctrl, int channel)
940{
941 u32 reg32, reg_4024, reg_c14, reg_c18, reg_4028;
942 int lane;
943 int slotrank, slot;
944 int full_shift = 0;
945 u16 slot320c[NUM_SLOTS];
946
947 FOR_ALL_POPULATED_RANKS {
948 if (full_shift < -ctrl->timings[channel][slotrank].val_320c)
949 full_shift = -ctrl->timings[channel][slotrank].val_320c;
950 }
951
952 for (slot = 0; slot < NUM_SLOTS; slot++)
953 switch ((ctrl->rankmap[channel] >> (2 * slot)) & 3) {
954 case 0:
955 default:
956 slot320c[slot] = 0x7f;
957 break;
958 case 1:
959 slot320c[slot] =
960 ctrl->timings[channel][2 * slot + 0].val_320c +
961 full_shift;
962 break;
963 case 2:
964 slot320c[slot] =
965 ctrl->timings[channel][2 * slot + 1].val_320c +
966 full_shift;
967 break;
968 case 3:
969 slot320c[slot] =
970 (ctrl->timings[channel][2 * slot].val_320c +
971 ctrl->timings[channel][2 * slot +
972 1].val_320c) / 2 +
973 full_shift;
974 break;
975 }
976
977 /* enable CMD XOVER */
978 reg32 = get_XOVER_CMD(ctrl->rankmap[channel]);
979 reg32 |= ((slot320c[0] & 0x3f) << 6) | ((slot320c[0] & 0x40) << 9);
980 reg32 |= (slot320c[1] & 0x7f) << 18;
981 reg32 |= (full_shift & 0x3f) | ((full_shift & 0x40) << 6);
982
983 MCHBAR32(0x320c + 0x100 * channel) = reg32;
984
985 /* enable CLK XOVER */
986 reg_c14 = get_XOVER_CLK(ctrl->rankmap[channel]);
987 reg_c18 = 0;
988
989 FOR_ALL_POPULATED_RANKS {
990 int shift =
991 ctrl->timings[channel][slotrank].val_320c + full_shift;
992 int offset_val_c14;
993 if (shift < 0)
994 shift = 0;
995 offset_val_c14 = ctrl->reg_c14_offset + shift;
996 /* set CLK phase shift */
997 reg_c14 |= (offset_val_c14 & 0x3f) << (6 * slotrank);
998 reg_c18 |= ((offset_val_c14 >> 6) & 1) << slotrank;
999 }
1000
1001 MCHBAR32(0xc14 + channel * 0x100) = reg_c14;
1002 MCHBAR32(0xc18 + channel * 0x100) = reg_c18;
1003
1004 reg_4028 = MCHBAR32(0x4028 + 0x400 * channel);
1005 reg_4028 &= 0xffff0000;
1006
1007 reg_4024 = 0;
1008
1009 FOR_ALL_POPULATED_RANKS {
1010 int post_timA_min_high = 7, post_timA_max_high = 0;
1011 int pre_timA_min_high = 7, pre_timA_max_high = 0;
1012 int shift_402x = 0;
1013 int shift =
1014 ctrl->timings[channel][slotrank].val_320c + full_shift;
1015
1016 if (shift < 0)
1017 shift = 0;
1018
1019 FOR_ALL_LANES {
Arthur Heymansabc504f2017-05-15 09:36:44 +02001020 post_timA_min_high = MIN(post_timA_min_high,
1021 (ctrl->timings[channel][slotrank].lanes[lane].
1022 timA + shift) >> 6);
1023 pre_timA_min_high = MIN(pre_timA_min_high,
1024 ctrl->timings[channel][slotrank].lanes[lane].
1025 timA >> 6);
1026 post_timA_max_high = MAX(post_timA_max_high,
1027 (ctrl->timings[channel][slotrank].lanes[lane].
1028 timA + shift) >> 6);
1029 pre_timA_max_high = MAX(pre_timA_max_high,
1030 ctrl->timings[channel][slotrank].lanes[lane].
1031 timA >> 6);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001032 }
1033
1034 if (pre_timA_max_high - pre_timA_min_high <
1035 post_timA_max_high - post_timA_min_high)
1036 shift_402x = +1;
1037 else if (pre_timA_max_high - pre_timA_min_high >
1038 post_timA_max_high - post_timA_min_high)
1039 shift_402x = -1;
1040
1041 reg_4028 |=
1042 (ctrl->timings[channel][slotrank].val_4028 + shift_402x -
1043 post_timA_min_high) << (4 * slotrank);
1044 reg_4024 |=
1045 (ctrl->timings[channel][slotrank].val_4024 +
1046 shift_402x) << (8 * slotrank);
1047
1048 FOR_ALL_LANES {
1049 MCHBAR32(lane_registers[lane] + 0x10 + 0x100 * channel +
1050 4 * slotrank)
1051 =
1052 (((ctrl->timings[channel][slotrank].lanes[lane].
1053 timA + shift) & 0x3f)
1054 |
1055 ((ctrl->timings[channel][slotrank].lanes[lane].
1056 rising + shift) << 8)
1057 |
1058 (((ctrl->timings[channel][slotrank].lanes[lane].
1059 timA + shift -
1060 (post_timA_min_high << 6)) & 0x1c0) << 10)
1061 | ((ctrl->timings[channel][slotrank].lanes[lane].
1062 falling + shift) << 20));
1063
1064 MCHBAR32(lane_registers[lane] + 0x20 + 0x100 * channel +
1065 4 * slotrank)
1066 =
1067 (((ctrl->timings[channel][slotrank].lanes[lane].
1068 timC + shift) & 0x3f)
1069 |
1070 (((ctrl->timings[channel][slotrank].lanes[lane].
1071 timB + shift) & 0x3f) << 8)
1072 |
1073 (((ctrl->timings[channel][slotrank].lanes[lane].
1074 timB + shift) & 0x1c0) << 9)
1075 |
1076 (((ctrl->timings[channel][slotrank].lanes[lane].
1077 timC + shift) & 0x40) << 13));
1078 }
1079 }
1080 MCHBAR32(0x4024 + 0x400 * channel) = reg_4024;
1081 MCHBAR32(0x4028 + 0x400 * channel) = reg_4028;
1082}
1083
1084static void test_timA(ramctr_timing * ctrl, int channel, int slotrank)
1085{
1086 wait_428c(channel);
1087
1088 /* DRAM command MRS
1089 * write MR3 MPR enable
1090 * in this mode only RD and RDA are allowed
1091 * all reads return a predefined pattern */
1092 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
1093 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1094 (0xc01 | (ctrl->tMOD << 16)));
1095 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1096 (slotrank << 24) | 0x360004);
1097 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1098
1099 /* DRAM command RD */
1100 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
1101 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x4040c01);
1102 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel, (slotrank << 24));
1103 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1104
1105 /* DRAM command RD */
1106 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
1107 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1108 0x100f | ((ctrl->CAS + 36) << 16));
1109 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1110 (slotrank << 24) | 0x60000);
1111 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1112
1113 /* DRAM command MRS
1114 * write MR3 MPR disable */
1115 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
1116 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1117 (0xc01 | (ctrl->tMOD << 16)));
1118 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1119 (slotrank << 24) | 0x360000);
1120 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1121
1122 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1123
1124 wait_428c(channel);
1125}
1126
1127static int does_lane_work(ramctr_timing * ctrl, int channel, int slotrank,
1128 int lane)
1129{
1130 u32 timA = ctrl->timings[channel][slotrank].lanes[lane].timA;
1131 return ((read32
1132 (DEFAULT_MCHBAR + lane_registers[lane] + channel * 0x100 + 4 +
1133 ((timA / 32) & 1) * 4)
1134 >> (timA % 32)) & 1);
1135}
1136
1137struct run {
1138 int middle;
1139 int end;
1140 int start;
1141 int all;
1142 int length;
1143};
1144
1145static struct run get_longest_zero_run(int *seq, int sz)
1146{
1147 int i, ls;
1148 int bl = 0, bs = 0;
1149 struct run ret;
1150
1151 ls = 0;
1152 for (i = 0; i < 2 * sz; i++)
1153 if (seq[i % sz]) {
1154 if (i - ls > bl) {
1155 bl = i - ls;
1156 bs = ls;
1157 }
1158 ls = i + 1;
1159 }
1160 if (bl == 0) {
1161 ret.middle = sz / 2;
1162 ret.start = 0;
1163 ret.end = sz;
1164 ret.all = 1;
1165 return ret;
1166 }
1167
1168 ret.start = bs % sz;
1169 ret.end = (bs + bl - 1) % sz;
1170 ret.middle = (bs + (bl - 1) / 2) % sz;
1171 ret.length = bl;
1172 ret.all = 0;
1173
1174 return ret;
1175}
1176
1177static void discover_timA_coarse(ramctr_timing * ctrl, int channel,
1178 int slotrank, int *upperA)
1179{
1180 int timA;
1181 int statistics[NUM_LANES][128];
1182 int lane;
1183
1184 for (timA = 0; timA < 128; timA++) {
1185 FOR_ALL_LANES {
1186 ctrl->timings[channel][slotrank].lanes[lane].timA = timA;
1187 }
1188 program_timings(ctrl, channel);
1189
1190 test_timA(ctrl, channel, slotrank);
1191
1192 FOR_ALL_LANES {
1193 statistics[lane][timA] =
1194 !does_lane_work(ctrl, channel, slotrank, lane);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001195 }
1196 }
1197 FOR_ALL_LANES {
1198 struct run rn = get_longest_zero_run(statistics[lane], 128);
1199 ctrl->timings[channel][slotrank].lanes[lane].timA = rn.middle;
1200 upperA[lane] = rn.end;
1201 if (upperA[lane] < rn.middle)
1202 upperA[lane] += 128;
Patrick Rudolph368b6152016-11-25 16:36:52 +01001203 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1204 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001205 }
1206}
1207
1208static void discover_timA_fine(ramctr_timing * ctrl, int channel, int slotrank,
1209 int *upperA)
1210{
1211 int timA_delta;
1212 int statistics[NUM_LANES][51];
1213 int lane, i;
1214
1215 memset(statistics, 0, sizeof(statistics));
1216
1217 for (timA_delta = -25; timA_delta <= 25; timA_delta++) {
1218 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
1219 timA = upperA[lane] + timA_delta + 0x40;
1220 program_timings(ctrl, channel);
1221
1222 for (i = 0; i < 100; i++) {
1223 test_timA(ctrl, channel, slotrank);
1224 FOR_ALL_LANES {
1225 statistics[lane][timA_delta + 25] +=
1226 does_lane_work(ctrl, channel, slotrank,
1227 lane);
1228 }
1229 }
1230 }
1231 FOR_ALL_LANES {
1232 int last_zero, first_all;
1233
1234 for (last_zero = -25; last_zero <= 25; last_zero++)
1235 if (statistics[lane][last_zero + 25])
1236 break;
1237 last_zero--;
1238 for (first_all = -25; first_all <= 25; first_all++)
1239 if (statistics[lane][first_all + 25] == 100)
1240 break;
1241
1242 printram("lane %d: %d, %d\n", lane, last_zero,
1243 first_all);
1244
1245 ctrl->timings[channel][slotrank].lanes[lane].timA =
1246 (last_zero + first_all) / 2 + upperA[lane];
1247 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
1248 lane, ctrl->timings[channel][slotrank].lanes[lane].timA);
1249 }
1250}
1251
1252static int discover_402x(ramctr_timing *ctrl, int channel, int slotrank,
1253 int *upperA)
1254{
1255 int works[NUM_LANES];
1256 int lane;
1257 while (1) {
1258 int all_works = 1, some_works = 0;
1259 program_timings(ctrl, channel);
1260 test_timA(ctrl, channel, slotrank);
1261 FOR_ALL_LANES {
1262 works[lane] =
1263 !does_lane_work(ctrl, channel, slotrank, lane);
1264 if (works[lane])
1265 some_works = 1;
1266 else
1267 all_works = 0;
1268 }
1269 if (all_works)
1270 return 0;
1271 if (!some_works) {
1272 if (ctrl->timings[channel][slotrank].val_4024 < 2) {
1273 printk(BIOS_EMERG, "402x discovery failed (1): %d, %d\n",
1274 channel, slotrank);
1275 return MAKE_ERR;
1276 }
1277 ctrl->timings[channel][slotrank].val_4024 -= 2;
1278 printram("4024 -= 2;\n");
1279 continue;
1280 }
1281 ctrl->timings[channel][slotrank].val_4028 += 2;
1282 printram("4028 += 2;\n");
1283 if (ctrl->timings[channel][slotrank].val_4028 >= 0x10) {
1284 printk(BIOS_EMERG, "402x discovery failed (2): %d, %d\n",
1285 channel, slotrank);
1286 return MAKE_ERR;
1287 }
1288 FOR_ALL_LANES if (works[lane]) {
1289 ctrl->timings[channel][slotrank].lanes[lane].timA +=
1290 128;
1291 upperA[lane] += 128;
1292 printram("increment %d, %d, %d\n", channel,
1293 slotrank, lane);
1294 }
1295 }
1296 return 0;
1297}
1298
1299struct timA_minmax {
1300 int timA_min_high, timA_max_high;
1301};
1302
1303static void pre_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
1304 struct timA_minmax *mnmx)
1305{
1306 int lane;
1307 mnmx->timA_min_high = 7;
1308 mnmx->timA_max_high = 0;
1309
1310 FOR_ALL_LANES {
1311 if (mnmx->timA_min_high >
1312 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1313 mnmx->timA_min_high =
1314 (ctrl->timings[channel][slotrank].lanes[lane].
1315 timA >> 6);
1316 if (mnmx->timA_max_high <
1317 (ctrl->timings[channel][slotrank].lanes[lane].timA >> 6))
1318 mnmx->timA_max_high =
1319 (ctrl->timings[channel][slotrank].lanes[lane].
1320 timA >> 6);
1321 }
1322}
1323
1324static void post_timA_change(ramctr_timing * ctrl, int channel, int slotrank,
1325 struct timA_minmax *mnmx)
1326{
1327 struct timA_minmax post;
1328 int shift_402x = 0;
1329
1330 /* Get changed maxima. */
1331 pre_timA_change(ctrl, channel, slotrank, &post);
1332
1333 if (mnmx->timA_max_high - mnmx->timA_min_high <
1334 post.timA_max_high - post.timA_min_high)
1335 shift_402x = +1;
1336 else if (mnmx->timA_max_high - mnmx->timA_min_high >
1337 post.timA_max_high - post.timA_min_high)
1338 shift_402x = -1;
1339 else
1340 shift_402x = 0;
1341
1342 ctrl->timings[channel][slotrank].val_4028 += shift_402x;
1343 ctrl->timings[channel][slotrank].val_4024 += shift_402x;
1344 printram("4024 += %d;\n", shift_402x);
1345 printram("4028 += %d;\n", shift_402x);
1346}
1347
1348/* Compensate the skew between DQS and DQs.
1349 * To ease PCB design a small skew between Data Strobe signals and
1350 * Data Signals is allowed.
1351 * The controller has to measure and compensate this skew for every byte-lane.
1352 * By delaying either all DQs signals or DQS signal, a full phase
1353 * shift can be introduced.
1354 * It is assumed that one byte-lane's DQs signals have the same routing delay.
1355 *
1356 * To measure the actual skew, the DRAM is placed in "read leveling" mode.
1357 * In read leveling mode the DRAM-chip outputs an alternating periodic pattern.
1358 * The memory controller iterates over all possible values to do a full phase shift
1359 * and issues read commands.
1360 * With DQS and DQs in phase the data read is expected to alternate on every byte:
1361 * 0xFF 0x00 0xFF ...
1362 * Once the controller has detected this pattern a bit in the result register is
1363 * set for the current phase shift.
1364 */
1365int read_training(ramctr_timing * ctrl)
1366{
1367 int channel, slotrank, lane;
1368 int err;
1369
1370 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
1371 int all_high, some_high;
1372 int upperA[NUM_LANES];
1373 struct timA_minmax mnmx;
1374
1375 wait_428c(channel);
1376
1377 /* DRAM command PREA */
1378 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1379 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1380 0xc01 | (ctrl->tRP << 16));
1381 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1382 (slotrank << 24) | 0x60400);
1383 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1384 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
1385
1386 write32(DEFAULT_MCHBAR + 0x3400, (slotrank << 2) | 0x8001);
1387
1388 ctrl->timings[channel][slotrank].val_4028 = 4;
1389 ctrl->timings[channel][slotrank].val_4024 = 55;
1390 program_timings(ctrl, channel);
1391
1392 discover_timA_coarse(ctrl, channel, slotrank, upperA);
1393
1394 all_high = 1;
1395 some_high = 0;
1396 FOR_ALL_LANES {
1397 if (ctrl->timings[channel][slotrank].lanes[lane].
1398 timA >= 0x40)
1399 some_high = 1;
1400 else
1401 all_high = 0;
1402 }
1403
1404 if (all_high) {
1405 ctrl->timings[channel][slotrank].val_4028--;
1406 printram("4028--;\n");
1407 FOR_ALL_LANES {
1408 ctrl->timings[channel][slotrank].lanes[lane].
1409 timA -= 0x40;
1410 upperA[lane] -= 0x40;
1411
1412 }
1413 } else if (some_high) {
1414 ctrl->timings[channel][slotrank].val_4024++;
1415 ctrl->timings[channel][slotrank].val_4028++;
1416 printram("4024++;\n");
1417 printram("4028++;\n");
1418 }
1419
1420 program_timings(ctrl, channel);
1421
1422 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1423
1424 err = discover_402x(ctrl, channel, slotrank, upperA);
1425 if (err)
1426 return err;
1427
1428 post_timA_change(ctrl, channel, slotrank, &mnmx);
1429 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1430
1431 discover_timA_fine(ctrl, channel, slotrank, upperA);
1432
1433 post_timA_change(ctrl, channel, slotrank, &mnmx);
1434 pre_timA_change(ctrl, channel, slotrank, &mnmx);
1435
1436 FOR_ALL_LANES {
1437 ctrl->timings[channel][slotrank].lanes[lane].timA -= mnmx.timA_min_high * 0x40;
1438 }
1439 ctrl->timings[channel][slotrank].val_4028 -= mnmx.timA_min_high;
1440 printram("4028 -= %d;\n", mnmx.timA_min_high);
1441
1442 post_timA_change(ctrl, channel, slotrank, &mnmx);
1443
1444 printram("4/8: %d, %d, %x, %x\n", channel, slotrank,
1445 ctrl->timings[channel][slotrank].val_4024,
1446 ctrl->timings[channel][slotrank].val_4028);
1447
1448 printram("final results:\n");
1449 FOR_ALL_LANES
1450 printram("Aval: %d, %d, %d: %x\n", channel, slotrank,
1451 lane,
1452 ctrl->timings[channel][slotrank].lanes[lane].timA);
1453
1454 write32(DEFAULT_MCHBAR + 0x3400, 0);
1455
1456 toggle_io_reset();
1457 }
1458
1459 FOR_ALL_POPULATED_CHANNELS {
1460 program_timings(ctrl, channel);
1461 }
1462 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
1463 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
1464 + 4 * lane, 0);
1465 }
1466 return 0;
1467}
1468
1469static void test_timC(ramctr_timing * ctrl, int channel, int slotrank)
1470{
1471 int lane;
1472
1473 FOR_ALL_LANES {
1474 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel + 4 * lane, 0);
1475 read32(DEFAULT_MCHBAR + 0x4140 + 0x400 * channel + 4 * lane);
1476 }
1477
1478 wait_428c(channel);
1479
1480 /* DRAM command ACT */
1481 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
1482 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1483 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD) << 10)
1484 | 4 | (ctrl->tRCD << 16));
1485
1486 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1487 (slotrank << 24) | (6 << 16));
1488
1489 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
1490
1491 /* DRAM command NOP */
1492 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
1493 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8041001);
1494 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1495 (slotrank << 24) | 8);
1496 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
1497
1498 /* DRAM command WR */
1499 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
1500 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x80411f4);
1501 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
1502 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
1503
1504 /* DRAM command NOP */
1505 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
1506 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1507 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
1508 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1509 (slotrank << 24) | 8);
1510 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
1511
1512 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1513
1514 wait_428c(channel);
1515
1516 /* DRAM command PREA */
1517 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1518 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1519 0xc01 | (ctrl->tRP << 16));
1520 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1521 (slotrank << 24) | 0x60400);
1522 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
1523
1524 /* DRAM command ACT */
1525 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
1526 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1527 (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) << 10)
1528 | 8 | (ctrl->CAS << 16));
1529
1530 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1531 (slotrank << 24) | 0x60000);
1532
1533 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x244);
1534
1535 /* DRAM command RD */
1536 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
1537 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1538 0x40011f4 | (max(ctrl->tRTP, 8) << 16));
1539 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel, (slotrank << 24));
1540 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x242);
1541
1542 /* DRAM command PREA */
1543 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
1544 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1545 0xc01 | (ctrl->tRP << 16));
1546 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1547 (slotrank << 24) | 0x60400);
1548 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
1549 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1550 wait_428c(channel);
1551}
1552
1553static int discover_timC(ramctr_timing *ctrl, int channel, int slotrank)
1554{
1555 int timC;
1556 int statistics[NUM_LANES][MAX_TIMC + 1];
1557 int lane;
1558
1559 wait_428c(channel);
1560
1561 /* DRAM command PREA */
1562 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1563 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1564 0xc01 | (ctrl->tRP << 16));
1565 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1566 (slotrank << 24) | 0x60400);
1567 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
1568 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
1569
1570 for (timC = 0; timC <= MAX_TIMC; timC++) {
1571 FOR_ALL_LANES ctrl->timings[channel][slotrank].lanes[lane].
1572 timC = timC;
1573 program_timings(ctrl, channel);
1574
1575 test_timC(ctrl, channel, slotrank);
1576
1577 FOR_ALL_LANES {
1578 statistics[lane][timC] =
1579 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
1580 0x400 * channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001581 }
1582 }
1583 FOR_ALL_LANES {
1584 struct run rn =
1585 get_longest_zero_run(statistics[lane], MAX_TIMC + 1);
1586 ctrl->timings[channel][slotrank].lanes[lane].timC = rn.middle;
1587 if (rn.all) {
1588 printk(BIOS_EMERG, "timC discovery failed: %d, %d, %d\n",
1589 channel, slotrank, lane);
1590 return MAKE_ERR;
1591 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001592 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1593 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001594 }
1595 return 0;
1596}
1597
1598static int get_precedening_channels(ramctr_timing * ctrl, int target_channel)
1599{
1600 int channel, ret = 0;
1601 FOR_ALL_POPULATED_CHANNELS if (channel < target_channel)
1602 ret++;
1603 return ret;
1604}
1605
1606static void fill_pattern0(ramctr_timing * ctrl, int channel, u32 a, u32 b)
1607{
1608 unsigned j;
1609 unsigned channel_offset =
1610 get_precedening_channels(ctrl, channel) * 0x40;
1611 for (j = 0; j < 16; j++)
1612 write32((void *)(0x04000000 + channel_offset + 4 * j), j & 2 ? b : a);
1613 sfence();
1614}
1615
1616static int num_of_channels(const ramctr_timing * ctrl)
1617{
1618 int ret = 0;
1619 int channel;
1620 FOR_ALL_POPULATED_CHANNELS ret++;
1621 return ret;
1622}
1623
1624static void fill_pattern1(ramctr_timing * ctrl, int channel)
1625{
1626 unsigned j;
1627 unsigned channel_offset =
1628 get_precedening_channels(ctrl, channel) * 0x40;
1629 unsigned channel_step = 0x40 * num_of_channels(ctrl);
1630 for (j = 0; j < 16; j++)
1631 write32((void *)(0x04000000 + channel_offset + j * 4), 0xffffffff);
1632 for (j = 0; j < 16; j++)
1633 write32((void *)(0x04000000 + channel_offset + channel_step + j * 4), 0);
1634 sfence();
1635}
1636
1637static void precharge(ramctr_timing * ctrl)
1638{
1639 int channel, slotrank, lane;
1640
1641 FOR_ALL_POPULATED_CHANNELS {
1642 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
1643 ctrl->timings[channel][slotrank].lanes[lane].falling =
1644 16;
1645 ctrl->timings[channel][slotrank].lanes[lane].rising =
1646 16;
1647 }
1648
1649 program_timings(ctrl, channel);
1650
1651 FOR_ALL_POPULATED_RANKS {
1652 wait_428c(channel);
1653
1654 /* DRAM command MRS
1655 * write MR3 MPR enable
1656 * in this mode only RD and RDA are allowed
1657 * all reads return a predefined pattern */
1658 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
1659 0x1f000);
1660 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1661 0xc01 | (ctrl->tMOD << 16));
1662 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1663 (slotrank << 24) | 0x360004);
1664 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1665
1666 /* DRAM command RD */
1667 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
1668 0x1f105);
1669 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1670 0x4041003);
1671 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1672 (slotrank << 24) | 0);
1673 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1674
1675 /* DRAM command RD */
1676 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
1677 0x1f105);
1678 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1679 0x1001 | ((ctrl->CAS + 8) << 16));
1680 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1681 (slotrank << 24) | 0x60000);
1682 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1683
1684 /* DRAM command MRS
1685 * write MR3 MPR disable */
1686 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
1687 0x1f000);
1688 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1689 0xc01 | (ctrl->tMOD << 16));
1690 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1691 (slotrank << 24) | 0x360000);
1692 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1693 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
1694 0xc0001);
1695
1696 wait_428c(channel);
1697 }
1698
1699 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
1700 ctrl->timings[channel][slotrank].lanes[lane].falling =
1701 48;
1702 ctrl->timings[channel][slotrank].lanes[lane].rising =
1703 48;
1704 }
1705
1706 program_timings(ctrl, channel);
1707
1708 FOR_ALL_POPULATED_RANKS {
1709 wait_428c(channel);
1710 /* DRAM command MRS
1711 * write MR3 MPR enable
1712 * in this mode only RD and RDA are allowed
1713 * all reads return a predefined pattern */
1714 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
1715 0x1f000);
1716 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1717 0xc01 | (ctrl->tMOD << 16));
1718 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1719 (slotrank << 24) | 0x360004);
1720 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1721
1722 /* DRAM command RD */
1723 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
1724 0x1f105);
1725 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1726 0x4041003);
1727 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1728 (slotrank << 24) | 0);
1729 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1730
1731 /* DRAM command RD */
1732 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
1733 0x1f105);
1734 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1735 0x1001 | ((ctrl->CAS + 8) << 16));
1736 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1737 (slotrank << 24) | 0x60000);
1738 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1739
1740 /* DRAM command MRS
1741 * write MR3 MPR disable */
1742 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
1743 0x1f000);
1744 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1745 0xc01 | (ctrl->tMOD << 16));
1746
1747 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1748 (slotrank << 24) | 0x360000);
1749 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
1750
1751 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
1752 0xc0001);
1753 wait_428c(channel);
1754 }
1755 }
1756}
1757
1758static void test_timB(ramctr_timing * ctrl, int channel, int slotrank)
1759{
1760 /* enable DQs on this slotrank */
1761 write_mrreg(ctrl, channel, slotrank, 1,
1762 0x80 | make_mr1(ctrl, slotrank, channel));
1763
1764 wait_428c(channel);
1765 /* DRAM command NOP */
1766 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f207);
1767 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1768 0x8000c01 | ((ctrl->CWL + ctrl->tWLO) << 16));
1769 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1770 8 | (slotrank << 24));
1771 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1772
1773 /* DRAM command NOP */
1774 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f107);
1775 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1776 0x4000c01 | ((ctrl->CAS + 38) << 16));
1777 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1778 (slotrank << 24) | 4);
1779 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1780
1781 write32(DEFAULT_MCHBAR + 0x400 * channel + 0x4284, 0x40001);
1782 wait_428c(channel);
1783
1784 /* disable DQs on this slotrank */
1785 write_mrreg(ctrl, channel, slotrank, 1,
1786 0x1080 | make_mr1(ctrl, slotrank, channel));
1787}
1788
1789static int discover_timB(ramctr_timing *ctrl, int channel, int slotrank)
1790{
1791 int timB;
1792 int statistics[NUM_LANES][128];
1793 int lane;
1794
1795 write32(DEFAULT_MCHBAR + 0x3400, 0x108052 | (slotrank << 2));
1796
1797 for (timB = 0; timB < 128; timB++) {
1798 FOR_ALL_LANES {
1799 ctrl->timings[channel][slotrank].lanes[lane].timB = timB;
1800 }
1801 program_timings(ctrl, channel);
1802
1803 test_timB(ctrl, channel, slotrank);
1804
1805 FOR_ALL_LANES {
1806 statistics[lane][timB] =
1807 !((read32
1808 (DEFAULT_MCHBAR + lane_registers[lane] +
1809 channel * 0x100 + 4 + ((timB / 32) & 1) * 4)
1810 >> (timB % 32)) & 1);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001811 }
1812 }
1813 FOR_ALL_LANES {
1814 struct run rn = get_longest_zero_run(statistics[lane], 128);
1815 /* timC is a direct function of timB's 6 LSBs.
1816 * Some tests increments the value of timB by a small value,
1817 * which might cause the 6bit value to overflow, if it's close
1818 * to 0x3F. Increment the value by a small offset if it's likely
1819 * to overflow, to make sure it won't overflow while running
1820 * tests and bricks the system due to a non matching timC.
1821 *
1822 * TODO: find out why some tests (edge write discovery)
1823 * increment timB. */
1824 if ((rn.start & 0x3F) == 0x3E)
1825 rn.start += 2;
1826 else if ((rn.start & 0x3F) == 0x3F)
1827 rn.start += 1;
1828 ctrl->timings[channel][slotrank].lanes[lane].timB = rn.start;
1829 if (rn.all) {
1830 printk(BIOS_EMERG, "timB discovery failed: %d, %d, %d\n",
1831 channel, slotrank, lane);
1832 return MAKE_ERR;
1833 }
Patrick Rudolph368b6152016-11-25 16:36:52 +01001834 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1835 channel, slotrank, lane, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01001836 }
1837 return 0;
1838}
1839
1840static int get_timB_high_adjust(u64 val)
1841{
1842 int i;
1843
1844 /* good */
1845 if (val == 0xffffffffffffffffLL)
1846 return 0;
1847
1848 if (val >= 0xf000000000000000LL) {
1849 /* needs negative adjustment */
1850 for (i = 0; i < 8; i++)
1851 if (val << (8 * (7 - i) + 4))
1852 return -i;
1853 } else {
1854 /* needs positive adjustment */
1855 for (i = 0; i < 8; i++)
1856 if (val >> (8 * (7 - i) + 4))
1857 return i;
1858 }
1859 return 8;
1860}
1861
1862static void adjust_high_timB(ramctr_timing * ctrl)
1863{
1864 int channel, slotrank, lane, old;
1865 write32(DEFAULT_MCHBAR + 0x3400, 0x200);
1866 FOR_ALL_POPULATED_CHANNELS {
1867 fill_pattern1(ctrl, channel);
1868 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 1);
1869 }
1870 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS {
1871
1872 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x10001);
1873
1874 wait_428c(channel);
1875
1876 /* DRAM command ACT */
1877 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
1878 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1879 0xc01 | (ctrl->tRCD << 16));
1880 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1881 (slotrank << 24) | 0x60000);
1882 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
1883
1884 /* DRAM command NOP */
1885 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f207);
1886 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x8040c01);
1887 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1888 (slotrank << 24) | 0x8);
1889 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x3e0);
1890
1891 /* DRAM command WR */
1892 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f201);
1893 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel, 0x8041003);
1894 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1895 (slotrank << 24));
1896 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x3e2);
1897
1898 /* DRAM command NOP */
1899 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f207);
1900 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
1901 0x8000c01 | ((ctrl->CWL + ctrl->tWTR + 5) << 16));
1902 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
1903 (slotrank << 24) | 0x8);
1904 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x3e0);
1905
1906 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
1907
1908 wait_428c(channel);
1909
1910 /* DRAM command PREA */
1911 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f002);
1912 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
1913 0xc01 | ((ctrl->tRP) << 16));
1914 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1915 (slotrank << 24) | 0x60400);
1916 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x240);
1917
1918 /* DRAM command ACT */
1919 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f006);
1920 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
1921 0xc01 | ((ctrl->tRCD) << 16));
1922 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
1923 (slotrank << 24) | 0x60000);
1924 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
1925
1926 /* DRAM command RD */
1927 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x3f105);
1928 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
1929 0x4000c01 |
1930 ((ctrl->tRP +
1931 ctrl->timings[channel][slotrank].val_4024 +
1932 ctrl->timings[channel][slotrank].val_4028) << 16));
1933 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
1934 (slotrank << 24) | 0x60008);
1935 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
1936
1937 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0x80001);
1938 wait_428c(channel);
1939 FOR_ALL_LANES {
1940 u64 res =
1941 read32(DEFAULT_MCHBAR + lane_registers[lane] +
1942 0x100 * channel + 4);
1943 res |=
1944 ((u64) read32(DEFAULT_MCHBAR + lane_registers[lane] +
1945 0x100 * channel + 8)) << 32;
1946 old = ctrl->timings[channel][slotrank].lanes[lane].timB;
1947 ctrl->timings[channel][slotrank].lanes[lane].timB +=
1948 get_timB_high_adjust(res) * 64;
1949
1950 printram("High adjust %d:%016llx\n", lane, res);
1951 printram("Bval+: %d, %d, %d, %x -> %x\n", channel,
1952 slotrank, lane, old,
1953 ctrl->timings[channel][slotrank].lanes[lane].
1954 timB);
1955 }
1956 }
1957 write32(DEFAULT_MCHBAR + 0x3400, 0);
1958}
1959
1960static void write_op(ramctr_timing * ctrl, int channel)
1961{
1962 int slotrank;
1963
1964 wait_428c(channel);
1965
1966 /* choose an existing rank. */
1967 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
1968
1969 /* DRAM command ACT */
1970 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
1971 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
1972
1973 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
1974 (slotrank << 24) | 0x60000);
1975
1976 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
1977
1978 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
1979 wait_428c(channel);
1980}
1981
1982/* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
1983 * DDR3 adopted the fly-by topology. The data and strobes signals reach
1984 * the chips at different times with respect to command, address and
1985 * clock signals.
1986 * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase
1987 * shift can be introduced.
1988 * It is assumed that the CLK/ADDR/CMD signals have the same routing delay.
1989 *
1990 * To find the required phase shift the DRAM is placed in "write leveling" mode.
1991 * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the
1992 * sampled value on the data lanes (DQs).
1993 */
1994int write_training(ramctr_timing * ctrl)
1995{
1996 int channel, slotrank, lane;
1997 int err;
1998
1999 FOR_ALL_POPULATED_CHANNELS
2000 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
2001 read32(DEFAULT_MCHBAR + 0x4008 +
2002 0x400 * channel) | 0x8000000);
2003
2004 FOR_ALL_POPULATED_CHANNELS {
2005 write_op(ctrl, channel);
2006 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2007 read32(DEFAULT_MCHBAR + 0x4020 +
2008 0x400 * channel) | 0x200000);
2009 }
2010
2011 /* refresh disable */
2012 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2013 FOR_ALL_POPULATED_CHANNELS {
2014 write_op(ctrl, channel);
2015 }
2016
2017 /* enable write leveling on all ranks
2018 * disable all DQ outputs
2019 * only NOP is allowed in this mode */
2020 FOR_ALL_CHANNELS
2021 FOR_ALL_POPULATED_RANKS
2022 write_mrreg(ctrl, channel, slotrank, 1,
2023 make_mr1(ctrl, slotrank, channel) | 0x1080);
2024
2025 write32(DEFAULT_MCHBAR + 0x3400, 0x108052);
2026
2027 toggle_io_reset();
2028
2029 /* set any valid value for timB, it gets corrected later */
2030 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2031 err = discover_timB(ctrl, channel, slotrank);
2032 if (err)
2033 return err;
2034 }
2035
2036 /* disable write leveling on all ranks */
2037 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2038 write_mrreg(ctrl, channel,
2039 slotrank, 1, make_mr1(ctrl, slotrank, channel));
2040
2041 write32(DEFAULT_MCHBAR + 0x3400, 0);
2042
2043 FOR_ALL_POPULATED_CHANNELS
2044 wait_428c(channel);
2045
2046 /* refresh enable */
2047 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) | 8);
2048
2049 FOR_ALL_POPULATED_CHANNELS {
2050 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2051 ~0x00200000 & read32(DEFAULT_MCHBAR + 0x4020 +
2052 0x400 * channel));
2053 read32(DEFAULT_MCHBAR + 0x428c + 0x400 * channel);
2054 wait_428c(channel);
2055
2056 /* DRAM command ZQCS */
2057 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2058 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x659001);
2059 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel, 0x60000);
2060 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2061
2062 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2063 wait_428c(channel);
2064 }
2065
2066 toggle_io_reset();
2067
2068 printram("CPE\n");
2069 precharge(ctrl);
2070 printram("CPF\n");
2071
2072 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2073 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2074 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2075 0);
2076 }
2077
2078 FOR_ALL_POPULATED_CHANNELS {
2079 fill_pattern0(ctrl, channel, 0xaaaaaaaa, 0x55555555);
2080 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2081 }
2082
2083 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2084 err = discover_timC(ctrl, channel, slotrank);
2085 if (err)
2086 return err;
2087 }
2088
2089 FOR_ALL_POPULATED_CHANNELS
2090 program_timings(ctrl, channel);
2091
2092 /* measure and adjust timB timings */
2093 adjust_high_timB(ctrl);
2094
2095 FOR_ALL_POPULATED_CHANNELS
2096 program_timings(ctrl, channel);
2097
2098 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2099 read32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane);
2100 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2101 0);
2102 }
2103 return 0;
2104}
2105
2106static int test_320c(ramctr_timing * ctrl, int channel, int slotrank)
2107{
2108 struct ram_rank_timings saved_rt = ctrl->timings[channel][slotrank];
2109 int timC_delta;
2110 int lanes_ok = 0;
2111 int ctr = 0;
2112 int lane;
2113
2114 for (timC_delta = -5; timC_delta <= 5; timC_delta++) {
2115 FOR_ALL_LANES {
2116 ctrl->timings[channel][slotrank].lanes[lane].timC =
2117 saved_rt.lanes[lane].timC + timC_delta;
2118 }
2119 program_timings(ctrl, channel);
2120 FOR_ALL_LANES {
2121 write32(DEFAULT_MCHBAR + 4 * lane + 0x4f40, 0);
2122 }
2123
2124 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2125
2126 wait_428c(channel);
2127 /* DRAM command ACT */
2128 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2129 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2130 ((max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1)) << 10)
2131 | 8 | (ctrl->tRCD << 16));
2132
2133 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2134 (slotrank << 24) | ctr | 0x60000);
2135
2136 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
2137 /* DRAM command WR */
2138 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2139 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2140 0x8001020 | ((ctrl->CWL + ctrl->tWTR + 8) << 16));
2141 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2142 (slotrank << 24));
2143 write32(DEFAULT_MCHBAR + 0x4244 + 0x400 * channel, 0x389abcd);
2144 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0x20e42);
2145
2146 /* DRAM command RD */
2147 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2148 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2149 0x4001020 | (max(ctrl->tRTP, 8) << 16));
2150 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2151 (slotrank << 24));
2152 write32(DEFAULT_MCHBAR + 0x4248 + 0x400 * channel, 0x389abcd);
2153 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0x20e42);
2154
2155 /* DRAM command PRE */
2156 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f002);
2157 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel, 0xf1001);
2158 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2159 (slotrank << 24) | 0x60400);
2160 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0x240);
2161
2162 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2163 wait_428c(channel);
2164 FOR_ALL_LANES {
2165 u32 r32 =
2166 read32(DEFAULT_MCHBAR + 0x4340 + 4 * lane +
2167 0x400 * channel);
2168
2169 if (r32 == 0)
2170 lanes_ok |= 1 << lane;
2171 }
2172 ctr++;
2173 if (lanes_ok == ((1 << NUM_LANES) - 1))
2174 break;
2175 }
2176
2177 ctrl->timings[channel][slotrank] = saved_rt;
2178
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002179 return lanes_ok != ((1 << NUM_LANES) - 1);
2180}
2181
2182#include "raminit_patterns.h"
2183
2184static void fill_pattern5(ramctr_timing * ctrl, int channel, int patno)
2185{
2186 unsigned i, j;
2187 unsigned channel_offset =
2188 get_precedening_channels(ctrl, channel) * 0x40;
2189 unsigned channel_step = 0x40 * num_of_channels(ctrl);
2190
2191 if (patno) {
2192 u8 base8 = 0x80 >> ((patno - 1) % 8);
2193 u32 base = base8 | (base8 << 8) | (base8 << 16) | (base8 << 24);
2194 for (i = 0; i < 32; i++) {
2195 for (j = 0; j < 16; j++) {
2196 u32 val = use_base[patno - 1][i] & (1 << (j / 2)) ? base : 0;
2197 if (invert[patno - 1][i] & (1 << (j / 2)))
2198 val = ~val;
2199 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2200 j * 4), val);
2201 }
2202 }
2203
2204 } else {
2205 for (i = 0; i < sizeof(pattern) / sizeof(pattern[0]); i++) {
2206 for (j = 0; j < 16; j++)
2207 write32((void *)(0x04000000 + channel_offset + i * channel_step +
2208 j * 4), pattern[i][j]);
2209 }
2210 sfence();
2211 }
2212}
2213
2214static void reprogram_320c(ramctr_timing * ctrl)
2215{
2216 int channel, slotrank;
2217
2218 FOR_ALL_POPULATED_CHANNELS {
2219 wait_428c(channel);
2220
2221 /* choose an existing rank. */
2222 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2223
2224 /* DRAM command ZQCS */
2225 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2226 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2227
2228 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2229 (slotrank << 24) | 0x60000);
2230
2231 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2232
2233 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2234 wait_428c(channel);
2235 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
2236 read32(DEFAULT_MCHBAR + 0x4020 +
2237 0x400 * channel) | 0x200000);
2238 }
2239
2240 /* refresh disable */
2241 write32(DEFAULT_MCHBAR + 0x5030, read32(DEFAULT_MCHBAR + 0x5030) & ~8);
2242 FOR_ALL_POPULATED_CHANNELS {
2243 wait_428c(channel);
2244
2245 /* choose an existing rank. */
2246 slotrank = !(ctrl->rankmap[channel] & 1) ? 2 : 0;
2247
2248 /* DRAM command ZQCS */
2249 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x0f003);
2250 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel, 0x41001);
2251
2252 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2253 (slotrank << 24) | 0x60000);
2254
2255 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x3e0);
2256
2257 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 1);
2258 wait_428c(channel);
2259 }
2260
2261 /* jedec reset */
2262 dram_jedecreset(ctrl);
2263 /* mrs commands. */
2264 dram_mrscommands(ctrl);
2265
2266 toggle_io_reset();
2267}
2268
2269#define MIN_C320C_LEN 13
2270
2271static int try_cmd_stretch(ramctr_timing *ctrl, int channel, int cmd_stretch)
2272{
2273 struct ram_rank_timings saved_timings[NUM_CHANNELS][NUM_SLOTRANKS];
2274 int slotrank;
2275 int c320c;
2276 int stat[NUM_SLOTRANKS][256];
2277 int delta = 0;
2278
2279 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch, channel);
2280
2281 FOR_ALL_POPULATED_RANKS {
2282 saved_timings[channel][slotrank] =
2283 ctrl->timings[channel][slotrank];
2284 }
2285
2286 ctrl->cmd_stretch[channel] = cmd_stretch;
2287
2288 MCHBAR32(0x4004 + 0x400 * channel) =
2289 ctrl->tRRD
2290 | (ctrl->tRTP << 4)
2291 | (ctrl->tCKE << 8)
2292 | (ctrl->tWTR << 12)
2293 | (ctrl->tFAW << 16)
2294 | (ctrl->tWR << 24)
2295 | (ctrl->cmd_stretch[channel] << 30);
2296
2297 if (ctrl->cmd_stretch[channel] == 2)
2298 delta = 2;
2299 else if (ctrl->cmd_stretch[channel] == 0)
2300 delta = 4;
2301
2302 FOR_ALL_POPULATED_RANKS {
2303 ctrl->timings[channel][slotrank].val_4024 -= delta;
2304 }
2305
2306 for (c320c = -127; c320c <= 127; c320c++) {
2307 FOR_ALL_POPULATED_RANKS {
2308 ctrl->timings[channel][slotrank].val_320c = c320c;
2309 }
2310 program_timings(ctrl, channel);
2311 reprogram_320c(ctrl);
2312 FOR_ALL_POPULATED_RANKS {
2313 stat[slotrank][c320c + 127] =
2314 test_320c(ctrl, channel, slotrank);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002315 }
2316 }
2317 FOR_ALL_POPULATED_RANKS {
2318 struct run rn =
2319 get_longest_zero_run(stat[slotrank], 255);
2320 ctrl->timings[channel][slotrank].val_320c =
2321 rn.middle - 127;
Patrick Rudolph368b6152016-11-25 16:36:52 +01002322 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2323 channel, slotrank, rn.start, rn.middle, rn.end);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002324 if (rn.all || rn.length < MIN_C320C_LEN) {
2325 FOR_ALL_POPULATED_RANKS {
2326 ctrl->timings[channel][slotrank] =
2327 saved_timings[channel][slotrank];
2328 }
2329 return MAKE_ERR;
2330 }
2331 }
2332
2333 return 0;
2334}
2335
2336/* Adjust CMD phase shift and try multiple command rates.
2337 * A command rate of 2T doubles the time needed for address and
2338 * command decode. */
2339int command_training(ramctr_timing *ctrl)
2340{
2341 int channel;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002342
2343 FOR_ALL_POPULATED_CHANNELS {
2344 fill_pattern5(ctrl, channel, 0);
2345 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2346 }
2347
2348 FOR_ALL_POPULATED_CHANNELS {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002349 int cmdrate, err;
2350
2351 /*
2352 * Dual DIMM per channel:
2353 * Issue: While c320c discovery seems to succeed raminit
2354 * will fail in write training.
2355 * Workaround: Skip 1T in dual DIMM mode, that's only
2356 * supported by a few DIMMs.
Dan Elkoubydabebc32018-04-13 18:47:10 +03002357 * Only try 1T mode for XMP DIMMs that request it in dual DIMM
2358 * mode.
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002359 *
2360 * Single DIMM per channel:
2361 * Try command rate 1T and 2T
2362 */
2363 cmdrate = ((ctrl->rankmap[channel] & 0x5) == 0x5);
Dan Elkoubydabebc32018-04-13 18:47:10 +03002364 if (ctrl->tCMD)
2365 /* XMP gives the CMD rate in clock ticks, not ns */
2366 cmdrate = MIN(DIV_ROUND_UP(ctrl->tCMD, 256) - 1, 1);
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002367
Elyes HAOUASadda3f812018-01-31 23:02:35 +01002368 for (; cmdrate < 2; cmdrate++) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002369 err = try_cmd_stretch(ctrl, channel, cmdrate << 1);
2370
2371 if (!err)
2372 break;
2373 }
2374
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002375 if (err) {
Patrick Rudolph58d16af2017-06-19 19:33:12 +02002376 printk(BIOS_EMERG, "c320c discovery failed\n");
2377 return err;
2378 }
2379
2380 printram("Using CMD rate %uT on channel %u\n",
2381 cmdrate + 1, channel);
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01002382 }
2383
2384 FOR_ALL_POPULATED_CHANNELS
2385 program_timings(ctrl, channel);
2386
2387 reprogram_320c(ctrl);
2388 return 0;
2389}
2390
2391
2392static int discover_edges_real(ramctr_timing *ctrl, int channel, int slotrank,
2393 int *edges)
2394{
2395 int edge;
2396 int statistics[NUM_LANES][MAX_EDGE_TIMING + 1];
2397 int lane;
2398
2399 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2400 FOR_ALL_LANES {
2401 ctrl->timings[channel][slotrank].lanes[lane].rising =
2402 edge;
2403 ctrl->timings[channel][slotrank].lanes[lane].falling =
2404 edge;
2405 }
2406 program_timings(ctrl, channel);
2407
2408 FOR_ALL_LANES {
2409 write32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2410 4 * lane, 0);
2411 read32(DEFAULT_MCHBAR + 0x400 * channel + 4 * lane +
2412 0x4140);
2413 }
2414
2415 wait_428c(channel);
2416 /* DRAM command MRS
2417 * write MR3 MPR enable
2418 * in this mode only RD and RDA are allowed
2419 * all reads return a predefined pattern */
2420 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f000);
2421 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2422 (0xc01 | (ctrl->tMOD << 16)));
2423 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2424 (slotrank << 24) | 0x360004);
2425 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2426
2427 /* DRAM command RD */
2428 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f105);
2429 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel, 0x40411f4);
2430 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2431 (slotrank << 24));
2432 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2433
2434 /* DRAM command RD */
2435 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel, 0x1f105);
2436 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2437 0x1001 | ((ctrl->CAS + 8) << 16));
2438 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2439 (slotrank << 24) | 0x60000);
2440 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2441
2442 /* DRAM command MRS
2443 * MR3 disable MPR */
2444 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel, 0x1f000);
2445 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2446 (0xc01 | (ctrl->tMOD << 16)));
2447 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2448 (slotrank << 24) | 0x360000);
2449 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2450
2451 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel, 0xc0001);
2452
2453 wait_428c(channel);
2454
2455 FOR_ALL_LANES {
2456 statistics[lane][edge] =
2457 read32(DEFAULT_MCHBAR + 0x4340 + 0x400 * channel +
2458 lane * 4);
2459 }
2460 }
2461 FOR_ALL_LANES {
2462 struct run rn =
2463 get_longest_zero_run(statistics[lane], MAX_EDGE_TIMING + 1);
2464 edges[lane] = rn.middle;
2465 if (rn.all) {
2466 printk(BIOS_EMERG, "edge discovery failed: %d, %d, %d\n",
2467 channel, slotrank, lane);
2468 return MAKE_ERR;
2469 }
2470 printram("eval %d, %d, %d: %02x\n", channel, slotrank,
2471 lane, edges[lane]);
2472 }
2473 return 0;
2474}
2475
2476int discover_edges(ramctr_timing *ctrl)
2477{
2478 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2479 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2480 int channel, slotrank, lane;
2481 int err;
2482
2483 write32(DEFAULT_MCHBAR + 0x3400, 0);
2484
2485 toggle_io_reset();
2486
2487 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES {
2488 write32(DEFAULT_MCHBAR + 4 * lane +
2489 0x400 * channel + 0x4080, 0);
2490 }
2491
2492 FOR_ALL_POPULATED_CHANNELS {
2493 fill_pattern0(ctrl, channel, 0, 0);
2494 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2495 FOR_ALL_LANES {
2496 read32(DEFAULT_MCHBAR + 0x400 * channel +
2497 lane * 4 + 0x4140);
2498 }
2499
2500 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2501 ctrl->timings[channel][slotrank].lanes[lane].falling =
2502 16;
2503 ctrl->timings[channel][slotrank].lanes[lane].rising =
2504 16;
2505 }
2506
2507 program_timings(ctrl, channel);
2508
2509 FOR_ALL_POPULATED_RANKS {
2510 wait_428c(channel);
2511
2512 /* DRAM command MRS
2513 * MR3 enable MPR
2514 * write MR3 MPR enable
2515 * in this mode only RD and RDA are allowed
2516 * all reads return a predefined pattern */
2517 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2518 0x1f000);
2519 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2520 0xc01 | (ctrl->tMOD << 16));
2521 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2522 (slotrank << 24) | 0x360004);
2523 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2524
2525 /* DRAM command RD */
2526 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2527 0x1f105);
2528 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2529 0x4041003);
2530 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2531 (slotrank << 24) | 0);
2532 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2533
2534 /* DRAM command RD */
2535 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2536 0x1f105);
2537 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2538 0x1001 | ((ctrl->CAS + 8) << 16));
2539 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2540 (slotrank << 24) | 0x60000);
2541 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2542
2543 /* DRAM command MRS
2544 * MR3 disable MPR */
2545 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2546 0x1f000);
2547 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2548 0xc01 | (ctrl->tMOD << 16));
2549 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2550 (slotrank << 24) | 0x360000);
2551 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2552 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2553 0xc0001);
2554
2555 wait_428c(channel);
2556 }
2557
2558 /* XXX: check any measured value ? */
2559
2560 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2561 ctrl->timings[channel][slotrank].lanes[lane].falling =
2562 48;
2563 ctrl->timings[channel][slotrank].lanes[lane].rising =
2564 48;
2565 }
2566
2567 program_timings(ctrl, channel);
2568
2569 FOR_ALL_POPULATED_RANKS {
2570 wait_428c(channel);
2571
2572 /* DRAM command MRS
2573 * MR3 enable MPR
2574 * write MR3 MPR enable
2575 * in this mode only RD and RDA are allowed
2576 * all reads return a predefined pattern */
2577 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2578 0x1f000);
2579 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2580 0xc01 | (ctrl->tMOD << 16));
2581 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2582 (slotrank << 24) | 0x360004);
2583 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0);
2584
2585 /* DRAM command RD */
2586 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2587 0x1f105);
2588 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2589 0x4041003);
2590 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2591 (slotrank << 24) | 0);
2592 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel, 0);
2593
2594 /* DRAM command RD */
2595 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2596 0x1f105);
2597 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2598 0x1001 | ((ctrl->CAS + 8) << 16));
2599 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2600 (slotrank << 24) | 0x60000);
2601 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel, 0);
2602
2603 /* DRAM command MRS
2604 * MR3 disable MPR */
2605 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2606 0x1f000);
2607 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2608 0xc01 | (ctrl->tMOD << 16));
2609 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2610 (slotrank << 24) | 0x360000);
2611 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2612
2613 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2614 0xc0001);
2615 wait_428c(channel);
2616 }
2617
2618 /* XXX: check any measured value ? */
2619
2620 FOR_ALL_LANES {
2621 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel +
2622 lane * 4,
2623 ~read32(DEFAULT_MCHBAR + 0x4040 +
2624 0x400 * channel + lane * 4) & 0xff);
2625 }
2626
2627 fill_pattern0(ctrl, channel, 0, 0xffffffff);
2628 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
2629 }
2630
2631 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
2632 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
2633 printram("discover falling edges:\n[%x] = %x\n", 0x4eb0, 0x300);
2634
2635 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2636 err = discover_edges_real(ctrl, channel, slotrank,
2637 falling_edges[channel][slotrank]);
2638 if (err)
2639 return err;
2640 }
2641
2642 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
2643 printram("discover rising edges:\n[%x] = %x\n", 0x4eb0, 0x200);
2644
2645 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2646 err = discover_edges_real(ctrl, channel, slotrank,
2647 rising_edges[channel][slotrank]);
2648 if (err)
2649 return err;
2650 }
2651
2652 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
2653
2654 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2655 ctrl->timings[channel][slotrank].lanes[lane].falling =
2656 falling_edges[channel][slotrank][lane];
2657 ctrl->timings[channel][slotrank].lanes[lane].rising =
2658 rising_edges[channel][slotrank][lane];
2659 }
2660
2661 FOR_ALL_POPULATED_CHANNELS {
2662 program_timings(ctrl, channel);
2663 }
2664
2665 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2666 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2667 0);
2668 }
2669 return 0;
2670}
2671
2672static int discover_edges_write_real(ramctr_timing *ctrl, int channel,
2673 int slotrank, int *edges)
2674{
2675 int edge;
2676 u32 raw_statistics[MAX_EDGE_TIMING + 1];
2677 int statistics[MAX_EDGE_TIMING + 1];
2678 const int reg3000b24[] = { 0, 0xc, 0x2c };
2679 int lane, i;
2680 int lower[NUM_LANES];
2681 int upper[NUM_LANES];
2682 int pat;
2683
2684 FOR_ALL_LANES {
2685 lower[lane] = 0;
2686 upper[lane] = MAX_EDGE_TIMING;
2687 }
2688
2689 for (i = 0; i < 3; i++) {
2690 write32(DEFAULT_MCHBAR + 0x3000 + 0x100 * channel,
2691 reg3000b24[i] << 24);
2692 printram("[%x] = 0x%08x\n",
2693 0x3000 + 0x100 * channel, reg3000b24[i] << 24);
2694 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2695 fill_pattern5(ctrl, channel, pat);
2696 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2697 printram("using pattern %d\n", pat);
2698 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++) {
2699 FOR_ALL_LANES {
2700 ctrl->timings[channel][slotrank].lanes[lane].
2701 rising = edge;
2702 ctrl->timings[channel][slotrank].lanes[lane].
2703 falling = edge;
2704 }
2705 program_timings(ctrl, channel);
2706
2707 FOR_ALL_LANES {
2708 write32(DEFAULT_MCHBAR + 0x4340 +
2709 0x400 * channel + 4 * lane, 0);
2710 read32(DEFAULT_MCHBAR + 0x400 * channel +
2711 4 * lane + 0x4140);
2712 }
2713 wait_428c(channel);
2714
2715 /* DRAM command ACT */
2716 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel,
2717 0x1f006);
2718 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2719 0x4 | (ctrl->tRCD << 16)
2720 | (max(ctrl->tRRD, (ctrl->tFAW >> 2) + 1) <<
2721 10));
2722 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2723 (slotrank << 24) | 0x60000);
2724 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel,
2725 0x240);
2726
2727 /* DRAM command WR */
2728 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel,
2729 0x1f201);
2730 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2731 0x8005020 | ((ctrl->tWTR + ctrl->CWL + 8) <<
2732 16));
2733 write32(DEFAULT_MCHBAR + 0x4204 + 0x400 * channel,
2734 (slotrank << 24));
2735 write32(DEFAULT_MCHBAR + 0x4214 + 0x400 * channel,
2736 0x242);
2737
2738 /* DRAM command RD */
2739 write32(DEFAULT_MCHBAR + 0x4228 + 0x400 * channel,
2740 0x1f105);
2741 write32(DEFAULT_MCHBAR + 0x4238 + 0x400 * channel,
2742 0x4005020 | (max(ctrl->tRTP, 8) << 16));
2743 write32(DEFAULT_MCHBAR + 0x4208 + 0x400 * channel,
2744 (slotrank << 24));
2745 write32(DEFAULT_MCHBAR + 0x4218 + 0x400 * channel,
2746 0x242);
2747
2748 /* DRAM command PRE */
2749 write32(DEFAULT_MCHBAR + 0x422c + 0x400 * channel,
2750 0x1f002);
2751 write32(DEFAULT_MCHBAR + 0x423c + 0x400 * channel,
2752 0xc01 | (ctrl->tRP << 16));
2753 write32(DEFAULT_MCHBAR + 0x420c + 0x400 * channel,
2754 (slotrank << 24) | 0x60400);
2755 write32(DEFAULT_MCHBAR + 0x421c + 0x400 * channel, 0);
2756
2757 write32(DEFAULT_MCHBAR + 0x4284 + 0x400 * channel,
2758 0xc0001);
2759 wait_428c(channel);
2760 FOR_ALL_LANES {
2761 read32(DEFAULT_MCHBAR + 0x4340 +
2762 0x400 * channel + lane * 4);
2763 }
2764
2765 raw_statistics[edge] =
2766 MCHBAR32(0x436c + 0x400 * channel);
2767 }
2768 FOR_ALL_LANES {
2769 struct run rn;
2770 for (edge = 0; edge <= MAX_EDGE_TIMING; edge++)
2771 statistics[edge] =
2772 ! !(raw_statistics[edge] & (1 << lane));
2773 rn = get_longest_zero_run(statistics,
2774 MAX_EDGE_TIMING + 1);
2775 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
2776 channel, slotrank, i, rn.start, rn.middle,
2777 rn.end, rn.start + ctrl->edge_offset[i],
2778 rn.end - ctrl->edge_offset[i]);
2779 lower[lane] =
2780 max(rn.start + ctrl->edge_offset[i], lower[lane]);
2781 upper[lane] =
2782 min(rn.end - ctrl->edge_offset[i], upper[lane]);
2783 edges[lane] = (lower[lane] + upper[lane]) / 2;
2784 if (rn.all || (lower[lane] > upper[lane])) {
2785 printk(BIOS_EMERG, "edge write discovery failed: %d, %d, %d\n",
2786 channel, slotrank, lane);
2787 return MAKE_ERR;
2788 }
2789 }
2790 }
2791 }
2792
2793 write32(DEFAULT_MCHBAR + 0x3000, 0);
2794 printram("CPA\n");
2795 return 0;
2796}
2797
2798int discover_edges_write(ramctr_timing *ctrl)
2799{
2800 int falling_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2801 int rising_edges[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2802 int channel, slotrank, lane;
2803 int err;
2804
2805 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
2806 write32(DEFAULT_MCHBAR + 0x4eb0, 0x300);
2807 printram("discover falling edges write:\n[%x] = %x\n", 0x4eb0, 0x300);
2808
2809 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2810 err = discover_edges_write_real(ctrl, channel, slotrank,
2811 falling_edges[channel][slotrank]);
2812 if (err)
2813 return err;
2814 }
2815
2816 write32(DEFAULT_MCHBAR + 0x4eb0, 0x200);
2817 printram("discover rising edges write:\n[%x] = %x\n", 0x4eb0, 0x200);
2818
2819 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
2820 err = discover_edges_write_real(ctrl, channel, slotrank,
2821 rising_edges[channel][slotrank]);
2822 if (err)
2823 return err;
2824 }
2825
2826 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
2827
2828 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2829 ctrl->timings[channel][slotrank].lanes[lane].falling =
2830 falling_edges[channel][slotrank][lane];
2831 ctrl->timings[channel][slotrank].lanes[lane].rising =
2832 rising_edges[channel][slotrank][lane];
2833 }
2834
2835 FOR_ALL_POPULATED_CHANNELS
2836 program_timings(ctrl, channel);
2837
2838 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2839 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel + 4 * lane,
2840 0);
2841 }
2842 return 0;
2843}
2844
2845static void test_timC_write(ramctr_timing *ctrl, int channel, int slotrank)
2846{
2847 wait_428c(channel);
2848 /* DRAM command ACT */
2849 write32(DEFAULT_MCHBAR + 0x4220 + 0x400 * channel, 0x1f006);
2850 write32(DEFAULT_MCHBAR + 0x4230 + 0x400 * channel,
2851 (max((ctrl->tFAW >> 2) + 1, ctrl->tRRD)
2852 << 10) | (ctrl->tRCD << 16) | 4);
2853 write32(DEFAULT_MCHBAR + 0x4200 + 0x400 * channel,
2854 (slotrank << 24) | 0x60000);
2855 write32(DEFAULT_MCHBAR + 0x4210 + 0x400 * channel, 0x244);
2856
2857 /* DRAM command WR */
2858 write32(DEFAULT_MCHBAR + 0x4224 + 0x400 * channel, 0x1f201);
2859 write32(DEFAULT_MCHBAR + 0x4234 + 0x400 * channel,
2860 0x80011e0 |
2861 ((ctrl->tWTR + ctrl->CWL + 8) << 16));
2862 write32(DEFAULT_MCHBAR + 0x4204 +
2863 0x400 * channel, (slotrank << 24));
2864 write32(DEFAULT_MCHBAR + 0x4214 +
2865 0x400 * channel, 0x242);
2866
2867 /* DRAM command RD */
2868 write32(DEFAULT_MCHBAR + 0x4228 +
2869 0x400 * channel, 0x1f105);
2870 write32(DEFAULT_MCHBAR + 0x4238 +
2871 0x400 * channel,
2872 0x40011e0 | (max(ctrl->tRTP, 8) << 16));
2873 write32(DEFAULT_MCHBAR + 0x4208 +
2874 0x400 * channel, (slotrank << 24));
2875 write32(DEFAULT_MCHBAR + 0x4218 +
2876 0x400 * channel, 0x242);
2877
2878 /* DRAM command PRE */
2879 write32(DEFAULT_MCHBAR + 0x422c +
2880 0x400 * channel, 0x1f002);
2881 write32(DEFAULT_MCHBAR + 0x423c +
2882 0x400 * channel,
2883 0x1001 | (ctrl->tRP << 16));
2884 write32(DEFAULT_MCHBAR + 0x420c +
2885 0x400 * channel,
2886 (slotrank << 24) | 0x60400);
2887 write32(DEFAULT_MCHBAR + 0x421c +
2888 0x400 * channel, 0);
2889
2890 write32(DEFAULT_MCHBAR + 0x4284 +
2891 0x400 * channel, 0xc0001);
2892 wait_428c(channel);
2893}
2894
2895int discover_timC_write(ramctr_timing *ctrl)
2896{
2897 const u8 rege3c_b24[3] = { 0, 0xf, 0x2f };
2898 int i, pat;
2899
2900 int lower[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2901 int upper[NUM_CHANNELS][NUM_SLOTRANKS][NUM_LANES];
2902 int channel, slotrank, lane;
2903
2904 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2905 lower[channel][slotrank][lane] = 0;
2906 upper[channel][slotrank][lane] = MAX_TIMC;
2907 }
2908
2909 write32(DEFAULT_MCHBAR + 0x4ea8, 1);
2910 printram("discover timC write:\n");
2911
2912 for (i = 0; i < 3; i++)
2913 FOR_ALL_POPULATED_CHANNELS {
2914 write32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100),
2915 (rege3c_b24[i] << 24)
2916 | (read32(DEFAULT_MCHBAR + 0xe3c + (channel * 0x100))
2917 & ~0x3f000000));
2918 udelay(2);
2919 for (pat = 0; pat < NUM_PATTERNS; pat++) {
2920 FOR_ALL_POPULATED_RANKS {
2921 int timC;
2922 u32 raw_statistics[MAX_TIMC + 1];
2923 int statistics[MAX_TIMC + 1];
2924
2925 /* Make sure rn.start < rn.end */
2926 statistics[MAX_TIMC] = 1;
2927
2928 fill_pattern5(ctrl, channel, pat);
2929 write32(DEFAULT_MCHBAR + 0x4288 + 0x400 * channel, 0x1f);
2930 for (timC = 0; timC < MAX_TIMC; timC++) {
2931 FOR_ALL_LANES
2932 ctrl->timings[channel][slotrank].lanes[lane].timC = timC;
2933 program_timings(ctrl, channel);
2934
2935 test_timC_write (ctrl, channel, slotrank);
2936
2937 raw_statistics[timC] =
2938 MCHBAR32(0x436c + 0x400 * channel);
2939 }
2940 FOR_ALL_LANES {
2941 struct run rn;
2942 for (timC = 0; timC < MAX_TIMC; timC++)
2943 statistics[timC] =
2944 !!(raw_statistics[timC] &
2945 (1 << lane));
2946
2947 rn = get_longest_zero_run(statistics,
2948 MAX_TIMC + 1);
2949 if (rn.all) {
2950 printk(BIOS_EMERG, "timC write discovery failed: %d, %d, %d\n",
2951 channel, slotrank, lane);
2952 return MAKE_ERR;
2953 }
2954 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
2955 channel, slotrank, i, rn.start,
2956 rn.middle, rn.end,
2957 rn.start + ctrl->timC_offset[i],
2958 rn.end - ctrl->timC_offset[i]);
2959 lower[channel][slotrank][lane] =
2960 max(rn.start + ctrl->timC_offset[i],
2961 lower[channel][slotrank][lane]);
2962 upper[channel][slotrank][lane] =
2963 min(rn.end - ctrl->timC_offset[i],
2964 upper[channel][slotrank][lane]);
2965
2966 }
2967 }
2968 }
2969 }
2970
2971 FOR_ALL_CHANNELS {
2972 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
2973 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
2974 ~0x3f000000));
2975 udelay(2);
2976 }
2977
2978 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
2979
2980 printram("CPB\n");
2981
2982 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
2983 printram("timC %d, %d, %d: %x\n", channel,
2984 slotrank, lane,
2985 (lower[channel][slotrank][lane] +
2986 upper[channel][slotrank][lane]) / 2);
2987 ctrl->timings[channel][slotrank].lanes[lane].timC =
2988 (lower[channel][slotrank][lane] +
2989 upper[channel][slotrank][lane]) / 2;
2990 }
2991 FOR_ALL_POPULATED_CHANNELS {
2992 program_timings(ctrl, channel);
2993 }
2994 return 0;
2995}
2996
2997void normalize_training(ramctr_timing * ctrl)
2998{
2999 int channel, slotrank, lane;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01003000 int mat;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003001
3002 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3003 int delta;
Patrick Rudolph3c8cb972016-11-25 16:00:01 +01003004 mat = 0;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003005 FOR_ALL_LANES mat =
3006 max(ctrl->timings[channel][slotrank].lanes[lane].timA, mat);
Patrick Rudolph413edc82016-11-25 15:40:07 +01003007 printram("normalize %d, %d, %d: mat %d\n",
3008 channel, slotrank, lane, mat);
3009
3010 delta = (mat >> 6) - ctrl->timings[channel][slotrank].val_4028;
3011 printram("normalize %d, %d, %d: delta %d\n",
3012 channel, slotrank, lane, delta);
3013
3014 ctrl->timings[channel][slotrank].val_4024 += delta;
3015 ctrl->timings[channel][slotrank].val_4028 += delta;
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003016 }
3017
3018 FOR_ALL_POPULATED_CHANNELS {
3019 program_timings(ctrl, channel);
3020 }
3021}
3022
3023void write_controller_mr(ramctr_timing * ctrl)
3024{
3025 int channel, slotrank;
3026
3027 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS {
3028 write32(DEFAULT_MCHBAR + 0x0004 + (channel << 8) +
3029 lane_registers[slotrank], make_mr0(ctrl, slotrank));
3030 write32(DEFAULT_MCHBAR + 0x0008 + (channel << 8) +
3031 lane_registers[slotrank],
3032 make_mr1(ctrl, slotrank, channel));
3033 }
3034}
3035
3036int channel_test(ramctr_timing *ctrl)
3037{
3038 int channel, slotrank, lane;
3039
3040 slotrank = 0;
3041 FOR_ALL_POPULATED_CHANNELS
3042 if (read32(DEFAULT_MCHBAR + 0x42a0 + (channel << 10)) & 0xa000) {
3043 printk(BIOS_EMERG, "Mini channel test failed (1): %d\n",
3044 channel);
3045 return MAKE_ERR;
3046 }
3047 FOR_ALL_POPULATED_CHANNELS {
3048 fill_pattern0(ctrl, channel, 0x12345678, 0x98765432);
3049
3050 write32(DEFAULT_MCHBAR + 0x4288 + (channel << 10), 0);
3051 }
3052
3053 for (slotrank = 0; slotrank < 4; slotrank++)
3054 FOR_ALL_CHANNELS
3055 if (ctrl->rankmap[channel] & (1 << slotrank)) {
3056 FOR_ALL_LANES {
3057 write32(DEFAULT_MCHBAR + (0x4f40 + 4 * lane), 0);
3058 write32(DEFAULT_MCHBAR + (0x4d40 + 4 * lane), 0);
3059 }
3060 wait_428c(channel);
3061 /* DRAM command ACT */
3062 write32(DEFAULT_MCHBAR + 0x4220 + (channel << 10), 0x0001f006);
3063 write32(DEFAULT_MCHBAR + 0x4230 + (channel << 10), 0x0028a004);
3064 write32(DEFAULT_MCHBAR + 0x4200 + (channel << 10),
3065 0x00060000 | (slotrank << 24));
3066 write32(DEFAULT_MCHBAR + 0x4210 + (channel << 10), 0x00000244);
3067 /* DRAM command WR */
3068 write32(DEFAULT_MCHBAR + 0x4224 + (channel << 10), 0x0001f201);
3069 write32(DEFAULT_MCHBAR + 0x4234 + (channel << 10), 0x08281064);
3070 write32(DEFAULT_MCHBAR + 0x4204 + (channel << 10),
3071 0x00000000 | (slotrank << 24));
3072 write32(DEFAULT_MCHBAR + 0x4214 + (channel << 10), 0x00000242);
3073 /* DRAM command RD */
3074 write32(DEFAULT_MCHBAR + 0x4228 + (channel << 10), 0x0001f105);
3075 write32(DEFAULT_MCHBAR + 0x4238 + (channel << 10), 0x04281064);
3076 write32(DEFAULT_MCHBAR + 0x4208 + (channel << 10),
3077 0x00000000 | (slotrank << 24));
3078 write32(DEFAULT_MCHBAR + 0x4218 + (channel << 10), 0x00000242);
3079 /* DRAM command PRE */
3080 write32(DEFAULT_MCHBAR + 0x422c + (channel << 10), 0x0001f002);
3081 write32(DEFAULT_MCHBAR + 0x423c + (channel << 10), 0x00280c01);
3082 write32(DEFAULT_MCHBAR + 0x420c + (channel << 10),
3083 0x00060400 | (slotrank << 24));
3084 write32(DEFAULT_MCHBAR + 0x421c + (channel << 10), 0x00000240);
3085 write32(DEFAULT_MCHBAR + 0x4284 + (channel << 10), 0x000c0001);
3086 wait_428c(channel);
3087 FOR_ALL_LANES
3088 if (read32(DEFAULT_MCHBAR + 0x4340 + (channel << 10) + 4 * lane)) {
3089 printk(BIOS_EMERG, "Mini channel test failed (2): %d, %d, %d\n",
3090 channel, slotrank, lane);
3091 return MAKE_ERR;
3092 }
3093 }
3094 return 0;
3095}
3096
3097void set_scrambling_seed(ramctr_timing * ctrl)
3098{
3099 int channel;
3100
3101 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them?
3102 I don't think so. */
3103 static u32 seeds[NUM_CHANNELS][3] = {
3104 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
3105 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
3106 };
3107 FOR_ALL_POPULATED_CHANNELS {
3108 MCHBAR32(0x4020 + 0x400 * channel) &= ~0x10000000;
Arthur Heymans6af8aab2017-09-26 23:18:14 +02003109 MCHBAR32(0x4034 + 0x400 * channel) = seeds[channel][0];
3110 MCHBAR32(0x403c + 0x400 * channel) = seeds[channel][1];
3111 MCHBAR32(0x4038 + 0x400 * channel) = seeds[channel][2];
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003112 }
3113}
3114
3115void set_4f8c(void)
3116{
3117 struct cpuid_result cpures;
3118 u32 cpu;
3119
3120 cpures = cpuid(1);
3121 cpu = (cpures.eax);
3122 if (IS_SANDY_CPU(cpu) && (IS_SANDY_CPU_D0(cpu) || IS_SANDY_CPU_D1(cpu))) {
3123 MCHBAR32(0x4f8c) = 0x141D1519;
3124 } else {
3125 MCHBAR32(0x4f8c) = 0x551D1519;
3126 }
3127}
3128
3129void prepare_training(ramctr_timing * ctrl)
3130{
3131 int channel;
3132
3133 FOR_ALL_POPULATED_CHANNELS {
3134 // Always drive command bus
3135 MCHBAR32(0x4004 + 0x400 * channel) |= 0x20000000;
3136 }
3137
3138 udelay(1);
3139
3140 FOR_ALL_POPULATED_CHANNELS {
3141 wait_428c(channel);
3142 }
3143}
3144
3145void set_4008c(ramctr_timing * ctrl)
3146{
3147 int channel, slotrank;
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01003148
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003149 FOR_ALL_POPULATED_CHANNELS {
3150 u32 b20, b4_8_12;
3151 int min_320c = 10000;
3152 int max_320c = -10000;
3153
3154 FOR_ALL_POPULATED_RANKS {
3155 max_320c = max(ctrl->timings[channel][slotrank].val_320c, max_320c);
3156 min_320c = min(ctrl->timings[channel][slotrank].val_320c, min_320c);
3157 }
3158
3159 if (max_320c - min_320c > 51)
3160 b20 = 0;
3161 else
3162 b20 = ctrl->ref_card_offset[channel];
3163
3164 if (ctrl->reg_320c_range_threshold < max_320c - min_320c)
3165 b4_8_12 = 0x3330;
3166 else
3167 b4_8_12 = 0x2220;
3168
Patrick Rudolph19c3dad2016-11-26 11:37:45 +01003169 dram_odt_stretch(ctrl, channel);
3170
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003171 write32(DEFAULT_MCHBAR + 0x4008 + (channel << 10),
3172 0x0a000000
3173 | (b20 << 20)
3174 | ((ctrl->ref_card_offset[channel] + 2) << 16)
3175 | b4_8_12);
3176 }
3177}
3178
3179void set_42a0(ramctr_timing * ctrl)
3180{
3181 int channel;
3182 FOR_ALL_POPULATED_CHANNELS {
3183 write32(DEFAULT_MCHBAR + (0x42a0 + 0x400 * channel),
3184 0x00001000 | ctrl->rankmap[channel]);
3185 MCHBAR32(0x4004 + 0x400 * channel) &= ~0x20000000; // OK
3186 }
3187}
3188
3189static int encode_5d10(int ns)
3190{
3191 return (ns + 499) / 500;
3192}
3193
3194/* FIXME: values in this function should be hardware revision-dependent. */
3195void final_registers(ramctr_timing * ctrl)
3196{
Patrick Rudolph74203de2017-11-20 11:57:01 +01003197 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
3198
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003199 int channel;
3200 int t1_cycles = 0, t1_ns = 0, t2_ns;
3201 int t3_ns;
3202 u32 r32;
3203
3204 write32(DEFAULT_MCHBAR + 0x4cd4, 0x00000046);
3205
3206 write32(DEFAULT_MCHBAR + 0x400c, (read32(DEFAULT_MCHBAR + 0x400c) & 0xFFFFCFFF) | 0x1000); // OK
3207 write32(DEFAULT_MCHBAR + 0x440c, (read32(DEFAULT_MCHBAR + 0x440c) & 0xFFFFCFFF) | 0x1000); // OK
Patrick Rudolph652c4912017-10-31 11:36:55 +01003208
Patrick Rudolph74203de2017-11-20 11:57:01 +01003209 if (is_mobile)
Patrick Rudolph652c4912017-10-31 11:36:55 +01003210 /* APD - DLL Off, 64 DCLKs until idle, decision per rank */
3211 MCHBAR32(PM_PDWN_Config) = 0x00000740;
3212 else
3213 /* APD - PPD, 64 DCLKs until idle, decision per rank */
3214 MCHBAR32(PM_PDWN_Config) = 0x00000340;
3215
Patrick Rudolphfd5fa2a2016-11-11 18:22:33 +01003216 write32(DEFAULT_MCHBAR + 0x4380, 0x00000aaa); // OK
3217 write32(DEFAULT_MCHBAR + 0x4780, 0x00000aaa); // OK
3218 write32(DEFAULT_MCHBAR + 0x4f88, 0x5f7003ff); // OK
3219 write32(DEFAULT_MCHBAR + 0x5064, 0x00073000 | ctrl->reg_5064b0); // OK
3220
3221 FOR_ALL_CHANNELS {
3222 switch (ctrl->rankmap[channel]) {
3223 /* Unpopulated channel. */
3224 case 0:
3225 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0);
3226 break;
3227 /* Only single-ranked dimms. */
3228 case 1:
3229 case 4:
3230 case 5:
3231 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x373131);
3232 break;
3233 /* Dual-ranked dimms present. */
3234 default:
3235 write32(DEFAULT_MCHBAR + 0x4384 + channel * 0x400, 0x9b6ea1);
3236 break;
3237 }
3238 }
3239
3240 write32 (DEFAULT_MCHBAR + 0x5880, 0xca9171e5);
3241 write32 (DEFAULT_MCHBAR + 0x5888,
3242 (read32 (DEFAULT_MCHBAR + 0x5888) & ~0xffffff) | 0xe4d5d0);
3243 write32 (DEFAULT_MCHBAR + 0x58a8, read32 (DEFAULT_MCHBAR + 0x58a8) & ~0x1f);
3244 write32 (DEFAULT_MCHBAR + 0x4294,
3245 (read32 (DEFAULT_MCHBAR + 0x4294) & ~0x30000)
3246 | (1 << 16));
3247 write32 (DEFAULT_MCHBAR + 0x4694,
3248 (read32 (DEFAULT_MCHBAR + 0x4694) & ~0x30000)
3249 | (1 << 16));
3250
3251 MCHBAR32(0x5030) |= 1; // OK
3252 MCHBAR32(0x5030) |= 0x80; // OK
3253 MCHBAR32(0x5f18) = 0xfa; // OK
3254
3255 /* Find a populated channel. */
3256 FOR_ALL_POPULATED_CHANNELS
3257 break;
3258
3259 t1_cycles = ((read32(DEFAULT_MCHBAR + 0x4290 + channel * 0x400) >> 8) & 0xff);
3260 r32 = read32(DEFAULT_MCHBAR + 0x5064);
3261 if (r32 & 0x20000)
3262 t1_cycles += (r32 & 0xfff);
3263 t1_cycles += (read32(DEFAULT_MCHBAR + channel * 0x400 + 0x42a4) & 0xfff);
3264 t1_ns = t1_cycles * ctrl->tCK / 256 + 544;
3265 if (!(r32 & 0x20000))
3266 t1_ns += 500;
3267
3268 t2_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f10) >> 8) & 0xfff);
3269 if ( read32(DEFAULT_MCHBAR + 0x5f00) & 8 )
3270 {
3271 t3_ns = 10 * ((read32(DEFAULT_MCHBAR + 0x5f20) >> 8) & 0xfff);
3272 t3_ns += 10 * (read32(DEFAULT_MCHBAR + 0x5f18) & 0xff);
3273 }
3274 else
3275 {
3276 t3_ns = 500;
3277 }
3278 printk(BIOS_DEBUG, "t123: %d, %d, %d\n",
3279 t1_ns, t2_ns, t3_ns);
3280 write32 (DEFAULT_MCHBAR + 0x5d10,
3281 ((encode_5d10(t1_ns) + encode_5d10(t2_ns)) << 16)
3282 | (encode_5d10(t1_ns) << 8)
3283 | ((encode_5d10(t3_ns) + encode_5d10(t2_ns) + encode_5d10(t1_ns)) << 24)
3284 | (read32(DEFAULT_MCHBAR + 0x5d10) & 0xC0C0C0C0)
3285 | 0xc);
3286}
3287
3288void restore_timings(ramctr_timing * ctrl)
3289{
3290 int channel, slotrank, lane;
3291
3292 FOR_ALL_POPULATED_CHANNELS
3293 MCHBAR32(0x4004 + 0x400 * channel) =
3294 ctrl->tRRD
3295 | (ctrl->tRTP << 4)
3296 | (ctrl->tCKE << 8)
3297 | (ctrl->tWTR << 12)
3298 | (ctrl->tFAW << 16)
3299 | (ctrl->tWR << 24)
3300 | (ctrl->cmd_stretch[channel] << 30);
3301
3302 udelay(1);
3303
3304 FOR_ALL_POPULATED_CHANNELS {
3305 wait_428c(channel);
3306 }
3307
3308 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES {
3309 write32(DEFAULT_MCHBAR + 0x4080 + 0x400 * channel
3310 + 4 * lane, 0);
3311 }
3312
3313 FOR_ALL_POPULATED_CHANNELS
3314 write32(DEFAULT_MCHBAR + 0x4008 + 0x400 * channel,
3315 read32(DEFAULT_MCHBAR + 0x4008 +
3316 0x400 * channel) | 0x8000000);
3317
3318 FOR_ALL_POPULATED_CHANNELS {
3319 udelay (1);
3320 write32(DEFAULT_MCHBAR + 0x4020 + 0x400 * channel,
3321 read32(DEFAULT_MCHBAR + 0x4020 +
3322 0x400 * channel) | 0x200000);
3323 }
3324
3325 printram("CPE\n");
3326
3327 write32(DEFAULT_MCHBAR + 0x3400, 0);
3328 write32(DEFAULT_MCHBAR + 0x4eb0, 0);
3329
3330 printram("CP5b\n");
3331
3332 FOR_ALL_POPULATED_CHANNELS {
3333 program_timings(ctrl, channel);
3334 }
3335
3336 u32 reg, addr;
3337
3338 while (!(MCHBAR32(0x5084) & 0x10000));
3339 do {
3340 reg = MCHBAR32(0x428c);
3341 } while ((reg & 0x14) == 0);
3342
3343 // Set state of memory controller
3344 MCHBAR32(0x5030) = 0x116;
3345 MCHBAR32(0x4ea0) = 0;
3346
3347 // Wait 500us
3348 udelay(500);
3349
3350 FOR_ALL_CHANNELS {
3351 // Set valid rank CKE
3352 reg = 0;
3353 reg = (reg & ~0xf) | ctrl->rankmap[channel];
3354 addr = 0x400 * channel + 0x42a0;
3355 MCHBAR32(addr) = reg;
3356
3357 // Wait 10ns for ranks to settle
3358 //udelay(0.01);
3359
3360 reg = (reg & ~0xf0) | (ctrl->rankmap[channel] << 4);
3361 MCHBAR32(addr) = reg;
3362
3363 // Write reset using a NOP
3364 write_reset(ctrl);
3365 }
3366
3367 /* mrs commands. */
3368 dram_mrscommands(ctrl);
3369
3370 printram("CP5c\n");
3371
3372 write32(DEFAULT_MCHBAR + 0x3000, 0);
3373
3374 FOR_ALL_CHANNELS {
3375 write32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c,
3376 0 | (read32(DEFAULT_MCHBAR + (channel * 0x100) + 0xe3c) &
3377 ~0x3f000000));
3378 udelay(2);
3379 }
3380
3381 write32(DEFAULT_MCHBAR + 0x4ea8, 0);
3382}