Superiotool: Add dump support to the Winbond W83697HF/F.
[coreboot.git] / util / superiotool / superiotool.c
1 /*
2  * This file is part of the LinuxBIOS project.
3  *
4  * Copyright (C) 2006 Ronald Minnich <rminnich@gmail.com>
5  * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
6  * Copyright (C) 2007 Carl-Daniel Hailfinger
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21  */
22
23 #include "superiotool.h"
24
25 /* Command line options. */
26 int dump = 0, verbose = 0;
27
28 uint8_t regval(uint16_t port, uint8_t reg)
29 {
30         outb(reg, port);
31         return inb(port + 1);
32 }
33
34 void regwrite(uint16_t port, uint8_t reg, uint8_t val)
35 {
36         outb(reg, port);
37         outb(val, port + 1);
38 }
39
40 int superio_unknown(const struct superio_registers reg_table[], uint16_t id)
41 {
42         return !strncmp(get_superio_name(reg_table, id), "<unknown>", 9);
43 }
44
45 const char *get_superio_name(const struct superio_registers reg_table[],
46                              uint16_t id)
47 {
48         int i;
49
50         for (i = 0; /* Nothing */; i++) {
51                 if (reg_table[i].superio_id == EOT)
52                         break;
53
54                 if ((uint16_t)reg_table[i].superio_id != id)
55                         continue;
56
57                 return reg_table[i].name;
58         }
59
60         return "<unknown>";
61 }
62
63 static void dump_regs(const struct superio_registers reg_table[],
64                       int i, int j, uint16_t port)
65 {
66         int k, *idx;
67
68         if (reg_table[i].ldn[j].ldn != NOLDN) {
69                 printf("LDN 0x%02x ", reg_table[i].ldn[j].ldn);
70                 if (reg_table[i].ldn[j].name != NULL)
71                         printf("(%s)", reg_table[i].ldn[j].name);
72                 regwrite(port, 0x07, reg_table[i].ldn[j].ldn);
73         } else {
74                 printf("Register dump:");
75         }
76
77         idx = reg_table[i].ldn[j].idx;
78
79         printf("\nidx ");
80         for (k = 0; /* Nothing */; k++) {
81                 if (idx[k] == EOT)
82                         break;
83                 printf("%02x ", idx[k]);
84         }
85
86         printf("\nval ");
87         for (k = 0; /* Nothing */; k++) {
88                 if (idx[k] == EOT)
89                         break;
90                 printf("%02x ", regval(port, idx[k]));
91         }
92
93         printf("\ndef ");
94         idx = reg_table[i].ldn[j].def;
95         for (k = 0; /* Nothing */; k++) {
96                 if (idx[k] == EOT)
97                         break;
98                 else if (idx[k] == NANA)
99                         printf("NA ");
100                 else if (idx[k] == RSVD)
101                         printf("RR ");
102                 else if (idx[k] == MISC)        /* TODO */
103                         printf("MM ");
104                 else
105                         printf("%02x ", idx[k]);
106         }
107         printf("\n");
108 }
109
110 void dump_superio(const char *vendor,
111                   const struct superio_registers reg_table[],
112                   uint16_t port, uint16_t id)
113 {
114         int i, j, no_dump_available = 1;
115
116         if (!dump)
117                 return;
118
119         for (i = 0; /* Nothing */; i++) {
120                 if (reg_table[i].superio_id == EOT)
121                         break;
122
123                 if ((uint16_t)reg_table[i].superio_id != id)
124                         continue;
125
126                 for (j = 0; /* Nothing */; j++) {
127                         if (reg_table[i].ldn[j].ldn == EOT)
128                                 break;
129                         no_dump_available = 0;
130                         dump_regs(reg_table, i, j, port);
131                 }
132
133                 if (no_dump_available)
134                         printf("No dump available for this Super I/O\n");
135         }
136 }
137
138 void no_superio_found(uint16_t port)
139 {
140         if (!verbose)
141                 return;
142
143         if (inb(port) == 0xff)
144                 printf("No Super I/O chip found at 0x%04x\n", port);
145         else
146                 printf("Probing 0x%04x, failed (0x%02x), data returns 0x%02x\n",
147                        port, inb(port), inb(port + 1));
148 }
149
150 int main(int argc, char *argv[])
151 {
152         int i, j, opt, option_index;
153
154         const static struct option long_options[] = {
155                 {"dump",        no_argument, NULL, 'd'},
156                 {"verbose",     no_argument, NULL, 'V'},
157                 {"version",     no_argument, NULL, 'v'},
158                 {"help",        no_argument, NULL, 'h'},
159                 {0, 0, 0, 0}
160         };
161
162         while ((opt = getopt_long(argc, argv, "dVvh",
163                                   long_options, &option_index)) != EOF) {
164                 switch (opt) {
165                 case 'd':
166                         dump = 1;
167                         break;
168                 case 'V':
169                         verbose = 1;
170                         break;
171                 case 'v':
172                         printf("superiotool %s\n", SUPERIOTOOL_VERSION);
173                         exit(0);
174                         break;
175                 case 'h':
176                         printf(USAGE);
177                         exit(0);
178                         break;
179                 default:
180                         /* Unknown option. */
181                         exit(1);
182                         break;
183                 }
184         }
185
186         if (iopl(3) < 0) {
187                 perror("iopl");
188                 printf("Superiotool must be run as root.\n");
189                 exit(1);
190         }
191
192         for (i = 0; i < ARRAY_SIZE(superio_ports_table); i++) {
193                 for (j = 0; superio_ports_table[i].ports[j] != EOT; j++)
194                         superio_ports_table[i].probe_idregs(
195                                 superio_ports_table[i].ports[j]);
196         }
197
198         return 0;
199 }