blob: 43e2a45e996815c7bef74fff9a18de19cfea4f34 [file] [log] [blame]
Richard Smithcb8eab42006-07-24 04:25:47 +00001/*
Stefan Reinauer7e61e452008-01-18 10:35:56 +00002 * This file is part of the coreboot project.
Uwe Hermann1a9c8922007-04-01 17:24:03 +00003 *
Uwe Hermann1683cef2008-11-27 00:47:07 +00004 * Copyright (C) 2007-2008 Uwe Hermann <uwe@hermann-uwe.de>
Keith Hui59356ca2010-03-06 18:16:25 +00005 * Copyright (C) 2010 Keith Hui <buurin@gmail.com>
Uwe Hermann1a9c8922007-04-01 17:24:03 +00006 *
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
Richard Smithcb8eab42006-07-24 04:25:47 +000020 */
21
Uwe Hermann1a9c8922007-04-01 17:24:03 +000022#include <spd.h>
23#include <sdram_mode.h>
24#include <delay.h>
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +000025#include <stdlib.h>
Uwe Hermann1a9c8922007-04-01 17:24:03 +000026#include "i440bx.h"
Keith Hui59356ca2010-03-06 18:16:25 +000027#include "raminit.h"
Richard Smithcb8eab42006-07-24 04:25:47 +000028
Uwe Hermann1a9c8922007-04-01 17:24:03 +000029/*-----------------------------------------------------------------------------
30Macros and definitions.
31-----------------------------------------------------------------------------*/
Richard Smithcb8eab42006-07-24 04:25:47 +000032
Uwe Hermann1a9c8922007-04-01 17:24:03 +000033/* Debugging macros. */
Uwe Hermann01ce6012010-03-05 10:03:50 +000034#if CONFIG_DEBUG_RAM_SETUP
Uwe Hermann1a9c8922007-04-01 17:24:03 +000035#define PRINT_DEBUG(x) print_debug(x)
36#define PRINT_DEBUG_HEX8(x) print_debug_hex8(x)
37#define PRINT_DEBUG_HEX16(x) print_debug_hex16(x)
38#define PRINT_DEBUG_HEX32(x) print_debug_hex32(x)
39#define DUMPNORTH() dump_pci_device(PCI_DEV(0, 0, 0))
Richard Smithcb8eab42006-07-24 04:25:47 +000040#else
Uwe Hermann941a6f02007-04-30 23:27:27 +000041#define PRINT_DEBUG(x)
Uwe Hermann1a9c8922007-04-01 17:24:03 +000042#define PRINT_DEBUG_HEX8(x)
43#define PRINT_DEBUG_HEX16(x)
44#define PRINT_DEBUG_HEX32(x)
45#define DUMPNORTH()
Richard Smithcb8eab42006-07-24 04:25:47 +000046#endif
47
Uwe Hermann1683cef2008-11-27 00:47:07 +000048#define NB PCI_DEV(0, 0, 0)
49
Uwe Hermann1a9c8922007-04-01 17:24:03 +000050/* SDRAMC[7:5] - SDRAM Mode Select (SMS). */
51#define RAM_COMMAND_NORMAL 0x0
52#define RAM_COMMAND_NOP 0x1
53#define RAM_COMMAND_PRECHARGE 0x2
54#define RAM_COMMAND_MRS 0x3
55#define RAM_COMMAND_CBR 0x4
Richard Smithcb8eab42006-07-24 04:25:47 +000056
Uwe Hermann1a9c8922007-04-01 17:24:03 +000057/* Map the JEDEC SPD refresh rates (array index) to 440BX refresh rates as
58 * defined in DRAMC[2:0].
59 *
60 * [0] == Normal 15.625 us -> 15.6 us
61 * [1] == Reduced(.25X) 3.9 us -> 7.8 ns
62 * [2] == Reduced(.5X) 7.8 us -> 7.8 us
63 * [3] == Extended(2x) 31.3 us -> 31.2 us
64 * [4] == Extended(4x) 62.5 us -> 62.4 us
65 * [5] == Extended(8x) 125 us -> 124.8 us
66 */
67static const uint32_t refresh_rate_map[] = {
68 1, 5, 5, 2, 3, 4
69};
Richard Smithcb8eab42006-07-24 04:25:47 +000070
Uwe Hermann1a9c8922007-04-01 17:24:03 +000071/* Table format: register, bitmask, value. */
72static const long register_values[] = {
73 /* NBXCFG - NBX Configuration Register
Uwe Hermannf5a6fd22007-05-27 23:31:31 +000074 * 0x50 - 0x53
Uwe Hermann1a9c8922007-04-01 17:24:03 +000075 *
76 * [31:24] SDRAM Row Without ECC
77 * 0 = ECC components are populated in this row
78 * 1 = ECC components are not populated in this row
79 * [23:19] Reserved
80 * [18:18] Host Bus Fast Data Ready Enable (HBFDRE)
81 * Assertion of DRAM data on host bus occurs...
82 * 0 = ...one clock after sampling snoop results (default)
83 * 1 = ...on the same clock the snoop result is being sampled
84 * (this mode is faster by one clock cycle)
85 * [17:17] ECC - EDO static Drive mode
86 * 0 = Normal mode (default)
87 * 1 = ECC signals are always driven
88 * [16:16] IDSEL_REDIRECT
89 * 0 = IDSEL1 is allocated to this bridge (default)
90 * 1 = IDSEL7 is allocated to this bridge
91 * [15:15] WSC# Handshake Disable
92 * 1 = Uni-processor mode
93 * 0 = Dual-processor mode with external IOAPIC (default)
94 * [14:14] Intel Reserved
95 * [13:12] Host/DRAM Frequency
96 * 00 = 100 MHz
97 * 01 = Reserved
98 * 10 = 66 MHz
99 * 11 = Reserved
100 * [11:11] AGP to PCI Access Enable
101 * 1 = Enable
102 * 0 = Disable
103 * [10:10] PCI Agent to Aperture Access Disable
104 * 1 = Disable
105 * 0 = Enable (default)
106 * [09:09] Aperture Access Global Enable
107 * 1 = Enable
108 * 0 = Disable
109 * [08:07] DRAM Data Integrity Mode (DDIM)
110 * 00 = Non-ECC
111 * 01 = EC-only
112 * 10 = ECC Mode
113 * 11 = ECC Mode with hardware scrubbing enabled
114 * [06:06] ECC Diagnostic Mode Enable (EDME)
115 * 1 = Enable
116 * 0 = Normal operation mode (default)
117 * [05:05] MDA Present (MDAP)
118 * Works in conjunction with the VGA_EN bit.
119 * VGA_EN MDAP
120 * 0 x All VGA cycles are sent to PCI
121 * 1 0 All VGA cycles are sent to AGP
122 * 1 1 All VGA cycles are sent to AGP, except for
123 * cycles in the MDA range.
124 * [04:04] Reserved
125 * [03:03] USWC Write Post During I/O Bridge Access Enable (UWPIO)
126 * 1 = Enable
127 * 0 = Disable
128 * [02:02] In-Order Queue Depth (IOQD)
129 * 1 = In-order queue = maximum
130 * 0 = A7# is sampled asserted (i.e., 0)
131 * [01:00] Reserved
Richard Smithcb8eab42006-07-24 04:25:47 +0000132 */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000133 // TODO
Uwe Hermannbc3594732007-06-07 22:16:30 +0000134 NBXCFG + 0, 0x00, 0x0c,
135 // NBXCFG + 1, 0x00, 0xa0,
136 NBXCFG + 1, 0x00, 0x80,
137 NBXCFG + 2, 0x00, 0x00,
138 NBXCFG + 3, 0x00, 0xff,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000139
140 /* DRAMC - DRAM Control Register
141 * 0x57
142 *
143 * [7:6] Reserved
144 * [5:5] Module Mode Configuration (MMCONFIG)
145 * TODO
146 * [4:3] DRAM Type (DT)
147 * 00 = EDO
148 * 01 = SDRAM
149 * 10 = Registered SDRAM
150 * 11 = Reserved
151 * Note: EDO, SDRAM and Registered SDRAM cannot be mixed.
152 * [2:0] DRAM Refresh Rate (DRR)
153 * 000 = Refresh disabled
154 * 001 = 15.6 us
155 * 010 = 31.2 us
156 * 011 = 62.4 us
157 * 100 = 124.8 us
158 * 101 = 249.6 us
159 * 110 = Reserved
160 * 111 = Reserved
Richard Smithcb8eab42006-07-24 04:25:47 +0000161 */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000162 /* Choose SDRAM (not registered), and disable refresh for now. */
Uwe Hermannbc3594732007-06-07 22:16:30 +0000163 DRAMC, 0x00, 0x08,
Richard Smithcb8eab42006-07-24 04:25:47 +0000164
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000165 /*
166 * PAM[6:0] - Programmable Attribute Map Registers
Uwe Hermannf5a6fd22007-05-27 23:31:31 +0000167 * 0x59 - 0x5f
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000168 *
169 * 0x59 [3:0] Reserved
170 * 0x59 [5:4] 0xF0000 - 0xFFFFF BIOS area
171 * 0x5a [1:0] 0xC0000 - 0xC3FFF ISA add-on BIOS
172 * 0x5a [5:4] 0xC4000 - 0xC7FFF ISA add-on BIOS
173 * 0x5b [1:0] 0xC8000 - 0xCBFFF ISA add-on BIOS
174 * 0x5b [5:4] 0xCC000 - 0xCFFFF ISA add-on BIOS
175 * 0x5c [1:0] 0xD0000 - 0xD3FFF ISA add-on BIOS
176 * 0x5c [5:4] 0xD4000 - 0xD7FFF ISA add-on BIOS
177 * 0x5d [1:0] 0xD8000 - 0xDBFFF ISA add-on BIOS
178 * 0x5d [5:4] 0xDC000 - 0xDFFFF ISA add-on BIOS
179 * 0x5e [1:0] 0xE0000 - 0xE3FFF BIOS entension
180 * 0x5e [5:4] 0xE4000 - 0xE7FFF BIOS entension
181 * 0x5f [1:0] 0xE8000 - 0xEBFFF BIOS entension
182 * 0x5f [5:4] 0xEC000 - 0xEFFFF BIOS entension
183 *
184 * Bit assignment:
185 * 00 = DRAM Disabled (all access goes to memory mapped I/O space)
186 * 01 = Read Only (Reads to DRAM, writes to memory mapped I/O space)
187 * 10 = Write Only (Writes to DRAM, reads to memory mapped I/O space)
188 * 11 = Read/Write (all access goes to DRAM)
189 */
Keith Hui59356ca2010-03-06 18:16:25 +0000190
191 /*
192 * Map all legacy regions to RAM (read/write). This is required if
193 * you want to use the RAM area from 768 KB - 1 MB. If the PAM
194 * registers are not set here appropriately, the RAM in that region
195 * will not be accessible, thus a RAM check of it will also fail.
196 *
197 * TODO: This was set in sdram_set_spd_registers().
198 * Test if it still works when set here.
199 */
200 PAM0, 0x00, 0x30,
201 PAM1, 0x00, 0x33,
202 PAM2, 0x00, 0x33,
203 PAM3, 0x00, 0x33,
204 PAM4, 0x00, 0x33,
205 PAM5, 0x00, 0x33,
206 PAM6, 0x00, 0x33,
Richard Smithcb8eab42006-07-24 04:25:47 +0000207
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000208 /* DRB[0:7] - DRAM Row Boundary Registers
209 * 0x60 - 0x67
210 *
211 * An array of 8 byte registers, which hold the ending memory address
212 * assigned to each pair of DIMMs, in 8MB granularity.
213 *
214 * 0x60 DRB0 = Total memory in row0 (in 8 MB)
215 * 0x61 DRB1 = Total memory in row0+1 (in 8 MB)
216 * 0x62 DRB2 = Total memory in row0+1+2 (in 8 MB)
217 * 0x63 DRB3 = Total memory in row0+1+2+3 (in 8 MB)
218 * 0x64 DRB4 = Total memory in row0+1+2+3+4 (in 8 MB)
219 * 0x65 DRB5 = Total memory in row0+1+2+3+4+5 (in 8 MB)
220 * 0x66 DRB6 = Total memory in row0+1+2+3+4+5+6 (in 8 MB)
221 * 0x67 DRB7 = Total memory in row0+1+2+3+4+5+6+7 (in 8 MB)
222 */
Uwe Hermannf5a6fd22007-05-27 23:31:31 +0000223 /* Set the DRBs to zero for now, this will be fixed later. */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000224 DRB0, 0x00, 0x00,
225 DRB1, 0x00, 0x00,
226 DRB2, 0x00, 0x00,
227 DRB3, 0x00, 0x00,
228 DRB4, 0x00, 0x00,
229 DRB5, 0x00, 0x00,
230 DRB6, 0x00, 0x00,
231 DRB7, 0x00, 0x00,
232
233 /* FDHC - Fixed DRAM Hole Control Register
234 * 0x68
235 *
236 * Controls two fixed DRAM holes: 512 KB - 640 KB and 15 MB - 16 MB.
237 *
238 * [7:6] Hole Enable (HEN)
239 * 00 = None
240 * 01 = 512 KB - 640 KB (128 KB)
241 * 10 = 15 MB - 16 MB (1 MB)
242 * 11 = Reserved
243 * [5:0] Reserved
244 */
245 /* No memory holes. */
246 FDHC, 0x00, 0x00,
247
248 /* RPS - SDRAM Row Page Size Register
249 * 0x74 - 0x75
250 *
251 * Sets the row page size for SDRAM. For EDO memory, the page
252 * size is fixed at 2 KB.
253 *
254 * [15:0] Page Size (PS)
255 * TODO
256 */
257 // TODO
Uwe Hermannbc3594732007-06-07 22:16:30 +0000258 RPS + 0, 0x00, 0x00,
259 RPS + 1, 0x00, 0x00,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000260
261 /* SDRAMC - SDRAM Control Register
Uwe Hermann7ea18cf2007-05-04 00:51:17 +0000262 * 0x76 - 0x77
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000263 *
264 * [15:10] Reserved
265 * [09:08] Idle/Pipeline DRAM Leadoff Timing (IPDLT)
266 * 00 = Illegal
267 * 01 = Add a clock delay to the lead-off clock count
268 * 10 = Illegal
269 * 11 = Illegal
270 * [07:05] SDRAM Mode Select (SMS)
271 * 000 = Normal SDRAM Operation (default)
272 * 001 = NOP Command Enable
273 * 010 = All Banks Precharge Enable
274 * 011 = Mode Register Set Enable
275 * 100 = CBR Enable
276 * 101 = Reserved
277 * 110 = Reserved
278 * 111 = Reserved
279 * [04:04] SDRAMPWR
280 * 0 = 3 DIMM configuration
281 * 1 = 4 DIMM configuration
282 * [03:03] Leadoff Command Timing (LCT)
283 * 0 = 4 CS# Clock
284 * 1 = 3 CS# Clock
285 * [02:02] CAS# Latency (CL)
286 * 0 = 3 DCLK CAS# latency
287 * 1 = 2 DCLK CAS# latency
288 * [01:01] SDRAM RAS# to CAS# Delay (SRCD)
289 * 0 = 3 clocks between a row activate and a read or write cmd.
290 * 1 = 2 clocks between a row activate and a read or write cmd.
291 * [00:00] SDRAM RAS# Precharge (SRP)
292 * 0 = 3 clocks of RAS# precharge
293 * 1 = 2 clocks of RAS# precharge
294 */
Keith Hui9c1e1f02010-03-13 20:16:48 +0000295#if CONFIG_SDRAMPWR_4DIMM
296 SDRAMC + 0, 0x00, 0x10, /* The board has 4 DIMM slots. */
297#else
298 SDRAMC + 0, 0x00, 0x00, /* The board has 3 DIMM slots.*/
299#endif
Mats Erik Andersson45db3662008-09-30 04:52:29 +0000300 SDRAMC + 1, 0x00, 0x00,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000301
302 /* PGPOL - Paging Policy Register
303 * 0x78 - 0x79
304 *
305 * [15:08] Banks per Row (BPR)
306 * TODO
307 * 0 = 2 banks
308 * 1 = 4 banks
309 * [07:05] Reserved
310 * [04:04] Intel Reserved
311 * [03:00] DRAM Idle Timer (DIT)
312 * 0000 = 0 clocks
313 * 0001 = 2 clocks
314 * 0010 = 4 clocks
315 * 0011 = 8 clocks
316 * 0100 = 10 clocks
317 * 0101 = 12 clocks
318 * 0110 = 16 clocks
319 * 0111 = 32 clocks
320 * 1xxx = Infinite (pages are not closed for idle condition)
321 */
322 // TODO
Uwe Hermannbc3594732007-06-07 22:16:30 +0000323 PGPOL + 0, 0x00, 0x00,
324 PGPOL + 1, 0x00, 0xff,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000325
326 /* PMCR - Power Management Control Register
327 * 0x7a
328 *
329 * [07:07] Power Down SDRAM Enable (PDSE)
330 * 1 = Enable
331 * 0 = Disable
332 * [06:06] ACPI Control Register Enable (SCRE)
333 * 1 = Enable
334 * 0 = Disable (default)
335 * [05:05] Suspend Refresh Type (SRT)
336 * 1 = Self refresh mode
337 * 0 = CBR fresh mode
338 * [04:04] Normal Refresh Enable (NREF_EN)
339 * 1 = Enable
340 * 0 = Disable
341 * [03:03] Quick Start Mode (QSTART)
342 * 1 = Quick start mode for the processor is enabled
343 * [02:02] Gated Clock Enable (GCLKEN)
344 * 1 = Enable
345 * 0 = Disable
346 * [01:01] AGP Disable (AGP_DIS)
347 * 1 = Disable
348 * 0 = Enable
349 * [00:00] CPU reset without PCIRST enable (CRst_En)
350 * 1 = Enable
351 * 0 = Disable
352 */
353 /* Enable normal refresh and the gated clock. */
354 // TODO: Only do this later?
355 // PMCR, 0x00, 0x14,
356 // PMCR, 0x00, 0x10,
357 PMCR, 0x00, 0x00,
Keith Hui59356ca2010-03-06 18:16:25 +0000358
359 /* Enable SCRR.SRRAEN and let BX choose the SRR. */
360 SCRR + 1, 0x00, 0x10,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000361};
362
363/*-----------------------------------------------------------------------------
364SDRAM configuration functions.
365-----------------------------------------------------------------------------*/
366
367/**
368 * Send the specified RAM command to all DIMMs.
369 *
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000370 * @param command The RAM command to send to the DIMM(s).
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000371 */
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000372static void do_ram_command(u32 command)
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000373{
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000374 int i, caslatency;
375 u8 dimm_start, dimm_end;
376 u16 reg16;
377 u32 addr, addr_offset;
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000378
379 /* Configure the RAM command. */
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000380 reg16 = pci_read_config16(NB, SDRAMC);
381 reg16 &= 0xff1f; /* Clear bits 7-5. */
382 reg16 |= (u16) (command << 5); /* Write command into bits 7-5. */
383 pci_write_config16(NB, SDRAMC, reg16);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000384
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000385 /*
386 * RAM_COMMAND_NORMAL affects only the memory controller and
387 * doesn't need to be "sent" to the DIMMs.
388 */
389 if (command == RAM_COMMAND_NORMAL)
390 return;
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000391
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000392 /* Send the RAM command to each row of memory. */
393 dimm_start = 0;
394 for (i = 0; i < (DIMM_SOCKETS * 2); i++) {
Keith Hui59356ca2010-03-06 18:16:25 +0000395 addr_offset = 0;
396 caslatency = 3; /* TODO: Dynamically get CAS latency later. */
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000397 if (command == RAM_COMMAND_MRS) {
398 /*
399 * MAA[12:11,9:0] must be inverted when sent to DIMM
400 * 2 or 3 (no inversion if sent to DIMM 0 or 1).
401 */
402 if ((i >= 0 && i <= 3) && caslatency == 3)
403 addr_offset = 0x1d0;
404 if ((i >= 4 && i <= 7) && caslatency == 3)
405 addr_offset = 0x1e28;
406 if ((i >= 0 && i <= 3) && caslatency == 2)
407 addr_offset = 0x150;
408 if ((i >= 4 && i <= 7) && caslatency == 2)
409 addr_offset = 0x1ea8;
410 }
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000411
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000412 dimm_end = pci_read_config8(NB, DRB + i);
413
414 addr = (dimm_start * 8 * 1024 * 1024) + addr_offset;
415 if (dimm_end > dimm_start) {
416#if 0
417 PRINT_DEBUG(" Sending RAM command 0x");
418 PRINT_DEBUG_HEX16(reg16);
419 PRINT_DEBUG(" to 0x");
420 PRINT_DEBUG_HEX32(addr);
421 PRINT_DEBUG("\r\n");
422#endif
423
424 read32(addr);
425 }
426
427 /* Set the start of the next DIMM. */
428 dimm_start = dimm_end;
429 }
Richard Smithcb8eab42006-07-24 04:25:47 +0000430}
431
Keith Hui59356ca2010-03-06 18:16:25 +0000432static void set_dram_buffer_strength(void)
433{
434 /* TODO: This needs to be set according to the DRAM tech
435 * (x8, x16, or x32). Argh, Intel provides no docs on this!
436 * Currently, it needs to be pulled from the output of
437 * lspci -xxx Rx92
438 *
439 * Relevant registers: MBSC, MBFS, BUFFC.
440 */
441
442 pci_write_config8(NB, MBSC, 0x03);
443}
444
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000445/*-----------------------------------------------------------------------------
446DIMM-independant configuration functions.
447-----------------------------------------------------------------------------*/
Richard Smithcb8eab42006-07-24 04:25:47 +0000448
Uwe Hermann1683cef2008-11-27 00:47:07 +0000449static void spd_enable_refresh(void)
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000450{
451 int i, value;
452 uint8_t reg;
453
Uwe Hermann1683cef2008-11-27 00:47:07 +0000454 reg = pci_read_config8(NB, DRAMC);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000455
456 for (i = 0; i < DIMM_SOCKETS; i++) {
Uwe Hermann1683cef2008-11-27 00:47:07 +0000457 value = spd_read_byte(DIMM_SPD_BASE + i, SPD_REFRESH);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000458 if (value < 0)
459 continue;
460 reg = (reg & 0xf8) | refresh_rate_map[(value & 0x7f)];
461
462 PRINT_DEBUG(" Enabling refresh (DRAMC = 0x");
463 PRINT_DEBUG_HEX8(reg);
464 PRINT_DEBUG(") for DIMM ");
465 PRINT_DEBUG_HEX8(i);
466 PRINT_DEBUG("\r\n");
467 }
468
Uwe Hermann1683cef2008-11-27 00:47:07 +0000469 pci_write_config8(NB, DRAMC, reg);
Richard Smithcb8eab42006-07-24 04:25:47 +0000470}
471
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000472/*-----------------------------------------------------------------------------
473Public interface.
474-----------------------------------------------------------------------------*/
475
Uwe Hermann1683cef2008-11-27 00:47:07 +0000476static void sdram_set_registers(void)
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000477{
478 int i, max;
Uwe Hermannbc3594732007-06-07 22:16:30 +0000479 uint8_t reg;
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000480
481 PRINT_DEBUG("Northbridge prior to SDRAM init:\r\n");
482 DUMPNORTH();
483
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000484 max = ARRAY_SIZE(register_values);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000485
Uwe Hermannf5a6fd22007-05-27 23:31:31 +0000486 /* Set registers as specified in the register_values[] array. */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000487 for (i = 0; i < max; i += 3) {
Uwe Hermann1683cef2008-11-27 00:47:07 +0000488 reg = pci_read_config8(NB, register_values[i]);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000489 reg &= register_values[i + 1];
490 reg |= register_values[i + 2] & ~(register_values[i + 1]);
Uwe Hermann1683cef2008-11-27 00:47:07 +0000491 pci_write_config8(NB, register_values[i], reg);
Keith Hui59356ca2010-03-06 18:16:25 +0000492#if 0
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000493 PRINT_DEBUG(" Set register 0x");
Uwe Hermannbc3594732007-06-07 22:16:30 +0000494 PRINT_DEBUG_HEX8(register_values[i]);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000495 PRINT_DEBUG(" to 0x");
Uwe Hermannbc3594732007-06-07 22:16:30 +0000496 PRINT_DEBUG_HEX8(reg);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000497 PRINT_DEBUG("\r\n");
Keith Hui59356ca2010-03-06 18:16:25 +0000498#endif
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000499 }
500}
501
Keith Hui59356ca2010-03-06 18:16:25 +0000502struct dimm_size {
503 unsigned long side1;
504 unsigned long side2;
505};
506
507static struct dimm_size spd_get_dimm_size(unsigned int device)
508{
509 struct dimm_size sz;
510 int i, module_density, dimm_banks;
511 sz.side1 = 0;
512 module_density = spd_read_byte(device, SPD_DENSITY_OF_EACH_ROW_ON_MODULE);
513 dimm_banks = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
514
515 /* Find the size of side1. */
516 /* Find the larger value. The larger value is always side1. */
517 for (i = 512; i >= 0; i >>= 1) {
518 if ((module_density & i) == i) {
519 sz.side1 = i;
520 break;
521 }
522 }
523
524 /* Set to 0 in case it's single sided. */
525 sz.side2 = 0;
526
527 /* Test if it's a dual-sided DIMM. */
528 if (dimm_banks > 1) {
529 /* Test if there's a second value. If so it's asymmetrical. */
530 if (module_density != i) {
531 /*
532 * Find second value, picking up where we left off.
533 * i >>= 1 done initially to make sure we don't get
534 * the same value again.
535 */
536 for (i >>= 1; i >= 0; i >>= 1) {
537 if (module_density == (sz.side1 | i)) {
538 sz.side2 = i;
539 break;
540 }
541 }
542 /* If not, it's symmetrical. */
543 } else {
544 sz.side2 = sz.side1;
545 }
546 }
547
548 /*
549 * SPD byte 31 is the memory size divided by 4 so we
550 * need to muliply by 4 to get the total size.
551 */
552 sz.side1 *= 4;
553 sz.side2 *= 4;
554
555 return sz;
556}
557/*
558 * Sets DRAM attributes one DIMM at a time, based on SPD data.
559 * Northbridge settings that are set: NBXCFG[31:24], DRB0-DRB7, RPS, DRAMC.
560 */
561static void set_dram_row_attributes(void)
562{
563 int i, dra, drb, col, width, value, rps, edosd, ecc, nbxecc;
564 u8 bpr; /* Top 8 bits of PGPOL */
565
566 edosd = 0;
567 rps = 0;
568 drb = 0;
569 bpr = 0;
570 nbxecc = 0xff;
571
572 for (i = 0; i < DIMM_SOCKETS; i++) {
573 unsigned int device;
574 device = DIMM_SPD_BASE + i;
575 bpr >>= 2;
576
577 /* First check if a DIMM is actually present. */
578 value = spd_read_byte(device, SPD_MEMORY_TYPE);
579 /* This is 440BX! We do EDO too! */
580 if (value == SPD_MEMORY_TYPE_EDO
581 || value == SPD_MEMORY_TYPE_SDRAM) {
582
583 PRINT_DEBUG("Found ");
584 if (value == SPD_MEMORY_TYPE_EDO) {
585 edosd |= 0x02;
586 } else if (value == SPD_MEMORY_TYPE_SDRAM) {
587 edosd |= 0x04;
588 }
589 PRINT_DEBUG("DIMM in slot ");
590 PRINT_DEBUG_HEX8(i);
591 PRINT_DEBUG("\r\n");
592
593 if (edosd == 0x06) {
594 print_err("Mixing EDO/SDRAM unsupported!\r\n");
595 die("HALT\r\n");
596 }
597
598 /* "DRA" is our RPS for the two rows on this DIMM. */
599 dra = 0;
600
601 /* Columns */
602 col = spd_read_byte(device, SPD_NUM_COLUMNS);
603
604 /*
605 * Is this an ECC DIMM? Actually will be a 2 if so.
606 * TODO: Other register than NBXCFG also needs this
607 * ECC information.
608 */
609 ecc = spd_read_byte(device, SPD_DIMM_CONFIG_TYPE);
610
611 /* Data width */
612 width = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_LSB);
613
614 /* Exclude error checking data width from page size calculations */
615 if (ecc) {
616 value = spd_read_byte(device,
617 SPD_ERROR_CHECKING_SDRAM_WIDTH);
618 width -= value;
619 /* ### ECC */
620 /* Clear top 2 bits to help set up NBXCFG. */
621 ecc &= 0x3f;
622 } else {
623 /* Without ECC, top 2 bits should be 11. */
624 ecc |= 0xc0;
625 }
626
627 /* Calculate page size in bits. */
628 value = ((1 << col) * width);
629
630 /* Convert to KB. */
631 dra = (value >> 13);
632
633 /* Number of banks of DIMM (single or double sided). */
634 value = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
635
636 /* Once we have dra, col is done and can be reused.
637 * So it's reused for number of banks.
638 */
639 col = spd_read_byte(device, SPD_NUM_BANKS_PER_SDRAM);
640
641 if (value == 1) {
642 /*
643 * Second bank of 1-bank DIMMs "doesn't have
644 * ECC" - or anything.
645 */
646 ecc |= 0x80;
647 if (dra == 2) {
648 dra = 0x0; /* 2KB */
649 } else if (dra == 4) {
650 dra = 0x1; /* 4KB */
651 } else if (dra == 8) {
652 dra = 0x2; /* 8KB */
653 } else {
654 dra = -1;
655 }
656 /*
657 * Sets a flag in PGPOL[BPR] if this DIMM has
658 * 4 banks per row.
659 */
660 if (col == 4)
661 bpr |= 0x40;
662 } else if (value == 2) {
663 if (dra == 2) {
664 dra = 0x0; /* 2KB */
665 } else if (dra == 4) {
666 dra = 0x05; /* 4KB */
667 } else if (dra == 8) {
668 dra = 0x0a; /* 8KB */
669 } else {
670 dra = -1;
671 }
672 /* Ditto */
673 if (col == 4)
674 bpr |= 0xc0;
675 } else {
676 print_err("# of banks of DIMM unsupported!\r\n");
677 die("HALT\r\n");
678 }
679 if (dra == -1) {
680 print_err("Page size not supported\r\n");
681 die("HALT\r\n");
682 }
683
684 /*
685 * 440BX supports asymmetrical dual-sided DIMMs,
686 * but can't handle DIMMs smaller than 8MB per
687 * side or larger than 128MB per side.
688 */
689 struct dimm_size sz = spd_get_dimm_size(device);
690 if ((sz.side1 < 8)) {
691 print_err("DIMMs smaller than 8MB per side\r\n"
692 "are not supported on this NB.\r\n");
693 die("HALT\r\n");
694 }
695 if ((sz.side1 > 128)) {
696 print_err ("DIMMs > 128MB per side\r\n"
697 "are not supported on this NB\r\n");
698 die("HALT\r\n");
699 }
700
701 /* Divide size by 8 to set up the DRB registers. */
702 drb += (sz.side1 / 8);
703
704 /*
705 * Build the DRB for the next row in MSB so it gets
706 * placed in DRB[n+1] where it belongs when written
707 * as a 16-bit word.
708 */
709 drb &= 0xff;
710 drb |= (drb + (sz.side2 / 8)) << 8;
711 } else {
712#if 0
713 PRINT_DEBUG("No DIMM found in slot ");
714 PRINT_DEBUG_HEX8(i);
715 PRINT_DEBUG("\r\n");
716#endif
717
718 /* If there's no DIMM in the slot, set dra to 0x00. */
719 dra = 0x00;
720 ecc = 0xc0;
721 /* Still have to propagate DRB over. */
722 drb &= 0xff;
723 drb |= (drb << 8);
724 }
725
726 pci_write_config16(NB, DRB + (2 * i), drb);
727#if 0
728 PRINT_DEBUG("DRB has been set to 0x");
729 PRINT_DEBUG_HEX16(drb);
730 PRINT_DEBUG("\r\n");
731#endif
732
733 /* Brings the upper DRB back down to be base for
734 * DRB calculations for the next two rows.
735 */
736 drb >>= 8;
737
738 rps |= (dra & 0x0f) << (i * 4);
739 nbxecc = (nbxecc >> 2) | (ecc & 0xc0);
740 }
741
742 /* Set paging policy register. */
743 pci_write_config8(NB, PGPOL + 1, bpr);
744 PRINT_DEBUG("PGPOL[BPR] has been set to 0x");
745 PRINT_DEBUG_HEX8(bpr);
746 PRINT_DEBUG("\r\n");
747
748 /* Set DRAM row page size register. */
749 pci_write_config16(NB, RPS, rps);
750 PRINT_DEBUG("RPS has been set to 0x");
751 PRINT_DEBUG_HEX16(rps);
752 PRINT_DEBUG("\r\n");
753
754 /* ### ECC */
755 pci_write_config8(NB, NBXCFG + 3, nbxecc);
756 PRINT_DEBUG("NBXECC[31:24] has been set to 0x");
757 PRINT_DEBUG_HEX8(nbxecc);
758 PRINT_DEBUG("\r\n");
759
760 /* Set DRAMC[4:3] to proper memory type (EDO/SDRAM).
761 * TODO: Registered SDRAM support.
762 */
763 edosd &= 0x07;
764 if (edosd & 0x02) {
765 edosd |= 0x00;
766 } else if (edosd & 0x04) {
767 edosd |= 0x08;
768 }
769 edosd &= 0x18;
770
771 /* edosd is now in the form needed for DRAMC[4:3]. */
772 value = pci_read_config8(NB, DRAMC) & 0xe7;
773 value |= edosd;
774 pci_write_config8(NB, DRAMC, value);
775 PRINT_DEBUG("DRAMC has been set to 0x");
776 PRINT_DEBUG_HEX8(value);
777 PRINT_DEBUG("\r\n");
778}
779
Uwe Hermann1683cef2008-11-27 00:47:07 +0000780static void sdram_set_spd_registers(void)
Richard Smithcb8eab42006-07-24 04:25:47 +0000781{
Keith Hui59356ca2010-03-06 18:16:25 +0000782 /* Setup DRAM row boundary registers and other attributes. */
783 set_dram_row_attributes();
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000784
785 /* TODO: Set SDRAMC. */
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000786 pci_write_config16(NB, SDRAMC, 0x0010); /* SDRAMPWR=1: 4 DIMM config */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000787
Keith Hui59356ca2010-03-06 18:16:25 +0000788 /* TODO */
789 set_dram_buffer_strength();
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000790
791 /* TODO: Set PMCR? */
Uwe Hermann1683cef2008-11-27 00:47:07 +0000792 // pci_write_config8(NB, PMCR, 0x14);
793 pci_write_config8(NB, PMCR, 0x10);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000794
795 /* TODO? */
Uwe Hermann1683cef2008-11-27 00:47:07 +0000796 pci_write_config8(NB, DRAMT, 0x03);
Richard Smithcb8eab42006-07-24 04:25:47 +0000797}
798
Uwe Hermann1683cef2008-11-27 00:47:07 +0000799static void sdram_enable(void)
Richard Smithcb8eab42006-07-24 04:25:47 +0000800{
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000801 int i;
Richard Smithcb8eab42006-07-24 04:25:47 +0000802
Uwe Hermann861f9642007-05-28 14:37:06 +0000803 /* 0. Wait until power/voltages and clocks are stable (200us). */
804 udelay(200);
Richard Smithcb8eab42006-07-24 04:25:47 +0000805
Uwe Hermann861f9642007-05-28 14:37:06 +0000806 /* 1. Apply NOP. Wait 200 clock cycles (200us should do). */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000807 PRINT_DEBUG("RAM Enable 1: Apply NOP\r\n");
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000808 do_ram_command(RAM_COMMAND_NOP);
Uwe Hermann861f9642007-05-28 14:37:06 +0000809 udelay(200);
Richard Smithcb8eab42006-07-24 04:25:47 +0000810
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000811 /* 2. Precharge all. Wait tRP. */
812 PRINT_DEBUG("RAM Enable 2: Precharge all\r\n");
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000813 do_ram_command(RAM_COMMAND_PRECHARGE);
Uwe Hermann861f9642007-05-28 14:37:06 +0000814 udelay(1);
Richard Smithcb8eab42006-07-24 04:25:47 +0000815
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000816 /* 3. Perform 8 refresh cycles. Wait tRC each time. */
817 PRINT_DEBUG("RAM Enable 3: CBR\r\n");
818 for (i = 0; i < 8; i++) {
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000819 do_ram_command(RAM_COMMAND_CBR);
Uwe Hermann861f9642007-05-28 14:37:06 +0000820 udelay(1);
Richard Smithcb8eab42006-07-24 04:25:47 +0000821 }
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000822
823 /* 4. Mode register set. Wait two memory cycles. */
824 PRINT_DEBUG("RAM Enable 4: Mode register set\r\n");
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000825 do_ram_command(RAM_COMMAND_MRS);
Uwe Hermann861f9642007-05-28 14:37:06 +0000826 udelay(2);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000827
828 /* 5. Normal operation. */
829 PRINT_DEBUG("RAM Enable 5: Normal operation\r\n");
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000830 do_ram_command(RAM_COMMAND_NORMAL);
Uwe Hermann861f9642007-05-28 14:37:06 +0000831 udelay(1);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000832
833 /* 6. Finally enable refresh. */
834 PRINT_DEBUG("RAM Enable 6: Enable refresh\r\n");
Uwe Hermann1683cef2008-11-27 00:47:07 +0000835 // pci_write_config8(NB, PMCR, 0x10);
836 spd_enable_refresh();
Uwe Hermann861f9642007-05-28 14:37:06 +0000837 udelay(1);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000838
839 PRINT_DEBUG("Northbridge following SDRAM init:\r\n");
840 DUMPNORTH();
Richard Smithcb8eab42006-07-24 04:25:47 +0000841}