2 * This file is part of the coreboot project.
4 * Copyright (C) 2007-2009 coresystems GmbH
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <sdram_mode.h>
25 #include "cx700_registers.h"
27 /* Debugging macros. */
28 #if CONFIG_DEBUG_RAM_SETUP
29 #define PRINTK_DEBUG(x...) printk(BIOS_DEBUG, x)
31 #define PRINTK_DEBUG(x...)
34 #define RAM_COMMAND_NORMAL 0x0
35 #define RAM_COMMAND_NOP 0x1
36 #define RAM_COMMAND_PRECHARGE 0x2
37 #define RAM_COMMAND_MRS 0x3
38 #define RAM_COMMAND_CBR 0x4
40 #define HOSTCTRL PCI_DEV(0, 0, 2)
41 #define MEMCTRL PCI_DEV(0, 0, 3)
52 #ifdef MEM_WIDTH_32BIT_MODE
53 #define SDRAM1X_RA_14 30
54 #define SDRAM1X_RA_13 29
55 #define SDRAM1X_RA_12 28
56 #define SDRAM1X_RA_12_8bk 26
57 #define SDRAM1X_CA_12 15
58 #define SDRAM1X_CA_11 14
59 #define SDRAM1X_CA_09 11
60 #define SDRAM1X_CA_09_8bk 11
61 #define SDRAM1X_BA1 13
62 #define SDRAM1X_BA2_8bk 14
63 #define SDRAM1X_BA1_8bk 13
65 #define SDRAM1X_RA_14 31
66 #define SDRAM1X_RA_13 30
67 #define SDRAM1X_RA_12 29
68 #define SDRAM1X_RA_12_8bk 27
69 #define SDRAM1X_CA_12 16
70 #define SDRAM1X_CA_11 15
71 #define SDRAM1X_CA_09 12
72 #define SDRAM1X_CA_09_8bk 12
73 #define SDRAM1X_BA1 14
74 #define SDRAM1X_BA2_8bk 15
75 #define SDRAM1X_BA1_8bk 14
78 #define MA_Column 0x06
81 #define MA_4_Bank 0x00
82 #define MA_8_Bank 0x08
83 #define MA_12_Row 0x00
84 #define MA_13_Row 0x10
85 #define MA_14_Row 0x20
86 #define MA_15_Row 0x30
87 #define MA_9_Column 0x00
88 #define MA_10_Column 0x02
89 #define MA_11_Column 0x04
90 #define MA_12_Column 0x06
92 #define GET_SPD(i, val, tmp, reg) \
96 for(i = 0; i < 2; i++) { \
97 if(pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_REG_BASE + (i << 1)))) { \
98 tmp = get_spd_data(ctrl, i, reg); \
105 #define REGISTERPRESET(bus,dev,fun,bdfspec) \
107 for (i=0; i<(sizeof((bdfspec))/sizeof(struct regmask)); i++) { \
108 printk(BIOS_DEBUG, "Writing bus " #bus " dev " #dev " fun " #fun " register "); \
109 printk(BIOS_DEBUG, "%02x", (bdfspec)[i].reg); \
110 printk(BIOS_DEBUG, "\n"); \
111 reg = pci_read_config8(PCI_DEV((bus), (dev), (fun)), (bdfspec)[i].reg); \
112 reg &= (bdfspec)[i].mask; \
113 reg |= (bdfspec)[i].val; \
114 pci_write_config8(PCI_DEV((bus), (dev), (fun)), (bdfspec)[i].reg, reg); \
119 static void do_ram_command(const struct mem_controller *ctrl, u8 command)
123 reg = pci_read_config8(MEMCTRL, 0x6b);
124 reg &= 0xf8; /* Clear bits 2-0. */
126 pci_write_config8(MEMCTRL, 0x6b, reg);
128 PRINTK_DEBUG(" Sending RAM command 0x%02x\n", reg);
131 // TODO factor out to another file
132 static void c7_cpu_setup(const struct mem_controller *ctrl)
135 size = sizeof(Reg_Val) / sizeof(Reg_Val[0]);
136 for (i = 0; i < size; i += 2)
137 pci_write_config8(HOSTCTRL, Reg_Val[i], Reg_Val[i + 1]);
140 static void ddr_detect(const struct mem_controller *ctrl)
142 /* FIXME: Only supports 2 ranks per DIMM */
147 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
148 val = get_spd_data(ctrl, dimm, 0);
149 if ((val == 0x80) || (val == 0xff)) {
151 rsize = get_spd_data(ctrl, dimm, SPD_RANK_SIZE);
153 rsize = (rsize << 3) | (rsize >> 5);
155 get_spd_data(ctrl, dimm,
156 SPD_MOD_ATTRIB_RANK) & SPD_MOD_ATTRIB_RANK_NUM_MASK;
159 pci_write_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_1 + (dimm << 1)),
161 rmap |= (1 << ((dimm << 1) + 1));
164 pci_write_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + (dimm << 1)),
166 rmap |= (1 << (dimm << 1));
171 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_DIMM_NUM, ndimm);
172 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM, nrank);
173 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_MAP, rmap);
176 static void sdram_set_safe_values(const struct mem_controller *ctrl)
178 /* The purpose of this function is to set initial values for the dram
179 * size and timings. It will be replaced with the SPD based function
180 * once the RAM commands are working with these values.
182 u8 regs, val, t, dimm;
185 regs = pci_read_config8(MEMCTRL, 0x6c);
187 printk(BIOS_DEBUG, "DDR2 Detected.\n");
189 die("ERROR: DDR1 memory detected but not supported by coreboot.\n");
193 pci_write_config8(MEMCTRL, 0x6c, regs);
195 /* SPD 5 # of ranks */
196 pci_write_config8(MEMCTRL, 0x6d, 0xc0);
198 /**********************************************/
199 /* Set DRAM Freq (DDR2 533) */
200 /**********************************************/
201 /* SPD 9 SDRAM Cycle Time */
202 GET_SPD(dimm, spds, regs, 9);
204 printk(BIOS_DEBUG, "\nDDRII ");
206 printk(BIOS_DEBUG, "533");
209 } else if (spds <= 0x50) {
210 printk(BIOS_DEBUG, "400");
213 } else if (spds <= 0x60) {
214 printk(BIOS_DEBUG, "333");
217 } else if (spds <= 0x75) {
218 printk(BIOS_DEBUG, "266");
222 printk(BIOS_DEBUG, "200");
226 /* To store DDRII frequence */
227 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ, val);
229 /* Manual reset and adjust DLL when DRAM change frequency
230 * This is a necessary sequence.
233 regs = pci_read_config8(MEMCTRL, 0x90);
235 pci_write_config8(MEMCTRL, 0x90, regs);
237 regs = pci_read_config8(MEMCTRL, 0x90);
240 pci_write_config8(MEMCTRL, 0x90, regs);
242 regs = pci_read_config8(MEMCTRL, 0x6b);
245 pci_write_config8(MEMCTRL, 0x6b, regs);
248 pci_write_config8(MEMCTRL, 0x6b, regs);
251 pci_write_config8(MEMCTRL, 0x6b, regs);
252 regs = pci_read_config8(MEMCTRL, 0x6f);
254 pci_write_config8(MEMCTRL, 0x6f, regs);
256 /**********************************************/
257 /* Set DRAM Timing Setting (DDR2 533) */
258 /**********************************************/
259 /* SPD 9 18 23 25 CAS Latency NB3DRAM_REG62[2:0] */
260 /* Read SPD byte 18 CAS Latency */
261 GET_SPD(dimm, spds, regs, SPD_CAS_LAT);
262 printk(BIOS_DEBUG, "\nCAS Supported ");
263 if (spds & SPD_CAS_LAT_2)
264 printk(BIOS_DEBUG, "2 ");
265 if (spds & SPD_CAS_LAT_3)
266 printk(BIOS_DEBUG, "3 ");
267 if (spds & SPD_CAS_LAT_4)
268 printk(BIOS_DEBUG, "4 ");
269 if (spds & SPD_CAS_LAT_5)
270 printk(BIOS_DEBUG, "5 ");
271 if (spds & SPD_CAS_LAT_6)
272 printk(BIOS_DEBUG, "6");
274 /* We don't consider CAS = 6, because CX700 doesn't support it */
275 printk(BIOS_DEBUG, "\n CAS:");
276 if (spds & SPD_CAS_LAT_5) {
277 printk(BIOS_DEBUG, "Starting at CL5");
279 /* See whether we can improve it */
280 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_1);
281 if ((spds & SPD_CAS_LAT_4) && (tmp < 0x50)) {
282 printk(BIOS_DEBUG, "\n... going to CL4");
285 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_2);
286 if ((spds & SPD_CAS_LAT_3) && (tmp < 0x50)) {
287 printk(BIOS_DEBUG, "\n... going to CL3");
291 printk(BIOS_DEBUG, "Starting at CL4");
293 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_1);
294 if ((spds & SPD_CAS_LAT_3) && (tmp < 0x50)) {
295 printk(BIOS_DEBUG, "\n... going to CL3");
298 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_2);
299 if ((spds & SPD_CAS_LAT_2) && (tmp < 0x50)) {
300 printk(BIOS_DEBUG, "\n... going to CL2");
304 regs = pci_read_config8(MEMCTRL, 0x62);
307 pci_write_config8(MEMCTRL, 0x62, regs);
309 /* SPD 27 Trp NB3DRAM_REG64[3:2] */
310 GET_SPD(dimm, spds, regs, SPD_TRP);
311 printk(BIOS_DEBUG, "\nTrp %d", spds);
313 for (val = 2; val <= 5; val++) {
314 if (spds <= (val * t / 10)) {
320 regs = pci_read_config8(MEMCTRL, 0x64);
323 pci_write_config8(MEMCTRL, 0x64, regs);
325 /* SPD 29 Trcd NB3DRAM_REG64[7:6] */
326 GET_SPD(dimm, spds, regs, SPD_TRCD);
327 printk(BIOS_DEBUG, "\nTrcd %d", spds);
329 for (val = 2; val <= 5; val++) {
330 if (spds <= (val * t / 10)) {
336 regs = pci_read_config8(MEMCTRL, 0x64);
339 pci_write_config8(MEMCTRL, 0x64, regs);
341 /* SPD 30 Tras NB3DRAM_REG62[7:4] */
342 GET_SPD(dimm, spds, regs, SPD_TRAS);
343 printk(BIOS_DEBUG, "\nTras %d", spds);
344 for (val = 5; val <= 20; val++) {
345 if (spds <= (val * t / 10)) {
351 regs = pci_read_config8(MEMCTRL, 0x62);
354 pci_write_config8(MEMCTRL, 0x62, regs);
356 /* SPD 42 SPD 40 Trfc NB3DRAM_REG61[5:0] */
357 GET_SPD(dimm, spds, regs, SPD_TRFC);
358 printk(BIOS_DEBUG, "\nTrfc %d", spds);
360 GET_SPD(dimm, spds, regs, SPD_EX_TRC_TRFC);
365 for (val = 8; val <= 71; val++) {
366 if (tmp <= (val * t / 10)) {
371 regs = pci_read_config8(MEMCTRL, 0x61);
374 pci_write_config8(MEMCTRL, 0x61, regs);
376 /* SPD 28 Trrd NB3DRAM_REG63[7:6] */
377 GET_SPD(dimm, spds, regs, SPD_TRRD);
378 for (val = 2; val <= 5; val++) {
379 if (spds <= (val * t / 10)) {
385 printk(BIOS_DEBUG, "\nTrrd val = 0x%x", val);
386 regs = pci_read_config8(MEMCTRL, 0x63);
389 pci_write_config8(MEMCTRL, 0x63, regs);
391 /* SPD 36 Twr NB3DRAM_REG61[7:6] */
392 GET_SPD(dimm, spds, regs, SPD_TWR);
393 for (val = 2; val <= 5; val++) {
394 if (spds <= (val * t / 10)) {
400 printk(BIOS_DEBUG, "\nTwr val = 0x%x", val);
402 regs = pci_read_config8(MEMCTRL, 0x61);
405 pci_write_config8(MEMCTRL, 0x61, regs);
407 /* SPD 37 Twtr NB3DRAM_REG63[1] */
408 GET_SPD(dimm, spds, regs, SPD_TWTR);
410 printk(BIOS_DEBUG, "\nTwtr 0x%x", spds);
411 if (spds <= (t * 2 / 10))
416 printk(BIOS_DEBUG, "\nTwtr val = 0x%x", val);
418 regs = pci_read_config8(MEMCTRL, 0x63);
421 pci_write_config8(MEMCTRL, 0x63, regs);
423 /* SPD 38 Trtp NB3DRAM_REG63[3] */
424 GET_SPD(dimm, spds, regs, SPD_TRTP);
426 printk(BIOS_DEBUG, "\nTrtp 0x%x", spds);
427 if (spds <= (t * 2 / 10))
432 printk(BIOS_DEBUG, "\nTrtp val = 0x%x", val);
434 regs = pci_read_config8(MEMCTRL, 0x63);
437 pci_write_config8(MEMCTRL, 0x63, regs);
439 /**********************************************/
440 /* Set DRAM DRDY Setting */
441 /**********************************************/
442 /* Write slowest value to register */
443 tmp = sizeof(Host_Reg_Val) / sizeof(Host_Reg_Val[0]);
444 for (val = 0; val < tmp; val += 2)
445 pci_write_config8(HOSTCTRL, Host_Reg_Val[val], Host_Reg_Val[val + 1]);
447 /* F2_RX51[7]=0, disable DRDY timing */
448 regs = pci_read_config8(HOSTCTRL, 0x51);
450 pci_write_config8(HOSTCTRL, 0x51, regs);
452 /**********************************************/
453 /* Set DRAM BurstLength */
454 /**********************************************/
455 regs = pci_read_config8(MEMCTRL, 0x6c);
456 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
457 if (pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_REG_BASE + (dimm << 1)))) {
458 spds = get_spd_data(ctrl, dimm, 16);
465 pci_write_config8(MEMCTRL, 0x6c, regs);
466 val = pci_read_config8(HOSTCTRL, 0x54);
470 pci_write_config8(HOSTCTRL, 0x54, val);
472 /**********************************************/
473 /* Set DRAM Driving Setting */
474 /**********************************************/
475 /* DRAM Timing ODT */
476 tmp = sizeof(Dram_Driving_ODT_CTRL) / sizeof(Dram_Driving_ODT_CTRL[0]);
477 for (val = 0; val < tmp; val += 2)
478 pci_write_config8(MEMCTRL, Dram_Driving_ODT_CTRL[val],
479 Dram_Driving_ODT_CTRL[val + 1]);
481 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
482 val = pci_read_config8(MEMCTRL, 0xd5);
492 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DIMM_NUM);
495 pci_write_config8(MEMCTRL, 0xd5, val);
497 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DIMM_NUM);
498 val = pci_read_config8(MEMCTRL, 0xd6);
502 pci_write_config8(MEMCTRL, 0xd6, val);
504 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_MAP);
505 tmp = sizeof(ODT_TBL) / sizeof(ODT_TBL[0]);
506 for (val = 0; val < tmp; val += 3) {
507 if (regs == ODT_TBL[val]) {
508 pci_write_config8(MEMCTRL, 0xd8, ODT_TBL[val + 1]);
509 /* Store DRAM & NB ODT setting in d0f4_Rxd8 */
510 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_NB_ODT, ODT_TBL[val + 2]);
515 pci_write_config8(MEMCTRL, 0xd9, 0x0a);
516 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
519 pci_write_config8(MEMCTRL, 0xe0, DQS_DQ_TBL[regs++]);
520 pci_write_config8(MEMCTRL, 0xe2, DQS_DQ_TBL[regs]);
523 pci_write_config8(MEMCTRL, 0xe4, 0x66);
525 /* DRAM Timing MAA */
527 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
528 if (pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_REG_BASE + (dimm << 1)))) {
529 spds = get_spd_data(ctrl, dimm, SPD_PRI_WIDTH);
532 (PCI_DEV(0, 0, 4), (SCRATCH_REG_BASE + (dimm << 1) + 1)))
537 printk(BIOS_DEBUG, "\nchip #%d", val);
542 pci_write_config8(MEMCTRL, 0xe8, regs);
544 /* DRAM Timing MAB */
545 pci_write_config8(MEMCTRL, 0xe9, 0x0);
547 /* DRAM Timing DCLK VT8454C always 0x66 */
548 pci_write_config8(MEMCTRL, 0xe6, 0xaa);
550 /**********************************************/
551 /* Set DRAM Duty Control */
552 /**********************************************/
553 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
556 case 2: /* 1~2 rank */
560 case 4: /* 3~4 rank */
561 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
562 if (regs == DDRII_533)
569 for (t = 0; t < 4; t++) {
570 pci_write_config8(MEMCTRL, regs, Duty_Control_DDR2[val]);
575 /**********************************************/
576 /* Set DRAM Clock Control */
577 /**********************************************/
578 /* Write Data Phase */
579 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
580 regs = pci_read_config8(MEMCTRL, 0x75);
584 pci_write_config8(MEMCTRL, 0x74, 0x07);
589 pci_write_config8(MEMCTRL, 0x74, 0x05);
593 pci_write_config8(MEMCTRL, 0x75, regs);
594 pci_write_config8(MEMCTRL, 0x76, 0x80);
596 /* Clock Phase Control for FeedBack Mode */
597 regs = pci_read_config8(MEMCTRL, 0x90);
599 pci_write_config8(MEMCTRL, 0x90, regs);
601 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
604 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
615 regs = pci_read_config8(MEMCTRL, 0x91);
618 pci_write_config8(MEMCTRL, 0x91, regs);
620 for (t = 0; t < 3; t++) {
621 dimm = pci_read_config8(MEMCTRL, regs);
623 dimm |= ChA_Clk_Phase_DDR2_Table[val];
624 pci_write_config8(MEMCTRL, regs, dimm);
629 pci_write_config8(MEMCTRL, 0x97, 0x12);
630 pci_write_config8(MEMCTRL, 0x98, 0x33);
632 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_0);
633 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_2);
635 pci_write_config8(MEMCTRL, 0x9d, 0x00);
637 pci_write_config8(MEMCTRL, 0x9d, 0x0f);
639 tmp = sizeof(DQ_DQS_Table) / sizeof(DQ_DQS_Table[0]);
640 for (val = 0; val < tmp; val += 2)
641 pci_write_config8(MEMCTRL, DQ_DQS_Table[val], DQ_DQS_Table[val + 1]);
642 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
643 if (regs == DDRII_533)
644 pci_write_config8(MEMCTRL, 0x7b, 0xa0);
646 pci_write_config8(MEMCTRL, 0x7b, 0x10);
648 /***************************************************/
649 /* Set necessary register before DRAM initialize */
650 /***************************************************/
651 tmp = sizeof(Mem_Reg_Init) / sizeof(Mem_Reg_Init[0]);
652 for (val = 0; val < tmp; val += 3) {
653 regs = pci_read_config8(MEMCTRL, Mem_Reg_Init[val]);
654 regs &= Mem_Reg_Init[val + 1];
655 regs |= Mem_Reg_Init[val + 2];
656 pci_write_config8(MEMCTRL, Mem_Reg_Init[val], regs);
658 regs = pci_read_config8(HOSTCTRL, 0x51);
659 regs &= 0xbf; // Clear bit 6 Disable Read Around Write
660 pci_write_config8(HOSTCTRL, 0x51, regs);
662 regs = pci_read_config8(HOSTCTRL, 0x54);
664 val = pci_read_config8(HOSTCTRL, 0x57);
674 pci_write_config8(HOSTCTRL, 0x57, val);
676 regs = pci_read_config8(HOSTCTRL, 0x51);
678 pci_write_config8(HOSTCTRL, 0x51, regs);
680 regs = pci_read_config8(MEMCTRL, 0x90);
685 regs = pci_read_config8(MEMCTRL, 0x76);
688 pci_write_config8(MEMCTRL, 0x76, regs);
690 regs = pci_read_config8(MEMCTRL, 0x6f);
692 pci_write_config8(MEMCTRL, 0x6f, regs);
694 /***************************************************/
695 /* Find suitable DQS value for ChA and ChB */
696 /***************************************************/
697 // Set DQS output delay for Channel A
698 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
699 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
715 for (t = 0; t < 2; t++)
716 pci_write_config8(MEMCTRL, (0x70 + t), DQSOChA_DDR2_Driving_Table[val + t]);
717 // Set DQS output delay for Channel B
718 pci_write_config8(MEMCTRL, 0x72, 0x0);
720 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_0);
721 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_2);
723 pci_write_config8(MEMCTRL, 0x73, 0xfd);
725 pci_write_config8(MEMCTRL, 0x73, 0x01);
728 static void sdram_set_registers(const struct mem_controller *ctrl)
732 sdram_set_safe_values(ctrl);
735 static void step_20_21(const struct mem_controller *ctrl)
742 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_NB_ODT);
743 if (val & DDR2_ODT_150ohm)
748 /* Step 21. Normal operation */
749 print_spew("RAM Enable 5: Normal operation\n");
750 do_ram_command(ctrl, RAM_COMMAND_NORMAL);
754 static void step_2_19(const struct mem_controller *ctrl)
760 val = pci_read_config8(MEMCTRL, 0x69);
762 pci_write_config8(MEMCTRL, 0x69, val);
764 /* Step 3 Apply NOP. */
765 print_spew("RAM Enable 1: Apply NOP\n");
766 do_ram_command(ctrl, RAM_COMMAND_NOP);
771 print_spew("SEND: ");
778 /* 6. Precharge all. Wait tRP. */
779 print_spew("RAM Enable 2: Precharge all\n");
780 do_ram_command(ctrl, RAM_COMMAND_PRECHARGE);
783 print_spew("SEND: ");
787 /* Step 8. Mode register set. */
788 print_spew("RAM Enable 4: Mode register set\n");
789 do_ram_command(ctrl, RAM_COMMAND_MRS); //enable dll
792 print_spew("SEND: ");
794 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_NB_ODT);
795 if (val & DDR2_ODT_150ohm)
796 read32(0x102200); //DDR2_ODT_150ohm
802 print_spew("SEND: ");
806 /* Step 11. Precharge all. Wait tRP. */
807 print_spew("RAM Enable 2: Precharge all\n");
808 do_ram_command(ctrl, RAM_COMMAND_PRECHARGE);
811 print_spew("SEND: ");
815 /* Step 13. Perform 8 refresh cycles. Wait tRC each time. */
816 print_spew("RAM Enable 3: CBR\n");
817 do_ram_command(ctrl, RAM_COMMAND_CBR);
819 /* JEDEC says only twice, do 8 times for posterity */
820 // Step 16: Repeat Step 14 and 15 another 7 times
821 for (i = 0; i < 8; i++) {
830 /* Step 17. Mode register set. Wait 200us. */
831 print_spew("\nRAM Enable 4: Mode register set\n");
833 //safe value for now, BL=8, WR=4, CAS=4
834 do_ram_command(ctrl, RAM_COMMAND_MRS);
837 /* Use Single Chanel temporarily */
838 val = pci_read_config8(MEMCTRL, 0x6c);
839 if (val & 0x8) { /* Burst Length = 8 */
840 val = pci_read_config8(MEMCTRL, 0x62);
842 i = DDR2_MRS_table[4 + val];
844 val = pci_read_config8(MEMCTRL, 0x62);
846 i = DDR2_MRS_table[val];
850 val = pci_read_config8(MEMCTRL, 0x61);
852 i |= DDR2_Twr_table[val];
855 printk(BIOS_DEBUG, "MRS = %08x\n", i);
860 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_NB_ODT);
861 if (val & DDR2_ODT_150ohm)
862 read32(0x103e00); //EMRS OCD Default
867 static void sdram_set_vr(const struct mem_controller *ctrl, u8 num)
870 val = 0x54 + (num >> 1);
871 reg = pci_read_config8(MEMCTRL, val);
872 reg &= (0xf << (4 * (num & 0x1)));
873 reg |= (((0x8 | num) << 4) >> (4 * (num & 0x1)));
874 pci_write_config8(MEMCTRL, val, reg);
876 static void sdram_ending_addr(const struct mem_controller *ctrl, u8 num)
879 /* Set Ending Address */
881 reg = pci_read_config8(MEMCTRL, val);
883 pci_write_config8(MEMCTRL, val, reg);
884 /* Set Beginning Address */
886 pci_write_config8(MEMCTRL, val, 0x0);
889 static void sdram_clear_vr_addr(const struct mem_controller *ctrl, u8 num)
892 val = 0x54 + (num >> 1);
893 reg = pci_read_config8(MEMCTRL, val);
894 reg = ~(0x80 >> (4 * (num & 0x1)));
895 pci_write_config8(MEMCTRL, val, reg);
897 reg = pci_read_config8(MEMCTRL, val);
899 pci_write_config8(MEMCTRL, val, reg);
901 pci_write_config8(MEMCTRL, val, 0x0);
904 /* Perform sizing DRAM by dynamic method */
905 static void sdram_calc_size(const struct mem_controller *ctrl, u8 num)
908 ba = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_FLAGS);
912 write8((1 << SDRAM1X_RA_12_8bk), 0x0c);
917 write8((1 << SDRAM1X_CA_09_8bk), 0x0c);
922 write8((1 << SDRAM1X_BA2_8bk), 0x02);
924 write8((1 << SDRAM1X_BA1_8bk), 0x01);
929 write8((1 << SDRAM1X_RA_14), 0x0e);
931 write8((1 << SDRAM1X_RA_13), 0x0d);
933 write8((1 << SDRAM1X_RA_12), 0x0c);
938 write8((1 << SDRAM1X_CA_12), 0x0b);
940 write8((1 << SDRAM1X_CA_11), 0x0a);
942 write8((1 << SDRAM1X_CA_09), 0x09);
947 write8((1 << SDRAM1X_BA1), 0x01);
951 if (ra < 10 || ra > 15)
953 if (ca < 8 || ca > 12)
955 if (ba < 1 || ba > 3)
958 /* Calculate MA type save to scratch register */
997 pci_write_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK0_MA_REG + num), reg);
1000 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_256M_BIT, 1);
1002 /* Calculate rank size save to scratch register */
1003 ra = ra + ca + ba + 3 - 26; /* 1 unit = 64M */
1005 pci_write_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK0_SIZE_REG + num), ra);
1008 static void sdram_enable(const struct mem_controller *ctrl)
1016 /* Init Present Bank */
1017 val = sizeof(Init_Rank_Reg_Table) / sizeof(Init_Rank_Reg_Table[0]);
1018 for (i = 0; i < val; i++)
1019 pci_write_config8(MEMCTRL, Init_Rank_Reg_Table[i], 0x0);
1021 /* Init other banks */
1022 for (i = 0; i < 4; i++) {
1023 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1025 sdram_set_vr(ctrl, i);
1026 sdram_ending_addr(ctrl, i);
1029 sdram_clear_vr_addr(ctrl, i);
1033 #ifdef MEM_WIDTH_32BIT_MODE
1034 /****************************************************************/
1035 /* Set Dram 32bit Mode */
1036 /****************************************************************/
1037 reg8 = pci_read_config8(MEMCTRL, 0x6c);
1039 pci_write_config(MEMCTRL, 0x6c, reg8);
1042 /****************************************************************/
1043 /* Find the DQSI Low/High bound and save it to Scratch register */
1044 /****************************************************************/
1045 for (dl = 0; dl < 0x3f; dl += 2) {
1047 reg8 |= 0x80; /* Set Manual Mode */
1048 pci_write_config8(MEMCTRL, 0x77, reg8);
1049 for (i = 0; i < 4; i++) {
1050 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1052 sdram_set_vr(ctrl, i);
1053 sdram_ending_addr(ctrl, i);
1054 write32(0, 0x55555555);
1055 write32(4, 0x55555555);
1057 if (read32(0) != 0x55555555)
1059 if (read32(4) != 0x55555555)
1061 write32(0, 0xaaaaaaaa);
1062 write32(4, 0xaaaaaaaa);
1064 if (read32(0) != 0xaaaaaaaa)
1066 if (read32(4) != 0xaaaaaaaa)
1068 sdram_clear_vr_addr(ctrl, i);
1074 sdram_clear_vr_addr(ctrl, i);
1076 printk(BIOS_DEBUG, "\nDQSI Low %08x", dl);
1077 for (dh = dl; dh < 0x3f; dh += 2) {
1079 reg8 |= 0x80; /* Set Manual Mode */
1080 pci_write_config8(MEMCTRL, 0x77, reg8);
1081 for (i = 0; i < 4; i++) {
1082 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1084 sdram_set_vr(ctrl, i);
1085 sdram_ending_addr(ctrl, i);
1087 write32(0, 0x55555555);
1088 write32(4, 0x55555555);
1090 if (read32(0) != 0x55555555)
1092 if (read32(4) != 0x55555555)
1094 write32(0, 0xaaaaaaaa);
1095 write32(4, 0xaaaaaaaa);
1097 if (read32(0) != 0xaaaaaaaa)
1099 if (read32(4) != 0xaaaaaaaa)
1101 sdram_clear_vr_addr(ctrl, i);
1105 sdram_clear_vr_addr(ctrl, i);
1109 printk(BIOS_DEBUG, "\nDQSI High %02x", dh);
1110 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_CHA_DQSI_LOW_REG, dl);
1111 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_CHA_DQSI_HIGH_REG, dh);
1112 reg8 = pci_read_config8(MEMCTRL, 0X90) & 0X7;
1113 val = DQSI_Rate_Table[reg8];
1118 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_ChA_DQSI_REG, val);
1121 pci_write_config8(MEMCTRL, 0x77, reg8);
1123 /****************************************************************/
1124 /* Find out the lowest Bank Interleave and Set Register */
1125 /****************************************************************/
1128 reg8 = pci_read_config8(MEMCTRL, 0x69);
1130 reg8 |= 0x80; //8 banks
1131 pci_write_config8(MEMCTRL, 0x69, reg8);
1134 for (i = 0; i < 4; i++) {
1135 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1137 reg8 = get_spd_data(ctrl, (i >> 1), 17);
1138 sdram_set_vr(ctrl, i);
1139 sdram_ending_addr(ctrl, i);
1143 write8((1 << SDRAM1X_BA1), 0x01);
1148 write8((1 << SDRAM1X_BA2_8bk), 0x02);
1150 write8((1 << SDRAM1X_BA1_8bk), 0x01);
1155 sdram_clear_vr_addr(ctrl, i);
1159 reg8 = pci_read_config8(MEMCTRL, 0x69);
1162 pci_write_config8(MEMCTRL, 0x69, reg8);
1164 /****************************************************************/
1165 /* DRAM Sizing and Fill MA type */
1166 /****************************************************************/
1167 for (i = 0; i < 4; i++) {
1168 val = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1170 reg8 = get_spd_data(ctrl, (i >> 1), 17);
1171 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_FLAGS, reg8);
1173 /* Use MA Type 3 for DRAM sizing */
1174 reg8 = pci_read_config8(MEMCTRL, 0x50);
1177 pci_write_config8(MEMCTRL, 0x50, reg8);
1178 pci_write_config8(MEMCTRL, 0x51, reg8);
1180 /* Use MA Type 5 for DRAM sizing */
1181 reg8 = pci_read_config8(MEMCTRL, 0x50);
1184 pci_write_config8(MEMCTRL, 0x50, reg8);
1185 pci_write_config8(MEMCTRL, 0x51, reg8);
1186 reg8 = pci_read_config8(MEMCTRL, 0x53);
1189 pci_write_config8(MEMCTRL, 0x53, reg8);
1191 sdram_set_vr(ctrl, i);
1193 reg8 = pci_read_config8(MEMCTRL, val);
1194 /* max size 3G for new MA table */
1196 pci_write_config8(MEMCTRL, val, reg8);
1197 /* Set Beginning Address */
1199 pci_write_config8(MEMCTRL, val, 0x0);
1201 sdram_calc_size(ctrl, i);
1204 val = 0x54 + (i >> 1);
1205 reg8 = pci_read_config8(MEMCTRL, val);
1206 reg8 = ~(0x80 >> (4 * (i & 0x1)));
1207 pci_write_config8(MEMCTRL, val, reg8);
1209 reg8 = pci_read_config8(MEMCTRL, val);
1211 pci_write_config8(MEMCTRL, val, reg8);
1213 pci_write_config8(MEMCTRL, val, 0x0);
1218 reg8 = pci_read_config8(MEMCTRL, 0x50);
1220 pci_write_config8(MEMCTRL, 0x50, reg8);
1221 pci_write_config8(MEMCTRL, 0x51, reg8);
1222 reg8 = pci_read_config8(MEMCTRL, 0x6b);
1224 pci_write_config8(MEMCTRL, 0x6b, reg8);
1226 /****************************************************************/
1227 /* DRAM re-initialize for burst length */
1228 /****************************************************************/
1229 for (i = 0; i < 4; i++) {
1230 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1232 sdram_set_vr(ctrl, i);
1233 sdram_ending_addr(ctrl, i);
1236 sdram_clear_vr_addr(ctrl, i);
1240 /****************************************************************/
1241 /* Set the MA Type */
1242 /****************************************************************/
1243 reg8 = pci_read_config8(MEMCTRL, 0x50);
1245 pci_write_config8(MEMCTRL, 0x50, reg8);
1247 reg8 = pci_read_config8(MEMCTRL, 0x51);
1249 pci_write_config8(MEMCTRL, 0x51, reg8);
1251 reg8 = pci_read_config8(MEMCTRL, 0x6b);
1253 pci_write_config8(MEMCTRL, 0x6b, reg8);
1255 for (i = 0; i < 4; i += 2) {
1256 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1258 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK0_MA_REG + i));
1259 reg8 &= (MA_Bank + MA_Column);
1260 val = pci_read_config8(MEMCTRL, 0x50);
1267 pci_write_config8(MEMCTRL, 0x50, val);
1271 /****************************************************************/
1272 /* Set Start and Ending Address */
1273 /****************************************************************/
1274 dl = 0; /* Begin Address */
1275 dh = 0; /* Ending Address */
1276 for (i = 0; i < 4; i++) {
1277 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1279 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK0_SIZE_REG + i));
1283 pci_write_config8(MEMCTRL, (0x40 + i), dh);
1284 pci_write_config8(MEMCTRL, (0x48 + i), dl);
1289 // F7_Rx57 Ending address mirror register
1290 pci_write_config8(PCI_DEV(0, 0, 7), 0x57, dh);
1291 dev = pci_locate_device(PCI_ID(0x1106, 0x324e), 0);
1292 pci_write_config8(dev, 0x57, dh);
1294 pci_write_config8(MEMCTRL, 0x88, dh);
1295 pci_write_config8(MEMCTRL, 0x85, dh);
1296 // also program vlink mirror
1297 pci_write_config8(PCI_DEV(0, 0, 7), 0xe5, dh);
1299 /****************************************************************/
1300 /* Set Physical to Virtual Rank mapping */
1301 /****************************************************************/
1302 pci_write_config32(MEMCTRL, 0x54, 0x0);
1303 for (i = 0; i < 4; i++) {
1304 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1306 reg8 = pci_read_config8(MEMCTRL, (0x54 + (i >> 1)));
1307 if (i & 0x1) { /* Odd Rank */
1310 } else { /* Even Rank */
1313 reg8 |= ((0x8 | i) << 4);
1315 pci_write_config8(MEMCTRL, (0x54 + (i >> 1)), reg8);
1319 /****************************************************************/
1320 /* Set DRAM Refresh Counter */
1321 /****************************************************************/
1322 val = pci_read_config8(MEMCTRL, 0X90) & 0X7;
1324 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_256M_BIT);
1327 pci_write_config8(MEMCTRL, 0x6a, REFC_Table[val]);
1329 /****************************************************************/
1330 /* Chipset Performance UP and other setting after DRAM Sizing */
1331 /****************************************************************/
1332 /* Dram Registers */
1333 val = sizeof(Dram_Table) / sizeof(Dram_Table[0]);
1334 for (i = 0; i < val; i += 3) {
1335 reg8 = pci_read_config8(MEMCTRL, Dram_Table[i]);
1336 reg8 &= Dram_Table[i + 1];
1337 reg8 |= Dram_Table[i + 2];
1338 pci_write_config8(MEMCTRL, Dram_Table[i], reg8);
1341 /* Host Registers */
1342 val = sizeof(Host_Table) / sizeof(Host_Table[0]);
1343 for (i = 0; i < val; i += 3) {
1344 reg8 = pci_read_config8(HOSTCTRL, Host_Table[i]);
1345 reg8 &= Host_Table[i + 1];
1346 reg8 |= Host_Table[i + 2];
1347 pci_write_config8(HOSTCTRL, Host_Table[i], reg8);
1351 #ifdef SETUP_PM_REGISTERS
1352 val = sizeof(PM_Table) / sizeof(PM_Table[0]);
1353 for (i = 0; i < val; i += 3) {
1354 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), PM_Table[i]);
1355 reg8 &= PM_Table[i + 1];
1356 reg8 |= PM_Table[i + 2];
1357 pci_write_config8(PCI_DEV(0, 0, 4), PM_Table[i], reg8);
1360 pci_write_config8(HOSTCTRL, 0x5d, 0xb2);
1362 /****************************************************************/
1363 /* UMA registers for N-series projects */
1364 /****************************************************************/
1366 /* Manual setting frame buffer bank */
1367 for (i = 0; i < 4; i++) {
1368 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1372 pci_write_config8(MEMCTRL, 0xb0, val);
1373 reg8 = 0x40; // Frame buffer size 64M
1374 reg8 |= 0x80; // VGA Enable
1375 reg8 |= 0x0a; // A[31:28] = 1010b
1376 pci_write_config8(MEMCTRL, 0xa1, reg8);
1380 outl(0x80000180, 0xcf8);
1383 outl(0x80000188, 0xcf8);
1386 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0xa5);
1388 pci_write_config8(PCI_DEV(0, 0, 0), 0xa5, reg8);
1390 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0x91);
1392 pci_write_config8(PCI_DEV(0, 0, 0), 0x91, reg8);
1395 static const struct regmask {
1400 { 0x40, 0x00, 0x8b},
1401 { 0x41, 0x80, 0x43},
1402 { 0x42, 0x00, 0x62},
1403 { 0x43, 0x00, 0x44},
1404 { 0x44, 0x00, 0x34},
1407 { 0x53, 0xf0, 0x0f},
1408 { 0x60, 0x00, 0x03},
1409 { 0x65, 0x00, 0xd9},
1410 { 0x66, 0x00, 0x80},
1411 { 0x67, 0x00, 0x00},
1412 { 0x68, 0x00, 0x01},
1413 { 0x69, 0xe0, 0x03},
1414 { 0x6b, 0x00, 0x10},
1415 { 0x6c, 0xc1, 0x08},
1416 { 0x6e, 0x00, 0x89},
1417 { 0x6f, 0x00, 0x51},
1418 { 0x75, ~0x40, 0x40},
1419 { 0x76, 0x8f, 0x00},
1420 { 0x7b, 0x00, 0xa0},
1421 { 0x86, 0x01, 0x24},
1422 { 0x86, 0x04, 0x29},
1423 { 0x8c, 0x00, 0x00},
1424 { 0x8d, 0x00, 0x00},
1425 { 0x95, ~0x40, 0x00},
1426 { 0xa2, 0x00, 0x44},
1429 { 0x4d, 0x00, 0x24},
1430 { 0x4f, 0x00, 0x01},
1431 { 0xbc, 0x00, 0x21},
1432 { 0xbe, 0x00, 0x00},
1435 { 0x40, ~0x01, 0x01}, // enable timer/counter shadow registers
1436 { 0x67, ~0x03, 0x01},
1437 { 0x5b, ~0x01, 0x00},
1438 { 0x8d, ~0x02, 0x02},
1439 { 0x97, ~0x80, 0x00},
1440 { 0xd2, ~0x18, 0x00},
1441 { 0xe2, ~0x36, 0x06},
1442 { 0xe4, ~0x80, 0x00},
1443 { 0xe5, 0x00, 0x40},
1444 { 0xe6, 0x00, 0x20},
1445 { 0xe7, ~0xd0, 0xc0},
1446 { 0xec, ~0x08, 0x00}
1448 { 0x4e, ~0x80, 0x80},
1449 { 0x4f, ~(1 << 6), 1 << 6 }, /* PG_CX700: 14.1.1 enable P2P Bridge Header for External PCI Bus */
1450 { 0x74, ~0x00, 0x04}, /* PG_CX700: 14.1.2 APIC FSB directly up to snmic, not on pci */
1451 { 0x7c, ~0x00, 0x02}, /* PG_CX700: 14.1.1 APIC FSB directly up to snmic, not on pci */
1452 { 0xe6, 0x0, 0x04} // MSI post
1453 }, b0d19f0[] = { /* P2PE */
1454 { 0x42, ~0x08, 0x08}, // Disable HD Audio,
1455 { 0x40, ~0xc0, 0x80} // 14.1.3.1.1 of the PG: extended cfg mode for pcie. enable capability, but don't activate
1457 { 0x50, ~0x40, 0x88},
1458 { 0x51, 0x80, 0x7b},
1459 { 0x52, 0x90, 0x6f},
1460 { 0x53, 0x00, 0x88},
1461 { 0x54, 0xe4, 0x16},
1462 { 0x55, 0xf2, 0x04},
1463 { 0x56, 0x0f, 0x00},
1464 { 0x57, ~0x04, 0x00},
1465 { 0x5d, 0x00, 0xb2},
1466 { 0x5e, 0x00, 0x88},
1467 { 0x5f, 0x00, 0xc7},
1471 REGISTERPRESET(0, 0, 0, b0d0f0);
1472 REGISTERPRESET(0, 0, 2, b0d0f2);
1473 REGISTERPRESET(0, 0, 3, b0d0f3);
1474 REGISTERPRESET(0, 1, 0, b0d1f0);
1475 REGISTERPRESET(0, 17, 0, b0d17f0);
1476 REGISTERPRESET(0, 17, 7, b0d17f7);
1477 REGISTERPRESET(0, 19, 0, b0d19f0);