blob: 4baed802933260013c9d707dd8f8b18962ece62b [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
35
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020036#define LVDS_CLOCK_A_POWERUP_ALL (3 << 8)
37#define LVDS_CLOCK_B_POWERUP_ALL (3 << 4)
38#define LVDS_CLOCK_BOTH_POWERUP_ALL (3 << 2)
39#define DISPPLANE_BGRX888 (0x6<<26)
40#define DPLLB_LVDS_P2_CLOCK_DIV_7 (1 << 24) /* i915 */
41
42#define DPLL_INTEGRATED_CRI_CLK_VLV (1<<14)
43
44#define PGETBL_CTL 0x2020
45#define PGETBL_ENABLED 0x00000001
46
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020047#define BASE_FREQUENCY 100000
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020048
Francis Rowe71512b22015-03-16 05:31:40 +000049static int gtt_setup(void *mmiobase)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020050{
51 unsigned long PGETBL_save;
Paul Menzelcc95f182014-06-05 22:45:35 +020052 unsigned long tom; // top of memory
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020053
Paul Menzelcc95f182014-06-05 22:45:35 +020054 /*
55 * The Video BIOS places the GTT right below top of memory.
Denis 'GNUtoo' Carikli16110e72014-10-14 07:33:53 +020056 */
Paul Menzelcc95f182014-06-05 22:45:35 +020057 tom = pci_read_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), TOLUD) << 24;
58 PGETBL_save = tom - 256 * KiB;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020059 PGETBL_save |= PGETBL_ENABLED;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020060 PGETBL_save |= 2; /* set GTT to 256kb */
61
62 write32(mmiobase + GFX_FLSH_CNTL, 0);
63
64 write32(mmiobase + PGETBL_CTL, PGETBL_save);
65
66 /* verify */
67 if (read32(mmiobase + PGETBL_CTL) & PGETBL_ENABLED) {
68 printk(BIOS_DEBUG, "gtt_setup is enabled.\n");
69 } else {
70 printk(BIOS_DEBUG, "gtt_setup failed!!!\n");
71 return 1;
72 }
73 write32(mmiobase + GFX_FLSH_CNTL, 0);
74
75 return 0;
76}
77
Arthur Heymansb59bcb22016-09-05 22:46:11 +020078static int intel_gma_init_lvds(struct northbridge_intel_i945_config *conf,
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020079 unsigned int pphysbase, unsigned int piobase,
Francis Rowe71512b22015-03-16 05:31:40 +000080 void *pmmio, unsigned int pgfx)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020081{
82 struct edid edid;
Mono2e4f83b2015-09-07 21:15:26 +020083 struct edid_mode *mode;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020084 u8 edid_data[128];
85 unsigned long temp;
86 int hpolarity, vpolarity;
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020087 u32 smallest_err = 0xffffffff;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020088 u32 target_frequency;
89 u32 pixel_p1 = 1;
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020090 u32 pixel_p2;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020091 u32 pixel_n = 1;
92 u32 pixel_m1 = 1;
93 u32 pixel_m2 = 1;
94 u32 hactive, vactive, right_border, bottom_border;
95 u32 vsync, hsync, vblank, hblank, hfront_porch, vfront_porch;
96 u32 i, j;
97 u32 uma_size;
98 u16 reg16;
99
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200100 printk(BIOS_SPEW,
Francis Rowe71512b22015-03-16 05:31:40 +0000101 "i915lightup: graphics %p mmio %p addrport %04x physbase %08x\n",
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200102 (void *)pgfx, pmmio, piobase, pphysbase);
103
Arthur Heymans7141ff32016-10-10 17:49:00 +0200104 intel_gmbus_read_edid(pmmio + GMBUS0, 3, 0x50, edid_data,
105 sizeof(edid_data));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200106 decode_edid(edid_data, sizeof(edid_data), &edid);
Mono2e4f83b2015-09-07 21:15:26 +0200107 mode = &edid.mode;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200108
Mono2e4f83b2015-09-07 21:15:26 +0200109 hpolarity = (mode->phsync == '-');
110 vpolarity = (mode->pvsync == '-');
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200111 hactive = edid.x_resolution;
112 vactive = edid.y_resolution;
Mono2e4f83b2015-09-07 21:15:26 +0200113 right_border = mode->hborder;
114 bottom_border = mode->vborder;
115 vblank = mode->vbl;
116 hblank = mode->hbl;
117 vsync = mode->vspw;
118 hsync = mode->hspw;
119 hfront_porch = mode->hso;
120 vfront_porch = mode->vso;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200121
122 for (i = 0; i < 2; i++)
123 for (j = 0; j < 0x100; j++)
Elyes HAOUAS0a15fe92016-09-17 19:12:27 +0200124 /* R = j, G = j, B = j. */
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200125 write32(pmmio + PALETTE(i) + 4 * j, 0x10101 * j);
126
127 write32(pmmio + PCH_PP_CONTROL, PANEL_UNLOCK_REGS
128 | (read32(pmmio + PCH_PP_CONTROL) & ~PANEL_UNLOCK_MASK));
129
130 write32(pmmio + MI_ARB_STATE, MI_ARB_C3_LP_WRITE_ENABLE | (1 << 27));
131 /* Clean registers. */
132 for (i = 0; i < 0x20; i += 4)
133 write32(pmmio + RENDER_RING_BASE + i, 0);
134 for (i = 0; i < 0x20; i += 4)
135 write32(pmmio + FENCE_REG_965_0 + i, 0);
136 write32(pmmio + PP_ON_DELAYS, 0);
137 write32(pmmio + PP_OFF_DELAYS, 0);
138
139 /* Disable VGA. */
140 write32(pmmio + VGACNTRL, VGA_DISP_DISABLE);
141
142 /* Disable pipes. */
143 write32(pmmio + PIPECONF(0), 0);
144 write32(pmmio + PIPECONF(1), 0);
145
146 /* Init PRB0. */
147 write32(pmmio + HWS_PGA, 0x352d2000);
148 write32(pmmio + PRB0_CTL, 0);
149 write32(pmmio + PRB0_HEAD, 0);
150 write32(pmmio + PRB0_TAIL, 0);
151 write32(pmmio + PRB0_START, 0);
152 write32(pmmio + PRB0_CTL, 0x0001f001);
153
154 write32(pmmio + D_STATE, DSTATE_PLL_D3_OFF
155 | DSTATE_GFX_CLOCK_GATING | DSTATE_DOT_CLOCK_GATING);
156 write32(pmmio + ECOSKPD, 0x00010000);
157 write32(pmmio + HWSTAM, 0xeffe);
158 write32(pmmio + PORT_HOTPLUG_EN, conf->gpu_hotplug);
159 write32(pmmio + INSTPM, 0x08000000 | INSTPM_AGPBUSY_DIS);
160
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200161 /* p2 divisor must 7 for dual channel LVDS */
162 /* and 14 for single channel LVDS */
163 pixel_p2 = mode->lvds_dual_channel ? 7 : 14;
164 target_frequency = mode->pixel_clock;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200165
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200166 /* Find suitable divisors, m1, m2, p1, n. */
167 /* refclock * (5 * (m1 + 2) + (m1 + 2)) / (n + 2) / p1 / p2 */
168 /* should be closest to target frequency as possible */
169 u32 candn, candm1, candm2, candp1;
170 for (candm1 = 8; candm1 <= 18; candm1++) {
171 for (candm2 = 3; candm2 <= 7; candm2++) {
172 for (candn = 1; candn <= 6; candn++) {
173 for (candp1 = 1; candp1 <= 8; candp1++) {
174 u32 m = 5 * (candm1 + 2) + (candm2 + 2);
175 u32 p = candp1 * pixel_p2;
176 u32 vco = DIV_ROUND_CLOSEST(BASE_FREQUENCY * m, candn + 2);
177 u32 dot = DIV_ROUND_CLOSEST(vco, p);
Arthur Heymans75f91312016-10-12 01:04:28 +0200178 u32 this_err = MAX(dot, target_frequency) -
179 MIN(dot, target_frequency);
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200180 if ((m < 70) || (m > 120))
181 continue;
182 if (this_err < smallest_err) {
183 smallest_err = this_err;
184 pixel_n = candn;
185 pixel_m1 = candm1;
186 pixel_m2 = candm2;
187 pixel_p1 = candp1;
188 }
189 }
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200190 }
191 }
192 }
193
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200194 if (smallest_err == 0xffffffff) {
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200195 printk (BIOS_ERR, "Couldn't find GFX clock divisors\n");
196 return -1;
197 }
198
199 printk(BIOS_INFO, "bringing up panel at resolution %d x %d\n",
200 hactive, vactive);
201 printk(BIOS_DEBUG, "Borders %d x %d\n", right_border, bottom_border);
202 printk(BIOS_DEBUG, "Blank %d x %d\n", hblank, vblank);
203 printk(BIOS_DEBUG, "Sync %d x %d\n", hsync, vsync);
204 printk(BIOS_DEBUG, "Front porch %d x %d\n", hfront_porch, vfront_porch);
205 printk(BIOS_DEBUG, (conf->gpu_lvds_use_spread_spectrum_clock
206 ? "Spread spectrum clock\n"
207 : "DREF clock\n"));
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200208 printk(BIOS_DEBUG, (mode->lvds_dual_channel
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200209 ? "Dual channel\n"
210 : "Single channel\n"));
211 printk(BIOS_DEBUG, "Polarities %d, %d\n",
212 hpolarity, vpolarity);
213 printk(BIOS_DEBUG, "Pixel N=%d, M1=%d, M2=%d, P1=%d\n",
214 pixel_n, pixel_m1, pixel_m2, pixel_p1);
215 printk(BIOS_DEBUG, "Pixel clock %d kHz\n",
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200216 BASE_FREQUENCY * (5 * (pixel_m1 + 2) + (pixel_m2 + 2)) /
217 (pixel_n + 2) / (pixel_p1 * pixel_p2));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200218
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200219 if (IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)) {
220 /* Disable panel fitter (we're in native resolution). */
221 write32(pmmio + PF_CTL(0), 0);
222 write32(pmmio + PF_WIN_SZ(0), 0);
223 write32(pmmio + PF_WIN_POS(0), 0);
224 write32(pmmio + PFIT_PGM_RATIOS, 0);
225 write32(pmmio + PFIT_CONTROL, 0);
226 } else {
227 write32(pmmio + PF_WIN_SZ(0), vactive | (hactive << 16));
228 write32(pmmio + PF_WIN_POS(0), 0);
229 write32(pmmio + PF_CTL(0), PF_ENABLE | PF_FILTER_MED_3x3);
230 write32(pmmio + PFIT_CONTROL, PFIT_ENABLE
231 | (1 << PFIT_PIPE_SHIFT) | HORIZ_AUTO_SCALE
232 | VERT_AUTO_SCALE);
233 }
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200234
235 mdelay(1);
236
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200237 write32(pmmio + DSPCNTR(0), DISPPLANE_BGRX888
238 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
239
240 mdelay(1);
241 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS
242 | (read32(pmmio + PP_CONTROL) & ~PANEL_UNLOCK_MASK));
243 write32(pmmio + FP0(1),
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200244 (pixel_n << 16)
245 | (pixel_m1 << 8) | pixel_m2);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200246 write32(pmmio + DPLL(1),
247 DPLL_VGA_MODE_DIS |
248 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200249 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200250 : DPLLB_LVDS_P2_CLOCK_DIV_14)
251 | (conf->gpu_lvds_use_spread_spectrum_clock
252 ? DPLL_INTEGRATED_CLOCK_VLV | DPLL_INTEGRATED_CRI_CLK_VLV
253 : 0)
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200254 | (0x10000 << (pixel_p1 - 1)));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200255 mdelay(1);
256 write32(pmmio + DPLL(1),
257 DPLL_VGA_MODE_DIS |
258 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200259 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200260 : DPLLB_LVDS_P2_CLOCK_DIV_14)
261 | ((conf->gpu_lvds_use_spread_spectrum_clock ? 3 : 0) << 13)
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200262 | (0x10000 << (pixel_p1 - 1)));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200263 mdelay(1);
264 write32(pmmio + HTOTAL(1),
265 ((hactive + right_border + hblank - 1) << 16)
266 | (hactive - 1));
267 write32(pmmio + HBLANK(1),
268 ((hactive + right_border + hblank - 1) << 16)
269 | (hactive + right_border - 1));
270 write32(pmmio + HSYNC(1),
271 ((hactive + right_border + hfront_porch + hsync - 1) << 16)
272 | (hactive + right_border + hfront_porch - 1));
273
274 write32(pmmio + VTOTAL(1), ((vactive + bottom_border + vblank - 1) << 16)
275 | (vactive - 1));
276 write32(pmmio + VBLANK(1), ((vactive + bottom_border + vblank - 1) << 16)
277 | (vactive + bottom_border - 1));
278 write32(pmmio + VSYNC(1),
Arthur Heymansc8c73a62016-10-13 14:12:45 +0200279 ((vactive + bottom_border + vfront_porch + vsync - 1) << 16)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200280 | (vactive + bottom_border + vfront_porch - 1));
281
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200282 if (IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)) {
283 write32(pmmio + PIPESRC(1), ((hactive - 1) << 16)
284 | (vactive - 1));
285 } else {
286 write32(pmmio + PIPESRC(1), (639 << 16) | 399);
287 }
288
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200289 mdelay(1);
290
291 write32(pmmio + DSPSIZE(0), (hactive - 1) | ((vactive - 1) << 16));
292 write32(pmmio + DSPPOS(0), 0);
293
294 /* Backlight init. */
295 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
296 write32(pmmio + FW_BLC, 0x011d011a);
297 write32(pmmio + FW_BLC2, 0x00000102);
298 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
299 write32(pmmio + FW_BLC_SELF, 0x0001003f);
300 write32(pmmio + FW_BLC, 0x011d0109);
301 write32(pmmio + FW_BLC2, 0x00000102);
302 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
303 write32(pmmio + BLC_PWM_CTL, conf->gpu_backlight);
304
305 edid.bytes_per_line = (edid.bytes_per_line + 63) & ~63;
306 write32(pmmio + DSPADDR(0), 0);
307 write32(pmmio + DSPSURF(0), 0);
308 write32(pmmio + DSPSTRIDE(0), edid.bytes_per_line);
309 write32(pmmio + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888
310 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
311 mdelay(1);
312
313 write32(pmmio + PIPECONF(1), PIPECONF_ENABLE);
314 write32(pmmio + LVDS, LVDS_ON
315 | (hpolarity << 20) | (vpolarity << 21)
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200316 | (mode->lvds_dual_channel ? LVDS_CLOCK_B_POWERUP_ALL
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200317 | LVDS_CLOCK_BOTH_POWERUP_ALL : 0)
318 | LVDS_CLOCK_A_POWERUP_ALL
319 | LVDS_PIPE(1));
320
321 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
322 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_RESET);
323 mdelay(1);
324 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS
325 | PANEL_POWER_ON | PANEL_POWER_RESET);
326
327 printk (BIOS_DEBUG, "waiting for panel powerup\n");
328 while (1) {
329 u32 reg32;
330 reg32 = read32(pmmio + PP_STATUS);
331 if ((reg32 & PP_SEQUENCE_MASK) == PP_SEQUENCE_NONE)
332 break;
333 }
334 printk (BIOS_DEBUG, "panel powered up\n");
335
336 write32(pmmio + PP_CONTROL, PANEL_POWER_ON | PANEL_POWER_RESET);
337
338 /* Clear interrupts. */
339 write32(pmmio + DEIIR, 0xffffffff);
340 write32(pmmio + SDEIIR, 0xffffffff);
341 write32(pmmio + IIR, 0xffffffff);
342 write32(pmmio + IMR, 0xffffffff);
343 write32(pmmio + EIR, 0xffffffff);
344
345 if (gtt_setup(pmmio)) {
346 printk(BIOS_ERR, "ERROR: GTT Setup Failed!!!\n");
347 return 0;
348 }
349
350 /* Setup GTT. */
351
352 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
353 uma_size = 0;
354 if (!(reg16 & 2)) {
Arthur Heymans874a8f92016-05-19 16:06:09 +0200355 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200356 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
357 }
358
Vladimir Serbinenko055fe032014-08-19 23:59:27 +0200359 for (i = 0; i < (uma_size - 256) / 4; i++)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200360 {
361 outl((i << 2) | 1, piobase);
362 outl(pphysbase + (i << 12) + 1, piobase + 4);
363 }
364
365 temp = read32(pmmio + PGETBL_CTL);
366 printk(BIOS_INFO, "GTT PGETBL_CTL register: 0x%lx\n", temp);
367
368 if (temp & 1)
369 printk(BIOS_INFO, "GTT Enabled\n");
370 else
371 printk(BIOS_ERR, "ERROR: GTT is still Disabled!!!\n");
372
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200373 if (IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)) {
374 printk(BIOS_SPEW, "memset %p to 0x00 for %d bytes\n",
375 (void *)pgfx, hactive * vactive * 4);
376 memset((void *)pgfx, 0x00, hactive * vactive * 4);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200377
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200378 set_vbe_mode_info_valid(&edid, pgfx);
379 } else {
380 vga_misc_write(0x67);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200381
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200382 write32(pmmio + DSPCNTR(0), DISPPLANE_SEL_PIPE_B);
383 write32(pmmio + VGACNTRL, 0x02c4008e
384 | VGA_PIPE_B_SELECT);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200385
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200386 vga_textmode_init();
387 }
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200388 return 0;
389}
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200390
391static int intel_gma_init_vga(struct northbridge_intel_i945_config *conf,
392 unsigned int pphysbase, unsigned int piobase,
393 void *pmmio, unsigned int pgfx)
394{
395 int i;
396 u32 hactive, vactive;
397 u16 reg16;
398 u32 uma_size;
399
400 printk(BIOS_SPEW, "pmmio %x addrport %x physbase %x\n",
401 (u32)pmmio, piobase, pphysbase);
402
403 gtt_setup(pmmio);
404
405 /* Disable VGA. */
406 write32(pmmio + VGACNTRL, VGA_DISP_DISABLE);
407
408 /* Disable pipes. */
409 write32(pmmio + PIPECONF(0), 0);
410 write32(pmmio + PIPECONF(1), 0);
411
412 write32(pmmio + INSTPM, 0x800);
413
414 vga_gr_write(0x18, 0);
415
416 write32(pmmio + VGA0, 0x200074);
417 write32(pmmio + VGA1, 0x200074);
418
419 write32(pmmio + DSPFW3, 0x7f3f00c1 & ~PINEVIEW_SELF_REFRESH_EN);
420 write32(pmmio + DSPCLK_GATE_D, 0);
421 write32(pmmio + FW_BLC, 0x03060106);
422 write32(pmmio + FW_BLC2, 0x00000306);
423
424 write32(pmmio + ADPA, ADPA_DAC_ENABLE
425 | ADPA_PIPE_A_SELECT
426 | ADPA_USE_VGA_HVPOLARITY
427 | ADPA_VSYNC_CNTL_ENABLE
428 | ADPA_HSYNC_CNTL_ENABLE
429 | ADPA_DPMS_ON
430 );
431
432 write32(pmmio + 0x7041c, 0x0);
433
434 write32(pmmio + DPLL_MD(0), 0x3);
435 write32(pmmio + DPLL_MD(1), 0x3);
436 write32(pmmio + DSPCNTR(1), 0x1000000);
437 write32(pmmio + PIPESRC(1), 0x027f01df);
438
439 vga_misc_write(0x67);
440 const u8 cr[] = { 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
441 0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x00, 0x00,
442 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
443 0xff
444 };
445 vga_cr_write(0x11, 0);
446
447 for (i = 0; i <= 0x18; i++)
448 vga_cr_write(i, cr[i]);
449
450 // Disable screen memory to prevent garbage from appearing.
451 vga_sr_write(1, vga_sr_read(1) | 0x20);
452 hactive = 640;
453 vactive = 400;
454
455 mdelay(1);
456 write32(pmmio + DPLL(0),
457 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
458 | DPLL_VGA_MODE_DIS
459 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
460 | 0x400601
461 );
462 mdelay(1);
463 write32(pmmio + DPLL(0),
464 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
465 | DPLL_VGA_MODE_DIS
466 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
467 | 0x400601
468 );
469
470 write32(pmmio + ADPA, ADPA_DAC_ENABLE
471 | ADPA_PIPE_A_SELECT
472 | ADPA_USE_VGA_HVPOLARITY
473 | ADPA_VSYNC_CNTL_ENABLE
474 | ADPA_HSYNC_CNTL_ENABLE
475 | ADPA_DPMS_ON
476 );
477
478 write32(pmmio + HTOTAL(0),
479 ((hactive - 1) << 16)
480 | (hactive - 1));
481 write32(pmmio + HBLANK(0),
482 ((hactive - 1) << 16)
483 | (hactive - 1));
484 write32(pmmio + HSYNC(0),
485 ((hactive - 1) << 16)
486 | (hactive - 1));
487
488 write32(pmmio + VTOTAL(0), ((vactive - 1) << 16)
489 | (vactive - 1));
490 write32(pmmio + VBLANK(0), ((vactive - 1) << 16)
491 | (vactive - 1));
492 write32(pmmio + VSYNC(0),
493 ((vactive - 1) << 16)
494 | (vactive - 1));
495
496 write32(pmmio + PF_WIN_POS(0), 0);
497
498 write32(pmmio + PIPESRC(0), (639 << 16) | 399);
499 write32(pmmio + PF_CTL(0),PF_ENABLE | PF_FILTER_MED_3x3);
500 write32(pmmio + PF_WIN_SZ(0), vactive | (hactive << 16));
501 write32(pmmio + PFIT_CONTROL, 0x0);
502
503 mdelay(1);
504
505 write32(pmmio + FDI_RX_CTL(0), 0x00002040);
506 mdelay(1);
507 write32(pmmio + FDI_RX_CTL(0), 0x80002050);
508 write32(pmmio + FDI_TX_CTL(0), 0x00044000);
509 mdelay(1);
510 write32(pmmio + FDI_TX_CTL(0), 0x80044000);
511 write32(pmmio + PIPECONF(0), PIPECONF_ENABLE | PIPECONF_BPP_6 | PIPECONF_DITHER_EN);
512
513 write32(pmmio + VGACNTRL, 0x0);
514 write32(pmmio + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888);
515 mdelay(1);
516
517 write32(pmmio + ADPA, ADPA_DAC_ENABLE
518 | ADPA_PIPE_A_SELECT
519 | ADPA_USE_VGA_HVPOLARITY
520 | ADPA_VSYNC_CNTL_ENABLE
521 | ADPA_HSYNC_CNTL_ENABLE
522 | ADPA_DPMS_ON
523 );
524
525 write32(pmmio + DSPFW3, 0x7f3f00c1);
526 write32(pmmio + MI_MODE, 0x200 | VS_TIMER_DISPATCH);
527 write32(pmmio + CACHE_MODE_0, (0x6820 | (1 << 9)) & ~(1 << 5));
528 write32(pmmio + CACHE_MODE_1, 0x380 & ~(1 << 9));
529
530 /* Set up GTT. */
531
532 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
533 uma_size = 0;
534 if (!(reg16 & 2)) {
535 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
536 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
537 }
538
539 for (i = 0; i < (uma_size - 256) / 4; i++)
540 {
541 outl((i << 2) | 1, piobase);
542 outl(pphysbase + (i << 12) + 1, piobase + 4);
543 }
544
545 /* Clear interrupts. */
546 write32(pmmio + DEIIR, 0xffffffff);
547 write32(pmmio + SDEIIR, 0xffffffff);
548 write32(pmmio + IIR, 0xffffffff);
549 write32(pmmio + IMR, 0xffffffff);
550 write32(pmmio + EIR, 0xffffffff);
551
552 vga_textmode_init();
553
554 /* Enable screen memory. */
555 vga_sr_write(1, vga_sr_read(1) & ~0x20);
556
557 return 0;
558
559}
560
561/* compare the header of the vga edid header */
562/* if vga is not connected it should have a correct header */
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200563static int probe_edid(u8 *pmmio, u8 slave)
564{
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200565 u8 vga_edid[128];
566 u8 header[8] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200567 intel_gmbus_read_edid(pmmio + GMBUS0, slave, 0x50, vga_edid, 128);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200568 intel_gmbus_stop(pmmio + GMBUS0);
569 for (int i = 0; i < 8; i++) {
570 if (vga_edid[i] != header[i]) {
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200571 printk(BIOS_DEBUG, "No display connected on slave %d\n",
572 slave);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200573 return 0;
574 }
575 }
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200576 printk(BIOS_SPEW, "Found a display on slave %d\n", slave);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200577 return 1;
578}
579
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200580
Stefan Reinauer30140a52009-03-11 16:20:39 +0000581static void gma_func0_init(struct device *dev)
582{
583 u32 reg32;
584
Patrick Georgi6444bd42012-07-06 11:31:39 +0200585 /* Unconditionally reset graphics */
586 pci_write_config8(dev, GDRST, 1);
587 udelay(50);
588 pci_write_config8(dev, GDRST, 0);
589 /* wait for device to finish */
590 while (pci_read_config8(dev, GDRST) & 1) { };
591
Stefan Reinauer30140a52009-03-11 16:20:39 +0000592 /* IGD needs to be Bus Master */
593 reg32 = pci_read_config32(dev, PCI_COMMAND);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200594 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER
595 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
Denis 'GNUtoo' Cariklied7e29e2013-02-24 12:01:44 +0100596
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200597 if (IS_ENABLED(CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT)) {
598 /* PCI Init, will run VBIOS */
599 pci_dev_init(dev);
Arthur Heymans333176e2016-09-07 22:10:57 +0200600 }
Arthur Heymans9c5fc622016-10-18 02:15:44 +0200601
602 if (IS_ENABLED(CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT)) {
603 /* This should probably run before post VBIOS init. */
604 printk(BIOS_SPEW, "Initializing VGA without OPROM.\n");
605 void *mmiobase;
606 u32 iobase, graphics_base;
607 struct northbridge_intel_i945_config *conf = dev->chip_info;
608
609 iobase = dev->resource_list[1].base;
610 mmiobase = (void *)(uintptr_t)dev->resource_list[0].base;
611 graphics_base = dev->resource_list[2].base;
612
613 printk(BIOS_SPEW, "GMADR = 0x%08x GTTADR = 0x%08x\n",
614 pci_read_config32(dev, GMADR),
615 pci_read_config32(dev, GTTADR)
616 );
617
618 int err;
619 /* probe if VGA is connected and alway run */
620 /* VGA init if no LVDS is connected */
621 if (!probe_edid(mmiobase, 3) || probe_edid(mmiobase, 2))
622 err = intel_gma_init_vga(conf,
623 pci_read_config32(dev, 0x5c) & ~0xf,
624 iobase, mmiobase, graphics_base);
625 else
626 err = intel_gma_init_lvds(conf,
627 pci_read_config32(dev, 0x5c) & ~0xf,
628 iobase, mmiobase, graphics_base);
629 if (err == 0)
630 gfx_set_init_done(1);
631 /* Linux relies on VBT for panel info. */
632 if (CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GM) {
633 generate_fake_intel_oprom(&conf->gfx, dev,
634 "$VBT CALISTOGA");
635 }
636 if (CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GC) {
637 generate_fake_intel_oprom(&conf->gfx, dev,
638 "$VBT LAKEPORT-G");
639 }
Arthur Heymans333176e2016-09-07 22:10:57 +0200640 }
Stefan Reinauer30140a52009-03-11 16:20:39 +0000641}
642
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200643/* This doesn't reclaim stolen UMA memory, but IGD could still
644 be reenabled later. */
645static void gma_func0_disable(struct device *dev)
646{
647 struct device *dev_host = dev_find_slot(0, PCI_DEVFN(0x0, 0));
648
649 pci_write_config16(dev, GCFC, 0xa00);
650 pci_write_config16(dev_host, GGC, (1 << 1));
651
652 unsigned int reg32 = pci_read_config32(dev_host, DEVEN);
653 reg32 &= ~(DEVEN_D2F0 | DEVEN_D2F1);
654 pci_write_config32(dev_host, DEVEN, reg32);
655
656 dev->enabled = 0;
657}
658
Stefan Reinauer30140a52009-03-11 16:20:39 +0000659static void gma_func1_init(struct device *dev)
660{
661 u32 reg32;
Alexander Couzensc7a1a3e2016-03-09 10:42:58 +0100662 u8 val;
Stefan Reinauer30140a52009-03-11 16:20:39 +0000663
664 /* IGD needs to be Bus Master, also enable IO accesss */
665 reg32 = pci_read_config32(dev, PCI_COMMAND);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000666 pci_write_config32(dev, PCI_COMMAND, reg32 |
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200667 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Sven Schnelleb629d142011-06-12 14:30:10 +0200668
Alexander Couzensc7a1a3e2016-03-09 10:42:58 +0100669 if (get_option(&val, "tft_brightness") == CB_SUCCESS)
670 pci_write_config8(dev, 0xf4, val);
671 else
672 pci_write_config8(dev, 0xf4, 0xff);
Stefan Reinauer30140a52009-03-11 16:20:39 +0000673}
674
675static void gma_set_subsystem(device_t dev, unsigned vendor, unsigned device)
676{
677 if (!vendor || !device) {
678 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
679 pci_read_config32(dev, PCI_VENDOR_ID));
680 } else {
681 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
682 ((device & 0xffff) << 16) | (vendor & 0xffff));
683 }
684}
685
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100686const struct i915_gpu_controller_info *
687intel_gma_get_controller_info(void)
688{
689 device_t dev = dev_find_slot(0, PCI_DEVFN(0x2,0));
690 if (!dev) {
691 return NULL;
692 }
693 struct northbridge_intel_i945_config *chip = dev->chip_info;
Patrick Georgi54e227e2015-08-08 22:02:12 +0200694 if (!chip) {
695 return NULL;
696 }
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100697 return &chip->gfx;
698}
699
Alexander Couzens5eea4582015-04-12 22:18:55 +0200700static void gma_ssdt(device_t device)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100701{
702 const struct i915_gpu_controller_info *gfx = intel_gma_get_controller_info();
703 if (!gfx) {
704 return;
705 }
706
707 drivers_intel_gma_displays_ssdt_generate(gfx);
708}
709
Stefan Reinauer30140a52009-03-11 16:20:39 +0000710static struct pci_operations gma_pci_ops = {
711 .set_subsystem = gma_set_subsystem,
712};
713
714static struct device_operations gma_func0_ops = {
715 .read_resources = pci_dev_read_resources,
716 .set_resources = pci_dev_set_resources,
717 .enable_resources = pci_dev_enable_resources,
718 .init = gma_func0_init,
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100719 .acpi_fill_ssdt_generator = gma_ssdt,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000720 .scan_bus = 0,
721 .enable = 0,
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200722 .disable = gma_func0_disable,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000723 .ops_pci = &gma_pci_ops,
724};
725
726
727static struct device_operations gma_func1_ops = {
728 .read_resources = pci_dev_read_resources,
729 .set_resources = pci_dev_set_resources,
730 .enable_resources = pci_dev_enable_resources,
731 .init = gma_func1_init,
732 .scan_bus = 0,
733 .enable = 0,
734 .ops_pci = &gma_pci_ops,
735};
736
Arthur Heymans0b9ecb52016-09-06 23:03:04 +0200737static const unsigned short pci_device_ids[] = { 0x27a2, 0x27ae, 0x2772, 0 };
Vladimir Serbinenko10dd0e32014-11-17 00:07:12 +0100738
Stefan Reinauer30140a52009-03-11 16:20:39 +0000739static const struct pci_driver i945_gma_func0_driver __pci_driver = {
740 .ops = &gma_func0_ops,
741 .vendor = PCI_VENDOR_ID_INTEL,
Vladimir Serbinenko10dd0e32014-11-17 00:07:12 +0100742 .devices = pci_device_ids,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000743};
744
745static const struct pci_driver i945_gma_func1_driver __pci_driver = {
746 .ops = &gma_func1_ops,
747 .vendor = PCI_VENDOR_ID_INTEL,
748 .device = 0x27a6,
749};