coreboot-v3/northbridge/amd/k8/raminit.c
<<
>>
Prefs
   1/*
   2 * This file is part of the coreboot project.
   3 *
   4 * Copyright (C) 2002 Linux Networx
   5 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
   6 * Copyright (C) 2004 YingHai Lu
   7 * Copyright (C) 2007 Ronald G. Minnich <rminnich@gmail.com>
   8 *
   9 * This program is free software; you can redistribute it and/or modify
  10 * it under the terms of the GNU General Public License as published by
  11 * the Free Software Foundation; version 2 of the License.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
  21 */
  22/*
  23        2004.11 yhlu add 4 rank DIMM support
  24        2004.12 yhlu add D0 support
  25        2005.02 yhlu add E0 memory hole support
  26        2005.10 yhlu make it support DDR2 only
  27*/
  28
  29#include <mainboard.h>
  30#include <console.h>
  31#include <string.h>
  32#include <mtrr.h>
  33#include <macros.h>
  34#include <spd_ddr2.h>
  35#include <cpu.h>
  36#include <msr.h>
  37#include <amd/k8/k8.h>
  38#include <amd/k8/sysconf.h>
  39#include <device/pci.h>
  40#include <pci_ops.h>
  41#include <mc146818rtc.h>
  42#include <lib.h>
  43
  44#include <spd_ddr2.h>
  45
  46#warning where to we define supported DIMM types
  47#define DIMM_SUPPORT 0x0104
  48
  49/* NOTE: F2 and later ONLY */
  50inline void print_raminit(const char *strval, u32 val)
  51{
  52        printk(BIOS_DEBUG, "%s%08x\n", strval, val);
  53}
  54
  55#if 0
  56#if defined(CONFIG_LB_MEM_TOPK) & ((CONFIG_LB_MEM_TOPK -1) != 0)
  57# error "CONFIG_LB_MEM_TOPK must be a power of 2"
  58#endif
  59#endif
  60
  61/* bit [10,8] are dev func, bit[1,0] are dev index */
  62u32 pci_read_config32_index(u32 dev, u32 index_reg, u32 index)
  63{
  64        u32 dword;
  65
  66        pci_conf1_write_config32(dev, index_reg, index);
  67
  68        dword = pci_conf1_read_config32(dev, index_reg+0x4);
  69
  70        return dword;
  71}
  72
  73void pci_write_config32_index(u32 dev, u32 index_reg, u32 index, u32 data)
  74{
  75
  76        pci_conf1_write_config32(dev, index_reg, index);
  77
  78        pci_conf1_write_config32(dev, index_reg + 0x4, data);
  79
  80}
  81
  82u32 pci_read_config32_index_wait(u32 dev, u32 index_reg, u32 index)
  83{
  84
  85        u32 dword;
  86
  87        index &= ~(1<<30);
  88        pci_conf1_write_config32(dev, index_reg, index);
  89
  90        do {
  91                dword = pci_conf1_read_config32(dev, index_reg);
  92        } while (!(dword & (1<<31)));
  93
  94        dword = pci_conf1_read_config32(dev, index_reg+0x4);
  95
  96        return dword;
  97}
  98
  99void pci_write_config32_index_wait(u32 dev, u32 index_reg, u32 index, u32 data)
 100{
 101
 102        u32 dword;
 103
 104        pci_conf1_write_config32(dev, index_reg + 0x4, data);
 105
 106        index |= (1<<30);
 107        pci_conf1_write_config32(dev, index_reg, index);
 108        do {
 109                dword = pci_conf1_read_config32(dev, index_reg);
 110        } while (!(dword & (1<<31)));
 111
 112}
 113
 114
 115
 116        // for PCM(0, 0x18, 2, 0x98) index, and PCM(0x, 0x18, 2, 0x9c) data
 117        /*
 118                index: 
 119                [29: 0] DctOffset (Dram Controller Offset)
 120                [30:30] DctAccessWrite (Dram Controller Read/Write Select)      
 121                        0 = read access
 122                        1 = write access
 123                [31:31] DctAccessDone (Dram Controller Access Done)
 124                        0 = Access in progress
 125                        1 = No access is progress
 126
 127                Data:
 128                [31: 0] DctOffsetData (Dram Controller Offset Data)
 129                
 130                Read:
 131                        - Write the register num to DctOffset with DctAccessWrite = 0
 132                        - poll the DctAccessDone until it = 1
 133                        - Read the data from DctOffsetData
 134                Write:
 135                        - Write the data to DctOffsetData
 136                        - Write register num to DctOffset with DctAccessWrite = 1
 137                        - poll the DctAccessDone untio it = 1
 138                
 139        */
 140
 141int controller_present(const struct mem_controller *ctrl)
 142{
 143        return pci_conf1_read_config32(ctrl->f0, 0) == 0x11001022;
 144}
 145
 146void sdram_set_registers(const struct mem_controller *ctrl, struct sys_info *sysinfo)
 147{
 148        static const struct rmap register_values[] = {
 149
 150        /* Careful set limit registers before base registers which contain the enables */
 151        /* DRAM Limit i Registers
 152         * F1:0x44 i = 0
 153         * F1:0x4C i = 1
 154         * F1:0x54 i = 2
 155         * F1:0x5C i = 3
 156         * F1:0x64 i = 4
 157         * F1:0x6C i = 5
 158         * F1:0x74 i = 6
 159         * F1:0x7C i = 7
 160         * [ 2: 0] Destination Node ID
 161         *         000 = Node 0
 162         *         001 = Node 1
 163         *         010 = Node 2
 164         *         011 = Node 3
 165         *         100 = Node 4
 166         *         101 = Node 5
 167         *         110 = Node 6
 168         *         111 = Node 7
 169         * [ 7: 3] Reserved
 170         * [10: 8] Interleave select
 171         *         specifies the values of A[14:12] to use with interleave enable.
 172         * [15:11] Reserved
 173         * [31:16] DRAM Limit Address i Bits 39-24
 174         *         This field defines the upper address bits of a 40 bit  address
 175         *         that define the end of the DRAM region.
 176         */
 177        PCM(0, 0x18, 1, 0x44, 0x0000f8f8, 0x00000000),
 178        PCM(0, 0x18, 1, 0x4C, 0x0000f8f8, 0x00000001),
 179        PCM(0, 0x18, 1, 0x54, 0x0000f8f8, 0x00000002),
 180        PCM(0, 0x18, 1, 0x5C, 0x0000f8f8, 0x00000003),
 181        PCM(0, 0x18, 1, 0x64, 0x0000f8f8, 0x00000004),
 182        PCM(0, 0x18, 1, 0x6C, 0x0000f8f8, 0x00000005),
 183        PCM(0, 0x18, 1, 0x74, 0x0000f8f8, 0x00000006),
 184        PCM(0, 0x18, 1, 0x7C, 0x0000f8f8, 0x00000007),
 185        /* DRAM Base i Registers
 186         * F1:0x40 i = 0
 187         * F1:0x48 i = 1
 188         * F1:0x50 i = 2
 189         * F1:0x58 i = 3
 190         * F1:0x60 i = 4
 191         * F1:0x68 i = 5
 192         * F1:0x70 i = 6
 193         * F1:0x78 i = 7
 194         * [ 0: 0] Read Enable
 195         *         0 = Reads Disabled
 196         *         1 = Reads Enabled
 197         * [ 1: 1] Write Enable
 198         *         0 = Writes Disabled
 199         *         1 = Writes Enabled
 200         * [ 7: 2] Reserved
 201         * [10: 8] Interleave Enable
 202         *         000 = No interleave
 203         *         001 = Interleave on A[12] (2 nodes)
 204         *         010 = reserved
 205         *         011 = Interleave on A[12] and A[14] (4 nodes)
 206         *         100 = reserved
 207         *         101 = reserved
 208         *         110 = reserved
 209         *         111 = Interleve on A[12] and A[13] and A[14] (8 nodes)
 210         * [15:11] Reserved
 211         * [13:16] DRAM Base Address i Bits 39-24
 212         *         This field defines the upper address bits of a 40-bit address
 213         *         that define the start of the DRAM region.
 214         */
 215        PCM(0, 0x18, 1, 0x40, 0x0000f8fc, 0x00000000),
 216        PCM(0, 0x18, 1, 0x48, 0x0000f8fc, 0x00000000),
 217        PCM(0, 0x18, 1, 0x50, 0x0000f8fc, 0x00000000),
 218        PCM(0, 0x18, 1, 0x58, 0x0000f8fc, 0x00000000),
 219        PCM(0, 0x18, 1, 0x60, 0x0000f8fc, 0x00000000),
 220        PCM(0, 0x18, 1, 0x68, 0x0000f8fc, 0x00000000),
 221        PCM(0, 0x18, 1, 0x70, 0x0000f8fc, 0x00000000),
 222        PCM(0, 0x18, 1, 0x78, 0x0000f8fc, 0x00000000),
 223
 224        /* DRAM CS Base Address i Registers
 225         * F2:0x40 i = 0
 226         * F2:0x44 i = 1
 227         * F2:0x48 i = 2
 228         * F2:0x4C i = 3
 229         * F2:0x50 i = 4
 230         * F2:0x54 i = 5
 231         * F2:0x58 i = 6
 232         * F2:0x5C i = 7
 233         * [ 0: 0] Chip-Select Bank Enable
 234         *         0 = Bank Disabled
 235         *         1 = Bank Enabled
 236         * [ 1: 1] Spare Rank
 237         * [ 2: 2] Memory Test Failed
 238         * [ 4: 3] Reserved
 239         * [13: 5] Base Address (21-13)
 240         *         An optimization used when all DIMM are the same size...
 241         * [18:14] Reserved
 242         * [28:19] Base Address (36-27)
 243         *         This field defines the top 11 addresses bit of a 40-bit
 244         *         address that define the memory address space.  These
 245         *         bits decode 32-MByte blocks of memory.
 246         * [31:29] Reserved
 247         */
 248        PCM(0, 0x18, 2, 0x40, 0xe007c018, 0x00000000),
 249        PCM(0, 0x18, 2, 0x44, 0xe007c018, 0x00000000),
 250        PCM(0, 0x18, 2, 0x48, 0xe007c018, 0x00000000),
 251        PCM(0, 0x18, 2, 0x4C, 0xe007c018, 0x00000000),
 252        PCM(0, 0x18, 2, 0x50, 0xe007c018, 0x00000000),
 253        PCM(0, 0x18, 2, 0x54, 0xe007c018, 0x00000000),
 254        PCM(0, 0x18, 2, 0x58, 0xe007c018, 0x00000000),
 255        PCM(0, 0x18, 2, 0x5C, 0xe007c018, 0x00000000),
 256        /* DRAM CS Mask Address i Registers
 257         * F2:0x60 i = 0,1
 258         * F2:0x64 i = 2,3
 259         * F2:0x68 i = 4,5
 260         * F2:0x6C i = 6,7
 261         * Select bits to exclude from comparison with the DRAM Base address register.
 262         * [ 4: 0] Reserved
 263         * [13: 5] Address Mask (21-13)
 264         *         Address to be excluded from the optimized case
 265         * [18:14] Reserved
 266         * [28:19] Address Mask (36-27)
 267         *         The bits with an address mask of 1 are excluded from address comparison
 268         * [31:29] Reserved
 269         * 
 270         */
 271        PCM(0, 0x18, 2, 0x60, 0xe007c01f, 0x00000000),
 272        PCM(0, 0x18, 2, 0x64, 0xe007c01f, 0x00000000),
 273        PCM(0, 0x18, 2, 0x68, 0xe007c01f, 0x00000000),
 274        PCM(0, 0x18, 2, 0x6C, 0xe007c01f, 0x00000000),
 275
 276        /* DRAM Control Register
 277         * F2:0x78
 278         * [ 3: 0] RdPtrInit ( Read Pointer Initial Value)
 279         *      0x03-0x00: reserved
 280         * [ 6: 4] RdPadRcvFifoDly (Read Delay from Pad Receive FIFO)
 281         *      000 = reserved
 282         *      001 = reserved
 283         *      010 = 1.5 Memory Clocks
 284         *      011 = 2 Memory Clocks
 285         *      100 = 2.5 Memory Clocks
 286         *      101 = 3 Memory Clocks
 287         *      110 = 3.5 Memory Clocks
 288         *      111 = Reseved
 289         * [15: 7] Reserved
 290         * [16:16] AltVidC3MemClkTriEn (AltVID Memory Clock Tristate Enable)
 291         *      Enables the DDR memory clocks to be tristated when alternate VID mode is enabled. This bit has no effect if the DisNbClkRamp bit (F3, 0x88) is set
 292         * [17:17] DllTempAdjTime (DLL Temperature Adjust Cycle Time)
 293         *      0 = 5 ms
 294         *      1 = 1 ms
 295         * [18:18] DqsRcvEnTrain (DQS Receiver Enable Training Mode)
 296         *      0 = Normal DQS Receiver enable operation
 297         *      1 = DQS receiver enable training mode
 298         * [31:19] reverved
 299        */
 300        PCM(0, 0x18, 2, 0x78, 0xfff80000, (6<<4)|(6<<0)),
 301
 302        /* DRAM Initialization Register
 303         * F2:0x7C
 304         * [15: 0] MrsAddress (Address for MRS/EMRS Commands)
 305         *      this field specifies the dsata driven on the DRAM address pins 15-0 for MRS and EMRS commands
 306         * [18:16] MrsBank (Bank Address for MRS/EMRS Commands)
 307         *      this files specifies the data driven on the DRAM bank pins for the MRS and EMRS commands
 308         * [23:19] reverved
 309         * [24:24] SendPchgAll (Send Precharge All Command)
 310         *      Setting this bit causes the DRAM controller to send a precharge all command. This bit is cleared by the hardware after the command completes
 311         * [25:25] SendAutoRefresh (Send Auto Refresh Command)
 312         *      Setting this bit causes the DRAM controller to send an auto refresh command. This bit is cleared by the hardware after the command completes
 313         * [26:26] SendMrsCmd (Send MRS/EMRS Command)
 314         *      Setting this bit causes the DRAM controller to send the MRS or EMRS command defined by the MrsAddress and MrsBank fields. This bit is cleared by the hardware adter the commmand completes
 315         * [27:27] DeassertMemRstX (De-assert Memory Reset)
 316         *      Setting this bit causes the DRAM controller to de-assert the memory reset pin. This bit cannot be used to assert the memory reset pin
 317         * [28:28] AssertCke (Assert CKE)
 318         *      setting this bit causes the DRAM controller to assert the CKE pins. This bit cannot be used to de-assert the CKE pins
 319         * [30:29] reverved
 320         * [31:31] EnDramInit (Enable DRAM Initialization)
 321         *      Setting this bit puts the DRAM controller in a BIOS controlled DRAM initialization mode. BIOS must clear this bit aster DRAM initialization is complete.
 322        */
 323//        PCM(0, 0x18, 2, 0x7C), 0x60f80000, 0, 
 324
 325
 326        /* DRAM Bank Address Mapping Register
 327         * F2:0x80
 328         * Specify the memory module size
 329         * [ 3: 0] CS1/0 
 330         * [ 7: 4] CS3/2
 331         * [11: 8] CS5/4
 332         * [15:12] CS7/6
 333         * [31:16]
 334              row    col   bank
 335          0:  13     9      2    :128M
 336          1:  13     10     2    :256M
 337          2:  14     10     2    :512M
 338          3:  13     11     2    :512M
 339          4:  13     10     3    :512M
 340          5:  14     10     3    :1G
 341          6:  14     11     2    :1G
 342          7:  15     10     3    :2G
 343          8:  14     11     3    :2G
 344          9:  15     11     3    :4G
 345         10:  16     10     3    :4G
 346         11:  16     11     3    :8G
 347        */
 348        PCM(0, 0x18, 2, 0x80, 0xffff0000, 0x00000000),
 349        /* DRAM Timing Low Register
 350         * F2:0x88
 351         * [ 2: 0] Tcl (Cas# Latency, Cas# to read-data-valid)
 352         *         000 = reserved
 353         *         001 = reserved
 354         *         010 = CL 3
 355         *         011 = CL 4
 356         *         100 = CL 5
 357         *         101 = CL 6
 358         *         110 = reserved
 359         *         111 = reserved
 360         * [ 3: 3] Reserved
 361         * [ 5: 4] Trcd (Ras#-active to Cas# read/write delay)
 362         *         00 = 3 clocks
 363         *         01 = 4 clocks
 364         *         10 = 5 clocks
 365         *         11 = 6 clocks
 366         * [ 7: 6] Reserved
 367         * [ 9: 8] Trp (Row Precharge Time, Precharge-to-Active or Auto-Refresh)
 368         *         00 = 3 clocks
 369         *         01 = 4 clocks
 370         *         10 = 5 clocks
 371         *         11 = 6 clocks
 372         * [10:10] Reserved
 373         * [11:11] Trtp (Read to Precharge Time, read Cas# to precharge time)
 374         *         0 = 2 clocks for Burst Length of 32 Bytes
 375         *             4 clocks for Burst Length of 64 Bytes
 376         *         1 = 3 clocks for Burst Length of 32 Bytes
 377         *             5 clocks for Burst Length of 64 Bytes
 378         * [15:12] Tras (Minimum Ras# Active Time)
 379         *         0000 = reserved
 380         *         0001 = reserved
 381         *         0010 = 5 bus clocks
 382         *         ...
 383         *         1111 = 18 bus clocks
 384         * [19:16] Trc (Row Cycle Time, Ras#-active to Ras#-active or auto refresh of the same bank)
 385         *         0000 = 11 bus clocks
 386         *         0010 = 12 bus clocks
 387         *         ...
 388         *         1110 = 25 bus clocks
 389         *         1111 = 26 bus clocks
 390         * [21:20] Twr (Write Recovery Time, From the last data to precharge, writes can go back-to-back) 
 391         *         00 = 3 bus clocks
 392         *         01 = 4 bus clocks
 393         *         10 = 5 bus clocks
 394         *         11 = 6 bus clocks
 395         * [23:22] Trrd (Active-to-active (Ras#-to-Ras#) Delay of different banks)
 396         *         00 = 2 bus clocks
 397         *         01 = 3 bus clocks
 398         *         10 = 4 bus clocks
 399         *         11 = 5 bus clocks
 400         * [31:24] MemClkDis ( Disable the MEMCLK outputs for DRAM channel A, BIOS should set it to reduce the power consumption)
 401         *         Bit          F(1207)         M2 Package      S1g1 Package
 402         *          0           N/A             MA1_CLK1        N/A
 403         *          1           N/A             MA0_CLK1        MA0_CLK1
 404         *          2           MA3_CLK         N/A             N/A
 405         *          3           MA2_CLK         N/A             N/A
 406         *          4           MA1_CLK         MA1_CLK0        N/A
 407         *          5           MA0_CLK         MA0_CLK0        MA0_CLK0
 408         *          6           N/A             MA1_CLK2        N/A
 409         *          7           N/A             MA0_CLK2        MA0_CLK2
 410         */
 411        PCM(0, 0x18, 2, 0x88, 0x000004c8, 0xff000002 /* 0x03623125 */ ),
 412        /* DRAM Timing High Register
 413         * F2:0x8C
 414         * [ 3: 0] Reserved
 415         * [ 6: 4] TrwtTO (Read-to-Write Turnaround for Data, DQS Contention)
 416         *         000 = 2 bus clocks
 417         *         001 = 3 bus clocks
 418         *         010 = 4 bus clocks
 419         *         011 = 5 bus clocks
 420         *         100 = 6 bus clocks
 421         *         101 = 7 bus clocks
 422         *         110 = 8 bus clocks
 423         *         111 = 9 bus clocks
 424         * [ 7: 7] Reserved
 425         * [ 9: 8] Twtr (Internal DRAM Write-to-Read Command Delay, minium write-to-read delay when both access the same chip select)
 426         *         00 = Reserved
 427         *         01 = 1 bus clocks
 428         *         10 = 2 bus clocks
 429         *         11 = 3 bus clocks
 430         * [11:10] Twrrd (Write to Read DIMM Termination Turnaround, minimum write-to-read delay when accessing two different DIMMs)
 431         *         00 = 0 bus clocks
 432         *         01 = 1 bus clocks
 433         *         10 = 2 bus clocks
 434         *         11 = 3 bus clocks
 435         * [13:12] Twrwr (Write to Write Timing)
 436         *         00 = 1 bus clocks ( 0 idle cycle on the bus)
 437         *         01 = 2 bus clocks ( 1 idle cycle on the bus)
 438         *         10 = 3 bus clocks ( 2 idle cycles on the bus)
 439         *         11 = Reserved
 440         * [15:14] Trdrd ( Read to Read Timing)
 441         *         00 = 2 bus clocks ( 1 idle cycle on the bus)
 442         *         01 = 3 bus clocks ( 2 idle cycles on the bus)
 443         *         10 = 4 bus clocks ( 3 idle cycles on the bus)
 444         *         11 = 5 bus clocks ( 4 idel cycles on the bus)
 445         * [17:16] Tref (Refresh Rate)
 446         *         00 = Undefined behavior
 447         *         01 = Reserved
 448         *         10 = Refresh interval of 7.8 microseconds
 449         *         11 = Refresh interval of 3.9 microseconds
 450         * [19:18] Reserved
 451         * [22:20] Trfc0 ( Auto-Refresh Row Cycle Time for the Logical DIMM0, based on DRAM density and speed)
 452         *         000 = 75 ns (all speeds, 256Mbit)
 453         *         001 = 105 ns (all speeds, 512Mbit)
 454         *         010 = 127.5 ns (all speeds, 1Gbit)
 455         *         011 = 195 ns (all speeds, 2Gbit)
 456         *         100 = 327.5 ns (all speeds, 4Gbit)
 457         *         101 = reserved
 458         *         110 = reserved
 459         *         111 = reserved
 460         * [25:23] Trfc1 ( Auto-Refresh Row Cycle Time for the Logical DIMM1, based on DRAM density and speed)
 461         * [28:26] Trfc2 ( Auto-Refresh Row Cycle Time for the Logical DIMM2, based on DRAM density and speed)
 462         * [31:29] Trfc3 ( Auto-Refresh Row Cycle Time for the Logical DIMM3, based on DRAM density and speed)
 463         */
 464        PCM(0, 0x18, 2, 0x8c, 0x000c008f, (2 << 16)|(1 << 8)),
 465        /* DRAM Config Low Register
 466         * F2:0x90
 467         * [ 0: 0] InitDram (Initialize DRAM)
 468         *         1 = write 1 cause DRAM controller to execute the DRAM initialization, when done it read to 0
 469         * [ 1: 1] ExitSelfRef ( Exit Self Refresh Command )
 470         *         1 = write 1 causes the DRAM controller to bring the DRAMs out fo self refresh mode
 471         * [ 3: 2] Reserved
 472         * [ 5: 4] DramTerm (DRAM Termination)
 473         *         00 = On die termination disabled
 474         *         01 = 75 ohms
 475         *         10 = 150 ohms
 476         *         11 = 50 ohms
 477         * [ 6: 6] Reserved
 478         * [ 7: 7] DramDrvWeak ( DRAM Drivers Weak Mode)
 479         *         0 = Normal drive strength mode.
 480         *         1 = Weak drive strength mode
 481         * [ 8: 8] ParEn (Parity Enable)
 482         *         1 = Enable address parity computation output, PAR, and enables the parity error input, ERR
 483         * [ 9: 9] SelfRefRateEn (Faster Self Refresh Rate Enable)
 484         *         1 = Enable high temperature ( two times normal ) self refresh rate
 485         * [10:10] BurstLength32 ( DRAM Burst Length Set for 32 Bytes)
 486         *         0 = 64-byte mode
 487         *         1 = 32-byte mode
 488         * [11:11] Width128 ( Width of DRAM interface) 
 489         *         0 = the controller DRAM interface is 64-bits wide
 490         *         1 = the controller DRAM interface is 128-bits wide
 491         * [12:12] X4Dimm (DIMM 0 is x4)
 492         * [13:13] X4Dimm (DIMM 1 is x4)
 493         * [14:14] X4Dimm (DIMM 2 is x4)
 494         * [15:15] X4Dimm (DIMM 3 is x4)
 495         *         0 = DIMM is not x4
 496         *         1 = x4 DIMM present
 497         * [16:16] UnBuffDimm ( Unbuffered DIMMs)
 498         *         0 = Buffered DIMMs
 499         *         1 = Unbuffered DIMMs
 500         * [18:17] Reserved
 501         * [19:19] DimmEccEn ( DIMM ECC Enable )
 502                   1 =  ECC checking is being enabled for all DIMMs on the DRAM controller ( Through F3 0x44[EccEn])
 503         * [31:20] Reserved
 504         */
 505        PCM(0, 0x18, 2, 0x90, 0xfff6004c, 0x00000010),
 506        /* DRAM Config High Register
 507         * F2:0x94
 508         * [ 0: 2] MemClkFreq ( Memory Clock Frequency)
 509         *         000 = 200MHz
 510         *         001 = 266MHz
 511         *         010 = 333MHz
 512         *         011 = reserved
 513         *         1xx = reserved
 514         * [ 3: 3] MemClkFreqVal (Memory Clock Freqency Valid)
 515         *         1 = BIOS need to set the bit when setting up MemClkFreq to the proper value  
 516         * [ 7: 4] MaxAsyncLat ( Maximum Asynchronous Latency)
 517         *         0000 = 0 ns
 518         *         ...
 519         *         1111 = 15 ns
 520         * [11: 8] Reserved
 521         * [12:12] RDqsEn ( Read DQS Enable) This bit is only be set if x8 registered DIMMs are present in the system
 522         *         0 = DM pins function as data mask pins
 523         *         1 = DM pins function as read DQS pins
 524         * [13:13] Reserved
 525         * [14:14] DisDramInterface ( Disable the DRAM interface ) When this bit is set, the DRAM controller is disabled, and interface in low power state
 526         *         0 = Enabled (default)
 527         *         1 = Disabled
 528         * [15:15] PowerDownEn ( Power Down Mode Enable ) 
 529         *         0 = Disabled (default)
 530         *         1 = Enabled
 531         * [16:16] PowerDown ( Power Down Mode )
 532         *         0 = Channel CKE Control
 533         *         1 = Chip Select CKE Control
 534         * [17:17] FourRankSODimm (Four Rank SO-DIMM) 
 535         *         1 = this bit is set by BIOS to indicate that a four rank SO-DIMM is present
 536         * [18:18] FourRankRDimm (Four Rank Registered DIMM)
 537         *         1 = this bit is set by BIOS to indicate that a four rank registered DIMM is present
 538         * [19:19] Reserved
 539         * [20:20] SlowAccessMode (Slow Access Mode (2T Mode))
 540         *         0 = DRAM address and control signals are driven for one MEMCLK cycle
 541         *         1 = One additional MEMCLK of setup time is provided on all DRAM address and control signals except CS, CKE, and ODT; i.e., these signals are drivern for two MEMCLK cycles rather than one
 542         * [21:21] Reserved
 543         * [22:22] BankSwizzleMode ( Bank Swizzle Mode), 
 544         *         0 = Disabled (default)
 545         *         1 = Enabled
 546         * [23:23] Reserved
 547         * [27:24] DcqBypassMax ( DRAM Controller Queue Bypass Maximum)
 548         *         0000 = No bypass; the oldest request is never bypassed
 549         *         0001 = The oldest request may be bypassed no more than 1 time
 550         *         ...
 551         *         1111 = The oldest request may be bypassed no more than 15 times
 552         * [31:28] FourActWindow ( Four Bank Activate Window) , not more than 4 banks in a 8 bank device are activated
 553         *         0000 = No tFAW window restriction 
 554         *         0001 = 8 MEMCLK cycles
 555         *         0010 = 9 MEMCLK cycles
 556         *         ...
 557         *         1101 = 20 MEMCLK cycles
 558         *         111x = reserved
 559         */
 560        PCM(0, 0x18, 2, 0x94, 0x00a82f00,0x00008000),
 561        /* DRAM Delay Line Register
 562         * F2:0xa0
 563         * [ 0: 0] MemClrStatus (Memory Clear Status) :    ---------Readonly 
 564         *         when set, this bit indicates that the memory clear function is complete. Only clear by reset. BIOS should not write or read the DRAM until this bit is set by hardware
 565         * [ 1: 1] DisableJitter ( Disable Jitter)
 566         *         When set the DDR compensation circuit will not change the values unless the change is more than one step from the current value 
 567         * [ 3: 2] RdWrQByp ( Read/Write Queue Bypass Count)
 568         *         00 = 2
 569         *         01 = 4
 570         *         10 = 8
 571         *         11 = 16
 572         * [ 4: 4] Mode64BitMux (Mismatched DIMM Support Enable)
 573         *         1 When bit enables support for mismatched DIMMs when using 128-bit DRAM interface, the Width128 no effect, only for M2 and s1g1
 574         * [ 5: 5] DCC_EN ( Dynamica Idle Cycle Counter Enable)
 575         *         When set to 1, indicates that each entry in the page tables dynamically adjusts the idle cycle limit based on page Conflict/Page Miss (PC/PM) traffic
 576         * [ 8: 6] ILD_lmt ( Idle Cycle Limit)
 577         *         000 = 0 cycles
 578         *         001 = 4 cycles
 579         *         010 = 8 cycles
 580         *         011 = 16 cycles
 581         *         100 = 32 cycles
 582         *         101 = 64 cycles
 583         *         110 = 128 cycles
 584         *         111 = 256 cycles 
 585         * [ 9: 9] DramEnabled ( DRAM Enabled)
 586         *         When Set, this bit indicates that the DRAM is enabled, this bit is set by hardware after DRAM initialization or on an exit from self refresh. The DRAM controller is intialized after the
 587         *         hardware-controlled initialization process ( initiated by the F2 0x90[DramInit]) completes or when the BIOS-controlled initialization process completes (F2 0x7c(EnDramInit] is 
 588         *         written from 1 to 0)
 589         * [23:10] Reserved
 590         * [31:24] MemClkDis ( Disable the MEMCLK outputs for DRAM channel B, BIOS should set it to reduce the power consumption)
 591         *         Bit          F(1207)         M2 Package      S1g1 Package
 592         *          0           N/A             MA1_CLK1        N/A
 593         *          1           N/A             MA0_CLK1        MA0_CLK1
 594         *          2           MA3_CLK         N/A             N/A
 595         *          3           MA2_CLK         N/A             N/A
 596         *          4           MA1_CLK         MA1_CLK0        N/A
 597         *          5           MA0_CLK         MA0_CLK0        MA0_CLK0
 598         *          6           N/A             MA1_CLK2        N/A
 599         *          7           N/A             MA0_CLK2        MA0_CLK2
 600         */
 601        PCM(0, 0x18, 2, 0xa0, 0x00fffc00, 0xff000000), 
 602
 603        /* DRAM Scrub Control Register
 604         * F3:0x58
 605         * [ 4: 0] DRAM Scrube Rate
 606         * [ 7: 5] reserved
 607         * [12: 8] L2 Scrub Rate
 608         * [15:13] reserved
 609         * [20:16] Dcache Scrub
 610         * [31:21] reserved
 611         *         Scrub Rates
 612         *         00000 = Do not scrub
 613         *         00001 =  40.00 ns
 614         *         00010 =  80.00 ns
 615         *         00011 = 160.00 ns
 616         *         00100 = 320.00 ns
 617         *         00101 = 640.00 ns
 618         *         00110 =   1.28 us
 619         *         00111 =   2.56 us
 620         *         01000 =   5.12 us
 621         *         01001 =  10.20 us
 622         *         01011 =  41.00 us
 623         *         01100 =  81.90 us
 624         *         01101 = 163.80 us
 625         *         01110 = 327.70 us
 626         *         01111 = 655.40 us
 627         *         10000 =   1.31 ms
 628         *         10001 =   2.62 ms
 629         *         10010 =   5.24 ms
 630         *         10011 =  10.49 ms
 631         *         10100 =  20.97 ms
 632         *         10101 =  42.00 ms
 633         *         10110 =  84.00 ms
 634         *         All Others = Reserved
 635         */
 636        PCM(0, 0x18, 3, 0x58, 0xffe0e0e0, 0x00000000),
 637        /* DRAM Scrub Address Low Register
 638         * F3:0x5C
 639         * [ 0: 0] DRAM Scrubber Redirect Enable
 640         *         0 = Do nothing
 641         *         1 = Scrubber Corrects errors found in normal operation
 642         * [ 5: 1] Reserved
 643         * [31: 6] DRAM Scrub Address 31-6
 644         */
 645        PCM(0, 0x18, 3, 0x5C, 0x0000003e, 0x00000000),
 646        /* DRAM Scrub Address High Register
 647         * F3:0x60
 648         * [ 7: 0] DRAM Scrubb Address 39-32
 649         * [31: 8] Reserved
 650         */
 651        PCM(0, 0x18, 3, 0x60, 0xffffff00, 0x00000000),
 652        };
 653        // for PCM(0, 0x18, 2, 0x98) index, and PCM(0x, 0x18, 2, 0x9c) data
 654        /*
 655                index: 
 656                [29: 0] DctOffset (Dram Controller Offset)
 657                [30:30] DctAccessWrite (Dram Controller Read/Write Select)      
 658                        0 = read access
 659                        1 = write access
 660                [31:31] DctAccessDone (Dram Controller Access Done)
 661                        0 = Access in progress
 662                        1 = No access is progress
 663
 664                Data:
 665                [31: 0] DctOffsetData (Dram Controller Offset Data)
 666                
 667                Read:
 668                        - Write the register num to DctOffset with DctAccessWrite = 0
 669                        - poll the DctAccessDone until it = 1
 670                        - Read the data from DctOffsetData
 671                Write:
 672                        - Write the data to DctOffsetData
 673                        - Write register num to DctOffset with DctAccessWrite = 1
 674                        - poll the DctAccessDone untio it = 1
 675                
 676        */
 677        int i;
 678        int max;
 679
 680        if (!controller_present(ctrl)) {
 681                sysinfo->ctrl_present[ctrl->node_id] = 0;
 682                return;
 683        }
 684        sysinfo->ctrl_present[ctrl->node_id] = 1;
 685        
 686        printk(BIOS_SPEW, "setting up CPU 0x%02x northbridge registers\n", ctrl->node_id);
 687        /* do this later when we're sure it actually works */
 688        //setup_resource_map_x_offset(register_values, ARRAY_SIZE(register_values));
 689        max = ARRAY_SIZE(register_values);
 690        for(i = 0; i < max; i++) {
 691                struct rmap const *rm = &register_values[i];
 692                u32 dev;
 693                unsigned where;
 694                unsigned long reg;
 695                dev = PCI_BDF(rm->pcm.bus, rm->pcm.dev, rm->pcm.fn);
 696                dev -= PCI_BDF(0, 0x18, 0);
 697                dev += ctrl->f0;
 698                //dev = (register_values[i] & ~0xff) - PCI_BDF(0, 0x18, 0) + ctrl->f0;
 699                where = rm->pcm.reg;
 700                reg = pci_conf1_read_config32(dev, where);
 701                reg &= rm->pcm.and;
 702                reg |= rm->pcm.or;
 703                pci_conf1_write_config32(dev, where, reg);
 704        }
 705
 706        printk(BIOS_SPEW, "done.\n");
 707}
 708
 709int is_dual_channel(const struct mem_controller *ctrl)
 710{
 711        u32 dcl;
 712        dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
 713        return dcl & DCL_Width128;
 714}
 715
 716/* the very existence of this function is puzzling. How did we get here otherwise? */
 717int is_opteron(const struct mem_controller *ctrl)
 718{
 719        /* Test to see if I am an Opteron.  
 720         * FIXME Testing dual channel capability is correct for now
 721         * but a better test is probably required.
 722         * m2 and s1g1 support dual channel too. but only support unbuffered dimm
 723         */
 724#warning "FIXME implement a better test for opterons"
 725        u32 nbcap;
 726        nbcap = pci_conf1_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
 727        return !!(nbcap & NBCAP_128Bit);
 728}
 729
 730int is_registered(const struct mem_controller *ctrl)
 731{
 732        /* Test to see if we are dealing with registered SDRAM.
 733         * If we are not registered we are unbuffered.
 734         * This function must be called after spd_handle_unbuffered_dimms.
 735         */
 736        u32 dcl;
 737        dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
 738        return !(dcl & DCL_UnBuffDimm);
 739}
 740
 741void spd_get_dimm_size(unsigned device, struct dimm_size *sz)
 742{
 743        /* Calculate the log base 2 size of a DIMM in bits */
 744        int value;
 745        sz->per_rank = 0;
 746        sz->rows = 0;
 747        sz->col = 0;
 748        sz->rank = 0;
 749
 750        value = spd_read_byte(device, SPD_ROW_NUM);     /* rows */
 751        if (value < 0) goto hw_err;
 752        if ((value & 0xff) == 0) goto val_err; // max is 16 ?
 753        sz->per_rank += value & 0xff;
 754        sz->rows = value & 0xff;
 755        printk(BIOS_SPEW, "%d rows\n", sz->rows);
 756
 757        value = spd_read_byte(device, SPD_COL_NUM);     /* columns */
 758        if (value < 0) goto hw_err;
 759        if ((value & 0xff) == 0) goto val_err;  //max is 11
 760        sz->per_rank += value & 0xff;
 761        sz->col = value & 0xff;
 762        printk(BIOS_SPEW, "%d pre_rank %d col\n", sz->per_rank, sz->col);
 763
 764        value = spd_read_byte(device, SPD_BANK_NUM);    /* banks */
 765        if (value < 0) goto hw_err;
 766        if ((value & 0xff) == 0) goto val_err;
 767        sz->bank = log2c(value & 0xff);  // convert 4 to 2, and 8 to 3
 768        printk(BIOS_SPEW, "%d SPD banks %d bank\n", value, sz->bank);
 769        sz->per_rank += sz->bank;
 770        printk(BIOS_SPEW, "sz->per_rank is now %d\n", sz->per_rank);
 771        /* Get the module data width and convert it to a power of two */
 772        value = spd_read_byte(device, SPD_DATA_WIDTH);  
 773        if (value < 0) goto hw_err;
 774        value &= 0xff;
 775        if ((value != 72) && (value != 64)) goto val_err;
 776        /* why log2f (floor) here? because 72 bits is really 64 bits + parity */
 777        sz->per_rank += log2f(value) - 3; //64 bit So another 3 lines
 778        printk(BIOS_SPEW, "value %d log2f(value) %d sz->per_rank now %d\n", value, log2f(value), sz->per_rank);
 779
 780        /* How many ranks? */
 781        value = spd_read_byte(device, SPD_MOD_ATTRIB_RANK);     /* number of physical banks */
 782        if (value < 0) goto hw_err;
 783//      value >>= SPD_MOD_ATTRIB_RANK_NUM_SHIFT;
 784        value &= SPD_MOD_ATTRIB_RANK_NUM_MASK;
 785        value += SPD_MOD_ATTRIB_RANK_NUM_BASE; // 0-->1, 1-->2, 3-->4
 786        printk(BIOS_SPEW, "# ranks %d\n", value);
 787        /*
 788          rank == 1 only one rank or say one side
 789          rank == 2 two side , and two ranks
 790          rank == 4 two side , and four ranks total
 791          Some one side two ranks, because of stacked
 792        */
 793        if ((value != 1) && (value != 2) && (value != 4 )) {
 794                goto val_err;
 795        }
 796        sz->rank = value;
 797
 798        /* verify if per_rank is equal byte 31 
 799          it has the DIMM size as a multiple of 128MB.
 800         */
 801        value = spd_read_byte(device, SPD_RANK_SIZE);
 802        if (value < 0) goto hw_err;
 803        value &= 0xff;
 804        printk(BIOS_SPEW, "spd rank size is %d\n", value);
 805        value = log2f(value);
 806        if(value <=4 ) value += 8; // add back to 1G to high
 807        value += (27-5); // make 128MB to the real lines
 808        printk(BIOS_SPEW, " computed value is %d\n", value);
 809        if( value != (sz->per_rank)) { 
 810                printk(BIOS_ERR, "Bad RANK Size -- value is 0x%x, and it should be 0x%x\n", value, sz->per_rank);
 811                printk(BIOS_ERR, "This error has been reduced to a warning for now\n");
 812//              goto val_err;
 813        }
 814
 815        goto out;
 816
 817 val_err:
 818        die("Bad SPD value\n");
 819        /* If a hardware error occurs report that I have no memory */
 820hw_err:
 821        sz->per_rank = 0;
 822        sz->rows = 0;
 823        sz->col = 0;
 824        sz->bank = 0;
 825        sz->rank = 0;
 826 out:
 827        return;
 828}
 829
 830void set_dimm_size(const struct mem_controller *ctrl, struct dimm_size *sz, unsigned index, int is_Width128)
 831{
 832        u32 base0, base1;
 833
 834
 835        /* For each base register.
 836         * Place the dimm size in 32 MB quantities in the bits 31 - 21.
 837         * The initialize dimm size is in bits.
 838         * Set the base enable bit0.
 839         */
 840        
 841        base0 = base1 = 0;
 842
 843        /* Make certain side1 of the dimm is at least 128MB */
 844        if (sz->per_rank >= 27) {
 845                base0 = (1 << ((sz->per_rank - 27 ) + 19)) | 1;
 846        }
 847        
 848        /* Make certain side2 of the dimm is at least 128MB */
 849        if (sz->rank > 1) { // 2 ranks or 4 ranks
 850                base1 = (1 << ((sz->per_rank - 27 ) + 19)) | 1;
 851        }
 852
 853        /* Double the size if we are using dual channel memory */
 854        if (is_Width128) {
 855                base0 = (base0 << 1) | (base0 & 1);
 856                base1 = (base1 << 1) | (base1 & 1);
 857        }
 858
 859        /* Clear the reserved bits */
 860        base0 &= ~0xe007fffe;
 861        base1 &= ~0xe007fffe;
 862
 863        /* Set the appropriate DIMM base address register */
 864        pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), base0);
 865        pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), base1);
 866#ifdef QRANK_DIMM_SUPPORT
 867        if(sz->rank == 4) {
 868                pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+4)<<2), base0);
 869                pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+5)<<2), base1);
 870        }
 871#endif
 872
 873        /* Enable the memory clocks for this DIMM by Clear the MemClkDis bit*/
 874        if (base0) {
 875                u32 dword;
 876                u32 ClkDis0;
 877#if CPU_SOCKET_TYPE == SOCKET_L1
 878                ClkDis0 = DTL_MemClkDis0;
 879#else 
 880        #if CPU_SOCKET_TYPE == SOCKET_AM2
 881                ClkDis0 = DTL_MemClkDis0_AM2;
 882        #endif
 883#endif 
 884
 885                dword = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_LOW); //Channel A
 886                dword &= ~(ClkDis0 >> index);
 887#ifdef QRANK_DIMM_SUPPORT
 888                if(sz->rank == 4) {
 889                        dword &= ~(ClkDis0 >> (index+2));
 890                }
 891#endif
 892                pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_LOW, dword);
 893        
 894                if (is_Width128) { //Channel B  
 895                        dword = pci_conf1_read_config32(ctrl->f2, DRAM_CTRL_MISC);
 896                        dword &= ~(ClkDis0 >> index);
 897#ifdef QRANK_DIMM_SUPPORT
 898                        if(sz->rank == 4) {
 899                                dword &= ~(ClkDis0 >> (index+2));
 900                        }
 901#endif
 902                        pci_conf1_write_config32(ctrl->f2, DRAM_CTRL_MISC, dword);
 903                }
 904
 905        }
 906}
 907
 908/*    row    col   bank  for 64 bit
 909  0:  13     9      2    :128M
 910  1:  13     10     2    :256M
 911  2:  14     10     2    :512M
 912  3:  13     11     2    :512M
 913  4:  13     10     3    :512M
 914  5:  14     10     3    :1G
 915  6:  14     11     2    :1G
 916  7:  15     10     3    :2G
 917  8:  14     11     3    :2G
 918  9:  15     11     3    :4G
 919 10:  16     10     3    :4G
 920 11:  16     11     3    :8G
 921*/
 922
 923void set_dimm_cs_map(const struct mem_controller *ctrl, struct dimm_size *sz, unsigned index)
 924{
 925        static const u8 cs_map_aaa[24] = {
 926                /* (bank=2, row=13, col=9)(3, 16, 11) ---> (0, 0, 0) (1, 3, 2) */
 927        //Bank2
 928                0, 1, 3,
 929                0, 2, 6,
 930                0, 0, 0,
 931                0, 0, 0,
 932        //Bank3
 933                0, 4, 0,
 934                0, 5, 8,
 935                0, 7, 9,
 936                0,10,11,
 937        };
 938
 939        u32 map;
 940
 941        map = pci_conf1_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
 942        map &= ~(0xf << (index * 4));
 943#ifdef QRANK_DIMM_SUPPORT
 944        if(sz->rank == 4) {
 945                map &= ~(0xf << ( (index + 2) * 4));
 946        }
 947#endif
 948
 949        /* Make certain side1 of the dimm is at least 128MB */
 950        if (sz->per_rank >= 27) {
 951                unsigned temp_map;
 952                temp_map = cs_map_aaa[(sz->bank-2)*3*4 + (sz->rows - 13)*3 + (sz->col - 9) ];
 953                map |= temp_map << (index*4);
 954#ifdef QRANK_DIMM_SUPPORT
 955                if(sz->rank == 4) {
 956                       map |=  temp_map << ( (index + 2) * 4);
 957                }
 958#endif
 959        }
 960
 961        pci_conf1_write_config32(ctrl->f2, DRAM_BANK_ADDR_MAP, map);
 962
 963}
 964
 965long spd_set_ram_size(const struct mem_controller *ctrl, long dimm_mask, struct mem_info *meminfo)
 966{
 967        int i;
 968        
 969        for(i = 0; i < DIMM_SOCKETS; i++) {
 970                struct dimm_size *sz = &(meminfo->sz[i]);
 971                if (!(dimm_mask & (1 << i))) {
 972                        continue;
 973                }
 974                spd_get_dimm_size(ctrl->channel0[i], sz);
 975                if (sz->per_rank == 0) {
 976                        return -1; /* Report SPD error */
 977                }
 978                set_dimm_size(ctrl, sz, i, meminfo->is_Width128);
 979                set_dimm_cs_map (ctrl, sz, i);
 980        }
 981        return dimm_mask;
 982}
 983
 984void route_dram_accesses(const struct mem_controller *ctrl,
 985        unsigned long base_k, unsigned long limit_k)
 986{
 987        /* Route the addresses to the controller node */
 988        unsigned node_id;
 989        unsigned limit;
 990        unsigned base;
 991        unsigned index;
 992        unsigned limit_reg, base_reg;
 993        u32 device;
 994
 995        node_id = ctrl->node_id;
 996        index = (node_id << 3);
 997        limit = (limit_k << 2);
 998        limit &= 0xffff0000;
 999        limit -= 0x00010000;
1000        limit |= ( 0 << 8) | (node_id << 0);
1001        base = (base_k << 2);
1002        base &= 0xffff0000;
1003        base |= (0 << 8) | (1<<1) | (1<<0);
1004
1005        limit_reg = 0x44 + index;
1006        base_reg = 0x40 + index;
1007        for(device = PCI_BDF(0, 0x18, 1); device <= PCI_BDF(0, 0x1f, 1); device += PCI_BDF(0, 1, 0)) {
1008                pci_conf1_write_config32(device, limit_reg, limit);
1009                pci_conf1_write_config32(device, base_reg, base);
1010        }
1011}
1012
1013void set_top_mem(unsigned tom_k, unsigned hole_startk)
1014{
1015        /* Error if I don't have memory */
1016        if (!tom_k) {
1017                die("No memory?");
1018        }
1019
1020        /* Report the amount of memory. */
1021        printk(BIOS_DEBUG, "RAM: 0x%x KB\n", tom_k);
1022
1023        struct msr msr;
1024        if(tom_k > (4*1024*1024)) {
1025                /* Now set top of memory */
1026                msr.lo = (tom_k & 0x003fffff) << 10;
1027                msr.hi = (tom_k & 0xffc00000) >> 22;
1028                wrmsr(TOP_MEM2, msr);
1029        }
1030
1031        /* Leave a 64M hole between TOP_MEM and TOP_MEM2
1032         * so I can see my rom chip and other I/O devices.
1033         */
1034        if (tom_k >= 0x003f0000) {
1035#if CONFIG_HW_MEM_HOLE_SIZEK != 0
1036                if(hole_startk != 0) {
1037                        tom_k = hole_startk;
1038                } else
1039#endif
1040                tom_k = 0x3f0000;
1041        }
1042        msr.lo = (tom_k & 0x003fffff) << 10;
1043        msr.hi = (tom_k & 0xffc00000) >> 22;
1044        wrmsr(TOP_MEM, msr);
1045}
1046
1047unsigned long interleave_chip_selects(const struct mem_controller *ctrl, int is_Width128)
1048{
1049        /* 35 - 27 */
1050
1051        static const u8 csbase_low_f0_shift[] = {
1052        /* 128MB */       (14 - (13-5)),
1053        /* 256MB */       (15 - (13-5)),
1054        /* 512MB */       (15 - (13-5)),
1055        /* 512MB */       (16 - (13-5)),
1056        /* 512MB */       (16 - (13-5)),
1057        /* 1GB   */       (16 - (13-5)),
1058        /* 1GB   */       (16 - (13-5)),
1059        /* 2GB   */       (16 - (13-5)),
1060        /* 2GB   */       (17 - (13-5)),
1061        /* 4GB   */       (17 - (13-5)),
1062        /* 4GB   */       (16 - (13-5)),
1063        /* 8GB   */       (17 - (13-5)),
1064        };
1065
1066        /* cs_base_high is not changed */
1067
1068        u32 csbase_inc;
1069        int chip_selects, index;
1070        int bits;
1071        unsigned common_size;
1072        unsigned common_cs_mode;
1073        u32 csbase, csmask;
1074
1075        /* See if all of the memory chip selects are the same size
1076         * and if so count them.
1077         */
1078        chip_selects = 0;
1079        common_size = 0;
1080        common_cs_mode = 0xff;
1081        for(index = 0; index < 8; index++) {
1082                unsigned size;
1083                unsigned cs_mode;
1084                u32 value;
1085                
1086                value = pci_conf1_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1087                
1088                /* Is it enabled? */
1089                if (!(value & 1)) {
1090                        continue;
1091                }
1092                chip_selects++;
1093                size = (value >> 19) & 0x3ff;
1094                if (common_size == 0) {
1095                        common_size = size;
1096                }
1097                /* The size differed fail */
1098                if (common_size != size) {
1099                        return 0;
1100                }
1101
1102                value = pci_conf1_read_config32(ctrl->f2, DRAM_BANK_ADDR_MAP);
1103                cs_mode =( value >> ((index>>1)*4)) & 0xf;
1104                if(common_cs_mode == 0xff) {
1105                        common_cs_mode = cs_mode;
1106                }
1107                /* The cs_mode differed fail */
1108                if(common_cs_mode != cs_mode) {
1109                        return 0;
1110                }
1111        }
1112
1113        /* Chip selects can only be interleaved when there is
1114         * more than one and their is a power of two of them.
1115         */
1116        bits = log2c(chip_selects);
1117        if (((1 << bits) != chip_selects) || (bits < 1) || (bits > 3)) { //chip_selects max = 8
1118                return 0;
1119        }
1120
1121        /* Find the bits of csbase that we need to interleave on */
1122        csbase_inc = 1 << (csbase_low_f0_shift[common_cs_mode]);
1123        if(is_Width128) {
1124                csbase_inc <<=1;
1125        }   
1126        
1127
1128        /* Compute the initial values for csbase and csbask. 
1129         * In csbase just set the enable bit and the base to zero.
1130         * In csmask set the mask bits for the size and page level interleave.
1131         */
1132        csbase = 0 | 1;
1133        csmask = (((common_size  << bits) - 1) << 19);
1134        csmask |= 0x3fe0 & ~((csbase_inc << bits) - csbase_inc);
1135        for(index = 0; index < 8; index++) {
1136                u32 value;
1137
1138                value = pci_conf1_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1139                /* Is it enabled? */
1140                if (!(value & 1)) {
1141                        continue;
1142                }
1143                pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (index << 2), csbase);
1144                if((index & 1) == 0) {  //only have 4 CSMASK
1145                        pci_conf1_write_config32(ctrl->f2, DRAM_CSMASK + ((index>>1) << 2), csmask);
1146                }
1147                csbase += csbase_inc;
1148        }
1149        
1150        printk(BIOS_DEBUG, "Interleaved\n");
1151
1152        /* Return the memory size in K */ 
1153        return common_size << ((27-10) + bits);
1154}
1155unsigned long order_chip_selects(const struct mem_controller *ctrl)
1156{
1157        unsigned long tom;
1158
1159        /* Remember which registers we have used in the high 8 bits of tom */
1160        tom = 0;
1161        for(;;) {
1162                /* Find the largest remaining candidate */
1163                unsigned index, candidate;
1164                u32 csbase, csmask;
1165                unsigned size;
1166                csbase = 0;
1167                candidate = 0;
1168                for(index = 0; index < 8; index++) {
1169                        u32 value;
1170                        value = pci_conf1_read_config32(ctrl->f2, DRAM_CSBASE + (index << 2));
1171
1172                        /* Is it enabled? */
1173                        if (!(value & 1)) {
1174                                continue;
1175                        }
1176                        
1177                        /* Is it greater? */
1178                        if (value <= csbase) {
1179                                continue;
1180                        }
1181                        
1182                        /* Has it already been selected */
1183                        if (tom & (1 << (index + 24))) {
1184                                continue;
1185                        }
1186                        /* I have a new candidate */
1187                        csbase = value;
1188                        candidate = index;
1189                }
1190                /* See if I have found a new candidate */
1191                if (csbase == 0) {
1192                        break;
1193                }
1194
1195                /* Remember the dimm size */
1196                size = csbase >> 19;
1197
1198                /* Remember I have used this register */
1199                tom |= (1 << (candidate + 24));
1200
1201                /* Recompute the cs base register value */
1202                csbase = (tom << 19) | 1;
1203
1204                /* Increment the top of memory */
1205                tom += size;
1206
1207                /* Compute the memory mask */
1208                csmask = ((size -1) << 19);
1209                csmask |= 0x3fe0;               /* For now don't optimize */
1210
1211                /* Write the new base register */
1212                pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (candidate << 2), csbase);
1213                /* Write the new mask register */
1214                if((candidate & 1) == 0) {  //only have 4 CSMASK
1215                        pci_conf1_write_config32(ctrl->f2, DRAM_CSMASK + ((candidate>>1) << 2), csmask);
1216                }
1217                
1218        }
1219        /* Return the memory size in K */ 
1220        return (tom & ~0xff000000) << (27-10);
1221}
1222
1223unsigned long memory_end_k(const struct mem_controller *ctrl, int max_node_id)
1224{
1225        unsigned node_id;
1226        unsigned end_k;
1227        /* Find the last memory address used */
1228        end_k = 0;
1229        for(node_id = 0; node_id < max_node_id; node_id++) {
1230                u32 limit, base;
1231                unsigned index;
1232                index = node_id << 3;
1233                base = pci_conf1_read_config32(ctrl->f1, 0x40 + index);
1234                /* Only look at the limit if the base is enabled */
1235                if ((base & 3) == 3) {
1236                        limit = pci_conf1_read_config32(ctrl->f1, 0x44 + index);
1237                        end_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
1238                }
1239        }
1240        return end_k;
1241}
1242
1243void order_dimms(const struct mem_controller *ctrl, struct mem_info *meminfo)
1244{
1245        unsigned long tom_k, base_k;
1246        int interleave = 1;
1247
1248        if (get_option(&interleave, "interleave_chip_selects")) {
1249                tom_k = interleave_chip_selects(ctrl, meminfo->is_Width128);
1250        } else {
1251                printk(BIOS_DEBUG, "Interleaving disabled\n");
1252                tom_k = 0;
1253        }
1254        if (!tom_k) {
1255                tom_k = order_chip_selects(ctrl);
1256        }
1257        /* Compute the memory base address */
1258        base_k = memory_end_k(ctrl, ctrl->node_id);
1259        tom_k += base_k;
1260        route_dram_accesses(ctrl, base_k, tom_k);
1261        set_top_mem(tom_k, 0);
1262}
1263
1264long disable_dimm(const struct mem_controller *ctrl, unsigned index, long dimm_mask, struct mem_info *meminfo)
1265{
1266        printk(BIOS_DEBUG, "disabling dimm 0x%x \n", index); 
1267        pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+0)<<2), 0);
1268        pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+1)<<2), 0);
1269#ifdef QRANK_DIMM_SUPPORT
1270        if(meminfo->sz[index].rank == 4) {
1271                pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+4)<<2), 0);
1272                pci_conf1_write_config32(ctrl->f2, DRAM_CSBASE + (((index << 1)+5)<<2), 0);
1273        }
1274#endif
1275
1276        dimm_mask &= ~(1 << index);
1277        return dimm_mask;
1278}
1279
1280long spd_handle_unbuffered_dimms(const struct mem_controller *ctrl, long dimm_mask, struct mem_info *meminfo)
1281{
1282        int i;
1283        u32 registered;
1284        u32 dcl;
1285        registered = 0;
1286        for(i = 0; (i < DIMM_SOCKETS); i++) {
1287                int value;
1288                if (!(dimm_mask & (1 << i))) {
1289                        continue;
1290                }
1291                value = spd_read_byte(ctrl->channel0[i], SPD_DIMM_TYPE);
1292                if (value < 0) {
1293                        return -1;
1294                }
1295                /* Registered dimm ? */
1296                value &= 0x3f;
1297                if ((value == SPD_DIMM_TYPE_RDIMM) || (value == SPD_DIMM_TYPE_mRDIMM)) {
1298                        //check SPD_MOD_ATTRIB to verify it is SPD_MOD_ATTRIB_REGADC (0x11)?
1299                        registered |= (1<<i);
1300                } 
1301        }
1302
1303        if (is_opteron(ctrl)) {
1304#if 0
1305                if ( registered != (dimm_mask & ((1<<DIMM_SOCKETS)-1)) ) {
1306                        dimm_mask &= (registered | (registered << DIMM_SOCKETS) ); //disable unbuffed dimm
1307        //              die("Mixed buffered and registered dimms not supported");
1308                }
1309                //By yhlu for debug M2, s1g1 can do dual channel, but it use unbuffer DIMM
1310                if (!registered) {
1311                        die("Unbuffered Dimms not supported on Opteron");
1312                }
1313#endif
1314        }
1315
1316
1317        dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1318        dcl &= ~DCL_UnBuffDimm;
1319        meminfo->is_registered = 1;
1320        if (!registered) {
1321                dcl |= DCL_UnBuffDimm;
1322                meminfo->is_registered = 0;
1323        }
1324        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1325        
1326#if 1
1327        if (meminfo->is_registered) {
1328                printk(BIOS_DEBUG, "Registered\n");
1329        } else {
1330                printk(BIOS_DEBUG, "Unbuffered\n");
1331        }
1332#endif
1333        return dimm_mask;
1334}
1335
1336unsigned int spd_detect_dimms(const struct mem_controller *ctrl)
1337{
1338        unsigned dimm_mask;
1339        int i;
1340        dimm_mask = 0;
1341        for(i = 0; i < DIMM_SOCKETS; i++) {
1342                int byte;
1343                unsigned device;
1344                device = ctrl->channel0[i];
1345                if (device) {
1346                        byte = spd_read_byte(ctrl->channel0[i], SPD_MEM_TYPE);  /* Type */
1347                        if (byte == SPD_MEM_TYPE_SDRAM_DDR2) {
1348                                dimm_mask |= (1 << i);
1349                        }
1350                }
1351                device = ctrl->channel1[i];
1352                if (device) {
1353                        byte = spd_read_byte(ctrl->channel1[i], SPD_MEM_TYPE);
1354                        if (byte == SPD_MEM_TYPE_SDRAM_DDR2) {
1355                                dimm_mask |= (1 << (i + DIMM_SOCKETS));
1356                        }
1357                }
1358        }
1359        return dimm_mask;
1360}
1361
1362long spd_enable_2channels(const struct mem_controller *ctrl, long dimm_mask, struct mem_info *meminfo)
1363{
1364        int i;
1365        u32 nbcap;
1366        /* SPD addresses to verify are identical */
1367        static const u8 addresses[] = {
1368                2,      /* Type should be DDR2 SDRAM */
1369                3,      /* *Row addresses */
1370                4,      /* *Column addresses */
1371                5,      /* *Number of DIMM Ranks */
1372                6,      /* *Module Data Width*/
1373                9,      /* *Cycle time at highest CAS Latency CL=X */
1374                11,     /* *DIMM Conf Type */
1375                13,     /* *Pri SDRAM Width */
1376                17,     /* *Logical Banks */
1377                18,     /* *Supported CAS Latencies */
1378                20,     /* *DIMM Type Info */
1379                21,     /* *SDRAM Module Attributes */
1380                23,     /* *Cycle time at CAS Latnecy (CLX - 1) */
1381                26,     /* *Cycle time at CAS Latnecy (CLX - 2) */
1382                27,     /* *tRP Row precharge time */
1383                28,     /* *Minimum Row Active to Row Active Delay (tRRD) */
1384                29,     /* *tRCD RAS to CAS */
1385                30,     /* *tRAS Activate to Precharge */
1386                36,     /* *Write recovery time (tWR) */
1387                37,     /* *Internal write to read command delay (tRDP) */
1388                38,     /* *Internal read to precharge commanfd delay (tRTP) */
1389                41,     /* *Extension of Byte 41 tRC and Byte 42 tRFC */
1390                41,     /* *Minimum Active to Active/Auto Refresh Time(Trc) */
1391                42,     /* *Minimum Auto Refresh Command Time(Trfc) */
1392        };
1393        /* If the dimms are not in pairs do not do dual channels */
1394        if ((dimm_mask & ((1 << DIMM_SOCKETS) - 1)) !=
1395                ((dimm_mask >> DIMM_SOCKETS) & ((1 << DIMM_SOCKETS) - 1))) { 
1396                goto single_channel;
1397        }
1398        /* If the cpu is not capable of doing dual channels don't do dual channels */
1399        nbcap = pci_conf1_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1400        if (!(nbcap & NBCAP_128Bit)) {
1401                goto single_channel;
1402        }
1403        for(i = 0; (i < 4) && (ctrl->channel0[i]); i++) {
1404                unsigned device0, device1;
1405                int value0, value1;
1406                int j;
1407                /* If I don't have a dimm skip this one */
1408                if (!(dimm_mask & (1 << i))) {
1409                        continue;
1410                }
1411                device0 = ctrl->channel0[i];
1412                device1 = ctrl->channel1[i];
1413                for(j = 0; j < ARRAY_SIZE(addresses); j++) {
1414                        unsigned addr;
1415                        addr = addresses[j];
1416                        value0 = spd_read_byte(device0, addr);
1417                        if (value0 < 0) {
1418                                return -1;
1419                        }
1420                        value1 = spd_read_byte(device1, addr);
1421                        if (value1 < 0) {
1422                                return -1;
1423                        }
1424                        if (value0 != value1) {
1425                                goto single_channel;
1426                        }
1427                }
1428        }
1429        printk(BIOS_SPEW, "Enabling dual channel memory\n");
1430        u32 dcl;
1431        dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1432        dcl &= ~DCL_BurstLength32;  
1433        /*      32byte mode may be preferred in platforms that include graphics controllers
1434         *  that generate a lot of 32-bytes system memory accesses
1435         * 32byte mode is not supported when the DRAM interface is 128 bits wide, 
1436         * even 32byte mode is set, system still use 64 byte mode       */
1437        dcl |= DCL_Width128;
1438        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
1439        meminfo->is_Width128 = 1;
1440        return dimm_mask;
1441 single_channel:
1442        dimm_mask &= ~((1 << (DIMM_SOCKETS *2)) - (1 << DIMM_SOCKETS));
1443        meminfo->is_Width128 = 0;
1444        return dimm_mask;
1445}
1446
1447struct mem_param {
1448        u16 cycle_time;
1449        u8 divisor; /* In 1/40 ns increments */
1450        u8 TrwtTO;
1451        u8 Twrrd;
1452        u8 Twrwr;
1453        u8 Trdrd;
1454        u8 DcqByPassMax;
1455        u32 dch_memclk;
1456        char name[9];
1457};
1458
1459        static const struct mem_param speed[] = {
1460                {
1461                        .name       = "200Mhz\n",
1462                        .cycle_time = 0x500,
1463                        .divisor    = 200, // how many 1/40ns per clock 
1464                        .dch_memclk = DCH_MemClkFreq_200MHz, //0
1465                        .TrwtTO     = 7,
1466                        .Twrrd      = 2,
1467                        .Twrwr      = 2,
1468                        .Trdrd      = 3,
1469                        .DcqByPassMax = 4,
1470
1471                },
1472                {
1473                        .name       = "266Mhz\n",
1474                        .cycle_time = 0x375,
1475                        .divisor    = 150, //????
1476                        .dch_memclk = DCH_MemClkFreq_266MHz, //1
1477                        .TrwtTO     = 7,
1478                        .Twrrd      = 2,
1479                        .Twrwr      = 2,
1480                        .Trdrd      = 3,
1481                        .DcqByPassMax = 4,
1482                },
1483                {
1484                        .name       = "333Mhz\n",
1485                        .cycle_time = 0x300,
1486                        .divisor    = 120,
1487                        .dch_memclk = DCH_MemClkFreq_333MHz, //2
1488                        .TrwtTO     = 7,
1489                        .Twrrd      = 2,
1490                        .Twrwr      = 2,
1491                        .Trdrd      = 3,
1492                        .DcqByPassMax = 4,
1493
1494                },
1495                {
1496                        .name       = "400Mhz\n",
1497                        .cycle_time = 0x250,
1498                        .divisor    = 100,
1499                        .dch_memclk = DCH_MemClkFreq_400MHz,//3
1500                        .TrwtTO     = 7,
1501                        .Twrrd      = 2,
1502                        .Twrwr      = 2,
1503                        .Trdrd      = 3,
1504                        .DcqByPassMax = 4,
1505                },
1506                {
1507                        .cycle_time = 0x000,
1508                },
1509        };
1510
1511const struct mem_param *get_mem_param(unsigned min_cycle_time)
1512{
1513
1514        const struct mem_param *param;
1515        for(param = &speed[0]; param->cycle_time ; param++) {
1516                if (min_cycle_time > (param+1)->cycle_time) {
1517                        break;
1518                }
1519        }
1520        if (!param->cycle_time) {
1521                die("min_cycle_time to low");
1522        }
1523        printk(BIOS_SPEW, param->name);
1524#ifdef DRAM_MIN_CYCLE_TIME
1525        printk(BIOS_DEBUG, param->name);
1526#endif
1527        return param;
1528}
1529
1530u8 get_exact_divisor(int i, u8 divisor)
1531{
1532        //input divisor could be 200(200), 150(266), 120(333), 100 (400)
1533        static const u8 dv_a[] = {
1534               /* 200  266  333  400 */
1535         /*4 */   250, 250, 250, 250,
1536         /*5 */   200, 200, 200, 100,
1537         /*6 */   200, 166, 166, 100,
1538         /*7 */   200, 171, 142, 100,
1539
1540         /*8 */   200, 150, 125, 100,
1541         /*9 */   200, 156, 133, 100,
1542         /*10*/   200, 160, 120, 100,
1543         /*11*/   200, 163, 127, 100,
1544
1545         /*12*/   200, 150, 133, 100,
1546         /*13*/   200, 153, 123, 100,
1547         /*14*/   200, 157, 128, 100,
1548         /*15*/   200, 160, 120, 100,
1549        }; 
1550        
1551        unsigned fid_cur;
1552        int index;
1553        
1554        struct msr msr;
1555        msr = rdmsr(0xc0010042);
1556        fid_cur = msr.lo & 0x3f;
1557
1558        index = fid_cur>>1;
1559
1560        if(index>12) return divisor;
1561
1562        if(i>3) return divisor;
1563
1564        return dv_a[index * 4+i];
1565
1566}
1567
1568struct spd_set_memclk_result {
1569        const struct mem_param *param;
1570        long dimm_mask;
1571};
1572
1573unsigned convert_to_linear(unsigned value)
1574{
1575         static const unsigned fraction[] = { 0x25, 0x33, 0x66, 0x75 };
1576         unsigned valuex;
1577
1578         /* We need to convert value to more readable */
1579         if((value & 0xf) < 10) { //no .25, .33, .66, .75
1580                value <<= 4;
1581         } else {
1582                valuex = ((value & 0xf0) << 4) | fraction [(value & 0xf)-10];
1583                value = valuex;
1584         }
1585         return value;
1586}
1587
1588struct spd_set_memclk_result spd_set_memclk(const struct mem_controller *ctrl, long dimm_mask, struct mem_info *meminfo)
1589{
1590        /* Compute the minimum cycle time for these dimms */
1591        struct spd_set_memclk_result result;
1592        unsigned min_cycle_time, min_latency, bios_cycle_time;
1593        int i;
1594        u32 value;
1595        unsigned int max_mem_clock = 0;
1596
1597        static const u8 latency_indicies[] = { 25, 23, 9 };
1598
1599        static const u16 min_cycle_times[] = { // use full speed to compare
1600                [NBCAP_MEMCLK_NOLIMIT] = 0x250, /*2.5ns */
1601                [NBCAP_MEMCLK_333MHZ] = 0x300, /* 3.0ns */
1602                [NBCAP_MEMCLK_266MHZ] = 0x375, /* 3.75ns */
1603                [NBCAP_MEMCLK_200MHZ] = 0x500, /* 5.0s */
1604        };
1605
1606
1607        value = pci_conf1_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
1608        min_cycle_time = min_cycle_times[(value >> NBCAP_MEMCLK_SHIFT) & NBCAP_MEMCLK_MASK];
1609        get_option(&max_mem_clock, "max_mem_clock");
1610        bios_cycle_time = min_cycle_times[max_mem_clock];
1611        if (bios_cycle_time > min_cycle_time) {
1612                min_cycle_time = bios_cycle_time;
1613        }
1614        min_latency = 3; 
1615
1616        printk(BIOS_DEBUG, "%s: 0x%x\n", "1 min_cycle_time:",  min_cycle_time);
1617
1618        /* Compute the least latency with the fastest clock supported
1619         * by both the memory controller and the dimms.
1620         */
1621        for(i = 0; i < DIMM_SOCKETS; i++) {
1622                int new_cycle_time, new_latency;
1623                int index;
1624                int latencies;
1625                int latency;
1626
1627                if (!(dimm_mask & (1 << i))) {
1628                        continue;
1629                }
1630
1631                /* First find the supported CAS latencies
1632                 * Byte 18 for DDR SDRAM is interpreted:
1633                 * bit 3 == CAS Latency = 3
1634                 * bit 4 == CAS Latency = 4
1635                 * bit 5 == CAS Latency = 5
1636                 * bit 6 == CAS Latency = 6
1637                 */
1638                new_cycle_time = 0x500;
1639                new_latency = 6;
1640
1641                latencies = spd_read_byte(ctrl->channel0[i], SPD_CAS_LAT);
1642                if (latencies <= 0) continue;
1643
1644                printk(BIOS_DEBUG, "%s: 0x%x\n", "i:", i);
1645                printk(BIOS_DEBUG, "%s: 0x%x\n", "\tlatencies:",  latencies);
1646                /* Compute the lowest cas latency supported */
1647                latency = log2f(latencies) - 2;
1648
1649                /* Loop through and find a fast clock with a low latency */
1650                for(index = 0; index < 3; index++, latency++) {
1651                        int value;
1652                        if ((latency < 3) || (latency > 6) ||
1653                                (!(latencies & (1 << latency)))) {
1654                                continue;
1655                        }
1656                        value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1657                        if (value < 0) {
1658                                goto hw_error;
1659                        }
1660                        printk(BIOS_DEBUG, "%s: 0x%x\n", "\tindex:",  index);
1661                        printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tlatency:",  latency);
1662                        printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tvalue1:",  value);
1663
1664                        value = convert_to_linear(value);
1665
1666                        printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tvalue2:",  value);
1667
1668                        /* Only increase the latency if we decreas the clock */
1669                        if (value >= min_cycle_time ) {
1670                                if(value < new_cycle_time) {
1671                                        new_cycle_time = value;
1672                                        new_latency = latency;
1673                                } else if (value == new_cycle_time) {
1674                                        if(new_latency > latency) {
1675                                                new_latency = latency;
1676                                        }
1677                                }
1678                        }
1679                        printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tnew_cycle_time:",  new_cycle_time);
1680                        printk(BIOS_DEBUG, "%s: 0x%x\n", "\t\tnew_latency:",  new_latency);
1681
1682                }
1683                if (new_latency > 6){
1684                        continue;
1685                }
1686                /* Does min_latency need to be increased? */
1687                if (new_cycle_time > min_cycle_time) {
1688                        min_cycle_time = new_cycle_time;
1689                }
1690                /* Does min_cycle_time need to be increased? */
1691                if (new_latency > min_latency) {
1692                        min_latency = new_latency;
1693                }
1694
1695                printk(BIOS_DEBUG, "%s: 0x%x\n", "2 min_cycle_time:",  min_cycle_time);
1696                printk(BIOS_DEBUG, "%s: 0x%x\n", "2 min_latency:",  min_latency);
1697        }
1698        /* Make a second pass through the dimms and disable
1699         * any that cannot support the selected memclk and cas latency.
1700         */
1701
1702        printk(BIOS_DEBUG, "%s: 0x%x\n", "3 min_cycle_time:",  min_cycle_time);
1703        printk(BIOS_DEBUG, "%s: 0x%x\n", "3 min_latency:",  min_latency);
1704        
1705        for(i = 0; (i < DIMM_SOCKETS) && (ctrl->channel0[i]); i++) {
1706                int latencies;
1707                int latency;
1708                int index;
1709                int value;
1710                if (!(dimm_mask & (1 << i))) {
1711                        continue;
1712                }
1713                latencies = spd_read_byte(ctrl->channel0[i], SPD_CAS_LAT);
1714                if (latencies < 0) goto hw_error;
1715                if (latencies == 0) {
1716                        continue;
1717                }
1718
1719                /* Compute the lowest cas latency supported */
1720                latency = log2f(latencies) -2;
1721
1722                /* Walk through searching for the selected latency */
1723                for(index = 0; index < 3; index++, latency++) {
1724                        if (!(latencies & (1 << latency))) {
1725                                continue;
1726                        }
1727                        if (latency == min_latency)
1728                                break;
1729                }
1730                /* If I can't find the latency or my index is bad error */
1731                if ((latency != min_latency) || (index >= 3)) {
1732                        goto dimm_err;
1733                }
1734        
1735                /* Read the min_cycle_time for this latency */
1736                value = spd_read_byte(ctrl->channel0[i], latency_indicies[index]);
1737                if (value < 0) goto hw_error;
1738        
1739                value = convert_to_linear(value);       
1740                /* All is good if the selected clock speed 
1741                 * is what I need or slower.
1742                 */
1743                if (value <= min_cycle_time) {
1744                        continue;
1745                }
1746                /* Otherwise I have an error, disable the dimm */
1747        dimm_err:
1748                dimm_mask = disable_dimm(ctrl, i, dimm_mask, meminfo);
1749        }
1750
1751        printk(BIOS_DEBUG, "%s: 0x%x\n", "4 min_cycle_time:",  min_cycle_time);
1752        
1753        /* Now that I know the minimum cycle time lookup the memory parameters */
1754        result.param = get_mem_param(min_cycle_time);
1755
1756        /* Update DRAM Config High with our selected memory speed */
1757        value = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
1758        value &= ~(DCH_MemClkFreq_MASK << DCH_MemClkFreq_SHIFT);
1759
1760        value |= result.param->dch_memclk << DCH_MemClkFreq_SHIFT;
1761        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, value);
1762
1763        printk(BIOS_DEBUG, result.param->name);
1764
1765        /* Update DRAM Timing Low with our selected cas latency */
1766        value = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1767        value &= ~(DTL_TCL_MASK << DTL_TCL_SHIFT);
1768        value |= (min_latency - DTL_TCL_BASE)  << DTL_TCL_SHIFT;
1769        pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_LOW, value);
1770        
1771        result.dimm_mask = dimm_mask;
1772        return result;
1773 hw_error:
1774        result.param = (const struct mem_param *)0;
1775        result.dimm_mask = -1;
1776        return result;
1777}
1778
1779unsigned convert_to_1_4(unsigned value)
1780{
1781         static const u8 fraction[] = { 0, 1, 2, 2, 3, 3, 0 };
1782         unsigned valuex;
1783
1784         /* We need to convert value to more readable */
1785         valuex =  fraction [value & 0x7];
1786         return valuex;
1787}
1788int update_dimm_Trc(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1789{
1790        unsigned clocks, old_clocks;
1791        u32 dtl;
1792        int value;
1793        int value2;
1794        value = spd_read_byte(ctrl->channel0[i], SPD_TRC);
1795        if (value < 0) return -1;
1796
1797                value2 = spd_read_byte(ctrl->channel0[i], SPD_TRC -1);
1798                value <<= 2;
1799                value += convert_to_1_4(value2>>4);
1800
1801        value *=10;
1802
1803        clocks = (value + param->divisor - 1)/param->divisor;
1804        
1805        if (clocks < DTL_TRC_MIN) {
1806                clocks = DTL_TRC_MIN;
1807        }
1808        if (clocks > DTL_TRC_MAX) {
1809                return 0;
1810        }
1811
1812        dtl = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1813        old_clocks = ((dtl >> DTL_TRC_SHIFT) & DTL_TRC_MASK) + DTL_TRC_BASE;
1814        if (old_clocks >= clocks) {  //?? someone did it 
1815                // clocks = old_clocks;
1816                return 1;
1817        }
1818        dtl &= ~(DTL_TRC_MASK << DTL_TRC_SHIFT);
1819        dtl |=  ((clocks - DTL_TRC_BASE) << DTL_TRC_SHIFT);
1820        pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1821        return 1;
1822}
1823
1824int update_dimm_Trfc(const struct mem_controller *ctrl, const struct mem_param *param, int i, struct mem_info *meminfo)
1825{
1826        unsigned clocks, old_clocks;
1827        u32 dth;
1828        int value;
1829
1830        //get the cs_size --> logic dimm size
1831        value = spd_read_byte(ctrl->channel0[i], SPD_PRI_WIDTH);
1832        if (value < 0) {
1833                return -1;
1834        }
1835
1836        value = 6 - log2f(value); //4-->4, 8-->3, 16-->2
1837
1838        clocks = meminfo->sz[i].per_rank - 27 + 2 - value;
1839
1840        dth = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1841
1842        old_clocks = ((dth >> (DTH_TRFC0_SHIFT+i*3)) & DTH_TRFC_MASK);
1843        if (old_clocks >= clocks) { // some one did it?
1844                return 1;
1845        }
1846        dth &= ~(DTH_TRFC_MASK << (DTH_TRFC0_SHIFT+i*3));
1847        dth |= clocks  << (DTH_TRFC0_SHIFT+i*3);
1848        pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1849        return 1;
1850}
1851
1852int update_dimm_TT_1_4(const struct mem_controller *ctrl, const struct mem_param *param, 
1853                                        int i, 
1854                                        unsigned TT_REG, 
1855                                        unsigned SPD_TT, unsigned TT_SHIFT, 
1856                                        unsigned TT_MASK, unsigned TT_BASE, 
1857                                        unsigned TT_MIN, unsigned TT_MAX )
1858{
1859        unsigned clocks, old_clocks;
1860        u32 dtl;
1861        int value;
1862        value = spd_read_byte(ctrl->channel0[i], SPD_TT); //already in 1/4 ns
1863        if (value < 0) return -1;
1864        value *=10;
1865        clocks = (value + param->divisor -1)/param->divisor;
1866        if (clocks < TT_MIN) {
1867                clocks = TT_MIN;
1868        }
1869        if (clocks > TT_MAX) {
1870                return 0;
1871        }
1872        dtl = pci_conf1_read_config32(ctrl->f2, TT_REG);
1873
1874        old_clocks = ((dtl >> TT_SHIFT) & TT_MASK) + TT_BASE;
1875        if (old_clocks >= clocks) { //some one did it?
1876//              clocks = old_clocks;
1877                return 1;
1878        }
1879        dtl &= ~(TT_MASK << TT_SHIFT);
1880        dtl |= ((clocks - TT_BASE) << TT_SHIFT);
1881        pci_conf1_write_config32(ctrl->f2, TT_REG, dtl);
1882        return 1;
1883}
1884
1885int update_dimm_Trcd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1886{
1887        return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TRCD, DTL_TRCD_SHIFT, DTL_TRCD_MASK, DTL_TRCD_BASE, DTL_TRCD_MIN, DTL_TRCD_MAX);
1888}
1889
1890int update_dimm_Trrd(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1891{
1892        return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TRRD, DTL_TRRD_SHIFT, DTL_TRRD_MASK, DTL_TRRD_BASE, DTL_TRRD_MIN, DTL_TRRD_MAX);
1893}
1894
1895int update_dimm_Tras(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1896{
1897        unsigned clocks, old_clocks;
1898        u32 dtl;
1899        int value;
1900        value = spd_read_byte(ctrl->channel0[i], SPD_TRAS); //in 1 ns
1901        if (value < 0) return -1;
1902        printk(BIOS_DEBUG, "%s: 0x%x\n", "update_dimm_Tras: 0 value=",  value);
1903        
1904        value<<=2; //convert it to in 1/4ns
1905
1906        value *= 10;
1907        printk(BIOS_DEBUG, "%s: 0x%x\n", "update_dimm_Tras:  1 value=",  value);
1908
1909        clocks = (value  + param->divisor - 1)/param->divisor;
1910        printk(BIOS_DEBUG, "%s: 0x%x\n", "update_dimm_Tras: divisor=",  param->divisor);
1911        printk(BIOS_DEBUG, "%s: 0x%x\n", "update_dimm_Tras: clocks=",  clocks);
1912        if (clocks < DTL_TRAS_MIN) {
1913                clocks = DTL_TRAS_MIN;
1914        }
1915        if (clocks > DTL_TRAS_MAX) {
1916                return 0;
1917        }
1918        dtl = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_LOW);
1919        old_clocks = ((dtl >> DTL_TRAS_SHIFT) & DTL_TRAS_MASK) + DTL_TRAS_BASE;
1920        if (old_clocks >= clocks) { // someone did it?
1921                return 1;
1922        }
1923        dtl &= ~(DTL_TRAS_MASK << DTL_TRAS_SHIFT);
1924        dtl |= ((clocks - DTL_TRAS_BASE) << DTL_TRAS_SHIFT);
1925        pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_LOW, dtl);
1926        return 1;
1927}
1928
1929int update_dimm_Trp(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1930{
1931        return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TRP, DTL_TRP_SHIFT, DTL_TRP_MASK, DTL_TRP_BASE, DTL_TRP_MIN, DTL_TRP_MAX);
1932}
1933
1934int update_dimm_Trtp(const struct mem_controller *ctrl, const struct mem_param *param, int i, struct mem_info *meminfo)
1935{
1936        //need to figure if it is 32 byte burst or 64 bytes burst
1937        int offset = 2;
1938        if(!meminfo->is_Width128) {
1939                u32 dword;
1940                dword = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
1941                if((dword &  DCL_BurstLength32)) offset = 0;
1942        }
1943        return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TRTP, DTL_TRTP_SHIFT, DTL_TRTP_MASK, DTL_TRTP_BASE+offset, DTL_TRTP_MIN+offset, DTL_TRTP_MAX+offset);
1944}
1945
1946
1947int update_dimm_Twr(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1948{
1949        return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_LOW, SPD_TWR, DTL_TWR_SHIFT, DTL_TWR_MASK, DTL_TWR_BASE, DTL_TWR_MIN, DTL_TWR_MAX);
1950}
1951
1952
1953int update_dimm_Tref(const struct mem_controller *ctrl, const struct mem_param *param, int i)
1954{
1955        u32 dth, dth_old;
1956        int value;
1957        value = spd_read_byte(ctrl->channel0[i], SPD_TREF); // 0: 15.625us, 1: 3.9us 2: 7.8 us....
1958        if (value < 0) return -1;
1959
1960        if(value == 1 ) {
1961                value = 3;
1962        } else {
1963                value = 2;
1964        }
1965
1966        dth = pci_conf1_read_config32(ctrl->f2, DRAM_TIMING_HIGH);
1967
1968        dth_old = dth;
1969        dth &= ~(DTH_TREF_MASK << DTH_TREF_SHIFT);
1970        dth |= (value << DTH_TREF_SHIFT);
1971        if(dth_old != dth) {
1972                pci_conf1_write_config32(ctrl->f2, DRAM_TIMING_HIGH, dth);
1973        }
1974        return 1;
1975}
1976
1977void set_4RankRDimm(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
1978{
1979#ifdef QRANK_DIMM_SUPPORT
1980        int value;
1981        int i;
1982        
1983
1984        if(!(meminfo->is_registered)) return; 
1985
1986        value = 0;
1987
1988        for(i = 0; i < DIMM_SOCKETS; i++) {
1989                if (!(dimm_mask & (1 << i))) {
1990                        continue;
1991                }
1992
1993                if(meminfo->sz.rank == 4) {
1994                        value = 1;
1995                        break;
1996                }
1997        }
1998
1999        if(value == 1) {
2000                u32 dch;
2001                dch = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2002                dch |= DCH_FourRankRDimm;
2003                pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2004        }
2005#endif
2006}
2007
2008
2009u32 get_extra_dimm_mask(const struct mem_controller *ctrl, struct mem_info *meminfo)
2010{
2011        int i;
2012
2013        u32 mask_x4;
2014        u32 mask_x16;
2015        u32 mask_single_rank;
2016        u32 mask_page_1k;
2017        int value;
2018#ifdef QRANK_DIMM_SUPPORT
2019        int rank;
2020#endif
2021
2022        long dimm_mask = meminfo->dimm_mask;
2023
2024
2025        mask_x4 = 0;
2026        mask_x16 = 0;
2027        mask_single_rank = 0;
2028        mask_page_1k = 0;
2029
2030        for(i = 0; i < DIMM_SOCKETS; i++) {
2031                
2032                if (!(dimm_mask & (1 << i))) {
2033                        continue;
2034                }
2035
2036                if(meminfo->sz[i].rank == 1) {
2037                        mask_single_rank |= 1<<i;
2038                }
2039
2040                if(meminfo->sz[i].col==10) {
2041                        mask_page_1k |= 1<<i;
2042                }
2043
2044
2045                value = spd_read_byte(ctrl->channel0[i], SPD_PRI_WIDTH);
2046
2047                #ifdef QRANK_DIMM_SUPPORT
2048                rank = meminfo->sz[i].rank;
2049                #endif
2050
2051                if(value==4) {
2052                        mask_x4 |= (1<<i);
2053                        #ifdef QRANK_DIMM_SUPPORT
2054                        if(rank==4) {
2055                                mask_x4 |= 1<<(i+2);
2056                        }
2057                        #endif
2058                } else if(value==16) {
2059                        mask_x16 |= (1<<i);
2060                        #ifdef QRANK_DIMM_SUPPORT
2061                        if(rank==4) {
2062                                mask_x16 |= 1<<(i+2);
2063                        }
2064                        #endif
2065                }
2066                
2067        }
2068        
2069        meminfo->x4_mask= mask_x4;
2070        meminfo->x16_mask = mask_x16;
2071        
2072        meminfo->single_rank_mask = mask_single_rank;
2073        meminfo->page_1k_mask = mask_page_1k;
2074
2075        return mask_x4;
2076
2077}
2078
2079
2080void set_dimm_x4(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
2081{
2082        u32 dcl;
2083        dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
2084        dcl &= ~(DCL_X4Dimm_MASK<<DCL_X4Dimm_SHIFT);
2085        dcl |= ((meminfo->x4_mask) & 0xf) << (DCL_X4Dimm_SHIFT);
2086        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2087}
2088
2089int count_ones(u32 dimm_mask)
2090{
2091        int dimms;
2092        unsigned index;
2093        dimms = 0;
2094        for(index = 0; index < DIMM_SOCKETS; index++, dimm_mask>>=1) {
2095                if (dimm_mask & 1) {
2096                        dimms++;
2097                }
2098        }
2099        return dimms;
2100}
2101
2102
2103void set_DramTerm(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
2104{
2105        u32 dcl;
2106        unsigned odt;
2107        odt = 1; // 75 ohms
2108
2109        if(param->divisor == 100) { //DDR2 800
2110                if(meminfo->is_Width128) {
2111                        if(count_ones(meminfo->dimm_mask & 0x0f)==2) {
2112                                odt = 3;  //50 ohms
2113                        }
2114                }
2115
2116        }       
2117        dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
2118        dcl &= ~(DCL_DramTerm_MASK<<DCL_DramTerm_SHIFT);
2119        dcl |= (odt & DCL_DramTerm_MASK) << (DCL_DramTerm_SHIFT);
2120        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2121}
2122
2123
2124void set_ecc(const struct mem_controller *ctrl,const struct mem_param *param, long dimm_mask, struct mem_info *meminfo)
2125{
2126        int i;
2127        int value;
2128        int ECC_memory = 1;
2129        
2130        u32 dcl, nbcap;
2131        nbcap = pci_conf1_read_config32(ctrl->f3, NORTHBRIDGE_CAP);
2132        dcl = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_LOW);
2133        dcl &= ~DCL_DimmEccEn;
2134        if (nbcap & NBCAP_ECC) {
2135                dcl |= DCL_DimmEccEn;
2136        }
2137        get_option(&ECC_memory, "ECC_memory");
2138        if (ECC_memory) {
2139                dcl &= ~DCL_DimmEccEn;
2140        }
2141        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2142
2143        meminfo->is_ecc = 1;
2144        if(!(dcl & DCL_DimmEccEn)) {
2145                meminfo->is_ecc = 0;
2146                return; // already disabled the ECC, so don't need to read SPD any more
2147        }
2148
2149        for(i = 0; i < DIMM_SOCKETS; i++) {
2150                
2151                if (!(dimm_mask & (1 << i))) {
2152                        continue;
2153                }
2154
2155                value = spd_read_byte(ctrl->channel0[i], SPD_DIMM_CONF_TYPE);
2156
2157                if(!(value & SPD_DIMM_CONF_TYPE_ECC)) {
2158                        dcl &= ~DCL_DimmEccEn;
2159                        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_LOW, dcl);
2160                        meminfo->is_ecc = 0;
2161                        return;
2162                }
2163
2164        }
2165}
2166
2167int update_dimm_Twtr(const struct mem_controller *ctrl, const struct mem_param *param, int i)
2168{
2169
2170        return update_dimm_TT_1_4(ctrl, param, i, DRAM_TIMING_HIGH, SPD_TWTR, DTH_TWTR_SHIFT, DTH_TWTR_MASK, DTH_TWTR_BASE, DTH_TWTR_MIN, DTH_TWTR_MAX);
2171
2172}
2173
2174void set_TT(const struct mem_controller *ctrl, const struct mem_param *param, unsigned TT_REG,
2175                unsigned TT_SHIFT, unsigned TT_MASK, unsigned TT_BASE, unsigned TT_MIN, unsigned TT_MAX, unsigned val, const char *str)
2176{
2177        u32 reg;
2178
2179        if ((val < TT_MIN) || (val > TT_MAX)) {
2180                printk(BIOS_ERR, str);
2181                die(" Unknown\n");
2182        }
2183
2184        reg = pci_conf1_read_config32(ctrl->f2, TT_REG);
2185        reg &= ~(TT_MASK << TT_SHIFT);
2186        reg |= ((val - TT_BASE) << TT_SHIFT);
2187        pci_conf1_write_config32(ctrl->f2, TT_REG, reg);
2188        return;
2189}
2190
2191void set_TrwtTO(const struct mem_controller *ctrl, const struct mem_param *param)
2192{
2193        set_TT(ctrl, param, DRAM_TIMING_HIGH, DTH_TRWTTO_SHIFT, DTH_TRWTTO_MASK,DTH_TRWTTO_BASE, DTH_TRWTTO_MIN, DTH_TRWTTO_MAX, param->TrwtTO, "TrwtTO");
2194}
2195
2196void set_Twrrd(const struct mem_controller *ctrl, const struct mem_param *param)
2197{
2198        set_TT(ctrl, param, DRAM_TIMING_HIGH, DTH_TWRRD_SHIFT, DTH_TWRRD_MASK,DTH_TWRRD_BASE, DTH_TWRRD_MIN, DTH_TWRRD_MAX, param->Twrrd, "Twrrd");
2199}
2200
2201void set_Twrwr(const struct mem_controller *ctrl, const struct mem_param *param)
2202{
2203        set_TT(ctrl, param, DRAM_TIMING_HIGH, DTH_TWRWR_SHIFT, DTH_TWRWR_MASK,DTH_TWRWR_BASE, DTH_TWRWR_MIN, DTH_TWRWR_MAX, param->Twrwr, "Twrwr");
2204}
2205
2206void set_Trdrd(const struct mem_controller *ctrl, const struct mem_param *param)
2207{
2208        set_TT(ctrl, param, DRAM_TIMING_HIGH, DTH_TRDRD_SHIFT, DTH_TRDRD_MASK,DTH_TRDRD_BASE, DTH_TRDRD_MIN, DTH_TRDRD_MAX, param->Trdrd, "Trdrd");
2209}
2210
2211void set_DcqBypassMax(const struct mem_controller *ctrl, const struct mem_param *param)
2212{
2213        set_TT(ctrl, param, DRAM_CONFIG_HIGH, DCH_DcqBypassMax_SHIFT, DCH_DcqBypassMax_MASK,DCH_DcqBypassMax_BASE, DCH_DcqBypassMax_MIN, DCH_DcqBypassMax_MAX, param->DcqByPassMax, "DcqBypassMax"); // value need to be in CMOS
2214}
2215
2216void set_Tfaw(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
2217{
2218        static const u8 faw_1k[] = {8, 10, 13, 14};
2219        static const u8 faw_2k[] = {10, 14, 17, 18};
2220        unsigned memclkfreq_index;
2221        unsigned faw;
2222
2223
2224        memclkfreq_index = param->dch_memclk;
2225
2226        if(meminfo->page_1k_mask != 0) { //1k page
2227                faw = faw_1k[memclkfreq_index];
2228        }
2229        else {
2230                faw = faw_2k[memclkfreq_index];
2231        }
2232
2233        set_TT(ctrl, param, DRAM_CONFIG_HIGH, DCH_FourActWindow_SHIFT, DCH_FourActWindow_MASK, DCH_FourActWindow_BASE, DCH_FourActWindow_MIN, DCH_FourActWindow_MAX, faw, "FourActWindow");
2234
2235}
2236
2237
2238void set_max_async_latency(const struct mem_controller *ctrl, const struct mem_param *param)
2239{
2240        u32 dch;
2241        unsigned async_lat;
2242
2243
2244        dch = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2245        dch &= ~(DCH_MaxAsyncLat_MASK << DCH_MaxAsyncLat_SHIFT);
2246
2247        //FIXME: We need to use Max of DqsRcvEnDelay + 6ns here: After trainning and get that from index reg 0x10, 0x13, 0x16, 0x19, 0x30, 0x33, 0x36, 0x39
2248        async_lat = 6+6;
2249        
2250        
2251        dch |= ((async_lat - DCH_MaxAsyncLat_BASE) << DCH_MaxAsyncLat_SHIFT);
2252        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2253}
2254
2255void set_SlowAccessMode(const struct mem_controller *ctrl)
2256{
2257        u32 dch;
2258
2259        dch = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2260
2261        dch |= (1<<20);
2262
2263        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2264}
2265
2266
2267/*
2268        DRAM_OUTPUT_DRV_COMP_CTRL 0, 0x20
2269        DRAM_ADDR_TIMING_CTRL 04, 0x24
2270*/
2271void set_misc_timing(const struct mem_controller *ctrl, struct mem_info *meminfo)
2272{
2273        u32 dword;
2274        u32 dwordx;
2275        long dimm_mask = meminfo->dimm_mask & 0x0f;
2276
2277#if DIMM_SUPPORT==0x0104   /* DDR2 and REG */
2278        /* for REG DIMM */      
2279        dword = 0x00111222;
2280        dwordx = 0x002f0000;
2281        switch (meminfo->memclk_set) {
2282        case DCH_MemClkFreq_266MHz:
2283                if( (dimm_mask == 0x03) || (dimm_mask == 0x02) || (dimm_mask == 0x01)) { 
2284                        dwordx = 0x002f2700; 
2285                }
2286                break;
2287        case DCH_MemClkFreq_333MHz:
2288                if( (dimm_mask == 0x03) || (dimm_mask == 0x02) || (dimm_mask == 0x01)) {
2289                        if ((meminfo->single_rank_mask & 0x03)!=0x03) { //any double rank there?
2290                                dwordx = 0x002f2f00;
2291                        }
2292                }
2293                break;
2294        case DCH_MemClkFreq_400MHz:
2295                dwordx = 0x002f3300;
2296                break;
2297        }
2298
2299#endif
2300
2301#if DIMM_SUPPORT==0x0004  /* DDR2 and unbuffered */
2302        unsigned SlowAccessMode = 0;
2303       /* for UNBUF DIMM */
2304        dword = 0x00111222;
2305        dwordx = 0x002f2f00;
2306        switch (meminfo->memclk_set) {
2307        case DCH_MemClkFreq_200MHz:
2308                if(dimm_mask == 0x03) {
2309                        SlowAccessMode = 1;
2310                        dword = 0x00111322;
2311                }
2312                break;
2313        case DCH_MemClkFreq_266MHz:
2314                if(dimm_mask == 0x03) {
2315                        SlowAccessMode = 1;
2316                        dword = 0x00111322;
2317                        if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0)) {
2318                                switch (meminfo->single_rank_mask) {
2319                                case 0x03:
2320                                        dwordx = 0x00002f00; //x8 single Rank
2321                                        break;
2322                                case 0x00:
2323                                        dwordx = 0x00342f00; //x8 double Rank
2324                                        break;
2325                                default: 
2326                                        dwordx = 0x00372f00; //x8 single Rank and double Rank mixed
2327                                }
2328                        } else if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0x01) && (meminfo->single_rank_mask == 0x01)) {
2329                                        dwordx = 0x00382f00; //x8 Double Rank and x16 single Rank mixed
2330                        } else if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0x02) && (meminfo->single_rank_mask == 0x02)) {
2331                                        dwordx = 0x00382f00; //x16 single Rank and x8 double Rank mixed
2332                        }
2333
2334                }
2335                else {
2336                        if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0x00) && ((meminfo->single_rank_mask == 0x01)||(meminfo->single_rank_mask == 0x02)))  { //x8 single rank
2337                                dwordx = 0x002f2f00;
2338                        } else {
2339                                dwordx = 0x002b2f00;
2340                        }
2341                }
2342                break;
2343        case DCH_MemClkFreq_333MHz:
2344                dwordx = 0x00202220;
2345                if(dimm_mask == 0x03) {
2346                        SlowAccessMode = 1;
2347                        dword = 0x00111322;
2348                        if((meminfo->x4_mask == 0 ) && (meminfo->x16_mask == 0)) {
2349                                switch (meminfo->single_rank_mask) {
2350                                case 0x03:
2351                                        dwordx = 0x00302220; //x8 single Rank
2352                                        break;
2353                                case 0x00:
2354                                        dwordx = 0x002b2220; //x8 double Rank
2355                                        break;
2356                                default:
2357                                        dwordx = 0x002a2220; //x8 single Rank and double Rank mixed
2358                                }
2359                        } else if((meminfo->x4_mask == 0) && (meminfo->x16_mask == 0x01) && (meminfo->single_rank_mask == 0x01)) {
2360                                        dwordx = 0x002c2220; //x8 Double Rank and x16 single Rank mixed
2361                        } else if((meminfo->x4_mask == 0) && (meminfo->x16_mask == 0x02) && (meminfo->single_rank_mask == 0x02)) {
2362                                        dwordx = 0x002c2220; //x16 single Rank and x8 double Rank mixed
2363                        }
2364                }
2365                break;
2366        case DCH_MemClkFreq_400MHz:
2367                dwordx = 0x00202520;
2368                SlowAccessMode = 1;
2369                if(dimm_mask == 0x03) {
2370                        dword = 0x00113322;
2371                } else {
2372                        dword = 0x00113222;
2373                }
2374                break;
2375        } 
2376
2377        printk(BIOS_DEBUG, "\tdimm_mask =  0x%x\n",  meminfo->dimm_mask);
2378        printk(BIOS_DEBUG, "\tx4_mask =  0x%x\n",  meminfo->x4_mask);
2379        printk(BIOS_DEBUG, "\tx16_mask =  0x%x\n",  meminfo->x16_mask);
2380        printk(BIOS_DEBUG, "\tsingle_rank_mask =  0x%x\n",  meminfo->single_rank_mask);
2381        printk(BIOS_DEBUG, "\tODC =  0x%x\n",  dword);
2382        printk(BIOS_DEBUG, "\tAddr Timing=  0x%x\n",  dwordx);
2383#endif
2384
2385#if (DIMM_SUPPORT & 0x0100)==0x0000 /* 2T mode only used for unbuffered DIMM */
2386        if(SlowAccessMode) {
2387                set_SlowAccessMode(ctrl);
2388        }
2389#endif
2390
2391        /* Program the Output Driver Compensation Control Registers (Function 2:Offset 0x9c, index 0, 0x20) */
2392        pci_write_config32_index_wait(ctrl->f2, 0x98, 0, dword);
2393        if(meminfo->is_Width128) {      
2394                pci_write_config32_index_wait(ctrl->f2, 0x98, 0x20, dword);
2395        }
2396
2397        /* Program the Address Timing Control Registers (Function 2:Offset 0x9c, index 4, 0x24) */
2398        pci_write_config32_index_wait(ctrl->f2, 0x98, 4, dwordx);
2399        if(meminfo->is_Width128) {
2400                pci_write_config32_index_wait(ctrl->f2, 0x98, 0x24, dwordx);
2401        }
2402
2403}
2404
2405
2406void set_RDqsEn(const struct mem_controller *ctrl, const struct mem_param *param, struct mem_info *meminfo)
2407{
2408#if CPU_SOCKET_TYPE==SOCKET_L1
2409        //only need to set for reg and x8
2410        u32 dch;
2411
2412        dch = pci_conf1_read_config32(ctrl->f2, DRAM_CONFIG_HIGH);
2413
2414        dch &= ~DCH_RDqsEn;
2415        if((!meminfo->x4_mask) && (!meminfo->x16_mask)) {
2416                dch |= DCH_RDqsEn;
2417        }
2418
2419        pci_conf1_write_config32(ctrl->f2, DRAM_CONFIG_HIGH, dch);
2420#endif
2421}
2422
2423
2424void set_idle_cycle_limit(const struct mem_controller *ctrl, const struct mem_param *param)
2425{
2426        u32 dcm;
2427        /* AMD says to Hardcode this */
2428        dcm = pci_conf1_read_config32(ctrl->f2, DRAM_CTRL_MISC);
2429        dcm &= ~(DCM_ILD_lmt_MASK << DCM_ILD_lmt_SHIFT);
2430        dcm |= DCM_ILD_lmt_16 << DCM_ILD_lmt_SHIFT;
2431        dcm |= DCM_DCC_EN;
2432        pci_conf1_write_config32(ctrl->f2, DRAM_CTRL_MISC, dcm);
2433}
2434
2435void set_RdWrQByp(const struct mem_controller *ctrl, const struct mem_param *param)
2436{
2437        set_TT(ctrl, param, DRAM_CTRL_MISC, DCM_RdWrQByp_SHIFT, DCM_RdWrQByp_MASK,0, 0, 3, 2, "RdWrQByp");
2438}
2439
2440
2441
2442long spd_set_dram_timing(const struct mem_controller *ctrl, const struct mem_param *param, long dimm_mask, struct mem_info *meminfo)
2443{
2444        int i;
2445
2446        for(i = 0; i < DIMM_SOCKETS; i++) {
2447                int rc;
2448                if (!(dimm_mask & (1 << i))) {
2449                        continue;
2450                }
2451                printk(BIOS_DEBUG, "%s: 0x%x\n", "dimm socket: ",  i);
2452                /* DRAM Timing Low Register */
2453                printk(BIOS_DEBUG, "\ttrc\n");
2454                if ((rc = update_dimm_Trc (ctrl, param, i)) <= 0) goto dimm_err;
2455
2456                printk(BIOS_DEBUG, "\ttrcd\n");
2457                if ((rc = update_dimm_Trcd(ctrl, param, i)) <= 0) goto dimm_err;
2458
2459                printk(BIOS_DEBUG, "\ttrrd\n");
2460                if ((rc = update_dimm_Trrd(ctrl, param, i)) <= 0) goto dimm_err;
2461
2462                printk(BIOS_DEBUG, "\ttras\n");
2463                if ((rc = update_dimm_Tras(ctrl, param, i)) <= 0) goto dimm_err;
2464
2465                printk(BIOS_DEBUG, "\ttrp\n");
2466                if ((rc = update_dimm_Trp (ctrl, param, i)) <= 0) goto dimm_err;
2467
2468                printk(BIOS_DEBUG, "\ttrtp\n");
2469                if ((rc = update_dimm_Trtp(ctrl, param, i, meminfo)) <= 0) goto dimm_err;
2470
2471                printk(BIOS_DEBUG, "\ttwr\n");
2472                if ((rc = update_dimm_Twr (ctrl, param, i)) <= 0) goto dimm_err;
2473
2474                /* DRAM Timing High Register */
2475                printk(BIOS_DEBUG, "\ttref\n");
2476                if ((rc = update_dimm_Tref(ctrl, param, i)) <= 0) goto dimm_err;
2477
2478                printk(BIOS_DEBUG, "\ttwtr\n");
2479                if ((rc = update_dimm_Twtr(ctrl, param, i)) <= 0) goto dimm_err;
2480
2481                printk(BIOS_DEBUG, "\ttrfc\n");
2482                if ((rc = update_dimm_Trfc(ctrl, param, i, meminfo)) <= 0) goto dimm_err;
2483
2484                /* DRAM Config Low */
2485
2486                continue;
2487        dimm_err:
2488                if (rc < 0) {
2489                        return -1;
2490                }
2491                dimm_mask = disable_dimm(ctrl, i, dimm_mask, meminfo);
2492        }
2493
2494        meminfo->dimm_mask = dimm_mask; // store final dimm_mask
2495
2496        get_extra_dimm_mask(ctrl, meminfo); // will be used by RDqsEn and dimm_x4
2497        /* DRAM Timing Low Register */
2498
2499        /* DRAM Timing High Register */
2500        set_TrwtTO(ctrl, param);
2501        set_Twrrd (ctrl, param);
2502        set_Twrwr (ctrl, param);
2503        set_Trdrd (ctrl, param);
2504        
2505        set_4RankRDimm(ctrl, param, meminfo);
2506
2507        /* DRAM Config High */
2508        set_Tfaw(ctrl, param, meminfo);
2509        set_DcqBypassMax(ctrl, param);
2510        set_max_async_latency(ctrl, param);
2511        set_RDqsEn(ctrl, param, meminfo);
2512
2513        /* DRAM Config Low */
2514        set_ecc(ctrl, param, dimm_mask, meminfo);
2515        set_dimm_x4(ctrl, param, meminfo);
2516        set_DramTerm(ctrl, param, meminfo);
2517        
2518        /* DRAM Control Misc */
2519        set_idle_cycle_limit(ctrl, param);
2520        set_RdWrQByp(ctrl, param);
2521
2522        return dimm_mask;
2523}
2524
2525void sdram_set_spd_registers(const struct mem_controller *ctrl, struct sys_info *sysinfo) 
2526{
2527        struct spd_set_memclk_result result;
2528        const struct mem_param *param;
2529        struct mem_param paramx;
2530        struct mem_info *meminfo;
2531        long dimm_mask;
2532        void activate_spd_rom(const struct mem_controller *ctrl);
2533        void hard_reset(void);
2534#if 1
2535        if (!sysinfo->ctrl_present[ctrl->node_id]) {
2536                return;
2537        }
2538#endif
2539        meminfo = &sysinfo->meminfo[ctrl->node_id];
2540
2541        printk(BIOS_DEBUG, "FIXME sdram_set_spd_registers: paramx :%p\n", &paramx);
2542        
2543        activate_spd_rom(ctrl);
2544        dimm_mask = spd_detect_dimms(ctrl);
2545        if (!(dimm_mask & ((1 << DIMM_SOCKETS) - 1))) {
2546                printk(BIOS_DEBUG, "No memory for this cpu\n");
2547                return;
2548        }
2549        dimm_mask = spd_enable_2channels(ctrl, dimm_mask, meminfo);        
2550        if (dimm_mask < 0) 
2551                goto hw_spd_err;
2552        dimm_mask = spd_set_ram_size(ctrl , dimm_mask, meminfo);           
2553        if (dimm_mask < 0) 
2554                goto hw_spd_err;
2555        dimm_mask = spd_handle_unbuffered_dimms(ctrl, dimm_mask, meminfo); 
2556        if (dimm_mask < 0) 
2557                goto hw_spd_err;
2558        result = spd_set_memclk(ctrl, dimm_mask, meminfo);
2559        param     = result.param;
2560        dimm_mask = result.dimm_mask;
2561        if (dimm_mask < 0) 
2562                goto hw_spd_err;
2563
2564        //store memclk set to sysinfo, incase we need rebuilt param again
2565        meminfo->memclk_set = param->dch_memclk;
2566
2567        memcpy(&paramx, param, sizeof(paramx));
2568        
2569        paramx.divisor = get_exact_divisor(param->dch_memclk, paramx.divisor);
2570
2571        dimm_mask = spd_set_dram_timing(ctrl, &paramx , dimm_mask, meminfo); // dimm_mask will be stored to meminfo->dimm_mask
2572        if (dimm_mask < 0)
2573                goto hw_spd_err;
2574        
2575        order_dimms(ctrl, meminfo);
2576
2577        return;
2578 hw_spd_err:
2579        /* Unrecoverable error reading SPD data */
2580        printk(BIOS_ERR, "SPD error - reset\n");
2581        hard_reset();
2582        return;
2583}
2584
2585#define TIMEOUT_LOOPS 300000
2586
2587//#include "raminit_f_dqs.c"
2588
2589#if CONFIG_HW_MEM_HOLE_SIZEK != 0
2590u32 hoist_memory(int controllers, const struct mem_controller *ctrl,unsigned hole_startk, int i)
2591{
2592        int ii;
2593        u32 carry_over;
2594        u32 dev;
2595        u32 base, limit;
2596        u32 basek;
2597        u32 hoist;
2598        int j;
2599
2600        carry_over = (4*1024*1024) - hole_startk;
2601
2602        for(ii=controllers - 1;ii>i;ii--) {
2603                base  = pci_conf1_read_config32(ctrl[0].f1, 0x40 + (ii << 3));
2604                if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2605                        continue;
2606                }
2607                limit = pci_conf1_read_config32(ctrl[0].f1, 0x44 + (ii << 3));
2608                limit += (carry_over << 2 );
2609                base  += (carry_over << 2 );
2610                for(j = 0; j < controllers; j++) {      
2611                        pci_conf1_write_config32(ctrl[j].f1, 0x44 + (ii << 3), limit);
2612                        pci_conf1_write_config32(ctrl[j].f1, 0x40 + (ii << 3), base );
2613                }
2614        }
2615        limit = pci_conf1_read_config32(ctrl[0].f1, 0x44 + (i << 3));
2616        limit += (carry_over << 2);
2617        for(j = 0; j < controllers; j++) {
2618                pci_conf1_write_config32(ctrl[j].f1, 0x44 + (i << 3), limit);
2619        }
2620        dev = ctrl[i].f1;
2621        base  = pci_conf1_read_config32(dev, 0x40 + (i << 3));
2622        basek  = (base & 0xffff0000) >> 2;
2623        if(basek == hole_startk) {
2624                //don't need set memhole here, because hole off set will be 0, overflow
2625                //so need to change base reg instead, new basek will be 4*1024*1024
2626                base &= 0x0000ffff;
2627                base |= (4*1024*1024)<<2;
2628                for(j = 0; j < controllers; j++) {
2629                        pci_conf1_write_config32(ctrl[j].f1, 0x40 + (i<<3), base);
2630                }
2631        }
2632        else
2633        {
2634                hoist = /* hole start address */
2635                        ((hole_startk << 10) & 0xff000000) +
2636                        /* hole address to memory controller address */
2637                        (((basek + carry_over) >> 6) & 0x0000ff00) +
2638                        /* enable */
2639                        1;
2640                pci_conf1_write_config32(dev, 0xf0, hoist);
2641        }       
2642
2643        return carry_over;
2644}
2645
2646void set_hw_mem_hole(int controllers, const struct mem_controller *ctrl)
2647{
2648
2649        u32 hole_startk;
2650        int i;
2651
2652        hole_startk = 4*1024*1024 - CONFIG_HW_MEM_HOLE_SIZEK;
2653
2654#if HW_MEM_HOLE_SIZE_AUTO_INC == 1
2655        //We need to double check if the hole_startk is valid, if it is equal to basek, we need to decrease it some
2656        u32 basek_pri = 0;
2657        for(i=0; i<controllers; i++) {
2658                        u32 base;
2659                        u32 base_k;
2660                        base  = pci_conf1_read_config32(ctrl[0].f1, 0x40 + (i << 3));
2661                        if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2662                                continue;
2663                        }
2664                        base_k = (base & 0xffff0000) >> 2;
2665                        if(base_k == hole_startk) {
2666                                hole_startk -= (base_k - basek_pri)>>1; // decrease mem hole startk to make sure it is on middle of previous node
2667                                break; //only one hole
2668                        }
2669                        basek_pri = base_k;
2670        }
2671#endif
2672        //find node index that need do set hole
2673        for(i=0; i<controllers; i++) {
2674                        u32 base, limit;
2675                        unsigned base_k, limit_k;
2676                        base  = pci_conf1_read_config32(ctrl[0].f1, 0x40 + (i << 3));
2677                        if ((base & ((1<<1)|(1<<0))) != ((1<<1)|(1<<0))) {
2678                                continue;
2679                        }
2680                        limit = pci_conf1_read_config32(ctrl[0].f1, 0x44 + (i << 3));
2681                        base_k = (base & 0xffff0000) >> 2;
2682                        limit_k = ((limit + 0x00010000) & 0xffff0000) >> 2;
2683                        if ((base_k <= hole_startk) && (limit_k > hole_startk)) {
2684                                unsigned end_k;
2685                                hoist_memory(controllers, ctrl, hole_startk, i);
2686                                end_k = memory_end_k(ctrl, controllers);
2687                                set_top_mem(end_k, hole_startk);
2688                                break; //only one hole
2689                        }
2690        }
2691
2692}
2693
2694#endif
2695static void wait_all_core0_mem_trained(struct sys_info *sysinfo)
2696{
2697
2698        int i;
2699        u32 mask = 0;
2700        unsigned int needs_reset = 0;
2701        void hard_reset(void);
2702
2703        if(sysinfo->nodes == 1) return; // in case only one cpu installed       
2704
2705        for(i=1; i<sysinfo->nodes; i++) {
2706                /* Skip everything if I don't have any memory on this controller */
2707                if(sysinfo->mem_trained[i]==0x00) continue;
2708
2709                mask |= (1<<i);
2710
2711        }
2712
2713        i = 1;
2714        while(1) {
2715                if(mask & (1<<i)) {
2716                        if((sysinfo->mem_trained[i])!=0x80) {
2717                                mask &= ~(1<<i);
2718                        }
2719                }
2720
2721                if(!mask) break;
2722
2723                i++;
2724                i%=sysinfo->nodes;
2725        }
2726
2727        for(i=0; i<sysinfo->nodes; i++) {
2728                printk(BIOS_DEBUG, "mem_trained[%02x]=%02x\n", i, sysinfo->mem_trained[i]); 
2729                switch(sysinfo->mem_trained[i]) {
2730                case 0: //don't need train
2731                case 1: //trained
2732                        break;
2733                case 0x81: //recv1: fail
2734                case 0x82: //Pos :fail
2735                case 0x83: //recv2: fail
2736                        needs_reset = 1;
2737                        break;
2738                }
2739        }
2740        if(needs_reset) {
2741                printk(BIOS_DEBUG, "mem trained failed\n"); 
2742                hard_reset();
2743        }
2744
2745}
2746
2747void sdram_enable(int controllers, const struct mem_controller *ctrl, struct sys_info *sysinfo)
2748{
2749        int i;
2750        void dqs_timing(int controllers, const struct mem_controller *ctrl, struct sys_info *sysinfo);
2751        void memreset(int controllers, const struct mem_controller *ctrl);
2752
2753        /* Error if I don't have memory */
2754        if (memory_end_k(ctrl, controllers) == 0) {
2755                die("No memory\n");
2756        }
2757
2758        /* Before enabling memory start the memory clocks */
2759        for(i = 0; i < controllers; i++) {
2760                u32 dch;
2761                if (!sysinfo->ctrl_present[ i ])
2762                        continue;
2763                dch = pci_conf1_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2764
2765                // if no memory installed, disabled the interface
2766                if(sysinfo->meminfo[i].dimm_mask==0x00){
2767                        dch |= DCH_DisDramInterface;
2768                        pci_conf1_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2769
2770                }
2771                else {
2772                        dch |= DCH_MemClkFreqVal;
2773                        pci_conf1_write_config32(ctrl[i].f2, DRAM_CONFIG_HIGH, dch);
2774                        /* address timing and Output driver comp Control */
2775                        set_misc_timing(ctrl+i, sysinfo->meminfo+i );
2776                }
2777        }
2778
2779        /* We need to wait a mimmium of 20 MEMCLKS to enable the  InitDram */
2780        memreset(controllers, ctrl);
2781#if 1
2782        printk(BIOS_DEBUG, "prepare to InitDram:");
2783        for(i=0; i<10; i++) {
2784                printk(BIOS_DEBUG, "%08x\b\b\b\b\b\b\b\b", i);
2785        }
2786        printk(BIOS_DEBUG, "\n");
2787#endif
2788
2789        for(i = 0; i < controllers; i++) {
2790                u32 dcl, dch;
2791                if (!sysinfo->ctrl_present[ i ])
2792                        continue;
2793                /* Skip everything if I don't have any memory on this controller */
2794                dch = pci_conf1_read_config32(ctrl[i].f2, DRAM_CONFIG_HIGH);
2795                if (!(dch & DCH_MemClkFreqVal)) {
2796                        continue;
2797                }
2798
2799                /* ChipKill */
2800                dcl = pci_conf1_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2801                if (dcl & DCL_DimmEccEn) {
2802                        u32 mnc;
2803                        printk(BIOS_SPEW, "ECC enabled\n");
2804                        mnc = pci_conf1_read_config32(ctrl[i].f3, MCA_NB_CONFIG);
2805                        mnc |= MNC_ECC_EN;
2806                        if (dcl & DCL_Width128) {
2807                                mnc |= MNC_CHIPKILL_EN;
2808                        }
2809                        pci_conf1_write_config32(ctrl[i].f3, MCA_NB_CONFIG, mnc);
2810                }
2811
2812#if 0
2813                               /* Set the DqsRcvEnTrain bit */
2814                        dword = pci_conf1_read_config32(ctrl[i].f2, DRAM_CTRL);
2815                        dword |= DC_DqsRcvEnTrain;
2816                        pci_conf1_write_config32(ctrl[i].f2, DRAM_CTRL, dword);
2817#endif
2818
2819                pci_conf1_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2820                dcl |= DCL_InitDram;
2821                pci_conf1_write_config32(ctrl[i].f2, DRAM_CONFIG_LOW, dcl);
2822
2823        }
2824
2825        for(i = 0; i < controllers; i++) {
2826                u32 dcl, dcm;
2827                if (!sysinfo->ctrl_present[ i ])
2828                        continue;
2829                /* Skip everything if I don't have any memory on this controller */
2830                if(sysinfo->meminfo[i].dimm_mask==0x00) continue;
2831
2832                printk(BIOS_DEBUG, "Initializing memory: ");
2833                int loops = 0;
2834                do {
2835                        dcl = pci_conf1_read_config32(ctrl[i].f2, DRAM_CONFIG_LOW);
2836                        loops++;
2837                        if ((loops & 1023) == 0) {
2838                                printk(BIOS_DEBUG, ".");
2839                        }
2840                } while(((dcl & DCL_InitDram) != 0) && (loops < TIMEOUT_LOOPS));
2841                if (loops >= TIMEOUT_LOOPS) {
2842                        printk(BIOS_DEBUG, " failed\n");
2843                        continue;
2844                }
2845
2846                /* Wait until it is safe to touch memory */
2847                do {
2848                        dcm = pci_conf1_read_config32(ctrl[i].f2, DRAM_CTRL_MISC);
2849                } while(((dcm & DCM_MemClrStatus) == 0) /* || ((dcm & DCM_DramEnabled) == 0)*/ );
2850
2851                printk(BIOS_DEBUG, " done\n");
2852        }
2853
2854#if CONFIG_HW_MEM_HOLE_SIZEK != 0
2855         // init hw mem hole here
2856        /* DramHoleValid bit only can be set after MemClrStatus is set by Hardware */
2857        set_hw_mem_hole(controllers, ctrl);
2858#endif
2859        
2860        //store tom to sysinfo, and it will be used by dqs_timing
2861        {
2862                struct msr msr;
2863                //[1M, TOM)
2864                msr = rdmsr(TOP_MEM);
2865                sysinfo->tom_k = ((msr.hi<<24) | (msr.lo>>8))>>2;
2866
2867                //[4G, TOM2)
2868                msr = rdmsr(TOP_MEM2);
2869                sysinfo->tom2_k = ((msr.hi<<24)| (msr.lo>>8))>>2;
2870        }
2871
2872        for(i = 0; i < controllers; i++) {
2873                sysinfo->mem_trained[i] = 0;
2874
2875                if (!sysinfo->ctrl_present[ i ])
2876                        continue;
2877
2878                /* Skip everything if I don't have any memory on this controller */
2879                if(sysinfo->meminfo[i].dimm_mask==0x00) 
2880                        continue;
2881
2882                sysinfo->mem_trained[i] = 0x80; // mem need to be trained
2883        }
2884
2885
2886#if MEM_TRAIN_SEQ ==  0
2887        dqs_timing(controllers, ctrl, sysinfo);
2888#else
2889
2890   #if MEM_TRAIN_SEQ == 2
2891           //need to enable mtrr, so dqs training could access the test address
2892        setup_mtrr_dqs(sysinfo->tom_k, sysinfo->tom2_k);
2893   #endif
2894
2895        for(i = 0; i < controllers; i++) {
2896                /* Skip everything if I don't have any memory on this controller */
2897                if(sysinfo->mem_trained[i]!=0x80) 
2898                        continue;
2899
2900                dqs_timing(i, &ctrl[i], sysinfo);
2901
2902   #if MEM_TRAIN_SEQ == 1
2903                break; // only train the first node with ram
2904   #endif
2905        }
2906
2907   #if MEM_TRAIN_SEQ == 2
2908        clear_mtrr_dqs(sysinfo->tom2_k);
2909   #endif
2910
2911#endif
2912
2913#if MEM_TRAIN_SEQ != 1
2914        wait_all_core0_mem_trained(sysinfo);
2915#endif
2916
2917}
2918void fill_mem_ctrl(int controllers, struct mem_controller *ctrl_a, const u16 *spd_addr)
2919{
2920        int i; 
2921        int j;
2922        struct mem_controller *ctrl;
2923        for(i=0;i<controllers; i++) {
2924                ctrl = &ctrl_a[i];
2925                ctrl->node_id = i;
2926                ctrl->f0 = PCI_BDF(0, 0x18+i, 0);
2927                ctrl->f1 = PCI_BDF(0, 0x18+i, 1);
2928                ctrl->f2 = PCI_BDF(0, 0x18+i, 2);
2929                ctrl->f3 = PCI_BDF(0, 0x18+i, 3);
2930
2931                if(spd_addr == (void *)0) continue;
2932
2933                for(j=0;j<DIMM_SOCKETS;j++) {
2934                        ctrl->channel0[j] = spd_addr[(i*2+0)*DIMM_SOCKETS + j];
2935                        ctrl->channel1[j] = spd_addr[(i*2+1)*DIMM_SOCKETS + j];
2936                }
2937        }
2938}
2939
2940/**
2941 * initialize sdram. There was a "generic" function in v2 that was not really that useful. 
2942 * we break it out by northbridge now to accomodate the different peculiarities of 
2943 * different chipsets. 
2944 *@param controllers Number of controllers
2945 *@param ctrl array of memory controllers
2946 *@param sysinfo pointer to sysinfo struct. 
2947 */
2948void sdram_initialize(int controllers, const struct mem_controller *ctrl, struct sys_info *sysinfo)
2949{
2950        int i;
2951        /* Set the registers we can set once to reasonable values */
2952        for(i = 0; i < controllers; i++) {
2953                printk(BIOS_DEBUG, "Ram1.%d, ",i);
2954
2955                sdram_set_registers(ctrl + i , sysinfo);
2956        }
2957
2958        /* Now setup those things we can auto detect */
2959        for(i = 0; i < controllers; i++) {
2960                printk(BIOS_DEBUG, "Ram2.%d, ",i);
2961                sdram_set_spd_registers(ctrl + i , sysinfo);
2962
2963        }
2964
2965        /* Now that everything is setup enable the SDRAM.
2966         * Some chipsets do the work for us while on others 
2967         * we need to it by hand.
2968         */
2969        printk(BIOS_DEBUG, "Ram3\n");
2970
2971        sdram_enable(controllers, ctrl, sysinfo);
2972
2973        printk(BIOS_DEBUG, "Ram4\n");
2974
2975}
2976
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.