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/pci-aspm.h>
13 #include <linux/slab.h>
16 MODULE_DESCRIPTION("Xillybus driver for PCIe");
17 MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
18 MODULE_VERSION("1.06");
19 MODULE_ALIAS("xillybus_pcie");
20 MODULE_LICENSE("GPL v2");
22 #define PCI_DEVICE_ID_XILLYBUS 0xebeb
24 #define PCI_VENDOR_ID_ACTEL 0x11aa
25 #define PCI_VENDOR_ID_LATTICE 0x1204
27 static const char xillyname
[] = "xillybus_pcie";
29 static const struct pci_device_id xillyids
[] = {
30 {PCI_DEVICE(PCI_VENDOR_ID_XILINX
, PCI_DEVICE_ID_XILLYBUS
)},
31 {PCI_DEVICE(PCI_VENDOR_ID_ALTERA
, PCI_DEVICE_ID_XILLYBUS
)},
32 {PCI_DEVICE(PCI_VENDOR_ID_ACTEL
, PCI_DEVICE_ID_XILLYBUS
)},
33 {PCI_DEVICE(PCI_VENDOR_ID_LATTICE
, PCI_DEVICE_ID_XILLYBUS
)},
34 { /* End: all zeroes */ }
37 static int xilly_pci_direction(int direction
)
41 return PCI_DMA_TODEVICE
;
43 return PCI_DMA_FROMDEVICE
;
45 return PCI_DMA_BIDIRECTIONAL
;
49 static void xilly_dma_sync_single_for_cpu_pci(struct xilly_endpoint
*ep
,
50 dma_addr_t dma_handle
,
54 pci_dma_sync_single_for_cpu(ep
->pdev
,
57 xilly_pci_direction(direction
));
60 static void xilly_dma_sync_single_for_device_pci(struct xilly_endpoint
*ep
,
61 dma_addr_t dma_handle
,
65 pci_dma_sync_single_for_device(ep
->pdev
,
68 xilly_pci_direction(direction
));
71 static void xilly_pci_unmap(void *ptr
)
73 struct xilly_mapping
*data
= ptr
;
75 pci_unmap_single(data
->device
, data
->dma_addr
,
76 data
->size
, data
->direction
);
82 * Map either through the PCI DMA mapper or the non_PCI one. Behind the
83 * scenes exactly the same functions are called with the same parameters,
84 * but that can change.
87 static int xilly_map_single_pci(struct xilly_endpoint
*ep
,
91 dma_addr_t
*ret_dma_handle
96 struct xilly_mapping
*this;
98 this = kzalloc(sizeof(*this), GFP_KERNEL
);
102 pci_direction
= xilly_pci_direction(direction
);
104 addr
= pci_map_single(ep
->pdev
, ptr
, size
, pci_direction
);
106 if (pci_dma_mapping_error(ep
->pdev
, addr
)) {
111 this->device
= ep
->pdev
;
112 this->dma_addr
= addr
;
114 this->direction
= pci_direction
;
116 *ret_dma_handle
= addr
;
118 return devm_add_action_or_reset(ep
->dev
, xilly_pci_unmap
, this);
121 static struct xilly_endpoint_hardware pci_hw
= {
122 .owner
= THIS_MODULE
,
123 .hw_sync_sgl_for_cpu
= xilly_dma_sync_single_for_cpu_pci
,
124 .hw_sync_sgl_for_device
= xilly_dma_sync_single_for_device_pci
,
125 .map_single
= xilly_map_single_pci
,
128 static int xilly_probe(struct pci_dev
*pdev
,
129 const struct pci_device_id
*ent
)
131 struct xilly_endpoint
*endpoint
;
134 endpoint
= xillybus_init_endpoint(pdev
, &pdev
->dev
, &pci_hw
);
139 pci_set_drvdata(pdev
, endpoint
);
141 rc
= pcim_enable_device(pdev
);
143 dev_err(endpoint
->dev
,
144 "pcim_enable_device() failed. Aborting.\n");
148 /* L0s has caused packet drops. No power saving, thank you. */
150 pci_disable_link_state(pdev
, PCIE_LINK_STATE_L0S
);
152 if (!(pci_resource_flags(pdev
, 0) & IORESOURCE_MEM
)) {
153 dev_err(endpoint
->dev
,
154 "Incorrect BAR configuration. Aborting.\n");
158 rc
= pcim_iomap_regions(pdev
, 0x01, xillyname
);
160 dev_err(endpoint
->dev
,
161 "pcim_iomap_regions() failed. Aborting.\n");
165 endpoint
->registers
= pcim_iomap_table(pdev
)[0];
167 pci_set_master(pdev
);
169 /* Set up a single MSI interrupt */
170 if (pci_enable_msi(pdev
)) {
171 dev_err(endpoint
->dev
,
172 "Failed to enable MSI interrupts. Aborting.\n");
175 rc
= devm_request_irq(&pdev
->dev
, pdev
->irq
, xillybus_isr
, 0,
176 xillyname
, endpoint
);
178 dev_err(endpoint
->dev
,
179 "Failed to register MSI handler. Aborting.\n");
184 * Some (old and buggy?) hardware drops 64-bit addressed PCIe packets,
185 * even when the PCIe driver claims that a 64-bit mask is OK. On the
186 * other hand, on some architectures, 64-bit addressing is mandatory.
187 * So go for the 64-bit mask only when failing is the other option.
190 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
191 endpoint
->dma_using_dac
= 0;
192 } else if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
193 endpoint
->dma_using_dac
= 1;
195 dev_err(endpoint
->dev
, "Failed to set DMA mask. Aborting.\n");
199 return xillybus_endpoint_discovery(endpoint
);
202 static void xilly_remove(struct pci_dev
*pdev
)
204 struct xilly_endpoint
*endpoint
= pci_get_drvdata(pdev
);
206 xillybus_endpoint_remove(endpoint
);
209 MODULE_DEVICE_TABLE(pci
, xillyids
);
211 static struct pci_driver xillybus_driver
= {
213 .id_table
= xillyids
,
214 .probe
= xilly_probe
,
215 .remove
= xilly_remove
,
218 module_pci_driver(xillybus_driver
);