blob: 867a4d915e7768f0b7e5d137e3609d5f40fe3ecd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200 and G400
4 *
5 * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
6 *
7 * Portions Copyright (c) 2001 Matrox Graphics Inc.
8 *
9 * Version: 1.65 2002/08/14
10 *
11 * MTRR stuff: 1998 Tom Rini <trini@kernel.crashing.org>
12 *
13 * Contributors: "menion?" <menion@mindless.com>
14 * Betatesting, fixes, ideas
15 *
16 * "Kurt Garloff" <garloff@suse.de>
17 * Betatesting, fixes, ideas, videomodes, videomodes timmings
18 *
19 * "Tom Rini" <trini@kernel.crashing.org>
20 * MTRR stuff, PPC cleanups, betatesting, fixes, ideas
21 *
22 * "Bibek Sahu" <scorpio@dodds.net>
23 * Access device through readb|w|l and write b|w|l
24 * Extensive debugging stuff
25 *
26 * "Daniel Haun" <haund@usa.net>
27 * Testing, hardware cursor fixes
28 *
29 * "Scott Wood" <sawst46+@pitt.edu>
30 * Fixes
31 *
32 * "Gerd Knorr" <kraxel@goldbach.isdn.cs.tu-berlin.de>
33 * Betatesting
34 *
35 * "Kelly French" <targon@hazmat.com>
36 * "Fernando Herrera" <fherrera@eurielec.etsit.upm.es>
37 * Betatesting, bug reporting
38 *
39 * "Pablo Bianucci" <pbian@pccp.com.ar>
40 * Fixes, ideas, betatesting
41 *
42 * "Inaky Perez Gonzalez" <inaky@peloncho.fis.ucm.es>
43 * Fixes, enhandcements, ideas, betatesting
44 *
45 * "Ryuichi Oikawa" <roikawa@rr.iiij4u.or.jp>
46 * PPC betatesting, PPC support, backward compatibility
47 *
48 * "Paul Womar" <Paul@pwomar.demon.co.uk>
49 * "Owen Waller" <O.Waller@ee.qub.ac.uk>
50 * PPC betatesting
51 *
52 * "Thomas Pornin" <pornin@bolet.ens.fr>
53 * Alpha betatesting
54 *
55 * "Pieter van Leuven" <pvl@iae.nl>
56 * "Ulf Jaenicke-Roessler" <ujr@physik.phy.tu-dresden.de>
57 * G100 testing
58 *
59 * "H. Peter Arvin" <hpa@transmeta.com>
60 * Ideas
61 *
62 * "Cort Dougan" <cort@cs.nmt.edu>
63 * CHRP fixes and PReP cleanup
64 *
65 * "Mark Vojkovich" <mvojkovi@ucsd.edu>
66 * G400 support
67 *
68 * "Samuel Hocevar" <sam@via.ecp.fr>
69 * Fixes
70 *
71 * "Anton Altaparmakov" <AntonA@bigfoot.com>
72 * G400 MAX/non-MAX distinction
73 *
74 * "Ken Aaker" <kdaaker@rchland.vnet.ibm.com>
75 * memtype extension (needed for GXT130P RS/6000 adapter)
76 *
77 * "Uns Lider" <unslider@miranda.org>
78 * G100 PLNWT fixes
79 *
80 * "Denis Zaitsev" <zzz@cd-club.ru>
81 * Fixes
82 *
83 * "Mike Pieper" <mike@pieper-family.de>
84 * TVOut enhandcements, V4L2 control interface.
85 *
86 * "Diego Biurrun" <diego@biurrun.de>
87 * DFP testing
88 *
89 * (following author is not in any relation with this code, but his code
90 * is included in this driver)
91 *
92 * Based on framebuffer driver for VBE 2.0 compliant graphic boards
93 * (c) 1998 Gerd Knorr <kraxel@cs.tu-berlin.de>
94 *
95 * (following author is not in any relation with this code, but his ideas
Robert P. J. Daybeb7dd82007-05-09 07:14:03 +020096 * were used when writing this driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
98 * FreeVBE/AF (Matrox), "Shawn Hargreaves" <shawn@talula.demon.co.uk>
99 *
100 */
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <linux/version.h>
103
Andrew Mortona50d9132006-06-21 01:57:39 -0300104#define __OLD_VIDIOC_
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#include "matroxfb_base.h"
107#include "matroxfb_misc.h"
108#include "matroxfb_accel.h"
109#include "matroxfb_DAC1064.h"
110#include "matroxfb_Ti3026.h"
111#include "matroxfb_maven.h"
112#include "matroxfb_crtc2.h"
113#include "matroxfb_g450.h"
114#include <linux/matroxfb.h>
115#include <linux/interrupt.h>
Krzysztof Helt84902b72007-10-16 01:29:04 -0700116#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118#ifdef CONFIG_PPC_PMAC
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +1100119#include <asm/machdep.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120unsigned char nvram_read_byte(int);
121static int default_vmode = VMODE_NVRAM;
122static int default_cmode = CMODE_NVRAM;
123#endif
124
125static void matroxfb_unregister_device(struct matrox_fb_info* minfo);
126
127/* --------------------------------------------------------------------- */
128
129/*
130 * card parameters
131 */
132
133/* --------------------------------------------------------------------- */
134
135static struct fb_var_screeninfo vesafb_defined = {
136 640,480,640,480,/* W,H, W, H (virtual) load xres,xres_virtual*/
137 0,0, /* virtual -> visible no offset */
138 8, /* depth -> load bits_per_pixel */
139 0, /* greyscale ? */
140 {0,0,0}, /* R */
141 {0,0,0}, /* G */
142 {0,0,0}, /* B */
143 {0,0,0}, /* transparency */
144 0, /* standard pixel format */
145 FB_ACTIVATE_NOW,
146 -1,-1,
147 FB_ACCELF_TEXT, /* accel flags */
148 39721L,48L,16L,33L,10L,
149 96L,2L,~0, /* No sync info */
150 FB_VMODE_NONINTERLACED,
151 0, {0,0,0,0,0}
152};
153
154
155
156/* --------------------------------------------------------------------- */
157static void update_crtc2(WPMINFO unsigned int pos) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700158 struct matroxfb_dh_fb_info *info = minfo->crtc2.info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 /* Make sure that displays are compatible */
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700161 if (info && (info->fbcon.var.bits_per_pixel == minfo->fbcon.var.bits_per_pixel)
162 && (info->fbcon.var.xres_virtual == minfo->fbcon.var.xres_virtual)
163 && (info->fbcon.var.green.length == minfo->fbcon.var.green.length)
Andrew Mortona50d9132006-06-21 01:57:39 -0300164 ) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700165 switch (minfo->fbcon.var.bits_per_pixel) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 case 16:
167 case 32:
168 pos = pos * 8;
169 if (info->interlaced) {
170 mga_outl(0x3C2C, pos);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700171 mga_outl(0x3C28, pos + minfo->fbcon.var.xres_virtual * minfo->fbcon.var.bits_per_pixel / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 } else {
173 mga_outl(0x3C28, pos);
174 }
175 break;
176 }
177 }
178}
179
180static void matroxfb_crtc1_panpos(WPMINFO2) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700181 if (minfo->crtc1.panpos >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned long flags;
183 int panpos;
184
185 matroxfb_DAC_lock_irqsave(flags);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700186 panpos = minfo->crtc1.panpos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (panpos >= 0) {
188 unsigned int extvga_reg;
189
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700190 minfo->crtc1.panpos = -1; /* No update pending anymore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 extvga_reg = mga_inb(M_EXTVGA_INDEX);
192 mga_setr(M_EXTVGA_INDEX, 0x00, panpos);
193 if (extvga_reg != 0x00) {
194 mga_outb(M_EXTVGA_INDEX, extvga_reg);
195 }
196 }
197 matroxfb_DAC_unlock_irqrestore(flags);
198 }
199}
200
David Howells7d12e782006-10-05 14:55:46 +0100201static irqreturn_t matrox_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 u_int32_t status;
204 int handled = 0;
205
206 MINFO_FROM(dev_id);
207
208 status = mga_inl(M_STATUS);
209
210 if (status & 0x20) {
211 mga_outl(M_ICLEAR, 0x20);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700212 minfo->crtc1.vsync.cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 matroxfb_crtc1_panpos(PMINFO2);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700214 wake_up_interruptible(&minfo->crtc1.vsync.wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 handled = 1;
216 }
217 if (status & 0x200) {
218 mga_outl(M_ICLEAR, 0x200);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700219 minfo->crtc2.vsync.cnt++;
220 wake_up_interruptible(&minfo->crtc2.vsync.wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 handled = 1;
222 }
223 return IRQ_RETVAL(handled);
224}
225
226int matroxfb_enable_irq(WPMINFO int reenable) {
227 u_int32_t bm;
Andrew Mortona50d9132006-06-21 01:57:39 -0300228
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700229 if (minfo->devflags.accelerator == FB_ACCEL_MATROX_MGAG400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 bm = 0x220;
231 else
232 bm = 0x020;
233
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700234 if (!test_and_set_bit(0, &minfo->irq_flags)) {
235 if (request_irq(minfo->pcidev->irq, matrox_irq,
236 IRQF_SHARED, "matroxfb", minfo)) {
237 clear_bit(0, &minfo->irq_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return -EINVAL;
239 }
240 /* Clear any pending field interrupts */
241 mga_outl(M_ICLEAR, bm);
242 mga_outl(M_IEN, mga_inl(M_IEN) | bm);
243 } else if (reenable) {
244 u_int32_t ien;
Andrew Mortona50d9132006-06-21 01:57:39 -0300245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 ien = mga_inl(M_IEN);
247 if ((ien & bm) != bm) {
248 printk(KERN_DEBUG "matroxfb: someone disabled IRQ [%08X]\n", ien);
249 mga_outl(M_IEN, ien | bm);
250 }
251 }
252 return 0;
253}
254
255static void matroxfb_disable_irq(WPMINFO2) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700256 if (test_and_clear_bit(0, &minfo->irq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /* Flush pending pan-at-vbl request... */
258 matroxfb_crtc1_panpos(PMINFO2);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700259 if (minfo->devflags.accelerator == FB_ACCEL_MATROX_MGAG400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 mga_outl(M_IEN, mga_inl(M_IEN) & ~0x220);
261 else
262 mga_outl(M_IEN, mga_inl(M_IEN) & ~0x20);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700263 free_irq(minfo->pcidev->irq, minfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265}
266
267int matroxfb_wait_for_sync(WPMINFO u_int32_t crtc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct matrox_vsync *vs;
269 unsigned int cnt;
270 int ret;
271
272 switch (crtc) {
273 case 0:
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700274 vs = &minfo->crtc1.vsync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 break;
276 case 1:
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700277 if (minfo->devflags.accelerator != FB_ACCEL_MATROX_MGAG400) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return -ENODEV;
279 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700280 vs = &minfo->crtc2.vsync;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 default:
283 return -ENODEV;
284 }
285 ret = matroxfb_enable_irq(PMINFO 0);
286 if (ret) {
287 return ret;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 cnt = vs->cnt;
291 ret = wait_event_interruptible_timeout(vs->wait, cnt != vs->cnt, HZ/10);
292 if (ret < 0) {
293 return ret;
294 }
295 if (ret == 0) {
296 matroxfb_enable_irq(PMINFO 1);
297 return -ETIMEDOUT;
298 }
299 return 0;
300}
301
302/* --------------------------------------------------------------------- */
303
304static void matrox_pan_var(WPMINFO struct fb_var_screeninfo *var) {
305 unsigned int pos;
306 unsigned short p0, p1, p2;
307#ifdef CONFIG_FB_MATROX_32MB
308 unsigned int p3;
309#endif
310 int vbl;
311 unsigned long flags;
312
313 CRITFLAGS
314
Harvey Harrison5ae12172008-04-28 02:15:47 -0700315 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700317 if (minfo->dead)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return;
319
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700320 minfo->fbcon.var.xoffset = var->xoffset;
321 minfo->fbcon.var.yoffset = var->yoffset;
322 pos = (minfo->fbcon.var.yoffset * minfo->fbcon.var.xres_virtual + minfo->fbcon.var.xoffset) * minfo->curr.final_bppShift / 32;
323 pos += minfo->curr.ydstorg.chunks;
324 p0 = minfo->hw.CRTC[0x0D] = pos & 0xFF;
325 p1 = minfo->hw.CRTC[0x0C] = (pos & 0xFF00) >> 8;
326 p2 = minfo->hw.CRTCEXT[0] = (minfo->hw.CRTCEXT[0] & 0xB0) | ((pos >> 16) & 0x0F) | ((pos >> 14) & 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#ifdef CONFIG_FB_MATROX_32MB
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700328 p3 = minfo->hw.CRTCEXT[8] = pos >> 21;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#endif
330
331 /* FB_ACTIVATE_VBL and we can acquire interrupts? Honor FB_ACTIVATE_VBL then... */
332 vbl = (var->activate & FB_ACTIVATE_VBL) && (matroxfb_enable_irq(PMINFO 0) == 0);
333
334 CRITBEGIN
335
336 matroxfb_DAC_lock_irqsave(flags);
337 mga_setr(M_CRTC_INDEX, 0x0D, p0);
338 mga_setr(M_CRTC_INDEX, 0x0C, p1);
339#ifdef CONFIG_FB_MATROX_32MB
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700340 if (minfo->devflags.support32MB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 mga_setr(M_EXTVGA_INDEX, 0x08, p3);
342#endif
343 if (vbl) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700344 minfo->crtc1.panpos = p2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 } else {
346 /* Abort any pending change */
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700347 minfo->crtc1.panpos = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 mga_setr(M_EXTVGA_INDEX, 0x00, p2);
349 }
350 matroxfb_DAC_unlock_irqrestore(flags);
Andrew Mortona50d9132006-06-21 01:57:39 -0300351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 update_crtc2(PMINFO pos);
353
354 CRITEND
355}
356
357static void matroxfb_remove(WPMINFO int dummy) {
358 /* Currently we are holding big kernel lock on all dead & usecount updates.
359 * Destroy everything after all users release it. Especially do not unregister
360 * framebuffer and iounmap memory, neither fbmem nor fbcon-cfb* does not check
361 * for device unplugged when in use.
362 * In future we should point mmio.vbase & video.vbase somewhere where we can
363 * write data without causing too much damage...
364 */
365
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700366 minfo->dead = 1;
367 if (minfo->usecount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* destroy it later */
369 return;
370 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700371 matroxfb_unregister_device(minfo);
372 unregister_framebuffer(&minfo->fbcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 matroxfb_g450_shutdown(PMINFO2);
374#ifdef CONFIG_MTRR
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700375 if (minfo->mtrr.vram_valid)
376 mtrr_del(minfo->mtrr.vram, minfo->video.base, minfo->video.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#endif
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700378 mga_iounmap(minfo->mmio.vbase);
379 mga_iounmap(minfo->video.vbase);
380 release_mem_region(minfo->video.base, minfo->video.len_maximum);
381 release_mem_region(minfo->mmio.base, 16384);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 kfree(minfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385 /*
386 * Open/Release the frame buffer device
387 */
388
389static int matroxfb_open(struct fb_info *info, int user)
390{
391 MINFO_FROM_INFO(info);
Andrew Mortona50d9132006-06-21 01:57:39 -0300392
Harvey Harrison5ae12172008-04-28 02:15:47 -0700393 DBG_LOOP(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700395 if (minfo->dead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return -ENXIO;
397 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700398 minfo->usecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (user) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700400 minfo->userusecount++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402 return(0);
403}
404
405static int matroxfb_release(struct fb_info *info, int user)
406{
407 MINFO_FROM_INFO(info);
Andrew Mortona50d9132006-06-21 01:57:39 -0300408
Harvey Harrison5ae12172008-04-28 02:15:47 -0700409 DBG_LOOP(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (user) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700412 if (0 == --minfo->userusecount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 matroxfb_disable_irq(PMINFO2);
414 }
415 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700416 if (!(--minfo->usecount) && minfo->dead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 matroxfb_remove(PMINFO 0);
418 }
419 return(0);
420}
421
422static int matroxfb_pan_display(struct fb_var_screeninfo *var,
423 struct fb_info* info) {
424 MINFO_FROM_INFO(info);
425
Harvey Harrison5ae12172008-04-28 02:15:47 -0700426 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 matrox_pan_var(PMINFO var);
429 return 0;
430}
431
432static int matroxfb_get_final_bppShift(CPMINFO int bpp) {
433 int bppshft2;
434
Harvey Harrison5ae12172008-04-28 02:15:47 -0700435 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 bppshft2 = bpp;
438 if (!bppshft2) {
439 return 8;
440 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700441 if (isInterleave(minfo))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 bppshft2 >>= 1;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700443 if (minfo->devflags.video64bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 bppshft2 >>= 1;
445 return bppshft2;
446}
447
448static int matroxfb_test_and_set_rounding(CPMINFO int xres, int bpp) {
449 int over;
450 int rounding;
451
Harvey Harrison5ae12172008-04-28 02:15:47 -0700452 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 switch (bpp) {
455 case 0: return xres;
456 case 4: rounding = 128;
457 break;
458 case 8: rounding = 64; /* doc says 64; 32 is OK for G400 */
459 break;
460 case 16: rounding = 32;
461 break;
462 case 24: rounding = 64; /* doc says 64; 32 is OK for G400 */
463 break;
464 default: rounding = 16;
465 /* on G400, 16 really does not work */
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700466 if (minfo->devflags.accelerator == FB_ACCEL_MATROX_MGAG400)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 rounding = 32;
468 break;
469 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700470 if (isInterleave(minfo)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 rounding *= 2;
472 }
473 over = xres % rounding;
474 if (over)
475 xres += rounding-over;
476 return xres;
477}
478
479static int matroxfb_pitch_adjust(CPMINFO int xres, int bpp) {
480 const int* width;
481 int xres_new;
482
Harvey Harrison5ae12172008-04-28 02:15:47 -0700483 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (!bpp) return xres;
486
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700487 width = minfo->capable.vxres;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700489 if (minfo->devflags.precise_width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 while (*width) {
491 if ((*width >= xres) && (matroxfb_test_and_set_rounding(PMINFO *width, bpp) == *width)) {
492 break;
493 }
494 width++;
495 }
496 xres_new = *width;
497 } else {
498 xres_new = matroxfb_test_and_set_rounding(PMINFO xres, bpp);
499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return xres_new;
501}
502
503static int matroxfb_get_cmap_len(struct fb_var_screeninfo *var) {
504
Harvey Harrison5ae12172008-04-28 02:15:47 -0700505 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 switch (var->bits_per_pixel) {
508 case 4:
509 return 16; /* pseudocolor... 16 entries HW palette */
510 case 8:
511 return 256; /* pseudocolor... 256 entries HW palette */
512 case 16:
513 return 16; /* directcolor... 16 entries SW palette */
514 /* Mystique: truecolor, 16 entries SW palette, HW palette hardwired into 1:1 mapping */
515 case 24:
516 return 16; /* directcolor... 16 entries SW palette */
517 /* Mystique: truecolor, 16 entries SW palette, HW palette hardwired into 1:1 mapping */
518 case 32:
519 return 16; /* directcolor... 16 entries SW palette */
520 /* Mystique: truecolor, 16 entries SW palette, HW palette hardwired into 1:1 mapping */
521 }
522 return 16; /* return something reasonable... or panic()? */
523}
524
525static int matroxfb_decode_var(CPMINFO struct fb_var_screeninfo *var, int *visual, int *video_cmap_len, unsigned int* ydstorg) {
526 struct RGBT {
527 unsigned char bpp;
528 struct {
529 unsigned char offset,
530 length;
531 } red,
532 green,
533 blue,
534 transp;
535 signed char visual;
536 };
537 static const struct RGBT table[]= {
538 { 8,{ 0,8},{0,8},{0,8},{ 0,0},MX_VISUAL_PSEUDOCOLOR},
539 {15,{10,5},{5,5},{0,5},{15,1},MX_VISUAL_DIRECTCOLOR},
540 {16,{11,5},{5,6},{0,5},{ 0,0},MX_VISUAL_DIRECTCOLOR},
541 {24,{16,8},{8,8},{0,8},{ 0,0},MX_VISUAL_DIRECTCOLOR},
542 {32,{16,8},{8,8},{0,8},{24,8},MX_VISUAL_DIRECTCOLOR}
543 };
544 struct RGBT const *rgbt;
545 unsigned int bpp = var->bits_per_pixel;
546 unsigned int vramlen;
547 unsigned int memlen;
548
Harvey Harrison5ae12172008-04-28 02:15:47 -0700549 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 switch (bpp) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700552 case 4: if (!minfo->capable.cfb4) return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 break;
554 case 8: break;
555 case 16: break;
556 case 24: break;
557 case 32: break;
558 default: return -EINVAL;
559 }
560 *ydstorg = 0;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700561 vramlen = minfo->video.len_usable;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (var->yres_virtual < var->yres)
563 var->yres_virtual = var->yres;
564 if (var->xres_virtual < var->xres)
565 var->xres_virtual = var->xres;
566
567 var->xres_virtual = matroxfb_pitch_adjust(PMINFO var->xres_virtual, bpp);
568 memlen = var->xres_virtual * bpp * var->yres_virtual / 8;
569 if (memlen > vramlen) {
570 var->yres_virtual = vramlen * 8 / (var->xres_virtual * bpp);
571 memlen = var->xres_virtual * bpp * var->yres_virtual / 8;
572 }
573 /* There is hardware bug that no line can cross 4MB boundary */
574 /* give up for CFB24, it is impossible to easy workaround it */
575 /* for other try to do something */
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700576 if (!minfo->capable.cross4MB && (memlen > 0x400000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 if (bpp == 24) {
578 /* sorry */
579 } else {
580 unsigned int linelen;
581 unsigned int m1 = linelen = var->xres_virtual * bpp / 8;
582 unsigned int m2 = PAGE_SIZE; /* or 128 if you do not need PAGE ALIGNED address */
583 unsigned int max_yres;
584
585 while (m1) {
586 int t;
587
588 while (m2 >= m1) m2 -= m1;
589 t = m1;
590 m1 = m2;
591 m2 = t;
592 }
593 m2 = linelen * PAGE_SIZE / m2;
594 *ydstorg = m2 = 0x400000 % m2;
595 max_yres = (vramlen - m2) / linelen;
596 if (var->yres_virtual > max_yres)
597 var->yres_virtual = max_yres;
598 }
599 }
600 /* YDSTLEN contains only signed 16bit value */
601 if (var->yres_virtual > 32767)
602 var->yres_virtual = 32767;
603 /* we must round yres/xres down, we already rounded y/xres_virtual up
604 if it was possible. We should return -EINVAL, but I disagree */
605 if (var->yres_virtual < var->yres)
606 var->yres = var->yres_virtual;
607 if (var->xres_virtual < var->xres)
608 var->xres = var->xres_virtual;
609 if (var->xoffset + var->xres > var->xres_virtual)
610 var->xoffset = var->xres_virtual - var->xres;
611 if (var->yoffset + var->yres > var->yres_virtual)
612 var->yoffset = var->yres_virtual - var->yres;
613
614 if (bpp == 16 && var->green.length == 5) {
615 bpp--; /* an artifical value - 15 */
616 }
617
618 for (rgbt = table; rgbt->bpp < bpp; rgbt++);
619#define SETCLR(clr)\
620 var->clr.offset = rgbt->clr.offset;\
621 var->clr.length = rgbt->clr.length
622 SETCLR(red);
623 SETCLR(green);
624 SETCLR(blue);
625 SETCLR(transp);
626#undef SETCLR
627 *visual = rgbt->visual;
628
629 if (bpp > 8)
630 dprintk("matroxfb: truecolor: "
631 "size=%d:%d:%d:%d, shift=%d:%d:%d:%d\n",
632 var->transp.length, var->red.length, var->green.length, var->blue.length,
633 var->transp.offset, var->red.offset, var->green.offset, var->blue.offset);
634
635 *video_cmap_len = matroxfb_get_cmap_len(var);
636 dprintk(KERN_INFO "requested %d*%d/%dbpp (%d*%d)\n", var->xres, var->yres, var->bits_per_pixel,
637 var->xres_virtual, var->yres_virtual);
638 return 0;
639}
640
641static int matroxfb_setcolreg(unsigned regno, unsigned red, unsigned green,
642 unsigned blue, unsigned transp,
643 struct fb_info *fb_info)
644{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct matrox_fb_info* minfo = container_of(fb_info, struct matrox_fb_info, fbcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Harvey Harrison5ae12172008-04-28 02:15:47 -0700647 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 /*
650 * Set a single color register. The values supplied are
651 * already rounded down to the hardware's capabilities
652 * (according to the entries in the `var' structure). Return
653 * != 0 for invalid regno.
654 */
655
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700656 if (regno >= minfo->curr.cmap_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 1;
658
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700659 if (minfo->fbcon.var.grayscale) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /* gray = 0.30*R + 0.59*G + 0.11*B */
661 red = green = blue = (red * 77 + green * 151 + blue * 28) >> 8;
662 }
663
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700664 red = CNVT_TOHW(red, minfo->fbcon.var.red.length);
665 green = CNVT_TOHW(green, minfo->fbcon.var.green.length);
666 blue = CNVT_TOHW(blue, minfo->fbcon.var.blue.length);
667 transp = CNVT_TOHW(transp, minfo->fbcon.var.transp.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700669 switch (minfo->fbcon.var.bits_per_pixel) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 case 4:
671 case 8:
672 mga_outb(M_DAC_REG, regno);
673 mga_outb(M_DAC_VAL, red);
674 mga_outb(M_DAC_VAL, green);
675 mga_outb(M_DAC_VAL, blue);
676 break;
677 case 16:
Antonino A. Daplas08a498d2007-07-17 04:05:45 -0700678 if (regno >= 16)
679 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 {
681 u_int16_t col =
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700682 (red << minfo->fbcon.var.red.offset) |
683 (green << minfo->fbcon.var.green.offset) |
684 (blue << minfo->fbcon.var.blue.offset) |
685 (transp << minfo->fbcon.var.transp.offset); /* for 1:5:5:5 */
686 minfo->cmap[regno] = col | (col << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 break;
689 case 24:
690 case 32:
Antonino A. Daplas08a498d2007-07-17 04:05:45 -0700691 if (regno >= 16)
692 break;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700693 minfo->cmap[regno] =
694 (red << minfo->fbcon.var.red.offset) |
695 (green << minfo->fbcon.var.green.offset) |
696 (blue << minfo->fbcon.var.blue.offset) |
697 (transp << minfo->fbcon.var.transp.offset); /* 8:8:8:8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 break;
699 }
700 return 0;
701}
702
703static void matroxfb_init_fix(WPMINFO2)
704{
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700705 struct fb_fix_screeninfo *fix = &minfo->fbcon.fix;
Harvey Harrison5ae12172008-04-28 02:15:47 -0700706 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 strcpy(fix->id,"MATROX");
709
710 fix->xpanstep = 8; /* 8 for 8bpp, 4 for 16bpp, 2 for 32bpp */
711 fix->ypanstep = 1;
712 fix->ywrapstep = 0;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700713 fix->mmio_start = minfo->mmio.base;
714 fix->mmio_len = minfo->mmio.len;
715 fix->accel = minfo->devflags.accelerator;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718static void matroxfb_update_fix(WPMINFO2)
719{
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700720 struct fb_fix_screeninfo *fix = &minfo->fbcon.fix;
Harvey Harrison5ae12172008-04-28 02:15:47 -0700721 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700723 mutex_lock(&minfo->fbcon.mm_lock);
724 fix->smem_start = minfo->video.base + minfo->curr.ydstorg.bytes;
725 fix->smem_len = minfo->video.len_usable - minfo->curr.ydstorg.bytes;
726 mutex_unlock(&minfo->fbcon.mm_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729static int matroxfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
730{
731 int err;
732 int visual;
733 int cmap_len;
734 unsigned int ydstorg;
735 MINFO_FROM_INFO(info);
736
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700737 if (minfo->dead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -ENXIO;
739 }
740 if ((err = matroxfb_decode_var(PMINFO var, &visual, &cmap_len, &ydstorg)) != 0)
741 return err;
742 return 0;
743}
744
745static int matroxfb_set_par(struct fb_info *info)
746{
747 int err;
748 int visual;
749 int cmap_len;
750 unsigned int ydstorg;
751 struct fb_var_screeninfo *var;
752 MINFO_FROM_INFO(info);
753
Harvey Harrison5ae12172008-04-28 02:15:47 -0700754 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700756 if (minfo->dead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return -ENXIO;
758 }
759
760 var = &info->var;
761 if ((err = matroxfb_decode_var(PMINFO var, &visual, &cmap_len, &ydstorg)) != 0)
762 return err;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700763 minfo->fbcon.screen_base = vaddr_va(minfo->video.vbase) + ydstorg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 matroxfb_update_fix(PMINFO2);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700765 minfo->fbcon.fix.visual = visual;
766 minfo->fbcon.fix.type = FB_TYPE_PACKED_PIXELS;
767 minfo->fbcon.fix.type_aux = 0;
768 minfo->fbcon.fix.line_length = (var->xres_virtual * var->bits_per_pixel) >> 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 {
770 unsigned int pos;
771
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700772 minfo->curr.cmap_len = cmap_len;
773 ydstorg += minfo->devflags.ydstorg;
774 minfo->curr.ydstorg.bytes = ydstorg;
775 minfo->curr.ydstorg.chunks = ydstorg >> (isInterleave(minfo) ? 3 : 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (var->bits_per_pixel == 4)
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700777 minfo->curr.ydstorg.pixels = ydstorg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 else
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700779 minfo->curr.ydstorg.pixels = (ydstorg * 8) / var->bits_per_pixel;
780 minfo->curr.final_bppShift = matroxfb_get_final_bppShift(PMINFO var->bits_per_pixel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 { struct my_timming mt;
782 struct matrox_hw_state* hw;
783 int out;
784
785 matroxfb_var2my(var, &mt);
786 mt.crtc = MATROXFB_SRC_CRTC1;
787 /* CRTC1 delays */
788 switch (var->bits_per_pixel) {
789 case 0: mt.delay = 31 + 0; break;
790 case 16: mt.delay = 21 + 8; break;
791 case 24: mt.delay = 17 + 8; break;
792 case 32: mt.delay = 16 + 8; break;
793 default: mt.delay = 31 + 8; break;
794 }
795
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700796 hw = &minfo->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700798 down_read(&minfo->altout.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700800 if (minfo->outputs[out].src == MATROXFB_SRC_CRTC1 &&
801 minfo->outputs[out].output->compute) {
802 minfo->outputs[out].output->compute(minfo->outputs[out].data, &mt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700805 up_read(&minfo->altout.lock);
806 minfo->crtc1.pixclock = mt.pixclock;
807 minfo->crtc1.mnp = mt.mnp;
808 minfo->hw_switch->init(PMINFO &mt);
809 pos = (var->yoffset * var->xres_virtual + var->xoffset) * minfo->curr.final_bppShift / 32;
810 pos += minfo->curr.ydstorg.chunks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 hw->CRTC[0x0D] = pos & 0xFF;
813 hw->CRTC[0x0C] = (pos & 0xFF00) >> 8;
814 hw->CRTCEXT[0] = (hw->CRTCEXT[0] & 0xF0) | ((pos >> 16) & 0x0F) | ((pos >> 14) & 0x40);
815 hw->CRTCEXT[8] = pos >> 21;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700816 minfo->hw_switch->restore(PMINFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 update_crtc2(PMINFO pos);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700818 down_read(&minfo->altout.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700820 if (minfo->outputs[out].src == MATROXFB_SRC_CRTC1 &&
821 minfo->outputs[out].output->program) {
822 minfo->outputs[out].output->program(minfo->outputs[out].data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824 }
825 for (out = 0; out < MATROXFB_MAX_OUTPUTS; out++) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700826 if (minfo->outputs[out].src == MATROXFB_SRC_CRTC1 &&
827 minfo->outputs[out].output->start) {
828 minfo->outputs[out].output->start(minfo->outputs[out].data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700831 up_read(&minfo->altout.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 matrox_cfbX_init(PMINFO2);
833 }
834 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700835 minfo->initialized = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return 0;
837}
838
839static int matroxfb_get_vblank(WPMINFO struct fb_vblank *vblank)
840{
841 unsigned int sts1;
842
843 matroxfb_enable_irq(PMINFO 0);
844 memset(vblank, 0, sizeof(*vblank));
845 vblank->flags = FB_VBLANK_HAVE_VCOUNT | FB_VBLANK_HAVE_VSYNC |
846 FB_VBLANK_HAVE_VBLANK | FB_VBLANK_HAVE_HBLANK;
847 sts1 = mga_inb(M_INSTS1);
848 vblank->vcount = mga_inl(M_VCOUNT);
849 /* BTW, on my PIII/450 with G400, reading M_INSTS1
850 byte makes this call about 12% slower (1.70 vs. 2.05 us
851 per ioctl()) */
852 if (sts1 & 1)
853 vblank->flags |= FB_VBLANK_HBLANKING;
854 if (sts1 & 8)
855 vblank->flags |= FB_VBLANK_VSYNCING;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700856 if (vblank->vcount >= minfo->fbcon.var.yres)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 vblank->flags |= FB_VBLANK_VBLANKING;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700858 if (test_bit(0, &minfo->irq_flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 vblank->flags |= FB_VBLANK_HAVE_COUNT;
Andrew Mortona50d9132006-06-21 01:57:39 -0300860 /* Only one writer, aligned int value...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 it should work without lock and without atomic_t */
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700862 vblank->count = minfo->crtc1.vsync.cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864 return 0;
865}
866
867static struct matrox_altout panellink_output = {
868 .name = "Panellink output",
869};
870
Christoph Hellwig67a66802006-01-14 13:21:25 -0800871static int matroxfb_ioctl(struct fb_info *info,
872 unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 void __user *argp = (void __user *)arg;
875 MINFO_FROM_INFO(info);
Andrew Mortona50d9132006-06-21 01:57:39 -0300876
Harvey Harrison5ae12172008-04-28 02:15:47 -0700877 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700879 if (minfo->dead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 return -ENXIO;
881 }
882
883 switch (cmd) {
884 case FBIOGET_VBLANK:
885 {
886 struct fb_vblank vblank;
887 int err;
888
889 err = matroxfb_get_vblank(PMINFO &vblank);
890 if (err)
891 return err;
892 if (copy_to_user(argp, &vblank, sizeof(vblank)))
893 return -EFAULT;
894 return 0;
895 }
896 case FBIO_WAITFORVSYNC:
897 {
898 u_int32_t crt;
899
900 if (get_user(crt, (u_int32_t __user *)arg))
901 return -EFAULT;
902
903 return matroxfb_wait_for_sync(PMINFO crt);
904 }
905 case MATROXFB_SET_OUTPUT_MODE:
906 {
907 struct matroxioc_output_mode mom;
908 struct matrox_altout *oproc;
909 int val;
910
911 if (copy_from_user(&mom, argp, sizeof(mom)))
912 return -EFAULT;
913 if (mom.output >= MATROXFB_MAX_OUTPUTS)
914 return -ENXIO;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700915 down_read(&minfo->altout.lock);
916 oproc = minfo->outputs[mom.output].output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (!oproc) {
918 val = -ENXIO;
919 } else if (!oproc->verifymode) {
920 if (mom.mode == MATROXFB_OUTPUT_MODE_MONITOR) {
921 val = 0;
922 } else {
923 val = -EINVAL;
924 }
925 } else {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700926 val = oproc->verifymode(minfo->outputs[mom.output].data, mom.mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 if (!val) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700929 if (minfo->outputs[mom.output].mode != mom.mode) {
930 minfo->outputs[mom.output].mode = mom.mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 val = 1;
932 }
933 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700934 up_read(&minfo->altout.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (val != 1)
936 return val;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700937 switch (minfo->outputs[mom.output].src) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 case MATROXFB_SRC_CRTC1:
939 matroxfb_set_par(info);
940 break;
941 case MATROXFB_SRC_CRTC2:
942 {
943 struct matroxfb_dh_fb_info* crtc2;
944
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700945 down_read(&minfo->crtc2.lock);
946 crtc2 = minfo->crtc2.info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (crtc2)
948 crtc2->fbcon.fbops->fb_set_par(&crtc2->fbcon);
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700949 up_read(&minfo->crtc2.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951 break;
952 }
953 return 0;
954 }
955 case MATROXFB_GET_OUTPUT_MODE:
956 {
957 struct matroxioc_output_mode mom;
958 struct matrox_altout *oproc;
959 int val;
960
961 if (copy_from_user(&mom, argp, sizeof(mom)))
962 return -EFAULT;
963 if (mom.output >= MATROXFB_MAX_OUTPUTS)
964 return -ENXIO;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700965 down_read(&minfo->altout.lock);
966 oproc = minfo->outputs[mom.output].output;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!oproc) {
968 val = -ENXIO;
969 } else {
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700970 mom.mode = minfo->outputs[mom.output].mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 val = 0;
972 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700973 up_read(&minfo->altout.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (val)
975 return val;
976 if (copy_to_user(argp, &mom, sizeof(mom)))
977 return -EFAULT;
978 return 0;
979 }
980 case MATROXFB_SET_OUTPUT_CONNECTION:
981 {
982 u_int32_t tmp;
983 int i;
984 int changes;
985
986 if (copy_from_user(&tmp, argp, sizeof(tmp)))
987 return -EFAULT;
988 for (i = 0; i < 32; i++) {
989 if (tmp & (1 << i)) {
990 if (i >= MATROXFB_MAX_OUTPUTS)
991 return -ENXIO;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700992 if (!minfo->outputs[i].output)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 return -ENXIO;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700994 switch (minfo->outputs[i].src) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 case MATROXFB_SRC_NONE:
996 case MATROXFB_SRC_CRTC1:
997 break;
998 default:
999 return -EBUSY;
1000 }
1001 }
1002 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001003 if (minfo->devflags.panellink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (tmp & MATROXFB_OUTPUT_CONN_DFP) {
1005 if (tmp & MATROXFB_OUTPUT_CONN_SECONDARY)
1006 return -EINVAL;
1007 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001008 if (minfo->outputs[i].src == MATROXFB_SRC_CRTC2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return -EBUSY;
1010 }
1011 }
1012 }
1013 }
1014 changes = 0;
1015 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
1016 if (tmp & (1 << i)) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001017 if (minfo->outputs[i].src != MATROXFB_SRC_CRTC1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 changes = 1;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001019 minfo->outputs[i].src = MATROXFB_SRC_CRTC1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001021 } else if (minfo->outputs[i].src == MATROXFB_SRC_CRTC1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 changes = 1;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001023 minfo->outputs[i].src = MATROXFB_SRC_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 }
1026 if (!changes)
1027 return 0;
1028 matroxfb_set_par(info);
1029 return 0;
1030 }
1031 case MATROXFB_GET_OUTPUT_CONNECTION:
1032 {
1033 u_int32_t conn = 0;
1034 int i;
1035
1036 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001037 if (minfo->outputs[i].src == MATROXFB_SRC_CRTC1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 conn |= 1 << i;
1039 }
1040 }
1041 if (put_user(conn, (u_int32_t __user *)arg))
1042 return -EFAULT;
1043 return 0;
1044 }
1045 case MATROXFB_GET_AVAILABLE_OUTPUTS:
1046 {
1047 u_int32_t conn = 0;
1048 int i;
1049
1050 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001051 if (minfo->outputs[i].output) {
1052 switch (minfo->outputs[i].src) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 case MATROXFB_SRC_NONE:
1054 case MATROXFB_SRC_CRTC1:
1055 conn |= 1 << i;
1056 break;
1057 }
1058 }
1059 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001060 if (minfo->devflags.panellink) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (conn & MATROXFB_OUTPUT_CONN_DFP)
1062 conn &= ~MATROXFB_OUTPUT_CONN_SECONDARY;
1063 if (conn & MATROXFB_OUTPUT_CONN_SECONDARY)
1064 conn &= ~MATROXFB_OUTPUT_CONN_DFP;
1065 }
1066 if (put_user(conn, (u_int32_t __user *)arg))
1067 return -EFAULT;
1068 return 0;
1069 }
1070 case MATROXFB_GET_ALL_OUTPUTS:
1071 {
1072 u_int32_t conn = 0;
1073 int i;
1074
1075 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001076 if (minfo->outputs[i].output) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 conn |= 1 << i;
1078 }
1079 }
1080 if (put_user(conn, (u_int32_t __user *)arg))
1081 return -EFAULT;
1082 return 0;
1083 }
1084 case VIDIOC_QUERYCAP:
1085 {
1086 struct v4l2_capability r;
Andrew Mortona50d9132006-06-21 01:57:39 -03001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 memset(&r, 0, sizeof(r));
1089 strcpy(r.driver, "matroxfb");
1090 strcpy(r.card, "Matrox");
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001091 sprintf(r.bus_info, "PCI:%s", pci_name(minfo->pcidev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 r.version = KERNEL_VERSION(1,0,0);
1093 r.capabilities = V4L2_CAP_VIDEO_OUTPUT;
1094 if (copy_to_user(argp, &r, sizeof(r)))
1095 return -EFAULT;
1096 return 0;
Andrew Mortona50d9132006-06-21 01:57:39 -03001097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099 case VIDIOC_QUERYCTRL:
1100 {
1101 struct v4l2_queryctrl qctrl;
1102 int err;
1103
1104 if (copy_from_user(&qctrl, argp, sizeof(qctrl)))
1105 return -EFAULT;
1106
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001107 down_read(&minfo->altout.lock);
1108 if (!minfo->outputs[1].output) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 err = -ENXIO;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001110 } else if (minfo->outputs[1].output->getqueryctrl) {
1111 err = minfo->outputs[1].output->getqueryctrl(minfo->outputs[1].data, &qctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 } else {
1113 err = -EINVAL;
1114 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001115 up_read(&minfo->altout.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 if (err >= 0 &&
1117 copy_to_user(argp, &qctrl, sizeof(qctrl)))
1118 return -EFAULT;
1119 return err;
1120 }
1121 case VIDIOC_G_CTRL:
1122 {
1123 struct v4l2_control ctrl;
1124 int err;
1125
1126 if (copy_from_user(&ctrl, argp, sizeof(ctrl)))
1127 return -EFAULT;
1128
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001129 down_read(&minfo->altout.lock);
1130 if (!minfo->outputs[1].output) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 err = -ENXIO;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001132 } else if (minfo->outputs[1].output->getctrl) {
1133 err = minfo->outputs[1].output->getctrl(minfo->outputs[1].data, &ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 } else {
1135 err = -EINVAL;
1136 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001137 up_read(&minfo->altout.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (err >= 0 &&
1139 copy_to_user(argp, &ctrl, sizeof(ctrl)))
1140 return -EFAULT;
1141 return err;
1142 }
1143 case VIDIOC_S_CTRL_OLD:
1144 case VIDIOC_S_CTRL:
1145 {
1146 struct v4l2_control ctrl;
1147 int err;
1148
1149 if (copy_from_user(&ctrl, argp, sizeof(ctrl)))
1150 return -EFAULT;
1151
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001152 down_read(&minfo->altout.lock);
1153 if (!minfo->outputs[1].output) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 err = -ENXIO;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001155 } else if (minfo->outputs[1].output->setctrl) {
1156 err = minfo->outputs[1].output->setctrl(minfo->outputs[1].data, &ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 } else {
1158 err = -EINVAL;
1159 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001160 up_read(&minfo->altout.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return err;
1162 }
1163 }
1164 return -ENOTTY;
1165}
1166
1167/* 0 unblank, 1 blank, 2 no vsync, 3 no hsync, 4 off */
1168
1169static int matroxfb_blank(int blank, struct fb_info *info)
1170{
1171 int seq;
1172 int crtc;
1173 CRITFLAGS
1174 MINFO_FROM_INFO(info);
1175
Harvey Harrison5ae12172008-04-28 02:15:47 -07001176 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001178 if (minfo->dead)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return 1;
1180
1181 switch (blank) {
1182 case FB_BLANK_NORMAL: seq = 0x20; crtc = 0x00; break; /* works ??? */
1183 case FB_BLANK_VSYNC_SUSPEND: seq = 0x20; crtc = 0x10; break;
1184 case FB_BLANK_HSYNC_SUSPEND: seq = 0x20; crtc = 0x20; break;
1185 case FB_BLANK_POWERDOWN: seq = 0x20; crtc = 0x30; break;
1186 default: seq = 0x00; crtc = 0x00; break;
1187 }
1188
1189 CRITBEGIN
1190
1191 mga_outb(M_SEQ_INDEX, 1);
1192 mga_outb(M_SEQ_DATA, (mga_inb(M_SEQ_DATA) & ~0x20) | seq);
1193 mga_outb(M_EXTVGA_INDEX, 1);
1194 mga_outb(M_EXTVGA_DATA, (mga_inb(M_EXTVGA_DATA) & ~0x30) | crtc);
1195
1196 CRITEND
1197 return 0;
1198}
1199
1200static struct fb_ops matroxfb_ops = {
1201 .owner = THIS_MODULE,
1202 .fb_open = matroxfb_open,
1203 .fb_release = matroxfb_release,
1204 .fb_check_var = matroxfb_check_var,
1205 .fb_set_par = matroxfb_set_par,
1206 .fb_setcolreg = matroxfb_setcolreg,
1207 .fb_pan_display =matroxfb_pan_display,
1208 .fb_blank = matroxfb_blank,
1209 .fb_ioctl = matroxfb_ioctl,
1210/* .fb_fillrect = <set by matrox_cfbX_init>, */
1211/* .fb_copyarea = <set by matrox_cfbX_init>, */
1212/* .fb_imageblit = <set by matrox_cfbX_init>, */
1213/* .fb_cursor = <set by matrox_cfbX_init>, */
1214};
1215
1216#define RSDepth(X) (((X) >> 8) & 0x0F)
1217#define RS8bpp 0x1
1218#define RS15bpp 0x2
1219#define RS16bpp 0x3
1220#define RS32bpp 0x4
1221#define RS4bpp 0x5
1222#define RS24bpp 0x6
1223#define RSText 0x7
1224#define RSText8 0x8
1225/* 9-F */
1226static struct { struct fb_bitfield red, green, blue, transp; int bits_per_pixel; } colors[] = {
1227 { { 0, 8, 0}, { 0, 8, 0}, { 0, 8, 0}, { 0, 0, 0}, 8 },
1228 { { 10, 5, 0}, { 5, 5, 0}, { 0, 5, 0}, { 15, 1, 0}, 16 },
1229 { { 11, 5, 0}, { 5, 6, 0}, { 0, 5, 0}, { 0, 0, 0}, 16 },
1230 { { 16, 8, 0}, { 8, 8, 0}, { 0, 8, 0}, { 24, 8, 0}, 32 },
1231 { { 0, 8, 0}, { 0, 8, 0}, { 0, 8, 0}, { 0, 0, 0}, 4 },
1232 { { 16, 8, 0}, { 8, 8, 0}, { 0, 8, 0}, { 0, 0, 0}, 24 },
1233 { { 0, 6, 0}, { 0, 6, 0}, { 0, 6, 0}, { 0, 0, 0}, 0 }, /* textmode with (default) VGA8x16 */
1234 { { 0, 6, 0}, { 0, 6, 0}, { 0, 6, 0}, { 0, 0, 0}, 0 }, /* textmode hardwired to VGA8x8 */
1235};
1236
1237/* initialized by setup, see explanation at end of file (search for MODULE_PARM_DESC) */
1238static unsigned int mem; /* "matrox:mem:xxxxxM" */
1239static int option_precise_width = 1; /* cannot be changed, option_precise_width==0 must imply noaccel */
1240static int inv24; /* "matrox:inv24" */
1241static int cross4MB = -1; /* "matrox:cross4MB" */
1242static int disabled; /* "matrox:disabled" */
1243static int noaccel; /* "matrox:noaccel" */
1244static int nopan; /* "matrox:nopan" */
1245static int no_pci_retry; /* "matrox:nopciretry" */
1246static int novga; /* "matrox:novga" */
1247static int nobios; /* "matrox:nobios" */
1248static int noinit = 1; /* "matrox:init" */
1249static int inverse; /* "matrox:inverse" */
1250static int sgram; /* "matrox:sgram" */
1251#ifdef CONFIG_MTRR
1252static int mtrr = 1; /* "matrox:nomtrr" */
1253#endif
1254static int grayscale; /* "matrox:grayscale" */
1255static int dev = -1; /* "matrox:dev:xxxxx" */
1256static unsigned int vesa = ~0; /* "matrox:vesa:xxxxx" */
1257static int depth = -1; /* "matrox:depth:xxxxx" */
1258static unsigned int xres; /* "matrox:xres:xxxxx" */
1259static unsigned int yres; /* "matrox:yres:xxxxx" */
1260static unsigned int upper = ~0; /* "matrox:upper:xxxxx" */
1261static unsigned int lower = ~0; /* "matrox:lower:xxxxx" */
1262static unsigned int vslen; /* "matrox:vslen:xxxxx" */
1263static unsigned int left = ~0; /* "matrox:left:xxxxx" */
1264static unsigned int right = ~0; /* "matrox:right:xxxxx" */
1265static unsigned int hslen; /* "matrox:hslen:xxxxx" */
1266static unsigned int pixclock; /* "matrox:pixclock:xxxxx" */
1267static int sync = -1; /* "matrox:sync:xxxxx" */
1268static unsigned int fv; /* "matrox:fv:xxxxx" */
1269static unsigned int fh; /* "matrox:fh:xxxxxk" */
1270static unsigned int maxclk; /* "matrox:maxclk:xxxxM" */
1271static int dfp; /* "matrox:dfp */
1272static int dfp_type = -1; /* "matrox:dfp:xxx */
1273static int memtype = -1; /* "matrox:memtype:xxx" */
1274static char outputs[8]; /* "matrox:outputs:xxx" */
1275
1276#ifndef MODULE
1277static char videomode[64]; /* "matrox:mode:xxxxx" or "matrox:xxxxx" */
1278#endif
1279
1280static int matroxfb_getmemory(WPMINFO unsigned int maxSize, unsigned int *realSize){
1281 vaddr_t vm;
1282 unsigned int offs;
1283 unsigned int offs2;
Ville Syrjälä6c12f302005-11-07 01:00:58 -08001284 unsigned char orig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 unsigned char bytes[32];
1286 unsigned char* tmp;
1287
Harvey Harrison5ae12172008-04-28 02:15:47 -07001288 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001290 vm = minfo->video.vbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 maxSize &= ~0x1FFFFF; /* must be X*2MB (really it must be 2 or X*4MB) */
1292 /* at least 2MB */
1293 if (maxSize < 0x0200000) return 0;
1294 if (maxSize > 0x2000000) maxSize = 0x2000000;
1295
1296 mga_outb(M_EXTVGA_INDEX, 0x03);
Jan Beulich438e5c52005-09-13 01:25:45 -07001297 orig = mga_inb(M_EXTVGA_DATA);
1298 mga_outb(M_EXTVGA_DATA, orig | 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 tmp = bytes;
1301 for (offs = 0x100000; offs < maxSize; offs += 0x200000)
1302 *tmp++ = mga_readb(vm, offs);
1303 for (offs = 0x100000; offs < maxSize; offs += 0x200000)
1304 mga_writeb(vm, offs, 0x02);
Ville Syrjälä6c12f302005-11-07 01:00:58 -08001305 mga_outb(M_CACHEFLUSH, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 for (offs = 0x100000; offs < maxSize; offs += 0x200000) {
1307 if (mga_readb(vm, offs) != 0x02)
1308 break;
1309 mga_writeb(vm, offs, mga_readb(vm, offs) - 0x02);
1310 if (mga_readb(vm, offs))
1311 break;
1312 }
1313 tmp = bytes;
1314 for (offs2 = 0x100000; offs2 < maxSize; offs2 += 0x200000)
1315 mga_writeb(vm, offs2, *tmp++);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 mga_outb(M_EXTVGA_INDEX, 0x03);
Jan Beulich438e5c52005-09-13 01:25:45 -07001318 mga_outb(M_EXTVGA_DATA, orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 *realSize = offs - 0x100000;
1321#ifdef CONFIG_FB_MATROX_MILLENIUM
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001322 minfo->interleave = !(!isMillenium(minfo) || ((offs - 0x100000) & 0x3FFFFF));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323#endif
1324 return 1;
1325}
1326
1327struct video_board {
1328 int maxvram;
1329 int maxdisplayable;
1330 int accelID;
1331 struct matrox_switch* lowlevel;
1332 };
1333#ifdef CONFIG_FB_MATROX_MILLENIUM
1334static struct video_board vbMillennium = {0x0800000, 0x0800000, FB_ACCEL_MATROX_MGA2064W, &matrox_millennium};
1335static struct video_board vbMillennium2 = {0x1000000, 0x0800000, FB_ACCEL_MATROX_MGA2164W, &matrox_millennium};
1336static struct video_board vbMillennium2A = {0x1000000, 0x0800000, FB_ACCEL_MATROX_MGA2164W_AGP, &matrox_millennium};
1337#endif /* CONFIG_FB_MATROX_MILLENIUM */
1338#ifdef CONFIG_FB_MATROX_MYSTIQUE
1339static struct video_board vbMystique = {0x0800000, 0x0800000, FB_ACCEL_MATROX_MGA1064SG, &matrox_mystique};
1340#endif /* CONFIG_FB_MATROX_MYSTIQUE */
1341#ifdef CONFIG_FB_MATROX_G
1342static struct video_board vbG100 = {0x0800000, 0x0800000, FB_ACCEL_MATROX_MGAG100, &matrox_G100};
1343static struct video_board vbG200 = {0x1000000, 0x1000000, FB_ACCEL_MATROX_MGAG200, &matrox_G100};
1344#ifdef CONFIG_FB_MATROX_32MB
1345/* from doc it looks like that accelerator can draw only to low 16MB :-( Direct accesses & displaying are OK for
1346 whole 32MB */
1347static struct video_board vbG400 = {0x2000000, 0x1000000, FB_ACCEL_MATROX_MGAG400, &matrox_G100};
1348#else
1349static struct video_board vbG400 = {0x2000000, 0x1000000, FB_ACCEL_MATROX_MGAG400, &matrox_G100};
1350#endif
1351#endif
1352
1353#define DEVF_VIDEO64BIT 0x0001
1354#define DEVF_SWAPS 0x0002
1355#define DEVF_SRCORG 0x0004
1356#define DEVF_DUALHEAD 0x0008
1357#define DEVF_CROSS4MB 0x0010
1358#define DEVF_TEXT4B 0x0020
1359/* #define DEVF_recycled 0x0040 */
1360/* #define DEVF_recycled 0x0080 */
1361#define DEVF_SUPPORT32MB 0x0100
1362#define DEVF_ANY_VXRES 0x0200
1363#define DEVF_TEXT16B 0x0400
1364#define DEVF_CRTC2 0x0800
1365#define DEVF_MAVEN_CAPABLE 0x1000
1366#define DEVF_PANELLINK_CAPABLE 0x2000
1367#define DEVF_G450DAC 0x4000
1368
1369#define DEVF_GCORE (DEVF_VIDEO64BIT | DEVF_SWAPS | DEVF_CROSS4MB)
1370#define DEVF_G2CORE (DEVF_GCORE | DEVF_ANY_VXRES | DEVF_MAVEN_CAPABLE | DEVF_PANELLINK_CAPABLE | DEVF_SRCORG | DEVF_DUALHEAD)
1371#define DEVF_G100 (DEVF_GCORE) /* no doc, no vxres... */
1372#define DEVF_G200 (DEVF_G2CORE)
1373#define DEVF_G400 (DEVF_G2CORE | DEVF_SUPPORT32MB | DEVF_TEXT16B | DEVF_CRTC2)
1374/* if you'll find how to drive DFP... */
1375#define DEVF_G450 (DEVF_GCORE | DEVF_ANY_VXRES | DEVF_SUPPORT32MB | DEVF_TEXT16B | DEVF_CRTC2 | DEVF_G450DAC | DEVF_SRCORG | DEVF_DUALHEAD)
1376#define DEVF_G550 (DEVF_G450)
1377
1378static struct board {
1379 unsigned short vendor, device, rev, svid, sid;
1380 unsigned int flags;
1381 unsigned int maxclk;
1382 enum mga_chip chip;
1383 struct video_board* base;
1384 const char* name;
1385 } dev_list[] = {
1386#ifdef CONFIG_FB_MATROX_MILLENIUM
1387 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL, 0xFF,
1388 0, 0,
1389 DEVF_TEXT4B,
1390 230000,
1391 MGA_2064,
1392 &vbMillennium,
1393 "Millennium (PCI)"},
1394 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL_2, 0xFF,
1395 0, 0,
1396 DEVF_SWAPS,
1397 220000,
1398 MGA_2164,
1399 &vbMillennium2,
1400 "Millennium II (PCI)"},
1401 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL_2_AGP, 0xFF,
1402 0, 0,
1403 DEVF_SWAPS,
1404 250000,
1405 MGA_2164,
1406 &vbMillennium2A,
1407 "Millennium II (AGP)"},
1408#endif
1409#ifdef CONFIG_FB_MATROX_MYSTIQUE
1410 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MYS, 0x02,
1411 0, 0,
1412 DEVF_VIDEO64BIT | DEVF_CROSS4MB,
1413 180000,
1414 MGA_1064,
1415 &vbMystique,
1416 "Mystique (PCI)"},
1417 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MYS, 0xFF,
1418 0, 0,
1419 DEVF_VIDEO64BIT | DEVF_SWAPS | DEVF_CROSS4MB,
1420 220000,
1421 MGA_1164,
1422 &vbMystique,
1423 "Mystique 220 (PCI)"},
Ville Syrjälä63921fb2005-11-07 01:00:57 -08001424 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MYS_AGP, 0x02,
1425 0, 0,
1426 DEVF_VIDEO64BIT | DEVF_CROSS4MB,
1427 180000,
1428 MGA_1064,
1429 &vbMystique,
1430 "Mystique (AGP)"},
1431 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MYS_AGP, 0xFF,
1432 0, 0,
1433 DEVF_VIDEO64BIT | DEVF_SWAPS | DEVF_CROSS4MB,
1434 220000,
1435 MGA_1164,
1436 &vbMystique,
1437 "Mystique 220 (AGP)"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438#endif
1439#ifdef CONFIG_FB_MATROX_G
1440 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G100_MM, 0xFF,
1441 0, 0,
1442 DEVF_G100,
1443 230000,
1444 MGA_G100,
1445 &vbG100,
1446 "MGA-G100 (PCI)"},
1447 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G100_AGP, 0xFF,
1448 0, 0,
1449 DEVF_G100,
1450 230000,
1451 MGA_G100,
1452 &vbG100,
1453 "MGA-G100 (AGP)"},
Darrick J. Wonge3a19382008-10-15 22:03:52 -07001454 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200EV_PCI, 0xFF,
1455 0, 0,
1456 DEVF_G200,
1457 230000,
1458 MGA_G200,
1459 &vbG200,
1460 "MGA-G200eV (PCI)"},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_PCI, 0xFF,
1462 0, 0,
1463 DEVF_G200,
1464 250000,
1465 MGA_G200,
1466 &vbG200,
1467 "MGA-G200 (PCI)"},
1468 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP, 0xFF,
1469 PCI_SS_VENDOR_ID_MATROX, PCI_SS_ID_MATROX_GENERIC,
1470 DEVF_G200,
1471 220000,
1472 MGA_G200,
1473 &vbG200,
1474 "MGA-G200 (AGP)"},
1475 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP, 0xFF,
1476 PCI_SS_VENDOR_ID_MATROX, PCI_SS_ID_MATROX_MYSTIQUE_G200_AGP,
1477 DEVF_G200,
1478 230000,
1479 MGA_G200,
1480 &vbG200,
1481 "Mystique G200 (AGP)"},
1482 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP, 0xFF,
1483 PCI_SS_VENDOR_ID_MATROX, PCI_SS_ID_MATROX_MILLENIUM_G200_AGP,
1484 DEVF_G200,
1485 250000,
1486 MGA_G200,
1487 &vbG200,
1488 "Millennium G200 (AGP)"},
1489 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP, 0xFF,
1490 PCI_SS_VENDOR_ID_MATROX, PCI_SS_ID_MATROX_MARVEL_G200_AGP,
1491 DEVF_G200,
1492 230000,
1493 MGA_G200,
1494 &vbG200,
1495 "Marvel G200 (AGP)"},
1496 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP, 0xFF,
1497 PCI_SS_VENDOR_ID_SIEMENS_NIXDORF, PCI_SS_ID_SIEMENS_MGA_G200_AGP,
1498 DEVF_G200,
1499 230000,
1500 MGA_G200,
1501 &vbG200,
1502 "MGA-G200 (AGP)"},
1503 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP, 0xFF,
1504 0, 0,
1505 DEVF_G200,
1506 230000,
1507 MGA_G200,
1508 &vbG200,
1509 "G200 (AGP)"},
1510 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400, 0x80,
1511 PCI_SS_VENDOR_ID_MATROX, PCI_SS_ID_MATROX_MILLENNIUM_G400_MAX_AGP,
1512 DEVF_G400,
1513 360000,
1514 MGA_G400,
1515 &vbG400,
1516 "Millennium G400 MAX (AGP)"},
1517 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400, 0x80,
1518 0, 0,
1519 DEVF_G400,
1520 300000,
1521 MGA_G400,
1522 &vbG400,
1523 "G400 (AGP)"},
1524 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400, 0xFF,
1525 0, 0,
1526 DEVF_G450,
1527 360000,
1528 MGA_G450,
1529 &vbG400,
1530 "G450"},
1531 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G550, 0xFF,
1532 0, 0,
1533 DEVF_G550,
1534 360000,
1535 MGA_G550,
1536 &vbG400,
1537 "G550"},
1538#endif
1539 {0, 0, 0xFF,
1540 0, 0,
1541 0,
1542 0,
1543 0,
1544 NULL,
1545 NULL}};
1546
1547#ifndef MODULE
1548static struct fb_videomode defaultmode = {
1549 /* 640x480 @ 60Hz, 31.5 kHz */
1550 NULL, 60, 640, 480, 39721, 40, 24, 32, 11, 96, 2,
1551 0, FB_VMODE_NONINTERLACED
1552};
1553#endif /* !MODULE */
1554
1555static int hotplug = 0;
1556
1557static void setDefaultOutputs(WPMINFO2) {
1558 unsigned int i;
1559 const char* ptr;
1560
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001561 minfo->outputs[0].default_src = MATROXFB_SRC_CRTC1;
1562 if (minfo->devflags.g450dac) {
1563 minfo->outputs[1].default_src = MATROXFB_SRC_CRTC1;
1564 minfo->outputs[2].default_src = MATROXFB_SRC_CRTC1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 } else if (dfp) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001566 minfo->outputs[2].default_src = MATROXFB_SRC_CRTC1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568 ptr = outputs;
1569 for (i = 0; i < MATROXFB_MAX_OUTPUTS; i++) {
1570 char c = *ptr++;
1571
1572 if (c == 0) {
1573 break;
1574 }
1575 if (c == '0') {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001576 minfo->outputs[i].default_src = MATROXFB_SRC_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 } else if (c == '1') {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001578 minfo->outputs[i].default_src = MATROXFB_SRC_CRTC1;
1579 } else if (c == '2' && minfo->devflags.crtc2) {
1580 minfo->outputs[i].default_src = MATROXFB_SRC_CRTC2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 } else {
1582 printk(KERN_ERR "matroxfb: Unknown outputs setting\n");
1583 break;
1584 }
1585 }
1586 /* Nullify this option for subsequent adapters */
1587 outputs[0] = 0;
1588}
1589
1590static int initMatrox2(WPMINFO struct board* b){
1591 unsigned long ctrlptr_phys = 0;
1592 unsigned long video_base_phys = 0;
1593 unsigned int memsize;
1594 int err;
1595
1596 static struct pci_device_id intel_82437[] = {
1597 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82437) },
1598 { },
1599 };
1600
Harvey Harrison5ae12172008-04-28 02:15:47 -07001601 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /* set default values... */
1604 vesafb_defined.accel_flags = FB_ACCELF_TEXT;
1605
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001606 minfo->hw_switch = b->base->lowlevel;
1607 minfo->devflags.accelerator = b->base->accelID;
1608 minfo->max_pixel_clock = b->maxclk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 printk(KERN_INFO "matroxfb: Matrox %s detected\n", b->name);
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001611 minfo->capable.plnwt = 1;
1612 minfo->chip = b->chip;
1613 minfo->capable.srcorg = b->flags & DEVF_SRCORG;
1614 minfo->devflags.video64bits = b->flags & DEVF_VIDEO64BIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (b->flags & DEVF_TEXT4B) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001616 minfo->devflags.vgastep = 4;
1617 minfo->devflags.textmode = 4;
1618 minfo->devflags.text_type_aux = FB_AUX_TEXT_MGA_STEP16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 } else if (b->flags & DEVF_TEXT16B) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001620 minfo->devflags.vgastep = 16;
1621 minfo->devflags.textmode = 1;
1622 minfo->devflags.text_type_aux = FB_AUX_TEXT_MGA_STEP16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 } else {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001624 minfo->devflags.vgastep = 8;
1625 minfo->devflags.textmode = 1;
1626 minfo->devflags.text_type_aux = FB_AUX_TEXT_MGA_STEP8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628#ifdef CONFIG_FB_MATROX_32MB
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001629 minfo->devflags.support32MB = (b->flags & DEVF_SUPPORT32MB) != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630#endif
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001631 minfo->devflags.precise_width = !(b->flags & DEVF_ANY_VXRES);
1632 minfo->devflags.crtc2 = (b->flags & DEVF_CRTC2) != 0;
1633 minfo->devflags.maven_capable = (b->flags & DEVF_MAVEN_CAPABLE) != 0;
1634 minfo->devflags.dualhead = (b->flags & DEVF_DUALHEAD) != 0;
1635 minfo->devflags.dfp_type = dfp_type;
1636 minfo->devflags.g450dac = (b->flags & DEVF_G450DAC) != 0;
1637 minfo->devflags.textstep = minfo->devflags.vgastep * minfo->devflags.textmode;
1638 minfo->devflags.textvram = 65536 / minfo->devflags.textmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 setDefaultOutputs(PMINFO2);
1640 if (b->flags & DEVF_PANELLINK_CAPABLE) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001641 minfo->outputs[2].data = minfo;
1642 minfo->outputs[2].output = &panellink_output;
1643 minfo->outputs[2].src = minfo->outputs[2].default_src;
1644 minfo->outputs[2].mode = MATROXFB_OUTPUT_MODE_MONITOR;
1645 minfo->devflags.panellink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001648 if (minfo->capable.cross4MB < 0)
1649 minfo->capable.cross4MB = b->flags & DEVF_CROSS4MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (b->flags & DEVF_SWAPS) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001651 ctrlptr_phys = pci_resource_start(minfo->pcidev, 1);
1652 video_base_phys = pci_resource_start(minfo->pcidev, 0);
1653 minfo->devflags.fbResource = PCI_BASE_ADDRESS_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 } else {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001655 ctrlptr_phys = pci_resource_start(minfo->pcidev, 0);
1656 video_base_phys = pci_resource_start(minfo->pcidev, 1);
1657 minfo->devflags.fbResource = PCI_BASE_ADDRESS_1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
1659 err = -EINVAL;
1660 if (!ctrlptr_phys) {
1661 printk(KERN_ERR "matroxfb: control registers are not available, matroxfb disabled\n");
1662 goto fail;
1663 }
1664 if (!video_base_phys) {
1665 printk(KERN_ERR "matroxfb: video RAM is not available in PCI address space, matroxfb disabled\n");
1666 goto fail;
1667 }
1668 memsize = b->base->maxvram;
1669 if (!request_mem_region(ctrlptr_phys, 16384, "matroxfb MMIO")) {
1670 goto fail;
1671 }
1672 if (!request_mem_region(video_base_phys, memsize, "matroxfb FB")) {
1673 goto failCtrlMR;
1674 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001675 minfo->video.len_maximum = memsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /* convert mem (autodetect k, M) */
1677 if (mem < 1024) mem *= 1024;
1678 if (mem < 0x00100000) mem *= 1024;
1679
1680 if (mem && (mem < memsize))
1681 memsize = mem;
1682 err = -ENOMEM;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001683 if (mga_ioremap(ctrlptr_phys, 16384, MGA_IOREMAP_MMIO, &minfo->mmio.vbase)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 printk(KERN_ERR "matroxfb: cannot ioremap(%lX, 16384), matroxfb disabled\n", ctrlptr_phys);
1685 goto failVideoMR;
1686 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001687 minfo->mmio.base = ctrlptr_phys;
1688 minfo->mmio.len = 16384;
1689 minfo->video.base = video_base_phys;
1690 if (mga_ioremap(video_base_phys, memsize, MGA_IOREMAP_FB, &minfo->video.vbase)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 printk(KERN_ERR "matroxfb: cannot ioremap(%lX, %d), matroxfb disabled\n",
1692 video_base_phys, memsize);
1693 goto failCtrlIO;
1694 }
1695 {
1696 u_int32_t cmd;
1697 u_int32_t mga_option;
1698
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001699 pci_read_config_dword(minfo->pcidev, PCI_OPTION_REG, &mga_option);
1700 pci_read_config_dword(minfo->pcidev, PCI_COMMAND, &cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 mga_option &= 0x7FFFFFFF; /* clear BIG_ENDIAN */
1702 mga_option |= MX_OPTION_BSWAP;
Andrew Mortona50d9132006-06-21 01:57:39 -03001703 /* disable palette snooping */
1704 cmd &= ~PCI_COMMAND_VGA_PALETTE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 if (pci_dev_present(intel_82437)) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001706 if (!(mga_option & 0x20000000) && !minfo->devflags.nopciretry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 printk(KERN_WARNING "matroxfb: Disabling PCI retries due to i82437 present\n");
1708 }
1709 mga_option |= 0x20000000;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001710 minfo->devflags.nopciretry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001712 pci_write_config_dword(minfo->pcidev, PCI_COMMAND, cmd);
1713 pci_write_config_dword(minfo->pcidev, PCI_OPTION_REG, mga_option);
1714 minfo->hw.MXoptionReg = mga_option;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
1716 /* select non-DMA memory for PCI_MGA_DATA, otherwise dump of PCI cfg space can lock PCI bus */
1717 /* maybe preinit() candidate, but it is same... for all devices... at this time... */
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001718 pci_write_config_dword(minfo->pcidev, PCI_MGA_INDEX, 0x00003C00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720
1721 err = -ENXIO;
1722 matroxfb_read_pins(PMINFO2);
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001723 if (minfo->hw_switch->preinit(PMINFO2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 goto failVideoIO;
1725 }
1726
1727 err = -ENOMEM;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001728 if (!matroxfb_getmemory(PMINFO memsize, &minfo->video.len) || !minfo->video.len) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 printk(KERN_ERR "matroxfb: cannot determine memory size\n");
1730 goto failVideoIO;
1731 }
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001732 minfo->devflags.ydstorg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001734 minfo->video.base = video_base_phys;
1735 minfo->video.len_usable = minfo->video.len;
1736 if (minfo->video.len_usable > b->base->maxdisplayable)
1737 minfo->video.len_usable = b->base->maxdisplayable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738#ifdef CONFIG_MTRR
1739 if (mtrr) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001740 minfo->mtrr.vram = mtrr_add(video_base_phys, minfo->video.len, MTRR_TYPE_WRCOMB, 1);
1741 minfo->mtrr.vram_valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 printk(KERN_INFO "matroxfb: MTRR's turned on\n");
1743 }
1744#endif /* CONFIG_MTRR */
1745
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001746 if (!minfo->devflags.novga)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 request_region(0x3C0, 32, "matrox");
1748 matroxfb_g450_connect(PMINFO2);
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001749 minfo->hw_switch->reset(PMINFO2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001751 minfo->fbcon.monspecs.hfmin = 0;
1752 minfo->fbcon.monspecs.hfmax = fh;
1753 minfo->fbcon.monspecs.vfmin = 0;
1754 minfo->fbcon.monspecs.vfmax = fv;
1755 minfo->fbcon.monspecs.dpms = 0; /* TBD */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 /* static settings */
1758 vesafb_defined.red = colors[depth-1].red;
1759 vesafb_defined.green = colors[depth-1].green;
1760 vesafb_defined.blue = colors[depth-1].blue;
1761 vesafb_defined.bits_per_pixel = colors[depth-1].bits_per_pixel;
1762 vesafb_defined.grayscale = grayscale;
1763 vesafb_defined.vmode = 0;
1764 if (noaccel)
1765 vesafb_defined.accel_flags &= ~FB_ACCELF_TEXT;
1766
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001767 minfo->fbops = matroxfb_ops;
1768 minfo->fbcon.fbops = &minfo->fbops;
1769 minfo->fbcon.pseudo_palette = minfo->cmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /* after __init time we are like module... no logo */
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001771 minfo->fbcon.flags = hotplug ? FBINFO_FLAG_MODULE : FBINFO_FLAG_DEFAULT;
1772 minfo->fbcon.flags |= FBINFO_PARTIAL_PAN_OK | /* Prefer panning for scroll under MC viewer/edit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 FBINFO_HWACCEL_COPYAREA | /* We have hw-assisted bmove */
1774 FBINFO_HWACCEL_FILLRECT | /* And fillrect */
1775 FBINFO_HWACCEL_IMAGEBLIT | /* And imageblit */
1776 FBINFO_HWACCEL_XPAN | /* And we support both horizontal */
1777 FBINFO_HWACCEL_YPAN; /* And vertical panning */
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001778 minfo->video.len_usable &= PAGE_MASK;
1779 fb_alloc_cmap(&minfo->fbcon.cmap, 256, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781#ifndef MODULE
1782 /* mode database is marked __init!!! */
1783 if (!hotplug) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001784 fb_find_mode(&vesafb_defined, &minfo->fbcon, videomode[0] ? videomode : NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 NULL, 0, &defaultmode, vesafb_defined.bits_per_pixel);
1786 }
1787#endif /* !MODULE */
1788
1789 /* mode modifiers */
1790 if (hslen)
1791 vesafb_defined.hsync_len = hslen;
1792 if (vslen)
1793 vesafb_defined.vsync_len = vslen;
1794 if (left != ~0)
1795 vesafb_defined.left_margin = left;
1796 if (right != ~0)
1797 vesafb_defined.right_margin = right;
1798 if (upper != ~0)
1799 vesafb_defined.upper_margin = upper;
1800 if (lower != ~0)
1801 vesafb_defined.lower_margin = lower;
1802 if (xres)
1803 vesafb_defined.xres = xres;
1804 if (yres)
1805 vesafb_defined.yres = yres;
1806 if (sync != -1)
1807 vesafb_defined.sync = sync;
1808 else if (vesafb_defined.sync == ~0) {
1809 vesafb_defined.sync = 0;
1810 if (yres < 400)
1811 vesafb_defined.sync |= FB_SYNC_HOR_HIGH_ACT;
1812 else if (yres < 480)
1813 vesafb_defined.sync |= FB_SYNC_VERT_HIGH_ACT;
1814 }
1815
1816 /* fv, fh, maxclk limits was specified */
1817 {
1818 unsigned int tmp;
1819
1820 if (fv) {
1821 tmp = fv * (vesafb_defined.upper_margin + vesafb_defined.yres
Andrew Mortona50d9132006-06-21 01:57:39 -03001822 + vesafb_defined.lower_margin + vesafb_defined.vsync_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if ((tmp < fh) || (fh == 0)) fh = tmp;
1824 }
1825 if (fh) {
1826 tmp = fh * (vesafb_defined.left_margin + vesafb_defined.xres
Andrew Mortona50d9132006-06-21 01:57:39 -03001827 + vesafb_defined.right_margin + vesafb_defined.hsync_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if ((tmp < maxclk) || (maxclk == 0)) maxclk = tmp;
1829 }
1830 tmp = (maxclk + 499) / 500;
1831 if (tmp) {
1832 tmp = (2000000000 + tmp) / tmp;
1833 if (tmp > pixclock) pixclock = tmp;
1834 }
1835 }
1836 if (pixclock) {
1837 if (pixclock < 2000) /* > 500MHz */
1838 pixclock = 4000; /* 250MHz */
1839 if (pixclock > 1000000)
1840 pixclock = 1000000; /* 1MHz */
1841 vesafb_defined.pixclock = pixclock;
1842 }
1843
1844 /* FIXME: Where to move this?! */
1845#if defined(CONFIG_PPC_PMAC)
1846#ifndef MODULE
Benjamin Herrenschmidte8222502006-03-28 23:15:54 +11001847 if (machine_is(powermac)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 struct fb_var_screeninfo var;
1849 if (default_vmode <= 0 || default_vmode > VMODE_MAX)
1850 default_vmode = VMODE_640_480_60;
1851#ifdef CONFIG_NVRAM
1852 if (default_cmode == CMODE_NVRAM)
1853 default_cmode = nvram_read_byte(NV_CMODE);
1854#endif
1855 if (default_cmode < CMODE_8 || default_cmode > CMODE_32)
1856 default_cmode = CMODE_8;
1857 if (!mac_vmode_to_var(default_vmode, default_cmode, &var)) {
1858 var.accel_flags = vesafb_defined.accel_flags;
1859 var.xoffset = var.yoffset = 0;
1860 /* Note: mac_vmode_to_var() does not set all parameters */
1861 vesafb_defined = var;
1862 }
1863 }
1864#endif /* !MODULE */
1865#endif /* CONFIG_PPC_PMAC */
1866 vesafb_defined.xres_virtual = vesafb_defined.xres;
1867 if (nopan) {
1868 vesafb_defined.yres_virtual = vesafb_defined.yres;
1869 } else {
1870 vesafb_defined.yres_virtual = 65536; /* large enough to be INF, but small enough
1871 to yres_virtual * xres_virtual < 2^32 */
1872 }
1873 matroxfb_init_fix(PMINFO2);
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001874 minfo->fbcon.screen_base = vaddr_va(minfo->video.vbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /* Normalize values (namely yres_virtual) */
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001876 matroxfb_check_var(&vesafb_defined, &minfo->fbcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 /* And put it into "current" var. Do NOT program hardware yet, or we'll not take over
1878 * vgacon correctly. fbcon_startup will call fb_set_par for us, WITHOUT check_var,
1879 * and unfortunately it will do it BEFORE vgacon contents is saved, so it won't work
1880 * anyway. But we at least tried... */
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001881 minfo->fbcon.var = vesafb_defined;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 err = -EINVAL;
1883
1884 printk(KERN_INFO "matroxfb: %dx%dx%dbpp (virtual: %dx%d)\n",
1885 vesafb_defined.xres, vesafb_defined.yres, vesafb_defined.bits_per_pixel,
1886 vesafb_defined.xres_virtual, vesafb_defined.yres_virtual);
1887 printk(KERN_INFO "matroxfb: framebuffer at 0x%lX, mapped to 0x%p, size %d\n",
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001888 minfo->video.base, vaddr_va(minfo->video.vbase), minfo->video.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890/* We do not have to set currcon to 0... register_framebuffer do it for us on first console
1891 * and we do not want currcon == 0 for subsequent framebuffers */
1892
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001893 minfo->fbcon.device = &minfo->pcidev->dev;
1894 if (register_framebuffer(&minfo->fbcon) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto failVideoIO;
1896 }
1897 printk("fb%d: %s frame buffer device\n",
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001898 minfo->fbcon.node, minfo->fbcon.fix.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /* there is no console on this fb... but we have to initialize hardware
1901 * until someone tells me what is proper thing to do */
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001902 if (!minfo->initialized) {
Andrew Mortona50d9132006-06-21 01:57:39 -03001903 printk(KERN_INFO "fb%d: initializing hardware\n",
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001904 minfo->fbcon.node);
Andrew Mortona50d9132006-06-21 01:57:39 -03001905 /* We have to use FB_ACTIVATE_FORCE, as we had to put vesafb_defined to the fbcon.var
1906 * already before, so register_framebuffer works correctly. */
1907 vesafb_defined.activate |= FB_ACTIVATE_FORCE;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001908 fb_set_var(&minfo->fbcon, &vesafb_defined);
Andrew Mortona50d9132006-06-21 01:57:39 -03001909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 return 0;
1912failVideoIO:;
1913 matroxfb_g450_shutdown(PMINFO2);
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001914 mga_iounmap(minfo->video.vbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915failCtrlIO:;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001916 mga_iounmap(minfo->mmio.vbase);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917failVideoMR:;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001918 release_mem_region(video_base_phys, minfo->video.len_maximum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919failCtrlMR:;
1920 release_mem_region(ctrlptr_phys, 16384);
1921fail:;
1922 return err;
1923}
1924
1925static LIST_HEAD(matroxfb_list);
1926static LIST_HEAD(matroxfb_driver_list);
1927
1928#define matroxfb_l(x) list_entry(x, struct matrox_fb_info, next_fb)
1929#define matroxfb_driver_l(x) list_entry(x, struct matroxfb_driver, node)
1930int matroxfb_register_driver(struct matroxfb_driver* drv) {
1931 struct matrox_fb_info* minfo;
1932
1933 list_add(&drv->node, &matroxfb_driver_list);
1934 for (minfo = matroxfb_l(matroxfb_list.next);
1935 minfo != matroxfb_l(&matroxfb_list);
1936 minfo = matroxfb_l(minfo->next_fb.next)) {
1937 void* p;
1938
1939 if (minfo->drivers_count == MATROXFB_MAX_FB_DRIVERS)
1940 continue;
1941 p = drv->probe(minfo);
1942 if (p) {
1943 minfo->drivers_data[minfo->drivers_count] = p;
1944 minfo->drivers[minfo->drivers_count++] = drv;
1945 }
1946 }
1947 return 0;
1948}
1949
1950void matroxfb_unregister_driver(struct matroxfb_driver* drv) {
1951 struct matrox_fb_info* minfo;
1952
1953 list_del(&drv->node);
1954 for (minfo = matroxfb_l(matroxfb_list.next);
1955 minfo != matroxfb_l(&matroxfb_list);
1956 minfo = matroxfb_l(minfo->next_fb.next)) {
1957 int i;
1958
1959 for (i = 0; i < minfo->drivers_count; ) {
1960 if (minfo->drivers[i] == drv) {
1961 if (drv && drv->remove)
1962 drv->remove(minfo, minfo->drivers_data[i]);
1963 minfo->drivers[i] = minfo->drivers[--minfo->drivers_count];
1964 minfo->drivers_data[i] = minfo->drivers_data[minfo->drivers_count];
1965 } else
1966 i++;
1967 }
1968 }
1969}
1970
1971static void matroxfb_register_device(struct matrox_fb_info* minfo) {
1972 struct matroxfb_driver* drv;
1973 int i = 0;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001974 list_add(&minfo->next_fb, &matroxfb_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 for (drv = matroxfb_driver_l(matroxfb_driver_list.next);
1976 drv != matroxfb_driver_l(&matroxfb_driver_list);
1977 drv = matroxfb_driver_l(drv->node.next)) {
1978 if (drv && drv->probe) {
1979 void *p = drv->probe(minfo);
1980 if (p) {
1981 minfo->drivers_data[i] = p;
1982 minfo->drivers[i++] = drv;
1983 if (i == MATROXFB_MAX_FB_DRIVERS)
1984 break;
1985 }
1986 }
1987 }
1988 minfo->drivers_count = i;
1989}
1990
1991static void matroxfb_unregister_device(struct matrox_fb_info* minfo) {
1992 int i;
1993
Jean Delvarefc2d10d2009-09-22 16:47:48 -07001994 list_del(&minfo->next_fb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 for (i = 0; i < minfo->drivers_count; i++) {
1996 struct matroxfb_driver* drv = minfo->drivers[i];
1997
1998 if (drv && drv->remove)
1999 drv->remove(minfo, minfo->drivers_data[i]);
2000 }
2001}
2002
2003static int matroxfb_probe(struct pci_dev* pdev, const struct pci_device_id* dummy) {
2004 struct board* b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 u_int16_t svid;
2006 u_int16_t sid;
2007 struct matrox_fb_info* minfo;
2008 int err;
2009 u_int32_t cmd;
Harvey Harrison5ae12172008-04-28 02:15:47 -07002010 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 svid = pdev->subsystem_vendor;
2013 sid = pdev->subsystem_device;
2014 for (b = dev_list; b->vendor; b++) {
Auke Kok44c10132007-06-08 15:46:36 -07002015 if ((b->vendor != pdev->vendor) || (b->device != pdev->device) || (b->rev < pdev->revision)) continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (b->svid)
2017 if ((b->svid != svid) || (b->sid != sid)) continue;
2018 break;
2019 }
2020 /* not match... */
2021 if (!b->vendor)
Jan Beulich438e5c52005-09-13 01:25:45 -07002022 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (dev > 0) {
2024 /* not requested one... */
2025 dev--;
Jan Beulich438e5c52005-09-13 01:25:45 -07002026 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 pci_read_config_dword(pdev, PCI_COMMAND, &cmd);
2029 if (pci_enable_device(pdev)) {
2030 return -1;
2031 }
2032
Robert P. J. Day5cbded52006-12-13 00:35:56 -08002033 minfo = kmalloc(sizeof(*minfo), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (!minfo)
2035 return -1;
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002036 memset(minfo, 0, sizeof(*minfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002038 minfo->pcidev = pdev;
2039 minfo->dead = 0;
2040 minfo->usecount = 0;
2041 minfo->userusecount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002043 pci_set_drvdata(pdev, minfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /* DEVFLAGS */
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002045 minfo->devflags.memtype = memtype;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (memtype != -1)
2047 noinit = 0;
2048 if (cmd & PCI_COMMAND_MEMORY) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002049 minfo->devflags.novga = novga;
2050 minfo->devflags.nobios = nobios;
2051 minfo->devflags.noinit = noinit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* subsequent heads always needs initialization and must not enable BIOS */
2053 novga = 1;
2054 nobios = 1;
2055 noinit = 0;
2056 } else {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002057 minfo->devflags.novga = 1;
2058 minfo->devflags.nobios = 1;
2059 minfo->devflags.noinit = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 }
2061
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002062 minfo->devflags.nopciretry = no_pci_retry;
2063 minfo->devflags.mga_24bpp_fix = inv24;
2064 minfo->devflags.precise_width = option_precise_width;
2065 minfo->devflags.sgram = sgram;
2066 minfo->capable.cross4MB = cross4MB;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002068 spin_lock_init(&minfo->lock.DAC);
2069 spin_lock_init(&minfo->lock.accel);
2070 init_rwsem(&minfo->crtc2.lock);
2071 init_rwsem(&minfo->altout.lock);
2072 mutex_init(&minfo->fbcon.mm_lock);
2073 minfo->irq_flags = 0;
2074 init_waitqueue_head(&minfo->crtc1.vsync.wait);
2075 init_waitqueue_head(&minfo->crtc2.vsync.wait);
2076 minfo->crtc1.panpos = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 err = initMatrox2(PMINFO b);
2079 if (!err) {
Jean Delvarefc2d10d2009-09-22 16:47:48 -07002080 matroxfb_register_device(minfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 kfree(minfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return -1;
2085}
2086
2087static void pci_remove_matrox(struct pci_dev* pdev) {
2088 struct matrox_fb_info* minfo;
2089
2090 minfo = pci_get_drvdata(pdev);
2091 matroxfb_remove(PMINFO 1);
2092}
2093
2094static struct pci_device_id matroxfb_devices[] = {
2095#ifdef CONFIG_FB_MATROX_MILLENIUM
2096 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL,
2097 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2098 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL_2,
2099 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2100 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MIL_2_AGP,
2101 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2102#endif
2103#ifdef CONFIG_FB_MATROX_MYSTIQUE
2104 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_MYS,
2105 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2106#endif
2107#ifdef CONFIG_FB_MATROX_G
2108 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G100_MM,
2109 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2110 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G100_AGP,
2111 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Darrick J. Wonge3a19382008-10-15 22:03:52 -07002112 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200EV_PCI,
2113 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_PCI,
2115 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2116 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP,
2117 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2118 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400,
2119 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2120 {PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G550,
2121 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
2122#endif
2123 {0, 0,
2124 0, 0, 0, 0, 0}
2125};
2126
2127MODULE_DEVICE_TABLE(pci, matroxfb_devices);
2128
2129
2130static struct pci_driver matroxfb_driver = {
2131 .name = "matroxfb",
2132 .id_table = matroxfb_devices,
2133 .probe = matroxfb_probe,
2134 .remove = pci_remove_matrox,
2135};
2136
2137/* **************************** init-time only **************************** */
2138
2139#define RSResolution(X) ((X) & 0x0F)
2140#define RS640x400 1
2141#define RS640x480 2
2142#define RS800x600 3
2143#define RS1024x768 4
2144#define RS1280x1024 5
2145#define RS1600x1200 6
2146#define RS768x576 7
2147#define RS960x720 8
2148#define RS1152x864 9
2149#define RS1408x1056 10
2150#define RS640x350 11
2151#define RS1056x344 12 /* 132 x 43 text */
2152#define RS1056x400 13 /* 132 x 50 text */
2153#define RS1056x480 14 /* 132 x 60 text */
2154#define RSNoxNo 15
2155/* 10-FF */
2156static struct { int xres, yres, left, right, upper, lower, hslen, vslen, vfreq; } timmings[] __initdata = {
2157 { 640, 400, 48, 16, 39, 8, 96, 2, 70 },
2158 { 640, 480, 48, 16, 33, 10, 96, 2, 60 },
2159 { 800, 600, 144, 24, 28, 8, 112, 6, 60 },
2160 { 1024, 768, 160, 32, 30, 4, 128, 4, 60 },
2161 { 1280, 1024, 224, 32, 32, 4, 136, 4, 60 },
2162 { 1600, 1200, 272, 48, 32, 5, 152, 5, 60 },
2163 { 768, 576, 144, 16, 28, 6, 112, 4, 60 },
2164 { 960, 720, 144, 24, 28, 8, 112, 4, 60 },
2165 { 1152, 864, 192, 32, 30, 4, 128, 4, 60 },
2166 { 1408, 1056, 256, 40, 32, 5, 144, 5, 60 },
2167 { 640, 350, 48, 16, 39, 8, 96, 2, 70 },
2168 { 1056, 344, 96, 24, 59, 44, 160, 2, 70 },
2169 { 1056, 400, 96, 24, 39, 8, 160, 2, 70 },
2170 { 1056, 480, 96, 24, 36, 12, 160, 3, 60 },
2171 { 0, 0, ~0, ~0, ~0, ~0, 0, 0, 0 }
2172};
2173
2174#define RSCreate(X,Y) ((X) | ((Y) << 8))
2175static struct { unsigned int vesa; unsigned int info; } *RSptr, vesamap[] __initdata = {
2176/* default must be first */
2177 { ~0, RSCreate(RSNoxNo, RS8bpp ) },
2178 { 0x101, RSCreate(RS640x480, RS8bpp ) },
2179 { 0x100, RSCreate(RS640x400, RS8bpp ) },
2180 { 0x180, RSCreate(RS768x576, RS8bpp ) },
2181 { 0x103, RSCreate(RS800x600, RS8bpp ) },
2182 { 0x188, RSCreate(RS960x720, RS8bpp ) },
2183 { 0x105, RSCreate(RS1024x768, RS8bpp ) },
2184 { 0x190, RSCreate(RS1152x864, RS8bpp ) },
2185 { 0x107, RSCreate(RS1280x1024, RS8bpp ) },
2186 { 0x198, RSCreate(RS1408x1056, RS8bpp ) },
2187 { 0x11C, RSCreate(RS1600x1200, RS8bpp ) },
2188 { 0x110, RSCreate(RS640x480, RS15bpp) },
2189 { 0x181, RSCreate(RS768x576, RS15bpp) },
2190 { 0x113, RSCreate(RS800x600, RS15bpp) },
2191 { 0x189, RSCreate(RS960x720, RS15bpp) },
2192 { 0x116, RSCreate(RS1024x768, RS15bpp) },
2193 { 0x191, RSCreate(RS1152x864, RS15bpp) },
2194 { 0x119, RSCreate(RS1280x1024, RS15bpp) },
2195 { 0x199, RSCreate(RS1408x1056, RS15bpp) },
2196 { 0x11D, RSCreate(RS1600x1200, RS15bpp) },
2197 { 0x111, RSCreate(RS640x480, RS16bpp) },
2198 { 0x182, RSCreate(RS768x576, RS16bpp) },
2199 { 0x114, RSCreate(RS800x600, RS16bpp) },
2200 { 0x18A, RSCreate(RS960x720, RS16bpp) },
2201 { 0x117, RSCreate(RS1024x768, RS16bpp) },
2202 { 0x192, RSCreate(RS1152x864, RS16bpp) },
2203 { 0x11A, RSCreate(RS1280x1024, RS16bpp) },
2204 { 0x19A, RSCreate(RS1408x1056, RS16bpp) },
2205 { 0x11E, RSCreate(RS1600x1200, RS16bpp) },
2206 { 0x1B2, RSCreate(RS640x480, RS24bpp) },
2207 { 0x184, RSCreate(RS768x576, RS24bpp) },
2208 { 0x1B5, RSCreate(RS800x600, RS24bpp) },
2209 { 0x18C, RSCreate(RS960x720, RS24bpp) },
2210 { 0x1B8, RSCreate(RS1024x768, RS24bpp) },
2211 { 0x194, RSCreate(RS1152x864, RS24bpp) },
2212 { 0x1BB, RSCreate(RS1280x1024, RS24bpp) },
2213 { 0x19C, RSCreate(RS1408x1056, RS24bpp) },
2214 { 0x1BF, RSCreate(RS1600x1200, RS24bpp) },
2215 { 0x112, RSCreate(RS640x480, RS32bpp) },
2216 { 0x183, RSCreate(RS768x576, RS32bpp) },
2217 { 0x115, RSCreate(RS800x600, RS32bpp) },
2218 { 0x18B, RSCreate(RS960x720, RS32bpp) },
2219 { 0x118, RSCreate(RS1024x768, RS32bpp) },
2220 { 0x193, RSCreate(RS1152x864, RS32bpp) },
2221 { 0x11B, RSCreate(RS1280x1024, RS32bpp) },
2222 { 0x19B, RSCreate(RS1408x1056, RS32bpp) },
2223 { 0x11F, RSCreate(RS1600x1200, RS32bpp) },
2224 { 0x010, RSCreate(RS640x350, RS4bpp ) },
2225 { 0x012, RSCreate(RS640x480, RS4bpp ) },
2226 { 0x102, RSCreate(RS800x600, RS4bpp ) },
2227 { 0x104, RSCreate(RS1024x768, RS4bpp ) },
2228 { 0x106, RSCreate(RS1280x1024, RS4bpp ) },
2229 { 0, 0 }};
2230
2231static void __init matroxfb_init_params(void) {
2232 /* fh from kHz to Hz */
2233 if (fh < 1000)
2234 fh *= 1000; /* 1kHz minimum */
2235 /* maxclk */
2236 if (maxclk < 1000) maxclk *= 1000; /* kHz -> Hz, MHz -> kHz */
2237 if (maxclk < 1000000) maxclk *= 1000; /* kHz -> Hz, 1MHz minimum */
2238 /* fix VESA number */
2239 if (vesa != ~0)
2240 vesa &= 0x1DFF; /* mask out clearscreen, acceleration and so on */
2241
2242 /* static settings */
2243 for (RSptr = vesamap; RSptr->vesa; RSptr++) {
2244 if (RSptr->vesa == vesa) break;
2245 }
2246 if (!RSptr->vesa) {
2247 printk(KERN_ERR "Invalid vesa mode 0x%04X\n", vesa);
2248 RSptr = vesamap;
2249 }
2250 {
2251 int res = RSResolution(RSptr->info)-1;
2252 if (left == ~0)
2253 left = timmings[res].left;
2254 if (!xres)
2255 xres = timmings[res].xres;
2256 if (right == ~0)
2257 right = timmings[res].right;
2258 if (!hslen)
2259 hslen = timmings[res].hslen;
2260 if (upper == ~0)
2261 upper = timmings[res].upper;
2262 if (!yres)
2263 yres = timmings[res].yres;
2264 if (lower == ~0)
2265 lower = timmings[res].lower;
2266 if (!vslen)
2267 vslen = timmings[res].vslen;
2268 if (!(fv||fh||maxclk||pixclock))
2269 fv = timmings[res].vfreq;
2270 if (depth == -1)
2271 depth = RSDepth(RSptr->info);
2272 }
2273}
2274
Antonino A. Daplasb0b10322006-10-03 01:14:52 -07002275static int __init matrox_init(void) {
2276 int err;
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 matroxfb_init_params();
Antonino A. Daplasb0b10322006-10-03 01:14:52 -07002279 err = pci_register_driver(&matroxfb_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 dev = -1; /* accept all new devices... */
Antonino A. Daplasb0b10322006-10-03 01:14:52 -07002281 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282}
2283
2284/* **************************** exit-time only **************************** */
2285
2286static void __exit matrox_done(void) {
2287 pci_unregister_driver(&matroxfb_driver);
2288}
2289
2290#ifndef MODULE
2291
2292/* ************************* init in-kernel code ************************** */
2293
2294static int __init matroxfb_setup(char *options) {
2295 char *this_opt;
2296
Harvey Harrison5ae12172008-04-28 02:15:47 -07002297 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 if (!options || !*options)
2300 return 0;
2301
2302 while ((this_opt = strsep(&options, ",")) != NULL) {
2303 if (!*this_opt) continue;
2304
2305 dprintk("matroxfb_setup: option %s\n", this_opt);
2306
2307 if (!strncmp(this_opt, "dev:", 4))
2308 dev = simple_strtoul(this_opt+4, NULL, 0);
2309 else if (!strncmp(this_opt, "depth:", 6)) {
2310 switch (simple_strtoul(this_opt+6, NULL, 0)) {
2311 case 0: depth = RSText; break;
2312 case 4: depth = RS4bpp; break;
2313 case 8: depth = RS8bpp; break;
2314 case 15:depth = RS15bpp; break;
2315 case 16:depth = RS16bpp; break;
2316 case 24:depth = RS24bpp; break;
2317 case 32:depth = RS32bpp; break;
2318 default:
2319 printk(KERN_ERR "matroxfb: unsupported color depth\n");
2320 }
2321 } else if (!strncmp(this_opt, "xres:", 5))
2322 xres = simple_strtoul(this_opt+5, NULL, 0);
2323 else if (!strncmp(this_opt, "yres:", 5))
2324 yres = simple_strtoul(this_opt+5, NULL, 0);
2325 else if (!strncmp(this_opt, "vslen:", 6))
2326 vslen = simple_strtoul(this_opt+6, NULL, 0);
2327 else if (!strncmp(this_opt, "hslen:", 6))
2328 hslen = simple_strtoul(this_opt+6, NULL, 0);
2329 else if (!strncmp(this_opt, "left:", 5))
2330 left = simple_strtoul(this_opt+5, NULL, 0);
2331 else if (!strncmp(this_opt, "right:", 6))
2332 right = simple_strtoul(this_opt+6, NULL, 0);
2333 else if (!strncmp(this_opt, "upper:", 6))
2334 upper = simple_strtoul(this_opt+6, NULL, 0);
2335 else if (!strncmp(this_opt, "lower:", 6))
2336 lower = simple_strtoul(this_opt+6, NULL, 0);
2337 else if (!strncmp(this_opt, "pixclock:", 9))
2338 pixclock = simple_strtoul(this_opt+9, NULL, 0);
2339 else if (!strncmp(this_opt, "sync:", 5))
2340 sync = simple_strtoul(this_opt+5, NULL, 0);
2341 else if (!strncmp(this_opt, "vesa:", 5))
2342 vesa = simple_strtoul(this_opt+5, NULL, 0);
2343 else if (!strncmp(this_opt, "maxclk:", 7))
2344 maxclk = simple_strtoul(this_opt+7, NULL, 0);
2345 else if (!strncmp(this_opt, "fh:", 3))
2346 fh = simple_strtoul(this_opt+3, NULL, 0);
2347 else if (!strncmp(this_opt, "fv:", 3))
2348 fv = simple_strtoul(this_opt+3, NULL, 0);
2349 else if (!strncmp(this_opt, "mem:", 4))
2350 mem = simple_strtoul(this_opt+4, NULL, 0);
2351 else if (!strncmp(this_opt, "mode:", 5))
2352 strlcpy(videomode, this_opt+5, sizeof(videomode));
2353 else if (!strncmp(this_opt, "outputs:", 8))
2354 strlcpy(outputs, this_opt+8, sizeof(outputs));
2355 else if (!strncmp(this_opt, "dfp:", 4)) {
2356 dfp_type = simple_strtoul(this_opt+4, NULL, 0);
2357 dfp = 1;
Andrew Mortona50d9132006-06-21 01:57:39 -03002358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359#ifdef CONFIG_PPC_PMAC
2360 else if (!strncmp(this_opt, "vmode:", 6)) {
2361 unsigned int vmode = simple_strtoul(this_opt+6, NULL, 0);
2362 if (vmode > 0 && vmode <= VMODE_MAX)
2363 default_vmode = vmode;
2364 } else if (!strncmp(this_opt, "cmode:", 6)) {
2365 unsigned int cmode = simple_strtoul(this_opt+6, NULL, 0);
2366 switch (cmode) {
2367 case 0:
2368 case 8:
2369 default_cmode = CMODE_8;
2370 break;
2371 case 15:
2372 case 16:
2373 default_cmode = CMODE_16;
2374 break;
2375 case 24:
2376 case 32:
2377 default_cmode = CMODE_32;
2378 break;
2379 }
2380 }
2381#endif
2382 else if (!strcmp(this_opt, "disabled")) /* nodisabled does not exist */
2383 disabled = 1;
2384 else if (!strcmp(this_opt, "enabled")) /* noenabled does not exist */
2385 disabled = 0;
2386 else if (!strcmp(this_opt, "sgram")) /* nosgram == sdram */
2387 sgram = 1;
2388 else if (!strcmp(this_opt, "sdram"))
2389 sgram = 0;
2390 else if (!strncmp(this_opt, "memtype:", 8))
2391 memtype = simple_strtoul(this_opt+8, NULL, 0);
2392 else {
2393 int value = 1;
2394
2395 if (!strncmp(this_opt, "no", 2)) {
2396 value = 0;
2397 this_opt += 2;
2398 }
2399 if (! strcmp(this_opt, "inverse"))
2400 inverse = value;
2401 else if (!strcmp(this_opt, "accel"))
2402 noaccel = !value;
2403 else if (!strcmp(this_opt, "pan"))
2404 nopan = !value;
2405 else if (!strcmp(this_opt, "pciretry"))
2406 no_pci_retry = !value;
2407 else if (!strcmp(this_opt, "vga"))
2408 novga = !value;
2409 else if (!strcmp(this_opt, "bios"))
2410 nobios = !value;
2411 else if (!strcmp(this_opt, "init"))
2412 noinit = !value;
2413#ifdef CONFIG_MTRR
2414 else if (!strcmp(this_opt, "mtrr"))
2415 mtrr = value;
2416#endif
2417 else if (!strcmp(this_opt, "inv24"))
2418 inv24 = value;
2419 else if (!strcmp(this_opt, "cross4MB"))
2420 cross4MB = value;
2421 else if (!strcmp(this_opt, "grayscale"))
2422 grayscale = value;
2423 else if (!strcmp(this_opt, "dfp"))
2424 dfp = value;
2425 else {
2426 strlcpy(videomode, this_opt, sizeof(videomode));
2427 }
2428 }
2429 }
2430 return 0;
2431}
2432
2433static int __initdata initialized = 0;
2434
2435static int __init matroxfb_init(void)
2436{
2437 char *option = NULL;
Antonino A. Daplasb0b10322006-10-03 01:14:52 -07002438 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Harvey Harrison5ae12172008-04-28 02:15:47 -07002440 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 if (fb_get_options("matroxfb", &option))
2443 return -ENODEV;
2444 matroxfb_setup(option);
2445
2446 if (disabled)
2447 return -ENXIO;
2448 if (!initialized) {
2449 initialized = 1;
Antonino A. Daplasb0b10322006-10-03 01:14:52 -07002450 err = matrox_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
2452 hotplug = 1;
2453 /* never return failure, user can hotplug matrox later... */
Antonino A. Daplasb0b10322006-10-03 01:14:52 -07002454 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
2457module_init(matroxfb_init);
2458
2459#else
2460
2461/* *************************** init module code **************************** */
2462
2463MODULE_AUTHOR("(c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
2464MODULE_DESCRIPTION("Accelerated FBDev driver for Matrox Millennium/Mystique/G100/G200/G400/G450/G550");
2465MODULE_LICENSE("GPL");
2466
2467module_param(mem, int, 0);
2468MODULE_PARM_DESC(mem, "Size of available memory in MB, KB or B (2,4,8,12,16MB, default=autodetect)");
2469module_param(disabled, int, 0);
2470MODULE_PARM_DESC(disabled, "Disabled (0 or 1=disabled) (default=0)");
2471module_param(noaccel, int, 0);
2472MODULE_PARM_DESC(noaccel, "Do not use accelerating engine (0 or 1=disabled) (default=0)");
2473module_param(nopan, int, 0);
2474MODULE_PARM_DESC(nopan, "Disable pan on startup (0 or 1=disabled) (default=0)");
2475module_param(no_pci_retry, int, 0);
2476MODULE_PARM_DESC(no_pci_retry, "PCI retries enabled (0 or 1=disabled) (default=0)");
2477module_param(novga, int, 0);
2478MODULE_PARM_DESC(novga, "VGA I/O (0x3C0-0x3DF) disabled (0 or 1=disabled) (default=0)");
2479module_param(nobios, int, 0);
2480MODULE_PARM_DESC(nobios, "Disables ROM BIOS (0 or 1=disabled) (default=do not change BIOS state)");
2481module_param(noinit, int, 0);
2482MODULE_PARM_DESC(noinit, "Disables W/SG/SD-RAM and bus interface initialization (0 or 1=do not initialize) (default=0)");
2483module_param(memtype, int, 0);
2484MODULE_PARM_DESC(memtype, "Memory type for G200/G400 (see Documentation/fb/matroxfb.txt for explanation) (default=3 for G200, 0 for G400)");
2485#ifdef CONFIG_MTRR
2486module_param(mtrr, int, 0);
2487MODULE_PARM_DESC(mtrr, "This speeds up video memory accesses (0=disabled or 1) (default=1)");
2488#endif
2489module_param(sgram, int, 0);
2490MODULE_PARM_DESC(sgram, "Indicates that G100/G200/G400 has SGRAM memory (0=SDRAM, 1=SGRAM) (default=0)");
2491module_param(inv24, int, 0);
2492MODULE_PARM_DESC(inv24, "Inverts clock polarity for 24bpp and loop frequency > 100MHz (default=do not invert polarity)");
2493module_param(inverse, int, 0);
2494MODULE_PARM_DESC(inverse, "Inverse (0 or 1) (default=0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495module_param(dev, int, 0);
2496MODULE_PARM_DESC(dev, "Multihead support, attach to device ID (0..N) (default=all working)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497module_param(vesa, int, 0);
2498MODULE_PARM_DESC(vesa, "Startup videomode (0x000-0x1FF) (default=0x101)");
2499module_param(xres, int, 0);
2500MODULE_PARM_DESC(xres, "Horizontal resolution (px), overrides xres from vesa (default=vesa)");
2501module_param(yres, int, 0);
2502MODULE_PARM_DESC(yres, "Vertical resolution (scans), overrides yres from vesa (default=vesa)");
2503module_param(upper, int, 0);
2504MODULE_PARM_DESC(upper, "Upper blank space (scans), overrides upper from vesa (default=vesa)");
2505module_param(lower, int, 0);
2506MODULE_PARM_DESC(lower, "Lower blank space (scans), overrides lower from vesa (default=vesa)");
2507module_param(vslen, int, 0);
2508MODULE_PARM_DESC(vslen, "Vertical sync length (scans), overrides lower from vesa (default=vesa)");
2509module_param(left, int, 0);
2510MODULE_PARM_DESC(left, "Left blank space (px), overrides left from vesa (default=vesa)");
2511module_param(right, int, 0);
2512MODULE_PARM_DESC(right, "Right blank space (px), overrides right from vesa (default=vesa)");
2513module_param(hslen, int, 0);
2514MODULE_PARM_DESC(hslen, "Horizontal sync length (px), overrides hslen from vesa (default=vesa)");
2515module_param(pixclock, int, 0);
2516MODULE_PARM_DESC(pixclock, "Pixelclock (ns), overrides pixclock from vesa (default=vesa)");
2517module_param(sync, int, 0);
2518MODULE_PARM_DESC(sync, "Sync polarity, overrides sync from vesa (default=vesa)");
2519module_param(depth, int, 0);
2520MODULE_PARM_DESC(depth, "Color depth (0=text,8,15,16,24,32) (default=vesa)");
2521module_param(maxclk, int, 0);
2522MODULE_PARM_DESC(maxclk, "Startup maximal clock, 0-999MHz, 1000-999999kHz, 1000000-INF Hz");
2523module_param(fh, int, 0);
2524MODULE_PARM_DESC(fh, "Startup horizontal frequency, 0-999kHz, 1000-INF Hz");
2525module_param(fv, int, 0);
2526MODULE_PARM_DESC(fv, "Startup vertical frequency, 0-INF Hz\n"
Niels de Vos61a2d072008-07-31 00:07:23 -07002527"You should specify \"fv:max_monitor_vsync,fh:max_monitor_hsync,maxclk:max_monitor_dotclock\"");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528module_param(grayscale, int, 0);
2529MODULE_PARM_DESC(grayscale, "Sets display into grayscale. Works perfectly with paletized videomode (4, 8bpp), some limitations apply to 16, 24 and 32bpp videomodes (default=nograyscale)");
2530module_param(cross4MB, int, 0);
2531MODULE_PARM_DESC(cross4MB, "Specifies that 4MB boundary can be in middle of line. (default=autodetected)");
2532module_param(dfp, int, 0);
2533MODULE_PARM_DESC(dfp, "Specifies whether to use digital flat panel interface of G200/G400 (0 or 1) (default=0)");
2534module_param(dfp_type, int, 0);
2535MODULE_PARM_DESC(dfp_type, "Specifies DFP interface type (0 to 255) (default=read from hardware)");
2536module_param_string(outputs, outputs, sizeof(outputs), 0);
2537MODULE_PARM_DESC(outputs, "Specifies which CRTC is mapped to which output (string of up to three letters, consisting of 0 (disabled), 1 (CRTC1), 2 (CRTC2)) (default=111 for Gx50, 101 for G200/G400 with DFP, and 100 for all other devices)");
2538#ifdef CONFIG_PPC_PMAC
2539module_param_named(vmode, default_vmode, int, 0);
2540MODULE_PARM_DESC(vmode, "Specify the vmode mode number that should be used (640x480 default)");
2541module_param_named(cmode, default_cmode, int, 0);
2542MODULE_PARM_DESC(cmode, "Specify the video depth that should be used (8bit default)");
2543#endif
2544
2545int __init init_module(void){
2546
Harvey Harrison5ae12172008-04-28 02:15:47 -07002547 DBG(__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
2549 if (disabled)
2550 return -ENXIO;
2551
2552 if (depth == 0)
2553 depth = RSText;
2554 else if (depth == 4)
2555 depth = RS4bpp;
2556 else if (depth == 8)
2557 depth = RS8bpp;
2558 else if (depth == 15)
2559 depth = RS15bpp;
2560 else if (depth == 16)
2561 depth = RS16bpp;
2562 else if (depth == 24)
2563 depth = RS24bpp;
2564 else if (depth == 32)
2565 depth = RS32bpp;
2566 else if (depth != -1) {
2567 printk(KERN_ERR "matroxfb: depth %d is not supported, using default\n", depth);
2568 depth = -1;
2569 }
2570 matrox_init();
2571 /* never return failure; user can hotplug matrox later... */
2572 return 0;
2573}
2574#endif /* MODULE */
2575
2576module_exit(matrox_done);
2577EXPORT_SYMBOL(matroxfb_register_driver);
2578EXPORT_SYMBOL(matroxfb_unregister_driver);
2579EXPORT_SYMBOL(matroxfb_wait_for_sync);
2580EXPORT_SYMBOL(matroxfb_enable_irq);
2581
2582/*
2583 * Overrides for Emacs so that we follow Linus's tabbing style.
2584 * ---------------------------------------------------------------------------
2585 * Local variables:
2586 * c-basic-offset: 8
2587 * End:
2588 */
2589