blob: d63ba1e311ddae5d4dfe4c700cfa38032cb5b88a [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Stefan Reinauer00636b02012-04-04 00:08:51 +02003
Stefan Reinauer00636b02012-04-04 00:08:51 +02004#include <console/console.h>
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +01005#include <device/mmio.h>
6#include <device/device.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02007#include <device/pci_ops.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +02008#include <device/pci_def.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +02009#include <option.h>
Kyösti Mälkkie39a8a92016-06-25 11:40:00 +030010#include <romstage_handoff.h>
Elyes HAOUAS51401c32019-05-15 21:09:30 +020011#include <types.h>
12
Stefan Reinauer00636b02012-04-04 00:08:51 +020013#include "sandybridge.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020014
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010015static void systemagent_vtd_init(void)
16{
Angel Pons7c49cb82020-03-16 23:17:32 +010017 const u32 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010018 if (capid0_a & (1 << 23))
19 return;
20
Angel Pons7c49cb82020-03-16 23:17:32 +010021 /* Setup BARs */
22 MCHBAR32(GFXVTBAR + 4) = GFXVT_BASE >> 32;
23 MCHBAR32(GFXVTBAR) = GFXVT_BASE | 1;
24 MCHBAR32(VTVC0BAR + 4) = VTVC0_BASE >> 32;
25 MCHBAR32(VTVC0BAR) = VTVC0_BASE | 1;
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010026
Angel Pons7c49cb82020-03-16 23:17:32 +010027 /* Lock policies */
28 write32((void *)(GFXVT_BASE + 0xff0), 0x80000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010029
30 const struct device *const azalia = pcidev_on_root(0x1b, 0);
31 if (azalia && azalia->enabled) {
Angel Pons7c49cb82020-03-16 23:17:32 +010032 write32((void *)(VTVC0_BASE + 0xff0), 0x20000000);
33 write32((void *)(VTVC0_BASE + 0xff0), 0xa0000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010034 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +010035 write32((void *)(VTVC0_BASE + 0xff0), 0x80000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010036 }
37}
38
39static void enable_pam_region(void)
40{
Angel Pons7c49cb82020-03-16 23:17:32 +010041 pci_write_config8(HOST_BRIDGE, PAM0, 0x30);
42 pci_write_config8(HOST_BRIDGE, PAM1, 0x33);
43 pci_write_config8(HOST_BRIDGE, PAM2, 0x33);
44 pci_write_config8(HOST_BRIDGE, PAM3, 0x33);
45 pci_write_config8(HOST_BRIDGE, PAM4, 0x33);
46 pci_write_config8(HOST_BRIDGE, PAM5, 0x33);
47 pci_write_config8(HOST_BRIDGE, PAM6, 0x33);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010048}
49
Stefan Reinauer00636b02012-04-04 00:08:51 +020050static void sandybridge_setup_bars(void)
51{
Stefan Reinauer00636b02012-04-04 00:08:51 +020052 printk(BIOS_DEBUG, "Setting up static northbridge registers...");
53 /* Set up all hardcoded northbridge BARs */
Angel Pons7c49cb82020-03-16 23:17:32 +010054 pci_write_config32(HOST_BRIDGE, EPBAR, DEFAULT_EPBAR | 1);
55 pci_write_config32(HOST_BRIDGE, EPBAR + 4, (0LL + DEFAULT_EPBAR) >> 32);
56 pci_write_config32(HOST_BRIDGE, MCHBAR, (uintptr_t)DEFAULT_MCHBAR | 1);
57 pci_write_config32(HOST_BRIDGE, MCHBAR + 4, (0LL + (uintptr_t)DEFAULT_MCHBAR) >> 32);
58 pci_write_config32(HOST_BRIDGE, DMIBAR, (uintptr_t)DEFAULT_DMIBAR | 1);
59 pci_write_config32(HOST_BRIDGE, DMIBAR + 4, (0LL + (uintptr_t)DEFAULT_DMIBAR) >> 32);
Stefan Reinauer00636b02012-04-04 00:08:51 +020060
Patrick Rudolph90050712019-03-25 09:53:23 +010061 printk(BIOS_DEBUG, " done\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +020062}
63
64static void sandybridge_setup_graphics(void)
65{
66 u32 reg32;
67 u16 reg16;
Angel Pons7c49cb82020-03-16 23:17:32 +010068 u8 reg8, gfxsize;
Stefan Reinauer00636b02012-04-04 00:08:51 +020069
Angel Pons7c49cb82020-03-16 23:17:32 +010070 reg16 = pci_read_config16(PCI_DEV(0, 2, 0), PCI_DEVICE_ID);
Stefan Reinauer00636b02012-04-04 00:08:51 +020071 switch (reg16) {
72 case 0x0102: /* GT1 Desktop */
73 case 0x0106: /* GT1 Mobile */
74 case 0x010a: /* GT1 Server */
75 case 0x0112: /* GT2 Desktop */
76 case 0x0116: /* GT2 Mobile */
77 case 0x0122: /* GT2 Desktop >=1.3GHz */
78 case 0x0126: /* GT2 Mobile >=1.3GHz */
Patrick Rudolph03a88d32015-07-05 13:29:41 +020079 case 0x0152: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080080 case 0x0156: /* IvyBridge */
Damien Zammita10bde92014-10-23 13:29:32 +110081 case 0x0162: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080082 case 0x0166: /* IvyBridge */
Vagiz Trakhanov1dd448c2017-09-28 14:42:11 +000083 case 0x016a: /* IvyBridge */
Stefan Reinauer00636b02012-04-04 00:08:51 +020084 break;
85 default:
86 printk(BIOS_DEBUG, "Graphics not supported by this CPU/chipset.\n");
87 return;
88 }
89
90 printk(BIOS_DEBUG, "Initializing Graphics...\n");
91
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +020092 if (get_option(&gfxsize, "gfx_uma_size") != CB_SUCCESS) {
93 /* Setup IGD memory by setting GGC[7:3] = 1 for 32MB */
94 gfxsize = 0;
95 }
Angel Pons7c49cb82020-03-16 23:17:32 +010096 reg16 = pci_read_config16(HOST_BRIDGE, GGC);
Stefan Reinauer00636b02012-04-04 00:08:51 +020097 reg16 &= ~0x00f8;
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +020098 reg16 |= (gfxsize + 1) << 3;
Stefan Reinauer00636b02012-04-04 00:08:51 +020099 /* Program GTT memory by setting GGC[9:8] = 2MB */
100 reg16 &= ~0x0300;
101 reg16 |= 2 << 8;
102 /* Enable VGA decode */
103 reg16 &= ~0x0002;
Angel Pons7c49cb82020-03-16 23:17:32 +0100104 pci_write_config16(HOST_BRIDGE, GGC, reg16);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200105
106 /* Enable 256MB aperture */
107 reg8 = pci_read_config8(PCI_DEV(0, 2, 0), MSAC);
108 reg8 &= ~0x06;
109 reg8 |= 0x02;
110 pci_write_config8(PCI_DEV(0, 2, 0), MSAC, reg8);
111
112 /* Erratum workarounds */
Angel Pons88521882020-01-05 20:21:20 +0100113 reg32 = MCHBAR32(SAPMCTL);
Angel Pons7c49cb82020-03-16 23:17:32 +0100114 reg32 |= (1 << 9) | (1 << 10);
Angel Pons88521882020-01-05 20:21:20 +0100115 MCHBAR32(SAPMCTL) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200116
117 /* Enable SA Clock Gating */
Angel Pons88521882020-01-05 20:21:20 +0100118 reg32 = MCHBAR32(SAPMCTL);
119 MCHBAR32(SAPMCTL) = reg32 | 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200120
121 /* GPU RC6 workaround for sighting 366252 */
Angel Pons7c49cb82020-03-16 23:17:32 +0100122 reg32 = MCHBAR32(SSKPD_HI);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200123 reg32 |= (1 << 31);
Angel Pons7c49cb82020-03-16 23:17:32 +0100124 MCHBAR32(SSKPD_HI) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200125
Angel Pons7c49cb82020-03-16 23:17:32 +0100126 /* VLW (Virtual Legacy Wire?) */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200127 reg32 = MCHBAR32(0x6120);
128 reg32 &= ~(1 << 0);
129 MCHBAR32(0x6120) = reg32;
130
Angel Pons7c49cb82020-03-16 23:17:32 +0100131 reg32 = MCHBAR32(INTRDIRCTL);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200132 reg32 |= (1 << 4) | (1 << 5);
Angel Pons7c49cb82020-03-16 23:17:32 +0100133 MCHBAR32(INTRDIRCTL) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200134}
135
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200136static void start_peg_link_training(void)
137{
Angel Pons7c49cb82020-03-16 23:17:32 +0100138 u32 tmp, deven;
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200139
Angel Pons7c49cb82020-03-16 23:17:32 +0100140 const u16 base_rev = pci_read_config16(HOST_BRIDGE, PCI_DEVICE_ID) & BASE_REV_MASK;
141 /*
142 * PEG on IvyBridge+ needs a special startup sequence.
143 * As the MRC has its own initialization code skip it.
144 */
145 if ((base_rev != BASE_REV_IVB) || CONFIG(HAVE_MRC))
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200146 return;
147
Angel Pons7c49cb82020-03-16 23:17:32 +0100148 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200149
Angel Pons7c49cb82020-03-16 23:17:32 +0100150 /*
151 * For each PEG device, set bit 5 to use three retries for OC (Offset Calibration).
152 * We also clear DEFER_OC (bit 16) in order to start PEG training.
153 */
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200154 if (deven & DEVEN_PEG10) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100155 tmp = pci_read_config32(PCI_DEV(0, 1, 0), AFE_PWRON) & ~(1 << 16);
156 pci_write_config32(PCI_DEV(0, 1, 0), AFE_PWRON, tmp | (1 << 5));
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200157 }
158
159 if (deven & DEVEN_PEG11) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100160 tmp = pci_read_config32(PCI_DEV(0, 1, 1), AFE_PWRON) & ~(1 << 16);
161 pci_write_config32(PCI_DEV(0, 1, 1), AFE_PWRON, tmp | (1 << 5));
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200162 }
163
164 if (deven & DEVEN_PEG12) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100165 tmp = pci_read_config32(PCI_DEV(0, 1, 2), AFE_PWRON) & ~(1 << 16);
166 pci_write_config32(PCI_DEV(0, 1, 2), AFE_PWRON, tmp | (1 << 5));
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200167 }
168
169 if (deven & DEVEN_PEG60) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100170 tmp = pci_read_config32(PCI_DEV(0, 6, 0), AFE_PWRON) & ~(1 << 16);
171 pci_write_config32(PCI_DEV(0, 6, 0), AFE_PWRON, tmp | (1 << 5));
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200172 }
173}
174
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100175void systemagent_early_init(void)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200176{
177 u32 capid0_a;
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200178 u32 deven;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200179 u8 reg8;
180
181 /* Device ID Override Enable should be done very early */
Angel Pons7c49cb82020-03-16 23:17:32 +0100182 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200183 if (capid0_a & (1 << 10)) {
Patrick Rudolph74203de2017-11-20 11:57:01 +0100184 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
185
Angel Pons7c49cb82020-03-16 23:17:32 +0100186 reg8 = pci_read_config8(HOST_BRIDGE, DIDOR);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200187 reg8 &= ~7; /* Clear 2:0 */
188
Patrick Rudolph74203de2017-11-20 11:57:01 +0100189 if (is_mobile)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200190 reg8 |= 1; /* Set bit 0 */
191
Angel Pons7c49cb82020-03-16 23:17:32 +0100192 pci_write_config8(HOST_BRIDGE, DIDOR, reg8);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200193 }
194
195 /* Setup all BARs required for early PCIe and raminit */
196 sandybridge_setup_bars();
197
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100198 /* Set C0000-FFFFF to access RAM on both reads and writes */
199 enable_pam_region();
200
Nico Huberbb9469c2015-10-21 11:49:23 +0200201 /* Setup IOMMU BARs */
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100202 systemagent_vtd_init();
Nico Huberbb9469c2015-10-21 11:49:23 +0200203
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200204 /* Device Enable, don't touch PEG bits */
Angel Pons7c49cb82020-03-16 23:17:32 +0100205 deven = pci_read_config32(HOST_BRIDGE, DEVEN) | DEVEN_IGD;
206 pci_write_config32(HOST_BRIDGE, DEVEN, deven);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200207
208 sandybridge_setup_graphics();
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200209
Angel Pons7c49cb82020-03-16 23:17:32 +0100210 /*
211 * Write magic values to start PEG link training. This should be done in PCI device
212 * enumeration, but the PCIe specification requires to wait at least 100msec after
213 * reset for devices to come up. As we don't want to increase boot time, enable it
214 * early and assume that PEG is up as soon as PCI enumeration starts.
215 *
216 * TODO: use timestamps to ensure the timings are met.
217 */
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200218 start_peg_link_training();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200219}
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200220
221void northbridge_romstage_finalize(int s3resume)
222{
Angel Pons7c49cb82020-03-16 23:17:32 +0100223 MCHBAR16(SSKPD_HI) = 0xCAFE;
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200224
Aaron Durbin77e13992016-11-29 17:43:04 -0600225 romstage_handoff_init(s3resume);
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200226}