forked from jpietari/mrf-linux-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrfevg.c
360 lines (314 loc) · 10 KB
/
mrfevg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
mrfevg.c -- Micro-Research Event Generator
Linux 2.6 driver
This driver handles multiple instances
Author: Jukka Pietarinen (MRF)
Date: 30.1.2014
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/pci.h>
#include <linux/seq_file.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/kdev_t.h>
#include <linux/interrupt.h>
#include <linux/version.h>
#include <asm/page.h>
#include <asm/uaccess.h>
#include "pci_mrfev.h"
MODULE_LICENSE("GPL");
#define DEVICE_NAME "mrfevg"
#define DETECT_UNCONFIGURED_BOARD 1
extern struct mrf_dev mrf_devices[MAX_MRF_DEVICES];
/*
File operations for Event Boards
*/
static struct file_operations evg_fops = {
.owner = THIS_MODULE,
.read = ev_read,
.write = ev_write,
#ifdef HAVE_UNLOCKED_IOCTL
.unlocked_ioctl = ev_unlocked_ioctl,
#else
.ioctl = ev_ioctl,
#endif
.open = ev_open,
.release = ev_release,
.fasync = ev_fasync,
.mmap = ev_remap_mmap,
};
/* The first prototypes shipped had both the vendor ID and subsystem
vendor ID set to MRF's vendor ID. Now, boards are shipped with
vendor ID and device ID set to PLX's 9030 default values and
subsystem field set to MRF's codes. This driver supports both
conventions. */
static struct pci_device_id evg_ids[] = {
{ .vendor = PCI_VENDOR_ID_PLX,
.device = PCI_DEVICE_ID_PLX_9030,
.subvendor = PCI_VENDOR_ID_MRF,
.subdevice = PCI_DEVICE_ID_MRF_PXIEVG220, },
{ .vendor = PCI_VENDOR_ID_PLX,
.device = PCI_DEVICE_ID_PLX_9030,
.subvendor = PCI_VENDOR_ID_MRF,
.subdevice = PCI_DEVICE_ID_MRF_PXIEVG230, },
{ PCI_DEVICE(PCI_VENDOR_ID_MRF, PCI_DEVICE_ID_MRF_PXIEVG220), },
#ifdef DETECT_UNCONFIGURED_BOARD
{ .vendor = MODULE_VENDOR_ID_NOCONF,
.device = MODULE_DEVICE_ID_NOCONF,
.subvendor = MODULE_SUBVENDOR_ID_NOCONF,
.subdevice = MODULE_SUBDEVICE_ID_NOCONF, },
#endif
{ .vendor = PCI_VENDOR_ID_MRF,
.device = PCI_DEVICE_ID_MRF_CPCIEVG300,
.subvendor = PCI_VENDOR_ID_MRF,
.subdevice = PCI_DEVICE_ID_MRF_CPCIEVG300},
{ .vendor = PCI_VENDOR_ID_LATTICE,
.device = PCI_DEVICE_ID_LATTICE_ECP3,
.subvendor = PCI_VENDOR_ID_MRF,
.subdevice = PCI_DEVICE_ID_MRF_CPCIEVG300},
{ .vendor = PCI_VENDOR_ID_XILINX,
.device = PCI_DEVICE_ID_0505,
.subvendor = PCI_VENDOR_ID_MRF,
.subdevice = PCI_DEVICE_ID_MRF_PXIEEVG300},
{ .vendor = PCI_VENDOR_ID_XILINX,
.device = PCI_DEVICE_ID_KINTEX7,
.subvendor = PCI_VENDOR_ID_MRF,
.subdevice = PCI_DEVICE_ID_MRF_MTCAEVG300},
{ 0, }};
MODULE_DEVICE_TABLE(pci, evg_ids);
static struct class *mrf_evg_class = NULL;
static int pci_evg_probe(struct pci_dev *pcidev, const struct pci_device_id *dev_id)
{
int i, res;
dev_t chrdev = 0;
struct mrf_dev *ev_device;
struct pci_device_id *id = (struct pci_device_id *) dev_id;
volatile u32 *evg_fw_version;
/* We keep device instance number in id->driver_data */
id->driver_data = -1;
/* Find empty mrf_dev structure - an empty subsys_id field shows this */
for (i = 0; i < MAX_MRF_DEVICES; i++)
if (mrf_devices[i].subsys_id == 0)
{
id->driver_data = i;
break;
}
if (id->driver_data < 0)
{
printk(KERN_WARNING DEVICE_NAME ": too many devices.\n");
return -EMFILE;
}
ev_device = &mrf_devices[id->driver_data];
/* Allocate device numbers for character device. */
res = alloc_chrdev_region(&chrdev, 0, DEVICE_MINOR_NUMBERS, DEVICE_NAME);
if (res < 0)
{
printk(KERN_WARNING DEVICE_NAME ": cannot register char device.\n");
return res;
}
/* Initialize device structure */
ev_device->major = MAJOR(chrdev);
cdev_init(&ev_device->cdev, &evg_fops);
ev_device->cdev.owner = THIS_MODULE;
ev_device->cdev.ops = &evg_fops;
ev_device->access_mode = 0;
ev_device->access_device = 0;
ev_device->eeprom_data = NULL;
ev_device->eeprom_data_size = 0;
ev_device->xcf_data = NULL;
ev_device->xcf_data_pos = -1;
ev_device->fpga_conf_data = NULL;
ev_device->fpga_conf_size = 0;
#ifndef init_MUTEX
sema_init(&ev_device->sem, 1);
#else
init_MUTEX(&ev_device->sem);
#endif
res = cdev_add(&ev_device->cdev, chrdev, DEVICE_MINOR_NUMBERS);
ev_device->jtag_refcount = 0;
ev_device->fpgaid = 0;
ev_device->xcfid = 0;
ev_device->fpgairlen = 0;
ev_device->xcfirlen = 0;
if (res)
{
printk(KERN_WARNING DEVICE_NAME ": error adding " DEVICE_NAME "0-%d\n.",
DEVICE_MINOR_NUMBERS);
}
/* Here we enable device before we can do any accesses to device. */
res = pci_enable_device(pcidev);
if (res)
{
printk(KERN_WARNING DEVICE_NAME ": error enabling device\n.");
return res;
}
for (i = 0; i < MAX_MRF_BARS; i++)
{
ev_device->BAR_start[i] = pci_resource_start(pcidev, i);
ev_device->BAR_end[i] = pci_resource_end(pcidev, i);
ev_device->BAR_flags[i] = pci_resource_flags(pcidev, i);
if (ev_device->BAR_start[i] != 0x00000000)
{
if (request_mem_region(ev_device->BAR_start[i], ev_device->BAR_end[i]
- ev_device->BAR_start[i] + 1,
DEVICE_NAME) != NULL)
{
ev_device->BAR_mmapped[i] = ioremap_nocache(ev_device->BAR_start[i],
ev_device->BAR_end[i] -
ev_device->BAR_start[i] + 1);
printk(KERN_WARNING DEVICE_NAME ":BAR%d start %08x end %08x mmaped %08x\n", i,
(unsigned int) ev_device->BAR_start[i],
(unsigned int) ev_device->BAR_end[i],
(unsigned int) ev_device->BAR_mmapped[i]);
}
else
printk(KERN_ALERT DEVICE_NAME ": Unable to to obtain I/O memory address 0x%08lx\n",
ev_device->BAR_start[i]);
}
else
ev_device->BAR_mmapped[i] = NULL;
}
/* Read subsystem device ID to identify board */
pci_read_config_word(pcidev, PCI_SUBSYSTEM_ID, &ev_device->subsys_id);
/* Set subsystem device ID to PXI-EVG-230 for unconfigured board. */
if (ev_device->subsys_id == 0)
ev_device->subsys_id = PCI_DEVICE_ID_MRF_PXIEVG230;
switch (ev_device->subsys_id)
{
case PCI_DEVICE_ID_MRF_PXIEVG220:
case PCI_DEVICE_ID_MRF_PXIEVG230:
case MODULE_SUBDEVICE_ID_NOCONF:
ev_device->devtype = MRF_DEVTYPE_V2P_9030;
ev_device->pLC = ev_device->BAR_mmapped[0];
ev_device->pEv = ev_device->BAR_mmapped[2];
break;
case PCI_DEVICE_ID_MRF_CPCIEVG300:
ev_device->devtype = MRF_DEVTYPE_ECP3_PCI;
ev_device->pEv = ev_device->BAR_mmapped[0];
break;
case PCI_DEVICE_ID_MRF_PXIEEVG300:
ev_device->devtype = MRF_DEVTYPE_V5_PCIE;
ev_device->pEv = ev_device->BAR_mmapped[0];
break;
case PCI_DEVICE_ID_MRF_MTCAEVG300:
ev_device->devtype = MRF_DEVTYPE_K7_PCIE;
ev_device->pEv = ev_device->BAR_mmapped[0];
break;
default:
printk(KERN_WARNING DEVICE_NAME ": Unknown subsystem device id 0x%04X\n.",
ev_device->subsys_id);
}
/* Check the interrupt line */
ev_device->irq = pcidev->irq;
/* Check firmware version */
evg_fw_version = ((ev_device->pEv) + EV_FW_VERSION_OFFSET);
ev_device->fw_version = be32_to_cpu(*evg_fw_version);
/* Register the devices so that device nodes are created. */
for (i = 0; i < DEVICE_MINOR_NUMBERS; ++i)
{
dev_t devno = MKDEV(ev_device->major, i);
char devletter = 'a' + (char) id->driver_data;
struct device *device = NULL;
device = device_create(mrf_evg_class, &pcidev->dev, devno, NULL, "eg%c%d",
devletter, i);
if (IS_ERR(device))
{
printk(KERN_WARNING DEVICE_NAME ": could not register device eg%c%d.\n", devletter, i);
}
}
return 0;
}
static void pci_evg_remove(struct pci_dev *pcidev)
{
int i;
struct mrf_dev *ev_device = NULL;
for (i = 0; i < MAX_MRF_DEVICES; i++)
if (mrf_devices[i].BAR_start[0] == pci_resource_start(pcidev, 0))
{
ev_device = &mrf_devices[i];
break;
}
if (ev_device == NULL)
{
printk(KERN_ALERT "Trying to remove uninstalled driver for bus\n");
}
else
{
/* Unregister the devices so that device nodes are removed. */
for (i = 0; i < DEVICE_MINOR_NUMBERS; ++i)
{
dev_t devno = MKDEV(ev_device->major, i);
device_destroy(mrf_evg_class, devno);
}
/* Unmap BARs */
for (i = 0; i < MAX_MRF_BARS; i++)
{
if (ev_device->BAR_mmapped[i] != NULL)
{
iounmap(ev_device->BAR_mmapped[i]);
release_mem_region(ev_device->BAR_start[i], ev_device->BAR_end[i]
- ev_device->BAR_start[i] + 1);
}
ev_device->BAR_mmapped[i] = NULL;
}
pci_disable_device(pcidev);
cdev_del(&ev_device->cdev);
unregister_chrdev_region(MKDEV(ev_device->major, 0), DEVICE_MINOR_NUMBERS);
/* Clear out subsys_id field when device is removed */
ev_device->subsys_id = 0;
}
}
static struct pci_driver evg_driver = {
.name = "pci_mrfevg",
.id_table = evg_ids,
.probe = pci_evg_probe,
.remove = pci_evg_remove,
};
int ev_assign_irq(struct mrf_dev *ev_device)
{
int result;
result = request_irq(ev_device->irq,
&ev_interrupt,
/* Is there need for SA_INTERRUPT? */
#if LINUX_VERSION_CODE > (0x020619)
IRQF_SHARED,
#else
SA_SHIRQ,
#endif
DEVICE_NAME,
(void *) ev_device);
if (result)
printk(KERN_INFO DEVICE_NAME ": cannot get interrupt %d\n",
ev_device->irq);
return result;
}
static int __init pci_evg_init(void)
{
/* Allocate and clear memory for all devices. */
memset(mrf_devices, 0, sizeof(struct mrf_dev)*MAX_MRF_DEVICES);
printk(KERN_ALERT "Event Generator PCI/PCIe driver init.\n");
mrf_evg_class = class_create(THIS_MODULE, DEVICE_NAME);
if (IS_ERR(mrf_evg_class))
printk(KERN_WARNING DEVICE_NAME ": cannot register device class.\n");
return pci_register_driver(&evg_driver);
}
static void __exit pci_evg_exit(void)
{
printk(KERN_ALERT "Event Generator PCI/PCIe driver exiting.\n");
pci_unregister_driver(&evg_driver);
if (mrf_evg_class)
{
class_destroy(mrf_evg_class);
mrf_evg_class = NULL;
}
}
module_init(pci_evg_init);
module_exit(pci_evg_exit);