blob: bb0bb7300fb314fa191f7209d1c5f5fdd45bd65e [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>
26#include <string.h>
Vladimir Serbinenko0092c992014-08-21 01:06:53 +020027#include <pc80/vga.h>
28#include <pc80/vga_io.h>
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020029#include <commonlib/helpers.h>
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020030
Patrick Georgice6e9fe2012-07-20 12:37:06 +020031#include "i945.h"
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020032#include "chip.h"
Stefan Reinauer30140a52009-03-11 16:20:39 +000033
Patrick Georgi6444bd42012-07-06 11:31:39 +020034#define GDRST 0xc0
Arthur Heymansc057a0612016-10-22 14:16:48 +020035#define MSAC 0x62 /* Multi Size Aperture Control */
Patrick Georgi6444bd42012-07-06 11:31:39 +020036
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020037#define LVDS_CLOCK_A_POWERUP_ALL (3 << 8)
38#define LVDS_CLOCK_B_POWERUP_ALL (3 << 4)
39#define LVDS_CLOCK_BOTH_POWERUP_ALL (3 << 2)
40#define DISPPLANE_BGRX888 (0x6<<26)
41#define DPLLB_LVDS_P2_CLOCK_DIV_7 (1 << 24) /* i915 */
42
43#define DPLL_INTEGRATED_CRI_CLK_VLV (1<<14)
44
45#define PGETBL_CTL 0x2020
46#define PGETBL_ENABLED 0x00000001
47
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020048#define BASE_FREQUENCY 100000
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020049
Francis Rowe71512b22015-03-16 05:31:40 +000050static int gtt_setup(void *mmiobase)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020051{
52 unsigned long PGETBL_save;
Paul Menzelcc95f182014-06-05 22:45:35 +020053 unsigned long tom; // top of memory
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020054
Paul Menzelcc95f182014-06-05 22:45:35 +020055 /*
56 * The Video BIOS places the GTT right below top of memory.
Denis 'GNUtoo' Carikli16110e72014-10-14 07:33:53 +020057 */
Paul Menzelcc95f182014-06-05 22:45:35 +020058 tom = pci_read_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), TOLUD) << 24;
59 PGETBL_save = tom - 256 * KiB;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020060 PGETBL_save |= PGETBL_ENABLED;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020061 PGETBL_save |= 2; /* set GTT to 256kb */
62
63 write32(mmiobase + GFX_FLSH_CNTL, 0);
64
65 write32(mmiobase + PGETBL_CTL, PGETBL_save);
66
67 /* verify */
68 if (read32(mmiobase + PGETBL_CTL) & PGETBL_ENABLED) {
69 printk(BIOS_DEBUG, "gtt_setup is enabled.\n");
70 } else {
71 printk(BIOS_DEBUG, "gtt_setup failed!!!\n");
72 return 1;
73 }
74 write32(mmiobase + GFX_FLSH_CNTL, 0);
75
76 return 0;
77}
78
Arthur Heymansb59bcb22016-09-05 22:46:11 +020079static int intel_gma_init_lvds(struct northbridge_intel_i945_config *conf,
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020080 unsigned int pphysbase, unsigned int piobase,
Francis Rowe71512b22015-03-16 05:31:40 +000081 void *pmmio, unsigned int pgfx)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020082{
83 struct edid edid;
Mono2e4f83b2015-09-07 21:15:26 +020084 struct edid_mode *mode;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020085 u8 edid_data[128];
86 unsigned long temp;
87 int hpolarity, vpolarity;
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020088 u32 smallest_err = 0xffffffff;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020089 u32 target_frequency;
90 u32 pixel_p1 = 1;
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020091 u32 pixel_p2;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020092 u32 pixel_n = 1;
93 u32 pixel_m1 = 1;
94 u32 pixel_m2 = 1;
95 u32 hactive, vactive, right_border, bottom_border;
96 u32 vsync, hsync, vblank, hblank, hfront_porch, vfront_porch;
97 u32 i, j;
98 u32 uma_size;
99 u16 reg16;
100
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200101 printk(BIOS_SPEW,
Francis Rowe71512b22015-03-16 05:31:40 +0000102 "i915lightup: graphics %p mmio %p addrport %04x physbase %08x\n",
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200103 (void *)pgfx, pmmio, piobase, pphysbase);
104
Arthur Heymans7141ff32016-10-10 17:49:00 +0200105 intel_gmbus_read_edid(pmmio + GMBUS0, 3, 0x50, edid_data,
106 sizeof(edid_data));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200107 decode_edid(edid_data, sizeof(edid_data), &edid);
Mono2e4f83b2015-09-07 21:15:26 +0200108 mode = &edid.mode;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200109
Mono2e4f83b2015-09-07 21:15:26 +0200110 hpolarity = (mode->phsync == '-');
111 vpolarity = (mode->pvsync == '-');
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200112 hactive = edid.x_resolution;
113 vactive = edid.y_resolution;
Mono2e4f83b2015-09-07 21:15:26 +0200114 right_border = mode->hborder;
115 bottom_border = mode->vborder;
116 vblank = mode->vbl;
117 hblank = mode->hbl;
118 vsync = mode->vspw;
119 hsync = mode->hspw;
120 hfront_porch = mode->hso;
121 vfront_porch = mode->vso;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200122
123 for (i = 0; i < 2; i++)
124 for (j = 0; j < 0x100; j++)
Elyes HAOUAS0a15fe92016-09-17 19:12:27 +0200125 /* R = j, G = j, B = j. */
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200126 write32(pmmio + PALETTE(i) + 4 * j, 0x10101 * j);
127
128 write32(pmmio + PCH_PP_CONTROL, PANEL_UNLOCK_REGS
129 | (read32(pmmio + PCH_PP_CONTROL) & ~PANEL_UNLOCK_MASK));
130
131 write32(pmmio + MI_ARB_STATE, MI_ARB_C3_LP_WRITE_ENABLE | (1 << 27));
132 /* Clean registers. */
133 for (i = 0; i < 0x20; i += 4)
134 write32(pmmio + RENDER_RING_BASE + i, 0);
135 for (i = 0; i < 0x20; i += 4)
136 write32(pmmio + FENCE_REG_965_0 + i, 0);
137 write32(pmmio + PP_ON_DELAYS, 0);
138 write32(pmmio + PP_OFF_DELAYS, 0);
139
140 /* Disable VGA. */
141 write32(pmmio + VGACNTRL, VGA_DISP_DISABLE);
142
143 /* Disable pipes. */
144 write32(pmmio + PIPECONF(0), 0);
145 write32(pmmio + PIPECONF(1), 0);
146
147 /* Init PRB0. */
148 write32(pmmio + HWS_PGA, 0x352d2000);
149 write32(pmmio + PRB0_CTL, 0);
150 write32(pmmio + PRB0_HEAD, 0);
151 write32(pmmio + PRB0_TAIL, 0);
152 write32(pmmio + PRB0_START, 0);
153 write32(pmmio + PRB0_CTL, 0x0001f001);
154
155 write32(pmmio + D_STATE, DSTATE_PLL_D3_OFF
156 | DSTATE_GFX_CLOCK_GATING | DSTATE_DOT_CLOCK_GATING);
157 write32(pmmio + ECOSKPD, 0x00010000);
158 write32(pmmio + HWSTAM, 0xeffe);
159 write32(pmmio + PORT_HOTPLUG_EN, conf->gpu_hotplug);
160 write32(pmmio + INSTPM, 0x08000000 | INSTPM_AGPBUSY_DIS);
161
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200162 /* p2 divisor must 7 for dual channel LVDS */
163 /* and 14 for single channel LVDS */
164 pixel_p2 = mode->lvds_dual_channel ? 7 : 14;
165 target_frequency = mode->pixel_clock;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200166
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200167 /* Find suitable divisors, m1, m2, p1, n. */
168 /* refclock * (5 * (m1 + 2) + (m1 + 2)) / (n + 2) / p1 / p2 */
169 /* should be closest to target frequency as possible */
170 u32 candn, candm1, candm2, candp1;
171 for (candm1 = 8; candm1 <= 18; candm1++) {
172 for (candm2 = 3; candm2 <= 7; candm2++) {
173 for (candn = 1; candn <= 6; candn++) {
174 for (candp1 = 1; candp1 <= 8; candp1++) {
175 u32 m = 5 * (candm1 + 2) + (candm2 + 2);
176 u32 p = candp1 * pixel_p2;
177 u32 vco = DIV_ROUND_CLOSEST(BASE_FREQUENCY * m, candn + 2);
178 u32 dot = DIV_ROUND_CLOSEST(vco, p);
Arthur Heymans75f91312016-10-12 01:04:28 +0200179 u32 this_err = MAX(dot, target_frequency) -
180 MIN(dot, target_frequency);
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200181 if ((m < 70) || (m > 120))
182 continue;
183 if (this_err < smallest_err) {
184 smallest_err = this_err;
185 pixel_n = candn;
186 pixel_m1 = candm1;
187 pixel_m2 = candm2;
188 pixel_p1 = candp1;
189 }
190 }
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200191 }
192 }
193 }
194
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200195 if (smallest_err == 0xffffffff) {
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200196 printk (BIOS_ERR, "Couldn't find GFX clock divisors\n");
197 return -1;
198 }
199
200 printk(BIOS_INFO, "bringing up panel at resolution %d x %d\n",
201 hactive, vactive);
202 printk(BIOS_DEBUG, "Borders %d x %d\n", right_border, bottom_border);
203 printk(BIOS_DEBUG, "Blank %d x %d\n", hblank, vblank);
204 printk(BIOS_DEBUG, "Sync %d x %d\n", hsync, vsync);
205 printk(BIOS_DEBUG, "Front porch %d x %d\n", hfront_porch, vfront_porch);
206 printk(BIOS_DEBUG, (conf->gpu_lvds_use_spread_spectrum_clock
207 ? "Spread spectrum clock\n"
208 : "DREF clock\n"));
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200209 printk(BIOS_DEBUG, (mode->lvds_dual_channel
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200210 ? "Dual channel\n"
211 : "Single channel\n"));
212 printk(BIOS_DEBUG, "Polarities %d, %d\n",
213 hpolarity, vpolarity);
214 printk(BIOS_DEBUG, "Pixel N=%d, M1=%d, M2=%d, P1=%d\n",
215 pixel_n, pixel_m1, pixel_m2, pixel_p1);
216 printk(BIOS_DEBUG, "Pixel clock %d kHz\n",
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200217 BASE_FREQUENCY * (5 * (pixel_m1 + 2) + (pixel_m2 + 2)) /
218 (pixel_n + 2) / (pixel_p1 * pixel_p2));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200219
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200220 if (IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)) {
221 /* Disable panel fitter (we're in native resolution). */
222 write32(pmmio + PF_CTL(0), 0);
223 write32(pmmio + PF_WIN_SZ(0), 0);
224 write32(pmmio + PF_WIN_POS(0), 0);
225 write32(pmmio + PFIT_PGM_RATIOS, 0);
226 write32(pmmio + PFIT_CONTROL, 0);
227 } else {
228 write32(pmmio + PF_WIN_SZ(0), vactive | (hactive << 16));
229 write32(pmmio + PF_WIN_POS(0), 0);
230 write32(pmmio + PF_CTL(0), PF_ENABLE | PF_FILTER_MED_3x3);
231 write32(pmmio + PFIT_CONTROL, PFIT_ENABLE
232 | (1 << PFIT_PIPE_SHIFT) | HORIZ_AUTO_SCALE
233 | VERT_AUTO_SCALE);
234 }
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200235
236 mdelay(1);
237
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200238 write32(pmmio + DSPCNTR(0), DISPPLANE_BGRX888
239 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
240
241 mdelay(1);
242 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS
243 | (read32(pmmio + PP_CONTROL) & ~PANEL_UNLOCK_MASK));
244 write32(pmmio + FP0(1),
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200245 (pixel_n << 16)
246 | (pixel_m1 << 8) | pixel_m2);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200247 write32(pmmio + DPLL(1),
248 DPLL_VGA_MODE_DIS |
249 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200250 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200251 : DPLLB_LVDS_P2_CLOCK_DIV_14)
252 | (conf->gpu_lvds_use_spread_spectrum_clock
253 ? DPLL_INTEGRATED_CLOCK_VLV | DPLL_INTEGRATED_CRI_CLK_VLV
254 : 0)
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200255 | (0x10000 << (pixel_p1 - 1)));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200256 mdelay(1);
257 write32(pmmio + DPLL(1),
258 DPLL_VGA_MODE_DIS |
259 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200260 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200261 : DPLLB_LVDS_P2_CLOCK_DIV_14)
262 | ((conf->gpu_lvds_use_spread_spectrum_clock ? 3 : 0) << 13)
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200263 | (0x10000 << (pixel_p1 - 1)));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200264 mdelay(1);
265 write32(pmmio + HTOTAL(1),
266 ((hactive + right_border + hblank - 1) << 16)
267 | (hactive - 1));
268 write32(pmmio + HBLANK(1),
269 ((hactive + right_border + hblank - 1) << 16)
270 | (hactive + right_border - 1));
271 write32(pmmio + HSYNC(1),
272 ((hactive + right_border + hfront_porch + hsync - 1) << 16)
273 | (hactive + right_border + hfront_porch - 1));
274
275 write32(pmmio + VTOTAL(1), ((vactive + bottom_border + vblank - 1) << 16)
276 | (vactive - 1));
277 write32(pmmio + VBLANK(1), ((vactive + bottom_border + vblank - 1) << 16)
278 | (vactive + bottom_border - 1));
279 write32(pmmio + VSYNC(1),
Arthur Heymansc8c73a62016-10-13 14:12:45 +0200280 ((vactive + bottom_border + vfront_porch + vsync - 1) << 16)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200281 | (vactive + bottom_border + vfront_porch - 1));
282
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200283 if (IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)) {
284 write32(pmmio + PIPESRC(1), ((hactive - 1) << 16)
285 | (vactive - 1));
286 } else {
287 write32(pmmio + PIPESRC(1), (639 << 16) | 399);
288 }
289
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200290 mdelay(1);
291
292 write32(pmmio + DSPSIZE(0), (hactive - 1) | ((vactive - 1) << 16));
293 write32(pmmio + DSPPOS(0), 0);
294
295 /* Backlight init. */
296 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
297 write32(pmmio + FW_BLC, 0x011d011a);
298 write32(pmmio + FW_BLC2, 0x00000102);
299 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
300 write32(pmmio + FW_BLC_SELF, 0x0001003f);
301 write32(pmmio + FW_BLC, 0x011d0109);
302 write32(pmmio + FW_BLC2, 0x00000102);
303 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
304 write32(pmmio + BLC_PWM_CTL, conf->gpu_backlight);
305
306 edid.bytes_per_line = (edid.bytes_per_line + 63) & ~63;
307 write32(pmmio + DSPADDR(0), 0);
308 write32(pmmio + DSPSURF(0), 0);
309 write32(pmmio + DSPSTRIDE(0), edid.bytes_per_line);
310 write32(pmmio + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888
311 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
312 mdelay(1);
313
314 write32(pmmio + PIPECONF(1), PIPECONF_ENABLE);
315 write32(pmmio + LVDS, LVDS_ON
316 | (hpolarity << 20) | (vpolarity << 21)
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200317 | (mode->lvds_dual_channel ? LVDS_CLOCK_B_POWERUP_ALL
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200318 | LVDS_CLOCK_BOTH_POWERUP_ALL : 0)
319 | LVDS_CLOCK_A_POWERUP_ALL
320 | LVDS_PIPE(1));
321
322 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
323 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_RESET);
324 mdelay(1);
325 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS
326 | PANEL_POWER_ON | PANEL_POWER_RESET);
327
328 printk (BIOS_DEBUG, "waiting for panel powerup\n");
329 while (1) {
330 u32 reg32;
331 reg32 = read32(pmmio + PP_STATUS);
332 if ((reg32 & PP_SEQUENCE_MASK) == PP_SEQUENCE_NONE)
333 break;
334 }
335 printk (BIOS_DEBUG, "panel powered up\n");
336
337 write32(pmmio + PP_CONTROL, PANEL_POWER_ON | PANEL_POWER_RESET);
338
339 /* Clear interrupts. */
340 write32(pmmio + DEIIR, 0xffffffff);
341 write32(pmmio + SDEIIR, 0xffffffff);
342 write32(pmmio + IIR, 0xffffffff);
343 write32(pmmio + IMR, 0xffffffff);
344 write32(pmmio + EIR, 0xffffffff);
345
346 if (gtt_setup(pmmio)) {
347 printk(BIOS_ERR, "ERROR: GTT Setup Failed!!!\n");
348 return 0;
349 }
350
351 /* Setup GTT. */
352
353 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
354 uma_size = 0;
355 if (!(reg16 & 2)) {
Arthur Heymans874a8f92016-05-19 16:06:09 +0200356 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200357 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
358 }
359
Vladimir Serbinenko055fe032014-08-19 23:59:27 +0200360 for (i = 0; i < (uma_size - 256) / 4; i++)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200361 {
362 outl((i << 2) | 1, piobase);
363 outl(pphysbase + (i << 12) + 1, piobase + 4);
364 }
365
366 temp = read32(pmmio + PGETBL_CTL);
367 printk(BIOS_INFO, "GTT PGETBL_CTL register: 0x%lx\n", temp);
368
369 if (temp & 1)
370 printk(BIOS_INFO, "GTT Enabled\n");
371 else
372 printk(BIOS_ERR, "ERROR: GTT is still Disabled!!!\n");
373
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200374 if (IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)) {
375 printk(BIOS_SPEW, "memset %p to 0x00 for %d bytes\n",
376 (void *)pgfx, hactive * vactive * 4);
377 memset((void *)pgfx, 0x00, hactive * vactive * 4);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200378
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200379 set_vbe_mode_info_valid(&edid, pgfx);
380 } else {
381 vga_misc_write(0x67);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200382
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200383 write32(pmmio + DSPCNTR(0), DISPPLANE_SEL_PIPE_B);
384 write32(pmmio + VGACNTRL, 0x02c4008e
385 | VGA_PIPE_B_SELECT);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200386
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200387 vga_textmode_init();
388 }
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200389 return 0;
390}
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200391
392static int intel_gma_init_vga(struct northbridge_intel_i945_config *conf,
393 unsigned int pphysbase, unsigned int piobase,
394 void *pmmio, unsigned int pgfx)
395{
396 int i;
397 u32 hactive, vactive;
398 u16 reg16;
399 u32 uma_size;
400
401 printk(BIOS_SPEW, "pmmio %x addrport %x physbase %x\n",
402 (u32)pmmio, piobase, pphysbase);
403
404 gtt_setup(pmmio);
405
406 /* Disable VGA. */
407 write32(pmmio + VGACNTRL, VGA_DISP_DISABLE);
408
409 /* Disable pipes. */
410 write32(pmmio + PIPECONF(0), 0);
411 write32(pmmio + PIPECONF(1), 0);
412
413 write32(pmmio + INSTPM, 0x800);
414
415 vga_gr_write(0x18, 0);
416
417 write32(pmmio + VGA0, 0x200074);
418 write32(pmmio + VGA1, 0x200074);
419
420 write32(pmmio + DSPFW3, 0x7f3f00c1 & ~PINEVIEW_SELF_REFRESH_EN);
421 write32(pmmio + DSPCLK_GATE_D, 0);
422 write32(pmmio + FW_BLC, 0x03060106);
423 write32(pmmio + FW_BLC2, 0x00000306);
424
425 write32(pmmio + ADPA, ADPA_DAC_ENABLE
426 | ADPA_PIPE_A_SELECT
427 | ADPA_USE_VGA_HVPOLARITY
428 | ADPA_VSYNC_CNTL_ENABLE
429 | ADPA_HSYNC_CNTL_ENABLE
430 | ADPA_DPMS_ON
431 );
432
433 write32(pmmio + 0x7041c, 0x0);
434
435 write32(pmmio + DPLL_MD(0), 0x3);
436 write32(pmmio + DPLL_MD(1), 0x3);
437 write32(pmmio + DSPCNTR(1), 0x1000000);
438 write32(pmmio + PIPESRC(1), 0x027f01df);
439
440 vga_misc_write(0x67);
441 const u8 cr[] = { 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
442 0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x00, 0x00,
443 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
444 0xff
445 };
446 vga_cr_write(0x11, 0);
447
448 for (i = 0; i <= 0x18; i++)
449 vga_cr_write(i, cr[i]);
450
451 // Disable screen memory to prevent garbage from appearing.
452 vga_sr_write(1, vga_sr_read(1) | 0x20);
453 hactive = 640;
454 vactive = 400;
455
456 mdelay(1);
457 write32(pmmio + DPLL(0),
458 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
459 | DPLL_VGA_MODE_DIS
460 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
461 | 0x400601
462 );
463 mdelay(1);
464 write32(pmmio + DPLL(0),
465 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
466 | DPLL_VGA_MODE_DIS
467 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
468 | 0x400601
469 );
470
471 write32(pmmio + ADPA, ADPA_DAC_ENABLE
472 | ADPA_PIPE_A_SELECT
473 | ADPA_USE_VGA_HVPOLARITY
474 | ADPA_VSYNC_CNTL_ENABLE
475 | ADPA_HSYNC_CNTL_ENABLE
476 | ADPA_DPMS_ON
477 );
478
479 write32(pmmio + HTOTAL(0),
480 ((hactive - 1) << 16)
481 | (hactive - 1));
482 write32(pmmio + HBLANK(0),
483 ((hactive - 1) << 16)
484 | (hactive - 1));
485 write32(pmmio + HSYNC(0),
486 ((hactive - 1) << 16)
487 | (hactive - 1));
488
489 write32(pmmio + VTOTAL(0), ((vactive - 1) << 16)
490 | (vactive - 1));
491 write32(pmmio + VBLANK(0), ((vactive - 1) << 16)
492 | (vactive - 1));
493 write32(pmmio + VSYNC(0),
494 ((vactive - 1) << 16)
495 | (vactive - 1));
496
497 write32(pmmio + PF_WIN_POS(0), 0);
498
499 write32(pmmio + PIPESRC(0), (639 << 16) | 399);
500 write32(pmmio + PF_CTL(0),PF_ENABLE | PF_FILTER_MED_3x3);
501 write32(pmmio + PF_WIN_SZ(0), vactive | (hactive << 16));
502 write32(pmmio + PFIT_CONTROL, 0x0);
503
504 mdelay(1);
505
506 write32(pmmio + FDI_RX_CTL(0), 0x00002040);
507 mdelay(1);
508 write32(pmmio + FDI_RX_CTL(0), 0x80002050);
509 write32(pmmio + FDI_TX_CTL(0), 0x00044000);
510 mdelay(1);
511 write32(pmmio + FDI_TX_CTL(0), 0x80044000);
512 write32(pmmio + PIPECONF(0), PIPECONF_ENABLE | PIPECONF_BPP_6 | PIPECONF_DITHER_EN);
513
514 write32(pmmio + VGACNTRL, 0x0);
515 write32(pmmio + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888);
516 mdelay(1);
517
518 write32(pmmio + ADPA, ADPA_DAC_ENABLE
519 | ADPA_PIPE_A_SELECT
520 | ADPA_USE_VGA_HVPOLARITY
521 | ADPA_VSYNC_CNTL_ENABLE
522 | ADPA_HSYNC_CNTL_ENABLE
523 | ADPA_DPMS_ON
524 );
525
526 write32(pmmio + DSPFW3, 0x7f3f00c1);
527 write32(pmmio + MI_MODE, 0x200 | VS_TIMER_DISPATCH);
528 write32(pmmio + CACHE_MODE_0, (0x6820 | (1 << 9)) & ~(1 << 5));
529 write32(pmmio + CACHE_MODE_1, 0x380 & ~(1 << 9));
530
531 /* Set up GTT. */
532
533 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
534 uma_size = 0;
535 if (!(reg16 & 2)) {
536 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
537 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
538 }
539
540 for (i = 0; i < (uma_size - 256) / 4; i++)
541 {
542 outl((i << 2) | 1, piobase);
543 outl(pphysbase + (i << 12) + 1, piobase + 4);
544 }
545
546 /* Clear interrupts. */
547 write32(pmmio + DEIIR, 0xffffffff);
548 write32(pmmio + SDEIIR, 0xffffffff);
549 write32(pmmio + IIR, 0xffffffff);
550 write32(pmmio + IMR, 0xffffffff);
551 write32(pmmio + EIR, 0xffffffff);
552
553 vga_textmode_init();
554
555 /* Enable screen memory. */
556 vga_sr_write(1, vga_sr_read(1) & ~0x20);
557
558 return 0;
559
560}
561
562/* compare the header of the vga edid header */
563/* if vga is not connected it should have a correct header */
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200564static int probe_edid(u8 *pmmio, u8 slave)
565{
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200566 u8 vga_edid[128];
567 u8 header[8] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200568 intel_gmbus_read_edid(pmmio + GMBUS0, slave, 0x50, vga_edid, 128);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200569 intel_gmbus_stop(pmmio + GMBUS0);
570 for (int i = 0; i < 8; i++) {
571 if (vga_edid[i] != header[i]) {
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200572 printk(BIOS_DEBUG, "No display connected on slave %d\n",
573 slave);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200574 return 0;
575 }
576 }
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200577 printk(BIOS_SPEW, "Found a display on slave %d\n", slave);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200578 return 1;
579}
580
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200581
Stefan Reinauer30140a52009-03-11 16:20:39 +0000582static void gma_func0_init(struct device *dev)
583{
584 u32 reg32;
585
Patrick Georgi6444bd42012-07-06 11:31:39 +0200586 /* Unconditionally reset graphics */
587 pci_write_config8(dev, GDRST, 1);
588 udelay(50);
589 pci_write_config8(dev, GDRST, 0);
590 /* wait for device to finish */
591 while (pci_read_config8(dev, GDRST) & 1) { };
592
Stefan Reinauer30140a52009-03-11 16:20:39 +0000593 /* IGD needs to be Bus Master */
594 reg32 = pci_read_config32(dev, PCI_COMMAND);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200595 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER
596 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
Denis 'GNUtoo' Cariklied7e29e2013-02-24 12:01:44 +0100597
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200598 if (IS_ENABLED(CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT)) {
599 /* This should probably run before post VBIOS init. */
600 printk(BIOS_SPEW, "Initializing VGA without OPROM.\n");
601 void *mmiobase;
602 u32 iobase, graphics_base;
603 struct northbridge_intel_i945_config *conf = dev->chip_info;
604
605 iobase = dev->resource_list[1].base;
606 mmiobase = (void *)(uintptr_t)dev->resource_list[0].base;
607 graphics_base = dev->resource_list[2].base;
608
609 printk(BIOS_SPEW, "GMADR = 0x%08x GTTADR = 0x%08x\n",
610 pci_read_config32(dev, GMADR),
611 pci_read_config32(dev, GTTADR)
612 );
613
614 int err;
615 /* probe if VGA is connected and alway run */
616 /* VGA init if no LVDS is connected */
617 if (!probe_edid(mmiobase, 3) || probe_edid(mmiobase, 2))
618 err = intel_gma_init_vga(conf,
619 pci_read_config32(dev, 0x5c) & ~0xf,
620 iobase, mmiobase, graphics_base);
621 else
622 err = intel_gma_init_lvds(conf,
623 pci_read_config32(dev, 0x5c) & ~0xf,
624 iobase, mmiobase, graphics_base);
625 if (err == 0)
626 gfx_set_init_done(1);
627 /* Linux relies on VBT for panel info. */
628 if (CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GM) {
629 generate_fake_intel_oprom(&conf->gfx, dev,
630 "$VBT CALISTOGA");
631 }
632 if (CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GC) {
633 generate_fake_intel_oprom(&conf->gfx, dev,
634 "$VBT LAKEPORT-G");
635 }
Arthur Heymansf3f4bea2016-10-20 20:44:54 +0200636 } else {
637 /* PCI Init, will run VBIOS */
638 pci_dev_init(dev);
Arthur Heymans333176e2016-09-07 22:10:57 +0200639 }
Stefan Reinauer30140a52009-03-11 16:20:39 +0000640}
641
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200642/* This doesn't reclaim stolen UMA memory, but IGD could still
643 be reenabled later. */
644static void gma_func0_disable(struct device *dev)
645{
646 struct device *dev_host = dev_find_slot(0, PCI_DEVFN(0x0, 0));
647
648 pci_write_config16(dev, GCFC, 0xa00);
649 pci_write_config16(dev_host, GGC, (1 << 1));
650
651 unsigned int reg32 = pci_read_config32(dev_host, DEVEN);
652 reg32 &= ~(DEVEN_D2F0 | DEVEN_D2F1);
653 pci_write_config32(dev_host, DEVEN, reg32);
654
655 dev->enabled = 0;
656}
657
Stefan Reinauer30140a52009-03-11 16:20:39 +0000658static void gma_func1_init(struct device *dev)
659{
660 u32 reg32;
Alexander Couzensc7a1a3e2016-03-09 10:42:58 +0100661 u8 val;
Stefan Reinauer30140a52009-03-11 16:20:39 +0000662
663 /* IGD needs to be Bus Master, also enable IO accesss */
664 reg32 = pci_read_config32(dev, PCI_COMMAND);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000665 pci_write_config32(dev, PCI_COMMAND, reg32 |
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200666 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Sven Schnelleb629d142011-06-12 14:30:10 +0200667
Alexander Couzensc7a1a3e2016-03-09 10:42:58 +0100668 if (get_option(&val, "tft_brightness") == CB_SUCCESS)
669 pci_write_config8(dev, 0xf4, val);
670 else
671 pci_write_config8(dev, 0xf4, 0xff);
Stefan Reinauer30140a52009-03-11 16:20:39 +0000672}
673
674static void gma_set_subsystem(device_t dev, unsigned vendor, unsigned device)
675{
676 if (!vendor || !device) {
677 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
678 pci_read_config32(dev, PCI_VENDOR_ID));
679 } else {
680 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
681 ((device & 0xffff) << 16) | (vendor & 0xffff));
682 }
683}
684
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100685const struct i915_gpu_controller_info *
686intel_gma_get_controller_info(void)
687{
688 device_t dev = dev_find_slot(0, PCI_DEVFN(0x2,0));
689 if (!dev) {
690 return NULL;
691 }
692 struct northbridge_intel_i945_config *chip = dev->chip_info;
Patrick Georgi54e227e2015-08-08 22:02:12 +0200693 if (!chip) {
694 return NULL;
695 }
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100696 return &chip->gfx;
697}
698
Alexander Couzens5eea4582015-04-12 22:18:55 +0200699static void gma_ssdt(device_t device)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100700{
701 const struct i915_gpu_controller_info *gfx = intel_gma_get_controller_info();
702 if (!gfx) {
703 return;
704 }
705
706 drivers_intel_gma_displays_ssdt_generate(gfx);
707}
708
Arthur Heymansc057a0612016-10-22 14:16:48 +0200709static void gma_func0_read_resources(device_t dev)
710{
711 u8 reg8;
712
713 /* Set Untrusted Aperture Size to 256mb */
714 reg8 = pci_read_config8(dev, MSAC);
715 reg8 &= ~0x3;
716 reg8 |= 0x2;
717 pci_write_config8(dev, MSAC, reg8);
718
719 pci_dev_read_resources(dev);
720}
721
Stefan Reinauer30140a52009-03-11 16:20:39 +0000722static struct pci_operations gma_pci_ops = {
723 .set_subsystem = gma_set_subsystem,
724};
725
726static struct device_operations gma_func0_ops = {
Arthur Heymansc057a0612016-10-22 14:16:48 +0200727 .read_resources = gma_func0_read_resources,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000728 .set_resources = pci_dev_set_resources,
729 .enable_resources = pci_dev_enable_resources,
730 .init = gma_func0_init,
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100731 .acpi_fill_ssdt_generator = gma_ssdt,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000732 .scan_bus = 0,
733 .enable = 0,
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200734 .disable = gma_func0_disable,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000735 .ops_pci = &gma_pci_ops,
736};
737
738
739static struct device_operations gma_func1_ops = {
740 .read_resources = pci_dev_read_resources,
741 .set_resources = pci_dev_set_resources,
742 .enable_resources = pci_dev_enable_resources,
743 .init = gma_func1_init,
744 .scan_bus = 0,
745 .enable = 0,
746 .ops_pci = &gma_pci_ops,
747};
748
Elyes HAOUASa2993452016-10-28 10:56:59 +0200749static const unsigned short i945_gma_func0_ids[] = {
750 0x2772, /* 82945G/GZ Integrated Graphics Controller */
751 0x27a2, /* Mobile 945GM/GMS Express Integrated Graphics Controller*/
752 0x27ae, /* Mobile 945GSE Express Integrated Graphics Controller */
753 0
754};
755
756static const unsigned short i945_gma_func1_ids[] = {
757 0x27a6, /* Mobile 945GM/GMS/GME Express Integrated Graphics Controller */
758 0
759};
Vladimir Serbinenko10dd0e32014-11-17 00:07:12 +0100760
Stefan Reinauer30140a52009-03-11 16:20:39 +0000761static const struct pci_driver i945_gma_func0_driver __pci_driver = {
762 .ops = &gma_func0_ops,
763 .vendor = PCI_VENDOR_ID_INTEL,
Elyes HAOUASa2993452016-10-28 10:56:59 +0200764 .devices = i945_gma_func0_ids,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000765};
766
767static const struct pci_driver i945_gma_func1_driver __pci_driver = {
768 .ops = &gma_func1_ops,
769 .vendor = PCI_VENDOR_ID_INTEL,
Elyes HAOUASa2993452016-10-28 10:56:59 +0200770 .devices = i945_gma_func1_ids,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000771};