Debian Codegraph @

src/northbridge/intel/i945/gma.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <console/console.h>
4 #include <bootmode.h>
5 #include <device/mmio.h>
6 #include <delay.h>
7 #include <arch/io.h>
8 #include <device/device.h>
9 #include <device/pci.h>
10 #include <device/pci_ops.h>
11 #include <device/pci_ids.h>
12 #include <option.h>
13 #include <edid.h>
14 #include <drivers/intel/gma/edid.h>
15 #include <drivers/intel/gma/i915.h>
16 #include <drivers/intel/gma/opregion.h>
17 #include <string.h>
18 #include <pc80/vga.h>
19 #include <pc80/vga_io.h>
20 #include <commonlib/helpers.h>
21 #include <types.h>
22 #include <framebuffer_info.h>
23
24 #include "i945.h"
25 #include "chip.h"
26
27 #define GDRST 0xc0
28 #define MSAC 0x62 /* Multi Size Aperture Control */
29
30 #define LVDS_CLOCK_A_POWERUP_ALL (3 << 8)
31 #define LVDS_CLOCK_B_POWERUP_ALL (3 << 4)
32 #define LVDS_CLOCK_BOTH_POWERUP_ALL (3 << 2)
33
34 #define DPLLB_LVDS_P2_CLOCK_DIV_7 (1 << 24) /* i915 */
35
36 #define DPLL_INTEGRATED_CRI_CLK_VLV (1 << 14)
37
38 #define PGETBL_CTL 0x2020
39 #define PGETBL_ENABLED 0x00000001
40
41 #define BASE_FREQUENCY 100000
42
43 #define DEFAULT_BLC_PWM 180
44
45 static int gtt_setup(u8 *mmiobase)
46 {
47 unsigned long PGETBL_save;
48 unsigned long tom; // top of memory
49
50 /*
51 * The Video BIOS places the GTT right below top of memory.
52 */
53 tom = pci_read_config8(pcidev_on_root(0, 0), TOLUD) << 24;
54 PGETBL_save = tom - 256 * KiB;
55 PGETBL_save |= PGETBL_ENABLED;
56 PGETBL_save |= 2; /* set GTT to 256kb */
57
58 write32(mmiobase + GFX_FLSH_CNTL, 0);
59
60 write32(mmiobase + PGETBL_CTL, PGETBL_save);
61
62 /* verify */
63 if (read32(mmiobase + PGETBL_CTL) & PGETBL_ENABLED) {
64 printk(BIOS_DEBUG, "%s is enabled.\n", __func__);
65 } else {
66 printk(BIOS_DEBUG, "%s failed!!!\n", __func__);
67 return 1;
68 }
69 write32(mmiobase + GFX_FLSH_CNTL, 0);
70
71 return 0;
72 }
73
74 static int intel_gma_init_lvds(struct northbridge_intel_i945_config *conf,
75 unsigned int pphysbase, unsigned int piobase,
76 u8 *mmiobase, uintptr_t pgfx)
77 {
78 struct edid edid;
79 struct edid_mode *mode;
80 u8 edid_data[128];
81 unsigned long temp;
82 int hpolarity, vpolarity;
83 u32 smallest_err = 0xffffffff;
84 u32 target_frequency;
85 u32 pixel_p1 = 1;
86 u32 pixel_p2;
87 u32 pixel_n = 1;
88 u32 pixel_m1 = 1;
89 u32 pixel_m2 = 1;
90 u32 hactive, vactive, right_border, bottom_border;
91 u32 vsync, hsync, vblank, hblank, hfront_porch, vfront_porch;
92 u32 i, j;
93 u32 uma_size;
94 u16 reg16;
95
96 printk(BIOS_SPEW,
97 "i915lightup: graphics %p mmio %p addrport %04x physbase %08x\n",
98 (void *)pgfx, mmiobase, piobase, pphysbase);
99
100 intel_gmbus_read_edid(mmiobase + GMBUS0, GMBUS_PORT_PANEL, 0x50,
101 edid_data, sizeof(edid_data));
102 decode_edid(edid_data, sizeof(edid_data), &edid);
103 mode = &edid.mode;
104
105 hpolarity = (mode->phsync == '-');
106 vpolarity = (mode->pvsync == '-');
107 hactive = edid.x_resolution;
108 vactive = edid.y_resolution;
109 right_border = mode->hborder;
110 bottom_border = mode->vborder;
111 vblank = mode->vbl;
112 hblank = mode->hbl;
113 vsync = mode->vspw;
114 hsync = mode->hspw;
115 hfront_porch = mode->hso;
116 vfront_porch = mode->vso;
117
118 for (i = 0; i < 2; i++)
119 for (j = 0; j < 0x100; j++)
120 /* R = j, G = j, B = j. */
121 write32(mmiobase + PALETTE(i) + 4 * j, 0x10101 * j);
122
123 write32(mmiobase + PCH_PP_CONTROL, PANEL_UNLOCK_REGS
124 | (read32(mmiobase + PCH_PP_CONTROL) & ~PANEL_UNLOCK_MASK));
125
126 write32(mmiobase + MI_ARB_STATE, MI_ARB_C3_LP_WRITE_ENABLE | (1 << 27));
127 /* Clean registers. */
128 for (i = 0; i < 0x20; i += 4)
129 write32(mmiobase + RENDER_RING_BASE + i, 0);
130 for (i = 0; i < 0x20; i += 4)
131 write32(mmiobase + FENCE_REG_965_0 + i, 0);
132
133 /* Disable VGA. */
134 write32(mmiobase + VGACNTRL, VGA_DISP_DISABLE);
135
136 /* Disable pipes. */
137 write32(mmiobase + PIPECONF(0), 0);
138 write32(mmiobase + PIPECONF(1), 0);
139
140 /* Init PRB0. */
141 write32(mmiobase + HWS_PGA, 0x352d2000);
142 write32(mmiobase + PRB0_CTL, 0);
143 write32(mmiobase + PRB0_HEAD, 0);
144 write32(mmiobase + PRB0_TAIL, 0);
145 write32(mmiobase + PRB0_START, 0);
146 write32(mmiobase + PRB0_CTL, 0x0001f001);
147
148 write32(mmiobase + D_STATE, DSTATE_PLL_D3_OFF
149 | DSTATE_GFX_CLOCK_GATING | DSTATE_DOT_CLOCK_GATING);
150 write32(mmiobase + ECOSKPD, 0x00010000);
151 write32(mmiobase + HWSTAM, 0xeffe);
152 write32(mmiobase + PORT_HOTPLUG_EN, conf->gpu_hotplug);
153 write32(mmiobase + INSTPM, 0x08000000 | INSTPM_AGPBUSY_DIS);
154
155 /* p2 divisor must 7 for dual channel LVDS */
156 /* and 14 for single channel LVDS */
157 pixel_p2 = mode->lvds_dual_channel ? 7 : 14;
158 target_frequency = mode->pixel_clock;
159
160 /* Find suitable divisors, m1, m2, p1, n. */
161 /* refclock * (5 * (m1 + 2) + (m1 + 2)) / (n + 2) / p1 / p2 */
162 /* should be closest to target frequency as possible */
163 u32 candn, candm1, candm2, candp1;
164 for (candm1 = 8; candm1 <= 18; candm1++) {
165 for (candm2 = 3; candm2 <= 7; candm2++) {
166 for (candn = 1; candn <= 6; candn++) {
167 for (candp1 = 1; candp1 <= 8; candp1++) {
168 u32 m = 5 * (candm1 + 2) + (candm2 + 2);
169 u32 p = candp1 * pixel_p2;
170 u32 vco = DIV_ROUND_CLOSEST(BASE_FREQUENCY * m, candn + 2);
171 u32 dot = DIV_ROUND_CLOSEST(vco, p);
172 u32 this_err = MAX(dot, target_frequency) -
173 MIN(dot, target_frequency);
174 if ((m < 70) || (m > 120))
175 continue;
176 if (this_err < smallest_err) {
177 smallest_err = this_err;
178 pixel_n = candn;
179 pixel_m1 = candm1;
180 pixel_m2 = candm2;
181 pixel_p1 = candp1;
182 }
183 }
184 }
185 }
186 }
187
188 if (smallest_err == 0xffffffff) {
189 printk(BIOS_ERR, "Couldn't find GFX clock divisors\n");
190 return -1;
191 }
192
193 printk(BIOS_INFO, "bringing up panel at resolution %d x %d\n",
194 hactive, vactive);
195 printk(BIOS_DEBUG, "Borders %d x %d\n", right_border, bottom_border);
196 printk(BIOS_DEBUG, "Blank %d x %d\n", hblank, vblank);
197 printk(BIOS_DEBUG, "Sync %d x %d\n", hsync, vsync);
198 printk(BIOS_DEBUG, "Front porch %d x %d\n", hfront_porch, vfront_porch);
199 printk(BIOS_DEBUG, (conf->gpu_lvds_use_spread_spectrum_clock
200 ? "Spread spectrum clock\n"
201 : "DREF clock\n"));
202 printk(BIOS_DEBUG, (mode->lvds_dual_channel
203 ? "Dual channel\n"
204 : "Single channel\n"));
205 printk(BIOS_DEBUG, "Polarities %d, %d\n",
206 hpolarity, vpolarity);
207 printk(BIOS_DEBUG, "Pixel N=%d, M1=%d, M2=%d, P1=%d\n",
208 pixel_n, pixel_m1, pixel_m2, pixel_p1);
209 printk(BIOS_DEBUG, "Pixel clock %d kHz\n",
210 BASE_FREQUENCY * (5 * (pixel_m1 + 2) + (pixel_m2 + 2)) /
211 (pixel_n + 2) / (pixel_p1 * pixel_p2));
212
213 printk(BIOS_INFO, "VGA mode: %s\n", CONFIG(LINEAR_FRAMEBUFFER) ?
214 "Linear framebuffer" : "text");
215 if (CONFIG(LINEAR_FRAMEBUFFER)) {
216 /* Disable panel fitter (we're in native resolution). */
217 write32(mmiobase + PF_CTL(0), 0);
218 write32(mmiobase + PF_WIN_SZ(0), 0);
219 write32(mmiobase + PF_WIN_POS(0), 0);
220 write32(mmiobase + PFIT_PGM_RATIOS, 0);
221 write32(mmiobase + PFIT_CONTROL, 0);
222 } else {
223 write32(mmiobase + PF_WIN_SZ(0), vactive | (hactive << 16));
224 write32(mmiobase + PF_WIN_POS(0), 0);
225 write32(mmiobase + PF_CTL(0), PF_ENABLE | PF_FILTER_MED_3x3);
226 write32(mmiobase + PFIT_CONTROL, PFIT_ENABLE
227 | (1 << PFIT_PIPE_SHIFT) | HORIZ_AUTO_SCALE
228 | VERT_AUTO_SCALE);
229 }
230
231 mdelay(1);
232
233 write32(mmiobase + DSPCNTR(0), DISPPLANE_BGRX888
234 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
235
236 mdelay(1);
237 write32(mmiobase + PP_CONTROL, PANEL_UNLOCK_REGS
238 | (read32(mmiobase + PP_CONTROL) & ~PANEL_UNLOCK_MASK));
239 write32(mmiobase + FP0(1),
240 (pixel_n << 16)
241 | (pixel_m1 << 8) | pixel_m2);
242 write32(mmiobase + DPLL(1),
243 DPLL_VGA_MODE_DIS |
244 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
245 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
246 : DPLLB_LVDS_P2_CLOCK_DIV_14)
247 | (conf->gpu_lvds_use_spread_spectrum_clock
248 ? DPLL_INTEGRATED_CLOCK_VLV | DPLL_INTEGRATED_CRI_CLK_VLV
249 : 0)
250 | (0x10000 << (pixel_p1 - 1)));
251 mdelay(1);
252 write32(mmiobase + DPLL(1),
253 DPLL_VGA_MODE_DIS |
254 DPLL_VCO_ENABLE | DPLLB_MODE_LVDS
255 | (mode->lvds_dual_channel ? DPLLB_LVDS_P2_CLOCK_DIV_7
256 : DPLLB_LVDS_P2_CLOCK_DIV_14)
257 | ((conf->gpu_lvds_use_spread_spectrum_clock ? 3 : 0) << 13)
258 | (0x10000 << (pixel_p1 - 1)));
259 mdelay(1);
260 write32(mmiobase + HTOTAL(1),
261 ((hactive + right_border + hblank - 1) << 16)
262 | (hactive - 1));
263 write32(mmiobase + HBLANK(1),
264 ((hactive + right_border + hblank - 1) << 16)
265 | (hactive + right_border - 1));
266 write32(mmiobase + HSYNC(1),
267 ((hactive + right_border + hfront_porch + hsync - 1) << 16)
268 | (hactive + right_border + hfront_porch - 1));
269
270 write32(mmiobase + VTOTAL(1), ((vactive + bottom_border + vblank - 1) << 16)
271 | (vactive - 1));
272 write32(mmiobase + VBLANK(1), ((vactive + bottom_border + vblank - 1) << 16)
273 | (vactive + bottom_border - 1));
274 write32(mmiobase + VSYNC(1),
275 ((vactive + bottom_border + vfront_porch + vsync - 1) << 16)
276 | (vactive + bottom_border + vfront_porch - 1));
277
278 if (CONFIG(LINEAR_FRAMEBUFFER)) {
279 write32(mmiobase + PIPESRC(1), ((hactive - 1) << 16)
280 | (vactive - 1));
281 } else {
282 write32(mmiobase + PIPESRC(1), (639 << 16) | 399);
283 }
284
285 mdelay(1);
286
287 write32(mmiobase + DSPSIZE(0), (hactive - 1) | ((vactive - 1) << 16));
288 write32(mmiobase + DSPPOS(0), 0);
289
290 edid.bytes_per_line = (edid.bytes_per_line + 63) & ~63;
291 write32(mmiobase + DSPADDR(0), 0);
292 write32(mmiobase + DSPSURF(0), 0);
293 write32(mmiobase + DSPSTRIDE(0), edid.bytes_per_line);
294 write32(mmiobase + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888
295 | DISPPLANE_SEL_PIPE_B | DISPPLANE_GAMMA_ENABLE);
296 mdelay(1);
297
298 write32(mmiobase + PIPECONF(1), PIPECONF_ENABLE);
299 write32(mmiobase + LVDS, LVDS_ON
300 | (hpolarity << 20) | (vpolarity << 21)
301 | (mode->lvds_dual_channel ? LVDS_CLOCK_B_POWERUP_ALL
302 | LVDS_CLOCK_BOTH_POWERUP_ALL : 0)
303 | LVDS_CLOCK_A_POWERUP_ALL
304 | LVDS_PIPE(1));
305
306 write32(mmiobase + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_OFF);
307 write32(mmiobase + PP_CONTROL, PANEL_UNLOCK_REGS | PANEL_POWER_RESET);
308 mdelay(1);
309 write32(mmiobase + PP_CONTROL, PANEL_UNLOCK_REGS
310 | PANEL_POWER_ON | PANEL_POWER_RESET);
311
312 printk(BIOS_DEBUG, "waiting for panel powerup\n");
313 while (1) {
314 u32 reg32;
315 reg32 = read32(mmiobase + PP_STATUS);
316 if ((reg32 & PP_SEQUENCE_MASK) == PP_SEQUENCE_NONE)
317 break;
318 }
319 printk(BIOS_DEBUG, "panel powered up\n");
320
321 write32(mmiobase + PP_CONTROL, PANEL_POWER_ON | PANEL_POWER_RESET);
322
323 /* Clear interrupts. */
324 write32(mmiobase + DEIIR, 0xffffffff);
325 write32(mmiobase + SDEIIR, 0xffffffff);
326 write32(mmiobase + IIR, 0xffffffff);
327 write32(mmiobase + IMR, 0xffffffff);
328 write32(mmiobase + EIR, 0xffffffff);
329
330 if (gtt_setup(mmiobase)) {
331 printk(BIOS_ERR, "GTT Setup Failed!!!\n");
332 return 0;
333 }
334
335 /* Setup GTT. */
336
337 reg16 = pci_read_config16(pcidev_on_root(0, 0), GGC);
338 uma_size = 0;
339 if (!(reg16 & 2)) {
340 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
341 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
342 }
343
344 for (i = 0; i < (uma_size - 256) / 4; i++) {
345 outl((i << 2) | 1, piobase);
346 outl((pphysbase + (i << 12)) | 1, piobase + 4);
347 }
348
349 temp = read32(mmiobase + PGETBL_CTL);
350 printk(BIOS_INFO, "GTT PGETBL_CTL register: 0x%lx\n", temp);
351
352 if (temp & 1)
353 printk(BIOS_INFO, "GTT Enabled\n");
354 else
355 printk(BIOS_ERR, "GTT is still Disabled!!!\n");
356
357 if (CONFIG(LINEAR_FRAMEBUFFER)) {
358 printk(BIOS_SPEW, "memset %p to 0x00 for %d bytes\n",
359 (void *)pgfx, hactive * vactive * 4);
360 memset((void *)pgfx, 0x00, hactive * vactive * 4);
361
362 fb_new_framebuffer_info_from_edid(&edid, pgfx);
363 } else {
364 vga_misc_write(0x67);
365
366 write32(mmiobase + DSPCNTR(0), DISPPLANE_SEL_PIPE_B);
367 write32(mmiobase + VGACNTRL, 0x02c4008e | VGA_PIPE_B_SELECT);
368
369 vga_textmode_init();
370 }
371 return 0;
372 }
373
374 static int intel_gma_init_vga(struct northbridge_intel_i945_config *conf,
375 unsigned int pphysbase, unsigned int piobase,
376 u8 *mmiobase, uintptr_t pgfx)
377 {
378 int i;
379 u32 hactive, vactive;
380 u16 reg16;
381 u32 uma_size;
382
383 printk(BIOS_SPEW, "mmiobase %lx addrport %x physbase %x\n",
384 (uintptr_t)mmiobase, piobase, pphysbase);
385
386 gtt_setup(mmiobase);
387
388 /* Disable VGA. */
389 write32(mmiobase + VGACNTRL, VGA_DISP_DISABLE);
390
391 /* Disable pipes. */
392 write32(mmiobase + PIPECONF(0), 0);
393 write32(mmiobase + PIPECONF(1), 0);
394
395 write32(mmiobase + INSTPM, 0x800);
396
397 vga_gr_write(0x18, 0);
398
399 write32(mmiobase + VGA0, 0x200074);
400 write32(mmiobase + VGA1, 0x200074);
401
402 write32(mmiobase + DSPFW3, 0x7f3f00c1 & ~PINEVIEW_SELF_REFRESH_EN);
403 write32(mmiobase + DSPCLK_GATE_D, 0);
404 write32(mmiobase + FW_BLC, 0x03060106);
405 write32(mmiobase + FW_BLC2, 0x00000306);
406
407 write32(mmiobase + ADPA, ADPA_DAC_ENABLE
408 | ADPA_PIPE_A_SELECT
409 | ADPA_USE_VGA_HVPOLARITY
410 | ADPA_VSYNC_CNTL_ENABLE
411 | ADPA_HSYNC_CNTL_ENABLE
412 | ADPA_DPMS_ON
413 );
414
415 write32(mmiobase + 0x7041c, 0x0);
416
417 write32(mmiobase + DPLL_MD(0), 0x3);
418 write32(mmiobase + DPLL_MD(1), 0x3);
419 write32(mmiobase + DSPCNTR(1), 0x1000000);
420 write32(mmiobase + PIPESRC(1), 0x027f01df);
421
422 vga_misc_write(0x67);
423 const u8 cr[] = { 0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f,
424 0x00, 0x4f, 0x0d, 0x0e, 0x00, 0x00, 0x00, 0x00,
425 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96, 0xb9, 0xa3,
426 0xff
427 };
428 vga_cr_write(0x11, 0);
429
430 for (i = 0; i <= 0x18; i++)
431 vga_cr_write(i, cr[i]);
432
433 // Disable screen memory to prevent garbage from appearing.
434 vga_sr_write(1, vga_sr_read(1) | 0x20);
435 hactive = 640;
436 vactive = 400;
437
438 mdelay(1);
439 write32(mmiobase + DPLL(0),
440 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
441 | DPLL_VGA_MODE_DIS
442 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
443 | 0x400601
444 );
445 mdelay(1);
446 write32(mmiobase + DPLL(0),
447 DPLL_VCO_ENABLE | DPLLB_MODE_DAC_SERIAL
448 | DPLL_VGA_MODE_DIS
449 | DPLL_DAC_SERIAL_P2_CLOCK_DIV_10
450 | 0x400601
451 );
452
453 write32(mmiobase + ADPA, ADPA_DAC_ENABLE
454 | ADPA_PIPE_A_SELECT
455 | ADPA_USE_VGA_HVPOLARITY
456 | ADPA_VSYNC_CNTL_ENABLE
457 | ADPA_HSYNC_CNTL_ENABLE
458 | ADPA_DPMS_ON
459 );
460
461 write32(mmiobase + HTOTAL(0),
462 ((hactive - 1) << 16)
463 | (hactive - 1));
464 write32(mmiobase + HBLANK(0),
465 ((hactive - 1) << 16)
466 | (hactive - 1));
467 write32(mmiobase + HSYNC(0),
468 ((hactive - 1) << 16)
469 | (hactive - 1));
470
471 write32(mmiobase + VTOTAL(0), ((vactive - 1) << 16)
472 | (vactive - 1));
473 write32(mmiobase + VBLANK(0), ((vactive - 1) << 16)
474 | (vactive - 1));
475 write32(mmiobase + VSYNC(0),
476 ((vactive - 1) << 16)
477 | (vactive - 1));
478
479 write32(mmiobase + PF_WIN_POS(0), 0);
480
481 write32(mmiobase + PIPESRC(0), (639 << 16) | 399);
482 write32(mmiobase + PF_CTL(0), PF_ENABLE | PF_FILTER_MED_3x3);
483 write32(mmiobase + PF_WIN_SZ(0), vactive | (hactive << 16));
484 write32(mmiobase + PFIT_CONTROL, 0x0);
485
486 mdelay(1);
487
488 write32(mmiobase + FDI_RX_CTL(0), 0x00002040);
489 mdelay(1);
490 write32(mmiobase + FDI_RX_CTL(0), 0x80002050);
491 write32(mmiobase + FDI_TX_CTL(0), 0x00044000);
492 mdelay(1);
493 write32(mmiobase + FDI_TX_CTL(0), 0x80044000);
494 write32(mmiobase + PIPECONF(0), PIPECONF_ENABLE | PIPECONF_BPP_6 | PIPECONF_DITHER_EN);
495
496 write32(mmiobase + VGACNTRL, 0x0);
497 write32(mmiobase + DSPCNTR(0), DISPLAY_PLANE_ENABLE | DISPPLANE_BGRX888);
498 mdelay(1);
499
500 write32(mmiobase + ADPA, ADPA_DAC_ENABLE
501 | ADPA_PIPE_A_SELECT
502 | ADPA_USE_VGA_HVPOLARITY
503 | ADPA_VSYNC_CNTL_ENABLE
504 | ADPA_HSYNC_CNTL_ENABLE
505 | ADPA_DPMS_ON
506 );
507
508 write32(mmiobase + DSPFW3, 0x7f3f00c1);
509 write32(mmiobase + MI_MODE, 0x200 | VS_TIMER_DISPATCH);
510 write32(mmiobase + CACHE_MODE_0, (0x6820 | (1 << 9)) & ~(1 << 5));
511 write32(mmiobase + CACHE_MODE_1, 0x380 & ~(1 << 9));
512
513 /* Set up GTT. */
514
515 reg16 = pci_read_config16(pcidev_on_root(0, 0), GGC);
516 uma_size = 0;
517 if (!(reg16 & 2)) {
518 uma_size = decode_igd_memory_size((reg16 >> 4) & 7);
519 printk(BIOS_DEBUG, "%dM UMA\n", uma_size >> 10);
520 }
521
522 for (i = 0; i < (uma_size - 256) / 4; i++) {
523 outl((i << 2) | 1, piobase);
524 outl((pphysbase + (i << 12)) | 1, piobase + 4);
525 }
526
527 /* Clear interrupts. */
528 write32(mmiobase + DEIIR, 0xffffffff);
529 write32(mmiobase + SDEIIR, 0xffffffff);
530 write32(mmiobase + IIR, 0xffffffff);
531 write32(mmiobase + IMR, 0xffffffff);
532 write32(mmiobase + EIR, 0xffffffff);
533
534 vga_textmode_init();
535
536 /* Enable screen memory. */
537 vga_sr_write(1, vga_sr_read(1) & ~0x20);
538
539 return 0;
540 }
541
542 /* compare the header of the vga edid header */
543 /* if vga is not connected it should have a correct header */
544 static int probe_edid(u8 *mmiobase, u8 slave)
545 {
546 int i;
547 u8 vga_edid[128];
548 u8 header[8] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
549 intel_gmbus_read_edid(mmiobase + GMBUS0, slave, 0x50, vga_edid, 128);
550 intel_gmbus_stop(mmiobase + GMBUS0);
551 for (i = 0; i < 8; i++) {
552 if (vga_edid[i] != header[i]) {
553 printk(BIOS_DEBUG, "No display connected on slave %d\n",
554 slave);
555 return 0;
556 }
557 }
558 printk(BIOS_SPEW, "Found a display on slave %d\n", slave);
559 return 1;
560 }
561
562 static u32 get_cdclk(struct device *const dev)
563 {
564 u16 gcfgc = pci_read_config16(dev, GCFGC);
565
566 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
567 return 133333333;
568
569 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
570 case GC_DISPLAY_CLOCK_333_320_MHZ:
571 return 320000000;
572 default:
573 case GC_DISPLAY_CLOCK_190_200_MHZ:
574 return 200000000;
575 }
576 }
577
578 static u32 freq_to_blc_pwm_ctl(struct device *const dev, u16 pwm_freq)
579 {
580 u32 blc_mod;
581
582 /* Set duty cycle to 100% due to use of legacy backlight control */
583 blc_mod = get_cdclk(dev) / (32 * pwm_freq);
584 return BLM_LEGACY_MODE | ((blc_mod / 2) << 17) | ((blc_mod / 2) << 1);
585 }
586
587 static void panel_setup(u8 *mmiobase, struct device *const dev)
588 {
589 const struct northbridge_intel_i945_config *const conf = dev->chip_info;
590
591 u32 reg32;
592
593 /* Set up Panel Power On Delays */
594 reg32 = (conf->gpu_panel_power_up_delay & 0x1fff) << 16;
595 reg32 |= (conf->gpu_panel_power_backlight_on_delay & 0x1fff);
596 write32(mmiobase + PP_ON_DELAYS, reg32);
597
598 /* Set up Panel Power Off Delays */
599 reg32 = (conf->gpu_panel_power_down_delay & 0x1fff) << 16;
600 reg32 |= (conf->gpu_panel_power_backlight_off_delay & 0x1fff);
601 write32(mmiobase + PP_OFF_DELAYS, reg32);
602
603 /* Set up Panel Power Cycle Delay */
604 reg32 = (get_cdclk(dev) / 20000 - 1) << PP_REFERENCE_DIVIDER_SHIFT;
605 reg32 |= conf->gpu_panel_power_cycle_delay & 0x1f;
606 write32(mmiobase + PP_DIVISOR, reg32);
607
608 /* Backlight init. */
609 if (conf->pwm_freq)
610 write32(mmiobase + BLC_PWM_CTL, freq_to_blc_pwm_ctl(dev,
611 conf->pwm_freq));
612 else
613 write32(mmiobase + BLC_PWM_CTL, freq_to_blc_pwm_ctl(dev,
614 DEFAULT_BLC_PWM));
615 }
616
617 static void gma_ngi(struct device *const dev)
618 {
619 /* This should probably run before post VBIOS init. */
620 printk(BIOS_INFO, "Initializing VGA without OPROM.\n");
621 void *mmiobase;
622 u32 iobase, graphics_base;
623 struct northbridge_intel_i945_config *conf = dev->chip_info;
624
625 iobase = dev->resource_list[1].base;
626 mmiobase = (void *)(uintptr_t)dev->resource_list[0].base;
627 graphics_base = dev->resource_list[2].base;
628
629 printk(BIOS_SPEW, "GMADR = 0x%08x GTTADR = 0x%08x\n",
630 pci_read_config32(dev, GMADR), pci_read_config32(dev, GTTADR));
631
632 int err;
633
634 if (CONFIG(NORTHBRIDGE_INTEL_SUBTYPE_I945GM))
635 panel_setup(mmiobase, dev);
636
637 /* probe if VGA is connected and always run */
638 /* VGA init if no LVDS is connected */
639 if (!probe_edid(mmiobase, GMBUS_PORT_PANEL) ||
640 probe_edid(mmiobase, GMBUS_PORT_VGADDC))
641 err = intel_gma_init_vga(conf,
642 pci_read_config32(dev, 0x5c) & ~0xf,
643 iobase, mmiobase, graphics_base);
644 else
645 err = intel_gma_init_lvds(conf,
646 pci_read_config32(dev, 0x5c) & ~0xf,
647 iobase, mmiobase, graphics_base);
648 if (err == 0)
649 gfx_set_init_done(1);
650 /* Linux relies on VBT for panel info. */
651 if (CONFIG(NORTHBRIDGE_INTEL_SUBTYPE_I945GM)) {
652 generate_fake_intel_oprom(&conf->gfx, dev, "$VBT CALISTOGA");
653 }
654 if (CONFIG(NORTHBRIDGE_INTEL_SUBTYPE_I945GC)) {
655 generate_fake_intel_oprom(&conf->gfx, dev, "$VBT LAKEPORT-G");
656 }
657 }
658
659 static void gma_func0_init(struct device *dev)
660 {
661 intel_gma_init_igd_opregion();
662
663 /* Unconditionally reset graphics */
664 pci_write_config8(dev, GDRST, 1);
665 udelay(50);
666 pci_write_config8(dev, GDRST, 0);
667 /* wait for device to finish */
668 while (pci_read_config8(dev, GDRST) & 1)
669 ;
670
671 if (!CONFIG(NO_GFX_INIT))
672 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
673
674 if (CONFIG(MAINBOARD_DO_NATIVE_VGA_INIT)) {
675 int vga_disable = (pci_read_config16(dev, GGC) & 2) >> 1;
676 if (acpi_is_wakeup_s3()) {
677 printk(BIOS_INFO,
678 "Skipping native VGA initialization when resuming from ACPI S3.\n");
679 } else {
680 if (vga_disable) {
681 printk(BIOS_INFO,
682 "IGD is not decoding legacy VGA MEM and IO: skipping NATIVE graphic init\n");
683 } else {
684 gma_ngi(dev);
685 }
686 }
687 } else {
688 /* PCI Init, will run VBIOS */
689 pci_dev_init(dev);
690 }
691 }
692
693 /* This doesn't reclaim stolen UMA memory, but IGD could still
694 be re-enabled later. */
695 static void gma_func0_disable(struct device *dev)
696 {
697 struct device *dev_host = pcidev_on_root(0x0, 0);
698
699 pci_write_config16(dev, GCFC, 0xa00);
700 pci_write_config16(dev_host, GGC, (1 << 1));
701
702 pci_and_config32(dev_host, DEVEN, ~(DEVEN_D2F0 | DEVEN_D2F1));
703
704 dev->enabled = 0;
705 }
706
707 static void gma_func1_init(struct device *dev)
708 {
709 if (!CONFIG(NO_GFX_INIT))
710 pci_or_config16(dev, PCI_COMMAND, PCI_COMMAND_MASTER);
711
712 pci_write_config8(dev, 0xf4, get_uint_option("tft_brightness", 0xff));
713 }
714
715 static void gma_generate_ssdt(const struct device *device)
716 {
717 const struct northbridge_intel_i945_config *chip = device->chip_info;
718
719 drivers_intel_gma_displays_ssdt_generate(&chip->gfx);
720 }
721
722 static void gma_func0_read_resources(struct device *dev)
723 {
724 /* Set Untrusted Aperture Size to 256MB */
725 pci_update_config8(dev, MSAC, ~0x3, 0x2);
726
727 pci_dev_read_resources(dev);
728 }
729
730 static const char *gma_acpi_name(const struct device *dev)
731 {
732 return "GFX0";
733 }
734
735 static struct device_operations gma_func0_ops = {
736 .read_resources = gma_func0_read_resources,
737 .set_resources = pci_dev_set_resources,
738 .enable_resources = pci_dev_enable_resources,
739 .init = gma_func0_init,
740 .acpi_fill_ssdt = gma_generate_ssdt,
741 .vga_disable = gma_func0_disable,
742 .ops_pci = &pci_dev_ops_pci,
743 .acpi_name = gma_acpi_name,
744 };
745
746 static struct device_operations gma_func1_ops = {
747 .read_resources = pci_dev_read_resources,
748 .set_resources = pci_dev_set_resources,
749 .enable_resources = pci_dev_enable_resources,
750 .init = gma_func1_init,
751 .ops_pci = &pci_dev_ops_pci,
752 };
753
754 static const unsigned short i945_gma_func0_ids[] = {
755 0x2772, /* 82945G/GZ Integrated Graphics Controller */
756 0x27a2, /* Mobile 945GM/GMS Express Integrated Graphics Controller*/
757 0x27ae, /* Mobile 945GSE Express Integrated Graphics Controller */
758 0
759 };
760
761 static const unsigned short i945_gma_func1_ids[] = {
762 0x2776, /* Desktop 82945G/GZ/GC */
763 0x27a6, /* Mobile 945GM/GMS/GME Express Integrated Graphics Controller */
764 0
765 };
766
767 static const struct pci_driver i945_gma_func0_driver __pci_driver = {
768 .ops = &gma_func0_ops,
769 .vendor = PCI_VID_INTEL,
770 .devices = i945_gma_func0_ids,
771 };
772
773 static const struct pci_driver i945_gma_func1_driver __pci_driver = {
774 .ops = &gma_func1_ops,
775 .vendor = PCI_VID_INTEL,
776 .devices = i945_gma_func1_ids,
777 };