1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/misc/xillybus_pcie.c
5 * Copyright 2011 Xillybus Ltd, http://xillybus.com
7 * Driver for the Xillybus FPGA/host framework using PCI Express.
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 #include <linux/slab.h>
15 MODULE_DESCRIPTION("Xillybus driver for PCIe");
16 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
17 MODULE_VERSION("1.06");
18 MODULE_ALIAS("xillybus_pcie");
19 MODULE_LICENSE("GPL v2");
21 #define PCI_DEVICE_ID_XILLYBUS 0xebeb
23 #define PCI_VENDOR_ID_ACTEL 0x11aa
24 #define PCI_VENDOR_ID_LATTICE 0x1204
26 static const char xillyname[] = "xillybus_pcie";
28 static const struct pci_device_id xillyids[] = {
29 {PCI_DEVICE(PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_XILLYBUS)},
30 {PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_XILLYBUS)},
31 {PCI_DEVICE(PCI_VENDOR_ID_ACTEL, PCI_DEVICE_ID_XILLYBUS)},
32 {PCI_DEVICE(PCI_VENDOR_ID_LATTICE, PCI_DEVICE_ID_XILLYBUS)},
33 { /* End: all zeroes */ }
36 static int xilly_pci_direction(int direction)
40 return PCI_DMA_TODEVICE;
42 return PCI_DMA_FROMDEVICE;
44 return PCI_DMA_BIDIRECTIONAL;
48 static void xilly_dma_sync_single_for_cpu_pci(struct xilly_endpoint *ep,
49 dma_addr_t dma_handle,
53 pci_dma_sync_single_for_cpu(ep->pdev,
56 xilly_pci_direction(direction));
59 static void xilly_dma_sync_single_for_device_pci(struct xilly_endpoint *ep,
60 dma_addr_t dma_handle,
64 pci_dma_sync_single_for_device(ep->pdev,
67 xilly_pci_direction(direction));
70 static void xilly_pci_unmap(void *ptr)
72 struct xilly_mapping *data = ptr;
74 pci_unmap_single(data->device, data->dma_addr,
75 data->size, data->direction);
81 * Map either through the PCI DMA mapper or the non_PCI one. Behind the
82 * scenes exactly the same functions are called with the same parameters,
83 * but that can change.
86 static int xilly_map_single_pci(struct xilly_endpoint *ep,
90 dma_addr_t *ret_dma_handle
95 struct xilly_mapping *this;
97 this = kzalloc(sizeof(*this), GFP_KERNEL);
101 pci_direction = xilly_pci_direction(direction);
103 addr = pci_map_single(ep->pdev, ptr, size, pci_direction);
105 if (pci_dma_mapping_error(ep->pdev, addr)) {
110 this->device = ep->pdev;
111 this->dma_addr = addr;
113 this->direction = pci_direction;
115 *ret_dma_handle = addr;
117 return devm_add_action_or_reset(ep->dev, xilly_pci_unmap, this);
120 static struct xilly_endpoint_hardware pci_hw = {
121 .owner = THIS_MODULE,
122 .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_pci,
123 .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_pci,
124 .map_single = xilly_map_single_pci,
127 static int xilly_probe(struct pci_dev *pdev,
128 const struct pci_device_id *ent)
130 struct xilly_endpoint *endpoint;
133 endpoint = xillybus_init_endpoint(pdev, &pdev->dev, &pci_hw);
138 pci_set_drvdata(pdev, endpoint);
140 rc = pcim_enable_device(pdev);
142 dev_err(endpoint->dev,
143 "pcim_enable_device() failed. Aborting.\n");
147 /* L0s has caused packet drops. No power saving, thank you. */
149 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
151 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
152 dev_err(endpoint->dev,
153 "Incorrect BAR configuration. Aborting.\n");
157 rc = pcim_iomap_regions(pdev, 0x01, xillyname);
159 dev_err(endpoint->dev,
160 "pcim_iomap_regions() failed. Aborting.\n");
164 endpoint->registers = pcim_iomap_table(pdev)[0];
166 pci_set_master(pdev);
168 /* Set up a single MSI interrupt */
169 if (pci_enable_msi(pdev)) {
170 dev_err(endpoint->dev,
171 "Failed to enable MSI interrupts. Aborting.\n");
174 rc = devm_request_irq(&pdev->dev, pdev->irq, xillybus_isr, 0,
175 xillyname, endpoint);
177 dev_err(endpoint->dev,
178 "Failed to register MSI handler. Aborting.\n");
183 * Some (old and buggy?) hardware drops 64-bit addressed PCIe packets,
184 * even when the PCIe driver claims that a 64-bit mask is OK. On the
185 * other hand, on some architectures, 64-bit addressing is mandatory.
186 * So go for the 64-bit mask only when failing is the other option.
189 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
190 endpoint->dma_using_dac = 0;
191 } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
192 endpoint->dma_using_dac = 1;
194 dev_err(endpoint->dev, "Failed to set DMA mask. Aborting.\n");
198 return xillybus_endpoint_discovery(endpoint);
201 static void xilly_remove(struct pci_dev *pdev)
203 struct xilly_endpoint *endpoint = pci_get_drvdata(pdev);
205 xillybus_endpoint_remove(endpoint);
208 MODULE_DEVICE_TABLE(pci, xillyids);
210 static struct pci_driver xillybus_driver = {
212 .id_table = xillyids,
213 .probe = xilly_probe,
214 .remove = xilly_remove,
217 module_pci_driver(xillybus_driver);