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