forked from ceton/infinitv_pcie
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathctn91xx_pci.c
290 lines (224 loc) · 7.8 KB
/
ctn91xx_pci.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
#include "ctn91xx_pci.h"
#include "ctn91xx_util.h"
#include "ctn91xx_interrupt.h"
#include "ctn91xx_mpeg.h"
#include "ctn91xx_net.h"
#include "ctn91xx_reset.h"
#include "ctn91xx_driver.h"
#if USE_PCI
#define MAX_BOARDS 20
static int boards[MAX_BOARDS] = {};
static int face_present = 0;
#if LINUX_VERSION_CODE > KERNEL_VERSION(5,6,0)
#define ioremap_nocache(a,b) ioremap(a,b)
#endif
static int ctn91xx_register(struct pci_dev *pdev, const struct pci_device_id *ent)
{
uint32_t freq;
ctn91xx_dev_t* dev = kzalloc( sizeof( ctn91xx_dev_t ), GFP_KERNEL );
if(!dev) {
return -ENOMEM;
}
dev->pdev = pdev;
if(!ctn91xx_board_init(dev)) {
return -ENOMEM;
}
if(pci_enable_device(pdev) != 0) {
ERROR("cannot enable pci device");
return -ENODEV;
}
pci_set_drvdata(pdev, (void*)dev);
dev->irq = pdev->irq;
if(pci_request_regions(pdev, "ctn91xx") != 0) {
pci_disable_device(pdev);
ERROR("pci device busy");
return -EBUSY;
}
/* Try to init DMA */
#if LINUX_VERSION_CODE < KERNEL_VERSION(3,1,0)
if(pci_set_dma_mask(pdev, DMA_32BIT_MASK)) {
#else
#if LINUX_VERSION_CODE < KERNEL_VERSION(5,18,0)
if(pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
#else
if(dma_set_mask(& pdev->dev, DMA_BIT_MASK(32))) {
#endif
#endif
ERROR("No suitable DMA mask available.");
}
pci_set_master(pdev);
dev->hw_reg_base = pci_resource_start(pdev, 0);
dev->base = ioremap_nocache(dev->hw_reg_base, CTN91XX_REG_REGION_SIZE);
if(!dev->base) {
ERROR("reg ioremap failed");
return -EIO;
}
INFO("IO hw_reg_base address: %lx", dev->hw_reg_base);
INFO("reg_base: %p", dev->base);
dev->translation_hw_reg_base = pci_resource_start(pdev, 1);
dev->translation_base = ioremap_nocache(dev->translation_hw_reg_base, CTN91XX_TRANSLATION_REG_REGION_SIZE);
if(!dev->translation_base) {
WARNING("translation reg ioremap failed");
} else {
INFO("IO translation_hw_reg_base address: %lx", dev->translation_hw_reg_base);
INFO("translation_reg_base: %p", dev->translation_base);
}
dev->mpeg_filter_dma_base = dev->base + FILTER_DMA_REG_OFFSET;
dev->mpeg_dma_base = dev->base + DMA_REG_OFFSET;
dev->system_control_base = dev->base + SYSTEM_CONTROL_OFFSET;
dev->msg_base = dev->base + MSG_BUFFER_OFFSET;
dev->timer_reg_base = dev->base + TIMER_REG_OFFSET;
dev->release_version = ctn91xx_read16(dev->system_control_base, CTN91XX_RELEASE_VERSION_OFFSET);
dev->chip_version = ctn91xx_read16(dev->system_control_base, CTN91XX_CHIP_VERSION_OFFSET);
if(dev->release_version == 0 || dev->chip_version == 0) {
WARNING("read versions %d and %d, retrying", dev->release_version, dev->chip_version);
dev->release_version = ctn91xx_read16(dev->system_control_base, CTN91XX_RELEASE_VERSION_OFFSET);
dev->chip_version = ctn91xx_read16(dev->system_control_base, CTN91XX_CHIP_VERSION_OFFSET);
}
if( dev->release_version == 0xffff ) {
ERROR("chip not configured %04x", dev->release_version);
goto reg_base_cleanup;
}
freq = ctn91xx_read32(dev->system_control_base, SYSTEM_CONTROL_CLOCK_FREQ);
dev->ticks_per_us = (freq / 1000000);
INFO("system clock freq %d, ticks per us %d", freq, dev->ticks_per_us);
if( dev->ticks_per_us == 100 ) {
//FIXME: acting like noah based on clock frequency
dev->is_noah = 1;
}
if( dev->is_noah ) {
//overwrite board_number with correct value from hw, if it supports it
//must be done before device files are registered
uint32_t tag = ctn91xx_read32( dev->msg_base, MSG_BUFFER_TAG );
uint8_t retries_left = 2;
uint8_t valid = ( tag >> 16 ) & 0x1;
dev->board_number = ( tag >> 8 ) & 0xff;
//0 is the reset value, and a valid board number (d'oh!)
//so try to ensure we got the right one
if( dev->board_number == 0 ) {
while( !valid && retries_left > 0 ) {
msleep(1000);
tag = ctn91xx_read32( dev->msg_base, MSG_BUFFER_TAG );
valid = ( tag >> 16 ) & 0x1;
dev->board_number = ( tag >> 8 ) & 0xff;
retries_left--;
}
}
dev->board_booting = valid;
if( valid && boards[0] == 0 && dev->board_number == 0 ) {
INFO("face detected as present");
face_present = 1;
}
dev->face_present = face_present;
if( !dev->face_present ) {
//will be set to next available by code below
dev->board_number = 0;
}
}
if( dev->board_number > MAX_BOARDS ) {
ERROR("invalid board number %d", dev->board_number);
dev->board_number = MAX_BOARDS;
}
if( !boards[dev->board_number] ) {
boards[dev->board_number] = 1;
} else {
int i;
for( i=0; i<MAX_BOARDS; i++ ) {
if( !boards[i] ) {
dev->board_number = i;
boards[i] = 1;
break;
}
}
}
if(dev->release_version != 0 && dev->chip_version != 0) {
dev->which_proc = ctn91xx_read32(dev->base, PROC_IDENT_OFFSET);
ctn91xx_reset_all(dev);
INFO("Driver initialization successful for CTN-%d v%d", dev->chip_version, dev->release_version);
} else {
WARNING("release_version %d chip_version %d", dev->release_version, dev->chip_version);
}
if( ctn91xx_register_ctrl_device( dev ) ) {
ERROR("Unable to register char device");
goto reg_base_cleanup;
}
#if HAS_MPEG_DMA
if( ctn91xx_init_mpeg(dev) < 0 ) {
goto reg_base_cleanup;
}
#endif
if( ctn91xx_net_init( dev ) ) {
ERROR("failed to init net device");
goto dev_cleanup;
}
/* Now enable our interrupt */
dev->int_enable = 1;
INFO("irq: %d", pdev->irq);
if(request_irq(pdev->irq, ctn91xx_isr, IRQF_SHARED, DEVICE_NAME, dev)) {
ERROR("Cannot allocate irq %d", pdev->irq);
pdev->irq = 0;
goto net_cleanup;
}
return 0;
net_cleanup:
ctn91xx_net_deinit( dev );
dev_cleanup:
ctn91xx_unregister_ctrl_device( dev );
#if HAS_MPEG_DMA
ctn91xx_deinit_mpeg(dev);
#endif
reg_base_cleanup:
if(dev->translation_base) {
iounmap(dev->translation_base);
}
iounmap(dev->base);
pci_release_regions(pdev);
pci_disable_device(pdev);
return -EIO;
}
static void ctn91xx_unregister(struct pci_dev *pdev)
{
ctn91xx_dev_t* dev = (ctn91xx_dev_t*)pci_get_drvdata(pdev);
free_irq(pdev->irq, dev);
dev->int_enable = 0;
ctn91xx_write8(0x0, dev->mpeg_dma_base, MPEG_DMA_INTERRUPT_ENABLE);
ctn91xx_write8(0x1, dev->mpeg_dma_base, MPEG_DMA_RESET);
msleep(1);
if(ctn91xx_read8(dev->mpeg_dma_base, MPEG_DMA_BUSY)) {
ERROR("resetting while dma is busy, this is BAD");
}
ctn91xx_net_deinit( dev );
iounmap(dev->base);
if(dev->translation_base) {
iounmap(dev->translation_base);
}
ctn91xx_unregister_ctrl_device( dev );
#if HAS_MPEG_DMA
ctn91xx_deinit_mpeg(dev);
#endif
pci_release_regions(pdev);
pci_disable_device(pdev);
ctn91xx_board_uninit(dev);
kfree( dev );
}
static struct pci_device_id ctn91xx_table[] = {
{ CETON_VENDOR_ID, CTN91XX_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
{ ALTERA_VENDOR_ID, CTN91XX_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
{ 0, }
};
static struct pci_driver ctn91xx_driver = {
.name = DEVICE_NAME,
.id_table = ctn91xx_table,
.probe = ctn91xx_register,
.remove = ctn91xx_unregister,
};
int ctn91xx_register_pci_driver()
{
return pci_register_driver(&ctn91xx_driver);
}
void ctn91xx_unregister_pci_driver()
{
pci_unregister_driver(&ctn91xx_driver);
}
MODULE_DEVICE_TABLE(pci, ctn91xx_table);
#endif