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_ALIAS("xillybus_pcie");
18 MODULE_LICENSE("GPL v2");
20 #define PCI_DEVICE_ID_XILLYBUS 0xebeb
22 #define PCI_VENDOR_ID_ACTEL 0x11aa
23 #define PCI_VENDOR_ID_LATTICE 0x1204
25 static const char xillyname
[] = "xillybus_pcie";
27 static const struct pci_device_id xillyids
[] = {
28 {PCI_DEVICE(PCI_VENDOR_ID_XILINX
, PCI_DEVICE_ID_XILLYBUS
)},
29 {PCI_DEVICE(PCI_VENDOR_ID_ALTERA
, PCI_DEVICE_ID_XILLYBUS
)},
30 {PCI_DEVICE(PCI_VENDOR_ID_ACTEL
, PCI_DEVICE_ID_XILLYBUS
)},
31 {PCI_DEVICE(PCI_VENDOR_ID_LATTICE
, PCI_DEVICE_ID_XILLYBUS
)},
32 { /* End: all zeroes */ }
35 static int xilly_pci_direction(int direction
)
39 return PCI_DMA_TODEVICE
;
41 return PCI_DMA_FROMDEVICE
;
43 return PCI_DMA_BIDIRECTIONAL
;
47 static void xilly_dma_sync_single_for_cpu_pci(struct xilly_endpoint
*ep
,
48 dma_addr_t dma_handle
,
52 pci_dma_sync_single_for_cpu(ep
->pdev
,
55 xilly_pci_direction(direction
));
58 static void xilly_dma_sync_single_for_device_pci(struct xilly_endpoint
*ep
,
59 dma_addr_t dma_handle
,
63 pci_dma_sync_single_for_device(ep
->pdev
,
66 xilly_pci_direction(direction
));
69 static void xilly_pci_unmap(void *ptr
)
71 struct xilly_mapping
*data
= ptr
;
73 pci_unmap_single(data
->device
, data
->dma_addr
,
74 data
->size
, data
->direction
);
80 * Map either through the PCI DMA mapper or the non_PCI one. Behind the
81 * scenes exactly the same functions are called with the same parameters,
82 * but that can change.
85 static int xilly_map_single_pci(struct xilly_endpoint
*ep
,
89 dma_addr_t
*ret_dma_handle
94 struct xilly_mapping
*this;
96 this = kzalloc(sizeof(*this), GFP_KERNEL
);
100 pci_direction
= xilly_pci_direction(direction
);
102 addr
= pci_map_single(ep
->pdev
, ptr
, size
, pci_direction
);
104 if (pci_dma_mapping_error(ep
->pdev
, addr
)) {
109 this->device
= ep
->pdev
;
110 this->dma_addr
= addr
;
112 this->direction
= pci_direction
;
114 *ret_dma_handle
= addr
;
116 return devm_add_action_or_reset(ep
->dev
, xilly_pci_unmap
, this);
119 static struct xilly_endpoint_hardware pci_hw
= {
120 .owner
= THIS_MODULE
,
121 .hw_sync_sgl_for_cpu
= xilly_dma_sync_single_for_cpu_pci
,
122 .hw_sync_sgl_for_device
= xilly_dma_sync_single_for_device_pci
,
123 .map_single
= xilly_map_single_pci
,
126 static int xilly_probe(struct pci_dev
*pdev
,
127 const struct pci_device_id
*ent
)
129 struct xilly_endpoint
*endpoint
;
132 endpoint
= xillybus_init_endpoint(pdev
, &pdev
->dev
, &pci_hw
);
137 pci_set_drvdata(pdev
, endpoint
);
139 rc
= pcim_enable_device(pdev
);
141 dev_err(endpoint
->dev
,
142 "pcim_enable_device() failed. Aborting.\n");
146 /* L0s has caused packet drops. No power saving, thank you. */
148 pci_disable_link_state(pdev
, PCIE_LINK_STATE_L0S
);
150 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
)) {
151 dev_err(endpoint
->dev
,
152 "Incorrect BAR configuration. Aborting.\n");
156 rc
= pcim_iomap_regions(pdev
, 0x01, xillyname
);
158 dev_err(endpoint
->dev
,
159 "pcim_iomap_regions() failed. Aborting.\n");
163 endpoint
->registers
= pcim_iomap_table(pdev
)[0];
165 pci_set_master(pdev
);
167 /* Set up a single MSI interrupt */
168 if (pci_enable_msi(pdev
)) {
169 dev_err(endpoint
->dev
,
170 "Failed to enable MSI interrupts. Aborting.\n");
173 rc
= devm_request_irq(&pdev
->dev
, pdev
->irq
, xillybus_isr
, 0,
174 xillyname
, endpoint
);
176 dev_err(endpoint
->dev
,
177 "Failed to register MSI handler. Aborting.\n");
182 * Some (old and buggy?) hardware drops 64-bit addressed PCIe packets,
183 * even when the PCIe driver claims that a 64-bit mask is OK. On the
184 * other hand, on some architectures, 64-bit addressing is mandatory.
185 * So go for the 64-bit mask only when failing is the other option.
188 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
189 endpoint
->dma_using_dac
= 0;
190 } else if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
191 endpoint
->dma_using_dac
= 1;
193 dev_err(endpoint
->dev
, "Failed to set DMA mask. Aborting.\n");
197 return xillybus_endpoint_discovery(endpoint
);
200 static void xilly_remove(struct pci_dev
*pdev
)
202 struct xilly_endpoint
*endpoint
= pci_get_drvdata(pdev
);
204 xillybus_endpoint_remove(endpoint
);
207 MODULE_DEVICE_TABLE(pci
, xillyids
);
209 static struct pci_driver xillybus_driver
= {
211 .id_table
= xillyids
,
212 .probe
= xilly_probe
,
213 .remove
= xilly_remove
,
216 module_pci_driver(xillybus_driver
);