summaryrefslogtreecommitdiff
path: root/src/hal/simdrivers/ppioctl.c
blob: 51e618931dfcabe7355d3837eed02ad54934e1c2 (plain)
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
// test program to time ppdev filedescriptor I/O versus
// direct register writes
// compile like so:
//
// gcc -O5 -g -Wall ppioctl.c  -oppioctl
//
// results: Intel(R) Atom(TM) CPU D525   @ 1.80GHz
// ioctl-based parport write: 2.8uSec/write
// direct register access: 1.8usec/write
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/io.h>

#include <string.h>
#include <linux/ppdev.h> 
#include <linux/parport.h>

typedef unsigned long long u64;
typedef unsigned long      u32;

#define N_ITER 10000

struct ppres  {
    unsigned int state;
    unsigned int reg_start;
    unsigned int reg_end;
    unsigned int irq;
};

static inline u64 rdtsc(void)
{
    if (sizeof(long) == sizeof(u64)) {
	u32 lo, hi;
	asm volatile("rdtsc" : "=a" (lo), "=d" (hi));
	return ((u64)(hi) << 32) | lo;
    }
    else {
	u64 tsc;
	asm volatile("rdtsc" : "=A" (tsc));
	return tsc;
    }
}

int get_ppdev_res(int dev, struct ppres *ppres)
{
	FILE *f;
	char path[1024], value[100],line[1024], *s;
	unsigned from, to;

	// parse this:
	// $ cat /sys/class/ppdev/parport0/device/resources 
	// state = active
	// io 0x378-0x37f
	// irq 7
	ppres->state = 0;
	ppres->reg_start = 0;
	ppres->reg_end = 0;
	ppres->irq = 0;

	sprintf(path,"/sys/class/ppdev/parport%d/device/resources",dev);

	f = fopen(path, "r");
	if (!f) {
	    perror(path);
	    return -1;
	}
	while (fgets(line, sizeof(line), f)) {
	    if (!strncmp(line, "state", 5)) {
		s = strchr(line, '=');
		if (s && 1 == sscanf(s, "= %s", value)) {
		    ppres->state = !strcmp(value,"active");
		} else  {
		    fprintf(stderr, "get_ppdev_res: cant parse '%s'\n",
			    line);
		    fclose(f);
		    return -1;
		}
	    }
	    if (!strncmp(line, "irq", 3)) {
		if (1 != sscanf(line+4, " %d", &ppres->irq)) {
		    fprintf(stderr, "get_ppdev_res: cant parse '%s'\n",
			    line);
		    fclose(f);
		    return -1;
		}
	    }
	    if (!strncmp(line, "io", 2)) {
		if (2 != sscanf(line+2, " 0x%x-0x%x", &from, &to)) {
		    fprintf(stderr, "get_ppdev_res: cant parse '%s'\n",
			    line);
		    fclose(f);
		    return -1;
		    
		} else {
		    ppres->reg_start = from;
		    ppres->reg_end = to;
		}
	    }
	}
	fclose(f);
	return 0;
}

float cpu_MHz(void) 
{
    char *path = "/proc/cpuinfo",  *s, line[1024];
    float freq;
    char *cpu_line = "cpu MHz";

    // parse /proc/cpuinfo for the line:
    // cpu MHz		: 2378.041
    FILE *f = fopen(path,"r");
    if (!f) {
	perror(path);
	return -1.0;
    }
    while (fgets(line, sizeof(line), f)) {
	if (!strncmp(line, cpu_line, strlen(cpu_line))) {
	    s = strchr(line, ':');
	    if (s && 1 == sscanf(s, ":%g", &freq)) {
		fclose(f);
		return freq;
	    }
	}
    }
    fclose(f);
    return -1.0;
}


int main(int argc, char *argv[])
{
    int i,result;
    unsigned char ctrl,data, status;
    int mode;
    int parportfd = 0;
    u64 start;
    unsigned int lap;
    double time;
    struct ppres ppres;
    char path[1024];
    int dev = 0;
    int time_ioctl = 1;
    float mhz = cpu_MHz();
    if (mhz < 0.0) {
	fprintf(stderr, "cant get CPU frequency from /proc/cpuinfo\n");
	exit (1);
    }
    printf("mhz = %g\n",mhz);

    if (argc > 1) {
	dev = atoi(argv[1]);
    }
    if (argc >  2) {
	time_ioctl = 0;
    }
    sprintf(path, "/dev/parport%d", dev);

    parportfd = open(path, O_RDWR);  
    if (parportfd > 0) 
	result = ioctl(parportfd, PPCLAIM);
    else {
	perror(path);
	exit(1);
    }
    ioctl(parportfd, PPGETMODES, &mode);
    printf("modes=0x%x\n", mode);

    if (get_ppdev_res(dev, &ppres) < 0) {
	exit(1);
    } else 
	printf("%s: io=0x%x-0x%x irq=%d state=%d\n", 
	       path, ppres.reg_start, 
	       ppres.reg_end, ppres.irq,ppres.state);

    ioctl( parportfd, PPRCONTROL, &ctrl );
    ioctl( parportfd, PPRSTATUS , &data );
    ioctl( parportfd, PPRDATA   , &status );
    
    printf("control=0x%x data=0x%x status=0x%x\n", ctrl, data, status);
   
    if (time_ioctl) { 
	lap = 0;
	for (i = 0; i < N_ITER; i++) {
	    if (data == 0) 
		data = 1;
	    start = rdtsc();
	    ioctl(parportfd, PPWDATA, &data);
	    lap += rdtsc() - start;
	    data = data << 1;
	}
	time = (double) lap;
	time /= ((double) mhz * (double)N_ITER) ;
	printf("time=%g uS/ioctl() write\n",time);

    } else { // timing with raw register I/O
	if (ioperm(ppres.reg_start, 3, 1) < 0) {
	    perror("ioperm access");
	    exit(1);
	}
	lap = 0;
	for (i = 0; i < N_ITER; i++) {
	    if (data == 0) 
		data = 1;
	    start = rdtsc();
	    outb(data, ppres.reg_start);  
	    lap += rdtsc() - start;
	    data = data << 1;
	}
	time = (double) lap;
	time /= ((double) mhz * (double)N_ITER) ;
	printf("direct register write: time=%g uS/write()\n",time);

	if (ioperm(ppres.reg_start, 3, 0) < 0) {  //Release the IO ports
	    perror("ioperm release");  
	}
    }
    result = ioctl(parportfd,PPRELEASE);
    close(parportfd);
    return result;
}