remove more warnings, and fix some boards (watchdog.h)
[coreboot.git] / src / mainboard / dell / s1850 / romstage.c
1 #define ASSEMBLY 1
2 #define __PRE_RAM__
3 #include <stdint.h>
4 #include <device/pci_def.h>
5 #include <arch/io.h>
6 #include <device/pnp_def.h>
7 #include <arch/romcc_io.h>
8 #include <cpu/x86/lapic.h>
9 #include <stdlib.h>
10 #include "option_table.h"
11 #include "pc80/mc146818rtc_early.c"
12 #include "pc80/serial.c"
13 #include "arch/i386/lib/console.c"
14 #include "lib/ramtest.c"
15 #include "southbridge/intel/i82801ex/i82801ex_early_smbus.c"
16 #include "northbridge/intel/e7520/raminit.h"
17 #include "superio/nsc/pc8374/pc8374_early_init.c"
18 #include "cpu/x86/lapic/boot_cpu.c"
19 #include "cpu/x86/mtrr/earlymtrr.c"
20 #include "debug.c"
21 #include "watchdog.c"
22 // Remove comment if resets in this file are actually used.
23 // #include "reset.c"
24 #include "s1850_fixups.c"
25 #include "northbridge/intel/e7520/memory_initialized.c"
26 #include "cpu/x86/bist.h"
27
28
29 #define SIO_GPIO_BASE 0x680
30 #define SIO_XBUS_BASE 0x4880
31
32 #define CONSOLE_SERIAL_DEV PNP_DEV(0x2e, PC8374_SP1)
33
34 #define DEVPRES_CONFIG  ( \
35         DEVPRES_D0F0 | \
36         DEVPRES_D1F0 | \
37         DEVPRES_D2F0 | \
38         DEVPRES_D3F0 | \
39         DEVPRES_D4F0 | \
40         DEVPRES_D6F0 | \
41         0 )
42 #define DEVPRES1_CONFIG (DEVPRES1_D0F1 | DEVPRES1_D8F0)
43
44 #define RECVENA_CONFIG  0x0808090a
45 #define RECVENB_CONFIG  0x0808090a
46
47 static inline void activate_spd_rom(const struct mem_controller *ctrl)
48 {
49         /* nothing to do */
50 }
51 static inline int spd_read_byte(unsigned device, unsigned address)
52 {
53         return smbus_read_byte(device, address);
54 }
55
56 /* this is very highly mainboard dependent, related to wiring */
57 /* from factory BIOS via lspci */
58 #define DIMM_MAP_LOGICAL 0x2841
59 #include "northbridge/intel/e7520/raminit.c"
60 #include "lib/generic_sdram.c"
61
62
63 /* IPMI garbage. This is all test stuff, if it really works we'll move it somewhere
64  */
65
66 #define nftransport  0xc
67
68 #define OBF  0
69 #define IBF 1
70
71 #define ipmidata  0xca0
72 #define ipmicsr  0xca4
73
74
75 static inline void  ibfzero(void)
76 {
77         while(inb(ipmicsr) &  (1<<IBF)) 
78                 ;
79 }
80 static inline void  clearobf(void)
81 {
82         (void) inb(ipmidata);
83 }
84
85 static inline void  waitobf(void)
86 {
87         while((inb(ipmicsr) &  (1<<OBF)) == 0) 
88                 ;
89 }
90 /* quite possibly the stupidest interface ever designed. */
91 static inline void  first_cmd_byte(unsigned char byte)
92 {
93         ibfzero();
94         clearobf();
95         outb(0x61, ipmicsr);
96         ibfzero();
97         clearobf();
98         outb(byte, ipmidata);
99 }
100
101 static inline void  next_cmd_byte(unsigned char byte)
102 {
103
104         ibfzero();
105         clearobf();
106         outb(byte, ipmidata);
107 }
108
109 static inline void  last_cmd_byte(unsigned char byte)
110 {
111         outb(0x62, ipmicsr);
112
113         ibfzero();
114         clearobf();
115         outb(byte,  ipmidata);
116 }
117
118 static inline void read_response_byte(void)
119 {
120         int val = -1;
121         if ((inb(ipmicsr)>>6) != 1)
122                 return;
123
124         ibfzero();
125         waitobf();
126         val = inb(ipmidata);
127         outb(0x68, ipmidata);
128
129         /* see if it is done */
130         if ((inb(ipmicsr)>>6) != 1){
131                 /* wait for the dummy read. Which describes this protocol */
132                 waitobf();
133                 (void)inb(ipmidata);
134         }
135 }
136
137 static inline void ipmidelay(void)
138 {
139         int i;
140         for(i = 0; i < 1000; i++) {
141                 inb(0x80);
142         }
143 }
144
145 static inline void bmc_foad(void)
146 {
147         unsigned char c;
148         /* be safe; make sure it is really ready */
149         while ((inb(ipmicsr)>>6)) {
150                 outb(0x60, ipmicsr);
151                 inb(ipmidata);
152         }
153         first_cmd_byte(nftransport << 2);
154         ipmidelay();
155         next_cmd_byte(0x12);
156         ipmidelay();
157         next_cmd_byte(2);
158         ipmidelay();
159         last_cmd_byte(3);
160         ipmidelay();
161 }
162
163 /* end IPMI garbage */
164
165 static void main(unsigned long bist)
166 {
167         u8 b;
168         u16 w;
169         u32 l;
170         int do_reset;
171         /*
172          * 
173          * 
174          */
175         static const struct mem_controller mch[] = {
176                 {
177                         .node_id = 0,
178                         .f0 = PCI_DEV(0, 0x00, 0),
179                         .f1 = PCI_DEV(0, 0x00, 1),
180                         .f2 = PCI_DEV(0, 0x00, 2),
181                         .f3 = PCI_DEV(0, 0x00, 3),
182                         /* the wiring on this part is really messed up */
183                         /* this is my best guess so far */
184                         .channel0 = {(0xa<<3)|0, (0xa<<3)|1, (0xa<<3)|2, (0xa<<3)|3, },
185                         .channel1 = {(0xa<<3)|4, (0xa<<3)|5, (0xa<<3)|6, (0xa<<3)|7, },
186                 }
187         };
188
189         /* superio setup */
190         /* observed from serialice */
191         static const u8 earlyinit[] = {
192                 0x21, 0x11, 0x11,
193                 0x22, 1, 1,
194                 0x23, 05, 05,
195                 0x24, 0x81, 0x81,
196                 0x26, 0, 0,
197                 0,
198         };
199
200         /* using SerialICE, we've seen this basic reset sequence on the dell. 
201          * we don't understand it as it uses undocumented registers, but
202          * we're going to clone it. 
203          */
204         /* enable a hidden device. */
205         b = pci_read_config8(PCI_DEV(0, 0, 0), 0xf4);
206         b |= 0x8;
207         pci_write_config8(PCI_DEV(0, 0, 0), 0xf4, b);
208
209         /* read-write lock in CMOS on LPC bridge on ICH5 */
210         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xd8, 4);
211
212         /* operate on undocumented device */
213         l = pci_read_config32(PCI_DEV(0, 0, 2), 0xa4);
214         l |= 0x1000;
215         pci_write_config32(PCI_DEV(0, 0, 2), 0xa4, l);
216
217         l = pci_read_config32(PCI_DEV(0, 0, 2), 0x9c);
218         l |= 0x8000;
219         pci_write_config32(PCI_DEV(0, 0, 2), 0x9c, l);
220
221         /* disable undocumented device */
222         b = pci_read_config8(PCI_DEV(0, 0, 0), 0xf4);
223         b &= ~0x8;
224         pci_write_config8(PCI_DEV(0, 0, 0), 0xf4, b);
225         
226         /* set up LPC bridge bits, some of which reply on undocumented
227          * registers
228          */
229         
230         b= pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xd8);
231         b |= 4;
232         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xd8, b);
233
234         b= pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xd4);
235         b |= 2;
236         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xd4, b);
237
238         /* ACPI base address */
239         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x40, 0x800);
240
241         /* Enable specific ACPI features */
242         b= pci_read_config8(PCI_DEV(0, 0x1f, 0), 0x44);
243         b |= 0x10;
244         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0x44, b);
245
246         /* ACPI control */
247         w = inw(0x868);
248         outw(w|0x800, 0x868);
249         w = inw(0x866);
250         outw(w|2, 0x866);
251
252 #if 0 
253         /*seriaice shows
254         dell does this so leave it here so I don't forget 
255          */
256         /* SMBUS */
257         pci_write_config16(PCI_DEV(0, 0x1f, 3), 0x20, 0x08c0);
258
259         /* unknown */
260         b = inb(0x8c2);
261         outb(0xdf, 0x8c2);
262 #endif
263
264         /* another device enable? */
265         b = pci_read_config8(PCI_DEV(0, 0, 0), 0xf4);
266         b |= 2;
267         pci_write_config8(PCI_DEV(0, 0, 0), 0xf4, b);
268         
269         /* ?? */
270         l = pci_read_config32(PCI_DEV(0, 8, 0), 0xc0);
271         do_reset = l & 0x8000000;
272         l |= 0x8000000;
273         pci_write_config32(PCI_DEV(0, 8, 0), 0xc0, l);
274
275         if (! do_reset) {
276                 outb(2, 0xcf9);
277                 outb(6, 0xcf9);
278         }
279         if (bist == 0) {
280                 /* Skip this if there was a built in self test failure */
281                 early_mtrr_init();
282                 if (memory_initialized()) {
283                         asm volatile ("jmp __cpu_reset");
284                 }
285         }
286         /* Setup the console */
287         mainboard_set_ich5();
288         //bmc_foad();
289         pc8374_enable_dev(CONSOLE_SERIAL_DEV, CONFIG_TTYS0_BASE);
290         uart_init();
291         console_init();
292
293
294         /* stuff we seem to need */
295         pc8374_enable_dev(PNP_DEV(0x2e, PC8374_KBCK), 0);
296
297         /* GPIOs */
298         pc8374_enable_dev(PNP_DEV(0x2e, PC8374_GPIO), 0xc20);
299
300         /* keep this in mind.
301         SerialICE-hlp: outb 002e <= 23
302         SerialICE-hlp:  inb 002f => 05
303         SerialICE-hlp: outb 002f <= 05
304         SerialICE-hlp: outb 002e <= 24
305         SerialICE-hlp:  inb 002f => c1
306         SerialICE-hlp: outb 002f <= c1
307          */
308
309         /* Halt if there was a built in self test failure */
310 //      report_bist_failure(bist);
311
312         /* MOVE ME TO A BETTER LOCATION !!! */
313         /* config LPC decode for flash memory access */
314         device_t dev;
315         dev = pci_locate_device(PCI_ID(0x8086, 0x24d0), 0);
316         if (dev == PCI_DEV_INVALID) {
317                 die("Missing ich5?");
318         }
319         pci_write_config32(dev, 0xe8, 0x00000000);
320         pci_write_config8(dev, 0xf0, 0x00);
321
322 #if 0
323         display_cpuid_update_microcode();
324 #endif
325 #if 1
326         print_pci_devices();
327 #endif
328 #if 1
329         enable_smbus();
330 #endif
331 #if 0
332 //      dump_spd_registers(&cpu[0]);
333         int i;
334         for(i = 0; i < 1; i++) {
335                 dump_spd_registers();
336         }
337 #endif
338 #if 1
339         show_dram_slots();
340 #endif
341         disable_watchdogs();
342 //      dump_ipmi_registers();
343         mainboard_set_e7520_leds();     
344 //      memreset_setup();
345
346         sdram_initialize(ARRAY_SIZE(mch), mch);
347 #if 0
348         dump_pci_devices();
349 #endif
350 #if 1
351         dump_pci_device(PCI_DEV(0, 0x00, 0));
352 //      dump_bar14(PCI_DEV(0, 0x00, 0));
353 #endif
354
355 #if 1 // temporarily disabled 
356         /* Check the first 1M */
357 //      ram_check(0x00000000, 0x000100000);
358 //      ram_check(0x00000000, 0x000a0000);
359 //      ram_check(0x00100000, 0x01000000);
360         ram_check(0x00100000, 0x00100100);
361         /* check the first 1M in the 3rd Gig */
362 //      ram_check(0x30100000, 0x31000000);
363 #endif
364 #if 0
365         ram_check(0x00000000, 0x02000000);
366 #endif
367         
368 #if 0   
369         while(1) {
370                 hlt();
371         }
372 #endif
373 }