blob: 3549b492343feecbcb2389835a66dd487ca7b962 [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>
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020030
31#include "drivers/intel/gma/i915_reg.h"
32#include "chip.h"
33#include "gm45.h"
Vladimir Serbinenko88010112014-08-16 03:35:33 +020034
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020035static struct resource *gtt_res = NULL;
36
Nico Huberb851cc62016-01-09 23:27:16 +010037u32 gtt_read(u32 reg)
38{
39 return read32(res2mmio(gtt_res, reg, 0));
40}
41
Vladimir Serbinenko88010112014-08-16 03:35:33 +020042void gtt_write(u32 reg, u32 data)
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020043{
Kevin Paul Herbertbde6d302014-12-24 18:43:20 -080044 write32(res2mmio(gtt_res, reg, 0), data);
Vladimir Serbinenko6481e102014-08-10 23:48:11 +020045}
46
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020047uintptr_t gma_get_gnvs_aslb(const void *gnvs)
48{
49 const global_nvs_t *gnvs_ptr = gnvs;
50 return (uintptr_t)(gnvs_ptr ? gnvs_ptr->aslb : 0);
51}
52
53void gma_set_gnvs_aslb(void *gnvs, uintptr_t aslb)
54{
55 global_nvs_t *gnvs_ptr = gnvs;
56 if (gnvs_ptr)
57 gnvs_ptr->aslb = aslb;
58}
59
Nico Huberd85a71a2016-11-27 14:43:12 +010060static u32 get_cdclk(struct device *const dev)
61{
62 const u16 cdclk_sel =
63 pci_read_config16 (dev, GCFGC_OFFSET) & GCFGC_CD_MASK;
64 switch (MCHBAR8(HPLLVCO_MCHBAR) & 0x7) {
65 case VCO_2666:
66 case VCO_4000:
67 case VCO_5333:
68 return cdclk_sel ? 333333333 : 222222222;
69 case VCO_3200:
70 return cdclk_sel ? 320000000 : 228571429;
71 default:
72 printk(BIOS_WARNING,
73 "Unknown VCO frequency, using default cdclk.\n");
74 return 222222222;
75 }
76}
77
Arthur Heymans12bed262016-11-24 13:23:05 +010078static u32 freq_to_blc_pwm_ctl(struct device *const dev,
79 u16 pwm_freq, u8 duty_perc)
80{
81 u32 blc_mod;
82
83 blc_mod = get_cdclk(dev) / (128 * pwm_freq);
84
85 if (duty_perc <= 100)
86 return (blc_mod << 16) | (blc_mod * duty_perc / 100);
87 else
88 return (blc_mod << 16) | blc_mod;
89}
90
Arthur Heymans4d2d1712018-11-29 12:25:31 +010091u16 get_blc_pwm_freq_value(const char *edid_ascii_string)
Arthur Heymansc679b1f2018-11-29 12:21:12 +010092{
93 static u16 blc_pwm_freq;
94 const struct blc_pwm_t *blc_pwm;
95 int i;
96 int blc_array_len;
97
98 if (blc_pwm_freq > 0)
99 return blc_pwm_freq;
100
101 blc_array_len = get_blc_values(&blc_pwm);
102 /* Find EDID string and pwm freq in lookup table */
103 for (i = 0; i < blc_array_len; i++) {
104 if (!strcmp(blc_pwm[i].ascii_string, edid_ascii_string)) {
105 blc_pwm_freq = blc_pwm[i].pwm_freq;
106 printk(BIOS_DEBUG, "Found EDID string: %s in lookup table, pwm: %dHz\n",
107 blc_pwm[i].ascii_string, blc_pwm_freq);
108 break;
109 }
110 }
111
112 if (i == blc_array_len)
113 printk(BIOS_NOTICE, "Your panels EDID `%s` wasn't found in the"
114 "lookup table.\n You may have issues with your panels"
115 "backlight.\n If you want to help improving coreboot"
116 "please report: this EDID string\n and the result"
117 "of `intel_read read BLC_PWM_CTL`"
118 "(from intel-gpu-tools)\n while running vendor BIOS\n",
119 edid_ascii_string);
120
121 return blc_pwm_freq;
122}
123
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200124static void gma_pm_init_post_vbios(struct device *const dev,
125 const char *edid_ascii_string)
Nico Huberb851cc62016-01-09 23:27:16 +0100126{
127 const struct northbridge_intel_gm45_config *const conf = dev->chip_info;
128
129 u32 reg32;
Arthur Heymans12bed262016-11-24 13:23:05 +0100130 u8 reg8;
Arthur Heymansc679b1f2018-11-29 12:21:12 +0100131 u16 pwm_freq;
Nico Huberb851cc62016-01-09 23:27:16 +0100132
133 /* Setup Panel Power On Delays */
134 reg32 = gtt_read(PP_ON_DELAYS);
135 if (!reg32) {
136 reg32 = (conf->gpu_panel_power_up_delay & 0x1fff) << 16;
137 reg32 |= (conf->gpu_panel_power_backlight_on_delay & 0x1fff);
138 gtt_write(PP_ON_DELAYS, reg32);
139 }
140
141 /* Setup Panel Power Off Delays */
142 reg32 = gtt_read(PP_OFF_DELAYS);
143 if (!reg32) {
144 reg32 = (conf->gpu_panel_power_down_delay & 0x1fff) << 16;
145 reg32 |= (conf->gpu_panel_power_backlight_off_delay & 0x1fff);
146 gtt_write(PP_OFF_DELAYS, reg32);
147 }
148
149 /* Setup Panel Power Cycle Delay */
150 if (conf->gpu_panel_power_cycle_delay) {
Nico Huberd85a71a2016-11-27 14:43:12 +0100151 reg32 = (get_cdclk(dev) / 20000 - 1)
152 << PP_REFERENCE_DIVIDER_SHIFT;
Nico Huberb851cc62016-01-09 23:27:16 +0100153 reg32 |= conf->gpu_panel_power_cycle_delay & 0x1f;
154 gtt_write(PP_DIVISOR, reg32);
155 }
156
157 /* Enable Backlight */
158 gtt_write(BLC_PWM_CTL2, (1 << 31));
Arthur Heymans12bed262016-11-24 13:23:05 +0100159 reg8 = 100;
160 if (conf->duty_cycle != 0)
161 reg8 = conf->duty_cycle;
Arthur Heymansc679b1f2018-11-29 12:21:12 +0100162 pwm_freq = get_blc_pwm_freq_value(edid_ascii_string);
163 if (pwm_freq == 0 && conf->default_pwm_freq != 0)
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200164 pwm_freq = conf->default_pwm_freq;
165
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200166 if (pwm_freq == 0)
Nico Huberb851cc62016-01-09 23:27:16 +0100167 gtt_write(BLC_PWM_CTL, 0x06100610);
168 else
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200169 gtt_write(BLC_PWM_CTL, freq_to_blc_pwm_ctl(dev, pwm_freq,
170 reg8));
Nico Huberb851cc62016-01-09 23:27:16 +0100171}
172
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200173static void gma_func0_init(struct device *dev)
174{
175 u32 reg32;
Arthur Heymans53485d22017-04-30 08:29:54 +0200176 u8 *mmio;
177 u8 edid_data_lvds[128];
178 struct edid edid_lvds;
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100179 const struct northbridge_intel_gm45_config *const conf = dev->chip_info;
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200180
181 /* IGD needs to be Bus Master */
182 reg32 = pci_read_config32(dev, PCI_COMMAND);
183 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
184 pci_write_config32(dev, PCI_COMMAND, reg32);
185
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200186 gtt_res = find_resource(dev, PCI_BASE_ADDRESS_0);
Arthur Heymans53485d22017-04-30 08:29:54 +0200187 if (gtt_res == NULL)
188 return;
189 mmio = res2mmio(gtt_res, 0, 0);
Timothy Pearsone7f70902015-04-06 22:01:23 -0500190
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100191
Julius Wernercd49cce2019-03-05 16:53:33 -0800192 if (!CONFIG(MAINBOARD_USE_LIBGFXINIT)) {
Nico Huberee352cd2016-01-09 23:15:53 +0100193 /* PCI Init, will run VBIOS */
Arthur Heymans53485d22017-04-30 08:29:54 +0200194 printk(BIOS_DEBUG, "Initialising IGD using VBIOS\n");
Nico Huberee352cd2016-01-09 23:15:53 +0100195 pci_dev_init(dev);
Nico Huberb851cc62016-01-09 23:27:16 +0100196 }
197
Arthur Heymans53485d22017-04-30 08:29:54 +0200198 printk(BIOS_DEBUG, "LVDS EDID\n");
Arthur Heymans8da22862017-08-06 15:56:30 +0200199 intel_gmbus_read_edid(mmio + GMBUS0, GMBUS_PORT_PANEL, 0x50,
200 edid_data_lvds, sizeof(edid_data_lvds));
Arthur Heymans53485d22017-04-30 08:29:54 +0200201 intel_gmbus_stop(mmio + GMBUS0);
202 decode_edid(edid_data_lvds, sizeof(edid_data_lvds), &edid_lvds);
203
Nico Huberb851cc62016-01-09 23:27:16 +0100204 /* Post VBIOS init */
Arthur Heymans20cb85f2017-04-29 14:31:32 +0200205 gma_pm_init_post_vbios(dev, edid_lvds.ascii_string);
Nico Huberb851cc62016-01-09 23:27:16 +0100206
Julius Wernercd49cce2019-03-05 16:53:33 -0800207 if (CONFIG(MAINBOARD_USE_LIBGFXINIT)) {
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100208 int vga_disable = (pci_read_config16(dev, D0F0_GGC) & 2) >> 1;
209 if (vga_disable) {
210 printk(BIOS_INFO,
211 "IGD is not decoding legacy VGA MEM and IO: skipping NATIVE graphic init\n");
212 } else {
Arthur Heymanse6c8f7e2018-08-09 11:31:51 +0200213 int lightup_ok;
214 gma_gfxinit(&lightup_ok);
Arthur Heymansa6ce5d32019-01-06 01:33:07 +0100215 /* Linux relies on VBT for panel info. */
216 generate_fake_intel_oprom(&conf->gfx, dev, "$VBT CANTIGA");
Arthur Heymanse6c8f7e2018-08-09 11:31:51 +0200217 }
Nico Huberf2dd0492017-10-29 15:42:44 +0100218 }
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200219
220 intel_gma_restore_opregion();
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200221}
222
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100223const struct i915_gpu_controller_info *
224intel_gma_get_controller_info(void)
225{
Kyösti Mälkkic70eed12018-05-22 02:18:00 +0300226 struct device *dev = pcidev_on_root(0x2, 0);
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100227 if (!dev) {
228 return NULL;
229 }
230 struct northbridge_intel_gm45_config *chip = dev->chip_info;
231 return &chip->gfx;
232}
233
Elyes HAOUAS6dcdaaf2018-02-09 07:44:31 +0100234static void gma_ssdt(struct device *device)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100235{
236 const struct i915_gpu_controller_info *gfx = intel_gma_get_controller_info();
237 if (!gfx) {
238 return;
239 }
240
241 drivers_intel_gma_displays_ssdt_generate(gfx);
242}
243
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200244static unsigned long
245gma_write_acpi_tables(struct device *const dev,
246 unsigned long current,
247 struct acpi_rsdp *const rsdp)
248{
249 igd_opregion_t *opregion = (igd_opregion_t *)current;
250 global_nvs_t *gnvs;
251
252 if (intel_gma_init_igd_opregion(opregion) != CB_SUCCESS)
253 return current;
254
255 current += sizeof(igd_opregion_t);
256
257 /* GNVS has been already set up */
258 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
259 if (gnvs) {
260 /* IGD OpRegion Base Address */
261 gma_set_gnvs_aslb(gnvs, (uintptr_t)opregion);
262 } else {
263 printk(BIOS_ERR, "Error: GNVS table not found.\n");
264 }
265
266 current = acpi_align_current(current);
267 return current;
268}
269
270static const char *gma_acpi_name(const struct device *dev)
271{
272 return "GFX0";
273}
274
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200275static struct pci_operations gma_pci_ops = {
Subrata Banik4a0f0712019-03-20 14:29:47 +0530276 .set_subsystem = pci_dev_set_subsystem,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200277};
278
279static struct device_operations gma_func0_ops = {
280 .read_resources = pci_dev_read_resources,
281 .set_resources = pci_dev_set_resources,
282 .enable_resources = pci_dev_enable_resources,
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100283 .acpi_fill_ssdt_generator = gma_ssdt,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200284 .init = gma_func0_init,
285 .scan_bus = 0,
286 .enable = 0,
287 .ops_pci = &gma_pci_ops,
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200288 .acpi_name = gma_acpi_name,
289 .write_acpi_tables = gma_write_acpi_tables,
Vladimir Serbinenko6481e102014-08-10 23:48:11 +0200290};
291
292static const unsigned short pci_device_ids[] =
293{
294 0x2a42, 0
295};
296
297static const struct pci_driver gma __pci_driver = {
298 .ops = &gma_func0_ops,
299 .vendor = PCI_VENDOR_ID_INTEL,
300 .devices = pci_device_ids,
301};