Refactor copy_and_run so that it uses a single code base instead of
[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 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         reg32 = EPBAR32(EPPVCCAP1);
166         reg32 &= ~(7 << 0);
167         reg32 |= 1;
168         EPBAR32(EPPVCCAP1) = reg32;
169
170         /* Egress Port Virtual Channel 1 Configuration */
171         reg32 = EPBAR32(0x2c);
172         reg32 &= 0xffffff00;
173         if ((MCHBAR32(CLKCFG) & 7) == 1)
174                 reg32 |= 0x0d;  /* 533MHz */
175         if ((MCHBAR32(CLKCFG) & 7) == 3)
176                 reg32 |= 0x10;  /* 667MHz */
177         EPBAR32(0x2c) = reg32;
178
179         EPBAR32(EPVC1MTS) = 0x0a0a0a0a;
180
181         reg32 = EPBAR32(EPVC1RCAP);
182         reg32 &= ~(0x7f << 16);
183         reg32 |= (0x0a << 16);
184         EPBAR32(EPVC1RCAP) = reg32;
185
186         if ((MCHBAR32(CLKCFG) & 7) == 1) {      /* 533MHz */
187                 EPBAR32(EPVC1IST + 0) = 0x009c009c;
188                 EPBAR32(EPVC1IST + 4) = 0x009c009c;
189         }
190
191         if ((MCHBAR32(CLKCFG) & 7) == 3) {      /* 667MHz */
192                 EPBAR32(EPVC1IST + 0) = 0x00c000c0;
193                 EPBAR32(EPVC1IST + 4) = 0x00c000c0;
194         }
195
196         /* Is internal graphics enabled? */
197         if (pci_read_config8(PCI_DEV(0, 0x0, 0), 0x54) & ((1 << 4) | (1 << 3))) {       /* DEVEN */
198                 MCHBAR32(MMARB1) |= (1 << 17);
199         }
200
201         /* Assign Virtual Channel ID 1 to VC1 */
202         reg32 = EPBAR32(EPVC1RCTL);
203         reg32 &= ~(7 << 24);
204         reg32 |= (1 << 24);
205         EPBAR32(EPVC1RCTL) = reg32;
206
207         reg32 = EPBAR32(EPVC1RCTL);
208         reg32 &= 0xffffff01;
209         reg32 |= (1 << 7);
210         EPBAR32(EPVC1RCTL) = reg32;
211
212         EPBAR32(PORTARB + 0x00) = 0x01000001;
213         EPBAR32(PORTARB + 0x04) = 0x00040000;
214         EPBAR32(PORTARB + 0x08) = 0x00001000;
215         EPBAR32(PORTARB + 0x0c) = 0x00000040;
216         EPBAR32(PORTARB + 0x10) = 0x01000001;
217         EPBAR32(PORTARB + 0x14) = 0x00040000;
218         EPBAR32(PORTARB + 0x18) = 0x00001000;
219         EPBAR32(PORTARB + 0x1c) = 0x00000040;
220
221         EPBAR32(EPVC1RCTL) |= (1 << 16);
222         EPBAR32(EPVC1RCTL) |= (1 << 16);
223
224         printk_debug("Loading port arbitration table ...");
225         /* Loop until bit 0 becomes 0 */
226         timeout = 0x7fffff;
227         while ((EPBAR16(EPVC1RSTS) & 1) && --timeout) ;
228         if (!timeout)
229                 printk_debug("timeout!\n");
230         else
231                 printk_debug("ok\n");
232
233         /* Now enable VC1 */
234         EPBAR32(EPVC1RCTL) |= (1 << 31);
235
236         printk_debug("Wait for VC1 negotiation ...");
237         /* Wait for VC1 negotiation pending */
238         timeout = 0x7fff;
239         while ((EPBAR16(EPVC1RSTS) & (1 << 1)) && --timeout) ;
240         if (!timeout)
241                 printk_debug("timeout!\n");
242         else
243                 printk_debug("ok\n");
244
245 }
246
247 static void ich7_setup_dmi_rcrb(void)
248 {
249         u16 reg16;
250         u32 reg32;
251
252         reg16 = RCBA16(LCTL);
253         reg16 &= ~(3 << 0);
254         reg16 |= 3;
255         RCBA16(LCTL) = reg16;
256
257         RCBA32(V0CTL) = 0x80000001;
258         RCBA32(V1CAP) = 0x03128010;
259         RCBA32(ESD) = 0x00000810;
260         RCBA32(RP1D) = 0x01000003;
261         RCBA32(RP2D) = 0x02000002;
262         RCBA32(RP3D) = 0x03000002;
263         RCBA32(RP4D) = 0x04000002;
264         RCBA32(HDD) = 0x0f000003;
265         RCBA32(RP5D) = 0x05000002;
266
267         RCBA32(RPFN) = 0x00543210;
268
269         pci_write_config16(PCI_DEV(0, 0x1c, 0), 0x42, 0x0141);
270         pci_write_config16(PCI_DEV(0, 0x1c, 4), 0x42, 0x0141);
271         pci_write_config16(PCI_DEV(0, 0x1c, 5), 0x42, 0x0141);
272
273         pci_write_config32(PCI_DEV(0, 0x1c, 4), 0x54, 0x00480ce0);
274         pci_write_config32(PCI_DEV(0, 0x1c, 5), 0x54, 0x00500ce0);
275
276         reg32 = RCBA32(V1CTL);
277         reg32 &= ~( (0x7f << 1) | (7 << 17) | (7 << 24) );
278         reg32 |= (0x40 << 1) | (4 << 17) | (1 << 24) | (1 << 31);
279         RCBA32(V1CTL) = reg32;
280
281         RCBA32(ESD) |= (2 << 16);
282
283         RCBA32(ULD) |= (1 << 24) | (1 << 16);
284
285         RCBA32(ULBA) = DEFAULT_DMIBAR;
286
287         RCBA32(RP1D) |= (2 << 16);
288         RCBA32(RP2D) |= (2 << 16);
289         RCBA32(RP3D) |= (2 << 16);
290         RCBA32(RP4D) |= (2 << 16);
291         RCBA32(HDD)  |= (2 << 16);
292         RCBA32(RP5D) |= (2 << 16);
293         RCBA32(RP6D) |= (2 << 16);
294
295         RCBA32(LCAP) |= (3 << 10);
296 }
297
298 static void i945_setup_dmi_rcrb(void)
299 {
300         u32 reg32;
301         u32 timeout;
302
303         int activate_aspm = 1;
304
305         printk_debug("Setting up DMI RCRB\n");
306
307         /* Virtual Channel 0 Configuration */
308         reg32 = DMIBAR32(DMIVC0RCTL0);
309         reg32 &= 0xffffff01;
310         DMIBAR32(DMIVC0RCTL0) = reg32;
311
312         reg32 = DMIBAR32(DMIPVCCAP1);
313         reg32 &= ~(7 << 0);
314         reg32 |= 1;
315         DMIBAR32(DMIPVCCAP1) = reg32;
316
317         reg32 = DMIBAR32(DMIVC1RCTL);
318         reg32 &= ~(7 << 24);
319         reg32 |= (1 << 24);     /* NOTE: This ID must match ICH7 side */
320         DMIBAR32(DMIVC1RCTL) = reg32;
321
322         reg32 = DMIBAR32(DMIVC1RCTL);
323         reg32 &= 0xffffff01;
324         reg32 |= (1 << 7);
325         DMIBAR32(DMIVC1RCTL) = reg32;
326
327         /* Now enable VC1 */
328         DMIBAR32(DMIVC1RCTL) |= (1 << 31);
329
330         printk_debug("Wait for VC1 negotiation ...");
331         /* Wait for VC1 negotiation pending */
332         timeout = 0x7ffff;
333         while ((DMIBAR16(DMIVC1RSTS) & (1 << 1)) && --timeout) ;
334         if (!timeout)
335                 printk_debug("timeout!\n");
336         else
337                 printk_debug("done..\n");
338 #if 1
339         /* Enable Active State Power Management (ASPM) L0 state */
340
341         reg32 = DMIBAR32(DMILCAP);
342         reg32 &= ~(7 << 12);
343         reg32 |= (2 << 12);
344
345         reg32 &= ~(7 << 15);
346         reg32 |= (2 << 15);
347         DMIBAR32(DMILCAP) = reg32;
348
349         reg32 = DMIBAR32(DMICC);
350         reg32 &= 0x00ffffff;
351         reg32 &= ~(3 << 0);
352         reg32 |= (1 << 0);
353
354         reg32 &= ~(3 << 20);
355         reg32 |= (1 << 20);
356
357         DMIBAR32(DMICC) = reg32;
358
359         if (activate_aspm) {
360                 DMIBAR32(DMILCTL) |= (3 << 0);
361         }
362 #endif
363
364         /* Last but not least, some additional steps */
365         reg32 = MCHBAR32(FSBSNPCTL);
366         reg32 &= ~(0xff << 2);
367         reg32 |= (0xaa << 2);
368         MCHBAR32(FSBSNPCTL) = reg32;
369
370         DMIBAR32(0x2c) = 0x86000040;
371
372         reg32 = DMIBAR32(0x204);
373         reg32 &= ~0x3ff;
374 #if 1
375         reg32 |= 0x13f;         /* for x4 DMI only */
376 #else
377         reg32 |= 0x1e4; /* for x2 DMI only */
378 #endif
379         DMIBAR32(0x204) = reg32;
380
381         if (pci_read_config8(PCI_DEV(0, 0x0, 0), 0x54) & ((1 << 4) | (1 << 3))) {       /* DEVEN */
382                 DMIBAR32(0x200) |= (1 << 21);
383         } else {
384                 DMIBAR32(0x200) &= ~(1 << 21);
385         }
386
387         reg32 = DMIBAR32(0x204);
388         reg32 &= ~((1 << 11) | (1 << 10));
389         DMIBAR32(0x204) = reg32;
390
391         reg32 = DMIBAR32(0x204);
392         reg32 &= ~(0xff << 12);
393         reg32 |= (0x0d << 12);
394         DMIBAR32(0x204) = reg32;
395
396         DMIBAR32(DMICTL1) |= (3 << 24);
397
398         reg32 = DMIBAR32(0x200);
399         reg32 &= ~(0x3 << 26);
400         reg32 |= (0x02 << 26);
401         DMIBAR32(0x200) = reg32;
402
403         DMIBAR32(DMIDRCCFG) &= ~(1 << 31);
404         DMIBAR32(DMICTL2) |= (1 << 31);
405
406         if (i945_silicon_revision() >= 3) {
407                 reg32 = DMIBAR32(0xec0);
408                 reg32 &= 0x0fffffff;
409                 reg32 |= (2 << 28);
410                 DMIBAR32(0xec0) = reg32;
411
412                 reg32 = DMIBAR32(0xed4);
413                 reg32 &= 0x0fffffff;
414                 reg32 |= (2 << 28);
415                 DMIBAR32(0xed4) = reg32;
416
417                 reg32 = DMIBAR32(0xee8);
418                 reg32 &= 0x0fffffff;
419                 reg32 |= (2 << 28);
420                 DMIBAR32(0xee8) = reg32;
421
422                 reg32 = DMIBAR32(0xefc);
423                 reg32 &= 0x0fffffff;
424                 reg32 |= (2 << 28);
425                 DMIBAR32(0xefc) = reg32;
426         }
427
428         /* wait for bit toggle to 0 */
429         printk_debug("Waiting for DMI hardware...");
430         timeout = 0x7fffff;
431         while ((DMIBAR8(0x32) & (1 << 1)) && --timeout) ;
432         if (!timeout)
433                 printk_debug("timeout!\n");
434         else
435                 printk_debug("ok\n");
436
437         DMIBAR32(0x1c4) = 0xffffffff;
438         DMIBAR32(0x1d0) = 0xffffffff;
439         DMIBAR32(0x228) = 0xffffffff;
440
441         DMIBAR32(0x308) = DMIBAR32(0x308);
442         DMIBAR32(0x314) = DMIBAR32(0x314);
443         DMIBAR32(0x324) = DMIBAR32(0x324);
444         DMIBAR32(0x328) = DMIBAR32(0x328);
445         DMIBAR32(0x338) = DMIBAR32(0x334);
446         DMIBAR32(0x338) = DMIBAR32(0x338);
447
448         if (i945_silicon_revision() == 1 && ((MCHBAR8(0xe08) & (1 << 5)) == 1)) {
449                 if ((MCHBAR32(0x214) & 0xf) != 0x3) {
450                         printk_info
451                             ("DMI link requires A1 stepping workaround. Rebooting.\n");
452                         reg32 = MCHBAR32(MMARB1);
453                         reg32 &= 0xfffffff8;
454                         reg32 |= 3;
455                         outb(0x06, 0xcf9);
456                         for (;;) ;      /* wait for reset */
457                 }
458         }
459 }
460
461 static void i945_setup_pci_express_x16(void)
462 {
463         u32 timeout;
464         u32 reg32;
465         u16 reg16;
466 #if SETUP_PCIE_X16_LINK
467         u8 reg8;
468
469         printk_debug("Enabling PCI Express x16 Link\n");
470
471         reg16 = pci_read_config16(PCI_DEV(0, 0x00, 0), DEVEN);
472         reg16 |= DEVEN_D1F0;
473         pci_write_config16(PCI_DEV(0, 0x00, 0), DEVEN, reg16);
474
475         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x208);
476         reg32 &= ~(1 << 8);
477         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x208, reg32);
478
479         MCHBAR16(UPMC1) &= ~( (1 << 5) | (1 << 0) );
480
481         /* Initialze PEG_CAP */
482         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), 0xa2);
483         reg16 |= (1 << 8);
484         pcie_write_config16(PCI_DEV(0, 0x01, 0), 0xa2, reg16);
485
486         /* Setup SLOTCAP */
487         /* TODO: These values are mainboard dependent and should
488          * be set from Config.lb or Options.lb.
489          */
490         /* NOTE: SLOTCAP becomes RO after the first write! */
491         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xb4);
492         reg32 &= 0x0007ffff; // TODO
493         reg32 &= 0xfffe007f; // TODO
494         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xb4, reg32);
495
496         /* Wait for training to succeed */
497         printk_debug("Wait for PCIe x16 link training ...");
498         timeout = 0x7fffff;
499         while ((((pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214) >> 16) & 4) != 3)  && --timeout) ;
500         if (!timeout) {
501                 printk_debug("timeout!\n");
502
503                 printk_debug("Restrain PCIe port to x1\n");
504
505                 reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214);
506                 reg32 &= ~(0xf << 1);
507                 reg32 |=1;
508                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x214, reg32);
509
510                 reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), 0x3e);
511
512                 reg16 |= (1 << 6);
513                 pcie_write_config16(PCI_DEV(0, 0x01, 0), 0x3e, reg16);
514                 reg16 &= ~(1 << 6);
515                 pcie_write_config16(PCI_DEV(0, 0x01, 0), 0x3e, reg16);
516
517                 printk_debug("Wait for PCIe x1 link training ...");
518                 timeout = 0x7fffff;
519                 while ((((pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214) >> 16) & 4) != 3)  && --timeout) ;
520                 if (!timeout) {
521                         printk_debug("timeout!\n");
522                         printk_debug("Disabling PCIe x16 port completely.\n");
523                         goto disable_pciexpress_x16_link;
524                 } else {
525                         printk_debug("ok\n");
526                 }
527         } else {
528                 printk_debug("ok\n");
529         }
530
531         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), 0xb2);
532         reg16 >>= 4;
533         reg16 &= 0x3f;
534
535         printk_debug("PCIe x%d link training succeeded.\n", reg16);
536
537         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x204);
538         reg32 &= 0xfffffc00;
539         if (reg16 == 1) {
540                 reg32 |= 0x32b;
541                 // TODO
542         } else if (reg16 == 16) {
543                 reg32 |= 0x0f4;
544                 // TODO
545         }
546
547         /* Enable GPEs */
548         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xec);
549         reg32 |= (1 << 2) | (1 << 1) | (1 << 0); /* PMEGPE, HPGPE, GENGPE */
550         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x114, reg32);
551
552         /* Virtual Channel Configuration: Only VC0 on PCIe x16 */
553         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x114);
554         reg32 &= 0xffffff01;
555         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x114, reg32);
556
557         /* Extended VC count */
558         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x104);
559         reg32 &= ~(7 << 0);
560         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x104, reg32);
561
562         /* Active State Power Management ASPM */
563
564         /* TODO */
565
566         /* Clear error bits */
567         pcie_write_config16(PCI_DEV(0, 0x01, 0), 0x06, 0xffff);
568         pcie_write_config16(PCI_DEV(0, 0x01, 0), 0x1e, 0xffff);
569         pcie_write_config16(PCI_DEV(0, 0x01, 0), 0xaa, 0xffff);
570         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x1c4, 0xffffffff);
571         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x1d0, 0xffffffff);
572         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x1f0, 0xffffffff);
573         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x228, 0xffffffff);
574
575         /* Program R/WO registers */
576         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x308);
577         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x308, reg32);
578
579         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x314);
580         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x314, reg32);
581
582         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x324);
583         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x324, reg32);
584
585         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x328);
586         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x328, reg32);
587
588         reg8 = pcie_read_config8(PCI_DEV(0, 0x01, 0), 0xb4);
589         pcie_write_config8(PCI_DEV(0, 0x01, 0), 0xb4, reg8);
590
591         /* Additional PCIe graphics setup */
592         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xf0);
593         reg32 |= (3 << 26);
594         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xf0, reg32);
595
596         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xf0);
597         reg32 |= (3 << 24);
598         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xf0, reg32);
599
600         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x200);
601         reg32 &= ~(3 << 26);
602         reg32 |= (2 << 26);
603         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x200, reg32);
604
605         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xe80);
606         if (i945_silicon_revision() >= 2) {
607                 reg32 |= (1 << 12);
608         } else {
609                 reg32 &= ~(1 << 12);
610         }
611         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xe80, reg32);
612
613         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xeb4);
614         reg32 &= ~(1 << 31);
615         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xeb4, reg32);
616
617         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xfc);
618         reg32 |= (1 << 31);
619         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xfc, reg32);
620
621         if (i945_silicon_revision() >= 3) {
622                 static const u32 reglist[] = {
623                         0xec0, 0xed4, 0xee8, 0xefc, 0xf10, 0xf24,
624                         0xf38, 0xf4c, 0xf60, 0xf74, 0xf88, 0xf9c,
625                         0xfb0, 0xfc4, 0xfd8, 0xfec
626                 };
627
628                 int i;
629                 for (i=0; i<ARRAY_SIZE(reglist); i++) {
630                         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), reglist[i]);
631                         reg32 &= 0x0fffffff;
632                         reg32 |= (2 << 28);
633                         pcie_write_config32(PCI_DEV(0, 0x01, 0), reglist[i], reg32);
634                 }
635         }
636
637         if (i945_silicon_revision() <= 2 ) {
638                 /* Set voltage specific parameters */
639                 reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0xe80);
640                 reg32 &= (0xf << 4);
641                 if ((MCHBAR32(0xe08) & (1 << 20)) == 0) {
642                         reg32 |= (7 << 4);
643                 }
644                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0xe80, reg32);
645         }
646
647         return;
648
649 disable_pciexpress_x16_link:
650 #endif
651         /* For now we just disable the x16 link */
652         printk_debug("Disabling PCI Express x16 Link\n");
653
654         MCHBAR16(UPMC1) |= (1 << 5) | (1 << 0);
655
656         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), BCTRL1);
657         reg16 |= (1 << 6);
658         pcie_write_config16(PCI_DEV(0, 0x01, 0), BCTRL1, reg16);
659
660         reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x224);
661         reg32 |= (1 << 8);
662         pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x224, reg32);
663
664         reg16 = pcie_read_config16(PCI_DEV(0, 0x01, 0), BCTRL1);
665         reg16 &= ~(1 << 6);
666         pcie_write_config16(PCI_DEV(0, 0x01, 0), BCTRL1, reg16);
667
668         printk_debug("Wait for link to enter detect state... ");
669         timeout = 0x7fffff;
670         for (reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x214);
671              (reg32 & 0x000f0000) && --timeout;) ;
672         if (!timeout)
673                 printk_debug("timeout!\n");
674         else
675                 printk_debug("ok\n");
676
677         /* Finally: Disable the PCI config header */
678         reg16 = pci_read_config16(PCI_DEV(0, 0x00, 0), DEVEN);
679         reg16 &= ~DEVEN_D1F0;
680         pci_write_config16(PCI_DEV(0, 0x00, 0), DEVEN, reg16);
681 }
682
683 static void i945_setup_root_complex_topology(void)
684 {
685         u32 reg32;
686
687         printk_debug("Setting up Root Complex Topology\n");
688         /* Egress Port Root Topology */
689
690         reg32 = EPBAR32(EPESD);
691         reg32 &= 0xff00ffff;
692         reg32 |= (1 << 16);
693         EPBAR32(EPESD) = reg32;
694
695         EPBAR32(EPLE1D) |= (1 << 16);
696
697         EPBAR32(EPLE1D) |= (1 << 0);
698
699         EPBAR32(EPLE1A) = DEFAULT_DMIBAR;
700 #if 0
701         EPBAR32(EPLE2D) |= (1 << 16);
702 #endif
703
704         EPBAR32(EPLE2D) |= (1 << 0);
705
706         /* DMI Port Root Topology */
707         reg32 = DMIBAR32(DMILE1D);
708         reg32 &= 0x00ffffff;
709         DMIBAR32(DMILE1D) = reg32;
710
711         reg32 = DMIBAR32(DMILE1D);
712         reg32 &= 0xff00ffff;
713         reg32 |= (2 << 16);
714         DMIBAR32(DMILE1D) = reg32;
715
716         DMIBAR32(DMILE1D) |= (1 << 0);
717
718         DMIBAR32(DMILE1A) = DEFAULT_RCBA;
719
720         DMIBAR32(DMILE2D) |= (1 << 0);
721
722         DMIBAR32(DMILE2A) = DEFAULT_EPBAR;
723
724         /* PCI Express x16 Port Root Topology */
725         if (pci_read_config8(PCI_DEV(0, 0x00, 0), DEVEN) & DEVEN_D1F0) {
726                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x158, DEFAULT_EPBAR);
727                 reg32 = pcie_read_config32(PCI_DEV(0, 0x01, 0), 0x150);
728                 reg32 |= (1 << 0);
729                 pcie_write_config32(PCI_DEV(0, 0x01, 0), 0x150, reg32);
730         }
731 }
732
733 static void ich7_setup_root_complex_topology(void)
734 {
735         RCBA32(0x104) = 0x00000802;
736         RCBA32(0x110) = 0x00000001;
737         RCBA32(0x114) = 0x00000000;
738         RCBA32(0x118) = 0x00000000;
739 }
740
741 static void ich7_setup_pci_express(void)
742 {
743         RCBA32(CG) |= (1 << 0);
744
745         pci_write_config32(PCI_DEV(0, 0x1c, 0), 0x54, 0x00000060);
746
747         pci_write_config32(PCI_DEV(0, 0x1c, 0), 0xd8, 0x00110000);
748 }
749
750 static void i945_early_initialization(void)
751 {
752         /* Print some chipset specific information */
753         i945_detect_chipset();
754
755         /* Setup all BARs required for early PCIe and raminit */
756         i945_setup_bars();
757
758         /* Change port80 to LPC */
759         RCBA32(GCS) &= (~0x04);
760
761         /* Just do it that way */
762         RCBA32(0x2010) |= (1 << 10);
763 }
764
765 static void i945_late_initialization(void)
766 {
767         i945_setup_egress_port();
768
769         ich7_setup_root_complex_topology();
770
771         ich7_setup_pci_express();
772
773         ich7_setup_dmi_rcrb();
774
775         i945_setup_dmi_rcrb();
776
777         i945_setup_pci_express_x16();
778
779         i945_setup_root_complex_topology();
780 }