blob: 5a621df5bf0224bdfb96da65f53178e6a02e9e33 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * intelfb
3 *
4 * Linux framebuffer driver for Intel(R) 865G integrated graphics chips.
5 *
6 * Copyright © 2002, 2003 David Dawes <dawes@xfree86.org>
7 * 2004 Sylvain Meyer
8 *
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).
15 *
16 * Author: David Dawes
17 *
18 */
19
20/* $DHD: intelfb/intelfbhw.c,v 1.9 2003/06/27 15:06:25 dawes Exp $ */
21
22#include <linux/config.h>
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/errno.h>
26#include <linux/string.h>
27#include <linux/mm.h>
28#include <linux/tty.h>
29#include <linux/slab.h>
30#include <linux/delay.h>
31#include <linux/fb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/ioport.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/pagemap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include <asm/io.h>
39
40#include "intelfb.h"
41#include "intelfbhw.h"
42
Dave Airlie7258b112006-03-20 20:02:24 +110043struct pll_min_max {
Dave Airlie51d79742006-04-03 16:19:26 +100044 int min_m, max_m, min_m1, max_m1;
45 int min_m2, max_m2, min_n, max_n;
46 int min_p, max_p, min_p1, max_p1;
47 int min_vco, max_vco, p_transition_clk, ref_clk;
Dave Airlie16109b32006-03-20 21:22:09 +110048 int p_inc_lo, p_inc_hi;
Dave Airlie7258b112006-03-20 20:02:24 +110049};
50
51#define PLLS_I8xx 0
52#define PLLS_I9xx 1
53#define PLLS_MAX 2
54
Dave Airlie46f60b82006-03-24 12:31:14 +110055static struct pll_min_max plls[PLLS_MAX] = {
Dave Airlie51d79742006-04-03 16:19:26 +100056 { 108, 140, 18, 26,
57 6, 16, 3, 16,
58 4, 128, 0, 31,
59 930000, 1400000, 165000, 48000,
60 4, 2 }, //I8xx
61
62 { 75, 120, 10, 20,
63 5, 9, 4, 7,
64 5, 80, 1, 8,
65 1400000, 2800000, 200000, 96000,
66 10, 5 } //I9xx
Dave Airlie7258b112006-03-20 20:02:24 +110067};
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069int
Dave Airlied0249602006-03-20 20:26:45 +110070intelfbhw_get_chipset(struct pci_dev *pdev, struct intelfb_info *dinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 u32 tmp;
Dave Airlied0249602006-03-20 20:26:45 +110073 if (!pdev || !dinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return 1;
75
76 switch (pdev->device) {
77 case PCI_DEVICE_ID_INTEL_830M:
Dave Airlied0249602006-03-20 20:26:45 +110078 dinfo->name = "Intel(R) 830M";
79 dinfo->chipset = INTEL_830M;
80 dinfo->mobile = 1;
81 dinfo->pll_index = PLLS_I8xx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83 case PCI_DEVICE_ID_INTEL_845G:
Dave Airlied0249602006-03-20 20:26:45 +110084 dinfo->name = "Intel(R) 845G";
85 dinfo->chipset = INTEL_845G;
86 dinfo->mobile = 0;
87 dinfo->pll_index = PLLS_I8xx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
89 case PCI_DEVICE_ID_INTEL_85XGM:
90 tmp = 0;
Dave Airlied0249602006-03-20 20:26:45 +110091 dinfo->mobile = 1;
92 dinfo->pll_index = PLLS_I8xx;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pci_read_config_dword(pdev, INTEL_85X_CAPID, &tmp);
94 switch ((tmp >> INTEL_85X_VARIANT_SHIFT) &
95 INTEL_85X_VARIANT_MASK) {
96 case INTEL_VAR_855GME:
Dave Airlied0249602006-03-20 20:26:45 +110097 dinfo->name = "Intel(R) 855GME";
98 dinfo->chipset = INTEL_855GME;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return 0;
100 case INTEL_VAR_855GM:
Dave Airlied0249602006-03-20 20:26:45 +1100101 dinfo->name = "Intel(R) 855GM";
102 dinfo->chipset = INTEL_855GM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104 case INTEL_VAR_852GME:
Dave Airlied0249602006-03-20 20:26:45 +1100105 dinfo->name = "Intel(R) 852GME";
106 dinfo->chipset = INTEL_852GME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return 0;
108 case INTEL_VAR_852GM:
Dave Airlied0249602006-03-20 20:26:45 +1100109 dinfo->name = "Intel(R) 852GM";
110 dinfo->chipset = INTEL_852GM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return 0;
112 default:
Dave Airlied0249602006-03-20 20:26:45 +1100113 dinfo->name = "Intel(R) 852GM/855GM";
114 dinfo->chipset = INTEL_85XGM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116 }
117 break;
118 case PCI_DEVICE_ID_INTEL_865G:
Dave Airlied0249602006-03-20 20:26:45 +1100119 dinfo->name = "Intel(R) 865G";
120 dinfo->chipset = INTEL_865G;
121 dinfo->mobile = 0;
122 dinfo->pll_index = PLLS_I8xx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124 case PCI_DEVICE_ID_INTEL_915G:
Dave Airlied0249602006-03-20 20:26:45 +1100125 dinfo->name = "Intel(R) 915G";
126 dinfo->chipset = INTEL_915G;
127 dinfo->mobile = 0;
128 dinfo->pll_index = PLLS_I9xx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
Scott MacKenzie3a590262005-11-07 01:00:33 -0800130 case PCI_DEVICE_ID_INTEL_915GM:
Dave Airlied0249602006-03-20 20:26:45 +1100131 dinfo->name = "Intel(R) 915GM";
132 dinfo->chipset = INTEL_915GM;
133 dinfo->mobile = 1;
134 dinfo->pll_index = PLLS_I9xx;
Scott MacKenzie3a590262005-11-07 01:00:33 -0800135 return 0;
Dave Airlie9639d5e2006-03-23 11:23:55 +1100136 case PCI_DEVICE_ID_INTEL_945G:
137 dinfo->name = "Intel(R) 945G";
138 dinfo->chipset = INTEL_945G;
139 dinfo->mobile = 0;
140 dinfo->pll_index = PLLS_I9xx;
141 return 0;
Dave Airlie9a906032006-03-23 21:53:05 +1100142 case PCI_DEVICE_ID_INTEL_945GM:
143 dinfo->name = "Intel(R) 945GM";
144 dinfo->chipset = INTEL_945GM;
145 dinfo->mobile = 1;
146 dinfo->pll_index = PLLS_I9xx;
147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 default:
149 return 1;
150 }
151}
152
153int
154intelfbhw_get_memory(struct pci_dev *pdev, int *aperture_size,
155 int *stolen_size)
156{
157 struct pci_dev *bridge_dev;
158 u16 tmp;
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000159 int stolen_overhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (!pdev || !aperture_size || !stolen_size)
162 return 1;
163
164 /* Find the bridge device. It is always 0:0.0 */
165 if (!(bridge_dev = pci_find_slot(0, PCI_DEVFN(0, 0)))) {
166 ERR_MSG("cannot find bridge device\n");
167 return 1;
168 }
169
170 /* Get the fb aperture size and "stolen" memory amount. */
171 tmp = 0;
172 pci_read_config_word(bridge_dev, INTEL_GMCH_CTRL, &tmp);
173 switch (pdev->device) {
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000174 case PCI_DEVICE_ID_INTEL_915G:
175 case PCI_DEVICE_ID_INTEL_915GM:
176 case PCI_DEVICE_ID_INTEL_945G:
177 case PCI_DEVICE_ID_INTEL_945GM:
178 /* 915 and 945 chipsets support a 256MB aperture.
179 Aperture size is determined by inspected the
180 base address of the aperture. */
181 if (pci_resource_start(pdev, 2) & 0x08000000)
182 *aperture_size = MB(128);
183 else
184 *aperture_size = MB(256);
185 break;
186 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if ((tmp & INTEL_GMCH_MEM_MASK) == INTEL_GMCH_MEM_64M)
188 *aperture_size = MB(64);
189 else
190 *aperture_size = MB(128);
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000191 break;
192 }
193
194 /* Stolen memory size is reduced by the GTT and the popup.
195 GTT is 1K per MB of aperture size, and popup is 4K. */
196 stolen_overhead = (*aperture_size / MB(1)) + 4;
197 switch(pdev->device) {
198 case PCI_DEVICE_ID_INTEL_830M:
199 case PCI_DEVICE_ID_INTEL_845G:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 switch (tmp & INTEL_830_GMCH_GMS_MASK) {
201 case INTEL_830_GMCH_GMS_STOLEN_512:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000202 *stolen_size = KB(512) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return 0;
204 case INTEL_830_GMCH_GMS_STOLEN_1024:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000205 *stolen_size = MB(1) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207 case INTEL_830_GMCH_GMS_STOLEN_8192:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000208 *stolen_size = MB(8) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210 case INTEL_830_GMCH_GMS_LOCAL:
211 ERR_MSG("only local memory found\n");
212 return 1;
213 case INTEL_830_GMCH_GMS_DISABLED:
214 ERR_MSG("video memory is disabled\n");
215 return 1;
216 default:
217 ERR_MSG("unexpected GMCH_GMS value: 0x%02x\n",
218 tmp & INTEL_830_GMCH_GMS_MASK);
219 return 1;
220 }
221 break;
222 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 switch (tmp & INTEL_855_GMCH_GMS_MASK) {
224 case INTEL_855_GMCH_GMS_STOLEN_1M:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000225 *stolen_size = MB(1) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return 0;
227 case INTEL_855_GMCH_GMS_STOLEN_4M:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000228 *stolen_size = MB(4) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return 0;
230 case INTEL_855_GMCH_GMS_STOLEN_8M:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000231 *stolen_size = MB(8) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return 0;
233 case INTEL_855_GMCH_GMS_STOLEN_16M:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000234 *stolen_size = MB(16) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return 0;
236 case INTEL_855_GMCH_GMS_STOLEN_32M:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000237 *stolen_size = MB(32) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return 0;
239 case INTEL_915G_GMCH_GMS_STOLEN_48M:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000240 *stolen_size = MB(48) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return 0;
242 case INTEL_915G_GMCH_GMS_STOLEN_64M:
Eric Hustvedt1aecb392006-05-27 18:30:00 +1000243 *stolen_size = MB(64) - KB(stolen_overhead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245 case INTEL_855_GMCH_GMS_DISABLED:
246 ERR_MSG("video memory is disabled\n");
247 return 0;
248 default:
249 ERR_MSG("unexpected GMCH_GMS value: 0x%02x\n",
250 tmp & INTEL_855_GMCH_GMS_MASK);
251 return 1;
252 }
253 }
254}
255
256int
257intelfbhw_check_non_crt(struct intelfb_info *dinfo)
258{
259 int dvo = 0;
260
261 if (INREG(LVDS) & PORT_ENABLE)
262 dvo |= LVDS_PORT;
263 if (INREG(DVOA) & PORT_ENABLE)
264 dvo |= DVOA_PORT;
265 if (INREG(DVOB) & PORT_ENABLE)
266 dvo |= DVOB_PORT;
267 if (INREG(DVOC) & PORT_ENABLE)
268 dvo |= DVOC_PORT;
269
270 return dvo;
271}
272
273const char *
274intelfbhw_dvo_to_string(int dvo)
275{
276 if (dvo & DVOA_PORT)
277 return "DVO port A";
278 else if (dvo & DVOB_PORT)
279 return "DVO port B";
280 else if (dvo & DVOC_PORT)
281 return "DVO port C";
282 else if (dvo & LVDS_PORT)
283 return "LVDS port";
284 else
285 return NULL;
286}
287
288
289int
290intelfbhw_validate_mode(struct intelfb_info *dinfo,
291 struct fb_var_screeninfo *var)
292{
293 int bytes_per_pixel;
294 int tmp;
295
296#if VERBOSE > 0
297 DBG_MSG("intelfbhw_validate_mode\n");
298#endif
299
300 bytes_per_pixel = var->bits_per_pixel / 8;
301 if (bytes_per_pixel == 3)
302 bytes_per_pixel = 4;
303
304 /* Check if enough video memory. */
305 tmp = var->yres_virtual * var->xres_virtual * bytes_per_pixel;
306 if (tmp > dinfo->fb.size) {
307 WRN_MSG("Not enough video ram for mode "
308 "(%d KByte vs %d KByte).\n",
309 BtoKB(tmp), BtoKB(dinfo->fb.size));
310 return 1;
311 }
312
313 /* Check if x/y limits are OK. */
314 if (var->xres - 1 > HACTIVE_MASK) {
315 WRN_MSG("X resolution too large (%d vs %d).\n",
316 var->xres, HACTIVE_MASK + 1);
317 return 1;
318 }
319 if (var->yres - 1 > VACTIVE_MASK) {
320 WRN_MSG("Y resolution too large (%d vs %d).\n",
321 var->yres, VACTIVE_MASK + 1);
322 return 1;
323 }
324
325 /* Check for interlaced/doublescan modes. */
326 if (var->vmode & FB_VMODE_INTERLACED) {
327 WRN_MSG("Mode is interlaced.\n");
328 return 1;
329 }
330 if (var->vmode & FB_VMODE_DOUBLE) {
331 WRN_MSG("Mode is double-scan.\n");
332 return 1;
333 }
334
335 /* Check if clock is OK. */
336 tmp = 1000000000 / var->pixclock;
337 if (tmp < MIN_CLOCK) {
338 WRN_MSG("Pixel clock is too low (%d MHz vs %d MHz).\n",
339 (tmp + 500) / 1000, MIN_CLOCK / 1000);
340 return 1;
341 }
342 if (tmp > MAX_CLOCK) {
343 WRN_MSG("Pixel clock is too high (%d MHz vs %d MHz).\n",
344 (tmp + 500) / 1000, MAX_CLOCK / 1000);
345 return 1;
346 }
347
348 return 0;
349}
350
351int
352intelfbhw_pan_display(struct fb_var_screeninfo *var, struct fb_info *info)
353{
354 struct intelfb_info *dinfo = GET_DINFO(info);
355 u32 offset, xoffset, yoffset;
356
357#if VERBOSE > 0
358 DBG_MSG("intelfbhw_pan_display\n");
359#endif
360
361 xoffset = ROUND_DOWN_TO(var->xoffset, 8);
362 yoffset = var->yoffset;
363
364 if ((xoffset + var->xres > var->xres_virtual) ||
365 (yoffset + var->yres > var->yres_virtual))
366 return -EINVAL;
367
368 offset = (yoffset * dinfo->pitch) +
369 (xoffset * var->bits_per_pixel) / 8;
370
371 offset += dinfo->fb.offset << 12;
372
373 OUTREG(DSPABASE, offset);
374
375 return 0;
376}
377
378/* Blank the screen. */
379void
380intelfbhw_do_blank(int blank, struct fb_info *info)
381{
382 struct intelfb_info *dinfo = GET_DINFO(info);
383 u32 tmp;
384
385#if VERBOSE > 0
386 DBG_MSG("intelfbhw_do_blank: blank is %d\n", blank);
387#endif
388
389 /* Turn plane A on or off */
390 tmp = INREG(DSPACNTR);
391 if (blank)
392 tmp &= ~DISPPLANE_PLANE_ENABLE;
393 else
394 tmp |= DISPPLANE_PLANE_ENABLE;
395 OUTREG(DSPACNTR, tmp);
396 /* Flush */
397 tmp = INREG(DSPABASE);
398 OUTREG(DSPABASE, tmp);
399
400 /* Turn off/on the HW cursor */
401#if VERBOSE > 0
402 DBG_MSG("cursor_on is %d\n", dinfo->cursor_on);
403#endif
404 if (dinfo->cursor_on) {
405 if (blank) {
406 intelfbhw_cursor_hide(dinfo);
407 } else {
408 intelfbhw_cursor_show(dinfo);
409 }
410 dinfo->cursor_on = 1;
411 }
412 dinfo->cursor_blanked = blank;
413
414 /* Set DPMS level */
415 tmp = INREG(ADPA) & ~ADPA_DPMS_CONTROL_MASK;
416 switch (blank) {
417 case FB_BLANK_UNBLANK:
418 case FB_BLANK_NORMAL:
419 tmp |= ADPA_DPMS_D0;
420 break;
421 case FB_BLANK_VSYNC_SUSPEND:
422 tmp |= ADPA_DPMS_D1;
423 break;
424 case FB_BLANK_HSYNC_SUSPEND:
425 tmp |= ADPA_DPMS_D2;
426 break;
427 case FB_BLANK_POWERDOWN:
428 tmp |= ADPA_DPMS_D3;
429 break;
430 }
431 OUTREG(ADPA, tmp);
432
433 return;
434}
435
436
437void
438intelfbhw_setcolreg(struct intelfb_info *dinfo, unsigned regno,
439 unsigned red, unsigned green, unsigned blue,
440 unsigned transp)
441{
442#if VERBOSE > 0
443 DBG_MSG("intelfbhw_setcolreg: %d: (%d, %d, %d)\n",
444 regno, red, green, blue);
445#endif
446
447 u32 palette_reg = (dinfo->pipe == PIPE_A) ?
448 PALETTE_A : PALETTE_B;
449
450 OUTREG(palette_reg + (regno << 2),
451 (red << PALETTE_8_RED_SHIFT) |
452 (green << PALETTE_8_GREEN_SHIFT) |
453 (blue << PALETTE_8_BLUE_SHIFT));
454}
455
456
457int
458intelfbhw_read_hw_state(struct intelfb_info *dinfo, struct intelfb_hwstate *hw,
459 int flag)
460{
461 int i;
462
463#if VERBOSE > 0
464 DBG_MSG("intelfbhw_read_hw_state\n");
465#endif
466
467 if (!hw || !dinfo)
468 return -1;
469
470 /* Read in as much of the HW state as possible. */
471 hw->vga0_divisor = INREG(VGA0_DIVISOR);
472 hw->vga1_divisor = INREG(VGA1_DIVISOR);
473 hw->vga_pd = INREG(VGAPD);
474 hw->dpll_a = INREG(DPLL_A);
475 hw->dpll_b = INREG(DPLL_B);
476 hw->fpa0 = INREG(FPA0);
477 hw->fpa1 = INREG(FPA1);
478 hw->fpb0 = INREG(FPB0);
479 hw->fpb1 = INREG(FPB1);
480
481 if (flag == 1)
482 return flag;
483
484#if 0
485 /* This seems to be a problem with the 852GM/855GM */
486 for (i = 0; i < PALETTE_8_ENTRIES; i++) {
487 hw->palette_a[i] = INREG(PALETTE_A + (i << 2));
488 hw->palette_b[i] = INREG(PALETTE_B + (i << 2));
489 }
490#endif
491
492 if (flag == 2)
493 return flag;
494
495 hw->htotal_a = INREG(HTOTAL_A);
496 hw->hblank_a = INREG(HBLANK_A);
497 hw->hsync_a = INREG(HSYNC_A);
498 hw->vtotal_a = INREG(VTOTAL_A);
499 hw->vblank_a = INREG(VBLANK_A);
500 hw->vsync_a = INREG(VSYNC_A);
501 hw->src_size_a = INREG(SRC_SIZE_A);
502 hw->bclrpat_a = INREG(BCLRPAT_A);
503 hw->htotal_b = INREG(HTOTAL_B);
504 hw->hblank_b = INREG(HBLANK_B);
505 hw->hsync_b = INREG(HSYNC_B);
506 hw->vtotal_b = INREG(VTOTAL_B);
507 hw->vblank_b = INREG(VBLANK_B);
508 hw->vsync_b = INREG(VSYNC_B);
509 hw->src_size_b = INREG(SRC_SIZE_B);
510 hw->bclrpat_b = INREG(BCLRPAT_B);
511
512 if (flag == 3)
513 return flag;
514
515 hw->adpa = INREG(ADPA);
516 hw->dvoa = INREG(DVOA);
517 hw->dvob = INREG(DVOB);
518 hw->dvoc = INREG(DVOC);
519 hw->dvoa_srcdim = INREG(DVOA_SRCDIM);
520 hw->dvob_srcdim = INREG(DVOB_SRCDIM);
521 hw->dvoc_srcdim = INREG(DVOC_SRCDIM);
522 hw->lvds = INREG(LVDS);
523
524 if (flag == 4)
525 return flag;
526
527 hw->pipe_a_conf = INREG(PIPEACONF);
528 hw->pipe_b_conf = INREG(PIPEBCONF);
529 hw->disp_arb = INREG(DISPARB);
530
531 if (flag == 5)
532 return flag;
533
534 hw->cursor_a_control = INREG(CURSOR_A_CONTROL);
535 hw->cursor_b_control = INREG(CURSOR_B_CONTROL);
536 hw->cursor_a_base = INREG(CURSOR_A_BASEADDR);
537 hw->cursor_b_base = INREG(CURSOR_B_BASEADDR);
538
539 if (flag == 6)
540 return flag;
541
542 for (i = 0; i < 4; i++) {
543 hw->cursor_a_palette[i] = INREG(CURSOR_A_PALETTE0 + (i << 2));
544 hw->cursor_b_palette[i] = INREG(CURSOR_B_PALETTE0 + (i << 2));
545 }
546
547 if (flag == 7)
548 return flag;
549
550 hw->cursor_size = INREG(CURSOR_SIZE);
551
552 if (flag == 8)
553 return flag;
554
555 hw->disp_a_ctrl = INREG(DSPACNTR);
556 hw->disp_b_ctrl = INREG(DSPBCNTR);
557 hw->disp_a_base = INREG(DSPABASE);
558 hw->disp_b_base = INREG(DSPBBASE);
559 hw->disp_a_stride = INREG(DSPASTRIDE);
560 hw->disp_b_stride = INREG(DSPBSTRIDE);
561
562 if (flag == 9)
563 return flag;
564
565 hw->vgacntrl = INREG(VGACNTRL);
566
567 if (flag == 10)
568 return flag;
569
570 hw->add_id = INREG(ADD_ID);
571
572 if (flag == 11)
573 return flag;
574
575 for (i = 0; i < 7; i++) {
576 hw->swf0x[i] = INREG(SWF00 + (i << 2));
577 hw->swf1x[i] = INREG(SWF10 + (i << 2));
578 if (i < 3)
579 hw->swf3x[i] = INREG(SWF30 + (i << 2));
580 }
581
582 for (i = 0; i < 8; i++)
583 hw->fence[i] = INREG(FENCE + (i << 2));
584
585 hw->instpm = INREG(INSTPM);
586 hw->mem_mode = INREG(MEM_MODE);
587 hw->fw_blc_0 = INREG(FW_BLC_0);
588 hw->fw_blc_1 = INREG(FW_BLC_1);
589
590 return 0;
591}
592
593
Dave Airlied0249602006-03-20 20:26:45 +1100594static int calc_vclock3(int index, int m, int n, int p)
595{
Dave Airlie7679f4d2006-03-23 12:30:05 +1100596 if (p == 0 || n == 0)
597 return 0;
Dave Airlie3aff13c2006-03-31 17:08:52 +1000598 return plls[index].ref_clk * m / n / p;
Dave Airlied0249602006-03-20 20:26:45 +1100599}
Dave Airlie8b91b0b2006-03-23 19:23:48 +1100600
Dave Airlie3aff13c2006-03-31 17:08:52 +1000601static int calc_vclock(int index, int m1, int m2, int n, int p1, int p2, int lvds)
Dave Airlied0249602006-03-20 20:26:45 +1100602{
Dave Airliec9daa872006-05-27 18:44:02 +1000603 struct pll_min_max *pll = &plls[index];
604 u32 m, vco, p;
605
606 m = (5 * (m1 + 2)) + (m2 + 2);
607 n += 2;
608 vco = pll->ref_clk * m / n;
609
610 if (index == PLLS_I8xx) {
611 p = ((p1 + 2) * (1 << (p2 + 1)));
612 } else {
613 p = ((p1) * (p2 ? 5 : 10));
Dave Airlied0249602006-03-20 20:26:45 +1100614 }
Dave Airliec9daa872006-05-27 18:44:02 +1000615 return vco / p;
Dave Airlied0249602006-03-20 20:26:45 +1100616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618void
619intelfbhw_print_hw_state(struct intelfb_info *dinfo, struct intelfb_hwstate *hw)
620{
621#if REGDUMP
622 int i, m1, m2, n, p1, p2;
Dave Airlied0249602006-03-20 20:26:45 +1100623 int index = dinfo->pll_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 DBG_MSG("intelfbhw_print_hw_state\n");
Dave Airlie3aff13c2006-03-31 17:08:52 +1000625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (!hw || !dinfo)
627 return;
628 /* Read in as much of the HW state as possible. */
629 printk("hw state dump start\n");
630 printk(" VGA0_DIVISOR: 0x%08x\n", hw->vga0_divisor);
631 printk(" VGA1_DIVISOR: 0x%08x\n", hw->vga1_divisor);
632 printk(" VGAPD: 0x%08x\n", hw->vga_pd);
633 n = (hw->vga0_divisor >> FP_N_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
634 m1 = (hw->vga0_divisor >> FP_M1_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
635 m2 = (hw->vga0_divisor >> FP_M2_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
636 if (hw->vga_pd & VGAPD_0_P1_FORCE_DIV2)
637 p1 = 0;
638 else
639 p1 = (hw->vga_pd >> VGAPD_0_P1_SHIFT) & DPLL_P1_MASK;
Dave Airlie3aff13c2006-03-31 17:08:52 +1000640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 p2 = (hw->vga_pd >> VGAPD_0_P2_SHIFT) & DPLL_P2_MASK;
Dave Airlie3aff13c2006-03-31 17:08:52 +1000642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 printk(" VGA0: (m1, m2, n, p1, p2) = (%d, %d, %d, %d, %d)\n",
Dave Airlied0249602006-03-20 20:26:45 +1100644 m1, m2, n, p1, p2);
Dave Airlie8b91b0b2006-03-23 19:23:48 +1100645 printk(" VGA0: clock is %d\n",
Dave Airlie3aff13c2006-03-31 17:08:52 +1000646 calc_vclock(index, m1, m2, n, p1, p2, 0));
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 n = (hw->vga1_divisor >> FP_N_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
649 m1 = (hw->vga1_divisor >> FP_M1_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
650 m2 = (hw->vga1_divisor >> FP_M2_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
651 if (hw->vga_pd & VGAPD_1_P1_FORCE_DIV2)
652 p1 = 0;
653 else
654 p1 = (hw->vga_pd >> VGAPD_1_P1_SHIFT) & DPLL_P1_MASK;
655 p2 = (hw->vga_pd >> VGAPD_1_P2_SHIFT) & DPLL_P2_MASK;
656 printk(" VGA1: (m1, m2, n, p1, p2) = (%d, %d, %d, %d, %d)\n",
Dave Airlied0249602006-03-20 20:26:45 +1100657 m1, m2, n, p1, p2);
Dave Airlie3aff13c2006-03-31 17:08:52 +1000658 printk(" VGA1: clock is %d\n", calc_vclock(index, m1, m2, n, p1, p2, 0));
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 printk(" DPLL_A: 0x%08x\n", hw->dpll_a);
661 printk(" DPLL_B: 0x%08x\n", hw->dpll_b);
662 printk(" FPA0: 0x%08x\n", hw->fpa0);
663 printk(" FPA1: 0x%08x\n", hw->fpa1);
664 printk(" FPB0: 0x%08x\n", hw->fpb0);
665 printk(" FPB1: 0x%08x\n", hw->fpb1);
Dave Airlie3aff13c2006-03-31 17:08:52 +1000666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 n = (hw->fpa0 >> FP_N_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
668 m1 = (hw->fpa0 >> FP_M1_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
669 m2 = (hw->fpa0 >> FP_M2_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
Dave Airlie3aff13c2006-03-31 17:08:52 +1000670
671 if (IS_I9XX(dinfo)) {
672 int tmpp1;
673
674 if (hw->dpll_a & DPLL_P1_FORCE_DIV2)
675 p1 = 0;
676 else
677 p1 = (hw->dpll_a >> DPLL_P1_SHIFT) & 0xff;
678
679 tmpp1 = p1;
680
681 switch (tmpp1)
682 {
683 case 0x1: p1 = 1; break;
684 case 0x2: p1 = 2; break;
685 case 0x4: p1 = 3; break;
686 case 0x8: p1 = 4; break;
687 case 0x10: p1 = 5; break;
688 case 0x20: p1 = 6; break;
689 case 0x40: p1 = 7; break;
690 case 0x80: p1 = 8; break;
691 default: break;
692 }
693
694 p2 = (hw->dpll_a >> DPLL_I9XX_P2_SHIFT) & DPLL_P2_MASK;
695
696 } else {
697 if (hw->dpll_a & DPLL_P1_FORCE_DIV2)
698 p1 = 0;
699 else
700 p1 = (hw->dpll_a >> DPLL_P1_SHIFT) & DPLL_P1_MASK;
701 p2 = (hw->dpll_a >> DPLL_P2_SHIFT) & DPLL_P2_MASK;
702 }
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 printk(" PLLA0: (m1, m2, n, p1, p2) = (%d, %d, %d, %d, %d)\n",
Dave Airlied0249602006-03-20 20:26:45 +1100705 m1, m2, n, p1, p2);
Dave Airlie3aff13c2006-03-31 17:08:52 +1000706 printk(" PLLA0: clock is %d\n", calc_vclock(index, m1, m2, n, p1, p2, 0));
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 n = (hw->fpa1 >> FP_N_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
709 m1 = (hw->fpa1 >> FP_M1_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
710 m2 = (hw->fpa1 >> FP_M2_DIVISOR_SHIFT) & FP_DIVISOR_MASK;
Dave Airlie3aff13c2006-03-31 17:08:52 +1000711
712 if (IS_I9XX(dinfo)) {
713 int tmpp1;
714
715 if (hw->dpll_a & DPLL_P1_FORCE_DIV2)
716 p1 = 0;
717 else
718 p1 = (hw->dpll_a >> DPLL_P1_SHIFT) & 0xff;
719
720 tmpp1 = p1;
721
Dave Airlie51d79742006-04-03 16:19:26 +1000722 switch (tmpp1) {
Dave Airlie3aff13c2006-03-31 17:08:52 +1000723 case 0x1: p1 = 1; break;
724 case 0x2: p1 = 2; break;
725 case 0x4: p1 = 3; break;
726 case 0x8: p1 = 4; break;
727 case 0x10: p1 = 5; break;
728 case 0x20: p1 = 6; break;
729 case 0x40: p1 = 7; break;
730 case 0x80: p1 = 8; break;
731 default: break;
732 }
Dave Airlie51d79742006-04-03 16:19:26 +1000733
Dave Airlie3aff13c2006-03-31 17:08:52 +1000734 p2 = (hw->dpll_a >> DPLL_I9XX_P2_SHIFT) & DPLL_P2_MASK;
735
736 } else {
737 if (hw->dpll_a & DPLL_P1_FORCE_DIV2)
738 p1 = 0;
739 else
740 p1 = (hw->dpll_a >> DPLL_P1_SHIFT) & DPLL_P1_MASK;
741 p2 = (hw->dpll_a >> DPLL_P2_SHIFT) & DPLL_P2_MASK;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 printk(" PLLA1: (m1, m2, n, p1, p2) = (%d, %d, %d, %d, %d)\n",
Dave Airlied0249602006-03-20 20:26:45 +1100744 m1, m2, n, p1, p2);
Dave Airlie3aff13c2006-03-31 17:08:52 +1000745 printk(" PLLA1: clock is %d\n", calc_vclock(index, m1, m2, n, p1, p2, 0));
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747#if 0
748 printk(" PALETTE_A:\n");
749 for (i = 0; i < PALETTE_8_ENTRIES)
Dave Airlied0249602006-03-20 20:26:45 +1100750 printk(" %3d: 0x%08x\n", i, hw->palette_a[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 printk(" PALETTE_B:\n");
752 for (i = 0; i < PALETTE_8_ENTRIES)
Dave Airlied0249602006-03-20 20:26:45 +1100753 printk(" %3d: 0x%08x\n", i, hw->palette_b[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#endif
755
756 printk(" HTOTAL_A: 0x%08x\n", hw->htotal_a);
757 printk(" HBLANK_A: 0x%08x\n", hw->hblank_a);
758 printk(" HSYNC_A: 0x%08x\n", hw->hsync_a);
759 printk(" VTOTAL_A: 0x%08x\n", hw->vtotal_a);
760 printk(" VBLANK_A: 0x%08x\n", hw->vblank_a);
761 printk(" VSYNC_A: 0x%08x\n", hw->vsync_a);
762 printk(" SRC_SIZE_A: 0x%08x\n", hw->src_size_a);
763 printk(" BCLRPAT_A: 0x%08x\n", hw->bclrpat_a);
764 printk(" HTOTAL_B: 0x%08x\n", hw->htotal_b);
765 printk(" HBLANK_B: 0x%08x\n", hw->hblank_b);
766 printk(" HSYNC_B: 0x%08x\n", hw->hsync_b);
767 printk(" VTOTAL_B: 0x%08x\n", hw->vtotal_b);
768 printk(" VBLANK_B: 0x%08x\n", hw->vblank_b);
769 printk(" VSYNC_B: 0x%08x\n", hw->vsync_b);
770 printk(" SRC_SIZE_B: 0x%08x\n", hw->src_size_b);
771 printk(" BCLRPAT_B: 0x%08x\n", hw->bclrpat_b);
772
773 printk(" ADPA: 0x%08x\n", hw->adpa);
774 printk(" DVOA: 0x%08x\n", hw->dvoa);
775 printk(" DVOB: 0x%08x\n", hw->dvob);
776 printk(" DVOC: 0x%08x\n", hw->dvoc);
777 printk(" DVOA_SRCDIM: 0x%08x\n", hw->dvoa_srcdim);
778 printk(" DVOB_SRCDIM: 0x%08x\n", hw->dvob_srcdim);
779 printk(" DVOC_SRCDIM: 0x%08x\n", hw->dvoc_srcdim);
780 printk(" LVDS: 0x%08x\n", hw->lvds);
781
782 printk(" PIPEACONF: 0x%08x\n", hw->pipe_a_conf);
783 printk(" PIPEBCONF: 0x%08x\n", hw->pipe_b_conf);
784 printk(" DISPARB: 0x%08x\n", hw->disp_arb);
785
786 printk(" CURSOR_A_CONTROL: 0x%08x\n", hw->cursor_a_control);
787 printk(" CURSOR_B_CONTROL: 0x%08x\n", hw->cursor_b_control);
788 printk(" CURSOR_A_BASEADDR: 0x%08x\n", hw->cursor_a_base);
789 printk(" CURSOR_B_BASEADDR: 0x%08x\n", hw->cursor_b_base);
790
791 printk(" CURSOR_A_PALETTE: ");
792 for (i = 0; i < 4; i++) {
793 printk("0x%08x", hw->cursor_a_palette[i]);
794 if (i < 3)
795 printk(", ");
796 }
797 printk("\n");
798 printk(" CURSOR_B_PALETTE: ");
799 for (i = 0; i < 4; i++) {
800 printk("0x%08x", hw->cursor_b_palette[i]);
801 if (i < 3)
802 printk(", ");
803 }
804 printk("\n");
805
806 printk(" CURSOR_SIZE: 0x%08x\n", hw->cursor_size);
807
808 printk(" DSPACNTR: 0x%08x\n", hw->disp_a_ctrl);
809 printk(" DSPBCNTR: 0x%08x\n", hw->disp_b_ctrl);
810 printk(" DSPABASE: 0x%08x\n", hw->disp_a_base);
811 printk(" DSPBBASE: 0x%08x\n", hw->disp_b_base);
812 printk(" DSPASTRIDE: 0x%08x\n", hw->disp_a_stride);
813 printk(" DSPBSTRIDE: 0x%08x\n", hw->disp_b_stride);
814
815 printk(" VGACNTRL: 0x%08x\n", hw->vgacntrl);
816 printk(" ADD_ID: 0x%08x\n", hw->add_id);
817
818 for (i = 0; i < 7; i++) {
819 printk(" SWF0%d 0x%08x\n", i,
820 hw->swf0x[i]);
821 }
822 for (i = 0; i < 7; i++) {
823 printk(" SWF1%d 0x%08x\n", i,
824 hw->swf1x[i]);
825 }
826 for (i = 0; i < 3; i++) {
827 printk(" SWF3%d 0x%08x\n", i,
Dave Airlied0249602006-03-20 20:26:45 +1100828 hw->swf3x[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 for (i = 0; i < 8; i++)
831 printk(" FENCE%d 0x%08x\n", i,
Dave Airlied0249602006-03-20 20:26:45 +1100832 hw->fence[i]);
Dave Airlie8b91b0b2006-03-23 19:23:48 +1100833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 printk(" INSTPM 0x%08x\n", hw->instpm);
835 printk(" MEM_MODE 0x%08x\n", hw->mem_mode);
836 printk(" FW_BLC_0 0x%08x\n", hw->fw_blc_0);
837 printk(" FW_BLC_1 0x%08x\n", hw->fw_blc_1);
838
839 printk("hw state dump end\n");
840#endif
841}
842
Dave Airlie8b91b0b2006-03-23 19:23:48 +1100843
Dave Airlied0249602006-03-20 20:26:45 +1100844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845/* Split the M parameter into M1 and M2. */
846static int
Dave Airlie7258b112006-03-20 20:02:24 +1100847splitm(int index, unsigned int m, unsigned int *retm1, unsigned int *retm2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 int m1, m2;
Dave Airlie8492f082006-03-20 20:54:12 +1100850 int testm;
Dave Airliec9daa872006-05-27 18:44:02 +1000851 struct pll_min_max *pll = &plls[index];
852
Dave Airlie8492f082006-03-20 20:54:12 +1100853 /* no point optimising too much - brute force m */
Dave Airliec9daa872006-05-27 18:44:02 +1000854 for (m1 = pll->min_m1; m1 < pll->max_m1 + 1; m1++) {
855 for (m2 = pll->min_m2; m2 < pll->max_m2 + 1; m2++) {
Dave Airlie3aff13c2006-03-31 17:08:52 +1000856 testm = (5 * (m1 + 2)) + (m2 + 2);
Dave Airlie8b91b0b2006-03-23 19:23:48 +1100857 if (testm == m) {
858 *retm1 = (unsigned int)m1;
859 *retm2 = (unsigned int)m2;
860 return 0;
861 }
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Dave Airlie8492f082006-03-20 20:54:12 +1100864 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
867/* Split the P parameter into P1 and P2. */
868static int
Dave Airlie7258b112006-03-20 20:02:24 +1100869splitp(int index, unsigned int p, unsigned int *retp1, unsigned int *retp2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 int p1, p2;
Dave Airliec9daa872006-05-27 18:44:02 +1000872 struct pll_min_max *pll = &plls[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Dave Airlie8b91b0b2006-03-23 19:23:48 +1100874 if (index == PLLS_I9xx) {
Dave Airlie51d79742006-04-03 16:19:26 +1000875 p2 = (p % 10) ? 1 : 0;
Dave Airlie3aff13c2006-03-31 17:08:52 +1000876
877 p1 = p / (p2 ? 5 : 10);
878
Dave Airlied0249602006-03-20 20:26:45 +1100879 *retp1 = (unsigned int)p1;
880 *retp2 = (unsigned int)p2;
881 return 0;
882 }
883
Dave Airliec9daa872006-05-27 18:44:02 +1000884 if (p % 4 == 0)
885 p2 = 1;
886 else
887 p2 = 0;
888 p1 = (p / (1 << (p2 + 1))) - 2;
889 if (p % 4 == 0 && p1 < pll->min_p1) {
890 p2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 p1 = (p / (1 << (p2 + 1))) - 2;
892 }
Dave Airliec9daa872006-05-27 18:44:02 +1000893 if (p1 < pll->min_p1 || p1 > pll->max_p1 ||
894 (p1 + 2) * (1 << (p2 + 1)) != p) {
895 return 1;
896 } else {
897 *retp1 = (unsigned int)p1;
898 *retp2 = (unsigned int)p2;
899 return 0;
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903static int
Dave Airlie7258b112006-03-20 20:02:24 +1100904calc_pll_params(int index, int clock, u32 *retm1, u32 *retm2, u32 *retn, u32 *retp1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 u32 *retp2, u32 *retclock)
906{
Dave Airlie7679f4d2006-03-23 12:30:05 +1100907 u32 m1, m2, n, p1, p2, n1, testm;
908 u32 f_vco, p, p_best = 0, m, f_out = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 u32 err_max, err_target, err_best = 10000000;
910 u32 n_best = 0, m_best = 0, f_best, f_err;
Dave Airlie51d79742006-04-03 16:19:26 +1000911 u32 p_min, p_max, p_inc, div_max;
912 struct pll_min_max *pll = &plls[index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* Accept 0.5% difference, but aim for 0.1% */
915 err_max = 5 * clock / 1000;
916 err_target = clock / 1000;
917
918 DBG_MSG("Clock is %d\n", clock);
919
Dave Airlie51d79742006-04-03 16:19:26 +1000920 div_max = pll->max_vco / clock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Dave Airlie51d79742006-04-03 16:19:26 +1000922 p_inc = (clock <= pll->p_transition_clk) ? pll->p_inc_lo : pll->p_inc_hi;
923 p_min = p_inc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 p_max = ROUND_DOWN_TO(div_max, p_inc);
Dave Airlie51d79742006-04-03 16:19:26 +1000925 if (p_min < pll->min_p)
926 p_min = pll->min_p;
927 if (p_max > pll->max_p)
928 p_max = pll->max_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 DBG_MSG("p range is %d-%d (%d)\n", p_min, p_max, p_inc);
931
932 p = p_min;
933 do {
Dave Airlie7258b112006-03-20 20:02:24 +1100934 if (splitp(index, p, &p1, &p2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 WRN_MSG("cannot split p = %d\n", p);
936 p += p_inc;
937 continue;
938 }
Dave Airlie51d79742006-04-03 16:19:26 +1000939 n = pll->min_n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 f_vco = clock * p;
941
942 do {
Dave Airlie51d79742006-04-03 16:19:26 +1000943 m = ROUND_UP_TO(f_vco * n, pll->ref_clk) / pll->ref_clk;
944 if (m < pll->min_m)
945 m = pll->min_m + 1;
946 if (m > pll->max_m)
947 m = pll->max_m - 1;
Dave Airlie7679f4d2006-03-23 12:30:05 +1100948 for (testm = m - 1; testm <= m; testm++) {
949 f_out = calc_vclock3(index, m, n, p);
Dave Airliec9daa872006-05-27 18:44:02 +1000950 if (splitm(index, testm, &m1, &m2)) {
Dave Airlie7679f4d2006-03-23 12:30:05 +1100951 WRN_MSG("cannot split m = %d\n", m);
952 n++;
953 continue;
954 }
955 if (clock > f_out)
956 f_err = clock - f_out;
957 else/* slightly bias the error for bigger clocks */
958 f_err = f_out - clock + 1;
Dave Airlie3aff13c2006-03-31 17:08:52 +1000959
Dave Airlie7679f4d2006-03-23 12:30:05 +1100960 if (f_err < err_best) {
Dave Airliec9daa872006-05-27 18:44:02 +1000961 m_best = testm;
Dave Airlie7679f4d2006-03-23 12:30:05 +1100962 n_best = n;
963 p_best = p;
964 f_best = f_out;
965 err_best = f_err;
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968 n++;
Dave Airlie51d79742006-04-03 16:19:26 +1000969 } while ((n <= pll->max_n) && (f_out >= clock));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 p += p_inc;
971 } while ((p <= p_max));
972
973 if (!m_best) {
974 WRN_MSG("cannot find parameters for clock %d\n", clock);
975 return 1;
976 }
977 m = m_best;
978 n = n_best;
979 p = p_best;
Dave Airlie7258b112006-03-20 20:02:24 +1100980 splitm(index, m, &m1, &m2);
981 splitp(index, p, &p1, &p2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 n1 = n - 2;
983
984 DBG_MSG("m, n, p: %d (%d,%d), %d (%d), %d (%d,%d), "
985 "f: %d (%d), VCO: %d\n",
986 m, m1, m2, n, n1, p, p1, p2,
Dave Airlie8b91b0b2006-03-23 19:23:48 +1100987 calc_vclock3(index, m, n, p),
Dave Airlie3aff13c2006-03-31 17:08:52 +1000988 calc_vclock(index, m1, m2, n1, p1, p2, 0),
Dave Airlied0249602006-03-20 20:26:45 +1100989 calc_vclock3(index, m, n, p) * p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 *retm1 = m1;
991 *retm2 = m2;
992 *retn = n1;
993 *retp1 = p1;
994 *retp2 = p2;
Dave Airlie3aff13c2006-03-31 17:08:52 +1000995 *retclock = calc_vclock(index, m1, m2, n1, p1, p2, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 return 0;
998}
999
1000static __inline__ int
1001check_overflow(u32 value, u32 limit, const char *description)
1002{
1003 if (value > limit) {
1004 WRN_MSG("%s value %d exceeds limit %d\n",
1005 description, value, limit);
1006 return 1;
1007 }
1008 return 0;
1009}
1010
1011/* It is assumed that hw is filled in with the initial state information. */
1012int
1013intelfbhw_mode_to_hw(struct intelfb_info *dinfo, struct intelfb_hwstate *hw,
1014 struct fb_var_screeninfo *var)
1015{
1016 int pipe = PIPE_A;
1017 u32 *dpll, *fp0, *fp1;
1018 u32 m1, m2, n, p1, p2, clock_target, clock;
1019 u32 hsync_start, hsync_end, hblank_start, hblank_end, htotal, hactive;
1020 u32 vsync_start, vsync_end, vblank_start, vblank_end, vtotal, vactive;
1021 u32 vsync_pol, hsync_pol;
1022 u32 *vs, *vb, *vt, *hs, *hb, *ht, *ss, *pipe_conf;
Dennis Munsiedf7df8a2006-05-27 18:17:52 +10001023 u32 stride_alignment;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 DBG_MSG("intelfbhw_mode_to_hw\n");
1026
1027 /* Disable VGA */
1028 hw->vgacntrl |= VGA_DISABLE;
1029
1030 /* Check whether pipe A or pipe B is enabled. */
1031 if (hw->pipe_a_conf & PIPECONF_ENABLE)
1032 pipe = PIPE_A;
1033 else if (hw->pipe_b_conf & PIPECONF_ENABLE)
1034 pipe = PIPE_B;
1035
1036 /* Set which pipe's registers will be set. */
1037 if (pipe == PIPE_B) {
1038 dpll = &hw->dpll_b;
1039 fp0 = &hw->fpb0;
1040 fp1 = &hw->fpb1;
1041 hs = &hw->hsync_b;
1042 hb = &hw->hblank_b;
1043 ht = &hw->htotal_b;
1044 vs = &hw->vsync_b;
1045 vb = &hw->vblank_b;
1046 vt = &hw->vtotal_b;
1047 ss = &hw->src_size_b;
1048 pipe_conf = &hw->pipe_b_conf;
1049 } else {
1050 dpll = &hw->dpll_a;
1051 fp0 = &hw->fpa0;
1052 fp1 = &hw->fpa1;
1053 hs = &hw->hsync_a;
1054 hb = &hw->hblank_a;
1055 ht = &hw->htotal_a;
1056 vs = &hw->vsync_a;
1057 vb = &hw->vblank_a;
1058 vt = &hw->vtotal_a;
1059 ss = &hw->src_size_a;
1060 pipe_conf = &hw->pipe_a_conf;
1061 }
1062
1063 /* Use ADPA register for sync control. */
1064 hw->adpa &= ~ADPA_USE_VGA_HVPOLARITY;
1065
1066 /* sync polarity */
1067 hsync_pol = (var->sync & FB_SYNC_HOR_HIGH_ACT) ?
1068 ADPA_SYNC_ACTIVE_HIGH : ADPA_SYNC_ACTIVE_LOW;
1069 vsync_pol = (var->sync & FB_SYNC_VERT_HIGH_ACT) ?
1070 ADPA_SYNC_ACTIVE_HIGH : ADPA_SYNC_ACTIVE_LOW;
1071 hw->adpa &= ~((ADPA_SYNC_ACTIVE_MASK << ADPA_VSYNC_ACTIVE_SHIFT) |
1072 (ADPA_SYNC_ACTIVE_MASK << ADPA_HSYNC_ACTIVE_SHIFT));
1073 hw->adpa |= (hsync_pol << ADPA_HSYNC_ACTIVE_SHIFT) |
1074 (vsync_pol << ADPA_VSYNC_ACTIVE_SHIFT);
1075
1076 /* Connect correct pipe to the analog port DAC */
1077 hw->adpa &= ~(PIPE_MASK << ADPA_PIPE_SELECT_SHIFT);
1078 hw->adpa |= (pipe << ADPA_PIPE_SELECT_SHIFT);
1079
1080 /* Set DPMS state to D0 (on) */
1081 hw->adpa &= ~ADPA_DPMS_CONTROL_MASK;
1082 hw->adpa |= ADPA_DPMS_D0;
1083
1084 hw->adpa |= ADPA_DAC_ENABLE;
1085
1086 *dpll |= (DPLL_VCO_ENABLE | DPLL_VGA_MODE_DISABLE);
1087 *dpll &= ~(DPLL_RATE_SELECT_MASK | DPLL_REFERENCE_SELECT_MASK);
1088 *dpll |= (DPLL_REFERENCE_DEFAULT | DPLL_RATE_SELECT_FP0);
1089
1090 /* Desired clock in kHz */
1091 clock_target = 1000000000 / var->pixclock;
1092
Dave Airlie3aff13c2006-03-31 17:08:52 +10001093 if (calc_pll_params(dinfo->pll_index, clock_target, &m1, &m2,
Dave Airlie8b91b0b2006-03-23 19:23:48 +11001094 &n, &p1, &p2, &clock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 WRN_MSG("calc_pll_params failed\n");
1096 return 1;
1097 }
1098
1099 /* Check for overflow. */
1100 if (check_overflow(p1, DPLL_P1_MASK, "PLL P1 parameter"))
1101 return 1;
1102 if (check_overflow(p2, DPLL_P2_MASK, "PLL P2 parameter"))
1103 return 1;
1104 if (check_overflow(m1, FP_DIVISOR_MASK, "PLL M1 parameter"))
1105 return 1;
1106 if (check_overflow(m2, FP_DIVISOR_MASK, "PLL M2 parameter"))
1107 return 1;
1108 if (check_overflow(n, FP_DIVISOR_MASK, "PLL N parameter"))
1109 return 1;
1110
1111 *dpll &= ~DPLL_P1_FORCE_DIV2;
1112 *dpll &= ~((DPLL_P2_MASK << DPLL_P2_SHIFT) |
1113 (DPLL_P1_MASK << DPLL_P1_SHIFT));
Dave Airlie3aff13c2006-03-31 17:08:52 +10001114
1115 if (IS_I9XX(dinfo)) {
1116 *dpll |= (p2 << DPLL_I9XX_P2_SHIFT);
1117 *dpll |= (1 << (p1 - 1)) << DPLL_P1_SHIFT;
1118 } else {
1119 *dpll |= (p2 << DPLL_P2_SHIFT) | (p1 << DPLL_P1_SHIFT);
1120 }
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 *fp0 = (n << FP_N_DIVISOR_SHIFT) |
1123 (m1 << FP_M1_DIVISOR_SHIFT) |
1124 (m2 << FP_M2_DIVISOR_SHIFT);
1125 *fp1 = *fp0;
1126
1127 hw->dvob &= ~PORT_ENABLE;
1128 hw->dvoc &= ~PORT_ENABLE;
1129
1130 /* Use display plane A. */
1131 hw->disp_a_ctrl |= DISPPLANE_PLANE_ENABLE;
1132 hw->disp_a_ctrl &= ~DISPPLANE_GAMMA_ENABLE;
1133 hw->disp_a_ctrl &= ~DISPPLANE_PIXFORMAT_MASK;
1134 switch (intelfb_var_to_depth(var)) {
1135 case 8:
1136 hw->disp_a_ctrl |= DISPPLANE_8BPP | DISPPLANE_GAMMA_ENABLE;
1137 break;
1138 case 15:
1139 hw->disp_a_ctrl |= DISPPLANE_15_16BPP;
1140 break;
1141 case 16:
1142 hw->disp_a_ctrl |= DISPPLANE_16BPP;
1143 break;
1144 case 24:
1145 hw->disp_a_ctrl |= DISPPLANE_32BPP_NO_ALPHA;
1146 break;
1147 }
1148 hw->disp_a_ctrl &= ~(PIPE_MASK << DISPPLANE_SEL_PIPE_SHIFT);
1149 hw->disp_a_ctrl |= (pipe << DISPPLANE_SEL_PIPE_SHIFT);
1150
1151 /* Set CRTC registers. */
1152 hactive = var->xres;
1153 hsync_start = hactive + var->right_margin;
1154 hsync_end = hsync_start + var->hsync_len;
1155 htotal = hsync_end + var->left_margin;
1156 hblank_start = hactive;
1157 hblank_end = htotal;
1158
1159 DBG_MSG("H: act %d, ss %d, se %d, tot %d bs %d, be %d\n",
1160 hactive, hsync_start, hsync_end, htotal, hblank_start,
1161 hblank_end);
1162
1163 vactive = var->yres;
1164 vsync_start = vactive + var->lower_margin;
1165 vsync_end = vsync_start + var->vsync_len;
1166 vtotal = vsync_end + var->upper_margin;
1167 vblank_start = vactive;
1168 vblank_end = vtotal;
1169 vblank_end = vsync_end + 1;
1170
1171 DBG_MSG("V: act %d, ss %d, se %d, tot %d bs %d, be %d\n",
1172 vactive, vsync_start, vsync_end, vtotal, vblank_start,
1173 vblank_end);
1174
1175 /* Adjust for register values, and check for overflow. */
1176 hactive--;
1177 if (check_overflow(hactive, HACTIVE_MASK, "CRTC hactive"))
1178 return 1;
1179 hsync_start--;
1180 if (check_overflow(hsync_start, HSYNCSTART_MASK, "CRTC hsync_start"))
1181 return 1;
1182 hsync_end--;
1183 if (check_overflow(hsync_end, HSYNCEND_MASK, "CRTC hsync_end"))
1184 return 1;
1185 htotal--;
1186 if (check_overflow(htotal, HTOTAL_MASK, "CRTC htotal"))
1187 return 1;
1188 hblank_start--;
1189 if (check_overflow(hblank_start, HBLANKSTART_MASK, "CRTC hblank_start"))
1190 return 1;
1191 hblank_end--;
1192 if (check_overflow(hblank_end, HBLANKEND_MASK, "CRTC hblank_end"))
1193 return 1;
1194
1195 vactive--;
1196 if (check_overflow(vactive, VACTIVE_MASK, "CRTC vactive"))
1197 return 1;
1198 vsync_start--;
1199 if (check_overflow(vsync_start, VSYNCSTART_MASK, "CRTC vsync_start"))
1200 return 1;
1201 vsync_end--;
1202 if (check_overflow(vsync_end, VSYNCEND_MASK, "CRTC vsync_end"))
1203 return 1;
1204 vtotal--;
1205 if (check_overflow(vtotal, VTOTAL_MASK, "CRTC vtotal"))
1206 return 1;
1207 vblank_start--;
1208 if (check_overflow(vblank_start, VBLANKSTART_MASK, "CRTC vblank_start"))
1209 return 1;
1210 vblank_end--;
1211 if (check_overflow(vblank_end, VBLANKEND_MASK, "CRTC vblank_end"))
1212 return 1;
1213
1214 *ht = (htotal << HTOTAL_SHIFT) | (hactive << HACTIVE_SHIFT);
1215 *hb = (hblank_start << HBLANKSTART_SHIFT) |
1216 (hblank_end << HSYNCEND_SHIFT);
1217 *hs = (hsync_start << HSYNCSTART_SHIFT) | (hsync_end << HSYNCEND_SHIFT);
1218
1219 *vt = (vtotal << VTOTAL_SHIFT) | (vactive << VACTIVE_SHIFT);
1220 *vb = (vblank_start << VBLANKSTART_SHIFT) |
1221 (vblank_end << VSYNCEND_SHIFT);
1222 *vs = (vsync_start << VSYNCSTART_SHIFT) | (vsync_end << VSYNCEND_SHIFT);
1223 *ss = (hactive << SRC_SIZE_HORIZ_SHIFT) |
1224 (vactive << SRC_SIZE_VERT_SHIFT);
1225
Dave Airlie3587c502006-04-03 14:46:55 +10001226 hw->disp_a_stride = dinfo->pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 DBG_MSG("pitch is %d\n", hw->disp_a_stride);
1228
1229 hw->disp_a_base = hw->disp_a_stride * var->yoffset +
1230 var->xoffset * var->bits_per_pixel / 8;
1231
1232 hw->disp_a_base += dinfo->fb.offset << 12;
1233
1234 /* Check stride alignment. */
Dennis Munsiedf7df8a2006-05-27 18:17:52 +10001235 stride_alignment = IS_I9XX(dinfo) ? STRIDE_ALIGNMENT_I9XX :
1236 STRIDE_ALIGNMENT;
1237 if (hw->disp_a_stride % stride_alignment != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 WRN_MSG("display stride %d has bad alignment %d\n",
Dennis Munsiedf7df8a2006-05-27 18:17:52 +10001239 hw->disp_a_stride, stride_alignment);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return 1;
1241 }
1242
1243 /* Set the palette to 8-bit mode. */
1244 *pipe_conf &= ~PIPECONF_GAMMA;
1245 return 0;
1246}
1247
1248/* Program a (non-VGA) video mode. */
1249int
1250intelfbhw_program_mode(struct intelfb_info *dinfo,
1251 const struct intelfb_hwstate *hw, int blank)
1252{
1253 int pipe = PIPE_A;
1254 u32 tmp;
1255 const u32 *dpll, *fp0, *fp1, *pipe_conf;
1256 const u32 *hs, *ht, *hb, *vs, *vt, *vb, *ss;
1257 u32 dpll_reg, fp0_reg, fp1_reg, pipe_conf_reg;
1258 u32 hsync_reg, htotal_reg, hblank_reg;
1259 u32 vsync_reg, vtotal_reg, vblank_reg;
1260 u32 src_size_reg;
Dave Airlie7679f4d2006-03-23 12:30:05 +11001261 u32 count, tmp_val[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /* Assume single pipe, display plane A, analog CRT. */
1264
1265#if VERBOSE > 0
1266 DBG_MSG("intelfbhw_program_mode\n");
1267#endif
1268
1269 /* Disable VGA */
1270 tmp = INREG(VGACNTRL);
1271 tmp |= VGA_DISABLE;
1272 OUTREG(VGACNTRL, tmp);
1273
1274 /* Check whether pipe A or pipe B is enabled. */
1275 if (hw->pipe_a_conf & PIPECONF_ENABLE)
1276 pipe = PIPE_A;
1277 else if (hw->pipe_b_conf & PIPECONF_ENABLE)
1278 pipe = PIPE_B;
1279
1280 dinfo->pipe = pipe;
1281
1282 if (pipe == PIPE_B) {
1283 dpll = &hw->dpll_b;
1284 fp0 = &hw->fpb0;
1285 fp1 = &hw->fpb1;
1286 pipe_conf = &hw->pipe_b_conf;
1287 hs = &hw->hsync_b;
1288 hb = &hw->hblank_b;
1289 ht = &hw->htotal_b;
1290 vs = &hw->vsync_b;
1291 vb = &hw->vblank_b;
1292 vt = &hw->vtotal_b;
1293 ss = &hw->src_size_b;
1294 dpll_reg = DPLL_B;
1295 fp0_reg = FPB0;
1296 fp1_reg = FPB1;
1297 pipe_conf_reg = PIPEBCONF;
1298 hsync_reg = HSYNC_B;
1299 htotal_reg = HTOTAL_B;
1300 hblank_reg = HBLANK_B;
1301 vsync_reg = VSYNC_B;
1302 vtotal_reg = VTOTAL_B;
1303 vblank_reg = VBLANK_B;
1304 src_size_reg = SRC_SIZE_B;
1305 } else {
1306 dpll = &hw->dpll_a;
1307 fp0 = &hw->fpa0;
1308 fp1 = &hw->fpa1;
1309 pipe_conf = &hw->pipe_a_conf;
1310 hs = &hw->hsync_a;
1311 hb = &hw->hblank_a;
1312 ht = &hw->htotal_a;
1313 vs = &hw->vsync_a;
1314 vb = &hw->vblank_a;
1315 vt = &hw->vtotal_a;
1316 ss = &hw->src_size_a;
1317 dpll_reg = DPLL_A;
1318 fp0_reg = FPA0;
1319 fp1_reg = FPA1;
1320 pipe_conf_reg = PIPEACONF;
1321 hsync_reg = HSYNC_A;
1322 htotal_reg = HTOTAL_A;
1323 hblank_reg = HBLANK_A;
1324 vsync_reg = VSYNC_A;
1325 vtotal_reg = VTOTAL_A;
1326 vblank_reg = VBLANK_A;
1327 src_size_reg = SRC_SIZE_A;
1328 }
1329
Dave Airlie7679f4d2006-03-23 12:30:05 +11001330 /* turn off pipe */
1331 tmp = INREG(pipe_conf_reg);
1332 tmp &= ~PIPECONF_ENABLE;
1333 OUTREG(pipe_conf_reg, tmp);
Dave Airlie3aff13c2006-03-31 17:08:52 +10001334
Dave Airlie7679f4d2006-03-23 12:30:05 +11001335 count = 0;
Dave Airlie8b91b0b2006-03-23 19:23:48 +11001336 do {
Dave Airlie3aff13c2006-03-31 17:08:52 +10001337 tmp_val[count%3] = INREG(0x70000);
1338 if ((tmp_val[0] == tmp_val[1]) && (tmp_val[1]==tmp_val[2]))
1339 break;
1340 count++;
1341 udelay(1);
1342 if (count % 200 == 0) {
1343 tmp = INREG(pipe_conf_reg);
1344 tmp &= ~PIPECONF_ENABLE;
1345 OUTREG(pipe_conf_reg, tmp);
1346 }
Dave Airlie7679f4d2006-03-23 12:30:05 +11001347 } while(count < 2000);
1348
1349 OUTREG(ADPA, INREG(ADPA) & ~ADPA_DAC_ENABLE);
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /* Disable planes A and B. */
1352 tmp = INREG(DSPACNTR);
1353 tmp &= ~DISPPLANE_PLANE_ENABLE;
1354 OUTREG(DSPACNTR, tmp);
1355 tmp = INREG(DSPBCNTR);
1356 tmp &= ~DISPPLANE_PLANE_ENABLE;
1357 OUTREG(DSPBCNTR, tmp);
1358
Dave Airlie3aff13c2006-03-31 17:08:52 +10001359 /* Wait for vblank. For now, just wait for a 50Hz cycle (20ms)) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 mdelay(20);
1361
1362 /* Disable Sync */
1363 tmp = INREG(ADPA);
1364 tmp &= ~ADPA_DPMS_CONTROL_MASK;
1365 tmp |= ADPA_DPMS_D3;
1366 OUTREG(ADPA, tmp);
1367
Dave Airlie7679f4d2006-03-23 12:30:05 +11001368 /* do some funky magic - xyzzy */
1369 OUTREG(0x61204, 0xabcd0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /* turn off PLL */
1372 tmp = INREG(dpll_reg);
1373 dpll_reg &= ~DPLL_VCO_ENABLE;
1374 OUTREG(dpll_reg, tmp);
1375
1376 /* Set PLL parameters */
1377 OUTREG(dpll_reg, *dpll & ~DPLL_VCO_ENABLE);
1378 OUTREG(fp0_reg, *fp0);
1379 OUTREG(fp1_reg, *fp1);
1380
Dave Airlie7679f4d2006-03-23 12:30:05 +11001381 /* Enable PLL */
1382 tmp = INREG(dpll_reg);
1383 tmp |= DPLL_VCO_ENABLE;
1384 OUTREG(dpll_reg, tmp);
1385
1386 /* Set DVOs B/C */
1387 OUTREG(DVOB, hw->dvob);
1388 OUTREG(DVOC, hw->dvoc);
1389
1390 /* undo funky magic */
1391 OUTREG(0x61204, 0x00000000);
1392
1393 /* Set ADPA */
1394 OUTREG(ADPA, INREG(ADPA) | ADPA_DAC_ENABLE);
1395 OUTREG(ADPA, (hw->adpa & ~(ADPA_DPMS_CONTROL_MASK)) | ADPA_DPMS_D3);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 /* Set pipe parameters */
1398 OUTREG(hsync_reg, *hs);
1399 OUTREG(hblank_reg, *hb);
1400 OUTREG(htotal_reg, *ht);
1401 OUTREG(vsync_reg, *vs);
1402 OUTREG(vblank_reg, *vb);
1403 OUTREG(vtotal_reg, *vt);
1404 OUTREG(src_size_reg, *ss);
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 /* Enable pipe */
1407 OUTREG(pipe_conf_reg, *pipe_conf | PIPECONF_ENABLE);
1408
1409 /* Enable sync */
1410 tmp = INREG(ADPA);
1411 tmp &= ~ADPA_DPMS_CONTROL_MASK;
1412 tmp |= ADPA_DPMS_D0;
1413 OUTREG(ADPA, tmp);
1414
1415 /* setup display plane */
1416 if (dinfo->pdev->device == PCI_DEVICE_ID_INTEL_830M) {
1417 /*
1418 * i830M errata: the display plane must be enabled
1419 * to allow writes to the other bits in the plane
1420 * control register.
1421 */
1422 tmp = INREG(DSPACNTR);
1423 if ((tmp & DISPPLANE_PLANE_ENABLE) != DISPPLANE_PLANE_ENABLE) {
1424 tmp |= DISPPLANE_PLANE_ENABLE;
1425 OUTREG(DSPACNTR, tmp);
1426 OUTREG(DSPACNTR,
1427 hw->disp_a_ctrl|DISPPLANE_PLANE_ENABLE);
1428 mdelay(1);
Dave Airlie3aff13c2006-03-31 17:08:52 +10001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
1432 OUTREG(DSPACNTR, hw->disp_a_ctrl & ~DISPPLANE_PLANE_ENABLE);
1433 OUTREG(DSPASTRIDE, hw->disp_a_stride);
1434 OUTREG(DSPABASE, hw->disp_a_base);
1435
1436 /* Enable plane */
1437 if (!blank) {
1438 tmp = INREG(DSPACNTR);
1439 tmp |= DISPPLANE_PLANE_ENABLE;
1440 OUTREG(DSPACNTR, tmp);
1441 OUTREG(DSPABASE, hw->disp_a_base);
1442 }
1443
1444 return 0;
1445}
1446
1447/* forward declarations */
1448static void refresh_ring(struct intelfb_info *dinfo);
1449static void reset_state(struct intelfb_info *dinfo);
1450static void do_flush(struct intelfb_info *dinfo);
1451
1452static int
1453wait_ring(struct intelfb_info *dinfo, int n)
1454{
1455 int i = 0;
1456 unsigned long end;
1457 u32 last_head = INREG(PRI_RING_HEAD) & RING_HEAD_MASK;
1458
1459#if VERBOSE > 0
1460 DBG_MSG("wait_ring: %d\n", n);
1461#endif
1462
1463 end = jiffies + (HZ * 3);
1464 while (dinfo->ring_space < n) {
1465 dinfo->ring_head = (u8 __iomem *)(INREG(PRI_RING_HEAD) &
1466 RING_HEAD_MASK);
1467 if (dinfo->ring_tail + RING_MIN_FREE <
1468 (u32 __iomem) dinfo->ring_head)
1469 dinfo->ring_space = (u32 __iomem) dinfo->ring_head
1470 - (dinfo->ring_tail + RING_MIN_FREE);
1471 else
1472 dinfo->ring_space = (dinfo->ring.size +
1473 (u32 __iomem) dinfo->ring_head)
1474 - (dinfo->ring_tail + RING_MIN_FREE);
1475 if ((u32 __iomem) dinfo->ring_head != last_head) {
1476 end = jiffies + (HZ * 3);
1477 last_head = (u32 __iomem) dinfo->ring_head;
1478 }
1479 i++;
1480 if (time_before(end, jiffies)) {
1481 if (!i) {
1482 /* Try again */
1483 reset_state(dinfo);
1484 refresh_ring(dinfo);
1485 do_flush(dinfo);
1486 end = jiffies + (HZ * 3);
1487 i = 1;
1488 } else {
1489 WRN_MSG("ring buffer : space: %d wanted %d\n",
1490 dinfo->ring_space, n);
1491 WRN_MSG("lockup - turning off hardware "
1492 "acceleration\n");
1493 dinfo->ring_lockup = 1;
1494 break;
1495 }
1496 }
1497 udelay(1);
1498 }
1499 return i;
1500}
1501
1502static void
1503do_flush(struct intelfb_info *dinfo) {
1504 START_RING(2);
1505 OUT_RING(MI_FLUSH | MI_WRITE_DIRTY_STATE | MI_INVALIDATE_MAP_CACHE);
1506 OUT_RING(MI_NOOP);
1507 ADVANCE_RING();
1508}
1509
1510void
1511intelfbhw_do_sync(struct intelfb_info *dinfo)
1512{
1513#if VERBOSE > 0
1514 DBG_MSG("intelfbhw_do_sync\n");
1515#endif
1516
1517 if (!dinfo->accel)
1518 return;
1519
1520 /*
1521 * Send a flush, then wait until the ring is empty. This is what
1522 * the XFree86 driver does, and actually it doesn't seem a lot worse
1523 * than the recommended method (both have problems).
1524 */
1525 do_flush(dinfo);
1526 wait_ring(dinfo, dinfo->ring.size - RING_MIN_FREE);
1527 dinfo->ring_space = dinfo->ring.size - RING_MIN_FREE;
1528}
1529
1530static void
1531refresh_ring(struct intelfb_info *dinfo)
1532{
1533#if VERBOSE > 0
1534 DBG_MSG("refresh_ring\n");
1535#endif
1536
1537 dinfo->ring_head = (u8 __iomem *) (INREG(PRI_RING_HEAD) &
1538 RING_HEAD_MASK);
1539 dinfo->ring_tail = INREG(PRI_RING_TAIL) & RING_TAIL_MASK;
1540 if (dinfo->ring_tail + RING_MIN_FREE < (u32 __iomem)dinfo->ring_head)
1541 dinfo->ring_space = (u32 __iomem) dinfo->ring_head
1542 - (dinfo->ring_tail + RING_MIN_FREE);
1543 else
1544 dinfo->ring_space = (dinfo->ring.size +
1545 (u32 __iomem) dinfo->ring_head)
1546 - (dinfo->ring_tail + RING_MIN_FREE);
1547}
1548
1549static void
1550reset_state(struct intelfb_info *dinfo)
1551{
1552 int i;
1553 u32 tmp;
1554
1555#if VERBOSE > 0
1556 DBG_MSG("reset_state\n");
1557#endif
1558
1559 for (i = 0; i < FENCE_NUM; i++)
1560 OUTREG(FENCE + (i << 2), 0);
1561
1562 /* Flush the ring buffer if it's enabled. */
1563 tmp = INREG(PRI_RING_LENGTH);
1564 if (tmp & RING_ENABLE) {
1565#if VERBOSE > 0
1566 DBG_MSG("reset_state: ring was enabled\n");
1567#endif
1568 refresh_ring(dinfo);
1569 intelfbhw_do_sync(dinfo);
1570 DO_RING_IDLE();
1571 }
1572
1573 OUTREG(PRI_RING_LENGTH, 0);
1574 OUTREG(PRI_RING_HEAD, 0);
1575 OUTREG(PRI_RING_TAIL, 0);
1576 OUTREG(PRI_RING_START, 0);
1577}
1578
1579/* Stop the 2D engine, and turn off the ring buffer. */
1580void
1581intelfbhw_2d_stop(struct intelfb_info *dinfo)
1582{
1583#if VERBOSE > 0
1584 DBG_MSG("intelfbhw_2d_stop: accel: %d, ring_active: %d\n", dinfo->accel,
1585 dinfo->ring_active);
1586#endif
1587
1588 if (!dinfo->accel)
1589 return;
1590
1591 dinfo->ring_active = 0;
1592 reset_state(dinfo);
1593}
1594
1595/*
1596 * Enable the ring buffer, and initialise the 2D engine.
1597 * It is assumed that the graphics engine has been stopped by previously
1598 * calling intelfb_2d_stop().
1599 */
1600void
1601intelfbhw_2d_start(struct intelfb_info *dinfo)
1602{
1603#if VERBOSE > 0
1604 DBG_MSG("intelfbhw_2d_start: accel: %d, ring_active: %d\n",
1605 dinfo->accel, dinfo->ring_active);
1606#endif
1607
1608 if (!dinfo->accel)
1609 return;
1610
1611 /* Initialise the primary ring buffer. */
1612 OUTREG(PRI_RING_LENGTH, 0);
1613 OUTREG(PRI_RING_TAIL, 0);
1614 OUTREG(PRI_RING_HEAD, 0);
1615
1616 OUTREG(PRI_RING_START, dinfo->ring.physical & RING_START_MASK);
1617 OUTREG(PRI_RING_LENGTH,
1618 ((dinfo->ring.size - GTT_PAGE_SIZE) & RING_LENGTH_MASK) |
1619 RING_NO_REPORT | RING_ENABLE);
1620 refresh_ring(dinfo);
1621 dinfo->ring_active = 1;
1622}
1623
1624/* 2D fillrect (solid fill or invert) */
1625void
1626intelfbhw_do_fillrect(struct intelfb_info *dinfo, u32 x, u32 y, u32 w, u32 h,
1627 u32 color, u32 pitch, u32 bpp, u32 rop)
1628{
1629 u32 br00, br09, br13, br14, br16;
1630
1631#if VERBOSE > 0
1632 DBG_MSG("intelfbhw_do_fillrect: (%d,%d) %dx%d, c 0x%06x, p %d bpp %d, "
1633 "rop 0x%02x\n", x, y, w, h, color, pitch, bpp, rop);
1634#endif
1635
1636 br00 = COLOR_BLT_CMD;
1637 br09 = dinfo->fb_start + (y * pitch + x * (bpp / 8));
1638 br13 = (rop << ROP_SHIFT) | pitch;
1639 br14 = (h << HEIGHT_SHIFT) | ((w * (bpp / 8)) << WIDTH_SHIFT);
1640 br16 = color;
1641
1642 switch (bpp) {
1643 case 8:
1644 br13 |= COLOR_DEPTH_8;
1645 break;
1646 case 16:
1647 br13 |= COLOR_DEPTH_16;
1648 break;
1649 case 32:
1650 br13 |= COLOR_DEPTH_32;
1651 br00 |= WRITE_ALPHA | WRITE_RGB;
1652 break;
1653 }
1654
1655 START_RING(6);
1656 OUT_RING(br00);
1657 OUT_RING(br13);
1658 OUT_RING(br14);
1659 OUT_RING(br09);
1660 OUT_RING(br16);
1661 OUT_RING(MI_NOOP);
1662 ADVANCE_RING();
1663
1664#if VERBOSE > 0
1665 DBG_MSG("ring = 0x%08x, 0x%08x (%d)\n", dinfo->ring_head,
1666 dinfo->ring_tail, dinfo->ring_space);
1667#endif
1668}
1669
1670void
1671intelfbhw_do_bitblt(struct intelfb_info *dinfo, u32 curx, u32 cury,
1672 u32 dstx, u32 dsty, u32 w, u32 h, u32 pitch, u32 bpp)
1673{
1674 u32 br00, br09, br11, br12, br13, br22, br23, br26;
1675
1676#if VERBOSE > 0
1677 DBG_MSG("intelfbhw_do_bitblt: (%d,%d)->(%d,%d) %dx%d, p %d bpp %d\n",
1678 curx, cury, dstx, dsty, w, h, pitch, bpp);
1679#endif
1680
1681 br00 = XY_SRC_COPY_BLT_CMD;
1682 br09 = dinfo->fb_start;
1683 br11 = (pitch << PITCH_SHIFT);
1684 br12 = dinfo->fb_start;
1685 br13 = (SRC_ROP_GXCOPY << ROP_SHIFT) | (pitch << PITCH_SHIFT);
1686 br22 = (dstx << WIDTH_SHIFT) | (dsty << HEIGHT_SHIFT);
1687 br23 = ((dstx + w) << WIDTH_SHIFT) |
1688 ((dsty + h) << HEIGHT_SHIFT);
1689 br26 = (curx << WIDTH_SHIFT) | (cury << HEIGHT_SHIFT);
1690
1691 switch (bpp) {
1692 case 8:
1693 br13 |= COLOR_DEPTH_8;
1694 break;
1695 case 16:
1696 br13 |= COLOR_DEPTH_16;
1697 break;
1698 case 32:
1699 br13 |= COLOR_DEPTH_32;
1700 br00 |= WRITE_ALPHA | WRITE_RGB;
1701 break;
1702 }
1703
1704 START_RING(8);
1705 OUT_RING(br00);
1706 OUT_RING(br13);
1707 OUT_RING(br22);
1708 OUT_RING(br23);
1709 OUT_RING(br09);
1710 OUT_RING(br26);
1711 OUT_RING(br11);
1712 OUT_RING(br12);
1713 ADVANCE_RING();
1714}
1715
1716int
1717intelfbhw_do_drawglyph(struct intelfb_info *dinfo, u32 fg, u32 bg, u32 w,
1718 u32 h, const u8* cdat, u32 x, u32 y, u32 pitch, u32 bpp)
1719{
1720 int nbytes, ndwords, pad, tmp;
1721 u32 br00, br09, br13, br18, br19, br22, br23;
1722 int dat, ix, iy, iw;
1723 int i, j;
1724
1725#if VERBOSE > 0
1726 DBG_MSG("intelfbhw_do_drawglyph: (%d,%d) %dx%d\n", x, y, w, h);
1727#endif
1728
1729 /* size in bytes of a padded scanline */
1730 nbytes = ROUND_UP_TO(w, 16) / 8;
1731
1732 /* Total bytes of padded scanline data to write out. */
1733 nbytes = nbytes * h;
1734
1735 /*
1736 * Check if the glyph data exceeds the immediate mode limit.
1737 * It would take a large font (1K pixels) to hit this limit.
1738 */
1739 if (nbytes > MAX_MONO_IMM_SIZE)
1740 return 0;
1741
1742 /* Src data is packaged a dword (32-bit) at a time. */
1743 ndwords = ROUND_UP_TO(nbytes, 4) / 4;
1744
1745 /*
1746 * Ring has to be padded to a quad word. But because the command starts
1747 with 7 bytes, pad only if there is an even number of ndwords
1748 */
1749 pad = !(ndwords % 2);
1750
1751 tmp = (XY_MONO_SRC_IMM_BLT_CMD & DW_LENGTH_MASK) + ndwords;
1752 br00 = (XY_MONO_SRC_IMM_BLT_CMD & ~DW_LENGTH_MASK) | tmp;
1753 br09 = dinfo->fb_start;
1754 br13 = (SRC_ROP_GXCOPY << ROP_SHIFT) | (pitch << PITCH_SHIFT);
1755 br18 = bg;
1756 br19 = fg;
1757 br22 = (x << WIDTH_SHIFT) | (y << HEIGHT_SHIFT);
1758 br23 = ((x + w) << WIDTH_SHIFT) | ((y + h) << HEIGHT_SHIFT);
1759
1760 switch (bpp) {
1761 case 8:
1762 br13 |= COLOR_DEPTH_8;
1763 break;
1764 case 16:
1765 br13 |= COLOR_DEPTH_16;
1766 break;
1767 case 32:
1768 br13 |= COLOR_DEPTH_32;
1769 br00 |= WRITE_ALPHA | WRITE_RGB;
1770 break;
1771 }
1772
1773 START_RING(8 + ndwords);
1774 OUT_RING(br00);
1775 OUT_RING(br13);
1776 OUT_RING(br22);
1777 OUT_RING(br23);
1778 OUT_RING(br09);
1779 OUT_RING(br18);
1780 OUT_RING(br19);
1781 ix = iy = 0;
1782 iw = ROUND_UP_TO(w, 8) / 8;
1783 while (ndwords--) {
1784 dat = 0;
1785 for (j = 0; j < 2; ++j) {
1786 for (i = 0; i < 2; ++i) {
1787 if (ix != iw || i == 0)
1788 dat |= cdat[iy*iw + ix++] << (i+j*2)*8;
1789 }
1790 if (ix == iw && iy != (h-1)) {
1791 ix = 0;
1792 ++iy;
1793 }
1794 }
1795 OUT_RING(dat);
1796 }
1797 if (pad)
1798 OUT_RING(MI_NOOP);
1799 ADVANCE_RING();
1800
1801 return 1;
1802}
1803
1804/* HW cursor functions. */
1805void
1806intelfbhw_cursor_init(struct intelfb_info *dinfo)
1807{
1808 u32 tmp;
1809
1810#if VERBOSE > 0
1811 DBG_MSG("intelfbhw_cursor_init\n");
1812#endif
1813
Dave Airlie3aff13c2006-03-31 17:08:52 +10001814 if (dinfo->mobile || IS_I9XX(dinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (!dinfo->cursor.physical)
1816 return;
1817 tmp = INREG(CURSOR_A_CONTROL);
1818 tmp &= ~(CURSOR_MODE_MASK | CURSOR_MOBILE_GAMMA_ENABLE |
1819 CURSOR_MEM_TYPE_LOCAL |
1820 (1 << CURSOR_PIPE_SELECT_SHIFT));
1821 tmp |= CURSOR_MODE_DISABLE;
1822 OUTREG(CURSOR_A_CONTROL, tmp);
1823 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.physical);
1824 } else {
1825 tmp = INREG(CURSOR_CONTROL);
1826 tmp &= ~(CURSOR_FORMAT_MASK | CURSOR_GAMMA_ENABLE |
1827 CURSOR_ENABLE | CURSOR_STRIDE_MASK);
1828 tmp = CURSOR_FORMAT_3C;
1829 OUTREG(CURSOR_CONTROL, tmp);
1830 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.offset << 12);
1831 tmp = (64 << CURSOR_SIZE_H_SHIFT) |
1832 (64 << CURSOR_SIZE_V_SHIFT);
1833 OUTREG(CURSOR_SIZE, tmp);
1834 }
1835}
1836
1837void
1838intelfbhw_cursor_hide(struct intelfb_info *dinfo)
1839{
1840 u32 tmp;
1841
1842#if VERBOSE > 0
1843 DBG_MSG("intelfbhw_cursor_hide\n");
1844#endif
1845
1846 dinfo->cursor_on = 0;
Dave Airlie3aff13c2006-03-31 17:08:52 +10001847 if (dinfo->mobile || IS_I9XX(dinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (!dinfo->cursor.physical)
1849 return;
1850 tmp = INREG(CURSOR_A_CONTROL);
1851 tmp &= ~CURSOR_MODE_MASK;
1852 tmp |= CURSOR_MODE_DISABLE;
1853 OUTREG(CURSOR_A_CONTROL, tmp);
1854 /* Flush changes */
1855 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.physical);
1856 } else {
1857 tmp = INREG(CURSOR_CONTROL);
1858 tmp &= ~CURSOR_ENABLE;
1859 OUTREG(CURSOR_CONTROL, tmp);
1860 }
1861}
1862
1863void
1864intelfbhw_cursor_show(struct intelfb_info *dinfo)
1865{
1866 u32 tmp;
1867
1868#if VERBOSE > 0
1869 DBG_MSG("intelfbhw_cursor_show\n");
1870#endif
1871
1872 dinfo->cursor_on = 1;
1873
1874 if (dinfo->cursor_blanked)
1875 return;
1876
Dave Airlie3aff13c2006-03-31 17:08:52 +10001877 if (dinfo->mobile || IS_I9XX(dinfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 if (!dinfo->cursor.physical)
1879 return;
1880 tmp = INREG(CURSOR_A_CONTROL);
1881 tmp &= ~CURSOR_MODE_MASK;
1882 tmp |= CURSOR_MODE_64_4C_AX;
1883 OUTREG(CURSOR_A_CONTROL, tmp);
1884 /* Flush changes */
1885 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.physical);
1886 } else {
1887 tmp = INREG(CURSOR_CONTROL);
1888 tmp |= CURSOR_ENABLE;
1889 OUTREG(CURSOR_CONTROL, tmp);
1890 }
1891}
1892
1893void
1894intelfbhw_cursor_setpos(struct intelfb_info *dinfo, int x, int y)
1895{
1896 u32 tmp;
1897
1898#if VERBOSE > 0
1899 DBG_MSG("intelfbhw_cursor_setpos: (%d, %d)\n", x, y);
1900#endif
1901
1902 /*
Dave Airlie3aff13c2006-03-31 17:08:52 +10001903 * Sets the position. The coordinates are assumed to already
1904 * have any offset adjusted. Assume that the cursor is never
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 * completely off-screen, and that x, y are always >= 0.
1906 */
1907
1908 tmp = ((x & CURSOR_POS_MASK) << CURSOR_X_SHIFT) |
1909 ((y & CURSOR_POS_MASK) << CURSOR_Y_SHIFT);
1910 OUTREG(CURSOR_A_POSITION, tmp);
Dave Airlie8bb91f62006-03-23 13:06:32 +11001911
Dave Airlie3aff13c2006-03-31 17:08:52 +10001912 if (IS_I9XX(dinfo)) {
Dave Airlie8bb91f62006-03-23 13:06:32 +11001913 OUTREG(CURSOR_A_BASEADDR, dinfo->cursor.physical);
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
1917void
1918intelfbhw_cursor_setcolor(struct intelfb_info *dinfo, u32 bg, u32 fg)
1919{
1920#if VERBOSE > 0
1921 DBG_MSG("intelfbhw_cursor_setcolor\n");
1922#endif
1923
1924 OUTREG(CURSOR_A_PALETTE0, bg & CURSOR_PALETTE_MASK);
1925 OUTREG(CURSOR_A_PALETTE1, fg & CURSOR_PALETTE_MASK);
1926 OUTREG(CURSOR_A_PALETTE2, fg & CURSOR_PALETTE_MASK);
1927 OUTREG(CURSOR_A_PALETTE3, bg & CURSOR_PALETTE_MASK);
1928}
1929
1930void
1931intelfbhw_cursor_load(struct intelfb_info *dinfo, int width, int height,
1932 u8 *data)
1933{
1934 u8 __iomem *addr = (u8 __iomem *)dinfo->cursor.virtual;
1935 int i, j, w = width / 8;
1936 int mod = width % 8, t_mask, d_mask;
1937
1938#if VERBOSE > 0
1939 DBG_MSG("intelfbhw_cursor_load\n");
1940#endif
1941
1942 if (!dinfo->cursor.virtual)
1943 return;
1944
1945 t_mask = 0xff >> mod;
1946 d_mask = ~(0xff >> mod);
1947 for (i = height; i--; ) {
1948 for (j = 0; j < w; j++) {
1949 writeb(0x00, addr + j);
1950 writeb(*(data++), addr + j+8);
1951 }
1952 if (mod) {
1953 writeb(t_mask, addr + j);
1954 writeb(*(data++) & d_mask, addr + j+8);
1955 }
1956 addr += 16;
1957 }
1958}
1959
1960void
1961intelfbhw_cursor_reset(struct intelfb_info *dinfo) {
1962 u8 __iomem *addr = (u8 __iomem *)dinfo->cursor.virtual;
1963 int i, j;
1964
1965#if VERBOSE > 0
1966 DBG_MSG("intelfbhw_cursor_reset\n");
1967#endif
1968
1969 if (!dinfo->cursor.virtual)
1970 return;
1971
1972 for (i = 64; i--; ) {
1973 for (j = 0; j < 8; j++) {
1974 writeb(0xff, addr + j+0);
1975 writeb(0x00, addr + j+8);
1976 }
1977 addr += 16;
1978 }
1979}