blob: aee502618974453d6c085f99b514b4beb4c71a63 [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Stefan Reinauer6a5bc462007-01-17 10:57:42 +00002
Uwe Hermann0120e1a2007-09-16 18:11:03 +00003#include "superiotool.h"
Ronald G. Minnich394e7c42006-02-22 22:12:21 +00004
Andriy Gaponb64aa602008-10-28 22:13:38 +00005#if defined(__FreeBSD__)
6#include <fcntl.h>
7#include <unistd.h>
8#endif
9
Uwe Hermann3acf31e2007-09-19 01:55:35 +000010/* Command line options. */
Nico Huber3812a722016-10-12 12:12:51 +020011int dump = 0, verbose = 0, extra_dump = 0, alternate_dump = 0;
Uwe Hermann3acf31e2007-09-19 01:55:35 +000012
Uwe Hermanne9d46162007-10-07 20:01:23 +000013/* Global flag which indicates whether a chip was detected at all. */
14int chip_found = 0;
15
Guenter Roecka89da092012-06-29 12:23:50 -070016static void set_bank(uint16_t port, uint8_t bank)
17{
18 OUTB(0x4E, port);
19 OUTB(bank, port + 1);
20}
21
22static uint8_t datareg(uint16_t port, uint8_t reg)
23{
24 OUTB(reg, port);
25 return INB(port + 1);
26}
27
Uwe Hermannde24a0e2007-09-19 00:03:14 +000028uint8_t regval(uint16_t port, uint8_t reg)
Uwe Hermann2046ff92007-09-01 21:02:44 +000029{
Andriy Gaponb64aa602008-10-28 22:13:38 +000030 OUTB(reg, port);
31 return INB(port + ((port == 0x3bd) ? 2 : 1)); /* 0x3bd is special. */
Ronald G. Minnich394e7c42006-02-22 22:12:21 +000032}
33
Uwe Hermannde24a0e2007-09-19 00:03:14 +000034void regwrite(uint16_t port, uint8_t reg, uint8_t val)
Uwe Hermann2046ff92007-09-01 21:02:44 +000035{
Andriy Gaponb64aa602008-10-28 22:13:38 +000036 OUTB(reg, port);
37 OUTB(val, port + 1);
Carl-Daniel Hailfinger7a7890a2007-08-27 07:28:28 +000038}
39
Uwe Hermannb4db2202007-09-20 23:37:56 +000040void enter_conf_mode_winbond_fintek_ite_8787(uint16_t port)
41{
Andriy Gaponb64aa602008-10-28 22:13:38 +000042 OUTB(0x87, port);
43 OUTB(0x87, port);
Uwe Hermannb4db2202007-09-20 23:37:56 +000044}
45
46void exit_conf_mode_winbond_fintek_ite_8787(uint16_t port)
47{
Andriy Gaponb64aa602008-10-28 22:13:38 +000048 OUTB(0xaa, port); /* Fintek, Winbond */
Uwe Hermannb4db2202007-09-20 23:37:56 +000049 regwrite(port, 0x02, 0x02); /* ITE */
50}
51
Stefan Reinauere7b7ae22010-08-17 08:24:01 +000052void enter_conf_mode_fintek_7777(uint16_t port)
53{
54 OUTB(0x77, port);
55 OUTB(0x77, port);
56}
57
58void exit_conf_mode_fintek_7777(uint16_t port)
59{
60 OUTB(0xaa, port); /* Fintek */
61}
62
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +000063int superio_unknown(const struct superio_registers reg_table[], uint16_t id)
64{
65 return !strncmp(get_superio_name(reg_table, id), "<unknown>", 9);
66}
67
Stefan Reinauere7b7ae22010-08-17 08:24:01 +000068
Uwe Hermann3acf31e2007-09-19 01:55:35 +000069const char *get_superio_name(const struct superio_registers reg_table[],
70 uint16_t id)
71{
72 int i;
73
74 for (i = 0; /* Nothing */; i++) {
75 if (reg_table[i].superio_id == EOT)
76 break;
77
78 if ((uint16_t)reg_table[i].superio_id != id)
79 continue;
80
81 return reg_table[i].name;
82 }
83
84 return "<unknown>";
85}
86
Uwe Hermann2c290e32007-09-20 00:00:49 +000087static void dump_regs(const struct superio_registers reg_table[],
Stefan Reinauer7a51e502008-12-01 14:18:57 +000088 int i, int j, uint16_t port, uint8_t ldn_sel)
Uwe Hermann2c290e32007-09-20 00:00:49 +000089{
Ward Vandewege2ee78d82007-09-24 22:02:31 +000090 int k;
Nico Huber3812a722016-10-12 12:12:51 +020091 const int16_t *idx, *def;
Uwe Hermann2c290e32007-09-20 00:00:49 +000092
93 if (reg_table[i].ldn[j].ldn != NOLDN) {
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +000094 printf("LDN 0x%02x", reg_table[i].ldn[j].ldn);
Uwe Hermann2c290e32007-09-20 00:00:49 +000095 if (reg_table[i].ldn[j].name != NULL)
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +000096 printf(" (%s)", reg_table[i].ldn[j].name);
Stefan Reinauer7a51e502008-12-01 14:18:57 +000097 regwrite(port, ldn_sel, reg_table[i].ldn[j].ldn);
Uwe Hermann2c290e32007-09-20 00:00:49 +000098 } else {
Anders Juel Jensen4fd7df92010-08-22 19:40:11 +000099 if (reg_table[i].ldn[j].name == NULL)
100 printf("Register dump:");
101 else
102 printf("(%s)", reg_table[i].ldn[j].name);
Uwe Hermann2c290e32007-09-20 00:00:49 +0000103 }
104
105 idx = reg_table[i].ldn[j].idx;
Nico Huber3812a722016-10-12 12:12:51 +0200106 def = reg_table[i].ldn[j].def;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000107
Nico Huber3812a722016-10-12 12:12:51 +0200108 if (alternate_dump) {
109 int skip_def = 0;
Matt DeVilliere9786d42024-01-15 18:11:43 -0600110 int val;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000111
Matt DeVilliere9786d42024-01-15 18:11:43 -0600112 printf("\nidx def val\n");
Uwe Hermann2c290e32007-09-20 00:00:49 +0000113
Nico Huber3812a722016-10-12 12:12:51 +0200114 for (k = 0; idx[k] != EOT; k++) {
Nico Huber3812a722016-10-12 12:12:51 +0200115 if (skip_def || def[k] == EOT) {
116 skip_def = 1;
117 printf("\n");
118 continue;
119 }
Matt DeVilliere9786d42024-01-15 18:11:43 -0600120
121 printf("0x%02x: ", idx[k]);
122 val = regval(port, idx[k]);
123
Nico Huber3812a722016-10-12 12:12:51 +0200124 if (def[k] == NANA)
Matt DeVilliere9786d42024-01-15 18:11:43 -0600125 printf("(NA) 0x%02x\n", val);
Nico Huber3812a722016-10-12 12:12:51 +0200126 else if (def[k] == RSVD)
Matt DeVilliere9786d42024-01-15 18:11:43 -0600127 printf("(RR) 0x%02x\n", val);
Nico Huber3812a722016-10-12 12:12:51 +0200128 else if (def[k] == MISC)
Matt DeVilliere9786d42024-01-15 18:11:43 -0600129 printf("(MM) 0x%02x\n", val);
130 else {
131 if (def[k] == val)
132 printf("0x%02x 0x%02x\n", def[k], val);
133 else
134 printf("0x%02x [0x%02x]\n", def[k], val);
135 }
Nico Huber3812a722016-10-12 12:12:51 +0200136 }
137 } else {
138 printf("\nidx");
139 for (k = 0; idx[k] != EOT; k++) {
140 if (k && !(k % 8))
141 putchar(' ');
Frieder Ferlemanna422c2d2007-11-13 09:09:33 +0000142 printf(" %02x", idx[k]);
Nico Huber3812a722016-10-12 12:12:51 +0200143 }
144
145 printf("\nval");
146 for (k = 0; idx[k] != EOT; k++) {
147 if (k && !(k % 8))
148 putchar(' ');
149 printf(" %02x", regval(port, idx[k]));
150 }
151
152 printf("\ndef");
153 for (k = 0; def[k] != EOT; k++) {
154 if (k && !(k % 8))
155 putchar(' ');
156 if (def[k] == NANA)
157 printf(" NA");
158 else if (def[k] == RSVD)
159 printf(" RR");
160 else if (def[k] == MISC)
161 printf(" MM");
162 else
163 printf(" %02x", def[k]);
164 }
Uwe Hermann2c290e32007-09-20 00:00:49 +0000165 }
166 printf("\n");
167}
168
169void dump_superio(const char *vendor,
170 const struct superio_registers reg_table[],
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000171 uint16_t port, uint16_t id, uint8_t ldn_sel)
Uwe Hermann519419b2007-09-16 20:59:01 +0000172{
Uwe Hermann2c290e32007-09-20 00:00:49 +0000173 int i, j, no_dump_available = 1;
Uwe Hermann519419b2007-09-16 20:59:01 +0000174
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000175 if (!dump)
176 return;
177
Uwe Hermann519419b2007-09-16 20:59:01 +0000178 for (i = 0; /* Nothing */; i++) {
179 if (reg_table[i].superio_id == EOT)
180 break;
181
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000182 if ((uint16_t)reg_table[i].superio_id != id)
Uwe Hermann519419b2007-09-16 20:59:01 +0000183 continue;
184
Uwe Hermannde24a0e2007-09-19 00:03:14 +0000185 for (j = 0; /* Nothing */; j++) {
Uwe Hermann519419b2007-09-16 20:59:01 +0000186 if (reg_table[i].ldn[j].ldn == EOT)
187 break;
Uwe Hermann2c290e32007-09-20 00:00:49 +0000188 no_dump_available = 0;
Stefan Reinauer7a51e502008-12-01 14:18:57 +0000189 dump_regs(reg_table, i, j, port, ldn_sel);
Uwe Hermann519419b2007-09-16 20:59:01 +0000190 }
Uwe Hermann25a6c0f2007-09-19 00:48:42 +0000191
Uwe Hermann2c290e32007-09-20 00:00:49 +0000192 if (no_dump_available)
193 printf("No dump available for this Super I/O\n");
Uwe Hermann519419b2007-09-16 20:59:01 +0000194 }
195}
196
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000197void dump_io(uint16_t iobase, uint16_t length)
198{
199 uint16_t i;
200
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000201 printf("Dumping %d I/O mapped registers at base 0x%04x:\n",
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000202 length, iobase);
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000203 for (i = 0; i < length; i++)
204 printf("%02x ", i);
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000205 printf("\n");
Uwe Hermann83da8dc2009-03-25 17:38:40 +0000206 for (i = 0; i < length; i++)
207 printf("%02x ", INB(iobase + i));
Stefan Reinauer6df0c622009-03-11 14:48:20 +0000208 printf("\n");
209}
210
Guenter Roecka89da092012-06-29 12:23:50 -0700211void dump_data(uint16_t iobase, int bank)
212{
213 uint16_t i;
214
215 printf("Bank %d:\n", bank);
216 printf(" ");
217 for (i = 0; i < 16; i++)
218 printf("%02x ", i);
219 set_bank(iobase, bank);
220 for (i = 0; i < 256; i++) {
221 if (i % 16 == 0)
222 printf("\n%02x: ", i / 16);
223 printf("%02x ", datareg(iobase, i));
224 }
225 printf("\n");
226}
227
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000228void probing_for(const char *vendor, const char *info, uint16_t port)
Uwe Hermann2c290e32007-09-20 00:00:49 +0000229{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000230 if (!verbose)
231 return;
232
Uwe Hermann8b8d0392007-10-04 15:23:38 +0000233 /* Yes, there's no space between '%s' and 'at'! */
Uwe Hermanneec5ff42008-03-01 18:49:39 +0000234 printf("Probing for %s Super I/O %sat 0x%x...\n", vendor, info, port);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000235}
236
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000237/** Print a list of all supported chips from the given vendor. */
238void print_vendor_chips(const char *vendor,
239 const struct superio_registers reg_table[])
240{
241 int i;
242
243 for (i = 0; reg_table[i].superio_id != EOT; i++) {
244 printf("%s %s", vendor, reg_table[i].name);
245
246 /* Unless the ldn is empty, assume this chip has a dump. */
247 if (reg_table[i].ldn[0].ldn != EOT)
248 printf(" (dump available)");
249
250 printf("\n");
251 }
252
253 /* If we printed any chips for this vendor, put in a blank line. */
254 if (i != 0)
255 printf("\n");
256}
257
258/** Print a list of all chips supported by superiotool. */
259void print_list_of_supported_chips(void)
260{
261 int i;
262
263 printf("Supported Super I/O chips:\n\n");
264
265 for (i = 0; i < ARRAY_SIZE(vendor_print_functions); i++)
266 vendor_print_functions[i].print_list();
267
Paul Menzela8843de2017-06-05 12:33:23 +0200268 printf("See <https://coreboot.org/Superiotool#Supported_devices> "
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000269 "for more information.\n");
270}
271
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000272static void print_version(void)
273{
Ulf Jordan39a5bf72007-10-13 18:06:12 +0000274 printf("superiotool r%s\n", SUPERIOTOOL_VERSION);
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000275}
276
Uwe Hermann2046ff92007-09-01 21:02:44 +0000277int main(int argc, char *argv[])
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000278{
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000279 int i, j, opt, option_index;
Andriy Gaponb64aa602008-10-28 22:13:38 +0000280#if defined(__FreeBSD__)
281 int io_fd;
282#endif
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000283
Uwe Hermann246be7d2007-10-31 22:22:11 +0000284 static const struct option long_options[] = {
Uwe Hermanneddc4732007-09-20 23:57:44 +0000285 {"dump", no_argument, NULL, 'd'},
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000286 {"extra-dump", no_argument, NULL, 'e'},
Nico Huber3812a722016-10-12 12:12:51 +0200287 {"alternate-dump", no_argument, NULL, 'a'},
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000288 {"list-supported", no_argument, NULL, 'l'},
Uwe Hermanneddc4732007-09-20 23:57:44 +0000289 {"verbose", no_argument, NULL, 'V'},
290 {"version", no_argument, NULL, 'v'},
291 {"help", no_argument, NULL, 'h'},
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000292 {0, 0, 0, 0}
293 };
294
Nico Huber3812a722016-10-12 12:12:51 +0200295 while ((opt = getopt_long(argc, argv, "dealVvh",
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000296 long_options, &option_index)) != EOF) {
297 switch (opt) {
298 case 'd':
299 dump = 1;
300 break;
Ronald Hoogenboom0be73bb2008-02-25 22:32:41 +0000301 case 'e':
302 extra_dump = 1;
303 break;
Nico Huber3812a722016-10-12 12:12:51 +0200304 case 'a':
305 alternate_dump = 1;
306 break;
Robinson P. Tryon552cfb72008-01-15 22:30:55 +0000307 case 'l':
308 print_list_of_supported_chips();
309 exit(0);
310 break;
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000311 case 'V':
312 verbose = 1;
313 break;
314 case 'v':
Robinson P. Tryonec1edd12007-10-02 23:32:21 +0000315 print_version();
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000316 exit(0);
317 break;
318 case 'h':
Uwe Hermanne4749562007-09-19 16:26:18 +0000319 printf(USAGE);
Uwe Hermann969a9f62008-03-17 13:43:48 +0000320 printf(USAGE_INFO);
Uwe Hermann3acf31e2007-09-19 01:55:35 +0000321 exit(0);
322 break;
323 default:
324 /* Unknown option. */
325 exit(1);
326 break;
327 }
328 }
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000329
Andriy Gaponb64aa602008-10-28 22:13:38 +0000330#if defined(__FreeBSD__)
331 if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
332 perror("/dev/io");
Fabian Groffen318ddb82023-03-10 18:12:25 +0100333#elif defined(__NetBSD__)
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000334 if (iopl(3) < 0) {
335 perror("iopl");
Fabian Groffen318ddb82023-03-10 18:12:25 +0100336#else
337 if (ioperm(0, 6000, 1) < 0) {
338 perror("ioperm");
Andriy Gaponb64aa602008-10-28 22:13:38 +0000339#endif
Uwe Hermann7e7e9ac2007-09-19 15:52:23 +0000340 printf("Superiotool must be run as root.\n");
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000341 exit(1);
342 }
343
Uwe Hermannd937b522007-10-17 23:42:02 +0000344 print_version();
345
Carl-Daniel Hailfingerbb38f322010-01-24 01:40:46 +0000346#ifdef PCI_SUPPORT
347 /* Do some basic libpci init. */
348 pacc = pci_alloc();
349 pci_init(pacc);
350 pci_scan_bus(pacc);
351#endif
352
Uwe Hermannd754d2c2007-09-18 23:30:24 +0000353 for (i = 0; i < ARRAY_SIZE(superio_ports_table); i++) {
354 for (j = 0; superio_ports_table[i].ports[j] != EOT; j++)
355 superio_ports_table[i].probe_idregs(
356 superio_ports_table[i].ports[j]);
357 }
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000358
Uwe Hermanne9d46162007-10-07 20:01:23 +0000359 if (!chip_found)
360 printf("No Super I/O found\n");
361
Andriy Gaponb64aa602008-10-28 22:13:38 +0000362#if defined(__FreeBSD__)
363 close(io_fd);
364#endif
Stefan Reinauer6a5bc462007-01-17 10:57:42 +0000365 return 0;
Ronald G. Minnich394e7c42006-02-22 22:12:21 +0000366}