2 * bt878.c: part of the driver for the Pinnacle PCTV Sat DVB PCI card
4 * Copyright (C) 2002 Peter Hettkamp <peter.hettkamp@htp-tel.de>
6 * large parts based on the bttv driver
7 * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@metzlerbros.de)
8 * & Marcus Metzler (mocm@metzlerbros.de)
9 * (c) 1999,2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
30 #include <linux/module.h>
31 #include <linux/moduleparam.h>
32 #include <linux/kernel.h>
33 #include <linux/pci.h>
35 #include <linux/ioport.h>
36 #include <asm/pgtable.h>
38 #include <linux/types.h>
39 #include <linux/interrupt.h>
40 #include <linux/kmod.h>
41 #include <linux/vmalloc.h>
42 #include <linux/init.h>
50 /**************************************/
51 /* Miscellaneous utility definitions */
52 /**************************************/
54 static unsigned int bt878_verbose = 1;
55 static unsigned int bt878_debug;
57 module_param_named(verbose, bt878_verbose, int, 0444);
58 MODULE_PARM_DESC(verbose,
59 "verbose startup messages, default is 1 (yes)");
60 module_param_named(debug, bt878_debug, int, 0644);
61 MODULE_PARM_DESC(debug, "Turn on/off debugging, default is 0 (off).");
64 struct bt878 bt878[BT878_MAX];
66 EXPORT_SYMBOL(bt878_debug);
67 EXPORT_SYMBOL(bt878_verbose);
68 EXPORT_SYMBOL(bt878_num);
71 #define btwrite(dat,adr) bmtwrite((dat), (bt->bt878_mem+(adr)))
72 #define btread(adr) bmtread(bt->bt878_mem+(adr))
74 #define btand(dat,adr) btwrite((dat) & btread(adr), adr)
75 #define btor(dat,adr) btwrite((dat) | btread(adr), adr)
76 #define btaor(dat,mask,adr) btwrite((dat) | ((mask) & btread(adr)), adr)
81 #define dprintk if(bt878_debug) printk
83 static void bt878_mem_free(struct bt878 *bt)
86 pci_free_consistent(bt->dev, bt->buf_size, bt->buf_cpu,
92 pci_free_consistent(bt->dev, bt->risc_size, bt->risc_cpu,
98 static int bt878_mem_alloc(struct bt878 *bt)
101 bt->buf_size = 128 * 1024;
104 pci_alloc_consistent(bt->dev, bt->buf_size,
110 memset(bt->buf_cpu, 0, bt->buf_size);
114 bt->risc_size = PAGE_SIZE;
116 pci_alloc_consistent(bt->dev, bt->risc_size,
124 memset(bt->risc_cpu, 0, bt->risc_size);
130 /* RISC instructions */
131 #define RISC_WRITE (0x01 << 28)
132 #define RISC_JUMP (0x07 << 28)
133 #define RISC_SYNC (0x08 << 28)
136 #define RISC_WR_SOL (1 << 27)
137 #define RISC_WR_EOL (1 << 26)
138 #define RISC_IRQ (1 << 24)
139 #define RISC_STATUS(status) ((((~status) & 0x0F) << 20) | ((status & 0x0F) << 16))
140 #define RISC_SYNC_RESYNC (1 << 15)
141 #define RISC_SYNC_FM1 0x06
142 #define RISC_SYNC_VRO 0x0C
144 #define RISC_FLUSH() bt->risc_pos = 0
145 #define RISC_INSTR(instr) bt->risc_cpu[bt->risc_pos++] = cpu_to_le32(instr)
147 static int bt878_make_risc(struct bt878 *bt)
149 bt->block_bytes = bt->buf_size >> 4;
150 bt->block_count = 1 << 4;
151 bt->line_bytes = bt->block_bytes;
152 bt->line_count = bt->block_count;
154 while (bt->line_bytes > 4095) {
155 bt->line_bytes >>= 1;
156 bt->line_count <<= 1;
159 if (bt->line_count > 255) {
160 printk("bt878: buffer size error!\n");
167 static void bt878_risc_program(struct bt878 *bt, u32 op_sync_orin)
173 RISC_INSTR(RISC_SYNC | RISC_SYNC_FM1 | op_sync_orin);
176 dprintk("bt878: risc len lines %u, bytes per line %u\n",
177 bt->line_count, bt->line_bytes);
178 for (line = 0; line < bt->line_count; line++) {
179 // At the beginning of every block we issue an IRQ with previous (finished) block number set
180 if (!(buf_pos % bt->block_bytes))
181 RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL |
183 RISC_STATUS(((buf_pos /
187 bt->block_count) | bt->
190 RISC_INSTR(RISC_WRITE | RISC_WR_SOL | RISC_WR_EOL |
192 RISC_INSTR(bt->buf_dma + buf_pos);
193 buf_pos += bt->line_bytes;
196 RISC_INSTR(RISC_SYNC | op_sync_orin | RISC_SYNC_VRO);
199 RISC_INSTR(RISC_JUMP);
200 RISC_INSTR(bt->risc_dma);
202 btwrite((bt->line_count << 16) | bt->line_bytes, BT878_APACK_LEN);
205 /*****************************/
206 /* Start/Stop grabbing funcs */
207 /*****************************/
209 void bt878_start(struct bt878 *bt, u32 controlreg, u32 op_sync_orin,
214 dprintk("bt878 debug: bt878_start (ctl=%8.8x)\n", controlreg);
215 /* complete the writing of the risc dma program now we have
218 bt878_risc_program(bt, op_sync_orin);
222 btwrite(bt->risc_dma, BT878_ARISC_START);
224 /* original int mask had :
226 * 1111 1111 1000 0000 0000
227 * SCERR|OCERR|PABORT|RIPERR|FDSR|FTRGT|FBUS|RISCI
229 * SCERR | OCERR | FDSR | FTRGT | FBUS | RISCI
231 int_mask = BT878_ASCERR | BT878_AOCERR | BT878_APABORT |
232 BT878_ARIPERR | BT878_APPERR | BT878_AFDSR | BT878_AFTRGT |
233 BT878_AFBUS | BT878_ARISCI;
236 /* ignore pesky bits */
237 int_mask &= ~irq_err_ignore;
239 btwrite(int_mask, BT878_AINT_MASK);
240 btwrite(controlreg, BT878_AGPIO_DMA_CTL);
243 void bt878_stop(struct bt878 *bt)
248 dprintk("bt878 debug: bt878_stop\n");
250 btwrite(0, BT878_AINT_MASK);
251 btand(~0x13, BT878_AGPIO_DMA_CTL);
254 stat = btread(BT878_AINT_STAT);
255 if (!(stat & BT878_ARISC_EN))
260 dprintk("bt878(%d) debug: bt878_stop, i=%d, stat=0x%8.8x\n",
264 EXPORT_SYMBOL(bt878_start);
265 EXPORT_SYMBOL(bt878_stop);
267 /*****************************/
268 /* Interrupt service routine */
269 /*****************************/
271 static irqreturn_t bt878_irq(int irq, void *dev_id, struct pt_regs *regs)
273 u32 stat, astat, mask;
277 bt = (struct bt878 *) dev_id;
281 stat = btread(BT878_AINT_STAT);
282 mask = btread(BT878_AINT_MASK);
283 if (!(astat = (stat & mask)))
284 return IRQ_NONE; /* this interrupt is not for me */
285 /* dprintk("bt878(%d) debug: irq count %d, stat 0x%8.8x, mask 0x%8.8x\n",bt->nr,count,stat,mask); */
286 btwrite(astat, BT878_AINT_STAT); /* try to clear interupt condition */
289 if (astat & (BT878_ASCERR | BT878_AOCERR)) {
291 printk("bt878(%d): irq%s%s risc_pc=%08x\n",
293 (astat & BT878_ASCERR) ? " SCERR" :
295 (astat & BT878_AOCERR) ? " OCERR" :
296 "", btread(BT878_ARISC_PC));
299 if (astat & (BT878_APABORT | BT878_ARIPERR | BT878_APPERR)) {
302 ("bt878(%d): irq%s%s%s risc_pc=%08x\n",
304 (astat & BT878_APABORT) ? " PABORT" :
306 (astat & BT878_ARIPERR) ? " RIPERR" :
308 (astat & BT878_APPERR) ? " PPERR" :
309 "", btread(BT878_ARISC_PC));
312 if (astat & (BT878_AFDSR | BT878_AFTRGT | BT878_AFBUS)) {
315 ("bt878(%d): irq%s%s%s risc_pc=%08x\n",
317 (astat & BT878_AFDSR) ? " FDSR" : "",
318 (astat & BT878_AFTRGT) ? " FTRGT" :
320 (astat & BT878_AFBUS) ? " FBUS" : "",
321 btread(BT878_ARISC_PC));
324 if (astat & BT878_ARISCI) {
325 bt->finished_block = (stat & BT878_ARISCS) >> 28;
326 tasklet_schedule(&bt->tasklet);
331 btwrite(0, BT878_AINT_MASK);
333 "bt878(%d): IRQ lockup, cleared int mask\n",
342 bt878_device_control(struct bt878 *bt, unsigned int cmd, union dst_gpio_packet *mp)
347 if (down_interruptible (&bt->gpio_lock))
349 /* special gpio signal */
352 // dprintk("dvb_bt8xx: dst enable mask 0x%02x enb 0x%02x \n", mp->dstg.enb.mask, mp->dstg.enb.enable);
353 retval = bttv_gpio_enable(bt->bttv_nr,
358 // dprintk("dvb_bt8xx: dst write gpio mask 0x%02x out 0x%02x\n", mp->dstg.outp.mask, mp->dstg.outp.highvals);
359 retval = bttv_write_gpio(bt->bttv_nr,
366 retval = bttv_read_gpio(bt->bttv_nr, &mp->rd.value);
367 // dprintk("dvb_bt8xx: dst read gpio 0x%02x\n", (unsigned)mp->dstg.rd.value);
370 /* Set packet size */
371 bt->TS_Size = mp->psize;
382 EXPORT_SYMBOL(bt878_device_control);
384 /***********************/
385 /* PCI device handling */
386 /***********************/
388 static int __devinit bt878_probe(struct pci_dev *dev,
389 const struct pci_device_id *pci_id)
394 #if defined(__powerpc__)
398 printk(KERN_INFO "bt878: Bt878 AUDIO function found (%d).\n",
400 if (pci_enable_device(dev))
403 bt = &bt878[bt878_num];
408 bt->id = dev->device;
410 bt->bt878_adr = pci_resource_start(dev, 0);
411 if (!request_mem_region(pci_resource_start(dev, 0),
412 pci_resource_len(dev, 0), "bt878")) {
417 pci_read_config_byte(dev, PCI_CLASS_REVISION, &bt->revision);
418 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
419 printk(KERN_INFO "bt878(%d): Bt%x (rev %d) at %02x:%02x.%x, ",
420 bt878_num, bt->id, bt->revision, dev->bus->number,
421 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
422 printk("irq: %d, latency: %d, memory: 0x%lx\n",
423 bt->irq, lat, bt->bt878_adr);
426 #if defined(__powerpc__)
427 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
428 /* response on cards with no firmware is not enabled by OF */
429 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
430 cmd = (cmd | PCI_COMMAND_MEMORY);
431 pci_write_config_dword(dev, PCI_COMMAND, cmd);
435 bt->bt878_mem = (unsigned char *) bt->bt878_adr;
437 bt->bt878_mem = ioremap(bt->bt878_adr, 0x1000);
440 /* clear interrupt mask */
441 btwrite(0, BT848_INT_MASK);
443 result = request_irq(bt->irq, bt878_irq,
444 SA_SHIRQ | SA_INTERRUPT, "bt878",
446 if (result == -EINVAL) {
447 printk(KERN_ERR "bt878(%d): Bad irq number or handler\n",
451 if (result == -EBUSY) {
453 "bt878(%d): IRQ %d busy, change your PnP config in BIOS\n",
461 pci_set_drvdata(dev, bt);
463 /* if(init_bt878(btv) < 0) {
469 if ((result = bt878_mem_alloc(bt))) {
470 printk("bt878: failed to allocate memory!\n");
475 btwrite(0, BT878_AINT_MASK);
481 free_irq(bt->irq, bt);
483 release_mem_region(pci_resource_start(bt->dev, 0),
484 pci_resource_len(bt->dev, 0));
486 pci_disable_device(dev);
490 static void __devexit bt878_remove(struct pci_dev *pci_dev)
493 struct bt878 *bt = pci_get_drvdata(pci_dev);
496 printk("bt878(%d): unloading\n", bt->nr);
498 /* turn off all capturing, DMA and IRQs */
499 btand(~0x13, BT878_AGPIO_DMA_CTL);
501 /* first disable interrupts before unmapping the memory! */
502 btwrite(0, BT878_AINT_MASK);
503 btwrite(~0U, BT878_AINT_STAT);
505 /* disable PCI bus-mastering */
506 pci_read_config_byte(bt->dev, PCI_COMMAND, &command);
507 /* Should this be &=~ ?? */
508 command &= ~PCI_COMMAND_MASTER;
509 pci_write_config_byte(bt->dev, PCI_COMMAND, command);
511 free_irq(bt->irq, bt);
512 printk(KERN_DEBUG "bt878_mem: 0x%p.\n", bt->bt878_mem);
514 iounmap(bt->bt878_mem);
516 release_mem_region(pci_resource_start(bt->dev, 0),
517 pci_resource_len(bt->dev, 0));
518 /* wake up any waiting processes
519 because shutdown flag is set, no new processes (in this queue)
525 pci_set_drvdata(pci_dev, NULL);
526 pci_disable_device(pci_dev);
530 static struct pci_device_id bt878_pci_tbl[] __devinitdata = {
531 {PCI_VENDOR_ID_BROOKTREE, PCI_DEVICE_ID_BROOKTREE_878,
532 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
536 MODULE_DEVICE_TABLE(pci, bt878_pci_tbl);
538 static struct pci_driver bt878_pci_driver = {
540 .id_table = bt878_pci_tbl,
541 .probe = bt878_probe,
542 .remove = bt878_remove,
545 static int bt878_pci_driver_registered = 0;
547 /*******************************/
548 /* Module management functions */
549 /*******************************/
551 static int bt878_init_module(void)
554 bt878_pci_driver_registered = 0;
556 printk(KERN_INFO "bt878: AUDIO driver version %d.%d.%d loaded\n",
557 (BT878_VERSION_CODE >> 16) & 0xff,
558 (BT878_VERSION_CODE >> 8) & 0xff,
559 BT878_VERSION_CODE & 0xff);
561 bt878_check_chipset();
563 /* later we register inside of bt878_find_audio_dma()
564 * because we may want to ignore certain cards */
565 bt878_pci_driver_registered = 1;
566 return pci_register_driver(&bt878_pci_driver);
569 static void bt878_cleanup_module(void)
571 if (bt878_pci_driver_registered) {
572 bt878_pci_driver_registered = 0;
573 pci_unregister_driver(&bt878_pci_driver);
578 module_init(bt878_init_module);
579 module_exit(bt878_cleanup_module);
581 //MODULE_AUTHOR("XXX");
582 MODULE_LICENSE("GPL");