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