Debian Codegraph @

src/northbridge/intel/i440bx/raminit.c

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