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