annotate wbread/wbread.c @ 0:c34b37680055 default tip

Inital commit of random SuperIO code.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 20 Oct 2011 16:48:24 +1030
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 #include <stdio.h>
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2 #include <errno.h>
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 #include <string.h>
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 #include <fcntl.h>
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 #include <stdlib.h>
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 #include <sys/types.h>
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 #include <machine/cpufunc.h>
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8 #include <unistd.h>
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 #define VPERLSB 0.008
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 #define MAXDIG (VPERLSB * 256)
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 #define WB_ADR 0x5
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 #define WB_DATA 0x6
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 void
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 usage(char *name) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 fprintf(stderr,
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 "Bad usage\n"
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 "\t%s [-b base]\n"
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 "\n"
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 "Read/write registers in Winbond SuperIO parts\n"
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 "base is the base address for the chip (default: 0x290)\n"
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 "adr is the register to read/write\n"
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 "data is the data to write to adr\n", name);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26 exit(1);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29 int
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 readwb(int base, int adr) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 /* Register to access */
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 outb(base + WB_ADR, adr);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 return(inb(base + WB_DATA));
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 void
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 writewb(int base, int adr, int val) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 /* Register to access */
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 outb(base + WB_ADR, adr);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 outb(base + WB_DATA, val);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 float
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 raw2volt(int val, float r1, float r2) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 float digval, result;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 digval = val * VPERLSB;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 result = digval * ((r1 + r2) / r2);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51 return(result);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 float
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55 raw2negvolt(int val, float r1, float r2) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 return((val * 0.008 - 2.048)/(r2 / (r1 + r2))) + 2.048;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 float
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 raw2rpm(int val, int divisor) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 return(1360000 / (float)(val) / divisor);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 int
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 raw2divisor(int val) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 return(1 << val);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 int
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 main(int argc, char **argv) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 int fd, base, ch, tmp;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 char *progname;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 base = 0x290;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 progname = argv[0];
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 while ((ch = getopt(argc, argv, "b:")) != -1) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 switch (ch) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 case 'b':
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 base = strtol(optarg, NULL, 0) & 0xffff;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 break;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 default:
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 usage(progname);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 break;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 argc -= optind;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 argv += optind;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 if ((fd = open("/dev/io", O_RDWR)) == -1) {
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 fprintf(stderr, "Can't open /dev/io: %s\n", strerror(errno));
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94 exit(1);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97 printf("3V3 = %.2f V\n", raw2volt(readwb(base, 0x23), 34000, 34000));
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 printf("-12V = %.2f V\n", raw2negvolt(readwb(base, 0x26), 232000, 10000));
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 tmp = (readwb(base, 0x47) & 0xc0) >> 6;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100 tmp |= (readwb(base, 0x5d) & 0x40) >> 4;
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 tmp = raw2divisor(tmp);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102 printf("CPU fan = %.0f rpm (divisor %d)\n", raw2rpm(readwb(base, 0x29), tmp), tmp);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 exit(0);
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104 }
c34b37680055 Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105