/* Mantis PCI bridge driver Copyright (C) 2005 Manu Abraham (manu@kromtek.com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include #include #include #include #include "mantis_common.h" #include "mantis_core.h" #include #include #include #include unsigned int verbose = 1; module_param(verbose, int, 0644); MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)"); #define PCI_VENDOR_ID_MANTIS 0x1822 #define PCI_DEVICE_ID_MANTIS_R11 0x4e35 #define DRIVER_NAME "Mantis" static struct pci_device_id mantis_pci_table[] = { { PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) }, { 0 }, }; MODULE_DEVICE_TABLE(pci, mantis_pci_table); static irqreturn_t mantis_pci_irq(int irq, void *dev_id, struct pt_regs *regs) { int count = 0, interrupts = 0; u32 stat = 0, mask = 0, temp; struct mantis_pci *mantis = dev_id; if (mantis == NULL) dprintk(verbose, MANTIS_DEBUG, 1, "Aeio, mantis ISR"); /* * Don't read each and everytime, but read in one shot * We need to save the states for later too .. * */ stat = mmread(MANTIS_INT_STAT); mask = mmread(MANTIS_INT_MASK); /* * To speed up things, do a basic check whether it is our * interrupt. * Also, we don't have a mask for I2CRACK */ if (!(stat & (mask | MANTIS_INT_I2CRACK)) ) { dprintk(verbose, MANTIS_ERROR, 1, "Not ours !"); return IRQ_NONE; } /* * The int is for us, clear int condition */ mmwrite(stat, MANTIS_INT_STAT); /* * Check how many 1's are in the INT_STAT, Count will reflect * no. of interrupts, we should loop count times. * */ temp = stat; while (temp) { if (temp & 0x01) { interrupts++; dprintk(verbose, MANTIS_DEBUG, 1, "Interrupt @ %d", interrupts); } temp >>= 1; count++; /* * We should never loop more than reg. width * else, Bail out */ if (count > 32) { dprintk(verbose, MANTIS_ERROR, 1, "Bailing out !!"); break; } } count = 0; while (count < interrupts) { if ((stat & mask) & MANTIS_INT_RISCEN) { dprintk(verbose, MANTIS_DEBUG, 1, "**** DMA enabl ****"); mantis->mantis_int_stat |= MANTIS_INT_RISCEN; stat &= ~MANTIS_INT_RISCEN; /* * It's a shame that there is no mask for I2CRACK ! */ } else if (stat & MANTIS_INT_I2CRACK) { dprintk(verbose, MANTIS_DEBUG, 1, "**** I2C R-ACK ****"); mantis->mantis_int_stat |= MANTIS_INT_I2CRACK; stat &= ~MANTIS_INT_I2CRACK; } else if ((stat & mask) & MANTIS_INT_IRQ0) { dprintk(verbose, MANTIS_DEBUG, 1, "**** INT IRQ-0 ****"); mantis->mantis_int_stat |= MANTIS_INT_IRQ0; stat &= ~MANTIS_INT_IRQ0; } else if ((stat & mask) & MANTIS_INT_IRQ1) { dprintk(verbose, MANTIS_DEBUG, 1, "**** INT IRQ-1 ****"); mantis->mantis_int_stat |= MANTIS_INT_IRQ1; stat &= ~MANTIS_INT_IRQ1; } else if ((stat & mask) & MANTIS_INT_OCERR) { dprintk(verbose, MANTIS_DEBUG, 1, "**** INT OCERR ****"); mantis->mantis_int_stat |= MANTIS_INT_OCERR; stat &= ~MANTIS_INT_OCERR; } else if ((stat & mask) & MANTIS_INT_PABORT) { dprintk(verbose, MANTIS_DEBUG, 1, "**** INT PABRT ****"); mantis->mantis_int_stat |= MANTIS_INT_PABORT; stat &= ~MANTIS_INT_PABORT; } else if ((stat & mask) & MANTIS_INT_RIPERR) { dprintk(verbose, MANTIS_DEBUG, 1, "**** INT RIPRR ****"); mantis->mantis_int_stat |= MANTIS_INT_RIPERR; stat &= ~MANTIS_INT_RIPERR; } else if ((stat & mask) & MANTIS_INT_PPERR) { dprintk(verbose, MANTIS_DEBUG, 1, "**** INT PPERR ****"); mantis->mantis_int_stat |= MANTIS_INT_PPERR; stat &= ~MANTIS_INT_PPERR; } else if ((stat & mask) & MANTIS_INT_FTRGT) { dprintk(verbose, MANTIS_DEBUG, 1, "**** INT FTRGT ****"); mantis->mantis_int_stat |= MANTIS_INT_FTRGT; stat &= ~MANTIS_INT_FTRGT; } else if ((stat & mask) & MANTIS_INT_RISCI) { mantis->finished_block = (mantis->mantis_int_stat & MANTIS_INT_RISCSTAT) >> 28; tasklet_schedule(&mantis->tasklet); stat &= ~MANTIS_INT_RISCI; } else if ((stat & mask) & MANTIS_INT_I2CDONE) { dprintk(verbose, MANTIS_DEBUG, 1, "**** I2C DONE ****"); mantis->mantis_int_stat |= MANTIS_INT_I2CDONE; stat &= ~MANTIS_INT_I2CDONE; } else { dprintk(verbose, MANTIS_DEBUG, 1, "Unknown INT ???"); } count++; /* * Being paranoid, check whether we are too loopy ! */ if (count > 32) { dprintk(verbose, MANTIS_ERROR, 1, "IRQ Lockup, clearing INT mask"); dprintk(verbose, MANTIS_ERROR, 1, "Count=%d, Interrupts=%d, stat=[0x%08x]", count, interrupts, stat); mmwrite(0, MANTIS_INT_MASK); stat = 0, interrupts = 0; break; } } return IRQ_HANDLED; } static int __devinit mantis_pci_probe(struct pci_dev *pdev, const struct pci_device_id *mantis_pci_table) { u8 revision, latency; struct mantis_pci *mantis; if (pci_enable_device(pdev)) { dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed"); goto exit; } /* * We need to change this to kzalloc on newer kernels * kzalloc() = kmalloc() + memset() * Code simplification for 2.6.14 and upwards */ mantis = kmalloc(sizeof (struct mantis_pci), GFP_KERNEL); // mantis = kzalloc(sizeof (struct mantis_pci), GFP_KERNEL); if (mantis == NULL) { dprintk(verbose, MANTIS_ERROR, 1, "Out of memory"); goto disable; } memset(mantis, 0, sizeof (struct mantis_pci)); mantis->mantis_addr = pci_resource_start(pdev, 0); if (!request_mem_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0), DRIVER_NAME)) { goto free; } if ((mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000)) == NULL) { dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed"); goto release; } /* * Clear and disable all interrupts at startup * to avoid lockup situations */ mmwrite(0x00, MANTIS_INT_STAT); mmwrite(0x00, MANTIS_INT_MASK); if (request_irq(pdev->irq, mantis_pci_irq, SA_SHIRQ | SA_INTERRUPT, DRIVER_NAME, mantis) < 0) { dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed"); goto unmap; } pci_set_master(pdev); pci_set_drvdata(pdev, mantis); pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency); pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision); mantis->latency = latency; mantis->revision = revision; /* * Setup default latency 32 if none specified */ if (!latency) pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32); printk("%s: Mantis Rev %d, ", __func__, mantis->revision); printk("irq: %d, latency: %d\nmemory: 0x%lx, mmio: 0x%p\n", pdev->irq, mantis->latency, mantis->mantis_addr, mantis->mantis_mmio); if ((mantis_dma_init(pdev)) < 0) { dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA init failed"); goto disable; } /* * No more PCI specific stuff ! */ if (mantis_core_init(mantis) < 0) { dprintk(verbose, MANTIS_ERROR, 1, "Mantis core init failed"); goto dma_exit; } return 0; /* * Error conditions .. */ dma_exit: if (mantis_dma_exit(pdev) < 0) dprintk(verbose, MANTIS_ERROR, 1, "Mantis DMA exit failed"); dprintk(verbose, MANTIS_DEBUG, 1, "Err: IO Unmap"); unmap: if (mantis->mantis_mmio) iounmap(mantis->mantis_mmio); release: dprintk(verbose, MANTIS_DEBUG, 1, "Err: Release regions"); release_mem_region(pci_resource_start(pdev, 0), pci_resource_len(pdev, 0)); free: kfree(mantis); disable: pci_disable_device(pdev); dprintk(verbose, MANTIS_DEBUG, 1, "Err: Disabling device"); exit: dprintk(verbose, MANTIS_DEBUG, 1, "Err:"); return -ENODEV; } static void __devexit mantis_pci_remove(struct pci_dev *pdev) { struct mantis_pci *mantis = pci_get_drvdata(pdev); if (mantis == NULL) { dprintk(verbose, MANTIS_ERROR, 1, "Aeio, Mantis NULL ptr"); return; } mantis_core_exit(mantis); printk("%s: Mantis irq: %d,latency: %d\n memory: 0x%lx, mmio: 0x%p", __func__, pdev->irq, mantis->latency, mantis->mantis_addr, mantis->mantis_mmio); free_irq(pdev->irq, mantis); pci_release_regions(pdev); if (mantis_dma_exit(pdev) < 0) dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed"); pci_set_drvdata(pdev, NULL); pci_disable_device(pdev); kfree(mantis); } static struct pci_driver mantis_pci_driver = { .name = DRIVER_NAME, .id_table = mantis_pci_table, .probe = mantis_pci_probe, .remove = __devexit_p(mantis_pci_remove), }; static int __init mantis_pci_init(void) { return pci_register_driver(&mantis_pci_driver); } static void __exit mantis_pci_exit(void) { pci_unregister_driver(&mantis_pci_driver); } module_init(mantis_pci_init); module_exit(mantis_pci_exit); MODULE_DESCRIPTION("Mantis PCI DTV bridge driver"); MODULE_AUTHOR("Manu Abraham"); MODULE_LICENSE("GPL");