Debian Codegraph @

drivers/mga_vid.c

raw
1 // YUY2 support (see config.format) added by A'rpi/ESP-team
2 // double buffering added by A'rpi/ESP-team
3 // read at vertical refresh time & partial SMP safeness by [email protected]
4
5
6 // Set this value, if autodetection fails! (video ram size in megabytes)
7 // #define MGA_MEMORY_SIZE 16
8
9 // for reading timestamp at vertical refresh
10 #define MGA_ALLOW_IRQ
11
12 #define MGA_VSYNC_POS 2
13
14 /*
15 *
16 * mga_vid.c
17 *
18 * Copyright (C) 1999 Aaron Holtzman
19 *
20 * Module skeleton based on gutted agpgart module by Jeff Hartmann
21 * <[email protected]>
22 *
23 * Matrox MGA G200/G400 YUV Video Interface module Version 0.1.0
24 *
25 * [email protected] - added support for sleeping read - wakes task at
26 * vertical refresh time - seems to be useful enough
27 * for clean Xv X11 support
28 *
29 * BES == Back End Scaler
30 *
31 * This software has been released under the terms of the GNU Public
32 * license. See http://www.gnu.org/copyleft/gpl.html for details.
33 */
34
35 //It's entirely possible this major conflicts with something else
36 /* mknod /dev/mga_vid c 178 0 */
37
38 #include <linux/config.h>
39 #include <linux/version.h>
40 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/mm.h>
45 #include <linux/string.h>
46 #include <linux/errno.h>
47 #include <linux/pci.h>
48 #include <linux/ioport.h>
49 #include <linux/init.h>
50 #include <linux/interrupt.h>
51
52 #ifdef CONFIG_MTRR
53 #include <asm/mtrr.h>
54 #endif
55
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
58 #include <asm/io.h>
59
60 #include "mga_vid.h"
61
62 #define TRUE 1
63 #define FALSE 0
64
65 #define MGA_VID_MAJOR 178
66
67 //#define MGA_VIDMEM_SIZE mga_ram_size
68
69 #ifndef PCI_DEVICE_ID_MATROX_G200_PCI
70 #define PCI_DEVICE_ID_MATROX_G200_PCI 0x0520
71 #endif
72
73 #ifndef PCI_DEVICE_ID_MATROX_G200_AGP
74 #define PCI_DEVICE_ID_MATROX_G200_AGP 0x0521
75 #endif
76
77 #ifndef PCI_DEVICE_ID_MATROX_G400
78 #define PCI_DEVICE_ID_MATROX_G400 0x0525
79 #endif
80
81 MODULE_AUTHOR("Aaron Holtzman <[email protected]>,"
82 "A'rpi/ESP-team Arpad Gereoffy <[email protected]>,"
83 "Zdenek Kabelac <[email protected]>");
84
85 typedef struct bes_registers_s
86 {
87 //BES Control
88 uint32_t besctl;
89 //BES Global control
90 uint32_t besglobctl;
91 //Luma control (brightness and contrast)
92 uint32_t beslumactl;
93 //Line pitch
94 uint32_t bespitch;
95
96 //Buffer A-1 Chroma 3 plane org
97 uint32_t besa1c3org;
98 //Buffer A-1 Chroma org
99 uint32_t besa1corg;
100 //Buffer A-1 Luma org
101 uint32_t besa1org;
102
103 //Buffer A-2 Chroma 3 plane org
104 uint32_t besa2c3org;
105 //Buffer A-2 Chroma org
106 uint32_t besa2corg;
107 //Buffer A-2 Luma org
108 uint32_t besa2org;
109
110 //Buffer B-1 Chroma 3 plane org
111 uint32_t besb1c3org;
112 //Buffer B-1 Chroma org
113 uint32_t besb1corg;
114 //Buffer B-1 Luma org
115 uint32_t besb1org;
116
117 //Buffer B-2 Chroma 3 plane org
118 uint32_t besb2c3org;
119 //Buffer B-2 Chroma org
120 uint32_t besb2corg;
121 //Buffer B-2 Luma org
122 uint32_t besb2org;
123
124 //BES Horizontal coord
125 uint32_t beshcoord;
126 //BES Horizontal inverse scaling [5.14]
127 uint32_t beshiscal;
128 //BES Horizontal source start [10.14] (for scaling)
129 uint32_t beshsrcst;
130 //BES Horizontal source ending [10.14] (for scaling)
131 uint32_t beshsrcend;
132 //BES Horizontal source last
133 uint32_t beshsrclst;
134
135
136 //BES Vertical coord
137 uint32_t besvcoord;
138 //BES Vertical inverse scaling [5.14]
139 uint32_t besviscal;
140 //BES Field 1 vertical source last position
141 uint32_t besv1srclst;
142 //BES Field 1 weight start
143 uint32_t besv1wght;
144 //BES Field 2 vertical source last position
145 uint32_t besv2srclst;
146 //BES Field 2 weight start
147 uint32_t besv2wght;
148
149 } bes_registers_t;
150
151 static bes_registers_t regs;
152 static uint32_t mga_vid_in_use = 0;
153 static uint32_t is_g400 = 0;
154 static uint32_t vid_src_ready = 0;
155 static uint32_t vid_overlay_on = 0;
156
157 static uint8_t *mga_mmio_base = 0;
158 static uint32_t mga_mem_base = 0;
159
160 static int mga_src_base = 0; // YUV buffer position in video memory
161
162 static uint32_t mga_ram_size = 0; // how much megabytes videoram we have
163
164 //static int mga_force_memsize = 0;
165
166 MODULE_PARM(mga_ram_size, "i");
167
168 static struct pci_dev *pci_dev;
169
170 static mga_vid_config_t mga_config;
171
172 static int mga_irq = -1;
173
174 static unsigned long irq_counter = 0;
175
176 static unsigned long irq_per_sec = 0;
177
178 static unsigned long last_jiffies = 0;
179
180 static struct tasklet_struct tasklet;
181
182 static spinlock_t mga_vid_spinlock = SPIN_LOCK_UNLOCKED;
183
184 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 3, 0)
185 static struct wait_queue *wait;
186 #else
187 static wait_queue_head_t wait;
188 #endif
189
190
191
192 //All register offsets are converted to word aligned offsets (32 bit)
193 //because we want all our register accesses to be 32 bits
194 #define VCOUNT 0x1e20
195
196 #define PALWTADD 0x3c00 // Index register for X_DATAREG port
197 #define X_DATAREG 0x3c0a
198
199 #define XMULCTRL 0x19
200 #define BPP_8 0x00
201 #define BPP_15 0x01
202 #define BPP_16 0x02
203 #define BPP_24 0x03
204 #define BPP_32_DIR 0x04
205 #define BPP_32_PAL 0x07
206
207 #define XCOLMSK 0x40
208 #define X_COLKEY 0x42
209 #define XKEYOPMODE 0x51
210 #define XCOLMSK0RED 0x52
211 #define XCOLMSK0GREEN 0x53
212 #define XCOLMSK0BLUE 0x54
213 #define XCOLKEY0RED 0x55
214 #define XCOLKEY0GREEN 0x56
215 #define XCOLKEY0BLUE 0x57
216
217 // Backend Scaler registers
218 #define BESCTL 0x3d20
219 #define BESGLOBCTL 0x3dc0
220 #define BESLUMACTL 0x3d40
221 #define BESPITCH 0x3d24
222
223 #define BESA1C3ORG 0x3d60
224 #define BESA1CORG 0x3d10
225 #define BESA1ORG 0x3d00
226
227 #define BESA2C3ORG 0x3d64
228 #define BESA2CORG 0x3d14
229 #define BESA2ORG 0x3d04
230
231 #define BESB1C3ORG 0x3d68
232 #define BESB1CORG 0x3d18
233 #define BESB1ORG 0x3d08
234
235 #define BESB2C3ORG 0x3d6C
236 #define BESB2CORG 0x3d1C
237 #define BESB2ORG 0x3d0C
238
239 #define BESHCOORD 0x3d28
240 #define BESHISCAL 0x3d30
241 #define BESHSRCEND 0x3d3C
242 #define BESHSRCLST 0x3d50
243 #define BESHSRCST 0x3d38
244 #define BESV1WGHT 0x3d48
245 #define BESV2WGHT 0x3d4c
246 #define BESV1SRCLST 0x3d54
247 #define BESV2SRCLST 0x3d58
248 #define BESVISCAL 0x3d34
249 #define BESVCOORD 0x3d2c
250 #define BESSTATUS 0x3dc4
251
252 #define CRTCX 0x1fd4
253 #define CRTCD 0x1fd5
254 #define IEN 0x1e1c
255 #define ICLEAR 0x1e18
256 #define STATUS 0x1e14
257
258 static int mga_next_frame=0;
259
260 static void mga_vid_frame_sel(int frame)
261 {
262 if ( mga_irq != -1 ) {
263 mga_next_frame=frame;
264 } else {
265
266 //we don't need the vcount protection as we're only hitting
267 //one register (and it doesn't seem to be double buffered)
268 regs.besctl = (regs.besctl & ~0x07000000) + (frame << 25);
269 writel( regs.besctl, mga_mmio_base + BESCTL );
270
271 // writel( regs.besglobctl + ((readl(mga_mmio_base + VCOUNT)+2)<<16),
272 writel( regs.besglobctl + (MGA_VSYNC_POS<<16),
273 mga_mmio_base + BESGLOBCTL);
274
275 }
276 }
277
278
279 static void mga_vid_write_regs(void)
280 {
281 //Make sure internal registers don't get updated until we're done
282 writel( (readl(mga_mmio_base + VCOUNT)-1)<<16,
283 mga_mmio_base + BESGLOBCTL);
284
285 // color or coordinate keying
286 writeb( XKEYOPMODE, mga_mmio_base + PALWTADD);
287 writeb( mga_config.colkey_on, mga_mmio_base + X_DATAREG);
288 if ( mga_config.colkey_on )
289 {
290 uint32_t r=0, g=0, b=0;
291
292 writeb( XMULCTRL, mga_mmio_base + PALWTADD);
293 switch (readb (mga_mmio_base + X_DATAREG))
294 {
295 case BPP_8:
296 /* Need to look up the color index, just using
297 color 0 for now. */
298 break;
299
300 case BPP_15:
301 r = mga_config.colkey_red >> 3;
302 g = mga_config.colkey_green >> 3;
303 b = mga_config.colkey_blue >> 3;
304 break;
305
306 case BPP_16:
307 r = mga_config.colkey_red >> 3;
308 g = mga_config.colkey_green >> 2;
309 b = mga_config.colkey_blue >> 3;
310 break;
311
312 case BPP_24:
313 case BPP_32_DIR:
314 case BPP_32_PAL:
315 r = mga_config.colkey_red;
316 g = mga_config.colkey_green;
317 b = mga_config.colkey_blue;
318 break;
319 }
320
321 // Disable color keying on alpha channel
322 writeb( XCOLMSK, mga_mmio_base + PALWTADD);
323 writeb( 0x00, mga_mmio_base + X_DATAREG);
324 writeb( X_COLKEY, mga_mmio_base + PALWTADD);
325 writeb( 0x00, mga_mmio_base + X_DATAREG);
326
327 // Set up color key registers
328 writeb( XCOLKEY0RED, mga_mmio_base + PALWTADD);
329 writeb( r, mga_mmio_base + X_DATAREG);
330 writeb( XCOLKEY0GREEN, mga_mmio_base + PALWTADD);
331 writeb( g, mga_mmio_base + X_DATAREG);
332 writeb( XCOLKEY0BLUE, mga_mmio_base + PALWTADD);
333 writeb( b, mga_mmio_base + X_DATAREG);
334
335 // Set up color key mask registers
336 writeb( XCOLMSK0RED, mga_mmio_base + PALWTADD);
337 writeb( 0xff, mga_mmio_base + X_DATAREG);
338 writeb( XCOLMSK0GREEN, mga_mmio_base + PALWTADD);
339 writeb( 0xff, mga_mmio_base + X_DATAREG);
340 writeb( XCOLMSK0BLUE, mga_mmio_base + PALWTADD);
341 writeb( 0xff, mga_mmio_base + X_DATAREG);
342 }
343
344 // Backend Scaler
345 writel( regs.besctl, mga_mmio_base + BESCTL);
346 if(is_g400)
347 writel( regs.beslumactl, mga_mmio_base + BESLUMACTL);
348 writel( regs.bespitch, mga_mmio_base + BESPITCH);
349
350 writel( regs.besa1org, mga_mmio_base + BESA1ORG);
351 writel( regs.besa1corg, mga_mmio_base + BESA1CORG);
352 writel( regs.besa2org, mga_mmio_base + BESA2ORG);
353 writel( regs.besa2corg, mga_mmio_base + BESA2CORG);
354 writel( regs.besb1org, mga_mmio_base + BESB1ORG);
355 writel( regs.besb1corg, mga_mmio_base + BESB1CORG);
356 writel( regs.besb2org, mga_mmio_base + BESB2ORG);
357 writel( regs.besb2corg, mga_mmio_base + BESB2CORG);
358 if(is_g400)
359 {
360 writel( regs.besa1c3org, mga_mmio_base + BESA1C3ORG);
361 writel( regs.besa2c3org, mga_mmio_base + BESA2C3ORG);
362 writel( regs.besb1c3org, mga_mmio_base + BESB1C3ORG);
363 writel( regs.besb2c3org, mga_mmio_base + BESB2C3ORG);
364 }
365
366 writel( regs.beshcoord, mga_mmio_base + BESHCOORD);
367 writel( regs.beshiscal, mga_mmio_base + BESHISCAL);
368 writel( regs.beshsrcst, mga_mmio_base + BESHSRCST);
369 writel( regs.beshsrcend, mga_mmio_base + BESHSRCEND);
370 writel( regs.beshsrclst, mga_mmio_base + BESHSRCLST);
371
372 writel( regs.besvcoord, mga_mmio_base + BESVCOORD);
373 writel( regs.besviscal, mga_mmio_base + BESVISCAL);
374
375 writel( regs.besv1srclst, mga_mmio_base + BESV1SRCLST);
376 writel( regs.besv1wght, mga_mmio_base + BESV1WGHT);
377 writel( regs.besv2srclst, mga_mmio_base + BESV2SRCLST);
378 writel( regs.besv2wght, mga_mmio_base + BESV2WGHT);
379
380 //update the registers somewhere between 1 and 2 frames from now.
381 writel( regs.besglobctl + ((readl(mga_mmio_base + VCOUNT)+2)<<16),
382 mga_mmio_base + BESGLOBCTL);
383
384 #if 0
385 printk(KERN_DEBUG "mga_vid: wrote BES registers\n");
386 printk(KERN_DEBUG "mga_vid: BESCTL = 0x%08x\n",
387 readl(mga_mmio_base + BESCTL));
388 printk(KERN_DEBUG "mga_vid: BESGLOBCTL = 0x%08x\n",
389 readl(mga_mmio_base + BESGLOBCTL));
390 printk(KERN_DEBUG "mga_vid: BESSTATUS= 0x%08x\n",
391 readl(mga_mmio_base + BESSTATUS));
392 #endif
393 }
394
395 static int mga_vid_set_config(mga_vid_config_t *config)
396 {
397 int x, y, sw, sh, dw, dh;
398 int besleft, bestop, ifactor, ofsleft, ofstop, baseadrofs, weight, weights;
399 int frame_size=config->frame_size;
400 x = config->x_org;
401 y = config->y_org;
402 sw = config->src_width;
403 sh = config->src_height;
404 dw = config->dest_width;
405 dh = config->dest_height;
406
407 printk(KERN_DEBUG "mga_vid: Setting up a %dx%d+%d+%d video window (src %dx%d) format %X\n",
408 dw, dh, x, y, sw, sh, config->format);
409
410 //FIXME check that window is valid and inside desktop
411
412 //FIXME figure out a better way to allocate memory on card
413 //allocate 2 megs
414 //mga_src_base = mga_mem_base + (MGA_VIDMEM_SIZE-2) * 0x100000;
415 //mga_src_base = (MGA_VIDMEM_SIZE-3) * 0x100000;
416
417
418 //Setup the BES registers for a three plane 4:2:0 video source
419
420 switch(config->format){
421 case MGA_VID_FORMAT_YV12:
422 regs.besctl = 1 // BES enabled
423 + (0<<6) // even start polarity
424 + (1<<10) // x filtering enabled
425 + (1<<11) // y filtering enabled
426 + (1<<16) // chroma upsampling
427 + (1<<17) // 4:2:0 mode
428 + (1<<18); // dither enabled
429
430 if(is_g400)
431 {
432 //zoom disabled, zoom filter disabled, 420 3 plane format, proc amp
433 //disabled, rgb mode disabled
434 regs.besglobctl = (1<<5);
435 }
436 else
437 {
438 //zoom disabled, zoom filter disabled, Cb samples in 0246, Cr
439 //in 1357, BES register update on besvcnt
440 regs.besglobctl = 0;
441 }
442 break;
443
444 case MGA_VID_FORMAT_YUY2:
445 regs.besctl = 1 // BES enabled
446 + (0<<6) // even start polarity
447 + (1<<10) // x filtering enabled
448 + (1<<11) // y filtering enabled
449 + (1<<16) // chroma upsampling
450 + (0<<17) // 4:2:2 mode
451 + (1<<18); // dither enabled
452
453 regs.besglobctl = 0; // YUY2 format selected
454 break;
455 default:
456 printk(KERN_ERR "mga_vid: Unsupported pixel format: 0x%X\n",config->format);
457 return -1;
458 }
459
460
461 //Disable contrast and brightness control
462 regs.besglobctl = (1<<5) + (1<<7);
463 regs.beslumactl = (0x7f << 16) + (0x80<<0);
464 regs.beslumactl = 0x80<<0;
465
466 //Setup destination window boundaries
467 besleft = x > 0 ? x : 0;
468 bestop = y > 0 ? y : 0;
469 regs.beshcoord = (besleft<<16) + (x + dw-1);
470 regs.besvcoord = (bestop<<16) + (y + dh-1);
471
472 //Setup source dimensions
473 regs.beshsrclst = (sw - 1) << 16;
474 regs.bespitch = (sw + 31) & ~31 ;
475
476 //Setup horizontal scaling
477 ifactor = ((sw-1)<<14)/(dw-1);
478 ofsleft = besleft - x;
479
480 regs.beshiscal = ifactor<<2;
481 regs.beshsrcst = (ofsleft*ifactor)<<2;
482 regs.beshsrcend = regs.beshsrcst + (((dw - ofsleft - 1) * ifactor) << 2);
483
484 //Setup vertical scaling
485 ifactor = ((sh-1)<<14)/(dh-1);
486 ofstop = bestop - y;
487
488 regs.besviscal = ifactor<<2;
489
490 baseadrofs = ((ofstop*regs.besviscal)>>16)*regs.bespitch;
491 //frame_size = ((sw + 31) & ~31) * sh + (((sw + 31) & ~31) * sh) / 2;
492 regs.besa1org = (uint32_t) mga_src_base + baseadrofs;
493 regs.besa2org = (uint32_t) mga_src_base + baseadrofs + 1*frame_size;
494 regs.besb1org = (uint32_t) mga_src_base + baseadrofs + 2*frame_size;
495 regs.besb2org = (uint32_t) mga_src_base + baseadrofs + 3*frame_size;
496
497 if(config->format==MGA_VID_FORMAT_YV12){
498 // planar YUV frames:
499 if (is_g400)
500 baseadrofs = (((ofstop*regs.besviscal)/4)>>16)*regs.bespitch;
501 else
502 baseadrofs = (((ofstop*regs.besviscal)/2)>>16)*regs.bespitch;
503
504 regs.besa1corg = (uint32_t) mga_src_base + baseadrofs + regs.bespitch * sh ;
505 regs.besa2corg = (uint32_t) mga_src_base + baseadrofs + 1*frame_size + regs.bespitch * sh;
506 regs.besb1corg = (uint32_t) mga_src_base + baseadrofs + 2*frame_size + regs.bespitch * sh;
507 regs.besb2corg = (uint32_t) mga_src_base + baseadrofs + 3*frame_size + regs.bespitch * sh;
508 regs.besa1c3org = regs.besa1corg + ((regs.bespitch * sh) / 4);
509 regs.besa2c3org = regs.besa2corg + ((regs.bespitch * sh) / 4);
510 regs.besb1c3org = regs.besb1corg + ((regs.bespitch * sh) / 4);
511 regs.besb2c3org = regs.besb2corg + ((regs.bespitch * sh) / 4);
512 }
513
514 weight = ofstop * (regs.besviscal >> 2);
515 weights = weight < 0 ? 1 : 0;
516 regs.besv2wght = regs.besv1wght = (weights << 16) + ((weight & 0x3FFF) << 2);
517 regs.besv2srclst = regs.besv1srclst = sh - 1 - (((ofstop * regs.besviscal) >> 16) & 0x03FF);
518
519 mga_vid_write_regs();
520 return 0;
521 }
522
523 #ifdef MGA_ALLOW_IRQ
524
525 #define vlinelen (1 << 5)
526 #define vlineiclr (1 << 5)
527 #define c2vlinelen (1 << 9)
528 #define c2vlineiclr (1 << 9)
529
530 static void mga_enable_irq(int cmd)
531 {
532 long cc, cl = 0;
533 unsigned long flags;
534
535 spin_lock_irqsave(&mga_vid_spinlock, flags);
536 cc = readl(mga_mmio_base + IEN) & ~(vlinelen | c2vlinelen);
537 //printk(KERN_ALERT "*** IRQREG = 0x%x\n", cc);
538 if (cmd == MGA_VID_IRQ_ON)
539 {
540 // clear any pending interrupts
541 cl = vlineiclr;
542 cc = vlinelen;
543 }
544 else if (cmd == MGA_VID_IRQ2_ON)
545 {
546 cl = c2vlineiclr;
547 cc = c2vlinelen;
548 }
549 writel(cl, mga_mmio_base + ICLEAR);
550 writel(cc, mga_mmio_base + IEN);
551
552 #if 0
553 // this will currently stops when the resolution is changed
554 writeb(0x11, mga_mmio_base + CRTCX);
555 writeb(0x20, mga_mmio_base + CRTCD ); /* clear 0, enable off */
556
557 writeb(0x00, mga_mmio_base + CRTCD ); /* enable on */
558 writeb(0x10, mga_mmio_base + CRTCD ); /* clear = 1 */
559 #endif
560 //writel( regs.besglobctl , mga_mmio_base + BESGLOBCTL);
561 spin_unlock_irqrestore(&mga_vid_spinlock, flags);
562
563 last_jiffies = 0;
564 }
565
566 static void mga_disable_irq(int cmd)
567 {
568 long cc, cl = 0;
569 unsigned long flags;
570
571 spin_lock_irqsave(&mga_vid_spinlock, flags);
572
573 cc = readl(mga_mmio_base + IEN);
574 if (cmd == MGA_VID_IRQ_OFF || cmd == -1)
575 {
576 cl |= vlineiclr;
577 cc &= ~vlinelen;
578 }
579 else if (cmd == MGA_VID_IRQ2_OFF || cmd == -1)
580 {
581 cl |= c2vlineiclr;
582 cc &= ~c2vlinelen;
583 }
584 writel(cl, mga_mmio_base + ICLEAR);
585 writel(cc, mga_mmio_base + IEN);
586 #if 0
587 writeb(0x11, mga_mmio_base + CRTCX);
588 writeb(0x20, mga_mmio_base + CRTCD ); /* clear 0, enable off */
589 #endif
590 spin_unlock_irqrestore(&mga_vid_spinlock, flags);
591 }
592
593 // executed after interrupt
594 static void mga_tasklet(unsigned long refdata)
595 {
596 static unsigned long first_jiffies = 0;
597 static unsigned long sec_jiffies;
598 static unsigned long sec_irq_counter = 0;
599
600 if ((jiffies - last_jiffies) > 2)
601 first_jiffies = 0;
602
603 last_jiffies = jiffies;
604
605 if (first_jiffies == 0) {
606 sec_jiffies = first_jiffies = last_jiffies;
607 sec_irq_counter = irq_per_sec = irq_counter = 0;
608 } else {
609 irq_counter++;
610
611 if ((last_jiffies - sec_jiffies) > HZ)
612 {
613 irq_per_sec = irq_counter - sec_irq_counter;
614 sec_irq_counter = irq_counter;
615 sec_jiffies = last_jiffies;
616 }
617 //if (! (d % HZ))
618 // printk(KERN_DEBUG "%ld %ld\n", irq_counter, irq_per_sec);
619 }
620
621 #if 0
622 if ((irq_counter % 500) == 0) {
623 printk(KERN_DEBUG "mga_vid: irq counter = %ld\n", irq_counter);
624 }
625 #endif
626
627 wake_up_interruptible(&wait);
628 }
629
630 static void mga_handle_irq(int irq, void *dev_id, struct pt_regs *pregs) {
631 // static int frame=0;
632 // if ( ! mga_enabled_flag ) return;
633
634 // printk(KERN_DEBUG "vcount = %d\n",readl(mga_mmio_base + VCOUNT));
635
636 //printk("mga_interrupt #%d\n", irq);
637
638 if ( irq != -1 ) {
639 unsigned long flags;
640 long cc, cl = 0;
641
642 spin_lock_irqsave(&mga_vid_spinlock, flags);
643 cc = readl(mga_mmio_base + STATUS);
644 if (cc & vlineiclr)
645 {
646 #if 0
647 writeb( 0x11, mga_mmio_base + CRTCX);
648 writeb( 0, mga_mmio_base + CRTCD );
649 writeb( 0x10, mga_mmio_base + CRTCD );
650 #endif
651 cl |= vlineiclr;
652 }
653 if (cc & c2vlineiclr)
654 cl |= c2vlineiclr;
655
656 if (cl)
657 writel(cl, mga_mmio_base + ICLEAR);
658
659 spin_unlock_irqrestore(&mga_vid_spinlock, flags);
660
661 if (cl)
662 tasklet_hi_schedule(&tasklet);
663 // debug_irqcnt++;
664 }
665
666 // if ( debug_irqignore ) {
667 // debug_irqignore = 0;
668
669 /*
670 if ( mga_conf_deinterlace ) {
671 if ( mga_first_field ) {
672 // printk("mga_interrupt first field\n");
673 if ( syncfb_interrupt() )
674 mga_first_field = 0;
675 } else {
676 // printk("mga_interrupt second field\n");
677 mga_select_buffer( mga_current_field | 2 );
678 mga_first_field = 1;
679 }
680 } else {
681 syncfb_interrupt();
682 }
683 */
684
685 // frame=(frame+1)&1;
686 //regs.besctl = (regs.besctl & ~0x07000000) + (mga_next_frame << 25);
687 //writel( regs.besctl, mga_mmio_base + BESCTL );
688
689
690 // } else {
691 // debug_irqignore = 1;
692 // }
693
694 // writel( regs.besglobctl, mga_mmio_base + BESGLOBCTL);
695 }
696
697 #endif
698
699 static int mga_vid_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
700 {
701 int frame;
702
703 switch(cmd)
704 {
705 case MGA_VID_CONFIG:
706 //FIXME remove
707 // printk(KERN_DEBUG "vcount = %d\n",readl(mga_mmio_base + VCOUNT));
708 printk(KERN_DEBUG "mga_mmio_base = %p\n",mga_mmio_base);
709 printk(KERN_DEBUG "mga_mem_base = %08X\n",mga_mem_base);
710 //FIXME remove
711
712 printk(KERN_DEBUG "mga_vid: Received configuration\n");
713
714 if(copy_from_user(&mga_config,(mga_vid_config_t*) arg,sizeof(mga_vid_config_t)))
715 {
716 printk(KERN_ERR "mga_vid: failed copy from userspace\n");
717 return(-EFAULT);
718 }
719 if(mga_config.version != MGA_VID_VERSION){
720 printk(KERN_ERR "mga_vid: incompatible version! driver: %X requested: %X\n",MGA_VID_VERSION,mga_config.version);
721 return(-EFAULT);
722 }
723
724 if(mga_config.frame_size==0 || mga_config.frame_size>1024*768*2){
725 printk(KERN_ERR "mga_vid: illegal frame_size: %d\n",mga_config.frame_size);
726 return(-EFAULT);
727 }
728
729 if(mga_config.num_frames<1 || mga_config.num_frames>4){
730 printk(KERN_ERR "mga_vid: illegal num_frames: %d\n",mga_config.num_frames);
731 return(-EFAULT);
732 }
733
734 mga_src_base = (mga_ram_size*0x100000-mga_config.num_frames*mga_config.frame_size);
735 if(mga_src_base<0){
736 printk(KERN_ERR "mga_vid: not enough memory for frames!\n");
737 return(-EFAULT);
738 }
739 mga_src_base &= (~0xFFFF); // 64k boundary
740 printk(KERN_DEBUG "mga YUV buffer base: 0x%X\n", mga_src_base);
741
742 if (is_g400)
743 mga_config.card_type = MGA_G400;
744 else
745 mga_config.card_type = MGA_G200;
746
747 mga_config.ram_size = mga_ram_size;
748
749 if (copy_to_user((mga_vid_config_t *) arg, &mga_config, sizeof(mga_vid_config_t)))
750 {
751 printk(KERN_ERR "mga_vid: failed copy to userspace\n");
752 return(-EFAULT);
753 }
754 return mga_vid_set_config(&mga_config);
755
756 case MGA_VID_ON:
757 printk(KERN_DEBUG "mga_vid: Video ON\n");
758 vid_src_ready = 1;
759 if(vid_overlay_on)
760 {
761 regs.besctl |= 1;
762 mga_vid_write_regs();
763 }
764 #ifdef MGA_ALLOW_IRQ
765 if ( mga_irq != -1 ) mga_enable_irq(0);
766 #endif
767 mga_next_frame=0;
768 break;
769
770 case MGA_VID_OFF:
771 printk(KERN_DEBUG "mga_vid: Video OFF\n");
772 vid_src_ready = 0;
773 #ifdef MGA_ALLOW_IRQ
774 if ( mga_irq != -1 ) mga_disable_irq(0);
775 #endif
776 regs.besctl &= ~1;
777 mga_vid_write_regs();
778 break;
779
780 case MGA_VID_FSEL:
781 if(copy_from_user(&frame,(int *) arg,sizeof(int)))
782 {
783 printk(KERN_ERR "mga_vid: FSEL failed copy from userspace\n");
784 return(-EFAULT);
785 }
786
787 mga_vid_frame_sel(frame);
788 break;
789
790 case MGA_VID_IRQ_ON:
791 case MGA_VID_IRQ2_ON:
792 printk(KERN_DEBUG "mga_vid: Video IRQ ON\n");
793 #ifdef MGA_ALLOW_IRQ
794 if ( mga_irq != -1 ) mga_enable_irq(cmd);
795 #endif
796 break;
797 case MGA_VID_IRQ_OFF:
798 case MGA_VID_IRQ2_OFF:
799 printk(KERN_DEBUG "mga_vid: Video IRQ OFF\n");
800 #ifdef MGA_ALLOW_IRQ
801 if ( mga_irq != -1 ) mga_disable_irq(cmd);
802 #endif
803 break;
804
805 default:
806 printk(KERN_ERR "mga_vid: Invalid ioctl\n");
807 return (-EINVAL);
808 }
809
810 return 0;
811 }
812
813 static int mga_vid_find_card(void)
814 {
815 struct pci_dev *dev = NULL;
816 unsigned int card_option;//, temp;
817
818 if((dev = pci_find_device(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G400, NULL)))
819 {
820 is_g400 = 1;
821 printk(KERN_INFO "mga_vid: Found MGA G400/G450\n");
822 }
823 else if((dev = pci_find_device(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_AGP, NULL)))
824 {
825 is_g400 = 0;
826 printk(KERN_INFO "mga_vid: Found MGA G200 AGP\n");
827 }
828 else if((dev = pci_find_device(PCI_VENDOR_ID_MATROX, PCI_DEVICE_ID_MATROX_G200_PCI, NULL)))
829 {
830 is_g400 = 0;
831 printk(KERN_INFO "mga_vid: Found MGA G200 PCI\n");
832 }
833 else
834 {
835 printk(KERN_ERR "mga_vid: No supported cards found\n");
836 return FALSE;
837 }
838
839 pci_dev = dev;
840
841 mga_irq = pci_dev->irq;
842
843 #if LINUX_VERSION_CODE >= 0x020300
844 mga_mmio_base = ioremap_nocache(dev->resource[1].start,0x4000);
845 mga_mem_base = dev->resource[0].start;
846 #else
847 mga_mmio_base = ioremap_nocache(dev->base_address[1] & PCI_BASE_ADDRESS_MEM_MASK,0x4000);
848 mga_mem_base = dev->base_address[0] & PCI_BASE_ADDRESS_MEM_MASK;
849 #endif
850 printk(KERN_INFO "mga_vid: MMIO at 0x%p IRQ: %d framebuffer: 0x%08X\n", mga_mmio_base, mga_irq, mga_mem_base);
851
852 pci_read_config_dword(dev, 0x40, &card_option);
853 printk(KERN_INFO "mga_vid: OPTION word: 0x%08X mem: 0x%02X %s\n", card_option,
854 (card_option>>10)&0x17, ((card_option>>14)&1)?"SGRAM":"SDRAM");
855
856 // temp = (card_option >> 10) & 0x17;
857
858 if (mga_ram_size) {
859 printk(KERN_INFO "mga_vid: RAMSIZE forced to %d MB\n", mga_ram_size);
860 } else {
861
862 #ifdef MGA_MEMORY_SIZE
863 mga_ram_size = MGA_MEMORY_SIZE;
864 printk(KERN_INFO "mga_vid: hard-coded RAMSIZE is %d MB\n", (unsigned int) mga_ram_size);
865
866 #else
867 if (is_g400){
868 switch((card_option>>10)&0x17){
869 // SDRAM:
870 case 0x00:
871 case 0x04: mga_ram_size = 16; break;
872 case 0x03: mga_ram_size = 32; break;
873 // SGRAM:
874 case 0x10:
875 case 0x14: mga_ram_size = 32; break;
876 case 0x11:
877 case 0x12: mga_ram_size = 16; break;
878 default:
879 mga_ram_size = 16;
880 printk(KERN_INFO "mga_vid: Couldn't detect RAMSIZE, assuming 16MB!");
881 }
882 }else{
883 switch((card_option>>11)&1){
884 case 0: mga_ram_size = 8; break;
885 default: mga_ram_size = 16;
886 }
887 }
888 #if 0
889 // printk("List resources -----------\n");
890 for(temp=0;temp<DEVICE_COUNT_RESOURCE;temp++){
891 struct resource *res=&pci_dev->resource[temp];
892 if(res->flags){
893 int size=(1+res->end-res->start)>>20;
894 printk(KERN_DEBUG "res %d: start: 0x%X end: 0x%X (%d MB) flags=0x%X\n",temp,res->start,res->end,size,res->flags);
895 if(res->flags&(IORESOURCE_MEM|IORESOURCE_PREFETCH)){
896 if(size>mga_ram_size && size<=64) mga_ram_size=size;
897 }
898 }
899 }
900 #endif
901 printk(KERN_INFO "mga_vid: detected RAMSIZE is %d MB\n", (unsigned int) mga_ram_size);
902 #endif
903 }
904
905
906 #ifdef MGA_ALLOW_IRQ
907 if ( mga_irq != -1 ) {
908 int tmp = request_irq(mga_irq, mga_handle_irq, SA_INTERRUPT | SA_SHIRQ, "Syncfb Time Base", &mga_irq);
909 if ( tmp ) {
910 printk(KERN_INFO "syncfb (mga): cannot register irq %d (Err: %d)\n", mga_irq, tmp);
911 mga_irq=-1;
912 } else {
913 printk(KERN_DEBUG "syncfb (mga): registered irq %d\n", mga_irq);
914 }
915 } else {
916 printk(KERN_INFO "syncfb (mga): No valid irq was found\n");
917 mga_irq=-1;
918 }
919 #else
920 printk(KERN_INFO "syncfb (mga): IRQ disabled in mga_vid.c\n");
921 mga_irq=-1;
922 #endif
923
924 return TRUE;
925 }
926
927 static ssize_t mga_vid_read(struct file *file, char *buf, size_t count, loff_t *ppos)
928 {
929 #ifdef MGA_ALLOW_IRQ
930 if ( mga_irq == -1 )
931 return -EINVAL;
932
933 // when the video mode is switched we have to renable mga irq
934 interruptible_sleep_on_timeout(&wait, HZ/10);
935 if (signal_pending(current)) {
936 return -ERESTARTSYS;
937 }
938
939 // check for timeout here
940 if ((jiffies - last_jiffies) >= HZ/2) {
941 printk(KERN_INFO "mga_vid: timeout\n");
942 if (mga_irq != -1) {
943 mga_enable_irq(0);
944 }
945 }
946
947 if (count < 4)
948 return 0;
949
950 copy_to_user(buf, &irq_counter, 4);
951
952 if (count < 8)
953 return 4;
954
955 copy_to_user(buf + 4, &irq_per_sec, 4);
956
957 return 8;
958 #endif
959
960 return -EINVAL;
961 }
962
963 static ssize_t mga_vid_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
964 {
965 return -EINVAL;
966 }
967
968 static int mga_vid_mmap(struct file *file, struct vm_area_struct *vma)
969 {
970
971 printk(KERN_DEBUG "mga_vid: mapping video memory into userspace\n");
972 if(remap_page_range(vma->vm_start, mga_mem_base + mga_src_base,
973 vma->vm_end - vma->vm_start, vma->vm_page_prot))
974 {
975 printk(KERN_ERR "mga_vid: error mapping video memory\n");
976 return -EAGAIN;
977 }
978
979 return 0;
980 }
981
982 static int mga_vid_release(struct inode *inode, struct file *file)
983 {
984 //Close the window just in case
985 printk(KERN_DEBUG "mga_vid: Video OFF (release)\n");
986
987 vid_src_ready = 0;
988 if (regs.besctl & 1)
989 {
990 printk("write_regs()\n");
991 regs.besctl &= ~1;
992 mga_vid_write_regs();
993 }
994
995 #ifdef MGA_ALLOW_IRQ
996 if ( mga_irq != -1 ) mga_disable_irq(-1);
997 #endif
998 mga_vid_in_use = 0;
999
1000 MOD_DEC_USE_COUNT;
1001
1002 return 0;
1003 }
1004
1005 static long long mga_vid_lseek(struct file *file, long long offset, int origin)
1006 {
1007 return -ESPIPE;
1008 }
1009
1010 static int mga_vid_open(struct inode *inode, struct file *file)
1011 {
1012 int minor = MINOR(inode->i_rdev);
1013
1014 if(minor != 0)
1015 return(-ENXIO);
1016
1017 if(mga_vid_in_use == 1)
1018 return(-EBUSY);
1019
1020 mga_vid_in_use = 1;
1021 MOD_INC_USE_COUNT;
1022 return(0);
1023 }
1024
1025 #if LINUX_VERSION_CODE >= 0x020400
1026 static struct file_operations mga_vid_fops =
1027 {
1028 llseek: mga_vid_lseek,
1029 read: mga_vid_read,
1030 write: mga_vid_write,
1031 ioctl: mga_vid_ioctl,
1032 mmap: mga_vid_mmap,
1033 open: mga_vid_open,
1034 release: mga_vid_release
1035 };
1036 #else
1037 static struct file_operations mga_vid_fops =
1038 {
1039 mga_vid_lseek,
1040 mga_vid_read,
1041 mga_vid_write,
1042 NULL,
1043 NULL,
1044 mga_vid_ioctl,
1045 mga_vid_mmap,
1046 mga_vid_open,
1047 NULL,
1048 mga_vid_release
1049 };
1050 #endif
1051
1052
1053 /*
1054 * Main Initialization Function
1055 */
1056
1057 static int mga_vid_initialize(void)
1058 {
1059 mga_vid_in_use = 0;
1060
1061 // printk(KERN_INFO "Matrox MGA G200/G400 YUV Video interface v0.01 (c) Aaron Holtzman \n");
1062 printk(KERN_INFO "Matrox MGA G200/G400/G450 YUV Video interface v2.01 (c) Aaron Holtzman & A'rpi\n");
1063
1064 if (mga_ram_size) {
1065 if (mga_ram_size<4 || mga_ram_size>64) {
1066 printk(KERN_ERR "mga_vid: invalid RAMSIZE: %d MB\n", mga_ram_size);
1067 return -EINVAL;
1068 }
1069 }
1070
1071 if(register_chrdev(MGA_VID_MAJOR, "mga_vid", &mga_vid_fops))
1072 {
1073 printk(KERN_ERR "mga_vid: unable to get major: %d\n", MGA_VID_MAJOR);
1074 return -EIO;
1075 }
1076
1077 if (!mga_vid_find_card())
1078 {
1079 printk(KERN_ERR "mga_vid: no supported devices found\n");
1080 unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
1081 return -EINVAL;
1082 }
1083
1084 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 3, 0)
1085 init_waitqueue_head(&wait);
1086 #else
1087 init_waitqueue(&wait);
1088 #endif
1089 tasklet_init(&tasklet, mga_tasklet, (unsigned long) 0);
1090
1091 return 0;
1092 }
1093
1094 int init_module(void)
1095 {
1096 return mga_vid_initialize();
1097 }
1098
1099 void cleanup_module(void)
1100 {
1101
1102 #ifdef MGA_ALLOW_IRQ
1103 if ( mga_irq != -1)
1104 free_irq(mga_irq, &mga_irq);
1105 #endif
1106
1107 if(mga_mmio_base)
1108 iounmap(mga_mmio_base);
1109
1110 //FIXME turn off BES
1111 printk(KERN_INFO "mga_vid: Cleaning up module\n");
1112 unregister_chrdev(MGA_VID_MAJOR, "mga_vid");
1113 }