2 * stradis.c - stradis 4:2:2 mpeg decoder driver
4 * Stradis 4:2:2 MPEG-2 Decoder Driver
5 * Copyright (C) 1999 Nathan Laredo <laredo@gnu.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/version.h>
24 #include <linux/delay.h>
25 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/major.h>
29 #include <linux/malloc.h>
31 #include <linux/init.h>
32 #include <linux/poll.h>
33 #include <linux/pci.h>
34 #include <linux/signal.h>
36 #include <linux/ioport.h>
37 #include <asm/pgtable.h>
39 #include <linux/sched.h>
40 #include <asm/segment.h>
41 #include <asm/types.h>
42 #include <linux/types.h>
43 #include <linux/wrapper.h>
44 #include <linux/interrupt.h>
45 #include <asm/uaccess.h>
46 #include <linux/vmalloc.h>
47 #include <linux/videodev.h>
48 #include <linux/i2c-old.h>
51 #include "saa7146reg.h"
56 #define DEBUG(x) /* debug driver */
57 #undef IDEBUG(x) /* debug irq handler */
58 #undef MDEBUG(x) /* debug memory management */
62 static struct saa7146 saa7146s
[SAA7146_MAX
];
64 static int saa_num
= 0; /* number of SAA7146s in use */
66 #define nDebNormal 0x00480000
67 #define nDebNoInc 0x00480000
68 #define nDebVideo 0xd0480000
69 #define nDebAudio 0xd0400000
70 #define nDebDMA 0x02c80000
72 #define oDebNormal 0x13c80000
73 #define oDebNoInc 0x13c80000
74 #define oDebVideo 0xd1080000
75 #define oDebAudio 0xd1080000
76 #define oDebDMA 0x03080000
78 #define NewCard (saa->boardcfg[3])
79 #define ChipControl (saa->boardcfg[1])
80 #define NTSCFirstActive (saa->boardcfg[4])
81 #define PALFirstActive (saa->boardcfg[5])
82 #define NTSCLastActive (saa->boardcfg[54])
83 #define PALLastActive (saa->boardcfg[55])
84 #define Have2MB (saa->boardcfg[18] & 0x40)
85 #define HaveCS8420 (saa->boardcfg[18] & 0x04)
86 #define IBMMPEGCD20 (saa->boardcfg[18] & 0x20)
87 #define HaveCS3310 (saa->boardcfg[18] & 0x01)
88 #define CS3310MaxLvl ((saa->boardcfg[30] << 8) | saa->boardcfg[31])
89 #define HaveCS4341 (saa->boardcfg[40] == 2)
90 #define SDIType (saa->boardcfg[27])
91 #define CurrentMode (saa->boardcfg[2])
93 #define debNormal (NewCard ? nDebNormal : oDebNormal)
94 #define debNoInc (NewCard ? nDebNoInc : oDebNoInc)
95 #define debVideo (NewCard ? nDebVideo : oDebVideo)
96 #define debAudio (NewCard ? nDebAudio : oDebAudio)
97 #define debDMA (NewCard ? nDebDMA : oDebDMA)
100 int stradis_driver(void) /* for the benefit of ksymoops */
106 #ifdef USE_RESCUE_EEPROM_SDM275
107 static unsigned char rescue_eeprom
[64] = {
108 0x00,0x01,0x04,0x13,0x26,0x0f,0x10,0x00,0x00,0x00,0x43,0x63,0x22,0x01,0x29,0x15,0x73,0x00,0x1f, 'd', 'e', 'c', 'x', 'l', 'd', 'v', 'a',0x02,0x00,0x01,0x00,0xcc,0xa4,0x63,0x09,0xe2,0x10,0x00,0x0a,0x00,0x02,0x02, 'd', 'e', 'c', 'x', 'l', 'a',0x00,0x00,0x42,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
112 /* ----------------------------------------------------------------------- */
113 /* Hardware I2C functions */
114 static void I2CWipe(struct saa7146
*saa
)
117 /* set i2c to ~=100kHz, abort transfer, clear busy */
118 saawrite(0x600 | SAA7146_I2C_ABORT
, SAA7146_I2C_STATUS
);
119 saawrite((SAA7146_MC2_UPLD_I2C
<< 16) |
120 SAA7146_MC2_UPLD_I2C
, SAA7146_MC2
);
121 /* wait for i2c registers to be programmed */
122 for (i
= 0; i
< 1000 &&
123 !(saaread(SAA7146_MC2
) & SAA7146_MC2_UPLD_I2C
); i
++)
125 saawrite(0x600, SAA7146_I2C_STATUS
);
126 saawrite((SAA7146_MC2_UPLD_I2C
<< 16) |
127 SAA7146_MC2_UPLD_I2C
, SAA7146_MC2
);
128 /* wait for i2c registers to be programmed */
129 for (i
= 0; i
< 1000 &&
130 !(saaread(SAA7146_MC2
) & SAA7146_MC2_UPLD_I2C
); i
++)
132 saawrite(0x600, SAA7146_I2C_STATUS
);
133 saawrite((SAA7146_MC2_UPLD_I2C
<< 16) |
134 SAA7146_MC2_UPLD_I2C
, SAA7146_MC2
);
135 /* wait for i2c registers to be programmed */
136 for (i
= 0; i
< 1000 &&
137 !(saaread(SAA7146_MC2
) & SAA7146_MC2_UPLD_I2C
); i
++)
141 static int I2CRead(struct i2c_bus
*bus
, unsigned char addr
,
142 unsigned char subaddr
, int dosub
)
144 struct saa7146
*saa
= (struct saa7146
*) bus
->data
;
148 if (saaread(SAA7146_I2C_STATUS
) & 0x3c)
150 for (i
= 0; i
< 1000 &&
151 (saaread(SAA7146_I2C_STATUS
) & SAA7146_I2C_BUSY
); i
++)
156 saawrite(((addr
& 0xfe) << 24) | (((addr
| 1) & 0xff) << 8) |
157 ((subaddr
& 0xff) << 16) | 0xed, SAA7146_I2C_TRANSFER
);
159 saawrite(((addr
& 0xfe) << 24) | (((addr
| 1) & 0xff) << 16) |
160 0xf1, SAA7146_I2C_TRANSFER
);
161 saawrite((SAA7146_MC2_UPLD_I2C
<< 16) |
162 SAA7146_MC2_UPLD_I2C
, SAA7146_MC2
);
163 /* wait for i2c registers to be programmed */
164 for (i
= 0; i
< 1000 &&
165 !(saaread(SAA7146_MC2
) & SAA7146_MC2_UPLD_I2C
); i
++)
167 /* wait for valid data */
168 for (i
= 0; i
< 1000 &&
169 (saaread(SAA7146_I2C_STATUS
) & SAA7146_I2C_BUSY
); i
++)
171 if (saaread(SAA7146_I2C_STATUS
) & SAA7146_I2C_ERR
)
174 printk("i2c setup read timeout\n");
175 saawrite(0x41, SAA7146_I2C_TRANSFER
);
176 saawrite((SAA7146_MC2_UPLD_I2C
<< 16) |
177 SAA7146_MC2_UPLD_I2C
, SAA7146_MC2
);
178 /* wait for i2c registers to be programmed */
179 for (i
= 0; i
< 1000 &&
180 !(saaread(SAA7146_MC2
) & SAA7146_MC2_UPLD_I2C
); i
++)
182 /* wait for valid data */
183 for (i
= 0; i
< 1000 &&
184 (saaread(SAA7146_I2C_TRANSFER
) & SAA7146_I2C_BUSY
); i
++)
186 if (saaread(SAA7146_I2C_TRANSFER
) & SAA7146_I2C_ERR
)
189 printk("i2c read timeout\n");
190 return ((saaread(SAA7146_I2C_TRANSFER
) >> 24) & 0xff);
192 static int I2CReadOld(struct i2c_bus
*bus
, unsigned char addr
)
194 return I2CRead(bus
, addr
, 0, 0);
197 /* set both to write both bytes, reset it to write only b1 */
199 static int I2CWrite(struct i2c_bus
*bus
, unsigned char addr
, unsigned char b1
,
200 unsigned char b2
, int both
)
202 struct saa7146
*saa
= (struct saa7146
*) bus
->data
;
206 if (saaread(SAA7146_I2C_STATUS
) & 0x3c)
208 for (i
= 0; i
< 1000 &&
209 (saaread(SAA7146_I2C_STATUS
) & SAA7146_I2C_BUSY
); i
++)
213 data
= ((addr
& 0xfe) << 24) | ((b1
& 0xff) << 16);
215 data
|= ((b2
& 0xff) << 8) | 0xe5;
218 saawrite(data
, SAA7146_I2C_TRANSFER
);
219 saawrite((SAA7146_MC2_UPLD_I2C
<< 16) | SAA7146_MC2_UPLD_I2C
,
224 static void attach_inform(struct i2c_bus
*bus
, int id
)
226 struct saa7146
*saa
= (struct saa7146
*) bus
->data
;
229 DEBUG(printk(KERN_DEBUG
"stradis%d: i2c: device found=%02x\n", saa
->nr
, id
));
230 if (id
== 0xa0) { /* we have rev2 or later board, fill in info */
231 for (i
= 0; i
< 64; i
++)
232 saa
->boardcfg
[i
] = I2CRead(bus
, 0xa0, i
, 1);
233 #ifdef USE_RESCUE_EEPROM_SDM275
234 if (saa
->boardcfg
[0] != 0) {
235 printk("stradis%d: WARNING: EEPROM STORED VALUES HAVE BEEN IGNORED\n", saa
->nr
);
236 for (i
= 0; i
< 64; i
++)
237 saa
->boardcfg
[i
] = rescue_eeprom
[i
];
240 printk("stradis%d: config =", saa
->nr
);
241 for (i
= 0; i
< 51; i
++) {
242 printk(" %02x",saa
->boardcfg
[i
]);
248 static void detach_inform(struct i2c_bus
*bus
, int id
)
250 struct saa7146
*saa
= (struct saa7146
*) bus
->data
;
255 static void I2CBusScan(struct i2c_bus
*bus
)
258 for (i
= 0; i
< 0xff; i
+= 2)
259 if ((I2CRead(bus
, i
, 0, 0)) >= 0)
260 attach_inform(bus
, i
);
263 static struct i2c_bus saa7146_i2c_bus_template
=
277 static int debiwait_maxwait
= 0;
279 static int wait_for_debi_done(struct saa7146
*saa
)
283 /* wait for registers to be programmed */
284 for (i
= 0; i
< 100000 &&
285 !(saaread(SAA7146_MC2
) & SAA7146_MC2_UPLD_DEBI
); i
++)
286 saaread(SAA7146_MC2
);
287 /* wait for transfer to complete */
288 for (i
= 0; i
< 500000 &&
289 (saaread(SAA7146_PSR
) & SAA7146_PSR_DEBI_S
); i
++)
290 saaread(SAA7146_MC2
);
291 if (i
> debiwait_maxwait
)
292 printk("wait-for-debi-done maxwait: %d\n",
293 debiwait_maxwait
= i
);
300 static int debiwrite(struct saa7146
*saa
, u32 config
, int addr
,
304 if (count
<= 0 || count
> 32764)
306 if (wait_for_debi_done(saa
) < 0)
308 saawrite(config
, SAA7146_DEBI_CONFIG
);
309 if (count
<= 4) /* immediate transfer */
310 saawrite(val
, SAA7146_DEBI_AD
);
311 else /* block transfer */
312 saawrite(virt_to_bus(saa
->dmadebi
), SAA7146_DEBI_AD
);
313 saawrite((cmd
= (count
<< 17) | (addr
& 0xffff)), SAA7146_DEBI_COMMAND
);
314 saawrite((SAA7146_MC2_UPLD_DEBI
<< 16) | SAA7146_MC2_UPLD_DEBI
,
319 static u32
debiread(struct saa7146
*saa
, u32 config
, int addr
, int count
)
323 if (count
> 32764 || count
<= 0)
325 if (wait_for_debi_done(saa
) < 0)
327 saawrite(virt_to_bus(saa
->dmadebi
), SAA7146_DEBI_AD
);
328 saawrite((count
<< 17) | 0x10000 | (addr
& 0xffff),
329 SAA7146_DEBI_COMMAND
);
330 saawrite(config
, SAA7146_DEBI_CONFIG
);
331 saawrite((SAA7146_MC2_UPLD_DEBI
<< 16) | SAA7146_MC2_UPLD_DEBI
,
333 if (count
> 4) /* not an immediate transfer */
335 wait_for_debi_done(saa
);
336 result
= saaread(SAA7146_DEBI_AD
);
347 /* MUST be a multiple of 8 bytes and 8-byte aligned and < 32768 bytes */
348 /* data copied into saa->dmadebi buffer, caller must re-enable interrupts */
349 static void ibm_block_dram_read(struct saa7146
*saa
, int address
, int bytes
)
353 buf
= (u32
*) saa
->dmadebi
;
356 saawrite(0, SAA7146_IER
); /* disable interrupts */
357 for (i
=0; i
< 10000 &&
358 (debiread(saa
, debNormal
, IBM_MP2_DRAM_CMD_STAT
, 2)
360 saaread(SAA7146_MC2
);
362 printk(KERN_ERR
"stradis%d: dram_busy never cleared\n",
364 debiwrite(saa
, debNormal
, IBM_MP2_SRC_ADDR
, (address
<<16) |
366 debiwrite(saa
, debNormal
, IBM_MP2_BLOCK_SIZE
, bytes
, 2);
367 debiwrite(saa
, debNormal
, IBM_MP2_CMD_STAT
, 0x8a10, 2);
368 for (j
= 0; j
< bytes
/4; j
++) {
369 for (i
= 0; i
< 10000 &&
370 (!(debiread(saa
, debNormal
, IBM_MP2_DRAM_CMD_STAT
, 2)
372 saaread(SAA7146_MC2
);
374 printk(KERN_ERR
"stradis%d: dram_ready never set\n",
376 buf
[j
] = debiread(saa
, debNormal
, IBM_MP2_DRAM_DATA
, 4);
381 static void do_irq_send_data(struct saa7146
*saa
)
383 int split
, audbytes
, vidbytes
;
385 saawrite(SAA7146_PSR_PIN1
, SAA7146_IER
);
386 /* if special feature mode in effect, disable audio sending */
387 if (saa
->playmode
!= VID_PLAY_NORMAL
)
388 saa
->audtail
= saa
->audhead
= 0;
389 if (saa
->audhead
<= saa
->audtail
)
390 audbytes
= saa
->audtail
- saa
->audhead
;
392 audbytes
= 65536 - (saa
->audhead
- saa
->audtail
);
393 if (saa
->vidhead
<= saa
->vidtail
)
394 vidbytes
= saa
->vidtail
- saa
->vidhead
;
396 vidbytes
= 524288 - (saa
->vidhead
- saa
->vidtail
);
397 if (audbytes
== 0 && vidbytes
== 0 && saa
->osdtail
== saa
->osdhead
) {
398 saawrite(0, SAA7146_IER
);
401 /* if at least 1 block audio waiting and audio fifo isn't full */
402 if (audbytes
>= 2048 && (debiread(saa
, debNormal
,
403 IBM_MP2_AUD_FIFO
, 2) & 0xff) < 60) {
404 if (saa
->audhead
> saa
->audtail
)
405 split
= 65536 - saa
->audhead
;
409 if (split
> 0 && split
< 2048) {
410 memcpy(saa
->dmadebi
, saa
->audbuf
+ saa
->audhead
,
416 memcpy(saa
->dmadebi
+ split
, saa
->audbuf
+ saa
->audhead
,
418 saa
->audhead
+= audbytes
;
419 saa
->audhead
&= 0xffff;
420 debiwrite(saa
, debAudio
, (NewCard
? IBM_MP2_AUD_FIFO
:
421 IBM_MP2_AUD_FIFOW
), 0, 2048);
422 wake_up_interruptible(&saa
->audq
);
423 /* if at least 1 block video waiting and video fifo isn't full */
424 } else if (vidbytes
>= 30720 && (debiread(saa
, debNormal
,
425 IBM_MP2_FIFO
, 2)) < 16384) {
426 if (saa
->vidhead
> saa
->vidtail
)
427 split
= 524288 - saa
->vidhead
;
431 if (split
> 0 && split
< 30720) {
432 memcpy(saa
->dmadebi
, saa
->vidbuf
+ saa
->vidhead
,
438 memcpy(saa
->dmadebi
+ split
, saa
->vidbuf
+ saa
->vidhead
,
440 saa
->vidhead
+= vidbytes
;
441 saa
->vidhead
&= 0x7ffff;
442 debiwrite(saa
, debVideo
, (NewCard
? IBM_MP2_FIFO
:
443 IBM_MP2_FIFOW
), 0, 30720);
444 wake_up_interruptible(&saa
->vidq
);
446 saawrite(SAA7146_PSR_DEBI_S
| SAA7146_PSR_PIN1
, SAA7146_IER
);
449 static void send_osd_data(struct saa7146
*saa
)
451 int size
= saa
->osdtail
- saa
->osdhead
;
454 /* ensure some multiple of 8 bytes is transferred */
455 size
= 8 * ((size
+ 8)>>3);
457 debiwrite(saa
, debNormal
, IBM_MP2_OSD_ADDR
,
458 (saa
->osdhead
>>3), 2);
459 memcpy(saa
->dmadebi
, &saa
->osdbuf
[saa
->osdhead
], size
);
460 saa
->osdhead
+= size
;
461 /* block transfer of next 8 bytes to ~32k bytes */
462 debiwrite(saa
, debNormal
, IBM_MP2_OSD_DATA
, 0, size
);
464 if (saa
->osdhead
>= saa
->osdtail
) {
465 saa
->osdhead
= saa
->osdtail
= 0;
466 debiwrite(saa
, debNormal
, IBM_MP2_MASK0
, 0xc00c, 2);
470 static void saa7146_irq(int irq
, void *dev_id
, struct pt_regs
*regs
)
472 struct saa7146
*saa
= (struct saa7146
*) dev_id
;
478 /* get/clear interrupt status bits */
479 stat
= saaread(SAA7146_ISR
);
480 astat
= stat
& saaread(SAA7146_IER
);
483 saawrite(astat
, SAA7146_ISR
);
484 if (astat
& SAA7146_PSR_DEBI_S
) {
485 do_irq_send_data(saa
);
487 if (astat
& SAA7146_PSR_PIN1
) {
489 /* the following read will trigger DEBI_S */
490 istat
= debiread(saa
, debNormal
, IBM_MP2_HOST_INT
, 2);
492 saawrite(0, SAA7146_IER
);
494 saawrite(SAA7146_PSR_DEBI_S
|
495 SAA7146_PSR_PIN1
, SAA7146_IER
);
497 if (istat
& 0x20) { /* Video Start */
498 saa
->vidinfo
.frame_count
++;
500 if (istat
& 0x400) { /* Picture Start */
501 /* update temporal reference */
503 if (istat
& 0x200) { /* Picture Resolution Change */
504 /* read new resolution */
506 if (istat
& 0x100) { /* New User Data found */
507 /* read new user data */
509 if (istat
& 0x1000) { /* new GOP/SMPTE */
512 if (istat
& 0x8000) { /* Sequence Start Code */
513 /* reset frame counter, load sizes */
514 saa
->vidinfo
.frame_count
= 0;
515 saa
->vidinfo
.h_size
= 704;
516 saa
->vidinfo
.v_size
= 480;
518 if (saa
->endmarkhead
!= saa
->endmarktail
) {
520 saa
->endmark
[saa
->endmarkhead
];
522 if (saa
->endmarkhead
>= MAX_MARKS
)
523 saa
->endmarkhead
= 0;
527 if (istat
& 0x4000) { /* Sequence Error Code */
528 if (saa
->endmarkhead
!= saa
->endmarktail
) {
530 saa
->endmark
[saa
->endmarkhead
];
532 if (saa
->endmarkhead
>= MAX_MARKS
)
533 saa
->endmarkhead
= 0;
538 if (astat
& SAA7146_PSR_PPEF
) {
539 IDEBUG(printk("stradis%d irq: PPEF\n", saa
->nr
));
541 if (astat
& SAA7146_PSR_PABO
) {
542 IDEBUG(printk("stradis%d irq: PABO\n", saa
->nr
));
544 if (astat
& SAA7146_PSR_PPED
) {
545 IDEBUG(printk("stradis%d irq: PPED\n", saa
->nr
));
547 if (astat
& SAA7146_PSR_RPS_I1
) {
548 IDEBUG(printk("stradis%d irq: RPS_I1\n", saa
->nr
));
550 if (astat
& SAA7146_PSR_RPS_I0
) {
551 IDEBUG(printk("stradis%d irq: RPS_I0\n", saa
->nr
));
553 if (astat
& SAA7146_PSR_RPS_LATE1
) {
554 IDEBUG(printk("stradis%d irq: RPS_LATE1\n", saa
->nr
));
556 if (astat
& SAA7146_PSR_RPS_LATE0
) {
557 IDEBUG(printk("stradis%d irq: RPS_LATE0\n", saa
->nr
));
559 if (astat
& SAA7146_PSR_RPS_E1
) {
560 IDEBUG(printk("stradis%d irq: RPS_E1\n", saa
->nr
));
562 if (astat
& SAA7146_PSR_RPS_E0
) {
563 IDEBUG(printk("stradis%d irq: RPS_E0\n", saa
->nr
));
565 if (astat
& SAA7146_PSR_RPS_TO1
) {
566 IDEBUG(printk("stradis%d irq: RPS_TO1\n", saa
->nr
));
568 if (astat
& SAA7146_PSR_RPS_TO0
) {
569 IDEBUG(printk("stradis%d irq: RPS_TO0\n", saa
->nr
));
571 if (astat
& SAA7146_PSR_UPLD
) {
572 IDEBUG(printk("stradis%d irq: UPLD\n", saa
->nr
));
574 if (astat
& SAA7146_PSR_DEBI_E
) {
575 IDEBUG(printk("stradis%d irq: DEBI_E\n", saa
->nr
));
577 if (astat
& SAA7146_PSR_I2C_S
) {
578 IDEBUG(printk("stradis%d irq: I2C_S\n", saa
->nr
));
580 if (astat
& SAA7146_PSR_I2C_E
) {
581 IDEBUG(printk("stradis%d irq: I2C_E\n", saa
->nr
));
583 if (astat
& SAA7146_PSR_A2_IN
) {
584 IDEBUG(printk("stradis%d irq: A2_IN\n", saa
->nr
));
586 if (astat
& SAA7146_PSR_A2_OUT
) {
587 IDEBUG(printk("stradis%d irq: A2_OUT\n", saa
->nr
));
589 if (astat
& SAA7146_PSR_A1_IN
) {
590 IDEBUG(printk("stradis%d irq: A1_IN\n", saa
->nr
));
592 if (astat
& SAA7146_PSR_A1_OUT
) {
593 IDEBUG(printk("stradis%d irq: A1_OUT\n", saa
->nr
));
595 if (astat
& SAA7146_PSR_AFOU
) {
596 IDEBUG(printk("stradis%d irq: AFOU\n", saa
->nr
));
598 if (astat
& SAA7146_PSR_V_PE
) {
599 IDEBUG(printk("stradis%d irq: V_PE\n", saa
->nr
));
601 if (astat
& SAA7146_PSR_VFOU
) {
602 IDEBUG(printk("stradis%d irq: VFOU\n", saa
->nr
));
604 if (astat
& SAA7146_PSR_FIDA
) {
605 IDEBUG(printk("stradis%d irq: FIDA\n", saa
->nr
));
607 if (astat
& SAA7146_PSR_FIDB
) {
608 IDEBUG(printk("stradis%d irq: FIDB\n", saa
->nr
));
610 if (astat
& SAA7146_PSR_PIN3
) {
611 IDEBUG(printk("stradis%d irq: PIN3\n", saa
->nr
));
613 if (astat
& SAA7146_PSR_PIN2
) {
614 IDEBUG(printk("stradis%d irq: PIN2\n", saa
->nr
));
616 if (astat
& SAA7146_PSR_PIN0
) {
617 IDEBUG(printk("stradis%d irq: PIN0\n", saa
->nr
));
619 if (astat
& SAA7146_PSR_ECS
) {
620 IDEBUG(printk("stradis%d irq: ECS\n", saa
->nr
));
622 if (astat
& SAA7146_PSR_EC3S
) {
623 IDEBUG(printk("stradis%d irq: EC3S\n", saa
->nr
));
625 if (astat
& SAA7146_PSR_EC0S
) {
626 IDEBUG(printk("stradis%d irq: EC0S\n", saa
->nr
));
631 printk(KERN_WARNING
"stradis%d: irq loop %d\n",
634 saawrite(0, SAA7146_IER
);
636 "stradis%d: IRQ loop cleared\n", saa
->nr
);
641 static int ibm_send_command(struct saa7146
*saa
,
642 int command
, int data
, int chain
)
647 debiwrite(saa
, debNormal
, IBM_MP2_COMMAND
, (command
<< 1) | 1, 2);
649 debiwrite(saa
, debNormal
, IBM_MP2_COMMAND
, command
<< 1, 2);
650 debiwrite(saa
, debNormal
, IBM_MP2_CMD_DATA
, data
, 2);
651 debiwrite(saa
, debNormal
, IBM_MP2_CMD_STAT
, 1, 2);
652 for (i
= 0; i
< 100 &&
653 (debiread(saa
, debNormal
, IBM_MP2_CMD_STAT
, 2) & 1); i
++)
660 static void cs4341_setlevel(struct saa7146
*saa
, int left
, int right
)
662 I2CWrite(&(saa
->i2c
), 0x22, 0x03,
663 left
> 94 ? 94 : left
, 2);
664 I2CWrite(&(saa
->i2c
), 0x22, 0x04,
665 right
> 94 ? 94 : right
, 2);
668 static void initialize_cs4341(struct saa7146
*saa
)
671 for (i
= 0; i
< 200; i
++) {
672 /* auto mute off, power on, no de-emphasis */
673 /* I2S data up to 24-bit 64xFs internal SCLK */
674 I2CWrite(&(saa
->i2c
), 0x22, 0x01, 0x11, 2);
675 /* ATAPI mixer setings */
676 I2CWrite(&(saa
->i2c
), 0x22, 0x02, 0x49, 2);
677 /* attenuation left 3db */
678 I2CWrite(&(saa
->i2c
), 0x22, 0x03, 0x00, 2);
679 /* attenuation right 3db */
680 I2CWrite(&(saa
->i2c
), 0x22, 0x04, 0x00, 2);
681 I2CWrite(&(saa
->i2c
), 0x22, 0x01, 0x10, 2);
682 if (I2CRead(&(saa
->i2c
), 0x22, 0x02, 1) == 0x49)
686 printk("stradis%d: CS4341 initialized (%d)\n", saa
->nr
, i
);
690 static void initialize_cs8420(struct saa7146
*saa
, int pro
)
695 sequence
= mode8420pro
;
697 sequence
= mode8420con
;
698 for (i
= 0; i
< INIT8420LEN
; i
++)
699 I2CWrite(&(saa
->i2c
), 0x20, init8420
[i
* 2],
700 init8420
[i
* 2 + 1], 2);
701 for (i
= 0; i
< MODE8420LEN
; i
++)
702 I2CWrite(&(saa
->i2c
), 0x20, sequence
[i
* 2],
703 sequence
[i
* 2 + 1], 2);
704 printk("stradis%d: CS8420 initialized\n", saa
->nr
);
707 static void initialize_saa7121(struct saa7146
*saa
, int dopal
)
712 sequence
= init7121pal
;
714 sequence
= init7121ntsc
;
715 mod
= saaread(SAA7146_PSR
) & 0x08;
716 /* initialize PAL/NTSC video encoder */
717 for (i
= 0; i
< INIT7121LEN
; i
++) {
718 if (NewCard
) { /* handle new card encoder differences */
719 if (sequence
[i
*2] == 0x3a)
720 I2CWrite(&(saa
->i2c
), 0x88, 0x3a, 0x13, 2);
721 else if (sequence
[i
*2] == 0x6b)
722 I2CWrite(&(saa
->i2c
), 0x88, 0x6b, 0x20, 2);
723 else if (sequence
[i
*2] == 0x6c)
724 I2CWrite(&(saa
->i2c
), 0x88, 0x6c,
725 dopal
? 0x09 : 0xf5, 2);
726 else if (sequence
[i
*2] == 0x6d)
727 I2CWrite(&(saa
->i2c
), 0x88, 0x6d,
728 dopal
? 0x20 : 0x00, 2);
729 else if (sequence
[i
*2] == 0x7a)
730 I2CWrite(&(saa
->i2c
), 0x88, 0x7a,
731 dopal
? (PALFirstActive
- 1) :
732 (NTSCFirstActive
- 4), 2);
733 else if (sequence
[i
*2] == 0x7b)
734 I2CWrite(&(saa
->i2c
), 0x88, 0x7b,
735 dopal
? PALLastActive
:
737 else I2CWrite(&(saa
->i2c
), 0x88, sequence
[i
* 2],
738 sequence
[i
* 2 + 1], 2);
740 if (sequence
[i
*2] == 0x6b && mod
)
741 I2CWrite(&(saa
->i2c
), 0x88, 0x6b,
742 (sequence
[i
* 2 + 1] ^ 0x09), 2);
743 else if (sequence
[i
*2] == 0x7a)
744 I2CWrite(&(saa
->i2c
), 0x88, 0x7a,
745 dopal
? (PALFirstActive
- 1) :
746 (NTSCFirstActive
- 4), 2);
747 else if (sequence
[i
*2] == 0x7b)
748 I2CWrite(&(saa
->i2c
), 0x88, 0x7b,
749 dopal
? PALLastActive
:
752 I2CWrite(&(saa
->i2c
), 0x88, sequence
[i
* 2],
753 sequence
[i
* 2 + 1], 2);
758 static void set_genlock_offset(struct saa7146
*saa
, int noffset
)
761 int PixelsPerLine
= 858;
762 if (CurrentMode
== VIDEO_MODE_PAL
)
766 else if (noffset
< -500)
768 nCode
= noffset
+ 0x100;
771 else if (nCode
< 1) nCode
= 0x400 + PixelsPerLine
+ nCode
;
772 debiwrite(saa
, debNormal
, XILINX_GLDELAY
, nCode
, 2);
775 static void set_out_format(struct saa7146
*saa
, int mode
)
777 initialize_saa7121(saa
, (mode
== VIDEO_MODE_NTSC
? 0 : 1));
778 saa
->boardcfg
[2] = mode
;
779 /* do not adjust analog video parameters here, use saa7121 init */
780 /* you will affect the SDI output on the new card */
781 if (mode
== VIDEO_MODE_PAL
) { /* PAL */
782 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0x0808, 2);
784 saawrite(0x012002c0, SAA7146_NUM_LINE_BYTE1
);
786 debiwrite(saa
, debNormal
, IBM_MP2_DISP_MODE
,
790 debiwrite(saa
, debNormal
, IBM_MP2_DISP_MODE
,
791 NewCard
? 0xe500: 0x6500, 2);
792 debiwrite(saa
, debNormal
, IBM_MP2_DISP_DLY
,
794 (NewCard
? PALFirstActive
: PALFirstActive
-6), 2);
796 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0x0800, 2);
798 saawrite(0x00f002c0, SAA7146_NUM_LINE_BYTE1
);
799 debiwrite(saa
, debNormal
, IBM_MP2_DISP_MODE
,
800 NewCard
? 0xe100: 0x6100, 2);
801 debiwrite(saa
, debNormal
, IBM_MP2_DISP_DLY
,
803 (NewCard
? NTSCFirstActive
: NTSCFirstActive
-6), 2);
808 /* Intialize bitmangler to map from a byte value to the mangled word that
809 * must be output to program the Xilinx part through the DEBI port.
810 * Xilinx Data Bit->DEBI Bit: 0->15 1->7 2->6 3->12 4->11 5->2 6->1 7->0
811 * transfer FPGA code, init IBM chip, transfer IBM microcode
812 * rev2 card mangles: 0->7 1->6 2->5 3->4 4->3 5->2 6->1 7->0
814 static u16 bitmangler
[256];
816 static int initialize_fpga(struct video_code
*bitdata
)
818 int i
, num
, startindex
, failure
= 0, loadtwo
, loadfile
= 0;
823 /* verify fpga code */
824 for (startindex
= 0; startindex
< bitdata
->datasize
; startindex
++)
825 if (bitdata
->data
[startindex
] == 255)
827 if (startindex
== bitdata
->datasize
) {
828 printk(KERN_INFO
"stradis: bad fpga code\n");
831 /* initialize all detected cards */
832 for (num
= 0; num
< saa_num
; num
++) {
833 saa
= &saa7146s
[num
];
834 if (saa
->boardcfg
[0] > 20)
835 continue; /* card was programmed */
836 loadtwo
= (saa
->boardcfg
[18] & 0x10);
837 if (!NewCard
) /* we have an old board */
838 for (i
= 0; i
< 256; i
++)
839 bitmangler
[i
] = ((i
& 0x01) << 15) |
840 ((i
& 0x02) << 6) | ((i
& 0x04) << 4) |
841 ((i
& 0x08) << 9) | ((i
& 0x10) << 7) |
842 ((i
& 0x20) >> 3) | ((i
& 0x40) >> 5) |
844 else /* else we have a new board */
845 for (i
= 0; i
< 256; i
++)
846 bitmangler
[i
] = ((i
& 0x01) << 7) |
847 ((i
& 0x02) << 5) | ((i
& 0x04) << 3) |
848 ((i
& 0x08) << 1) | ((i
& 0x10) >> 1) |
849 ((i
& 0x20) >> 3) | ((i
& 0x40) >> 5) |
852 dmabuf
= (u16
*) saa
->dmadebi
;
853 newdma
= (u8
*) saa
->dmadebi
;
854 if (NewCard
) { /* SDM2xxx */
855 if (!strncmp(bitdata
->loadwhat
, "decoder2", 8))
856 continue; /* fpga not for this card */
857 if (!strncmp(&saa
->boardcfg
[42],
858 bitdata
->loadwhat
, 8)) {
860 } else if (loadtwo
&& !strncmp(&saa
->boardcfg
[19],
861 bitdata
->loadwhat
, 8)) {
863 } else if (!saa
->boardcfg
[42] && /* special */
864 !strncmp("decxl", bitdata
->loadwhat
, 8)) {
867 continue; /* fpga not for this card */
868 if (loadfile
!= 1 && loadfile
!= 2) {
869 continue; /* skip to next card */
871 if (saa
->boardcfg
[0] && loadfile
== 1 )
872 continue; /* skip to next card */
873 if (saa
->boardcfg
[0] != 1 && loadfile
== 2)
874 continue; /* skip to next card */
875 saa
->boardcfg
[0]++; /* mark fpga handled */
876 printk("stradis%d: loading %s\n", saa
->nr
,
878 if (loadtwo
&& loadfile
== 2)
879 goto send_fpga_stuff
;
880 /* turn on the Audio interface to set PROG low */
881 saawrite(0x00400040, SAA7146_GPIO_CTRL
);
882 saaread(SAA7146_PSR
); /* ensure posted write */
883 /* wait for everyone to reset */
885 saawrite(0x00400000, SAA7146_GPIO_CTRL
);
886 } else { /* original card */
887 if (strncmp(bitdata
->loadwhat
, "decoder2", 8))
888 continue; /* fpga not for this card */
889 /* Pull the Xilinx PROG signal WS3 low */
890 saawrite(0x02000200, SAA7146_MC1
);
891 /* Turn on the Audio interface so can set PROG low */
892 saawrite(0x000000c0, SAA7146_ACON1
);
893 /* Pull the Xilinx INIT signal (GPIO2) low */
894 saawrite(0x00400000, SAA7146_GPIO_CTRL
);
895 /* Make sure everybody resets */
896 saaread(SAA7146_PSR
); /* ensure posted write */
898 /* Release the Xilinx PROG signal */
899 saawrite(0x00000000, SAA7146_ACON1
);
900 /* Turn off the Audio interface */
901 saawrite(0x02000000, SAA7146_MC1
);
903 /* Release Xilinx INIT signal (WS2) */
904 saawrite(0x00000000, SAA7146_GPIO_CTRL
);
905 /* Wait for the INIT to go High */
906 for (i
= 0; i
< 10000 &&
907 !(saaread(SAA7146_PSR
) & SAA7146_PSR_PIN2
); i
++)
910 printk(KERN_INFO
"stradis%d: no fpga INIT\n", saa
->nr
);
915 for (i
= startindex
; i
< bitdata
->datasize
; i
++)
916 newdma
[i
- startindex
] =
917 bitmangler
[bitdata
->data
[i
]];
918 debiwrite(saa
, 0x01420000, 0, 0,
919 ((bitdata
->datasize
- startindex
) + 5));
923 "awaiting 2nd FPGA bitfile\n",
925 continue; /* skip to next card */
930 for (i
= startindex
; i
< bitdata
->datasize
; i
++)
931 dmabuf
[i
- startindex
] =
932 bitmangler
[bitdata
->data
[i
]];
933 debiwrite(saa
, 0x014a0000, 0, 0,
934 ((bitdata
->datasize
- startindex
) + 5) * 2);
936 for (i
= 0; i
< 1000 &&
937 !(saaread(SAA7146_PSR
) & SAA7146_PSR_PIN2
); i
++)
940 printk(KERN_INFO
"stradis%d: FPGA load failed\n",
946 /* Pull the Xilinx INIT signal (GPIO2) low */
947 saawrite(0x00400000, SAA7146_GPIO_CTRL
);
948 saaread(SAA7146_PSR
); /* ensure posted write */
950 saawrite(0x00000000, SAA7146_GPIO_CTRL
);
953 printk(KERN_INFO
"stradis%d: FPGA Loaded\n", saa
->nr
);
954 saa
->boardcfg
[0] = 26; /* mark fpga programmed */
955 /* set VXCO to its lowest frequency */
956 debiwrite(saa
, debNormal
, XILINX_PWM
, 0, 2);
960 debiwrite(saa
, debNormal
, XILINX_CS3310_CMPLT
,
962 /* set VXCO to PWM mode, release reset, blank on */
963 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0xffc4, 2);
967 debiwrite(saa
, debNormal
, XILINX_CTL0
,
970 /* set source Black */
971 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0x1707, 2);
972 saa
->boardcfg
[4] = 22; /* set NTSC First Active Line */
973 saa
->boardcfg
[5] = 23; /* set PAL First Active Line */
974 saa
->boardcfg
[54] = 2; /* set NTSC Last Active Line - 256 */
975 saa
->boardcfg
[55] = 54; /* set PAL Last Active Line - 256 */
976 set_out_format(saa
, VIDEO_MODE_NTSC
);
978 /* begin IBM chip init */
979 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, 4, 2);
980 saaread(SAA7146_PSR
); /* wait for reset */
982 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, 0, 2);
983 debiread(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, 2);
984 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, 0x10, 2);
985 debiwrite(saa
, debNormal
, IBM_MP2_CMD_ADDR
, 0, 2);
986 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_MODE
, 0x2e, 2);
989 /* set i2s rate converter to 48KHz */
990 debiwrite(saa
, debNormal
, 0x80c0, 6, 2);
991 /* we must init CS8420 first since rev b pulls i2s */
992 /* master clock low and CS4341 needs i2s master to */
993 /* run the i2c port. */
995 /* 0=consumer, 1=pro */
996 initialize_cs8420(saa
, 0);
1000 initialize_cs4341(saa
);
1002 debiwrite(saa
, debNormal
, IBM_MP2_INFC_CTL
, 0x48, 2);
1003 debiwrite(saa
, debNormal
, IBM_MP2_BEEP_CTL
, 0xa000, 2);
1004 debiwrite(saa
, debNormal
, IBM_MP2_DISP_LBOR
, 0, 2);
1005 debiwrite(saa
, debNormal
, IBM_MP2_DISP_TBOR
, 0, 2);
1007 set_genlock_offset(saa
, 0);
1008 debiwrite(saa
, debNormal
, IBM_MP2_FRNT_ATTEN
, 0, 2);
1010 /* enable genlock */
1011 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0x8000, 2);
1013 /* disable genlock */
1014 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0x8080, 2);
1020 static int do_ibm_reset(struct saa7146
*saa
)
1022 /* failure if decoder not previously programmed */
1023 if (saa
->boardcfg
[0] < 37)
1027 debiwrite(saa
, debNormal
, XILINX_CS3310_CMPLT
, 0, 2);
1028 /* disable interrupts */
1029 saawrite(0, SAA7146_IER
);
1030 saa
->audhead
= saa
->audtail
= 0;
1031 saa
->vidhead
= saa
->vidtail
= 0;
1032 /* tristate debi bus, disable debi transfers */
1033 saawrite(0x00880000, SAA7146_MC1
);
1034 /* ensure posted write */
1035 saaread(SAA7146_MC1
);
1037 /* re-enable debi transfers */
1038 saawrite(0x00880088, SAA7146_MC1
);
1039 /* set source Black */
1040 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0x1707, 2);
1041 /* begin IBM chip init */
1042 set_out_format(saa
, CurrentMode
);
1043 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, 4, 2);
1044 saaread(SAA7146_PSR
); /* wait for reset */
1046 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, 0, 2);
1047 debiread(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, 2);
1048 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, ChipControl
, 2);
1049 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_MODE
, 0x2e, 2);
1052 /* set i2s rate converter to 48KHz */
1053 debiwrite(saa
, debNormal
, 0x80c0, 6, 2);
1054 /* we must init CS8420 first since rev b pulls i2s */
1055 /* master clock low and CS4341 needs i2s master to */
1056 /* run the i2c port. */
1058 /* 0=consumer, 1=pro */
1059 initialize_cs8420(saa
, 1);
1063 initialize_cs4341(saa
);
1065 debiwrite(saa
, debNormal
, IBM_MP2_INFC_CTL
, 0x48, 2);
1066 debiwrite(saa
, debNormal
, IBM_MP2_BEEP_CTL
, 0xa000, 2);
1067 debiwrite(saa
, debNormal
, IBM_MP2_DISP_LBOR
, 0, 2);
1068 debiwrite(saa
, debNormal
, IBM_MP2_DISP_TBOR
, 0, 2);
1070 set_genlock_offset(saa
, 0);
1071 debiwrite(saa
, debNormal
, IBM_MP2_FRNT_ATTEN
, 0, 2);
1072 debiwrite(saa
, debNormal
, IBM_MP2_OSD_SIZE
, 0x2000, 2);
1073 debiwrite(saa
, debNormal
, IBM_MP2_AUD_CTL
, 0x4552, 2);
1074 if (ibm_send_command(saa
, IBM_MP2_CONFIG_DECODER
,
1075 (ChipControl
== 0x43 ? 0xe800 : 0xe000), 1)) {
1076 printk(KERN_ERR
"stradis%d: IBM config failed\n", saa
->nr
);
1079 int i
= CS3310MaxLvl
;
1080 debiwrite(saa
, debNormal
, XILINX_CS3310_CMPLT
, ((i
<<8)|i
), 2);
1082 /* start video decoder */
1083 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
, ChipControl
, 2);
1084 /* 256k vid, 3520 bytes aud */
1085 debiwrite(saa
, debNormal
, IBM_MP2_RB_THRESHOLD
, 0x4037, 2);
1086 debiwrite(saa
, debNormal
, IBM_MP2_AUD_CTL
, 0x4573, 2);
1087 ibm_send_command(saa
, IBM_MP2_PLAY
, 0, 0);
1088 /* enable buffer threshold irq */
1089 debiwrite(saa
, debNormal
, IBM_MP2_MASK0
, 0xc00c, 2);
1090 /* clear pending interrupts */
1091 debiread(saa
, debNormal
, IBM_MP2_HOST_INT
, 2);
1092 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0x1711, 2);
1096 /* load the decoder microcode */
1097 static int initialize_ibmmpeg2(struct video_code
*microcode
)
1100 struct saa7146
*saa
;
1102 for (num
= 0; num
< saa_num
; num
++) {
1103 saa
= &saa7146s
[num
];
1104 /* check that FPGA is loaded */
1105 debiwrite(saa
, debNormal
, IBM_MP2_OSD_SIZE
, 0xa55a, 2);
1106 if ((i
= debiread(saa
, debNormal
, IBM_MP2_OSD_SIZE
, 2)) !=
1108 printk(KERN_INFO
"stradis%d: %04x != 0xa55a\n",
1114 if (!strncmp(microcode
->loadwhat
, "decoder.vid", 11)) {
1115 if (saa
->boardcfg
[0] > 27)
1116 continue; /* skip to next card */
1117 /* load video control store */
1118 saa
->boardcfg
[1] = 0x13; /* no-sync default */
1119 debiwrite(saa
, debNormal
, IBM_MP2_WR_PROT
, 1, 2);
1120 debiwrite(saa
, debNormal
, IBM_MP2_PROC_IADDR
, 0, 2);
1121 for (i
= 0; i
< microcode
->datasize
/ 2; i
++)
1122 debiwrite(saa
, debNormal
, IBM_MP2_PROC_IDATA
,
1123 (microcode
->data
[i
* 2] << 8) |
1124 microcode
->data
[i
* 2 + 1], 2);
1125 debiwrite(saa
, debNormal
, IBM_MP2_PROC_IADDR
, 0, 2);
1126 debiwrite(saa
, debNormal
, IBM_MP2_WR_PROT
, 0, 2);
1127 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
,
1129 saa
->boardcfg
[0] = 28;
1131 if (!strncmp(microcode
->loadwhat
, "decoder.aud", 11)) {
1132 if (saa
->boardcfg
[0] > 35)
1133 continue; /* skip to next card */
1134 /* load audio control store */
1135 debiwrite(saa
, debNormal
, IBM_MP2_WR_PROT
, 1, 2);
1136 debiwrite(saa
, debNormal
, IBM_MP2_AUD_IADDR
, 0, 2);
1137 for (i
= 0; i
< microcode
->datasize
; i
++)
1138 debiwrite(saa
, debNormal
, IBM_MP2_AUD_IDATA
,
1139 microcode
->data
[i
], 1);
1140 debiwrite(saa
, debNormal
, IBM_MP2_AUD_IADDR
, 0, 2);
1141 debiwrite(saa
, debNormal
, IBM_MP2_WR_PROT
, 0, 2);
1142 debiwrite(saa
, debNormal
, IBM_MP2_OSD_SIZE
, 0x2000, 2);
1143 debiwrite(saa
, debNormal
, IBM_MP2_AUD_CTL
, 0x4552, 2);
1144 if (ibm_send_command(saa
, IBM_MP2_CONFIG_DECODER
,
1147 "stradis%d: IBM config failed\n",
1151 /* set PWM to center value */
1153 debiwrite(saa
, debNormal
, XILINX_PWM
,
1155 (saa
->boardcfg
[13]<<8), 2);
1157 debiwrite(saa
, debNormal
, XILINX_PWM
,
1161 debiwrite(saa
, debNormal
,
1162 XILINX_CS3310_CMPLT
, ((i
<<8)|i
), 2);
1165 "stradis%d: IBM MPEGCD%d Initialized\n",
1166 saa
->nr
, 18 + (debiread(saa
, debNormal
,
1167 IBM_MP2_CHIP_CONTROL
, 2) >> 12));
1168 /* start video decoder */
1169 debiwrite(saa
, debNormal
, IBM_MP2_CHIP_CONTROL
,
1171 debiwrite(saa
, debNormal
, IBM_MP2_RB_THRESHOLD
,
1172 0x4037, 2); /* 256k vid, 3520 bytes aud */
1173 debiwrite(saa
, debNormal
, IBM_MP2_AUD_CTL
, 0x4573, 2);
1174 ibm_send_command(saa
, IBM_MP2_PLAY
, 0, 0);
1175 /* enable buffer threshold irq */
1176 debiwrite(saa
, debNormal
, IBM_MP2_MASK0
, 0xc00c, 2);
1177 debiread(saa
, debNormal
, IBM_MP2_HOST_INT
, 2);
1178 /* enable gpio irq */
1179 saawrite(0x00002000, SAA7146_GPIO_CTRL
);
1180 /* enable decoder output to HPS */
1181 debiwrite(saa
, debNormal
, XILINX_CTL0
, 0x1711, 2);
1182 saa
->boardcfg
[0] = 37;
1188 static u32 palette2fmt
[] =
1189 { /* some of these YUV translations are wrong */
1190 0xffffffff, 0x86000000, 0x87000000, 0x80000000, 0x8100000, 0x82000000,
1191 0x83000000, 0x00000000, 0x03000000, 0x03000000, 0x0a00000, 0x03000000,
1192 0x06000000, 0x00000000, 0x03000000, 0x0a000000, 0x0300000
1194 static int bpp2fmt
[4] =
1196 VIDEO_PALETTE_HI240
, VIDEO_PALETTE_RGB565
, VIDEO_PALETTE_RGB24
,
1200 /* I wish I could find a formula to calculate these... */
1201 static u32 h_prescale
[64] =
1203 0x10000000, 0x18040202, 0x18080000, 0x380c0606, 0x38100204, 0x38140808,
1204 0x38180000, 0x381c0000, 0x3820161c, 0x38242a3b, 0x38281230, 0x382c4460,
1205 0x38301040, 0x38340080, 0x38380000, 0x383c0000, 0x3840fefe, 0x3844ee9f,
1206 0x3848ee9f, 0x384cee9f, 0x3850ee9f, 0x38542a3b, 0x38581230, 0x385c0000,
1207 0x38600000, 0x38640000, 0x38680000, 0x386c0000, 0x38700000, 0x38740000,
1208 0x38780000, 0x387c0000, 0x30800000, 0x38840000, 0x38880000, 0x388c0000,
1209 0x38900000, 0x38940000, 0x38980000, 0x389c0000, 0x38a00000, 0x38a40000,
1210 0x38a80000, 0x38ac0000, 0x38b00000, 0x38b40000, 0x38b80000, 0x38bc0000,
1211 0x38c00000, 0x38c40000, 0x38c80000, 0x38cc0000, 0x38d00000, 0x38d40000,
1212 0x38d80000, 0x38dc0000, 0x38e00000, 0x38e40000, 0x38e80000, 0x38ec0000,
1213 0x38f00000, 0x38f40000, 0x38f80000, 0x38fc0000,
1215 static u32 v_gain
[64] =
1217 0x016000ff, 0x016100ff, 0x016100ff, 0x016200ff, 0x016200ff, 0x016200ff,
1218 0x016200ff, 0x016300ff, 0x016300ff, 0x016300ff, 0x016300ff, 0x016300ff,
1219 0x016300ff, 0x016300ff, 0x016300ff, 0x016400ff, 0x016400ff, 0x016400ff,
1220 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1221 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1222 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1223 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1224 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1225 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1226 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1227 0x016400ff, 0x016400ff, 0x016400ff, 0x016400ff,
1231 static void saa7146_set_winsize(struct saa7146
*saa
)
1234 int offset
, yacl
, ysci
;
1235 saa
->win
.color_fmt
= format
=
1236 (saa
->win
.depth
== 15) ? palette2fmt
[VIDEO_PALETTE_RGB555
] :
1237 palette2fmt
[bpp2fmt
[(saa
->win
.bpp
- 1) & 3]];
1238 offset
= saa
->win
.x
* saa
->win
.bpp
+ saa
->win
.y
* saa
->win
.bpl
;
1239 saawrite(saa
->win
.vidadr
+ offset
, SAA7146_BASE_EVEN1
);
1240 saawrite(saa
->win
.vidadr
+ offset
+ saa
->win
.bpl
, SAA7146_BASE_ODD1
);
1241 saawrite(saa
->win
.bpl
* 2, SAA7146_PITCH1
);
1242 saawrite(saa
->win
.vidadr
+ saa
->win
.bpl
* saa
->win
.sheight
,
1243 SAA7146_PROT_ADDR1
);
1244 saawrite(0, SAA7146_PAGE1
);
1245 saawrite(format
|0x60, SAA7146_CLIP_FORMAT_CTRL
);
1246 offset
= (704 / (saa
->win
.width
- 1)) & 0x3f;
1247 saawrite(h_prescale
[offset
], SAA7146_HPS_H_PRESCALE
);
1248 offset
= (720896 / saa
->win
.width
) / (offset
+ 1);
1249 saawrite((offset
<<12)|0x0c, SAA7146_HPS_H_SCALE
);
1250 if (CurrentMode
== VIDEO_MODE_NTSC
) {
1251 yacl
= /*(480 / saa->win.height - 1) & 0x3f*/ 0;
1252 ysci
= 1024 - (saa
->win
.height
* 1024 / 480);
1254 yacl
= /*(576 / saa->win.height - 1) & 0x3f*/ 0;
1255 ysci
= 1024 - (saa
->win
.height
* 1024 / 576);
1257 saawrite((1<<31)|(ysci
<<21)|(yacl
<<15), SAA7146_HPS_V_SCALE
);
1258 saawrite(v_gain
[yacl
], SAA7146_HPS_V_GAIN
);
1259 saawrite(((SAA7146_MC2_UPLD_DMA1
| SAA7146_MC2_UPLD_HPS_V
|
1260 SAA7146_MC2_UPLD_HPS_H
) << 16) | (SAA7146_MC2_UPLD_DMA1
|
1261 SAA7146_MC2_UPLD_HPS_V
| SAA7146_MC2_UPLD_HPS_H
),
1265 /* clip_draw_rectangle(cm,x,y,w,h) -- handle clipping an area
1266 * bitmap is fixed width, 128 bytes (1024 pixels represented)
1267 * arranged most-sigificant-bit-left in 32-bit words
1268 * based on saa7146 clipping hardware, it swaps bytes if LE
1269 * much of this makes up for egcs brain damage -- so if you
1270 * are wondering "why did he do this?" it is because the C
1271 * was adjusted to generate the optimal asm output without
1272 * writing non-portable __asm__ directives.
1275 static void clip_draw_rectangle(u32
*clipmap
, int x
, int y
, int w
, int h
)
1277 register int startword
, endword
;
1278 register u32 bitsleft
, bitsright
;
1288 if (w
<= 0 || h
<= 0 || x
> 1023 || y
> 639)
1289 return; /* throw away bad clips */
1294 startword
= (x
>> 5);
1295 endword
= ((x
+ w
) >> 5);
1296 bitsleft
= (0xffffffff >> (x
& 31));
1297 bitsright
= (0xffffffff << (~((x
+ w
) - (endword
<<5))));
1298 temp
= &clipmap
[(y
<<5) + startword
];
1299 w
= endword
- startword
;
1301 bitsleft
|= bitsright
;
1302 for (y
= 0; y
< h
; y
++) {
1307 for (y
= 0; y
< h
; y
++) {
1308 *temp
++ |= bitsleft
;
1309 for (x
= 1; x
< w
; x
++)
1310 *temp
++ = 0xffffffff;
1317 static void make_clip_tab(struct saa7146
*saa
, struct video_clip
*cr
, int ncr
)
1319 int i
, width
, height
;
1322 clipmap
= saa
->dmavid2
;
1323 if((width
=saa
->win
.width
)>1023)
1324 width
= 1023; /* sanity check */
1325 if((height
=saa
->win
.height
)>640)
1326 height
= 639; /* sanity check */
1327 if (ncr
> 0) { /* rectangles pased */
1328 /* convert rectangular clips to a bitmap */
1329 memset(clipmap
, 0, VIDEO_CLIPMAP_SIZE
); /* clear map */
1330 for (i
= 0; i
< ncr
; i
++)
1331 clip_draw_rectangle(clipmap
, cr
[i
].x
, cr
[i
].y
,
1332 cr
[i
].width
, cr
[i
].height
);
1334 /* clip against viewing window AND screen
1335 so we do not have to rely on the user program
1337 clip_draw_rectangle(clipmap
,(saa
->win
.x
+width
>saa
->win
.swidth
) ?
1338 (saa
->win
.swidth
-saa
->win
.x
) : width
, 0, 1024, 768);
1339 clip_draw_rectangle(clipmap
,0,(saa
->win
.y
+height
>saa
->win
.sheight
) ?
1340 (saa
->win
.sheight
-saa
->win
.y
) : height
,1024,768);
1342 clip_draw_rectangle(clipmap
, 0, 0, -(saa
->win
.x
), 768);
1344 clip_draw_rectangle(clipmap
, 0, 0, 1024, -(saa
->win
.y
));
1347 static int saa_ioctl(struct video_device
*dev
, unsigned int cmd
, void *arg
)
1349 struct saa7146
*saa
= (struct saa7146
*) dev
;
1353 struct video_capability b
;
1354 strcpy(b
.name
, saa
->video_dev
.name
);
1355 b
.type
= VID_TYPE_CAPTURE
|
1366 if (copy_to_user(arg
, &b
, sizeof(b
)))
1372 struct video_picture p
= saa
->picture
;
1373 if (saa
->win
.depth
== 8)
1374 p
.palette
= VIDEO_PALETTE_HI240
;
1375 if (saa
->win
.depth
== 15)
1376 p
.palette
= VIDEO_PALETTE_RGB555
;
1377 if (saa
->win
.depth
== 16)
1378 p
.palette
= VIDEO_PALETTE_RGB565
;
1379 if (saa
->win
.depth
== 24)
1380 p
.palette
= VIDEO_PALETTE_RGB24
;
1381 if (saa
->win
.depth
== 32)
1382 p
.palette
= VIDEO_PALETTE_RGB32
;
1383 if (copy_to_user(arg
, &p
, sizeof(p
)))
1389 struct video_picture p
;
1391 if (copy_from_user(&p
, arg
, sizeof(p
)))
1393 if (p
.palette
< sizeof(palette2fmt
) / sizeof(u32
)) {
1394 format
= palette2fmt
[p
.palette
];
1395 saa
->win
.color_fmt
= format
;
1396 saawrite(format
|0x60, SAA7146_CLIP_FORMAT_CTRL
);
1398 saawrite(((p
.brightness
& 0xff00) << 16) |
1399 ((p
.contrast
& 0xfe00) << 7) |
1400 ((p
.colour
& 0xfe00) >> 9), SAA7146_BCS_CTRL
);
1402 /* upload changed registers */
1403 saawrite(((SAA7146_MC2_UPLD_HPS_H
|
1404 SAA7146_MC2_UPLD_HPS_V
) << 16) |
1405 SAA7146_MC2_UPLD_HPS_H
| SAA7146_MC2_UPLD_HPS_V
,
1411 struct video_window vw
;
1412 struct video_clip
*vcp
= NULL
;
1414 if (copy_from_user(&vw
, arg
, sizeof(vw
)))
1417 if (vw
.flags
|| vw
.width
< 16 || vw
.height
< 16) { /* stop capture */
1418 saawrite((SAA7146_MC1_TR_E_1
<< 16), SAA7146_MC1
);
1421 if (saa
->win
.bpp
< 4) { /* 32-bit align start and adjust width */
1423 vw
.x
= (vw
.x
+ 3) & ~3;
1429 saa
->win
.width
= vw
.width
;
1430 if (saa
->win
.width
> 768)
1431 saa
->win
.width
= 768;
1432 saa
->win
.height
= vw
.height
;
1433 if (CurrentMode
== VIDEO_MODE_NTSC
) {
1434 if (saa
->win
.height
> 480)
1435 saa
->win
.height
= 480;
1437 if (saa
->win
.height
> 576)
1438 saa
->win
.height
= 576;
1442 saawrite((SAA7146_MC1_TR_E_1
<< 16), SAA7146_MC1
);
1443 saa7146_set_winsize(saa
);
1448 if (vw
.clipcount
< 0) {
1449 if (copy_from_user(saa
->dmavid2
, vw
.clips
,
1450 VIDEO_CLIPMAP_SIZE
))
1452 } else if (vw
.clipcount
> 0) {
1453 if ((vcp
= vmalloc(sizeof(struct video_clip
) *
1454 (vw
.clipcount
))) == NULL
)
1456 if (copy_from_user(vcp
, vw
.clips
,
1457 sizeof(struct video_clip
) *
1462 } else /* nothing clipped */
1463 memset(saa
->dmavid2
, 0, VIDEO_CLIPMAP_SIZE
);
1464 make_clip_tab(saa
, vcp
, vw
.clipcount
);
1465 if (vw
.clipcount
> 0)
1468 /* start capture & clip dma if we have an address */
1469 if ((saa
->cap
& 3) && saa
->win
.vidadr
!= 0)
1470 saawrite(((SAA7146_MC1_TR_E_1
|
1471 SAA7146_MC1_TR_E_2
) << 16) | 0xffff,
1477 struct video_window vw
;
1480 vw
.width
= saa
->win
.width
;
1481 vw
.height
= saa
->win
.height
;
1484 if (copy_to_user(arg
, &vw
, sizeof(vw
)))
1491 if (copy_from_user(&v
, arg
, sizeof(v
)))
1495 saawrite((SAA7146_MC1_TR_E_1
<< 16),
1498 if (saa
->win
.vidadr
== 0 || saa
->win
.width
== 0
1499 || saa
->win
.height
== 0)
1502 saawrite((SAA7146_MC1_TR_E_1
<< 16) | 0xffff,
1509 struct video_buffer v
;
1510 v
.base
= (void *) saa
->win
.vidadr
;
1511 v
.height
= saa
->win
.sheight
;
1512 v
.width
= saa
->win
.swidth
;
1513 v
.depth
= saa
->win
.depth
;
1514 v
.bytesperline
= saa
->win
.bpl
;
1515 if (copy_to_user(arg
, &v
, sizeof(v
)))
1522 struct video_buffer v
;
1523 if (!capable(CAP_SYS_ADMIN
))
1525 if (copy_from_user(&v
, arg
, sizeof(v
)))
1527 if (v
.depth
!= 8 && v
.depth
!= 15 && v
.depth
!= 16 &&
1528 v
.depth
!= 24 && v
.depth
!= 32 && v
.width
> 16 &&
1529 v
.height
> 16 && v
.bytesperline
> 16)
1532 saa
->win
.vidadr
= (unsigned long) v
.base
;
1533 saa
->win
.sheight
= v
.height
;
1534 saa
->win
.swidth
= v
.width
;
1535 saa
->win
.bpp
= ((v
.depth
+ 7) & 0x38) / 8;
1536 saa
->win
.depth
= v
.depth
;
1537 saa
->win
.bpl
= v
.bytesperline
;
1539 DEBUG(printk("Display at %p is %d by %d, bytedepth %d, bpl %d\n",
1540 v
.base
, v
.width
, v
.height
, saa
->win
.bpp
, saa
->win
.bpl
));
1541 saa7146_set_winsize(saa
);
1546 /* Will be handled higher up .. */
1552 struct video_audio v
;
1554 v
.flags
&= ~(VIDEO_AUDIO_MUTE
| VIDEO_AUDIO_MUTABLE
);
1555 v
.flags
|= VIDEO_AUDIO_MUTABLE
| VIDEO_AUDIO_VOLUME
;
1556 strcpy(v
.name
, "MPEG");
1557 v
.mode
= VIDEO_SOUND_STEREO
;
1558 if (copy_to_user(arg
, &v
, sizeof(v
)))
1564 struct video_audio v
;
1566 if (copy_from_user(&v
, arg
, sizeof(v
)))
1568 i
= (~(v
.volume
>>8))&0xff;
1570 if (v
.flags
& VIDEO_AUDIO_MUTE
) {
1571 debiwrite(saa
, debNormal
,
1575 if (!(v
.flags
& VIDEO_AUDIO_MUTE
))
1576 debiwrite(saa
, debNormal
,
1579 if (v
.flags
& VIDEO_AUDIO_VOLUME
)
1580 debiwrite(saa
, debNormal
,
1584 if (v
.flags
& VIDEO_AUDIO_MUTE
)
1585 cs4341_setlevel(saa
, 0xff, 0xff);
1586 if (!(v
.flags
& VIDEO_AUDIO_MUTE
))
1587 cs4341_setlevel(saa
, 0, 0);
1588 if (v
.flags
& VIDEO_AUDIO_VOLUME
)
1589 cs4341_setlevel(saa
, i
, i
);
1597 struct video_unit vu
;
1598 vu
.video
= saa
->video_dev
.minor
;
1599 vu
.vbi
= VIDEO_NO_UNIT
;
1600 vu
.radio
= VIDEO_NO_UNIT
;
1601 vu
.audio
= VIDEO_NO_UNIT
;
1602 vu
.teletext
= VIDEO_NO_UNIT
;
1603 if (copy_to_user((void *) arg
, (void *) &vu
, sizeof(vu
)))
1607 case VIDIOCSPLAYMODE
:
1609 struct video_play_mode pmode
;
1610 if (copy_from_user((void *) &pmode
, arg
,
1611 sizeof(struct video_play_mode
)))
1613 switch (pmode
.mode
) {
1614 case VID_PLAY_VID_OUT_MODE
:
1615 if (pmode
.p1
!= VIDEO_MODE_NTSC
&&
1616 pmode
.p1
!= VIDEO_MODE_PAL
)
1618 set_out_format(saa
, pmode
.p1
);
1620 case VID_PLAY_GENLOCK
:
1621 debiwrite(saa
, debNormal
,
1623 (pmode
.p1
? 0x8000 : 0x8080),
1626 set_genlock_offset(saa
,
1629 case VID_PLAY_NORMAL
:
1630 debiwrite(saa
, debNormal
,
1631 IBM_MP2_CHIP_CONTROL
,
1633 ibm_send_command(saa
,
1634 IBM_MP2_PLAY
, 0, 0);
1635 saa
->playmode
= pmode
.mode
;
1637 case VID_PLAY_PAUSE
:
1638 /* IBM removed the PAUSE command */
1639 /* they say use SINGLE_FRAME now */
1640 case VID_PLAY_SINGLE_FRAME
:
1641 ibm_send_command(saa
,
1642 IBM_MP2_SINGLE_FRAME
,
1644 if (saa
->playmode
== pmode
.mode
) {
1645 debiwrite(saa
, debNormal
,
1646 IBM_MP2_CHIP_CONTROL
,
1649 saa
->playmode
= pmode
.mode
;
1651 case VID_PLAY_FAST_FORWARD
:
1652 ibm_send_command(saa
,
1653 IBM_MP2_FAST_FORWARD
, 0, 0);
1654 saa
->playmode
= pmode
.mode
;
1656 case VID_PLAY_SLOW_MOTION
:
1657 ibm_send_command(saa
,
1658 IBM_MP2_SLOW_MOTION
,
1660 saa
->playmode
= pmode
.mode
;
1662 case VID_PLAY_IMMEDIATE_NORMAL
:
1663 /* ensure transfers resume */
1664 debiwrite(saa
, debNormal
,
1665 IBM_MP2_CHIP_CONTROL
,
1667 ibm_send_command(saa
,
1668 IBM_MP2_IMED_NORM_PLAY
, 0, 0);
1669 saa
->playmode
= VID_PLAY_NORMAL
;
1671 case VID_PLAY_SWITCH_CHANNELS
:
1672 saa
->audhead
= saa
->audtail
= 0;
1673 saa
->vidhead
= saa
->vidtail
= 0;
1674 ibm_send_command(saa
,
1675 IBM_MP2_FREEZE_FRAME
, 0, 1);
1676 ibm_send_command(saa
,
1677 IBM_MP2_RESET_AUD_RATE
, 0, 1);
1678 debiwrite(saa
, debNormal
,
1679 IBM_MP2_CHIP_CONTROL
, 0, 2);
1680 ibm_send_command(saa
,
1681 IBM_MP2_CHANNEL_SWITCH
, 0, 1);
1682 debiwrite(saa
, debNormal
,
1683 IBM_MP2_CHIP_CONTROL
,
1685 ibm_send_command(saa
,
1686 IBM_MP2_PLAY
, 0, 0);
1687 saa
->playmode
= VID_PLAY_NORMAL
;
1689 case VID_PLAY_FREEZE_FRAME
:
1690 ibm_send_command(saa
,
1691 IBM_MP2_FREEZE_FRAME
, 0, 0);
1692 saa
->playmode
= pmode
.mode
;
1694 case VID_PLAY_STILL_MODE
:
1695 ibm_send_command(saa
,
1696 IBM_MP2_SET_STILL_MODE
, 0, 0);
1697 saa
->playmode
= pmode
.mode
;
1699 case VID_PLAY_MASTER_MODE
:
1700 if (pmode
.p1
== VID_PLAY_MASTER_NONE
)
1701 saa
->boardcfg
[1] = 0x13;
1702 else if (pmode
.p1
==
1703 VID_PLAY_MASTER_VIDEO
)
1704 saa
->boardcfg
[1] = 0x23;
1705 else if (pmode
.p1
==
1706 VID_PLAY_MASTER_AUDIO
)
1707 saa
->boardcfg
[1] = 0x43;
1710 debiwrite(saa
, debNormal
,
1711 IBM_MP2_CHIP_CONTROL
,
1714 case VID_PLAY_ACTIVE_SCANLINES
:
1715 if (CurrentMode
== VIDEO_MODE_PAL
) {
1719 saa
->boardcfg
[5] = pmode
.p1
;
1720 saa
->boardcfg
[55] = (pmode
.p1
+
1727 saa
->boardcfg
[4] = pmode
.p1
;
1728 saa
->boardcfg
[54] = (pmode
.p1
+
1732 set_out_format(saa
, CurrentMode
);
1733 case VID_PLAY_RESET
:
1734 return do_ibm_reset(saa
);
1735 case VID_PLAY_END_MARK
:
1736 if (saa
->endmarktail
<
1738 if (saa
->endmarkhead
-
1739 saa
->endmarktail
< 2)
1741 } else if (saa
->endmarkhead
<=
1743 if (saa
->endmarktail
-
1749 saa
->endmark
[saa
->endmarktail
] =
1752 if (saa
->endmarktail
>= MAX_MARKS
)
1753 saa
->endmarktail
= 0;
1757 case VIDIOCSWRITEMODE
:
1760 if (copy_from_user((void *) &mode
, arg
, sizeof(int)))
1762 if (mode
== VID_WRITE_MPEG_AUD
||
1763 mode
== VID_WRITE_MPEG_VID
||
1764 mode
== VID_WRITE_CC
||
1765 mode
== VID_WRITE_TTX
||
1766 mode
== VID_WRITE_OSD
) {
1767 saa
->writemode
= mode
;
1772 case VIDIOCSMICROCODE
:
1774 struct video_code ucode
;
1777 if (copy_from_user((void *) &ucode
, arg
,
1780 if (ucode
.datasize
> 65536 || ucode
.datasize
< 1024 ||
1781 strncmp(ucode
.loadwhat
, "dec", 3))
1783 if ((udata
= vmalloc(ucode
.datasize
)) == NULL
)
1785 if (copy_from_user((void *) udata
, ucode
.data
,
1791 if (!strncmp(ucode
.loadwhat
, "decoder.aud", 11)
1792 || !strncmp(ucode
.loadwhat
, "decoder.vid", 11))
1793 i
= initialize_ibmmpeg2(&ucode
);
1795 i
= initialize_fpga(&ucode
);
1802 case VIDIOCGCHAN
: /* this makes xawtv happy */
1804 struct video_channel v
;
1805 if (copy_from_user(&v
, arg
, sizeof(v
)))
1807 v
.flags
= VIDEO_VC_AUDIO
;
1809 v
.type
= VID_TYPE_MPEG_DECODER
;
1810 v
.norm
= CurrentMode
;
1811 strcpy(v
.name
, "MPEG2");
1812 if (copy_to_user(arg
, &v
, sizeof(v
)))
1816 case VIDIOCSCHAN
: /* this makes xawtv happy */
1818 struct video_channel v
;
1819 if (copy_from_user(&v
, arg
, sizeof(v
)))
1825 return -ENOIOCTLCMD
;
1830 static int saa_init_done(struct video_device
*dev
)
1835 static int saa_mmap(struct video_device
*dev
, const char *adr
,
1838 struct saa7146
*saa
= (struct saa7146
*) dev
;
1839 printk(KERN_DEBUG
"stradis%d: saa_mmap called\n", saa
->nr
);
1843 static long saa_read(struct video_device
*dev
, char *buf
,
1844 unsigned long count
, int nonblock
)
1849 static long saa_write(struct video_device
*dev
, const char *buf
,
1850 unsigned long count
, int nonblock
)
1852 struct saa7146
*saa
= (struct saa7146
*) dev
;
1853 unsigned long todo
= count
;
1854 int blocksize
, split
;
1855 unsigned long flags
;
1858 if (saa
->writemode
== VID_WRITE_MPEG_AUD
) {
1859 spin_lock_irqsave(&saa
->lock
, flags
);
1860 if (saa
->audhead
<= saa
->audtail
)
1861 blocksize
= 65536-(saa
->audtail
- saa
->audhead
);
1863 blocksize
= saa
->audhead
- saa
->audtail
;
1864 spin_unlock_irqrestore(&saa
->lock
, flags
);
1865 if (blocksize
< 16384) {
1866 saawrite(SAA7146_PSR_DEBI_S
|
1867 SAA7146_PSR_PIN1
, SAA7146_IER
);
1868 saawrite(SAA7146_PSR_PIN1
, SAA7146_PSR
);
1869 /* wait for buffer space to open */
1870 interruptible_sleep_on(&saa
->audq
);
1872 spin_lock_irqsave(&saa
->lock
, flags
);
1873 if (saa
->audhead
<= saa
->audtail
) {
1874 blocksize
= 65536-(saa
->audtail
- saa
->audhead
);
1875 split
= 65536 - saa
->audtail
;
1877 blocksize
= saa
->audhead
- saa
->audtail
;
1880 spin_unlock_irqrestore(&saa
->lock
, flags
);
1882 if (blocksize
> todo
)
1884 /* double check that we really have space */
1887 if (split
< blocksize
) {
1888 if (copy_from_user(saa
->audbuf
+
1889 saa
->audtail
, buf
, split
))
1896 if (copy_from_user(saa
->audbuf
+ saa
->audtail
, buf
,
1899 saa
->audtail
+= blocksize
;
1902 saa
->audtail
&= 0xffff;
1903 } else if (saa
->writemode
== VID_WRITE_MPEG_VID
) {
1904 spin_lock_irqsave(&saa
->lock
, flags
);
1905 if (saa
->vidhead
<= saa
->vidtail
)
1906 blocksize
=524288-(saa
->vidtail
- saa
->vidhead
);
1908 blocksize
= saa
->vidhead
- saa
->vidtail
;
1909 spin_unlock_irqrestore(&saa
->lock
, flags
);
1910 if (blocksize
< 65536) {
1911 saawrite(SAA7146_PSR_DEBI_S
|
1912 SAA7146_PSR_PIN1
, SAA7146_IER
);
1913 saawrite(SAA7146_PSR_PIN1
, SAA7146_PSR
);
1914 /* wait for buffer space to open */
1915 interruptible_sleep_on(&saa
->vidq
);
1917 spin_lock_irqsave(&saa
->lock
, flags
);
1918 if (saa
->vidhead
<= saa
->vidtail
) {
1919 blocksize
=524288-(saa
->vidtail
- saa
->vidhead
);
1920 split
= 524288 - saa
->vidtail
;
1922 blocksize
= saa
->vidhead
- saa
->vidtail
;
1925 spin_unlock_irqrestore(&saa
->lock
, flags
);
1927 if (blocksize
> todo
)
1929 /* double check that we really have space */
1932 if (split
< blocksize
) {
1933 if (copy_from_user(saa
->vidbuf
+
1934 saa
->vidtail
, buf
, split
))
1941 if (copy_from_user(saa
->vidbuf
+ saa
->vidtail
, buf
,
1944 saa
->vidtail
+= blocksize
;
1947 saa
->vidtail
&= 0x7ffff;
1948 } else if (saa
->writemode
== VID_WRITE_OSD
) {
1951 if (copy_from_user(saa
->osdbuf
, buf
, count
))
1955 saa
->osdtail
= count
;
1956 debiwrite(saa
, debNormal
, IBM_MP2_OSD_ADDR
, 0, 2);
1957 debiwrite(saa
, debNormal
, IBM_MP2_OSD_LINK_ADDR
, 0, 2);
1958 debiwrite(saa
, debNormal
, IBM_MP2_MASK0
, 0xc00d, 2);
1959 debiwrite(saa
, debNormal
, IBM_MP2_DISP_MODE
,
1960 debiread(saa
, debNormal
,
1961 IBM_MP2_DISP_MODE
, 2) | 1, 2);
1962 /* trigger osd data transfer */
1963 saawrite(SAA7146_PSR_DEBI_S
|
1964 SAA7146_PSR_PIN1
, SAA7146_IER
);
1965 saawrite(SAA7146_PSR_PIN1
, SAA7146_PSR
);
1971 static int saa_open(struct video_device
*dev
, int flags
)
1973 struct saa7146
*saa
= (struct saa7146
*) dev
;
1975 saa
->video_dev
.busy
= 0;
1978 return 0; /* device open already, don't reset */
1979 saa
->writemode
= VID_WRITE_MPEG_VID
; /* default to video */
1983 static void saa_close(struct video_device
*dev
)
1985 struct saa7146
*saa
= (struct saa7146
*) dev
;
1987 saa
->video_dev
.busy
= 0;
1988 if (saa
->user
> 0) /* still someone using device */
1990 saawrite(0x007f0000, SAA7146_MC1
); /* stop all overlay dma */
1993 /* template for video_device-structure */
1994 static struct video_device saa_template
=
1997 VID_TYPE_CAPTURE
| VID_TYPE_OVERLAY
,
1998 VID_HARDWARE_SAA7146
,
2012 static int configure_saa7146(struct pci_dev
*dev
, int num
)
2015 struct saa7146
*saa
;
2017 saa
= &saa7146s
[num
];
2019 saa
->endmarkhead
= saa
->endmarktail
= 0;
2020 saa
->win
.x
= saa
->win
.y
= 0;
2021 saa
->win
.width
= saa
->win
.cropwidth
= 720;
2022 saa
->win
.height
= saa
->win
.cropheight
= 480;
2023 saa
->win
.cropx
= saa
->win
.cropy
= 0;
2025 saa
->win
.depth
= 16;
2026 saa
->win
.color_fmt
= palette2fmt
[VIDEO_PALETTE_RGB565
];
2027 saa
->win
.bpl
= 1024 * saa
->win
.bpp
;
2028 saa
->win
.swidth
= 1024;
2029 saa
->win
.sheight
= 768;
2030 saa
->picture
.brightness
= 32768;
2031 saa
->picture
.contrast
= 38768;
2032 saa
->picture
.colour
= 32768;
2036 saa
->playmode
= VID_PLAY_NORMAL
;
2037 memset(saa
->boardcfg
, 0, 64); /* clear board config area */
2038 saa
->saa7146_mem
= NULL
;
2039 saa
->dmavid1
= saa
->dmavid2
= saa
->dmavid3
= saa
->dmaa1in
=
2040 saa
->dmaa1out
= saa
->dmaa2in
= saa
->dmaa2out
=
2041 saa
->pagevid1
= saa
->pagevid2
= saa
->pagevid3
= saa
->pagea1in
=
2042 saa
->pagea1out
= saa
->pagea2in
= saa
->pagea2out
=
2043 saa
->pagedebi
= saa
->dmaRPS1
= saa
->dmaRPS2
= saa
->pageRPS1
=
2044 saa
->pageRPS2
= NULL
;
2045 saa
->audbuf
= saa
->vidbuf
= saa
->osdbuf
= saa
->dmadebi
= NULL
;
2046 saa
->audhead
= saa
->vidtail
= 0;
2048 init_waitqueue_head(&saa
->i2cq
);
2049 init_waitqueue_head(&saa
->audq
);
2050 init_waitqueue_head(&saa
->debiq
);
2051 init_waitqueue_head(&saa
->vidq
);
2052 spin_lock_init(&saa
->lock
);
2054 if (pci_enable_device(dev
))
2057 saa
->id
= dev
->device
;
2058 saa
->irq
= dev
->irq
;
2059 saa
->video_dev
.minor
= -1;
2060 saa
->saa7146_adr
= pci_resource_start(dev
, 0);
2061 pci_read_config_byte(dev
, PCI_CLASS_REVISION
, &saa
->revision
);
2063 saa
->saa7146_mem
= ioremap(saa
->saa7146_adr
, 0x200);
2064 if (!saa
->saa7146_mem
)
2067 memcpy(&(saa
->i2c
), &saa7146_i2c_bus_template
, sizeof(struct i2c_bus
));
2068 memcpy(&saa
->video_dev
, &saa_template
, sizeof(saa_template
));
2069 sprintf(saa
->i2c
.name
, "stradis%d", num
);
2070 saa
->i2c
.data
= saa
;
2071 saawrite(0, SAA7146_IER
); /* turn off all interrupts */
2072 result
= request_irq(saa
->irq
, saa7146_irq
,
2073 SA_SHIRQ
| SA_INTERRUPT
, "stradis", (void *) saa
);
2074 if (result
== -EINVAL
)
2075 printk(KERN_ERR
"stradis%d: Bad irq number or handler\n",
2077 if (result
== -EBUSY
)
2078 printk(KERN_ERR
"stradis%d: IRQ %ld busy, change your PnP"
2079 " config in BIOS\n", num
, saa
->irq
);
2082 pci_set_master(dev
);
2083 if (video_register_device(&saa
->video_dev
, VFL_TYPE_GRABBER
) < 0)
2086 /* i2c generic interface is currently BROKEN */
2087 i2c_register_bus(&saa
->i2c
);
2092 static int init_saa7146(int i
)
2094 struct saa7146
*saa
= &saa7146s
[i
];
2097 /* reset the saa7146 */
2098 saawrite(0xffff0000, SAA7146_MC1
);
2100 /* enable debi and i2c transfers and pins */
2101 saawrite(((SAA7146_MC1_EDP
| SAA7146_MC1_EI2C
|
2102 SAA7146_MC1_TR_E_DEBI
) << 16) | 0xffff, SAA7146_MC1
);
2103 /* ensure proper state of chip */
2104 saawrite(0x00000000, SAA7146_PAGE1
);
2105 saawrite(0x00f302c0, SAA7146_NUM_LINE_BYTE1
);
2106 saawrite(0x00000000, SAA7146_PAGE2
);
2107 saawrite(0x01400080, SAA7146_NUM_LINE_BYTE2
);
2108 saawrite(0x00000000, SAA7146_DD1_INIT
);
2109 saawrite(0x00000000, SAA7146_DD1_STREAM_B
);
2110 saawrite(0x00000000, SAA7146_DD1_STREAM_A
);
2111 saawrite(0x00000000, SAA7146_BRS_CTRL
);
2112 saawrite(0x80400040, SAA7146_BCS_CTRL
);
2113 saawrite(0x0000e000 /*| (1<<29)*/, SAA7146_HPS_CTRL
);
2114 saawrite(0x00000060, SAA7146_CLIP_FORMAT_CTRL
);
2115 saawrite(0x00000000, SAA7146_ACON1
);
2116 saawrite(0x00000000, SAA7146_ACON2
);
2117 saawrite(0x00000600, SAA7146_I2C_STATUS
);
2118 saawrite(((SAA7146_MC2_UPLD_D1_B
| SAA7146_MC2_UPLD_D1_A
|
2119 SAA7146_MC2_UPLD_BRS
| SAA7146_MC2_UPLD_HPS_H
|
2120 SAA7146_MC2_UPLD_HPS_V
| SAA7146_MC2_UPLD_DMA2
|
2121 SAA7146_MC2_UPLD_DMA1
| SAA7146_MC2_UPLD_I2C
) << 16) | 0xffff,
2123 /* setup arbitration control registers */
2124 saawrite(0x1412121a, SAA7146_PCI_BT_V1
);
2126 /* allocate 32k dma buffer + 4k for page table */
2127 if ((saa
->dmadebi
= kmalloc(32768 + 4096, GFP_KERNEL
)) == NULL
) {
2128 printk(KERN_ERR
"stradis%d: debi kmalloc failed\n", i
);
2132 saa
->pagedebi
= saa
->dmadebi
+ 32768; /* top 4k is for mmu */
2133 saawrite(virt_to_bus(saa
->pagedebi
) /*|0x800 */ , SAA7146_DEBI_PAGE
);
2134 for (i
= 0; i
< 12; i
++) /* setup mmu page table */
2135 saa
->pagedebi
[i
] = virt_to_bus((saa
->dmadebi
+ i
* 4096));
2137 saa
->audhead
= saa
->vidhead
= saa
->osdhead
= 0;
2138 saa
->audtail
= saa
->vidtail
= saa
->osdtail
= 0;
2139 if (saa
->vidbuf
== NULL
)
2140 if ((saa
->vidbuf
= vmalloc(524288)) == NULL
) {
2141 printk(KERN_ERR
"stradis%d: malloc failed\n", saa
->nr
);
2144 if (saa
->audbuf
== NULL
)
2145 if ((saa
->audbuf
= vmalloc(65536)) == NULL
) {
2146 printk(KERN_ERR
"stradis%d: malloc failed\n", saa
->nr
);
2151 if (saa
->osdbuf
== NULL
)
2152 if ((saa
->osdbuf
= vmalloc(131072)) == NULL
) {
2153 printk(KERN_ERR
"stradis%d: malloc failed\n", saa
->nr
);
2156 saa
->vidbuf
= saa
->audbuf
= NULL
;
2159 /* allocate 81920 byte buffer for clipping */
2160 if ((saa
->dmavid2
= kmalloc(VIDEO_CLIPMAP_SIZE
, GFP_KERNEL
)) == NULL
) {
2161 printk(KERN_ERR
"stradis%d: clip kmalloc failed\n", saa
->nr
);
2165 saa
->vidbuf
= saa
->audbuf
= saa
->osdbuf
= NULL
;
2166 saa
->dmavid2
= NULL
;
2169 memset(saa
->dmavid2
, 0x00, VIDEO_CLIPMAP_SIZE
); /* clip everything */
2170 /* setup clipping registers */
2171 saawrite(virt_to_bus(saa
->dmavid2
), SAA7146_BASE_EVEN2
);
2172 saawrite(virt_to_bus(saa
->dmavid2
) + 128, SAA7146_BASE_ODD2
);
2173 saawrite(virt_to_bus(saa
->dmavid2
) + VIDEO_CLIPMAP_SIZE
,
2174 SAA7146_PROT_ADDR2
);
2175 saawrite(256, SAA7146_PITCH2
);
2176 saawrite(4, SAA7146_PAGE2
); /* dma direction: read, no byteswap */
2177 saawrite(((SAA7146_MC2_UPLD_DMA2
) << 16) | SAA7146_MC2_UPLD_DMA2
,
2179 I2CBusScan(&(saa
->i2c
));
2183 static void release_saa(void)
2187 struct saa7146
*saa
;
2189 for (i
= 0; i
< saa_num
; i
++) {
2192 /* turn off all capturing, DMA and IRQs */
2193 saawrite(0xffff0000, SAA7146_MC1
); /* reset chip */
2194 saawrite(0, SAA7146_MC2
);
2195 saawrite(0, SAA7146_IER
);
2196 saawrite(0xffffffffUL
, SAA7146_ISR
);
2198 /* unregister i2c_bus */
2199 i2c_unregister_bus((&saa
->i2c
));
2202 /* disable PCI bus-mastering */
2203 pci_read_config_byte(saa
->dev
, PCI_COMMAND
, &command
);
2204 /* Should this be &=~ ?? */
2205 command
&= ~PCI_COMMAND_MASTER
;
2206 pci_write_config_byte(saa
->dev
, PCI_COMMAND
, command
);
2207 /* unmap and free memory */
2208 saa
->audhead
= saa
->audtail
= saa
->osdhead
= 0;
2209 saa
->vidhead
= saa
->vidtail
= saa
->osdtail
= 0;
2217 kfree((void *) saa
->dmavid2
);
2218 saa
->audbuf
= saa
->vidbuf
= saa
->osdbuf
= NULL
;
2219 saa
->dmavid2
= NULL
;
2221 kfree((void *) saa
->dmadebi
);
2223 kfree((void *) saa
->dmavid1
);
2225 kfree((void *) saa
->dmavid2
);
2227 kfree((void *) saa
->dmavid3
);
2229 kfree((void *) saa
->dmaa1in
);
2231 kfree((void *) saa
->dmaa1out
);
2233 kfree((void *) saa
->dmaa2in
);
2235 kfree((void *) saa
->dmaa2out
);
2237 kfree((void *) saa
->dmaRPS1
);
2239 kfree((void *) saa
->dmaRPS2
);
2240 free_irq(saa
->irq
, saa
);
2241 if (saa
->saa7146_mem
)
2242 iounmap(saa
->saa7146_mem
);
2243 if (saa
->video_dev
.minor
!= -1)
2244 video_unregister_device(&saa
->video_dev
);
2249 static int __init
stradis_init (void)
2251 struct pci_dev
*dev
= NULL
;
2256 while ((dev
= pci_find_device(PCI_VENDOR_ID_PHILIPS
, PCI_DEVICE_ID_PHILIPS_SAA7146
, dev
))) {
2257 if (!dev
->subsystem_vendor
)
2258 printk(KERN_INFO
"stradis%d: rev1 decoder\n", saa_num
);
2260 printk(KERN_INFO
"stradis%d: SDM2xx found\n", saa_num
);
2261 result
= configure_saa7146(dev
, saa_num
++);
2266 printk(KERN_INFO
"stradis: %d card(s) found.\n", saa_num
);
2269 for (i
= 0; i
< saa_num
; i
++)
2270 if (init_saa7146(i
) < 0) {
2278 static void __exit
stradis_exit (void)
2281 printk(KERN_INFO
"stradis: module cleanup complete\n");
2285 module_init(stradis_init
);
2286 module_exit(stradis_exit
);