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
21 #include <cpu/x86/mtrr.h>
22 #include <cpu/x86/cache.h>
23 #include "raminit_ep80579.h"
26 #define BAR 0x90000000
28 static void sdram_set_registers(const struct mem_controller *ctrl)
30 static const u32 register_values[] = {
31 PCI_ADDR(0, 0x00, 0, CKDIS), 0xffff0000, 0x0000ffff,
32 PCI_ADDR(0, 0x00, 0, DEVPRES), 0x00000000, 0x07420001 | DEVPRES_CONFIG,
33 PCI_ADDR(0, 0x00, 0, PAM-1), 0xcccccc7f, 0x33333000,
34 PCI_ADDR(0, 0x00, 0, PAM+3), 0xcccccccc, 0x33333333,
35 PCI_ADDR(0, 0x00, 0, DEVPRES1), 0xffffffff, 0x0040003a,
36 PCI_ADDR(0, 0x00, 0, SMRBASE), 0x00000fff, BAR | 0,
41 for (i = 0; i < ARRAY_SIZE(register_values); i += 3) {
45 dev = (register_values[i] & ~0xff) - PCI_DEV(0, 0x00, 0) + ctrl->f0;
46 where = register_values[i] & 0xff;
47 reg = pci_read_config32(dev, where);
48 reg &= register_values[i+1];
49 reg |= register_values[i+2];
50 pci_write_config32(dev, where, reg);
59 static struct dimm_size spd_get_dimm_size(u16 device)
61 /* Calculate the log base 2 size of a DIMM in bits */
67 /* Note it might be easier to use byte 31 here, it has the DIMM size as
68 * a multiple of 4MB. The way we do it now we can size both
69 * sides of an assymetric dimm.
71 value = spd_read_byte(device, SPD_NUM_ROWS);
72 if (value < 0) goto hw_err;
73 if ((value & 0xf) == 0) goto val_err;
74 sz.side1 += value & 0xf;
76 value = spd_read_byte(device, SPD_NUM_COLUMNS);
77 if (value < 0) goto hw_err;
78 if ((value & 0xf) == 0) goto val_err;
79 sz.side1 += value & 0xf;
81 value = spd_read_byte(device, SPD_NUM_BANKS_PER_SDRAM);
82 if (value < 0) goto hw_err;
83 if ((value & 0xff) == 0) goto val_err;
84 sz.side1 += log2(value & 0xff);
86 /* Get the module data width and convert it to a power of two */
87 value = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_MSB);
88 if (value < 0) goto hw_err;
92 low = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_LSB);
93 if (low < 0) goto hw_err;
94 value = value | (low & 0xff);
95 if ((value != 72) && (value != 64)) goto val_err;
96 sz.side1 += log2(value);
99 value = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
101 if (value < 0) goto hw_err;
104 if (value == 1) goto out;
105 if (value != 2) goto val_err;
107 /* Start with the symmetrical case */
110 value = spd_read_byte(device, SPD_NUM_ROWS);
111 if (value < 0) goto hw_err;
112 if ((value & 0xf0) == 0) goto out; /* If symmetrical we are done */
113 sz.side2 -= (value & 0x0f); /* Subtract out rows on side 1 */
114 sz.side2 += ((value >> 4) & 0x0f); /* Add in rows on side 2 */
116 value = spd_read_byte(device, SPD_NUM_COLUMNS);
117 if (value < 0) goto hw_err;
118 if ((value & 0xff) == 0) goto val_err;
119 sz.side2 -= (value & 0x0f); /* Subtract out columns on side 1 */
120 sz.side2 += ((value >> 4) & 0x0f); /* Add in columns on side 2 */
124 die("Bad SPD value\n");
125 /* If an hw_error occurs report that I have no memory */
130 print_debug("dimm ");
131 print_debug_hex8(device);
132 print_debug(" size = ");
133 print_debug_hex8(sz.side1);
135 print_debug_hex8(sz.side2);
141 static long spd_set_ram_size(const struct mem_controller *ctrl, u8 dimm_mask)
146 for (i = cum = 0; i < DIMM_SOCKETS; i++) {
148 if (dimm_mask & (1 << i)) {
149 sz = spd_get_dimm_size(ctrl->channel0[i]);
151 return -1; /* Report SPD error */
153 /* convert bits to multiples of 64MB */
155 cum += (1 << sz.side1);
156 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
157 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
158 if (spd_read_byte(ctrl->channel0[i], SPD_NUM_DIMM_BANKS) & 0x1) {
163 pci_write_config8(ctrl->f0, DRB + (i*2), cum);
164 pci_write_config8(ctrl->f0, DRB+1 + (i*2), cum);
167 print_debug("DRB = ");
168 print_debug_hex32(pci_read_config32(ctrl->f0, DRB));
172 /* set TOM top of memory */
173 pci_write_config16(ctrl->f0, TOM, cum);
174 print_debug("TOM = ");
175 print_debug_hex16(cum);
177 /* set TOLM top of low memory */
182 pci_write_config16(ctrl->f0, TOLM, cum);
183 print_debug("TOLM = ");
184 print_debug_hex16(cum);
190 static u8 spd_detect_dimms(const struct mem_controller *ctrl)
194 for (i = 0; i < DIMM_SOCKETS; i++) {
197 device = ctrl->channel0[i];
199 byte = spd_read_byte(device, SPD_MEMORY_TYPE);
201 print_debug_hex8(device);
203 print_debug_hex8(byte);
206 dimm_mask |= (1 << i);
214 static int spd_set_row_attributes(const struct mem_controller *ctrl,
220 for (i = 0; i < DIMM_SOCKETS; i++) {
224 if (!(dimm_mask & (1 << i))) {
228 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_ROWS);
229 if (value < 0) die("Bad SPD data\n");
230 if ((value & 0xf) == 0) die("Invalid # of rows\n");
231 dra |= (((value-13) & 0x7) << 23);
232 dra |= (((value-13) & 0x7) << 29);
235 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_COLUMNS);
236 if (value < 0) die("Bad SPD data\n");
237 if ((value & 0xf) == 0) die("Invalid # of columns\n");
238 dra |= (((value-10) & 0x7) << 20);
239 dra |= (((value-10) & 0x7) << 26);
242 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_BANKS_PER_SDRAM);
243 if (value < 0) die("Bad SPD data\n");
244 if ((value & 0xff) == 0) die("Invalid # of banks\n");
245 reg += log2(value & 0xff);
247 print_debug("dimm ");
249 print_debug(" reg = ");
250 print_debug_hex8(reg);
253 /* set device density */
255 dra |= ((31-reg) << 6);
257 /* set device width (x8) */
261 /* set device type (registered) */
264 /* set number of ranks (0=single, 1=dual) */
265 value = spd_read_byte(ctrl->channel0[i], SPD_NUM_DIMM_BANKS);
266 dra |= ((value & 0x1) << 17);
271 print_debug_hex32(dra);
274 pci_write_config32(ctrl->f0, DRA + (i*4), dra);
280 static u32 spd_set_drt_attributes(const struct mem_controller *ctrl,
281 u8 dimm_mask, u32 drc)
291 int index = drc & 0x00000003;
293 static const u8 latencies[] = { /* 533, 800, 400, 667 */
294 0x10, 0x60, 0x10, 0x20 };
295 static const u32 drt0[] = { /* 533, 800, 400, 667 */
296 0x24240002, 0x24360002, 0x24220002, 0x24360002 };
297 static const u32 drt1[] = { /* 533, 800, 400, 667 */
298 0x00400000, 0x00900000, 0x00200000, 0x00700000 };
299 static const u32 magic[] = { /* 533, 800, 400, 667 */
300 0x007b8221, 0x00b94331, 0x005ca1a1, 0x009a62b1 };
301 static const u32 mrs[] = { /* 533, 800, 400, 667 */
302 0x07020000, 0x0b020000, 0x05020000, 0x09020000 };
303 static const int cycle[] = { /* 533, 800, 400, 667 */
304 15, 10, 20, 12 }; /* cycle time in 1/4 ns units */
305 static const int byte40rem[] = {
306 0, 1, 2, 2, 3, 3, 0, 0 }; /* byte 40 remainder in 1/4 ns units */
308 /* CAS latency in cycles */
309 val = latencies[index];
310 for (i = 0; i < DIMM_SOCKETS; i++) {
311 if (!(dimm_mask & (1 << i)))
313 val &= spd_read_byte(ctrl->channel0[i], SPD_ACCEPTABLE_CAS_LATENCIES);
322 die("CAS latency mismatch\n");
323 print_debug("cl = ");
324 print_debug_hex8(cl);
329 /* Trc, Trfc in cycles */
330 for (i = 0; i < DIMM_SOCKETS; i++) {
331 if (!(dimm_mask & (1 << i)))
333 val1 = spd_read_byte(ctrl->channel0[i], SPD_BYTE_41_42_EXTENSION);
334 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_ACT_TO_ACT_AUTO_REFRESH);
335 val <<= 2; /* convert to 1/4 ns */
336 val += byte40rem[(val1 >> 4) & 0x7];
337 val = (val + ci - 1) / ci + 1; /* convert to cycles */
340 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_AUTO_REFRESH_TO_ACT);
341 val <<= 2; /* convert to 1/4 ns */
344 val += byte40rem[(val1 >> 1) & 0x7];
345 val = (val + ci - 1) / ci; /* convert to cycles */
349 print_debug("trc = ");
350 print_debug_hex8(trc);
352 print_debug("trfc = ");
353 print_debug_hex8(trfc);
356 /* Tras, Trtp, Twtr in cycles */
357 for (i = 0; i < DIMM_SOCKETS; i++) {
358 if (!(dimm_mask & (1 << i)))
360 val = spd_read_byte(ctrl->channel0[i], SPD_MIN_ACTIVE_TO_PRECHARGE_DELAY);
361 val <<= 2; /* convert to 1/4 ns */
362 val = (val + ci - 1) / ci; /* convert to cycles */
365 val = spd_read_byte(ctrl->channel0[i], SPD_INT_READ_TO_PRECHARGE_DELAY);
366 val = (val + ci - 1) / ci; /* convert to cycles */
369 val = spd_read_byte(ctrl->channel0[i], SPD_INT_WRITE_TO_READ_DELAY);
370 val = (val + ci - 1) / ci; /* convert to cycles */
374 print_debug("tras = ");
375 print_debug_hex8(tras);
377 print_debug("trtp = ");
378 print_debug_hex8(trtp);
380 print_debug("twtr = ");
381 print_debug_hex8(twtr);
384 val = (drt0[index] | ((trc - 11) << 12) | ((cl - 3) << 9)
385 | ((cl - 3) << 6) | ((cl - 3) << 3));
386 print_debug("drt0 = ");
387 print_debug_hex32(val);
389 pci_write_config32(ctrl->f0, DRT0, val);
391 val = (drt1[index] | ((tras - 8) << 28) | ((trtp - 2) << 25)
393 print_debug("drt1 = ");
394 print_debug_hex32(val);
396 pci_write_config32(ctrl->f0, DRT1, val);
398 val = (magic[index]);
399 print_debug("magic = ");
400 print_debug_hex32(val);
402 pci_write_config32(PCI_DEV(0, 0x08, 0), 0xcc, val);
404 val = (mrs[index] | (cl << 20));
405 print_debug("mrs = ");
406 print_debug_hex32(val);
411 static int spd_set_dram_controller_mode(const struct mem_controller *ctrl,
420 for (i = 0; i < DIMM_SOCKETS; i++) {
421 if (!(dimm_mask & (1 << i)))
423 if ((spd_read_byte(ctrl->channel0[i], SPD_MODULE_DATA_WIDTH_LSB) & 0xf0) != 0x40)
424 die("ERROR: Only 64-bit DIMMs supported\n");
425 if (!(spd_read_byte(ctrl->channel0[i], SPD_DIMM_CONFIG_TYPE) & 0x02))
426 die("ERROR: Only ECC DIMMs supported\n");
427 if (spd_read_byte(ctrl->channel0[i], SPD_PRIMARY_SDRAM_WIDTH) != 0x08)
428 die("ERROR: Only x8 DIMMs supported\n");
430 value = spd_read_byte(ctrl->channel0[i], SPD_MIN_CYCLE_TIME_AT_CAS_MAX);
434 print_debug("cycle = ");
435 print_debug_hex8(cycle);
438 drc |= (1 << 20); /* enable ECC */
439 drc |= (3 << 30); /* enable CKE on each DIMM */
440 drc |= (1 << 4); /* enable CKE globally */
443 /* set front side bus speed */
444 msr = rdmsr(0xcd); /* returns 0 on Pentium M 90nm */
445 print_debug("msr 0xcd = ");
446 print_debug_hex32(msr.hi);
447 print_debug_hex32(msr.lo);
450 /* TODO check that this msr really indicates fsb speed! */
452 print_info("533 MHz FSB\n");
455 print_info("400 MHz DDR\n");
456 } else if (cycle <= 0x30) {
458 print_info("333 MHz DDR\n");
459 } else if (cycle <= 0x3d) {
461 print_info("266 MHz DDR\n");
464 print_info("200 MHz DDR\n");
468 print_info("400 MHz FSB\n");
471 print_info("333 MHz DDR\n");
472 } else if (cycle <= 0x3d) {
474 print_info("266 MHz DDR\n");
477 print_info("200 MHz DDR\n");
481 print_debug("DRC = ");
482 print_debug_hex32(drc);
488 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
493 /* Test if we can read the SPD */
494 dimm_mask = spd_detect_dimms(ctrl);
495 if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
496 print_err("No memory for this cpu\n");
502 static void set_on_dimm_termination_enable(const struct mem_controller *ctrl)
509 /* Set up northbridge values */
511 pci_write_config32(ctrl->f0, SDRC, 0xa0002c30);
513 c1 = pci_read_config8(ctrl->f0, DRB);
514 c2 = pci_read_config8(ctrl->f0, DRB+2);
516 /* 1 single-rank DIMM */
520 /* 2 single-rank DIMMs or 1 double-rank DIMM */
524 print_debug("ODT Value = ");
525 print_debug_hex32(data32);
528 pci_write_config32(ctrl->f0, DDR2ODTC, data32);
530 for (i = 0; i < 2; i++) {
531 print_debug("ODT CS");
535 write32(BAR+DCALADDR, 0x0b840001);
536 write32(BAR+DCALCSR, 0x80000003 | ((i+1)<<21));
537 do data32 = read32(BAR+DCALCSR);
538 while (data32 & 0x80000000);
543 static void dump_dcal_regs(void)
546 for (i = 0x0; i < 0x2a0; i += 4) {
549 print_debug_hex16(i);
552 print_debug_hex32(read32(BAR+i));
559 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
570 mask = spd_detect_dimms(ctrl);
571 print_debug("Starting SDRAM Enable\n");
573 /* Set DRAM type and Front Side Bus frequency */
574 drc = spd_set_dram_controller_mode(ctrl, mask);
576 die("Error calculating DRC\n");
578 data32 = drc & ~(3 << 20); /* clear ECC mode */
579 data32 = data32 | (3 << 5); /* temp turn off ODT */
580 /* Set DRAM controller mode */
581 pci_write_config32(ctrl->f0, DRC, data32);
583 /* Turn the clocks on */
584 pci_write_config16(ctrl->f0, CKDIS, 0x0000);
586 /* Program row size */
587 spd_set_ram_size(ctrl, mask);
589 /* Program row attributes */
590 spd_set_row_attributes(ctrl, mask);
592 /* Program timing values */
593 mode_reg = spd_set_drt_attributes(ctrl, mask, drc);
598 for (cs = 0; cs < 2; cs++) {
599 print_debug("NOP CS");
600 print_debug_hex8(cs);
603 write32(BAR+DCALCSR, (0x00000000 | ((cs+1)<<21)));
604 write32(BAR+DCALCSR, (0x80000000 | ((cs+1)<<21)));
605 do data32 = read32(BAR+DCALCSR);
606 while (data32 & 0x80000000);
611 for (cs = 0; cs < 2; cs++) {
612 print_debug("NOP CS");
613 print_debug_hex8(cs);
615 write32(BAR + DCALCSR, (0x80000000 | ((cs+1)<<21)));
616 do data32 = read32(BAR+DCALCSR);
617 while (data32 & 0x80000000);
620 /* Precharge all banks */
622 for (cs = 0; cs < 2; cs++) {
623 print_debug("Precharge CS");
624 print_debug_hex8(cs);
626 write32(BAR+DCALADDR, 0x04000000);
627 write32(BAR+DCALCSR, (0x80000002 | ((cs+1)<<21)));
628 do data32 = read32(BAR+DCALCSR);
629 while (data32 & 0x80000000);
632 /* EMRS: Enable DLLs, set OCD calibration mode to default */
634 for (cs = 0; cs < 2; cs++) {
635 print_debug("EMRS CS");
636 print_debug_hex8(cs);
638 write32(BAR+DCALADDR, 0x0b840001);
639 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
640 do data32 = read32(BAR+DCALCSR);
641 while (data32 & 0x80000000);
643 /* MRS: Reset DLLs */
645 for (cs = 0; cs < 2; cs++) {
646 print_debug("MRS CS");
647 print_debug_hex8(cs);
649 write32(BAR+DCALADDR, mode_reg);
650 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
651 do data32 = read32(BAR+DCALCSR);
652 while (data32 & 0x80000000);
655 /* Precharge all banks */
657 for (cs = 0; cs < 2; cs++) {
658 print_debug("Precharge CS");
659 print_debug_hex8(cs);
661 write32(BAR+DCALADDR, 0x04000000);
662 write32(BAR+DCALCSR, (0x80000002 | ((cs+1)<<21)));
663 do data32 = read32(BAR+DCALCSR);
664 while (data32 & 0x80000000);
668 for (i = 0; i < 2; i++) {
670 for (cs = 0; cs < 2; cs++) {
671 print_debug("Refresh CS");
672 print_debug_hex8(cs);
674 write32(BAR+DCALCSR, (0x80000001 | ((cs+1)<<21)));
675 do data32 = read32(BAR+DCALCSR);
676 while (data32 & 0x80000000);
680 /* MRS: Set DLLs to normal */
682 for (cs = 0; cs < 2; cs++) {
683 print_debug("MRS CS");
684 print_debug_hex8(cs);
686 write32(BAR+DCALADDR, (mode_reg & ~(1<<24)));
687 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
688 do data32 = read32(BAR+DCALCSR);
689 while (data32 & 0x80000000);
692 /* EMRS: Enable DLLs */
694 for (cs = 0; cs < 2; cs++) {
695 print_debug("EMRS CS");
696 print_debug_hex8(cs);
698 write32(BAR+DCALADDR, 0x0b840001);
699 write32(BAR+DCALCSR, (0x80000003 | ((cs+1)<<21)));
700 do data32 = read32(BAR+DCALCSR);
701 while (data32 & 0x80000000);
706 write32(BAR+DCALCSR, 0x0000000f);
708 write32(BAR, 0x00100000);
710 /* Enable on-DIMM termination */
711 set_on_dimm_termination_enable(ctrl);
715 /* Receive enable calibration */
717 for (cs = 0; cs < 1; cs++) {
718 print_debug("receive enable calibration CS");
719 print_debug_hex8(cs);
721 write32(BAR+DCALCSR, (0x8000000c | ((cs+1)<<21)));
722 do data32 = read32(BAR+DCALCSR);
723 while (data32 & 0x80000000);
729 data32 = read32(BAR+DDRIOMC2);
730 data32 &= ~(0xf << 16);
731 data32 |= (0xb << 16);
732 write32(BAR+DDRIOMC2, data32);
736 data32 = drc & ~(3 << 20); /* clear ECC mode */
737 pci_write_config32(ctrl->f0, DRC, data32);
738 write32(BAR+DCALCSR, 0x0008000f);
740 /* Clear memory and init ECC */
741 for (cs = 0; cs < 2; cs++) {
742 if (!(mask & (1<<cs)))
744 print_debug("clear memory CS");
745 print_debug_hex8(cs);
747 write32(BAR+MBCSR, 0xa00000f0 | ((cs+1)<<20) | (0<<16));
748 do data32 = read32(BAR+MBCSR);
749 while (data32 & 0x80000000);
750 if (data32 & 0x40000000)
751 print_debug("failed!\n");
754 /* Clear read/write FIFO pointers */
755 print_debug("clear read/write fifo pointers\n");
756 write32(BAR+DDRIOMC2, read32(BAR+DDRIOMC2) | (1<<15));
758 write32(BAR+DDRIOMC2, read32(BAR+DDRIOMC2) & ~(1<<15));
763 print_debug("Done\n");
765 /* Set initialization complete */
768 data32 = drc & ~(3 << 20); /* clear ECC mode */
769 pci_write_config32(ctrl->f0, DRC, data32);
771 /* Set the ECC mode */
772 pci_write_config32(ctrl->f0, DRC, drc);
774 /* The memory is now set up--use it */
775 cache_lbmem(MTRR_TYPE_WRBACK);
778 static inline int memory_initialized(void)
780 return pci_read_config32(PCI_DEV(0, 0x00, 0), DRC) & (1 << 29);