blob: 10ac071f52034aa36f55e6d66ef64119e373007b [file] [log] [blame]
Stefan Reinauer00636b02012-04-04 00:08:51 +02001/*
2 * This file is part of the coreboot project.
3 *
Stefan Reinauer00636b02012-04-04 00:08:51 +02004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 2 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Stefan Reinauer00636b02012-04-04 00:08:51 +020013 */
14
Stefan Reinauer00636b02012-04-04 00:08:51 +020015#include <console/console.h>
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010016#include <device/mmio.h>
17#include <device/device.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020018#include <device/pci_ops.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +020019#include <device/pci_def.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +020020#include <option.h>
Kyösti Mälkkie39a8a92016-06-25 11:40:00 +030021#include <romstage_handoff.h>
Elyes HAOUAS51401c32019-05-15 21:09:30 +020022#include <types.h>
23
Stefan Reinauer00636b02012-04-04 00:08:51 +020024#include "sandybridge.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020025
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010026static void systemagent_vtd_init(void)
27{
28 const u32 capid0_a = pci_read_config32(PCI_DEV(0, 0, 0), CAPID0_A);
29 if (capid0_a & (1 << 23))
30 return;
31
32 /* setup BARs */
Angel Pons88521882020-01-05 20:21:20 +010033 MCHBAR32(VTD1_BASE + 4) = IOMMU_BASE1 >> 32;
34 MCHBAR32(VTD1_BASE) = IOMMU_BASE1 | 1;
35 MCHBAR32(VTD2_BASE + 4) = IOMMU_BASE2 >> 32;
36 MCHBAR32(VTD2_BASE) = IOMMU_BASE2 | 1;
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010037
38 /* lock policies */
39 write32((void *)(IOMMU_BASE1 + 0xff0), 0x80000000);
40
41 const struct device *const azalia = pcidev_on_root(0x1b, 0);
42 if (azalia && azalia->enabled) {
43 write32((void *)(IOMMU_BASE2 + 0xff0), 0x20000000);
44 write32((void *)(IOMMU_BASE2 + 0xff0), 0xa0000000);
45 } else {
46 write32((void *)(IOMMU_BASE2 + 0xff0), 0x80000000);
47 }
48}
49
50static void enable_pam_region(void)
51{
52 pci_write_config8(PCI_DEV(0, 0x00, 0), PAM0, 0x30);
53 pci_write_config8(PCI_DEV(0, 0x00, 0), PAM1, 0x33);
54 pci_write_config8(PCI_DEV(0, 0x00, 0), PAM2, 0x33);
55 pci_write_config8(PCI_DEV(0, 0x00, 0), PAM3, 0x33);
56 pci_write_config8(PCI_DEV(0, 0x00, 0), PAM4, 0x33);
57 pci_write_config8(PCI_DEV(0, 0x00, 0), PAM5, 0x33);
58 pci_write_config8(PCI_DEV(0, 0x00, 0), PAM6, 0x33);
59}
60
Stefan Reinauer00636b02012-04-04 00:08:51 +020061static void sandybridge_setup_bars(void)
62{
Stefan Reinauer00636b02012-04-04 00:08:51 +020063 printk(BIOS_DEBUG, "Setting up static northbridge registers...");
64 /* Set up all hardcoded northbridge BARs */
65 pci_write_config32(PCI_DEV(0, 0x00, 0), EPBAR, DEFAULT_EPBAR | 1);
66 pci_write_config32(PCI_DEV(0, 0x00, 0), EPBAR + 4, (0LL+DEFAULT_EPBAR) >> 32);
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080067 pci_write_config32(PCI_DEV(0, 0x00, 0), MCHBAR, (uintptr_t)DEFAULT_MCHBAR | 1);
68 pci_write_config32(PCI_DEV(0, 0x00, 0), MCHBAR + 4, (0LL+(uintptr_t)DEFAULT_MCHBAR) >> 32);
69 pci_write_config32(PCI_DEV(0, 0x00, 0), DMIBAR, (uintptr_t)DEFAULT_DMIBAR | 1);
70 pci_write_config32(PCI_DEV(0, 0x00, 0), DMIBAR + 4, (0LL+(uintptr_t)DEFAULT_DMIBAR) >> 32);
Stefan Reinauer00636b02012-04-04 00:08:51 +020071
Patrick Rudolph90050712019-03-25 09:53:23 +010072 printk(BIOS_DEBUG, " done\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +020073}
74
75static void sandybridge_setup_graphics(void)
76{
77 u32 reg32;
78 u16 reg16;
79 u8 reg8;
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +020080 u8 gfxsize;
Stefan Reinauer00636b02012-04-04 00:08:51 +020081
82 reg16 = pci_read_config16(PCI_DEV(0,2,0), PCI_DEVICE_ID);
83 switch (reg16) {
84 case 0x0102: /* GT1 Desktop */
85 case 0x0106: /* GT1 Mobile */
86 case 0x010a: /* GT1 Server */
87 case 0x0112: /* GT2 Desktop */
88 case 0x0116: /* GT2 Mobile */
89 case 0x0122: /* GT2 Desktop >=1.3GHz */
90 case 0x0126: /* GT2 Mobile >=1.3GHz */
Patrick Rudolph03a88d32015-07-05 13:29:41 +020091 case 0x0152: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080092 case 0x0156: /* IvyBridge */
Damien Zammita10bde92014-10-23 13:29:32 +110093 case 0x0162: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080094 case 0x0166: /* IvyBridge */
Vagiz Trakhanov1dd448c2017-09-28 14:42:11 +000095 case 0x016a: /* IvyBridge */
Stefan Reinauer00636b02012-04-04 00:08:51 +020096 break;
97 default:
98 printk(BIOS_DEBUG, "Graphics not supported by this CPU/chipset.\n");
99 return;
100 }
101
102 printk(BIOS_DEBUG, "Initializing Graphics...\n");
103
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +0200104 if (get_option(&gfxsize, "gfx_uma_size") != CB_SUCCESS) {
105 /* Setup IGD memory by setting GGC[7:3] = 1 for 32MB */
106 gfxsize = 0;
107 }
Stefan Reinauer00636b02012-04-04 00:08:51 +0200108 reg16 = pci_read_config16(PCI_DEV(0,0,0), GGC);
109 reg16 &= ~0x00f8;
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +0200110 reg16 |= (gfxsize + 1) << 3;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200111 /* Program GTT memory by setting GGC[9:8] = 2MB */
112 reg16 &= ~0x0300;
113 reg16 |= 2 << 8;
114 /* Enable VGA decode */
115 reg16 &= ~0x0002;
116 pci_write_config16(PCI_DEV(0,0,0), GGC, reg16);
117
118 /* Enable 256MB aperture */
119 reg8 = pci_read_config8(PCI_DEV(0, 2, 0), MSAC);
120 reg8 &= ~0x06;
121 reg8 |= 0x02;
122 pci_write_config8(PCI_DEV(0, 2, 0), MSAC, reg8);
123
124 /* Erratum workarounds */
Angel Pons88521882020-01-05 20:21:20 +0100125 reg32 = MCHBAR32(SAPMCTL);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200126 reg32 |= (1 << 9)|(1 << 10);
Angel Pons88521882020-01-05 20:21:20 +0100127 MCHBAR32(SAPMCTL) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200128
129 /* Enable SA Clock Gating */
Angel Pons88521882020-01-05 20:21:20 +0100130 reg32 = MCHBAR32(SAPMCTL);
131 MCHBAR32(SAPMCTL) = reg32 | 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200132
133 /* GPU RC6 workaround for sighting 366252 */
134 reg32 = MCHBAR32(0x5d14);
135 reg32 |= (1 << 31);
136 MCHBAR32(0x5d14) = reg32;
137
138 /* VLW */
139 reg32 = MCHBAR32(0x6120);
140 reg32 &= ~(1 << 0);
141 MCHBAR32(0x6120) = reg32;
142
Angel Pons88521882020-01-05 20:21:20 +0100143 reg32 = MCHBAR32(PAIR_CTL);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200144 reg32 |= (1 << 4) | (1 << 5);
Angel Pons88521882020-01-05 20:21:20 +0100145 MCHBAR32(PAIR_CTL) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200146}
147
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200148static void start_peg_link_training(void)
149{
150 u32 tmp;
151 u32 deven;
152
153 /* PEG on IvyBridge+ needs a special startup sequence.
154 * As the MRC has its own initialization code skip it. */
155 if (((pci_read_config16(PCI_DEV(0, 0, 0), PCI_DEVICE_ID) &
156 BASE_REV_MASK) != BASE_REV_IVB) ||
Julius Wernercd49cce2019-03-05 16:53:33 -0800157 CONFIG(HAVE_MRC))
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200158 return;
159
160 deven = pci_read_config32(PCI_DEV(0, 0, 0), DEVEN);
161
162 if (deven & DEVEN_PEG10) {
163 tmp = pci_read_config32(PCI_DEV(0, 1, 0), 0xC24) & ~(1 << 16);
164 pci_write_config32(PCI_DEV(0, 1, 0), 0xC24, tmp | (1 << 5));
165 }
166
167 if (deven & DEVEN_PEG11) {
168 tmp = pci_read_config32(PCI_DEV(0, 1, 1), 0xC24) & ~(1 << 16);
169 pci_write_config32(PCI_DEV(0, 1, 1), 0xC24, tmp | (1 << 5));
170 }
171
172 if (deven & DEVEN_PEG12) {
173 tmp = pci_read_config32(PCI_DEV(0, 1, 2), 0xC24) & ~(1 << 16);
174 pci_write_config32(PCI_DEV(0, 1, 2), 0xC24, tmp | (1 << 5));
175 }
176
177 if (deven & DEVEN_PEG60) {
178 tmp = pci_read_config32(PCI_DEV(0, 6, 0), 0xC24) & ~(1 << 16);
179 pci_write_config32(PCI_DEV(0, 6, 0), 0xC24, tmp | (1 << 5));
180 }
181}
182
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100183void systemagent_early_init(void)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200184{
185 u32 capid0_a;
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200186 u32 deven;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200187 u8 reg8;
188
189 /* Device ID Override Enable should be done very early */
190 capid0_a = pci_read_config32(PCI_DEV(0, 0, 0), 0xe4);
191 if (capid0_a & (1 << 10)) {
Patrick Rudolph74203de2017-11-20 11:57:01 +0100192 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
193
Stefan Reinauer00636b02012-04-04 00:08:51 +0200194 reg8 = pci_read_config8(PCI_DEV(0, 0, 0), 0xf3);
195 reg8 &= ~7; /* Clear 2:0 */
196
Patrick Rudolph74203de2017-11-20 11:57:01 +0100197 if (is_mobile)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200198 reg8 |= 1; /* Set bit 0 */
199
200 pci_write_config8(PCI_DEV(0, 0, 0), 0xf3, reg8);
201 }
202
203 /* Setup all BARs required for early PCIe and raminit */
204 sandybridge_setup_bars();
205
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100206 /* Set C0000-FFFFF to access RAM on both reads and writes */
207 enable_pam_region();
208
Nico Huberbb9469c2015-10-21 11:49:23 +0200209 /* Setup IOMMU BARs */
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100210 systemagent_vtd_init();
Nico Huberbb9469c2015-10-21 11:49:23 +0200211
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200212 /* Device Enable, don't touch PEG bits */
213 deven = pci_read_config32(PCI_DEV(0, 0, 0), DEVEN) | DEVEN_IGD;
214 pci_write_config32(PCI_DEV(0, 0, 0), DEVEN, deven);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200215
216 sandybridge_setup_graphics();
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200217
218 /* Write magic value to start PEG link training.
219 * This should be done in PCI device enumeration, but
220 * the PCIe specification requires to wait at least 100msec
221 * after reset for devices to come up.
222 * As we don't want to increase boot time, enable it early and
223 * assume the PEG is up as soon as PCI enumeration starts.
224 * TODO: use time stamps to ensure the timings are met */
225 start_peg_link_training();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200226}
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200227
228void northbridge_romstage_finalize(int s3resume)
229{
230 MCHBAR16(SSKPD) = 0xCAFE;
231
Aaron Durbin77e13992016-11-29 17:43:04 -0600232 romstage_handoff_init(s3resume);
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200233}