blob: 8ce1a5d2e4be823684a1fec87aae53fb006242b7 [file] [log] [blame]
Damien Zammit43a1f782015-08-19 15:16:59 +10001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2011 Chromium OS Authors
5 * Copyright (C) 2013 Vladimir Serbinenko
6 * Copyright (C) 2015 Damien Zammit <damien@zamaudio.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18#include <arch/io.h>
19#include <console/console.h>
20#include <delay.h>
21#include <device/device.h>
22#include <device/pci.h>
23#include <device/pci_ids.h>
24#include <string.h>
25#include <device/pci_ops.h>
26#include <cpu/x86/msr.h>
27#include <cpu/x86/mtrr.h>
Arthur Heymansde14ea72016-09-04 16:01:11 +020028#include <commonlib/helpers.h>
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020029#include <cbmem.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100030
31#include "drivers/intel/gma/i915_reg.h"
32#include "chip.h"
33#include "x4x.h"
34#include <drivers/intel/gma/intel_bios.h>
Arthur Heymansde14ea72016-09-04 16:01:11 +020035#include <drivers/intel/gma/edid.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100036#include <drivers/intel/gma/i915.h>
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020037#include <drivers/intel/gma/opregion.h>
Damien Zammit43a1f782015-08-19 15:16:59 +100038#include <pc80/vga.h>
39#include <pc80/vga_io.h>
40
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020041#if IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801JX)
42#include <southbridge/intel/i82801jx/nvs.h>
43#elif IS_ENABLED(CONFIG_SOUTHBRIDGE_INTEL_I82801GX)
44#include <southbridge/intel/i82801gx/nvs.h>
45#endif
46
Arthur Heymansde14ea72016-09-04 16:01:11 +020047#define BASE_FREQUENCY 96000
48
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020049uintptr_t gma_get_gnvs_aslb(const void *gnvs)
50{
51 const global_nvs_t *gnvs_ptr = gnvs;
52 return (uintptr_t)(gnvs_ptr ? gnvs_ptr->aslb : 0);
53}
54
55void gma_set_gnvs_aslb(void *gnvs, uintptr_t aslb)
56{
57 global_nvs_t *gnvs_ptr = gnvs;
58 if (gnvs_ptr)
59 gnvs_ptr->aslb = aslb;
60}
61
Arthur Heymansde14ea72016-09-04 16:01:11 +020062static u8 edid_is_present(u8 *edid, u32 edid_size)
63{
64 u32 i;
65 for (i = 0; i < edid_size; i++) {
66 if (*(edid + i) != 0)
67 return 1;
68 }
69 return 0;
70}
Damien Zammit43a1f782015-08-19 15:16:59 +100071static void intel_gma_init(const struct northbridge_intel_x4x_config *info,
Arthur Heymansde14ea72016-09-04 16:01:11 +020072 u8 *mmio, u32 physbase, u16 piobase, u32 lfb)
Damien Zammit43a1f782015-08-19 15:16:59 +100073{
74
Arthur Heymansde14ea72016-09-04 16:01:11 +020075
Damien Zammit43a1f782015-08-19 15:16:59 +100076 int i;
Arthur Heymansde14ea72016-09-04 16:01:11 +020077 u8 edid_data[128];
78 struct edid edid;
79 struct edid_mode *mode;
80 u8 edid_is_found;
81
82 /* Initialise mode variables for 640 x 480 @ 60Hz */
83 u32 hactive = 640, vactive = 480;
84 u32 right_border = 0, bottom_border = 0;
85 int hpolarity = 0, vpolarity = 0;
86 u32 hsync = 96, vsync = 2;
87 u32 hblank = 160, vblank = 45;
88 u32 hfront_porch = 16, vfront_porch = 10;
89 u32 target_frequency = 25175;
90
91 u32 err_most = 0xffffffff;
92 u32 pixel_p1 = 1;
Arthur Heymans063cd5f2016-10-12 00:05:00 +020093 u32 pixel_p2;
Arthur Heymansde14ea72016-09-04 16:01:11 +020094 u32 pixel_n = 1;
95 u32 pixel_m1 = 1;
96 u32 pixel_m2 = 1;
Damien Zammit43a1f782015-08-19 15:16:59 +100097
Arthur Heymansc3cbe942017-08-06 16:00:18 +020098 u8 vga_gmbus = GMBUS_PORT_VGADDC;
99
100 if (IS_ENABLED(CONFIG_GFX_GMA_ANALOG_I2C_HDMI_B))
101 vga_gmbus = GMBUS_PORT_DPB;
102 else if (IS_ENABLED(CONFIG_GFX_GMA_ANALOG_I2C_HDMI_C))
103 vga_gmbus = GMBUS_PORT_DPC;
104 else if (IS_ENABLED(CONFIG_GFX_GMA_ANALOG_I2C_HDMI_D))
105 vga_gmbus = GMBUS_PORT_DPD;
106
Damien Zammit216fc502016-01-22 19:13:18 +1100107 vga_gr_write(0x18, 0);
Damien Zammit43a1f782015-08-19 15:16:59 +1000108
Arthur Heymansde14ea72016-09-04 16:01:11 +0200109 /* Set up GTT */
110 for (i = 0; i < 0x1000; i++) {
111 outl((i << 2) | 1, piobase);
112 outl(physbase + (i << 12) + 1, piobase + 4);
113 }
114
Damien Zammit43a1f782015-08-19 15:16:59 +1000115 write32(mmio + VGA0, 0x31108);
116 write32(mmio + VGA1, 0x31406);
117
118 write32(mmio + ADPA, ADPA_DAC_ENABLE
119 | ADPA_PIPE_A_SELECT
120 | ADPA_CRT_HOTPLUG_MONITOR_COLOR
121 | ADPA_CRT_HOTPLUG_ENABLE
122 | ADPA_USE_VGA_HVPOLARITY
123 | ADPA_VSYNC_CNTL_ENABLE
124 | ADPA_HSYNC_CNTL_ENABLE
125 | ADPA_DPMS_ON
126 );
127
128 write32(mmio + 0x7041c, 0x0);
129 write32(mmio + DPLL_MD(0), 0x3);
130 write32(mmio + DPLL_MD(1), 0x3);
131
132 vga_misc_write(0x67);
133
134 const u8 cr[] = { 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
135 0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x00, 0x00,
136 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
137 0xff
138 };
139 vga_cr_write(0x11, 0);
140
141 for (i = 0; i <= 0x18; i++)
142 vga_cr_write(i, cr[i]);
143
Arthur Heymansde14ea72016-09-04 16:01:11 +0200144 udelay(1);
145
Arthur Heymansc3cbe942017-08-06 16:00:18 +0200146 /*
147 * TODO: check if it is actually an analog display.
148 * No harm is done but the console output could be confusing.
149 */
150 intel_gmbus_read_edid(mmio + GMBUS0, vga_gmbus, 0x50, edid_data,
Arthur Heymans7141ff32016-10-10 17:49:00 +0200151 sizeof(edid_data));
Arthur Heymansde14ea72016-09-04 16:01:11 +0200152 intel_gmbus_stop(mmio + GMBUS0);
153 decode_edid(edid_data,
154 sizeof(edid_data), &edid);
155 mode = &edid.mode;
156
157
Damien Zammit43a1f782015-08-19 15:16:59 +1000158 /* Disable screen memory to prevent garbage from appearing. */
159 vga_sr_write(1, vga_sr_read(1) | 0x20);
160
Arthur Heymansde14ea72016-09-04 16:01:11 +0200161 edid_is_found = edid_is_present(edid_data, sizeof(edid_data));
162 if (edid_is_found) {
163 printk(BIOS_DEBUG, "EDID is not null");
164 hactive = edid.x_resolution;
165 vactive = edid.y_resolution;
166 right_border = mode->hborder;
167 bottom_border = mode->vborder;
168 hpolarity = (mode->phsync == '-');
169 vpolarity = (mode->pvsync == '-');
170 vsync = mode->vspw;
171 hsync = mode->hspw;
172 vblank = mode->vbl;
173 hblank = mode->hbl;
174 hfront_porch = mode->hso;
175 vfront_porch = mode->vso;
176 target_frequency = mode->pixel_clock;
177 } else
178 printk(BIOS_DEBUG, "EDID is null, using 640 x 480 @ 60Hz mode");
179
Nico Huber6d8266b2017-05-20 16:46:01 +0200180 if (IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER)) {
Arthur Heymansde14ea72016-09-04 16:01:11 +0200181 vga_sr_write(1, 1);
182 vga_sr_write(0x2, 0xf);
183 vga_sr_write(0x3, 0x0);
184 vga_sr_write(0x4, 0xe);
185 vga_gr_write(0, 0x0);
186 vga_gr_write(1, 0x0);
187 vga_gr_write(2, 0x0);
188 vga_gr_write(3, 0x0);
189 vga_gr_write(4, 0x0);
190 vga_gr_write(5, 0x0);
191 vga_gr_write(6, 0x5);
192 vga_gr_write(7, 0xf);
193 vga_gr_write(0x10, 0x1);
194 vga_gr_write(0x11, 0);
195
196 edid.bytes_per_line = (edid.bytes_per_line + 63) & ~63;
197
198 write32(mmio + DSPCNTR(0), DISPLAY_PLANE_ENABLE
199 | DISPPLANE_BGRX888);
200 write32(mmio + DSPADDR(0), 0);
201 write32(mmio + DSPSTRIDE(0), edid.bytes_per_line);
202 write32(mmio + DSPSURF(0), 0);
203 for (i = 0; i < 0x100; i++)
204 write32(mmio + LGC_PALETTE(0) + 4 * i, i * 0x010101);
205 } else {
206 vga_textmode_init();
207 }
208
Arthur Heymans063cd5f2016-10-12 00:05:00 +0200209 pixel_p2 = target_frequency <= 225000 ? 10 : 5;
210
Arthur Heymansde14ea72016-09-04 16:01:11 +0200211 u32 candn, candm1, candm2, candp1;
212 for (candn = 1; candn <= 4; candn++) {
213 for (candm1 = 23; candm1 >= 16; candm1--) {
214 for (candm2 = 11; candm2 >= 5; candm2--) {
215 for (candp1 = 8; candp1 >= 1; candp1--) {
216 u32 m = 5 * (candm1 + 2) + (candm2 + 2);
Arthur Heymans063cd5f2016-10-12 00:05:00 +0200217 u32 p = candp1 * pixel_p2;
Arthur Heymansde14ea72016-09-04 16:01:11 +0200218 u32 vco = DIV_ROUND_CLOSEST(
219 BASE_FREQUENCY * m, candn + 2);
220 u32 dot = DIV_ROUND_CLOSEST(vco, p);
Arthur Heymans75f91312016-10-12 01:04:28 +0200221 u32 this_err = MAX(dot, target_frequency) -
222 MIN(dot, target_frequency);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200223 if (this_err < err_most) {
224 err_most = this_err;
225 pixel_n = candn;
226 pixel_m1 = candm1;
227 pixel_m2 = candm2;
228 pixel_p1 = candp1;
229 }
230 }
231 }
232 }
233 }
234
235 if (err_most == 0xffffffff) {
236 printk(BIOS_ERR, "Couldn't find GFX clock divisors\n");
237 return;
238 }
239
Arthur Heymansde14ea72016-09-04 16:01:11 +0200240 printk(BIOS_INFO, "bringing up panel at resolution %d x %d\n",
241 hactive, vactive);
242 printk(BIOS_DEBUG, "Borders %d x %d\n",
243 right_border, bottom_border);
244 printk(BIOS_DEBUG, "Blank %d x %d\n",
245 hblank, vblank);
246 printk(BIOS_DEBUG, "Sync %d x %d\n",
247 hsync, vsync);
248 printk(BIOS_DEBUG, "Front porch %d x %d\n",
249 hfront_porch, vfront_porch);
250 printk(BIOS_DEBUG, (info->gfx.use_spread_spectrum_clock
251 ? "Spread spectrum clock\n" : "DREF clock\n"));
252 printk(BIOS_DEBUG, "Polarities %d, %d\n",
253 hpolarity, vpolarity);
Arthur Heymans063cd5f2016-10-12 00:05:00 +0200254 printk(BIOS_DEBUG, "Pixel N=%d, M1=%d, M2=%d, P1=%d, P2=%d\n",
255 pixel_n, pixel_m1, pixel_m2, pixel_p1, pixel_p2);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200256 printk(BIOS_DEBUG, "Pixel clock %d kHz\n",
257 BASE_FREQUENCY * (5 * (pixel_m1 + 2) + (pixel_m2 + 2)) /
Arthur Heymans063cd5f2016-10-12 00:05:00 +0200258 (pixel_n + 2) / (pixel_p1 * pixel_p2));
Damien Zammit43a1f782015-08-19 15:16:59 +1000259
Damien Zammit43a1f782015-08-19 15:16:59 +1000260 mdelay(1);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200261 write32(mmio + FP0(0), (pixel_n << 16)
262 | (pixel_m1 << 8) | pixel_m2);
263 write32(mmio + DPLL(0), DPLL_VCO_ENABLE
264 | DPLL_VGA_MODE_DIS | DPLLB_MODE_DAC_SERIAL
Arthur Heymans063cd5f2016-10-12 00:05:00 +0200265 | (pixel_p2 == 10 ? DPLL_DAC_SERIAL_P2_CLOCK_DIV_10 :
266 DPLL_DAC_SERIAL_P2_CLOCK_DIV_5)
Arthur Heymansde14ea72016-09-04 16:01:11 +0200267 | (0x10000 << (pixel_p1 - 1))
268 | (6 << 9));
269
Damien Zammit43a1f782015-08-19 15:16:59 +1000270 mdelay(1);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200271 write32(mmio + DPLL(0), DPLL_VCO_ENABLE
272 | DPLL_VGA_MODE_DIS | DPLLB_MODE_DAC_SERIAL
Arthur Heymans063cd5f2016-10-12 00:05:00 +0200273 | (pixel_p2 == 10 ? DPLL_DAC_SERIAL_P2_CLOCK_DIV_10 :
274 DPLL_DAC_SERIAL_P2_CLOCK_DIV_5)
Arthur Heymansde14ea72016-09-04 16:01:11 +0200275 | (0x10000 << (pixel_p1 - 1))
276 | (6 << 9));
Damien Zammit43a1f782015-08-19 15:16:59 +1000277
278 write32(mmio + ADPA, ADPA_DAC_ENABLE
279 | ADPA_PIPE_A_SELECT
280 | ADPA_CRT_HOTPLUG_MONITOR_COLOR
281 | ADPA_CRT_HOTPLUG_ENABLE
Damien Zammit43a1f782015-08-19 15:16:59 +1000282 | ADPA_VSYNC_CNTL_ENABLE
283 | ADPA_HSYNC_CNTL_ENABLE
284 | ADPA_DPMS_ON
Arthur Heymansde14ea72016-09-04 16:01:11 +0200285 | (vpolarity ? ADPA_VSYNC_ACTIVE_LOW :
286 ADPA_VSYNC_ACTIVE_HIGH)
287 | (hpolarity ? ADPA_HSYNC_ACTIVE_LOW :
288 ADPA_HSYNC_ACTIVE_HIGH));
Damien Zammit43a1f782015-08-19 15:16:59 +1000289
290 write32(mmio + HTOTAL(0),
Arthur Heymansde14ea72016-09-04 16:01:11 +0200291 ((hactive + right_border + hblank - 1) << 16)
Damien Zammit43a1f782015-08-19 15:16:59 +1000292 | (hactive - 1));
293 write32(mmio + HBLANK(0),
Arthur Heymansde14ea72016-09-04 16:01:11 +0200294 ((hactive + right_border + hblank - 1) << 16)
295 | (hactive + right_border - 1));
Damien Zammit43a1f782015-08-19 15:16:59 +1000296 write32(mmio + HSYNC(0),
Arthur Heymansde14ea72016-09-04 16:01:11 +0200297 ((hactive + right_border + hfront_porch + hsync - 1) << 16)
298 | (hactive + right_border + hfront_porch - 1));
Damien Zammit43a1f782015-08-19 15:16:59 +1000299
Arthur Heymansde14ea72016-09-04 16:01:11 +0200300 write32(mmio + VTOTAL(0), ((vactive + bottom_border + vblank - 1) << 16)
Damien Zammit43a1f782015-08-19 15:16:59 +1000301 | (vactive - 1));
Arthur Heymansde14ea72016-09-04 16:01:11 +0200302 write32(mmio + VBLANK(0), ((vactive + bottom_border + vblank - 1) << 16)
303 | (vactive + bottom_border - 1));
Damien Zammit43a1f782015-08-19 15:16:59 +1000304 write32(mmio + VSYNC(0),
Arthur Heymansde14ea72016-09-04 16:01:11 +0200305 ((vactive + bottom_border + vfront_porch + vsync - 1) << 16)
306 | (vactive + bottom_border + vfront_porch - 1));
Damien Zammit43a1f782015-08-19 15:16:59 +1000307
308 write32(mmio + PIPECONF(0), PIPECONF_DISABLE);
309
310 write32(mmio + PF_WIN_POS(0), 0);
Nico Huber6d8266b2017-05-20 16:46:01 +0200311 if (IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER)) {
Arthur Heymansde14ea72016-09-04 16:01:11 +0200312 write32(mmio + PIPESRC(0), ((hactive - 1) << 16)
313 | (vactive - 1));
314 write32(mmio + PF_CTL(0), 0);
315 write32(mmio + PF_WIN_SZ(0), 0);
316 write32(mmio + PFIT_CONTROL, 0);
317 } else {
318 write32(mmio + PIPESRC(0), (639 << 16) | 399);
319 write32(mmio + PF_CTL(0), PF_ENABLE | PF_FILTER_MED_3x3);
320 write32(mmio + PF_WIN_SZ(0), vactive | (hactive << 16));
321 write32(mmio + PFIT_CONTROL, 0x80000000);
322 }
Damien Zammit43a1f782015-08-19 15:16:59 +1000323
324 mdelay(1);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200325 write32(mmio + PIPECONF(0), PIPECONF_BPP_6);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200326 write32(mmio + PIPECONF(0), PIPECONF_BPP_6 | PIPECONF_DITHER_EN);
Damien Zammit216fc502016-01-22 19:13:18 +1100327 write32(mmio + PIPECONF(0), PIPECONF_ENABLE
328 | PIPECONF_BPP_6 | PIPECONF_DITHER_EN);
Damien Zammit43a1f782015-08-19 15:16:59 +1000329
Nico Huber6d8266b2017-05-20 16:46:01 +0200330 if (IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER)) {
Arthur Heymansde14ea72016-09-04 16:01:11 +0200331 write32(mmio + VGACNTRL, VGA_DISP_DISABLE);
332 write32(mmio + DSPCNTR(0), DISPLAY_PLANE_ENABLE
333 | DISPPLANE_BGRX888);
334 mdelay(1);
335 } else {
336 write32(mmio + VGACNTRL, 0xc4008e);
337 }
Damien Zammit43a1f782015-08-19 15:16:59 +1000338
339 write32(mmio + ADPA, ADPA_DAC_ENABLE
340 | ADPA_PIPE_A_SELECT
341 | ADPA_CRT_HOTPLUG_MONITOR_COLOR
342 | ADPA_CRT_HOTPLUG_ENABLE
Damien Zammit43a1f782015-08-19 15:16:59 +1000343 | ADPA_VSYNC_CNTL_ENABLE
344 | ADPA_HSYNC_CNTL_ENABLE
345 | ADPA_DPMS_ON
Arthur Heymansde14ea72016-09-04 16:01:11 +0200346 | (vpolarity ? ADPA_VSYNC_ACTIVE_LOW :
347 ADPA_VSYNC_ACTIVE_HIGH)
348 | (hpolarity ? ADPA_HSYNC_ACTIVE_LOW :
349 ADPA_HSYNC_ACTIVE_HIGH));
Damien Zammit43a1f782015-08-19 15:16:59 +1000350
Arthur Heymansde14ea72016-09-04 16:01:11 +0200351 write32(mmio + PP_CONTROL, PANEL_POWER_ON | PANEL_POWER_RESET);
Damien Zammit43a1f782015-08-19 15:16:59 +1000352
Arthur Heymansde14ea72016-09-04 16:01:11 +0200353 /* Enable screen memory. */
Damien Zammit43a1f782015-08-19 15:16:59 +1000354 vga_sr_write(1, vga_sr_read(1) & ~0x20);
355
356 /* Clear interrupts. */
357 write32(mmio + DEIIR, 0xffffffff);
358 write32(mmio + SDEIIR, 0xffffffff);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200359
Nico Huber6d8266b2017-05-20 16:46:01 +0200360 if (IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER)) {
Arthur Heymansde14ea72016-09-04 16:01:11 +0200361 memset((void *) lfb, 0,
362 hactive * vactive * 4);
363 set_vbe_mode_info_valid(&edid, lfb);
364 }
Damien Zammit43a1f782015-08-19 15:16:59 +1000365}
366
Damien Zammit216fc502016-01-22 19:13:18 +1100367static void native_init(struct device *dev)
368{
Arthur Heymansde14ea72016-09-04 16:01:11 +0200369 struct resource *lfb_res;
370 struct resource *pio_res;
371 u32 physbase;
Damien Zammit216fc502016-01-22 19:13:18 +1100372 struct resource *gtt_res = find_resource(dev, PCI_BASE_ADDRESS_0);
373 struct northbridge_intel_x4x_config *conf = dev->chip_info;
374
Arthur Heymansde14ea72016-09-04 16:01:11 +0200375 lfb_res = find_resource(dev, PCI_BASE_ADDRESS_2);
376 pio_res = find_resource(dev, PCI_BASE_ADDRESS_4);
377 physbase = pci_read_config32(dev, 0x5c) & ~0xf;
378
Damien Zammit216fc502016-01-22 19:13:18 +1100379 if (gtt_res && gtt_res->base) {
380 printk(BIOS_SPEW,
381 "Initializing VGA without OPROM. MMIO 0x%llx\n",
382 gtt_res->base);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200383 intel_gma_init(conf, res2mmio(gtt_res, 0, 0),
384 physbase, pio_res->base, lfb_res->base);
Damien Zammit216fc502016-01-22 19:13:18 +1100385 }
386
387 /* Linux relies on VBT for panel info. */
Arthur Heymansd3284a62016-09-25 22:48:00 +0200388 generate_fake_intel_oprom(&conf->gfx, dev, "$VBT EAGLELAKE");
Damien Zammit216fc502016-01-22 19:13:18 +1100389}
390
Damien Zammit43a1f782015-08-19 15:16:59 +1000391static void gma_func0_init(struct device *dev)
392{
Arthur Heymans2e7efe62017-05-06 18:05:57 +0200393 u16 reg16, ggc;
Damien Zammit43a1f782015-08-19 15:16:59 +1000394 u32 reg32;
395
396 /* IGD needs to be Bus Master */
397 reg32 = pci_read_config32(dev, PCI_COMMAND);
398 reg32 |= PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO;
399 pci_write_config32(dev, PCI_COMMAND, reg32);
400
Arthur Heymansde14ea72016-09-04 16:01:11 +0200401 /* configure GMBUSFREQ */
Arthur Heymans70a1dda2017-03-09 01:58:24 +0100402 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0x2, 0)), 0xcc);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200403 reg16 &= ~0x1ff;
404 reg16 |= 0xbc;
Arthur Heymans70a1dda2017-03-09 01:58:24 +0100405 pci_write_config16(dev_find_slot(0, PCI_DEVFN(0x2, 0)), 0xcc, reg16);
Arthur Heymansde14ea72016-09-04 16:01:11 +0200406
Arthur Heymans2e7efe62017-05-06 18:05:57 +0200407 ggc = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), D0F0_GGC);
408
409 if (IS_ENABLED(CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT)) {
410 if (ggc & (1 << 1)) {
411 printk(BIOS_DEBUG, "VGA cycles not assigned to IGD. "
412 "Not running native graphic init.\n");
413 return;
414 }
Damien Zammit216fc502016-01-22 19:13:18 +1100415 native_init(dev);
Arthur Heymans2e7efe62017-05-06 18:05:57 +0200416 } else {
Damien Zammit216fc502016-01-22 19:13:18 +1100417 pci_dev_init(dev);
Arthur Heymans2e7efe62017-05-06 18:05:57 +0200418 }
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200419
420 intel_gma_restore_opregion();
Damien Zammit43a1f782015-08-19 15:16:59 +1000421}
422
Arthur Heymansc80748c2017-02-26 23:04:51 +0100423static void gma_func0_disable(struct device *dev)
424{
425 struct device *dev_host = dev_find_slot(0, PCI_DEVFN(0, 0));
426 u16 ggc;
427
428 ggc = pci_read_config16(dev_host, D0F0_GGC);
429 ggc |= (1 << 1); /* VGA cycles to discrete GPU */
430 pci_write_config16(dev_host, D0F0_GGC, ggc);
431}
432
Elyes HAOUASfea02e12018-02-08 14:59:03 +0100433static void gma_set_subsystem(struct device *dev, unsigned int vendor,
Arthur Heymans70a1dda2017-03-09 01:58:24 +0100434 unsigned int device)
Damien Zammit43a1f782015-08-19 15:16:59 +1000435{
436 if (!vendor || !device) {
437 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
438 pci_read_config32(dev, PCI_VENDOR_ID));
439 } else {
440 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
441 ((device & 0xffff) << 16) | (vendor &
442 0xffff));
443 }
444}
445
446const struct i915_gpu_controller_info *
447intel_gma_get_controller_info(void)
448{
Elyes HAOUASfea02e12018-02-08 14:59:03 +0100449 struct device *dev = dev_find_slot(0, PCI_DEVFN(0x2, 0));
Arthur Heymans70a1dda2017-03-09 01:58:24 +0100450 if (!dev)
Damien Zammit43a1f782015-08-19 15:16:59 +1000451 return NULL;
Damien Zammit43a1f782015-08-19 15:16:59 +1000452 struct northbridge_intel_x4x_config *chip = dev->chip_info;
453 return &chip->gfx;
454}
455
Elyes HAOUASfea02e12018-02-08 14:59:03 +0100456static void gma_ssdt(struct device *device)
Damien Zammit43a1f782015-08-19 15:16:59 +1000457{
458 const struct i915_gpu_controller_info *gfx = intel_gma_get_controller_info();
Arthur Heymans70a1dda2017-03-09 01:58:24 +0100459 if (!gfx)
Damien Zammit43a1f782015-08-19 15:16:59 +1000460 return;
Damien Zammit43a1f782015-08-19 15:16:59 +1000461
462 drivers_intel_gma_displays_ssdt_generate(gfx);
463}
464
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200465static unsigned long
466gma_write_acpi_tables(struct device *const dev,
467 unsigned long current,
468 struct acpi_rsdp *const rsdp)
469{
470 igd_opregion_t *opregion = (igd_opregion_t *)current;
471 global_nvs_t *gnvs;
472
473 if (intel_gma_init_igd_opregion(opregion) != CB_SUCCESS)
474 return current;
475
476 current += sizeof(igd_opregion_t);
477
478 /* GNVS has been already set up */
479 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
480 if (gnvs) {
481 /* IGD OpRegion Base Address */
482 gma_set_gnvs_aslb(gnvs, (uintptr_t)opregion);
483 } else {
484 printk(BIOS_ERR, "Error: GNVS table not found.\n");
485 }
486
487 current = acpi_align_current(current);
488 return current;
489}
490
491static const char *gma_acpi_name(const struct device *dev)
492{
493 return "GFX0";
494}
495
Damien Zammit43a1f782015-08-19 15:16:59 +1000496static struct pci_operations gma_pci_ops = {
497 .set_subsystem = gma_set_subsystem,
498};
499
500static struct device_operations gma_func0_ops = {
501 .read_resources = pci_dev_read_resources,
502 .set_resources = pci_dev_set_resources,
503 .enable_resources = pci_dev_enable_resources,
504 .acpi_fill_ssdt_generator = gma_ssdt,
505 .init = gma_func0_init,
Damien Zammit43a1f782015-08-19 15:16:59 +1000506 .ops_pci = &gma_pci_ops,
Arthur Heymansc80748c2017-02-26 23:04:51 +0100507 .disable = gma_func0_disable,
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200508 .acpi_name = gma_acpi_name,
509 .write_acpi_tables = gma_write_acpi_tables,
Damien Zammit43a1f782015-08-19 15:16:59 +1000510};
511
Arthur Heymans70a1dda2017-03-09 01:58:24 +0100512static const unsigned short pci_device_ids[] = {
Arthur Heymans9e70ce02016-12-16 15:32:32 +0100513 0x2e02, /* Eaglelake */
514 0x2e12, /* Q43/Q45 */
515 0x2e22, /* G43/G45 */
516 0x2e32, /* G41 */
517 0x2e42, /* B43 */
518 0x2e92, /* B43_I */
519 0
Damien Zammit43a1f782015-08-19 15:16:59 +1000520};
521
522static const struct pci_driver gma __pci_driver = {
523 .ops = &gma_func0_ops,
524 .vendor = PCI_VENDOR_ID_INTEL,
525 .devices = pci_device_ids,
526};