Fix build with CONFIG_DEBUG_RAM_SETUP on Intel 440BX, use printk().
[coreboot.git] / src / northbridge / intel / i440bx / raminit.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2008 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2010 Keith Hui <buurin@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 #include <spd.h>
23 #include <delay.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <arch/io.h>
27 #include <arch/romcc_io.h>
28 #include <device/pci_def.h>
29 #include <console/console.h>
30 #include "i440bx.h"
31 #include "raminit.h"
32
33 /*-----------------------------------------------------------------------------
34 Macros and definitions.
35 -----------------------------------------------------------------------------*/
36
37 #define NB PCI_DEV(0, 0, 0)
38
39 /* Debugging macros. */
40 #if CONFIG_DEBUG_RAM_SETUP
41 #include "lib/debug.c"
42 #define PRINT_DEBUG(x...)       printk(BIOS_DEBUG, x)
43 #define PRINT_DEBUG_HEX8(x)     PRINT_DEBUG("%02x", x)
44 #define PRINT_DEBUG_HEX16(x)    PRINT_DEBUG("%04x", x)
45 #define PRINT_DEBUG_HEX32(x)    PRINT_DEBUG("%08x", x)
46 #define DUMPNORTH()             dump_pci_device(NB)
47 #else
48 #define PRINT_DEBUG(x...)
49 #define PRINT_DEBUG_HEX8(x)
50 #define PRINT_DEBUG_HEX16(x)
51 #define PRINT_DEBUG_HEX32(x)
52 #define DUMPNORTH()
53 #endif
54
55 /* SDRAMC[7:5] - SDRAM Mode Select (SMS). */
56 #define RAM_COMMAND_NORMAL      0x0
57 #define RAM_COMMAND_NOP         0x1
58 #define RAM_COMMAND_PRECHARGE   0x2
59 #define RAM_COMMAND_MRS         0x3
60 #define RAM_COMMAND_CBR         0x4
61
62 /* Map the JEDEC SPD refresh rates (array index) to 440BX refresh rates as
63  * defined in DRAMC[2:0].
64  *
65  * [0] == Normal        15.625 us ->  15.6 us
66  * [1] == Reduced(.25X)    3.9 us ->   7.8 ns
67  * [2] == Reduced(.5X)     7.8 us ->   7.8 us
68  * [3] == Extended(2x)    31.3 us ->  31.2 us
69  * [4] == Extended(4x)    62.5 us ->  62.4 us
70  * [5] == Extended(8x)     125 us -> 124.8 us
71  */
72 static const uint32_t refresh_rate_map[] = {
73         1, 5, 5, 2, 3, 4
74 };
75
76 /* Table format: register, bitmask, value. */
77 static const u8 register_values[] = {
78         /* NBXCFG - NBX Configuration Register
79          * 0x50 - 0x53
80          *
81          * [31:24] SDRAM Row Without ECC
82          *         0 = ECC components are populated in this row
83          *         1 = ECC components are not populated in this row
84          * [23:19] Reserved
85          * [18:18] Host Bus Fast Data Ready Enable (HBFDRE)
86          *         Assertion of DRAM data on host bus occurs...
87          *         0 = ...one clock after sampling snoop results (default)
88          *         1 = ...on the same clock the snoop result is being sampled
89          *             (this mode is faster by one clock cycle)
90          * [17:17] ECC - EDO static Drive mode
91          *         0 = Normal mode (default)
92          *         1 = ECC signals are always driven
93          * [16:16] IDSEL_REDIRECT
94          *         0 = IDSEL1 is allocated to this bridge (default)
95          *         1 = IDSEL7 is allocated to this bridge
96          * [15:15] WSC# Handshake Disable
97          *         1 = Uni-processor mode
98          *         0 = Dual-processor mode with external IOAPIC (default)
99          * [14:14] Intel Reserved
100          * [13:12] Host/DRAM Frequency
101          *         00 = 100 MHz
102          *         01 = Reserved
103          *         10 = 66 MHz
104          *         11 = Reserved
105          * [11:11] AGP to PCI Access Enable
106          *         1 = Enable
107          *         0 = Disable
108          * [10:10] PCI Agent to Aperture Access Disable
109          *         1 = Disable
110          *         0 = Enable (default)
111          * [09:09] Aperture Access Global Enable
112          *         1 = Enable
113          *         0 = Disable
114          * [08:07] DRAM Data Integrity Mode (DDIM)
115          *         00 = Non-ECC
116          *         01 = EC-only
117          *         10 = ECC Mode
118          *         11 = ECC Mode with hardware scrubbing enabled
119          * [06:06] ECC Diagnostic Mode Enable (EDME)
120          *         1 = Enable
121          *         0 = Normal operation mode (default)
122          * [05:05] MDA Present (MDAP)
123          *         Works in conjunction with the VGA_EN bit.
124          *         VGA_EN MDAP
125          *           0     x   All VGA cycles are sent to PCI
126          *           1     0   All VGA cycles are sent to AGP
127          *           1     1   All VGA cycles are sent to AGP, except for
128          *                     cycles in the MDA range.
129          * [04:04] Reserved
130          * [03:03] USWC Write Post During I/O Bridge Access Enable (UWPIO)
131          *         1 = Enable
132          *         0 = Disable
133          * [02:02] In-Order Queue Depth (IOQD)
134          *         1 = In-order queue = maximum
135          *         0 = A7# is sampled asserted (i.e., 0)
136          * [01:00] Reserved
137          */
138         NBXCFG + 0, 0x00, 0x0c,
139         // TODO: Bit 15 should be 0 for multiprocessor boards
140         NBXCFG + 1, 0x00, 0x80,
141         NBXCFG + 2, 0x00, 0x00,
142         NBXCFG + 3, 0x00, 0xff,
143
144         /* DRAMC - DRAM Control Register
145          * 0x57
146          *
147          * [7:6] Reserved
148          * [5:5] Module Mode Configuration (MMCONFIG)
149          *       The combination of SDRAMPWR and this bit (which is set by an
150          *       external strapping option) determine how CKE works.
151          *       SDRAMPWR MMCONFIG
152          *       0        0         = 3 DIMM, CKE0[5:0] driven
153          *       X        1         = 3 DIMM, CKE0 only
154          *       1        0         = 4 DIMM, GCKE only
155          * [4:3] DRAM Type (DT)
156          *       00 = EDO
157          *       01 = SDRAM
158          *       10 = Registered SDRAM
159          *       11 = Reserved
160          *       Note: EDO, SDRAM and Registered SDRAM cannot be mixed.
161          * [2:0] DRAM Refresh Rate (DRR)
162          *       000 = Refresh disabled
163          *       001 = 15.6 us
164          *       010 = 31.2 us
165          *       011 = 62.4 us
166          *       100 = 124.8 us
167          *       101 = 249.6 us
168          *       110 = Reserved
169          *       111 = Reserved
170          */
171         /* Choose SDRAM (not registered), and disable refresh for now. */
172         DRAMC, 0x00, 0x08,
173
174         /*
175          * PAM[6:0] - Programmable Attribute Map Registers
176          * 0x59 - 0x5f
177          *
178          * 0x59 [3:0] Reserved
179          * 0x59 [5:4] 0xF0000 - 0xFFFFF BIOS area
180          * 0x5a [1:0] 0xC0000 - 0xC3FFF ISA add-on BIOS
181          * 0x5a [5:4] 0xC4000 - 0xC7FFF ISA add-on BIOS
182          * 0x5b [1:0] 0xC8000 - 0xCBFFF ISA add-on BIOS
183          * 0x5b [5:4] 0xCC000 - 0xCFFFF ISA add-on BIOS
184          * 0x5c [1:0] 0xD0000 - 0xD3FFF ISA add-on BIOS
185          * 0x5c [5:4] 0xD4000 - 0xD7FFF ISA add-on BIOS
186          * 0x5d [1:0] 0xD8000 - 0xDBFFF ISA add-on BIOS
187          * 0x5d [5:4] 0xDC000 - 0xDFFFF ISA add-on BIOS
188          * 0x5e [1:0] 0xE0000 - 0xE3FFF BIOS entension
189          * 0x5e [5:4] 0xE4000 - 0xE7FFF BIOS entension
190          * 0x5f [1:0] 0xE8000 - 0xEBFFF BIOS entension
191          * 0x5f [5:4] 0xEC000 - 0xEFFFF BIOS entension
192          *
193          * Bit assignment:
194          * 00 = DRAM Disabled (all access goes to memory mapped I/O space)
195          * 01 = Read Only (Reads to DRAM, writes to memory mapped I/O space)
196          * 10 = Write Only (Writes to DRAM, reads to memory mapped I/O space)
197          * 11 = Read/Write (all access goes to DRAM)
198          */
199
200         /*
201          * Map all legacy regions to RAM (read/write). This is required if
202          * you want to use the RAM area from 768 KB - 1 MB. If the PAM
203          * registers are not set here appropriately, the RAM in that region
204          * will not be accessible, thus a RAM check of it will also fail.
205          *
206          * TODO: This was set in sdram_set_spd_registers().
207          * Test if it still works when set here.
208          */
209         PAM0, 0x00, 0x30,
210         PAM1, 0x00, 0x33,
211         PAM2, 0x00, 0x33,
212         PAM3, 0x00, 0x33,
213         PAM4, 0x00, 0x33,
214         PAM5, 0x00, 0x33,
215         PAM6, 0x00, 0x33,
216
217         /* DRB[0:7] - DRAM Row Boundary Registers
218          * 0x60 - 0x67
219          *
220          * An array of 8 byte registers, which hold the ending memory address
221          * assigned to each pair of DIMMs, in 8MB granularity.
222          *
223          * 0x60 DRB0 = Total memory in row0 (in 8 MB)
224          * 0x61 DRB1 = Total memory in row0+1 (in 8 MB)
225          * 0x62 DRB2 = Total memory in row0+1+2 (in 8 MB)
226          * 0x63 DRB3 = Total memory in row0+1+2+3 (in 8 MB)
227          * 0x64 DRB4 = Total memory in row0+1+2+3+4 (in 8 MB)
228          * 0x65 DRB5 = Total memory in row0+1+2+3+4+5 (in 8 MB)
229          * 0x66 DRB6 = Total memory in row0+1+2+3+4+5+6 (in 8 MB)
230          * 0x67 DRB7 = Total memory in row0+1+2+3+4+5+6+7 (in 8 MB)
231          */
232         /* Set the DRBs to zero for now, this will be fixed later. */
233         DRB0, 0x00, 0x00,
234         DRB1, 0x00, 0x00,
235         DRB2, 0x00, 0x00,
236         DRB3, 0x00, 0x00,
237         DRB4, 0x00, 0x00,
238         DRB5, 0x00, 0x00,
239         DRB6, 0x00, 0x00,
240         DRB7, 0x00, 0x00,
241
242         /* FDHC - Fixed DRAM Hole Control Register
243          * 0x68
244          *
245          * Controls two fixed DRAM holes: 512 KB - 640 KB and 15 MB - 16 MB.
246          *
247          * [7:6] Hole Enable (HEN)
248          *       00 = None
249          *       01 = 512 KB - 640 KB (128 KB)
250          *       10 = 15 MB - 16 MB (1 MB)
251          *       11 = Reserved
252          * [5:0] Reserved
253          */
254         /* No memory holes. */
255         FDHC, 0x00, 0x00,
256
257         /* RPS - SDRAM Row Page Size Register
258          * 0x74 - 0x75
259          *
260          * Sets the row page size for SDRAM. For EDO memory, the page
261          * size is fixed at 2 KB.
262          *
263          * Bits[1:0] Page Size
264          * 00        2 KB
265          * 01        4 KB
266          * 10        8 KB
267          * 11        Reserved
268          * 
269          * RPS bits Corresponding DRB register
270          * [01:00]  DRB[0], row 0
271          * [03:02]  DRB[1], row 1
272          * [05:04]  DRB[2], row 2
273          * [07:06]  DRB[3], row 3
274          * [09:08]  DRB[4], row 4
275          * [11:10]  DRB[5], row 5
276          * [13:12]  DRB[6], row 6
277          * [15:14]  DRB[7], row 7
278          */
279         /* Power on defaults to 2KB. Will be set later. */
280         // RPS + 0, 0x00, 0x00,
281         // RPS + 1, 0x00, 0x00,
282
283         /* SDRAMC - SDRAM Control Register
284          * 0x76 - 0x77
285          *
286          * [15:10] Reserved
287          * [09:08] Idle/Pipeline DRAM Leadoff Timing (IPDLT)
288          *         00 = Illegal
289          *         01 = Add a clock delay to the lead-off clock count
290          *         1x = Illegal
291          * [07:05] SDRAM Mode Select (SMS)
292          *         000 = Normal SDRAM Operation (default)
293          *         001 = NOP Command Enable
294          *         010 = All Banks Precharge Enable
295          *         011 = Mode Register Set Enable
296          *         100 = CBR Enable
297          *         101 = Reserved
298          *         110 = Reserved
299          *         111 = Reserved
300          * [04:04] SDRAMPWR
301          *         0 = 3 DIMM configuration
302          *         1 = 4 DIMM configuration
303          * [03:03] Leadoff Command Timing (LCT)
304          *         0 = 4 CS# Clock
305          *         1 = 3 CS# Clock
306          * [02:02] CAS# Latency (CL)
307          *         0 = 3 DCLK CAS# latency
308          *         1 = 2 DCLK CAS# latency
309          * [01:01] SDRAM RAS# to CAS# Delay (SRCD)
310          *         0 = 3 clocks between a row activate and a read or write cmd.
311          *         1 = 2 clocks between a row activate and a read or write cmd.
312          * [00:00] SDRAM RAS# Precharge (SRP)
313          *         0 = 3 clocks of RAS# precharge
314          *         1 = 2 clocks of RAS# precharge
315          */
316 #if CONFIG_SDRAMPWR_4DIMM
317         SDRAMC + 0, 0x00, 0x10, /* The board has 4 DIMM slots. */
318 #else
319         SDRAMC + 0, 0x00, 0x00, /* The board has 3 DIMM slots. */
320 #endif
321         SDRAMC + 1, 0x00, 0x00,
322
323         /* PGPOL - Paging Policy Register
324          * 0x78 - 0x79
325          *
326          * [15:08] Banks per Row (BPR)
327          *         Each bit in this field corresponds to one row of the memory
328          *         array. Bit 15 corresponds to row 7 while bit 8 corresponds
329          *         to row 0. Bits for empty rows are "don't care".
330          *         0 = 2 banks
331          *         1 = 4 banks
332          * [07:05] Reserved
333          * [04:04] Intel Reserved
334          * [03:00] DRAM Idle Timer (DIT)
335          *         0000 = 0 clocks
336          *         0001 = 2 clocks
337          *         0010 = 4 clocks
338          *         0011 = 8 clocks
339          *         0100 = 10 clocks
340          *         0101 = 12 clocks
341          *         0110 = 16 clocks
342          *         0111 = 32 clocks
343          *         1xxx = Infinite (pages are not closed for idle condition)
344          */
345         PGPOL + 0, 0x00, 0x00,
346         PGPOL + 1, 0x00, 0xff,
347
348         /* PMCR - Power Management Control Register
349          * 0x7a
350          *
351          * [07:07] Power Down SDRAM Enable (PDSE)
352          *         1 = Enable
353          *         0 = Disable
354          * [06:06] ACPI Control Register Enable (SCRE)
355          *         1 = Enable
356          *         0 = Disable (default)
357          * [05:05] Suspend Refresh Type (SRT)
358          *         1 = Self refresh mode
359          *         0 = CBR fresh mode
360          * [04:04] Normal Refresh Enable (NREF_EN)
361          *         1 = Enable
362          *         0 = Disable
363          * [03:03] Quick Start Mode (QSTART)
364          *         1 = Quick start mode for the processor is enabled
365          * [02:02] Gated Clock Enable (GCLKEN)
366          *         1 = Enable
367          *         0 = Disable
368          * [01:01] AGP Disable (AGP_DIS)
369          *         1 = Disable
370          *         0 = Enable
371          * [00:00] CPU reset without PCIRST enable (CRst_En)
372          *         1 = Enable
373          *         0 = Disable
374          */
375         /* Enable normal refresh and the gated clock. */
376         // TODO: Only do this later?
377         // PMCR, 0x00, 0x14,
378         PMCR, 0x00, 0x00,
379
380         /* Enable SCRR.SRRAEN and let BX choose the SRR. */
381         SCRR + 1, 0x00, 0x10,
382 };
383
384 /*-----------------------------------------------------------------------------
385 SDRAM configuration functions.
386 -----------------------------------------------------------------------------*/
387
388 /**
389  * Send the specified RAM command to all DIMMs.
390  *
391  * @param command The RAM command to send to the DIMM(s).
392  */
393 static void do_ram_command(u32 command)
394 {
395         int i, caslatency;
396         u8 dimm_start, dimm_end;
397         u16 reg16;
398         u32 addr, addr_offset;
399
400         /* Configure the RAM command. */
401         reg16 = pci_read_config16(NB, SDRAMC);
402         reg16 &= 0xff1f;                /* Clear bits 7-5. */
403         reg16 |= (u16) (command << 5);  /* Write command into bits 7-5. */
404         pci_write_config16(NB, SDRAMC, reg16);
405
406         /*
407          * RAM_COMMAND_NORMAL affects only the memory controller and
408          * doesn't need to be "sent" to the DIMMs.
409          */
410         if (command == RAM_COMMAND_NORMAL)
411                 return;
412
413         /* Send the RAM command to each row of memory. */
414         dimm_start = 0;
415         for (i = 0; i < (DIMM_SOCKETS * 2); i++) {
416                 addr_offset = 0;
417                 caslatency = 3; /* TODO: Dynamically get CAS latency later. */
418                 if (command == RAM_COMMAND_MRS) {
419                         /*
420                          * MAA[12:11,9:0] must be inverted when sent to DIMM
421                          * 2 or 3 (no inversion if sent to DIMM 0 or 1).
422                          */
423                         if ((i >= 0 && i <= 3) && caslatency == 3)
424                                 addr_offset = 0x1d0;
425                         if ((i >= 4 && i <= 7) && caslatency == 3)
426                                 addr_offset = 0x1e28;
427                         if ((i >= 0 && i <= 3) && caslatency == 2)
428                                 addr_offset = 0x150;
429                         if ((i >= 4 && i <= 7) && caslatency == 2)
430                                 addr_offset = 0x1ea8;
431                 }
432
433                 dimm_end = pci_read_config8(NB, DRB + i);
434
435                 addr = (dimm_start * 8 * 1024 * 1024) + addr_offset;
436                 if (dimm_end > dimm_start) {
437 #if 0
438                         PRINT_DEBUG("    Sending RAM command 0x");
439                         PRINT_DEBUG_HEX16(reg16);
440                         PRINT_DEBUG(" to 0x");
441                         PRINT_DEBUG_HEX32(addr);
442                         PRINT_DEBUG("\n");
443 #endif
444
445                         read32(addr);
446                 }
447
448                 /* Set the start of the next DIMM. */
449                 dimm_start = dimm_end;
450         }
451 }
452
453 static void set_dram_buffer_strength(void)
454 {
455         /* To give some breathing room for romcc,
456          * mbsc0 doubles as drb
457          * mbsc1 doubles as drb1
458          * mbfs0 doubles as i and reg
459          */
460         uint8_t mbsc0,mbsc1,mbsc3,mbsc4,mbfs0,mbfs2,fsb;
461
462         /* Tally how many rows between rows 0-3 and rows 4-7 are populated.
463          * This determines how to program MBFS and MBSC.
464          */
465         uint8_t dimm03 = 0;
466         uint8_t dimm47 = 0;
467
468         mbsc0 = 0;
469         for (mbfs0 = DRB0; mbfs0 <= DRB7; mbfs0++) {
470                 mbsc1 = pci_read_config8(NB, mbfs0);
471                 if (mbsc0 != mbsc1) {
472                         if (mbfs0 <= DRB3) {
473                                 dimm03++;
474                         } else {
475                                 dimm47++;
476                         }
477                         mbsc0 = mbsc1;
478                 }
479         }
480
481         /* Algorithm bitmap for programming MBSC[39:0] and MBFS[23:0].
482          *
483          * The 440BX datasheet says buffer frequency is independent from bus
484          * frequency and mismatch both ways are possible. This is how it is
485          * programmed in the ASUS P2B-LS mainboard.
486          *
487          * There are four main conditions to check when programming DRAM buffer
488          * frequency and strength:
489          *
490          * a: >2 rows populated across DIMM0,1
491          * b: >2 rows populated across DIMM2,3
492          * c: >4 rows populated across all DIMM slots
493          * and either one of:
494          * 1: NBXCFG[13] strapped as 100MHz, or
495          * 6: NBXCFG[13] strapped as 66MHz
496          *
497          * CKE0/FENA ----------------------------------------------------------+
498          * CKE1/GCKE -------------------[    MBFS    ]------------------------+|
499          * DQMA/CASA[764320]# ----------[ 0 = 66MHz  ]-----------------------+||
500          * DQMB1/CASB1# ----------------[ 1 = 100MHz ]----------------------+|||
501          * DQMB5/CASB5# ---------------------------------------------------+||||
502          * DQMA1/CASA1# --------------------------------------------------+|||||
503          * DQMA5/CASA5# -------------------------------------------------+||||||
504          * CSA0-5#,CSB0-5# ----------------------------------------++++++|||||||
505          * CSA6#/CKE2# -------------------------------------------+|||||||||||||
506          * CSB6#/CKE4# ------------------------------------------+||||||||||||||
507          * CSA7#/CKE3# -----------------------------------------+|||||||||||||||
508          * CSB7#/CKE5# ----------------------------------------+||||||||||||||||
509          * MECC[7:0] #2/#1 (100MHz) -------------------------++|||||||||||||||||
510          * MD[63:0] #2/#1 (100MHz) ------------------------++|||||||||||||||||||
511          * MAB[12:11,9:0]#,MAB[13,10],WEB#,SRASB#,SCASB# -+|||||||||||||||||||||
512          * MAA[13:0],WEA#,SRASA#,SCASA# -----------------+||||||||||||||||||||||
513          * Reserved ------------------------------------+|||||||||||||||||||||||
514          *                                              ||||||||||||||||||||||||
515          *   3        32        21        10        0 * 2  21        10        0
516          *   9876543210987654321098765432109876543210 * 321098765432109876543210
517          * a 10------------------------1010---------- * -1---------------11-----  a
518          *!a 11------------------------1111---------- * -0---------------00----- !a
519          * b --10--------------------------1010------ * --1----------------11---  b
520          *!b --11--------------------------1111------ * --0----------------00--- !b
521          * c ----------------------------------1100-- * ----------------------1-  c
522          *!c ----------------------------------1011-- * ----------------------0- !c
523          * 1 ----1010101000000000000000------------00 * ---11111111111111----1-0  1
524          * 6 ----000000000000000000000010101010----00 * ---1111111111111100000-0  6
525          *   | | | | | | | | | | ||||||| | | | | | |
526          *   | | | | | | | | | | ||||||| | | | | | +- CKE0/FENA
527          *   | | | | | | | | | | ||||||| | | | | +--- CKE1/GCKE
528          *   | | | | | | | | | | ||||||| | | | +----- DQMA/CASA[764320]#
529          *   | | | | | | | | | | ||||||| | | +------- DQMB1/CASB1#
530          *   | | | | | | | | | | ||||||| | +--------- DQMB5/CASB5#
531          *   | | | | | | | | | | ||||||| +----------- DQMA1/CASA1#
532          *   | | | | | | | | | | ||||||+------------- DQMA5/CASA5#
533          *   | | | | | | | | | | ++++++-------------- CSA0-5#,CSB0-5# [ 0=1x;1=2x ]
534          *   | | | | | | | | | +--------------------- CSA6#/CKE2#
535          *   | | | | | | | | +---[    MBSC    ]------ CSB6#/CKE4#
536          *   | | | | | | | +-----[ 00 = 1x    ]------ CSA7#/CKE3#
537          *   | | | | | | +-------[ 01 invalid ]------ CSB7#/CKE5#
538          *   | | | | | +---------[ 10 = 2x    ]------ MECC[7:0] #1 (2x)
539          *   | | | | +-----------[ 11 = 3x    ]------ MECC[7:0] #2 (2x)
540          *   | | | +--------------------------------- MD[63:0] #1 (2x)
541          *   | | +----------------------------------- MD[63:0] #2 (2x)
542          *   | +------------------------------------- MAB[12:11,9:0]#,MAB[13,10],WEB#,SRASB#,SCASB#
543          *   +--------------------------------------- MAA[13:0],WEA#,SRASA#,SCASA#
544          * MBSC[47:40] and MBFS[23] are reserved.
545          *
546          * This algorithm is checked against the ASUS P2B-LS (which has
547          * 4 DIMM slots) factory BIOS.
548          * Therefore it assumes a board with 4 slots, and will need testing
549          * on boards with 3 DIMM slots.
550          */
551
552         mbsc0 = 0x80;
553         mbsc1 = 0x2a;
554         mbfs2 = 0x1f;
555         if (pci_read_config8(NB, NBXCFG + 1) & 0x30) {
556                 fsb = 66;
557                 mbsc3 = 0x00;
558                 mbsc4 = 0x00;
559                 mbfs0 = 0x80;
560         } else {
561                 fsb = 100;
562                 mbsc3 = 0xa0;
563                 mbsc4 = 0x0a;
564                 mbfs0 = 0x84;
565         }
566
567         if (dimm03 > 2) {
568                 mbsc4 = mbsc4 | 0x80;
569                 mbsc1 = mbsc1 | 0x28;
570                 mbfs2 = mbfs2 | 0x40;
571                 mbfs0 = mbfs0 | 0x60;
572         } else {
573                 mbsc4 = mbsc4 | 0xc0;
574                 if (fsb == 100) {
575                         mbsc1 = mbsc1 | 0x3c;
576                 }
577         }
578         if (dimm47 > 2) {
579                 mbsc4 = mbsc4 | 0x20;
580                 mbsc1 = mbsc1 | 0x02;
581                 mbsc0 = mbsc0 | 0x80;
582                 mbfs2 = mbfs2 | 0x20;
583                 mbfs0 = mbfs0 | 0x18;
584         } else {
585                 mbsc4 = mbsc4 | 0x30;
586                 if (fsb == 100) {
587                         mbsc1 = mbsc1 | 0x03;
588                         mbsc0 = mbsc0 | 0xc0;
589                 }
590         }
591         if ((dimm03 + dimm47) > 4) {
592                 mbsc0 = mbsc0 | 0x30;
593                 mbfs0 = mbfs0 | 0x02;
594         } else {
595                 mbsc0 = mbsc0 | 0x2c;
596         }
597
598         pci_write_config8(NB, MBSC + 0, mbsc0);
599         pci_write_config8(NB, MBSC + 1, mbsc1);
600         pci_write_config8(NB, MBSC + 2, 0x00);
601         pci_write_config8(NB, MBSC + 3, mbsc3);
602         pci_write_config8(NB, MBSC + 4, mbsc4);
603         pci_write_config8(NB, MBFS + 0, mbfs0);
604         pci_write_config8(NB, MBFS + 1, 0xff);
605         pci_write_config8(NB, MBFS + 2, mbfs2);
606 }
607
608 /*-----------------------------------------------------------------------------
609 DIMM-independant configuration functions.
610 -----------------------------------------------------------------------------*/
611
612 static void spd_enable_refresh(void)
613 {
614         int i, value;
615         uint8_t reg;
616
617         reg = pci_read_config8(NB, DRAMC);
618
619         for (i = 0; i < DIMM_SOCKETS; i++) {
620                 value = spd_read_byte(DIMM0 + i, SPD_REFRESH);
621                 if (value < 0)
622                         continue;
623                 reg = (reg & 0xf8) | refresh_rate_map[(value & 0x7f)];
624
625                 PRINT_DEBUG("    Enabling refresh (DRAMC = 0x%02x) for DIMM %02x\n", reg, i);
626         }
627
628         pci_write_config8(NB, DRAMC, reg);
629 }
630
631 /*-----------------------------------------------------------------------------
632 Public interface.
633 -----------------------------------------------------------------------------*/
634
635 void sdram_set_registers(void)
636 {
637         int i, max;
638         uint8_t reg;
639
640         PRINT_DEBUG("Northbridge prior to SDRAM init:\n");
641         DUMPNORTH();
642
643         max = ARRAY_SIZE(register_values);
644
645         /* Set registers as specified in the register_values[] array. */
646         for (i = 0; i < max; i += 3) {
647                 reg = pci_read_config8(NB, register_values[i]);
648                 reg &= register_values[i + 1];
649                 reg |= register_values[i + 2] & ~(register_values[i + 1]);
650                 pci_write_config8(NB, register_values[i], reg);
651 #if 0
652                 PRINT_DEBUG("    Set register 0x");
653                 PRINT_DEBUG_HEX8(register_values[i]);
654                 PRINT_DEBUG(" to 0x");
655                 PRINT_DEBUG_HEX8(reg);
656                 PRINT_DEBUG("\n");
657 #endif
658         }
659 }
660
661 struct dimm_size {
662         u32 side1;
663         u32 side2;
664 };
665
666 static struct dimm_size spd_get_dimm_size(unsigned int device)
667 {
668         struct dimm_size sz;
669         int i, module_density, dimm_banks;
670         sz.side1 = 0;
671         module_density = spd_read_byte(device, SPD_DENSITY_OF_EACH_ROW_ON_MODULE);
672         dimm_banks = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
673
674         /* Find the size of side1. */
675         /* Find the larger value. The larger value is always side1. */
676         for (i = 512; i >= 0; i >>= 1) {
677                 if ((module_density & i) == i) {
678                         sz.side1 = i;
679                         break;
680                 }
681         }
682
683         /* Set to 0 in case it's single sided. */
684         sz.side2 = 0;
685
686         /* Test if it's a dual-sided DIMM. */
687         if (dimm_banks > 1) {
688                 /* Test if there's a second value. If so it's asymmetrical. */
689                 if (module_density != i) {
690                         /*
691                          * Find second value, picking up where we left off.
692                          * i >>= 1 done initially to make sure we don't get
693                          * the same value again.
694                          */
695                         for (i >>= 1; i >= 0; i >>= 1) {
696                                 if (module_density == (sz.side1 | i)) {
697                                         sz.side2 = i;
698                                         break;
699                                 }
700                         }
701                         /* If not, it's symmetrical. */
702                 } else {
703                         sz.side2 = sz.side1;
704                 }
705         }
706
707         /*
708          * SPD byte 31 is the memory size divided by 4 so we
709          * need to muliply by 4 to get the total size.
710          */
711         sz.side1 *= 4;
712         sz.side2 *= 4;
713
714         /* It is possible to partially use larger then supported
715          * modules by setting them to a supported size.
716          */
717         if(sz.side1 > 128) {
718                 PRINT_DEBUG("Side1 was %dMB but only 128MB will be used.\n",
719                         sz.side1);
720                 sz.side1 = 128;
721
722                 if(sz.side2 > 128) {
723                         PRINT_DEBUG("Side2 was %dMB but only 128MB will be used.\n",
724                                 sz.side2);
725                         sz.side2 = 128;
726                 }
727         }
728
729         return sz;
730 }
731 /*
732  * Sets DRAM attributes one DIMM at a time, based on SPD data.
733  * Northbridge settings that are set: NBXCFG[31:24], DRB0-DRB7, RPS, DRAMC.
734  */
735 static void set_dram_row_attributes(void)
736 {
737         int i, dra, drb, col, width, value, rps, edosd, ecc, nbxecc;
738         u8 bpr; /* Top 8 bits of PGPOL */
739
740         edosd = 0;
741         rps = 0;
742         drb = 0;
743         bpr = 0;
744         nbxecc = 0xff;
745
746         for (i = 0; i < DIMM_SOCKETS; i++) {
747                 unsigned int device;
748                 device = DIMM0 + i;
749                 bpr >>= 2;
750
751                 /* First check if a DIMM is actually present. */
752                 value = spd_read_byte(device, SPD_MEMORY_TYPE);
753                 /* This is 440BX! We do EDO too! */
754                 if (value == SPD_MEMORY_TYPE_EDO
755                         || value == SPD_MEMORY_TYPE_SDRAM) {
756
757                         if (value == SPD_MEMORY_TYPE_EDO) {
758                                 edosd |= 0x02;
759                         } else if (value == SPD_MEMORY_TYPE_SDRAM) {
760                                 edosd |= 0x04;
761                         }
762                         PRINT_DEBUG("Found DIMM in slot %d\n", i);
763
764                         if (edosd == 0x06) {
765                                 print_err("Mixing EDO/SDRAM unsupported!\n");
766                                 die("HALT\n");
767                         }
768
769                         /* "DRA" is our RPS for the two rows on this DIMM. */
770                         dra = 0;
771
772                         /* Columns */
773                         col = spd_read_byte(device, SPD_NUM_COLUMNS);
774
775                         /*
776                          * Is this an ECC DIMM? Actually will be a 2 if so.
777                          * TODO: Other register than NBXCFG also needs this
778                          * ECC information.
779                          */
780                         ecc = spd_read_byte(device, SPD_DIMM_CONFIG_TYPE);
781
782                         /* Data width */
783                         width = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_LSB);
784
785                         /* Exclude error checking data width from page size calculations */
786                         if (ecc) {
787                                 value = spd_read_byte(device,
788                                         SPD_ERROR_CHECKING_SDRAM_WIDTH);
789                                 width -= value;
790                                 /* ### ECC */
791                                 /* Clear top 2 bits to help set up NBXCFG. */
792                                 ecc &= 0x3f;
793                         } else {
794                                 /* Without ECC, top 2 bits should be 11. */
795                                 ecc |= 0xc0;
796                         }
797
798                         /* Calculate page size in bits. */
799                         value = ((1 << col) * width);
800
801                         /* Convert to KB. */
802                         dra = (value >> 13);
803
804                         /* Number of banks of DIMM (single or double sided). */
805                         value = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
806
807                         /* Once we have dra, col is done and can be reused.
808                          * So it's reused for number of banks.
809                          */
810                         col = spd_read_byte(device, SPD_NUM_BANKS_PER_SDRAM);
811
812                         if (value == 1) {
813                                 /*
814                                  * Second bank of 1-bank DIMMs "doesn't have
815                                  * ECC" - or anything.
816                                  */
817                                 ecc |= 0x80;
818                                 if (dra == 2) {
819                                         dra = 0x0; /* 2KB */
820                                 } else if (dra == 4) {
821                                         dra = 0x1; /* 4KB */
822                                 } else if (dra == 8) {
823                                         dra = 0x2; /* 8KB */
824                                 } else if (dra >= 16) {
825                                         /* Page sizes larger than supported are
826                                          * set to 8KB to use module partially.
827                                          */
828                                         PRINT_DEBUG("Page size forced to 8KB.\n");
829                                         dra = 0x2; /* 8KB */
830                                 } else {
831                                         dra = -1;
832                                 }
833                                 /*
834                                  * Sets a flag in PGPOL[BPR] if this DIMM has
835                                  * 4 banks per row.
836                                  */
837                                 if (col == 4)
838                                         bpr |= 0x40;
839                         } else if (value == 2) {
840                                 if (dra == 2) {
841                                         dra = 0x0; /* 2KB */
842                                 } else if (dra == 4) {
843                                         dra = 0x05; /* 4KB */
844                                 } else if (dra == 8) {
845                                         dra = 0x0a; /* 8KB */
846                                 } else if (dra >= 16) {
847                                         /* Ditto */
848                                         PRINT_DEBUG("Page size forced to 8KB.\n");
849                                         dra = 0x0a; /* 8KB */
850                                 } else {
851                                         dra = -1;
852                                 }
853                                 /* Ditto */
854                                 if (col == 4)
855                                         bpr |= 0xc0;
856                         } else {
857                                 print_err("# of banks of DIMM unsupported!\n");
858                                 die("HALT\n");
859                         }
860                         if (dra == -1) {
861                                 print_err("Page size not supported\n");
862                                 die("HALT\n");
863                         }
864
865                         /*
866                          * 440BX supports asymmetrical dual-sided DIMMs,
867                          * but can't handle DIMMs smaller than 8MB per
868                          * side.
869                          */
870                         struct dimm_size sz = spd_get_dimm_size(device);
871                         if ((sz.side1 < 8)) {
872                                 print_err("DIMMs smaller than 8MB per side\n"
873                                           "are not supported on this NB.\n");
874                                 die("HALT\n");
875                         }
876
877                         /* Divide size by 8 to set up the DRB registers. */
878                         drb += (sz.side1 / 8);
879
880                         /*
881                          * Build the DRB for the next row in MSB so it gets
882                          * placed in DRB[n+1] where it belongs when written
883                          * as a 16-bit word.
884                          */
885                         drb &= 0xff;
886                         drb |= (drb + (sz.side2 / 8)) << 8;
887                 } else {
888 #if 0
889                         PRINT_DEBUG("No DIMM found in slot ");
890                         PRINT_DEBUG_HEX8(i);
891                         PRINT_DEBUG("\n");
892 #endif
893
894                         /* If there's no DIMM in the slot, set dra to 0x00. */
895                         dra = 0x00;
896                         ecc = 0xc0;
897                         /* Still have to propagate DRB over. */
898                         drb &= 0xff;
899                         drb |= (drb << 8);
900                 }
901
902                 pci_write_config16(NB, DRB + (2 * i), drb);
903 #if 0
904                 PRINT_DEBUG("DRB has been set to 0x");
905                 PRINT_DEBUG_HEX16(drb);
906                 PRINT_DEBUG("\n");
907 #endif
908
909                 /* Brings the upper DRB back down to be base for
910                  * DRB calculations for the next two rows.
911                  */
912                 drb >>= 8;
913
914                 rps |= (dra & 0x0f) << (i * 4);
915                 nbxecc = (nbxecc >> 2) | (ecc & 0xc0);
916         }
917
918         /* Set paging policy register. */
919         pci_write_config8(NB, PGPOL + 1, bpr);
920         PRINT_DEBUG("PGPOL[BPR] has been set to 0x%02x\n", bpr);
921
922         /* Set DRAM row page size register. */
923         pci_write_config16(NB, RPS, rps);
924         PRINT_DEBUG("RPS has been set to 0x%04x\n", rps);
925
926         /* ### ECC */
927         pci_write_config8(NB, NBXCFG + 3, nbxecc);
928         PRINT_DEBUG("NBXECC[31:24] has been set to 0x%02x\n", nbxecc);
929
930         /* Set DRAMC[4:3] to proper memory type (EDO/SDRAM).
931          * TODO: Registered SDRAM support.
932          */
933         edosd &= 0x07;
934         if (edosd & 0x02) {
935                 edosd |= 0x00;
936         } else if (edosd & 0x04) {
937                 edosd |= 0x08;
938         }
939         edosd &= 0x18;
940
941         /* edosd is now in the form needed for DRAMC[4:3]. */
942         value = pci_read_config8(NB, DRAMC) & 0xe7;
943         value |= edosd;
944         pci_write_config8(NB, DRAMC, value);
945         PRINT_DEBUG("DRAMC has been set to 0x%02x\n", value);
946 }
947
948 void sdram_set_spd_registers(void)
949 {
950         /* Setup DRAM row boundary registers and other attributes. */
951         set_dram_row_attributes();
952
953         /* Setup DRAM buffer strength. */
954         set_dram_buffer_strength();
955
956         /* TODO: Set PMCR? */
957         // pci_write_config8(NB, PMCR, 0x14);
958         pci_write_config8(NB, PMCR, 0x10);
959
960         /* TODO: This is for EDO memory only. */
961         pci_write_config8(NB, DRAMT, 0x03);
962 }
963
964 void sdram_enable(void)
965 {
966         int i;
967
968         /* 0. Wait until power/voltages and clocks are stable (200us). */
969         udelay(200);
970
971         /* 1. Apply NOP. Wait 200 clock cycles (200us should do). */
972         PRINT_DEBUG("RAM Enable 1: Apply NOP\n");
973         do_ram_command(RAM_COMMAND_NOP);
974         udelay(200);
975
976         /* 2. Precharge all. Wait tRP. */
977         PRINT_DEBUG("RAM Enable 2: Precharge all\n");
978         do_ram_command(RAM_COMMAND_PRECHARGE);
979         udelay(1);
980
981         /* 3. Perform 8 refresh cycles. Wait tRC each time. */
982         PRINT_DEBUG("RAM Enable 3: CBR\n");
983         for (i = 0; i < 8; i++) {
984                 do_ram_command(RAM_COMMAND_CBR);
985                 udelay(1);
986         }
987
988         /* 4. Mode register set. Wait two memory cycles. */
989         PRINT_DEBUG("RAM Enable 4: Mode register set\n");
990         do_ram_command(RAM_COMMAND_MRS);
991         udelay(2);
992
993         /* 5. Normal operation. */
994         PRINT_DEBUG("RAM Enable 5: Normal operation\n");
995         do_ram_command(RAM_COMMAND_NORMAL);
996         udelay(1);
997
998         /* 6. Finally enable refresh. */
999         PRINT_DEBUG("RAM Enable 6: Enable refresh\n");
1000         // pci_write_config8(NB, PMCR, 0x10);
1001         spd_enable_refresh();
1002         udelay(1);
1003
1004         PRINT_DEBUG("Northbridge following SDRAM init:\n");
1005         DUMPNORTH();
1006 }