blob: 9418300214e985472fbd76f1c3e5cd1d1c186a2a [file] [log] [blame]
Eli Billauer48bae052013-06-24 18:55:47 +03001/*
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
13#include <linux/module.h>
14#include <linux/pci.h>
15#include <linux/pci-aspm.h>
16#include <linux/slab.h>
17#include "xillybus.h"
18
19MODULE_DESCRIPTION("Xillybus driver for PCIe");
20MODULE_AUTHOR("Eli Billauer, Xillybus Ltd.");
21MODULE_VERSION("1.06");
22MODULE_ALIAS("xillybus_pcie");
23MODULE_LICENSE("GPL v2");
24
25#define PCI_DEVICE_ID_XILLYBUS 0xebeb
26
27#define PCI_VENDOR_ID_ALTERA 0x1172
28#define PCI_VENDOR_ID_ACTEL 0x11aa
29#define PCI_VENDOR_ID_LATTICE 0x1204
30
Eli Billauere71042f2013-07-27 00:24:00 +030031static const char xillyname[] = "xillybus_pcie";
32
Jingoo Han41e043f2013-12-03 08:26:00 +090033static const struct pci_device_id xillyids[] = {
Eli Billauer48bae052013-06-24 18:55:47 +030034 {PCI_DEVICE(PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_XILLYBUS)},
35 {PCI_DEVICE(PCI_VENDOR_ID_ALTERA, PCI_DEVICE_ID_XILLYBUS)},
36 {PCI_DEVICE(PCI_VENDOR_ID_ACTEL, PCI_DEVICE_ID_XILLYBUS)},
37 {PCI_DEVICE(PCI_VENDOR_ID_LATTICE, PCI_DEVICE_ID_XILLYBUS)},
38 { /* End: all zeroes */ }
39};
40
41static int xilly_pci_direction(int direction)
42{
43 switch (direction) {
44 case DMA_TO_DEVICE:
45 return PCI_DMA_TODEVICE;
46 case DMA_FROM_DEVICE:
47 return PCI_DMA_FROMDEVICE;
48 default:
49 return PCI_DMA_BIDIRECTIONAL;
50 }
51}
52
53static void xilly_dma_sync_single_for_cpu_pci(struct xilly_endpoint *ep,
54 dma_addr_t dma_handle,
55 size_t size,
56 int direction)
57{
58 pci_dma_sync_single_for_cpu(ep->pdev,
59 dma_handle,
60 size,
61 xilly_pci_direction(direction));
62}
63
64static void xilly_dma_sync_single_for_device_pci(struct xilly_endpoint *ep,
65 dma_addr_t dma_handle,
66 size_t size,
67 int direction)
68{
69 pci_dma_sync_single_for_device(ep->pdev,
70 dma_handle,
71 size,
72 xilly_pci_direction(direction));
73}
74
Eli Billauer525be902014-06-21 14:07:12 +030075static void xilly_pci_unmap(void *ptr)
76{
77 struct xilly_mapping *data = ptr;
78
79 pci_unmap_single(data->device, data->dma_addr,
80 data->size, data->direction);
81
82 kfree(ptr);
83}
84
Eli Billauer48bae052013-06-24 18:55:47 +030085/*
86 * Map either through the PCI DMA mapper or the non_PCI one. Behind the
87 * scenes exactly the same functions are called with the same parameters,
88 * but that can change.
89 */
90
Eli Billauer525be902014-06-21 14:07:12 +030091static int xilly_map_single_pci(struct xilly_endpoint *ep,
92 void *ptr,
93 size_t size,
94 int direction,
95 dma_addr_t *ret_dma_handle
Eli Billauer48bae052013-06-24 18:55:47 +030096 )
97{
Eli Billauer48bae052013-06-24 18:55:47 +030098 int pci_direction;
Eli Billauer525be902014-06-21 14:07:12 +030099 dma_addr_t addr;
100 struct xilly_mapping *this;
Eli Billauer40931bb2014-09-04 17:47:48 +0300101 int rc;
Eli Billauer48bae052013-06-24 18:55:47 +0300102
Eli Billauer525be902014-06-21 14:07:12 +0300103 this = kzalloc(sizeof(*this), GFP_KERNEL);
Eli Billauer48bae052013-06-24 18:55:47 +0300104 if (!this)
Eli Billauer525be902014-06-21 14:07:12 +0300105 return -ENOMEM;
Eli Billauer48bae052013-06-24 18:55:47 +0300106
107 pci_direction = xilly_pci_direction(direction);
Eli Billauer525be902014-06-21 14:07:12 +0300108
Eli Billauer48bae052013-06-24 18:55:47 +0300109 addr = pci_map_single(ep->pdev, ptr, size, pci_direction);
Eli Billauer48bae052013-06-24 18:55:47 +0300110
111 if (pci_dma_mapping_error(ep->pdev, addr)) {
112 kfree(this);
Eli Billauer525be902014-06-21 14:07:12 +0300113 return -ENODEV;
Eli Billauer48bae052013-06-24 18:55:47 +0300114 }
115
Eli Billauer525be902014-06-21 14:07:12 +0300116 this->device = ep->pdev;
Eli Billauer48bae052013-06-24 18:55:47 +0300117 this->dma_addr = addr;
Eli Billauer48bae052013-06-24 18:55:47 +0300118 this->size = size;
Eli Billauer525be902014-06-21 14:07:12 +0300119 this->direction = pci_direction;
Eli Billauer48bae052013-06-24 18:55:47 +0300120
Eli Billauer525be902014-06-21 14:07:12 +0300121 *ret_dma_handle = addr;
Eli Billauer48bae052013-06-24 18:55:47 +0300122
Eli Billauer525be902014-06-21 14:07:12 +0300123 rc = devm_add_action(ep->dev, xilly_pci_unmap, this);
Eli Billauer525be902014-06-21 14:07:12 +0300124 if (rc) {
125 pci_unmap_single(ep->pdev, addr, size, pci_direction);
126 kfree(this);
Eli Billauer40931bb2014-09-04 17:47:48 +0300127 return rc;
Eli Billauer525be902014-06-21 14:07:12 +0300128 }
129
Eli Billauer40931bb2014-09-04 17:47:48 +0300130 return 0;
Eli Billauer48bae052013-06-24 18:55:47 +0300131}
132
133static struct xilly_endpoint_hardware pci_hw = {
134 .owner = THIS_MODULE,
Eli Billauer7ee9ded2013-07-31 11:22:43 +0300135 .hw_sync_sgl_for_cpu = xilly_dma_sync_single_for_cpu_pci,
136 .hw_sync_sgl_for_device = xilly_dma_sync_single_for_device_pci,
Eli Billauer48bae052013-06-24 18:55:47 +0300137 .map_single = xilly_map_single_pci,
Eli Billauer48bae052013-06-24 18:55:47 +0300138};
139
140static int xilly_probe(struct pci_dev *pdev,
Eli Billauer91a2dea2014-09-04 17:47:56 +0300141 const struct pci_device_id *ent)
Eli Billauer48bae052013-06-24 18:55:47 +0300142{
143 struct xilly_endpoint *endpoint;
Eli Billauer40931bb2014-09-04 17:47:48 +0300144 int rc;
Eli Billauer48bae052013-06-24 18:55:47 +0300145
Eli Billauer26a6fc92013-10-19 01:02:25 +0300146 endpoint = xillybus_init_endpoint(pdev, &pdev->dev, &pci_hw);
Eli Billauer48bae052013-06-24 18:55:47 +0300147
148 if (!endpoint)
149 return -ENOMEM;
150
151 pci_set_drvdata(pdev, endpoint);
152
Eli Billauer92674622014-05-11 19:53:46 +0300153 rc = pcim_enable_device(pdev);
Eli Billauer92674622014-05-11 19:53:46 +0300154 if (rc) {
155 dev_err(endpoint->dev,
156 "pcim_enable_device() failed. Aborting.\n");
157 return rc;
158 }
Eli Billauer48bae052013-06-24 18:55:47 +0300159
160 /* L0s has caused packet drops. No power saving, thank you. */
161
162 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
163
Eli Billauer48bae052013-06-24 18:55:47 +0300164 if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
Eli Billauer2cb85c02013-10-19 01:02:26 +0300165 dev_err(endpoint->dev,
166 "Incorrect BAR configuration. Aborting.\n");
Eli Billauer92674622014-05-11 19:53:46 +0300167 return -ENODEV;
Eli Billauer48bae052013-06-24 18:55:47 +0300168 }
169
Eli Billauer92674622014-05-11 19:53:46 +0300170 rc = pcim_iomap_regions(pdev, 0x01, xillyname);
Eli Billauer48bae052013-06-24 18:55:47 +0300171 if (rc) {
Eli Billauer2cb85c02013-10-19 01:02:26 +0300172 dev_err(endpoint->dev,
Eli Billauer92674622014-05-11 19:53:46 +0300173 "pcim_iomap_regions() failed. Aborting.\n");
174 return rc;
Eli Billauer48bae052013-06-24 18:55:47 +0300175 }
176
Eli Billauer92674622014-05-11 19:53:46 +0300177 endpoint->registers = pcim_iomap_table(pdev)[0];
Eli Billauer48bae052013-06-24 18:55:47 +0300178
179 pci_set_master(pdev);
180
181 /* Set up a single MSI interrupt */
182 if (pci_enable_msi(pdev)) {
Eli Billauer2cb85c02013-10-19 01:02:26 +0300183 dev_err(endpoint->dev,
184 "Failed to enable MSI interrupts. Aborting.\n");
Eli Billauer92674622014-05-11 19:53:46 +0300185 return -ENODEV;
Eli Billauer48bae052013-06-24 18:55:47 +0300186 }
Eli Billauer92674622014-05-11 19:53:46 +0300187 rc = devm_request_irq(&pdev->dev, pdev->irq, xillybus_isr, 0,
188 xillyname, endpoint);
Eli Billauer48bae052013-06-24 18:55:47 +0300189 if (rc) {
Eli Billauer2cb85c02013-10-19 01:02:26 +0300190 dev_err(endpoint->dev,
191 "Failed to register MSI handler. Aborting.\n");
Eli Billauer92674622014-05-11 19:53:46 +0300192 return -ENODEV;
Eli Billauer48bae052013-06-24 18:55:47 +0300193 }
194
195 /*
Eli Billauer6497a872015-08-05 13:03:26 +0300196 * Some (old and buggy?) hardware drops 64-bit addressed PCIe packets,
197 * even when the PCIe driver claims that a 64-bit mask is OK. On the
198 * other hand, on some architectures, 64-bit addressing is mandatory.
199 * So go for the 64-bit mask only when failing is the other option.
Eli Billauer48bae052013-06-24 18:55:47 +0300200 */
201
Eli Billauerc14cc622014-09-04 17:47:51 +0300202 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Eli Billauer48bae052013-06-24 18:55:47 +0300203 endpoint->dma_using_dac = 0;
Eli Billauer6497a872015-08-05 13:03:26 +0300204 } else if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
205 endpoint->dma_using_dac = 1;
Eli Billauerc14cc622014-09-04 17:47:51 +0300206 } else {
Eli Billauer2cb85c02013-10-19 01:02:26 +0300207 dev_err(endpoint->dev, "Failed to set DMA mask. Aborting.\n");
Eli Billauer92674622014-05-11 19:53:46 +0300208 return -ENODEV;
Eli Billauer48bae052013-06-24 18:55:47 +0300209 }
210
Eli Billauer525be902014-06-21 14:07:12 +0300211 return xillybus_endpoint_discovery(endpoint);
Eli Billauer48bae052013-06-24 18:55:47 +0300212}
213
214static void xilly_remove(struct pci_dev *pdev)
215{
216 struct xilly_endpoint *endpoint = pci_get_drvdata(pdev);
217
218 xillybus_endpoint_remove(endpoint);
Eli Billauer48bae052013-06-24 18:55:47 +0300219}
220
221MODULE_DEVICE_TABLE(pci, xillyids);
222
223static struct pci_driver xillybus_driver = {
224 .name = xillyname,
225 .id_table = xillyids,
226 .probe = xilly_probe,
227 .remove = xilly_remove,
228};
229
230module_pci_driver(xillybus_driver);