Angel Pons | 6e5aabd | 2020-03-23 23:44:42 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 2 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 3 | #include <console/console.h> |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 4 | #include <device/mmio.h> |
| 5 | #include <device/device.h> |
Kyösti Mälkki | f1b58b7 | 2019-03-01 13:43:02 +0200 | [diff] [blame] | 6 | #include <device/pci_ops.h> |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 7 | #include <device/pci_def.h> |
Kyösti Mälkki | cbf9571 | 2020-01-05 08:05:45 +0200 | [diff] [blame] | 8 | #include <option.h> |
Kyösti Mälkki | e39a8a9 | 2016-06-25 11:40:00 +0300 | [diff] [blame] | 9 | #include <romstage_handoff.h> |
Elyes HAOUAS | 51401c3 | 2019-05-15 21:09:30 +0200 | [diff] [blame] | 10 | #include <types.h> |
| 11 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 12 | #include "sandybridge.h" |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 13 | |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 14 | static void systemagent_vtd_init(void) |
| 15 | { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 16 | const u32 capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A); |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 17 | if (capid0_a & (1 << 23)) |
| 18 | return; |
| 19 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 20 | /* 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 Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 25 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 26 | /* Lock policies */ |
| 27 | write32((void *)(GFXVT_BASE + 0xff0), 0x80000000); |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 28 | |
| 29 | const struct device *const azalia = pcidev_on_root(0x1b, 0); |
| 30 | if (azalia && azalia->enabled) { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 31 | write32((void *)(VTVC0_BASE + 0xff0), 0x20000000); |
| 32 | write32((void *)(VTVC0_BASE + 0xff0), 0xa0000000); |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 33 | } else { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 34 | write32((void *)(VTVC0_BASE + 0xff0), 0x80000000); |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
| 38 | static void enable_pam_region(void) |
| 39 | { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 40 | 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 Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 47 | } |
| 48 | |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 49 | static void sandybridge_setup_bars(void) |
| 50 | { |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 51 | printk(BIOS_DEBUG, "Setting up static northbridge registers..."); |
| 52 | /* Set up all hardcoded northbridge BARs */ |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 53 | 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 Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 59 | |
Patrick Rudolph | 9005071 | 2019-03-25 09:53:23 +0100 | [diff] [blame] | 60 | printk(BIOS_DEBUG, " done\n"); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static void sandybridge_setup_graphics(void) |
| 64 | { |
| 65 | u32 reg32; |
| 66 | u16 reg16; |
Angel Pons | 9733f6a | 2020-06-07 19:23:03 +0200 | [diff] [blame^] | 67 | u8 gfxsize; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 68 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 69 | reg16 = pci_read_config16(PCI_DEV(0, 2, 0), PCI_DEVICE_ID); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 70 | 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 Rudolph | 03a88d3 | 2015-07-05 13:29:41 +0200 | [diff] [blame] | 78 | case 0x0152: /* IvyBridge */ |
Stefan Reinauer | 816e9d1 | 2013-01-14 10:25:43 -0800 | [diff] [blame] | 79 | case 0x0156: /* IvyBridge */ |
Damien Zammit | a10bde9 | 2014-10-23 13:29:32 +1100 | [diff] [blame] | 80 | case 0x0162: /* IvyBridge */ |
Stefan Reinauer | 816e9d1 | 2013-01-14 10:25:43 -0800 | [diff] [blame] | 81 | case 0x0166: /* IvyBridge */ |
Vagiz Trakhanov | 1dd448c | 2017-09-28 14:42:11 +0000 | [diff] [blame] | 82 | case 0x016a: /* IvyBridge */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 83 | 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 Serbinenko | 5fc04d1 | 2014-08-03 01:59:38 +0200 | [diff] [blame] | 91 | 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 Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 95 | reg16 = pci_read_config16(HOST_BRIDGE, GGC); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 96 | reg16 &= ~0x00f8; |
Vladimir Serbinenko | 5fc04d1 | 2014-08-03 01:59:38 +0200 | [diff] [blame] | 97 | reg16 |= (gfxsize + 1) << 3; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 98 | /* Program GTT memory by setting GGC[9:8] = 2MB */ |
| 99 | reg16 &= ~0x0300; |
| 100 | reg16 |= 2 << 8; |
| 101 | /* Enable VGA decode */ |
| 102 | reg16 &= ~0x0002; |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 103 | pci_write_config16(HOST_BRIDGE, GGC, reg16); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 104 | |
| 105 | /* Enable 256MB aperture */ |
Angel Pons | 9733f6a | 2020-06-07 19:23:03 +0200 | [diff] [blame^] | 106 | pci_update_config8(PCI_DEV(0, 2, 0), MSAC, ~0x06, 0x02); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 107 | |
| 108 | /* Erratum workarounds */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 109 | reg32 = MCHBAR32(SAPMCTL); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 110 | reg32 |= (1 << 9) | (1 << 10); |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 111 | MCHBAR32(SAPMCTL) = reg32; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 112 | |
| 113 | /* Enable SA Clock Gating */ |
Angel Pons | 8852188 | 2020-01-05 20:21:20 +0100 | [diff] [blame] | 114 | reg32 = MCHBAR32(SAPMCTL); |
| 115 | MCHBAR32(SAPMCTL) = reg32 | 1; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 116 | |
| 117 | /* GPU RC6 workaround for sighting 366252 */ |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 118 | reg32 = MCHBAR32(SSKPD_HI); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 119 | reg32 |= (1 << 31); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 120 | MCHBAR32(SSKPD_HI) = reg32; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 121 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 122 | /* VLW (Virtual Legacy Wire?) */ |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 123 | reg32 = MCHBAR32(0x6120); |
| 124 | reg32 &= ~(1 << 0); |
| 125 | MCHBAR32(0x6120) = reg32; |
| 126 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 127 | reg32 = MCHBAR32(INTRDIRCTL); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 128 | reg32 |= (1 << 4) | (1 << 5); |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 129 | MCHBAR32(INTRDIRCTL) = reg32; |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Patrick Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 132 | static void start_peg_link_training(void) |
| 133 | { |
Angel Pons | 9733f6a | 2020-06-07 19:23:03 +0200 | [diff] [blame^] | 134 | u32 deven; |
Patrick Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 135 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 136 | 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 Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 142 | return; |
| 143 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 144 | deven = pci_read_config32(HOST_BRIDGE, DEVEN); |
Patrick Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 145 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 146 | /* |
| 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 Pons | 9733f6a | 2020-06-07 19:23:03 +0200 | [diff] [blame^] | 150 | if (deven & DEVEN_PEG10) |
| 151 | pci_update_config32(PCI_DEV(0, 1, 0), AFE_PWRON, ~(1 << 16), 1 << 5); |
Patrick Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 152 | |
Angel Pons | 9733f6a | 2020-06-07 19:23:03 +0200 | [diff] [blame^] | 153 | if (deven & DEVEN_PEG11) |
| 154 | pci_update_config32(PCI_DEV(0, 1, 1), AFE_PWRON, ~(1 << 16), 1 << 5); |
Patrick Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 155 | |
Angel Pons | 9733f6a | 2020-06-07 19:23:03 +0200 | [diff] [blame^] | 156 | if (deven & DEVEN_PEG12) |
| 157 | pci_update_config32(PCI_DEV(0, 1, 2), AFE_PWRON, ~(1 << 16), 1 << 5); |
Patrick Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 158 | |
Angel Pons | 9733f6a | 2020-06-07 19:23:03 +0200 | [diff] [blame^] | 159 | if (deven & DEVEN_PEG60) |
| 160 | pci_update_config32(PCI_DEV(0, 6, 0), AFE_PWRON, ~(1 << 16), 1 << 5); |
Patrick Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 161 | } |
| 162 | |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 163 | void systemagent_early_init(void) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 164 | { |
| 165 | u32 capid0_a; |
| 166 | u8 reg8; |
| 167 | |
| 168 | /* Device ID Override Enable should be done very early */ |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 169 | capid0_a = pci_read_config32(HOST_BRIDGE, CAPID0_A); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 170 | if (capid0_a & (1 << 10)) { |
Patrick Rudolph | 74203de | 2017-11-20 11:57:01 +0100 | [diff] [blame] | 171 | const size_t is_mobile = get_platform_type() == PLATFORM_MOBILE; |
| 172 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 173 | reg8 = pci_read_config8(HOST_BRIDGE, DIDOR); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 174 | reg8 &= ~7; /* Clear 2:0 */ |
| 175 | |
Patrick Rudolph | 74203de | 2017-11-20 11:57:01 +0100 | [diff] [blame] | 176 | if (is_mobile) |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 177 | reg8 |= 1; /* Set bit 0 */ |
| 178 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 179 | pci_write_config8(HOST_BRIDGE, DIDOR, reg8); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* Setup all BARs required for early PCIe and raminit */ |
| 183 | sandybridge_setup_bars(); |
| 184 | |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 185 | /* Set C0000-FFFFF to access RAM on both reads and writes */ |
| 186 | enable_pam_region(); |
| 187 | |
Nico Huber | bb9469c | 2015-10-21 11:49:23 +0200 | [diff] [blame] | 188 | /* Setup IOMMU BARs */ |
Patrick Rudolph | 2cdb65d | 2019-03-24 18:08:43 +0100 | [diff] [blame] | 189 | systemagent_vtd_init(); |
Nico Huber | bb9469c | 2015-10-21 11:49:23 +0200 | [diff] [blame] | 190 | |
Patrick Rudolph | 2a510a7 | 2015-07-28 07:51:10 +0200 | [diff] [blame] | 191 | /* Device Enable, don't touch PEG bits */ |
Angel Pons | 9733f6a | 2020-06-07 19:23:03 +0200 | [diff] [blame^] | 192 | pci_or_config32(HOST_BRIDGE, DEVEN, DEVEN_IGD); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 193 | |
| 194 | sandybridge_setup_graphics(); |
Patrick Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 195 | |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 196 | /* |
| 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 Rudolph | e4f9d5c | 2015-10-15 11:09:15 +0200 | [diff] [blame] | 204 | start_peg_link_training(); |
Stefan Reinauer | 00636b0 | 2012-04-04 00:08:51 +0200 | [diff] [blame] | 205 | } |
Vladimir Serbinenko | c845b43 | 2014-09-05 03:37:44 +0200 | [diff] [blame] | 206 | |
| 207 | void northbridge_romstage_finalize(int s3resume) |
| 208 | { |
Angel Pons | 7c49cb8 | 2020-03-16 23:17:32 +0100 | [diff] [blame] | 209 | MCHBAR16(SSKPD_HI) = 0xCAFE; |
Vladimir Serbinenko | c845b43 | 2014-09-05 03:37:44 +0200 | [diff] [blame] | 210 | |
Aaron Durbin | 77e1399 | 2016-11-29 17:43:04 -0600 | [diff] [blame] | 211 | romstage_handoff_init(s3resume); |
Vladimir Serbinenko | c845b43 | 2014-09-05 03:37:44 +0200 | [diff] [blame] | 212 | } |