summaryrefslogtreecommitdiff
path: root/src/northbridge/intel/i855/raminit.c
blob: 8e928bd279ef7ee9e50fdd728ab969672808df77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2006 Jon Dufresne <jon.dufresne@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
 
#include <sdram_mode.h>

#define dumpnorth() dump_pci_device(PCI_DEV(0, 0, 1))
#define VG85X_MODE (SDRAM_BURST_4 | SDRAM_BURST_INTERLEAVED | SDRAM_CAS_2_5)

/**
  * Set only what I need until it works, then make it figure things out on boot
  * assumes only one dimm is populated
  */
 
static void sdram_set_registers(const struct mem_controller *ctrl)
{
	/*
	print_debug("Before configuration:\r\n");
	dump_pci_devices();
	*/
}

static void spd_set_row_attributes(const struct mem_controller *ctrl)
{
	uint16_t dra_reg;
	
	dra_reg = 0x7733;
	pci_write_config16(ctrl->d0, 0x50, dra_reg);
}

static void spd_set_dram_controller_mode(const struct mem_controller *ctrl)
{
	uint32_t drc_reg;
	
	/* drc_reg = 0x00009101; */
	drc_reg = 0x00009901;
	pci_write_config32(ctrl->d0, 0x70, drc_reg);
}

static void spd_set_dram_timing(const struct mem_controller *ctrl)
{
	uint32_t drt_reg;
	
	drt_reg = 0x2a004405;
	pci_write_config32(ctrl->d0, 0x60, drt_reg);
}

static void spd_set_dram_size(const struct mem_controller *ctrl)
{
	uint32_t drb_reg;

	drb_reg = 0x20202010;
	pci_write_config32(ctrl->d0, 0x40, drb_reg);
}

static void spd_set_dram_pwr_management(const struct mem_controller *ctrl)
{
	uint32_t pwrmg_reg;
	
	pwrmg_reg = 0x10f10430;
	pci_write_config32(ctrl->d0, 0x68, pwrmg_reg);
}

static void spd_set_dram_throttle_control(const struct mem_controller *ctrl)
{
	uint32_t dtc_reg;

	dtc_reg = 0x300aa2ff;
	pci_write_config32(ctrl->d0, 0xa0, dtc_reg);
}

void udelay(int usecs)
{
        int i;
        for(i = 0; i < usecs; i++)
                outb(i & 0xff, 0x80);
}

#define delay() udelay(200)

/* if ram still doesn't work do this function */
static void spd_set_undocumented_registers(const struct mem_controller *ctrl)
{
	uint16_t word;
	
	/* 0:0.0 */
	/*
	pci_write_config32(PCI_DEV(0, 0, 0), 0x10, 0xe0000008);
	pci_write_config32(PCI_DEV(0, 0, 0), 0x2c, 0x35808086);
	pci_write_config32(PCI_DEV(0, 0, 0), 0x48, 0xfec10000);
	pci_write_config32(PCI_DEV(0, 0, 0), 0x50, 0x00440100);
	
	pci_write_config32(PCI_DEV(0, 0, 0), 0x58, 0x11111000);
	
	pci_write_config16(PCI_DEV(0, 0, 0), 0x52, 0x0002);
	*/
	pci_write_config16(PCI_DEV(0, 0, 0), 0x52, 0x0044);
	/*
	pci_write_config16(PCI_DEV(0, 0, 0), 0x52, 0x0000);
	*/
	pci_write_config32(PCI_DEV(0, 0, 0), 0x58, 0x33333000);	
	pci_write_config32(PCI_DEV(0, 0, 0), 0x5c, 0x33333333);
	/*
	pci_write_config32(PCI_DEV(0, 0, 0), 0x60, 0x0000390a);
	pci_write_config32(PCI_DEV(0, 0, 0), 0x74, 0x02006056);
	pci_write_config32(PCI_DEV(0, 0, 0), 0x78, 0x00800001);
	*/
	pci_write_config32(PCI_DEV(0, 0, 0), 0xa8, 0x00000001);	
	
	pci_write_config32(PCI_DEV(0, 0, 0), 0xbc, 0x00001020);
	/*
	pci_write_config32(PCI_DEV(0, 0, 0), 0xfc, 0x00000109);
	*/

	/* 0:0.1 */
	pci_write_config32(ctrl->d0, 0x74, 0x00000001);	
	pci_write_config32(ctrl->d0, 0x78, 0x001fe974);
	pci_write_config32(ctrl->d0, 0x80, 0x00af0039);
	pci_write_config32(ctrl->d0, 0x84, 0x0000033c);
	pci_write_config32(ctrl->d0, 0x88, 0x00000010);
	pci_write_config32(ctrl->d0, 0x98, 0xde5a868c);
	pci_write_config32(ctrl->d0, 0x9c, 0x404e0046);
	pci_write_config32(ctrl->d0, 0xa8, 0x00020e1a);
	pci_write_config32(ctrl->d0, 0xb4, 0x0044cdac);
	pci_write_config32(ctrl->d0, 0xb8, 0x000055d4);
	pci_write_config32(ctrl->d0, 0xbc, 0x024acd38);
	pci_write_config32(ctrl->d0, 0xc0, 0x00000003);
	
	/* 0:0.3 */
	/*
	pci_write_config32(PCI_DEV(0, 0, 3), 0x2c, 0x35858086);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x44, 0x11110000);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x48, 0x09614a3c);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x4c, 0x4b09604a);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x50, 0x00000962);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x5c, 0x0b023331);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x6c, 0x0000402e);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x78, 0xe7c70f7f);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x7c, 0x0284007f);
	pci_write_config32(PCI_DEV(0, 0, 3), 0x84, 0x000000ef);
	*/
	
	/*
	pci_write_config16(PCI_DEV(0, 0, 3), 0xc0, 0x0200);
	pci_write_config16(PCI_DEV(0, 0, 3), 0xc0, 0x0400);
	*/
	
	/*
	pci_write_config32(PCI_DEV(0, 0, 3), 0xc4, 0x00000000);
	pci_write_config32(PCI_DEV(0, 0, 3), 0xd8, 0xff00c308);
	pci_write_config32(PCI_DEV(0, 0, 3), 0xdc, 0x00000025);
	pci_write_config32(PCI_DEV(0, 0, 3), 0xe0, 0x001f002a);
	pci_write_config32(PCI_DEV(0, 0, 3), 0xe4, 0x009f0098);
	pci_write_config32(PCI_DEV(0, 0, 3), 0xec, 0x00000400);
	pci_write_config32(PCI_DEV(0, 0, 3), 0xf0, 0xc0000000);
	*/
}

static void sdram_set_spd_registers(const struct mem_controller *ctrl)
{
	spd_set_row_attributes(ctrl);
	spd_set_dram_controller_mode(ctrl);
	spd_set_dram_timing(ctrl);
	spd_set_dram_size(ctrl);
	spd_set_dram_pwr_management(ctrl);
	spd_set_dram_throttle_control(ctrl);
	spd_set_undocumented_registers(ctrl);
}

static void ram_command(const struct mem_controller *ctrl,
			uint8_t command,
			uint32_t addr)
{
	uint32_t drc_reg;
	
	drc_reg = pci_read_config32(ctrl->d0, 0x70);
	drc_reg &= ~(7 << 4);
	drc_reg |= (command << 4);
	pci_write_config8(ctrl->d0, 0x70, drc_reg);
	read32(addr);
}

static void ram_command_mrs(const struct mem_controller *ctrl,
							uint8_t command,
							uint32_t mode,
							uint32_t addr)
{
	uint32_t drc_reg;
	uint32_t adjusted_mode;
	
	drc_reg = pci_read_config32(ctrl->d0, 0x70);
	drc_reg &= ~(7 << 4);
	drc_reg |= (command << 4);
	pci_write_config8(ctrl->d0, 0x70, drc_reg);
	/* Host address lines [13:3] map to DIMM address lines [11, 9:0] */	
	adjusted_mode = ((mode & 0x800) << (13 - 11)) | ((mode & 0x3ff) << (12 - 9));
	print_debug("Setting mode: ");
	print_debug_hex32(adjusted_mode + addr);
	print_debug("\r\n");
	read32(adjusted_mode + addr);
}

static void set_initialize_complete(const struct mem_controller *ctrl)
{
	uint32_t drc_reg;
	
	drc_reg = pci_read_config32(ctrl->d0, 0x70);
	drc_reg |= (1 << 29);
	pci_write_config32(ctrl->d0, 0x70, drc_reg);
}

static void sdram_enable(int controllers, const struct mem_controller *ctrl)
{
	int i;
	uint32_t rank1 = (1 << 30) / 2;
	print_debug("Ram enable 1\r\n");
	delay();
	delay();
	
	print_debug("Ram enable 2\r\n");
	ram_command(ctrl, 1, 0);
	ram_command(ctrl, 1, rank1);
	delay();
	delay();

	print_debug("Ram enable 3\r\n");
	ram_command(ctrl, 2, 0);
	ram_command(ctrl, 2, rank1);
	delay();
	delay();

	print_debug("Ram enable 4\r\n");
	ram_command_mrs(ctrl, 4, SDRAM_EXTMODE_DLL_ENABLE, 0);
	ram_command_mrs(ctrl, 4, SDRAM_EXTMODE_DLL_ENABLE, rank1);
	delay();
	delay();
	
	print_debug("Ram enable 5\r\n");
	ram_command_mrs(ctrl, 3, VG85X_MODE | SDRAM_MODE_DLL_RESET, 0);
	ram_command_mrs(ctrl, 3, VG85X_MODE | SDRAM_MODE_DLL_RESET, rank1);
	
	print_debug("Ram enable 6\r\n");
	ram_command(ctrl, 2, 0);
	ram_command(ctrl, 2, rank1);
	delay();
	delay();
	
	print_debug("Ram enable 7\r\n");
	for(i = 0; i < 8; i++) {
		ram_command(ctrl, 6, 0);
		ram_command(ctrl, 6, rank1);
		delay();
		delay();
	}

	print_debug("Ram enable 8\r\n");
	ram_command_mrs(ctrl, 3, VG85X_MODE | SDRAM_MODE_NORMAL, 0);
	ram_command_mrs(ctrl, 3, VG85X_MODE | SDRAM_MODE_NORMAL, rank1);

	print_debug("Ram enable 9\r\n");
	ram_command(ctrl, 7, 0);
	ram_command(ctrl, 7, rank1);
	delay();
	delay();
	
	print_debug("Ram enable 9\r\n");
	set_initialize_complete(ctrl);
	
	delay();
	delay();
	delay();
	
	print_debug("After configuration:\r\n");
	/* dump_pci_devices(); */
	
	/*
	print_debug("\n\n***** RAM TEST *****\r\n");	
	ram_check(0, 0xa0000);
	ram_check(0x100000, 0x40000000);
	*/
}