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