blob: 1e6da69878b00e9f845f9b497584147b13cd7660 [file] [log] [blame]
Vladimir Serbinenko6481e102014-08-10 23:48:11 +02001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 Chromium OS Authors
5 * Copyright (C) 2013 Vladimir Serbinenko
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020015 */
16
Kyösti Mälkki13f66502019-03-03 08:01:05 +020017#include <device/mmio.h>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020018#include <console/console.h>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020019#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
Elyes HAOUASbf0970e2019-03-21 11:10:03 +010022#include <drivers/intel/gma/edid.h>
23#include <drivers/intel/gma/opregion.h>
24#include <drivers/intel/gma/libgfxinit.h>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020025#include <string.h>
26#include <device/pci_ops.h>
Arthur Heymansc51522f2016-08-27 01:09:19 +020027#include <commonlib/helpers.h>
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020028#include <cbmem.h>
29#include <southbridge/intel/i82801ix/nvs.h>
Elyes HAOUAS51401c32019-05-15 21:09:30 +020030#include <types.h>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020031
32#include "drivers/intel/gma/i915_reg.h"
33#include "chip.h"
34#include "gm45.h"
Vladimir Serbinenko88010112014-08-16 03:35:33 +020035
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020036static struct resource *gtt_res = NULL;
37
Nico Huberb851cc62016-01-09 23:27:16 +010038u32 gtt_read(u32 reg)
39{
40 return read32(res2mmio(gtt_res, reg, 0));
41}
42
Vladimir Serbinenko88010112014-08-16 03:35:33 +020043void gtt_write(u32 reg, u32 data)
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020044{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080045 write32(res2mmio(gtt_res, reg, 0), data);
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020046}
47
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020048uintptr_t gma_get_gnvs_aslb(const void *gnvs)
49{
50 const global_nvs_t *gnvs_ptr = gnvs;
51 return (uintptr_t)(gnvs_ptr ? gnvs_ptr->aslb : 0);
52}
53
54void gma_set_gnvs_aslb(void *gnvs, uintptr_t aslb)
55{
56 global_nvs_t *gnvs_ptr = gnvs;
57 if (gnvs_ptr)
58 gnvs_ptr->aslb = aslb;
59}
60
Nico Huberd85a71a2016-11-27 14:43:12 +010061static u32 get_cdclk(struct device *const dev)
62{
63 const u16 cdclk_sel =
64 pci_read_config16 (dev, GCFGC_OFFSET) & GCFGC_CD_MASK;
65 switch (MCHBAR8(HPLLVCO_MCHBAR) & 0x7) {
66 case VCO_2666:
67 case VCO_4000:
68 case VCO_5333:
69 return cdclk_sel ? 333333333 : 222222222;
70 case VCO_3200:
71 return cdclk_sel ? 320000000 : 228571429;
72 default:
73 printk(BIOS_WARNING,
74 "Unknown VCO frequency, using default cdclk.\n");
75 return 222222222;
76 }
77}
78
Arthur Heymans12bed262016-11-24 13:23:05 +010079static u32 freq_to_blc_pwm_ctl(struct device *const dev,
80 u16 pwm_freq, u8 duty_perc)
81{
82 u32 blc_mod;
83
84 blc_mod = get_cdclk(dev) / (128 * pwm_freq);
85
86 if (duty_perc <= 100)
87 return (blc_mod << 16) | (blc_mod * duty_perc / 100);
88 else
89 return (blc_mod << 16) | blc_mod;
90}
91
Arthur Heymans4d2d1712018-11-29 12:25:31 +010092u16 get_blc_pwm_freq_value(const char *edid_ascii_string)
Arthur Heymansc679b1f2018-11-29 12:21:12 +010093{
94 static u16 blc_pwm_freq;
95 const struct blc_pwm_t *blc_pwm;
96 int i;
97 int blc_array_len;
98
99 if (blc_pwm_freq > 0)
100 return blc_pwm_freq;
101
102 blc_array_len = get_blc_values(&blc_pwm);
103 /* Find EDID string and pwm freq in lookup table */
104 for (i = 0; i < blc_array_len; i++) {
105 if (!strcmp(blc_pwm[i].ascii_string, edid_ascii_string)) {
106 blc_pwm_freq = blc_pwm[i].pwm_freq;
107 printk(BIOS_DEBUG, "Found EDID string: %s in lookup table, pwm: %dHz\n",
108 blc_pwm[i].ascii_string, blc_pwm_freq);
109 break;
110 }
111 }
112
113 if (i == blc_array_len)
114 printk(BIOS_NOTICE, "Your panels EDID `%s` wasn't found in the"
115 "lookup table.\n You may have issues with your panels"
116 "backlight.\n If you want to help improving coreboot"
117 "please report: this EDID string\n and the result"
118 "of `intel_read read BLC_PWM_CTL`"
119 "(from intel-gpu-tools)\n while running vendor BIOS\n",
120 edid_ascii_string);
121
122 return blc_pwm_freq;
123}
124
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200125static void gma_pm_init_post_vbios(struct device *const dev,
126 const char *edid_ascii_string)
Nico Huberb851cc62016-01-09 23:27:16 +0100127{
128 const struct northbridge_intel_gm45_config *const conf = dev->chip_info;
129
130 u32 reg32;
Arthur Heymans12bed262016-11-24 13:23:05 +0100131 u8 reg8;
Arthur Heymansc679b1f2018-11-29 12:21:12 +0100132 u16 pwm_freq;
Nico Huberb851cc62016-01-09 23:27:16 +0100133
134 /* Setup Panel Power On Delays */
135 reg32 = gtt_read(PP_ON_DELAYS);
136 if (!reg32) {
137 reg32 = (conf->gpu_panel_power_up_delay & 0x1fff) << 16;
138 reg32 |= (conf->gpu_panel_power_backlight_on_delay & 0x1fff);
139 gtt_write(PP_ON_DELAYS, reg32);
140 }
141
142 /* Setup Panel Power Off Delays */
143 reg32 = gtt_read(PP_OFF_DELAYS);
144 if (!reg32) {
145 reg32 = (conf->gpu_panel_power_down_delay & 0x1fff) << 16;
146 reg32 |= (conf->gpu_panel_power_backlight_off_delay & 0x1fff);
147 gtt_write(PP_OFF_DELAYS, reg32);
148 }
149
150 /* Setup Panel Power Cycle Delay */
151 if (conf->gpu_panel_power_cycle_delay) {
Nico Huberd85a71a2016-11-27 14:43:12 +0100152 reg32 = (get_cdclk(dev) / 20000 - 1)
153 << PP_REFERENCE_DIVIDER_SHIFT;
Nico Huberb851cc62016-01-09 23:27:16 +0100154 reg32 |= conf->gpu_panel_power_cycle_delay & 0x1f;
155 gtt_write(PP_DIVISOR, reg32);
156 }
157
158 /* Enable Backlight */
159 gtt_write(BLC_PWM_CTL2, (1 << 31));
Arthur Heymans12bed262016-11-24 13:23:05 +0100160 reg8 = 100;
161 if (conf->duty_cycle != 0)
162 reg8 = conf->duty_cycle;
Arthur Heymansc679b1f2018-11-29 12:21:12 +0100163 pwm_freq = get_blc_pwm_freq_value(edid_ascii_string);
164 if (pwm_freq == 0 && conf->default_pwm_freq != 0)
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200165 pwm_freq = conf->default_pwm_freq;
166
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200167 if (pwm_freq == 0)
Nico Huberb851cc62016-01-09 23:27:16 +0100168 gtt_write(BLC_PWM_CTL, 0x06100610);
169 else
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200170 gtt_write(BLC_PWM_CTL, freq_to_blc_pwm_ctl(dev, pwm_freq,
171 reg8));
Nico Huberb851cc62016-01-09 23:27:16 +0100172}
173
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200174static void gma_func0_init(struct device *dev)
175{
176 u32 reg32;
Arthur Heymans53485d22017-04-30 08:29:54 +0200177 u8 *mmio;
178 u8 edid_data_lvds[128];
179 struct edid edid_lvds;
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100180 const struct northbridge_intel_gm45_config *const conf = dev->chip_info;
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200181
182 /* IGD needs to be Bus Master */
183 reg32 = pci_read_config32(dev, PCI_COMMAND);
184 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
185 pci_write_config32(dev, PCI_COMMAND, reg32);
186
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200187 gtt_res = find_resource(dev, PCI_BASE_ADDRESS_0);
Arthur Heymans53485d22017-04-30 08:29:54 +0200188 if (gtt_res == NULL)
189 return;
190 mmio = res2mmio(gtt_res, 0, 0);
Timothy Pearsone7f70902015-04-06 22:01:23 -0500191
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100192
Julius Wernercd49cce2019-03-05 16:53:33 -0800193 if (!CONFIG(MAINBOARD_USE_LIBGFXINIT)) {
Nico Huberee352cd2016-01-09 23:15:53 +0100194 /* PCI Init, will run VBIOS */
Arthur Heymans53485d22017-04-30 08:29:54 +0200195 printk(BIOS_DEBUG, "Initialising IGD using VBIOS\n");
Nico Huberee352cd2016-01-09 23:15:53 +0100196 pci_dev_init(dev);
Nico Huberb851cc62016-01-09 23:27:16 +0100197 }
198
Arthur Heymans53485d22017-04-30 08:29:54 +0200199 printk(BIOS_DEBUG, "LVDS EDID\n");
Arthur Heymans8da22862017-08-06 15:56:30 +0200200 intel_gmbus_read_edid(mmio + GMBUS0, GMBUS_PORT_PANEL, 0x50,
201 edid_data_lvds, sizeof(edid_data_lvds));
Arthur Heymans53485d22017-04-30 08:29:54 +0200202 intel_gmbus_stop(mmio + GMBUS0);
203 decode_edid(edid_data_lvds, sizeof(edid_data_lvds), &edid_lvds);
204
Nico Huberb851cc62016-01-09 23:27:16 +0100205 /* Post VBIOS init */
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200206 gma_pm_init_post_vbios(dev, edid_lvds.ascii_string);
Nico Huberb851cc62016-01-09 23:27:16 +0100207
Arthur Heymans29e53582019-10-12 17:39:31 +0200208 if (CONFIG(MAINBOARD_USE_LIBGFXINIT) && !acpi_is_wakeup_s3()) {
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100209 int vga_disable = (pci_read_config16(dev, D0F0_GGC) & 2) >> 1;
210 if (vga_disable) {
211 printk(BIOS_INFO,
212 "IGD is not decoding legacy VGA MEM and IO: skipping NATIVE graphic init\n");
213 } else {
Arthur Heymanse6c8f7e2018-08-09 11:31:51 +0200214 int lightup_ok;
215 gma_gfxinit(&lightup_ok);
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100216 /* Linux relies on VBT for panel info. */
217 generate_fake_intel_oprom(&conf->gfx, dev, "$VBT CANTIGA");
Arthur Heymanse6c8f7e2018-08-09 11:31:51 +0200218 }
Nico Huberf2dd0492017-10-29 15:42:44 +0100219 }
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200220
221 intel_gma_restore_opregion();
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200222}
223
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100224const struct i915_gpu_controller_info *
225intel_gma_get_controller_info(void)
226{
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300227 struct device *dev = pcidev_on_root(0x2, 0);
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100228 if (!dev) {
229 return NULL;
230 }
231 struct northbridge_intel_gm45_config *chip = dev->chip_info;
232 return &chip->gfx;
233}
234
Elyes HAOUAS6dcdaaf2018-02-09 07:44:31 +0100235static void gma_ssdt(struct device *device)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100236{
237 const struct i915_gpu_controller_info *gfx = intel_gma_get_controller_info();
238 if (!gfx) {
239 return;
240 }
241
242 drivers_intel_gma_displays_ssdt_generate(gfx);
243}
244
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200245static unsigned long
246gma_write_acpi_tables(struct device *const dev,
247 unsigned long current,
248 struct acpi_rsdp *const rsdp)
249{
250 igd_opregion_t *opregion = (igd_opregion_t *)current;
251 global_nvs_t *gnvs;
252
253 if (intel_gma_init_igd_opregion(opregion) != CB_SUCCESS)
254 return current;
255
256 current += sizeof(igd_opregion_t);
257
258 /* GNVS has been already set up */
259 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
260 if (gnvs) {
261 /* IGD OpRegion Base Address */
262 gma_set_gnvs_aslb(gnvs, (uintptr_t)opregion);
263 } else {
264 printk(BIOS_ERR, "Error: GNVS table not found.\n");
265 }
266
267 current = acpi_align_current(current);
268 return current;
269}
270
271static const char *gma_acpi_name(const struct device *dev)
272{
273 return "GFX0";
274}
275
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200276static struct pci_operations gma_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530277 .set_subsystem = pci_dev_set_subsystem,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200278};
279
280static struct device_operations gma_func0_ops = {
281 .read_resources = pci_dev_read_resources,
282 .set_resources = pci_dev_set_resources,
283 .enable_resources = pci_dev_enable_resources,
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100284 .acpi_fill_ssdt_generator = gma_ssdt,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200285 .init = gma_func0_init,
286 .scan_bus = 0,
287 .enable = 0,
288 .ops_pci = &gma_pci_ops,
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200289 .acpi_name = gma_acpi_name,
290 .write_acpi_tables = gma_write_acpi_tables,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200291};
292
293static const unsigned short pci_device_ids[] =
294{
295 0x2a42, 0
296};
297
298static const struct pci_driver gma __pci_driver = {
299 .ops = &gma_func0_ops,
300 .vendor = PCI_VENDOR_ID_INTEL,
301 .devices = pci_device_ids,
302};