blob: 6c93f58c6babc20598c0ad85285a7848f6095a06 [file] [log] [blame]
Stefan Reinauer6a5bc462007-01-17 10:57:42 +00001/*
Uwe Hermannafe83092007-09-28 15:45:43 +00002 * This file is part of the superiotool project.
Stefan Reinauer6a5bc462007-01-17 10:57:42 +00003 *
4 * Copyright (C) 2006 Ronald Minnich <rminnich@gmail.com>
Uwe Hermannbd263922007-09-01 19:42:42 +00005 * Copyright (C) 2007 Uwe Hermann <uwe@hermann-uwe.de>
Uwe Hermann519419b2007-09-16 20:59:01 +00006 * Copyright (C) 2007 Carl-Daniel Hailfinger
Stefan Reinauer6a5bc462007-01-17 10:57:42 +00007 *
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
Uwe Hermann0120e1a2007-09-16 18:11:03 +000023#include "superiotool.h"
Ronald G. Minnich394e7c42006-02-22 22:12:21 +000024
Uwe Hermann3acf31e2007-09-19 01:55:35 +000025/* Command line options. */
Uwe Hermanneddc4732007-09-20 23:57:44 +000026int dump = 0, dump_readable = 0, verbose = 0;
Uwe Hermann3acf31e2007-09-19 01:55:35 +000027
Uwe Hermanne9d46162007-10-07 20:01:23 +000028/* Global flag which indicates whether a chip was detected at all. */
29int chip_found = 0;
30
Uwe Hermannde24a0e2007-09-19 00:03:14 +000031uint8_t regval(uint16_t port, uint8_t reg)
Uwe Hermann2046ff92007-09-01 21:02:44 +000032{
Ronald G. Minnich394e7c42006-02-22 22:12:21 +000033 outb(reg, port);
Carl-Daniel Hailfingerb1786c22007-08-28 10:43:57 +000034 return inb(port + 1);
Ronald G. Minnich394e7c42006-02-22 22:12:21 +000035}
36
Uwe Hermannde24a0e2007-09-19 00:03:14 +000037void regwrite(uint16_t port, uint8_t reg, uint8_t val)
Uwe Hermann2046ff92007-09-01 21:02:44 +000038{
Carl-Daniel Hailfinger7a7890a2007-08-27 07:28:28 +000039 outb(reg, port);
Carl-Daniel Hailfingerb1786c22007-08-28 10:43:57 +000040 outb(val, port + 1);
Carl-Daniel Hailfinger7a7890a2007-08-27 07:28:28 +000041}
42
Uwe Hermannb4db2202007-09-20 23:37:56 +000043void enter_conf_mode_winbond_fintek_ite_8787(uint16_t port)
44{
45 outb(0x87, port);
46 outb(0x87, port);
47}
48
49void exit_conf_mode_winbond_fintek_ite_8787(uint16_t port)
50{
51 outb(0xaa, port); /* Fintek, Winbond */
52 regwrite(port, 0x02, 0x02); /* ITE */
53}
54
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +000055int superio_unknown(const struct superio_registers reg_table[], uint16_t id)
56{
57 return !strncmp(get_superio_name(reg_table, id), "<unknown>", 9);
58}
59
Uwe Hermann3acf31e2007-09-19 01:55:35 +000060const char *get_superio_name(const struct superio_registers reg_table[],
61 uint16_t id)
62{
63 int i;
64
65 for (i = 0; /* Nothing */; i++) {
66 if (reg_table[i].superio_id == EOT)
67 break;
68
69 if ((uint16_t)reg_table[i].superio_id != id)
70 continue;
71
72 return reg_table[i].name;
73 }
74
75 return "<unknown>";
76}
77
Uwe Hermann2c290e32007-09-20 00:00:49 +000078static void dump_regs(const struct superio_registers reg_table[],
79 int i, int j, uint16_t port)
80{
Ward Vandewege2ee78d82007-09-24 22:02:31 +000081 int k;
82 const int *idx;
Uwe Hermann2c290e32007-09-20 00:00:49 +000083
84 if (reg_table[i].ldn[j].ldn != NOLDN) {
85 printf("LDN 0x%02x ", reg_table[i].ldn[j].ldn);
86 if (reg_table[i].ldn[j].name != NULL)
87 printf("(%s)", reg_table[i].ldn[j].name);
88 regwrite(port, 0x07, reg_table[i].ldn[j].ldn);
89 } else {
90 printf("Register dump:");
91 }
92
93 idx = reg_table[i].ldn[j].idx;
94
95 printf("\nidx ");
96 for (k = 0; /* Nothing */; k++) {
97 if (idx[k] == EOT)
98 break;
99 printf("%02x ", idx[k]);
100 }
101
102 printf("\nval ");
103 for (k = 0; /* Nothing */; k++) {
104 if (idx[k] == EOT)
105 break;
106 printf("%02x ", regval(port, idx[k]));
107 }
108
109 printf("\ndef ");
110 idx = reg_table[i].ldn[j].def;
111 for (k = 0; /* Nothing */; k++) {
112 if (idx[k] == EOT)
113 break;
114 else if (idx[k] == NANA)
115 printf("NA ");
116 else if (idx[k] == RSVD)
117 printf("RR ");
118 else if (idx[k] == MISC) /* TODO */
119 printf("MM ");
120 else
121 printf("%02x ", idx[k]);
122 }
123 printf("\n");
124}
125
126void dump_superio(const char *vendor,
127 const struct superio_registers reg_table[],
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000128 uint16_t port, uint16_t id)
Uwe Hermann519419b2007-09-16 20:59:01 +0000129{
Uwe Hermann2c290e32007-09-20 00:00:49 +0000130 int i, j, no_dump_available = 1;
Uwe Hermann519419b2007-09-16 20:59:01 +0000131
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000132 if (!dump)
133 return;
134
Uwe Hermann519419b2007-09-16 20:59:01 +0000135 for (i = 0; /* Nothing */; i++) {
136 if (reg_table[i].superio_id == EOT)
137 break;
138
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000139 if ((uint16_t)reg_table[i].superio_id != id)
Uwe Hermann519419b2007-09-16 20:59:01 +0000140 continue;
141
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000142 for (j = 0; /* Nothing */; j++) {
Uwe Hermann519419b2007-09-16 20:59:01 +0000143 if (reg_table[i].ldn[j].ldn == EOT)
144 break;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000145 no_dump_available = 0;
146 dump_regs(reg_table, i, j, port);
Uwe Hermann519419b2007-09-16 20:59:01 +0000147 }
Uwe Hermann25a6c0f2007-09-19 00:48:42 +0000148
Uwe Hermann2c290e32007-09-20 00:00:49 +0000149 if (no_dump_available)
150 printf("No dump available for this Super I/O\n");
Uwe Hermann519419b2007-09-16 20:59:01 +0000151 }
152}
153
Uwe Hermanneddc4732007-09-20 23:57:44 +0000154void dump_superio_readable(uint16_t port)
155{
156 /* TODO */
157 if (dump_readable)
158 printf("No human-readable dump available for this Super I/O\n");
159}
160
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000161void probing_for(const char *vendor, const char *info, uint16_t port)
Uwe Hermann2c290e32007-09-20 00:00:49 +0000162{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000163 if (!verbose)
164 return;
165
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000166 /* Yes, there's no space between '%s' and 'at'! */
167 printf("Probing for %s Super I/O %sat 0x%x...\n",
168 vendor, info, port);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000169}
170
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000171static void print_version(void)
172{
173 char tmp[80];
174
175 strncpy((char *)&tmp,
176 (const char *)&SUPERIOTOOL_VERSION[6],
177 strlen(SUPERIOTOOL_VERSION) - 8);
178 printf("superiotool r%s\n", (char *)&tmp);
179}
180
Uwe Hermann2046ff92007-09-01 21:02:44 +0000181int main(int argc, char *argv[])
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000182{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000183 int i, j, opt, option_index;
184
185 const static struct option long_options[] = {
Uwe Hermanneddc4732007-09-20 23:57:44 +0000186 {"dump", no_argument, NULL, 'd'},
187 {"dump-readable", no_argument, NULL, 'D'},
188 {"verbose", no_argument, NULL, 'V'},
189 {"version", no_argument, NULL, 'v'},
190 {"help", no_argument, NULL, 'h'},
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000191 {0, 0, 0, 0}
192 };
193
Uwe Hermanneddc4732007-09-20 23:57:44 +0000194 while ((opt = getopt_long(argc, argv, "dDVvh",
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000195 long_options, &option_index)) != EOF) {
196 switch (opt) {
197 case 'd':
198 dump = 1;
199 break;
Uwe Hermanneddc4732007-09-20 23:57:44 +0000200 case 'D':
201 dump_readable = 1;
202 break;
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000203 case 'V':
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000204 /* Print version in --verbose mode. */
205 print_version();
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000206 verbose = 1;
207 break;
208 case 'v':
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000209 print_version();
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000210 exit(0);
211 break;
212 case 'h':
Uwe Hermanne4749562007-09-19 16:26:18 +0000213 printf(USAGE);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000214 exit(0);
215 break;
216 default:
217 /* Unknown option. */
218 exit(1);
219 break;
220 }
221 }
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000222
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000223 if (iopl(3) < 0) {
224 perror("iopl");
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000225 printf("Superiotool must be run as root.\n");
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000226 exit(1);
227 }
228
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000229 for (i = 0; i < ARRAY_SIZE(superio_ports_table); i++) {
230 for (j = 0; superio_ports_table[i].ports[j] != EOT; j++)
231 superio_ports_table[i].probe_idregs(
232 superio_ports_table[i].ports[j]);
233 }
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000234
Uwe Hermanne9d46162007-10-07 20:01:23 +0000235 if (!chip_found)
236 printf("No Super I/O found\n");
237
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000238 return 0;
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000239}