blob: a5ebf9d3a757016ed4c4595c3ce1a67fe4131ee4 [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 */
Angel Pons66780a02021-03-26 13:33:22 +010020 mchbar_write32(GFXVTBAR + 4, GFXVT_BASE >> 32);
21 mchbar_write32(GFXVTBAR + 0, GFXVT_BASE | 1);
22 mchbar_write32(VTVC0BAR + 4, VTVC0_BASE >> 32);
23 mchbar_write32(VTVC0BAR + 0, VTVC0_BASE | 1);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010024
Angel Pons7c49cb82020-03-16 23:17:32 +010025 /* Lock policies */
Elyes Haouasee4646e2022-12-04 09:16:07 +010026 write32p(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) {
Elyes Haouasee4646e2022-12-04 09:16:07 +010030 write32p(VTVC0_BASE + 0xff0, 0x20000000);
31 write32p(VTVC0_BASE + 0xff0, 0xa0000000);
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +010032 } else {
Elyes Haouasee4646e2022-12-04 09:16:07 +010033 write32p(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
Angel Ponsf9c93902020-11-02 22:21:54 +010089 /* Fall back to 32 MiB for IGD memory by setting GGC[7:3] = 1 */
Angel Pons88dcb312021-04-26 17:10:28 +020090 gfxsize = get_uint_option("gfx_uma_size", 0);
Angel Ponsf9c93902020-11-02 22:21:54 +010091
Angel Pons7c49cb82020-03-16 23:17:32 +010092 reg16 = pci_read_config16(HOST_BRIDGE, GGC);
Stefan Reinauer00636b02012-04-04 00:08:51 +020093 reg16 &= ~0x00f8;
Vladimir Serbinenko5fc04d12014-08-03 01:59:38 +020094 reg16 |= (gfxsize + 1) << 3;
Stefan Reinauer00636b02012-04-04 00:08:51 +020095 /* Program GTT memory by setting GGC[9:8] = 2MB */
96 reg16 &= ~0x0300;
97 reg16 |= 2 << 8;
98 /* Enable VGA decode */
99 reg16 &= ~0x0002;
Angel Pons7c49cb82020-03-16 23:17:32 +0100100 pci_write_config16(HOST_BRIDGE, GGC, reg16);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200101
102 /* Enable 256MB aperture */
Angel Pons9733f6a2020-06-07 19:23:03 +0200103 pci_update_config8(PCI_DEV(0, 2, 0), MSAC, ~0x06, 0x02);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200104
105 /* Erratum workarounds */
Angel Pons66780a02021-03-26 13:33:22 +0100106 mchbar_setbits32(SAPMCTL, 1 << 9 | 1 << 10);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200107
108 /* Enable SA Clock Gating */
Angel Pons66780a02021-03-26 13:33:22 +0100109 mchbar_setbits32(SAPMCTL, 1 << 0);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200110
111 /* GPU RC6 workaround for sighting 366252 */
Angel Pons66780a02021-03-26 13:33:22 +0100112 mchbar_setbits32(SSKPD_HI, 1 << 31);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200113
Angel Pons7c49cb82020-03-16 23:17:32 +0100114 /* VLW (Virtual Legacy Wire?) */
Angel Pons66780a02021-03-26 13:33:22 +0100115 mchbar_clrbits32(0x6120, 1 << 0);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200116
Angel Pons66780a02021-03-26 13:33:22 +0100117 mchbar_setbits32(INTRDIRCTL, 1 << 4 | 1 << 5);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200118}
119
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200120static void start_peg_link_training(void)
121{
Angel Pons9733f6a2020-06-07 19:23:03 +0200122 u32 deven;
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200123
Angel Pons7c49cb82020-03-16 23:17:32 +0100124 const u16 base_rev = pci_read_config16(HOST_BRIDGE, PCI_DEVICE_ID) & BASE_REV_MASK;
125 /*
126 * PEG on IvyBridge+ needs a special startup sequence.
127 * As the MRC has its own initialization code skip it.
128 */
129 if ((base_rev != BASE_REV_IVB) || CONFIG(HAVE_MRC))
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200130 return;
131
Angel Pons7c49cb82020-03-16 23:17:32 +0100132 deven = pci_read_config32(HOST_BRIDGE, DEVEN);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200133
Angel Pons7c49cb82020-03-16 23:17:32 +0100134 /*
135 * For each PEG device, set bit 5 to use three retries for OC (Offset Calibration).
136 * We also clear DEFER_OC (bit 16) in order to start PEG training.
137 */
Angel Pons9733f6a2020-06-07 19:23:03 +0200138 if (deven & DEVEN_PEG10)
139 pci_update_config32(PCI_DEV(0, 1, 0), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200140
Angel Pons9733f6a2020-06-07 19:23:03 +0200141 if (deven & DEVEN_PEG11)
142 pci_update_config32(PCI_DEV(0, 1, 1), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200143
Angel Pons9733f6a2020-06-07 19:23:03 +0200144 if (deven & DEVEN_PEG12)
145 pci_update_config32(PCI_DEV(0, 1, 2), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200146
Angel Pons9733f6a2020-06-07 19:23:03 +0200147 if (deven & DEVEN_PEG60)
148 pci_update_config32(PCI_DEV(0, 6, 0), AFE_PWRON, ~(1 << 16), 1 << 5);
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200149}
150
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100151void systemagent_early_init(void)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200152{
Patrick Rudolph51518e52023-10-29 19:44:52 +0100153 const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE;
Stefan Reinauer00636b02012-04-04 00:08:51 +0200154 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)) {
Angel Pons7c49cb82020-03-16 23:17:32 +0100160 reg8 = pci_read_config8(HOST_BRIDGE, DIDOR);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200161 reg8 &= ~7; /* Clear 2:0 */
162
Patrick Rudolph74203de2017-11-20 11:57:01 +0100163 if (is_mobile)
Stefan Reinauer00636b02012-04-04 00:08:51 +0200164 reg8 |= 1; /* Set bit 0 */
165
Angel Pons7c49cb82020-03-16 23:17:32 +0100166 pci_write_config8(HOST_BRIDGE, DIDOR, reg8);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200167 }
168
Patrick Rudolph51518e52023-10-29 19:44:52 +0100169 /* Print platform type */
170 printk(BIOS_INFO, "Detected system type: %s\n", is_mobile ? "mobile" : "desktop");
171
Stefan Reinauer00636b02012-04-04 00:08:51 +0200172 /* Setup all BARs required for early PCIe and raminit */
173 sandybridge_setup_bars();
174
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100175 /* Set C0000-FFFFF to access RAM on both reads and writes */
176 enable_pam_region();
177
Nico Huberbb9469c2015-10-21 11:49:23 +0200178 /* Setup IOMMU BARs */
Patrick Rudolph2cdb65d2019-03-24 18:08:43 +0100179 systemagent_vtd_init();
Nico Huberbb9469c2015-10-21 11:49:23 +0200180
Patrick Rudolph2a510a72015-07-28 07:51:10 +0200181 /* Device Enable, don't touch PEG bits */
Angel Pons9733f6a2020-06-07 19:23:03 +0200182 pci_or_config32(HOST_BRIDGE, DEVEN, DEVEN_IGD);
Stefan Reinauer00636b02012-04-04 00:08:51 +0200183
184 sandybridge_setup_graphics();
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200185
Angel Pons7c49cb82020-03-16 23:17:32 +0100186 /*
187 * Write magic values to start PEG link training. This should be done in PCI device
188 * enumeration, but the PCIe specification requires to wait at least 100msec after
189 * reset for devices to come up. As we don't want to increase boot time, enable it
190 * early and assume that PEG is up as soon as PCI enumeration starts.
191 *
192 * TODO: use timestamps to ensure the timings are met.
193 */
Patrick Rudolphe4f9d5c2015-10-15 11:09:15 +0200194 start_peg_link_training();
Stefan Reinauer00636b02012-04-04 00:08:51 +0200195}
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200196
Kyösti Mälkki4ce0a072021-02-17 18:10:49 +0200197void northbridge_romstage_finalize(void)
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200198{
Angel Pons66780a02021-03-26 13:33:22 +0100199 mchbar_write16(SSKPD_HI, 0xcafe);
Vladimir Serbinenkoc845b432014-09-05 03:37:44 +0200200}