blob: b7ee3225ec24e2c80e1a2f51327c25cd4c3aa19e [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>
Elyes HAOUAS51401c32019-05-15 21:09:30 +02009#include <types.h>
10
Stefan Reinauer00636b02012-04-04 00:08:51 +020011#include "sandybridge.h"
Stefan Reinauer00636b02012-04-04 00:08:51 +020012
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010013static void systemagent_vtd_init(void)
14{
Angel Pons7c49cb82020-03-16 23:17:32 +010015 const u32 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010016 if (capid0_a & (1 << 23))
17 return;
18
Angel Pons7c49cb82020-03-16 23:17:32 +010019 /* Setup BARs */
20 MCHBAR32(GFXVTBAR + 4) = GFXVT_BASE >> 32;
21 MCHBAR32(GFXVTBAR) = GFXVT_BASE | 1;
22 MCHBAR32(VTVC0BAR + 4) = VTVC0_BASE >> 32;
23 MCHBAR32(VTVC0BAR) = VTVC0_BASE | 1;
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010024
Angel Pons7c49cb82020-03-16 23:17:32 +010025 /* Lock policies */
26 write32((void *)(GFXVT_BASE + 0xff0), 0x80000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010027
28 const struct device *const azalia = pcidev_on_root(0x1b, 0);
29 if (azalia && azalia->enabled) {
Angel Pons7c49cb82020-03-16 23:17:32 +010030 write32((void *)(VTVC0_BASE + 0xff0), 0x20000000);
31 write32((void *)(VTVC0_BASE + 0xff0), 0xa0000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010032 } else {
Angel Pons7c49cb82020-03-16 23:17:32 +010033 write32((void *)(VTVC0_BASE + 0xff0), 0x80000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010034 }
35}
36
37static void enable_pam_region(void)
38{
Angel Pons7c49cb82020-03-16 23:17:32 +010039 pci_write_config8(HOST_BRIDGE, PAM0, 0x30);
40 pci_write_config8(HOST_BRIDGE, PAM1, 0x33);
41 pci_write_config8(HOST_BRIDGE, PAM2, 0x33);
42 pci_write_config8(HOST_BRIDGE, PAM3, 0x33);
43 pci_write_config8(HOST_BRIDGE, PAM4, 0x33);
44 pci_write_config8(HOST_BRIDGE, PAM5, 0x33);
45 pci_write_config8(HOST_BRIDGE, PAM6, 0x33);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010046}
47
Stefan Reinauer00636b02012-04-04 00:08:51 +020048static void sandybridge_setup_bars(void)
49{
Stefan Reinauer00636b02012-04-04 00:08:51 +020050 printk(BIOS_DEBUG, "Setting up static northbridge registers...");
51 /* Set up all hardcoded northbridge BARs */
Angel Ponsd9e58dc2021-01-20 01:22:20 +010052 pci_write_config32(HOST_BRIDGE, EPBAR, CONFIG_FIXED_EPBAR_MMIO_BASE | 1);
53 pci_write_config32(HOST_BRIDGE, EPBAR + 4, 0);
54 pci_write_config32(HOST_BRIDGE, MCHBAR, CONFIG_FIXED_MCHBAR_MMIO_BASE | 1);
55 pci_write_config32(HOST_BRIDGE, MCHBAR + 4, 0);
56 pci_write_config32(HOST_BRIDGE, DMIBAR, CONFIG_FIXED_DMIBAR_MMIO_BASE | 1);
57 pci_write_config32(HOST_BRIDGE, DMIBAR + 4, 0);
Stefan Reinauer00636b02012-04-04 00:08:51 +020058
Patrick Rudolph90050712019-03-25 09:53:23 +010059 printk(BIOS_DEBUG, " done\n");
Stefan Reinauer00636b02012-04-04 00:08:51 +020060}
61
62static void sandybridge_setup_graphics(void)
63{
Stefan Reinauer00636b02012-04-04 00:08:51 +020064 u16 reg16;
Angel Pons9733f6a2020-06-07 19:23:03 +020065 u8 gfxsize;
Stefan Reinauer00636b02012-04-04 00:08:51 +020066
Angel Pons7c49cb82020-03-16 23:17:32 +010067 reg16 = pci_read_config16(PCI_DEV(0, 2, 0), PCI_DEVICE_ID);
Stefan Reinauer00636b02012-04-04 00:08:51 +020068 switch (reg16) {
69 case 0x0102: /* GT1 Desktop */
70 case 0x0106: /* GT1 Mobile */
71 case 0x010a: /* GT1 Server */
72 case 0x0112: /* GT2 Desktop */
73 case 0x0116: /* GT2 Mobile */
74 case 0x0122: /* GT2 Desktop >=1.3GHz */
75 case 0x0126: /* GT2 Mobile >=1.3GHz */
Patrick Rudolph03a88d32015-07-05 13:29:41 +020076 case 0x0152: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080077 case 0x0156: /* IvyBridge */
Damien Zammita10bde92014-10-23 13:29:32 +110078 case 0x0162: /* IvyBridge */
Stefan Reinauer816e9d12013-01-14 10:25:43 -080079 case 0x0166: /* IvyBridge */
Vagiz Trakhanov1dd448c2017-09-28 14:42:11 +000080 case 0x016a: /* IvyBridge */
Stefan Reinauer00636b02012-04-04 00:08:51 +020081 break;
82 default:
83 printk(BIOS_DEBUG, "Graphics not supported by this CPU/chipset.\n");
84 return;
85 }
86
87 printk(BIOS_DEBUG, "Initializing Graphics...\n");
88
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +020089 if (get_option(&gfxsize, "gfx_uma_size") != CB_SUCCESS) {
90 /* Setup IGD memory by setting GGC[7:3] = 1 for 32MB */
91 gfxsize = 0;
92 }
Angel Pons7c49cb82020-03-16 23:17:32 +010093 reg16 = pci_read_config16(HOST_BRIDGE, GGC);
Stefan Reinauer00636b02012-04-04 00:08:51 +020094 reg16 &= ~0x00f8;
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +020095 reg16 |= (gfxsize + 1) << 3;
Stefan Reinauer00636b02012-04-04 00:08:51 +020096 /* Program GTT memory by setting GGC[9:8] = 2MB */
97 reg16 &= ~0x0300;
98 reg16 |= 2 << 8;
99 /* Enable VGA decode */
100 reg16 &= ~0x0002;
Angel Pons7c49cb82020-03-16 23:17:32 +0100101 pci_write_config16(HOST_BRIDGE, GGC, reg16);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200102
103 /* Enable 256MB aperture */
Angel Pons9733f6a2020-06-07 19:23:03 +0200104 pci_update_config8(PCI_DEV(0, 2, 0), MSAC, ~0x06, 0x02);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200105
106 /* Erratum workarounds */
Angel Pons71892b42020-06-07 19:30:26 +0200107 MCHBAR32_OR(SAPMCTL, (1 << 9) | (1 << 10));
Stefan Reinauer00636b02012-04-04 00:08:51 +0200108
109 /* Enable SA Clock Gating */
Angel Pons71892b42020-06-07 19:30:26 +0200110 MCHBAR32_OR(SAPMCTL, 1);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200111
112 /* GPU RC6 workaround for sighting 366252 */
Angel Pons71892b42020-06-07 19:30:26 +0200113 MCHBAR32_OR(SSKPD_HI, 1 << 31);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200114
Angel Pons7c49cb82020-03-16 23:17:32 +0100115 /* VLW (Virtual Legacy Wire?) */
Angel Pons71892b42020-06-07 19:30:26 +0200116 MCHBAR32_AND(0x6120, ~(1 << 0));
Stefan Reinauer00636b02012-04-04 00:08:51 +0200117
Angel Pons71892b42020-06-07 19:30:26 +0200118 MCHBAR32_OR(INTRDIRCTL, (1 << 4) | (1 << 5));
Stefan Reinauer00636b02012-04-04 00:08:51 +0200119}
120
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200121static void start_peg_link_training(void)
122{
Angel Pons9733f6a2020-06-07 19:23:03 +0200123 u32 deven;
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200124
Angel Pons7c49cb82020-03-16 23:17:32 +0100125 const u16 base_rev = pci_read_config16(HOST_BRIDGE, PCI_DEVICE_ID) & BASE_REV_MASK;
126 /*
127 * PEG on IvyBridge+ needs a special startup sequence.
128 * As the MRC has its own initialization code skip it.
129 */
130 if ((base_rev != BASE_REV_IVB) || CONFIG(HAVE_MRC))
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200131 return;
132
Angel Pons7c49cb82020-03-16 23:17:32 +0100133 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200134
Angel Pons7c49cb82020-03-16 23:17:32 +0100135 /*
136 * For each PEG device, set bit 5 to use three retries for OC (Offset Calibration).
137 * We also clear DEFER_OC (bit 16) in order to start PEG training.
138 */
Angel Pons9733f6a2020-06-07 19:23:03 +0200139 if (deven & DEVEN_PEG10)
140 pci_update_config32(PCI_DEV(0, 1, 0), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200141
Angel Pons9733f6a2020-06-07 19:23:03 +0200142 if (deven & DEVEN_PEG11)
143 pci_update_config32(PCI_DEV(0, 1, 1), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200144
Angel Pons9733f6a2020-06-07 19:23:03 +0200145 if (deven & DEVEN_PEG12)
146 pci_update_config32(PCI_DEV(0, 1, 2), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200147
Angel Pons9733f6a2020-06-07 19:23:03 +0200148 if (deven & DEVEN_PEG60)
149 pci_update_config32(PCI_DEV(0, 6, 0), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200150}
151
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100152void systemagent_early_init(void)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200153{
154 u32 capid0_a;
155 u8 reg8;
156
157 /* Device ID Override Enable should be done very early */
Angel Pons7c49cb82020-03-16 23:17:32 +0100158 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200159 if (capid0_a & (1 << 10)) {
Patrick Rudolph74203de2017-11-20 11:57:01 +0100160 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
161
Angel Pons7c49cb82020-03-16 23:17:32 +0100162 reg8 = pci_read_config8(HOST_BRIDGE, DIDOR);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200163 reg8 &= ~7; /* Clear 2:0 */
164
Patrick Rudolph74203de2017-11-20 11:57:01 +0100165 if (is_mobile)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200166 reg8 |= 1; /* Set bit 0 */
167
Angel Pons7c49cb82020-03-16 23:17:32 +0100168 pci_write_config8(HOST_BRIDGE, DIDOR, reg8);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200169 }
170
171 /* Setup all BARs required for early PCIe and raminit */
172 sandybridge_setup_bars();
173
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100174 /* Set C0000-FFFFF to access RAM on both reads and writes */
175 enable_pam_region();
176
Nico Huberbb9469c2015-10-21 11:49:23 +0200177 /* Setup IOMMU BARs */
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100178 systemagent_vtd_init();
Nico Huberbb9469c2015-10-21 11:49:23 +0200179
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200180 /* Device Enable, don't touch PEG bits */
Angel Pons9733f6a2020-06-07 19:23:03 +0200181 pci_or_config32(HOST_BRIDGE, DEVEN, DEVEN_IGD);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200182
183 sandybridge_setup_graphics();
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200184
Angel Pons7c49cb82020-03-16 23:17:32 +0100185 /*
186 * Write magic values to start PEG link training. This should be done in PCI device
187 * enumeration, but the PCIe specification requires to wait at least 100msec after
188 * reset for devices to come up. As we don't want to increase boot time, enable it
189 * early and assume that PEG is up as soon as PCI enumeration starts.
190 *
191 * TODO: use timestamps to ensure the timings are met.
192 */
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200193 start_peg_link_training();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200194}
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200195
Kyösti Mälkki4ce0a072021-02-17 18:10:49 +0200196void northbridge_romstage_finalize(void)
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200197{
Angel Pons7c49cb82020-03-16 23:17:32 +0100198 MCHBAR16(SSKPD_HI) = 0xCAFE;
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200199}