blob: 6034ae301e930c381afa908e7d56ecd20054f516 [file] [log] [blame]
Angel Pons4b429832020-04-02 23:48:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Vladimir Serbinenko6481e102014-08-10 23:48:11 +02002
Kyösti Mälkki13f66502019-03-03 08:01:05 +02003#include <device/mmio.h>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +02004#include <console/console.h>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +02005#include <device/device.h>
6#include <device/pci.h>
7#include <device/pci_ids.h>
Elyes HAOUASbf0970e2019-03-21 11:10:03 +01008#include <drivers/intel/gma/edid.h>
9#include <drivers/intel/gma/opregion.h>
10#include <drivers/intel/gma/libgfxinit.h>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020011#include <string.h>
12#include <device/pci_ops.h>
Arthur Heymansc51522f2016-08-27 01:09:19 +020013#include <commonlib/helpers.h>
Elyes HAOUAS51401c32019-05-15 21:09:30 +020014#include <types.h>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020015
16#include "drivers/intel/gma/i915_reg.h"
17#include "chip.h"
18#include "gm45.h"
Vladimir Serbinenko88010112014-08-16 03:35:33 +020019
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020020static struct resource *gtt_res = NULL;
21
Nico Huberb851cc62016-01-09 23:27:16 +010022u32 gtt_read(u32 reg)
23{
24 return read32(res2mmio(gtt_res, reg, 0));
25}
26
Vladimir Serbinenko88010112014-08-16 03:35:33 +020027void gtt_write(u32 reg, u32 data)
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020028{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080029 write32(res2mmio(gtt_res, reg, 0), data);
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020030}
31
Nico Huberd85a71a2016-11-27 14:43:12 +010032static u32 get_cdclk(struct device *const dev)
33{
Angel Ponsb0535832020-06-08 11:46:58 +020034 const u16 cdclk_sel = pci_read_config16(dev, GCFGC_OFFSET) & GCFGC_CD_MASK;
35
Angel Pons3f1f8ef2021-03-27 13:52:43 +010036 switch (mchbar_read8(HPLLVCO_MCHBAR) & 0x7) {
Nico Huberd85a71a2016-11-27 14:43:12 +010037 case VCO_2666:
38 case VCO_4000:
39 case VCO_5333:
40 return cdclk_sel ? 333333333 : 222222222;
41 case VCO_3200:
42 return cdclk_sel ? 320000000 : 228571429;
43 default:
44 printk(BIOS_WARNING,
45 "Unknown VCO frequency, using default cdclk.\n");
46 return 222222222;
47 }
48}
49
Arthur Heymans12bed262016-11-24 13:23:05 +010050static u32 freq_to_blc_pwm_ctl(struct device *const dev,
51 u16 pwm_freq, u8 duty_perc)
52{
53 u32 blc_mod;
54
55 blc_mod = get_cdclk(dev) / (128 * pwm_freq);
56
57 if (duty_perc <= 100)
58 return (blc_mod << 16) | (blc_mod * duty_perc / 100);
59 else
60 return (blc_mod << 16) | blc_mod;
61}
62
Arthur Heymans4d2d1712018-11-29 12:25:31 +010063u16 get_blc_pwm_freq_value(const char *edid_ascii_string)
Arthur Heymansc679b1f2018-11-29 12:21:12 +010064{
65 static u16 blc_pwm_freq;
66 const struct blc_pwm_t *blc_pwm;
67 int i;
68 int blc_array_len;
69
Bill XIE9c3407b2023-04-04 10:59:13 +080070 /* Prevent null-deref on strcmp() below */
71 if (blc_pwm_freq > 0 || !edid_ascii_string)
Arthur Heymansc679b1f2018-11-29 12:21:12 +010072 return blc_pwm_freq;
73
74 blc_array_len = get_blc_values(&blc_pwm);
75 /* Find EDID string and pwm freq in lookup table */
76 for (i = 0; i < blc_array_len; i++) {
77 if (!strcmp(blc_pwm[i].ascii_string, edid_ascii_string)) {
78 blc_pwm_freq = blc_pwm[i].pwm_freq;
79 printk(BIOS_DEBUG, "Found EDID string: %s in lookup table, pwm: %dHz\n",
80 blc_pwm[i].ascii_string, blc_pwm_freq);
81 break;
82 }
83 }
84
85 if (i == blc_array_len)
86 printk(BIOS_NOTICE, "Your panels EDID `%s` wasn't found in the"
87 "lookup table.\n You may have issues with your panels"
88 "backlight.\n If you want to help improving coreboot"
89 "please report: this EDID string\n and the result"
90 "of `intel_read read BLC_PWM_CTL`"
91 "(from intel-gpu-tools)\n while running vendor BIOS\n",
92 edid_ascii_string);
93
94 return blc_pwm_freq;
95}
96
Arthur Heymans20cb85f2017-04-29 14:31:32 +020097static void gma_pm_init_post_vbios(struct device *const dev,
98 const char *edid_ascii_string)
Nico Huberb851cc62016-01-09 23:27:16 +010099{
100 const struct northbridge_intel_gm45_config *const conf = dev->chip_info;
101
102 u32 reg32;
Arthur Heymans12bed262016-11-24 13:23:05 +0100103 u8 reg8;
Arthur Heymansc679b1f2018-11-29 12:21:12 +0100104 u16 pwm_freq;
Nico Huberb851cc62016-01-09 23:27:16 +0100105
106 /* Setup Panel Power On Delays */
107 reg32 = gtt_read(PP_ON_DELAYS);
108 if (!reg32) {
109 reg32 = (conf->gpu_panel_power_up_delay & 0x1fff) << 16;
110 reg32 |= (conf->gpu_panel_power_backlight_on_delay & 0x1fff);
111 gtt_write(PP_ON_DELAYS, reg32);
112 }
113
114 /* Setup Panel Power Off Delays */
115 reg32 = gtt_read(PP_OFF_DELAYS);
116 if (!reg32) {
117 reg32 = (conf->gpu_panel_power_down_delay & 0x1fff) << 16;
118 reg32 |= (conf->gpu_panel_power_backlight_off_delay & 0x1fff);
119 gtt_write(PP_OFF_DELAYS, reg32);
120 }
121
122 /* Setup Panel Power Cycle Delay */
123 if (conf->gpu_panel_power_cycle_delay) {
Nico Huberd85a71a2016-11-27 14:43:12 +0100124 reg32 = (get_cdclk(dev) / 20000 - 1)
125 << PP_REFERENCE_DIVIDER_SHIFT;
Nico Huberb851cc62016-01-09 23:27:16 +0100126 reg32 |= conf->gpu_panel_power_cycle_delay & 0x1f;
127 gtt_write(PP_DIVISOR, reg32);
128 }
129
130 /* Enable Backlight */
131 gtt_write(BLC_PWM_CTL2, (1 << 31));
Arthur Heymans12bed262016-11-24 13:23:05 +0100132 reg8 = 100;
133 if (conf->duty_cycle != 0)
134 reg8 = conf->duty_cycle;
Arthur Heymansc679b1f2018-11-29 12:21:12 +0100135 pwm_freq = get_blc_pwm_freq_value(edid_ascii_string);
136 if (pwm_freq == 0 && conf->default_pwm_freq != 0)
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200137 pwm_freq = conf->default_pwm_freq;
138
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200139 if (pwm_freq == 0)
Nico Huberb851cc62016-01-09 23:27:16 +0100140 gtt_write(BLC_PWM_CTL, 0x06100610);
141 else
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200142 gtt_write(BLC_PWM_CTL, freq_to_blc_pwm_ctl(dev, pwm_freq,
143 reg8));
Nico Huberb851cc62016-01-09 23:27:16 +0100144}
145
Nico Hubere5888da2023-05-18 13:51:18 +0200146const char *gm45_get_lvds_edid_str(void)
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200147{
Arthur Heymans53485d22017-04-30 08:29:54 +0200148 u8 *mmio;
149 u8 edid_data_lvds[128];
150 struct edid edid_lvds;
Bill XIEf63cdcf2023-04-03 21:54:56 +0800151 static char edid_str[EDID_ASCII_STRING_LENGTH + 1];
152
153 if (edid_str[0])
154 return edid_str;
Nico Hubere5888da2023-05-18 13:51:18 +0200155 if (!gtt_res) {
156 printk(BIOS_ERR, "Never call %s() outside dev.init() context.\n", __func__);
Bill XIEf63cdcf2023-04-03 21:54:56 +0800157 return NULL;
Nico Hubere5888da2023-05-18 13:51:18 +0200158 }
Bill XIEf63cdcf2023-04-03 21:54:56 +0800159 mmio = res2mmio(gtt_res, 0, 0);
160
161 printk(BIOS_DEBUG, "LVDS EDID\n");
162 intel_gmbus_read_edid(mmio + GMBUS0, GMBUS_PORT_PANEL, 0x50,
163 edid_data_lvds, sizeof(edid_data_lvds));
164 intel_gmbus_stop(mmio + GMBUS0);
165
166 if (decode_edid(edid_data_lvds, sizeof(edid_data_lvds), &edid_lvds)
167 != EDID_CONFORMANT)
168 return NULL;
169 memcpy(edid_str, edid_lvds.ascii_string, sizeof(edid_str));
170 return edid_str;
171}
172
173static void gma_func0_init(struct device *dev)
174{
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100175 const struct northbridge_intel_gm45_config *const conf = dev->chip_info;
Bill XIEf63cdcf2023-04-03 21:54:56 +0800176 const char *edid_str;
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200177
Nico Hubere5888da2023-05-18 13:51:18 +0200178 /* Probe MMIO resource first. It's needed even for
179 intel_gma_init_igd_opregion() which may call back. */
180 gtt_res = probe_resource(dev, PCI_BASE_ADDRESS_0);
181 if (!gtt_res)
182 return;
183
Nico Huberf2a0be22020-04-26 17:01:25 +0200184 intel_gma_init_igd_opregion();
185
Nico Hubere5888da2023-05-18 13:51:18 +0200186 edid_str = gm45_get_lvds_edid_str();
Bill XIEf63cdcf2023-04-03 21:54:56 +0800187 if (!edid_str)
188 printk(BIOS_ERR, "Failed to obtain LVDS EDID string!\n");
189
Arthur Heymanscc0b4522022-05-06 17:04:41 +0200190 /*
191 * GTT base is at a 2M offset and is 2M big. If GTT is smaller than 2M
192 * cycles are simply not decoded which is fine.
193 */
194 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
Nico Hubere55825f2023-05-18 13:53:35 +0200195 memset(res2mmio(gtt_res, 2*MiB, 0), 0, 2*MiB);
Arthur Heymanscc0b4522022-05-06 17:04:41 +0200196
197 if (CONFIG(NO_GFX_INIT))
198 pci_and_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MASTER);
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100199
Julius Wernercd49cce2019-03-05 16:53:33 -0800200 if (!CONFIG(MAINBOARD_USE_LIBGFXINIT)) {
Nico Huberee352cd2016-01-09 23:15:53 +0100201 /* PCI Init, will run VBIOS */
Arthur Heymans53485d22017-04-30 08:29:54 +0200202 printk(BIOS_DEBUG, "Initialising IGD using VBIOS\n");
Nico Huberee352cd2016-01-09 23:15:53 +0100203 pci_dev_init(dev);
Nico Huberb851cc62016-01-09 23:27:16 +0100204 }
205
206 /* Post VBIOS init */
Bill XIEf63cdcf2023-04-03 21:54:56 +0800207 gma_pm_init_post_vbios(dev, edid_str);
Nico Huberb851cc62016-01-09 23:27:16 +0100208
Arthur Heymans29e53582019-10-12 17:39:31 +0200209 if (CONFIG(MAINBOARD_USE_LIBGFXINIT) && !acpi_is_wakeup_s3()) {
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100210 int vga_disable = (pci_read_config16(dev, D0F0_GGC) & 2) >> 1;
211 if (vga_disable) {
212 printk(BIOS_INFO,
213 "IGD is not decoding legacy VGA MEM and IO: skipping NATIVE graphic init\n");
214 } else {
Arthur Heymanse6c8f7e2018-08-09 11:31:51 +0200215 int lightup_ok;
216 gma_gfxinit(&lightup_ok);
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100217 /* Linux relies on VBT for panel info. */
218 generate_fake_intel_oprom(&conf->gfx, dev, "$VBT CANTIGA");
Arthur Heymanse6c8f7e2018-08-09 11:31:51 +0200219 }
Nico Huberf2dd0492017-10-29 15:42:44 +0100220 }
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200221}
222
Furquan Shaikh7536a392020-04-24 21:59:21 -0700223static void gma_generate_ssdt(const struct device *device)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100224{
Matt DeVilliere91883f2020-03-30 22:20:03 -0500225 const struct northbridge_intel_gm45_config *chip = device->chip_info;
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100226
Matt DeVilliere91883f2020-03-30 22:20:03 -0500227 drivers_intel_gma_displays_ssdt_generate(&chip->gfx);
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100228}
229
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200230static const char *gma_acpi_name(const struct device *dev)
231{
232 return "GFX0";
233}
234
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200235static struct device_operations gma_func0_ops = {
Matt DeVilliere91883f2020-03-30 22:20:03 -0500236 .read_resources = pci_dev_read_resources,
237 .set_resources = pci_dev_set_resources,
238 .enable_resources = pci_dev_enable_resources,
239 .acpi_fill_ssdt = gma_generate_ssdt,
240 .init = gma_func0_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200241 .ops_pci = &pci_dev_ops_pci,
Matt DeVilliere91883f2020-03-30 22:20:03 -0500242 .acpi_name = gma_acpi_name,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200243};
244
245static const unsigned short pci_device_ids[] =
246{
247 0x2a42, 0
248};
249
250static const struct pci_driver gma __pci_driver = {
251 .ops = &gma_func0_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100252 .vendor = PCI_VID_INTEL,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200253 .devices = pci_device_ids,
254};