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