xillybus_pcie.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * linux/drivers/misc/xillybus_pcie.c
  3. *
  4. * Copyright 2011 Xillybus Ltd, http://xillybus.com
  5. *
  6. * Driver for the Xillybus FPGA/host framework using PCI Express.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the smems of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/pci-aspm.h>
  15. #include <linux/slab.h>
  16. #include "xillybus.h"
  17. MODULE_DESCRIPTION("Xillybus driver for PCIe");
  18. MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
  19. MODULE_VERSION("1.06");
  20. MODULE_ALIAS("xillybus_pcie");
  21. MODULE_LICENSE("GPL v2");
  22. #define PCI_DEVICE_ID_XILLYBUS 0xebeb
  23. #define PCI_VENDOR_ID_ACTEL 0x11aa
  24. #define PCI_VENDOR_ID_LATTICE 0x1204
  25. static const char xillyname[] = "xillybus_pcie";
  26. static const struct pci_device_id xillyids[] = {
  27. {PCI_DEVICE(PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_XILLYBUS)},
  28. {PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_XILLYBUS)},
  29. {PCI_DEVICE(PCI_VENDOR_ID_ACTEL, PCI_DEVICE_ID_XILLYBUS)},
  30. {PCI_DEVICE(PCI_VENDOR_ID_LATTICE, PCI_DEVICE_ID_XILLYBUS)},
  31. { /* End: all zeroes */ }
  32. };
  33. static int xilly_pci_direction(int direction)
  34. {
  35. switch (direction) {
  36. case DMA_TO_DEVICE:
  37. return PCI_DMA_TODEVICE;
  38. case DMA_FROM_DEVICE:
  39. return PCI_DMA_FROMDEVICE;
  40. default:
  41. return PCI_DMA_BIDIRECTIONAL;
  42. }
  43. }
  44. static void xilly_dma_sync_single_for_cpu_pci(struct xilly_endpoint *ep,
  45. dma_addr_t dma_handle,
  46. size_t size,
  47. int direction)
  48. {
  49. pci_dma_sync_single_for_cpu(ep->pdev,
  50. dma_handle,
  51. size,
  52. xilly_pci_direction(direction));
  53. }
  54. static void xilly_dma_sync_single_for_device_pci(struct xilly_endpoint *ep,
  55. dma_addr_t dma_handle,
  56. size_t size,
  57. int direction)
  58. {
  59. pci_dma_sync_single_for_device(ep->pdev,
  60. dma_handle,
  61. size,
  62. xilly_pci_direction(direction));
  63. }
  64. static void xilly_pci_unmap(void *ptr)
  65. {
  66. struct xilly_mapping *data = ptr;
  67. pci_unmap_single(data->device, data->dma_addr,
  68. data->size, data->direction);
  69. kfree(ptr);
  70. }
  71. /*
  72. * Map either through the PCI DMA mapper or the non_PCI one. Behind the
  73. * scenes exactly the same functions are called with the same parameters,
  74. * but that can change.
  75. */
  76. static int xilly_map_single_pci(struct xilly_endpoint *ep,
  77. void *ptr,
  78. size_t size,
  79. int direction,
  80. dma_addr_t *ret_dma_handle
  81. )
  82. {
  83. int pci_direction;
  84. dma_addr_t addr;
  85. struct xilly_mapping *this;
  86. this = kzalloc(sizeof(*this), GFP_KERNEL);
  87. if (!this)
  88. return -ENOMEM;
  89. pci_direction = xilly_pci_direction(direction);
  90. addr = pci_map_single(ep->pdev, ptr, size, pci_direction);
  91. if (pci_dma_mapping_error(ep->pdev, addr)) {
  92. kfree(this);
  93. return -ENODEV;
  94. }
  95. this->device = ep->pdev;
  96. this->dma_addr = addr;
  97. this->size = size;
  98. this->direction = pci_direction;
  99. *ret_dma_handle = addr;
  100. return devm_add_action_or_reset(ep->dev, xilly_pci_unmap, this);
  101. }
  102. static struct xilly_endpoint_hardware pci_hw = {
  103. .owner = THIS_MODULE,
  104. .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_pci,
  105. .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_pci,
  106. .map_single = xilly_map_single_pci,
  107. };
  108. static int xilly_probe(struct pci_dev *pdev,
  109. const struct pci_device_id *ent)
  110. {
  111. struct xilly_endpoint *endpoint;
  112. int rc;
  113. endpoint = xillybus_init_endpoint(pdev, &pdev->dev, &pci_hw);
  114. if (!endpoint)
  115. return -ENOMEM;
  116. pci_set_drvdata(pdev, endpoint);
  117. rc = pcim_enable_device(pdev);
  118. if (rc) {
  119. dev_err(endpoint->dev,
  120. "pcim_enable_device() failed. Aborting.\n");
  121. return rc;
  122. }
  123. /* L0s has caused packet drops. No power saving, thank you. */
  124. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
  125. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  126. dev_err(endpoint->dev,
  127. "Incorrect BAR configuration. Aborting.\n");
  128. return -ENODEV;
  129. }
  130. rc = pcim_iomap_regions(pdev, 0x01, xillyname);
  131. if (rc) {
  132. dev_err(endpoint->dev,
  133. "pcim_iomap_regions() failed. Aborting.\n");
  134. return rc;
  135. }
  136. endpoint->registers = pcim_iomap_table(pdev)[0];
  137. pci_set_master(pdev);
  138. /* Set up a single MSI interrupt */
  139. if (pci_enable_msi(pdev)) {
  140. dev_err(endpoint->dev,
  141. "Failed to enable MSI interrupts. Aborting.\n");
  142. return -ENODEV;
  143. }
  144. rc = devm_request_irq(&pdev->dev, pdev->irq, xillybus_isr, 0,
  145. xillyname, endpoint);
  146. if (rc) {
  147. dev_err(endpoint->dev,
  148. "Failed to register MSI handler. Aborting.\n");
  149. return -ENODEV;
  150. }
  151. /*
  152. * Some (old and buggy?) hardware drops 64-bit addressed PCIe packets,
  153. * even when the PCIe driver claims that a 64-bit mask is OK. On the
  154. * other hand, on some architectures, 64-bit addressing is mandatory.
  155. * So go for the 64-bit mask only when failing is the other option.
  156. */
  157. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  158. endpoint->dma_using_dac = 0;
  159. } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  160. endpoint->dma_using_dac = 1;
  161. } else {
  162. dev_err(endpoint->dev, "Failed to set DMA mask. Aborting.\n");
  163. return -ENODEV;
  164. }
  165. return xillybus_endpoint_discovery(endpoint);
  166. }
  167. static void xilly_remove(struct pci_dev *pdev)
  168. {
  169. struct xilly_endpoint *endpoint = pci_get_drvdata(pdev);
  170. xillybus_endpoint_remove(endpoint);
  171. }
  172. MODULE_DEVICE_TABLE(pci, xillyids);
  173. static struct pci_driver xillybus_driver = {
  174. .name = xillyname,
  175. .id_table = xillyids,
  176. .probe = xilly_probe,
  177. .remove = xilly_remove,
  178. };
  179. module_pci_driver(xillybus_driver);