Mercurial > ~darius > hgwebdir.cgi > hwmon
annotate superio/superio.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 |
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 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 usage(char *name) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 fprintf(stderr, |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
13 "Bad usage\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 "\t%s [-b base] [-d dev] [-t type] adr [data]\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
15 "\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
16 "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
|
17 "base is the base address for the chip (default: 0x2e)\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
18 "dev is the sub-device to talk to (default: -1/global)\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
19 "type is the chip type (i = ITE, w = WinBond)\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
20 "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
|
21 "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
|
22 exit(1); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
23 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
24 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
25 int |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 main(int argc, char **argv) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
27 int fd, base, dev, type, adr, data, ch; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 char *progname; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
30 type = 'w'; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
31 base = 0x2e; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
32 dev = -1; |
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 progname = argv[0]; |
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 while ((ch = getopt(argc, argv, "b:d:t:")) != -1) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
37 switch (ch) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 case 'b': |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 base = strtol(optarg, NULL, 0) & 0xffff; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
40 break; |
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 case 'd': |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
43 dev = strtol(optarg, NULL, 0) & 0xff; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
44 break; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
45 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
46 case 't': |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 if (optarg[0] != 'w' && optarg[0] != 'i') { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
48 fprintf(stderr, "type must be w or i\n\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
49 usage(progname); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
50 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
51 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
52 type = optarg[0]; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
53 break; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
55 default: |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
56 usage(progname); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
57 break; |
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 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 argc -= optind; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 argv += optind; |
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 if (argc != 1 && argc != 2) |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
64 usage(progname); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
65 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
66 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
|
67 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
|
68 exit(1); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
69 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
70 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
71 adr = strtol(argv[0], NULL, 0) & 0xff; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
73 /* Enter extended function mode */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
74 if (type == 'w') { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
75 outb(base, 0x87); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
76 outb(base, 0x87); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
77 } else { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
78 /* Taken from the Linux driver drivers/hwmon/it87.c */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 outb(base, 0x87); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
80 outb(base, 0x01); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
81 outb(base, 0x55); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
82 outb(base, 0x55); |
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 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
85 /* Sub device to select (skip if global) */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
86 if (dev != -1) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
87 outb(base, 0x07); /* Logical device select register */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
88 outb(base + 1, dev & 0xff); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
89 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
90 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
91 /* Register to access */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
92 outb(base, adr); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
93 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
94 if (argc == 2) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
95 data = strtol(argv[1], NULL, 0) & 0xff; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
96 printf("Bank 0x%02x: 0x%02x <- 0x%02x\n", dev, adr, data); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
97 outb(base + 1, data); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
98 } else |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
99 printf("Bank 0x%02x: 0x%02x -> 0x%02x\n", dev, adr, inb(base + 1)); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
100 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
101 /* Exit extended function mode */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
102 if (type == 'w') { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
103 outb(base, 0xaa); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
104 outb(base, 0xaa); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
105 } else { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
106 outb(base, 0x02); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
107 outb(base, 0x02); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
108 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
109 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
110 exit(0); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
111 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
112 |