blob: 390fadca65382111be4126bd29b7fde83c5bdc2c [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{
Angel Pons7c49cb82020-03-16 23:17:32 +010028 const u32 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010029 if (capid0_a & (1 << 23))
30 return;
31
Angel Pons7c49cb82020-03-16 23:17:32 +010032 /* Setup BARs */
33 MCHBAR32(GFXVTBAR + 4) = GFXVT_BASE >> 32;
34 MCHBAR32(GFXVTBAR) = GFXVT_BASE | 1;
35 MCHBAR32(VTVC0BAR + 4) = VTVC0_BASE >> 32;
36 MCHBAR32(VTVC0BAR) = VTVC0_BASE | 1;
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010037
Angel Pons7c49cb82020-03-16 23:17:32 +010038 /* Lock policies */
39 write32((void *)(GFXVT_BASE + 0xff0), 0x80000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010040
41 const struct device *const azalia = pcidev_on_root(0x1b, 0);
42 if (azalia && azalia->enabled) {
Angel Pons7c49cb82020-03-16 23:17:32 +010043 write32((void *)(VTVC0_BASE + 0xff0), 0x20000000);
44 write32((void *)(VTVC0_BASE + 0xff0), 0xa0000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010045 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +010046 write32((void *)(VTVC0_BASE + 0xff0), 0x80000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010047 }
48}
49
50static void enable_pam_region(void)
51{
Angel Pons7c49cb82020-03-16 23:17:32 +010052 pci_write_config8(HOST_BRIDGE, PAM0, 0x30);
53 pci_write_config8(HOST_BRIDGE, PAM1, 0x33);
54 pci_write_config8(HOST_BRIDGE, PAM2, 0x33);
55 pci_write_config8(HOST_BRIDGE, PAM3, 0x33);
56 pci_write_config8(HOST_BRIDGE, PAM4, 0x33);
57 pci_write_config8(HOST_BRIDGE, PAM5, 0x33);
58 pci_write_config8(HOST_BRIDGE, PAM6, 0x33);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010059}
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 */
Angel Pons7c49cb82020-03-16 23:17:32 +010065 pci_write_config32(HOST_BRIDGE, EPBAR, DEFAULT_EPBAR | 1);
66 pci_write_config32(HOST_BRIDGE, EPBAR + 4, (0LL + DEFAULT_EPBAR) >> 32);
67 pci_write_config32(HOST_BRIDGE, MCHBAR, (uintptr_t)DEFAULT_MCHBAR | 1);
68 pci_write_config32(HOST_BRIDGE, MCHBAR + 4, (0LL + (uintptr_t)DEFAULT_MCHBAR) >> 32);
69 pci_write_config32(HOST_BRIDGE, DMIBAR, (uintptr_t)DEFAULT_DMIBAR | 1);
70 pci_write_config32(HOST_BRIDGE, 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;
Angel Pons7c49cb82020-03-16 23:17:32 +010079 u8 reg8, gfxsize;
Stefan Reinauer00636b02012-04-04 00:08:51 +020080
Angel Pons7c49cb82020-03-16 23:17:32 +010081 reg16 = pci_read_config16(PCI_DEV(0, 2, 0), PCI_DEVICE_ID);
Stefan Reinauer00636b02012-04-04 00:08:51 +020082 switch (reg16) {
83 case 0x0102: /* GT1 Desktop */
84 case 0x0106: /* GT1 Mobile */
85 case 0x010a: /* GT1 Server */
86 case 0x0112: /* GT2 Desktop */
87 case 0x0116: /* GT2 Mobile */
88 case 0x0122: /* GT2 Desktop >=1.3GHz */
89 case 0x0126: /* GT2 Mobile >=1.3GHz */
Patrick Rudolph03a88d32015-07-05 13:29:41 +020090 case 0x0152: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080091 case 0x0156: /* IvyBridge */
Damien Zammita10bde92014-10-23 13:29:32 +110092 case 0x0162: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080093 case 0x0166: /* IvyBridge */
Vagiz Trakhanov1dd448c2017-09-28 14:42:11 +000094 case 0x016a: /* IvyBridge */
Stefan Reinauer00636b02012-04-04 00:08:51 +020095 break;
96 default:
97 printk(BIOS_DEBUG, "Graphics not supported by this CPU/chipset.\n");
98 return;
99 }
100
101 printk(BIOS_DEBUG, "Initializing Graphics...\n");
102
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +0200103 if (get_option(&gfxsize, "gfx_uma_size") != CB_SUCCESS) {
104 /* Setup IGD memory by setting GGC[7:3] = 1 for 32MB */
105 gfxsize = 0;
106 }
Angel Pons7c49cb82020-03-16 23:17:32 +0100107 reg16 = pci_read_config16(HOST_BRIDGE, GGC);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200108 reg16 &= ~0x00f8;
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +0200109 reg16 |= (gfxsize + 1) << 3;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200110 /* Program GTT memory by setting GGC[9:8] = 2MB */
111 reg16 &= ~0x0300;
112 reg16 |= 2 << 8;
113 /* Enable VGA decode */
114 reg16 &= ~0x0002;
Angel Pons7c49cb82020-03-16 23:17:32 +0100115 pci_write_config16(HOST_BRIDGE, GGC, reg16);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200116
117 /* Enable 256MB aperture */
118 reg8 = pci_read_config8(PCI_DEV(0, 2, 0), MSAC);
119 reg8 &= ~0x06;
120 reg8 |= 0x02;
121 pci_write_config8(PCI_DEV(0, 2, 0), MSAC, reg8);
122
123 /* Erratum workarounds */
Angel Pons88521882020-01-05 20:21:20 +0100124 reg32 = MCHBAR32(SAPMCTL);
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 reg32 |= (1 << 9) | (1 << 10);
Angel Pons88521882020-01-05 20:21:20 +0100126 MCHBAR32(SAPMCTL) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200127
128 /* Enable SA Clock Gating */
Angel Pons88521882020-01-05 20:21:20 +0100129 reg32 = MCHBAR32(SAPMCTL);
130 MCHBAR32(SAPMCTL) = reg32 | 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200131
132 /* GPU RC6 workaround for sighting 366252 */
Angel Pons7c49cb82020-03-16 23:17:32 +0100133 reg32 = MCHBAR32(SSKPD_HI);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200134 reg32 |= (1 << 31);
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 MCHBAR32(SSKPD_HI) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200136
Angel Pons7c49cb82020-03-16 23:17:32 +0100137 /* VLW (Virtual Legacy Wire?) */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200138 reg32 = MCHBAR32(0x6120);
139 reg32 &= ~(1 << 0);
140 MCHBAR32(0x6120) = reg32;
141
Angel Pons7c49cb82020-03-16 23:17:32 +0100142 reg32 = MCHBAR32(INTRDIRCTL);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200143 reg32 |= (1 << 4) | (1 << 5);
Angel Pons7c49cb82020-03-16 23:17:32 +0100144 MCHBAR32(INTRDIRCTL) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200145}
146
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200147static void start_peg_link_training(void)
148{
Angel Pons7c49cb82020-03-16 23:17:32 +0100149 u32 tmp, deven;
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200150
Angel Pons7c49cb82020-03-16 23:17:32 +0100151 const u16 base_rev = pci_read_config16(HOST_BRIDGE, PCI_DEVICE_ID) & BASE_REV_MASK;
152 /*
153 * PEG on IvyBridge+ needs a special startup sequence.
154 * As the MRC has its own initialization code skip it.
155 */
156 if ((base_rev != BASE_REV_IVB) || CONFIG(HAVE_MRC))
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200157 return;
158
Angel Pons7c49cb82020-03-16 23:17:32 +0100159 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200160
Angel Pons7c49cb82020-03-16 23:17:32 +0100161 /*
162 * For each PEG device, set bit 5 to use three retries for OC (Offset Calibration).
163 * We also clear DEFER_OC (bit 16) in order to start PEG training.
164 */
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200165 if (deven & DEVEN_PEG10) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100166 tmp = pci_read_config32(PCI_DEV(0, 1, 0), AFE_PWRON) & ~(1 << 16);
167 pci_write_config32(PCI_DEV(0, 1, 0), AFE_PWRON, tmp | (1 << 5));
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200168 }
169
170 if (deven & DEVEN_PEG11) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100171 tmp = pci_read_config32(PCI_DEV(0, 1, 1), AFE_PWRON) & ~(1 << 16);
172 pci_write_config32(PCI_DEV(0, 1, 1), AFE_PWRON, tmp | (1 << 5));
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200173 }
174
175 if (deven & DEVEN_PEG12) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100176 tmp = pci_read_config32(PCI_DEV(0, 1, 2), AFE_PWRON) & ~(1 << 16);
177 pci_write_config32(PCI_DEV(0, 1, 2), AFE_PWRON, tmp | (1 << 5));
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200178 }
179
180 if (deven & DEVEN_PEG60) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100181 tmp = pci_read_config32(PCI_DEV(0, 6, 0), AFE_PWRON) & ~(1 << 16);
182 pci_write_config32(PCI_DEV(0, 6, 0), AFE_PWRON, tmp | (1 << 5));
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200183 }
184}
185
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100186void systemagent_early_init(void)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200187{
188 u32 capid0_a;
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200189 u32 deven;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200190 u8 reg8;
191
192 /* Device ID Override Enable should be done very early */
Angel Pons7c49cb82020-03-16 23:17:32 +0100193 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200194 if (capid0_a & (1 << 10)) {
Patrick Rudolph74203de2017-11-20 11:57:01 +0100195 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
196
Angel Pons7c49cb82020-03-16 23:17:32 +0100197 reg8 = pci_read_config8(HOST_BRIDGE, DIDOR);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200198 reg8 &= ~7; /* Clear 2:0 */
199
Patrick Rudolph74203de2017-11-20 11:57:01 +0100200 if (is_mobile)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200201 reg8 |= 1; /* Set bit 0 */
202
Angel Pons7c49cb82020-03-16 23:17:32 +0100203 pci_write_config8(HOST_BRIDGE, DIDOR, reg8);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200204 }
205
206 /* Setup all BARs required for early PCIe and raminit */
207 sandybridge_setup_bars();
208
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100209 /* Set C0000-FFFFF to access RAM on both reads and writes */
210 enable_pam_region();
211
Nico Huberbb9469c2015-10-21 11:49:23 +0200212 /* Setup IOMMU BARs */
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100213 systemagent_vtd_init();
Nico Huberbb9469c2015-10-21 11:49:23 +0200214
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200215 /* Device Enable, don't touch PEG bits */
Angel Pons7c49cb82020-03-16 23:17:32 +0100216 deven = pci_read_config32(HOST_BRIDGE, DEVEN) | DEVEN_IGD;
217 pci_write_config32(HOST_BRIDGE, DEVEN, deven);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200218
219 sandybridge_setup_graphics();
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200220
Angel Pons7c49cb82020-03-16 23:17:32 +0100221 /*
222 * Write magic values to start PEG link training. This should be done in PCI device
223 * enumeration, but the PCIe specification requires to wait at least 100msec after
224 * reset for devices to come up. As we don't want to increase boot time, enable it
225 * early and assume that PEG is up as soon as PCI enumeration starts.
226 *
227 * TODO: use timestamps to ensure the timings are met.
228 */
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200229 start_peg_link_training();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200230}
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200231
232void northbridge_romstage_finalize(int s3resume)
233{
Angel Pons7c49cb82020-03-16 23:17:32 +0100234 MCHBAR16(SSKPD_HI) = 0xCAFE;
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200235
Aaron Durbin77e13992016-11-29 17:43:04 -0600236 romstage_handoff_init(s3resume);
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200237}