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 // #define DEBUG_RAM_SETUP 1
29 /* Debugging macros. */
30 #if defined(DEBUG_RAM_SETUP)
31 #define PRINTK_DEBUG(x...) printk_debug(x)
33 #define PRINTK_DEBUG(x...)
36 #define RAM_COMMAND_NORMAL 0x0
37 #define RAM_COMMAND_NOP 0x1
38 #define RAM_COMMAND_PRECHARGE 0x2
39 #define RAM_COMMAND_MRS 0x3
40 #define RAM_COMMAND_CBR 0x4
42 #define HOSTCTRL PCI_DEV(0, 0, 2)
43 #define MEMCTRL PCI_DEV(0, 0, 3)
54 #ifdef MEM_WIDTH_32BIT_MODE
55 #define SDRAM1X_RA_14 30
56 #define SDRAM1X_RA_13 29
57 #define SDRAM1X_RA_12 28
58 #define SDRAM1X_RA_12_8bk 26
59 #define SDRAM1X_CA_12 15
60 #define SDRAM1X_CA_11 14
61 #define SDRAM1X_CA_09 11
62 #define SDRAM1X_CA_09_8bk 11
63 #define SDRAM1X_BA1 13
64 #define SDRAM1X_BA2_8bk 14
65 #define SDRAM1X_BA1_8bk 13
67 #define SDRAM1X_RA_14 31
68 #define SDRAM1X_RA_13 30
69 #define SDRAM1X_RA_12 29
70 #define SDRAM1X_RA_12_8bk 27
71 #define SDRAM1X_CA_12 16
72 #define SDRAM1X_CA_11 15
73 #define SDRAM1X_CA_09 12
74 #define SDRAM1X_CA_09_8bk 12
75 #define SDRAM1X_BA1 14
76 #define SDRAM1X_BA2_8bk 15
77 #define SDRAM1X_BA1_8bk 14
80 #define MA_Column 0x06
83 #define MA_4_Bank 0x00
84 #define MA_8_Bank 0x08
85 #define MA_12_Row 0x00
86 #define MA_13_Row 0x10
87 #define MA_14_Row 0x20
88 #define MA_15_Row 0x30
89 #define MA_9_Column 0x00
90 #define MA_10_Column 0x02
91 #define MA_11_Column 0x04
92 #define MA_12_Column 0x06
94 #define GET_SPD(i, val, tmp, reg) \
98 for(i = 0; i < 2; i++) { \
99 if(pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_REG_BASE + (i << 1)))) { \
100 tmp = get_spd_data(ctrl, i, reg); \
107 #define REGISTERPRESET(bus,dev,fun,bdfspec) \
109 for (i=0; i<(sizeof((bdfspec))/sizeof(struct regmask)); i++) { \
110 printk_debug("Writing bus " #bus " dev " #dev " fun " #fun " register "); \
111 printk_debug("%02x", (bdfspec)[i].reg); \
112 printk_debug("\n"); \
113 reg = pci_read_config8(PCI_DEV((bus), (dev), (fun)), (bdfspec)[i].reg); \
114 reg &= (bdfspec)[i].mask; \
115 reg |= (bdfspec)[i].val; \
116 pci_write_config8(PCI_DEV((bus), (dev), (fun)), (bdfspec)[i].reg, reg); \
121 static void do_ram_command(const struct mem_controller *ctrl, u8 command)
125 reg = pci_read_config8(MEMCTRL, 0x6b);
126 reg &= 0xf8; /* Clear bits 2-0. */
128 pci_write_config8(MEMCTRL, 0x6b, reg);
130 PRINTK_DEBUG(" Sending RAM command 0x%02x\n", reg);
133 // TODO factor out to another file
134 static void c7_cpu_setup(const struct mem_controller *ctrl)
137 size = sizeof(Reg_Val) / sizeof(Reg_Val[0]);
138 for (i = 0; i < size; i += 2)
139 pci_write_config8(HOSTCTRL, Reg_Val[i], Reg_Val[i + 1]);
142 static void ddr_detect(const struct mem_controller *ctrl)
144 /* FIXME: Only supports 2 ranks per DIMM */
149 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
150 val = get_spd_data(ctrl, dimm, 0);
151 if ((val == 0x80) || (val == 0xff)) {
153 rsize = get_spd_data(ctrl, dimm, SPD_RANK_SIZE);
155 rsize = (rsize << 3) | (rsize >> 5);
157 get_spd_data(ctrl, dimm,
158 SPD_MOD_ATTRIB_RANK) & SPD_MOD_ATTRIB_RANK_NUM_MASK;
161 pci_write_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_1 + (dimm << 1)),
163 rmap |= (1 << ((dimm << 1) + 1));
166 pci_write_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + (dimm << 1)),
168 rmap |= (1 << (dimm << 1));
173 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_DIMM_NUM, ndimm);
174 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM, nrank);
175 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_MAP, rmap);
178 static void sdram_set_safe_values(const struct mem_controller *ctrl)
180 /* The purpose of this function is to set initial values for the dram
181 * size and timings. It will be replaced with the SPD based function
182 * once the RAM commands are working with these values.
184 u8 regs, val, t, dimm;
187 regs = pci_read_config8(MEMCTRL, 0x6c);
189 printk_debug("DDR2 Detected.\n");
191 die("ERROR: DDR1 memory detected but not supported by coreboot.\n");
195 pci_write_config8(MEMCTRL, 0x6c, regs);
197 /* SPD 5 # of ranks */
198 pci_write_config8(MEMCTRL, 0x6d, 0xc0);
200 /**********************************************/
201 /* Set DRAM Freq (DDR2 533) */
202 /**********************************************/
203 /* SPD 9 SDRAM Cycle Time */
204 GET_SPD(dimm, spds, regs, 9);
206 printk_debug("\nDDRII ");
211 } else if (spds <= 0x50) {
215 } else if (spds <= 0x60) {
219 } else if (spds <= 0x75) {
228 /* To store DDRII frequence */
229 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ, val);
231 /* Manual reset and adjust DLL when DRAM change frequency
232 * This is a necessary sequence.
235 regs = pci_read_config8(MEMCTRL, 0x90);
237 pci_write_config8(MEMCTRL, 0x90, regs);
239 regs = pci_read_config8(MEMCTRL, 0x90);
242 pci_write_config8(MEMCTRL, 0x90, regs);
244 regs = pci_read_config8(MEMCTRL, 0x6b);
247 pci_write_config8(MEMCTRL, 0x6b, regs);
250 pci_write_config8(MEMCTRL, 0x6b, regs);
253 pci_write_config8(MEMCTRL, 0x6b, regs);
254 regs = pci_read_config8(MEMCTRL, 0x6f);
256 pci_write_config8(MEMCTRL, 0x6f, regs);
258 /**********************************************/
259 /* Set DRAM Timing Setting (DDR2 533) */
260 /**********************************************/
261 /* SPD 9 18 23 25 CAS Latency NB3DRAM_REG62[2:0] */
262 /* Read SPD byte 18 CAS Latency */
263 GET_SPD(dimm, spds, regs, SPD_CAS_LAT);
264 printk_debug("\nCAS Supported ");
265 if (spds & SPD_CAS_LAT_2)
267 if (spds & SPD_CAS_LAT_3)
269 if (spds & SPD_CAS_LAT_4)
271 if (spds & SPD_CAS_LAT_5)
273 if (spds & SPD_CAS_LAT_6)
276 /* We don't consider CAS = 6, because CX700 doesn't support it */
277 printk_debug("\n CAS:");
278 if (spds & SPD_CAS_LAT_5) {
279 printk_debug("Starting at CL5");
281 /* See whether we can improve it */
282 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_1);
283 if ((spds & SPD_CAS_LAT_4) && (tmp < 0x50)) {
284 printk_debug("\n... going to CL4");
287 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_2);
288 if ((spds & SPD_CAS_LAT_3) && (tmp < 0x50)) {
289 printk_debug("\n... going to CL3");
293 printk_debug("Starting at CL4");
295 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_1);
296 if ((spds & SPD_CAS_LAT_3) && (tmp < 0x50)) {
297 printk_debug("\n... going to CL3");
300 GET_SPD(dimm, tmp, regs, SPD_CAS_LAT_MIN_X_2);
301 if ((spds & SPD_CAS_LAT_2) && (tmp < 0x50)) {
302 printk_debug("\n... going to CL2");
306 regs = pci_read_config8(MEMCTRL, 0x62);
309 pci_write_config8(MEMCTRL, 0x62, regs);
311 /* SPD 27 Trp NB3DRAM_REG64[3:2] */
312 GET_SPD(dimm, spds, regs, SPD_TRP);
313 printk_debug("\nTrp %d", spds);
315 for (val = 2; val <= 5; val++) {
316 if (spds <= (val * t / 10)) {
322 regs = pci_read_config8(MEMCTRL, 0x64);
325 pci_write_config8(MEMCTRL, 0x64, regs);
327 /* SPD 29 Trcd NB3DRAM_REG64[7:6] */
328 GET_SPD(dimm, spds, regs, SPD_TRCD);
329 printk_debug("\nTrcd %d", spds);
331 for (val = 2; val <= 5; val++) {
332 if (spds <= (val * t / 10)) {
338 regs = pci_read_config8(MEMCTRL, 0x64);
341 pci_write_config8(MEMCTRL, 0x64, regs);
343 /* SPD 30 Tras NB3DRAM_REG62[7:4] */
344 GET_SPD(dimm, spds, regs, SPD_TRAS);
345 printk_debug("\nTras %d", spds);
346 for (val = 5; val <= 20; val++) {
347 if (spds <= (val * t / 10)) {
353 regs = pci_read_config8(MEMCTRL, 0x62);
356 pci_write_config8(MEMCTRL, 0x62, regs);
358 /* SPD 42 SPD 40 Trfc NB3DRAM_REG61[5:0] */
359 GET_SPD(dimm, spds, regs, SPD_TRFC);
360 printk_debug("\nTrfc %d", spds);
362 GET_SPD(dimm, spds, regs, SPD_EX_TRC_TRFC);
367 for (val = 8; val <= 71; val++) {
368 if (tmp <= (val * t / 10)) {
373 regs = pci_read_config8(MEMCTRL, 0x61);
376 pci_write_config8(MEMCTRL, 0x61, regs);
378 /* SPD 28 Trrd NB3DRAM_REG63[7:6] */
379 GET_SPD(dimm, spds, regs, SPD_TRRD);
380 for (val = 2; val <= 5; val++) {
381 if (spds <= (val * t / 10)) {
387 printk_debug("\nTrrd val = 0x%x", val);
388 regs = pci_read_config8(MEMCTRL, 0x63);
391 pci_write_config8(MEMCTRL, 0x63, regs);
393 /* SPD 36 Twr NB3DRAM_REG61[7:6] */
394 GET_SPD(dimm, spds, regs, SPD_TWR);
395 for (val = 2; val <= 5; val++) {
396 if (spds <= (val * t / 10)) {
402 printk_debug("\nTwr val = 0x%x", val);
404 regs = pci_read_config8(MEMCTRL, 0x61);
407 pci_write_config8(MEMCTRL, 0x61, regs);
409 /* SPD 37 Twtr NB3DRAM_REG63[1] */
410 GET_SPD(dimm, spds, regs, SPD_TWTR);
412 printk_debug("\nTwtr 0x%x", spds);
413 if (spds <= (t * 2 / 10))
418 printk_debug("\nTwtr val = 0x%x", val);
420 regs = pci_read_config8(MEMCTRL, 0x63);
423 pci_write_config8(MEMCTRL, 0x63, regs);
425 /* SPD 38 Trtp NB3DRAM_REG63[3] */
426 GET_SPD(dimm, spds, regs, SPD_TRTP);
428 printk_debug("\nTrtp 0x%x", spds);
429 if (spds <= (t * 2 / 10))
434 printk_debug("\nTrtp val = 0x%x", val);
436 regs = pci_read_config8(MEMCTRL, 0x63);
439 pci_write_config8(MEMCTRL, 0x63, regs);
441 /**********************************************/
442 /* Set DRAM DRDY Setting */
443 /**********************************************/
444 /* Write slowest value to register */
445 tmp = sizeof(Host_Reg_Val) / sizeof(Host_Reg_Val[0]);
446 for (val = 0; val < tmp; val += 2)
447 pci_write_config8(HOSTCTRL, Host_Reg_Val[val], Host_Reg_Val[val + 1]);
449 /* F2_RX51[7]=0, disable DRDY timing */
450 regs = pci_read_config8(HOSTCTRL, 0x51);
452 pci_write_config8(HOSTCTRL, 0x51, regs);
454 /**********************************************/
455 /* Set DRAM BurstLength */
456 /**********************************************/
457 regs = pci_read_config8(MEMCTRL, 0x6c);
458 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
459 if (pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_REG_BASE + (dimm << 1)))) {
460 spds = get_spd_data(ctrl, dimm, 16);
467 pci_write_config8(MEMCTRL, 0x6c, regs);
468 val = pci_read_config8(HOSTCTRL, 0x54);
472 pci_write_config8(HOSTCTRL, 0x54, val);
474 /**********************************************/
475 /* Set DRAM Driving Setting */
476 /**********************************************/
477 /* DRAM Timing ODT */
478 tmp = sizeof(Dram_Driving_ODT_CTRL) / sizeof(Dram_Driving_ODT_CTRL[0]);
479 for (val = 0; val < tmp; val += 2)
480 pci_write_config8(MEMCTRL, Dram_Driving_ODT_CTRL[val],
481 Dram_Driving_ODT_CTRL[val + 1]);
483 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
484 val = pci_read_config8(MEMCTRL, 0xd5);
494 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DIMM_NUM);
497 pci_write_config8(MEMCTRL, 0xd5, val);
499 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DIMM_NUM);
500 val = pci_read_config8(MEMCTRL, 0xd6);
504 pci_write_config8(MEMCTRL, 0xd6, val);
506 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_MAP);
507 tmp = sizeof(ODT_TBL) / sizeof(ODT_TBL[0]);
508 for (val = 0; val < tmp; val += 3) {
509 if (regs == ODT_TBL[val]) {
510 pci_write_config8(MEMCTRL, 0xd8, ODT_TBL[val + 1]);
511 /* Store DRAM & NB ODT setting in d0f4_Rxd8 */
512 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_NB_ODT, ODT_TBL[val + 2]);
517 pci_write_config8(MEMCTRL, 0xd9, 0x0a);
518 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
521 pci_write_config8(MEMCTRL, 0xe0, DQS_DQ_TBL[regs++]);
522 pci_write_config8(MEMCTRL, 0xe2, DQS_DQ_TBL[regs]);
525 pci_write_config8(MEMCTRL, 0xe4, 0x66);
527 /* DRAM Timing MAA */
529 for (dimm = 0; dimm < DIMM_SOCKETS; dimm++) {
530 if (pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_REG_BASE + (dimm << 1)))) {
531 spds = get_spd_data(ctrl, dimm, SPD_PRI_WIDTH);
534 (PCI_DEV(0, 0, 4), (SCRATCH_REG_BASE + (dimm << 1) + 1)))
539 printk_debug("\nchip #%d", val);
544 pci_write_config8(MEMCTRL, 0xe8, regs);
546 /* DRAM Timing MAB */
547 pci_write_config8(MEMCTRL, 0xe9, 0x0);
549 /* DRAM Timing DCLK VT8454C always 0x66 */
550 pci_write_config8(MEMCTRL, 0xe6, 0xaa);
552 /**********************************************/
553 /* Set DRAM Duty Control */
554 /**********************************************/
555 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
558 case 2: /* 1~2 rank */
562 case 4: /* 3~4 rank */
563 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
564 if (regs == DDRII_533)
571 for (t = 0; t < 4; t++) {
572 pci_write_config8(MEMCTRL, regs, Duty_Control_DDR2[val]);
577 /**********************************************/
578 /* Set DRAM Clock Control */
579 /**********************************************/
580 /* Write Data Phase */
581 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
582 regs = pci_read_config8(MEMCTRL, 0x75);
586 pci_write_config8(MEMCTRL, 0x74, 0x07);
591 pci_write_config8(MEMCTRL, 0x74, 0x05);
595 pci_write_config8(MEMCTRL, 0x75, regs);
596 pci_write_config8(MEMCTRL, 0x76, 0x80);
598 /* Clock Phase Control for FeedBack Mode */
599 regs = pci_read_config8(MEMCTRL, 0x90);
601 pci_write_config8(MEMCTRL, 0x90, regs);
603 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
606 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
617 regs = pci_read_config8(MEMCTRL, 0x91);
620 pci_write_config8(MEMCTRL, 0x91, regs);
622 for (t = 0; t < 3; t++) {
623 dimm = pci_read_config8(MEMCTRL, regs);
625 dimm |= ChA_Clk_Phase_DDR2_Table[val];
626 pci_write_config8(MEMCTRL, regs, dimm);
631 pci_write_config8(MEMCTRL, 0x97, 0x12);
632 pci_write_config8(MEMCTRL, 0x98, 0x33);
634 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_0);
635 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_2);
637 pci_write_config8(MEMCTRL, 0x9d, 0x00);
639 pci_write_config8(MEMCTRL, 0x9d, 0x0f);
641 tmp = sizeof(DQ_DQS_Table) / sizeof(DQ_DQS_Table[0]);
642 for (val = 0; val < tmp; val += 2)
643 pci_write_config8(MEMCTRL, DQ_DQS_Table[val], DQ_DQS_Table[val + 1]);
644 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
645 if (regs == DDRII_533)
646 pci_write_config8(MEMCTRL, 0x7b, 0xa0);
648 pci_write_config8(MEMCTRL, 0x7b, 0x10);
650 /***************************************************/
651 /* Set necessary register before DRAM initialize */
652 /***************************************************/
653 tmp = sizeof(Mem_Reg_Init) / sizeof(Mem_Reg_Init[0]);
654 for (val = 0; val < tmp; val += 3) {
655 regs = pci_read_config8(MEMCTRL, Mem_Reg_Init[val]);
656 regs &= Mem_Reg_Init[val + 1];
657 regs |= Mem_Reg_Init[val + 2];
658 pci_write_config8(MEMCTRL, Mem_Reg_Init[val], regs);
660 regs = pci_read_config8(HOSTCTRL, 0x51);
661 regs &= 0xbf; // Clear bit 6 Disable Read Around Write
662 pci_write_config8(HOSTCTRL, 0x51, regs);
664 regs = pci_read_config8(HOSTCTRL, 0x54);
666 val = pci_read_config8(HOSTCTRL, 0x57);
676 pci_write_config8(HOSTCTRL, 0x57, val);
678 regs = pci_read_config8(HOSTCTRL, 0x51);
680 pci_write_config8(HOSTCTRL, 0x51, regs);
682 regs = pci_read_config8(MEMCTRL, 0x90);
687 regs = pci_read_config8(MEMCTRL, 0x76);
690 pci_write_config8(MEMCTRL, 0x76, regs);
692 regs = pci_read_config8(MEMCTRL, 0x6f);
694 pci_write_config8(MEMCTRL, 0x6f, regs);
696 /***************************************************/
697 /* Find suitable DQS value for ChA and ChB */
698 /***************************************************/
699 // Set DQS output delay for Channel A
700 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_FREQ);
701 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_NUM);
717 for (t = 0; t < 2; t++)
718 pci_write_config8(MEMCTRL, (0x70 + t), DQSOChA_DDR2_Driving_Table[val + t]);
719 // Set DQS output delay for Channel B
720 pci_write_config8(MEMCTRL, 0x72, 0x0);
722 regs = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_0);
723 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_RANK_2);
725 pci_write_config8(MEMCTRL, 0x73, 0xfd);
727 pci_write_config8(MEMCTRL, 0x73, 0x01);
730 static void sdram_set_registers(const struct mem_controller *ctrl)
734 sdram_set_safe_values(ctrl);
737 static void step_20_21(const struct mem_controller *ctrl)
744 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_NB_ODT);
745 if (val & DDR2_ODT_150ohm)
750 /* Step 21. Normal operation */
751 print_spew("RAM Enable 5: Normal operation\n");
752 do_ram_command(ctrl, RAM_COMMAND_NORMAL);
756 static void step_2_19(const struct mem_controller *ctrl)
762 val = pci_read_config8(MEMCTRL, 0x69);
764 pci_write_config8(MEMCTRL, 0x69, val);
766 /* Step 3 Apply NOP. */
767 print_spew("RAM Enable 1: Apply NOP\n");
768 do_ram_command(ctrl, RAM_COMMAND_NOP);
773 print_spew("SEND: ");
780 /* 6. Precharge all. Wait tRP. */
781 print_spew("RAM Enable 2: Precharge all\n");
782 do_ram_command(ctrl, RAM_COMMAND_PRECHARGE);
785 print_spew("SEND: ");
789 /* Step 8. Mode register set. */
790 print_spew("RAM Enable 4: Mode register set\n");
791 do_ram_command(ctrl, RAM_COMMAND_MRS); //enable dll
794 print_spew("SEND: ");
796 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_NB_ODT);
797 if (val & DDR2_ODT_150ohm)
798 read32(0x102200); //DDR2_ODT_150ohm
804 print_spew("SEND: ");
808 /* Step 11. Precharge all. Wait tRP. */
809 print_spew("RAM Enable 2: Precharge all\n");
810 do_ram_command(ctrl, RAM_COMMAND_PRECHARGE);
813 print_spew("SEND: ");
817 /* Step 13. Perform 8 refresh cycles. Wait tRC each time. */
818 print_spew("RAM Enable 3: CBR\n");
819 do_ram_command(ctrl, RAM_COMMAND_CBR);
821 /* JEDEC says only twice, do 8 times for posterity */
822 // Step 16: Repeat Step 14 and 15 another 7 times
823 for (i = 0; i < 8; i++) {
832 /* Step 17. Mode register set. Wait 200us. */
833 print_spew("\nRAM Enable 4: Mode register set\n");
835 //safe value for now, BL=8, WR=4, CAS=4
836 do_ram_command(ctrl, RAM_COMMAND_MRS);
839 /* Use Single Chanel temporarily */
840 val = pci_read_config8(MEMCTRL, 0x6c);
841 if (val & 0x8) { /* Burst Length = 8 */
842 val = pci_read_config8(MEMCTRL, 0x62);
844 i = DDR2_MRS_table[4 + val];
846 val = pci_read_config8(MEMCTRL, 0x62);
848 i = DDR2_MRS_table[val];
852 val = pci_read_config8(MEMCTRL, 0x61);
854 i |= DDR2_Twr_table[val];
857 printk_debug("MRS = %08x\n", i);
862 val = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_NB_ODT);
863 if (val & DDR2_ODT_150ohm)
864 read32(0x103e00); //EMRS OCD Default
869 static void sdram_set_vr(const struct mem_controller *ctrl, u8 num)
872 val = 0x54 + (num >> 1);
873 reg = pci_read_config8(MEMCTRL, val);
874 reg &= (0xf << (4 * (num & 0x1)));
875 reg |= (((0x8 | num) << 4) >> (4 * (num & 0x1)));
876 pci_write_config8(MEMCTRL, val, reg);
878 static void sdram_ending_addr(const struct mem_controller *ctrl, u8 num)
881 /* Set Ending Address */
883 reg = pci_read_config8(MEMCTRL, val);
885 pci_write_config8(MEMCTRL, val, reg);
886 /* Set Beginning Address */
888 pci_write_config8(MEMCTRL, val, 0x0);
891 static void sdram_clear_vr_addr(const struct mem_controller *ctrl, u8 num)
894 val = 0x54 + (num >> 1);
895 reg = pci_read_config8(MEMCTRL, val);
896 reg = ~(0x80 >> (4 * (num & 0x1)));
897 pci_write_config8(MEMCTRL, val, reg);
899 reg = pci_read_config8(MEMCTRL, val);
901 pci_write_config8(MEMCTRL, val, reg);
903 pci_write_config8(MEMCTRL, val, 0x0);
906 /* Perform sizing DRAM by dynamic method */
907 static void sdram_calc_size(const struct mem_controller *ctrl, u8 num)
910 ba = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_FLAGS);
914 write8((1 << SDRAM1X_RA_12_8bk), 0x0c);
919 write8((1 << SDRAM1X_CA_09_8bk), 0x0c);
924 write8((1 << SDRAM1X_BA2_8bk), 0x02);
926 write8((1 << SDRAM1X_BA1_8bk), 0x01);
931 write8((1 << SDRAM1X_RA_14), 0x0e);
933 write8((1 << SDRAM1X_RA_13), 0x0d);
935 write8((1 << SDRAM1X_RA_12), 0x0c);
940 write8((1 << SDRAM1X_CA_12), 0x0b);
942 write8((1 << SDRAM1X_CA_11), 0x0a);
944 write8((1 << SDRAM1X_CA_09), 0x09);
949 write8((1 << SDRAM1X_BA1), 0x01);
953 if (ra < 10 || ra > 15)
955 if (ca < 8 || ca > 12)
957 if (ba < 1 || ba > 3)
960 /* Calculate MA type save to scratch register */
999 pci_write_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK0_MA_REG + num), reg);
1002 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_256M_BIT, 1);
1004 /* Calculate rank size save to scratch register */
1005 ra = ra + ca + ba + 3 - 26; /* 1 unit = 64M */
1007 pci_write_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK0_SIZE_REG + num), ra);
1010 static void sdram_enable(const struct mem_controller *ctrl)
1018 /* Init Present Bank */
1019 val = sizeof(Init_Rank_Reg_Table) / sizeof(Init_Rank_Reg_Table[0]);
1020 for (i = 0; i < val; i++)
1021 pci_write_config8(MEMCTRL, Init_Rank_Reg_Table[i], 0x0);
1023 /* Init other banks */
1024 for (i = 0; i < 4; i++) {
1025 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1027 sdram_set_vr(ctrl, i);
1028 sdram_ending_addr(ctrl, i);
1031 sdram_clear_vr_addr(ctrl, i);
1035 #ifdef MEM_WIDTH_32BIT_MODE
1036 /****************************************************************/
1037 /* Set Dram 32bit Mode */
1038 /****************************************************************/
1039 reg8 = pci_read_config8(MEMCTRL, 0x6c);
1041 pci_write_config(MEMCTRL, 0x6c, reg8);
1044 /****************************************************************/
1045 /* Find the DQSI Low/High bound and save it to Scratch register */
1046 /****************************************************************/
1047 for (dl = 0; dl < 0x3f; dl += 2) {
1049 reg8 |= 0x80; /* Set Manual Mode */
1050 pci_write_config8(MEMCTRL, 0x77, reg8);
1051 for (i = 0; i < 4; i++) {
1052 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1054 sdram_set_vr(ctrl, i);
1055 sdram_ending_addr(ctrl, i);
1056 write32(0, 0x55555555);
1057 write32(4, 0x55555555);
1059 if (read32(0) != 0x55555555)
1061 if (read32(4) != 0x55555555)
1063 write32(0, 0xaaaaaaaa);
1064 write32(4, 0xaaaaaaaa);
1066 if (read32(0) != 0xaaaaaaaa)
1068 if (read32(4) != 0xaaaaaaaa)
1070 sdram_clear_vr_addr(ctrl, i);
1076 sdram_clear_vr_addr(ctrl, i);
1078 printk_debug("\nDQSI Low %08x", dl);
1079 for (dh = dl; dh < 0x3f; dh += 2) {
1081 reg8 |= 0x80; /* Set Manual Mode */
1082 pci_write_config8(MEMCTRL, 0x77, reg8);
1083 for (i = 0; i < 4; i++) {
1084 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1086 sdram_set_vr(ctrl, i);
1087 sdram_ending_addr(ctrl, i);
1089 write32(0, 0x55555555);
1090 write32(4, 0x55555555);
1092 if (read32(0) != 0x55555555)
1094 if (read32(4) != 0x55555555)
1096 write32(0, 0xaaaaaaaa);
1097 write32(4, 0xaaaaaaaa);
1099 if (read32(0) != 0xaaaaaaaa)
1101 if (read32(4) != 0xaaaaaaaa)
1103 sdram_clear_vr_addr(ctrl, i);
1107 sdram_clear_vr_addr(ctrl, i);
1111 printk_debug("\nDQSI High %02x", dh);
1112 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_CHA_DQSI_LOW_REG, dl);
1113 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_CHA_DQSI_HIGH_REG, dh);
1114 reg8 = pci_read_config8(MEMCTRL, 0X90) & 0X7;
1115 val = DQSI_Rate_Table[reg8];
1120 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_ChA_DQSI_REG, val);
1123 pci_write_config8(MEMCTRL, 0x77, reg8);
1125 /****************************************************************/
1126 /* Find out the lowest Bank Interleave and Set Register */
1127 /****************************************************************/
1130 reg8 = pci_read_config8(MEMCTRL, 0x69);
1132 reg8 |= 0x80; //8 banks
1133 pci_write_config8(MEMCTRL, 0x69, reg8);
1136 for (i = 0; i < 4; i++) {
1137 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1139 reg8 = get_spd_data(ctrl, (i >> 1), 17);
1140 sdram_set_vr(ctrl, i);
1141 sdram_ending_addr(ctrl, i);
1145 write8((1 << SDRAM1X_BA1), 0x01);
1150 write8((1 << SDRAM1X_BA2_8bk), 0x02);
1152 write8((1 << SDRAM1X_BA1_8bk), 0x01);
1157 sdram_clear_vr_addr(ctrl, i);
1161 reg8 = pci_read_config8(MEMCTRL, 0x69);
1164 pci_write_config8(MEMCTRL, 0x69, reg8);
1166 /****************************************************************/
1167 /* DRAM Sizing and Fill MA type */
1168 /****************************************************************/
1169 for (i = 0; i < 4; i++) {
1170 val = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1172 reg8 = get_spd_data(ctrl, (i >> 1), 17);
1173 pci_write_config8(PCI_DEV(0, 0, 4), SCRATCH_FLAGS, reg8);
1175 /* Use MA Type 3 for DRAM sizing */
1176 reg8 = pci_read_config8(MEMCTRL, 0x50);
1179 pci_write_config8(MEMCTRL, 0x50, reg8);
1180 pci_write_config8(MEMCTRL, 0x51, reg8);
1182 /* Use MA Type 5 for DRAM sizing */
1183 reg8 = pci_read_config8(MEMCTRL, 0x50);
1186 pci_write_config8(MEMCTRL, 0x50, reg8);
1187 pci_write_config8(MEMCTRL, 0x51, reg8);
1188 reg8 = pci_read_config8(MEMCTRL, 0x53);
1191 pci_write_config8(MEMCTRL, 0x53, reg8);
1193 sdram_set_vr(ctrl, i);
1195 reg8 = pci_read_config8(MEMCTRL, val);
1196 /* max size 3G for new MA table */
1198 pci_write_config8(MEMCTRL, val, reg8);
1199 /* Set Beginning Address */
1201 pci_write_config8(MEMCTRL, val, 0x0);
1203 sdram_calc_size(ctrl, i);
1206 val = 0x54 + (i >> 1);
1207 reg8 = pci_read_config8(MEMCTRL, val);
1208 reg8 = ~(0x80 >> (4 * (i & 0x1)));
1209 pci_write_config8(MEMCTRL, val, reg8);
1211 reg8 = pci_read_config8(MEMCTRL, val);
1213 pci_write_config8(MEMCTRL, val, reg8);
1215 pci_write_config8(MEMCTRL, val, 0x0);
1220 reg8 = pci_read_config8(MEMCTRL, 0x50);
1222 pci_write_config8(MEMCTRL, 0x50, reg8);
1223 pci_write_config8(MEMCTRL, 0x51, reg8);
1224 reg8 = pci_read_config8(MEMCTRL, 0x6b);
1226 pci_write_config8(MEMCTRL, 0x6b, reg8);
1228 /****************************************************************/
1229 /* DRAM re-initialize for burst length */
1230 /****************************************************************/
1231 for (i = 0; i < 4; i++) {
1232 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1234 sdram_set_vr(ctrl, i);
1235 sdram_ending_addr(ctrl, i);
1238 sdram_clear_vr_addr(ctrl, i);
1242 /****************************************************************/
1243 /* Set the MA Type */
1244 /****************************************************************/
1245 reg8 = pci_read_config8(MEMCTRL, 0x50);
1247 pci_write_config8(MEMCTRL, 0x50, reg8);
1249 reg8 = pci_read_config8(MEMCTRL, 0x51);
1251 pci_write_config8(MEMCTRL, 0x51, reg8);
1253 reg8 = pci_read_config8(MEMCTRL, 0x6b);
1255 pci_write_config8(MEMCTRL, 0x6b, reg8);
1257 for (i = 0; i < 4; i += 2) {
1258 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1260 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK0_MA_REG + i));
1261 reg8 &= (MA_Bank + MA_Column);
1262 val = pci_read_config8(MEMCTRL, 0x50);
1269 pci_write_config8(MEMCTRL, 0x50, val);
1273 /****************************************************************/
1274 /* Set Start and Ending Address */
1275 /****************************************************************/
1276 dl = 0; /* Begin Address */
1277 dh = 0; /* Ending Address */
1278 for (i = 0; i < 4; i++) {
1279 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1281 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK0_SIZE_REG + i));
1285 pci_write_config8(MEMCTRL, (0x40 + i), dh);
1286 pci_write_config8(MEMCTRL, (0x48 + i), dl);
1291 // F7_Rx57 Ending address mirror register
1292 pci_write_config8(PCI_DEV(0, 0, 7), 0x57, dh);
1293 dev = pci_locate_device(PCI_ID(0x1106, 0x324e), 0);
1294 pci_write_config8(dev, 0x57, dh);
1296 pci_write_config8(MEMCTRL, 0x88, dh);
1297 pci_write_config8(MEMCTRL, 0x85, dh);
1298 // also program vlink mirror
1299 pci_write_config8(PCI_DEV(0, 0, 7), 0xe5, dh);
1301 /****************************************************************/
1302 /* Set Physical to Virtual Rank mapping */
1303 /****************************************************************/
1304 pci_write_config32(MEMCTRL, 0x54, 0x0);
1305 for (i = 0; i < 4; i++) {
1306 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1308 reg8 = pci_read_config8(MEMCTRL, (0x54 + (i >> 1)));
1309 if (i & 0x1) { /* Odd Rank */
1312 } else { /* Even Rank */
1315 reg8 |= ((0x8 | i) << 4);
1317 pci_write_config8(MEMCTRL, (0x54 + (i >> 1)), reg8);
1321 /****************************************************************/
1322 /* Set DRAM Refresh Counter */
1323 /****************************************************************/
1324 val = pci_read_config8(MEMCTRL, 0X90) & 0X7;
1326 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), SCRATCH_DRAM_256M_BIT);
1329 pci_write_config8(MEMCTRL, 0x6a, REFC_Table[val]);
1331 /****************************************************************/
1332 /* Chipset Performance UP and other setting after DRAM Sizing */
1333 /****************************************************************/
1334 /* Dram Registers */
1335 val = sizeof(Dram_Table) / sizeof(Dram_Table[0]);
1336 for (i = 0; i < val; i += 3) {
1337 reg8 = pci_read_config8(MEMCTRL, Dram_Table[i]);
1338 reg8 &= Dram_Table[i + 1];
1339 reg8 |= Dram_Table[i + 2];
1340 pci_write_config8(MEMCTRL, Dram_Table[i], reg8);
1343 /* Host Registers */
1344 val = sizeof(Host_Table) / sizeof(Host_Table[0]);
1345 for (i = 0; i < val; i += 3) {
1346 reg8 = pci_read_config8(HOSTCTRL, Host_Table[i]);
1347 reg8 &= Host_Table[i + 1];
1348 reg8 |= Host_Table[i + 2];
1349 pci_write_config8(HOSTCTRL, Host_Table[i], reg8);
1353 #ifdef SETUP_PM_REGISTERS
1354 val = sizeof(PM_Table) / sizeof(PM_Table[0]);
1355 for (i = 0; i < val; i += 3) {
1356 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), PM_Table[i]);
1357 reg8 &= PM_Table[i + 1];
1358 reg8 |= PM_Table[i + 2];
1359 pci_write_config8(PCI_DEV(0, 0, 4), PM_Table[i], reg8);
1362 pci_write_config8(HOSTCTRL, 0x5d, 0xb2);
1364 /****************************************************************/
1365 /* UMA registers for N-series projects */
1366 /****************************************************************/
1368 /* Manual setting frame buffer bank */
1369 for (i = 0; i < 4; i++) {
1370 reg8 = pci_read_config8(PCI_DEV(0, 0, 4), (SCRATCH_RANK_0 + i));
1374 pci_write_config8(MEMCTRL, 0xb0, val);
1375 reg8 = 0x40; // Frame buffer size 64M
1376 reg8 |= 0x80; // VGA Enable
1377 reg8 |= 0x0a; // A[31:28] = 1010b
1378 pci_write_config8(MEMCTRL, 0xa1, reg8);
1382 outl(0x80000180, 0xcf8);
1385 outl(0x80000188, 0xcf8);
1388 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0xa5);
1390 pci_write_config8(PCI_DEV(0, 0, 0), 0xa5, reg8);
1392 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0x91);
1394 pci_write_config8(PCI_DEV(0, 0, 0), 0x91, reg8);
1397 static const struct regmask {
1402 { 0x40, 0x00, 0x8b},
1403 { 0x41, 0x80, 0x43},
1404 { 0x42, 0x00, 0x62},
1405 { 0x43, 0x00, 0x44},
1406 { 0x44, 0x00, 0x34},
1409 { 0x53, 0xf0, 0x0f},
1410 { 0x60, 0x00, 0x03},
1411 { 0x65, 0x00, 0xd9},
1412 { 0x66, 0x00, 0x80},
1413 { 0x67, 0x00, 0x00},
1414 { 0x68, 0x00, 0x01},
1415 { 0x69, 0xe0, 0x03},
1416 { 0x6b, 0x00, 0x10},
1417 { 0x6c, 0xc1, 0x08},
1418 { 0x6e, 0x00, 0x89},
1419 { 0x6f, 0x00, 0x51},
1420 { 0x75, ~0x40, 0x40},
1421 { 0x76, 0x8f, 0x00},
1422 { 0x7b, 0x00, 0xa0},
1423 { 0x86, 0x01, 0x24},
1424 { 0x86, 0x04, 0x29},
1425 { 0x8c, 0x00, 0x00},
1426 { 0x8d, 0x00, 0x00},
1427 { 0x95, ~0x40, 0x00},
1428 { 0xa2, 0x00, 0x44},
1431 { 0x4d, 0x00, 0x24},
1432 { 0x4f, 0x00, 0x01},
1433 { 0xbc, 0x00, 0x21},
1434 { 0xbe, 0x00, 0x00},
1437 { 0x40, ~0x01, 0x01}, // enable timer/counter shadow registers
1438 { 0x67, ~0x03, 0x01},
1439 { 0x5b, ~0x01, 0x00},
1440 { 0x8d, ~0x02, 0x02},
1441 { 0x97, ~0x80, 0x00},
1442 { 0xd2, ~0x18, 0x00},
1443 { 0xe2, ~0x36, 0x06},
1444 { 0xe4, ~0x80, 0x00},
1445 { 0xe5, 0x00, 0x40},
1446 { 0xe6, 0x00, 0x20},
1447 { 0xe7, ~0xd0, 0xc0},
1448 { 0xec, ~0x08, 0x00}
1450 { 0x4e, ~0x80, 0x80},
1451 { 0x4f, ~(1 << 6), 1 << 6 }, /* PG_CX700: 14.1.1 enable P2P Bridge Header for External PCI Bus */
1452 { 0x74, ~0x00, 0x04}, /* PG_CX700: 14.1.2 APIC FSB directly up to snmic, not on pci */
1453 { 0x7c, ~0x00, 0x02}, /* PG_CX700: 14.1.1 APIC FSB directly up to snmic, not on pci */
1454 { 0xe6, 0x0, 0x04} // MSI post
1455 }, b0d19f0[] = { /* P2PE */
1456 { 0x42, ~0x08, 0x08}, // Disable HD Audio,
1457 { 0x40, ~0xc0, 0x80} // 14.1.3.1.1 of the PG: extended cfg mode for pcie. enable capability, but don't activate
1459 { 0x50, ~0x40, 0x88},
1460 { 0x51, 0x80, 0x7b},
1461 { 0x52, 0x90, 0x6f},
1462 { 0x53, 0x00, 0x88},
1463 { 0x54, 0xe4, 0x16},
1464 { 0x55, 0xf2, 0x04},
1465 { 0x56, 0x0f, 0x00},
1466 { 0x57, ~0x04, 0x00},
1467 { 0x5d, 0x00, 0xb2},
1468 { 0x5e, 0x00, 0x88},
1469 { 0x5f, 0x00, 0xc7},
1473 REGISTERPRESET(0, 0, 0, b0d0f0);
1474 REGISTERPRESET(0, 0, 2, b0d0f2);
1475 REGISTERPRESET(0, 0, 3, b0d0f3);
1476 REGISTERPRESET(0, 1, 0, b0d1f0);
1477 REGISTERPRESET(0, 17, 0, b0d17f0);
1478 REGISTERPRESET(0, 17, 7, b0d17f7);
1479 REGISTERPRESET(0, 19, 0, b0d19f0);