blob: e40b9f00a8fcf56cd7f06e2e1e1e2a4a8dbee5da [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
49#if CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT
50
Francis Rowe71512b22015-03-16 05:31:40 +000051static int gtt_setup(void *mmiobase)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020052{
53 unsigned long PGETBL_save;
Paul Menzelcc95f182014-06-05 22:45:35 +020054 unsigned long tom; // top of memory
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020055
Paul Menzelcc95f182014-06-05 22:45:35 +020056 /*
57 * The Video BIOS places the GTT right below top of memory.
Denis 'GNUtoo' Carikli16110e72014-10-14 07:33:53 +020058 */
Paul Menzelcc95f182014-06-05 22:45:35 +020059 tom = pci_read_config8(dev_find_slot(0, PCI_DEVFN(0, 0)), TOLUD) << 24;
60 PGETBL_save = tom - 256 * KiB;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020061 PGETBL_save |= PGETBL_ENABLED;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020062 PGETBL_save |= 2; /* set GTT to 256kb */
63
64 write32(mmiobase + GFX_FLSH_CNTL, 0);
65
66 write32(mmiobase + PGETBL_CTL, PGETBL_save);
67
68 /* verify */
69 if (read32(mmiobase + PGETBL_CTL) & PGETBL_ENABLED) {
70 printk(BIOS_DEBUG, "gtt_setup is enabled.\n");
71 } else {
72 printk(BIOS_DEBUG, "gtt_setup failed!!!\n");
73 return 1;
74 }
75 write32(mmiobase + GFX_FLSH_CNTL, 0);
76
77 return 0;
78}
79
Arthur Heymansb59bcb22016-09-05 22:46:11 +020080static int intel_gma_init_lvds(struct northbridge_intel_i945_config *conf,
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020081 unsigned int pphysbase, unsigned int piobase,
Francis Rowe71512b22015-03-16 05:31:40 +000082 void *pmmio, unsigned int pgfx)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020083{
84 struct edid edid;
Mono2e4f83b2015-09-07 21:15:26 +020085 struct edid_mode *mode;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020086 u8 edid_data[128];
87 unsigned long temp;
88 int hpolarity, vpolarity;
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020089 u32 smallest_err = 0xffffffff;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020090 u32 target_frequency;
91 u32 pixel_p1 = 1;
Arthur Heymans7dfc8a52016-09-02 22:35:32 +020092 u32 pixel_p2;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +020093 u32 pixel_n = 1;
94 u32 pixel_m1 = 1;
95 u32 pixel_m2 = 1;
96 u32 hactive, vactive, right_border, bottom_border;
97 u32 vsync, hsync, vblank, hblank, hfront_porch, vfront_porch;
98 u32 i, j;
99 u32 uma_size;
100 u16 reg16;
101
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200102 printk(BIOS_SPEW,
Francis Rowe71512b22015-03-16 05:31:40 +0000103 "i915lightup: graphics %p mmio %p addrport %04x physbase %08x\n",
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200104 (void *)pgfx, pmmio, piobase, pphysbase);
105
Arthur Heymans7141ff32016-10-10 17:49:00 +0200106 intel_gmbus_read_edid(pmmio + GMBUS0, 3, 0x50, edid_data,
107 sizeof(edid_data));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200108 decode_edid(edid_data, sizeof(edid_data), &edid);
Mono2e4f83b2015-09-07 21:15:26 +0200109 mode = &edid.mode;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200110
Mono2e4f83b2015-09-07 21:15:26 +0200111 hpolarity = (mode->phsync == '-');
112 vpolarity = (mode->pvsync == '-');
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200113 hactive = edid.x_resolution;
114 vactive = edid.y_resolution;
Mono2e4f83b2015-09-07 21:15:26 +0200115 right_border = mode->hborder;
116 bottom_border = mode->vborder;
117 vblank = mode->vbl;
118 hblank = mode->hbl;
119 vsync = mode->vspw;
120 hsync = mode->hspw;
121 hfront_porch = mode->hso;
122 vfront_porch = mode->vso;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200123
124 for (i = 0; i < 2; i++)
125 for (j = 0; j < 0x100; j++)
Elyes HAOUAS0a15fe92016-09-17 19:12:27 +0200126 /* R = j, G = j, B = j. */
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200127 write32(pmmio + PALETTE(i) + 4 * j, 0x10101 * j);
128
129 write32(pmmio + PCH_PP_CONTROL, PANEL_UNLOCK_REGS
130 | (read32(pmmio + PCH_PP_CONTROL) & ~PANEL_UNLOCK_MASK));
131
132 write32(pmmio + MI_ARB_STATE, MI_ARB_C3_LP_WRITE_ENABLE | (1 << 27));
133 /* Clean registers. */
134 for (i = 0; i < 0x20; i += 4)
135 write32(pmmio + RENDER_RING_BASE + i, 0);
136 for (i = 0; i < 0x20; i += 4)
137 write32(pmmio + FENCE_REG_965_0 + i, 0);
138 write32(pmmio + PP_ON_DELAYS, 0);
139 write32(pmmio + PP_OFF_DELAYS, 0);
140
141 /* Disable VGA. */
142 write32(pmmio + VGACNTRL, VGA_DISP_DISABLE);
143
144 /* Disable pipes. */
145 write32(pmmio + PIPECONF(0), 0);
146 write32(pmmio + PIPECONF(1), 0);
147
148 /* Init PRB0. */
149 write32(pmmio + HWS_PGA, 0x352d2000);
150 write32(pmmio + PRB0_CTL, 0);
151 write32(pmmio + PRB0_HEAD, 0);
152 write32(pmmio + PRB0_TAIL, 0);
153 write32(pmmio + PRB0_START, 0);
154 write32(pmmio + PRB0_CTL, 0x0001f001);
155
156 write32(pmmio + D_STATE, DSTATE_PLL_D3_OFF
157 | DSTATE_GFX_CLOCK_GATING | DSTATE_DOT_CLOCK_GATING);
158 write32(pmmio + ECOSKPD, 0x00010000);
159 write32(pmmio + HWSTAM, 0xeffe);
160 write32(pmmio + PORT_HOTPLUG_EN, conf->gpu_hotplug);
161 write32(pmmio + INSTPM, 0x08000000 | INSTPM_AGPBUSY_DIS);
162
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200163 /* p2 divisor must 7 for dual channel LVDS */
164 /* and 14 for single channel LVDS */
165 pixel_p2 = mode->lvds_dual_channel ? 7 : 14;
166 target_frequency = mode->pixel_clock;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200167
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200168 /* Find suitable divisors, m1, m2, p1, n. */
169 /* refclock * (5 * (m1 + 2) + (m1 + 2)) / (n + 2) / p1 / p2 */
170 /* should be closest to target frequency as possible */
171 u32 candn, candm1, candm2, candp1;
172 for (candm1 = 8; candm1 <= 18; candm1++) {
173 for (candm2 = 3; candm2 <= 7; candm2++) {
174 for (candn = 1; candn <= 6; candn++) {
175 for (candp1 = 1; candp1 <= 8; candp1++) {
176 u32 m = 5 * (candm1 + 2) + (candm2 + 2);
177 u32 p = candp1 * pixel_p2;
178 u32 vco = DIV_ROUND_CLOSEST(BASE_FREQUENCY * m, candn + 2);
179 u32 dot = DIV_ROUND_CLOSEST(vco, p);
180 u32 this_err = ABS(dot - target_frequency);
181 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
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200220#if !IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)
221 write32(pmmio + PF_WIN_SZ(0), vactive | (hactive << 16));
222 write32(pmmio + PF_WIN_POS(0), 0);
223 write32(pmmio + PF_CTL(0),PF_ENABLE | PF_FILTER_MED_3x3);
224 write32(pmmio + PFIT_CONTROL, PFIT_ENABLE | (1 << PFIT_PIPE_SHIFT) | HORIZ_AUTO_SCALE | VERT_AUTO_SCALE);
225#else
226 /* Disable panel fitter (we're in native resolution). */
227 write32(pmmio + PF_CTL(0), 0);
228 write32(pmmio + PF_WIN_SZ(0), 0);
229 write32(pmmio + PF_WIN_POS(0), 0);
230 write32(pmmio + PFIT_PGM_RATIOS, 0);
231 write32(pmmio + PFIT_CONTROL, 0);
232#endif
233
234 mdelay(1);
235
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200236 write32(pmmio + DSPCNTR(0), DISPPLANE_BGRX888
237 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
238
239 mdelay(1);
240 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS
241 | (read32(pmmio + PP_CONTROL) & ~PANEL_UNLOCK_MASK));
242 write32(pmmio + FP0(1),
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200243 (pixel_n << 16)
244 | (pixel_m1 << 8) | pixel_m2);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200245 write32(pmmio + DPLL(1),
246 DPLL_VGA_MODE_DIS |
247 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200248 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200249 : DPLLB_LVDS_P2_CLOCK_DIV_14)
250 | (conf->gpu_lvds_use_spread_spectrum_clock
251 ? DPLL_INTEGRATED_CLOCK_VLV | DPLL_INTEGRATED_CRI_CLK_VLV
252 : 0)
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200253 | (0x10000 << (pixel_p1 - 1)));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200254 mdelay(1);
255 write32(pmmio + DPLL(1),
256 DPLL_VGA_MODE_DIS |
257 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200258 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200259 : DPLLB_LVDS_P2_CLOCK_DIV_14)
260 | ((conf->gpu_lvds_use_spread_spectrum_clock ? 3 : 0) << 13)
Arthur Heymans7dfc8a52016-09-02 22:35:32 +0200261 | (0x10000 << (pixel_p1 - 1)));
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200262 mdelay(1);
263 write32(pmmio + HTOTAL(1),
264 ((hactive + right_border + hblank - 1) << 16)
265 | (hactive - 1));
266 write32(pmmio + HBLANK(1),
267 ((hactive + right_border + hblank - 1) << 16)
268 | (hactive + right_border - 1));
269 write32(pmmio + HSYNC(1),
270 ((hactive + right_border + hfront_porch + hsync - 1) << 16)
271 | (hactive + right_border + hfront_porch - 1));
272
273 write32(pmmio + VTOTAL(1), ((vactive + bottom_border + vblank - 1) << 16)
274 | (vactive - 1));
275 write32(pmmio + VBLANK(1), ((vactive + bottom_border + vblank - 1) << 16)
276 | (vactive + bottom_border - 1));
277 write32(pmmio + VSYNC(1),
Arthur Heymansc8c73a62016-10-13 14:12:45 +0200278 ((vactive + bottom_border + vfront_porch + vsync - 1) << 16)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200279 | (vactive + bottom_border + vfront_porch - 1));
280
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200281#if !IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)
282 write32(pmmio + PIPESRC(1), (639 << 16) | 399);
283#else
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200284 write32(pmmio + PIPESRC(1), ((hactive - 1) << 16) | (vactive - 1));
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200285#endif
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200286 mdelay(1);
287
288 write32(pmmio + DSPSIZE(0), (hactive - 1) | ((vactive - 1) << 16));
289 write32(pmmio + DSPPOS(0), 0);
290
291 /* Backlight init. */
292 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
293 write32(pmmio + FW_BLC, 0x011d011a);
294 write32(pmmio + FW_BLC2, 0x00000102);
295 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
296 write32(pmmio + FW_BLC_SELF, 0x0001003f);
297 write32(pmmio + FW_BLC, 0x011d0109);
298 write32(pmmio + FW_BLC2, 0x00000102);
299 write32(pmmio + FW_BLC_SELF, FW_BLC_SELF_EN_MASK);
300 write32(pmmio + BLC_PWM_CTL, conf->gpu_backlight);
301
302 edid.bytes_per_line = (edid.bytes_per_line + 63) & ~63;
303 write32(pmmio + DSPADDR(0), 0);
304 write32(pmmio + DSPSURF(0), 0);
305 write32(pmmio + DSPSTRIDE(0), edid.bytes_per_line);
306 write32(pmmio + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888
307 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
308 mdelay(1);
309
310 write32(pmmio + PIPECONF(1), PIPECONF_ENABLE);
311 write32(pmmio + LVDS, LVDS_ON
312 | (hpolarity << 20) | (vpolarity << 21)
Vladimir Serbinenko551cff02015-10-10 23:58:08 +0200313 | (mode->lvds_dual_channel ? LVDS_CLOCK_B_POWERUP_ALL
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200314 | LVDS_CLOCK_BOTH_POWERUP_ALL : 0)
315 | LVDS_CLOCK_A_POWERUP_ALL
316 | LVDS_PIPE(1));
317
318 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
319 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_RESET);
320 mdelay(1);
321 write32(pmmio + PP_CONTROL, PANEL_UNLOCK_REGS
322 | PANEL_POWER_ON | PANEL_POWER_RESET);
323
324 printk (BIOS_DEBUG, "waiting for panel powerup\n");
325 while (1) {
326 u32 reg32;
327 reg32 = read32(pmmio + PP_STATUS);
328 if ((reg32 & PP_SEQUENCE_MASK) == PP_SEQUENCE_NONE)
329 break;
330 }
331 printk (BIOS_DEBUG, "panel powered up\n");
332
333 write32(pmmio + PP_CONTROL, PANEL_POWER_ON | PANEL_POWER_RESET);
334
335 /* Clear interrupts. */
336 write32(pmmio + DEIIR, 0xffffffff);
337 write32(pmmio + SDEIIR, 0xffffffff);
338 write32(pmmio + IIR, 0xffffffff);
339 write32(pmmio + IMR, 0xffffffff);
340 write32(pmmio + EIR, 0xffffffff);
341
342 if (gtt_setup(pmmio)) {
343 printk(BIOS_ERR, "ERROR: GTT Setup Failed!!!\n");
344 return 0;
345 }
346
347 /* Setup GTT. */
348
349 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
350 uma_size = 0;
351 if (!(reg16 & 2)) {
Arthur Heymans874a8f92016-05-19 16:06:09 +0200352 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200353 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
354 }
355
Vladimir Serbinenko055fe032014-08-19 23:59:27 +0200356 for (i = 0; i < (uma_size - 256) / 4; i++)
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200357 {
358 outl((i << 2) | 1, piobase);
359 outl(pphysbase + (i << 12) + 1, piobase + 4);
360 }
361
362 temp = read32(pmmio + PGETBL_CTL);
363 printk(BIOS_INFO, "GTT PGETBL_CTL register: 0x%lx\n", temp);
364
365 if (temp & 1)
366 printk(BIOS_INFO, "GTT Enabled\n");
367 else
368 printk(BIOS_ERR, "ERROR: GTT is still Disabled!!!\n");
369
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200370#if !IS_ENABLED(CONFIG_FRAMEBUFFER_KEEP_VESA_MODE)
371 vga_misc_write(0x67);
372
373 write32(pmmio + DSPCNTR(0), DISPPLANE_SEL_PIPE_B);
374
375 write32(pmmio + VGACNTRL, 0x02c4008e | VGA_PIPE_B_SELECT);
376
377 vga_textmode_init();
378#else
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200379 printk(BIOS_SPEW, "memset %p to 0x00 for %d bytes\n",
380 (void *)pgfx, hactive * vactive * 4);
381 memset((void *)pgfx, 0x00, hactive * vactive * 4);
382
383 set_vbe_mode_info_valid(&edid, pgfx);
Vladimir Serbinenko0092c992014-08-21 01:06:53 +0200384#endif
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200385 return 0;
386}
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200387
388static int intel_gma_init_vga(struct northbridge_intel_i945_config *conf,
389 unsigned int pphysbase, unsigned int piobase,
390 void *pmmio, unsigned int pgfx)
391{
392 int i;
393 u32 hactive, vactive;
394 u16 reg16;
395 u32 uma_size;
396
397 printk(BIOS_SPEW, "pmmio %x addrport %x physbase %x\n",
398 (u32)pmmio, piobase, pphysbase);
399
400 gtt_setup(pmmio);
401
402 /* Disable VGA. */
403 write32(pmmio + VGACNTRL, VGA_DISP_DISABLE);
404
405 /* Disable pipes. */
406 write32(pmmio + PIPECONF(0), 0);
407 write32(pmmio + PIPECONF(1), 0);
408
409 write32(pmmio + INSTPM, 0x800);
410
411 vga_gr_write(0x18, 0);
412
413 write32(pmmio + VGA0, 0x200074);
414 write32(pmmio + VGA1, 0x200074);
415
416 write32(pmmio + DSPFW3, 0x7f3f00c1 & ~PINEVIEW_SELF_REFRESH_EN);
417 write32(pmmio + DSPCLK_GATE_D, 0);
418 write32(pmmio + FW_BLC, 0x03060106);
419 write32(pmmio + FW_BLC2, 0x00000306);
420
421 write32(pmmio + ADPA, ADPA_DAC_ENABLE
422 | ADPA_PIPE_A_SELECT
423 | ADPA_USE_VGA_HVPOLARITY
424 | ADPA_VSYNC_CNTL_ENABLE
425 | ADPA_HSYNC_CNTL_ENABLE
426 | ADPA_DPMS_ON
427 );
428
429 write32(pmmio + 0x7041c, 0x0);
430
431 write32(pmmio + DPLL_MD(0), 0x3);
432 write32(pmmio + DPLL_MD(1), 0x3);
433 write32(pmmio + DSPCNTR(1), 0x1000000);
434 write32(pmmio + PIPESRC(1), 0x027f01df);
435
436 vga_misc_write(0x67);
437 const u8 cr[] = { 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
438 0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x00, 0x00,
439 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
440 0xff
441 };
442 vga_cr_write(0x11, 0);
443
444 for (i = 0; i <= 0x18; i++)
445 vga_cr_write(i, cr[i]);
446
447 // Disable screen memory to prevent garbage from appearing.
448 vga_sr_write(1, vga_sr_read(1) | 0x20);
449 hactive = 640;
450 vactive = 400;
451
452 mdelay(1);
453 write32(pmmio + DPLL(0),
454 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
455 | DPLL_VGA_MODE_DIS
456 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
457 | 0x400601
458 );
459 mdelay(1);
460 write32(pmmio + DPLL(0),
461 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
462 | DPLL_VGA_MODE_DIS
463 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
464 | 0x400601
465 );
466
467 write32(pmmio + ADPA, ADPA_DAC_ENABLE
468 | ADPA_PIPE_A_SELECT
469 | ADPA_USE_VGA_HVPOLARITY
470 | ADPA_VSYNC_CNTL_ENABLE
471 | ADPA_HSYNC_CNTL_ENABLE
472 | ADPA_DPMS_ON
473 );
474
475 write32(pmmio + HTOTAL(0),
476 ((hactive - 1) << 16)
477 | (hactive - 1));
478 write32(pmmio + HBLANK(0),
479 ((hactive - 1) << 16)
480 | (hactive - 1));
481 write32(pmmio + HSYNC(0),
482 ((hactive - 1) << 16)
483 | (hactive - 1));
484
485 write32(pmmio + VTOTAL(0), ((vactive - 1) << 16)
486 | (vactive - 1));
487 write32(pmmio + VBLANK(0), ((vactive - 1) << 16)
488 | (vactive - 1));
489 write32(pmmio + VSYNC(0),
490 ((vactive - 1) << 16)
491 | (vactive - 1));
492
493 write32(pmmio + PF_WIN_POS(0), 0);
494
495 write32(pmmio + PIPESRC(0), (639 << 16) | 399);
496 write32(pmmio + PF_CTL(0),PF_ENABLE | PF_FILTER_MED_3x3);
497 write32(pmmio + PF_WIN_SZ(0), vactive | (hactive << 16));
498 write32(pmmio + PFIT_CONTROL, 0x0);
499
500 mdelay(1);
501
502 write32(pmmio + FDI_RX_CTL(0), 0x00002040);
503 mdelay(1);
504 write32(pmmio + FDI_RX_CTL(0), 0x80002050);
505 write32(pmmio + FDI_TX_CTL(0), 0x00044000);
506 mdelay(1);
507 write32(pmmio + FDI_TX_CTL(0), 0x80044000);
508 write32(pmmio + PIPECONF(0), PIPECONF_ENABLE | PIPECONF_BPP_6 | PIPECONF_DITHER_EN);
509
510 write32(pmmio + VGACNTRL, 0x0);
511 write32(pmmio + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888);
512 mdelay(1);
513
514 write32(pmmio + ADPA, ADPA_DAC_ENABLE
515 | ADPA_PIPE_A_SELECT
516 | ADPA_USE_VGA_HVPOLARITY
517 | ADPA_VSYNC_CNTL_ENABLE
518 | ADPA_HSYNC_CNTL_ENABLE
519 | ADPA_DPMS_ON
520 );
521
522 write32(pmmio + DSPFW3, 0x7f3f00c1);
523 write32(pmmio + MI_MODE, 0x200 | VS_TIMER_DISPATCH);
524 write32(pmmio + CACHE_MODE_0, (0x6820 | (1 << 9)) & ~(1 << 5));
525 write32(pmmio + CACHE_MODE_1, 0x380 & ~(1 << 9));
526
527 /* Set up GTT. */
528
529 reg16 = pci_read_config16(dev_find_slot(0, PCI_DEVFN(0, 0)), GGC);
530 uma_size = 0;
531 if (!(reg16 & 2)) {
532 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
533 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
534 }
535
536 for (i = 0; i < (uma_size - 256) / 4; i++)
537 {
538 outl((i << 2) | 1, piobase);
539 outl(pphysbase + (i << 12) + 1, piobase + 4);
540 }
541
542 /* Clear interrupts. */
543 write32(pmmio + DEIIR, 0xffffffff);
544 write32(pmmio + SDEIIR, 0xffffffff);
545 write32(pmmio + IIR, 0xffffffff);
546 write32(pmmio + IMR, 0xffffffff);
547 write32(pmmio + EIR, 0xffffffff);
548
549 vga_textmode_init();
550
551 /* Enable screen memory. */
552 vga_sr_write(1, vga_sr_read(1) & ~0x20);
553
554 return 0;
555
556}
557
558/* compare the header of the vga edid header */
559/* if vga is not connected it should have a correct header */
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200560static int probe_edid(u8 *pmmio, u8 slave)
561{
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200562 u8 vga_edid[128];
563 u8 header[8] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200564 intel_gmbus_read_edid(pmmio + GMBUS0, slave, 0x50, vga_edid, 128);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200565 intel_gmbus_stop(pmmio + GMBUS0);
566 for (int i = 0; i < 8; i++) {
567 if (vga_edid[i] != header[i]) {
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200568 printk(BIOS_DEBUG, "No display connected on slave %d\n",
569 slave);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200570 return 0;
571 }
572 }
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200573 printk(BIOS_SPEW, "Found a display on slave %d\n", slave);
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200574 return 1;
575}
576
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200577#endif
578
Stefan Reinauer30140a52009-03-11 16:20:39 +0000579static void gma_func0_init(struct device *dev)
580{
581 u32 reg32;
582
Patrick Georgi6444bd42012-07-06 11:31:39 +0200583 /* Unconditionally reset graphics */
584 pci_write_config8(dev, GDRST, 1);
585 udelay(50);
586 pci_write_config8(dev, GDRST, 0);
587 /* wait for device to finish */
588 while (pci_read_config8(dev, GDRST) & 1) { };
589
Stefan Reinauer30140a52009-03-11 16:20:39 +0000590 /* IGD needs to be Bus Master */
591 reg32 = pci_read_config32(dev, PCI_COMMAND);
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200592 pci_write_config32(dev, PCI_COMMAND, reg32 | PCI_COMMAND_MASTER
593 | PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
Denis 'GNUtoo' Cariklied7e29e2013-02-24 12:01:44 +0100594
595#if !CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT
596 /* PCI Init, will run VBIOS */
Stefan Reinauer30140a52009-03-11 16:20:39 +0000597 pci_dev_init(dev);
Denis 'GNUtoo' Cariklied7e29e2013-02-24 12:01:44 +0100598#endif
599
600
601#if CONFIG_MAINBOARD_DO_NATIVE_VGA_INIT
602 /* This should probably run before post VBIOS init. */
603 printk(BIOS_SPEW, "Initializing VGA without OPROM.\n");
Francis Rowe71512b22015-03-16 05:31:40 +0000604 void *mmiobase;
605 u32 iobase, graphics_base;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200606 struct northbridge_intel_i945_config *conf = dev->chip_info;
607
Denis 'GNUtoo' Cariklied7e29e2013-02-24 12:01:44 +0100608 iobase = dev->resource_list[1].base;
Francis Rowe71512b22015-03-16 05:31:40 +0000609 mmiobase = (void *)(uintptr_t)dev->resource_list[0].base;
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200610 graphics_base = dev->resource_list[2].base;
Denis 'GNUtoo' Cariklied7e29e2013-02-24 12:01:44 +0100611
Elyes HAOUAS0a15fe92016-09-17 19:12:27 +0200612 printk(BIOS_SPEW, "GMADR = 0x%08x GTTADR = 0x%08x\n",
Paul Menzeld235da12014-06-03 00:15:30 +0200613 pci_read_config32(dev, GMADR),
614 pci_read_config32(dev, GTTADR)
Peter Stugec6f09972013-06-08 01:31:44 +0200615 );
616
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200617 int err;
Arthur Heymans62f4dad2016-09-06 23:53:32 +0200618 /* probe if VGA is connected and alway run */
619 /* VGA init if no LVDS is connected */
620 if (!probe_edid(mmiobase, 3) || probe_edid(mmiobase, 2))
Arthur Heymansb59bcb22016-09-05 22:46:11 +0200621 err = intel_gma_init_vga(conf, pci_read_config32(dev, 0x5c) & ~0xf,
622 iobase, mmiobase, graphics_base);
623 else
624 err = intel_gma_init_lvds(conf, pci_read_config32(dev, 0x5c) & ~0xf,
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200625 iobase, mmiobase, graphics_base);
626 if (err == 0)
Kyösti Mälkkiab56b3b2013-11-28 16:44:51 +0200627 gfx_set_init_done(1);
Arthur Heymans333176e2016-09-07 22:10:57 +0200628 /* Linux relies on VBT for panel info. */
629 if (CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GM) {
630 generate_fake_intel_oprom(&conf->gfx, dev,
631 "$VBT CALISTOGA");
632 }
633 if (CONFIG_NORTHBRIDGE_INTEL_SUBTYPE_I945GC) {
634 generate_fake_intel_oprom(&conf->gfx, dev,
635 "$VBT LAKEPORT-G");
636 }
Denis 'GNUtoo' Cariklied7e29e2013-02-24 12:01:44 +0100637#endif
Stefan Reinauer30140a52009-03-11 16:20:39 +0000638}
639
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200640/* This doesn't reclaim stolen UMA memory, but IGD could still
641 be reenabled later. */
642static void gma_func0_disable(struct device *dev)
643{
644 struct device *dev_host = dev_find_slot(0, PCI_DEVFN(0x0, 0));
645
646 pci_write_config16(dev, GCFC, 0xa00);
647 pci_write_config16(dev_host, GGC, (1 << 1));
648
649 unsigned int reg32 = pci_read_config32(dev_host, DEVEN);
650 reg32 &= ~(DEVEN_D2F0 | DEVEN_D2F1);
651 pci_write_config32(dev_host, DEVEN, reg32);
652
653 dev->enabled = 0;
654}
655
Stefan Reinauer30140a52009-03-11 16:20:39 +0000656static void gma_func1_init(struct device *dev)
657{
658 u32 reg32;
Alexander Couzensc7a1a3e2016-03-09 10:42:58 +0100659 u8 val;
Stefan Reinauer30140a52009-03-11 16:20:39 +0000660
661 /* IGD needs to be Bus Master, also enable IO accesss */
662 reg32 = pci_read_config32(dev, PCI_COMMAND);
Stefan Reinauer109ab312009-08-12 16:08:05 +0000663 pci_write_config32(dev, PCI_COMMAND, reg32 |
Vladimir Serbinenko26ca08c2014-06-01 00:24:05 +0200664 PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY | PCI_COMMAND_IO);
Sven Schnelleb629d142011-06-12 14:30:10 +0200665
Alexander Couzensc7a1a3e2016-03-09 10:42:58 +0100666 if (get_option(&val, "tft_brightness") == CB_SUCCESS)
667 pci_write_config8(dev, 0xf4, val);
668 else
669 pci_write_config8(dev, 0xf4, 0xff);
Stefan Reinauer30140a52009-03-11 16:20:39 +0000670}
671
672static void gma_set_subsystem(device_t dev, unsigned vendor, unsigned device)
673{
674 if (!vendor || !device) {
675 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
676 pci_read_config32(dev, PCI_VENDOR_ID));
677 } else {
678 pci_write_config32(dev, PCI_SUBSYSTEM_VENDOR_ID,
679 ((device & 0xffff) << 16) | (vendor & 0xffff));
680 }
681}
682
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100683const struct i915_gpu_controller_info *
684intel_gma_get_controller_info(void)
685{
686 device_t dev = dev_find_slot(0, PCI_DEVFN(0x2,0));
687 if (!dev) {
688 return NULL;
689 }
690 struct northbridge_intel_i945_config *chip = dev->chip_info;
Patrick Georgi54e227e2015-08-08 22:02:12 +0200691 if (!chip) {
692 return NULL;
693 }
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100694 return &chip->gfx;
695}
696
Alexander Couzens5eea4582015-04-12 22:18:55 +0200697static void gma_ssdt(device_t device)
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100698{
699 const struct i915_gpu_controller_info *gfx = intel_gma_get_controller_info();
700 if (!gfx) {
701 return;
702 }
703
704 drivers_intel_gma_displays_ssdt_generate(gfx);
705}
706
Stefan Reinauer30140a52009-03-11 16:20:39 +0000707static struct pci_operations gma_pci_ops = {
708 .set_subsystem = gma_set_subsystem,
709};
710
711static struct device_operations gma_func0_ops = {
712 .read_resources = pci_dev_read_resources,
713 .set_resources = pci_dev_set_resources,
714 .enable_resources = pci_dev_enable_resources,
715 .init = gma_func0_init,
Vladimir Serbinenkodd2bc3f2014-10-31 09:16:31 +0100716 .acpi_fill_ssdt_generator = gma_ssdt,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000717 .scan_bus = 0,
718 .enable = 0,
Patrick Georgice6e9fe2012-07-20 12:37:06 +0200719 .disable = gma_func0_disable,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000720 .ops_pci = &gma_pci_ops,
721};
722
723
724static struct device_operations gma_func1_ops = {
725 .read_resources = pci_dev_read_resources,
726 .set_resources = pci_dev_set_resources,
727 .enable_resources = pci_dev_enable_resources,
728 .init = gma_func1_init,
729 .scan_bus = 0,
730 .enable = 0,
731 .ops_pci = &gma_pci_ops,
732};
733
Arthur Heymans0b9ecb52016-09-06 23:03:04 +0200734static const unsigned short pci_device_ids[] = { 0x27a2, 0x27ae, 0x2772, 0 };
Vladimir Serbinenko10dd0e32014-11-17 00:07:12 +0100735
Stefan Reinauer30140a52009-03-11 16:20:39 +0000736static const struct pci_driver i945_gma_func0_driver __pci_driver = {
737 .ops = &gma_func0_ops,
738 .vendor = PCI_VENDOR_ID_INTEL,
Vladimir Serbinenko10dd0e32014-11-17 00:07:12 +0100739 .devices = pci_device_ids,
Stefan Reinauer30140a52009-03-11 16:20:39 +0000740};
741
742static const struct pci_driver i945_gma_func1_driver __pci_driver = {
743 .ops = &gma_func1_ops,
744 .vendor = PCI_VENDOR_ID_INTEL,
745 .device = 0x27a6,
746};