Fix a few whitespace and coding style issues.
[coreboot.git] / src / mainboard / iwave / iWRainbowG6 / romstage.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009-2010 iWave Systems
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19
20 #include <stdint.h>
21 #include <string.h>
22 #include <arch/io.h>
23 #include <arch/romcc_io.h>
24 #include <device/pci_def.h>
25 #include <device/pnp_def.h>
26 #include <cpu/x86/lapic.h>
27 #include <cpu/x86/cache.h>
28 #include <arch/cpu.h>
29 #include <console/console.h>
30 #if 0
31 #include "ram/ramtest.c"
32 #include "southbridge/intel/sch/early_smbus.c"
33 #endif
34
35 #define RFID_TEST 0
36
37 #if RFID_TEST
38 #define RFID_ADDR 0xA0
39 #define RFID_SELECT_CARD_COMMAND 0x01
40 #define SELECT_COMMAND_LENGTH 0x01
41
42 #define SMBUS_BASE_ADDRESS 0x400
43
44 static u32 sch_SMbase_read(void)
45 {
46         u32 SMBusBase;
47
48         /* SMBus address */
49         SMBusBase = pci_read_config32(PCI_DEV(0, 0x1f, 0), 0x40);
50         SMBusBase &= 0xFFFF;
51         printk(BIOS_DEBUG, "SMBus base = %x\r\n", SMBusBase);
52         return SMBusBase;
53 }
54
55 static void sch_SMbase_init(void)
56 {
57         u32 SMBusBase;
58
59         SMBusBase = sch_SMbase_read();
60         outb(0x3F, SMBusBase + SMBCLKDIV);
61 }
62
63 static void sch_SMbus_regs(void)
64 {
65         u32 SMBusBase;
66
67         SMBusBase = sch_SMbase_read();
68         printk(BIOS_DEBUG, "SMBHSTCNT. =%x\r\n", inb(SMBusBase + SMBHSTCNT));
69         printk(BIOS_DEBUG, "SMBHSTSTS. =%x\r\n", inb(SMBusBase + SMBHSTSTS));
70         printk(BIOS_DEBUG, "SMBCLKDIV. =%x\r\n", inb(SMBusBase + SMBCLKDIV));
71
72         printk(BIOS_DEBUG, "SMBHSTADD. =%x\r\n", inb(SMBusBase + SMBHSTADD));
73         printk(BIOS_DEBUG, "SMBHSTCMD. =%x\r\n", inb(SMBusBase + SMBHSTCMD));
74 }
75
76 void smb_clear(void)
77 {
78         u32 SMBusBase;
79
80         SMBusBase = sch_SMbase_read();
81         outb(0x00, SMBusBase + SMBHSTCNT);
82         outb(0x07, SMBusBase + SMBHSTSTS);
83 }
84
85 void data_clear(void)
86 {
87         u32 SMBusBase;
88
89         SMBusBase = sch_SMbase_read();
90         outb(0x00, SMBusBase + SMBHSTDAT0);
91         outb(0x00, SMBusBase + SMBHSTCMD);
92         outb(0x00, SMBusBase + SMBHSTDAT1);
93         outb(0x00, SMBusBase + SMBHSTDATB);
94         outb(0x00, SMBusBase + (SMBHSTDATB + 0x1));
95         outb(0x00, SMBusBase + (SMBHSTDATB + 0x2));
96         outb(0x00, SMBusBase + (SMBHSTDATB + 0x3));
97         outb(0x00, SMBusBase + (SMBHSTDATB + 0x4));
98         outb(0x00, SMBusBase + (SMBHSTDATB + 0x5));
99         outb(0x00, SMBusBase + (SMBHSTDATB + 0x6));
100 }
101
102 void transaction1(unsigned char dev_addr)
103 {
104         int temp, a;
105         u32 SMBusBase;
106
107         SMBusBase = sch_SMbase_read();
108         printk(BIOS_DEBUG, "Transaction 1");
109         //clear the control and status registers
110         smb_clear();
111         //clear the data register
112         data_clear();
113         //program TSA register
114         outb(dev_addr, SMBusBase + SMBHSTADD);
115         //program command register
116         outb(0x04, SMBusBase + SMBHSTCMD);
117         //write data register
118         outb(0x04, SMBusBase + SMBHSTDAT0);
119         outb(0x04, SMBusBase + SMBHSTDATB);
120
121         outb(0x09, SMBusBase + (SMBHSTDATB + 0x1));
122         outb(0x11, SMBusBase + (SMBHSTDATB + 0x2));
123         outb(0x22, SMBusBase + (SMBHSTDATB + 0x3));
124
125         //set the control register
126         outb(0x15, SMBusBase + SMBHSTCNT);
127         //check the status register for busy state
128         //sch_SMbus_regs ();
129         temp = inb(SMBusBase + SMBHSTSTS);
130         //printk(BIOS_DEBUG, "SMBus Busy.. status =%x\r\n",temp);
131         //printk(BIOS_DEBUG, "SMBHSTSTS. =%x\r\n",inb(SMBusBase+SMBHSTSTS));
132         do {
133                 temp = inb(SMBusBase + SMBHSTSTS);
134                 printk(BIOS_DEBUG, "SMBus Busy.. status =%x\r\n", temp);
135                 //sch_SMbus_regs ();
136                 printk(BIOS_DEBUG, "SMBHSTSTS. =%x\r\n",
137                        inb(SMBusBase + SMBHSTSTS));
138                 if (temp > 0)
139                         break;
140         } while (1);
141
142         switch (temp) {
143         case 1:
144                 printk(BIOS_DEBUG, "SMBus Success");
145                 break;
146         default:
147                 printk(BIOS_DEBUG, "SMBus error %d", temp);
148                 break;
149
150         }
151         sch_SMbus_regs();
152         printk(BIOS_DEBUG, "Command in TRansaction 1=%x\r\n\n",
153                inb(SMBusBase + SMBHSTCMD));
154 }
155
156 void transaction2(unsigned char dev_addr)
157 {
158         int temp, a;
159         u32 SMBusBase;
160
161         SMBusBase = sch_SMbase_read();
162         printk(BIOS_DEBUG, "Transaction 2");
163         //clear the control and status registers
164         smb_clear();
165         //clear the data register
166         data_clear();
167         //program TSA register
168         outb(dev_addr, SMBusBase + SMBHSTADD);
169         //program command register
170         outb(0x03, SMBusBase + SMBHSTCMD);
171         //write data register
172         outb(0x02, SMBusBase + SMBHSTDAT0);
173         outb(0x03, SMBusBase + SMBHSTDATB);
174         outb(0x09, SMBusBase + (SMBHSTDATB + 0x1));
175         outb(0x15, SMBusBase + SMBHSTCNT);
176         //check the status register for busy state
177         //sch_SMbus_regs ();
178         temp = inb(SMBusBase + SMBHSTSTS);
179         //printk(BIOS_DEBUG, "SMBus Busy.. status =%x\r\n",temp);
180         //printk(BIOS_DEBUG, "SMBHSTSTS. =%x\r\n",inb(SMBusBase+SMBHSTSTS));
181         do {
182                 temp = inb(SMBusBase + SMBHSTSTS);
183                 printk(BIOS_DEBUG, "SMBus Busy.. status =%x\r\n", temp);
184                 //sch_SMbus_regs ();
185                 printk(BIOS_DEBUG, "SMBHSTSTS. =%x\r\n",
186                        inb(SMBusBase + SMBHSTSTS));
187                 if (temp > 0)
188                         break;
189         } while (1);
190
191         switch (temp) {
192         case 1:
193                 printk(BIOS_DEBUG, "SMBus Success");
194                 break;
195         default:
196                 printk(BIOS_DEBUG, "SMBus error %d", temp);
197                 break;
198
199         }
200         sch_SMbus_regs();
201
202         printk(BIOS_DEBUG, "Command in TRansaction 2=%x\r\n\n",
203                inb(SMBusBase + SMBHSTCMD));
204 }
205
206 void transaction3(unsigned char dev_addr)
207 {
208         int temp, index, length;
209         u32 SMBusBase;
210
211         SMBusBase = sch_SMbase_read();
212         printk(BIOS_DEBUG, "smb_read_multiple_bytes");
213         smb_clear();
214         data_clear();
215         outb(dev_addr, SMBusBase + SMBHSTADD);
216         outb(0x03, SMBusBase + SMBHSTCMD);
217         outb(0x11, SMBusBase + SMBHSTCNT);
218
219         //data_clear();
220         outb(dev_addr + 1, SMBusBase + SMBHSTADD);
221
222         outb(0x15, SMBusBase + SMBHSTCNT);
223
224         // sch_SMbus_regs ();
225         //check the status register for busy state
226         //temp=inb(SMBusBase+SMBHSTSTS);
227         //printk(BIOS_DEBUG, "SMBus Busy.. status =%x\r\n",temp);
228         //sch_SMbus_regs ();
229         //printk(BIOS_DEBUG, "SMBHSTSTS. =%x\r\n",inb(SMBusBase+SMBHSTSTS));
230         do {
231                 temp = inb(SMBusBase + SMBHSTSTS);
232                 printk(BIOS_DEBUG, "SMBHSTSTS. =%x\r\n",
233                        inb(SMBusBase + SMBHSTSTS));
234                 //sch_SMbus_regs ();
235                 if (temp > 0)
236                         break;
237         } while (1);
238
239         switch (temp) {
240         case 1:
241                 printk(BIOS_DEBUG, "SMBus Success\n");
242                 break;
243         default:
244                 printk(BIOS_DEBUG, "SMBus error %d", temp);
245                 break;
246
247         }
248
249         sch_SMbus_regs();
250         printk(BIOS_DEBUG, "ADDRESS is.. %x\r\n", inb(SMBusBase + SMBHSTADD));
251         length = inb(SMBusBase + SMBHSTDAT0);
252
253         printk(BIOS_DEBUG, "Length is.. %x\r\n", inb(SMBusBase + SMBHSTDAT0));
254
255         printk(BIOS_DEBUG, "Command is... %x\r\n", inb(SMBusBase + SMBHSTDATB));
256         printk(BIOS_DEBUG, "Status .. %x\r\n", inb(SMBusBase + SMBHSTDATB + 1));
257         for (index = 0; index < length; index++)
258                 printk(BIOS_DEBUG, "Serial Byte[%x]..%x\r\n", index,
259                        inb(SMBusBase + SMBHSTDATB + index));
260 }
261
262 int selectcard(void)
263 {
264         int i;
265
266         printk(BIOS_DEBUG, "%s", "\r\nCase 9....... \n\r");
267         // send the length byte and command code through RFID interface
268
269         transaction1(RFID_ADDR);
270         transaction2(RFID_ADDR);
271         transaction3(RFID_ADDR);
272         return (1);
273 }
274 #endif
275
276 #include "northbridge/intel/sch/early_init.c"
277 #include "northbridge/intel/sch/raminit.h"
278 #include "northbridge/intel/sch/raminit.c"
279
280 static void sch_enable_lpc(void)
281 {
282         /* Initialize the FWH decode/Enable registers according to platform design */
283         pci_write_config32(PCI_DEV(0, 0x1f, 0), 0xD0, 0x00112233);
284         pci_write_config32(PCI_DEV(0, 0x1f, 0), 0xD4, 0xC0000000);
285         pci_write_config32(PCI_DEV(0, 0x1f, 0), 0x60, 0x808A8B8B);
286         pci_write_config32(PCI_DEV(0, 0x1f, 0), 0x64, 0x8F898F89);
287 }
288
289 static void sch_shadow_CMC(void)
290 {
291         u32 reg32;
292
293         /* FIXME: proper dest, proper src, and wbinvd, too */
294         memcpy((void *)CMC_SHADOW, (void *)0xfffd0000, 64 * 1024);
295         // __asm__ volatile ("wbinvd \n"
296         //);
297         printk(BIOS_DEBUG, "copy done ");
298         memcpy((void *)0x3f5f0000, (void *)0x3faf0000, 64 * 1024);
299         printk(BIOS_DEBUG, "copy 2 done ");
300         reg32 = cpuid_eax(0x00000001);
301         printk(BIOS_INFO, "CPU ID: %d.\n", reg32);
302
303         reg32 = cpuid_eax(0x80000008);
304         printk(BIOS_INFO, "Physical Address size: %d.\n", (reg32 & 0xFF));
305         printk(BIOS_INFO, "Virtual Address size: %d.\n",
306                ((reg32 & 0xFF00) >> 8));
307         sch_port_access_write_ram_cmd(0xB8, 4, 0, 0x3faf0000);
308         printk(BIOS_DEBUG, "1 ");
309         sch_port_access_write_ram_cmd(0xBA, 4, 0, reg32);
310         printk(BIOS_DEBUG, "2 ");
311 }
312
313 static void poulsbo_setup_Stage1Regs(void)
314 {
315         u32 reg32;
316
317         printk(BIOS_DEBUG, "E000/F000 Routing ");
318         reg32 = sch_port_access_read(2, 3, 4);
319         sch_port_access_write(2, 3, 4, (reg32 | 0x6));
320 }
321
322 static void poulsbo_setup_Stage2Regs(void)
323 {
324         u32 reg32;
325
326         printk(BIOS_DEBUG, "Reserved");
327         reg32 = pci_read_config32(PCI_DEV(0, 0x2, 0), 0x62);
328         pci_write_config32(PCI_DEV(0, 0x2, 0), 0x62, (reg32 | 0x3));
329         /* Slot capabilities */
330         pci_write_config32(PCI_DEV(0, 28, 0), 0x54, 0x80500);
331         pci_write_config32(PCI_DEV(0, 28, 1), 0x54, 0x100500);
332         /* FIXME: CPU ID identification */
333         printk(BIOS_DEBUG, " done.\n");
334 }
335
336 void main(unsigned long bist)
337 {
338         int boot_mode = 0;
339
340         if (bist == 0)
341                 enable_lapic();
342
343         sch_enable_lpc();
344         uart_init();
345         console_init();
346
347         /* Halt if there was a built in self test failure */
348         // report_bist_failure(bist);
349         // outl (0x00, 0x1088);
350
351         /*
352          * Perform some early chipset initialization required
353          * before RAM initialization can work.
354          */
355         sch_early_initialization();
356         sdram_initialize(boot_mode);
357
358         sch_shadow_CMC();
359         poulsbo_setup_Stage1Regs();
360         poulsbo_setup_Stage2Regs();
361 #if 0
362         sch_SMbase_init();
363
364         /* Perform some initialization that must run before stage2. */
365 #endif
366
367         /*
368          * This should probably go away. Until now it is required
369          * and mainboard specific.
370          */
371
372         /* Chipset Errata! */
373         pci_write_config16(PCI_DEV(0, 0x2, 0), GGC, 0x20);
374         pci_write_config32(PCI_DEV(0, 0x2, 0), 0xc4, 0x00000002);
375         pci_write_config32(PCI_DEV(0, 0x2, 0), 0xe0, 0x00008000);
376         pci_write_config32(PCI_DEV(0, 0x2, 0), 0xf0, 0x00000005);
377         pci_write_config16(PCI_DEV(0, 0x2, 0), 0xf7, 0x80);
378         pci_write_config16(PCI_DEV(0, 0x2, 0), 0x4, 0x7);
379
380 #if RFID_TEST
381         sch_SMbase_init();
382         selectcard();
383 #endif
384 }