-
Notifications
You must be signed in to change notification settings - Fork 1
/
agtbus.c
348 lines (255 loc) · 6.47 KB
/
agtbus.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
#include <linux/platform_device.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci_ids.h>
#include <linux/interrupt.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/serial.h>
#include <linux/console.h>
#include <linux/serial_core.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/clk.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/of.h>
#include <uapi/linux/serial_reg.h>
#include "agtbus.h"
#include "agt_version.h"
#define SUCCESS 0
#define PREFIX "[agtbus]: "
#define PCI_DEVICE_ID_XILINX 0x7013
#define DGT_BUF_SIZE (size_t)(sizeof(struct dgt_xpdev_t))
struct dgt_xpdev_t drv_access;
struct dgt_xpdev_t *drv_access2 = NULL;
static int isIRQ = 0;
unsigned int AXI_UART_REG[] =
{
AXI4_UART0_REG,
AXI4_UART1_REG,
AXI4_UART2_REG,
AXI4_UART3_REG,
AXI4_UART4_REG,
AXI4_UART5_REG,
AXI4_UART6_REG,
AXI4_UART7_REG,
AXI4_UART9_REG
};
#define COUNT_METERS 8
typedef struct
{
int array_meters[COUNT_METERS];
int array_crc[COUNT_METERS];
unsigned int turn;
unsigned int update;
u64 mask;
} meters_data;
meters_data g_meters_data;
#include "nvram.c"
#include "uart_agt.c"
#include "buttons.c"
/*************** PCIe Main functions ****************************/
// IRQ handler function
static irqreturn_t dgt_xpdev_irq(int irq, void* data)
{
return IRQ_HANDLED;
}
static int dgt_xpdev_create(struct pci_dev* pcidev, struct dgt_xpdev_t** result)
{
struct dgt_xpdev_t* xpdev;
xpdev = kmalloc(sizeof(dgt_xpdev_t), GFP_KERNEL);
if (xpdev == NULL)
return -ENOMEM;
memset(xpdev, 0, sizeof(dgt_xpdev_t));
spin_lock_init(&xpdev->lock);
xpdev->pcidev = pcidev;
pci_set_drvdata(pcidev, xpdev);
*result = xpdev;
return SUCCESS;
}
static int agtbus_probe(struct pci_dev* dev, const struct pci_device_id* id);
static void agtbus_remove(struct pci_dev* dev);
#if LINUX_VERSION_CODE < KERNEL_VERSION(4,8,0)
DEFINE_PCI_DEVICE_TABLE(agtbus_id_table)=
#else
static const struct pci_device_id agtbus_id_table[] =
#endif
{
{ PCI_DEVICE(PCI_VENDOR_ID_XILINX, PCI_DEVICE_ID_XILINX) },
{ 0, 0 }
};
static struct pci_driver agtbus_pci_driver =
{
.name = KBUILD_MODNAME,
.id_table = agtbus_id_table,
.probe = agtbus_probe,
.remove = agtbus_remove
};
static int agtbus_probe(struct pci_dev* pcidev, const struct pci_device_id* id)
{
int bar;
int res;
u16 vendor, device;
unsigned char myirq = 0;
unsigned char irq_pin = 0;
struct dgt_xpdev_t* xpdev;
printk(KERN_DEBUG PREFIX "!!!! agtbus_probe\n");
res = pci_enable_device(pcidev);
if(res)
{
printk(KERN_DEBUG PREFIX "Couldn't enable the device\n");
return res;
}
pci_read_config_word(pcidev, PCI_VENDOR_ID, &vendor);
pci_read_config_word(pcidev, PCI_DEVICE_ID, &device);
res = pci_read_config_byte(pcidev, PCI_INTERRUPT_LINE, &myirq);
if(res)
{
printk(PREFIX "Error read IRQ from FPGA\n");
return res;
}
printk(PREFIX "Device vendor: 0x%X device_id: 0x%X\n", vendor, device);
printk(PREFIX "IRQ: irq: %u\n", myirq);
pci_read_config_byte(pcidev, PCI_INTERRUPT_PIN, &irq_pin);
if(res)
{
printk(PREFIX "Error read IRQ PIN\n");
return res;
}
printk(PREFIX "IRQ PIN: %u\n", irq_pin);
res = dgt_xpdev_create(pcidev, &xpdev);
if (res != SUCCESS)
{
printk(KERN_ERR PREFIX "Coudln't create xpdev.\n");
return res;
}
xpdev->base_start = pci_resource_start(pcidev, 0);
xpdev->base_len = pci_resource_len(pcidev, 0);
bar = pci_select_bars(pcidev, IORESOURCE_MEM);
printk(PREFIX "Device available MEM BAR are 0x%x\n", bar);
res = pci_enable_device_mem(pcidev);
if(res != SUCCESS)
{
printk(KERN_ERR PREFIX "Failed to enable device memory, err: %i\n", res);
return res;
}
res = pci_request_region(pcidev, bar, "agtbus");
if (res != SUCCESS)
{
printk(KERN_ERR PREFIX "Coudln't pci_request_region.\n");
return res;
}
xpdev->iobase = ioremap(xpdev->base_start, xpdev->base_len);
if (!xpdev->iobase)
{
printk(KERN_ERR PREFIX "Coudln't map the iobase in the system.\n");
//release_device(pcidev);
pci_release_region(pcidev, pci_select_bars(pcidev, IORESOURCE_MEM));
pci_disable_device(pcidev);
return -ENOMEM;
}
{
if(isIRQ)
{
int res;
res = request_irq(pcidev->irq, dgt_xpdev_irq, IRQF_SHARED, "agtbus", xpdev);
if (res != SUCCESS)
{
printk(KERN_ERR PREFIX "Unable to request IRQ %d\n", pcidev->irq);
return res;
}
}
printk(KERN_ERR PREFIX "Set handler to IRQ %d\n", pcidev->irq);
xpdev->irq = pcidev->irq;
}
// set MRAM enable
{
dgt_xpdev_writeb(xpdev, AXI4_FPGA_GMIO0_REG + 16, 1);
}
{
dgt_xpdev_writeb(xpdev, AXI4_FPGA_GMIO0_REG + 13, 1);
}
{
dgt_xpdev_writeb(xpdev, AXI4_FPGA_GMIO0_REG + 14, 1);
}
{
// mask interrupt
unsigned int int_mask = 0x02FF0000;
dgt_xpdev_writel(xpdev, AXI4_INTERRUPT_REG + 0x20, int_mask);
}
// set timer
{
dgt_xpdev_writeb(xpdev, AXI4_FPGA_GMIO0_REG + 15, 0);
udelay(1000);
dgt_xpdev_writeb(xpdev, AXI4_FPGA_GMIO0_REG + 15, 1);
}
{ //IREDR
unsigned int int_mask = 0x00000027;
dgt_xpdev_writel(xpdev, AXI4_INTERRUPT_REG + 0x24, int_mask);
}
drv_access = *xpdev;
drv_access2 = xpdev;
//create NVRAM device
create_mramdev();
//create button device
create_but();
//create UART device
create_uart();
return SUCCESS;
}
static void agtbus_remove(struct pci_dev* dev)
{
struct dgt_xpdev_t* xpdev = pci_get_drvdata(dev);
printk(KERN_DEBUG PREFIX "!!!! agtbus_remove\n");
if(xpdev)
{
if(isIRQ)
{
if (xpdev->irq > 0)
{
free_irq(xpdev->irq, xpdev);
}
}
if (xpdev->iobase != NULL)
{
iounmap(xpdev->iobase);
}
kfree(xpdev);
}
pci_disable_device(dev);
}
static int __init agtbus_init(void)
{
int ret;
printk(KERN_DEBUG PREFIX "AGT_MODULE_VERSION %s\n", AGT__MODULE_VERSION);
ret = pci_register_driver(&agtbus_pci_driver);
if(ret != SUCCESS)
{
printk(KERN_ERR PREFIX "Couldn't register the pcidev\n");
return ret;
}
printk(KERN_DEBUG PREFIX "Enable the device\n");
return SUCCESS;
}
static void __exit agtbus_exit(void)
{
printk(KERN_DEBUG PREFIX "Exit\n");
// remove NVRAM device
remove_mramdev();
// remove buttons device
remove_but();
// remove UART device
remove_uart();
pci_unregister_driver(&agtbus_pci_driver);
}
module_init(agtbus_init);
module_exit(agtbus_exit);
MODULE_DESCRIPTION("Xilinx AGT bus PCIe driver");
MODULE_AUTHOR("cyrillwork");
MODULE_LICENSE("GPL");
MODULE_VERSION("0.0.1");
MODULE_ALIAS("agtbus_pcie");