mantis_pci.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. Mantis PCI bridge driver
  3. Copyright (C) 2005, 2006 Manu Abraham (abraham.manu@gmail.com)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <asm/io.h>
  17. #include <asm/pgtable.h>
  18. #include <asm/page.h>
  19. #include <linux/kmod.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/init.h>
  22. #include <linux/device.h>
  23. #include "mantis_common.h"
  24. #include "mantis_core.h"
  25. #include <asm/irq.h>
  26. #include <linux/signal.h>
  27. #include <linux/sched.h>
  28. #include <linux/interrupt.h>
  29. unsigned int verbose = 1;
  30. module_param(verbose, int, 0644);
  31. MODULE_PARM_DESC(verbose, "verbose startup messages, default is 1 (yes)");
  32. unsigned int devs;
  33. #define PCI_VENDOR_ID_MANTIS 0x1822
  34. #define PCI_DEVICE_ID_MANTIS_R11 0x4e35
  35. #define DRIVER_NAME "Mantis"
  36. static struct pci_device_id mantis_pci_table[] = {
  37. { PCI_DEVICE(PCI_VENDOR_ID_MANTIS, PCI_DEVICE_ID_MANTIS_R11) },
  38. { 0 },
  39. };
  40. MODULE_DEVICE_TABLE(pci, mantis_pci_table);
  41. static irqreturn_t mantis_pci_irq(int irq, void *dev_id)
  42. {
  43. u32 stat = 0, mask = 0, lstat = 0, mstat = 0;
  44. struct mantis_pci *mantis;
  45. struct mantis_ca *ca;
  46. mantis = (struct mantis_pci *) dev_id;
  47. if (unlikely(mantis == NULL)) {
  48. dprintk(verbose, MANTIS_ERROR, 1, "Mantis == NULL");
  49. return IRQ_NONE;
  50. }
  51. ca = mantis->mantis_ca;
  52. stat = mmread(MANTIS_INT_STAT);
  53. mask = mmread(MANTIS_INT_MASK);
  54. mstat = lstat = stat & ~MANTIS_INT_RISCSTAT;
  55. if (!(stat & mask))
  56. return IRQ_NONE;
  57. mantis->mantis_int_stat = stat;
  58. mantis->mantis_int_mask = mask;
  59. dprintk(verbose, MANTIS_DEBUG, 0, "=== Interrupts[%04x/%04x]= [", stat, mask);
  60. if (stat & MANTIS_INT_RISCEN) {
  61. dprintk(verbose, MANTIS_DEBUG, 0, "* DMA enabl *");
  62. }
  63. if (stat & MANTIS_INT_IRQ0) {
  64. dprintk(verbose, MANTIS_DEBUG, 0, "* INT IRQ-0 *");
  65. schedule_work(&ca->hif_evm_work);
  66. }
  67. if (stat & MANTIS_INT_IRQ1) {
  68. dprintk(verbose, MANTIS_DEBUG, 0, "* INT IRQ-1 *");
  69. }
  70. if (stat & MANTIS_INT_OCERR) {
  71. dprintk(verbose, MANTIS_DEBUG, 0, "* INT OCERR *");
  72. }
  73. if (stat & MANTIS_INT_PABORT) {
  74. dprintk(verbose, MANTIS_DEBUG, 0, "* INT PABRT *");
  75. }
  76. if (stat & MANTIS_INT_RIPERR) {
  77. dprintk(verbose, MANTIS_DEBUG, 0, "* INT RIPRR *");
  78. }
  79. if (stat & MANTIS_INT_PPERR) {
  80. dprintk(verbose, MANTIS_DEBUG, 0, "* INT PPERR *");
  81. }
  82. if (stat & MANTIS_INT_FTRGT) {
  83. dprintk(verbose, MANTIS_DEBUG, 0, "* INT FTRGT *");
  84. }
  85. if (stat & MANTIS_INT_RISCI) {
  86. dprintk(verbose, MANTIS_DEBUG, 0, "* INT RISCI *");
  87. mantis->finished_block = (stat & MANTIS_INT_RISCSTAT) >> 28;
  88. tasklet_schedule(&mantis->tasklet);
  89. }
  90. if (stat & MANTIS_INT_I2CDONE) {
  91. dprintk(verbose, MANTIS_DEBUG, 0, "* I2C DONE *");
  92. wake_up(&mantis->i2c_wq);
  93. }
  94. mmwrite(stat, MANTIS_INT_STAT);
  95. stat &= ~(MANTIS_INT_RISCEN | MANTIS_INT_I2CDONE |
  96. MANTIS_INT_I2CRACK | MANTIS_INT_PCMCIA7 |
  97. MANTIS_INT_PCMCIA6 | MANTIS_INT_PCMCIA5 |
  98. MANTIS_INT_PCMCIA4 | MANTIS_INT_PCMCIA3 |
  99. MANTIS_INT_PCMCIA2 | MANTIS_INT_PCMCIA1 |
  100. MANTIS_INT_PCMCIA0 | MANTIS_INT_IRQ1 |
  101. MANTIS_INT_IRQ0 | MANTIS_INT_OCERR |
  102. MANTIS_INT_PABORT | MANTIS_INT_RIPERR |
  103. MANTIS_INT_PPERR | MANTIS_INT_FTRGT |
  104. MANTIS_INT_RISCI);
  105. if (stat)
  106. dprintk(verbose, MANTIS_DEBUG, 0, "* Unknown [%04x] *", stat);
  107. dprintk(verbose, MANTIS_DEBUG, 0, "] ===\n");
  108. return IRQ_HANDLED;
  109. }
  110. static int __devinit mantis_pci_probe(struct pci_dev *pdev,
  111. const struct pci_device_id *mantis_pci_table)
  112. {
  113. u8 revision, latency;
  114. struct mantis_pci *mantis;
  115. int ret = 0;
  116. mantis = kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
  117. if (mantis == NULL) {
  118. printk("%s: Out of memory\n", __func__);
  119. ret = -ENOMEM;
  120. goto err;
  121. }
  122. memset(mantis, 0, sizeof (struct mantis_pci));
  123. mantis->num = devs;
  124. devs++;
  125. if (pci_enable_device(pdev)) {
  126. dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
  127. ret = -ENODEV;
  128. goto err;
  129. }
  130. mantis->mantis_addr = pci_resource_start(pdev, 0);
  131. if (!request_mem_region(pci_resource_start(pdev, 0),
  132. pci_resource_len(pdev, 0), DRIVER_NAME)) {
  133. ret = -ENODEV;
  134. goto err0;
  135. }
  136. if ((mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
  137. dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
  138. ret = -ENODEV;
  139. goto err1;
  140. }
  141. // Clear and disable all interrupts at startup
  142. // to avoid lockup situations
  143. mmwrite(0x00, MANTIS_INT_MASK);
  144. if (request_irq(pdev->irq, mantis_pci_irq, IRQF_SHARED | IRQF_DISABLED,
  145. DRIVER_NAME, mantis) < 0) {
  146. dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
  147. ret = -ENODEV;
  148. goto err2;
  149. }
  150. pci_set_master(pdev);
  151. pci_set_drvdata(pdev, mantis);
  152. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
  153. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
  154. mantis->latency = latency;
  155. mantis->revision = revision;
  156. mantis->pdev = pdev;
  157. mantis->subsystem_vendor = pdev->subsystem_vendor;
  158. mantis->subsystem_device = pdev->subsystem_device;
  159. init_waitqueue_head(&mantis->i2c_wq);
  160. mantis_set_direction(mantis, 0); /* CAM bypass */
  161. if (!latency)
  162. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
  163. dprintk(verbose, MANTIS_ERROR, 0,
  164. "irq: %d, latency: %d\n memory: 0x%lx, mmio: 0x%p\n",
  165. pdev->irq, mantis->latency,
  166. mantis->mantis_addr, mantis->mantis_mmio);
  167. // No more PCI specific stuff !
  168. if (mantis_core_init(mantis) < 0) {
  169. dprintk(verbose, MANTIS_ERROR, 1, "Mantis core init failed");
  170. ret = -ENODEV;
  171. goto err2;
  172. }
  173. return 0;
  174. // Error conditions ..
  175. err2:
  176. dprintk(verbose, MANTIS_DEBUG, 1, "Err: IO Unmap");
  177. if (mantis->mantis_mmio)
  178. iounmap(mantis->mantis_mmio);
  179. err1:
  180. dprintk(verbose, MANTIS_DEBUG, 1, "Err: Release regions");
  181. release_mem_region(pci_resource_start(pdev, 0),
  182. pci_resource_len(pdev, 0));
  183. pci_disable_device(pdev);
  184. err0:
  185. dprintk(verbose, MANTIS_DEBUG, 1, "Err: Free");
  186. kfree(mantis);
  187. err:
  188. dprintk(verbose, MANTIS_DEBUG, 1, "Err:");
  189. return ret;
  190. }
  191. static void __devexit mantis_pci_remove(struct pci_dev *pdev)
  192. {
  193. struct mantis_pci *mantis = pci_get_drvdata(pdev);
  194. if (mantis == NULL) {
  195. dprintk(verbose, MANTIS_ERROR, 1, "Aeio, Mantis NULL ptr");
  196. return;
  197. }
  198. mantis_core_exit(mantis);
  199. dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, latency: %d\n memory: 0x%lx, mmio: 0x%p",
  200. pdev->irq, mantis->latency, mantis->mantis_addr,
  201. mantis->mantis_mmio);
  202. free_irq(pdev->irq, mantis);
  203. pci_release_regions(pdev);
  204. if (mantis_dma_exit(mantis) < 0)
  205. dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed");
  206. pci_set_drvdata(pdev, NULL);
  207. pci_disable_device(pdev);
  208. kfree(mantis);
  209. }
  210. static struct pci_driver mantis_pci_driver = {
  211. .name = DRIVER_NAME,
  212. .id_table = mantis_pci_table,
  213. .probe = mantis_pci_probe,
  214. .remove = mantis_pci_remove,
  215. };
  216. static int __devinit mantis_pci_init(void)
  217. {
  218. return pci_register_driver(&mantis_pci_driver);
  219. }
  220. static void __devexit mantis_pci_exit(void)
  221. {
  222. pci_unregister_driver(&mantis_pci_driver);
  223. }
  224. module_init(mantis_pci_init);
  225. module_exit(mantis_pci_exit);
  226. MODULE_DESCRIPTION("Mantis PCI DTV bridge driver");
  227. MODULE_AUTHOR("Manu Abraham");
  228. MODULE_LICENSE("GPL");