[i945] Add SPD adress mapping
[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
41 void setup_ich7_gpios(void)
42 {
43         printk(BIOS_DEBUG, " GPIOS...");
44
45         /* X60 GPIO:
46             1: HDD_PRESENCE#
47             6: Unknown (Pulled high by R215 to VCC3B)
48             7: BDC_PRESENCE#
49             8: H8_WAKE#
50             9: RTC_BAT_IN#
51            10: Unknown (Pulled high by R700 to VCC3M
52            12: H8SCI#
53            13: SLICE_ON_3M#
54            14: Unknown (Pulled high by R321 to VCC3)
55            15: Unknown (Pulled high by R258 to VCC3)
56            19: Unknown (Pulled low  by R594)
57            21: Unknown (Pulled high by R145 to VCC3)
58            22: FWH_WP#
59            25: MDC_KILL#
60            33: HDD_PRESENCE_2#
61            35: CLKREQ_SATA#
62            36: PLANARID0
63            37: PLANARID1
64            38: PLANARID2
65            39: PLANARID3
66            48: FWH_TBL#
67         */
68
69         outl(0x1f40f7c2, DEFAULT_GPIOBASE + 0x00);      /* GPIO_USE_SEL */
70         outl(0xe0e8ffc3, DEFAULT_GPIOBASE + 0x04);      /* GP_IO_SEL */
71         outl(0xfbf6ddfd, DEFAULT_GPIOBASE + 0x0c);      /* GP_LVL */
72         /* Output Control Registers */
73         outl(0x00040000, DEFAULT_GPIOBASE + 0x18);      /* GPO_BLINK */
74         /* Input Control Registers */
75         outl(0x000039ff, DEFAULT_GPIOBASE + 0x2c);      /* GPI_INV */
76         outl(0x000100f2, DEFAULT_GPIOBASE + 0x30);      /* GPIO_USE_SEL2 */
77         outl(0x000000f0, DEFAULT_GPIOBASE + 0x34);      /* GP_IO_SEL2 */
78         outl(0x00030043, DEFAULT_GPIOBASE + 0x38);      /* GP_LVL */
79 }
80
81 static void ich7_enable_lpc(void)
82 {
83         // Enable Serial IRQ
84         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0x64, 0xd0);
85         // decode range
86         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x80, 0x0210);
87         // decode range
88         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x82, 0x1f0d);
89
90         /* range 0x1600 - 0x167f */
91         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x84, 0x1601);
92         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x86, 0x007c);
93
94         /* range 0x15e0 - 0x10ef */
95         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x88, 0x15e1);
96         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x8a, 0x000c);
97
98         /* range 0x1680 - 0x169f */
99         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x8c, 0x1681);
100         pci_write_config16(PCI_DEV(0, 0x1f, 0), 0x8e, 0x001c);
101 }
102
103 static void pnp_write_register(device_t dev, int reg, int val)
104 {
105         unsigned int port = dev >> 8;
106         outb(reg, port);
107         outb(val, port+1);
108 }
109
110 static void dock_write_register(int reg, int value)
111 {
112         outb(reg, 0x164e);
113         outb(value, 0x164f);
114         /* original software reads the chip id after every
115            I/O operation. Not sure if they are doing it for
116            some code switching depending on hardware or just
117            to have a delay after every operation.
118
119            Do it the same way for now, we may remove it later
120            if it isn't needed
121         */
122         outb(0x20, 0x164e);
123         inb(0x164f);
124 }
125
126 static void dock_dlpc_init(void)
127 {
128         /* Select DLPC module */
129         dock_write_register(0x07, 0x19);
130         /* DLPC Base Address 0x164c */
131         dock_write_register(0x60, 0x16);
132         dock_write_register(0x61, 0x4c);
133         /* Activate DLPC */
134         dock_write_register(0x30, 0x01);
135         outb(0x07, 0x164c);
136
137         while(!(inb(0x164c) & 8))
138                 udelay(100 * 100);
139 }
140
141 static void dock_gpio_set_mode(int port, int mode)
142 {
143         dock_write_register(0xf0, port);
144         dock_write_register(0xf1, mode);
145
146 }
147
148 static void dock_gpio_init(void)
149 {
150         /* Select GPIO module */
151         dock_write_register(0x07, 0x07);
152         /* GPIO Base Address 0x1680 */
153         dock_write_register(0x60, 0x16);
154         dock_write_register(0x61, 0x80);
155
156         /* Activate GPIO */
157         dock_write_register(0x30, 0x01);
158
159         dock_gpio_set_mode(0x00, 3);
160         dock_gpio_set_mode(0x01, 3);
161         dock_gpio_set_mode(0x02, 0);
162         dock_gpio_set_mode(0x03, 3);
163         dock_gpio_set_mode(0x04, 4);
164         dock_gpio_set_mode(0x20, 4);
165         dock_gpio_set_mode(0x21, 4);
166         dock_gpio_set_mode(0x23, 4);
167 }
168
169 static void connect_dock(void)
170 {
171         /* Enable 14.318MHz CLK on CLKIN */
172         dock_write_register(0x29, 0x00);
173         dock_write_register(0x29, 0xa0);
174         dock_gpio_init();
175         /* Assert D_PLTRST# */
176         outb(0xfe, 0x1680);
177         dock_dlpc_init();
178         /* Deassert D_PLTRST# */
179         outb(0xff, 0x1680);
180 }
181
182 static void early_superio_config(void)
183 {
184         device_t dev;
185
186         dev=PNP_DEV(0x2e, 0x00);
187         pnp_write_register(dev, 0x29, 0x06);
188
189         /* Enable COM1 */
190         pnp_write_register(dev, 0x07, 0x03);
191         pnp_write_register(dev, 0x60, 0x03);
192         pnp_write_register(dev, 0x61, 0xf8);
193         pnp_write_register(dev, 0x30, 0x01);
194 }
195
196 static void rcba_config(void)
197 {
198         /* Set up virtual channel 0 */
199         RCBA32(0x0014) = 0x80000001;
200         RCBA32(0x001c) = 0x03128010;
201
202         /* Device 1f interrupt pin register */
203         RCBA32(0x3100) = 0x00001230;
204         RCBA32(0x3108) = 0x40004321;
205
206         /* PCIe Interrupts */
207         RCBA32(0x310c) = 0x00004321;
208         /* HD Audio Interrupt */
209         RCBA32(0x3110) = 0x00000002;
210
211         /* dev irq route register */
212         RCBA16(0x3140) = 0x1007;
213         RCBA16(0x3142) = 0x0076;
214         RCBA16(0x3144) = 0x3210;
215         RCBA16(0x3146) = 0x7654;
216         RCBA16(0x3148) = 0x0010;
217
218         /* Enable IOAPIC */
219         RCBA8(0x31ff) = 0x03;
220
221         /* Enable upper 128bytes of CMOS */
222         RCBA32(0x3400) = (1 << 2);
223
224         /* Disable unused devices */
225         RCBA32(0x3418) = FD_PCIE6 | FD_PCIE5 | FD_INTLAN | FD_ACMOD | FD_ACAUD;
226         RCBA32(0x3418) |= (1 << 0); // Required.
227
228         /* Set up I/O Trap #0 for 0xfe00 (SMIC) */
229         RCBA32(0x1e84) = 0x00020001;
230         RCBA32(0x1e80) = 0x0000fe01;
231
232         /* Set up I/O Trap #3 for 0x800-0x80c (Trap) */
233         RCBA32(0x1e9c) = 0x000200f0;
234         RCBA32(0x1e98) = 0x000c0801;
235 }
236
237 static void early_ich7_init(void)
238 {
239         uint8_t reg8;
240         uint32_t reg32;
241
242         // program secondary mlt XXX byte?
243         pci_write_config8(PCI_DEV(0, 0x1e, 0), 0x1b, 0x20);
244
245         // reset rtc power status
246         reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xa4);
247         reg8 &= ~(1 << 2);
248         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xa4, reg8);
249
250         // usb transient disconnect
251         reg8 = pci_read_config8(PCI_DEV(0, 0x1f, 0), 0xad);
252         reg8 |= (3 << 0);
253         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0xad, reg8);
254
255         reg32 = pci_read_config32(PCI_DEV(0, 0x1d, 7), 0xfc);
256         reg32 |= (1 << 29) | (1 << 17);
257         pci_write_config32(PCI_DEV(0, 0x1d, 7), 0xfc, reg32);
258
259         reg32 = pci_read_config32(PCI_DEV(0, 0x1d, 7), 0xdc);
260         reg32 |= (1 << 31) | (1 << 27);
261         pci_write_config32(PCI_DEV(0, 0x1d, 7), 0xdc, reg32);
262
263         RCBA32(0x0088) = 0x0011d000;
264         RCBA16(0x01fc) = 0x060f;
265         RCBA32(0x01f4) = 0x86000040;
266         RCBA32(0x0214) = 0x10030549;
267         RCBA32(0x0218) = 0x00020504;
268         RCBA8(0x0220) = 0xc5;
269         reg32 = RCBA32(0x3410);
270         reg32 |= (1 << 6);
271         RCBA32(0x3410) = reg32;
272         reg32 = RCBA32(0x3430);
273         reg32 &= ~(3 << 0);
274         reg32 |= (1 << 0);
275         RCBA32(0x3430) = reg32;
276         RCBA32(0x3418) |= (1 << 0);
277         RCBA16(0x0200) = 0x2008;
278         RCBA8(0x2027) = 0x0d;
279         RCBA16(0x3e08) |= (1 << 7);
280         RCBA16(0x3e48) |= (1 << 7);
281         RCBA32(0x3e0e) |= (1 << 7);
282         RCBA32(0x3e4e) |= (1 << 7);
283
284         // next step only on ich7m b0 and later:
285         reg32 = RCBA32(0x2034);
286         reg32 &= ~(0x0f << 16);
287         reg32 |= (5 << 16);
288         RCBA32(0x2034) = reg32;
289 }
290
291 #include <cbmem.h>
292
293 void main(unsigned long bist)
294 {
295         u32 reg32;
296         int boot_mode = 0;
297         const u8 spd_addrmap[2 * DIMM_SOCKETS] = { 0x50, 0x52, 0x51, 0x53 };
298
299         if (bist == 0)
300                 enable_lapic();
301
302         /* Force PCIRST# */
303         pci_write_config16(PCI_DEV(0, 0x1e, 0), BCTRL, SBR);
304         udelay(200 * 1000);
305         pci_write_config16(PCI_DEV(0, 0x1e, 0), BCTRL, 0);
306
307         ich7_enable_lpc();
308
309         connect_dock();
310
311         early_superio_config();
312
313         /* Set up the console */
314         uart_init();
315
316 #if CONFIG_USBDEBUG
317         i82801gx_enable_usbdebug(1);
318         early_usbdebug_init();
319 #endif
320
321         console_init();
322
323         /* Halt if there was a built in self test failure */
324         report_bist_failure(bist);
325
326         if (MCHBAR16(SSKPD) == 0xCAFE) {
327                 printk(BIOS_DEBUG, "soft reset detected, rebooting properly\n");
328                 outb(0x6, 0xcf9);
329                 while (1) asm("hlt");
330         }
331
332         /* Perform some early chipset initialization required
333          * before RAM initialization can work
334          */
335         i945_early_initialization();
336
337         /* Read PM1_CNT */
338         reg32 = inl(DEFAULT_PMBASE + 0x04);
339         printk(BIOS_DEBUG, "PM1_CNT: %08x\n", reg32);
340         if (((reg32 >> 10) & 7) == 5) {
341 #if CONFIG_HAVE_ACPI_RESUME
342                 printk(BIOS_DEBUG, "Resume from S3 detected.\n");
343                 boot_mode = 2;
344                 /* Clear SLP_TYPE. This will break stage2 but
345                  * we care for that when we get there.
346                  */
347                 outl(reg32 & ~(7 << 10), DEFAULT_PMBASE + 0x04);
348
349 #else
350                 printk(BIOS_DEBUG, "Resume from S3 detected, but disabled.\n");
351 #endif
352         }
353
354         /* Enable SPD ROMs and DDR-II DRAM */
355         enable_smbus();
356
357 #if CONFIG_DEFAULT_CONSOLE_LOGLEVEL > 8
358         dump_spd_registers();
359 #endif
360
361         sdram_initialize(boot_mode, spd_addrmap);
362
363         /* Perform some initialization that must run before stage2 */
364         early_ich7_init();
365
366         /* This should probably go away. Until now it is required
367          * and mainboard specific
368          */
369         rcba_config();
370
371         /* Chipset Errata! */
372         fixup_i945_errata();
373
374         /* Initialize the internal PCIe links before we go into stage2 */
375         i945_late_initialization();
376
377 #if !CONFIG_HAVE_ACPI_RESUME
378 #if CONFIG_DEFAULT_CONSOLE_LOGLEVEL > 8
379 #if CONFIG_DEBUG_RAM_SETUP
380         sdram_dump_mchbar_registers();
381
382         {
383                 /* This will not work if TSEG is in place! */
384                 u32 tom = pci_read_config32(PCI_DEV(0,2,0), 0x5c);
385
386                 printk(BIOS_DEBUG, "TOM: 0x%08x\n", tom);
387                 ram_check(0x00000000, 0x000a0000);
388                 ram_check(0x00100000, tom);
389         }
390 #endif
391 #endif
392 #endif
393
394         MCHBAR16(SSKPD) = 0xCAFE;
395
396 #if CONFIG_HAVE_ACPI_RESUME
397         /* Start address of high memory tables */
398         unsigned long high_ram_base = get_top_of_ram() - HIGH_MEMORY_SIZE;
399
400         /* If there is no high memory area, we didn't boot before, so
401          * this is not a resume. In that case we just create the cbmem toc.
402          */
403         if ((boot_mode == 2) && cbmem_reinit((u64)high_ram_base)) {
404                 void *resume_backup_memory = cbmem_find(CBMEM_ID_RESUME);
405
406                 /* copy 1MB - 64K to high tables ram_base to prevent memory corruption
407                  * through stage 2. We could keep stuff like stack and heap in high tables
408                  * memory completely, but that's a wonderful clean up task for another
409                  * day.
410                  */
411                 if (resume_backup_memory)
412                         memcpy(resume_backup_memory, (void *)CONFIG_RAMBASE, HIGH_MEMORY_SAVE);
413
414                 /* Magic for S3 resume */
415                 pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafed00d);
416         }
417 #endif
418         /* Set legacy Brightness control to full brightness */
419         pci_write_config8(PCI_DEV(0, 2, 1), 0xf4, 0xff);
420 }