Lower the RAM init delays we use on the Intel 440BX.
[coreboot.git] / src / northbridge / intel / i440bx / raminit.c
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <spd.h>
22 #include <sdram_mode.h>
23 #include <delay.h>
24 #include "i440bx.h"
25
26 /*-----------------------------------------------------------------------------
27 Macros and definitions.
28 -----------------------------------------------------------------------------*/
29
30 /* Uncomment this to enable debugging output. */
31 #define DEBUG_RAM_SETUP 1
32
33 /* Debugging macros. */
34 #if defined(DEBUG_RAM_SETUP)
35 #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))
40 #else
41 #define PRINT_DEBUG(x)
42 #define PRINT_DEBUG_HEX8(x)
43 #define PRINT_DEBUG_HEX16(x)
44 #define PRINT_DEBUG_HEX32(x)
45 #define DUMPNORTH()
46 #endif
47
48 /* SDRAMC[7:5] - SDRAM Mode Select (SMS). */
49 #define RAM_COMMAND_NORMAL      0x0
50 #define RAM_COMMAND_NOP         0x1
51 #define RAM_COMMAND_PRECHARGE   0x2
52 #define RAM_COMMAND_MRS         0x3
53 #define RAM_COMMAND_CBR         0x4
54
55 /* Map the JEDEC SPD refresh rates (array index) to 440BX refresh rates as
56  * defined in DRAMC[2:0].
57  *
58  * [0] == Normal        15.625 us ->  15.6 us
59  * [1] == Reduced(.25X)    3.9 us ->   7.8 ns
60  * [2] == Reduced(.5X)     7.8 us ->   7.8 us
61  * [3] == Extended(2x)    31.3 us ->  31.2 us
62  * [4] == Extended(4x)    62.5 us ->  62.4 us
63  * [5] == Extended(8x)     125 us -> 124.8 us
64  */
65 static const uint32_t refresh_rate_map[] = {
66         1, 5, 5, 2, 3, 4
67 };
68
69 /* Table format: register, bitmask, value. */
70 static const long register_values[] = {
71         /* NBXCFG - NBX Configuration Register
72          * 0x50 - 0x53
73          *
74          * [31:24] SDRAM Row Without ECC
75          *         0 = ECC components are populated in this row
76          *         1 = ECC components are not populated in this row
77          * [23:19] Reserved
78          * [18:18] Host Bus Fast Data Ready Enable (HBFDRE)
79          *         Assertion of DRAM data on host bus occurs...
80          *         0 = ...one clock after sampling snoop results (default)
81          *         1 = ...on the same clock the snoop result is being sampled
82          *             (this mode is faster by one clock cycle)
83          * [17:17] ECC - EDO static Drive mode
84          *         0 = Normal mode (default)
85          *         1 = ECC signals are always driven
86          * [16:16] IDSEL_REDIRECT
87          *         0 = IDSEL1 is allocated to this bridge (default)
88          *         1 = IDSEL7 is allocated to this bridge
89          * [15:15] WSC# Handshake Disable
90          *         1 = Uni-processor mode
91          *         0 = Dual-processor mode with external IOAPIC (default)
92          * [14:14] Intel Reserved
93          * [13:12] Host/DRAM Frequency
94          *         00 = 100 MHz
95          *         01 = Reserved
96          *         10 = 66 MHz
97          *         11 = Reserved
98          * [11:11] AGP to PCI Access Enable
99          *         1 = Enable
100          *         0 = Disable
101          * [10:10] PCI Agent to Aperture Access Disable
102          *         1 = Disable
103          *         0 = Enable (default)
104          * [09:09] Aperture Access Global Enable
105          *         1 = Enable
106          *         0 = Disable
107          * [08:07] DRAM Data Integrity Mode (DDIM)
108          *         00 = Non-ECC
109          *         01 = EC-only
110          *         10 = ECC Mode
111          *         11 = ECC Mode with hardware scrubbing enabled
112          * [06:06] ECC Diagnostic Mode Enable (EDME)
113          *         1 = Enable
114          *         0 = Normal operation mode (default)
115          * [05:05] MDA Present (MDAP)
116          *         Works in conjunction with the VGA_EN bit.
117          *         VGA_EN MDAP
118          *           0     x   All VGA cycles are sent to PCI
119          *           1     0   All VGA cycles are sent to AGP
120          *           1     1   All VGA cycles are sent to AGP, except for
121          *                     cycles in the MDA range.
122          * [04:04] Reserved
123          * [03:03] USWC Write Post During I/O Bridge Access Enable (UWPIO)
124          *         1 = Enable
125          *         0 = Disable
126          * [02:02] In-Order Queue Depth (IOQD)
127          *         1 = In-order queue = maximum
128          *         0 = A7# is sampled asserted (i.e., 0)
129          * [01:00] Reserved
130          */
131         // TODO
132         NBXCFG, 0x00000000, 0xff00a00c,
133
134         /* DRAMC - DRAM Control Register
135          * 0x57
136          *
137          * [7:6] Reserved
138          * [5:5] Module Mode Configuration (MMCONFIG)
139          *       TODO
140          * [4:3] DRAM Type (DT)
141          *       00 = EDO
142          *       01 = SDRAM
143          *       10 = Registered SDRAM
144          *       11 = Reserved
145          *       Note: EDO, SDRAM and Registered SDRAM cannot be mixed.
146          * [2:0] DRAM Refresh Rate (DRR)
147          *       000 = Refresh disabled
148          *       001 = 15.6 us
149          *       010 = 31.2 us
150          *       011 = 62.4 us
151          *       100 = 124.8 us
152          *       101 = 249.6 us
153          *       110 = Reserved
154          *       111 = Reserved
155          */
156         /* Choose SDRAM (not registered), and disable refresh for now. */
157         DRAMC, 0x00, 0x8,
158
159         /*
160          * PAM[6:0] - Programmable Attribute Map Registers
161          * 0x59 - 0x5f
162          *
163          * 0x59 [3:0] Reserved
164          * 0x59 [5:4] 0xF0000 - 0xFFFFF BIOS area
165          * 0x5a [1:0] 0xC0000 - 0xC3FFF ISA add-on BIOS
166          * 0x5a [5:4] 0xC4000 - 0xC7FFF ISA add-on BIOS
167          * 0x5b [1:0] 0xC8000 - 0xCBFFF ISA add-on BIOS
168          * 0x5b [5:4] 0xCC000 - 0xCFFFF ISA add-on BIOS
169          * 0x5c [1:0] 0xD0000 - 0xD3FFF ISA add-on BIOS
170          * 0x5c [5:4] 0xD4000 - 0xD7FFF ISA add-on BIOS
171          * 0x5d [1:0] 0xD8000 - 0xDBFFF ISA add-on BIOS
172          * 0x5d [5:4] 0xDC000 - 0xDFFFF ISA add-on BIOS
173          * 0x5e [1:0] 0xE0000 - 0xE3FFF BIOS entension
174          * 0x5e [5:4] 0xE4000 - 0xE7FFF BIOS entension
175          * 0x5f [1:0] 0xE8000 - 0xEBFFF BIOS entension
176          * 0x5f [5:4] 0xEC000 - 0xEFFFF BIOS entension
177          *
178          * Bit assignment:
179          * 00 = DRAM Disabled (all access goes to memory mapped I/O space)
180          * 01 = Read Only (Reads to DRAM, writes to memory mapped I/O space)
181          * 10 = Write Only (Writes to DRAM, reads to memory mapped I/O space)
182          * 11 = Read/Write (all access goes to DRAM)
183          */
184         // TODO
185         PAM0, 0x00, 0x00,
186         PAM1, 0x00, 0x00,
187         PAM2, 0x00, 0x00,
188         PAM3, 0x00, 0x00,
189         PAM4, 0x00, 0x00,
190         PAM5, 0x00, 0x00,
191         PAM6, 0x00, 0x00,
192
193         /* DRB[0:7] - DRAM Row Boundary Registers
194          * 0x60 - 0x67
195          *
196          * An array of 8 byte registers, which hold the ending memory address
197          * assigned to each pair of DIMMs, in 8MB granularity.   
198          *
199          * 0x60 DRB0 = Total memory in row0 (in 8 MB)
200          * 0x61 DRB1 = Total memory in row0+1 (in 8 MB)
201          * 0x62 DRB2 = Total memory in row0+1+2 (in 8 MB)
202          * 0x63 DRB3 = Total memory in row0+1+2+3 (in 8 MB)
203          * 0x64 DRB4 = Total memory in row0+1+2+3+4 (in 8 MB)
204          * 0x65 DRB5 = Total memory in row0+1+2+3+4+5 (in 8 MB)
205          * 0x66 DRB6 = Total memory in row0+1+2+3+4+5+6 (in 8 MB)
206          * 0x67 DRB7 = Total memory in row0+1+2+3+4+5+6+7 (in 8 MB)
207          */
208         /* Set the DRBs to zero for now, this will be fixed later. */
209         DRB0, 0x00, 0x00,
210         DRB1, 0x00, 0x00,
211         DRB2, 0x00, 0x00,
212         DRB3, 0x00, 0x00,
213         DRB4, 0x00, 0x00,
214         DRB5, 0x00, 0x00,
215         DRB6, 0x00, 0x00,
216         DRB7, 0x00, 0x00,
217
218         /* FDHC - Fixed DRAM Hole Control Register
219          * 0x68
220          *
221          * Controls two fixed DRAM holes: 512 KB - 640 KB and 15 MB - 16 MB.
222          *
223          * [7:6] Hole Enable (HEN)
224          *       00 = None
225          *       01 = 512 KB - 640 KB (128 KB)
226          *       10 = 15 MB - 16 MB (1 MB)
227          *       11 = Reserved
228          * [5:0] Reserved
229          */
230         /* No memory holes. */
231         FDHC, 0x00, 0x00,
232
233         /* RPS - SDRAM Row Page Size Register
234          * 0x74 - 0x75
235          *
236          * Sets the row page size for SDRAM. For EDO memory, the page
237          * size is fixed at 2 KB.
238          *
239          * [15:0] Page Size (PS)
240          *        TODO
241          */
242         // TODO
243         RPS, 0x0000, 0x0000,
244
245         /* SDRAMC - SDRAM Control Register
246          * 0x76 - 0x77
247          *
248          * [15:10] Reserved
249          * [09:08] Idle/Pipeline DRAM Leadoff Timing (IPDLT)
250          *         00 = Illegal
251          *         01 = Add a clock delay to the lead-off clock count
252          *         10 = Illegal
253          *         11 = Illegal
254          * [07:05] SDRAM Mode Select (SMS)
255          *         000 = Normal SDRAM Operation (default)
256          *         001 = NOP Command Enable
257          *         010 = All Banks Precharge Enable
258          *         011 = Mode Register Set Enable
259          *         100 = CBR Enable
260          *         101 = Reserved
261          *         110 = Reserved
262          *         111 = Reserved
263          * [04:04] SDRAMPWR
264          *         0 = 3 DIMM configuration
265          *         1 = 4 DIMM configuration
266          * [03:03] Leadoff Command Timing (LCT)
267          *         0 = 4 CS# Clock
268          *         1 = 3 CS# Clock
269          * [02:02] CAS# Latency (CL)
270          *         0 = 3 DCLK CAS# latency
271          *         1 = 2 DCLK CAS# latency
272          * [01:01] SDRAM RAS# to CAS# Delay (SRCD)
273          *         0 = 3 clocks between a row activate and a read or write cmd.
274          *         1 = 2 clocks between a row activate and a read or write cmd.
275          * [00:00] SDRAM RAS# Precharge (SRP)
276          *         0 = 3 clocks of RAS# precharge
277          *         1 = 2 clocks of RAS# precharge
278          */
279         SDRAMC, 0x0000, 0x0000,
280
281         /* PGPOL - Paging Policy Register
282          * 0x78 - 0x79
283          *
284          * [15:08] Banks per Row (BPR)
285          *         TODO
286          *         0 = 2 banks
287          *         1 = 4 banks
288          * [07:05] Reserved
289          * [04:04] Intel Reserved
290          * [03:00] DRAM Idle Timer (DIT)
291          *         0000 = 0 clocks
292          *         0001 = 2 clocks
293          *         0010 = 4 clocks
294          *         0011 = 8 clocks
295          *         0100 = 10 clocks
296          *         0101 = 12 clocks
297          *         0110 = 16 clocks
298          *         0111 = 32 clocks
299          *         1xxx = Infinite (pages are not closed for idle condition)
300          */
301         // TODO
302         PGPOL, 0x0000, 0xff00,
303
304         /* PMCR - Power Management Control Register
305          * 0x7a
306          *
307          * [07:07] Power Down SDRAM Enable (PDSE)
308          *         1 = Enable
309          *         0 = Disable
310          * [06:06] ACPI Control Register Enable (SCRE)
311          *         1 = Enable
312          *         0 = Disable (default)
313          * [05:05] Suspend Refresh Type (SRT)
314          *         1 = Self refresh mode
315          *         0 = CBR fresh mode
316          * [04:04] Normal Refresh Enable (NREF_EN)
317          *         1 = Enable
318          *         0 = Disable
319          * [03:03] Quick Start Mode (QSTART)
320          *         1 = Quick start mode for the processor is enabled
321          * [02:02] Gated Clock Enable (GCLKEN)
322          *         1 = Enable
323          *         0 = Disable
324          * [01:01] AGP Disable (AGP_DIS)
325          *         1 = Disable
326          *         0 = Enable
327          * [00:00] CPU reset without PCIRST enable (CRst_En)
328          *         1 = Enable
329          *         0 = Disable
330          */
331         /* Enable normal refresh and the gated clock. */
332         // TODO: Only do this later?
333         // PMCR, 0x00, 0x14,
334         // PMCR, 0x00, 0x10,
335         PMCR, 0x00, 0x00,
336 };
337
338 /*-----------------------------------------------------------------------------
339 SDRAM configuration functions.
340 -----------------------------------------------------------------------------*/
341
342 /**
343  * Send the specified RAM command to all DIMMs.
344  *
345  * @param Memory controller
346  * @param TODO
347  * @param TODO
348  */
349 static void do_ram_command(const struct mem_controller *ctrl,
350                            uint32_t command, uint32_t addr_offset)
351 {
352         int i;
353         uint16_t reg;
354
355         /* TODO: Support for multiple DIMMs. */
356
357         /* Configure the RAM command. */
358         reg = pci_read_config16(ctrl->d0, SDRAMC);
359         reg &= 0xff1f;          /* Clear bits 7-5. */
360         reg |= (uint16_t) (command << 5);
361         pci_write_config16(ctrl->d0, SDRAMC, reg);
362
363         /* RAM_COMMAND_NORMAL affects only the memory controller and
364            doesn't need to be "sent" to the DIMMs. */
365         /* if (command == RAM_COMMAND_NORMAL) return; */
366
367         PRINT_DEBUG("    Sending RAM command 0x");
368         PRINT_DEBUG_HEX16(reg);
369         PRINT_DEBUG(" to 0x");
370         PRINT_DEBUG_HEX32(0 + addr_offset); // FIXME
371         PRINT_DEBUG("\r\n");
372
373         /* Read from (DIMM start address + addr_offset). */
374         read32(0 + addr_offset); // FIXME
375 }
376
377 /*-----------------------------------------------------------------------------
378 DIMM-independant configuration functions.
379 -----------------------------------------------------------------------------*/
380
381 /**
382  * TODO.
383  *
384  * @param Memory controller
385  */
386 static void spd_enable_refresh(const struct mem_controller *ctrl)
387 {
388         int i, value;
389         uint8_t reg;
390
391         reg = pci_read_config8(ctrl->d0, DRAMC);
392
393         for (i = 0; i < DIMM_SOCKETS; i++) {
394                 value = spd_read_byte(ctrl->channel0[i], SPD_REFRESH);
395                 if (value < 0)
396                         continue;
397                 reg = (reg & 0xf8) | refresh_rate_map[(value & 0x7f)];
398
399                 PRINT_DEBUG("    Enabling refresh (DRAMC = 0x");
400                 PRINT_DEBUG_HEX8(reg);
401                 PRINT_DEBUG(") for DIMM ");
402                 PRINT_DEBUG_HEX8(i);
403                 PRINT_DEBUG("\r\n");
404         }
405
406         pci_write_config8(ctrl->d0, DRAMC, reg);
407 }
408
409 /*-----------------------------------------------------------------------------
410 Public interface.
411 -----------------------------------------------------------------------------*/
412
413 /**
414  * TODO.
415  *
416  * @param Memory controller
417  */
418 static void sdram_set_registers(const struct mem_controller *ctrl)
419 {
420         int i, max;
421         uint32_t reg;
422
423         PRINT_DEBUG("Northbridge prior to SDRAM init:\r\n");
424         DUMPNORTH();
425
426         max = sizeof(register_values) / sizeof(register_values[0]);
427
428         /* Set registers as specified in the register_values[] array. */
429         for (i = 0; i < max; i += 3) {
430                 reg = pci_read_config32(ctrl->d0, register_values[i]);
431                 reg &= register_values[i + 1];
432                 reg |= register_values[i + 2] & ~(register_values[i + 1]);
433                 pci_write_config32(ctrl->d0, register_values[i], reg);
434
435                 PRINT_DEBUG("    Set register 0x");
436                 PRINT_DEBUG_HEX32(register_values[i]);
437                 PRINT_DEBUG(" to 0x");
438                 PRINT_DEBUG_HEX32(reg);
439                 PRINT_DEBUG("\r\n");
440         }
441 }
442
443 /**
444  * TODO.
445  *
446  * @param Memory controller
447  */
448 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
449 {
450         /* TODO: Don't hardcode the values here, get info via SPD. */
451
452         /* Map all legacy regions to RAM (read/write). This is required if
453          * you want to use the RAM area from 768 KB - 1 MB. If the PAM
454          * registers are not set here appropriately, the RAM in that region
455          * will not be accessible, thus a RAM check of it will also fail.
456          */
457         pci_write_config8(ctrl->d0, PAM0, 0x30);
458         pci_write_config8(ctrl->d0, PAM1, 0x33);
459         pci_write_config8(ctrl->d0, PAM2, 0x33);
460         pci_write_config8(ctrl->d0, PAM3, 0x33);
461         pci_write_config8(ctrl->d0, PAM4, 0x33);
462         pci_write_config8(ctrl->d0, PAM5, 0x33);
463         pci_write_config8(ctrl->d0, PAM6, 0x33);
464
465         /* TODO: Set DRB0-DRB7. */
466         /* Currently this is hardcoded to one 64 MB DIMM in slot 0. */
467         pci_write_config8(ctrl->d0, DRB0, 0x08);
468         pci_write_config8(ctrl->d0, DRB1, 0x08);
469         pci_write_config8(ctrl->d0, DRB2, 0x08);
470         pci_write_config8(ctrl->d0, DRB3, 0x08);
471         pci_write_config8(ctrl->d0, DRB4, 0x08);
472         pci_write_config8(ctrl->d0, DRB5, 0x08);
473         pci_write_config8(ctrl->d0, DRB6, 0x08);
474         pci_write_config8(ctrl->d0, DRB7, 0x08);
475
476         /* TODO: Set DRAMC. Don't enable refresh for now. */
477         pci_write_config8(ctrl->d0, DRAMC, 0x08);
478
479         /* TODO: Set RPS. */
480         pci_write_config16(ctrl->d0, RPS, 0x0001);
481
482         /* TODO: Set SDRAMC. */
483         // pci_write_config16(ctrl->d0, SDRAMC, 0x0000);
484
485         /* TODO: Set PGPOL. */
486         pci_write_config16(ctrl->d0, PGPOL, 0x0107);
487
488         /* TODO: Set NBXCFG. */
489         // pci_write_config32(ctrl->d0, NBXCFG, 0x0100220c);
490
491         /* TODO: Set PMCR? */
492         // pci_write_config8(ctrl->d0, PMCR, 0x14);
493         // pci_write_config8(ctrl->d0, PMCR, 0x10);
494
495         /* TODO? */
496         // pci_write_config8(ctrl->d0, MLT, 0x40);
497         // pci_write_config8(ctrl->d0, DRAMT, 0x03);
498         // pci_write_config8(ctrl->d0, MBSC, 0x03);
499         // pci_write_config8(ctrl->d0, SCRR, 0x38);
500 }
501
502 /**
503  * Enable SDRAM.
504  *
505  * @param Number of controllers
506  * @param Memory controller
507  */
508 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
509 {
510         int i;
511
512         /* 0. Wait until power/voltages and clocks are stable (200us). */
513         udelay(200);
514
515         /* 1. Apply NOP. Wait 200 clock cycles (200us should do). */
516         PRINT_DEBUG("RAM Enable 1: Apply NOP\r\n");
517         do_ram_command(ctrl, RAM_COMMAND_NOP, 0);
518         udelay(200);
519
520         /* 2. Precharge all. Wait tRP. */
521         PRINT_DEBUG("RAM Enable 2: Precharge all\r\n");
522         do_ram_command(ctrl, RAM_COMMAND_PRECHARGE, 0);
523         udelay(1);
524
525         /* 3. Perform 8 refresh cycles. Wait tRC each time. */
526         PRINT_DEBUG("RAM Enable 3: CBR\r\n");
527         for (i = 0; i < 8; i++) {
528                 do_ram_command(ctrl, RAM_COMMAND_CBR, 0);
529                 udelay(1);
530         }
531
532         /* 4. Mode register set. Wait two memory cycles. */
533         PRINT_DEBUG("RAM Enable 4: Mode register set\r\n");
534         do_ram_command(ctrl, RAM_COMMAND_MRS, 0x1d0);
535         udelay(2);
536
537         /* 5. Normal operation. */
538         PRINT_DEBUG("RAM Enable 5: Normal operation\r\n");
539         do_ram_command(ctrl, RAM_COMMAND_NORMAL, 0);
540         udelay(1);
541
542         /* 6. Finally enable refresh. */
543         PRINT_DEBUG("RAM Enable 6: Enable refresh\r\n");
544         // pci_write_config8(ctrl->d0, PMCR, 0x10);
545         spd_enable_refresh(ctrl);
546         udelay(1);
547
548         PRINT_DEBUG("Northbridge following SDRAM init:\r\n");
549         DUMPNORTH();
550 }