blob: 0c9325da41997daf7282023e2ff9e2d08720e80a [file] [log] [blame]
Angel Pons6e5aabd2020-03-23 23:44:42 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer00636b02012-04-04 00:08:51 +02002
Stefan Reinauer00636b02012-04-04 00:08:51 +02003#include <console/console.h>
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +01004#include <device/mmio.h>
5#include <device/device.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +02006#include <device/pci_ops.h>
Stefan Reinauer00636b02012-04-04 00:08:51 +02007#include <device/pci_def.h>
Kyösti Mälkkicbf95712020-01-05 08:05:45 +02008#include <option.h>
Kyösti Mälkkie39a8a92016-06-25 11:40:00 +03009#include <romstage_handoff.h>
Elyes HAOUAS51401c32019-05-15 21:09:30 +020010#include <types.h>
11
Stefan Reinauer00636b02012-04-04 00:08:51 +020012#include "sandybridge.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020013
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010014static void systemagent_vtd_init(void)
15{
Angel Pons7c49cb82020-03-16 23:17:32 +010016 const u32 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010017 if (capid0_a & (1 << 23))
18 return;
19
Angel Pons7c49cb82020-03-16 23:17:32 +010020 /* Setup BARs */
21 MCHBAR32(GFXVTBAR + 4) = GFXVT_BASE >> 32;
22 MCHBAR32(GFXVTBAR) = GFXVT_BASE | 1;
23 MCHBAR32(VTVC0BAR + 4) = VTVC0_BASE >> 32;
24 MCHBAR32(VTVC0BAR) = VTVC0_BASE | 1;
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010025
Angel Pons7c49cb82020-03-16 23:17:32 +010026 /* Lock policies */
27 write32((void *)(GFXVT_BASE + 0xff0), 0x80000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010028
29 const struct device *const azalia = pcidev_on_root(0x1b, 0);
30 if (azalia && azalia->enabled) {
Angel Pons7c49cb82020-03-16 23:17:32 +010031 write32((void *)(VTVC0_BASE + 0xff0), 0x20000000);
32 write32((void *)(VTVC0_BASE + 0xff0), 0xa0000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010033 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +010034 write32((void *)(VTVC0_BASE + 0xff0), 0x80000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010035 }
36}
37
38static void enable_pam_region(void)
39{
Angel Pons7c49cb82020-03-16 23:17:32 +010040 pci_write_config8(HOST_BRIDGE, PAM0, 0x30);
41 pci_write_config8(HOST_BRIDGE, PAM1, 0x33);
42 pci_write_config8(HOST_BRIDGE, PAM2, 0x33);
43 pci_write_config8(HOST_BRIDGE, PAM3, 0x33);
44 pci_write_config8(HOST_BRIDGE, PAM4, 0x33);
45 pci_write_config8(HOST_BRIDGE, PAM5, 0x33);
46 pci_write_config8(HOST_BRIDGE, PAM6, 0x33);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010047}
48
Stefan Reinauer00636b02012-04-04 00:08:51 +020049static void sandybridge_setup_bars(void)
50{
Stefan Reinauer00636b02012-04-04 00:08:51 +020051 printk(BIOS_DEBUG, "Setting up static northbridge registers...");
52 /* Set up all hardcoded northbridge BARs */
Angel Pons7c49cb82020-03-16 23:17:32 +010053 pci_write_config32(HOST_BRIDGE, EPBAR, DEFAULT_EPBAR | 1);
54 pci_write_config32(HOST_BRIDGE, EPBAR + 4, (0LL + DEFAULT_EPBAR) >> 32);
55 pci_write_config32(HOST_BRIDGE, MCHBAR, (uintptr_t)DEFAULT_MCHBAR | 1);
56 pci_write_config32(HOST_BRIDGE, MCHBAR + 4, (0LL + (uintptr_t)DEFAULT_MCHBAR) >> 32);
57 pci_write_config32(HOST_BRIDGE, DMIBAR, (uintptr_t)DEFAULT_DMIBAR | 1);
58 pci_write_config32(HOST_BRIDGE, DMIBAR + 4, (0LL + (uintptr_t)DEFAULT_DMIBAR) >> 32);
Stefan Reinauer00636b02012-04-04 00:08:51 +020059
Patrick Rudolph90050712019-03-25 09:53:23 +010060 printk(BIOS_DEBUG, " done\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +020061}
62
63static void sandybridge_setup_graphics(void)
64{
65 u32 reg32;
66 u16 reg16;
Angel Pons9733f6a2020-06-07 19:23:03 +020067 u8 gfxsize;
Stefan Reinauer00636b02012-04-04 00:08:51 +020068
Angel Pons7c49cb82020-03-16 23:17:32 +010069 reg16 = pci_read_config16(PCI_DEV(0, 2, 0), PCI_DEVICE_ID);
Stefan Reinauer00636b02012-04-04 00:08:51 +020070 switch (reg16) {
71 case 0x0102: /* GT1 Desktop */
72 case 0x0106: /* GT1 Mobile */
73 case 0x010a: /* GT1 Server */
74 case 0x0112: /* GT2 Desktop */
75 case 0x0116: /* GT2 Mobile */
76 case 0x0122: /* GT2 Desktop >=1.3GHz */
77 case 0x0126: /* GT2 Mobile >=1.3GHz */
Patrick Rudolph03a88d32015-07-05 13:29:41 +020078 case 0x0152: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080079 case 0x0156: /* IvyBridge */
Damien Zammita10bde92014-10-23 13:29:32 +110080 case 0x0162: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080081 case 0x0166: /* IvyBridge */
Vagiz Trakhanov1dd448c2017-09-28 14:42:11 +000082 case 0x016a: /* IvyBridge */
Stefan Reinauer00636b02012-04-04 00:08:51 +020083 break;
84 default:
85 printk(BIOS_DEBUG, "Graphics not supported by this CPU/chipset.\n");
86 return;
87 }
88
89 printk(BIOS_DEBUG, "Initializing Graphics...\n");
90
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +020091 if (get_option(&gfxsize, "gfx_uma_size") != CB_SUCCESS) {
92 /* Setup IGD memory by setting GGC[7:3] = 1 for 32MB */
93 gfxsize = 0;
94 }
Angel Pons7c49cb82020-03-16 23:17:32 +010095 reg16 = pci_read_config16(HOST_BRIDGE, GGC);
Stefan Reinauer00636b02012-04-04 00:08:51 +020096 reg16 &= ~0x00f8;
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +020097 reg16 |= (gfxsize + 1) << 3;
Stefan Reinauer00636b02012-04-04 00:08:51 +020098 /* Program GTT memory by setting GGC[9:8] = 2MB */
99 reg16 &= ~0x0300;
100 reg16 |= 2 << 8;
101 /* Enable VGA decode */
102 reg16 &= ~0x0002;
Angel Pons7c49cb82020-03-16 23:17:32 +0100103 pci_write_config16(HOST_BRIDGE, GGC, reg16);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200104
105 /* Enable 256MB aperture */
Angel Pons9733f6a2020-06-07 19:23:03 +0200106 pci_update_config8(PCI_DEV(0, 2, 0), MSAC, ~0x06, 0x02);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200107
108 /* Erratum workarounds */
Angel Pons88521882020-01-05 20:21:20 +0100109 reg32 = MCHBAR32(SAPMCTL);
Angel Pons7c49cb82020-03-16 23:17:32 +0100110 reg32 |= (1 << 9) | (1 << 10);
Angel Pons88521882020-01-05 20:21:20 +0100111 MCHBAR32(SAPMCTL) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200112
113 /* Enable SA Clock Gating */
Angel Pons88521882020-01-05 20:21:20 +0100114 reg32 = MCHBAR32(SAPMCTL);
115 MCHBAR32(SAPMCTL) = reg32 | 1;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200116
117 /* GPU RC6 workaround for sighting 366252 */
Angel Pons7c49cb82020-03-16 23:17:32 +0100118 reg32 = MCHBAR32(SSKPD_HI);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200119 reg32 |= (1 << 31);
Angel Pons7c49cb82020-03-16 23:17:32 +0100120 MCHBAR32(SSKPD_HI) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200121
Angel Pons7c49cb82020-03-16 23:17:32 +0100122 /* VLW (Virtual Legacy Wire?) */
Stefan Reinauer00636b02012-04-04 00:08:51 +0200123 reg32 = MCHBAR32(0x6120);
124 reg32 &= ~(1 << 0);
125 MCHBAR32(0x6120) = reg32;
126
Angel Pons7c49cb82020-03-16 23:17:32 +0100127 reg32 = MCHBAR32(INTRDIRCTL);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200128 reg32 |= (1 << 4) | (1 << 5);
Angel Pons7c49cb82020-03-16 23:17:32 +0100129 MCHBAR32(INTRDIRCTL) = reg32;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200130}
131
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200132static void start_peg_link_training(void)
133{
Angel Pons9733f6a2020-06-07 19:23:03 +0200134 u32 deven;
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200135
Angel Pons7c49cb82020-03-16 23:17:32 +0100136 const u16 base_rev = pci_read_config16(HOST_BRIDGE, PCI_DEVICE_ID) & BASE_REV_MASK;
137 /*
138 * PEG on IvyBridge+ needs a special startup sequence.
139 * As the MRC has its own initialization code skip it.
140 */
141 if ((base_rev != BASE_REV_IVB) || CONFIG(HAVE_MRC))
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200142 return;
143
Angel Pons7c49cb82020-03-16 23:17:32 +0100144 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200145
Angel Pons7c49cb82020-03-16 23:17:32 +0100146 /*
147 * For each PEG device, set bit 5 to use three retries for OC (Offset Calibration).
148 * We also clear DEFER_OC (bit 16) in order to start PEG training.
149 */
Angel Pons9733f6a2020-06-07 19:23:03 +0200150 if (deven & DEVEN_PEG10)
151 pci_update_config32(PCI_DEV(0, 1, 0), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200152
Angel Pons9733f6a2020-06-07 19:23:03 +0200153 if (deven & DEVEN_PEG11)
154 pci_update_config32(PCI_DEV(0, 1, 1), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200155
Angel Pons9733f6a2020-06-07 19:23:03 +0200156 if (deven & DEVEN_PEG12)
157 pci_update_config32(PCI_DEV(0, 1, 2), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200158
Angel Pons9733f6a2020-06-07 19:23:03 +0200159 if (deven & DEVEN_PEG60)
160 pci_update_config32(PCI_DEV(0, 6, 0), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200161}
162
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100163void systemagent_early_init(void)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200164{
165 u32 capid0_a;
166 u8 reg8;
167
168 /* Device ID Override Enable should be done very early */
Angel Pons7c49cb82020-03-16 23:17:32 +0100169 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200170 if (capid0_a & (1 << 10)) {
Patrick Rudolph74203de2017-11-20 11:57:01 +0100171 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
172
Angel Pons7c49cb82020-03-16 23:17:32 +0100173 reg8 = pci_read_config8(HOST_BRIDGE, DIDOR);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200174 reg8 &= ~7; /* Clear 2:0 */
175
Patrick Rudolph74203de2017-11-20 11:57:01 +0100176 if (is_mobile)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200177 reg8 |= 1; /* Set bit 0 */
178
Angel Pons7c49cb82020-03-16 23:17:32 +0100179 pci_write_config8(HOST_BRIDGE, DIDOR, reg8);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200180 }
181
182 /* Setup all BARs required for early PCIe and raminit */
183 sandybridge_setup_bars();
184
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100185 /* Set C0000-FFFFF to access RAM on both reads and writes */
186 enable_pam_region();
187
Nico Huberbb9469c2015-10-21 11:49:23 +0200188 /* Setup IOMMU BARs */
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100189 systemagent_vtd_init();
Nico Huberbb9469c2015-10-21 11:49:23 +0200190
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200191 /* Device Enable, don't touch PEG bits */
Angel Pons9733f6a2020-06-07 19:23:03 +0200192 pci_or_config32(HOST_BRIDGE, DEVEN, DEVEN_IGD);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200193
194 sandybridge_setup_graphics();
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200195
Angel Pons7c49cb82020-03-16 23:17:32 +0100196 /*
197 * Write magic values to start PEG link training. This should be done in PCI device
198 * enumeration, but the PCIe specification requires to wait at least 100msec after
199 * reset for devices to come up. As we don't want to increase boot time, enable it
200 * early and assume that PEG is up as soon as PCI enumeration starts.
201 *
202 * TODO: use timestamps to ensure the timings are met.
203 */
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200204 start_peg_link_training();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200205}
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200206
207void northbridge_romstage_finalize(int s3resume)
208{
Angel Pons7c49cb82020-03-16 23:17:32 +0100209 MCHBAR16(SSKPD_HI) = 0xCAFE;
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200210
Aaron Durbin77e13992016-11-29 17:43:04 -0600211 romstage_handoff_init(s3resume);
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200212}