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