Drop i855pm port and rename i855gme to i855 instead.
[coreboot.git] / src / northbridge / intel / i855 / raminit.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2006 Jon Dufresne <jon.dufresne@gmail.com>
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 <sdram_mode.h>
22
23 #define dumpnorth() dump_pci_device(PCI_DEV(0, 0, 1))
24 #define VG85X_MODE (SDRAM_BURST_4 | SDRAM_BURST_INTERLEAVED | SDRAM_CAS_2_5)
25
26 /**
27   * Set only what I need until it works, then make it figure things out on boot
28   * assumes only one dimm is populated
29   */
30  
31 static void sdram_set_registers(const struct mem_controller *ctrl)
32 {
33         /*
34         print_debug("Before configuration:\r\n");
35         dump_pci_devices();
36         */
37 }
38
39 static void spd_set_row_attributes(const struct mem_controller *ctrl)
40 {
41         uint16_t dra_reg;
42         
43         dra_reg = 0x7733;
44         pci_write_config16(ctrl->d0, 0x50, dra_reg);
45 }
46
47 static void spd_set_dram_controller_mode(const struct mem_controller *ctrl)
48 {
49         uint32_t drc_reg;
50         
51         /* drc_reg = 0x00009101; */
52         drc_reg = 0x00009901;
53         pci_write_config32(ctrl->d0, 0x70, drc_reg);
54 }
55
56 static void spd_set_dram_timing(const struct mem_controller *ctrl)
57 {
58         uint32_t drt_reg;
59         
60         drt_reg = 0x2a004405;
61         pci_write_config32(ctrl->d0, 0x60, drt_reg);
62 }
63
64 static void spd_set_dram_size(const struct mem_controller *ctrl)
65 {
66         uint32_t drb_reg;
67
68         drb_reg = 0x20202010;
69         pci_write_config32(ctrl->d0, 0x40, drb_reg);
70 }
71
72 static void spd_set_dram_pwr_management(const struct mem_controller *ctrl)
73 {
74         uint32_t pwrmg_reg;
75         
76         pwrmg_reg = 0x10f10430;
77         pci_write_config32(ctrl->d0, 0x68, pwrmg_reg);
78 }
79
80 static void spd_set_dram_throttle_control(const struct mem_controller *ctrl)
81 {
82         uint32_t dtc_reg;
83
84         dtc_reg = 0x300aa2ff;
85         pci_write_config32(ctrl->d0, 0xa0, dtc_reg);
86 }
87
88 void udelay(int usecs)
89 {
90         int i;
91         for(i = 0; i < usecs; i++)
92                 outb(i & 0xff, 0x80);
93 }
94
95 #define delay() udelay(200)
96
97 /* if ram still doesn't work do this function */
98 static void spd_set_undocumented_registers(const struct mem_controller *ctrl)
99 {
100         uint16_t word;
101         
102         /* 0:0.0 */
103         /*
104         pci_write_config32(PCI_DEV(0, 0, 0), 0x10, 0xe0000008);
105         pci_write_config32(PCI_DEV(0, 0, 0), 0x2c, 0x35808086);
106         pci_write_config32(PCI_DEV(0, 0, 0), 0x48, 0xfec10000);
107         pci_write_config32(PCI_DEV(0, 0, 0), 0x50, 0x00440100);
108         
109         pci_write_config32(PCI_DEV(0, 0, 0), 0x58, 0x11111000);
110         
111         pci_write_config16(PCI_DEV(0, 0, 0), 0x52, 0x0002);
112         */
113         pci_write_config16(PCI_DEV(0, 0, 0), 0x52, 0x0044);
114         /*
115         pci_write_config16(PCI_DEV(0, 0, 0), 0x52, 0x0000);
116         */
117         pci_write_config32(PCI_DEV(0, 0, 0), 0x58, 0x33333000); 
118         pci_write_config32(PCI_DEV(0, 0, 0), 0x5c, 0x33333333);
119         /*
120         pci_write_config32(PCI_DEV(0, 0, 0), 0x60, 0x0000390a);
121         pci_write_config32(PCI_DEV(0, 0, 0), 0x74, 0x02006056);
122         pci_write_config32(PCI_DEV(0, 0, 0), 0x78, 0x00800001);
123         */
124         pci_write_config32(PCI_DEV(0, 0, 0), 0xa8, 0x00000001); 
125         
126         pci_write_config32(PCI_DEV(0, 0, 0), 0xbc, 0x00001020);
127         /*
128         pci_write_config32(PCI_DEV(0, 0, 0), 0xfc, 0x00000109);
129         */
130
131         /* 0:0.1 */
132         pci_write_config32(ctrl->d0, 0x74, 0x00000001); 
133         pci_write_config32(ctrl->d0, 0x78, 0x001fe974);
134         pci_write_config32(ctrl->d0, 0x80, 0x00af0039);
135         pci_write_config32(ctrl->d0, 0x84, 0x0000033c);
136         pci_write_config32(ctrl->d0, 0x88, 0x00000010);
137         pci_write_config32(ctrl->d0, 0x98, 0xde5a868c);
138         pci_write_config32(ctrl->d0, 0x9c, 0x404e0046);
139         pci_write_config32(ctrl->d0, 0xa8, 0x00020e1a);
140         pci_write_config32(ctrl->d0, 0xb4, 0x0044cdac);
141         pci_write_config32(ctrl->d0, 0xb8, 0x000055d4);
142         pci_write_config32(ctrl->d0, 0xbc, 0x024acd38);
143         pci_write_config32(ctrl->d0, 0xc0, 0x00000003);
144         
145         /* 0:0.3 */
146         /*
147         pci_write_config32(PCI_DEV(0, 0, 3), 0x2c, 0x35858086);
148         pci_write_config32(PCI_DEV(0, 0, 3), 0x44, 0x11110000);
149         pci_write_config32(PCI_DEV(0, 0, 3), 0x48, 0x09614a3c);
150         pci_write_config32(PCI_DEV(0, 0, 3), 0x4c, 0x4b09604a);
151         pci_write_config32(PCI_DEV(0, 0, 3), 0x50, 0x00000962);
152         pci_write_config32(PCI_DEV(0, 0, 3), 0x5c, 0x0b023331);
153         pci_write_config32(PCI_DEV(0, 0, 3), 0x6c, 0x0000402e);
154         pci_write_config32(PCI_DEV(0, 0, 3), 0x78, 0xe7c70f7f);
155         pci_write_config32(PCI_DEV(0, 0, 3), 0x7c, 0x0284007f);
156         pci_write_config32(PCI_DEV(0, 0, 3), 0x84, 0x000000ef);
157         */
158         
159         /*
160         pci_write_config16(PCI_DEV(0, 0, 3), 0xc0, 0x0200);
161         pci_write_config16(PCI_DEV(0, 0, 3), 0xc0, 0x0400);
162         */
163         
164         /*
165         pci_write_config32(PCI_DEV(0, 0, 3), 0xc4, 0x00000000);
166         pci_write_config32(PCI_DEV(0, 0, 3), 0xd8, 0xff00c308);
167         pci_write_config32(PCI_DEV(0, 0, 3), 0xdc, 0x00000025);
168         pci_write_config32(PCI_DEV(0, 0, 3), 0xe0, 0x001f002a);
169         pci_write_config32(PCI_DEV(0, 0, 3), 0xe4, 0x009f0098);
170         pci_write_config32(PCI_DEV(0, 0, 3), 0xec, 0x00000400);
171         pci_write_config32(PCI_DEV(0, 0, 3), 0xf0, 0xc0000000);
172         */
173 }
174
175 static void sdram_set_spd_registers(const struct mem_controller *ctrl)
176 {
177         spd_set_row_attributes(ctrl);
178         spd_set_dram_controller_mode(ctrl);
179         spd_set_dram_timing(ctrl);
180         spd_set_dram_size(ctrl);
181         spd_set_dram_pwr_management(ctrl);
182         spd_set_dram_throttle_control(ctrl);
183         spd_set_undocumented_registers(ctrl);
184 }
185
186 static void ram_command(const struct mem_controller *ctrl,
187                         uint8_t command,
188                         uint32_t addr)
189 {
190         uint32_t drc_reg;
191         
192         drc_reg = pci_read_config32(ctrl->d0, 0x70);
193         drc_reg &= ~(7 << 4);
194         drc_reg |= (command << 4);
195         pci_write_config8(ctrl->d0, 0x70, drc_reg);
196         read32(addr);
197 }
198
199 static void ram_command_mrs(const struct mem_controller *ctrl,
200                                                         uint8_t command,
201                                                         uint32_t mode,
202                                                         uint32_t addr)
203 {
204         uint32_t drc_reg;
205         uint32_t adjusted_mode;
206         
207         drc_reg = pci_read_config32(ctrl->d0, 0x70);
208         drc_reg &= ~(7 << 4);
209         drc_reg |= (command << 4);
210         pci_write_config8(ctrl->d0, 0x70, drc_reg);
211         /* Host address lines [13:3] map to DIMM address lines [11, 9:0] */     
212         adjusted_mode = ((mode & 0x800) << (13 - 11)) | ((mode & 0x3ff) << (12 - 9));
213         print_debug("Setting mode: ");
214         print_debug_hex32(adjusted_mode + addr);
215         print_debug("\r\n");
216         read32(adjusted_mode + addr);
217 }
218
219 static void set_initialize_complete(const struct mem_controller *ctrl)
220 {
221         uint32_t drc_reg;
222         
223         drc_reg = pci_read_config32(ctrl->d0, 0x70);
224         drc_reg |= (1 << 29);
225         pci_write_config32(ctrl->d0, 0x70, drc_reg);
226 }
227
228 static void sdram_enable(int controllers, const struct mem_controller *ctrl)
229 {
230         int i;
231         uint32_t rank1 = (1 << 30) / 2;
232         print_debug("Ram enable 1\r\n");
233         delay();
234         delay();
235         
236         print_debug("Ram enable 2\r\n");
237         ram_command(ctrl, 1, 0);
238         ram_command(ctrl, 1, rank1);
239         delay();
240         delay();
241
242         print_debug("Ram enable 3\r\n");
243         ram_command(ctrl, 2, 0);
244         ram_command(ctrl, 2, rank1);
245         delay();
246         delay();
247
248         print_debug("Ram enable 4\r\n");
249         ram_command_mrs(ctrl, 4, SDRAM_EXTMODE_DLL_ENABLE, 0);
250         ram_command_mrs(ctrl, 4, SDRAM_EXTMODE_DLL_ENABLE, rank1);
251         delay();
252         delay();
253         
254         print_debug("Ram enable 5\r\n");
255         ram_command_mrs(ctrl, 3, VG85X_MODE | SDRAM_MODE_DLL_RESET, 0);
256         ram_command_mrs(ctrl, 3, VG85X_MODE | SDRAM_MODE_DLL_RESET, rank1);
257         
258         print_debug("Ram enable 6\r\n");
259         ram_command(ctrl, 2, 0);
260         ram_command(ctrl, 2, rank1);
261         delay();
262         delay();
263         
264         print_debug("Ram enable 7\r\n");
265         for(i = 0; i < 8; i++) {
266                 ram_command(ctrl, 6, 0);
267                 ram_command(ctrl, 6, rank1);
268                 delay();
269                 delay();
270         }
271
272         print_debug("Ram enable 8\r\n");
273         ram_command_mrs(ctrl, 3, VG85X_MODE | SDRAM_MODE_NORMAL, 0);
274         ram_command_mrs(ctrl, 3, VG85X_MODE | SDRAM_MODE_NORMAL, rank1);
275
276         print_debug("Ram enable 9\r\n");
277         ram_command(ctrl, 7, 0);
278         ram_command(ctrl, 7, rank1);
279         delay();
280         delay();
281         
282         print_debug("Ram enable 9\r\n");
283         set_initialize_complete(ctrl);
284         
285         delay();
286         delay();
287         delay();
288         
289         print_debug("After configuration:\r\n");
290         /* dump_pci_devices(); */
291         
292         /*
293         print_debug("\n\n***** RAM TEST *****\r\n");    
294         ram_check(0, 0xa0000);
295         ram_check(0x100000, 0x40000000);
296         */
297 }