2 * This file is part of the coreboot project.
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>
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.
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.
18 #include <console/console.h>
19 #include <console/usb.h>
23 #include <arch/cbfs.h>
25 #include <northbridge/intel/sandybridge/chip.h>
26 #include <device/pci_def.h>
29 #include "raminit_native.h"
30 #include "raminit_common.h"
31 #include "sandybridge.h"
33 /* FIXME: no ECC support. */
34 /* FIXME: no support for 3-channel chipsets. */
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.
44 * Command queue index Y = [0, 1, 2, 3]
46 * DEFAULT_MCHBAR + 0x4220 + 0x400 * X + 4 * Y: command io register
47 * Controls the DRAM command signals
52 * DEFAULT_MCHBAR + 0x4200 + 0x400 * X + 4 * Y: addr bankslot io register
53 * Controls the address, bank address and slotrank signals
55 * Bit 20-22: Bank Address
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
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
68 static void sfence(void)
70 asm volatile ("sfence");
73 static void toggle_io_reset(void) {
74 /* toggle IO reset bit */
75 u32 r32
= read32(DEFAULT_MCHBAR
+ 0x5030);
76 write32(DEFAULT_MCHBAR
+ 0x5030, r32
| 0x20);
78 write32(DEFAULT_MCHBAR
+ 0x5030, r32
& ~0x20);
82 static u32
get_XOVER_CLK(u8 rankmap
)
87 static u32
get_XOVER_CMD(u8 rankmap
)
104 /* CAS write latency. To be programmed in MR2.
105 * See DDR3 SPEC for MR2 documentation. */
108 /* Get CWL based on tCK using the following rule: */
133 void dram_find_common_params(ramctr_timing
*ctrl
)
137 dimm_info
*dimms
= &ctrl
->info
;
139 ctrl
->cas_supported
= (1 << (MAX_CAS
- MIN_CAS
+ 1)) - 1;
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
)
147 /* Find all possible CAS combinations */
148 ctrl
->cas_supported
&= dimm
->cas_supported
;
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
);
162 ctrl
->tCWL
= MAX(ctrl
->tCWL
, dimm
->tCWL
);
163 ctrl
->tCMD
= MAX(ctrl
->tCMD
, dimm
->tCMD
);
166 if (!ctrl
->cas_supported
)
167 die("Unsupported DIMM combination. "
168 "DIMMS do not support common CAS latency");
170 die("No valid DIMMs found");
173 void dram_xover(ramctr_timing
* ctrl
)
180 reg
= get_XOVER_CLK(ctrl
->rankmap
[channel
]);
181 printram("XOVER CLK [%x] = %x\n", channel
* 0x100 + 0xc14,
183 MCHBAR32(channel
* 0x100 + 0xc14) = reg
;
185 // enable xover ctl & xover cmd
186 reg
= get_XOVER_CMD(ctrl
->rankmap
[channel
]);
187 printram("XOVER CMD [%x] = %x\n", 0x100 * channel
+ 0x320c,
189 MCHBAR32(0x100 * channel
+ 0x320c) = reg
;
193 static void dram_odt_stretch(ramctr_timing
*ctrl
, int channel
)
195 struct cpuid_result cpures
;
196 u32 reg
, addr
, cpu
, stretch
;
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. */
203 if (IS_SANDY_CPU(cpu
) && IS_SANDY_CPU_C(cpu
)) {
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
);
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
);
223 void dram_timing_regs(ramctr_timing
*ctrl
)
225 u32 reg
, addr
, val32
;
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
;
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);
248 printram("RAP [%x] = %x\n", 0x400 * channel
+ 0x4004, reg
);
249 MCHBAR32(0x400 * channel
+ 0x4004) = reg
;
252 addr
= 0x400 * channel
+ 0x400c;
255 reg
|= (ctrl
->tXP
<< 5);
256 reg
|= (ctrl
->tAONPD
<< 8);
258 printram("OTHP [%x] = %x\n", addr
, reg
);
259 MCHBAR32(addr
) = reg
;
261 MCHBAR32(0x400 * channel
+ 0x4014) = 0;
263 MCHBAR32(addr
) |= 0x00020000;
265 dram_odt_stretch(ctrl
, channel
);
270 reg
= (reg
& ~0xffff) | val32
;
272 reg
= (reg
& ~0x1ff0000) | (val32
<< 16);
273 val32
= (u32
) (ctrl
->tREFI
* 9) / 1024;
274 reg
= (reg
& ~0xfe000000) | (val32
<< 25);
275 printram("REFI [%x] = %x\n", 0x400 * channel
+ 0x4298,
277 MCHBAR32(0x400 * channel
+ 0x4298) = reg
;
279 MCHBAR32(0x400 * channel
+ 0x4294) |= 0xff;
284 reg
= (reg
& ~0xfff) | val32
;
285 val32
= ctrl
->tXSOffset
;
286 reg
= (reg
& ~0xf000) | (val32
<< 12);
287 val32
= tDLLK
- ctrl
->tXSOffset
;
288 reg
= (reg
& ~0x3ff0000) | (val32
<< 16);
289 val32
= ctrl
->tMOD
- 8;
290 reg
= (reg
& ~0xf0000000) | (val32
<< 28);
291 printram("SRFTP [%x] = %x\n", 0x400 * channel
+ 0x42a4,
293 MCHBAR32(0x400 * channel
+ 0x42a4) = reg
;
297 void dram_dimm_mapping(ramctr_timing
*ctrl
)
300 dimm_info
*info
= &ctrl
->info
;
303 dimm_attr
*dimmA
, *dimmB
;
306 if (info
->dimm
[channel
][0].size_mb
>=
307 info
->dimm
[channel
][1].size_mb
) {
308 dimmA
= &info
->dimm
[channel
][0];
309 dimmB
= &info
->dimm
[channel
][1];
312 dimmA
= &info
->dimm
[channel
][1];
313 dimmB
= &info
->dimm
[channel
][0];
317 if (dimmA
&& (dimmA
->ranks
> 0)) {
318 reg
|= dimmA
->size_mb
/ 256;
319 reg
|= (dimmA
->ranks
- 1) << 17;
320 reg
|= (dimmA
->width
/ 8 - 1) << 19;
323 if (dimmB
&& (dimmB
->ranks
> 0)) {
324 reg
|= (dimmB
->size_mb
/ 256) << 8;
325 reg
|= (dimmB
->ranks
- 1) << 18;
326 reg
|= (dimmB
->width
/ 8 - 1) << 20;
329 reg
|= 1 << 21; /* rank interleave */
330 reg
|= 1 << 22; /* enhanced interleave */
332 if ((dimmA
&& (dimmA
->ranks
> 0))
333 || (dimmB
&& (dimmB
->ranks
> 0))) {
334 ctrl
->mad_dimm
[channel
] = reg
;
336 ctrl
->mad_dimm
[channel
] = 0;
341 void dram_dimm_set_mapping(ramctr_timing
* ctrl
)
345 MCHBAR32(0x5004 + channel
* 4) = ctrl
->mad_dimm
[channel
];
349 void dram_zones(ramctr_timing
* ctrl
, int training
)
351 u32 reg
, ch0size
, ch1size
;
356 ch0size
= ctrl
->channel_size_mb
[0] ? 256 : 0;
357 ch1size
= ctrl
->channel_size_mb
[1] ? 256 : 0;
359 ch0size
= ctrl
->channel_size_mb
[0];
360 ch1size
= ctrl
->channel_size_mb
[1];
363 if (ch0size
>= ch1size
) {
364 reg
= MCHBAR32(0x5014);
366 reg
= (reg
& ~0xff000000) | val
<< 24;
367 reg
= (reg
& ~0xff0000) | (2 * val
) << 16;
368 MCHBAR32(0x5014) = reg
;
369 MCHBAR32(0x5000) = 0x24;
371 reg
= MCHBAR32(0x5014);
373 reg
= (reg
& ~0xff000000) | val
<< 24;
374 reg
= (reg
& ~0xff0000) | (2 * val
) << 16;
375 MCHBAR32(0x5014) = reg
;
376 MCHBAR32(0x5000) = 0x21;
380 #define HOST_BRIDGE PCI_DEVFN(0, 0)
381 #define DEFAULT_TCK TCK_800MHZ
383 unsigned int get_mem_min_tck(void)
387 const struct device
*dev
;
388 const struct northbridge_intel_sandybridge_config
*cfg
= NULL
;
390 dev
= dev_find_slot(0, HOST_BRIDGE
);
392 cfg
= dev
->chip_info
;
394 /* If this is zero, it just means devicetree.cb didn't set it */
395 if (!cfg
|| cfg
->max_mem_clock_mhz
== 0) {
396 if (IS_ENABLED(CONFIG_NATIVE_RAMINIT_IGNORE_MAX_MEM_FUSES
))
399 rev
= pci_read_config8(PCI_DEV(0, 0, 0), PCI_DEVICE_ID
);
401 if ((rev
& BASE_REV_MASK
) == BASE_REV_SNB
) {
402 /* read Capabilities A Register DMFC bits */
403 reg32
= pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_A
);
407 case 7: return TCK_533MHZ
;
408 case 6: return TCK_666MHZ
;
409 case 5: return TCK_800MHZ
;
415 /* read Capabilities B Register DMFC bits */
416 reg32
= pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_B
);
417 reg32
= (reg32
>> 4) & 0x7;
420 case 7: return TCK_533MHZ
;
421 case 6: return TCK_666MHZ
;
422 case 5: return TCK_800MHZ
;
423 case 4: return TCK_933MHZ
;
424 case 3: return TCK_1066MHZ
;
425 case 2: return TCK_1200MHZ
;
426 case 1: return TCK_1333MHZ
;
434 if (cfg
->max_mem_clock_mhz
>= 1066)
436 else if (cfg
->max_mem_clock_mhz
>= 933)
438 else if (cfg
->max_mem_clock_mhz
>= 800)
440 else if (cfg
->max_mem_clock_mhz
>= 666)
442 else if (cfg
->max_mem_clock_mhz
>= 533)
449 #define DEFAULT_PCI_MMIO_SIZE 2048
451 static unsigned int get_mmio_size(void)
453 const struct device
*dev
;
454 const struct northbridge_intel_sandybridge_config
*cfg
= NULL
;
456 dev
= dev_find_slot(0, HOST_BRIDGE
);
458 cfg
= dev
->chip_info
;
460 /* If this is zero, it just means devicetree.cb didn't set it */
461 if (!cfg
|| cfg
->pci_mmio_size
== 0)
462 return DEFAULT_PCI_MMIO_SIZE
;
464 return cfg
->pci_mmio_size
;
467 void dram_memorymap(ramctr_timing
* ctrl
, int me_uma_size
)
469 u32 reg
, val
, reclaim
;
470 u32 tom
, gfxstolen
, gttsize
;
471 size_t tsegsize
, mmiosize
, toludbase
, touudbase
, gfxstolenbase
, gttbase
,
472 tsegbase
, mestolenbase
;
473 size_t tsegbasedelta
, remapbase
, remaplimit
;
476 mmiosize
= get_mmio_size();
478 ggc
= pci_read_config16(NORTHBRIDGE
, GGC
);
480 gfxstolen
= ((ggc
>> 3) & 0x1f) * 32;
481 gttsize
= ((ggc
>> 8) & 0x3);
487 tsegsize
= CONFIG_SMM_TSEG_SIZE
>> 20;
489 tom
= ctrl
->channel_size_mb
[0] + ctrl
->channel_size_mb
[1];
491 mestolenbase
= tom
- me_uma_size
;
493 toludbase
= MIN(4096 - mmiosize
+ gfxstolen
+ gttsize
+ tsegsize
,
495 gfxstolenbase
= toludbase
- gfxstolen
;
496 gttbase
= gfxstolenbase
- gttsize
;
498 tsegbase
= gttbase
- tsegsize
;
500 // Round tsegbase down to nearest address aligned to tsegsize
501 tsegbasedelta
= tsegbase
& (tsegsize
- 1);
502 tsegbase
&= ~(tsegsize
- 1);
504 gttbase
-= tsegbasedelta
;
505 gfxstolenbase
-= tsegbasedelta
;
506 toludbase
-= tsegbasedelta
;
508 // Test if it is possible to reclaim a hole in the RAM addressing
509 if (tom
- me_uma_size
> toludbase
) {
510 // Reclaim is possible
512 remapbase
= MAX(4096, tom
- me_uma_size
);
514 remapbase
+ MIN(4096, tom
- me_uma_size
) - toludbase
- 1;
515 touudbase
= remaplimit
+ 1;
517 // Reclaim not possible
519 touudbase
= tom
- me_uma_size
;
522 // Update memory map in pci-e configuration space
523 printk(BIOS_DEBUG
, "Update PCI-E configuration space:\n");
525 // TOM (top of memory)
526 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0xa0);
528 reg
= (reg
& ~0xfff00000) | (val
<< 20);
529 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0xa0, reg
);
530 pci_write_config32(PCI_DEV(0, 0, 0), 0xa0, reg
);
532 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0xa4);
533 val
= tom
& 0xfffff000;
534 reg
= (reg
& ~0x000fffff) | (val
>> 12);
535 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0xa4, reg
);
536 pci_write_config32(PCI_DEV(0, 0, 0), 0xa4, reg
);
538 // TOLUD (top of low used dram)
539 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0xbc);
540 val
= toludbase
& 0xfff;
541 reg
= (reg
& ~0xfff00000) | (val
<< 20);
542 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0xbc, reg
);
543 pci_write_config32(PCI_DEV(0, 0, 0), 0xbc, reg
);
545 // TOUUD LSB (top of upper usable dram)
546 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0xa8);
547 val
= touudbase
& 0xfff;
548 reg
= (reg
& ~0xfff00000) | (val
<< 20);
549 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0xa8, reg
);
550 pci_write_config32(PCI_DEV(0, 0, 0), 0xa8, reg
);
553 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0xac);
554 val
= touudbase
& 0xfffff000;
555 reg
= (reg
& ~0x000fffff) | (val
>> 12);
556 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0xac, reg
);
557 pci_write_config32(PCI_DEV(0, 0, 0), 0xac, reg
);
561 pci_write_config32(PCI_DEV(0, 0, 0), 0x90, remapbase
<< 20);
562 pci_write_config32(PCI_DEV(0, 0, 0), 0x94, remapbase
>> 12);
565 pci_write_config32(PCI_DEV(0, 0, 0), 0x98, remaplimit
<< 20);
566 pci_write_config32(PCI_DEV(0, 0, 0), 0x9c, remaplimit
>> 12);
569 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0xb8);
570 val
= tsegbase
& 0xfff;
571 reg
= (reg
& ~0xfff00000) | (val
<< 20);
572 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0xb8, reg
);
573 pci_write_config32(PCI_DEV(0, 0, 0), 0xb8, reg
);
576 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0xb0);
577 val
= gfxstolenbase
& 0xfff;
578 reg
= (reg
& ~0xfff00000) | (val
<< 20);
579 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0xb0, reg
);
580 pci_write_config32(PCI_DEV(0, 0, 0), 0xb0, reg
);
583 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0xb4);
584 val
= gttbase
& 0xfff;
585 reg
= (reg
& ~0xfff00000) | (val
<< 20);
586 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0xb4, reg
);
587 pci_write_config32(PCI_DEV(0, 0, 0), 0xb4, reg
);
590 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0x7c);
591 val
= (0x80000 - me_uma_size
) & 0xfffff000;
592 reg
= (reg
& ~0x000fffff) | (val
>> 12);
593 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0x7c, reg
);
594 pci_write_config32(PCI_DEV(0, 0, 0), 0x7c, reg
);
597 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0x70);
598 val
= mestolenbase
& 0xfff;
599 reg
= (reg
& ~0xfff00000) | (val
<< 20);
600 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0x70, reg
);
601 pci_write_config32(PCI_DEV(0, 0, 0), 0x70, reg
);
603 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0x74);
604 val
= mestolenbase
& 0xfffff000;
605 reg
= (reg
& ~0x000fffff) | (val
>> 12);
606 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0x74, reg
);
607 pci_write_config32(PCI_DEV(0, 0, 0), 0x74, reg
);
610 reg
= pci_read_config32(PCI_DEV(0, 0, 0), 0x78);
611 val
= (0x80000 - me_uma_size
) & 0xfff;
612 reg
= (reg
& ~0xfff00000) | (val
<< 20);
613 reg
= (reg
& ~0x400) | (1 << 10); // set lockbit on ME mem
615 reg
= (reg
& ~0x800) | (1 << 11); // set ME memory enable
616 printk(BIOS_DEBUG
, "PCI(0, 0, 0)[%x] = %x\n", 0x78, reg
);
617 pci_write_config32(PCI_DEV(0, 0, 0), 0x78, reg
);
621 static void wait_428c(int channel
)
624 if (read32(DEFAULT_MCHBAR
+ 0x428c + (channel
<< 10)) & 0x50)
629 static void write_reset(ramctr_timing
* ctrl
)
631 int channel
, slotrank
;
633 /* choose a populated channel. */
634 channel
= (ctrl
->rankmap
[0]) ? 0 : 1;
638 /* choose a populated rank. */
639 slotrank
= (ctrl
->rankmap
[channel
] & 1) ? 0 : 2;
641 /* DRAM command ZQCS */
642 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x0f003);
643 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
, 0x80c01);
645 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
646 (slotrank
<< 24) | 0x60000);
648 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
650 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0x400001);
654 void dram_jedecreset(ramctr_timing
* ctrl
)
659 while (!(MCHBAR32(0x5084) & 0x10000));
661 reg
= MCHBAR32(0x428c);
662 } while ((reg
& 0x14) == 0);
664 // Set state of memory controller
666 MCHBAR32(0x5030) = reg
;
667 MCHBAR32(0x4ea0) = 0;
668 reg
|= 2; //ddr reset
669 MCHBAR32(0x5030) = reg
;
671 // Assert dimm reset signal
672 reg
= MCHBAR32(0x5030);
674 MCHBAR32(0x5030) = reg
;
679 // Deassert dimm reset signal
680 MCHBAR32(0x5030) |= 2;
686 MCHBAR32(0x5030) |= 4;
692 // Set valid rank CKE
694 reg
= (reg
& ~0xf) | ctrl
->rankmap
[channel
];
695 addr
= 0x400 * channel
+ 0x42a0;
696 MCHBAR32(addr
) = reg
;
698 // Wait 10ns for ranks to settle
701 reg
= (reg
& ~0xf0) | (ctrl
->rankmap
[channel
] << 4);
702 MCHBAR32(addr
) = reg
;
704 // Write reset using a NOP
709 static odtmap
get_ODT(ramctr_timing
*ctrl
, u8 rank
, int channel
)
711 /* Get ODT based on rankmap: */
712 int dimms_per_ch
= (ctrl
->rankmap
[channel
] & 1)
713 + ((ctrl
->rankmap
[channel
] >> 2) & 1);
715 if (dimms_per_ch
== 1) {
716 return (const odtmap
){60, 60};
718 return (const odtmap
){120, 30};
722 static void write_mrreg(ramctr_timing
*ctrl
, int channel
, int slotrank
,
727 if (ctrl
->rank_mirror
[channel
][slotrank
]) {
728 /* DDR3 Rank1 Address mirror
729 * swap the following pins:
730 * A3<->A4, A5<->A6, A7<->A8, BA0<->BA1 */
731 reg
= ((reg
>> 1) & 1) | ((reg
<< 1) & 2);
732 val
= (val
& ~0x1f8) | ((val
>> 1) & 0xa8)
733 | ((val
& 0xa8) << 1);
736 /* DRAM command MRS */
737 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x0f000);
738 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
, 0x41001);
739 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
740 (slotrank
<< 24) | (reg
<< 20) | val
| 0x60000);
741 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
743 /* DRAM command MRS */
744 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f000);
745 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
, 0x41001);
746 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
747 (slotrank
<< 24) | (reg
<< 20) | val
| 0x60000);
748 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
750 /* DRAM command MRS */
751 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
, 0x0f000);
752 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
753 0x1001 | (ctrl
->tMOD
<< 16));
754 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
755 (slotrank
<< 24) | (reg
<< 20) | val
| 0x60000);
756 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0);
757 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0x80001);
760 static u32
make_mr0(ramctr_timing
* ctrl
, u8 rank
)
762 u16 mr0reg
, mch_cas
, mch_wr
;
763 static const u8 mch_wr_t
[12] = { 1, 2, 3, 4, 0, 5, 0, 6, 0, 7, 0, 0 };
765 /* DLL Reset - self clearing - set after CLK frequency has been changed */
768 // Convert CAS to MCH register friendly
769 if (ctrl
->CAS
< 12) {
770 mch_cas
= (u16
) ((ctrl
->CAS
- 4) << 1);
772 mch_cas
= (u16
) (ctrl
->CAS
- 12);
773 mch_cas
= ((mch_cas
<< 1) | 0x1);
776 // Convert tWR to MCH register friendly
777 mch_wr
= mch_wr_t
[ctrl
->tWR
- 5];
779 mr0reg
= (mr0reg
& ~0x4) | ((mch_cas
& 0x1) << 2);
780 mr0reg
= (mr0reg
& ~0x70) | ((mch_cas
& 0xe) << 3);
781 mr0reg
= (mr0reg
& ~0xe00) | (mch_wr
<< 9);
783 // Precharge PD - Fast (desktop) 0x1 or slow (mobile) 0x0 - mostly power-saving feature
784 mr0reg
= (mr0reg
& ~0x1000) | (!ctrl
->mobile
<< 12);
788 static void dram_mr0(ramctr_timing
*ctrl
, u8 rank
, int channel
)
790 write_mrreg(ctrl
, channel
, rank
, 0,
791 make_mr0(ctrl
, rank
));
794 static u32
encode_odt(u32 odt
)
798 return (1 << 9) | (1 << 2); // RZQ/8, RZQ/4
800 return (1 << 2); // RZQ/4
802 return (1 << 6); // RZQ/2
809 static u32
make_mr1(ramctr_timing
*ctrl
, u8 rank
, int channel
)
814 odt
= get_ODT(ctrl
, rank
, channel
);
817 mr1reg
|= encode_odt(odt
.rttnom
);
822 static void dram_mr1(ramctr_timing
*ctrl
, u8 rank
, int channel
)
826 mr1reg
= make_mr1(ctrl
, rank
, channel
);
828 write_mrreg(ctrl
, channel
, rank
, 1, mr1reg
);
831 static void dram_mr2(ramctr_timing
*ctrl
, u8 rank
, int channel
)
833 u16 pasr
, cwl
, mr2reg
;
839 odt
= get_ODT(ctrl
, rank
, channel
);
841 srt
= ctrl
->extended_temperature_range
&& !ctrl
->auto_self_refresh
;
844 mr2reg
= (mr2reg
& ~0x7) | pasr
;
845 mr2reg
= (mr2reg
& ~0x38) | (cwl
<< 3);
846 mr2reg
= (mr2reg
& ~0x40) | (ctrl
->auto_self_refresh
<< 6);
847 mr2reg
= (mr2reg
& ~0x80) | (srt
<< 7);
848 mr2reg
|= (odt
.rttwr
/ 60) << 9;
850 write_mrreg(ctrl
, channel
, rank
, 2, mr2reg
);
853 static void dram_mr3(ramctr_timing
*ctrl
, u8 rank
, int channel
)
855 write_mrreg(ctrl
, channel
, rank
, 3, 0);
858 void dram_mrscommands(ramctr_timing
* ctrl
)
864 FOR_ALL_POPULATED_CHANNELS
{
865 FOR_ALL_POPULATED_RANKS
{
867 dram_mr2(ctrl
, slotrank
, channel
);
870 dram_mr3(ctrl
, slotrank
, channel
);
873 dram_mr1(ctrl
, slotrank
, channel
);
876 dram_mr0(ctrl
, slotrank
, channel
);
880 /* DRAM command NOP */
881 write32(DEFAULT_MCHBAR
+ 0x4e20, 0x7);
882 write32(DEFAULT_MCHBAR
+ 0x4e30, 0xf1001);
883 write32(DEFAULT_MCHBAR
+ 0x4e00, 0x60002);
884 write32(DEFAULT_MCHBAR
+ 0x4e10, 0);
886 /* DRAM command ZQCL */
887 write32(DEFAULT_MCHBAR
+ 0x4e24, 0x1f003);
888 write32(DEFAULT_MCHBAR
+ 0x4e34, 0x1901001);
889 write32(DEFAULT_MCHBAR
+ 0x4e04, 0x60400);
890 write32(DEFAULT_MCHBAR
+ 0x4e14, 0x288);
892 /* execute command queue on all channels ? */
893 write32(DEFAULT_MCHBAR
+ 0x4e84, 0x40004);
897 // Wait for ref drained
902 MCHBAR32(0x5030) |= 8;
904 FOR_ALL_POPULATED_CHANNELS
{
905 addr
= 0x400 * channel
+ 0x4020;
906 reg
= MCHBAR32(addr
);
908 MCHBAR32(addr
) = reg
;
912 slotrank
= (ctrl
->rankmap
[channel
] & 1) ? 0 : 2;
917 /* DRAM command ZQCS */
918 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x0f003);
919 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
, 0x659001);
920 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
921 (slotrank
<< 24) | 0x60000);
922 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x3e0);
923 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0x1);
930 static const u32 lane_registers
[] = {
931 0x0000, 0x0200, 0x0400, 0x0600,
932 0x1000, 0x1200, 0x1400, 0x1600,
936 void program_timings(ramctr_timing
* ctrl
, int channel
)
938 u32 reg32
, reg_4024
, reg_c14
, reg_c18
, reg_4028
;
942 u16 slot320c
[NUM_SLOTS
];
944 FOR_ALL_POPULATED_RANKS
{
945 if (full_shift
< -ctrl
->timings
[channel
][slotrank
].val_320c
)
946 full_shift
= -ctrl
->timings
[channel
][slotrank
].val_320c
;
949 for (slot
= 0; slot
< NUM_SLOTS
; slot
++)
950 switch ((ctrl
->rankmap
[channel
] >> (2 * slot
)) & 3) {
953 slot320c
[slot
] = 0x7f;
957 ctrl
->timings
[channel
][2 * slot
+ 0].val_320c
+
962 ctrl
->timings
[channel
][2 * slot
+ 1].val_320c
+
967 (ctrl
->timings
[channel
][2 * slot
].val_320c
+
968 ctrl
->timings
[channel
][2 * slot
+
974 /* enable CMD XOVER */
975 reg32
= get_XOVER_CMD(ctrl
->rankmap
[channel
]);
976 reg32
|= ((slot320c
[0] & 0x3f) << 6) | ((slot320c
[0] & 0x40) << 9);
977 reg32
|= (slot320c
[1] & 0x7f) << 18;
978 reg32
|= (full_shift
& 0x3f) | ((full_shift
& 0x40) << 6);
980 MCHBAR32(0x320c + 0x100 * channel
) = reg32
;
982 /* enable CLK XOVER */
983 reg_c14
= get_XOVER_CLK(ctrl
->rankmap
[channel
]);
986 FOR_ALL_POPULATED_RANKS
{
988 ctrl
->timings
[channel
][slotrank
].val_320c
+ full_shift
;
992 offset_val_c14
= ctrl
->reg_c14_offset
+ shift
;
993 /* set CLK phase shift */
994 reg_c14
|= (offset_val_c14
& 0x3f) << (6 * slotrank
);
995 reg_c18
|= ((offset_val_c14
>> 6) & 1) << slotrank
;
998 MCHBAR32(0xc14 + channel
* 0x100) = reg_c14
;
999 MCHBAR32(0xc18 + channel
* 0x100) = reg_c18
;
1001 reg_4028
= MCHBAR32(0x4028 + 0x400 * channel
);
1002 reg_4028
&= 0xffff0000;
1006 FOR_ALL_POPULATED_RANKS
{
1007 int post_timA_min_high
= 7, post_timA_max_high
= 0;
1008 int pre_timA_min_high
= 7, pre_timA_max_high
= 0;
1011 ctrl
->timings
[channel
][slotrank
].val_320c
+ full_shift
;
1017 post_timA_min_high
= MIN(post_timA_min_high
,
1018 (ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1019 timA
+ shift
) >> 6);
1020 pre_timA_min_high
= MIN(pre_timA_min_high
,
1021 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1023 post_timA_max_high
= MAX(post_timA_max_high
,
1024 (ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1025 timA
+ shift
) >> 6);
1026 pre_timA_max_high
= MAX(pre_timA_max_high
,
1027 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1031 if (pre_timA_max_high
- pre_timA_min_high
<
1032 post_timA_max_high
- post_timA_min_high
)
1034 else if (pre_timA_max_high
- pre_timA_min_high
>
1035 post_timA_max_high
- post_timA_min_high
)
1039 (ctrl
->timings
[channel
][slotrank
].val_4028
+ shift_402x
-
1040 post_timA_min_high
) << (4 * slotrank
);
1042 (ctrl
->timings
[channel
][slotrank
].val_4024
+
1043 shift_402x
) << (8 * slotrank
);
1046 MCHBAR32(lane_registers
[lane
] + 0x10 + 0x100 * channel
+
1049 (((ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1050 timA
+ shift
) & 0x3f)
1052 ((ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1053 rising
+ shift
) << 8)
1055 (((ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1057 (post_timA_min_high
<< 6)) & 0x1c0) << 10)
1058 | ((ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1059 falling
+ shift
) << 20));
1061 MCHBAR32(lane_registers
[lane
] + 0x20 + 0x100 * channel
+
1064 (((ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1065 timC
+ shift
) & 0x3f)
1067 (((ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1068 timB
+ shift
) & 0x3f) << 8)
1070 (((ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1071 timB
+ shift
) & 0x1c0) << 9)
1073 (((ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1074 timC
+ shift
) & 0x40) << 13));
1077 MCHBAR32(0x4024 + 0x400 * channel
) = reg_4024
;
1078 MCHBAR32(0x4028 + 0x400 * channel
) = reg_4028
;
1081 static void test_timA(ramctr_timing
* ctrl
, int channel
, int slotrank
)
1086 * write MR3 MPR enable
1087 * in this mode only RD and RDA are allowed
1088 * all reads return a predefined pattern */
1089 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f000);
1090 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1091 (0xc01 | (ctrl
->tMOD
<< 16)));
1092 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1093 (slotrank
<< 24) | 0x360004);
1094 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
1096 /* DRAM command RD */
1097 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f105);
1098 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
, 0x4040c01);
1099 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
, (slotrank
<< 24));
1100 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
1102 /* DRAM command RD */
1103 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
, 0x1f105);
1104 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
1105 0x100f | ((ctrl
->CAS
+ 36) << 16));
1106 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
1107 (slotrank
<< 24) | 0x60000);
1108 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0);
1111 * write MR3 MPR disable */
1112 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
, 0x1f000);
1113 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
1114 (0xc01 | (ctrl
->tMOD
<< 16)));
1115 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
1116 (slotrank
<< 24) | 0x360000);
1117 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0);
1119 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0xc0001);
1124 static int does_lane_work(ramctr_timing
* ctrl
, int channel
, int slotrank
,
1127 u32 timA
= ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
;
1129 (DEFAULT_MCHBAR
+ lane_registers
[lane
] + channel
* 0x100 + 4 +
1130 ((timA
/ 32) & 1) * 4)
1131 >> (timA
% 32)) & 1);
1142 static struct run
get_longest_zero_run(int *seq
, int sz
)
1149 for (i
= 0; i
< 2 * sz
; i
++)
1158 ret
.middle
= sz
/ 2;
1165 ret
.start
= bs
% sz
;
1166 ret
.end
= (bs
+ bl
- 1) % sz
;
1167 ret
.middle
= (bs
+ (bl
- 1) / 2) % sz
;
1174 static void discover_timA_coarse(ramctr_timing
* ctrl
, int channel
,
1175 int slotrank
, int *upperA
)
1178 int statistics
[NUM_LANES
][128];
1181 for (timA
= 0; timA
< 128; timA
++) {
1183 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
= timA
;
1185 program_timings(ctrl
, channel
);
1187 test_timA(ctrl
, channel
, slotrank
);
1190 statistics
[lane
][timA
] =
1191 !does_lane_work(ctrl
, channel
, slotrank
, lane
);
1195 struct run rn
= get_longest_zero_run(statistics
[lane
], 128);
1196 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
= rn
.middle
;
1197 upperA
[lane
] = rn
.end
;
1198 if (upperA
[lane
] < rn
.middle
)
1199 upperA
[lane
] += 128;
1200 printram("timA: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1201 channel
, slotrank
, lane
, rn
.start
, rn
.middle
, rn
.end
);
1205 static void discover_timA_fine(ramctr_timing
* ctrl
, int channel
, int slotrank
,
1209 int statistics
[NUM_LANES
][51];
1212 memset(statistics
, 0, sizeof(statistics
));
1214 for (timA_delta
= -25; timA_delta
<= 25; timA_delta
++) {
1215 FOR_ALL_LANES ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1216 timA
= upperA
[lane
] + timA_delta
+ 0x40;
1217 program_timings(ctrl
, channel
);
1219 for (i
= 0; i
< 100; i
++) {
1220 test_timA(ctrl
, channel
, slotrank
);
1222 statistics
[lane
][timA_delta
+ 25] +=
1223 does_lane_work(ctrl
, channel
, slotrank
,
1229 int last_zero
, first_all
;
1231 for (last_zero
= -25; last_zero
<= 25; last_zero
++)
1232 if (statistics
[lane
][last_zero
+ 25])
1235 for (first_all
= -25; first_all
<= 25; first_all
++)
1236 if (statistics
[lane
][first_all
+ 25] == 100)
1239 printram("lane %d: %d, %d\n", lane
, last_zero
,
1242 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
=
1243 (last_zero
+ first_all
) / 2 + upperA
[lane
];
1244 printram("Aval: %d, %d, %d: %x\n", channel
, slotrank
,
1245 lane
, ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
);
1249 static int discover_402x(ramctr_timing
*ctrl
, int channel
, int slotrank
,
1252 int works
[NUM_LANES
];
1255 int all_works
= 1, some_works
= 0;
1256 program_timings(ctrl
, channel
);
1257 test_timA(ctrl
, channel
, slotrank
);
1260 !does_lane_work(ctrl
, channel
, slotrank
, lane
);
1269 if (ctrl
->timings
[channel
][slotrank
].val_4024
< 2) {
1270 printk(BIOS_EMERG
, "402x discovery failed (1): %d, %d\n",
1274 ctrl
->timings
[channel
][slotrank
].val_4024
-= 2;
1275 printram("4024 -= 2;\n");
1278 ctrl
->timings
[channel
][slotrank
].val_4028
+= 2;
1279 printram("4028 += 2;\n");
1280 if (ctrl
->timings
[channel
][slotrank
].val_4028
>= 0x10) {
1281 printk(BIOS_EMERG
, "402x discovery failed (2): %d, %d\n",
1285 FOR_ALL_LANES
if (works
[lane
]) {
1286 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
+=
1288 upperA
[lane
] += 128;
1289 printram("increment %d, %d, %d\n", channel
,
1296 struct timA_minmax
{
1297 int timA_min_high
, timA_max_high
;
1300 static void pre_timA_change(ramctr_timing
* ctrl
, int channel
, int slotrank
,
1301 struct timA_minmax
*mnmx
)
1304 mnmx
->timA_min_high
= 7;
1305 mnmx
->timA_max_high
= 0;
1308 if (mnmx
->timA_min_high
>
1309 (ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
>> 6))
1310 mnmx
->timA_min_high
=
1311 (ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1313 if (mnmx
->timA_max_high
<
1314 (ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
>> 6))
1315 mnmx
->timA_max_high
=
1316 (ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1321 static void post_timA_change(ramctr_timing
* ctrl
, int channel
, int slotrank
,
1322 struct timA_minmax
*mnmx
)
1324 struct timA_minmax post
;
1327 /* Get changed maxima. */
1328 pre_timA_change(ctrl
, channel
, slotrank
, &post
);
1330 if (mnmx
->timA_max_high
- mnmx
->timA_min_high
<
1331 post
.timA_max_high
- post
.timA_min_high
)
1333 else if (mnmx
->timA_max_high
- mnmx
->timA_min_high
>
1334 post
.timA_max_high
- post
.timA_min_high
)
1339 ctrl
->timings
[channel
][slotrank
].val_4028
+= shift_402x
;
1340 ctrl
->timings
[channel
][slotrank
].val_4024
+= shift_402x
;
1341 printram("4024 += %d;\n", shift_402x
);
1342 printram("4028 += %d;\n", shift_402x
);
1345 /* Compensate the skew between DQS and DQs.
1346 * To ease PCB design a small skew between Data Strobe signals and
1347 * Data Signals is allowed.
1348 * The controller has to measure and compensate this skew for every byte-lane.
1349 * By delaying either all DQs signals or DQS signal, a full phase
1350 * shift can be introduced.
1351 * It is assumed that one byte-lane's DQs signals have the same routing delay.
1353 * To measure the actual skew, the DRAM is placed in "read leveling" mode.
1354 * In read leveling mode the DRAM-chip outputs an alternating periodic pattern.
1355 * The memory controller iterates over all possible values to do a full phase shift
1356 * and issues read commands.
1357 * With DQS and DQs in phase the data read is expected to alternate on every byte:
1358 * 0xFF 0x00 0xFF ...
1359 * Once the controller has detected this pattern a bit in the result register is
1360 * set for the current phase shift.
1362 int read_training(ramctr_timing
* ctrl
)
1364 int channel
, slotrank
, lane
;
1367 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
1368 int all_high
, some_high
;
1369 int upperA
[NUM_LANES
];
1370 struct timA_minmax mnmx
;
1374 /* DRAM command PREA */
1375 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f002);
1376 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1377 0xc01 | (ctrl
->tRP
<< 16));
1378 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1379 (slotrank
<< 24) | 0x60400);
1380 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
1381 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 1);
1383 write32(DEFAULT_MCHBAR
+ 0x3400, (slotrank
<< 2) | 0x8001);
1385 ctrl
->timings
[channel
][slotrank
].val_4028
= 4;
1386 ctrl
->timings
[channel
][slotrank
].val_4024
= 55;
1387 program_timings(ctrl
, channel
);
1389 discover_timA_coarse(ctrl
, channel
, slotrank
, upperA
);
1394 if (ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1402 ctrl
->timings
[channel
][slotrank
].val_4028
--;
1403 printram("4028--;\n");
1405 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1407 upperA
[lane
] -= 0x40;
1410 } else if (some_high
) {
1411 ctrl
->timings
[channel
][slotrank
].val_4024
++;
1412 ctrl
->timings
[channel
][slotrank
].val_4028
++;
1413 printram("4024++;\n");
1414 printram("4028++;\n");
1417 program_timings(ctrl
, channel
);
1419 pre_timA_change(ctrl
, channel
, slotrank
, &mnmx
);
1421 err
= discover_402x(ctrl
, channel
, slotrank
, upperA
);
1425 post_timA_change(ctrl
, channel
, slotrank
, &mnmx
);
1426 pre_timA_change(ctrl
, channel
, slotrank
, &mnmx
);
1428 discover_timA_fine(ctrl
, channel
, slotrank
, upperA
);
1430 post_timA_change(ctrl
, channel
, slotrank
, &mnmx
);
1431 pre_timA_change(ctrl
, channel
, slotrank
, &mnmx
);
1434 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
-= mnmx
.timA_min_high
* 0x40;
1436 ctrl
->timings
[channel
][slotrank
].val_4028
-= mnmx
.timA_min_high
;
1437 printram("4028 -= %d;\n", mnmx
.timA_min_high
);
1439 post_timA_change(ctrl
, channel
, slotrank
, &mnmx
);
1441 printram("4/8: %d, %d, %x, %x\n", channel
, slotrank
,
1442 ctrl
->timings
[channel
][slotrank
].val_4024
,
1443 ctrl
->timings
[channel
][slotrank
].val_4028
);
1445 printram("final results:\n");
1447 printram("Aval: %d, %d, %d: %x\n", channel
, slotrank
,
1449 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
);
1451 write32(DEFAULT_MCHBAR
+ 0x3400, 0);
1456 FOR_ALL_POPULATED_CHANNELS
{
1457 program_timings(ctrl
, channel
);
1459 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
1460 write32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
1466 static void test_timC(ramctr_timing
* ctrl
, int channel
, int slotrank
)
1471 write32(DEFAULT_MCHBAR
+ 0x4340 + 0x400 * channel
+ 4 * lane
, 0);
1472 read32(DEFAULT_MCHBAR
+ 0x4140 + 0x400 * channel
+ 4 * lane
);
1477 /* DRAM command ACT */
1478 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f006);
1479 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1480 (max((ctrl
->tFAW
>> 2) + 1, ctrl
->tRRD
) << 10)
1481 | 4 | (ctrl
->tRCD
<< 16));
1483 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1484 (slotrank
<< 24) | (6 << 16));
1486 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x244);
1488 /* DRAM command NOP */
1489 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f207);
1490 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
, 0x8041001);
1491 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
1492 (slotrank
<< 24) | 8);
1493 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0x3e0);
1495 /* DRAM command WR */
1496 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
, 0x1f201);
1497 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
, 0x80411f4);
1498 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
, (slotrank
<< 24));
1499 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0x242);
1501 /* DRAM command NOP */
1502 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
, 0x1f207);
1503 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
1504 0x8000c01 | ((ctrl
->CWL
+ ctrl
->tWTR
+ 5) << 16));
1505 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
1506 (slotrank
<< 24) | 8);
1507 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0x3e0);
1509 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0xc0001);
1513 /* DRAM command PREA */
1514 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f002);
1515 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1516 0xc01 | (ctrl
->tRP
<< 16));
1517 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1518 (slotrank
<< 24) | 0x60400);
1519 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x240);
1521 /* DRAM command ACT */
1522 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f006);
1523 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
1524 (max(ctrl
->tRRD
, (ctrl
->tFAW
>> 2) + 1) << 10)
1525 | 8 | (ctrl
->CAS
<< 16));
1527 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
1528 (slotrank
<< 24) | 0x60000);
1530 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0x244);
1532 /* DRAM command RD */
1533 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
, 0x1f105);
1534 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
1535 0x40011f4 | (max(ctrl
->tRTP
, 8) << 16));
1536 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
, (slotrank
<< 24));
1537 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0x242);
1539 /* DRAM command PREA */
1540 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
, 0x1f002);
1541 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
1542 0xc01 | (ctrl
->tRP
<< 16));
1543 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
1544 (slotrank
<< 24) | 0x60400);
1545 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0x240);
1546 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0xc0001);
1550 static int discover_timC(ramctr_timing
*ctrl
, int channel
, int slotrank
)
1553 int statistics
[NUM_LANES
][MAX_TIMC
+ 1];
1558 /* DRAM command PREA */
1559 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f002);
1560 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1561 0xc01 | (ctrl
->tRP
<< 16));
1562 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1563 (slotrank
<< 24) | 0x60400);
1564 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x240);
1565 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 1);
1567 for (timC
= 0; timC
<= MAX_TIMC
; timC
++) {
1568 FOR_ALL_LANES ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1570 program_timings(ctrl
, channel
);
1572 test_timC(ctrl
, channel
, slotrank
);
1575 statistics
[lane
][timC
] =
1576 read32(DEFAULT_MCHBAR
+ 0x4340 + 4 * lane
+
1582 get_longest_zero_run(statistics
[lane
], MAX_TIMC
+ 1);
1583 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timC
= rn
.middle
;
1585 printk(BIOS_EMERG
, "timC discovery failed: %d, %d, %d\n",
1586 channel
, slotrank
, lane
);
1589 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1590 channel
, slotrank
, lane
, rn
.start
, rn
.middle
, rn
.end
);
1595 static int get_precedening_channels(ramctr_timing
* ctrl
, int target_channel
)
1597 int channel
, ret
= 0;
1598 FOR_ALL_POPULATED_CHANNELS
if (channel
< target_channel
)
1603 static void fill_pattern0(ramctr_timing
* ctrl
, int channel
, u32 a
, u32 b
)
1606 unsigned channel_offset
=
1607 get_precedening_channels(ctrl
, channel
) * 0x40;
1608 for (j
= 0; j
< 16; j
++)
1609 write32((void *)(0x04000000 + channel_offset
+ 4 * j
), j
& 2 ? b
: a
);
1613 static int num_of_channels(const ramctr_timing
* ctrl
)
1617 FOR_ALL_POPULATED_CHANNELS ret
++;
1621 static void fill_pattern1(ramctr_timing
* ctrl
, int channel
)
1624 unsigned channel_offset
=
1625 get_precedening_channels(ctrl
, channel
) * 0x40;
1626 unsigned channel_step
= 0x40 * num_of_channels(ctrl
);
1627 for (j
= 0; j
< 16; j
++)
1628 write32((void *)(0x04000000 + channel_offset
+ j
* 4), 0xffffffff);
1629 for (j
= 0; j
< 16; j
++)
1630 write32((void *)(0x04000000 + channel_offset
+ channel_step
+ j
* 4), 0);
1634 static void precharge(ramctr_timing
* ctrl
)
1636 int channel
, slotrank
, lane
;
1638 FOR_ALL_POPULATED_CHANNELS
{
1639 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
1640 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].falling
=
1642 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].rising
=
1646 program_timings(ctrl
, channel
);
1648 FOR_ALL_POPULATED_RANKS
{
1652 * write MR3 MPR enable
1653 * in this mode only RD and RDA are allowed
1654 * all reads return a predefined pattern */
1655 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
,
1657 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1658 0xc01 | (ctrl
->tMOD
<< 16));
1659 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1660 (slotrank
<< 24) | 0x360004);
1661 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
1663 /* DRAM command RD */
1664 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
,
1666 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
1668 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
1669 (slotrank
<< 24) | 0);
1670 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
1672 /* DRAM command RD */
1673 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
,
1675 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
1676 0x1001 | ((ctrl
->CAS
+ 8) << 16));
1677 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
1678 (slotrank
<< 24) | 0x60000);
1679 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0);
1682 * write MR3 MPR disable */
1683 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
,
1685 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
1686 0xc01 | (ctrl
->tMOD
<< 16));
1687 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
1688 (slotrank
<< 24) | 0x360000);
1689 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0);
1690 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
,
1696 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
1697 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].falling
=
1699 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].rising
=
1703 program_timings(ctrl
, channel
);
1705 FOR_ALL_POPULATED_RANKS
{
1708 * write MR3 MPR enable
1709 * in this mode only RD and RDA are allowed
1710 * all reads return a predefined pattern */
1711 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
,
1713 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1714 0xc01 | (ctrl
->tMOD
<< 16));
1715 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1716 (slotrank
<< 24) | 0x360004);
1717 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
1719 /* DRAM command RD */
1720 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
,
1722 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
1724 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
1725 (slotrank
<< 24) | 0);
1726 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
1728 /* DRAM command RD */
1729 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
,
1731 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
1732 0x1001 | ((ctrl
->CAS
+ 8) << 16));
1733 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
1734 (slotrank
<< 24) | 0x60000);
1735 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0);
1738 * write MR3 MPR disable */
1739 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
,
1741 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
1742 0xc01 | (ctrl
->tMOD
<< 16));
1744 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
1745 (slotrank
<< 24) | 0x360000);
1746 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0);
1748 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
,
1755 static void test_timB(ramctr_timing
* ctrl
, int channel
, int slotrank
)
1757 /* enable DQs on this slotrank */
1758 write_mrreg(ctrl
, channel
, slotrank
, 1,
1759 0x80 | make_mr1(ctrl
, slotrank
, channel
));
1762 /* DRAM command NOP */
1763 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f207);
1764 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1765 0x8000c01 | ((ctrl
->CWL
+ ctrl
->tWLO
) << 16));
1766 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1767 8 | (slotrank
<< 24));
1768 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
1770 /* DRAM command NOP */
1771 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f107);
1772 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
1773 0x4000c01 | ((ctrl
->CAS
+ 38) << 16));
1774 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
1775 (slotrank
<< 24) | 4);
1776 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
1778 write32(DEFAULT_MCHBAR
+ 0x400 * channel
+ 0x4284, 0x40001);
1781 /* disable DQs on this slotrank */
1782 write_mrreg(ctrl
, channel
, slotrank
, 1,
1783 0x1080 | make_mr1(ctrl
, slotrank
, channel
));
1786 static int discover_timB(ramctr_timing
*ctrl
, int channel
, int slotrank
)
1789 int statistics
[NUM_LANES
][128];
1792 write32(DEFAULT_MCHBAR
+ 0x3400, 0x108052 | (slotrank
<< 2));
1794 for (timB
= 0; timB
< 128; timB
++) {
1796 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timB
= timB
;
1798 program_timings(ctrl
, channel
);
1800 test_timB(ctrl
, channel
, slotrank
);
1803 statistics
[lane
][timB
] =
1805 (DEFAULT_MCHBAR
+ lane_registers
[lane
] +
1806 channel
* 0x100 + 4 + ((timB
/ 32) & 1) * 4)
1807 >> (timB
% 32)) & 1);
1811 struct run rn
= get_longest_zero_run(statistics
[lane
], 128);
1812 /* timC is a direct function of timB's 6 LSBs.
1813 * Some tests increments the value of timB by a small value,
1814 * which might cause the 6bit value to overflow, if it's close
1815 * to 0x3F. Increment the value by a small offset if it's likely
1816 * to overflow, to make sure it won't overflow while running
1817 * tests and bricks the system due to a non matching timC.
1819 * TODO: find out why some tests (edge write discovery)
1820 * increment timB. */
1821 if ((rn
.start
& 0x3F) == 0x3E)
1823 else if ((rn
.start
& 0x3F) == 0x3F)
1825 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timB
= rn
.start
;
1827 printk(BIOS_EMERG
, "timB discovery failed: %d, %d, %d\n",
1828 channel
, slotrank
, lane
);
1831 printram("timB: %d, %d, %d: 0x%02x-0x%02x-0x%02x\n",
1832 channel
, slotrank
, lane
, rn
.start
, rn
.middle
, rn
.end
);
1837 static int get_timB_high_adjust(u64 val
)
1842 if (val
== 0xffffffffffffffffLL
)
1845 if (val
>= 0xf000000000000000LL
) {
1846 /* needs negative adjustment */
1847 for (i
= 0; i
< 8; i
++)
1848 if (val
<< (8 * (7 - i
) + 4))
1851 /* needs positive adjustment */
1852 for (i
= 0; i
< 8; i
++)
1853 if (val
>> (8 * (7 - i
) + 4))
1859 static void adjust_high_timB(ramctr_timing
* ctrl
)
1861 int channel
, slotrank
, lane
, old
;
1862 write32(DEFAULT_MCHBAR
+ 0x3400, 0x200);
1863 FOR_ALL_POPULATED_CHANNELS
{
1864 fill_pattern1(ctrl
, channel
);
1865 write32(DEFAULT_MCHBAR
+ 0x4288 + (channel
<< 10), 1);
1867 FOR_ALL_POPULATED_CHANNELS FOR_ALL_POPULATED_RANKS
{
1869 write32(DEFAULT_MCHBAR
+ 0x4288 + 0x400 * channel
, 0x10001);
1873 /* DRAM command ACT */
1874 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f006);
1875 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1876 0xc01 | (ctrl
->tRCD
<< 16));
1877 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1878 (slotrank
<< 24) | 0x60000);
1879 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
1881 /* DRAM command NOP */
1882 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f207);
1883 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
, 0x8040c01);
1884 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
1885 (slotrank
<< 24) | 0x8);
1886 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0x3e0);
1888 /* DRAM command WR */
1889 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
, 0x1f201);
1890 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
, 0x8041003);
1891 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
1893 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0x3e2);
1895 /* DRAM command NOP */
1896 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
, 0x1f207);
1897 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
1898 0x8000c01 | ((ctrl
->CWL
+ ctrl
->tWTR
+ 5) << 16));
1899 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
1900 (slotrank
<< 24) | 0x8);
1901 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0x3e0);
1903 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0xc0001);
1907 /* DRAM command PREA */
1908 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f002);
1909 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
1910 0xc01 | ((ctrl
->tRP
) << 16));
1911 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1912 (slotrank
<< 24) | 0x60400);
1913 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x240);
1915 /* DRAM command ACT */
1916 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f006);
1917 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
1918 0xc01 | ((ctrl
->tRCD
) << 16));
1919 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
1920 (slotrank
<< 24) | 0x60000);
1921 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
1923 /* DRAM command RD */
1924 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
, 0x3f105);
1925 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
1928 ctrl
->timings
[channel
][slotrank
].val_4024
+
1929 ctrl
->timings
[channel
][slotrank
].val_4028
) << 16));
1930 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
1931 (slotrank
<< 24) | 0x60008);
1932 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0);
1934 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0x80001);
1938 read32(DEFAULT_MCHBAR
+ lane_registers
[lane
] +
1939 0x100 * channel
+ 4);
1941 ((u64
) read32(DEFAULT_MCHBAR
+ lane_registers
[lane
] +
1942 0x100 * channel
+ 8)) << 32;
1943 old
= ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timB
;
1944 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timB
+=
1945 get_timB_high_adjust(res
) * 64;
1947 printram("High adjust %d:%016llx\n", lane
, res
);
1948 printram("Bval+: %d, %d, %d, %x -> %x\n", channel
,
1949 slotrank
, lane
, old
,
1950 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
1954 write32(DEFAULT_MCHBAR
+ 0x3400, 0);
1957 static void write_op(ramctr_timing
* ctrl
, int channel
)
1963 /* choose an existing rank. */
1964 slotrank
= !(ctrl
->rankmap
[channel
] & 1) ? 2 : 0;
1966 /* DRAM command ACT */
1967 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x0f003);
1968 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
, 0x41001);
1970 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
1971 (slotrank
<< 24) | 0x60000);
1973 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x3e0);
1975 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 1);
1979 /* Compensate the skew between CMD/ADDR/CLK and DQ/DQS lanes.
1980 * DDR3 adopted the fly-by topology. The data and strobes signals reach
1981 * the chips at different times with respect to command, address and
1983 * By delaying either all DQ/DQs or all CMD/ADDR/CLK signals, a full phase
1984 * shift can be introduced.
1985 * It is assumed that the CLK/ADDR/CMD signals have the same routing delay.
1987 * To find the required phase shift the DRAM is placed in "write leveling" mode.
1988 * In this mode the DRAM-chip samples the CLK on every DQS edge and feeds back the
1989 * sampled value on the data lanes (DQs).
1991 int write_training(ramctr_timing
* ctrl
)
1993 int channel
, slotrank
, lane
;
1996 FOR_ALL_POPULATED_CHANNELS
1997 write32(DEFAULT_MCHBAR
+ 0x4008 + 0x400 * channel
,
1998 read32(DEFAULT_MCHBAR
+ 0x4008 +
1999 0x400 * channel
) | 0x8000000);
2001 FOR_ALL_POPULATED_CHANNELS
{
2002 write_op(ctrl
, channel
);
2003 write32(DEFAULT_MCHBAR
+ 0x4020 + 0x400 * channel
,
2004 read32(DEFAULT_MCHBAR
+ 0x4020 +
2005 0x400 * channel
) | 0x200000);
2008 /* refresh disable */
2009 write32(DEFAULT_MCHBAR
+ 0x5030, read32(DEFAULT_MCHBAR
+ 0x5030) & ~8);
2010 FOR_ALL_POPULATED_CHANNELS
{
2011 write_op(ctrl
, channel
);
2014 /* enable write leveling on all ranks
2015 * disable all DQ outputs
2016 * only NOP is allowed in this mode */
2018 FOR_ALL_POPULATED_RANKS
2019 write_mrreg(ctrl
, channel
, slotrank
, 1,
2020 make_mr1(ctrl
, slotrank
, channel
) | 0x1080);
2022 write32(DEFAULT_MCHBAR
+ 0x3400, 0x108052);
2026 /* set any valid value for timB, it gets corrected later */
2027 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
2028 err
= discover_timB(ctrl
, channel
, slotrank
);
2033 /* disable write leveling on all ranks */
2034 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
2035 write_mrreg(ctrl
, channel
,
2036 slotrank
, 1, make_mr1(ctrl
, slotrank
, channel
));
2038 write32(DEFAULT_MCHBAR
+ 0x3400, 0);
2040 FOR_ALL_POPULATED_CHANNELS
2043 /* refresh enable */
2044 write32(DEFAULT_MCHBAR
+ 0x5030, read32(DEFAULT_MCHBAR
+ 0x5030) | 8);
2046 FOR_ALL_POPULATED_CHANNELS
{
2047 write32(DEFAULT_MCHBAR
+ 0x4020 + 0x400 * channel
,
2048 ~0x00200000 & read32(DEFAULT_MCHBAR
+ 0x4020 +
2050 read32(DEFAULT_MCHBAR
+ 0x428c + 0x400 * channel
);
2053 /* DRAM command ZQCS */
2054 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x0f003);
2055 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
, 0x659001);
2056 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
, 0x60000);
2057 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x3e0);
2059 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 1);
2069 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2070 read32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
+ 4 * lane
);
2071 write32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
+ 4 * lane
,
2075 FOR_ALL_POPULATED_CHANNELS
{
2076 fill_pattern0(ctrl
, channel
, 0xaaaaaaaa, 0x55555555);
2077 write32(DEFAULT_MCHBAR
+ 0x4288 + (channel
<< 10), 0);
2080 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
2081 err
= discover_timC(ctrl
, channel
, slotrank
);
2086 FOR_ALL_POPULATED_CHANNELS
2087 program_timings(ctrl
, channel
);
2089 /* measure and adjust timB timings */
2090 adjust_high_timB(ctrl
);
2092 FOR_ALL_POPULATED_CHANNELS
2093 program_timings(ctrl
, channel
);
2095 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2096 read32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
+ 4 * lane
);
2097 write32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
+ 4 * lane
,
2103 static int test_320c(ramctr_timing
* ctrl
, int channel
, int slotrank
)
2105 struct ram_rank_timings saved_rt
= ctrl
->timings
[channel
][slotrank
];
2111 for (timC_delta
= -5; timC_delta
<= 5; timC_delta
++) {
2113 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timC
=
2114 saved_rt
.lanes
[lane
].timC
+ timC_delta
;
2116 program_timings(ctrl
, channel
);
2118 write32(DEFAULT_MCHBAR
+ 4 * lane
+ 0x4f40, 0);
2121 write32(DEFAULT_MCHBAR
+ 0x4288 + 0x400 * channel
, 0x1f);
2124 /* DRAM command ACT */
2125 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f006);
2126 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
2127 ((max(ctrl
->tRRD
, (ctrl
->tFAW
>> 2) + 1)) << 10)
2128 | 8 | (ctrl
->tRCD
<< 16));
2130 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
2131 (slotrank
<< 24) | ctr
| 0x60000);
2133 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x244);
2134 /* DRAM command WR */
2135 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f201);
2136 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
2137 0x8001020 | ((ctrl
->CWL
+ ctrl
->tWTR
+ 8) << 16));
2138 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
2140 write32(DEFAULT_MCHBAR
+ 0x4244 + 0x400 * channel
, 0x389abcd);
2141 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0x20e42);
2143 /* DRAM command RD */
2144 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
, 0x1f105);
2145 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
2146 0x4001020 | (max(ctrl
->tRTP
, 8) << 16));
2147 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
2149 write32(DEFAULT_MCHBAR
+ 0x4248 + 0x400 * channel
, 0x389abcd);
2150 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0x20e42);
2152 /* DRAM command PRE */
2153 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
, 0x1f002);
2154 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
, 0xf1001);
2155 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
2156 (slotrank
<< 24) | 0x60400);
2157 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0x240);
2159 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0xc0001);
2163 read32(DEFAULT_MCHBAR
+ 0x4340 + 4 * lane
+
2167 lanes_ok
|= 1 << lane
;
2170 if (lanes_ok
== ((1 << NUM_LANES
) - 1))
2174 ctrl
->timings
[channel
][slotrank
] = saved_rt
;
2176 return lanes_ok
!= ((1 << NUM_LANES
) - 1);
2179 #include "raminit_patterns.h"
2181 static void fill_pattern5(ramctr_timing
* ctrl
, int channel
, int patno
)
2184 unsigned channel_offset
=
2185 get_precedening_channels(ctrl
, channel
) * 0x40;
2186 unsigned channel_step
= 0x40 * num_of_channels(ctrl
);
2189 u8 base8
= 0x80 >> ((patno
- 1) % 8);
2190 u32 base
= base8
| (base8
<< 8) | (base8
<< 16) | (base8
<< 24);
2191 for (i
= 0; i
< 32; i
++) {
2192 for (j
= 0; j
< 16; j
++) {
2193 u32 val
= use_base
[patno
- 1][i
] & (1 << (j
/ 2)) ? base
: 0;
2194 if (invert
[patno
- 1][i
] & (1 << (j
/ 2)))
2196 write32((void *)(0x04000000 + channel_offset
+ i
* channel_step
+
2202 for (i
= 0; i
< sizeof(pattern
) / sizeof(pattern
[0]); i
++) {
2203 for (j
= 0; j
< 16; j
++)
2204 write32((void *)(0x04000000 + channel_offset
+ i
* channel_step
+
2205 j
* 4), pattern
[i
][j
]);
2211 static void reprogram_320c(ramctr_timing
* ctrl
)
2213 int channel
, slotrank
;
2215 FOR_ALL_POPULATED_CHANNELS
{
2218 /* choose an existing rank. */
2219 slotrank
= !(ctrl
->rankmap
[channel
] & 1) ? 2 : 0;
2221 /* DRAM command ZQCS */
2222 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x0f003);
2223 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
, 0x41001);
2225 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
2226 (slotrank
<< 24) | 0x60000);
2228 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x3e0);
2230 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 1);
2232 write32(DEFAULT_MCHBAR
+ 0x4020 + 0x400 * channel
,
2233 read32(DEFAULT_MCHBAR
+ 0x4020 +
2234 0x400 * channel
) | 0x200000);
2237 /* refresh disable */
2238 write32(DEFAULT_MCHBAR
+ 0x5030, read32(DEFAULT_MCHBAR
+ 0x5030) & ~8);
2239 FOR_ALL_POPULATED_CHANNELS
{
2242 /* choose an existing rank. */
2243 slotrank
= !(ctrl
->rankmap
[channel
] & 1) ? 2 : 0;
2245 /* DRAM command ZQCS */
2246 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x0f003);
2247 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
, 0x41001);
2249 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
2250 (slotrank
<< 24) | 0x60000);
2252 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x3e0);
2254 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 1);
2259 dram_jedecreset(ctrl
);
2261 dram_mrscommands(ctrl
);
2266 #define MIN_C320C_LEN 13
2268 static int try_cmd_stretch(ramctr_timing
*ctrl
, int channel
, int cmd_stretch
)
2270 struct ram_rank_timings saved_timings
[NUM_CHANNELS
][NUM_SLOTRANKS
];
2273 int stat
[NUM_SLOTRANKS
][256];
2276 printram("Trying cmd_stretch %d on channel %d\n", cmd_stretch
, channel
);
2278 FOR_ALL_POPULATED_RANKS
{
2279 saved_timings
[channel
][slotrank
] =
2280 ctrl
->timings
[channel
][slotrank
];
2283 ctrl
->cmd_stretch
[channel
] = cmd_stretch
;
2285 MCHBAR32(0x4004 + 0x400 * channel
) =
2289 | (ctrl
->tWTR
<< 12)
2290 | (ctrl
->tFAW
<< 16)
2292 | (ctrl
->cmd_stretch
[channel
] << 30);
2294 if (ctrl
->cmd_stretch
[channel
] == 2)
2296 else if (ctrl
->cmd_stretch
[channel
] == 0)
2299 FOR_ALL_POPULATED_RANKS
{
2300 ctrl
->timings
[channel
][slotrank
].val_4024
-= delta
;
2303 for (c320c
= -127; c320c
<= 127; c320c
++) {
2304 FOR_ALL_POPULATED_RANKS
{
2305 ctrl
->timings
[channel
][slotrank
].val_320c
= c320c
;
2307 program_timings(ctrl
, channel
);
2308 reprogram_320c(ctrl
);
2309 FOR_ALL_POPULATED_RANKS
{
2310 stat
[slotrank
][c320c
+ 127] =
2311 test_320c(ctrl
, channel
, slotrank
);
2314 FOR_ALL_POPULATED_RANKS
{
2316 get_longest_zero_run(stat
[slotrank
], 255);
2317 ctrl
->timings
[channel
][slotrank
].val_320c
=
2319 printram("cmd_stretch: %d, %d: 0x%02x-0x%02x-0x%02x\n",
2320 channel
, slotrank
, rn
.start
, rn
.middle
, rn
.end
);
2321 if (rn
.all
|| rn
.length
< MIN_C320C_LEN
) {
2322 FOR_ALL_POPULATED_RANKS
{
2323 ctrl
->timings
[channel
][slotrank
] =
2324 saved_timings
[channel
][slotrank
];
2333 /* Adjust CMD phase shift and try multiple command rates.
2334 * A command rate of 2T doubles the time needed for address and
2335 * command decode. */
2336 int command_training(ramctr_timing
*ctrl
)
2340 FOR_ALL_POPULATED_CHANNELS
{
2341 fill_pattern5(ctrl
, channel
, 0);
2342 write32(DEFAULT_MCHBAR
+ 0x4288 + 0x400 * channel
, 0x1f);
2345 FOR_ALL_POPULATED_CHANNELS
{
2349 * Dual DIMM per channel:
2350 * Issue: While c320c discovery seems to succeed raminit
2351 * will fail in write training.
2352 * Workaround: Skip 1T in dual DIMM mode, that's only
2353 * supported by a few DIMMs.
2354 * Only try 1T mode for XMP DIMMs that request it in dual DIMM
2357 * Single DIMM per channel:
2358 * Try command rate 1T and 2T
2360 cmdrate
= ((ctrl
->rankmap
[channel
] & 0x5) == 0x5);
2362 /* XMP gives the CMD rate in clock ticks, not ns */
2363 cmdrate
= MIN(DIV_ROUND_UP(ctrl
->tCMD
, 256) - 1, 1);
2365 for (; cmdrate
< 2; cmdrate
++) {
2366 err
= try_cmd_stretch(ctrl
, channel
, cmdrate
<< 1);
2373 printk(BIOS_EMERG
, "c320c discovery failed\n");
2377 printram("Using CMD rate %uT on channel %u\n",
2378 cmdrate
+ 1, channel
);
2381 FOR_ALL_POPULATED_CHANNELS
2382 program_timings(ctrl
, channel
);
2384 reprogram_320c(ctrl
);
2389 static int discover_edges_real(ramctr_timing
*ctrl
, int channel
, int slotrank
,
2393 int statistics
[NUM_LANES
][MAX_EDGE_TIMING
+ 1];
2396 for (edge
= 0; edge
<= MAX_EDGE_TIMING
; edge
++) {
2398 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].rising
=
2400 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].falling
=
2403 program_timings(ctrl
, channel
);
2406 write32(DEFAULT_MCHBAR
+ 0x4340 + 0x400 * channel
+
2408 read32(DEFAULT_MCHBAR
+ 0x400 * channel
+ 4 * lane
+
2414 * write MR3 MPR enable
2415 * in this mode only RD and RDA are allowed
2416 * all reads return a predefined pattern */
2417 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f000);
2418 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
2419 (0xc01 | (ctrl
->tMOD
<< 16)));
2420 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
2421 (slotrank
<< 24) | 0x360004);
2422 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
2424 /* DRAM command RD */
2425 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f105);
2426 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
, 0x40411f4);
2427 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
2429 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
2431 /* DRAM command RD */
2432 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
, 0x1f105);
2433 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
2434 0x1001 | ((ctrl
->CAS
+ 8) << 16));
2435 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
2436 (slotrank
<< 24) | 0x60000);
2437 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0);
2440 * MR3 disable MPR */
2441 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
, 0x1f000);
2442 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
2443 (0xc01 | (ctrl
->tMOD
<< 16)));
2444 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
2445 (slotrank
<< 24) | 0x360000);
2446 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0);
2448 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
, 0xc0001);
2453 statistics
[lane
][edge
] =
2454 read32(DEFAULT_MCHBAR
+ 0x4340 + 0x400 * channel
+
2460 get_longest_zero_run(statistics
[lane
], MAX_EDGE_TIMING
+ 1);
2461 edges
[lane
] = rn
.middle
;
2463 printk(BIOS_EMERG
, "edge discovery failed: %d, %d, %d\n",
2464 channel
, slotrank
, lane
);
2467 printram("eval %d, %d, %d: %02x\n", channel
, slotrank
,
2473 int discover_edges(ramctr_timing
*ctrl
)
2475 int falling_edges
[NUM_CHANNELS
][NUM_SLOTRANKS
][NUM_LANES
];
2476 int rising_edges
[NUM_CHANNELS
][NUM_SLOTRANKS
][NUM_LANES
];
2477 int channel
, slotrank
, lane
;
2480 write32(DEFAULT_MCHBAR
+ 0x3400, 0);
2484 FOR_ALL_POPULATED_CHANNELS FOR_ALL_LANES
{
2485 write32(DEFAULT_MCHBAR
+ 4 * lane
+
2486 0x400 * channel
+ 0x4080, 0);
2489 FOR_ALL_POPULATED_CHANNELS
{
2490 fill_pattern0(ctrl
, channel
, 0, 0);
2491 write32(DEFAULT_MCHBAR
+ 0x4288 + (channel
<< 10), 0);
2493 read32(DEFAULT_MCHBAR
+ 0x400 * channel
+
2497 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2498 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].falling
=
2500 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].rising
=
2504 program_timings(ctrl
, channel
);
2506 FOR_ALL_POPULATED_RANKS
{
2511 * write MR3 MPR enable
2512 * in this mode only RD and RDA are allowed
2513 * all reads return a predefined pattern */
2514 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
,
2516 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
2517 0xc01 | (ctrl
->tMOD
<< 16));
2518 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
2519 (slotrank
<< 24) | 0x360004);
2520 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
2522 /* DRAM command RD */
2523 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
,
2525 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
2527 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
2528 (slotrank
<< 24) | 0);
2529 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
2531 /* DRAM command RD */
2532 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
,
2534 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
2535 0x1001 | ((ctrl
->CAS
+ 8) << 16));
2536 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
2537 (slotrank
<< 24) | 0x60000);
2538 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0);
2541 * MR3 disable MPR */
2542 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
,
2544 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
2545 0xc01 | (ctrl
->tMOD
<< 16));
2546 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
2547 (slotrank
<< 24) | 0x360000);
2548 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0);
2549 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
,
2555 /* XXX: check any measured value ? */
2557 FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2558 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].falling
=
2560 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].rising
=
2564 program_timings(ctrl
, channel
);
2566 FOR_ALL_POPULATED_RANKS
{
2571 * write MR3 MPR enable
2572 * in this mode only RD and RDA are allowed
2573 * all reads return a predefined pattern */
2574 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
,
2576 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
2577 0xc01 | (ctrl
->tMOD
<< 16));
2578 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
2579 (slotrank
<< 24) | 0x360004);
2580 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0);
2582 /* DRAM command RD */
2583 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
,
2585 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
2587 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
2588 (slotrank
<< 24) | 0);
2589 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
, 0);
2591 /* DRAM command RD */
2592 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
,
2594 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
2595 0x1001 | ((ctrl
->CAS
+ 8) << 16));
2596 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
2597 (slotrank
<< 24) | 0x60000);
2598 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
, 0);
2601 * MR3 disable MPR */
2602 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
,
2604 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
2605 0xc01 | (ctrl
->tMOD
<< 16));
2606 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
2607 (slotrank
<< 24) | 0x360000);
2608 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0);
2610 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
,
2615 /* XXX: check any measured value ? */
2618 write32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
+
2620 ~read32(DEFAULT_MCHBAR
+ 0x4040 +
2621 0x400 * channel
+ lane
* 4) & 0xff);
2624 fill_pattern0(ctrl
, channel
, 0, 0xffffffff);
2625 write32(DEFAULT_MCHBAR
+ 0x4288 + (channel
<< 10), 0);
2628 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
2629 write32(DEFAULT_MCHBAR
+ 0x4eb0, 0x300);
2630 printram("discover falling edges:\n[%x] = %x\n", 0x4eb0, 0x300);
2632 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
2633 err
= discover_edges_real(ctrl
, channel
, slotrank
,
2634 falling_edges
[channel
][slotrank
]);
2639 write32(DEFAULT_MCHBAR
+ 0x4eb0, 0x200);
2640 printram("discover rising edges:\n[%x] = %x\n", 0x4eb0, 0x200);
2642 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
2643 err
= discover_edges_real(ctrl
, channel
, slotrank
,
2644 rising_edges
[channel
][slotrank
]);
2649 write32(DEFAULT_MCHBAR
+ 0x4eb0, 0);
2651 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2652 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].falling
=
2653 falling_edges
[channel
][slotrank
][lane
];
2654 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].rising
=
2655 rising_edges
[channel
][slotrank
][lane
];
2658 FOR_ALL_POPULATED_CHANNELS
{
2659 program_timings(ctrl
, channel
);
2662 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2663 write32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
+ 4 * lane
,
2669 static int discover_edges_write_real(ramctr_timing
*ctrl
, int channel
,
2670 int slotrank
, int *edges
)
2673 u32 raw_statistics
[MAX_EDGE_TIMING
+ 1];
2674 int statistics
[MAX_EDGE_TIMING
+ 1];
2675 const int reg3000b24
[] = { 0, 0xc, 0x2c };
2677 int lower
[NUM_LANES
];
2678 int upper
[NUM_LANES
];
2683 upper
[lane
] = MAX_EDGE_TIMING
;
2686 for (i
= 0; i
< 3; i
++) {
2687 write32(DEFAULT_MCHBAR
+ 0x3000 + 0x100 * channel
,
2688 reg3000b24
[i
] << 24);
2689 printram("[%x] = 0x%08x\n",
2690 0x3000 + 0x100 * channel
, reg3000b24
[i
] << 24);
2691 for (pat
= 0; pat
< NUM_PATTERNS
; pat
++) {
2692 fill_pattern5(ctrl
, channel
, pat
);
2693 write32(DEFAULT_MCHBAR
+ 0x4288 + 0x400 * channel
, 0x1f);
2694 printram("using pattern %d\n", pat
);
2695 for (edge
= 0; edge
<= MAX_EDGE_TIMING
; edge
++) {
2697 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
2699 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].
2702 program_timings(ctrl
, channel
);
2705 write32(DEFAULT_MCHBAR
+ 0x4340 +
2706 0x400 * channel
+ 4 * lane
, 0);
2707 read32(DEFAULT_MCHBAR
+ 0x400 * channel
+
2712 /* DRAM command ACT */
2713 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
,
2715 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
2716 0x4 | (ctrl
->tRCD
<< 16)
2717 | (max(ctrl
->tRRD
, (ctrl
->tFAW
>> 2) + 1) <<
2719 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
2720 (slotrank
<< 24) | 0x60000);
2721 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
,
2724 /* DRAM command WR */
2725 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
,
2727 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
2728 0x8005020 | ((ctrl
->tWTR
+ ctrl
->CWL
+ 8) <<
2730 write32(DEFAULT_MCHBAR
+ 0x4204 + 0x400 * channel
,
2732 write32(DEFAULT_MCHBAR
+ 0x4214 + 0x400 * channel
,
2735 /* DRAM command RD */
2736 write32(DEFAULT_MCHBAR
+ 0x4228 + 0x400 * channel
,
2738 write32(DEFAULT_MCHBAR
+ 0x4238 + 0x400 * channel
,
2739 0x4005020 | (max(ctrl
->tRTP
, 8) << 16));
2740 write32(DEFAULT_MCHBAR
+ 0x4208 + 0x400 * channel
,
2742 write32(DEFAULT_MCHBAR
+ 0x4218 + 0x400 * channel
,
2745 /* DRAM command PRE */
2746 write32(DEFAULT_MCHBAR
+ 0x422c + 0x400 * channel
,
2748 write32(DEFAULT_MCHBAR
+ 0x423c + 0x400 * channel
,
2749 0xc01 | (ctrl
->tRP
<< 16));
2750 write32(DEFAULT_MCHBAR
+ 0x420c + 0x400 * channel
,
2751 (slotrank
<< 24) | 0x60400);
2752 write32(DEFAULT_MCHBAR
+ 0x421c + 0x400 * channel
, 0);
2754 write32(DEFAULT_MCHBAR
+ 0x4284 + 0x400 * channel
,
2758 read32(DEFAULT_MCHBAR
+ 0x4340 +
2759 0x400 * channel
+ lane
* 4);
2762 raw_statistics
[edge
] =
2763 MCHBAR32(0x436c + 0x400 * channel
);
2767 for (edge
= 0; edge
<= MAX_EDGE_TIMING
; edge
++)
2769 ! !(raw_statistics
[edge
] & (1 << lane
));
2770 rn
= get_longest_zero_run(statistics
,
2771 MAX_EDGE_TIMING
+ 1);
2772 printram("edges: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
2773 channel
, slotrank
, i
, rn
.start
, rn
.middle
,
2774 rn
.end
, rn
.start
+ ctrl
->edge_offset
[i
],
2775 rn
.end
- ctrl
->edge_offset
[i
]);
2777 max(rn
.start
+ ctrl
->edge_offset
[i
], lower
[lane
]);
2779 min(rn
.end
- ctrl
->edge_offset
[i
], upper
[lane
]);
2780 edges
[lane
] = (lower
[lane
] + upper
[lane
]) / 2;
2781 if (rn
.all
|| (lower
[lane
] > upper
[lane
])) {
2782 printk(BIOS_EMERG
, "edge write discovery failed: %d, %d, %d\n",
2783 channel
, slotrank
, lane
);
2790 write32(DEFAULT_MCHBAR
+ 0x3000, 0);
2795 int discover_edges_write(ramctr_timing
*ctrl
)
2797 int falling_edges
[NUM_CHANNELS
][NUM_SLOTRANKS
][NUM_LANES
];
2798 int rising_edges
[NUM_CHANNELS
][NUM_SLOTRANKS
][NUM_LANES
];
2799 int channel
, slotrank
, lane
;
2802 /* FIXME: under some conditions (older chipsets?) vendor BIOS sets both edges to the same value. */
2803 write32(DEFAULT_MCHBAR
+ 0x4eb0, 0x300);
2804 printram("discover falling edges write:\n[%x] = %x\n", 0x4eb0, 0x300);
2806 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
2807 err
= discover_edges_write_real(ctrl
, channel
, slotrank
,
2808 falling_edges
[channel
][slotrank
]);
2813 write32(DEFAULT_MCHBAR
+ 0x4eb0, 0x200);
2814 printram("discover rising edges write:\n[%x] = %x\n", 0x4eb0, 0x200);
2816 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
2817 err
= discover_edges_write_real(ctrl
, channel
, slotrank
,
2818 rising_edges
[channel
][slotrank
]);
2823 write32(DEFAULT_MCHBAR
+ 0x4eb0, 0);
2825 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2826 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].falling
=
2827 falling_edges
[channel
][slotrank
][lane
];
2828 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].rising
=
2829 rising_edges
[channel
][slotrank
][lane
];
2832 FOR_ALL_POPULATED_CHANNELS
2833 program_timings(ctrl
, channel
);
2835 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2836 write32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
+ 4 * lane
,
2842 static void test_timC_write(ramctr_timing
*ctrl
, int channel
, int slotrank
)
2845 /* DRAM command ACT */
2846 write32(DEFAULT_MCHBAR
+ 0x4220 + 0x400 * channel
, 0x1f006);
2847 write32(DEFAULT_MCHBAR
+ 0x4230 + 0x400 * channel
,
2848 (max((ctrl
->tFAW
>> 2) + 1, ctrl
->tRRD
)
2849 << 10) | (ctrl
->tRCD
<< 16) | 4);
2850 write32(DEFAULT_MCHBAR
+ 0x4200 + 0x400 * channel
,
2851 (slotrank
<< 24) | 0x60000);
2852 write32(DEFAULT_MCHBAR
+ 0x4210 + 0x400 * channel
, 0x244);
2854 /* DRAM command WR */
2855 write32(DEFAULT_MCHBAR
+ 0x4224 + 0x400 * channel
, 0x1f201);
2856 write32(DEFAULT_MCHBAR
+ 0x4234 + 0x400 * channel
,
2858 ((ctrl
->tWTR
+ ctrl
->CWL
+ 8) << 16));
2859 write32(DEFAULT_MCHBAR
+ 0x4204 +
2860 0x400 * channel
, (slotrank
<< 24));
2861 write32(DEFAULT_MCHBAR
+ 0x4214 +
2862 0x400 * channel
, 0x242);
2864 /* DRAM command RD */
2865 write32(DEFAULT_MCHBAR
+ 0x4228 +
2866 0x400 * channel
, 0x1f105);
2867 write32(DEFAULT_MCHBAR
+ 0x4238 +
2869 0x40011e0 | (max(ctrl
->tRTP
, 8) << 16));
2870 write32(DEFAULT_MCHBAR
+ 0x4208 +
2871 0x400 * channel
, (slotrank
<< 24));
2872 write32(DEFAULT_MCHBAR
+ 0x4218 +
2873 0x400 * channel
, 0x242);
2875 /* DRAM command PRE */
2876 write32(DEFAULT_MCHBAR
+ 0x422c +
2877 0x400 * channel
, 0x1f002);
2878 write32(DEFAULT_MCHBAR
+ 0x423c +
2880 0x1001 | (ctrl
->tRP
<< 16));
2881 write32(DEFAULT_MCHBAR
+ 0x420c +
2883 (slotrank
<< 24) | 0x60400);
2884 write32(DEFAULT_MCHBAR
+ 0x421c +
2885 0x400 * channel
, 0);
2887 write32(DEFAULT_MCHBAR
+ 0x4284 +
2888 0x400 * channel
, 0xc0001);
2892 int discover_timC_write(ramctr_timing
*ctrl
)
2894 const u8 rege3c_b24
[3] = { 0, 0xf, 0x2f };
2897 int lower
[NUM_CHANNELS
][NUM_SLOTRANKS
][NUM_LANES
];
2898 int upper
[NUM_CHANNELS
][NUM_SLOTRANKS
][NUM_LANES
];
2899 int channel
, slotrank
, lane
;
2901 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2902 lower
[channel
][slotrank
][lane
] = 0;
2903 upper
[channel
][slotrank
][lane
] = MAX_TIMC
;
2906 write32(DEFAULT_MCHBAR
+ 0x4ea8, 1);
2907 printram("discover timC write:\n");
2909 for (i
= 0; i
< 3; i
++)
2910 FOR_ALL_POPULATED_CHANNELS
{
2911 write32(DEFAULT_MCHBAR
+ 0xe3c + (channel
* 0x100),
2912 (rege3c_b24
[i
] << 24)
2913 | (read32(DEFAULT_MCHBAR
+ 0xe3c + (channel
* 0x100))
2916 for (pat
= 0; pat
< NUM_PATTERNS
; pat
++) {
2917 FOR_ALL_POPULATED_RANKS
{
2919 u32 raw_statistics
[MAX_TIMC
+ 1];
2920 int statistics
[MAX_TIMC
+ 1];
2922 /* Make sure rn.start < rn.end */
2923 statistics
[MAX_TIMC
] = 1;
2925 fill_pattern5(ctrl
, channel
, pat
);
2926 write32(DEFAULT_MCHBAR
+ 0x4288 + 0x400 * channel
, 0x1f);
2927 for (timC
= 0; timC
< MAX_TIMC
; timC
++) {
2929 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timC
= timC
;
2930 program_timings(ctrl
, channel
);
2932 test_timC_write (ctrl
, channel
, slotrank
);
2934 raw_statistics
[timC
] =
2935 MCHBAR32(0x436c + 0x400 * channel
);
2939 for (timC
= 0; timC
< MAX_TIMC
; timC
++)
2941 !!(raw_statistics
[timC
] &
2944 rn
= get_longest_zero_run(statistics
,
2947 printk(BIOS_EMERG
, "timC write discovery failed: %d, %d, %d\n",
2948 channel
, slotrank
, lane
);
2951 printram("timC: %d, %d, %d: 0x%02x-0x%02x-0x%02x, 0x%02x-0x%02x\n",
2952 channel
, slotrank
, i
, rn
.start
,
2954 rn
.start
+ ctrl
->timC_offset
[i
],
2955 rn
.end
- ctrl
->timC_offset
[i
]);
2956 lower
[channel
][slotrank
][lane
] =
2957 max(rn
.start
+ ctrl
->timC_offset
[i
],
2958 lower
[channel
][slotrank
][lane
]);
2959 upper
[channel
][slotrank
][lane
] =
2960 min(rn
.end
- ctrl
->timC_offset
[i
],
2961 upper
[channel
][slotrank
][lane
]);
2969 write32(DEFAULT_MCHBAR
+ (channel
* 0x100) + 0xe3c,
2970 0 | (read32(DEFAULT_MCHBAR
+ (channel
* 0x100) + 0xe3c) &
2975 write32(DEFAULT_MCHBAR
+ 0x4ea8, 0);
2979 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
2980 printram("timC %d, %d, %d: %x\n", channel
,
2982 (lower
[channel
][slotrank
][lane
] +
2983 upper
[channel
][slotrank
][lane
]) / 2);
2984 ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timC
=
2985 (lower
[channel
][slotrank
][lane
] +
2986 upper
[channel
][slotrank
][lane
]) / 2;
2988 FOR_ALL_POPULATED_CHANNELS
{
2989 program_timings(ctrl
, channel
);
2994 void normalize_training(ramctr_timing
* ctrl
)
2996 int channel
, slotrank
, lane
;
2999 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
3003 max(ctrl
->timings
[channel
][slotrank
].lanes
[lane
].timA
, mat
);
3004 printram("normalize %d, %d, %d: mat %d\n",
3005 channel
, slotrank
, lane
, mat
);
3007 delta
= (mat
>> 6) - ctrl
->timings
[channel
][slotrank
].val_4028
;
3008 printram("normalize %d, %d, %d: delta %d\n",
3009 channel
, slotrank
, lane
, delta
);
3011 ctrl
->timings
[channel
][slotrank
].val_4024
+= delta
;
3012 ctrl
->timings
[channel
][slotrank
].val_4028
+= delta
;
3015 FOR_ALL_POPULATED_CHANNELS
{
3016 program_timings(ctrl
, channel
);
3020 void write_controller_mr(ramctr_timing
* ctrl
)
3022 int channel
, slotrank
;
3024 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS
{
3025 write32(DEFAULT_MCHBAR
+ 0x0004 + (channel
<< 8) +
3026 lane_registers
[slotrank
], make_mr0(ctrl
, slotrank
));
3027 write32(DEFAULT_MCHBAR
+ 0x0008 + (channel
<< 8) +
3028 lane_registers
[slotrank
],
3029 make_mr1(ctrl
, slotrank
, channel
));
3033 int channel_test(ramctr_timing
*ctrl
)
3035 int channel
, slotrank
, lane
;
3038 FOR_ALL_POPULATED_CHANNELS
3039 if (read32(DEFAULT_MCHBAR
+ 0x42a0 + (channel
<< 10)) & 0xa000) {
3040 printk(BIOS_EMERG
, "Mini channel test failed (1): %d\n",
3044 FOR_ALL_POPULATED_CHANNELS
{
3045 fill_pattern0(ctrl
, channel
, 0x12345678, 0x98765432);
3047 write32(DEFAULT_MCHBAR
+ 0x4288 + (channel
<< 10), 0);
3050 for (slotrank
= 0; slotrank
< 4; slotrank
++)
3052 if (ctrl
->rankmap
[channel
] & (1 << slotrank
)) {
3054 write32(DEFAULT_MCHBAR
+ (0x4f40 + 4 * lane
), 0);
3055 write32(DEFAULT_MCHBAR
+ (0x4d40 + 4 * lane
), 0);
3058 /* DRAM command ACT */
3059 write32(DEFAULT_MCHBAR
+ 0x4220 + (channel
<< 10), 0x0001f006);
3060 write32(DEFAULT_MCHBAR
+ 0x4230 + (channel
<< 10), 0x0028a004);
3061 write32(DEFAULT_MCHBAR
+ 0x4200 + (channel
<< 10),
3062 0x00060000 | (slotrank
<< 24));
3063 write32(DEFAULT_MCHBAR
+ 0x4210 + (channel
<< 10), 0x00000244);
3064 /* DRAM command WR */
3065 write32(DEFAULT_MCHBAR
+ 0x4224 + (channel
<< 10), 0x0001f201);
3066 write32(DEFAULT_MCHBAR
+ 0x4234 + (channel
<< 10), 0x08281064);
3067 write32(DEFAULT_MCHBAR
+ 0x4204 + (channel
<< 10),
3068 0x00000000 | (slotrank
<< 24));
3069 write32(DEFAULT_MCHBAR
+ 0x4214 + (channel
<< 10), 0x00000242);
3070 /* DRAM command RD */
3071 write32(DEFAULT_MCHBAR
+ 0x4228 + (channel
<< 10), 0x0001f105);
3072 write32(DEFAULT_MCHBAR
+ 0x4238 + (channel
<< 10), 0x04281064);
3073 write32(DEFAULT_MCHBAR
+ 0x4208 + (channel
<< 10),
3074 0x00000000 | (slotrank
<< 24));
3075 write32(DEFAULT_MCHBAR
+ 0x4218 + (channel
<< 10), 0x00000242);
3076 /* DRAM command PRE */
3077 write32(DEFAULT_MCHBAR
+ 0x422c + (channel
<< 10), 0x0001f002);
3078 write32(DEFAULT_MCHBAR
+ 0x423c + (channel
<< 10), 0x00280c01);
3079 write32(DEFAULT_MCHBAR
+ 0x420c + (channel
<< 10),
3080 0x00060400 | (slotrank
<< 24));
3081 write32(DEFAULT_MCHBAR
+ 0x421c + (channel
<< 10), 0x00000240);
3082 write32(DEFAULT_MCHBAR
+ 0x4284 + (channel
<< 10), 0x000c0001);
3085 if (read32(DEFAULT_MCHBAR
+ 0x4340 + (channel
<< 10) + 4 * lane
)) {
3086 printk(BIOS_EMERG
, "Mini channel test failed (2): %d, %d, %d\n",
3087 channel
, slotrank
, lane
);
3094 void set_scrambling_seed(ramctr_timing
* ctrl
)
3098 /* FIXME: we hardcode seeds. Do we need to use some PRNG for them?
3099 I don't think so. */
3100 static u32 seeds
[NUM_CHANNELS
][3] = {
3101 {0x00009a36, 0xbafcfdcf, 0x46d1ab68},
3102 {0x00028bfa, 0x53fe4b49, 0x19ed5483}
3104 FOR_ALL_POPULATED_CHANNELS
{
3105 MCHBAR32(0x4020 + 0x400 * channel
) &= ~0x10000000;
3106 MCHBAR32(0x4034 + 0x400 * channel
) = seeds
[channel
][0];
3107 MCHBAR32(0x403c + 0x400 * channel
) = seeds
[channel
][1];
3108 MCHBAR32(0x4038 + 0x400 * channel
) = seeds
[channel
][2];
3114 struct cpuid_result cpures
;
3119 if (IS_SANDY_CPU(cpu
) && (IS_SANDY_CPU_D0(cpu
) || IS_SANDY_CPU_D1(cpu
))) {
3120 MCHBAR32(0x4f8c) = 0x141D1519;
3122 MCHBAR32(0x4f8c) = 0x551D1519;
3126 void prepare_training(ramctr_timing
* ctrl
)
3130 FOR_ALL_POPULATED_CHANNELS
{
3131 // Always drive command bus
3132 MCHBAR32(0x4004 + 0x400 * channel
) |= 0x20000000;
3137 FOR_ALL_POPULATED_CHANNELS
{
3142 void set_4008c(ramctr_timing
* ctrl
)
3144 int channel
, slotrank
;
3146 FOR_ALL_POPULATED_CHANNELS
{
3148 int min_320c
= 10000;
3149 int max_320c
= -10000;
3151 FOR_ALL_POPULATED_RANKS
{
3152 max_320c
= max(ctrl
->timings
[channel
][slotrank
].val_320c
, max_320c
);
3153 min_320c
= min(ctrl
->timings
[channel
][slotrank
].val_320c
, min_320c
);
3156 if (max_320c
- min_320c
> 51)
3159 b20
= ctrl
->ref_card_offset
[channel
];
3161 if (ctrl
->reg_320c_range_threshold
< max_320c
- min_320c
)
3166 dram_odt_stretch(ctrl
, channel
);
3168 write32(DEFAULT_MCHBAR
+ 0x4008 + (channel
<< 10),
3171 | ((ctrl
->ref_card_offset
[channel
] + 2) << 16)
3176 void set_42a0(ramctr_timing
* ctrl
)
3179 FOR_ALL_POPULATED_CHANNELS
{
3180 write32(DEFAULT_MCHBAR
+ (0x42a0 + 0x400 * channel
),
3181 0x00001000 | ctrl
->rankmap
[channel
]);
3182 MCHBAR32(0x4004 + 0x400 * channel
) &= ~0x20000000; // OK
3186 static int encode_5d10(int ns
)
3188 return (ns
+ 499) / 500;
3191 /* FIXME: values in this function should be hardware revision-dependent. */
3192 void final_registers(ramctr_timing
* ctrl
)
3195 int t1_cycles
= 0, t1_ns
= 0, t2_ns
;
3199 write32(DEFAULT_MCHBAR
+ 0x4cd4, 0x00000046);
3201 write32(DEFAULT_MCHBAR
+ 0x400c, (read32(DEFAULT_MCHBAR
+ 0x400c) & 0xFFFFCFFF) | 0x1000); // OK
3202 write32(DEFAULT_MCHBAR
+ 0x440c, (read32(DEFAULT_MCHBAR
+ 0x440c) & 0xFFFFCFFF) | 0x1000); // OK
3203 write32(DEFAULT_MCHBAR
+ 0x4cb0, 0x00000740);
3204 write32(DEFAULT_MCHBAR
+ 0x4380, 0x00000aaa); // OK
3205 write32(DEFAULT_MCHBAR
+ 0x4780, 0x00000aaa); // OK
3206 write32(DEFAULT_MCHBAR
+ 0x4f88, 0x5f7003ff); // OK
3207 write32(DEFAULT_MCHBAR
+ 0x5064, 0x00073000 | ctrl
->reg_5064b0
); // OK
3210 switch (ctrl
->rankmap
[channel
]) {
3211 /* Unpopulated channel. */
3213 write32(DEFAULT_MCHBAR
+ 0x4384 + channel
* 0x400, 0);
3215 /* Only single-ranked dimms. */
3219 write32(DEFAULT_MCHBAR
+ 0x4384 + channel
* 0x400, 0x373131);
3221 /* Dual-ranked dimms present. */
3223 write32(DEFAULT_MCHBAR
+ 0x4384 + channel
* 0x400, 0x9b6ea1);
3228 write32 (DEFAULT_MCHBAR
+ 0x5880, 0xca9171e5);
3229 write32 (DEFAULT_MCHBAR
+ 0x5888,
3230 (read32 (DEFAULT_MCHBAR
+ 0x5888) & ~0xffffff) | 0xe4d5d0);
3231 write32 (DEFAULT_MCHBAR
+ 0x58a8, read32 (DEFAULT_MCHBAR
+ 0x58a8) & ~0x1f);
3232 write32 (DEFAULT_MCHBAR
+ 0x4294,
3233 (read32 (DEFAULT_MCHBAR
+ 0x4294) & ~0x30000)
3235 write32 (DEFAULT_MCHBAR
+ 0x4694,
3236 (read32 (DEFAULT_MCHBAR
+ 0x4694) & ~0x30000)
3239 MCHBAR32(0x5030) |= 1; // OK
3240 MCHBAR32(0x5030) |= 0x80; // OK
3241 MCHBAR32(0x5f18) = 0xfa; // OK
3243 /* Find a populated channel. */
3244 FOR_ALL_POPULATED_CHANNELS
3247 t1_cycles
= ((read32(DEFAULT_MCHBAR
+ 0x4290 + channel
* 0x400) >> 8) & 0xff);
3248 r32
= read32(DEFAULT_MCHBAR
+ 0x5064);
3250 t1_cycles
+= (r32
& 0xfff);
3251 t1_cycles
+= (read32(DEFAULT_MCHBAR
+ channel
* 0x400 + 0x42a4) & 0xfff);
3252 t1_ns
= t1_cycles
* ctrl
->tCK
/ 256 + 544;
3253 if (!(r32
& 0x20000))
3256 t2_ns
= 10 * ((read32(DEFAULT_MCHBAR
+ 0x5f10) >> 8) & 0xfff);
3257 if ( read32(DEFAULT_MCHBAR
+ 0x5f00) & 8 )
3259 t3_ns
= 10 * ((read32(DEFAULT_MCHBAR
+ 0x5f20) >> 8) & 0xfff);
3260 t3_ns
+= 10 * (read32(DEFAULT_MCHBAR
+ 0x5f18) & 0xff);
3266 printk(BIOS_DEBUG
, "t123: %d, %d, %d\n",
3267 t1_ns
, t2_ns
, t3_ns
);
3268 write32 (DEFAULT_MCHBAR
+ 0x5d10,
3269 ((encode_5d10(t1_ns
) + encode_5d10(t2_ns
)) << 16)
3270 | (encode_5d10(t1_ns
) << 8)
3271 | ((encode_5d10(t3_ns
) + encode_5d10(t2_ns
) + encode_5d10(t1_ns
)) << 24)
3272 | (read32(DEFAULT_MCHBAR
+ 0x5d10) & 0xC0C0C0C0)
3276 void restore_timings(ramctr_timing
* ctrl
)
3278 int channel
, slotrank
, lane
;
3280 FOR_ALL_POPULATED_CHANNELS
3281 MCHBAR32(0x4004 + 0x400 * channel
) =
3285 | (ctrl
->tWTR
<< 12)
3286 | (ctrl
->tFAW
<< 16)
3288 | (ctrl
->cmd_stretch
[channel
] << 30);
3292 FOR_ALL_POPULATED_CHANNELS
{
3296 FOR_ALL_CHANNELS FOR_ALL_POPULATED_RANKS FOR_ALL_LANES
{
3297 write32(DEFAULT_MCHBAR
+ 0x4080 + 0x400 * channel
3301 FOR_ALL_POPULATED_CHANNELS
3302 write32(DEFAULT_MCHBAR
+ 0x4008 + 0x400 * channel
,
3303 read32(DEFAULT_MCHBAR
+ 0x4008 +
3304 0x400 * channel
) | 0x8000000);
3306 FOR_ALL_POPULATED_CHANNELS
{
3308 write32(DEFAULT_MCHBAR
+ 0x4020 + 0x400 * channel
,
3309 read32(DEFAULT_MCHBAR
+ 0x4020 +
3310 0x400 * channel
) | 0x200000);
3315 write32(DEFAULT_MCHBAR
+ 0x3400, 0);
3316 write32(DEFAULT_MCHBAR
+ 0x4eb0, 0);
3320 FOR_ALL_POPULATED_CHANNELS
{
3321 program_timings(ctrl
, channel
);
3326 while (!(MCHBAR32(0x5084) & 0x10000));
3328 reg
= MCHBAR32(0x428c);
3329 } while ((reg
& 0x14) == 0);
3331 // Set state of memory controller
3332 MCHBAR32(0x5030) = 0x116;
3333 MCHBAR32(0x4ea0) = 0;
3339 // Set valid rank CKE
3341 reg
= (reg
& ~0xf) | ctrl
->rankmap
[channel
];
3342 addr
= 0x400 * channel
+ 0x42a0;
3343 MCHBAR32(addr
) = reg
;
3345 // Wait 10ns for ranks to settle
3348 reg
= (reg
& ~0xf0) | (ctrl
->rankmap
[channel
] << 4);
3349 MCHBAR32(addr
) = reg
;
3351 // Write reset using a NOP
3356 dram_mrscommands(ctrl
);
3360 write32(DEFAULT_MCHBAR
+ 0x3000, 0);
3363 write32(DEFAULT_MCHBAR
+ (channel
* 0x100) + 0xe3c,
3364 0 | (read32(DEFAULT_MCHBAR
+ (channel
* 0x100) + 0xe3c) &
3369 write32(DEFAULT_MCHBAR
+ 0x4ea8, 0);