4 * Linux framebuffer driver for Intel(R) 865G integrated graphics chips.
6 * Copyright © 2002, 2003 David Dawes <dawes@xfree86.org>
9 * This driver consists of two parts. The first part (intelfbdrv.c) provides
10 * the basic fbdev interfaces, is derived in part from the radeonfb and
11 * vesafb drivers, and is covered by the GPL. The second part (intelfbhw.c)
12 * provides the code to program the hardware. Most of it is derived from
13 * the i810/i830 XFree86 driver. The HW-specific code is covered here
14 * under a dual license (GPL and MIT/XFree86 license).
20 /* $DHD: intelfb/intelfbhw.c,v 1.9 2003/06/27 15:06:25 dawes Exp $ */
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/errno.h>
25 #include <linux/string.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
30 #include <linux/ioport.h>
31 #include <linux/init.h>
32 #include <linux/pci.h>
33 #include <linux/vmalloc.h>
34 #include <linux/pagemap.h>
35 #include <linux/interrupt.h>
40 #include "intelfbhw.h"
43 int min_m, max_m, min_m1, max_m1;
44 int min_m2, max_m2, min_n, max_n;
45 int min_p, max_p, min_p1, max_p1;
46 int min_vco, max_vco, p_transition_clk, ref_clk;
47 int p_inc_lo, p_inc_hi;
54 static struct pll_min_max plls[PLLS_MAX] = {
58 930000, 1400000, 165000, 48000,
64 1400000, 2800000, 200000, 96000,
69 intelfbhw_get_chipset(struct pci_dev *pdev, struct intelfb_info *dinfo)
75 switch (pdev->device) {
76 case PCI_DEVICE_ID_INTEL_830M:
77 dinfo->name = "Intel(R) 830M";
78 dinfo->chipset = INTEL_830M;
80 dinfo->pll_index = PLLS_I8xx;
82 case PCI_DEVICE_ID_INTEL_845G:
83 dinfo->name = "Intel(R) 845G";
84 dinfo->chipset = INTEL_845G;
86 dinfo->pll_index = PLLS_I8xx;
88 case PCI_DEVICE_ID_INTEL_85XGM:
91 dinfo->pll_index = PLLS_I8xx;
92 pci_read_config_dword(pdev, INTEL_85X_CAPID, &tmp);
93 switch ((tmp >> INTEL_85X_VARIANT_SHIFT) &
94 INTEL_85X_VARIANT_MASK) {
95 case INTEL_VAR_855GME:
96 dinfo->name = "Intel(R) 855GME";
97 dinfo->chipset = INTEL_855GME;
100 dinfo->name = "Intel(R) 855GM";
101 dinfo->chipset = INTEL_855GM;
103 case INTEL_VAR_852GME:
104 dinfo->name = "Intel(R) 852GME";
105 dinfo->chipset = INTEL_852GME;
107 case INTEL_VAR_852GM:
108 dinfo->name = "Intel(R) 852GM";
109 dinfo->chipset = INTEL_852GM;
112 dinfo->name = "Intel(R) 852GM/855GM";
113 dinfo->chipset = INTEL_85XGM;
117 case PCI_DEVICE_ID_INTEL_865G:
118 dinfo->name = "Intel(R) 865G";
119 dinfo->chipset = INTEL_865G;
121 dinfo->pll_index = PLLS_I8xx;
123 case PCI_DEVICE_ID_INTEL_915G:
124 dinfo->name = "Intel(R) 915G";
125 dinfo->chipset = INTEL_915G;
127 dinfo->pll_index = PLLS_I9xx;
129 case PCI_DEVICE_ID_INTEL_915GM:
130 dinfo->name = "Intel(R) 915GM";
131 dinfo->chipset = INTEL_915GM;
133 dinfo->pll_index = PLLS_I9xx;
135 case PCI_DEVICE_ID_INTEL_945G:
136 dinfo->name = "Intel(R) 945G";
137 dinfo->chipset = INTEL_945G;
139 dinfo->pll_index = PLLS_I9xx;
141 case PCI_DEVICE_ID_INTEL_945GM:
142 dinfo->name = "Intel(R) 945GM";
143 dinfo->chipset = INTEL_945GM;
145 dinfo->pll_index = PLLS_I9xx;
153 intelfbhw_get_memory(struct pci_dev *pdev, int *aperture_size,
156 struct pci_dev *bridge_dev;
160 if (!pdev || !aperture_size || !stolen_size)
163 /* Find the bridge device. It is always 0:0.0 */
164 if (!(bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0, 0)))) {
165 ERR_MSG("cannot find bridge device\n");
169 /* Get the fb aperture size and "stolen" memory amount. */
171 pci_read_config_word(bridge_dev, INTEL_GMCH_CTRL, &tmp);
172 pci_dev_put(bridge_dev);
174 switch (pdev->device) {
175 case PCI_DEVICE_ID_INTEL_915G:
176 case PCI_DEVICE_ID_INTEL_915GM:
177 case PCI_DEVICE_ID_INTEL_945G:
178 case PCI_DEVICE_ID_INTEL_945GM:
179 /* 915 and 945 chipsets support a 256MB aperture.
180 Aperture size is determined by inspected the
181 base address of the aperture. */
182 if (pci_resource_start(pdev, 2) & 0x08000000)
183 *aperture_size = MB(128);
185 *aperture_size = MB(256);
188 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
189 *aperture_size = MB(64);
191 *aperture_size = MB(128);
195 /* Stolen memory size is reduced by the GTT and the popup.
196 GTT is 1K per MB of aperture size, and popup is 4K. */
197 stolen_overhead = (*aperture_size / MB(1)) + 4;
198 switch(pdev->device) {
199 case PCI_DEVICE_ID_INTEL_830M:
200 case PCI_DEVICE_ID_INTEL_845G:
201 switch (tmp & INTEL_830_GMCH_GMS_MASK) {
202 case INTEL_830_GMCH_GMS_STOLEN_512:
203 *stolen_size = KB(512) - KB(stolen_overhead);
205 case INTEL_830_GMCH_GMS_STOLEN_1024:
206 *stolen_size = MB(1) - KB(stolen_overhead);
208 case INTEL_830_GMCH_GMS_STOLEN_8192:
209 *stolen_size = MB(8) - KB(stolen_overhead);
211 case INTEL_830_GMCH_GMS_LOCAL:
212 ERR_MSG("only local memory found\n");
214 case INTEL_830_GMCH_GMS_DISABLED:
215 ERR_MSG("video memory is disabled\n");
218 ERR_MSG("unexpected GMCH_GMS value: 0x%02x\n",
219 tmp & INTEL_830_GMCH_GMS_MASK);
224 switch (tmp & INTEL_855_GMCH_GMS_MASK) {
225 case INTEL_855_GMCH_GMS_STOLEN_1M:
226 *stolen_size = MB(1) - KB(stolen_overhead);
228 case INTEL_855_GMCH_GMS_STOLEN_4M:
229 *stolen_size = MB(4) - KB(stolen_overhead);
231 case INTEL_855_GMCH_GMS_STOLEN_8M:
232 *stolen_size = MB(8) - KB(stolen_overhead);
234 case INTEL_855_GMCH_GMS_STOLEN_16M:
235 *stolen_size = MB(16) - KB(stolen_overhead);
237 case INTEL_855_GMCH_GMS_STOLEN_32M:
238 *stolen_size = MB(32) - KB(stolen_overhead);
240 case INTEL_915G_GMCH_GMS_STOLEN_48M:
241 *stolen_size = MB(48) - KB(stolen_overhead);
243 case INTEL_915G_GMCH_GMS_STOLEN_64M:
244 *stolen_size = MB(64) - KB(stolen_overhead);
246 case INTEL_855_GMCH_GMS_DISABLED:
247 ERR_MSG("video memory is disabled\n");
250 ERR_MSG("unexpected GMCH_GMS value: 0x%02x\n",
251 tmp & INTEL_855_GMCH_GMS_MASK);
258 intelfbhw_check_non_crt(struct intelfb_info *dinfo)
262 if (INREG(LVDS) & PORT_ENABLE)
264 if (INREG(DVOA) & PORT_ENABLE)
266 if (INREG(DVOB) & PORT_ENABLE)
268 if (INREG(DVOC) & PORT_ENABLE)
275 intelfbhw_dvo_to_string(int dvo)
279 else if (dvo & DVOB_PORT)
281 else if (dvo & DVOC_PORT)
283 else if (dvo & LVDS_PORT)
291 intelfbhw_validate_mode(struct intelfb_info *dinfo,
292 struct fb_var_screeninfo *var)
298 DBG_MSG("intelfbhw_validate_mode\n");
301 bytes_per_pixel = var->bits_per_pixel / 8;
302 if (bytes_per_pixel == 3)
305 /* Check if enough video memory. */
306 tmp = var->yres_virtual * var->xres_virtual * bytes_per_pixel;
307 if (tmp > dinfo->fb.size) {
308 WRN_MSG("Not enough video ram for mode "
309 "(%d KByte vs %d KByte).\n",
310 BtoKB(tmp), BtoKB(dinfo->fb.size));
314 /* Check if x/y limits are OK. */
315 if (var->xres - 1 > HACTIVE_MASK) {
316 WRN_MSG("X resolution too large (%d vs %d).\n",
317 var->xres, HACTIVE_MASK + 1);
320 if (var->yres - 1 > VACTIVE_MASK) {
321 WRN_MSG("Y resolution too large (%d vs %d).\n",
322 var->yres, VACTIVE_MASK + 1);
326 /* Check for interlaced/doublescan modes. */
327 if (var->vmode & FB_VMODE_INTERLACED) {
328 WRN_MSG("Mode is interlaced.\n");
331 if (var->vmode & FB_VMODE_DOUBLE) {
332 WRN_MSG("Mode is double-scan.\n");
336 /* Check if clock is OK. */
337 tmp = 1000000000 / var->pixclock;
338 if (tmp < MIN_CLOCK) {
339 WRN_MSG("Pixel clock is too low (%d MHz vs %d MHz).\n",
340 (tmp + 500) / 1000, MIN_CLOCK / 1000);
343 if (tmp > MAX_CLOCK) {
344 WRN_MSG("Pixel clock is too high (%d MHz vs %d MHz).\n",
345 (tmp + 500) / 1000, MAX_CLOCK / 1000);
353 intelfbhw_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
355 struct intelfb_info *dinfo = GET_DINFO(info);
356 u32 offset, xoffset, yoffset;
359 DBG_MSG("intelfbhw_pan_display\n");
362 xoffset = ROUND_DOWN_TO(var->xoffset, 8);
363 yoffset = var->yoffset;
365 if ((xoffset + var->xres > var->xres_virtual) ||
366 (yoffset + var->yres > var->yres_virtual))
369 offset = (yoffset * dinfo->pitch) +
370 (xoffset * var->bits_per_pixel) / 8;
372 offset += dinfo->fb.offset << 12;
374 dinfo->vsync.pan_offset = offset;
375 if ((var->activate & FB_ACTIVATE_VBL) && !intelfbhw_enable_irq(dinfo, 0)) {
376 dinfo->vsync.pan_display = 1;
378 dinfo->vsync.pan_display = 0;
379 OUTREG(DSPABASE, offset);
385 /* Blank the screen. */
387 intelfbhw_do_blank(int blank, struct fb_info *info)
389 struct intelfb_info *dinfo = GET_DINFO(info);
393 DBG_MSG("intelfbhw_do_blank: blank is %d\n", blank);
396 /* Turn plane A on or off */
397 tmp = INREG(DSPACNTR);
399 tmp &= ~DISPPLANE_PLANE_ENABLE;
401 tmp |= DISPPLANE_PLANE_ENABLE;
402 OUTREG(DSPACNTR, tmp);
404 tmp = INREG(DSPABASE);
405 OUTREG(DSPABASE, tmp);
407 /* Turn off/on the HW cursor */
409 DBG_MSG("cursor_on is %d\n", dinfo->cursor_on);
411 if (dinfo->cursor_on) {
413 intelfbhw_cursor_hide(dinfo);
415 intelfbhw_cursor_show(dinfo);
417 dinfo->cursor_on = 1;
419 dinfo->cursor_blanked = blank;
422 tmp = INREG(ADPA) & ~ADPA_DPMS_CONTROL_MASK;
424 case FB_BLANK_UNBLANK:
425 case FB_BLANK_NORMAL:
428 case FB_BLANK_VSYNC_SUSPEND:
431 case FB_BLANK_HSYNC_SUSPEND:
434 case FB_BLANK_POWERDOWN:
445 intelfbhw_setcolreg(struct intelfb_info *dinfo, unsigned regno,
446 unsigned red, unsigned green, unsigned blue,
450 DBG_MSG("intelfbhw_setcolreg: %d: (%d, %d, %d)\n",
451 regno, red, green, blue);
454 u32 palette_reg = (dinfo->pipe == PIPE_A) ?
455 PALETTE_A : PALETTE_B;
457 OUTREG(palette_reg + (regno << 2),
458 (red << PALETTE_8_RED_SHIFT) |
459 (green << PALETTE_8_GREEN_SHIFT) |
460 (blue << PALETTE_8_BLUE_SHIFT));
465 intelfbhw_read_hw_state(struct intelfb_info *dinfo, struct intelfb_hwstate *hw,
471 DBG_MSG("intelfbhw_read_hw_state\n");
477 /* Read in as much of the HW state as possible. */
478 hw->vga0_divisor = INREG(VGA0_DIVISOR);
479 hw->vga1_divisor = INREG(VGA1_DIVISOR);
480 hw->vga_pd = INREG(VGAPD);
481 hw->dpll_a = INREG(DPLL_A);
482 hw->dpll_b = INREG(DPLL_B);
483 hw->fpa0 = INREG(FPA0);
484 hw->fpa1 = INREG(FPA1);
485 hw->fpb0 = INREG(FPB0);
486 hw->fpb1 = INREG(FPB1);
492 /* This seems to be a problem with the 852GM/855GM */
493 for (i = 0; i < PALETTE_8_ENTRIES; i++) {
494 hw->palette_a[i] = INREG(PALETTE_A + (i << 2));
495 hw->palette_b[i] = INREG(PALETTE_B + (i << 2));
502 hw->htotal_a = INREG(HTOTAL_A);
503 hw->hblank_a = INREG(HBLANK_A);
504 hw->hsync_a = INREG(HSYNC_A);
505 hw->vtotal_a = INREG(VTOTAL_A);
506 hw->vblank_a = INREG(VBLANK_A);
507 hw->vsync_a = INREG(VSYNC_A);
508 hw->src_size_a = INREG(SRC_SIZE_A);
509 hw->bclrpat_a = INREG(BCLRPAT_A);
510 hw->htotal_b = INREG(HTOTAL_B);
511 hw->hblank_b = INREG(HBLANK_B);
512 hw->hsync_b = INREG(HSYNC_B);
513 hw->vtotal_b = INREG(VTOTAL_B);
514 hw->vblank_b = INREG(VBLANK_B);
515 hw->vsync_b = INREG(VSYNC_B);
516 hw->src_size_b = INREG(SRC_SIZE_B);
517 hw->bclrpat_b = INREG(BCLRPAT_B);
522 hw->adpa = INREG(ADPA);
523 hw->dvoa = INREG(DVOA);
524 hw->dvob = INREG(DVOB);
525 hw->dvoc = INREG(DVOC);
526 hw->dvoa_srcdim = INREG(DVOA_SRCDIM);
527 hw->dvob_srcdim = INREG(DVOB_SRCDIM);
528 hw->dvoc_srcdim = INREG(DVOC_SRCDIM);
529 hw->lvds = INREG(LVDS);
534 hw->pipe_a_conf = INREG(PIPEACONF);
535 hw->pipe_b_conf = INREG(PIPEBCONF);
536 hw->disp_arb = INREG(DISPARB);
541 hw->cursor_a_control = INREG(CURSOR_A_CONTROL);
542 hw->cursor_b_control = INREG(CURSOR_B_CONTROL);
543 hw->cursor_a_base = INREG(CURSOR_A_BASEADDR);
544 hw->cursor_b_base = INREG(CURSOR_B_BASEADDR);
549 for (i = 0; i < 4; i++) {
550 hw->cursor_a_palette[i] = INREG(CURSOR_A_PALETTE0 + (i << 2));
551 hw->cursor_b_palette[i] = INREG(CURSOR_B_PALETTE0 + (i << 2));
557 hw->cursor_size = INREG(CURSOR_SIZE);
562 hw->disp_a_ctrl = INREG(DSPACNTR);
563 hw->disp_b_ctrl = INREG(DSPBCNTR);
564 hw->disp_a_base = INREG(DSPABASE);
565 hw->disp_b_base = INREG(DSPBBASE);
566 hw->disp_a_stride = INREG(DSPASTRIDE);
567 hw->disp_b_stride = INREG(DSPBSTRIDE);
572 hw->vgacntrl = INREG(VGACNTRL);
577 hw->add_id = INREG(ADD_ID);
582 for (i = 0; i < 7; i++) {
583 hw->swf0x[i] = INREG(SWF00 + (i << 2));
584 hw->swf1x[i] = INREG(SWF10 + (i << 2));
586 hw->swf3x[i] = INREG(SWF30 + (i << 2));
589 for (i = 0; i < 8; i++)
590 hw->fence[i] = INREG(FENCE + (i << 2));
592 hw->instpm = INREG(INSTPM);
593 hw->mem_mode = INREG(MEM_MODE);
594 hw->fw_blc_0 = INREG(FW_BLC_0);
595 hw->fw_blc_1 = INREG(FW_BLC_1);
597 hw->hwstam = INREG16(HWSTAM);
598 hw->ier = INREG16(IER);
599 hw->iir = INREG16(IIR);
600 hw->imr = INREG16(IMR);
606 static int calc_vclock3(int index, int m, int n, int p)
608 if (p == 0 || n == 0)
610 return plls[index].ref_clk * m / n / p;
613 static int calc_vclock(int index, int m1, int m2, int n, int p1, int p2, int lvds)
615 struct pll_min_max *pll = &plls[index];
618 m = (5 * (m1 + 2)) + (m2 + 2);
620 vco = pll->ref_clk * m / n;
622 if (index == PLLS_I8xx) {
623 p = ((p1 + 2) * (1 << (p2 + 1)));
625 p = ((p1) * (p2 ? 5 : 10));
632 intelfbhw_get_p1p2(struct intelfb_info *dinfo, int dpll, int *o_p1, int *o_p2)
636 if (IS_I9XX(dinfo)) {
637 if (dpll & DPLL_P1_FORCE_DIV2)
640 p1 = (dpll >> DPLL_P1_SHIFT) & 0xff;
644 p2 = (dpll >> DPLL_I9XX_P2_SHIFT) & DPLL_P2_MASK;
646 if (dpll & DPLL_P1_FORCE_DIV2)
649 p1 = (dpll >> DPLL_P1_SHIFT) & DPLL_P1_MASK;
650 p2 = (dpll >> DPLL_P2_SHIFT) & DPLL_P2_MASK;
660 intelfbhw_print_hw_state(struct intelfb_info *dinfo, struct intelfb_hwstate *hw)
663 int i, m1, m2, n, p1, p2;
664 int index = dinfo->pll_index;
665 DBG_MSG("intelfbhw_print_hw_state\n");
669 /* Read in as much of the HW state as possible. */
670 printk("hw state dump start\n");
671 printk(" VGA0_DIVISOR: 0x%08x\n", hw->vga0_divisor);
672 printk(" VGA1_DIVISOR: 0x%08x\n", hw->vga1_divisor);
673 printk(" VGAPD: 0x%08x\n", hw->vga_pd);
674 n = (hw->vga0_divisor >> FP_N_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
675 m1 = (hw->vga0_divisor >> FP_M1_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
676 m2 = (hw->vga0_divisor >> FP_M2_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
678 intelfbhw_get_p1p2(dinfo, hw->vga_pd, &p1, &p2);
680 printk(" VGA0: (m1, m2, n, p1, p2) = (%d, %d, %d, %d, %d)\n",
682 printk(" VGA0: clock is %d\n",
683 calc_vclock(index, m1, m2, n, p1, p2, 0));
685 n = (hw->vga1_divisor >> FP_N_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
686 m1 = (hw->vga1_divisor >> FP_M1_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
687 m2 = (hw->vga1_divisor >> FP_M2_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
689 intelfbhw_get_p1p2(dinfo, hw->vga_pd, &p1, &p2);
690 printk(" VGA1: (m1, m2, n, p1, p2) = (%d, %d, %d, %d, %d)\n",
692 printk(" VGA1: clock is %d\n", calc_vclock(index, m1, m2, n, p1, p2, 0));
694 printk(" DPLL_A: 0x%08x\n", hw->dpll_a);
695 printk(" DPLL_B: 0x%08x\n", hw->dpll_b);
696 printk(" FPA0: 0x%08x\n", hw->fpa0);
697 printk(" FPA1: 0x%08x\n", hw->fpa1);
698 printk(" FPB0: 0x%08x\n", hw->fpb0);
699 printk(" FPB1: 0x%08x\n", hw->fpb1);
701 n = (hw->fpa0 >> FP_N_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
702 m1 = (hw->fpa0 >> FP_M1_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
703 m2 = (hw->fpa0 >> FP_M2_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
705 intelfbhw_get_p1p2(dinfo, hw->dpll_a, &p1, &p2);
707 printk(" PLLA0: (m1, m2, n, p1, p2) = (%d, %d, %d, %d, %d)\n",
709 printk(" PLLA0: clock is %d\n", calc_vclock(index, m1, m2, n, p1, p2, 0));
711 n = (hw->fpa1 >> FP_N_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
712 m1 = (hw->fpa1 >> FP_M1_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
713 m2 = (hw->fpa1 >> FP_M2_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
715 intelfbhw_get_p1p2(dinfo, hw->dpll_a, &p1, &p2);
717 printk(" PLLA1: (m1, m2, n, p1, p2) = (%d, %d, %d, %d, %d)\n",
719 printk(" PLLA1: clock is %d\n", calc_vclock(index, m1, m2, n, p1, p2, 0));
722 printk(" PALETTE_A:\n");
723 for (i = 0; i < PALETTE_8_ENTRIES)
724 printk(" %3d: 0x%08x\n", i, hw->palette_a[i]);
725 printk(" PALETTE_B:\n");
726 for (i = 0; i < PALETTE_8_ENTRIES)
727 printk(" %3d: 0x%08x\n", i, hw->palette_b[i]);
730 printk(" HTOTAL_A: 0x%08x\n", hw->htotal_a);
731 printk(" HBLANK_A: 0x%08x\n", hw->hblank_a);
732 printk(" HSYNC_A: 0x%08x\n", hw->hsync_a);
733 printk(" VTOTAL_A: 0x%08x\n", hw->vtotal_a);
734 printk(" VBLANK_A: 0x%08x\n", hw->vblank_a);
735 printk(" VSYNC_A: 0x%08x\n", hw->vsync_a);
736 printk(" SRC_SIZE_A: 0x%08x\n", hw->src_size_a);
737 printk(" BCLRPAT_A: 0x%08x\n", hw->bclrpat_a);
738 printk(" HTOTAL_B: 0x%08x\n", hw->htotal_b);
739 printk(" HBLANK_B: 0x%08x\n", hw->hblank_b);
740 printk(" HSYNC_B: 0x%08x\n", hw->hsync_b);
741 printk(" VTOTAL_B: 0x%08x\n", hw->vtotal_b);
742 printk(" VBLANK_B: 0x%08x\n", hw->vblank_b);
743 printk(" VSYNC_B: 0x%08x\n", hw->vsync_b);
744 printk(" SRC_SIZE_B: 0x%08x\n", hw->src_size_b);
745 printk(" BCLRPAT_B: 0x%08x\n", hw->bclrpat_b);
747 printk(" ADPA: 0x%08x\n", hw->adpa);
748 printk(" DVOA: 0x%08x\n", hw->dvoa);
749 printk(" DVOB: 0x%08x\n", hw->dvob);
750 printk(" DVOC: 0x%08x\n", hw->dvoc);
751 printk(" DVOA_SRCDIM: 0x%08x\n", hw->dvoa_srcdim);
752 printk(" DVOB_SRCDIM: 0x%08x\n", hw->dvob_srcdim);
753 printk(" DVOC_SRCDIM: 0x%08x\n", hw->dvoc_srcdim);
754 printk(" LVDS: 0x%08x\n", hw->lvds);
756 printk(" PIPEACONF: 0x%08x\n", hw->pipe_a_conf);
757 printk(" PIPEBCONF: 0x%08x\n", hw->pipe_b_conf);
758 printk(" DISPARB: 0x%08x\n", hw->disp_arb);
760 printk(" CURSOR_A_CONTROL: 0x%08x\n", hw->cursor_a_control);
761 printk(" CURSOR_B_CONTROL: 0x%08x\n", hw->cursor_b_control);
762 printk(" CURSOR_A_BASEADDR: 0x%08x\n", hw->cursor_a_base);
763 printk(" CURSOR_B_BASEADDR: 0x%08x\n", hw->cursor_b_base);
765 printk(" CURSOR_A_PALETTE: ");
766 for (i = 0; i < 4; i++) {
767 printk("0x%08x", hw->cursor_a_palette[i]);
772 printk(" CURSOR_B_PALETTE: ");
773 for (i = 0; i < 4; i++) {
774 printk("0x%08x", hw->cursor_b_palette[i]);
780 printk(" CURSOR_SIZE: 0x%08x\n", hw->cursor_size);
782 printk(" DSPACNTR: 0x%08x\n", hw->disp_a_ctrl);
783 printk(" DSPBCNTR: 0x%08x\n", hw->disp_b_ctrl);
784 printk(" DSPABASE: 0x%08x\n", hw->disp_a_base);
785 printk(" DSPBBASE: 0x%08x\n", hw->disp_b_base);
786 printk(" DSPASTRIDE: 0x%08x\n", hw->disp_a_stride);
787 printk(" DSPBSTRIDE: 0x%08x\n", hw->disp_b_stride);
789 printk(" VGACNTRL: 0x%08x\n", hw->vgacntrl);
790 printk(" ADD_ID: 0x%08x\n", hw->add_id);
792 for (i = 0; i < 7; i++) {
793 printk(" SWF0%d 0x%08x\n", i,
796 for (i = 0; i < 7; i++) {
797 printk(" SWF1%d 0x%08x\n", i,
800 for (i = 0; i < 3; i++) {
801 printk(" SWF3%d 0x%08x\n", i,
804 for (i = 0; i < 8; i++)
805 printk(" FENCE%d 0x%08x\n", i,
808 printk(" INSTPM 0x%08x\n", hw->instpm);
809 printk(" MEM_MODE 0x%08x\n", hw->mem_mode);
810 printk(" FW_BLC_0 0x%08x\n", hw->fw_blc_0);
811 printk(" FW_BLC_1 0x%08x\n", hw->fw_blc_1);
813 printk(" HWSTAM 0x%04x\n", hw->hwstam);
814 printk(" IER 0x%04x\n", hw->ier);
815 printk(" IIR 0x%04x\n", hw->iir);
816 printk(" IMR 0x%04x\n", hw->imr);
817 printk("hw state dump end\n");
823 /* Split the M parameter into M1 and M2. */
825 splitm(int index, unsigned int m, unsigned int *retm1, unsigned int *retm2)
829 struct pll_min_max *pll = &plls[index];
831 /* no point optimising too much - brute force m */
832 for (m1 = pll->min_m1; m1 < pll->max_m1 + 1; m1++) {
833 for (m2 = pll->min_m2; m2 < pll->max_m2 + 1; m2++) {
834 testm = (5 * (m1 + 2)) + (m2 + 2);
836 *retm1 = (unsigned int)m1;
837 *retm2 = (unsigned int)m2;
845 /* Split the P parameter into P1 and P2. */
847 splitp(int index, unsigned int p, unsigned int *retp1, unsigned int *retp2)
850 struct pll_min_max *pll = &plls[index];
852 if (index == PLLS_I9xx) {
853 p2 = (p % 10) ? 1 : 0;
855 p1 = p / (p2 ? 5 : 10);
857 *retp1 = (unsigned int)p1;
858 *retp2 = (unsigned int)p2;
866 p1 = (p / (1 << (p2 + 1))) - 2;
867 if (p % 4 == 0 && p1 < pll->min_p1) {
869 p1 = (p / (1 << (p2 + 1))) - 2;
871 if (p1 < pll->min_p1 || p1 > pll->max_p1 ||
872 (p1 + 2) * (1 << (p2 + 1)) != p) {
875 *retp1 = (unsigned int)p1;
876 *retp2 = (unsigned int)p2;
882 calc_pll_params(int index, int clock, u32 *retm1, u32 *retm2, u32 *retn, u32 *retp1,
883 u32 *retp2, u32 *retclock)
885 u32 m1, m2, n, p1, p2, n1, testm;
886 u32 f_vco, p, p_best = 0, m, f_out = 0;
887 u32 err_max, err_target, err_best = 10000000;
888 u32 n_best = 0, m_best = 0, f_best, f_err;
889 u32 p_min, p_max, p_inc, div_max;
890 struct pll_min_max *pll = &plls[index];
892 /* Accept 0.5% difference, but aim for 0.1% */
893 err_max = 5 * clock / 1000;
894 err_target = clock / 1000;
896 DBG_MSG("Clock is %d\n", clock);
898 div_max = pll->max_vco / clock;
900 p_inc = (clock <= pll->p_transition_clk) ? pll->p_inc_lo : pll->p_inc_hi;
902 p_max = ROUND_DOWN_TO(div_max, p_inc);
903 if (p_min < pll->min_p)
905 if (p_max > pll->max_p)
908 DBG_MSG("p range is %d-%d (%d)\n", p_min, p_max, p_inc);
912 if (splitp(index, p, &p1, &p2)) {
913 WRN_MSG("cannot split p = %d\n", p);
921 m = ROUND_UP_TO(f_vco * n, pll->ref_clk) / pll->ref_clk;
926 for (testm = m - 1; testm <= m; testm++) {
927 f_out = calc_vclock3(index, m, n, p);
928 if (splitm(index, testm, &m1, &m2)) {
929 WRN_MSG("cannot split m = %d\n", m);
934 f_err = clock - f_out;
935 else/* slightly bias the error for bigger clocks */
936 f_err = f_out - clock + 1;
938 if (f_err < err_best) {
947 } while ((n <= pll->max_n) && (f_out >= clock));
949 } while ((p <= p_max));
952 WRN_MSG("cannot find parameters for clock %d\n", clock);
958 splitm(index, m, &m1, &m2);
959 splitp(index, p, &p1, &p2);
962 DBG_MSG("m, n, p: %d (%d,%d), %d (%d), %d (%d,%d), "
963 "f: %d (%d), VCO: %d\n",
964 m, m1, m2, n, n1, p, p1, p2,
965 calc_vclock3(index, m, n, p),
966 calc_vclock(index, m1, m2, n1, p1, p2, 0),
967 calc_vclock3(index, m, n, p) * p);
973 *retclock = calc_vclock(index, m1, m2, n1, p1, p2, 0);
978 static __inline__ int
979 check_overflow(u32 value, u32 limit, const char *description)
982 WRN_MSG("%s value %d exceeds limit %d\n",
983 description, value, limit);
989 /* It is assumed that hw is filled in with the initial state information. */
991 intelfbhw_mode_to_hw(struct intelfb_info *dinfo, struct intelfb_hwstate *hw,
992 struct fb_var_screeninfo *var)
995 u32 *dpll, *fp0, *fp1;
996 u32 m1, m2, n, p1, p2, clock_target, clock;
997 u32 hsync_start, hsync_end, hblank_start, hblank_end, htotal, hactive;
998 u32 vsync_start, vsync_end, vblank_start, vblank_end, vtotal, vactive;
999 u32 vsync_pol, hsync_pol;
1000 u32 *vs, *vb, *vt, *hs, *hb, *ht, *ss, *pipe_conf;
1001 u32 stride_alignment;
1003 DBG_MSG("intelfbhw_mode_to_hw\n");
1006 hw->vgacntrl |= VGA_DISABLE;
1008 /* Check whether pipe A or pipe B is enabled. */
1009 if (hw->pipe_a_conf & PIPECONF_ENABLE)
1011 else if (hw->pipe_b_conf & PIPECONF_ENABLE)
1014 /* Set which pipe's registers will be set. */
1015 if (pipe == PIPE_B) {
1025 ss = &hw->src_size_b;
1026 pipe_conf = &hw->pipe_b_conf;
1037 ss = &hw->src_size_a;
1038 pipe_conf = &hw->pipe_a_conf;
1041 /* Use ADPA register for sync control. */
1042 hw->adpa &= ~ADPA_USE_VGA_HVPOLARITY;
1045 hsync_pol = (var->sync & FB_SYNC_HOR_HIGH_ACT) ?
1046 ADPA_SYNC_ACTIVE_HIGH : ADPA_SYNC_ACTIVE_LOW;
1047 vsync_pol = (var->sync & FB_SYNC_VERT_HIGH_ACT) ?
1048 ADPA_SYNC_ACTIVE_HIGH : ADPA_SYNC_ACTIVE_LOW;
1049 hw->adpa &= ~((ADPA_SYNC_ACTIVE_MASK << ADPA_VSYNC_ACTIVE_SHIFT) |
1050 (ADPA_SYNC_ACTIVE_MASK << ADPA_HSYNC_ACTIVE_SHIFT));
1051 hw->adpa |= (hsync_pol << ADPA_HSYNC_ACTIVE_SHIFT) |
1052 (vsync_pol << ADPA_VSYNC_ACTIVE_SHIFT);
1054 /* Connect correct pipe to the analog port DAC */
1055 hw->adpa &= ~(PIPE_MASK << ADPA_PIPE_SELECT_SHIFT);
1056 hw->adpa |= (pipe << ADPA_PIPE_SELECT_SHIFT);
1058 /* Set DPMS state to D0 (on) */
1059 hw->adpa &= ~ADPA_DPMS_CONTROL_MASK;
1060 hw->adpa |= ADPA_DPMS_D0;
1062 hw->adpa |= ADPA_DAC_ENABLE;
1064 *dpll |= (DPLL_VCO_ENABLE | DPLL_VGA_MODE_DISABLE);
1065 *dpll &= ~(DPLL_RATE_SELECT_MASK | DPLL_REFERENCE_SELECT_MASK);
1066 *dpll |= (DPLL_REFERENCE_DEFAULT | DPLL_RATE_SELECT_FP0);
1068 /* Desired clock in kHz */
1069 clock_target = 1000000000 / var->pixclock;
1071 if (calc_pll_params(dinfo->pll_index, clock_target, &m1, &m2,
1072 &n, &p1, &p2, &clock)) {
1073 WRN_MSG("calc_pll_params failed\n");
1077 /* Check for overflow. */
1078 if (check_overflow(p1, DPLL_P1_MASK, "PLL P1 parameter"))
1080 if (check_overflow(p2, DPLL_P2_MASK, "PLL P2 parameter"))
1082 if (check_overflow(m1, FP_DIVISOR_MASK, "PLL M1 parameter"))
1084 if (check_overflow(m2, FP_DIVISOR_MASK, "PLL M2 parameter"))
1086 if (check_overflow(n, FP_DIVISOR_MASK, "PLL N parameter"))
1089 *dpll &= ~DPLL_P1_FORCE_DIV2;
1090 *dpll &= ~((DPLL_P2_MASK << DPLL_P2_SHIFT) |
1091 (DPLL_P1_MASK << DPLL_P1_SHIFT));
1093 if (IS_I9XX(dinfo)) {
1094 *dpll |= (p2 << DPLL_I9XX_P2_SHIFT);
1095 *dpll |= (1 << (p1 - 1)) << DPLL_P1_SHIFT;
1097 *dpll |= (p2 << DPLL_P2_SHIFT) | (p1 << DPLL_P1_SHIFT);
1100 *fp0 = (n << FP_N_DIVISOR_SHIFT) |
1101 (m1 << FP_M1_DIVISOR_SHIFT) |
1102 (m2 << FP_M2_DIVISOR_SHIFT);
1105 hw->dvob &= ~PORT_ENABLE;
1106 hw->dvoc &= ~PORT_ENABLE;
1108 /* Use display plane A. */
1109 hw->disp_a_ctrl |= DISPPLANE_PLANE_ENABLE;
1110 hw->disp_a_ctrl &= ~DISPPLANE_GAMMA_ENABLE;
1111 hw->disp_a_ctrl &= ~DISPPLANE_PIXFORMAT_MASK;
1112 switch (intelfb_var_to_depth(var)) {
1114 hw->disp_a_ctrl |= DISPPLANE_8BPP | DISPPLANE_GAMMA_ENABLE;
1117 hw->disp_a_ctrl |= DISPPLANE_15_16BPP;
1120 hw->disp_a_ctrl |= DISPPLANE_16BPP;
1123 hw->disp_a_ctrl |= DISPPLANE_32BPP_NO_ALPHA;
1126 hw->disp_a_ctrl &= ~(PIPE_MASK << DISPPLANE_SEL_PIPE_SHIFT);
1127 hw->disp_a_ctrl |= (pipe << DISPPLANE_SEL_PIPE_SHIFT);
1129 /* Set CRTC registers. */
1130 hactive = var->xres;
1131 hsync_start = hactive + var->right_margin;
1132 hsync_end = hsync_start + var->hsync_len;
1133 htotal = hsync_end + var->left_margin;
1134 hblank_start = hactive;
1135 hblank_end = htotal;
1137 DBG_MSG("H: act %d, ss %d, se %d, tot %d bs %d, be %d\n",
1138 hactive, hsync_start, hsync_end, htotal, hblank_start,
1141 vactive = var->yres;
1142 vsync_start = vactive + var->lower_margin;
1143 vsync_end = vsync_start + var->vsync_len;
1144 vtotal = vsync_end + var->upper_margin;
1145 vblank_start = vactive;
1146 vblank_end = vtotal;
1147 vblank_end = vsync_end + 1;
1149 DBG_MSG("V: act %d, ss %d, se %d, tot %d bs %d, be %d\n",
1150 vactive, vsync_start, vsync_end, vtotal, vblank_start,
1153 /* Adjust for register values, and check for overflow. */
1155 if (check_overflow(hactive, HACTIVE_MASK, "CRTC hactive"))
1158 if (check_overflow(hsync_start, HSYNCSTART_MASK, "CRTC hsync_start"))
1161 if (check_overflow(hsync_end, HSYNCEND_MASK, "CRTC hsync_end"))
1164 if (check_overflow(htotal, HTOTAL_MASK, "CRTC htotal"))
1167 if (check_overflow(hblank_start, HBLANKSTART_MASK, "CRTC hblank_start"))
1170 if (check_overflow(hblank_end, HBLANKEND_MASK, "CRTC hblank_end"))
1174 if (check_overflow(vactive, VACTIVE_MASK, "CRTC vactive"))
1177 if (check_overflow(vsync_start, VSYNCSTART_MASK, "CRTC vsync_start"))
1180 if (check_overflow(vsync_end, VSYNCEND_MASK, "CRTC vsync_end"))
1183 if (check_overflow(vtotal, VTOTAL_MASK, "CRTC vtotal"))
1186 if (check_overflow(vblank_start, VBLANKSTART_MASK, "CRTC vblank_start"))
1189 if (check_overflow(vblank_end, VBLANKEND_MASK, "CRTC vblank_end"))
1192 *ht = (htotal << HTOTAL_SHIFT) | (hactive << HACTIVE_SHIFT);
1193 *hb = (hblank_start << HBLANKSTART_SHIFT) |
1194 (hblank_end << HSYNCEND_SHIFT);
1195 *hs = (hsync_start << HSYNCSTART_SHIFT) | (hsync_end << HSYNCEND_SHIFT);
1197 *vt = (vtotal << VTOTAL_SHIFT) | (vactive << VACTIVE_SHIFT);
1198 *vb = (vblank_start << VBLANKSTART_SHIFT) |
1199 (vblank_end << VSYNCEND_SHIFT);
1200 *vs = (vsync_start << VSYNCSTART_SHIFT) | (vsync_end << VSYNCEND_SHIFT);
1201 *ss = (hactive << SRC_SIZE_HORIZ_SHIFT) |
1202 (vactive << SRC_SIZE_VERT_SHIFT);
1204 hw->disp_a_stride = dinfo->pitch;
1205 DBG_MSG("pitch is %d\n", hw->disp_a_stride);
1207 hw->disp_a_base = hw->disp_a_stride * var->yoffset +
1208 var->xoffset * var->bits_per_pixel / 8;
1210 hw->disp_a_base += dinfo->fb.offset << 12;
1212 /* Check stride alignment. */
1213 stride_alignment = IS_I9XX(dinfo) ? STRIDE_ALIGNMENT_I9XX :
1215 if (hw->disp_a_stride % stride_alignment != 0) {
1216 WRN_MSG("display stride %d has bad alignment %d\n",
1217 hw->disp_a_stride, stride_alignment);
1221 /* Set the palette to 8-bit mode. */
1222 *pipe_conf &= ~PIPECONF_GAMMA;
1226 /* Program a (non-VGA) video mode. */
1228 intelfbhw_program_mode(struct intelfb_info *dinfo,
1229 const struct intelfb_hwstate *hw, int blank)
1233 const u32 *dpll, *fp0, *fp1, *pipe_conf;
1234 const u32 *hs, *ht, *hb, *vs, *vt, *vb, *ss;
1235 u32 dpll_reg, fp0_reg, fp1_reg, pipe_conf_reg;
1236 u32 hsync_reg, htotal_reg, hblank_reg;
1237 u32 vsync_reg, vtotal_reg, vblank_reg;
1239 u32 count, tmp_val[3];
1241 /* Assume single pipe, display plane A, analog CRT. */
1244 DBG_MSG("intelfbhw_program_mode\n");
1248 tmp = INREG(VGACNTRL);
1250 OUTREG(VGACNTRL, tmp);
1252 /* Check whether pipe A or pipe B is enabled. */
1253 if (hw->pipe_a_conf & PIPECONF_ENABLE)
1255 else if (hw->pipe_b_conf & PIPECONF_ENABLE)
1260 if (pipe == PIPE_B) {
1264 pipe_conf = &hw->pipe_b_conf;
1271 ss = &hw->src_size_b;
1275 pipe_conf_reg = PIPEBCONF;
1276 hsync_reg = HSYNC_B;
1277 htotal_reg = HTOTAL_B;
1278 hblank_reg = HBLANK_B;
1279 vsync_reg = VSYNC_B;
1280 vtotal_reg = VTOTAL_B;
1281 vblank_reg = VBLANK_B;
1282 src_size_reg = SRC_SIZE_B;
1287 pipe_conf = &hw->pipe_a_conf;
1294 ss = &hw->src_size_a;
1298 pipe_conf_reg = PIPEACONF;
1299 hsync_reg = HSYNC_A;
1300 htotal_reg = HTOTAL_A;
1301 hblank_reg = HBLANK_A;
1302 vsync_reg = VSYNC_A;
1303 vtotal_reg = VTOTAL_A;
1304 vblank_reg = VBLANK_A;
1305 src_size_reg = SRC_SIZE_A;
1309 tmp = INREG(pipe_conf_reg);
1310 tmp &= ~PIPECONF_ENABLE;
1311 OUTREG(pipe_conf_reg, tmp);
1315 tmp_val[count%3] = INREG(0x70000);
1316 if ((tmp_val[0] == tmp_val[1]) && (tmp_val[1]==tmp_val[2]))
1320 if (count % 200 == 0) {
1321 tmp = INREG(pipe_conf_reg);
1322 tmp &= ~PIPECONF_ENABLE;
1323 OUTREG(pipe_conf_reg, tmp);
1325 } while(count < 2000);
1327 OUTREG(ADPA, INREG(ADPA) & ~ADPA_DAC_ENABLE);
1329 /* Disable planes A and B. */
1330 tmp = INREG(DSPACNTR);
1331 tmp &= ~DISPPLANE_PLANE_ENABLE;
1332 OUTREG(DSPACNTR, tmp);
1333 tmp = INREG(DSPBCNTR);
1334 tmp &= ~DISPPLANE_PLANE_ENABLE;
1335 OUTREG(DSPBCNTR, tmp);
1337 /* Wait for vblank. For now, just wait for a 50Hz cycle (20ms)) */
1340 OUTREG(DVOB, INREG(DVOB) & ~PORT_ENABLE);
1341 OUTREG(DVOC, INREG(DVOC) & ~PORT_ENABLE);
1342 OUTREG(ADPA, INREG(ADPA) & ~ADPA_DAC_ENABLE);
1346 tmp &= ~ADPA_DPMS_CONTROL_MASK;
1347 tmp |= ADPA_DPMS_D3;
1350 /* do some funky magic - xyzzy */
1351 OUTREG(0x61204, 0xabcd0000);
1354 tmp = INREG(dpll_reg);
1355 dpll_reg &= ~DPLL_VCO_ENABLE;
1356 OUTREG(dpll_reg, tmp);
1358 /* Set PLL parameters */
1359 OUTREG(fp0_reg, *fp0);
1360 OUTREG(fp1_reg, *fp1);
1363 OUTREG(dpll_reg, *dpll);
1366 OUTREG(DVOB, hw->dvob);
1367 OUTREG(DVOC, hw->dvoc);
1369 /* undo funky magic */
1370 OUTREG(0x61204, 0x00000000);
1373 OUTREG(ADPA, INREG(ADPA) | ADPA_DAC_ENABLE);
1374 OUTREG(ADPA, (hw->adpa & ~(ADPA_DPMS_CONTROL_MASK)) | ADPA_DPMS_D3);
1376 /* Set pipe parameters */
1377 OUTREG(hsync_reg, *hs);
1378 OUTREG(hblank_reg, *hb);
1379 OUTREG(htotal_reg, *ht);
1380 OUTREG(vsync_reg, *vs);
1381 OUTREG(vblank_reg, *vb);
1382 OUTREG(vtotal_reg, *vt);
1383 OUTREG(src_size_reg, *ss);
1386 OUTREG(pipe_conf_reg, *pipe_conf | PIPECONF_ENABLE);
1390 tmp &= ~ADPA_DPMS_CONTROL_MASK;
1391 tmp |= ADPA_DPMS_D0;
1394 /* setup display plane */
1395 if (dinfo->pdev->device == PCI_DEVICE_ID_INTEL_830M) {
1397 * i830M errata: the display plane must be enabled
1398 * to allow writes to the other bits in the plane
1401 tmp = INREG(DSPACNTR);
1402 if ((tmp & DISPPLANE_PLANE_ENABLE) != DISPPLANE_PLANE_ENABLE) {
1403 tmp |= DISPPLANE_PLANE_ENABLE;
1404 OUTREG(DSPACNTR, tmp);
1406 hw->disp_a_ctrl|DISPPLANE_PLANE_ENABLE);
1411 OUTREG(DSPACNTR, hw->disp_a_ctrl & ~DISPPLANE_PLANE_ENABLE);
1412 OUTREG(DSPASTRIDE, hw->disp_a_stride);
1413 OUTREG(DSPABASE, hw->disp_a_base);
1417 tmp = INREG(DSPACNTR);
1418 tmp |= DISPPLANE_PLANE_ENABLE;
1419 OUTREG(DSPACNTR, tmp);
1420 OUTREG(DSPABASE, hw->disp_a_base);
1426 /* forward declarations */
1427 static void refresh_ring(struct intelfb_info *dinfo);
1428 static void reset_state(struct intelfb_info *dinfo);
1429 static void do_flush(struct intelfb_info *dinfo);
1432 wait_ring(struct intelfb_info *dinfo, int n)
1436 u32 last_head = INREG(PRI_RING_HEAD) & RING_HEAD_MASK;
1439 DBG_MSG("wait_ring: %d\n", n);
1442 end = jiffies + (HZ * 3);
1443 while (dinfo->ring_space < n) {
1444 dinfo->ring_head = INREG(PRI_RING_HEAD) & RING_HEAD_MASK;
1445 if (dinfo->ring_tail + RING_MIN_FREE < dinfo->ring_head)
1446 dinfo->ring_space = dinfo->ring_head
1447 - (dinfo->ring_tail + RING_MIN_FREE);
1449 dinfo->ring_space = (dinfo->ring.size +
1451 - (dinfo->ring_tail + RING_MIN_FREE);
1452 if (dinfo->ring_head != last_head) {
1453 end = jiffies + (HZ * 3);
1454 last_head = dinfo->ring_head;
1457 if (time_before(end, jiffies)) {
1461 refresh_ring(dinfo);
1463 end = jiffies + (HZ * 3);
1466 WRN_MSG("ring buffer : space: %d wanted %d\n",
1467 dinfo->ring_space, n);
1468 WRN_MSG("lockup - turning off hardware "
1470 dinfo->ring_lockup = 1;
1480 do_flush(struct intelfb_info *dinfo) {
1482 OUT_RING(MI_FLUSH | MI_WRITE_DIRTY_STATE | MI_INVALIDATE_MAP_CACHE);
1488 intelfbhw_do_sync(struct intelfb_info *dinfo)
1491 DBG_MSG("intelfbhw_do_sync\n");
1498 * Send a flush, then wait until the ring is empty. This is what
1499 * the XFree86 driver does, and actually it doesn't seem a lot worse
1500 * than the recommended method (both have problems).
1503 wait_ring(dinfo, dinfo->ring.size - RING_MIN_FREE);
1504 dinfo->ring_space = dinfo->ring.size - RING_MIN_FREE;
1508 refresh_ring(struct intelfb_info *dinfo)
1511 DBG_MSG("refresh_ring\n");
1514 dinfo->ring_head = INREG(PRI_RING_HEAD) & RING_HEAD_MASK;
1515 dinfo->ring_tail = INREG(PRI_RING_TAIL) & RING_TAIL_MASK;
1516 if (dinfo->ring_tail + RING_MIN_FREE < dinfo->ring_head)
1517 dinfo->ring_space = dinfo->ring_head
1518 - (dinfo->ring_tail + RING_MIN_FREE);
1520 dinfo->ring_space = (dinfo->ring.size + dinfo->ring_head)
1521 - (dinfo->ring_tail + RING_MIN_FREE);
1525 reset_state(struct intelfb_info *dinfo)
1531 DBG_MSG("reset_state\n");
1534 for (i = 0; i < FENCE_NUM; i++)
1535 OUTREG(FENCE + (i << 2), 0);
1537 /* Flush the ring buffer if it's enabled. */
1538 tmp = INREG(PRI_RING_LENGTH);
1539 if (tmp & RING_ENABLE) {
1541 DBG_MSG("reset_state: ring was enabled\n");
1543 refresh_ring(dinfo);
1544 intelfbhw_do_sync(dinfo);
1548 OUTREG(PRI_RING_LENGTH, 0);
1549 OUTREG(PRI_RING_HEAD, 0);
1550 OUTREG(PRI_RING_TAIL, 0);
1551 OUTREG(PRI_RING_START, 0);
1554 /* Stop the 2D engine, and turn off the ring buffer. */
1556 intelfbhw_2d_stop(struct intelfb_info *dinfo)
1559 DBG_MSG("intelfbhw_2d_stop: accel: %d, ring_active: %d\n", dinfo->accel,
1560 dinfo->ring_active);
1566 dinfo->ring_active = 0;
1571 * Enable the ring buffer, and initialise the 2D engine.
1572 * It is assumed that the graphics engine has been stopped by previously
1573 * calling intelfb_2d_stop().
1576 intelfbhw_2d_start(struct intelfb_info *dinfo)
1579 DBG_MSG("intelfbhw_2d_start: accel: %d, ring_active: %d\n",
1580 dinfo->accel, dinfo->ring_active);
1586 /* Initialise the primary ring buffer. */
1587 OUTREG(PRI_RING_LENGTH, 0);
1588 OUTREG(PRI_RING_TAIL, 0);
1589 OUTREG(PRI_RING_HEAD, 0);
1591 OUTREG(PRI_RING_START, dinfo->ring.physical & RING_START_MASK);
1592 OUTREG(PRI_RING_LENGTH,
1593 ((dinfo->ring.size - GTT_PAGE_SIZE) & RING_LENGTH_MASK) |
1594 RING_NO_REPORT | RING_ENABLE);
1595 refresh_ring(dinfo);
1596 dinfo->ring_active = 1;
1599 /* 2D fillrect (solid fill or invert) */
1601 intelfbhw_do_fillrect(struct intelfb_info *dinfo, u32 x, u32 y, u32 w, u32 h,
1602 u32 color, u32 pitch, u32 bpp, u32 rop)
1604 u32 br00, br09, br13, br14, br16;
1607 DBG_MSG("intelfbhw_do_fillrect: (%d,%d) %dx%d, c 0x%06x, p %d bpp %d, "
1608 "rop 0x%02x\n", x, y, w, h, color, pitch, bpp, rop);
1611 br00 = COLOR_BLT_CMD;
1612 br09 = dinfo->fb_start + (y * pitch + x * (bpp / 8));
1613 br13 = (rop << ROP_SHIFT) | pitch;
1614 br14 = (h << HEIGHT_SHIFT) | ((w * (bpp / 8)) << WIDTH_SHIFT);
1619 br13 |= COLOR_DEPTH_8;
1622 br13 |= COLOR_DEPTH_16;
1625 br13 |= COLOR_DEPTH_32;
1626 br00 |= WRITE_ALPHA | WRITE_RGB;
1640 DBG_MSG("ring = 0x%08x, 0x%08x (%d)\n", dinfo->ring_head,
1641 dinfo->ring_tail, dinfo->ring_space);
1646 intelfbhw_do_bitblt(struct intelfb_info *dinfo, u32 curx, u32 cury,
1647 u32 dstx, u32 dsty, u32 w, u32 h, u32 pitch, u32 bpp)
1649 u32 br00, br09, br11, br12, br13, br22, br23, br26;
1652 DBG_MSG("intelfbhw_do_bitblt: (%d,%d)->(%d,%d) %dx%d, p %d bpp %d\n",
1653 curx, cury, dstx, dsty, w, h, pitch, bpp);
1656 br00 = XY_SRC_COPY_BLT_CMD;
1657 br09 = dinfo->fb_start;
1658 br11 = (pitch << PITCH_SHIFT);
1659 br12 = dinfo->fb_start;
1660 br13 = (SRC_ROP_GXCOPY << ROP_SHIFT) | (pitch << PITCH_SHIFT);
1661 br22 = (dstx << WIDTH_SHIFT) | (dsty << HEIGHT_SHIFT);
1662 br23 = ((dstx + w) << WIDTH_SHIFT) |
1663 ((dsty + h) << HEIGHT_SHIFT);
1664 br26 = (curx << WIDTH_SHIFT) | (cury << HEIGHT_SHIFT);
1668 br13 |= COLOR_DEPTH_8;
1671 br13 |= COLOR_DEPTH_16;
1674 br13 |= COLOR_DEPTH_32;
1675 br00 |= WRITE_ALPHA | WRITE_RGB;
1692 intelfbhw_do_drawglyph(struct intelfb_info *dinfo, u32 fg, u32 bg, u32 w,
1693 u32 h, const u8* cdat, u32 x, u32 y, u32 pitch, u32 bpp)
1695 int nbytes, ndwords, pad, tmp;
1696 u32 br00, br09, br13, br18, br19, br22, br23;
1697 int dat, ix, iy, iw;
1701 DBG_MSG("intelfbhw_do_drawglyph: (%d,%d) %dx%d\n", x, y, w, h);
1704 /* size in bytes of a padded scanline */
1705 nbytes = ROUND_UP_TO(w, 16) / 8;
1707 /* Total bytes of padded scanline data to write out. */
1708 nbytes = nbytes * h;
1711 * Check if the glyph data exceeds the immediate mode limit.
1712 * It would take a large font (1K pixels) to hit this limit.
1714 if (nbytes > MAX_MONO_IMM_SIZE)
1717 /* Src data is packaged a dword (32-bit) at a time. */
1718 ndwords = ROUND_UP_TO(nbytes, 4) / 4;
1721 * Ring has to be padded to a quad word. But because the command starts
1722 with 7 bytes, pad only if there is an even number of ndwords
1724 pad = !(ndwords % 2);
1726 tmp = (XY_MONO_SRC_IMM_BLT_CMD & DW_LENGTH_MASK) + ndwords;
1727 br00 = (XY_MONO_SRC_IMM_BLT_CMD & ~DW_LENGTH_MASK) | tmp;
1728 br09 = dinfo->fb_start;
1729 br13 = (SRC_ROP_GXCOPY << ROP_SHIFT) | (pitch << PITCH_SHIFT);
1732 br22 = (x << WIDTH_SHIFT) | (y << HEIGHT_SHIFT);
1733 br23 = ((x + w) << WIDTH_SHIFT) | ((y + h) << HEIGHT_SHIFT);
1737 br13 |= COLOR_DEPTH_8;
1740 br13 |= COLOR_DEPTH_16;
1743 br13 |= COLOR_DEPTH_32;
1744 br00 |= WRITE_ALPHA | WRITE_RGB;
1748 START_RING(8 + ndwords);
1757 iw = ROUND_UP_TO(w, 8) / 8;
1760 for (j = 0; j < 2; ++j) {
1761 for (i = 0; i < 2; ++i) {
1762 if (ix != iw || i == 0)
1763 dat |= cdat[iy*iw + ix++] << (i+j*2)*8;
1765 if (ix == iw && iy != (h-1)) {
1779 /* HW cursor functions. */
1781 intelfbhw_cursor_init(struct intelfb_info *dinfo)
1786 DBG_MSG("intelfbhw_cursor_init\n");
1789 if (dinfo->mobile || IS_I9XX(dinfo)) {
1790 if (!dinfo->cursor.physical)
1792 tmp = INREG(CURSOR_A_CONTROL);
1793 tmp &= ~(CURSOR_MODE_MASK | CURSOR_MOBILE_GAMMA_ENABLE |
1794 CURSOR_MEM_TYPE_LOCAL |
1795 (1 << CURSOR_PIPE_SELECT_SHIFT));
1796 tmp |= CURSOR_MODE_DISABLE;
1797 OUTREG(CURSOR_A_CONTROL, tmp);
1798 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.physical);
1800 tmp = INREG(CURSOR_CONTROL);
1801 tmp &= ~(CURSOR_FORMAT_MASK | CURSOR_GAMMA_ENABLE |
1802 CURSOR_ENABLE | CURSOR_STRIDE_MASK);
1803 tmp = CURSOR_FORMAT_3C;
1804 OUTREG(CURSOR_CONTROL, tmp);
1805 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.offset << 12);
1806 tmp = (64 << CURSOR_SIZE_H_SHIFT) |
1807 (64 << CURSOR_SIZE_V_SHIFT);
1808 OUTREG(CURSOR_SIZE, tmp);
1813 intelfbhw_cursor_hide(struct intelfb_info *dinfo)
1818 DBG_MSG("intelfbhw_cursor_hide\n");
1821 dinfo->cursor_on = 0;
1822 if (dinfo->mobile || IS_I9XX(dinfo)) {
1823 if (!dinfo->cursor.physical)
1825 tmp = INREG(CURSOR_A_CONTROL);
1826 tmp &= ~CURSOR_MODE_MASK;
1827 tmp |= CURSOR_MODE_DISABLE;
1828 OUTREG(CURSOR_A_CONTROL, tmp);
1830 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.physical);
1832 tmp = INREG(CURSOR_CONTROL);
1833 tmp &= ~CURSOR_ENABLE;
1834 OUTREG(CURSOR_CONTROL, tmp);
1839 intelfbhw_cursor_show(struct intelfb_info *dinfo)
1844 DBG_MSG("intelfbhw_cursor_show\n");
1847 dinfo->cursor_on = 1;
1849 if (dinfo->cursor_blanked)
1852 if (dinfo->mobile || IS_I9XX(dinfo)) {
1853 if (!dinfo->cursor.physical)
1855 tmp = INREG(CURSOR_A_CONTROL);
1856 tmp &= ~CURSOR_MODE_MASK;
1857 tmp |= CURSOR_MODE_64_4C_AX;
1858 OUTREG(CURSOR_A_CONTROL, tmp);
1860 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.physical);
1862 tmp = INREG(CURSOR_CONTROL);
1863 tmp |= CURSOR_ENABLE;
1864 OUTREG(CURSOR_CONTROL, tmp);
1869 intelfbhw_cursor_setpos(struct intelfb_info *dinfo, int x, int y)
1874 DBG_MSG("intelfbhw_cursor_setpos: (%d, %d)\n", x, y);
1878 * Sets the position. The coordinates are assumed to already
1879 * have any offset adjusted. Assume that the cursor is never
1880 * completely off-screen, and that x, y are always >= 0.
1883 tmp = ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT) |
1884 ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
1885 OUTREG(CURSOR_A_POSITION, tmp);
1887 if (IS_I9XX(dinfo)) {
1888 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.physical);
1893 intelfbhw_cursor_setcolor(struct intelfb_info *dinfo, u32 bg, u32 fg)
1896 DBG_MSG("intelfbhw_cursor_setcolor\n");
1899 OUTREG(CURSOR_A_PALETTE0, bg & CURSOR_PALETTE_MASK);
1900 OUTREG(CURSOR_A_PALETTE1, fg & CURSOR_PALETTE_MASK);
1901 OUTREG(CURSOR_A_PALETTE2, fg & CURSOR_PALETTE_MASK);
1902 OUTREG(CURSOR_A_PALETTE3, bg & CURSOR_PALETTE_MASK);
1906 intelfbhw_cursor_load(struct intelfb_info *dinfo, int width, int height,
1909 u8 __iomem *addr = (u8 __iomem *)dinfo->cursor.virtual;
1910 int i, j, w = width / 8;
1911 int mod = width % 8, t_mask, d_mask;
1914 DBG_MSG("intelfbhw_cursor_load\n");
1917 if (!dinfo->cursor.virtual)
1920 t_mask = 0xff >> mod;
1921 d_mask = ~(0xff >> mod);
1922 for (i = height; i--; ) {
1923 for (j = 0; j < w; j++) {
1924 writeb(0x00, addr + j);
1925 writeb(*(data++), addr + j+8);
1928 writeb(t_mask, addr + j);
1929 writeb(*(data++) & d_mask, addr + j+8);
1936 intelfbhw_cursor_reset(struct intelfb_info *dinfo) {
1937 u8 __iomem *addr = (u8 __iomem *)dinfo->cursor.virtual;
1941 DBG_MSG("intelfbhw_cursor_reset\n");
1944 if (!dinfo->cursor.virtual)
1947 for (i = 64; i--; ) {
1948 for (j = 0; j < 8; j++) {
1949 writeb(0xff, addr + j+0);
1950 writeb(0x00, addr + j+8);
1957 intelfbhw_irq(int irq, void *dev_id) {
1960 struct intelfb_info *dinfo = (struct intelfb_info *)dev_id;
1962 spin_lock(&dinfo->int_lock);
1965 tmp &= VSYNC_PIPE_A_INTERRUPT;
1968 spin_unlock(&dinfo->int_lock);
1969 return IRQ_RETVAL(handled);
1974 if (tmp & VSYNC_PIPE_A_INTERRUPT) {
1975 dinfo->vsync.count++;
1976 if (dinfo->vsync.pan_display) {
1977 dinfo->vsync.pan_display = 0;
1978 OUTREG(DSPABASE, dinfo->vsync.pan_offset);
1980 wake_up_interruptible(&dinfo->vsync.wait);
1984 spin_unlock(&dinfo->int_lock);
1986 return IRQ_RETVAL(handled);
1990 intelfbhw_enable_irq(struct intelfb_info *dinfo, int reenable) {
1992 if (!test_and_set_bit(0, &dinfo->irq_flags)) {
1993 if (request_irq(dinfo->pdev->irq, intelfbhw_irq, IRQF_SHARED,
1994 "intelfb", dinfo)) {
1995 clear_bit(0, &dinfo->irq_flags);
1999 spin_lock_irq(&dinfo->int_lock);
2000 OUTREG16(HWSTAM, 0xfffe);
2002 OUTREG16(IER, VSYNC_PIPE_A_INTERRUPT);
2003 spin_unlock_irq(&dinfo->int_lock);
2004 } else if (reenable) {
2007 spin_lock_irq(&dinfo->int_lock);
2009 if ((ier & VSYNC_PIPE_A_INTERRUPT)) {
2010 DBG_MSG("someone disabled the IRQ [%08X]\n", ier);
2011 OUTREG(IER, VSYNC_PIPE_A_INTERRUPT);
2013 spin_unlock_irq(&dinfo->int_lock);
2019 intelfbhw_disable_irq(struct intelfb_info *dinfo) {
2022 if (test_and_clear_bit(0, &dinfo->irq_flags)) {
2023 if (dinfo->vsync.pan_display) {
2024 dinfo->vsync.pan_display = 0;
2025 OUTREG(DSPABASE, dinfo->vsync.pan_offset);
2027 spin_lock_irq(&dinfo->int_lock);
2028 OUTREG16(HWSTAM, 0xffff);
2029 OUTREG16(IMR, 0xffff);
2034 spin_unlock_irq(&dinfo->int_lock);
2036 free_irq(dinfo->pdev->irq, dinfo);
2041 intelfbhw_wait_for_vsync(struct intelfb_info *dinfo, u32 pipe) {
2042 struct intelfb_vsync *vsync;
2048 vsync = &dinfo->vsync;
2054 ret = intelfbhw_enable_irq(dinfo, 0);
2059 count = vsync->count;
2060 ret = wait_event_interruptible_timeout(vsync->wait, count != vsync->count, HZ/10);
2065 intelfbhw_enable_irq(dinfo, 1);
2066 DBG_MSG("wait_for_vsync timed out!\n");