mantis_pci.c 8.0 KB

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