blob: 0c9496b2d56af71fa135cbc4e43fe5f1fac66497 [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 Hui9aa45e62017-07-20 21:00:56 -04005 * Copyright (C) 2010,2017 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.
Richard Smithcb8eab42006-07-24 04:25:47 +000016 */
17
Uwe Hermann1a9c8922007-04-01 17:24:03 +000018#include <spd.h>
Uwe Hermann1a9c8922007-04-01 17:24:03 +000019#include <delay.h>
Uwe Hermann115c5b92010-10-09 17:00:18 +000020#include <stdint.h>
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +000021#include <stdlib.h>
Uwe Hermann115c5b92010-10-09 17:00:18 +000022#include <arch/io.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020023#include <device/pci_ops.h>
Uwe Hermann115c5b92010-10-09 17:00:18 +000024#include <device/pci_def.h>
25#include <console/console.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
Keith Hui9aa45e62017-07-20 21:00:56 -040029/*
30 * Macros and definitions
31 */
Keith Huidf35cdc2010-09-20 23:41:37 +000032
Uwe Hermann1a9c8922007-04-01 17:24:03 +000033/* Debugging macros. */
Martin Roth33232602017-06-24 14:48:50 -060034#if IS_ENABLED(CONFIG_DEBUG_RAM_SETUP)
Keith Hui09f5a742010-12-23 17:12:03 +000035#define PRINT_DEBUG(x...) printk(BIOS_DEBUG, x)
Keith Huidf35cdc2010-09-20 23:41:37 +000036#define DUMPNORTH() dump_pci_device(NB)
Richard Smithcb8eab42006-07-24 04:25:47 +000037#else
Keith Hui09f5a742010-12-23 17:12:03 +000038#define PRINT_DEBUG(x...)
Uwe Hermann1a9c8922007-04-01 17:24:03 +000039#define DUMPNORTH()
Richard Smithcb8eab42006-07-24 04:25:47 +000040#endif
41
Uwe Hermann1a9c8922007-04-01 17:24:03 +000042/* SDRAMC[7:5] - SDRAM Mode Select (SMS). */
43#define RAM_COMMAND_NORMAL 0x0
44#define RAM_COMMAND_NOP 0x1
45#define RAM_COMMAND_PRECHARGE 0x2
46#define RAM_COMMAND_MRS 0x3
47#define RAM_COMMAND_CBR 0x4
Richard Smithcb8eab42006-07-24 04:25:47 +000048
Uwe Hermann1a9c8922007-04-01 17:24:03 +000049/* Map the JEDEC SPD refresh rates (array index) to 440BX refresh rates as
50 * defined in DRAMC[2:0].
51 *
52 * [0] == Normal 15.625 us -> 15.6 us
53 * [1] == Reduced(.25X) 3.9 us -> 7.8 ns
54 * [2] == Reduced(.5X) 7.8 us -> 7.8 us
55 * [3] == Extended(2x) 31.3 us -> 31.2 us
56 * [4] == Extended(4x) 62.5 us -> 62.4 us
57 * [5] == Extended(8x) 125 us -> 124.8 us
58 */
59static const uint32_t refresh_rate_map[] = {
60 1, 5, 5, 2, 3, 4
61};
Richard Smithcb8eab42006-07-24 04:25:47 +000062
Uwe Hermann1a9c8922007-04-01 17:24:03 +000063/* Table format: register, bitmask, value. */
Keith Huidf35cdc2010-09-20 23:41:37 +000064static const u8 register_values[] = {
Uwe Hermann1a9c8922007-04-01 17:24:03 +000065 /* NBXCFG - NBX Configuration Register
Uwe Hermannf5a6fd22007-05-27 23:31:31 +000066 * 0x50 - 0x53
Uwe Hermann1a9c8922007-04-01 17:24:03 +000067 *
68 * [31:24] SDRAM Row Without ECC
69 * 0 = ECC components are populated in this row
70 * 1 = ECC components are not populated in this row
71 * [23:19] Reserved
72 * [18:18] Host Bus Fast Data Ready Enable (HBFDRE)
73 * Assertion of DRAM data on host bus occurs...
74 * 0 = ...one clock after sampling snoop results (default)
75 * 1 = ...on the same clock the snoop result is being sampled
76 * (this mode is faster by one clock cycle)
77 * [17:17] ECC - EDO static Drive mode
78 * 0 = Normal mode (default)
79 * 1 = ECC signals are always driven
80 * [16:16] IDSEL_REDIRECT
81 * 0 = IDSEL1 is allocated to this bridge (default)
82 * 1 = IDSEL7 is allocated to this bridge
83 * [15:15] WSC# Handshake Disable
84 * 1 = Uni-processor mode
85 * 0 = Dual-processor mode with external IOAPIC (default)
86 * [14:14] Intel Reserved
87 * [13:12] Host/DRAM Frequency
88 * 00 = 100 MHz
89 * 01 = Reserved
90 * 10 = 66 MHz
91 * 11 = Reserved
92 * [11:11] AGP to PCI Access Enable
93 * 1 = Enable
94 * 0 = Disable
95 * [10:10] PCI Agent to Aperture Access Disable
96 * 1 = Disable
97 * 0 = Enable (default)
98 * [09:09] Aperture Access Global Enable
99 * 1 = Enable
100 * 0 = Disable
101 * [08:07] DRAM Data Integrity Mode (DDIM)
102 * 00 = Non-ECC
103 * 01 = EC-only
104 * 10 = ECC Mode
105 * 11 = ECC Mode with hardware scrubbing enabled
106 * [06:06] ECC Diagnostic Mode Enable (EDME)
107 * 1 = Enable
108 * 0 = Normal operation mode (default)
109 * [05:05] MDA Present (MDAP)
110 * Works in conjunction with the VGA_EN bit.
111 * VGA_EN MDAP
112 * 0 x All VGA cycles are sent to PCI
113 * 1 0 All VGA cycles are sent to AGP
114 * 1 1 All VGA cycles are sent to AGP, except for
115 * cycles in the MDA range.
116 * [04:04] Reserved
117 * [03:03] USWC Write Post During I/O Bridge Access Enable (UWPIO)
118 * 1 = Enable
119 * 0 = Disable
120 * [02:02] In-Order Queue Depth (IOQD)
121 * 1 = In-order queue = maximum
122 * 0 = A7# is sampled asserted (i.e., 0)
123 * [01:00] Reserved
Richard Smithcb8eab42006-07-24 04:25:47 +0000124 */
Uwe Hermannbc3594732007-06-07 22:16:30 +0000125 NBXCFG + 0, 0x00, 0x0c,
Keith Huidf35cdc2010-09-20 23:41:37 +0000126 // TODO: Bit 15 should be 0 for multiprocessor boards
Uwe Hermannbc3594732007-06-07 22:16:30 +0000127 NBXCFG + 1, 0x00, 0x80,
128 NBXCFG + 2, 0x00, 0x00,
129 NBXCFG + 3, 0x00, 0xff,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000130
131 /* DRAMC - DRAM Control Register
132 * 0x57
133 *
134 * [7:6] Reserved
135 * [5:5] Module Mode Configuration (MMCONFIG)
Keith Huia8380fc2017-12-04 00:05:56 -0500136 * The combination of SDRAMPWR and this bit (set by an
Keith Huidf35cdc2010-09-20 23:41:37 +0000137 * external strapping option) determine how CKE works.
138 * SDRAMPWR MMCONFIG
Keith Huia8380fc2017-12-04 00:05:56 -0500139 * 0 0 = 3 DIMM, CKE[5:0] driven
140 * X 1 = 3 DIMM, CKE0 only
141 * 1 0 = 4 DIMM, GCKE only
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000142 * [4:3] DRAM Type (DT)
143 * 00 = EDO
144 * 01 = SDRAM
145 * 10 = Registered SDRAM
146 * 11 = Reserved
147 * Note: EDO, SDRAM and Registered SDRAM cannot be mixed.
148 * [2:0] DRAM Refresh Rate (DRR)
149 * 000 = Refresh disabled
150 * 001 = 15.6 us
151 * 010 = 31.2 us
152 * 011 = 62.4 us
153 * 100 = 124.8 us
154 * 101 = 249.6 us
155 * 110 = Reserved
156 * 111 = Reserved
Richard Smithcb8eab42006-07-24 04:25:47 +0000157 */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000158 /* Choose SDRAM (not registered), and disable refresh for now. */
Uwe Hermannbc3594732007-06-07 22:16:30 +0000159 DRAMC, 0x00, 0x08,
Richard Smithcb8eab42006-07-24 04:25:47 +0000160
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000161 /*
162 * PAM[6:0] - Programmable Attribute Map Registers
Uwe Hermannf5a6fd22007-05-27 23:31:31 +0000163 * 0x59 - 0x5f
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000164 *
165 * 0x59 [3:0] Reserved
166 * 0x59 [5:4] 0xF0000 - 0xFFFFF BIOS area
167 * 0x5a [1:0] 0xC0000 - 0xC3FFF ISA add-on BIOS
168 * 0x5a [5:4] 0xC4000 - 0xC7FFF ISA add-on BIOS
169 * 0x5b [1:0] 0xC8000 - 0xCBFFF ISA add-on BIOS
170 * 0x5b [5:4] 0xCC000 - 0xCFFFF ISA add-on BIOS
171 * 0x5c [1:0] 0xD0000 - 0xD3FFF ISA add-on BIOS
172 * 0x5c [5:4] 0xD4000 - 0xD7FFF ISA add-on BIOS
173 * 0x5d [1:0] 0xD8000 - 0xDBFFF ISA add-on BIOS
174 * 0x5d [5:4] 0xDC000 - 0xDFFFF ISA add-on BIOS
Martin Roth128c1042016-11-18 09:29:03 -0700175 * 0x5e [1:0] 0xE0000 - 0xE3FFF BIOS extension
176 * 0x5e [5:4] 0xE4000 - 0xE7FFF BIOS extension
177 * 0x5f [1:0] 0xE8000 - 0xEBFFF BIOS extension
178 * 0x5f [5:4] 0xEC000 - 0xEFFFF BIOS extension
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000179 *
180 * Bit assignment:
181 * 00 = DRAM Disabled (all access goes to memory mapped I/O space)
182 * 01 = Read Only (Reads to DRAM, writes to memory mapped I/O space)
183 * 10 = Write Only (Writes to DRAM, reads to memory mapped I/O space)
184 * 11 = Read/Write (all access goes to DRAM)
185 */
Keith Hui59356ca2010-03-06 18:16:25 +0000186
187 /*
188 * Map all legacy regions to RAM (read/write). This is required if
189 * you want to use the RAM area from 768 KB - 1 MB. If the PAM
190 * registers are not set here appropriately, the RAM in that region
191 * will not be accessible, thus a RAM check of it will also fail.
Keith Hui59356ca2010-03-06 18:16:25 +0000192 */
193 PAM0, 0x00, 0x30,
194 PAM1, 0x00, 0x33,
195 PAM2, 0x00, 0x33,
196 PAM3, 0x00, 0x33,
197 PAM4, 0x00, 0x33,
198 PAM5, 0x00, 0x33,
199 PAM6, 0x00, 0x33,
Richard Smithcb8eab42006-07-24 04:25:47 +0000200
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000201 /* DRB[0:7] - DRAM Row Boundary Registers
202 * 0x60 - 0x67
203 *
204 * An array of 8 byte registers, which hold the ending memory address
Anders Jenbo0e1e8062010-04-27 06:35:31 +0000205 * assigned to each pair of DIMMs, in 8MB granularity.
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000206 *
207 * 0x60 DRB0 = Total memory in row0 (in 8 MB)
208 * 0x61 DRB1 = Total memory in row0+1 (in 8 MB)
209 * 0x62 DRB2 = Total memory in row0+1+2 (in 8 MB)
210 * 0x63 DRB3 = Total memory in row0+1+2+3 (in 8 MB)
211 * 0x64 DRB4 = Total memory in row0+1+2+3+4 (in 8 MB)
212 * 0x65 DRB5 = Total memory in row0+1+2+3+4+5 (in 8 MB)
213 * 0x66 DRB6 = Total memory in row0+1+2+3+4+5+6 (in 8 MB)
214 * 0x67 DRB7 = Total memory in row0+1+2+3+4+5+6+7 (in 8 MB)
215 */
Keith Huia8380fc2017-12-04 00:05:56 -0500216 /* DRBs will be set later. */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000217 DRB0, 0x00, 0x00,
218 DRB1, 0x00, 0x00,
219 DRB2, 0x00, 0x00,
220 DRB3, 0x00, 0x00,
221 DRB4, 0x00, 0x00,
222 DRB5, 0x00, 0x00,
223 DRB6, 0x00, 0x00,
224 DRB7, 0x00, 0x00,
225
226 /* FDHC - Fixed DRAM Hole Control Register
227 * 0x68
228 *
229 * Controls two fixed DRAM holes: 512 KB - 640 KB and 15 MB - 16 MB.
230 *
231 * [7:6] Hole Enable (HEN)
232 * 00 = None
233 * 01 = 512 KB - 640 KB (128 KB)
234 * 10 = 15 MB - 16 MB (1 MB)
235 * 11 = Reserved
236 * [5:0] Reserved
237 */
238 /* No memory holes. */
239 FDHC, 0x00, 0x00,
240
241 /* RPS - SDRAM Row Page Size Register
242 * 0x74 - 0x75
243 *
244 * Sets the row page size for SDRAM. For EDO memory, the page
245 * size is fixed at 2 KB.
246 *
Keith Huidf35cdc2010-09-20 23:41:37 +0000247 * Bits[1:0] Page Size
248 * 00 2 KB
249 * 01 4 KB
250 * 10 8 KB
251 * 11 Reserved
Keith Huie089a3f2011-08-02 22:28:14 -0400252 *
Keith Huidf35cdc2010-09-20 23:41:37 +0000253 * RPS bits Corresponding DRB register
254 * [01:00] DRB[0], row 0
255 * [03:02] DRB[1], row 1
256 * [05:04] DRB[2], row 2
257 * [07:06] DRB[3], row 3
258 * [09:08] DRB[4], row 4
259 * [11:10] DRB[5], row 5
260 * [13:12] DRB[6], row 6
261 * [15:14] DRB[7], row 7
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000262 */
Keith Huidf35cdc2010-09-20 23:41:37 +0000263 /* Power on defaults to 2KB. Will be set later. */
264 // RPS + 0, 0x00, 0x00,
265 // RPS + 1, 0x00, 0x00,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000266
267 /* SDRAMC - SDRAM Control Register
Uwe Hermann7ea18cf2007-05-04 00:51:17 +0000268 * 0x76 - 0x77
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000269 *
270 * [15:10] Reserved
271 * [09:08] Idle/Pipeline DRAM Leadoff Timing (IPDLT)
272 * 00 = Illegal
273 * 01 = Add a clock delay to the lead-off clock count
Keith Huidf35cdc2010-09-20 23:41:37 +0000274 * 1x = Illegal
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000275 * [07:05] SDRAM Mode Select (SMS)
276 * 000 = Normal SDRAM Operation (default)
277 * 001 = NOP Command Enable
278 * 010 = All Banks Precharge Enable
279 * 011 = Mode Register Set Enable
280 * 100 = CBR Enable
281 * 101 = Reserved
282 * 110 = Reserved
283 * 111 = Reserved
284 * [04:04] SDRAMPWR
285 * 0 = 3 DIMM configuration
286 * 1 = 4 DIMM configuration
287 * [03:03] Leadoff Command Timing (LCT)
288 * 0 = 4 CS# Clock
289 * 1 = 3 CS# Clock
290 * [02:02] CAS# Latency (CL)
291 * 0 = 3 DCLK CAS# latency
292 * 1 = 2 DCLK CAS# latency
293 * [01:01] SDRAM RAS# to CAS# Delay (SRCD)
294 * 0 = 3 clocks between a row activate and a read or write cmd.
295 * 1 = 2 clocks between a row activate and a read or write cmd.
296 * [00:00] SDRAM RAS# Precharge (SRP)
297 * 0 = 3 clocks of RAS# precharge
298 * 1 = 2 clocks of RAS# precharge
299 */
Martin Roth33232602017-06-24 14:48:50 -0600300#if IS_ENABLED(CONFIG_SDRAMPWR_4DIMM)
Keith Hui9c1e1f02010-03-13 20:16:48 +0000301 SDRAMC + 0, 0x00, 0x10, /* The board has 4 DIMM slots. */
302#else
Keith Huidf35cdc2010-09-20 23:41:37 +0000303 SDRAMC + 0, 0x00, 0x00, /* The board has 3 DIMM slots. */
Keith Hui9c1e1f02010-03-13 20:16:48 +0000304#endif
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000305
306 /* PGPOL - Paging Policy Register
307 * 0x78 - 0x79
308 *
309 * [15:08] Banks per Row (BPR)
Keith Huidf35cdc2010-09-20 23:41:37 +0000310 * Each bit in this field corresponds to one row of the memory
311 * array. Bit 15 corresponds to row 7 while bit 8 corresponds
312 * to row 0. Bits for empty rows are "don't care".
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000313 * 0 = 2 banks
314 * 1 = 4 banks
315 * [07:05] Reserved
316 * [04:04] Intel Reserved
317 * [03:00] DRAM Idle Timer (DIT)
318 * 0000 = 0 clocks
319 * 0001 = 2 clocks
320 * 0010 = 4 clocks
321 * 0011 = 8 clocks
322 * 0100 = 10 clocks
323 * 0101 = 12 clocks
324 * 0110 = 16 clocks
325 * 0111 = 32 clocks
326 * 1xxx = Infinite (pages are not closed for idle condition)
327 */
Uwe Hermannbc3594732007-06-07 22:16:30 +0000328 PGPOL + 0, 0x00, 0x00,
329 PGPOL + 1, 0x00, 0xff,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000330
331 /* PMCR - Power Management Control Register
332 * 0x7a
333 *
334 * [07:07] Power Down SDRAM Enable (PDSE)
335 * 1 = Enable
336 * 0 = Disable
337 * [06:06] ACPI Control Register Enable (SCRE)
338 * 1 = Enable
339 * 0 = Disable (default)
340 * [05:05] Suspend Refresh Type (SRT)
341 * 1 = Self refresh mode
342 * 0 = CBR fresh mode
343 * [04:04] Normal Refresh Enable (NREF_EN)
344 * 1 = Enable
345 * 0 = Disable
346 * [03:03] Quick Start Mode (QSTART)
347 * 1 = Quick start mode for the processor is enabled
348 * [02:02] Gated Clock Enable (GCLKEN)
349 * 1 = Enable
350 * 0 = Disable
351 * [01:01] AGP Disable (AGP_DIS)
352 * 1 = Disable
353 * 0 = Enable
354 * [00:00] CPU reset without PCIRST enable (CRst_En)
355 * 1 = Enable
356 * 0 = Disable
357 */
358 /* Enable normal refresh and the gated clock. */
359 // TODO: Only do this later?
360 // PMCR, 0x00, 0x14,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000361 PMCR, 0x00, 0x00,
Keith Hui59356ca2010-03-06 18:16:25 +0000362
363 /* Enable SCRR.SRRAEN and let BX choose the SRR. */
364 SCRR + 1, 0x00, 0x10,
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000365};
366
367/*-----------------------------------------------------------------------------
368SDRAM configuration functions.
369-----------------------------------------------------------------------------*/
370
371/**
372 * Send the specified RAM command to all DIMMs.
373 *
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000374 * @param command The RAM command to send to the DIMM(s).
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000375 */
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000376static void do_ram_command(u32 command)
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000377{
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000378 int i, caslatency;
379 u8 dimm_start, dimm_end;
380 u16 reg16;
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800381 void *addr;
382 u32 addr_offset;
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000383
384 /* Configure the RAM command. */
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000385 reg16 = pci_read_config16(NB, SDRAMC);
386 reg16 &= 0xff1f; /* Clear bits 7-5. */
387 reg16 |= (u16) (command << 5); /* Write command into bits 7-5. */
388 pci_write_config16(NB, SDRAMC, reg16);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000389
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000390 /*
391 * RAM_COMMAND_NORMAL affects only the memory controller and
392 * doesn't need to be "sent" to the DIMMs.
393 */
394 if (command == RAM_COMMAND_NORMAL)
395 return;
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000396
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000397 /* Send the RAM command to each row of memory. */
398 dimm_start = 0;
399 for (i = 0; i < (DIMM_SOCKETS * 2); i++) {
Keith Hui59356ca2010-03-06 18:16:25 +0000400 addr_offset = 0;
401 caslatency = 3; /* TODO: Dynamically get CAS latency later. */
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000402 if (command == RAM_COMMAND_MRS) {
403 /*
404 * MAA[12:11,9:0] must be inverted when sent to DIMM
405 * 2 or 3 (no inversion if sent to DIMM 0 or 1).
406 */
407 if ((i >= 0 && i <= 3) && caslatency == 3)
408 addr_offset = 0x1d0;
409 if ((i >= 4 && i <= 7) && caslatency == 3)
410 addr_offset = 0x1e28;
411 if ((i >= 0 && i <= 3) && caslatency == 2)
412 addr_offset = 0x150;
413 if ((i >= 4 && i <= 7) && caslatency == 2)
414 addr_offset = 0x1ea8;
415 }
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000416
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000417 dimm_end = pci_read_config8(NB, DRB + i);
418
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -0800419 addr = (void *)((dimm_start * 8 * 1024 * 1024) + addr_offset);
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000420 if (dimm_end > dimm_start) {
Uwe Hermann8b643cea2008-12-09 16:36:12 +0000421 read32(addr);
422 }
423
424 /* Set the start of the next DIMM. */
425 dimm_start = dimm_end;
426 }
Richard Smithcb8eab42006-07-24 04:25:47 +0000427}
428
Keith Hui59356ca2010-03-06 18:16:25 +0000429static void set_dram_buffer_strength(void)
430{
Keith Huia8380fc2017-12-04 00:05:56 -0500431 /*
432 * Program MBSC[39:0] and MBFS[23:0].
433 *
434 * The 440BX datasheet says buffer frequency is independent from bus
435 * frequency and mismatch both ways are possible.
436 *
437 * MBSC[47:40] and MBFS[23] are reserved.
Keith Hui59356ca2010-03-06 18:16:25 +0000438 */
439
Keith Huia8380fc2017-12-04 00:05:56 -0500440 unsigned int i, reg, drb;
441 uint8_t mbsc0, mbfs0, mbfs1, mbfs2;
442 uint16_t mbsc1, mbsc3;
443
444 /*
445 * Tally how many rows between rows 0-3 and rows 4-7 are populated.
Keith Huib48ba662010-03-17 02:15:07 +0000446 * This determines how to program MBFS and MBSC.
447 */
448 uint8_t dimm03 = 0;
449 uint8_t dimm47 = 0;
450
Keith Huia8380fc2017-12-04 00:05:56 -0500451 for (drb = 0, i = DRB0; i <= DRB7; i++) {
452 reg = pci_read_config8(NB, i);
453 if (drb != reg) {
454 if (i <= DRB3)
Keith Huib48ba662010-03-17 02:15:07 +0000455 dimm03++;
Keith Huia8380fc2017-12-04 00:05:56 -0500456 else
Keith Huib48ba662010-03-17 02:15:07 +0000457 dimm47++;
Keith Huia8380fc2017-12-04 00:05:56 -0500458
459 drb = reg;
Keith Huib48ba662010-03-17 02:15:07 +0000460 }
461 }
462
Keith Huia8380fc2017-12-04 00:05:56 -0500463 if (IS_ENABLED(CONFIG_SDRAMPWR_4DIMM)) {
464 /*
465 * For a 4 DIMM board, based on ASUS P2B-LS mainboard.
Keith Huib48ba662010-03-17 02:15:07 +0000466 *
Keith Huia8380fc2017-12-04 00:05:56 -0500467 * There are four main conditions to check when programming
468 * DRAM buffer frequency and strength:
Keith Huib48ba662010-03-17 02:15:07 +0000469 *
470 * a: >2 rows populated across DIMM0,1
471 * b: >2 rows populated across DIMM2,3
472 * c: >4 rows populated across all DIMM slots
473 * and either one of:
474 * 1: NBXCFG[13] strapped as 100MHz, or
475 * 6: NBXCFG[13] strapped as 66MHz
476 *
477 * CKE0/FENA ----------------------------------------------------------+
Keith Huia8380fc2017-12-04 00:05:56 -0500478 * CKE1/GCKE ----------------------[ MBFS ]---------------------+|
479 * DQMA/CASA[764320]# -------------[ 0 = 66MHz ]--------------------+||
480 * DQMB1/CASB1# (Fixed for 66MHz) -[ 1 = 100MHz ]-------------------+|||
481 * DQMB5/CASB5# (Fixed for 66MHz) ---------------------------------+||||
482 * DQMA1/CASA1# (Fixed for 66MHz) --------------------------------+|||||
483 * DQMA5/CASA5# (Fixed for 66MHz) -------------------------------+||||||
484 * CSA[5:0]#,CSB[5:0]# ------------------------------------++++++|||||||
485 * CS[B7,A7,B6,A6]#/CKE[5342] -------------------------++++|||||||||||||
486 * MECC[7:0] #2/#1 ----------------------------------++|||||||||||||||||
487 * MD[63:0] #2/#1 ---------------------------------++|||||||||||||||||||
488 * MAB[12:11,9:0]#,MAB[13,10],WEB#,SRASB#,SCASB# -+|||||||||||||||||||||
489 * MAA[13:0],WEA#,SRASA#,SCASA# -----------------+||||||||||||||||||||||
490 * Reserved ------------------------------------+|||||||||||||||||||||||
491 * ||||||||||||||||||||||||
492 * 3 32 21 10 0 * 2 21 10 0
493 * 9876543210987654321098765432109876543210 * 321098765432109876543210
494 * 10------------------------1010---------- a -1---------------11-----
495 * 11------------------------1111---------- !a -0---------------00-----
496 * --10--------------------------1010------ b --1----------------11---
497 * --11--------------------------1111------ !b --0----------------00---
498 * ----------------------------------1100-- c ----------------------1-
499 * ----------------------------------1011-- !c ----------------------0-
500 * ----1010101000000000000000------------00 1 ---11111111111111----1-0
501 * ----000000000000000000000010101010----00 6 ---1111111111111100000-0
502 * | | | | | | | | | | ||||||| | | | | | |
503 * | | | | | | | | | | ||||||| | | | | | +- CKE0/FENA
504 * | | | | | | | | | | ||||||| | | | | +--- CKE1/GCKE
505 * | | | | | | | | | | ||||||| | | | +----- DQMA/CASA[764320]#
506 * | | | | | | | | | | ||||||| | | +------- DQMB1/CASB1# (66MHz: 2x)
507 * | | | | | | | | | | ||||||| | +--------- DQMB5/CASB5# (66MHz: 2x)
508 * | | | | | | | | | | ||||||| +----------- DQMA1/CASA1# (66MHz: 2x)
509 * | | | | | | | | | | ||||||+------------- DQMA5/CASA5# (66MHz: 2x)
510 * | | | | | | | | | | ++++++-------------- CSA0-5#,CSB0-5# (1x)
511 * | | | | | | | | | +--------------------- CSA6#/CKE2
512 * | | | | | | | | +---[ MBSC ]------ CSB6#/CKE4
513 * | | | | | | | +-----[ 00 = 1x ]------ CSA7#/CKE3
514 * | | | | | | +-------[ 01 invalid ]------ CSB7#/CKE5
515 * | | | | | +---------[ 10 = 2x ]------ MECC[7:0] #1
516 * | | | | +-----------[ 11 = 3x ]------ MECC[7:0] #2
517 * | | | +--------------------------------- MD[63:0] #1
518 * | | +----------------------------------- MD[63:0] #2
519 * | +------------------ MAB[12:11,9:0]#,MAB[13,10],WEB#,SRASB#,SCASB#
520 * +------------------------------------- MAA[13:0],WEA#,SRASA#,SCASA#
521 */
522 unsigned int fsb;
523
524 mbsc0 = 0xa0;
525 mbsc1 = 0x002a;
526 mbfs1 = 0xff;
527 mbfs2 = 0x1f;
528 if (pci_read_config8(NB, NBXCFG + 1) & 0x30) {
529 fsb = 66;
530 mbsc3 = 0xa000;
531 mbfs0 = 0x80;
532 } else {
533 fsb = 100;
534 mbsc3 = 0xaaa0;
535 mbfs0 = 0x84;
536 }
537 if (dimm03 > 2) {
538 mbfs2 |= 0x40;
539 if (fsb == 100)
540 mbfs0 |= 0x60;
541 } else {
542 mbsc3 |= 0xc000;
543 if (fsb == 100)
544 mbsc1 |= 0x003c;
545 }
546 if (dimm47 > 2) {
547 mbfs2 |= 0x20;
548 if (fsb == 100)
549 mbfs0 |= 0x18;
550 } else {
551 mbsc3 |= 0x3000;
552 if (fsb == 100) {
553 mbsc1 |= 0x0003;
554 mbsc0 |= 0xc0;
555 }
556 }
557 if ((dimm03 + dimm47) > 4) {
558 mbsc0 |= 0x30;
559 mbfs0 |= 0x02;
560 } else {
561 mbsc0 |= 0x2c;
562 }
563 } else {
564 /*
565 * For a 3 DIMM board, based on ASUS P2B mainboard.
566 *
567 * There are two main conditions to check when programming DRAM buffer
568 * frequency and strength:
569 *
570 * a: >2 rows populated across DIMM0,1
571 * c: >4 rows populated across all DIMM slots
572 *
573 * CKE0 ---------------------------------------------------------------+
574 * CKE1 ------------------------[ MBFS ]------------------------+|
Keith Huib48ba662010-03-17 02:15:07 +0000575 * DQMA/CASA[764320]# ----------[ 0 = 66MHz ]-----------------------+||
576 * DQMB1/CASB1# ----------------[ 1 = 100MHz ]----------------------+|||
577 * DQMB5/CASB5# ---------------------------------------------------+||||
578 * DQMA1/CASA1# --------------------------------------------------+|||||
579 * DQMA5/CASA5# -------------------------------------------------+||||||
580 * CSA0-5#,CSB0-5# ----------------------------------------++++++|||||||
Keith Huia8380fc2017-12-04 00:05:56 -0500581 * CS[B7,A7,B6,A6]#/CKE[5342] -------------------------++++|||||||||||||
Keith Huib48ba662010-03-17 02:15:07 +0000582 * MECC[7:0] #2/#1 (100MHz) -------------------------++|||||||||||||||||
583 * MD[63:0] #2/#1 (100MHz) ------------------------++|||||||||||||||||||
584 * MAB[12:11,9:0]#,MAB[13,10],WEB#,SRASB#,SCASB# -+|||||||||||||||||||||
585 * MAA[13:0],WEA#,SRASA#,SCASA# -----------------+||||||||||||||||||||||
586 * Reserved ------------------------------------+|||||||||||||||||||||||
587 * ||||||||||||||||||||||||
Keith Huia8380fc2017-12-04 00:05:56 -0500588 * 3 32 21 10 0 * 2 21 10 0
589 * 9876543210987654321098765432109876543210 * 321098765432109876543210
590 * 10------------------------1111---------- a -1----------------------
591 * 11------------------------1010---------- !a -0----------------------
592 * --110000000010101010111111----1010--1010 * --01111000000000000000-0
593 * ----------------------------------11---- c ----------------------1-
594 * ----------------------------------10---- !c ----------------------0-
595 * | | | | | | | | | | ||||||| | | | | | |
596 * | | | | | | | | | | ||||||| | | | | | +- CKE0
597 * | | | | | | | | | | ||||||| | | | | +--- CKE1
598 * | | | | | | | | | | ||||||| | | | +----- DQMA/CASA[764320]#
599 * | | | | | | | | | | ||||||| | | +------- DQMB1/CASB1#
600 * | | | | | | | | | | ||||||| | +--------- DQMB5/CASB5#
601 * | | | | | | | | | | ||||||| +----------- DQMA1/CASA1#
602 * | | | | | | | | | | ||||||+------------- DQMA5/CASA5#
603 * | | | | | | | | | | ++++++-------------- CSA0-5#,CSB0-5# (2x)
604 * | | | | | | | | | +--------------------- CSA6#/CKE2
605 * | | | | | | | | +---[ MBSC ]------ CSB6#/CKE4
606 * | | | | | | | +-----[ 00 = 1x ]------ CSA7#/CKE3
607 * | | | | | | +-------[ 01 invalid ]------ CSB7#/CKE5
608 * | | | | | +---------[ 10 = 2x ]------ MECC[7:0] #1 (1x)
609 * | | | | +-----------[ 11 = 3x ]------ MECC[7:0] #2 (1x)
610 * | | | +--------------------------------- MD[63:0] #1 (1x)
611 * | | +----------------------------------- MD[63:0] #2 (1x)
612 * | +------------------ MAB[12:11,9:0]#,MAB[13,10],WEB#,SRASB#,SCASB#
613 * +------------------------------------- MAA[13:0],WEA#,SRASA#,SCASA#
Keith Huib48ba662010-03-17 02:15:07 +0000614 */
Anders Jenbo0e1e8062010-04-27 06:35:31 +0000615
Keith Huia8380fc2017-12-04 00:05:56 -0500616 mbsc0 = 0xaa;
617 mbsc1 = 0xafea;
618 mbsc3 = 0xb00a;
619 mbfs0 = 0x00;
620 mbfs1 = 0x00;
621 mbfs2 = 0x1e;
Anders Jenbo0e1e8062010-04-27 06:35:31 +0000622
Keith Huia8380fc2017-12-04 00:05:56 -0500623 if (dimm03 > 2) {
624 mbsc1 |= 0x003c;
625 mbfs2 |= 0x40;
626 } else {
627 mbsc3 |= 0xc000;
Keith Huib48ba662010-03-17 02:15:07 +0000628 }
Keith Huia8380fc2017-12-04 00:05:56 -0500629 if ((dimm03 + dimm47) > 4) {
630 mbsc0 |= 0x30;
631 mbfs0 |= 0x02;
Keith Huib48ba662010-03-17 02:15:07 +0000632 }
633 }
Keith Huib48ba662010-03-17 02:15:07 +0000634
635 pci_write_config8(NB, MBSC + 0, mbsc0);
Keith Huia8380fc2017-12-04 00:05:56 -0500636 pci_write_config16(NB, MBSC + 1, mbsc1);
637 pci_write_config16(NB, MBSC + 3, mbsc3);
638 pci_write_config16(NB, MBFS + 0, mbfs1 << 8 | mbfs0);
Keith Huib48ba662010-03-17 02:15:07 +0000639 pci_write_config8(NB, MBFS + 2, mbfs2);
Keith Hui59356ca2010-03-06 18:16:25 +0000640}
641
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000642/*-----------------------------------------------------------------------------
Martin Roth128c1042016-11-18 09:29:03 -0700643DIMM-independent configuration functions.
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000644-----------------------------------------------------------------------------*/
Richard Smithcb8eab42006-07-24 04:25:47 +0000645
Uwe Hermann1683cef2008-11-27 00:47:07 +0000646static void spd_enable_refresh(void)
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000647{
648 int i, value;
649 uint8_t reg;
650
Uwe Hermann1683cef2008-11-27 00:47:07 +0000651 reg = pci_read_config8(NB, DRAMC);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000652
653 for (i = 0; i < DIMM_SOCKETS; i++) {
Uwe Hermannd773fd32010-11-20 20:23:08 +0000654 value = spd_read_byte(DIMM0 + i, SPD_REFRESH);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000655 if (value < 0)
656 continue;
657 reg = (reg & 0xf8) | refresh_rate_map[(value & 0x7f)];
658
Keith Hui09f5a742010-12-23 17:12:03 +0000659 PRINT_DEBUG(" Enabling refresh (DRAMC = 0x%02x) for DIMM %02x\n", reg, i);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000660 }
661
Uwe Hermann1683cef2008-11-27 00:47:07 +0000662 pci_write_config8(NB, DRAMC, reg);
Richard Smithcb8eab42006-07-24 04:25:47 +0000663}
664
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000665/*-----------------------------------------------------------------------------
666Public interface.
667-----------------------------------------------------------------------------*/
668
Uwe Hermann115c5b92010-10-09 17:00:18 +0000669void sdram_set_registers(void)
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000670{
671 int i, max;
Uwe Hermannbc3594732007-06-07 22:16:30 +0000672 uint8_t reg;
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000673
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000674 PRINT_DEBUG("Northbridge prior to SDRAM init:\n");
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000675 DUMPNORTH();
676
Carl-Daniel Hailfinger2ee67792008-10-01 12:52:52 +0000677 max = ARRAY_SIZE(register_values);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000678
Uwe Hermannf5a6fd22007-05-27 23:31:31 +0000679 /* Set registers as specified in the register_values[] array. */
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000680 for (i = 0; i < max; i += 3) {
Uwe Hermann1683cef2008-11-27 00:47:07 +0000681 reg = pci_read_config8(NB, register_values[i]);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000682 reg &= register_values[i + 1];
683 reg |= register_values[i + 2] & ~(register_values[i + 1]);
Uwe Hermann1683cef2008-11-27 00:47:07 +0000684 pci_write_config8(NB, register_values[i], reg);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000685 }
686}
687
Keith Hui59356ca2010-03-06 18:16:25 +0000688struct dimm_size {
Keith Hui09f5a742010-12-23 17:12:03 +0000689 u32 side1;
690 u32 side2;
Keith Hui59356ca2010-03-06 18:16:25 +0000691};
692
693static struct dimm_size spd_get_dimm_size(unsigned int device)
694{
695 struct dimm_size sz;
696 int i, module_density, dimm_banks;
697 sz.side1 = 0;
698 module_density = spd_read_byte(device, SPD_DENSITY_OF_EACH_ROW_ON_MODULE);
699 dimm_banks = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
700
701 /* Find the size of side1. */
702 /* Find the larger value. The larger value is always side1. */
703 for (i = 512; i >= 0; i >>= 1) {
704 if ((module_density & i) == i) {
705 sz.side1 = i;
706 break;
707 }
708 }
709
710 /* Set to 0 in case it's single sided. */
711 sz.side2 = 0;
712
713 /* Test if it's a dual-sided DIMM. */
714 if (dimm_banks > 1) {
715 /* Test if there's a second value. If so it's asymmetrical. */
716 if (module_density != i) {
717 /*
718 * Find second value, picking up where we left off.
719 * i >>= 1 done initially to make sure we don't get
720 * the same value again.
721 */
722 for (i >>= 1; i >= 0; i >>= 1) {
723 if (module_density == (sz.side1 | i)) {
724 sz.side2 = i;
725 break;
726 }
727 }
728 /* If not, it's symmetrical. */
729 } else {
730 sz.side2 = sz.side1;
731 }
732 }
733
734 /*
735 * SPD byte 31 is the memory size divided by 4 so we
Martin Roth128c1042016-11-18 09:29:03 -0700736 * need to multiply by 4 to get the total size.
Keith Hui59356ca2010-03-06 18:16:25 +0000737 */
738 sz.side1 *= 4;
739 sz.side2 *= 4;
740
Keith Huia8380fc2017-12-04 00:05:56 -0500741 /*
742 * It is possible to partially use larger than supported
Anders Jenbo771b0e42010-04-27 08:45:30 +0000743 * modules by setting them to a supported size.
744 */
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200745 if (sz.side1 > 128) {
Keith Hui09f5a742010-12-23 17:12:03 +0000746 PRINT_DEBUG("Side1 was %dMB but only 128MB will be used.\n",
747 sz.side1);
Anders Jenbo771b0e42010-04-27 08:45:30 +0000748 sz.side1 = 128;
749
Elyes HAOUAS12df9502016-08-23 21:29:48 +0200750 if (sz.side2 > 128) {
Keith Hui09f5a742010-12-23 17:12:03 +0000751 PRINT_DEBUG("Side2 was %dMB but only 128MB will be used.\n",
752 sz.side2);
Anders Jenbo771b0e42010-04-27 08:45:30 +0000753 sz.side2 = 128;
754 }
755 }
756
Keith Hui59356ca2010-03-06 18:16:25 +0000757 return sz;
758}
759/*
760 * Sets DRAM attributes one DIMM at a time, based on SPD data.
761 * Northbridge settings that are set: NBXCFG[31:24], DRB0-DRB7, RPS, DRAMC.
762 */
763static void set_dram_row_attributes(void)
764{
Keith Huie089a3f2011-08-02 22:28:14 -0400765 int i, dra, drb, col, width, value, rps;
Keith Hui59356ca2010-03-06 18:16:25 +0000766 u8 bpr; /* Top 8 bits of PGPOL */
Keith Huie089a3f2011-08-02 22:28:14 -0400767 u8 nbxecc = 0; /* NBXCFG[31:24] */
768 u8 edo, sd, regsd; /* EDO, SDRAM, registered SDRAM */
Keith Hui59356ca2010-03-06 18:16:25 +0000769
Keith Huie089a3f2011-08-02 22:28:14 -0400770 edo = 0;
771 sd = 0;
772 regsd = 1;
Keith Hui59356ca2010-03-06 18:16:25 +0000773 rps = 0;
774 drb = 0;
775 bpr = 0;
Keith Hui59356ca2010-03-06 18:16:25 +0000776
777 for (i = 0; i < DIMM_SOCKETS; i++) {
778 unsigned int device;
Uwe Hermannd773fd32010-11-20 20:23:08 +0000779 device = DIMM0 + i;
Keith Hui59356ca2010-03-06 18:16:25 +0000780 bpr >>= 2;
Keith Huie089a3f2011-08-02 22:28:14 -0400781 nbxecc >>= 2;
Keith Hui59356ca2010-03-06 18:16:25 +0000782
783 /* First check if a DIMM is actually present. */
784 value = spd_read_byte(device, SPD_MEMORY_TYPE);
785 /* This is 440BX! We do EDO too! */
786 if (value == SPD_MEMORY_TYPE_EDO
787 || value == SPD_MEMORY_TYPE_SDRAM) {
788
Keith Hui59356ca2010-03-06 18:16:25 +0000789 if (value == SPD_MEMORY_TYPE_EDO) {
Keith Huie089a3f2011-08-02 22:28:14 -0400790 edo = 1;
Anders Jenbo0e1e8062010-04-27 06:35:31 +0000791 } else if (value == SPD_MEMORY_TYPE_SDRAM) {
Keith Huie089a3f2011-08-02 22:28:14 -0400792 sd = 1;
Keith Hui59356ca2010-03-06 18:16:25 +0000793 }
Keith Hui09f5a742010-12-23 17:12:03 +0000794 PRINT_DEBUG("Found DIMM in slot %d\n", i);
Keith Hui59356ca2010-03-06 18:16:25 +0000795
Keith Huie089a3f2011-08-02 22:28:14 -0400796 if (edo && sd) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800797 printk(BIOS_ERR, "Mixing EDO/SDRAM unsupported!\n");
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000798 die("HALT\n");
Keith Hui59356ca2010-03-06 18:16:25 +0000799 }
800
801 /* "DRA" is our RPS for the two rows on this DIMM. */
802 dra = 0;
803
804 /* Columns */
805 col = spd_read_byte(device, SPD_NUM_COLUMNS);
806
807 /*
808 * Is this an ECC DIMM? Actually will be a 2 if so.
809 * TODO: Other register than NBXCFG also needs this
810 * ECC information.
811 */
Keith Huie089a3f2011-08-02 22:28:14 -0400812 value = spd_read_byte(device, SPD_DIMM_CONFIG_TYPE);
Keith Hui59356ca2010-03-06 18:16:25 +0000813
814 /* Data width */
815 width = spd_read_byte(device, SPD_MODULE_DATA_WIDTH_LSB);
Anders Jenbo0e1e8062010-04-27 06:35:31 +0000816
Keith Hui59356ca2010-03-06 18:16:25 +0000817 /* Exclude error checking data width from page size calculations */
Keith Huie089a3f2011-08-02 22:28:14 -0400818 if (value) {
Keith Hui59356ca2010-03-06 18:16:25 +0000819 value = spd_read_byte(device,
820 SPD_ERROR_CHECKING_SDRAM_WIDTH);
821 width -= value;
822 /* ### ECC */
823 /* Clear top 2 bits to help set up NBXCFG. */
Keith Huie089a3f2011-08-02 22:28:14 -0400824 nbxecc &= 0x3f;
Keith Hui59356ca2010-03-06 18:16:25 +0000825 } else {
826 /* Without ECC, top 2 bits should be 11. */
Keith Huie089a3f2011-08-02 22:28:14 -0400827 nbxecc |= 0xc0;
Keith Hui59356ca2010-03-06 18:16:25 +0000828 }
829
Keith Huie089a3f2011-08-02 22:28:14 -0400830 /* If any installed DIMM is *not* registered, this system cannot be
831 * configured for registered SDRAM.
832 * By registered, only the address and control lines need to be, which
833 * we can tell by reading SPD byte 21, bit 1.
834 */
835 value = spd_read_byte(device, SPD_MODULE_ATTRIBUTES);
836
837 PRINT_DEBUG("DIMM is ");
838 if ((value & MODULE_REGISTERED) == 0) {
839 regsd = 0;
840 PRINT_DEBUG("not ");
841 }
842 PRINT_DEBUG("registered\n");
843
Keith Hui59356ca2010-03-06 18:16:25 +0000844 /* Calculate page size in bits. */
845 value = ((1 << col) * width);
846
847 /* Convert to KB. */
848 dra = (value >> 13);
849
850 /* Number of banks of DIMM (single or double sided). */
851 value = spd_read_byte(device, SPD_NUM_DIMM_BANKS);
852
853 /* Once we have dra, col is done and can be reused.
854 * So it's reused for number of banks.
855 */
856 col = spd_read_byte(device, SPD_NUM_BANKS_PER_SDRAM);
857
858 if (value == 1) {
859 /*
860 * Second bank of 1-bank DIMMs "doesn't have
861 * ECC" - or anything.
862 */
Keith Hui59356ca2010-03-06 18:16:25 +0000863 if (dra == 2) {
864 dra = 0x0; /* 2KB */
865 } else if (dra == 4) {
866 dra = 0x1; /* 4KB */
867 } else if (dra == 8) {
868 dra = 0x2; /* 8KB */
Anders Jenbo771b0e42010-04-27 08:45:30 +0000869 } else if (dra >= 16) {
870 /* Page sizes larger than supported are
871 * set to 8KB to use module partially.
872 */
873 PRINT_DEBUG("Page size forced to 8KB.\n");
874 dra = 0x2; /* 8KB */
Keith Hui59356ca2010-03-06 18:16:25 +0000875 } else {
876 dra = -1;
877 }
878 /*
879 * Sets a flag in PGPOL[BPR] if this DIMM has
880 * 4 banks per row.
881 */
882 if (col == 4)
883 bpr |= 0x40;
884 } else if (value == 2) {
885 if (dra == 2) {
886 dra = 0x0; /* 2KB */
887 } else if (dra == 4) {
888 dra = 0x05; /* 4KB */
889 } else if (dra == 8) {
890 dra = 0x0a; /* 8KB */
Anders Jenbo771b0e42010-04-27 08:45:30 +0000891 } else if (dra >= 16) {
892 /* Ditto */
893 PRINT_DEBUG("Page size forced to 8KB.\n");
894 dra = 0x0a; /* 8KB */
Keith Hui59356ca2010-03-06 18:16:25 +0000895 } else {
896 dra = -1;
897 }
898 /* Ditto */
899 if (col == 4)
900 bpr |= 0xc0;
901 } else {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800902 printk(BIOS_ERR, "# of banks of DIMM unsupported!\n");
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000903 die("HALT\n");
Keith Hui59356ca2010-03-06 18:16:25 +0000904 }
905 if (dra == -1) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800906 printk(BIOS_ERR, "Page size not supported\n");
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000907 die("HALT\n");
Keith Hui59356ca2010-03-06 18:16:25 +0000908 }
909
910 /*
911 * 440BX supports asymmetrical dual-sided DIMMs,
912 * but can't handle DIMMs smaller than 8MB per
Anders Jenbo771b0e42010-04-27 08:45:30 +0000913 * side.
Keith Hui59356ca2010-03-06 18:16:25 +0000914 */
915 struct dimm_size sz = spd_get_dimm_size(device);
916 if ((sz.side1 < 8)) {
Stefan Reinauer65b72ab2015-01-05 12:59:54 -0800917 printk(BIOS_ERR, "DIMMs smaller than 8MB per side\n"
Stefan Reinauer64ed2b72010-03-31 14:47:43 +0000918 "are not supported on this NB.\n");
919 die("HALT\n");
Keith Hui59356ca2010-03-06 18:16:25 +0000920 }
Keith Hui59356ca2010-03-06 18:16:25 +0000921
922 /* Divide size by 8 to set up the DRB registers. */
923 drb += (sz.side1 / 8);
924
925 /*
926 * Build the DRB for the next row in MSB so it gets
927 * placed in DRB[n+1] where it belongs when written
928 * as a 16-bit word.
929 */
930 drb &= 0xff;
931 drb |= (drb + (sz.side2 / 8)) << 8;
932 } else {
Keith Hui59356ca2010-03-06 18:16:25 +0000933 /* If there's no DIMM in the slot, set dra to 0x00. */
934 dra = 0x00;
Keith Hui59356ca2010-03-06 18:16:25 +0000935 /* Still have to propagate DRB over. */
936 drb &= 0xff;
937 drb |= (drb << 8);
938 }
939
940 pci_write_config16(NB, DRB + (2 * i), drb);
Keith Hui59356ca2010-03-06 18:16:25 +0000941
942 /* Brings the upper DRB back down to be base for
943 * DRB calculations for the next two rows.
944 */
945 drb >>= 8;
946
947 rps |= (dra & 0x0f) << (i * 4);
Keith Hui59356ca2010-03-06 18:16:25 +0000948 }
949
950 /* Set paging policy register. */
951 pci_write_config8(NB, PGPOL + 1, bpr);
Keith Hui09f5a742010-12-23 17:12:03 +0000952 PRINT_DEBUG("PGPOL[BPR] has been set to 0x%02x\n", bpr);
Keith Hui59356ca2010-03-06 18:16:25 +0000953
954 /* Set DRAM row page size register. */
955 pci_write_config16(NB, RPS, rps);
Keith Hui09f5a742010-12-23 17:12:03 +0000956 PRINT_DEBUG("RPS has been set to 0x%04x\n", rps);
Keith Hui59356ca2010-03-06 18:16:25 +0000957
958 /* ### ECC */
959 pci_write_config8(NB, NBXCFG + 3, nbxecc);
Keith Hui09f5a742010-12-23 17:12:03 +0000960 PRINT_DEBUG("NBXECC[31:24] has been set to 0x%02x\n", nbxecc);
Keith Hui59356ca2010-03-06 18:16:25 +0000961
Keith Huie089a3f2011-08-02 22:28:14 -0400962 /* Set DRAMC[4:3] to proper memory type (EDO/SDRAM/Registered SDRAM). */
Keith Hui59356ca2010-03-06 18:16:25 +0000963
Keith Huie089a3f2011-08-02 22:28:14 -0400964 /* i will be used to set DRAMC[4:3]. */
965 if (regsd && sd) {
966 i = 0x10; // Registered SDRAM
967 } else if (sd) {
968 i = 0x08; // SDRAM
969 } else {
970 i = 0; // EDO
971 }
972
Keith Hui59356ca2010-03-06 18:16:25 +0000973 value = pci_read_config8(NB, DRAMC) & 0xe7;
Keith Huie089a3f2011-08-02 22:28:14 -0400974 value |= i;
Keith Hui59356ca2010-03-06 18:16:25 +0000975 pci_write_config8(NB, DRAMC, value);
Keith Hui09f5a742010-12-23 17:12:03 +0000976 PRINT_DEBUG("DRAMC has been set to 0x%02x\n", value);
Keith Hui59356ca2010-03-06 18:16:25 +0000977}
978
Uwe Hermann115c5b92010-10-09 17:00:18 +0000979void sdram_set_spd_registers(void)
Richard Smithcb8eab42006-07-24 04:25:47 +0000980{
Keith Hui59356ca2010-03-06 18:16:25 +0000981 /* Setup DRAM row boundary registers and other attributes. */
982 set_dram_row_attributes();
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000983
Keith Huidf35cdc2010-09-20 23:41:37 +0000984 /* Setup DRAM buffer strength. */
Keith Hui59356ca2010-03-06 18:16:25 +0000985 set_dram_buffer_strength();
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000986
987 /* TODO: Set PMCR? */
Uwe Hermann1683cef2008-11-27 00:47:07 +0000988 // pci_write_config8(NB, PMCR, 0x14);
989 pci_write_config8(NB, PMCR, 0x10);
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000990
Keith Huidf35cdc2010-09-20 23:41:37 +0000991 /* TODO: This is for EDO memory only. */
Uwe Hermann1683cef2008-11-27 00:47:07 +0000992 pci_write_config8(NB, DRAMT, 0x03);
Richard Smithcb8eab42006-07-24 04:25:47 +0000993}
994
Uwe Hermann115c5b92010-10-09 17:00:18 +0000995void sdram_enable(void)
Richard Smithcb8eab42006-07-24 04:25:47 +0000996{
Uwe Hermann1a9c8922007-04-01 17:24:03 +0000997 int i;
Richard Smithcb8eab42006-07-24 04:25:47 +0000998
Uwe Hermann861f9642007-05-28 14:37:06 +0000999 /* 0. Wait until power/voltages and clocks are stable (200us). */
1000 udelay(200);
Richard Smithcb8eab42006-07-24 04:25:47 +00001001
Uwe Hermann861f9642007-05-28 14:37:06 +00001002 /* 1. Apply NOP. Wait 200 clock cycles (200us should do). */
Stefan Reinauer64ed2b72010-03-31 14:47:43 +00001003 PRINT_DEBUG("RAM Enable 1: Apply NOP\n");
Uwe Hermann8b643cea2008-12-09 16:36:12 +00001004 do_ram_command(RAM_COMMAND_NOP);
Uwe Hermann861f9642007-05-28 14:37:06 +00001005 udelay(200);
Richard Smithcb8eab42006-07-24 04:25:47 +00001006
Uwe Hermann1a9c8922007-04-01 17:24:03 +00001007 /* 2. Precharge all. Wait tRP. */
Stefan Reinauer64ed2b72010-03-31 14:47:43 +00001008 PRINT_DEBUG("RAM Enable 2: Precharge all\n");
Uwe Hermann8b643cea2008-12-09 16:36:12 +00001009 do_ram_command(RAM_COMMAND_PRECHARGE);
Uwe Hermann861f9642007-05-28 14:37:06 +00001010 udelay(1);
Richard Smithcb8eab42006-07-24 04:25:47 +00001011
Uwe Hermann1a9c8922007-04-01 17:24:03 +00001012 /* 3. Perform 8 refresh cycles. Wait tRC each time. */
Stefan Reinauer64ed2b72010-03-31 14:47:43 +00001013 PRINT_DEBUG("RAM Enable 3: CBR\n");
Uwe Hermann1a9c8922007-04-01 17:24:03 +00001014 for (i = 0; i < 8; i++) {
Uwe Hermann8b643cea2008-12-09 16:36:12 +00001015 do_ram_command(RAM_COMMAND_CBR);
Uwe Hermann861f9642007-05-28 14:37:06 +00001016 udelay(1);
Richard Smithcb8eab42006-07-24 04:25:47 +00001017 }
Uwe Hermann1a9c8922007-04-01 17:24:03 +00001018
1019 /* 4. Mode register set. Wait two memory cycles. */
Stefan Reinauer64ed2b72010-03-31 14:47:43 +00001020 PRINT_DEBUG("RAM Enable 4: Mode register set\n");
Uwe Hermann8b643cea2008-12-09 16:36:12 +00001021 do_ram_command(RAM_COMMAND_MRS);
Uwe Hermann861f9642007-05-28 14:37:06 +00001022 udelay(2);
Uwe Hermann1a9c8922007-04-01 17:24:03 +00001023
1024 /* 5. Normal operation. */
Stefan Reinauer64ed2b72010-03-31 14:47:43 +00001025 PRINT_DEBUG("RAM Enable 5: Normal operation\n");
Uwe Hermann8b643cea2008-12-09 16:36:12 +00001026 do_ram_command(RAM_COMMAND_NORMAL);
Uwe Hermann861f9642007-05-28 14:37:06 +00001027 udelay(1);
Uwe Hermann1a9c8922007-04-01 17:24:03 +00001028
1029 /* 6. Finally enable refresh. */
Stefan Reinauer64ed2b72010-03-31 14:47:43 +00001030 PRINT_DEBUG("RAM Enable 6: Enable refresh\n");
Uwe Hermann1683cef2008-11-27 00:47:07 +00001031 // pci_write_config8(NB, PMCR, 0x10);
1032 spd_enable_refresh();
Uwe Hermann861f9642007-05-28 14:37:06 +00001033 udelay(1);
Uwe Hermann1a9c8922007-04-01 17:24:03 +00001034
Stefan Reinauer64ed2b72010-03-31 14:47:43 +00001035 PRINT_DEBUG("Northbridge following SDRAM init:\n");
Uwe Hermann1a9c8922007-04-01 17:24:03 +00001036 DUMPNORTH();
Richard Smithcb8eab42006-07-24 04:25:47 +00001037}
Keith Hui078e3242017-07-20 21:14:21 -04001038
1039void sdram_initialize(void)
1040{
1041 dump_spd_registers();
1042 sdram_set_registers();
1043 sdram_set_spd_registers();
1044 sdram_enable();
Martin Rothe1695e22017-07-24 11:28:50 -06001045}