Mercurial > ~darius > hgwebdir.cgi > hwmon
annotate pcmon/pcmonhelper.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 #define _WITH_GETLINE |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
2 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
3 #include <stdio.h> |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
4 #include <errno.h> |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
5 #include <string.h> |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
6 #include <fcntl.h> |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
7 #include <stdlib.h> |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 #include <sys/types.h> |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
9 #include <machine/cpufunc.h> |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
10 #include <unistd.h> |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 /* Prototypes */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
13 void ec_outb(uint16_t base, uint8_t adr, uint8_t val); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 uint8_t ec_inb(uint16_t base, uint8_t adr); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
15 void ec_run(uint16_t base); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
16 void freeargs(char **args, size_t nargs); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
17 void splitline(char *line, char ***args, size_t *nargs); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
18 void superio_enter(uint16_t base, char type); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
19 void superio_exit(uint16_t base, char type); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
20 uint8_t superio_inb(uint8_t base, uint8_t adr); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
21 void superio_outb(uint16_t base, uint8_t adr, uint8_t val); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
22 void usage(char *name); |
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 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
25 usage(char *name) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 fprintf(stderr, |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
27 "Bad usage\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 "\t%s [-b base] [-t type]\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 "\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
30 "Read/write registers in the EC function of Winbond/ITE parts\n" |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
31 "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
|
32 "type is the chip type (i = ITE, w = WinBond)\n", name); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
33 exit(1); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
34 } |
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 int |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
37 main(int argc, char **argv) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 int fd, base, ch, ecbase, devid; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 char *progname, type; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
40 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
41 type = 'w'; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
42 base = 0x2e; |
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 progname = argv[0]; |
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 while ((ch = getopt(argc, argv, "b:t:")) != -1) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 switch (ch) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
48 case 'b': |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
49 base = strtol(optarg, NULL, 0) & 0xffff; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
50 break; |
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 case 't': |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
53 if (optarg[0] != 'w' && optarg[0] != 'i') { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
54 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
|
55 usage(progname); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
56 } |
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 type = optarg[0]; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 break; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 default: |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
62 usage(progname); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
63 break; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
64 } |
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 argc -= optind; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
67 argv += optind; |
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 if (argc != 0) |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
70 usage(progname); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
71 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 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
|
73 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
|
74 exit(1); |
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 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
77 /* Enter extended function mode */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
78 superio_enter(base, type); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
80 /* Check device ID */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
81 devid = superio_inb(base, 0x20) << 8 | superio_inb(base, 0x21); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
82 printf("Device ID = 0x%04x\n", devid); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
83 if (devid != 0x8720) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
84 fprintf(stderr, "Device ID mismatch\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
85 superio_exit(base, type); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
86 exit(1); |
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 /* Select EC config registers */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
90 superio_outb(base, 0x7, 4); |
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 /* Determine EC base address */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
93 ecbase = superio_inb(base, 0x60) << 8; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
94 ecbase |= superio_inb(base, 0x61); |
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 /* Exit extended function mode */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
97 superio_exit(base, type); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
98 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
99 ec_run(ecbase); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
100 exit(0); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
101 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
102 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
103 /* Enter extended function mode */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
104 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
105 superio_enter(uint16_t base, char type) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
106 if (type == 'w') { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
107 outb(base, 0x87); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
108 outb(base, 0x87); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
109 } else { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
110 outb(base, 0x87); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
111 outb(base, 0x01); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
112 outb(base, 0x55); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
113 outb(base, 0x55); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
114 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
115 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
116 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
117 /* Exit extended function mode */ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
118 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
119 superio_exit(uint16_t base, char type) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
120 if (type == 'w') { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
121 outb(base, 0xaa); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
122 outb(base, 0xaa); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
123 } else { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
124 outb(base, 0x02); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
125 outb(base, 0x02); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
126 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
127 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
128 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
129 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
130 superio_outb(uint16_t base, uint8_t adr, uint8_t val) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
131 outb(base, adr); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
132 outb(base + 1, val); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
133 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
134 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
135 uint8_t |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
136 superio_inb(uint8_t base, uint8_t adr) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
137 outb(base, adr); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
138 return(inb(base + 1)); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
139 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
140 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
141 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
142 ec_outb(uint16_t base, uint8_t adr, uint8_t val) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
143 outb(base + 5, adr); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
144 outb(base + 6, val); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
145 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
146 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
147 uint8_t |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
148 ec_inb(uint16_t base, uint8_t adr) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
149 outb(base + 5, adr); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
150 return(inb(base + 6)); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
151 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
152 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
153 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
154 ec_run(uint16_t base) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
155 char *line = NULL, **args = NULL, *p; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
156 size_t llen = 0, nargs; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
157 long tmp; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
158 uint8_t adr, val; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
159 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
160 printf("EC base address = 0x%04x\n", base); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
161 printf("EC Vendor ID = 0x%02x\n", ec_inb(base, 0x58)); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
162 printf("EC Core ID = 0x%02x\n", ec_inb(base, 0x5b)); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
163 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
164 while (1) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
165 if (getline(&line, &llen, stdin) == -1) |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
166 break; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
167 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
168 /* Snip trailing new line*/ |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
169 *rindex(line, '\n')= '\0'; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
170 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
171 splitline(line, &args, &nargs); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
172 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
173 if (nargs != 2 && nargs != 3) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
174 printf("Unable to parse line\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
175 continue; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
176 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
177 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
178 tmp = strtol(args[1], &p, 0); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
179 if (p == args[1]) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
180 printf("Unable to parse address\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
181 continue; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
182 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
183 if (tmp < 0 || tmp > 255) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
184 printf("Address out of range\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
185 continue; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
186 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
187 adr = tmp; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
188 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
189 if (!strcmp("read", args[0])) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
190 if (nargs != 2) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
191 printf("Bad usage of read command\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
192 continue; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
193 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
194 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
195 printf("0x%02x -> 0x%02x\n", adr, ec_inb(base, adr)); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
196 } else if (!strcmp("write", args[0])) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
197 if (nargs != 3) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
198 printf("Bad usage of write command\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
199 continue; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
200 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
201 tmp = strtol(args[2], &p, 0); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
202 if (p == args[1]) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
203 printf("Unable to parse value\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
204 continue; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
205 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
206 if (tmp < 0 || tmp > 255) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
207 printf("Value out of range\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
208 continue; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
209 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
210 val = tmp; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
211 ec_outb(base, adr, val); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
212 printf("0x%02x <- 0x%02x\n", adr, val); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
213 } else { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
214 printf("Unable to parse line\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
215 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
216 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
217 free(line); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
218 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
219 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
220 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
221 splitline(char *line, char ***args, size_t *nargs) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
222 char *token; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
223 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
224 freeargs(*args, *nargs); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
225 *args = NULL; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
226 *nargs = 0; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
227 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
228 while ((token = strsep(&line, " ")) != NULL) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
229 *args = realloc(*args, sizeof(char *) * (*nargs + 1)); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
230 if (args == NULL) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
231 fprintf(stderr, "Unable to allocate memory\n"); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
232 exit(1); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
233 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
234 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
235 (*args)[*nargs] = malloc(strlen(token) + 1); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
236 strcpy((*args)[*nargs], token); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
237 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
238 (*nargs)++; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
239 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
240 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
241 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
242 void |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
243 freeargs(char **args, size_t nargs) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
244 int i; |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
245 |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
246 if (args != NULL) { |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
247 for (i = 0; i < nargs; i++) |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
248 free(args[i]); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
249 free(args); |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
250 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
251 } |
c34b37680055
Inital commit of random SuperIO code.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
252 |