3 * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200, G400 and G450.
5 * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
7 * Portions Copyright (c) 2001 Matrox Graphics Inc.
9 * Version: 1.65 2002/08/14
13 #include "matroxfb_maven.h"
14 #include "matroxfb_crtc2.h"
15 #include "matroxfb_misc.h"
16 #include "matroxfb_DAC1064.h"
17 #include <linux/matroxfb.h>
18 #include <asm/uaccess.h>
20 /* **************************************************** */
22 static int mem = 8192;
24 module_param(mem, int, 0);
25 MODULE_PARM_DESC(mem, "Memory size reserved for dualhead (default=8MB)");
27 /* **************************************************** */
29 static int matroxfb_dh_setcolreg(unsigned regno, unsigned red, unsigned green,
30 unsigned blue, unsigned transp, struct fb_info* info) {
32 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
36 if (m2info->fbcon.var.grayscale) {
37 /* gray = 0.30*R + 0.59*G + 0.11*B */
38 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
40 red = CNVT_TOHW(red, m2info->fbcon.var.red.length);
41 green = CNVT_TOHW(green, m2info->fbcon.var.green.length);
42 blue = CNVT_TOHW(blue, m2info->fbcon.var.blue.length);
43 transp = CNVT_TOHW(transp, m2info->fbcon.var.transp.length);
45 col = (red << m2info->fbcon.var.red.offset) |
46 (green << m2info->fbcon.var.green.offset) |
47 (blue << m2info->fbcon.var.blue.offset) |
48 (transp << m2info->fbcon.var.transp.offset);
50 switch (m2info->fbcon.var.bits_per_pixel) {
52 m2info->cmap[regno] = col | (col << 16);
55 m2info->cmap[regno] = col;
62 static void matroxfb_dh_restore(struct matroxfb_dh_fb_info* m2info,
63 struct my_timming* mt,
68 MINFO_FROM(m2info->primary_dev);
82 tmp |= 0x00000001; /* enable CRTC2 */
84 if (ACCESS_FBINFO(outputs[1]).src == MATROXFB_SRC_CRTC2) {
85 if (ACCESS_FBINFO(devflags.g450dac)) {
86 tmp |= 0x00000006; /* source from secondary pixel PLL */
87 /* no vidrst when in monitor mode */
88 if (ACCESS_FBINFO(outputs[1]).mode != MATROXFB_OUTPUT_MODE_MONITOR) {
89 tmp |= 0xC0001000; /* Enable H/V vidrst */
92 tmp |= 0x00000002; /* source from VDOCLK */
93 tmp |= 0xC0000000; /* enable vvidrst & hvidrst */
94 /* MGA TVO is our clock source */
96 } else if (ACCESS_FBINFO(outputs[0]).src == MATROXFB_SRC_CRTC2) {
97 tmp |= 0x00000004; /* source from pixclock */
98 /* PIXPLL is our clock source */
100 if (ACCESS_FBINFO(outputs[0]).src == MATROXFB_SRC_CRTC2) {
101 tmp |= 0x00100000; /* connect CRTC2 to DAC */
103 if (mt->interlaced) {
104 tmp |= 0x02000000; /* interlaced, second field is bigger, as G450 apparently ignores it */
106 mt->VSyncStart >>= 1;
110 if ((mt->HTotal & 7) == 2) {
111 datactl |= 0x00000010;
114 tmp |= 0x10000000; /* 0x10000000 is VIDRST polarity */
115 mga_outl(0x3C14, ((mt->HDisplay - 8) << 16) | (mt->HTotal - 8));
116 mga_outl(0x3C18, ((mt->HSyncEnd - 8) << 16) | (mt->HSyncStart - 8));
117 mga_outl(0x3C1C, ((mt->VDisplay - 1) << 16) | (mt->VTotal - 1));
118 mga_outl(0x3C20, ((mt->VSyncEnd - 1) << 16) | (mt->VSyncStart - 1));
119 mga_outl(0x3C24, ((mt->VSyncStart) << 16) | (mt->HSyncStart)); /* preload */
121 u_int32_t linelen = m2info->fbcon.var.xres_virtual * (m2info->fbcon.var.bits_per_pixel >> 3);
122 if (tmp & 0x02000000) {
123 /* field #0 is smaller, so... */
124 mga_outl(0x3C2C, pos); /* field #1 vmemory start */
125 mga_outl(0x3C28, pos + linelen); /* field #0 vmemory start */
127 m2info->interlaced = 1;
129 mga_outl(0x3C28, pos); /* vmemory start */
130 m2info->interlaced = 0;
132 mga_outl(0x3C40, linelen);
134 mga_outl(0x3C4C, datactl); /* data control */
135 if (tmp & 0x02000000) {
138 mga_outl(0x3C10, tmp & ~0x02000000);
139 for (i = 0; i < 2; i++) {
141 unsigned int lastl = 0;
143 while ((nl = mga_inl(0x3C48) & 0xFFF) >= lastl) {
148 mga_outl(0x3C10, tmp);
149 ACCESS_FBINFO(hw).crtc2.ctl = tmp;
151 tmp = mt->VDisplay << 16; /* line compare */
152 if (mt->sync & FB_SYNC_HOR_HIGH_ACT)
154 if (mt->sync & FB_SYNC_VERT_HIGH_ACT)
156 mga_outl(0x3C44, tmp);
159 static void matroxfb_dh_disable(struct matroxfb_dh_fb_info* m2info) {
160 MINFO_FROM(m2info->primary_dev);
162 mga_outl(0x3C10, 0x00000004); /* disable CRTC2, CRTC1->DAC1, PLL as clock source */
163 ACCESS_FBINFO(hw).crtc2.ctl = 0x00000004;
166 static void matroxfb_dh_pan_var(struct matroxfb_dh_fb_info* m2info,
167 struct fb_var_screeninfo* var) {
169 unsigned int linelen;
170 unsigned int pixelsize;
171 MINFO_FROM(m2info->primary_dev);
173 m2info->fbcon.var.xoffset = var->xoffset;
174 m2info->fbcon.var.yoffset = var->yoffset;
175 pixelsize = m2info->fbcon.var.bits_per_pixel >> 3;
176 linelen = m2info->fbcon.var.xres_virtual * pixelsize;
177 pos = m2info->fbcon.var.yoffset * linelen + m2info->fbcon.var.xoffset * pixelsize;
178 pos += m2info->video.offbase;
179 if (m2info->interlaced) {
180 mga_outl(0x3C2C, pos);
181 mga_outl(0x3C28, pos + linelen);
183 mga_outl(0x3C28, pos);
187 static int matroxfb_dh_decode_var(struct matroxfb_dh_fb_info* m2info,
188 struct fb_var_screeninfo* var,
194 unsigned int vramlen;
196 switch (var->bits_per_pixel) {
197 case 16: mask = 0x1F;
199 case 32: mask = 0x0F;
201 default: return -EINVAL;
203 vramlen = m2info->video.len_usable;
204 if (var->yres_virtual < var->yres)
205 var->yres_virtual = var->yres;
206 if (var->xres_virtual < var->xres)
207 var->xres_virtual = var->xres;
208 var->xres_virtual = (var->xres_virtual + mask) & ~mask;
209 if (var->yres_virtual > 32767)
211 memlen = var->xres_virtual * var->yres_virtual * (var->bits_per_pixel >> 3);
212 if (memlen > vramlen)
214 if (var->xoffset + var->xres > var->xres_virtual)
215 var->xoffset = var->xres_virtual - var->xres;
216 if (var->yoffset + var->yres > var->yres_virtual)
217 var->yoffset = var->yres_virtual - var->yres;
220 var->left_margin &= ~7;
221 var->right_margin &= ~7;
222 var->hsync_len &= ~7;
224 *mode = var->bits_per_pixel;
225 if (var->bits_per_pixel == 16) {
226 if (var->green.length == 5) {
227 var->red.offset = 10;
229 var->green.offset = 5;
230 var->green.length = 5;
231 var->blue.offset = 0;
232 var->blue.length = 5;
233 var->transp.offset = 15;
234 var->transp.length = 1;
237 var->red.offset = 11;
239 var->green.offset = 5;
240 var->green.length = 6;
241 var->blue.offset = 0;
242 var->blue.length = 5;
243 var->transp.offset = 0;
244 var->transp.length = 0;
247 var->red.offset = 16;
249 var->green.offset = 8;
250 var->green.length = 8;
251 var->blue.offset = 0;
252 var->blue.length = 8;
253 var->transp.offset = 24;
254 var->transp.length = 8;
256 *visual = FB_VISUAL_TRUECOLOR;
257 *video_cmap_len = 16;
261 static int matroxfb_dh_open(struct fb_info* info, int user) {
262 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
263 MINFO_FROM(m2info->primary_dev);
268 if (ACCESS_FBINFO(dead)) {
271 err = ACCESS_FBINFO(fbops).fb_open(&ACCESS_FBINFO(fbcon), user);
280 static int matroxfb_dh_release(struct fb_info* info, int user) {
281 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
283 MINFO_FROM(m2info->primary_dev);
286 err = ACCESS_FBINFO(fbops).fb_release(&ACCESS_FBINFO(fbcon), user);
292 static void matroxfb_dh_init_fix(struct matroxfb_dh_fb_info *m2info) {
293 struct fb_fix_screeninfo *fix = &m2info->fbcon.fix;
295 strcpy(fix->id, "MATROX DH");
297 fix->smem_start = m2info->video.base;
298 fix->smem_len = m2info->video.len_usable;
301 fix->xpanstep = 8; /* TBD */
302 fix->mmio_start = m2info->mmio.base;
303 fix->mmio_len = m2info->mmio.len;
304 fix->accel = 0; /* no accel... */
307 static int matroxfb_dh_check_var(struct fb_var_screeninfo* var, struct fb_info* info) {
308 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
313 return matroxfb_dh_decode_var(m2info, var, &visual, &cmap_len, &mode);
317 static int matroxfb_dh_set_par(struct fb_info* info) {
318 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
323 struct fb_var_screeninfo* var = &info->var;
324 MINFO_FROM(m2info->primary_dev);
326 if ((err = matroxfb_dh_decode_var(m2info, var, &visual, &cmap_len, &mode)) != 0)
330 m2info->fbcon.screen_base = vaddr_va(m2info->video.vbase);
331 m2info->fbcon.fix.visual = visual;
332 m2info->fbcon.fix.type = FB_TYPE_PACKED_PIXELS;
333 m2info->fbcon.fix.type_aux = 0;
334 m2info->fbcon.fix.line_length = (var->xres_virtual * var->bits_per_pixel) >> 3;
337 struct my_timming mt;
342 matroxfb_var2my(&m2info->fbcon.var, &mt);
343 mt.crtc = MATROXFB_SRC_CRTC2;
347 pos = (m2info->fbcon.var.yoffset * m2info->fbcon.var.xres_virtual + m2info->fbcon.var.xoffset) * m2info->fbcon.var.bits_per_pixel >> 3;
348 pos += m2info->video.offbase;
350 down_read(&ACCESS_FBINFO(altout).lock);
351 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
352 if (ACCESS_FBINFO(outputs[out]).src == MATROXFB_SRC_CRTC2) {
354 if (ACCESS_FBINFO(outputs[out]).output->compute) {
355 ACCESS_FBINFO(outputs[out]).output->compute(ACCESS_FBINFO(outputs[out]).data, &mt);
359 ACCESS_FBINFO(crtc2).pixclock = mt.pixclock;
360 ACCESS_FBINFO(crtc2).mnp = mt.mnp;
361 up_read(&ACCESS_FBINFO(altout).lock);
363 matroxfb_dh_restore(m2info, &mt, mode, pos);
365 matroxfb_dh_disable(m2info);
367 DAC1064_global_init(PMINFO2);
368 DAC1064_global_restore(PMINFO2);
369 down_read(&ACCESS_FBINFO(altout).lock);
370 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
371 if (ACCESS_FBINFO(outputs[out]).src == MATROXFB_SRC_CRTC2 &&
372 ACCESS_FBINFO(outputs[out]).output->program) {
373 ACCESS_FBINFO(outputs[out]).output->program(ACCESS_FBINFO(outputs[out]).data);
376 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
377 if (ACCESS_FBINFO(outputs[out]).src == MATROXFB_SRC_CRTC2 &&
378 ACCESS_FBINFO(outputs[out]).output->start) {
379 ACCESS_FBINFO(outputs[out]).output->start(ACCESS_FBINFO(outputs[out]).data);
382 up_read(&ACCESS_FBINFO(altout).lock);
384 m2info->initialized = 1;
389 static int matroxfb_dh_pan_display(struct fb_var_screeninfo* var, struct fb_info* info) {
390 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
391 matroxfb_dh_pan_var(m2info, var);
396 static int matroxfb_dh_get_vblank(const struct matroxfb_dh_fb_info* m2info, struct fb_vblank* vblank) {
397 MINFO_FROM(m2info->primary_dev);
399 matroxfb_enable_irq(PMINFO 0);
400 memset(vblank, 0, sizeof(*vblank));
401 vblank->flags = FB_VBLANK_HAVE_VCOUNT | FB_VBLANK_HAVE_VBLANK;
402 /* mask out reserved bits + field number (odd/even) */
403 vblank->vcount = mga_inl(0x3C48) & 0x000007FF;
404 /* compatibility stuff */
405 if (vblank->vcount >= m2info->fbcon.var.yres)
406 vblank->flags |= FB_VBLANK_VBLANKING;
407 if (test_bit(0, &ACCESS_FBINFO(irq_flags))) {
408 vblank->flags |= FB_VBLANK_HAVE_COUNT;
409 /* Only one writer, aligned int value...
410 it should work without lock and without atomic_t */
411 vblank->count = ACCESS_FBINFO(crtc2).vsync.cnt;
416 static int matroxfb_dh_ioctl(struct fb_info *info,
420 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
421 MINFO_FROM(m2info->primary_dev);
428 struct fb_vblank vblank;
431 err = matroxfb_dh_get_vblank(m2info, &vblank);
434 if (copy_to_user((void __user *)arg, &vblank, sizeof(vblank)))
438 case FBIO_WAITFORVSYNC:
442 if (get_user(crt, (u_int32_t __user *)arg))
447 return matroxfb_wait_for_sync(PMINFO 1);
449 case MATROXFB_SET_OUTPUT_MODE:
450 case MATROXFB_GET_OUTPUT_MODE:
451 case MATROXFB_GET_ALL_OUTPUTS:
453 return ACCESS_FBINFO(fbcon.fbops)->fb_ioctl(&ACCESS_FBINFO(fbcon), cmd, arg);
455 case MATROXFB_SET_OUTPUT_CONNECTION:
461 if (get_user(tmp, (u_int32_t __user *)arg))
463 for (out = 0; out < 32; out++) {
464 if (tmp & (1 << out)) {
465 if (out >= MATROXFB_MAX_OUTPUTS)
467 if (!ACCESS_FBINFO(outputs[out]).output)
469 switch (ACCESS_FBINFO(outputs[out]).src) {
470 case MATROXFB_SRC_NONE:
471 case MATROXFB_SRC_CRTC2:
478 if (ACCESS_FBINFO(devflags.panellink)) {
479 if (tmp & MATROXFB_OUTPUT_CONN_DFP)
481 if ((ACCESS_FBINFO(outputs[2]).src == MATROXFB_SRC_CRTC1) && tmp)
485 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
486 if (tmp & (1 << out)) {
487 if (ACCESS_FBINFO(outputs[out]).src != MATROXFB_SRC_CRTC2) {
489 ACCESS_FBINFO(outputs[out]).src = MATROXFB_SRC_CRTC2;
491 } else if (ACCESS_FBINFO(outputs[out]).src == MATROXFB_SRC_CRTC2) {
493 ACCESS_FBINFO(outputs[out]).src = MATROXFB_SRC_NONE;
498 matroxfb_dh_set_par(info);
501 case MATROXFB_GET_OUTPUT_CONNECTION:
506 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
507 if (ACCESS_FBINFO(outputs[out]).src == MATROXFB_SRC_CRTC2) {
511 if (put_user(conn, (u_int32_t __user *)arg))
515 case MATROXFB_GET_AVAILABLE_OUTPUTS:
520 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
521 if (ACCESS_FBINFO(outputs[out]).output) {
522 switch (ACCESS_FBINFO(outputs[out]).src) {
523 case MATROXFB_SRC_NONE:
524 case MATROXFB_SRC_CRTC2:
530 if (ACCESS_FBINFO(devflags.panellink)) {
531 tmp &= ~MATROXFB_OUTPUT_CONN_DFP;
532 if (ACCESS_FBINFO(outputs[2]).src == MATROXFB_SRC_CRTC1) {
536 if (put_user(tmp, (u_int32_t __user *)arg))
545 static int matroxfb_dh_blank(int blank, struct fb_info* info) {
546 #define m2info (container_of(info, struct matroxfb_dh_fb_info, fbcon))
554 /* do something... */
559 static struct fb_ops matroxfb_dh_ops = {
560 .owner = THIS_MODULE,
561 .fb_open = matroxfb_dh_open,
562 .fb_release = matroxfb_dh_release,
563 .fb_check_var = matroxfb_dh_check_var,
564 .fb_set_par = matroxfb_dh_set_par,
565 .fb_setcolreg = matroxfb_dh_setcolreg,
566 .fb_pan_display =matroxfb_dh_pan_display,
567 .fb_blank = matroxfb_dh_blank,
568 .fb_ioctl = matroxfb_dh_ioctl,
569 .fb_fillrect = cfb_fillrect,
570 .fb_copyarea = cfb_copyarea,
571 .fb_imageblit = cfb_imageblit,
574 static struct fb_var_screeninfo matroxfb_dh_defined = {
575 640,480,640,480,/* W,H, virtual W,H */
585 -1,-1, /* display size */
587 39721L,48L,16L,33L,10L,
588 96L,2,0, /* no sync info */
589 FB_VMODE_NONINTERLACED,
593 static int matroxfb_dh_regit(CPMINFO struct matroxfb_dh_fb_info* m2info) {
594 #define minfo (m2info->primary_dev)
597 m2info->fbcon.fbops = &matroxfb_dh_ops;
598 m2info->fbcon.flags = FBINFO_FLAG_DEFAULT;
599 m2info->fbcon.flags |= FBINFO_HWACCEL_XPAN |
601 m2info->fbcon.pseudo_palette = m2info->cmap;
602 fb_alloc_cmap(&m2info->fbcon.cmap, 256, 1);
608 mem &= ~0x00000FFF; /* PAGE_MASK? */
609 if (ACCESS_FBINFO(video.len_usable) + mem <= ACCESS_FBINFO(video.len))
610 m2info->video.offbase = ACCESS_FBINFO(video.len) - mem;
611 else if (ACCESS_FBINFO(video.len) < mem) {
613 } else { /* check yres on first head... */
614 m2info->video.borrowed = mem;
615 ACCESS_FBINFO(video.len_usable) -= mem;
616 m2info->video.offbase = ACCESS_FBINFO(video.len_usable);
618 m2info->video.base = ACCESS_FBINFO(video.base) + m2info->video.offbase;
619 m2info->video.len = m2info->video.len_usable = m2info->video.len_maximum = mem;
620 m2info->video.vbase.vaddr = vaddr_va(ACCESS_FBINFO(video.vbase)) + m2info->video.offbase;
621 m2info->mmio.base = ACCESS_FBINFO(mmio.base);
622 m2info->mmio.vbase = ACCESS_FBINFO(mmio.vbase);
623 m2info->mmio.len = ACCESS_FBINFO(mmio.len);
625 matroxfb_dh_init_fix(m2info);
626 if (register_framebuffer(&m2info->fbcon)) {
629 if (!m2info->initialized)
630 fb_set_var(&m2info->fbcon, &matroxfb_dh_defined);
631 down_write(&ACCESS_FBINFO(crtc2.lock));
632 oldcrtc2 = ACCESS_FBINFO(crtc2.info);
633 ACCESS_FBINFO(crtc2.info) = m2info;
634 up_write(&ACCESS_FBINFO(crtc2.lock));
636 printk(KERN_ERR "matroxfb_crtc2: Internal consistency check failed: crtc2 already present: %p\n",
643 /* ************************** */
645 static int matroxfb_dh_registerfb(struct matroxfb_dh_fb_info* m2info) {
646 #define minfo (m2info->primary_dev)
647 if (matroxfb_dh_regit(PMINFO m2info)) {
648 printk(KERN_ERR "matroxfb_crtc2: secondary head failed to register\n");
651 printk(KERN_INFO "matroxfb_crtc2: secondary head of fb%u was registered as fb%u\n",
652 ACCESS_FBINFO(fbcon.node), m2info->fbcon.node);
653 m2info->fbcon_registered = 1;
658 static void matroxfb_dh_deregisterfb(struct matroxfb_dh_fb_info* m2info) {
659 #define minfo (m2info->primary_dev)
660 if (m2info->fbcon_registered) {
662 struct matroxfb_dh_fb_info* crtc2;
664 down_write(&ACCESS_FBINFO(crtc2.lock));
665 crtc2 = ACCESS_FBINFO(crtc2.info);
667 ACCESS_FBINFO(crtc2.info) = NULL;
668 up_write(&ACCESS_FBINFO(crtc2.lock));
669 if (crtc2 != m2info) {
670 printk(KERN_ERR "matroxfb_crtc2: Internal consistency check failed: crtc2 mismatch at unload: %p != %p\n",
672 printk(KERN_ERR "matroxfb_crtc2: Expect kernel crash after module unload.\n");
675 id = m2info->fbcon.node;
676 unregister_framebuffer(&m2info->fbcon);
677 /* return memory back to primary head */
678 ACCESS_FBINFO(video.len_usable) += m2info->video.borrowed;
679 printk(KERN_INFO "matroxfb_crtc2: fb%u unregistered\n", id);
680 m2info->fbcon_registered = 0;
685 static void* matroxfb_crtc2_probe(struct matrox_fb_info* minfo) {
686 struct matroxfb_dh_fb_info* m2info;
688 /* hardware is CRTC2 incapable... */
689 if (!ACCESS_FBINFO(devflags.crtc2))
691 m2info = kzalloc(sizeof(*m2info), GFP_KERNEL);
693 printk(KERN_ERR "matroxfb_crtc2: Not enough memory for CRTC2 control structs\n");
696 m2info->primary_dev = MINFO;
697 if (matroxfb_dh_registerfb(m2info)) {
699 printk(KERN_ERR "matroxfb_crtc2: CRTC2 framebuffer failed to register\n");
705 static void matroxfb_crtc2_remove(struct matrox_fb_info* minfo, void* crtc2) {
706 matroxfb_dh_deregisterfb(crtc2);
710 static struct matroxfb_driver crtc2 = {
711 .name = "Matrox G400 CRTC2",
712 .probe = matroxfb_crtc2_probe,
713 .remove = matroxfb_crtc2_remove };
715 static int matroxfb_crtc2_init(void) {
716 if (fb_get_options("matrox_crtc2fb", NULL))
719 matroxfb_register_driver(&crtc2);
723 static void matroxfb_crtc2_exit(void) {
724 matroxfb_unregister_driver(&crtc2);
727 MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
728 MODULE_DESCRIPTION("Matrox G400 CRTC2 driver");
729 MODULE_LICENSE("GPL");
730 module_init(matroxfb_crtc2_init);
731 module_exit(matroxfb_crtc2_exit);
732 /* we do not have __setup() yet */