blob: 804382224d5bc7f4c0e3538ea220bacfb34216ed [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
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200146static void gma_func0_init(struct device *dev)
147{
Arthur Heymans53485d22017-04-30 08:29:54 +0200148 u8 *mmio;
149 u8 edid_data_lvds[128];
150 struct edid edid_lvds;
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100151 const struct northbridge_intel_gm45_config *const conf = dev->chip_info;
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200152
Nico Huberf2a0be22020-04-26 17:01:25 +0200153 intel_gma_init_igd_opregion();
154
Angel Pons536d36a2021-11-03 13:13:00 +0100155 gtt_res = probe_resource(dev, PCI_BASE_ADDRESS_0);
Elyes Haouas5e6b0f02022-09-13 09:55:49 +0200156 if (!gtt_res)
Arthur Heymans53485d22017-04-30 08:29:54 +0200157 return;
158 mmio = res2mmio(gtt_res, 0, 0);
Timothy Pearsone7f70902015-04-06 22:01:23 -0500159
Arthur Heymanscc0b4522022-05-06 17:04:41 +0200160 /*
161 * GTT base is at a 2M offset and is 2M big. If GTT is smaller than 2M
162 * cycles are simply not decoded which is fine.
163 */
164 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
165 memset(mmio + 2 * MiB, 0, 2 * MiB);
166
167 if (CONFIG(NO_GFX_INIT))
168 pci_and_config16(dev, PCI_COMMAND, ~PCI_COMMAND_MASTER);
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100169
Julius Wernercd49cce2019-03-05 16:53:33 -0800170 if (!CONFIG(MAINBOARD_USE_LIBGFXINIT)) {
Nico Huberee352cd2016-01-09 23:15:53 +0100171 /* PCI Init, will run VBIOS */
Arthur Heymans53485d22017-04-30 08:29:54 +0200172 printk(BIOS_DEBUG, "Initialising IGD using VBIOS\n");
Nico Huberee352cd2016-01-09 23:15:53 +0100173 pci_dev_init(dev);
Nico Huberb851cc62016-01-09 23:27:16 +0100174 }
175
Arthur Heymans53485d22017-04-30 08:29:54 +0200176 printk(BIOS_DEBUG, "LVDS EDID\n");
Arthur Heymans8da22862017-08-06 15:56:30 +0200177 intel_gmbus_read_edid(mmio + GMBUS0, GMBUS_PORT_PANEL, 0x50,
178 edid_data_lvds, sizeof(edid_data_lvds));
Arthur Heymans53485d22017-04-30 08:29:54 +0200179 intel_gmbus_stop(mmio + GMBUS0);
180 decode_edid(edid_data_lvds, sizeof(edid_data_lvds), &edid_lvds);
181
Nico Huberb851cc62016-01-09 23:27:16 +0100182 /* Post VBIOS init */
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200183 gma_pm_init_post_vbios(dev, edid_lvds.ascii_string);
Nico Huberb851cc62016-01-09 23:27:16 +0100184
Arthur Heymans29e53582019-10-12 17:39:31 +0200185 if (CONFIG(MAINBOARD_USE_LIBGFXINIT) && !acpi_is_wakeup_s3()) {
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100186 int vga_disable = (pci_read_config16(dev, D0F0_GGC) & 2) >> 1;
187 if (vga_disable) {
188 printk(BIOS_INFO,
189 "IGD is not decoding legacy VGA MEM and IO: skipping NATIVE graphic init\n");
190 } else {
Arthur Heymanse6c8f7e2018-08-09 11:31:51 +0200191 int lightup_ok;
192 gma_gfxinit(&lightup_ok);
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100193 /* Linux relies on VBT for panel info. */
194 generate_fake_intel_oprom(&conf->gfx, dev, "$VBT CANTIGA");
Arthur Heymanse6c8f7e2018-08-09 11:31:51 +0200195 }
Nico Huberf2dd0492017-10-29 15:42:44 +0100196 }
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200197}
198
Furquan Shaikh7536a392020-04-24 21:59:21 -0700199static void gma_generate_ssdt(const struct device *device)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100200{
Matt DeVilliere91883f2020-03-30 22:20:03 -0500201 const struct northbridge_intel_gm45_config *chip = device->chip_info;
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100202
Matt DeVilliere91883f2020-03-30 22:20:03 -0500203 drivers_intel_gma_displays_ssdt_generate(&chip->gfx);
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100204}
205
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200206static const char *gma_acpi_name(const struct device *dev)
207{
208 return "GFX0";
209}
210
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200211static struct device_operations gma_func0_ops = {
Matt DeVilliere91883f2020-03-30 22:20:03 -0500212 .read_resources = pci_dev_read_resources,
213 .set_resources = pci_dev_set_resources,
214 .enable_resources = pci_dev_enable_resources,
215 .acpi_fill_ssdt = gma_generate_ssdt,
216 .init = gma_func0_init,
Angel Pons1fc0edd2020-05-31 00:03:28 +0200217 .ops_pci = &pci_dev_ops_pci,
Matt DeVilliere91883f2020-03-30 22:20:03 -0500218 .acpi_name = gma_acpi_name,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200219};
220
221static const unsigned short pci_device_ids[] =
222{
223 0x2a42, 0
224};
225
226static const struct pci_driver gma __pci_driver = {
227 .ops = &gma_func0_ops,
Felix Singer43b7f412022-03-07 04:34:52 +0100228 .vendor = PCI_VID_INTEL,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200229 .devices = pci_device_ids,
230};