2 * This file is part of the coreboot project.
4 * Copyright (C) 2005 Eric W. Biederman and Tom Zimmerman
5 * Copyright (C) 2008 Arastra, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <cpu/x86/mem.h>
23 #include <cpu/x86/mtrr.h>
24 #include <cpu/x86/cache.h>
25 #include "raminit_ep80579.h"
28 #define BAR 0x90000000
30 static void sdram_set_registers(const struct mem_controller *ctrl)
32 static const u32 register_values[] = {
33 PCI_ADDR(0, 0x00, 0, CKDIS), 0xffff0000, 0x0000ffff,
34 PCI_ADDR(0, 0x00, 0, DEVPRES), 0x00000000, 0x07420801 | DEVPRES_CONFIG,
35 PCI_ADDR(0, 0x00, 0, PAM-1), 0xcccccc7f, 0x33333000,
36 PCI_ADDR(0, 0x00, 0, PAM+3), 0xcccccccc, 0x33333333,
37 PCI_ADDR(0, 0x00, 0, DEVPRES1), 0xffffffff, 0x0040003a,
38 PCI_ADDR(0, 0x00, 0, SMRBASE), 0x00000fff, BAR | 0,
43 for (i = 0; i < ARRAY_SIZE(register_values); i += 3) {
47 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x00, 0) + ctrl->f0;
48 where = register_values[i] & 0xff;
49 reg = pci_read_config32(dev, where);
50 reg &= register_values[i+1];
51 reg |= register_values[i+2];
52 pci_write_config32(dev, where, reg);
61 static struct dimm_size spd_get_dimm_size(u16 device)
63 /* Calculate the log base 2 size of a DIMM in bits */
69 /* Note it might be easier to use byte 31 here, it has the DIMM size as
70 * a multiple of 4MB. The way we do it now we can size both
71 * sides of an assymetric dimm.
73 value = spd_read_byte(device, SPD_NUM_ROWS);
74 if (value < 0) goto hw_err;
75 if ((value & 0xf) == 0) goto val_err;
76 sz.side1 += value & 0xf;
78 value = spd_read_byte(device, SPD_NUM_COLUMNS);
79 if (value < 0) goto hw_err;
80 if ((value & 0xf) == 0) goto val_err;
81 sz.side1 += value & 0xf;
83 value = spd_read_byte(device, SPD_NUM_BANKS_PER_SDRAM);
84 if (value < 0) goto hw_err;
85 if ((value & 0xff) == 0) goto val_err;
86 sz.side1 += log2(value & 0xff);
88 /* Get the module data width and convert it to a power of two */
89 value = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_MSB);
90 if (value < 0) goto hw_err;
94 low = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_LSB);
95 if (low < 0) goto hw_err;
96 value = value | (low & 0xff);
97 if ((value != 72) && (value != 64)) goto val_err;
98 sz.side1 += log2(value);
101 value = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
103 if (value < 0) goto hw_err;
106 if (value == 1) goto out;
107 if (value != 2) goto val_err;
109 /* Start with the symmetrical case */
112 value = spd_read_byte(device, SPD_NUM_ROWS);
113 if (value < 0) goto hw_err;
114 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
115 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
116 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
118 value = spd_read_byte(device, SPD_NUM_COLUMNS);
119 if (value < 0) goto hw_err;
120 if ((value & 0xff) == 0) goto val_err;
121 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
122 sz.side2 += ((value >> 4) & 0x0f); /* Add in columns on side 2 */
126 die("Bad SPD value\r\n");
127 /* If an hw_error occurs report that I have no memory */
132 print_debug("dimm ");
133 print_debug_hex8(device);
134 print_debug(" size = ");
135 print_debug_hex8(sz.side1);
137 print_debug_hex8(sz.side2);
143 static long spd_set_ram_size(const struct mem_controller *ctrl, u8 dimm_mask)
148 for (i = cum = 0; i < DIMM_SOCKETS; i++) {
150 if (dimm_mask & (1 << i)) {
151 sz = spd_get_dimm_size(ctrl->channel0[i]);
153 return -1; /* Report SPD error */
155 /* convert bits to multiples of 64MB */
157 cum += (1 << sz.side1);
158 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
159 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
160 if (spd_read_byte(ctrl->channel0[i], SPD_NUM_DIMM_BANKS) & 0x1) {
165 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
166 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
169 print_debug("DRB = ");
170 print_debug_hex32(pci_read_config32(ctrl->f0, DRB));
174 /* set TOM top of memory */
175 pci_write_config16(ctrl->f0, TOM, cum);
176 print_debug("TOM = ");
177 print_debug_hex16(cum);
179 /* set TOLM top of low memory */
184 pci_write_config16(ctrl->f0, TOLM, cum);
185 print_debug("TOLM = ");
186 print_debug_hex16(cum);
192 static u8 spd_detect_dimms(const struct mem_controller *ctrl)
196 for (i = 0; i < DIMM_SOCKETS; i++) {
199 device = ctrl->channel0[i];
201 byte = spd_read_byte(device, SPD_MEMORY_TYPE);
203 print_debug_hex8(device);
205 print_debug_hex8(byte);
208 dimm_mask |= (1 << i);
216 static int spd_set_row_attributes(const struct mem_controller *ctrl,
222 for (i = 0; i < DIMM_SOCKETS; i++) {
226 if (!(dimm_mask & (1 << i))) {
230 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_ROWS);
231 if (value < 0) die("Bad SPD data\r\n");
232 if ((value & 0xf) == 0) die("Invalid # of rows\r\n");
233 dra |= (((value-13) & 0x7) << 23);
234 dra |= (((value-13) & 0x7) << 29);
237 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_COLUMNS);
238 if (value < 0) die("Bad SPD data\r\n");
239 if ((value & 0xf) == 0) die("Invalid # of columns\r\n");
240 dra |= (((value-10) & 0x7) << 20);
241 dra |= (((value-10) & 0x7) << 26);
244 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_BANKS_PER_SDRAM);
245 if (value < 0) die("Bad SPD data\r\n");
246 if ((value & 0xff) == 0) die("Invalid # of banks\r\n");
247 reg += log2(value & 0xff);
249 print_debug("dimm ");
251 print_debug(" reg = ");
252 print_debug_hex8(reg);
255 /* set device density */
257 dra |= ((31-reg) << 6);
259 /* set device width (x8) */
263 /* set device type (registered) */
266 /* set number of ranks (0=single, 1=dual) */
267 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_DIMM_BANKS);
268 dra |= ((value & 0x1) << 17);
273 print_debug_hex32(dra);
276 pci_write_config32(ctrl->f0, DRA + (i*4), dra);
282 static u32 spd_set_drt_attributes(const struct mem_controller *ctrl,
283 u8 dimm_mask, u32 drc)
293 int index = drc & 0x00000003;
295 static const u8 latencies[] = { /* 533, 800, 400, 667 */
296 0x10, 0x60, 0x10, 0x20 };
297 static const u32 drt0[] = { /* 533, 800, 400, 667 */
298 0x24240002, 0x24360002, 0x24220002, 0x24360002 };
299 static const u32 drt1[] = { /* 533, 800, 400, 667 */
300 0x00400000, 0x00900000, 0x00200000, 0x00700000 };
301 static const u32 magic[] = { /* 533, 800, 400, 667 */
302 0x007b8221, 0x00b94331, 0x005ca1a1, 0x009a62b1 };
303 static const u32 mrs[] = { /* 533, 800, 400, 667 */
304 0x07020000, 0x0b020000, 0x05020000, 0x09020000 };
305 static const int cycle[] = { /* 533, 800, 400, 667 */
306 15, 10, 20, 12 }; /* cycle time in 1/4 ns units */
307 static const int byte40rem[] = {
308 0, 1, 2, 2, 3, 3, 0, 0 }; /* byte 40 remainder in 1/4 ns units */
310 /* CAS latency in cycles */
311 val = latencies[index];
312 for (i = 0; i < DIMM_SOCKETS; i++) {
313 if (!(dimm_mask & (1 << i)))
315 val &= spd_read_byte(ctrl->channel0[i], SPD_ACCEPTABLE_CAS_LATENCIES);
324 die("CAS latency mismatch\r\n");
325 print_debug("cl = ");
326 print_debug_hex8(cl);
331 /* Trc, Trfc in cycles */
332 for (i = 0; i < DIMM_SOCKETS; i++) {
333 if (!(dimm_mask & (1 << i)))
335 val1 = spd_read_byte(ctrl->channel0[i], SPD_BYTE_41_42_EXTENSION);
336 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_ACT_TO_ACT_AUTO_REFRESH);
337 val <<= 2; /* convert to 1/4 ns */
338 val += byte40rem[(val1 >> 4) & 0x7];
339 val = (val + ci - 1) / ci + 1; /* convert to cycles */
342 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_AUTO_REFRESH_TO_ACT);
343 val <<= 2; /* convert to 1/4 ns */
346 val += byte40rem[(val1 >> 1) & 0x7];
347 val = (val + ci - 1) / ci; /* convert to cycles */
351 print_debug("trc = ");
352 print_debug_hex8(trc);
354 print_debug("trfc = ");
355 print_debug_hex8(trfc);
358 /* Tras, Trtp, Twtr in cycles */
359 for (i = 0; i < DIMM_SOCKETS; i++) {
360 if (!(dimm_mask & (1 << i)))
362 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_ACTIVE_TO_PRECHARGE_DELAY);
363 val <<= 2; /* convert to 1/4 ns */
364 val = (val + ci - 1) / ci; /* convert to cycles */
367 val = spd_read_byte(ctrl->channel0[i], SPD_INT_READ_TO_PRECHARGE_DELAY);
368 val = (val + ci - 1) / ci; /* convert to cycles */
371 val = spd_read_byte(ctrl->channel0[i], SPD_INT_WRITE_TO_READ_DELAY);
372 val = (val + ci - 1) / ci; /* convert to cycles */
376 print_debug("tras = ");
377 print_debug_hex8(tras);
379 print_debug("trtp = ");
380 print_debug_hex8(trtp);
382 print_debug("twtr = ");
383 print_debug_hex8(twtr);
386 val = (drt0[index] | ((trc - 11) << 12) | ((cl - 3) << 9)
387 | ((cl - 3) << 6) | ((cl - 3) << 3));
388 print_debug("drt0 = ");
389 print_debug_hex32(val);
391 pci_write_config32(ctrl->f0, DRT0, val);
393 val = (drt1[index] | ((tras - 8) << 28) | ((trtp - 2) << 25)
395 print_debug("drt1 = ");
396 print_debug_hex32(val);
398 pci_write_config32(ctrl->f0, DRT1, val);
400 val = (magic[index]);
401 print_debug("magic = ");
402 print_debug_hex32(val);
404 pci_write_config32(PCI_DEV(0, 0x08, 0), 0xcc, val);
406 val = (mrs[index] | (cl << 20));
407 print_debug("mrs = ");
408 print_debug_hex32(val);
413 static int spd_set_dram_controller_mode(const struct mem_controller *ctrl,
422 for (i = 0; i < DIMM_SOCKETS; i++) {
423 if (!(dimm_mask & (1 << i)))
425 if ((spd_read_byte(ctrl->channel0[i], SPD_MODULE_DATA_WIDTH_LSB) & 0xf0) != 0x40)
426 die("ERROR: Only 64-bit DIMMs supported\r\n");
427 if (!(spd_read_byte(ctrl->channel0[i], SPD_DIMM_CONFIG_TYPE) & 0x02))
428 die("ERROR: Only ECC DIMMs supported\r\n");
429 if (spd_read_byte(ctrl->channel0[i], SPD_PRIMARY_SDRAM_WIDTH) != 0x08)
430 die("ERROR: Only x8 DIMMs supported\r\n");
432 value = spd_read_byte(ctrl->channel0[i], SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
436 print_debug("cycle = ");
437 print_debug_hex8(cycle);
440 drc |= (1 << 20); /* enable ECC */
441 drc |= (3 << 30); /* enable CKE on each DIMM */
442 drc |= (1 << 4); /* enable CKE globally */
445 /* set front side bus speed */
446 msr = rdmsr(0xcd); /* returns 0 on Pentium M 90nm */
447 print_debug("msr 0xcd = ");
448 print_debug_hex32(msr.hi);
449 print_debug_hex32(msr.lo);
452 /* TODO check that this msr really indicates fsb speed! */
454 print_info("533 MHz FSB\r\n");
457 print_info("400 MHz DDR\r\n");
458 } else if (cycle <= 0x30) {
460 print_info("333 MHz DDR\r\n");
461 } else if (cycle <= 0x3d) {
463 print_info("266 MHz DDR\r\n");
466 print_info("200 MHz DDR\r\n");
470 print_info("400 MHz FSB\r\n");
473 print_info("333 MHz DDR\r\n");
474 } else if (cycle <= 0x3d) {
476 print_info("266 MHz DDR\r\n");
479 print_info("200 MHz DDR\r\n");
483 print_debug("DRC = ");
484 print_debug_hex32(drc);
490 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
495 /* Test if we can read the SPD */
496 dimm_mask = spd_detect_dimms(ctrl);
497 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
498 print_err("No memory for this cpu\r\n");
504 static void set_on_dimm_termination_enable(const struct mem_controller *ctrl)
511 /* Set up northbridge values */
513 pci_write_config32(ctrl->f0, SDRC, 0xa0002c30);
515 c1 = pci_read_config8(ctrl->f0, DRB);
516 c2 = pci_read_config8(ctrl->f0, DRB+2);
518 /* 1 single-rank DIMM */
522 /* 2 single-rank DIMMs or 1 double-rank DIMM */
526 print_debug("ODT Value = ");
527 print_debug_hex32(data32);
530 pci_write_config32(ctrl->f0, DDR2ODTC, data32);
532 for (i = 0; i < 2; i++) {
533 print_debug("ODT CS");
537 write32(BAR+DCALADDR, 0x0b840001);
538 write32(BAR+DCALCSR, 0x80000003 | ((i+1)<<21));
539 data32 = read32(BAR+DCALCSR);
540 while (data32 & 0x80000000)
541 data32 = read32(BAR+DCALCSR);
546 static void dump_dcal_regs(void)
549 for (i = 0x0; i < 0x2a0; i += 4) {
552 print_debug_hex16(i);
555 print_debug_hex32(read32(BAR+i));
562 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
573 mask = spd_detect_dimms(ctrl);
574 print_debug("Starting SDRAM Enable\r\n");
576 /* Set DRAM type and Front Side Bus frequency */
577 drc = spd_set_dram_controller_mode(ctrl, mask);
579 die("Error calculating DRC\r\n");
581 data32 = drc & ~(3 << 20); /* clear ECC mode */
582 data32 = data32 | (3 << 5); /* temp turn off ODT */
583 /* Set DRAM controller mode */
584 pci_write_config32(ctrl->f0, DRC, data32);
586 /* Turn the clocks on */
587 pci_write_config16(ctrl->f0, CKDIS, 0x0000);
589 /* Program row size */
590 spd_set_ram_size(ctrl, mask);
592 /* Program row attributes */
593 spd_set_row_attributes(ctrl, mask);
595 /* Program timing values */
596 mode_reg = spd_set_drt_attributes(ctrl, mask, drc);
601 for (cs = 0; cs < 2; cs++) {
602 print_debug("NOP CS");
603 print_debug_hex8(cs);
606 write32(BAR+DCALCSR, (0x00000000 | ((cs+1)<<21)));
607 write32(BAR+DCALCSR, (0x80000000 | ((cs+1)<<21)));
608 data32 = read32(BAR+DCALCSR);
609 while (data32 & 0x80000000)
610 data32 = read32(BAR+DCALCSR);
615 for (cs = 0; cs < 2; cs++) {
616 print_debug("NOP CS");
617 print_debug_hex8(cs);
619 write32(BAR + DCALCSR, (0x80000000 | ((cs+1)<<21)));
620 data32 = read32(BAR+DCALCSR);
621 while (data32 & 0x80000000)
622 data32 = read32(BAR+DCALCSR);
625 /* Precharge all banks */
627 for (cs = 0; cs < 2; cs++) {
628 print_debug("Precharge CS");
629 print_debug_hex8(cs);
631 write32(BAR+DCALADDR, 0x04000000);
632 write32(BAR+DCALCSR, (0x80000002 | ((cs+1)<<21)));
633 data32 = read32(BAR+DCALCSR);
634 while (data32 & 0x80000000)
635 data32 = read32(BAR+DCALCSR);
638 /* EMRS: Enable DLLs, set OCD calibration mode to default */
640 for (cs = 0; cs < 2; cs++) {
641 print_debug("EMRS CS");
642 print_debug_hex8(cs);
644 write32(BAR+DCALADDR, 0x0b840001);
645 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
646 data32 = read32(BAR+DCALCSR);
647 while (data32 & 0x80000000)
648 data32 = read32(BAR+DCALCSR);
650 /* MRS: Reset DLLs */
652 for (cs = 0; cs < 2; cs++) {
653 print_debug("MRS CS");
654 print_debug_hex8(cs);
656 write32(BAR+DCALADDR, mode_reg);
657 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
658 data32 = read32(BAR+DCALCSR);
659 while (data32 & 0x80000000)
660 data32 = read32(BAR+DCALCSR);
663 /* Precharge all banks */
665 for (cs = 0; cs < 2; cs++) {
666 print_debug("Precharge CS");
667 print_debug_hex8(cs);
669 write32(BAR+DCALADDR, 0x04000000);
670 write32(BAR+DCALCSR, (0x80000002 | ((cs+1)<<21)));
671 data32 = read32(BAR+DCALCSR);
672 while (data32 & 0x80000000)
673 data32 = read32(BAR+DCALCSR);
677 for (i = 0; i < 2; i++) {
679 for (cs = 0; cs < 2; cs++) {
680 print_debug("Refresh CS");
681 print_debug_hex8(cs);
683 write32(BAR+DCALCSR, (0x80000001 | ((cs+1)<<21)));
684 data32 = read32(BAR+DCALCSR);
685 while (data32 & 0x80000000)
686 data32 = read32(BAR+DCALCSR);
690 /* MRS: Set DLLs to normal */
692 for (cs = 0; cs < 2; cs++) {
693 print_debug("MRS CS");
694 print_debug_hex8(cs);
696 write32(BAR+DCALADDR, (mode_reg & ~(1<<24)));
697 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
698 data32 = read32(BAR+DCALCSR);
699 while (data32 & 0x80000000)
700 data32 = read32(BAR+DCALCSR);
703 /* EMRS: Enable DLLs */
705 for (cs = 0; cs < 2; cs++) {
706 print_debug("EMRS CS");
707 print_debug_hex8(cs);
709 write32(BAR+DCALADDR, 0x0b840001);
710 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
711 data32 = read32(BAR+DCALCSR);
712 while (data32 & 0x80000000)
713 data32 = read32(BAR+DCALCSR);
718 write32(BAR+DCALCSR, 0x0000000f);
720 write32(BAR, 0x00100000);
722 /* Enable on-DIMM termination */
723 set_on_dimm_termination_enable(ctrl);
727 /* Receive enable calibration */
729 for (cs = 0; cs < 1; cs++) {
730 print_debug("receive enable calibration CS");
731 print_debug_hex8(cs);
733 write32(BAR+DCALCSR, (0x8000000c | ((cs+1)<<21)));
734 data32 = read32(BAR+DCALCSR);
735 while (data32 & 0x80000000)
736 data32 = read32(BAR+DCALCSR);
742 data32 = read32(BAR+DDRIOMC2);
743 data32 &= ~(0xf << 16);
744 data32 |= (0xb << 16);
745 write32(BAR+DDRIOMC2, data32);
749 data32 = drc & ~(3 << 20); /* clear ECC mode */
750 pci_write_config32(ctrl->f0, DRC, data32);
751 write32(BAR+DCALCSR, 0x0008000f);
753 /* Clear memory and init ECC */
754 for (cs = 0; cs < 2; cs++) {
755 if (!(mask & (1<<cs)))
757 print_debug("clear memory CS");
758 print_debug_hex8(cs);
760 write32(BAR+MBCSR, 0xa00000f0 | ((cs+1)<<20) | (0<<16));
761 data32 = read32(BAR+MBCSR);
762 while (data32 & 0x80000000)
763 data32 = read32(BAR+MBCSR);
764 if (data32 & 0x40000000)
765 print_debug("failed!\r\n");
768 /* Clear read/write FIFO pointers */
769 print_debug("clear read/write fifo pointers\r\n");
770 write32(BAR+DDRIOMC2, read32(BAR+DDRIOMC2) | (1<<15));
772 write32(BAR+DDRIOMC2, read32(BAR+DDRIOMC2) & ~(1<<15));
777 print_debug("Done\r\n");
779 /* Set initialization complete */
782 data32 = drc & ~(3 << 20); /* clear ECC mode */
783 pci_write_config32(ctrl->f0, DRC, data32);
785 /* Set the ECC mode */
786 pci_write_config32(ctrl->f0, DRC, drc);
788 /* The memory is now set up--use it */
789 cache_lbmem(MTRR_TYPE_WRBACK);
792 static inline int memory_initialized(void)
794 return pci_read_config32(PCI_DEV(0, 0x00, 0), DRC) & (1 << 29);