-
Notifications
You must be signed in to change notification settings - Fork 25
/
pandriver-dma.c
356 lines (316 loc) · 9.84 KB
/
pandriver-dma.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
/*
* panalyzer.c A Logic Analyzer for the RaspberryPi
* Copyright (c) 2012 Richard Hirst <richardghirst@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* If you want the device node created automatically:
*
* ============= /etc/udev/rules.d/20-panalyzer.rules =============
* SUBSYSTEM=="module", DEVPATH=="/module/pandriver", RUN+="/lib/udev/panalyzer"
* ================================================================
*
* ===================== /lib/udev/panalyzer ======================
* #!/bin/bash
*
* if [ "$ACTION" = "remove" ]; then
* rm -f /dev/panalyzer
* elif [ "$ACTION" = "add" ]; then
* major=$( sed -n 's/ panalyzer//p' /proc/devices )
* [ "$major" ] && mknod -m 0666 /dev/panalyzer c $major 0
* fi
*
* exit 0
* ================================================================
*/
/* TODO:
* - Ensure relevant GPIO pins are inputs
*/
#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/vmalloc.h>
#include <linux/cdev.h>
#include <linux/scatterlist.h>
#include <linux/delay.h>
#include <mach/platform.h>
#include <asm/uaccess.h>
#include <mach/dma.h>
#include "panalyzer.h"
static int dev_open(struct inode *, struct file *);
static int dev_close(struct inode *, struct file *);
static ssize_t dev_read(struct file *, char *, size_t, loff_t *);
static ssize_t dev_write(struct file *, const char *, size_t, loff_t *);
static loff_t dev_llseek(struct file *flip, loff_t off, int whence);
static panctl_t def_panctl = DEF_PANCTL;
static panctl_t panctl;
static struct file_operations fops =
{
.open = dev_open,
.read = dev_read,
.write = dev_write,
.llseek = dev_llseek,
.release = dev_close,
};
static uint32_t *buffer;
static uint32_t first_data_index;
static volatile uint32_t *data;
static volatile uint32_t *ticker;
static volatile uint32_t *armtick;
static volatile uint32_t *dmactl;
static dev_t devno;
static struct cdev my_cdev;
static int my_major;
//static uint32_t foo, bar;
static int capture(void)
{
uint32_t start_time, end_time, abort_time, t = 0, t1;
uint32_t start_tick, end_tick;
uint32_t *buf_start = buffer;
uint32_t *buf_end = buffer + panctl.num_samples;
uint32_t *buf_ptr = buf_start;
uint32_t sample;
int post_trigger_samples;
int pre_trigger_samples;
int state = 0, last_state, state_samples = panctl.trigger[0].min_samples;
int overruns = 0;
for (last_state = 0; last_state < MAX_TRIGGERS && panctl.trigger[last_state].enabled; last_state++)
;
if (--last_state < 0)
state = -1;
if (panctl.trigger_point == 0)
post_trigger_samples = panctl.num_samples * 19 / 20;
else if (panctl.trigger_point == 1)
post_trigger_samples = panctl.num_samples / 2;
else
post_trigger_samples = panctl.num_samples / 20;
pre_trigger_samples = panctl.num_samples - post_trigger_samples;
local_irq_disable();
local_fiq_disable();
start_time = *ticker;
start_tick = armtick[8];
t = start_time;
abort_time = start_time + (panctl.num_samples > 500000 ? panctl.num_samples * 2 : 1000000);
#ifdef RUN_FLATOUT
armtick[2] = 1<<9;
while (buf_ptr != buf_end) {
volatile uint32_t x = *ticker;
*buf_ptr++ = armtick[8];
}
#else
for (;;) {
do { t1 = *ticker; } while (t1 == t);
sample = *ticker;
overruns += t1 - t - 1;
t = t1;
*buf_ptr++ = sample;
if (buf_ptr == buf_end)
buf_ptr = buf_start;
if (pre_trigger_samples > 0) {
pre_trigger_samples--;
} else if (state < 0) {
if (--post_trigger_samples <= 0)
break;
} else if (t1 == abort_time) {
local_irq_enable();
printk(KERN_INFO "Aborted, state %d, mask %08x, value %08x, sample %08x, state_samples %d\n",
state, panctl.trigger[state].mask, panctl.trigger[state].value, sample, state_samples);
return -EINTR;
} else {
recheck:
// TODO: Not convinced this logic is correct..
if (state_samples == 0) {
if (state == last_state) {
state = -1;
continue;
} else if ((panctl.trigger[state+1].mask & sample) == panctl.trigger[state+1].value) {
state++;
state_samples = panctl.trigger[state].min_samples - 1;
goto recheck;
}
} else {
state_samples--;
}
if ((panctl.trigger[state].mask & sample) != panctl.trigger[state].value) {
state = 0;
state_samples = panctl.trigger[state].min_samples;
}
}
}
#endif
end_time = *ticker;
end_tick = armtick[8];
local_fiq_enable();
local_irq_enable();
first_data_index = buf_ptr - buf_start;
// printk(KERN_INFO "%d samples in %dus, %d overruns\n", panctl.num_samples, end_time - start_time, overruns);
printk(KERN_INFO "%d samples in %dus\n", end_tick - start_tick, end_time - start_time);
return 0;
}
int init_module(void)
{
int res;
res = alloc_chrdev_region(&devno, 0, 1, "panalyzer");
if (res < 0) {
printk(KERN_WARNING "Panalyzer: Can't allocated device number\n");
return res;
}
my_major = MAJOR(devno);
cdev_init(&my_cdev, &fops);
my_cdev.owner = THIS_MODULE;
my_cdev.ops = &fops;
res = cdev_add(&my_cdev, MKDEV(my_major, 0), 1);
if (res) {
printk(KERN_WARNING "Panalyzer: Error %d adding device\n", res);
unregister_chrdev_region(devno, 1);
return res;
}
data = (uint32_t *)ioremap(0x20200034, 4);
ticker = (uint32_t *)ioremap(0x20003004, 4);
armtick = (uint32_t *)ioremap(0x2000b400, 0x24);
dmactl = (uint32_t *)ioremap(DMA_BASE, 0x24);
return 0;
}
void cleanup_module(void)
{
iounmap(data);
iounmap(ticker);
iounmap(armtick);
cdev_del(&my_cdev);
unregister_chrdev_region(devno, 1);
}
static int dev_open(struct inode *inod,struct file *fil)
{
memcpy(&panctl, &def_panctl, sizeof(panctl));
return 0;
}
static ssize_t dev_read(struct file *filp,char *buf,size_t count,loff_t *f_pos)
{
if (panctl.magic != PAN_MAGIC)
return -EINVAL;
#define BUF_ORDER 10
#define NUM_CBS 1
#define BCM2708_DMA_END (1<<1) // Why is this not in mach/dma.h ?
if (buffer == NULL) {
struct bcm2708_dma_cb *cb;
uint32_t min, max;
int i;
int *histogram;
buffer = (uint32_t *)__get_free_pages(GFP_KERNEL, BUF_ORDER); // order 10 = 4MB, 1M samples, about 50ms ?
if (buffer == NULL) {
printk(KERN_ALERT "alloc_pages failed\n");
return -EFAULT;
}
memset(buffer, 0, 0x1000 << BUF_ORDER);
cb = (struct bcm2708_dma_cb *)(buffer + (0x1000 << BUF_ORDER >> 2)) - NUM_CBS;
printk(KERN_INFO "Buffer address %p, cb address %p\n", buffer, cb);
// return -EFAULT;
cb->info = BCM2708_DMA_D_INC
| BCM2708_DMA_BURST(15)
| BCM2708_DMA_D_WIDTH
| BCM2708_DMA_WAITS(8);
cb->src = 0x7e00b420; // Free running 250MHz counter
cb->dst = (uint32_t)buffer & 0x7fffffff; // Hmmm...
cb->length = (0x1000 << BUF_ORDER) - sizeof(struct bcm2708_dma_cb) * NUM_CBS;
cb->stride = 0;
cb->next = 0;
armtick[2] = 1<<9; // Start free-running counter
dmactl[0] = BCM2708_DMA_RESET;
udelay(10); // Give it chance to reset...
dmactl[0] = BCM2708_DMA_INT | BCM2708_DMA_END;
dmactl[1] = (uint32_t)cb & 0x7fffffff; // Hmmm...
dmactl[8] = 7; // clear debug error flags
dmactl[0] = 0x00ff0001; // go, max priority
mdelay(100);
min = 0xffffffff;
max = 0;
histogram = (int *)vmalloc(sizeof(int) * 250); // 250 * 4ns = 1us...
memset(histogram, 0, sizeof(int)*250);
for (i = 0; i < (((0x1000 << BUF_ORDER) - sizeof(struct bcm2708_dma_cb) * NUM_CBS) >> 2) - 1; i++) {
uint32_t d = buffer[i+1] - buffer[i];
if (d < min)
min = d;
if (d > max)
max = d;
if (d > 100)
printk(KERN_INFO "found %d at index %d\n", d, i);
if (d >= 250)
d = 249;
histogram[d]++;
}
printk(KERN_INFO "min %d, max %d\n", min, max);
for (i = min; i <= max && i < 250; i++)
printk(KERN_INFO "%4d: %6d\n", i, histogram[i]);
vfree(histogram);
}
if (*f_pos >= sizeof(panctl_t) + panctl.num_samples * sizeof(uint32_t))
return 0;
if (*f_pos < sizeof(panctl_t)) {
count = sizeof(panctl_t) - *f_pos;
// printk(KERN_INFO "READ: returning %x bytes from %p\n", count, (char *)&panctl + *f_pos);
if (copy_to_user(buf, (char *)&panctl + *f_pos, count))
return -EFAULT;
}
else {
char *start = (char *)buffer;
char *data_start = start + first_data_index * sizeof(uint32_t);
char *end = start + panctl.num_samples * sizeof(uint32_t);
int index = *f_pos - sizeof(panctl);
char *p = data_start + index;
if (p >= end) {
p -= end - start;
if (count > data_start - p)
count = data_start - p;
} else if (count > end - p) {
count = end - p;
}
// printk(KERN_INFO "READ: returning %x bytes from %p\n", count, (char *)buffer + *f_pos);
if(copy_to_user(buf, p, count))
return -EFAULT;
}
*f_pos += count;
return count;
}
static ssize_t dev_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos)
{
if (*f_pos >= sizeof(panctl))
return 0;
count = sizeof(panctl) - *f_pos;
if (copy_from_user((char *)&panctl + *f_pos, buf, count))
return -EFAULT;
if (panctl.magic != PAN_MAGIC)
return -EINVAL;
*f_pos += count;
return count;
}
static loff_t dev_llseek(struct file *filp, loff_t off, int whence)
{
if (whence == SEEK_SET && off >= 0 && off < sizeof(panctl) + panctl.num_samples * sizeof(uint32_t)) {
filp->f_pos = off;
return off;
} else {
return -EINVAL;
}
}
static int dev_close(struct inode *inod,struct file *fil)
{
free_pages((unsigned long)buffer, BUF_ORDER);
buffer = NULL;
return 0;
}
MODULE_DESCRIPTION("Panalyzer, a RaspberryPi based Logic Analyzer");
MODULE_AUTHOR("Richard Hirst <richardghirst@gmail.com>");
MODULE_LICENSE("GPL v2");