printk_foo -> printk(BIOS_FOO, ...)
[coreboot.git] / src / northbridge / intel / i945 / early_init.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2007-2009 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 i945m_detect_chipset(void)
29 {
30         u8 reg8;
31
32         printk(BIOS_INFO, "\n");
33         reg8 = (pci_read_config8(PCI_DEV(0, 0x00, 0), 0xe7) & 0x70) >> 4;
34         switch (reg8) {
35         case 1:
36                 printk(BIOS_INFO, "Mobile Intel(R) 82945GM/GME Express");
37                 break;
38         case 2:
39                 printk(BIOS_INFO, "Mobile Intel(R) 82945GMS/GU Express");
40                 break;
41         case 3:
42                 printk(BIOS_INFO, "Mobile Intel(R) 82945PM Express");
43                 break;
44         case 5:
45                 printk(BIOS_INFO, "Intel(R) 82945GT Express");
46                 break;
47         case 6:
48                 printk(BIOS_INFO, "Mobile Intel(R) 82943/82940GML Express");
49                 break;
50         default:
51                 printk(BIOS_INFO, "Unknown (%02x)", reg8);      /* Others reserved. */
52         }
53         printk(BIOS_INFO, " Chipset\n");
54
55         printk(BIOS_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(BIOS_DEBUG, "800 MHz"); /* According to 965 spec */
60                 break;
61         case 3:
62                 printk(BIOS_DEBUG, "667 MHz");
63                 break;
64         case 4:
65                 printk(BIOS_DEBUG, "533 MHz");
66                 break;
67         default:
68                 printk(BIOS_DEBUG, "N/A MHz (%02x)", reg8);
69         }
70         printk(BIOS_DEBUG, "\n");
71
72         printk(BIOS_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(BIOS_DEBUG, "up to DDR2-667");
77                 break;
78         case 3:
79                 printk(BIOS_DEBUG, "up to DDR2-533");
80                 break;
81         case 4:
82                 printk(BIOS_DEBUG, "DDR2-400");
83                 break;
84         default:
85                 printk(BIOS_INFO, "unknown max. RAM clock (%02x).", reg8);      /* Others reserved. */
86         }
87         printk(BIOS_DEBUG, "\n");
88 }
89
90 static void i945_detect_chipset(void)
91 {
92         u8 reg8;
93
94         printk(BIOS_INFO, "\nIntel(R) ");
95
96         reg8 = ((pci_read_config8(PCI_DEV(0, 0x00, 0), 0xe7) >> 5) & 4) | ((pci_read_config8(PCI_DEV(0, 0x00, 0), 0xe4) >> 4) & 3);
97         switch (reg8) {
98         case 0:
99         case 1:
100                 printk(BIOS_INFO, "82945G");
101                 break;
102         case 2:
103         case 3:
104                 printk(BIOS_INFO, "82945P");
105                 break;
106         case 4:
107                 printk(BIOS_INFO, "82945GC");
108                 break;
109         case 5:
110                 printk(BIOS_INFO, "82945GZ");
111                 break;
112         case 6:
113         case 7:
114                 printk(BIOS_INFO, "82945PL");
115                 break;
116         default:
117                 break;
118         }
119         printk(BIOS_INFO, " Chipset\n");
120
121         printk(BIOS_DEBUG, "(G)MCH capable of ");
122         reg8 = (pci_read_config8(PCI_DEV(0, 0x00, 0), 0xe4) & 0x07);
123         switch (reg8) {
124         case 0:
125                 printk(BIOS_DEBUG, "up to DDR2-667");
126                 break;
127         case 3:
128                 printk(BIOS_DEBUG, "up to DDR2-533");
129                 break;
130         default:
131                 printk(BIOS_INFO, "unknown max. RAM clock (%02x).", reg8);      /* Others reserved. */
132         }
133         printk(BIOS_DEBUG, "\n");
134 }
135
136 static void i945_setup_bars(void)
137 {
138         u8 reg8;
139
140         /* As of now, we don't have all the A0 workarounds implemented */
141         if (i945_silicon_revision() == 0)
142                 printk(BIOS_INFO, "Warning: i945 silicon revision A0 might not work correctly.\n");
143
144         /* Setting up Southbridge. In the northbridge code. */
145         printk(BIOS_DEBUG, "Setting up static southbridge registers...");
146         pci_write_config32(PCI_DEV(0, 0x1f, 0), RCBA, DEFAULT_RCBA | 1);
147
148         pci_write_config32(PCI_DEV(0, 0x1f, 0), PMBASE, DEFAULT_PMBASE | 1);
149         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0x44 /* ACPI_CNTL */ , 0x80); /* Enable ACPI BAR */
150
151         pci_write_config32(PCI_DEV(0, 0x1f, 0), GPIOBASE, DEFAULT_GPIOBASE | 1);
152         pci_write_config8(PCI_DEV(0, 0x1f, 0), 0x4c /* GC */ , 0x10);   /* Enable GPIOs */
153         setup_ich7_gpios();
154         printk(BIOS_DEBUG, " done.\n");
155
156         printk(BIOS_DEBUG, "Disabling Watchdog reboot...");
157         RCBA32(GCS) = (RCBA32(0x3410)) | (1 << 5);      /* No reset */
158         outw((1 << 11), DEFAULT_PMBASE | 0x60 | 0x08);  /* halt timer */
159         printk(BIOS_DEBUG, " done.\n");
160
161         printk(BIOS_DEBUG, "Setting up static northbridge registers...");
162         /* Set up all hardcoded northbridge BARs */
163         pci_write_config32(PCI_DEV(0, 0x00, 0), EPBAR, DEFAULT_EPBAR | 1);
164         pci_write_config32(PCI_DEV(0, 0x00, 0), MCHBAR, DEFAULT_MCHBAR | 1);
165         pci_write_config32(PCI_DEV(0, 0x00, 0), PCIEXBAR, DEFAULT_PCIEXBAR | 5); /* 64MB - busses 0-63 */
166         pci_write_config32(PCI_DEV(0, 0x00, 0), DMIBAR, DEFAULT_DMIBAR | 1);
167         pci_write_config32(PCI_DEV(0, 0x00, 0), X60BAR, DEFAULT_X60BAR | 1);
168
169         /* Hardware default is 8MB UMA. If someone wants to make this a
170          * CMOS or compile time option, send a patch.
171          * pci_write_config16(PCI_DEV(0, 0x00, 0), GGC, 0x30);
172          */
173
174         /* Set C0000-FFFFF to access RAM on both reads and writes */
175         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM0, 0x30);
176         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM1, 0x33);
177         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM2, 0x33);
178         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM3, 0x33);
179         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM4, 0x33);
180         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM5, 0x33);
181         pci_write_config8(PCI_DEV(0, 0x00, 0), PAM6, 0x33);
182
183         pci_write_config32(PCI_DEV(0, 0x00, 0), SKPAD, 0xcafebabe);
184         printk(BIOS_DEBUG, " done.\n");
185
186         /* Wait for MCH BAR to come up */
187         printk(BIOS_DEBUG, "Waiting for MCHBAR to come up...");
188         if ((pci_read_config8(PCI_DEV(0, 0x0f, 0), 0xe6) & 0x2) == 0x00) { /* Bit 49 of CAPID0 */
189                 do {
190                         reg8 = *(volatile u8 *)0xfed40000;
191                 } while (!(reg8 & 0x80));
192         }
193         printk(BIOS_DEBUG, "ok\n");
194 }
195
196 static void i945_setup_egress_port(void)
197 {
198         u32 reg32;
199         u32 timeout;
200
201         printk(BIOS_DEBUG, "Setting up Egress Port RCRB\n");
202
203         /* Egress Port Virtual Channel 0 Configuration */
204
205         /* map only TC0 to VC0 */
206         reg32 = EPBAR32(EPVC0RCTL);
207         reg32 &= 0xffffff01;
208         EPBAR32(EPVC0RCTL) = reg32;
209
210         reg32 = EPBAR32(EPPVCCAP1);
211         reg32 &= ~(7 << 0);
212         reg32 |= 1;
213         EPBAR32(EPPVCCAP1) = reg32;
214
215         /* Egress Port Virtual Channel 1 Configuration */
216         reg32 = EPBAR32(0x2c);
217         reg32 &= 0xffffff00;
218         if ((MCHBAR32(CLKCFG) & 7) == 1)
219                 reg32 |= 0x0d;  /* 533MHz */
220         if ((MCHBAR32(CLKCFG) & 7) == 3)
221                 reg32 |= 0x10;  /* 667MHz */
222         EPBAR32(0x2c) = reg32;
223
224         EPBAR32(EPVC1MTS) = 0x0a0a0a0a;
225
226         reg32 = EPBAR32(EPVC1RCAP);
227         reg32 &= ~(0x7f << 16);
228         reg32 |= (0x0a << 16);
229         EPBAR32(EPVC1RCAP) = reg32;
230
231         if ((MCHBAR32(CLKCFG) & 7) == 1) {      /* 533MHz */
232                 EPBAR32(EPVC1IST + 0) = 0x009c009c;
233                 EPBAR32(EPVC1IST + 4) = 0x009c009c;
234         }
235
236         if ((MCHBAR32(CLKCFG) & 7) == 3) {      /* 667MHz */
237                 EPBAR32(EPVC1IST + 0) = 0x00c000c0;
238                 EPBAR32(EPVC1IST + 4) = 0x00c000c0;
239         }
240
241         /* Is internal graphics enabled? */
242         if (pci_read_config8(PCI_DEV(0, 0x0, 0), 0x54) & ((1 << 4) | (1 << 3))) {       /* DEVEN */
243                 MCHBAR32(MMARB1) |= (1 << 17);
244         }
245
246         /* Assign Virtual Channel ID 1 to VC1 */
247         reg32 = EPBAR32(EPVC1RCTL);
248         reg32 &= ~(7 << 24);
249         reg32 |= (1 << 24);
250         EPBAR32(EPVC1RCTL) = reg32;
251
252         reg32 = EPBAR32(EPVC1RCTL);
253         reg32 &= 0xffffff01;
254         reg32 |= (1 << 7);
255         EPBAR32(EPVC1RCTL) = reg32;
256
257         EPBAR32(PORTARB + 0x00) = 0x01000001;
258         EPBAR32(PORTARB + 0x04) = 0x00040000;
259         EPBAR32(PORTARB + 0x08) = 0x00001000;
260         EPBAR32(PORTARB + 0x0c) = 0x00000040;
261         EPBAR32(PORTARB + 0x10) = 0x01000001;
262         EPBAR32(PORTARB + 0x14) = 0x00040000;
263         EPBAR32(PORTARB + 0x18) = 0x00001000;
264         EPBAR32(PORTARB + 0x1c) = 0x00000040;
265
266         EPBAR32(EPVC1RCTL) |= (1 << 16);
267         EPBAR32(EPVC1RCTL) |= (1 << 16);
268
269         printk(BIOS_DEBUG, "Loading port arbitration table ...");
270         /* Loop until bit 0 becomes 0 */
271         timeout = 0x7fffff;
272         while ((EPBAR16(EPVC1RSTS) & 1) && --timeout) ;
273         if (!timeout)
274                 printk(BIOS_DEBUG, "timeout!\n");
275         else
276                 printk(BIOS_DEBUG, "ok\n");
277
278         /* Now enable VC1 */
279         EPBAR32(EPVC1RCTL) |= (1 << 31);
280
281         printk(BIOS_DEBUG, "Wait for VC1 negotiation ...");
282         /* Wait for VC1 negotiation pending */
283         timeout = 0x7fff;
284         while ((EPBAR16(EPVC1RSTS) & (1 << 1)) && --timeout) ;
285         if (!timeout)
286                 printk(BIOS_DEBUG, "timeout!\n");
287         else
288                 printk(BIOS_DEBUG, "ok\n");
289
290 }
291
292 static void ich7_setup_dmi_rcrb(void)
293 {
294         u16 reg16;
295         u32 reg32;
296
297         reg16 = RCBA16(LCTL);
298         reg16 &= ~(3 << 0);
299         reg16 |= 3;
300         RCBA16(LCTL) = reg16;
301
302         RCBA32(V0CTL) = 0x80000001;
303         RCBA32(V1CAP) = 0x03128010;
304         RCBA32(ESD) = 0x00000810;
305         RCBA32(RP1D) = 0x01000003;
306         RCBA32(RP2D) = 0x02000002;
307         RCBA32(RP3D) = 0x03000002;
308         RCBA32(RP4D) = 0x04000002;
309         RCBA32(HDD) = 0x0f000003;
310         RCBA32(RP5D) = 0x05000002;
311
312         RCBA32(RPFN) = 0x00543210;
313
314         pci_write_config16(PCI_DEV(0, 0x1c, 0), 0x42, 0x0141);
315         pci_write_config16(PCI_DEV(0, 0x1c, 4), 0x42, 0x0141);
316         pci_write_config16(PCI_DEV(0, 0x1c, 5), 0x42, 0x0141);
317
318         pci_write_config32(PCI_DEV(0, 0x1c, 4), 0x54, 0x00480ce0);
319         pci_write_config32(PCI_DEV(0, 0x1c, 5), 0x54, 0x00500ce0);
320
321         reg32 = RCBA32(V1CTL);
322         reg32 &= ~( (0x7f << 1) | (7 << 17) | (7 << 24) );
323         reg32 |= (0x40 << 1) | (4 << 17) | (1 << 24) | (1 << 31);
324         RCBA32(V1CTL) = reg32;
325
326         RCBA32(ESD) |= (2 << 16);
327
328         RCBA32(ULD) |= (1 << 24) | (1 << 16);
329
330         RCBA32(ULBA) = DEFAULT_DMIBAR;
331
332         RCBA32(RP1D) |= (2 << 16);
333         RCBA32(RP2D) |= (2 << 16);
334         RCBA32(RP3D) |= (2 << 16);
335         RCBA32(RP4D) |= (2 << 16);
336         RCBA32(HDD)  |= (2 << 16);
337         RCBA32(RP5D) |= (2 << 16);
338         RCBA32(RP6D) |= (2 << 16);
339
340         RCBA32(LCAP) |= (3 << 10);
341 }
342
343 static void i945_setup_dmi_rcrb(void)
344 {
345         u32 reg32;
346         u32 timeout;
347
348         int activate_aspm = 1;
349
350         printk(BIOS_DEBUG, "Setting up DMI RCRB\n");
351
352         /* Virtual Channel 0 Configuration */
353         reg32 = DMIBAR32(DMIVC0RCTL0);
354         reg32 &= 0xffffff01;
355         DMIBAR32(DMIVC0RCTL0) = reg32;
356
357         reg32 = DMIBAR32(DMIPVCCAP1);
358         reg32 &= ~(7 << 0);
359         reg32 |= 1;
360         DMIBAR32(DMIPVCCAP1) = reg32;
361
362         reg32 = DMIBAR32(DMIVC1RCTL);
363         reg32 &= ~(7 << 24);
364         reg32 |= (1 << 24);     /* NOTE: This ID must match ICH7 side */
365         DMIBAR32(DMIVC1RCTL) = reg32;
366
367         reg32 = DMIBAR32(DMIVC1RCTL);
368         reg32 &= 0xffffff01;
369         reg32 |= (1 << 7);
370         DMIBAR32(DMIVC1RCTL) = reg32;
371
372         /* Now enable VC1 */
373         DMIBAR32(DMIVC1RCTL) |= (1 << 31);
374
375         printk(BIOS_DEBUG, "Wait for VC1 negotiation ...");
376         /* Wait for VC1 negotiation pending */
377         timeout = 0x7ffff;
378         while ((DMIBAR16(DMIVC1RSTS) & (1 << 1)) && --timeout) ;
379         if (!timeout)
380                 printk(BIOS_DEBUG, "timeout!\n");
381         else
382                 printk(BIOS_DEBUG, "done..\n");
383 #if 1
384         /* Enable Active State Power Management (ASPM) L0 state */
385
386         reg32 = DMIBAR32(DMILCAP);
387         reg32 &= ~(7 << 12);
388         reg32 |= (2 << 12);
389
390         reg32 &= ~(7 << 15);
391
392         reg32 |= (2 << 15);
393         DMIBAR32(DMILCAP) = reg32;
394
395         reg32 = DMIBAR32(DMICC);
396         reg32 &= 0x00ffffff;
397         reg32 &= ~(3 << 0);
398         reg32 |= (1 << 0);
399         reg32 &= ~(3 << 20);
400         reg32 |= (1 << 20);
401
402         DMIBAR32(DMICC) = reg32;
403
404         if (activate_aspm) {
405                 DMIBAR32(DMILCTL) |= (3 << 0);
406         }
407 #endif
408
409         /* Last but not least, some additional steps */
410         reg32 = MCHBAR32(FSBSNPCTL);
411         reg32 &= ~(0xff << 2);
412         reg32 |= (0xaa << 2);
413         MCHBAR32(FSBSNPCTL) = reg32;
414
415         DMIBAR32(0x2c) = 0x86000040;
416
417         reg32 = DMIBAR32(0x204);
418         reg32 &= ~0x3ff;
419 #if 1
420         reg32 |= 0x13f;         /* for x4 DMI only */
421 #else
422         reg32 |= 0x1e4; /* for x2 DMI only */
423 #endif
424         DMIBAR32(0x204) = reg32;
425
426         if (pci_read_config8(PCI_DEV(0, 0x0, 0), 0x54) & ((1 << 4) | (1 << 3))) {       /* DEVEN */
427                 printk(BIOS_DEBUG, "Internal graphics: enabled\n");
428                 DMIBAR32(0x200) |= (1 << 21);
429         } else {
430                 printk(BIOS_DEBUG, "Internal graphics: disabled\n");
431                 DMIBAR32(0x200) &= ~(1 << 21);
432         }
433
434         reg32 = DMIBAR32(0x204);
435         reg32 &= ~((1 << 11) | (1 << 10));
436         DMIBAR32(0x204) = reg32;
437
438         reg32 = DMIBAR32(0x204);
439         reg32 &= ~(0xff << 12);
440         reg32 |= (0x0d << 12);
441         DMIBAR32(0x204) = reg32;
442
443         DMIBAR32(DMICTL1) |= (3 << 24);
444
445         reg32 = DMIBAR32(0x200);
446         reg32 &= ~(0x3 << 26);
447         reg32 |= (0x02 << 26);
448         DMIBAR32(0x200) = reg32;
449
450         DMIBAR32(DMIDRCCFG) &= ~(1 << 31);
451         DMIBAR32(DMICTL2) |= (1 << 31);
452
453         if (i945_silicon_revision() >= 3) {
454                 reg32 = DMIBAR32(0xec0);
455                 reg32 &= 0x0fffffff;
456                 reg32 |= (2 << 28);
457                 DMIBAR32(0xec0) = reg32;
458
459                 reg32 = DMIBAR32(0xed4);
460                 reg32 &= 0x0fffffff;
461                 reg32 |= (2 << 28);
462                 DMIBAR32(0xed4) = reg32;
463
464                 reg32 = DMIBAR32(0xee8);
465                 reg32 &= 0x0fffffff;
466                 reg32 |= (2 << 28);
467                 DMIBAR32(0xee8) = reg32;
468
469                 reg32 = DMIBAR32(0xefc);
470                 reg32 &= 0x0fffffff;
471                 reg32 |= (2 << 28);
472                 DMIBAR32(0xefc) = reg32;
473         }
474
475         /* wait for bit toggle to 0 */
476         printk(BIOS_DEBUG, "Waiting for DMI hardware...");
477         timeout = 0x7fffff;
478         while ((DMIBAR8(0x32) & (1 << 1)) && --timeout) ;
479         if (!timeout)
480                 printk(BIOS_DEBUG, "timeout!\n");
481         else
482                 printk(BIOS_DEBUG, "ok\n");
483
484         DMIBAR32(0x1c4) = 0xffffffff;
485         DMIBAR32(0x1d0) = 0xffffffff;
486         DMIBAR32(0x228) = 0xffffffff;
487
488         DMIBAR32(0x308) = DMIBAR32(0x308);
489         DMIBAR32(0x314) = DMIBAR32(0x314);
490         DMIBAR32(0x324) = DMIBAR32(0x324);
491         DMIBAR32(0x328) = DMIBAR32(0x328);
492         DMIBAR32(0x338) = DMIBAR32(0x334);
493         DMIBAR32(0x338) = DMIBAR32(0x338);
494
495         if (i945_silicon_revision() == 1 && ((MCHBAR8(0xe08) & (1 << 5)) == 1)) {
496                 if ((MCHBAR32(0x214) & 0xf) != 0x3) {
497                         printk(BIOS_INFO, "DMI link requires A1 stepping workaround. Rebooting.\n");
498                         reg32 = DMIBAR32(0x224);
499                         reg32 &= ~(7 << 0);
500                         reg32 |= (3 << 0);
501                         DMIBAR32(0x224) = reg32;
502                         outb(0x06, 0xcf9);
503                         for (;;) ;      /* wait for reset */
504                 }
505         }
506 }
507
508 static void i945_setup_pci_express_x16(void)
509 {
510         u32 timeout;
511         u32 reg32;
512         u16 reg16;
513
514         u8 reg8;
515
516         printk(BIOS_DEBUG, "Enabling PCI Express x16 Link\n");
517
518         reg16 = pci_read_config16(PCI_DEV(0, 0x00, 0), DEVEN);
519         reg16 |= DEVEN_D1F0;
520         pci_write_config16(PCI_DEV(0, 0x00, 0), DEVEN, reg16);
521
522         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x208);
523         reg32 &= ~(1 << 8);
524         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x208, reg32);
525
526         /* We have no success with querying the usual PCIe registers
527          * for link setup success on the i945. Hence we assign a temporary
528          * PCI bus 0x0a and check whether we find a device on 0:a.0
529          */
530
531         /* First we reset the secondary bus */
532         reg16 = pci_read_config16(PCI_DEV(0, 0x01, 0), 0x3e);
533         reg16 |= (1 << 6);
534         pci_write_config16(PCI_DEV(0, 0x01, 0), 0x3e, reg16);
535         /* Read back and clear reset bit. */
536         reg16 = pci_read_config16(PCI_DEV(0, 0x01, 0), 0x3e);
537         reg16 &= ~(1 << 6);
538         pci_write_config16(PCI_DEV(0, 0x01, 0), 0x3e, reg16);
539
540         reg16 = pci_read_config16(PCI_DEV(0, 0x01, 0), 0xba);
541         printk(BIOS_DEBUG, "SLOTSTS: %04x\n", reg16);
542         if (!(reg16 & 0x48)) {
543                 goto disable_pciexpress_x16_link;
544         }
545         reg16 |= (1 << 4) | (1 << 0);
546         pci_write_config16(PCI_DEV(0, 0x01, 0), 0xba, reg16);
547
548         pci_write_config8(PCI_DEV(0, 0x01, 0), 0x19, 0x00);
549         pci_write_config8(PCI_DEV(0, 0x01, 0), 0x1a, 0x00);
550         pci_write_config8(PCI_DEV(0, 0x01, 0), 0x19, 0x0a);
551         pci_write_config8(PCI_DEV(0, 0x01, 0), 0x1a, 0x0a);
552
553         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x224);
554         reg32 &= ~(1 << 8);
555         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x224, reg32);
556
557         MCHBAR16(UPMC1) &= ~( (1 << 5) | (1 << 0) );
558
559         /* Initialze PEG_CAP */
560         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), 0xa2);
561         reg16 |= (1 << 8);
562         pcie_write_config16(PCI_DEV(0, 0x01, 0), 0xa2, reg16);
563
564         /* Setup SLOTCAP */
565         /* TODO: These values are mainboard dependent and should
566          * be set from Config.lb or Options.lb.
567          */
568         /* NOTE: SLOTCAP becomes RO after the first write! */
569         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xb4);
570         reg32 &= 0x0007ffff;
571
572         reg32 &= 0xfffe007f;
573
574         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xb4, reg32);
575
576         /* Wait for training to succeed */
577         printk(BIOS_DEBUG, "PCIe link training ...");
578         timeout = 0x7ffff;
579         while ((((pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214) >> 16) & 4) != 3)  && --timeout) ;
580
581         reg32 = pci_read_config32(PCI_DEV(0x0a, 0x0, 0), 0);
582         if (reg32 != 0x00000000 && reg32 != 0xffffffff) {
583                 printk(BIOS_DEBUG, " Detected PCIe device %04x:%04x\n",
584                                 reg32 & 0xffff, reg32 >> 16);
585         } else {
586                 printk(BIOS_DEBUG, " timeout!\n");
587
588                 printk(BIOS_DEBUG, "Restrain PCIe port to x1\n");
589
590                 reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214);
591                 reg32 &= ~(0xf << 1);
592                 reg32 |=1;
593                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x214, reg32);
594
595                 reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), 0x3e);
596
597                 reg16 |= (1 << 6);
598                 pcie_write_config16(PCI_DEV(0, 0x01, 0), 0x3e, reg16);
599                 reg16 &= ~(1 << 6);
600                 pcie_write_config16(PCI_DEV(0, 0x01, 0), 0x3e, reg16);
601
602                 printk(BIOS_DEBUG, "PCIe link training ...");
603                 timeout = 0x7ffff;
604                 while ((((pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214) >> 16) & 4) != 3)  && --timeout) ;
605
606                 reg32 = pci_read_config32(PCI_DEV(0xa, 0x00, 0), 0);
607                 if (reg32 != 0x00000000 && reg32 != 0xffffffff) {
608                         printk(BIOS_DEBUG, " Detected PCIe x1 device %04x:%04x\n",
609                                 reg32 & 0xffff, reg32 >> 16);
610                 } else {
611                         printk(BIOS_DEBUG, " timeout!\n");
612                         printk(BIOS_DEBUG, "Disabling PCIe x16 port completely.\n");
613                         goto disable_pciexpress_x16_link;
614                 }
615         }
616
617         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), 0xb2);
618         reg16 >>= 4;
619         reg16 &= 0x3f;
620         /* reg16 == 1 -> x1; reg16 == 16 -> x16 */
621         printk(BIOS_DEBUG, "PCIe x%d link training succeeded.\n", reg16);
622
623         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x204);
624         reg32 &= 0xfffffc00; /* clear [9:0] */
625         if (reg16 == 1) {
626                 reg32 |= 0x32b;
627                 // TODO
628         } else if (reg16 == 16) {
629                 reg32 |= 0x0f4;
630                 // TODO
631         }
632
633         reg32 = (pci_read_config32(PCI_DEV(0xa, 0, 0), 0x8) >> 8);
634         printk(BIOS_DEBUG, "PCIe device class: %06x\n", reg32);
635         if (reg32 == 0x030000) {
636                 printk(BIOS_DEBUG, "PCIe device is VGA. Disabling IGD.\n");
637                 reg16 = (1 << 1);
638                 pci_write_config16(PCI_DEV(0, 0x0, 0), 0x52, reg16);
639
640                 /* DEVEN */
641                 reg32 = pci_read_config32(PCI_DEV(0, 0x0, 0), 0x54);
642                 reg32 &= ~((1 << 3) | (1 << 4));
643                 pci_write_config32(PCI_DEV(0, 0x0, 0), 0x54, reg32);
644
645                 /* Set VGA enable bit in PCIe bridge */
646                 reg16 = pci_read_config16(PCI_DEV(0, 0x1, 0), 0x3e);
647                 reg16 |= (1 << 3);
648                 pci_write_config16(PCI_DEV(0, 0x1, 0), 0x3e, reg16);
649         }
650
651         /* Enable GPEs */
652         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xec);
653         reg32 |= (1 << 2) | (1 << 1) | (1 << 0); /* PMEGPE, HPGPE, GENGPE */
654         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x114, reg32);
655
656         /* Virtual Channel Configuration: Only VC0 on PCIe x16 */
657         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x114);
658         reg32 &= 0xffffff01;
659         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x114, reg32);
660
661         /* Extended VC count */
662         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x104);
663         reg32 &= ~(7 << 0);
664         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x104, reg32);
665
666         /* Active State Power Management ASPM */
667
668         /* TODO */
669
670         /* Clear error bits */
671         pcie_write_config16(PCI_DEV(0, 0x01, 0), 0x06, 0xffff);
672         pcie_write_config16(PCI_DEV(0, 0x01, 0), 0x1e, 0xffff);
673         pcie_write_config16(PCI_DEV(0, 0x01, 0), 0xaa, 0xffff);
674         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x1c4, 0xffffffff);
675         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x1d0, 0xffffffff);
676         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x1f0, 0xffffffff);
677         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x228, 0xffffffff);
678
679         /* Program R/WO registers */
680         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x308);
681         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x308, reg32);
682
683         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x314);
684         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x314, reg32);
685
686         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x324);
687         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x324, reg32);
688
689         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x328);
690         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x328, reg32);
691
692         reg8 = pcie_read_config8(PCI_DEV(0, 0x01, 0), 0xb4);
693         pcie_write_config8(PCI_DEV(0, 0x01, 0), 0xb4, reg8);
694
695         /* Additional PCIe graphics setup */
696         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xf0);
697         reg32 |= (3 << 26);
698         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xf0, reg32);
699
700         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xf0);
701         reg32 |= (3 << 24);
702         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xf0, reg32);
703
704         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xf0);
705         reg32 |= (1 << 5);
706         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xf0, reg32);
707
708         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x200);
709         reg32 &= ~(3 << 26);
710         reg32 |= (2 << 26);
711         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x200, reg32);
712
713         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xe80);
714         if (i945_silicon_revision() >= 2) {
715                 reg32 |= (1 << 12);
716         } else {
717                 reg32 &= ~(1 << 12);
718         }
719         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xe80, reg32);
720
721         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xeb4);
722         reg32 &= ~(1 << 31);
723         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xeb4, reg32);
724
725         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xfc);
726         reg32 |= (1 << 31);
727         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xfc, reg32);
728
729         if (i945_silicon_revision() >= 3) {
730                 static const u32 reglist[] = {
731                         0xec0, 0xed4, 0xee8, 0xefc, 0xf10, 0xf24,
732                         0xf38, 0xf4c, 0xf60, 0xf74, 0xf88, 0xf9c,
733                         0xfb0, 0xfc4, 0xfd8, 0xfec
734                 };
735
736                 int i;
737                 for (i=0; i<ARRAY_SIZE(reglist); i++) {
738                         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), reglist[i]);
739                         reg32 &= 0x0fffffff;
740                         reg32 |= (2 << 28);
741                         pcie_write_config32(PCI_DEV(0, 0x01, 0), reglist[i], reg32);
742                 }
743         }
744
745         if (i945_silicon_revision() <= 2 ) {
746                 /* Set voltage specific parameters */
747                 reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xe80);
748                 reg32 &= (0xf << 4);
749                 if ((MCHBAR32(0xe08) & (1 << 20)) == 0) {
750                         reg32 |= (7 << 4);
751                 }
752                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xe80, reg32);
753         }
754
755         return;
756
757 disable_pciexpress_x16_link:
758         /* For now we just disable the x16 link */
759         printk(BIOS_DEBUG, "Disabling PCI Express x16 Link\n");
760
761         MCHBAR16(UPMC1) |= (1 << 5) | (1 << 0);
762
763         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), BCTRL1);
764         reg16 |= (1 << 6);
765         pcie_write_config16(PCI_DEV(0, 0x01, 0), BCTRL1, reg16);
766
767         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x224);
768         reg32 |= (1 << 8);
769         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x224, reg32);
770
771         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), BCTRL1);
772         reg16 &= ~(1 << 6);
773         pcie_write_config16(PCI_DEV(0, 0x01, 0), BCTRL1, reg16);
774
775         printk(BIOS_DEBUG, "Wait for link to enter detect state... ");
776         timeout = 0x7fffff;
777         for (reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214);
778              (reg32 & 0x000f0000) && --timeout;) ;
779         if (!timeout)
780                 printk(BIOS_DEBUG, "timeout!\n");
781         else
782                 printk(BIOS_DEBUG, "ok\n");
783
784         /* Finally: Disable the PCI config header */
785         reg16 = pci_read_config16(PCI_DEV(0, 0x00, 0), DEVEN);
786         reg16 &= ~DEVEN_D1F0;
787         pci_write_config16(PCI_DEV(0, 0x00, 0), DEVEN, reg16);
788 }
789
790 static void i945_setup_root_complex_topology(void)
791 {
792         u32 reg32;
793
794         printk(BIOS_DEBUG, "Setting up Root Complex Topology\n");
795         /* Egress Port Root Topology */
796
797         reg32 = EPBAR32(EPESD);
798         reg32 &= 0xff00ffff;
799         reg32 |= (1 << 16);
800         EPBAR32(EPESD) = reg32;
801
802         EPBAR32(EPLE1D) |= (1 << 16) | (1 << 0);
803
804         EPBAR32(EPLE1A) = DEFAULT_DMIBAR;
805
806         EPBAR32(EPLE2D) |= (1 << 16) | (1 << 0);
807
808         /* DMI Port Root Topology */
809
810         reg32 = DMIBAR32(DMILE1D);
811         reg32 &= 0x00ffffff;
812
813         reg32 &= 0xff00ffff;
814         reg32 |= (2 << 16);
815
816         reg32 |= (1 << 0);
817         DMIBAR32(DMILE1D) = reg32;
818
819         DMIBAR32(DMILE1A) = DEFAULT_RCBA;
820
821         DMIBAR32(DMILE2D) |= (1 << 16) | (1 << 0);
822
823         DMIBAR32(DMILE2A) = DEFAULT_EPBAR;
824
825         /* PCI Express x16 Port Root Topology */
826         if (pci_read_config8(PCI_DEV(0, 0x00, 0), DEVEN) & DEVEN_D1F0) {
827                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x158, DEFAULT_EPBAR);
828                 reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x150);
829                 reg32 |= (1 << 0);
830                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x150, reg32);
831         }
832 }
833
834 static void ich7_setup_root_complex_topology(void)
835 {
836         RCBA32(0x104) = 0x00000802;
837         RCBA32(0x110) = 0x00000001;
838         RCBA32(0x114) = 0x00000000;
839         RCBA32(0x118) = 0x00000000;
840 }
841
842 static void ich7_setup_pci_express(void)
843 {
844         RCBA32(CG) |= (1 << 0);
845
846         pci_write_config32(PCI_DEV(0, 0x1c, 0), 0x54, 0x00000060);
847
848         pci_write_config32(PCI_DEV(0, 0x1c, 0), 0xd8, 0x00110000);
849 }
850
851 static void i945_early_initialization(void)
852 {
853         /* Print some chipset specific information */
854         switch (pci_read_config32(PCI_DEV(0, 0x00, 0), 0)) {
855         case 0x27708086:
856                 i945_detect_chipset();
857                 break;
858         case 0x27a08086:
859         case 0x27ac8086:
860                 i945m_detect_chipset();
861                 break;
862         }
863
864         /* Setup all BARs required for early PCIe and raminit */
865         i945_setup_bars();
866
867         /* Change port80 to LPC */
868         RCBA32(GCS) &= (~0x04);
869
870         /* Just do it that way */
871         RCBA32(0x2010) |= (1 << 10);
872 }
873
874 static void i945_late_initialization(void)
875 {
876         i945_setup_egress_port();
877
878         ich7_setup_root_complex_topology();
879
880         ich7_setup_pci_express();
881
882         ich7_setup_dmi_rcrb();
883
884         i945_setup_dmi_rcrb();
885
886         i945_setup_pci_express_x16();
887
888         i945_setup_root_complex_topology();
889 }
890