mantis_pci.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. u32 rst_stat = 0, rst_mask = 0;
  45. struct mantis_pci *mantis;
  46. struct mantis_ca *ca;
  47. mantis = (struct mantis_pci *) dev_id;
  48. if (unlikely(mantis == NULL)) {
  49. dprintk(verbose, MANTIS_ERROR, 1, "Mantis == NULL");
  50. return IRQ_NONE;
  51. }
  52. ca = mantis->mantis_ca;
  53. stat = mmread(MANTIS_INT_STAT);
  54. mask = mmread(MANTIS_INT_MASK);
  55. mstat = lstat = stat & ~MANTIS_INT_RISCSTAT;
  56. if (!(stat & mask))
  57. return IRQ_NONE;
  58. rst_mask = MANTIS_GPIF_WRACK |
  59. MANTIS_GPIF_OTHERR |
  60. MANTIS_SBUF_WSTO |
  61. MANTIS_GPIF_EXTIRQ;
  62. rst_stat = mmread(MANTIS_GPIF_STATUS);
  63. rst_stat &= rst_mask;
  64. mmwrite(rst_stat, MANTIS_GPIF_STATUS);
  65. mantis->mantis_int_stat = stat;
  66. mantis->mantis_int_mask = mask;
  67. dprintk(verbose, MANTIS_DEBUG, 0, "=== Interrupts[%04x/%04x]= [", stat, mask);
  68. if (stat & MANTIS_INT_RISCEN) {
  69. dprintk(verbose, MANTIS_DEBUG, 0, "* DMA enabl *");
  70. }
  71. if (stat & MANTIS_INT_IRQ0) {
  72. dprintk(verbose, MANTIS_DEBUG, 0, "* INT IRQ-0 *");
  73. mantis->gpif_status = rst_stat;
  74. wake_up(&ca->hif_write_wq);
  75. schedule_work(&ca->hif_evm_work);
  76. }
  77. if (stat & MANTIS_INT_IRQ1) {
  78. dprintk(verbose, MANTIS_DEBUG, 0, "* INT IRQ-1 *");
  79. }
  80. if (stat & MANTIS_INT_OCERR) {
  81. dprintk(verbose, MANTIS_DEBUG, 0, "* INT OCERR *");
  82. }
  83. if (stat & MANTIS_INT_PABORT) {
  84. dprintk(verbose, MANTIS_DEBUG, 0, "* INT PABRT *");
  85. }
  86. if (stat & MANTIS_INT_RIPERR) {
  87. dprintk(verbose, MANTIS_DEBUG, 0, "* INT RIPRR *");
  88. }
  89. if (stat & MANTIS_INT_PPERR) {
  90. dprintk(verbose, MANTIS_DEBUG, 0, "* INT PPERR *");
  91. }
  92. if (stat & MANTIS_INT_FTRGT) {
  93. dprintk(verbose, MANTIS_DEBUG, 0, "* INT FTRGT *");
  94. }
  95. if (stat & MANTIS_INT_RISCI) {
  96. dprintk(verbose, MANTIS_DEBUG, 0, "* INT RISCI *");
  97. mantis->finished_block = (stat & MANTIS_INT_RISCSTAT) >> 28;
  98. tasklet_schedule(&mantis->tasklet);
  99. }
  100. if (stat & MANTIS_INT_I2CDONE) {
  101. dprintk(verbose, MANTIS_DEBUG, 0, "* I2C DONE *");
  102. wake_up(&mantis->i2c_wq);
  103. }
  104. mmwrite(stat, MANTIS_INT_STAT);
  105. stat &= ~(MANTIS_INT_RISCEN | MANTIS_INT_I2CDONE |
  106. MANTIS_INT_I2CRACK | MANTIS_INT_PCMCIA7 |
  107. MANTIS_INT_PCMCIA6 | MANTIS_INT_PCMCIA5 |
  108. MANTIS_INT_PCMCIA4 | MANTIS_INT_PCMCIA3 |
  109. MANTIS_INT_PCMCIA2 | MANTIS_INT_PCMCIA1 |
  110. MANTIS_INT_PCMCIA0 | MANTIS_INT_IRQ1 |
  111. MANTIS_INT_IRQ0 | MANTIS_INT_OCERR |
  112. MANTIS_INT_PABORT | MANTIS_INT_RIPERR |
  113. MANTIS_INT_PPERR | MANTIS_INT_FTRGT |
  114. MANTIS_INT_RISCI);
  115. if (stat)
  116. dprintk(verbose, MANTIS_DEBUG, 0, "* Unknown [%04x] *", stat);
  117. dprintk(verbose, MANTIS_DEBUG, 0, "] ===\n");
  118. return IRQ_HANDLED;
  119. }
  120. static int __devinit mantis_pci_probe(struct pci_dev *pdev,
  121. const struct pci_device_id *mantis_pci_table)
  122. {
  123. u8 revision, latency;
  124. struct mantis_pci *mantis;
  125. int ret = 0;
  126. mantis = kmalloc(sizeof (struct mantis_pci), GFP_KERNEL);
  127. if (mantis == NULL) {
  128. printk("%s: Out of memory\n", __func__);
  129. ret = -ENOMEM;
  130. goto err;
  131. }
  132. memset(mantis, 0, sizeof (struct mantis_pci));
  133. mantis->num = devs;
  134. devs++;
  135. if (pci_enable_device(pdev)) {
  136. dprintk(verbose, MANTIS_ERROR, 1, "Mantis PCI enable failed");
  137. ret = -ENODEV;
  138. goto err;
  139. }
  140. mantis->mantis_addr = pci_resource_start(pdev, 0);
  141. if (!request_mem_region(pci_resource_start(pdev, 0),
  142. pci_resource_len(pdev, 0), DRIVER_NAME)) {
  143. ret = -ENODEV;
  144. goto err0;
  145. }
  146. if ((mantis->mantis_mmio = ioremap(mantis->mantis_addr, 0x1000)) == NULL) {
  147. dprintk(verbose, MANTIS_ERROR, 1, "IO remap failed");
  148. ret = -ENODEV;
  149. goto err1;
  150. }
  151. // Clear and disable all interrupts at startup
  152. // to avoid lockup situations
  153. mmwrite(0x00, MANTIS_INT_MASK);
  154. if (request_irq(pdev->irq, mantis_pci_irq, IRQF_SHARED | IRQF_DISABLED,
  155. DRIVER_NAME, mantis) < 0) {
  156. dprintk(verbose, MANTIS_ERROR, 1, "Mantis IRQ reg failed");
  157. ret = -ENODEV;
  158. goto err2;
  159. }
  160. pci_set_master(pdev);
  161. pci_set_drvdata(pdev, mantis);
  162. pci_read_config_byte(pdev, PCI_LATENCY_TIMER, &latency);
  163. pci_read_config_byte(pdev, PCI_CLASS_REVISION, &revision);
  164. mantis->latency = latency;
  165. mantis->revision = revision;
  166. mantis->pdev = pdev;
  167. mantis->subsystem_vendor = pdev->subsystem_vendor;
  168. mantis->subsystem_device = pdev->subsystem_device;
  169. init_waitqueue_head(&mantis->i2c_wq);
  170. mantis_set_direction(mantis, 0); /* CAM bypass */
  171. if (!latency)
  172. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 32);
  173. dprintk(verbose, MANTIS_ERROR, 0,
  174. "irq: %d, latency: %d\n memory: 0x%lx, mmio: 0x%p\n",
  175. pdev->irq, mantis->latency,
  176. mantis->mantis_addr, mantis->mantis_mmio);
  177. // No more PCI specific stuff !
  178. if (mantis_core_init(mantis) < 0) {
  179. dprintk(verbose, MANTIS_ERROR, 1, "Mantis core init failed");
  180. ret = -ENODEV;
  181. goto err2;
  182. }
  183. return 0;
  184. // Error conditions ..
  185. err2:
  186. dprintk(verbose, MANTIS_DEBUG, 1, "Err: IO Unmap");
  187. if (mantis->mantis_mmio)
  188. iounmap(mantis->mantis_mmio);
  189. err1:
  190. dprintk(verbose, MANTIS_DEBUG, 1, "Err: Release regions");
  191. release_mem_region(pci_resource_start(pdev, 0),
  192. pci_resource_len(pdev, 0));
  193. pci_disable_device(pdev);
  194. err0:
  195. dprintk(verbose, MANTIS_DEBUG, 1, "Err: Free");
  196. kfree(mantis);
  197. err:
  198. dprintk(verbose, MANTIS_DEBUG, 1, "Err:");
  199. return ret;
  200. }
  201. static void __devexit mantis_pci_remove(struct pci_dev *pdev)
  202. {
  203. struct mantis_pci *mantis = pci_get_drvdata(pdev);
  204. if (mantis == NULL) {
  205. dprintk(verbose, MANTIS_ERROR, 1, "Aeio, Mantis NULL ptr");
  206. return;
  207. }
  208. mantis_core_exit(mantis);
  209. dprintk(verbose, MANTIS_ERROR, 1, "Removing -->Mantis irq: %d, latency: %d\n memory: 0x%lx, mmio: 0x%p",
  210. pdev->irq, mantis->latency, mantis->mantis_addr,
  211. mantis->mantis_mmio);
  212. free_irq(pdev->irq, mantis);
  213. pci_release_regions(pdev);
  214. if (mantis_dma_exit(mantis) < 0)
  215. dprintk(verbose, MANTIS_ERROR, 1, "DMA exit failed");
  216. pci_set_drvdata(pdev, NULL);
  217. pci_disable_device(pdev);
  218. kfree(mantis);
  219. }
  220. static struct pci_driver mantis_pci_driver = {
  221. .name = DRIVER_NAME,
  222. .id_table = mantis_pci_table,
  223. .probe = mantis_pci_probe,
  224. .remove = mantis_pci_remove,
  225. };
  226. static int __devinit mantis_pci_init(void)
  227. {
  228. return pci_register_driver(&mantis_pci_driver);
  229. }
  230. static void __devexit mantis_pci_exit(void)
  231. {
  232. pci_unregister_driver(&mantis_pci_driver);
  233. }
  234. module_init(mantis_pci_init);
  235. module_exit(mantis_pci_exit);
  236. MODULE_DESCRIPTION("Mantis PCI DTV bridge driver");
  237. MODULE_AUTHOR("Manu Abraham");
  238. MODULE_LICENSE("GPL");