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