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/mtrr.h>
23 #include <cpu/x86/cache.h>
24 #include "raminit_ep80579.h"
27 #define BAR 0x90000000
29 static void sdram_set_registers(const struct mem_controller *ctrl)
31 static const u32 register_values[] = {
32 PCI_ADDR(0, 0x00, 0, CKDIS), 0xffff0000, 0x0000ffff,
33 PCI_ADDR(0, 0x00, 0, DEVPRES), 0x00000000, 0x07420001 | DEVPRES_CONFIG,
34 PCI_ADDR(0, 0x00, 0, PAM-1), 0xcccccc7f, 0x33333000,
35 PCI_ADDR(0, 0x00, 0, PAM+3), 0xcccccccc, 0x33333333,
36 PCI_ADDR(0, 0x00, 0, DEVPRES1), 0xffffffff, 0x0040003a,
37 PCI_ADDR(0, 0x00, 0, SMRBASE), 0x00000fff, BAR | 0,
42 for (i = 0; i < ARRAY_SIZE(register_values); i += 3) {
46 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x00, 0) + ctrl->f0;
47 where = register_values[i] & 0xff;
48 reg = pci_read_config32(dev, where);
49 reg &= register_values[i+1];
50 reg |= register_values[i+2];
51 pci_write_config32(dev, where, reg);
60 static struct dimm_size spd_get_dimm_size(u16 device)
62 /* Calculate the log base 2 size of a DIMM in bits */
68 /* Note it might be easier to use byte 31 here, it has the DIMM size as
69 * a multiple of 4MB. The way we do it now we can size both
70 * sides of an assymetric dimm.
72 value = spd_read_byte(device, SPD_NUM_ROWS);
73 if (value < 0) goto hw_err;
74 if ((value & 0xf) == 0) goto val_err;
75 sz.side1 += value & 0xf;
77 value = spd_read_byte(device, SPD_NUM_COLUMNS);
78 if (value < 0) goto hw_err;
79 if ((value & 0xf) == 0) goto val_err;
80 sz.side1 += value & 0xf;
82 value = spd_read_byte(device, SPD_NUM_BANKS_PER_SDRAM);
83 if (value < 0) goto hw_err;
84 if ((value & 0xff) == 0) goto val_err;
85 sz.side1 += log2(value & 0xff);
87 /* Get the module data width and convert it to a power of two */
88 value = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_MSB);
89 if (value < 0) goto hw_err;
93 low = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_LSB);
94 if (low < 0) goto hw_err;
95 value = value | (low & 0xff);
96 if ((value != 72) && (value != 64)) goto val_err;
97 sz.side1 += log2(value);
100 value = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
102 if (value < 0) goto hw_err;
105 if (value == 1) goto out;
106 if (value != 2) goto val_err;
108 /* Start with the symmetrical case */
111 value = spd_read_byte(device, SPD_NUM_ROWS);
112 if (value < 0) goto hw_err;
113 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
114 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
115 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
117 value = spd_read_byte(device, SPD_NUM_COLUMNS);
118 if (value < 0) goto hw_err;
119 if ((value & 0xff) == 0) goto val_err;
120 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
121 sz.side2 += ((value >> 4) & 0x0f); /* Add in columns on side 2 */
125 die("Bad SPD value\n");
126 /* If an hw_error occurs report that I have no memory */
131 print_debug("dimm ");
132 print_debug_hex8(device);
133 print_debug(" size = ");
134 print_debug_hex8(sz.side1);
136 print_debug_hex8(sz.side2);
142 static long spd_set_ram_size(const struct mem_controller *ctrl, u8 dimm_mask)
147 for (i = cum = 0; i < DIMM_SOCKETS; i++) {
149 if (dimm_mask & (1 << i)) {
150 sz = spd_get_dimm_size(ctrl->channel0[i]);
152 return -1; /* Report SPD error */
154 /* convert bits to multiples of 64MB */
156 cum += (1 << sz.side1);
157 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
158 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
159 if (spd_read_byte(ctrl->channel0[i], SPD_NUM_DIMM_BANKS) & 0x1) {
164 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
165 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
168 print_debug("DRB = ");
169 print_debug_hex32(pci_read_config32(ctrl->f0, DRB));
173 /* set TOM top of memory */
174 pci_write_config16(ctrl->f0, TOM, cum);
175 print_debug("TOM = ");
176 print_debug_hex16(cum);
178 /* set TOLM top of low memory */
183 pci_write_config16(ctrl->f0, TOLM, cum);
184 print_debug("TOLM = ");
185 print_debug_hex16(cum);
191 static u8 spd_detect_dimms(const struct mem_controller *ctrl)
195 for (i = 0; i < DIMM_SOCKETS; i++) {
198 device = ctrl->channel0[i];
200 byte = spd_read_byte(device, SPD_MEMORY_TYPE);
202 print_debug_hex8(device);
204 print_debug_hex8(byte);
207 dimm_mask |= (1 << i);
215 static int spd_set_row_attributes(const struct mem_controller *ctrl,
221 for (i = 0; i < DIMM_SOCKETS; i++) {
225 if (!(dimm_mask & (1 << i))) {
229 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_ROWS);
230 if (value < 0) die("Bad SPD data\n");
231 if ((value & 0xf) == 0) die("Invalid # of rows\n");
232 dra |= (((value-13) & 0x7) << 23);
233 dra |= (((value-13) & 0x7) << 29);
236 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_COLUMNS);
237 if (value < 0) die("Bad SPD data\n");
238 if ((value & 0xf) == 0) die("Invalid # of columns\n");
239 dra |= (((value-10) & 0x7) << 20);
240 dra |= (((value-10) & 0x7) << 26);
243 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_BANKS_PER_SDRAM);
244 if (value < 0) die("Bad SPD data\n");
245 if ((value & 0xff) == 0) die("Invalid # of banks\n");
246 reg += log2(value & 0xff);
248 print_debug("dimm ");
250 print_debug(" reg = ");
251 print_debug_hex8(reg);
254 /* set device density */
256 dra |= ((31-reg) << 6);
258 /* set device width (x8) */
262 /* set device type (registered) */
265 /* set number of ranks (0=single, 1=dual) */
266 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_DIMM_BANKS);
267 dra |= ((value & 0x1) << 17);
272 print_debug_hex32(dra);
275 pci_write_config32(ctrl->f0, DRA + (i*4), dra);
281 static u32 spd_set_drt_attributes(const struct mem_controller *ctrl,
282 u8 dimm_mask, u32 drc)
292 int index = drc & 0x00000003;
294 static const u8 latencies[] = { /* 533, 800, 400, 667 */
295 0x10, 0x60, 0x10, 0x20 };
296 static const u32 drt0[] = { /* 533, 800, 400, 667 */
297 0x24240002, 0x24360002, 0x24220002, 0x24360002 };
298 static const u32 drt1[] = { /* 533, 800, 400, 667 */
299 0x00400000, 0x00900000, 0x00200000, 0x00700000 };
300 static const u32 magic[] = { /* 533, 800, 400, 667 */
301 0x007b8221, 0x00b94331, 0x005ca1a1, 0x009a62b1 };
302 static const u32 mrs[] = { /* 533, 800, 400, 667 */
303 0x07020000, 0x0b020000, 0x05020000, 0x09020000 };
304 static const int cycle[] = { /* 533, 800, 400, 667 */
305 15, 10, 20, 12 }; /* cycle time in 1/4 ns units */
306 static const int byte40rem[] = {
307 0, 1, 2, 2, 3, 3, 0, 0 }; /* byte 40 remainder in 1/4 ns units */
309 /* CAS latency in cycles */
310 val = latencies[index];
311 for (i = 0; i < DIMM_SOCKETS; i++) {
312 if (!(dimm_mask & (1 << i)))
314 val &= spd_read_byte(ctrl->channel0[i], SPD_ACCEPTABLE_CAS_LATENCIES);
323 die("CAS latency mismatch\n");
324 print_debug("cl = ");
325 print_debug_hex8(cl);
330 /* Trc, Trfc in cycles */
331 for (i = 0; i < DIMM_SOCKETS; i++) {
332 if (!(dimm_mask & (1 << i)))
334 val1 = spd_read_byte(ctrl->channel0[i], SPD_BYTE_41_42_EXTENSION);
335 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_ACT_TO_ACT_AUTO_REFRESH);
336 val <<= 2; /* convert to 1/4 ns */
337 val += byte40rem[(val1 >> 4) & 0x7];
338 val = (val + ci - 1) / ci + 1; /* convert to cycles */
341 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_AUTO_REFRESH_TO_ACT);
342 val <<= 2; /* convert to 1/4 ns */
345 val += byte40rem[(val1 >> 1) & 0x7];
346 val = (val + ci - 1) / ci; /* convert to cycles */
350 print_debug("trc = ");
351 print_debug_hex8(trc);
353 print_debug("trfc = ");
354 print_debug_hex8(trfc);
357 /* Tras, Trtp, Twtr in cycles */
358 for (i = 0; i < DIMM_SOCKETS; i++) {
359 if (!(dimm_mask & (1 << i)))
361 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_ACTIVE_TO_PRECHARGE_DELAY);
362 val <<= 2; /* convert to 1/4 ns */
363 val = (val + ci - 1) / ci; /* convert to cycles */
366 val = spd_read_byte(ctrl->channel0[i], SPD_INT_READ_TO_PRECHARGE_DELAY);
367 val = (val + ci - 1) / ci; /* convert to cycles */
370 val = spd_read_byte(ctrl->channel0[i], SPD_INT_WRITE_TO_READ_DELAY);
371 val = (val + ci - 1) / ci; /* convert to cycles */
375 print_debug("tras = ");
376 print_debug_hex8(tras);
378 print_debug("trtp = ");
379 print_debug_hex8(trtp);
381 print_debug("twtr = ");
382 print_debug_hex8(twtr);
385 val = (drt0[index] | ((trc - 11) << 12) | ((cl - 3) << 9)
386 | ((cl - 3) << 6) | ((cl - 3) << 3));
387 print_debug("drt0 = ");
388 print_debug_hex32(val);
390 pci_write_config32(ctrl->f0, DRT0, val);
392 val = (drt1[index] | ((tras - 8) << 28) | ((trtp - 2) << 25)
394 print_debug("drt1 = ");
395 print_debug_hex32(val);
397 pci_write_config32(ctrl->f0, DRT1, val);
399 val = (magic[index]);
400 print_debug("magic = ");
401 print_debug_hex32(val);
403 pci_write_config32(PCI_DEV(0, 0x08, 0), 0xcc, val);
405 val = (mrs[index] | (cl << 20));
406 print_debug("mrs = ");
407 print_debug_hex32(val);
412 static int spd_set_dram_controller_mode(const struct mem_controller *ctrl,
421 for (i = 0; i < DIMM_SOCKETS; i++) {
422 if (!(dimm_mask & (1 << i)))
424 if ((spd_read_byte(ctrl->channel0[i], SPD_MODULE_DATA_WIDTH_LSB) & 0xf0) != 0x40)
425 die("ERROR: Only 64-bit DIMMs supported\n");
426 if (!(spd_read_byte(ctrl->channel0[i], SPD_DIMM_CONFIG_TYPE) & 0x02))
427 die("ERROR: Only ECC DIMMs supported\n");
428 if (spd_read_byte(ctrl->channel0[i], SPD_PRIMARY_SDRAM_WIDTH) != 0x08)
429 die("ERROR: Only x8 DIMMs supported\n");
431 value = spd_read_byte(ctrl->channel0[i], SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
435 print_debug("cycle = ");
436 print_debug_hex8(cycle);
439 drc |= (1 << 20); /* enable ECC */
440 drc |= (3 << 30); /* enable CKE on each DIMM */
441 drc |= (1 << 4); /* enable CKE globally */
444 /* set front side bus speed */
445 msr = rdmsr(0xcd); /* returns 0 on Pentium M 90nm */
446 print_debug("msr 0xcd = ");
447 print_debug_hex32(msr.hi);
448 print_debug_hex32(msr.lo);
451 /* TODO check that this msr really indicates fsb speed! */
453 print_info("533 MHz FSB\n");
456 print_info("400 MHz DDR\n");
457 } else if (cycle <= 0x30) {
459 print_info("333 MHz DDR\n");
460 } else if (cycle <= 0x3d) {
462 print_info("266 MHz DDR\n");
465 print_info("200 MHz DDR\n");
469 print_info("400 MHz FSB\n");
472 print_info("333 MHz DDR\n");
473 } else if (cycle <= 0x3d) {
475 print_info("266 MHz DDR\n");
478 print_info("200 MHz DDR\n");
482 print_debug("DRC = ");
483 print_debug_hex32(drc);
489 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
494 /* Test if we can read the SPD */
495 dimm_mask = spd_detect_dimms(ctrl);
496 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
497 print_err("No memory for this cpu\n");
503 static void set_on_dimm_termination_enable(const struct mem_controller *ctrl)
510 /* Set up northbridge values */
512 pci_write_config32(ctrl->f0, SDRC, 0xa0002c30);
514 c1 = pci_read_config8(ctrl->f0, DRB);
515 c2 = pci_read_config8(ctrl->f0, DRB+2);
517 /* 1 single-rank DIMM */
521 /* 2 single-rank DIMMs or 1 double-rank DIMM */
525 print_debug("ODT Value = ");
526 print_debug_hex32(data32);
529 pci_write_config32(ctrl->f0, DDR2ODTC, data32);
531 for (i = 0; i < 2; i++) {
532 print_debug("ODT CS");
536 write32(BAR+DCALADDR, 0x0b840001);
537 write32(BAR+DCALCSR, 0x80000003 | ((i+1)<<21));
538 data32 = read32(BAR+DCALCSR);
539 while (data32 & 0x80000000)
540 data32 = read32(BAR+DCALCSR);
545 static void dump_dcal_regs(void)
548 for (i = 0x0; i < 0x2a0; i += 4) {
551 print_debug_hex16(i);
554 print_debug_hex32(read32(BAR+i));
561 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
572 mask = spd_detect_dimms(ctrl);
573 print_debug("Starting SDRAM Enable\n");
575 /* Set DRAM type and Front Side Bus frequency */
576 drc = spd_set_dram_controller_mode(ctrl, mask);
578 die("Error calculating DRC\n");
580 data32 = drc & ~(3 << 20); /* clear ECC mode */
581 data32 = data32 | (3 << 5); /* temp turn off ODT */
582 /* Set DRAM controller mode */
583 pci_write_config32(ctrl->f0, DRC, data32);
585 /* Turn the clocks on */
586 pci_write_config16(ctrl->f0, CKDIS, 0x0000);
588 /* Program row size */
589 spd_set_ram_size(ctrl, mask);
591 /* Program row attributes */
592 spd_set_row_attributes(ctrl, mask);
594 /* Program timing values */
595 mode_reg = spd_set_drt_attributes(ctrl, mask, drc);
600 for (cs = 0; cs < 2; cs++) {
601 print_debug("NOP CS");
602 print_debug_hex8(cs);
605 write32(BAR+DCALCSR, (0x00000000 | ((cs+1)<<21)));
606 write32(BAR+DCALCSR, (0x80000000 | ((cs+1)<<21)));
607 data32 = read32(BAR+DCALCSR);
608 while (data32 & 0x80000000)
609 data32 = read32(BAR+DCALCSR);
614 for (cs = 0; cs < 2; cs++) {
615 print_debug("NOP CS");
616 print_debug_hex8(cs);
618 write32(BAR + DCALCSR, (0x80000000 | ((cs+1)<<21)));
619 data32 = read32(BAR+DCALCSR);
620 while (data32 & 0x80000000)
621 data32 = read32(BAR+DCALCSR);
624 /* Precharge all banks */
626 for (cs = 0; cs < 2; cs++) {
627 print_debug("Precharge CS");
628 print_debug_hex8(cs);
630 write32(BAR+DCALADDR, 0x04000000);
631 write32(BAR+DCALCSR, (0x80000002 | ((cs+1)<<21)));
632 data32 = read32(BAR+DCALCSR);
633 while (data32 & 0x80000000)
634 data32 = read32(BAR+DCALCSR);
637 /* EMRS: Enable DLLs, set OCD calibration mode to default */
639 for (cs = 0; cs < 2; cs++) {
640 print_debug("EMRS CS");
641 print_debug_hex8(cs);
643 write32(BAR+DCALADDR, 0x0b840001);
644 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
645 data32 = read32(BAR+DCALCSR);
646 while (data32 & 0x80000000)
647 data32 = read32(BAR+DCALCSR);
649 /* MRS: Reset DLLs */
651 for (cs = 0; cs < 2; cs++) {
652 print_debug("MRS CS");
653 print_debug_hex8(cs);
655 write32(BAR+DCALADDR, mode_reg);
656 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
657 data32 = read32(BAR+DCALCSR);
658 while (data32 & 0x80000000)
659 data32 = read32(BAR+DCALCSR);
662 /* Precharge all banks */
664 for (cs = 0; cs < 2; cs++) {
665 print_debug("Precharge CS");
666 print_debug_hex8(cs);
668 write32(BAR+DCALADDR, 0x04000000);
669 write32(BAR+DCALCSR, (0x80000002 | ((cs+1)<<21)));
670 data32 = read32(BAR+DCALCSR);
671 while (data32 & 0x80000000)
672 data32 = read32(BAR+DCALCSR);
676 for (i = 0; i < 2; i++) {
678 for (cs = 0; cs < 2; cs++) {
679 print_debug("Refresh CS");
680 print_debug_hex8(cs);
682 write32(BAR+DCALCSR, (0x80000001 | ((cs+1)<<21)));
683 data32 = read32(BAR+DCALCSR);
684 while (data32 & 0x80000000)
685 data32 = read32(BAR+DCALCSR);
689 /* MRS: Set DLLs to normal */
691 for (cs = 0; cs < 2; cs++) {
692 print_debug("MRS CS");
693 print_debug_hex8(cs);
695 write32(BAR+DCALADDR, (mode_reg & ~(1<<24)));
696 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
697 data32 = read32(BAR+DCALCSR);
698 while (data32 & 0x80000000)
699 data32 = read32(BAR+DCALCSR);
702 /* EMRS: Enable DLLs */
704 for (cs = 0; cs < 2; cs++) {
705 print_debug("EMRS CS");
706 print_debug_hex8(cs);
708 write32(BAR+DCALADDR, 0x0b840001);
709 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
710 data32 = read32(BAR+DCALCSR);
711 while (data32 & 0x80000000)
712 data32 = read32(BAR+DCALCSR);
717 write32(BAR+DCALCSR, 0x0000000f);
719 write32(BAR, 0x00100000);
721 /* Enable on-DIMM termination */
722 set_on_dimm_termination_enable(ctrl);
726 /* Receive enable calibration */
728 for (cs = 0; cs < 1; cs++) {
729 print_debug("receive enable calibration CS");
730 print_debug_hex8(cs);
732 write32(BAR+DCALCSR, (0x8000000c | ((cs+1)<<21)));
733 data32 = read32(BAR+DCALCSR);
734 while (data32 & 0x80000000)
735 data32 = read32(BAR+DCALCSR);
741 data32 = read32(BAR+DDRIOMC2);
742 data32 &= ~(0xf << 16);
743 data32 |= (0xb << 16);
744 write32(BAR+DDRIOMC2, data32);
748 data32 = drc & ~(3 << 20); /* clear ECC mode */
749 pci_write_config32(ctrl->f0, DRC, data32);
750 write32(BAR+DCALCSR, 0x0008000f);
752 /* Clear memory and init ECC */
753 for (cs = 0; cs < 2; cs++) {
754 if (!(mask & (1<<cs)))
756 print_debug("clear memory CS");
757 print_debug_hex8(cs);
759 write32(BAR+MBCSR, 0xa00000f0 | ((cs+1)<<20) | (0<<16));
760 data32 = read32(BAR+MBCSR);
761 while (data32 & 0x80000000)
762 data32 = read32(BAR+MBCSR);
763 if (data32 & 0x40000000)
764 print_debug("failed!\n");
767 /* Clear read/write FIFO pointers */
768 print_debug("clear read/write fifo pointers\n");
769 write32(BAR+DDRIOMC2, read32(BAR+DDRIOMC2) | (1<<15));
771 write32(BAR+DDRIOMC2, read32(BAR+DDRIOMC2) & ~(1<<15));
776 print_debug("Done\n");
778 /* Set initialization complete */
781 data32 = drc & ~(3 << 20); /* clear ECC mode */
782 pci_write_config32(ctrl->f0, DRC, data32);
784 /* Set the ECC mode */
785 pci_write_config32(ctrl->f0, DRC, drc);
787 /* The memory is now set up--use it */
788 cache_lbmem(MTRR_TYPE_WRBACK);
791 static inline int memory_initialized(void)
793 return pci_read_config32(PCI_DEV(0, 0x00, 0), DRC) & (1 << 29);