i82801gx: replace cafed00d/cafebabe by defines
[coreboot.git] / src / mainboard / lenovo / x60 / romstage.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2009 coresystems GmbH
5  * Copyright (C) 2011 Sven Schnelle <svens@stackframe.org>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; version 2 of
10  * the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20  * MA 02110-1301 USA
21  */
22
23 // __PRE_RAM__ means: use "unsigned" for device, not a struct.
24
25 #include <stdint.h>
26 #include <string.h>
27 #include <arch/io.h>
28 #include <arch/romcc_io.h>
29 #include <device/pci_def.h>
30 #include <device/pnp_def.h>
31 #include <cpu/x86/lapic.h>
32 #include <lib.h>
33 #include <pc80/mc146818rtc.h>
34 #include <console/console.h>
35 #include <usbdebug.h>
36 #include <cpu/x86/bist.h>
37 #include "northbridge/intel/i945/i945.h"
38 #include "northbridge/intel/i945/raminit.h"
39 #include "southbridge/intel/i82801gx/i82801gx.h"
40 #include "dock.h"
41
42 void setup_ich7_gpios(void)
43 {
44         printk(BIOS_DEBUG, " GPIOS...");
45
46         /* X60 GPIO:
47             1: HDD_PRESENCE#
48             6: Unknown (Pulled high by R215 to VCC3B)
49             7: BDC_PRESENCE#
50             8: H8_WAKE#
51             9: RTC_BAT_IN#
52            10: Unknown (Pulled high by R700 to VCC3M
53            12: H8SCI#
54            13: SLICE_ON_3M#
55            14: Unknown (Pulled high by R321 to VCC3)
56            15: Unknown (Pulled high by R258 to VCC3)
57            19: Unknown (Pulled low  by R594)
58            21: Unknown (Pulled high by R145 to VCC3)
59            22: FWH_WP#
60            25: MDC_KILL#
61            33: HDD_PRESENCE_2#
62            35: CLKREQ_SATA#
63            36: PLANARID0
64            37: PLANARID1
65            38: PLANARID2
66            39: PLANARID3
67            48: FWH_TBL#
68         */
69
70         outl(0x1f40f7c2, DEFAULT_GPIOBASE + 0x00);      /* GPIO_USE_SEL */
71         outl(0xe0e8ffc3, DEFAULT_GPIOBASE + 0x04);      /* GP_IO_SEL */
72         outl(0xfbf6ddfd, DEFAULT_GPIOBASE + 0x0c);      /* GP_LVL */
73         /* Output Control Registers */
74         outl(0x00040000, DEFAULT_GPIOBASE + 0x18);      /* GPO_BLINK */
75         /* Input Control Registers */
76         outl(0x000039ff, DEFAULT_GPIOBASE + 0x2c);      /* GPI_INV */
77         outl(0x000100f2, DEFAULT_GPIOBASE + 0x30);      /* GPIO_USE_SEL2 */
78         outl(0x000000f0, DEFAULT_GPIOBASE + 0x34);      /* GP_IO_SEL2 */
79         outl(0x00030043, DEFAULT_GPIOBASE + 0x38);      /* GP_LVL */
80 }
81
82 static void ich7_enable_lpc(void)
83 {
84         // Enable Serial IRQ
85         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0x64, 0xd0);
86         // decode range
87         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x80, 0x0210);
88         // decode range
89         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x82, 0x1f0d);
90
91         /* range 0x1600 - 0x167f */
92         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x84, 0x1601);
93         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x86, 0x007c);
94
95         /* range 0x15e0 - 0x10ef */
96         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x88, 0x15e1);
97         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x8a, 0x000c);
98
99         /* range 0x1680 - 0x169f */
100         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x8c, 0x1681);
101         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x8e, 0x001c);
102 }
103
104 static void early_superio_config(void)
105 {
106         int timeout = 100000;
107         device_t dev = PNP_DEV(0x2e, 3);
108
109         pnp_write_config(dev, 0x29, 0x06);
110
111         while(!(pnp_read_config(dev, 0x29) & 0x08) && timeout--)
112                 udelay(1000);
113
114         /* Enable COM1 */
115         pnp_set_logical_device(dev);
116         pnp_set_iobase(dev, PNP_IDX_IO0, 0x3f8);
117         pnp_set_enable(dev, 1);
118 }
119
120 static void rcba_config(void)
121 {
122         /* Set up virtual channel 0 */
123         RCBA32(0x0014) = 0x80000001;
124         RCBA32(0x001c) = 0x03128010;
125
126         /* Device 1f interrupt pin register */
127         RCBA32(0x3100) = 0x00001230;
128         RCBA32(0x3108) = 0x40004321;
129
130         /* PCIe Interrupts */
131         RCBA32(0x310c) = 0x00004321;
132         /* HD Audio Interrupt */
133         RCBA32(0x3110) = 0x00000002;
134
135         /* dev irq route register */
136         RCBA16(0x3140) = 0x1007;
137         RCBA16(0x3142) = 0x0076;
138         RCBA16(0x3144) = 0x3210;
139         RCBA16(0x3146) = 0x7654;
140         RCBA16(0x3148) = 0x0010;
141
142         /* Enable IOAPIC */
143         RCBA8(0x31ff) = 0x03;
144
145         /* Enable upper 128bytes of CMOS */
146         RCBA32(0x3400) = (1 << 2);
147
148         /* Disable unused devices */
149         RCBA32(0x3418) = FD_PCIE6 | FD_PCIE5 | FD_INTLAN | FD_ACMOD | FD_ACAUD;
150         RCBA32(0x3418) |= (1 << 0); // Required.
151
152         /* Set up I/O Trap #0 for 0xfe00 (SMIC) */
153         RCBA32(0x1e84) = 0x00020001;
154         RCBA32(0x1e80) = 0x0000fe01;
155
156         /* Set up I/O Trap #3 for 0x800-0x80c (Trap) */
157         RCBA32(0x1e9c) = 0x000200f0;
158         RCBA32(0x1e98) = 0x000c0801;
159 }
160
161 static void early_ich7_init(void)
162 {
163         uint8_t reg8;
164         uint32_t reg32;
165
166         // program secondary mlt XXX byte?
167         pci_write_config8(PCI_DEV(0, 0x1e, 0), 0x1b, 0x20);
168
169         // reset rtc power status
170         reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xa4);
171         reg8 &= ~(1 << 2);
172         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xa4, reg8);
173
174         // usb transient disconnect
175         reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xad);
176         reg8 |= (3 << 0);
177         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xad, reg8);
178
179         reg32 = pci_read_config32(PCI_DEV(0, 0x1d, 7), 0xfc);
180         reg32 |= (1 << 29) | (1 << 17);
181         pci_write_config32(PCI_DEV(0, 0x1d, 7), 0xfc, reg32);
182
183         reg32 = pci_read_config32(PCI_DEV(0, 0x1d, 7), 0xdc);
184         reg32 |= (1 << 31) | (1 << 27);
185         pci_write_config32(PCI_DEV(0, 0x1d, 7), 0xdc, reg32);
186
187         RCBA32(0x0088) = 0x0011d000;
188         RCBA16(0x01fc) = 0x060f;
189         RCBA32(0x01f4) = 0x86000040;
190         RCBA32(0x0214) = 0x10030549;
191         RCBA32(0x0218) = 0x00020504;
192         RCBA8(0x0220) = 0xc5;
193         reg32 = RCBA32(0x3410);
194         reg32 |= (1 << 6);
195         RCBA32(0x3410) = reg32;
196         reg32 = RCBA32(0x3430);
197         reg32 &= ~(3 << 0);
198         reg32 |= (1 << 0);
199         RCBA32(0x3430) = reg32;
200         RCBA32(0x3418) |= (1 << 0);
201         RCBA16(0x0200) = 0x2008;
202         RCBA8(0x2027) = 0x0d;
203         RCBA16(0x3e08) |= (1 << 7);
204         RCBA16(0x3e48) |= (1 << 7);
205         RCBA32(0x3e0e) |= (1 << 7);
206         RCBA32(0x3e4e) |= (1 << 7);
207
208         // next step only on ich7m b0 and later:
209         reg32 = RCBA32(0x2034);
210         reg32 &= ~(0x0f << 16);
211         reg32 |= (5 << 16);
212         RCBA32(0x2034) = reg32;
213 }
214
215 #include <cbmem.h>
216
217 void main(unsigned long bist)
218 {
219         u32 reg32;
220         int boot_mode = 0;
221         const u8 spd_addrmap[2 * DIMM_SOCKETS] = { 0x50, 0x52, 0x51, 0x53 };
222
223         if (bist == 0)
224                 enable_lapic();
225
226         /* Force PCIRST# */
227         pci_write_config16(PCI_DEV(0, 0x1e, 0), BCTRL, SBR);
228         udelay(200 * 1000);
229         pci_write_config16(PCI_DEV(0, 0x1e, 0), BCTRL, 0);
230
231         ich7_enable_lpc();
232
233
234         /* dock_init initializes the DLPC switch on
235          *  thinpad side, so this is required even
236          *  if we're undocked.
237          */
238         if (!dlpc_init() && dock_present()) {
239                 dock_connect();
240                 early_superio_config();
241                 /* Set up the console */
242         }
243
244         console_init();
245
246         /* Halt if there was a built in self test failure */
247         report_bist_failure(bist);
248
249         if (MCHBAR16(SSKPD) == 0xCAFE) {
250                 printk(BIOS_DEBUG, "soft reset detected, rebooting properly\n");
251                 outb(0x6, 0xcf9);
252                 while (1) asm("hlt");
253         }
254
255         /* Perform some early chipset initialization required
256          * before RAM initialization can work
257          */
258         i945_early_initialization();
259
260         /* Read PM1_CNT */
261         reg32 = inl(DEFAULT_PMBASE + 0x04);
262         printk(BIOS_DEBUG, "PM1_CNT: %08x\n", reg32);
263         if (((reg32 >> 10) & 7) == 5) {
264 #if CONFIG_HAVE_ACPI_RESUME
265                 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
266                 boot_mode = 2;
267                 /* Clear SLP_TYPE. This will break stage2 but
268                  * we care for that when we get there.
269                  */
270                 outl(reg32 & ~(7 << 10), DEFAULT_PMBASE + 0x04);
271
272 #else
273                 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
274 #endif
275         }
276
277         /* Enable SPD ROMs and DDR-II DRAM */
278         enable_smbus();
279
280 #if CONFIG_DEFAULT_CONSOLE_LOGLEVEL > 8
281         dump_spd_registers();
282 #endif
283
284         sdram_initialize(boot_mode, spd_addrmap);
285
286         /* Perform some initialization that must run before stage2 */
287         early_ich7_init();
288
289         /* This should probably go away. Until now it is required
290          * and mainboard specific
291          */
292         rcba_config();
293
294         /* Chipset Errata! */
295         fixup_i945_errata();
296
297         /* Initialize the internal PCIe links before we go into stage2 */
298         i945_late_initialization();
299
300 #if !CONFIG_HAVE_ACPI_RESUME
301 #if CONFIG_DEFAULT_CONSOLE_LOGLEVEL > 8
302 #if CONFIG_DEBUG_RAM_SETUP
303         sdram_dump_mchbar_registers();
304
305         {
306                 /* This will not work if TSEG is in place! */
307                 u32 tom = pci_read_config32(PCI_DEV(0,2,0), 0x5c);
308
309                 printk(BIOS_DEBUG, "TOM: 0x%08x\n", tom);
310                 ram_check(0x00000000, 0x000a0000);
311                 ram_check(0x00100000, tom);
312         }
313 #endif
314 #endif
315 #endif
316
317         MCHBAR16(SSKPD) = 0xCAFE;
318
319 #if CONFIG_HAVE_ACPI_RESUME
320         /* Start address of high memory tables */
321         unsigned long high_ram_base = get_top_of_ram() - HIGH_MEMORY_SIZE;
322
323         /* If there is no high memory area, we didn't boot before, so
324          * this is not a resume. In that case we just create the cbmem toc.
325          */
326         if ((boot_mode == 2) && cbmem_reinit((u64)high_ram_base)) {
327                 void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
328
329                 /* copy 1MB - 64K to high tables ram_base to prevent memory corruption
330                  * through stage 2. We could keep stuff like stack and heap in high tables
331                  * memory completely, but that's a wonderful clean up task for another
332                  * day.
333                  */
334                 if (resume_backup_memory)
335                         memcpy(resume_backup_memory, (void *)CONFIG_RAMBASE, HIGH_MEMORY_SAVE);
336
337                 /* Magic for S3 resume */
338                 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, SKPAD_ACPI_S3_MAGIC);
339         }
340 #endif
341         /* Set legacy Brightness control to full brightness */
342         pci_write_config8(PCI_DEV(0, 2, 1), 0xf4, 0xff);
343 }