Merge some parts of the i945 review (trivial):
[coreboot.git] / src / northbridge / intel / i945 / early_init.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2008 coresystems GmbH
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * 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 "i945.h"
21 #include "pcie_config.c"
22
23 static int i945_silicon_revision(void)
24 {
25         return pci_read_config8(PCI_DEV(0, 0x00, 0), PCI_CLASS_REVISION);
26 }
27
28 static void i945_detect_chipset(void)
29 {
30         u8 reg8;
31
32         printk_info("\n");
33         reg8 = (pci_read_config8(PCI_DEV(0, 0x00, 0), 0xe7) & 0x70) >> 4;
34         switch (reg8) {
35         case 1:
36                 printk_info("Mobile Intel(R) 945GM/GME Express");
37                 break;
38         case 2:
39                 printk_info("Mobile Intel(R) 945GMS/GU Express");
40                 break;
41         case 3:
42                 printk_info("Mobile Intel(R) 945PM Express");
43                 break;
44         case 5:
45                 printk_info("Intel(R) 945GT Express");
46                 break;
47         case 6:
48                 printk_info("Mobile Intel(R) 943/940GML Express");
49                 break;
50         default:
51                 printk_info("Unknown (%02x)", reg8);    /* Others reserved. */
52         }
53         printk_info(" Chipset\n");
54
55         printk_debug("(G)MCH capable of up to FSB ");
56         reg8 = (pci_read_config8(PCI_DEV(0, 0x00, 0), 0xe3) & 0xe0) >> 5;
57         switch (reg8) {
58         case 2:
59                 printk_debug("800 MHz"); /* According to 965 spec */
60                 break;
61         case 3:
62                 printk_debug("667 MHz");
63                 break;
64         case 4:
65                 printk_debug("533 MHz");
66                 break;
67         default:
68                 printk_debug("N/A MHz (%02x)", reg8);
69         }
70         printk_debug("\n");
71
72         printk_debug("(G)MCH capable of ");
73         reg8 = (pci_read_config8(PCI_DEV(0, 0x00, 0), 0xe4) & 0x07);
74         switch (reg8) {
75         case 2:
76                 printk_debug("up to DDR2-667");
77                 break;
78         case 3:
79                 printk_debug("up to DDR2-533");
80                 break;
81         case 4:
82                 printk_debug("DDR2-400");
83                 break;
84         default:
85                 printk_info("unknown max. RAM clock (%02x).", reg8);    /* Others reserved. */
86         }
87         printk_debug("\n");
88 }
89
90 static void i945_setup_bars(void)
91 {
92         u8 reg8;
93
94         /* As of now, we don't have all the A0 workarounds implemented */
95         if (i945_silicon_revision() == 0)
96                 printk_info
97                     ("Warning: i945 silicon revision A0 might not work correctly.\n");
98
99         /* Setting up Southbridge. In the northbridge code. */
100         printk_debug("Setting up static southbridge registers...");
101         pci_write_config32(PCI_DEV(0, 0x1f, 0), RCBA, DEFAULT_RCBA | 1);
102
103         pci_write_config32(PCI_DEV(0, 0x1f, 0), PMBASE, DEFAULT_PMBASE | 1);
104         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0x44 /* ACPI_CNTL */ , 0x80); /* Enable ACPI BAR */
105
106         pci_write_config32(PCI_DEV(0, 0x1f, 0), GPIOBASE, DEFAULT_GPIOBASE | 1);
107         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0x4c /* GC */ , 0x10);   /* Enable GPIOs */
108         setup_ich7_gpios();
109         printk_debug(" done.\n");
110
111         printk_debug("Disabling Watchdog reboot...");
112         RCBA32(GCS) = (RCBA32(0x3410)) | (1 << 5);      /* No reset */
113         outw((1 << 11), DEFAULT_PMBASE | 0x60 | 0x08);  /* halt timer */
114         printk_debug(" done.\n");
115
116         printk_debug("Setting up static northbridge registers...");
117         /* Set up all hardcoded northbridge BARs */
118         pci_write_config32(PCI_DEV(0, 0x00, 0), EPBAR, DEFAULT_EPBAR | 1);
119         pci_write_config32(PCI_DEV(0, 0x00, 0), MCHBAR, DEFAULT_MCHBAR | 1);
120         pci_write_config32(PCI_DEV(0, 0x00, 0), PCIEXBAR, DEFAULT_PCIEXBAR | 5); /* 64MB - busses 0-63 */
121         pci_write_config32(PCI_DEV(0, 0x00, 0), DMIBAR, DEFAULT_DMIBAR | 1);
122         pci_write_config32(PCI_DEV(0, 0x00, 0), X60BAR, DEFAULT_X60BAR | 1);
123
124         /* Hardware default is 8MB UMA. If someone wants to make this a
125          * CMOS or compile time option, send a patch.
126          * pci_write_config16(PCI_DEV(0, 0x00, 0), GGC, 0x30);
127          */
128
129         /* Set C0000-FFFFF to access RAM on both reads and writes */
130         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM0, 0x30);
131         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM1, 0x33);
132         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM2, 0x33);
133         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM3, 0x33);
134         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM4, 0x33);
135         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM5, 0x33);
136         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM6, 0x33);
137
138         pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafebabe);
139         printk_debug(" done.\n");
140
141         /* Wait for MCH BAR to come up */
142         printk_debug("Waiting for MCHBAR to come up...");
143         if ((pci_read_config8(PCI_DEV(0, 0x0f, 0), 0xe6) & 0x2) == 0x00) { /* Bit 49 of CAPID0 */
144                 do {
145                         reg8 = *(volatile u8 *)0xfed40000;
146                 } while (!(reg8 & 0x80));
147         }
148         printk_debug("ok\n");
149 }
150
151 static void i945_setup_egress_port(void)
152 {
153         u32 reg32;
154         u32 timeout;
155
156         printk_debug("Setting up Egress Port RCRB\n");
157
158         /* Egress Port Virtual Channel 0 Configuration */
159
160         /* map only TC0 to VC0 */
161         reg32 = EPBAR32(EPVC0RCTL);
162         reg32 &= 0xffffff01;
163         EPBAR32(EPVC0RCTL) = reg32;
164
165         
166         reg32 = EPBAR32(EPPVCCAP1);
167         reg32 &= ~(7 << 0);
168         reg32 |= 1;
169         EPBAR32(EPPVCCAP1) = reg32;
170
171         /* Egress Port Virtual Channel 1 Configuration */
172         reg32 = EPBAR32(0x2c);
173         reg32 &= 0xffffff00;
174         if ((MCHBAR32(CLKCFG) & 7) == 1)
175                 reg32 |= 0x0d;  /* 533MHz */
176         if ((MCHBAR32(CLKCFG) & 7) == 3)
177                 reg32 |= 0x10;  /* 667MHz */
178         EPBAR32(0x2c) = reg32;
179
180         EPBAR32(EPVC1MTS) = 0x0a0a0a0a;
181
182         reg32 = EPBAR32(EPVC1RCAP);
183         reg32 &= ~(0x7f << 16);
184         reg32 |= (0x0a << 16);
185         EPBAR32(EPVC1RCAP) = reg32;
186
187         if ((MCHBAR32(CLKCFG) & 7) == 1) {      /* 533MHz */
188                 EPBAR32(EPVC1IST + 0) = 0x009c009c;
189                 EPBAR32(EPVC1IST + 4) = 0x009c009c;
190         }
191
192         if ((MCHBAR32(CLKCFG) & 7) == 3) {      /* 667MHz */
193                 EPBAR32(EPVC1IST + 0) = 0x00c000c0;
194                 EPBAR32(EPVC1IST + 4) = 0x00c000c0;
195         }
196
197         /* Is internal graphics enabled? */
198         if (pci_read_config8(PCI_DEV(0, 0x0, 0), 54) & ((1 << 4) | (1 << 3))) { /* DEVEN */
199                 MCHBAR32(MMARB1) |= (1 << 17);
200         }
201
202         /* Assign Virtual Channel ID 1 to VC1 */
203         reg32 = EPBAR32(EPVC1RCTL);
204         reg32 &= ~(7 << 24);
205         reg32 |= (1 << 24);
206         EPBAR32(EPVC1RCTL) = reg32;
207         
208         reg32 = EPBAR32(EPVC1RCTL);
209         reg32 &= 0xffffff01;
210         reg32 |= (1 << 7);
211         EPBAR32(EPVC1RCTL) = reg32;
212         
213         EPBAR32(PORTARB + 0x00) = 0x01000001;
214         EPBAR32(PORTARB + 0x04) = 0x00040000;
215         EPBAR32(PORTARB + 0x08) = 0x00001000;
216         EPBAR32(PORTARB + 0x0c) = 0x00000040;
217         EPBAR32(PORTARB + 0x10) = 0x01000001;
218         EPBAR32(PORTARB + 0x14) = 0x00040000;
219         EPBAR32(PORTARB + 0x18) = 0x00001000;
220         EPBAR32(PORTARB + 0x1c) = 0x00000040;
221         
222         EPBAR32(EPVC1RCTL) |= (1 << 16);
223         EPBAR32(EPVC1RCTL) |= (1 << 16);
224
225         printk_debug("Loading port arbitration table ...");
226         /* Loop until bit 0 becomes 0 */
227         timeout = 0x7fffff;
228         while ((EPBAR16(EPVC1RSTS) & 1) && --timeout) ;
229         if (!timeout)
230                 printk_debug("timeout!\n");
231         else
232                 printk_debug("ok\n");
233
234         /* Now enable VC1 */
235         EPBAR32(EPVC1RCTL) |= (1 << 31);
236
237         printk_debug("Wait for VC1 negotiation ...");
238         /* Wait for VC1 negotiation pending */
239         timeout = 0x7fff;
240         while ((EPBAR16(EPVC1RSTS) & (1 << 1)) && --timeout) ;
241         if (!timeout)
242                 printk_debug("timeout!\n");
243         else
244                 printk_debug("ok\n");
245
246 }
247
248 static void ich7_setup_dmi_rcrb(void)
249 {
250         u16 reg16;
251
252         
253         reg16 = RCBA16(LCTL);
254         reg16 &= ~(3 << 0);
255         reg16 |= 1;
256         RCBA16(LCTL) = reg16;
257
258         RCBA32(V0CTL) = 0x80000001;
259         RCBA32(V1CAP) = 0x03128010;
260         RCBA32(ESD) = 0x00000810;
261         RCBA32(RP1D) = 0x01000003;
262         RCBA32(RP2D) = 0x02000002;
263         RCBA32(RP3D) = 0x03000002;
264         RCBA32(RP4D) = 0x04000002;
265         RCBA32(HDD) = 0x0f000003;
266         RCBA32(RP5D) = 0x05000002;
267
268         RCBA32(RPFN) = 0x00543210;
269
270         pci_write_config16(PCI_DEV(0, 0x1c, 0), 0x42, 0x0141);  
271         pci_write_config16(PCI_DEV(0, 0x1c, 4), 0x42, 0x0141);  
272         pci_write_config16(PCI_DEV(0, 0x1c, 5), 0x42, 0x0141);  
273
274         pci_write_config32(PCI_DEV(0, 0x1c, 4), 0x54, 0x00480ce0);      
275         pci_write_config32(PCI_DEV(0, 0x1c, 5), 0x54, 0x00500ce0);      
276 }
277
278 static void i945_setup_dmi_rcrb(void)
279 {
280         u32 reg32;
281         u32 timeout;
282
283         printk_debug("Setting up DMI RCRB\n");
284
285         /* Virtual Channel 0 Configuration */
286         reg32 = DMIBAR32(DMIVC0RCTL0);
287         reg32 &= 0xffffff01;
288         DMIBAR32(DMIVC0RCTL0) = reg32;
289         
290         reg32 = DMIBAR32(DMIPVCCAP1);
291         reg32 &= ~(7 << 0);
292         reg32 |= 1;
293         DMIBAR32(DMIPVCCAP1) = reg32;
294         
295         reg32 = DMIBAR32(DMIVC1RCTL);
296         reg32 &= ~(7 << 24);
297         reg32 |= (1 << 24);     /* NOTE: This ID must match ICH7 side */
298         DMIBAR32(DMIVC1RCTL) = reg32;
299         
300         reg32 = DMIBAR32(DMIVC1RCTL);
301         reg32 &= 0xffffff01;
302         reg32 |= (1 << 7);
303         DMIBAR32(DMIVC1RCTL) = reg32;
304
305         /* Now enable VC1 */
306         DMIBAR32(DMIVC1RCTL) |= (1 << 31);
307
308         printk_debug("Wait for VC1 negotiation ...");
309         /* Wait for VC1 negotiation pending */
310         timeout = 0x7ffff;
311         while ((DMIBAR16(DMIVC1RSTS) & (1 << 1)) && --timeout) ;
312         if (!timeout)
313                 printk_debug("timeout!\n");
314         else
315                 printk_debug("done..\n");
316 #if 1
317         /* Enable Active State Power Management (ASPM) L0 state */
318
319         reg32 = DMIBAR32(DMILCAP);
320         reg32 &= ~(7 << 12);
321         reg32 |= (2 << 12);     
322
323         reg32 &= ~(7 << 15);
324         
325         reg32 |= (2 << 15);     
326         DMIBAR32(DMILCAP) = reg32;
327
328         reg32 = DMIBAR32(DMICC);
329         reg32 &= 0x00ffffff;    
330         reg32 &= ~(3 << 0);
331         reg32 |= (1 << 0);      
332         DMIBAR32(DMICC) = reg32;
333
334         if (0) {
335                 DMIBAR32(DMILCTL) |= (3 << 0);
336         }
337 #endif
338
339         /* Last but not least, some additional steps */
340         reg32 = MCHBAR32(FSBSNPCTL);
341         reg32 &= ~(0xff << 2);
342         reg32 |= (0xaa << 2);
343         MCHBAR32(FSBSNPCTL) = reg32;
344
345         DMIBAR32(0x2c) = 0x86000040;
346
347         reg32 = DMIBAR32(0x204);
348         reg32 &= ~0x3ff;
349 #if 1
350         reg32 |= 0x13f;         /* for x4 DMI only */
351 #else
352         reg32 |= 0x1e4; /* for x2 DMI only */
353 #endif
354         DMIBAR32(0x204) = reg32;
355
356         if (pci_read_config8(PCI_DEV(0, 0x0, 0), 54) & ((1 << 4) | (1 << 3))) { /* DEVEN */
357                 DMIBAR32(0x200) |= (1 << 21);
358         } else {
359                 DMIBAR32(0x200) &= ~(1 << 21);
360         }
361
362         reg32 = DMIBAR32(0x204);
363         reg32 &= ~((1 << 11) | (1 << 10));
364         DMIBAR32(0x204) = reg32;
365
366         reg32 = DMIBAR32(0x204);
367         reg32 &= ~(0xff << 12);
368         reg32 |= (0x0d << 12);
369         DMIBAR32(0x204) = reg32;
370
371         DMIBAR32(DMICTL1) |= (3 << 24);
372
373         reg32 = DMIBAR32(0x200);
374         reg32 &= ~(0x3 << 26);
375         reg32 |= (0x02 << 26);
376         DMIBAR32(0x200) = reg32;
377
378         DMIBAR32(DMIDRCCFG) &= ~(1 << 31);
379         DMIBAR32(DMICTL2) |= (1 << 31);
380
381         if (i945_silicon_revision() >= 3) {
382                 reg32 = DMIBAR32(0xec0);
383                 reg32 &= 0x0fffffff;
384                 reg32 |= (2 << 28);
385                 DMIBAR32(0xec0) = reg32;
386
387                 reg32 = DMIBAR32(0xed4);
388                 reg32 &= 0x0fffffff;
389                 reg32 |= (2 << 28);
390                 DMIBAR32(0xed4) = reg32;
391
392                 reg32 = DMIBAR32(0xee8);
393                 reg32 &= 0x0fffffff;
394                 reg32 |= (2 << 28);
395                 DMIBAR32(0xee8) = reg32;
396
397                 reg32 = DMIBAR32(0xefc);
398                 reg32 &= 0x0fffffff;
399                 reg32 |= (2 << 28);
400                 DMIBAR32(0xefc) = reg32;
401         }
402
403         /* wait for bit toggle to 0 */
404         printk_debug("Waiting for DMI hardware...");
405         timeout = 0x7fffff;
406         while ((DMIBAR8(0x32) & (1 << 1)) && --timeout) ;
407         if (!timeout)
408                 printk_debug("timeout!\n");
409         else
410                 printk_debug("ok\n");
411         
412         DMIBAR32(0x1c4) = 0xffffffff;
413         DMIBAR32(0x1d0) = 0xffffffff;
414         DMIBAR32(0x228) = 0xffffffff;
415         
416         DMIBAR32(0x308) = DMIBAR32(0x308);
417         DMIBAR32(0x314) = DMIBAR32(0x314);
418         DMIBAR32(0x324) = DMIBAR32(0x324);
419         DMIBAR32(0x328) = DMIBAR32(0x328);
420         DMIBAR32(0x338) = DMIBAR32(0x334);
421         DMIBAR32(0x338) = DMIBAR32(0x338);
422
423         if (i945_silicon_revision() == 1 && ((MCHBAR8(0xe08) & (1 << 5)) == 1)) {
424                 if ((MCHBAR32(0x214) & 0xf) != 0x3) {   
425                         printk_info
426                             ("DMI link requires A1 stepping workaround. Rebooting.\n");
427                         reg32 = MCHBAR32(MMARB1);
428                         reg32 &= 0xfffffff8;
429                         reg32 |= 3;
430                         outb(0x06, 0xcf9);
431                         for (;;) ;      /* wait for reset */
432                 }
433         }
434 }
435
436 static void i945_setup_pci_express_x16(void)
437 {
438         u32 timeout;
439         u32 reg32;
440         u16 reg16;
441
442         /* For now we just disable the x16 link */
443         printk_debug("Disabling PCI Express x16 Link\n");
444
445         MCHBAR16(UPMC1) |= (1 << 5) | (1 << 0);
446
447         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), BCTRL1);
448         reg16 |= (1 << 6);
449         pcie_write_config16(PCI_DEV(0, 0x01, 0), BCTRL1, reg16);
450
451         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x224);
452         reg32 |= (1 << 8);
453         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x224, reg32);
454
455         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), BCTRL1);
456         reg16 &= ~(1 << 6);
457         pcie_write_config16(PCI_DEV(0, 0x01, 0), BCTRL1, reg16);
458
459         printk_debug("Wait for link to enter detect state... ");
460         timeout = 0x7fffff;
461         for (reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214);
462              (reg32 & 0x000f0000) && --timeout;) ;
463         if (!timeout)
464                 printk_debug("timeout!\n");
465         else
466                 printk_debug("ok\n");
467
468         /* Finally: Disable the PCI config header */
469         reg16 = pci_read_config16(PCI_DEV(0, 0x00, 0), DEVEN);
470         reg16 &= ~DEVEN_D1F0;
471         pci_write_config16(PCI_DEV(0, 0x00, 0), DEVEN, reg16);
472 }
473
474 static void i945_setup_root_complex_topology(void)
475 {
476         u32 reg32;
477
478         printk_debug("Setting up Root Complex Topology\n");
479         /* Egress Port Root Topology */
480         reg32 = EPBAR32(EPESD);
481         reg32 &= 0xff00ffff;
482         reg32 |= (1 << 16);
483         EPBAR32(EPESD) = reg32;
484         
485         EPBAR32(EPLE1D) |= (1 << 0);
486         
487         EPBAR32(EPLE1A) = DEFAULT_PCIEXBAR + 0x4000;
488         
489         EPBAR32(EPLE2D) |= (1 << 0);
490
491         /* DMI Port Root Topology */
492         reg32 = DMIBAR32(DMILE1D);
493         reg32 &= 0x00ffffff;
494         DMIBAR32(DMILE1D) = reg32;
495         
496         reg32 = DMIBAR32(DMILE1D);
497         reg32 &= 0xff00ffff;
498         reg32 |= (2 << 16);
499         DMIBAR32(DMILE1D) = reg32;
500         
501         DMIBAR32(DMILE1D) |= (1 << 0);
502         
503         DMIBAR32(DMILE1A) = DEFAULT_PCIEXBAR + 0x8000;
504         
505         DMIBAR32(DMILE2D) |= (1 << 0);
506         
507         DMIBAR32(DMILE2A) = DEFAULT_PCIEXBAR + 0x5000;
508
509         /* PCI Express x16 Port Root Topology */
510         if (pci_read_config8(PCI_DEV(0, 0x00, 0), DEVEN) & DEVEN_D1F0) {
511                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x158,
512                                     DEFAULT_PCIEXBAR + 0x5000);
513
514                 reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x150);
515                 reg32 |= (1 << 0);
516                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x150, reg32);
517         }
518 }
519
520 static void ich7_setup_root_complex_topology(void)
521 {
522         RCBA32(0x104) = 0x00000802;
523         RCBA32(0x110) = 0x00000001;
524         RCBA32(0x114) = 0x00000000;
525         RCBA32(0x118) = 0x00000000;
526 }
527
528 static void ich7_setup_pci_express(void)
529 {
530         RCBA32(CG) |= (1 << 0); 
531         
532         pci_write_config32(PCI_DEV(0, 0x1c, 0), 0x54, 0x00000060);
533
534         pci_write_config32(PCI_DEV(0, 0x1c, 0), 0xd8, 0x00110000);
535 }
536
537 static void i945_early_initialization(void)
538 {
539         /* Print some chipset specific information */
540         i945_detect_chipset();
541
542         /* Setup all BARs required for early PCIe and raminit */
543         i945_setup_bars();
544
545         /* Change port80 to LPC */
546         RCBA32(GCS) &= (~0x04);
547 }
548
549 static void i945_late_initialization(void)
550 {
551         i945_setup_egress_port();
552
553         ich7_setup_root_complex_topology();
554
555         ich7_setup_pci_express();
556
557         ich7_setup_dmi_rcrb();
558
559         i945_setup_dmi_rcrb();
560
561         i945_setup_pci_express_x16();
562
563         i945_setup_root_complex_topology();
564 }