blob: c0e1b717e0c45afa6c4c16ee963075c6c9a731a8 [file] [log] [blame]
Stefan Reinauer30140a52009-03-11 16:20:39 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2008-2009 coresystems GmbH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Stefan Reinauer30140a52009-03-11 16:20:39 +000014 */
15
16#include <console/console.h>
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +020017#include <bootmode.h>
Patrick Georgi6444bd42012-07-06 11:31:39 +020018#include <delay.h>
Stefan Reinauer30140a52009-03-11 16:20:39 +000019#include <device/device.h>
20#include <device/pci.h>
21#include <device/pci_ids.h>
Sven Schnelleb629d142011-06-12 14:30:10 +020022#include <pc80/mc146818rtc.h>
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020023#include <edid.h>
24#include <drivers/intel/gma/edid.h>
25#include <drivers/intel/gma/i915.h>
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020026#include <drivers/intel/gma/opregion.h>
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020027#include <string.h>
Vladimir Serbinenko0092c992014-08-21 01:06:53 +020028#include <pc80/vga.h>
29#include <pc80/vga_io.h>
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020030#include <commonlib/helpers.h>
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020031#include <cbmem.h>
32#include <southbridge/intel/i82801gx/nvs.h>
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020033
Patrick Georgice6e9fe2012-07-20 12:37:06 +020034#include "i945.h"
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020035#include "chip.h"
Stefan Reinauer30140a52009-03-11 16:20:39 +000036
Patrick Georgi6444bd42012-07-06 11:31:39 +020037#define GDRST 0xc0
Arthur Heymansc057a0612016-10-22 14:16:48 +020038#define MSAC 0x62 /* Multi Size Aperture Control */
Patrick Georgi6444bd42012-07-06 11:31:39 +020039
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020040#define LVDS_CLOCK_A_POWERUP_ALL (3 << 8)
41#define LVDS_CLOCK_B_POWERUP_ALL (3 << 4)
42#define LVDS_CLOCK_BOTH_POWERUP_ALL (3 << 2)
Elyes HAOUAS8868fc62017-06-28 20:41:53 +020043
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020044#define DPLLB_LVDS_P2_CLOCK_DIV_7 (1 << 24) /* i915 */
45
Elyes HAOUAS692e7df2017-06-28 20:44:41 +020046#define DPLL_INTEGRATED_CRI_CLK_VLV (1 << 14)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020047
48#define PGETBL_CTL 0x2020
49#define PGETBL_ENABLED 0x00000001
50
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020051#define BASE_FREQUENCY 100000
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020052
Arthur Heymans8e079002017-01-14 22:31:54 +010053#define DEFAULT_BLC_PWM 180
54
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +020055uintptr_t gma_get_gnvs_aslb(const void *gnvs)
56{
57 const global_nvs_t *gnvs_ptr = gnvs;
58 return (uintptr_t)(gnvs_ptr ? gnvs_ptr->aslb : 0);
59}
60
61void gma_set_gnvs_aslb(void *gnvs, uintptr_t aslb)
62{
63 global_nvs_t *gnvs_ptr = gnvs;
64 if (gnvs_ptr)
65 gnvs_ptr->aslb = aslb;
66}
67
Arthur Heymans85cfddb2017-02-06 13:47:21 +010068static int gtt_setup(u8 *mmiobase)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020069{
70 unsigned long PGETBL_save;
Paul Menzelcc95f182014-06-05 22:45:35 +020071 unsigned long tom; // top of memory
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020072
Paul Menzelcc95f182014-06-05 22:45:35 +020073 /*
74 * The Video BIOS places the GTT right below top of memory.
Denis 'GNUtoo' Carikli16110e72014-10-14 07:33:53 +020075 */
Paul Menzelcc95f182014-06-05 22:45:35 +020076 tom = pci_read_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), TOLUD) << 24;
77 PGETBL_save = tom - 256 * KiB;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020078 PGETBL_save |= PGETBL_ENABLED;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020079 PGETBL_save |= 2; /* set GTT to 256kb */
80
81 write32(mmiobase + GFX_FLSH_CNTL, 0);
82
83 write32(mmiobase + PGETBL_CTL, PGETBL_save);
84
85 /* verify */
86 if (read32(mmiobase + PGETBL_CTL) & PGETBL_ENABLED) {
87 printk(BIOS_DEBUG, "gtt_setup is enabled.\n");
88 } else {
89 printk(BIOS_DEBUG, "gtt_setup failed!!!\n");
90 return 1;
91 }
92 write32(mmiobase + GFX_FLSH_CNTL, 0);
93
94 return 0;
95}
96
Arthur Heymansb59bcb22016-09-05 22:46:11 +020097static int intel_gma_init_lvds(struct northbridge_intel_i945_config *conf,
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020098 unsigned int pphysbase, unsigned int piobase,
Arthur Heymans85cfddb2017-02-06 13:47:21 +010099 u8 *mmiobase, unsigned int pgfx)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200100{
101 struct edid edid;
Mono2e4f83b2015-09-07 21:15:26 +0200102 struct edid_mode *mode;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200103 u8 edid_data[128];
104 unsigned long temp;
105 int hpolarity, vpolarity;
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200106 u32 smallest_err = 0xffffffff;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200107 u32 target_frequency;
108 u32 pixel_p1 = 1;
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200109 u32 pixel_p2;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200110 u32 pixel_n = 1;
111 u32 pixel_m1 = 1;
112 u32 pixel_m2 = 1;
113 u32 hactive, vactive, right_border, bottom_border;
114 u32 vsync, hsync, vblank, hblank, hfront_porch, vfront_porch;
115 u32 i, j;
116 u32 uma_size;
117 u16 reg16;
118
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200119 printk(BIOS_SPEW,
Francis Rowe71512b22015-03-16 05:31:40 +0000120 "i915lightup: graphics %p mmio %p addrport %04x physbase %08x\n",
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100121 (void *)pgfx, mmiobase, piobase, pphysbase);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200122
Arthur Heymans8da22862017-08-06 15:56:30 +0200123 intel_gmbus_read_edid(mmiobase + GMBUS0, GMBUS_PORT_PANEL, 0x50,
124 edid_data, sizeof(edid_data));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200125 decode_edid(edid_data, sizeof(edid_data), &edid);
Mono2e4f83b2015-09-07 21:15:26 +0200126 mode = &edid.mode;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200127
Mono2e4f83b2015-09-07 21:15:26 +0200128 hpolarity = (mode->phsync == '-');
129 vpolarity = (mode->pvsync == '-');
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200130 hactive = edid.x_resolution;
131 vactive = edid.y_resolution;
Mono2e4f83b2015-09-07 21:15:26 +0200132 right_border = mode->hborder;
133 bottom_border = mode->vborder;
134 vblank = mode->vbl;
135 hblank = mode->hbl;
136 vsync = mode->vspw;
137 hsync = mode->hspw;
138 hfront_porch = mode->hso;
139 vfront_porch = mode->vso;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200140
141 for (i = 0; i < 2; i++)
142 for (j = 0; j < 0x100; j++)
Elyes HAOUAS0a15fe92016-09-17 19:12:27 +0200143 /* R = j, G = j, B = j. */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100144 write32(mmiobase + PALETTE(i) + 4 * j, 0x10101 * j);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200145
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100146 write32(mmiobase + PCH_PP_CONTROL, PANEL_UNLOCK_REGS
147 | (read32(mmiobase + PCH_PP_CONTROL) & ~PANEL_UNLOCK_MASK));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200148
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100149 write32(mmiobase + MI_ARB_STATE, MI_ARB_C3_LP_WRITE_ENABLE | (1 << 27));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200150 /* Clean registers. */
151 for (i = 0; i < 0x20; i += 4)
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100152 write32(mmiobase + RENDER_RING_BASE + i, 0);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200153 for (i = 0; i < 0x20; i += 4)
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100154 write32(mmiobase + FENCE_REG_965_0 + i, 0);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200155
156 /* Disable VGA. */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100157 write32(mmiobase + VGACNTRL, VGA_DISP_DISABLE);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200158
159 /* Disable pipes. */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100160 write32(mmiobase + PIPECONF(0), 0);
161 write32(mmiobase + PIPECONF(1), 0);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200162
163 /* Init PRB0. */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100164 write32(mmiobase + HWS_PGA, 0x352d2000);
165 write32(mmiobase + PRB0_CTL, 0);
166 write32(mmiobase + PRB0_HEAD, 0);
167 write32(mmiobase + PRB0_TAIL, 0);
168 write32(mmiobase + PRB0_START, 0);
169 write32(mmiobase + PRB0_CTL, 0x0001f001);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200170
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100171 write32(mmiobase + D_STATE, DSTATE_PLL_D3_OFF
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200172 | DSTATE_GFX_CLOCK_GATING | DSTATE_DOT_CLOCK_GATING);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100173 write32(mmiobase + ECOSKPD, 0x00010000);
174 write32(mmiobase + HWSTAM, 0xeffe);
175 write32(mmiobase + PORT_HOTPLUG_EN, conf->gpu_hotplug);
176 write32(mmiobase + INSTPM, 0x08000000 | INSTPM_AGPBUSY_DIS);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200177
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200178 /* p2 divisor must 7 for dual channel LVDS */
179 /* and 14 for single channel LVDS */
180 pixel_p2 = mode->lvds_dual_channel ? 7 : 14;
181 target_frequency = mode->pixel_clock;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200182
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200183 /* Find suitable divisors, m1, m2, p1, n. */
184 /* refclock * (5 * (m1 + 2) + (m1 + 2)) / (n + 2) / p1 / p2 */
185 /* should be closest to target frequency as possible */
186 u32 candn, candm1, candm2, candp1;
187 for (candm1 = 8; candm1 <= 18; candm1++) {
188 for (candm2 = 3; candm2 <= 7; candm2++) {
189 for (candn = 1; candn <= 6; candn++) {
190 for (candp1 = 1; candp1 <= 8; candp1++) {
191 u32 m = 5 * (candm1 + 2) + (candm2 + 2);
192 u32 p = candp1 * pixel_p2;
193 u32 vco = DIV_ROUND_CLOSEST(BASE_FREQUENCY * m, candn + 2);
194 u32 dot = DIV_ROUND_CLOSEST(vco, p);
Arthur Heymans75f91312016-10-12 01:04:28 +0200195 u32 this_err = MAX(dot, target_frequency) -
196 MIN(dot, target_frequency);
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200197 if ((m < 70) || (m > 120))
198 continue;
199 if (this_err < smallest_err) {
200 smallest_err = this_err;
201 pixel_n = candn;
202 pixel_m1 = candm1;
203 pixel_m2 = candm2;
204 pixel_p1 = candp1;
205 }
206 }
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200207 }
208 }
209 }
210
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200211 if (smallest_err == 0xffffffff) {
Arthur Heymans70a8e342017-03-09 11:30:23 +0100212 printk(BIOS_ERR, "Couldn't find GFX clock divisors\n");
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200213 return -1;
214 }
215
216 printk(BIOS_INFO, "bringing up panel at resolution %d x %d\n",
217 hactive, vactive);
218 printk(BIOS_DEBUG, "Borders %d x %d\n", right_border, bottom_border);
219 printk(BIOS_DEBUG, "Blank %d x %d\n", hblank, vblank);
220 printk(BIOS_DEBUG, "Sync %d x %d\n", hsync, vsync);
221 printk(BIOS_DEBUG, "Front porch %d x %d\n", hfront_porch, vfront_porch);
222 printk(BIOS_DEBUG, (conf->gpu_lvds_use_spread_spectrum_clock
223 ? "Spread spectrum clock\n"
224 : "DREF clock\n"));
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200225 printk(BIOS_DEBUG, (mode->lvds_dual_channel
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200226 ? "Dual channel\n"
227 : "Single channel\n"));
228 printk(BIOS_DEBUG, "Polarities %d, %d\n",
229 hpolarity, vpolarity);
230 printk(BIOS_DEBUG, "Pixel N=%d, M1=%d, M2=%d, P1=%d\n",
231 pixel_n, pixel_m1, pixel_m2, pixel_p1);
232 printk(BIOS_DEBUG, "Pixel clock %d kHz\n",
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200233 BASE_FREQUENCY * (5 * (pixel_m1 + 2) + (pixel_m2 + 2)) /
234 (pixel_n + 2) / (pixel_p1 * pixel_p2));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200235
Paul Menzelbcf9a0a2018-02-18 10:05:53 +0100236 printk(BIOS_INFO, "VGA mode: %s\n", IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER) ?
237 "Linear framebuffer" : "text");
Nico Huber6d8266b2017-05-20 16:46:01 +0200238 if (IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER)) {
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200239 /* Disable panel fitter (we're in native resolution). */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100240 write32(mmiobase + PF_CTL(0), 0);
241 write32(mmiobase + PF_WIN_SZ(0), 0);
242 write32(mmiobase + PF_WIN_POS(0), 0);
243 write32(mmiobase + PFIT_PGM_RATIOS, 0);
244 write32(mmiobase + PFIT_CONTROL, 0);
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200245 } else {
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100246 write32(mmiobase + PF_WIN_SZ(0), vactive | (hactive << 16));
247 write32(mmiobase + PF_WIN_POS(0), 0);
248 write32(mmiobase + PF_CTL(0), PF_ENABLE | PF_FILTER_MED_3x3);
249 write32(mmiobase + PFIT_CONTROL, PFIT_ENABLE
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200250 | (1 << PFIT_PIPE_SHIFT) | HORIZ_AUTO_SCALE
251 | VERT_AUTO_SCALE);
252 }
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200253
254 mdelay(1);
255
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100256 write32(mmiobase + DSPCNTR(0), DISPPLANE_BGRX888
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200257 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
258
259 mdelay(1);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100260 write32(mmiobase + PP_CONTROL, PANEL_UNLOCK_REGS
261 | (read32(mmiobase + PP_CONTROL) & ~PANEL_UNLOCK_MASK));
262 write32(mmiobase + FP0(1),
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200263 (pixel_n << 16)
264 | (pixel_m1 << 8) | pixel_m2);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100265 write32(mmiobase + DPLL(1),
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200266 DPLL_VGA_MODE_DIS |
267 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200268 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200269 : DPLLB_LVDS_P2_CLOCK_DIV_14)
270 | (conf->gpu_lvds_use_spread_spectrum_clock
271 ? DPLL_INTEGRATED_CLOCK_VLV | DPLL_INTEGRATED_CRI_CLK_VLV
272 : 0)
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200273 | (0x10000 << (pixel_p1 - 1)));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200274 mdelay(1);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100275 write32(mmiobase + DPLL(1),
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200276 DPLL_VGA_MODE_DIS |
277 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200278 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200279 : DPLLB_LVDS_P2_CLOCK_DIV_14)
280 | ((conf->gpu_lvds_use_spread_spectrum_clock ? 3 : 0) << 13)
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200281 | (0x10000 << (pixel_p1 - 1)));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200282 mdelay(1);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100283 write32(mmiobase + HTOTAL(1),
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200284 ((hactive + right_border + hblank - 1) << 16)
285 | (hactive - 1));
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100286 write32(mmiobase + HBLANK(1),
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200287 ((hactive + right_border + hblank - 1) << 16)
288 | (hactive + right_border - 1));
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100289 write32(mmiobase + HSYNC(1),
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200290 ((hactive + right_border + hfront_porch + hsync - 1) << 16)
291 | (hactive + right_border + hfront_porch - 1));
292
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100293 write32(mmiobase + VTOTAL(1), ((vactive + bottom_border + vblank - 1) << 16)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200294 | (vactive - 1));
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100295 write32(mmiobase + VBLANK(1), ((vactive + bottom_border + vblank - 1) << 16)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200296 | (vactive + bottom_border - 1));
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100297 write32(mmiobase + VSYNC(1),
Arthur Heymansc8c73a62016-10-13 14:12:45 +0200298 ((vactive + bottom_border + vfront_porch + vsync - 1) << 16)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200299 | (vactive + bottom_border + vfront_porch - 1));
300
Nico Huber6d8266b2017-05-20 16:46:01 +0200301 if (IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER)) {
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100302 write32(mmiobase + PIPESRC(1), ((hactive - 1) << 16)
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200303 | (vactive - 1));
304 } else {
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100305 write32(mmiobase + PIPESRC(1), (639 << 16) | 399);
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200306 }
307
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200308 mdelay(1);
309
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100310 write32(mmiobase + DSPSIZE(0), (hactive - 1) | ((vactive - 1) << 16));
311 write32(mmiobase + DSPPOS(0), 0);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200312
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200313 edid.bytes_per_line = (edid.bytes_per_line + 63) & ~63;
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100314 write32(mmiobase + DSPADDR(0), 0);
315 write32(mmiobase + DSPSURF(0), 0);
316 write32(mmiobase + DSPSTRIDE(0), edid.bytes_per_line);
317 write32(mmiobase + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200318 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
319 mdelay(1);
320
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100321 write32(mmiobase + PIPECONF(1), PIPECONF_ENABLE);
322 write32(mmiobase + LVDS, LVDS_ON
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200323 | (hpolarity << 20) | (vpolarity << 21)
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200324 | (mode->lvds_dual_channel ? LVDS_CLOCK_B_POWERUP_ALL
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200325 | LVDS_CLOCK_BOTH_POWERUP_ALL : 0)
326 | LVDS_CLOCK_A_POWERUP_ALL
327 | LVDS_PIPE(1));
328
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100329 write32(mmiobase + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
330 write32(mmiobase + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_RESET);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200331 mdelay(1);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100332 write32(mmiobase + PP_CONTROL, PANEL_UNLOCK_REGS
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200333 | PANEL_POWER_ON | PANEL_POWER_RESET);
334
Arthur Heymans70a8e342017-03-09 11:30:23 +0100335 printk(BIOS_DEBUG, "waiting for panel powerup\n");
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200336 while (1) {
337 u32 reg32;
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100338 reg32 = read32(mmiobase + PP_STATUS);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200339 if ((reg32 & PP_SEQUENCE_MASK) == PP_SEQUENCE_NONE)
340 break;
341 }
Arthur Heymans70a8e342017-03-09 11:30:23 +0100342 printk(BIOS_DEBUG, "panel powered up\n");
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200343
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100344 write32(mmiobase + PP_CONTROL, PANEL_POWER_ON | PANEL_POWER_RESET);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200345
346 /* Clear interrupts. */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100347 write32(mmiobase + DEIIR, 0xffffffff);
348 write32(mmiobase + SDEIIR, 0xffffffff);
349 write32(mmiobase + IIR, 0xffffffff);
350 write32(mmiobase + IMR, 0xffffffff);
351 write32(mmiobase + EIR, 0xffffffff);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200352
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100353 if (gtt_setup(mmiobase)) {
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200354 printk(BIOS_ERR, "ERROR: GTT Setup Failed!!!\n");
355 return 0;
356 }
357
358 /* Setup GTT. */
359
360 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
361 uma_size = 0;
362 if (!(reg16 & 2)) {
Arthur Heymans874a8f92016-05-19 16:06:09 +0200363 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200364 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
365 }
366
Arthur Heymans70a8e342017-03-09 11:30:23 +0100367 for (i = 0; i < (uma_size - 256) / 4; i++) {
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200368 outl((i << 2) | 1, piobase);
369 outl(pphysbase + (i << 12) + 1, piobase + 4);
370 }
371
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100372 temp = read32(mmiobase + PGETBL_CTL);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200373 printk(BIOS_INFO, "GTT PGETBL_CTL register: 0x%lx\n", temp);
374
375 if (temp & 1)
376 printk(BIOS_INFO, "GTT Enabled\n");
377 else
378 printk(BIOS_ERR, "ERROR: GTT is still Disabled!!!\n");
379
Nico Huber6d8266b2017-05-20 16:46:01 +0200380 if (IS_ENABLED(CONFIG_LINEAR_FRAMEBUFFER)) {
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200381 printk(BIOS_SPEW, "memset %p to 0x00 for %d bytes\n",
382 (void *)pgfx, hactive * vactive * 4);
383 memset((void *)pgfx, 0x00, hactive * vactive * 4);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200384
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200385 set_vbe_mode_info_valid(&edid, pgfx);
386 } else {
387 vga_misc_write(0x67);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200388
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100389 write32(mmiobase + DSPCNTR(0), DISPPLANE_SEL_PIPE_B);
390 write32(mmiobase + VGACNTRL, 0x02c4008e
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200391 | VGA_PIPE_B_SELECT);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200392
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200393 vga_textmode_init();
394 }
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200395 return 0;
396}
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200397
398static int intel_gma_init_vga(struct northbridge_intel_i945_config *conf,
399 unsigned int pphysbase, unsigned int piobase,
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100400 u8 *mmiobase, unsigned int pgfx)
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200401{
402 int i;
403 u32 hactive, vactive;
404 u16 reg16;
405 u32 uma_size;
406
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100407 printk(BIOS_SPEW, "mmiobase %x addrport %x physbase %x\n",
408 (u32)mmiobase, piobase, pphysbase);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200409
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100410 gtt_setup(mmiobase);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200411
412 /* Disable VGA. */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100413 write32(mmiobase + VGACNTRL, VGA_DISP_DISABLE);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200414
415 /* Disable pipes. */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100416 write32(mmiobase + PIPECONF(0), 0);
417 write32(mmiobase + PIPECONF(1), 0);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200418
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100419 write32(mmiobase + INSTPM, 0x800);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200420
421 vga_gr_write(0x18, 0);
422
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100423 write32(mmiobase + VGA0, 0x200074);
424 write32(mmiobase + VGA1, 0x200074);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200425
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100426 write32(mmiobase + DSPFW3, 0x7f3f00c1 & ~PINEVIEW_SELF_REFRESH_EN);
427 write32(mmiobase + DSPCLK_GATE_D, 0);
428 write32(mmiobase + FW_BLC, 0x03060106);
429 write32(mmiobase + FW_BLC2, 0x00000306);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200430
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100431 write32(mmiobase + ADPA, ADPA_DAC_ENABLE
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200432 | ADPA_PIPE_A_SELECT
433 | ADPA_USE_VGA_HVPOLARITY
434 | ADPA_VSYNC_CNTL_ENABLE
435 | ADPA_HSYNC_CNTL_ENABLE
436 | ADPA_DPMS_ON
437 );
438
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100439 write32(mmiobase + 0x7041c, 0x0);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200440
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100441 write32(mmiobase + DPLL_MD(0), 0x3);
442 write32(mmiobase + DPLL_MD(1), 0x3);
443 write32(mmiobase + DSPCNTR(1), 0x1000000);
444 write32(mmiobase + PIPESRC(1), 0x027f01df);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200445
446 vga_misc_write(0x67);
447 const u8 cr[] = { 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
448 0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x00, 0x00,
449 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
450 0xff
451 };
452 vga_cr_write(0x11, 0);
453
454 for (i = 0; i <= 0x18; i++)
455 vga_cr_write(i, cr[i]);
456
457 // Disable screen memory to prevent garbage from appearing.
458 vga_sr_write(1, vga_sr_read(1) | 0x20);
459 hactive = 640;
460 vactive = 400;
461
462 mdelay(1);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100463 write32(mmiobase + DPLL(0),
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200464 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
465 | DPLL_VGA_MODE_DIS
466 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
467 | 0x400601
468 );
469 mdelay(1);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100470 write32(mmiobase + DPLL(0),
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200471 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
472 | DPLL_VGA_MODE_DIS
473 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
474 | 0x400601
475 );
476
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100477 write32(mmiobase + ADPA, ADPA_DAC_ENABLE
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200478 | ADPA_PIPE_A_SELECT
479 | ADPA_USE_VGA_HVPOLARITY
480 | ADPA_VSYNC_CNTL_ENABLE
481 | ADPA_HSYNC_CNTL_ENABLE
482 | ADPA_DPMS_ON
483 );
484
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100485 write32(mmiobase + HTOTAL(0),
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200486 ((hactive - 1) << 16)
487 | (hactive - 1));
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100488 write32(mmiobase + HBLANK(0),
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200489 ((hactive - 1) << 16)
490 | (hactive - 1));
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100491 write32(mmiobase + HSYNC(0),
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200492 ((hactive - 1) << 16)
493 | (hactive - 1));
494
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100495 write32(mmiobase + VTOTAL(0), ((vactive - 1) << 16)
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200496 | (vactive - 1));
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100497 write32(mmiobase + VBLANK(0), ((vactive - 1) << 16)
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200498 | (vactive - 1));
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100499 write32(mmiobase + VSYNC(0),
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200500 ((vactive - 1) << 16)
501 | (vactive - 1));
502
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100503 write32(mmiobase + PF_WIN_POS(0), 0);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200504
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100505 write32(mmiobase + PIPESRC(0), (639 << 16) | 399);
Arthur Heymans70a8e342017-03-09 11:30:23 +0100506 write32(mmiobase + PF_CTL(0), PF_ENABLE | PF_FILTER_MED_3x3);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100507 write32(mmiobase + PF_WIN_SZ(0), vactive | (hactive << 16));
508 write32(mmiobase + PFIT_CONTROL, 0x0);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200509
510 mdelay(1);
511
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100512 write32(mmiobase + FDI_RX_CTL(0), 0x00002040);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200513 mdelay(1);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100514 write32(mmiobase + FDI_RX_CTL(0), 0x80002050);
515 write32(mmiobase + FDI_TX_CTL(0), 0x00044000);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200516 mdelay(1);
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100517 write32(mmiobase + FDI_TX_CTL(0), 0x80044000);
518 write32(mmiobase + PIPECONF(0), PIPECONF_ENABLE | PIPECONF_BPP_6 | PIPECONF_DITHER_EN);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200519
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100520 write32(mmiobase + VGACNTRL, 0x0);
521 write32(mmiobase + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200522 mdelay(1);
523
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100524 write32(mmiobase + ADPA, ADPA_DAC_ENABLE
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200525 | ADPA_PIPE_A_SELECT
526 | ADPA_USE_VGA_HVPOLARITY
527 | ADPA_VSYNC_CNTL_ENABLE
528 | ADPA_HSYNC_CNTL_ENABLE
529 | ADPA_DPMS_ON
530 );
531
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100532 write32(mmiobase + DSPFW3, 0x7f3f00c1);
533 write32(mmiobase + MI_MODE, 0x200 | VS_TIMER_DISPATCH);
534 write32(mmiobase + CACHE_MODE_0, (0x6820 | (1 << 9)) & ~(1 << 5));
535 write32(mmiobase + CACHE_MODE_1, 0x380 & ~(1 << 9));
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200536
537 /* Set up GTT. */
538
539 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
540 uma_size = 0;
541 if (!(reg16 & 2)) {
542 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
543 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
544 }
545
Arthur Heymans70a8e342017-03-09 11:30:23 +0100546 for (i = 0; i < (uma_size - 256) / 4; i++) {
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200547 outl((i << 2) | 1, piobase);
548 outl(pphysbase + (i << 12) + 1, piobase + 4);
549 }
550
551 /* Clear interrupts. */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100552 write32(mmiobase + DEIIR, 0xffffffff);
553 write32(mmiobase + SDEIIR, 0xffffffff);
554 write32(mmiobase + IIR, 0xffffffff);
555 write32(mmiobase + IMR, 0xffffffff);
556 write32(mmiobase + EIR, 0xffffffff);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200557
558 vga_textmode_init();
559
560 /* Enable screen memory. */
561 vga_sr_write(1, vga_sr_read(1) & ~0x20);
562
563 return 0;
564
565}
566
567/* compare the header of the vga edid header */
568/* if vga is not connected it should have a correct header */
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100569static int probe_edid(u8 *mmiobase, u8 slave)
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200570{
Paul Menzel533a3852016-11-27 22:17:44 +0100571 int i;
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200572 u8 vga_edid[128];
573 u8 header[8] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
Arthur Heymans85cfddb2017-02-06 13:47:21 +0100574 intel_gmbus_read_edid(mmiobase + GMBUS0, slave, 0x50, vga_edid, 128);
575 intel_gmbus_stop(mmiobase + GMBUS0);
Paul Menzel533a3852016-11-27 22:17:44 +0100576 for (i = 0; i < 8; i++) {
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200577 if (vga_edid[i] != header[i]) {
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200578 printk(BIOS_DEBUG, "No display connected on slave %d\n",
579 slave);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200580 return 0;
581 }
582 }
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200583 printk(BIOS_SPEW, "Found a display on slave %d\n", slave);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200584 return 1;
585}
586
Arthur Heymans8e079002017-01-14 22:31:54 +0100587static u32 get_cdclk(struct device *const dev)
588{
589 u16 gcfgc = pci_read_config16(dev, GCFGC);
590
591 if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
592 return 133333333;
593 } else {
594 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
595 case GC_DISPLAY_CLOCK_333_320_MHZ:
596 return 320000000;
597 default:
598 case GC_DISPLAY_CLOCK_190_200_MHZ:
599 return 200000000;
600 }
601 }
602}
603
604static u32 freq_to_blc_pwm_ctl(struct device *const dev, u16 pwm_freq)
605{
606 u32 blc_mod;
607
608 /* Set duty cycle to 100% due to use of legacy backlight control */
609 blc_mod = get_cdclk(dev) / (32 * pwm_freq);
610 return BLM_LEGACY_MODE | ((blc_mod / 2) << 17) | ((blc_mod / 2) << 1);
611}
612
613
614static void panel_setup(u8 *mmiobase, struct device *const dev)
615{
616 const struct northbridge_intel_i945_config *const conf = dev->chip_info;
617
618 u32 reg32;
619
620 /* Set up Panel Power On Delays */
621 reg32 = (conf->gpu_panel_power_up_delay & 0x1fff) << 16;
622 reg32 |= (conf->gpu_panel_power_backlight_on_delay & 0x1fff);
623 write32(mmiobase + PP_ON_DELAYS, reg32);
624
625 /* Set up Panel Power Off Delays */
626 reg32 = (conf->gpu_panel_power_down_delay & 0x1fff) << 16;
627 reg32 |= (conf->gpu_panel_power_backlight_off_delay & 0x1fff);
628 write32(mmiobase + PP_OFF_DELAYS, reg32);
629
630 /* Set up Panel Power Cycle Delay */
631 reg32 = (get_cdclk(dev) / 20000 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
632 reg32 |= conf->gpu_panel_power_cycle_delay & 0x1f;
633 write32(mmiobase + PP_DIVISOR, reg32);
634
635 /* Backlight init. */
636 if (conf->pwm_freq)
637 write32(mmiobase + BLC_PWM_CTL, freq_to_blc_pwm_ctl(dev,
638 conf->pwm_freq));
639 else
640 write32(mmiobase + BLC_PWM_CTL, freq_to_blc_pwm_ctl(dev,
641 DEFAULT_BLC_PWM));
642}
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200643
Stefan Reinauer30140a52009-03-11 16:20:39 +0000644static void gma_func0_init(struct device *dev)
645{
646 u32 reg32;
647
Patrick Georgi6444bd42012-07-06 11:31:39 +0200648 /* Unconditionally reset graphics */
649 pci_write_config8(dev, GDRST, 1);
650 udelay(50);
651 pci_write_config8(dev, GDRST, 0);
652 /* wait for device to finish */
Arthur Heymans70a8e342017-03-09 11:30:23 +0100653 while (pci_read_config8(dev, GDRST) & 1)
654 ;
Patrick Georgi6444bd42012-07-06 11:31:39 +0200655
Stefan Reinauer30140a52009-03-11 16:20:39 +0000656 /* IGD needs to be Bus Master */
657 reg32 = pci_read_config32(dev, PCI_COMMAND);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200658 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER
659 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
Denis 'GNUtoo' Cariklied7e29e2013-02-24 12:01:44 +0100660
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200661 if (IS_ENABLED(CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT)) {
662 /* This should probably run before post VBIOS init. */
663 printk(BIOS_SPEW, "Initializing VGA without OPROM.\n");
664 void *mmiobase;
665 u32 iobase, graphics_base;
666 struct northbridge_intel_i945_config *conf = dev->chip_info;
667
668 iobase = dev->resource_list[1].base;
669 mmiobase = (void *)(uintptr_t)dev->resource_list[0].base;
670 graphics_base = dev->resource_list[2].base;
671
672 printk(BIOS_SPEW, "GMADR = 0x%08x GTTADR = 0x%08x\n",
673 pci_read_config32(dev, GMADR),
674 pci_read_config32(dev, GTTADR)
675 );
676
677 int err;
Arthur Heymans8e079002017-01-14 22:31:54 +0100678
679 if (IS_ENABLED(CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GM))
680 panel_setup(mmiobase, dev);
681
Martin Roth128c1042016-11-18 09:29:03 -0700682 /* probe if VGA is connected and always run */
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200683 /* VGA init if no LVDS is connected */
Arthur Heymans8da22862017-08-06 15:56:30 +0200684 if (!probe_edid(mmiobase, GMBUS_PORT_PANEL) ||
685 probe_edid(mmiobase, GMBUS_PORT_VGADDC))
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200686 err = intel_gma_init_vga(conf,
687 pci_read_config32(dev, 0x5c) & ~0xf,
688 iobase, mmiobase, graphics_base);
689 else
690 err = intel_gma_init_lvds(conf,
691 pci_read_config32(dev, 0x5c) & ~0xf,
692 iobase, mmiobase, graphics_base);
693 if (err == 0)
694 gfx_set_init_done(1);
695 /* Linux relies on VBT for panel info. */
696 if (CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GM) {
697 generate_fake_intel_oprom(&conf->gfx, dev,
698 "$VBT CALISTOGA");
699 }
700 if (CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GC) {
701 generate_fake_intel_oprom(&conf->gfx, dev,
702 "$VBT LAKEPORT-G");
703 }
Arthur Heymansf3f4bea2016-10-20 20:44:54 +0200704 } else {
705 /* PCI Init, will run VBIOS */
706 pci_dev_init(dev);
Arthur Heymans333176e2016-09-07 22:10:57 +0200707 }
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200708
709 intel_gma_restore_opregion();
Stefan Reinauer30140a52009-03-11 16:20:39 +0000710}
711
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200712/* This doesn't reclaim stolen UMA memory, but IGD could still
Martin Roth128c1042016-11-18 09:29:03 -0700713 be re-enabled later. */
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200714static void gma_func0_disable(struct device *dev)
715{
716 struct device *dev_host = dev_find_slot(0, PCI_DEVFN(0x0, 0));
717
718 pci_write_config16(dev, GCFC, 0xa00);
719 pci_write_config16(dev_host, GGC, (1 << 1));
720
721 unsigned int reg32 = pci_read_config32(dev_host, DEVEN);
722 reg32 &= ~(DEVEN_D2F0 | DEVEN_D2F1);
723 pci_write_config32(dev_host, DEVEN, reg32);
724
725 dev->enabled = 0;
726}
727
Stefan Reinauer30140a52009-03-11 16:20:39 +0000728static void gma_func1_init(struct device *dev)
729{
730 u32 reg32;
Alexander Couzensc7a1a3e2016-03-09 10:42:58 +0100731 u8 val;
Stefan Reinauer30140a52009-03-11 16:20:39 +0000732
Martin Roth128c1042016-11-18 09:29:03 -0700733 /* IGD needs to be Bus Master, also enable IO access */
Stefan Reinauer30140a52009-03-11 16:20:39 +0000734 reg32 = pci_read_config32(dev, PCI_COMMAND);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000735 pci_write_config32(dev, PCI_COMMAND, reg32 |
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200736 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Sven Schnelleb629d142011-06-12 14:30:10 +0200737
Alexander Couzensc7a1a3e2016-03-09 10:42:58 +0100738 if (get_option(&val, "tft_brightness") == CB_SUCCESS)
739 pci_write_config8(dev, 0xf4, val);
740 else
741 pci_write_config8(dev, 0xf4, 0xff);
Stefan Reinauer30140a52009-03-11 16:20:39 +0000742}
743
Arthur Heymans70a8e342017-03-09 11:30:23 +0100744static void gma_set_subsystem(device_t dev, unsigned int vendor,
745 unsigned int device)
Stefan Reinauer30140a52009-03-11 16:20:39 +0000746{
747 if (!vendor || !device) {
748 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
749 pci_read_config32(dev, PCI_VENDOR_ID));
750 } else {
751 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
752 ((device & 0xffff) << 16) | (vendor & 0xffff));
753 }
754}
755
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100756const struct i915_gpu_controller_info *
757intel_gma_get_controller_info(void)
758{
Arthur Heymans70a8e342017-03-09 11:30:23 +0100759 device_t dev = dev_find_slot(0, PCI_DEVFN(0x2, 0));
760 if (!dev)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100761 return NULL;
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100762 struct northbridge_intel_i945_config *chip = dev->chip_info;
Arthur Heymans70a8e342017-03-09 11:30:23 +0100763 if (!chip)
Patrick Georgi54e227e2015-08-08 22:02:12 +0200764 return NULL;
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100765 return &chip->gfx;
766}
767
Alexander Couzens5eea4582015-04-12 22:18:55 +0200768static void gma_ssdt(device_t device)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100769{
770 const struct i915_gpu_controller_info *gfx = intel_gma_get_controller_info();
Arthur Heymans70a8e342017-03-09 11:30:23 +0100771 if (!gfx)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100772 return;
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100773
774 drivers_intel_gma_displays_ssdt_generate(gfx);
775}
776
Arthur Heymansc057a0612016-10-22 14:16:48 +0200777static void gma_func0_read_resources(device_t dev)
778{
779 u8 reg8;
780
781 /* Set Untrusted Aperture Size to 256mb */
782 reg8 = pci_read_config8(dev, MSAC);
783 reg8 &= ~0x3;
784 reg8 |= 0x2;
785 pci_write_config8(dev, MSAC, reg8);
786
787 pci_dev_read_resources(dev);
788}
789
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200790static unsigned long
791gma_write_acpi_tables(struct device *const dev,
792 unsigned long current,
793 struct acpi_rsdp *const rsdp)
794{
795 igd_opregion_t *opregion = (igd_opregion_t *)current;
796 global_nvs_t *gnvs;
797
798 if (intel_gma_init_igd_opregion(opregion) != CB_SUCCESS)
799 return current;
800
801 current += sizeof(igd_opregion_t);
802
803 /* GNVS has been already set up */
804 gnvs = cbmem_find(CBMEM_ID_ACPI_GNVS);
805 if (gnvs) {
806 /* IGD OpRegion Base Address */
807 gma_set_gnvs_aslb(gnvs, (uintptr_t)opregion);
808 } else {
809 printk(BIOS_ERR, "Error: GNVS table not found.\n");
810 }
811
812 current = acpi_align_current(current);
813 return current;
814}
815
816static const char *gma_acpi_name(const struct device *dev)
817{
818 return "GFX0";
819}
820
Stefan Reinauer30140a52009-03-11 16:20:39 +0000821static struct pci_operations gma_pci_ops = {
822 .set_subsystem = gma_set_subsystem,
823};
824
825static struct device_operations gma_func0_ops = {
Arthur Heymansc057a0612016-10-22 14:16:48 +0200826 .read_resources = gma_func0_read_resources,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000827 .set_resources = pci_dev_set_resources,
828 .enable_resources = pci_dev_enable_resources,
829 .init = gma_func0_init,
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100830 .acpi_fill_ssdt_generator = gma_ssdt,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000831 .scan_bus = 0,
832 .enable = 0,
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200833 .disable = gma_func0_disable,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000834 .ops_pci = &gma_pci_ops,
Patrick Rudolphf6aa7d92017-09-29 18:28:23 +0200835 .acpi_name = gma_acpi_name,
836 .write_acpi_tables = gma_write_acpi_tables,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000837};
838
839
840static struct device_operations gma_func1_ops = {
841 .read_resources = pci_dev_read_resources,
842 .set_resources = pci_dev_set_resources,
843 .enable_resources = pci_dev_enable_resources,
844 .init = gma_func1_init,
845 .scan_bus = 0,
846 .enable = 0,
847 .ops_pci = &gma_pci_ops,
848};
849
Elyes HAOUASa2993452016-10-28 10:56:59 +0200850static const unsigned short i945_gma_func0_ids[] = {
851 0x2772, /* 82945G/GZ Integrated Graphics Controller */
852 0x27a2, /* Mobile 945GM/GMS Express Integrated Graphics Controller*/
853 0x27ae, /* Mobile 945GSE Express Integrated Graphics Controller */
854 0
855};
856
857static const unsigned short i945_gma_func1_ids[] = {
858 0x27a6, /* Mobile 945GM/GMS/GME Express Integrated Graphics Controller */
859 0
860};
Vladimir Serbinenko10dd0e32014-11-17 00:07:12 +0100861
Stefan Reinauer30140a52009-03-11 16:20:39 +0000862static const struct pci_driver i945_gma_func0_driver __pci_driver = {
Paul Menzel82683c02018-04-14 19:56:46 +0200863 .ops = &gma_func0_ops,
864 .vendor = PCI_VENDOR_ID_INTEL,
Elyes HAOUASa2993452016-10-28 10:56:59 +0200865 .devices = i945_gma_func0_ids,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000866};
867
868static const struct pci_driver i945_gma_func1_driver __pci_driver = {
Paul Menzel82683c02018-04-14 19:56:46 +0200869 .ops = &gma_func1_ops,
870 .vendor = PCI_VENDOR_ID_INTEL,
Elyes HAOUASa2993452016-10-28 10:56:59 +0200871 .devices = i945_gma_func1_ids,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000872};